@traqula/core 0.0.1-alpha.176 → 0.0.1-alpha.9

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 (35) 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/RangeArithmetic.d.ts +14 -0
  5. package/lib/RangeArithmetic.js +71 -0
  6. package/lib/RangeArithmetic.js.map +1 -0
  7. package/lib/Transformer.d.ts +26 -0
  8. package/lib/Transformer.js +57 -0
  9. package/lib/Transformer.js.map +1 -0
  10. package/lib/generator-builder/builderTypes.d.ts +4 -1
  11. package/lib/generator-builder/builderTypes.js.map +1 -1
  12. package/lib/generator-builder/dynamicGenerator.d.ts +18 -3
  13. package/lib/generator-builder/dynamicGenerator.js +104 -17
  14. package/lib/generator-builder/dynamicGenerator.js.map +1 -1
  15. package/lib/generator-builder/generatorBuilder.d.ts +4 -3
  16. package/lib/generator-builder/generatorBuilder.js +4 -1
  17. package/lib/generator-builder/generatorBuilder.js.map +1 -1
  18. package/lib/generator-builder/generatorTypes.d.ts +13 -4
  19. package/lib/generator-builder/generatorTypes.js.map +1 -1
  20. package/lib/index.cjs +363 -13
  21. package/lib/index.d.ts +4 -0
  22. package/lib/index.js +4 -0
  23. package/lib/index.js.map +1 -1
  24. package/lib/lexer-builder/LexerBuilder.d.ts +4 -0
  25. package/lib/lexer-builder/LexerBuilder.js +5 -1
  26. package/lib/lexer-builder/LexerBuilder.js.map +1 -1
  27. package/lib/nodeTypings.d.ts +53 -0
  28. package/lib/nodeTypings.js +2 -0
  29. package/lib/nodeTypings.js.map +1 -0
  30. package/lib/parser-builder/parserBuilder.d.ts +3 -2
  31. package/lib/parser-builder/parserBuilder.js +13 -3
  32. package/lib/parser-builder/parserBuilder.js.map +1 -1
  33. package/lib/utils.d.ts +1 -1
  34. package/lib/utils.js.map +1 -1
  35. package/package.json +2 -2
@@ -10,7 +10,7 @@ function listToRuleDefMap(rules) {
10
10
  return newRules;
11
11
  }
