@traqula/core 0.0.1-alpha.176 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/lib/CoreFactory.d.ts +39 -0
  2. package/lib/CoreFactory.js +135 -0
  3. package/lib/CoreFactory.js.map +1 -0
  4. package/lib/Transformer.d.ts +26 -0
  5. package/lib/Transformer.js +57 -0
  6. package/lib/Transformer.js.map +1 -0
  7. package/lib/generator-builder/builderTypes.d.ts +6 -6
  8. package/lib/generator-builder/builderTypes.js.map +1 -1
  9. package/lib/generator-builder/dynamicGenerator.d.ts +18 -3
  10. package/lib/generator-builder/dynamicGenerator.js +104 -17
  11. package/lib/generator-builder/dynamicGenerator.js.map +1 -1
  12. package/lib/generator-builder/generatorBuilder.d.ts +9 -7
  13. package/lib/generator-builder/generatorBuilder.js +4 -1
  14. package/lib/generator-builder/generatorBuilder.js.map +1 -1
  15. package/lib/generator-builder/generatorTypes.d.ts +13 -4
  16. package/lib/generator-builder/generatorTypes.js.map +1 -1
  17. package/lib/index.cjs +394 -13
  18. package/lib/index.d.ts +5 -0
  19. package/lib/index.js +6 -0
  20. package/lib/index.js.map +1 -1
  21. package/lib/indirection-builder/IndirBuilder.d.ts +43 -0
  22. package/lib/indirection-builder/IndirBuilder.js +60 -0
  23. package/lib/indirection-builder/IndirBuilder.js.map +1 -0
  24. package/lib/indirection-builder/dynamicIndirected.d.ts +9 -0
  25. package/lib/indirection-builder/dynamicIndirected.js +28 -0
  26. package/lib/indirection-builder/dynamicIndirected.js.map +1 -0
  27. package/lib/indirection-builder/helpers.d.ts +24 -0
  28. package/lib/indirection-builder/helpers.js +11 -0
  29. package/lib/indirection-builder/helpers.js.map +1 -0
  30. package/lib/lexer-builder/LexerBuilder.d.ts +4 -0
  31. package/lib/lexer-builder/LexerBuilder.js +5 -1
  32. package/lib/lexer-builder/LexerBuilder.js.map +1 -1
  33. package/lib/nodeTypings.d.ts +53 -0
  34. package/lib/nodeTypings.js +2 -0
  35. package/lib/nodeTypings.js.map +1 -0
  36. package/lib/parser-builder/builderTypes.d.ts +3 -1
  37. package/lib/parser-builder/builderTypes.js.map +1 -1
  38. package/lib/parser-builder/parserBuilder.d.ts +3 -2
  39. package/lib/parser-builder/parserBuilder.js +13 -3
  40. package/lib/parser-builder/parserBuilder.js.map +1 -1
  41. package/lib/utils.d.ts +1 -1
  42. package/lib/utils.js.map +1 -1
  43. package/package.json +2 -2
