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