Skip to content

Refs

The docs

ParsecBasic

ParsecBasic is just the basic callable interface

Parsec

Parsec combinator fluid interface and constructors.

Examples:

>>> number = Parsec.from_re(re.compile(r"-?\d+")).map(int)
>>> number("123")
(123, '')
>>> number("-69")
(-69, '')

from_func classmethod

from_func(parser: ParsecBasic[_T])

Creates an instance of Parsec[T] from a callable.

Parameters:

Name Type Description Default
parser ParserBasic[_T]

A callable parser

required

Examples:

Parser that consumes an string if it starts with "foo"

>>> literal = lambda x: lambda s: (x, s[len(x):]) if s.startswith(x) else None
>>> parser = Parsec.from_func(literal("foo"))
>>> parser("foobar")
('foo', 'bar')
>>> parser("bar")  # returns None

__or__

__or__(other: ParsecBasic[_R]) -> Parsec[_T | _R]

Creates a new Parsec instance that operates a disjunction between the two Parsec instances provided. In practice it will try to apply one first and if it fails it will try to apply the second.

Examples:

>>> true = Parsec.from_string("True").map(lambda _: True)
>>> false = Parsec.from_string("False").map(lambda _: False)
>>> parser = true | false
>>> parser("True")
(True, '')
>>> parser("False")
(False, '')
>>> parser("Trulse")  # Guess what

__and__

__and__(other: ParsecBasic[_R]) -> Parsec[Tuple[_T, _R]]

Creates a new Parsec instance that operates a conjunction between the two Parsec instances provided. In practice it will apply both parsers and return a the combination of the results as a tuple.

>>> one = Parsec.from_string("1").map(int)
>>> two = Parsec.from_string("2").map(int)
>>> three = Parsec.from_string("3").map(int)
>>> parser = one & two & three  # The same as (one & two) & three
>>> parser("123")
(((1, 2), 3), '')
>>> parser("321")  # Uff

map

map(mapping: Callable[[_T], _R]) -> Parsec[_R]

Creates a new Parsec instance that applies the provided mapping function

from_re staticmethod

from_re(r: re.Pattern[str]) -> Parsec[str]

Creates a new Parsec instance that consumes a string that matches the provided regular expression.

Examples:

>>> number = Parsec.from_re(re.compile(r"-?\d+")).map(int)
>>> number("123")
(123, '')
>>> number("-69")
(-69, '')

Parameters:

Name Type Description Default
r re.Pattern[str]

A regular expression pattern

required

Returns:

Type Description
Parsec[str]

A new Parsec instance that consumes a string that matches the provided

Parsec[str]

regular expression.

from_string staticmethod

from_string(x: str) -> Parsec[str]

Creates a new Parsec instance that consumes a string that starts with the provided string.

Examples:

>>> one = Parsec.from_string("1").map(int)
>>> one("123")
(1, '23')
>>> one("321")  # Uff

Parameters:

Name Type Description Default
x str

A string

required

Returns:

Type Description
Parsec[str]

A new Parsec instance that consumes a string that starts with the provided

Parsec[str]

string.

ignore

ignore() -> Parsec[None]

Returns a new Parsec instance that will ignore the result of the current instance. It's current implementation is equivalent to self.map(lambda _: None).

maybe

maybe() -> Parsec[_T | None]

Returns a new Parsec instance that will recover in case the current instance fails. It's current implementation is equivalent to self.else_(lambda: None).

Example:

>>> one = Parsec.from_string("1").map(int).maybe()
>>> one("1")
(1, '')
>>> one("2")
(None, '2')

many

many() -> Parsec[List[_T]]

Returns a new Parsec instance that will consume the current parser many times. If it can't consume anything it will return an empty list.

else_

else_(f: Callable[[], _R]) -> Parsec[_T | _R]

Returns a new Parsec instance that will recover in case the current instance fails.

constant staticmethod

constant(f: Callable[[], _T]) -> Parsec[_T]

Returns a new Parsec instance that will always return the result of the provided function.

sep_by

sep_by(sep: Parsec[Any]) -> Parsec[List[_T]]

Returns a parser that will consume the current parser many times separated by sep. If it can't consume anything it will return an empty list.

Parameters:

Name Type Description Default
sep Parsec[Any]

Separator

required

Examples:

>>> bit = (
...     Parsec.from_string("0") | Parsec.from_string("1")
... ).map(int)
>>> bit_array = bit.sep_by(Parsec.from_string(","))
>>> bit_array("1,1,0")
([1, 1, 0], '')
>>> bit_array("")
([], '')