roll-parser - v3.4.0
    Preparing search index...

    Function lex

    • Tokenizes a dice notation string. The first stage of the pipeline — parse calls it for you; reach for lex directly only to build a syntax highlighter or an editor integration.

      Notation is case-insensitive and whitespace-tolerant: 2D20 + 5 and 2d20+5 produce the same tokens, and identifier tokens carry a lowercased value. The one exception is @name, whose case is preserved.

      Parameters

      • input: string

        The dice notation to tokenize

      Returns Token[]

      Every token in source order, always ending with one TokenType.EOF token

      If an invalid character or unknown identifier is found

      INVALID_NOTATION_TYPE when input is not a string — raised before scanning, so it carries no position

      import { lex, TokenType } from 'roll-parser';

      const tokens = lex('2d20+5');
      tokens.length; // 6 — NUMBER DICE NUMBER PLUS NUMBER EOF
      tokens[0]; // { type: TokenType.NUMBER, value: '2', position: 0, end: 1 }
      tokens[1].type === TokenType.DICE; // true
      tokens.at(-1)?.type === TokenType.EOF; // true