Documentation for the Code¶
A universal Python parser combinator library inspired by Parsec library of Haskell.
-
exception
parsec.
ParseError
(expected, text, index)¶ Parser error.
-
loc
()¶ Locate the error position in the source code text.
-
static
loc_info
(text, index)¶ Location of index in source code text.
-
-
class
parsec.
Parser
(fn)¶ A Parser is an object that wraps a function to do the parsing work. Arguments of the function should be a string to be parsed and the index on which to begin parsing. The function should return either Value.success(next_index, value) if parsing successfully, or Value.failure(index, expected) on the failure.
-
bind
(fn)¶ This is the monadic binding operation. Returns a parser which, if parser is successful, passes the result to fn, and continues with the parser returned from fn.
-
choice
(other)¶ (|) This combinator implements choice. The parser p | q first applies p. If it succeeds, the value of p is returned. If p fails without consuming any input, parser q is tried. NOTICE: without backtrack.
-
compose
(other)¶ (>>) Sequentially compose two actions, discarding any value produced by the first.
-
desc
(description)¶ Describe a parser, when it failed, print out the description text.
-
ends_with
(other)¶ (<) Ends with a specified parser, and at the end parser hasn’t consumed any input.
-
joint
(*parsers)¶ (+) Joint two or more parsers into one. Return the aggregate of two results from this two parser.
-
mark
()¶ Mark the line and column information of the result of this parser.
-
parse
(text)¶ Parser a given string text.
-
parse_partial
(text)¶ Parse the longest possible prefix of a given string. Return a tuple of the result value and the rest of the string. If failed, raise a ParseError.
-
parse_strict
(text)¶ Parse the longest possible prefix of the entire given string. If the parser worked successfully and NONE text was rested, return the result value, else raise a ParseError. The difference between parse and parse_strict is that whether entire given text must be used.
-
parsecmap
(fn)¶ Returns a parser that transforms the produced value of parser with fn.
-
result
(res)¶ Return a value according to the parameter res when parse successfully.
-
skip
(other)¶ (<<) Ends with a specified parser, and at the end parser consumed the end flag.
-
try_choice
(other)¶ (^) Choice with backtrack. This combinator is used whenever arbitrary look ahead is needed. The parser p || q first applies p, if it success, the value of p is returned. If p fails, it pretends that it hasn’t consumed any input, and then parser q is tried.
-
-
class
parsec.
Value
¶ Represent the result of the Parser.
-
aggregate
(other=None)¶ collect the furthest failure from self and other.
-
static
combinate
(values)¶ aggregate multiple values into tuple
-
static
failure
(index, expected)¶ Create failure value.
-
static
success
(index, actual)¶ Create success value.
-
-
parsec.
bind
(p, fn)¶ Bind two parsers, implements the operator of (>>=).
-
parsec.
choice
(pa, pb)¶ Choice one from two parsers, implements the operator of (|).
-
parsec.
compose
(pa, pb)¶ Compose two parsers, implements the operator of (>>).
-
parsec.
count
(p, n)¶ count n p 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.
-
parsec.
desc
(p, description)¶ Describe a parser, when it failed, print out the description text.
-
parsec.
digit
()¶ Parse a digit character.
-
parsec.
endBy
(p, sep)¶ endBy(p, sep) parses zero or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.
-
parsec.
endBy1
(p, sep)¶ endBy1(p, sep) parses one or more occurrences of `p, separated and ended by sep. Returns a list of values returned by p.
-
parsec.
ends_with
(pa, pb)¶ Ends with a specified parser, and at the end parser hasn’t consumed any input. Implements the operator of (<).
-
parsec.
eof
()¶ Parser EOF flag of a string.
-
parsec.
generate
(fn)¶ Parser generator. (combinator syntax).
-
parsec.
joint
(*parsers)¶ Joint two or more parsers, implements the operator of (+).
-
parsec.
letter
()¶ Parse a letter in alphabet.
-
parsec.
many
(p)¶ Repeat a parser 0 to infinity times. DO AS MUCH MATCH AS IT CAN. Return a list of values.
-
parsec.
many1
(p)¶ Repeat a parser 1 to infinity times. DO AS MUCH MATCH AS IT CAN. Return a list of values.
-
parsec.
mark
(p)¶ Mark the line and column information of the result of the parser p.
-
parsec.
none_of
(s)¶ Parser a char NOT from specified string.
-
parsec.
one_of
(s)¶ Parser a char from specified string.
-
parsec.
parse
(p, text, index)¶ Parse a string and return the result or raise a ParseError.
-
parsec.
parsecmap
(p, fn)¶ Returns a parser that transforms the produced value of parser with fn.
-
parsec.
regex
(exp, flags=0)¶ Parser according to a regular expression.
-
parsec.
result
(p, res)¶ Return a value according to the parameter res when parse successfully.
-
parsec.
sepBy
(p, sep)¶ sepBy(p, sep) parses zero or more occurrences of p, separated by sep. Returns a list of values returned by p.
-
parsec.
sepBy1
(p, sep)¶ sepBy1(p, sep) parses one or more occurrences of p, separated by sep. Returns a list of values returned by p.
-
parsec.
sepEndBy
(p, sep)¶ sepEndBy(p, sep) parses zero or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.
-
parsec.
sepEndBy1
(p, sep)¶ sepEndBy1(p, sep) parses one or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.
-
parsec.
separated
(p, sep, mint, maxt=None, end=None)¶ Repeat a parser p separated by s between mint and maxt times. When end is None, a trailing separator is optional. When end is True, a trailing separator is required. When end is False, a trailing separator is not allowed. MATCHES AS MUCH AS POSSIBLE. Return list of values returned by p.
-
parsec.
skip
(pa, pb)¶ Ends with a specified parser, and at the end parser consumed the end flag. Implements the operator of (<<).
-
parsec.
space
()¶ Parser a whitespace character.
-
parsec.
spaces
()¶ Parser zero or more whitespace characters.
-
parsec.
string
(s)¶ Parser a string.
-
parsec.
times
(p, mint, maxt=None)¶ Repeat a parser between mint and maxt times. DO AS MUCH MATCH AS IT CAN. Return a list of values.
-
parsec.
try_choice
(pa, pb)¶ Choice one from two parsers with backtrack, implements the operator of (^).