@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.
Files changed (42) hide show
  1. package/README.md +0 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +38 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/lib/Malloy/MalloyTagLexer.d.ts +42 -0
  6. package/dist/lib/Malloy/MalloyTagLexer.js +385 -0
  7. package/dist/lib/Malloy/MalloyTagLexer.js.map +1 -0
  8. package/dist/lib/Malloy/MalloyTagParser.d.ts +180 -0
  9. package/dist/lib/Malloy/MalloyTagParser.js +1051 -0
  10. package/dist/lib/Malloy/MalloyTagParser.js.map +1 -0
  11. package/dist/lib/Malloy/MalloyTagVisitor.d.ts +120 -0
  12. package/dist/lib/Malloy/MalloyTagVisitor.js +4 -0
  13. package/dist/lib/Malloy/MalloyTagVisitor.js.map +1 -0
  14. package/dist/tags.d.ts +90 -0
  15. package/dist/tags.js +654 -0
  16. package/dist/tags.js.map +1 -0
  17. package/dist/tags.spec.d.ts +8 -0
  18. package/dist/tags.spec.js +318 -0
  19. package/dist/tags.spec.js.map +1 -0
  20. package/dist/util.d.ts +11 -0
  21. package/dist/util.js +84 -0
  22. package/dist/util.js.map +1 -0
  23. package/dist/util.spec.d.ts +1 -0
  24. package/dist/util.spec.js +43 -0
  25. package/dist/util.spec.js.map +1 -0
  26. package/package.json +30 -0
  27. package/scripts/build_parser.js +98 -0
  28. package/src/MalloyTag.g4 +102 -0
  29. package/src/index.ts +8 -0
  30. package/src/lib/Malloy/MalloyTag.interp +61 -0
  31. package/src/lib/Malloy/MalloyTag.tokens +32 -0
  32. package/src/lib/Malloy/MalloyTagLexer.interp +85 -0
  33. package/src/lib/Malloy/MalloyTagLexer.tokens +32 -0
  34. package/src/lib/Malloy/MalloyTagLexer.ts +386 -0
  35. package/src/lib/Malloy/MalloyTagParser.ts +1048 -0
  36. package/src/lib/Malloy/MalloyTagVisitor.ts +141 -0
  37. package/src/lib/Malloy/_BUILD_DIGEST_ +1 -0
  38. package/src/tags.spec.ts +331 -0
  39. package/src/tags.ts +761 -0
  40. package/src/util.spec.ts +43 -0
  41. package/src/util.ts +73 -0
  42. package/tsconfig.json +11 -0
