@traqula/core 0.0.1-alpha.176 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/lib/CoreFactory.d.ts +39 -0
  2. package/lib/CoreFactory.js +135 -0
  3. package/lib/CoreFactory.js.map +1 -0
  4. package/lib/Transformer.d.ts +26 -0
  5. package/lib/Transformer.js +57 -0
  6. package/lib/Transformer.js.map +1 -0
  7. package/lib/generator-builder/builderTypes.d.ts +6 -6
  8. package/lib/generator-builder/builderTypes.js.map +1 -1
  9. package/lib/generator-builder/dynamicGenerator.d.ts +18 -3
  10. package/lib/generator-builder/dynamicGenerator.js +104 -17
  11. package/lib/generator-builder/dynamicGenerator.js.map +1 -1
  12. package/lib/generator-builder/generatorBuilder.d.ts +9 -7
  13. package/lib/generator-builder/generatorBuilder.js +4 -1
  14. package/lib/generator-builder/generatorBuilder.js.map +1 -1
  15. package/lib/generator-builder/generatorTypes.d.ts +13 -4
  16. package/lib/generator-builder/generatorTypes.js.map +1 -1
  17. package/lib/index.cjs +394 -13
  18. package/lib/index.d.ts +5 -0
  19. package/lib/index.js +6 -0
  20. package/lib/index.js.map +1 -1
  21. package/lib/indirection-builder/IndirBuilder.d.ts +43 -0
  22. package/lib/indirection-builder/IndirBuilder.js +60 -0
  23. package/lib/indirection-builder/IndirBuilder.js.map +1 -0
  24. package/lib/indirection-builder/dynamicIndirected.d.ts +9 -0
  25. package/lib/indirection-builder/dynamicIndirected.js +28 -0
  26. package/lib/indirection-builder/dynamicIndirected.js.map +1 -0
  27. package/lib/indirection-builder/helpers.d.ts +24 -0
  28. package/lib/indirection-builder/helpers.js +11 -0
  29. package/lib/indirection-builder/helpers.js.map +1 -0
  30. package/lib/lexer-builder/LexerBuilder.d.ts +4 -0
  31. package/lib/lexer-builder/LexerBuilder.js +5 -1
  32. package/lib/lexer-builder/LexerBuilder.js.map +1 -1
  33. package/lib/nodeTypings.d.ts +53 -0
  34. package/lib/nodeTypings.js +2 -0
  35. package/lib/nodeTypings.js.map +1 -0
  36. package/lib/parser-builder/builderTypes.d.ts +3 -1
  37. package/lib/parser-builder/builderTypes.js.map +1 -1
  38. package/lib/parser-builder/parserBuilder.d.ts +3 -2
  39. package/lib/parser-builder/parserBuilder.js +13 -3
  40. package/lib/parser-builder/parserBuilder.js.map +1 -1
  41. package/lib/utils.d.ts +1 -1
  42. package/lib/utils.js.map +1 -1
  43. package/package.json +2 -2
