thekevinscott commited on
Commit
a2b203d
·
verified ·
1 Parent(s): 7307dcd

Upload folder using huggingface_hub (part 7)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/print.test.ts +92 -0
  2. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/print.ts +68 -0
  3. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/rule-ref.test.ts +22 -0
  4. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/rule-ref.ts +21 -0
  5. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/type-guards.test.ts +142 -0
  6. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/type-guards.ts +29 -0
  7. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/types.ts +80 -0
  8. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-parser/build-rule-stack.test.ts +283 -0
  9. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-parser/build-rule-stack.ts +89 -0
  10. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/index.ts +8 -0
  11. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/index.ts +2 -0
  12. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-space.ts +4 -0
  13. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-word-char.test.ts +25 -0
  14. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-word-char.ts +1 -0
  15. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-char.test.ts +37 -0
  16. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-char.ts +105 -0
  17. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-name.test.ts +22 -0
  18. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-name.ts +21 -0
  19. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-space.test.ts +64 -0
  20. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-space.ts +21 -0
  21. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/rules-builder.test.ts +1254 -0
  22. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/rules-builder.ts +263 -0
  23. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/symbol-ids.ts +47 -0
  24. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/types.ts +99 -0
  25. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/code-points.ts +23 -0
  26. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/build-error-position.test.ts +33 -0
  27. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/build-error-position.ts +31 -0
  28. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/get-input-as-string.test.ts +15 -0
  29. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/get-input-as-string.ts +11 -0
  30. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/grammar-parse-error.test.ts +12 -0
  31. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/grammar-parse-error.ts +17 -0
  32. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/index.ts +2 -0
  33. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/input-parse-error.test.ts +23 -0
  34. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/input-parse-error.ts +41 -0
  35. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/types.ts +1 -0
  36. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/is-point-in-range.test.ts +15 -0
  37. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/is-point-in-range.ts +8 -0
  38. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/validate-non-empty.ts +6 -0
  39. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration-with-additional-strings.test.ts +662 -0
  40. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration-with-an-initial-string.test.ts +517 -0
  41. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration.test.ts +120 -0
  42. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/validation/validate-grammar.test.ts +87 -0
  43. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/validation/validate-input.test.ts +352 -0
  44. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tsconfig.json +16 -0
  45. v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/vitest.config.ts +8 -0
  46. v2-models-effort/runs/20261111-214430-weathered-fire/proxy.log +18 -0
  47. v2-models-effort/runs/20261111-214430-weathered-fire/run.json +28 -0
  48. v2-models-effort/runs/20261111-214430-weathered-fire/run.lock +0 -0
  49. v2-models-effort/runs/20261111-214430-weathered-fire/stderr.log +0 -0
  50. v2-models-effort/runs/20261111-214430-weathered-fire/stderr.txt +1 -0
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/print.test.ts ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { beforeEach, describe, expect, test, vi } from 'vitest';
2
+ import { getParentStackId } from './get-parent-stack-id.ts';
3
+ import type { GraphNode } from './graph-node.ts';
4
+ import type { GraphPointer } from './graph-pointer.ts';
5
+ import { Pointers } from './pointers.ts';
6
+ import { printGraphNode, printGraphPointer } from './print.ts';
7
+ import { RuleRef } from './rule-ref.ts';
8
+ import { type PrintOpts, RuleChar, type UnresolvedRule } from './types.ts';
9
+
10
+ vi.mock('./get-parent-stack-id.ts', () => ({ getParentStackId: vi.fn() }));
11
+
12
+ const mockGetParentStackId = vi.mocked(getParentStackId);
13
+
14
+ beforeEach(() => {
15
+ mockGetParentStackId.mockReset();
16
+ });
17
+
18
+ const COLORS: Record<string, string> = {
19
+ '\x1b[34m': 'BLUE',
20
+ '\x1b[36m': 'CYAN',
21
+ '\x1b[32m': 'GREEN',
22
+ '\x1b[31m': 'RED',
23
+ '\x1b[90m': 'GRAY',
24
+ '\x1b[33m': 'YELLOW',
25
+ };
26
+
27
+ const mockColorize = (text: string | number, color: string): string => {
28
+ if (!(color in COLORS)) {
29
+ throw new Error(`Invalid color: ${color}`);
30
+ }
31
+ return `[${COLORS[color]}]:${text}`;
32
+ };
33
+
34
+ interface MockNode {
35
+ id: number;
36
+ rule: UnresolvedRule;
37
+ next: GraphNode | undefined;
38
+ print: (opts: PrintOpts) => string;
39
+ }
40
+
41
+ const createMockNode = (id: number, rule: UnresolvedRule, next?: GraphNode): MockNode => ({
42
+ id,
43
+ rule,
44
+ next,
45
+ print: (_opts) => `Node(${id})`,
46
+ });
47
+
48
+ const createMockGraphPointer = (node: MockNode) => ({
49
+ node,
50
+ parent: undefined,
51
+ print: (_opts: PrintOpts) => `Pointer to ${node.id}`,
52
+ });
53
+
54
+ describe('print', () => {
55
+ describe('printGraphPointer', () => {
56
+ test('it prints graph pointer details correctly', () => {
57
+ const mockNode = createMockNode(1, new RuleRef(100));
58
+ const mockPointer = createMockGraphPointer(mockNode);
59
+ mockGetParentStackId.mockReturnValue('foo');
60
+ const result = printGraphPointer(mockPointer as unknown as GraphPointer)({
61
+ colorize: mockColorize,
62
+ pointers: new Pointers(),
63
+ showPosition: false,
64
+ });
65
+ expect(result).toEqual('[RED]:*foo');
66
+ });
67
+ });
68
+
69
+ describe('printGraphNode', () => {
70
+ test('it prints graph node with a character rule', () => {
71
+ const ruleChar = new RuleChar([65]);
72
+ const mockNode = createMockNode(1, ruleChar);
73
+ const result = printGraphNode(mockNode as unknown as GraphNode)({
74
+ colorize: mockColorize,
75
+ showPosition: false,
76
+ pointers: new Pointers(),
77
+ });
78
+ expect(result).toEqual('[GRAY]:[[YELLOW]:A[GRAY]:]');
79
+ });
80
+
81
+ test('it prints graph node with a rule reference', () => {
82
+ const ruleRef = new RuleRef(200);
83
+ const mockNode = createMockNode(1, ruleRef);
84
+ const result = printGraphNode(mockNode as unknown as GraphNode)({
85
+ colorize: mockColorize,
86
+ showPosition: true,
87
+ pointers: new Pointers(),
88
+ });
89
+ expect(result).toEqual('[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)');
90
+ });
91
+ });
92
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/print.ts ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Color } from './colorize.ts';
2
+ import { getParentStackId } from './get-parent-stack-id.ts';
3
+ import type { GraphNode } from './graph-node.ts';
4
+ import type { GraphPointer } from './graph-pointer.ts';
5
+ import { isRange, isRuleChar, isRuleRef } from './type-guards.ts';
6
+ import type { PrintOpts } from './types.ts';
7
+
8
+ export const printGraphPointer =
9
+ (pointer: GraphPointer) =>
10
+ (opts: PrintOpts): string => {
11
+ const col = opts.colorize;
12
+ return col(`*${getParentStackId(pointer, col)}`, Color.RED);
13
+ };
14
+
15
+ export const printGraphNode =
16
+ (node: GraphNode) =>
17
+ (opts: PrintOpts): string => {
18
+ const pointers: Iterable<GraphPointer> = opts.pointers ?? [];
19
+ const col = opts.colorize;
20
+ const showPosition = opts.showPosition ?? false;
21
+ const rule = node.rule;
22
+
23
+ const parts: string[] = [];
24
+ if (showPosition) {
25
+ parts.push(col('{', Color.BLUE), col(node.id, Color.GRAY), col('}', Color.BLUE));
26
+ }
27
+
28
+ if (isRuleChar(rule)) {
29
+ parts.push(
30
+ col('[', Color.GRAY),
31
+ col(
32
+ rule.value
33
+ .map((v) => (isRange(v) ? v.map((val) => col(getChar(val), Color.YELLOW)).join('') : getChar(v)))
34
+ .join(''),
35
+ Color.YELLOW,
36
+ ),
37
+ col(']', Color.GRAY),
38
+ );
39
+ } else if (isRuleRef(rule)) {
40
+ parts.push(col('Ref(', Color.GRAY), col(rule.value, Color.GREEN), col(')', Color.GRAY));
41
+ } else {
42
+ parts.push(col(rule.type, Color.YELLOW));
43
+ }
44
+
45
+ for (const pointer of pointers) {
46
+ const pointerParts: string[] = [];
47
+ if (pointer.node === node) {
48
+ pointerParts.push(pointer.print(opts));
49
+ }
50
+ if (pointerParts.length > 0) {
51
+ parts.push(col('[', Color.GRAY), col(pointerParts.join(''), Color.YELLOW), col(']', Color.GRAY));
52
+ }
53
+ }
54
+
55
+ const partsToReturn = [parts.join('')];
56
+ if (node.next) {
57
+ partsToReturn.push(node.next.print(opts));
58
+ }
59
+ return partsToReturn.join(col('-> ', Color.GRAY));
60
+ };
61
+
62
+ const getChar = (charCode: number): string => {
63
+ const char = String.fromCodePoint(charCode);
64
+ if (char === '\n') {
65
+ return '\\n';
66
+ }
67
+ return char;
68
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/rule-ref.test.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import type { GraphNode } from './graph-node.ts';
3
+ import { RuleRef } from './rule-ref.ts';
4
+
5
+ describe('RuleRef', () => {
6
+ test('initializes with a given value', () => {
7
+ const ruleRef = new RuleRef(123);
8
+ expect(ruleRef.value).toEqual(123);
9
+ });
10
+
11
+ test('allows setting and getting nodes', () => {
12
+ const mockNodes = new Set([1]) as unknown as Set<GraphNode>;
13
+ const ruleRef = new RuleRef(123);
14
+ ruleRef.nodes = mockNodes;
15
+ expect(ruleRef.nodes).toEqual(mockNodes);
16
+ });
17
+
18
+ test('throws an error if trying to get nodes before setting', () => {
19
+ const ruleRef = new RuleRef(123);
20
+ expect(() => ruleRef.nodes).toThrow(new Error('Nodes are not set'));
21
+ });
22
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/rule-ref.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { GraphNode } from './graph-node.ts';
2
+
3
+ export class RuleRef {
4
+ value: number;
5
+ #nodes?: Set<GraphNode>;
6
+
7
+ constructor(value: number) {
8
+ this.value = value;
9
+ }
10
+
11
+ get nodes(): Set<GraphNode> {
12
+ if (this.#nodes === undefined) {
13
+ throw new Error('Nodes are not set');
14
+ }
15
+ return this.#nodes;
16
+ }
17
+
18
+ set nodes(nodes: Set<GraphNode>) {
19
+ this.#nodes = nodes;
20
+ }
21
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/type-guards.test.ts ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GraphNode } from './graph-node.ts';
3
+ import { GraphPointer } from './graph-pointer.ts';
4
+ import { RuleRef } from './rule-ref.ts';
5
+ import {
6
+ isGraphPointerRuleChar,
7
+ isGraphPointerRuleCharExclude,
8
+ isGraphPointerRuleEnd,
9
+ isGraphPointerRuleRef,
10
+ isRange,
11
+ isRule,
12
+ isRuleChar,
13
+ isRuleCharExclude,
14
+ isRuleEnd,
15
+ isRuleRef,
16
+ } from './type-guards.ts';
17
+ import { RuleChar, RuleCharExclude, RuleEnd } from './types.ts';
18
+
19
+ describe('rule type guards', () => {
20
+ describe('isGraphPointerRuleRef', () => {
21
+ test('it returns true', () => {
22
+ const node = new GraphNode(new RuleRef(1), { stackId: 1, pathId: 2, stepId: 3 });
23
+ const pointer = new GraphPointer(node);
24
+ expect(isGraphPointerRuleRef(pointer)).toBe(true);
25
+ });
26
+
27
+ test('it returns false', () => {
28
+ const node = new GraphNode(new RuleEnd(), { stackId: 1, pathId: 2, stepId: 3 });
29
+ const pointer = new GraphPointer(node);
30
+ expect(isGraphPointerRuleRef(pointer)).toBe(false);
31
+ });
32
+ });
33
+
34
+ describe('isGraphPointerRuleEnd', () => {
35
+ test('it returns false', () => {
36
+ const node = new GraphNode(new RuleRef(1), { stackId: 1, pathId: 2, stepId: 3 });
37
+ const pointer = new GraphPointer(node);
38
+ expect(isGraphPointerRuleEnd(pointer)).toBe(false);
39
+ });
40
+
41
+ test('it returns true', () => {
42
+ const node = new GraphNode(new RuleEnd(), { stackId: 1, pathId: 2, stepId: 3 });
43
+ const pointer = new GraphPointer(node);
44
+ expect(isGraphPointerRuleEnd(pointer)).toBe(true);
45
+ });
46
+ });
47
+
48
+ describe('isGraphPointerRuleChar', () => {
49
+ test('it returns true', () => {
50
+ const node = new GraphNode(new RuleChar([97]), { stackId: 1, pathId: 2, stepId: 3 });
51
+ const pointer = new GraphPointer(node);
52
+ expect(isGraphPointerRuleChar(pointer)).toBe(true);
53
+ });
54
+
55
+ test('it returns false', () => {
56
+ const node = new GraphNode(new RuleEnd(), { stackId: 1, pathId: 2, stepId: 3 });
57
+ const pointer = new GraphPointer(node);
58
+ expect(isGraphPointerRuleChar(pointer)).toBe(false);
59
+ });
60
+ });
61
+
62
+ describe('isGraphPointerRuleCharExclude', () => {
63
+ test('it returns true', () => {
64
+ const node = new GraphNode(new RuleCharExclude([97]), { stackId: 1, pathId: 2, stepId: 3 });
65
+ const pointer = new GraphPointer(node);
66
+ expect(isGraphPointerRuleCharExclude(pointer)).toBe(true);
67
+ });
68
+
69
+ test('it returns false', () => {
70
+ const node = new GraphNode(new RuleChar([97]), { stackId: 1, pathId: 2, stepId: 3 });
71
+ const pointer = new GraphPointer(node);
72
+ expect(isGraphPointerRuleCharExclude(pointer)).toBe(false);
73
+ });
74
+ });
75
+
76
+ describe('isRule', () => {
77
+ test('it returns false for null', () => {
78
+ expect(isRule(undefined)).toBe(false);
79
+ expect(isRule(null)).toBe(false);
80
+ });
81
+
82
+ test('it returns false for a non rule', () => {
83
+ expect(isRule({ type: 'invalid', value: [] })).toBe(false);
84
+ });
85
+
86
+ test('it returns true for valid rule objects', () => {
87
+ const ruleChar = new RuleChar([65, [66, 67]]);
88
+ expect(isRule(ruleChar)).toBe(true);
89
+ });
90
+ });
91
+
92
+ describe('isRuleRef', () => {
93
+ test('it returns true for valid rule refs', () => {
94
+ const ruleRef = new RuleRef(1);
95
+ expect(isRuleRef(ruleRef)).toBe(true);
96
+ });
97
+
98
+ test('it returns false for invalid rule refs', () => {
99
+ expect(isRuleRef(new RuleChar([65]))).toBe(false);
100
+ });
101
+ });
102
+
103
+ describe('isRuleEnd', () => {
104
+ test('it returns true for valid rule ends', () => {
105
+ expect(isRuleEnd(new RuleEnd())).toBe(true);
106
+ });
107
+
108
+ test('it returns false for invalid rule ends', () => {
109
+ expect(isRuleEnd(new RuleChar([65]))).toBe(false);
110
+ });
111
+ });
112
+
113
+ describe('isRuleChar', () => {
114
+ test('it returns true for valid rule chars', () => {
115
+ expect(isRuleChar(new RuleChar([65]))).toBe(true);
116
+ });
117
+
118
+ test('it returns false for invalid rule chars', () => {
119
+ expect(isRuleChar(new RuleCharExclude([65]))).toBe(false);
120
+ });
121
+ });
122
+
123
+ describe('isRuleCharExclude', () => {
124
+ test('it returns true for valid rule chars', () => {
125
+ expect(isRuleCharExclude(new RuleCharExclude([65]))).toBe(true);
126
+ });
127
+
128
+ test('it returns false for invalid rule chars', () => {
129
+ expect(isRuleCharExclude(new RuleChar([65]))).toBe(false);
130
+ });
131
+ });
132
+
133
+ describe('isRange', () => {
134
+ test('it returns true for valid ranges', () => {
135
+ expect(isRange([1, 10])).toBe(true);
136
+ });
137
+
138
+ test.each<[unknown]>([[[1, '10']], [[1]], [5]])('it returns false for invalid ranges %j', (value) => {
139
+ expect(isRange(value)).toBe(false);
140
+ });
141
+ });
142
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/type-guards.ts ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { GraphPointer } from './graph-pointer.ts';
2
+ import { RuleRef } from './rule-ref.ts';
3
+ import { type Range, RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from './types.ts';
4
+
5
+ export const isRule = (rule?: unknown): rule is UnresolvedRule =>
6
+ rule instanceof RuleChar || rule instanceof RuleCharExclude || rule instanceof RuleEnd || rule instanceof RuleRef;
7
+
8
+ export const isRuleRef = (rule?: unknown): rule is RuleRef => rule instanceof RuleRef;
9
+
10
+ export const isRuleEnd = (rule?: unknown): rule is RuleEnd => rule instanceof RuleEnd;
11
+
12
+ export const isRuleChar = (rule?: unknown): rule is RuleChar => rule instanceof RuleChar;
13
+
14
+ export const isRuleCharExclude = (rule?: unknown): rule is RuleCharExclude => rule instanceof RuleCharExclude;
15
+
16
+ export const isRange = (inpt?: unknown): inpt is Range =>
17
+ Array.isArray(inpt) && inpt.length === 2 && inpt.every((n) => Number.isInteger(n));
18
+
19
+ export const isGraphPointerRuleRef = (pointer: GraphPointer): pointer is GraphPointer<RuleRef> =>
20
+ isRuleRef(pointer.rule);
21
+
22
+ export const isGraphPointerRuleEnd = (pointer: GraphPointer): pointer is GraphPointer<RuleEnd> =>
23
+ isRuleEnd(pointer.rule);
24
+
25
+ export const isGraphPointerRuleChar = (pointer: GraphPointer): pointer is GraphPointer<RuleChar> =>
26
+ isRuleChar(pointer.rule);
27
+
28
+ export const isGraphPointerRuleCharExclude = (pointer: GraphPointer): pointer is GraphPointer<RuleCharExclude> =>
29
+ isRuleCharExclude(pointer.rule);
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-graph/types.ts ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { GraphPointer } from './graph-pointer.ts';
2
+ import type { Pointers } from './pointers.ts';
3
+ import { RuleRef } from './rule-ref.ts';
4
+
5
+ export { RuleRef };
6
+
7
+ export interface PrintOpts {
8
+ pointers?: Pointers;
9
+ colorize: (text: string | number, color: string) => string;
10
+ showPosition?: boolean;
11
+ }
12
+
13
+ export type Range = [number, number];
14
+
15
+ export const RuleType = {
16
+ CHAR: 'char',
17
+ CHAR_EXCLUDE: 'char_exclude',
18
+ END: 'end',
19
+ } as const;
20
+ export type RuleType = (typeof RuleType)[keyof typeof RuleType];
21
+
22
+ // Rules carry their type as a plain property, so that two rules of the same type and value are
23
+ // structurally equal (e.g. with `toEqual`) and serialize to `{ type, value }`.
24
+ export class Rule {
25
+ readonly type: RuleType;
26
+
27
+ protected constructor(type: RuleType) {
28
+ this.type = type;
29
+ }
30
+ }
31
+
32
+ export class RuleWithValue<T = unknown> extends Rule {
33
+ value: T;
34
+
35
+ protected constructor(type: RuleType, value: T) {
36
+ super(type);
37
+ this.value = value;
38
+ }
39
+ }
40
+
41
+ export class RuleWithListOfIntsOrRanges extends RuleWithValue<(number | Range)[]> {
42
+ protected constructor(type: RuleType, value: (number | Range)[] = []) {
43
+ super(type, value.slice());
44
+ }
45
+ }
46
+
47
+ export class RuleChar extends RuleWithListOfIntsOrRanges {
48
+ declare readonly type: typeof RuleType.CHAR;
49
+
50
+ constructor(value?: (number | Range)[]) {
51
+ super(RuleType.CHAR, value);
52
+ }
53
+ }
54
+
55
+ export class RuleCharExclude extends RuleWithListOfIntsOrRanges {
56
+ declare readonly type: typeof RuleType.CHAR_EXCLUDE;
57
+
58
+ constructor(value?: (number | Range)[]) {
59
+ super(RuleType.CHAR_EXCLUDE, value);
60
+ }
61
+ }
62
+
63
+ export class RuleEnd extends Rule {
64
+ declare readonly type: typeof RuleType.END;
65
+
66
+ constructor() {
67
+ super(RuleType.END);
68
+ }
69
+ }
70
+
71
+ export type UnresolvedRule = RuleChar | RuleCharExclude | RuleRef | RuleEnd;
72
+
73
+ // ValidInput can either be a string, or a number indicating a code point.
74
+ // It CANNOT be a number representing a number; a number intended as input (like "8")
75
+ // should be passed in as a string.
76
+ export type ValidInput = string | number | number[];
77
+
78
+ // RuleRefs should never be exposed to the end user.
79
+ export type ResolvedRule = RuleCharExclude | RuleChar | RuleEnd;
80
+ export type ResolvedGraphPointer = GraphPointer<ResolvedRule>;
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-parser/build-rule-stack.test.ts ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { RuleRef } from '../grammar-graph/rule-ref.ts';
3
+ import { type Range, RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from '../grammar-graph/types.ts';
4
+ import {
5
+ type InternalRuleDef,
6
+ InternalRuleDefAlt,
7
+ InternalRuleDefChar,
8
+ InternalRuleDefCharAlt,
9
+ InternalRuleDefCharNot,
10
+ InternalRuleDefCharRngUpper,
11
+ InternalRuleDefEnd,
12
+ InternalRuleDefReference,
13
+ } from '../rules-builder/types.ts';
14
+ import { buildRuleStack } from './build-rule-stack.ts';
15
+
16
+ const ord = (char: string): number => char.codePointAt(0) as number;
17
+
18
+ const ichar = (value: number[]): InternalRuleDefChar => new InternalRuleDefChar(value);
19
+
20
+ const icharAlt = (value: number): InternalRuleDefCharAlt => new InternalRuleDefCharAlt(value);
21
+
22
+ const icharRngUpper = (value: number): InternalRuleDefCharRngUpper => new InternalRuleDefCharRngUpper(value);
23
+
24
+ const icharNot = (value: number[]): InternalRuleDefCharNot => new InternalRuleDefCharNot(value);
25
+
26
+ const ialt = (): InternalRuleDefAlt => new InternalRuleDefAlt();
27
+
28
+ const iend = (): InternalRuleDefEnd => new InternalRuleDefEnd();
29
+
30
+ const iref = (value: number): InternalRuleDefReference => new InternalRuleDefReference(value);
31
+
32
+ const makeRange = (lower: number | string, upper: number | string): Range => [
33
+ typeof lower === 'number' ? lower : ord(lower),
34
+ typeof upper === 'number' ? upper : ord(upper),
35
+ ];
36
+
37
+ describe('buildRuleStack', () => {
38
+ test('it builds rule stack for a single path', () => {
39
+ expect(buildRuleStack([ichar([120])])).toEqual([[new RuleChar([120]), new RuleEnd()]]);
40
+ });
41
+
42
+ test('it builds rule stack for two alternate paths', () => {
43
+ const result = buildRuleStack([ichar([ord('x')]), ialt(), ichar([ord('y')])]);
44
+ expect(result).toEqual([[new RuleChar([ord('x')]), new RuleEnd()], [new RuleChar([ord('y')]), new RuleEnd()]]);
45
+ });
46
+
47
+ test('it builds rule stack for three alternate paths', () => {
48
+ expect(buildRuleStack([ichar([ord('x')]), ialt(), ichar([ord('y')]), ialt(), ichar([ord('z')])])).toEqual([
49
+ [new RuleChar([ord('x')]), new RuleEnd()],
50
+ [new RuleChar([ord('y')]), new RuleEnd()],
51
+ [new RuleChar([ord('z')]), new RuleEnd()],
52
+ ]);
53
+ });
54
+
55
+ test('it builds rule stack for char not', () => {
56
+ expect(
57
+ buildRuleStack([icharNot([ord('x')]), ialt(), icharNot([ord('y')]), ialt(), icharNot([ord('z')])]),
58
+ ).toEqual([
59
+ [new RuleCharExclude([ord('x')]), new RuleEnd()],
60
+ [new RuleCharExclude([ord('y')]), new RuleEnd()],
61
+ [new RuleCharExclude([ord('z')]), new RuleEnd()],
62
+ ]);
63
+ });
64
+
65
+ test('it builds rule stack for mixed char and char not', () => {
66
+ expect(buildRuleStack([icharNot([ord('x')]), ialt(), ichar([ord('y')]), ialt(), icharNot([ord('z')])])).toEqual([
67
+ [new RuleCharExclude([ord('x')]), new RuleEnd()],
68
+ [new RuleChar([ord('y')]), new RuleEnd()],
69
+ [new RuleCharExclude([ord('z')]), new RuleEnd()],
70
+ ]);
71
+ });
72
+
73
+ test('it builds rule stack for char not with two characters and a range', () => {
74
+ const result = buildRuleStack([icharNot([ord('x')]), icharAlt(ord('y')), icharAlt(ord('z')), icharRngUpper(130)]);
75
+ expect(result).toEqual([[new RuleCharExclude([120, 121, makeRange(122, 130)]), new RuleEnd()]]);
76
+ });
77
+
78
+ describe('ranges', () => {
79
+ test.each<[string, InternalRuleDef[], UnresolvedRule[][]]>([
80
+ [
81
+ '[a-z]',
82
+ [ichar([ord('a')]), icharRngUpper(ord('z')), iend()],
83
+ [[new RuleChar([makeRange('a', 'z')]), new RuleEnd()]],
84
+ ],
85
+ [
86
+ '[a-zA-Z]',
87
+ [ichar([ord('a')]), icharRngUpper(ord('z')), icharAlt(ord('A')), icharRngUpper(ord('Z')), iend()],
88
+ [[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()]],
89
+ ],
90
+ [
91
+ '[a-zA-Z0-9]',
92
+ [
93
+ ichar([ord('a')]),
94
+ icharRngUpper(ord('z')),
95
+ icharAlt(ord('A')),
96
+ icharRngUpper(ord('Z')),
97
+ icharAlt(ord('0')),
98
+ icharRngUpper(ord('9')),
99
+ iend(),
100
+ ],
101
+ [[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]), new RuleEnd()]],
102
+ ],
103
+ ])('it builds rule stack for a char with no modifiers: %s', (grammar, input, expected) => {
104
+ const result = buildRuleStack(input);
105
+ expect(result).toEqual(expected);
106
+ });
107
+ });
108
+
109
+ test.each<[string, InternalRuleDef[], UnresolvedRule[][]]>([
110
+ [
111
+ '[a-z]?',
112
+ [ichar([ord('a')]), icharRngUpper(ord('z')), ialt(), iend()],
113
+ [[new RuleChar([makeRange('a', 'z')]), new RuleEnd()], [new RuleEnd()]],
114
+ ],
115
+ [
116
+ '[a-zA-Z]?',
117
+ [ichar([ord('a')]), icharRngUpper(ord('z')), icharAlt(ord('A')), icharRngUpper(ord('Z')), ialt(), iend()],
118
+ [[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()], [new RuleEnd()]],
119
+ ],
120
+ [
121
+ '[a-zA-Z0-9]?',
122
+ [
123
+ ichar([ord('a')]),
124
+ icharRngUpper(ord('z')),
125
+ icharAlt(ord('A')),
126
+ icharRngUpper(ord('Z')),
127
+ icharAlt(ord('0')),
128
+ icharRngUpper(ord('9')),
129
+ ialt(),
130
+ iend(),
131
+ ],
132
+ [[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]), new RuleEnd()], [new RuleEnd()]],
133
+ ],
134
+ ])('it builds rule stack for a char with question mark modifier: %s', (grammar, input, expected) => {
135
+ expect(buildRuleStack(input)).toEqual(expected);
136
+ });
137
+
138
+ test.each<[string, InternalRuleDef[], UnresolvedRule[][]]>([
139
+ [
140
+ '[a-z]+',
141
+ [ichar([ord('a')]), icharRngUpper(ord('z')), iref(1), ialt(), ichar([ord('a')]), icharRngUpper(ord('z')), iend()],
142
+ [
143
+ [new RuleChar([makeRange('a', 'z')]), new RuleRef(1), new RuleEnd()],
144
+ [new RuleChar([makeRange('a', 'z')]), new RuleEnd()],
145
+ ],
146
+ ],
147
+ [
148
+ '[a-zA-Z]+',
149
+ [
150
+ ichar([ord('a')]),
151
+ icharRngUpper(ord('z')),
152
+ icharAlt(ord('A')),
153
+ icharRngUpper(ord('Z')),
154
+ iref(1),
155
+ ialt(),
156
+ ichar([ord('a')]),
157
+ icharRngUpper(ord('z')),
158
+ icharAlt(ord('A')),
159
+ icharRngUpper(ord('Z')),
160
+ iend(),
161
+ ],
162
+ [
163
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()],
164
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()],
165
+ ],
166
+ ],
167
+ [
168
+ '[a-zA-Z0-9]+',
169
+ [
170
+ ichar([ord('a')]),
171
+ icharRngUpper(ord('z')),
172
+ icharAlt(ord('A')),
173
+ icharRngUpper(ord('Z')),
174
+ icharAlt(ord('0')),
175
+ icharRngUpper(ord('9')),
176
+ iref(1),
177
+ ialt(),
178
+ ichar([ord('a')]),
179
+ icharRngUpper(ord('z')),
180
+ icharAlt(ord('A')),
181
+ icharRngUpper(ord('Z')),
182
+ icharAlt(ord('0')),
183
+ icharRngUpper(ord('9')),
184
+ iend(),
185
+ ],
186
+ [
187
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]), new RuleRef(1), new RuleEnd()],
188
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]), new RuleEnd()],
189
+ ],
190
+ ],
191
+ ])('it builds rule stack for a char with plus modifier: %s', (grammar, input, expected) => {
192
+ const result = buildRuleStack(input);
193
+ expect(result).toEqual(expected);
194
+ });
195
+
196
+ test.each<[string, InternalRuleDef[], UnresolvedRule[][]]>([
197
+ [
198
+ '[a-z]*',
199
+ [ichar([ord('a')]), icharRngUpper(ord('z')), iref(1), ialt(), iend()],
200
+ [[new RuleChar([makeRange('a', 'z')]), new RuleRef(1), new RuleEnd()], [new RuleEnd()]],
201
+ ],
202
+ [
203
+ '[a-zA-Z]*',
204
+ [
205
+ ichar([ord('a')]),
206
+ icharRngUpper(ord('z')),
207
+ icharAlt(ord('A')),
208
+ icharRngUpper(ord('Z')),
209
+ iref(1),
210
+ ialt(),
211
+ iend(),
212
+ ],
213
+ [[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()], [new RuleEnd()]],
214
+ ],
215
+ [
216
+ '[a-zA-Z0-9]*',
217
+ [
218
+ ichar([ord('a')]),
219
+ icharRngUpper(ord('z')),
220
+ icharAlt(ord('A')),
221
+ icharRngUpper(ord('Z')),
222
+ icharAlt(ord('0')),
223
+ icharRngUpper(ord('9')),
224
+ iref(1),
225
+ ialt(),
226
+ iend(),
227
+ ],
228
+ [
229
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]), new RuleRef(1), new RuleEnd()],
230
+ [new RuleEnd()],
231
+ ],
232
+ ],
233
+ ])('it builds rule stack for a char with asterisk modifier: %s', (grammar, input, expected) => {
234
+ const result = buildRuleStack(input);
235
+ expect(result).toEqual(expected);
236
+ });
237
+
238
+ test('char with range', () => {
239
+ const input: InternalRuleDef[] = [
240
+ ichar([ord('a')]),
241
+ icharRngUpper(ord('z')),
242
+ icharAlt(ord('A')),
243
+ icharRngUpper(ord('Z')),
244
+ icharAlt(ord('_')),
245
+ iend(),
246
+ ];
247
+
248
+ expect(buildRuleStack(input)).toEqual([
249
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), ord('_')]), new RuleEnd()],
250
+ ]);
251
+ });
252
+
253
+ test('it builds rule stack for situation root ws plus newline ws star', () => {
254
+ const input: InternalRuleDef[] = [ichar([32]), icharAlt(92), icharAlt(110), iref(4), ialt(), iend()];
255
+
256
+ const expected = [[new RuleChar([32, 92, 110]), new RuleRef(4), new RuleEnd()], [new RuleEnd()]];
257
+
258
+ expect(buildRuleStack(input)).toEqual(expected);
259
+ });
260
+
261
+ test('it builds rule stack for situation root char plus char range', () => {
262
+ const input: InternalRuleDef[] = [
263
+ ichar([ord('a')]),
264
+ icharRngUpper(ord('z')),
265
+ icharAlt(ord('A')),
266
+ icharRngUpper(ord('Z')),
267
+ iref(1),
268
+ ialt(),
269
+ ichar([ord('a')]),
270
+ icharRngUpper(ord('z')),
271
+ icharAlt(ord('A')),
272
+ icharRngUpper(ord('Z')),
273
+ iend(),
274
+ ];
275
+
276
+ const expected = [
277
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()],
278
+ [new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()],
279
+ ];
280
+
281
+ expect(buildRuleStack(input)).toEqual(expected);
282
+ });
283
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/grammar-parser/build-rule-stack.ts ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { isRange, isRuleEnd } from '../grammar-graph/type-guards.ts';
2
+ import { RuleChar, RuleCharExclude, RuleEnd, RuleRef, type UnresolvedRule } from '../grammar-graph/types.ts';
3
+ import {
4
+ type InternalRuleDef,
5
+ type InternalRuleDefChar,
6
+ type InternalRuleDefCharNot,
7
+ isRuleDefAlt,
8
+ isRuleDefChar,
9
+ isRuleDefCharAlt,
10
+ isRuleDefCharNot,
11
+ isRuleDefCharRngUpper,
12
+ isRuleDefEnd,
13
+ isRuleDefRef,
14
+ } from '../rules-builder/types.ts';
15
+
16
+ export const makeCharRule = (ruleDef: InternalRuleDefChar | InternalRuleDefCharNot): RuleChar | RuleCharExclude => {
17
+ const value = ruleDef.value;
18
+ if (isRuleDefCharNot(ruleDef)) {
19
+ return new RuleCharExclude(value);
20
+ }
21
+ if (isRuleDefChar(ruleDef)) {
22
+ return new RuleChar(value);
23
+ }
24
+ throw new Error(`Unsupported rule type for make_char_rule: ${JSON.stringify(ruleDef)}`);
25
+ };
26
+
27
+ export const buildRuleStack = (linearRules: InternalRuleDef[]): UnresolvedRule[][] => {
28
+ let paths: UnresolvedRule[] = [];
29
+ const stack: UnresolvedRule[][] = [];
30
+ let idx = 0;
31
+
32
+ while (idx < linearRules.length) {
33
+ const ruleDef = linearRules[idx];
34
+ if (isRuleDefChar(ruleDef) || isRuleDefCharNot(ruleDef)) {
35
+ // this could be a single char, or a range, or a sequence of alts; we don't know until we step through it.
36
+ const charRule = makeCharRule(ruleDef);
37
+ idx += 1;
38
+ let rule = idx < linearRules.length ? linearRules[idx] : undefined;
39
+ while (idx < linearRules.length && (isRuleDefCharRngUpper(rule) || isRuleDefCharAlt(rule))) {
40
+ if (isRuleDefCharRngUpper(rule)) {
41
+ // previous rule value should be a number
42
+ const prevValue = charRule.value.pop();
43
+ if (isRange(prevValue)) {
44
+ throw new Error(`Unexpected range, expected a number but got an array: [${prevValue.join(', ')}]`);
45
+ }
46
+ if (prevValue === undefined) {
47
+ throw new Error('Unexpected undefined value');
48
+ }
49
+ charRule.value.push([prevValue, rule.value]);
50
+ }
51
+ if (isRuleDefCharAlt(rule)) {
52
+ charRule.value.push(rule.value);
53
+ }
54
+ idx += 1;
55
+ rule = idx < linearRules.length ? linearRules[idx] : undefined;
56
+ }
57
+ paths.push(charRule);
58
+ } else {
59
+ if (isRuleDefAlt(ruleDef)) {
60
+ if (paths.length === 0) {
61
+ throw new Error('Encountered alt without anything before it');
62
+ }
63
+ paths.push(new RuleEnd());
64
+ stack.push(paths);
65
+ paths = [];
66
+ } else if (isRuleDefEnd(ruleDef)) {
67
+ paths.push(new RuleEnd());
68
+ } else if (isRuleDefRef(ruleDef)) {
69
+ paths.push(new RuleRef(ruleDef.value));
70
+ } else if (isRuleDefCharAlt(ruleDef)) {
71
+ throw new Error(`Encountered char alt, should be handled by above block: ${JSON.stringify(ruleDef)}`);
72
+ } else {
73
+ throw new Error(`Unsupported rule type: ${JSON.stringify(ruleDef)}`);
74
+ }
75
+ idx += 1;
76
+ }
77
+ }
78
+
79
+ // the reference reads the last element of the path unconditionally, and so fails on an empty path
80
+ if (paths.length === 0) {
81
+ throw new RangeError('list index out of range');
82
+ }
83
+ if (!isRuleEnd(paths[paths.length - 1])) {
84
+ paths.push(new RuleEnd());
85
+ }
86
+ stack.push(paths);
87
+
88
+ return stack;
89
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/index.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import { GBNF } from './gbnf.ts';
2
+
3
+ export { GBNF };
4
+ export default GBNF;
5
+ export { ParseState } from './grammar-graph/parse-state.ts';
6
+ export { RuleChar, RuleCharExclude, RuleEnd, RuleType } from './grammar-graph/types.ts';
7
+ export type { Range, ResolvedRule } from './grammar-graph/types.ts';
8
+ export { GrammarParseError, InputParseError } from './utils/errors/index.ts';
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/index.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export { GrammarParseError, InputParseError } from '../utils/errors/index.ts';
2
+ export { RulesBuilder } from './rules-builder.ts';
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-space.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ // The characters that Python's str.isspace() considers to be whitespace.
2
+ const WHITESPACE = /^[\t-\r\x1c-\x20\x85\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]$/;
3
+
4
+ export const isSpace = (c: string): boolean => WHITESPACE.test(c);
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-word-char.test.ts ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, test } from 'vitest';
2
+ import { isWordChar } from './is-word-char.ts';
3
+
4
+ test('should return true for lowercase letters', () => {
5
+ expect(isWordChar('a')).toBe(true);
6
+ expect(isWordChar('z')).toBe(true);
7
+ });
8
+
9
+ test('should return true for uppercase letters', () => {
10
+ expect(isWordChar('A')).toBe(true);
11
+ expect(isWordChar('Z')).toBe(true);
12
+ });
13
+
14
+ test('should return false for digits', () => {
15
+ expect(isWordChar('0')).toBe(false);
16
+ expect(isWordChar('9')).toBe(false);
17
+ });
18
+
19
+ test('should return false for non word characters', () => {
20
+ expect(isWordChar('-')).toBe(false);
21
+ expect(isWordChar('@')).toBe(false);
22
+ expect(isWordChar('_')).toBe(false);
23
+ expect(isWordChar('?')).toBe(false);
24
+ expect(isWordChar(' ')).toBe(false);
25
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/is-word-char.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export const isWordChar = (c: string): boolean => /[a-zA-Z]/.test(c);
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-char.test.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, test } from 'vitest';
2
+ import { GrammarParseError } from '../utils/errors/index.ts';
3
+ import { parseChar } from './parse-char.ts';
4
+
5
+ const ord = (char: string): number => char.codePointAt(0) as number;
6
+
7
+ test.each<[string, string, number]>([
8
+ ['escaped 8-bit unicode char', 'a', ord('a')],
9
+ ['escaped 8-bit unicode char', '9', ord('9')],
10
+ ])('parse char simple (%j, %j)', (_description, char, codePoint) => {
11
+ const grammar = `root ::= "${char}" "foo"`;
12
+ expect(parseChar(grammar, 'root ::= "'.length)).toEqual([codePoint, 1]);
13
+ });
14
+
15
+ test.each<[string, string, number, number]>([
16
+ ['escaped 8-bit unicode char', '\\x2A', ord('\x2A'), 4],
17
+ ['escaped 16-bit unicode char', '\\u006F', ord('\u006F'), 6],
18
+ ['escaped 32-bit unicode char', '\\U0001F4A9', 128169, 10],
19
+ ['escaped tab char', '\\t', ord('\t'), 2],
20
+ ['escaped new line char', '\\n', ord('\n'), 2],
21
+ ['escaped \r char', '\\r', ord('\r'), 2],
22
+ ['escaped quote char', '\\"', ord('"'), 2],
23
+ ['escaped [ char', '\\[', ord('['), 2],
24
+ ['escaped ] char', '\\]', ord(']'), 2],
25
+ ['escaped \\ char', '\\\\', ord('\\'), 2],
26
+ ])('parse char complex (%j, %j)', (_description, escapedChar, codePoint, incPos) => {
27
+ const grammar = `root ::= "${escapedChar}" "foo"`;
28
+ expect(parseChar(grammar, 'root ::= "'.length)).toEqual([codePoint, incPos]);
29
+ });
30
+
31
+ test.each<[string, number]>([
32
+ ['', 0],
33
+ ['a', 1],
34
+ ['a', 2],
35
+ ])('parse char raises (%j, %i)', (input, pos) => {
36
+ expect(() => parseChar(input, pos)).toThrow(GrammarParseError);
37
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-char.ts ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getChars } from '../utils/code-points.ts';
2
+ import { GrammarParseError } from '../utils/errors/index.ts';
3
+
4
+ const ESCAPED_CONTROL_CHARS: Record<string, string> = { t: '\t', r: '\r', n: '\n' };
5
+
6
+ const NON_ASCII_WHITESPACE = /[\x85\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/;
7
+ const DECIMAL_DIGIT = /^\p{Nd}$/u;
8
+ const NON_PRINTABLE = /^[\p{C}\p{Z}]$/u;
9
+ const HEX_LITERAL = /^[ \t-\r]*([+-]?)(?:0[xX]_?)?([0-9a-fA-F]+(?:_[0-9a-fA-F]+)*)[ \t-\r]*$/;
10
+
11
+ // Unicode encodes decimal digits in contiguous runs of ten, in ascending order.
12
+ const getDecimalDigit = (char: string): number | undefined => {
13
+ if (!DECIMAL_DIGIT.test(char)) {
14
+ return undefined;
15
+ }
16
+ const codePoint = char.codePointAt(0) as number;
17
+ let start = codePoint;
18
+ while (DECIMAL_DIGIT.test(String.fromCodePoint(start - 1))) {
19
+ start -= 1;
20
+ }
21
+ return (codePoint - start) % 10;
22
+ };
23
+
24
+ const escapeCodePoint = (codePoint: number): string => {
25
+ const hex = codePoint.toString(16);
26
+ if (codePoint <= 0xff) {
27
+ return `\\x${hex.padStart(2, '0')}`;
28
+ }
29
+ if (codePoint <= 0xffff) {
30
+ return `\\u${hex.padStart(4, '0')}`;
31
+ }
32
+ return `\\U${hex.padStart(8, '0')}`;
33
+ };
34
+
35
+ // Quotes a string the way Python's repr() does, which is how the reference reports a malformed escape.
36
+ const repr = (text: string): string => {
37
+ const quote = text.includes("'") && !text.includes('"') ? '"' : "'";
38
+ let quoted = quote;
39
+ for (const char of text) {
40
+ if (char === quote || char === '\\') {
41
+ quoted += `\\${char}`;
42
+ } else if (char === '\t') {
43
+ quoted += '\\t';
44
+ } else if (char === '\n') {
45
+ quoted += '\\n';
46
+ } else if (char === '\r') {
47
+ quoted += '\\r';
48
+ } else if (char !== ' ' && NON_PRINTABLE.test(char)) {
49
+ quoted += escapeCodePoint(char.codePointAt(0) as number);
50
+ } else {
51
+ quoted += char;
52
+ }
53
+ }
54
+ return quoted + quote;
55
+ };
56
+
57
+ // Escapes are read the way the Python reference reads them, with int(text, 16). On top of plain hex digits,
58
+ // that accepts surrounding whitespace, a sign, a 0x prefix, single underscores between digits, and any
59
+ // Unicode decimal digit.
60
+ export const parseHex = (text: string): number => {
61
+ let normalized = '';
62
+ for (const char of text) {
63
+ if ((char.codePointAt(0) as number) < 127) {
64
+ normalized += char;
65
+ } else if (NON_ASCII_WHITESPACE.test(char)) {
66
+ normalized += ' ';
67
+ } else {
68
+ normalized += getDecimalDigit(char) ?? '?';
69
+ }
70
+ }
71
+
72
+ const match = HEX_LITERAL.exec(normalized);
73
+ if (match === null) {
74
+ throw new Error(`invalid literal for int() with base 16: ${repr(text)}`);
75
+ }
76
+ const value = Number.parseInt(match[2].replaceAll('_', ''), 16);
77
+ return match[1] === '-' && value !== 0 ? -value : value;
78
+ };
79
+
80
+ export const parseChar = (src: string, pos: number): [number, number] => {
81
+ const chars = getChars(src);
82
+ if (pos >= chars.length) {
83
+ throw new GrammarParseError(src, pos, 'Unexpected end of grammar input, failed to complete parse');
84
+ }
85
+ if (chars[pos] === '\\') {
86
+ const escaped = chars[pos + 1];
87
+ if (escaped === 'x') {
88
+ return [parseHex(chars.slice(pos + 2, pos + 4).join('')), 4];
89
+ }
90
+ if (escaped === 'u') {
91
+ return [parseHex(chars.slice(pos + 2, pos + 6).join('')), 6];
92
+ }
93
+ if (escaped === 'U') {
94
+ return [parseHex(chars.slice(pos + 2, pos + 10).join('')), 10];
95
+ }
96
+ if (escaped === 't' || escaped === 'r' || escaped === 'n') {
97
+ return [ESCAPED_CONTROL_CHARS[escaped].codePointAt(0) as number, 2];
98
+ }
99
+ if (escaped === '"' || escaped === '[' || escaped === ']' || escaped === '\\') {
100
+ return [escaped.codePointAt(0) as number, 2];
101
+ }
102
+ throw new GrammarParseError(src, pos, `Unknown escape at ${chars[pos]}`);
103
+ }
104
+ return [chars[pos].codePointAt(0) as number, 1];
105
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-name.test.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, test } from 'vitest';
2
+ import { GrammarParseError } from '../utils/errors/index.ts';
3
+ import { PARSE_NAME_ERROR, parseName } from './parse-name.ts';
4
+
5
+ test('should return correct name when encountering a valid name', () => {
6
+ const src = 'validName';
7
+ const name = parseName(src, 0);
8
+ expect(name).toEqual(src);
9
+ });
10
+
11
+ test('should return correct name when encountering a valid name starting at a non zero position', () => {
12
+ const src = '123validName';
13
+ const name = parseName(src, 3);
14
+ expect(name).toEqual('validName');
15
+ });
16
+
17
+ test('should throw error when encountering an invalid name', () => {
18
+ const src = '123';
19
+ const act = () => parseName(src, 0);
20
+ expect(act).toThrow(GrammarParseError);
21
+ expect(act).toThrow(new GrammarParseError(src, 0, PARSE_NAME_ERROR));
22
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-name.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getChars } from '../utils/code-points.ts';
2
+ import { GrammarParseError } from '../utils/errors/index.ts';
3
+ import { isWordChar } from './is-word-char.ts';
4
+
5
+ export const PARSE_NAME_ERROR = 'Failed to find a valid name';
6
+ const VALID_NAME_SEPARATORS = ['-', '_'];
7
+
8
+ export const parseName = (grammar: string, pos: number): string => {
9
+ const chars = getChars(grammar);
10
+ let name = '';
11
+
12
+ while (pos < chars.length && (isWordChar(chars[pos]) || VALID_NAME_SEPARATORS.includes(chars[pos]))) {
13
+ name += chars[pos];
14
+ pos += 1;
15
+ }
16
+
17
+ if (!name) {
18
+ throw new GrammarParseError(grammar, pos, PARSE_NAME_ERROR);
19
+ }
20
+ return name;
21
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-space.test.ts ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, test } from 'vitest';
2
+ import { parseSpace } from './parse-space.ts';
3
+
4
+ test('return input string when no whitespace or comments', () => {
5
+ const inputStr = 'abcdefghijk';
6
+ const pos = parseSpace(inputStr, 0, true);
7
+ expect(inputStr.slice(pos)).toEqual(inputStr);
8
+ });
9
+
10
+ test('skip leading spaces and tabs', () => {
11
+ const inputStr = ' \t abcdefghijk';
12
+ const expected = 'abcdefghijk';
13
+ const pos = parseSpace(inputStr, 0, true);
14
+ expect(inputStr.slice(pos)).toEqual(expected);
15
+ });
16
+
17
+ test('skip leading newline characters when newline ok true', () => {
18
+ const inputStr = '\n\n\r\n\r\nabcdefghijk';
19
+ const expected = 'abcdefghijk';
20
+ const pos = parseSpace(inputStr, 0, true);
21
+ expect(inputStr.slice(pos)).toEqual(expected);
22
+ });
23
+
24
+ test('not skip leading newline characters when newline ok false', () => {
25
+ const inputStr = '\n\n\r\n\r\nabcdefghijk';
26
+ const expected = '\n\n\r\n\r\nabcdefghijk';
27
+ const pos = parseSpace(inputStr, 0, false);
28
+ expect(inputStr.slice(pos)).toEqual(expected);
29
+ });
30
+
31
+ test('skip comments and leading spaces', () => {
32
+ const inputStr = ' # This is a comment\n\t abcdefghijk';
33
+ const expected = 'abcdefghijk';
34
+ const pos = parseSpace(inputStr, 0, true);
35
+ expect(inputStr.slice(pos)).toEqual(expected);
36
+ });
37
+
38
+ test('skip comments and leading newline characters when newline ok true', () => {
39
+ const inputStr = '\n\n # This is a comment\n\r\n\r\nabcdefghijk';
40
+ const expected = 'abcdefghijk';
41
+ const pos = parseSpace(inputStr, 0, true);
42
+ expect(inputStr.slice(pos)).toEqual(expected);
43
+ });
44
+
45
+ test('skip comments and leading newline characters when newline ok false', () => {
46
+ const inputStr = '\n\n # This is a comment\n\r\n\r\nabcdefghijk';
47
+ const expected = '\n\n # This is a comment\n\r\n\r\nabcdefghijk';
48
+ const pos = parseSpace(inputStr, 0, false);
49
+ expect(inputStr.slice(pos)).toEqual(expected);
50
+ });
51
+
52
+ test('return empty string if input all whitespace and comments', () => {
53
+ const inputStr = ' \t# Comment\n# Another comment\n\n';
54
+ const expected = '';
55
+ const pos = parseSpace(inputStr, 0, true);
56
+ expect(inputStr.slice(pos)).toEqual(expected);
57
+ });
58
+
59
+ test('return empty string for empty input string', () => {
60
+ const inputStr = '';
61
+ const expected = '';
62
+ const pos = parseSpace(inputStr, 0, true);
63
+ expect(inputStr.slice(pos)).toEqual(expected);
64
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/parse-space.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getChars } from '../utils/code-points.ts';
2
+
3
+ export const parseSpace = (src: string, pos: number, newlineOk: boolean): number => {
4
+ const chars = getChars(src);
5
+ while (
6
+ pos < chars.length &&
7
+ (chars[pos] === ' ' ||
8
+ chars[pos] === '\t' ||
9
+ chars[pos] === '#' ||
10
+ (newlineOk && (chars[pos] === '\r' || chars[pos] === '\n')))
11
+ ) {
12
+ if (chars[pos] === '#') {
13
+ while (pos < chars.length && chars[pos] !== '\r' && chars[pos] !== '\n') {
14
+ pos += 1;
15
+ }
16
+ } else {
17
+ pos += 1;
18
+ }
19
+ }
20
+ return pos;
21
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/rules-builder.test.ts ADDED
@@ -0,0 +1,1254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, test } from 'vitest';
2
+ import { RulesBuilder } from './rules-builder.ts';
3
+ import {
4
+ type InternalRuleDef,
5
+ InternalRuleDefAlt,
6
+ InternalRuleDefChar,
7
+ InternalRuleDefCharAlt,
8
+ InternalRuleDefCharNot,
9
+ InternalRuleDefCharRngUpper,
10
+ InternalRuleDefEnd,
11
+ InternalRuleDefReference,
12
+ } from './types.ts';
13
+
14
+ const ord = (char: string): number => char.codePointAt(0) as number;
15
+
16
+ type TestCase = [string, string, [[string, number][], InternalRuleDef[][]]];
17
+
18
+ const testCases: TestCase[] = [
19
+ [
20
+ 'single-string',
21
+ 'root ::= "foo"',
22
+ [
23
+ [['root', 0]],
24
+ [
25
+ [
26
+ new InternalRuleDefChar([ord('f')]),
27
+ new InternalRuleDefChar([ord('o')]),
28
+ new InternalRuleDefChar([ord('o')]),
29
+ new InternalRuleDefEnd(),
30
+ ],
31
+ ],
32
+ ],
33
+ ],
34
+ [
35
+ 'quote character',
36
+ 'root ::= "\\""',
37
+ [[['root', 0]], [[new InternalRuleDefChar([ord('"')]), new InternalRuleDefEnd()]]],
38
+ ],
39
+ [
40
+ 'two-lines-referencing-expression',
41
+ `root ::= foo
42
+ foo ::= "bar"`,
43
+ [
44
+ [['root', 0], ['foo', 1]],
45
+ [
46
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
47
+ [
48
+ new InternalRuleDefChar([ord('b')]),
49
+ new InternalRuleDefChar([ord('a')]),
50
+ new InternalRuleDefChar([ord('r')]),
51
+ new InternalRuleDefEnd(),
52
+ ],
53
+ ],
54
+ ],
55
+ ],
56
+ [
57
+ 'expression with dash',
58
+ `root ::= foo-bar
59
+ foo-bar ::= "bar"`,
60
+ [
61
+ [['root', 0], ['foo-bar', 1]],
62
+ [
63
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
64
+ [
65
+ new InternalRuleDefChar([ord('b')]),
66
+ new InternalRuleDefChar([ord('a')]),
67
+ new InternalRuleDefChar([ord('r')]),
68
+ new InternalRuleDefEnd(),
69
+ ],
70
+ ],
71
+ ],
72
+ ],
73
+ [
74
+ 'simple-grammar',
75
+ `
76
+ root ::= (expr "=" term "
77
+ ")+
78
+ expr ::= term ([-+*/] term)*
79
+ term ::= [0-9]+
80
+ `,
81
+ [
82
+ [
83
+ ['root', 0],
84
+ ['root_1', 1],
85
+ ['expr', 2],
86
+ ['term', 3],
87
+ ['root_4', 4],
88
+ ['expr_5', 5],
89
+ ['expr_6', 6],
90
+ ['term_7', 7],
91
+ ],
92
+ [
93
+ [new InternalRuleDefReference(4), new InternalRuleDefEnd()],
94
+ [
95
+ new InternalRuleDefReference(2),
96
+ new InternalRuleDefChar([61]),
97
+ new InternalRuleDefReference(3),
98
+ new InternalRuleDefChar([10]),
99
+ new InternalRuleDefEnd(),
100
+ ],
101
+ [new InternalRuleDefReference(3), new InternalRuleDefReference(6), new InternalRuleDefEnd()],
102
+ [new InternalRuleDefReference(7), new InternalRuleDefEnd()],
103
+ [
104
+ new InternalRuleDefReference(1),
105
+ new InternalRuleDefReference(4),
106
+ new InternalRuleDefAlt(),
107
+ new InternalRuleDefReference(1),
108
+ new InternalRuleDefEnd(),
109
+ ],
110
+ [
111
+ new InternalRuleDefChar([45]),
112
+ new InternalRuleDefCharAlt(43),
113
+ new InternalRuleDefCharAlt(42),
114
+ new InternalRuleDefCharAlt(47),
115
+ new InternalRuleDefReference(3),
116
+ new InternalRuleDefEnd(),
117
+ ],
118
+ [
119
+ new InternalRuleDefReference(5),
120
+ new InternalRuleDefReference(6),
121
+ new InternalRuleDefAlt(),
122
+ new InternalRuleDefEnd(),
123
+ ],
124
+ [
125
+ new InternalRuleDefChar([48]),
126
+ new InternalRuleDefCharRngUpper(57),
127
+ new InternalRuleDefReference(7),
128
+ new InternalRuleDefAlt(),
129
+ new InternalRuleDefChar([48]),
130
+ new InternalRuleDefCharRngUpper(57),
131
+ new InternalRuleDefEnd(),
132
+ ],
133
+ ],
134
+ ],
135
+ ],
136
+ [
137
+ 'longer-grammar',
138
+ '\n root ::= (expr "=" ws term "\n")+\n expr ::= term ([-+*/] term)*\n term ::= ident | num | "(" ws expr ")" ws\n ident ::= [a-z] [a-z0-9_]* ws\n num ::= [0-9]+ ws\n ws ::= [ \t\n]*\n ',
139
+ [
140
+ [
141
+ ['root', 0],
142
+ ['root_1', 1],
143
+ ['expr', 2],
144
+ ['ws', 3],
145
+ ['term', 4],
146
+ ['root_5', 5],
147
+ ['expr_6', 6],
148
+ ['expr_7', 7],
149
+ ['ident', 8],
150
+ ['num', 9],
151
+ ['ident_10', 10],
152
+ ['num_11', 11],
153
+ ['ws_12', 12],
154
+ ],
155
+ [
156
+ [new InternalRuleDefReference(5), new InternalRuleDefEnd()],
157
+ [
158
+ new InternalRuleDefReference(2),
159
+ new InternalRuleDefChar([61]),
160
+ new InternalRuleDefReference(3),
161
+ new InternalRuleDefReference(4),
162
+ new InternalRuleDefChar([10]),
163
+ new InternalRuleDefEnd(),
164
+ ],
165
+ [new InternalRuleDefReference(4), new InternalRuleDefReference(7), new InternalRuleDefEnd()],
166
+ [new InternalRuleDefReference(12), new InternalRuleDefEnd()],
167
+ [
168
+ new InternalRuleDefReference(8),
169
+ new InternalRuleDefAlt(),
170
+ new InternalRuleDefReference(9),
171
+ new InternalRuleDefAlt(),
172
+ new InternalRuleDefChar([40]),
173
+ new InternalRuleDefReference(3),
174
+ new InternalRuleDefReference(2),
175
+ new InternalRuleDefChar([41]),
176
+ new InternalRuleDefReference(3),
177
+ new InternalRuleDefEnd(),
178
+ ],
179
+ [
180
+ new InternalRuleDefReference(1),
181
+ new InternalRuleDefReference(5),
182
+ new InternalRuleDefAlt(),
183
+ new InternalRuleDefReference(1),
184
+ new InternalRuleDefEnd(),
185
+ ],
186
+ [
187
+ new InternalRuleDefChar([45]),
188
+ new InternalRuleDefCharAlt(43),
189
+ new InternalRuleDefCharAlt(42),
190
+ new InternalRuleDefCharAlt(47),
191
+ new InternalRuleDefReference(4),
192
+ new InternalRuleDefEnd(),
193
+ ],
194
+ [
195
+ new InternalRuleDefReference(6),
196
+ new InternalRuleDefReference(7),
197
+ new InternalRuleDefAlt(),
198
+ new InternalRuleDefEnd(),
199
+ ],
200
+ [
201
+ new InternalRuleDefChar([97]),
202
+ new InternalRuleDefCharRngUpper(122),
203
+ new InternalRuleDefReference(10),
204
+ new InternalRuleDefReference(3),
205
+ new InternalRuleDefEnd(),
206
+ ],
207
+ [new InternalRuleDefReference(11), new InternalRuleDefReference(3), new InternalRuleDefEnd()],
208
+ [
209
+ new InternalRuleDefChar([97]),
210
+ new InternalRuleDefCharRngUpper(122),
211
+ new InternalRuleDefCharAlt(48),
212
+ new InternalRuleDefCharRngUpper(57),
213
+ new InternalRuleDefCharAlt(95),
214
+ new InternalRuleDefReference(10),
215
+ new InternalRuleDefAlt(),
216
+ new InternalRuleDefEnd(),
217
+ ],
218
+ [
219
+ new InternalRuleDefChar([48]),
220
+ new InternalRuleDefCharRngUpper(57),
221
+ new InternalRuleDefReference(11),
222
+ new InternalRuleDefAlt(),
223
+ new InternalRuleDefChar([48]),
224
+ new InternalRuleDefCharRngUpper(57),
225
+ new InternalRuleDefEnd(),
226
+ ],
227
+ [
228
+ new InternalRuleDefChar([32]),
229
+ new InternalRuleDefCharAlt(9),
230
+ new InternalRuleDefCharAlt(10),
231
+ new InternalRuleDefReference(12),
232
+ new InternalRuleDefAlt(),
233
+ new InternalRuleDefEnd(),
234
+ ],
235
+ ],
236
+ ],
237
+ ],
238
+ [
239
+ 'character unicode grammar',
240
+ 'root ::= [ぁ-ゟ]',
241
+ [
242
+ [['root', 0]],
243
+ [[new InternalRuleDefChar([ord('ぁ')]), new InternalRuleDefCharRngUpper(ord('ゟ')), new InternalRuleDefEnd()]],
244
+ ],
245
+ ],
246
+ [
247
+ 'character alts grammar',
248
+ 'root ::= [az]',
249
+ [
250
+ [['root', 0]],
251
+ [[new InternalRuleDefChar([ord('a')]), new InternalRuleDefCharAlt(ord('z')), new InternalRuleDefEnd()]],
252
+ ],
253
+ ],
254
+ [
255
+ 'character range grammar',
256
+ 'root ::= [a-zA-Z0-9]',
257
+ [
258
+ [['root', 0]],
259
+ [
260
+ [
261
+ new InternalRuleDefChar([ord('a')]),
262
+ new InternalRuleDefCharRngUpper(ord('z')),
263
+ new InternalRuleDefCharAlt(ord('A')),
264
+ new InternalRuleDefCharRngUpper(ord('Z')),
265
+ new InternalRuleDefCharAlt(ord('0')),
266
+ new InternalRuleDefCharRngUpper(ord('9')),
267
+ new InternalRuleDefEnd(),
268
+ ],
269
+ ],
270
+ ],
271
+ ],
272
+ [
273
+ 'character range grammar with dash at end',
274
+ 'root ::= [a-z-]',
275
+ [
276
+ [['root', 0]],
277
+ [
278
+ [
279
+ new InternalRuleDefChar([ord('a')]),
280
+ new InternalRuleDefCharRngUpper(ord('z')),
281
+ new InternalRuleDefCharAlt(ord('-')),
282
+ new InternalRuleDefEnd(),
283
+ ],
284
+ ],
285
+ ],
286
+ ],
287
+ [
288
+ 'grouping',
289
+ 'root ::= "f" ("b" | "a")',
290
+ [
291
+ [['root', 0], ['root_1', 1]],
292
+ [
293
+ [new InternalRuleDefChar([ord('f')]), new InternalRuleDefReference(1), new InternalRuleDefEnd()],
294
+ [
295
+ new InternalRuleDefChar([ord('b')]),
296
+ new InternalRuleDefAlt(),
297
+ new InternalRuleDefChar([ord('a')]),
298
+ new InternalRuleDefEnd(),
299
+ ],
300
+ ],
301
+ ],
302
+ ],
303
+ [
304
+ 'optional',
305
+ 'root ::= "f"?',
306
+ [
307
+ [['root', 0], ['root_1', 1]],
308
+ [
309
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
310
+ [new InternalRuleDefChar([ord('f')]), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
311
+ ],
312
+ ],
313
+ ],
314
+ [
315
+ 'repeating',
316
+ 'root ::= "f"*',
317
+ [
318
+ [['root', 0], ['root_1', 1]],
319
+ [
320
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
321
+ [
322
+ new InternalRuleDefChar([ord('f')]),
323
+ new InternalRuleDefReference(1),
324
+ new InternalRuleDefAlt(),
325
+ new InternalRuleDefEnd(),
326
+ ],
327
+ ],
328
+ ],
329
+ ],
330
+ [
331
+ 'repeating-at-least-once',
332
+ 'root ::= "f"+',
333
+ [
334
+ [['root', 0], ['root_1', 1]],
335
+ [
336
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
337
+ [
338
+ new InternalRuleDefChar([ord('f')]),
339
+ new InternalRuleDefReference(1),
340
+ new InternalRuleDefAlt(),
341
+ new InternalRuleDefChar([ord('f')]),
342
+ new InternalRuleDefEnd(),
343
+ ],
344
+ ],
345
+ ],
346
+ ],
347
+ [
348
+ 'character range with optional repeating',
349
+ 'root ::= [a-zA-Z0-9]*',
350
+ [
351
+ [['root', 0], ['root_1', 1]],
352
+ [
353
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
354
+ [
355
+ new InternalRuleDefChar([ord('a')]),
356
+ new InternalRuleDefCharRngUpper(ord('z')),
357
+ new InternalRuleDefCharAlt(ord('A')),
358
+ new InternalRuleDefCharRngUpper(ord('Z')),
359
+ new InternalRuleDefCharAlt(ord('0')),
360
+ new InternalRuleDefCharRngUpper(ord('9')),
361
+ new InternalRuleDefReference(1),
362
+ new InternalRuleDefAlt(),
363
+ new InternalRuleDefEnd(),
364
+ ],
365
+ ],
366
+ ],
367
+ ],
368
+ [
369
+ 'grouping-repeating',
370
+ 'root ::= "f" ("b" | "a")*',
371
+ [
372
+ [['root', 0], ['root_1', 1], ['root_2', 2]],
373
+ [
374
+ [new InternalRuleDefChar([ord('f')]), new InternalRuleDefReference(2), new InternalRuleDefEnd()],
375
+ [
376
+ new InternalRuleDefChar([ord('b')]),
377
+ new InternalRuleDefAlt(),
378
+ new InternalRuleDefChar([ord('a')]),
379
+ new InternalRuleDefEnd(),
380
+ ],
381
+ [
382
+ new InternalRuleDefReference(1),
383
+ new InternalRuleDefReference(2),
384
+ new InternalRuleDefAlt(),
385
+ new InternalRuleDefEnd(),
386
+ ],
387
+ ],
388
+ ],
389
+ ],
390
+ [
391
+ 'negation',
392
+ 'root ::= [^\\n]',
393
+ [[['root', 0]], [[new InternalRuleDefCharNot([ord('\n')]), new InternalRuleDefEnd()]]],
394
+ ],
395
+ [
396
+ 'negation of range',
397
+ 'root ::= [^0-9]',
398
+ [
399
+ [['root', 0]],
400
+ [[new InternalRuleDefCharNot([ord('0')]), new InternalRuleDefCharRngUpper(ord('9')), new InternalRuleDefEnd()]],
401
+ ],
402
+ ],
403
+ [
404
+ 'negation with after',
405
+ 'root ::= [^\\n]+ "\\n"',
406
+ [
407
+ [['root', 0], ['root_1', 1]],
408
+ [
409
+ [new InternalRuleDefReference(1), new InternalRuleDefChar([ord('\n')]), new InternalRuleDefEnd()],
410
+ [
411
+ new InternalRuleDefCharNot([ord('\n')]),
412
+ new InternalRuleDefReference(1),
413
+ new InternalRuleDefAlt(),
414
+ new InternalRuleDefCharNot([ord('\n')]),
415
+ new InternalRuleDefEnd(),
416
+ ],
417
+ ],
418
+ ],
419
+ ],
420
+ [
421
+ 'longer negation',
422
+ 'root ::= "\\"" ( [^"abcdefgh])* ',
423
+ [
424
+ [['root', 0], ['root_1', 1], ['root_2', 2]],
425
+ [
426
+ [new InternalRuleDefChar([34]), new InternalRuleDefReference(2), new InternalRuleDefEnd()],
427
+ [
428
+ new InternalRuleDefCharNot([34]),
429
+ new InternalRuleDefCharAlt(97),
430
+ new InternalRuleDefCharAlt(98),
431
+ new InternalRuleDefCharAlt(99),
432
+ new InternalRuleDefCharAlt(100),
433
+ new InternalRuleDefCharAlt(101),
434
+ new InternalRuleDefCharAlt(102),
435
+ new InternalRuleDefCharAlt(103),
436
+ new InternalRuleDefCharAlt(104),
437
+ new InternalRuleDefEnd(),
438
+ ],
439
+ [
440
+ new InternalRuleDefReference(1),
441
+ new InternalRuleDefReference(2),
442
+ new InternalRuleDefAlt(),
443
+ new InternalRuleDefEnd(),
444
+ ],
445
+ ],
446
+ ],
447
+ ],
448
+ [
449
+ 'longer negation with a range',
450
+ 'root ::= "\\"" ( [^"abcdefghA-Z])* ',
451
+ [
452
+ [['root', 0], ['root_1', 1], ['root_2', 2]],
453
+ [
454
+ [new InternalRuleDefChar([34]), new InternalRuleDefReference(2), new InternalRuleDefEnd()],
455
+ [
456
+ new InternalRuleDefCharNot([34]),
457
+ new InternalRuleDefCharAlt(97),
458
+ new InternalRuleDefCharAlt(98),
459
+ new InternalRuleDefCharAlt(99),
460
+ new InternalRuleDefCharAlt(100),
461
+ new InternalRuleDefCharAlt(101),
462
+ new InternalRuleDefCharAlt(102),
463
+ new InternalRuleDefCharAlt(103),
464
+ new InternalRuleDefCharAlt(104),
465
+ new InternalRuleDefCharAlt(65),
466
+ new InternalRuleDefCharRngUpper(ord('Z')),
467
+ new InternalRuleDefEnd(),
468
+ ],
469
+ [
470
+ new InternalRuleDefReference(1),
471
+ new InternalRuleDefReference(2),
472
+ new InternalRuleDefAlt(),
473
+ new InternalRuleDefEnd(),
474
+ ],
475
+ ],
476
+ ],
477
+ ],
478
+ ...(
479
+ [
480
+ ['escaped 8-bit unicode char', '\\x2A', ord('*')],
481
+ ['escaped 16-bit unicode char', '\\u006F', ord('o')],
482
+ ['escaped 32-bit unicode char', '\\U0001F4A9', 128169],
483
+ ['escaped tab char', '\\t', ord('\t')],
484
+ ['escaped new line char', '\\n', ord('\n')],
485
+ ['escaped \r char', '\\r', ord('\r')],
486
+ ['escaped quote char', '\\"', ord('"')],
487
+ ['escaped [ char', '\\[', ord('[')],
488
+ ['escaped ] char', '\\]', ord(']')],
489
+ ['escaped \\ char', '\\\\', ord('\\')],
490
+ ] as [string, string, number][]
491
+ ).map(([key, escapedChar, actualChar]): TestCase => [
492
+ key,
493
+ `root ::= "${escapedChar}"`,
494
+ [[['root', 0]], [[new InternalRuleDefChar([actualChar]), new InternalRuleDefEnd()]]],
495
+ ]),
496
+ [
497
+ 'simple arithmetic',
498
+ `
499
+ root ::= (expr "=" term "\\n")+
500
+ expr ::= term ([-+*/] term)*
501
+ term ::= [0-9]+
502
+ `,
503
+ [
504
+ [
505
+ ['root', 0],
506
+ ['root_1', 1],
507
+ ['expr', 2],
508
+ ['term', 3],
509
+ ['root_4', 4],
510
+ ['expr_5', 5],
511
+ ['expr_6', 6],
512
+ ['term_7', 7],
513
+ ],
514
+ [
515
+ [new InternalRuleDefReference(4), new InternalRuleDefEnd()],
516
+ [
517
+ new InternalRuleDefReference(2),
518
+ new InternalRuleDefChar([61]),
519
+ new InternalRuleDefReference(3),
520
+ new InternalRuleDefChar([10]),
521
+ new InternalRuleDefEnd(),
522
+ ],
523
+ [new InternalRuleDefReference(3), new InternalRuleDefReference(6), new InternalRuleDefEnd()],
524
+ [new InternalRuleDefReference(7), new InternalRuleDefEnd()],
525
+ [
526
+ new InternalRuleDefReference(1),
527
+ new InternalRuleDefReference(4),
528
+ new InternalRuleDefAlt(),
529
+ new InternalRuleDefReference(1),
530
+ new InternalRuleDefEnd(),
531
+ ],
532
+ [
533
+ new InternalRuleDefChar([45]),
534
+ new InternalRuleDefCharAlt(43),
535
+ new InternalRuleDefCharAlt(42),
536
+ new InternalRuleDefCharAlt(47),
537
+ new InternalRuleDefReference(3),
538
+ new InternalRuleDefEnd(),
539
+ ],
540
+ [
541
+ new InternalRuleDefReference(5),
542
+ new InternalRuleDefReference(6),
543
+ new InternalRuleDefAlt(),
544
+ new InternalRuleDefEnd(),
545
+ ],
546
+ [
547
+ new InternalRuleDefChar([48]),
548
+ new InternalRuleDefCharRngUpper(57),
549
+ new InternalRuleDefReference(7),
550
+ new InternalRuleDefAlt(),
551
+ new InternalRuleDefChar([48]),
552
+ new InternalRuleDefCharRngUpper(57),
553
+ new InternalRuleDefEnd(),
554
+ ],
555
+ ],
556
+ ],
557
+ ],
558
+ [
559
+ 'ranges with chars',
560
+ `
561
+ root ::= [a-z0-9_]*
562
+ `,
563
+ [
564
+ [['root', 0], ['root_1', 1]],
565
+ [
566
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
567
+ [
568
+ new InternalRuleDefChar([97]),
569
+ new InternalRuleDefCharRngUpper(122),
570
+ new InternalRuleDefCharAlt(48),
571
+ new InternalRuleDefCharRngUpper(57),
572
+ new InternalRuleDefCharAlt(95),
573
+ new InternalRuleDefReference(1),
574
+ new InternalRuleDefAlt(),
575
+ new InternalRuleDefEnd(),
576
+ ],
577
+ ],
578
+ ],
579
+ ],
580
+ [
581
+ 'nested ranges with chars',
582
+ `
583
+ root ::= [a-z] [a-z0-9_]*
584
+ `,
585
+ [
586
+ [['root', 0], ['root_1', 1]],
587
+ [
588
+ [
589
+ new InternalRuleDefChar([97]),
590
+ new InternalRuleDefCharRngUpper(122),
591
+ new InternalRuleDefReference(1),
592
+ new InternalRuleDefEnd(),
593
+ ],
594
+ [
595
+ new InternalRuleDefChar([97]),
596
+ new InternalRuleDefCharRngUpper(122),
597
+ new InternalRuleDefCharAlt(48),
598
+ new InternalRuleDefCharRngUpper(57),
599
+ new InternalRuleDefCharAlt(95),
600
+ new InternalRuleDefReference(1),
601
+ new InternalRuleDefAlt(),
602
+ new InternalRuleDefEnd(),
603
+ ],
604
+ ],
605
+ ],
606
+ ],
607
+ [
608
+ 'expression with nested range with chars',
609
+ `
610
+ root ::= ident
611
+ ident ::= [a-z] [a-z0-9_]* ws
612
+ ws ::= [ \\t\\n]*
613
+ `,
614
+ [
615
+ [['root', 0], ['ident', 1], ['ident_2', 2], ['ws', 3], ['ws_4', 4]],
616
+ [
617
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
618
+ [
619
+ new InternalRuleDefChar([97]),
620
+ new InternalRuleDefCharRngUpper(122),
621
+ new InternalRuleDefReference(2),
622
+ new InternalRuleDefReference(3),
623
+ new InternalRuleDefEnd(),
624
+ ],
625
+ [
626
+ new InternalRuleDefChar([97]),
627
+ new InternalRuleDefCharRngUpper(122),
628
+ new InternalRuleDefCharAlt(48),
629
+ new InternalRuleDefCharRngUpper(57),
630
+ new InternalRuleDefCharAlt(95),
631
+ new InternalRuleDefReference(2),
632
+ new InternalRuleDefAlt(),
633
+ new InternalRuleDefEnd(),
634
+ ],
635
+ [new InternalRuleDefReference(4), new InternalRuleDefEnd()],
636
+ [
637
+ new InternalRuleDefChar([32]),
638
+ new InternalRuleDefCharAlt(9),
639
+ new InternalRuleDefCharAlt(10),
640
+ new InternalRuleDefReference(4),
641
+ new InternalRuleDefAlt(),
642
+ new InternalRuleDefEnd(),
643
+ ],
644
+ ],
645
+ ],
646
+ ],
647
+ [
648
+ 'lots of escapes',
649
+ 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\"',
650
+ [
651
+ [['root', 0]],
652
+ [
653
+ [
654
+ new InternalRuleDefChar([42]),
655
+ new InternalRuleDefChar([111]),
656
+ new InternalRuleDefChar([128169]),
657
+ new InternalRuleDefChar([9]),
658
+ new InternalRuleDefChar([10]),
659
+ new InternalRuleDefChar([13]),
660
+ new InternalRuleDefChar([34]),
661
+ new InternalRuleDefChar([91]),
662
+ new InternalRuleDefChar([93]),
663
+ new InternalRuleDefChar([92]),
664
+ new InternalRuleDefEnd(),
665
+ ],
666
+ ],
667
+ ],
668
+ ],
669
+ [
670
+ 'lots of escape and alternate escapes',
671
+ `root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\" (
672
+ "\\x2A" | "\\u006F" | "\\U0001F4A9" | "\\t" | "\\n" | "\\r" | "\\"" | "\\[" | "\\]" | "\\\\" )`,
673
+ [
674
+ [['root', 0], ['root_1', 1]],
675
+ [
676
+ [
677
+ new InternalRuleDefChar([42]),
678
+ new InternalRuleDefChar([111]),
679
+ new InternalRuleDefChar([128169]),
680
+ new InternalRuleDefChar([9]),
681
+ new InternalRuleDefChar([10]),
682
+ new InternalRuleDefChar([13]),
683
+ new InternalRuleDefChar([34]),
684
+ new InternalRuleDefChar([91]),
685
+ new InternalRuleDefChar([93]),
686
+ new InternalRuleDefChar([92]),
687
+ new InternalRuleDefReference(1),
688
+ new InternalRuleDefEnd(),
689
+ ],
690
+ [
691
+ new InternalRuleDefChar([42]),
692
+ new InternalRuleDefAlt(),
693
+ new InternalRuleDefChar([111]),
694
+ new InternalRuleDefAlt(),
695
+ new InternalRuleDefChar([128169]),
696
+ new InternalRuleDefAlt(),
697
+ new InternalRuleDefChar([9]),
698
+ new InternalRuleDefAlt(),
699
+ new InternalRuleDefChar([10]),
700
+ new InternalRuleDefAlt(),
701
+ new InternalRuleDefChar([13]),
702
+ new InternalRuleDefAlt(),
703
+ new InternalRuleDefChar([34]),
704
+ new InternalRuleDefAlt(),
705
+ new InternalRuleDefChar([91]),
706
+ new InternalRuleDefAlt(),
707
+ new InternalRuleDefChar([93]),
708
+ new InternalRuleDefAlt(),
709
+ new InternalRuleDefChar([92]),
710
+ new InternalRuleDefEnd(),
711
+ ],
712
+ ],
713
+ ],
714
+ ],
715
+ [
716
+ 'arithmetic',
717
+ `
718
+ root ::= (expr "=" ws term "\\n")+
719
+ expr ::= term ([-+*/] term)*
720
+ term ::= ident | num | "(" ws expr ")" ws
721
+ ident ::= [a-z] [a-z0-9_]* ws
722
+ num ::= [0-9]+ ws
723
+ ws ::= [ \\t\\n]*
724
+ `,
725
+ [
726
+ [
727
+ ['root', 0],
728
+ ['root_1', 1],
729
+ ['expr', 2],
730
+ ['ws', 3],
731
+ ['term', 4],
732
+ ['root_5', 5],
733
+ ['expr_6', 6],
734
+ ['expr_7', 7],
735
+ ['ident', 8],
736
+ ['num', 9],
737
+ ['ident_10', 10],
738
+ ['num_11', 11],
739
+ ['ws_12', 12],
740
+ ],
741
+ [
742
+ [new InternalRuleDefReference(5), new InternalRuleDefEnd()],
743
+ [
744
+ new InternalRuleDefReference(2),
745
+ new InternalRuleDefChar([61]),
746
+ new InternalRuleDefReference(3),
747
+ new InternalRuleDefReference(4),
748
+ new InternalRuleDefChar([10]),
749
+ new InternalRuleDefEnd(),
750
+ ],
751
+ [new InternalRuleDefReference(4), new InternalRuleDefReference(7), new InternalRuleDefEnd()],
752
+ [new InternalRuleDefReference(12), new InternalRuleDefEnd()],
753
+ [
754
+ new InternalRuleDefReference(8),
755
+ new InternalRuleDefAlt(),
756
+ new InternalRuleDefReference(9),
757
+ new InternalRuleDefAlt(),
758
+ new InternalRuleDefChar([40]),
759
+ new InternalRuleDefReference(3),
760
+ new InternalRuleDefReference(2),
761
+ new InternalRuleDefChar([41]),
762
+ new InternalRuleDefReference(3),
763
+ new InternalRuleDefEnd(),
764
+ ],
765
+ [
766
+ new InternalRuleDefReference(1),
767
+ new InternalRuleDefReference(5),
768
+ new InternalRuleDefAlt(),
769
+ new InternalRuleDefReference(1),
770
+ new InternalRuleDefEnd(),
771
+ ],
772
+ [
773
+ new InternalRuleDefChar([45]),
774
+ new InternalRuleDefCharAlt(43),
775
+ new InternalRuleDefCharAlt(42),
776
+ new InternalRuleDefCharAlt(47),
777
+ new InternalRuleDefReference(4),
778
+ new InternalRuleDefEnd(),
779
+ ],
780
+ [
781
+ new InternalRuleDefReference(6),
782
+ new InternalRuleDefReference(7),
783
+ new InternalRuleDefAlt(),
784
+ new InternalRuleDefEnd(),
785
+ ],
786
+ [
787
+ new InternalRuleDefChar([97]),
788
+ new InternalRuleDefCharRngUpper(122),
789
+ new InternalRuleDefReference(10),
790
+ new InternalRuleDefReference(3),
791
+ new InternalRuleDefEnd(),
792
+ ],
793
+ [new InternalRuleDefReference(11), new InternalRuleDefReference(3), new InternalRuleDefEnd()],
794
+ [
795
+ new InternalRuleDefChar([97]),
796
+ new InternalRuleDefCharRngUpper(122),
797
+ new InternalRuleDefCharAlt(48),
798
+ new InternalRuleDefCharRngUpper(57),
799
+ new InternalRuleDefCharAlt(95),
800
+ new InternalRuleDefReference(10),
801
+ new InternalRuleDefAlt(),
802
+ new InternalRuleDefEnd(),
803
+ ],
804
+ [
805
+ new InternalRuleDefChar([48]),
806
+ new InternalRuleDefCharRngUpper(57),
807
+ new InternalRuleDefReference(11),
808
+ new InternalRuleDefAlt(),
809
+ new InternalRuleDefChar([48]),
810
+ new InternalRuleDefCharRngUpper(57),
811
+ new InternalRuleDefEnd(),
812
+ ],
813
+ [
814
+ new InternalRuleDefChar([32]),
815
+ new InternalRuleDefCharAlt(9),
816
+ new InternalRuleDefCharAlt(10),
817
+ new InternalRuleDefReference(12),
818
+ new InternalRuleDefAlt(),
819
+ new InternalRuleDefEnd(),
820
+ ],
821
+ ],
822
+ ],
823
+ ],
824
+ [
825
+ 'json.gbnf (string)',
826
+ `
827
+ root ::=
828
+ "\\"" (
829
+ [^"\\\\\\x7F\\x00-\\x1F] |
830
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
831
+ )* "\\""
832
+ `,
833
+ [
834
+ [['root', 0], ['root_1', 1], ['root_2', 2], ['root_3', 3]],
835
+ [
836
+ [
837
+ new InternalRuleDefChar([34]),
838
+ new InternalRuleDefReference(3),
839
+ new InternalRuleDefChar([34]),
840
+ new InternalRuleDefEnd(),
841
+ ],
842
+ [
843
+ new InternalRuleDefCharNot([34]),
844
+ new InternalRuleDefCharAlt(92),
845
+ new InternalRuleDefCharAlt(127),
846
+ new InternalRuleDefCharAlt(0),
847
+ new InternalRuleDefCharRngUpper(31),
848
+ new InternalRuleDefAlt(),
849
+ new InternalRuleDefChar([92]),
850
+ new InternalRuleDefReference(2),
851
+ new InternalRuleDefEnd(),
852
+ ],
853
+ [
854
+ new InternalRuleDefChar([34]),
855
+ new InternalRuleDefCharAlt(92),
856
+ new InternalRuleDefCharAlt(47),
857
+ new InternalRuleDefCharAlt(98),
858
+ new InternalRuleDefCharAlt(102),
859
+ new InternalRuleDefCharAlt(110),
860
+ new InternalRuleDefCharAlt(114),
861
+ new InternalRuleDefCharAlt(116),
862
+ new InternalRuleDefAlt(),
863
+ new InternalRuleDefChar([117]),
864
+ new InternalRuleDefChar([48]),
865
+ new InternalRuleDefCharRngUpper(57),
866
+ new InternalRuleDefCharAlt(97),
867
+ new InternalRuleDefCharRngUpper(102),
868
+ new InternalRuleDefCharAlt(65),
869
+ new InternalRuleDefCharRngUpper(70),
870
+ new InternalRuleDefChar([48]),
871
+ new InternalRuleDefCharRngUpper(57),
872
+ new InternalRuleDefCharAlt(97),
873
+ new InternalRuleDefCharRngUpper(102),
874
+ new InternalRuleDefCharAlt(65),
875
+ new InternalRuleDefCharRngUpper(70),
876
+ new InternalRuleDefChar([48]),
877
+ new InternalRuleDefCharRngUpper(57),
878
+ new InternalRuleDefCharAlt(97),
879
+ new InternalRuleDefCharRngUpper(102),
880
+ new InternalRuleDefCharAlt(65),
881
+ new InternalRuleDefCharRngUpper(70),
882
+ new InternalRuleDefChar([48]),
883
+ new InternalRuleDefCharRngUpper(57),
884
+ new InternalRuleDefCharAlt(97),
885
+ new InternalRuleDefCharRngUpper(102),
886
+ new InternalRuleDefCharAlt(65),
887
+ new InternalRuleDefCharRngUpper(70),
888
+ new InternalRuleDefEnd(),
889
+ ],
890
+ [
891
+ new InternalRuleDefReference(1),
892
+ new InternalRuleDefReference(3),
893
+ new InternalRuleDefAlt(),
894
+ new InternalRuleDefEnd(),
895
+ ],
896
+ ],
897
+ ],
898
+ ],
899
+ [
900
+ 'json.gbnf (full)',
901
+ `
902
+ root ::= object
903
+ value ::= object | array | string | number | ("true" | "false" | "null") ws
904
+ object ::=
905
+ "{" ws (
906
+ string ":" ws value
907
+ ("," ws string ":" ws value)*
908
+ )? "}" ws
909
+ array ::=
910
+ "[" ws (
911
+ value
912
+ ("," ws value)*
913
+ )? "]" ws
914
+ string ::=
915
+ "\\"" (
916
+ [^"\\\\\\x7F\\x00-\\x1F] |
917
+ "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
918
+ )* "\\"" ws
919
+ number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
920
+ # Optional space: by convention, applied in this grammar after literal chars when allowed
921
+ ws ::= ([ \\t\\n] ws)?
922
+ `,
923
+ [
924
+ [
925
+ ['root', 0],
926
+ ['object', 1],
927
+ ['value', 2],
928
+ ['array', 3],
929
+ ['string', 4],
930
+ ['number', 5],
931
+ ['value_6', 6],
932
+ ['ws', 7],
933
+ ['object_8', 8],
934
+ ['object_9', 9],
935
+ ['object_10', 10],
936
+ ['object_11', 11],
937
+ ['array_12', 12],
938
+ ['array_13', 13],
939
+ ['array_14', 14],
940
+ ['array_15', 15],
941
+ ['string_16', 16],
942
+ ['string_17', 17],
943
+ ['string_18', 18],
944
+ ['number_19', 19],
945
+ ['number_20', 20],
946
+ ['number_21', 21],
947
+ ['number_22', 22],
948
+ ['number_23', 23],
949
+ ['number_24', 24],
950
+ ['number_25', 25],
951
+ ['number_26', 26],
952
+ ['number_27', 27],
953
+ ['number_28', 28],
954
+ ['number_29', 29],
955
+ ['ws_30', 30],
956
+ ['ws_31', 31],
957
+ ],
958
+ [
959
+ [new InternalRuleDefReference(1), new InternalRuleDefEnd()],
960
+ [
961
+ new InternalRuleDefChar([123]),
962
+ new InternalRuleDefReference(7),
963
+ new InternalRuleDefReference(11),
964
+ new InternalRuleDefChar([125]),
965
+ new InternalRuleDefReference(7),
966
+ new InternalRuleDefEnd(),
967
+ ],
968
+ [
969
+ new InternalRuleDefReference(1),
970
+ new InternalRuleDefAlt(),
971
+ new InternalRuleDefReference(3),
972
+ new InternalRuleDefAlt(),
973
+ new InternalRuleDefReference(4),
974
+ new InternalRuleDefAlt(),
975
+ new InternalRuleDefReference(5),
976
+ new InternalRuleDefAlt(),
977
+ new InternalRuleDefReference(6),
978
+ new InternalRuleDefReference(7),
979
+ new InternalRuleDefEnd(),
980
+ ],
981
+ [
982
+ new InternalRuleDefChar([91]),
983
+ new InternalRuleDefReference(7),
984
+ new InternalRuleDefReference(15),
985
+ new InternalRuleDefChar([93]),
986
+ new InternalRuleDefReference(7),
987
+ new InternalRuleDefEnd(),
988
+ ],
989
+ [
990
+ new InternalRuleDefChar([34]),
991
+ new InternalRuleDefReference(18),
992
+ new InternalRuleDefChar([34]),
993
+ new InternalRuleDefReference(7),
994
+ new InternalRuleDefEnd(),
995
+ ],
996
+ [
997
+ new InternalRuleDefReference(19),
998
+ new InternalRuleDefReference(25),
999
+ new InternalRuleDefReference(29),
1000
+ new InternalRuleDefReference(7),
1001
+ new InternalRuleDefEnd(),
1002
+ ],
1003
+ [
1004
+ new InternalRuleDefChar([116]),
1005
+ new InternalRuleDefChar([114]),
1006
+ new InternalRuleDefChar([117]),
1007
+ new InternalRuleDefChar([101]),
1008
+ new InternalRuleDefAlt(),
1009
+ new InternalRuleDefChar([102]),
1010
+ new InternalRuleDefChar([97]),
1011
+ new InternalRuleDefChar([108]),
1012
+ new InternalRuleDefChar([115]),
1013
+ new InternalRuleDefChar([101]),
1014
+ new InternalRuleDefAlt(),
1015
+ new InternalRuleDefChar([110]),
1016
+ new InternalRuleDefChar([117]),
1017
+ new InternalRuleDefChar([108]),
1018
+ new InternalRuleDefChar([108]),
1019
+ new InternalRuleDefEnd(),
1020
+ ],
1021
+ [new InternalRuleDefReference(31), new InternalRuleDefEnd()],
1022
+ [
1023
+ new InternalRuleDefReference(4),
1024
+ new InternalRuleDefChar([58]),
1025
+ new InternalRuleDefReference(7),
1026
+ new InternalRuleDefReference(2),
1027
+ new InternalRuleDefReference(10),
1028
+ new InternalRuleDefEnd(),
1029
+ ],
1030
+ [
1031
+ new InternalRuleDefChar([44]),
1032
+ new InternalRuleDefReference(7),
1033
+ new InternalRuleDefReference(4),
1034
+ new InternalRuleDefChar([58]),
1035
+ new InternalRuleDefReference(7),
1036
+ new InternalRuleDefReference(2),
1037
+ new InternalRuleDefEnd(),
1038
+ ],
1039
+ [
1040
+ new InternalRuleDefReference(9),
1041
+ new InternalRuleDefReference(10),
1042
+ new InternalRuleDefAlt(),
1043
+ new InternalRuleDefEnd(),
1044
+ ],
1045
+ [new InternalRuleDefReference(8), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1046
+ [new InternalRuleDefReference(2), new InternalRuleDefReference(14), new InternalRuleDefEnd()],
1047
+ [
1048
+ new InternalRuleDefChar([44]),
1049
+ new InternalRuleDefReference(7),
1050
+ new InternalRuleDefReference(2),
1051
+ new InternalRuleDefEnd(),
1052
+ ],
1053
+ [
1054
+ new InternalRuleDefReference(13),
1055
+ new InternalRuleDefReference(14),
1056
+ new InternalRuleDefAlt(),
1057
+ new InternalRuleDefEnd(),
1058
+ ],
1059
+ [new InternalRuleDefReference(12), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1060
+ [
1061
+ new InternalRuleDefCharNot([34]),
1062
+ new InternalRuleDefCharAlt(92),
1063
+ new InternalRuleDefCharAlt(127),
1064
+ new InternalRuleDefCharAlt(0),
1065
+ new InternalRuleDefCharRngUpper(31),
1066
+ new InternalRuleDefAlt(),
1067
+ new InternalRuleDefChar([92]),
1068
+ new InternalRuleDefReference(17),
1069
+ new InternalRuleDefEnd(),
1070
+ ],
1071
+ [
1072
+ new InternalRuleDefChar([34]),
1073
+ new InternalRuleDefCharAlt(92),
1074
+ new InternalRuleDefCharAlt(47),
1075
+ new InternalRuleDefCharAlt(98),
1076
+ new InternalRuleDefCharAlt(102),
1077
+ new InternalRuleDefCharAlt(110),
1078
+ new InternalRuleDefCharAlt(114),
1079
+ new InternalRuleDefCharAlt(116),
1080
+ new InternalRuleDefAlt(),
1081
+ new InternalRuleDefChar([117]),
1082
+ new InternalRuleDefChar([48]),
1083
+ new InternalRuleDefCharRngUpper(57),
1084
+ new InternalRuleDefCharAlt(97),
1085
+ new InternalRuleDefCharRngUpper(102),
1086
+ new InternalRuleDefCharAlt(65),
1087
+ new InternalRuleDefCharRngUpper(70),
1088
+ new InternalRuleDefChar([48]),
1089
+ new InternalRuleDefCharRngUpper(57),
1090
+ new InternalRuleDefCharAlt(97),
1091
+ new InternalRuleDefCharRngUpper(102),
1092
+ new InternalRuleDefCharAlt(65),
1093
+ new InternalRuleDefCharRngUpper(70),
1094
+ new InternalRuleDefChar([48]),
1095
+ new InternalRuleDefCharRngUpper(57),
1096
+ new InternalRuleDefCharAlt(97),
1097
+ new InternalRuleDefCharRngUpper(102),
1098
+ new InternalRuleDefCharAlt(65),
1099
+ new InternalRuleDefCharRngUpper(70),
1100
+ new InternalRuleDefChar([48]),
1101
+ new InternalRuleDefCharRngUpper(57),
1102
+ new InternalRuleDefCharAlt(97),
1103
+ new InternalRuleDefCharRngUpper(102),
1104
+ new InternalRuleDefCharAlt(65),
1105
+ new InternalRuleDefCharRngUpper(70),
1106
+ new InternalRuleDefEnd(),
1107
+ ],
1108
+ [
1109
+ new InternalRuleDefReference(16),
1110
+ new InternalRuleDefReference(18),
1111
+ new InternalRuleDefAlt(),
1112
+ new InternalRuleDefEnd(),
1113
+ ],
1114
+ [new InternalRuleDefReference(20), new InternalRuleDefReference(21), new InternalRuleDefEnd()],
1115
+ [new InternalRuleDefChar([45]), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1116
+ [
1117
+ new InternalRuleDefChar([48]),
1118
+ new InternalRuleDefCharRngUpper(57),
1119
+ new InternalRuleDefAlt(),
1120
+ new InternalRuleDefChar([49]),
1121
+ new InternalRuleDefCharRngUpper(57),
1122
+ new InternalRuleDefReference(22),
1123
+ new InternalRuleDefEnd(),
1124
+ ],
1125
+ [
1126
+ new InternalRuleDefChar([48]),
1127
+ new InternalRuleDefCharRngUpper(57),
1128
+ new InternalRuleDefReference(22),
1129
+ new InternalRuleDefAlt(),
1130
+ new InternalRuleDefEnd(),
1131
+ ],
1132
+ [new InternalRuleDefChar([46]), new InternalRuleDefReference(24), new InternalRuleDefEnd()],
1133
+ [
1134
+ new InternalRuleDefChar([48]),
1135
+ new InternalRuleDefCharRngUpper(57),
1136
+ new InternalRuleDefReference(24),
1137
+ new InternalRuleDefAlt(),
1138
+ new InternalRuleDefChar([48]),
1139
+ new InternalRuleDefCharRngUpper(57),
1140
+ new InternalRuleDefEnd(),
1141
+ ],
1142
+ [new InternalRuleDefReference(23), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1143
+ [
1144
+ new InternalRuleDefChar([101]),
1145
+ new InternalRuleDefCharAlt(69),
1146
+ new InternalRuleDefReference(27),
1147
+ new InternalRuleDefReference(28),
1148
+ new InternalRuleDefEnd(),
1149
+ ],
1150
+ [
1151
+ new InternalRuleDefChar([45]),
1152
+ new InternalRuleDefCharAlt(43),
1153
+ new InternalRuleDefAlt(),
1154
+ new InternalRuleDefEnd(),
1155
+ ],
1156
+ [
1157
+ new InternalRuleDefChar([48]),
1158
+ new InternalRuleDefCharRngUpper(57),
1159
+ new InternalRuleDefReference(28),
1160
+ new InternalRuleDefAlt(),
1161
+ new InternalRuleDefChar([48]),
1162
+ new InternalRuleDefCharRngUpper(57),
1163
+ new InternalRuleDefEnd(),
1164
+ ],
1165
+ [new InternalRuleDefReference(26), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1166
+ [
1167
+ new InternalRuleDefChar([32]),
1168
+ new InternalRuleDefCharAlt(9),
1169
+ new InternalRuleDefCharAlt(10),
1170
+ new InternalRuleDefReference(7),
1171
+ new InternalRuleDefEnd(),
1172
+ ],
1173
+ [new InternalRuleDefReference(30), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
1174
+ ],
1175
+ ],
1176
+ ],
1177
+ [
1178
+ 'japanese',
1179
+ `
1180
+ # A probably incorrect grammar for Japanese
1181
+ root ::= jp-char+ ([ \\t\\n] jp-char+)*
1182
+ jp-char ::= hiragana | katakana | punctuation | cjk
1183
+ hiragana ::= [ぁ-ゟ]
1184
+ katakana ::= [ァ-ヿ]
1185
+ punctuation ::= [、-〾]
1186
+ cjk ::= [一-鿿]
1187
+ `,
1188
+ [
1189
+ [
1190
+ ['root', 0],
1191
+ ['jp-char', 1],
1192
+ ['root_2', 2],
1193
+ ['root_3', 3],
1194
+ ['root_4', 4],
1195
+ ['root_5', 5],
1196
+ ['hiragana', 6],
1197
+ ['katakana', 7],
1198
+ ['punctuation', 8],
1199
+ ['cjk', 9],
1200
+ ],
1201
+ [
1202
+ [new InternalRuleDefReference(2), new InternalRuleDefReference(5), new InternalRuleDefEnd()],
1203
+ [
1204
+ new InternalRuleDefReference(6),
1205
+ new InternalRuleDefAlt(),
1206
+ new InternalRuleDefReference(7),
1207
+ new InternalRuleDefAlt(),
1208
+ new InternalRuleDefReference(8),
1209
+ new InternalRuleDefAlt(),
1210
+ new InternalRuleDefReference(9),
1211
+ new InternalRuleDefEnd(),
1212
+ ],
1213
+ [
1214
+ new InternalRuleDefReference(1),
1215
+ new InternalRuleDefReference(2),
1216
+ new InternalRuleDefAlt(),
1217
+ new InternalRuleDefReference(1),
1218
+ new InternalRuleDefEnd(),
1219
+ ],
1220
+ [
1221
+ new InternalRuleDefChar([32]),
1222
+ new InternalRuleDefCharAlt(9),
1223
+ new InternalRuleDefCharAlt(10),
1224
+ new InternalRuleDefReference(4),
1225
+ new InternalRuleDefEnd(),
1226
+ ],
1227
+ [
1228
+ new InternalRuleDefReference(1),
1229
+ new InternalRuleDefReference(4),
1230
+ new InternalRuleDefAlt(),
1231
+ new InternalRuleDefReference(1),
1232
+ new InternalRuleDefEnd(),
1233
+ ],
1234
+ [
1235
+ new InternalRuleDefReference(3),
1236
+ new InternalRuleDefReference(5),
1237
+ new InternalRuleDefAlt(),
1238
+ new InternalRuleDefEnd(),
1239
+ ],
1240
+ [new InternalRuleDefChar([12353]), new InternalRuleDefCharRngUpper(12447), new InternalRuleDefEnd()],
1241
+ [new InternalRuleDefChar([12449]), new InternalRuleDefCharRngUpper(12543), new InternalRuleDefEnd()],
1242
+ [new InternalRuleDefChar([12289]), new InternalRuleDefCharRngUpper(12350), new InternalRuleDefEnd()],
1243
+ [new InternalRuleDefChar([19968]), new InternalRuleDefCharRngUpper(40959), new InternalRuleDefEnd()],
1244
+ ],
1245
+ ],
1246
+ ],
1247
+ ];
1248
+
1249
+ test.each(testCases)('grammar parser: %s', (_key, grammar, expected) => {
1250
+ const [symbolIdsExpected, rulesExpected] = expected;
1251
+ const parsedGrammar = new RulesBuilder(grammar.replaceAll('\\n', '\n'));
1252
+ expect(parsedGrammar.rules).toEqual(rulesExpected);
1253
+ expect([...parsedGrammar.symbolIds.items()]).toEqual(symbolIdsExpected);
1254
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/rules-builder.ts ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getChars } from '../utils/code-points.ts';
2
+ import { GrammarParseError } from '../utils/errors/index.ts';
3
+ import { isSpace } from './is-space.ts';
4
+ import { isWordChar } from './is-word-char.ts';
5
+ import { parseChar } from './parse-char.ts';
6
+ import { parseName } from './parse-name.ts';
7
+ import { parseSpace } from './parse-space.ts';
8
+ import { SymbolIds } from './symbol-ids.ts';
9
+ import {
10
+ type InternalRuleDef,
11
+ InternalRuleDefAlt,
12
+ InternalRuleDefChar,
13
+ InternalRuleDefCharAlt,
14
+ InternalRuleDefCharNot,
15
+ InternalRuleDefCharRngUpper,
16
+ InternalRuleDefEnd,
17
+ InternalRuleDefReference,
18
+ } from './types.ts';
19
+
20
+ type InternalRuleDefClass = new (...args: never[]) => InternalRuleDef;
21
+
22
+ // declared locally so that type-checking depends on neither the DOM nor the node typings
23
+ declare const performance: { now(): number };
24
+
25
+ const perfCounter = (): number => performance.now() / 1000;
26
+
27
+ export const getOutElements = (typeOfRule: InternalRuleDefClass, startcharValue: number): InternalRuleDef => {
28
+ if (typeOfRule === InternalRuleDefChar) {
29
+ return new InternalRuleDefChar([startcharValue]);
30
+ }
31
+ if (typeOfRule === InternalRuleDefCharNot) {
32
+ return new InternalRuleDefCharNot([startcharValue]);
33
+ }
34
+ if (typeOfRule === InternalRuleDefCharRngUpper) {
35
+ return new InternalRuleDefCharRngUpper(startcharValue);
36
+ }
37
+ if (typeOfRule === InternalRuleDefAlt) {
38
+ return new InternalRuleDefAlt();
39
+ }
40
+ if (typeOfRule === InternalRuleDefEnd) {
41
+ return new InternalRuleDefEnd();
42
+ }
43
+ if (typeOfRule === InternalRuleDefCharAlt) {
44
+ return new InternalRuleDefCharAlt(startcharValue);
45
+ }
46
+ throw new Error(`Invalid type: ${typeof typeOfRule === 'function' ? typeOfRule.name : typeOfRule}`);
47
+ };
48
+
49
+ export class RulesBuilder {
50
+ pos: number;
51
+ symbolIds: SymbolIds;
52
+ rules: InternalRuleDef[][];
53
+ src: string;
54
+ start: number;
55
+ timeLimit: number;
56
+
57
+ // the time limit is measured in seconds
58
+ constructor(src: string, limit = 1000) {
59
+ this.pos = 0;
60
+ this.symbolIds = new SymbolIds();
61
+ this.rules = [];
62
+ this.src = src;
63
+ this.start = perfCounter();
64
+ this.timeLimit = limit;
65
+ this.parse(src);
66
+ }
67
+
68
+ parse(src: string): void {
69
+ const chars = getChars(src);
70
+ this.pos = parseSpace(src, 0, true);
71
+ while (this.pos < chars.length) {
72
+ this.parseRule(src);
73
+ }
74
+
75
+ // Validate the state to ensure that all rules are defined
76
+ for (const rule of this.rules) {
77
+ for (const elem of rule) {
78
+ if (elem instanceof InternalRuleDefReference) {
79
+ const ruleExists = elem.value < this.rules.length && this.rules[elem.value].length > 0;
80
+ if (!ruleExists) {
81
+ const missingRuleName = this.symbolIds.reverseGet(elem.value);
82
+ let missingRulePos = this.symbolIds.getPos(missingRuleName);
83
+
84
+ // Skip over the ::= and any whitespace
85
+ while (
86
+ missingRulePos < chars.length &&
87
+ (chars[missingRulePos] === ':' || chars[missingRulePos] === '=' || isSpace(chars[missingRulePos]))
88
+ ) {
89
+ missingRulePos += 1;
90
+ }
91
+
92
+ throw new GrammarParseError(src, missingRulePos, `Undefined rule identifier "${missingRuleName}"`);
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+ parseRule(src: string): void {
100
+ const chars = getChars(src);
101
+ const name = parseName(src, this.pos);
102
+ this.pos = parseSpace(src, this.pos + name.length, false);
103
+ const ruleId = this.getSymbolId(name, name.length);
104
+
105
+ this.pos = parseSpace(src, this.pos, true);
106
+ if (
107
+ !(
108
+ this.pos + 2 < chars.length && // Ensure the position + 2 is within bounds
109
+ chars[this.pos] === ':' &&
110
+ chars[this.pos + 1] === ':' &&
111
+ chars[this.pos + 2] === '='
112
+ )
113
+ ) {
114
+ throw new GrammarParseError(src, this.pos, `Expecting ::= at ${this.pos}`);
115
+ }
116
+ this.pos += 3;
117
+ this.pos = parseSpace(src, this.pos, true);
118
+
119
+ this.parseAlternates(name, ruleId);
120
+
121
+ // Check if this.pos is within the bounds of src before checking for a carriage return
122
+ if (this.pos < chars.length && chars[this.pos] === '\r') {
123
+ // the reference reads the character after the carriage return without checking that there is
124
+ // one, and so fails on a grammar that ends in a carriage return
125
+ if (this.pos + 1 >= chars.length) {
126
+ throw new RangeError('string index out of range');
127
+ }
128
+ this.pos += chars[this.pos + 1] === '\n' ? 2 : 1;
129
+ } else if (this.pos < chars.length && chars[this.pos] === '\n') {
130
+ this.pos += 1;
131
+ } else if (this.pos < chars.length && chars[this.pos]) {
132
+ throw new GrammarParseError(src, this.pos, `Expecting newline or end at ${this.pos}`);
133
+ }
134
+ this.pos = parseSpace(src, this.pos, true);
135
+ }
136
+
137
+ getSymbolId(src: string, length: number): number {
138
+ const nextId = this.symbolIds.size;
139
+ const key = Array.from(src).slice(0, length).join('');
140
+ if (!this.symbolIds.has(key)) {
141
+ this.symbolIds.set(key, nextId, this.pos);
142
+ }
143
+ return this.symbolIds.get(key) as number;
144
+ }
145
+
146
+ generateSymbolId(baseName: string): number {
147
+ const nextId = this.symbolIds.size;
148
+ this.symbolIds.set(`${baseName}_${nextId}`, nextId, this.pos);
149
+ return nextId;
150
+ }
151
+
152
+ addRule(ruleId: number, rule: InternalRuleDef[]): void {
153
+ while (this.rules.length <= ruleId) {
154
+ this.rules.push([]);
155
+ }
156
+ this.rules[ruleId] = rule;
157
+ }
158
+
159
+ checkDuration(): void {
160
+ if (perfCounter() - this.start > this.timeLimit) {
161
+ throw new GrammarParseError(this.src, this.pos, `Duration of ${this.timeLimit} exceeded`);
162
+ }
163
+ }
164
+
165
+ parseSequence(ruleName: string, outElements: InternalRuleDef[], depth = 0): void {
166
+ const isNested = depth !== 0;
167
+ const src = this.src;
168
+ const chars = getChars(src);
169
+ let lastSymStart = outElements.length;
170
+
171
+ while (this.pos < chars.length) {
172
+ if (chars[this.pos] === '"') {
173
+ this.pos += 1;
174
+ lastSymStart = outElements.length;
175
+ while (chars[this.pos] !== '"') {
176
+ this.checkDuration();
177
+ const [value, incPos] = parseChar(src, this.pos);
178
+ outElements.push(new InternalRuleDefChar([value]));
179
+ this.pos += incPos;
180
+ }
181
+ this.pos = parseSpace(src, this.pos + 1, isNested);
182
+ } else if (chars[this.pos] === '[') {
183
+ this.pos += 1;
184
+ let startType: InternalRuleDefClass = InternalRuleDefChar;
185
+ if (chars[this.pos] === '^') {
186
+ this.pos += 1;
187
+ startType = InternalRuleDefCharNot;
188
+ }
189
+ lastSymStart = outElements.length;
190
+ while (chars[this.pos] !== ']') {
191
+ this.checkDuration();
192
+ const type: InternalRuleDefClass = lastSymStart < outElements.length ? InternalRuleDefCharAlt : startType;
193
+ const [startcharValue, incPos] = parseChar(src, this.pos);
194
+ this.pos += incPos;
195
+ outElements.push(getOutElements(type, startcharValue));
196
+
197
+ if (chars[this.pos] === '-' && chars[this.pos + 1] !== ']') {
198
+ this.pos += 1;
199
+ const [endcharValue, endcharIncPos] = parseChar(src, this.pos);
200
+ outElements.push(new InternalRuleDefCharRngUpper(endcharValue));
201
+ this.pos += endcharIncPos;
202
+ }
203
+ }
204
+ this.pos = parseSpace(src, this.pos + 1, isNested);
205
+ } else if (isWordChar(chars[this.pos])) {
206
+ const name = parseName(src, this.pos);
207
+ const refRuleId = this.getSymbolId(name, name.length);
208
+ this.pos += name.length;
209
+ this.pos = parseSpace(src, this.pos, isNested);
210
+
211
+ lastSymStart = outElements.length;
212
+ outElements.push(new InternalRuleDefReference(refRuleId));
213
+ } else if (chars[this.pos] === '(') {
214
+ this.pos = parseSpace(src, this.pos + 1, true);
215
+ const subRuleId = this.generateSymbolId(ruleName);
216
+ this.parseAlternates(ruleName, subRuleId, depth + 1);
217
+ lastSymStart = outElements.length;
218
+ outElements.push(new InternalRuleDefReference(subRuleId));
219
+ if (chars[this.pos] !== ')') {
220
+ throw new GrammarParseError(src, this.pos, `Expecting ')' at ${this.pos}`);
221
+ }
222
+ this.pos = parseSpace(src, this.pos + 1, isNested);
223
+ } else if ('*+?'.includes(chars[this.pos])) {
224
+ if (lastSymStart === outElements.length) {
225
+ throw new GrammarParseError(src, this.pos, `Expecting preceding item to */+/? at ${this.pos}`);
226
+ }
227
+ const subRuleId = this.generateSymbolId(ruleName);
228
+ const subRule = outElements.slice(lastSymStart);
229
+ if ('*+'.includes(chars[this.pos])) {
230
+ subRule.push(new InternalRuleDefReference(subRuleId));
231
+ }
232
+ subRule.push(new InternalRuleDefAlt());
233
+ if (chars[this.pos] === '+') {
234
+ for (const elem of outElements.slice(lastSymStart)) {
235
+ subRule.push(elem);
236
+ }
237
+ }
238
+ subRule.push(new InternalRuleDefEnd());
239
+ this.addRule(subRuleId, subRule);
240
+ outElements.splice(lastSymStart, outElements.length - lastSymStart, new InternalRuleDefReference(subRuleId));
241
+ this.pos = parseSpace(src, this.pos + 1, isNested);
242
+ } else {
243
+ break;
244
+ }
245
+ }
246
+ }
247
+
248
+ parseAlternates(ruleName: string, ruleId: number, depth = 0): void {
249
+ const src = this.src;
250
+ const chars = getChars(src);
251
+ const rule: InternalRuleDef[] = [];
252
+ this.parseSequence(ruleName, rule, depth);
253
+ // Ensure that this.pos is within bounds before checking chars[this.pos]
254
+ while (this.pos < chars.length && chars[this.pos] === '|') {
255
+ this.checkDuration();
256
+ rule.push(new InternalRuleDefAlt());
257
+ this.pos = parseSpace(src, this.pos + 1, true);
258
+ this.parseSequence(ruleName, rule, depth);
259
+ }
260
+ rule.push(new InternalRuleDefEnd());
261
+ this.addRule(ruleId, rule);
262
+ }
263
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/symbol-ids.ts ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export class SymbolIds implements Iterable<[string, number]> {
2
+ #map = new Map<string, number>();
3
+ #pos = new Map<string, number>();
4
+ #reverseMap = new Map<number, string>();
5
+
6
+ items(): IterableIterator<[string, number]> {
7
+ return this.#map.entries();
8
+ }
9
+
10
+ [Symbol.iterator](): IterableIterator<[string, number]> {
11
+ return this.#map.entries();
12
+ }
13
+
14
+ get size(): number {
15
+ return this.#map.size;
16
+ }
17
+
18
+ get(key: string): number | undefined {
19
+ return this.#map.get(key);
20
+ }
21
+
22
+ set(key: string, value: number, pos: number): void {
23
+ this.#map.set(key, value);
24
+ this.#reverseMap.set(value, key);
25
+ this.#pos.set(key, pos);
26
+ }
27
+
28
+ has(key: string): boolean {
29
+ return this.#map.has(key);
30
+ }
31
+
32
+ reverseGet(key: number): string {
33
+ const val = this.#reverseMap.get(key);
34
+ if (val === undefined) {
35
+ throw new Error(`SymbolIds does not contain value: ${key}`);
36
+ }
37
+ return val;
38
+ }
39
+
40
+ getPos(key: string): number {
41
+ const val = this.#pos.get(key);
42
+ if (val === undefined) {
43
+ throw new Error(`SymbolIds does not contain key: ${key}`);
44
+ }
45
+ return val;
46
+ }
47
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/rules-builder/types.ts ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const InternalRuleType = {
2
+ CHAR: 'CHAR',
3
+ CHAR_RNG_UPPER: 'CHAR_RNG_UPPER',
4
+ RULE_REF: 'RULE_REF',
5
+ ALT: 'ALT',
6
+ END: 'END',
7
+ CHAR_NOT: 'CHAR_NOT',
8
+ CHAR_ALT: 'CHAR_ALT',
9
+ } as const;
10
+ export type InternalRuleType = (typeof InternalRuleType)[keyof typeof InternalRuleType];
11
+
12
+ export class InternalRuleDefWithNumericValue {
13
+ value: number;
14
+
15
+ constructor(value: number) {
16
+ this.value = value;
17
+ }
18
+ }
19
+
20
+ export class InternalBase {}
21
+
22
+ export class InternalBaseWithValue<T = unknown> {
23
+ value: T;
24
+
25
+ constructor(value: T) {
26
+ this.value = value;
27
+ }
28
+ }
29
+
30
+ export class InternalBaseWithInt extends InternalBaseWithValue<number> {}
31
+
32
+ export class InternalBaseWithListOfInts extends InternalBaseWithValue<number[]> {
33
+ constructor(value: number[] = []) {
34
+ super(value.slice());
35
+ }
36
+ }
37
+
38
+ // Each rule definition carries its type as a plain property, so that definitions of different
39
+ // classes are never structurally equal (e.g. with `toEqual`), even when their values match.
40
+ export class InternalRuleDefChar extends InternalBaseWithListOfInts {
41
+ readonly type = InternalRuleType.CHAR;
42
+ }
43
+
44
+ export class InternalRuleDefCharAlt extends InternalBaseWithInt {
45
+ readonly type = InternalRuleType.CHAR_ALT;
46
+ }
47
+
48
+ export class InternalRuleDefAlt extends InternalBase {
49
+ readonly type = InternalRuleType.ALT;
50
+ }
51
+
52
+ export class InternalRuleDefCharNot extends InternalBaseWithListOfInts {
53
+ readonly type = InternalRuleType.CHAR_NOT;
54
+ }
55
+
56
+ export class InternalRuleDefCharRngUpper extends InternalBaseWithInt {
57
+ readonly type = InternalRuleType.CHAR_RNG_UPPER;
58
+ }
59
+
60
+ export class InternalRuleDefReference extends InternalBaseWithInt {
61
+ readonly type = InternalRuleType.RULE_REF;
62
+ }
63
+
64
+ export class InternalRuleDefEnd extends InternalBase {
65
+ readonly type = InternalRuleType.END;
66
+ }
67
+
68
+ export class InternalRuleDefWithoutValue extends InternalBase {}
69
+
70
+ export type InternalRuleDef =
71
+ | InternalRuleDefChar
72
+ | InternalRuleDefEnd
73
+ | InternalRuleDefReference
74
+ | InternalRuleDefCharNot
75
+ | InternalRuleDefWithNumericValue
76
+ | InternalRuleDefWithoutValue
77
+ | InternalRuleDefAlt
78
+ | InternalRuleDefCharRngUpper
79
+ | InternalRuleDefCharAlt;
80
+
81
+ export type InternalRuleDefCharOrAltChar = InternalRuleDefChar | InternalRuleDefCharAlt;
82
+
83
+ export const isRuleDefAlt = (rule?: unknown): rule is InternalRuleDefAlt => rule instanceof InternalRuleDefAlt;
84
+
85
+ export const isRuleDefRef = (rule?: unknown): rule is InternalRuleDefReference =>
86
+ rule instanceof InternalRuleDefReference;
87
+
88
+ export const isRuleDefEnd = (rule?: unknown): rule is InternalRuleDefEnd => rule instanceof InternalRuleDefEnd;
89
+
90
+ export const isRuleDefChar = (rule?: unknown): rule is InternalRuleDefChar => rule instanceof InternalRuleDefChar;
91
+
92
+ export const isRuleDefCharNot = (rule?: unknown): rule is InternalRuleDefCharNot =>
93
+ rule instanceof InternalRuleDefCharNot;
94
+
95
+ export const isRuleDefCharAlt = (rule?: unknown): rule is InternalRuleDefCharAlt =>
96
+ rule instanceof InternalRuleDefCharAlt;
97
+
98
+ export const isRuleDefCharRngUpper = (rule?: unknown): rule is InternalRuleDefCharRngUpper =>
99
+ rule instanceof InternalRuleDefCharRngUpper;
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/code-points.ts ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Python strings are sequences of code points, whereas JavaScript strings are sequences of
2
+ // UTF-16 code units. Positions and lengths in this library are measured in code points, so
3
+ // strings are split with the helpers below before they are indexed.
4
+
5
+ let cachedSrc: string | undefined;
6
+ let cachedChars: readonly string[] = [];
7
+
8
+ // The most recently split string is cached, as the grammar parser repeatedly indexes into the same source.
9
+ export const getChars = (src: string): readonly string[] => {
10
+ if (src !== cachedSrc) {
11
+ cachedChars = Array.from(src);
12
+ cachedSrc = src;
13
+ }
14
+ return cachedChars;
15
+ };
16
+
17
+ export const getLength = (src: string): number => {
18
+ let length = 0;
19
+ for (const _ of src) {
20
+ length += 1;
21
+ }
22
+ return length;
23
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/build-error-position.test.ts ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it, test } from 'vitest';
2
+ import { buildErrorPosition } from './build-error-position.ts';
3
+
4
+ describe('buildErrorPosition', () => {
5
+ it.each<[string, number, [string, string]]>([
6
+ ['root ::= "foo"', 1, ['root ::= "foo"', ' ^']],
7
+ ['root ::= "foo"', 5, ['root ::= "foo"', ' ^']],
8
+ // multi line grammars with pos on first line
9
+ ['aa\\nbb', 1, ['aa', ' ^']],
10
+ // multi line grammars with pos on second line, first character
11
+ ['aa\\nbb', 2, ['aa\\nbb', '^']],
12
+ // multi line grammars with pos on second line, second character
13
+ ['aa\\nbb', 2 + 1, ['aa\\nbb', ' ^']],
14
+ // multi line grammars beyond error with pos on second line
15
+ ['aa\\nbb\\ncc', 2 + 1, ['aa\\nbb', ' ^']],
16
+ // multi line grammars beyond error with pos on third line, first char
17
+ ['aa\\nbb\\ncc', 2 + 2 + 0, ['aa\\nbb\\ncc', '^']],
18
+ // multi line grammars beyond error with pos on third line, second char
19
+ ['aa\\nbb\\ncc', 2 + 2 + 1, ['aa\\nbb\\ncc', ' ^']],
20
+ // multi line grammars beyond error with pos on fourth line, first char
21
+ ['aa\\nbb\\ncc\\ndd', 2 + 2 + 2 + 0, ['bb\\ncc\\ndd', '^']],
22
+ // multi line grammars beyond error with pos on fifth line, second char
23
+ ['aa\\nbb\\ncc\\ndd\\nee', 2 + 2 + 2 + 2 + 1, ['cc\\ndd\\nee', ' ^']],
24
+ ])('correctly shows position for single line grammars (%j, %i)', (grammar, pos, expected) => {
25
+ const [grammarOut, posOut] = expected;
26
+ const result = buildErrorPosition(grammar.split('\\n').join('\n'), pos);
27
+ expect(result).toEqual([...grammarOut.split('\\n'), posOut]);
28
+ });
29
+
30
+ test('it renders a message for empty input', () => {
31
+ expect(buildErrorPosition('', 0)).toEqual(['No input provided']);
32
+ });
33
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/build-error-position.ts ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getLength } from '../code-points.ts';
2
+
3
+ export const MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW = 3;
4
+
5
+ export const buildErrorPosition = (src: string, pos: number): string[] => {
6
+ if (src === '') {
7
+ return ['No input provided'];
8
+ }
9
+ const lines = src.split('\n');
10
+ const srcLength = getLength(src);
11
+
12
+ const getLine = (idx: number): string => {
13
+ if (idx >= lines.length) {
14
+ throw new RangeError('list index out of range');
15
+ }
16
+ return lines[idx];
17
+ };
18
+
19
+ let lineIdx = 0;
20
+ while (getLine(lineIdx) && pos > getLength(lines[lineIdx]) - 1 && pos < srcLength) {
21
+ pos -= getLength(lines[lineIdx]);
22
+ lineIdx += 1;
23
+ }
24
+
25
+ const linesToShow: string[] = [];
26
+ for (let i = Math.max(0, lineIdx - (MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW - 1)); i <= lineIdx; i++) {
27
+ linesToShow.push(lines[i]);
28
+ }
29
+
30
+ return [...linesToShow, ' '.repeat(Math.max(0, pos)) + '^'];
31
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/get-input-as-string.test.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from 'vitest';
2
+ import { getInputAsString } from './get-input-as-string.ts';
3
+ import type { ValidInput } from './types.ts';
4
+
5
+ describe('getInputAsString', () => {
6
+ it.each<[ValidInput, string]>([
7
+ ['hello', 'hello'],
8
+ [[104, 101, 108, 108, 111], 'hello'],
9
+ [104, 'h'],
10
+ [0x1f600, '\u{1F600}'],
11
+ [[0x1f600, 0x1f601], '\u{1F600}\u{1F601}'],
12
+ ])('correctly converts strings to strings (%j)', (input, expected) => {
13
+ expect(getInputAsString(input)).toEqual(expected);
14
+ });
15
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/get-input-as-string.ts ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ValidInput } from './types.ts';
2
+
3
+ export const getInputAsString = (src: ValidInput): string => {
4
+ if (typeof src === 'string') {
5
+ return src;
6
+ }
7
+ if (typeof src === 'number') {
8
+ return String.fromCodePoint(src);
9
+ }
10
+ return src.map((cp) => String.fromCodePoint(cp)).join('');
11
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/grammar-parse-error.test.ts ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GRAMMAR_PARSER_ERROR_HEADER_MESSAGE, GrammarParseError } from './grammar-parse-error.ts';
3
+
4
+ describe('GrammarParseError', () => {
5
+ test('it renders a message', () => {
6
+ const grammar = 'aa\\nbb\\ncc\\ndd\\nee';
7
+ const pos = 5;
8
+ const reason = 'reason';
9
+ const err = new GrammarParseError(grammar.split('\\n').join('\n'), pos, reason);
10
+ expect(err.message).toEqual([GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason), '', 'aa', 'bb', 'cc', ' ^'].join('\n'));
11
+ });
12
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/grammar-parse-error.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { buildErrorPosition } from './build-error-position.ts';
2
+
3
+ export const GRAMMAR_PARSER_ERROR_HEADER_MESSAGE = (reason: string): string => `Failed to parse grammar: ${reason}`;
4
+
5
+ export class GrammarParseError extends Error {
6
+ grammar: string;
7
+ pos: number;
8
+ reason: string;
9
+
10
+ constructor(grammar: string, pos: number, reason: string) {
11
+ super([GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason), '', ...buildErrorPosition(grammar, pos)].join('\n'));
12
+ this.name = 'GrammarParseError';
13
+ this.grammar = grammar;
14
+ this.pos = pos;
15
+ this.reason = reason;
16
+ }
17
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/index.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export { GrammarParseError } from './grammar-parse-error.ts';
2
+ export { InputParseError } from './input-parse-error.ts';
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/input-parse-error.test.ts ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { INPUT_PARSER_ERROR_HEADER_MESSAGE, InputParseError } from './input-parse-error.ts';
3
+
4
+ describe('InputParseError', () => {
5
+ test('it renders a message', () => {
6
+ const input = 'some input';
7
+ const pos = 1;
8
+ const err = new InputParseError(input, pos);
9
+ expect(err.message).toEqual([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', input, ' ^'].join('\n'));
10
+ });
11
+
12
+ test('it renders a message for code point', () => {
13
+ const pos = 0;
14
+ const err = new InputParseError('a', pos);
15
+ expect(err.message).toEqual([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', 'a', '^'].join('\n'));
16
+ });
17
+
18
+ test('it renders a message for code points', () => {
19
+ const pos = 2;
20
+ const err = new InputParseError('abcd', pos);
21
+ expect(err.message).toEqual([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', 'abcd', ' ^'].join('\n'));
22
+ });
23
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/input-parse-error.ts ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getLength } from '../code-points.ts';
2
+ import { buildErrorPosition } from './build-error-position.ts';
3
+ import { getInputAsString } from './get-input-as-string.ts';
4
+ import type { ValidInput } from './types.ts';
5
+
6
+ export const INPUT_PARSER_ERROR_HEADER_MESSAGE = 'Failed to parse input string:';
7
+
8
+ export class InputParseError extends Error {
9
+ mostRecentInput: ValidInput;
10
+ pos: number;
11
+ previousInput: ValidInput;
12
+
13
+ constructor(mostRecentInput: ValidInput, pos: number, previousInput: ValidInput = '') {
14
+ super(
15
+ [
16
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
17
+ '',
18
+ ...buildErrorPosition(
19
+ `${getInputAsString(previousInput)}${getInputAsString(mostRecentInput)}`,
20
+ pos + getLength(getInputAsString(previousInput)),
21
+ ),
22
+ ].join('\n'),
23
+ );
24
+ this.name = 'InputParseError';
25
+ this.mostRecentInput = mostRecentInput;
26
+ this.pos = pos;
27
+ this.previousInput = previousInput;
28
+ }
29
+
30
+ get src(): string {
31
+ return `${getInputAsString(this.previousInput)}${getInputAsString(this.mostRecentInput)}`;
32
+ }
33
+
34
+ get errorForMostRecentInput(): string {
35
+ return [
36
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
37
+ '',
38
+ ...buildErrorPosition(getInputAsString(this.mostRecentInput), this.pos),
39
+ ].join('\n');
40
+ }
41
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/errors/types.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export type ValidInput = string | number | number[];
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/is-point-in-range.test.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Range } from '../grammar-graph/types.ts';
3
+ import { isPointInRange } from './is-point-in-range.ts';
4
+
5
+ describe('isPointInRange', () => {
6
+ it.each<[number, Range, boolean]>([
7
+ [96, [97, 122], false],
8
+ [97, [97, 122], true],
9
+ [98, [97, 122], true],
10
+ [122, [97, 122], true],
11
+ [123, [97, 122], false],
12
+ ])('checks if point %i is in range %j: %s', (point, range, expectation) => {
13
+ expect(isPointInRange(point, range)).toEqual(expectation);
14
+ });
15
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/is-point-in-range.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import type { Range } from '../grammar-graph/types.ts';
2
+
3
+ export const isPointInRange = (point: number, givenRange: Range): boolean => {
4
+ if (!Number.isInteger(point)) {
5
+ throw new Error('point must be an integer');
6
+ }
7
+ return point >= givenRange[0] && point <= givenRange[1];
8
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/src/utils/validate-non-empty.ts ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ export const validateNonEmpty = (value: number[]): number[] => {
2
+ if (!value || value.length === 0) {
3
+ throw new Error('Value cannot be empty.');
4
+ }
5
+ return value;
6
+ };
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration-with-additional-strings.test.ts ADDED
@@ -0,0 +1,662 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import {
3
+ GBNF,
4
+ InputParseError,
5
+ type Range,
6
+ type ResolvedRule,
7
+ RuleChar,
8
+ RuleCharExclude,
9
+ RuleEnd,
10
+ } from '../../../src/index.ts';
11
+
12
+ const ord = (char: string): number => char.codePointAt(0) as number;
13
+
14
+ interface ExpectedRule {
15
+ type: string;
16
+ value?: (number | Range)[];
17
+ }
18
+
19
+ const byJson = (a: ResolvedRule, b: ResolvedRule): number => {
20
+ const [keyA, keyB] = [JSON.stringify(a), JSON.stringify(b)];
21
+ if (keyA === keyB) {
22
+ return 0;
23
+ }
24
+ return keyA < keyB ? -1 : 1;
25
+ };
26
+
27
+ // runs the callback, and returns the error it is expected to throw
28
+ const raises = <T extends Error>(expected: new (...args: never[]) => T, callback: () => unknown): T => {
29
+ let error: unknown;
30
+ try {
31
+ callback();
32
+ } catch (err) {
33
+ error = err;
34
+ }
35
+ expect(error).toBeInstanceOf(expected);
36
+ return error as T;
37
+ };
38
+
39
+ describe('iteration', () => {
40
+ test.each<[string, string, string]>([
41
+ // single char rule
42
+ [
43
+ // single char rule
44
+ 'root ::= "foo"',
45
+ // single char rule
46
+ 'f',
47
+ // single char rule
48
+ '1',
49
+ ],
50
+ ['root ::= "foo"', 'f', 'b'],
51
+ ['root ::= "foo"', 'f', 'o1'],
52
+ ['root ::= "foo"', 'f', 'ooo'],
53
+ ['root ::= "foo"', 'f', 'ooooooo'],
54
+ // two char rules
55
+ [
56
+ // two char rules
57
+ 'root ::= "foo" | "bar"',
58
+ // two char rules
59
+ 'f',
60
+ // two char rules
61
+ '1',
62
+ ],
63
+ ['root ::= "foo" | "bar"', 'b', '1'],
64
+ ['root ::= "foo" | "bar"', 'f', 'o1'],
65
+ ['root ::= "foo" | "bar"', 'b', 'a1'],
66
+ ['root ::= "foo" | "bar"', 'f', 'ooo'],
67
+ ['root ::= "foo" | "bar"', 'b', 'arrr'],
68
+ // three char rules
69
+ [
70
+ // three char rules
71
+ 'root ::= "foo" | "bar" | "baz"',
72
+ // three char rules
73
+ 'f',
74
+ // three char rules
75
+ '1',
76
+ ],
77
+ ['root ::= "foo" | "bar" | "baz"', 'f', 'z'],
78
+ ['root ::= "foo" | "bar" | "baz"', 'b', 'b1'],
79
+ ['root ::= "foo" | "bar" | "baz"', 'f', 'o1'],
80
+ ['root ::= "foo" | "bar" | "baz"', 'b', 'al'],
81
+ ['root ::= "foo" | "bar" | "baz"', 'f', 'ooo'],
82
+ ['root ::= "foo" | "bar" | "baz"', 'b', 'azrr'],
83
+ // expressions
84
+ [
85
+ `root ::= foo
86
+ foo ::="foo"`,
87
+ 'f',
88
+ '1',
89
+ ],
90
+ [
91
+ `root ::= foo
92
+ foo ::="foo"`,
93
+ 'f',
94
+ 'b',
95
+ ],
96
+ [
97
+ `root ::= foo
98
+ foo ::="foo"`,
99
+ 'f',
100
+ 'o1',
101
+ ],
102
+ [
103
+ `root ::= foo
104
+ foo ::="foo"`,
105
+ 'f',
106
+ 'ooo',
107
+ ],
108
+ // expression and a char rule
109
+ [
110
+ `root::=foo|"bar"
111
+ foo::="foo"`,
112
+ 'f',
113
+ '1',
114
+ ],
115
+ [
116
+ `root::=foo|"bar"
117
+ foo::="foo"`,
118
+ 'f',
119
+ 'z',
120
+ ],
121
+ [
122
+ `root::=foo|"bar"
123
+ foo::="foo"`,
124
+ 'b',
125
+ 'b1',
126
+ ],
127
+ [
128
+ `root::=foo|"bar"
129
+ foo::="foo"`,
130
+ 'f',
131
+ 'o1',
132
+ ],
133
+ [
134
+ `root::=foo|"bar"
135
+ foo::="foo"`,
136
+ 'f',
137
+ 'ooo',
138
+ ],
139
+ [
140
+ `root::=foo|"bar"
141
+ foo::="foo"`,
142
+ 'b',
143
+ 'arr',
144
+ ],
145
+ // two expressions
146
+ [
147
+ `root::= foo | bar
148
+ foo::="foo"
149
+ bar::="bar"`,
150
+ 'f',
151
+ '1',
152
+ ],
153
+ [
154
+ `root::= foo | bar
155
+ foo::="foo"
156
+ bar::="bar"`,
157
+ 'f',
158
+ 'z',
159
+ ],
160
+ [
161
+ `root::= foo | bar
162
+ foo::="foo"
163
+ bar::="bar"`,
164
+ 'b',
165
+ '1',
166
+ ],
167
+ [
168
+ `root::= foo | bar
169
+ foo::="foo"
170
+ bar::="bar"`,
171
+ 'f',
172
+ 'o1',
173
+ ],
174
+ [
175
+ `root::= foo | bar
176
+ foo::="foo"
177
+ bar::="bar"`,
178
+ 'f',
179
+ 'ooo',
180
+ ],
181
+ [
182
+ `root::= foo | bar
183
+ foo::="foo"
184
+ bar::="bar"`,
185
+ 'b',
186
+ 'arr',
187
+ ],
188
+ // nested expressions
189
+ [
190
+ `root ::= f | b
191
+ f ::= fo
192
+ b ::= ba
193
+ fo ::= foo
194
+ ba ::= bar | baz
195
+ foo ::= "foo"
196
+ bar ::= "bar"
197
+ baz ::= "baz"`,
198
+ 'f',
199
+ '1',
200
+ ],
201
+ [
202
+ `root ::= f | b
203
+ f ::= fo
204
+ b ::= ba
205
+ fo ::= foo
206
+ ba ::= bar | baz
207
+ foo ::= "foo"
208
+ bar ::= "bar"
209
+ baz ::= "baz"`,
210
+ 'f',
211
+ 'z',
212
+ ],
213
+ [
214
+ `root ::= f | b
215
+ f ::= fo
216
+ b ::= ba
217
+ fo ::= foo
218
+ ba ::= bar | baz
219
+ foo ::= "foo"
220
+ bar ::= "bar"
221
+ baz ::= "baz"`,
222
+ 'b',
223
+ '1',
224
+ ],
225
+ [
226
+ `root ::= f | b
227
+ f ::= fo
228
+ b ::= ba
229
+ fo ::= foo
230
+ ba ::= bar | baz
231
+ foo ::= "foo"
232
+ bar ::= "bar"
233
+ baz ::= "baz"`,
234
+ 'f',
235
+ 'o1',
236
+ ],
237
+ [
238
+ `root ::= f | b
239
+ f ::= fo
240
+ b ::= ba
241
+ fo ::= foo
242
+ ba ::= bar | baz
243
+ foo ::= "foo"
244
+ bar ::= "bar"
245
+ baz ::= "baz"`,
246
+ 'f',
247
+ 'ooo',
248
+ ],
249
+ [
250
+ `root ::= f | b
251
+ f ::= fo
252
+ b ::= ba
253
+ fo ::= foo
254
+ ba ::= bar | baz
255
+ foo ::= "foo"
256
+ bar ::= "bar"
257
+ baz ::= "baz"`,
258
+ 'b',
259
+ 'arr',
260
+ ],
261
+ [
262
+ `root ::= f | b
263
+ f ::= fo
264
+ b ::= ba
265
+ fo ::= foo
266
+ ba ::= bar | baz
267
+ foo ::= "foo"
268
+ bar ::= "bar"
269
+ baz ::= "baz"`,
270
+ 'b',
271
+ 'azr',
272
+ ],
273
+ // ranges
274
+ [
275
+ // ranges
276
+ 'root ::= [a-z]',
277
+ // ranges
278
+ 'a',
279
+ // ranges
280
+ 'A',
281
+ ],
282
+ ['root ::= [a-z]', 'a', '0'],
283
+ ['root ::= [a-z]', 'a', 'az'],
284
+ // range with ? modifier
285
+ [
286
+ // range with ? modifier
287
+ 'root ::= [a-z]?',
288
+ // range with ? modifier
289
+ 'a',
290
+ // range with ? modifier
291
+ 'A',
292
+ ],
293
+ ['root ::= [a-z]?', 'a', '0'],
294
+ ['root ::= [a-z]?', 'a', 'az'],
295
+ // range with + modifier
296
+ [
297
+ // range with + modifier
298
+ 'root ::= [a-z]+',
299
+ // range with + modifier
300
+ 'a',
301
+ // range with + modifier
302
+ 'A',
303
+ ],
304
+ ['root ::= [a-z]+', 'a', '0'],
305
+ ['root ::= [a-z]+', 'a', 'z0'],
306
+ // range with * modifier
307
+ [
308
+ // range with * modifier
309
+ 'root ::= [a-z]*',
310
+ // range with * modifier
311
+ 'a',
312
+ // range with * modifier
313
+ 'A',
314
+ ],
315
+ ['root ::= [a-z]*', 'a', '0'],
316
+ ['root ::= [a-z]*', 'a', 'z0'],
317
+ ])(
318
+ 'it raises if encountering grammar %j with starting %j and additional %j [%#]',
319
+ (grammar, starting, additional) => {
320
+ const state = GBNF(grammar, starting);
321
+ expect(() => state.add(additional)).toThrow(InputParseError);
322
+ },
323
+ );
324
+
325
+ test.each<[string, string, string, ExpectedRule[]]>([
326
+ ['root ::= "foo"', '', 'f', [{ type: 'char', value: [ord('o')] }]],
327
+ ['root ::= "foo"', 'f', 'o', [{ type: 'char', value: [ord('o')] }]],
328
+ ['root ::= "foo"', 'fo', 'o', [{ type: 'end' }]],
329
+ // two char rules
330
+ ['root ::= "foo" | "bar" ', '', 'f', [{ type: 'char', value: [ord('o')] }]],
331
+ ['root ::= "foo" | "bar" ', 'f', 'o', [{ type: 'char', value: [ord('o')] }]],
332
+ ['root ::= "foo" | "bar" ', 'fo', 'o', [{ type: 'end' }]],
333
+ ['root ::= "foo" | "bar" ', '', 'b', [{ type: 'char', value: [ord('a')] }]],
334
+ ['root ::= "foo" | "bar" ', 'b', 'a', [{ type: 'char', value: [ord('r')] }]],
335
+ ['root ::= "foo" | "bar" ', 'ba', 'r', [{ type: 'end' }]],
336
+ // three char rules
337
+ [
338
+ 'root ::= "foo" | "bar" | "baz"',
339
+ 'b',
340
+ 'a',
341
+ [{ type: 'char', value: [ord('r')] }, { type: 'char', value: [ord('z')] }],
342
+ ],
343
+ // char not
344
+ ['root ::= [^f] "o"', 'g', 'o', [{ type: 'end' }]],
345
+ ['root ::= [^A-Z]', '', 'a', [{ type: 'end' }]],
346
+ ['root ::= [^A-Z0-9]', '', 'a', [{ type: 'end' }]],
347
+ ['root ::= [^A-Z0-9_-]', '', 'a', [{ type: 'end' }]],
348
+ // expressions
349
+ [
350
+ `root ::= foo
351
+ foo ::= "foo"`,
352
+ '',
353
+ 'f',
354
+ [{ type: 'char', value: [ord('o')] }],
355
+ ],
356
+ [
357
+ `root ::= foo
358
+ foo ::= "foo"`,
359
+ 'f',
360
+ 'o',
361
+ [{ type: 'char', value: [ord('o')] }],
362
+ ],
363
+ [
364
+ `root ::= foo
365
+ foo ::= "foo"`,
366
+ 'fo',
367
+ 'o',
368
+ [{ type: 'end' }],
369
+ ],
370
+ // expression and a char rule
371
+ [
372
+ `root ::= foo | "bar"
373
+ foo ::= "foo"`,
374
+ '',
375
+ 'f',
376
+ [{ type: 'char', value: [ord('o')] }],
377
+ ],
378
+ [
379
+ `root ::= foo | "bar"
380
+ foo ::= "foo"`,
381
+ 'f',
382
+ 'o',
383
+ [{ type: 'char', value: [ord('o')] }],
384
+ ],
385
+ [
386
+ `root ::= foo | "bar"
387
+ foo ::= "foo"`,
388
+ 'fo',
389
+ 'o',
390
+ [{ type: 'end' }],
391
+ ],
392
+ [
393
+ `root ::= foo | "bar"
394
+ foo ::= "foo"`,
395
+ '',
396
+ 'b',
397
+ [{ type: 'char', value: [ord('a')] }],
398
+ ],
399
+ [
400
+ `root ::= foo | "bar"
401
+ foo ::= "foo"`,
402
+ 'b',
403
+ 'a',
404
+ [{ type: 'char', value: [ord('r')] }],
405
+ ],
406
+ [
407
+ `root ::= foo | "bar"
408
+ foo ::= "foo"`,
409
+ 'ba',
410
+ 'r',
411
+ [{ type: 'end' }],
412
+ ],
413
+ // two expressions
414
+ [
415
+ `root ::= foo | bar
416
+ foo ::= "foo"
417
+ bar ::= "bar"`,
418
+ '',
419
+ 'f',
420
+ [{ type: 'char', value: [ord('o')] }],
421
+ ],
422
+ [
423
+ `
424
+ root ::= foo | bar
425
+ foo ::= "foo"
426
+ bar ::= "bar"`,
427
+ 'f',
428
+ 'o',
429
+ [{ type: 'char', value: [ord('o')] }],
430
+ ],
431
+ [
432
+ `
433
+ root ::= foo | bar
434
+ foo ::= "foo"
435
+ bar ::= "bar"`,
436
+ 'fo',
437
+ 'o',
438
+ [{ type: 'end' }],
439
+ ],
440
+ [
441
+ `root ::= foo | bar
442
+ foo ::= "foo"
443
+ bar ::= "bar"`,
444
+ '',
445
+ 'b',
446
+ [{ type: 'char', value: [ord('a')] }],
447
+ ],
448
+ [
449
+ `root ::= foo | bar
450
+ foo ::= "foo"
451
+ bar ::= "bar"`,
452
+ 'b',
453
+ 'a',
454
+ [{ type: 'char', value: [ord('r')] }],
455
+ ],
456
+ [
457
+ `root ::= foo | bar
458
+ foo ::= "foo"
459
+ bar ::= "bar"`,
460
+ 'ba',
461
+ 'r',
462
+ [{ type: 'end' }],
463
+ ],
464
+ // nested expressions
465
+ [
466
+ `
467
+ root ::= f | b
468
+ f ::= fo
469
+ b ::= ba
470
+ fo ::= foo
471
+ ba ::= bar | baz
472
+ foo ::= "foo"
473
+ bar ::= "bar"
474
+ baz ::= "baz"`,
475
+ '',
476
+ 'f',
477
+ [{ type: 'char', value: [ord('o')] }],
478
+ ],
479
+ [
480
+ `root ::= f | b
481
+ f ::= fo
482
+ b ::= ba
483
+ fo ::= foo
484
+ ba ::= bar | baz
485
+ foo ::= "foo"
486
+ bar ::= "bar"
487
+ baz ::= "baz"`,
488
+ 'f',
489
+ 'o',
490
+ [{ type: 'char', value: [ord('o')] }],
491
+ ],
492
+ [
493
+ `root ::= f | b
494
+ f ::= fo
495
+ b ::= ba
496
+ fo ::= foo
497
+ ba ::= bar | baz
498
+ foo ::= "foo"
499
+ bar ::= "bar"
500
+ baz ::= "baz"`,
501
+ 'fo',
502
+ 'o',
503
+ [{ type: 'end' }],
504
+ ],
505
+ [
506
+ `root ::= f | b
507
+ f ::= fo
508
+ b ::= ba
509
+ fo ::= foo
510
+ ba ::= bar | baz
511
+ foo ::= "foo"
512
+ bar ::= "bar"
513
+ baz ::= "baz"`,
514
+ '',
515
+ 'b',
516
+ [{ type: 'char', value: [ord('a')] }],
517
+ ],
518
+ [
519
+ `
520
+ root ::= f | b
521
+ f ::= fo
522
+ b ::= ba
523
+ fo ::= foo
524
+ ba ::= bar | baz
525
+ foo ::= "foo"
526
+ bar ::= "bar"
527
+ baz ::= "baz"`,
528
+ 'b',
529
+ 'a',
530
+ [{ type: 'char', value: [ord('r')] }, { type: 'char', value: [ord('z')] }],
531
+ ],
532
+ [
533
+ `
534
+ root ::= f | b
535
+ f ::= fo
536
+ b ::= ba
537
+ fo ::= foo
538
+ ba ::= bar | baz
539
+ foo ::= "foo"
540
+ bar ::= "bar"
541
+ baz ::= "baz"`,
542
+ 'ba',
543
+ 'r',
544
+ [{ type: 'end' }],
545
+ ],
546
+ [
547
+ `
548
+ root ::= f | b
549
+ f ::= fo
550
+ b ::= ba
551
+ fo ::= foo
552
+ ba ::= bar | baz
553
+ foo ::= "foo"
554
+ bar ::= "bar"
555
+ baz ::= "baz"`,
556
+ 'ba',
557
+ 'z',
558
+ [{ type: 'end' }],
559
+ ],
560
+ // ranges
561
+ ['root ::= [a-z]', '', 'a', [{ type: 'end' }]],
562
+ ['root ::= [a-z]', '', 'm', [{ type: 'end' }]],
563
+ ['root ::= [a-z]', '', 'z', [{ type: 'end' }]],
564
+ ['root ::= [a-zA-Z]', '', 'a', [{ type: 'end' }]],
565
+ ['root ::= [a-zA-Z]', '', 'Z', [{ type: 'end' }]],
566
+ ['root ::= [a-zA-Z0-9]', '', 'Z', [{ type: 'end' }]],
567
+ ['root ::= [a-zA-Z0-9]', '', '0', [{ type: 'end' }]],
568
+ ['root ::= [a-zA-Z0-9]', '', '9', [{ type: 'end' }]],
569
+ // range with ? modifier
570
+ ['root ::= [a-z]?', '', 'a', [{ type: 'end' }]],
571
+ ['root ::= [a-zA-Z]?', '', 'Z', [{ type: 'end' }]],
572
+ ['root ::= [a-zA-Z0-9]?', '', '0', [{ type: 'end' }]],
573
+ // range with + modifier
574
+ ['root ::= [a-z]+', '', 'a', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
575
+ [
576
+ 'root ::= [a-zA-Z]+',
577
+ '',
578
+ 'Z',
579
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
580
+ ],
581
+ [
582
+ 'root ::= [a-zA-Z]+',
583
+ 'azA',
584
+ 'Z',
585
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
586
+ ],
587
+ // range with * modifier
588
+ ['root ::= [a-z]*', '', 'a', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
589
+ [
590
+ 'root ::= [a-zA-Z]+',
591
+ '',
592
+ 'Z',
593
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
594
+ ],
595
+ [
596
+ 'root ::= [a-zA-Z]+',
597
+ 'acczABC',
598
+ 'Z',
599
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
600
+ ],
601
+ ['root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ', 'baza', 'a', [{ type: 'char', value: [ord('r')] }]],
602
+ // char not with modifiers
603
+ [
604
+ 'root ::= [^f]+ "o"',
605
+ 'aaa',
606
+ 'a',
607
+ [{ type: 'char_exclude', value: [ord('f')] }, { type: 'char', value: [ord('o')] }],
608
+ ],
609
+ ['root ::= [^A-Z]+', 'abc', 'd', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')]] }, { type: 'end' }]],
610
+ [
611
+ 'root ::= [^A-Z0-9]*',
612
+ 'abc',
613
+ 'd',
614
+ [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')]] }, { type: 'end' }],
615
+ ],
616
+ [
617
+ 'root ::= [^A-Z0-9_-]*',
618
+ 'abc',
619
+ 'd',
620
+ [
621
+ { type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')], ord('_'), ord('-')] },
622
+ { type: 'end' },
623
+ ],
624
+ ],
625
+ ])(
626
+ 'it parses a grammar %j with starting %j and additional %j [%#]',
627
+ (grammar, starting, additional, expected) => {
628
+ const transformedExpectedDict = (e: ExpectedRule): ResolvedRule => {
629
+ if (e.type === 'char') {
630
+ return new RuleChar(e.value);
631
+ } else if (e.type === 'char_exclude') {
632
+ return new RuleCharExclude(e.value);
633
+ } else if (e.type === 'end') {
634
+ return new RuleEnd();
635
+ } else {
636
+ throw new Error(`Unknown type found in expectation array: ${e.type}`);
637
+ }
638
+ };
639
+
640
+ let state = GBNF(grammar, starting);
641
+ state = state.add(additional);
642
+ expect([...state].sort(byJson)).toEqual(expected.map(transformedExpectedDict).sort(byJson));
643
+ },
644
+ );
645
+
646
+ test.each<boolean>([false, true])(
647
+ 'it raises a particular error (error for most recent input: %s)',
648
+ (errorForMostRecentInput) => {
649
+ const grammar = 'root ::= "bar"';
650
+ let state = GBNF(grammar);
651
+ state = state.add('b');
652
+ state = state.add('a');
653
+ const err = raises(InputParseError, () => state.add('z'));
654
+ const expectedError = new InputParseError('z', 0, 'ba');
655
+ if (errorForMostRecentInput) {
656
+ expect(err.errorForMostRecentInput).toEqual(expectedError.errorForMostRecentInput);
657
+ } else {
658
+ expect(err.message).toEqual(expectedError.message);
659
+ }
660
+ },
661
+ );
662
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration-with-an-initial-string.test.ts ADDED
@@ -0,0 +1,517 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GBNF, type Range, type ResolvedRule, RuleChar, RuleCharExclude, RuleEnd } from '../../../src/index.ts';
3
+
4
+ const ord = (char: string): number => char.codePointAt(0) as number;
5
+
6
+ interface ExpectedRule {
7
+ type: string;
8
+ value?: (number | Range)[];
9
+ }
10
+
11
+ const byJson = (a: ResolvedRule, b: ResolvedRule): number => {
12
+ const [keyA, keyB] = [JSON.stringify(a), JSON.stringify(b)];
13
+ if (keyA === keyB) {
14
+ return 0;
15
+ }
16
+ return keyA < keyB ? -1 : 1;
17
+ };
18
+
19
+ describe('iteration', () => {
20
+ test.each<[string, string, ExpectedRule[]]>([
21
+ // single char rule
22
+ ['root ::= "foo"', '', [{ type: 'char', value: [ord('f')] }]],
23
+ ['root ::= "foo"', 'f', [{ type: 'char', value: [ord('o')] }]],
24
+ ['root ::= "foo"', 'fo', [{ type: 'char', value: [ord('o')] }]],
25
+ ['root ::= "foo"', 'foo', [{ type: 'end' }]],
26
+ // two char rules
27
+ ['root ::= "foo" | "bar" ', '', [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }]],
28
+ ['root ::= "foo" | "bar" ', 'f', [{ type: 'char', value: [ord('o')] }]],
29
+ ['root ::= "foo" | "bar" ', 'fo', [{ type: 'char', value: [ord('o')] }]],
30
+ ['root ::= "foo" | "bar" ', 'foo', [{ type: 'end' }]],
31
+ ['root ::= "foo" | "bar" ', 'b', [{ type: 'char', value: [ord('a')] }]],
32
+ ['root ::= "foo" | "bar" ', 'ba', [{ type: 'char', value: [ord('r')] }]],
33
+ ['root ::= "foo" | "bar" ', 'bar', [{ type: 'end' }]],
34
+ // three char rules
35
+ [
36
+ 'root ::= "foo" | "bar" | "baz"',
37
+ 'ba',
38
+ [{ type: 'char', value: [ord('r')] }, { type: 'char', value: [ord('z')] }],
39
+ ],
40
+ // chars that shadow built-in characters
41
+ ['root ::= "["', '[', [{ type: 'end' }]],
42
+ ['root ::= "]"', ']', [{ type: 'end' }]],
43
+ ['root ::= "[]"', '[]', [{ type: 'end' }]],
44
+ ['root ::= "{"', '{', [{ type: 'end' }]],
45
+ ['root ::= "}"', '}', [{ type: 'end' }]],
46
+ ['root ::= "{}"', '{}', [{ type: 'end' }]],
47
+ ['root ::= "[{}]"', '[{}]', [{ type: 'end' }]],
48
+ // ('root ::= "[{\"}]"', '[{"}]', [
49
+ // { "type": 'end' },
50
+ // ]),
51
+ // char not
52
+ ['root ::= [^f] "o"', '', [{ type: 'char_exclude', value: [ord('f')] }]],
53
+ ['root ::= [^f] "o"', 'a', [{ type: 'char', value: [ord('o')] }]],
54
+ ['root ::= [^A-Z]', '', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')]] }]],
55
+ ['root ::= [^A-Z0-9]', '', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')]] }]],
56
+ [
57
+ 'root ::= [^A-Z0-9_-]',
58
+ '',
59
+ [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')], ord('_'), ord('-')] }],
60
+ ],
61
+ // expressions
62
+ [
63
+ `
64
+ root ::= foo
65
+ foo ::= "foo"`,
66
+ '',
67
+ [{ type: 'char', value: [ord('f')] }],
68
+ ],
69
+ [
70
+ `root ::= foo
71
+ foo ::= "foo"
72
+ `,
73
+ 'f',
74
+ [{ type: 'char', value: [ord('o')] }],
75
+ ],
76
+ [
77
+ `
78
+ root ::= foo
79
+ foo ::= "foo"
80
+ `,
81
+ 'fo',
82
+ [{ type: 'char', value: [ord('o')] }],
83
+ ],
84
+ [
85
+ `root ::= foo
86
+ foo ::= "foo"`,
87
+ 'foo',
88
+ [{ type: 'end' }],
89
+ ],
90
+ [
91
+ `root ::= f
92
+ f ::= foo
93
+ foo ::= "foo"`,
94
+ '',
95
+ [{ type: 'char', value: [ord('f')] }],
96
+ ],
97
+ // expression and a char rule
98
+ [
99
+ `
100
+ root ::= foo | "bar"
101
+ foo ::= "foo"
102
+ `,
103
+ '',
104
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
105
+ ],
106
+ [
107
+ `
108
+ root ::= foo | "bar"
109
+ foo ::= "foo"`,
110
+ 'f',
111
+ [{ type: 'char', value: [ord('o')] }],
112
+ ],
113
+ [
114
+ `root ::= foo | "bar"
115
+ foo ::= "foo"`,
116
+ 'fo',
117
+ [{ type: 'char', value: [ord('o')] }],
118
+ ],
119
+ [
120
+ `root ::= foo | "bar"
121
+ foo ::= "foo"`,
122
+ 'foo',
123
+ [{ type: 'end' }],
124
+ ],
125
+ [
126
+ `root ::= foo | "bar"
127
+ foo ::= "foo"`,
128
+ 'b',
129
+ [{ type: 'char', value: [ord('a')] }],
130
+ ],
131
+ [
132
+ `root ::= foo | "bar"
133
+ foo ::= "foo"`,
134
+ 'ba',
135
+ [{ type: 'char', value: [ord('r')] }],
136
+ ],
137
+ [
138
+ `root ::= foo | "bar"
139
+ foo ::= "foo"`,
140
+ 'bar',
141
+ [{ type: 'end' }],
142
+ ],
143
+ // two expressions
144
+ [
145
+ `
146
+ root ::= foo | bar
147
+ foo ::= "foo"
148
+ bar ::= "bar"`,
149
+ '',
150
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
151
+ ],
152
+ [
153
+ `
154
+ root ::= foo | bar
155
+ foo ::= "foo"
156
+ bar ::= "bar"`,
157
+ 'f',
158
+ [{ type: 'char', value: [ord('o')] }],
159
+ ],
160
+ [
161
+ `
162
+ root ::= foo | bar
163
+ foo ::= "foo"
164
+ bar ::= "bar"`,
165
+ 'fo',
166
+ [{ type: 'char', value: [ord('o')] }],
167
+ ],
168
+ [
169
+ `
170
+ root ::= foo | bar
171
+ foo ::= "foo"
172
+ bar ::= "bar"`,
173
+ 'foo',
174
+ [{ type: 'end' }],
175
+ ],
176
+ [
177
+ `
178
+ root ::= foo | bar
179
+ foo ::= "foo"
180
+ bar ::= "bar"`,
181
+ 'b',
182
+ [{ type: 'char', value: [ord('a')] }],
183
+ ],
184
+ [
185
+ `
186
+ root ::= foo | bar
187
+ foo ::= "foo"
188
+ bar ::= "bar"`,
189
+ 'ba',
190
+ [{ type: 'char', value: [ord('r')] }],
191
+ ],
192
+ [
193
+ `
194
+ root ::= foo | bar
195
+ foo ::= "foo"
196
+ bar ::= "bar"`,
197
+ 'bar',
198
+ [{ type: 'end' }],
199
+ ],
200
+ // nested expressions
201
+ [
202
+ `
203
+ root ::= f | b
204
+ f ::= fo
205
+ b ::= ba
206
+ fo ::= foo
207
+ ba ::= bar | baz
208
+ foo ::= "foo"
209
+ bar ::= "bar"
210
+ baz ::= "baz"`,
211
+ '',
212
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
213
+ ],
214
+ [
215
+ `
216
+ root ::= f | b
217
+ f ::= fo
218
+ b ::= ba
219
+ fo ::= foo
220
+ ba ::= bar | baz
221
+ foo ::= "foo"
222
+ bar ::= "bar"
223
+ baz ::= "baz"`,
224
+ 'f',
225
+ [{ type: 'char', value: [ord('o')] }],
226
+ ],
227
+ [
228
+ `
229
+ root ::= f | b
230
+ f ::= fo
231
+ b ::= ba
232
+ fo ::= foo
233
+ ba ::= bar | baz
234
+ foo ::= "foo"
235
+ bar ::= "bar"
236
+ baz ::= "baz"`,
237
+ 'fo',
238
+ [{ type: 'char', value: [ord('o')] }],
239
+ ],
240
+ [
241
+ `
242
+ root ::= f | b
243
+ f ::= fo
244
+ b ::= ba
245
+ fo ::= foo
246
+ ba ::= bar | baz
247
+ foo ::= "foo"
248
+ bar ::= "bar"
249
+ baz ::= "baz"`,
250
+ 'foo',
251
+ [{ type: 'end' }],
252
+ ],
253
+ [
254
+ `
255
+ root ::= f | b
256
+ f ::= fo
257
+ b ::= ba
258
+ fo ::= foo
259
+ ba ::= bar | baz
260
+ foo ::= "foo"
261
+ bar ::= "bar"
262
+ baz ::= "baz"`,
263
+ 'b',
264
+ [{ type: 'char', value: [ord('a')] }],
265
+ ],
266
+ [
267
+ `
268
+ root ::= f | b
269
+ f ::= fo
270
+ b ::= ba
271
+ fo ::= foo
272
+ ba ::= bar | baz
273
+ foo ::= "foo"
274
+ bar ::= "bar"
275
+ baz ::= "baz"`,
276
+ 'ba',
277
+ [{ type: 'char', value: [ord('r')] }, { type: 'char', value: [ord('z')] }],
278
+ ],
279
+ [
280
+ `
281
+ root ::= f | b
282
+ f ::= fo
283
+ b ::= ba
284
+ fo ::= foo
285
+ ba ::= bar | baz
286
+ foo ::= "foo"
287
+ bar ::= "bar"
288
+ baz ::= "baz"`,
289
+ 'bar',
290
+ [{ type: 'end' }],
291
+ ],
292
+ [
293
+ `
294
+ root ::= f | b
295
+ f ::= fo
296
+ b ::= ba
297
+ fo ::= foo
298
+ ba ::= bar | baz
299
+ foo ::= "foo"
300
+ bar ::= "bar"
301
+ baz ::= "baz"`,
302
+ 'baz',
303
+ [{ type: 'end' }],
304
+ ],
305
+ // ranges
306
+ ['root ::= [a-z]', '', [{ type: 'char', value: [[ord('a'), ord('z')]] }]],
307
+ ['root ::= [a-zA-Z]', '', [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }]],
308
+ ['root ::= [a-z]', 'a', [{ type: 'end' }]],
309
+ ['root ::= [a-z]', 'm', [{ type: 'end' }]],
310
+ ['root ::= [a-z]', 'z', [{ type: 'end' }]],
311
+ ['root ::= [a-zA-Z]', 'a', [{ type: 'end' }]],
312
+ ['root ::= [a-zA-Z]', 'Z', [{ type: 'end' }]],
313
+ ['root ::= [a-zA-Z0-9]', 'Z', [{ type: 'end' }]],
314
+ ['root ::= [a-zA-Z0-9]', '0', [{ type: 'end' }]],
315
+ ['root ::= [a-zA-Z0-9]', '9', [{ type: 'end' }]],
316
+ // range with ? modifier
317
+ ['root ::= [a-z]?', '', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
318
+ ['root ::= [a-z]?', 'a', [{ type: 'end' }]],
319
+ ['root ::= [a-zA-Z]?', 'Z', [{ type: 'end' }]],
320
+ ['root ::= [a-zA-Z0-9]?', '0', [{ type: 'end' }]],
321
+ // range with + modifier
322
+ ['root ::= [a-z]+', '', [{ type: 'char', value: [[ord('a'), ord('z')]] }]],
323
+ ['root ::= [a-z]+', 'l', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
324
+ [
325
+ 'root ::= [a-zA-Z]+',
326
+ 'Z',
327
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
328
+ ],
329
+ [
330
+ 'root ::= [a-zA-Z]+',
331
+ 'aZ',
332
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
333
+ ],
334
+ [
335
+ 'root ::= [a-zA-Z]+',
336
+ 'azAZ',
337
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
338
+ ],
339
+ // range with * modifier
340
+ ['root ::= [a-z]*', '', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
341
+ ['root ::= [a-z]*', 'a', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
342
+ [
343
+ 'root ::= [a-zA-Z]+',
344
+ 'Z',
345
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
346
+ ],
347
+ [
348
+ 'root ::= [a-zA-Z]+',
349
+ 'abczABCZ',
350
+ [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }, { type: 'end' }],
351
+ ],
352
+ // char not with modifiers
353
+ ['root ::= [^f]+ "o"', 'aaa', [{ type: 'char_exclude', value: [ord('f')] }, { type: 'char', value: [ord('o')] }]],
354
+ ['root ::= [^A-Z]+', 'abc', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')]] }, { type: 'end' }]],
355
+ [
356
+ 'root ::= [^A-Z0-9]*',
357
+ 'abc',
358
+ [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')]] }, { type: 'end' }],
359
+ ],
360
+ [
361
+ 'root ::= [^A-Z0-9_-]*',
362
+ 'abc',
363
+ [
364
+ { type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')], ord('_'), ord('-')] },
365
+ { type: 'end' },
366
+ ],
367
+ ],
368
+ // real world bugs
369
+ [
370
+ `
371
+ root ::= [a-z] | xyx
372
+ xyx ::= "foo"`,
373
+ 'f',
374
+ [{ type: 'end' }, { type: 'char', value: [ord('o')] }],
375
+ ],
376
+ // "baz" and "bazaar" are ambiguous, and a string "bazaa" should not result in an 'a' CHAR rule
377
+ ['root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ', 'bazaa', [{ type: 'char', value: [ord('r')] }]],
378
+ // should be able to step _into_ a rule, and then continue with the previous rule
379
+ ['root ::= ("bar" | "foo") "zyx"', 'bar', [{ type: 'char', value: [ord('z')] }]],
380
+ // should be able to process a rule, then step _into_ a rule, with a pointer to previous rule
381
+ [
382
+ 'root ::= "z" ("bar" | "foo") "zzzz"',
383
+ 'z',
384
+ [{ type: 'char', value: [ord('b')] }, { type: 'char', value: [ord('f')] }],
385
+ ],
386
+ // should be able to process a rule, then step _into_ a rule, and then continue with the previous rule
387
+ ['root ::= "z" ("bar" | "foo") "zzz"', 'zbar', [{ type: 'char', value: [ord('z')] }]],
388
+ [
389
+ '\nroot ::= termz ([-+*/] termz)* \ntermz ::= [0-9]+',
390
+ '1',
391
+ [
392
+ { type: 'char', value: [[ord('0'), ord('9')]] },
393
+ { type: 'char', value: [ord('-'), ord('+'), ord('*'), ord('/')] },
394
+ { type: 'end' },
395
+ ],
396
+ ],
397
+ [
398
+ '\nroot ::= expr "=" termy \nexpr ::= termy ([-+*/] termy)*\ntermy ::= [0-9]+',
399
+ '1',
400
+ [
401
+ { type: 'char', value: [[ord('0'), ord('9')]] },
402
+ { type: 'char', value: [ord('-'), ord('+'), ord('*'), ord('/')] },
403
+ { type: 'char', value: [ord('=')] },
404
+ ],
405
+ ],
406
+ [
407
+ `
408
+ root ::= (expr "=" terma "
409
+ ")+
410
+ expr ::= terma ([-+*/] terma)*
411
+ terma ::= [0-9]+`,
412
+ '1',
413
+ [
414
+ { type: 'char', value: [[ord('0'), ord('9')]] },
415
+ { type: 'char', value: [ord('-'), ord('+'), ord('*'), ord('/')] },
416
+ { type: 'char', value: [ord('=')] },
417
+ ],
418
+ ],
419
+ [
420
+ `root ::= (expr "=" termb "
421
+ ")+
422
+ expr ::= termb ([-+*/] termb)*
423
+ termb ::= [0-9]+`,
424
+ '1+',
425
+ [{ type: 'char', value: [[ord('0'), ord('9')]] }],
426
+ ],
427
+ [
428
+ `
429
+ root ::= (expr "=" termc "
430
+ ")+
431
+ expr ::= termc ([-+*/] termc)*
432
+ termc ::= [0-9]+`,
433
+ '1=',
434
+ [{ type: 'char', value: [[ord('0'), ord('9')]] }],
435
+ ],
436
+ [
437
+ `root ::= (expr "=" termd "
438
+ ")+
439
+ expr ::= termd ([-+*/] termd)*
440
+ termd ::= [0-9]+`,
441
+ '1+1',
442
+ [
443
+ { type: 'char', value: [[ord('0'), ord('9')]] },
444
+ { type: 'char', value: [ord('-'), ord('+'), ord('*'), ord('/')] },
445
+ { type: 'char', value: [ord('=')] },
446
+ ],
447
+ ],
448
+ [
449
+ `root ::= (expr "=" term "
450
+ ")+
451
+ expr ::= term ([-+*/] term)*
452
+ term ::= [0-9]+`,
453
+ '1=1',
454
+ [{ type: 'char', value: [[ord('0'), ord('9')]] }, { type: 'char', value: [ord('\n')] }],
455
+ ],
456
+ [
457
+ 'root ::= "\\"" ( [^\\"abcdefghA-Z])* ',
458
+ '"is not only in its lyrism, its vow to sustin it in its poo-sion, its r,v:l\'y, it\'s tory,',
459
+ [
460
+ {
461
+ type: 'char_exclude',
462
+ value: [
463
+ ord('"'),
464
+ ord('a'),
465
+ ord('b'),
466
+ ord('c'),
467
+ ord('d'),
468
+ ord('e'),
469
+ ord('f'),
470
+ ord('g'),
471
+ ord('h'),
472
+ [ord('A'), ord('Z')],
473
+ ],
474
+ },
475
+ { type: 'end' },
476
+ ],
477
+ ],
478
+ // sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
479
+ // side by side, _either_ is valid.
480
+ // that means that input explicitly forbidden by char_not can be allowed by the char
481
+ // rule.
482
+ // it seems weird. but it's the way it is.
483
+ // so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
484
+ // also allowed, because of the first rule. So really the only character disallowed is 'a'.
485
+ [
486
+ 'root ::= ( [^abcdefgh] | [b-z])* ',
487
+ 'bcdefghzyxw0123ABCZ',
488
+ [
489
+ {
490
+ type: 'char_exclude',
491
+ value: [ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), ord('f'), ord('g'), ord('h')],
492
+ },
493
+ { type: 'char', value: [[ord('b'), ord('z')]] },
494
+ { type: 'end' },
495
+ ],
496
+ ],
497
+ ])(
498
+ 'it returns parse state for grammar %j and initial string %j [%#]',
499
+ (grammar, inputString, expected) => {
500
+ const transformedExpectedDict = (e: ExpectedRule): ResolvedRule => {
501
+ if (e.type === 'char') {
502
+ return new RuleChar(e.value);
503
+ } else if (e.type === 'char_exclude') {
504
+ return new RuleCharExclude(e.value);
505
+ } else if (e.type === 'end') {
506
+ return new RuleEnd();
507
+ } else {
508
+ throw new Error(`Unknown type found in expectation array: ${e.type}`);
509
+ }
510
+ };
511
+
512
+ let state = GBNF(grammar);
513
+ state = state.add(inputString);
514
+ expect([...state].sort(byJson)).toEqual(expected.map(transformedExpectedDict).sort(byJson));
515
+ },
516
+ );
517
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/iteration/iteration.test.ts ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GBNF, type Range, type ResolvedRule, RuleChar, RuleCharExclude, RuleEnd } from '../../../src/index.ts';
3
+
4
+ const ord = (char: string): number => char.codePointAt(0) as number;
5
+
6
+ interface ExpectedRule {
7
+ type: string;
8
+ value?: (number | Range)[];
9
+ }
10
+
11
+ describe('iteration', () => {
12
+ test.each<[string, ExpectedRule[]]>([
13
+ // single char rule
14
+ ['root ::= "foo"', [{ type: 'char', value: [ord('f')] }]],
15
+ // two char rules
16
+ ['root ::= "foo" | "bar" ', [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }]],
17
+ // three char rules
18
+ [
19
+ 'root ::= "foo" | "bar" | "gaz"',
20
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }, { type: 'char', value: [ord('g')] }],
21
+ ],
22
+ ['root ::= "foo" | "bar" | "baz"', [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }]],
23
+ // char not
24
+ ['root ::= [^x]', [{ type: 'char_exclude', value: [ord('x')] }]],
25
+ ['root ::= [^f] "o"', [{ type: 'char_exclude', value: [ord('f')] }]],
26
+ ['root ::= [^A-Z]', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')]] }]],
27
+ ['root ::= [^A-Z0-9]', [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')]] }]],
28
+ [
29
+ 'root ::= [^A-Z0-9_-]',
30
+ [{ type: 'char_exclude', value: [[ord('A'), ord('Z')], [ord('0'), ord('9')], ord('_'), ord('-')] }],
31
+ ],
32
+ // expressions
33
+ [
34
+ `
35
+ root ::= foo
36
+ foo ::= "foo"
37
+ `,
38
+ [{ type: 'char', value: [ord('f')] }],
39
+ ],
40
+ [
41
+ `
42
+ root ::= f
43
+ f ::= foo
44
+ foo ::= "foo"
45
+ `,
46
+ [{ type: 'char', value: [ord('f')] }],
47
+ ],
48
+ // expression and a char rule
49
+ [
50
+ `
51
+ root ::= foo | "bar"
52
+ foo ::= "foo"`,
53
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
54
+ ],
55
+ // two expressions
56
+ [
57
+ `
58
+ root ::= foo | bar
59
+ foo ::= "foo"
60
+ bar ::= "bar"
61
+ `,
62
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
63
+ ],
64
+ // nested expressions
65
+ [
66
+ `
67
+ root ::= f | b
68
+ f ::= fo
69
+ b ::= ba
70
+ fo ::= foo
71
+ ba ::= bar | baz
72
+ foo ::= "foo"
73
+ bar ::= "bar"
74
+ baz ::= "baz"
75
+ `,
76
+ [{ type: 'char', value: [ord('f')] }, { type: 'char', value: [ord('b')] }],
77
+ ],
78
+ // ranges
79
+ ['root ::= [a-z]', [{ type: 'char', value: [[ord('a'), ord('z')]] }]],
80
+ ['root ::= [a-zA-Z]', [{ type: 'char', value: [[ord('a'), ord('z')], [ord('A'), ord('Z')]] }]],
81
+ // range with ? modifier
82
+ ['root ::= [a-z]?', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
83
+ // range with + modifier
84
+ ['root ::= [a-z]+', [{ type: 'char', value: [[ord('a'), ord('z')]] }]],
85
+ // range with * modifier
86
+ ['root ::= [a-z]*', [{ type: 'char', value: [[ord('a'), ord('z')]] }, { type: 'end' }]],
87
+ // rules that aren't used are silently ignored
88
+ [
89
+ `
90
+ root ::= "foo"
91
+ foo ::= "foo"`,
92
+ [{ type: 'char', value: [ord('f')] }],
93
+ ],
94
+ // real world use cases
95
+ // arithmetic
96
+ [
97
+ `
98
+ root ::= (expr "=" term "")+
99
+ expr ::= term ([-+*/] term)*
100
+ term ::= [0-9]+
101
+ `,
102
+ [{ type: 'char', value: [[ord('0'), ord('9')]] }],
103
+ ],
104
+ ])('it returns parse state for grammar %j [%#]', (grammar, expected) => {
105
+ const transformedExpectedDict = (e: ExpectedRule): ResolvedRule => {
106
+ if (e.type === 'char') {
107
+ return new RuleChar(e.value);
108
+ } else if (e.type === 'char_exclude') {
109
+ return new RuleCharExclude(e.value);
110
+ } else if (e.type === 'end') {
111
+ return new RuleEnd();
112
+ } else {
113
+ throw new Error(`Unknown type found in expectation array: ${e.type}`);
114
+ }
115
+ };
116
+
117
+ const state = [...GBNF(grammar)];
118
+ expect(state).toEqual(expected.map(transformedExpectedDict));
119
+ });
120
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/validation/validate-grammar.test.ts ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GBNF, GrammarParseError } from '../../../src/index.ts';
3
+
4
+ // runs the callback, and returns the error it is expected to throw
5
+ const raises = <T extends Error>(expected: new (...args: never[]) => T, callback: () => unknown): T => {
6
+ let error: unknown;
7
+ try {
8
+ callback();
9
+ } catch (err) {
10
+ error = err;
11
+ }
12
+ expect(error).toBeInstanceOf(expected);
13
+ return error as T;
14
+ };
15
+
16
+ describe('validate grammar', () => {
17
+ test.each<string>([
18
+ 'root ::= "foo"',
19
+ 'root ::= "foo" | "bar"',
20
+ 'root ::= ("foo" | "bar")',
21
+ 'root ::= ("foo" | "bar")?',
22
+ 'root ::= ("foo" | "bar")*',
23
+ 'root ::= ("foo" | "bar")+',
24
+ 'root ::= [a-z]',
25
+ 'root ::= [a-zA-Z]',
26
+ 'root ::= [a-zA-Z0-9]',
27
+ 'root ::= [a-zA-Z0-9]*',
28
+ 'root ::= [a-zA-Z0-9]?',
29
+ 'root ::= [a-z]+',
30
+ 'root ::= [a-zA-Z0-9]+',
31
+ 'root ::= ([a-zA-Z0-9])*',
32
+ 'root ::= ([a-zA-Z0-9])?',
33
+ 'root ::= ([a-zA-Z0-9])+',
34
+ `
35
+ root ::= foo
36
+ foo ::= "foo"`,
37
+ `
38
+ root ::= foo
39
+ foo ::= "foo" | "bar" | ([a-z])?
40
+ `,
41
+ ])('it parses a grammar %j [%#]', (grammar) => {
42
+ GBNF(grammar);
43
+ });
44
+
45
+ test.each<[string, number, string]>([
46
+ ['', 0, 'No rules were found'],
47
+ ['root = "foo"', 5, 'Expecting ::= at 5'],
48
+ [
49
+ `root ::= foo
50
+ foo := "foo"
51
+ `,
52
+ 17,
53
+ 'Expecting ::= at 17',
54
+ ],
55
+ ['root ::= foo', 9, 'Undefined rule identifier "foo"'],
56
+ [
57
+ `root ::= foo
58
+ bar ::= "bar"
59
+ `,
60
+ 9,
61
+ 'Undefined rule identifier "foo"',
62
+ ],
63
+ [
64
+ `root ::= foo
65
+ foo ::= baz
66
+ bar ::= "bar"
67
+ `,
68
+ 21,
69
+ 'Undefined rule identifier "baz"',
70
+ ],
71
+ ['root ::= foo ::= bar', 13, 'Expecting newline or end at 13'],
72
+ [
73
+ `root ::= ([a-z]
74
+ foo ::= "foo"
75
+ `,
76
+ 20,
77
+ "Expecting ')' at 20",
78
+ ],
79
+ ])(
80
+ 'it reports an error for an invalid grammar %j [%#]',
81
+ (grammar, errorPos, errorReason) => {
82
+ const expected = new GrammarParseError(grammar, errorPos, errorReason);
83
+ const e = raises(GrammarParseError, () => GBNF(grammar));
84
+ expect(e.message).toEqual(expected.message);
85
+ },
86
+ );
87
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tests/generated/validation/validate-input.test.ts ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, test } from 'vitest';
2
+ import { GBNF, InputParseError } from '../../../src/index.ts';
3
+
4
+ // runs the callback, and returns the error it is expected to throw
5
+ const raises = <T extends Error>(expected: new (...args: never[]) => T, callback: () => unknown): T => {
6
+ let error: unknown;
7
+ try {
8
+ callback();
9
+ } catch (err) {
10
+ error = err;
11
+ }
12
+ expect(error).toBeInstanceOf(expected);
13
+ return error as T;
14
+ };
15
+
16
+ describe('validate input', () => {
17
+ test.each<[string, string]>([['root ::= "foo"', 'f']])(
18
+ 'it parses a grammar %j with input %j [%#]',
19
+ (grammar, input) => {
20
+ const graph = GBNF(grammar, input);
21
+ expect(Boolean(graph)).toBe(true);
22
+ },
23
+ );
24
+
25
+ test.each<[string, string, number]>([
26
+ ['root ::= "foo"', '2', 0],
27
+ ['root ::= "foo"', 'b', 0],
28
+ ['root ::= "foo"', 'f1', 1],
29
+ ['root ::= "foo"', 'fo1', 2],
30
+ ['root ::= "foo"', 'fooo', 3],
31
+ ['root ::= "foo"', 'fooooooo', 3],
32
+ ['root ::= "foo" | "bar"', '1', 0],
33
+ ['root ::= "foo" | "bar"', 'z', 0],
34
+ ['root ::= "foo" | "bar"', 'f1', 1],
35
+ ['root ::= "foo" | "bar"', 'b1', 1],
36
+ ['root ::= "foo" | "bar"', 'fo1', 2],
37
+ ['root ::= "foo" | "bar"', 'ba1', 2],
38
+ ['root ::= "foo" | "bar"', 'fooo', 3],
39
+ ['root ::= "foo" | "bar"', 'barrr', 3],
40
+ ['root ::= "foo" | "bar" | "baz"', '1', 0],
41
+ ['root ::= "foo" | "bar" | "baz"', 'z', 0],
42
+ ['root ::= "foo" | "bar" | "baz"', 'f1', 1],
43
+ ['root ::= "foo" | "bar" | "baz"', 'b1', 1],
44
+ ['root ::= "foo" | "bar" | "baz"', 'fo1', 2],
45
+ ['root ::= "foo" | "bar" | "baz"', 'bal', 2],
46
+ ['root ::= "foo" | "bar" | "baz"', 'fooo', 3],
47
+ ['root ::= "foo" | "bar" | "baz"', 'bazrr', 3],
48
+ ['root ::= [^a]', 'a', 0],
49
+ ['root ::= [^abc]', 'b', 0],
50
+ ['root ::= [^a-z]', 'z', 0],
51
+ ['root ::= [^a-zA-Z]', 'X', 0],
52
+ ['root ::= [^a-zA-Z0-9]', '8', 0],
53
+ [
54
+ `
55
+ root ::= foo
56
+ foo ::= "foo"`,
57
+ '1',
58
+ 0,
59
+ ],
60
+ [
61
+ `
62
+ root ::= foo
63
+ foo ::= "foo"`,
64
+ 'b',
65
+ 0,
66
+ ],
67
+ [
68
+ `
69
+ root ::= foo
70
+ foo ::= "foo"`,
71
+ 'f1',
72
+ 1,
73
+ ],
74
+ [
75
+ `
76
+ root ::= foo
77
+ foo ::= "foo"`,
78
+ 'fo1',
79
+ 2,
80
+ ],
81
+ [
82
+ `
83
+ root ::= foo
84
+ foo ::= "foo"`,
85
+ 'fooo',
86
+ 3,
87
+ ],
88
+ [
89
+ `
90
+ root::=foo|"bar"
91
+ foo::="foo"`,
92
+ '1',
93
+ 0,
94
+ ],
95
+ [
96
+ `
97
+ root::=foo|"bar"
98
+ foo::="foo"`,
99
+ 'z',
100
+ 0,
101
+ ],
102
+ [
103
+ `
104
+ root::=foo|"bar"
105
+ foo::="foo"`,
106
+ 'f1',
107
+ 1,
108
+ ],
109
+ [
110
+ `
111
+ root::=foo|"bar"
112
+ foo::="foo"`,
113
+ 'b1',
114
+ 1,
115
+ ],
116
+ [
117
+ `
118
+ root::=foo|"bar"
119
+ foo::="foo"`,
120
+ 'fo1',
121
+ 2,
122
+ ],
123
+ [
124
+ `
125
+ root::=foo|"bar"
126
+ foo::="foo"`,
127
+ 'fooo',
128
+ 3,
129
+ ],
130
+ [
131
+ `
132
+ root::=foo|"bar"
133
+ foo::="foo"`,
134
+ 'barr',
135
+ 3,
136
+ ],
137
+ [
138
+ `
139
+ root::= foo | bar
140
+ foo::="foo"
141
+ bar::="bar"`,
142
+ '1',
143
+ 0,
144
+ ],
145
+ [
146
+ `
147
+ root::= foo | bar
148
+ foo::="foo"
149
+ bar::="bar"`,
150
+ 'z',
151
+ 0,
152
+ ],
153
+ [
154
+ `
155
+ root::= foo | bar
156
+ foo::="foo"
157
+ bar::="bar"`,
158
+ 'f1',
159
+ 1,
160
+ ],
161
+ [
162
+ `
163
+ root::= foo | bar
164
+ foo::="foo"
165
+ bar::="bar"`,
166
+ 'b1',
167
+ 1,
168
+ ],
169
+ [
170
+ `
171
+ root::= foo | bar
172
+ foo::="foo"
173
+ bar::="bar"`,
174
+ 'fo1',
175
+ 2,
176
+ ],
177
+ [
178
+ `
179
+ root::= foo | bar
180
+ foo::="foo"
181
+ bar::="bar"`,
182
+ 'fooo',
183
+ 3,
184
+ ],
185
+ [
186
+ `
187
+ root::= foo | bar
188
+ foo::="foo"
189
+ bar::="bar"`,
190
+ 'barr',
191
+ 3,
192
+ ],
193
+ [
194
+ `
195
+ root ::= f | b
196
+ f ::= fo
197
+ b ::= ba
198
+ fo ::= foo
199
+ ba ::= bar | baz
200
+ foo ::= "foo"
201
+ bar ::= "bar"
202
+ baz ::= "baz"
203
+ `,
204
+ '1',
205
+ 0,
206
+ ],
207
+ [
208
+ `
209
+ root ::= f | b
210
+ f ::= fo
211
+ b ::= ba
212
+ fo ::= foo
213
+ ba ::= bar | baz
214
+ foo ::= "foo"
215
+ bar ::= "bar"
216
+ baz ::= "baz"`,
217
+ 'z',
218
+ 0,
219
+ ],
220
+ [
221
+ `
222
+ root ::= f | b
223
+ f ::= fo
224
+ b ::= ba
225
+ fo ::= foo
226
+ ba ::= bar | baz
227
+ foo ::= "foo"
228
+ bar ::= "bar"
229
+ baz ::= "baz"`,
230
+ 'f1',
231
+ 1,
232
+ ],
233
+ [
234
+ `
235
+ root ::= f | b
236
+ f ::= fo
237
+ b ::= ba
238
+ fo ::= foo
239
+ ba ::= bar | baz
240
+ foo ::= "foo"
241
+ bar ::= "bar"
242
+ baz ::= "baz"`,
243
+ 'b1',
244
+ 1,
245
+ ],
246
+ [
247
+ `
248
+ root ::= f | b
249
+ f ::= fo
250
+ b ::= ba
251
+ fo ::= foo
252
+ ba ::= bar | baz
253
+ foo ::= "foo"
254
+ bar ::= "bar"
255
+ baz ::= "baz"`,
256
+ 'fo1',
257
+ 2,
258
+ ],
259
+ [
260
+ `
261
+ root ::= f | b
262
+ f ::= fo
263
+ b ::= ba
264
+ fo ::= foo
265
+ ba ::= bar | baz
266
+ foo ::= "foo"
267
+ bar ::= "bar"
268
+ baz ::= "baz"`,
269
+ 'fooo',
270
+ 3,
271
+ ],
272
+ [
273
+ `
274
+ root ::= f | b
275
+ f ::= fo
276
+ b ::= ba
277
+ fo ::= foo
278
+ ba ::= bar | baz
279
+ foo ::= "foo"
280
+ bar ::= "bar"
281
+ baz ::= "baz"`,
282
+ 'barr',
283
+ 3,
284
+ ],
285
+ [
286
+ `
287
+ root ::= f | b
288
+ f ::= fo
289
+ b ::= ba
290
+ fo ::= foo
291
+ ba ::= bar | baz
292
+ foo ::= "foo"
293
+ bar ::= "bar"
294
+ baz ::= "baz"`,
295
+ 'bazr',
296
+ 3,
297
+ ],
298
+ ['root ::= [a-z]', 'A', 0],
299
+ ['root ::= [a-z]', '0', 0],
300
+ ['root ::= [a-z]', 'az', 1],
301
+ ['root ::= [a-z]?', 'A', 0],
302
+ ['root ::= [a-z]?', '0', 0],
303
+ ['root ::= [a-z]?', 'az', 1],
304
+ ['root ::= [a-z]+', 'A', 0],
305
+ ['root ::= [a-z]+', '0', 0],
306
+ ['root ::= [a-z]+', 'az0', 2],
307
+ ['root ::= [a-z]*', 'A', 0],
308
+ ['root ::= [a-z]*', '0', 0],
309
+ ['root ::= [a-z]*', 'az0', 2],
310
+ // sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
311
+ // side by side, _either_ is valid.
312
+ // that means that input explicitly forbidden by char_not can be allowed by the char
313
+ // rule.
314
+ // it seems weird. but it's the way it is.
315
+ // so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
316
+ // also allowed, because of the first rule. So really the only character disallowed is 'a'.
317
+ [
318
+ // sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
319
+ // side by side, _either_ is valid.
320
+ // that means that input explicitly forbidden by char_not can be allowed by the char
321
+ // rule.
322
+ // it seems weird. but it's the way it is.
323
+ // so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
324
+ // also allowed, because of the first rule. So really the only character disallowed is 'a'.
325
+ 'root ::= ( [^abcdefgh] | [b-z])*',
326
+ // sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
327
+ // side by side, _either_ is valid.
328
+ // that means that input explicitly forbidden by char_not can be allowed by the char
329
+ // rule.
330
+ // it seems weird. but it's the way it is.
331
+ // so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
332
+ // also allowed, because of the first rule. So really the only character disallowed is 'a'.
333
+ 'a',
334
+ // sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
335
+ // side by side, _either_ is valid.
336
+ // that means that input explicitly forbidden by char_not can be allowed by the char
337
+ // rule.
338
+ // it seems weird. but it's the way it is.
339
+ // so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
340
+ // also allowed, because of the first rule. So really the only character disallowed is 'a'.
341
+ 0,
342
+ ],
343
+ ])(
344
+ 'it reports an error for an invalid input %j for grammar %j [%#]',
345
+ (grammar, inputText, errorPos) => {
346
+ const graph = GBNF(grammar);
347
+ const expected = new InputParseError(inputText, errorPos);
348
+ const e = raises(InputParseError, () => graph.add(inputText));
349
+ expect(e.message).toEqual(expected.message);
350
+ },
351
+ );
352
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/tsconfig.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["ES2023"],
5
+ "module": "NodeNext",
6
+ "moduleResolution": "NodeNext",
7
+ "strict": true,
8
+ "noEmit": true,
9
+ "allowImportingTsExtensions": true,
10
+ "verbatimModuleSyntax": true,
11
+ "erasableSyntaxOnly": true,
12
+ "isolatedModules": true,
13
+ "skipLibCheck": true
14
+ },
15
+ "include": ["src", "tests", "vitest.config.ts"]
16
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/ported_implementation/vitest.config.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ // unit tests are colocated with the source, integration tests live under tests/generated
6
+ include: ['src/**/*.test.ts', 'tests/**/*.test.ts'],
7
+ },
8
+ });
v2-models-effort/runs/20261111-214430-weathered-fire/proxy.log ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICE Nov 12 09:11:23.129 [8]: Initializing tinyproxy ...
2
+ NOTICE Nov 12 09:11:23.129 [8]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
3
+ INFO Nov 12 09:11:23.129 [8]: Added address [127.0.0.1] to listen addresses.
4
+ INFO Nov 12 09:11:23.129 [8]: Adding Port [443] to the list allowed by CONNECT
5
+ NOTICE Nov 12 09:11:23.129 [8]: Reloading config file finished
6
+ INFO Nov 12 09:11:23.129 [8]: listen_sock called with addr = '127.0.0.1'
7
+ INFO Nov 12 09:11:23.129 [8]: trying to listen on host[127.0.0.1], family[2], socktype[1], proto[6]
8
+ INFO Nov 12 09:11:23.129 [8]: listening on fd [3]
9
+ INFO Nov 12 09:11:23.129 [8]: Not running as root, so not changing UID/GID.
10
+ INFO Nov 12 09:11:23.129 [8]: Setting the various signals.
11
+ INFO Nov 12 09:11:23.129 [8]: Starting main loop. Accepting connections.
12
+ botbox-proxy: listening
13
+ CONNECT Nov 12 09:11:23.963 [8]: Connect (file descriptor 4): 127.0.0.1
14
+ CONNECT Nov 12 09:11:23.963 [8]: Request (file descriptor 4): CONNECT api.anthropic.com:443 HTTP/1.1
15
+ NOTICE Nov 12 09:11:23.963 [8]: Proxying refused on filtered url "api.anthropic.com:443"
16
+ CONNECT Nov 12 10:54:46.916 [8]: Connect (file descriptor 4): 127.0.0.1
17
+ CONNECT Nov 12 10:54:46.916 [8]: Request (file descriptor 4): CONNECT registry.npmjs.org:443 HTTP/1.1
18
+ NOTICE Nov 12 10:54:46.916 [8]: Proxying refused on filtered url "registry.npmjs.org:443"
v2-models-effort/runs/20261111-214430-weathered-fire/run.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "python",
4
+ "model": "claude-fable-5-2",
5
+ "effort": "max"
6
+ },
7
+ "command": "uv run --project packages/gbnf_experiment run_gbnf_experiment --include-unit-tests 1 --include-source-integration-tests 1 --include-target-integration-tests 0 --output /home/duncan/work/code/research/ports/experiments/v2-models-effort/runs/20261111-214430-weathered-fire --source-language python --model claude-fable-5-2 --effort max --auth subscription --memory 24g",
8
+ "created": "2026-11-12T09:11:11Z",
9
+ "ended": "2026-11-12T11:06:35Z",
10
+ "exit_code": 143,
11
+ "error": null,
12
+ "ended_by": "stop",
13
+ "signal": 15,
14
+ "git": {
15
+ "commit": "8422439f49db061ed5b56e9b883ef244807898f8",
16
+ "dirty": true
17
+ },
18
+ "labnotes_version": "0.2.39.dev138",
19
+ "labnotes_command": "labnotes experiments v2-models-effort run --concurrency 1 --model=claude-fable-5-2 -- --auth subscription --memory 24g",
20
+ "args": [
21
+ "--auth",
22
+ "subscription",
23
+ "--memory",
24
+ "24g"
25
+ ],
26
+ "pid": 1404935,
27
+ "pid_created": 1794474671.46
28
+ }
v2-models-effort/runs/20261111-214430-weathered-fire/run.lock ADDED
File without changes
v2-models-effort/runs/20261111-214430-weathered-fire/stderr.log ADDED
File without changes
v2-models-effort/runs/20261111-214430-weathered-fire/stderr.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ [claude-code:unrecognized_model] {"model":"claude-fable-5-2","query_source":"sdk"}