Skip to content

epic: Roll Parser v3 - TypeScript Rewrite #9

@edloidas

Description

@edloidas

Complete TypeScript rewrite of roll-parser using Pratt Parser architecture, targeting Bun runtime for high-performance dice notation parsing.


Motivation

v2 limitations:

  • JavaScript codebase with inconsistent typing
  • PEG-based parser with cold-start penalty
  • Legacy tooling (Jest, ESLint, Travis CI)
  • Limited modifier support

v3 goals:

  • Performance: Pratt Parser outperforms recursive descent and PEG
  • Type Safety: Strict TypeScript throughout (no any)
  • Modern Tooling: Bun runtime, Biome linting, GitHub Actions
  • Extensibility: Clean architecture for Stage 2/3 features

Target Systems (Stage 1)

  • D&D 5e MVP
  • Basic arithmetic (+, -, *, /, %, **)
  • Keep/drop modifiers (advantage, disadvantage, stat generation)

Architecture

Input → [Lexer] → Tokens → [Pratt Parser] → AST → [Evaluator] → Result

Key Design Decisions

Decision Choice Rationale
Parser Pratt Handles precedence naturally, extensible for modifiers
Runtime Bun Fast startup, built-in test runner, native TypeScript
Linting Biome Single tool for lint + format, fast
Testing fast-check Property-based invariant testing

Implementation Phases

Phase 0: Infrastructure

Phase 1: Lexer

Phase 2: Parser

Phase 3: RNG

Phase 4: Evaluator

Phase 5: Public API

Phase 6: CLI & Build


Success Criteria

  • All Stage 1 notation parses correctly
  • Performance: <1ms for simple rolls
  • Test coverage: >90% statements, 100% functions
  • CI pipeline green
  • npm publish works
  • README updated with v3 documentation

Syntax Support (Stage 1)

Feature Syntax Example
Basic roll NdX 2d6
Implicit count dX d201d20
Arithmetic +, -, *, /, %, ** 1d20+5
Parentheses (...) (1d4+1)*2
Keep Highest khN or kN 2d20kh1
Keep Lowest klN 2d20kl1
Drop Lowest dlN 4d6dl1
Drop Highest dhN 4d6dh1
Negative results 1d4-5 → can be -4
Computed dice (expr)d(expr) (1+1)d(3*2)

Future (Stage 2/3)

Not in scope for this epic:

  • Exploding dice (1d6!, 1d6!!, 1d6!p)
  • Reroll mechanics (2d6r<2, 2d6ro<3)
  • Success counting (10d10>=6, 10d10>=6f1)
  • Compare points (>, >=, <, <=, =)
  • Math functions (floor, ceil, max, min)
  • Variables (@strMod, @{modifier})
  • Grouped rolls ({1d8, 1d10}kh1)
  • Sorting modifiers (4d6s, 4d6sd)
  • Critical thresholds (1d20cs>19)

References

Files are local only.

  • PRD.md — Full requirements document
  • PLAN.md — Implementation phases and details

Drafted with AI assistance

Sub-issues

Metadata

Metadata

Assignees

Labels

epicIssue that combines multiple related tasks

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions