Skip to content

edloidas/roll-parser

Repository files navigation

Roll Parser

High-performance dice notation parser for tabletop RPGs.
TypeScript-first, Bun-optimized, Pratt parser architecture.

CI Status npm version TypeScript Bun

Status

v3 in Development - Complete architectural rewrite in progress.

For production use, install v2.3.2.

Features

Stage 1: Core Engine (In Progress)

  • Basic dice notation: 2d6, d20, 4d6+4
  • Full arithmetic: +, -, *, /, %, **
  • Parentheses: (1d4+1)*2
  • Keep/Drop modifiers: 4d6kh3, 2d20kl1, 4d6dl1
  • Computed dice: (1+1)d(3*2)
  • Seedable RNG for reproducible rolls

Stage 2: System Compatibility (Planned)

  • Exploding dice: 1d6!, 1d6!!, 1d6!p
  • Reroll mechanics: 2d6r<2, 2d6ro<3
  • Success counting: 10d10>=6, 10d10>=6f1
  • Math functions: floor(), ceil(), max(), min()

Stage 3: Advanced Features (Planned)

  • Variables: 1d20+@str, 1d20+@{modifier}
  • Grouped rolls: {1d8, 1d10}kh1
  • Rich JSON output with roll breakdown

Installation

# When v3 is released
bun add roll-parser
npm install roll-parser

Usage (v3 API Preview)

import { roll, parse } from 'roll-parser';

// Simple roll
const result = roll('4d6kh3');
console.log(result.total);      // e.g., 14
console.log(result.rolls);      // Individual die results
console.log(result.rendered);   // "4d6kh3[6,5,3,2] = 14"

// With seeded RNG for reproducibility
const seeded = roll('2d20', { seed: 'my-seed' });

// Parse without rolling (for inspection)
const ast = parse('2d6+5');

CLI

# Roll dice from command line
roll-parser 2d6+3
roll-parser 4d6kh3
roll-parser --help

Development

bun install          # Install dependencies
bun check:fix        # Type check + lint + format
bun test             # Run tests
bun run build        # Build library
bun release:dry      # Validate release

Architecture

Built with a Pratt parser for:

  • Clean precedence handling - Operator binding power, not grammar rules
  • Easy extensibility - Add new modifiers without restructuring
  • Optimal performance - Target <1ms for complex expressions
  • Type safety - Strict TypeScript, no any

Project Structure

src/
├── lexer/       # Tokenization (d, kh, kl, dh, dl, numbers, operators)
├── parser/      # Pratt parser with AST generation
├── evaluator/   # AST evaluation with modifier handling
├── rng/         # Seedable random number generation
├── cli/         # Command-line interface
└── index.ts     # Public API exports

Supported Systems

System Notation Examples Stage
D&D 5e 2d6, 1d20+5, 4d6kh3 1
Pathfinder 2e 1d20+10 vs 25 2
World of Darkness 10d10>=6f1 2
Savage Worlds 1d6! 2

Contributing

See CONTRIBUTING.md for development guidelines.

License

MIT © Mikita Taukachou

About

🐉 Roll parser for D&D5e, Pathfinder 1 & 2, WoD

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •