@malloydata/malloy 0.0.237-dev250225015031 → 0.0.237-dev250225213433

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 (38) hide show
  1. package/dist/annotation.d.ts +15 -0
  2. package/dist/annotation.js +86 -0
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.js +5 -3
  5. package/dist/lang/ast/types/annotation-elements.d.ts +1 -1
  6. package/dist/lang/ast/types/annotation-elements.js +2 -2
  7. package/dist/lang/ast/types/malloy-element.d.ts +1 -1
  8. package/dist/lang/ast/types/malloy-element.js +2 -2
  9. package/dist/lang/malloy-to-ast.d.ts +1 -1
  10. package/dist/lang/malloy-to-ast.js +2 -2
  11. package/dist/lang/parse-malloy.d.ts +1 -1
  12. package/dist/lang/parse-malloy.js +4 -3
  13. package/dist/lang/parse-utils.d.ts +0 -11
  14. package/dist/lang/parse-utils.js +4 -82
  15. package/dist/lang/syntax-errors/custom-error-messages.d.ts +4 -2
  16. package/dist/lang/syntax-errors/custom-error-messages.js +75 -16
  17. package/dist/lang/syntax-errors/malloy-error-strategy.d.ts +4 -6
  18. package/dist/lang/syntax-errors/malloy-error-strategy.js +3 -22
  19. package/dist/lang/syntax-errors/malloy-parser-error-listener.d.ts +1 -1
  20. package/dist/lang/syntax-errors/malloy-parser-error-listener.js +107 -10
  21. package/dist/lang/test/literals.spec.js +0 -34
  22. package/dist/lang/test/syntax-errors.spec.js +113 -59
  23. package/dist/malloy.d.ts +13 -8
  24. package/dist/malloy.js +23 -23
  25. package/dist/model/malloy_query.d.ts +3 -1
  26. package/dist/model/malloy_query.js +18 -3
  27. package/dist/model/materialization/utils.js +2 -2
  28. package/dist/to_stable.d.ts +3 -0
  29. package/dist/to_stable.js +170 -0
  30. package/package.json +3 -1
  31. package/dist/lang/lib/Malloy/MalloyTagLexer.d.ts +0 -42
  32. package/dist/lang/lib/Malloy/MalloyTagLexer.js +0 -385
  33. package/dist/lang/lib/Malloy/MalloyTagParser.d.ts +0 -180
  34. package/dist/lang/lib/Malloy/MalloyTagParser.js +0 -1051
  35. package/dist/lang/lib/Malloy/MalloyTagVisitor.d.ts +0 -120
  36. package/dist/lang/lib/Malloy/MalloyTagVisitor.js +0 -4
  37. package/dist/tags.d.ts +0 -72
  38. package/dist/tags.js +0 -512
