blob: 151ee4eb2931c45cb397328b05fac3ee90759704 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
Taewoo Kima12d8cd2015-03-04 13:47:08 -08003
vinayakb38b7ca42012-03-05 05:44:15 +00004 STATIC = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08005
vinayakb38b7ca42012-03-05 05:44:15 +00006}
7
8
9PARSER_BEGIN(AQLPlusParser)
10
Ian Maxonf18bba22015-08-21 12:35:14 -070011package org.apache.asterix.aqlplus.parser;
vinayakb38b7ca42012-03-05 05:44:15 +000012
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17import java.util.Map;
18import java.util.HashMap;
19
Ian Maxonf18bba22015-08-21 12:35:14 -070020import org.apache.asterix.aql.literal.FloatLiteral;
21import org.apache.asterix.aql.literal.DoubleLiteral;
22import org.apache.asterix.aql.literal.FalseLiteral;
23import org.apache.asterix.aql.base.Literal;
24import org.apache.asterix.aql.literal.IntegerLiteral;
25import org.apache.asterix.aql.literal.LongIntegerLiteral;
26import org.apache.asterix.aql.literal.NullLiteral;
27import org.apache.asterix.aql.literal.StringLiteral;
28import org.apache.asterix.aql.literal.TrueLiteral;
29import org.apache.asterix.aql.parser.ScopeChecker;
30import org.apache.asterix.aql.base.*;
31import org.apache.asterix.aql.expression.*;
32import org.apache.asterix.aql.expression.visitor.AQLPrintVisitor;
33import org.apache.asterix.aql.expression.UnaryExpr.Sign;
34import org.apache.asterix.aql.expression.TypeExpression.TypeExprKind;
35import org.apache.asterix.aql.base.Statement.Kind;
36import org.apache.asterix.aql.context.Scope;
37import org.apache.asterix.aql.context.RootScopeFactory;
38import org.apache.asterix.common.exceptions.AsterixException;
39import org.apache.asterix.om.functions.AsterixFunction;
40import org.apache.asterix.common.functions.FunctionSignature;
41import org.apache.asterix.metadata.bootstrap.MetadataConstants;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000042
Ian Maxonf18bba22015-08-21 12:35:14 -070043import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
44import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
45import org.apache.hyracks.algebricks.common.utils.Pair;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000046
47
ramangrover29a13f2422012-03-15 23:01:27 +000048
vinayakb38b7ca42012-03-05 05:44:15 +000049public class AQLPlusParser extends ScopeChecker {
50
51/*
52 private void printHints(Token t) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -080053 //System.err.println("token="+t.image+"\t special="+t.specialToken);
vinayakb38b7ca42012-03-05 05:44:15 +000054 if (t.specialToken == null) return;
55 Token tmp_t = t.specialToken;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080056 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
vinayakb38b7ca42012-03-05 05:44:15 +000057 while (tmp_t != null) {
58 System.out.println(tmp_t.image);
59 tmp_t = tmp_t.next;
60 }
61 }
62*/
Taewoo Kima12d8cd2015-03-04 13:47:08 -080063
vinayakb38b7ca42012-03-05 05:44:15 +000064 private static final String HASH_GROUP_BY_HINT = "hash";
65 private static final String BROADCAST_JOIN_HINT = "bcast";
66 private static final String INMEMORY_HINT = "inmem";
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000067 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
Taewoo Kima12d8cd2015-03-04 13:47:08 -080068
69
70
vinayakb38b7ca42012-03-05 05:44:15 +000071 private static String getHint(Token t) {
72 if (t.specialToken == null) {
73 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080074 }
vinayakb38b7ca42012-03-05 05:44:15 +000075 String s = t.specialToken.image;
76 int n = s.length();
77 if (n < 2) {
78 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080079 }
vinayakb38b7ca42012-03-05 05:44:15 +000080 return s.substring(1).trim();
81 }
82
Taewoo Kima12d8cd2015-03-04 13:47:08 -080083 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
84 File file = new File(args[0]);
85 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
86 AQLPlusParser parser = new AQLPlusParser(fis);
87 List<Statement> st = parser.Statement();
88 }
vinayakb38b7ca42012-03-05 05:44:15 +000089
90 public void initScope() {
91 scopeStack.push(RootScopeFactory.createRootScope(this));
92 }
93}
94
95PARSER_END(AQLPlusParser)
96
97
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000098List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +000099{
100 Query query = null;
101 // scopeStack.push(RootScopeFactory.createRootScope(this));
102 initScope();
103 List<Statement> decls = new ArrayList<Statement>();
104}
105{
106 (
107 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800108 (
vinayakb38b7ca42012-03-05 05:44:15 +0000109 "use"
110 {
111 decls.add(DataverseDeclaration());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800112 }
vinayakb38b7ca42012-03-05 05:44:15 +0000113 | "declare"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800114 ( "function" {
115 decls.add(FunctionDeclaration());
vinayakb38b7ca42012-03-05 05:44:15 +0000116 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800117 | "type" {
118 decls.add(TypeDeclaration());
119 }
120 )
vinayakb38b7ca42012-03-05 05:44:15 +0000121 | "load" {
122 decls.add(LoadStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800123 }
124
vinayakb38b7ca42012-03-05 05:44:15 +0000125 | "write" {
126 decls.add(WriteStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800127 }
vinayakb38b7ca42012-03-05 05:44:15 +0000128 | "set" {
129 decls.add(SetStatement());
130 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800131 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000132 {
133 decls.add(Query()) ;
134 } ";"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800135
136
vinayakb38b7ca42012-03-05 05:44:15 +0000137 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000138 )
139
140 <EOF>
141 )
142 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800143
144 return decls;
vinayakb38b7ca42012-03-05 05:44:15 +0000145 }
146}
147
148Statement SetStatement() throws ParseException:
149{
150 String pn = null;
151 Statement stmt = null;
152}
153{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800154 <IDENTIFIER> { pn = token.image; }
vinayakb38b7ca42012-03-05 05:44:15 +0000155 <STRING_LITERAL>
156 { String pv = removeQuotesAndEscapes(token.image); }
157 ";"
158 {
159 return new SetStatement(pn, pv);
160 }
161}
162
163Statement WriteStatement() throws ParseException:
164{
165 Identifier nodeName = null;
166 String fileName = null;
167 Identifier datasetName = null;
168 Statement stmt = null;
169 Query query;
170}
171{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800172 ( "output" "to"
173 <IDENTIFIER> { nodeName = new Identifier(token.image); }
174 ":" <STRING_LITERAL> {
vinayakb38b7ca42012-03-05 05:44:15 +0000175 fileName = removeQuotesAndEscapes(token.image);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800176 stmt = new WriteStatement(nodeName, fileName, null);
Till Westmann35a0f702013-07-01 14:06:34 -0700177 }
178 ) ";"
vinayakb38b7ca42012-03-05 05:44:15 +0000179 {
180 return stmt;
181 }
182}
183
184DataverseDecl DataverseDeclaration() throws ParseException:
185{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800186 Identifier dvName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000187}
188{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000189 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000190 ";"
191 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000192 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000193 }
194}
195
zheilbron28e026f2013-11-20 10:15:15 -0800196LoadStatement LoadStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000197{
198 Identifier datasetName = null;
199 boolean alreadySorted = false;
200 String adapter;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800201 Map<String,String> properties = new HashMap<String,String>();
vinayakb38b7ca42012-03-05 05:44:15 +0000202 String name;
203 String value;
204}
205{
206 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800207
208 "using"
vinayakb38b7ca42012-03-05 05:44:15 +0000209 (
210 <STRING_LITERAL>
211 {
212 adapter = removeQuotesAndEscapes(token.image);
213 }
214 <LEFTPAREN>
215 (
216 (
217 <LEFTPAREN>
218 (
219 <STRING_LITERAL>
220 {
221 name = removeQuotesAndEscapes(token.image);
222 }
223 "=" <STRING_LITERAL>
224 {
225 value = removeQuotesAndEscapes(token.image);
226 }
227 )
228 <RIGHTPAREN>
229 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800230 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000231 }
232 )
233 (
234 "," <LEFTPAREN>
235 (
236 <STRING_LITERAL>
237 {
238 name = removeQuotesAndEscapes(token.image);
239 }
240 "=" <STRING_LITERAL>
241 {
242 value = removeQuotesAndEscapes(token.image);
243 }
244 )
245 <RIGHTPAREN>
246 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800247 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000248 }
249 )*
250 )?
251 <RIGHTPAREN>
252 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800253
254 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000255 { alreadySorted = true; }
256 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800257
vinayakb38b7ca42012-03-05 05:44:15 +0000258 ";"
259 {
zheilbron28e026f2013-11-20 10:15:15 -0800260 return new LoadStatement(null, datasetName, adapter, properties, alreadySorted);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800261 }
vinayakb38b7ca42012-03-05 05:44:15 +0000262}
263
264TypeDecl TypeDeclaration() throws ParseException:
265{
266 Identifier ident;
267 TypeExpression typeExpr;
268}
269{
270 <IDENTIFIER>
271 {
272 ident = new Identifier(token.image.toString());
273 }
274 "as"
275 ( typeExpr = TypeExpr() )
276 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000277 return new TypeDecl(null, ident, typeExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000278 }
279}
280
281TypeExpression TypeExpr() throws ParseException:
282{
283 TypeExpression typeExpr = null;
284}
285{
286 (
287 typeExpr = RecordTypeDef()
288 | typeExpr = TypeReference()
289 | typeExpr = OrderedListTypeDef()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800290 | typeExpr = UnorderedListTypeDef()
291 )
vinayakb38b7ca42012-03-05 05:44:15 +0000292 {
293 return typeExpr;
294 }
295}
296
297RecordTypeDefinition RecordTypeDef() throws ParseException:
298{
299 RecordTypeDefinition recType = new RecordTypeDefinition();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800300 RecordTypeDefinition.RecordKind recordKind = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000301}
302{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800303 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
vinayakb38b7ca42012-03-05 05:44:15 +0000304 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
305 "{"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800306 (
307 RecordField(recType)
308 ( "," RecordField(recType) )*
309 )?
vinayakb38b7ca42012-03-05 05:44:15 +0000310 "}"
311 {
312 if (recordKind == null) {
313 recordKind = RecordTypeDefinition.RecordKind.OPEN;
314 }
315 recType.setRecordKind(recordKind);
316 return recType;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800317 }
vinayakb38b7ca42012-03-05 05:44:15 +0000318}
319
320void RecordField(RecordTypeDefinition recType) throws ParseException:
321{
322 String fieldName;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800323 TypeExpression type = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000324 boolean nullable = false;
325}
326{
327 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800328 {
329 Token t = getToken(0);
330 fieldName = t.toString();
331 }
332 ":"
333 ( type = TypeExpr() )
334 ("?" { nullable = true; } )?
335 {
336
337 recType.addField(fieldName, type, nullable);
338 }
vinayakb38b7ca42012-03-05 05:44:15 +0000339}
340
341TypeReferenceExpression TypeReference() throws ParseException:
342{}
343{
344 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800345 {
346 Token t = getToken(0);
347 Identifier id;
348 if (t.toString().equalsIgnoreCase("int")) {
349 id = new Identifier("int64");
350 } else {
351 id = new Identifier(t.toString());
352 }
353 return new TypeReferenceExpression(id);
354 }
vinayakb38b7ca42012-03-05 05:44:15 +0000355}
356
357OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800358{
vinayakb38b7ca42012-03-05 05:44:15 +0000359 TypeExpression type = null;
360}
361{
362 "["
363 ( type = TypeExpr() )
364 "]"
365 {
366 return new OrderedListTypeDefinition(type);
367 }
368}
369
370
371UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800372{
vinayakb38b7ca42012-03-05 05:44:15 +0000373 TypeExpression type = null;
374}
375{
376 "<"
377 ( type = TypeExpr() )
378 ">"
379 {
380 return new UnorderedListTypeDefinition(type);
381 }
382}
383
384
385FunctionDecl FunctionDeclaration() throws ParseException:
386{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000387 FunctionDecl funcDecl;
388 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +0000389 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +0000390 int arity = 0;
391 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
392 Expression funcBody;
393 VarIdentifier var = null;
394 createNewScope();
395}
396{
397
398 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800399 {
400 Token t = getToken(0);
401 functionName = t.toString();
402 if (functionName.equalsIgnoreCase("int")) {
403 functionName = "int64";
404 }
405 }
vinayakb38b7ca42012-03-05 05:44:15 +0000406 <LEFTPAREN> (<VARIABLE>
407 {
408 var = new VarIdentifier();
409 var.setValue(getToken(0).toString());
410 paramList.add(var);
411 getCurrentScope().addNewVarSymbolToScope(var);
412 arity++;
413 }
414 ("," <VARIABLE>
415 {
416 var = new VarIdentifier();
417 var.setValue(getToken(0).toString());
418 paramList.add(var);
419 getCurrentScope().addNewVarSymbolToScope(var);
420 arity++;
421 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
422
423 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000424 signature = new FunctionSignature(defaultDataverse, functionName, arity);
425 getCurrentScope().addFunctionDescriptor(signature, false);
426 funcDecl = new FunctionDecl(signature, paramList, funcBody);
427 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +0000428 }
429}
430
431Query Query()throws ParseException:
432{
433 Query query = new Query();
434 Expression expr;
435}
436{
437 expr = Expression()
438
439 {
440 query.setBody(expr);
441 return query;
442 }
443}
444
445
446
447Expression Expression():
448{
449 Expression expr = null;
450 Expression exprP = null;
451}
452{
453(
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800454
vinayakb38b7ca42012-03-05 05:44:15 +0000455//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
456 expr = OperatorExpr()
457 | expr = IfThenElse()
458 | expr = FLWOGR()
459 | expr = QuantifiedExpression()
460
461
462)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800463 {
464 return (exprP==null) ? expr : exprP;
465 }
vinayakb38b7ca42012-03-05 05:44:15 +0000466}
467
468
469
470Expression OperatorExpr()throws ParseException:
471{
472 OperatorExpr op = null;
473 Expression operand = null;
474}
475{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800476 operand = AndExpr()
477 (
478
479 "or"
480 {
481 if (op == null) {
482 op = new OperatorExpr();
483 op.addOperand(operand);
484 op.setCurrentop(true);
485 }
486 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000487 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800488 }
vinayakb38b7ca42012-03-05 05:44:15 +0000489
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800490 operand = AndExpr()
491 {
492 op.addOperand(operand);
493 }
vinayakb38b7ca42012-03-05 05:44:15 +0000494
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800495 )*
496
497 {
498 return op==null? operand: op;
499 }
vinayakb38b7ca42012-03-05 05:44:15 +0000500}
501
502Expression AndExpr()throws ParseException:
503{
504 OperatorExpr op = null;
505 Expression operand = null;
506}
507{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800508 operand = RelExpr()
509 (
510
511 "and"
512 {
513 if (op == null) {
514 op = new OperatorExpr();
515 op.addOperand(operand);
516 op.setCurrentop(true);
517 }
518 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000519 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800520 }
vinayakb38b7ca42012-03-05 05:44:15 +0000521
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800522 operand = RelExpr()
523 {
524 op.addOperand(operand);
525 }
vinayakb38b7ca42012-03-05 05:44:15 +0000526
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800527 )*
528
529 {
530 return op==null? operand: op;
531 }
vinayakb38b7ca42012-03-05 05:44:15 +0000532}
533
534
535
536Expression RelExpr()throws ParseException:
537{
538 OperatorExpr op = null;
539 Expression operand = null;
540 boolean broadcast = false;
541}
542{
543 operand = AddExpr()
544 {
545 if (operand instanceof VariableExpr) {
546 String hint = getHint(token);
547 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800548 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000549 }
550 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800551 }
vinayakb38b7ca42012-03-05 05:44:15 +0000552
553 (
554 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800555 {
556 if (op == null) {
557 op = new OperatorExpr();
558 op.addOperand(operand, broadcast);
vinayakb38b7ca42012-03-05 05:44:15 +0000559 op.setCurrentop(true);
560 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800561 }
562 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000563 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800564 }
565
566 operand = AddExpr()
567 {
vinayakb38b7ca42012-03-05 05:44:15 +0000568 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800569 if (operand instanceof VariableExpr) {
vinayakb38b7ca42012-03-05 05:44:15 +0000570 String hint = getHint(token);
571 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
572 broadcast = true;
573 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800574 }
vinayakb38b7ca42012-03-05 05:44:15 +0000575 op.addOperand(operand, broadcast);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800576 }
vinayakb38b7ca42012-03-05 05:44:15 +0000577 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800578
579 {
580 return op==null? operand: op;
581 }
vinayakb38b7ca42012-03-05 05:44:15 +0000582}
583
584Expression AddExpr()throws ParseException:
585{
586 OperatorExpr op = null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800587 Expression operand = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000588}
589{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800590 operand = MultExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000591
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800592 ( ("+" | "-")
593 {
594 if (op == null) {
595 op = new OperatorExpr();
596 op.addOperand(operand);
597 op.setCurrentop(true);
598 }
599 Token t = getToken(0);
600 ((OperatorExpr)op).addOperator(t.toString());
601 }
vinayakb38b7ca42012-03-05 05:44:15 +0000602
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800603 operand = MultExpr()
604 {
605 op.addOperand(operand);
606 }
607 )*
608
609 {
610 return op==null? operand: op;
611 }
vinayakb38b7ca42012-03-05 05:44:15 +0000612}
613
614Expression MultExpr()throws ParseException:
615{
616 OperatorExpr op = null;
617 Expression operand = null;
618}
619{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800620 operand = UnionExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000621
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800622 (( "*" | "/" | "%" | <CARET> | "idiv")
623 {
624 if (op == null) {
625 op = new OperatorExpr();
vinayakb38b7ca42012-03-05 05:44:15 +0000626 op.addOperand(operand);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800627 op.setCurrentop(true);
628 }
629 Token t = getToken(0);
630 op.addOperator(t.toString());
631 }
632 operand = UnionExpr()
633 {
634 op.addOperand(operand);
635 }
636 )*
637
638 {
639 return op==null?operand:op;
640 }
vinayakb38b7ca42012-03-05 05:44:15 +0000641}
642
643Expression UnionExpr() throws ParseException:
644{
645 UnionExpr union = null;
646 Expression operand1 = null;
647 Expression operand2 = null;
648}
649{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800650 operand1 = UnaryExpr()
651 ("union"
vinayakb38b7ca42012-03-05 05:44:15 +0000652 (operand2 = UnaryExpr()) {
653 if (union == null) {
654 union = new UnionExpr();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800655 union.addExpr(operand1);
vinayakb38b7ca42012-03-05 05:44:15 +0000656 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800657 union.addExpr(operand2);
vinayakb38b7ca42012-03-05 05:44:15 +0000658 } )*
659 {
660 return (union == null)? operand1: union;
661 }
662}
663
664Expression UnaryExpr() throws ParseException:
665{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800666 Expression uexpr = null;
667 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000668}
669{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800670 (( "+"|"-")
671 {
672 uexpr = new UnaryExpr();
673 Token t = getToken(0);
674 if("+".equals(t.toString()))
675 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
676 else if("-".equals(t.toString()))
677 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
678 else
679 throw new ParseException();
680 }
681 )?
682
683 expr = ValueExpr()
684 {
685 if(uexpr!=null){
686 ((UnaryExpr)uexpr).setExpr(expr);
687 return uexpr;
688 }
689 else{
690 return expr;
691 }
692 }
vinayakb38b7ca42012-03-05 05:44:15 +0000693}
694
695Expression ValueExpr() throws ParseException:
696{
697 Expression expr;
698}
699{
700 expr = FieldOrIndexAccessor()
701 {
702 return expr;
703 }
704}
705
706
707Expression FieldOrIndexAccessor()throws ParseException:
708{
709 Expression expr = null;
710 Identifier ident = null;
711 AbstractAccessor fa = null;
icetindila611ac72014-05-16 10:10:11 -0700712 Expression indexExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000713
714}
715{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800716 ( expr = PrimaryExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000717
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800718 )
vinayakb38b7ca42012-03-05 05:44:15 +0000719
720
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800721 (
722 (
723 ident = Field()
724 {
725 if(fa == null)
726 fa = new FieldAccessor(expr, ident);
727 else
728 fa = new FieldAccessor(fa, ident);
729 }
730 )
731 | (
732 indexExpr = Index()
733 {
734 if(fa == null)
735 fa = new IndexAccessor(expr, indexExpr);
736 else
737 fa = new IndexAccessor(fa, indexExpr);
738 }
739 )
740 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000741
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800742
743 {
744 return fa==null?expr:fa;
745 }
vinayakb38b7ca42012-03-05 05:44:15 +0000746}
747
748Identifier Field() throws ParseException:
749{
750 Identifier ident = null;
751
752}
753{
754 "." < IDENTIFIER >
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800755 {
vinayakb38b7ca42012-03-05 05:44:15 +0000756
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800757 ident = new Identifier();
758 ident.setValue(getToken(0).toString());
759
760 return ident;
761 }
vinayakb38b7ca42012-03-05 05:44:15 +0000762}
763
icetindila611ac72014-05-16 10:10:11 -0700764Expression Index() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000765{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800766 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000767}
768{
769 "[" ( expr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800770 {
771 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
772 {
773 Literal lit = ((LiteralExpr)expr).getValue();
774 if(lit.getLiteralType() != Literal.Type.INTEGER &&
775 lit.getLiteralType() != Literal.Type.LONG) {
776 throw new ParseException("Index should be an INTEGER");
vinayakb38b7ca42012-03-05 05:44:15 +0000777 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800778 }
779 }
vinayakb38b7ca42012-03-05 05:44:15 +0000780
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800781 | "?" // ANY
782
783 )
vinayakb38b7ca42012-03-05 05:44:15 +0000784
785 "]"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800786 {
787 return expr;
788 }
vinayakb38b7ca42012-03-05 05:44:15 +0000789}
790
791
792Expression PrimaryExpr()throws ParseException:
793{
794 Expression expr = null;
795}
796{
ilovesoupc9fef1d2012-07-08 19:30:42 +0000797 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800798 (
799 expr =Literal()
800 | expr = FunctionCallExpr()
801 | expr =VariableRef()
802
vinayakb38b7ca42012-03-05 05:44:15 +0000803 {
804 if(((VariableExpr)expr).getIsNewVar() == true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800805 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +0000806 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800807 | expr = ListConstructor()
808 | expr = RecordConstructor()
809 | expr = ParenthesizedExpression()
810 | expr = MetaVariableRef()
811 )
812 {
813 return expr;
814 }
vinayakb38b7ca42012-03-05 05:44:15 +0000815}
816
817Expression Literal() throws ParseException:
818{
819
820 LiteralExpr lit = new LiteralExpr();
821 Token t;
822}
823{
824(
825 <STRING_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +0000826 {
827 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800828 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
829 }
830
831 | <INTEGER_LITERAL>
832 {
833 t= getToken(0);
834 try {
835 lit.setValue(new IntegerLiteral(new Integer(t.image)));
836 } catch(NumberFormatException ex) {
837 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
838 }
839 }
vinayakb38b7ca42012-03-05 05:44:15 +0000840 | < FLOAT_LITERAL >
841 {
842 t= getToken(0);
843 lit.setValue(new FloatLiteral(new Float(t.image)));
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800844 }
845 | < DOUBLE_LITERAL >
vinayakb38b7ca42012-03-05 05:44:15 +0000846 {
847 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800848 lit.setValue(new DoubleLiteral(new Double(t.image)));
849 }
850 | <NULL>
851 {
852 t= getToken(0);
853 lit.setValue(NullLiteral.INSTANCE);
854 }
855 | <TRUE>
856 {
857 t= getToken(0);
858 lit.setValue(TrueLiteral.INSTANCE);
859 }
860 | <FALSE>
861 {
862 t= getToken(0);
863 lit.setValue(FalseLiteral.INSTANCE);
864 }
vinayakb38b7ca42012-03-05 05:44:15 +0000865)
866 {
867 return lit;
868 }
869}
870
871
872VariableExpr VariableRef() throws ParseException:
873{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800874 VariableExpr varExp = new VariableExpr();
875 VarIdentifier var = new VarIdentifier();
876 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000877}
878{
879 <VARIABLE>
880 {
881 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800882 String varName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +0000883 Identifier ident = lookupSymbol(varName);
884 if (isInForbiddenScopes(varName)) {
885 throw new ParseException("Inside limit clauses, it is disallowed to reference a variable having the same name as any variable bound in the same scope as the limit clause.");
886 }
887 if(ident != null) { // exist such ident
888 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800889 varExp.setVar((VarIdentifier)ident);
vinayakb38b7ca42012-03-05 05:44:15 +0000890 } else {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800891 varExp.setVar(var);
vinayakb38b7ca42012-03-05 05:44:15 +0000892 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800893 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000894 return varExp;
895 }
896}
897
898
899VariableExpr Variable() throws ParseException:
900{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800901 VariableExpr varExp = new VariableExpr();
902 VarIdentifier var = new VarIdentifier();
903 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000904}
905{
906 <VARIABLE>
907 {
908 t = getToken(0);//get current token
909 Identifier ident = lookupSymbol(t.toString());
910 if(ident != null) { // exist such ident
911 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800912 }
913 varExp.setVar(var);
914 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000915 return varExp;
916 }
917}
918
919MetaVariableExpr MetaVariableRef() throws ParseException:
920{
921 MetaVariableExpr metaVarExp = new MetaVariableExpr();
922 VarIdentifier var = new VarIdentifier();
923 Token t;
924}
925{
926 <METAVARIABLE>
927 {
928 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800929 metaVarExp.setVar(var);
930 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000931 return metaVarExp;
932 }
933}
934
935Expression ListConstructor() throws ParseException:
936{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800937 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000938}
939{
940 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800941 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
vinayakb38b7ca42012-03-05 05:44:15 +0000942 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800943
vinayakb38b7ca42012-03-05 05:44:15 +0000944 {
945 return expr;
946 }
947}
948
949
950ListConstructor OrderedListConstructor() throws ParseException:
951{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800952 ListConstructor expr = new ListConstructor();
953 Expression tmp = null;
954 List<Expression> exprList = new ArrayList<Expression>();
955 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +0000956}
957{
958
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800959 "["
960 ( tmp = Expression()
961 {
962 exprList.add(tmp);
963 }
964
965 ("," tmp = Expression() { exprList.add(tmp); })*
966 )?
967
vinayakb38b7ca42012-03-05 05:44:15 +0000968 "]"
969
970 {
971 expr.setExprList(exprList);
972 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800973 }
vinayakb38b7ca42012-03-05 05:44:15 +0000974}
975
976ListConstructor UnorderedListConstructor() throws ParseException:
977{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800978 ListConstructor expr = new ListConstructor();
979 Expression tmp = null;
980 List<Expression> exprList = new ArrayList<Expression>();
981 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +0000982}
983{
984
985 "{{" ( tmp = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800986 {
987 exprList.add(tmp);
988 }
vinayakb38b7ca42012-03-05 05:44:15 +0000989 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
990 {
991 expr.setExprList(exprList);
992 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800993 }
vinayakb38b7ca42012-03-05 05:44:15 +0000994}
995
996RecordConstructor RecordConstructor() throws ParseException:
997{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800998 RecordConstructor expr = new RecordConstructor();
999 FieldBinding tmp = null;
1000 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
vinayakb38b7ca42012-03-05 05:44:15 +00001001}
1002{
1003 "{" (tmp = FieldBinding()
1004 {
1005 fbList.add(tmp);
1006 }
1007 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1008 {
1009 expr.setFbList(fbList);
1010 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001011 }
vinayakb38b7ca42012-03-05 05:44:15 +00001012}
1013
1014FieldBinding FieldBinding() throws ParseException:
1015{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001016 FieldBinding fb = new FieldBinding();
1017 Expression left, right;
vinayakb38b7ca42012-03-05 05:44:15 +00001018}
1019{
1020 left = Expression() ":" right = Expression()
1021 {
1022 fb.setLeftExpr(left);
1023 fb.setRightExpr(right);
1024 return fb;
1025 }
1026}
1027
1028Expression FunctionCallExpr() throws ParseException:
1029{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001030 CallExpr callExpr;
1031 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001032 Expression tmp;
1033 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001034 String funcName;
1035 String dataverse;
1036 String hint=null;
1037 String id1=null;
1038 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001039}
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001040{
1041 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;}
1042 ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})?
1043 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001044 <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();}
1045 )
vinayakb38b7ca42012-03-05 05:44:15 +00001046 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001047 hint=getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001048 }
1049 <LEFTPAREN> (tmp = Expression()
1050 {
1051 argList.add(tmp);
1052 arity ++;
1053 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1054
1055 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001056 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1057 if(signature == null)
1058 {
1059 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1060 }
1061 callExpr = new CallExpr(signature,argList);
1062 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1063 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1064 }
1065 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001066 }
1067}
1068
1069
1070Expression ParenthesizedExpression() throws ParseException:
1071{
1072 Expression expr;
1073}
1074{
1075 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1076 {
1077 return expr;
1078 }
1079}
1080
1081Expression IfThenElse() throws ParseException:
1082{
1083 Expression condExpr;
1084 Expression thenExpr;
1085 Expression elseExpr;
1086 IfExpr ifExpr = new IfExpr();
1087}
1088{
1089 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1090
1091 {
1092 ifExpr.setCondExpr(condExpr);
1093 ifExpr.setThenExpr(thenExpr);
1094 ifExpr.setElseExpr(elseExpr);
1095 return ifExpr;
1096 }
1097}
1098
1099Expression FLWOGR() throws ParseException:
1100{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001101 FLWOGRExpression flworg = new FLWOGRExpression();
1102 List<Clause> clauseList = new ArrayList<Clause>();
1103 Expression returnExpr;
1104 Clause tmp;
1105 createNewScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001106}
1107{
1108 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);})
1109 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1110
1111 {
1112 flworg.setClauseList(clauseList);
1113 flworg.setReturnExpr(returnExpr);
1114 removeCurrentScope();
1115 return flworg;
1116 }
1117}
1118
1119List<Clause> Clauses() throws ParseException:
1120{
1121 List<Clause> clauses = new ArrayList<Clause>();
1122 Clause c = null;
1123}
1124{
1125 (
1126 (
1127 c = Clause() {
1128 clauses.add(c);
1129 }
1130 )*
1131 )
1132 {
1133 return clauses;
1134 }
1135}
1136
1137Clause Clause() throws ParseException :
1138{
1139 Clause clause;
1140}
1141{
1142 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001143 clause = ForClause()
1144 | clause = LetClause()
1145 | clause = WhereClause()
1146 | clause = OrderbyClause()
1147 | clause = GroupClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001148 | clause = LimitClause()
1149 | clause = DistinctClause()
1150 | clause = MetaVariableClause()
1151 | clause = JoinClause()
1152 )
1153 {
1154 return clause;
1155 }
1156}
1157
1158Clause MetaVariableClause() throws ParseException :
1159{
1160 MetaVariableClause mc = new MetaVariableClause();
1161 VarIdentifier var = new VarIdentifier();
1162 Token t;
1163}
1164{
1165 <METAVARIABLECLAUSE>
1166 {
1167 t = getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001168 mc.setVar(var);
1169 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +00001170 return mc;
1171 }
1172}
1173
1174Clause JoinClause() throws ParseException :
1175{
1176 Expression whereExpr;
1177 List<Clause> leftClauses, rightClauses;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001178 JoinClause.JoinKind kind = JoinClause.JoinKind.INNER;
vinayakb38b7ca42012-03-05 05:44:15 +00001179}
1180{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001181 ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } )
1182 <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> ","
1183 <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> ","
vinayakb38b7ca42012-03-05 05:44:15 +00001184 whereExpr = Expression() <RIGHTPAREN>
1185 {
1186 JoinClause jc = new JoinClause(kind);
1187 jc.setLeftClauses(leftClauses);
1188 jc.setRightClauses(rightClauses);
1189 jc.setWhereExpr(whereExpr);
1190 return jc;
1191 }
1192}
1193
1194Clause ForClause()throws ParseException :
1195{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001196 ForClause fc = new ForClause();
1197 VariableExpr varExp;
1198 VariableExpr varPos = null;
1199 Expression inExp;
1200 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001201}
1202{
1203 "for" varExp = Variable()
1204 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001205 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1206 }
1207 ("at" varPos = Variable()
1208 {
1209 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1210 }
1211 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001212 "in" ( inExp = Expression() )
1213 {
1214 fc.setVarExpr(varExp);
1215 fc.setInExpr(inExp);
1216 if (varPos != null) {
1217 fc.setPosExpr(varPos);
1218 }
1219 return fc;
1220 }
1221}
1222
1223Clause LetClause() throws ParseException:
1224{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001225 LetClause lc = new LetClause();
1226 VariableExpr varExp;
1227 Expression beExp;
1228 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001229}
1230{
ilovesoupb2527c12012-07-12 03:21:13 +00001231 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001232 {
1233 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001234 lc.setVarExpr(varExp);
1235 lc.setBeExpr(beExp);
1236 return lc;
1237 }
1238}
1239
1240Clause WhereClause()throws ParseException :
1241{
1242 WhereClause wc = new WhereClause();
1243 Expression whereExpr;
1244}
1245{
1246 "where" whereExpr = Expression()
1247 {
1248 wc.setWhereExpr(whereExpr);
1249 return wc;
1250 }
1251}
1252
1253Clause OrderbyClause()throws ParseException :
1254{
1255 OrderbyClause oc = new OrderbyClause();
1256 Expression orderbyExpr;
1257 List<Expression> orderbyList = new ArrayList<Expression>();
1258 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1259 int numOfOrderby = 0;
1260}
1261{
1262 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001263 "order"
vinayakb38b7ca42012-03-05 05:44:15 +00001264 {
1265 String hint = getHint(token);
1266 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001267 String splits[] = hint.split(" +");
vinayakb38b7ca42012-03-05 05:44:15 +00001268 int numFrames = Integer.parseInt(splits[1]);
1269 int numTuples = Integer.parseInt(splits[2]);
1270 oc.setNumFrames(numFrames);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001271 oc.setNumTuples(numTuples);
1272 }
1273 }
vinayakb38b7ca42012-03-05 05:44:15 +00001274 "by" orderbyExpr = Expression()
1275 {
1276 orderbyList.add(orderbyExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001277 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
vinayakb38b7ca42012-03-05 05:44:15 +00001278 }
1279 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1280 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1281 {
1282 modifierList.add(modif);
1283 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001284
vinayakb38b7ca42012-03-05 05:44:15 +00001285 ("," orderbyExpr = Expression()
1286 {
1287 orderbyList.add(orderbyExpr);
1288 modif = OrderbyClause.OrderModifier.ASC;
1289 }
1290 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1291 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1292 {
1293 modifierList.add(modif);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001294 }
vinayakb38b7ca42012-03-05 05:44:15 +00001295 )*
1296)
1297 {
1298 oc.setModifierList(modifierList);
1299 oc.setOrderbyList(orderbyList);
1300 return oc;
1301 }
1302}
1303Clause GroupClause()throws ParseException :
1304{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001305 GroupbyClause gbc = new GroupbyClause();
1306 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1307 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
vinayakb38b7ca42012-03-05 05:44:15 +00001308 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001309 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1310 VariableExpr var = null;
1311 VariableExpr withVar = null;
1312 Expression expr = null;
1313 VariableExpr decorVar = null;
1314 Expression decorExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001315}
1316{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001317 {
1318 Scope newScope = extendCurrentScopeNoPush(true);
1319 // extendCurrentScope(true);
1320 }
vinayakb38b7ca42012-03-05 05:44:15 +00001321 "group"
1322 {
1323 String hint = getHint(token);
1324 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001325 gbc.setHashGroupByHint(true);
1326 }
1327 }
vinayakb38b7ca42012-03-05 05:44:15 +00001328 "by" (LOOKAHEAD(2) var = Variable()
1329 {
1330 newScope.addNewVarSymbolToScope(var.getVar());
1331 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001332 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001333 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001334 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001335 vePairList.add(pair1);
1336 }
1337 ("," ( LOOKAHEAD(2) var = Variable()
1338 {
1339 newScope.addNewVarSymbolToScope(var.getVar());
1340 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001341 expr = Expression()
1342 {
1343 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001344 vePairList.add(pair2);
1345 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001346 )*
vinayakb38b7ca42012-03-05 05:44:15 +00001347 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001348 {
1349 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001350 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1351 decorPairList.add(pair3);
1352 }
1353 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001354 {
1355 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001356 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001357 decorPairList.add(pair4);
vinayakb38b7ca42012-03-05 05:44:15 +00001358 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001359 )*
1360 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001361 "with" withVar = VariableRef()
1362 {
1363 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001364 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001365 withVarList.add(withVar);
1366 newScope.addNewVarSymbolToScope(withVar.getVar());
1367 }
1368 ("," withVar = VariableRef()
1369 {
1370 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001371 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001372 withVarList.add(withVar);
1373 newScope.addNewVarSymbolToScope(withVar.getVar());
1374 })*
1375 {
1376 gbc.setGbyPairList(vePairList);
1377 gbc.setDecorPairList(decorPairList);
1378 gbc.setWithVarList(withVarList);
1379 replaceCurrentScope(newScope);
1380 return gbc;
1381 }
1382}
1383
1384
1385LimitClause LimitClause() throws ParseException:
1386{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001387 LimitClause lc = new LimitClause();
1388 Expression expr;
1389 pushForbiddenScope(getCurrentScope());
vinayakb38b7ca42012-03-05 05:44:15 +00001390}
1391{
1392 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1393 ("offset" expr = Expression() { lc.setOffset(expr); })?
1394
1395 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001396 popForbiddenScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001397 return lc;
1398 }
1399}
1400
1401DistinctClause DistinctClause() throws ParseException:
1402{
1403 List<Expression> exprs = new ArrayList<Expression>();
1404 Expression expr;
1405}
1406{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001407 "distinct" "by" expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001408 {
1409 exprs.add(expr);
1410 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001411 ("," expr = Expression()
1412 {
1413 exprs.add(expr);
1414 }
vinayakb38b7ca42012-03-05 05:44:15 +00001415 )*
1416 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001417 return new DistinctClause(exprs);
vinayakb38b7ca42012-03-05 05:44:15 +00001418 }
1419}
1420
1421
1422QuantifiedExpression QuantifiedExpression()throws ParseException:
1423{
1424 QuantifiedExpression qc = new QuantifiedExpression();
1425 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
1426 Expression satisfiesExpr;
1427 VariableExpr var;
1428 Expression inExpr;
1429 QuantifiedPair pair;
1430}
1431{
1432 {
1433 createNewScope();
1434 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001435
vinayakb38b7ca42012-03-05 05:44:15 +00001436 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001437 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
1438 var = Variable() "in" inExpr = Expression()
1439 {
vinayakb38b7ca42012-03-05 05:44:15 +00001440 pair = new QuantifiedPair(var, inExpr);
1441 getCurrentScope().addNewVarSymbolToScope(var.getVar());
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001442 quantifiedList.add(pair);
1443 }
1444 (
1445 "," var = Variable() "in" inExpr = Expression()
1446 {
1447 pair = new QuantifiedPair(var, inExpr);
1448 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1449 quantifiedList.add(pair);
1450 }
1451 )*
1452 "satisfies" satisfiesExpr = Expression()
1453 {
1454 qc.setSatisfiesExpr(satisfiesExpr);
1455 qc.setQuantifiedList(quantifiedList);
1456 removeCurrentScope();
1457 return qc;
1458 }
vinayakb38b7ca42012-03-05 05:44:15 +00001459}
1460
1461TOKEN_MGR_DECLS:
1462{
1463 public int commentDepth = 0;
1464}
1465
1466<DEFAULT>
1467TOKEN :
1468{
1469 <CARET : "^" >
1470}
1471
1472<DEFAULT>
1473TOKEN :
1474{
1475 <DATASET : "dataset" >
1476}
1477
1478<DEFAULT>
1479TOKEN :
1480{
1481 <LEFTPAREN : "(" >
1482}
1483
1484<DEFAULT>
1485TOKEN :
1486{
1487 <RIGHTPAREN : ")" >
1488}
1489
1490
1491<DEFAULT>
1492TOKEN :
1493{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001494 <INTEGER_LITERAL : (<DIGIT>)+ >
vinayakb38b7ca42012-03-05 05:44:15 +00001495}
1496
1497
1498<DEFAULT>
1499TOKEN :
1500{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001501 <NULL : "null">
vinayakb38b7ca42012-03-05 05:44:15 +00001502}
1503
1504<DEFAULT>
1505TOKEN :
1506{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001507 <TRUE : "true">
vinayakb38b7ca42012-03-05 05:44:15 +00001508}
1509
1510<DEFAULT>
1511TOKEN :
1512{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001513 <FALSE : "false">
vinayakb38b7ca42012-03-05 05:44:15 +00001514}
1515
1516<DEFAULT>
1517TOKEN :
1518{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001519 <#DIGIT : ["0" - "9"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001520}
1521
1522
1523TOKEN:
1524{
1525 < DOUBLE_LITERAL: <INTEGER>
1526 | <INTEGER> ( "." <INTEGER> )?
1527 | "." <INTEGER>
1528 >
1529 |
1530 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
1531 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
1532 | "." <INTEGER> ( "f" | "F" )
1533 >
1534 |
1535 <INTEGER : (<DIGIT>)+ >
1536}
1537
1538<DEFAULT>
1539TOKEN :
1540{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001541 <#LETTER : ["A" - "Z", "a" - "z"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001542}
1543
1544<DEFAULT>
1545TOKEN :
1546{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001547 <SPECIALCHARS : ["$", "_", "-"] >
vinayakb38b7ca42012-03-05 05:44:15 +00001548}
1549
1550<DEFAULT>
1551TOKEN :
1552{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001553 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
1554 |
1555 < #EscapeQuot: "\\\"" >
1556 |
vinayakb38b7ca42012-03-05 05:44:15 +00001557 < #EscapeApos: "\\\'" >
1558}
1559
1560<DEFAULT>
1561TOKEN :
1562{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001563 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
vinayakb38b7ca42012-03-05 05:44:15 +00001564}
1565
1566<DEFAULT>
1567TOKEN :
1568{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001569 <VARIABLE : "$" <IDENTIFIER> >
vinayakb38b7ca42012-03-05 05:44:15 +00001570}
1571
1572<DEFAULT>
1573TOKEN :
1574{
1575 <METAVARIABLECLAUSE : "#" <IDENTIFIER> >
1576}
1577
1578<DEFAULT>
1579TOKEN :
1580{
1581 <METAVARIABLE : "$$" <IDENTIFIER> >
1582}
1583
1584SKIP:
1585{
1586 " "
1587| "\t"
1588| "\r"
1589| "\n"
1590}
1591
1592SKIP:
1593{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001594 <"//" (~["\n"])* "\n">
vinayakb38b7ca42012-03-05 05:44:15 +00001595}
1596
1597SKIP:
1598{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001599 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
vinayakb38b7ca42012-03-05 05:44:15 +00001600}
1601
1602
1603SKIP:
1604{
1605 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
1606}
1607
1608<INSIDE_COMMENT>
1609SPECIAL_TOKEN:
1610{
1611 <"+"(" ")*(~["/","*"])*>
1612}
1613
1614<INSIDE_COMMENT>
1615SKIP:
1616{
1617 <"/*"> {commentDepth++;}
1618}
1619
1620<INSIDE_COMMENT>
1621SKIP:
1622{
1623 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
1624| <~[]>
1625}