@traqula/core 0.0.23 → 0.0.25

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 (75) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/lib/AstCoreFactory.js +181 -149
  3. package/dist/cjs/lib/AstCoreFactory.js.map +1 -0
  4. package/dist/cjs/lib/generator-builder/builderTypes.js +2 -15
  5. package/dist/cjs/lib/generator-builder/builderTypes.js.map +1 -0
  6. package/dist/cjs/lib/generator-builder/dynamicGenerator.js +209 -200
  7. package/dist/cjs/lib/generator-builder/dynamicGenerator.js.map +1 -0
  8. package/dist/cjs/lib/generator-builder/generatorBuilder.js +103 -117
  9. package/dist/cjs/lib/generator-builder/generatorBuilder.js.map +1 -0
  10. package/dist/cjs/lib/generator-builder/generatorTypes.js +2 -15
  11. package/dist/cjs/lib/generator-builder/generatorTypes.js.map +1 -0
  12. package/dist/cjs/lib/index.js +39 -57
  13. package/dist/cjs/lib/index.js.map +1 -0
  14. package/dist/cjs/lib/indirection-builder/IndirBuilder.js +63 -85
  15. package/dist/cjs/lib/indirection-builder/IndirBuilder.js.map +1 -0
  16. package/dist/cjs/lib/indirection-builder/dynamicIndirected.js +28 -50
  17. package/dist/cjs/lib/indirection-builder/dynamicIndirected.js.map +1 -0
  18. package/dist/cjs/lib/indirection-builder/helpers.js +11 -31
  19. package/dist/cjs/lib/indirection-builder/helpers.js.map +1 -0
  20. package/dist/cjs/lib/lexer-builder/LexerBuilder.js +92 -114
  21. package/dist/cjs/lib/lexer-builder/LexerBuilder.js.map +1 -0
  22. package/dist/cjs/lib/parser-builder/builderTypes.js +2 -15
  23. package/dist/cjs/lib/parser-builder/builderTypes.js.map +1 -0
  24. package/dist/cjs/lib/parser-builder/dynamicParser.js +116 -136
  25. package/dist/cjs/lib/parser-builder/dynamicParser.js.map +1 -0
  26. package/dist/cjs/lib/parser-builder/parserBuilder.js +167 -169
  27. package/dist/cjs/lib/parser-builder/parserBuilder.js.map +1 -0
  28. package/dist/cjs/lib/parser-builder/ruleDefTypes.js +2 -15
  29. package/dist/cjs/lib/parser-builder/ruleDefTypes.js.map +1 -0
  30. package/dist/cjs/lib/transformers/TransformerObject.js +196 -198
  31. package/dist/cjs/lib/transformers/TransformerObject.js.map +1 -0
  32. package/dist/cjs/lib/transformers/TransformerSubTyped.js +110 -141
  33. package/dist/cjs/lib/transformers/TransformerSubTyped.js.map +1 -0
  34. package/dist/cjs/lib/transformers/TransformerTyped.js +87 -120
  35. package/dist/cjs/lib/transformers/TransformerTyped.js.map +1 -0
  36. package/dist/cjs/lib/types.js +2 -15
  37. package/dist/cjs/lib/types.js.map +1 -0
  38. package/dist/cjs/lib/utils.js +20 -34
  39. package/dist/cjs/lib/utils.js.map +1 -0
  40. package/dist/esm/lib/AstCoreFactory.d.ts +19 -7
  41. package/dist/esm/lib/AstCoreFactory.js +50 -5
  42. package/dist/esm/lib/AstCoreFactory.js.map +1 -1
  43. package/dist/esm/lib/generator-builder/dynamicGenerator.d.ts +5 -0
  44. package/dist/esm/lib/generator-builder/dynamicGenerator.js +20 -0
  45. package/dist/esm/lib/generator-builder/dynamicGenerator.js.map +1 -1
  46. package/dist/esm/lib/parser-builder/dynamicParser.js +1 -0
  47. package/dist/esm/lib/parser-builder/dynamicParser.js.map +1 -1
  48. package/dist/esm/lib/parser-builder/parserBuilder.js +2 -2
  49. package/dist/esm/lib/parser-builder/parserBuilder.js.map +1 -1
  50. package/dist/esm/lib/transformers/TransformerObject.d.ts +1 -1
  51. package/dist/esm/lib/transformers/TransformerObject.js.map +1 -1
  52. package/dist/esm/lib/transformers/TransformerSubTyped.d.ts +31 -22
  53. package/dist/esm/lib/transformers/TransformerSubTyped.js +30 -40
  54. package/dist/esm/lib/transformers/TransformerSubTyped.js.map +1 -1
  55. package/dist/esm/lib/transformers/TransformerTyped.d.ts +24 -20
  56. package/dist/esm/lib/transformers/TransformerTyped.js +23 -35
  57. package/dist/esm/lib/transformers/TransformerTyped.js.map +1 -1
  58. package/dist/esm/lib/types.d.ts +67 -5
  59. package/dist/esm/lib/types.js.map +1 -1
  60. package/dist/esm/lib/utils.d.ts +10 -0
  61. package/dist/esm/lib/utils.js +7 -0
  62. package/dist/esm/lib/utils.js.map +1 -1
  63. package/package.json +9 -9
  64. package/dist/cjs/test/generatorBuilder/generatorBuilder.types.test.js +0 -30
  65. package/dist/cjs/test/grammar-builder/parserBuilder.types.test.js +0 -27
  66. package/dist/cjs/test/transformer.test.js +0 -82
  67. package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.d.ts +0 -1
  68. package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.js +0 -38
  69. package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.js.map +0 -1
  70. package/dist/esm/test/grammar-builder/parserBuilder.types.test.d.ts +0 -1
  71. package/dist/esm/test/grammar-builder/parserBuilder.types.test.js +0 -38
  72. package/dist/esm/test/grammar-builder/parserBuilder.types.test.js.map +0 -1
  73. package/dist/esm/test/transformer.test.d.ts +0 -1
  74. package/dist/esm/test/transformer.test.js +0 -82
  75. package/dist/esm/test/transformer.test.js.map +0 -1
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Traqula core package
2
2
 