package/dist/tags.js DELETED
@@ -1,512 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2023 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.Tag = void 0;
26
- const tree_1 = require("antlr4ts/tree");
27
- const MalloyTagLexer_1 = require("./lang/lib/Malloy/MalloyTagLexer");
28
- const MalloyTagParser_1 = require("./lang/lib/Malloy/MalloyTagParser");
29
- const antlr4ts_1 = require("antlr4ts");
30
- const parse_utils_1 = require("./lang/parse-utils");
31
- /**
32
- * Class for interacting with the parsed output of an annotation
33
- * containing the Malloy tag language. Used by the parser to
34
- * generate parsed data, and as an API to that data.
35
- * ```
36
- * tag.text(p?) => string value of tag.p or undefined
37
- * tag.array(p?) => Tag[] value of tag.p or undefined
38
- * tag.numeric(p?) => numeric value of tag.p or undefined
39
- * tag.textArray(p ?) => string[] value of elements in tag.p or undefined
40
- * tag.numericArray(p?) => string[] value of elements in tag.p or undefined
41
- * tag.tag(p?) => Tag value of tag.p
42
- * tag.has(p?) => boolean "tag contains tag.p"
43
- * tag.bare(p?) => tag.p exists and has no properties
44
- * tag.dict => Record<string,Tag> of tag properties
45
- * ```
46
- */
47
- class Tag {
48
- static tagFrom(from = {}) {
49
- if (from instanceof Tag) {
50
- return from;
51
- }
52
- return new Tag(from);
53
- }
54
- static id(t) {
55
- let thisTagId = Tag.ids.get(t);
56
- if (thisTagId === undefined) {
57
- thisTagId = Tag.nextTagId;
58
- Tag.ids.set(t, thisTagId);
59
- Tag.nextTagId += 1;
60
- }
61
- return thisTagId;
62
- }
63
- peek(indent = 0) {
64
- const spaces = ' '.repeat(indent);
65
- let str = `#${Tag.id(this)}`;
66
- if (this.properties === undefined &&
67
- this.eq &&
68
- typeof this.eq === 'string') {
69
- return str + `=${this.eq}`;
70
- }
71
- str += ' {';
72
- if (this.eq) {
73
- if (typeof this.eq === 'string') {
74
- str += `\n${spaces} =: ${this.eq}`;
75
- }
76
- else {
77
- str += `\n${spaces} =: [\n${spaces} ${this.eq
78
- .map(el => Tag.tagFrom(el).peek(indent + 4))
79
- .join(`\n${spaces} `)}\n${spaces} ]`;
80
- }
81
- }
82
- if (this.properties) {
83
- for (const k in this.properties) {
84
- const val = Tag.tagFrom(this.properties[k]);
85
- str += `\n${spaces} ${k}: ${val.peek(indent + 2)}`;
86
- }
87
- }
88
- str += `\n${spaces}}`;
89
- return str;
90
- }
91
- /**
92
- * Parse a line of Malloy tag language into a Tag which can be queried
93
- * @param source -- The source line to be parsed. If the string starts with #, then it skips
94
- * all characters up to the first space.
95
- * @param extending A tag which this line will be extending
96
- * @param importing Outer "scopes" for $() references
97
- * @returns Something shaped like { tag: Tag , log: ParseErrors[] }
98
- */
99
- static fromTagline(str, extending, ...importing) {
100
- // TODO -- Figure out the right thing about the URL
101
- const url = `internal://tag-parse/from-tag-line?${encodeURIComponent(str)}`;
102
- return parseTagline(str, extending, importing, url, 0, 0);
103
- }
104
- static addModelScope(spec, modelScope) {
105
- const useSpec = spec ? { ...spec } : {};
106
- if (useSpec.scopes) {
107
- useSpec.scopes = useSpec.scopes.concat(modelScope);
108
- }
109
- else {
110
- useSpec.scopes = [modelScope];
111
- }
112
- return useSpec;
113
- }
114
- static annotationToTaglines(annote, prefix) {
115
- annote || (annote = {});
116
- const tagLines = annote.inherits
117
- ? Tag.annotationToTaglines(annote.inherits, prefix)
118
- : [];
119
- function prefixed(na) {
120
- const ret = [];
121
- for (const n of na || []) {
122
- if (prefix === undefined || n.text.match(prefix)) {
123
- ret.push(n.text);
124
- }
125
- }
126
- return ret;
127
- }
128
- return tagLines.concat(prefixed(annote.blockNotes), prefixed(annote.notes));
129
- }
130
- static annotationToTag(annote, spec = {}) {
131
- let extending = spec.extending || new Tag();
132
- const prefix = spec.prefix || /^##? /;
133
- annote || (annote = {});
134
- const allErrs = [];
135
- if (annote.inherits) {
136
- const inherits = Tag.annotationToTag(annote.inherits, spec);
137
- allErrs.push(...inherits.log);
138
- extending = inherits.tag;
139
- }
140
- const allNotes = [];
141
- if (annote.blockNotes) {
142
- allNotes.push(...annote.blockNotes);
143
- }
144
- if (annote.notes) {
145
- allNotes.push(...annote.notes);
146
- }
147
- for (const note of allNotes) {
148
- if (note.text.match(prefix)) {
149
- const noteParse = parseTagline(note.text, extending, spec.scopes || [], note.at.url, note.at.range.start.line, note.at.range.start.character);
150
- extending = noteParse.tag;
151
- allErrs.push(...noteParse.log);
152
- }
153
- }
154
- return { tag: extending, log: allErrs };
155
- }
156
- constructor(from = {}) {
157
- if (from.eq) {
158
- this.eq = from.eq;
159
- }
160
- if (from.properties) {
161
- this.properties = from.properties;
162
- }
163
- }
164
- find(at) {
165
- let lookAt = Tag.tagFrom(this);
166
- for (const seg of at) {
167
- const lookup = lookAt.properties && lookAt.properties[seg];
168
- if (!lookup) {
169
- return;
170
- }
171
- lookAt = Tag.tagFrom(lookup);
172
- }
173
- return lookAt;
174
- }
175
- tag(...at) {
176
- return this.find(at);
177
- }
178
- has(...at) {
179
- return this.find(at) !== undefined;
180
- }
181
- text(...at) {
182
- var _a;
183
- const str = (_a = this.find(at)) === null || _a === void 0 ? void 0 : _a.eq;
184
- if (typeof str === 'string') {
185
- return str;
186
- }
187
- }
188
- numeric(...at) {
189
- var _a;
190
- const str = (_a = this.find(at)) === null || _a === void 0 ? void 0 : _a.eq;
191
- if (typeof str === 'string') {
192
- const num = Number.parseFloat(str);
193
- if (!Number.isNaN(num)) {
194
- return num;
195
- }
196
- }
197
- }
198
- bare(...at) {
199
- const p = this.find(at);
200
- if (p === undefined) {
201
- return;
202
- }
203
- return (p.properties === undefined || Object.entries(p.properties).length === 0);
204
- }
205
- get dict() {
206
- const newDict = {};
207
- if (this.properties) {
208
- for (const key in this.properties) {
209
- newDict[key] = Tag.tagFrom(this.properties[key]);
210
- }
211
- }
212
- return newDict;
213
- }
214
- array(...at) {
215
- var _a;
216
- const array = (_a = this.find(at)) === null || _a === void 0 ? void 0 : _a.eq;
217
- if (array === undefined || typeof array === 'string') {
218
- return undefined;
219
- }
220
- return array.map(el => typeof el === 'string' ? new Tag({ eq: el }) : Tag.tagFrom(el));
221
- }
222
- textArray(...at) {
223
- var _a;
224
- const array = (_a = this.find(at)) === null || _a === void 0 ? void 0 : _a.eq;
225
- if (array === undefined || typeof array === 'string') {
226
- return undefined;
227
- }
228
- return array.reduce((allStrs, el) => typeof el.eq === 'string' ? allStrs.concat(el.eq) : allStrs, []);
229
- }
230
- numericArray(...at) {
231
- var _a;
232
- const array = (_a = this.find(at)) === null || _a === void 0 ? void 0 : _a.eq;
233
- if (array === undefined || typeof array === 'string') {
234
- return undefined;
235
- }
236
- return array.reduce((allNums, el) => {
237
- if (typeof el.eq === 'string') {
238
- const num = Number.parseFloat(el.eq);
239
- if (!Number.isNaN(num)) {
240
- return allNums.concat(num);
241
- }
242
- }
243
- return allNums;
244
- }, []);
245
- }
246
- // Has the sometimes desireable side effect of initalizing properties
247
- getProperties() {
248
- if (this.properties === undefined) {
249
- this.properties = {};
250
- }
251
- return this.properties;
252
- }
253
- clone() {
254
- return new Tag(structuredClone(this));
255
- }
256
- }
257
- exports.Tag = Tag;
258
- // --- Just for debugging ---
259
- Tag.ids = new Map();
260
- Tag.nextTagId = 1000;
261
- class TagErrorListener {
262
- constructor(sourceURL, atLine, fromChar) {
263
- this.sourceURL = sourceURL;
264
- this.atLine = atLine;
265
- this.fromChar = fromChar;
266
- this.log = [];
267
- }
268
- syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, _e) {
269
- const errAt = {
270
- line: this.atLine,
271
- character: this.fromChar + charPositionInLine,
272
- };
273
- const range = { start: errAt, end: errAt };
274
- const logMsg = {
275
- code: 'tag-parse-syntax-error',
276
- message: msg,
277
- at: { url: this.sourceURL, range },
278
- severity: 'error',
279
- };
280
- this.log.push(logMsg);
281
- }
282
- semanticError(cx, code, msg) {
283
- const left = this.fromChar + cx.start.charPositionInLine;
284
- const errAt = {
285
- line: this.atLine,
286
- character: left,
287
- };
288
- const range = { start: errAt, end: errAt };
289
- const logMsg = {
290
- code,
291
- message: msg,
292
- at: { url: this.sourceURL, range },
293
- severity: 'error',
294
- };
295
- this.log.push(logMsg);
296
- }
297
- }
298
- function getBuildOn(ctx) {
299
- const buildOn = ctx['buildOn'];
300
- if (buildOn instanceof Tag) {
301
- return buildOn;
302
- }
303
- return new Tag();
304
- }
305
- /**
306
- * When chasing a path reference, the two interesting gestures are to
307
- * find the path-ed tag so it can be extended, or to find the path tag
308
- * so it can be deleted. This returns the parent and the final tag
309
- * so that the caller can delete the tag with delete parent.tagName
310
- * or assign to it with parent[tagName] = new_value
311
- */
312
- function buildAccessPath(buildOn, path) {
313
- var _a;
314
- var _b;
315
- let parentPropertyObject = buildOn.getProperties();
316
- for (const p of path.slice(0, path.length - 1)) {
317
- let next;
318
- if (parentPropertyObject[p] === undefined) {
319
- next = new Tag({});
320
- parentPropertyObject[p] = next;
321
- }
322
- else {
323
- // The access that we are performing requires that `.properties` be the
324
- // same JS object (not equal, but identical), and `Tag.tagFrom` only copies
325
- // the exact object in if it is actually present.
326
- (_a = (_b = parentPropertyObject[p]).properties) !== null && _a !== void 0 ? _a : (_b.properties = {});
327
- next = Tag.tagFrom(parentPropertyObject[p]);
328
- }
329
- parentPropertyObject = next.getProperties();
330
- }
331
- return [path[path.length - 1], parentPropertyObject];
332
- }
333
- function getString(ctx) {
334
- if (ctx.SQ_STRING() || ctx.DQ_STRING()) {
335
- return (0, parse_utils_1.parseString)(ctx.text, ctx.text[0]);
336
- }
337
- return ctx.text;
338
- }
339
- function parseTagline(source, extending, outerScope, sourceURL, onLine, atChar) {
340
- if (source[0] === '#') {
341
- const skipTo = source.indexOf(' ');
342
- if (skipTo > 0) {
343
- source = source.slice(skipTo);
344
- }
345
- else {
346
- source = '';
347
- }
348
- }
349
- const inputStream = antlr4ts_1.CharStreams.fromString(source);
350
- const lexer = new MalloyTagLexer_1.MalloyTagLexer(inputStream);
351
- const tokenStream = new antlr4ts_1.CommonTokenStream(lexer);
352
- const pLog = new TagErrorListener(sourceURL, onLine, atChar);
353
- const taglineParser = new MalloyTagParser_1.MalloyTagParser(tokenStream);
354
- taglineParser.removeErrorListeners();
355
- taglineParser.addErrorListener(pLog);
356
- const tagTree = taglineParser.tagLine();
357
- const treeWalker = new TaglineParser(outerScope, pLog);
358
- const tag = treeWalker.tagLineToTag(tagTree, extending);
359
- return { tag, log: pLog.log };
360
- }
361
- class TaglineParser extends tree_1.AbstractParseTreeVisitor {
362
- constructor(outerScopes = [], msgLog) {
363
- super();
364
- this.scopes = [];
365
- this.msgLog = msgLog;
366
- this.scopes.unshift(...outerScopes);
367
- }
368
- defaultResult() {
369
- return new Tag();
370
- }
371
- visitString(ctx) {
372
- return new Tag({ eq: getString(ctx) });
373
- }
374
- getPropName(ctx) {
375
- return ctx
376
- .identifier()
377
- .map(cx => cx.BARE_STRING() ? cx.text : (0, parse_utils_1.parseString)(cx.text, cx.text[0]));
378
- }
379
- getTags(tags, tagLine) {
380
- for (const tagSpec of tags) {
381
- // Stash the current state of this tag in the context and then visit it
382
- // visit functions should alter the tagLine
383
- tagSpec['buildOn'] = tagLine;
384
- this.visit(tagSpec);
385
- }
386
- return tagLine;
387
- }
388
- tagLineToTag(ctx, extending) {
389
- extending = (extending === null || extending === void 0 ? void 0 : extending.clone()) || new Tag({});
390
- this.scopes.unshift(extending);
391
- this.getTags(ctx.tagSpec(), extending);
392
- return extending;
393
- }
394
- visitTagLine(_ctx) {
395
- throw new Error('INTERNAL: ERROR: Call tagLineToTag, not vistTagLine');
396
- return this.defaultResult();
397
- }
398
- visitProperties(ctx) {
399
- return this.getTags(ctx.tagSpec(), getBuildOn(ctx));
400
- }
401
- visitArrayValue(ctx) {
402
- return new Tag({ eq: this.getArray(ctx) });
403
- }
404
- getArray(ctx) {
405
- return ctx.arrayElement().map(v => this.visit(v));
406
- }
407
- visitArrayElement(ctx) {
408
- const propCx = ctx.properties();
409
- const properties = propCx ? this.visitProperties(propCx) : undefined;
410
- const strCx = ctx.string();
411
- let value = strCx ? getString(strCx) : undefined;
412
- const arrayCx = ctx.arrayValue();
413
- if (arrayCx) {
414
- value = this.getArray(arrayCx);
415
- }
416
- if (properties) {
417
- if (value) {
418
- properties.eq = value;
419
- }
420
- return properties;
421
- }
422
- const refCx = ctx.reference();
423
- if (refCx) {
424
- return this.visitReference(refCx);
425
- }
426
- return new Tag({ eq: value });
427
- }
428
- visitReference(ctx) {
429
- const path = this.getPropName(ctx.propName());
430
- for (const scope of this.scopes) {
431
- // first scope which has the first component gets to resolve the whole path
432
- if (scope.has(path[0])) {
433
- const refTo = scope.tag(...path);
434
- if (refTo) {
435
- return refTo.clone();
436
- }
437
- break;
438
- }
439
- }
440
- this.msgLog.semanticError(ctx, 'tag-property-not-found', `Reference to undefined property ${path.join('.')}`);
441
- return this.defaultResult();
442
- }
443
- visitTagEq(ctx) {
444
- const buildOn = getBuildOn(ctx);
445
- const name = this.getPropName(ctx.propName());
446
- const [writeKey, writeInto] = buildAccessPath(buildOn, name);
447
- const eq = this.visit(ctx.eqValue());
448
- const propCx = ctx.properties();
449
- if (propCx) {
450
- // a.b.c { -y } means i want to do -y on
451
- if (propCx.DOTTY() === undefined) {
452
- const properties = this.visitProperties(propCx).dict;
453
- // Add new properties old value
454
- writeInto[writeKey] = { ...eq, properties };
455
- }
456
- else {
457
- // preserve old properties, add new value
458
- writeInto[writeKey] = { ...writeInto[writeKey], ...eq };
459
- }
460
- }
461
- else {
462
- writeInto[writeKey] = eq;
463
- }
464
- return buildOn;
465
- }
466
- visitTagReplaceProperties(ctx) {
467
- const buildOn = getBuildOn(ctx);
468
- const name = this.getPropName(ctx.propName());
469
- const [writeKey, writeInto] = buildAccessPath(buildOn, name);
470
- const propCx = ctx.properties();
471
- const props = this.visitProperties(propCx);
472
- if (ctx.DOTTY() === undefined) {
473
- // No dots, thropw away the value
474
- writeInto[writeKey] = { properties: props.dict };
475
- }
476
- else {
477
- /// DOTS, just update the properties
478
- writeInto[writeKey].properties = props.dict;
479
- }
480
- return buildOn;
481
- }
482
- visitTagUpdateProperties(ctx) {
483
- const buildOn = getBuildOn(ctx);
484
- const name = this.getPropName(ctx.propName());
485
- const [writeKey, writeInto] = buildAccessPath(buildOn, name);
486
- const propCx = ctx.properties();
487
- propCx['buildOn'] = Tag.tagFrom(writeInto[writeKey]);
488
- const props = this.visitProperties(propCx);
489
- const thisObj = writeInto[writeKey] || new Tag({});
490
- const properties = { ...thisObj.properties, ...props.dict };
491
- writeInto[writeKey] = { ...thisObj, properties };
492
- return buildOn;
493
- }
494
- visitTagDef(ctx) {
495
- const buildOn = getBuildOn(ctx);
496
- const path = this.getPropName(ctx.propName());
497
- const [writeKey, writeInto] = buildAccessPath(buildOn, path);
498
- if (ctx.MINUS()) {
499
- delete writeInto[writeKey];
500
- }
501
- else {
502
- writeInto[writeKey] = new Tag({});
503
- }
504
- return buildOn;
505
- }
506
- visitTagEmpty(ctx) {
507
- const tagList = ctx['buildOn'];
508
- tagList.properties = {};
509
- return tagList;
510
- }
511
- }
512
- //# sourceMappingURL=tags.js.map