@malloydata/malloy 0.0.22-dev230126015148 → 0.0.22-dev230206094309

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 (93) hide show
  1. package/package.json +2 -2
  2. package/dist/connection_utils.d.ts +0 -1
  3. package/dist/connection_utils.js +0 -66
  4. package/dist/dialect/dialect.d.ts +0 -66
  5. package/dist/dialect/dialect.js +0 -85
  6. package/dist/dialect/dialect_map.d.ts +0 -3
  7. package/dist/dialect/dialect_map.js +0 -45
  8. package/dist/dialect/duckdb.d.ts +0 -52
  9. package/dist/dialect/duckdb.js +0 -364
  10. package/dist/dialect/index.d.ts +0 -6
  11. package/dist/dialect/index.js +0 -37
  12. package/dist/dialect/postgres.d.ts +0 -52
  13. package/dist/dialect/postgres.js +0 -330
  14. package/dist/dialect/standardsql.d.ts +0 -49
  15. package/dist/dialect/standardsql.js +0 -381
  16. package/dist/index.d.ts +0 -10
  17. package/dist/index.js +0 -59
  18. package/dist/lang/ast/apply-expr.d.ts +0 -3
  19. package/dist/lang/ast/apply-expr.js +0 -34
  20. package/dist/lang/ast/ast-expr.d.ts +0 -206
  21. package/dist/lang/ast/ast-expr.js +0 -781
  22. package/dist/lang/ast/ast-main.d.ts +0 -915
  23. package/dist/lang/ast/ast-main.js +0 -3672
  24. package/dist/lang/ast/ast-time-expr.d.ts +0 -69
  25. package/dist/lang/ast/ast-time-expr.js +0 -389
  26. package/dist/lang/ast/ast-types.d.ts +0 -83
  27. package/dist/lang/ast/ast-types.js +0 -225
  28. package/dist/lang/ast/index.d.ts +0 -5
  29. package/dist/lang/ast/index.js +0 -44
  30. package/dist/lang/ast/time-utils.d.ts +0 -9
  31. package/dist/lang/ast/time-utils.js +0 -104
  32. package/dist/lang/field-utils.d.ts +0 -5
  33. package/dist/lang/field-utils.js +0 -43
  34. package/dist/lang/index.d.ts +0 -9
  35. package/dist/lang/index.js +0 -32
  36. package/dist/lang/lib/Malloy/MalloyLexer.d.ts +0 -165
  37. package/dist/lang/lib/Malloy/MalloyLexer.js +0 -1232
  38. package/dist/lang/lib/Malloy/MalloyParser.d.ts +0 -1947
  39. package/dist/lang/lib/Malloy/MalloyParser.js +0 -11909
  40. package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +0 -2022
  41. package/dist/lang/lib/Malloy/MalloyParserListener.js +0 -4
  42. package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +0 -1274
  43. package/dist/lang/lib/Malloy/MalloyParserVisitor.js +0 -4
  44. package/dist/lang/parse-log.d.ts +0 -27
  45. package/dist/lang/parse-log.js +0 -51
  46. package/dist/lang/parse-malloy.d.ts +0 -236
  47. package/dist/lang/parse-malloy.js +0 -746
  48. package/dist/lang/parse-to-ast.d.ts +0 -155
  49. package/dist/lang/parse-to-ast.js +0 -990
  50. package/dist/lang/parse-tree-walkers/document-completion-walker.d.ts +0 -10
  51. package/dist/lang/parse-tree-walkers/document-completion-walker.js +0 -148
  52. package/dist/lang/parse-tree-walkers/document-help-context-walker.d.ts +0 -9
  53. package/dist/lang/parse-tree-walkers/document-help-context-walker.js +0 -107
  54. package/dist/lang/parse-tree-walkers/document-highlight-walker.d.ts +0 -83
  55. package/dist/lang/parse-tree-walkers/document-highlight-walker.js +0 -385
  56. package/dist/lang/parse-tree-walkers/document-symbol-walker.d.ts +0 -11
  57. package/dist/lang/parse-tree-walkers/document-symbol-walker.js +0 -182
  58. package/dist/lang/parse-tree-walkers/explore-query-walker.d.ts +0 -34
  59. package/dist/lang/parse-tree-walkers/explore-query-walker.js +0 -74
  60. package/dist/lang/parse-tree-walkers/find-external-references.d.ts +0 -11
  61. package/dist/lang/parse-tree-walkers/find-external-references.js +0 -61
  62. package/dist/lang/reference-list.d.ts +0 -10
  63. package/dist/lang/reference-list.js +0 -90
  64. package/dist/lang/space-field.d.ts +0 -17
  65. package/dist/lang/space-field.js +0 -127
  66. package/dist/lang/test/document-help-context-walker.spec.d.ts +0 -1
  67. package/dist/lang/test/document-help-context-walker.spec.js +0 -55
  68. package/dist/lang/test/document-symbol-walker.spec.d.ts +0 -1
  69. package/dist/lang/test/document-symbol-walker.spec.js +0 -110
  70. package/dist/lang/test/field-symbols.spec.d.ts +0 -1
  71. package/dist/lang/test/field-symbols.spec.js +0 -180
  72. package/dist/lang/test/parse.spec.d.ts +0 -14
  73. package/dist/lang/test/parse.spec.js +0 -2134
  74. package/dist/lang/test/test-translator.d.ts +0 -38
  75. package/dist/lang/test/test-translator.js +0 -344
  76. package/dist/lang/zone.d.ts +0 -57
  77. package/dist/lang/zone.js +0 -107
  78. package/dist/malloy.d.ts +0 -1146
  79. package/dist/malloy.js +0 -2413
  80. package/dist/model/index.d.ts +0 -2
  81. package/dist/model/index.js +0 -45
  82. package/dist/model/malloy_query.d.ts +0 -351
  83. package/dist/model/malloy_query.js +0 -3174
  84. package/dist/model/malloy_types.d.ts +0 -551
  85. package/dist/model/malloy_types.js +0 -378
  86. package/dist/model/sql_block.d.ts +0 -7
  87. package/dist/model/sql_block.js +0 -56
  88. package/dist/model/utils.d.ts +0 -22
  89. package/dist/model/utils.js +0 -102
  90. package/dist/run_sql_options.d.ts +0 -3
  91. package/dist/run_sql_options.js +0 -25
  92. package/dist/runtime_types.d.ts +0 -105
  93. package/dist/runtime_types.js +0 -25
