blob: 5b97d04dc13bffcb92dffa09b34f37efaa675c80 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLPlusParser)
10
11package edu.uci.ics.asterix.aqlplus.parser;
12
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17import java.util.Map;
18import java.util.HashMap;
19
20import edu.uci.ics.asterix.aql.literal.FloatLiteral;
21import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
22import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000023import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000024import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000025import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000026import edu.uci.ics.asterix.aql.literal.NullLiteral;
27import edu.uci.ics.asterix.aql.literal.StringLiteral;
28import edu.uci.ics.asterix.aql.literal.TrueLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000029import edu.uci.ics.asterix.aql.parser.ScopeChecker;
30import edu.uci.ics.asterix.aql.base.*;
31import edu.uci.ics.asterix.aql.expression.*;
32import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
33import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
34import edu.uci.ics.asterix.aql.base.Statement.Kind;
35import edu.uci.ics.asterix.aql.context.Scope;
36import edu.uci.ics.asterix.aql.context.RootScopeFactory;
37import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000038import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000039import edu.uci.ics.asterix.common.functions.FunctionSignature;
40import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
41
42import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
43import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
44import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
45
46
ramangrover29a13f2422012-03-15 23:01:27 +000047
vinayakb38b7ca42012-03-05 05:44:15 +000048public class AQLPlusParser extends ScopeChecker {
49
50/*
51 private void printHints(Token t) {
52 //System.err.println("token="+t.image+"\t special="+t.specialToken);
53 if (t.specialToken == null) return;
54 Token tmp_t = t.specialToken;
55 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
56 while (tmp_t != null) {
57 System.out.println(tmp_t.image);
58 tmp_t = tmp_t.next;
59 }
60 }
61*/
62
63 private static final String HASH_GROUP_BY_HINT = "hash";
64 private static final String BROADCAST_JOIN_HINT = "bcast";
65 private static final String INMEMORY_HINT = "inmem";
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000066 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
67
vinayakb38b7ca42012-03-05 05:44:15 +000068
69
70 private static String getHint(Token t) {
71 if (t.specialToken == null) {
72 return null;
73 }
74 String s = t.specialToken.image;
75 int n = s.length();
76 if (n < 2) {
77 return null;
78 }
79 return s.substring(1).trim();
80 }
81
82 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
83 File file = new File(args[0]);
84 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
85 AQLPlusParser parser = new AQLPlusParser(fis);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000086 List<Statement> st = parser.Statement();
vinayakb38b7ca42012-03-05 05:44:15 +000087 }
88
89 public void initScope() {
90 scopeStack.push(RootScopeFactory.createRootScope(this));
91 }
92}
93
94PARSER_END(AQLPlusParser)
95
96
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000097List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +000098{
99 Query query = null;
100 // scopeStack.push(RootScopeFactory.createRootScope(this));
101 initScope();
102 List<Statement> decls = new ArrayList<Statement>();
103}
104{
105 (
106 (
107 (
108 "use"
109 {
110 decls.add(DataverseDeclaration());
111 }
112 | "declare"
113 ( "function" {
114 decls.add(FunctionDeclaration());
115 }
116 | "type" {
117 decls.add(TypeDeclaration());
118 }
119 )
120 | "load" {
121 decls.add(LoadStatement());
122 }
123
124 | "write" {
125 decls.add(WriteStatement());
126 }
127 | "set" {
128 decls.add(SetStatement());
129 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000130 |
131 {
132 decls.add(Query()) ;
133 } ";"
vinayakb38b7ca42012-03-05 05:44:15 +0000134
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000135
vinayakb38b7ca42012-03-05 05:44:15 +0000136 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000137 )
138
139 <EOF>
140 )
141 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000142
143 return decls;
vinayakb38b7ca42012-03-05 05:44:15 +0000144 }
145}
146
147Statement SetStatement() throws ParseException:
148{
149 String pn = null;
150 Statement stmt = null;
151}
152{
153 <IDENTIFIER> { pn = token.image; }
154 <STRING_LITERAL>
155 { String pv = removeQuotesAndEscapes(token.image); }
156 ";"
157 {
158 return new SetStatement(pn, pv);
159 }
160}
161
162Statement WriteStatement() throws ParseException:
163{
164 Identifier nodeName = null;
165 String fileName = null;
166 Identifier datasetName = null;
167 Statement stmt = null;
168 Query query;
169}
170{
171 (( "output" "to"
172 <IDENTIFIER> { nodeName = new Identifier(token.image); }
173 ":" <STRING_LITERAL> {
174 fileName = removeQuotesAndEscapes(token.image);
175 stmt = new WriteStatement(nodeName, fileName, null);
176 } )
177 |
178 ( "into"
179 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
180 <LEFTPAREN> query = Query() <RIGHTPAREN>
181 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000182 stmt = new WriteFromQueryResultStatement(null, datasetName, query, getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +0000183 } ))
184
185 ";"
186 {
187 return stmt;
188 }
189}
190
191DataverseDecl DataverseDeclaration() throws ParseException:
192{
193 Identifier dvName = null;
194}
195{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000196 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000197 ";"
198 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000199 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000200 }
201}
202
203LoadFromFileStatement LoadStatement() throws ParseException:
204{
205 Identifier datasetName = null;
206 boolean alreadySorted = false;
207 String adapter;
208 Map<String,String> properties = new HashMap<String,String>();
209 String name;
210 String value;
211}
212{
213 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
214
215 "using"
216 (
217 <STRING_LITERAL>
218 {
219 adapter = removeQuotesAndEscapes(token.image);
220 }
221 <LEFTPAREN>
222 (
223 (
224 <LEFTPAREN>
225 (
226 <STRING_LITERAL>
227 {
228 name = removeQuotesAndEscapes(token.image);
229 }
230 "=" <STRING_LITERAL>
231 {
232 value = removeQuotesAndEscapes(token.image);
233 }
234 )
235 <RIGHTPAREN>
236 {
237 properties.put(name, value);
238 }
239 )
240 (
241 "," <LEFTPAREN>
242 (
243 <STRING_LITERAL>
244 {
245 name = removeQuotesAndEscapes(token.image);
246 }
247 "=" <STRING_LITERAL>
248 {
249 value = removeQuotesAndEscapes(token.image);
250 }
251 )
252 <RIGHTPAREN>
253 {
254 properties.put(name, value);
255 }
256 )*
257 )?
258 <RIGHTPAREN>
259 )
260
261 ("pre-sorted"
262 { alreadySorted = true; }
263 )?
264
265 ";"
266 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000267 return new LoadFromFileStatement(null, datasetName, adapter, properties, alreadySorted);
vinayakb38b7ca42012-03-05 05:44:15 +0000268 }
269}
270
271TypeDecl TypeDeclaration() throws ParseException:
272{
273 Identifier ident;
274 TypeExpression typeExpr;
275}
276{
277 <IDENTIFIER>
278 {
279 ident = new Identifier(token.image.toString());
280 }
281 "as"
282 ( typeExpr = TypeExpr() )
283 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000284 return new TypeDecl(null, ident, typeExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000285 }
286}
287
288TypeExpression TypeExpr() throws ParseException:
289{
290 TypeExpression typeExpr = null;
291}
292{
293 (
294 typeExpr = RecordTypeDef()
295 | typeExpr = TypeReference()
296 | typeExpr = OrderedListTypeDef()
297 | typeExpr = UnorderedListTypeDef()
298 )
299 {
300 return typeExpr;
301 }
302}
303
304RecordTypeDefinition RecordTypeDef() throws ParseException:
305{
306 RecordTypeDefinition recType = new RecordTypeDefinition();
307 RecordTypeDefinition.RecordKind recordKind = null;
308}
309{
310 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
311 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
312 "{"
313 (
314 RecordField(recType)
315 ( "," RecordField(recType) )*
316 )?
317 "}"
318 {
319 if (recordKind == null) {
320 recordKind = RecordTypeDefinition.RecordKind.OPEN;
321 }
322 recType.setRecordKind(recordKind);
323 return recType;
324 }
325}
326
327void RecordField(RecordTypeDefinition recType) throws ParseException:
328{
329 String fieldName;
330 TypeExpression type = null;
331 boolean nullable = false;
332}
333{
334 <IDENTIFIER>
335 {
336 Token t = getToken(0);
337 fieldName = t.toString();
338 }
339 ":"
340 ( type = TypeExpr() )
341 ("?" { nullable = true; } )?
342 {
343 recType.addField(fieldName, type, nullable);
344 }
345}
346
347TypeReferenceExpression TypeReference() throws ParseException:
348{}
349{
350 <IDENTIFIER>
351 {
352 Token t = getToken(0);
353 Identifier id = new Identifier(t.toString());
354 return new TypeReferenceExpression(id);
355 }
356}
357
358OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
359{
360 TypeExpression type = null;
361}
362{
363 "["
364 ( type = TypeExpr() )
365 "]"
366 {
367 return new OrderedListTypeDefinition(type);
368 }
369}
370
371
372UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
373{
374 TypeExpression type = null;
375}
376{
377 "<"
378 ( type = TypeExpr() )
379 ">"
380 {
381 return new UnorderedListTypeDefinition(type);
382 }
383}
384
385
386FunctionDecl FunctionDeclaration() throws ParseException:
387{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000388 FunctionDecl funcDecl;
389 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +0000390 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +0000391 int arity = 0;
392 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
393 Expression funcBody;
394 VarIdentifier var = null;
395 createNewScope();
396}
397{
398
399 <IDENTIFIER>
400 {
401 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +0000402 functionName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +0000403 }
404 <LEFTPAREN> (<VARIABLE>
405 {
406 var = new VarIdentifier();
407 var.setValue(getToken(0).toString());
408 paramList.add(var);
409 getCurrentScope().addNewVarSymbolToScope(var);
410 arity++;
411 }
412 ("," <VARIABLE>
413 {
414 var = new VarIdentifier();
415 var.setValue(getToken(0).toString());
416 paramList.add(var);
417 getCurrentScope().addNewVarSymbolToScope(var);
418 arity++;
419 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
420
421 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000422 signature = new FunctionSignature(defaultDataverse, functionName, arity);
423 getCurrentScope().addFunctionDescriptor(signature, false);
424 funcDecl = new FunctionDecl(signature, paramList, funcBody);
425 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +0000426 }
427}
428
429Query Query()throws ParseException:
430{
431 Query query = new Query();
432 Expression expr;
433}
434{
435 expr = Expression()
436
437 {
438 query.setBody(expr);
439 return query;
440 }
441}
442
443
444
445Expression Expression():
446{
447 Expression expr = null;
448 Expression exprP = null;
449}
450{
451(
452
453//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
454 expr = OperatorExpr()
455 | expr = IfThenElse()
456 | expr = FLWOGR()
457 | expr = QuantifiedExpression()
458
459
460)
461 {
462 return (exprP==null) ? expr : exprP;
463 }
464}
465
466
467
468Expression OperatorExpr()throws ParseException:
469{
470 OperatorExpr op = null;
471 Expression operand = null;
472}
473{
474 operand = AndExpr()
475 (
476
477 "or"
478 {
479 if (op == null) {
480 op = new OperatorExpr();
481 op.addOperand(operand);
482 op.setCurrentop(true);
483 }
484 Token t = getToken(0);
485 op.addOperator(t.toString());
486 }
487
488 operand = AndExpr()
489 {
490 op.addOperand(operand);
491 }
492
493 )*
494
495 {
496 return op==null? operand: op;
497 }
498}
499
500Expression AndExpr()throws ParseException:
501{
502 OperatorExpr op = null;
503 Expression operand = null;
504}
505{
506 operand = RelExpr()
507 (
508
509 "and"
510 {
511 if (op == null) {
512 op = new OperatorExpr();
513 op.addOperand(operand);
514 op.setCurrentop(true);
515 }
516 Token t = getToken(0);
517 op.addOperator(t.toString());
518 }
519
520 operand = RelExpr()
521 {
522 op.addOperand(operand);
523 }
524
525 )*
526
527 {
528 return op==null? operand: op;
529 }
530}
531
532
533
534Expression RelExpr()throws ParseException:
535{
536 OperatorExpr op = null;
537 Expression operand = null;
538 boolean broadcast = false;
539}
540{
541 operand = AddExpr()
542 {
543 if (operand instanceof VariableExpr) {
544 String hint = getHint(token);
545 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
546 broadcast = true;
547 }
548 }
549 }
550
551 (
552 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
553 {
554 if (op == null) {
555 op = new OperatorExpr();
556 op.addOperand(operand, broadcast);
557 op.setCurrentop(true);
558 broadcast = false;
559 }
560 Token t = getToken(0);
561 op.addOperator(t.toString());
562 }
563
564 operand = AddExpr()
565 {
566 broadcast = false;
567 if (operand instanceof VariableExpr) {
568 String hint = getHint(token);
569 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
570 broadcast = true;
571 }
572 }
573 op.addOperand(operand, broadcast);
574 }
575 )?
576
577 {
578 return op==null? operand: op;
579 }
580}
581
582Expression AddExpr()throws ParseException:
583{
584 OperatorExpr op = null;
585 Expression operand = null;
586}
587{
588 operand = MultExpr()
589
590 ( ("+" | "-")
591 {
592 if (op == null) {
593 op = new OperatorExpr();
594 op.addOperand(operand);
595 op.setCurrentop(true);
596 }
597 Token t = getToken(0);
598 ((OperatorExpr)op).addOperator(t.toString());
599 }
600
601 operand = MultExpr()
602 {
603 op.addOperand(operand);
604 }
605 )*
606
607 {
608 return op==null? operand: op;
609 }
610}
611
612Expression MultExpr()throws ParseException:
613{
614 OperatorExpr op = null;
615 Expression operand = null;
616}
617{
618 operand = UnionExpr()
619
620 (( "*" | "/" | "%" | <CARET> | "idiv")
621 {
622 if (op == null) {
623 op = new OperatorExpr();
624 op.addOperand(operand);
625 op.setCurrentop(true);
626 }
627 Token t = getToken(0);
628 op.addOperator(t.toString());
629 }
630 operand = UnionExpr()
631 {
632 op.addOperand(operand);
633 }
634 )*
635
636 {
637 return op==null?operand:op;
638 }
639}
640
641Expression UnionExpr() throws ParseException:
642{
643 UnionExpr union = null;
644 Expression operand1 = null;
645 Expression operand2 = null;
646}
647{
648 operand1 = UnaryExpr()
649 ("union"
650 (operand2 = UnaryExpr()) {
651 if (union == null) {
652 union = new UnionExpr();
653 union.addExpr(operand1);
654 }
655 union.addExpr(operand2);
656 } )*
657 {
658 return (union == null)? operand1: union;
659 }
660}
661
662Expression UnaryExpr() throws ParseException:
663{
664 Expression uexpr = null;
665 Expression expr = null;
666}
667{
668 (( "+"|"-")
669 {
670 uexpr = new UnaryExpr();
671 Token t = getToken(0);
672 if("+".equals(t.toString()))
673 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
674 else if("-".equals(t.toString()))
675 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
676 else
677 throw new ParseException();
678 }
679 )?
680
681 expr = ValueExpr()
682 {
683 if(uexpr!=null){
684 ((UnaryExpr)uexpr).setExpr(expr);
685 return uexpr;
686 }
687 else{
688 return expr;
689 }
690 }
691}
692
693Expression ValueExpr() throws ParseException:
694{
695 Expression expr;
696}
697{
698 expr = FieldOrIndexAccessor()
699 {
700 return expr;
701 }
702}
703
704
705Expression FieldOrIndexAccessor()throws ParseException:
706{
707 Expression expr = null;
708 Identifier ident = null;
709 AbstractAccessor fa = null;
710 int index;
711
712}
713{
714 ( expr = PrimaryExpr()
715
716 )
717
718
719 (
720 (
721 ident = Field()
722 {
723 if(fa == null)
724 fa = new FieldAccessor(expr, ident);
725 else
726 fa = new FieldAccessor(fa, ident);
727 }
728 )
729 | (
730 index = Index()
731 {
732 if(fa == null)
733 fa = new IndexAccessor(expr, index);
734 else
735 fa = new IndexAccessor(fa, index);
736 }
737 )
738 )*
739
740
741 {
742 return fa==null?expr:fa;
743 }
744}
745
746Identifier Field() throws ParseException:
747{
748 Identifier ident = null;
749
750}
751{
752 "." < IDENTIFIER >
753 {
754
755 ident = new Identifier();
756 ident.setValue(getToken(0).toString());
757
758 return ident;
759 }
760}
761
762int Index() throws ParseException:
763{
764 Expression expr = null;
765 int idx = -2;
766}
767{
768 "[" ( expr = Expression()
769 {
770 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
771 {
ilovesoupc9fef1d2012-07-08 19:30:42 +0000772 Literal lit = ((LiteralExpr)expr).getValue();
773 if(lit.getLiteralType() == Literal.Type.INTEGER ||
774 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +0000775 idx = Integer.valueOf(lit.getStringValue());
776 }
777 else {
778 throw new ParseException("Index should be an INTEGER");
779 }
780 }
781
782 }
783
784 | "?"
785 {
786 idx = IndexAccessor.ANY;
787 // ANY
788 }
789
790 )
791
792 "]"
793 {
794 return idx;
795 }
796}
797
798
799Expression PrimaryExpr()throws ParseException:
800{
801 Expression expr = null;
802}
803{
ilovesoupc9fef1d2012-07-08 19:30:42 +0000804 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +0000805 (
806 expr =Literal()
807 | expr = FunctionCallExpr()
808 | expr =VariableRef()
809
810 {
811 if(((VariableExpr)expr).getIsNewVar() == true)
812 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
813 }
814 | expr = ListConstructor()
815 | expr = RecordConstructor()
816 | expr = ParenthesizedExpression()
817 | expr = MetaVariableRef()
818 )
819 {
820 return expr;
821 }
822}
823
824Expression Literal() throws ParseException:
825{
826
827 LiteralExpr lit = new LiteralExpr();
828 Token t;
829}
830{
831(
832 <STRING_LITERAL>
833 {
834 t= getToken(0);
835 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
836 }
837
838 | <INTEGER_LITERAL>
839 {
840 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +0000841 try {
842 lit.setValue(new IntegerLiteral(new Integer(t.image)));
843 } catch(NumberFormatException ex) {
844 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
845 }
vinayakb38b7ca42012-03-05 05:44:15 +0000846 }
847 | < FLOAT_LITERAL >
848 {
849 t= getToken(0);
850 lit.setValue(new FloatLiteral(new Float(t.image)));
851 }
852 | < DOUBLE_LITERAL >
853 {
854 t= getToken(0);
855 lit.setValue(new DoubleLiteral(new Double(t.image)));
856 }
857 | <NULL>
858 {
859 t= getToken(0);
860 lit.setValue(NullLiteral.INSTANCE);
861 }
862 | <TRUE>
863 {
864 t= getToken(0);
865 lit.setValue(TrueLiteral.INSTANCE);
866 }
867 | <FALSE>
868 {
869 t= getToken(0);
870 lit.setValue(FalseLiteral.INSTANCE);
871 }
872)
873 {
874 return lit;
875 }
876}
877
878
879VariableExpr VariableRef() throws ParseException:
880{
881 VariableExpr varExp = new VariableExpr();
882 VarIdentifier var = new VarIdentifier();
883 Token t;
884}
885{
886 <VARIABLE>
887 {
888 t = getToken(0);//get current token
889 String varName = t.toString();
890 Identifier ident = lookupSymbol(varName);
891 if (isInForbiddenScopes(varName)) {
892 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.");
893 }
894 if(ident != null) { // exist such ident
895 varExp.setIsNewVar(false);
896 varExp.setVar((VarIdentifier)ident);
897 } else {
898 varExp.setVar(var);
899 }
900 var.setValue(t.toString());
901 return varExp;
902 }
903}
904
905
906VariableExpr Variable() throws ParseException:
907{
908 VariableExpr varExp = new VariableExpr();
909 VarIdentifier var = new VarIdentifier();
910 Token t;
911}
912{
913 <VARIABLE>
914 {
915 t = getToken(0);//get current token
916 Identifier ident = lookupSymbol(t.toString());
917 if(ident != null) { // exist such ident
918 varExp.setIsNewVar(false);
919 }
920 varExp.setVar(var);
921 var.setValue(t.toString());
922 return varExp;
923 }
924}
925
926MetaVariableExpr MetaVariableRef() throws ParseException:
927{
928 MetaVariableExpr metaVarExp = new MetaVariableExpr();
929 VarIdentifier var = new VarIdentifier();
930 Token t;
931}
932{
933 <METAVARIABLE>
934 {
935 t = getToken(0);//get current token
936 metaVarExp.setVar(var);
937 var.setValue(t.toString());
938 return metaVarExp;
939 }
940}
941
942Expression ListConstructor() throws ParseException:
943{
944 Expression expr = null;
945}
946{
947 (
948 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
949 )
950
951 {
952 return expr;
953 }
954}
955
956
957ListConstructor OrderedListConstructor() throws ParseException:
958{
959 ListConstructor expr = new ListConstructor();
960 Expression tmp = null;
961 List<Expression> exprList = new ArrayList<Expression>();
962 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
963}
964{
965
966 "["
967 ( tmp = Expression()
968 {
969 exprList.add(tmp);
970 }
971
972 ("," tmp = Expression() { exprList.add(tmp); })*
973 )?
974
975 "]"
976
977 {
978 expr.setExprList(exprList);
979 return expr;
980 }
981}
982
983ListConstructor UnorderedListConstructor() throws ParseException:
984{
985 ListConstructor expr = new ListConstructor();
986 Expression tmp = null;
987 List<Expression> exprList = new ArrayList<Expression>();
988 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
989}
990{
991
992 "{{" ( tmp = Expression()
993 {
994 exprList.add(tmp);
995 }
996 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
997 {
998 expr.setExprList(exprList);
999 return expr;
1000 }
1001}
1002
1003RecordConstructor RecordConstructor() throws ParseException:
1004{
1005 RecordConstructor expr = new RecordConstructor();
1006 FieldBinding tmp = null;
1007 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1008}
1009{
1010 "{" (tmp = FieldBinding()
1011 {
1012 fbList.add(tmp);
1013 }
1014 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1015 {
1016 expr.setFbList(fbList);
1017 return expr;
1018 }
1019}
1020
1021FieldBinding FieldBinding() throws ParseException:
1022{
1023 FieldBinding fb = new FieldBinding();
1024 Expression left, right;
1025}
1026{
1027 left = Expression() ":" right = Expression()
1028 {
1029 fb.setLeftExpr(left);
1030 fb.setRightExpr(right);
1031 return fb;
1032 }
1033}
1034
1035Expression FunctionCallExpr() throws ParseException:
1036{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001037 CallExpr callExpr;
1038 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001039 Expression tmp;
1040 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001041 String funcName;
1042 String dataverse;
1043 String hint=null;
1044 String id1=null;
1045 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001046}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001047{
1048 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;}
1049 ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})?
1050 |
1051 <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();}
1052 )
vinayakb38b7ca42012-03-05 05:44:15 +00001053 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001054 hint=getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001055 }
1056 <LEFTPAREN> (tmp = Expression()
1057 {
1058 argList.add(tmp);
1059 arity ++;
1060 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1061
1062 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001063 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1064 if(signature == null)
1065 {
1066 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1067 }
1068 callExpr = new CallExpr(signature,argList);
1069 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1070 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1071 }
1072 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001073 }
1074}
1075
1076
1077Expression ParenthesizedExpression() throws ParseException:
1078{
1079 Expression expr;
1080}
1081{
1082 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1083 {
1084 return expr;
1085 }
1086}
1087
1088Expression IfThenElse() throws ParseException:
1089{
1090 Expression condExpr;
1091 Expression thenExpr;
1092 Expression elseExpr;
1093 IfExpr ifExpr = new IfExpr();
1094}
1095{
1096 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1097
1098 {
1099 ifExpr.setCondExpr(condExpr);
1100 ifExpr.setThenExpr(thenExpr);
1101 ifExpr.setElseExpr(elseExpr);
1102 return ifExpr;
1103 }
1104}
1105
1106Expression FLWOGR() throws ParseException:
1107{
1108 FLWOGRExpression flworg = new FLWOGRExpression();
1109 List<Clause> clauseList = new ArrayList<Clause>();
1110 Expression returnExpr;
1111 Clause tmp;
1112 createNewScope();
1113}
1114{
1115 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);})
1116 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1117
1118 {
1119 flworg.setClauseList(clauseList);
1120 flworg.setReturnExpr(returnExpr);
1121 removeCurrentScope();
1122 return flworg;
1123 }
1124}
1125
1126List<Clause> Clauses() throws ParseException:
1127{
1128 List<Clause> clauses = new ArrayList<Clause>();
1129 Clause c = null;
1130}
1131{
1132 (
1133 (
1134 c = Clause() {
1135 clauses.add(c);
1136 }
1137 )*
1138 )
1139 {
1140 return clauses;
1141 }
1142}
1143
1144Clause Clause() throws ParseException :
1145{
1146 Clause clause;
1147}
1148{
1149 (
1150 clause = ForClause()
1151 | clause = LetClause()
1152 | clause = WhereClause()
1153 | clause = OrderbyClause()
1154 | clause = GroupClause()
1155 | clause = LimitClause()
1156 | clause = DistinctClause()
1157 | clause = MetaVariableClause()
1158 | clause = JoinClause()
1159 )
1160 {
1161 return clause;
1162 }
1163}
1164
1165Clause MetaVariableClause() throws ParseException :
1166{
1167 MetaVariableClause mc = new MetaVariableClause();
1168 VarIdentifier var = new VarIdentifier();
1169 Token t;
1170}
1171{
1172 <METAVARIABLECLAUSE>
1173 {
1174 t = getToken(0);
1175 mc.setVar(var);
1176 var.setValue(t.toString());
1177 return mc;
1178 }
1179}
1180
1181Clause JoinClause() throws ParseException :
1182{
1183 Expression whereExpr;
1184 List<Clause> leftClauses, rightClauses;
1185 JoinClause.JoinKind kind = JoinClause.JoinKind.INNER;
1186}
1187{
1188 ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } )
1189 <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> ","
1190 <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> ","
1191 whereExpr = Expression() <RIGHTPAREN>
1192 {
1193 JoinClause jc = new JoinClause(kind);
1194 jc.setLeftClauses(leftClauses);
1195 jc.setRightClauses(rightClauses);
1196 jc.setWhereExpr(whereExpr);
1197 return jc;
1198 }
1199}
1200
1201Clause ForClause()throws ParseException :
1202{
1203 ForClause fc = new ForClause();
1204 VariableExpr varExp;
1205 VariableExpr varPos = null;
1206 Expression inExp;
1207 extendCurrentScope();
1208}
1209{
1210 "for" varExp = Variable()
1211 {
1212 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1213 }
1214 ("at" varPos = Variable()
1215 {
1216 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1217 }
1218 )?
1219 "in" ( inExp = Expression() )
1220 {
1221 fc.setVarExpr(varExp);
1222 fc.setInExpr(inExp);
1223 if (varPos != null) {
1224 fc.setPosExpr(varPos);
1225 }
1226 return fc;
1227 }
1228}
1229
1230Clause LetClause() throws ParseException:
1231{
1232 LetClause lc = new LetClause();
1233 VariableExpr varExp;
1234 Expression beExp;
1235 extendCurrentScope();
1236}
1237{
ilovesoupb2527c12012-07-12 03:21:13 +00001238 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001239 {
1240 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001241 lc.setVarExpr(varExp);
1242 lc.setBeExpr(beExp);
1243 return lc;
1244 }
1245}
1246
1247Clause WhereClause()throws ParseException :
1248{
1249 WhereClause wc = new WhereClause();
1250 Expression whereExpr;
1251}
1252{
1253 "where" whereExpr = Expression()
1254 {
1255 wc.setWhereExpr(whereExpr);
1256 return wc;
1257 }
1258}
1259
1260Clause OrderbyClause()throws ParseException :
1261{
1262 OrderbyClause oc = new OrderbyClause();
1263 Expression orderbyExpr;
1264 List<Expression> orderbyList = new ArrayList<Expression>();
1265 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1266 int numOfOrderby = 0;
1267}
1268{
1269 (
1270 "order"
1271 {
1272 String hint = getHint(token);
1273 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1274 String splits[] = hint.split(" +");
1275 int numFrames = Integer.parseInt(splits[1]);
1276 int numTuples = Integer.parseInt(splits[2]);
1277 oc.setNumFrames(numFrames);
1278 oc.setNumTuples(numTuples);
1279 }
1280 }
1281 "by" orderbyExpr = Expression()
1282 {
1283 orderbyList.add(orderbyExpr);
1284 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1285 }
1286 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1287 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1288 {
1289 modifierList.add(modif);
1290 }
1291
1292 ("," orderbyExpr = Expression()
1293 {
1294 orderbyList.add(orderbyExpr);
1295 modif = OrderbyClause.OrderModifier.ASC;
1296 }
1297 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1298 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1299 {
1300 modifierList.add(modif);
1301 }
1302 )*
1303)
1304 {
1305 oc.setModifierList(modifierList);
1306 oc.setOrderbyList(orderbyList);
1307 return oc;
1308 }
1309}
1310Clause GroupClause()throws ParseException :
1311{
1312 GroupbyClause gbc = new GroupbyClause();
1313 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1314 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1315 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1316 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1317 VariableExpr var = null;
1318 VariableExpr withVar = null;
1319 Expression expr = null;
1320 VariableExpr decorVar = null;
1321 Expression decorExpr = null;
1322}
1323{
1324 {
1325 Scope newScope = extendCurrentScopeNoPush(true);
1326 // extendCurrentScope(true);
1327 }
1328 "group"
1329 {
1330 String hint = getHint(token);
1331 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1332 gbc.setHashGroupByHint(true);
1333 }
1334 }
1335 "by" (LOOKAHEAD(2) var = Variable()
1336 {
1337 newScope.addNewVarSymbolToScope(var.getVar());
1338 } ":=")?
1339 expr = Expression()
1340 {
1341 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1342 vePairList.add(pair1);
1343 }
1344 ("," ( LOOKAHEAD(2) var = Variable()
1345 {
1346 newScope.addNewVarSymbolToScope(var.getVar());
1347 } ":=")?
1348 expr = Expression()
1349 {
1350 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1351 vePairList.add(pair2);
1352 }
1353 )*
1354 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1355 {
1356 newScope.addNewVarSymbolToScope(decorVar.getVar());
1357 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1358 decorPairList.add(pair3);
1359 }
1360 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1361 {
1362 newScope.addNewVarSymbolToScope(decorVar.getVar());
1363 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1364 decorPairList.add(pair4);
1365 }
1366 )*
1367 )?
1368 "with" withVar = VariableRef()
1369 {
1370 if(withVar.getIsNewVar()==true)
1371 throw new ParseException("can't find variable " + withVar.getVar());
1372 withVarList.add(withVar);
1373 newScope.addNewVarSymbolToScope(withVar.getVar());
1374 }
1375 ("," withVar = VariableRef()
1376 {
1377 if(withVar.getIsNewVar()==true)
1378 throw new ParseException("can't find variable " + withVar.getVar());
1379 withVarList.add(withVar);
1380 newScope.addNewVarSymbolToScope(withVar.getVar());
1381 })*
1382 {
1383 gbc.setGbyPairList(vePairList);
1384 gbc.setDecorPairList(decorPairList);
1385 gbc.setWithVarList(withVarList);
1386 replaceCurrentScope(newScope);
1387 return gbc;
1388 }
1389}
1390
1391
1392LimitClause LimitClause() throws ParseException:
1393{
1394 LimitClause lc = new LimitClause();
1395 Expression expr;
1396 pushForbiddenScope(getCurrentScope());
1397}
1398{
1399 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1400 ("offset" expr = Expression() { lc.setOffset(expr); })?
1401
1402 {
1403 popForbiddenScope();
1404 return lc;
1405 }
1406}
1407
1408DistinctClause DistinctClause() throws ParseException:
1409{
1410 List<Expression> exprs = new ArrayList<Expression>();
1411 Expression expr;
1412}
1413{
1414 "distinct" "by" expr = Expression()
1415 {
1416 exprs.add(expr);
1417 }
1418 ("," expr = Expression()
1419 {
1420 exprs.add(expr);
1421 }
1422 )*
1423 {
1424 return new DistinctClause(exprs);
1425 }
1426}
1427
1428
1429QuantifiedExpression QuantifiedExpression()throws ParseException:
1430{
1431 QuantifiedExpression qc = new QuantifiedExpression();
1432 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
1433 Expression satisfiesExpr;
1434 VariableExpr var;
1435 Expression inExpr;
1436 QuantifiedPair pair;
1437}
1438{
1439 {
1440 createNewScope();
1441 }
1442
1443 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
1444 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
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 "," var = Variable() "in" inExpr = Expression()
1453 {
1454 pair = new QuantifiedPair(var, inExpr);
1455 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1456 quantifiedList.add(pair);
1457 }
1458 )*
1459 "satisfies" satisfiesExpr = Expression()
1460 {
1461 qc.setSatisfiesExpr(satisfiesExpr);
1462 qc.setQuantifiedList(quantifiedList);
1463 removeCurrentScope();
1464 return qc;
1465 }
1466}
1467
1468TOKEN_MGR_DECLS:
1469{
1470 public int commentDepth = 0;
1471}
1472
1473<DEFAULT>
1474TOKEN :
1475{
1476 <CARET : "^" >
1477}
1478
1479<DEFAULT>
1480TOKEN :
1481{
1482 <DATASET : "dataset" >
1483}
1484
1485<DEFAULT>
1486TOKEN :
1487{
1488 <LEFTPAREN : "(" >
1489}
1490
1491<DEFAULT>
1492TOKEN :
1493{
1494 <RIGHTPAREN : ")" >
1495}
1496
1497
1498<DEFAULT>
1499TOKEN :
1500{
1501 <INTEGER_LITERAL : (<DIGIT>)+ >
1502}
1503
1504
1505<DEFAULT>
1506TOKEN :
1507{
1508 <NULL : "null">
1509}
1510
1511<DEFAULT>
1512TOKEN :
1513{
1514 <TRUE : "true">
1515}
1516
1517<DEFAULT>
1518TOKEN :
1519{
1520 <FALSE : "false">
1521}
1522
1523<DEFAULT>
1524TOKEN :
1525{
1526 <#DIGIT : ["0" - "9"]>
1527}
1528
1529
1530TOKEN:
1531{
1532 < DOUBLE_LITERAL: <INTEGER>
1533 | <INTEGER> ( "." <INTEGER> )?
1534 | "." <INTEGER>
1535 >
1536 |
1537 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
1538 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
1539 | "." <INTEGER> ( "f" | "F" )
1540 >
1541 |
1542 <INTEGER : (<DIGIT>)+ >
1543}
1544
1545<DEFAULT>
1546TOKEN :
1547{
1548 <#LETTER : ["A" - "Z", "a" - "z"]>
1549}
1550
1551<DEFAULT>
1552TOKEN :
1553{
1554 <SPECIALCHARS : ["$", "_", "-"] >
1555}
1556
1557<DEFAULT>
1558TOKEN :
1559{
1560 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
1561 |
1562 < #EscapeQuot: "\\\"" >
1563 |
1564 < #EscapeApos: "\\\'" >
1565}
1566
1567<DEFAULT>
1568TOKEN :
1569{
1570 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
1571}
1572
1573<DEFAULT>
1574TOKEN :
1575{
1576 <VARIABLE : "$" <IDENTIFIER> >
1577}
1578
1579<DEFAULT>
1580TOKEN :
1581{
1582 <METAVARIABLECLAUSE : "#" <IDENTIFIER> >
1583}
1584
1585<DEFAULT>
1586TOKEN :
1587{
1588 <METAVARIABLE : "$$" <IDENTIFIER> >
1589}
1590
1591SKIP:
1592{
1593 " "
1594| "\t"
1595| "\r"
1596| "\n"
1597}
1598
1599SKIP:
1600{
1601 <"//" (~["\n"])* "\n">
1602}
1603
1604SKIP:
1605{
1606 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
1607}
1608
1609
1610SKIP:
1611{
1612 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
1613}
1614
1615<INSIDE_COMMENT>
1616SPECIAL_TOKEN:
1617{
1618 <"+"(" ")*(~["/","*"])*>
1619}
1620
1621<INSIDE_COMMENT>
1622SKIP:
1623{
1624 <"/*"> {commentDepth++;}
1625}
1626
1627<INSIDE_COMMENT>
1628SKIP:
1629{
1630 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
1631| <~[]>
1632}