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