Upload experiments v1, v2-models-effort, v2-tests-configuration as on disk 2026-11-11 (part 57)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/get-serialized-rule-key.test.ts +51 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/get-serialized-rule-key.ts +29 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/grammar-graph-types.test.ts +60 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/grammar-graph-types.ts +72 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-node.test.ts +54 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-node.ts +42 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-pointer.test.ts +227 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-pointer.ts +112 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph.test.ts +229 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph.ts +244 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/parse-state.test.ts +83 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/parse-state.ts +55 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/pointers.test.ts +34 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/pointers.ts +28 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/print.test.ts +125 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/print.ts +75 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/rule-ref.test.ts +29 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/rule-ref.ts +28 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/type-guards.test.ts +142 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/type-guards.ts +60 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-parser/build-rule-stack.test.ts +431 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-parser/build-rule-stack.ts +104 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/index.test.ts +37 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/index.ts +5 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/grammar-source.test.ts +18 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/grammar-source.ts +14 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/index.ts +2 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/is-word-char.test.ts +32 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/is-word-char.ts +3 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-char.test.ts +66 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-char.ts +50 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-name.test.ts +31 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-name.ts +23 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-space.test.ts +68 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-space.ts +22 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder-types.test.ts +58 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder-types.ts +102 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder.test.ts +1399 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder.ts +284 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/symbol-ids.test.ts +43 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/symbol-ids.ts +48 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/code-points.test.ts +32 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/code-points.ts +21 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/build-error-position.test.ts +97 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/build-error-position.ts +33 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/errors-types.ts +4 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/get-input-as-string.test.ts +17 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/get-input-as-string.ts +12 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/grammar-parse-error.test.ts +27 -0
- v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/grammar-parse-error.ts +22 -0
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/get-serialized-rule-key.test.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
| 2 |
+
import { KEY_TRANSLATION, getSerializedRuleKey } from './get-serialized-rule-key.js';
|
| 3 |
+
import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from './grammar-graph-types.js';
|
| 4 |
+
import { RuleRef } from './rule-ref.js';
|
| 5 |
+
import { isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from './type-guards.js';
|
| 6 |
+
|
| 7 |
+
vi.mock('./type-guards.js', () => ({
|
| 8 |
+
isRuleEnd: vi.fn(),
|
| 9 |
+
isRuleChar: vi.fn(),
|
| 10 |
+
isRuleCharExclude: vi.fn(),
|
| 11 |
+
isRuleRef: vi.fn(),
|
| 12 |
+
}));
|
| 13 |
+
|
| 14 |
+
beforeEach(() => {
|
| 15 |
+
for (const guard of [isRuleEnd, isRuleChar, isRuleCharExclude, isRuleRef]) {
|
| 16 |
+
vi.mocked(guard).mockReset().mockReturnValue(false);
|
| 17 |
+
}
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
describe('getSerializedRuleKey', () => {
|
| 21 |
+
it('returns type for end rules', () => {
|
| 22 |
+
vi.mocked(isRuleEnd).mockReturnValue(true);
|
| 23 |
+
const rule = new RuleEnd();
|
| 24 |
+
expect(getSerializedRuleKey(rule)).toBe(`${KEY_TRANSLATION.end}`);
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
it('returns type and value for character rules', () => {
|
| 28 |
+
vi.mocked(isRuleChar).mockReturnValue(true);
|
| 29 |
+
const rule = new RuleChar([97]);
|
| 30 |
+
expect(getSerializedRuleKey(rule)).toBe(`${KEY_TRANSLATION.char}-[97]`);
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
it('returns type and value for character exclude rules', () => {
|
| 34 |
+
vi.mocked(isRuleCharExclude).mockReturnValue(true);
|
| 35 |
+
const rule = new RuleCharExclude([97]);
|
| 36 |
+
expect(getSerializedRuleKey(rule)).toBe(`${KEY_TRANSLATION.char_exclude}-[97]`);
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
it('returns ref type with value for reference rules', () => {
|
| 40 |
+
vi.mocked(isRuleRef).mockReturnValue(true);
|
| 41 |
+
const rule = new RuleRef(99);
|
| 42 |
+
expect(getSerializedRuleKey(rule)).toBe('3-99');
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
it('throws an error for unknown rule types', () => {
|
| 46 |
+
const rule = { type: 'UNKNOWN', value: 'something' } as unknown as UnresolvedRule;
|
| 47 |
+
expect(() => getSerializedRuleKey(rule)).toThrow(
|
| 48 |
+
'Unknown rule type: {"type":"UNKNOWN","value":"something"}',
|
| 49 |
+
);
|
| 50 |
+
});
|
| 51 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/get-serialized-rule-key.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { UnresolvedRule } from './grammar-graph-types.js';
|
| 2 |
+
import { isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from './type-guards.js';
|
| 3 |
+
|
| 4 |
+
export const KEY_TRANSLATION = {
|
| 5 |
+
end: 0,
|
| 6 |
+
char: 1,
|
| 7 |
+
char_exclude: 2,
|
| 8 |
+
} as const;
|
| 9 |
+
|
| 10 |
+
/** Two rules that mean the same thing serialize to the same key. */
|
| 11 |
+
export function getSerializedRuleKey(rule: UnresolvedRule): string {
|
| 12 |
+
if (isRuleEnd(rule)) {
|
| 13 |
+
return `${KEY_TRANSLATION.end}`;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
if (isRuleChar(rule)) {
|
| 17 |
+
return `${KEY_TRANSLATION.char}-${JSON.stringify(rule.value)}`;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
if (isRuleCharExclude(rule)) {
|
| 21 |
+
return `${KEY_TRANSLATION.char_exclude}-${JSON.stringify(rule.value)}`;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if (isRuleRef(rule)) {
|
| 25 |
+
return `3-${rule.value}`;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
throw new Error(`Unknown rule type: ${JSON.stringify(rule)}`);
|
| 29 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/grammar-graph-types.test.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { RuleChar, RuleCharExclude, RuleEnd } from './grammar-graph-types.js';
|
| 3 |
+
|
| 4 |
+
describe('rules', () => {
|
| 5 |
+
it('describe themselves with a type and a value', () => {
|
| 6 |
+
expect(JSON.stringify(new RuleChar([97, [98, 99]]))).toBe(
|
| 7 |
+
'{"type":"char","value":[97,[98,99]]}',
|
| 8 |
+
);
|
| 9 |
+
expect(JSON.stringify(new RuleCharExclude([97]))).toBe('{"type":"char_exclude","value":[97]}');
|
| 10 |
+
expect(JSON.stringify(new RuleEnd())).toBe('{"type":"end"}');
|
| 11 |
+
});
|
| 12 |
+
|
| 13 |
+
it('are equal to plain objects that describe the same rule', () => {
|
| 14 |
+
expect(new RuleChar([97])).toEqual({ type: 'char', value: [97] });
|
| 15 |
+
expect(new RuleCharExclude([[97, 99]])).toEqual({ type: 'char_exclude', value: [[97, 99]] });
|
| 16 |
+
expect(new RuleEnd()).toEqual({ type: 'end' });
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
it('are only equal to rules of the same kind that have the same value', () => {
|
| 20 |
+
expect(new RuleChar([97])).toStrictEqual(new RuleChar([97]));
|
| 21 |
+
expect(new RuleChar([97])).not.toStrictEqual(new RuleChar([98]));
|
| 22 |
+
expect(new RuleChar([97])).not.toStrictEqual(new RuleCharExclude([97]));
|
| 23 |
+
expect(new RuleChar([97])).not.toEqual(new RuleCharExclude([97]));
|
| 24 |
+
expect(new RuleEnd()).not.toEqual(new RuleChar([]));
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
it('copy the value they are given', () => {
|
| 28 |
+
const value = [97];
|
| 29 |
+
const rule = new RuleChar(value);
|
| 30 |
+
value.push(98);
|
| 31 |
+
expect(rule.value).toEqual([97]);
|
| 32 |
+
const excluded = new RuleCharExclude(value);
|
| 33 |
+
expect(excluded.value).not.toBe(value);
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
it('have no value by default', () => {
|
| 37 |
+
expect(new RuleChar().value).toEqual([]);
|
| 38 |
+
expect(new RuleCharExclude().value).toEqual([]);
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
it('are instances of their class', () => {
|
| 42 |
+
expect(new RuleChar([97])).toBeInstanceOf(RuleChar);
|
| 43 |
+
expect(new RuleChar([97])).not.toBeInstanceOf(RuleCharExclude);
|
| 44 |
+
expect(new RuleEnd()).toBeInstanceOf(RuleEnd);
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
it('describe themselves as text', () => {
|
| 48 |
+
expect(`${new RuleChar([97, [98, 99]])}`).toBe('RuleChar(value=[97,[98,99]])');
|
| 49 |
+
expect(`${new RuleCharExclude([97])}`).toBe('RuleCharExclude(value=[97])');
|
| 50 |
+
expect(`${new RuleEnd()}`).toBe('RuleEnd()');
|
| 51 |
+
});
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
describe('the module', () => {
|
| 55 |
+
it('also makes the rule refs available, which are not exposed to end users', async () => {
|
| 56 |
+
const { RuleRef } = await import('./grammar-graph-types.js');
|
| 57 |
+
const { RuleRef: original } = await import('./rule-ref.js');
|
| 58 |
+
expect(RuleRef).toBe(original);
|
| 59 |
+
});
|
| 60 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/grammar-graph-types.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { ValidInput } from '../utils/errors/errors-types.js';
|
| 2 |
+
import type { GraphPointer } from './graph-pointer.js';
|
| 3 |
+
import { RuleRef } from './rule-ref.js';
|
| 4 |
+
|
| 5 |
+
export { RuleRef };
|
| 6 |
+
export type { ValidInput };
|
| 7 |
+
|
| 8 |
+
export type Range = [number, number];
|
| 9 |
+
|
| 10 |
+
export type Colorize = (text: string | number, color: string) => string;
|
| 11 |
+
|
| 12 |
+
export interface PrintOpts {
|
| 13 |
+
pointers?: Iterable<GraphPointer>;
|
| 14 |
+
colorize: Colorize;
|
| 15 |
+
showPosition?: boolean;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
/** The values of a character rule: code points, and inclusive `[lower, upper]` ranges. */
|
| 19 |
+
export type RuleCharValue = Array<number | Range>;
|
| 20 |
+
|
| 21 |
+
export abstract class Rule<Type extends string = string> {
|
| 22 |
+
readonly type: Type;
|
| 23 |
+
|
| 24 |
+
protected constructor(type: Type) {
|
| 25 |
+
this.type = type;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
toString(): string {
|
| 29 |
+
return `${this.constructor.name}()`;
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
export abstract class RuleWithValue<Type extends string, Value> extends Rule<Type> {
|
| 34 |
+
value: Value;
|
| 35 |
+
|
| 36 |
+
protected constructor(type: Type, value: Value) {
|
| 37 |
+
super(type);
|
| 38 |
+
this.value = value;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
override toString(): string {
|
| 42 |
+
return `${this.constructor.name}(value=${JSON.stringify(this.value)})`;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/** Matches any one of its code points or ranges. */
|
| 47 |
+
export class RuleChar extends RuleWithValue<'char', RuleCharValue> {
|
| 48 |
+
constructor(value: RuleCharValue = []) {
|
| 49 |
+
super('char', [...value]);
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/** Matches any code point that isn't one of its code points or ranges. */
|
| 54 |
+
export class RuleCharExclude extends RuleWithValue<'char_exclude', RuleCharValue> {
|
| 55 |
+
constructor(value: RuleCharValue = []) {
|
| 56 |
+
super('char_exclude', [...value]);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
/** Marks that the input so far is complete. */
|
| 61 |
+
export class RuleEnd extends Rule<'end'> {
|
| 62 |
+
constructor() {
|
| 63 |
+
super('end');
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
export type UnresolvedRule = RuleChar | RuleCharExclude | RuleRef | RuleEnd;
|
| 68 |
+
|
| 69 |
+
// RuleRefs should never be exposed to the end user.
|
| 70 |
+
export type ResolvedRule = RuleCharExclude | RuleChar | RuleEnd;
|
| 71 |
+
|
| 72 |
+
export type ResolvedGraphPointer = GraphPointer<ResolvedRule>;
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-node.test.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
| 2 |
+
import type { PrintOpts } from './grammar-graph-types.js';
|
| 3 |
+
import { GraphNode, type GraphNodeMeta } from './graph-node.js';
|
| 4 |
+
import { printGraphNode } from './print.js';
|
| 5 |
+
import { RuleRef } from './rule-ref.js';
|
| 6 |
+
|
| 7 |
+
vi.mock('./print.js', () => ({
|
| 8 |
+
printGraphNode: vi.fn(),
|
| 9 |
+
}));
|
| 10 |
+
|
| 11 |
+
beforeEach(() => {
|
| 12 |
+
vi.mocked(printGraphNode).mockReset().mockReturnValue(() => 'mocked_response');
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
describe('GraphNode', () => {
|
| 16 |
+
const meta: GraphNodeMeta = { stackId: 1, pathId: 2, stepId: 3 };
|
| 17 |
+
const rule = new RuleRef(42);
|
| 18 |
+
|
| 19 |
+
it('constructs with rule and meta', () => {
|
| 20 |
+
const node = new GraphNode(rule, meta);
|
| 21 |
+
expect(node.rule).toBe(rule);
|
| 22 |
+
expect(node.meta).toEqual(meta);
|
| 23 |
+
expect(node.next).toBeUndefined();
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
it('throws if meta is undefined', () => {
|
| 27 |
+
expect(() => new GraphNode(rule, undefined)).toThrow('Meta is undefined');
|
| 28 |
+
expect(() => new GraphNode(rule, null)).toThrow('Meta is undefined');
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
it('correctly calculates and caches its id', () => {
|
| 32 |
+
const node = new GraphNode(rule, { ...meta });
|
| 33 |
+
expect(node.id).toBe('1,2,3');
|
| 34 |
+
node.meta = { stackId: 4, pathId: 5, stepId: 6 };
|
| 35 |
+
expect(node.id).toBe('1,2,3');
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
it('delegates print to the printGraphNode function', () => {
|
| 39 |
+
const node = new GraphNode(rule, meta);
|
| 40 |
+
const opts: PrintOpts = { colorize: (x) => String(x), showPosition: false };
|
| 41 |
+
expect(node.print(opts)).toBe('mocked_response');
|
| 42 |
+
expect(printGraphNode).toHaveBeenCalledWith(node);
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
it('handles next node linkage', () => {
|
| 46 |
+
const nextNode = new GraphNode(new RuleRef(43), meta);
|
| 47 |
+
const node = new GraphNode(rule, meta, nextNode);
|
| 48 |
+
expect(node.next).toBe(nextNode);
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
it('describes itself', () => {
|
| 52 |
+
expect(new GraphNode(rule, meta).toString()).toBe('<GraphNode 1,2,3 RuleRef(value=42)>');
|
| 53 |
+
});
|
| 54 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-node.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { PrintOpts, UnresolvedRule } from './grammar-graph-types.js';
|
| 2 |
+
import { printGraphNode } from './print.js';
|
| 3 |
+
import type { RuleRef } from './rule-ref.js';
|
| 4 |
+
|
| 5 |
+
export interface GraphNodeMeta {
|
| 6 |
+
stackId: number;
|
| 7 |
+
pathId: number;
|
| 8 |
+
stepId: number;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
export class GraphNode<T extends UnresolvedRule = UnresolvedRule> {
|
| 12 |
+
rule: T;
|
| 13 |
+
next: GraphNode | undefined;
|
| 14 |
+
meta: GraphNodeMeta;
|
| 15 |
+
#id: string | undefined;
|
| 16 |
+
|
| 17 |
+
constructor(rule: T, meta: GraphNodeMeta | null | undefined, next?: GraphNode) {
|
| 18 |
+
this.rule = rule;
|
| 19 |
+
if (meta === null || meta === undefined) {
|
| 20 |
+
throw new Error('Meta is undefined');
|
| 21 |
+
}
|
| 22 |
+
this.meta = meta;
|
| 23 |
+
this.next = next;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
get id(): string {
|
| 27 |
+
if (this.#id === undefined) {
|
| 28 |
+
this.#id = `${this.meta.stackId},${this.meta.pathId},${this.meta.stepId}`;
|
| 29 |
+
}
|
| 30 |
+
return this.#id;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
print(opts: PrintOpts): string {
|
| 34 |
+
return printGraphNode(this)(opts);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
toString(): string {
|
| 38 |
+
return `<GraphNode ${this.id} ${this.rule}>`;
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
export type GraphNodeRuleRef = GraphNode<RuleRef>;
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-pointer.test.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
| 2 |
+
import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from './grammar-graph-types.js';
|
| 3 |
+
import { GraphNode, type GraphNodeMeta } from './graph-node.js';
|
| 4 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 5 |
+
import { RuleRef } from './rule-ref.js';
|
| 6 |
+
import {
|
| 7 |
+
isGraphPointerRuleChar,
|
| 8 |
+
isGraphPointerRuleCharExclude,
|
| 9 |
+
isGraphPointerRuleEnd,
|
| 10 |
+
isGraphPointerRuleRef,
|
| 11 |
+
} from './type-guards.js';
|
| 12 |
+
|
| 13 |
+
vi.mock('./type-guards.js', async (importOriginal) => ({
|
| 14 |
+
...(await importOriginal<typeof import('./type-guards.js')>()),
|
| 15 |
+
isGraphPointerRuleRef: vi.fn(),
|
| 16 |
+
isGraphPointerRuleEnd: vi.fn(),
|
| 17 |
+
isGraphPointerRuleChar: vi.fn(),
|
| 18 |
+
isGraphPointerRuleCharExclude: vi.fn(),
|
| 19 |
+
}));
|
| 20 |
+
|
| 21 |
+
const guards = {
|
| 22 |
+
isGraphPointerRuleRef: vi.mocked(isGraphPointerRuleRef),
|
| 23 |
+
isGraphPointerRuleEnd: vi.mocked(isGraphPointerRuleEnd),
|
| 24 |
+
isGraphPointerRuleChar: vi.mocked(isGraphPointerRuleChar),
|
| 25 |
+
isGraphPointerRuleCharExclude: vi.mocked(isGraphPointerRuleCharExclude),
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
beforeEach(() => {
|
| 29 |
+
for (const guard of Object.values(guards)) {
|
| 30 |
+
guard.mockReset().mockReturnValue(false);
|
| 31 |
+
}
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
const meta = (stackId: number, pathId: number, stepId: number): GraphNodeMeta => ({
|
| 35 |
+
stackId,
|
| 36 |
+
pathId,
|
| 37 |
+
stepId,
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
describe('GraphPointer', () => {
|
| 41 |
+
it('constructs from a node', () => {
|
| 42 |
+
const node = new GraphNode(new RuleEnd(), meta(1, 2, 3));
|
| 43 |
+
const pointer = new GraphPointer(node);
|
| 44 |
+
expect(pointer.node).toBe(node);
|
| 45 |
+
expect(pointer.id).toBe('1,2,3');
|
| 46 |
+
expect(pointer.parent).toBeUndefined();
|
| 47 |
+
expect(pointer.valid).toBeUndefined();
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
it('raises an error if node is undefined', () => {
|
| 51 |
+
expect(() => new GraphPointer(undefined as unknown as GraphNode)).toThrow('Node is undefined');
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
it('initializes correctly with node and parent', () => {
|
| 55 |
+
const parentNode = new GraphNode(new RuleChar([97]), meta(1, 2, 3));
|
| 56 |
+
const childNode = new GraphNode(new RuleChar([98]), meta(4, 5, 6));
|
| 57 |
+
const parentPointer = new GraphPointer(parentNode);
|
| 58 |
+
const childPointer = new GraphPointer(childNode, parentPointer);
|
| 59 |
+
expect(childPointer.parent).toBe(parentPointer);
|
| 60 |
+
expect(childPointer.id).toBe('1,2,3-4,5,6');
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
it('exposes the rule of its node', () => {
|
| 64 |
+
const rule = new RuleChar([97]);
|
| 65 |
+
expect(new GraphPointer(new GraphNode(rule, meta(1, 2, 3))).rule).toBe(rule);
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
describe('resolve', () => {
|
| 69 |
+
it('raises an error on unknown rule types', () => {
|
| 70 |
+
const unknownRule = { type: 'UNKNOWN_RULE_TYPE' } as unknown as UnresolvedRule;
|
| 71 |
+
const node = new GraphNode(unknownRule, meta(1, 2, 3));
|
| 72 |
+
const pointer = new GraphPointer(node);
|
| 73 |
+
expect(() => [...pointer.resolve()]).toThrow('Unknown rule');
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
it('yields a char rule', () => {
|
| 77 |
+
guards.isGraphPointerRuleChar.mockReturnValue(true);
|
| 78 |
+
const node = new GraphNode(new RuleChar([97]), meta(1, 2, 3));
|
| 79 |
+
const pointer = new GraphPointer(node);
|
| 80 |
+
expect([...pointer.resolve()]).toEqual([pointer]);
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
it('yields a char excluded rule', () => {
|
| 84 |
+
guards.isGraphPointerRuleCharExclude.mockReturnValue(true);
|
| 85 |
+
const node = new GraphNode(new RuleCharExclude([97]), meta(1, 2, 3));
|
| 86 |
+
const pointer = new GraphPointer(node);
|
| 87 |
+
expect([...pointer.resolve()]).toEqual([pointer]);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
it('yields an end rule without a parent', () => {
|
| 91 |
+
guards.isGraphPointerRuleEnd.mockReturnValue(true);
|
| 92 |
+
const node = new GraphNode(new RuleEnd(), meta(1, 2, 3));
|
| 93 |
+
const pointer = new GraphPointer(node);
|
| 94 |
+
expect([...pointer.resolve()]).toEqual([pointer]);
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
it('yields an end rule with a parent', () => {
|
| 98 |
+
guards.isGraphPointerRuleEnd.mockReturnValue(true);
|
| 99 |
+
const rule = new RuleEnd();
|
| 100 |
+
const parentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
|
| 101 |
+
const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
|
| 102 |
+
const resolved = [...childPointer.resolve()];
|
| 103 |
+
expect(resolved).toHaveLength(1);
|
| 104 |
+
expect(resolved[0]).toBe(parentPointer);
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
it('yields an end rule with a grandparent', () => {
|
| 108 |
+
guards.isGraphPointerRuleEnd.mockReturnValue(true);
|
| 109 |
+
const rule = new RuleEnd();
|
| 110 |
+
const grandparentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
|
| 111 |
+
const parentNode = new GraphNode(rule, meta(2, 1, 1));
|
| 112 |
+
const parentPointer = new GraphPointer(parentNode, grandparentPointer);
|
| 113 |
+
const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
|
| 114 |
+
const resolved = [...childPointer.resolve()];
|
| 115 |
+
expect(resolved).toHaveLength(1);
|
| 116 |
+
expect(resolved[0]).toBe(grandparentPointer);
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
it('yields an end rule with a parent that is not an end with a grandparent', async () => {
|
| 120 |
+
const actual = await vi.importActual<typeof import('./type-guards.js')>('./type-guards.js');
|
| 121 |
+
guards.isGraphPointerRuleEnd.mockImplementation(actual.isGraphPointerRuleEnd);
|
| 122 |
+
guards.isGraphPointerRuleChar.mockImplementation(actual.isGraphPointerRuleChar);
|
| 123 |
+
const rule = new RuleEnd();
|
| 124 |
+
const grandparentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
|
| 125 |
+
const parentNode = new GraphNode(new RuleChar([97]), meta(2, 1, 1));
|
| 126 |
+
const parentPointer = new GraphPointer(parentNode, grandparentPointer);
|
| 127 |
+
const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
|
| 128 |
+
const resolved = [...childPointer.resolve()];
|
| 129 |
+
expect(resolved).toHaveLength(1);
|
| 130 |
+
expect(resolved[0]).toBe(parentPointer);
|
| 131 |
+
});
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
describe('with the real type guards', () => {
|
| 135 |
+
beforeEach(async () => {
|
| 136 |
+
const actual = await vi.importActual<typeof import('./type-guards.js')>('./type-guards.js');
|
| 137 |
+
guards.isGraphPointerRuleRef.mockImplementation(actual.isGraphPointerRuleRef);
|
| 138 |
+
guards.isGraphPointerRuleEnd.mockImplementation(actual.isGraphPointerRuleEnd);
|
| 139 |
+
guards.isGraphPointerRuleChar.mockImplementation(actual.isGraphPointerRuleChar);
|
| 140 |
+
guards.isGraphPointerRuleCharExclude.mockImplementation(actual.isGraphPointerRuleCharExclude);
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
// a path through `a`, `b`, and the end
|
| 144 |
+
const buildPath = (): GraphNode[] => {
|
| 145 |
+
const end = new GraphNode(new RuleEnd(), meta(0, 0, 2));
|
| 146 |
+
const b = new GraphNode(new RuleChar([98]), meta(0, 0, 1), end);
|
| 147 |
+
const a = new GraphNode(new RuleChar([97]), meta(0, 0, 0), b);
|
| 148 |
+
return [a, b, end];
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
it('resolves a reference to the nodes it references', () => {
|
| 152 |
+
const [a, b] = buildPath();
|
| 153 |
+
const ref = new RuleRef(1);
|
| 154 |
+
ref.nodes = new Set([a, b]);
|
| 155 |
+
const refPointer = new GraphPointer(new GraphNode(ref, meta(1, 0, 0)));
|
| 156 |
+
const resolved = [...refPointer.resolve()];
|
| 157 |
+
expect(resolved.map((pointer) => pointer.node)).toEqual([a, b]);
|
| 158 |
+
expect(resolved.map((pointer) => pointer.parent)).toEqual([refPointer, refPointer]);
|
| 159 |
+
});
|
| 160 |
+
|
| 161 |
+
it('resolves the end of a referenced rule to what comes after the reference', () => {
|
| 162 |
+
const [a, , end] = buildPath();
|
| 163 |
+
const afterRef = new GraphNode(new RuleChar([99]), meta(1, 0, 1));
|
| 164 |
+
const ref = new GraphNode(new RuleRef(0), meta(1, 0, 0), afterRef);
|
| 165 |
+
const refPointer = new GraphPointer(ref);
|
| 166 |
+
const endPointer = new GraphPointer(end, refPointer);
|
| 167 |
+
const resolved = [...endPointer.resolve()];
|
| 168 |
+
expect(resolved).toHaveLength(1);
|
| 169 |
+
expect(resolved[0].node).toBe(afterRef);
|
| 170 |
+
expect(a).toBeDefined();
|
| 171 |
+
});
|
| 172 |
+
|
| 173 |
+
it('raises if resolving a reference that is the end of its path', () => {
|
| 174 |
+
const ref = new GraphNode(new RuleRef(0), meta(1, 0, 0));
|
| 175 |
+
const refPointer = new GraphPointer(ref);
|
| 176 |
+
expect(() => [...refPointer.resolve(true)]).toThrow('No next node');
|
| 177 |
+
});
|
| 178 |
+
|
| 179 |
+
describe('fetchNext', () => {
|
| 180 |
+
it('yields nothing when the pointer is not valid', () => {
|
| 181 |
+
const [a] = buildPath();
|
| 182 |
+
const pointer = new GraphPointer(a);
|
| 183 |
+
expect([...pointer.fetchNext()]).toEqual([]);
|
| 184 |
+
pointer.valid = false;
|
| 185 |
+
expect([...pointer.fetchNext()]).toEqual([]);
|
| 186 |
+
});
|
| 187 |
+
|
| 188 |
+
it('yields the pointer for the next node when the pointer is valid', () => {
|
| 189 |
+
const [a, b] = buildPath();
|
| 190 |
+
const pointer = new GraphPointer(a);
|
| 191 |
+
pointer.valid = true;
|
| 192 |
+
const next = [...pointer.fetchNext()];
|
| 193 |
+
expect(next).toHaveLength(1);
|
| 194 |
+
expect(next[0].node).toBe(b);
|
| 195 |
+
expect(next[0].parent).toBeUndefined();
|
| 196 |
+
});
|
| 197 |
+
|
| 198 |
+
it('keeps the parent of the pointer', () => {
|
| 199 |
+
const [a, b] = buildPath();
|
| 200 |
+
const parent = new GraphPointer(new GraphNode(new RuleRef(0), meta(1, 0, 0)));
|
| 201 |
+
const pointer = new GraphPointer(a, parent);
|
| 202 |
+
pointer.valid = true;
|
| 203 |
+
const next = [...pointer.fetchNext()];
|
| 204 |
+
expect(next[0].node).toBe(b);
|
| 205 |
+
expect(next[0].parent).toBe(parent);
|
| 206 |
+
});
|
| 207 |
+
|
| 208 |
+
it('yields nothing at the end of the grammar', () => {
|
| 209 |
+
const [, , end] = buildPath();
|
| 210 |
+
const pointer = new GraphPointer(end);
|
| 211 |
+
pointer.valid = true;
|
| 212 |
+
expect([...pointer.fetchNext()]).toEqual([]);
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
it('raises if there is no next node', () => {
|
| 216 |
+
const pointer = new GraphPointer(new GraphNode(new RuleChar([97]), meta(0, 0, 0)));
|
| 217 |
+
pointer.valid = true;
|
| 218 |
+
expect(() => [...pointer.fetchNext()]).toThrow('No next node');
|
| 219 |
+
});
|
| 220 |
+
});
|
| 221 |
+
});
|
| 222 |
+
|
| 223 |
+
it('describes itself', () => {
|
| 224 |
+
const pointer = new GraphPointer(new GraphNode(new RuleChar([97]), meta(1, 2, 3)));
|
| 225 |
+
expect(pointer.toString()).toBe('<GraphPointer 1,2,3 <GraphNode 1,2,3 RuleChar(value=[97])>>');
|
| 226 |
+
});
|
| 227 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph-pointer.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { PrintOpts, UnresolvedRule } from './grammar-graph-types.js';
|
| 2 |
+
import type { GraphNode } from './graph-node.js';
|
| 3 |
+
import { printGraphPointer } from './print.js';
|
| 4 |
+
import type { RuleRef } from './rule-ref.js';
|
| 5 |
+
import {
|
| 6 |
+
isGraphPointerRuleChar,
|
| 7 |
+
isGraphPointerRuleCharExclude,
|
| 8 |
+
isGraphPointerRuleEnd,
|
| 9 |
+
isGraphPointerRuleRef,
|
| 10 |
+
isRuleEnd,
|
| 11 |
+
} from './type-guards.js';
|
| 12 |
+
|
| 13 |
+
export type GraphPointerKey = string;
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* A position in the graph: a node, along with the chain of rule references that were followed to
|
| 17 |
+
* get to it (its parents), so that the end of a rule can lead back to where the rule was used.
|
| 18 |
+
*/
|
| 19 |
+
export class GraphPointer<T extends UnresolvedRule = UnresolvedRule> {
|
| 20 |
+
node: GraphNode<T>;
|
| 21 |
+
parent: GraphPointer | undefined;
|
| 22 |
+
id: string;
|
| 23 |
+
/** Whether the code point that was most recently parsed was accepted at this pointer. */
|
| 24 |
+
valid: boolean | undefined;
|
| 25 |
+
|
| 26 |
+
constructor(node: GraphNode<T>, parent?: GraphPointer) {
|
| 27 |
+
if (node === null || node === undefined) {
|
| 28 |
+
throw new Error('Node is undefined');
|
| 29 |
+
}
|
| 30 |
+
this.node = node;
|
| 31 |
+
this.parent = parent;
|
| 32 |
+
this.id = parent ? `${parent.id}-${node.id}` : node.id;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
get rule(): T {
|
| 36 |
+
return this.node.rule;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
print(opts: PrintOpts): string {
|
| 40 |
+
return printGraphPointer(this)(opts);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/**
|
| 44 |
+
* Resolve the graph pointer: references are followed, and the ends of rules are followed back
|
| 45 |
+
* to the rule that referenced them, until pointers to characters (or to the end of the grammar)
|
| 46 |
+
* are found.
|
| 47 |
+
*
|
| 48 |
+
* @param resolved whether the pointer is a reference that has already been followed
|
| 49 |
+
*/
|
| 50 |
+
*resolve(resolved = false): Generator<GraphPointer, void, undefined> {
|
| 51 |
+
// Following an end back to the rule that referenced it, and carrying on from there, can be
|
| 52 |
+
// done many times over: a rule that repeats refers to itself once for each repetition. These
|
| 53 |
+
// are the last thing that is done, so they are a loop rather than a recursion, which would
|
| 54 |
+
// run out of stack for input that repeats a lot.
|
| 55 |
+
let pointer: GraphPointer = this;
|
| 56 |
+
let isResolved = resolved;
|
| 57 |
+
for (;;) {
|
| 58 |
+
if (isGraphPointerRuleRef(pointer)) {
|
| 59 |
+
if (isResolved) {
|
| 60 |
+
if (!pointer.node.next) {
|
| 61 |
+
throw new Error(`No next node: ${pointer.node}`);
|
| 62 |
+
}
|
| 63 |
+
pointer = new GraphPointer(pointer.node.next, pointer.parent);
|
| 64 |
+
isResolved = false;
|
| 65 |
+
} else {
|
| 66 |
+
for (const node of (pointer.node.rule as RuleRef).nodes) {
|
| 67 |
+
yield* new GraphPointer(node, pointer).resolve();
|
| 68 |
+
}
|
| 69 |
+
return;
|
| 70 |
+
}
|
| 71 |
+
} else if (isGraphPointerRuleEnd(pointer)) {
|
| 72 |
+
if (!pointer.parent) {
|
| 73 |
+
yield pointer;
|
| 74 |
+
return;
|
| 75 |
+
}
|
| 76 |
+
pointer = pointer.parent;
|
| 77 |
+
isResolved = true;
|
| 78 |
+
} else if (isGraphPointerRuleChar(pointer) || isGraphPointerRuleCharExclude(pointer)) {
|
| 79 |
+
yield pointer;
|
| 80 |
+
return;
|
| 81 |
+
} else {
|
| 82 |
+
throw new Error(`Unknown rule: ${JSON.stringify(pointer.node.rule)}`);
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/** The resolved pointers that follow this one, provided this one was valid for the last input. */
|
| 88 |
+
*fetchNext(): Generator<GraphPointer, void, undefined> {
|
| 89 |
+
// If this pointer is invalid, then we don't return any new pointers
|
| 90 |
+
if (!this.valid) {
|
| 91 |
+
return;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
// If this pointer is an end node, we return the parent's next node.
|
| 95 |
+
// If no parent exists, we return nothing, since it's the end of the line.
|
| 96 |
+
if (isRuleEnd(this.node.rule)) {
|
| 97 |
+
if (this.parent) {
|
| 98 |
+
yield* this.parent.fetchNext();
|
| 99 |
+
}
|
| 100 |
+
} else {
|
| 101 |
+
if (!this.node.next) {
|
| 102 |
+
throw new Error(`No next node: ${this.node}`);
|
| 103 |
+
}
|
| 104 |
+
const pointer = new GraphPointer(this.node.next, this.parent);
|
| 105 |
+
yield* pointer.resolve();
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
toString(): string {
|
| 110 |
+
return `<GraphPointer ${this.id} ${this.node}>`;
|
| 111 |
+
}
|
| 112 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph.test.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { InputParseError } from '../utils/errors/index.js';
|
| 3 |
+
import {
|
| 4 |
+
RuleChar,
|
| 5 |
+
RuleCharExclude,
|
| 6 |
+
RuleEnd,
|
| 7 |
+
type UnresolvedRule,
|
| 8 |
+
} from './grammar-graph-types.js';
|
| 9 |
+
import { Graph } from './graph.js';
|
| 10 |
+
import { GraphNode } from './graph-node.js';
|
| 11 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 12 |
+
import { Pointers } from './pointers.js';
|
| 13 |
+
import { RuleRef } from './rule-ref.js';
|
| 14 |
+
|
| 15 |
+
describe('Graph', () => {
|
| 16 |
+
const grammar = 'example-grammar';
|
| 17 |
+
const stackedRules: UnresolvedRule[][][] = [
|
| 18 |
+
[[new RuleChar([65, 66, 67])], [new RuleChar([68, 69, 70])]],
|
| 19 |
+
[[new RuleChar([71, 72, 73])], [new RuleChar([74, 75, 76])]],
|
| 20 |
+
];
|
| 21 |
+
const rootId = 0;
|
| 22 |
+
|
| 23 |
+
// the graph for `root ::= "a" | "b"`
|
| 24 |
+
const buildAOrB = (): Graph =>
|
| 25 |
+
new Graph(
|
| 26 |
+
'root ::= "a" | "b"',
|
| 27 |
+
[[[new RuleChar([97]), new RuleEnd()], [new RuleChar([98]), new RuleEnd()]]],
|
| 28 |
+
0,
|
| 29 |
+
);
|
| 30 |
+
|
| 31 |
+
it('should create a graph instance', () => {
|
| 32 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 33 |
+
expect(graph).toBeInstanceOf(Graph);
|
| 34 |
+
expect(graph.grammar).toBe(grammar);
|
| 35 |
+
});
|
| 36 |
+
|
| 37 |
+
it('should get the root node', () => {
|
| 38 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 39 |
+
const rootNode = graph['getRootNode'](rootId);
|
| 40 |
+
expect(rootNode).toBeInstanceOf(Map);
|
| 41 |
+
expect(rootNode.size).toBe(2);
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
it('should raise if asked for a root node that does not exist', () => {
|
| 45 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 46 |
+
expect(() => graph['getRootNode'](5)).toThrow('Root node not found for value: 5');
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
it('should raise if the root does not exist', () => {
|
| 50 |
+
expect(() => new Graph(grammar, stackedRules, 5)).toThrow('Root node not found for value: 5');
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
it('should raise if a rule has no paths', () => {
|
| 54 |
+
expect(() => new Graph(grammar, [[[]]], 0)).toThrow('Could not get node');
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
it('should link each node to the next node of its path', () => {
|
| 58 |
+
const graph = buildAOrB();
|
| 59 |
+
const [first, second] = [...graph['getRootNode'](0).values()];
|
| 60 |
+
expect(first.rule).toEqual(new RuleChar([97]));
|
| 61 |
+
expect(first.next?.rule).toEqual(new RuleEnd());
|
| 62 |
+
expect(first.next?.next).toBeUndefined();
|
| 63 |
+
expect(second.rule).toEqual(new RuleChar([98]));
|
| 64 |
+
expect(first.id).toBe('0,0,0');
|
| 65 |
+
expect(second.id).toBe('0,1,0');
|
| 66 |
+
expect(second.next?.id).toBe('0,1,1');
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
it('should give the rule refs the nodes they refer to', () => {
|
| 70 |
+
const ref = new RuleRef(1);
|
| 71 |
+
const graph = new Graph(
|
| 72 |
+
'root ::= foo\nfoo ::= "a" | "b"',
|
| 73 |
+
[
|
| 74 |
+
[[ref, new RuleEnd()]],
|
| 75 |
+
[[new RuleChar([97]), new RuleEnd()], [new RuleChar([98]), new RuleEnd()]],
|
| 76 |
+
],
|
| 77 |
+
0,
|
| 78 |
+
);
|
| 79 |
+
expect([...ref.nodes]).toEqual([...graph['getRootNode'](1).values()]);
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
it('should print the graph', () => {
|
| 83 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 84 |
+
const printedGraph = graph.print(undefined, true);
|
| 85 |
+
expect(typeof printedGraph).toBe('string');
|
| 86 |
+
expect(printedGraph.length).toBeGreaterThan(0);
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
it('should print each of the paths of the graph on its own line', () => {
|
| 90 |
+
const graph = buildAOrB();
|
| 91 |
+
expect(graph.print()).toBe(
|
| 92 |
+
['{0,0,0}[a]-> {0,0,1}end', '{0,1,0}[b]-> {0,1,1}end'].join('\n'),
|
| 93 |
+
);
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
it('should print where the pointers are', () => {
|
| 97 |
+
const graph = buildAOrB();
|
| 98 |
+
const pointers = graph['getInitialPointers']();
|
| 99 |
+
expect(graph.print(pointers)).toBe(
|
| 100 |
+
['{0,0,0}[a][*]-> {0,0,1}end', '{0,1,0}[b][*]-> {0,1,1}end'].join('\n'),
|
| 101 |
+
);
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
it('should print the graph with colors', () => {
|
| 105 |
+
const graph = buildAOrB();
|
| 106 |
+
expect(graph.print(undefined, true)).toContain('\x1b[');
|
| 107 |
+
expect(graph.print()).not.toContain('\x1b[');
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
it('should get the initial pointers', () => {
|
| 111 |
+
const graph = buildAOrB();
|
| 112 |
+
const pointers = graph['getInitialPointers']();
|
| 113 |
+
expect([...pointers].map((pointer) => pointer.rule)).toEqual([
|
| 114 |
+
new RuleChar([97]),
|
| 115 |
+
new RuleChar([98]),
|
| 116 |
+
]);
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
it('should iterate over pointers', () => {
|
| 120 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 121 |
+
const rules = [
|
| 122 |
+
new RuleChar([65, 66, 67]),
|
| 123 |
+
new RuleChar([68, 69, 70]),
|
| 124 |
+
new RuleChar([71, 72, 73]),
|
| 125 |
+
new RuleChar([74, 75, 76]),
|
| 126 |
+
new RuleCharExclude([1]),
|
| 127 |
+
new RuleEnd(),
|
| 128 |
+
];
|
| 129 |
+
const mockPointers = new Pointers(
|
| 130 |
+
...rules.map(
|
| 131 |
+
(rule, i) => new GraphPointer(new GraphNode(rule, { stackId: i, pathId: 0, stepId: 0 })),
|
| 132 |
+
),
|
| 133 |
+
);
|
| 134 |
+
|
| 135 |
+
const result = [...graph['iterateOverPointers'](mockPointers)];
|
| 136 |
+
expect(result).toHaveLength(6);
|
| 137 |
+
for (const [rule, pointers] of result) {
|
| 138 |
+
expect(rules).toContain(rule);
|
| 139 |
+
expect(pointers.length).toBeGreaterThanOrEqual(1);
|
| 140 |
+
for (const pointer of pointers) {
|
| 141 |
+
expect([...mockPointers]).toContain(pointer);
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
it('should group the pointers that share a rule', () => {
|
| 147 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 148 |
+
const rule = new RuleChar([65]);
|
| 149 |
+
const pointers = [0, 1, 2].map(
|
| 150 |
+
(i) => new GraphPointer(new GraphNode(rule, { stackId: i, pathId: 0, stepId: 0 })),
|
| 151 |
+
);
|
| 152 |
+
const result = [...graph['iterateOverPointers'](pointers)];
|
| 153 |
+
expect(result).toEqual([[rule, pointers]]);
|
| 154 |
+
});
|
| 155 |
+
|
| 156 |
+
it('should raise an error on reference rule', () => {
|
| 157 |
+
const graph = new Graph(grammar, stackedRules, rootId);
|
| 158 |
+
const mockPointers = [
|
| 159 |
+
new GraphPointer(
|
| 160 |
+
new GraphNode(new RuleRef(0), { stackId: 0, pathId: 0, stepId: 0 }),
|
| 161 |
+
),
|
| 162 |
+
];
|
| 163 |
+
|
| 164 |
+
expect(() => [...graph['iterateOverPointers'](mockPointers)]).toThrow(
|
| 165 |
+
'Encountered a reference rule in the graph',
|
| 166 |
+
);
|
| 167 |
+
});
|
| 168 |
+
|
| 169 |
+
describe('add', () => {
|
| 170 |
+
it('should only accept strings', () => {
|
| 171 |
+
const graph = buildAOrB();
|
| 172 |
+
expect(() => graph.add(97)).toThrow('src must be a string in graph.add');
|
| 173 |
+
expect(() => graph.add([97])).toThrow('src must be a string in graph.add');
|
| 174 |
+
});
|
| 175 |
+
|
| 176 |
+
it('should return the initial pointers for an empty string', () => {
|
| 177 |
+
const graph = buildAOrB();
|
| 178 |
+
expect([...graph.add('')].map((pointer) => pointer.rule)).toEqual([
|
| 179 |
+
new RuleChar([97]),
|
| 180 |
+
new RuleChar([98]),
|
| 181 |
+
]);
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
it('should continue on from the given pointers', () => {
|
| 185 |
+
const graph = buildAOrB();
|
| 186 |
+
const pointers = graph.add('a');
|
| 187 |
+
expect([...pointers].map((pointer) => pointer.rule)).toEqual([new RuleEnd()]);
|
| 188 |
+
});
|
| 189 |
+
|
| 190 |
+
it('should start from the beginning if given no pointers to continue from', () => {
|
| 191 |
+
const graph = buildAOrB();
|
| 192 |
+
expect(graph.add('b', new Pointers()).size).toBe(1);
|
| 193 |
+
});
|
| 194 |
+
|
| 195 |
+
it('should raise an error when the input can not be parsed', () => {
|
| 196 |
+
const graph = buildAOrB();
|
| 197 |
+
expect(() => graph.add('ab')).toThrow(InputParseError);
|
| 198 |
+
});
|
| 199 |
+
|
| 200 |
+
it('should remember the input that was parsed, and report it when there is an error', () => {
|
| 201 |
+
const graph = buildAOrB();
|
| 202 |
+
const pointers = graph.add('a');
|
| 203 |
+
expect(graph.previousCodePoints).toEqual([97]);
|
| 204 |
+
let error: InputParseError | undefined;
|
| 205 |
+
try {
|
| 206 |
+
graph.add('b', pointers);
|
| 207 |
+
} catch (e) {
|
| 208 |
+
error = e as InputParseError;
|
| 209 |
+
}
|
| 210 |
+
expect(error).toBeInstanceOf(InputParseError);
|
| 211 |
+
expect(error?.pos).toBe(0);
|
| 212 |
+
expect(error?.mostRecentInput).toEqual([98]);
|
| 213 |
+
expect(error?.previousInput).toEqual([97]);
|
| 214 |
+
expect(error?.message).toBe(new InputParseError('b', 0, 'a').message);
|
| 215 |
+
// input that wasn't parsed isn't remembered
|
| 216 |
+
expect(graph.previousCodePoints).toEqual([97]);
|
| 217 |
+
});
|
| 218 |
+
|
| 219 |
+
it('should parse characters outside of the basic multilingual plane as one code point', () => {
|
| 220 |
+
const graph = new Graph(
|
| 221 |
+
'root ::= "😀"',
|
| 222 |
+
[[[new RuleChar([0x1f600]), new RuleEnd()]]],
|
| 223 |
+
0,
|
| 224 |
+
);
|
| 225 |
+
expect([...graph.add('😀')].map((pointer) => pointer.rule)).toEqual([new RuleEnd()]);
|
| 226 |
+
expect(graph.previousCodePoints).toEqual([0x1f600]);
|
| 227 |
+
});
|
| 228 |
+
});
|
| 229 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/graph.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { InputParseError } from '../utils/errors/index.js';
|
| 2 |
+
import { isPointInRange } from '../utils/is-point-in-range.js';
|
| 3 |
+
import { colorize } from './colorize.js';
|
| 4 |
+
import { getInputAsCodePoints } from './get-input-as-code-points.js';
|
| 5 |
+
import { getSerializedRuleKey } from './get-serialized-rule-key.js';
|
| 6 |
+
import type {
|
| 7 |
+
ResolvedGraphPointer,
|
| 8 |
+
RuleCharValue,
|
| 9 |
+
UnresolvedRule,
|
| 10 |
+
ValidInput,
|
| 11 |
+
} from './grammar-graph-types.js';
|
| 12 |
+
import { GraphNode } from './graph-node.js';
|
| 13 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 14 |
+
import { Pointers } from './pointers.js';
|
| 15 |
+
import type { RuleRef } from './rule-ref.js';
|
| 16 |
+
import { isRange, isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from './type-guards.js';
|
| 17 |
+
|
| 18 |
+
/** The first node of each path of a rule, by path id. */
|
| 19 |
+
export type RootNode = Map<number, GraphNode>;
|
| 20 |
+
|
| 21 |
+
/** Whether the code point is one of the code points, or in one of the ranges, of a rule. */
|
| 22 |
+
function matchesAny(value: RuleCharValue, codePoint: number): boolean {
|
| 23 |
+
return value.some((possibleCodePoint) =>
|
| 24 |
+
isRange(possibleCodePoint)
|
| 25 |
+
? isPointInRange(codePoint, possibleCodePoint)
|
| 26 |
+
: codePoint === possibleCodePoint,
|
| 27 |
+
);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
export class Graph {
|
| 31 |
+
grammar: string;
|
| 32 |
+
previousCodePoints: number[];
|
| 33 |
+
private roots: Map<number, RootNode>;
|
| 34 |
+
private rootNode: RootNode | undefined;
|
| 35 |
+
|
| 36 |
+
constructor(grammar: string, stackedRules: UnresolvedRule[][][], rootId: number) {
|
| 37 |
+
this.roots = new Map();
|
| 38 |
+
this.grammar = grammar;
|
| 39 |
+
this.previousCodePoints = [];
|
| 40 |
+
const ruleRefs: RuleRef[] = [];
|
| 41 |
+
const uniqueRules = new Map<string, UnresolvedRule>();
|
| 42 |
+
|
| 43 |
+
for (let stackId = 0; stackId < stackedRules.length; stackId++) {
|
| 44 |
+
const stack = stackedRules[stackId];
|
| 45 |
+
const nodes: RootNode = new Map();
|
| 46 |
+
for (let pathId = 0; pathId < stack.length; pathId++) {
|
| 47 |
+
const path = stack[pathId];
|
| 48 |
+
let node: GraphNode | undefined;
|
| 49 |
+
for (let stepId = path.length - 1; stepId >= 0; stepId--) {
|
| 50 |
+
const nextNode = node;
|
| 51 |
+
const rule = path[stepId];
|
| 52 |
+
if (isRuleRef(rule)) {
|
| 53 |
+
ruleRefs.push(rule);
|
| 54 |
+
}
|
| 55 |
+
// rules coming in may be identical but have different references.
|
| 56 |
+
// here, we ensure we always use the same reference for an identical rule.
|
| 57 |
+
// this makes future comparisons easier.
|
| 58 |
+
const key = getSerializedRuleKey(rule);
|
| 59 |
+
const uniqueRule = uniqueRules.get(key) ?? rule;
|
| 60 |
+
uniqueRules.set(key, uniqueRule);
|
| 61 |
+
node = new GraphNode(uniqueRule, { stackId, pathId, stepId }, nextNode);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
if (node === undefined) {
|
| 65 |
+
throw new Error('Could not get node');
|
| 66 |
+
}
|
| 67 |
+
nodes.set(pathId, node);
|
| 68 |
+
}
|
| 69 |
+
this.roots.set(stackId, nodes);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
const rootNode = this.roots.get(rootId);
|
| 73 |
+
if (rootNode === undefined) {
|
| 74 |
+
throw new Error(`Root node not found for value: ${rootId}`);
|
| 75 |
+
}
|
| 76 |
+
this.rootNode = rootNode;
|
| 77 |
+
|
| 78 |
+
for (const ruleRef of ruleRefs) {
|
| 79 |
+
ruleRef.nodes = new Set(this.getRootNode(ruleRef.value).values());
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
private getRootNode(value: number): RootNode {
|
| 84 |
+
const rootNode = this.roots.get(value);
|
| 85 |
+
if (rootNode === undefined) {
|
| 86 |
+
throw new Error(`Root node not found for value: ${value}`);
|
| 87 |
+
}
|
| 88 |
+
return rootNode;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
private getInitialPointers(): Pointers {
|
| 92 |
+
const pointers = new Pointers();
|
| 93 |
+
|
| 94 |
+
const rootNode = this.rootNode;
|
| 95 |
+
if (rootNode === undefined) {
|
| 96 |
+
throw new Error('Root node is not defined');
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
for (const [node, parent] of this.fetchNodesForRootNode(rootNode)) {
|
| 100 |
+
const pointer = new GraphPointer(node, parent);
|
| 101 |
+
for (const resolvedPointer of this.resolvePointer(pointer)) {
|
| 102 |
+
pointers.add(resolvedPointer);
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
return pointers;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
private setValid(pointers: GraphPointer[], valid: boolean): void {
|
| 109 |
+
for (const pointer of pointers) {
|
| 110 |
+
pointer.valid = valid;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
private parse(currentPointers: Pointers, codePoint: number): Pointers {
|
| 115 |
+
for (const [rule, graphPointers] of this.iterateOverPointers(currentPointers)) {
|
| 116 |
+
if (isRuleChar(rule)) {
|
| 117 |
+
this.setValid(graphPointers, matchesAny(rule.value, codePoint));
|
| 118 |
+
} else if (isRuleCharExclude(rule)) {
|
| 119 |
+
this.setValid(graphPointers, !matchesAny(rule.value, codePoint));
|
| 120 |
+
} else if (!isRuleEnd(rule)) {
|
| 121 |
+
throw new Error(`Unsupported rule: ${rule}`);
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// a pointer's id is the sum of its node's id and its parent's id chain.
|
| 126 |
+
// if two pointers share the same id, it means they point to the same node and have identical
|
| 127 |
+
// parent chains. for the purposes of walking the graph, we only need to keep one of them.
|
| 128 |
+
const nextPointers = new Pointers();
|
| 129 |
+
for (const currentPointer of currentPointers) {
|
| 130 |
+
for (const unresolvedNextPointer of currentPointer.fetchNext()) {
|
| 131 |
+
for (const resolvedNextPointer of this.resolvePointer(unresolvedNextPointer)) {
|
| 132 |
+
nextPointers.add(resolvedNextPointer);
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
return nextPointers;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
private *resolvePointer(
|
| 140 |
+
unresolvedPointer: GraphPointer,
|
| 141 |
+
): Generator<ResolvedGraphPointer, void, undefined> {
|
| 142 |
+
for (const resolvedPointer of unresolvedPointer.resolve()) {
|
| 143 |
+
if (isRuleRef(resolvedPointer.node.rule)) {
|
| 144 |
+
throw new Error('Encountered a reference rule when building pointers to the graph');
|
| 145 |
+
}
|
| 146 |
+
if (isRuleEnd(resolvedPointer.node.rule) && resolvedPointer.parent !== undefined) {
|
| 147 |
+
throw new Error(
|
| 148 |
+
'Encountered an ending rule with a parent when building pointers to the graph',
|
| 149 |
+
);
|
| 150 |
+
}
|
| 151 |
+
yield resolvedPointer as ResolvedGraphPointer;
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
/**
|
| 156 |
+
* Parses src, continuing on from the given pointers, or from the start of the grammar if there
|
| 157 |
+
* are none. Returns the pointers that are reached by the end of src.
|
| 158 |
+
*
|
| 159 |
+
* @throws {InputParseError} if src can't be parsed
|
| 160 |
+
*/
|
| 161 |
+
add(src: ValidInput, pointers?: Pointers): Pointers {
|
| 162 |
+
if (typeof src !== 'string') {
|
| 163 |
+
throw new Error('src must be a string in graph.add');
|
| 164 |
+
}
|
| 165 |
+
let currentPointers = pointers && pointers.size > 0 ? pointers : this.getInitialPointers();
|
| 166 |
+
|
| 167 |
+
const codePoints = getInputAsCodePoints(src);
|
| 168 |
+
for (const codePoint of codePoints) {
|
| 169 |
+
if (!Number.isInteger(codePoint)) {
|
| 170 |
+
throw new Error('code_point must be an integer!');
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
for (let codePointPos = 0; codePointPos < codePoints.length; codePointPos++) {
|
| 175 |
+
currentPointers = this.parse(currentPointers, codePoints[codePointPos]);
|
| 176 |
+
if (currentPointers.size === 0) {
|
| 177 |
+
throw new InputParseError(codePoints, codePointPos, [...this.previousCodePoints]);
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
for (const codePoint of codePoints) {
|
| 181 |
+
this.previousCodePoints.push(codePoint);
|
| 182 |
+
}
|
| 183 |
+
return currentPointers;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// generator that yields either the node, or if a reference rule, the referenced node
|
| 187 |
+
// we need these function, as distinct from leveraging the logic in GraphPointer,
|
| 188 |
+
// because that needs a rule ref with already defined nodes;
|
| 189 |
+
// this function is used to _set_ those nodes
|
| 190 |
+
private *fetchNodesForRootNode(
|
| 191 |
+
rootNodes: RootNode,
|
| 192 |
+
parent?: GraphPointer,
|
| 193 |
+
): Generator<[GraphNode, GraphPointer | undefined], void, undefined> {
|
| 194 |
+
for (const node of rootNodes.values()) {
|
| 195 |
+
if (isRuleRef(node.rule)) {
|
| 196 |
+
yield* this.fetchNodesForRootNode(
|
| 197 |
+
this.getRootNode(node.rule.value),
|
| 198 |
+
new GraphPointer(node, parent),
|
| 199 |
+
);
|
| 200 |
+
} else {
|
| 201 |
+
yield [node, parent];
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
print(pointers?: Pointers, colors = false): string {
|
| 207 |
+
const nodes: GraphNode[][] = [...this.roots.values()].map((rootNode) => [...rootNode.values()]);
|
| 208 |
+
const graphView: string[] = [];
|
| 209 |
+
for (const rootNode of nodes) {
|
| 210 |
+
for (const node of rootNode) {
|
| 211 |
+
graphView.push(
|
| 212 |
+
node.print({
|
| 213 |
+
pointers: pointers ?? new Pointers(),
|
| 214 |
+
showPosition: true,
|
| 215 |
+
colorize: colors ? colorize : (s) => String(s),
|
| 216 |
+
}),
|
| 217 |
+
);
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
return graphView.join('\n');
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
private *iterateOverPointers(
|
| 225 |
+
pointers: Iterable<GraphPointer>,
|
| 226 |
+
): Generator<[UnresolvedRule, GraphPointer[]], void, undefined> {
|
| 227 |
+
const seenRules = new Map<UnresolvedRule, GraphPointer[]>();
|
| 228 |
+
for (const pointer of pointers) {
|
| 229 |
+
const rule = pointer.rule;
|
| 230 |
+
if (isRuleRef(rule)) {
|
| 231 |
+
throw new Error('Encountered a reference rule in the graph');
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
let seenRule = seenRules.get(rule);
|
| 235 |
+
if (seenRule === undefined) {
|
| 236 |
+
seenRule = [];
|
| 237 |
+
seenRules.set(rule, seenRule);
|
| 238 |
+
}
|
| 239 |
+
seenRule.push(pointer);
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
yield* seenRules.entries();
|
| 243 |
+
}
|
| 244 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/parse-state.test.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it, vi } from 'vitest';
|
| 2 |
+
import { RuleChar } from './grammar-graph-types.js';
|
| 3 |
+
import type { Graph } from './graph.js';
|
| 4 |
+
import { GraphNode } from './graph-node.js';
|
| 5 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 6 |
+
import { ParseState } from './parse-state.js';
|
| 7 |
+
import { Pointers } from './pointers.js';
|
| 8 |
+
|
| 9 |
+
const makeMockGraph = () => {
|
| 10 |
+
const graph = {
|
| 11 |
+
add: vi.fn((_input: string, pointers: Pointers) => pointers),
|
| 12 |
+
grammar: 'sample-grammar',
|
| 13 |
+
};
|
| 14 |
+
return { graph, asGraph: graph as unknown as Graph };
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
const mockPointers = new Pointers(
|
| 18 |
+
new GraphPointer(new GraphNode(new RuleChar([65]), { stackId: 1, pathId: 1, stepId: 1 })),
|
| 19 |
+
);
|
| 20 |
+
|
| 21 |
+
describe('ParseState', () => {
|
| 22 |
+
it('constructs with given graph and pointers', () => {
|
| 23 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 24 |
+
expect(parseState).toBeInstanceOf(ParseState);
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
it('returns unique rules from pointers', () => {
|
| 28 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 29 |
+
const rules = [...parseState.rules()];
|
| 30 |
+
expect(rules).toHaveLength(1);
|
| 31 |
+
expect(rules[0]).toBeInstanceOf(RuleChar);
|
| 32 |
+
expect(rules[0]).toEqual(new RuleChar([65]));
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
it('only returns a rule once, however many pointers there are to it', () => {
|
| 36 |
+
const pointers = new Pointers(
|
| 37 |
+
new GraphPointer(new GraphNode(new RuleChar([65]), { stackId: 1, pathId: 1, stepId: 1 })),
|
| 38 |
+
new GraphPointer(new GraphNode(new RuleChar([66]), { stackId: 1, pathId: 2, stepId: 1 })),
|
| 39 |
+
new GraphPointer(new GraphNode(new RuleChar([65]), { stackId: 2, pathId: 1, stepId: 1 })),
|
| 40 |
+
);
|
| 41 |
+
const parseState = new ParseState(makeMockGraph().asGraph, pointers);
|
| 42 |
+
expect([...parseState.rules()]).toEqual([new RuleChar([65]), new RuleChar([66])]);
|
| 43 |
+
expect(parseState.size).toBe(2);
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
it('iterates over unique rules using the iterator protocol', () => {
|
| 47 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 48 |
+
const rules = [...parseState];
|
| 49 |
+
expect(rules).toHaveLength(1);
|
| 50 |
+
expect(rules[0]).toBeInstanceOf(RuleChar);
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
it('adds new input and returns a new parse state with updated pointers', () => {
|
| 54 |
+
const { graph, asGraph } = makeMockGraph();
|
| 55 |
+
const newPointers = new Pointers();
|
| 56 |
+
graph.add.mockReturnValueOnce(newPointers);
|
| 57 |
+
|
| 58 |
+
const parseState = new ParseState(asGraph, mockPointers);
|
| 59 |
+
const newState = parseState.add('B');
|
| 60 |
+
expect(newState).toBeInstanceOf(ParseState);
|
| 61 |
+
expect(newState).not.toBe(parseState);
|
| 62 |
+
expect(graph.add).toHaveBeenCalledWith('B', mockPointers);
|
| 63 |
+
expect([...newState]).toEqual([]);
|
| 64 |
+
expect([...parseState]).toHaveLength(1);
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
it('only accepts strings as new input', () => {
|
| 68 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 69 |
+
expect(() => parseState.add(1 as unknown as string)).toThrow(
|
| 70 |
+
'input text must be of type string',
|
| 71 |
+
);
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
it('calculates the size of unique rules correctly', () => {
|
| 75 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 76 |
+
expect(parseState.size).toBe(1);
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
it('provides access to the underlying graph grammar', () => {
|
| 80 |
+
const parseState = new ParseState(makeMockGraph().asGraph, mockPointers);
|
| 81 |
+
expect(parseState.grammar).toBe('sample-grammar');
|
| 82 |
+
});
|
| 83 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/parse-state.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { ResolvedRule } from './grammar-graph-types.js';
|
| 2 |
+
import type { Graph } from './graph.js';
|
| 3 |
+
import type { Pointers } from './pointers.js';
|
| 4 |
+
|
| 5 |
+
/**
|
| 6 |
+
* The state of a grammar having parsed some input: the rules that may come next.
|
| 7 |
+
* States are immutable; adding input to a state returns a new state.
|
| 8 |
+
*/
|
| 9 |
+
export class ParseState implements Iterable<ResolvedRule> {
|
| 10 |
+
private readonly graph: Graph;
|
| 11 |
+
private readonly pointers: Pointers;
|
| 12 |
+
|
| 13 |
+
constructor(graph: Graph, pointers: Pointers) {
|
| 14 |
+
this.graph = graph;
|
| 15 |
+
this.pointers = pointers;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
[Symbol.iterator](): Generator<ResolvedRule, void, undefined> {
|
| 19 |
+
return this.rules();
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/** The unique rules that may come next, in the order in which they were found. */
|
| 23 |
+
*rules(): Generator<ResolvedRule, void, undefined> {
|
| 24 |
+
const seen = new Set<string>();
|
| 25 |
+
for (const pointer of this.pointers) {
|
| 26 |
+
const rule = pointer.rule as ResolvedRule;
|
| 27 |
+
const key = JSON.stringify(rule);
|
| 28 |
+
if (!seen.has(key)) {
|
| 29 |
+
seen.add(key);
|
| 30 |
+
yield rule;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/**
|
| 36 |
+
* @returns the state that results from parsing text from this state
|
| 37 |
+
* @throws {InputParseError} if text can't be parsed from this state
|
| 38 |
+
*/
|
| 39 |
+
add(text: string): ParseState {
|
| 40 |
+
if (typeof text !== 'string') {
|
| 41 |
+
throw new Error('input text must be of type string');
|
| 42 |
+
}
|
| 43 |
+
const pointers = this.graph.add(text, this.pointers);
|
| 44 |
+
return new ParseState(this.graph, pointers);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/** The number of unique rules that may come next. */
|
| 48 |
+
get size(): number {
|
| 49 |
+
return [...this.rules()].length;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
get grammar(): string {
|
| 53 |
+
return this.graph.grammar;
|
| 54 |
+
}
|
| 55 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/pointers.test.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { RuleEnd } from './grammar-graph-types.js';
|
| 3 |
+
import { GraphNode } from './graph-node.js';
|
| 4 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 5 |
+
import { Pointers } from './pointers.js';
|
| 6 |
+
|
| 7 |
+
const createPointer = (stackId: number): GraphPointer =>
|
| 8 |
+
new GraphPointer(new GraphNode(new RuleEnd(), { stackId, pathId: 0, stepId: 0 }));
|
| 9 |
+
|
| 10 |
+
describe('Pointers', () => {
|
| 11 |
+
it('is empty to begin with', () => {
|
| 12 |
+
const pointers = new Pointers();
|
| 13 |
+
expect(pointers.size).toBe(0);
|
| 14 |
+
expect([...pointers]).toEqual([]);
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
it('holds the pointers it is given, in order', () => {
|
| 18 |
+
const [a, b, c] = [createPointer(0), createPointer(1), createPointer(2)];
|
| 19 |
+
const pointers = new Pointers(c, a);
|
| 20 |
+
pointers.add(b);
|
| 21 |
+
expect(pointers.size).toBe(3);
|
| 22 |
+
expect([...pointers]).toEqual([c, a, b]);
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
it('holds a single pointer per id, in the position where its id was first added', () => {
|
| 26 |
+
const [a, b] = [createPointer(0), createPointer(1)];
|
| 27 |
+
const duplicate = createPointer(0);
|
| 28 |
+
const pointers = new Pointers(a, b, duplicate);
|
| 29 |
+
expect(pointers.size).toBe(2);
|
| 30 |
+
const [first, second] = [...pointers];
|
| 31 |
+
expect(first).toBe(duplicate);
|
| 32 |
+
expect(second).toBe(b);
|
| 33 |
+
});
|
| 34 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/pointers.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { GraphPointer } from './graph-pointer.js';
|
| 2 |
+
|
| 3 |
+
/** The pointers into the graph, unique by id, in the order in which they were first added. */
|
| 4 |
+
export class Pointers implements Iterable<GraphPointer> {
|
| 5 |
+
private readonly pointers = new Map<string, GraphPointer>();
|
| 6 |
+
|
| 7 |
+
constructor(...pointers: GraphPointer[]) {
|
| 8 |
+
for (const pointer of pointers) {
|
| 9 |
+
this.add(pointer);
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
add(pointer: GraphPointer): void {
|
| 14 |
+
this.pointers.set(pointer.id, pointer);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
[Symbol.iterator](): IterableIterator<GraphPointer> {
|
| 18 |
+
return this.pointers.values();
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
get size(): number {
|
| 22 |
+
return this.pointers.size;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
toString(): string {
|
| 26 |
+
return `<Pointers ${[...this].map((pointer) => pointer.node.rule).join(', ')}>`;
|
| 27 |
+
}
|
| 28 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/print.test.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
| 2 |
+
import { getParentStackId } from './get-parent-stack-id.js';
|
| 3 |
+
import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from './grammar-graph-types.js';
|
| 4 |
+
import { GraphNode } from './graph-node.js';
|
| 5 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 6 |
+
import { getChar, printGraphNode, printGraphPointer } from './print.js';
|
| 7 |
+
import { RuleRef } from './rule-ref.js';
|
| 8 |
+
|
| 9 |
+
vi.mock('./get-parent-stack-id.js', () => ({
|
| 10 |
+
getParentStackId: vi.fn(),
|
| 11 |
+
}));
|
| 12 |
+
|
| 13 |
+
beforeEach(() => {
|
| 14 |
+
vi.mocked(getParentStackId).mockReset();
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
const COLORS: Record<string, string> = {
|
| 18 |
+
'\x1b[34m': 'BLUE',
|
| 19 |
+
'\x1b[36m': 'CYAN',
|
| 20 |
+
'\x1b[32m': 'GREEN',
|
| 21 |
+
'\x1b[31m': 'RED',
|
| 22 |
+
'\x1b[90m': 'GRAY',
|
| 23 |
+
'\x1b[33m': 'YELLOW',
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
const mockColorize = (text: string | number, color: string): string => {
|
| 27 |
+
if (!(color in COLORS)) {
|
| 28 |
+
throw new Error(`Invalid color: ${color}`);
|
| 29 |
+
}
|
| 30 |
+
return `[${COLORS[color]}]:${text}`;
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
// a stand-in for a node that can be printed without the nodes that it would link to
|
| 34 |
+
const createMockNode = (id: number, rule: unknown, next?: GraphNode): GraphNode =>
|
| 35 |
+
({ id, rule, next, print: () => `Node(${id})` }) as unknown as GraphNode;
|
| 36 |
+
|
| 37 |
+
const createRealNode = (rule: UnresolvedRule, stepId = 0, next?: GraphNode): GraphNode =>
|
| 38 |
+
new GraphNode(rule, { stackId: 0, pathId: 0, stepId }, next);
|
| 39 |
+
|
| 40 |
+
describe('print', () => {
|
| 41 |
+
describe('printGraphPointer', () => {
|
| 42 |
+
it('prints graph pointer details correctly', () => {
|
| 43 |
+
const mockPointer = {
|
| 44 |
+
node: createMockNode(1, new RuleRef(100)),
|
| 45 |
+
parent: undefined,
|
| 46 |
+
} as unknown as GraphPointer;
|
| 47 |
+
vi.mocked(getParentStackId).mockReturnValue('foo');
|
| 48 |
+
const result = printGraphPointer(mockPointer)({
|
| 49 |
+
colorize: mockColorize,
|
| 50 |
+
pointers: [],
|
| 51 |
+
showPosition: false,
|
| 52 |
+
});
|
| 53 |
+
expect(result).toBe('[RED]:*foo');
|
| 54 |
+
expect(getParentStackId).toHaveBeenCalledWith(mockPointer, mockColorize);
|
| 55 |
+
});
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
describe('printGraphNode', () => {
|
| 59 |
+
it('prints graph node with a character rule', () => {
|
| 60 |
+
const mockNode = createMockNode(1, new RuleChar([65]));
|
| 61 |
+
const result = printGraphNode(mockNode)({
|
| 62 |
+
colorize: mockColorize,
|
| 63 |
+
showPosition: false,
|
| 64 |
+
pointers: [],
|
| 65 |
+
});
|
| 66 |
+
expect(result).toBe('[GRAY]:[[YELLOW]:A[GRAY]:]');
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
it('prints graph node with a rule reference', () => {
|
| 70 |
+
const mockNode = createMockNode(1, new RuleRef(200));
|
| 71 |
+
const result = printGraphNode(mockNode)({
|
| 72 |
+
colorize: mockColorize,
|
| 73 |
+
showPosition: true,
|
| 74 |
+
pointers: [],
|
| 75 |
+
});
|
| 76 |
+
expect(result).toBe('[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)');
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
it('prints ranges, escaped newlines, and astral characters', () => {
|
| 80 |
+
const mockNode = createMockNode(1, new RuleChar([[97, 99], 10, 0x1f600]));
|
| 81 |
+
const result = printGraphNode(mockNode)({ colorize: mockColorize });
|
| 82 |
+
expect(result).toBe('[GRAY]:[[YELLOW]:[YELLOW]:a[YELLOW]:c\\n😀[GRAY]:]');
|
| 83 |
+
});
|
| 84 |
+
|
| 85 |
+
it('prints the type of the rules that have nothing else to show', () => {
|
| 86 |
+
const opts = { colorize: mockColorize };
|
| 87 |
+
expect(printGraphNode(createMockNode(1, new RuleEnd()))(opts)).toBe('[YELLOW]:end');
|
| 88 |
+
expect(printGraphNode(createMockNode(1, new RuleCharExclude([97])))(opts)).toBe(
|
| 89 |
+
'[YELLOW]:char_exclude',
|
| 90 |
+
);
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
it('prints the nodes that follow the node, separated by arrows', () => {
|
| 94 |
+
const end = createRealNode(new RuleEnd(), 1);
|
| 95 |
+
const node = createRealNode(new RuleChar([97]), 0, end);
|
| 96 |
+
const result = printGraphNode(node)({ colorize: mockColorize, showPosition: true });
|
| 97 |
+
expect(result).toBe(
|
| 98 |
+
'[BLUE]:{[GRAY]:0,0,0[BLUE]:}[GRAY]:[[YELLOW]:a[GRAY]:][GRAY]:-> ' +
|
| 99 |
+
'[BLUE]:{[GRAY]:0,0,1[BLUE]:}[YELLOW]:end',
|
| 100 |
+
);
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
it('prints the pointers that point at the node', () => {
|
| 104 |
+
vi.mocked(getParentStackId).mockReturnValue('');
|
| 105 |
+
const node = createRealNode(new RuleChar([97]));
|
| 106 |
+
const other = createRealNode(new RuleChar([98]), 1);
|
| 107 |
+
const result = printGraphNode(node)({
|
| 108 |
+
colorize: mockColorize,
|
| 109 |
+
pointers: [new GraphPointer(other), new GraphPointer(node)],
|
| 110 |
+
});
|
| 111 |
+
expect(result).toBe('[GRAY]:[[YELLOW]:a[GRAY]:][GRAY]:[[YELLOW]:[RED]:*[GRAY]:]');
|
| 112 |
+
});
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
describe('getChar', () => {
|
| 116 |
+
it('returns the character for a code point', () => {
|
| 117 |
+
expect(getChar(97)).toBe('a');
|
| 118 |
+
expect(getChar(0x1f600)).toBe('😀');
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
it('escapes new lines', () => {
|
| 122 |
+
expect(getChar(10)).toBe('\\n');
|
| 123 |
+
});
|
| 124 |
+
});
|
| 125 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/print.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Color } from './colorize.js';
|
| 2 |
+
import { getParentStackId } from './get-parent-stack-id.js';
|
| 3 |
+
import type { PrintOpts } from './grammar-graph-types.js';
|
| 4 |
+
import type { GraphNode } from './graph-node.js';
|
| 5 |
+
import type { GraphPointer } from './graph-pointer.js';
|
| 6 |
+
import { isRange, isRuleChar, isRuleRef } from './type-guards.js';
|
| 7 |
+
|
| 8 |
+
export function getChar(charCode: number): string {
|
| 9 |
+
const char = String.fromCodePoint(charCode);
|
| 10 |
+
if (char === '\n') {
|
| 11 |
+
return '\\n';
|
| 12 |
+
}
|
| 13 |
+
return char;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
export function printGraphPointer(pointer: GraphPointer): (opts: PrintOpts) => string {
|
| 17 |
+
return (opts) => {
|
| 18 |
+
const col = opts.colorize;
|
| 19 |
+
return col(`*${getParentStackId(pointer, col)}`, Color.RED);
|
| 20 |
+
};
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
export function printGraphNode(node: GraphNode): (opts: PrintOpts) => string {
|
| 24 |
+
return (opts) => {
|
| 25 |
+
const pointers = opts.pointers ?? [];
|
| 26 |
+
const col = opts.colorize;
|
| 27 |
+
const showPosition = opts.showPosition ?? false;
|
| 28 |
+
const rule = node.rule;
|
| 29 |
+
const parts: string[] = [];
|
| 30 |
+
if (showPosition) {
|
| 31 |
+
parts.push(col('{', Color.BLUE), col(node.id, Color.GRAY), col('}', Color.BLUE));
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if (isRuleChar(rule)) {
|
| 35 |
+
parts.push(
|
| 36 |
+
col('[', Color.GRAY),
|
| 37 |
+
col(
|
| 38 |
+
rule.value
|
| 39 |
+
.map((v) =>
|
| 40 |
+
isRange(v) ? v.map((val) => col(getChar(val), Color.YELLOW)).join('') : getChar(v),
|
| 41 |
+
)
|
| 42 |
+
.join(''),
|
| 43 |
+
Color.YELLOW,
|
| 44 |
+
),
|
| 45 |
+
col(']', Color.GRAY),
|
| 46 |
+
);
|
| 47 |
+
} else if (isRuleRef(rule)) {
|
| 48 |
+
parts.push(col('Ref(', Color.GRAY), col(rule.value, Color.GREEN), col(')', Color.GRAY));
|
| 49 |
+
} else {
|
| 50 |
+
parts.push(col(rule.type, Color.YELLOW));
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
for (const pointer of pointers) {
|
| 54 |
+
const pointerParts: string[] = [];
|
| 55 |
+
if (pointer.node === node) {
|
| 56 |
+
pointerParts.push(pointer.print(opts));
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
if (pointerParts.length > 0) {
|
| 60 |
+
parts.push(
|
| 61 |
+
col('[', Color.GRAY),
|
| 62 |
+
col(pointerParts.join(''), Color.YELLOW),
|
| 63 |
+
col(']', Color.GRAY),
|
| 64 |
+
);
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
const partsToReturn = [parts.join('')];
|
| 69 |
+
if (node.next) {
|
| 70 |
+
partsToReturn.push(node.next.print(opts));
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return partsToReturn.join(col('-> ', Color.GRAY));
|
| 74 |
+
};
|
| 75 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/rule-ref.test.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import type { GraphNode } from './graph-node.js';
|
| 3 |
+
import { RuleRef } from './rule-ref.js';
|
| 4 |
+
|
| 5 |
+
describe('RuleRef', () => {
|
| 6 |
+
it('initializes with a given value', () => {
|
| 7 |
+
const ruleRef = new RuleRef(123);
|
| 8 |
+
expect(ruleRef.value).toBe(123);
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
it('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).toBe(mockNodes);
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
it('throws an error if trying to get nodes before setting', () => {
|
| 19 |
+
const ruleRef = new RuleRef(123);
|
| 20 |
+
expect(() => ruleRef.nodes).toThrow('Nodes are not set');
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
it('does not expose its nodes when serialized or compared', () => {
|
| 24 |
+
const ruleRef = new RuleRef(123);
|
| 25 |
+
ruleRef.nodes = new Set();
|
| 26 |
+
expect(JSON.stringify(ruleRef)).toBe('{"type":"ref","value":123}');
|
| 27 |
+
expect(ruleRef).toEqual(new RuleRef(123));
|
| 28 |
+
});
|
| 29 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/rule-ref.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { GraphNode } from './graph-node.js';
|
| 2 |
+
|
| 3 |
+
/** A reference from one rule to another. Rule refs are an implementation detail of the graph. */
|
| 4 |
+
export class RuleRef {
|
| 5 |
+
readonly type = 'ref';
|
| 6 |
+
value: number;
|
| 7 |
+
#nodes: Set<GraphNode> | undefined;
|
| 8 |
+
|
| 9 |
+
constructor(value: number) {
|
| 10 |
+
this.value = value;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
/** The nodes at the start of each path of the referenced rule. */
|
| 14 |
+
get nodes(): Set<GraphNode> {
|
| 15 |
+
if (this.#nodes === undefined) {
|
| 16 |
+
throw new Error('Nodes are not set');
|
| 17 |
+
}
|
| 18 |
+
return this.#nodes;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
set nodes(nodes: Set<GraphNode>) {
|
| 22 |
+
this.#nodes = nodes;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
toString(): string {
|
| 26 |
+
return `RuleRef(value=${this.value})`;
|
| 27 |
+
}
|
| 28 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/type-guards.test.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { RuleChar, RuleCharExclude, RuleEnd } from './grammar-graph-types.js';
|
| 3 |
+
import { GraphNode } from './graph-node.js';
|
| 4 |
+
import { GraphPointer } from './graph-pointer.js';
|
| 5 |
+
import { RuleRef } from './rule-ref.js';
|
| 6 |
+
import {
|
| 7 |
+
isGraphPointerRuleChar,
|
| 8 |
+
isGraphPointerRuleCharExclude,
|
| 9 |
+
isGraphPointerRuleEnd,
|
| 10 |
+
isGraphPointerRuleRef,
|
| 11 |
+
isRange,
|
| 12 |
+
isRule,
|
| 13 |
+
isRuleChar,
|
| 14 |
+
isRuleCharExclude,
|
| 15 |
+
isRuleEnd,
|
| 16 |
+
isRuleRef,
|
| 17 |
+
} from './type-guards.js';
|
| 18 |
+
|
| 19 |
+
const meta = { stackId: 1, pathId: 2, stepId: 3 };
|
| 20 |
+
|
| 21 |
+
describe('rule type guards', () => {
|
| 22 |
+
describe('isGraphPointerRuleRef', () => {
|
| 23 |
+
it('returns true', () => {
|
| 24 |
+
const pointer = new GraphPointer(new GraphNode(new RuleRef(1), meta));
|
| 25 |
+
expect(isGraphPointerRuleRef(pointer)).toBe(true);
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
it('returns false', () => {
|
| 29 |
+
const pointer = new GraphPointer(new GraphNode(new RuleEnd(), meta));
|
| 30 |
+
expect(isGraphPointerRuleRef(pointer)).toBe(false);
|
| 31 |
+
});
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
describe('isGraphPointerRuleEnd', () => {
|
| 35 |
+
it('returns false', () => {
|
| 36 |
+
const pointer = new GraphPointer(new GraphNode(new RuleRef(1), meta));
|
| 37 |
+
expect(isGraphPointerRuleEnd(pointer)).toBe(false);
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
it('returns true', () => {
|
| 41 |
+
const pointer = new GraphPointer(new GraphNode(new RuleEnd(), meta));
|
| 42 |
+
expect(isGraphPointerRuleEnd(pointer)).toBe(true);
|
| 43 |
+
});
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
describe('isGraphPointerRuleChar', () => {
|
| 47 |
+
it('returns true', () => {
|
| 48 |
+
const pointer = new GraphPointer(new GraphNode(new RuleChar([97]), meta));
|
| 49 |
+
expect(isGraphPointerRuleChar(pointer)).toBe(true);
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
it('returns false', () => {
|
| 53 |
+
const pointer = new GraphPointer(new GraphNode(new RuleEnd(), meta));
|
| 54 |
+
expect(isGraphPointerRuleChar(pointer)).toBe(false);
|
| 55 |
+
});
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
describe('isGraphPointerRuleCharExclude', () => {
|
| 59 |
+
it('returns true', () => {
|
| 60 |
+
const pointer = new GraphPointer(new GraphNode(new RuleCharExclude([97]), meta));
|
| 61 |
+
expect(isGraphPointerRuleCharExclude(pointer)).toBe(true);
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
it('returns false', () => {
|
| 65 |
+
const pointer = new GraphPointer(new GraphNode(new RuleChar([97]), meta));
|
| 66 |
+
expect(isGraphPointerRuleCharExclude(pointer)).toBe(false);
|
| 67 |
+
});
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
describe('isRule', () => {
|
| 71 |
+
it('returns false for null', () => {
|
| 72 |
+
expect(isRule(null)).toBe(false);
|
| 73 |
+
expect(isRule(undefined)).toBe(false);
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
it('returns false for a non rule', () => {
|
| 77 |
+
expect(isRule({ type: 'invalid', value: [] })).toBe(false);
|
| 78 |
+
expect(isRule('char')).toBe(false);
|
| 79 |
+
});
|
| 80 |
+
|
| 81 |
+
it('returns true for valid rule objects', () => {
|
| 82 |
+
expect(isRule(new RuleChar([65, [66, 67]]))).toBe(true);
|
| 83 |
+
expect(isRule(new RuleCharExclude([65]))).toBe(true);
|
| 84 |
+
expect(isRule(new RuleEnd())).toBe(true);
|
| 85 |
+
expect(isRule(new RuleRef(1))).toBe(true);
|
| 86 |
+
});
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
describe('isRuleRef', () => {
|
| 90 |
+
it('returns true for valid rule refs', () => {
|
| 91 |
+
expect(isRuleRef(new RuleRef(1))).toBe(true);
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
it('returns false for invalid rule refs', () => {
|
| 95 |
+
expect(isRuleRef(new RuleChar([65]))).toBe(false);
|
| 96 |
+
});
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
describe('isRuleEnd', () => {
|
| 100 |
+
it('returns true for valid rule ends', () => {
|
| 101 |
+
expect(isRuleEnd(new RuleEnd())).toBe(true);
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
it('returns false for invalid rule ends', () => {
|
| 105 |
+
expect(isRuleEnd(new RuleChar([65]))).toBe(false);
|
| 106 |
+
expect(isRuleEnd(undefined)).toBe(false);
|
| 107 |
+
});
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
describe('isRuleChar', () => {
|
| 111 |
+
it('returns true for valid rule chars', () => {
|
| 112 |
+
expect(isRuleChar(new RuleChar([65]))).toBe(true);
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
it('returns false for invalid rule chars', () => {
|
| 116 |
+
expect(isRuleChar(new RuleCharExclude([65]))).toBe(false);
|
| 117 |
+
});
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
describe('isRuleCharExclude', () => {
|
| 121 |
+
it('returns true for valid rule chars', () => {
|
| 122 |
+
expect(isRuleCharExclude(new RuleCharExclude([65]))).toBe(true);
|
| 123 |
+
});
|
| 124 |
+
|
| 125 |
+
it('returns false for invalid rule chars', () => {
|
| 126 |
+
expect(isRuleCharExclude(new RuleChar([65]))).toBe(false);
|
| 127 |
+
});
|
| 128 |
+
});
|
| 129 |
+
|
| 130 |
+
describe('isRange', () => {
|
| 131 |
+
it('returns true for valid ranges', () => {
|
| 132 |
+
expect(isRange([1, 10])).toBe(true);
|
| 133 |
+
});
|
| 134 |
+
|
| 135 |
+
it.each([[[1, '10']], [[1]], [[1, 2, 3]], [5], ['ab'], [null]])(
|
| 136 |
+
'returns false for the invalid range %j',
|
| 137 |
+
(value) => {
|
| 138 |
+
expect(isRange(value)).toBe(false);
|
| 139 |
+
},
|
| 140 |
+
);
|
| 141 |
+
});
|
| 142 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-graph/type-guards.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type {
|
| 2 |
+
Range,
|
| 3 |
+
RuleChar,
|
| 4 |
+
RuleCharExclude,
|
| 5 |
+
RuleEnd,
|
| 6 |
+
UnresolvedRule,
|
| 7 |
+
} from './grammar-graph-types.js';
|
| 8 |
+
import type { GraphPointer } from './graph-pointer.js';
|
| 9 |
+
import type { RuleRef } from './rule-ref.js';
|
| 10 |
+
|
| 11 |
+
function hasType(rule: unknown, type: UnresolvedRule['type']): boolean {
|
| 12 |
+
return typeof rule === 'object' && rule !== null && (rule as { type?: unknown }).type === type;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export function isRule(rule: unknown): rule is UnresolvedRule {
|
| 16 |
+
return (
|
| 17 |
+
hasType(rule, 'char') ||
|
| 18 |
+
hasType(rule, 'char_exclude') ||
|
| 19 |
+
hasType(rule, 'end') ||
|
| 20 |
+
hasType(rule, 'ref')
|
| 21 |
+
);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export function isRuleRef(rule: unknown): rule is RuleRef {
|
| 25 |
+
return hasType(rule, 'ref');
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
export function isRuleEnd(rule: unknown): rule is RuleEnd {
|
| 29 |
+
return hasType(rule, 'end');
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
export function isRuleChar(rule: unknown): rule is RuleChar {
|
| 33 |
+
return hasType(rule, 'char');
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
export function isRuleCharExclude(rule: unknown): rule is RuleCharExclude {
|
| 37 |
+
return hasType(rule, 'char_exclude');
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
export function isRange(input: unknown): input is Range {
|
| 41 |
+
return Array.isArray(input) && input.length === 2 && input.every((n) => Number.isInteger(n));
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
export function isGraphPointerRuleRef(pointer: GraphPointer): pointer is GraphPointer<RuleRef> {
|
| 45 |
+
return isRuleRef(pointer.rule);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
export function isGraphPointerRuleEnd(pointer: GraphPointer): pointer is GraphPointer<RuleEnd> {
|
| 49 |
+
return isRuleEnd(pointer.rule);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
export function isGraphPointerRuleChar(pointer: GraphPointer): pointer is GraphPointer<RuleChar> {
|
| 53 |
+
return isRuleChar(pointer.rule);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
export function isGraphPointerRuleCharExclude(
|
| 57 |
+
pointer: GraphPointer,
|
| 58 |
+
): pointer is GraphPointer<RuleCharExclude> {
|
| 59 |
+
return isRuleCharExclude(pointer.rule);
|
| 60 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-parser/build-rule-stack.test.ts
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import {
|
| 3 |
+
type Range,
|
| 4 |
+
RuleChar,
|
| 5 |
+
RuleCharExclude,
|
| 6 |
+
RuleEnd,
|
| 7 |
+
} from '../grammar-graph/grammar-graph-types.js';
|
| 8 |
+
import { RuleRef } from '../grammar-graph/rule-ref.js';
|
| 9 |
+
import {
|
| 10 |
+
type InternalRuleDef,
|
| 11 |
+
InternalRuleDefAlt,
|
| 12 |
+
InternalRuleDefChar,
|
| 13 |
+
InternalRuleDefCharAlt,
|
| 14 |
+
InternalRuleDefCharNot,
|
| 15 |
+
InternalRuleDefCharRngUpper,
|
| 16 |
+
InternalRuleDefEnd,
|
| 17 |
+
InternalRuleDefReference,
|
| 18 |
+
} from '../rules-builder/rules-builder-types.js';
|
| 19 |
+
import { buildRuleStack } from './build-rule-stack.js';
|
| 20 |
+
|
| 21 |
+
const ord = (char: string): number => char.codePointAt(0) as number;
|
| 22 |
+
|
| 23 |
+
const ichar = (value: number[]) => new InternalRuleDefChar(value);
|
| 24 |
+
const icharAlt = (value: number) => new InternalRuleDefCharAlt(value);
|
| 25 |
+
const icharRngUpper = (value: number) => new InternalRuleDefCharRngUpper(value);
|
| 26 |
+
const icharNot = (value: number[]) => new InternalRuleDefCharNot(value);
|
| 27 |
+
const ialt = () => new InternalRuleDefAlt();
|
| 28 |
+
const iend = () => new InternalRuleDefEnd();
|
| 29 |
+
const iref = (value: number) => new InternalRuleDefReference(value);
|
| 30 |
+
|
| 31 |
+
const makeRange = (lower: number | string, upper: number | string): Range => [
|
| 32 |
+
typeof lower === 'number' ? lower : ord(lower),
|
| 33 |
+
typeof upper === 'number' ? upper : ord(upper),
|
| 34 |
+
];
|
| 35 |
+
|
| 36 |
+
type Case = [grammar: string, input: InternalRuleDef[], expected: unknown[][]];
|
| 37 |
+
|
| 38 |
+
describe('buildRuleStack', () => {
|
| 39 |
+
it('builds rule stack for a single path', () => {
|
| 40 |
+
expect(buildRuleStack([ichar([120])])).toStrictEqual([[new RuleChar([120]), new RuleEnd()]]);
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
it('builds rule stack for two alternate paths', () => {
|
| 44 |
+
expect(buildRuleStack([ichar([ord('x')]), ialt(), ichar([ord('y')])])).toStrictEqual([
|
| 45 |
+
[new RuleChar([ord('x')]), new RuleEnd()],
|
| 46 |
+
[new RuleChar([ord('y')]), new RuleEnd()],
|
| 47 |
+
]);
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
it('builds rule stack for three alternate paths', () => {
|
| 51 |
+
expect(
|
| 52 |
+
buildRuleStack([
|
| 53 |
+
ichar([ord('x')]),
|
| 54 |
+
ialt(),
|
| 55 |
+
ichar([ord('y')]),
|
| 56 |
+
ialt(),
|
| 57 |
+
ichar([ord('z')]),
|
| 58 |
+
]),
|
| 59 |
+
).toStrictEqual([
|
| 60 |
+
[new RuleChar([ord('x')]), new RuleEnd()],
|
| 61 |
+
[new RuleChar([ord('y')]), new RuleEnd()],
|
| 62 |
+
[new RuleChar([ord('z')]), new RuleEnd()],
|
| 63 |
+
]);
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
it('builds rule stack for char not', () => {
|
| 67 |
+
expect(
|
| 68 |
+
buildRuleStack([
|
| 69 |
+
icharNot([ord('x')]),
|
| 70 |
+
ialt(),
|
| 71 |
+
icharNot([ord('y')]),
|
| 72 |
+
ialt(),
|
| 73 |
+
icharNot([ord('z')]),
|
| 74 |
+
]),
|
| 75 |
+
).toStrictEqual([
|
| 76 |
+
[new RuleCharExclude([ord('x')]), new RuleEnd()],
|
| 77 |
+
[new RuleCharExclude([ord('y')]), new RuleEnd()],
|
| 78 |
+
[new RuleCharExclude([ord('z')]), new RuleEnd()],
|
| 79 |
+
]);
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
it('builds rule stack for mixed char and char not', () => {
|
| 83 |
+
expect(
|
| 84 |
+
buildRuleStack([
|
| 85 |
+
icharNot([ord('x')]),
|
| 86 |
+
ialt(),
|
| 87 |
+
ichar([ord('y')]),
|
| 88 |
+
ialt(),
|
| 89 |
+
icharNot([ord('z')]),
|
| 90 |
+
]),
|
| 91 |
+
).toStrictEqual([
|
| 92 |
+
[new RuleCharExclude([ord('x')]), new RuleEnd()],
|
| 93 |
+
[new RuleChar([ord('y')]), new RuleEnd()],
|
| 94 |
+
[new RuleCharExclude([ord('z')]), new RuleEnd()],
|
| 95 |
+
]);
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
it('builds rule stack for char not with two characters and a range', () => {
|
| 99 |
+
expect(
|
| 100 |
+
buildRuleStack([
|
| 101 |
+
icharNot([ord('x')]),
|
| 102 |
+
icharAlt(ord('y')),
|
| 103 |
+
icharAlt(ord('z')),
|
| 104 |
+
icharRngUpper(130),
|
| 105 |
+
]),
|
| 106 |
+
).toStrictEqual([[new RuleCharExclude([120, 121, makeRange(122, 130)]), new RuleEnd()]]);
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
describe('ranges', () => {
|
| 110 |
+
const cases: Case[] = [
|
| 111 |
+
[
|
| 112 |
+
'[a-z]',
|
| 113 |
+
[ichar([ord('a')]), icharRngUpper(ord('z')), iend()],
|
| 114 |
+
[[new RuleChar([makeRange('a', 'z')]), new RuleEnd()]],
|
| 115 |
+
],
|
| 116 |
+
[
|
| 117 |
+
'[a-zA-Z]',
|
| 118 |
+
[
|
| 119 |
+
ichar([ord('a')]),
|
| 120 |
+
icharRngUpper(ord('z')),
|
| 121 |
+
icharAlt(ord('A')),
|
| 122 |
+
icharRngUpper(ord('Z')),
|
| 123 |
+
iend(),
|
| 124 |
+
],
|
| 125 |
+
[[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()]],
|
| 126 |
+
],
|
| 127 |
+
[
|
| 128 |
+
'[a-zA-Z0-9]',
|
| 129 |
+
[
|
| 130 |
+
ichar([ord('a')]),
|
| 131 |
+
icharRngUpper(ord('z')),
|
| 132 |
+
icharAlt(ord('A')),
|
| 133 |
+
icharRngUpper(ord('Z')),
|
| 134 |
+
icharAlt(ord('0')),
|
| 135 |
+
icharRngUpper(ord('9')),
|
| 136 |
+
iend(),
|
| 137 |
+
],
|
| 138 |
+
[
|
| 139 |
+
[
|
| 140 |
+
new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]),
|
| 141 |
+
new RuleEnd(),
|
| 142 |
+
],
|
| 143 |
+
],
|
| 144 |
+
],
|
| 145 |
+
];
|
| 146 |
+
|
| 147 |
+
it.each(cases)(
|
| 148 |
+
'builds rule stack for a char with no modifiers: %s',
|
| 149 |
+
(_grammar, input, expected) => {
|
| 150 |
+
expect(buildRuleStack(input)).toStrictEqual(expected);
|
| 151 |
+
},
|
| 152 |
+
);
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
describe('modifiers', () => {
|
| 156 |
+
const questionMarkCases: Case[] = [
|
| 157 |
+
[
|
| 158 |
+
'[a-z]?',
|
| 159 |
+
[ichar([ord('a')]), icharRngUpper(ord('z')), ialt(), iend()],
|
| 160 |
+
[[new RuleChar([makeRange('a', 'z')]), new RuleEnd()], [new RuleEnd()]],
|
| 161 |
+
],
|
| 162 |
+
[
|
| 163 |
+
'[a-zA-Z]?',
|
| 164 |
+
[
|
| 165 |
+
ichar([ord('a')]),
|
| 166 |
+
icharRngUpper(ord('z')),
|
| 167 |
+
icharAlt(ord('A')),
|
| 168 |
+
icharRngUpper(ord('Z')),
|
| 169 |
+
ialt(),
|
| 170 |
+
iend(),
|
| 171 |
+
],
|
| 172 |
+
[
|
| 173 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()],
|
| 174 |
+
[new RuleEnd()],
|
| 175 |
+
],
|
| 176 |
+
],
|
| 177 |
+
[
|
| 178 |
+
'[a-zA-Z0-9]?',
|
| 179 |
+
[
|
| 180 |
+
ichar([ord('a')]),
|
| 181 |
+
icharRngUpper(ord('z')),
|
| 182 |
+
icharAlt(ord('A')),
|
| 183 |
+
icharRngUpper(ord('Z')),
|
| 184 |
+
icharAlt(ord('0')),
|
| 185 |
+
icharRngUpper(ord('9')),
|
| 186 |
+
ialt(),
|
| 187 |
+
iend(),
|
| 188 |
+
],
|
| 189 |
+
[
|
| 190 |
+
[
|
| 191 |
+
new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]),
|
| 192 |
+
new RuleEnd(),
|
| 193 |
+
],
|
| 194 |
+
[new RuleEnd()],
|
| 195 |
+
],
|
| 196 |
+
],
|
| 197 |
+
];
|
| 198 |
+
|
| 199 |
+
it.each(questionMarkCases)(
|
| 200 |
+
'builds rule stack for a char with question mark modifier: %s',
|
| 201 |
+
(_grammar, input, expected) => {
|
| 202 |
+
expect(buildRuleStack(input)).toStrictEqual(expected);
|
| 203 |
+
},
|
| 204 |
+
);
|
| 205 |
+
|
| 206 |
+
const plusCases: Case[] = [
|
| 207 |
+
[
|
| 208 |
+
'[a-z]+',
|
| 209 |
+
[
|
| 210 |
+
ichar([ord('a')]),
|
| 211 |
+
icharRngUpper(ord('z')),
|
| 212 |
+
iref(1),
|
| 213 |
+
ialt(),
|
| 214 |
+
ichar([ord('a')]),
|
| 215 |
+
icharRngUpper(ord('z')),
|
| 216 |
+
iend(),
|
| 217 |
+
],
|
| 218 |
+
[
|
| 219 |
+
[new RuleChar([makeRange('a', 'z')]), new RuleRef(1), new RuleEnd()],
|
| 220 |
+
[new RuleChar([makeRange('a', 'z')]), new RuleEnd()],
|
| 221 |
+
],
|
| 222 |
+
],
|
| 223 |
+
[
|
| 224 |
+
'[a-zA-Z]+',
|
| 225 |
+
[
|
| 226 |
+
ichar([ord('a')]),
|
| 227 |
+
icharRngUpper(ord('z')),
|
| 228 |
+
icharAlt(ord('A')),
|
| 229 |
+
icharRngUpper(ord('Z')),
|
| 230 |
+
iref(1),
|
| 231 |
+
ialt(),
|
| 232 |
+
ichar([ord('a')]),
|
| 233 |
+
icharRngUpper(ord('z')),
|
| 234 |
+
icharAlt(ord('A')),
|
| 235 |
+
icharRngUpper(ord('Z')),
|
| 236 |
+
iend(),
|
| 237 |
+
],
|
| 238 |
+
[
|
| 239 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()],
|
| 240 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()],
|
| 241 |
+
],
|
| 242 |
+
],
|
| 243 |
+
[
|
| 244 |
+
'[a-zA-Z0-9]+',
|
| 245 |
+
[
|
| 246 |
+
ichar([ord('a')]),
|
| 247 |
+
icharRngUpper(ord('z')),
|
| 248 |
+
icharAlt(ord('A')),
|
| 249 |
+
icharRngUpper(ord('Z')),
|
| 250 |
+
icharAlt(ord('0')),
|
| 251 |
+
icharRngUpper(ord('9')),
|
| 252 |
+
iref(1),
|
| 253 |
+
ialt(),
|
| 254 |
+
ichar([ord('a')]),
|
| 255 |
+
icharRngUpper(ord('z')),
|
| 256 |
+
icharAlt(ord('A')),
|
| 257 |
+
icharRngUpper(ord('Z')),
|
| 258 |
+
icharAlt(ord('0')),
|
| 259 |
+
icharRngUpper(ord('9')),
|
| 260 |
+
iend(),
|
| 261 |
+
],
|
| 262 |
+
[
|
| 263 |
+
[
|
| 264 |
+
new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]),
|
| 265 |
+
new RuleRef(1),
|
| 266 |
+
new RuleEnd(),
|
| 267 |
+
],
|
| 268 |
+
[
|
| 269 |
+
new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]),
|
| 270 |
+
new RuleEnd(),
|
| 271 |
+
],
|
| 272 |
+
],
|
| 273 |
+
],
|
| 274 |
+
];
|
| 275 |
+
|
| 276 |
+
it.each(plusCases)(
|
| 277 |
+
'builds rule stack for a char with plus modifier: %s',
|
| 278 |
+
(_grammar, input, expected) => {
|
| 279 |
+
expect(buildRuleStack(input)).toStrictEqual(expected);
|
| 280 |
+
},
|
| 281 |
+
);
|
| 282 |
+
|
| 283 |
+
const asteriskCases: Case[] = [
|
| 284 |
+
[
|
| 285 |
+
'[a-z]*',
|
| 286 |
+
[ichar([ord('a')]), icharRngUpper(ord('z')), iref(1), ialt(), iend()],
|
| 287 |
+
[
|
| 288 |
+
[new RuleChar([makeRange('a', 'z')]), new RuleRef(1), new RuleEnd()],
|
| 289 |
+
[new RuleEnd()],
|
| 290 |
+
],
|
| 291 |
+
],
|
| 292 |
+
[
|
| 293 |
+
'[a-zA-Z]*',
|
| 294 |
+
[
|
| 295 |
+
ichar([ord('a')]),
|
| 296 |
+
icharRngUpper(ord('z')),
|
| 297 |
+
icharAlt(ord('A')),
|
| 298 |
+
icharRngUpper(ord('Z')),
|
| 299 |
+
iref(1),
|
| 300 |
+
ialt(),
|
| 301 |
+
iend(),
|
| 302 |
+
],
|
| 303 |
+
[
|
| 304 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()],
|
| 305 |
+
[new RuleEnd()],
|
| 306 |
+
],
|
| 307 |
+
],
|
| 308 |
+
[
|
| 309 |
+
'[a-zA-Z0-9]*',
|
| 310 |
+
[
|
| 311 |
+
ichar([ord('a')]),
|
| 312 |
+
icharRngUpper(ord('z')),
|
| 313 |
+
icharAlt(ord('A')),
|
| 314 |
+
icharRngUpper(ord('Z')),
|
| 315 |
+
icharAlt(ord('0')),
|
| 316 |
+
icharRngUpper(ord('9')),
|
| 317 |
+
iref(1),
|
| 318 |
+
ialt(),
|
| 319 |
+
iend(),
|
| 320 |
+
],
|
| 321 |
+
[
|
| 322 |
+
[
|
| 323 |
+
new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), makeRange('0', '9')]),
|
| 324 |
+
new RuleRef(1),
|
| 325 |
+
new RuleEnd(),
|
| 326 |
+
],
|
| 327 |
+
[new RuleEnd()],
|
| 328 |
+
],
|
| 329 |
+
],
|
| 330 |
+
];
|
| 331 |
+
|
| 332 |
+
it.each(asteriskCases)(
|
| 333 |
+
'builds rule stack for a char with asterisk modifier: %s',
|
| 334 |
+
(_grammar, input, expected) => {
|
| 335 |
+
expect(buildRuleStack(input)).toStrictEqual(expected);
|
| 336 |
+
},
|
| 337 |
+
);
|
| 338 |
+
});
|
| 339 |
+
|
| 340 |
+
it('builds a char with ranges and single characters', () => {
|
| 341 |
+
const input: InternalRuleDef[] = [
|
| 342 |
+
ichar([ord('a')]),
|
| 343 |
+
icharRngUpper(ord('z')),
|
| 344 |
+
icharAlt(ord('A')),
|
| 345 |
+
icharRngUpper(ord('Z')),
|
| 346 |
+
icharAlt(ord('_')),
|
| 347 |
+
iend(),
|
| 348 |
+
];
|
| 349 |
+
|
| 350 |
+
expect(buildRuleStack(input)).toStrictEqual([
|
| 351 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z'), ord('_')]), new RuleEnd()],
|
| 352 |
+
]);
|
| 353 |
+
});
|
| 354 |
+
|
| 355 |
+
it('builds rule stack for situation root ws plus newline ws star', () => {
|
| 356 |
+
const input: InternalRuleDef[] = [
|
| 357 |
+
ichar([32]),
|
| 358 |
+
icharAlt(92),
|
| 359 |
+
icharAlt(110),
|
| 360 |
+
iref(4),
|
| 361 |
+
ialt(),
|
| 362 |
+
iend(),
|
| 363 |
+
];
|
| 364 |
+
|
| 365 |
+
expect(buildRuleStack(input)).toStrictEqual([
|
| 366 |
+
[new RuleChar([32, 92, 110]), new RuleRef(4), new RuleEnd()],
|
| 367 |
+
[new RuleEnd()],
|
| 368 |
+
]);
|
| 369 |
+
});
|
| 370 |
+
|
| 371 |
+
it('builds rule stack for situation root char plus char range', () => {
|
| 372 |
+
const input: InternalRuleDef[] = [
|
| 373 |
+
ichar([ord('a')]),
|
| 374 |
+
icharRngUpper(ord('z')),
|
| 375 |
+
icharAlt(ord('A')),
|
| 376 |
+
icharRngUpper(ord('Z')),
|
| 377 |
+
iref(1),
|
| 378 |
+
ialt(),
|
| 379 |
+
ichar([ord('a')]),
|
| 380 |
+
icharRngUpper(ord('z')),
|
| 381 |
+
icharAlt(ord('A')),
|
| 382 |
+
icharRngUpper(ord('Z')),
|
| 383 |
+
iend(),
|
| 384 |
+
];
|
| 385 |
+
|
| 386 |
+
expect(buildRuleStack(input)).toStrictEqual([
|
| 387 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleRef(1), new RuleEnd()],
|
| 388 |
+
[new RuleChar([makeRange('a', 'z'), makeRange('A', 'Z')]), new RuleEnd()],
|
| 389 |
+
]);
|
| 390 |
+
});
|
| 391 |
+
|
| 392 |
+
it('adds the end of the rule if it is missing', () => {
|
| 393 |
+
expect(buildRuleStack([ichar([ord('a')]), iref(1)])).toStrictEqual([
|
| 394 |
+
[new RuleChar([ord('a')]), new RuleRef(1), new RuleEnd()],
|
| 395 |
+
]);
|
| 396 |
+
});
|
| 397 |
+
|
| 398 |
+
it('does not share the values of the rules it is given', () => {
|
| 399 |
+
const input = ichar([ord('a')]);
|
| 400 |
+
const [[rule]] = buildRuleStack([input, icharAlt(ord('b'))]);
|
| 401 |
+
expect(rule).toStrictEqual(new RuleChar([ord('a'), ord('b')]));
|
| 402 |
+
expect(input.value).toEqual([ord('a')]);
|
| 403 |
+
});
|
| 404 |
+
|
| 405 |
+
describe('errors', () => {
|
| 406 |
+
it('raises on an alt without anything before it', () => {
|
| 407 |
+
expect(() => buildRuleStack([ialt(), ichar([ord('a')])])).toThrow(
|
| 408 |
+
'Encountered alt without anything before it',
|
| 409 |
+
);
|
| 410 |
+
expect(() => buildRuleStack([ichar([ord('a')]), ialt(), ialt()])).toThrow(
|
| 411 |
+
'Encountered alt without anything before it',
|
| 412 |
+
);
|
| 413 |
+
});
|
| 414 |
+
|
| 415 |
+
it('raises on a range that follows a range', () => {
|
| 416 |
+
expect(() =>
|
| 417 |
+
buildRuleStack([ichar([ord('a')]), icharRngUpper(ord('b')), icharRngUpper(ord('c'))]),
|
| 418 |
+
).toThrow('Unexpected range, expected a number but got an array: [97,98]');
|
| 419 |
+
});
|
| 420 |
+
|
| 421 |
+
it('raises on a char alt that has no char before it', () => {
|
| 422 |
+
expect(() => buildRuleStack([icharAlt(ord('a'))])).toThrow(
|
| 423 |
+
'Encountered char alt, should be handled by above block',
|
| 424 |
+
);
|
| 425 |
+
});
|
| 426 |
+
|
| 427 |
+
it('raises on a range upper that has no char before it', () => {
|
| 428 |
+
expect(() => buildRuleStack([icharRngUpper(ord('a'))])).toThrow('Unsupported rule type');
|
| 429 |
+
});
|
| 430 |
+
});
|
| 431 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/grammar-parser/build-rule-stack.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
type Range,
|
| 3 |
+
RuleChar,
|
| 4 |
+
RuleCharExclude,
|
| 5 |
+
RuleEnd,
|
| 6 |
+
type UnresolvedRule,
|
| 7 |
+
} from '../grammar-graph/grammar-graph-types.js';
|
| 8 |
+
import { RuleRef } from '../grammar-graph/rule-ref.js';
|
| 9 |
+
import { isRange, isRuleEnd } from '../grammar-graph/type-guards.js';
|
| 10 |
+
import {
|
| 11 |
+
type InternalRuleDef,
|
| 12 |
+
type InternalRuleDefChar,
|
| 13 |
+
type InternalRuleDefCharNot,
|
| 14 |
+
isRuleDefAlt,
|
| 15 |
+
isRuleDefChar,
|
| 16 |
+
isRuleDefCharAlt,
|
| 17 |
+
isRuleDefCharNot,
|
| 18 |
+
isRuleDefCharRngUpper,
|
| 19 |
+
isRuleDefEnd,
|
| 20 |
+
isRuleDefRef,
|
| 21 |
+
} from '../rules-builder/rules-builder-types.js';
|
| 22 |
+
|
| 23 |
+
export function makeCharRule(
|
| 24 |
+
ruleDef: InternalRuleDefChar | InternalRuleDefCharNot,
|
| 25 |
+
): RuleChar | RuleCharExclude {
|
| 26 |
+
if (isRuleDefCharNot(ruleDef)) {
|
| 27 |
+
return new RuleCharExclude(ruleDef.value);
|
| 28 |
+
}
|
| 29 |
+
if (isRuleDefChar(ruleDef)) {
|
| 30 |
+
return new RuleChar(ruleDef.value);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
throw new Error(`Unsupported rule type for make_char_rule: ${JSON.stringify(ruleDef)}`);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/**
|
| 37 |
+
* Turns the linear definition of one rule into its stack: one path of rules per alternate,
|
| 38 |
+
* each of which ends with a RuleEnd.
|
| 39 |
+
*/
|
| 40 |
+
export function buildRuleStack(linearRules: InternalRuleDef[]): UnresolvedRule[][] {
|
| 41 |
+
let paths: UnresolvedRule[] = [];
|
| 42 |
+
const stack: UnresolvedRule[][] = [];
|
| 43 |
+
let idx = 0;
|
| 44 |
+
|
| 45 |
+
while (idx < linearRules.length) {
|
| 46 |
+
const ruleDef = linearRules[idx];
|
| 47 |
+
if (isRuleDefChar(ruleDef) || isRuleDefCharNot(ruleDef)) {
|
| 48 |
+
// this could be a single char, or a range, or a sequence of alts;
|
| 49 |
+
// we don't know until we step through it.
|
| 50 |
+
const charRule = makeCharRule(ruleDef);
|
| 51 |
+
idx += 1;
|
| 52 |
+
let rule: InternalRuleDef | undefined = linearRules[idx];
|
| 53 |
+
while (idx < linearRules.length && (isRuleDefCharRngUpper(rule) || isRuleDefCharAlt(rule))) {
|
| 54 |
+
if (isRuleDefCharRngUpper(rule)) {
|
| 55 |
+
// previous rule value should be a number
|
| 56 |
+
const prevValue = charRule.value.pop();
|
| 57 |
+
if (isRange(prevValue)) {
|
| 58 |
+
throw new Error(
|
| 59 |
+
`Unexpected range, expected a number but got an array: ${JSON.stringify(prevValue)}`,
|
| 60 |
+
);
|
| 61 |
+
}
|
| 62 |
+
if (prevValue === undefined) {
|
| 63 |
+
throw new Error('Unexpected undefined value');
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
charRule.value.push([prevValue, rule.value] as Range);
|
| 67 |
+
}
|
| 68 |
+
if (isRuleDefCharAlt(rule)) {
|
| 69 |
+
charRule.value.push(rule.value);
|
| 70 |
+
}
|
| 71 |
+
idx += 1;
|
| 72 |
+
rule = linearRules[idx];
|
| 73 |
+
}
|
| 74 |
+
paths.push(charRule);
|
| 75 |
+
} else {
|
| 76 |
+
if (isRuleDefAlt(ruleDef)) {
|
| 77 |
+
if (paths.length === 0) {
|
| 78 |
+
throw new Error('Encountered alt without anything before it');
|
| 79 |
+
}
|
| 80 |
+
paths.push(new RuleEnd());
|
| 81 |
+
stack.push(paths);
|
| 82 |
+
paths = [];
|
| 83 |
+
} else if (isRuleDefEnd(ruleDef)) {
|
| 84 |
+
paths.push(new RuleEnd());
|
| 85 |
+
} else if (isRuleDefRef(ruleDef)) {
|
| 86 |
+
paths.push(new RuleRef(ruleDef.value));
|
| 87 |
+
} else if (isRuleDefCharAlt(ruleDef)) {
|
| 88 |
+
throw new Error(
|
| 89 |
+
`Encountered char alt, should be handled by above block: ${JSON.stringify(ruleDef)}`,
|
| 90 |
+
);
|
| 91 |
+
} else {
|
| 92 |
+
throw new Error(`Unsupported rule type: ${JSON.stringify(ruleDef)}`);
|
| 93 |
+
}
|
| 94 |
+
idx += 1;
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (!isRuleEnd(paths[paths.length - 1])) {
|
| 99 |
+
paths.push(new RuleEnd());
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
stack.push(paths);
|
| 103 |
+
return stack;
|
| 104 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/index.test.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import GBNFDefault, {
|
| 3 |
+
GBNF,
|
| 4 |
+
GrammarParseError,
|
| 5 |
+
InputParseError,
|
| 6 |
+
ParseState,
|
| 7 |
+
RuleChar,
|
| 8 |
+
RuleCharExclude,
|
| 9 |
+
RuleEnd,
|
| 10 |
+
} from './index.js';
|
| 11 |
+
|
| 12 |
+
describe('the package', () => {
|
| 13 |
+
it('exports the entry point', () => {
|
| 14 |
+
expect(typeof GBNF).toBe('function');
|
| 15 |
+
expect(GBNFDefault).toBe(GBNF);
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
it('exports the rules that parse states are made of', () => {
|
| 19 |
+
for (const rule of [RuleChar, RuleCharExclude, RuleEnd]) {
|
| 20 |
+
expect(typeof rule).toBe('function');
|
| 21 |
+
}
|
| 22 |
+
expect([...GBNF('root ::= "a"')][0]).toBeInstanceOf(RuleChar);
|
| 23 |
+
expect([...GBNF('root ::= [^a]')][0]).toBeInstanceOf(RuleCharExclude);
|
| 24 |
+
expect([...GBNF('root ::= "a"', 'a')][0]).toBeInstanceOf(RuleEnd);
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
it('exports the errors that are raised', () => {
|
| 28 |
+
expect(() => GBNF('')).toThrow(GrammarParseError);
|
| 29 |
+
expect(() => GBNF('root ::= "a"', 'b')).toThrow(InputParseError);
|
| 30 |
+
expect(new GrammarParseError('', 0, '')).toBeInstanceOf(Error);
|
| 31 |
+
expect(new InputParseError('', 0)).toBeInstanceOf(Error);
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
it('exports the parse state', () => {
|
| 35 |
+
expect(GBNF('root ::= "a"')).toBeInstanceOf(ParseState);
|
| 36 |
+
});
|
| 37 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { GBNF, GBNF as default } from './GBNF.js';
|
| 2 |
+
export { RuleChar, RuleCharExclude, RuleEnd } from './grammar-graph/grammar-graph-types.js';
|
| 3 |
+
export type { Range, ResolvedRule, ValidInput } from './grammar-graph/grammar-graph-types.js';
|
| 4 |
+
export { ParseState } from './grammar-graph/parse-state.js';
|
| 5 |
+
export { GrammarParseError, InputParseError } from './utils/errors/index.js';
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/grammar-source.test.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { toChars, toText } from './grammar-source.js';
|
| 3 |
+
|
| 4 |
+
describe('grammar source', () => {
|
| 5 |
+
it('splits text into code points', () => {
|
| 6 |
+
expect(toChars('a😀b')).toEqual(['a', '😀', 'b']);
|
| 7 |
+
});
|
| 8 |
+
|
| 9 |
+
it('leaves code points that are already split as they are', () => {
|
| 10 |
+
const chars = ['a', '😀', 'b'];
|
| 11 |
+
expect(toChars(chars)).toBe(chars);
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
it('turns code points back into the text', () => {
|
| 15 |
+
expect(toText(['a', '😀', 'b'])).toBe('a😀b');
|
| 16 |
+
expect(toText('a😀b')).toBe('a😀b');
|
| 17 |
+
});
|
| 18 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/grammar-source.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Grammars are parsed one Unicode code point at a time, and positions within them are counted in
|
| 3 |
+
* code points. A grammar is therefore handed around either as text, or as its code points
|
| 4 |
+
* (each one a string) so that indexing doesn't have to split the text over and over.
|
| 5 |
+
*/
|
| 6 |
+
export type GrammarSource = string | readonly string[];
|
| 7 |
+
|
| 8 |
+
export function toChars(src: GrammarSource): readonly string[] {
|
| 9 |
+
return typeof src === 'string' ? Array.from(src) : src;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export function toText(src: GrammarSource): string {
|
| 13 |
+
return typeof src === 'string' ? src : src.join('');
|
| 14 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/index.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { GrammarParseError, InputParseError } from '../utils/errors/index.js';
|
| 2 |
+
export { RulesBuilder } from './rules-builder.js';
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/is-word-char.test.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { isWordChar } from './is-word-char.js';
|
| 3 |
+
|
| 4 |
+
describe('isWordChar', () => {
|
| 5 |
+
it('should return true for lowercase letters', () => {
|
| 6 |
+
expect(isWordChar('a')).toBe(true);
|
| 7 |
+
expect(isWordChar('z')).toBe(true);
|
| 8 |
+
});
|
| 9 |
+
|
| 10 |
+
it('should return true for uppercase letters', () => {
|
| 11 |
+
expect(isWordChar('A')).toBe(true);
|
| 12 |
+
expect(isWordChar('Z')).toBe(true);
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
it('should return false for digits', () => {
|
| 16 |
+
expect(isWordChar('0')).toBe(false);
|
| 17 |
+
expect(isWordChar('9')).toBe(false);
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
it('should return false for non-word characters', () => {
|
| 21 |
+
expect(isWordChar('-')).toBe(false);
|
| 22 |
+
expect(isWordChar('@')).toBe(false);
|
| 23 |
+
expect(isWordChar('_')).toBe(false);
|
| 24 |
+
expect(isWordChar('?')).toBe(false);
|
| 25 |
+
expect(isWordChar(' ')).toBe(false);
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
it('should return false when there is no character', () => {
|
| 29 |
+
expect(isWordChar('')).toBe(false);
|
| 30 |
+
expect(isWordChar(undefined as unknown as string)).toBe(false);
|
| 31 |
+
});
|
| 32 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/is-word-char.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function isWordChar(c: string): boolean {
|
| 2 |
+
return typeof c === 'string' && /[a-zA-Z]/.test(c);
|
| 3 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-char.test.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 3 |
+
import { parseChar } from './parse-char.js';
|
| 4 |
+
|
| 5 |
+
const ord = (char: string): number => char.codePointAt(0) as number;
|
| 6 |
+
|
| 7 |
+
describe('parseChar', () => {
|
| 8 |
+
it.each([
|
| 9 |
+
['a', ord('a')],
|
| 10 |
+
['9', ord('9')],
|
| 11 |
+
])('parses the plain char %s', (char, codePoint) => {
|
| 12 |
+
const grammar = `root ::= "${char}" "foo"`;
|
| 13 |
+
expect(parseChar(grammar, 'root ::= "'.length)).toEqual([codePoint, 1]);
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
it.each([
|
| 17 |
+
['escaped 8-bit unicode char', '\\x2A', ord('\x2A'), 4],
|
| 18 |
+
['escaped 16-bit unicode char', '\\u006F', ord('o'), 6],
|
| 19 |
+
['escaped 32-bit unicode char', '\\U0001F4A9', 128169, 10],
|
| 20 |
+
['escaped lowercase hex digits', '\\x2a', 0x2a, 4],
|
| 21 |
+
['escaped tab char', '\\t', ord('\t'), 2],
|
| 22 |
+
['escaped new line char', '\\n', ord('\n'), 2],
|
| 23 |
+
['escaped \\r char', '\\r', ord('\r'), 2],
|
| 24 |
+
['escaped quote char', '\\"', ord('"'), 2],
|
| 25 |
+
['escaped [ char', '\\[', ord('['), 2],
|
| 26 |
+
['escaped ] char', '\\]', ord(']'), 2],
|
| 27 |
+
['escaped \\ char', '\\\\', ord('\\'), 2],
|
| 28 |
+
])('parses the %s', (_description, escapedChar, codePoint, incPos) => {
|
| 29 |
+
const grammar = `root ::= "${escapedChar}" "foo"`;
|
| 30 |
+
expect(parseChar(grammar, 'root ::= "'.length)).toEqual([codePoint, incPos]);
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
it.each([
|
| 34 |
+
['', 0],
|
| 35 |
+
['a', 1],
|
| 36 |
+
['a', 2],
|
| 37 |
+
])('raises for the input %j at the position %d', (input, pos) => {
|
| 38 |
+
expect(() => parseChar(input, pos)).toThrow(GrammarParseError);
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
it('raises for an unknown escape', () => {
|
| 42 |
+
expect(() => parseChar('\\q', 0)).toThrow(
|
| 43 |
+
new GrammarParseError('\\q', 0, 'Unknown escape at \\'),
|
| 44 |
+
);
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
it('raises for an escape at the very end of the grammar', () => {
|
| 48 |
+
expect(() => parseChar('\\', 0)).toThrow(GrammarParseError);
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
it.each(['\\xZZ', '\\x4', '\\x', '\\u12G4', '\\U0001F4G9', '\\x -1', '\\x+1', '\\x0x', '\\x1_'])(
|
| 52 |
+
'raises for the malformed hex escape %s',
|
| 53 |
+
(escapedChar) => {
|
| 54 |
+
expect(() => parseChar(`${escapedChar} `, 0)).toThrow(GrammarParseError);
|
| 55 |
+
},
|
| 56 |
+
);
|
| 57 |
+
|
| 58 |
+
it('counts characters outside of the basic multilingual plane as one', () => {
|
| 59 |
+
expect(parseChar('"😀"', 1)).toEqual([0x1f600, 1]);
|
| 60 |
+
expect(parseChar('"😀"', 2)).toEqual([ord('"'), 1]);
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
it('parses an array of characters in the same way as a string', () => {
|
| 64 |
+
expect(parseChar(Array.from('"😀"'), 1)).toEqual([0x1f600, 1]);
|
| 65 |
+
});
|
| 66 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-char.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 2 |
+
import { type GrammarSource, toChars, toText } from './grammar-source.js';
|
| 3 |
+
|
| 4 |
+
const HEX_ESCAPE_LENGTHS: Readonly<Record<string, number>> = { x: 2, u: 4, U: 8 };
|
| 5 |
+
|
| 6 |
+
const CONTROL_ESCAPES: Readonly<Record<string, number>> = { t: 0x09, r: 0x0d, n: 0x0a };
|
| 7 |
+
|
| 8 |
+
const LITERAL_ESCAPES = ['"', '[', ']', '\\'];
|
| 9 |
+
|
| 10 |
+
/**
|
| 11 |
+
* Parses the (possibly escaped) character at `pos`.
|
| 12 |
+
*
|
| 13 |
+
* @returns the code point, and the number of characters of `src` that were used to express it
|
| 14 |
+
*/
|
| 15 |
+
export function parseChar(src: GrammarSource, pos: number): [codePoint: number, length: number] {
|
| 16 |
+
const chars = toChars(src);
|
| 17 |
+
if (pos >= chars.length) {
|
| 18 |
+
throw new GrammarParseError(
|
| 19 |
+
toText(src),
|
| 20 |
+
pos,
|
| 21 |
+
'Unexpected end of grammar input, failed to complete parse',
|
| 22 |
+
);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
if (chars[pos] === '\\') {
|
| 26 |
+
const escape = chars[pos + 1];
|
| 27 |
+
const hexLength = HEX_ESCAPE_LENGTHS[escape];
|
| 28 |
+
if (hexLength !== undefined) {
|
| 29 |
+
const digits = chars.slice(pos + 2, pos + 2 + hexLength).join('');
|
| 30 |
+
if (digits.length !== hexLength || !/^[0-9a-fA-F]+$/.test(digits)) {
|
| 31 |
+
throw new GrammarParseError(
|
| 32 |
+
toText(src),
|
| 33 |
+
pos,
|
| 34 |
+
`Invalid hexadecimal escape \\${escape}${digits}`,
|
| 35 |
+
);
|
| 36 |
+
}
|
| 37 |
+
return [parseInt(digits, 16), 2 + hexLength];
|
| 38 |
+
}
|
| 39 |
+
const controlCharacter = CONTROL_ESCAPES[escape];
|
| 40 |
+
if (controlCharacter !== undefined) {
|
| 41 |
+
return [controlCharacter, 2];
|
| 42 |
+
}
|
| 43 |
+
if (LITERAL_ESCAPES.includes(escape)) {
|
| 44 |
+
return [escape.codePointAt(0) as number, 2];
|
| 45 |
+
}
|
| 46 |
+
throw new GrammarParseError(toText(src), pos, `Unknown escape at ${chars[pos]}`);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
return [chars[pos].codePointAt(0) as number, 1];
|
| 50 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-name.test.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 3 |
+
import { PARSE_NAME_ERROR, parseName } from './parse-name.js';
|
| 4 |
+
|
| 5 |
+
describe('parseName', () => {
|
| 6 |
+
it('should return correct name when encountering a valid name', () => {
|
| 7 |
+
const src = 'validName';
|
| 8 |
+
const name = parseName(src, 0);
|
| 9 |
+
expect(name).toBe(src);
|
| 10 |
+
});
|
| 11 |
+
|
| 12 |
+
it('should return correct name when a valid name starts at a non-zero position', () => {
|
| 13 |
+
const src = '123validName';
|
| 14 |
+
const name = parseName(src, 3);
|
| 15 |
+
expect(name).toBe('validName');
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
it('should allow dashes and underscores', () => {
|
| 19 |
+
expect(parseName('foo-bar_baz ::=', 0)).toBe('foo-bar_baz');
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
it('should stop at digits', () => {
|
| 23 |
+
expect(parseName('foo1', 0)).toBe('foo');
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
it('should throw error when encountering an invalid name', () => {
|
| 27 |
+
const src = '123';
|
| 28 |
+
expect(() => parseName(src, 0)).toThrow(GrammarParseError);
|
| 29 |
+
expect(() => parseName(src, 0)).toThrow(new GrammarParseError(src, 0, PARSE_NAME_ERROR));
|
| 30 |
+
});
|
| 31 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-name.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 2 |
+
import { type GrammarSource, toChars, toText } from './grammar-source.js';
|
| 3 |
+
import { isWordChar } from './is-word-char.js';
|
| 4 |
+
|
| 5 |
+
export const PARSE_NAME_ERROR = 'Failed to find a valid name';
|
| 6 |
+
|
| 7 |
+
export const VALID_NAME_SEPARATORS = ['-', '_'];
|
| 8 |
+
|
| 9 |
+
export function parseName(grammar: GrammarSource, pos: number): string {
|
| 10 |
+
const chars = toChars(grammar);
|
| 11 |
+
let name = '';
|
| 12 |
+
while (
|
| 13 |
+
pos < chars.length &&
|
| 14 |
+
(isWordChar(chars[pos]) || VALID_NAME_SEPARATORS.includes(chars[pos]))
|
| 15 |
+
) {
|
| 16 |
+
name += chars[pos];
|
| 17 |
+
pos += 1;
|
| 18 |
+
}
|
| 19 |
+
if (!name) {
|
| 20 |
+
throw new GrammarParseError(toText(grammar), pos, PARSE_NAME_ERROR);
|
| 21 |
+
}
|
| 22 |
+
return name;
|
| 23 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-space.test.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { parseSpace } from './parse-space.js';
|
| 3 |
+
|
| 4 |
+
describe('parseSpace', () => {
|
| 5 |
+
it('should return input string when no whitespace or comments', () => {
|
| 6 |
+
const input = 'abcdefghijk';
|
| 7 |
+
const pos = parseSpace(input, 0, true);
|
| 8 |
+
expect(input.slice(pos)).toBe(input);
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
it('should skip leading spaces and tabs', () => {
|
| 12 |
+
const input = ' \t abcdefghijk';
|
| 13 |
+
const pos = parseSpace(input, 0, true);
|
| 14 |
+
expect(input.slice(pos)).toBe('abcdefghijk');
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
it('should skip leading newline characters when newlineOk is true', () => {
|
| 18 |
+
const input = '\n\n\r\n\r\nabcdefghijk';
|
| 19 |
+
const pos = parseSpace(input, 0, true);
|
| 20 |
+
expect(input.slice(pos)).toBe('abcdefghijk');
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
it('should not skip leading newline characters when newlineOk is false', () => {
|
| 24 |
+
const input = '\n\n\r\n\r\nabcdefghijk';
|
| 25 |
+
const pos = parseSpace(input, 0, false);
|
| 26 |
+
expect(input.slice(pos)).toBe('\n\n\r\n\r\nabcdefghijk');
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
it('should skip comments and leading spaces', () => {
|
| 30 |
+
const input = ' # This is a comment\n\t abcdefghijk';
|
| 31 |
+
const pos = parseSpace(input, 0, true);
|
| 32 |
+
expect(input.slice(pos)).toBe('abcdefghijk');
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
it('should skip comments and leading newline characters when newlineOk is true', () => {
|
| 36 |
+
const input = '\n\n # This is a comment\n\r\n\r\nabcdefghijk';
|
| 37 |
+
const pos = parseSpace(input, 0, true);
|
| 38 |
+
expect(input.slice(pos)).toBe('abcdefghijk');
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
it('should skip comments but stop at the newline after them when newlineOk is false', () => {
|
| 42 |
+
const input = '\n\n # This is a comment\n\r\n\r\nabcdefghijk';
|
| 43 |
+
const pos = parseSpace(input, 0, false);
|
| 44 |
+
expect(input.slice(pos)).toBe('\n\n # This is a comment\n\r\n\r\nabcdefghijk');
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
it('should return an empty string if input is all whitespace and comments', () => {
|
| 48 |
+
const input = ' \t# Comment\n# Another comment\n\n';
|
| 49 |
+
const pos = parseSpace(input, 0, true);
|
| 50 |
+
expect(input.slice(pos)).toBe('');
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
it('should return an empty string for an empty input string', () => {
|
| 54 |
+
const input = '';
|
| 55 |
+
const pos = parseSpace(input, 0, true);
|
| 56 |
+
expect(input.slice(pos)).toBe('');
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
it('should skip a comment that runs to the end of the input', () => {
|
| 60 |
+
const input = 'a # comment';
|
| 61 |
+
expect(parseSpace(input, 1, false)).toBe(input.length);
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
it('should skip comments in the same way for an array of characters', () => {
|
| 65 |
+
const chars = Array.from('😀 # 😀\nab');
|
| 66 |
+
expect(parseSpace(chars, 1, true)).toBe(chars.length - 2);
|
| 67 |
+
});
|
| 68 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/parse-space.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type GrammarSource, toChars } from './grammar-source.js';
|
| 2 |
+
|
| 3 |
+
/** Skips spaces, tabs and comments (and newlines, if `newlineOk`), returning the next position. */
|
| 4 |
+
export function parseSpace(src: GrammarSource, pos: number, newlineOk: boolean): number {
|
| 5 |
+
const chars = toChars(src);
|
| 6 |
+
while (
|
| 7 |
+
pos < chars.length &&
|
| 8 |
+
(chars[pos] === ' ' ||
|
| 9 |
+
chars[pos] === '\t' ||
|
| 10 |
+
chars[pos] === '#' ||
|
| 11 |
+
(newlineOk && (chars[pos] === '\r' || chars[pos] === '\n')))
|
| 12 |
+
) {
|
| 13 |
+
if (chars[pos] === '#') {
|
| 14 |
+
while (pos < chars.length && chars[pos] !== '\r' && chars[pos] !== '\n') {
|
| 15 |
+
pos += 1;
|
| 16 |
+
}
|
| 17 |
+
} else {
|
| 18 |
+
pos += 1;
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
return pos;
|
| 22 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder-types.test.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import {
|
| 3 |
+
InternalRuleDefAlt,
|
| 4 |
+
InternalRuleDefChar,
|
| 5 |
+
InternalRuleDefCharAlt,
|
| 6 |
+
InternalRuleDefCharNot,
|
| 7 |
+
InternalRuleDefCharRngUpper,
|
| 8 |
+
InternalRuleDefEnd,
|
| 9 |
+
InternalRuleDefReference,
|
| 10 |
+
isRuleDefAlt,
|
| 11 |
+
isRuleDefChar,
|
| 12 |
+
isRuleDefCharAlt,
|
| 13 |
+
isRuleDefCharNot,
|
| 14 |
+
isRuleDefCharRngUpper,
|
| 15 |
+
isRuleDefEnd,
|
| 16 |
+
isRuleDefRef,
|
| 17 |
+
} from './rules-builder-types.js';
|
| 18 |
+
|
| 19 |
+
describe('rules builder types', () => {
|
| 20 |
+
const rules = {
|
| 21 |
+
char: new InternalRuleDefChar([97]),
|
| 22 |
+
charNot: new InternalRuleDefCharNot([97]),
|
| 23 |
+
charAlt: new InternalRuleDefCharAlt(97),
|
| 24 |
+
charRngUpper: new InternalRuleDefCharRngUpper(122),
|
| 25 |
+
ref: new InternalRuleDefReference(1),
|
| 26 |
+
alt: new InternalRuleDefAlt(),
|
| 27 |
+
end: new InternalRuleDefEnd(),
|
| 28 |
+
};
|
| 29 |
+
|
| 30 |
+
it('copies the values it is given', () => {
|
| 31 |
+
const value = [97];
|
| 32 |
+
const rule = new InternalRuleDefChar(value);
|
| 33 |
+
value.push(98);
|
| 34 |
+
expect(rule.value).toEqual([97]);
|
| 35 |
+
expect(new InternalRuleDefCharNot(value).value).not.toBe(value);
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
it('tells the kinds of rule apart', () => {
|
| 39 |
+
const guards = {
|
| 40 |
+
char: isRuleDefChar,
|
| 41 |
+
charNot: isRuleDefCharNot,
|
| 42 |
+
charAlt: isRuleDefCharAlt,
|
| 43 |
+
charRngUpper: isRuleDefCharRngUpper,
|
| 44 |
+
ref: isRuleDefRef,
|
| 45 |
+
alt: isRuleDefAlt,
|
| 46 |
+
end: isRuleDefEnd,
|
| 47 |
+
};
|
| 48 |
+
for (const [guardName, guard] of Object.entries(guards)) {
|
| 49 |
+
for (const [ruleName, rule] of Object.entries(rules)) {
|
| 50 |
+
expect(guard(rule), `${guardName} guard for a ${ruleName} rule`).toBe(
|
| 51 |
+
guardName === ruleName,
|
| 52 |
+
);
|
| 53 |
+
}
|
| 54 |
+
expect(guard(undefined)).toBe(false);
|
| 55 |
+
expect(guard({})).toBe(false);
|
| 56 |
+
}
|
| 57 |
+
});
|
| 58 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder-types.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// The intermediate representation of a grammar: every rule is a flat list of these elements,
|
| 2 |
+
// which `buildRuleStack` later turns into paths of the rules that the graph is made of.
|
| 3 |
+
|
| 4 |
+
/**
|
| 5 |
+
* `[a-z]` is represented as a `Char` followed by a `CharRngUpper`,
|
| 6 |
+
* and `[ab]` as a `Char` followed by a `CharAlt`.
|
| 7 |
+
*/
|
| 8 |
+
export class InternalRuleDefChar {
|
| 9 |
+
readonly type = 'char';
|
| 10 |
+
value: number[];
|
| 11 |
+
|
| 12 |
+
constructor(value: number[] = []) {
|
| 13 |
+
this.value = [...value];
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export class InternalRuleDefCharNot {
|
| 18 |
+
readonly type = 'char_not';
|
| 19 |
+
value: number[];
|
| 20 |
+
|
| 21 |
+
constructor(value: number[] = []) {
|
| 22 |
+
this.value = [...value];
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
export class InternalRuleDefCharAlt {
|
| 27 |
+
readonly type = 'char_alt';
|
| 28 |
+
value: number;
|
| 29 |
+
|
| 30 |
+
constructor(value: number) {
|
| 31 |
+
this.value = value;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
export class InternalRuleDefCharRngUpper {
|
| 36 |
+
readonly type = 'char_rng_upper';
|
| 37 |
+
value: number;
|
| 38 |
+
|
| 39 |
+
constructor(value: number) {
|
| 40 |
+
this.value = value;
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
export class InternalRuleDefReference {
|
| 45 |
+
readonly type = 'ref';
|
| 46 |
+
value: number;
|
| 47 |
+
|
| 48 |
+
constructor(value: number) {
|
| 49 |
+
this.value = value;
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export class InternalRuleDefAlt {
|
| 54 |
+
readonly type = 'alt';
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
export class InternalRuleDefEnd {
|
| 58 |
+
readonly type = 'end';
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
export type InternalRuleDef =
|
| 62 |
+
| InternalRuleDefChar
|
| 63 |
+
| InternalRuleDefCharNot
|
| 64 |
+
| InternalRuleDefCharAlt
|
| 65 |
+
| InternalRuleDefCharRngUpper
|
| 66 |
+
| InternalRuleDefReference
|
| 67 |
+
| InternalRuleDefAlt
|
| 68 |
+
| InternalRuleDefEnd;
|
| 69 |
+
|
| 70 |
+
export type InternalRuleDefCharOrAltChar = InternalRuleDefChar | InternalRuleDefCharAlt;
|
| 71 |
+
|
| 72 |
+
function hasType(rule: unknown, type: InternalRuleDef['type']): boolean {
|
| 73 |
+
return typeof rule === 'object' && rule !== null && (rule as { type?: unknown }).type === type;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
export function isRuleDefAlt(rule: unknown): rule is InternalRuleDefAlt {
|
| 77 |
+
return hasType(rule, 'alt');
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
export function isRuleDefRef(rule: unknown): rule is InternalRuleDefReference {
|
| 81 |
+
return hasType(rule, 'ref');
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
export function isRuleDefEnd(rule: unknown): rule is InternalRuleDefEnd {
|
| 85 |
+
return hasType(rule, 'end');
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
export function isRuleDefChar(rule: unknown): rule is InternalRuleDefChar {
|
| 89 |
+
return hasType(rule, 'char');
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
export function isRuleDefCharNot(rule: unknown): rule is InternalRuleDefCharNot {
|
| 93 |
+
return hasType(rule, 'char_not');
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
export function isRuleDefCharAlt(rule: unknown): rule is InternalRuleDefCharAlt {
|
| 97 |
+
return hasType(rule, 'char_alt');
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
export function isRuleDefCharRngUpper(rule: unknown): rule is InternalRuleDefCharRngUpper {
|
| 101 |
+
return hasType(rule, 'char_rng_upper');
|
| 102 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder.test.ts
ADDED
|
@@ -0,0 +1,1399 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 3 |
+
import { RulesBuilder } from './rules-builder.js';
|
| 4 |
+
import {
|
| 5 |
+
type InternalRuleDef,
|
| 6 |
+
InternalRuleDefAlt,
|
| 7 |
+
InternalRuleDefChar,
|
| 8 |
+
InternalRuleDefCharAlt,
|
| 9 |
+
InternalRuleDefCharNot,
|
| 10 |
+
InternalRuleDefCharRngUpper,
|
| 11 |
+
InternalRuleDefEnd,
|
| 12 |
+
InternalRuleDefReference,
|
| 13 |
+
} from './rules-builder-types.js';
|
| 14 |
+
|
| 15 |
+
const ord = (char: string): number => char.codePointAt(0) as number;
|
| 16 |
+
|
| 17 |
+
interface TestCase {
|
| 18 |
+
key: string;
|
| 19 |
+
grammar: string;
|
| 20 |
+
symbolIds: Array<[string, number]>;
|
| 21 |
+
rules: InternalRuleDef[][];
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
const testCases: TestCase[] = [
|
| 25 |
+
{
|
| 26 |
+
key: 'single-string',
|
| 27 |
+
grammar: 'root ::= "foo"',
|
| 28 |
+
symbolIds: [['root', 0]],
|
| 29 |
+
rules: [
|
| 30 |
+
[
|
| 31 |
+
new InternalRuleDefChar([ord('f')]),
|
| 32 |
+
new InternalRuleDefChar([ord('o')]),
|
| 33 |
+
new InternalRuleDefChar([ord('o')]),
|
| 34 |
+
new InternalRuleDefEnd(),
|
| 35 |
+
],
|
| 36 |
+
],
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
key: 'quote character',
|
| 40 |
+
grammar: 'root ::= "\\""',
|
| 41 |
+
symbolIds: [['root', 0]],
|
| 42 |
+
rules: [[new InternalRuleDefChar([34]), new InternalRuleDefEnd()]],
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
key: 'two-lines-referencing-expression',
|
| 46 |
+
grammar: 'root ::= foo\n foo ::= "bar"',
|
| 47 |
+
symbolIds: [['root', 0], ['foo', 1]],
|
| 48 |
+
rules: [
|
| 49 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 50 |
+
[
|
| 51 |
+
new InternalRuleDefChar([ord('b')]),
|
| 52 |
+
new InternalRuleDefChar([ord('a')]),
|
| 53 |
+
new InternalRuleDefChar([ord('r')]),
|
| 54 |
+
new InternalRuleDefEnd(),
|
| 55 |
+
],
|
| 56 |
+
],
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
key: 'expression with dash',
|
| 60 |
+
grammar: 'root ::= foo-bar\n foo-bar ::= "bar"',
|
| 61 |
+
symbolIds: [['root', 0], ['foo-bar', 1]],
|
| 62 |
+
rules: [
|
| 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 |
+
key: 'simple-grammar',
|
| 74 |
+
grammar: `
|
| 75 |
+
root ::= (expr "=" term "
|
| 76 |
+
")+
|
| 77 |
+
expr ::= term ([-+*/] term)*
|
| 78 |
+
term ::= [0-9]+
|
| 79 |
+
`,
|
| 80 |
+
symbolIds: [
|
| 81 |
+
['root', 0],
|
| 82 |
+
['root_1', 1],
|
| 83 |
+
['expr', 2],
|
| 84 |
+
['term', 3],
|
| 85 |
+
['root_4', 4],
|
| 86 |
+
['expr_5', 5],
|
| 87 |
+
['expr_6', 6],
|
| 88 |
+
['term_7', 7],
|
| 89 |
+
],
|
| 90 |
+
rules: [
|
| 91 |
+
[new InternalRuleDefReference(4), new InternalRuleDefEnd()],
|
| 92 |
+
[
|
| 93 |
+
new InternalRuleDefReference(2),
|
| 94 |
+
new InternalRuleDefChar([61]),
|
| 95 |
+
new InternalRuleDefReference(3),
|
| 96 |
+
new InternalRuleDefChar([10]),
|
| 97 |
+
new InternalRuleDefEnd(),
|
| 98 |
+
],
|
| 99 |
+
[new InternalRuleDefReference(3), new InternalRuleDefReference(6), new InternalRuleDefEnd()],
|
| 100 |
+
[new InternalRuleDefReference(7), new InternalRuleDefEnd()],
|
| 101 |
+
[
|
| 102 |
+
new InternalRuleDefReference(1),
|
| 103 |
+
new InternalRuleDefReference(4),
|
| 104 |
+
new InternalRuleDefAlt(),
|
| 105 |
+
new InternalRuleDefReference(1),
|
| 106 |
+
new InternalRuleDefEnd(),
|
| 107 |
+
],
|
| 108 |
+
[
|
| 109 |
+
new InternalRuleDefChar([45]),
|
| 110 |
+
new InternalRuleDefCharAlt(43),
|
| 111 |
+
new InternalRuleDefCharAlt(42),
|
| 112 |
+
new InternalRuleDefCharAlt(47),
|
| 113 |
+
new InternalRuleDefReference(3),
|
| 114 |
+
new InternalRuleDefEnd(),
|
| 115 |
+
],
|
| 116 |
+
[
|
| 117 |
+
new InternalRuleDefReference(5),
|
| 118 |
+
new InternalRuleDefReference(6),
|
| 119 |
+
new InternalRuleDefAlt(),
|
| 120 |
+
new InternalRuleDefEnd(),
|
| 121 |
+
],
|
| 122 |
+
[
|
| 123 |
+
new InternalRuleDefChar([ord('0')]),
|
| 124 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 125 |
+
new InternalRuleDefReference(7),
|
| 126 |
+
new InternalRuleDefAlt(),
|
| 127 |
+
new InternalRuleDefChar([ord('0')]),
|
| 128 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 129 |
+
new InternalRuleDefEnd(),
|
| 130 |
+
],
|
| 131 |
+
],
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
key: 'longer-grammar',
|
| 135 |
+
grammar: `
|
| 136 |
+
root ::= (expr "=" ws term "
|
| 137 |
+
")+
|
| 138 |
+
expr ::= term ([-+*/] term)*
|
| 139 |
+
term ::= ident | num | "(" ws expr ")" ws
|
| 140 |
+
ident ::= [a-z] [a-z0-9_]* ws
|
| 141 |
+
num ::= [0-9]+ ws
|
| 142 |
+
ws ::= [ \t
|
| 143 |
+
]*
|
| 144 |
+
`,
|
| 145 |
+
symbolIds: [
|
| 146 |
+
['root', 0],
|
| 147 |
+
['root_1', 1],
|
| 148 |
+
['expr', 2],
|
| 149 |
+
['ws', 3],
|
| 150 |
+
['term', 4],
|
| 151 |
+
['root_5', 5],
|
| 152 |
+
['expr_6', 6],
|
| 153 |
+
['expr_7', 7],
|
| 154 |
+
['ident', 8],
|
| 155 |
+
['num', 9],
|
| 156 |
+
['ident_10', 10],
|
| 157 |
+
['num_11', 11],
|
| 158 |
+
['ws_12', 12],
|
| 159 |
+
],
|
| 160 |
+
rules: [
|
| 161 |
+
[new InternalRuleDefReference(5), new InternalRuleDefEnd()],
|
| 162 |
+
[
|
| 163 |
+
new InternalRuleDefReference(2),
|
| 164 |
+
new InternalRuleDefChar([61]),
|
| 165 |
+
new InternalRuleDefReference(3),
|
| 166 |
+
new InternalRuleDefReference(4),
|
| 167 |
+
new InternalRuleDefChar([10]),
|
| 168 |
+
new InternalRuleDefEnd(),
|
| 169 |
+
],
|
| 170 |
+
[new InternalRuleDefReference(4), new InternalRuleDefReference(7), new InternalRuleDefEnd()],
|
| 171 |
+
[new InternalRuleDefReference(12), new InternalRuleDefEnd()],
|
| 172 |
+
[
|
| 173 |
+
new InternalRuleDefReference(8),
|
| 174 |
+
new InternalRuleDefAlt(),
|
| 175 |
+
new InternalRuleDefReference(9),
|
| 176 |
+
new InternalRuleDefAlt(),
|
| 177 |
+
new InternalRuleDefChar([40]),
|
| 178 |
+
new InternalRuleDefReference(3),
|
| 179 |
+
new InternalRuleDefReference(2),
|
| 180 |
+
new InternalRuleDefChar([41]),
|
| 181 |
+
new InternalRuleDefReference(3),
|
| 182 |
+
new InternalRuleDefEnd(),
|
| 183 |
+
],
|
| 184 |
+
[
|
| 185 |
+
new InternalRuleDefReference(1),
|
| 186 |
+
new InternalRuleDefReference(5),
|
| 187 |
+
new InternalRuleDefAlt(),
|
| 188 |
+
new InternalRuleDefReference(1),
|
| 189 |
+
new InternalRuleDefEnd(),
|
| 190 |
+
],
|
| 191 |
+
[
|
| 192 |
+
new InternalRuleDefChar([45]),
|
| 193 |
+
new InternalRuleDefCharAlt(43),
|
| 194 |
+
new InternalRuleDefCharAlt(42),
|
| 195 |
+
new InternalRuleDefCharAlt(47),
|
| 196 |
+
new InternalRuleDefReference(4),
|
| 197 |
+
new InternalRuleDefEnd(),
|
| 198 |
+
],
|
| 199 |
+
[
|
| 200 |
+
new InternalRuleDefReference(6),
|
| 201 |
+
new InternalRuleDefReference(7),
|
| 202 |
+
new InternalRuleDefAlt(),
|
| 203 |
+
new InternalRuleDefEnd(),
|
| 204 |
+
],
|
| 205 |
+
[
|
| 206 |
+
new InternalRuleDefChar([ord('a')]),
|
| 207 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 208 |
+
new InternalRuleDefReference(10),
|
| 209 |
+
new InternalRuleDefReference(3),
|
| 210 |
+
new InternalRuleDefEnd(),
|
| 211 |
+
],
|
| 212 |
+
[new InternalRuleDefReference(11), new InternalRuleDefReference(3), new InternalRuleDefEnd()],
|
| 213 |
+
[
|
| 214 |
+
new InternalRuleDefChar([ord('a')]),
|
| 215 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 216 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 217 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 218 |
+
new InternalRuleDefCharAlt(95),
|
| 219 |
+
new InternalRuleDefReference(10),
|
| 220 |
+
new InternalRuleDefAlt(),
|
| 221 |
+
new InternalRuleDefEnd(),
|
| 222 |
+
],
|
| 223 |
+
[
|
| 224 |
+
new InternalRuleDefChar([ord('0')]),
|
| 225 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 226 |
+
new InternalRuleDefReference(11),
|
| 227 |
+
new InternalRuleDefAlt(),
|
| 228 |
+
new InternalRuleDefChar([ord('0')]),
|
| 229 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 230 |
+
new InternalRuleDefEnd(),
|
| 231 |
+
],
|
| 232 |
+
[
|
| 233 |
+
new InternalRuleDefChar([32]),
|
| 234 |
+
new InternalRuleDefCharAlt(9),
|
| 235 |
+
new InternalRuleDefCharAlt(10),
|
| 236 |
+
new InternalRuleDefReference(12),
|
| 237 |
+
new InternalRuleDefAlt(),
|
| 238 |
+
new InternalRuleDefEnd(),
|
| 239 |
+
],
|
| 240 |
+
],
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
key: 'character unicode grammar',
|
| 244 |
+
grammar: 'root ::= [ぁ-ゟ]',
|
| 245 |
+
symbolIds: [['root', 0]],
|
| 246 |
+
rules: [
|
| 247 |
+
[
|
| 248 |
+
new InternalRuleDefChar([12353]),
|
| 249 |
+
new InternalRuleDefCharRngUpper(12447),
|
| 250 |
+
new InternalRuleDefEnd(),
|
| 251 |
+
],
|
| 252 |
+
],
|
| 253 |
+
},
|
| 254 |
+
{
|
| 255 |
+
key: 'character alts grammar',
|
| 256 |
+
grammar: 'root ::= [az]',
|
| 257 |
+
symbolIds: [['root', 0]],
|
| 258 |
+
rules: [
|
| 259 |
+
[
|
| 260 |
+
new InternalRuleDefChar([ord('a')]),
|
| 261 |
+
new InternalRuleDefCharAlt(ord('z')),
|
| 262 |
+
new InternalRuleDefEnd(),
|
| 263 |
+
],
|
| 264 |
+
],
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
key: 'character range grammar',
|
| 268 |
+
grammar: 'root ::= [a-zA-Z0-9]',
|
| 269 |
+
symbolIds: [['root', 0]],
|
| 270 |
+
rules: [
|
| 271 |
+
[
|
| 272 |
+
new InternalRuleDefChar([ord('a')]),
|
| 273 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 274 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 275 |
+
new InternalRuleDefCharRngUpper(ord('Z')),
|
| 276 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 277 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 278 |
+
new InternalRuleDefEnd(),
|
| 279 |
+
],
|
| 280 |
+
],
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
key: 'character range grammar with dash at end',
|
| 284 |
+
grammar: 'root ::= [a-z-]',
|
| 285 |
+
symbolIds: [['root', 0]],
|
| 286 |
+
rules: [
|
| 287 |
+
[
|
| 288 |
+
new InternalRuleDefChar([ord('a')]),
|
| 289 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 290 |
+
new InternalRuleDefCharAlt(45),
|
| 291 |
+
new InternalRuleDefEnd(),
|
| 292 |
+
],
|
| 293 |
+
],
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
key: 'grouping',
|
| 297 |
+
grammar: 'root ::= "f" ("b" | "a")',
|
| 298 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 299 |
+
rules: [
|
| 300 |
+
[
|
| 301 |
+
new InternalRuleDefChar([ord('f')]),
|
| 302 |
+
new InternalRuleDefReference(1),
|
| 303 |
+
new InternalRuleDefEnd(),
|
| 304 |
+
],
|
| 305 |
+
[
|
| 306 |
+
new InternalRuleDefChar([ord('b')]),
|
| 307 |
+
new InternalRuleDefAlt(),
|
| 308 |
+
new InternalRuleDefChar([ord('a')]),
|
| 309 |
+
new InternalRuleDefEnd(),
|
| 310 |
+
],
|
| 311 |
+
],
|
| 312 |
+
},
|
| 313 |
+
{
|
| 314 |
+
key: 'optional',
|
| 315 |
+
grammar: 'root ::= "f"?',
|
| 316 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 317 |
+
rules: [
|
| 318 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 319 |
+
[new InternalRuleDefChar([ord('f')]), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 320 |
+
],
|
| 321 |
+
},
|
| 322 |
+
{
|
| 323 |
+
key: 'repeating',
|
| 324 |
+
grammar: 'root ::= "f"*',
|
| 325 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 326 |
+
rules: [
|
| 327 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 328 |
+
[
|
| 329 |
+
new InternalRuleDefChar([ord('f')]),
|
| 330 |
+
new InternalRuleDefReference(1),
|
| 331 |
+
new InternalRuleDefAlt(),
|
| 332 |
+
new InternalRuleDefEnd(),
|
| 333 |
+
],
|
| 334 |
+
],
|
| 335 |
+
},
|
| 336 |
+
{
|
| 337 |
+
key: 'repeating-at-least-once',
|
| 338 |
+
grammar: 'root ::= "f"+',
|
| 339 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 340 |
+
rules: [
|
| 341 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 342 |
+
[
|
| 343 |
+
new InternalRuleDefChar([ord('f')]),
|
| 344 |
+
new InternalRuleDefReference(1),
|
| 345 |
+
new InternalRuleDefAlt(),
|
| 346 |
+
new InternalRuleDefChar([ord('f')]),
|
| 347 |
+
new InternalRuleDefEnd(),
|
| 348 |
+
],
|
| 349 |
+
],
|
| 350 |
+
},
|
| 351 |
+
{
|
| 352 |
+
key: 'character range with optional repeating',
|
| 353 |
+
grammar: 'root ::= [a-zA-Z0-9]*',
|
| 354 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 355 |
+
rules: [
|
| 356 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 357 |
+
[
|
| 358 |
+
new InternalRuleDefChar([ord('a')]),
|
| 359 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 360 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 361 |
+
new InternalRuleDefCharRngUpper(ord('Z')),
|
| 362 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 363 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 364 |
+
new InternalRuleDefReference(1),
|
| 365 |
+
new InternalRuleDefAlt(),
|
| 366 |
+
new InternalRuleDefEnd(),
|
| 367 |
+
],
|
| 368 |
+
],
|
| 369 |
+
},
|
| 370 |
+
{
|
| 371 |
+
key: 'grouping-repeating',
|
| 372 |
+
grammar: 'root ::= "f" ("b" | "a")*',
|
| 373 |
+
symbolIds: [['root', 0], ['root_1', 1], ['root_2', 2]],
|
| 374 |
+
rules: [
|
| 375 |
+
[
|
| 376 |
+
new InternalRuleDefChar([ord('f')]),
|
| 377 |
+
new InternalRuleDefReference(2),
|
| 378 |
+
new InternalRuleDefEnd(),
|
| 379 |
+
],
|
| 380 |
+
[
|
| 381 |
+
new InternalRuleDefChar([ord('b')]),
|
| 382 |
+
new InternalRuleDefAlt(),
|
| 383 |
+
new InternalRuleDefChar([ord('a')]),
|
| 384 |
+
new InternalRuleDefEnd(),
|
| 385 |
+
],
|
| 386 |
+
[
|
| 387 |
+
new InternalRuleDefReference(1),
|
| 388 |
+
new InternalRuleDefReference(2),
|
| 389 |
+
new InternalRuleDefAlt(),
|
| 390 |
+
new InternalRuleDefEnd(),
|
| 391 |
+
],
|
| 392 |
+
],
|
| 393 |
+
},
|
| 394 |
+
{
|
| 395 |
+
key: 'negation',
|
| 396 |
+
grammar: 'root ::= [^\\n]',
|
| 397 |
+
symbolIds: [['root', 0]],
|
| 398 |
+
rules: [[new InternalRuleDefCharNot([10]), new InternalRuleDefEnd()]],
|
| 399 |
+
},
|
| 400 |
+
{
|
| 401 |
+
key: 'negation of range',
|
| 402 |
+
grammar: 'root ::= [^0-9]',
|
| 403 |
+
symbolIds: [['root', 0]],
|
| 404 |
+
rules: [
|
| 405 |
+
[
|
| 406 |
+
new InternalRuleDefCharNot([ord('0')]),
|
| 407 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 408 |
+
new InternalRuleDefEnd(),
|
| 409 |
+
],
|
| 410 |
+
],
|
| 411 |
+
},
|
| 412 |
+
{
|
| 413 |
+
key: 'negation with after',
|
| 414 |
+
grammar: 'root ::= [^\\n]+ "\\n"',
|
| 415 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 416 |
+
rules: [
|
| 417 |
+
[new InternalRuleDefReference(1), new InternalRuleDefChar([10]), new InternalRuleDefEnd()],
|
| 418 |
+
[
|
| 419 |
+
new InternalRuleDefCharNot([10]),
|
| 420 |
+
new InternalRuleDefReference(1),
|
| 421 |
+
new InternalRuleDefAlt(),
|
| 422 |
+
new InternalRuleDefCharNot([10]),
|
| 423 |
+
new InternalRuleDefEnd(),
|
| 424 |
+
],
|
| 425 |
+
],
|
| 426 |
+
},
|
| 427 |
+
{
|
| 428 |
+
key: 'longer negation',
|
| 429 |
+
grammar: 'root ::= "\\"" ( [^"abcdefgh])* ',
|
| 430 |
+
symbolIds: [['root', 0], ['root_1', 1], ['root_2', 2]],
|
| 431 |
+
rules: [
|
| 432 |
+
[new InternalRuleDefChar([34]), new InternalRuleDefReference(2), new InternalRuleDefEnd()],
|
| 433 |
+
[
|
| 434 |
+
new InternalRuleDefCharNot([34]),
|
| 435 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 436 |
+
new InternalRuleDefCharAlt(ord('b')),
|
| 437 |
+
new InternalRuleDefCharAlt(ord('c')),
|
| 438 |
+
new InternalRuleDefCharAlt(ord('d')),
|
| 439 |
+
new InternalRuleDefCharAlt(ord('e')),
|
| 440 |
+
new InternalRuleDefCharAlt(ord('f')),
|
| 441 |
+
new InternalRuleDefCharAlt(ord('g')),
|
| 442 |
+
new InternalRuleDefCharAlt(ord('h')),
|
| 443 |
+
new InternalRuleDefEnd(),
|
| 444 |
+
],
|
| 445 |
+
[
|
| 446 |
+
new InternalRuleDefReference(1),
|
| 447 |
+
new InternalRuleDefReference(2),
|
| 448 |
+
new InternalRuleDefAlt(),
|
| 449 |
+
new InternalRuleDefEnd(),
|
| 450 |
+
],
|
| 451 |
+
],
|
| 452 |
+
},
|
| 453 |
+
{
|
| 454 |
+
key: 'longer negation with a range',
|
| 455 |
+
grammar: 'root ::= "\\"" ( [^"abcdefghA-Z])* ',
|
| 456 |
+
symbolIds: [['root', 0], ['root_1', 1], ['root_2', 2]],
|
| 457 |
+
rules: [
|
| 458 |
+
[new InternalRuleDefChar([34]), new InternalRuleDefReference(2), new InternalRuleDefEnd()],
|
| 459 |
+
[
|
| 460 |
+
new InternalRuleDefCharNot([34]),
|
| 461 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 462 |
+
new InternalRuleDefCharAlt(ord('b')),
|
| 463 |
+
new InternalRuleDefCharAlt(ord('c')),
|
| 464 |
+
new InternalRuleDefCharAlt(ord('d')),
|
| 465 |
+
new InternalRuleDefCharAlt(ord('e')),
|
| 466 |
+
new InternalRuleDefCharAlt(ord('f')),
|
| 467 |
+
new InternalRuleDefCharAlt(ord('g')),
|
| 468 |
+
new InternalRuleDefCharAlt(ord('h')),
|
| 469 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 470 |
+
new InternalRuleDefCharRngUpper(ord('Z')),
|
| 471 |
+
new InternalRuleDefEnd(),
|
| 472 |
+
],
|
| 473 |
+
[
|
| 474 |
+
new InternalRuleDefReference(1),
|
| 475 |
+
new InternalRuleDefReference(2),
|
| 476 |
+
new InternalRuleDefAlt(),
|
| 477 |
+
new InternalRuleDefEnd(),
|
| 478 |
+
],
|
| 479 |
+
],
|
| 480 |
+
},
|
| 481 |
+
{
|
| 482 |
+
key: 'escaped 8-bit unicode char',
|
| 483 |
+
grammar: 'root ::= "\\x2A"',
|
| 484 |
+
symbolIds: [['root', 0]],
|
| 485 |
+
rules: [[new InternalRuleDefChar([42]), new InternalRuleDefEnd()]],
|
| 486 |
+
},
|
| 487 |
+
{
|
| 488 |
+
key: 'escaped 16-bit unicode char',
|
| 489 |
+
grammar: 'root ::= "\\u006F"',
|
| 490 |
+
symbolIds: [['root', 0]],
|
| 491 |
+
rules: [[new InternalRuleDefChar([ord('o')]), new InternalRuleDefEnd()]],
|
| 492 |
+
},
|
| 493 |
+
{
|
| 494 |
+
key: 'escaped 32-bit unicode char',
|
| 495 |
+
grammar: 'root ::= "\\U0001F4A9"',
|
| 496 |
+
symbolIds: [['root', 0]],
|
| 497 |
+
rules: [[new InternalRuleDefChar([128169]), new InternalRuleDefEnd()]],
|
| 498 |
+
},
|
| 499 |
+
{
|
| 500 |
+
key: 'escaped tab char',
|
| 501 |
+
grammar: 'root ::= "\\t"',
|
| 502 |
+
symbolIds: [['root', 0]],
|
| 503 |
+
rules: [[new InternalRuleDefChar([9]), new InternalRuleDefEnd()]],
|
| 504 |
+
},
|
| 505 |
+
{
|
| 506 |
+
key: 'escaped new line char',
|
| 507 |
+
grammar: 'root ::= "\\n"',
|
| 508 |
+
symbolIds: [['root', 0]],
|
| 509 |
+
rules: [[new InternalRuleDefChar([10]), new InternalRuleDefEnd()]],
|
| 510 |
+
},
|
| 511 |
+
{
|
| 512 |
+
key: 'escaped \r char',
|
| 513 |
+
grammar: 'root ::= "\\r"',
|
| 514 |
+
symbolIds: [['root', 0]],
|
| 515 |
+
rules: [[new InternalRuleDefChar([13]), new InternalRuleDefEnd()]],
|
| 516 |
+
},
|
| 517 |
+
{
|
| 518 |
+
key: 'escaped quote char',
|
| 519 |
+
grammar: 'root ::= "\\""',
|
| 520 |
+
symbolIds: [['root', 0]],
|
| 521 |
+
rules: [[new InternalRuleDefChar([34]), new InternalRuleDefEnd()]],
|
| 522 |
+
},
|
| 523 |
+
{
|
| 524 |
+
key: 'escaped [ char',
|
| 525 |
+
grammar: 'root ::= "\\["',
|
| 526 |
+
symbolIds: [['root', 0]],
|
| 527 |
+
rules: [[new InternalRuleDefChar([91]), new InternalRuleDefEnd()]],
|
| 528 |
+
},
|
| 529 |
+
{
|
| 530 |
+
key: 'escaped ] char',
|
| 531 |
+
grammar: 'root ::= "\\]"',
|
| 532 |
+
symbolIds: [['root', 0]],
|
| 533 |
+
rules: [[new InternalRuleDefChar([93]), new InternalRuleDefEnd()]],
|
| 534 |
+
},
|
| 535 |
+
{
|
| 536 |
+
key: 'escaped \\ char',
|
| 537 |
+
grammar: 'root ::= "\\\\"',
|
| 538 |
+
symbolIds: [['root', 0]],
|
| 539 |
+
rules: [[new InternalRuleDefChar([92]), new InternalRuleDefEnd()]],
|
| 540 |
+
},
|
| 541 |
+
{
|
| 542 |
+
key: 'simple arithmetic',
|
| 543 |
+
grammar: `
|
| 544 |
+
root ::= (expr "=" term "\\n")+
|
| 545 |
+
expr ::= term ([-+*/] term)*
|
| 546 |
+
term ::= [0-9]+
|
| 547 |
+
`,
|
| 548 |
+
symbolIds: [
|
| 549 |
+
['root', 0],
|
| 550 |
+
['root_1', 1],
|
| 551 |
+
['expr', 2],
|
| 552 |
+
['term', 3],
|
| 553 |
+
['root_4', 4],
|
| 554 |
+
['expr_5', 5],
|
| 555 |
+
['expr_6', 6],
|
| 556 |
+
['term_7', 7],
|
| 557 |
+
],
|
| 558 |
+
rules: [
|
| 559 |
+
[new InternalRuleDefReference(4), new InternalRuleDefEnd()],
|
| 560 |
+
[
|
| 561 |
+
new InternalRuleDefReference(2),
|
| 562 |
+
new InternalRuleDefChar([61]),
|
| 563 |
+
new InternalRuleDefReference(3),
|
| 564 |
+
new InternalRuleDefChar([10]),
|
| 565 |
+
new InternalRuleDefEnd(),
|
| 566 |
+
],
|
| 567 |
+
[new InternalRuleDefReference(3), new InternalRuleDefReference(6), new InternalRuleDefEnd()],
|
| 568 |
+
[new InternalRuleDefReference(7), new InternalRuleDefEnd()],
|
| 569 |
+
[
|
| 570 |
+
new InternalRuleDefReference(1),
|
| 571 |
+
new InternalRuleDefReference(4),
|
| 572 |
+
new InternalRuleDefAlt(),
|
| 573 |
+
new InternalRuleDefReference(1),
|
| 574 |
+
new InternalRuleDefEnd(),
|
| 575 |
+
],
|
| 576 |
+
[
|
| 577 |
+
new InternalRuleDefChar([45]),
|
| 578 |
+
new InternalRuleDefCharAlt(43),
|
| 579 |
+
new InternalRuleDefCharAlt(42),
|
| 580 |
+
new InternalRuleDefCharAlt(47),
|
| 581 |
+
new InternalRuleDefReference(3),
|
| 582 |
+
new InternalRuleDefEnd(),
|
| 583 |
+
],
|
| 584 |
+
[
|
| 585 |
+
new InternalRuleDefReference(5),
|
| 586 |
+
new InternalRuleDefReference(6),
|
| 587 |
+
new InternalRuleDefAlt(),
|
| 588 |
+
new InternalRuleDefEnd(),
|
| 589 |
+
],
|
| 590 |
+
[
|
| 591 |
+
new InternalRuleDefChar([ord('0')]),
|
| 592 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 593 |
+
new InternalRuleDefReference(7),
|
| 594 |
+
new InternalRuleDefAlt(),
|
| 595 |
+
new InternalRuleDefChar([ord('0')]),
|
| 596 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 597 |
+
new InternalRuleDefEnd(),
|
| 598 |
+
],
|
| 599 |
+
],
|
| 600 |
+
},
|
| 601 |
+
{
|
| 602 |
+
key: 'ranges with chars',
|
| 603 |
+
grammar: `
|
| 604 |
+
root ::= [a-z0-9_]*
|
| 605 |
+
`,
|
| 606 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 607 |
+
rules: [
|
| 608 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 609 |
+
[
|
| 610 |
+
new InternalRuleDefChar([ord('a')]),
|
| 611 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 612 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 613 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 614 |
+
new InternalRuleDefCharAlt(95),
|
| 615 |
+
new InternalRuleDefReference(1),
|
| 616 |
+
new InternalRuleDefAlt(),
|
| 617 |
+
new InternalRuleDefEnd(),
|
| 618 |
+
],
|
| 619 |
+
],
|
| 620 |
+
},
|
| 621 |
+
{
|
| 622 |
+
key: 'nested ranges with chars',
|
| 623 |
+
grammar: `
|
| 624 |
+
root ::= [a-z] [a-z0-9_]*
|
| 625 |
+
`,
|
| 626 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 627 |
+
rules: [
|
| 628 |
+
[
|
| 629 |
+
new InternalRuleDefChar([ord('a')]),
|
| 630 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 631 |
+
new InternalRuleDefReference(1),
|
| 632 |
+
new InternalRuleDefEnd(),
|
| 633 |
+
],
|
| 634 |
+
[
|
| 635 |
+
new InternalRuleDefChar([ord('a')]),
|
| 636 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 637 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 638 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 639 |
+
new InternalRuleDefCharAlt(95),
|
| 640 |
+
new InternalRuleDefReference(1),
|
| 641 |
+
new InternalRuleDefAlt(),
|
| 642 |
+
new InternalRuleDefEnd(),
|
| 643 |
+
],
|
| 644 |
+
],
|
| 645 |
+
},
|
| 646 |
+
{
|
| 647 |
+
key: 'expression with nested range with chars',
|
| 648 |
+
grammar: `
|
| 649 |
+
root ::= ident
|
| 650 |
+
ident ::= [a-z] [a-z0-9_]* ws
|
| 651 |
+
ws ::= [ \\t\\n]*
|
| 652 |
+
`,
|
| 653 |
+
symbolIds: [['root', 0], ['ident', 1], ['ident_2', 2], ['ws', 3], ['ws_4', 4]],
|
| 654 |
+
rules: [
|
| 655 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 656 |
+
[
|
| 657 |
+
new InternalRuleDefChar([ord('a')]),
|
| 658 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 659 |
+
new InternalRuleDefReference(2),
|
| 660 |
+
new InternalRuleDefReference(3),
|
| 661 |
+
new InternalRuleDefEnd(),
|
| 662 |
+
],
|
| 663 |
+
[
|
| 664 |
+
new InternalRuleDefChar([ord('a')]),
|
| 665 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 666 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 667 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 668 |
+
new InternalRuleDefCharAlt(95),
|
| 669 |
+
new InternalRuleDefReference(2),
|
| 670 |
+
new InternalRuleDefAlt(),
|
| 671 |
+
new InternalRuleDefEnd(),
|
| 672 |
+
],
|
| 673 |
+
[new InternalRuleDefReference(4), new InternalRuleDefEnd()],
|
| 674 |
+
[
|
| 675 |
+
new InternalRuleDefChar([32]),
|
| 676 |
+
new InternalRuleDefCharAlt(9),
|
| 677 |
+
new InternalRuleDefCharAlt(10),
|
| 678 |
+
new InternalRuleDefReference(4),
|
| 679 |
+
new InternalRuleDefAlt(),
|
| 680 |
+
new InternalRuleDefEnd(),
|
| 681 |
+
],
|
| 682 |
+
],
|
| 683 |
+
},
|
| 684 |
+
{
|
| 685 |
+
key: 'lots of escapes',
|
| 686 |
+
grammar: 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\"',
|
| 687 |
+
symbolIds: [['root', 0]],
|
| 688 |
+
rules: [
|
| 689 |
+
[
|
| 690 |
+
new InternalRuleDefChar([42]),
|
| 691 |
+
new InternalRuleDefChar([ord('o')]),
|
| 692 |
+
new InternalRuleDefChar([128169]),
|
| 693 |
+
new InternalRuleDefChar([9]),
|
| 694 |
+
new InternalRuleDefChar([10]),
|
| 695 |
+
new InternalRuleDefChar([13]),
|
| 696 |
+
new InternalRuleDefChar([34]),
|
| 697 |
+
new InternalRuleDefChar([91]),
|
| 698 |
+
new InternalRuleDefChar([93]),
|
| 699 |
+
new InternalRuleDefChar([92]),
|
| 700 |
+
new InternalRuleDefEnd(),
|
| 701 |
+
],
|
| 702 |
+
],
|
| 703 |
+
},
|
| 704 |
+
{
|
| 705 |
+
key: 'lots of escape and alternate escapes',
|
| 706 |
+
grammar: `root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\" (
|
| 707 |
+
"\\x2A" | "\\u006F" | "\\U0001F4A9" | "\\t" | "\\n" | "\\r" | "\\"" | "\\[" | "\\]" | "\\\\" )`,
|
| 708 |
+
symbolIds: [['root', 0], ['root_1', 1]],
|
| 709 |
+
rules: [
|
| 710 |
+
[
|
| 711 |
+
new InternalRuleDefChar([42]),
|
| 712 |
+
new InternalRuleDefChar([ord('o')]),
|
| 713 |
+
new InternalRuleDefChar([128169]),
|
| 714 |
+
new InternalRuleDefChar([9]),
|
| 715 |
+
new InternalRuleDefChar([10]),
|
| 716 |
+
new InternalRuleDefChar([13]),
|
| 717 |
+
new InternalRuleDefChar([34]),
|
| 718 |
+
new InternalRuleDefChar([91]),
|
| 719 |
+
new InternalRuleDefChar([93]),
|
| 720 |
+
new InternalRuleDefChar([92]),
|
| 721 |
+
new InternalRuleDefReference(1),
|
| 722 |
+
new InternalRuleDefEnd(),
|
| 723 |
+
],
|
| 724 |
+
[
|
| 725 |
+
new InternalRuleDefChar([42]),
|
| 726 |
+
new InternalRuleDefAlt(),
|
| 727 |
+
new InternalRuleDefChar([ord('o')]),
|
| 728 |
+
new InternalRuleDefAlt(),
|
| 729 |
+
new InternalRuleDefChar([128169]),
|
| 730 |
+
new InternalRuleDefAlt(),
|
| 731 |
+
new InternalRuleDefChar([9]),
|
| 732 |
+
new InternalRuleDefAlt(),
|
| 733 |
+
new InternalRuleDefChar([10]),
|
| 734 |
+
new InternalRuleDefAlt(),
|
| 735 |
+
new InternalRuleDefChar([13]),
|
| 736 |
+
new InternalRuleDefAlt(),
|
| 737 |
+
new InternalRuleDefChar([34]),
|
| 738 |
+
new InternalRuleDefAlt(),
|
| 739 |
+
new InternalRuleDefChar([91]),
|
| 740 |
+
new InternalRuleDefAlt(),
|
| 741 |
+
new InternalRuleDefChar([93]),
|
| 742 |
+
new InternalRuleDefAlt(),
|
| 743 |
+
new InternalRuleDefChar([92]),
|
| 744 |
+
new InternalRuleDefEnd(),
|
| 745 |
+
],
|
| 746 |
+
],
|
| 747 |
+
},
|
| 748 |
+
{
|
| 749 |
+
key: 'arithmetic',
|
| 750 |
+
grammar: `
|
| 751 |
+
root ::= (expr "=" ws term "\\n")+
|
| 752 |
+
expr ::= term ([-+*/] term)*
|
| 753 |
+
term ::= ident | num | "(" ws expr ")" ws
|
| 754 |
+
ident ::= [a-z] [a-z0-9_]* ws
|
| 755 |
+
num ::= [0-9]+ ws
|
| 756 |
+
ws ::= [ \\t\\n]*
|
| 757 |
+
`,
|
| 758 |
+
symbolIds: [
|
| 759 |
+
['root', 0],
|
| 760 |
+
['root_1', 1],
|
| 761 |
+
['expr', 2],
|
| 762 |
+
['ws', 3],
|
| 763 |
+
['term', 4],
|
| 764 |
+
['root_5', 5],
|
| 765 |
+
['expr_6', 6],
|
| 766 |
+
['expr_7', 7],
|
| 767 |
+
['ident', 8],
|
| 768 |
+
['num', 9],
|
| 769 |
+
['ident_10', 10],
|
| 770 |
+
['num_11', 11],
|
| 771 |
+
['ws_12', 12],
|
| 772 |
+
],
|
| 773 |
+
rules: [
|
| 774 |
+
[new InternalRuleDefReference(5), new InternalRuleDefEnd()],
|
| 775 |
+
[
|
| 776 |
+
new InternalRuleDefReference(2),
|
| 777 |
+
new InternalRuleDefChar([61]),
|
| 778 |
+
new InternalRuleDefReference(3),
|
| 779 |
+
new InternalRuleDefReference(4),
|
| 780 |
+
new InternalRuleDefChar([10]),
|
| 781 |
+
new InternalRuleDefEnd(),
|
| 782 |
+
],
|
| 783 |
+
[new InternalRuleDefReference(4), new InternalRuleDefReference(7), new InternalRuleDefEnd()],
|
| 784 |
+
[new InternalRuleDefReference(12), new InternalRuleDefEnd()],
|
| 785 |
+
[
|
| 786 |
+
new InternalRuleDefReference(8),
|
| 787 |
+
new InternalRuleDefAlt(),
|
| 788 |
+
new InternalRuleDefReference(9),
|
| 789 |
+
new InternalRuleDefAlt(),
|
| 790 |
+
new InternalRuleDefChar([40]),
|
| 791 |
+
new InternalRuleDefReference(3),
|
| 792 |
+
new InternalRuleDefReference(2),
|
| 793 |
+
new InternalRuleDefChar([41]),
|
| 794 |
+
new InternalRuleDefReference(3),
|
| 795 |
+
new InternalRuleDefEnd(),
|
| 796 |
+
],
|
| 797 |
+
[
|
| 798 |
+
new InternalRuleDefReference(1),
|
| 799 |
+
new InternalRuleDefReference(5),
|
| 800 |
+
new InternalRuleDefAlt(),
|
| 801 |
+
new InternalRuleDefReference(1),
|
| 802 |
+
new InternalRuleDefEnd(),
|
| 803 |
+
],
|
| 804 |
+
[
|
| 805 |
+
new InternalRuleDefChar([45]),
|
| 806 |
+
new InternalRuleDefCharAlt(43),
|
| 807 |
+
new InternalRuleDefCharAlt(42),
|
| 808 |
+
new InternalRuleDefCharAlt(47),
|
| 809 |
+
new InternalRuleDefReference(4),
|
| 810 |
+
new InternalRuleDefEnd(),
|
| 811 |
+
],
|
| 812 |
+
[
|
| 813 |
+
new InternalRuleDefReference(6),
|
| 814 |
+
new InternalRuleDefReference(7),
|
| 815 |
+
new InternalRuleDefAlt(),
|
| 816 |
+
new InternalRuleDefEnd(),
|
| 817 |
+
],
|
| 818 |
+
[
|
| 819 |
+
new InternalRuleDefChar([ord('a')]),
|
| 820 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 821 |
+
new InternalRuleDefReference(10),
|
| 822 |
+
new InternalRuleDefReference(3),
|
| 823 |
+
new InternalRuleDefEnd(),
|
| 824 |
+
],
|
| 825 |
+
[new InternalRuleDefReference(11), new InternalRuleDefReference(3), new InternalRuleDefEnd()],
|
| 826 |
+
[
|
| 827 |
+
new InternalRuleDefChar([ord('a')]),
|
| 828 |
+
new InternalRuleDefCharRngUpper(ord('z')),
|
| 829 |
+
new InternalRuleDefCharAlt(ord('0')),
|
| 830 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 831 |
+
new InternalRuleDefCharAlt(95),
|
| 832 |
+
new InternalRuleDefReference(10),
|
| 833 |
+
new InternalRuleDefAlt(),
|
| 834 |
+
new InternalRuleDefEnd(),
|
| 835 |
+
],
|
| 836 |
+
[
|
| 837 |
+
new InternalRuleDefChar([ord('0')]),
|
| 838 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 839 |
+
new InternalRuleDefReference(11),
|
| 840 |
+
new InternalRuleDefAlt(),
|
| 841 |
+
new InternalRuleDefChar([ord('0')]),
|
| 842 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 843 |
+
new InternalRuleDefEnd(),
|
| 844 |
+
],
|
| 845 |
+
[
|
| 846 |
+
new InternalRuleDefChar([32]),
|
| 847 |
+
new InternalRuleDefCharAlt(9),
|
| 848 |
+
new InternalRuleDefCharAlt(10),
|
| 849 |
+
new InternalRuleDefReference(12),
|
| 850 |
+
new InternalRuleDefAlt(),
|
| 851 |
+
new InternalRuleDefEnd(),
|
| 852 |
+
],
|
| 853 |
+
],
|
| 854 |
+
},
|
| 855 |
+
{
|
| 856 |
+
key: 'json.gbnf (string)',
|
| 857 |
+
grammar: `
|
| 858 |
+
root ::=
|
| 859 |
+
"\\"" (
|
| 860 |
+
[^"\\\\\\x7F\\x00-\\x1F] |
|
| 861 |
+
"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
|
| 862 |
+
)* "\\""
|
| 863 |
+
`,
|
| 864 |
+
symbolIds: [['root', 0], ['root_1', 1], ['root_2', 2], ['root_3', 3]],
|
| 865 |
+
rules: [
|
| 866 |
+
[
|
| 867 |
+
new InternalRuleDefChar([34]),
|
| 868 |
+
new InternalRuleDefReference(3),
|
| 869 |
+
new InternalRuleDefChar([34]),
|
| 870 |
+
new InternalRuleDefEnd(),
|
| 871 |
+
],
|
| 872 |
+
[
|
| 873 |
+
new InternalRuleDefCharNot([34]),
|
| 874 |
+
new InternalRuleDefCharAlt(92),
|
| 875 |
+
new InternalRuleDefCharAlt(127),
|
| 876 |
+
new InternalRuleDefCharAlt(0),
|
| 877 |
+
new InternalRuleDefCharRngUpper(31),
|
| 878 |
+
new InternalRuleDefAlt(),
|
| 879 |
+
new InternalRuleDefChar([92]),
|
| 880 |
+
new InternalRuleDefReference(2),
|
| 881 |
+
new InternalRuleDefEnd(),
|
| 882 |
+
],
|
| 883 |
+
[
|
| 884 |
+
new InternalRuleDefChar([34]),
|
| 885 |
+
new InternalRuleDefCharAlt(92),
|
| 886 |
+
new InternalRuleDefCharAlt(47),
|
| 887 |
+
new InternalRuleDefCharAlt(ord('b')),
|
| 888 |
+
new InternalRuleDefCharAlt(ord('f')),
|
| 889 |
+
new InternalRuleDefCharAlt(ord('n')),
|
| 890 |
+
new InternalRuleDefCharAlt(ord('r')),
|
| 891 |
+
new InternalRuleDefCharAlt(ord('t')),
|
| 892 |
+
new InternalRuleDefAlt(),
|
| 893 |
+
new InternalRuleDefChar([ord('u')]),
|
| 894 |
+
new InternalRuleDefChar([ord('0')]),
|
| 895 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 896 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 897 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 898 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 899 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 900 |
+
new InternalRuleDefChar([ord('0')]),
|
| 901 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 902 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 903 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 904 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 905 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 906 |
+
new InternalRuleDefChar([ord('0')]),
|
| 907 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 908 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 909 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 910 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 911 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 912 |
+
new InternalRuleDefChar([ord('0')]),
|
| 913 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 914 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 915 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 916 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 917 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 918 |
+
new InternalRuleDefEnd(),
|
| 919 |
+
],
|
| 920 |
+
[
|
| 921 |
+
new InternalRuleDefReference(1),
|
| 922 |
+
new InternalRuleDefReference(3),
|
| 923 |
+
new InternalRuleDefAlt(),
|
| 924 |
+
new InternalRuleDefEnd(),
|
| 925 |
+
],
|
| 926 |
+
],
|
| 927 |
+
},
|
| 928 |
+
{
|
| 929 |
+
key: 'json.gbnf (full)',
|
| 930 |
+
grammar: `
|
| 931 |
+
root ::= object
|
| 932 |
+
value ::= object | array | string | number | ("true" | "false" | "null") ws
|
| 933 |
+
object ::=
|
| 934 |
+
"{" ws (
|
| 935 |
+
string ":" ws value
|
| 936 |
+
("," ws string ":" ws value)*
|
| 937 |
+
)? "}" ws
|
| 938 |
+
array ::=
|
| 939 |
+
"[" ws (
|
| 940 |
+
value
|
| 941 |
+
("," ws value)*
|
| 942 |
+
)? "]" ws
|
| 943 |
+
string ::=
|
| 944 |
+
"\\"" (
|
| 945 |
+
[^"\\\\\\x7F\\x00-\\x1F] |
|
| 946 |
+
"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
|
| 947 |
+
)* "\\"" ws
|
| 948 |
+
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
|
| 949 |
+
# Optional space: by convention, applied in this grammar after literal chars when allowed
|
| 950 |
+
ws ::= ([ \\t\\n] ws)?
|
| 951 |
+
`,
|
| 952 |
+
symbolIds: [
|
| 953 |
+
['root', 0],
|
| 954 |
+
['object', 1],
|
| 955 |
+
['value', 2],
|
| 956 |
+
['array', 3],
|
| 957 |
+
['string', 4],
|
| 958 |
+
['number', 5],
|
| 959 |
+
['value_6', 6],
|
| 960 |
+
['ws', 7],
|
| 961 |
+
['object_8', 8],
|
| 962 |
+
['object_9', 9],
|
| 963 |
+
['object_10', 10],
|
| 964 |
+
['object_11', 11],
|
| 965 |
+
['array_12', 12],
|
| 966 |
+
['array_13', 13],
|
| 967 |
+
['array_14', 14],
|
| 968 |
+
['array_15', 15],
|
| 969 |
+
['string_16', 16],
|
| 970 |
+
['string_17', 17],
|
| 971 |
+
['string_18', 18],
|
| 972 |
+
['number_19', 19],
|
| 973 |
+
['number_20', 20],
|
| 974 |
+
['number_21', 21],
|
| 975 |
+
['number_22', 22],
|
| 976 |
+
['number_23', 23],
|
| 977 |
+
['number_24', 24],
|
| 978 |
+
['number_25', 25],
|
| 979 |
+
['number_26', 26],
|
| 980 |
+
['number_27', 27],
|
| 981 |
+
['number_28', 28],
|
| 982 |
+
['number_29', 29],
|
| 983 |
+
['ws_30', 30],
|
| 984 |
+
['ws_31', 31],
|
| 985 |
+
],
|
| 986 |
+
rules: [
|
| 987 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 988 |
+
[
|
| 989 |
+
new InternalRuleDefChar([123]),
|
| 990 |
+
new InternalRuleDefReference(7),
|
| 991 |
+
new InternalRuleDefReference(11),
|
| 992 |
+
new InternalRuleDefChar([125]),
|
| 993 |
+
new InternalRuleDefReference(7),
|
| 994 |
+
new InternalRuleDefEnd(),
|
| 995 |
+
],
|
| 996 |
+
[
|
| 997 |
+
new InternalRuleDefReference(1),
|
| 998 |
+
new InternalRuleDefAlt(),
|
| 999 |
+
new InternalRuleDefReference(3),
|
| 1000 |
+
new InternalRuleDefAlt(),
|
| 1001 |
+
new InternalRuleDefReference(4),
|
| 1002 |
+
new InternalRuleDefAlt(),
|
| 1003 |
+
new InternalRuleDefReference(5),
|
| 1004 |
+
new InternalRuleDefAlt(),
|
| 1005 |
+
new InternalRuleDefReference(6),
|
| 1006 |
+
new InternalRuleDefReference(7),
|
| 1007 |
+
new InternalRuleDefEnd(),
|
| 1008 |
+
],
|
| 1009 |
+
[
|
| 1010 |
+
new InternalRuleDefChar([91]),
|
| 1011 |
+
new InternalRuleDefReference(7),
|
| 1012 |
+
new InternalRuleDefReference(15),
|
| 1013 |
+
new InternalRuleDefChar([93]),
|
| 1014 |
+
new InternalRuleDefReference(7),
|
| 1015 |
+
new InternalRuleDefEnd(),
|
| 1016 |
+
],
|
| 1017 |
+
[
|
| 1018 |
+
new InternalRuleDefChar([34]),
|
| 1019 |
+
new InternalRuleDefReference(18),
|
| 1020 |
+
new InternalRuleDefChar([34]),
|
| 1021 |
+
new InternalRuleDefReference(7),
|
| 1022 |
+
new InternalRuleDefEnd(),
|
| 1023 |
+
],
|
| 1024 |
+
[
|
| 1025 |
+
new InternalRuleDefReference(19),
|
| 1026 |
+
new InternalRuleDefReference(25),
|
| 1027 |
+
new InternalRuleDefReference(29),
|
| 1028 |
+
new InternalRuleDefReference(7),
|
| 1029 |
+
new InternalRuleDefEnd(),
|
| 1030 |
+
],
|
| 1031 |
+
[
|
| 1032 |
+
new InternalRuleDefChar([ord('t')]),
|
| 1033 |
+
new InternalRuleDefChar([ord('r')]),
|
| 1034 |
+
new InternalRuleDefChar([ord('u')]),
|
| 1035 |
+
new InternalRuleDefChar([ord('e')]),
|
| 1036 |
+
new InternalRuleDefAlt(),
|
| 1037 |
+
new InternalRuleDefChar([ord('f')]),
|
| 1038 |
+
new InternalRuleDefChar([ord('a')]),
|
| 1039 |
+
new InternalRuleDefChar([ord('l')]),
|
| 1040 |
+
new InternalRuleDefChar([ord('s')]),
|
| 1041 |
+
new InternalRuleDefChar([ord('e')]),
|
| 1042 |
+
new InternalRuleDefAlt(),
|
| 1043 |
+
new InternalRuleDefChar([ord('n')]),
|
| 1044 |
+
new InternalRuleDefChar([ord('u')]),
|
| 1045 |
+
new InternalRuleDefChar([ord('l')]),
|
| 1046 |
+
new InternalRuleDefChar([ord('l')]),
|
| 1047 |
+
new InternalRuleDefEnd(),
|
| 1048 |
+
],
|
| 1049 |
+
[new InternalRuleDefReference(31), new InternalRuleDefEnd()],
|
| 1050 |
+
[
|
| 1051 |
+
new InternalRuleDefReference(4),
|
| 1052 |
+
new InternalRuleDefChar([58]),
|
| 1053 |
+
new InternalRuleDefReference(7),
|
| 1054 |
+
new InternalRuleDefReference(2),
|
| 1055 |
+
new InternalRuleDefReference(10),
|
| 1056 |
+
new InternalRuleDefEnd(),
|
| 1057 |
+
],
|
| 1058 |
+
[
|
| 1059 |
+
new InternalRuleDefChar([44]),
|
| 1060 |
+
new InternalRuleDefReference(7),
|
| 1061 |
+
new InternalRuleDefReference(4),
|
| 1062 |
+
new InternalRuleDefChar([58]),
|
| 1063 |
+
new InternalRuleDefReference(7),
|
| 1064 |
+
new InternalRuleDefReference(2),
|
| 1065 |
+
new InternalRuleDefEnd(),
|
| 1066 |
+
],
|
| 1067 |
+
[
|
| 1068 |
+
new InternalRuleDefReference(9),
|
| 1069 |
+
new InternalRuleDefReference(10),
|
| 1070 |
+
new InternalRuleDefAlt(),
|
| 1071 |
+
new InternalRuleDefEnd(),
|
| 1072 |
+
],
|
| 1073 |
+
[new InternalRuleDefReference(8), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1074 |
+
[new InternalRuleDefReference(2), new InternalRuleDefReference(14), new InternalRuleDefEnd()],
|
| 1075 |
+
[
|
| 1076 |
+
new InternalRuleDefChar([44]),
|
| 1077 |
+
new InternalRuleDefReference(7),
|
| 1078 |
+
new InternalRuleDefReference(2),
|
| 1079 |
+
new InternalRuleDefEnd(),
|
| 1080 |
+
],
|
| 1081 |
+
[
|
| 1082 |
+
new InternalRuleDefReference(13),
|
| 1083 |
+
new InternalRuleDefReference(14),
|
| 1084 |
+
new InternalRuleDefAlt(),
|
| 1085 |
+
new InternalRuleDefEnd(),
|
| 1086 |
+
],
|
| 1087 |
+
[new InternalRuleDefReference(12), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1088 |
+
[
|
| 1089 |
+
new InternalRuleDefCharNot([34]),
|
| 1090 |
+
new InternalRuleDefCharAlt(92),
|
| 1091 |
+
new InternalRuleDefCharAlt(127),
|
| 1092 |
+
new InternalRuleDefCharAlt(0),
|
| 1093 |
+
new InternalRuleDefCharRngUpper(31),
|
| 1094 |
+
new InternalRuleDefAlt(),
|
| 1095 |
+
new InternalRuleDefChar([92]),
|
| 1096 |
+
new InternalRuleDefReference(17),
|
| 1097 |
+
new InternalRuleDefEnd(),
|
| 1098 |
+
],
|
| 1099 |
+
[
|
| 1100 |
+
new InternalRuleDefChar([34]),
|
| 1101 |
+
new InternalRuleDefCharAlt(92),
|
| 1102 |
+
new InternalRuleDefCharAlt(47),
|
| 1103 |
+
new InternalRuleDefCharAlt(ord('b')),
|
| 1104 |
+
new InternalRuleDefCharAlt(ord('f')),
|
| 1105 |
+
new InternalRuleDefCharAlt(ord('n')),
|
| 1106 |
+
new InternalRuleDefCharAlt(ord('r')),
|
| 1107 |
+
new InternalRuleDefCharAlt(ord('t')),
|
| 1108 |
+
new InternalRuleDefAlt(),
|
| 1109 |
+
new InternalRuleDefChar([ord('u')]),
|
| 1110 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1111 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1112 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 1113 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 1114 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 1115 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 1116 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1117 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1118 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 1119 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 1120 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 1121 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 1122 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1123 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1124 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 1125 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 1126 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 1127 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 1128 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1129 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1130 |
+
new InternalRuleDefCharAlt(ord('a')),
|
| 1131 |
+
new InternalRuleDefCharRngUpper(ord('f')),
|
| 1132 |
+
new InternalRuleDefCharAlt(ord('A')),
|
| 1133 |
+
new InternalRuleDefCharRngUpper(ord('F')),
|
| 1134 |
+
new InternalRuleDefEnd(),
|
| 1135 |
+
],
|
| 1136 |
+
[
|
| 1137 |
+
new InternalRuleDefReference(16),
|
| 1138 |
+
new InternalRuleDefReference(18),
|
| 1139 |
+
new InternalRuleDefAlt(),
|
| 1140 |
+
new InternalRuleDefEnd(),
|
| 1141 |
+
],
|
| 1142 |
+
[
|
| 1143 |
+
new InternalRuleDefReference(20),
|
| 1144 |
+
new InternalRuleDefReference(21),
|
| 1145 |
+
new InternalRuleDefEnd(),
|
| 1146 |
+
],
|
| 1147 |
+
[new InternalRuleDefChar([45]), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1148 |
+
[
|
| 1149 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1150 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1151 |
+
new InternalRuleDefAlt(),
|
| 1152 |
+
new InternalRuleDefChar([ord('1')]),
|
| 1153 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1154 |
+
new InternalRuleDefReference(22),
|
| 1155 |
+
new InternalRuleDefEnd(),
|
| 1156 |
+
],
|
| 1157 |
+
[
|
| 1158 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1159 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1160 |
+
new InternalRuleDefReference(22),
|
| 1161 |
+
new InternalRuleDefAlt(),
|
| 1162 |
+
new InternalRuleDefEnd(),
|
| 1163 |
+
],
|
| 1164 |
+
[new InternalRuleDefChar([46]), new InternalRuleDefReference(24), new InternalRuleDefEnd()],
|
| 1165 |
+
[
|
| 1166 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1167 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1168 |
+
new InternalRuleDefReference(24),
|
| 1169 |
+
new InternalRuleDefAlt(),
|
| 1170 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1171 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1172 |
+
new InternalRuleDefEnd(),
|
| 1173 |
+
],
|
| 1174 |
+
[new InternalRuleDefReference(23), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1175 |
+
[
|
| 1176 |
+
new InternalRuleDefChar([ord('e')]),
|
| 1177 |
+
new InternalRuleDefCharAlt(ord('E')),
|
| 1178 |
+
new InternalRuleDefReference(27),
|
| 1179 |
+
new InternalRuleDefReference(28),
|
| 1180 |
+
new InternalRuleDefEnd(),
|
| 1181 |
+
],
|
| 1182 |
+
[
|
| 1183 |
+
new InternalRuleDefChar([45]),
|
| 1184 |
+
new InternalRuleDefCharAlt(43),
|
| 1185 |
+
new InternalRuleDefAlt(),
|
| 1186 |
+
new InternalRuleDefEnd(),
|
| 1187 |
+
],
|
| 1188 |
+
[
|
| 1189 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1190 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1191 |
+
new InternalRuleDefReference(28),
|
| 1192 |
+
new InternalRuleDefAlt(),
|
| 1193 |
+
new InternalRuleDefChar([ord('0')]),
|
| 1194 |
+
new InternalRuleDefCharRngUpper(ord('9')),
|
| 1195 |
+
new InternalRuleDefEnd(),
|
| 1196 |
+
],
|
| 1197 |
+
[new InternalRuleDefReference(26), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1198 |
+
[
|
| 1199 |
+
new InternalRuleDefChar([32]),
|
| 1200 |
+
new InternalRuleDefCharAlt(9),
|
| 1201 |
+
new InternalRuleDefCharAlt(10),
|
| 1202 |
+
new InternalRuleDefReference(7),
|
| 1203 |
+
new InternalRuleDefEnd(),
|
| 1204 |
+
],
|
| 1205 |
+
[new InternalRuleDefReference(30), new InternalRuleDefAlt(), new InternalRuleDefEnd()],
|
| 1206 |
+
],
|
| 1207 |
+
},
|
| 1208 |
+
{
|
| 1209 |
+
key: 'japanese',
|
| 1210 |
+
grammar: `
|
| 1211 |
+
# A probably incorrect grammar for Japanese
|
| 1212 |
+
root ::= jp-char+ ([ \\t\\n] jp-char+)*
|
| 1213 |
+
jp-char ::= hiragana | katakana | punctuation | cjk
|
| 1214 |
+
hiragana ::= [ぁ-ゟ]
|
| 1215 |
+
katakana ::= [ァ-ヿ]
|
| 1216 |
+
punctuation ::= [、-〾]
|
| 1217 |
+
cjk ::= [一-鿿]
|
| 1218 |
+
`,
|
| 1219 |
+
symbolIds: [
|
| 1220 |
+
['root', 0],
|
| 1221 |
+
['jp-char', 1],
|
| 1222 |
+
['root_2', 2],
|
| 1223 |
+
['root_3', 3],
|
| 1224 |
+
['root_4', 4],
|
| 1225 |
+
['root_5', 5],
|
| 1226 |
+
['hiragana', 6],
|
| 1227 |
+
['katakana', 7],
|
| 1228 |
+
['punctuation', 8],
|
| 1229 |
+
['cjk', 9],
|
| 1230 |
+
],
|
| 1231 |
+
rules: [
|
| 1232 |
+
[new InternalRuleDefReference(2), new InternalRuleDefReference(5), new InternalRuleDefEnd()],
|
| 1233 |
+
[
|
| 1234 |
+
new InternalRuleDefReference(6),
|
| 1235 |
+
new InternalRuleDefAlt(),
|
| 1236 |
+
new InternalRuleDefReference(7),
|
| 1237 |
+
new InternalRuleDefAlt(),
|
| 1238 |
+
new InternalRuleDefReference(8),
|
| 1239 |
+
new InternalRuleDefAlt(),
|
| 1240 |
+
new InternalRuleDefReference(9),
|
| 1241 |
+
new InternalRuleDefEnd(),
|
| 1242 |
+
],
|
| 1243 |
+
[
|
| 1244 |
+
new InternalRuleDefReference(1),
|
| 1245 |
+
new InternalRuleDefReference(2),
|
| 1246 |
+
new InternalRuleDefAlt(),
|
| 1247 |
+
new InternalRuleDefReference(1),
|
| 1248 |
+
new InternalRuleDefEnd(),
|
| 1249 |
+
],
|
| 1250 |
+
[
|
| 1251 |
+
new InternalRuleDefChar([32]),
|
| 1252 |
+
new InternalRuleDefCharAlt(9),
|
| 1253 |
+
new InternalRuleDefCharAlt(10),
|
| 1254 |
+
new InternalRuleDefReference(4),
|
| 1255 |
+
new InternalRuleDefEnd(),
|
| 1256 |
+
],
|
| 1257 |
+
[
|
| 1258 |
+
new InternalRuleDefReference(1),
|
| 1259 |
+
new InternalRuleDefReference(4),
|
| 1260 |
+
new InternalRuleDefAlt(),
|
| 1261 |
+
new InternalRuleDefReference(1),
|
| 1262 |
+
new InternalRuleDefEnd(),
|
| 1263 |
+
],
|
| 1264 |
+
[
|
| 1265 |
+
new InternalRuleDefReference(3),
|
| 1266 |
+
new InternalRuleDefReference(5),
|
| 1267 |
+
new InternalRuleDefAlt(),
|
| 1268 |
+
new InternalRuleDefEnd(),
|
| 1269 |
+
],
|
| 1270 |
+
[
|
| 1271 |
+
new InternalRuleDefChar([12353]),
|
| 1272 |
+
new InternalRuleDefCharRngUpper(12447),
|
| 1273 |
+
new InternalRuleDefEnd(),
|
| 1274 |
+
],
|
| 1275 |
+
[
|
| 1276 |
+
new InternalRuleDefChar([12449]),
|
| 1277 |
+
new InternalRuleDefCharRngUpper(12543),
|
| 1278 |
+
new InternalRuleDefEnd(),
|
| 1279 |
+
],
|
| 1280 |
+
[
|
| 1281 |
+
new InternalRuleDefChar([12289]),
|
| 1282 |
+
new InternalRuleDefCharRngUpper(12350),
|
| 1283 |
+
new InternalRuleDefEnd(),
|
| 1284 |
+
],
|
| 1285 |
+
[
|
| 1286 |
+
new InternalRuleDefChar([19968]),
|
| 1287 |
+
new InternalRuleDefCharRngUpper(40959),
|
| 1288 |
+
new InternalRuleDefEnd(),
|
| 1289 |
+
],
|
| 1290 |
+
],
|
| 1291 |
+
},
|
| 1292 |
+
];
|
| 1293 |
+
|
| 1294 |
+
describe('RulesBuilder', () => {
|
| 1295 |
+
it.each(testCases)('parses the grammar: $key', ({ grammar, symbolIds, rules }) => {
|
| 1296 |
+
const parsedGrammar = new RulesBuilder(grammar.replaceAll('\\n', '\n'));
|
| 1297 |
+
expect(parsedGrammar.rules).toStrictEqual(rules);
|
| 1298 |
+
expect([...parsedGrammar.symbolIds.items()]).toEqual(symbolIds);
|
| 1299 |
+
});
|
| 1300 |
+
|
| 1301 |
+
it('parses escapes in the grammar itself, rather than relying on them being replaced', () => {
|
| 1302 |
+
const parsedGrammar = new RulesBuilder(String.raw`root ::= [^\n] "\t" "\x41"`);
|
| 1303 |
+
expect(parsedGrammar.rules).toStrictEqual([
|
| 1304 |
+
[
|
| 1305 |
+
new InternalRuleDefCharNot([10]),
|
| 1306 |
+
new InternalRuleDefChar([9]),
|
| 1307 |
+
new InternalRuleDefChar([65]),
|
| 1308 |
+
new InternalRuleDefEnd(),
|
| 1309 |
+
],
|
| 1310 |
+
]);
|
| 1311 |
+
});
|
| 1312 |
+
|
| 1313 |
+
it('parses characters outside of the basic multilingual plane as one character', () => {
|
| 1314 |
+
const parsedGrammar = new RulesBuilder('root ::= "😀" [😀-😎]');
|
| 1315 |
+
expect(parsedGrammar.rules).toStrictEqual([
|
| 1316 |
+
[
|
| 1317 |
+
new InternalRuleDefChar([0x1f600]),
|
| 1318 |
+
new InternalRuleDefChar([0x1f600]),
|
| 1319 |
+
new InternalRuleDefCharRngUpper(0x1f60e),
|
| 1320 |
+
new InternalRuleDefEnd(),
|
| 1321 |
+
],
|
| 1322 |
+
]);
|
| 1323 |
+
});
|
| 1324 |
+
|
| 1325 |
+
it('counts positions in code points when reporting errors', () => {
|
| 1326 |
+
const grammar = 'root ::= "😀😀" foo';
|
| 1327 |
+
expect(() => new RulesBuilder(grammar)).toThrow(
|
| 1328 |
+
new GrammarParseError(grammar, 14, 'Undefined rule identifier "foo"'),
|
| 1329 |
+
);
|
| 1330 |
+
expect(() => new RulesBuilder(grammar)).toThrow(
|
| 1331 |
+
/Undefined rule identifier "foo"\n\nroot ::= "😀😀" foo\n {14}\^/,
|
| 1332 |
+
);
|
| 1333 |
+
});
|
| 1334 |
+
|
| 1335 |
+
it('allows windows line endings', () => {
|
| 1336 |
+
const parsedGrammar = new RulesBuilder('root ::= foo\r\nfoo ::= "a"\r\n');
|
| 1337 |
+
expect(parsedGrammar.rules).toStrictEqual([
|
| 1338 |
+
[new InternalRuleDefReference(1), new InternalRuleDefEnd()],
|
| 1339 |
+
[new InternalRuleDefChar([97]), new InternalRuleDefEnd()],
|
| 1340 |
+
]);
|
| 1341 |
+
expect([...parsedGrammar.symbolIds.items()]).toEqual([
|
| 1342 |
+
['root', 0],
|
| 1343 |
+
['foo', 1],
|
| 1344 |
+
]);
|
| 1345 |
+
});
|
| 1346 |
+
|
| 1347 |
+
it('allows comments, and rules that span multiple lines', () => {
|
| 1348 |
+
const parsedGrammar = new RulesBuilder(
|
| 1349 |
+
'# a comment\nroot ::= "a" | # another\n "b"\n# the end',
|
| 1350 |
+
);
|
| 1351 |
+
expect(parsedGrammar.rules).toStrictEqual([
|
| 1352 |
+
[
|
| 1353 |
+
new InternalRuleDefChar([97]),
|
| 1354 |
+
new InternalRuleDefAlt(),
|
| 1355 |
+
new InternalRuleDefChar([98]),
|
| 1356 |
+
new InternalRuleDefEnd(),
|
| 1357 |
+
],
|
| 1358 |
+
]);
|
| 1359 |
+
});
|
| 1360 |
+
|
| 1361 |
+
describe('errors', () => {
|
| 1362 |
+
const endOfInput = 'Unexpected end of grammar input, failed to complete parse';
|
| 1363 |
+
|
| 1364 |
+
it.each([
|
| 1365 |
+
['an unterminated string', 'root ::= "foo', 13, endOfInput],
|
| 1366 |
+
['an unterminated character class', 'root ::= [a-z', 13, endOfInput],
|
| 1367 |
+
['an unterminated range', 'root ::= [a-', 12, endOfInput],
|
| 1368 |
+
['an unterminated group', 'root ::= ("a"', 13, "Expecting ')' at 13"],
|
| 1369 |
+
['a lone opening parenthesis', 'root ::= (', 10, "Expecting ')' at 10"],
|
| 1370 |
+
['a trailing backslash', 'root ::= "\\', 10, 'Unknown escape at \\'],
|
| 1371 |
+
['an unknown escape', 'root ::= "\\q"', 10, 'Unknown escape at \\'],
|
| 1372 |
+
['a malformed hex escape', 'root ::= "\\xZZ"', 10, 'Invalid hexadecimal escape \\xZZ'],
|
| 1373 |
+
['a hex escape that is cut short', 'root ::= "\\x4', 10, 'Invalid hexadecimal escape \\x4'],
|
| 1374 |
+
[
|
| 1375 |
+
'a quantifier with nothing before it',
|
| 1376 |
+
'root ::= * "a"',
|
| 1377 |
+
9,
|
| 1378 |
+
'Expecting preceding item to */+/? at 9',
|
| 1379 |
+
],
|
| 1380 |
+
[
|
| 1381 |
+
'a quantifier at the start of a group',
|
| 1382 |
+
'root ::= ( * )',
|
| 1383 |
+
11,
|
| 1384 |
+
'Expecting preceding item to */+/? at 11',
|
| 1385 |
+
],
|
| 1386 |
+
['a rule without a name', '::= "a"', 0, 'Failed to find a valid name'],
|
| 1387 |
+
['junk after a rule', 'root ::= "a" 1', 13, 'Expecting newline or end at 13'],
|
| 1388 |
+
])('reports %s', (_description, grammar, pos, reason) => {
|
| 1389 |
+
expect(() => new RulesBuilder(grammar)).toThrow(new GrammarParseError(grammar, pos, reason));
|
| 1390 |
+
});
|
| 1391 |
+
|
| 1392 |
+
it('gives up if parsing takes longer than the limit', () => {
|
| 1393 |
+
const grammar = 'root ::= "a"';
|
| 1394 |
+
expect(() => new RulesBuilder(grammar, -1)).toThrow(
|
| 1395 |
+
new GrammarParseError(grammar, 10, 'Duration of -1 exceeded'),
|
| 1396 |
+
);
|
| 1397 |
+
});
|
| 1398 |
+
});
|
| 1399 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/rules-builder.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GrammarParseError } from '../utils/errors/index.js';
|
| 2 |
+
import { isWordChar } from './is-word-char.js';
|
| 3 |
+
import { parseChar } from './parse-char.js';
|
| 4 |
+
import { parseName } from './parse-name.js';
|
| 5 |
+
import { parseSpace } from './parse-space.js';
|
| 6 |
+
import {
|
| 7 |
+
type InternalRuleDef,
|
| 8 |
+
InternalRuleDefAlt,
|
| 9 |
+
InternalRuleDefChar,
|
| 10 |
+
InternalRuleDefCharAlt,
|
| 11 |
+
InternalRuleDefCharNot,
|
| 12 |
+
InternalRuleDefCharRngUpper,
|
| 13 |
+
InternalRuleDefEnd,
|
| 14 |
+
InternalRuleDefReference,
|
| 15 |
+
isRuleDefRef,
|
| 16 |
+
} from './rules-builder-types.js';
|
| 17 |
+
import { SymbolIds } from './symbol-ids.js';
|
| 18 |
+
|
| 19 |
+
export type InternalRuleDefClass =
|
| 20 |
+
| typeof InternalRuleDefChar
|
| 21 |
+
| typeof InternalRuleDefCharNot
|
| 22 |
+
| typeof InternalRuleDefCharAlt
|
| 23 |
+
| typeof InternalRuleDefCharRngUpper
|
| 24 |
+
| typeof InternalRuleDefAlt
|
| 25 |
+
| typeof InternalRuleDefEnd;
|
| 26 |
+
|
| 27 |
+
export function getOutElements(
|
| 28 |
+
typeOfRule: InternalRuleDefClass,
|
| 29 |
+
startcharValue: number,
|
| 30 |
+
): InternalRuleDef {
|
| 31 |
+
if (typeOfRule === InternalRuleDefChar) {
|
| 32 |
+
return new InternalRuleDefChar([startcharValue]);
|
| 33 |
+
}
|
| 34 |
+
if (typeOfRule === InternalRuleDefCharNot) {
|
| 35 |
+
return new InternalRuleDefCharNot([startcharValue]);
|
| 36 |
+
}
|
| 37 |
+
if (typeOfRule === InternalRuleDefCharRngUpper) {
|
| 38 |
+
return new InternalRuleDefCharRngUpper(startcharValue);
|
| 39 |
+
}
|
| 40 |
+
if (typeOfRule === InternalRuleDefAlt) {
|
| 41 |
+
return new InternalRuleDefAlt();
|
| 42 |
+
}
|
| 43 |
+
if (typeOfRule === InternalRuleDefEnd) {
|
| 44 |
+
return new InternalRuleDefEnd();
|
| 45 |
+
}
|
| 46 |
+
if (typeOfRule === InternalRuleDefCharAlt) {
|
| 47 |
+
return new InternalRuleDefCharAlt(startcharValue);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
throw new Error(`Invalid type: ${String(typeOfRule)}`);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// the same characters as Python's `str.isspace()`
|
| 54 |
+
const WHITESPACE =
|
| 55 |
+
/^[\t-\r\x1c-\x20\x85\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]$/;
|
| 56 |
+
|
| 57 |
+
const isSpace = (char: string): boolean => WHITESPACE.test(char);
|
| 58 |
+
|
| 59 |
+
export class RulesBuilder {
|
| 60 |
+
pos: number;
|
| 61 |
+
symbolIds: SymbolIds;
|
| 62 |
+
rules: InternalRuleDef[][];
|
| 63 |
+
src: string;
|
| 64 |
+
start: number;
|
| 65 |
+
/** In seconds. */
|
| 66 |
+
timeLimit: number;
|
| 67 |
+
|
| 68 |
+
/** The grammar, split into code points; `pos` is an index into this. */
|
| 69 |
+
private readonly chars: readonly string[];
|
| 70 |
+
|
| 71 |
+
constructor(src: string, limit = 1000) {
|
| 72 |
+
this.pos = 0;
|
| 73 |
+
this.symbolIds = new SymbolIds();
|
| 74 |
+
this.rules = [];
|
| 75 |
+
this.src = src;
|
| 76 |
+
this.chars = Array.from(src);
|
| 77 |
+
this.start = performance.now();
|
| 78 |
+
this.timeLimit = limit;
|
| 79 |
+
this.parse();
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
private parse(): void {
|
| 83 |
+
const chars = this.chars;
|
| 84 |
+
this.pos = parseSpace(chars, 0, true);
|
| 85 |
+
while (this.pos < chars.length) {
|
| 86 |
+
this.parseRule();
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// Validate the state to ensure that all rules are defined
|
| 90 |
+
for (const rule of this.rules) {
|
| 91 |
+
for (const elem of rule) {
|
| 92 |
+
if (isRuleDefRef(elem)) {
|
| 93 |
+
const ruleExists = elem.value < this.rules.length && this.rules[elem.value].length > 0;
|
| 94 |
+
if (!ruleExists) {
|
| 95 |
+
const missingRuleName = this.symbolIds.reverseGet(elem.value);
|
| 96 |
+
let missingRulePos = this.symbolIds.getPos(missingRuleName);
|
| 97 |
+
|
| 98 |
+
// Skip over the ::= and any whitespace
|
| 99 |
+
while (
|
| 100 |
+
missingRulePos < chars.length &&
|
| 101 |
+
(chars[missingRulePos] === ':' ||
|
| 102 |
+
chars[missingRulePos] === '=' ||
|
| 103 |
+
isSpace(chars[missingRulePos]))
|
| 104 |
+
) {
|
| 105 |
+
missingRulePos += 1;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
throw new GrammarParseError(
|
| 109 |
+
this.src,
|
| 110 |
+
missingRulePos,
|
| 111 |
+
`Undefined rule identifier "${missingRuleName}"`,
|
| 112 |
+
);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
private parseRule(): void {
|
| 120 |
+
const chars = this.chars;
|
| 121 |
+
const name = parseName(chars, this.pos);
|
| 122 |
+
this.pos = parseSpace(chars, this.pos + name.length, false);
|
| 123 |
+
const ruleId = this.getSymbolId(name, name.length);
|
| 124 |
+
|
| 125 |
+
this.pos = parseSpace(chars, this.pos, true);
|
| 126 |
+
if (
|
| 127 |
+
!(
|
| 128 |
+
this.pos + 2 < chars.length && // Ensure the position + 2 is within bounds
|
| 129 |
+
chars[this.pos] === ':' &&
|
| 130 |
+
chars[this.pos + 1] === ':' &&
|
| 131 |
+
chars[this.pos + 2] === '='
|
| 132 |
+
)
|
| 133 |
+
) {
|
| 134 |
+
throw new GrammarParseError(this.src, this.pos, `Expecting ::= at ${this.pos}`);
|
| 135 |
+
}
|
| 136 |
+
this.pos += 3;
|
| 137 |
+
this.pos = parseSpace(chars, this.pos, true);
|
| 138 |
+
|
| 139 |
+
this.parseAlternates(name, ruleId);
|
| 140 |
+
|
| 141 |
+
// Check if this.pos is within the bounds of src before checking for a carriage return
|
| 142 |
+
if (this.pos < chars.length && chars[this.pos] === '\r') {
|
| 143 |
+
this.pos += chars[this.pos + 1] === '\n' ? 2 : 1;
|
| 144 |
+
} else if (this.pos < chars.length && chars[this.pos] === '\n') {
|
| 145 |
+
this.pos += 1;
|
| 146 |
+
} else if (this.pos < chars.length) {
|
| 147 |
+
throw new GrammarParseError(this.src, this.pos, `Expecting newline or end at ${this.pos}`);
|
| 148 |
+
}
|
| 149 |
+
this.pos = parseSpace(chars, this.pos, true);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
private getSymbolId(src: string, length: number): number {
|
| 153 |
+
const nextId = this.symbolIds.size;
|
| 154 |
+
const key = src.slice(0, length);
|
| 155 |
+
if (!this.symbolIds.has(key)) {
|
| 156 |
+
this.symbolIds.set(key, nextId, this.pos);
|
| 157 |
+
}
|
| 158 |
+
return this.symbolIds.get(key) as number;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
private generateSymbolId(baseName: string): number {
|
| 162 |
+
const nextId = this.symbolIds.size;
|
| 163 |
+
this.symbolIds.set(`${baseName}_${nextId}`, nextId, this.pos);
|
| 164 |
+
return nextId;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
private addRule(ruleId: number, rule: InternalRuleDef[]): void {
|
| 168 |
+
while (this.rules.length <= ruleId) {
|
| 169 |
+
this.rules.push([]);
|
| 170 |
+
}
|
| 171 |
+
this.rules[ruleId] = rule;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
private checkDuration(): void {
|
| 175 |
+
if ((performance.now() - this.start) / 1000 > this.timeLimit) {
|
| 176 |
+
throw new GrammarParseError(this.src, this.pos, `Duration of ${this.timeLimit} exceeded`);
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
private parseSequence(ruleName: string, outElements: InternalRuleDef[], depth = 0): void {
|
| 181 |
+
const isNested = depth !== 0;
|
| 182 |
+
const chars = this.chars;
|
| 183 |
+
let lastSymStart = outElements.length;
|
| 184 |
+
|
| 185 |
+
while (this.pos < chars.length) {
|
| 186 |
+
const char = chars[this.pos];
|
| 187 |
+
if (char === '"') {
|
| 188 |
+
this.pos += 1;
|
| 189 |
+
lastSymStart = outElements.length;
|
| 190 |
+
while (chars[this.pos] !== '"') {
|
| 191 |
+
this.checkDuration();
|
| 192 |
+
const [value, incPos] = parseChar(chars, this.pos);
|
| 193 |
+
outElements.push(new InternalRuleDefChar([value]));
|
| 194 |
+
this.pos += incPos;
|
| 195 |
+
}
|
| 196 |
+
this.pos = parseSpace(chars, this.pos + 1, isNested);
|
| 197 |
+
} else if (char === '[') {
|
| 198 |
+
this.pos += 1;
|
| 199 |
+
let startType: InternalRuleDefClass = InternalRuleDefChar;
|
| 200 |
+
if (chars[this.pos] === '^') {
|
| 201 |
+
this.pos += 1;
|
| 202 |
+
startType = InternalRuleDefCharNot;
|
| 203 |
+
}
|
| 204 |
+
lastSymStart = outElements.length;
|
| 205 |
+
while (chars[this.pos] !== ']') {
|
| 206 |
+
this.checkDuration();
|
| 207 |
+
const type: InternalRuleDefClass =
|
| 208 |
+
lastSymStart < outElements.length ? InternalRuleDefCharAlt : startType;
|
| 209 |
+
const [startcharValue, startIncPos] = parseChar(chars, this.pos);
|
| 210 |
+
this.pos += startIncPos;
|
| 211 |
+
outElements.push(getOutElements(type, startcharValue));
|
| 212 |
+
|
| 213 |
+
if (chars[this.pos] === '-' && chars[this.pos + 1] !== ']') {
|
| 214 |
+
this.pos += 1;
|
| 215 |
+
const [endcharValue, endIncPos] = parseChar(chars, this.pos);
|
| 216 |
+
outElements.push(new InternalRuleDefCharRngUpper(endcharValue));
|
| 217 |
+
this.pos += endIncPos;
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
this.pos = parseSpace(chars, this.pos + 1, isNested);
|
| 221 |
+
} else if (isWordChar(char)) {
|
| 222 |
+
const name = parseName(chars, this.pos);
|
| 223 |
+
const refRuleId = this.getSymbolId(name, name.length);
|
| 224 |
+
this.pos += name.length;
|
| 225 |
+
this.pos = parseSpace(chars, this.pos, isNested);
|
| 226 |
+
|
| 227 |
+
lastSymStart = outElements.length;
|
| 228 |
+
outElements.push(new InternalRuleDefReference(refRuleId));
|
| 229 |
+
} else if (char === '(') {
|
| 230 |
+
this.pos = parseSpace(chars, this.pos + 1, true);
|
| 231 |
+
const subRuleId = this.generateSymbolId(ruleName);
|
| 232 |
+
this.parseAlternates(ruleName, subRuleId, depth + 1);
|
| 233 |
+
lastSymStart = outElements.length;
|
| 234 |
+
outElements.push(new InternalRuleDefReference(subRuleId));
|
| 235 |
+
if (chars[this.pos] !== ')') {
|
| 236 |
+
throw new GrammarParseError(this.src, this.pos, `Expecting ')' at ${this.pos}`);
|
| 237 |
+
}
|
| 238 |
+
this.pos = parseSpace(chars, this.pos + 1, isNested);
|
| 239 |
+
} else if (char === '*' || char === '+' || char === '?') {
|
| 240 |
+
if (lastSymStart === outElements.length) {
|
| 241 |
+
throw new GrammarParseError(
|
| 242 |
+
this.src,
|
| 243 |
+
this.pos,
|
| 244 |
+
`Expecting preceding item to */+/? at ${this.pos}`,
|
| 245 |
+
);
|
| 246 |
+
}
|
| 247 |
+
const subRuleId = this.generateSymbolId(ruleName);
|
| 248 |
+
const subRule = outElements.slice(lastSymStart);
|
| 249 |
+
if (char === '*' || char === '+') {
|
| 250 |
+
subRule.push(new InternalRuleDefReference(subRuleId));
|
| 251 |
+
}
|
| 252 |
+
subRule.push(new InternalRuleDefAlt());
|
| 253 |
+
if (char === '+') {
|
| 254 |
+
subRule.push(...outElements.slice(lastSymStart));
|
| 255 |
+
}
|
| 256 |
+
subRule.push(new InternalRuleDefEnd());
|
| 257 |
+
this.addRule(subRuleId, subRule);
|
| 258 |
+
outElements.splice(
|
| 259 |
+
lastSymStart,
|
| 260 |
+
outElements.length - lastSymStart,
|
| 261 |
+
new InternalRuleDefReference(subRuleId),
|
| 262 |
+
);
|
| 263 |
+
this.pos = parseSpace(chars, this.pos + 1, isNested);
|
| 264 |
+
} else {
|
| 265 |
+
break;
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
private parseAlternates(ruleName: string, ruleId: number, depth = 0): void {
|
| 271 |
+
const chars = this.chars;
|
| 272 |
+
const rule: InternalRuleDef[] = [];
|
| 273 |
+
this.parseSequence(ruleName, rule, depth);
|
| 274 |
+
// Ensure that this.pos is within bounds before checking chars[this.pos]
|
| 275 |
+
while (this.pos < chars.length && chars[this.pos] === '|') {
|
| 276 |
+
this.checkDuration();
|
| 277 |
+
rule.push(new InternalRuleDefAlt());
|
| 278 |
+
this.pos = parseSpace(chars, this.pos + 1, true);
|
| 279 |
+
this.parseSequence(ruleName, rule, depth);
|
| 280 |
+
}
|
| 281 |
+
rule.push(new InternalRuleDefEnd());
|
| 282 |
+
this.addRule(ruleId, rule);
|
| 283 |
+
}
|
| 284 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/symbol-ids.test.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { SymbolIds } from './symbol-ids.js';
|
| 3 |
+
|
| 4 |
+
describe('SymbolIds', () => {
|
| 5 |
+
const build = (): SymbolIds => {
|
| 6 |
+
const symbolIds = new SymbolIds();
|
| 7 |
+
symbolIds.set('root', 0, 0);
|
| 8 |
+
symbolIds.set('foo', 1, 12);
|
| 9 |
+
return symbolIds;
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
it('is empty to begin with', () => {
|
| 13 |
+
expect(new SymbolIds().size).toBe(0);
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
it('looks up ids by name', () => {
|
| 17 |
+
const symbolIds = build();
|
| 18 |
+
expect(symbolIds.get('foo')).toBe(1);
|
| 19 |
+
expect(symbolIds.get('bar')).toBeUndefined();
|
| 20 |
+
expect(symbolIds.has('root')).toBe(true);
|
| 21 |
+
expect(symbolIds.has('bar')).toBe(false);
|
| 22 |
+
expect(symbolIds.size).toBe(2);
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
it('iterates over the names and ids in the order they were set', () => {
|
| 26 |
+
const symbolIds = build();
|
| 27 |
+
expect([...symbolIds.items()]).toEqual([
|
| 28 |
+
['root', 0],
|
| 29 |
+
['foo', 1],
|
| 30 |
+
]);
|
| 31 |
+
expect([...symbolIds]).toEqual([...symbolIds.items()]);
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
it('looks up names by id', () => {
|
| 35 |
+
expect(build().reverseGet(1)).toBe('foo');
|
| 36 |
+
expect(() => build().reverseGet(2)).toThrow('SymbolIds does not contain value: 2');
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
it('remembers where a name was first seen', () => {
|
| 40 |
+
expect(build().getPos('foo')).toBe(12);
|
| 41 |
+
expect(() => build().getPos('bar')).toThrow('SymbolIds does not contain key: bar');
|
| 42 |
+
});
|
| 43 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/rules-builder/symbol-ids.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/** Maps the names of a grammar's symbols to their ids, remembering where each was first seen. */
|
| 2 |
+
export class SymbolIds {
|
| 3 |
+
private readonly ids = new Map<string, number>();
|
| 4 |
+
private readonly positions = new Map<string, number>();
|
| 5 |
+
private readonly names = new Map<number, string>();
|
| 6 |
+
|
| 7 |
+
items(): IterableIterator<[string, number]> {
|
| 8 |
+
return this.ids.entries();
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
[Symbol.iterator](): IterableIterator<[string, number]> {
|
| 12 |
+
return this.items();
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
get size(): number {
|
| 16 |
+
return this.ids.size;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
get(key: string): number | undefined {
|
| 20 |
+
return this.ids.get(key);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
set(key: string, value: number, pos: number): void {
|
| 24 |
+
this.ids.set(key, value);
|
| 25 |
+
this.names.set(value, key);
|
| 26 |
+
this.positions.set(key, pos);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
has(key: string): boolean {
|
| 30 |
+
return this.ids.has(key);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
reverseGet(id: number): string {
|
| 34 |
+
const name = this.names.get(id);
|
| 35 |
+
if (name === undefined) {
|
| 36 |
+
throw new Error(`SymbolIds does not contain value: ${id}`);
|
| 37 |
+
}
|
| 38 |
+
return name;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
getPos(key: string): number {
|
| 42 |
+
const pos = this.positions.get(key);
|
| 43 |
+
if (pos === undefined) {
|
| 44 |
+
throw new Error(`SymbolIds does not contain key: ${key}`);
|
| 45 |
+
}
|
| 46 |
+
return pos;
|
| 47 |
+
}
|
| 48 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/code-points.test.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { codePointLength, fromCodePoints, toCodePoints } from './code-points.js';
|
| 3 |
+
|
| 4 |
+
describe('code points', () => {
|
| 5 |
+
describe('toCodePoints', () => {
|
| 6 |
+
it('converts text with characters outside of the basic multilingual plane', () => {
|
| 7 |
+
expect(toCodePoints('a😀b')).toEqual([97, 0x1f600, 98]);
|
| 8 |
+
});
|
| 9 |
+
|
| 10 |
+
it('converts empty text', () => {
|
| 11 |
+
expect(toCodePoints('')).toEqual([]);
|
| 12 |
+
});
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
describe('fromCodePoints', () => {
|
| 16 |
+
it('is the inverse of toCodePoints', () => {
|
| 17 |
+
expect(fromCodePoints(toCodePoints('a😀ぁb'))).toBe('a😀ぁb');
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
it('handles more code points than can be spread in one call', () => {
|
| 21 |
+
const codePoints = Array.from({ length: 300_000 }, (_, i) => 97 + (i % 26));
|
| 22 |
+
expect(fromCodePoints(codePoints).length).toBe(300_000);
|
| 23 |
+
});
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
describe('codePointLength', () => {
|
| 27 |
+
it('counts code points rather than UTF-16 code units', () => {
|
| 28 |
+
expect(codePointLength('a😀b')).toBe(3);
|
| 29 |
+
expect('a😀b'.length).toBe(4);
|
| 30 |
+
});
|
| 31 |
+
});
|
| 32 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/code-points.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const CHUNK_SIZE = 4096;
|
| 2 |
+
|
| 3 |
+
/** Splits text into its Unicode code points (not UTF-16 code units). */
|
| 4 |
+
export function toCodePoints(text: string): number[] {
|
| 5 |
+
return Array.from(text, (char) => char.codePointAt(0) as number);
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
/** The inverse of `toCodePoints`. */
|
| 9 |
+
export function fromCodePoints(codePoints: readonly number[]): string {
|
| 10 |
+
let text = '';
|
| 11 |
+
// spreading a very large array into a single call overflows the argument limit
|
| 12 |
+
for (let i = 0; i < codePoints.length; i += CHUNK_SIZE) {
|
| 13 |
+
text += String.fromCodePoint(...codePoints.slice(i, i + CHUNK_SIZE));
|
| 14 |
+
}
|
| 15 |
+
return text;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
/** The number of Unicode code points in text. */
|
| 19 |
+
export function codePointLength(text: string): number {
|
| 20 |
+
return Array.from(text).length;
|
| 21 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/build-error-position.test.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import { buildErrorPosition } from './build-error-position.js';
|
| 3 |
+
|
| 4 |
+
interface Expected {
|
| 5 |
+
lines: string[];
|
| 6 |
+
caret: string;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
type Case = [description: string, grammar: string[], pos: number, expected: Expected];
|
| 10 |
+
|
| 11 |
+
describe('buildErrorPosition', () => {
|
| 12 |
+
const cases: Case[] = [
|
| 13 |
+
[
|
| 14 |
+
'a single line, near the start',
|
| 15 |
+
['root ::= "foo"'],
|
| 16 |
+
1,
|
| 17 |
+
{ lines: ['root ::= "foo"'], caret: ' ^' },
|
| 18 |
+
],
|
| 19 |
+
[
|
| 20 |
+
'a single line, further along',
|
| 21 |
+
['root ::= "foo"'],
|
| 22 |
+
5,
|
| 23 |
+
{ lines: ['root ::= "foo"'], caret: ' ^' },
|
| 24 |
+
],
|
| 25 |
+
[
|
| 26 |
+
'multiple lines with pos on the first line',
|
| 27 |
+
['aa', 'bb'],
|
| 28 |
+
1,
|
| 29 |
+
{ lines: ['aa'], caret: ' ^' },
|
| 30 |
+
],
|
| 31 |
+
[
|
| 32 |
+
'multiple lines with pos on the second line, first character',
|
| 33 |
+
['aa', 'bb'],
|
| 34 |
+
2,
|
| 35 |
+
{ lines: ['aa', 'bb'], caret: '^' },
|
| 36 |
+
],
|
| 37 |
+
[
|
| 38 |
+
'multiple lines with pos on the second line, second character',
|
| 39 |
+
['aa', 'bb'],
|
| 40 |
+
2 + 1,
|
| 41 |
+
{ lines: ['aa', 'bb'], caret: ' ^' },
|
| 42 |
+
],
|
| 43 |
+
[
|
| 44 |
+
'multiple lines beyond the error with pos on the second line',
|
| 45 |
+
['aa', 'bb', 'cc'],
|
| 46 |
+
2 + 1,
|
| 47 |
+
{ lines: ['aa', 'bb'], caret: ' ^' },
|
| 48 |
+
],
|
| 49 |
+
[
|
| 50 |
+
'multiple lines with pos on the third line, first char',
|
| 51 |
+
['aa', 'bb', 'cc'],
|
| 52 |
+
2 + 2 + 0,
|
| 53 |
+
{ lines: ['aa', 'bb', 'cc'], caret: '^' },
|
| 54 |
+
],
|
| 55 |
+
[
|
| 56 |
+
'multiple lines with pos on the third line, second char',
|
| 57 |
+
['aa', 'bb', 'cc'],
|
| 58 |
+
2 + 2 + 1,
|
| 59 |
+
{ lines: ['aa', 'bb', 'cc'], caret: ' ^' },
|
| 60 |
+
],
|
| 61 |
+
[
|
| 62 |
+
'multiple lines with pos on the fourth line, first char',
|
| 63 |
+
['aa', 'bb', 'cc', 'dd'],
|
| 64 |
+
2 + 2 + 2 + 0,
|
| 65 |
+
{ lines: ['bb', 'cc', 'dd'], caret: '^' },
|
| 66 |
+
],
|
| 67 |
+
[
|
| 68 |
+
'multiple lines with pos on the fifth line, second char',
|
| 69 |
+
['aa', 'bb', 'cc', 'dd', 'ee'],
|
| 70 |
+
2 + 2 + 2 + 2 + 1,
|
| 71 |
+
{ lines: ['cc', 'dd', 'ee'], caret: ' ^' },
|
| 72 |
+
],
|
| 73 |
+
];
|
| 74 |
+
|
| 75 |
+
it.each(cases)('correctly shows the position for %s', (_description, grammar, pos, expected) => {
|
| 76 |
+
expect(buildErrorPosition(grammar.join('\n'), pos)).toEqual([
|
| 77 |
+
...expected.lines,
|
| 78 |
+
expected.caret,
|
| 79 |
+
]);
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
it('renders a message for empty input', () => {
|
| 83 |
+
expect(buildErrorPosition('', 0)).toEqual(['No input provided']);
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
+
it('counts the position in code points', () => {
|
| 87 |
+
expect(buildErrorPosition('"😀😀" x', 5)).toEqual(['"😀😀" x', ' ^']);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
it('does not run past the last line when the position is past the end of the lines', () => {
|
| 91 |
+
expect(buildErrorPosition('ab\nc', 3)).toEqual(['ab', 'c', '^']);
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
it('never renders a negative number of spaces', () => {
|
| 95 |
+
expect(buildErrorPosition('abc', -1)).toEqual(['abc', '^']);
|
| 96 |
+
});
|
| 97 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/build-error-position.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { codePointLength } from '../code-points.js';
|
| 2 |
+
|
| 3 |
+
export const MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW = 3;
|
| 4 |
+
|
| 5 |
+
/**
|
| 6 |
+
* Renders the lines of `src` leading up to `pos`, followed by a line with a caret under the error.
|
| 7 |
+
* `pos` is an index in Unicode code points.
|
| 8 |
+
*/
|
| 9 |
+
export function buildErrorPosition(src: string, pos: number): string[] {
|
| 10 |
+
if (src === '') {
|
| 11 |
+
return ['No input provided'];
|
| 12 |
+
}
|
| 13 |
+
const lines = src.split('\n');
|
| 14 |
+
const srcLength = codePointLength(src);
|
| 15 |
+
|
| 16 |
+
let lineIdx = 0;
|
| 17 |
+
while (
|
| 18 |
+
lineIdx < lines.length &&
|
| 19 |
+
lines[lineIdx] &&
|
| 20 |
+
pos > codePointLength(lines[lineIdx]) - 1 &&
|
| 21 |
+
pos < srcLength
|
| 22 |
+
) {
|
| 23 |
+
pos -= codePointLength(lines[lineIdx]);
|
| 24 |
+
lineIdx += 1;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const linesToShow = lines.slice(
|
| 28 |
+
Math.max(0, lineIdx - (MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW - 1)),
|
| 29 |
+
lineIdx + 1,
|
| 30 |
+
);
|
| 31 |
+
|
| 32 |
+
return [...linesToShow, ' '.repeat(Math.max(0, pos)) + '^'];
|
| 33 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/errors-types.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ValidInput can either be a string, or a number indicating a code point, or a list of code points.
|
| 2 |
+
// It CANNOT be a number representing a number; a number intended as input (like "8")
|
| 3 |
+
// should be passed in as a string.
|
| 4 |
+
export type ValidInput = string | number | number[];
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/get-input-as-string.test.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import type { ValidInput } from './errors-types.js';
|
| 3 |
+
import { getInputAsString } from './get-input-as-string.js';
|
| 4 |
+
|
| 5 |
+
describe('getInputAsString', () => {
|
| 6 |
+
const cases: Array<[input: ValidInput, expected: string]> = [
|
| 7 |
+
['hello', 'hello'],
|
| 8 |
+
[[104, 101, 108, 108, 111], 'hello'],
|
| 9 |
+
[104, 'h'],
|
| 10 |
+
[0x1f600, '😀'],
|
| 11 |
+
[[0x1f600, 0x1f601], '😀😁'],
|
| 12 |
+
];
|
| 13 |
+
|
| 14 |
+
it.each(cases)('correctly converts %j to a string', (input, expected) => {
|
| 15 |
+
expect(getInputAsString(input)).toBe(expected);
|
| 16 |
+
});
|
| 17 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/get-input-as-string.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { fromCodePoints } from '../code-points.js';
|
| 2 |
+
import type { ValidInput } from './errors-types.js';
|
| 3 |
+
|
| 4 |
+
export function getInputAsString(src: ValidInput): string {
|
| 5 |
+
if (typeof src === 'string') {
|
| 6 |
+
return src;
|
| 7 |
+
}
|
| 8 |
+
if (typeof src === 'number') {
|
| 9 |
+
return String.fromCodePoint(src);
|
| 10 |
+
}
|
| 11 |
+
return fromCodePoints(src);
|
| 12 |
+
}
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/grammar-parse-error.test.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, it } from 'vitest';
|
| 2 |
+
import {
|
| 3 |
+
GRAMMAR_PARSER_ERROR_HEADER_MESSAGE,
|
| 4 |
+
GrammarParseError,
|
| 5 |
+
} from './grammar-parse-error.js';
|
| 6 |
+
|
| 7 |
+
describe('GrammarParseError', () => {
|
| 8 |
+
it('renders a message', () => {
|
| 9 |
+
const grammar = 'aa\nbb\ncc\ndd\nee';
|
| 10 |
+
const pos = 5;
|
| 11 |
+
const reason = 'reason';
|
| 12 |
+
const err = new GrammarParseError(grammar, pos, reason);
|
| 13 |
+
expect(err.message).toBe(
|
| 14 |
+
[GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason), '', 'aa', 'bb', 'cc', ' ^'].join('\n'),
|
| 15 |
+
);
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
it('is an error that keeps what it was given', () => {
|
| 19 |
+
const err = new GrammarParseError('root ::= foo', 9, 'reason');
|
| 20 |
+
expect(err).toBeInstanceOf(Error);
|
| 21 |
+
expect(err).toBeInstanceOf(GrammarParseError);
|
| 22 |
+
expect(err.name).toBe('GrammarParseError');
|
| 23 |
+
expect(err.grammar).toBe('root ::= foo');
|
| 24 |
+
expect(err.pos).toBe(9);
|
| 25 |
+
expect(err.reason).toBe('reason');
|
| 26 |
+
});
|
| 27 |
+
});
|
v2-models-effort/runs/20261103-021157-icy-block/ported_implementation/src/utils/errors/grammar-parse-error.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { buildErrorPosition } from './build-error-position.js';
|
| 2 |
+
|
| 3 |
+
export const GRAMMAR_PARSER_ERROR_HEADER_MESSAGE = (reason: string): string =>
|
| 4 |
+
`Failed to parse grammar: ${reason}`;
|
| 5 |
+
|
| 6 |
+
export class GrammarParseError extends Error {
|
| 7 |
+
grammar: string;
|
| 8 |
+
pos: number;
|
| 9 |
+
reason: string;
|
| 10 |
+
|
| 11 |
+
constructor(grammar: string, pos: number, reason: string) {
|
| 12 |
+
super(
|
| 13 |
+
[GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason), '', ...buildErrorPosition(grammar, pos)].join(
|
| 14 |
+
'\n',
|
| 15 |
+
),
|
| 16 |
+
);
|
| 17 |
+
this.name = 'GrammarParseError';
|
| 18 |
+
this.grammar = grammar;
|
| 19 |
+
this.pos = pos;
|
| 20 |
+
this.reason = reason;
|
| 21 |
+
}
|
| 22 |
+
}
|