12
12
  export class GeneratorBuilder {
13
- static createBuilder(start) {
13
+ static create(start) {
14
14
  if (start instanceof GeneratorBuilder) {
15
15
  return new GeneratorBuilder({ ...start.rules });
16
16
  }
@@ -19,6 +19,9 @@ export class GeneratorBuilder {
19
19
  constructor(startRules) {
20
20
  this.rules = startRules;
21
21
  }
22
+ widenContext() {
23
+ return this;
24
+ }
22
25
  typePatch() {
23
26
  return this;
24
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"generatorBuilder.js","sourceRoot":"","sources":["generatorBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD;;GAEG;AACH,SAAS,gBAAgB,CAAqC,KAAQ;IACpE,MAAM,QAAQ,GAAkC,EAAE,CAAC;IACnD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAA4B,QAAQ,CAAC;AACvC,CAAC;AAED,MAAM,OAAO,gBAAgB;IAcpB,MAAM,CAAC,aAAa,CAMzB,KAAyD;QAEzD,IAAI,KAAK,YAAY,gBAAgB,EAAE,CAAC;YACtC,OAAO,IAAI,gBAAgB,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;QAClD,CAAC;QACD,OAA8D,IAAI,gBAAgB,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9G,CAAC;IAID,YAAoB,UAAoB;QACtC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;IAC1B,CAAC;IAEM,SAAS;QAKd,OAG8F,IAAI,CAAC;IACrG,CAAC;IAED;;OAEG;IACI,SAAS,CAA6B,KAA2C;QAKtF,MAAM,IAAI,GAGE,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IA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AAC,KAAK,SAAS,EAAE,CAAC;gBACxC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC3C,wCAAwC;gBACxC,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;oBAC1B,MAAM,QAAQ,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,CAAC;oBACjE,mFAAmF;oBACnF,IAAI,QAAQ,EAAE,CAAC;wBACb,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;oBACnC,CAAC;yBAAM,CAAC;wBACN,MAAM,IAAI,KAAK,CAAC,mBAAmB,IAAI,CAAC,IAAI,mFAAmF,CAAC,CAAC;oBACnI,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,CAAC,KAAK,GAAmB,UAAU,CAAC;QACxC,OAAuB,IAAI,CAAC;IAC9B,CAAC;IAEM,KAAK;QACV,OAAsD,IAAI,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACzF,CAAC;CACF","sourcesContent":["import type { CheckOverlap } from '../utils';\nimport type { GeneratorFromRules, GenNamesFromList, GenRuleMap, GenRulesToObject } from './builderTypes';\nimport { DynamicGenerator } from './dynamicGenerator';\nimport type { GeneratorRule } from './generatorTypes';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly GeneratorRule[]>(rules: T): GenRulesToObject<T> {\n const newRules: Record<string, GeneratorRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <GenRulesToObject<T>>newRules;\n}\n\nexport class GeneratorBuilder<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n /**\n * Create a GeneratorBuilder from some initial grammar rules or an existing GeneratorBuilder.\n * If a GeneratorBuilder is provided, a new copy will be created.\n */\n public static createBuilder<Context, Names extends string, RuleDefs extends GenRuleMap<Names>>(\n args: GeneratorBuilder<Context, Names, RuleDefs>\n ): GeneratorBuilder<Context, Names, RuleDefs>;\n public static createBuilder<\n Rules extends readonly GeneratorRule[] = readonly GeneratorRule[],\n Context = Rules[0] extends GeneratorRule<infer context> ? context : never,\n Names extends string = GenNamesFromList<Rules>,\n RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>,\n >(rules: Rules): GeneratorBuilder<Context, Names, RuleDefs>;\n public static createBuilder<\n Rules extends readonly GeneratorRule[] = readonly GeneratorRule[],\n Context = Rules[0] extends GeneratorRule<infer context> ? context : never,\n Names extends string = GenNamesFromList<Rules>,\n RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>,\n >(\n start: Rules | GeneratorBuilder<Context, Names, RuleDefs>,\n ): GeneratorBuilder<Context, Names, RuleDefs> {\n if (start instanceof GeneratorBuilder) {\n return new GeneratorBuilder({ ...start.rules });\n }\n return <GeneratorBuilder<Context, Names, RuleDefs>> <unknown> new GeneratorBuilder(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 GeneratorBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends GeneratorRule<Context, Key, any, infer Args> ?\n GeneratorRule<Context, Key, Patch[Key], Args> : never\n ) : (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <GeneratorBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends GeneratorRule<Context, Key, any, infer Args> ?\n GeneratorRule<Context, Key, Patch[Key], Args> : never\n ) : (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing generator rule.\n */\n public patchRule<U extends Names, RET, ARGS>(patch: GeneratorRule<Context, U, RET, ARGS>):\n GeneratorBuilder<Context, Names, {[Key in Names]: Key extends U ?\n GeneratorRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <GeneratorBuilder<Context, Names, {[Key in Names]: Key extends U ?\n GeneratorRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends GeneratorRule<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: GeneratorRule<Context, U, RET, ARGS>):\n GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never)\n }> {\n const self = <GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never) }>>\n <unknown> this;\n const rules = <Record<string, GeneratorRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);\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, GeneratorRule<Context, U, RET, ARGS>>,\n ): GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never)\n }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly GeneratorRule<Context>[]>(\n ...rules: CheckOverlap<GenNamesFromList<U>, Names, U>\n ): GeneratorBuilder<\n Context,\n Names | GenNamesFromList<U>,\n {[K in Names | GenNamesFromList<U>]:\n K extends keyof GenRulesToObject<typeof rules> ? (\n GenRulesToObject<typeof rules>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends GeneratorRule<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 GeneratorBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <GeneratorBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n /**\n * Merge this grammar GeneratorBuilder 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 GenRuleMap<OtherNames>,\n OW extends readonly GeneratorRule<Context>[],\n >(\n GeneratorBuilder: GeneratorBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n GeneratorBuilder<\n Context,\n Names | OtherNames | GenNamesFromList<OW>,\n {[K in Names | OtherNames | GenNamesFromList<OW>]:\n K extends keyof GenRulesToObject<OW> ? (\n GenRulesToObject<OW>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends GeneratorRule<Context, K> ? OtherRules[K] : never)\n : 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, GeneratorRule<Context>> = { ...GeneratorBuilder.rules };\n const myRules: Record<string, GeneratorRule<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 GeneratorBuilder, 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 public build(): GeneratorFromRules<Context, Names, RuleDefs> {\n return <GeneratorFromRules<Context, Names, RuleDefs>> new DynamicGenerator(this.rules);\n }\n}\n"]}
1
+ {"version":3,"file":"generatorBuilder.js","sourceRoot":"","sources":["generatorBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAGtD;;GAEG;AACH,SAAS,gBAAgB,CAAqC,KAAQ;IACpE,MAAM,QAAQ,GAAkC,EAAE,CAAC;IACnD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAA4B,QAAQ,CAAC;AACvC,CAAC;AAED,MAAM,OAAO,gBAAgB;IAcpB,MAAM,CAAC,MAAM,CAMlB,KAAyD;QAEzD,IAAI,KAAK,YAAY,gBAAgB,EAAE,CAAC;YACtC,OAAO,IAAI,gBAAgB,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CA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type { GeneratorFromRules, GenRuleMap, GenRulesToObject, GenNamesFromList } from './builderTypes';\nimport { DynamicGenerator } from './dynamicGenerator';\nimport type { GeneratorRule } from './generatorTypes';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly GeneratorRule[]>(rules: T): GenRulesToObject<T> {\n const newRules: Record<string, GeneratorRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <GenRulesToObject<T>>newRules;\n}\n\nexport class GeneratorBuilder<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n /**\n * Create a GeneratorBuilder from some initial grammar rules or an existing GeneratorBuilder.\n * If a GeneratorBuilder is provided, a new copy will be created.\n */\n public static create<Context, Names extends string, RuleDefs extends GenRuleMap<Names>>(\n args: GeneratorBuilder<Context, Names, RuleDefs>\n ): GeneratorBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly GeneratorRule[] = readonly GeneratorRule[],\n Context = Rules[0] extends GeneratorRule<infer context> ? context : never,\n Names extends string = GenNamesFromList<Rules>,\n RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>,\n >(rules: Rules): GeneratorBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly GeneratorRule[] = readonly GeneratorRule[],\n Context = Rules[0] extends GeneratorRule<infer context> ? context : never,\n Names extends string = GenNamesFromList<Rules>,\n RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>,\n >(\n start: Rules | GeneratorBuilder<Context, Names, RuleDefs>,\n ): GeneratorBuilder<Context, Names, RuleDefs> {\n if (start instanceof GeneratorBuilder) {\n return new GeneratorBuilder({ ...start.rules });\n }\n return <GeneratorBuilder<Context, Names, RuleDefs>> <unknown> new GeneratorBuilder(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>(): GeneratorBuilder<NewContext, Names, RuleDefs> {\n return <GeneratorBuilder<NewContext, Names, RuleDefs>> <unknown> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: any }>():\n GeneratorBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends GeneratorRule<Context, Key, any, infer Args> ?\n GeneratorRule<Context, Key, Patch[Key], Args> : never\n ) : (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <GeneratorBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends GeneratorRule<Context, Key, any, infer Args> ?\n GeneratorRule<Context, Key, Patch[Key], Args> : never\n ) : (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing generator rule.\n */\n public patchRule<U extends Names, RET, ARGS>(patch: GeneratorRule<Context, U, RET, ARGS>):\n GeneratorBuilder<Context, Names, {[Key in Names]: Key extends U ?\n GeneratorRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends GeneratorRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <GeneratorBuilder<Context, Names, {[Key in Names]: Key extends U ?\n GeneratorRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends GeneratorRule<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: GeneratorRule<Context, U, RET, ARGS>):\n GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never)\n }> {\n const self = <GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never) }>>\n <unknown> this;\n const rules = <Record<string, GeneratorRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);\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, GeneratorRule<Context, U, RET, ARGS>>,\n ): GeneratorBuilder<Context, Names | U, {[K in Names | U]: K extends Names ?\n (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never)\n }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly GeneratorRule<Context>[]>(\n ...rules: CheckOverlap<GenNamesFromList<U>, Names, U>\n ): GeneratorBuilder<\n Context,\n Names | GenNamesFromList<U>,\n {[K in Names | GenNamesFromList<U>]:\n K extends keyof GenRulesToObject<typeof rules> ? (\n GenRulesToObject<typeof rules>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends GeneratorRule<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 GeneratorBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <GeneratorBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n /**\n * Merge this grammar GeneratorBuilder 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 GenRuleMap<OtherNames>,\n OW extends readonly GeneratorRule<Context>[],\n >(\n GeneratorBuilder: GeneratorBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n GeneratorBuilder<\n Context,\n Names | OtherNames | GenNamesFromList<OW>,\n {[K in Names | OtherNames | GenNamesFromList<OW>]:\n K extends keyof GenRulesToObject<OW> ? (\n GenRulesToObject<OW>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends GeneratorRule<Context, K> ? OtherRules[K] : never)\n : 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, GeneratorRule<Context>> = { ...GeneratorBuilder.rules };\n const myRules: Record<string, GeneratorRule<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 GeneratorBuilder, 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 public build(): GeneratorFromRules<Context, Names, RuleDefs> {\n return <GeneratorFromRules<Context, Names, RuleDefs>> new DynamicGenerator(this.rules);\n }\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import type { Localized } from '../nodeTypings';
1
2
  /**
2
3
  * Type used to declare generator rules.
3
4
  */
@@ -11,8 +12,10 @@ Context = any,
11
12
  */
12
13
  NameType extends string = string,
13
14
  /**
14
- * Type that will be returned after a correct parse of this rule.
15
- * This type will be the return type of calling SUBRULE with this grammar rule.
15
+ * Type that of the AST that we will generate the string for.
16
+ * This type will be the provided when calling SUBRULE with this generator rule.
17
+ * Generation happens on a per AST node basis.
18
+ * The core will implement the generation as such. If this ever changes, we will cross that bridge when we get there.
16
19
  */
17
20
  ReturnType = any,
18
21
  /**
@@ -20,8 +23,14 @@ ReturnType = any,
20
23
  */
21
24
  ParamType = any> = {
22
25
  name: NameType;
23
- gImpl: (def: RuleDefArg) => (ast: ReturnType, context: Context, params: ParamType) => string;
26
+ gImpl: (def: RuleDefArg) => (ast: ReturnType, context: Context, params: ParamType) => void;
24
27
  };
25
28
  export interface RuleDefArg {
26
- SUBRULE: <T, U>(cstDef: GeneratorRule<any, any, T, U>, input: T, arg: U) => string;
29
+ SUBRULE: <T, U>(cstDef: GeneratorRule<any, any, T, U>, input: T, arg: U) => void;
30
+ PRINT: (...args: string[]) => void;
31
+ PRINT_SPACE_LEFT: (...args: string[]) => void;
32
+ PRINT_WORD: (...args: string[]) => void;
33
+ PRINT_WORDS: (...args: string[]) => void;
34
+ HANDLE_LOC: <T>(loc: Localized, nodeHandle: () => T) => T | undefined;
35
+ CATCHUP: (until: number) => void;
27
36
  }
@@ -1 +1 @@
1
- {"version":3,"file":"generatorTypes.js","sourceRoot":"","sources":["generatorTypes.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Type used to declare generator rules.\n */\nexport type GeneratorRule<\n /**\n * Context object available in rule implementation.\n */\n Context = any,\n /**\n * Name of grammar rule, should be a strict subtype of string like 'myGrammarRule'.\n */\n NameType extends string = string,\n /**\n * Type that will be returned after a correct parse of this rule.\n * This type will be the return type of calling SUBRULE with this grammar rule.\n */\n ReturnType = any,\n /**\n * Function arguments that can be given to convey the state of the current parse operation.\n */\n ParamType = any,\n> = {\n name: NameType;\n gImpl: (def: RuleDefArg) =>\n (ast: ReturnType, context: Context, params: ParamType) => string;\n};\n\nexport interface RuleDefArg {\n SUBRULE: <T, U>(cstDef: GeneratorRule<any, any, T, U>, input: T, arg: U) => string;\n}\n"]}
1
+ {"version":3,"file":"generatorTypes.js","sourceRoot":"","sources":["generatorTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { Localized } from '../nodeTypings';\n\n/**\n * Type used to declare generator rules.\n */\nexport type GeneratorRule<\n /**\n * Context object available in rule implementation.\n */\n Context = any,\n /**\n * Name of grammar rule, should be a strict subtype of string like 'myGrammarRule'.\n */\n NameType extends string = string,\n /**\n * Type that of the AST that we will generate the string for.\n * This type will be the provided when calling SUBRULE with this generator rule.\n * Generation happens on a per AST node basis.\n * The core will implement the generation as such. If this ever changes, we will cross that bridge when we get there.\n */\n ReturnType = any,\n /**\n * Function arguments that can be given to convey the state of the current parse operation.\n */\n ParamType = any,\n> = {\n name: NameType;\n gImpl: (def: RuleDefArg) =>\n (ast: ReturnType, context: Context, params: ParamType) => void;\n};\n\nexport interface RuleDefArg {\n SUBRULE: <T, U>(cstDef: GeneratorRule<any, any, T, U>, input: T, arg: U) => void;\n PRINT: (...args: string[]) => void;\n PRINT_SPACE_LEFT: (...args: string[]) => void;\n PRINT_WORD: (...args: string[]) => void;\n PRINT_WORDS: (...args: string[]) => void;\n HANDLE_LOC: <T>(loc: Localized, nodeHandle: () => T) => T | undefined;\n CATCHUP: (until: number) => void;\n}\n"]}
package/lib/index.cjs CHANGED
@@ -20,31 +20,242 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // lib/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ CoreFactory: () => CoreFactory,
23
24
  GeneratorBuilder: () => GeneratorBuilder,
24
25
  LexerBuilder: () => LexerBuilder,
25
26
  ParserBuilder: () => ParserBuilder,
27
+ RangeArithmetic: () => RangeArithmetic,
28
+ Transformer: () => Transformer,
26
29
  createToken: () => createToken2,
27
30
  unCapitalize: () => unCapitalize
28
31
  });
29
32
  module.exports = __toCommonJS(index_exports);
30
33
 
34
+ // lib/CoreFactory.ts
35
+ var CoreFactory = class {
36
+ wrap(val, loc) {
37
+ return { val, loc };
38
+ }
39
+ isLocalized(obj) {
40
+ return typeof obj === "object" && obj !== null && "loc" in obj && typeof obj.loc === "object" && obj.loc !== null && "sourceLocationType" in obj.loc;
41
+ }
42
+ sourceLocation(...elements) {
43
+ const pureElements = elements.filter((x) => x !== void 0);
44
+ if (pureElements.length === 0) {
45
+ return this.sourceLocationNoMaterialize();
46
+ }
47
+ const filtered = pureElements.filter((element) => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) || this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
48
+ if (filtered.length === 0) {
49
+ return this.gen();
50
+ }
51
+ const first2 = filtered[0];
52
+ const last2 = filtered.at(-1);
53
+ return {
54
+ sourceLocationType: "source",
55
+ start: this.isLocalized(first2) ? first2.loc.start : first2.startOffset,
56
+ end: this.isLocalized(last2) ? last2.loc.end : last2.endOffset + 1
57
+ };
58
+ }
59
+ sourceLocationNoMaterialize() {
60
+ return { sourceLocationType: "noMaterialize" };
61
+ }
62
+ /**
63
+ * Returns a copy of the argument that is not materialized
64
+ */
65
+ dematerialized(arg) {
66
+ return { ...arg, loc: this.sourceLocationNoMaterialize() };
67
+ }
68
+ safeObjectTransform(value, mapper) {
69
+ if (value && typeof value === "object") {
70
+ if (Array.isArray(value)) {
71
+ return value.map((x) => this.safeObjectTransform(x, mapper));
72
+ }
73
+ return mapper(value);
74
+ }
75
+ return value;
76
+ }
77
+ forcedAutoGenTree(obj) {
78
+ const copy = { ...obj };
79
+ for (const [key, value] of Object.entries(copy)) {
80
+ copy[key] = this.safeObjectTransform(value, (obj2) => this.forcedAutoGenTree(obj2));
81
+ }
82
+ if (this.isLocalized(copy)) {
83
+ copy.loc = this.gen();
84
+ }
85
+ return copy;
86
+ }
87
+ forceMaterialized(arg) {
88
+ if (this.isSourceLocationNoMaterialize(arg.loc)) {
89
+ return this.forcedAutoGenTree(arg);
90
+ }
91
+ return { ...arg };
92
+ }
93
+ isSourceLocation(loc) {
94
+ return "sourceLocationType" in loc;
95
+ }
96
+ sourceLocationSource(start, end) {
97
+ return {
98
+ sourceLocationType: "source",
99
+ start,
100
+ end
101
+ };
102
+ }
103
+ gen() {
104
+ return { sourceLocationType: "autoGenerate" };
105
+ }
106
+ isSourceLocationSource(loc) {
107
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "source";
108
+ }
109
+ isSourceLocationStringReplace(loc) {
110
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "stringReplace";
111
+ }
112
+ sourceLocationNodeReplaceUnsafe(loc) {
113
+ if (this.isSourceLocationSource(loc)) {
114
+ return this.sourceLocationNodeReplace(loc);
115
+ }
116
+ throw new Error("not possible");
117
+ }
118
+ sourceLocationNodeReplace(startOrLoc, end) {
119
+ let starting;
120
+ let ending;
121
+ if (typeof startOrLoc === "number") {
122
+ starting = startOrLoc;
123
+ ending = end;
124
+ } else {
125
+ starting = startOrLoc.start;
126
+ ending = startOrLoc.end;
127
+ }
128
+ return {
129
+ sourceLocationType: "nodeReplace",
130
+ start: starting,
131
+ end: ending
132
+ };
133
+ }
134
+ isSourceLocationNodeReplace(loc) {
135
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "nodeReplace";
136
+ }
137
+ isSourceLocationNodeAutoGenerate(loc) {
138
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "autoGenerate";
139
+ }
140
+ nodeShouldPrint(node) {
141
+ return this.isSourceLocationNodeReplace(node.loc) || this.isSourceLocationNodeAutoGenerate(node.loc);
142
+ }
143
+ printFilter(node, callback) {
144
+ if (this.nodeShouldPrint(node)) {
145
+ callback();
146
+ }
147
+ }
148
+ isSourceLocationNoMaterialize(loc) {
149
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "noMaterialize";
150
+ }
151
+ isOfType(obj, type) {
152
+ const casted = obj;
153
+ return casted.type === type;
154
+ }
155
+ isOfSubType(obj, type, subType) {
156
+ const temp = obj;
157
+ return temp.type === type && temp.subType === subType;
158
+ }
159
+ };
160
+
31
161
  // lib/generator-builder/dynamicGenerator.ts
32
162
  var DynamicGenerator = class {
33
163
  constructor(rules) {
164
+ this.rules = rules;
165
+ this.factory = new CoreFactory();
34
166
  this.__context = void 0;
35
- const selfRef = {
36
- SUBRULE: (cstDef, input, arg) => {
37
- const def = rules[cstDef.name];
38
- if (!def) {
39
- throw new Error(`Rule ${cstDef.name} not found`);
167
+ this.origSource = "";
168
+ this.generatedUntil = 0;
169
+ this.stringBuilder = [];
170
+ this.subrule = (cstDef, ast, arg) => {
171
+ const def = this.rules[cstDef.name];
172
+ if (!def) {
173
+ throw new Error(`Rule ${cstDef.name} not found`);
174
+ }
175
+ const generate = () => def.gImpl({
176
+ SUBRULE: this.subrule,
177
+ PRINT: this.print,
178
+ PRINT_SPACE_LEFT: this.printSpaceLeft,
179
+ PRINT_WORD: this.printWord,
180
+ PRINT_WORDS: this.printWords,
181
+ CATCHUP: this.catchup,
182
+ HANDLE_LOC: this.handleLoc
183
+ })(ast, this.getSafeContext(), arg);
184
+ if (this.factory.isLocalized(ast)) {
185
+ this.handleLoc(ast, generate);
186
+ } else {
187
+ generate();
188
+ }
189
+ };
190
+ this.handleLoc = (localized, handle) => {
191
+ if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
192
+ return;
193
+ }
194
+ if (this.factory.isSourceLocationStringReplace(localized.loc)) {
195
+ this.catchup(localized.loc.start);
196
+ this.print(localized.loc.newSource);
197
+ this.generatedUntil = localized.loc.end;
198
+ return;
199
+ }
200
+ if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
201
+ this.catchup(localized.loc.start);
202
+ this.generatedUntil = localized.loc.end;
203
+ }
204
+ if (this.factory.isSourceLocationSource(localized.loc)) {
205
+ this.catchup(localized.loc.start);
206
+ }
207
+ const ret = handle();
208
+ if (this.factory.isSourceLocationSource(localized.loc)) {
209
+ this.catchup(localized.loc.end);
210
+ }
211
+ return ret;
212
+ };
213
+ this.catchup = (until) => {
214
+ const start = this.generatedUntil;
215
+ if (start < until) {
216
+ this.print(this.origSource.slice(start, until));
217
+ }
218
+ this.generatedUntil = Math.max(this.generatedUntil, until);
219
+ };
220
+ this.print = (...args) => {
221
+ const pureArgs = args.filter((x) => x.length > 0);
222
+ if (pureArgs.length > 0) {
223
+ const [head2, ...tail] = pureArgs;
224
+ if (this.expectsSpace) {
225
+ this.expectsSpace = false;
226
+ const blanks = /* @__PURE__ */ new Set(["\n", " ", " "]);
227
+ if (this.stringBuilder.length > 0 && !(blanks.has(head2[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
228
+ this.stringBuilder.push(" ");
229
+ }
40
230
  }
41
- return def.gImpl(selfRef)(input, this.getSafeContext(), arg);
231
+ this.stringBuilder.push(head2);
232
+ this.stringBuilder.push(...tail);
233
+ }
234
+ };
235
+ this.printWord = (...args) => {
236
+ this.expectsSpace = true;
237
+ this.print(...args);
238
+ this.expectsSpace = true;
239
+ };
240
+ this.printSpaceLeft = (...args) => {
241
+ this.expectsSpace = true;
242
+ this.print(...args);
243
+ };
244
+ this.printWords = (...args) => {
245
+ for (const arg of args) {
246
+ this.printWord(arg);
42
247
  }
43
248
  };
44
249
  for (const rule of Object.values(rules)) {
45
250
  this[rule.name] = (input, context, args) => {
251
+ this.expectsSpace = false;
252
+ this.stringBuilder.length = 0;
253
+ this.origSource = context.origSource;
254
+ this.generatedUntil = context?.offset ?? 0;
46
255
  this.setContext(context);
47
- return rule.gImpl(selfRef)(input, this.getSafeContext(), args);
256
+ this.subrule(rule, input, args);
257
+ this.catchup(this.origSource.length);
258
+ return this.stringBuilder.join("");
48
259
  };
49
260
  }
50
261
  }
@@ -65,7 +276,7 @@ function listToRuleDefMap(rules) {
65
276
  return newRules;
66
277
  }
67
278
  var GeneratorBuilder = class _GeneratorBuilder {
68
- static createBuilder(start) {
279
+ static create(start) {
69
280
  if (start instanceof _GeneratorBuilder) {
70
281
  return new _GeneratorBuilder({ ...start.rules });
71
282
  }
@@ -74,6 +285,9 @@ var GeneratorBuilder = class _GeneratorBuilder {
74
285
  constructor(startRules) {
75
286
  this.rules = startRules;
76
287
  }
288
+ widenContext() {
289
+ return this;
290
+ }
77
291
  typePatch() {
78
292
  return this;
79
293
  }
@@ -1514,8 +1728,8 @@ var Promise2 = getNative_default(root_default, "Promise");
1514
1728
  var Promise_default = Promise2;
1515
1729
 
1516
1730
  // ../../node_modules/lodash-es/_Set.js
1517
- var Set = getNative_default(root_default, "Set");
1518
- var Set_default = Set;
1731
+ var Set2 = getNative_default(root_default, "Set");
1732
+ var Set_default = Set2;
1519
1733
 
1520
1734
  // ../../node_modules/lodash-es/_getTag.js
1521
1735
  var mapTag2 = "[object Map]";
@@ -9420,6 +9634,10 @@ var LexerBuilder = class _LexerBuilder {
9420
9634
  }
9421
9635
  return this;
9422
9636
  }
9637
+ /**
9638
+ * @param before token to move rest before
9639
+ * @param tokens tokens to move before the first token
9640
+ */
9423
9641
  moveBefore(before, ...tokens) {
9424
9642
  return this.moveBeforeOrAfter("before", before, ...tokens);
9425
9643
  }
@@ -9448,9 +9666,9 @@ var LexerBuilder = class _LexerBuilder {
9448
9666
  return new Lexer(this.tokens, {
9449
9667
  positionTracking: "onlyStart",
9450
9668
  recoveryEnabled: false,
9669
+ ensureOptimizations: true,
9451
9670
  // SafeMode: true,
9452
9671
  // SkipValidations: true,
9453
- ensureOptimizations: true,
9454
9672
  ...lexerConfig
9455
9673
  });
9456
9674
  }
@@ -9584,7 +9802,7 @@ var ParserBuilder = class _ParserBuilder {
9584
9802
  * Create a builder from some initial grammar rules or an existing builder.
9585
9803
  * If a builder is provided, a new copy will be created.
9586
9804
  */
9587
- static createBuilder(start) {
9805
+ static create(start) {
9588
9806
  if (start instanceof _ParserBuilder) {
9589
9807
  return new _ParserBuilder({ ...start.rules });
9590
9808
  }
@@ -9593,6 +9811,9 @@ var ParserBuilder = class _ParserBuilder {
9593
9811
  constructor(startRules) {
9594
9812
  this.rules = startRules;
9595
9813
  }
9814
+ widenContext() {
9815
+ return this;
9816
+ }
9596
9817
  typePatch() {
9597
9818
  return this;
9598
9819
  }
@@ -9688,7 +9909,14 @@ ${firstError.message}`);
9688
9909
  queryPreProcessor = (s) => s,
9689
9910
  errorHandler
9690
9911
  }) {
9691
- const lexer = LexerBuilder.create().add(...tokenVocabulary).build(lexerConfig);
9912
+ const lexer = LexerBuilder.create().add(...tokenVocabulary).build({
9913
+ positionTracking: "full",
9914
+ recoveryEnabled: false,
9915
+ // SafeMode: true,
9916
+ // SkipValidations: true,
9917
+ // ensureOptimizations: true,
9918
+ ...lexerConfig
9919
+ });
9692
9920
  const parser = this.consume({
9693
9921
  tokenVocabulary,
9694
9922
  config: parserConfig
@@ -9725,6 +9953,128 @@ function unCapitalize(str) {
9725
9953
  function createToken2(config) {
9726
9954
  return createToken(config);
9727
9955
  }
9956
+
9957
+ // lib/RangeArithmetic.ts
9958
+ var RangeArithmetic = class _RangeArithmetic {
9959
+ constructor(start, end) {
9960
+ this.ranges = [];
9961
+ this.initRange = _RangeArithmetic.validate(start, end);
9962
+ this.ranges.push(this.initRange);
9963
+ }
9964
+ static validate(...range) {
9965
+ const [start, end] = range;
9966
+ if (start >= end) {
9967
+ throw new Error("Invalid range");
9968
+ }
9969
+ return range;
9970
+ }
9971
+ static substractRange(included, ...range) {
9972
+ const [sMinus, eMinus] = _RangeArithmetic.validate(...range);
9973
+ return included.flatMap(([sCur, eCur]) => {
9974
+ if (sCur < sMinus && eMinus < eCur) {
9975
+ return [[sCur, sMinus], [eMinus, eCur]];
9976
+ }
9977
+ if (sMinus <= sCur && sCur < eMinus && eMinus < eCur) {
9978
+ return [[eMinus, eCur]];
9979
+ }
9980
+ if (sCur < sMinus && sMinus < eCur && eCur <= eMinus) {
9981
+ return [[sCur, sMinus]];
9982
+ }
9983
+ if (sMinus <= sCur && eCur <= eMinus) {
9984
+ return [];
9985
+ }
9986
+ return [[sCur, eCur]];
9987
+ });
9988
+ }
9989
+ subtract(...range) {
9990
+ this.ranges = _RangeArithmetic.substractRange(this.ranges, ...range);
9991
+ return this;
9992
+ }
9993
+ negate() {
9994
+ let iter = [this.initRange];
9995
+ for (const range of this.ranges) {
9996
+ iter = _RangeArithmetic.substractRange(iter, ...range);
9997
+ }
9998
+ this.ranges = iter;
9999
+ return this;
10000
+ }
10001
+ projection(...range) {
10002
+ const [sProj, eProj] = range;
10003
+ if (sProj >= eProj) {
10004
+ return [];
10005
+ }
10006
+ return this.ranges.flatMap(([sCur, eCur]) => {
10007
+ if (sCur < sProj && eProj < eCur) {
10008
+ return [[sProj, eProj]];
10009
+ }
10010
+ if (sProj <= sCur && eCur <= eProj) {
10011
+ return [[sCur, eCur]];
10012
+ }
10013
+ if (sProj <= sCur && sCur < eProj && eProj < eCur) {
10014
+ return [[sCur, eProj]];
10015
+ }
10016
+ if (sCur < sProj && sProj < eCur && eCur < eProj) {
10017
+ return [[sProj, eCur]];
10018
+ }
10019
+ return [];
10020
+ });
10021
+ }
10022
+ };
10023
+
10024
+ // lib/Transformer.ts
10025
+ var Transformer = class {
10026
+ transformNode(curObject, searchType, patch) {
10027
+ const copy = { ...curObject };
10028
+ for (const [key, value] of Object.entries(copy)) {
10029
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, searchType, patch));
10030
+ }
10031
+ if (copy.type === searchType) {
10032
+ return patch(copy);
10033
+ }
10034
+ return copy;
10035
+ }
10036
+ transformNodeSpecific(curObject, searchType, searchSubType, patch) {
10037
+ const copy = { ...curObject };
10038
+ for (const [key, value] of Object.entries(copy)) {
10039
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, searchType, searchSubType, patch));
10040
+ }
10041
+ if (copy.type === searchType && copy.subType === searchSubType) {
10042
+ return patch(copy);
10043
+ }
10044
+ return copy;
10045
+ }
10046
+ visitObjects(curObject, visitor) {
10047
+ for (const value of Object.values(curObject)) {
10048
+ this.safeObjectVisit(value, (obj) => this.visitObjects(obj, visitor));
10049
+ }
10050
+ }
10051
+ visitNode(curObject, searchType, visitor) {
10052
+ for (const value of Object.values(curObject)) {
10053
+ this.safeObjectVisit(value, (obj) => this.visitNode(obj, searchType, visitor));
10054
+ }
10055
+ if (curObject.type === searchType) {
10056
+ visitor(curObject);
10057
+ }
10058
+ }
10059
+ visitNodeSpecific(curObject, searchType, searchSubType, visitor) {
10060
+ for (const value of Object.values(curObject)) {
10061
+ this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, searchType, searchSubType, visitor));
10062
+ }
10063
+ const cast = curObject;
10064
+ if (cast.type === searchType && cast.subType === searchSubType) {
10065
+ visitor(curObject);
10066
+ }
10067
+ }
10068
+ safeObjectVisit(value, mapper) {
10069
+ if (value && typeof value === "object") {
10070
+ if (Array.isArray(value)) {
10071
+ return value.map((x) => this.safeObjectVisit(x, mapper));
10072
+ }
10073
+ return mapper(value);
10074
+ }
10075
+ return value;
10076
+ }
10077
+ };
9728
10078
  /*! Bundled license information:
9729
10079
 
9730
10080
  lodash-es/lodash.js:
package/lib/index.d.ts CHANGED
@@ -6,3 +6,7 @@ export * from './parser-builder/builderTypes';
6
6
  export * from './parser-builder/parserBuilder';
7
7
  export * from './parser-builder/ruleDefTypes';
8
8
  export * from './utils';
9
+ export * from './RangeArithmetic';
10
+ export * from './CoreFactory';
11
+ export * from './nodeTypings';
12
+ export * from './Transformer';
package/lib/index.js CHANGED
@@ -10,4 +10,8 @@ export * from './parser-builder/parserBuilder';
10
10
  export * from './parser-builder/ruleDefTypes';
11
11
  // Export general types
12
12
  export * from './utils';
13
+ export * from './RangeArithmetic';
14
+ export * from './CoreFactory';
15
+ export * from './nodeTypings';
16
+ export * from './Transformer';
13
17
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,cAAc,kCAAkC,CAAC;AACjD,cAAc,sCAAsC,CAAC;AACrD,cAAc,oCAAoC,CAAC;AACnD,uBAAuB;AACvB,cAAc,8BAA8B,CAAC;AAC7C,wBAAwB;AACxB,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,uBAAuB;AACvB,cAAc,SAAS,CAAC","sourcesContent":["// Export generator builder\nexport * from './generator-builder/builderTypes';\nexport * from './generator-builder/generatorBuilder';\nexport * from './generator-builder/generatorTypes';\n// Export lexer builder\nexport * from './lexer-builder/LexerBuilder';\n// Export parser builder\nexport * from './parser-builder/builderTypes';\nexport * from './parser-builder/parserBuilder';\nexport * from './parser-builder/ruleDefTypes';\n// Export general types\nexport * from './utils';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,cAAc,kCAAkC,CAAC;AACjD,cAAc,sCAAsC,CAAC;AACrD,cAAc,oCAAoC,CAAC;AACnD,uBAAuB;AACvB,cAAc,8BAA8B,CAAC;AAC7C,wBAAwB;AACxB,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,uBAAuB;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC","sourcesContent":["// Export generator builder\nexport * from './generator-builder/builderTypes';\nexport * from './generator-builder/generatorBuilder';\nexport * from './generator-builder/generatorTypes';\n// Export lexer builder\nexport * from './lexer-builder/LexerBuilder';\n// Export parser builder\nexport * from './parser-builder/builderTypes';\nexport * from './parser-builder/parserBuilder';\nexport * from './parser-builder/ruleDefTypes';\n// Export general types\nexport * from './utils';\nexport * from './RangeArithmetic';\nexport * from './CoreFactory';\nexport * from './nodeTypings';\nexport * from './Transformer';\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
  }