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