@malloydata/malloy 0.0.11-dev221202152346 → 0.0.11

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.
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ // Generated from Malloy.g4 by ANTLR 4.9.0-SNAPSHOT
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=MalloyVisitor.js.map
@@ -1,6 +1,6 @@
1
1
  import { Token, CommonTokenStream, ParserRuleContext, CodePointCharStream } from "antlr4ts";
2
2
  import type { ParseTree } from "antlr4ts/tree";
3
- import { Query, NamedStructDefs, StructDef, ModelDef, SQLBlock, DocumentReference, DocumentPosition, DocumentLocation, DocumentRange } from "../model/malloy_types";
3
+ import { Query, NamedStructDefs, StructDef, ModelDef, SQLBlockSource, DocumentReference, DocumentPosition, DocumentLocation, DocumentRange, SQLBlockStructDef } from "../model/malloy_types";
4
4
  import * as ast from "./ast";
5
5
  import { LogMessage, MessageLog } from "./parse-log";
6
6
  import { Zone, ZoneData } from "./zone";
@@ -26,24 +26,14 @@ export interface NeedSchemaData {
26
26
  export interface NeedURLData {
27
27
  urls: string[];
28
28
  }
29
- /**
30
- * An SQL Block contains a unique key inside it, and we use that to
31
- * reference and define blocks, since Zone really wants keys to be strings.
32
- *
33
- * If I had SQLBlocks when Zones were defined, this would not be
34
- * a one off class, it would be Zone<keyType,valueType>
35
- */
36
- export declare class SQLExploreZone extends Zone<StructDef> {
37
- keyed: Record<string, SQLBlock>;
38
- referenceBlock(from: ast.SQLStatement, at: DocumentLocation): void;
39
- getUndefinedBlocks(): SQLBlock[] | undefined;
40
- }
41
- export interface NeedSQLStruct {
42
- sqlStructs: SQLBlock[];
29
+ export interface NeedCompileSQL {
30
+ compileSQL: SQLBlockSource;
31
+ partialModel: ModelDef | undefined;
43
32
  }
44
- interface NeededData extends NeedURLData, NeedSchemaData, NeedSQLStruct {
33
+ interface NeededData extends NeedURLData, NeedSchemaData, NeedCompileSQL {
45
34
  }
46
35
  export declare type DataRequestResponse = Partial<NeededData> | null;
36
+ export declare type ModelDataRequest = NeedCompileSQL | undefined;
47
37
  interface ASTData extends ErrorResponse, NeededData, FinalResponse {
48
38
  ast: ast.MalloyElement;
49
39
  }
@@ -65,7 +55,7 @@ interface TranslatedResponseData extends NeededData, ErrorResponse, FinalRespons
65
55
  translated: {
66
56
  modelDef: ModelDef;
67
57
  queryList: Query[];
68
- sqlBlocks: SQLBlock[];
58
+ sqlBlocks: SQLBlockStructDef[];
69
59
  };
70
60
  }
71
61
  export declare type TranslateResponse = Partial<TranslatedResponseData>;
@@ -166,7 +156,7 @@ export declare abstract class MalloyTranslation {
166
156
  childTranslators: Map<string, MalloyTranslation>;
167
157
  urlIsFullPath?: boolean;
168
158
  queryList: Query[];
169
- sqlBlocks: SQLBlock[];
159
+ sqlBlocks: SQLBlockStructDef[];
170
160
  modelDef: ModelDef;
171
161
  readonly parseStep: ParseStep;
172
162
  readonly importsAndTablesStep: ImportsAndTablesStep;
@@ -188,6 +178,7 @@ export declare abstract class MalloyTranslation {
188
178
  errors(): ErrorResponse;
189
179
  getLineMap(url: string): string[] | undefined;
190
180
  prettyErrors(): string;
181
+ childRequest(importURL: string): ModelDataRequest;
191
182
  getChildExports(importURL: string): NamedStructDefs;
192
183
  private finalAnswer?;
193
184
  translate(extendingModel?: ModelDef): TranslateResponse;
@@ -218,7 +209,7 @@ export declare abstract class MalloyTranslation {
218
209
  export declare class MalloyTranslator extends MalloyTranslation {
219
210
  schemaZone: Zone<StructDef>;
220
211
  importZone: Zone<string>;
221
- sqlQueryZone: SQLExploreZone;
212
+ sqlQueryZone: Zone<SQLBlockStructDef>;
222
213
  logger: MessageLog;
223
214
  readonly root: MalloyTranslator;
224
215
  constructor(rootURL: string, preload?: ParseUpdate | null);
@@ -227,7 +218,7 @@ export declare class MalloyTranslator extends MalloyTranslation {
227
218
  interface ErrorData {
228
219
  tables: Record<string, string>;
229
220
  urls: Record<string, string>;
230
- sqlStructs: Record<string, string>;
221
+ compileSQL: Record<string, string>;
231
222
  }
232
223
  export interface URLData {
233
224
  urls: ZoneData<string>;
@@ -235,10 +226,10 @@ export interface URLData {
235
226
  export interface SchemaData {
236
227
  tables: ZoneData<StructDef>;
237
228
  }
238
- export interface SQLStructData {
239
- sqlStructs: ZoneData<StructDef>;
229
+ export interface SQLBlockData {
230
+ compileSQL: ZoneData<SQLBlockStructDef>;
240
231
  }
241
- export interface UpdateData extends URLData, SchemaData, SQLStructData {
232
+ export interface UpdateData extends URLData, SchemaData, SQLBlockData {
242
233
  errors: Partial<ErrorData>;
243
234
  }
244
235
  export declare type ParseUpdate = Partial<UpdateData>;
@@ -35,7 +35,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
35
35
  return result;
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.MalloyTranslator = exports.MalloyTranslation = exports.SQLExploreZone = void 0;
38
+ exports.MalloyTranslator = exports.MalloyTranslation = void 0;
39
39
  const antlr4ts_1 = require("antlr4ts");
40
40
  const MalloyLexer_1 = require("./lib/Malloy/MalloyLexer");
41
41
  const MalloyParser_1 = require("./lib/Malloy/MalloyParser");
@@ -66,34 +66,8 @@ class MalloyParserErrorHandler {
66
66
  this.messages.log(error);
67
67
  }
68
68
  }
69
- /**
70
- * An SQL Block contains a unique key inside it, and we use that to
71
- * reference and define blocks, since Zone really wants keys to be strings.
72
- *
73
- * If I had SQLBlocks when Zones were defined, this would not be
74
- * a one off class, it would be Zone<keyType,valueType>
75
- */
76
- class SQLExploreZone extends zone_1.Zone {
77
- constructor() {
78
- super(...arguments);
79
- this.keyed = {};
80
- }
81
- referenceBlock(from, at) {
82
- const sql = from.sqlBlock();
83
- this.reference(sql.name, at);
84
- this.keyed[sql.name] = sql;
85
- }
86
- getUndefinedBlocks() {
87
- const blockRefs = this.getUndefined();
88
- if (blockRefs) {
89
- return blockRefs.map((ref) => this.keyed[ref]);
90
- }
91
- return undefined;
92
- }
93
- }
94
- exports.SQLExploreZone = SQLExploreZone;
95
69
  function isNeedResponse(dr) {
96
- return !!dr && (dr.tables || dr.urls || dr.sqlStructs) != undefined;
70
+ return !!dr && (dr.tables || dr.urls || dr.compileSQL) != undefined;
97
71
  }
98
72
  class ParseStep {
99
73
  step(that) {
@@ -332,30 +306,21 @@ class ASTStep {
332
306
  this.response = that.fatalErrors();
333
307
  return this.response;
334
308
  }
335
- // Make sure there is a request/reference for all explored-sql entities
336
- const sqlZone = that.root.sqlQueryZone;
337
- for (const sqlExploreRef in sqlExplores) {
338
- const sqlExplore = sqlExplores[sqlExploreRef];
339
- if (sqlExplore.ref && sqlExplore.def) {
340
- const sqlAt = {
341
- url: that.sourceURL,
342
- range: sqlExplore.def.location.range,
343
- };
344
- that.root.sqlQueryZone.referenceBlock(sqlExplore.def, sqlAt);
345
- }
346
- }
347
- // Now make sure that every child also has all sql blocks resolved
309
+ /*
310
+ TODO we used to scna the AST for SQL blocks here, don't need to do that
311
+ becausae that happens at the translate step, but the code I need to
312
+ understand is how a root translate can return the need of a chiold
313
+ which has an SQL block ...
314
+
315
+ */
316
+ // Now make sure that every child has fully translated itself
317
+ // before this tree is ready to also translate ...
348
318
  for (const child of that.childTranslators.values()) {
349
319
  const kidNeeds = child.astStep.step(child);
350
320
  if (isNeedResponse(kidNeeds)) {
351
321
  return kidNeeds;
352
322
  }
353
323
  }
354
- // TODO report errors from here!
355
- const missingSqlStructs = sqlZone.getUndefinedBlocks();
356
- if (missingSqlStructs) {
357
- return { sqlStructs: missingSqlStructs };
358
- }
359
324
  newAst.setTranslator(that);
360
325
  this.response = {
361
326
  ...that.errors(),
@@ -478,9 +443,19 @@ class TranslateStep {
478
443
  if (that.grammarRule === "malloyDocument") {
479
444
  if (astResponse.ast instanceof ast.Document) {
480
445
  const doc = astResponse.ast;
481
- that.modelDef = doc.getModelDef(extendingModel);
482
- that.queryList = doc.queryList;
483
- that.sqlBlocks = doc.sqlBlocks;
446
+ doc.initModelDef(extendingModel);
447
+ for (;;) {
448
+ const docCompile = doc.compile();
449
+ if (docCompile.needs) {
450
+ return docCompile.needs;
451
+ }
452
+ else {
453
+ that.modelDef = docCompile.modelDef;
454
+ that.queryList = docCompile.queryList;
455
+ that.sqlBlocks = docCompile.sqlBlocks;
456
+ break;
457
+ }
458
+ }
484
459
  }
485
460
  else {
486
461
  that.root.logger.log({
@@ -604,6 +579,17 @@ class MalloyTranslation {
604
579
  }
605
580
  return lovely;
606
581
  }
582
+ childRequest(importURL) {
583
+ var _a;
584
+ const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
585
+ const ret = (_a = this.childTranslators.get(childURL)) === null || _a === void 0 ? void 0 : _a.translate();
586
+ if (ret === null || ret === void 0 ? void 0 : ret.compileSQL) {
587
+ return {
588
+ compileSQL: ret.compileSQL,
589
+ partialModel: ret.partialModel,
590
+ };
591
+ }
592
+ }
607
593
  getChildExports(importURL) {
608
594
  const exports = {};
609
595
  const childURL = decodeURI(new URL(importURL, this.sourceURL).toString());
@@ -711,7 +697,7 @@ class MalloyTranslator extends MalloyTranslation {
711
697
  super(rootURL);
712
698
  this.schemaZone = new zone_1.Zone();
713
699
  this.importZone = new zone_1.Zone();
714
- this.sqlQueryZone = new SQLExploreZone();
700
+ this.sqlQueryZone = new zone_1.Zone();
715
701
  this.logger = new parse_log_1.MessageLog();
716
702
  this.root = this;
717
703
  if (preload) {
@@ -722,7 +708,7 @@ class MalloyTranslator extends MalloyTranslation {
722
708
  var _a, _b, _c;
723
709
  this.schemaZone.updateFrom(dd.tables, (_a = dd.errors) === null || _a === void 0 ? void 0 : _a.tables);
724
710
  this.importZone.updateFrom(dd.urls, (_b = dd.errors) === null || _b === void 0 ? void 0 : _b.urls);
725
- this.sqlQueryZone.updateFrom(dd.sqlStructs, (_c = dd.errors) === null || _c === void 0 ? void 0 : _c.sqlStructs);
711
+ this.sqlQueryZone.updateFrom(dd.compileSQL, (_c = dd.errors) === null || _c === void 0 ? void 0 : _c.compileSQL);
726
712
  }
727
713
  }
728
714
  exports.MalloyTranslator = MalloyTranslator;
@@ -37,6 +37,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
37
37
  protected onlyNestedQueries(els: ast.MalloyElement[]): ast.NestedQuery[];
38
38
  protected onlyQueryRefs(els: ast.MalloyElement[]): ast.QueryItem[];
39
39
  protected getNumber(term: ParseTree): number;
40
+ protected optionalString(fromTerm: ParseTree | undefined): string | undefined;
40
41
  protected getIdText(fromTerm: ParseTree): string;
41
42
  protected getFieldName(cx: ParserRuleContext): ast.FieldName;
42
43
  protected getModelEntryName(cx: ParserRuleContext): ast.ModelEntryReference;
@@ -51,6 +52,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
51
52
  protected getSegments(segments: parse.PipeElementContext[]): ast.QOPDesc[];
52
53
  protected getFilterShortcut(cx: parse.FilterShortcutContext): ast.Filter;
53
54
  protected getExploreSource(pcx: parse.ExploreSourceContext): ast.Mallobj;
55
+ protected makeSqlString(pcx: parse.SqlStringContext, sqlStr: ast.SQLString): void;
54
56
  visitMalloyDocument(pcx: parse.MalloyDocumentContext): ast.Document;
55
57
  visitDefineExploreStatement(pcx: parse.DefineExploreStatementContext): ast.DefineSourceList | ast.DefineExplore;
56
58
  visitExploreDefinition(pcx: parse.ExploreDefinitionContext): ast.DefineExplore;
@@ -58,7 +60,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
58
60
  visitExploreProperties(pcx: parse.ExplorePropertiesContext): ast.ExploreDesc;
59
61
  visitExploreTable(pcx: parse.ExploreTableContext): ast.TableSource;
60
62
  visitTableSource(pcx: parse.TableSourceContext): ast.TableSource;
61
- visitSQLSource(pcx: parse.SQLSourceContext): ast.SQLSource;
63
+ visitSQLSourceName(pcx: parse.SQLSourceNameContext): ast.SQLSource;
62
64
  visitQuerySource(pcx: parse.QuerySourceContext): ast.Mallobj;
63
65
  visitDefJoinMany(pcx: parse.DefJoinManyContext): ast.Joins;
64
66
  visitDefJoinOne(pcx: parse.DefJoinOneContext): ast.Joins;
@@ -150,6 +152,5 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
150
152
  visitImportStatement(pcx: parse.ImportStatementContext): ast.ImportStatement;
151
153
  visitJustExpr(pcx: parse.JustExprContext): ast.ExpressionDef;
152
154
  visitDefineSQLStatement(pcx: parse.DefineSQLStatementContext): ast.SQLStatement;
153
- visitSQLStatementDef(pcx: parse.SqlStatementDefContext): ast.SQLStatement;
154
155
  visitSampleStatement(pcx: parse.SampleStatementContext): ast.SampleProperty;
155
156
  }
@@ -148,6 +148,12 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
148
148
  getNumber(term) {
149
149
  return Number.parseInt(term.text);
150
150
  }
151
+ optionalString(fromTerm) {
152
+ if (fromTerm) {
153
+ return this.stripQuotes(fromTerm.text);
154
+ }
155
+ return undefined;
156
+ }
151
157
  getIdText(fromTerm) {
152
158
  return this.stripQuotes(fromTerm.text);
153
159
  }
@@ -230,6 +236,19 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
230
236
  }
231
237
  throw this.internalError(pcx, `'${element.elementType}': illegal explore source`);
232
238
  }
239
+ makeSqlString(pcx, sqlStr) {
240
+ var _a;
241
+ for (const part of pcx.sqlInterpolation()) {
242
+ const upToOpen = part.OPEN_CODE().text;
243
+ if (upToOpen.length > 2) {
244
+ sqlStr.push(upToOpen.slice(0, upToOpen.length - 2));
245
+ }
246
+ sqlStr.push(this.visit(part.query()));
247
+ }
248
+ const lastChars = (_a = pcx.SQL_END()) === null || _a === void 0 ? void 0 : _a.text.slice(0, -3);
249
+ sqlStr.push(lastChars || "");
250
+ this.astAt(sqlStr, pcx);
251
+ }
233
252
  visitMalloyDocument(pcx) {
234
253
  const stmts = this.onlyDocStatements(pcx.malloyStatement().map((scx) => this.visit(scx)));
235
254
  return new ast.Document(stmts);
@@ -270,7 +289,7 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
270
289
  visitTableSource(pcx) {
271
290
  return this.visitExploreTable(pcx.exploreTable());
272
291
  }
273
- visitSQLSource(pcx) {
292
+ visitSQLSourceName(pcx) {
274
293
  const name = this.getModelEntryName(pcx.sqlExploreNameRef());
275
294
  return this.astAt(new ast.SQLSource(name), pcx);
276
295
  }
@@ -914,19 +933,32 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
914
933
  return this.getFieldExpr(pcx.fieldExpr());
915
934
  }
916
935
  visitDefineSQLStatement(pcx) {
917
- return this.visitSQLStatementDef(pcx.sqlStatementDef());
918
- }
919
- visitSQLStatementDef(pcx) {
920
- const commands = pcx.SQL_STRING().text;
921
- const sqlStmt = new ast.SQLStatement({
922
- select: commands.slice(2, commands.length - 2),
923
- connection: this.optionalText(pcx.connectionName()),
924
- });
925
- const nameCx = pcx.sqlCommandNameDef();
926
- if (nameCx) {
927
- sqlStmt.is = this.getIdText(nameCx);
936
+ var _a;
937
+ const blockName = (_a = pcx.nameSQLBlock()) === null || _a === void 0 ? void 0 : _a.text;
938
+ const blockParts = pcx.sqlBlock().blockSQLDef();
939
+ const sqlStr = new ast.SQLString();
940
+ let connectionName;
941
+ for (const blockEnt of blockParts) {
942
+ const nmCx = blockEnt.connectionName();
943
+ if (nmCx) {
944
+ if (connectionName) {
945
+ this.contextError(nmCx, "Cannot redefine connection");
946
+ }
947
+ else {
948
+ connectionName = this.getIdText(nmCx);
949
+ }
950
+ }
951
+ const selCx = blockEnt.sqlString();
952
+ if (selCx) {
953
+ this.makeSqlString(selCx, sqlStr);
954
+ }
928
955
  }
929
- return this.astAt(sqlStmt, pcx);
956
+ const stmt = new ast.SQLStatement(sqlStr);
957
+ if (connectionName !== undefined) {
958
+ stmt.connection = connectionName;
959
+ }
960
+ stmt.is = blockName;
961
+ return this.astAt(stmt, pcx);
930
962
  }
931
963
  visitSampleStatement(pcx) {
932
964
  const rowCx = pcx.sampleSpec().INTEGER_LITERAL();
@@ -150,9 +150,9 @@ class DocumentSymbolWalker {
150
150
  parent.children.push(symbol);
151
151
  }
152
152
  }
153
- enterSqlStatementDef(pcx) {
153
+ enterDefineSQLStatement(pcx) {
154
154
  var _a;
155
- const name = (_a = pcx.sqlCommandNameDef()) === null || _a === void 0 ? void 0 : _a.id().text;
155
+ const name = (_a = pcx.nameSQLBlock()) === null || _a === void 0 ? void 0 : _a.text;
156
156
  const symbol = {
157
157
  range: this.translator.rangeFromContext(pcx),
158
158
  name: name || "unnamed_sql",
@@ -1,12 +1,14 @@
1
+ import { DocumentLocation } from "../../model";
1
2
  declare global {
2
3
  namespace jest {
3
4
  interface Matchers<R> {
4
- toCompile(): R;
5
+ modelParsed(): R;
5
6
  toBeErrorless(): R;
6
- toTranslate(): R;
7
+ toCompile(): R;
8
+ modelCompiled(): R;
7
9
  toReturnType(tp: string): R;
8
10
  compileToFailWith(...expectedErrors: string[]): R;
11
+ isLocationIn(at: DocumentLocation, txt: string): R;
9
12
  }
10
13
  }
11
14
  }
12
- export {};