Abstract Syntax Tree

This page documents the structure of the Expression object as returned by parse(...). If you’re looking for the Expression object returned by roll(...)expr, check out Expression Tree.

class d20.ast.ChildMixin

A mixin that tree nodes must implement to support tree traversal utilities.

property children

(read-only) The children of this object, usually used for traversing a tree.

Return type

list[Node]

property left

The leftmost child of this object, usually used for traversing a tree.

Return type

Node

property right

The rightmost child of this object, usually used for traversing a tree..

Return type

Node

set_child(index, value)

Sets the ith child of this object.

Parameters
  • index (int) – The index of the value to set.

  • value (ChildMixin) – The new value to set it to.

class d20.ast.Node

Bases: abc.ABC, d20.diceast.ChildMixin

The base class for all AST nodes.

A Node has no specific attributes, but supports all the methods in ChildMixin for traversal.

class d20.ast.Expression(roll, comment=None)

Bases: d20.diceast.Node

Expressions are usually the root of all ASTs.

roll: d20.ast.Node

The subtree representing the expression’s roll.

comment: Optional[str]

The comment of this expression.

class d20.ast.AnnotatedNumber(value, *annotations)

Bases: d20.diceast.Node

Represents a value with an annotation.

value: d20.ast.Node

The subtree representing the annotated value.

annotations: list[str]

The annotations on the value.

class d20.ast.Literal(value)

Bases: d20.diceast.Node

value: Union[int, float]

The literal number represented.

class d20.ast.Parenthetical(value)

Bases: d20.diceast.Node

value: d20.ast.Node

The subtree inside the parentheses.

class d20.ast.UnOp(op, value)

Bases: d20.diceast.Node

op: str

The unary operation.

value: d20.ast.Node

The subtree that the operation operates on.

class d20.ast.BinOp(left, op, right)

Bases: d20.diceast.Node

op: str

The binary operation.

left: d20.ast.Node

The left subtree that the operation operates on.

right: d20.ast.Node

The right subtree that the operation operates on.

class d20.ast.OperatedSet(the_set, *operations)

Bases: d20.diceast.Node

value: d20.ast.NumberSet

The set to be operated on.

operations: list[d20.SetOperation]

The operations to run on the set.

class d20.ast.NumberSet(values)

Bases: d20.diceast.Node

values: list[d20.ast.NumberSet]

The elements of the set.

class d20.ast.OperatedDice(the_dice, *operations)

Bases: d20.diceast.OperatedSet

class d20.ast.Dice(num, size)

Bases: d20.diceast.Node

num: int

The number of dice to roll.

size: int

How many sides each die should have.

class d20.ast.SetOperator(op, sels)
op: str

The operation to run on the selected elements of the set.

sels: list[d20.SetSelector]

The selectors that describe how to select operands.

class d20.ast.SetSelector(cat, num)
cat: Optional[str]

The type of selection (lowest, highest, literal, etc).

num: int

The number to select (the N in lowest/highest/etc N, or literally N).