@@ -0,0 +1,39 @@
1
+ import type { IToken } from 'chevrotain';
2
+ import type { SourceLocation, SourceLocationNodeAutoGenerate, SourceLocationNodeReplace, SourceLocationNoMaterialize, SourceLocationSource, SourceLocationStringReplace, Node, Localized, Wrap } from './nodeTypings';
3
+ export type Typed<Type extends PropertyKey> = {
4
+ type: Type;
5
+ };
6
+ export type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = {
7
+ type: Type;
8
+ subType: Subtype;
9
+ };
10
+ export declare class CoreFactory {
11
+ wrap<T>(val: T, loc: SourceLocation): Wrap<T>;
12
+ isLocalized(obj: unknown): obj is Localized;
13
+ sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation;
14
+ sourceLocationNoMaterialize(): SourceLocationNoMaterialize;
15
+ /**
16
+ * Returns a copy of the argument that is not materialized
17
+ */
18
+ dematerialized<T extends Node>(arg: T): T & {
19
+ loc: SourceLocationNoMaterialize;
20
+ };
21
+ safeObjectTransform(value: unknown, mapper: (some: object) => any): any;
22
+ forcedAutoGenTree<T extends object>(obj: T): T;
23
+ forceMaterialized<T extends Node>(arg: T): T;
24
+ isSourceLocation(loc: object): loc is SourceLocation;
25
+ sourceLocationSource(start: number, end: number): SourceLocationSource;
26
+ gen(): SourceLocationNodeAutoGenerate;
27
+ isSourceLocationSource(loc: object): loc is SourceLocationSource;
28
+ isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace;
29
+ sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace;
30
+ sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;
31
+ sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;
32
+ isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace;
33
+ isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate;
34
+ nodeShouldPrint(node: Localized): boolean;
35
+ printFilter(node: Localized, callback: () => void): void;
36
+ isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize;
37
+ isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type>;
38
+ isOfSubType<Type extends string, SubType extends string>(obj: object, type: Type, subType: SubType): obj is SubTyped<Type, SubType>;
39
+ }
@@ -0,0 +1,135 @@
1
+ export class CoreFactory {
2
+ wrap(val, loc) {
3
+ return { val, loc };
4
+ }
5
+ isLocalized(obj) {
6
+ return typeof obj === 'object' && obj !== null && 'loc' in obj &&
7
+ typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;
8
+ }
9
+ sourceLocation(...elements) {
10
+ const pureElements = elements.filter(x => x !== undefined);
11
+ if (pureElements.length === 0) {
12
+ return this.sourceLocationNoMaterialize();
13
+ }
14
+ const filtered = pureElements.filter(element => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||
15
+ this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
16
+ if (filtered.length === 0) {
17
+ return this.gen();
18
+ }
19
+ const first = filtered[0];
20
+ const last = filtered.at(-1);
21
+ return {
22
+ sourceLocationType: 'source',
23
+ start: this.isLocalized(first) ?
24
+ first.loc.start :
25
+ first.startOffset,
26
+ end: this.isLocalized(last) ?
27
+ last.loc.end :
28
+ (last.endOffset + 1),
29
+ };
30
+ }
31
+ sourceLocationNoMaterialize() {
32
+ return { sourceLocationType: 'noMaterialize' };
33
+ }
34
+ /**
35
+ * Returns a copy of the argument that is not materialized
36
+ */
37
+ dematerialized(arg) {
38
+ return { ...arg, loc: this.sourceLocationNoMaterialize() };
39
+ }
40
+ safeObjectTransform(value, mapper) {
41
+ if (value && typeof value === 'object') {
42
+ // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects
43
+ if (Array.isArray(value)) {
44
+ return value.map(x => this.safeObjectTransform(x, mapper));
45
+ }
46
+ return mapper(value);
47
+ }
48
+ return value;
49
+ }
50
+ forcedAutoGenTree(obj) {
51
+ const copy = { ...obj };
52
+ for (const [key, value] of Object.entries(copy)) {
53
+ copy[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));
54
+ }
55
+ if (this.isLocalized(copy)) {
56
+ copy.loc = this.gen();
57
+ }
58
+ return copy;
59
+ }
60
+ forceMaterialized(arg) {
61
+ if (this.isSourceLocationNoMaterialize(arg.loc)) {
62
+ return this.forcedAutoGenTree(arg);
63
+ }
64
+ return { ...arg };
65
+ }
66
+ isSourceLocation(loc) {
67
+ return 'sourceLocationType' in loc;
68
+ }
69
+ sourceLocationSource(start, end) {
70
+ return {
71
+ sourceLocationType: 'source',
72
+ start,
73
+ end,
74
+ };
75
+ }
76
+ gen() {
77
+ return { sourceLocationType: 'autoGenerate' };
78
+ }
79
+ isSourceLocationSource(loc) {
80
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';
81
+ }
82
+ isSourceLocationStringReplace(loc) {
83
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';
84
+ }
85
+ sourceLocationNodeReplaceUnsafe(loc) {
86
+ if (this.isSourceLocationSource(loc)) {
87
+ return this.sourceLocationNodeReplace(loc);
88
+ }
89
+ throw new Error('not possible');
90
+ }
91
+ sourceLocationNodeReplace(startOrLoc, end) {
92
+ let starting;
93
+ let ending;
94
+ if (typeof startOrLoc === 'number') {
95
+ starting = startOrLoc;
96
+ ending = end;
97
+ }
98
+ else {
99
+ starting = startOrLoc.start;
100
+ ending = startOrLoc.end;
101
+ }
102
+ return {
103
+ sourceLocationType: 'nodeReplace',
104
+ start: starting,
105
+ end: ending,
106
+ };
107
+ }
108
+ isSourceLocationNodeReplace(loc) {
109
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';
110
+ }
111
+ isSourceLocationNodeAutoGenerate(loc) {
112
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';
113
+ }
114
+ nodeShouldPrint(node) {
115
+ return this.isSourceLocationNodeReplace(node.loc) ||
116
+ this.isSourceLocationNodeAutoGenerate(node.loc);
117
+ }
118
+ printFilter(node, callback) {
119
+ if (this.nodeShouldPrint(node)) {
120
+ callback();
121
+ }
122
+ }
123
+ isSourceLocationNoMaterialize(loc) {
124
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';
125
+ }
126
+ isOfType(obj, type) {
127
+ const casted = obj;
128
+ return casted.type === type;
129
+ }
130
+ isOfSubType(obj, type, subType) {
131
+ const temp = obj;
132
+ return temp.type === type && temp.subType === subType;
133
+ }
134
+ }
135
+ //# sourceMappingURL=CoreFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CoreFactory.js","sourceRoot":"","sources":["CoreFactory.ts"],"names":[],"mappings":"AAiBA,MAAM,OAAO,WAAW;IACf,IAAI,CAAI,GAAM,EAAE,GAAmB;QACxC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,WAAW,CAAC,GAAY;QAC7B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,GAAG;YAC5D,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,IAAI,oBAAoB,IAAI,GAAG,CAAC,GAAG,CAAC;IACvF,CAAC;IAEM,cAAc,CAAC,GAAG,QAA4C;QACnE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3D,IAAI,YAAY,CAAC,MAAM,KAAK,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;QACjD,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,cAAc,CAAC;IACjF,CAAC;IAEM,eAAe,CAAC,IAAe;QACpC,OAAO,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC;YAC/C,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpD,CAAC;IAEM,WAAW,CAAC,IAAe,EAAE,QAAoB;QACtD,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/B,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAEM,6BAA6B,CAAC,GAAW;QAC9C,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,eAAe,CAAC;IAClF,CAAC;IAEM,QAAQ,CAAsB,GAAW,EAAE,IAAU;QAC1D,MAAM,MAAM,GAAmB,GAAG,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IAC9B,CAAC;IAEM,WAAW,CAA8C,GAAW,EAAE,IAAU,EAAE,OAAgB;QAEvG,MAAM,IAAI,GAA0C,GAAG,CAAC;QACxD,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC;IACxD,CAAC;CACF","sourcesContent":["import type { IToken } from 'chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n} from './nodeTypings';\n\nexport type Typed<Type extends PropertyKey> = { type: Type };\nexport type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = { type: Type; subType: Subtype };\n\nexport class CoreFactory {\n public wrap<T>(val: T, loc: SourceLocation): Wrap<T> {\n return { val, loc };\n }\n\n public isLocalized(obj: unknown): obj is Localized {\n return typeof obj === 'object' && obj !== null && 'loc' in obj &&\n typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;\n }\n\n public sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation {\n const pureElements = elements.filter(x => x !== undefined);\n if (pureElements.length === 0) {\n return this.sourceLocationNoMaterialize();\n }\n const filtered = pureElements.filter(element =>\n !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||\n this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));\n if (filtered.length === 0) {\n return this.gen();\n }\n const first = filtered[0];\n const last = filtered.at(-1)!;\n return {\n sourceLocationType: 'source',\n start: this.isLocalized(first) ?\n (<SourceLocationSource | SourceLocationStringReplace> first.loc).start :\n first.startOffset,\n end: this.isLocalized(last) ?\n (<SourceLocationSource | SourceLocationStringReplace> last.loc).end :\n (last.endOffset! + 1),\n };\n }\n\n public sourceLocationNoMaterialize(): SourceLocationNoMaterialize {\n return { sourceLocationType: 'noMaterialize' };\n }\n\n /**\n * Returns a copy of the argument that is not materialized\n */\n public dematerialized<T extends Node>(arg: T): T & { loc: SourceLocationNoMaterialize } {\n return { ...arg, loc: this.sourceLocationNoMaterialize() };\n }\n\n public safeObjectTransform(value: unknown, mapper: (some: object) => any): any {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectTransform(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n public forcedAutoGenTree<T extends object>(obj: T): T {\n const copy = { ...obj };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, object>> copy)[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));\n }\n if (this.isLocalized(copy)) {\n copy.loc = this.gen();\n }\n return copy;\n }\n\n public forceMaterialized<T extends Node>(arg: T): T {\n if (this.isSourceLocationNoMaterialize(arg.loc)) {\n return this.forcedAutoGenTree(arg);\n }\n return { ...arg };\n }\n\n public isSourceLocation(loc: object): loc is SourceLocation {\n return 'sourceLocationType' in loc;\n }\n\n public sourceLocationSource(start: number, end: number): SourceLocationSource {\n return {\n sourceLocationType: 'source',\n start,\n end,\n };\n }\n\n public gen(): SourceLocationNodeAutoGenerate {\n return { sourceLocationType: 'autoGenerate' };\n }\n\n public isSourceLocationSource(loc: object): loc is SourceLocationSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';\n }\n\n public isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';\n }\n\n public sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace {\n if (this.isSourceLocationSource(loc)) {\n return this.sourceLocationNodeReplace(loc);\n }\n throw new Error('not possible');\n }\n\n public sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(startOrLoc: number | SourceLocationSource, end?: number): SourceLocationNodeReplace {\n let starting;\n let ending;\n if (typeof startOrLoc === 'number') {\n starting = startOrLoc;\n ending = end!;\n } else {\n starting = startOrLoc.start;\n ending = startOrLoc.end;\n }\n return {\n sourceLocationType: 'nodeReplace',\n start: starting,\n end: ending,\n };\n }\n\n public isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';\n }\n\n public isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';\n }\n\n public nodeShouldPrint(node: Localized): boolean {\n return this.isSourceLocationNodeReplace(node.loc) ||\n this.isSourceLocationNodeAutoGenerate(node.loc);\n }\n\n public printFilter(node: Localized, callback: () => void): void {\n if (this.nodeShouldPrint(node)) {\n callback();\n }\n }\n\n public isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';\n }\n\n public isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type> {\n const casted: { type?: any } = obj;\n return casted.type === type;\n }\n\n public isOfSubType<Type extends string, SubType extends string>(obj: object, type: Type, subType: SubType):\n obj is SubTyped<Type, SubType> {\n const temp: { type?: unknown; subType?: unknown } = obj;\n return temp.type === type && temp.subType === subType;\n }\n}\n"]}
@@ -0,0 +1,26 @@
1
+ import type { SubTyped } from './CoreFactory';
2
+ import type { Node } from './nodeTypings';
3
+ type MapNodeTypeToImpls<Nodes extends Node> = {
4
+ [Node in Nodes as Node['type']]: Node;
5
+ };
6
+ type MapNodeSubTypeToImpls<Nodes extends Node & {
7
+ subType: string;
8
+ }> = {
9
+ [Node in Nodes as Node['subType']]: Node;
10
+ };
11
+ export type AlterNodeOutput<RecursiveObject extends object, Input, Out> = {
12
+ [Key in keyof RecursiveObject]: RecursiveObject[Key] extends object ? (AlterNodeOutput<RecursiveObject[Key], Input, Out> extends Input ? Out : AlterNodeOutput<RecursiveObject[Key], Input, Out>) : RecursiveObject[Key];
13
+ };
14
+ export declare class Transformer<Nodes extends Node, NodeMapping extends MapNodeTypeToImpls<Nodes> & Record<string, unknown> = MapNodeTypeToImpls<Nodes>> {
15
+ transformNode<Input extends object, TypeFilter extends keyof NodeMapping, Out>(curObject: Input, searchType: TypeFilter, patch: (current: NodeMapping[TypeFilter]) => Out): AlterNodeOutput<Input, NodeMapping[TypeFilter], Out>;
16
+ transformNodeSpecific<Input extends object, TypeFilter extends keyof NodeMapping, SpecificType extends keyof SpecificNodes, Out, SpecificNodes = NodeMapping[TypeFilter] extends Node & {
17
+ subType: string;
18
+ } ? MapNodeSubTypeToImpls<NodeMapping[TypeFilter]> : never>(curObject: Input, searchType: TypeFilter, searchSubType: SpecificType, patch: (current: SpecificNodes[SpecificType]) => Out): AlterNodeOutput<Input, SubTyped<TypeFilter, SpecificType>, Out>;
19
+ visitObjects(curObject: object, visitor: (current: object) => void): void;
20
+ visitNode<Input extends object, TypeFilter extends keyof NodeMapping>(curObject: Input, searchType: TypeFilter, visitor: (current: Readonly<NodeMapping[TypeFilter]>) => void): void;
21
+ visitNodeSpecific<Input extends object, TypeFilter extends keyof NodeMapping, SpecificType extends keyof SpecificNodes, SpecificNodes = NodeMapping[TypeFilter] extends Node & {
22
+ subType: string;
23
+ } ? MapNodeSubTypeToImpls<NodeMapping[TypeFilter]> : never>(curObject: Input, searchType: TypeFilter, searchSubType: SpecificType, visitor: (current: Readonly<SpecificNodes[SpecificType]>) => void): void;
24
+ private safeObjectVisit;
25
+ }
26
+ export {};
@@ -0,0 +1,57 @@
1
+ export class Transformer {
2
+ transformNode(curObject, searchType, patch) {
3
+ const copy = { ...curObject };
4
+ for (const [key, value] of Object.entries(copy)) {
5
+ copy[key] =
6
+ this.safeObjectVisit(value, obj => this.transformNode(obj, searchType, patch));
7
+ }
8
+ if (copy.type === searchType) {
9
+ return patch(copy);
10
+ }
11
+ return copy;
12
+ }
13
+ transformNodeSpecific(curObject, searchType, searchSubType, patch) {
14
+ const copy = { ...curObject };
15
+ for (const [key, value] of Object.entries(copy)) {
16
+ copy[key] =
17
+ this.safeObjectVisit(value, obj => this.transformNodeSpecific(obj, searchType, searchSubType, patch));
18
+ }
19
+ if (copy.type === searchType && copy.subType === searchSubType) {
20
+ return patch(copy);
21
+ }
22
+ return copy;
23
+ }
24
+ visitObjects(curObject, visitor) {
25
+ for (const value of Object.values(curObject)) {
26
+ this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));
27
+ }
28
+ }
29
+ visitNode(curObject, searchType, visitor) {
30
+ for (const value of Object.values(curObject)) {
31
+ this.safeObjectVisit(value, obj => this.visitNode(obj, searchType, visitor));
32
+ }
33
+ if (curObject.type === searchType) {
34
+ visitor(curObject);
35
+ }
36
+ }
37
+ visitNodeSpecific(curObject, searchType, searchSubType, visitor) {
38
+ for (const value of Object.values(curObject)) {
39
+ this.safeObjectVisit(value, obj => this.visitNodeSpecific(obj, searchType, searchSubType, visitor));
40
+ }
41
+ const cast = curObject;
42
+ if (cast.type === searchType && cast.subType === searchSubType) {
43
+ visitor(curObject);
44
+ }
45
+ }
46
+ safeObjectVisit(value, mapper) {
47
+ if (value && typeof value === 'object') {
48
+ // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects
49
+ if (Array.isArray(value)) {
50
+ return value.map(x => this.safeObjectVisit(x, mapper));
51
+ }
52
+ return mapper(value);
53
+ }
54
+ return value;
55
+ }
56
+ }
57
+ //# sourceMappingURL=Transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Transformer.js","sourceRoot":"","sources":["Transformer.ts"],"names":[],"mappings":"AAcA,MAAM,OAAO,WAAW;IAIf,aAAa,CAClB,SAAgB,EAChB,UAAsB,EACtB,KAAgD;QAEhD,MAAM,IAAI,GAAuB,EAAE,GAAG,SAAS,EAAE,CAAC;QAClD,KAAK,MAAM,CAAE,GAAG,EAAE,KAAK,CAAE,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,IAAK,CAAC,GAAG,CAAC;gBACnC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;QACnF,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAC7B,OAAa,KAAK,CAA2B,IAAI,CAAC,CAAC;QACrD,CAAC;QACD,OAAa,IAAI,CAAC;IACpB,CAAC;IAEM,qBAAqB,CAQ1B,SAAgB,EAChB,UAAsB,EACtB,aAA2B,EAC3B,KAAoD;QAEpD,MAAM,IAAI,GAA0C,EAAE,GAAG,SAAS,EAAE,CAAC;QACrE,KAAK,MAAM,CAAE,GAAG,EAAE,KAAK,CAAE,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAA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CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC;QACtG,CAAC;QACD,MAAM,IAAI,GAA0C,SAAS,CAAC;QAC9D,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,OAAO,KAAK,aAAa,EAAE,CAAC;YAC/D,OAAO,CAA+B,SAAS,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;IAEO,eAAe,CAAC,KAAc,EAAE,MAA6B;QACnE,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACvC,6GAA6G;YAC7G,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBACzB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;YACzD,CAAC;YACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { SubTyped } from './CoreFactory';\nimport type { Node } from './nodeTypings';\n\ntype MapNodeTypeToImpls<Nodes extends Node> = {[Node in Nodes as Node['type']]: Node };\ntype MapNodeSubTypeToImpls<Nodes extends Node & { subType: string }> = {[Node in Nodes as Node['subType']]: Node };\n\nexport type AlterNodeOutput<RecursiveObject extends object, Input, Out>\n = {\n [Key in keyof RecursiveObject]: RecursiveObject[Key] extends object ?\n (AlterNodeOutput<RecursiveObject[Key], Input, Out> extends Input ?\n Out : AlterNodeOutput<RecursiveObject[Key], Input, Out>) :\n RecursiveObject[Key]\n };\n\nexport class Transformer<\n Nodes extends Node,\nNodeMapping extends MapNodeTypeToImpls<Nodes> & Record<string, unknown> = MapNodeTypeToImpls<Nodes>,\n> {\n public transformNode<Input extends object, TypeFilter extends keyof NodeMapping, Out>(\n curObject: Input,\n searchType: TypeFilter,\n patch: (current: NodeMapping[TypeFilter]) => Out,\n ): AlterNodeOutput<Input, NodeMapping[TypeFilter], Out> {\n const copy: { type?: unknown } = { ...curObject };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, unknown>> copy)[key] =\n this.safeObjectVisit(value, obj => this.transformNode(obj, searchType, patch));\n }\n if (copy.type === searchType) {\n return <any> patch(<NodeMapping[TypeFilter]> copy);\n }\n return <any> copy;\n }\n\n public transformNodeSpecific<\n Input extends object,\nTypeFilter extends keyof NodeMapping,\nSpecificType extends keyof SpecificNodes,\nOut,\nSpecificNodes = NodeMapping[TypeFilter] extends Node & { subType: string } ?\n MapNodeSubTypeToImpls<NodeMapping[TypeFilter]> : never,\n>(\n curObject: Input,\n searchType: TypeFilter,\n searchSubType: SpecificType,\n patch: (current: SpecificNodes[SpecificType]) => Out,\n ): AlterNodeOutput<Input, SubTyped<TypeFilter, SpecificType>, Out> {\n const copy: { type?: unknown; subType?: unknown } = { ...curObject };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, unknown>> copy)[key] =\n this.safeObjectVisit(value, obj => this.transformNodeSpecific(obj, searchType, searchSubType, patch));\n }\n if (copy.type === searchType && copy.subType === searchSubType) {\n return <any> patch(<SpecificNodes[SpecificType]> copy);\n }\n return <any> copy;\n }\n\n public visitObjects(curObject: object, visitor: (current: object) => void): void {\n for (const value of Object.values(curObject)) {\n this.safeObjectVisit(value, obj => this.visitObjects(obj, visitor));\n }\n }\n\n public visitNode<Input extends object, TypeFilter extends keyof NodeMapping>(\n curObject: Input,\n searchType: TypeFilter,\n visitor: (current: Readonly<NodeMapping[TypeFilter]>) => void,\n ): void {\n for (const value of Object.values(curObject)) {\n this.safeObjectVisit(value, obj => this.visitNode(obj, searchType, visitor));\n }\n if ((<{ type?: unknown }>curObject).type === searchType) {\n visitor(<NodeMapping[TypeFilter]> curObject);\n }\n }\n\n public visitNodeSpecific<\n Input extends object,\nTypeFilter extends keyof NodeMapping,\nSpecificType extends keyof SpecificNodes,\nSpecificNodes = NodeMapping[TypeFilter] extends Node & { subType: string } ?\n MapNodeSubTypeToImpls<NodeMapping[TypeFilter]> : never,\n >(\n curObject: Input,\n searchType: TypeFilter,\n searchSubType: SpecificType,\n visitor: (current: Readonly<SpecificNodes[SpecificType]>) => void,\n ): void {\n for (const value of Object.values(curObject)) {\n this.safeObjectVisit(value, obj => this.visitNodeSpecific(obj, searchType, searchSubType, visitor));\n }\n const cast = <{ type?: unknown; subType?: unknown }>curObject;\n if (cast.type === searchType && cast.subType === searchSubType) {\n visitor(<SpecificNodes[SpecificType]> curObject);\n }\n }\n\n private safeObjectVisit(value: unknown, mapper: (some: object) => any): any {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectVisit(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n}\n"]}
@@ -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,9 +9,12 @@ 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>> = {
19
- [K in Names]: RuleDefs[K] extends GeneratorRule<Context, K, infer RET, infer ARGS> ? (input: RET, context: Context, args: ARGS) => string : never;
16
+ [K in Names]: RuleDefs[K] extends GeneratorRule<Context, K, infer RET, infer ARGS> ? (input: RET, context: Context & {
17
+ origSource: string;
18
+ offset?: number;
19
+ }, args: ARGS) => string : never;
20
20
  };
