@malloydata/malloy-tag 0.0.237-dev250225144145
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/README.md +0 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/Malloy/MalloyTagLexer.d.ts +42 -0
- package/dist/lib/Malloy/MalloyTagLexer.js +385 -0
- package/dist/lib/Malloy/MalloyTagLexer.js.map +1 -0
- package/dist/lib/Malloy/MalloyTagParser.d.ts +180 -0
- package/dist/lib/Malloy/MalloyTagParser.js +1051 -0
- package/dist/lib/Malloy/MalloyTagParser.js.map +1 -0
- package/dist/lib/Malloy/MalloyTagVisitor.d.ts +120 -0
- package/dist/lib/Malloy/MalloyTagVisitor.js +4 -0
- package/dist/lib/Malloy/MalloyTagVisitor.js.map +1 -0
- package/dist/tags.d.ts +90 -0
- package/dist/tags.js +654 -0
- package/dist/tags.js.map +1 -0
- package/dist/tags.spec.d.ts +8 -0
- package/dist/tags.spec.js +318 -0
- package/dist/tags.spec.js.map +1 -0
- package/dist/util.d.ts +11 -0
- package/dist/util.js +84 -0
- package/dist/util.js.map +1 -0
- package/dist/util.spec.d.ts +1 -0
- package/dist/util.spec.js +43 -0
- package/dist/util.spec.js.map +1 -0
- package/package.json +30 -0
- package/scripts/build_parser.js +98 -0
- package/src/MalloyTag.g4 +102 -0
- package/src/index.ts +8 -0
- package/src/lib/Malloy/MalloyTag.interp +61 -0
- package/src/lib/Malloy/MalloyTag.tokens +32 -0
- package/src/lib/Malloy/MalloyTagLexer.interp +85 -0
- package/src/lib/Malloy/MalloyTagLexer.tokens +32 -0
- package/src/lib/Malloy/MalloyTagLexer.ts +386 -0
- package/src/lib/Malloy/MalloyTagParser.ts +1048 -0
- package/src/lib/Malloy/MalloyTagVisitor.ts +141 -0
- package/src/lib/Malloy/_BUILD_DIGEST_ +1 -0
- package/src/tags.spec.ts +331 -0
- package/src/tags.ts +761 -0
- package/src/util.spec.ts +43 -0
- package/src/util.ts +73 -0
- package/tsconfig.json +11 -0
package/src/tags.ts
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files
|
|
6
|
+
* (the "Software"), to deal in the Software without restriction,
|
|
7
|
+
* including without limitation the rights to use, copy, modify, merge,
|
|
8
|
+
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
9
|
+
* and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
* subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
18
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
19
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
20
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
21
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {AbstractParseTreeVisitor} from 'antlr4ts/tree';
|
|
25
|
+
import {MalloyTagLexer} from './lib/Malloy/MalloyTagLexer';
|
|
26
|
+
import {
|
|
27
|
+
ArrayElementContext,
|
|
28
|
+
ArrayValueContext,
|
|
29
|
+
MalloyTagParser,
|
|
30
|
+
PropNameContext,
|
|
31
|
+
PropertiesContext,
|
|
32
|
+
ReferenceContext,
|
|
33
|
+
StringContext,
|
|
34
|
+
TagDefContext,
|
|
35
|
+
TagEmptyContext,
|
|
36
|
+
TagEqContext,
|
|
37
|
+
TagLineContext,
|
|
38
|
+
TagReplacePropertiesContext,
|
|
39
|
+
TagSpecContext,
|
|
40
|
+
TagUpdatePropertiesContext,
|
|
41
|
+
} from './lib/Malloy/MalloyTagParser';
|
|
42
|
+
import {MalloyTagVisitor} from './lib/Malloy/MalloyTagVisitor';
|
|
43
|
+
import {
|
|
44
|
+
ANTLRErrorListener,
|
|
45
|
+
CharStreams,
|
|
46
|
+
CommonTokenStream,
|
|
47
|
+
ParserRuleContext,
|
|
48
|
+
Token,
|
|
49
|
+
} from 'antlr4ts';
|
|
50
|
+
import {parseString} from './util';
|
|
51
|
+
|
|
52
|
+
export interface TagError {
|
|
53
|
+
message: string;
|
|
54
|
+
line: number;
|
|
55
|
+
offset: number;
|
|
56
|
+
code: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The distinction between the interface and the Tag class exists solely to
|
|
60
|
+
// make it possible to write tests and specify expected results This
|
|
61
|
+
// is why only TagDict interface is exported.
|
|
62
|
+
export type TagDict = Record<string, TagInterface>;
|
|
63
|
+
|
|
64
|
+
type TagValue = string | TagInterface[];
|
|
65
|
+
|
|
66
|
+
export interface TagInterface {
|
|
67
|
+
eq?: TagValue;
|
|
68
|
+
properties?: TagDict;
|
|
69
|
+
deleted?: boolean;
|
|
70
|
+
prefix?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface TagParse {
|
|
74
|
+
tag: Tag;
|
|
75
|
+
log: TagError[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type PathSegment = string | number;
|
|
79
|
+
export type Path = PathSegment[];
|
|
80
|
+
|
|
81
|
+
export type TagSetValue = string | number | string[] | number[] | null;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Class for interacting with the parsed output of an annotation
|
|
85
|
+
* containing the Malloy tag language. Used by the parser to
|
|
86
|
+
* generate parsed data, and as an API to that data.
|
|
87
|
+
* ```
|
|
88
|
+
* tag.text(p?) => string value of tag.p or undefined
|
|
89
|
+
* tag.array(p?) => Tag[] value of tag.p or undefined
|
|
90
|
+
* tag.numeric(p?) => numeric value of tag.p or undefined
|
|
91
|
+
* tag.textArray(p ?) => string[] value of elements in tag.p or undefined
|
|
92
|
+
* tag.numericArray(p?) => string[] value of elements in tag.p or undefined
|
|
93
|
+
* tag.tag(p?) => Tag value of tag.p
|
|
94
|
+
* tag.has(p?) => boolean "tag contains tag.p"
|
|
95
|
+
* tag.bare(p?) => tag.p exists and has no properties
|
|
96
|
+
* tag.dict => Record<string,Tag> of tag properties
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export class Tag implements TagInterface {
|
|
100
|
+
eq?: TagValue;
|
|
101
|
+
properties?: TagDict;
|
|
102
|
+
prefix?: string;
|
|
103
|
+
deleted?: boolean;
|
|
104
|
+
|
|
105
|
+
static tagFrom(from: TagInterface = {}) {
|
|
106
|
+
if (from instanceof Tag) {
|
|
107
|
+
return from;
|
|
108
|
+
}
|
|
109
|
+
return new Tag(from);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// --- Just for debugging ---
|
|
113
|
+
static ids = new Map<Tag, number>();
|
|
114
|
+
static nextTagId = 1000;
|
|
115
|
+
static id(t: Tag): number {
|
|
116
|
+
let thisTagId = Tag.ids.get(t);
|
|
117
|
+
if (thisTagId === undefined) {
|
|
118
|
+
thisTagId = Tag.nextTagId;
|
|
119
|
+
Tag.ids.set(t, thisTagId);
|
|
120
|
+
Tag.nextTagId += 1;
|
|
121
|
+
}
|
|
122
|
+
return thisTagId;
|
|
123
|
+
}
|
|
124
|
+
peek(indent = 0): string {
|
|
125
|
+
const spaces = ' '.repeat(indent);
|
|
126
|
+
let str = `#${Tag.id(this)}`;
|
|
127
|
+
if (
|
|
128
|
+
this.properties === undefined &&
|
|
129
|
+
this.eq &&
|
|
130
|
+
typeof this.eq === 'string'
|
|
131
|
+
) {
|
|
132
|
+
return str + `=${this.eq}`;
|
|
133
|
+
}
|
|
134
|
+
str += ' {';
|
|
135
|
+
if (this.eq) {
|
|
136
|
+
if (typeof this.eq === 'string') {
|
|
137
|
+
str += `\n${spaces} =: ${this.eq}`;
|
|
138
|
+
} else {
|
|
139
|
+
str += `\n${spaces} =: [\n${spaces} ${this.eq
|
|
140
|
+
.map(el => Tag.tagFrom(el).peek(indent + 4))
|
|
141
|
+
.join(`\n${spaces} `)}\n${spaces} ]`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (this.properties) {
|
|
146
|
+
for (const k in this.properties) {
|
|
147
|
+
const val = Tag.tagFrom(this.properties[k]);
|
|
148
|
+
str += `\n${spaces} ${k}: ${val.peek(indent + 2)}`;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
str += `\n${spaces}}`;
|
|
152
|
+
return str;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Parse a line of Malloy tag language into a Tag which can be queried
|
|
157
|
+
* @param source -- The source line to be parsed. If the string starts with #, then it skips
|
|
158
|
+
* all characters up to the first space.
|
|
159
|
+
* @param lineNumber -- A line number to be associated with the parse errors.
|
|
160
|
+
* @param extending A tag which this line will be extending
|
|
161
|
+
* @param importing Outer "scopes" for $() references
|
|
162
|
+
* @returns Something shaped like { tag: Tag, log: ParseErrors[] }
|
|
163
|
+
*/
|
|
164
|
+
static fromTagLine(
|
|
165
|
+
source: string,
|
|
166
|
+
lineNumber = 0,
|
|
167
|
+
extending?: Tag,
|
|
168
|
+
...importing: Tag[]
|
|
169
|
+
): TagParse {
|
|
170
|
+
return parseTagLine(source, extending, importing, lineNumber);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Parse multiple lines of Malloy tag language, merging them into a single Tag
|
|
175
|
+
* @param lines -- The source line to be parsed. If the string starts with #, then it skips
|
|
176
|
+
* all characters up to the first space.
|
|
177
|
+
* @param extending A tag which this line will be extending
|
|
178
|
+
* @param importing Outer "scopes" for $() references
|
|
179
|
+
* @returns Something shaped like { tag: Tag, log: ParseErrors[] }
|
|
180
|
+
*/
|
|
181
|
+
static fromTagLines(lines: string[], extending?: Tag, ...importing: Tag[]) {
|
|
182
|
+
const allErrs: TagError[] = [];
|
|
183
|
+
let current: Tag | undefined = extending;
|
|
184
|
+
for (let i = 0; i < lines.length; i++) {
|
|
185
|
+
const text = lines[i];
|
|
186
|
+
const noteParse = parseTagLine(text, extending, importing, i);
|
|
187
|
+
current = noteParse.tag;
|
|
188
|
+
allErrs.push(...noteParse.log);
|
|
189
|
+
}
|
|
190
|
+
return {tag: current, log: allErrs};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
constructor(from: TagInterface = {}) {
|
|
194
|
+
if (from.eq) {
|
|
195
|
+
this.eq = from.eq;
|
|
196
|
+
}
|
|
197
|
+
if (from.properties) {
|
|
198
|
+
this.properties = from.properties;
|
|
199
|
+
}
|
|
200
|
+
if (from.deleted) {
|
|
201
|
+
this.deleted = from.deleted;
|
|
202
|
+
}
|
|
203
|
+
if (from.prefix) {
|
|
204
|
+
this.prefix = from.prefix;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static withPrefix(prefix: string) {
|
|
209
|
+
return new Tag({prefix});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
tag(...at: Path): Tag | undefined {
|
|
213
|
+
return this.find(at);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
text(...at: Path): string | undefined {
|
|
217
|
+
const str = this.find(at)?.eq;
|
|
218
|
+
if (typeof str === 'string') {
|
|
219
|
+
return str;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
numeric(...at: Path): number | undefined {
|
|
224
|
+
const str = this.find(at)?.eq;
|
|
225
|
+
if (typeof str === 'string') {
|
|
226
|
+
const num = Number.parseFloat(str);
|
|
227
|
+
if (!Number.isNaN(num)) {
|
|
228
|
+
return num;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
bare(...at: Path): boolean | undefined {
|
|
234
|
+
const p = this.find(at);
|
|
235
|
+
if (p === undefined) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
return (
|
|
239
|
+
p.properties === undefined || Object.entries(p.properties).length === 0
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
get dict(): Record<string, Tag> {
|
|
244
|
+
const newDict: Record<string, Tag> = {};
|
|
245
|
+
if (this.properties) {
|
|
246
|
+
for (const key in this.properties) {
|
|
247
|
+
newDict[key] = Tag.tagFrom(this.properties[key]);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return newDict;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
array(...at: Path): Tag[] | undefined {
|
|
254
|
+
const array = this.find(at)?.eq;
|
|
255
|
+
if (array === undefined || typeof array === 'string') {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
return array.map(el =>
|
|
259
|
+
typeof el === 'string' ? new Tag({eq: el}) : Tag.tagFrom(el)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
textArray(...at: Path): string[] | undefined {
|
|
264
|
+
const array = this.find(at)?.eq;
|
|
265
|
+
if (array === undefined || typeof array === 'string') {
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
return array.reduce<string[]>(
|
|
269
|
+
(allStrs, el) =>
|
|
270
|
+
typeof el.eq === 'string' ? allStrs.concat(el.eq) : allStrs,
|
|
271
|
+
[]
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
numericArray(...at: Path): number[] | undefined {
|
|
276
|
+
const array = this.find(at)?.eq;
|
|
277
|
+
if (array === undefined || typeof array === 'string') {
|
|
278
|
+
return undefined;
|
|
279
|
+
}
|
|
280
|
+
return array.reduce<number[]>((allNums, el) => {
|
|
281
|
+
if (typeof el.eq === 'string') {
|
|
282
|
+
const num = Number.parseFloat(el.eq);
|
|
283
|
+
if (!Number.isNaN(num)) {
|
|
284
|
+
return allNums.concat(num);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return allNums;
|
|
288
|
+
}, []);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Has the sometimes desireable side effect of initalizing properties
|
|
292
|
+
getProperties(): TagDict {
|
|
293
|
+
if (this.properties === undefined) {
|
|
294
|
+
this.properties = {};
|
|
295
|
+
}
|
|
296
|
+
return this.properties;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
clone(): Tag {
|
|
300
|
+
return new Tag(structuredClone(this));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
toString(): string {
|
|
304
|
+
let annotation = this.prefix ?? '# ';
|
|
305
|
+
function addChildren(tag: TagInterface) {
|
|
306
|
+
const props = Object.keys(tag.properties ?? {});
|
|
307
|
+
for (let i = 0; i < props.length; i++) {
|
|
308
|
+
addChild(props[i], tag.properties![props[i]]);
|
|
309
|
+
if (i < props.length - 1) {
|
|
310
|
+
annotation += ' ';
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function addTag(child: TagInterface, isArrayEl = false) {
|
|
315
|
+
if (child.eq !== undefined) {
|
|
316
|
+
if (!isArrayEl) annotation += ' = ';
|
|
317
|
+
if (Array.isArray(child.eq)) {
|
|
318
|
+
annotation += '[';
|
|
319
|
+
for (let i = 0; i < child.eq.length; i++) {
|
|
320
|
+
addTag(child.eq[i], true);
|
|
321
|
+
if (i !== child.eq.length - 1) annotation += ', ';
|
|
322
|
+
}
|
|
323
|
+
annotation += ']';
|
|
324
|
+
} else {
|
|
325
|
+
annotation += `${child.eq}`;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (child.properties) {
|
|
329
|
+
const props = Object.keys(child.properties);
|
|
330
|
+
if (
|
|
331
|
+
props.length === 1 &&
|
|
332
|
+
!props.some(c => (child.properties ?? {})[c].deleted) &&
|
|
333
|
+
child.eq === undefined
|
|
334
|
+
) {
|
|
335
|
+
annotation += '.';
|
|
336
|
+
addChildren(child);
|
|
337
|
+
} else {
|
|
338
|
+
annotation += ' { ';
|
|
339
|
+
addChildren(child);
|
|
340
|
+
annotation += ' }';
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function addChild(prop: string, child: TagInterface) {
|
|
345
|
+
if (child.deleted) {
|
|
346
|
+
annotation += `-${prop}`;
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
annotation += prop;
|
|
350
|
+
addTag(child);
|
|
351
|
+
}
|
|
352
|
+
addChildren(this);
|
|
353
|
+
return annotation;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
find(path: Path): Tag | undefined {
|
|
357
|
+
let currentTag: Tag = Tag.tagFrom(this);
|
|
358
|
+
for (const segment of path) {
|
|
359
|
+
if (typeof segment === 'number') {
|
|
360
|
+
if (
|
|
361
|
+
currentTag.eq === undefined ||
|
|
362
|
+
!Array.isArray(currentTag.eq) ||
|
|
363
|
+
currentTag.eq.length <= segment
|
|
364
|
+
) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
currentTag = Tag.tagFrom(currentTag.eq[segment]);
|
|
368
|
+
} else {
|
|
369
|
+
const properties = currentTag.properties ?? {};
|
|
370
|
+
if (segment in properties) {
|
|
371
|
+
currentTag = Tag.tagFrom(properties[segment]);
|
|
372
|
+
} else {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return currentTag;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
has(...path: Path): boolean {
|
|
381
|
+
return this.find(path) !== undefined;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
set(path: Path, value: TagSetValue = null): Tag {
|
|
385
|
+
const copy = Tag.tagFrom(this);
|
|
386
|
+
let currentTag: TagInterface = copy;
|
|
387
|
+
for (const segment of path) {
|
|
388
|
+
if (typeof segment === 'number') {
|
|
389
|
+
if (currentTag.eq === undefined || !Array.isArray(currentTag.eq)) {
|
|
390
|
+
currentTag.eq = Array.from({length: segment + 1}).map(_ => ({}));
|
|
391
|
+
} else if (currentTag.eq.length <= segment) {
|
|
392
|
+
const values = currentTag.eq;
|
|
393
|
+
const newVal = Array.from({length: segment + 1}).map((_, i) =>
|
|
394
|
+
i < values.length ? values[i] : {}
|
|
395
|
+
);
|
|
396
|
+
currentTag.eq = newVal;
|
|
397
|
+
}
|
|
398
|
+
currentTag = currentTag.eq[segment];
|
|
399
|
+
} else {
|
|
400
|
+
const properties = currentTag.properties;
|
|
401
|
+
if (properties === undefined) {
|
|
402
|
+
currentTag.properties = {[segment]: {}};
|
|
403
|
+
currentTag = currentTag.properties[segment];
|
|
404
|
+
} else if (segment in properties) {
|
|
405
|
+
currentTag = properties[segment];
|
|
406
|
+
if (currentTag.deleted) {
|
|
407
|
+
currentTag.deleted = false;
|
|
408
|
+
}
|
|
409
|
+
} else {
|
|
410
|
+
properties[segment] = {};
|
|
411
|
+
currentTag = properties[segment];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (value === null) {
|
|
416
|
+
currentTag.eq = undefined;
|
|
417
|
+
} else if (typeof value === 'string') {
|
|
418
|
+
currentTag.eq = value;
|
|
419
|
+
} else if (typeof value === 'number') {
|
|
420
|
+
currentTag.eq = value.toString(); // TODO big numbers?
|
|
421
|
+
} else if (Array.isArray(value)) {
|
|
422
|
+
currentTag.eq = value.map((v: string | number) => {
|
|
423
|
+
return {eq: typeof v === 'string' ? v : v.toString()};
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
return copy;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
delete(...path: Path): Tag {
|
|
430
|
+
return this.remove(path, false);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
unset(...path: Path): Tag {
|
|
434
|
+
return this.remove(path, true);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
private remove(path: Path, hard = false): Tag {
|
|
438
|
+
const origCopy = Tag.tagFrom(this);
|
|
439
|
+
let currentTag: TagInterface = origCopy;
|
|
440
|
+
for (const segment of path.slice(0, path.length - 1)) {
|
|
441
|
+
if (typeof segment === 'number') {
|
|
442
|
+
if (currentTag.eq === undefined || !Array.isArray(currentTag.eq)) {
|
|
443
|
+
if (!hard) return origCopy;
|
|
444
|
+
currentTag.eq = Array.from({length: segment}).map(_ => ({}));
|
|
445
|
+
} else if (currentTag.eq.length <= segment) {
|
|
446
|
+
if (!hard) return origCopy;
|
|
447
|
+
const values = currentTag.eq;
|
|
448
|
+
const newVal = Array.from({length: segment}).map((_, i) =>
|
|
449
|
+
i < values.length ? values[i] : {}
|
|
450
|
+
);
|
|
451
|
+
currentTag.eq = newVal;
|
|
452
|
+
}
|
|
453
|
+
currentTag = currentTag.eq[segment];
|
|
454
|
+
} else {
|
|
455
|
+
const properties = currentTag.properties;
|
|
456
|
+
if (properties === undefined) {
|
|
457
|
+
if (!hard) return origCopy;
|
|
458
|
+
currentTag.properties = {[segment]: {}};
|
|
459
|
+
currentTag = currentTag.properties[segment];
|
|
460
|
+
} else if (segment in properties) {
|
|
461
|
+
currentTag = properties[segment];
|
|
462
|
+
} else {
|
|
463
|
+
if (!hard) return origCopy;
|
|
464
|
+
properties[segment] = {};
|
|
465
|
+
currentTag = properties[segment];
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const segment = path[path.length - 1];
|
|
470
|
+
if (typeof segment === 'string') {
|
|
471
|
+
if (currentTag.properties && segment in currentTag.properties) {
|
|
472
|
+
delete currentTag.properties[segment];
|
|
473
|
+
} else if (hard) {
|
|
474
|
+
currentTag.properties ??= {};
|
|
475
|
+
currentTag.properties[segment] = {deleted: true};
|
|
476
|
+
}
|
|
477
|
+
} else {
|
|
478
|
+
if (Array.isArray(currentTag.eq)) {
|
|
479
|
+
currentTag.eq.splice(segment, 1);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return origCopy;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
class TagErrorListener implements ANTLRErrorListener<Token> {
|
|
487
|
+
log: TagError[] = [];
|
|
488
|
+
constructor(readonly line: number) {}
|
|
489
|
+
|
|
490
|
+
syntaxError(
|
|
491
|
+
recognizer: unknown,
|
|
492
|
+
offendingSymbol: Token | undefined,
|
|
493
|
+
line: number,
|
|
494
|
+
charPositionInLine: number,
|
|
495
|
+
msg: string,
|
|
496
|
+
_e: unknown
|
|
497
|
+
): void {
|
|
498
|
+
const logMsg: TagError = {
|
|
499
|
+
code: 'tag-parse-syntax-error',
|
|
500
|
+
message: msg,
|
|
501
|
+
line: this.line,
|
|
502
|
+
offset: charPositionInLine,
|
|
503
|
+
};
|
|
504
|
+
this.log.push(logMsg);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
semanticError(cx: ParserRuleContext, code: string, msg: string): void {
|
|
508
|
+
const logMsg: TagError = {
|
|
509
|
+
code,
|
|
510
|
+
message: msg,
|
|
511
|
+
line: this.line,
|
|
512
|
+
offset: cx.start.charPositionInLine,
|
|
513
|
+
};
|
|
514
|
+
this.log.push(logMsg);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function getBuildOn(ctx: ParserRuleContext): Tag {
|
|
519
|
+
const buildOn = ctx['buildOn'];
|
|
520
|
+
if (buildOn instanceof Tag) {
|
|
521
|
+
return buildOn;
|
|
522
|
+
}
|
|
523
|
+
return new Tag();
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* When chasing a path reference, the two interesting gestures are to
|
|
528
|
+
* find the path-ed tag so it can be extended, or to find the path tag
|
|
529
|
+
* so it can be deleted. This returns the parent and the final tag
|
|
530
|
+
* so that the caller can delete the tag with delete parent.tagName
|
|
531
|
+
* or assign to it with parent[tagName] = new_value
|
|
532
|
+
*/
|
|
533
|
+
function buildAccessPath(buildOn: Tag, path: string[]): [string, TagDict] {
|
|
534
|
+
let parentPropertyObject = buildOn.getProperties();
|
|
535
|
+
for (const p of path.slice(0, path.length - 1)) {
|
|
536
|
+
let next: Tag;
|
|
537
|
+
if (parentPropertyObject[p] === undefined) {
|
|
538
|
+
next = new Tag({});
|
|
539
|
+
parentPropertyObject[p] = next;
|
|
540
|
+
} else {
|
|
541
|
+
// The access that we are performing requires that `.properties` be the
|
|
542
|
+
// same JS object (not equal, but identical), and `Tag.tagFrom` only copies
|
|
543
|
+
// the exact object in if it is actually present.
|
|
544
|
+
parentPropertyObject[p].properties ??= {};
|
|
545
|
+
next = Tag.tagFrom(parentPropertyObject[p]);
|
|
546
|
+
}
|
|
547
|
+
parentPropertyObject = next.getProperties();
|
|
548
|
+
}
|
|
549
|
+
return [path[path.length - 1], parentPropertyObject];
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function getString(ctx: StringContext) {
|
|
553
|
+
if (ctx.SQ_STRING() || ctx.DQ_STRING()) {
|
|
554
|
+
return parseString(ctx.text, ctx.text[0]);
|
|
555
|
+
}
|
|
556
|
+
return ctx.text;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function parseTagLine(
|
|
560
|
+
source: string,
|
|
561
|
+
extending: Tag | undefined,
|
|
562
|
+
outerScope: Tag[],
|
|
563
|
+
onLine: number
|
|
564
|
+
): TagParse {
|
|
565
|
+
if (source[0] === '#') {
|
|
566
|
+
const skipTo = source.indexOf(' ');
|
|
567
|
+
if (skipTo > 0) {
|
|
568
|
+
source = source.slice(skipTo);
|
|
569
|
+
} else {
|
|
570
|
+
source = '';
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const inputStream = CharStreams.fromString(source);
|
|
574
|
+
const lexer = new MalloyTagLexer(inputStream);
|
|
575
|
+
const tokenStream = new CommonTokenStream(lexer);
|
|
576
|
+
const pLog = new TagErrorListener(onLine);
|
|
577
|
+
const taglineParser = new MalloyTagParser(tokenStream);
|
|
578
|
+
taglineParser.removeErrorListeners();
|
|
579
|
+
taglineParser.addErrorListener(pLog);
|
|
580
|
+
const tagTree = taglineParser.tagLine();
|
|
581
|
+
const treeWalker = new TagLineParser(outerScope, pLog);
|
|
582
|
+
const tag = treeWalker.tagLineToTag(tagTree, extending);
|
|
583
|
+
return {tag, log: pLog.log};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
class TagLineParser
|
|
587
|
+
extends AbstractParseTreeVisitor<Tag>
|
|
588
|
+
implements MalloyTagVisitor<Tag>
|
|
589
|
+
{
|
|
590
|
+
scopes: Tag[] = [];
|
|
591
|
+
msgLog: TagErrorListener;
|
|
592
|
+
constructor(outerScopes: Tag[] = [], msgLog: TagErrorListener) {
|
|
593
|
+
super();
|
|
594
|
+
this.msgLog = msgLog;
|
|
595
|
+
this.scopes.unshift(...outerScopes);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
defaultResult() {
|
|
599
|
+
return new Tag();
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
visitString(ctx: StringContext): Tag {
|
|
603
|
+
return new Tag({eq: getString(ctx)});
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
protected getPropName(ctx: PropNameContext): string[] {
|
|
607
|
+
return ctx
|
|
608
|
+
.identifier()
|
|
609
|
+
.map(cx =>
|
|
610
|
+
cx.BARE_STRING() ? cx.text : parseString(cx.text, cx.text[0])
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
getTags(tags: TagSpecContext[], tagLine: Tag): Tag {
|
|
615
|
+
for (const tagSpec of tags) {
|
|
616
|
+
// Stash the current state of this tag in the context and then visit it
|
|
617
|
+
// visit functions should alter the tagLine
|
|
618
|
+
tagSpec['buildOn'] = tagLine;
|
|
619
|
+
this.visit(tagSpec);
|
|
620
|
+
}
|
|
621
|
+
return tagLine;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
tagLineToTag(ctx: TagLineContext, extending: Tag | undefined): Tag {
|
|
625
|
+
extending = extending?.clone() || new Tag({});
|
|
626
|
+
this.scopes.unshift(extending);
|
|
627
|
+
this.getTags(ctx.tagSpec(), extending);
|
|
628
|
+
return extending;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
visitTagLine(_ctx: TagLineContext): Tag {
|
|
632
|
+
throw new Error('INTERNAL: ERROR: Call tagLineToTag, not vistTagLine');
|
|
633
|
+
return this.defaultResult();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
visitProperties(ctx: PropertiesContext): Tag {
|
|
637
|
+
return this.getTags(ctx.tagSpec(), getBuildOn(ctx));
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
visitArrayValue(ctx: ArrayValueContext): Tag {
|
|
641
|
+
return new Tag({eq: this.getArray(ctx)});
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
getArray(ctx: ArrayValueContext): Tag[] {
|
|
645
|
+
return ctx.arrayElement().map(v => this.visit(v));
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
visitArrayElement(ctx: ArrayElementContext): Tag {
|
|
649
|
+
const propCx = ctx.properties();
|
|
650
|
+
const properties = propCx ? this.visitProperties(propCx) : undefined;
|
|
651
|
+
const strCx = ctx.string();
|
|
652
|
+
let value: TagValue | undefined = strCx ? getString(strCx) : undefined;
|
|
653
|
+
|
|
654
|
+
const arrayCx = ctx.arrayValue();
|
|
655
|
+
if (arrayCx) {
|
|
656
|
+
value = this.getArray(arrayCx);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (properties) {
|
|
660
|
+
if (value) {
|
|
661
|
+
properties.eq = value;
|
|
662
|
+
}
|
|
663
|
+
return properties;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const refCx = ctx.reference();
|
|
667
|
+
if (refCx) {
|
|
668
|
+
return this.visitReference(refCx);
|
|
669
|
+
}
|
|
670
|
+
return new Tag({eq: value});
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
visitReference(ctx: ReferenceContext): Tag {
|
|
674
|
+
const path = this.getPropName(ctx.propName());
|
|
675
|
+
for (const scope of this.scopes) {
|
|
676
|
+
// first scope which has the first component gets to resolve the whole path
|
|
677
|
+
if (scope.has(path[0])) {
|
|
678
|
+
const refTo = scope.tag(...path);
|
|
679
|
+
if (refTo) {
|
|
680
|
+
return refTo.clone();
|
|
681
|
+
}
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
this.msgLog.semanticError(
|
|
686
|
+
ctx,
|
|
687
|
+
'tag-property-not-found',
|
|
688
|
+
`Reference to undefined property ${path.join('.')}`
|
|
689
|
+
);
|
|
690
|
+
return this.defaultResult();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
visitTagEq(ctx: TagEqContext): Tag {
|
|
694
|
+
const buildOn = getBuildOn(ctx);
|
|
695
|
+
const name = this.getPropName(ctx.propName());
|
|
696
|
+
const [writeKey, writeInto] = buildAccessPath(buildOn, name);
|
|
697
|
+
const eq = this.visit(ctx.eqValue());
|
|
698
|
+
const propCx = ctx.properties();
|
|
699
|
+
if (propCx) {
|
|
700
|
+
// a.b.c { -y } means i want to do -y on
|
|
701
|
+
if (propCx.DOTTY() === undefined) {
|
|
702
|
+
const properties = this.visitProperties(propCx).dict;
|
|
703
|
+
// Add new properties old value
|
|
704
|
+
writeInto[writeKey] = {...eq, properties};
|
|
705
|
+
} else {
|
|
706
|
+
// preserve old properties, add new value
|
|
707
|
+
writeInto[writeKey] = {...writeInto[writeKey], ...eq};
|
|
708
|
+
}
|
|
709
|
+
} else {
|
|
710
|
+
writeInto[writeKey] = eq;
|
|
711
|
+
}
|
|
712
|
+
return buildOn;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
visitTagReplaceProperties(ctx: TagReplacePropertiesContext): Tag {
|
|
716
|
+
const buildOn = getBuildOn(ctx);
|
|
717
|
+
const name = this.getPropName(ctx.propName());
|
|
718
|
+
const [writeKey, writeInto] = buildAccessPath(buildOn, name);
|
|
719
|
+
const propCx = ctx.properties();
|
|
720
|
+
const props = this.visitProperties(propCx);
|
|
721
|
+
if (ctx.DOTTY() === undefined) {
|
|
722
|
+
// No dots, thropw away the value
|
|
723
|
+
writeInto[writeKey] = {properties: props.dict};
|
|
724
|
+
} else {
|
|
725
|
+
/// DOTS, just update the properties
|
|
726
|
+
writeInto[writeKey].properties = props.dict;
|
|
727
|
+
}
|
|
728
|
+
return buildOn;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
visitTagUpdateProperties(ctx: TagUpdatePropertiesContext): Tag {
|
|
732
|
+
const buildOn = getBuildOn(ctx);
|
|
733
|
+
const name = this.getPropName(ctx.propName());
|
|
734
|
+
const [writeKey, writeInto] = buildAccessPath(buildOn, name);
|
|
735
|
+
const propCx = ctx.properties();
|
|
736
|
+
propCx['buildOn'] = Tag.tagFrom(writeInto[writeKey]);
|
|
737
|
+
const props = this.visitProperties(propCx);
|
|
738
|
+
const thisObj = writeInto[writeKey] || new Tag({});
|
|
739
|
+
const properties = {...thisObj.properties, ...props.dict};
|
|
740
|
+
writeInto[writeKey] = {...thisObj, properties};
|
|
741
|
+
return buildOn;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
visitTagDef(ctx: TagDefContext): Tag {
|
|
745
|
+
const buildOn = getBuildOn(ctx);
|
|
746
|
+
const path = this.getPropName(ctx.propName());
|
|
747
|
+
const [writeKey, writeInto] = buildAccessPath(buildOn, path);
|
|
748
|
+
if (ctx.MINUS()) {
|
|
749
|
+
delete writeInto[writeKey];
|
|
750
|
+
} else {
|
|
751
|
+
writeInto[writeKey] = new Tag({});
|
|
752
|
+
}
|
|
753
|
+
return buildOn;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
visitTagEmpty(ctx: TagEmptyContext): Tag {
|
|
757
|
+
const tagList = ctx['buildOn'] as Tag;
|
|
758
|
+
tagList.properties = {};
|
|
759
|
+
return tagList;
|
|
760
|
+
}
|
|
761
|
+
}
|