Class Reference
Looking for information on d20.Expression
or d20.ast.Node
? Check out
Expression Tree for information on the Expression returned by a roll, or
Abstract Syntax Tree for the AST returned by a parse.
Dice
- class d20.Roller(context: RollContext | None = None)[source]
The main class responsible for parsing dice into an AST and evaluating that AST.
- context: d20.RollContext
The class used to track roll limits.
- parse(expr: str, allow_comments: bool = False) Expression [source]
Parses a dice expression into an AST.
- Parameters:
- Return type:
- roll(expr: str | ASTNode, stringifier: Stringifier | None = None, allow_comments: bool = False, advantage: AdvType = AdvType.NONE) RollResult [source]
Rolls the dice.
- Parameters:
stringifier (d20.Stringifier) – The stringifier to stringify the result. Defaults to MarkdownStringifier.
allow_comments (bool) – Whether to parse for comments after the main roll expression (potential slowdown)
advantage (AdvType) – If the roll should be made at advantage. Only applies if the leftmost node is 1d20.
- Return type:
- class d20.RollResult(the_ast: ASTNode, the_roll: Expression, stringifier: Stringifier)[source]
Holds information about the result of a roll. This should generally not be constructed manually.
- ast: d20.ast.Node
The abstract syntax tree of the dice expression that was rolled.
- expr: d20.Expression
The Expression representation of the result of the roll.
- comment: str or None
If
allow_comments
was True and a comment was found, the comment. Otherwise, None.
- property crit: CritType
If the leftmost node was Xd20kh1, returns
CritType.CRIT
if the roll was a 20 andCritType.FAIL
if the roll was a 1. ReturnsCritType.NONE
otherwise.- Return type:
- class d20.RollContext(max_rolls=1000)[source]
A class to track information about rolls to ensure all rolls halt eventually.
To use this class, pass an instance to the constructor of
d20.Roller
.- count_roll(n=1)[source]
Called each time a die is about to be rolled.
- Parameters:
n (int) – The number of rolls about to be made.
- Raises:
d20.TooManyRolls – if the roller should stop rolling dice because too many have been rolled.
Stringifiers
- class d20.Stringifier[source]
ABC for string builder from dice result. Children should implement all
_str_*
methods to transform an Expression into a str.- stringify(the_roll: ExpressionNode) str [source]
Transforms a rolled expression into a string recursively, bottom-up.
- Parameters:
the_roll (d20.Expression) – The expression to stringify.
- Return type:
- _stringify(node: ExpressionNode) str [source]
Called on each node that needs to be stringified.
- Parameters:
node (d20.Number) – The node to stringify.
- Return type:
- _str_expression(node: Expression) str [source]
- Parameters:
node (d20.Expression) – The node to stringify.
- Return type:
- _str_literal(node: Literal) str [source]
- Parameters:
node (d20.Literal) – The node to stringify.
- Return type:
- _str_parenthetical(node: Parenthetical) str [source]
- Parameters:
node (d20.Parenthetical) – The node to stringify.
- Return type:
Errors
- class d20.RollError(msg)[source]
Generic exception happened in the roll. Base exception for all library exceptions.