@@ -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, 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,7 +1,22 @@
1
+ import { CoreFactory } from '../CoreFactory';
1
2
  import type { GenRuleMap } from './builderTypes';
3
+ import type { RuleDefArg } from './generatorTypes';
2
4
  export declare class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {
3
- private __context;
4
- setContext(context: Context): void;
5
- private getSafeContext;
5
+ protected rules: RuleDefs;
6
+ protected readonly factory: CoreFactory;
7
+ protected __context: Context | undefined;
8
+ protected origSource: string;
9
+ protected generatedUntil: number;
10
+ protected expectsSpace: boolean;
11
+ protected readonly stringBuilder: string[];
6
12
  constructor(rules: RuleDefs);
13
+ setContext(context: Context): void;
14
+ protected getSafeContext(): Context;
15
+ protected readonly subrule: RuleDefArg['SUBRULE'];
16
+ protected readonly handleLoc: RuleDefArg['HANDLE_LOC'];
17
+ protected readonly catchup: RuleDefArg['CATCHUP'];
18
+ protected readonly print: RuleDefArg['PRINT'];
19
+ private readonly printWord;
20
+ private readonly printSpaceLeft;
21
+ private readonly printWords;
7
22
  }
@@ -1,28 +1,115 @@
1
+ import { CoreFactory } from '../CoreFactory';
1
2
  export class DynamicGenerator {
2
- setContext(context) {
3
- this.__context = context;
4
- }
5
- getSafeContext() {
6
- return this.__context;
7
- }
8
3
  constructor(rules) {
4
+ this.rules = rules;
5
+ this.factory = new CoreFactory();
9
6
  this.__context = undefined;
10
- const selfRef = {
11
- SUBRULE: (cstDef, input, arg) => {
12
- const def = rules[cstDef.name];
13
- if (!def) {
14
- throw new Error(`Rule ${cstDef.name} not found`);
7
+ this.origSource = '';
8
+ this.generatedUntil = 0;
9
+ this.stringBuilder = [];
10
+ this.subrule = (cstDef, ast, arg) => {
11
+ const def = this.rules[cstDef.name];
12
+ if (!def) {
13
+ throw new Error(`Rule ${cstDef.name} not found`);
14
+ }
15
+ const generate = () => def.gImpl({
16
+ SUBRULE: this.subrule,
17
+ PRINT: this.print,
18
+ PRINT_SPACE_LEFT: this.printSpaceLeft,
19
+ PRINT_WORD: this.printWord,
20
+ PRINT_WORDS: this.printWords,
21
+ CATCHUP: this.catchup,
22
+ HANDLE_LOC: this.handleLoc,
23
+ })(ast, this.getSafeContext(), arg);
24
+ if (this.factory.isLocalized(ast)) {
25
+ this.handleLoc(ast, generate);
26
+ }
27
+ else {
28
+ generate();
29
+ }
30
+ };
31
+ this.handleLoc = (localized, handle) => {
32
+ if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
33
+ return;
34
+ }
35
+ if (this.factory.isSourceLocationStringReplace(localized.loc)) {
36
+ this.catchup(localized.loc.start);
37
+ this.print(localized.loc.newSource);
38
+ this.generatedUntil = localized.loc.end;
39
+ return;
40
+ }
41
+ if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
42
+ this.catchup(localized.loc.start);
43
+ this.generatedUntil = localized.loc.end;
44
+ }
45
+ if (this.factory.isSourceLocationSource(localized.loc)) {
46
+ this.catchup(localized.loc.start);
47
+ }
48
+ // If autoGenerate - do nothing
49
+ const ret = handle();
50
+ if (this.factory.isSourceLocationSource(localized.loc)) {
51
+ this.catchup(localized.loc.end);
52
+ }
53
+ return ret;
54
+ };
55
+ this.catchup = (until) => {
56
+ const start = this.generatedUntil;
57
+ if (start < until) {
58
+ this.print(this.origSource.slice(start, until));
59
+ }
60
+ this.generatedUntil = Math.max(this.generatedUntil, until);
61
+ };
62
+ this.print = (...args) => {
63
+ const pureArgs = args.filter(x => x.length > 0);
64
+ if (pureArgs.length > 0) {
65
+ const [head, ...tail] = pureArgs;
66
+ if (this.expectsSpace) {
67
+ this.expectsSpace = false;
68
+ const blanks = new Set(['\n', ' ', '\t']);
69
+ if (this.stringBuilder.length > 0 &&
70
+ !(blanks.has(head[0]) || blanks.has(this.stringBuilder.at(-1).at(-1)))) {
71
+ this.stringBuilder.push(' ');
72
+ }
15
73
  }
16
- return def.gImpl(selfRef)(input, this.getSafeContext(), arg);
17
- },
74
+ this.stringBuilder.push(head);
75
+ this.stringBuilder.push(...tail);
76
+ }
77
+ };
78
+ this.printWord = (...args) => {
79
+ this.expectsSpace = true;
80
+ this.print(...args);
81
+ this.expectsSpace = true;
82
+ };
83
+ this.printSpaceLeft = (...args) => {
84
+ this.expectsSpace = true;
85
+ this.print(...args);
86
+ };
87
+ this.printWords = (...args) => {
88
+ for (const arg of args) {
89
+ this.printWord(arg);
90
+ }
18
91
  };
19
92
  // eslint-disable-next-line ts/no-unnecessary-type-assertion
20
93
  for (const rule of Object.values(rules)) {
21
- this[rule.name] = ((input, context, args) => {
22
- this.setContext(context);
23
- return rule.gImpl(selfRef)(input, this.getSafeContext(), args);
24
- });
94
+ // Define function implementation
95
+ this[rule.name] =
96
+ ((input, context, args) => {
97
+ this.expectsSpace = false;
98
+ this.stringBuilder.length = 0;
99
+ this.origSource = context.origSource;
100
+ this.generatedUntil = context?.offset ?? 0;
101
+ this.setContext(context);
102
+ this.subrule(rule, input, args);
103
+ this.catchup(this.origSource.length);
104
+ return this.stringBuilder.join('');
105
+ });
25
106
  }
26
107
  }
108
+ setContext(context) {
109
+ this.__context = context;
110
+ }
111
+ getSafeContext() {
112
+ return this.__context;
113
+ }
27
114
  }
