@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
@@ -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,IAAI,CAAC,GAAS,KAAK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E,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":"AAGA,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,CAAC;QAClD,CAAC;QACD,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verlap } from '../utils';\nimport type { GeneratorFromRules, 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 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 = ParseNamesFromList<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 = ParseNamesFromList<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<ParseNamesFromList<U>, Names, U>\n ): GeneratorBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<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 | ParseNamesFromList<OW>,\n {[K in Names | OtherNames | ParseNamesFromList<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,243 @@ 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,
25
+ IndirBuilder: () => IndirBuilder,
24
26
  LexerBuilder: () => LexerBuilder,
25
27
  ParserBuilder: () => ParserBuilder,
28
+ Transformer: () => Transformer,
26
29
  createToken: () => createToken2,
30
+ listToIndirectionMap: () => listToIndirectionMap,
27
31
  unCapitalize: () => unCapitalize
28
32
  });
29
33
  module.exports = __toCommonJS(index_exports);
30
34
 
35
+ // lib/CoreFactory.ts
36
+ var CoreFactory = class {
37
+ wrap(val, loc) {
38
+ return { val, loc };
39
+ }
40
+ isLocalized(obj) {
41
+ return typeof obj === "object" && obj !== null && "loc" in obj && typeof obj.loc === "object" && obj.loc !== null && "sourceLocationType" in obj.loc;
42
+ }
43
+ sourceLocation(...elements) {
44
+ const pureElements = elements.filter((x) => x !== void 0);
45
+ if (pureElements.length === 0) {
46
+ return this.sourceLocationNoMaterialize();
47
+ }
48
+ const filtered = pureElements.filter((element) => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) || this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
49
+ if (filtered.length === 0) {
50
+ return this.gen();
51
+ }
52
+ const first2 = filtered[0];
53
+ const last2 = filtered.at(-1);
54
+ return {
55
+ sourceLocationType: "source",
56
+ start: this.isLocalized(first2) ? first2.loc.start : first2.startOffset,
57
+ end: this.isLocalized(last2) ? last2.loc.end : last2.endOffset + 1
58
+ };
59
+ }
60
+ sourceLocationNoMaterialize() {
61
+ return { sourceLocationType: "noMaterialize" };
62
+ }
63
+ /**
64
+ * Returns a copy of the argument that is not materialized
65
+ */
66
+ dematerialized(arg) {
67
+ return { ...arg, loc: this.sourceLocationNoMaterialize() };
68
+ }
69
+ safeObjectTransform(value, mapper) {
70
+ if (value && typeof value === "object") {
71
+ if (Array.isArray(value)) {
72
+ return value.map((x) => this.safeObjectTransform(x, mapper));
73
+ }
74
+ return mapper(value);
75
+ }
76
+ return value;
77
+ }
78
+ forcedAutoGenTree(obj) {
79
+ const copy = { ...obj };
80
+ for (const [key, value] of Object.entries(copy)) {
81
+ copy[key] = this.safeObjectTransform(value, (obj2) => this.forcedAutoGenTree(obj2));
82
+ }
83
+ if (this.isLocalized(copy)) {
84
+ copy.loc = this.gen();
85
+ }
86
+ return copy;
87
+ }
88
+ forceMaterialized(arg) {
89
+ if (this.isSourceLocationNoMaterialize(arg.loc)) {
90
+ return this.forcedAutoGenTree(arg);
91
+ }
92
+ return { ...arg };
93
+ }
94
+ isSourceLocation(loc) {
95
+ return "sourceLocationType" in loc;
96
+ }
97
+ sourceLocationSource(start, end) {
98
+ return {
99
+ sourceLocationType: "source",
100
+ start,
101
+ end
102
+ };
103
+ }
104
+ gen() {
105
+ return { sourceLocationType: "autoGenerate" };
106
+ }
107
+ isSourceLocationSource(loc) {
108
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "source";
109
+ }
110
+ isSourceLocationStringReplace(loc) {
111
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "stringReplace";
112
+ }
113
+ sourceLocationNodeReplaceUnsafe(loc) {
114
+ if (this.isSourceLocationSource(loc)) {
115
+ return this.sourceLocationNodeReplace(loc);
116
+ }
117
+ throw new Error("not possible");
118
+ }
119
+ sourceLocationNodeReplace(startOrLoc, end) {
120
+ let starting;
121
+ let ending;
122
+ if (typeof startOrLoc === "number") {
123
+ starting = startOrLoc;
124
+ ending = end;
125
+ } else {
126
+ starting = startOrLoc.start;
127
+ ending = startOrLoc.end;
128
+ }
129
+ return {
130
+ sourceLocationType: "nodeReplace",
131
+ start: starting,
132
+ end: ending
133
+ };
134
+ }
135
+ isSourceLocationNodeReplace(loc) {
136
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "nodeReplace";
137
+ }
138
+ isSourceLocationNodeAutoGenerate(loc) {
139
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "autoGenerate";
140
+ }
141
+ nodeShouldPrint(node) {
142
+ return this.isSourceLocationNodeReplace(node.loc) || this.isSourceLocationNodeAutoGenerate(node.loc);
143
+ }
144
+ printFilter(node, callback) {
145
+ if (this.nodeShouldPrint(node)) {
146
+ callback();
147
+ }
148
+ }
149
+ isSourceLocationNoMaterialize(loc) {
150
+ return this.isSourceLocation(loc) && loc.sourceLocationType === "noMaterialize";
151
+ }
152
+ isOfType(obj, type) {
153
+ const casted = obj;
154
+ return casted.type === type;
155
+ }
156
+ isOfSubType(obj, type, subType) {
157
+ const temp = obj;
158
+ return temp.type === type && temp.subType === subType;
159
+ }
160
+ };
161
+
31
162
  // lib/generator-builder/dynamicGenerator.ts
