Parsers

Basic parsers

Primitive input-agnostic parsers.

class reparsec.primitive.Pure(x)

Parser that always succeeds, consumes no input, and returns constant value.

>>> from reparsec.primitive import Pure
>>> Pure(0).parse("").unwrap()
0
Parameters:

x (TypeVar(A_co, covariant=True)) – Value to return

class reparsec.primitive.PureFn(fn)

Parser that always succeeds, consumes no input, and returns the result of function.

>>> from reparsec.primitive import PureFn
>>> PureFn(lambda: list()).parse("").unwrap()
[]
Parameters:

fn (Callable[[], TypeVar(A_co, covariant=True)]) – Function that produces a value to return

reparsec.primitive.unexpected(expected)

Parser that always fails and consumes no input.

>>> from reparsec.primitive import unexpected
>>> unexpected("a").parse("").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: expected a
Parameters:

expected (str) – Error label

Return type:

TupleParser[object, None]

Parsers for sequences

Parsers for arbitrary sequences.

reparsec.sequence.eof()

Succeeds at the end of the input. :rtype: TupleParser[Sized, None]

>>> from reparsec.sequence import eof
>>> eof().parse("").unwrap()
>>> eof().parse("a").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: expected end of file
reparsec.sequence.satisfy(test)

Succeeds for sequence element for which test returns True and returns that element.

>>> from reparsec.sequence import satisfy
>>> parser = satisfy(lambda c: c.isalpha())
>>> parser.parse("a").unwrap()
'a'
>>> parser.parse("0").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: unexpected input
Parameters:

test (Callable[[TypeVar(A)], bool]) – Predicate for sequence elements

Return type:

TupleParser[Sequence[TypeVar(A)], TypeVar(A)]

reparsec.sequence.sym(s, label=None)

Parses s and returns the parsed element.

>>> from reparsec.sequence import sym
>>> sym("a").parse("a").unwrap()
'a'
>>> sym("a").parse("0").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: expected 'a'
Parameters:
  • s (TypeVar(A)) – Value to parse

  • label (Optional[str]) – Label to use instead of repr(s)

Return type:

TupleParser[Sequence[TypeVar(A)], TypeVar(A)]

Parsers for scannerless parsing

Parsers for scannerless parsing of strings.

reparsec.scannerless.literal(s)

Parses the string s and returns it.

>>> from reparsec.scannerless import literal
>>> parser = literal("ab")
>>> parser.parse("ab").unwrap()
'ab'
>>> parser.parse("ac").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: expected 'ab'
Parameters:

s (str) – String to parse

Return type:

TupleParser[str, str]

reparsec.scannerless.regexp(pat, group=0)

Parses the prefix of input that matches pat and returns the value of group.

>>> from reparsec.scannerless import regexp
>>> parser = regexp("a(.)", 1)
>>> parser.parse("ab").unwrap()
'b'
>>> parser.parse("bb").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 0: unexpected input
Parameters:
  • pat (str) – Regular expression

  • group (Union[int, str]) – Group index or name

Return type:

TupleParser[str, str]

reparsec.scannerless.parse(parser, stream, recover=False)

Wrapper around reparsec.Parser.parse() that enables line and column tracking for scannerless parsers.

>>> from reparsec.scannerless import literal, parse
>>> parser = literal("a\n") + literal("b") + literal("c")
>>> parser.parse("a\nbb").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 3: expected 'c'
>>> parse(parser, "a\nbb").unwrap()
Traceback (most recent call last):
  ...
reparsec.types.ParseError: at 2:2: expected 'c'
Parameters:
  • parser (Parser[str, TypeVar(A)]) – Parser to run

  • stream (str) – String to parse

  • recover (bool) – Flag to enable error recovery

Return type:

ParseResult[TypeVar(A), str]