@@ -0,0 +1,141 @@
1
+ // Generated from MalloyTag.g4 by ANTLR 4.9.0-SNAPSHOT
2
+
3
+
4
+ import { ParseTreeVisitor } from "antlr4ts/tree/ParseTreeVisitor";
5
+
6
+ import { TagEqContext } from "./MalloyTagParser";
7
+ import { TagReplacePropertiesContext } from "./MalloyTagParser";
8
+ import { TagUpdatePropertiesContext } from "./MalloyTagParser";
9
+ import { TagDefContext } from "./MalloyTagParser";
10
+ import { TagEmptyContext } from "./MalloyTagParser";
11
+ import { TagLineContext } from "./MalloyTagParser";
12
+ import { TagSpecContext } from "./MalloyTagParser";
13
+ import { StringContext } from "./MalloyTagParser";
14
+ import { IdentifierContext } from "./MalloyTagParser";
15
+ import { PropNameContext } from "./MalloyTagParser";
16
+ import { EqValueContext } from "./MalloyTagParser";
17
+ import { ArrayElementContext } from "./MalloyTagParser";
18
+ import { ReferenceContext } from "./MalloyTagParser";
19
+ import { ArrayValueContext } from "./MalloyTagParser";
20
+ import { PropertiesContext } from "./MalloyTagParser";
21
+
22
+
23
+ /**
24
+ * This interface defines a complete generic visitor for a parse tree produced
25
+ * by `MalloyTagParser`.
26
+ *
27
+ * @param <Result> The return type of the visit operation. Use `void` for
28
+ * operations with no return type.
29
+ */
30
+ export interface MalloyTagVisitor<Result> extends ParseTreeVisitor<Result> {
31
+ /**
32
+ * Visit a parse tree produced by the `tagEq`
33
+ * labeled alternative in `MalloyTagParser.tagSpec`.
34
+ * @param ctx the parse tree
35
+ * @return the visitor result
36
+ */
37
+ visitTagEq?: (ctx: TagEqContext) => Result;
38
+
39
+ /**
40
+ * Visit a parse tree produced by the `tagReplaceProperties`
41
+ * labeled alternative in `MalloyTagParser.tagSpec`.
42
+ * @param ctx the parse tree
43
+ * @return the visitor result
44
+ */
45
+ visitTagReplaceProperties?: (ctx: TagReplacePropertiesContext) => Result;
46
+
47
+ /**
48
+ * Visit a parse tree produced by the `tagUpdateProperties`
49
+ * labeled alternative in `MalloyTagParser.tagSpec`.
50
+ * @param ctx the parse tree
51
+ * @return the visitor result
52
+ */
53
+ visitTagUpdateProperties?: (ctx: TagUpdatePropertiesContext) => Result;
54
+
55
+ /**
56
+ * Visit a parse tree produced by the `tagDef`
57
+ * labeled alternative in `MalloyTagParser.tagSpec`.
58
+ * @param ctx the parse tree
59
+ * @return the visitor result
60
+ */
61
+ visitTagDef?: (ctx: TagDefContext) => Result;
62
+
63
+ /**
64
+ * Visit a parse tree produced by the `tagEmpty`
65
+ * labeled alternative in `MalloyTagParser.tagSpec`.
66
+ * @param ctx the parse tree
67
+ * @return the visitor result
68
+ */
69
+ visitTagEmpty?: (ctx: TagEmptyContext) => Result;
70
+
71
+ /**
72
+ * Visit a parse tree produced by `MalloyTagParser.tagLine`.
73
+ * @param ctx the parse tree
74
+ * @return the visitor result
75
+ */
76
+ visitTagLine?: (ctx: TagLineContext) => Result;
77
+
78
+ /**
79
+ * Visit a parse tree produced by `MalloyTagParser.tagSpec`.
80
+ * @param ctx the parse tree
81
+ * @return the visitor result
82
+ */
83
+ visitTagSpec?: (ctx: TagSpecContext) => Result;
84
+
85
+ /**
86
+ * Visit a parse tree produced by `MalloyTagParser.string`.
87
+ * @param ctx the parse tree
88
+ * @return the visitor result
89
+ */
90
+ visitString?: (ctx: StringContext) => Result;
91
+
92
+ /**
93
+ * Visit a parse tree produced by `MalloyTagParser.identifier`.
94
+ * @param ctx the parse tree
95
+ * @return the visitor result
96
+ */
97
+ visitIdentifier?: (ctx: IdentifierContext) => Result;
98
+
99
+ /**
100
+ * Visit a parse tree produced by `MalloyTagParser.propName`.
101
+ * @param ctx the parse tree
102
+ * @return the visitor result
103
+ */
104
+ visitPropName?: (ctx: PropNameContext) => Result;
105
+
106
+ /**
107
+ * Visit a parse tree produced by `MalloyTagParser.eqValue`.
108
+ * @param ctx the parse tree
109
+ * @return the visitor result
110
+ */
111
+ visitEqValue?: (ctx: EqValueContext) => Result;
112
+
113
+ /**
114
+ * Visit a parse tree produced by `MalloyTagParser.arrayElement`.
115
+ * @param ctx the parse tree
116
+ * @return the visitor result
117
+ */
118
+ visitArrayElement?: (ctx: ArrayElementContext) => Result;
119
+
120
+ /**
121
+ * Visit a parse tree produced by `MalloyTagParser.reference`.
122
+ * @param ctx the parse tree
123
+ * @return the visitor result
124
+ */
125
+ visitReference?: (ctx: ReferenceContext) => Result;
126
+
127
+ /**
128
+ * Visit a parse tree produced by `MalloyTagParser.arrayValue`.
129
+ * @param ctx the parse tree
130
+ * @return the visitor result
131
+ */
132
+ visitArrayValue?: (ctx: ArrayValueContext) => Result;
133
+
134
+ /**
135
+ * Visit a parse tree produced by `MalloyTagParser.properties`.
136
+ * @param ctx the parse tree
137
+ * @return the visitor result
138
+ */
139
+ visitProperties?: (ctx: PropertiesContext) => Result;
140
+ }
141
+
@@ -0,0 +1 @@
1
+ {"/home/runner/work/malloy/malloy/packages/malloy-tag/scripts/build_parser.js":"37886ea9-7b84-5377-bc58-fe80ec897c23","MalloyTag.g4":"b40515f5-cbf3-528f-bb13-81707cb0a69b"}
@@ -0,0 +1,331 @@
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 {TagDict, Tag} from './tags';
25
+
26
+ declare global {
27
+ // eslint-disable-next-line @typescript-eslint/no-namespace
28
+ namespace jest {
29
+ interface Matchers<R> {
30
+ tagsAre(t: TagDict): R;
31
+ }
32
+ }
33
+ }
34
+
35
+ expect.extend({
36
+ tagsAre(src: string | Tag, result: Tag) {
37
+ if (typeof src === 'string') {
38
+ const {tag, log} = Tag.fromTagLine(src, 0, undefined);
39
+ const errs = log.map(e => e.message);
40
+ if (log.length > 0) {
41
+ return {
42
+ pass: false,
43
+ message: () => `${src}: Tag Parsing Error(s)\n${errs.join('\n')}`,
44
+ };
45
+ }
46
+ src = tag;
47
+ }
48
+ const got = src.properties;
49
+ if (this.equals(got, result)) {
50
+ return {
51
+ pass: true,
52
+ message: () => 'Parse returned expected object',
53
+ };
54
+ }
55
+ return {
56
+ pass: false,
57
+ message: () => this.utils.diff(result, got) ?? 'Not different',
58
+ };
59
+ },
60
+ });
61
+
62
+ type TagTestTuple = [string, TagDict];
63
+ describe('tagParse to Tag', () => {
64
+ const tagTests: TagTestTuple[] = [
65
+ ['just_name', {just_name: {}}],
66
+ ['name=bare_string', {name: {eq: 'bare_string'}}],
67
+ ['name="quoted_string"', {name: {eq: 'quoted_string'}}],
68
+ ['name {prop1}', {name: {properties: {prop1: {}}}}],
69
+ [
70
+ 'name {prop1 prop2=value}',
71
+ {
72
+ name: {
73
+ properties: {
74
+ prop1: {},
75
+ prop2: {eq: 'value'},
76
+ },
77
+ },
78
+ },
79
+ ],
80
+ ['name.prop', {name: {properties: {prop: {}}}}],
81
+ ['name.prop=value', {name: {properties: {prop: {eq: 'value'}}}}],
82
+ [
83
+ 'name.prop.sub=value',
84
+ {name: {properties: {prop: {properties: {sub: {eq: 'value'}}}}}},
85
+ ],
86
+ [
87
+ 'name{first3=[a, b, c]}',
88
+ {name: {properties: {first3: {eq: [{eq: 'a'}, {eq: 'b'}, {eq: 'c'}]}}}},
89
+ ],
90
+ ['name{first1=[a,]}', {name: {properties: {first1: {eq: [{eq: 'a'}]}}}}],
91
+ [
92
+ 'name{first=[a {A}]}',
93
+ {name: {properties: {first: {eq: [{eq: 'a', properties: {A: {}}}]}}}},
94
+ ],
95
+ [
96
+ 'name{first=[{A}]}',
97
+ {name: {properties: {first: {eq: [{properties: {A: {}}}]}}}},
98
+ ],
99
+ ['name=value {prop}', {name: {eq: 'value', properties: {prop: {}}}}],
100
+ [
101
+ 'name.prop={prop2}',
102
+ {name: {properties: {prop: {properties: {prop2: {}}}}}},
103
+ ],
104
+ ['no yes -no', {yes: {}}],
105
+
106
+ // TODO interesting behavior that removing a non-existant element, or the last element,
107
+ // does not remove the `properties`.
108
+ ['x -x.y', {x: {properties: {}}}],
109
+ ['x={y} -x.y', {x: {properties: {}}}],
110
+
111
+ ['x={y z} -x.y', {x: {properties: {z: {}}}}],
112
+ ['x={y z} x {-y}', {x: {properties: {z: {}}}}],
113
+ ['x=1 x {xx=11}', {x: {eq: '1', properties: {xx: {eq: '11'}}}}],
114
+ ['x.y=xx x=1 {...}', {x: {eq: '1', properties: {y: {eq: 'xx'}}}}],
115
+ ['a {b c} a=1', {a: {eq: '1'}}],
116
+ ['a=1 a=...{b}', {a: {eq: '1', properties: {b: {}}}}],
117
+ [
118
+ 'a=red { shade=dark } color=$(a) shade=$(a.shade)',
119
+ {
120
+ a: {eq: 'red', properties: {shade: {eq: 'dark'}}},
121
+ color: {eq: 'red', properties: {shade: {eq: 'dark'}}},
122
+ shade: {eq: 'dark'},
123
+ },
124
+ ],
125
+ ['x=.01', {x: {eq: '.01'}}],
126
+ ['x=-7', {x: {eq: '-7'}}],
127
+ ['x=7', {x: {eq: '7'}}],
128
+ ['x=7.0', {x: {eq: '7.0'}}],
129
+ ['x=.7', {x: {eq: '.7'}}],
130
+ ['x=.7e2', {x: {eq: '.7e2'}}],
131
+ ['x=7E2', {x: {eq: '7E2'}}],
132
+ ['`spacey name`=Zaphod', {'spacey name': {eq: 'Zaphod'}}],
133
+ [
134
+ 'image { alt=hello { field=department } }',
135
+ {
136
+ image: {
137
+ properties: {
138
+ alt: {eq: 'hello', properties: {field: {eq: 'department'}}},
139
+ },
140
+ },
141
+ },
142
+ ],
143
+ [
144
+ 'image image.alt=hello image.alt.field=department',
145
+ {
146
+ image: {
147
+ properties: {
148
+ alt: {eq: 'hello', properties: {field: {eq: 'department'}}},
149
+ },
150
+ },
151
+ },
152
+ ],
153
+ ['can remove.properties -...', {}],
154
+ ];
155
+ test.each(tagTests)('tag %s', (expression: string, expected: TagDict) => {
156
+ expect(expression).tagsAre(expected);
157
+ });
158
+ test.skip('unskip to debug just one of the expressions', () => {
159
+ const x: TagTestTuple = ['word -...', {}];
160
+ expect(x[0]).tagsAre(x[1]);
161
+ });
162
+ });
163
+
164
+ describe('Tag access', () => {
165
+ test('just text', () => {
166
+ const strToParse = 'a=b';
167
+ const getTags = Tag.fromTagLine(strToParse, undefined);
168
+ expect(getTags.log).toEqual([]);
169
+ const a = getTags.tag.tag('a');
170
+ expect(a).toBeDefined();
171
+ expect(a?.text()).toEqual('b');
172
+ });
173
+ test('tag path', () => {
174
+ const strToParse = 'a.b.c.d.e=f';
175
+ const tagParse = Tag.fromTagLine(strToParse, undefined);
176
+ expect(tagParse.log).toEqual([]);
177
+ const abcde = tagParse.tag.tag('a', 'b', 'c', 'd', 'e');
178
+ expect(abcde).toBeDefined();
179
+ expect(abcde?.text()).toEqual('f');
180
+ });
181
+ test('just array', () => {
182
+ const strToParse = 'a=[b]';
183
+ const getTags = Tag.fromTagLine(strToParse, undefined);
184
+ expect(getTags.log).toEqual([]);
185
+ const a = getTags.tag.tag('a');
186
+ const aval = a?.array();
187
+ expect(aval).toBeDefined();
188
+ if (aval) {
189
+ expect(aval.length).toEqual(1);
190
+ expect(aval[0].text()).toEqual('b');
191
+ }
192
+ });
193
+ test('tag path into array', () => {
194
+ const strToParse = 'a.b.c = [{d=e}]';
195
+ const tagParse = Tag.fromTagLine(strToParse, undefined);
196
+ expect(tagParse.log).toEqual([]);
197
+ const abcde = tagParse.tag.tag('a', 'b', 'c', 0, 'd');
198
+ expect(abcde).toBeDefined();
199
+ expect(abcde?.text()).toEqual('e');
200
+ });
201
+ test('array as text', () => {
202
+ const strToParse = 'a=[b]';
203
+ const getTags = Tag.fromTagLine(strToParse, undefined);
204
+ expect(getTags.log).toEqual([]);
205
+ const a = getTags.tag.tag('a');
206
+ expect(a).toBeDefined();
207
+ expect(a?.text()).toBeUndefined();
208
+ });
209
+ test('text as array', () => {
210
+ const strToParse = 'a=b';
211
+ const getTags = Tag.fromTagLine(strToParse, undefined);
212
+ expect(getTags.log).toEqual([]);
213
+ const a = getTags.tag.tag('a');
214
+ expect(a).toBeDefined();
215
+ expect(a?.array()).toBeUndefined();
216
+ });
217
+ test('just numeric', () => {
218
+ const strToParse = 'a=7';
219
+ const getTags = Tag.fromTagLine(strToParse, undefined);
220
+ expect(getTags.log).toEqual([]);
221
+ const a = getTags.tag.tag('a');
222
+ expect(a).toBeDefined();
223
+ const n = a?.numeric();
224
+ expect(typeof n).toBe('number');
225
+ expect(n).toEqual(7);
226
+ });
227
+ test('text as numeric', () => {
228
+ const strToParse = 'a=seven';
229
+ const getTags = Tag.fromTagLine(strToParse, undefined);
230
+ expect(getTags.log).toEqual([]);
231
+ const a = getTags.tag.tag('a');
232
+ expect(a).toBeDefined();
233
+ const n = a?.numeric();
234
+ expect(n).toBeUndefined();
235
+ });
236
+ test('array as numeric', () => {
237
+ const strToParse = 'a=[seven]';
238
+ const getTags = Tag.fromTagLine(strToParse, undefined);
239
+ expect(getTags.log).toEqual([]);
240
+ const a = getTags.tag.tag('a');
241
+ expect(a).toBeDefined();
242
+ const n = a?.numeric();
243
+ expect(n).toBeUndefined();
244
+ });
245
+ test('full text array', () => {
246
+ const strToParse = 'a=[b,c]';
247
+ const getTags = Tag.fromTagLine(strToParse, undefined);
248
+ expect(getTags.log).toEqual([]);
249
+ const a = getTags.tag.tag('a');
250
+ expect(a).toBeDefined();
251
+ const ais = a?.textArray();
252
+ expect(ais).toEqual(['b', 'c']);
253
+ });
254
+ test('filtered text array', () => {
255
+ const strToParse = 'a=[b,c,{d}]';
256
+ const getTags = Tag.fromTagLine(strToParse, undefined);
257
+ expect(getTags.log).toEqual([]);
258
+ const a = getTags.tag.tag('a');
259
+ expect(a).toBeDefined();
260
+ const ais = a?.textArray();
261
+ expect(ais).toEqual(['b', 'c']);
262
+ });
263
+ test('full numeric array', () => {
264
+ const strToParse = 'a=[1,2]';
265
+ const getTags = Tag.fromTagLine(strToParse, undefined);
266
+ expect(getTags.log).toEqual([]);
267
+ const a = getTags.tag.tag('a');
268
+ expect(a).toBeDefined();
269
+ const ais = a?.numericArray();
270
+ expect(ais).toEqual([1, 2]);
271
+ });
272
+ test('filtered numeric array', () => {
273
+ const strToParse = 'a=[1,2,three]';
274
+ const getTags = Tag.fromTagLine(strToParse);
275
+ expect(getTags.log).toEqual([]);
276
+ const a = getTags.tag.tag('a');
277
+ expect(a).toBeDefined();
278
+ const ais = a?.numericArray();
279
+ expect(ais).toEqual([1, 2]);
280
+ });
281
+ test('has', () => {
282
+ const strToParse = 'a b.d';
283
+ const getTags = Tag.fromTagLine(strToParse);
284
+ expect(getTags.log).toEqual([]);
285
+ expect(getTags.tag.has('a')).toBeTruthy();
286
+ expect(getTags.tag.has('b', 'd')).toBeTruthy();
287
+ expect(getTags.tag.has('c')).toBeFalsy();
288
+ });
289
+ test('property access on existing tag (which does not yet have properties)', () => {
290
+ const parsePlot = Tag.fromTagLine('# plot');
291
+ const parsed = Tag.fromTagLine('# plot.x=2', 0, parsePlot.tag);
292
+ const allTags = parsed.tag;
293
+ const plotTag = allTags.tag('plot');
294
+ const xTag = plotTag!.tag('x');
295
+ const x = xTag!.numeric();
296
+ expect(parsed.tag.numeric('plot', 'x')).toEqual(2);
297
+ expect(plotTag!.numeric('x')).toEqual(2);
298
+ expect(x).toEqual(2);
299
+ });
300
+ test('set tag', () => {
301
+ const base = Tag.withPrefix('# ');
302
+ const ext = base.set(['a', 'b', 0], 3).set(['a', 'b', 1], 4);
303
+ expect(ext).tagsAre({
304
+ a: {properties: {b: {eq: [{eq: '3'}, {eq: '4'}]}}},
305
+ });
306
+ expect(ext.toString()).toBe('# a.b = [3, 4]');
307
+ });
308
+ test('soft remove', () => {
309
+ const base = Tag.fromTagLine('# a.b.c = [{ d = 1 }]').tag;
310
+ const ext = base.delete('a', 'b', 'c', 0, 'd').delete('a', 'b', 'c', 0);
311
+ expect(ext).tagsAre({
312
+ a: {properties: {b: {properties: {c: {eq: []}}}}},
313
+ });
314
+ expect(ext.toString()).toBe('# a.b.c = []');
315
+ });
316
+ test('hard remove', () => {
317
+ const base = Tag.fromTagLine('# hello').tag;
318
+ const ext = base.unset('goodbye').unset('a', 'dieu');
319
+ expect(ext).tagsAre({
320
+ hello: {},
321
+ goodbye: {deleted: true},
322
+ a: {properties: {dieu: {deleted: true}}},
323
+ });
324
+ expect(ext.toString()).toBe('# hello -goodbye a { -dieu }');
325
+ });
326
+ test('set with different prefix', () => {
327
+ const base = Tag.withPrefix('#(docs) ');
328
+ const ext = base.set(['a'], 3).set(['a', 'b'], null);
329
+ expect(ext.toString()).toBe('#(docs) a = 3 { b }');
330
+ });
331
+ });