blob: 59a7753cd78d812dfa629aa0049d53982e7d3fb2 [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;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000029import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
vinayakb38b7ca42012-03-05 05:44:15 +000030
31import edu.uci.ics.asterix.aql.base.*;
32import edu.uci.ics.asterix.aql.expression.*;
33import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
34import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
35import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
36import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
37import edu.uci.ics.asterix.aql.base.Statement.Kind;
38import edu.uci.ics.asterix.aql.context.Scope;
39import edu.uci.ics.asterix.aql.context.RootScopeFactory;
40import edu.uci.ics.asterix.common.annotations.*;
41import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000042import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000043import edu.uci.ics.asterix.common.functions.FunctionSignature;
alexander.behm07617fd2012-07-25 10:13:50 +000044import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
45import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000046import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
47import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
48import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
49
50
vinayakb38b7ca42012-03-05 05:44:15 +000051
52
53public class AQLParser extends ScopeChecker {
54
vinayakb38b7ca42012-03-05 05:44:15 +000055 // optimizer hints
56 private static final String HASH_GROUP_BY_HINT = "hash";
57 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000058 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000059 private static final String INMEMORY_HINT = "inmem";
60 private static final String VAL_FILE_HINT = "val-files";
61 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
62 private static final String INTERVAL_HINT = "interval";
63 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
64 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
65 private static final String LIST_VAL_FILE_HINT = "list-val-file";
66 private static final String LIST_HINT = "list";
67 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
68 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
69 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
70 private static final String AUTO_HINT = "auto";
71
72 private static final String GEN_FIELDS_HINT = "gen-fields";
73
74 // data generator hints
75 private static final String DGEN_HINT = "dgen";
76
77 private static String getHint(Token t) {
78 if (t.specialToken == null) {
79 return null;
80 }
81 String s = t.specialToken.image;
82 int n = s.length();
83 if (n < 2) {
84 return null;
85 }
86 return s.substring(1).trim();
87 }
88
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000089 public AQLParser(String s){
90 this(new StringReader(s));
91 super.setInput(s);
92 }
93
vinayakb38b7ca42012-03-05 05:44:15 +000094 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
95 File file = new File(args[0]);
96 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
97 AQLParser parser = new AQLParser(fis);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000098 List<Statement> st = parser.Statement();
99 //st.accept(new AQLPrintVisitor(), 0);
vinayakb38b7ca42012-03-05 05:44:15 +0000100 }
101
102
103}
104
105PARSER_END(AQLParser)
106
107
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000108List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000109{
vinayakb38b7ca42012-03-05 05:44:15 +0000110 scopeStack.push(RootScopeFactory.createRootScope(this));
111 List<Statement> decls = new ArrayList<Statement>();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000112 Query query=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000113}
114{
115 (
116 (
117 (
118 "use"
119 {
120 decls.add(DataverseDeclaration());
121 }
122 | "declare" "function" {
123 decls.add(FunctionDeclaration());
124 }
125 | "create" (
126 {
127 String hint = getHint(token);
128 boolean dgen = false;
129 if (hint != null && hint.startsWith(DGEN_HINT)) {
130 dgen = true;
131 }
132 }
133 "type"
134 {
135 decls.add(TypeDeclaration(dgen, hint));
136 }
137 | "nodegroup"
138 {
139 decls.add(NodegroupDeclaration());
140 }
141 | "external" <DATASET>
142 {
143 decls.add(DatasetDeclaration(DatasetType.EXTERNAL));
144 }
145 | "feed" <DATASET>
146 {
147 decls.add(DatasetDeclaration(DatasetType.FEED));
148 }
149 | <DATASET>
150 {
151 decls.add(DatasetDeclaration(DatasetType.INTERNAL));
152 }
153 | "index"
154 {
155 decls.add(CreateIndexStatement());
156 }
157 | "dataverse"
158 {
159 decls.add(CreateDataverseStatement());
160 }
161 | "function"
162 {
163 decls.add(FunctionCreation());
164 }
165 )
166 | "load" {
167 decls.add(LoadStatement());
168 }
169
170 | "drop"
171 (
172 <DATASET>
173 {
174 decls.add(DropStatement());
175 }
176 | "index"
177 {
178 decls.add(IndexDropStatement());
179 }
180 | "nodegroup"
181 {
182 decls.add(NodeGroupDropStatement());
183 }
184 | "type"
185 {
186 decls.add(TypeDropStatement());
187 }
188 | "dataverse"
189 {
190 decls.add(DataverseDropStatement());
191 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000192 | "function"
193 {
194 decls.add(FunctionDropStatement());
195 }
vinayakb38b7ca42012-03-05 05:44:15 +0000196 )
197 | "write" {
198 decls.add(WriteStatement());
199 }
200 | "set" {
201 decls.add(SetStatement());
202 }
203 | "insert" {
204 decls.add(InsertStatement());
205 }
206 | "delete" {
207 decls.add(DeleteStatement());
208 }
209 | "update" {
210 decls.add(UpdateStatement());
211 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000212 | "begin" "feed"
213 {
214 Pair<Identifier,Identifier> nameComponents = getDotSeparatedPair();
215 decls.add(new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter()));
216 } ";"
217
218 | "suspend" "feed"
219 {
220 decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.SUSPEND));
221 } ";"
222 | "resume" "feed" {
223 decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.RESUME));
vinayakb38b7ca42012-03-05 05:44:15 +0000224 } ";"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000225 | "end" "feed" {
226 decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.END));
vinayakb38b7ca42012-03-05 05:44:15 +0000227 } ";"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000228 | "alter" "feed" {
229 decls.add(AlterFeedDeclaration());
230 } ";"
231
232 | (query = Query()) {
233 decls.add(query);
234 }
235 )*
236 // (query = Query())?
vinayakb38b7ca42012-03-05 05:44:15 +0000237 )
238
239 <EOF>
240 )
241 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000242 return decls;
vinayakb38b7ca42012-03-05 05:44:15 +0000243 }
244}
245
246InsertStatement InsertStatement() throws ParseException:
247{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000248 Identifier dataverseName;
vinayakb38b7ca42012-03-05 05:44:15 +0000249 Identifier datasetName;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000250 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000251 Query query;
252}
253{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000254 "into" <DATASET>
255
256 {
257 nameComponents = getDotSeparatedPair();
258 dataverseName = nameComponents.first;
259 datasetName = nameComponents.second;
260 }
261
262 <LEFTPAREN> query = Query() <RIGHTPAREN> ";"
263 {return new InsertStatement(dataverseName, datasetName, query, getVarCounter());}
vinayakb38b7ca42012-03-05 05:44:15 +0000264}
265
266DeleteStatement DeleteStatement() throws ParseException:
267{
268 VariableExpr var = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000269 Identifier dataverseName;
vinayakb38b7ca42012-03-05 05:44:15 +0000270 Identifier datasetName = null;
271 Expression condition = null;
272 Clause dieClause = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000273 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000274}
275{
276 var = Variable() { getCurrentScope().addNewVarSymbolToScope(var.getVar()); }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000277 "from"
278 <DATASET>
279 {
280 nameComponents = getDotSeparatedPair();
281 }
282 ("where" condition = Expression())? (dieClause = DieClause())? ";"
283 {return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, dieClause, getVarCounter()); }
vinayakb38b7ca42012-03-05 05:44:15 +0000284}
285
286UpdateStatement UpdateStatement() throws ParseException:
287{
288 VariableExpr vars;
289 Expression target;
290 Expression condition;
291 UpdateClause uc;
292 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
293}
294{
295 vars = Variable() "in" target = Expression()
296 "where" condition = Expression()
297 <LEFTPAREN> (uc=UpdateClause() {ucs.add(uc); } ("," uc=UpdateClause() {ucs.add(uc); } )*) <RIGHTPAREN> ";"
298 {return new UpdateStatement(vars, target, condition, ucs);}
299}
300
301
302
303UpdateClause UpdateClause() throws ParseException:
304{
305 Expression target = null;
306 Expression value = null ;
307 InsertStatement is = null;
308 DeleteStatement ds = null;
309 UpdateStatement us = null;
310 Expression condition = null;
311 UpdateClause ifbranch = null;
312 UpdateClause elsebranch = null;
313}
314{
315 "set" target = Expression() ":=" value = Expression()
316 | "insert" is = InsertStatement()
317 | "delete" ds = DeleteStatement()
318 | "update" us = UpdateStatement()
319 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN> "then" ifbranch = UpdateClause() [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
320 {return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);}
321}
322
323
324Statement SetStatement() throws ParseException:
325{
326 String pn = null;
327 Statement stmt = null;
328}
329{
330 <IDENTIFIER> { pn = token.image; }
331 <STRING_LITERAL>
332 { String pv = removeQuotesAndEscapes(token.image); }
333 ";"
334 {
335 return new SetStatement(pn, pv);
336 }
337}
338
339Statement WriteStatement() throws ParseException:
340{
341 Identifier nodeName = null;
342 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000343 Statement stmt = null;
344 Query query;
345 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000346 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000347}
348{
349 (( "output" "to"
350 <IDENTIFIER> { nodeName = new Identifier(token.image); }
351 ":" <STRING_LITERAL> { fileName = removeQuotesAndEscapes(token.image); }
352 ( "using" <STRING_LITERAL> { writerClass = removeQuotesAndEscapes(token.image); } )?
353 {
354 stmt = new WriteStatement(nodeName, fileName, writerClass);
355 } )
356 |
357 ( "into"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000358 <DATASET>
359
360 {
361 nameComponents = getDotSeparatedPair();
362 }
363
vinayakb38b7ca42012-03-05 05:44:15 +0000364 <LEFTPAREN> query = Query() <RIGHTPAREN>
365 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000366 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +0000367 } ))
368
369 ";"
370 {
371 return stmt;
372 }
373}
374
375CreateIndexStatement CreateIndexStatement() throws ParseException:
376{
377 CreateIndexStatement cis = new CreateIndexStatement();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000378 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000379}
380{
381 <IDENTIFIER> { cis.setIndexName(new Identifier(token.image)); }
382 (
383 "if not exists"
384 {
385 cis.setIfNotExists(true);
386 }
387 )?
388 "on"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000389
390 {
391 nameComponents = getDotSeparatedPair();
392 cis.setDataverseName(nameComponents.first);
393 cis.setDatasetName(nameComponents.second);
394 }
395
vinayakb38b7ca42012-03-05 05:44:15 +0000396 <LEFTPAREN>
397 ( <IDENTIFIER> { cis.addFieldExpr(token.image); } )
398 ("," <IDENTIFIER> { cis.addFieldExpr(token.image); })*
399 <RIGHTPAREN>
400 ("type"
alexander.behmb742e092012-11-24 11:54:56 +0000401 ("btree" { cis.setIndexType(IndexType.BTREE); }
alexander.behmc576c602012-07-06 02:41:15 +0000402 | "rtree" { cis.setIndexType(IndexType.RTREE); }
alexander.behmb742e092012-11-24 11:54:56 +0000403 | "keyword" { cis.setIndexType(IndexType.WORD_INVIX); }
404 | "fuzzy keyword" { cis.setIndexType(IndexType.FUZZY_WORD_INVIX); }
alexander.behmc576c602012-07-06 02:41:15 +0000405 | "ngram"
406 <LEFTPAREN>
407 (<INTEGER_LITERAL>
408 {
409 cis.setIndexType(IndexType.NGRAM_INVIX);
410 cis.setGramLength(Integer.valueOf(token.image));
411 }
412 )
alexander.behmb742e092012-11-24 11:54:56 +0000413 <RIGHTPAREN>
414 | "fuzzy ngram"
415 <LEFTPAREN>
416 (<INTEGER_LITERAL>
417 {
418 cis.setIndexType(IndexType.FUZZY_NGRAM_INVIX);
419 cis.setGramLength(Integer.valueOf(token.image));
420 }
421 )
422 <RIGHTPAREN>
alexander.behmc576c602012-07-06 02:41:15 +0000423 )
vinayakb38b7ca42012-03-05 05:44:15 +0000424 ";"
425 | ";"
426 )
427 {
428 return cis;
429 }
430}
431
432DataverseDecl DataverseDeclaration() throws ParseException:
433{
434 Identifier dvName = null;
435}
436{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000437 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000438 ";"
439 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000440 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000441 }
442}
443
444DropStatement DropStatement() throws ParseException :
445{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000446 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000447 Identifier datasetName = null;
448 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000449 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000450}
451{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000452 {
453 nameComponents = getDotSeparatedPair();
454 dataverseName = nameComponents.first;
455 datasetName = nameComponents.second;
456 }
457
458
vinayakb38b7ca42012-03-05 05:44:15 +0000459 (
460 "if exists"
461 {
462 ifExists = true;
463 }
464 )? ";"
465 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000466 return new DropStatement(dataverseName, datasetName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000467 }
468}
469
470IndexDropStatement IndexDropStatement() throws ParseException :
471{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000472 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000473 Identifier datasetName = null;
474 Identifier indexName = null;
475 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000476 Triple<Identifier,Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000477}
478{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000479
vinayakb38b7ca42012-03-05 05:44:15 +0000480 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000481 nameComponents = getDotSeparatedTriple();
482 dataverseName = nameComponents.first;
483 datasetName = nameComponents.second;
484 indexName = nameComponents.third;
485 }
486
vinayakb38b7ca42012-03-05 05:44:15 +0000487 (
488 "if exists"
489 {
490 ifExists = true;
491 }
492 )? ";"
493 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000494 return new IndexDropStatement(dataverseName, datasetName, indexName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000495 }
496}
497
498NodeGroupDropStatement NodeGroupDropStatement() throws ParseException :
499{
500 Identifier groupName = null;
501 boolean ifExists = false;
502}
503{
504 < IDENTIFIER >
505 {
506 groupName = new Identifier(token.image);
507 }
508 (
509 "if exists"
510 {
511 ifExists = true;
512 }
513 )? ";"
514 {
515 return new NodeGroupDropStatement(groupName, ifExists);
516 }
517}
518
519TypeDropStatement TypeDropStatement() throws ParseException :
520{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000521 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000522 Identifier typeName = null;
523 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000524 Pair<Identifier,Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000525}
526{
vinayakb38b7ca42012-03-05 05:44:15 +0000527 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000528 nameComponents = getDotSeparatedPair();
529 dataverseName = nameComponents.first == null ? new Identifier(defaultDataverse) : nameComponents.first;
530 typeName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000531 }
532 (
533 "if exists"
534 {
535 ifExists = true;
536 }
537 )? ";"
538 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000539 return new TypeDropStatement(dataverseName, typeName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000540 }
541}
542
543DataverseDropStatement DataverseDropStatement() throws ParseException :
544{
545 Identifier dataverseName = null;
546 boolean ifExists = false;
547}
548{
549 < IDENTIFIER >
550 {
551 dataverseName = new Identifier(token.image);
552 }
553 (
554 "if exists"
555 {
556 ifExists = true;
557 }
558 )? ";"
559 {
560 return new DataverseDropStatement(dataverseName, ifExists);
561 }
562}
563
564CreateDataverseStatement CreateDataverseStatement() throws ParseException :
565{
566 Identifier dvName = null;
567 boolean ifNotExists = false;
568 String format = null;
569}
570{
571 < IDENTIFIER >
572 {
573 dvName = new Identifier(token.image);
574 }
575 (
576 "if not exists"
577 {
578 ifNotExists = true;
579 }
580 )?
581 (
582 "with format" < STRING_LITERAL >
583 {
584 format = removeQuotesAndEscapes(token.image);
585 }
586 )?
587 ";"
588 {
589 return new CreateDataverseStatement(dvName, format, ifNotExists);
590 }
591}
592
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000593
594FunctionDropStatement FunctionDropStatement() throws ParseException :
595{
596 String dataverse;
597 String functionName;
598 int arity=0;
599 boolean ifExists = false;
600 Pair<Identifier, Identifier> nameComponents=null;
601}
602{
603 {
604 nameComponents = getDotSeparatedPair();
605 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
606 functionName = nameComponents.second.getValue();
607 }
608
609 "@"
610 <INTEGER_LITERAL>
611 {
612 Token t= getToken(0);
613 arity = new Integer(t.image);
614 if( arity < 0 && arity != FunctionIdentifier.VARARGS){
615 throw new ParseException(" invalid arity:" + arity);
616 }
617 }
618
619 (
620 "if exists"
621 {
622 ifExists = true;
623 }
624 )? ";"
625 {
626 return new FunctionDropStatement(new FunctionSignature(dataverse, functionName, arity), ifExists);
627 }
628}
629
630
vinayakb38b7ca42012-03-05 05:44:15 +0000631LoadFromFileStatement LoadStatement() throws ParseException:
632{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000633 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000634 Identifier datasetName = null;
635 boolean alreadySorted = false;
636 String adapterClassname;
637 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000638 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000639}
640{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000641 <DATASET>
642 {
643 nameComponents = getDotSeparatedPair();
644 dataverseName = nameComponents.first;
645 datasetName = nameComponents.second;
646 }
647
vinayakb38b7ca42012-03-05 05:44:15 +0000648 "using"
649
650 <STRING_LITERAL>
651 {
652 adapterClassname = removeQuotesAndEscapes(token.image);
653 }
654
655 {
656 properties = getConfiguration();
657 }
658
659 ("pre-sorted"
660 { alreadySorted = true; }
661 )?
662
663 ";"
664 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000665 return new LoadFromFileStatement(dataverseName, datasetName, adapterClassname, properties, alreadySorted);
vinayakb38b7ca42012-03-05 05:44:15 +0000666 }
667}
668
669
670
671DatasetDecl DatasetDeclaration(DatasetType datasetType) throws ParseException :
672{
673 DatasetDecl dd = null;
674 Identifier datasetName = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000675 Identifier dataverseName = null;
676 Identifier itemDataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000677 Identifier itemTypeName = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000678 String nameComponentFirst = null;
679 String nameComponentSecond = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000680 boolean ifNotExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000681 IDatasetDetailsDecl datasetDetails = null;
682 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000683}
684{
vinayakb38b7ca42012-03-05 05:44:15 +0000685 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000686 nameComponents = getDotSeparatedPair();
687 dataverseName = nameComponents.first;
688 datasetName = nameComponents.second;
689 }
690
vinayakb38b7ca42012-03-05 05:44:15 +0000691 (
692 "if not exists"
693 {
694 ifNotExists = true;
695 }
696 )?
697 (
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000698 < LEFTPAREN > <IDENTIFIER>
vinayakb38b7ca42012-03-05 05:44:15 +0000699 {
700 itemTypeName = new Identifier(token.image);
701 }
702 < RIGHTPAREN >
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000703 )
vinayakb38b7ca42012-03-05 05:44:15 +0000704 {
705 if(datasetType == DatasetType.INTERNAL) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000706 datasetDetails = InternalDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000707 }
708 else if(datasetType == DatasetType.EXTERNAL) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000709 datasetDetails = ExternalDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000710 }
711 else if(datasetType == DatasetType.FEED) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000712 datasetDetails = FeedDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000713 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000714 dd = new DatasetDecl(dataverseName, datasetName, itemTypeName, datasetType, datasetDetails,ifNotExists);
715
vinayakb38b7ca42012-03-05 05:44:15 +0000716 }
717 {
718 return dd;
719 }
720}
721
722InternalDetailsDecl InternalDatasetDeclaration() throws ParseException :
723{
724 InternalDetailsDecl idd = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000725 List<String> partitioningExprs = new ArrayList<String>();
726 Identifier nodeGroupName=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000727}
728{
vinayakb38b7ca42012-03-05 05:44:15 +0000729 "partitioned" "by" "key"
730 < IDENTIFIER >
731 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000732 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000733 }
734 (
735 "," < IDENTIFIER >
736 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000737 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000738 }
739 )*
740 (
741 "on" < IDENTIFIER >
742 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000743 nodeGroupName = new Identifier(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000744 }
745 )?
746 ";"
747 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000748 idd = new InternalDetailsDecl(nodeGroupName, partitioningExprs);
vinayakb38b7ca42012-03-05 05:44:15 +0000749 return idd;
750 }
751}
752
753ExternalDetailsDecl ExternalDatasetDeclaration() throws ParseException :
754{
755 ExternalDetailsDecl edd = null;
756 String adapterClassname = null;
757 Map < String, String > properties;
758}
759{
760 {
761 edd = new ExternalDetailsDecl();
762 }
763
764 "using"
765
766 <STRING_LITERAL>
767 {
768 adapterClassname = removeQuotesAndEscapes(token.image);
769 }
770
771 {
772 properties = getConfiguration();
773 }
774
775 {
776 edd = new ExternalDetailsDecl();
777 edd.setAdapter(adapterClassname);
778 edd.setProperties(properties);
779 }
780 ";"
781
782 {
783 return edd;
784 }
785}
786
787FeedDetailsDecl FeedDatasetDeclaration() throws ParseException :
788{
789 FeedDetailsDecl fdd = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000790 String adapterFactoryClassname = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000791 Map < String, String > properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000792 Pair<Identifier,Identifier> nameComponents;
793 List<String> partitioningExprs = new ArrayList<String>();
794 Identifier nodeGroupName=null;
795 FunctionSignature appliedFunction=null;
796 String dataverse;
797 String functionName;
798 int arity;
vinayakb38b7ca42012-03-05 05:44:15 +0000799}
800{
vinayakb38b7ca42012-03-05 05:44:15 +0000801 "using"
802
803 <STRING_LITERAL>
804 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000805 adapterFactoryClassname = removeQuotesAndEscapes(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000806 }
807
808 {
809 properties = getConfiguration();
810 }
811
812 ("apply" "function"
vinayakb38b7ca42012-03-05 05:44:15 +0000813 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000814 nameComponents = getDotSeparatedPair();
815 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
816 functionName = nameComponents.second.getValue();
vinayakb38b7ca42012-03-05 05:44:15 +0000817 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000818 ("@" <IDENTIFIER>
819 {
820 arity = Integer.parseInt(token.image);
821 }
822 )
823
824 {
825 appliedFunction = new FunctionSignature(dataverse, functionName, arity);
826 }
vinayakb38b7ca42012-03-05 05:44:15 +0000827 )?
828
829 "partitioned" "by" "key"
830 < IDENTIFIER >
831 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000832 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000833 }
834 (
835 "," < IDENTIFIER >
836 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000837 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000838 }
839 )*
840 (
841 "on" < IDENTIFIER >
842 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000843 nodeGroupName = new Identifier(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000844 }
845 )?
846 ";"
847 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000848 fdd = new FeedDetailsDecl(adapterFactoryClassname, properties, appliedFunction, nodeGroupName, partitioningExprs);
vinayakb38b7ca42012-03-05 05:44:15 +0000849 return fdd;
850 }
851}
852
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000853ControlFeedStatement ControlFeedDeclaration(ControlFeedStatement.OperationType operationType) throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000854{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000855 Pair<Identifier,Identifier> nameComponents = null;
856}
857{
858 {
859 nameComponents = getDotSeparatedPair();
860 return new ControlFeedStatement(operationType, nameComponents.first, nameComponents.second);
861 }
862}
863
864
865ControlFeedStatement AlterFeedDeclaration() throws ParseException :
866{
867 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000868 Map < String, String > configuration = new HashMap < String, String > ();
869}
870{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000871 {
872 nameComponents = getDotSeparatedPair();
873 }
874
vinayakb38b7ca42012-03-05 05:44:15 +0000875 "set"
876 {
877 configuration = getConfiguration();
878 }
879 ";"
880 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000881 return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
vinayakb38b7ca42012-03-05 05:44:15 +0000882 }
883}
884
885Map<String,String> getConfiguration() throws ParseException :
886{
887 Map<String,String> configuration = new HashMap<String,String>();
888 String key;
889 String value;
890}
891{
892
893<LEFTPAREN>
894 (
895 (
896 <LEFTPAREN>
897 (
898 <STRING_LITERAL>
899 {
900 key = removeQuotesAndEscapes(token.image);
901 }
902 "=" <STRING_LITERAL>
903 {
904 value = removeQuotesAndEscapes(token.image);
905 }
906 )
907 <RIGHTPAREN>
908 {
909 configuration.put(key, value);
910 }
911 )
912 (
913 "," <LEFTPAREN>
914 (
915 <STRING_LITERAL>
916 {
917 key = removeQuotesAndEscapes(token.image);
918 }
919 "=" <STRING_LITERAL>
920 {
921 value = removeQuotesAndEscapes(token.image);
922 }
923 )
924 <RIGHTPAREN>
925 {
926 configuration.put(key, value);
927 }
928 )*
929 )?
930 <RIGHTPAREN>
931 {
932 return configuration;
933 }
934}
935
936
937NodegroupDecl NodegroupDeclaration() throws ParseException :
938{
939 Identifier name = null;
940 List < Identifier > ncNames = new ArrayList < Identifier > ();
941 boolean ifNotExists = false;
942}
943{
944 < IDENTIFIER >
945 {
946 name = new Identifier(token.image);
947 }
948 (
949 "if not exists"
950 {
951 ifNotExists = true;
952 }
953 )?
954 "on" < IDENTIFIER >
955 {
956 ncNames.add(new Identifier(token.image));
957 }
958 (
959 "," < IDENTIFIER >
960 {
961 ncNames.add(new Identifier(token.image));
962 }
963 )*
964 ";"
965 {
966 return new NodegroupDecl(name, ncNames, ifNotExists);
967 }
968}
969
970
971TypeDecl TypeDeclaration(boolean dgen, String hint) throws ParseException:
972{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000973 Identifier dataverse;
vinayakb38b7ca42012-03-05 05:44:15 +0000974 Identifier ident;
975 TypeExpression typeExpr;
976 boolean ifNotExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000977 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000978}
979{
vinayakb38b7ca42012-03-05 05:44:15 +0000980 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000981 nameComponents = getDotSeparatedPair();
982 dataverse = nameComponents.first;
983 ident = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000984 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000985
vinayakb38b7ca42012-03-05 05:44:15 +0000986 (
987 "if not exists"
988 {
989 ifNotExists = true;
990 }
991 )?
992 "as"
993 ( typeExpr = TypeExpr() )
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000994 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +0000995 {
996 long numValues = -1;
997 String filename = null;
998 if (dgen) {
999 String splits[] = hint.split(" +");
1000 if (splits.length != 3) {
1001 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
1002 }
1003 filename = splits[1];
1004 numValues = Long.parseLong(splits[2]);
1005 }
1006 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001007 return new TypeDecl(dataverse, ident, typeExpr, tddg, ifNotExists);
vinayakb38b7ca42012-03-05 05:44:15 +00001008 }
1009}
1010
1011TypeExpression TypeExpr() throws ParseException:
1012{
1013 TypeExpression typeExpr = null;
1014}
1015{
1016 (
1017 typeExpr = RecordTypeDef()
1018 | typeExpr = TypeReference()
1019 | typeExpr = OrderedListTypeDef()
1020 | typeExpr = UnorderedListTypeDef()
1021 )
1022 {
1023 return typeExpr;
1024 }
1025}
1026
1027RecordTypeDefinition RecordTypeDef() throws ParseException:
1028{
1029 RecordTypeDefinition recType = new RecordTypeDefinition();
1030 RecordTypeDefinition.RecordKind recordKind = null;
1031}
1032{
1033 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1034 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1035 "{"
1036 {
1037 String hint = getHint(token);
1038 if (hint != null) {
1039 String splits[] = hint.split(" +");
1040 if (splits[0].equals(GEN_FIELDS_HINT)) {
1041 if (splits.length != 5) {
1042 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1043 }
1044 if (!splits[1].equals("int")) {
1045 throw new ParseException("The only supported type for gen-fields is int.");
1046 }
1047 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1048 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1049 recType.setUndeclaredFieldsDataGen(ufdg);
1050 }
1051 }
1052
1053 }
1054 (
1055 RecordField(recType)
1056 ( "," RecordField(recType) )*
1057 )?
1058 "}"
1059 {
1060 if (recordKind == null) {
1061 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1062 }
1063 recType.setRecordKind(recordKind);
1064 return recType;
1065 }
1066}
1067
1068void RecordField(RecordTypeDefinition recType) throws ParseException:
1069{
1070 String fieldName;
1071 TypeExpression type = null;
1072 boolean nullable = false;
1073}
1074{
1075 <IDENTIFIER>
1076 {
1077 Token t = getToken(0);
1078 fieldName = t.toString();
1079 String hint = getHint(t);
1080 IRecordFieldDataGen rfdg = null;
1081 if (hint != null) {
1082 String splits[] = hint.split(" +");
1083 if (splits[0].equals(VAL_FILE_HINT)) {
1084 File[] valFiles = new File[splits.length - 1];
1085 for (int k=1; k<splits.length; k++) {
1086 valFiles[k-1] = new File(splits[k]);
1087 }
1088 rfdg = new FieldValFileDataGen(valFiles);
1089 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
1090 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
1091 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
1092 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
1093 } else if (splits[0].equals(LIST_HINT)) {
1094 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1095 } else if (splits[0].equals(INTERVAL_HINT)) {
1096 FieldIntervalDataGen.ValueType vt;
1097 if (splits[1].equals("int")) {
1098 vt = FieldIntervalDataGen.ValueType.INT;
1099 } else if (splits[1].equals("long")) {
1100 vt = FieldIntervalDataGen.ValueType.LONG;
1101 } else if (splits[1].equals("float")) {
1102 vt = FieldIntervalDataGen.ValueType.FLOAT;
1103 } else if (splits[1].equals("double")) {
1104 vt = FieldIntervalDataGen.ValueType.DOUBLE;
1105 } else {
1106 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
1107 }
1108 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
1109 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
1110 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
1111 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
1112 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1113 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
1114 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1115 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
1116 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
1117 } else if (splits[0].equals(AUTO_HINT)) {
1118 rfdg = new AutoDataGen(splits[1]);
1119 }
1120 }
1121 }
1122 ":"
1123 ( type = TypeExpr() )
1124 ("?" { nullable = true; } )?
1125 {
1126 recType.addField(fieldName, type, nullable, rfdg);
1127 }
1128}
1129
1130TypeReferenceExpression TypeReference() throws ParseException:
1131{}
1132{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001133 <IDENTIFIER>
1134 {
1135 Token t = getToken(0);
1136 Identifier id = new Identifier(t.toString());
1137 return new TypeReferenceExpression(id);
1138 }
vinayakb38b7ca42012-03-05 05:44:15 +00001139}
1140
1141OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1142{
1143 TypeExpression type = null;
1144}
1145{
1146 "["
1147 ( type = TypeExpr() )
1148 "]"
1149 {
1150 return new OrderedListTypeDefinition(type);
1151 }
1152}
1153
1154
1155UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1156{
1157 TypeExpression type = null;
1158}
1159{
1160 "{{"
1161 ( type = TypeExpr() )
1162 "}}"
1163 {
1164 return new UnorderedListTypeDefinition(type);
1165 }
1166}
1167
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001168Pair<Identifier,Identifier> getDotSeparatedPair() throws ParseException:
1169{
1170 Identifier first = null;
1171 Identifier second = null;
1172}
1173{
1174 < IDENTIFIER >
1175 {
1176 first = new Identifier(token.image);
1177 }
1178 ("." <IDENTIFIER>
1179 {
1180 second = new Identifier(token.image);
1181 }
1182 )?
1183
1184 {
1185 if(second == null){
1186 second = first;
1187 first = null;
1188 }
1189
1190 return new Pair<Identifier,Identifier>(first,second);
1191 }
1192}
1193
1194Triple<Identifier,Identifier,Identifier> getDotSeparatedTriple() throws ParseException:
1195{
1196 Identifier first = null;
1197 Identifier second = null;
1198 Identifier third = null;
1199}
1200{
1201 < IDENTIFIER >
1202 {
1203 first = new Identifier(token.image);
1204 }
1205 "." <IDENTIFIER>
1206 {
1207 second = new Identifier(token.image);
1208 }
1209 (
1210 "." <IDENTIFIER>
1211 {
1212 third = new Identifier(token.image);
1213 }
1214 )?
1215
1216 {
1217 if(third == null){
1218 third = second;
1219 second = first;
1220 first = null;
1221 }
1222
1223 return new Triple<Identifier,Identifier,Identifier>(first,second,third);
1224 }
1225}
1226
1227
1228
vinayakb38b7ca42012-03-05 05:44:15 +00001229
1230FunctionDecl FunctionDeclaration() throws ParseException:
1231{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001232 FunctionDecl funcDecl;
1233 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001234 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001235 int arity = 0;
1236 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1237 Expression funcBody;
1238 VarIdentifier var = null;
1239 createNewScope();
1240}
1241{
1242
1243 <IDENTIFIER>
1244 {
1245 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001246 functionName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001247 }
1248 <LEFTPAREN> (<VARIABLE>
1249 {
1250 var = new VarIdentifier();
1251 var.setValue(getToken(0).toString());
1252 paramList.add(var);
1253 getCurrentScope().addNewVarSymbolToScope(var);
1254 arity++;
1255 }
1256 ("," <VARIABLE>
1257 {
1258 var = new VarIdentifier();
1259 var.setValue(getToken(0).toString());
1260 paramList.add(var);
1261 getCurrentScope().addNewVarSymbolToScope(var);
1262 arity++;
1263 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
1264
1265 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001266 signature = new FunctionSignature(defaultDataverse, functionName, arity);
1267 getCurrentScope().addFunctionDescriptor(signature, false);
1268 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1269 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001270 }
1271}
1272
1273CreateFunctionStatement FunctionCreation() throws ParseException:
1274{
1275 CreateFunctionStatement cfs = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001276 FunctionSignature signature;
1277 String dataverse;
ramangrover29a13f2422012-03-15 23:01:27 +00001278 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001279 boolean ifNotExists = false;
1280 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001281 String functionBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001282 VarIdentifier var = null;
1283 createNewScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001284 Expression functionBodyExpr;
1285 Token beginPos;
1286 Token endPos;
1287 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001288}
1289{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001290 {
1291 nameComponents = getDotSeparatedPair();
1292 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
1293 functionName= nameComponents.second.getValue();
vinayakb38b7ca42012-03-05 05:44:15 +00001294 }
1295
1296 (
1297 "if not exists"
1298 {
1299 ifNotExists = true;
1300 }
1301 )?
1302
1303 <LEFTPAREN> (<VARIABLE>
1304 {
1305 var = new VarIdentifier();
1306 var.setValue(getToken(0).toString());
1307 paramList.add(var);
1308 getCurrentScope().addNewVarSymbolToScope(var);
vinayakb38b7ca42012-03-05 05:44:15 +00001309 }
1310 ("," <VARIABLE>
1311 {
1312 var = new VarIdentifier();
1313 var.setValue(getToken(0).toString());
1314 paramList.add(var);
1315 getCurrentScope().addNewVarSymbolToScope(var);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001316 })*)? <RIGHTPAREN> "{"
vinayakb38b7ca42012-03-05 05:44:15 +00001317 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001318 beginPos = getToken(0);
1319 }
1320 functionBodyExpr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001321 "}"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001322 {
1323 endPos = getToken(0);
1324 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
1325 }
vinayakb38b7ca42012-03-05 05:44:15 +00001326 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001327 signature = new FunctionSignature(dataverse, functionName, paramList.size());
1328 getCurrentScope().addFunctionDescriptor(signature, false);
1329 cfs = new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
vinayakb38b7ca42012-03-05 05:44:15 +00001330 return cfs;
1331 }
1332}
1333
1334
1335
1336Query Query()throws ParseException:
1337{
1338 Query query = new Query();
1339 Expression expr;
1340}
1341{
1342 expr = Expression()
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001343 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +00001344 {
1345 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001346 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001347 return query;
1348 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001349
vinayakb38b7ca42012-03-05 05:44:15 +00001350}
1351
1352
1353
1354Expression Expression():
1355{
1356 Expression expr = null;
1357 Expression exprP = null;
1358}
1359{
1360(
1361
1362//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1363 expr = OperatorExpr()
1364 | expr = IfThenElse()
1365 | expr = FLWOGR()
1366 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001367
vinayakb38b7ca42012-03-05 05:44:15 +00001368
1369)
1370 {
1371 return (exprP==null) ? expr : exprP;
1372 }
1373}
1374
1375
1376
1377Expression OperatorExpr()throws ParseException:
1378{
1379 OperatorExpr op = null;
1380 Expression operand = null;
1381}
1382{
1383 operand = AndExpr()
1384 (
1385
1386 "or"
1387 {
1388 if (op == null) {
1389 op = new OperatorExpr();
1390 op.addOperand(operand);
1391 op.setCurrentop(true);
1392 }
1393 Token t = getToken(0);
1394 op.addOperator(t.toString());
1395 }
1396
1397 operand = AndExpr()
1398 {
1399 op.addOperand(operand);
1400 }
1401
1402 )*
1403
1404 {
1405 return op==null? operand: op;
1406 }
1407}
1408
1409Expression AndExpr()throws ParseException:
1410{
1411 OperatorExpr op = null;
1412 Expression operand = null;
1413}
1414{
1415 operand = RelExpr()
1416 (
1417
1418 "and"
1419 {
1420 if (op == null) {
1421 op = new OperatorExpr();
1422 op.addOperand(operand);
1423 op.setCurrentop(true);
1424 }
1425 Token t = getToken(0);
1426 op.addOperator(t.toString());
1427 }
1428
1429 operand = RelExpr()
1430 {
1431 op.addOperand(operand);
1432 }
1433
1434 )*
1435
1436 {
1437 return op==null? operand: op;
1438 }
1439}
1440
1441
1442
1443Expression RelExpr()throws ParseException:
1444{
1445 OperatorExpr op = null;
1446 Expression operand = null;
1447 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001448 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001449}
1450{
1451 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001452 {
1453 if (operand instanceof VariableExpr) {
1454 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001455 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001456 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001457 }
1458 }
1459 }
1460
1461 (
1462 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1463 {
alexander.behm07617fd2012-07-25 10:13:50 +00001464 String mhint = getHint(token);
1465 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1466 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1467 }
vinayakb38b7ca42012-03-05 05:44:15 +00001468 if (op == null) {
1469 op = new OperatorExpr();
1470 op.addOperand(operand, broadcast);
1471 op.setCurrentop(true);
1472 broadcast = false;
1473 }
1474 Token t = getToken(0);
1475 op.addOperator(t.toString());
1476 }
1477
1478 operand = AddExpr()
1479 {
alexander.behm07617fd2012-07-25 10:13:50 +00001480 broadcast = false;
1481 if (operand instanceof VariableExpr) {
1482 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001483 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1484 broadcast = true;
1485 }
alexander.behm07617fd2012-07-25 10:13:50 +00001486 }
vinayakb38b7ca42012-03-05 05:44:15 +00001487 op.addOperand(operand, broadcast);
1488 }
1489 )?
1490
1491 {
alexander.behm07617fd2012-07-25 10:13:50 +00001492 if (annotation != null) {
1493 op.addHint(annotation);
1494 }
vinayakb38b7ca42012-03-05 05:44:15 +00001495 return op==null? operand: op;
1496 }
1497}
1498
1499Expression AddExpr()throws ParseException:
1500{
1501 OperatorExpr op = null;
1502 Expression operand = null;
1503}
1504{
1505 operand = MultExpr()
1506
1507 ( ("+" | "-")
1508 {
1509 if (op == null) {
1510 op = new OperatorExpr();
1511 op.addOperand(operand);
1512 op.setCurrentop(true);
1513 }
1514 Token t = getToken(0);
1515 ((OperatorExpr)op).addOperator(t.toString());
1516 }
1517
1518 operand = MultExpr()
1519 {
1520 op.addOperand(operand);
1521 }
1522 )*
1523
1524 {
1525 return op==null? operand: op;
1526 }
1527}
1528
1529Expression MultExpr()throws ParseException:
1530{
1531 OperatorExpr op = null;
1532 Expression operand = null;
1533}
1534{
1535 operand = UnionExpr()
1536
1537 (( "*" | "/" | "%" | <CARET> | "idiv")
1538 {
1539 if (op == null) {
1540 op = new OperatorExpr();
1541 op.addOperand(operand);
1542 op.setCurrentop(true);
1543 }
1544 Token t = getToken(0);
1545 op.addOperator(t.toString());
1546 }
1547 operand = UnionExpr()
1548 {
1549 op.addOperand(operand);
1550 }
1551 )*
1552
1553 {
1554 return op==null?operand:op;
1555 }
1556}
1557
1558Expression UnionExpr() throws ParseException:
1559{
1560 UnionExpr union = null;
1561 Expression operand1 = null;
1562 Expression operand2 = null;
1563}
1564{
1565 operand1 = UnaryExpr()
1566 ("union"
1567 (operand2 = UnaryExpr()) {
1568 if (union == null) {
1569 union = new UnionExpr();
1570 union.addExpr(operand1);
1571 }
1572 union.addExpr(operand2);
1573 } )*
1574 {
1575 return (union == null)? operand1: union;
1576 }
1577}
1578
1579Expression UnaryExpr() throws ParseException:
1580{
1581 Expression uexpr = null;
1582 Expression expr = null;
1583}
1584{
1585 (( "+"|"-")
1586 {
1587 uexpr = new UnaryExpr();
1588 Token t = getToken(0);
1589 if("+".equals(t.toString()))
1590 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1591 else if("-".equals(t.toString()))
1592 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1593 else
1594 throw new ParseException();
1595 }
1596 )?
1597
1598 expr = ValueExpr()
1599 {
1600 if(uexpr!=null){
1601 ((UnaryExpr)uexpr).setExpr(expr);
1602 return uexpr;
1603 }
1604 else{
1605 return expr;
1606 }
1607 }
1608}
1609
1610Expression ValueExpr() throws ParseException:
1611{
1612 Expression expr;
1613}
1614{
1615 expr = FieldOrIndexAccessor()
1616 {
1617 return expr;
1618 }
1619}
1620
1621
1622Expression FieldOrIndexAccessor()throws ParseException:
1623{
1624 Expression expr = null;
1625 Identifier ident = null;
1626 AbstractAccessor fa = null;
1627 int index;
1628
1629}
1630{
1631 ( expr = PrimaryExpr()
1632
1633 )
1634
1635
1636 (
1637 (
1638 ident = Field()
1639 {
1640 if(fa == null)
1641 fa = new FieldAccessor(expr, ident);
1642 else
1643 fa = new FieldAccessor(fa, ident);
1644 }
1645 )
1646 | (
1647 index = Index()
1648 {
1649 if(fa == null)
1650 fa = new IndexAccessor(expr, index);
1651 else
1652 fa = new IndexAccessor(fa, index);
1653 }
1654 )
1655 )*
1656
1657
1658 {
1659 return fa==null?expr:fa;
1660 }
1661}
1662
1663Identifier Field() throws ParseException:
1664{
1665 Identifier ident = null;
1666
1667}
1668{
1669 "." < IDENTIFIER >
1670 {
1671
1672 ident = new Identifier();
1673 ident.setValue(getToken(0).toString());
1674
1675 return ident;
1676 }
1677}
1678
1679int Index() throws ParseException:
1680{
1681 Expression expr = null;
1682 int idx = -2;
1683}
1684{
1685 "[" ( expr = Expression()
1686 {
1687 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1688 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001689 Literal lit = ((LiteralExpr)expr).getValue();
1690 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1691 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001692 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001693 }
vinayakb38b7ca42012-03-05 05:44:15 +00001694 else {
1695 throw new ParseException("Index should be an INTEGER");
1696 }
1697 }
1698
1699 }
1700
1701 | "?"
1702 {
1703 idx = IndexAccessor.ANY;
1704 // ANY
1705 }
1706
1707 )
1708
1709 "]"
1710 {
1711 return idx;
1712 }
1713}
1714
1715
1716Expression PrimaryExpr()throws ParseException:
1717{
1718 Expression expr = null;
1719}
1720{
ilovesoupc9fef1d2012-07-08 19:30:42 +00001721 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +00001722 (
1723 expr =Literal()
1724 | expr = FunctionCallExpr()
1725 | expr =VariableRef()
1726
1727 {
1728 if(((VariableExpr)expr).getIsNewVar() == true)
1729 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1730 }
1731 | expr = ListConstructor()
1732 | expr = RecordConstructor()
1733 | expr = ParenthesizedExpression()
1734 )
1735 {
1736 return expr;
1737 }
1738}
1739
1740Expression Literal() throws ParseException:
1741{
1742
1743 LiteralExpr lit = new LiteralExpr();
1744 Token t;
1745}
1746{
1747(
1748 <STRING_LITERAL>
1749 {
1750 t= getToken(0);
1751 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
1752 }
1753
1754 | <INTEGER_LITERAL>
1755 {
1756 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +00001757 try {
1758 lit.setValue(new IntegerLiteral(new Integer(t.image)));
1759 } catch(NumberFormatException ex) {
1760 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
1761 }
vinayakb38b7ca42012-03-05 05:44:15 +00001762 }
1763 | < FLOAT_LITERAL >
1764 {
1765 t= getToken(0);
1766 lit.setValue(new FloatLiteral(new Float(t.image)));
1767 }
1768 | < DOUBLE_LITERAL >
1769 {
1770 t= getToken(0);
1771 lit.setValue(new DoubleLiteral(new Double(t.image)));
1772 }
1773 | <NULL>
1774 {
1775 t= getToken(0);
1776 lit.setValue(NullLiteral.INSTANCE);
1777 }
1778 | <TRUE>
1779 {
1780 t= getToken(0);
1781 lit.setValue(TrueLiteral.INSTANCE);
1782 }
1783 | <FALSE>
1784 {
1785 t= getToken(0);
1786 lit.setValue(FalseLiteral.INSTANCE);
1787 }
1788)
1789 {
1790 return lit;
1791 }
1792}
1793
1794
1795VariableExpr VariableRef() throws ParseException:
1796{
1797 VariableExpr varExp = new VariableExpr();
1798 VarIdentifier var = new VarIdentifier();
1799 Token t;
1800}
1801{
1802 <VARIABLE>
1803 {
1804 t = getToken(0);//get current token
1805 String varName = t.toString();
1806 Identifier ident = lookupSymbol(varName);
1807 if (isInForbiddenScopes(varName)) {
1808 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.");
1809 }
1810 if(ident != null) { // exist such ident
1811 varExp.setIsNewVar(false);
1812 varExp.setVar((VarIdentifier)ident);
1813 } else {
1814 varExp.setVar(var);
1815 }
1816 var.setValue(t.toString());
1817 return varExp;
1818 }
1819}
1820
1821
1822VariableExpr Variable() throws ParseException:
1823{
1824 VariableExpr varExp = new VariableExpr();
1825 VarIdentifier var = new VarIdentifier();
1826 Token t;
1827}
1828{
1829 <VARIABLE>
1830 {
1831 t = getToken(0);//get current token
1832 Identifier ident = lookupSymbol(t.toString());
1833 if(ident != null) { // exist such ident
1834 varExp.setIsNewVar(false);
1835 }
1836 varExp.setVar(var);
1837 var.setValue(t.toString());
1838 return varExp;
1839 }
1840}
1841
1842Expression ListConstructor() throws ParseException:
1843{
1844 Expression expr = null;
1845}
1846{
1847 (
1848 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1849 )
1850
1851 {
1852 return expr;
1853 }
1854}
1855
1856
1857ListConstructor OrderedListConstructor() throws ParseException:
1858{
1859 ListConstructor expr = new ListConstructor();
1860 Expression tmp = null;
1861 List<Expression> exprList = new ArrayList<Expression>();
1862 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1863}
1864{
1865
1866 "["
1867 ( tmp = Expression()
1868 {
1869 exprList.add(tmp);
1870 }
1871
1872 ("," tmp = Expression() { exprList.add(tmp); })*
1873 )?
1874
1875 "]"
1876
1877 {
1878 expr.setExprList(exprList);
1879 return expr;
1880 }
1881}
1882
1883ListConstructor UnorderedListConstructor() throws ParseException:
1884{
1885 ListConstructor expr = new ListConstructor();
1886 Expression tmp = null;
1887 List<Expression> exprList = new ArrayList<Expression>();
1888 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1889}
1890{
1891
1892 "{{" ( tmp = Expression()
1893 {
1894 exprList.add(tmp);
1895 }
1896 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1897 {
1898 expr.setExprList(exprList);
1899 return expr;
1900 }
1901}
1902
1903RecordConstructor RecordConstructor() throws ParseException:
1904{
1905 RecordConstructor expr = new RecordConstructor();
1906 FieldBinding tmp = null;
1907 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1908}
1909{
1910 "{" (tmp = FieldBinding()
1911 {
1912 fbList.add(tmp);
1913 }
1914 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1915 {
1916 expr.setFbList(fbList);
1917 return expr;
1918 }
1919}
1920
1921FieldBinding FieldBinding() throws ParseException:
1922{
1923 FieldBinding fb = new FieldBinding();
1924 Expression left, right;
1925}
1926{
1927 left = Expression() ":" right = Expression()
1928 {
1929 fb.setLeftExpr(left);
1930 fb.setRightExpr(right);
1931 return fb;
1932 }
1933}
1934
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001935
vinayakb38b7ca42012-03-05 05:44:15 +00001936Expression FunctionCallExpr() throws ParseException:
1937{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001938 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001939 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001940 Expression tmp;
1941 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001942 String funcName;
1943 String dataverse;
1944 String hint=null;
1945 String id1=null;
1946 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001947}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001948{
1949 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;} ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})? | <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();})
vinayakb38b7ca42012-03-05 05:44:15 +00001950 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001951 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001952 }
1953 <LEFTPAREN> (tmp = Expression()
1954 {
1955 argList.add(tmp);
1956 arity ++;
1957 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1958
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001959 {
1960 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1961 if(signature == null)
1962 {
1963 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1964 }
1965 callExpr = new CallExpr(signature,argList);
1966 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1967 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1968 }
1969 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001970 }
1971}
1972
1973
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001974
vinayakb38b7ca42012-03-05 05:44:15 +00001975Expression ParenthesizedExpression() throws ParseException:
1976{
1977 Expression expr;
1978}
1979{
1980 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1981 {
1982 return expr;
1983 }
1984}
1985
1986Expression IfThenElse() throws ParseException:
1987{
1988 Expression condExpr;
1989 Expression thenExpr;
1990 Expression elseExpr;
1991 IfExpr ifExpr = new IfExpr();
1992}
1993{
1994 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1995
1996 {
1997 ifExpr.setCondExpr(condExpr);
1998 ifExpr.setThenExpr(thenExpr);
1999 ifExpr.setElseExpr(elseExpr);
2000 return ifExpr;
2001 }
2002}
2003
2004Expression FLWOGR() throws ParseException:
2005{
2006 FLWOGRExpression flworg = new FLWOGRExpression();
2007 List<Clause> clauseList = new ArrayList<Clause>();
2008 Expression returnExpr;
2009 Clause tmp;
2010 createNewScope();
2011}
2012{
2013 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2014 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
2015
2016 {
2017 flworg.setClauseList(clauseList);
2018 flworg.setReturnExpr(returnExpr);
2019 removeCurrentScope();
2020 return flworg;
2021 }
2022}
2023
2024Clause Clause()throws ParseException :
2025{
2026 Clause clause;
2027}
2028{
2029 (
2030 clause = ForClause()
2031 | clause = LetClause()
2032 | clause = WhereClause()
2033 | clause = OrderbyClause()
2034 | clause = GroupClause()
2035 | clause = LimitClause()
2036 | clause = DistinctClause()
2037 | clause = DieClause()
2038 )
2039 {
2040 return clause;
2041 }
2042}
2043
2044Clause ForClause()throws ParseException :
2045{
2046 ForClause fc = new ForClause();
2047 VariableExpr varExp;
2048 VariableExpr varPos = null;
2049 Expression inExp;
2050 extendCurrentScope();
2051}
2052{
2053 "for" varExp = Variable()
2054 {
2055 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2056 }
2057 ("at" varPos = Variable()
2058 {
2059 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2060 }
2061 )?
2062 "in" ( inExp = Expression() )
2063 {
2064 fc.setVarExpr(varExp);
2065 fc.setInExpr(inExp);
2066 if (varPos != null) {
2067 fc.setPosExpr(varPos);
2068 }
2069 return fc;
2070 }
2071}
2072
2073Clause LetClause() throws ParseException:
2074{
2075 LetClause lc = new LetClause();
2076 VariableExpr varExp;
2077 Expression beExp;
2078 extendCurrentScope();
2079}
2080{
ilovesoupb2527c12012-07-12 03:21:13 +00002081 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00002082 {
2083 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00002084 lc.setVarExpr(varExp);
2085 lc.setBeExpr(beExp);
2086 return lc;
2087 }
2088}
2089
2090Clause WhereClause()throws ParseException :
2091{
2092 WhereClause wc = new WhereClause();
2093 Expression whereExpr;
2094}
2095{
2096 "where" whereExpr = Expression()
2097 {
2098 wc.setWhereExpr(whereExpr);
2099 return wc;
2100 }
2101}
2102
2103Clause OrderbyClause()throws ParseException :
2104{
2105 OrderbyClause oc = new OrderbyClause();
2106 Expression orderbyExpr;
2107 List<Expression> orderbyList = new ArrayList<Expression>();
2108 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2109 int numOfOrderby = 0;
2110}
2111{
2112 (
2113 "order"
2114 {
2115 String hint = getHint(token);
2116 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
2117 String splits[] = hint.split(" +");
2118 int numFrames = Integer.parseInt(splits[1]);
2119 int numTuples = Integer.parseInt(splits[2]);
2120 oc.setNumFrames(numFrames);
2121 oc.setNumTuples(numTuples);
2122 }
2123 }
2124 "by" orderbyExpr = Expression()
2125 {
2126 orderbyList.add(orderbyExpr);
2127 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2128 }
2129 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
2130 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
2131 {
2132 modifierList.add(modif);
2133 }
2134
2135 ("," orderbyExpr = Expression()
2136 {
2137 orderbyList.add(orderbyExpr);
2138 modif = OrderbyClause.OrderModifier.ASC;
2139 }
2140 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
2141 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
2142 {
2143 modifierList.add(modif);
2144 }
2145 )*
2146)
2147 {
2148 oc.setModifierList(modifierList);
2149 oc.setOrderbyList(orderbyList);
2150 return oc;
2151 }
2152}
2153Clause GroupClause()throws ParseException :
2154{
2155 GroupbyClause gbc = new GroupbyClause();
2156 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2157 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2158 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
2159 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
2160 VariableExpr var = null;
2161 VariableExpr withVar = null;
2162 Expression expr = null;
2163 VariableExpr decorVar = null;
2164 Expression decorExpr = null;
2165}
2166{
2167 {
2168 Scope newScope = extendCurrentScopeNoPush(true);
2169 // extendCurrentScope(true);
2170 }
2171 "group"
2172 {
2173 String hint = getHint(token);
2174 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2175 gbc.setHashGroupByHint(true);
2176 }
2177 }
2178 "by" (LOOKAHEAD(2) var = Variable()
2179 {
2180 newScope.addNewVarSymbolToScope(var.getVar());
2181 } ":=")?
2182 expr = Expression()
2183 {
2184 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2185 vePairList.add(pair1);
2186 }
2187 ("," ( LOOKAHEAD(2) var = Variable()
2188 {
2189 newScope.addNewVarSymbolToScope(var.getVar());
2190 } ":=")?
2191 expr = Expression()
2192 {
2193 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2194 vePairList.add(pair2);
2195 }
2196 )*
2197 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2198 {
2199 newScope.addNewVarSymbolToScope(decorVar.getVar());
2200 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2201 decorPairList.add(pair3);
2202 }
2203 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2204 {
2205 newScope.addNewVarSymbolToScope(decorVar.getVar());
2206 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2207 decorPairList.add(pair4);
2208 }
2209 )*
2210 )?
2211 "with" withVar = VariableRef()
2212 {
2213 if(withVar.getIsNewVar()==true)
2214 throw new ParseException("can't find variable " + withVar.getVar());
2215 withVarList.add(withVar);
2216 newScope.addNewVarSymbolToScope(withVar.getVar());
2217 }
2218 ("," withVar = VariableRef()
2219 {
2220 if(withVar.getIsNewVar()==true)
2221 throw new ParseException("can't find variable " + withVar.getVar());
2222 withVarList.add(withVar);
2223 newScope.addNewVarSymbolToScope(withVar.getVar());
2224 })*
2225 {
2226 gbc.setGbyPairList(vePairList);
2227 gbc.setDecorPairList(decorPairList);
2228 gbc.setWithVarList(withVarList);
2229 replaceCurrentScope(newScope);
2230 return gbc;
2231 }
2232}
2233
2234
2235LimitClause LimitClause() throws ParseException:
2236{
2237 LimitClause lc = new LimitClause();
2238 Expression expr;
2239 pushForbiddenScope(getCurrentScope());
2240}
2241{
2242 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2243 ("offset" expr = Expression() { lc.setOffset(expr); })?
2244
2245 {
2246 popForbiddenScope();
2247 return lc;
2248 }
2249}
2250
2251DistinctClause DistinctClause() throws ParseException:
2252{
2253 List<Expression> exprs = new ArrayList<Expression>();
2254 Expression expr;
2255}
2256{
2257 "distinct" "by" expr = Expression()
2258 {
2259 exprs.add(expr);
2260 }
2261 ("," expr = Expression()
2262 {
2263 exprs.add(expr);
2264 }
2265 )*
2266 {
2267 return new DistinctClause(exprs);
2268 }
2269}
2270
2271DieClause DieClause() throws ParseException:
2272{
2273 DieClause lc = new DieClause();
2274 Expression expr;
2275 pushForbiddenScope(getCurrentScope());
2276}
2277{
2278 "die" "after" expr = Expression() { lc.setDieExpr(expr); }
2279 {
2280 popForbiddenScope();
2281 return lc;
2282 }
2283}
2284
2285
2286QuantifiedExpression QuantifiedExpression()throws ParseException:
2287{
2288 QuantifiedExpression qc = new QuantifiedExpression();
2289 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2290 Expression satisfiesExpr;
2291 VariableExpr var;
2292 Expression inExpr;
2293 QuantifiedPair pair;
2294}
2295{
2296 {
2297 createNewScope();
2298 }
2299
2300 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2301 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2302 var = Variable() "in" inExpr = Expression()
2303 {
2304 pair = new QuantifiedPair(var, inExpr);
2305 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2306 quantifiedList.add(pair);
2307 }
2308 (
2309 "," var = Variable() "in" inExpr = Expression()
2310 {
2311 pair = new QuantifiedPair(var, inExpr);
2312 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2313 quantifiedList.add(pair);
2314 }
2315 )*
2316 "satisfies" satisfiesExpr = Expression()
2317 {
2318 qc.setSatisfiesExpr(satisfiesExpr);
2319 qc.setQuantifiedList(quantifiedList);
2320 removeCurrentScope();
2321 return qc;
2322 }
2323}
2324
2325TOKEN_MGR_DECLS:
2326{
2327 public int commentDepth = 0;
2328}
2329
2330<DEFAULT>
2331TOKEN :
2332{
2333 <CARET : "^" >
2334}
2335
2336<DEFAULT>
2337TOKEN :
2338{
2339 <DATASET : "dataset" >
2340}
2341
2342<DEFAULT>
2343TOKEN :
2344{
2345 <LEFTPAREN : "(" >
2346}
2347
2348<DEFAULT>
2349TOKEN :
2350{
2351 <RIGHTPAREN : ")" >
2352}
2353
2354
2355<DEFAULT>
2356TOKEN :
2357{
2358 <INTEGER_LITERAL : (<DIGIT>)+ >
2359}
2360
2361
2362<DEFAULT>
2363TOKEN :
2364{
2365 <NULL : "null">
2366}
2367
2368<DEFAULT>
2369TOKEN :
2370{
2371 <TRUE : "true">
2372}
2373
2374<DEFAULT>
2375TOKEN :
2376{
2377 <FALSE : "false">
2378}
2379
2380<DEFAULT>
2381TOKEN :
2382{
2383 <#DIGIT : ["0" - "9"]>
2384}
2385
2386
2387TOKEN:
2388{
2389 < DOUBLE_LITERAL: <INTEGER>
2390 | <INTEGER> ( "." <INTEGER> )?
2391 | "." <INTEGER>
2392 >
2393 |
2394 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2395 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2396 | "." <INTEGER> ( "f" | "F" )
2397 >
2398 |
2399 <INTEGER : (<DIGIT>)+ >
2400}
2401
2402<DEFAULT>
2403TOKEN :
2404{
2405 <#LETTER : ["A" - "Z", "a" - "z"]>
2406}
2407
2408<DEFAULT>
2409TOKEN :
2410{
2411 <SPECIALCHARS : ["$", "_", "-"] >
2412}
2413
2414<DEFAULT>
2415TOKEN :
2416{
2417 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2418 |
2419 < #EscapeQuot: "\\\"" >
2420 |
2421 < #EscapeApos: "\\\'" >
2422}
2423
2424<DEFAULT>
2425TOKEN :
2426{
2427 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2428}
2429
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002430
vinayakb38b7ca42012-03-05 05:44:15 +00002431<DEFAULT>
2432TOKEN :
2433{
2434 <VARIABLE : "$" <IDENTIFIER> >
2435}
2436
2437SKIP:
2438{
2439 " "
2440| "\t"
2441| "\r"
2442| "\n"
2443}
2444
2445SKIP:
2446{
2447 <"//" (~["\n"])* "\n">
2448}
2449
2450SKIP:
2451{
2452 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2453}
2454
2455
2456SKIP:
2457{
2458 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2459}
2460
2461<INSIDE_COMMENT>
2462SPECIAL_TOKEN:
2463{
2464 <"+"(" ")*(~["*"])*>
2465}
2466
2467<INSIDE_COMMENT>
2468SKIP:
2469{
2470 <"/*"> {commentDepth++;}
2471}
2472
2473<INSIDE_COMMENT>
2474SKIP:
2475{
2476 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2477| <~[]>
2478}