3
+ [![npm version](https://badge.fury.io/js/@traqula%2Fcore.svg)](https://www.npmjs.com/package/@traqula/core)
4
+
3
5
  Traqula core contains core components of Traqula.
4
6
  Most importantly, its [lexer builder](./lib/lexer-builder/LexerBuilder.ts), [parser builder](./lib/parser-builder/parserBuilder.ts), and [generator builder](./lib/generator-builder/generatorBuilder.ts).
5
7
  This library heavily relies on the amazing [Chevrotain package](https://chevrotain.io/docs/).
@@ -1,152 +1,184 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var AstCoreFactory_exports = {};
20
- __export(AstCoreFactory_exports, {
21
- AstCoreFactory: () => AstCoreFactory
22
- });
23
- module.exports = __toCommonJS(AstCoreFactory_exports);
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstCoreFactory = void 0;
24
4
  class AstCoreFactory {
25
- wrap(val, loc) {
26
- return { val, loc };
27
- }
28
- isLocalized(obj) {
29
- return typeof obj === "object" && obj !== null && "loc" in obj && typeof obj.loc === "object" && obj.loc !== null && "sourceLocationType" in obj.loc;
30
- }
31
- sourceLocation(...elements) {
32
- const pureElements = elements.filter((x) => x !== void 0);
33
- if (pureElements.length === 0) {
34
- return this.sourceLocationNoMaterialize();
35
- }
36
- const filtered = pureElements.filter((element) => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) || this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
37
- if (filtered.length === 0) {
38
- return this.gen();
39
- }
40
- const first = filtered.at(0);
41
- const last = filtered.at(-1);
42
- return {
43
- sourceLocationType: "source",
44
- start: this.isLocalized(first) ? first.loc.start : first.startOffset,
45
- end: this.isLocalized(last) ? last.loc.end : last.endOffset + 1
46
- };
47
- }
48
- sourceLocationNoMaterialize() {
49
- return { sourceLocationType: "noMaterialize" };
50
- }
51
- /**
52
- * Returns a copy of the argument that is not materialized
53
- */
54
- dematerialized(arg) {
55
- return { ...arg, loc: this.sourceLocationNoMaterialize() };
56
- }
57
- safeObjectTransform(value, mapper) {
58
- if (value && typeof value === "object") {
59
- if (Array.isArray(value)) {
60
- return value.map((x) => this.safeObjectTransform(x, mapper));
61
- }
62
- return mapper(value);
63
- }
64
- return value;
65
- }
66
- forcedAutoGenTree(obj) {
67
- const copy = { ...obj };
68
- for (const [key, value] of Object.entries(copy)) {
69
- copy[key] = this.safeObjectTransform(value, (obj2) => this.forcedAutoGenTree(obj2));
70
- }
71
- if (this.isLocalized(copy)) {
72
- copy.loc = this.gen();
73
- }
74
- return copy;
75
- }
76
- forceMaterialized(arg) {
77
- if (this.isSourceLocationNoMaterialize(arg.loc)) {
78
- return this.forcedAutoGenTree(arg);
79
- }
80
- return { ...arg };
81
- }
82
- isSourceLocation(loc) {
83
- return "sourceLocationType" in loc;
84
- }
85
- sourceLocationSource(start, end) {
86
- return {
87
- sourceLocationType: "source",
88
- start,
89
- end
90
- };
91
- }
92
- gen() {
93
- return { sourceLocationType: "autoGenerate" };
94
- }
95
- isSourceLocationSource(loc) {
96
- return this.isSourceLocation(loc) && loc.sourceLocationType === "source";
97
- }
98
- isSourceLocationStringReplace(loc) {
99
- return this.isSourceLocation(loc) && loc.sourceLocationType === "stringReplace";
100
- }
101
- sourceLocationNodeReplaceUnsafe(loc) {
102
- if (this.isSourceLocationSource(loc)) {
103
- return this.sourceLocationNodeReplace(loc);
104
- }
105
- throw new Error("not possible");
106
- }
107
- sourceLocationNodeReplace(startOrLoc, end) {
108
- let starting;
109
- let ending;
110
- if (typeof startOrLoc === "number") {
111
- starting = startOrLoc;
112
- ending = end;
113
- } else {
114
- starting = startOrLoc.start;
115
- ending = startOrLoc.end;
116
- }
117
- return {
118
- sourceLocationType: "nodeReplace",
119
- start: starting,
120
- end: ending
121
- };
122
- }
123
- isSourceLocationNodeReplace(loc) {
124
- return this.isSourceLocation(loc) && loc.sourceLocationType === "nodeReplace";
125
- }
126
- isSourceLocationNodeAutoGenerate(loc) {
127
- return this.isSourceLocation(loc) && loc.sourceLocationType === "autoGenerate";
128
- }
129
- nodeShouldPrint(node) {
130
- return this.isSourceLocationNodeReplace(node.loc) || this.isSourceLocationNodeAutoGenerate(node.loc);
131
- }
132
- printFilter(node, callback) {
133
- if (this.nodeShouldPrint(node)) {
134
- callback();
135
- }
136
- }
137
- isSourceLocationNoMaterialize(loc) {
138
- return this.isSourceLocation(loc) && loc.sourceLocationType === "noMaterialize";
139
- }
140
- isOfType(obj, type) {
141
- const casted = obj;
142
- return casted.type === type;
143
- }
144
- isOfSubType(obj, type, subType) {
145
- const temp = obj;
146
- return temp.type === type && temp.subType === subType;
147
- }
5
+ tracksSourceLocation;
6
+ constructor(args = {}) {
7
+ this.tracksSourceLocation = args.tracksSourceLocation ?? true;
8
+ }
9
+ wrap(val, loc) {
10
+ return { val, loc };
11
+ }
12
+ isLocalized(obj) {
13
+ return typeof obj === 'object' && obj !== null && 'loc' in obj &&
14
+ typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;
15
+ }
16
+ sourceLocation(...elements) {
17
+ if (!this.tracksSourceLocation) {
18
+ return this.gen();
19
+ }
20
+ const pureElements = elements.filter(x => x !== undefined);
21
+ if (pureElements.length === 0) {
22
+ return this.sourceLocationNoMaterialize();
23
+ }
24
+ const filtered = pureElements.filter(element => !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||
25
+ this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));
26
+ if (filtered.length === 0) {
27
+ return this.gen();
28
+ }
29
+ const first = filtered.at(0);
30
+ const last = filtered.at(-1);
31
+ return {
32
+ sourceLocationType: 'source',
33
+ start: this.isLocalized(first) ?
34
+ first.loc.start :
35
+ first.startOffset,
36
+ end: this.isLocalized(last) ?
37
+ last.loc.end :
38
+ (last.endOffset + 1),
39
+ };
40
+ }
41
+ sourceLocationNoMaterialize() {
42
+ if (!this.tracksSourceLocation) {
43
+ return this.gen();
44
+ }
45
+ return { sourceLocationType: 'noMaterialize' };
46
+ }
47
+ /**
48
+ * Returns a copy of the argument that is not materialized
49
+ */
50
+ dematerialized(arg) {
51
+ return { ...arg, loc: this.sourceLocationNoMaterialize() };
52
+ }
53
+ safeObjectTransform(value, mapper) {
54
+ if (value && typeof value === 'object') {
55
+ // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects
56
+ if (Array.isArray(value)) {
57
+ return value.map(x => this.safeObjectTransform(x, mapper));
58
+ }
59
+ return mapper(value);
60
+ }
61
+ return value;
62
+ }
63
+ forcedAutoGenTree(obj) {
64
+ const copy = { ...obj };
65
+ for (const [key, value] of Object.entries(copy)) {
66
+ copy[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));
67
+ }
68
+ if (this.isLocalized(copy)) {
69
+ copy.loc = this.gen();
70
+ }
71
+ return copy;
72
+ }
73
+ forceMaterialized(arg) {
74
+ if (this.isSourceLocationNoMaterialize(arg.loc)) {
75
+ return this.forcedAutoGenTree(arg);
76
+ }
77
+ return { ...arg };
78
+ }
79
+ isSourceLocation(loc) {
80
+ return 'sourceLocationType' in loc;
81
+ }
82
+ sourceLocationSource(start, end) {
83
+ return {
84
+ sourceLocationType: 'source',
85
+ start,
86
+ end,
87
+ };
88
+ }
89
+ /**
90
+ * {@inheritDoc SourceLocationInlinedSource}
91
+ */
92
+ sourceLocationInlinedSource(newSource, subLoc, start, end, startOnNew = 0, endOnNew = newSource.length) {
93
+ if (!this.tracksSourceLocation) {
94
+ return this.gen();
95
+ }
96
+ if (this.isSourceLocationSource(subLoc)) {
97
+ startOnNew = subLoc.start;
98
+ endOnNew = subLoc.end;
99
+ }
100
+ return {
101
+ sourceLocationType: 'inlinedSource',
102
+ newSource,
103
+ start,
104
+ end,
105
+ loc: subLoc,
106
+ startOnNew,
107
+ endOnNew,
108
+ };
109
+ }
110
+ ;
111
+ isSourceLocationInlinedSource(loc) {
112
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'inlinedSource';
113
+ }
114
+ gen() {
115
+ return { sourceLocationType: 'autoGenerate' };
116
+ }
117
+ isSourceLocationSource(loc) {
118
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';
119
+ }
120
+ sourceLocationStringReplace(newSource, start, end) {
121
+ if (!this.tracksSourceLocation) {
122
+ return this.gen();
123
+ }
124
+ return { sourceLocationType: 'stringReplace', newSource, start, end };
125
+ }
126
+ isSourceLocationStringReplace(loc) {
127
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';
128
+ }
129
+ sourceLocationNodeReplaceUnsafe(loc) {
130
+ if (this.isSourceLocationSource(loc)) {
131
+ return this.sourceLocationNodeReplace(loc);
132
+ }
133
+ if (this.isSourceLocationInlinedSource(loc)) {
134
+ return this.sourceLocationNodeReplaceUnsafe(loc.loc);
135
+ }
136
+ throw new Error(`Cannot convert SourceLocation of type ${loc.sourceLocationType} to SourceLocationNodeReplace`);
137
+ }
138
+ sourceLocationNodeReplace(startOrLoc, end) {
139
+ let starting;
140
+ let ending;
141
+ if (typeof startOrLoc === 'number') {
142
+ starting = startOrLoc;
143
+ ending = end;
144
+ }
145
+ else {
146
+ starting = startOrLoc.start;
147
+ ending = startOrLoc.end;
148
+ }
149
+ return {
150
+ sourceLocationType: 'nodeReplace',
151
+ start: starting,
152
+ end: ending,
153
+ };
154
+ }
155
+ isSourceLocationNodeReplace(loc) {
156
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';
157
+ }
158
+ isSourceLocationNodeAutoGenerate(loc) {
159
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';
160
+ }
161
+ isPrintingLoc(loc) {
162
+ return this.isSourceLocationNodeReplace(loc) ||
163
+ this.isSourceLocationNodeAutoGenerate(loc) ||
164
+ (this.isSourceLocationInlinedSource(loc) && this.isPrintingLoc(loc.loc));
165
+ }
166
+ printFilter(node, callback) {
167
+ if (this.isPrintingLoc(node.loc)) {
168
+ callback();
169
+ }
170
+ }
171
+ isSourceLocationNoMaterialize(loc) {
172
+ return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';
173
+ }
174
+ isOfType(obj, type) {
175
+ const casted = obj;
176
+ return casted.type === type;
177
+ }
178
+ isOfSubType(obj, type, subType) {
179
+ const temp = obj;
180
+ return temp.type === type && temp.subType === subType;
181
+ }
148
182
  }
149
- // Annotate the CommonJS export names for ESM import in node:
150
- 0 && (module.exports = {
151
- AstCoreFactory
152
- });
183
+ exports.AstCoreFactory = AstCoreFactory;
184
+ //# sourceMappingURL=AstCoreFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AstCoreFactory.js","sourceRoot":"","sources":["../../../lib/AstCoreFactory.ts"],"names":[],"mappings":";;;AAwBA,MAAa,cAAc;IAClB,oBAAoB,CAAU;IACrC,YAAmB,OAAoC,EAAE;QACvD,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC;IAChE,CAAC;IAEM,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,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC;QACpB,CAAC;QAED,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3D,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC,2BAA2B,EAAE,CAAC;QAC5C,CAAC;QAED,MAAM,QAAQ,GAAG,YAAY,CAAC,MAA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Aa,CAAC,GAAmB;QACtC,OAAO,IAAI,CAAC,2BAA2B,CAAC,GAAG,CAAC;YAC1C,IAAI,CAAC,gCAAgC,CAAC,GAAG,CAAC;YAC1C,CAAC,IAAI,CAAC,6BAA6B,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7E,CAAC;IAEM,WAAW,CAAC,IAAe,EAAE,QAAoB;QACtD,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACjC,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;AAtND,wCAsNC","sourcesContent":["import type { IToken } from '@traqula/chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n Typed,\n SubTyped,\n SourceLocationInlinedSource,\n} from './types.js';\n\nexport interface AstCoreFactoryArgs {\n /**\n * Whether the AstFactory can track sources, if not, the sourceLocation function returns autoGen. Default true\n */\n tracksSourceLocation: boolean;\n}\n\nexport class AstCoreFactory implements AstCoreFactoryArgs {\n public tracksSourceLocation: boolean;\n public constructor(args: Partial<AstCoreFactoryArgs> = {}) {\n this.tracksSourceLocation = args.tracksSourceLocation ?? true;\n }\n\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 if (!this.tracksSourceLocation) {\n return this.gen();\n }\n\n const pureElements = elements.filter(x => x !== undefined);\n if (pureElements.length === 0) {\n return this.sourceLocationNoMaterialize();\n }\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.at(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(): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\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 {\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 /**\n * {@inheritDoc SourceLocationInlinedSource}\n */\n public sourceLocationInlinedSource(\n newSource: string,\n subLoc: SourceLocation,\n start: number,\n end: number,\n startOnNew = 0,\n endOnNew: number = newSource.length,\n ): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n if (this.isSourceLocationSource(subLoc)) {\n startOnNew = subLoc.start;\n endOnNew = subLoc.end;\n }\n return {\n sourceLocationType: 'inlinedSource',\n newSource,\n start,\n end,\n loc: subLoc,\n startOnNew,\n endOnNew,\n }satisfies SourceLocationInlinedSource;\n };\n\n public isSourceLocationInlinedSource(loc: object): loc is SourceLocationInlinedSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'inlinedSource';\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 sourceLocationStringReplace(newSource: string, start: number, end: number): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n return { sourceLocationType: 'stringReplace', newSource, start, end } satisfies SourceLocationStringReplace;\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 if (this.isSourceLocationInlinedSource(loc)) {\n return this.sourceLocationNodeReplaceUnsafe(loc.loc);\n }\n throw new Error(`Cannot convert SourceLocation of type ${loc.sourceLocationType} to SourceLocationNodeReplace`);\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 isPrintingLoc(loc: SourceLocation): boolean {\n return this.isSourceLocationNodeReplace(loc) ||\n this.isSourceLocationNodeAutoGenerate(loc) ||\n (this.isSourceLocationInlinedSource(loc) && this.isPrintingLoc(loc.loc));\n }\n\n public printFilter(node: Localized, callback: () => void): void {\n if (this.isPrintingLoc(node.loc)) {\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"]}
@@ -1,16 +1,3 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var builderTypes_exports = {};
16
- module.exports = __toCommonJS(builderTypes_exports);
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=builderTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["../../../../lib/generator-builder/builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParseNamesFromList } from '../parser-builder/builderTypes.js';\nimport type { GeneratorRule } from './generatorTypes.js';\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"]}