@traqula/core 0.0.22 → 0.0.24
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/dist/cjs/lib/AstCoreFactory.js +136 -149
- package/dist/cjs/lib/AstCoreFactory.js.map +1 -0
- package/dist/cjs/lib/generator-builder/builderTypes.js +2 -15
- package/dist/cjs/lib/generator-builder/builderTypes.js.map +1 -0
- package/dist/cjs/lib/generator-builder/dynamicGenerator.js +189 -200
- package/dist/cjs/lib/generator-builder/dynamicGenerator.js.map +1 -0
- package/dist/cjs/lib/generator-builder/generatorBuilder.js +103 -117
- package/dist/cjs/lib/generator-builder/generatorBuilder.js.map +1 -0
- package/dist/cjs/lib/generator-builder/generatorTypes.js +2 -15
- package/dist/cjs/lib/generator-builder/generatorTypes.js.map +1 -0
- package/dist/cjs/lib/index.js +39 -57
- package/dist/cjs/lib/index.js.map +1 -0
- package/dist/cjs/lib/indirection-builder/IndirBuilder.js +63 -85
- package/dist/cjs/lib/indirection-builder/IndirBuilder.js.map +1 -0
- package/dist/cjs/lib/indirection-builder/dynamicIndirected.js +28 -50
- package/dist/cjs/lib/indirection-builder/dynamicIndirected.js.map +1 -0
- package/dist/cjs/lib/indirection-builder/helpers.js +11 -31
- package/dist/cjs/lib/indirection-builder/helpers.js.map +1 -0
- package/dist/cjs/lib/lexer-builder/LexerBuilder.js +92 -114
- package/dist/cjs/lib/lexer-builder/LexerBuilder.js.map +1 -0
- package/dist/cjs/lib/parser-builder/builderTypes.js +2 -15
- package/dist/cjs/lib/parser-builder/builderTypes.js.map +1 -0
- package/dist/cjs/lib/parser-builder/dynamicParser.js +115 -136
- package/dist/cjs/lib/parser-builder/dynamicParser.js.map +1 -0
- package/dist/cjs/lib/parser-builder/parserBuilder.js +167 -169
- package/dist/cjs/lib/parser-builder/parserBuilder.js.map +1 -0
- package/dist/cjs/lib/parser-builder/ruleDefTypes.js +2 -15
- package/dist/cjs/lib/parser-builder/ruleDefTypes.js.map +1 -0
- package/dist/cjs/lib/transformers/TransformerObject.js +196 -198
- package/dist/cjs/lib/transformers/TransformerObject.js.map +1 -0
- package/dist/cjs/lib/transformers/TransformerSubTyped.js +120 -141
- package/dist/cjs/lib/transformers/TransformerSubTyped.js.map +1 -0
- package/dist/cjs/lib/transformers/TransformerTyped.js +99 -120
- package/dist/cjs/lib/transformers/TransformerTyped.js.map +1 -0
- package/dist/cjs/lib/types.js +2 -15
- package/dist/cjs/lib/types.js.map +1 -0
- package/dist/cjs/lib/utils.js +13 -34
- package/dist/cjs/lib/utils.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/package.json +9 -8
- package/dist/cjs/test/generatorBuilder/generatorBuilder.types.test.js +0 -30
- package/dist/cjs/test/grammar-builder/parserBuilder.types.test.js +0 -27
- package/dist/cjs/test/transformer.test.js +0 -82
- package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.d.ts +0 -1
- package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.js +0 -38
- package/dist/esm/test/generatorBuilder/generatorBuilder.types.test.js.map +0 -1
- package/dist/esm/test/grammar-builder/parserBuilder.types.test.d.ts +0 -1
- package/dist/esm/test/grammar-builder/parserBuilder.types.test.js +0 -38
- package/dist/esm/test/grammar-builder/parserBuilder.types.test.js.map +0 -1
- package/dist/esm/test/transformer.test.d.ts +0 -1
- package/dist/esm/test/transformer.test.js +0 -82
- package/dist/esm/test/transformer.test.js.map +0 -1
|
@@ -1,207 +1,196 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 dynamicGenerator_exports = {};
|
|
20
|
-
__export(dynamicGenerator_exports, {
|
|
21
|
-
DynamicGenerator: () => DynamicGenerator
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(dynamicGenerator_exports);
|
|
24
|
-
var import_AstCoreFactory = require("../AstCoreFactory.js");
|
|
25
|
-
var import_utils = require("../utils.js");
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DynamicGenerator = void 0;
|
|
4
|
+
const AstCoreFactory_js_1 = require("../AstCoreFactory.js");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
26
6
|
class DynamicGenerator {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
getSafeContext() {
|
|
56
|
-
return this.__context;
|
|
57
|
-
}
|
|
58
|
-
subrule = (cstDef, ast, ...arg) => {
|
|
59
|
-
const def = this.rules[cstDef.name];
|
|
60
|
-
if (!def) {
|
|
61
|
-
throw new Error(`Rule ${cstDef.name} not found`);
|
|
62
|
-
}
|
|
63
|
-
const generate = () => def.gImpl({
|
|
64
|
-
SUBRULE: this.subrule,
|
|
65
|
-
PRINT: this.print,
|
|
66
|
-
ENSURE: this.ensure,
|
|
67
|
-
ENSURE_EITHER: this.ensureEither,
|
|
68
|
-
NEW_LINE: this.newLine,
|
|
69
|
-
HANDLE_LOC: this.handleLoc,
|
|
70
|
-
CATCHUP: this.catchup,
|
|
71
|
-
PRINT_WORD: this.printWord,
|
|
72
|
-
PRINT_WORDS: this.printWords,
|
|
73
|
-
PRINT_ON_EMPTY: this.printOnEmpty,
|
|
74
|
-
PRINT_ON_OWN_LINE: this.printOnOwnLine
|
|
75
|
-
})(ast, this.getSafeContext(), ...arg);
|
|
76
|
-
if (this.factory.isLocalized(ast)) {
|
|
77
|
-
this.handleLoc(ast, generate);
|
|
78
|
-
} else {
|
|
79
|
-
generate();
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
handleLoc = (localized, handle) => {
|
|
83
|
-
if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (this.factory.isSourceLocationStringReplace(localized.loc)) {
|
|
87
|
-
this.catchup(localized.loc.start);
|
|
88
|
-
this.print(localized.loc.newSource);
|
|
89
|
-
this.generatedUntil = localized.loc.end;
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
|
|
93
|
-
this.catchup(localized.loc.start);
|
|
94
|
-
this.generatedUntil = localized.loc.end;
|
|
95
|
-
}
|
|
96
|
-
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
97
|
-
this.catchup(localized.loc.start);
|
|
98
|
-
}
|
|
99
|
-
const ret = handle();
|
|
100
|
-
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
101
|
-
this.catchup(localized.loc.end);
|
|
102
|
-
}
|
|
103
|
-
return ret;
|
|
104
|
-
};
|
|
105
|
-
catchup = (until) => {
|
|
106
|
-
const start = this.generatedUntil;
|
|
107
|
-
if (start < until) {
|
|
108
|
-
this.print(this.origSource.slice(start, until));
|
|
109
|
-
}
|
|
110
|
-
this.generatedUntil = Math.max(this.generatedUntil, until);
|
|
111
|
-
};
|
|
112
|
-
handeEnsured(toPrint) {
|
|
113
|
-
for (const callBack of this.toEnsure) {
|
|
114
|
-
callBack(toPrint);
|
|
115
|
-
}
|
|
116
|
-
this.toEnsure.length = 0;
|
|
117
|
-
}
|
|
118
|
-
print = (...args) => {
|
|
119
|
-
const joined = args.join("");
|
|
120
|
-
this.handeEnsured(joined);
|
|
121
|
-
this.stringBuilder.push(joined);
|
|
122
|
-
};
|
|
123
|
-
doesEndWith(subsStr) {
|
|
124
|
-
const len = subsStr.length;
|
|
125
|
-
let temp = "";
|
|
126
|
-
while (temp.length < len && this.stringBuilder.length > 0) {
|
|
127
|
-
temp = this.stringBuilder.pop() + temp;
|
|
128
|
-
}
|
|
129
|
-
this.stringBuilder.push(temp);
|
|
130
|
-
return temp.endsWith(subsStr);
|
|
131
|
-
}
|
|
132
|
-
ensure = (...args) => {
|
|
133
|
-
const toEnsure = args.join("");
|
|
134
|
-
if (!this.doesEndWith(toEnsure)) {
|
|
135
|
-
this.toEnsure.push((willPrint) => {
|
|
136
|
-
if (!willPrint.startsWith(toEnsure) && !this.doesEndWith(toEnsure)) {
|
|
137
|
-
this.stringBuilder.push(toEnsure);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
ensureEither = (...args) => {
|
|
143
|
-
if (args.length === 1) {
|
|
144
|
-
this.ensure(...args);
|
|
145
|
-
} else if (args.length > 1 && // Not already matched?
|
|
146
|
-
!args.some((subStr) => this.doesEndWith(subStr))) {
|
|
147
|
-
this.toEnsure.push((willPrint) => {
|
|
148
|
-
if (!args.some((subStr) => willPrint.startsWith(subStr)) && !args.some((subStr) => this.doesEndWith(subStr))) {
|
|
149
|
-
this.stringBuilder.push(args[0]);
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
pruneEndingBlanks() {
|
|
155
|
-
let temp = "";
|
|
156
|
-
while (/^[ \t]*$/u.test(temp) && this.stringBuilder.length > 0) {
|
|
157
|
-
temp = this.stringBuilder.pop() + temp;
|
|
7
|
+
rules;
|
|
8
|
+
factory = new AstCoreFactory_js_1.AstCoreFactory();
|
|
9
|
+
__context = undefined;
|
|
10
|
+
origSource = '';
|
|
11
|
+
generatedUntil = 0;
|
|
12
|
+
toEnsure = [];
|
|
13
|
+
/**
|
|
14
|
+
* Should not contain empty strings
|
|
15
|
+
* @protected
|
|
16
|
+
*/
|
|
17
|
+
stringBuilder = [];
|
|
18
|
+
constructor(rules) {
|
|
19
|
+
this.rules = rules;
|
|
20
|
+
// eslint-disable-next-line ts/no-unnecessary-type-assertion
|
|
21
|
+
for (const rule of Object.values(rules)) {
|
|
22
|
+
// Define function implementation
|
|
23
|
+
this[rule.name] =
|
|
24
|
+
((input, context, args) => {
|
|
25
|
+
this.stringBuilder.length = 0;
|
|
26
|
+
this.origSource = context.origSource;
|
|
27
|
+
this.generatedUntil = context?.offset ?? 0;
|
|
28
|
+
this.setContext(context);
|
|
29
|
+
this.subrule(rule, input, args);
|
|
30
|
+
this.catchup(this.origSource.length);
|
|
31
|
+
return this.stringBuilder.join('');
|
|
32
|
+
});
|
|
33
|
+
}
|
|
158
34
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
newLine = (arg) => {
|
|
162
|
-
const indentation = this.getSafeContext()[import_utils.traqulaIndentation] ?? 0;
|
|
163
|
-
if (indentation === -1) {
|
|
164
|
-
return;
|
|
35
|
+
setContext(context) {
|
|
36
|
+
this.__context = context;
|
|
165
37
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (force) {
|
|
169
|
-
this.print("\n", " ".repeat(indentation));
|
|
170
|
-
} else {
|
|
171
|
-
let temp = "";
|
|
172
|
-
while (!temp.includes("\n") && this.stringBuilder.length > 0) {
|
|
173
|
-
temp = this.stringBuilder.pop() + temp;
|
|
174
|
-
}
|
|
175
|
-
if (/\n[ \t]*$/u.test(temp)) {
|
|
176
|
-
temp = temp.replace(/\n[ \t]*$/u, `
|
|
177
|
-
${" ".repeat(indentation)}`);
|
|
178
|
-
this.print(temp);
|
|
179
|
-
} else {
|
|
180
|
-
this.print(temp, "\n", " ".repeat(indentation));
|
|
181
|
-
}
|
|
38
|
+
getSafeContext() {
|
|
39
|
+
return this.__context;
|
|
182
40
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
41
|
+
subrule = (cstDef, ast, ...arg) => {
|
|
42
|
+
const def = this.rules[cstDef.name];
|
|
43
|
+
if (!def) {
|
|
44
|
+
throw new Error(`Rule ${cstDef.name} not found`);
|
|
45
|
+
}
|
|
46
|
+
const generate = () => def.gImpl({
|
|
47
|
+
SUBRULE: this.subrule,
|
|
48
|
+
PRINT: this.print,
|
|
49
|
+
ENSURE: this.ensure,
|
|
50
|
+
ENSURE_EITHER: this.ensureEither,
|
|
51
|
+
NEW_LINE: this.newLine,
|
|
52
|
+
HANDLE_LOC: this.handleLoc,
|
|
53
|
+
CATCHUP: this.catchup,
|
|
54
|
+
PRINT_WORD: this.printWord,
|
|
55
|
+
PRINT_WORDS: this.printWords,
|
|
56
|
+
PRINT_ON_EMPTY: this.printOnEmpty,
|
|
57
|
+
PRINT_ON_OWN_LINE: this.printOnOwnLine,
|
|
58
|
+
})(ast, this.getSafeContext(), ...arg);
|
|
59
|
+
if (this.factory.isLocalized(ast)) {
|
|
60
|
+
this.handleLoc(ast, generate);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
generate();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
handleLoc = (localized, handle) => {
|
|
67
|
+
if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (this.factory.isSourceLocationStringReplace(localized.loc)) {
|
|
71
|
+
this.catchup(localized.loc.start);
|
|
72
|
+
this.print(localized.loc.newSource);
|
|
73
|
+
this.generatedUntil = localized.loc.end;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (this.factory.isSourceLocationNodeReplace(localized.loc)) {
|
|
77
|
+
this.catchup(localized.loc.start);
|
|
78
|
+
this.generatedUntil = localized.loc.end;
|
|
79
|
+
}
|
|
80
|
+
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
81
|
+
this.catchup(localized.loc.start);
|
|
82
|
+
}
|
|
83
|
+
// If autoGenerate - do nothing
|
|
84
|
+
const ret = handle();
|
|
85
|
+
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
86
|
+
this.catchup(localized.loc.end);
|
|
87
|
+
}
|
|
88
|
+
return ret;
|
|
89
|
+
};
|
|
90
|
+
catchup = (until) => {
|
|
91
|
+
const start = this.generatedUntil;
|
|
92
|
+
if (start < until) {
|
|
93
|
+
this.print(this.origSource.slice(start, until));
|
|
94
|
+
}
|
|
95
|
+
this.generatedUntil = Math.max(this.generatedUntil, until);
|
|
96
|
+
};
|
|
97
|
+
handeEnsured(toPrint) {
|
|
98
|
+
for (const callBack of this.toEnsure) {
|
|
99
|
+
callBack(toPrint);
|
|
100
|
+
}
|
|
101
|
+
this.toEnsure.length = 0;
|
|
102
|
+
}
|
|
103
|
+
print = (...args) => {
|
|
104
|
+
const joined = args.join('');
|
|
105
|
+
this.handeEnsured(joined);
|
|
106
|
+
this.stringBuilder.push(joined);
|
|
107
|
+
};
|
|
108
|
+
doesEndWith(subsStr) {
|
|
109
|
+
const len = subsStr.length;
|
|
110
|
+
let temp = '';
|
|
111
|
+
while (temp.length < len && this.stringBuilder.length > 0) {
|
|
112
|
+
temp = this.stringBuilder.pop() + temp;
|
|
113
|
+
}
|
|
114
|
+
this.stringBuilder.push(temp);
|
|
115
|
+
return temp.endsWith(subsStr);
|
|
116
|
+
}
|
|
117
|
+
ensure = (...args) => {
|
|
118
|
+
// Check whether already present
|
|
119
|
+
const toEnsure = args.join('');
|
|
120
|
+
if (!this.doesEndWith(toEnsure)) {
|
|
121
|
+
this.toEnsure.push((willPrint) => {
|
|
122
|
+
if (!willPrint.startsWith(toEnsure) && !this.doesEndWith(toEnsure)) {
|
|
123
|
+
this.stringBuilder.push(toEnsure);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
ensureEither = (...args) => {
|
|
129
|
+
if (args.length === 1) {
|
|
130
|
+
this.ensure(...args);
|
|
131
|
+
}
|
|
132
|
+
else if (args.length > 1 &&
|
|
133
|
+
// Not already matched?
|
|
134
|
+
!args.some(subStr => this.doesEndWith(subStr))) {
|
|
135
|
+
this.toEnsure.push((willPrint) => {
|
|
136
|
+
if (!args.some(subStr => willPrint.startsWith(subStr)) && !args.some(subStr => this.doesEndWith(subStr))) {
|
|
137
|
+
this.stringBuilder.push(args[0]);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
pruneEndingBlanks() {
|
|
143
|
+
let temp = '';
|
|
144
|
+
while (/^[ \t]*$/u.test(temp) && this.stringBuilder.length > 0) {
|
|
145
|
+
temp = this.stringBuilder.pop() + temp;
|
|
146
|
+
}
|
|
147
|
+
this.print(temp.trimEnd());
|
|
192
148
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
149
|
+
newLine = (arg) => {
|
|
150
|
+
const indentation = this.getSafeContext()[utils_js_1.traqulaIndentation] ?? 0;
|
|
151
|
+
if (indentation === -1) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const force = arg?.force ?? false;
|
|
155
|
+
this.pruneEndingBlanks();
|
|
156
|
+
if (force) {
|
|
157
|
+
this.print('\n', ' '.repeat(indentation));
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
let temp = '';
|
|
161
|
+
while (!temp.includes('\n') && this.stringBuilder.length > 0) {
|
|
162
|
+
temp = this.stringBuilder.pop() + temp;
|
|
163
|
+
}
|
|
164
|
+
if (/\n[ \t]*$/u.test(temp)) {
|
|
165
|
+
// Pointer is on empty newline -> set correct indentation
|
|
166
|
+
temp = temp.replace(/\n[ \t]*$/u, `\n${' '.repeat(indentation)}`);
|
|
167
|
+
this.print(temp);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
// Pointer not on empty newline, print newline.
|
|
171
|
+
this.print(temp, '\n', ' '.repeat(indentation));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
printWord = (...args) => {
|
|
176
|
+
this.ensureEither(' ', '\n');
|
|
177
|
+
this.print(...args);
|
|
178
|
+
this.ensureEither(' ', '\n');
|
|
179
|
+
};
|
|
180
|
+
printWords = (...args) => {
|
|
181
|
+
for (const arg of args) {
|
|
182
|
+
this.printWord(arg);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
printOnEmpty = (...args) => {
|
|
186
|
+
this.newLine();
|
|
187
|
+
this.print(...args);
|
|
188
|
+
};
|
|
189
|
+
printOnOwnLine = (...args) => {
|
|
190
|
+
this.newLine();
|
|
191
|
+
this.print(...args);
|
|
192
|
+
this.newLine();
|
|
193
|
+
};
|
|
203
194
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
DynamicGenerator
|
|
207
|
-
});
|
|
195
|
+
exports.DynamicGenerator = DynamicGenerator;
|
|
196
|
+
//# sourceMappingURL=dynamicGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dynamicGenerator.js","sourceRoot":"","sources":["../../../../lib/generator-builder/dynamicGenerator.ts"],"names":[],"mappings":";;;AAAA,4DAAsD;AACtD,0CAAiD;AAIjD,MAAa,gBAAgB;IAYE;IAXV,OAAO,GAAG,IAAI,kCAAc,EAAE,CAAC;IACxC,SAAS,GAAwB,SAA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AC,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC,CAAC;IAEe,UAAU,GAA6B,CAAC,GAAG,IAAI,EAAE,EAAE;QAClE,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC;IAEe,YAAY,GAAiC,CAAC,GAAG,IAAI,EAAE,EAAE;QACxE,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;IACtB,CAAC,CAAC;IAEe,cAAc,GAAoC,CAAC,GAAG,IAAI,EAAE,EAAE;QAC7E,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;QACpB,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC,CAAC;CACH;AA/MD,4CA+MC","sourcesContent":["import { AstCoreFactory } from '../AstCoreFactory.js';\nimport { traqulaIndentation } from '../utils.js';\nimport type { GenRuleMap } from './builderTypes.js';\nimport type { GeneratorRule, RuleDefArg } from './generatorTypes.js';\n\nexport class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n protected readonly factory = new AstCoreFactory();\n protected __context: Context | undefined = undefined;\n protected origSource = '';\n protected generatedUntil = 0;\n protected toEnsure: ((willPrint: string) => void)[] = [];\n /**\n * Should not contain empty strings\n * @protected\n */\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.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 & { [traqulaIndentation]?: number } {\n return <Context & { [traqulaIndentation]?: number }> 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 ENSURE: this.ensure,\n ENSURE_EITHER: this.ensureEither,\n NEW_LINE: this.newLine,\n HANDLE_LOC: this.handleLoc,\n CATCHUP: this.catchup,\n\n PRINT_WORD: this.printWord,\n PRINT_WORDS: this.printWords,\n PRINT_ON_EMPTY: this.printOnEmpty,\n PRINT_ON_OWN_LINE: this.printOnOwnLine,\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 private handeEnsured(toPrint: string): void {\n for (const callBack of this.toEnsure) {\n callBack(toPrint);\n }\n this.toEnsure.length = 0;\n }\n\n protected readonly print: RuleDefArg['PRINT'] = (...args) => {\n const joined = args.join('');\n this.handeEnsured(joined);\n this.stringBuilder.push(joined);\n };\n\n private doesEndWith(subsStr: string): boolean {\n const len = subsStr.length;\n let temp = '';\n while (temp.length < len && this.stringBuilder.length > 0) {\n temp = this.stringBuilder.pop() + temp;\n }\n this.stringBuilder.push(temp);\n return temp.endsWith(subsStr);\n }\n\n protected readonly ensure: RuleDefArg['ENSURE'] = (...args) => {\n // Check whether already present\n const toEnsure = args.join('');\n if (!this.doesEndWith(toEnsure)) {\n this.toEnsure.push((willPrint) => {\n if (!willPrint.startsWith(toEnsure) && !this.doesEndWith(toEnsure)) {\n this.stringBuilder.push(toEnsure);\n }\n });\n }\n };\n\n protected readonly ensureEither: RuleDefArg['ENSURE_EITHER'] = (...args) => {\n if (args.length === 1) {\n this.ensure(...args);\n } else if (args.length > 1 &&\n // Not already matched?\n !args.some(subStr => this.doesEndWith(subStr))) {\n this.toEnsure.push((willPrint) => {\n if (!args.some(subStr => willPrint.startsWith(subStr)) && !args.some(subStr => this.doesEndWith(subStr))) {\n this.stringBuilder.push(args[0]);\n }\n });\n }\n };\n\n private pruneEndingBlanks(): void {\n let temp = '';\n while (/^[ \\t]*$/u.test(temp) && this.stringBuilder.length > 0) {\n temp = this.stringBuilder.pop() + temp;\n }\n this.print(temp.trimEnd());\n }\n\n protected readonly newLine: RuleDefArg['NEW_LINE'] = (arg) => {\n const indentation = this.getSafeContext()[traqulaIndentation] ?? 0;\n if (indentation === -1) {\n return;\n }\n const force = arg?.force ?? false;\n this.pruneEndingBlanks();\n if (force) {\n this.print('\\n', ' '.repeat(indentation));\n } else {\n let temp = '';\n while (!temp.includes('\\n') && this.stringBuilder.length > 0) {\n temp = this.stringBuilder.pop() + temp;\n }\n if (/\\n[ \\t]*$/u.test(temp)) {\n // Pointer is on empty newline -> set correct indentation\n temp = temp.replace(/\\n[ \\t]*$/u, `\\n${' '.repeat(indentation)}`);\n this.print(temp);\n } else {\n // Pointer not on empty newline, print newline.\n this.print(temp, '\\n', ' '.repeat(indentation));\n }\n }\n };\n\n private readonly printWord: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.ensureEither(' ', '\\n');\n this.print(...args);\n this.ensureEither(' ', '\\n');\n };\n\n private readonly printWords: RuleDefArg['PRINT_WORD'] = (...args) => {\n for (const arg of args) {\n this.printWord(arg);\n }\n };\n\n private readonly printOnEmpty: RuleDefArg['PRINT_ON_EMPTY'] = (...args) => {\n this.newLine();\n this.print(...args);\n };\n\n private readonly printOnOwnLine: RuleDefArg['PRINT_ON_OWN_LINE'] = (...args) => {\n this.newLine();\n this.print(...args);\n this.newLine();\n };\n}\n"]}
|
|
@@ -1,126 +1,112 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 generatorBuilder_exports = {};
|
|
20
|
-
__export(generatorBuilder_exports, {
|
|
21
|
-
GeneratorBuilder: () => GeneratorBuilder
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(generatorBuilder_exports);
|
|
24
|
-
var import_dynamicGenerator = require("./dynamicGenerator.js");
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeneratorBuilder = void 0;
|
|
4
|
+
const dynamicGenerator_js_1 = require("./dynamicGenerator.js");
|
|
5
|
+
/**
|
|
6
|
+
* Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
|
|
7
|
+
*/
|
|
25
8
|
function listToRuleDefMap(rules) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
9
|
+
const newRules = {};
|
|
10
|
+
for (const rule of rules) {
|
|
11
|
+
newRules[rule.name] = rule;
|
|
12
|
+
}
|
|
13
|
+
return newRules;
|
|
31
14
|
}
|
|
32
15
|
class GeneratorBuilder {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
16
|
+
static create(start) {
|
|
17
|
+
if (Array.isArray(start)) {
|
|
18
|
+
return new GeneratorBuilder(listToRuleDefMap(start));
|
|
19
|
+
}
|
|
20
|
+
return new GeneratorBuilder({ ...start.rules });
|
|
21
|
+
}
|
|
22
|
+
rules;
|
|
23
|
+
constructor(startRules) {
|
|
24
|
+
this.rules = startRules;
|
|
25
|
+
}
|
|
26
|
+
widenContext() {
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
typePatch() {
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Change the implementation of an existing generator rule.
|
|
34
|
+
*/
|
|
35
|
+
patchRule(patch) {
|
|
36
|
+
const self = this;
|
|
37
|
+
self.rules[patch.name] = patch;
|
|
38
|
+
return self;
|
|
36
39
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
|
|
59
|
-
*/
|
|
60
|
-
addRuleRedundant(rule) {
|
|
61
|
-
const self = this;
|
|
62
|
-
const rules = self.rules;
|
|
63
|
-
if (rules[rule.name] !== void 0 && rules[rule.name] !== rule) {
|
|
64
|
-
throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);
|
|
40
|
+
/**
|
|
41
|
+
* Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
|
|
42
|
+
*/
|
|
43
|
+
addRuleRedundant(rule) {
|
|
44
|
+
const self = this;
|
|
45
|
+
const rules = self.rules;
|
|
46
|
+
if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {
|
|
47
|
+
throw new Error(`Rule ${rule.name} already exists in the GeneratorBuilder`);
|
|
48
|
+
}
|
|
49
|
+
rules[rule.name] = rule;
|
|
50
|
+
return self;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
|
|
54
|
+
*/
|
|
55
|
+
addRule(rule) {
|
|
56
|
+
return this.addRuleRedundant(rule);
|
|
57
|
+
}
|
|
58
|
+
addMany(...rules) {
|
|
59
|
+
this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
|
|
60
|
+
return this;
|
|
65
61
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const override = overridingRules.find((x) => x.name === rule.name);
|
|
108
|
-
if (override) {
|
|
109
|
-
otherRules[rule.name] = override;
|
|
110
|
-
} else {
|
|
111
|
-
throw new Error(`Rule with name "${rule.name}" already exists in the GeneratorBuilder, specify an override to resolve conflict`);
|
|
112
|
-
}
|
|
62
|
+
/**
|
|
63
|
+
* Delete a grammar rule by its name.
|
|
64
|
+
*/
|
|
65
|
+
deleteRule(ruleName) {
|
|
66
|
+
delete this.rules[ruleName];
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
getRule(ruleName) {
|
|
70
|
+
return this.rules[ruleName];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Merge this grammar GeneratorBuilder with another.
|
|
74
|
+
* It is best to merge the bigger grammar with the smaller one.
|
|
75
|
+
* If the two builders both have a grammar rule with the same name,
|
|
76
|
+
* no error will be thrown case they map to the same ruledef object.
|
|
77
|
+
* If they map to a different object, an error will be thrown.
|
|
78
|
+
* To fix this problem, the overridingRules array should contain a rule with the same conflicting name,
|
|
79
|
+
* this rule implementation will be used.
|
|
80
|
+
*/
|
|
81
|
+
merge(GeneratorBuilder, overridingRules) {
|
|
82
|
+
// Assume the other grammar is bigger than yours. So start from that one and add this one
|
|
83
|
+
const otherRules = { ...GeneratorBuilder.rules };
|
|
84
|
+
const myRules = this.rules;
|
|
85
|
+
for (const rule of Object.values(myRules)) {
|
|
86
|
+
if (otherRules[rule.name] === undefined) {
|
|
87
|
+
otherRules[rule.name] = rule;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const existingRule = otherRules[rule.name];
|
|
91
|
+
// If same rule, no issue, move on. Else
|
|
92
|
+
if (existingRule !== rule) {
|
|
93
|
+
const override = overridingRules.find(x => x.name === rule.name);
|
|
94
|
+
// If override specified, take override, else, inform user that there is a conflict
|
|
95
|
+
if (override) {
|
|
96
|
+
otherRules[rule.name] = override;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
throw new Error(`Rule with name "${rule.name}" already exists in the GeneratorBuilder, specify an override to resolve conflict`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
113
103
|
}
|
|
114
|
-
|
|
104
|
+
this.rules = otherRules;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
build() {
|
|
108
|
+
return new dynamicGenerator_js_1.DynamicGenerator(this.rules);
|
|
115
109
|
}
|
|
116
|
-
this.rules = otherRules;
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
build() {
|
|
120
|
-
return new import_dynamicGenerator.DynamicGenerator(this.rules);
|
|
121
|
-
}
|
|
122
110
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
GeneratorBuilder
|
|
126
|
-
});
|
|
111
|
+
exports.GeneratorBuilder = GeneratorBuilder;
|
|
112
|
+
//# sourceMappingURL=generatorBuilder.js.map
|