32
163
  var DynamicGenerator = class {
33
164
  constructor(rules) {
165
+ this.rules = rules;
166
+ this.factory = new CoreFactory();
34
167
  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`);
168
+ this.origSource = "";
169
+ this.generatedUntil = 0;
170
+ this.stringBuilder = [];
171
+ this.subrule = (cstDef, ast, arg) => {
172
+ const def = this.rules[cstDef.name];
173
+ if (!def) {
174
+ throw new Error(`Rule ${cstDef.name} not found`);
175
+ }
176
+ const generate = () => def.gImpl({
177
+ SUBRULE: this.subrule,
178
+ PRINT: this.print,
179
+ PRINT_SPACE_LEFT: this.printSpaceLeft,
180
+ PRINT_WORD: this.printWord,
181
+ PRINT_WORDS: this.printWords,
182
+ CATCHUP: this.catchup,
183
+ HANDLE_LOC: this.handleLoc
184
+ })(ast, this.getSafeContext(), arg);
185
+ if (this.factory.isLocalized(ast)) {
186
+ this.handleLoc(ast, generate);
187
+ } else {
188
+ generate();
189
+ }
190
+ };
191
+ this.handleLoc = (localized, handle) => {
192
+ if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
193
+ return;
194
+ }
195
+ if (this.factory.isSourceLocationStringReplace(localized.loc)) {
196
+ this.catchup(localized.loc.start);
197
+ this.print(localized.loc.newSource);
198
+ this.generatedUntil = localized.loc.end;
199
+ return;
200
+ }
201
+ if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
202
+ this.catchup(localized.loc.start);
203
+ this.generatedUntil = localized.loc.end;
204
+ }
205
+ if (this.factory.isSourceLocationSource(localized.loc)) {
206
+ this.catchup(localized.loc.start);
207
+ }
208
+ const ret = handle();
209
+ if (this.factory.isSourceLocationSource(localized.loc)) {
210
+ this.catchup(localized.loc.end);
211
+ }
212
+ return ret;
213
+ };
214
+ this.catchup = (until) => {
215
+ const start = this.generatedUntil;
216
+ if (start < until) {
217
+ this.print(this.origSource.slice(start, until));
218
+ }
219
+ this.generatedUntil = Math.max(this.generatedUntil, until);
220
+ };
221
+ this.print = (...args) => {
222
+ const pureArgs = args.filter((x) => x.length > 0);
223
+ if (pureArgs.length > 0) {
224
+ const [head2, ...tail] = pureArgs;
225
+ if (this.expectsSpace) {
226
+ this.expectsSpace = false;
227
+ const blanks = /* @__PURE__ */ new Set(["\n", " ", " "]);
228
+ if (this.stringBuilder.length > 0 && !(blanks.has(head2[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
229
+ this.stringBuilder.push(" ");
230
+ }
40
231
  }
41
- return def.gImpl(selfRef)(input, this.getSafeContext(), arg);
232
+ this.stringBuilder.push(head2);
233
+ this.stringBuilder.push(...tail);
234
+ }
235
+ };
236
+ this.printWord = (...args) => {
237
+ this.expectsSpace = true;
238
+ this.print(...args);
239
+ this.expectsSpace = true;
240
+ };
241
+ this.printSpaceLeft = (...args) => {
242
+ this.expectsSpace = true;
243
+ this.print(...args);
244
+ };
245
+ this.printWords = (...args) => {
246
+ for (const arg of args) {
247
+ this.printWord(arg);
42
248
  }
43
249
  };
44
250
  for (const rule of Object.values(rules)) {
45
251
  this[rule.name] = (input, context, args) => {
252
+ this.expectsSpace = false;
253
+ this.stringBuilder.length = 0;
254
+ this.origSource = context.origSource;
255
+ this.generatedUntil = context?.offset ?? 0;
46
256
  this.setContext(context);
47
- return rule.gImpl(selfRef)(input, this.getSafeContext(), args);
257
+ this.subrule(rule, input, args);
258
+ this.catchup(this.origSource.length);
259
+ return this.stringBuilder.join("");
48
260
  };
49
261
  }
50
262
  }
@@ -65,7 +277,7 @@ function listToRuleDefMap(rules) {
65
277
  return newRules;
66
278
  }
67
279
  var GeneratorBuilder = class _GeneratorBuilder {
68
- static createBuilder(start) {
280
+ static create(start) {
69
281
  if (start instanceof _GeneratorBuilder) {
70
282
  return new _GeneratorBuilder({ ...start.rules });
71
283
  }
@@ -74,6 +286,9 @@ var GeneratorBuilder = class _GeneratorBuilder {
74
286
  constructor(startRules) {
75
287
  this.rules = startRules;
76
288
  }
289
+ widenContext() {
290
+ return this;
291
+ }
77
292
  typePatch() {
78
293
  return this;
79
294
  }
@@ -149,6 +364,103 @@ var GeneratorBuilder = class _GeneratorBuilder {
149
364
  }
150
365
  };
151
366
 
367
+ // lib/indirection-builder/helpers.ts
368
+ function listToIndirectionMap(rules) {
369
+ const newRules = {};
370
+ for (const rule of rules) {
371
+ newRules[rule.name] = rule;
372
+ }
373
+ return newRules;
374
+ }
375
+
376
+ // lib/indirection-builder/dynamicIndirected.ts
377
+ var DynamicIndirect = class {
378
+ constructor(rules) {
379
+ this.rules = rules;
380
+ this.__context = void 0;
381
+ this.subrule = (cstDef, ...args) => {
382
+ const def = this.rules[cstDef.name];
383
+ if (!def) {
384
+ throw new Error(`Rule ${cstDef.name} not found`);
385
+ }
386
+ return def.fun({
387
+ SUBRULE: this.subrule
388
+ })(this.getSafeContext(), ...args);
389
+ };
390
+ for (const rule of Object.values(rules)) {
391
+ this[rule.name] = (context, ...args) => {
392
+ this.setContext(context);
393
+ return this.subrule(rule, ...args);
394
+ };
395
+ }
396
+ }
397
+ setContext(context) {
398
+ this.__context = context;
399
+ }
400
+ getSafeContext() {
401
+ return this.__context;
402
+ }
403
+ };
404
+
405
+ // lib/indirection-builder/IndirBuilder.ts
406
+ var IndirBuilder = class _IndirBuilder {
407
+ static create(start) {
408
+ if (start instanceof _IndirBuilder) {
409
+ return new _IndirBuilder({ ...start.rules });
410
+ }
411
+ return new _IndirBuilder(listToIndirectionMap(start));
412
+ }
413
+ constructor(startRules) {
414
+ this.rules = startRules;
415
+ }
416
+ widenContext() {
417
+ return this;
418
+ }
419
+ typePatch() {
420
+ return this;
421
+ }
422
+ /**
423
+ * Change the implementation of an existing indirection.
424
+ */
425
+ patchRule(patch) {
426
+ const self2 = this;
427
+ self2.rules[patch.name] = patch;
428
+ return self2;
429
+ }
430
+ /**
431
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
432
+ */
433
+ addRuleRedundant(rule) {
434
+ const self2 = this;
435
+ const rules = self2.rules;
436
+ if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
437
+ throw new Error(`Function ${rule.name} already exists in the builder`);
438
+ }
439
+ rules[rule.name] = rule;
440
+ return self2;
441
+ }
442
+ /**
443
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
444
+ */
445
+ addRule(rule) {
446
+ return this.addRuleRedundant(rule);
447
+ }
448
+ addMany(...rules) {
449
+ this.rules = { ...this.rules, ...listToIndirectionMap(rules) };
450
+ return this;
451
+ }
452
+ /**
453
+ * Delete a grammar rule by its name.
454
+ */
455
+ deleteRule(ruleName) {
456
+ delete this.rules[ruleName];
457
+ return this;
458
+ }
459
+ build() {
460
+ return new DynamicIndirect(this.rules);
461
+ }
462
+ };
463
+
152
464
  // ../../node_modules/lodash-es/_freeGlobal.js
153
465
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
154
466
  var freeGlobal_default = freeGlobal;
@@ -1514,8 +1826,8 @@ var Promise2 = getNative_default(root_default, "Promise");
1514
1826
  var Promise_default = Promise2;
1515
1827
 
1516
1828
  // ../../node_modules/lodash-es/_Set.js
1517
- var Set = getNative_default(root_default, "Set");
1518
- var Set_default = Set;
1829
+ var Set2 = getNative_default(root_default, "Set");
1830
+ var Set_default = Set2;
1519
1831
 
1520
1832
  // ../../node_modules/lodash-es/_getTag.js
1521
1833
  var mapTag2 = "[object Map]";
@@ -9420,6 +9732,10 @@ var LexerBuilder = class _LexerBuilder {
9420
9732
  }
9421
9733
  return this;
9422
9734
  }
9735
+ /**
9736
+ * @param before token to move rest before
9737
+ * @param tokens tokens to move before the first token
9738
+ */
9423
9739
  moveBefore(before, ...tokens) {
9424
9740
  return this.moveBeforeOrAfter("before", before, ...tokens);
9425
9741
  }
@@ -9448,9 +9764,9 @@ var LexerBuilder = class _LexerBuilder {
9448
9764
  return new Lexer(this.tokens, {
9449
9765
  positionTracking: "onlyStart",
9450
9766
  recoveryEnabled: false,
9767
+ ensureOptimizations: true,
9451
9768
  // SafeMode: true,
9452
9769
  // SkipValidations: true,
9453
- ensureOptimizations: true,
9454
9770
  ...lexerConfig
9455
9771
  });
9456
9772
  }
@@ -9584,7 +9900,7 @@ var ParserBuilder = class _ParserBuilder {
9584
9900
  * Create a builder from some initial grammar rules or an existing builder.
9585
9901
  * If a builder is provided, a new copy will be created.
9586
9902
  */
9587
- static createBuilder(start) {
9903
+ static create(start) {
9588
9904
  if (start instanceof _ParserBuilder) {
9589
9905
  return new _ParserBuilder({ ...start.rules });
9590
9906
  }
@@ -9593,6 +9909,9 @@ var ParserBuilder = class _ParserBuilder {
9593
9909
  constructor(startRules) {
9594
9910
  this.rules = startRules;
9595
9911
  }
9912
+ widenContext() {
9913
+ return this;
9914
+ }
9596
9915
  typePatch() {
9597
9916
  return this;
9598
9917
  }
@@ -9688,7 +10007,14 @@ ${firstError.message}`);
9688
10007
  queryPreProcessor = (s) => s,
9689
10008
  errorHandler
9690
10009
  }) {
9691
- const lexer = LexerBuilder.create().add(...tokenVocabulary).build(lexerConfig);
10010
+ const lexer = LexerBuilder.create().add(...tokenVocabulary).build({
10011
+ positionTracking: "full",
10012
+ recoveryEnabled: false,
10013
+ // SafeMode: true,
10014
+ // SkipValidations: true,
10015
+ // ensureOptimizations: true,
10016
+ ...lexerConfig
10017
+ });
9692
10018
  const parser = this.consume({
9693
10019
  tokenVocabulary,
9694
10020
  config: parserConfig
@@ -9725,6 +10051,61 @@ function unCapitalize(str) {
9725
10051
  function createToken2(config) {
9726
10052
  return createToken(config);
9727
10053
  }
10054
+
10055
+ // lib/Transformer.ts
10056
+ var Transformer = class {
10057
+ transformNode(curObject, searchType, patch) {
10058
+ const copy = { ...curObject };
10059
+ for (const [key, value] of Object.entries(copy)) {
10060
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNode(obj, searchType, patch));
10061
+ }
10062
+ if (copy.type === searchType) {
10063
+ return patch(copy);
10064
+ }
10065
+ return copy;
10066
+ }
10067
+ transformNodeSpecific(curObject, searchType, searchSubType, patch) {
10068
+ const copy = { ...curObject };
10069
+ for (const [key, value] of Object.entries(copy)) {
10070
+ copy[key] = this.safeObjectVisit(value, (obj) => this.transformNodeSpecific(obj, searchType, searchSubType, patch));
10071
+ }
10072
+ if (copy.type === searchType && copy.subType === searchSubType) {
10073
+ return patch(copy);
10074
+ }
10075
+ return copy;
10076
+ }
10077
+ visitObjects(curObject, visitor) {
10078
+ for (const value of Object.values(curObject)) {
10079
+ this.safeObjectVisit(value, (obj) => this.visitObjects(obj, visitor));
10080
+ }
10081
+ }
10082
+ visitNode(curObject, searchType, visitor) {
10083
+ for (const value of Object.values(curObject)) {
10084
+ this.safeObjectVisit(value, (obj) => this.visitNode(obj, searchType, visitor));
10085
+ }
10086
+ if (curObject.type === searchType) {
10087
+ visitor(curObject);
10088
+ }
10089
+ }
10090
+ visitNodeSpecific(curObject, searchType, searchSubType, visitor) {
10091
+ for (const value of Object.values(curObject)) {
10092
+ this.safeObjectVisit(value, (obj) => this.visitNodeSpecific(obj, searchType, searchSubType, visitor));
10093
+ }
10094
+ const cast = curObject;
10095
+ if (cast.type === searchType && cast.subType === searchSubType) {
10096
+ visitor(curObject);
10097
+ }
10098
+ }
10099
+ safeObjectVisit(value, mapper) {
10100
+ if (value && typeof value === "object") {
10101
+ if (Array.isArray(value)) {
10102
+ return value.map((x) => this.safeObjectVisit(x, mapper));
10103
+ }
10104
+ return mapper(value);
10105
+ }
10106
+ return value;
10107
+ }
10108
+ };
9728
10109
  /*! Bundled license information:
9729
10110
 
9730
10111
  lodash-es/lodash.js:
package/lib/index.d.ts CHANGED
@@ -1,8 +1,13 @@
1
1
  export * from './generator-builder/builderTypes';
2
2
  export * from './generator-builder/generatorBuilder';
3
3
  export * from './generator-builder/generatorTypes';
4
+ export * from './indirection-builder/helpers';
5
+ export * from './indirection-builder/IndirBuilder';
4
6
  export * from './lexer-builder/LexerBuilder';
5
7
  export * from './parser-builder/builderTypes';
6
8
  export * from './parser-builder/parserBuilder';
7
9
  export * from './parser-builder/ruleDefTypes';
8
10
  export * from './utils';
11
+ export * from './CoreFactory';
12
+ export * from './nodeTypings';
13
+ export * from './Transformer';
package/lib/index.js CHANGED
@@ -2,6 +2,9 @@
2
2
  export * from './generator-builder/builderTypes';
3
3
  export * from './generator-builder/generatorBuilder';
4
4
  export * from './generator-builder/generatorTypes';
5
+ // Export Indirection Builder
6
+ export * from './indirection-builder/helpers';
7
+ export * from './indirection-builder/IndirBuilder';
5
8
  // Export lexer builder
6
9
  export * from './lexer-builder/LexerBuilder';
7
10
  // Export parser builder
@@ -10,4 +13,7 @@ export * from './parser-builder/parserBuilder';
10
13
  export * from './parser-builder/ruleDefTypes';
11
14
  // Export general types
12
15
  export * from './utils';
16
+ export * from './CoreFactory';
17
+ export * from './nodeTypings';
18
+ export * from './Transformer';
13
19
  //# 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,6BAA6B;AAC7B,cAAc,+BAA+B,CAAC;AAC9C,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,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 Indirection Builder\nexport * from './indirection-builder/helpers';\nexport * from './indirection-builder/IndirBuilder';\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 './CoreFactory';\nexport * from './nodeTypings';\nexport * from './Transformer';\n"]}