Skip to content

Commit

Permalink
Fix several pylint warnings/errors, as well as add test cases for #23
Browse files Browse the repository at this point in the history
…and #26.

Signed-off-by: HE, Tao <sighingnow@gmail.com>
  • Loading branch information
sighingnow committed Jan 19, 2019
1 parent 6a855d4 commit b1872d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/parsec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def parse_strict(self, text):
result value, else raise a ParseError.
The difference between `parse` and `parse_strict` is that whether entire
given text must be used.'''
# pylint: disable=comparison-with-callable
# Here the `<` is not comparison.
return (self < eof()).parse_partial(text)[0]

def bind(self, fn):
Expand Down Expand Up @@ -216,10 +218,11 @@ def ends_with_parser(text, index):
def parsecmap(self, fn):
'''Returns a parser that transforms the produced value of parser with `fn`.'''
return self.bind(lambda res: Parser(lambda _, index: Value.success(index, fn(res))))

def parsecapp(self, other):
'''Returns a parser that applies the produced value of this parser to the produced value of `other`.'''
return self.bind(lambda f: other.parsecmap(lambda x: f(x)))
# pylint: disable=unnecessary-lambda
return self.bind(lambda res: other.parsecmap(lambda x: res(x)))

def result(self, res):
'''Return a value according to the parameter `res` when parse successfully.'''
Expand Down Expand Up @@ -330,10 +333,14 @@ def parsecmap(p, fn):
'''Returns a parser that transforms the produced value of parser with `fn`.'''
return p.parsecmap(fn)


def parsecapp(p, other):
'''Returns a parser that applies the produced value of this parser to the produced value of `other`.'''
'''Returns a parser that applies the produced value of this parser to the produced value of `other`.
There should be an operator `(<*>)`, but that is impossible in Python.
'''
return p.parsecapp(other)


def result(p, res):
'''Return a value according to the parameter `res` when parse successfully.'''
return p.result(res)
Expand All @@ -348,9 +355,10 @@ def desc(p, description):
'''Describe a parser, when it failed, print out the description text.'''
return p.desc(description)


##########################################################################
# Parser Generator
##
#
# The most powerful way to construct a parser is to use the generate decorator.
# the `generate` creates a parser from a generator that should yield parsers.
# These parsers are applied successively and their results are sent back to the
Expand Down Expand Up @@ -383,6 +391,7 @@ def generated(text, index):
return Value.success(index, endval)
return generated.desc(fn.__name__)


##########################################################################
# Text.Parsec.Combinator
##########################################################################
Expand Down Expand Up @@ -414,7 +423,7 @@ def times_parser(text, index):


def count(p, n):
'''`count n p` parses n occurrences of p. If n is smaller or equal to zero,
'''`count p n` parses n occurrences of p. If n is smaller or equal to zero,
the parser equals to return []. Returns a list of n values returned by p.'''
return times(p, n, n)

Expand All @@ -430,7 +439,7 @@ def optional_parser(text, index):
if res.status:
return Value.success(res.index, res.value)
else:
'''Return None without doing anything.'''
# Return the maybe existing default value without doing anything.
return Value.success(res.index, default_value)
return optional_parser

Expand Down Expand Up @@ -530,6 +539,7 @@ def sepEndBy1(p, sep):
optionally ended by `sep`. Returns a list of values returned by `p`.'''
return separated(p, sep, 1, maxt=float('inf'))


##########################################################################
# Text.Parsec.Char
##########################################################################
Expand Down
15 changes: 14 additions & 1 deletion tests/test_parsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,19 @@ def test_ends_with(self):
def test_parsecmap(self):

def mapfn(p):
return p+p
return p + p

parser = string('x').parsecmap(mapfn)
self.assertEqual(parser.parse('x'), 'xx')

def test_parsecapp(self):

def genfn(p):
return lambda c: 'fn:' + p + c + c

parser = string('x').parsecmap(genfn).parsecapp(string('y'))
self.assertEqual(parser.parse('xy'), 'fn:xyy')

def test_desc(self):
parser = string('x')
self.assertEqual(parser.parse('x'), 'x')
Expand Down Expand Up @@ -170,6 +178,11 @@ def test_optional(self):
self.assertEqual(parser.parse('xx'), 'xx')
self.assertEqual(parser.parse('xy'), None)

def test_optional_default(self):
parser = optional(string('xx'), 'k')
self.assertEqual(parser.parse('xx'), 'xx')
self.assertEqual(parser.parse('xy'), 'k')

def test_many(self):
parser = many(letter())
self.assertEqual(parser.parse('x'), ['x'])
Expand Down

0 comments on commit b1872d2

Please sign in to comment.