blob: 0767c4ecb9fc700c55e14fe05ae80361bb6d1a25 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package edu.uci.ics.asterix.aql.parser;
12
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17
18import java.util.Map;
19import java.util.HashMap;
20import edu.uci.ics.asterix.aql.literal.FloatLiteral;
21import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
22import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000023import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000024import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000025import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000026import edu.uci.ics.asterix.aql.literal.NullLiteral;
27import edu.uci.ics.asterix.aql.literal.StringLiteral;
28import edu.uci.ics.asterix.aql.literal.TrueLiteral;
29
30import edu.uci.ics.asterix.aql.base.*;
31import edu.uci.ics.asterix.aql.expression.*;
32import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
33import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
34import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
35import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
36import edu.uci.ics.asterix.aql.base.Statement.Kind;
37import edu.uci.ics.asterix.aql.context.Scope;
38import edu.uci.ics.asterix.aql.context.RootScopeFactory;
39import edu.uci.ics.asterix.common.annotations.*;
40import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000041import edu.uci.ics.asterix.om.functions.AsterixFunction;
vinayakb38b7ca42012-03-05 05:44:15 +000042
43
44public class AQLParser extends ScopeChecker {
45
46/*
47 private void printHints(Token t) {
48 //System.err.println("token="+t.image+"\t special="+t.specialToken);
49 if (t.specialToken == null) return;
50 Token tmp_t = t.specialToken;
51 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
52 while (tmp_t != null) {
53 System.out.println(tmp_t.image);
54 tmp_t = tmp_t.next;
55 }
56 }
57*/
58
59 // optimizer hints
60 private static final String HASH_GROUP_BY_HINT = "hash";
61 private static final String BROADCAST_JOIN_HINT = "bcast";
62 private static final String INMEMORY_HINT = "inmem";
63 private static final String VAL_FILE_HINT = "val-files";
64 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
65 private static final String INTERVAL_HINT = "interval";
66 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
67 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
68 private static final String LIST_VAL_FILE_HINT = "list-val-file";
69 private static final String LIST_HINT = "list";
70 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
71 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
72 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
73 private static final String AUTO_HINT = "auto";
74
75 private static final String GEN_FIELDS_HINT = "gen-fields";
76
77 // data generator hints
78 private static final String DGEN_HINT = "dgen";
79
80 private static String getHint(Token t) {
81 if (t.specialToken == null) {
82 return null;
83 }
84 String s = t.specialToken.image;
85 int n = s.length();
86 if (n < 2) {
87 return null;
88 }
89 return s.substring(1).trim();
90 }
91
92 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
93 File file = new File(args[0]);
94 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
95 AQLParser parser = new AQLParser(fis);
96 Statement st = parser.Statement();
97 st.accept(new AQLPrintVisitor(), 0);
98 }
99
100
101}
102
103PARSER_END(AQLParser)
104
105
106Statement Statement() throws ParseException:
107{
108 Query query = null;
109 scopeStack.push(RootScopeFactory.createRootScope(this));
110 List<Statement> decls = new ArrayList<Statement>();
111}
112{
113 (
114 (
115 (
116 "use"
117 {
118 decls.add(DataverseDeclaration());
119 }
120 | "declare" "function" {
121 decls.add(FunctionDeclaration());
122 }
123 | "create" (
124 {
125 String hint = getHint(token);
126 boolean dgen = false;
127 if (hint != null && hint.startsWith(DGEN_HINT)) {
128 dgen = true;
129 }
130 }
131 "type"
132 {
133 decls.add(TypeDeclaration(dgen, hint));
134 }
135 | "nodegroup"
136 {
137 decls.add(NodegroupDeclaration());
138 }
139 | "external" <DATASET>
140 {
141 decls.add(DatasetDeclaration(DatasetType.EXTERNAL));
142 }
143 | "feed" <DATASET>
144 {
145 decls.add(DatasetDeclaration(DatasetType.FEED));
146 }
147 | <DATASET>
148 {
149 decls.add(DatasetDeclaration(DatasetType.INTERNAL));
150 }
151 | "index"
152 {
153 decls.add(CreateIndexStatement());
154 }
155 | "dataverse"
156 {
157 decls.add(CreateDataverseStatement());
158 }
159 | "function"
160 {
161 decls.add(FunctionCreation());
162 }
163 )
164 | "load" {
165 decls.add(LoadStatement());
166 }
167
168 | "drop"
169 (
170 <DATASET>
171 {
172 decls.add(DropStatement());
173 }
174 | "index"
175 {
176 decls.add(IndexDropStatement());
177 }
178 | "nodegroup"
179 {
180 decls.add(NodeGroupDropStatement());
181 }
182 | "type"
183 {
184 decls.add(TypeDropStatement());
185 }
186 | "dataverse"
187 {
188 decls.add(DataverseDropStatement());
189 }
190 )
191 | "write" {
192 decls.add(WriteStatement());
193 }
194 | "set" {
195 decls.add(SetStatement());
196 }
197 | "insert" {
198 decls.add(InsertStatement());
199 }
200 | "delete" {
201 decls.add(DeleteStatement());
202 }
203 | "update" {
204 decls.add(UpdateStatement());
205 }
206 | "begin" "feed" <IDENTIFIER> {
207 Identifier datasetName = new Identifier(token.image);
208 decls.add(new BeginFeedStatement(datasetName, getVarCounter()));
209 } ";"
210 | "suspend" "feed" <IDENTIFIER> {
211 datasetName = new Identifier(token.image);
212 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, datasetName));
213 } ";"
214 | "resume" "feed" <IDENTIFIER> {
215 datasetName = new Identifier(token.image);
216 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, datasetName));
217 } ";"
218 | "end" "feed" <IDENTIFIER> {
219 datasetName = new Identifier(token.image);
220 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.END, datasetName));
221 } ";"
222 | "alter" "feed" <IDENTIFIER> {
223 datasetName = new Identifier(token.image);
224 decls.add(AlterFeedDeclaration(datasetName));
225 }
226
227 )*
228 (query = Query())?
229 )
230
231 <EOF>
232 )
233 {
234 if (query == null) {
235 query = new Query(true);
236 }
237 query.setPrologDeclList(decls);
238
239 return query;
240 }
241}
242
243InsertStatement InsertStatement() throws ParseException:
244{
245 Identifier datasetName;
246 Query query;
247}
248{
249 "into" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
250 <LEFTPAREN> query = Query() <RIGHTPAREN> ";"
251 {return new InsertStatement(datasetName, query, getVarCounter());}
252}
253
254DeleteStatement DeleteStatement() throws ParseException:
255{
256 VariableExpr var = null;
257 Identifier datasetName = null;
258 Expression condition = null;
259 Clause dieClause = null;
260}
261{
262 var = Variable() { getCurrentScope().addNewVarSymbolToScope(var.getVar()); }
263 "from" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
264 ("where" condition = Expression())? (dieClause = DieClause())? ";"
265 {return new DeleteStatement(var, datasetName, condition, dieClause, getVarCounter()); }
266}
267
268UpdateStatement UpdateStatement() throws ParseException:
269{
270 VariableExpr vars;
271 Expression target;
272 Expression condition;
273 UpdateClause uc;
274 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
275}
276{
277 vars = Variable() "in" target = Expression()
278 "where" condition = Expression()
279 <LEFTPAREN> (uc=UpdateClause() {ucs.add(uc); } ("," uc=UpdateClause() {ucs.add(uc); } )*) <RIGHTPAREN> ";"
280 {return new UpdateStatement(vars, target, condition, ucs);}
281}
282
283
284
285UpdateClause UpdateClause() throws ParseException:
286{
287 Expression target = null;
288 Expression value = null ;
289 InsertStatement is = null;
290 DeleteStatement ds = null;
291 UpdateStatement us = null;
292 Expression condition = null;
293 UpdateClause ifbranch = null;
294 UpdateClause elsebranch = null;
295}
296{
297 "set" target = Expression() ":=" value = Expression()
298 | "insert" is = InsertStatement()
299 | "delete" ds = DeleteStatement()
300 | "update" us = UpdateStatement()
301 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN> "then" ifbranch = UpdateClause() [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
302 {return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);}
303}
304
305
306Statement SetStatement() throws ParseException:
307{
308 String pn = null;
309 Statement stmt = null;
310}
311{
312 <IDENTIFIER> { pn = token.image; }
313 <STRING_LITERAL>
314 { String pv = removeQuotesAndEscapes(token.image); }
315 ";"
316 {
317 return new SetStatement(pn, pv);
318 }
319}
320
321Statement WriteStatement() throws ParseException:
322{
323 Identifier nodeName = null;
324 String fileName = null;
325 Identifier datasetName = null;
326 Statement stmt = null;
327 Query query;
328 String writerClass = null;
329}
330{
331 (( "output" "to"
332 <IDENTIFIER> { nodeName = new Identifier(token.image); }
333 ":" <STRING_LITERAL> { fileName = removeQuotesAndEscapes(token.image); }
334 ( "using" <STRING_LITERAL> { writerClass = removeQuotesAndEscapes(token.image); } )?
335 {
336 stmt = new WriteStatement(nodeName, fileName, writerClass);
337 } )
338 |
339 ( "into"
340 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
341 <LEFTPAREN> query = Query() <RIGHTPAREN>
342 {
343 stmt = new WriteFromQueryResultStatement(datasetName, query, getVarCounter());
344 } ))
345
346 ";"
347 {
348 return stmt;
349 }
350}
351
352CreateIndexStatement CreateIndexStatement() throws ParseException:
353{
354 CreateIndexStatement cis = new CreateIndexStatement();
355}
356{
357 <IDENTIFIER> { cis.setIndexName(new Identifier(token.image)); }
358 (
359 "if not exists"
360 {
361 cis.setIfNotExists(true);
362 }
363 )?
364 "on"
365 <IDENTIFIER> { cis.setDatasetName(new Identifier(token.image)); }
366 <LEFTPAREN>
367 ( <IDENTIFIER> { cis.addFieldExpr(token.image); } )
368 ("," <IDENTIFIER> { cis.addFieldExpr(token.image); })*
369 <RIGHTPAREN>
370 ("type"
371 ("btree" { cis.setIndexType(IndexType.BTREE); }
alexander.behmc576c602012-07-06 02:41:15 +0000372 | "keyword" { cis.setIndexType(IndexType.WORD_INVIX); }
373 | "rtree" { cis.setIndexType(IndexType.RTREE); }
374 | "ngram"
375 <LEFTPAREN>
376 (<INTEGER_LITERAL>
377 {
378 cis.setIndexType(IndexType.NGRAM_INVIX);
379 cis.setGramLength(Integer.valueOf(token.image));
380 }
381 )
382 <RIGHTPAREN>
383 )
vinayakb38b7ca42012-03-05 05:44:15 +0000384 ";"
385 | ";"
386 )
387 {
388 return cis;
389 }
390}
391
392DataverseDecl DataverseDeclaration() throws ParseException:
393{
394 Identifier dvName = null;
395}
396{
397 "dataverse" <IDENTIFIER> { dvName = new Identifier(token.image); }
398 ";"
399 {
400 return new DataverseDecl(dvName);
401 }
402}
403
404DropStatement DropStatement() throws ParseException :
405{
406 Identifier datasetName = null;
407 boolean ifExists = false;
408}
409{
410 < IDENTIFIER >
411 {
412 datasetName = new Identifier(token.image);
413 }
414 (
415 "if exists"
416 {
417 ifExists = true;
418 }
419 )? ";"
420 {
421 return new DropStatement(datasetName, ifExists);
422 }
423}
424
425IndexDropStatement IndexDropStatement() throws ParseException :
426{
427 Identifier datasetName = null;
428 Identifier indexName = null;
429 boolean ifExists = false;
430}
431{
432 < IDENTIFIER >
433 {
434 datasetName = new Identifier(token.image);
435 }
436 "." < IDENTIFIER >
437 {
438 indexName = new Identifier(token.image);
439 }
440 (
441 "if exists"
442 {
443 ifExists = true;
444 }
445 )? ";"
446 {
447 return new IndexDropStatement(datasetName, indexName, ifExists);
448 }
449}
450
451NodeGroupDropStatement NodeGroupDropStatement() throws ParseException :
452{
453 Identifier groupName = null;
454 boolean ifExists = false;
455}
456{
457 < IDENTIFIER >
458 {
459 groupName = new Identifier(token.image);
460 }
461 (
462 "if exists"
463 {
464 ifExists = true;
465 }
466 )? ";"
467 {
468 return new NodeGroupDropStatement(groupName, ifExists);
469 }
470}
471
472TypeDropStatement TypeDropStatement() throws ParseException :
473{
474 Identifier typeName = null;
475 boolean ifExists = false;
476}
477{
478 < IDENTIFIER >
479 {
480 typeName = new Identifier(token.image);
481 }
482 (
483 "if exists"
484 {
485 ifExists = true;
486 }
487 )? ";"
488 {
489 return new TypeDropStatement(typeName, ifExists);
490 }
491}
492
493DataverseDropStatement DataverseDropStatement() throws ParseException :
494{
495 Identifier dataverseName = null;
496 boolean ifExists = false;
497}
498{
499 < IDENTIFIER >
500 {
501 dataverseName = new Identifier(token.image);
502 }
503 (
504 "if exists"
505 {
506 ifExists = true;
507 }
508 )? ";"
509 {
510 return new DataverseDropStatement(dataverseName, ifExists);
511 }
512}
513
514CreateDataverseStatement CreateDataverseStatement() throws ParseException :
515{
516 Identifier dvName = null;
517 boolean ifNotExists = false;
518 String format = null;
519}
520{
521 < IDENTIFIER >
522 {
523 dvName = new Identifier(token.image);
524 }
525 (
526 "if not exists"
527 {
528 ifNotExists = true;
529 }
530 )?
531 (
532 "with format" < STRING_LITERAL >
533 {
534 format = removeQuotesAndEscapes(token.image);
535 }
536 )?
537 ";"
538 {
539 return new CreateDataverseStatement(dvName, format, ifNotExists);
540 }
541}
542
543LoadFromFileStatement LoadStatement() throws ParseException:
544{
545 Identifier datasetName = null;
546 boolean alreadySorted = false;
547 String adapterClassname;
548 Map<String,String> properties;
549}
550{
551 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
552
553 "using"
554
555 <STRING_LITERAL>
556 {
557 adapterClassname = removeQuotesAndEscapes(token.image);
558 }
559
560 {
561 properties = getConfiguration();
562 }
563
564 ("pre-sorted"
565 { alreadySorted = true; }
566 )?
567
568 ";"
569 {
570 return new LoadFromFileStatement(datasetName, adapterClassname, properties, alreadySorted);
571 }
572}
573
574
575
576DatasetDecl DatasetDeclaration(DatasetType datasetType) throws ParseException :
577{
578 DatasetDecl dd = null;
579 Identifier datasetName = null;
580 Identifier itemTypeName = null;
581 boolean ifNotExists = false;
582 IDatasetDetailsDecl idd = null;
583}
584{
585 < IDENTIFIER >
586 {
587 datasetName = new Identifier(token.image);
588 }
589 (
590 "if not exists"
591 {
592 ifNotExists = true;
593 }
594 )?
595 (
596 < LEFTPAREN > < IDENTIFIER >
597 {
598 itemTypeName = new Identifier(token.image);
599 }
600 < RIGHTPAREN >
601 )?
602 {
603 if(datasetType == DatasetType.INTERNAL) {
604 idd = InternalDatasetDeclaration();
605 dd = new DatasetDecl(datasetName, itemTypeName, idd, ifNotExists);
606 }
607 else if(datasetType == DatasetType.EXTERNAL) {
608 idd = ExternalDatasetDeclaration();
609 dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
610 }
611 else if(datasetType == DatasetType.FEED) {
612 idd = FeedDatasetDeclaration();
613 dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
614 }
615 dd.setDatasetType(datasetType);
616 }
617 {
618 return dd;
619 }
620}
621
622InternalDetailsDecl InternalDatasetDeclaration() throws ParseException :
623{
624 InternalDetailsDecl idd = null;
625}
626{
627 {
628 idd = new InternalDetailsDecl();
629 }
630 "partitioned" "by" "key"
631 < IDENTIFIER >
632 {
633 idd.addPartitioningExpr(token.image);
634 }
635 (
636 "," < IDENTIFIER >
637 {
638 idd.addPartitioningExpr(token.image);
639 }
640 )*
641 (
642 "on" < IDENTIFIER >
643 {
644 idd.setNodegroupName(new Identifier(token.image));
645 }
646 )?
647 ";"
648 {
649 return idd;
650 }
651}
652
653ExternalDetailsDecl ExternalDatasetDeclaration() throws ParseException :
654{
655 ExternalDetailsDecl edd = null;
656 String adapterClassname = null;
657 Map < String, String > properties;
658}
659{
660 {
661 edd = new ExternalDetailsDecl();
662 }
663
664 "using"
665
666 <STRING_LITERAL>
667 {
668 adapterClassname = removeQuotesAndEscapes(token.image);
669 }
670
671 {
672 properties = getConfiguration();
673 }
674
675 {
676 edd = new ExternalDetailsDecl();
677 edd.setAdapter(adapterClassname);
678 edd.setProperties(properties);
679 }
680 ";"
681
682 {
683 return edd;
684 }
685}
686
687FeedDetailsDecl FeedDatasetDeclaration() throws ParseException :
688{
689 FeedDetailsDecl fdd = null;
690 String adapterClassname = null;
691 Map < String, String > properties;
692}
693{
694 {
695 fdd = new FeedDetailsDecl();
696 }
697
698 "using"
699
700 <STRING_LITERAL>
701 {
702 adapterClassname = removeQuotesAndEscapes(token.image);
703 }
704
705 {
706 properties = getConfiguration();
707 }
708
709 ("apply" "function"
710 < IDENTIFIER >
711 {
712 fdd.setFunctionIdentifier(token.image);
713 }
714 )?
715
716 "partitioned" "by" "key"
717 < IDENTIFIER >
718 {
719 fdd.addPartitioningExpr(token.image);
720 }
721 (
722 "," < IDENTIFIER >
723 {
724 fdd.addPartitioningExpr(token.image);
725 }
726 )*
727 (
728 "on" < IDENTIFIER >
729 {
730 fdd.setNodegroupName(new Identifier(token.image));
731 }
732 )?
733 ";"
734 {
735 fdd.setAdapterClassname(adapterClassname);
736 fdd.setProperties(properties);
737 return fdd;
738 }
739}
740
741ControlFeedStatement AlterFeedDeclaration(Identifier datasetName) throws ParseException :
742{
743 String name = null;
744 String value = null;
745 Map < String, String > configuration = new HashMap < String, String > ();
746}
747{
748 "set"
749 {
750 configuration = getConfiguration();
751 }
752 ";"
753 {
754 return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, datasetName, configuration);
755 }
756}
757
758Map<String,String> getConfiguration() throws ParseException :
759{
760 Map<String,String> configuration = new HashMap<String,String>();
761 String key;
762 String value;
763}
764{
765
766<LEFTPAREN>
767 (
768 (
769 <LEFTPAREN>
770 (
771 <STRING_LITERAL>
772 {
773 key = removeQuotesAndEscapes(token.image);
774 }
775 "=" <STRING_LITERAL>
776 {
777 value = removeQuotesAndEscapes(token.image);
778 }
779 )
780 <RIGHTPAREN>
781 {
782 configuration.put(key, value);
783 }
784 )
785 (
786 "," <LEFTPAREN>
787 (
788 <STRING_LITERAL>
789 {
790 key = removeQuotesAndEscapes(token.image);
791 }
792 "=" <STRING_LITERAL>
793 {
794 value = removeQuotesAndEscapes(token.image);
795 }
796 )
797 <RIGHTPAREN>
798 {
799 configuration.put(key, value);
800 }
801 )*
802 )?
803 <RIGHTPAREN>
804 {
805 return configuration;
806 }
807}
808
809
810NodegroupDecl NodegroupDeclaration() throws ParseException :
811{
812 Identifier name = null;
813 List < Identifier > ncNames = new ArrayList < Identifier > ();
814 boolean ifNotExists = false;
815}
816{
817 < IDENTIFIER >
818 {
819 name = new Identifier(token.image);
820 }
821 (
822 "if not exists"
823 {
824 ifNotExists = true;
825 }
826 )?
827 "on" < IDENTIFIER >
828 {
829 ncNames.add(new Identifier(token.image));
830 }
831 (
832 "," < IDENTIFIER >
833 {
834 ncNames.add(new Identifier(token.image));
835 }
836 )*
837 ";"
838 {
839 return new NodegroupDecl(name, ncNames, ifNotExists);
840 }
841}
842
843
844TypeDecl TypeDeclaration(boolean dgen, String hint) throws ParseException:
845{
846 Identifier ident;
847 TypeExpression typeExpr;
848 boolean ifNotExists = false;
849}
850{
851 <IDENTIFIER>
852 {
853 ident = new Identifier(token.image.toString());
854 }
855 (
856 "if not exists"
857 {
858 ifNotExists = true;
859 }
860 )?
861 "as"
862 ( typeExpr = TypeExpr() )
863 {
864 long numValues = -1;
865 String filename = null;
866 if (dgen) {
867 String splits[] = hint.split(" +");
868 if (splits.length != 3) {
869 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
870 }
871 filename = splits[1];
872 numValues = Long.parseLong(splits[2]);
873 }
874 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
875 return new TypeDecl(ident, typeExpr, tddg, ifNotExists);
876 }
877}
878
879TypeExpression TypeExpr() throws ParseException:
880{
881 TypeExpression typeExpr = null;
882}
883{
884 (
885 typeExpr = RecordTypeDef()
886 | typeExpr = TypeReference()
887 | typeExpr = OrderedListTypeDef()
888 | typeExpr = UnorderedListTypeDef()
889 )
890 {
891 return typeExpr;
892 }
893}
894
895RecordTypeDefinition RecordTypeDef() throws ParseException:
896{
897 RecordTypeDefinition recType = new RecordTypeDefinition();
898 RecordTypeDefinition.RecordKind recordKind = null;
899}
900{
901 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
902 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
903 "{"
904 {
905 String hint = getHint(token);
906 if (hint != null) {
907 String splits[] = hint.split(" +");
908 if (splits[0].equals(GEN_FIELDS_HINT)) {
909 if (splits.length != 5) {
910 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
911 }
912 if (!splits[1].equals("int")) {
913 throw new ParseException("The only supported type for gen-fields is int.");
914 }
915 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
916 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
917 recType.setUndeclaredFieldsDataGen(ufdg);
918 }
919 }
920
921 }
922 (
923 RecordField(recType)
924 ( "," RecordField(recType) )*
925 )?
926 "}"
927 {
928 if (recordKind == null) {
929 recordKind = RecordTypeDefinition.RecordKind.OPEN;
930 }
931 recType.setRecordKind(recordKind);
932 return recType;
933 }
934}
935
936void RecordField(RecordTypeDefinition recType) throws ParseException:
937{
938 String fieldName;
939 TypeExpression type = null;
940 boolean nullable = false;
941}
942{
943 <IDENTIFIER>
944 {
945 Token t = getToken(0);
946 fieldName = t.toString();
947 String hint = getHint(t);
948 IRecordFieldDataGen rfdg = null;
949 if (hint != null) {
950 String splits[] = hint.split(" +");
951 if (splits[0].equals(VAL_FILE_HINT)) {
952 File[] valFiles = new File[splits.length - 1];
953 for (int k=1; k<splits.length; k++) {
954 valFiles[k-1] = new File(splits[k]);
955 }
956 rfdg = new FieldValFileDataGen(valFiles);
957 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
958 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
959 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
960 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
961 } else if (splits[0].equals(LIST_HINT)) {
962 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
963 } else if (splits[0].equals(INTERVAL_HINT)) {
964 FieldIntervalDataGen.ValueType vt;
965 if (splits[1].equals("int")) {
966 vt = FieldIntervalDataGen.ValueType.INT;
967 } else if (splits[1].equals("long")) {
968 vt = FieldIntervalDataGen.ValueType.LONG;
969 } else if (splits[1].equals("float")) {
970 vt = FieldIntervalDataGen.ValueType.FLOAT;
971 } else if (splits[1].equals("double")) {
972 vt = FieldIntervalDataGen.ValueType.DOUBLE;
973 } else {
974 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
975 }
976 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
977 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
978 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
979 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
980 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
981 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
982 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
983 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
984 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
985 } else if (splits[0].equals(AUTO_HINT)) {
986 rfdg = new AutoDataGen(splits[1]);
987 }
988 }
989 }
990 ":"
991 ( type = TypeExpr() )
992 ("?" { nullable = true; } )?
993 {
994 recType.addField(fieldName, type, nullable, rfdg);
995 }
996}
997
998TypeReferenceExpression TypeReference() throws ParseException:
999{}
1000{
1001 <IDENTIFIER>
1002 {
1003 Token t = getToken(0);
1004 Identifier id = new Identifier(t.toString());
1005 return new TypeReferenceExpression(id);
1006 }
1007}
1008
1009OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1010{
1011 TypeExpression type = null;
1012}
1013{
1014 "["
1015 ( type = TypeExpr() )
1016 "]"
1017 {
1018 return new OrderedListTypeDefinition(type);
1019 }
1020}
1021
1022
1023UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1024{
1025 TypeExpression type = null;
1026}
1027{
1028 "{{"
1029 ( type = TypeExpr() )
1030 "}}"
1031 {
1032 return new UnorderedListTypeDefinition(type);
1033 }
1034}
1035
1036
1037FunctionDecl FunctionDeclaration() throws ParseException:
1038{
1039 FunctionDecl func = new FunctionDecl();
ramangrover29a13f2422012-03-15 23:01:27 +00001040 AsterixFunction ident;
1041 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001042 int arity = 0;
1043 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1044 Expression funcBody;
1045 VarIdentifier var = null;
1046 createNewScope();
1047}
1048{
1049
1050 <IDENTIFIER>
1051 {
1052 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001053 functionName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001054 }
1055 <LEFTPAREN> (<VARIABLE>
1056 {
1057 var = new VarIdentifier();
1058 var.setValue(getToken(0).toString());
1059 paramList.add(var);
1060 getCurrentScope().addNewVarSymbolToScope(var);
1061 arity++;
1062 }
1063 ("," <VARIABLE>
1064 {
1065 var = new VarIdentifier();
1066 var.setValue(getToken(0).toString());
1067 paramList.add(var);
1068 getCurrentScope().addNewVarSymbolToScope(var);
1069 arity++;
1070 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
1071
1072 {
ramangrover29a13f2422012-03-15 23:01:27 +00001073 ident = new AsterixFunction(functionName,arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001074 getCurrentScope().addFunctionDescriptor(ident, false);
1075 func.setIdent(ident);
1076 func.setFuncBody(funcBody);
1077 func.setParamList(paramList);
1078 return func;
1079 }
1080}
1081
1082CreateFunctionStatement FunctionCreation() throws ParseException:
1083{
1084 CreateFunctionStatement cfs = null;
ramangrover29a13f2422012-03-15 23:01:27 +00001085 AsterixFunction ident;
1086 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001087 int arity = 0;
1088 boolean ifNotExists = false;
1089 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1090 String funcBody;
1091 VarIdentifier var = null;
1092 createNewScope();
1093}
1094{
1095
1096 <IDENTIFIER>
1097 {
1098 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001099 functionName= t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001100 }
1101
1102 (
1103 "if not exists"
1104 {
1105 ifNotExists = true;
1106 }
1107 )?
1108
1109 <LEFTPAREN> (<VARIABLE>
1110 {
1111 var = new VarIdentifier();
1112 var.setValue(getToken(0).toString());
1113 paramList.add(var);
1114 getCurrentScope().addNewVarSymbolToScope(var);
1115 arity++;
1116 }
1117 ("," <VARIABLE>
1118 {
1119 var = new VarIdentifier();
1120 var.setValue(getToken(0).toString());
1121 paramList.add(var);
1122 getCurrentScope().addNewVarSymbolToScope(var);
1123 arity++;
1124 })*)? <RIGHTPAREN> "{" <STRING_LITERAL>
1125 {
1126 funcBody = removeQuotesAndEscapes(token.image);
1127 }
1128 "}"
1129 {
ramangrover29a13f2422012-03-15 23:01:27 +00001130 ident = new AsterixFunction(functionName, arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001131 getCurrentScope().addFunctionDescriptor(ident, false);
1132 cfs = new CreateFunctionStatement(ident, paramList, funcBody, ifNotExists);
1133 return cfs;
1134 }
1135}
1136
1137
1138
1139Query Query()throws ParseException:
1140{
1141 Query query = new Query();
1142 Expression expr;
1143}
1144{
1145 expr = Expression()
1146
1147 {
1148 query.setBody(expr);
1149 return query;
1150 }
1151}
1152
1153
1154
1155Expression Expression():
1156{
1157 Expression expr = null;
1158 Expression exprP = null;
1159}
1160{
1161(
1162
1163//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1164 expr = OperatorExpr()
1165 | expr = IfThenElse()
1166 | expr = FLWOGR()
1167 | expr = QuantifiedExpression()
1168
1169
1170)
1171 {
1172 return (exprP==null) ? expr : exprP;
1173 }
1174}
1175
1176
1177
1178Expression OperatorExpr()throws ParseException:
1179{
1180 OperatorExpr op = null;
1181 Expression operand = null;
1182}
1183{
1184 operand = AndExpr()
1185 (
1186
1187 "or"
1188 {
1189 if (op == null) {
1190 op = new OperatorExpr();
1191 op.addOperand(operand);
1192 op.setCurrentop(true);
1193 }
1194 Token t = getToken(0);
1195 op.addOperator(t.toString());
1196 }
1197
1198 operand = AndExpr()
1199 {
1200 op.addOperand(operand);
1201 }
1202
1203 )*
1204
1205 {
1206 return op==null? operand: op;
1207 }
1208}
1209
1210Expression AndExpr()throws ParseException:
1211{
1212 OperatorExpr op = null;
1213 Expression operand = null;
1214}
1215{
1216 operand = RelExpr()
1217 (
1218
1219 "and"
1220 {
1221 if (op == null) {
1222 op = new OperatorExpr();
1223 op.addOperand(operand);
1224 op.setCurrentop(true);
1225 }
1226 Token t = getToken(0);
1227 op.addOperator(t.toString());
1228 }
1229
1230 operand = RelExpr()
1231 {
1232 op.addOperand(operand);
1233 }
1234
1235 )*
1236
1237 {
1238 return op==null? operand: op;
1239 }
1240}
1241
1242
1243
1244Expression RelExpr()throws ParseException:
1245{
1246 OperatorExpr op = null;
1247 Expression operand = null;
1248 boolean broadcast = false;
1249}
1250{
1251 operand = AddExpr()
1252 {
1253 if (operand instanceof VariableExpr) {
1254 String hint = getHint(token);
1255 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1256 broadcast = true;
1257 }
1258 }
1259 }
1260
1261 (
1262 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1263 {
1264 if (op == null) {
1265 op = new OperatorExpr();
1266 op.addOperand(operand, broadcast);
1267 op.setCurrentop(true);
1268 broadcast = false;
1269 }
1270 Token t = getToken(0);
1271 op.addOperator(t.toString());
1272 }
1273
1274 operand = AddExpr()
1275 {
1276 broadcast = false;
1277 if (operand instanceof VariableExpr) {
1278 String hint = getHint(token);
1279 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1280 broadcast = true;
1281 }
1282 }
1283 op.addOperand(operand, broadcast);
1284 }
1285 )?
1286
1287 {
1288 return op==null? operand: op;
1289 }
1290}
1291
1292Expression AddExpr()throws ParseException:
1293{
1294 OperatorExpr op = null;
1295 Expression operand = null;
1296}
1297{
1298 operand = MultExpr()
1299
1300 ( ("+" | "-")
1301 {
1302 if (op == null) {
1303 op = new OperatorExpr();
1304 op.addOperand(operand);
1305 op.setCurrentop(true);
1306 }
1307 Token t = getToken(0);
1308 ((OperatorExpr)op).addOperator(t.toString());
1309 }
1310
1311 operand = MultExpr()
1312 {
1313 op.addOperand(operand);
1314 }
1315 )*
1316
1317 {
1318 return op==null? operand: op;
1319 }
1320}
1321
1322Expression MultExpr()throws ParseException:
1323{
1324 OperatorExpr op = null;
1325 Expression operand = null;
1326}
1327{
1328 operand = UnionExpr()
1329
1330 (( "*" | "/" | "%" | <CARET> | "idiv")
1331 {
1332 if (op == null) {
1333 op = new OperatorExpr();
1334 op.addOperand(operand);
1335 op.setCurrentop(true);
1336 }
1337 Token t = getToken(0);
1338 op.addOperator(t.toString());
1339 }
1340 operand = UnionExpr()
1341 {
1342 op.addOperand(operand);
1343 }
1344 )*
1345
1346 {
1347 return op==null?operand:op;
1348 }
1349}
1350
1351Expression UnionExpr() throws ParseException:
1352{
1353 UnionExpr union = null;
1354 Expression operand1 = null;
1355 Expression operand2 = null;
1356}
1357{
1358 operand1 = UnaryExpr()
1359 ("union"
1360 (operand2 = UnaryExpr()) {
1361 if (union == null) {
1362 union = new UnionExpr();
1363 union.addExpr(operand1);
1364 }
1365 union.addExpr(operand2);
1366 } )*
1367 {
1368 return (union == null)? operand1: union;
1369 }
1370}
1371
1372Expression UnaryExpr() throws ParseException:
1373{
1374 Expression uexpr = null;
1375 Expression expr = null;
1376}
1377{
1378 (( "+"|"-")
1379 {
1380 uexpr = new UnaryExpr();
1381 Token t = getToken(0);
1382 if("+".equals(t.toString()))
1383 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1384 else if("-".equals(t.toString()))
1385 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1386 else
1387 throw new ParseException();
1388 }
1389 )?
1390
1391 expr = ValueExpr()
1392 {
1393 if(uexpr!=null){
1394 ((UnaryExpr)uexpr).setExpr(expr);
1395 return uexpr;
1396 }
1397 else{
1398 return expr;
1399 }
1400 }
1401}
1402
1403Expression ValueExpr() throws ParseException:
1404{
1405 Expression expr;
1406}
1407{
1408 expr = FieldOrIndexAccessor()
1409 {
1410 return expr;
1411 }
1412}
1413
1414
1415Expression FieldOrIndexAccessor()throws ParseException:
1416{
1417 Expression expr = null;
1418 Identifier ident = null;
1419 AbstractAccessor fa = null;
1420 int index;
1421
1422}
1423{
1424 ( expr = PrimaryExpr()
1425
1426 )
1427
1428
1429 (
1430 (
1431 ident = Field()
1432 {
1433 if(fa == null)
1434 fa = new FieldAccessor(expr, ident);
1435 else
1436 fa = new FieldAccessor(fa, ident);
1437 }
1438 )
1439 | (
1440 index = Index()
1441 {
1442 if(fa == null)
1443 fa = new IndexAccessor(expr, index);
1444 else
1445 fa = new IndexAccessor(fa, index);
1446 }
1447 )
1448 )*
1449
1450
1451 {
1452 return fa==null?expr:fa;
1453 }
1454}
1455
1456Identifier Field() throws ParseException:
1457{
1458 Identifier ident = null;
1459
1460}
1461{
1462 "." < IDENTIFIER >
1463 {
1464
1465 ident = new Identifier();
1466 ident.setValue(getToken(0).toString());
1467
1468 return ident;
1469 }
1470}
1471
1472int Index() throws ParseException:
1473{
1474 Expression expr = null;
1475 int idx = -2;
1476}
1477{
1478 "[" ( expr = Expression()
1479 {
1480 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1481 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001482 Literal lit = ((LiteralExpr)expr).getValue();
1483 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1484 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001485 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001486 }
vinayakb38b7ca42012-03-05 05:44:15 +00001487 else {
1488 throw new ParseException("Index should be an INTEGER");
1489 }
1490 }
1491
1492 }
1493
1494 | "?"
1495 {
1496 idx = IndexAccessor.ANY;
1497 // ANY
1498 }
1499
1500 )
1501
1502 "]"
1503 {
1504 return idx;
1505 }
1506}
1507
1508
1509Expression PrimaryExpr()throws ParseException:
1510{
1511 Expression expr = null;
1512}
1513{
ilovesoupc9fef1d2012-07-08 19:30:42 +00001514 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +00001515 (
1516 expr =Literal()
1517 | expr = FunctionCallExpr()
1518 | expr =VariableRef()
1519
1520 {
1521 if(((VariableExpr)expr).getIsNewVar() == true)
1522 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1523 }
1524 | expr = ListConstructor()
1525 | expr = RecordConstructor()
1526 | expr = ParenthesizedExpression()
1527 )
1528 {
1529 return expr;
1530 }
1531}
1532
1533Expression Literal() throws ParseException:
1534{
1535
1536 LiteralExpr lit = new LiteralExpr();
1537 Token t;
1538}
1539{
1540(
1541 <STRING_LITERAL>
1542 {
1543 t= getToken(0);
1544 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
1545 }
1546
1547 | <INTEGER_LITERAL>
1548 {
1549 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +00001550 try {
1551 lit.setValue(new IntegerLiteral(new Integer(t.image)));
1552 } catch(NumberFormatException ex) {
1553 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
1554 }
vinayakb38b7ca42012-03-05 05:44:15 +00001555 }
1556 | < FLOAT_LITERAL >
1557 {
1558 t= getToken(0);
1559 lit.setValue(new FloatLiteral(new Float(t.image)));
1560 }
1561 | < DOUBLE_LITERAL >
1562 {
1563 t= getToken(0);
1564 lit.setValue(new DoubleLiteral(new Double(t.image)));
1565 }
1566 | <NULL>
1567 {
1568 t= getToken(0);
1569 lit.setValue(NullLiteral.INSTANCE);
1570 }
1571 | <TRUE>
1572 {
1573 t= getToken(0);
1574 lit.setValue(TrueLiteral.INSTANCE);
1575 }
1576 | <FALSE>
1577 {
1578 t= getToken(0);
1579 lit.setValue(FalseLiteral.INSTANCE);
1580 }
1581)
1582 {
1583 return lit;
1584 }
1585}
1586
1587
1588VariableExpr VariableRef() throws ParseException:
1589{
1590 VariableExpr varExp = new VariableExpr();
1591 VarIdentifier var = new VarIdentifier();
1592 Token t;
1593}
1594{
1595 <VARIABLE>
1596 {
1597 t = getToken(0);//get current token
1598 String varName = t.toString();
1599 Identifier ident = lookupSymbol(varName);
1600 if (isInForbiddenScopes(varName)) {
1601 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.");
1602 }
1603 if(ident != null) { // exist such ident
1604 varExp.setIsNewVar(false);
1605 varExp.setVar((VarIdentifier)ident);
1606 } else {
1607 varExp.setVar(var);
1608 }
1609 var.setValue(t.toString());
1610 return varExp;
1611 }
1612}
1613
1614
1615VariableExpr Variable() throws ParseException:
1616{
1617 VariableExpr varExp = new VariableExpr();
1618 VarIdentifier var = new VarIdentifier();
1619 Token t;
1620}
1621{
1622 <VARIABLE>
1623 {
1624 t = getToken(0);//get current token
1625 Identifier ident = lookupSymbol(t.toString());
1626 if(ident != null) { // exist such ident
1627 varExp.setIsNewVar(false);
1628 }
1629 varExp.setVar(var);
1630 var.setValue(t.toString());
1631 return varExp;
1632 }
1633}
1634
1635Expression ListConstructor() throws ParseException:
1636{
1637 Expression expr = null;
1638}
1639{
1640 (
1641 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1642 )
1643
1644 {
1645 return expr;
1646 }
1647}
1648
1649
1650ListConstructor OrderedListConstructor() throws ParseException:
1651{
1652 ListConstructor expr = new ListConstructor();
1653 Expression tmp = null;
1654 List<Expression> exprList = new ArrayList<Expression>();
1655 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1656}
1657{
1658
1659 "["
1660 ( tmp = Expression()
1661 {
1662 exprList.add(tmp);
1663 }
1664
1665 ("," tmp = Expression() { exprList.add(tmp); })*
1666 )?
1667
1668 "]"
1669
1670 {
1671 expr.setExprList(exprList);
1672 return expr;
1673 }
1674}
1675
1676ListConstructor UnorderedListConstructor() throws ParseException:
1677{
1678 ListConstructor expr = new ListConstructor();
1679 Expression tmp = null;
1680 List<Expression> exprList = new ArrayList<Expression>();
1681 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1682}
1683{
1684
1685 "{{" ( tmp = Expression()
1686 {
1687 exprList.add(tmp);
1688 }
1689 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1690 {
1691 expr.setExprList(exprList);
1692 return expr;
1693 }
1694}
1695
1696RecordConstructor RecordConstructor() throws ParseException:
1697{
1698 RecordConstructor expr = new RecordConstructor();
1699 FieldBinding tmp = null;
1700 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1701}
1702{
1703 "{" (tmp = FieldBinding()
1704 {
1705 fbList.add(tmp);
1706 }
1707 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1708 {
1709 expr.setFbList(fbList);
1710 return expr;
1711 }
1712}
1713
1714FieldBinding FieldBinding() throws ParseException:
1715{
1716 FieldBinding fb = new FieldBinding();
1717 Expression left, right;
1718}
1719{
1720 left = Expression() ":" right = Expression()
1721 {
1722 fb.setLeftExpr(left);
1723 fb.setRightExpr(right);
1724 return fb;
1725 }
1726}
1727
1728Expression FunctionCallExpr() throws ParseException:
1729{
1730 CallExpr pf = new CallExpr();
1731 List<Expression > argList = new ArrayList<Expression >();
1732 Expression tmp;
1733 int arity = 0;
1734 Token funcName;
1735}
1736{
1737 ( <IDENTIFIER> | <DATASET> )
1738 {
1739 funcName = getToken(0);
1740 }
1741 <LEFTPAREN> (tmp = Expression()
1742 {
1743 argList.add(tmp);
1744 arity ++;
1745 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1746
1747 {
ramangrover29a13f2422012-03-15 23:01:27 +00001748 AsterixFunction fd = lookupFunctionSignature(funcName.toString(), arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001749 if(fd == null)
1750 {
ramangrover29a13f2422012-03-15 23:01:27 +00001751 fd = new AsterixFunction(funcName.toString(), arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001752// notFoundFunctionList.add(fd);
1753 }
1754// throw new ParseException("can't find function "+ funcName.toString() + "@" + arity);
1755 pf.setIdent(fd);
1756 pf.setExprList(argList);
1757 return pf;
1758 }
1759}
1760
1761
1762Expression ParenthesizedExpression() throws ParseException:
1763{
1764 Expression expr;
1765}
1766{
1767 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1768 {
1769 return expr;
1770 }
1771}
1772
1773Expression IfThenElse() throws ParseException:
1774{
1775 Expression condExpr;
1776 Expression thenExpr;
1777 Expression elseExpr;
1778 IfExpr ifExpr = new IfExpr();
1779}
1780{
1781 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1782
1783 {
1784 ifExpr.setCondExpr(condExpr);
1785 ifExpr.setThenExpr(thenExpr);
1786 ifExpr.setElseExpr(elseExpr);
1787 return ifExpr;
1788 }
1789}
1790
1791Expression FLWOGR() throws ParseException:
1792{
1793 FLWOGRExpression flworg = new FLWOGRExpression();
1794 List<Clause> clauseList = new ArrayList<Clause>();
1795 Expression returnExpr;
1796 Clause tmp;
1797 createNewScope();
1798}
1799{
1800 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1801 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1802
1803 {
1804 flworg.setClauseList(clauseList);
1805 flworg.setReturnExpr(returnExpr);
1806 removeCurrentScope();
1807 return flworg;
1808 }
1809}
1810
1811Clause Clause()throws ParseException :
1812{
1813 Clause clause;
1814}
1815{
1816 (
1817 clause = ForClause()
1818 | clause = LetClause()
1819 | clause = WhereClause()
1820 | clause = OrderbyClause()
1821 | clause = GroupClause()
1822 | clause = LimitClause()
1823 | clause = DistinctClause()
1824 | clause = DieClause()
1825 )
1826 {
1827 return clause;
1828 }
1829}
1830
1831Clause ForClause()throws ParseException :
1832{
1833 ForClause fc = new ForClause();
1834 VariableExpr varExp;
1835 VariableExpr varPos = null;
1836 Expression inExp;
1837 extendCurrentScope();
1838}
1839{
1840 "for" varExp = Variable()
1841 {
1842 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1843 }
1844 ("at" varPos = Variable()
1845 {
1846 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1847 }
1848 )?
1849 "in" ( inExp = Expression() )
1850 {
1851 fc.setVarExpr(varExp);
1852 fc.setInExpr(inExp);
1853 if (varPos != null) {
1854 fc.setPosExpr(varPos);
1855 }
1856 return fc;
1857 }
1858}
1859
1860Clause LetClause() throws ParseException:
1861{
1862 LetClause lc = new LetClause();
1863 VariableExpr varExp;
1864 Expression beExp;
1865 extendCurrentScope();
1866}
1867{
ilovesoupb2527c12012-07-12 03:21:13 +00001868 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001869 {
1870 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001871 lc.setVarExpr(varExp);
1872 lc.setBeExpr(beExp);
1873 return lc;
1874 }
1875}
1876
1877Clause WhereClause()throws ParseException :
1878{
1879 WhereClause wc = new WhereClause();
1880 Expression whereExpr;
1881}
1882{
1883 "where" whereExpr = Expression()
1884 {
1885 wc.setWhereExpr(whereExpr);
1886 return wc;
1887 }
1888}
1889
1890Clause OrderbyClause()throws ParseException :
1891{
1892 OrderbyClause oc = new OrderbyClause();
1893 Expression orderbyExpr;
1894 List<Expression> orderbyList = new ArrayList<Expression>();
1895 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1896 int numOfOrderby = 0;
1897}
1898{
1899 (
1900 "order"
1901 {
1902 String hint = getHint(token);
1903 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1904 String splits[] = hint.split(" +");
1905 int numFrames = Integer.parseInt(splits[1]);
1906 int numTuples = Integer.parseInt(splits[2]);
1907 oc.setNumFrames(numFrames);
1908 oc.setNumTuples(numTuples);
1909 }
1910 }
1911 "by" orderbyExpr = Expression()
1912 {
1913 orderbyList.add(orderbyExpr);
1914 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1915 }
1916 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1917 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1918 {
1919 modifierList.add(modif);
1920 }
1921
1922 ("," orderbyExpr = Expression()
1923 {
1924 orderbyList.add(orderbyExpr);
1925 modif = OrderbyClause.OrderModifier.ASC;
1926 }
1927 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1928 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1929 {
1930 modifierList.add(modif);
1931 }
1932 )*
1933)
1934 {
1935 oc.setModifierList(modifierList);
1936 oc.setOrderbyList(orderbyList);
1937 return oc;
1938 }
1939}
1940Clause GroupClause()throws ParseException :
1941{
1942 GroupbyClause gbc = new GroupbyClause();
1943 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1944 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1945 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1946 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1947 VariableExpr var = null;
1948 VariableExpr withVar = null;
1949 Expression expr = null;
1950 VariableExpr decorVar = null;
1951 Expression decorExpr = null;
1952}
1953{
1954 {
1955 Scope newScope = extendCurrentScopeNoPush(true);
1956 // extendCurrentScope(true);
1957 }
1958 "group"
1959 {
1960 String hint = getHint(token);
1961 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1962 gbc.setHashGroupByHint(true);
1963 }
1964 }
1965 "by" (LOOKAHEAD(2) var = Variable()
1966 {
1967 newScope.addNewVarSymbolToScope(var.getVar());
1968 } ":=")?
1969 expr = Expression()
1970 {
1971 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1972 vePairList.add(pair1);
1973 }
1974 ("," ( LOOKAHEAD(2) var = Variable()
1975 {
1976 newScope.addNewVarSymbolToScope(var.getVar());
1977 } ":=")?
1978 expr = Expression()
1979 {
1980 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1981 vePairList.add(pair2);
1982 }
1983 )*
1984 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1985 {
1986 newScope.addNewVarSymbolToScope(decorVar.getVar());
1987 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1988 decorPairList.add(pair3);
1989 }
1990 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1991 {
1992 newScope.addNewVarSymbolToScope(decorVar.getVar());
1993 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1994 decorPairList.add(pair4);
1995 }
1996 )*
1997 )?
1998 "with" withVar = VariableRef()
1999 {
2000 if(withVar.getIsNewVar()==true)
2001 throw new ParseException("can't find variable " + withVar.getVar());
2002 withVarList.add(withVar);
2003 newScope.addNewVarSymbolToScope(withVar.getVar());
2004 }
2005 ("," withVar = VariableRef()
2006 {
2007 if(withVar.getIsNewVar()==true)
2008 throw new ParseException("can't find variable " + withVar.getVar());
2009 withVarList.add(withVar);
2010 newScope.addNewVarSymbolToScope(withVar.getVar());
2011 })*
2012 {
2013 gbc.setGbyPairList(vePairList);
2014 gbc.setDecorPairList(decorPairList);
2015 gbc.setWithVarList(withVarList);
2016 replaceCurrentScope(newScope);
2017 return gbc;
2018 }
2019}
2020
2021
2022LimitClause LimitClause() throws ParseException:
2023{
2024 LimitClause lc = new LimitClause();
2025 Expression expr;
2026 pushForbiddenScope(getCurrentScope());
2027}
2028{
2029 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2030 ("offset" expr = Expression() { lc.setOffset(expr); })?
2031
2032 {
2033 popForbiddenScope();
2034 return lc;
2035 }
2036}
2037
2038DistinctClause DistinctClause() throws ParseException:
2039{
2040 List<Expression> exprs = new ArrayList<Expression>();
2041 Expression expr;
2042}
2043{
2044 "distinct" "by" expr = Expression()
2045 {
2046 exprs.add(expr);
2047 }
2048 ("," expr = Expression()
2049 {
2050 exprs.add(expr);
2051 }
2052 )*
2053 {
2054 return new DistinctClause(exprs);
2055 }
2056}
2057
2058DieClause DieClause() throws ParseException:
2059{
2060 DieClause lc = new DieClause();
2061 Expression expr;
2062 pushForbiddenScope(getCurrentScope());
2063}
2064{
2065 "die" "after" expr = Expression() { lc.setDieExpr(expr); }
2066 {
2067 popForbiddenScope();
2068 return lc;
2069 }
2070}
2071
2072
2073QuantifiedExpression QuantifiedExpression()throws ParseException:
2074{
2075 QuantifiedExpression qc = new QuantifiedExpression();
2076 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2077 Expression satisfiesExpr;
2078 VariableExpr var;
2079 Expression inExpr;
2080 QuantifiedPair pair;
2081}
2082{
2083 {
2084 createNewScope();
2085 }
2086
2087 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2088 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2089 var = Variable() "in" inExpr = Expression()
2090 {
2091 pair = new QuantifiedPair(var, inExpr);
2092 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2093 quantifiedList.add(pair);
2094 }
2095 (
2096 "," var = Variable() "in" inExpr = Expression()
2097 {
2098 pair = new QuantifiedPair(var, inExpr);
2099 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2100 quantifiedList.add(pair);
2101 }
2102 )*
2103 "satisfies" satisfiesExpr = Expression()
2104 {
2105 qc.setSatisfiesExpr(satisfiesExpr);
2106 qc.setQuantifiedList(quantifiedList);
2107 removeCurrentScope();
2108 return qc;
2109 }
2110}
2111
2112TOKEN_MGR_DECLS:
2113{
2114 public int commentDepth = 0;
2115}
2116
2117<DEFAULT>
2118TOKEN :
2119{
2120 <CARET : "^" >
2121}
2122
2123<DEFAULT>
2124TOKEN :
2125{
2126 <DATASET : "dataset" >
2127}
2128
2129<DEFAULT>
2130TOKEN :
2131{
2132 <LEFTPAREN : "(" >
2133}
2134
2135<DEFAULT>
2136TOKEN :
2137{
2138 <RIGHTPAREN : ")" >
2139}
2140
2141
2142<DEFAULT>
2143TOKEN :
2144{
2145 <INTEGER_LITERAL : (<DIGIT>)+ >
2146}
2147
2148
2149<DEFAULT>
2150TOKEN :
2151{
2152 <NULL : "null">
2153}
2154
2155<DEFAULT>
2156TOKEN :
2157{
2158 <TRUE : "true">
2159}
2160
2161<DEFAULT>
2162TOKEN :
2163{
2164 <FALSE : "false">
2165}
2166
2167<DEFAULT>
2168TOKEN :
2169{
2170 <#DIGIT : ["0" - "9"]>
2171}
2172
2173
2174TOKEN:
2175{
2176 < DOUBLE_LITERAL: <INTEGER>
2177 | <INTEGER> ( "." <INTEGER> )?
2178 | "." <INTEGER>
2179 >
2180 |
2181 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2182 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2183 | "." <INTEGER> ( "f" | "F" )
2184 >
2185 |
2186 <INTEGER : (<DIGIT>)+ >
2187}
2188
2189<DEFAULT>
2190TOKEN :
2191{
2192 <#LETTER : ["A" - "Z", "a" - "z"]>
2193}
2194
2195<DEFAULT>
2196TOKEN :
2197{
2198 <SPECIALCHARS : ["$", "_", "-"] >
2199}
2200
2201<DEFAULT>
2202TOKEN :
2203{
2204 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2205 |
2206 < #EscapeQuot: "\\\"" >
2207 |
2208 < #EscapeApos: "\\\'" >
2209}
2210
2211<DEFAULT>
2212TOKEN :
2213{
2214 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2215}
2216
2217<DEFAULT>
2218TOKEN :
2219{
2220 <VARIABLE : "$" <IDENTIFIER> >
2221}
2222
2223SKIP:
2224{
2225 " "
2226| "\t"
2227| "\r"
2228| "\n"
2229}
2230
2231SKIP:
2232{
2233 <"//" (~["\n"])* "\n">
2234}
2235
2236SKIP:
2237{
2238 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2239}
2240
2241
2242SKIP:
2243{
2244 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2245}
2246
2247<INSIDE_COMMENT>
2248SPECIAL_TOKEN:
2249{
2250 <"+"(" ")*(~["*"])*>
2251}
2252
2253<INSIDE_COMMENT>
2254SKIP:
2255{
2256 <"/*"> {commentDepth++;}
2257}
2258
2259<INSIDE_COMMENT>
2260SKIP:
2261{
2262 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2263| <~[]>
2264}