Parsing

Warning

If you are using this package to implement dice rolling, you should never have to directly interact with these classes. These are only documented to help with maintenance.

Parsing is the act of matching tokens, in this case produced by a lexer, to a set of rules for execution.

YADN is maybe a bit over complex. This implementation has three different parsers to handle its subsyntaxes.

class yadr.parser.Parser[source]

A state machine for parsing YADN.

How Parsing Works

Essentially, parsing turns an ordered list of tokens into a tree structure for execution. For example, let’s say we have the YADN expression:

3 * ( 4 - 2 )

That is lexed into the tokens:

Token(NUMBER, 3)
Token(MD_OPERATOR, '*')
Token(GROUP_OPEN, '(')
Token(NUMBER, 4)
Token(AS_OPERATOR, '-')
Token(NUMBER, 2)
Token(GROUP_CLOSE, ')')

Which is then parsed into the tree:

    *
   / \
  -   3
 / \
4   2

Where each token ends up in each tree is dependent on the Order of Operations defined by YADN.

parse(tokens: Sequence[tuple[Token, int | bool | str | tuple[int, ...] | tuple[str, dict[int, int | str]]]]) None | int | bool | str | tuple[int, ...] | tuple[str, dict[int, int | str]] | CompoundResult[source]

Parse one or more die rolls.

Parameters:

tokens – A sequence of lexed YADN tokens to parse.

Returns:

A class defined in either yadr.model.Result or yadr.model.CompoundResult.

Return type:

Result | CompoundResult

class yadr.maps.Parser[source]

A state machine for parsing YADN dice maps.

parse(tokens: tuple[tuple[Token, int | bool | str | tuple[int, ...] | tuple[str, dict[int, int | str]]], ...]) tuple[str, dict[int, int | str]][source]

Parse YADN dice mapping tokens.

Parameters:

tokens – A dice map as a sequence of YADN tokens to parse.

Returns:

A class defined in yadr.model.NamedMap.

Return type:

tuple

class yadr.pools.Parser[source]

A state machine for parsing YADN dice pools.

parse(tokens: Sequence[tuple[Token, int | bool | str | tuple[int, ...] | tuple[str, dict[int, int | str]]]]) tuple[int, ...][source]

Parse YADN pool tokens.