The dice notation to parse
The root AST node, with start/end spans on every node
INVALID_NOTATION_TYPE when notation is not a
string — raised before lexing, so it carries no position
import { parse } from 'roll-parser';
parse('2d6+3');
// {
// type: 'BinaryOp', operator: '+', start: 0, end: 5,
// left: {
// type: 'Dice', start: 0, end: 3,
// count: { type: 'Literal', value: 2, start: 0, end: 1 },
// sides: { type: 'Literal', value: 6, start: 2, end: 3 },
// },
// right: { type: 'Literal', value: 3, start: 4, end: 5 },
// }
count and sides are full nodes, not numbers — that is what makes
computed dice like (1d4)d6 expressible.
Parses a dice notation string into an ASTNode tree, lexing it first.
evaluate never mutates the AST, so parse once and evaluate many times when rolling the same notation repeatedly — that skips the lexer and parser on every roll after the first. Use
parsealone to validate notation without consuming randomness.