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.Resultoryadr.model.CompoundResult.- Return type:
Result | CompoundResult