@@ -1,746 +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
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- var desc = Object.getOwnPropertyDescriptor(m, k);
27
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
28
- desc = { enumerable: true, get: function() { return m[k]; } };
29
- }
30
- Object.defineProperty(o, k2, desc);
31
- }) : (function(o, m, k, k2) {
32
- if (k2 === undefined) k2 = k;
33
- o[k2] = m[k];
34
- }));
35
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
36
- Object.defineProperty(o, "default", { enumerable: true, value: v });
37
- }) : function(o, v) {
38
- o["default"] = v;
39
- });
40
- var __importStar = (this && this.__importStar) || function (mod) {
41
- if (mod && mod.__esModule) return mod;
42
- var result = {};
43
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
44
- __setModuleDefault(result, mod);
45
- return result;
46
- };
47
- Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.MalloyTranslator = exports.MalloyTranslation = void 0;
49
- const antlr4ts_1 = require("antlr4ts");
50
- const MalloyLexer_1 = require("./lib/Malloy/MalloyLexer");
51
- const MalloyParser_1 = require("./lib/Malloy/MalloyParser");
52
- const ast = __importStar(require("./ast"));
53
- const parse_to_ast_1 = require("./parse-to-ast");
54
- const parse_log_1 = require("./parse-log");
55
- const find_external_references_1 = require("./parse-tree-walkers/find-external-references");
56
- const zone_1 = require("./zone");
57
- const document_symbol_walker_1 = require("./parse-tree-walkers/document-symbol-walker");
58
- const document_highlight_walker_1 = require("./parse-tree-walkers/document-highlight-walker");
59
- const document_completion_walker_1 = require("./parse-tree-walkers/document-completion-walker");
60
- const document_help_context_walker_1 = require("./parse-tree-walkers/document-help-context-walker");
61
- const reference_list_1 = require("./reference-list");
62
- /**
63
- * This ignores a -> popMode when the mode stack is empty, which is a hack,
64
- * but it let's us parse }%
65
- */
66
- class HandlesOverpoppingLexer extends MalloyLexer_1.MalloyLexer {
67
- popMode() {
68
- if (this._modeStack.isEmpty) {
69
- return this._mode;
70
- }
71
- return super.popMode();
72
- }
73
- }
74
- class MalloyParserErrorHandler {
75
- constructor(translator, messages) {
76
- this.translator = translator;
77
- this.messages = messages;
78
- }
79
- syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, _e) {
80
- const errAt = { line: line - 1, character: charPositionInLine };
81
- const range = offendingSymbol
82
- ? this.translator.rangeFromToken(offendingSymbol)
83
- : { start: errAt, end: errAt };
84
- const error = {
85
- message: msg,
86
- at: { url: this.translator.sourceURL, range },
87
- };
88
- this.messages.log(error);
89
- }
90
- }
91
- function isNeedResponse(dr) {
92
- return !!dr && (dr.tables || dr.urls || dr.compileSQL) != undefined;
93
- }
94
- class ParseStep {
95
- step(that) {
96
- if (this.response) {
97
- return this.response;
98
- }
99
- if (that.urlIsFullPath === undefined) {
100
- try {
101
- const _checkFull = new URL(that.sourceURL);
102
- that.urlIsFullPath = true;
103
- }
104
- catch (e) {
105
- const msg = e instanceof Error ? e.message : "";
106
- that.urlIsFullPath = false;
107
- that.root.logger.log({
108
- message: `Could not compute full path URL: ${msg}`,
109
- });
110
- }
111
- }
112
- if (!that.urlIsFullPath) {
113
- return that.fatalErrors();
114
- }
115
- const srcEnt = that.root.importZone.getEntry(that.sourceURL);
116
- if (srcEnt.status !== "present") {
117
- if (srcEnt.status === "error") {
118
- const message = srcEnt.message.includes(that.sourceURL)
119
- ? `import error: ${srcEnt.message}`
120
- : `import '${that.sourceURL}' error: ${srcEnt.message}`;
121
- const at = srcEnt.firstReference || that.defaultLocation();
122
- that.root.logger.log({ message, at });
123
- this.response = that.fatalErrors();
124
- return this.response;
125
- }
126
- return { urls: [that.sourceURL] };
127
- }
128
- const source = srcEnt.value == "" ? "\n" : srcEnt.value;
129
- this.sourceInfo = this.getSourceInfo(source);
130
- let parse;
131
- try {
132
- parse = this.runParser(source, that);
133
- }
134
- catch (parseException) {
135
- that.root.logger.log({
136
- message: `Malloy internal parser exception [${parseException.message}]`,
137
- });
138
- parse = undefined;
139
- }
140
- if (that.root.logger.hasErrors()) {
141
- this.response = {
142
- parse,
143
- ...that.fatalErrors(),
144
- };
145
- }
146
- else {
147
- this.response = { parse };
148
- }
149
- return this.response;
150
- }
151
- /**
152
- * Split the source up into lines so we can correctly compute offset
153
- * to the line/char positions favored by LSP and VSCode.
154
- */
155
- getSourceInfo(code) {
156
- const eolRegex = /\r?\n/;
157
- const info = {
158
- at: [],
159
- lines: [],
160
- length: code.length,
161
- };
162
- let src = code;
163
- let lineStart = 0;
164
- while (src !== "") {
165
- const eol = src.match(eolRegex);
166
- if (eol && eol.index != undefined) {
167
- // line text DOES NOT include the EOL
168
- info.lines.push(src.slice(0, eol.index));
169
- const lineLength = eol.index + eol[0].length;
170
- info.at.push({
171
- begin: lineStart,
172
- end: lineStart + lineLength,
173
- });
174
- lineStart += lineLength;
175
- src = src.slice(lineLength);
176
- }
177
- else {
178
- // last line, does not have a line end
179
- info.lines.push(src);
180
- info.at.push({ begin: lineStart, end: lineStart + src.length });
181
- break;
182
- }
183
- }
184
- return info;
185
- }
186
- runParser(source, that) {
187
- const inputStream = antlr4ts_1.CharStreams.fromString(source);
188
- const lexer = new HandlesOverpoppingLexer(inputStream);
189
- const tokenStream = new antlr4ts_1.CommonTokenStream(lexer);
190
- const malloyParser = new MalloyParser_1.MalloyParser(tokenStream);
191
- malloyParser.removeErrorListeners();
192
- malloyParser.addErrorListener(new MalloyParserErrorHandler(that, that.root.logger));
193
- // Admitted code smell here, testing likes to parse from an arbitrary
194
- // node and this is the simplest way to allow that.
195
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
196
- const parseFunc = malloyParser[that.grammarRule];
197
- if (!parseFunc) {
198
- throw new Error(`No such parse rule as ${that.grammarRule}`);
199
- }
200
- return {
201
- root: parseFunc.call(malloyParser),
202
- tokenStream: tokenStream,
203
- sourceStream: inputStream,
204
- // TODO put the real version here
205
- malloyVersion: "?.?.?-????",
206
- subTranslator: that,
207
- };
208
- }
209
- }
210
- class ImportsAndTablesStep {
211
- constructor(parseStep) {
212
- this.parseStep = parseStep;
213
- this.alreadyLooked = false;
214
- }
215
- step(that) {
216
- const parseReq = this.parseStep.step(that);
217
- if (parseReq.parse == undefined) {
218
- return parseReq;
219
- }
220
- if (!this.alreadyLooked) {
221
- this.alreadyLooked = true;
222
- const parseRefs = (0, find_external_references_1.findReferences)(that, parseReq.parse.tokenStream, parseReq.parse.root);
223
- if (parseRefs === null || parseRefs === void 0 ? void 0 : parseRefs.tables) {
224
- for (const ref in parseRefs.tables) {
225
- that.root.schemaZone.reference(ref, {
226
- url: that.sourceURL,
227
- range: parseRefs.tables[ref],
228
- });
229
- }
230
- }
231
- if (parseRefs === null || parseRefs === void 0 ? void 0 : parseRefs.urls) {
232
- for (const relativeRef in parseRefs.urls) {
233
- const firstRef = parseRefs.urls[relativeRef];
234
- try {
235
- const ref = decodeURI(new URL(relativeRef, that.sourceURL).toString());
236
- that.addChild(ref);
237
- that.root.importZone.reference(ref, {
238
- url: that.sourceURL,
239
- range: firstRef,
240
- });
241
- }
242
- catch (err) {
243
- // This import spec is so bad the URL library threw up, this
244
- // may be impossible, because it will append any garbage
245
- // to the known good rootURL assuming it is relative
246
- that.root.logger.log({
247
- message: `Malformed URL '${relativeRef}'"`,
248
- at: { url: that.sourceURL, range: firstRef },
249
- });
250
- }
251
- }
252
- }
253
- }
254
- if (that.root.logger.hasErrors()) {
255
- // Since we knew we parsed without errors, this would only be from
256
- // having a malformed URL on an import reference.
257
- return null;
258
- }
259
- let allMissing = {};
260
- const missingTables = that.root.schemaZone.getUndefined();
261
- if (missingTables) {
262
- allMissing = { tables: missingTables };
263
- }
264
- const missingImports = that.root.importZone.getUndefined();
265
- if (missingImports) {
266
- allMissing = { ...allMissing, urls: missingImports };
267
- }
268
- if (isNeedResponse(allMissing)) {
269
- return allMissing;
270
- }
271
- for (const child of that.childTranslators.values()) {
272
- const kidNeeds = child.importsAndTablesStep.step(child);
273
- if (isNeedResponse(kidNeeds)) {
274
- return kidNeeds;
275
- }
276
- }
277
- return null;
278
- }
279
- }
280
- class ASTStep {
281
- constructor(importStep) {
282
- this.importStep = importStep;
283
- this.walked = false;
284
- }
285
- step(that) {
286
- if (this.response) {
287
- return this.response;
288
- }
289
- const mustResolve = this.importStep.step(that);
290
- if (mustResolve) {
291
- return mustResolve;
292
- }
293
- const parseResponse = that.parseStep.response;
294
- // Errors in self or children will show up here ..
295
- if (that.root.logger.hasErrors()) {
296
- this.response = that.fatalErrors();
297
- return this.response;
298
- }
299
- const parse = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.parse;
300
- if (!parse) {
301
- throw new Error("TRANSLATOR INTERNAL ERROR: Translator parse response had no errors, but also no parser");
302
- }
303
- const secondPass = new parse_to_ast_1.MalloyToAST(parse, that.root.logger);
304
- const newAst = secondPass.visit(parse.root);
305
- if (that.root.logger.hasErrors()) {
306
- this.response = that.fatalErrors();
307
- return this.response;
308
- }
309
- if (newAst.elementType === "unimplemented") {
310
- throw new Error("TRANSLATOR INTERNAL ERROR: Unimplemented AST node");
311
- }
312
- // I kind of think table refs should probably also be collected here
313
- // instead of in the parse step. Note to myself, do that someday.
314
- const sqlExplores = {};
315
- if (!this.walked) {
316
- newAst.walk((walkedTo) => {
317
- if (walkedTo instanceof ast.SQLSource) {
318
- if (!sqlExplores[walkedTo.refName]) {
319
- sqlExplores[walkedTo.refName] = {};
320
- }
321
- sqlExplores[walkedTo.refName].ref = walkedTo;
322
- }
323
- else if (walkedTo instanceof ast.SQLStatement && walkedTo.is) {
324
- if (!sqlExplores[walkedTo.is]) {
325
- sqlExplores[walkedTo.is] = {};
326
- }
327
- sqlExplores[walkedTo.is].def = walkedTo;
328
- }
329
- else if (walkedTo instanceof ast.Unimplemented) {
330
- walkedTo.log("INTERNAL COMPILER ERROR: Untranslated parse node");
331
- }
332
- });
333
- this.walked = true;
334
- }
335
- // If there is a partial ast ...
336
- if (that.root.logger.hasErrors()) {
337
- this.response = that.fatalErrors();
338
- return this.response;
339
- }
340
- /*
341
- TODO we used to scna the AST for SQL blocks here, don't need to do that
342
- becausae that happens at the translate step, but the code I need to
343
- understand is how a root translate can return the need of a chiold
344
- which has an SQL block ...
345
-
346
- */
347
- // Now make sure that every child has fully translated itself
348
- // before this tree is ready to also translate ...
349
- for (const child of that.childTranslators.values()) {
350
- const kidNeeds = child.astStep.step(child);
351
- if (isNeedResponse(kidNeeds)) {
352
- return kidNeeds;
353
- }
354
- }
355
- newAst.setTranslator(that);
356
- this.response = {
357
- ...that.errors(),
358
- ast: newAst,
359
- final: true,
360
- };
361
- return this.response;
362
- }
363
- }
364
- class MetadataStep {
365
- constructor(parseStep) {
366
- this.parseStep = parseStep;
367
- }
368
- step(that) {
369
- if (!this.response) {
370
- const tryParse = this.parseStep.step(that);
371
- if (!tryParse.parse) {
372
- return tryParse;
373
- }
374
- else {
375
- // Wrap the parse tree walker in a try block -- if the parse is bad, this walk
376
- // could result in unexpected errors due to the parse tree not looking as expected.
377
- // We still want to attempt to walk the tree, to preserve document symbols even
378
- // when there's a bad parse, but we also want to be safe about it.
379
- let symbols;
380
- try {
381
- symbols = (0, document_symbol_walker_1.walkForDocumentSymbols)(that, tryParse.parse.tokenStream, tryParse.parse.root);
382
- }
383
- catch {
384
- // Do nothing, symbols already `undefined`
385
- }
386
- let walkHighlights;
387
- try {
388
- walkHighlights = (0, document_highlight_walker_1.walkForDocumentHighlights)(tryParse.parse.tokenStream, tryParse.parse.root);
389
- }
390
- catch {
391
- walkHighlights = [];
392
- }
393
- this.response = {
394
- symbols,
395
- highlights: (0, document_highlight_walker_1.sortHighlights)([
396
- ...(0, document_highlight_walker_1.passForHighlights)(tryParse.parse.tokenStream),
397
- ...walkHighlights,
398
- ]),
399
- final: true,
400
- };
401
- }
402
- }
403
- return this.response;
404
- }
405
- }
406
- class CompletionsStep {
407
- constructor(parseStep) {
408
- this.parseStep = parseStep;
409
- }
410
- step(that, position) {
411
- const tryParse = this.parseStep.step(that);
412
- if (!tryParse.parse) {
413
- return tryParse;
414
- }
415
- else {
416
- let completions = [];
417
- if (position !== undefined) {
418
- try {
419
- completions = (0, document_completion_walker_1.walkForDocumentCompletions)(tryParse.parse.tokenStream, tryParse.parse.root, position);
420
- }
421
- catch {
422
- /* Do nothing */
423
- }
424
- }
425
- return {
426
- ...tryParse,
427
- completions,
428
- };
429
- }
430
- }
431
- }
432
- class HelpContextStep {
433
- constructor(parseStep) {
434
- this.parseStep = parseStep;
435
- }
436
- step(that, position) {
437
- const tryParse = this.parseStep.step(that);
438
- if (!tryParse.parse) {
439
- return tryParse;
440
- }
441
- else {
442
- let helpContext;
443
- if (position !== undefined) {
444
- try {
445
- helpContext = (0, document_help_context_walker_1.walkForDocumentHelpContext)(tryParse.parse.root, position);
446
- }
447
- catch {
448
- /* Do nothing */
449
- }
450
- }
451
- return {
452
- ...tryParse,
453
- helpContext,
454
- };
455
- }
456
- }
457
- }
458
- class TranslateStep {
459
- constructor(astStep) {
460
- this.astStep = astStep;
461
- }
462
- step(that, extendingModel) {
463
- if (this.response) {
464
- return this.response;
465
- }
466
- const astResponse = this.astStep.step(that);
467
- if (isNeedResponse(astResponse)) {
468
- return astResponse;
469
- }
470
- if (!astResponse.ast) {
471
- this.response = astResponse;
472
- return this.response;
473
- }
474
- if (that.grammarRule === "malloyDocument") {
475
- if (astResponse.ast instanceof ast.Document) {
476
- const doc = astResponse.ast;
477
- doc.initModelDef(extendingModel);
478
- for (;;) {
479
- const docCompile = doc.compile();
480
- if (docCompile.needs) {
481
- return docCompile.needs;
482
- }
483
- else {
484
- that.modelDef = docCompile.modelDef;
485
- that.queryList = docCompile.queryList;
486
- that.sqlBlocks = docCompile.sqlBlocks;
487
- break;
488
- }
489
- }
490
- }
491
- else {
492
- that.root.logger.log({
493
- message: `'${that.sourceURL}' did not parse to malloy document`,
494
- at: that.defaultLocation(),
495
- });
496
- }
497
- }
498
- if (that.root.logger.hasErrors()) {
499
- this.response = that.fatalErrors();
500
- }
501
- else {
502
- this.response = {
503
- translated: {
504
- modelDef: that.modelDef,
505
- queryList: that.queryList,
506
- sqlBlocks: that.sqlBlocks,
507
- },
508
- ...that.errors(),
509
- final: true,
510
- };
511
- }
512
- return this.response;
513
- }
514
- }
515
- class MalloyTranslation {
516
- constructor(sourceURL, grammarRule = "malloyDocument") {
517
- this.sourceURL = sourceURL;
518
- this.grammarRule = grammarRule;
519
- this.queryList = [];
520
- this.sqlBlocks = [];
521
- this.childTranslators = new Map();
522
- this.modelDef = {
523
- name: sourceURL,
524
- exports: [],
525
- contents: {},
526
- };
527
- /**
528
- * This is sort of the makefile for the translation, all the steps
529
- * and the dependencies of the steps are declared here. Then when
530
- * a translator method is called which needs the result of a step,
531
- * it asks the step what it needs to complete, and all the right
532
- * things will happen automatically.
533
- */
534
- this.parseStep = new ParseStep();
535
- this.metadataStep = new MetadataStep(this.parseStep);
536
- this.completionsStep = new CompletionsStep(this.parseStep);
537
- this.helpContextStep = new HelpContextStep(this.parseStep);
538
- this.importsAndTablesStep = new ImportsAndTablesStep(this.parseStep);
539
- this.astStep = new ASTStep(this.importsAndTablesStep);
540
- this.translateStep = new TranslateStep(this.astStep);
541
- this.references = new reference_list_1.ReferenceList(sourceURL);
542
- }
543
- addChild(url) {
544
- if (!this.childTranslators.get(url)) {
545
- this.childTranslators.set(url, new MalloyChildTranslator(url, this.root));
546
- }
547
- }
548
- addReference(reference) {
549
- this.references.add(reference);
550
- }
551
- referenceAt(position) {
552
- return this.references.find(position);
553
- }
554
- fatalErrors() {
555
- return {
556
- final: true,
557
- errors: [...this.root.logger.getLog()],
558
- };
559
- }
560
- /**
561
- * The error log can grow as progressively deeper questions are asked.
562
- * When returning "errors so far", make a snapshot.
563
- */
564
- errors() {
565
- const errors = this.root.logger.getLog();
566
- return { errors: [...errors] };
567
- }
568
- getLineMap(url) {
569
- var _a, _b;
570
- if (url == this.sourceURL) {
571
- return (_a = this.parseStep.sourceInfo) === null || _a === void 0 ? void 0 : _a.lines;
572
- }
573
- const theChild = this.childTranslators.get(url);
574
- if (theChild) {
575
- return (_b = theChild.parseStep.sourceInfo) === null || _b === void 0 ? void 0 : _b.lines;
576
- }
577
- }
578
- prettyErrors() {
579
- let lovely = "";
580
- let inFile = "";
581
- for (const entry of this.root.logger.getLog()) {
582
- let cooked = entry.message;
583
- let errorURL = this.sourceURL;
584
- if (entry.at) {
585
- errorURL = entry.at.url;
586
- const lineNo = entry.at.range.start.line;
587
- const charFrom = entry.at.range.start.character;
588
- const lines = this.getLineMap(entry.at.url);
589
- if (lines) {
590
- const errorLine = lines[lineNo];
591
- cooked = `line ${lineNo + 1}: ${entry.message}\n | ${errorLine}`;
592
- if (charFrom > 0) {
593
- cooked = cooked + `\n | ${" ".repeat(charFrom)}^`;
594
- }
595
- }
596
- else {
597
- cooked = `line ${lineNo + 1}: char ${charFrom}: ${entry.message}`;
598
- }
599
- }
600
- if (inFile !== errorURL) {
601
- cooked = `FILE: ${errorURL}\n` + cooked;
602
- inFile = errorURL;
603
- }
604
- if (lovely !== "") {
605
- lovely = `${lovely}\n${cooked}`;
606
- }
607
- else {
608
- lovely = cooked;
609
- }
610
- }
611
- return lovely;
612
- }
613
- childRequest(importURL) {
614
- var _a;
615
- const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
616
- const ret = (_a = this.childTranslators.get(childURL)) === null || _a === void 0 ? void 0 : _a.translate();
617
- if (ret === null || ret === void 0 ? void 0 : ret.compileSQL) {
618
- return {
619
- compileSQL: ret.compileSQL,
620
- partialModel: ret.partialModel,
621
- };
622
- }
623
- }
624
- getChildExports(importURL) {
625
- const exports = {};
626
- const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
627
- const child = this.childTranslators.get(childURL);
628
- if (child) {
629
- const did = child.translate();
630
- if (did.translated) {
631
- for (const fromChild of child.modelDef.exports) {
632
- const modelEntry = child.modelDef.contents[fromChild];
633
- if (modelEntry.type === "struct") {
634
- exports[fromChild] = modelEntry;
635
- }
636
- }
637
- }
638
- // else nothing, assuming there are already errors in the log
639
- }
640
- return exports;
641
- }
642
- translate(extendingModel) {
643
- if (this.finalAnswer) {
644
- return this.finalAnswer;
645
- }
646
- const attempt = this.translateStep.step(this, extendingModel);
647
- if (attempt.final) {
648
- this.finalAnswer = attempt;
649
- }
650
- return attempt;
651
- }
652
- metadata() {
653
- return this.metadataStep.step(this);
654
- }
655
- completions(position) {
656
- return this.completionsStep.step(this, position);
657
- }
658
- helpContext(position) {
659
- return this.helpContextStep.step(this, position);
660
- }
661
- defaultLocation() {
662
- return {
663
- url: this.sourceURL,
664
- range: {
665
- start: { line: 0, character: 0 },
666
- end: { line: 0, character: 0 },
667
- },
668
- };
669
- }
670
- rangeFromContext(pcx) {
671
- return this.rangeFromTokens(pcx.start, pcx.stop || pcx.start);
672
- }
673
- rangeFromTokens(startToken, stopToken) {
674
- const start = {
675
- line: startToken.line - 1,
676
- character: startToken.charPositionInLine,
677
- };
678
- if (this.parseStep.sourceInfo && stopToken.stopIndex != -1) {
679
- // Find the line which contains the stopIndex
680
- const lastLine = this.parseStep.sourceInfo.lines.length - 1;
681
- for (let lineNo = startToken.line - 1; lineNo <= lastLine; lineNo++) {
682
- const at = this.parseStep.sourceInfo.at[lineNo];
683
- if (stopToken.stopIndex >= at.begin && stopToken.stopIndex <= at.end) {
684
- return {
685
- start,
686
- end: {
687
- line: lineNo,
688
- character: stopToken.stopIndex - at.begin + 1,
689
- },
690
- };
691
- }
692
- }
693
- // Should be impossible to get here, but if we do ... return the last
694
- // character of the last line of the file
695
- return {
696
- start,
697
- end: {
698
- line: lastLine,
699
- character: this.parseStep.sourceInfo.lines[lastLine].length,
700
- },
701
- };
702
- }
703
- return { start, end: start };
704
- }
705
- rangeFromToken(token) {
706
- return this.rangeFromTokens(token, token);
707
- }
708
- }
709
- exports.MalloyTranslation = MalloyTranslation;
710
- class MalloyChildTranslator extends MalloyTranslation {
711
- constructor(rootURL, root) {
712
- super(rootURL);
713
- this.root = root;
714
- }
715
- }
716
- /**
717
- * The main interface to Malloy tranlsation. It has a call pattern
718
- * similar to a server. Once a translator is instantiated
719
- * you can request tralnsations repeatedly. Responses to that request
720
- * will either be "NeedResponse" or "TranslateResponse" objects. The
721
- * correct pattern is to call "translation" in a loop, calling
722
- * "update" in response to each "NeedResponse" until a "TranslateResponse"
723
- * is returned. If you get a response with "final:true", there is
724
- * no need to call again, the translation is finished or error'd.
725
- */
726
- class MalloyTranslator extends MalloyTranslation {
727
- constructor(rootURL, preload = null) {
728
- super(rootURL);
729
- this.schemaZone = new zone_1.Zone();
730
- this.importZone = new zone_1.Zone();
731
- this.sqlQueryZone = new zone_1.Zone();
732
- this.logger = new parse_log_1.MessageLog();
733
- this.root = this;
734
- if (preload) {
735
- this.update(preload);
736
- }
737
- }
738
- update(dd) {
739
- var _a, _b, _c;
740
- this.schemaZone.updateFrom(dd.tables, (_a = dd.errors) === null || _a === void 0 ? void 0 : _a.tables);
741
- this.importZone.updateFrom(dd.urls, (_b = dd.errors) === null || _b === void 0 ? void 0 : _b.urls);
742
- this.sqlQueryZone.updateFrom(dd.compileSQL, (_c = dd.errors) === null || _c === void 0 ? void 0 : _c.compileSQL);
743
- }
744
- }
745
- exports.MalloyTranslator = MalloyTranslator;
746
- //# sourceMappingURL=parse-malloy.js.map