blob: 8672fd1d30cb3797ee8f4bd6844561310b690849 [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"
401 ("btree" { cis.setIndexType(IndexType.BTREE); }
alexander.behmc576c602012-07-06 02:41:15 +0000402 | "keyword" { cis.setIndexType(IndexType.WORD_INVIX); }
403 | "rtree" { cis.setIndexType(IndexType.RTREE); }
404 | "ngram"
405 <LEFTPAREN>
406 (<INTEGER_LITERAL>
407 {
408 cis.setIndexType(IndexType.NGRAM_INVIX);
409 cis.setGramLength(Integer.valueOf(token.image));
410 }
411 )
412 <RIGHTPAREN>
413 )
vinayakb38b7ca42012-03-05 05:44:15 +0000414 ";"
415 | ";"
416 )
417 {
418 return cis;
419 }
420}
421
422DataverseDecl DataverseDeclaration() throws ParseException:
423{
424 Identifier dvName = null;
425}
426{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000427 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000428 ";"
429 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000430 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000431 }
432}
433
434DropStatement DropStatement() throws ParseException :
435{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000436 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000437 Identifier datasetName = null;
438 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000439 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000440}
441{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000442 {
443 nameComponents = getDotSeparatedPair();
444 dataverseName = nameComponents.first;
445 datasetName = nameComponents.second;
446 }
447
448
vinayakb38b7ca42012-03-05 05:44:15 +0000449 (
450 "if exists"
451 {
452 ifExists = true;
453 }
454 )? ";"
455 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000456 return new DropStatement(dataverseName, datasetName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000457 }
458}
459
460IndexDropStatement IndexDropStatement() throws ParseException :
461{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000462 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000463 Identifier datasetName = null;
464 Identifier indexName = null;
465 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000466 Triple<Identifier,Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000467}
468{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000469
vinayakb38b7ca42012-03-05 05:44:15 +0000470 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000471 nameComponents = getDotSeparatedTriple();
472 dataverseName = nameComponents.first;
473 datasetName = nameComponents.second;
474 indexName = nameComponents.third;
475 }
476
vinayakb38b7ca42012-03-05 05:44:15 +0000477 (
478 "if exists"
479 {
480 ifExists = true;
481 }
482 )? ";"
483 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000484 return new IndexDropStatement(dataverseName, datasetName, indexName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000485 }
486}
487
488NodeGroupDropStatement NodeGroupDropStatement() throws ParseException :
489{
490 Identifier groupName = null;
491 boolean ifExists = false;
492}
493{
494 < IDENTIFIER >
495 {
496 groupName = new Identifier(token.image);
497 }
498 (
499 "if exists"
500 {
501 ifExists = true;
502 }
503 )? ";"
504 {
505 return new NodeGroupDropStatement(groupName, ifExists);
506 }
507}
508
509TypeDropStatement TypeDropStatement() throws ParseException :
510{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000511 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000512 Identifier typeName = null;
513 boolean ifExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000514 Pair<Identifier,Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000515}
516{
vinayakb38b7ca42012-03-05 05:44:15 +0000517 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000518 nameComponents = getDotSeparatedPair();
519 dataverseName = nameComponents.first == null ? new Identifier(defaultDataverse) : nameComponents.first;
520 typeName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000521 }
522 (
523 "if exists"
524 {
525 ifExists = true;
526 }
527 )? ";"
528 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000529 return new TypeDropStatement(dataverseName, typeName, ifExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000530 }
531}
532
533DataverseDropStatement DataverseDropStatement() throws ParseException :
534{
535 Identifier dataverseName = null;
536 boolean ifExists = false;
537}
538{
539 < IDENTIFIER >
540 {
541 dataverseName = new Identifier(token.image);
542 }
543 (
544 "if exists"
545 {
546 ifExists = true;
547 }
548 )? ";"
549 {
550 return new DataverseDropStatement(dataverseName, ifExists);
551 }
552}
553
554CreateDataverseStatement CreateDataverseStatement() throws ParseException :
555{
556 Identifier dvName = null;
557 boolean ifNotExists = false;
558 String format = null;
559}
560{
561 < IDENTIFIER >
562 {
563 dvName = new Identifier(token.image);
564 }
565 (
566 "if not exists"
567 {
568 ifNotExists = true;
569 }
570 )?
571 (
572 "with format" < STRING_LITERAL >
573 {
574 format = removeQuotesAndEscapes(token.image);
575 }
576 )?
577 ";"
578 {
579 return new CreateDataverseStatement(dvName, format, ifNotExists);
580 }
581}
582
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000583
584FunctionDropStatement FunctionDropStatement() throws ParseException :
585{
586 String dataverse;
587 String functionName;
588 int arity=0;
589 boolean ifExists = false;
590 Pair<Identifier, Identifier> nameComponents=null;
591}
592{
593 {
594 nameComponents = getDotSeparatedPair();
595 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
596 functionName = nameComponents.second.getValue();
597 }
598
599 "@"
600 <INTEGER_LITERAL>
601 {
602 Token t= getToken(0);
603 arity = new Integer(t.image);
604 if( arity < 0 && arity != FunctionIdentifier.VARARGS){
605 throw new ParseException(" invalid arity:" + arity);
606 }
607 }
608
609 (
610 "if exists"
611 {
612 ifExists = true;
613 }
614 )? ";"
615 {
616 return new FunctionDropStatement(new FunctionSignature(dataverse, functionName, arity), ifExists);
617 }
618}
619
620
vinayakb38b7ca42012-03-05 05:44:15 +0000621LoadFromFileStatement LoadStatement() throws ParseException:
622{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000623 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000624 Identifier datasetName = null;
625 boolean alreadySorted = false;
626 String adapterClassname;
627 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000628 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000629}
630{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000631 <DATASET>
632 {
633 nameComponents = getDotSeparatedPair();
634 dataverseName = nameComponents.first;
635 datasetName = nameComponents.second;
636 }
637
vinayakb38b7ca42012-03-05 05:44:15 +0000638 "using"
639
640 <STRING_LITERAL>
641 {
642 adapterClassname = removeQuotesAndEscapes(token.image);
643 }
644
645 {
646 properties = getConfiguration();
647 }
648
649 ("pre-sorted"
650 { alreadySorted = true; }
651 )?
652
653 ";"
654 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000655 return new LoadFromFileStatement(dataverseName, datasetName, adapterClassname, properties, alreadySorted);
vinayakb38b7ca42012-03-05 05:44:15 +0000656 }
657}
658
659
660
661DatasetDecl DatasetDeclaration(DatasetType datasetType) throws ParseException :
662{
663 DatasetDecl dd = null;
664 Identifier datasetName = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000665 Identifier dataverseName = null;
666 Identifier itemDataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000667 Identifier itemTypeName = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000668 String nameComponentFirst = null;
669 String nameComponentSecond = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000670 boolean ifNotExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000671 IDatasetDetailsDecl datasetDetails = null;
672 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000673}
674{
vinayakb38b7ca42012-03-05 05:44:15 +0000675 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000676 nameComponents = getDotSeparatedPair();
677 dataverseName = nameComponents.first;
678 datasetName = nameComponents.second;
679 }
680
vinayakb38b7ca42012-03-05 05:44:15 +0000681 (
682 "if not exists"
683 {
684 ifNotExists = true;
685 }
686 )?
687 (
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000688 < LEFTPAREN > <IDENTIFIER>
vinayakb38b7ca42012-03-05 05:44:15 +0000689 {
690 itemTypeName = new Identifier(token.image);
691 }
692 < RIGHTPAREN >
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000693 )
vinayakb38b7ca42012-03-05 05:44:15 +0000694 {
695 if(datasetType == DatasetType.INTERNAL) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000696 datasetDetails = InternalDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000697 }
698 else if(datasetType == DatasetType.EXTERNAL) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000699 datasetDetails = ExternalDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000700 }
701 else if(datasetType == DatasetType.FEED) {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000702 datasetDetails = FeedDatasetDeclaration();
vinayakb38b7ca42012-03-05 05:44:15 +0000703 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000704 dd = new DatasetDecl(dataverseName, datasetName, itemTypeName, datasetType, datasetDetails,ifNotExists);
705
vinayakb38b7ca42012-03-05 05:44:15 +0000706 }
707 {
708 return dd;
709 }
710}
711
712InternalDetailsDecl InternalDatasetDeclaration() throws ParseException :
713{
714 InternalDetailsDecl idd = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000715 List<String> partitioningExprs = new ArrayList<String>();
716 Identifier nodeGroupName=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000717}
718{
vinayakb38b7ca42012-03-05 05:44:15 +0000719 "partitioned" "by" "key"
720 < IDENTIFIER >
721 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000722 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000723 }
724 (
725 "," < IDENTIFIER >
726 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000727 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000728 }
729 )*
730 (
731 "on" < IDENTIFIER >
732 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000733 nodeGroupName = new Identifier(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000734 }
735 )?
736 ";"
737 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000738 idd = new InternalDetailsDecl(nodeGroupName, partitioningExprs);
vinayakb38b7ca42012-03-05 05:44:15 +0000739 return idd;
740 }
741}
742
743ExternalDetailsDecl ExternalDatasetDeclaration() throws ParseException :
744{
745 ExternalDetailsDecl edd = null;
746 String adapterClassname = null;
747 Map < String, String > properties;
748}
749{
750 {
751 edd = new ExternalDetailsDecl();
752 }
753
754 "using"
755
756 <STRING_LITERAL>
757 {
758 adapterClassname = removeQuotesAndEscapes(token.image);
759 }
760
761 {
762 properties = getConfiguration();
763 }
764
765 {
766 edd = new ExternalDetailsDecl();
767 edd.setAdapter(adapterClassname);
768 edd.setProperties(properties);
769 }
770 ";"
771
772 {
773 return edd;
774 }
775}
776
777FeedDetailsDecl FeedDatasetDeclaration() throws ParseException :
778{
779 FeedDetailsDecl fdd = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000780 String adapterFactoryClassname = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000781 Map < String, String > properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000782 Pair<Identifier,Identifier> nameComponents;
783 List<String> partitioningExprs = new ArrayList<String>();
784 Identifier nodeGroupName=null;
785 FunctionSignature appliedFunction=null;
786 String dataverse;
787 String functionName;
788 int arity;
vinayakb38b7ca42012-03-05 05:44:15 +0000789}
790{
vinayakb38b7ca42012-03-05 05:44:15 +0000791 "using"
792
793 <STRING_LITERAL>
794 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000795 adapterFactoryClassname = removeQuotesAndEscapes(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000796 }
797
798 {
799 properties = getConfiguration();
800 }
801
802 ("apply" "function"
vinayakb38b7ca42012-03-05 05:44:15 +0000803 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000804 nameComponents = getDotSeparatedPair();
805 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
806 functionName = nameComponents.second.getValue();
vinayakb38b7ca42012-03-05 05:44:15 +0000807 }
ramangrover29fbaba5f2013-01-26 02:36:29 +0000808 ("@" <INTEGER_LITERAL>
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000809 {
810 arity = Integer.parseInt(token.image);
811 }
812 )
813
814 {
815 appliedFunction = new FunctionSignature(dataverse, functionName, arity);
816 }
vinayakb38b7ca42012-03-05 05:44:15 +0000817 )?
818
819 "partitioned" "by" "key"
820 < IDENTIFIER >
821 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000822 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000823 }
824 (
825 "," < IDENTIFIER >
826 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000827 partitioningExprs.add(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000828 }
829 )*
830 (
831 "on" < IDENTIFIER >
832 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000833 nodeGroupName = new Identifier(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000834 }
835 )?
836 ";"
837 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000838 fdd = new FeedDetailsDecl(adapterFactoryClassname, properties, appliedFunction, nodeGroupName, partitioningExprs);
vinayakb38b7ca42012-03-05 05:44:15 +0000839 return fdd;
840 }
841}
842
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000843ControlFeedStatement ControlFeedDeclaration(ControlFeedStatement.OperationType operationType) throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000844{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000845 Pair<Identifier,Identifier> nameComponents = null;
846}
847{
848 {
849 nameComponents = getDotSeparatedPair();
850 return new ControlFeedStatement(operationType, nameComponents.first, nameComponents.second);
851 }
852}
853
854
855ControlFeedStatement AlterFeedDeclaration() throws ParseException :
856{
857 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000858 Map < String, String > configuration = new HashMap < String, String > ();
859}
860{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000861 {
862 nameComponents = getDotSeparatedPair();
863 }
864
vinayakb38b7ca42012-03-05 05:44:15 +0000865 "set"
866 {
867 configuration = getConfiguration();
868 }
ramangrover29fbaba5f2013-01-26 02:36:29 +0000869
vinayakb38b7ca42012-03-05 05:44:15 +0000870 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000871 return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
vinayakb38b7ca42012-03-05 05:44:15 +0000872 }
873}
874
875Map<String,String> getConfiguration() throws ParseException :
876{
877 Map<String,String> configuration = new HashMap<String,String>();
878 String key;
879 String value;
880}
881{
882
883<LEFTPAREN>
884 (
885 (
886 <LEFTPAREN>
887 (
888 <STRING_LITERAL>
889 {
890 key = removeQuotesAndEscapes(token.image);
891 }
892 "=" <STRING_LITERAL>
893 {
894 value = removeQuotesAndEscapes(token.image);
895 }
896 )
897 <RIGHTPAREN>
898 {
899 configuration.put(key, value);
900 }
901 )
902 (
903 "," <LEFTPAREN>
904 (
905 <STRING_LITERAL>
906 {
907 key = removeQuotesAndEscapes(token.image);
908 }
909 "=" <STRING_LITERAL>
910 {
911 value = removeQuotesAndEscapes(token.image);
912 }
913 )
914 <RIGHTPAREN>
915 {
916 configuration.put(key, value);
917 }
918 )*
919 )?
920 <RIGHTPAREN>
921 {
922 return configuration;
923 }
924}
925
926
927NodegroupDecl NodegroupDeclaration() throws ParseException :
928{
929 Identifier name = null;
930 List < Identifier > ncNames = new ArrayList < Identifier > ();
931 boolean ifNotExists = false;
932}
933{
934 < IDENTIFIER >
935 {
936 name = new Identifier(token.image);
937 }
938 (
939 "if not exists"
940 {
941 ifNotExists = true;
942 }
943 )?
944 "on" < IDENTIFIER >
945 {
946 ncNames.add(new Identifier(token.image));
947 }
948 (
949 "," < IDENTIFIER >
950 {
951 ncNames.add(new Identifier(token.image));
952 }
953 )*
954 ";"
955 {
956 return new NodegroupDecl(name, ncNames, ifNotExists);
957 }
958}
959
960
961TypeDecl TypeDeclaration(boolean dgen, String hint) throws ParseException:
962{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000963 Identifier dataverse;
vinayakb38b7ca42012-03-05 05:44:15 +0000964 Identifier ident;
965 TypeExpression typeExpr;
966 boolean ifNotExists = false;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000967 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +0000968}
969{
vinayakb38b7ca42012-03-05 05:44:15 +0000970 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000971 nameComponents = getDotSeparatedPair();
972 dataverse = nameComponents.first;
973 ident = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000974 }
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000975
vinayakb38b7ca42012-03-05 05:44:15 +0000976 (
977 "if not exists"
978 {
979 ifNotExists = true;
980 }
981 )?
982 "as"
983 ( typeExpr = TypeExpr() )
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000984 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +0000985 {
986 long numValues = -1;
987 String filename = null;
988 if (dgen) {
989 String splits[] = hint.split(" +");
990 if (splits.length != 3) {
991 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
992 }
993 filename = splits[1];
994 numValues = Long.parseLong(splits[2]);
995 }
996 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000997 return new TypeDecl(dataverse, ident, typeExpr, tddg, ifNotExists);
vinayakb38b7ca42012-03-05 05:44:15 +0000998 }
999}
1000
1001TypeExpression TypeExpr() throws ParseException:
1002{
1003 TypeExpression typeExpr = null;
1004}
1005{
1006 (
1007 typeExpr = RecordTypeDef()
1008 | typeExpr = TypeReference()
1009 | typeExpr = OrderedListTypeDef()
1010 | typeExpr = UnorderedListTypeDef()
1011 )
1012 {
1013 return typeExpr;
1014 }
1015}
1016
1017RecordTypeDefinition RecordTypeDef() throws ParseException:
1018{
1019 RecordTypeDefinition recType = new RecordTypeDefinition();
1020 RecordTypeDefinition.RecordKind recordKind = null;
1021}
1022{
1023 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1024 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1025 "{"
1026 {
1027 String hint = getHint(token);
1028 if (hint != null) {
1029 String splits[] = hint.split(" +");
1030 if (splits[0].equals(GEN_FIELDS_HINT)) {
1031 if (splits.length != 5) {
1032 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1033 }
1034 if (!splits[1].equals("int")) {
1035 throw new ParseException("The only supported type for gen-fields is int.");
1036 }
1037 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1038 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1039 recType.setUndeclaredFieldsDataGen(ufdg);
1040 }
1041 }
1042
1043 }
1044 (
1045 RecordField(recType)
1046 ( "," RecordField(recType) )*
1047 )?
1048 "}"
1049 {
1050 if (recordKind == null) {
1051 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1052 }
1053 recType.setRecordKind(recordKind);
1054 return recType;
1055 }
1056}
1057
1058void RecordField(RecordTypeDefinition recType) throws ParseException:
1059{
1060 String fieldName;
1061 TypeExpression type = null;
1062 boolean nullable = false;
1063}
1064{
1065 <IDENTIFIER>
1066 {
1067 Token t = getToken(0);
1068 fieldName = t.toString();
1069 String hint = getHint(t);
1070 IRecordFieldDataGen rfdg = null;
1071 if (hint != null) {
1072 String splits[] = hint.split(" +");
1073 if (splits[0].equals(VAL_FILE_HINT)) {
1074 File[] valFiles = new File[splits.length - 1];
1075 for (int k=1; k<splits.length; k++) {
1076 valFiles[k-1] = new File(splits[k]);
1077 }
1078 rfdg = new FieldValFileDataGen(valFiles);
1079 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
1080 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
1081 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
1082 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
1083 } else if (splits[0].equals(LIST_HINT)) {
1084 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1085 } else if (splits[0].equals(INTERVAL_HINT)) {
1086 FieldIntervalDataGen.ValueType vt;
1087 if (splits[1].equals("int")) {
1088 vt = FieldIntervalDataGen.ValueType.INT;
1089 } else if (splits[1].equals("long")) {
1090 vt = FieldIntervalDataGen.ValueType.LONG;
1091 } else if (splits[1].equals("float")) {
1092 vt = FieldIntervalDataGen.ValueType.FLOAT;
1093 } else if (splits[1].equals("double")) {
1094 vt = FieldIntervalDataGen.ValueType.DOUBLE;
1095 } else {
1096 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
1097 }
1098 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
1099 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
1100 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
1101 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
1102 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1103 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
1104 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
1105 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
1106 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
1107 } else if (splits[0].equals(AUTO_HINT)) {
1108 rfdg = new AutoDataGen(splits[1]);
1109 }
1110 }
1111 }
1112 ":"
1113 ( type = TypeExpr() )
1114 ("?" { nullable = true; } )?
1115 {
1116 recType.addField(fieldName, type, nullable, rfdg);
1117 }
1118}
1119
1120TypeReferenceExpression TypeReference() throws ParseException:
1121{}
1122{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001123 <IDENTIFIER>
1124 {
1125 Token t = getToken(0);
1126 Identifier id = new Identifier(t.toString());
1127 return new TypeReferenceExpression(id);
1128 }
vinayakb38b7ca42012-03-05 05:44:15 +00001129}
1130
1131OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1132{
1133 TypeExpression type = null;
1134}
1135{
1136 "["
1137 ( type = TypeExpr() )
1138 "]"
1139 {
1140 return new OrderedListTypeDefinition(type);
1141 }
1142}
1143
1144
1145UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1146{
1147 TypeExpression type = null;
1148}
1149{
1150 "{{"
1151 ( type = TypeExpr() )
1152 "}}"
1153 {
1154 return new UnorderedListTypeDefinition(type);
1155 }
1156}
1157
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001158Pair<Identifier,Identifier> getDotSeparatedPair() throws ParseException:
1159{
1160 Identifier first = null;
1161 Identifier second = null;
1162}
1163{
1164 < IDENTIFIER >
1165 {
1166 first = new Identifier(token.image);
1167 }
1168 ("." <IDENTIFIER>
1169 {
1170 second = new Identifier(token.image);
1171 }
1172 )?
1173
1174 {
1175 if(second == null){
1176 second = first;
1177 first = null;
1178 }
1179
1180 return new Pair<Identifier,Identifier>(first,second);
1181 }
1182}
1183
1184Triple<Identifier,Identifier,Identifier> getDotSeparatedTriple() throws ParseException:
1185{
1186 Identifier first = null;
1187 Identifier second = null;
1188 Identifier third = null;
1189}
1190{
1191 < IDENTIFIER >
1192 {
1193 first = new Identifier(token.image);
1194 }
1195 "." <IDENTIFIER>
1196 {
1197 second = new Identifier(token.image);
1198 }
1199 (
1200 "." <IDENTIFIER>
1201 {
1202 third = new Identifier(token.image);
1203 }
1204 )?
1205
1206 {
1207 if(third == null){
1208 third = second;
1209 second = first;
1210 first = null;
1211 }
1212
1213 return new Triple<Identifier,Identifier,Identifier>(first,second,third);
1214 }
1215}
1216
1217
1218
vinayakb38b7ca42012-03-05 05:44:15 +00001219
1220FunctionDecl FunctionDeclaration() throws ParseException:
1221{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001222 FunctionDecl funcDecl;
1223 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001224 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001225 int arity = 0;
1226 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1227 Expression funcBody;
1228 VarIdentifier var = null;
1229 createNewScope();
1230}
1231{
1232
1233 <IDENTIFIER>
1234 {
1235 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001236 functionName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001237 }
1238 <LEFTPAREN> (<VARIABLE>
1239 {
1240 var = new VarIdentifier();
1241 var.setValue(getToken(0).toString());
1242 paramList.add(var);
1243 getCurrentScope().addNewVarSymbolToScope(var);
1244 arity++;
1245 }
1246 ("," <VARIABLE>
1247 {
1248 var = new VarIdentifier();
1249 var.setValue(getToken(0).toString());
1250 paramList.add(var);
1251 getCurrentScope().addNewVarSymbolToScope(var);
1252 arity++;
1253 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
1254
1255 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001256 signature = new FunctionSignature(defaultDataverse, functionName, arity);
1257 getCurrentScope().addFunctionDescriptor(signature, false);
1258 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1259 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001260 }
1261}
1262
1263CreateFunctionStatement FunctionCreation() throws ParseException:
1264{
1265 CreateFunctionStatement cfs = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001266 FunctionSignature signature;
1267 String dataverse;
ramangrover29a13f2422012-03-15 23:01:27 +00001268 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001269 boolean ifNotExists = false;
1270 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001271 String functionBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001272 VarIdentifier var = null;
1273 createNewScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001274 Expression functionBodyExpr;
1275 Token beginPos;
1276 Token endPos;
1277 Pair<Identifier,Identifier> nameComponents=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001278}
1279{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001280 {
1281 nameComponents = getDotSeparatedPair();
1282 dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
1283 functionName= nameComponents.second.getValue();
vinayakb38b7ca42012-03-05 05:44:15 +00001284 }
1285
1286 (
1287 "if not exists"
1288 {
1289 ifNotExists = true;
1290 }
1291 )?
1292
1293 <LEFTPAREN> (<VARIABLE>
1294 {
1295 var = new VarIdentifier();
1296 var.setValue(getToken(0).toString());
1297 paramList.add(var);
1298 getCurrentScope().addNewVarSymbolToScope(var);
vinayakb38b7ca42012-03-05 05:44:15 +00001299 }
1300 ("," <VARIABLE>
1301 {
1302 var = new VarIdentifier();
1303 var.setValue(getToken(0).toString());
1304 paramList.add(var);
1305 getCurrentScope().addNewVarSymbolToScope(var);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001306 })*)? <RIGHTPAREN> "{"
vinayakb38b7ca42012-03-05 05:44:15 +00001307 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001308 beginPos = getToken(0);
1309 }
1310 functionBodyExpr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001311 "}"
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001312 {
1313 endPos = getToken(0);
1314 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
1315 }
vinayakb38b7ca42012-03-05 05:44:15 +00001316 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001317 signature = new FunctionSignature(dataverse, functionName, paramList.size());
1318 getCurrentScope().addFunctionDescriptor(signature, false);
1319 cfs = new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
vinayakb38b7ca42012-03-05 05:44:15 +00001320 return cfs;
1321 }
1322}
1323
1324
1325
1326Query Query()throws ParseException:
1327{
1328 Query query = new Query();
1329 Expression expr;
1330}
1331{
1332 expr = Expression()
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001333 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +00001334 {
1335 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001336 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001337 return query;
1338 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001339
vinayakb38b7ca42012-03-05 05:44:15 +00001340}
1341
1342
1343
1344Expression Expression():
1345{
1346 Expression expr = null;
1347 Expression exprP = null;
1348}
1349{
1350(
1351
1352//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1353 expr = OperatorExpr()
1354 | expr = IfThenElse()
1355 | expr = FLWOGR()
1356 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001357
vinayakb38b7ca42012-03-05 05:44:15 +00001358
1359)
1360 {
1361 return (exprP==null) ? expr : exprP;
1362 }
1363}
1364
1365
1366
1367Expression OperatorExpr()throws ParseException:
1368{
1369 OperatorExpr op = null;
1370 Expression operand = null;
1371}
1372{
1373 operand = AndExpr()
1374 (
1375
1376 "or"
1377 {
1378 if (op == null) {
1379 op = new OperatorExpr();
1380 op.addOperand(operand);
1381 op.setCurrentop(true);
1382 }
1383 Token t = getToken(0);
1384 op.addOperator(t.toString());
1385 }
1386
1387 operand = AndExpr()
1388 {
1389 op.addOperand(operand);
1390 }
1391
1392 )*
1393
1394 {
1395 return op==null? operand: op;
1396 }
1397}
1398
1399Expression AndExpr()throws ParseException:
1400{
1401 OperatorExpr op = null;
1402 Expression operand = null;
1403}
1404{
1405 operand = RelExpr()
1406 (
1407
1408 "and"
1409 {
1410 if (op == null) {
1411 op = new OperatorExpr();
1412 op.addOperand(operand);
1413 op.setCurrentop(true);
1414 }
1415 Token t = getToken(0);
1416 op.addOperator(t.toString());
1417 }
1418
1419 operand = RelExpr()
1420 {
1421 op.addOperand(operand);
1422 }
1423
1424 )*
1425
1426 {
1427 return op==null? operand: op;
1428 }
1429}
1430
1431
1432
1433Expression RelExpr()throws ParseException:
1434{
1435 OperatorExpr op = null;
1436 Expression operand = null;
1437 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001438 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001439}
1440{
1441 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001442 {
1443 if (operand instanceof VariableExpr) {
1444 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001445 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001446 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001447 }
1448 }
1449 }
1450
1451 (
1452 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1453 {
alexander.behm07617fd2012-07-25 10:13:50 +00001454 String mhint = getHint(token);
1455 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1456 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1457 }
vinayakb38b7ca42012-03-05 05:44:15 +00001458 if (op == null) {
1459 op = new OperatorExpr();
1460 op.addOperand(operand, broadcast);
1461 op.setCurrentop(true);
1462 broadcast = false;
1463 }
1464 Token t = getToken(0);
1465 op.addOperator(t.toString());
1466 }
1467
1468 operand = AddExpr()
1469 {
alexander.behm07617fd2012-07-25 10:13:50 +00001470 broadcast = false;
1471 if (operand instanceof VariableExpr) {
1472 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001473 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1474 broadcast = true;
1475 }
alexander.behm07617fd2012-07-25 10:13:50 +00001476 }
vinayakb38b7ca42012-03-05 05:44:15 +00001477 op.addOperand(operand, broadcast);
1478 }
1479 )?
1480
1481 {
alexander.behm07617fd2012-07-25 10:13:50 +00001482 if (annotation != null) {
1483 op.addHint(annotation);
1484 }
vinayakb38b7ca42012-03-05 05:44:15 +00001485 return op==null? operand: op;
1486 }
1487}
1488
1489Expression AddExpr()throws ParseException:
1490{
1491 OperatorExpr op = null;
1492 Expression operand = null;
1493}
1494{
1495 operand = MultExpr()
1496
1497 ( ("+" | "-")
1498 {
1499 if (op == null) {
1500 op = new OperatorExpr();
1501 op.addOperand(operand);
1502 op.setCurrentop(true);
1503 }
1504 Token t = getToken(0);
1505 ((OperatorExpr)op).addOperator(t.toString());
1506 }
1507
1508 operand = MultExpr()
1509 {
1510 op.addOperand(operand);
1511 }
1512 )*
1513
1514 {
1515 return op==null? operand: op;
1516 }
1517}
1518
1519Expression MultExpr()throws ParseException:
1520{
1521 OperatorExpr op = null;
1522 Expression operand = null;
1523}
1524{
1525 operand = UnionExpr()
1526
1527 (( "*" | "/" | "%" | <CARET> | "idiv")
1528 {
1529 if (op == null) {
1530 op = new OperatorExpr();
1531 op.addOperand(operand);
1532 op.setCurrentop(true);
1533 }
1534 Token t = getToken(0);
1535 op.addOperator(t.toString());
1536 }
1537 operand = UnionExpr()
1538 {
1539 op.addOperand(operand);
1540 }
1541 )*
1542
1543 {
1544 return op==null?operand:op;
1545 }
1546}
1547
1548Expression UnionExpr() throws ParseException:
1549{
1550 UnionExpr union = null;
1551 Expression operand1 = null;
1552 Expression operand2 = null;
1553}
1554{
1555 operand1 = UnaryExpr()
1556 ("union"
1557 (operand2 = UnaryExpr()) {
1558 if (union == null) {
1559 union = new UnionExpr();
1560 union.addExpr(operand1);
1561 }
1562 union.addExpr(operand2);
1563 } )*
1564 {
1565 return (union == null)? operand1: union;
1566 }
1567}
1568
1569Expression UnaryExpr() throws ParseException:
1570{
1571 Expression uexpr = null;
1572 Expression expr = null;
1573}
1574{
1575 (( "+"|"-")
1576 {
1577 uexpr = new UnaryExpr();
1578 Token t = getToken(0);
1579 if("+".equals(t.toString()))
1580 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1581 else if("-".equals(t.toString()))
1582 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1583 else
1584 throw new ParseException();
1585 }
1586 )?
1587
1588 expr = ValueExpr()
1589 {
1590 if(uexpr!=null){
1591 ((UnaryExpr)uexpr).setExpr(expr);
1592 return uexpr;
1593 }
1594 else{
1595 return expr;
1596 }
1597 }
1598}
1599
1600Expression ValueExpr() throws ParseException:
1601{
1602 Expression expr;
1603}
1604{
1605 expr = FieldOrIndexAccessor()
1606 {
1607 return expr;
1608 }
1609}
1610
1611
1612Expression FieldOrIndexAccessor()throws ParseException:
1613{
1614 Expression expr = null;
1615 Identifier ident = null;
1616 AbstractAccessor fa = null;
1617 int index;
1618
1619}
1620{
1621 ( expr = PrimaryExpr()
1622
1623 )
1624
1625
1626 (
1627 (
1628 ident = Field()
1629 {
1630 if(fa == null)
1631 fa = new FieldAccessor(expr, ident);
1632 else
1633 fa = new FieldAccessor(fa, ident);
1634 }
1635 )
1636 | (
1637 index = Index()
1638 {
1639 if(fa == null)
1640 fa = new IndexAccessor(expr, index);
1641 else
1642 fa = new IndexAccessor(fa, index);
1643 }
1644 )
1645 )*
1646
1647
1648 {
1649 return fa==null?expr:fa;
1650 }
1651}
1652
1653Identifier Field() throws ParseException:
1654{
1655 Identifier ident = null;
1656
1657}
1658{
1659 "." < IDENTIFIER >
1660 {
1661
1662 ident = new Identifier();
1663 ident.setValue(getToken(0).toString());
1664
1665 return ident;
1666 }
1667}
1668
1669int Index() throws ParseException:
1670{
1671 Expression expr = null;
1672 int idx = -2;
1673}
1674{
1675 "[" ( expr = Expression()
1676 {
1677 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1678 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001679 Literal lit = ((LiteralExpr)expr).getValue();
1680 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1681 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001682 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001683 }
vinayakb38b7ca42012-03-05 05:44:15 +00001684 else {
1685 throw new ParseException("Index should be an INTEGER");
1686 }
1687 }
1688
1689 }
1690
1691 | "?"
1692 {
1693 idx = IndexAccessor.ANY;
1694 // ANY
1695 }
1696
1697 )
1698
1699 "]"
1700 {
1701 return idx;
1702 }
1703}
1704
1705
1706Expression PrimaryExpr()throws ParseException:
1707{
1708 Expression expr = null;
1709}
1710{
ilovesoupc9fef1d2012-07-08 19:30:42 +00001711 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +00001712 (
1713 expr =Literal()
1714 | expr = FunctionCallExpr()
1715 | expr =VariableRef()
1716
1717 {
1718 if(((VariableExpr)expr).getIsNewVar() == true)
1719 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1720 }
1721 | expr = ListConstructor()
1722 | expr = RecordConstructor()
1723 | expr = ParenthesizedExpression()
1724 )
1725 {
1726 return expr;
1727 }
1728}
1729
1730Expression Literal() throws ParseException:
1731{
1732
1733 LiteralExpr lit = new LiteralExpr();
1734 Token t;
1735}
1736{
1737(
1738 <STRING_LITERAL>
1739 {
1740 t= getToken(0);
1741 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
1742 }
1743
1744 | <INTEGER_LITERAL>
1745 {
1746 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +00001747 try {
1748 lit.setValue(new IntegerLiteral(new Integer(t.image)));
1749 } catch(NumberFormatException ex) {
1750 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
1751 }
vinayakb38b7ca42012-03-05 05:44:15 +00001752 }
1753 | < FLOAT_LITERAL >
1754 {
1755 t= getToken(0);
1756 lit.setValue(new FloatLiteral(new Float(t.image)));
1757 }
1758 | < DOUBLE_LITERAL >
1759 {
1760 t= getToken(0);
1761 lit.setValue(new DoubleLiteral(new Double(t.image)));
1762 }
1763 | <NULL>
1764 {
1765 t= getToken(0);
1766 lit.setValue(NullLiteral.INSTANCE);
1767 }
1768 | <TRUE>
1769 {
1770 t= getToken(0);
1771 lit.setValue(TrueLiteral.INSTANCE);
1772 }
1773 | <FALSE>
1774 {
1775 t= getToken(0);
1776 lit.setValue(FalseLiteral.INSTANCE);
1777 }
1778)
1779 {
1780 return lit;
1781 }
1782}
1783
1784
1785VariableExpr VariableRef() throws ParseException:
1786{
1787 VariableExpr varExp = new VariableExpr();
1788 VarIdentifier var = new VarIdentifier();
1789 Token t;
1790}
1791{
1792 <VARIABLE>
1793 {
1794 t = getToken(0);//get current token
1795 String varName = t.toString();
1796 Identifier ident = lookupSymbol(varName);
1797 if (isInForbiddenScopes(varName)) {
1798 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.");
1799 }
1800 if(ident != null) { // exist such ident
1801 varExp.setIsNewVar(false);
1802 varExp.setVar((VarIdentifier)ident);
1803 } else {
1804 varExp.setVar(var);
1805 }
1806 var.setValue(t.toString());
1807 return varExp;
1808 }
1809}
1810
1811
1812VariableExpr Variable() throws ParseException:
1813{
1814 VariableExpr varExp = new VariableExpr();
1815 VarIdentifier var = new VarIdentifier();
1816 Token t;
1817}
1818{
1819 <VARIABLE>
1820 {
1821 t = getToken(0);//get current token
1822 Identifier ident = lookupSymbol(t.toString());
1823 if(ident != null) { // exist such ident
1824 varExp.setIsNewVar(false);
1825 }
1826 varExp.setVar(var);
1827 var.setValue(t.toString());
1828 return varExp;
1829 }
1830}
1831
1832Expression ListConstructor() throws ParseException:
1833{
1834 Expression expr = null;
1835}
1836{
1837 (
1838 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1839 )
1840
1841 {
1842 return expr;
1843 }
1844}
1845
1846
1847ListConstructor OrderedListConstructor() throws ParseException:
1848{
1849 ListConstructor expr = new ListConstructor();
1850 Expression tmp = null;
1851 List<Expression> exprList = new ArrayList<Expression>();
1852 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1853}
1854{
1855
1856 "["
1857 ( tmp = Expression()
1858 {
1859 exprList.add(tmp);
1860 }
1861
1862 ("," tmp = Expression() { exprList.add(tmp); })*
1863 )?
1864
1865 "]"
1866
1867 {
1868 expr.setExprList(exprList);
1869 return expr;
1870 }
1871}
1872
1873ListConstructor UnorderedListConstructor() throws ParseException:
1874{
1875 ListConstructor expr = new ListConstructor();
1876 Expression tmp = null;
1877 List<Expression> exprList = new ArrayList<Expression>();
1878 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1879}
1880{
1881
1882 "{{" ( tmp = Expression()
1883 {
1884 exprList.add(tmp);
1885 }
1886 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1887 {
1888 expr.setExprList(exprList);
1889 return expr;
1890 }
1891}
1892
1893RecordConstructor RecordConstructor() throws ParseException:
1894{
1895 RecordConstructor expr = new RecordConstructor();
1896 FieldBinding tmp = null;
1897 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1898}
1899{
1900 "{" (tmp = FieldBinding()
1901 {
1902 fbList.add(tmp);
1903 }
1904 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1905 {
1906 expr.setFbList(fbList);
1907 return expr;
1908 }
1909}
1910
1911FieldBinding FieldBinding() throws ParseException:
1912{
1913 FieldBinding fb = new FieldBinding();
1914 Expression left, right;
1915}
1916{
1917 left = Expression() ":" right = Expression()
1918 {
1919 fb.setLeftExpr(left);
1920 fb.setRightExpr(right);
1921 return fb;
1922 }
1923}
1924
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001925
vinayakb38b7ca42012-03-05 05:44:15 +00001926Expression FunctionCallExpr() throws ParseException:
1927{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001928 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001929 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001930 Expression tmp;
1931 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001932 String funcName;
1933 String dataverse;
1934 String hint=null;
1935 String id1=null;
1936 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001937}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001938{
1939 ( <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 +00001940 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001941 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001942 }
1943 <LEFTPAREN> (tmp = Expression()
1944 {
1945 argList.add(tmp);
1946 arity ++;
1947 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1948
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001949 {
1950 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1951 if(signature == null)
1952 {
1953 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1954 }
1955 callExpr = new CallExpr(signature,argList);
1956 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1957 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1958 }
1959 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001960 }
1961}
1962
1963
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001964
vinayakb38b7ca42012-03-05 05:44:15 +00001965Expression ParenthesizedExpression() throws ParseException:
1966{
1967 Expression expr;
1968}
1969{
1970 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1971 {
1972 return expr;
1973 }
1974}
1975
1976Expression IfThenElse() throws ParseException:
1977{
1978 Expression condExpr;
1979 Expression thenExpr;
1980 Expression elseExpr;
1981 IfExpr ifExpr = new IfExpr();
1982}
1983{
1984 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1985
1986 {
1987 ifExpr.setCondExpr(condExpr);
1988 ifExpr.setThenExpr(thenExpr);
1989 ifExpr.setElseExpr(elseExpr);
1990 return ifExpr;
1991 }
1992}
1993
1994Expression FLWOGR() throws ParseException:
1995{
1996 FLWOGRExpression flworg = new FLWOGRExpression();
1997 List<Clause> clauseList = new ArrayList<Clause>();
1998 Expression returnExpr;
1999 Clause tmp;
2000 createNewScope();
2001}
2002{
2003 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2004 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
2005
2006 {
2007 flworg.setClauseList(clauseList);
2008 flworg.setReturnExpr(returnExpr);
2009 removeCurrentScope();
2010 return flworg;
2011 }
2012}
2013
2014Clause Clause()throws ParseException :
2015{
2016 Clause clause;
2017}
2018{
2019 (
2020 clause = ForClause()
2021 | clause = LetClause()
2022 | clause = WhereClause()
2023 | clause = OrderbyClause()
2024 | clause = GroupClause()
2025 | clause = LimitClause()
2026 | clause = DistinctClause()
2027 | clause = DieClause()
2028 )
2029 {
2030 return clause;
2031 }
2032}
2033
2034Clause ForClause()throws ParseException :
2035{
2036 ForClause fc = new ForClause();
2037 VariableExpr varExp;
2038 VariableExpr varPos = null;
2039 Expression inExp;
2040 extendCurrentScope();
2041}
2042{
2043 "for" varExp = Variable()
2044 {
2045 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2046 }
2047 ("at" varPos = Variable()
2048 {
2049 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2050 }
2051 )?
2052 "in" ( inExp = Expression() )
2053 {
2054 fc.setVarExpr(varExp);
2055 fc.setInExpr(inExp);
2056 if (varPos != null) {
2057 fc.setPosExpr(varPos);
2058 }
2059 return fc;
2060 }
2061}
2062
2063Clause LetClause() throws ParseException:
2064{
2065 LetClause lc = new LetClause();
2066 VariableExpr varExp;
2067 Expression beExp;
2068 extendCurrentScope();
2069}
2070{
ilovesoupb2527c12012-07-12 03:21:13 +00002071 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00002072 {
2073 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00002074 lc.setVarExpr(varExp);
2075 lc.setBeExpr(beExp);
2076 return lc;
2077 }
2078}
2079
2080Clause WhereClause()throws ParseException :
2081{
2082 WhereClause wc = new WhereClause();
2083 Expression whereExpr;
2084}
2085{
2086 "where" whereExpr = Expression()
2087 {
2088 wc.setWhereExpr(whereExpr);
2089 return wc;
2090 }
2091}
2092
2093Clause OrderbyClause()throws ParseException :
2094{
2095 OrderbyClause oc = new OrderbyClause();
2096 Expression orderbyExpr;
2097 List<Expression> orderbyList = new ArrayList<Expression>();
2098 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2099 int numOfOrderby = 0;
2100}
2101{
2102 (
2103 "order"
2104 {
2105 String hint = getHint(token);
2106 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
2107 String splits[] = hint.split(" +");
2108 int numFrames = Integer.parseInt(splits[1]);
2109 int numTuples = Integer.parseInt(splits[2]);
2110 oc.setNumFrames(numFrames);
2111 oc.setNumTuples(numTuples);
2112 }
2113 }
2114 "by" orderbyExpr = Expression()
2115 {
2116 orderbyList.add(orderbyExpr);
2117 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2118 }
2119 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
2120 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
2121 {
2122 modifierList.add(modif);
2123 }
2124
2125 ("," orderbyExpr = Expression()
2126 {
2127 orderbyList.add(orderbyExpr);
2128 modif = OrderbyClause.OrderModifier.ASC;
2129 }
2130 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
2131 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
2132 {
2133 modifierList.add(modif);
2134 }
2135 )*
2136)
2137 {
2138 oc.setModifierList(modifierList);
2139 oc.setOrderbyList(orderbyList);
2140 return oc;
2141 }
2142}
2143Clause GroupClause()throws ParseException :
2144{
2145 GroupbyClause gbc = new GroupbyClause();
2146 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2147 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2148 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
2149 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
2150 VariableExpr var = null;
2151 VariableExpr withVar = null;
2152 Expression expr = null;
2153 VariableExpr decorVar = null;
2154 Expression decorExpr = null;
2155}
2156{
2157 {
2158 Scope newScope = extendCurrentScopeNoPush(true);
2159 // extendCurrentScope(true);
2160 }
2161 "group"
2162 {
2163 String hint = getHint(token);
2164 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2165 gbc.setHashGroupByHint(true);
2166 }
2167 }
2168 "by" (LOOKAHEAD(2) var = Variable()
2169 {
2170 newScope.addNewVarSymbolToScope(var.getVar());
2171 } ":=")?
2172 expr = Expression()
2173 {
2174 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2175 vePairList.add(pair1);
2176 }
2177 ("," ( LOOKAHEAD(2) var = Variable()
2178 {
2179 newScope.addNewVarSymbolToScope(var.getVar());
2180 } ":=")?
2181 expr = Expression()
2182 {
2183 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2184 vePairList.add(pair2);
2185 }
2186 )*
2187 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2188 {
2189 newScope.addNewVarSymbolToScope(decorVar.getVar());
2190 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2191 decorPairList.add(pair3);
2192 }
2193 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2194 {
2195 newScope.addNewVarSymbolToScope(decorVar.getVar());
2196 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2197 decorPairList.add(pair4);
2198 }
2199 )*
2200 )?
2201 "with" withVar = VariableRef()
2202 {
2203 if(withVar.getIsNewVar()==true)
2204 throw new ParseException("can't find variable " + withVar.getVar());
2205 withVarList.add(withVar);
2206 newScope.addNewVarSymbolToScope(withVar.getVar());
2207 }
2208 ("," withVar = VariableRef()
2209 {
2210 if(withVar.getIsNewVar()==true)
2211 throw new ParseException("can't find variable " + withVar.getVar());
2212 withVarList.add(withVar);
2213 newScope.addNewVarSymbolToScope(withVar.getVar());
2214 })*
2215 {
2216 gbc.setGbyPairList(vePairList);
2217 gbc.setDecorPairList(decorPairList);
2218 gbc.setWithVarList(withVarList);
2219 replaceCurrentScope(newScope);
2220 return gbc;
2221 }
2222}
2223
2224
2225LimitClause LimitClause() throws ParseException:
2226{
2227 LimitClause lc = new LimitClause();
2228 Expression expr;
2229 pushForbiddenScope(getCurrentScope());
2230}
2231{
2232 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2233 ("offset" expr = Expression() { lc.setOffset(expr); })?
2234
2235 {
2236 popForbiddenScope();
2237 return lc;
2238 }
2239}
2240
2241DistinctClause DistinctClause() throws ParseException:
2242{
2243 List<Expression> exprs = new ArrayList<Expression>();
2244 Expression expr;
2245}
2246{
2247 "distinct" "by" expr = Expression()
2248 {
2249 exprs.add(expr);
2250 }
2251 ("," expr = Expression()
2252 {
2253 exprs.add(expr);
2254 }
2255 )*
2256 {
2257 return new DistinctClause(exprs);
2258 }
2259}
2260
2261DieClause DieClause() throws ParseException:
2262{
2263 DieClause lc = new DieClause();
2264 Expression expr;
2265 pushForbiddenScope(getCurrentScope());
2266}
2267{
2268 "die" "after" expr = Expression() { lc.setDieExpr(expr); }
2269 {
2270 popForbiddenScope();
2271 return lc;
2272 }
2273}
2274
2275
2276QuantifiedExpression QuantifiedExpression()throws ParseException:
2277{
2278 QuantifiedExpression qc = new QuantifiedExpression();
2279 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2280 Expression satisfiesExpr;
2281 VariableExpr var;
2282 Expression inExpr;
2283 QuantifiedPair pair;
2284}
2285{
2286 {
2287 createNewScope();
2288 }
2289
2290 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2291 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2292 var = Variable() "in" inExpr = Expression()
2293 {
2294 pair = new QuantifiedPair(var, inExpr);
2295 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2296 quantifiedList.add(pair);
2297 }
2298 (
2299 "," var = Variable() "in" inExpr = Expression()
2300 {
2301 pair = new QuantifiedPair(var, inExpr);
2302 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2303 quantifiedList.add(pair);
2304 }
2305 )*
2306 "satisfies" satisfiesExpr = Expression()
2307 {
2308 qc.setSatisfiesExpr(satisfiesExpr);
2309 qc.setQuantifiedList(quantifiedList);
2310 removeCurrentScope();
2311 return qc;
2312 }
2313}
2314
2315TOKEN_MGR_DECLS:
2316{
2317 public int commentDepth = 0;
2318}
2319
2320<DEFAULT>
2321TOKEN :
2322{
2323 <CARET : "^" >
2324}
2325
2326<DEFAULT>
2327TOKEN :
2328{
2329 <DATASET : "dataset" >
2330}
2331
2332<DEFAULT>
2333TOKEN :
2334{
2335 <LEFTPAREN : "(" >
2336}
2337
2338<DEFAULT>
2339TOKEN :
2340{
2341 <RIGHTPAREN : ")" >
2342}
2343
2344
2345<DEFAULT>
2346TOKEN :
2347{
2348 <INTEGER_LITERAL : (<DIGIT>)+ >
2349}
2350
2351
2352<DEFAULT>
2353TOKEN :
2354{
2355 <NULL : "null">
2356}
2357
2358<DEFAULT>
2359TOKEN :
2360{
2361 <TRUE : "true">
2362}
2363
2364<DEFAULT>
2365TOKEN :
2366{
2367 <FALSE : "false">
2368}
2369
2370<DEFAULT>
2371TOKEN :
2372{
2373 <#DIGIT : ["0" - "9"]>
2374}
2375
2376
2377TOKEN:
2378{
2379 < DOUBLE_LITERAL: <INTEGER>
2380 | <INTEGER> ( "." <INTEGER> )?
2381 | "." <INTEGER>
2382 >
2383 |
2384 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2385 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2386 | "." <INTEGER> ( "f" | "F" )
2387 >
2388 |
2389 <INTEGER : (<DIGIT>)+ >
2390}
2391
2392<DEFAULT>
2393TOKEN :
2394{
2395 <#LETTER : ["A" - "Z", "a" - "z"]>
2396}
2397
2398<DEFAULT>
2399TOKEN :
2400{
2401 <SPECIALCHARS : ["$", "_", "-"] >
2402}
2403
2404<DEFAULT>
2405TOKEN :
2406{
2407 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2408 |
2409 < #EscapeQuot: "\\\"" >
2410 |
2411 < #EscapeApos: "\\\'" >
2412}
2413
2414<DEFAULT>
2415TOKEN :
2416{
2417 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2418}
2419
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002420
vinayakb38b7ca42012-03-05 05:44:15 +00002421<DEFAULT>
2422TOKEN :
2423{
2424 <VARIABLE : "$" <IDENTIFIER> >
2425}
2426
2427SKIP:
2428{
2429 " "
2430| "\t"
2431| "\r"
2432| "\n"
2433}
2434
2435SKIP:
2436{
2437 <"//" (~["\n"])* "\n">
2438}
2439
2440SKIP:
2441{
2442 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2443}
2444
2445
2446SKIP:
2447{
2448 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2449}
2450
2451<INSIDE_COMMENT>
2452SPECIAL_TOKEN:
2453{
2454 <"+"(" ")*(~["*"])*>
2455}
2456
2457<INSIDE_COMMENT>
2458SKIP:
2459{
2460 <"/*"> {commentDepth++;}
2461}
2462
2463<INSIDE_COMMENT>
2464SKIP:
2465{
2466 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2467| <~[]>
2468}