@traqula/core 0.0.1-alpha.9 → 0.0.2

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.
@@ -1,8 +1,5 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
1
2
  import type { GeneratorRule } from './generatorTypes';
2
- /**
3
- * Get union-type of names used in list of ruledefs.
4
- */
5
- export type GenNamesFromList<T extends readonly GeneratorRule[]> = T[number]['name'];
6
3
  /**
7
4
  * Convert a list of ruledefs to a record that maps each rule name to its definition.
8
5
  */
@@ -12,7 +9,7 @@ export type GenRuleMap<RuleNames extends string> = {
12
9
  /**
13
10
  * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.
14
11
  */
15
- export type GenRulesToObject<T extends readonly GeneratorRule[], Names extends string = GenNamesFromList<T>, Agg extends Record<string, GeneratorRule> = Record<never, never>> = T extends readonly [infer First, ...infer Rest] ? (First extends GeneratorRule ? (Rest extends readonly GeneratorRule[] ? (GenRulesToObject<Rest, Names, {
12
+ export type GenRulesToObject<T extends readonly GeneratorRule[], Names extends string = ParseNamesFromList<T>, Agg extends Record<string, GeneratorRule> = Record<never, never>> = T extends readonly [infer First, ...infer Rest] ? (First extends GeneratorRule ? (Rest extends readonly GeneratorRule[] ? (GenRulesToObject<Rest, Names, {
16
13
  [K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K];
17
14
  }>) : never) : never) : GenRuleMap<Names> & Agg;
18
15
  export type GeneratorFromRules<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> = {
@@ -1 +1 @@
1
- {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { GeneratorRule } from './generatorTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type GenNamesFromList<T extends readonly GeneratorRule[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type GenRuleMap<RuleNames extends string> = {[Key in RuleNames]: GeneratorRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type GenRulesToObject<\n T extends readonly GeneratorRule[],\n Names extends string = GenNamesFromList<T>,\n Agg extends Record<string, GeneratorRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends GeneratorRule ? (\n Rest extends readonly GeneratorRule[] ? (\n GenRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : GenRuleMap<Names> & Agg;\n\nexport type GeneratorFromRules<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends GeneratorRule<Context, K, infer RET, infer ARGS> ?\n (input: RET, context: Context & { origSource: string; offset?: number }, args: ARGS) => string : never\n};\n"]}
1
+ {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes';\nimport type { GeneratorRule } from './generatorTypes';\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type GenRuleMap<RuleNames extends string> = {[Key in RuleNames]: GeneratorRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type GenRulesToObject<\n T extends readonly GeneratorRule[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, GeneratorRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends GeneratorRule ? (\n Rest extends readonly GeneratorRule[] ? (\n GenRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : GenRuleMap<Names> & Agg;\n\nexport type GeneratorFromRules<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends GeneratorRule<Context, K, infer RET, infer ARGS> ?\n (input: RET, context: Context & { origSource: string; offset?: number }, args: ARGS) => string : never\n};\n"]}
@@ -1,5 +1,6 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
1
2
  import type { CheckOverlap } from '../utils';
2
- import type { GeneratorFromRules, GenRuleMap, GenRulesToObject, GenNamesFromList } from './builderTypes';
3
+ import type { GeneratorFromRules, GenRuleMap, GenRulesToObject } from './builderTypes';
3
4
  import type { GeneratorRule } from './generatorTypes';
4
5
  export declare class GeneratorBuilder<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {
5
6
  /**
@@ -7,7 +8,7 @@ export declare class GeneratorBuilder<Context, Names extends string, RuleDefs ex
7
8
  * If a GeneratorBuilder is provided, a new copy will be created.
8
9
  */
9
10
  static create<Context, Names extends string, RuleDefs extends GenRuleMap<Names>>(args: GeneratorBuilder<Context, Names, RuleDefs>): GeneratorBuilder<Context, Names, RuleDefs>;
10
- static create<Rules extends readonly GeneratorRule[] = readonly GeneratorRule[], Context = Rules[0] extends GeneratorRule<infer context> ? context : never, Names extends string = GenNamesFromList<Rules>, RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>>(rules: Rules): GeneratorBuilder<Context, Names, RuleDefs>;
11
+ static create<Rules extends readonly GeneratorRule[] = readonly GeneratorRule[], Context = Rules[0] extends GeneratorRule<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends GenRuleMap<Names> = GenRulesToObject<Rules>>(rules: Rules): GeneratorBuilder<Context, Names, RuleDefs>;
11
12
  private rules;
12
13
  private constructor();
13
14
  widenContext<NewContext extends Context>(): GeneratorBuilder<NewContext, Names, RuleDefs>;
@@ -34,8 +35,8 @@ export declare class GeneratorBuilder<Context, Names extends string, RuleDefs ex
34
35
  addRule<U extends string, RET, ARGS>(rule: CheckOverlap<U, Names, GeneratorRule<Context, U, RET, ARGS>>): GeneratorBuilder<Context, Names | U, {
35
36
  [K in Names | U]: K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never) : (K extends U ? GeneratorRule<Context, K, RET, ARGS> : never);
36
37
  }>;
37
- addMany<U extends readonly GeneratorRule<Context>[]>(...rules: CheckOverlap<GenNamesFromList<U>, Names, U>): GeneratorBuilder<Context, Names | GenNamesFromList<U>, {
38
- [K in Names | GenNamesFromList<U>]: K extends keyof GenRulesToObject<typeof rules> ? (GenRulesToObject<typeof rules>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<typeof rules>[K] : never) : (K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never) : never);
38
+ addMany<U extends readonly GeneratorRule<Context>[]>(...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>): GeneratorBuilder<Context, Names | ParseNamesFromList<U>, {
39
+ [K in Names | ParseNamesFromList<U>]: K extends keyof GenRulesToObject<typeof rules> ? (GenRulesToObject<typeof rules>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<typeof rules>[K] : never) : (K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never) : never);
39
40
  }>;
40
41
  /**
41
42
  * Delete a grammar rule by its name.
@@ -52,8 +53,8 @@ export declare class GeneratorBuilder<Context, Names extends string, RuleDefs ex
52
53
  * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
53
54
  * this rule implementation will be used.
54
55
  */
55
- merge<OtherNames extends string, OtherRules extends GenRuleMap<OtherNames>, OW extends readonly GeneratorRule<Context>[]>(GeneratorBuilder: GeneratorBuilder<Context, OtherNames, OtherRules>, overridingRules: OW): GeneratorBuilder<Context, Names | OtherNames | GenNamesFromList<OW>, {
56
- [K in Names | OtherNames | GenNamesFromList<OW>]: K extends keyof GenRulesToObject<OW> ? (GenRulesToObject<OW>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<OW>[K] : never) : (K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never) : K extends OtherNames ? (OtherRules[K] extends GeneratorRule<Context, K> ? OtherRules[K] : never) : never);
56
+ merge<OtherNames extends string, OtherRules extends GenRuleMap<OtherNames>, OW extends readonly GeneratorRule<Context>[]>(GeneratorBuilder: GeneratorBuilder<Context, OtherNames, OtherRules>, overridingRules: OW): GeneratorBuilder<Context, Names | OtherNames | ParseNamesFromList<OW>, {
57
+ [K in Names | OtherNames | ParseNamesFromList<OW>]: K extends keyof GenRulesToObject<OW> ? (GenRulesToObject<OW>[K] extends GeneratorRule<Context, K> ? GenRulesToObject<OW>[K] : never) : (K extends Names ? (RuleDefs[K] extends GeneratorRule<Context, K> ? RuleDefs[K] : never) : K extends OtherNames ? (OtherRules[K] extends GeneratorRule<Context, K> ? OtherRules[K] : never) : never);
57
58
  }>;
58
59
  build(): GeneratorFromRules<Context, Names, RuleDefs>;
59
60
  }
@@ -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,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,OAA8D,IAAI,gBAAgB,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9G,CAAC;IAID,YAAoB,UAAoB;QACtC,IAAI,CAAC,KAAK,GAAG,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QACzF,MAAM,UAAU,GAA2C,EAAE,GAAG,gBAAgB,CAAC,KAAK,EAAE,CAAC;QACzF,MAAM,OAAO,GAA2C,IAAI,CAAC,KAAK,CAAC;QAEnE,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YAC1C,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,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, 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
+ {"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,MA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C;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 { ParseNamesFromList } from '../parser-builder/builderTypes';\nimport type { CheckOverlap } 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"]}
package/lib/index.cjs CHANGED
@@ -22,11 +22,12 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  CoreFactory: () => CoreFactory,
24
24
  GeneratorBuilder: () => GeneratorBuilder,
25
+ IndirBuilder: () => IndirBuilder,
25
26
  LexerBuilder: () => LexerBuilder,
26
27
  ParserBuilder: () => ParserBuilder,
27
- RangeArithmetic: () => RangeArithmetic,
28
28
  Transformer: () => Transformer,
29
29
  createToken: () => createToken2,
30
+ listToIndirectionMap: () => listToIndirectionMap,
30
31
  unCapitalize: () => unCapitalize
31
32
  });
32
33
  module.exports = __toCommonJS(index_exports);
@@ -363,6 +364,103 @@ var GeneratorBuilder = class _GeneratorBuilder {
363
364
  }
364
365
  };
365
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
+
366
464
  // ../../node_modules/lodash-es/_freeGlobal.js
367
465
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
368
466
  var freeGlobal_default = freeGlobal;
@@ -9954,73 +10052,6 @@ function createToken2(config) {
9954
10052
  return createToken(config);
9955
10053
  }
9956
10054
 
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
10055
  // lib/Transformer.ts
10025
10056
  var Transformer = class {
10026
10057
  transformNode(curObject, searchType, patch) {
package/lib/index.d.ts CHANGED
@@ -1,12 +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';
9
- export * from './RangeArithmetic';
10
11
  export * from './CoreFactory';
11
12
  export * from './nodeTypings';
12
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,7 +13,6 @@ export * from './parser-builder/parserBuilder';
10
13
  export * from './parser-builder/ruleDefTypes';
11
14
  // Export general types
12
15
  export * from './utils';
13
- export * from './RangeArithmetic';
14
16
  export * from './CoreFactory';
15
17
  export * from './nodeTypings';
16
18
  export * from './Transformer';
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;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"]}
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"]}
@@ -0,0 +1,43 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
2
+ import type { CheckOverlap } from '../utils';
3
+ import type { IndirDef, IndirectionMap, IndirectObjFromIndirDefs, ParseIndirsToObject } from './helpers';
4
+ export declare class IndirBuilder<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {
5
+ static create<Context, Names extends string, RuleDefs extends IndirectionMap<Names>>(args: IndirBuilder<Context, Names, RuleDefs>): IndirBuilder<Context, Names, RuleDefs>;
6
+ static create<Rules extends readonly IndirDef[] = readonly IndirDef[], Context = Rules[0] extends IndirDef<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>>(rules: Rules): IndirBuilder<Context, Names, RuleDefs>;
7
+ private rules;
8
+ private constructor();
9
+ widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names, RuleDefs>;
10
+ typePatch<Patch extends {
11
+ [Key in Names]?: any;
12
+ }>(): IndirBuilder<Context, Names, {
13
+ [Key in Names]: Key extends keyof Patch ? (IndirDef<Context, Key, Patch[Key][0], Patch[Key][1]>) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never);
14
+ }>;
15
+ /**
16
+ * Change the implementation of an existing indirection.
17
+ */
18
+ patchRule<U extends Names, RET, ARGS extends any[]>(patch: IndirDef<Context, U, RET, ARGS>): IndirBuilder<Context, Names, {
19
+ [Key in Names]: Key extends U ? IndirDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never);
20
+ }>;
21
+ /**
22
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
23
+ */
24
+ addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: IndirDef<Context, U, RET, ARGS>): IndirBuilder<Context, Names | U, {
25
+ [K in Names | U]: K extends U ? IndirDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
26
+ }>;
27
+ /**
28
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
29
+ */
30
+ addRule<U extends string, RET, ARGS extends any[]>(rule: CheckOverlap<U, Names, IndirDef<Context, U, RET, ARGS>>): IndirBuilder<Context, Names | U, {
31
+ [K in Names | U]: K extends U ? IndirDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
32
+ }>;
33
+ addMany<U extends readonly IndirDef<Context>[]>(...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>): IndirBuilder<Context, Names | ParseNamesFromList<U>, {
34
+ [K in Names | ParseNamesFromList<U>]: K extends keyof ParseIndirsToObject<typeof rules> ? (ParseIndirsToObject<typeof rules>[K] extends IndirDef<Context, K> ? ParseIndirsToObject<typeof rules>[K] : never) : (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never);
35
+ }>;
36
+ /**
37
+ * Delete a grammar rule by its name.
38
+ */
39
+ deleteRule<U extends Names>(ruleName: U): IndirBuilder<Context, Exclude<Names, U>, {
40
+ [K in Exclude<Names, U>]: RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never;
41
+ }>;
42
+ build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs>;
43
+ }
@@ -0,0 +1,60 @@
1
+ import { DynamicIndirect } from './dynamicIndirected';
2
+ import { listToIndirectionMap } from './helpers';
3
+ export class IndirBuilder {
4
+ static create(start) {
5
+ if (start instanceof IndirBuilder) {
6
+ return new IndirBuilder({ ...start.rules });
7
+ }
8
+ return new IndirBuilder(listToIndirectionMap(start));
9
+ }
10
+ constructor(startRules) {
11
+ this.rules = startRules;
12
+ }
13
+ widenContext() {
14
+ return this;
15
+ }
16
+ typePatch() {
17
+ return this;
18
+ }
19
+ /**
20
+ * Change the implementation of an existing indirection.
21
+ */
22
+ patchRule(patch) {
23
+ const self = this;
24
+ self.rules[patch.name] = patch;
25
+ return self;
26
+ }
27
+ /**
28
+ * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.
29
+ */
30
+ addRuleRedundant(rule) {
31
+ const self = this;
32
+ const rules = self.rules;
33
+ if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {
34
+ throw new Error(`Function ${rule.name} already exists in the builder`);
35
+ }
36
+ rules[rule.name] = rule;
37
+ return self;
38
+ }
39
+ /**
40
+ * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
41
+ */
42
+ addRule(rule) {
43
+ return this.addRuleRedundant(rule);
44
+ }
45
+ addMany(...rules) {
46
+ this.rules = { ...this.rules, ...listToIndirectionMap(rules) };
47
+ return this;
48
+ }
49
+ /**
50
+ * Delete a grammar rule by its name.
51
+ */
52
+ deleteRule(ruleName) {
53
+ delete this.rules[ruleName];
54
+ return this;
55
+ }
56
+ build() {
57
+ return new DynamicIndirect(this.rules);
58
+ }
59
+ }
60
+ //# sourceMappingURL=IndirBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IndirBuilder.js","sourceRoot":"","sources":["IndirBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,OAAO,YAAY;IAUhB,MAAM,CAAC,MAAM,CAMlB,KAAqD;QAErD,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;YAClC,OAAO,IAAI,YAAY,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,OAA0D,IAAI,YAAY,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1G,CAAC;IAID,YAAoB,UAAoB;QACtC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;IAC1B,CAAC;IAEM,YAAY;QACjB,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../parser-builder/builderTypes';\nimport type { CheckOverlap } from '../utils';\nimport { DynamicIndirect } from './dynamicIndirected';\nimport type { IndirDef, IndirectionMap, IndirectObjFromIndirDefs, ParseIndirsToObject } from './helpers';\nimport { listToIndirectionMap } from './helpers';\n\nexport class IndirBuilder<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {\n public static create<Context, Names extends string, RuleDefs extends IndirectionMap<Names>>(\n args: IndirBuilder<Context, Names, RuleDefs>\n ): IndirBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly IndirDef[] = readonly IndirDef[],\n Context = Rules[0] extends IndirDef<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>,\n >(rules: Rules): IndirBuilder<Context, Names, RuleDefs>;\n public static create<\n Rules extends readonly IndirDef[] = readonly IndirDef[],\n Context = Rules[0] extends IndirDef<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends IndirectionMap<Names> = ParseIndirsToObject<Rules>,\n >(\n start: Rules | IndirBuilder<Context, Names, RuleDefs>,\n ): IndirBuilder<Context, Names, RuleDefs> {\n if (start instanceof IndirBuilder) {\n return new IndirBuilder({ ...start.rules });\n }\n return <IndirBuilder<Context, Names, RuleDefs>> <unknown> new IndirBuilder(listToIndirectionMap(start));\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names, RuleDefs> {\n return <IndirBuilder<NewContext, Names, RuleDefs>> <unknown> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: any }>():\n IndirBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n IndirDef<Context, Key, Patch[Key][0], Patch[Key][1]>\n ) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }> {\n return <IndirBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n RuleDefs[Key] extends IndirDef<Context, Key, any, infer Par> ? IndirDef<Context, Key, Patch[Key], Par> : never\n ) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }>> <unknown> this;\n }\n\n /**\n * Change the implementation of an existing indirection.\n */\n public patchRule<U extends Names, RET, ARGS extends any[]>(patch: IndirDef<Context, U, RET, ARGS>):\n IndirBuilder<Context, Names, {[Key in Names]: Key extends U ?\n IndirDef<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <IndirBuilder<Context, Names, {[Key in Names]: Key extends U ?\n IndirDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add an indirection function. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: IndirDef<Context, U, RET, ARGS>):\n IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, IndirDef<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Function ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS extends any[]>(\n rule: CheckOverlap<U, Names, IndirDef<Context, U, RET, ARGS>>,\n ): IndirBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n IndirDef<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly IndirDef<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): IndirBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseIndirsToObject<typeof rules> ? (\n ParseIndirsToObject<typeof rules>[K] extends IndirDef<Context, K> ? ParseIndirsToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToIndirectionMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n IndirBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <IndirBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n public build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs> {\n return <IndirectObjFromIndirDefs<Context, Names, RuleDefs>> new DynamicIndirect(this.rules);\n }\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import type { IndirDefArg, IndirectionMap } from './helpers';
2
+ export declare class DynamicIndirect<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {
3
+ protected rules: RuleDefs;
4
+ protected __context: Context | undefined;
5
+ constructor(rules: RuleDefs);
6
+ setContext(context: Context): void;
7
+ protected getSafeContext(): Context;
8
+ protected readonly subrule: IndirDefArg['SUBRULE'];
9
+ }
@@ -0,0 +1,28 @@
1
+ export class DynamicIndirect {
2
+ constructor(rules) {
3
+ this.rules = rules;
4
+ this.__context = undefined;
5
+ this.subrule = (cstDef, ...args) => {
6
+ const def = this.rules[cstDef.name];
7
+ if (!def) {
8
+ throw new Error(`Rule ${cstDef.name} not found`);
9
+ }
10
+ return def.fun({
11
+ SUBRULE: this.subrule,
12
+ })(this.getSafeContext(), ...args);
13
+ };
14
+ for (const rule of Object.values(rules)) {
15
+ this[rule.name] = ((context, ...args) => {
16
+ this.setContext(context);
17
+ return this.subrule(rule, ...args);
18
+ });
19
+ }
20
+ }
21
+ setContext(context) {
22
+ this.__context = context;
23
+ }
24
+ getSafeContext() {
25
+ return this.__context;
26
+ }
27
+ }
28
+ //# sourceMappingURL=dynamicIndirected.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamicIndirected.js","sourceRoot":"","sources":["dynamicIndirected.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,eAAe;IAG1B,YAA6B,KAAe;QAAf,UAAK,GAAL,KAAK,CAAU;QAFlC,cAAS,GAAwB,SAAS,CAAC;QAmBlC,YAAO,GAA2B,CAAC,MAAM,EAAE,GAAG,IAAI,EAAE,EAAE;YACvE,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAmB,MAAM,CAAC,IAAI,CAAC,CAAC;YACtD,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,MAAM,IAAI,KAAK,CAAC,QAAQ,MAAM,CAAC,IAAI,YAAY,CAAC,CAAC;YACnD,CAAC;YAED,OAAa,GAAG,CAAC,GAAG,CAAC;gBACnB,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;QACrC,CAAC,CAAC;QAzBA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAoC,KAAK,CAAC,EAAE,CAAC;YAC3E,IAAI,CAAsB,IAAI,CAAC,IAAI,CAAC,GAAS,CAAC,CAAC,OAAgB,EAAE,GAAG,IAAS,EAAO,EAAE;gBACpF,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;gBACzB,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;YACrC,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAEM,UAAU,CAAC,OAAgB;QAChC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAES,cAAc;QACtB,OAAiB,IAAI,CAAC,SAAS,CAAC;IAClC,CAAC;CAYF","sourcesContent":["import type { IndirDef, IndirDefArg, IndirectionMap } from './helpers';\n\nexport class DynamicIndirect<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> {\n protected __context: Context | undefined = undefined;\n\n public constructor(protected rules: RuleDefs) {\n for (const rule of Object.values(<Record<string, IndirDef<Context>>>rules)) {\n this[<keyof (typeof this)>rule.name] = <any> ((context: Context, ...args: any): any => {\n this.setContext(context);\n return this.subrule(rule, ...args);\n });\n }\n }\n\n public setContext(context: Context): void {\n this.__context = context;\n }\n\n protected getSafeContext(): Context {\n return <Context> this.__context;\n }\n\n protected readonly subrule: IndirDefArg['SUBRULE'] = (cstDef, ...args) => {\n const def = this.rules[<Names> <unknown> cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n\n return <any> def.fun({\n SUBRULE: this.subrule,\n })(this.getSafeContext(), ...args);\n };\n}\n"]}
@@ -0,0 +1,24 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
2
+ export type IndirDef<Context = any, NameType extends string = string, ReturnType = unknown, ParamType extends any[] = any[]> = {
3
+ name: NameType;
4
+ fun: (def: IndirDefArg) => (c: Context, ...params: ParamType) => ReturnType;
5
+ };
6
+ export type IndirDefArg = {
7
+ SUBRULE: <T, U extends any[]>(cstDef: IndirDef<any, any, T, U>, ...arg: U) => T;
8
+ };
9
+ export type IndirectionMap<RuleNames extends string> = {
10
+ [Key in RuleNames]: IndirDef<any, Key>;
11
+ };
12
+ /**
13
+ * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
14
+ */
15
+ export declare function listToIndirectionMap<T extends readonly IndirDef[]>(rules: T): ParseIndirsToObject<T>;
16
+ /**
17
+ * Convert a list of IndirDefs to a Record with the name of the IndirDef as the key, matching the IndirectionMap type.
18
+ */
19
+ export type ParseIndirsToObject<T extends readonly IndirDef[], Names extends string = ParseNamesFromList<T>, Agg extends Record<string, IndirDef> = Record<never, never>> = T extends readonly [infer First, ...infer Rest] ? (First extends IndirDef ? (Rest extends readonly IndirDef[] ? (ParseIndirsToObject<Rest, Names, {
20
+ [K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K];
21
+ }>) : never) : never) : IndirectionMap<Names> & Agg;
22
+ export type IndirectObjFromIndirDefs<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> = {
23
+ [K in Names]: RuleDefs[K] extends IndirDef<Context, K, infer RET, infer ARGS> ? (context: Context, ...args: ARGS) => RET : never;
24
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
3
+ */
4
+ export function listToIndirectionMap(rules) {
5
+ const newRules = {};
6
+ for (const rule of rules) {
7
+ newRules[rule.name] = rule;
8
+ }
9
+ return newRules;
10
+ }
11
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["helpers.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAgC,KAAQ;IAC1E,MAAM,QAAQ,GAA6B,EAAE,CAAC;IAC9C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAA+B,QAAQ,CAAC;AAC1C,CAAC","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes';\n\nexport type IndirDef<\n Context = any,\n NameType extends string = string,\n ReturnType = unknown,\n ParamType extends any[] = any[],\n> = {\n name: NameType;\n fun: (def: IndirDefArg) => (c: Context, ...params: ParamType) => ReturnType;\n};\n\nexport type IndirDefArg = {\n SUBRULE: <T, U extends any[]>(cstDef: IndirDef<any, any, T, U>, ...arg: U) => T;\n};\n\nexport type IndirectionMap<RuleNames extends string> = {[Key in RuleNames]: IndirDef<any, Key> };\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nexport function listToIndirectionMap<T extends readonly IndirDef[]>(rules: T): ParseIndirsToObject<T> {\n const newRules: Record<string, IndirDef> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseIndirsToObject<T>>newRules;\n}\n\n/**\n * Convert a list of IndirDefs to a Record with the name of the IndirDef as the key, matching the IndirectionMap type.\n */\nexport type ParseIndirsToObject<\n T extends readonly IndirDef[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, IndirDef> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends IndirDef ? (\n Rest extends readonly IndirDef[] ? (\n ParseIndirsToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : IndirectionMap<Names> & Agg;\n\nexport type IndirectObjFromIndirDefs<Context, Names extends string, RuleDefs extends IndirectionMap<Names>> = {\n [K in Names]: RuleDefs[K] extends IndirDef<Context, K, infer RET, infer ARGS> ?\n (context: Context, ...args: ARGS) => RET : never\n};\n"]}
@@ -3,7 +3,9 @@ import type { ParserRule } from './ruleDefTypes';
3
3
  /**
4
4
  * Get union-type of names used in list of ruledefs.
5
5
  */
6
- export type ParseNamesFromList<T extends readonly ParserRule[]> = T[number]['name'];
6
+ export type ParseNamesFromList<T extends readonly {
7
+ name: string;
8
+ }[]> = T[number]['name'];
7
9
  /**
8
10
  * Convert a list of ruledefs to a record that maps each rule name to its definition.
9
11
  */
@@ -1 +1 @@
1
- {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParserMethod } from 'chevrotain';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type ParseNamesFromList<T extends readonly ParserRule[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type ParseRuleMap<RuleNames extends string> = {[Key in RuleNames]: ParserRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type ParseRulesToObject<\n T extends readonly ParserRule[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, ParserRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends ParserRule ? (\n Rest extends readonly ParserRule[] ? (\n ParseRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : ParseRuleMap<Names> & Agg;\n\nexport type ParserFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n (input: string, context: Context, args: ARGS) => RET : never\n};\n\nexport type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n ParserMethod<[Context, ARGS], RET> : never\n};\n"]}
1
+ {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParserMethod } from 'chevrotain';\nimport type { ParserRule } from './ruleDefTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type ParseNamesFromList<T extends readonly { name: string }[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type ParseRuleMap<RuleNames extends string> = {[Key in RuleNames]: ParserRule<any, Key> };\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type ParseRulesToObject<\n T extends readonly ParserRule[],\n Names extends string = ParseNamesFromList<T>,\n Agg extends Record<string, ParserRule> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends ParserRule ? (\n Rest extends readonly ParserRule[] ? (\n ParseRulesToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : ParseRuleMap<Names> & Agg;\n\nexport type ParserFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n (input: string, context: Context, args: ARGS) => RET : never\n};\n\nexport type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> = {\n [K in Names]: RuleDefs[K] extends ParserRule<Context, K, infer RET, infer ARGS> ?\n ParserMethod<[Context, ARGS], RET> : never\n};\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@traqula/core",
3
3
  "type": "module",
4
- "version": "0.0.1-alpha.9+427f381",
4
+ "version": "0.0.2",
5
5
  "description": "Core components of Traqula",
6
6
  "lsd:module": true,
7
7
  "license": "MIT",
@@ -15,8 +15,11 @@
15
15
  },
16
16
  "sideEffects": false,
17
17
  "exports": {
18
- "import": "./lib/index.js",
19
- "require": "./lib/index.cjs"
18
+ "./package.json": "./package.json",
19
+ ".": {
20
+ "import": "./lib/index.js",
21
+ "require": "./lib/index.cjs"
22
+ }
20
23
  },
21
24
  "main": "lib/index.js",
22
25
  "publishConfig": {
@@ -43,5 +46,5 @@
43
46
  "devDependencies": {
44
47
  "@chevrotain/types": "^11.0.3"
45
48
  },
46
- "gitHead": "427f3819138dd557621cae40c761213573e60017"
49
+ "gitHead": "8d4d4a6b155e14db6e1f87a0b855ade7515c4e35"
47
50
  }
@@ -1,14 +0,0 @@
1
- /**
2
- * Starting inclusive, ending exclusive
3
- */
4
- export type Range<A extends number = number, B extends number = number> = [A, B];
5
- export declare class RangeArithmetic {
6
- ranges: Range[];
7
- private readonly initRange;
8
- constructor(start: number, end: number);
9
- private static validate;
10
- private static substractRange;
11
- subtract(...range: Range): this;
12
- negate(): this;
13
- projection(...range: Range): Range[];
14
- }
@@ -1,71 +0,0 @@
1
- export class RangeArithmetic {
2
- constructor(start, end) {
3
- this.ranges = [];
4
- this.initRange = RangeArithmetic.validate(start, end);
5
- this.ranges.push(this.initRange);
6
- }
7
- static validate(...range) {
8
- const [start, end] = range;
9
- if (start >= end) {
10
- throw new Error('Invalid range');
11
- }
12
- return range;
13
- }
14
- static substractRange(included, ...range) {
15
- const [sMinus, eMinus] = RangeArithmetic.validate(...range);
16
- return included.flatMap(([sCur, eCur]) => {
17
- // Split in half
18
- if (sCur < sMinus && eMinus < eCur) {
19
- return [[sCur, sMinus], [eMinus, eCur]];
20
- }
21
- if (sMinus <= sCur && sCur < eMinus && eMinus < eCur) {
22
- return [[eMinus, eCur]];
23
- }
24
- if (sCur < sMinus && sMinus < eCur && eCur <= eMinus) {
25
- return [[sCur, sMinus]];
26
- }
27
- if (sMinus <= sCur && eCur <= eMinus) {
28
- return [];
29
- }
30
- return [[sCur, eCur]];
31
- });
32
- }
33
- subtract(...range) {
34
- this.ranges = RangeArithmetic.substractRange(this.ranges, ...range);
35
- return this;
36
- }
37
- negate() {
38
- // Can be optimized
39
- let iter = [this.initRange];
40
- for (const range of this.ranges) {
41
- iter = RangeArithmetic.substractRange(iter, ...range);
42
- }
43
- this.ranges = iter;
44
- return this;
45
- }
46
- projection(...range) {
47
- const [sProj, eProj] = range;
48
- if (sProj >= eProj) {
49
- return [];
50
- }
51
- return this.ranges.flatMap(([sCur, eCur]) => {
52
- // If projection is inside the range
53
- if (sCur < sProj && eProj < eCur) {
54
- return [[sProj, eProj]];
55
- }
56
- // Projection wraps around
57
- if (sProj <= sCur && eCur <= eProj) {
58
- return [[sCur, eCur]];
59
- }
60
- // Matches left side
61
- if (sProj <= sCur && sCur < eProj && eProj < eCur) {
62
- return [[sCur, eProj]];
63
- }
64
- if (sCur < sProj && sProj < eCur && eCur < eProj) {
65
- return [[sProj, eCur]];
66
- }
67
- return [];
68
- });
69
- }
70
- }
71
- //# sourceMappingURL=RangeArithmetic.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RangeArithmetic.js","sourceRoot":"","sources":["RangeArithmetic.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,eAAe;IAG1B,YAAmB,KAAa,EAAE,GAAW;QAFtC,WAAM,GAAY,EAAE,CAAC;QAG1B,IAAI,CAAC,SAAS,GAAG,eAAe,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACtD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAEO,MAAM,CAAC,QAAQ,CAAC,GAAG,KAAY;QACrC,MAAM,CAAE,KAAK,EAAE,GAAG,CAAE,GAAG,KAAK,CAAC;QAC7B,IAAI,KAAK,IAAI,GAAG,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAEO,MAAM,CAAC,cAAc,CAAC,QAAiB,EAAE,GAAG,KAAY;QAC9D,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,GAAG,eAAe,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,CAAC;QAE9D,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAE,IAAI,EAAE,IAAI,CAAE,EAAE,EAAE;YACzC,gBAAgB;YAChB,IAAI,IAAI,GAAG,MAAM,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;gBACnC,OAAO,CAAC,CAAE,IAAI,EAAE,MAAM,CAAE,EAAE,CAAE,MAAM,EAAE,IAAI,CAAE,CAAC,CAAC;YAC9C,CAAC;YACD,IAAI,MAAM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,IAAI,CAAE,EAAE,EAAE;YAC5C,oCAAoC;YACpC,IAAI,IAAI,GAAG,KAAK,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;gBACjC,OAAO,CAAC,CAAE,KAAK,EAAE,KAAK,CAAE,CAAC,CAAC;YAC5B,CAAC;YACD,0BAA0B;YAC1B,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;gBACnC,OAAO,CAAC,CAAE,IAAI,EAAE,IAAI,CAAE,CAAC,CAAC;YAC1B,CAAC;YACD,oBAAoB;YACpB,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,GAAG,KAAK,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;gBAClD,OAAO,CAAC,CAAE,IAAI,EAAE,KAAK,CAAE,CAAC,CAAC;YAC3B,CAAC;YACD,IAAI,IAAI,GAAG,KAAK,IAAI,KAAK,GAAG,IAAI,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;gBACjD,OAAO,CAAC,CAAE,KAAK,EAAE,IAAI,CAAE,CAAC,CAAC;YAC3B,CAAC;YACD,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["/**\n * Starting inclusive, ending exclusive\n */\nexport type Range<A extends number = number, B extends number = number> = [A, B];\n\nexport class RangeArithmetic {\n public ranges: Range[] = [];\n private readonly initRange: Range;\n public constructor(start: number, end: number) {\n this.initRange = RangeArithmetic.validate(start, end);\n this.ranges.push(this.initRange);\n }\n\n private static validate(...range: Range): Range {\n const [ start, end ] = range;\n if (start >= end) {\n throw new Error('Invalid range');\n }\n return range;\n }\n\n private static substractRange(included: Range[], ...range: Range): Range[] {\n const [ sMinus, eMinus ] = RangeArithmetic.validate(...range);\n\n return included.flatMap(([ sCur, eCur ]) => {\n // Split in half\n if (sCur < sMinus && eMinus < eCur) {\n return [[ sCur, sMinus ], [ eMinus, eCur ]];\n }\n if (sMinus <= sCur && sCur < eMinus && eMinus < eCur) {\n return [[ eMinus, eCur ]];\n }\n if (sCur < sMinus && sMinus < eCur && eCur <= eMinus) {\n return [[ sCur, sMinus ]];\n }\n if (sMinus <= sCur && eCur <= eMinus) {\n return [];\n }\n return [[ sCur, eCur ]];\n });\n }\n\n public subtract(...range: Range): this {\n this.ranges = RangeArithmetic.substractRange(this.ranges, ...range);\n return this;\n }\n\n public negate(): this {\n // Can be optimized\n let iter = [ this.initRange ];\n for (const range of this.ranges) {\n iter = RangeArithmetic.substractRange(iter, ...range);\n }\n this.ranges = iter;\n return this;\n }\n\n public projection(...range: Range): Range[] {\n const [ sProj, eProj ] = range;\n if (sProj >= eProj) {\n return [];\n }\n return this.ranges.flatMap(([ sCur, eCur ]) => {\n // If projection is inside the range\n if (sCur < sProj && eProj < eCur) {\n return [[ sProj, eProj ]];\n }\n // Projection wraps around\n if (sProj <= sCur && eCur <= eProj) {\n return [[ sCur, eCur ]];\n }\n // Matches left side\n if (sProj <= sCur && sCur < eProj && eProj < eCur) {\n return [[ sCur, eProj ]];\n }\n if (sCur < sProj && sProj < eCur && eCur < eProj) {\n return [[ sProj, eCur ]];\n }\n return [];\n });\n }\n}\n"]}