blob: b10d83740e46a507fbecaba1a6b1ad99eab0f1fb [file] [log] [blame]
Till Westmann5b431ca2015-10-01 19:16:11 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
vinayakb38b7ca42012-03-05 05:44:15 +000020options {
21
Taewoo Kima12d8cd2015-03-04 13:47:08 -080022
vinayakb38b7ca42012-03-05 05:44:15 +000023 STATIC = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080024
vinayakb38b7ca42012-03-05 05:44:15 +000025}
26
27
28PARSER_BEGIN(AQLPlusParser)
29
Ian Maxonf18bba22015-08-21 12:35:14 -070030package org.apache.asterix.aqlplus.parser;
vinayakb38b7ca42012-03-05 05:44:15 +000031
Yingyi Bu391f09e2015-10-29 13:49:39 -070032import java.io.BufferedReader;
33import java.io.File;
34import java.io.FileInputStream;
35import java.io.FileNotFoundException;
36import java.io.IOException;
37import java.io.InputStreamReader;
38import java.io.Reader;
vinayakb38b7ca42012-03-05 05:44:15 +000039import java.util.ArrayList;
vinayakb38b7ca42012-03-05 05:44:15 +000040import java.util.HashMap;
Yingyi Bu391f09e2015-10-29 13:49:39 -070041import java.util.List;
42import java.util.Map;
vinayakb38b7ca42012-03-05 05:44:15 +000043
Abdullah Alamoudie800e6d2016-01-14 11:20:55 +030044import org.apache.asterix.common.config.MetadataConstants;
Ian Maxonf18bba22015-08-21 12:35:14 -070045import org.apache.asterix.common.exceptions.AsterixException;
Ian Maxonf18bba22015-08-21 12:35:14 -070046import org.apache.asterix.common.functions.FunctionSignature;
Yingyi Bu391f09e2015-10-29 13:49:39 -070047import org.apache.asterix.lang.aql.clause.DistinctClause;
48import org.apache.asterix.lang.aql.clause.ForClause;
49import org.apache.asterix.lang.aql.clause.JoinClause;
50import org.apache.asterix.lang.aql.clause.MetaVariableClause;
51import org.apache.asterix.lang.aql.expression.FLWOGRExpression;
52import org.apache.asterix.lang.aql.expression.MetaVariableExpr;
53import org.apache.asterix.lang.aql.expression.UnionExpr;
54import org.apache.asterix.lang.common.base.Clause;
55import org.apache.asterix.lang.common.base.Expression;
56import org.apache.asterix.lang.common.base.Literal;
57import org.apache.asterix.lang.common.base.Statement;
58import org.apache.asterix.lang.common.clause.GroupbyClause;
59import org.apache.asterix.lang.common.clause.LetClause;
60import org.apache.asterix.lang.common.clause.LimitClause;
61import org.apache.asterix.lang.common.clause.OrderbyClause;
62import org.apache.asterix.lang.common.clause.WhereClause;
63import org.apache.asterix.lang.common.context.RootScopeFactory;
64import org.apache.asterix.lang.common.context.Scope;
65import org.apache.asterix.lang.common.expression.AbstractAccessor;
66import org.apache.asterix.lang.common.expression.CallExpr;
67import org.apache.asterix.lang.common.expression.FieldAccessor;
68import org.apache.asterix.lang.common.expression.FieldBinding;
69import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
70import org.apache.asterix.lang.common.expression.IfExpr;
71import org.apache.asterix.lang.common.expression.IndexAccessor;
72import org.apache.asterix.lang.common.expression.ListConstructor;
73import org.apache.asterix.lang.common.expression.LiteralExpr;
74import org.apache.asterix.lang.common.expression.OperatorExpr;
75import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
76import org.apache.asterix.lang.common.expression.QuantifiedExpression;
77import org.apache.asterix.lang.common.expression.RecordConstructor;
78import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
79import org.apache.asterix.lang.common.expression.TypeExpression;
80import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
81import org.apache.asterix.lang.common.expression.UnaryExpr;
82import org.apache.asterix.lang.common.expression.UnaryExpr.Sign;
83import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
84import org.apache.asterix.lang.common.expression.VariableExpr;
85import org.apache.asterix.lang.common.literal.DoubleLiteral;
86import org.apache.asterix.lang.common.literal.FalseLiteral;
87import org.apache.asterix.lang.common.literal.FloatLiteral;
88import org.apache.asterix.lang.common.literal.IntegerLiteral;
89import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
90import org.apache.asterix.lang.common.literal.NullLiteral;
91import org.apache.asterix.lang.common.literal.StringLiteral;
92import org.apache.asterix.lang.common.literal.TrueLiteral;
93import org.apache.asterix.lang.common.parser.ScopeChecker;
94import org.apache.asterix.lang.common.statement.DataverseDecl;
95import org.apache.asterix.lang.common.statement.FunctionDecl;
96import org.apache.asterix.lang.common.statement.LoadStatement;
97import org.apache.asterix.lang.common.statement.Query;
98import org.apache.asterix.lang.common.statement.SetStatement;
99import org.apache.asterix.lang.common.statement.TypeDecl;
100import org.apache.asterix.lang.common.statement.WriteStatement;
101import org.apache.asterix.lang.common.struct.Identifier;
102import org.apache.asterix.lang.common.struct.QuantifiedPair;
103import org.apache.asterix.lang.common.struct.VarIdentifier;
Ian Maxonf18bba22015-08-21 12:35:14 -0700104import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000105
ramangrover29a13f2422012-03-15 23:01:27 +0000106
vinayakb38b7ca42012-03-05 05:44:15 +0000107public class AQLPlusParser extends ScopeChecker {
108
109/*
110 private void printHints(Token t) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800111 //System.err.println("token="+t.image+"\t special="+t.specialToken);
vinayakb38b7ca42012-03-05 05:44:15 +0000112 if (t.specialToken == null) return;
113 Token tmp_t = t.specialToken;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800114 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
vinayakb38b7ca42012-03-05 05:44:15 +0000115 while (tmp_t != null) {
116 System.out.println(tmp_t.image);
117 tmp_t = tmp_t.next;
118 }
119 }
120*/
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800121
vinayakb38b7ca42012-03-05 05:44:15 +0000122 private static final String HASH_GROUP_BY_HINT = "hash";
123 private static final String BROADCAST_JOIN_HINT = "bcast";
124 private static final String INMEMORY_HINT = "inmem";
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000125 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800126
127
128
vinayakb38b7ca42012-03-05 05:44:15 +0000129 private static String getHint(Token t) {
130 if (t.specialToken == null) {
131 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800132 }
vinayakb38b7ca42012-03-05 05:44:15 +0000133 String s = t.specialToken.image;
134 int n = s.length();
135 if (n < 2) {
136 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800137 }
vinayakb38b7ca42012-03-05 05:44:15 +0000138 return s.substring(1).trim();
139 }
140
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800141 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
142 File file = new File(args[0]);
143 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
144 AQLPlusParser parser = new AQLPlusParser(fis);
145 List<Statement> st = parser.Statement();
146 }
vinayakb38b7ca42012-03-05 05:44:15 +0000147
148 public void initScope() {
149 scopeStack.push(RootScopeFactory.createRootScope(this));
150 }
151}
152
153PARSER_END(AQLPlusParser)
154
155
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000156List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000157{
158 Query query = null;
159 // scopeStack.push(RootScopeFactory.createRootScope(this));
160 initScope();
161 List<Statement> decls = new ArrayList<Statement>();
162}
163{
164 (
165 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800166 (
vinayakb38b7ca42012-03-05 05:44:15 +0000167 "use"
168 {
169 decls.add(DataverseDeclaration());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800170 }
vinayakb38b7ca42012-03-05 05:44:15 +0000171 | "declare"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800172 ( "function" {
173 decls.add(FunctionDeclaration());
vinayakb38b7ca42012-03-05 05:44:15 +0000174 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800175 | "type" {
176 decls.add(TypeDeclaration());
177 }
178 )
vinayakb38b7ca42012-03-05 05:44:15 +0000179 | "load" {
180 decls.add(LoadStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800181 }
182
vinayakb38b7ca42012-03-05 05:44:15 +0000183 | "write" {
184 decls.add(WriteStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800185 }
vinayakb38b7ca42012-03-05 05:44:15 +0000186 | "set" {
187 decls.add(SetStatement());
188 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800189 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000190 {
191 decls.add(Query()) ;
192 } ";"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800193
194
vinayakb38b7ca42012-03-05 05:44:15 +0000195 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000196 )
197
198 <EOF>
199 )
200 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800201
202 return decls;
vinayakb38b7ca42012-03-05 05:44:15 +0000203 }
204}
205
206Statement SetStatement() throws ParseException:
207{
208 String pn = null;
209 Statement stmt = null;
210}
211{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800212 <IDENTIFIER> { pn = token.image; }
vinayakb38b7ca42012-03-05 05:44:15 +0000213 <STRING_LITERAL>
214 { String pv = removeQuotesAndEscapes(token.image); }
215 ";"
216 {
217 return new SetStatement(pn, pv);
218 }
219}
220
221Statement WriteStatement() throws ParseException:
222{
223 Identifier nodeName = null;
224 String fileName = null;
225 Identifier datasetName = null;
226 Statement stmt = null;
227 Query query;
228}
229{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800230 ( "output" "to"
231 <IDENTIFIER> { nodeName = new Identifier(token.image); }
232 ":" <STRING_LITERAL> {
vinayakb38b7ca42012-03-05 05:44:15 +0000233 fileName = removeQuotesAndEscapes(token.image);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800234 stmt = new WriteStatement(nodeName, fileName, null);
Till Westmann35a0f702013-07-01 14:06:34 -0700235 }
236 ) ";"
vinayakb38b7ca42012-03-05 05:44:15 +0000237 {
238 return stmt;
239 }
240}
241
242DataverseDecl DataverseDeclaration() throws ParseException:
243{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800244 Identifier dvName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000245}
246{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000247 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000248 ";"
249 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000250 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000251 }
252}
253
zheilbron28e026f2013-11-20 10:15:15 -0800254LoadStatement LoadStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000255{
256 Identifier datasetName = null;
257 boolean alreadySorted = false;
258 String adapter;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800259 Map<String,String> properties = new HashMap<String,String>();
vinayakb38b7ca42012-03-05 05:44:15 +0000260 String name;
261 String value;
262}
263{
264 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800265
266 "using"
vinayakb38b7ca42012-03-05 05:44:15 +0000267 (
268 <STRING_LITERAL>
269 {
270 adapter = removeQuotesAndEscapes(token.image);
271 }
272 <LEFTPAREN>
273 (
274 (
275 <LEFTPAREN>
276 (
277 <STRING_LITERAL>
278 {
279 name = removeQuotesAndEscapes(token.image);
280 }
281 "=" <STRING_LITERAL>
282 {
283 value = removeQuotesAndEscapes(token.image);
284 }
285 )
286 <RIGHTPAREN>
287 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800288 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000289 }
290 )
291 (
292 "," <LEFTPAREN>
293 (
294 <STRING_LITERAL>
295 {
296 name = removeQuotesAndEscapes(token.image);
297 }
298 "=" <STRING_LITERAL>
299 {
300 value = removeQuotesAndEscapes(token.image);
301 }
302 )
303 <RIGHTPAREN>
304 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800305 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000306 }
307 )*
308 )?
309 <RIGHTPAREN>
310 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800311
312 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000313 { alreadySorted = true; }
314 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800315
vinayakb38b7ca42012-03-05 05:44:15 +0000316 ";"
317 {
zheilbron28e026f2013-11-20 10:15:15 -0800318 return new LoadStatement(null, datasetName, adapter, properties, alreadySorted);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800319 }
vinayakb38b7ca42012-03-05 05:44:15 +0000320}
321
322TypeDecl TypeDeclaration() throws ParseException:
323{
324 Identifier ident;
325 TypeExpression typeExpr;
326}
327{
328 <IDENTIFIER>
329 {
330 ident = new Identifier(token.image.toString());
331 }
332 "as"
333 ( typeExpr = TypeExpr() )
334 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000335 return new TypeDecl(null, ident, typeExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000336 }
337}
338
339TypeExpression TypeExpr() throws ParseException:
340{
341 TypeExpression typeExpr = null;
342}
343{
344 (
345 typeExpr = RecordTypeDef()
346 | typeExpr = TypeReference()
347 | typeExpr = OrderedListTypeDef()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800348 | typeExpr = UnorderedListTypeDef()
349 )
vinayakb38b7ca42012-03-05 05:44:15 +0000350 {
351 return typeExpr;
352 }
353}
354
355RecordTypeDefinition RecordTypeDef() throws ParseException:
356{
357 RecordTypeDefinition recType = new RecordTypeDefinition();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800358 RecordTypeDefinition.RecordKind recordKind = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000359}
360{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800361 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
vinayakb38b7ca42012-03-05 05:44:15 +0000362 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
363 "{"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800364 (
365 RecordField(recType)
366 ( "," RecordField(recType) )*
367 )?
vinayakb38b7ca42012-03-05 05:44:15 +0000368 "}"
369 {
370 if (recordKind == null) {
371 recordKind = RecordTypeDefinition.RecordKind.OPEN;
372 }
373 recType.setRecordKind(recordKind);
374 return recType;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800375 }
vinayakb38b7ca42012-03-05 05:44:15 +0000376}
377
378void RecordField(RecordTypeDefinition recType) throws ParseException:
379{
380 String fieldName;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800381 TypeExpression type = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000382 boolean nullable = false;
383}
384{
385 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800386 {
387 Token t = getToken(0);
388 fieldName = t.toString();
389 }
390 ":"
391 ( type = TypeExpr() )
392 ("?" { nullable = true; } )?
393 {
394
395 recType.addField(fieldName, type, nullable);
396 }
vinayakb38b7ca42012-03-05 05:44:15 +0000397}
398
399TypeReferenceExpression TypeReference() throws ParseException:
400{}
401{
402 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800403 {
404 Token t = getToken(0);
405 Identifier id;
406 if (t.toString().equalsIgnoreCase("int")) {
407 id = new Identifier("int64");
408 } else {
409 id = new Identifier(t.toString());
410 }
411 return new TypeReferenceExpression(id);
412 }
vinayakb38b7ca42012-03-05 05:44:15 +0000413}
414
415OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800416{
vinayakb38b7ca42012-03-05 05:44:15 +0000417 TypeExpression type = null;
418}
419{
420 "["
421 ( type = TypeExpr() )
422 "]"
423 {
424 return new OrderedListTypeDefinition(type);
425 }
426}
427
428
429UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800430{
vinayakb38b7ca42012-03-05 05:44:15 +0000431 TypeExpression type = null;
432}
433{
434 "<"
435 ( type = TypeExpr() )
436 ">"
437 {
438 return new UnorderedListTypeDefinition(type);
439 }
440}
441
442
443FunctionDecl FunctionDeclaration() throws ParseException:
444{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000445 FunctionDecl funcDecl;
446 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +0000447 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +0000448 int arity = 0;
449 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
450 Expression funcBody;
451 VarIdentifier var = null;
452 createNewScope();
453}
454{
455
456 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800457 {
458 Token t = getToken(0);
459 functionName = t.toString();
460 if (functionName.equalsIgnoreCase("int")) {
461 functionName = "int64";
462 }
463 }
vinayakb38b7ca42012-03-05 05:44:15 +0000464 <LEFTPAREN> (<VARIABLE>
465 {
466 var = new VarIdentifier();
467 var.setValue(getToken(0).toString());
468 paramList.add(var);
469 getCurrentScope().addNewVarSymbolToScope(var);
470 arity++;
471 }
472 ("," <VARIABLE>
473 {
474 var = new VarIdentifier();
475 var.setValue(getToken(0).toString());
476 paramList.add(var);
477 getCurrentScope().addNewVarSymbolToScope(var);
478 arity++;
479 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
480
481 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000482 signature = new FunctionSignature(defaultDataverse, functionName, arity);
483 getCurrentScope().addFunctionDescriptor(signature, false);
484 funcDecl = new FunctionDecl(signature, paramList, funcBody);
485 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +0000486 }
487}
488
489Query Query()throws ParseException:
490{
491 Query query = new Query();
492 Expression expr;
493}
494{
495 expr = Expression()
496
497 {
498 query.setBody(expr);
499 return query;
500 }
501}
502
503
504
505Expression Expression():
506{
507 Expression expr = null;
508 Expression exprP = null;
509}
510{
511(
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800512
vinayakb38b7ca42012-03-05 05:44:15 +0000513//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
514 expr = OperatorExpr()
515 | expr = IfThenElse()
516 | expr = FLWOGR()
517 | expr = QuantifiedExpression()
518
519
520)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800521 {
522 return (exprP==null) ? expr : exprP;
523 }
vinayakb38b7ca42012-03-05 05:44:15 +0000524}
525
526
527
528Expression OperatorExpr()throws ParseException:
529{
530 OperatorExpr op = null;
531 Expression operand = null;
532}
533{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800534 operand = AndExpr()
535 (
536
537 "or"
538 {
539 if (op == null) {
540 op = new OperatorExpr();
541 op.addOperand(operand);
542 op.setCurrentop(true);
543 }
544 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000545 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800546 }
vinayakb38b7ca42012-03-05 05:44:15 +0000547
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800548 operand = AndExpr()
549 {
550 op.addOperand(operand);
551 }
vinayakb38b7ca42012-03-05 05:44:15 +0000552
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800553 )*
554
555 {
556 return op==null? operand: op;
557 }
vinayakb38b7ca42012-03-05 05:44:15 +0000558}
559
560Expression AndExpr()throws ParseException:
561{
562 OperatorExpr op = null;
563 Expression operand = null;
564}
565{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800566 operand = RelExpr()
567 (
568
569 "and"
570 {
571 if (op == null) {
572 op = new OperatorExpr();
573 op.addOperand(operand);
574 op.setCurrentop(true);
575 }
576 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000577 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800578 }
vinayakb38b7ca42012-03-05 05:44:15 +0000579
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800580 operand = RelExpr()
581 {
582 op.addOperand(operand);
583 }
vinayakb38b7ca42012-03-05 05:44:15 +0000584
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800585 )*
586
587 {
588 return op==null? operand: op;
589 }
vinayakb38b7ca42012-03-05 05:44:15 +0000590}
591
592
593
594Expression RelExpr()throws ParseException:
595{
596 OperatorExpr op = null;
597 Expression operand = null;
598 boolean broadcast = false;
599}
600{
601 operand = AddExpr()
602 {
603 if (operand instanceof VariableExpr) {
604 String hint = getHint(token);
605 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800606 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000607 }
608 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800609 }
vinayakb38b7ca42012-03-05 05:44:15 +0000610
611 (
612 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800613 {
614 if (op == null) {
615 op = new OperatorExpr();
616 op.addOperand(operand, broadcast);
vinayakb38b7ca42012-03-05 05:44:15 +0000617 op.setCurrentop(true);
618 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800619 }
620 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000621 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800622 }
623
624 operand = AddExpr()
625 {
vinayakb38b7ca42012-03-05 05:44:15 +0000626 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800627 if (operand instanceof VariableExpr) {
vinayakb38b7ca42012-03-05 05:44:15 +0000628 String hint = getHint(token);
629 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
630 broadcast = true;
631 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800632 }
vinayakb38b7ca42012-03-05 05:44:15 +0000633 op.addOperand(operand, broadcast);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800634 }
vinayakb38b7ca42012-03-05 05:44:15 +0000635 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800636
637 {
638 return op==null? operand: op;
639 }
vinayakb38b7ca42012-03-05 05:44:15 +0000640}
641
642Expression AddExpr()throws ParseException:
643{
644 OperatorExpr op = null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800645 Expression operand = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000646}
647{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800648 operand = MultExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000649
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800650 ( ("+" | "-")
651 {
652 if (op == null) {
653 op = new OperatorExpr();
654 op.addOperand(operand);
655 op.setCurrentop(true);
656 }
657 Token t = getToken(0);
658 ((OperatorExpr)op).addOperator(t.toString());
659 }
vinayakb38b7ca42012-03-05 05:44:15 +0000660
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800661 operand = MultExpr()
662 {
663 op.addOperand(operand);
664 }
665 )*
666
667 {
668 return op==null? operand: op;
669 }
vinayakb38b7ca42012-03-05 05:44:15 +0000670}
671
672Expression MultExpr()throws ParseException:
673{
674 OperatorExpr op = null;
675 Expression operand = null;
676}
677{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800678 operand = UnionExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000679
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800680 (( "*" | "/" | "%" | <CARET> | "idiv")
681 {
682 if (op == null) {
683 op = new OperatorExpr();
vinayakb38b7ca42012-03-05 05:44:15 +0000684 op.addOperand(operand);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800685 op.setCurrentop(true);
686 }
687 Token t = getToken(0);
688 op.addOperator(t.toString());
689 }
690 operand = UnionExpr()
691 {
692 op.addOperand(operand);
693 }
694 )*
695
696 {
697 return op==null?operand:op;
698 }
vinayakb38b7ca42012-03-05 05:44:15 +0000699}
700
701Expression UnionExpr() throws ParseException:
702{
703 UnionExpr union = null;
704 Expression operand1 = null;
705 Expression operand2 = null;
706}
707{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800708 operand1 = UnaryExpr()
709 ("union"
vinayakb38b7ca42012-03-05 05:44:15 +0000710 (operand2 = UnaryExpr()) {
711 if (union == null) {
712 union = new UnionExpr();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800713 union.addExpr(operand1);
vinayakb38b7ca42012-03-05 05:44:15 +0000714 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800715 union.addExpr(operand2);
vinayakb38b7ca42012-03-05 05:44:15 +0000716 } )*
717 {
718 return (union == null)? operand1: union;
719 }
720}
721
722Expression UnaryExpr() throws ParseException:
723{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800724 Expression uexpr = null;
725 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000726}
727{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800728 (( "+"|"-")
729 {
730 uexpr = new UnaryExpr();
731 Token t = getToken(0);
732 if("+".equals(t.toString()))
733 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
734 else if("-".equals(t.toString()))
735 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
736 else
737 throw new ParseException();
738 }
739 )?
740
741 expr = ValueExpr()
742 {
743 if(uexpr!=null){
744 ((UnaryExpr)uexpr).setExpr(expr);
745 return uexpr;
746 }
747 else{
748 return expr;
749 }
750 }
vinayakb38b7ca42012-03-05 05:44:15 +0000751}
752
753Expression ValueExpr() throws ParseException:
754{
755 Expression expr;
756}
757{
758 expr = FieldOrIndexAccessor()
759 {
760 return expr;
761 }
762}
763
764
765Expression FieldOrIndexAccessor()throws ParseException:
766{
767 Expression expr = null;
768 Identifier ident = null;
769 AbstractAccessor fa = null;
icetindila611ac72014-05-16 10:10:11 -0700770 Expression indexExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000771
772}
773{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800774 ( expr = PrimaryExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000775
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800776 )
vinayakb38b7ca42012-03-05 05:44:15 +0000777
778
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800779 (
780 (
781 ident = Field()
782 {
783 if(fa == null)
784 fa = new FieldAccessor(expr, ident);
785 else
786 fa = new FieldAccessor(fa, ident);
787 }
788 )
789 | (
790 indexExpr = Index()
791 {
792 if(fa == null)
793 fa = new IndexAccessor(expr, indexExpr);
794 else
795 fa = new IndexAccessor(fa, indexExpr);
796 }
797 )
798 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000799
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800800
801 {
802 return fa==null?expr:fa;
803 }
vinayakb38b7ca42012-03-05 05:44:15 +0000804}
805
806Identifier Field() throws ParseException:
807{
808 Identifier ident = null;
809
810}
811{
812 "." < IDENTIFIER >
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800813 {
vinayakb38b7ca42012-03-05 05:44:15 +0000814
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800815 ident = new Identifier();
816 ident.setValue(getToken(0).toString());
817
818 return ident;
819 }
vinayakb38b7ca42012-03-05 05:44:15 +0000820}
821
icetindila611ac72014-05-16 10:10:11 -0700822Expression Index() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000823{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800824 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000825}
826{
827 "[" ( expr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800828 {
829 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
830 {
831 Literal lit = ((LiteralExpr)expr).getValue();
832 if(lit.getLiteralType() != Literal.Type.INTEGER &&
833 lit.getLiteralType() != Literal.Type.LONG) {
834 throw new ParseException("Index should be an INTEGER");
vinayakb38b7ca42012-03-05 05:44:15 +0000835 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800836 }
837 }
vinayakb38b7ca42012-03-05 05:44:15 +0000838
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800839 | "?" // ANY
840
841 )
vinayakb38b7ca42012-03-05 05:44:15 +0000842
843 "]"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800844 {
845 return expr;
846 }
vinayakb38b7ca42012-03-05 05:44:15 +0000847}
848
849
850Expression PrimaryExpr()throws ParseException:
851{
852 Expression expr = null;
853}
854{
ilovesoupc9fef1d2012-07-08 19:30:42 +0000855 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800856 (
857 expr =Literal()
858 | expr = FunctionCallExpr()
859 | expr =VariableRef()
860
vinayakb38b7ca42012-03-05 05:44:15 +0000861 {
862 if(((VariableExpr)expr).getIsNewVar() == true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800863 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +0000864 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800865 | expr = ListConstructor()
866 | expr = RecordConstructor()
867 | expr = ParenthesizedExpression()
868 | expr = MetaVariableRef()
869 )
870 {
871 return expr;
872 }
vinayakb38b7ca42012-03-05 05:44:15 +0000873}
874
875Expression Literal() throws ParseException:
876{
877
878 LiteralExpr lit = new LiteralExpr();
879 Token t;
880}
881{
882(
883 <STRING_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +0000884 {
885 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800886 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
887 }
888
889 | <INTEGER_LITERAL>
890 {
891 t= getToken(0);
892 try {
893 lit.setValue(new IntegerLiteral(new Integer(t.image)));
894 } catch(NumberFormatException ex) {
895 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
896 }
897 }
vinayakb38b7ca42012-03-05 05:44:15 +0000898 | < FLOAT_LITERAL >
899 {
900 t= getToken(0);
901 lit.setValue(new FloatLiteral(new Float(t.image)));
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800902 }
903 | < DOUBLE_LITERAL >
vinayakb38b7ca42012-03-05 05:44:15 +0000904 {
905 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800906 lit.setValue(new DoubleLiteral(new Double(t.image)));
907 }
908 | <NULL>
909 {
910 t= getToken(0);
911 lit.setValue(NullLiteral.INSTANCE);
912 }
913 | <TRUE>
914 {
915 t= getToken(0);
916 lit.setValue(TrueLiteral.INSTANCE);
917 }
918 | <FALSE>
919 {
920 t= getToken(0);
921 lit.setValue(FalseLiteral.INSTANCE);
922 }
vinayakb38b7ca42012-03-05 05:44:15 +0000923)
924 {
925 return lit;
926 }
927}
928
929
930VariableExpr VariableRef() throws ParseException:
931{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800932 VariableExpr varExp = new VariableExpr();
933 VarIdentifier var = new VarIdentifier();
934 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000935}
936{
937 <VARIABLE>
938 {
939 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800940 String varName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +0000941 Identifier ident = lookupSymbol(varName);
942 if (isInForbiddenScopes(varName)) {
943 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.");
944 }
945 if(ident != null) { // exist such ident
946 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800947 varExp.setVar((VarIdentifier)ident);
vinayakb38b7ca42012-03-05 05:44:15 +0000948 } else {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800949 varExp.setVar(var);
vinayakb38b7ca42012-03-05 05:44:15 +0000950 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800951 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000952 return varExp;
953 }
954}
955
956
957VariableExpr Variable() throws ParseException:
958{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800959 VariableExpr varExp = new VariableExpr();
960 VarIdentifier var = new VarIdentifier();
961 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000962}
963{
964 <VARIABLE>
965 {
966 t = getToken(0);//get current token
967 Identifier ident = lookupSymbol(t.toString());
968 if(ident != null) { // exist such ident
969 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800970 }
971 varExp.setVar(var);
972 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000973 return varExp;
974 }
975}
976
977MetaVariableExpr MetaVariableRef() throws ParseException:
978{
979 MetaVariableExpr metaVarExp = new MetaVariableExpr();
980 VarIdentifier var = new VarIdentifier();
981 Token t;
982}
983{
984 <METAVARIABLE>
985 {
986 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800987 metaVarExp.setVar(var);
988 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000989 return metaVarExp;
990 }
991}
992
993Expression ListConstructor() throws ParseException:
994{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800995 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000996}
997{
998 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800999 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
vinayakb38b7ca42012-03-05 05:44:15 +00001000 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001001
vinayakb38b7ca42012-03-05 05:44:15 +00001002 {
1003 return expr;
1004 }
1005}
1006
1007
1008ListConstructor OrderedListConstructor() throws ParseException:
1009{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001010 ListConstructor expr = new ListConstructor();
1011 Expression tmp = null;
1012 List<Expression> exprList = new ArrayList<Expression>();
1013 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +00001014}
1015{
1016
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001017 "["
1018 ( tmp = Expression()
1019 {
1020 exprList.add(tmp);
1021 }
1022
1023 ("," tmp = Expression() { exprList.add(tmp); })*
1024 )?
1025
vinayakb38b7ca42012-03-05 05:44:15 +00001026 "]"
1027
1028 {
1029 expr.setExprList(exprList);
1030 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001031 }
vinayakb38b7ca42012-03-05 05:44:15 +00001032}
1033
1034ListConstructor UnorderedListConstructor() throws ParseException:
1035{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001036 ListConstructor expr = new ListConstructor();
1037 Expression tmp = null;
1038 List<Expression> exprList = new ArrayList<Expression>();
1039 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +00001040}
1041{
1042
1043 "{{" ( tmp = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001044 {
1045 exprList.add(tmp);
1046 }
vinayakb38b7ca42012-03-05 05:44:15 +00001047 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1048 {
1049 expr.setExprList(exprList);
1050 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001051 }
vinayakb38b7ca42012-03-05 05:44:15 +00001052}
1053
1054RecordConstructor RecordConstructor() throws ParseException:
1055{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001056 RecordConstructor expr = new RecordConstructor();
1057 FieldBinding tmp = null;
1058 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
vinayakb38b7ca42012-03-05 05:44:15 +00001059}
1060{
1061 "{" (tmp = FieldBinding()
1062 {
1063 fbList.add(tmp);
1064 }
1065 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1066 {
1067 expr.setFbList(fbList);
1068 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001069 }
vinayakb38b7ca42012-03-05 05:44:15 +00001070}
1071
1072FieldBinding FieldBinding() throws ParseException:
1073{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001074 FieldBinding fb = new FieldBinding();
1075 Expression left, right;
vinayakb38b7ca42012-03-05 05:44:15 +00001076}
1077{
1078 left = Expression() ":" right = Expression()
1079 {
1080 fb.setLeftExpr(left);
1081 fb.setRightExpr(right);
1082 return fb;
1083 }
1084}
1085
1086Expression FunctionCallExpr() throws ParseException:
1087{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001088 CallExpr callExpr;
1089 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001090 Expression tmp;
1091 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001092 String funcName;
1093 String dataverse;
1094 String hint=null;
1095 String id1=null;
1096 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001097}
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001098{
1099 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;}
1100 ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})?
1101 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001102 <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();}
1103 )
vinayakb38b7ca42012-03-05 05:44:15 +00001104 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001105 hint=getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001106 }
1107 <LEFTPAREN> (tmp = Expression()
1108 {
1109 argList.add(tmp);
1110 arity ++;
1111 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1112
1113 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001114 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1115 if(signature == null)
1116 {
1117 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1118 }
1119 callExpr = new CallExpr(signature,argList);
1120 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1121 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1122 }
1123 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001124 }
1125}
1126
1127
1128Expression ParenthesizedExpression() throws ParseException:
1129{
1130 Expression expr;
1131}
1132{
1133 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1134 {
1135 return expr;
1136 }
1137}
1138
1139Expression IfThenElse() throws ParseException:
1140{
1141 Expression condExpr;
1142 Expression thenExpr;
1143 Expression elseExpr;
1144 IfExpr ifExpr = new IfExpr();
1145}
1146{
1147 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1148
1149 {
1150 ifExpr.setCondExpr(condExpr);
1151 ifExpr.setThenExpr(thenExpr);
1152 ifExpr.setElseExpr(elseExpr);
1153 return ifExpr;
1154 }
1155}
1156
1157Expression FLWOGR() throws ParseException:
1158{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001159 FLWOGRExpression flworg = new FLWOGRExpression();
1160 List<Clause> clauseList = new ArrayList<Clause>();
1161 Expression returnExpr;
1162 Clause tmp;
1163 createNewScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001164}
1165{
1166 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);})
1167 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1168
1169 {
1170 flworg.setClauseList(clauseList);
1171 flworg.setReturnExpr(returnExpr);
1172 removeCurrentScope();
1173 return flworg;
1174 }
1175}
1176
1177List<Clause> Clauses() throws ParseException:
1178{
1179 List<Clause> clauses = new ArrayList<Clause>();
1180 Clause c = null;
1181}
1182{
1183 (
1184 (
1185 c = Clause() {
1186 clauses.add(c);
1187 }
1188 )*
1189 )
1190 {
1191 return clauses;
1192 }
1193}
1194
1195Clause Clause() throws ParseException :
1196{
1197 Clause clause;
1198}
1199{
1200 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001201 clause = ForClause()
1202 | clause = LetClause()
1203 | clause = WhereClause()
1204 | clause = OrderbyClause()
1205 | clause = GroupClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001206 | clause = LimitClause()
1207 | clause = DistinctClause()
1208 | clause = MetaVariableClause()
1209 | clause = JoinClause()
1210 )
1211 {
1212 return clause;
1213 }
1214}
1215
1216Clause MetaVariableClause() throws ParseException :
1217{
1218 MetaVariableClause mc = new MetaVariableClause();
1219 VarIdentifier var = new VarIdentifier();
1220 Token t;
1221}
1222{
1223 <METAVARIABLECLAUSE>
1224 {
1225 t = getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001226 mc.setVar(var);
1227 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +00001228 return mc;
1229 }
1230}
1231
1232Clause JoinClause() throws ParseException :
1233{
1234 Expression whereExpr;
1235 List<Clause> leftClauses, rightClauses;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001236 JoinClause.JoinKind kind = JoinClause.JoinKind.INNER;
vinayakb38b7ca42012-03-05 05:44:15 +00001237}
1238{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001239 ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } )
1240 <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> ","
1241 <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> ","
vinayakb38b7ca42012-03-05 05:44:15 +00001242 whereExpr = Expression() <RIGHTPAREN>
1243 {
1244 JoinClause jc = new JoinClause(kind);
1245 jc.setLeftClauses(leftClauses);
1246 jc.setRightClauses(rightClauses);
1247 jc.setWhereExpr(whereExpr);
1248 return jc;
1249 }
1250}
1251
1252Clause ForClause()throws ParseException :
1253{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001254 ForClause fc = new ForClause();
1255 VariableExpr varExp;
1256 VariableExpr varPos = null;
1257 Expression inExp;
1258 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001259}
1260{
1261 "for" varExp = Variable()
1262 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001263 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1264 }
1265 ("at" varPos = Variable()
1266 {
1267 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1268 }
1269 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001270 "in" ( inExp = Expression() )
1271 {
1272 fc.setVarExpr(varExp);
1273 fc.setInExpr(inExp);
1274 if (varPos != null) {
1275 fc.setPosExpr(varPos);
1276 }
1277 return fc;
1278 }
1279}
1280
1281Clause LetClause() throws ParseException:
1282{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001283 LetClause lc = new LetClause();
1284 VariableExpr varExp;
1285 Expression beExp;
1286 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001287}
1288{
ilovesoupb2527c12012-07-12 03:21:13 +00001289 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001290 {
1291 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001292 lc.setVarExpr(varExp);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001293 lc.setBindingExpr(beExp);
vinayakb38b7ca42012-03-05 05:44:15 +00001294 return lc;
1295 }
1296}
1297
1298Clause WhereClause()throws ParseException :
1299{
1300 WhereClause wc = new WhereClause();
1301 Expression whereExpr;
1302}
1303{
1304 "where" whereExpr = Expression()
1305 {
1306 wc.setWhereExpr(whereExpr);
1307 return wc;
1308 }
1309}
1310
1311Clause OrderbyClause()throws ParseException :
1312{
1313 OrderbyClause oc = new OrderbyClause();
1314 Expression orderbyExpr;
1315 List<Expression> orderbyList = new ArrayList<Expression>();
1316 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1317 int numOfOrderby = 0;
1318}
1319{
1320 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001321 "order"
vinayakb38b7ca42012-03-05 05:44:15 +00001322 {
1323 String hint = getHint(token);
1324 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001325 String splits[] = hint.split(" +");
vinayakb38b7ca42012-03-05 05:44:15 +00001326 int numFrames = Integer.parseInt(splits[1]);
1327 int numTuples = Integer.parseInt(splits[2]);
1328 oc.setNumFrames(numFrames);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001329 oc.setNumTuples(numTuples);
1330 }
1331 }
vinayakb38b7ca42012-03-05 05:44:15 +00001332 "by" orderbyExpr = Expression()
1333 {
1334 orderbyList.add(orderbyExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001335 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
vinayakb38b7ca42012-03-05 05:44:15 +00001336 }
1337 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1338 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1339 {
1340 modifierList.add(modif);
1341 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001342
vinayakb38b7ca42012-03-05 05:44:15 +00001343 ("," orderbyExpr = Expression()
1344 {
1345 orderbyList.add(orderbyExpr);
1346 modif = OrderbyClause.OrderModifier.ASC;
1347 }
1348 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1349 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1350 {
1351 modifierList.add(modif);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001352 }
vinayakb38b7ca42012-03-05 05:44:15 +00001353 )*
1354)
1355 {
1356 oc.setModifierList(modifierList);
1357 oc.setOrderbyList(orderbyList);
1358 return oc;
1359 }
1360}
1361Clause GroupClause()throws ParseException :
1362{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001363 GroupbyClause gbc = new GroupbyClause();
1364 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1365 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
vinayakb38b7ca42012-03-05 05:44:15 +00001366 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001367 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1368 VariableExpr var = null;
1369 VariableExpr withVar = null;
1370 Expression expr = null;
1371 VariableExpr decorVar = null;
1372 Expression decorExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001373}
1374{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001375 {
1376 Scope newScope = extendCurrentScopeNoPush(true);
1377 // extendCurrentScope(true);
1378 }
vinayakb38b7ca42012-03-05 05:44:15 +00001379 "group"
1380 {
1381 String hint = getHint(token);
1382 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001383 gbc.setHashGroupByHint(true);
1384 }
1385 }
vinayakb38b7ca42012-03-05 05:44:15 +00001386 "by" (LOOKAHEAD(2) var = Variable()
1387 {
1388 newScope.addNewVarSymbolToScope(var.getVar());
1389 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001390 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001391 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001392 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001393 vePairList.add(pair1);
1394 }
1395 ("," ( LOOKAHEAD(2) var = Variable()
1396 {
1397 newScope.addNewVarSymbolToScope(var.getVar());
1398 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001399 expr = Expression()
1400 {
1401 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001402 vePairList.add(pair2);
1403 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001404 )*
vinayakb38b7ca42012-03-05 05:44:15 +00001405 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001406 {
1407 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001408 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1409 decorPairList.add(pair3);
1410 }
1411 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001412 {
1413 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001414 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001415 decorPairList.add(pair4);
vinayakb38b7ca42012-03-05 05:44:15 +00001416 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001417 )*
1418 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001419 "with" withVar = VariableRef()
1420 {
1421 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001422 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001423 withVarList.add(withVar);
1424 newScope.addNewVarSymbolToScope(withVar.getVar());
1425 }
1426 ("," withVar = VariableRef()
1427 {
1428 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001429 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001430 withVarList.add(withVar);
1431 newScope.addNewVarSymbolToScope(withVar.getVar());
1432 })*
1433 {
1434 gbc.setGbyPairList(vePairList);
1435 gbc.setDecorPairList(decorPairList);
1436 gbc.setWithVarList(withVarList);
1437 replaceCurrentScope(newScope);
1438 return gbc;
1439 }
1440}
1441
1442
1443LimitClause LimitClause() throws ParseException:
1444{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001445 LimitClause lc = new LimitClause();
1446 Expression expr;
1447 pushForbiddenScope(getCurrentScope());
vinayakb38b7ca42012-03-05 05:44:15 +00001448}
1449{
1450 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1451 ("offset" expr = Expression() { lc.setOffset(expr); })?
1452
1453 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001454 popForbiddenScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001455 return lc;
1456 }
1457}
1458
1459DistinctClause DistinctClause() throws ParseException:
1460{
1461 List<Expression> exprs = new ArrayList<Expression>();
1462 Expression expr;
1463}
1464{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001465 "distinct" "by" expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001466 {
1467 exprs.add(expr);
1468 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001469 ("," expr = Expression()
1470 {
1471 exprs.add(expr);
1472 }
vinayakb38b7ca42012-03-05 05:44:15 +00001473 )*
1474 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001475 return new DistinctClause(exprs);
vinayakb38b7ca42012-03-05 05:44:15 +00001476 }
1477}
1478
1479
1480QuantifiedExpression QuantifiedExpression()throws ParseException:
1481{
1482 QuantifiedExpression qc = new QuantifiedExpression();
1483 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
1484 Expression satisfiesExpr;
1485 VariableExpr var;
1486 Expression inExpr;
1487 QuantifiedPair pair;
1488}
1489{
1490 {
1491 createNewScope();
1492 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001493
vinayakb38b7ca42012-03-05 05:44:15 +00001494 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001495 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
1496 var = Variable() "in" inExpr = Expression()
1497 {
vinayakb38b7ca42012-03-05 05:44:15 +00001498 pair = new QuantifiedPair(var, inExpr);
1499 getCurrentScope().addNewVarSymbolToScope(var.getVar());
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001500 quantifiedList.add(pair);
1501 }
1502 (
1503 "," var = Variable() "in" inExpr = Expression()
1504 {
1505 pair = new QuantifiedPair(var, inExpr);
1506 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1507 quantifiedList.add(pair);
1508 }
1509 )*
1510 "satisfies" satisfiesExpr = Expression()
1511 {
1512 qc.setSatisfiesExpr(satisfiesExpr);
1513 qc.setQuantifiedList(quantifiedList);
1514 removeCurrentScope();
1515 return qc;
1516 }
vinayakb38b7ca42012-03-05 05:44:15 +00001517}
1518
1519TOKEN_MGR_DECLS:
1520{
1521 public int commentDepth = 0;
1522}
1523
1524<DEFAULT>
1525TOKEN :
1526{
1527 <CARET : "^" >
1528}
1529
1530<DEFAULT>
1531TOKEN :
1532{
1533 <DATASET : "dataset" >
1534}
1535
1536<DEFAULT>
1537TOKEN :
1538{
1539 <LEFTPAREN : "(" >
1540}
1541
1542<DEFAULT>
1543TOKEN :
1544{
1545 <RIGHTPAREN : ")" >
1546}
1547
1548
1549<DEFAULT>
1550TOKEN :
1551{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001552 <INTEGER_LITERAL : (<DIGIT>)+ >
vinayakb38b7ca42012-03-05 05:44:15 +00001553}
1554
1555
1556<DEFAULT>
1557TOKEN :
1558{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001559 <NULL : "null">
vinayakb38b7ca42012-03-05 05:44:15 +00001560}
1561
1562<DEFAULT>
1563TOKEN :
1564{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001565 <TRUE : "true">
vinayakb38b7ca42012-03-05 05:44:15 +00001566}
1567
1568<DEFAULT>
1569TOKEN :
1570{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001571 <FALSE : "false">
vinayakb38b7ca42012-03-05 05:44:15 +00001572}
1573
1574<DEFAULT>
1575TOKEN :
1576{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001577 <#DIGIT : ["0" - "9"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001578}
1579
1580
1581TOKEN:
1582{
1583 < DOUBLE_LITERAL: <INTEGER>
1584 | <INTEGER> ( "." <INTEGER> )?
1585 | "." <INTEGER>
1586 >
1587 |
1588 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
1589 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
1590 | "." <INTEGER> ( "f" | "F" )
1591 >
1592 |
1593 <INTEGER : (<DIGIT>)+ >
1594}
1595
1596<DEFAULT>
1597TOKEN :
1598{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001599 <#LETTER : ["A" - "Z", "a" - "z"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001600}
1601
1602<DEFAULT>
1603TOKEN :
1604{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001605 <SPECIALCHARS : ["$", "_", "-"] >
vinayakb38b7ca42012-03-05 05:44:15 +00001606}
1607
1608<DEFAULT>
1609TOKEN :
1610{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001611 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
1612 |
1613 < #EscapeQuot: "\\\"" >
1614 |
vinayakb38b7ca42012-03-05 05:44:15 +00001615 < #EscapeApos: "\\\'" >
1616}
1617
1618<DEFAULT>
1619TOKEN :
1620{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001621 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
vinayakb38b7ca42012-03-05 05:44:15 +00001622}
1623
1624<DEFAULT>
1625TOKEN :
1626{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001627 <VARIABLE : "$" <IDENTIFIER> >
vinayakb38b7ca42012-03-05 05:44:15 +00001628}
1629
1630<DEFAULT>
1631TOKEN :
1632{
1633 <METAVARIABLECLAUSE : "#" <IDENTIFIER> >
1634}
1635
1636<DEFAULT>
1637TOKEN :
1638{
1639 <METAVARIABLE : "$$" <IDENTIFIER> >
1640}
1641
1642SKIP:
1643{
1644 " "
1645| "\t"
1646| "\r"
1647| "\n"
1648}
1649
1650SKIP:
1651{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001652 <"//" (~["\n"])* "\n">
vinayakb38b7ca42012-03-05 05:44:15 +00001653}
1654
1655SKIP:
1656{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001657 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
vinayakb38b7ca42012-03-05 05:44:15 +00001658}
1659
1660
1661SKIP:
1662{
1663 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
1664}
1665
1666<INSIDE_COMMENT>
1667SPECIAL_TOKEN:
1668{
1669 <"+"(" ")*(~["/","*"])*>
1670}
1671
1672<INSIDE_COMMENT>
1673SKIP:
1674{
1675 <"/*"> {commentDepth++;}
1676}
1677
1678<INSIDE_COMMENT>
1679SKIP:
1680{
1681 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
1682| <~[]>
1683}