roll-parser 2.3.2 → 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -178
- package/dist/cli/args.d.ts +35 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/format.d.ts +20 -0
- package/dist/cli/format.d.ts.map +1 -0
- package/dist/cli/index.d.ts +8 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli.js +1775 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/evaluator/evaluator.d.ts +65 -0
- package/dist/evaluator/evaluator.d.ts.map +1 -0
- package/dist/evaluator/index.d.ts +8 -0
- package/dist/evaluator/index.d.ts.map +1 -0
- package/dist/evaluator/modifiers/compare.d.ts +15 -0
- package/dist/evaluator/modifiers/compare.d.ts.map +1 -0
- package/dist/evaluator/modifiers/explode.d.ts +44 -0
- package/dist/evaluator/modifiers/explode.d.ts.map +1 -0
- package/dist/evaluator/modifiers/keep-drop.d.ts +44 -0
- package/dist/evaluator/modifiers/keep-drop.d.ts.map +1 -0
- package/dist/evaluator/modifiers/reroll.d.ts +33 -0
- package/dist/evaluator/modifiers/reroll.d.ts.map +1 -0
- package/dist/evaluator/modifiers/success-count.d.ts +28 -0
- package/dist/evaluator/modifiers/success-count.d.ts.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1723 -0
- package/dist/index.mjs +1724 -0
- package/dist/lexer/lexer.d.ts +75 -0
- package/dist/lexer/lexer.d.ts.map +1 -0
- package/dist/lexer/tokens.d.ts +87 -0
- package/dist/lexer/tokens.d.ts.map +1 -0
- package/dist/parser/ast.d.ts +183 -0
- package/dist/parser/ast.d.ts.map +1 -0
- package/dist/parser/parser.d.ts +102 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/rng/index.d.ts +8 -0
- package/dist/rng/index.d.ts.map +1 -0
- package/dist/rng/mock.d.ts +36 -0
- package/dist/rng/mock.d.ts.map +1 -0
- package/dist/rng/seeded.d.ts +33 -0
- package/dist/rng/seeded.d.ts.map +1 -0
- package/dist/rng/types.d.ts +24 -0
- package/dist/rng/types.d.ts.map +1 -0
- package/dist/roll.d.ts +47 -0
- package/dist/roll.d.ts.map +1 -0
- package/dist/testing.d.ts +11 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +38 -0
- package/dist/testing.mjs +39 -0
- package/dist/types.d.ts +105 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +62 -43
- package/src/cli/args.ts +72 -0
- package/src/cli/format.ts +45 -0
- package/src/cli/index.ts +75 -0
- package/src/errors.ts +78 -0
- package/src/evaluator/evaluator.ts +837 -0
- package/src/evaluator/index.ts +14 -0
- package/src/evaluator/modifiers/compare.ts +29 -0
- package/src/evaluator/modifiers/explode.ts +215 -0
- package/src/evaluator/modifiers/keep-drop.ts +151 -0
- package/src/evaluator/modifiers/reroll.ts +172 -0
- package/src/evaluator/modifiers/success-count.ts +59 -0
- package/src/index.ts +72 -0
- package/src/lexer/lexer.ts +281 -0
- package/src/lexer/tokens.ts +147 -0
- package/src/parser/ast.ts +261 -0
- package/src/parser/parser.ts +716 -0
- package/src/rng/index.ts +8 -0
- package/src/rng/mock.ts +65 -0
- package/src/rng/seeded.ts +127 -0
- package/src/rng/types.ts +25 -0
- package/src/roll.ts +66 -0
- package/src/testing.ts +16 -0
- package/src/types.ts +112 -0
- package/bin/help.txt +0 -19
- package/bin/roll-parser.js +0 -23
- package/dist/roll-parser.js +0 -697
- package/dist/roll-parser.min.js +0 -1
- package/index.js +0 -24
- package/src/complex/parse.js +0 -37
- package/src/complex/parseAndRoll.js +0 -21
- package/src/complex/parseAndRollClassic.js +0 -20
- package/src/complex/parseAndRollSimple.js +0 -19
- package/src/complex/parseAndRollWod.js +0 -20
- package/src/complex/parseClassicRoll.js +0 -22
- package/src/complex/parseSimpleRoll.js +0 -21
- package/src/complex/parseWodRoll.js +0 -21
- package/src/converter.js +0 -45
- package/src/grammar.js +0 -14
- package/src/mapper.js +0 -50
- package/src/normalizer.js +0 -51
- package/src/object/Result.js +0 -23
- package/src/object/Roll.js +0 -26
- package/src/object/Type.js +0 -5
- package/src/object/WodRoll.js +0 -29
- package/src/parser.js +0 -43
- package/src/random.js +0 -22
- package/src/roller.js +0 -106
- package/src/stringifier.js +0 -33
package/README.md
CHANGED
|
@@ -1,210 +1,79 @@
|
|
|
1
1
|
<h1 align="center">Roll Parser</h1>
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
-
|
|
4
|
+
High-performance dice notation parser for tabletop RPGs.<br>
|
|
5
|
+
TypeScript-first, Bun-optimized, Pratt parser architecture.
|
|
5
6
|
</p>
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-6-blue.svg" alt="TypeScript"></a>
|
|
10
|
+
<a href="https://bun.sh/"><img src="https://img.shields.io/badge/Bun-1.3+-black.svg" alt="Bun"></a>
|
|
11
|
+
<a href="https://nodejs.org/"><img src="https://img.shields.io/badge/Node.js-22%2B-339933.svg" alt="Node.js"></a>
|
|
12
|
+
</p>
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
[![Project is on npm][npm-image]][npm-url]
|
|
14
|
+
## Status
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
> **v3 Alpha** — Stage 1 (Core Engine) is complete with 300+ tests.
|
|
17
|
+
> Stages 2–3 are planned.
|
|
18
|
+
>
|
|
19
|
+
> For production use, install [v2.3.2](https://www.npmjs.com/package/roll-parser/v/2.3.2).
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
## Features
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
### Stage 1: Core Engine (Complete)
|
|
21
24
|
|
|
22
|
-
|
|
25
|
+
- Basic dice notation: `2d6`, `d20`, `4d6+4`
|
|
26
|
+
- Full arithmetic: `+`, `-`, `*`, `/`, `%`, `**`
|
|
27
|
+
- Parentheses: `(1d4+1)*2`
|
|
28
|
+
- Keep/Drop modifiers: `4d6kh3`, `2d20kl1`, `4d6dl1`
|
|
29
|
+
- Computed dice: `(1+1)d(3*2)`
|
|
30
|
+
- Seedable RNG for reproducible rolls
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
npm install roll-parser
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Then in the console or JS file:
|
|
29
|
-
```js
|
|
30
|
-
const rollParser = require('roll-parser');
|
|
31
|
-
```
|
|
32
|
+
### Stage 2: System Compatibility (Planned)
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
- Exploding dice: `1d6!`, `1d6!!`, `1d6!p`
|
|
35
|
+
- Reroll mechanics: `2d6r<2`, `2d6ro<3`
|
|
36
|
+
- Success counting: `10d10>=6`, `10d10>=6f1`
|
|
37
|
+
- Math functions: `floor()`, `ceil()`, `max()`, `min()`
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
<script src="https://unpkg.com/roll-parser/dist/roll-parser.js"></script>
|
|
37
|
-
```
|
|
38
|
-
Minified version:
|
|
39
|
-
```html
|
|
40
|
-
<script src="https://unpkg.com/roll-parser/dist/roll-parser.min.js"></script>
|
|
41
|
-
```
|
|
39
|
+
### Stage 3: Advanced Features (Planned)
|
|
42
40
|
|
|
43
|
-
|
|
41
|
+
- Variables: `1d20+@str`, `1d20+@{modifier}`
|
|
42
|
+
- Grouped rolls: `{1d8, 1d10}kh1`
|
|
43
|
+
- Rich JSON output with roll breakdown
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
## Installation
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
|
-
|
|
48
|
+
bun add roll-parser
|
|
49
|
+
npm install roll-parser
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
Run `roll-parser --help` for more details.
|
|
52
|
-
|
|
53
52
|
## Usage
|
|
54
53
|
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// `parse()` function parses any notation and returns `Roll` or `WodRoll` object
|
|
59
|
-
//=> { dice: 6, count: 4, modifier: 1 }
|
|
60
|
-
const parsedRoll = parse('4d6+1');
|
|
61
|
-
|
|
62
|
-
// `Roll` or `WodRoll` can be stringified
|
|
63
|
-
//=> '4d6+1'
|
|
64
|
-
const rollNotation = parsedRoll.toString();
|
|
65
|
-
|
|
66
|
-
//=> { notation: '4d6+1', value: 16, rolls: [3, 1, 6, 5] }
|
|
67
|
-
const result1 = roll(parsedRoll);
|
|
68
|
-
//=> { notation: '2d20-3', value: 23, rolls: [11, 15] }
|
|
69
|
-
const result2 = roll(new Roll(20, 2, -3));
|
|
70
|
-
// Can also accept plain objects
|
|
71
|
-
//=> { notation: '2d10>7', value: 1, rolls: [4, 8] }
|
|
72
|
-
const result3 = roll({dice: 10, count: 2, success: 7});
|
|
54
|
+
```typescript
|
|
55
|
+
import { roll } from 'roll-parser';
|
|
73
56
|
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
const result4 = parseAndRoll('3d10!>8f1');
|
|
78
|
-
|
|
79
|
-
//=> '(3d10!>8f1) 2 [3,10,7,9]'
|
|
80
|
-
const resultNotation = result4.toString();
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
Specific parsers can be used.
|
|
84
|
-
|
|
85
|
-
__Classic (D&D):__
|
|
86
|
-
|
|
87
|
-
```js
|
|
88
|
-
const {
|
|
89
|
-
parseClassicRoll,
|
|
90
|
-
rollClassic,
|
|
91
|
-
parseAndRollClassic,
|
|
92
|
-
Roll
|
|
93
|
-
} = require('roll-parser');
|
|
94
|
-
|
|
95
|
-
//=> { dice: 10, count: 1, modifier: 0 }
|
|
96
|
-
const parsedRoll = parseClassicRoll('d10');
|
|
97
|
-
|
|
98
|
-
//=> { notation: 'd10', value: 7, rolls: [7] }
|
|
99
|
-
const result1 = rollClassic(parsedRoll);
|
|
100
|
-
|
|
101
|
-
//=> { notation: '2d20', value: 26, rolls: [11, 15] }
|
|
102
|
-
const result2 = rollClassic(new Roll(20, 2));
|
|
103
|
-
|
|
104
|
-
//=> { notation: '4d10+1', value: 22, rolls: [4, 6, 2, 9] }
|
|
105
|
-
const result3 = rollClassic({ dice: 10, count: 4, modifier: 1 });
|
|
106
|
-
|
|
107
|
-
//=> { notation: '3d6', value: 15, rolls: [6, 6, 3] }
|
|
108
|
-
const result4 = parseAndRollClassic('3d6');
|
|
57
|
+
const result = roll('4d6kh3');
|
|
58
|
+
console.log(result.total); // e.g., 14
|
|
59
|
+
console.log(result.notation); // "4d6kh3"
|
|
109
60
|
```
|
|
110
61
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
const {
|
|
115
|
-
parseWodRoll,
|
|
116
|
-
rollWod,
|
|
117
|
-
parseAndRollWod,
|
|
118
|
-
WodRoll
|
|
119
|
-
} = require('roll-parser');
|
|
120
|
-
|
|
121
|
-
//=> { dice: 10, count: 1, again: false, success: 6, fail: 0 }
|
|
122
|
-
const parsedRoll = parseWodRoll('d10>6');
|
|
123
|
-
|
|
124
|
-
// Returns notation, number of success rolls and list of all dice rolls
|
|
125
|
-
//=> { notation: 'd10', value: 1, rolls: [7] }
|
|
126
|
-
const result1 = rollWod(parsedRoll);
|
|
127
|
-
|
|
128
|
-
//=> { notation: '4d10>6f1', value: 1, rolls: [4, 10, 5, 2] }
|
|
129
|
-
const result2 = rollWod(new WodRoll(10, 4, false, 6, 1));
|
|
62
|
+
### CLI
|
|
130
63
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const result4 = parseAndRollWod('4d10>7f4');
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
__Simple (D&D, space-separated):__
|
|
139
|
-
|
|
140
|
-
```js
|
|
141
|
-
const { parseSimpleRoll, parseAndRollSimple } = require('roll-parser');
|
|
142
|
-
|
|
143
|
-
//=> { dice: 10, count: 1, modifier: 0 }
|
|
144
|
-
const parsedRoll = parseSimpleRoll('10');
|
|
145
|
-
|
|
146
|
-
//=> { notation: '4d10-1', value: 23, rolls: [3, 6, 8, 7] }
|
|
147
|
-
const result = parseAndRollSimple('4 10 -1');
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Random number generator can be used to roll the dice.
|
|
151
|
-
|
|
152
|
-
```js
|
|
153
|
-
const { random } = require('roll-parser');
|
|
154
|
-
|
|
155
|
-
//=> 84 - d100-like roll
|
|
156
|
-
random(100);
|
|
157
|
-
|
|
158
|
-
//=> 7 - d10-like roll
|
|
159
|
-
random(10);
|
|
160
|
-
|
|
161
|
-
//=> [2, 5, 2, 6] - 4d6-like roll
|
|
162
|
-
[...Array(4)].map(() => random(6));
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
Even so the parse&roll functions uses checks to convert non-standard objects to `Roll` or `WodRoll`, explicit conversion can be used in some cases:
|
|
166
|
-
|
|
167
|
-
```js
|
|
168
|
-
const { convert } = require('roll-parser');
|
|
169
|
-
|
|
170
|
-
//=> new Roll(undefined, 4, -3)
|
|
171
|
-
convert({ count: 4, modifier: -3 });
|
|
172
|
-
|
|
173
|
-
//=> new WodRoll(10, 6, true, undefined, 2)
|
|
174
|
-
convert({ dice: 10, count: 6, again: true, fail: 2 });
|
|
64
|
+
```bash
|
|
65
|
+
roll-parser 2d6+3
|
|
66
|
+
roll-parser 4d6kh3
|
|
67
|
+
roll-parser --help
|
|
175
68
|
```
|
|
176
69
|
|
|
177
|
-
##
|
|
70
|
+
## Known Limitations
|
|
178
71
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
♥ [roll-parser](https://github.com/edloidas/roll-parser) and want to get involved?<br>
|
|
184
|
-
Please, check the [guide](CONTRIBUTING.md) first.
|
|
72
|
+
- **`4d6d1` parses as nested dice, not "drop 1".** The bare `d` token is always
|
|
73
|
+
interpreted as the dice operator, so `4d6d1` becomes `(4d6)d1` (roll 4d6, then
|
|
74
|
+
use the result as the count for d1). To drop dice, use the explicit `dl`
|
|
75
|
+
(drop lowest) or `dh` (drop highest) modifiers: `4d6dl1`.
|
|
185
76
|
|
|
186
77
|
## License
|
|
187
78
|
|
|
188
79
|
[MIT](LICENSE) © [Mikita Taukachou](https://edloidas.com)
|
|
189
|
-
|
|
190
|
-
<!-- Links -->
|
|
191
|
-
[travis-url]: https://travis-ci.org/edloidas/roll-parser
|
|
192
|
-
[travis-image]: https://img.shields.io/travis/edloidas/roll-parser.svg?label=linux%20build
|
|
193
|
-
|
|
194
|
-
[appveyor-url]: https://ci.appveyor.com/project/edloidas/roll-parser
|
|
195
|
-
[appveyor-image]: https://img.shields.io/appveyor/ci/edloidas/roll-parser.svg?label=windows%20build
|
|
196
|
-
|
|
197
|
-
[coveralls-url]: https://coveralls.io/github/edloidas/roll-parser?branch=master
|
|
198
|
-
[coveralls-image]: https://coveralls.io/repos/github/edloidas/roll-parser/badge.svg?branch=master
|
|
199
|
-
|
|
200
|
-
[dep-url]: https://david-dm.org/edloidas/roll-parser
|
|
201
|
-
[dep-image]: https://david-dm.org/edloidas/roll-parser.svg
|
|
202
|
-
|
|
203
|
-
[devdep-url]: https://david-dm.org/edloidas/roll-parser#info=devDependencies
|
|
204
|
-
[devdep-image]: https://david-dm.org/edloidas/roll-parser/dev-status.svg
|
|
205
|
-
|
|
206
|
-
[node-url]: https://nodejs.org
|
|
207
|
-
[node-image]: https://img.shields.io/badge/node-≥%206.0.0-green.svg
|
|
208
|
-
|
|
209
|
-
[npm-url]: https://www.npmjs.com/package/roll-parser
|
|
210
|
-
[npm-image]: https://img.shields.io/badge/npm-roll--parser-blue.svg
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI argument parser.
|
|
3
|
+
*
|
|
4
|
+
* Pure function that parses process.argv-style string arrays into typed options.
|
|
5
|
+
*
|
|
6
|
+
* @module cli/args
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Parsed CLI arguments.
|
|
10
|
+
*/
|
|
11
|
+
export type CliArgs = {
|
|
12
|
+
notation: string | undefined;
|
|
13
|
+
verbose: boolean;
|
|
14
|
+
seed: string | undefined;
|
|
15
|
+
showHelp: boolean;
|
|
16
|
+
showVersion: boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Result of parsing CLI arguments — either success or a usage error.
|
|
20
|
+
*/
|
|
21
|
+
export type ParseArgsResult = {
|
|
22
|
+
ok: true;
|
|
23
|
+
args: CliArgs;
|
|
24
|
+
} | {
|
|
25
|
+
ok: false;
|
|
26
|
+
error: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Parses a raw argument array into typed CLI options.
|
|
30
|
+
*
|
|
31
|
+
* @param argv - Arguments to parse (typically `process.argv.slice(2)`)
|
|
32
|
+
* @returns Parsed result or an error message for usage errors
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseArgs(argv: string[]): ParseArgsResult;
|
|
35
|
+
//# sourceMappingURL=args.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"args.d.ts","sourceRoot":"","sources":["../../src/cli/args.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,QAAQ,EAAE,OAAO,CAAC;IAClB,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzF;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,eAAe,CAyCzD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI output formatting.
|
|
3
|
+
*
|
|
4
|
+
* Transforms RollResult into terminal-appropriate strings.
|
|
5
|
+
*
|
|
6
|
+
* @module cli/format
|
|
7
|
+
*/
|
|
8
|
+
import type { RollResult } from '../types';
|
|
9
|
+
/**
|
|
10
|
+
* Formats a roll result for terminal display.
|
|
11
|
+
*
|
|
12
|
+
* In normal mode, returns just the total. In verbose mode, returns the
|
|
13
|
+
* rendered breakdown with terminal-safe formatting for dropped dice.
|
|
14
|
+
*
|
|
15
|
+
* @param result - The roll result to format
|
|
16
|
+
* @param verbose - Whether to show the detailed breakdown
|
|
17
|
+
* @returns Formatted string for terminal output
|
|
18
|
+
*/
|
|
19
|
+
export declare function formatResult(result: RollResult, verbose: boolean): string;
|
|
20
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../src/cli/format.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,MAAM,CAMzE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":";AAEA;;;;GAIG"}
|