blob: dd72de413d05f0a7b194a190378a4a51ae907e4b [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;
icetindila611ac72014-05-16 10:10:11 -0700702 Expression indexExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000703
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 | (
icetindila611ac72014-05-16 10:10:11 -0700722 indexExpr = Index()
vinayakb38b7ca42012-03-05 05:44:15 +0000723 {
724 if(fa == null)
icetindila611ac72014-05-16 10:10:11 -0700725 fa = new IndexAccessor(expr, indexExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000726 else
icetindila611ac72014-05-16 10:10:11 -0700727 fa = new IndexAccessor(fa, indexExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000728 }
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
icetindila611ac72014-05-16 10:10:11 -0700754Expression Index() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000755{
756 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000757}
758{
759 "[" ( expr = Expression()
760 {
761 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
762 {
ilovesoupc9fef1d2012-07-08 19:30:42 +0000763 Literal lit = ((LiteralExpr)expr).getValue();
icetindila611ac72014-05-16 10:10:11 -0700764 if(lit.getLiteralType() != Literal.Type.INTEGER &&
765 lit.getLiteralType() != Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +0000766 throw new ParseException("Index should be an INTEGER");
767 }
768 }
vinayakb38b7ca42012-03-05 05:44:15 +0000769 }
770
771 | "?"
772 {
vinayakb38b7ca42012-03-05 05:44:15 +0000773 // ANY
774 }
775
776 )
777
778 "]"
779 {
icetindila611ac72014-05-16 10:10:11 -0700780 return expr;
vinayakb38b7ca42012-03-05 05:44:15 +0000781 }
782}
783
784
785Expression PrimaryExpr()throws ParseException:
786{
787 Expression expr = null;
788}
789{
ilovesoupc9fef1d2012-07-08 19:30:42 +0000790 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +0000791 (
792 expr =Literal()
793 | expr = FunctionCallExpr()
794 | expr =VariableRef()
795
796 {
797 if(((VariableExpr)expr).getIsNewVar() == true)
798 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
799 }
800 | expr = ListConstructor()
801 | expr = RecordConstructor()
802 | expr = ParenthesizedExpression()
803 | expr = MetaVariableRef()
804 )
805 {
806 return expr;
807 }
808}
809
810Expression Literal() throws ParseException:
811{
812
813 LiteralExpr lit = new LiteralExpr();
814 Token t;
815}
816{
817(
818 <STRING_LITERAL>
819 {
820 t= getToken(0);
821 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
822 }
823
824 | <INTEGER_LITERAL>
825 {
826 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +0000827 try {
828 lit.setValue(new IntegerLiteral(new Integer(t.image)));
829 } catch(NumberFormatException ex) {
830 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
831 }
vinayakb38b7ca42012-03-05 05:44:15 +0000832 }
833 | < FLOAT_LITERAL >
834 {
835 t= getToken(0);
836 lit.setValue(new FloatLiteral(new Float(t.image)));
837 }
838 | < DOUBLE_LITERAL >
839 {
840 t= getToken(0);
841 lit.setValue(new DoubleLiteral(new Double(t.image)));
842 }
843 | <NULL>
844 {
845 t= getToken(0);
846 lit.setValue(NullLiteral.INSTANCE);
847 }
848 | <TRUE>
849 {
850 t= getToken(0);
851 lit.setValue(TrueLiteral.INSTANCE);
852 }
853 | <FALSE>
854 {
855 t= getToken(0);
856 lit.setValue(FalseLiteral.INSTANCE);
857 }
858)
859 {
860 return lit;
861 }
862}
863
864
865VariableExpr VariableRef() throws ParseException:
866{
867 VariableExpr varExp = new VariableExpr();
868 VarIdentifier var = new VarIdentifier();
869 Token t;
870}
871{
872 <VARIABLE>
873 {
874 t = getToken(0);//get current token
875 String varName = t.toString();
876 Identifier ident = lookupSymbol(varName);
877 if (isInForbiddenScopes(varName)) {
878 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.");
879 }
880 if(ident != null) { // exist such ident
881 varExp.setIsNewVar(false);
882 varExp.setVar((VarIdentifier)ident);
883 } else {
884 varExp.setVar(var);
885 }
886 var.setValue(t.toString());
887 return varExp;
888 }
889}
890
891
892VariableExpr Variable() throws ParseException:
893{
894 VariableExpr varExp = new VariableExpr();
895 VarIdentifier var = new VarIdentifier();
896 Token t;
897}
898{
899 <VARIABLE>
900 {
901 t = getToken(0);//get current token
902 Identifier ident = lookupSymbol(t.toString());
903 if(ident != null) { // exist such ident
904 varExp.setIsNewVar(false);
905 }
906 varExp.setVar(var);
907 var.setValue(t.toString());
908 return varExp;
909 }
910}
911
912MetaVariableExpr MetaVariableRef() throws ParseException:
913{
914 MetaVariableExpr metaVarExp = new MetaVariableExpr();
915 VarIdentifier var = new VarIdentifier();
916 Token t;
917}
918{
919 <METAVARIABLE>
920 {
921 t = getToken(0);//get current token
922 metaVarExp.setVar(var);
923 var.setValue(t.toString());
924 return metaVarExp;
925 }
926}
927
928Expression ListConstructor() throws ParseException:
929{
930 Expression expr = null;
931}
932{
933 (
934 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
935 )
936
937 {
938 return expr;
939 }
940}
941
942
943ListConstructor OrderedListConstructor() throws ParseException:
944{
945 ListConstructor expr = new ListConstructor();
946 Expression tmp = null;
947 List<Expression> exprList = new ArrayList<Expression>();
948 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
949}
950{
951
952 "["
953 ( tmp = Expression()
954 {
955 exprList.add(tmp);
956 }
957
958 ("," tmp = Expression() { exprList.add(tmp); })*
959 )?
960
961 "]"
962
963 {
964 expr.setExprList(exprList);
965 return expr;
966 }
967}
968
969ListConstructor UnorderedListConstructor() throws ParseException:
970{
971 ListConstructor expr = new ListConstructor();
972 Expression tmp = null;
973 List<Expression> exprList = new ArrayList<Expression>();
974 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
975}
976{
977
978 "{{" ( tmp = Expression()
979 {
980 exprList.add(tmp);
981 }
982 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
983 {
984 expr.setExprList(exprList);
985 return expr;
986 }
987}
988
989RecordConstructor RecordConstructor() throws ParseException:
990{
991 RecordConstructor expr = new RecordConstructor();
992 FieldBinding tmp = null;
993 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
994}
995{
996 "{" (tmp = FieldBinding()
997 {
998 fbList.add(tmp);
999 }
1000 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1001 {
1002 expr.setFbList(fbList);
1003 return expr;
1004 }
1005}
1006
1007FieldBinding FieldBinding() throws ParseException:
1008{
1009 FieldBinding fb = new FieldBinding();
1010 Expression left, right;
1011}
1012{
1013 left = Expression() ":" right = Expression()
1014 {
1015 fb.setLeftExpr(left);
1016 fb.setRightExpr(right);
1017 return fb;
1018 }
1019}
1020
1021Expression FunctionCallExpr() throws ParseException:
1022{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001023 CallExpr callExpr;
1024 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001025 Expression tmp;
1026 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001027 String funcName;
1028 String dataverse;
1029 String hint=null;
1030 String id1=null;
1031 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001032}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001033{
1034 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;}
1035 ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})?
1036 |
1037 <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();}
1038 )
vinayakb38b7ca42012-03-05 05:44:15 +00001039 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001040 hint=getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001041 }
1042 <LEFTPAREN> (tmp = Expression()
1043 {
1044 argList.add(tmp);
1045 arity ++;
1046 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1047
1048 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001049 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1050 if(signature == null)
1051 {
1052 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1053 }
1054 callExpr = new CallExpr(signature,argList);
1055 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1056 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1057 }
1058 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001059 }
1060}
1061
1062
1063Expression ParenthesizedExpression() throws ParseException:
1064{
1065 Expression expr;
1066}
1067{
1068 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1069 {
1070 return expr;
1071 }
1072}
1073
1074Expression IfThenElse() throws ParseException:
1075{
1076 Expression condExpr;
1077 Expression thenExpr;
1078 Expression elseExpr;
1079 IfExpr ifExpr = new IfExpr();
1080}
1081{
1082 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1083
1084 {
1085 ifExpr.setCondExpr(condExpr);
1086 ifExpr.setThenExpr(thenExpr);
1087 ifExpr.setElseExpr(elseExpr);
1088 return ifExpr;
1089 }
1090}
1091
1092Expression FLWOGR() throws ParseException:
1093{
1094 FLWOGRExpression flworg = new FLWOGRExpression();
1095 List<Clause> clauseList = new ArrayList<Clause>();
1096 Expression returnExpr;
1097 Clause tmp;
1098 createNewScope();
1099}
1100{
1101 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);})
1102 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1103
1104 {
1105 flworg.setClauseList(clauseList);
1106 flworg.setReturnExpr(returnExpr);
1107 removeCurrentScope();
1108 return flworg;
1109 }
1110}
1111
1112List<Clause> Clauses() throws ParseException:
1113{
1114 List<Clause> clauses = new ArrayList<Clause>();
1115 Clause c = null;
1116}
1117{
1118 (
1119 (
1120 c = Clause() {
1121 clauses.add(c);
1122 }
1123 )*
1124 )
1125 {
1126 return clauses;
1127 }
1128}
1129
1130Clause Clause() throws ParseException :
1131{
1132 Clause clause;
1133}
1134{
1135 (
1136 clause = ForClause()
1137 | clause = LetClause()
1138 | clause = WhereClause()
1139 | clause = OrderbyClause()
1140 | clause = GroupClause()
1141 | clause = LimitClause()
1142 | clause = DistinctClause()
1143 | clause = MetaVariableClause()
1144 | clause = JoinClause()
1145 )
1146 {
1147 return clause;
1148 }
1149}
1150
1151Clause MetaVariableClause() throws ParseException :
1152{
1153 MetaVariableClause mc = new MetaVariableClause();
1154 VarIdentifier var = new VarIdentifier();
1155 Token t;
1156}
1157{
1158 <METAVARIABLECLAUSE>
1159 {
1160 t = getToken(0);
1161 mc.setVar(var);
1162 var.setValue(t.toString());
1163 return mc;
1164 }
1165}
1166
1167Clause JoinClause() throws ParseException :
1168{
1169 Expression whereExpr;
1170 List<Clause> leftClauses, rightClauses;
1171 JoinClause.JoinKind kind = JoinClause.JoinKind.INNER;
1172}
1173{
1174 ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } )
1175 <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> ","
1176 <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> ","
1177 whereExpr = Expression() <RIGHTPAREN>
1178 {
1179 JoinClause jc = new JoinClause(kind);
1180 jc.setLeftClauses(leftClauses);
1181 jc.setRightClauses(rightClauses);
1182 jc.setWhereExpr(whereExpr);
1183 return jc;
1184 }
1185}
1186
1187Clause ForClause()throws ParseException :
1188{
1189 ForClause fc = new ForClause();
1190 VariableExpr varExp;
1191 VariableExpr varPos = null;
1192 Expression inExp;
1193 extendCurrentScope();
1194}
1195{
1196 "for" varExp = Variable()
1197 {
1198 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1199 }
1200 ("at" varPos = Variable()
1201 {
1202 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1203 }
1204 )?
1205 "in" ( inExp = Expression() )
1206 {
1207 fc.setVarExpr(varExp);
1208 fc.setInExpr(inExp);
1209 if (varPos != null) {
1210 fc.setPosExpr(varPos);
1211 }
1212 return fc;
1213 }
1214}
1215
1216Clause LetClause() throws ParseException:
1217{
1218 LetClause lc = new LetClause();
1219 VariableExpr varExp;
1220 Expression beExp;
1221 extendCurrentScope();
1222}
1223{
ilovesoupb2527c12012-07-12 03:21:13 +00001224 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001225 {
1226 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001227 lc.setVarExpr(varExp);
1228 lc.setBeExpr(beExp);
1229 return lc;
1230 }
1231}
1232
1233Clause WhereClause()throws ParseException :
1234{
1235 WhereClause wc = new WhereClause();
1236 Expression whereExpr;
1237}
1238{
1239 "where" whereExpr = Expression()
1240 {
1241 wc.setWhereExpr(whereExpr);
1242 return wc;
1243 }
1244}
1245
1246Clause OrderbyClause()throws ParseException :
1247{
1248 OrderbyClause oc = new OrderbyClause();
1249 Expression orderbyExpr;
1250 List<Expression> orderbyList = new ArrayList<Expression>();
1251 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1252 int numOfOrderby = 0;
1253}
1254{
1255 (
1256 "order"
1257 {
1258 String hint = getHint(token);
1259 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1260 String splits[] = hint.split(" +");
1261 int numFrames = Integer.parseInt(splits[1]);
1262 int numTuples = Integer.parseInt(splits[2]);
1263 oc.setNumFrames(numFrames);
1264 oc.setNumTuples(numTuples);
1265 }
1266 }
1267 "by" orderbyExpr = Expression()
1268 {
1269 orderbyList.add(orderbyExpr);
1270 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1271 }
1272 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1273 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1274 {
1275 modifierList.add(modif);
1276 }
1277
1278 ("," orderbyExpr = Expression()
1279 {
1280 orderbyList.add(orderbyExpr);
1281 modif = OrderbyClause.OrderModifier.ASC;
1282 }
1283 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1284 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1285 {
1286 modifierList.add(modif);
1287 }
1288 )*
1289)
1290 {
1291 oc.setModifierList(modifierList);
1292 oc.setOrderbyList(orderbyList);
1293 return oc;
1294 }
1295}
1296Clause GroupClause()throws ParseException :
1297{
1298 GroupbyClause gbc = new GroupbyClause();
1299 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1300 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1301 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1302 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1303 VariableExpr var = null;
1304 VariableExpr withVar = null;
1305 Expression expr = null;
1306 VariableExpr decorVar = null;
1307 Expression decorExpr = null;
1308}
1309{
1310 {
1311 Scope newScope = extendCurrentScopeNoPush(true);
1312 // extendCurrentScope(true);
1313 }
1314 "group"
1315 {
1316 String hint = getHint(token);
1317 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1318 gbc.setHashGroupByHint(true);
1319 }
1320 }
1321 "by" (LOOKAHEAD(2) var = Variable()
1322 {
1323 newScope.addNewVarSymbolToScope(var.getVar());
1324 } ":=")?
1325 expr = Expression()
1326 {
1327 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1328 vePairList.add(pair1);
1329 }
1330 ("," ( LOOKAHEAD(2) var = Variable()
1331 {
1332 newScope.addNewVarSymbolToScope(var.getVar());
1333 } ":=")?
1334 expr = Expression()
1335 {
1336 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1337 vePairList.add(pair2);
1338 }
1339 )*
1340 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1341 {
1342 newScope.addNewVarSymbolToScope(decorVar.getVar());
1343 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1344 decorPairList.add(pair3);
1345 }
1346 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1347 {
1348 newScope.addNewVarSymbolToScope(decorVar.getVar());
1349 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1350 decorPairList.add(pair4);
1351 }
1352 )*
1353 )?
1354 "with" withVar = VariableRef()
1355 {
1356 if(withVar.getIsNewVar()==true)
1357 throw new ParseException("can't find variable " + withVar.getVar());
1358 withVarList.add(withVar);
1359 newScope.addNewVarSymbolToScope(withVar.getVar());
1360 }
1361 ("," withVar = VariableRef()
1362 {
1363 if(withVar.getIsNewVar()==true)
1364 throw new ParseException("can't find variable " + withVar.getVar());
1365 withVarList.add(withVar);
1366 newScope.addNewVarSymbolToScope(withVar.getVar());
1367 })*
1368 {
1369 gbc.setGbyPairList(vePairList);
1370 gbc.setDecorPairList(decorPairList);
1371 gbc.setWithVarList(withVarList);
1372 replaceCurrentScope(newScope);
1373 return gbc;
1374 }
1375}
1376
1377
1378LimitClause LimitClause() throws ParseException:
1379{
1380 LimitClause lc = new LimitClause();
1381 Expression expr;
1382 pushForbiddenScope(getCurrentScope());
1383}
1384{
1385 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1386 ("offset" expr = Expression() { lc.setOffset(expr); })?
1387
1388 {
1389 popForbiddenScope();
1390 return lc;
1391 }
1392}
1393
1394DistinctClause DistinctClause() throws ParseException:
1395{
1396 List<Expression> exprs = new ArrayList<Expression>();
1397 Expression expr;
1398}
1399{
1400 "distinct" "by" expr = Expression()
1401 {
1402 exprs.add(expr);
1403 }
1404 ("," expr = Expression()
1405 {
1406 exprs.add(expr);
1407 }
1408 )*
1409 {
1410 return new DistinctClause(exprs);
1411 }
1412}
1413
1414
1415QuantifiedExpression QuantifiedExpression()throws ParseException:
1416{
1417 QuantifiedExpression qc = new QuantifiedExpression();
1418 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
1419 Expression satisfiesExpr;
1420 VariableExpr var;
1421 Expression inExpr;
1422 QuantifiedPair pair;
1423}
1424{
1425 {
1426 createNewScope();
1427 }
1428
1429 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
1430 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
1431 var = Variable() "in" inExpr = Expression()
1432 {
1433 pair = new QuantifiedPair(var, inExpr);
1434 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1435 quantifiedList.add(pair);
1436 }
1437 (
1438 "," var = Variable() "in" inExpr = Expression()
1439 {
1440 pair = new QuantifiedPair(var, inExpr);
1441 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1442 quantifiedList.add(pair);
1443 }
1444 )*
1445 "satisfies" satisfiesExpr = Expression()
1446 {
1447 qc.setSatisfiesExpr(satisfiesExpr);
1448 qc.setQuantifiedList(quantifiedList);
1449 removeCurrentScope();
1450 return qc;
1451 }
1452}
1453
1454TOKEN_MGR_DECLS:
1455{
1456 public int commentDepth = 0;
1457}
1458
1459<DEFAULT>
1460TOKEN :
1461{
1462 <CARET : "^" >
1463}
1464
1465<DEFAULT>
1466TOKEN :
1467{
1468 <DATASET : "dataset" >
1469}
1470
1471<DEFAULT>
1472TOKEN :
1473{
1474 <LEFTPAREN : "(" >
1475}
1476
1477<DEFAULT>
1478TOKEN :
1479{
1480 <RIGHTPAREN : ")" >
1481}
1482
1483
1484<DEFAULT>
1485TOKEN :
1486{
1487 <INTEGER_LITERAL : (<DIGIT>)+ >
1488}
1489
1490
1491<DEFAULT>
1492TOKEN :
1493{
1494 <NULL : "null">
1495}
1496
1497<DEFAULT>
1498TOKEN :
1499{
1500 <TRUE : "true">
1501}
1502
1503<DEFAULT>
1504TOKEN :
1505{
1506 <FALSE : "false">
1507}
1508
1509<DEFAULT>
1510TOKEN :
1511{
1512 <#DIGIT : ["0" - "9"]>
1513}
1514
1515
1516TOKEN:
1517{
1518 < DOUBLE_LITERAL: <INTEGER>
1519 | <INTEGER> ( "." <INTEGER> )?
1520 | "." <INTEGER>
1521 >
1522 |
1523 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
1524 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
1525 | "." <INTEGER> ( "f" | "F" )
1526 >
1527 |
1528 <INTEGER : (<DIGIT>)+ >
1529}
1530
1531<DEFAULT>
1532TOKEN :
1533{
1534 <#LETTER : ["A" - "Z", "a" - "z"]>
1535}
1536
1537<DEFAULT>
1538TOKEN :
1539{
1540 <SPECIALCHARS : ["$", "_", "-"] >
1541}
1542
1543<DEFAULT>
1544TOKEN :
1545{
1546 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
1547 |
1548 < #EscapeQuot: "\\\"" >
1549 |
1550 < #EscapeApos: "\\\'" >
1551}
1552
1553<DEFAULT>
1554TOKEN :
1555{
1556 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
1557}
1558
1559<DEFAULT>
1560TOKEN :
1561{
1562 <VARIABLE : "$" <IDENTIFIER> >
1563}
1564
1565<DEFAULT>
1566TOKEN :
1567{
1568 <METAVARIABLECLAUSE : "#" <IDENTIFIER> >
1569}
1570
1571<DEFAULT>
1572TOKEN :
1573{
1574 <METAVARIABLE : "$$" <IDENTIFIER> >
1575}
1576
1577SKIP:
1578{
1579 " "
1580| "\t"
1581| "\r"
1582| "\n"
1583}
1584
1585SKIP:
1586{
1587 <"//" (~["\n"])* "\n">
1588}
1589
1590SKIP:
1591{
1592 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
1593}
1594
1595
1596SKIP:
1597{
1598 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
1599}
1600
1601<INSIDE_COMMENT>
1602SPECIAL_TOKEN:
1603{
1604 <"+"(" ")*(~["/","*"])*>
1605}
1606
1607<INSIDE_COMMENT>
1608SKIP:
1609{
1610 <"/*"> {commentDepth++;}
1611}
1612
1613<INSIDE_COMMENT>
1614SKIP:
1615{
1616 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
1617| <~[]>
1618}