@@ -0,0 +1,43 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
2
+ import type { CheckOverlap } from '../utils';
3
+ import type { IndirDef, IndirectionMap, IndirectObjFromIndirDefs, ParseIndirsToObject } from './helpers';
4
+ export declare class IndirBuilder<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {
5
+ static create<Context, Names extends string, RuleDefs extends IndirectionMap<Names>>(args: IndirBuilder<Context, Names, RuleDefs>): IndirBuilder<Context, Names, RuleDefs>;
6
+ static create<Rules extends readonly IndirDef[] = readonly IndirDef[], Context = Rules[0] extends IndirDef<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>>(rules: Rules): IndirBuilder<Context, Names, RuleDefs>;
7
+ private rules;
8
+ private constructor();
9
+ widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names, RuleDefs>;
10
+ typePatch<Patch extends {
11
+ [Key in Names]?: any;
12
+ }>(): IndirBuilder<Context, Names, {
13
+ [Key in Names]: Key extends keyof Patch ? (IndirDef<Context, Key, Patch[Key][0], Patch[Key][1]>) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never);
14
+ }>;
15
+ /**
16
+ * Change the implementation of an existing indirection.
17
+ */
18
+ patchRule<U extends Names, RET, ARGS extends any[]>(patch: IndirDef<Context, U, RET, ARGS>): IndirBuilder<Context, Names, {
19
+ [Key in Names]: Key extends U ? IndirDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never);
20
+ }>;
21
+ /**
22
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
23
+ */
24
+ addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: IndirDef<Context, U, RET, ARGS>): IndirBuilder<Context, Names | U, {
25
+ [K in Names | U]: K extends U ? IndirDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
26
+ }>;
27
+ /**
28
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
29
+ */
30
+ addRule<U extends string, RET, ARGS extends any[]>(rule: CheckOverlap<U, Names, IndirDef<Context, U, RET, ARGS>>): IndirBuilder<Context, Names | U, {
31
+ [K in Names | U]: K extends U ? IndirDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
32
+ }>;
33
+ addMany<U extends readonly IndirDef<Context>[]>(...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>): IndirBuilder<Context, Names | ParseNamesFromList<U>, {
34
+ [K in Names | ParseNamesFromList<U>]: K extends keyof ParseIndirsToObject<typeof rules> ? (ParseIndirsToObject<typeof rules>[K] extends IndirDef<Context, K> ? ParseIndirsToObject<typeof rules>[K] : never) : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
35
+ }>;
36
+ /**
37
+ * Delete a grammar rule by its name.
38
+ */
39
+ deleteRule<U extends Names>(ruleName: U): IndirBuilder<Context, Exclude<Names, U>, {
40
+ [K in Exclude<Names, U>]: RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never;
41
+ }>;
42
+ build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs>;
43
+ }
@@ -0,0 +1,60 @@
1
+ import { DynamicIndirect } from './dynamicIndirected';
2
+ import { listToIndirectionMap } from './helpers';
3
+ export class IndirBuilder {
4
+ static create(start) {
5
+ if (start instanceof IndirBuilder) {
6
+ return new IndirBuilder({ ...start.rules });
7
+ }
8
+ return new IndirBuilder(listToIndirectionMap(start));
9
+ }
10
+ constructor(startRules) {
11
+ this.rules = startRules;
12
+ }
13
+ widenContext() {
14
+ return this;
15
+ }
16
+ typePatch() {
17
+ return this;
18
+ }
19
+ /**
20
+ * Change the implementation of an existing indirection.
21
+ */
22
+ patchRule(patch) {
23
+ const self = this;
24
+ self.rules[patch.name] = patch;
25
+ return self;
26
+ }
27
+ /**
28
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
29
+ */
30
+ addRuleRedundant(rule) {
31
+ const self = this;
32
+ const rules = self.rules;
33
+ if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {
34
+ throw new Error(`Function ${rule.name} already exists in the builder`);
35
+ }
36
+ rules[rule.name] = rule;
37
+ return self;
38
+ }
39
+ /**
40
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
41
+ */
42
+ addRule(rule) {
43
+ return this.addRuleRedundant(rule);
44
+ }
45
+ addMany(...rules) {
46
+ this.rules = { ...this.rules, ...listToIndirectionMap(rules) };
47
+ return this;
48
+ }
49
+ /**
50
+ * Delete a grammar rule by its name.
51
+ */
52
+ deleteRule(ruleName) {
53
+ delete this.rules[ruleName];
54
+ return this;
55
+ }
56
+ build() {
57
+ return new DynamicIndirect(this.rules);
58
+ }
59
+ }
60
+ //# sourceMappingURL=IndirBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IndirBuilder.js","sourceRoot":"","sources":["IndirBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,OAAO,YAAY;IAUhB,MAAM,CAAC,MAAM,CAMlB,KAAqD;QAErD,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;YAClC,OAAO,IAAI,YAAY,CA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EG;IACI,UAAU,CAAkB,QAAW;QAG5C,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC5B,OAEY,IAAI,CAAC;IACnB,CAAC;IAEM,KAAK;QACV,OAA4D,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC9F,CAAC;CACF","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes';\nimport type { CheckOverlap } from '../utils';\nimport { DynamicIndirect } from './dynamicIndirected';\nimport type { IndirDef, IndirectionMap, IndirectObjFromIndirDefs, ParseIndirsToObject } from './helpers';\nimport { listToIndirectionMap } from './helpers';\n\nexport class IndirBuilder<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {\n public static create<Context, Names extends string, RuleDefs extends IndirectionMap<Names>>(\n args: IndirBuilder<Context, Names, RuleDefs>\n ): IndirBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly IndirDef[] = readonly IndirDef[],\n Context = Rules[0] extends IndirDef<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>,\n >(rules: Rules): IndirBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly IndirDef[] = readonly IndirDef[],\n Context = Rules[0] extends IndirDef<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>,\n >(\n start: Rules | IndirBuilder<Context, Names, RuleDefs>,\n ): IndirBuilder<Context, Names, RuleDefs> {\n if (start instanceof IndirBuilder) {\n return new IndirBuilder({ ...start.rules });\n }\n return <IndirBuilder<Context, Names, RuleDefs>> <unknown> new IndirBuilder(listToIndirectionMap(start));\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names, RuleDefs> {\n return <IndirBuilder<NewContext, Names, RuleDefs>> <unknown> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: any }>():\n IndirBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n IndirDef<Context, Key, Patch[Key][0], Patch[Key][1]>\n ) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }> {\n return <IndirBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends IndirDef<Context, Key, any, infer Par> ? IndirDef<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing indirection.\n */\n public patchRule<U extends Names, RET, ARGS extends any[]>(patch: IndirDef<Context, U, RET, ARGS>):\n IndirBuilder<Context, Names, {[Key in Names]: Key extends U ?\n IndirDef<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <IndirBuilder<Context, Names, {[Key in Names]: Key extends U ?\n IndirDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: IndirDef<Context, U, RET, ARGS>):\n IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, IndirDef<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Function ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS extends any[]>(\n rule: CheckOverlap<U, Names, IndirDef<Context, U, RET, ARGS>>,\n ): IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly IndirDef<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): IndirBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseIndirsToObject<typeof rules> ? (\n ParseIndirsToObject<typeof rules>[K] extends IndirDef<Context, K> ? ParseIndirsToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToIndirectionMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n IndirBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <IndirBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n public build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs> {\n return <IndirectObjFromIndirDefs<Context, Names, RuleDefs>> new DynamicIndirect(this.rules);\n }\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import type { IndirDefArg, IndirectionMap } from './helpers';
2
+ export declare class DynamicIndirect<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {
3
+ protected rules: RuleDefs;
4
+ protected __context: Context | undefined;
5
+ constructor(rules: RuleDefs);
6
+ setContext(context: Context): void;
7
+ protected getSafeContext(): Context;
8
+ protected readonly subrule: IndirDefArg['SUBRULE'];
9
+ }
@@ -0,0 +1,28 @@
1
+ export class DynamicIndirect {
2
+ constructor(rules) {
3
+ this.rules = rules;
4
+ this.__context = undefined;
5
+ this.subrule = (cstDef, ...args) => {
6
+ const def = this.rules[cstDef.name];
7
+ if (!def) {
8
+ throw new Error(`Rule ${cstDef.name} not found`);
9
+ }
10
+ return def.fun({
11
+ SUBRULE: this.subrule,
12
+ })(this.getSafeContext(), ...args);
13
+ };
14
+ for (const rule of Object.values(rules)) {
15
+ this[rule.name] = ((context, ...args) => {
16
+ this.setContext(context);
17
+ return this.subrule(rule, ...args);
18
+ });
19
+ }
20
+ }
21
+ setContext(context) {
22
+ this.__context = context;
23
+ }
24
+ getSafeContext() {
25
+ return this.__context;
26
+ }
27
+ }
28
+ //# sourceMappingURL=dynamicIndirected.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamicIndirected.js","sourceRoot":"","sources":["dynamicIndirected.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,eAAe;IAG1B,YAA6B,KAAe;QAAf,UAAK,GAAL,KAAK,CAAU;QAFlC,cAAS,GAAwB,SAAS,CAAC;QAmBlC,YAAO,GAA2B,CAAC,MAAM,EAAE,GAAG,IAAI,EAAE,EAAE;YACvE,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAmB,MAAM,CAAC,IAAI,CAAC,CAAC;YACtD,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,MAAM,IAAI,KAAK,CAAC,QAAQ,MAAM,CAAC,IAAI,YAAY,CAAC,CAAC;YACnD,CAAC;YAED,OAAa,GAAG,CAAC,GAAG,CAAC;gBACnB,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;QACrC,CAAC,CAAC;QAzBA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAoC,KAAK,CAAC,EAAE,CAAC;YAC3E,IAAI,CAAsB,IAAI,CAAC,IAAI,CAAC,GAAS,CAAC,CAAC,OAAgB,EAAE,GAAG,IAAS,EAAO,EAAE;gBACpF,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;gBACzB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;YACrC,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAEM,UAAU,CAAC,OAAgB;QAChC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAES,cAAc;QACtB,OAAiB,IAAI,CAAC,SAAS,CAAC;IAClC,CAAC;CAYF","sourcesContent":["import type { IndirDef, IndirDefArg, IndirectionMap } from './helpers';\n\nexport class DynamicIndirect<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {\n protected __context: Context | undefined = undefined;\n\n public constructor(protected rules: RuleDefs) {\n for (const rule of Object.values(<Record<string, IndirDef<Context>>>rules)) {\n this[<keyof (typeof this)>rule.name] = <any> ((context: Context, ...args: any): any => {\n this.setContext(context);\n return this.subrule(rule, ...args);\n });\n }\n }\n\n public setContext(context: Context): void {\n this.__context = context;\n }\n\n protected getSafeContext(): Context {\n return <Context> this.__context;\n }\n\n protected readonly subrule: IndirDefArg['SUBRULE'] = (cstDef, ...args) => {\n const def = this.rules[<Names> <unknown> cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n\n return <any> def.fun({\n SUBRULE: this.subrule,\n })(this.getSafeContext(), ...args);\n };\n}\n"]}
@@ -0,0 +1,24 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
2
+ export type IndirDef<Context = any, NameType extends string = string, ReturnType = unknown, ParamType extends any[] = any[]> = {
3
+ name: NameType;
4
+ fun: (def: IndirDefArg) => (c: Context, ...params: ParamType) => ReturnType;
5
+ };
6
+ export type IndirDefArg = {
7
+ SUBRULE: <T, U extends any[]>(cstDef: IndirDef<any, any, T, U>, ...arg: U) => T;
8
+ };
9
+ export type IndirectionMap<RuleNames extends string> = {
10
+ [Key in RuleNames]: IndirDef<any, Key>;
11
+ };
12
+ /**
13
+ * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
14
+ */
15
+ export declare function listToIndirectionMap<T extends readonly IndirDef[]>(rules: T): ParseIndirsToObject<T>;
16
+ /**
17
+ * Convert a list of IndirDefs to a Record with the name of the IndirDef as the key, matching the IndirectionMap type.
18
+ */
19
+ export type ParseIndirsToObject<T extends readonly IndirDef[], Names extends string = ParseNamesFromList<T>, Agg extends Record<string, IndirDef> = Record<never, never>> = T extends readonly [infer First, ...infer Rest] ? (First extends IndirDef ? (Rest extends readonly IndirDef[] ? (ParseIndirsToObject<Rest, Names, {
20
+ [K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K];
21
+ }>) : never) : never) : IndirectionMap<Names> & Agg;
22
+ export type IndirectObjFromIndirDefs<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> = {
23
+ [K in Names]: RuleDefs[K] extends IndirDef<Context, K, infer RET, infer ARGS> ? (context: Context, ...args: ARGS) => RET : never;
24
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
3
+ */
4
+ export function listToIndirectionMap(rules) {
5
+ const newRules = {};
6
+ for (const rule of rules) {
7
+ newRules[rule.name] = rule;
8
+ }
9
+ return newRules;
10
+ }
11
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["helpers.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAgC,KAAQ;IAC1E,MAAM,QAAQ,GAA6B,EAAE,CAAC;IAC9C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAA+B,QAAQ,CAAC;AAC1C,CAAC","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes';\n\nexport type IndirDef<\n Context = any,\n NameType extends string = string,\n ReturnType = unknown,\n ParamType extends any[] = any[],\n> = {\n name: NameType;\n fun: (def: IndirDefArg) => (c: Context, ...params: ParamType) => ReturnType;\n};\n\nexport type IndirDefArg = {\n SUBRULE: <T, U extends any[]>(cstDef: IndirDef<any, any, T, U>, ...arg: U) => T;\n};\n\nexport type IndirectionMap<RuleNames extends string> = {[Key in RuleNames]: IndirDef<any, Key> };\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nexport function listToIndirectionMap<T extends readonly IndirDef[]>(rules: T): ParseIndirsToObject<T> {\n const newRules: Record<string, IndirDef> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseIndirsToObject<T>>newRules;\n}\n\n/**\n * Convert a list of IndirDefs to a Record with the name of the IndirDef as the key, matching the IndirectionMap type.\n */\nexport type ParseIndirsToObject<\n T extends readonly IndirDef[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, IndirDef> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends IndirDef ? (\n Rest extends readonly IndirDef[] ? (\n ParseIndirsToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : IndirectionMap<Names> & Agg;\n\nexport type IndirectObjFromIndirDefs<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> = {\n [K in Names]: RuleDefs[K] extends IndirDef<Context, K, infer RET, infer ARGS> ?\n (context: Context, ...args: ARGS) => RET : never\n};\n"]}
@@ -10,6 +10,10 @@ export declare class LexerBuilder<NAMES extends string = string> {
10
10
  add<Name extends string>(...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>): LexerBuilder<Name | NAMES>;
11
11
  addBefore<Name extends string>(before: NamedToken<NAMES>, ...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>): LexerBuilder<NAMES | Name>;
12
12
  private moveBeforeOrAfter;
13
+ /**
14
+ * @param before token to move rest before
15
+ * @param tokens tokens to move before the first token
16
+ */
13
17
  moveBefore<Name extends string>(before: NamedToken<NAMES>, ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>): LexerBuilder<NAMES>;
14
18
  moveAfter<Name extends string>(after: NamedToken<NAMES>, ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>): LexerBuilder<NAMES>;
15
19
  addAfter<Name extends string>(after: NamedToken<NAMES>, ...token: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>): LexerBuilder<NAMES | Name>;
@@ -51,6 +51,10 @@ export class LexerBuilder {
51
51
  }
52
52
  return this;
53
53
  }