28
115
  //# sourceMappingURL=dynamicGenerator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dynamicGenerator.js","sourceRoot":"","sources":["dynamicGenerator.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,gBAAgB;IAGpB,UAAU,CAAC,OAAgB;QAChC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAEO,cAAc;QACpB,OAAiB,IAAI,CAAC,SAAS,CAAC;IAClC,CAAC;IAED,YAAmB,KAAe;QAV1B,cAAS,GAAwB,SAAS,CAAC;QAWjD,MAAM,OAAO,GAAe;YAC1B,OAAO,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;gBAC9B,MAAM,GAAG,GAAG,KAAK,CAAQ,MAAM,CAAC,IAAI,CAAC,CAAC;gBACtC,IAAI,CAAC,GAAG,EAAE,CAAC;oBACT,MAAM,IAAI,KAAK,CAAC,QAAQ,MAAM,CAAC,IAAI,YAAY,CAAC,CAAC;gBACnD,CAAC;gBACD,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,EAAE,GAAG,CAAC,CAAC;YAC/D,CAAC;SACF,CAAC;QAEF,4DAA4D;QAC5D,KAAK,MAAM,IAAI,IAAqB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACzD,IAAI,CAAsB,IAAI,CAAC,IAAI,CAAC,GAAQ,CAAC,CAAC,KAAU,EAAE,OAAgB,EAAE,IAAS,EAAE,EAAE;gBACvF,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;gBACzB,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,EAAE,EAAE,IAAI,CAAC,CAAC;YACjE,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF","sourcesContent":["import type { GenRuleMap } from './builderTypes';\nimport type { GeneratorRule, RuleDefArg } from './generatorTypes';\n\nexport class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n private __context: Context | undefined = undefined;\n\n public setContext(context: Context): void {\n this.__context = context;\n }\n\n private getSafeContext(): Context {\n return <Context> this.__context;\n }\n\n public constructor(rules: RuleDefs) {\n const selfRef: RuleDefArg = {\n SUBRULE: (cstDef, input, arg) => {\n const def = rules[<Names>cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n return def.gImpl(selfRef)(input, this.getSafeContext(), arg);\n },\n };\n\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <GeneratorRule[]>Object.values(rules)) {\n this[<keyof (typeof this)>rule.name] = <any>((input: any, context: Context, args: any) => {\n this.setContext(context);\n return rule.gImpl(selfRef)(input, this.getSafeContext(), args);\n });\n }\n }\n}\n"]}
