@traqula/core 0.0.18 → 0.0.20
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.
- package/lib/AstCoreFactory.js +1 -1
- package/lib/AstCoreFactory.js.map +1 -1
- package/lib/TransformerObject.d.ts +64 -0
- package/lib/TransformerObject.js +204 -0
- package/lib/TransformerObject.js.map +1 -0
- package/lib/TransformerSubTyped.d.ts +94 -0
- package/lib/TransformerSubTyped.js +116 -0
- package/lib/TransformerSubTyped.js.map +1 -0
- package/lib/TransformerTyped.d.ts +62 -0
- package/lib/TransformerTyped.js +95 -0
- package/lib/TransformerTyped.js.map +1 -0
- package/lib/generator-builder/dynamicGenerator.d.ts +13 -3
- package/lib/generator-builder/dynamicGenerator.js +90 -60
- package/lib/generator-builder/dynamicGenerator.js.map +1 -1
- package/lib/generator-builder/generatorBuilder.d.ts +5 -2
- package/lib/generator-builder/generatorBuilder.js +6 -3
- package/lib/generator-builder/generatorBuilder.js.map +1 -1
- package/lib/generator-builder/generatorTypes.d.ts +35 -21
- package/lib/generator-builder/generatorTypes.js.map +1 -1
- package/lib/index.cjs +346 -138
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/indirection-builder/IndirBuilder.d.ts +4 -1
- package/lib/indirection-builder/IndirBuilder.js +6 -3
- package/lib/indirection-builder/IndirBuilder.js.map +1 -1
- package/lib/parser-builder/parserBuilder.d.ts +12 -8
- package/lib/parser-builder/parserBuilder.js +6 -3
- package/lib/parser-builder/parserBuilder.js.map +1 -1
- package/lib/utils.d.ts +4 -0
- package/lib/utils.js +4 -0
- package/lib/utils.js.map +1 -1
- package/package.json +2 -2
- package/lib/Transformers.d.ts +0 -129
- package/lib/Transformers.js +0 -226
- package/lib/Transformers.js.map +0 -1
package/lib/index.d.ts
CHANGED
|
@@ -10,4 +10,6 @@ export * from './parser-builder/ruleDefTypes.js';
|
|
|
10
10
|
export * from './utils.js';
|
|
11
11
|
export * from './AstCoreFactory.js';
|
|
12
12
|
export * from './nodeTypings.js';
|
|
13
|
-
export
|
|
13
|
+
export { VisitContext, TransformContext, TransformerObject, SelectiveTraversalContext } from './TransformerObject.js';
|
|
14
|
+
export { TransformerTyped, Safeness, SafeWrap } from './TransformerTyped.js';
|
|
15
|
+
export { TransformerSubTyped } from './TransformerSubTyped.js';
|
package/lib/index.js
CHANGED
|
@@ -15,5 +15,7 @@ export * from './parser-builder/ruleDefTypes.js';
|
|
|
15
15
|
export * from './utils.js';
|
|
16
16
|
export * from './AstCoreFactory.js';
|
|
17
17
|
export * from './nodeTypings.js';
|
|
18
|
-
export
|
|
18
|
+
export { TransformerObject } from './TransformerObject.js';
|
|
19
|
+
export { TransformerTyped } from './TransformerTyped.js';
|
|
20
|
+
export { TransformerSubTyped } from './TransformerSubTyped.js';
|
|
19
21
|
//# 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,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,uCAAuC,CAAC;AACtD,6BAA6B;AAC7B,cAAc,kCAAkC,CAAC;AACjD,cAAc,uCAAuC,CAAC;AACtD,uBAAuB;AACvB,cAAc,iCAAiC,CAAC;AAChD,wBAAwB;AACxB,cAAc,kCAAkC,CAAC;AACjD,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,uBAAuB;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,uCAAuC,CAAC;AACtD,6BAA6B;AAC7B,cAAc,kCAAkC,CAAC;AACjD,cAAc,uCAAuC,CAAC;AACtD,uBAAuB;AACvB,cAAc,iCAAiC,CAAC;AAChD,wBAAwB;AACxB,cAAc,kCAAkC,CAAC;AACjD,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,uBAAuB;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAkC,iBAAiB,EAA6B,MAAM,wBAAwB,CAAC;AACtH,OAAO,EAAE,gBAAgB,EAAsB,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC","sourcesContent":["// Export generator builder\nexport * from './generator-builder/builderTypes.js';\nexport * from './generator-builder/generatorBuilder.js';\nexport * from './generator-builder/generatorTypes.js';\n// Export Indirection Builder\nexport * from './indirection-builder/helpers.js';\nexport * from './indirection-builder/IndirBuilder.js';\n// Export lexer builder\nexport * from './lexer-builder/LexerBuilder.js';\n// Export parser builder\nexport * from './parser-builder/builderTypes.js';\nexport * from './parser-builder/parserBuilder.js';\nexport * from './parser-builder/ruleDefTypes.js';\n// Export general types\nexport * from './utils.js';\nexport * from './AstCoreFactory.js';\nexport * from './nodeTypings.js';\nexport { VisitContext, TransformContext, TransformerObject, SelectiveTraversalContext } from './TransformerObject.js';\nexport { TransformerTyped, Safeness, SafeWrap } from './TransformerTyped.js';\nexport { TransformerSubTyped } from './TransformerSubTyped.js';\n"]}
|
|
@@ -6,7 +6,9 @@ export declare class IndirBuilder<Context, Names extends string, RuleDefs extend
|
|
|
6
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
7
|
private rules;
|
|
8
8
|
private constructor();
|
|
9
|
-
widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names,
|
|
9
|
+
widenContext<NewContext extends Context>(): IndirBuilder<NewContext, Names, {
|
|
10
|
+
[Key in keyof RuleDefs]: Key extends Names ? (RuleDefs[Key] extends IndirDef<any, any, infer RT, infer PT> ? IndirDef<NewContext, Key, RT, PT> : never) : never;
|
|
11
|
+
}>;
|
|
10
12
|
typePatch<Patch extends {
|
|
11
13
|
[Key in Names]?: [any] | [any, any[]];
|
|
12
14
|
}>(): IndirBuilder<Context, Names, {
|
|
@@ -39,5 +41,6 @@ export declare class IndirBuilder<Context, Names extends string, RuleDefs extend
|
|
|
39
41
|
deleteRule<U extends Names>(ruleName: U): IndirBuilder<Context, Exclude<Names, U>, {
|
|
40
42
|
[K in Exclude<Names, U>]: RuleDefs[K] extends IndirDef<Context, K> ? RuleDefs[K] : never;
|
|
41
43
|
}>;
|
|
44
|
+
getRule<U extends Names>(ruleName: U): RuleDefs[U] extends IndirDef<any, U, infer RT, infer PT> ? IndirDef<Context, U, RT, PT> : never;
|
|
42
45
|
build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs>;
|
|
43
46
|
}
|
|
@@ -2,10 +2,10 @@ import { DynamicIndirect } from './dynamicIndirected.js';
|
|
|
2
2
|
import { listToIndirectionMap } from './helpers.js';
|
|
3
3
|
export class IndirBuilder {
|
|
4
4
|
static create(start) {
|
|
5
|
-
if (start
|
|
6
|
-
return new IndirBuilder(
|
|
5
|
+
if (Array.isArray(start)) {
|
|
6
|
+
return new IndirBuilder(listToIndirectionMap(start));
|
|
7
7
|
}
|
|
8
|
-
return new IndirBuilder(
|
|
8
|
+
return new IndirBuilder({ ...start.rules });
|
|
9
9
|
}
|
|
10
10
|
rules;
|
|
11
11
|
constructor(startRules) {
|
|
@@ -54,6 +54,9 @@ export class IndirBuilder {
|
|
|
54
54
|
delete this.rules[ruleName];
|
|
55
55
|
return this;
|
|
56
56
|
}
|
|
57
|
+
getRule(ruleName) {
|
|
58
|
+
return this.rules[ruleName];
|
|
59
|
+
}
|
|
57
60
|
build() {
|
|
58
61
|
return new DynamicIndirect(this.rules);
|
|
59
62
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IndirBuilder.js","sourceRoot":"","sources":["IndirBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,OAAO,YAAY;IAUhB,MAAM,CAAC,MAAM,CAMlB,KAAqD;QAErD,IAAI,KAAK,
|
|
1
|
+
{"version":3,"file":"IndirBuilder.js","sourceRoot":"","sources":["IndirBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,OAAO,YAAY;IAUhB,MAAM,CAAC,MAAM,CAMlB,KAAqD;QAErD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,OAA0D,IAAI,YAAY,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1G,CAAC;QACD,OAAO,IAAI,YAAY,CAAC,EAAE,GAAiC,KAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IAC7E,CAAC;IAEO,KAAK,CAAW;IAExB,YAAoB,UAAoB;QACtC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;IAC1B,CAAC;IAEM,YAAY;QAOjB,OAAa,IAAI,CAAC;IACpB,CAAC;IAEM,SAAS;QASd,OAAa,IAAI,CAAC;IACpB,CAAC;IAED;;OAEG;IACI,SAAS,CAA2C,KAAsC;QAK/F,MAAM,IAAI,GAEE,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,GAAS,KAAK,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,gBAAgB,CAA4C,IAAqC;QAKtG,MAAM,IAAI,GAGE,IAAI,CAAC;QACjB,MAAM,KAAK,GAAuC,IAAI,CAAC,KAAK,CAAC;QAC7D,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YAChE,MAAM,IAAI,KAAK,CAAC,YAAY,IAAI,CAAC,IAAI,gCAAgC,CAAC,CAAC;QACzE,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,OAAO,CACZ,IAA6D;QAI7D,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAEM,OAAO,CACZ,GAAG,KAAoD;QAYvD,IAAI,CAAC,KAAK,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,oBAAoB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/D,OAAuB,IAAI,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,UAAU,CAAkB,QAAW;QAG5C,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC5B,OAEY,IAAI,CAAC;IACnB,CAAC;IAEM,OAAO,CAAkB,QAAW;QAEzC,OAAa,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACpC,CAAC;IAEM,KAAK;QACV,OAA4D,IAAI,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC9F,CAAC;CACF","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes.js';\nimport type { CheckOverlap } from '../utils.js';\nimport { DynamicIndirect } from './dynamicIndirected.js';\nimport type { IndirDef, IndirectionMap, IndirectObjFromIndirDefs, ParseIndirsToObject } from './helpers.js';\nimport { listToIndirectionMap } from './helpers.js';\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 (Array.isArray(start)) {\n return <IndirBuilder<Context, Names, RuleDefs>> <unknown> new IndirBuilder(listToIndirectionMap(start));\n }\n return new IndirBuilder({ ...(<IndirBuilder<any, any, any>>start).rules });\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<\n NewContext,\n Names,\n {[Key in keyof RuleDefs]: Key extends Names ?\n (RuleDefs[Key] extends IndirDef<any, any, infer RT, infer PT> ? IndirDef<NewContext, Key, RT, PT> : never)\n : never }\n > {\n return <any> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: [any] | [any, any[]]}>():\n IndirBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n Patch[Key] extends [any, any[]] ? IndirDef<Context, Key, Patch[Key][0], Patch[Key][1]> : (\n // Only one - infer arg yourself\n Patch[Key] extends [ any ] ? (\n RuleDefs[Key] extends IndirDef<any, any, any, infer Par> ? IndirDef<Context, Key, Patch[Key][0], Par> : never\n ) : never\n )\n ) : (RuleDefs[Key] extends IndirDef<Context, Key> ? RuleDefs[Key] : never) }> {\n return <any> 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 getRule<U extends Names>(ruleName: U): RuleDefs[U] extends IndirDef<any, U, infer RT, infer PT> ?\n IndirDef<Context, U, RT, PT> : never {\n return <any> this.rules[ruleName];\n }\n\n public build(): IndirectObjFromIndirDefs<Context, Names, RuleDefs> {\n return <IndirectObjFromIndirDefs<Context, Names, RuleDefs>> new DynamicIndirect(this.rules);\n }\n}\n"]}
|
|
@@ -3,6 +3,13 @@ import type { TokenType } from 'chevrotain';
|
|
|
3
3
|
import type { CheckOverlap } from '../utils.js';
|
|
4
4
|
import type { ParserFromRules, ParseRuleMap, ParseRulesToObject, ParseNamesFromList } from './builderTypes.js';
|
|
5
5
|
import type { ParserRule } from './ruleDefTypes.js';
|
|
6
|
+
export interface ParserBuildArgs {
|
|
7
|
+
tokenVocabulary: readonly TokenType[];
|
|
8
|
+
parserConfig?: IParserConfig;
|
|
9
|
+
lexerConfig?: ILexerConfig;
|
|
10
|
+
queryPreProcessor?: (input: string) => string;
|
|
11
|
+
errorHandler?: (errors: IRecognitionException[]) => void;
|
|
12
|
+
}
|
|
6
13
|
/**
|
|
7
14
|
* The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).
|
|
8
15
|
* Using the builder you can create a grammar + AST creator.
|
|
@@ -17,7 +24,9 @@ export declare class ParserBuilder<Context, Names extends string, RuleDefs exten
|
|
|
17
24
|
static create<Rules extends readonly ParserRule[] = readonly ParserRule[], Context = Rules[0] extends ParserRule<infer context> ? context : never, Names extends string = ParseNamesFromList<Rules>, RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>>(start: Rules | ParserBuilder<Context, Names, RuleDefs>): ParserBuilder<Context, Names, RuleDefs>;
|
|
18
25
|
private rules;
|
|
19
26
|
private constructor();
|
|
20
|
-
widenContext<NewContext extends Context>(): ParserBuilder<NewContext, Names,
|
|
27
|
+
widenContext<NewContext extends Context>(): ParserBuilder<NewContext, Names, {
|
|
28
|
+
[Key in keyof RuleDefs]: Key extends Names ? (RuleDefs[Key] extends ParserRule<any, any, infer RT, infer PT> ? ParserRule<NewContext, Key, RT, PT> : never) : never;
|
|
29
|
+
}>;
|
|
21
30
|
typePatch<Patch extends {
|
|
22
31
|
[Key in Names]?: [any] | [any, any[]];
|
|
23
32
|
}>(): ParserBuilder<Context, Names, {
|
|
@@ -50,6 +59,7 @@ export declare class ParserBuilder<Context, Names extends string, RuleDefs exten
|
|
|
50
59
|
deleteRule<U extends Names>(ruleName: U): ParserBuilder<Context, Exclude<Names, U>, {
|
|
51
60
|
[K in Exclude<Names, U>]: RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never;
|
|
52
61
|
}>;
|
|
62
|
+
getRule<U extends Names>(ruleName: U): RuleDefs[U] extends ParserRule<any, U, infer RT, infer PT> ? ParserRule<Context, U, RT, PT> : never;
|
|
53
63
|
/**
|
|
54
64
|
* Merge this grammar builder with another.
|
|
55
65
|
* It is best to merge the bigger grammar with the smaller one.
|
|
@@ -63,12 +73,6 @@ export declare class ParserBuilder<Context, Names extends string, RuleDefs exten
|
|
|
63
73
|
[K in Names | OtherNames | ParseNamesFromList<OW>]: K extends keyof ParseRulesToObject<OW> ? (ParseRulesToObject<OW>[K] extends ParserRule<Context, K> ? ParseRulesToObject<OW>[K] : never) : (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : K extends OtherNames ? (OtherRules[K] extends ParserRule<Context, K> ? OtherRules[K] : never) : never);
|
|
64
74
|
}>;
|
|
65
75
|
private defaultErrorHandler;
|
|
66
|
-
build({ tokenVocabulary, parserConfig, lexerConfig, queryPreProcessor, errorHandler, }:
|
|
67
|
-
tokenVocabulary: readonly TokenType[];
|
|
68
|
-
parserConfig?: IParserConfig;
|
|
69
|
-
lexerConfig?: ILexerConfig;
|
|
70
|
-
queryPreProcessor?: (input: string) => string;
|
|
71
|
-
errorHandler?: (errors: IRecognitionException[]) => void;
|
|
72
|
-
}): ParserFromRules<Context, Names, RuleDefs>;
|
|
76
|
+
build({ tokenVocabulary, parserConfig, lexerConfig, queryPreProcessor, errorHandler, }: ParserBuildArgs): ParserFromRules<Context, Names, RuleDefs>;
|
|
73
77
|
private consume;
|
|
74
78
|
}
|
|
@@ -23,10 +23,10 @@ export class ParserBuilder {
|
|
|
23
23
|
* If a builder is provided, a new copy will be created.
|
|
24
24
|
*/
|
|
25
25
|
static create(start) {
|
|
26
|
-
if (start
|
|
27
|
-
return new ParserBuilder(
|
|
26
|
+
if (Array.isArray(start)) {
|
|
27
|
+
return new ParserBuilder(listToRuleDefMap(start));
|
|
28
28
|
}
|
|
29
|
-
return new ParserBuilder(
|
|
29
|
+
return new ParserBuilder({ ...start.rules });
|
|
30
30
|
}
|
|
31
31
|
rules;
|
|
32
32
|
constructor(startRules) {
|
|
@@ -75,6 +75,9 @@ export class ParserBuilder {
|
|
|
75
75
|
delete this.rules[ruleName];
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
|
+
getRule(ruleName) {
|
|
79
|
+
return this.rules[ruleName];
|
|
80
|
+
}
|
|
78
81
|
/**
|
|
79
82
|
* Merge this grammar builder with another.
|
|
80
83
|
* It is best to merge the bigger grammar with the smaller one.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parserBuilder.js","sourceRoot":"","sources":["parserBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD;;GAEG;AACH,SAAS,gBAAgB,CAAkC,KAAQ;IACjE,MAAM,QAAQ,GAA+B,EAAE,CAAC;IAChD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC7B,CAAC;IACD,OAA8B,QAAQ,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,iDAAiD;AACjD,MAAM,OAAO,aAAa;IACxB;;;OAGG;IACI,MAAM,CAAC,MAAM,CAMlB,KAAsD;QAEtD,IAAI,KAAK,YAAY,aAAa,EAAE,CAAC;YACnC,OAAO,IAAI,aAAa,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;QAC/C,CAAC;QACD,OAA2D,IAAI,aAAa,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;IACxG,CAAC;IAEO,KAAK,CAAW;IAExB,YAAoB,UAAoB;QACtC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC;IAC1B,CAAC;IAEM,YAAY;QACjB,OAA8D,IAAI,CAAC;IACrE,CAAC;IAEM,SAAS;QAUd,OAAa,IAAI,CAAC;IACpB,CAAC;IAED;;OAEG;IACI,SAAS,CAA2C,KAAwC;QAKjG,MAAM,IAAI,GAEE,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,C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K,CAAC,CAAC;gBAChD,MAAM,SAAS,GAAG,KAAK,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;gBAEjD,8BAA8B;gBAC9B,MAAM,CAAC,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC;gBAChC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;gBAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;gBACnD,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC7B,0BAA0B;oBAC1B,IAAI,YAAY,EAAE,CAAC;wBACjB,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;oBAC9B,CAAC;yBAAM,CAAC;wBACN,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;oBAC1D,CAAC;gBACH,CAAC;gBACD,OAAO,MAAM,CAAC;YAChB,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAmD,oBAAoB,CAAC;IAC1E,CAAC;IAEO,OAAO,CAAC,EAAE,eAAe,EAAE,MAAM,GAAG,EAAE,EAG7C;QAEC,OACuD,IAAI,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,eAAe,EAAE,MAAM,CAAC,CAAC;IAChH,CAAC;CACF","sourcesContent":["import type { ILexerConfig, IParserConfig, IRecognitionException } from '@chevrotain/types';\nimport type { TokenType, TokenVocabulary, EmbeddedActionsParser } from 'chevrotain';\nimport { LexerBuilder } from '../lexer-builder/LexerBuilder.js';\nimport type { CheckOverlap } from '../utils.js';\nimport type {\n ParseMethodsFromRules,\n ParserFromRules,\n ParseRuleMap,\n ParseRulesToObject,\n ParseNamesFromList,\n} from './builderTypes.js';\nimport { DynamicParser } from './dynamicParser.js';\nimport type { ParserRule } from './ruleDefTypes.js';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly ParserRule[]>(rules: T): ParseRulesToObject<T> {\n const newRules: Record<string, ParserRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseRulesToObject<T>>newRules;\n}\n\n/**\n * The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).\n * Using the builder you can create a grammar + AST creator.\n * At any point in time, a parser can be constructed from the added rules.\n * Constructing a parser will cause a validation which will validate the correctness of the grammar.\n */\n// This code is wild so other code can be simple.\nexport class ParserBuilder<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> {\n /**\n * Create a builder from some initial grammar rules or an existing builder.\n * If a builder is provided, a new copy will be created.\n */\n public static create<\n Rules extends readonly ParserRule[] = readonly ParserRule[],\n Context = Rules[0] extends ParserRule<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>,\n >(\n start: Rules | ParserBuilder<Context, Names, RuleDefs>,\n ): ParserBuilder<Context, Names, RuleDefs> {\n if (start instanceof ParserBuilder) {\n return new ParserBuilder({ ...start.rules });\n }\n return <ParserBuilder<Context, Names, RuleDefs>> <unknown> new ParserBuilder(listToRuleDefMap(start));\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public widenContext<NewContext extends Context>(): ParserBuilder<NewContext, Names, RuleDefs> {\n return <ParserBuilder<NewContext, Names, RuleDefs>> <unknown> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: [any] | [any, any[]]}>():\n ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n Patch[Key] extends [any, any[]] ? ParserRule<Context, Key, Patch[Key][0], Patch[Key][1]> : (\n // Only one - infer yourself\n Patch[Key] extends [any] ? (\n RuleDefs[Key] extends ParserRule<any, any, any, infer Par> ?\n ParserRule<Context, Key, Patch[Key][0], Par> : never\n ) : never\n )\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <any> this;\n }\n\n /**\n * Change the implementation of an existing grammar rule.\n */\n public patchRule<U extends Names, RET, ARGS extends any[]>(patch: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, ParserRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS extends any[]>(\n rule: CheckOverlap<U, Names, ParserRule<Context, U, RET, ARGS>>,\n ): ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly ParserRule<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): ParserBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseRulesToObject<typeof rules> ? (\n ParseRulesToObject<typeof rules>[K] extends ParserRule<Context, K> ? ParseRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToRuleDefMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n /**\n * Merge this grammar builder with another.\n * It is best to merge the bigger grammar with the smaller one.\n * If the two builders both have a grammar rule with the same name,\n * no error will be thrown case they map to the same ruledef object.\n * If they map to a different object, an error will be thrown.\n * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,\n * this rule implementation will be used.\n */\n public merge<\n OtherNames extends string,\n OtherRules extends ParseRuleMap<OtherNames>,\n OW extends readonly ParserRule<Context>[],\n >(\n builder: ParserBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n ParserBuilder<\n Context,\n Names | OtherNames | ParseNamesFromList<OW>,\n {[K in Names | OtherNames | ParseNamesFromList<OW>]:\n K extends keyof ParseRulesToObject<OW> ? (\n ParseRulesToObject<OW>[K] extends ParserRule<Context, K> ? ParseRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends ParserRule<Context, K> ? OtherRules[K] : never) : never\n ) }\n > {\n // Assume the other grammar is bigger than yours. So start from that one and add this one\n const otherRules: Record<string, ParserRule<Context>> = { ...builder.rules };\n const myRules: Record<string, ParserRule<Context>> = this.rules;\n\n for (const rule of Object.values(myRules)) {\n if (otherRules[rule.name] === undefined) {\n otherRules[rule.name] = rule;\n } else {\n const existingRule = otherRules[rule.name];\n // If same rule, no issue, move on. Else\n if (existingRule !== rule) {\n const override = overridingRules.find(x => x.name === rule.name);\n // If override specified, take override, else, inform user that there is a conflict\n if (override) {\n otherRules[rule.name] = override;\n } else {\n throw new Error(`Rule with name \"${rule.name}\" already exists in the builder, specify an override to resolve conflict`);\n }\n }\n }\n }\n\n this.rules = <any> <unknown> otherRules;\n return <any> <unknown> this;\n }\n\n private defaultErrorHandler(input: string, errors: IRecognitionException[]): void {\n const firstError = errors[0];\n const messageBuilder: string[] = [ 'Parse error' ];\n const lineIdx = firstError.token.startLine;\n if (lineIdx !== undefined && !Number.isNaN(lineIdx)) {\n const errorLine = input.split('\\n')[lineIdx - 1];\n messageBuilder.push(` on line ${lineIdx}\n${errorLine}`);\n const columnIdx = firstError.token.startColumn;\n if (columnIdx !== undefined) {\n messageBuilder.push(`\\n${'-'.repeat(columnIdx - 1)}^`);\n }\n }\n messageBuilder.push(`\\n${firstError.message}`);\n throw new Error(messageBuilder.join(''));\n }\n\n public build({\n tokenVocabulary,\n parserConfig = {},\n lexerConfig = {},\n queryPreProcessor = s => s,\n errorHandler,\n }: {\n tokenVocabulary: readonly TokenType[];\n parserConfig?: IParserConfig;\n lexerConfig?: ILexerConfig;\n queryPreProcessor?: (input: string) => string;\n errorHandler?: (errors: IRecognitionException[]) => void;\n }): ParserFromRules<Context, Names, RuleDefs> {\n const lexer = LexerBuilder.create().add(...tokenVocabulary).build({\n positionTracking: 'full',\n recoveryEnabled: false,\n ensureOptimizations: true,\n safeMode: false,\n skipValidations: true,\n ...lexerConfig,\n });\n // Get the chevrotain parser\n const parser = this.consume({\n tokenVocabulary: <TokenType[]> tokenVocabulary,\n config: parserConfig,\n });\n // Start building a parser that does not pass input using a state, but instead gets it as a function argument.\n const selfSufficientParser: Partial<ParserFromRules<Context, Names, RuleDefs>> = {};\n\n // To do that, we need to create a wrapper for each parser rule.\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <ParserRule<Context, Names>[]> Object.values(this.rules)) {\n selfSufficientParser[rule.name] = <any> ((input: string, context: Context, ...args: unknown[]) => {\n const processedInput = queryPreProcessor(input);\n const lexResult = lexer.tokenize(processedInput);\n\n // This also resets the parser\n parser.input = lexResult.tokens;\n parser.setContext(context);\n const result = parser[rule.name](context, ...args);\n if (parser.errors.length > 0) {\n // Console.log(lexResult);\n if (errorHandler) {\n errorHandler(parser.errors);\n } else {\n this.defaultErrorHandler(processedInput, parser.errors);\n }\n }\n return result;\n });\n }\n return <ParserFromRules<Context, Names, RuleDefs>> selfSufficientParser;\n }\n\n private consume({ tokenVocabulary, config = {}}: {\n tokenVocabulary: TokenVocabulary;\n config?: IParserConfig;\n }): EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void } {\n return <EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void }><unknown> new DynamicParser(this.rules, tokenVocabulary, config);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"parserBuilder.js","sourceRoot":"","sources":["parserBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD;;GAEG;AACH,SAAS,gBAAgB,CAAkC,KAAQ;IACjE,MAAM,QAAQ,GAA+B,EAAE,CAAC;IAChD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAA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AAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;oBAC9B,CAAC;yBAAM,CAAC;wBACN,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;oBAC1D,CAAC;gBACH,CAAC;gBACD,OAAO,MAAM,CAAC;YAChB,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAmD,oBAAoB,CAAC;IAC1E,CAAC;IAEO,OAAO,CAAC,EAAE,eAAe,EAAE,MAAM,GAAG,EAAE,EAG7C;QAEC,OACuD,IAAI,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,eAAe,EAAE,MAAM,CAAC,CAAC;IAChH,CAAC;CACF","sourcesContent":["import type { ILexerConfig, IParserConfig, IRecognitionException } from '@chevrotain/types';\nimport type { TokenType, TokenVocabulary, EmbeddedActionsParser } from 'chevrotain';\nimport { LexerBuilder } from '../lexer-builder/LexerBuilder.js';\nimport type { CheckOverlap } from '../utils.js';\nimport type {\n ParseMethodsFromRules,\n ParserFromRules,\n ParseRuleMap,\n ParseRulesToObject,\n ParseNamesFromList,\n} from './builderTypes.js';\nimport { DynamicParser } from './dynamicParser.js';\nimport type { ParserRule } from './ruleDefTypes.js';\n\n/**\n * Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.\n */\nfunction listToRuleDefMap<T extends readonly ParserRule[]>(rules: T): ParseRulesToObject<T> {\n const newRules: Record<string, ParserRule> = {};\n for (const rule of rules) {\n newRules[rule.name] = rule;\n }\n return <ParseRulesToObject<T>>newRules;\n}\n\nexport interface ParserBuildArgs {\n tokenVocabulary: readonly TokenType[];\n parserConfig?: IParserConfig;\n lexerConfig?: ILexerConfig;\n queryPreProcessor?: (input: string) => string;\n errorHandler?: (errors: IRecognitionException[]) => void;\n}\n\n/**\n * The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).\n * Using the builder you can create a grammar + AST creator.\n * At any point in time, a parser can be constructed from the added rules.\n * Constructing a parser will cause a validation which will validate the correctness of the grammar.\n */\n// This code is wild so other code can be simple.\nexport class ParserBuilder<Context, Names extends string, RuleDefs extends ParseRuleMap<Names>> {\n /**\n * Create a builder from some initial grammar rules or an existing builder.\n * If a builder is provided, a new copy will be created.\n */\n public static create<\n Rules extends readonly ParserRule[] = readonly ParserRule[],\n Context = Rules[0] extends ParserRule<infer context> ? context : never,\n Names extends string = ParseNamesFromList<Rules>,\n RuleDefs extends ParseRuleMap<Names> = ParseRulesToObject<Rules>,\n >(\n start: Rules | ParserBuilder<Context, Names, RuleDefs>,\n ): ParserBuilder<Context, Names, RuleDefs> {\n if (Array.isArray(start)) {\n return <ParserBuilder<Context, Names, RuleDefs>> <unknown> new ParserBuilder(listToRuleDefMap(start));\n }\n return new ParserBuilder({ ...(<ParserBuilder<any, any, any>>start).rules });\n }\n\n private rules: RuleDefs;\n\n private constructor(startRules: RuleDefs) {\n this.rules = startRules;\n }\n\n public widenContext<NewContext extends Context>(): ParserBuilder<\n NewContext,\nNames,\n{[Key in keyof RuleDefs]: Key extends Names ?\n (RuleDefs[Key] extends ParserRule<any, any, infer RT, infer PT> ? ParserRule<NewContext, Key, RT, PT> : never)\n : never }\n> {\n return <any> this;\n }\n\n public typePatch<Patch extends {[Key in Names]?: [any] | [any, any[]]}>():\n ParserBuilder<Context, Names, {[Key in Names]: Key extends keyof Patch ? (\n Patch[Key] extends [any, any[]] ? ParserRule<Context, Key, Patch[Key][0], Patch[Key][1]> : (\n // Only one - infer yourself\n Patch[Key] extends [any] ? (\n RuleDefs[Key] extends ParserRule<any, any, any, infer Par> ?\n ParserRule<Context, Key, Patch[Key][0], Par> : never\n ) : never\n )\n ) : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }> {\n return <any> this;\n }\n\n /**\n * Change the implementation of an existing grammar rule.\n */\n public patchRule<U extends Names, RET, ARGS extends any[]>(patch: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> :\n (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never)\n } > {\n const self = <ParserBuilder<Context, Names, {[Key in Names]: Key extends U ?\n ParserRule<Context, Key, RET, ARGS> : (RuleDefs[Key] extends ParserRule<Context, Key> ? RuleDefs[Key] : never) }>>\n <unknown> this;\n self.rules[patch.name] = <any> patch;\n return self;\n }\n\n /**\n * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.\n */\n public addRuleRedundant<U extends string, RET, ARGS extends any[]>(rule: ParserRule<Context, U, RET, ARGS>):\n ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never)\n }> {\n const self = <ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }>>\n <unknown> this;\n const rules = <Record<string, ParserRule<Context>>> self.rules;\n if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {\n throw new Error(`Rule ${rule.name} already exists in the builder`);\n }\n rules[rule.name] = rule;\n return self;\n }\n\n /**\n * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.\n */\n public addRule<U extends string, RET, ARGS extends any[]>(\n rule: CheckOverlap<U, Names, ParserRule<Context, U, RET, ARGS>>,\n ): ParserBuilder<Context, Names | U, {[K in Names | U]: K extends U ?\n ParserRule<Context, K, RET, ARGS> :\n (K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never) }> {\n return this.addRuleRedundant(rule);\n }\n\n public addMany<U extends readonly ParserRule<Context>[]>(\n ...rules: CheckOverlap<ParseNamesFromList<U>, Names, U>\n ): ParserBuilder<\n Context,\n Names | ParseNamesFromList<U>,\n {[K in Names | ParseNamesFromList<U>]:\n K extends keyof ParseRulesToObject<typeof rules> ? (\n ParseRulesToObject<typeof rules>[K] extends ParserRule<Context, K> ? ParseRulesToObject<typeof rules>[K] : never\n ) : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never) : never\n )\n }\n > {\n this.rules = { ...this.rules, ...listToRuleDefMap(rules) };\n return <any> <unknown> this;\n }\n\n /**\n * Delete a grammar rule by its name.\n */\n public deleteRule<U extends Names>(ruleName: U):\n ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }> {\n delete this.rules[ruleName];\n return <ParserBuilder<Context, Exclude<Names, U>, {[K in Exclude<Names, U>]:\n RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never }>>\n <unknown> this;\n }\n\n public getRule<U extends Names>(ruleName: U): RuleDefs[U] extends ParserRule<any, U, infer RT, infer PT> ?\n ParserRule<Context, U, RT, PT> : never {\n return <any> this.rules[ruleName];\n }\n\n /**\n * Merge this grammar builder with another.\n * It is best to merge the bigger grammar with the smaller one.\n * If the two builders both have a grammar rule with the same name,\n * no error will be thrown case they map to the same ruledef object.\n * If they map to a different object, an error will be thrown.\n * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,\n * this rule implementation will be used.\n */\n public merge<\n OtherNames extends string,\n OtherRules extends ParseRuleMap<OtherNames>,\n OW extends readonly ParserRule<Context>[],\n >(\n builder: ParserBuilder<Context, OtherNames, OtherRules>,\n overridingRules: OW,\n ):\n ParserBuilder<\n Context,\n Names | OtherNames | ParseNamesFromList<OW>,\n {[K in Names | OtherNames | ParseNamesFromList<OW>]:\n K extends keyof ParseRulesToObject<OW> ? (\n ParseRulesToObject<OW>[K] extends ParserRule<Context, K> ? ParseRulesToObject<OW>[K] : never\n )\n : (\n K extends Names ? (RuleDefs[K] extends ParserRule<Context, K> ? RuleDefs[K] : never)\n : K extends OtherNames ? (OtherRules[K] extends ParserRule<Context, K> ? OtherRules[K] : never) : never\n ) }\n > {\n // Assume the other grammar is bigger than yours. So start from that one and add this one\n const otherRules: Record<string, ParserRule<Context>> = { ...builder.rules };\n const myRules: Record<string, ParserRule<Context>> = this.rules;\n\n for (const rule of Object.values(myRules)) {\n if (otherRules[rule.name] === undefined) {\n otherRules[rule.name] = rule;\n } else {\n const existingRule = otherRules[rule.name];\n // If same rule, no issue, move on. Else\n if (existingRule !== rule) {\n const override = overridingRules.find(x => x.name === rule.name);\n // If override specified, take override, else, inform user that there is a conflict\n if (override) {\n otherRules[rule.name] = override;\n } else {\n throw new Error(`Rule with name \"${rule.name}\" already exists in the builder, specify an override to resolve conflict`);\n }\n }\n }\n }\n\n this.rules = <any> <unknown> otherRules;\n return <any> <unknown> this;\n }\n\n private defaultErrorHandler(input: string, errors: IRecognitionException[]): void {\n const firstError = errors[0];\n const messageBuilder: string[] = [ 'Parse error' ];\n const lineIdx = firstError.token.startLine;\n if (lineIdx !== undefined && !Number.isNaN(lineIdx)) {\n const errorLine = input.split('\\n')[lineIdx - 1];\n messageBuilder.push(` on line ${lineIdx}\n${errorLine}`);\n const columnIdx = firstError.token.startColumn;\n if (columnIdx !== undefined) {\n messageBuilder.push(`\\n${'-'.repeat(columnIdx - 1)}^`);\n }\n }\n messageBuilder.push(`\\n${firstError.message}`);\n throw new Error(messageBuilder.join(''));\n }\n\n public build({\n tokenVocabulary,\n parserConfig = {},\n lexerConfig = {},\n queryPreProcessor = s => s,\n errorHandler,\n }: ParserBuildArgs): ParserFromRules<Context, Names, RuleDefs> {\n const lexer = LexerBuilder.create().add(...tokenVocabulary).build({\n positionTracking: 'full',\n recoveryEnabled: false,\n ensureOptimizations: true,\n safeMode: false,\n skipValidations: true,\n ...lexerConfig,\n });\n // Get the chevrotain parser\n const parser = this.consume({\n tokenVocabulary: <TokenType[]> tokenVocabulary,\n config: parserConfig,\n });\n // Start building a parser that does not pass input using a state, but instead gets it as a function argument.\n const selfSufficientParser: Partial<ParserFromRules<Context, Names, RuleDefs>> = {};\n\n // To do that, we need to create a wrapper for each parser rule.\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <ParserRule<Context, Names>[]> Object.values(this.rules)) {\n selfSufficientParser[rule.name] = <any> ((input: string, context: Context, ...args: unknown[]) => {\n const processedInput = queryPreProcessor(input);\n const lexResult = lexer.tokenize(processedInput);\n\n // This also resets the parser\n parser.input = lexResult.tokens;\n parser.setContext(context);\n const result = parser[rule.name](context, ...args);\n if (parser.errors.length > 0) {\n // Console.log(lexResult);\n if (errorHandler) {\n errorHandler(parser.errors);\n } else {\n this.defaultErrorHandler(processedInput, parser.errors);\n }\n }\n return result;\n });\n }\n return <ParserFromRules<Context, Names, RuleDefs>> selfSufficientParser;\n }\n\n private consume({ tokenVocabulary, config = {}}: {\n tokenVocabulary: TokenVocabulary;\n config?: IParserConfig;\n }): EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void } {\n return <EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> &\n { setContext: (context: Context) => void }><unknown> new DynamicParser(this.rules, tokenVocabulary, config);\n }\n}\n"]}
|
package/lib/utils.d.ts
CHANGED
|
@@ -15,4 +15,8 @@ export type Patch<T extends object, Patch extends {
|
|
|
15
15
|
}> = {
|
|
16
16
|
[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key];
|
|
17
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Key that allows you to set the space based indentation of your query when generating newlines using the NEW_LINE.
|
|
20
|
+
* A value of -1 disables the generation of newlines from the NEW_LINE function
|
|
21
|
+
*/
|
|
18
22
|
export declare const traqulaIndentation = "When you use this string, you expect traqula to handle indentation after every newline";
|
package/lib/utils.js
CHANGED
|
@@ -5,5 +5,9 @@ export function unCapitalize(str) {
|
|
|
5
5
|
export function createToken(config) {
|
|
6
6
|
return chevcT(config);
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Key that allows you to set the space based indentation of your query when generating newlines using the NEW_LINE.
|
|
10
|
+
* A value of -1 disables the generation of newlines from the NEW_LINE function
|
|
11
|
+
*/
|
|
8
12
|
export const traqulaIndentation = 'When you use this string, you expect traqula to handle indentation after every newline';
|
|
9
13
|
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,YAAY,CAAC;AAOnD,MAAM,UAAU,YAAY,CAAmB,GAAM;IACnD,OAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAID,MAAM,UAAU,WAAW,CAAsB,MAAqC;IACpF,OAAoC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC;AAKD,MAAM,CAAC,MAAM,kBAAkB,GAC7B,wFAAwF,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@chevrotain/types';\nimport { createToken as chevcT } from 'chevrotain';\n\n/**\n * Check whether the first two types overlap, if no, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\nexport function unCapitalize<T extends string>(str: T): Uncapitalize<T> {\n return <Uncapitalize<T>> (str.charAt(0).toLowerCase() + str.slice(1));\n}\n\nexport type NamedToken<Name extends string = string> = TokenType & { name: Name };\n\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: unknown }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n\nexport const traqulaIndentation =\n 'When you use this string, you expect traqula to handle indentation after every newline';\n"]}
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,YAAY,CAAC;AAOnD,MAAM,UAAU,YAAY,CAAmB,GAAM;IACnD,OAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAID,MAAM,UAAU,WAAW,CAAsB,MAAqC;IACpF,OAAoC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC;AAKD;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAC7B,wFAAwF,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@chevrotain/types';\nimport { createToken as chevcT } from 'chevrotain';\n\n/**\n * Check whether the first two types overlap, if no, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\nexport function unCapitalize<T extends string>(str: T): Uncapitalize<T> {\n return <Uncapitalize<T>> (str.charAt(0).toLowerCase() + str.slice(1));\n}\n\nexport type NamedToken<Name extends string = string> = TokenType & { name: Name };\n\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: unknown }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n\n/**\n * Key that allows you to set the space based indentation of your query when generating newlines using the NEW_LINE.\n * A value of -1 disables the generation of newlines from the NEW_LINE function\n */\nexport const traqulaIndentation =\n 'When you use this string, you expect traqula to handle indentation after every newline';\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@traqula/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.20",
|
|
5
5
|
"description": "Core components of Traqula",
|
|
6
6
|
"lsd:module": true,
|
|
7
7
|
"license": "MIT",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@chevrotain/types": "^11.0.3"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "caf35c28d4ddbfec5947566b697d34f38c1e6e31"
|
|
51
51
|
}
|
package/lib/Transformers.d.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import type { Node } from './nodeTypings.js';
|
|
2
|
-
type Safeness = 'safe' | 'unsafe';
|
|
3
|
-
type SafeWrap<Safe extends Safeness, obj extends object> = Safe extends 'safe' ? {
|
|
4
|
-
[key in keyof obj]: unknown;
|
|
5
|
-
} : obj;
|
|
6
|
-
export interface VisitContext {
|
|
7
|
-
shortcut?: boolean;
|
|
8
|
-
continue?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface SelectiveTraversalContext<Nodes> {
|
|
11
|
-
next?: Nodes[];
|
|
12
|
-
shortcut?: boolean;
|
|
13
|
-
}
|
|
14
|
-
export declare class TransformerType<Nodes extends Pick<Node, 'type'>> {
|
|
15
|
-
protected clone<T>(obj: T): T;
|
|
16
|
-
protected safeObjectVisit(value: unknown, mapper: (some: object) => any): unknown;
|
|
17
|
-
/**
|
|
18
|
-
* Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.
|
|
19
|
-
* @param startObject object to start iterating from
|
|
20
|
-
* @param mapper mapper to transform the various objects - argument is a copy of the original
|
|
21
|
-
* @param preVisitor callback that is evaluated before iterating deeper.
|
|
22
|
-
* If continues is false, we do not iterate deeper, current object is still mapped. - default: true
|
|
23
|
-
* If shortcut is true, we do not iterate deeper, nor do we branch out, this mapper will be the last one called.
|
|
24
|
-
* - Default false
|
|
25
|
-
*/
|
|
26
|
-
transformObject(startObject: object, mapper: (some: object) => any, preVisitor?: (some: object) => VisitContext): unknown;
|
|
27
|
-
/**
|
|
28
|
-
* Visitor that visits all objects. Visits deeper objects first.
|
|
29
|
-
*/
|
|
30
|
-
visitObject(startObject: object, visitor: (some: object) => void, preVisitor?: (some: object) => VisitContext): void;
|
|
31
|
-
transformNode<Safe extends 'safe' | 'unsafe' = 'safe'>(startObject: object, nodeCallBacks: {
|
|
32
|
-
[T in Nodes['type']]?: {
|
|
33
|
-
transform?: (op: SafeWrap<Safe, Extract<Nodes, {
|
|
34
|
-
type: T;
|
|
35
|
-
}>>) => any;
|
|
36
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
37
|
-
type: T;
|
|
38
|
-
}>) => VisitContext;
|
|
39
|
-
};
|
|
40
|
-
}): any;
|
|
41
|
-
visitNode(startObject: object, nodeCallBacks: {
|
|
42
|
-
[T in Nodes['type']]?: {
|
|
43
|
-
visitor?: (op: Extract<Nodes, {
|
|
44
|
-
type: T;
|
|
45
|
-
}>) => void;
|
|
46
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
47
|
-
type: T;
|
|
48
|
-
}>) => VisitContext;
|
|
49
|
-
};
|
|
50
|
-
}): void;
|
|
51
|
-
traverseNodes(currentNode: Nodes, traverse: {
|
|
52
|
-
[T in Nodes['type']]?: (op: Extract<Nodes, {
|
|
53
|
-
type: T;
|
|
54
|
-
}>) => SelectiveTraversalContext<Nodes>;
|
|
55
|
-
}): void;
|
|
56
|
-
}
|
|
57
|
-
export declare class TransformerSubType<Nodes extends Pick<Node, 'type' | 'subType'>> extends TransformerType<Nodes> {
|
|
58
|
-
transformNodeSpecific<Safe extends 'safe' | 'unsafe' = 'safe'>(startObject: object, nodeCallBacks: {
|
|
59
|
-
[T in Nodes['type']]?: {
|
|
60
|
-
transform?: (op: SafeWrap<Safe, Extract<Nodes, {
|
|
61
|
-
type: T;
|
|
62
|
-
}>>) => any;
|
|
63
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
64
|
-
type: T;
|
|
65
|
-
}>) => VisitContext;
|
|
66
|
-
};
|
|
67
|
-
}, nodeSpecificCallBacks: {
|
|
68
|
-
[Type in Nodes['type']]?: {
|
|
69
|
-
[SubType in Extract<Nodes, {
|
|
70
|
-
type: Type;
|
|
71
|
-
subType: string;
|
|
72
|
-
}>['subType']]?: {
|
|
73
|
-
transform?: (op: SafeWrap<Safe, Extract<Nodes, {
|
|
74
|
-
type: Type;
|
|
75
|
-
subType: SubType;
|
|
76
|
-
}>>) => any;
|
|
77
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
78
|
-
type: Type;
|
|
79
|
-
subType: SubType;
|
|
80
|
-
}>) => VisitContext;
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
}): any;
|
|
84
|
-
/**
|
|
85
|
-
* When both nodeCallBack and NodeSpecific callBack are matched, will only look at nodeSpecifCallback
|
|
86
|
-
*/
|
|
87
|
-
visitNodeSpecific(startObject: object, nodeCallBacks: {
|
|
88
|
-
[T in Nodes['type']]?: {
|
|
89
|
-
visitor?: (op: Extract<Nodes, {
|
|
90
|
-
type: T;
|
|
91
|
-
}>) => void;
|
|
92
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
93
|
-
type: T;
|
|
94
|
-
}>) => VisitContext;
|
|
95
|
-
};
|
|
96
|
-
}, nodeSpecificCallBacks: {
|
|
97
|
-
[Type in Nodes['type']]?: {
|
|
98
|
-
[Subtype in Extract<Nodes, {
|
|
99
|
-
type: Type;
|
|
100
|
-
subType: string;
|
|
101
|
-
}>['subType']]?: {
|
|
102
|
-
visitor?: (op: Extract<Nodes, {
|
|
103
|
-
type: Type;
|
|
104
|
-
subType: Subtype;
|
|
105
|
-
}>) => void;
|
|
106
|
-
preVisitor?: (op: Extract<Nodes, {
|
|
107
|
-
type: Type;
|
|
108
|
-
subType: Subtype;
|
|
109
|
-
}>) => VisitContext;
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
}): void;
|
|
113
|
-
traverseSubNodes(currentNode: Nodes, traverseNode: {
|
|
114
|
-
[Type in Nodes['type']]?: (op: Extract<Nodes, {
|
|
115
|
-
type: Type;
|
|
116
|
-
}>) => SelectiveTraversalContext<Nodes>;
|
|
117
|
-
}, traverseSubNode: {
|
|
118
|
-
[Type in Nodes['type']]?: {
|
|
119
|
-
[Subtype in Extract<Nodes, {
|
|
120
|
-
type: Type;
|
|
121
|
-
subType: string;
|
|
122
|
-
}>['subType']]?: (op: Extract<Nodes, {
|
|
123
|
-
type: Type;
|
|
124
|
-
subType: Subtype;
|
|
125
|
-
}>) => SelectiveTraversalContext<Nodes>;
|
|
126
|
-
};
|
|
127
|
-
}): void;
|
|
128
|
-
}
|
|
129
|
-
export {};
|