54
+ /**
55
+ * @param before token to move rest before
56
+ * @param tokens tokens to move before the first token
57
+ */
54
58
  moveBefore(before, ...tokens) {
55
59
  return this.moveBeforeOrAfter('before', before, ...tokens);
56
60
  }
@@ -79,9 +83,9 @@ export class LexerBuilder {
79
83
  return new Lexer(this.tokens, {
80
84
  positionTracking: 'onlyStart',
81
85
  recoveryEnabled: false,
86
+ ensureOptimizations: true,
82
87
  // SafeMode: true,
83
88
  // SkipValidations: true,
84
- ensureOptimizations: true,
85
89
  ...lexerConfig,
86
90
  });
87
91
  }
@@ -1 +1 @@
1
- {"version":3,"file":"LexerBuilder.js","sourceRoot":"","sources":["LexerBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,MAAM,OAAO,YAAY;IAGhB,MAAM,CAAC,MAAM,CAAsD,OAAW;QACnF,OAAW,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,YAAoB,OAA6B;QAC/C,IAAI,CAAC,MAAM,GAAG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,CAAC;IAC3D,CAAC;IAEM,QAAQ,CACb,KAAwB,EACxB,GAAG,KAA2D;QAE9D,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACzC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,MAAM,CAAqB,GAAG,KAAyB;QAC5D,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACrC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;YACrC,CAAC;YACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,WAA0B;QACrC,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;YAC5B,gBAAgB,EAAE,WAAW;YAC7B,eAAe,EAAE,KAAK;YACtB,kBAAkB;YAClB,yBAAyB;YACzB,mBAAmB,EAAE,IAAI;YACzB,GAAG,WAAW;SACf,CAAC,CAAC;IACL,CAAC;IAED,IAAW,eAAe;QACxB,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF","sourcesContent":["import type { ILexerConfig } from '@chevrotain/types';\nimport type { TokenType } from 'chevrotain';\nimport { Lexer } from 'chevrotain';\nimport type { CheckOverlap, NamedToken } from '../utils';\n\nexport class LexerBuilder<NAMES extends string = string> {\n private readonly tokens: TokenType[];\n\n public static create<U extends LexerBuilder<T>, T extends string = never>(starter?: U): U {\n return <U> new LexerBuilder(starter);\n }\n\n private constructor(starter?: LexerBuilder<NAMES>) {\n this.tokens = starter?.tokens ? [ ...starter.tokens ] : [];\n }\n\n public merge<OtherNames extends string, OW extends string>(\n merge: LexerBuilder<OtherNames>,\n overwrite: NamedToken<OW>[] = [],\n ):\n LexerBuilder<NAMES | OtherNames> {\n const extraTokens = merge.tokens.filter((token) => {\n const overwriteToken = overwrite.find(t => t.name === token.name);\n if (overwriteToken) {\n return false;\n }\n const match = this.tokens.find(t => t.name === token.name);\n if (match) {\n if (match !== token) {\n throw new Error(`Token with name ${token.name} already exists. Implementation is different and no overwrite was provided.`);\n }\n return false;\n }\n return true;\n });\n this.tokens.push(...extraTokens);\n return this;\n }\n\n public add<Name extends string>(...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>):\n LexerBuilder<Name | NAMES> {\n this.tokens.push(...token);\n return this;\n }\n\n public addBefore<Name extends string>(\n before: NamedToken<NAMES>,\n ...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>\n ): LexerBuilder<NAMES | Name> {\n const index = this.tokens.indexOf(before);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index, 0, ...token);\n return this;\n }\n\n private moveBeforeOrAfter<Name extends string>(\n beforeOrAfter: 'before' | 'after',\n before: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n const beforeIndex = this.tokens.indexOf(before) + (beforeOrAfter === 'before' ? 0 : 1);\n if (beforeIndex === -1) {\n throw new Error('BeforeToken not found');\n }\n for (const token of tokens) {\n const tokenIndex = this.tokens.indexOf(token);\n if (tokenIndex === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(tokenIndex, 1);\n this.tokens.splice(beforeIndex, 0, token);\n }\n return this;\n }\n\n public moveBefore<Name extends string>(\n before: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n return this.moveBeforeOrAfter('before', before, ...tokens);\n }\n\n public moveAfter<Name extends string>(\n after: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n return this.moveBeforeOrAfter('after', after, ...tokens);\n }\n\n public addAfter<Name extends string>(\n after: NamedToken<NAMES>,\n ...token: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES | Name> {\n const index = this.tokens.indexOf(after);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index + 1, 0, ...token);\n return this;\n }\n\n public delete<Name extends NAMES>(...token: NamedToken<Name>[]): LexerBuilder<Exclude<NAMES, Name>> {\n for (const t of token) {\n const index = this.tokens.indexOf(t);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index, 1);\n }\n return this;\n }\n\n public build(lexerConfig?: ILexerConfig): Lexer {\n return new Lexer(this.tokens, {\n positionTracking: 'onlyStart',\n recoveryEnabled: false,\n // SafeMode: true,\n // SkipValidations: true,\n ensureOptimizations: true,\n ...lexerConfig,\n });\n }\n\n public get tokenVocabulary(): readonly TokenType[] {\n return this.tokens;\n }\n}\n"]}
1
+ {"version":3,"file":"LexerBuilder.js","sourceRoot":"","sources":["LexerBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,MAAM,OAAO,YAAY;IAGhB,MAAM,CAAC,MAAM,CAAsD,OAAW;QACnF,OAAW,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,YAAoB,OAA6B;QAC/C,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAAE,GAAG,OAAO,CAAC,MAAM,CAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC7D,CAAC;IAEM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AAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACzC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,MAAM,CAAqB,GAAG,KAAyB;QAC5D,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACrC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;YACrC,CAAC;YACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,WAA0B;QACrC,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;YAC5B,gBAAgB,EAAE,WAAW;YAC7B,eAAe,EAAE,KAAK;YACtB,mBAAmB,EAAE,IAAI;YACzB,kBAAkB;YAClB,yBAAyB;YACzB,GAAG,WAAW;SACf,CAAC,CAAC;IACL,CAAC;IAED,IAAW,eAAe;QACxB,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF","sourcesContent":["import type { ILexerConfig } from '@chevrotain/types';\nimport type { TokenType } from 'chevrotain';\nimport { Lexer } from 'chevrotain';\nimport type { CheckOverlap, NamedToken } from '../utils';\n\nexport class LexerBuilder<NAMES extends string = string> {\n private readonly tokens: TokenType[];\n\n public static create<U extends LexerBuilder<T>, T extends string = never>(starter?: U): U {\n return <U> new LexerBuilder(starter);\n }\n\n private constructor(starter?: LexerBuilder<NAMES>) {\n this.tokens = starter?.tokens ? [ ...starter.tokens ] : [];\n }\n\n public merge<OtherNames extends string, OW extends string>(\n merge: LexerBuilder<OtherNames>,\n overwrite: NamedToken<OW>[] = [],\n ):\n LexerBuilder<NAMES | OtherNames> {\n const extraTokens = merge.tokens.filter((token) => {\n const overwriteToken = overwrite.find(t => t.name === token.name);\n if (overwriteToken) {\n return false;\n }\n const match = this.tokens.find(t => t.name === token.name);\n if (match) {\n if (match !== token) {\n throw new Error(`Token with name ${token.name} already exists. Implementation is different and no overwrite was provided.`);\n }\n return false;\n }\n return true;\n });\n this.tokens.push(...extraTokens);\n return this;\n }\n\n public add<Name extends string>(...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>):\n LexerBuilder<Name | NAMES> {\n this.tokens.push(...token);\n return this;\n }\n\n public addBefore<Name extends string>(\n before: NamedToken<NAMES>,\n ...token: CheckOverlap<Name, NAMES, NamedToken<Name>[]>\n ): LexerBuilder<NAMES | Name> {\n const index = this.tokens.indexOf(before);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index, 0, ...token);\n return this;\n }\n\n private moveBeforeOrAfter<Name extends string>(\n beforeOrAfter: 'before' | 'after',\n before: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n const beforeIndex = this.tokens.indexOf(before) + (beforeOrAfter === 'before' ? 0 : 1);\n if (beforeIndex === -1) {\n throw new Error('BeforeToken not found');\n }\n for (const token of tokens) {\n const tokenIndex = this.tokens.indexOf(token);\n if (tokenIndex === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(tokenIndex, 1);\n this.tokens.splice(beforeIndex, 0, token);\n }\n return this;\n }\n\n /**\n * @param before token to move rest before\n * @param tokens tokens to move before the first token\n */\n public moveBefore<Name extends string>(\n before: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n return this.moveBeforeOrAfter('before', before, ...tokens);\n }\n\n public moveAfter<Name extends string>(\n after: NamedToken<NAMES>,\n ...tokens: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES> {\n return this.moveBeforeOrAfter('after', after, ...tokens);\n }\n\n public addAfter<Name extends string>(\n after: NamedToken<NAMES>,\n ...token: CheckOverlap<Name, NAMES, never, NamedToken<Name>[]>\n ): LexerBuilder<NAMES | Name> {\n const index = this.tokens.indexOf(after);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index + 1, 0, ...token);\n return this;\n }\n\n public delete<Name extends NAMES>(...token: NamedToken<Name>[]): LexerBuilder<Exclude<NAMES, Name>> {\n for (const t of token) {\n const index = this.tokens.indexOf(t);\n if (index === -1) {\n throw new Error('Token not found');\n }\n this.tokens.splice(index, 1);\n }\n return this;\n }\n\n public build(lexerConfig?: ILexerConfig): Lexer {\n return new Lexer(this.tokens, {\n positionTracking: 'onlyStart',\n recoveryEnabled: false,\n ensureOptimizations: true,\n // SafeMode: true,\n // SkipValidations: true,\n ...lexerConfig,\n });\n }\n\n public get tokenVocabulary(): readonly TokenType[] {\n return this.tokens;\n }\n}\n"]}
@@ -0,0 +1,53 @@
1
+ export interface Localized {
2
+ /**
3
+ * Location undefined means the node does have a string representation, but it was not clarified.
4
+ * This happens when an AST node is patched by a client of the lib.
5
+ */
6
+ loc: SourceLocation;
7
+ }
8
+ export interface Wrap<T> extends Localized {
9
+ val: T;
10
+ }
11
+ /**
12
+ * A AST node. Nodes are indexable by their types.
13
+ * When generating, the SUBRULES called should be located within the current location range.
14
+ */
15
+ export interface Node extends Localized {
16
+ type: string;
17
+ subType?: string;
18
+ }
19
+ export interface SourceLocationBase {
20
+ sourceLocationType: string;
21
+ }
22
+ export interface SourceLocationSource extends SourceLocationBase {
23
+ sourceLocationType: 'source';
24
+ start: number;
25
+ end: number;
26
+ }
27
+ /**
28
+ * NoStringManifestation means the node does not have a string representation.
29
+ * For example the literal '5' has an integer type (which is an AST node),
30
+ * but the type does not have an associated string representation.
31
+ * When set to true, the node will not be printed, start and end are meaningless in this case.
32
+ */
33
+ export interface SourceLocationNoMaterialize extends SourceLocationBase {
34
+ sourceLocationType: 'noMaterialize';
35
+ }
36
+ export interface SourceLocationStringReplace extends SourceLocationBase {
37
+ sourceLocationType: 'stringReplace';
38
+ newSource: string;
39
+ start: number;
40
+ end: number;
41
+ }
42
+ export interface SourceLocationNodeReplace extends SourceLocationBase {
43
+ sourceLocationType: 'nodeReplace';
44
+ start: number;
45
+ end: number;
46
+ }
47
+ /**
48
+ * Must have an ancestor of type {@link SourceLocationNodeReplace}
49
+ */
50
+ export interface SourceLocationNodeAutoGenerate extends SourceLocationBase {
51
+ sourceLocationType: 'autoGenerate';
52
+ }
53
+ export type SourceLocation = SourceLocationSource | SourceLocationNoMaterialize | SourceLocationStringReplace | SourceLocationNodeReplace | SourceLocationNodeAutoGenerate;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=nodeTypings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeTypings.js","sourceRoot":"","sources":["nodeTypings.ts"],"names":[],"mappings":"","sourcesContent":["export interface Localized {\n /**\n * Location undefined means the node does have a string representation, but it was not clarified.\n * This happens when an AST node is patched by a client of the lib.\n */\n loc: SourceLocation;\n}\n\nexport interface Wrap<T> extends Localized {\n val: T;\n}\n\n/**\n * A AST node. Nodes are indexable by their types.\n * When generating, the SUBRULES called should be located within the current location range.\n */\nexport interface Node extends Localized {\n type: string;\n subType?: string;\n}\n\nexport interface SourceLocationBase {\n sourceLocationType: string;\n}\n\nexport interface SourceLocationSource extends SourceLocationBase {\n sourceLocationType: 'source';\n start: number;\n end: number;\n}\n\n/**\n * NoStringManifestation means the node does not have a string representation.\n * For example the literal '5' has an integer type (which is an AST node),\n * but the type does not have an associated string representation.\n * When set to true, the node will not be printed, start and end are meaningless in this case.\n */\nexport interface SourceLocationNoMaterialize extends SourceLocationBase {\n sourceLocationType: 'noMaterialize';\n}\n\nexport interface SourceLocationStringReplace extends SourceLocationBase {\n sourceLocationType: 'stringReplace';\n newSource: string;\n start: number;\n end: number;\n}\n\nexport interface SourceLocationNodeReplace extends SourceLocationBase {\n sourceLocationType: 'nodeReplace';\n start: number;\n end: number;\n}\n/**\n * Must have an ancestor of type {@link SourceLocationNodeReplace}\n */\nexport interface SourceLocationNodeAutoGenerate extends SourceLocationBase {\n sourceLocationType: 'autoGenerate';\n}\n\nexport type SourceLocation =\n | SourceLocationSource\n | SourceLocationNoMaterialize\n | SourceLocationStringReplace\n | SourceLocationNodeReplace\n | SourceLocationNodeAutoGenerate;\n"]}
@@ -3,7 +3,9 @@ import type { ParserRule } from './ruleDefTypes';
3
3
  /**
4
4
  * Get union-type of names used in list of ruledefs.
5
5
  */
6
- export type ParseNamesFromList<T extends readonly ParserRule[]> = T[number]['name'];
6
+ export type ParseNamesFromList<T extends readonly {
7
+ name: string;
8
+ }[]> = T[number]['name'];
7
9
  /**
8
10
  * Convert a list of ruledefs to a record that maps each rule name to its definition.
9
11
  */
@@ -1 +1 @@
1
- {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParserMethod } from 'chevrotain';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type ParseNamesFromList<T extends readonly ParserRule[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type ParseRuleMap<RuleNames extends string> = {[Key in RuleNames]: ParserRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type ParseRulesToObject<\n T extends readonly ParserRule[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, ParserRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends ParserRule ? (\n Rest extends readonly ParserRule[] ? (\n ParseRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : ParseRuleMap<Names> & Agg;\n\nexport type ParserFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n (input: string, context: Context, args: ARGS) => RET : never\n};\n\nexport type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n ParserMethod<[Context, ARGS], RET> : never\n};\n"]}
1
+ {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParserMethod } from 'chevrotain';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type ParseNamesFromList<T extends readonly { name: string }[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type ParseRuleMap<RuleNames extends string> = {[Key in RuleNames]: ParserRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type ParseRulesToObject<\n T extends readonly ParserRule[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, ParserRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends ParserRule ? (\n Rest extends readonly ParserRule[] ? (\n ParseRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : ParseRuleMap<Names> & Agg;\n\nexport type ParserFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n (input: string, context: Context, args: ARGS) => RET : never\n};\n\nexport type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n ParserMethod<[Context, ARGS], RET> : never\n};\n"]}
@@ -1,7 +1,7 @@
1
1
  import type { ILexerConfig, IParserConfig, IRecognitionException } from '@chevrotain/types';
2
2
  import type { TokenType } from 'chevrotain';
3
3
  import type { CheckOverlap } from '../utils';
4
- import type { ParseNamesFromList, ParserFromRules, ParseRuleMap, ParseRulesToObject } from './builderTypes';
4
+ import type { ParserFromRules, ParseRuleMap, ParseRulesToObject, ParseNamesFromList } from './builderTypes';
5
5
  import type { ParserRule } from './ruleDefTypes';
6
6
  /**
7
7
  * The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).
@@ -14,9 +14,10 @@ export declare class ParserBuilder<Context, Names extends string, RuleDefs exten
14
14
  * Create a builder from some initial grammar rules or an existing builder.
15
15
  * If a builder is provided, a new copy will be created.
16
16
  */
17
- static createBuilder<Rules extends readonly ParserRule[] = readonly ParserRule[], Context = Rules[0] extends ParserRule<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>>(start: Rules | ParserBuilder<Context, Names, RuleDefs>): ParserBuilder<Context, Names, RuleDefs>;
17
+ static create<Rules extends readonly ParserRule[] = readonly ParserRule[], Context = Rules[0] extends ParserRule<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>>(start: Rules | ParserBuilder<Context, Names, RuleDefs>): ParserBuilder<Context, Names, RuleDefs>;
18
18
  private rules;
19
19
  private constructor();
20
+ widenContext<NewContext extends Context>(): ParserBuilder<NewContext, Names, RuleDefs>;
20
21
  typePatch<Patch extends {
21
22
  [Key in Names]?: any;
22
23
  }>(): ParserBuilder<Context, Names, {
@@ -22,7 +22,7 @@ export class ParserBuilder {
22
22
  * Create a builder from some initial grammar rules or an existing builder.
23
23
  * If a builder is provided, a new copy will be created.
24
24
  */
25
- static createBuilder(start) {
25
+ static create(start) {
26
26
  if (start instanceof ParserBuilder) {
27
27
  return new ParserBuilder({ ...start.rules });
28
28
  }
@@ -31,6 +31,9 @@ export class ParserBuilder {
31
31
  constructor(startRules) {
32
32
  this.rules = startRules;
33
33
  }
34
+ widenContext() {
35
+ return this;
36
+ }
34
37
  typePatch() {
35
38
  return this;
36
39
  }
@@ -123,13 +126,20 @@ ${errorLine}`);
123
126
  throw new Error(messageBuilder.join(''));
124
127
  }
125
128
  build({ tokenVocabulary, parserConfig = {}, lexerConfig = {}, queryPreProcessor = s => s, errorHandler, }) {
126
- const lexer = LexerBuilder.create().add(...tokenVocabulary).build(lexerConfig);
129
+ const lexer = LexerBuilder.create().add(...tokenVocabulary).build({
130
+ positionTracking: 'full',
131
+ recoveryEnabled: false,
132
+ // SafeMode: true,
133
+ // SkipValidations: true,
134
+ // ensureOptimizations: true,
135
+ ...lexerConfig,
136
+ });
127
137
  // Get the chevrotain parser
128
138
  const parser = this.consume({
129
139
  tokenVocabulary: tokenVocabulary,
130
140
  config: parserConfig,
131
141
  });
132
- // Start building a parser accepts a string instead of tokenized input
142
+ // Start building a parser that does not pass input using a state, but instead gets it as a function argument.
133
143
  const selfSufficientParser = {};
134
144
  // To do that, we need to create a wrapper for each parser rule.
135
145
  // eslint-disable-next-line ts/no-unnecessary-type-assertion
@@ -1 +1 @@
1
- {"version":3,"file":"parserBuilder.js","sourceRoot":"","sources":["parserBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAS7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD;;GAEG;AACH,SAAS,gBAAgB,CAAkC,KAAQ;IACjE,MAAM,QAAQ,GAA+B,EAAE,CAAC;IAChD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;I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CAAC;CACF","sourcesContent":["import type { ILexerConfig, IParserConfig, IRecognitionException } from '@chevrotain/types';\nimport type { Lexer, TokenType, TokenVocabulary, EmbeddedActionsParser } from 'chevrotain';\nimport { LexerBuilder } from '../lexer-builder/LexerBuilder';\nimport type { CheckOverlap } from '../utils';\nimport type {\n ParseMethodsFromRules,\n ParseNamesFromList,\n ParserFromRules,\n ParseRuleMap,\n ParseRulesToObject,\n} from './builderTypes';\nimport { DynamicParser } from './dynamicParser';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly ParserRule[]>(rules: T): ParseRulesToObject<T> {\n const newRules: Record<string, ParserRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseRulesToObject<T>>newRules;\n}\n\n/**\n * The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).\n * Using the builder you can create a grammar + AST creator.\n * At any point in time, a parser can be constructed from the added rules.\n * Constructing a parser will cause a validation which will validate the correctness of the grammar.\n */\n// This code is wild so other code can be simple.\nexport class ParserBuilder<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> {\n /**\n * Create a builder from some initial grammar rules or an existing builder.\n * If a builder is provided, a new copy will be created.\n */\n public static createBuilder<\n Rules extends readonly ParserRule[] = readonly ParserRule[],\n Context = Rules[0] extends ParserRule<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>,\n >(\n start: Rules | ParserBuilder<Context, Names, RuleDefs>,\n ): ParserBuilder<Context, Names, RuleDefs> {\n if (start instanceof ParserBuilder) {\n return new ParserBuilder({ ...start.rules });\n }\n return <ParserBuilder<Context, Names, RuleDefs>> <unknown> new ParserBuilder(listToRuleDefMap(start));\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public typePatch<Patch extends {[Key in Names]?: any }>():\n ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends ParserRule<Context, Key, any, infer Par> ? ParserRule<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends ParserRule<Context, Key, any, infer Par> ? ParserRule<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing grammar rule.\n */\n public patchRule<U extends Names, RET, ARGS>(patch: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS>(rule: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, ParserRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS>(\n rule: CheckOverlap<U, Names, ParserRule<Context, U, RET, ARGS>>,\n ): ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly ParserRule<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): ParserBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseRulesToObject<typeof rules> ? (\n ParseRulesToObject<typeof rules>[K] extends ParserRule<Context, K> ? ParseRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToRuleDefMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n /**\n * Merge this grammar builder with another.\n * It is best to merge the bigger grammar with the smaller one.\n * If the two builders both have a grammar rule with the same name,\n * no error will be thrown case they map to the same ruledef object.\n * If they map to a different object, an error will be thrown.\n * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,\n * this rule implementation will be used.\n */\n public merge<\n OtherNames extends string,\n OtherRules extends ParseRuleMap<OtherNames>,\n OW extends readonly ParserRule<Context>[],\n >(\n builder: ParserBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n ParserBuilder<\n Context,\n Names | OtherNames | ParseNamesFromList<OW>,\n {[K in Names | OtherNames | ParseNamesFromList<OW>]:\n K extends keyof ParseRulesToObject<OW> ? (\n ParseRulesToObject<OW>[K] extends ParserRule<Context, K> ? ParseRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends ParserRule<Context, K> ? OtherRules[K] : never) : never\n ) }\n > {\n // Assume the other grammar is bigger than yours. So start from that one and add this one\n const otherRules: Record<string, ParserRule<Context>> = { ...builder.rules };\n const myRules: Record<string, ParserRule<Context>> = this.rules;\n\n for (const rule of Object.values(myRules)) {\n if (otherRules[rule.name] === undefined) {\n otherRules[rule.name] = rule;\n } else {\n const existingRule = otherRules[rule.name];\n // If same rule, no issue, move on. Else\n if (existingRule !== rule) {\n const override = overridingRules.find(x => x.name === rule.name);\n // If override specified, take override, else, inform user that there is a conflict\n if (override) {\n otherRules[rule.name] = override;\n } else {\n throw new Error(`Rule with name \"${rule.name}\" already exists in the builder, specify an override to resolve conflict`);\n }\n }\n }\n }\n\n this.rules = <any> <unknown> otherRules;\n return <any> <unknown> this;\n }\n\n private defaultErrorHandler(input: string, errors: IRecognitionException[]): void {\n const firstError = errors[0];\n const messageBuilder: string[] = [ 'Parse error' ];\n const lineIdx = firstError.token.startLine;\n if (lineIdx !== undefined) {\n const errorLine = input.split('\\n')[lineIdx - 1];\n messageBuilder.push(` on line ${lineIdx}\n${errorLine}`);\n const columnIdx = firstError.token.startColumn;\n if (columnIdx !== undefined) {\n messageBuilder.push(`\\n${'-'.repeat(columnIdx - 1)}^`);\n }\n }\n messageBuilder.push(`\\n${firstError.message}`);\n throw new Error(messageBuilder.join(''));\n }\n\n public build({\n tokenVocabulary,\n parserConfig = {},\n lexerConfig = {},\n queryPreProcessor = s => s,\n errorHandler,\n }: {\n tokenVocabulary: readonly TokenType[];\n parserConfig?: IParserConfig;\n lexerConfig?: ILexerConfig;\n queryPreProcessor?: (input: string) => string;\n errorHandler?: (errors: IRecognitionException[]) => void;\n }): ParserFromRules<Context, Names, RuleDefs> {\n const lexer: Lexer = LexerBuilder.create().add(...tokenVocabulary).build(lexerConfig);\n\n // Get the chevrotain parser\n const parser = this.consume({\n tokenVocabulary: <TokenType[]> tokenVocabulary,\n config: parserConfig,\n });\n\n // Start building a parser accepts a string instead of tokenized input\n const selfSufficientParser: Partial<ParserFromRules<Context, Names, RuleDefs>> = {};\n\n // To do that, we need to create a wrapper for each parser rule.\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <ParserRule<Context, Names>[]> Object.values(this.rules)) {\n selfSufficientParser[rule.name] = <any> ((input: string, context: Context, arg: unknown) => {\n const processedInput = queryPreProcessor(input);\n const lexResult = lexer.tokenize(processedInput);\n\n // This also resets the parser\n parser.input = lexResult.tokens;\n parser.setContext(context);\n const result = parser[rule.name](context, arg);\n if (parser.errors.length > 0) {\n if (errorHandler) {\n errorHandler(parser.errors);\n } else {\n this.defaultErrorHandler(processedInput, parser.errors);\n }\n }\n return result;\n });\n }\n return <ParserFromRules<Context, Names, RuleDefs>> selfSufficientParser;\n }\n\n private consume({ tokenVocabulary, config = {}}: {\n tokenVocabulary: TokenVocabulary;\n config?: IParserConfig;\n }): EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void } {\n return <EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void }><unknown> new DynamicParser(this.rules, tokenVocabulary, config);\n }\n}\n"]}
1
+ {"version":3,"file":"parserBuilder.js","sourceRoot":"","sources":["parserBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAS7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBA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C;yBAAM,CAAC;wBACN,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;oBAC1D,CAAC;gBACH,CAAC;gBACD,OAAO,MAAM,CAAC;YAChB,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAmD,oBAAoB,CAAC;IAC1E,CAAC;IAEO,OAAO,CAAC,EAAE,eAAe,EAAE,MAAM,GAAG,EAAE,EAG7C;QAEC,OACuD,IAAI,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,eAAe,EAAE,MAAM,CAAC,CAAC;IAChH,CAAC;CACF","sourcesContent":["import type { ILexerConfig, IParserConfig, IRecognitionException } from '@chevrotain/types';\nimport type { TokenType, TokenVocabulary, EmbeddedActionsParser } from 'chevrotain';\nimport { LexerBuilder } from '../lexer-builder/LexerBuilder';\nimport type { CheckOverlap } from '../utils';\nimport type {\n ParseMethodsFromRules,\n ParserFromRules,\n ParseRuleMap,\n ParseRulesToObject,\n ParseNamesFromList,\n} from './builderTypes';\nimport { DynamicParser } from './dynamicParser';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly ParserRule[]>(rules: T): ParseRulesToObject<T> {\n const newRules: Record<string, ParserRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseRulesToObject<T>>newRules;\n}\n\n/**\n * The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).\n * Using the builder you can create a grammar + AST creator.\n * At any point in time, a parser can be constructed from the added rules.\n * Constructing a parser will cause a validation which will validate the correctness of the grammar.\n */\n// This code is wild so other code can be simple.\nexport class ParserBuilder<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> {\n /**\n * Create a builder from some initial grammar rules or an existing builder.\n * If a builder is provided, a new copy will be created.\n */\n public static create<\n Rules extends readonly ParserRule[] = readonly ParserRule[],\n Context = Rules[0] extends ParserRule<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>,\n >(\n start: Rules | ParserBuilder<Context, Names, RuleDefs>,\n ): ParserBuilder<Context, Names, RuleDefs> {\n if (start instanceof ParserBuilder) {\n return new ParserBuilder({ ...start.rules });\n }\n return <ParserBuilder<Context, Names, RuleDefs>> <unknown> new ParserBuilder(listToRuleDefMap(start));\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public widenContext<NewContext extends Context>(): ParserBuilder<NewContext, Names, RuleDefs> {\n return <ParserBuilder<NewContext, Names, RuleDefs>> <unknown> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: any }>():\n ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends ParserRule<Context, Key, any, infer Par> ? ParserRule<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends ParserRule<Context, Key, any, infer Par> ? ParserRule<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing grammar rule.\n */\n public patchRule<U extends Names, RET, ARGS>(patch: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS>(rule: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, ParserRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS>(\n rule: CheckOverlap<U, Names, ParserRule<Context, U, RET, ARGS>>,\n ): ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly ParserRule<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): ParserBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseRulesToObject<typeof rules> ? (\n ParseRulesToObject<typeof rules>[K] extends ParserRule<Context, K> ? ParseRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToRuleDefMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n /**\n * Merge this grammar builder with another.\n * It is best to merge the bigger grammar with the smaller one.\n * If the two builders both have a grammar rule with the same name,\n * no error will be thrown case they map to the same ruledef object.\n * If they map to a different object, an error will be thrown.\n * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,\n * this rule implementation will be used.\n */\n public merge<\n OtherNames extends string,\n OtherRules extends ParseRuleMap<OtherNames>,\n OW extends readonly ParserRule<Context>[],\n >(\n builder: ParserBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n ParserBuilder<\n Context,\n Names | OtherNames | ParseNamesFromList<OW>,\n {[K in Names | OtherNames | ParseNamesFromList<OW>]:\n K extends keyof ParseRulesToObject<OW> ? (\n ParseRulesToObject<OW>[K] extends ParserRule<Context, K> ? ParseRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends ParserRule<Context, K> ? OtherRules[K] : never) : never\n ) }\n > {\n // Assume the other grammar is bigger than yours. So start from that one and add this one\n const otherRules: Record<string, ParserRule<Context>> = { ...builder.rules };\n const myRules: Record<string, ParserRule<Context>> = this.rules;\n\n for (const rule of Object.values(myRules)) {\n if (otherRules[rule.name] === undefined) {\n otherRules[rule.name] = rule;\n } else {\n const existingRule = otherRules[rule.name];\n // If same rule, no issue, move on. Else\n if (existingRule !== rule) {\n const override = overridingRules.find(x => x.name === rule.name);\n // If override specified, take override, else, inform user that there is a conflict\n if (override) {\n otherRules[rule.name] = override;\n } else {\n throw new Error(`Rule with name \"${rule.name}\" already exists in the builder, specify an override to resolve conflict`);\n }\n }\n }\n }\n\n this.rules = <any> <unknown> otherRules;\n return <any> <unknown> this;\n }\n\n private defaultErrorHandler(input: string, errors: IRecognitionException[]): void {\n const firstError = errors[0];\n const messageBuilder: string[] = [ 'Parse error' ];\n const lineIdx = firstError.token.startLine;\n if (lineIdx !== undefined) {\n const errorLine = input.split('\\n')[lineIdx - 1];\n messageBuilder.push(` on line ${lineIdx}\n${errorLine}`);\n const columnIdx = firstError.token.startColumn;\n if (columnIdx !== undefined) {\n messageBuilder.push(`\\n${'-'.repeat(columnIdx - 1)}^`);\n }\n }\n messageBuilder.push(`\\n${firstError.message}`);\n throw new Error(messageBuilder.join(''));\n }\n\n public build({\n tokenVocabulary,\n parserConfig = {},\n lexerConfig = {},\n queryPreProcessor = s => s,\n errorHandler,\n }: {\n tokenVocabulary: readonly TokenType[];\n parserConfig?: IParserConfig;\n lexerConfig?: ILexerConfig;\n queryPreProcessor?: (input: string) => string;\n errorHandler?: (errors: IRecognitionException[]) => void;\n }): ParserFromRules<Context, Names, RuleDefs> {\n const lexer = LexerBuilder.create().add(...tokenVocabulary).build({\n positionTracking: 'full',\n recoveryEnabled: false,\n // SafeMode: true,\n // SkipValidations: true,\n // ensureOptimizations: true,\n ...lexerConfig,\n });\n // Get the chevrotain parser\n const parser = this.consume({\n tokenVocabulary: <TokenType[]> tokenVocabulary,\n config: parserConfig,\n });\n // Start building a parser that does not pass input using a state, but instead gets it as a function argument.\n const selfSufficientParser: Partial<ParserFromRules<Context, Names, RuleDefs>> = {};\n\n // To do that, we need to create a wrapper for each parser rule.\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <ParserRule<Context, Names>[]> Object.values(this.rules)) {\n selfSufficientParser[rule.name] = <any> ((input: string, context: Context, arg: unknown) => {\n const processedInput = queryPreProcessor(input);\n const lexResult = lexer.tokenize(processedInput);\n\n // This also resets the parser\n parser.input = lexResult.tokens;\n parser.setContext(context);\n const result = parser[rule.name](context, arg);\n if (parser.errors.length > 0) {\n if (errorHandler) {\n errorHandler(parser.errors);\n } else {\n this.defaultErrorHandler(processedInput, parser.errors);\n }\n }\n return result;\n });\n }\n return <ParserFromRules<Context, Names, RuleDefs>> selfSufficientParser;\n }\n\n private consume({ tokenVocabulary, config = {}}: {\n tokenVocabulary: TokenVocabulary;\n config?: IParserConfig;\n }): EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void } {\n return <EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void }><unknown> new DynamicParser(this.rules, tokenVocabulary, config);\n }\n}\n"]}
package/lib/utils.d.ts CHANGED
@@ -11,7 +11,7 @@ export declare function createToken<Name extends string>(config: ITokenConfig &
11
11
  name: Name;
12
12
  }): NamedToken<Name>;
13
13
  export type Patch<T extends object, Patch extends {
14
- [Key in keyof T]?: any;
14
+ [Key in keyof T]?: unknown;
15
15
  }> = {
16
16
  [Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key];
17
17
  };
package/lib/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,YAAY,CAAC;AAOnD,MAAM,UAAU,YAAY,CAAmB,GAAM;IACnD,OAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAID,MAAM,UAAU,WAAW,CAAsB,MAAqC;IACpF,OAAoC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@chevrotain/types';\nimport { createToken as chevcT } from 'chevrotain';\n\n/**\n * Check whether the first two types overlap, if no, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\nexport function unCapitalize<T extends string>(str: T): Uncapitalize<T> {\n return <Uncapitalize<T>> (str.charAt(0).toLowerCase() + str.slice(1));\n}\n\nexport type NamedToken<Name extends string> = TokenType & { name: Name };\n\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: any }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,YAAY,CAAC;AAOnD,MAAM,UAAU,YAAY,CAAmB,GAAM;IACnD,OAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAID,MAAM,UAAU,WAAW,CAAsB,MAAqC;IACpF,OAAoC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@chevrotain/types';\nimport { createToken as chevcT } from 'chevrotain';\n\n/**\n * Check whether the first two types overlap, if no, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\nexport function unCapitalize<T extends string>(str: T): Uncapitalize<T> {\n return <Uncapitalize<T>> (str.charAt(0).toLowerCase() + str.slice(1));\n}\n\nexport type NamedToken<Name extends string> = TokenType & { name: Name };\n\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: unknown }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@traqula/core",
3
3
  "type": "module",
4
- "version": "0.0.1-alpha.176+9400dc5",
4
+ "version": "0.0.1",
5
5
  "description": "Core components of Traqula",
6
6
  "lsd:module": true,
7
7
  "license": "MIT",
@@ -43,5 +43,5 @@
43
43
  "devDependencies": {
44
44
  "@chevrotain/types": "^11.0.3"
45
45
  },
46
- "gitHead": "9400dc5f8bda1d8c2b3d1f4cf56b308995744ba0"
46
+ "gitHead": "2bfa8222d972fda2ca6f0adc80a5362e0d583871"
47
47
  }