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