API Doc

dine.exceptions module

exception dine.exceptions.InvalidBranchException

Bases: ParseException

exception dine.exceptions.ParseException(msg)

Bases: Exception

dine.parser module

class dine.parser.Parser(parse_fn, *, label)

Bases: Generic[A]

and_then(other)

Parses A and then B

Parameters

other (Parser[B]) – B parser

Return type

Parser[Tuple[A, B]]

apply(f_parser)

Functional apply function for parsers

This function takes a parser that parses a converting function from type A to type B and converts it into a function that converts a parser of A to a parser of B.

Parameters

f_parser (Parser[Callable[[A], B]]) – the converting function

Returns

the parser for an object of type B

Return type

Callable[[Parser[A]], Parser[B]]

static ascii()

Parser that parses an ASCII character

Return type

Parser[str]

static ascii_lowercase()

Parser that parses a lowercase ASCII character

Return type

Parser[str]

static ascii_uppercase()

Parser that parses a lowercase ASCII character

Return type

Parser[str]

bind(f)

This method may be used to put a parser after the current parser, with the later parser potentially depends on the output of the current parser.

This method passes the output of the current parser to the function f to create a new parser. This new parser will take the remaining of the parse stream if the current parser parses successfully. This method is akin to the bind function in functional programming.

Parameters

f (Callable[[A], Parser[B]]) – a parser creating function that takes an object of type A (the type of the current parser) and create a parser of type B. If the current parser successfully parses, the B parser then takes the remaining stream from the result of the current parser.

Return type

A parser of type B

static char(char)

Parser for a single character

Parameters

char (str) – the character to parse

Return type

Parser[str]

static choice(parsers)

Parser that parses the first matching alternative

Parser for a list of alternatives. The first matching alternative is parsed.

Parameters

parsers (Iterable[Parser]) – A sequence of parsers for the alternatives

Return type

Parser

static digit()

Parser that parses a digit

Return type

Parser[str]

static digit_nonzero()

Parser that parses a nonzero digit

Return type

Parser[str]

static just(a)

A parser that always parses successfully and returns the desired value without affecting the parse stream

Parameters

a (A) – the value

Returns

A parser that always successfully parses the value while keeping the parse stream unchanged

Return type

Parser[A]

many0()

Parses 0 or more times

Returns

Parser that parses A 0 or more times

Return type

Parser[list]

many0_recur(s)
Return type

Tuple[list, Stream]

many0_sep_by(sep_parser)

Parses 0 or more times with separator

Parses A 0 or more times with a separator between each pair of occurences.

Parameters

sep_parser (Parser) – parser for the separator

Return type

Parser[list[A]]

many1()

Parses 1 or more times

Return type

Parser[list]

many1_sep_by(sep_parser)

Parses 1 or more times with separator

Parses A 1 or more times with a separator between each pair of occurences.

Parameters
  • val_parser (Parser[A]) – parser for the value

  • sep_parser (Parser) – parser for the separator

Return type

Parser[list[A]]

map(f)

If the parser parses successfully, pass the result to a function

Parameters

f (Callable[[A], B]) – the function that takes the output of the parser and convert it to a new object

Return type

Parser[B]

optional(default=None)

Parses 0 or 1 time

Returns

Parser that parses A 0 or 1 time

Return type

Parser[list]

or_else(other)

Parses A or B

Parameters

other (Parser[B]) – B parser

Return type

Parser[Union[A, B]]

preceded_by(other)

Parses A preceded by something

Parameters

other (Parser) – parser for the thing preceding A

Return type

Parser[A]

static satisfy(predicate, *, label='satisfy')

Parser that parses the next character if the predicate is True

Parameters
  • label (str) – label of the parser

  • predicate (Callable[[str], bool]) – the condition on which the next character is parsed or not

Return type

Parser[str]

static sequence(parsers)

Parser for a sequence of things

Parameters

parsers (Iterable[Parser]) – A sequence of parsers

Return type

Parser[list]

set_label(label)
Return type

Parser[TypeVar(A)]

static string(literal)

Parser that parses a string literal

Parameters

literal (str) – The string literal

Return type

Parser[str]

succeeded_by(other)

Parses A preceded by something

Parameters

other (Parser) – parser for the thing succeeding A

Return type

Parser[A]

surrounded_by(lparser, rparser)

Parses A surrounded by two other things

Parameters
  • lparser (Parser) – parser for the left thing

  • rparser (Parser) – parser for the right thing

Return type

Parser[A]

times(n)

Parses A exactly n times

Parameters

n (int) – return

Return type

Parser[A]

static until(predicate, label='until')

Parser that parses until a predicate is true

Parameters
  • label (str) – the label of the parser

  • predicate (Callable[[str], bool]) – the condition on which the parser stops

Return type

Parser[str]

dine.result module

class dine.result.ParseFailure(loc, label, msg)

Bases: ParseResult

Result of the parser when it does not parse successfully

class dine.result.ParseResult(loc)

Bases: Generic[A]

Base class for parse result

class dine.result.ParseSuccess(loc, val, rs)

Bases: ParseResult[A]

Result of the parser when it parses successfully

dine.stream module

class dine.stream.Location(line, col)

Bases: object

class dine.stream.Stream(buf, begin=0)

Bases: object

head()

Next character in the parse stream and its location

Returns

next character in the parse stream and its location, or None if the stream is exhausted

Return type

Optional[Tuple[str, Location]]

remain()
tail()

The tail of the parse stream

Returns

a new stream including the remaining characters after the head of the current stream

Return type

Stream