Skip to content

Commit c39162a

Browse files
committed
update/fix pythonParser & SqlParser
1 parent 662f609 commit c39162a

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

packages/cubejs-schema-compiler/src/parser/PythonParser.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Python3Parser, {
1818
Single_string_template_atomContext,
1919
ArglistContext,
2020
CallArgumentsContext,
21-
AnnassignContext,
2221
} from './Python3Parser';
2322
import { UserError } from '../compiler/UserError';
2423
import Python3ParserVisitor from './Python3ParserVisitor';
@@ -70,11 +69,8 @@ export class PythonParser {
7069
protected parse() {
7170
const { codeString } = this;
7271

73-
const chars = CharStreams.fromString(codeString);
74-
chars.getText = (interval) => {
75-
const start = interval.a;
76-
let stop = interval.b;
77-
72+
const chars = new CharStream(codeString);
73+
chars.getText = (start, stop) => {
7874
if (stop >= chars.size) {
7975
stop = chars.size - 1;
8076
}
@@ -131,7 +127,7 @@ export class PythonParser {
131127
if (children.length === 1) {
132128
return children[0];
133129
} else {
134-
throw new UserError(`Unsupported Python multiple children node: ${node.constructor.name}: ${node.text}`);
130+
throw new UserError(`Unsupported Python multiple children node: ${node.constructor.name}: ${node.getText()}`);
135131
}
136132
};
137133

@@ -141,7 +137,7 @@ export class PythonParser {
141137
if (children.length === 1) {
142138
return t.expressionStatement(children[0]);
143139
} else {
144-
throw new UserError(`Unsupported Python multiple children node: ${node.constructor.name}: ${node.text}`);
140+
throw new UserError(`Unsupported Python multiple children node: ${node.constructor.name}: ${node.getText()}`);
145141
}
146142
} else if (
147143
node instanceof Double_string_template_atomContext ||
@@ -150,7 +146,7 @@ export class PythonParser {
150146
if ((node.test() || node.star_expr()) && children.length === 1) {
151147
return children[0];
152148
}
153-
return t.templateElement({ raw: node.text, cooked: node.text });
149+
return t.templateElement({ raw: node.getText(), cooked: node.getText() });
154150
} else if (node instanceof String_templateContext) {
155151
if (children[children.length - 1].type === 'TemplateElement') {
156152
children[children.length - 1].tail = true;
@@ -177,15 +173,21 @@ export class PythonParser {
177173
}
178174
return expr;
179175
} else {
180-
throw new UserError(`Empty Python atom_expr node: ${node.constructor.name}: ${node.text}`);
176+
throw new UserError(`Empty Python atom_expr node: ${node.constructor.name}: ${node.getText()}`);
181177
}
182178
} else if (node instanceof AtomContext) {
183179
const name = node.NAME();
184-
const string = node.STRING();
180+
const stringList = node.STRING_list();
181+
const number = node.NUMBER();
182+
185183
if (name) {
186-
return t.identifier(name.text);
187-
} else if (string?.length) {
188-
return t.stringLiteral(string.map(s => this.stripQuotes(s.text)).join(''));
184+
return t.identifier(name.getText());
185+
} else if (stringList && stringList.length) {
186+
return t.stringLiteral(stringList.map(s => this.stripQuotes(s.getText())).join(''));
187+
} else if (number) {
188+
const numText = number.getText();
189+
const numValue = parseFloat(numText);
190+
return t.numericLiteral(numValue);
189191
} else {
190192
return singleNodeReturn();
191193
}
@@ -206,16 +208,16 @@ export class PythonParser {
206208
// which was already processed (see other if branch)
207209
return children[0];
208210
} else if (name) {
209-
return { identifier: t.identifier(name.text) };
211+
return { identifier: t.identifier(name.getText()) };
210212
} else {
211-
throw new UserError(`Unsupported Python Trailer children node: ${node.constructor.name}: ${node.text}`);
213+
throw new UserError(`Unsupported Python Trailer children node: ${node.constructor.name}: ${node.getText()}`);
212214
}
213215
} else if (node instanceof VfpdefContext) {
214216
const name = node.NAME();
215217
if (name) {
216-
return t.identifier(name.text);
218+
return t.identifier(name.getText());
217219
} else {
218-
throw new UserError(`Unsupported Python vfpdef children node: ${node.constructor.name}: ${node.text}`);
220+
throw new UserError(`Unsupported Python vfpdef children node: ${node.constructor.name}: ${node.getText()}`);
219221
}
220222
} else if (node instanceof VarargslistContext) {
221223
return { args: children };

packages/cubejs-schema-compiler/src/parser/SqlParser.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-underscore-dangle */
22
import R from 'ramda';
3-
import { ErrorListener, CommonTokenStream, CharStream, RuleNode } from 'antlr4';
3+
import { ErrorListener, CommonTokenStream, CharStream, RuleNode, ParseTree } from 'antlr4';
44

55
import GenericSqlLexer from './GenericSqlLexer';
66
import GenericSqlParser, {
@@ -92,11 +92,8 @@ export class SqlParser {
9292
protected parse() {
9393
const { sql } = this;
9494

95-
const chars = CharStreams.fromString(SqlParser.sqlUpperCase(sql));
96-
chars.getText = (interval) => {
97-
const start = interval.a;
98-
let stop = interval.b;
99-
95+
const chars = new CharStream(SqlParser.sqlUpperCase(sql));
96+
chars.getText = (start, stop) => {
10097
if (stop >= chars.size) {
10198
stop = chars.size - 1;
10299
}
@@ -166,8 +163,8 @@ export class SqlParser {
166163
this.ast.accept(nodeVisitor({
167164
visitNode(ctx) {
168165
if (ctx instanceof QueryContext) {
169-
const selectItems = ctx.tryGetRuleContext(0, SelectFieldsContext);
170-
if (selectItems && selectItems.text === '*') {
166+
const selectItems = ctx.getTypedRuleContext(SelectFieldsContext, 0);
167+
if (selectItems && selectItems.getText() === '*') {
171168
result = true;
172169
}
173170
}
@@ -191,38 +188,39 @@ export class SqlParser {
191188
const whereBuildingVisitor = nodeVisitor({
192189
visitNode(ctx) {
193190
if (ctx instanceof IdPathContext) {
194-
result += sql.substring(cursor, ctx.start.startIndex);
195-
cursor = ctx.start.startIndex;
196-
197-
const child = ctx.getChild(0);
198-
if (child && child.text === originalAlias) {
199-
const withoutFirst = R.drop(1, <ParseTree[]>ctx.children);
200-
result += [tableAlias].concat(withoutFirst.map(c => c.text)).join('');
201-
cursor = <number>ctx.stop?.stopIndex + 1;
202-
} else if (ctx.childCount === 1) {
203-
result += [tableAlias, '.'].concat(ctx.children?.map(c => c.text)).join('');
204-
cursor = <number>ctx.stop?.stopIndex + 1;
191+
result += sql.substring(cursor, ctx.start.start);
192+
cursor = ctx.start.start;
193+
194+
const { children } = ctx as any;
195+
const child = children ? children[0] : null;
196+
if (child && child.getText() === originalAlias) {
197+
const withoutFirst = R.drop(1, <ParseTree[]>ctx.children || []);
198+
result += [tableAlias].concat(withoutFirst.map((c: ParseTree) => c.getText())).join('');
199+
cursor = (ctx.stop?.stop || 0) + 1;
200+
} else if (children && children.length === 1) {
201+
result += [tableAlias, '.'].concat(children?.map((c: ParseTree) => c.getText())).join('');
202+
cursor = (ctx.stop?.stop || 0) + 1;
205203
} else {
206-
result += sql.substring(cursor, ctx.stop?.stopIndex);
207-
cursor = <number>ctx.stop?.stopIndex;
204+
result += sql.substring(cursor, ctx.stop?.stop);
205+
cursor = <number>ctx.stop?.stop;
208206
}
209207
}
210208
}
211209
});
212210

213211
this.ast.accept(nodeVisitor({
214212
visitNode(ctx) {
215-
if (ctx instanceof QueryContext && ctx._from && ctx._where) {
216-
const aliasField = ctx._from.getRuleContext(0, AliasFieldContext);
217-
const lastNode = aliasField.getChild(aliasField.childCount - 1);
213+
if (ctx instanceof QueryContext && ctx._from_ && ctx._where) {
214+
const aliasField = ctx._from_.getTypedRuleContext(AliasFieldContext, 0);
215+
const lastNode: any = (aliasField as any).children ? (aliasField as any).children[(aliasField as any).children.length - 1] : null;
218216
if (lastNode instanceof IdPathContext) {
219-
originalAlias = lastNode.getChild(lastNode.childCount - 1).text;
217+
originalAlias = lastNode.children ? lastNode.children[lastNode.children.length - 1].getText() : '';
220218
} else {
221-
originalAlias = lastNode.text;
219+
originalAlias = lastNode ? lastNode.getText() : '';
222220
}
223221

224-
cursor = ctx._where.start.startIndex;
225-
end = <number>ctx._where.stop?.stopIndex + 1;
222+
cursor = ctx._where.start.start;
223+
end = <number>(ctx._where.stop?.stop || 0) + 1;
226224
ctx._where.accept(whereBuildingVisitor);
227225
}
228226
}
@@ -239,9 +237,9 @@ export class SqlParser {
239237

240238
this.ast.accept(nodeVisitor({
241239
visitNode(ctx) {
242-
if (ctx instanceof QueryContext && ctx._from) {
243-
const aliasField = ctx._from.getRuleContext(0, AliasFieldContext);
244-
result = aliasField.getChild(0).text;
240+
if (ctx instanceof QueryContext && ctx._from_) {
241+
const aliasField = ctx._from_.getTypedRuleContext(AliasFieldContext, 0);
242+
result = (aliasField as any).children ? (aliasField as any).children[0].getText() : null;
245243
}
246244
}
247245
}));

0 commit comments

Comments
 (0)