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