1
+ {"version":3,"file":"dynamicGenerator.js","sourceRoot":"","sources":["dynamicGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAI7C,MAAM,OAAO,gBAAgB;IAQ3B,YAA6B,KAAe;QAAf,UAAK,GAAL,KAAK,CAAU;QAPzB,YAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QACrC,cAAS,GAAwB,SAAS,CAAC;QAC3C,eAAU,GAAG,EAAE,CAAC;QAChB,mBAAc,GAAG,CAAC,CAAC;QAEV,kBAAa,GAAa,EAAE,CAAC;QA+B7B,YAAO,GAA0B,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;YACvE,MAAM,GAAG,GAAG,I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tB,CAAC,CAAC;QAEe,eAAU,GAA6B,CAAC,GAAG,IAAI,EAAE,EAAE;YAClE,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;gBACvB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;YACtB,CAAC;QACH,CAAC,CAAC;QAtHA,4DAA4D;QAC5D,KAAK,MAAM,IAAI,IAAsB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1D,iCAAiC;YACjC,IAAI,CAAuB,IAAI,CAAC,IAAI,CAAC;gBAC7B,CAAC,CAAC,KAAU,EAAE,OAA0D,EAAE,IAAS,EAAE,EAAE;oBAC3F,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;oBAC1B,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC;oBAC9B,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;oBACrC,IAAI,CAAC,cAAc,GAAG,OAAO,EAAE,MAAM,IAAI,CAAC,CAAC;oBAC3C,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;oBAEzB,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;oBAEhC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;oBAErC,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAC;QACP,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;CA6FF","sourcesContent":["import { CoreFactory } from '../CoreFactory';\nimport type { GenRuleMap } from './builderTypes';\nimport type { GeneratorRule, RuleDefArg } from './generatorTypes';\n\nexport class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n protected readonly factory = new CoreFactory();\n protected __context: Context | undefined = undefined;\n protected origSource = '';\n protected generatedUntil = 0;\n protected expectsSpace: boolean;\n protected readonly stringBuilder: string[] = [];\n\n public constructor(protected rules: RuleDefs) {\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <GeneratorRule[]> Object.values(rules)) {\n // Define function implementation\n this[<keyof (typeof this)> rule.name] =\n <any> ((input: any, context: Context & { origSource: string; offset?: number }, args: any) => {\n this.expectsSpace = false;\n this.stringBuilder.length = 0;\n this.origSource = context.origSource;\n this.generatedUntil = context?.offset ?? 0;\n this.setContext(context);\n\n this.subrule(rule, input, args);\n\n this.catchup(this.origSource.length);\n\n return this.stringBuilder.join('');\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: RuleDefArg['SUBRULE'] = (cstDef, ast, arg) => {\n const def = this.rules[<Names> cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n\n const generate = (): void => def.gImpl({\n SUBRULE: this.subrule,\n PRINT: this.print,\n PRINT_SPACE_LEFT: this.printSpaceLeft,\n PRINT_WORD: this.printWord,\n PRINT_WORDS: this.printWords,\n CATCHUP: this.catchup,\n HANDLE_LOC: this.handleLoc,\n })(ast, this.getSafeContext(), arg);\n\n if (this.factory.isLocalized(ast)) {\n this.handleLoc(ast, generate);\n } else {\n generate();\n }\n };\n\n protected readonly handleLoc: RuleDefArg['HANDLE_LOC'] = (localized, handle) => {\n if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {\n return;\n }\n if (this.factory.isSourceLocationStringReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.print(localized.loc.newSource);\n this.generatedUntil = localized.loc.end;\n return;\n }\n if (this.factory.isSourceLocationNodeReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.generatedUntil = localized.loc.end;\n }\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.start);\n }\n // If autoGenerate - do nothing\n\n const ret = handle();\n\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.end);\n }\n return ret;\n };\n\n protected readonly catchup: RuleDefArg['CATCHUP'] = (until) => {\n const start = this.generatedUntil;\n if (start < until) {\n this.print(this.origSource.slice(start, until));\n }\n this.generatedUntil = Math.max(this.generatedUntil, until);\n };\n\n protected readonly print: RuleDefArg['PRINT'] = (...args) => {\n const pureArgs = args.filter(x => x.length > 0);\n if (pureArgs.length > 0) {\n const [ head, ...tail ] = pureArgs;\n if (this.expectsSpace) {\n this.expectsSpace = false;\n const blanks = new Set([ '\\n', ' ', '\\t' ]);\n if (this.stringBuilder.length > 0 &&\n !(blanks.has(head[0]) || blanks.has(this.stringBuilder.at(-1)!.at(-1)!))) {\n this.stringBuilder.push(' ');\n }\n }\n this.stringBuilder.push(head);\n this.stringBuilder.push(...tail);\n }\n };\n\n private readonly printWord: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n this.expectsSpace = true;\n };\n\n private readonly printSpaceLeft: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n };\n\n private readonly printWords: RuleDefArg['PRINT_WORD'] = (...args) => {\n for (const arg of args) {\n this.printWord(arg);\n }\n };\n}\n"]}
@@ -1,15 +1,17 @@
1
+ import type { ParseNamesFromList } from '../parser-builder/builderTypes';
1
2
  import type { CheckOverlap } from '../utils';
2
- import type { GeneratorFromRules, GenNamesFromList, GenRuleMap, GenRulesToObject } 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
  /**
6
7
  * Create a GeneratorBuilder from some initial grammar rules or an existing GeneratorBuilder.
7
8
  * If a GeneratorBuilder is provided, a new copy will be created.
8
9
  */
9
- static createBuilder<Context, Names extends string, RuleDefs extends GenRuleMap<Names>>(args: GeneratorBuilder<Context, Names, RuleDefs>): GeneratorBuilder<Context, Names, RuleDefs>;
10
- static createBuilder<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>;
10
+ static create<Context, Names extends string, RuleDefs extends GenRuleMap<Names>>(args: GeneratorBuilder<Context, Names, RuleDefs>): 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();
14
+ widenContext<NewContext extends Context>(): GeneratorBuilder<NewContext, Names, RuleDefs>;
13
15
  typePatch<Patch extends {
14
16
  [Key in Names]?: any;
15
17
  }>(): GeneratorBuilder<Context, Names, {
@@ -33,8 +35,8 @@ export declare class GeneratorBuilder<Context, Names extends string, RuleDefs ex
33
35
  addRule<U extends string, RET, ARGS>(rule: CheckOverlap<U, Names, GeneratorRule<Context, U, RET, ARGS>>): GeneratorBuilder<Context, Names | U, {
34
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);
35
37
  }>;
36
- addMany<U extends readonly GeneratorRule<Context>[]>(...rules: CheckOverlap<GenNamesFromList<U>, Names, U>): GeneratorBuilder<Context, Names | GenNamesFromList<U>, {
37
- [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);
38
40
  }>;
39
41
  /**
40
42
  * Delete a grammar rule by its name.
@@ -51,8 +53,8 @@ export declare class GeneratorBuilder<Context, Names extends string, RuleDefs ex
51
53
  * To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
52
54
  * this rule implementation will be used.
53
55
  */
54
- 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>, {
55
- [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);
56
58
  }>;
57
59
  build(): GeneratorFromRules<Context, Names, RuleDefs>;
58
60
  }
@@ -10,7 +10,7 @@ function listToRuleDefMap(rules) {
10
10
  return newRules;
11
11
  }
12
12
  export class GeneratorBuilder {
13
- static createBuilder(start) {
13
+ static create(start) {
14
14
  if (start instanceof GeneratorBuilder) {
15
15
  return new GeneratorBuilder({ ...start.rules });
16
16
  }
@@ -19,6 +19,9 @@ export class GeneratorBuilder {
19
19
  constructor(startRules) {
20
20
  this.rules = startRules;
21
21
  }
22
+ widenContext() {
23
+ return this;
24
+ }
22
25
  typePatch() {
23
26
  return this;
24
27
  }