blob: fdcd86e8b7b304f513cb3e65a56b6f57c91958d9 [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;
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +000020import java.util.LinkedHashMap;
vinayakb38b7ca42012-03-05 05:44:15 +000021import edu.uci.ics.asterix.aql.literal.FloatLiteral;
22import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
23import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000024import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000025import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000026import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000027import edu.uci.ics.asterix.aql.literal.NullLiteral;
28import edu.uci.ics.asterix.aql.literal.StringLiteral;
29import edu.uci.ics.asterix.aql.literal.TrueLiteral;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000030import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
vinayakb38b7ca42012-03-05 05:44:15 +000031
32import edu.uci.ics.asterix.aql.base.*;
33import edu.uci.ics.asterix.aql.expression.*;
34import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
35import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
36import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
37import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
38import edu.uci.ics.asterix.aql.base.Statement.Kind;
39import edu.uci.ics.asterix.aql.context.Scope;
40import edu.uci.ics.asterix.aql.context.RootScopeFactory;
41import edu.uci.ics.asterix.common.annotations.*;
42import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000043import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000044import edu.uci.ics.asterix.common.functions.FunctionSignature;
alexander.behm07617fd2012-07-25 10:13:50 +000045import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
46import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000047import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
48import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
49import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
50
51
vinayakb38b7ca42012-03-05 05:44:15 +000052
53
54public class AQLParser extends ScopeChecker {
55
vinayakb38b7ca42012-03-05 05:44:15 +000056 // optimizer hints
57 private static final String HASH_GROUP_BY_HINT = "hash";
58 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000059 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000060 private static final String INMEMORY_HINT = "inmem";
61 private static final String VAL_FILE_HINT = "val-files";
62 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
63 private static final String INTERVAL_HINT = "interval";
64 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
65 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
66 private static final String LIST_VAL_FILE_HINT = "list-val-file";
67 private static final String LIST_HINT = "list";
68 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
69 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
70 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
71 private static final String AUTO_HINT = "auto";
72
73 private static final String GEN_FIELDS_HINT = "gen-fields";
74
75 // data generator hints
76 private static final String DGEN_HINT = "dgen";
Till Westmann31c21f92013-05-08 09:21:53 -070077
78 private static class IndexParams {
79 public IndexType type;
80 public int gramLength;
81
82 public IndexParams(IndexType type, int gramLength) {
83 this.type = type;
84 this.gramLength = gramLength;
85 }
86 };
vinayakb38b7ca42012-03-05 05:44:15 +000087
88 private static String getHint(Token t) {
Till Westmann31c21f92013-05-08 09:21:53 -070089 if (t.specialToken == null) {
90 return null;
91 }
92 String s = t.specialToken.image;
93 int n = s.length();
94 if (n < 2) {
95 return null;
96 }
97 return s.substring(1).trim();
vinayakb38b7ca42012-03-05 05:44:15 +000098 }
Till Westmann77cb2f42013-07-15 16:44:19 -070099
100 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
101 IRecordFieldDataGen rfdg = null;
102 String splits[] = hint.split(" +");
103 if (splits[0].equals(VAL_FILE_HINT)) {
104 File[] valFiles = new File[splits.length - 1];
105 for (int k=1; k<splits.length; k++) {
106 valFiles[k-1] = new File(splits[k]);
107 }
108 rfdg = new FieldValFileDataGen(valFiles);
109 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
110 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
111 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
112 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
113 } else if (splits[0].equals(LIST_HINT)) {
114 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
115 } else if (splits[0].equals(INTERVAL_HINT)) {
116 FieldIntervalDataGen.ValueType vt;
117 if (splits[1].equals("int")) {
118 vt = FieldIntervalDataGen.ValueType.INT;
119 } else if (splits[1].equals("long")) {
120 vt = FieldIntervalDataGen.ValueType.LONG;
121 } else if (splits[1].equals("float")) {
122 vt = FieldIntervalDataGen.ValueType.FLOAT;
123 } else if (splits[1].equals("double")) {
124 vt = FieldIntervalDataGen.ValueType.DOUBLE;
125 } else {
126 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
127 }
128 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
129 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
130 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
131 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
132 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
133 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
134 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
135 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
136 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
137 } else if (splits[0].equals(AUTO_HINT)) {
138 rfdg = new AutoDataGen(splits[1]);
139 }
140 return rfdg;
141 }
vinayakb38b7ca42012-03-05 05:44:15 +0000142
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000143 public AQLParser(String s){
Till Westmann31c21f92013-05-08 09:21:53 -0700144 this(new StringReader(s));
145 super.setInput(s);
146 }
vinayakb38b7ca42012-03-05 05:44:15 +0000147
Till Westmann31c21f92013-05-08 09:21:53 -0700148 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
149 File file = new File(args[0]);
150 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
151 AQLParser parser = new AQLParser(fis);
152 List<Statement> st = parser.Statement();
153 //st.accept(new AQLPrintVisitor(), 0);
154 }
vinayakb38b7ca42012-03-05 05:44:15 +0000155}
156
157PARSER_END(AQLParser)
158
159
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000160List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000161{
vinayakb38b7ca42012-03-05 05:44:15 +0000162 scopeStack.push(RootScopeFactory.createRootScope(this));
163 List<Statement> decls = new ArrayList<Statement>();
Till Westmann31c21f92013-05-08 09:21:53 -0700164 Statement stmt = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000165}
166{
Till Westmann31c21f92013-05-08 09:21:53 -0700167 ( stmt = SingleStatement() (";") ?
vinayakb38b7ca42012-03-05 05:44:15 +0000168 {
Till Westmann31c21f92013-05-08 09:21:53 -0700169 decls.add(stmt);
170 }
171 )*
172 <EOF>
173 {
174 return decls;
175 }
176}
177
178Statement SingleStatement() throws ParseException:
179{
180 Statement stmt = null;
181}
182{
183 (
184 stmt = DataverseDeclaration()
185 | stmt = FunctionDeclaration()
186 | stmt = CreateStatement()
187 | stmt = LoadStatement()
188 | stmt = DropStatement()
189 | stmt = WriteStatement()
190 | stmt = SetStatement()
191 | stmt = InsertStatement()
192 | stmt = DeleteStatement()
193 | stmt = UpdateStatement()
194 | stmt = FeedStatement()
195 | stmt = Query()
196 )
197 {
198 return stmt;
199 }
200}
201
202DataverseDecl DataverseDeclaration() throws ParseException:
203{
Till Westmann14a20a72013-05-09 00:06:24 -0700204 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700205}
206{
Till Westmanna4242bc2013-05-08 17:49:55 -0700207 "use" "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700208 {
Till Westmann14a20a72013-05-09 00:06:24 -0700209 defaultDataverse = dvName;
210 return new DataverseDecl(new Identifier(dvName));
Till Westmann31c21f92013-05-08 09:21:53 -0700211 }
212}
213
214Statement CreateStatement() throws ParseException:
215{
216 String hint = null;
217 boolean dgen = false;
218 Statement stmt = null;
219}
220{
221 "create"
222 (
223 {
224 hint = getHint(token);
225 if (hint != null && hint.startsWith(DGEN_HINT)) {
226 dgen = true;
227 }
228 }
229 stmt = TypeSpecification(hint, dgen)
230 | stmt = NodegroupSpecification()
231 | stmt = DatasetSpecification()
232 | stmt = IndexSpecification()
233 | stmt = DataverseSpecification()
234 | stmt = FunctionSpecification()
235 )
236 {
237 return stmt;
238 }
239}
240
241TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
242{
243 Pair<Identifier,Identifier> nameComponents = null;
244 boolean ifNotExists = false;
245 TypeExpression typeExpr = null;
246}
247{
248 "type" nameComponents = FunctionOrTypeName() ifNotExists = IfNotExists()
249 "as" typeExpr = TypeExpr()
250 {
251 long numValues = -1;
252 String filename = null;
253 if (dgen) {
254 String splits[] = hint.split(" +");
255 if (splits.length != 3) {
256 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
257 }
258 filename = splits[1];
259 numValues = Long.parseLong(splits[2]);
260 }
261 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
262 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
263 }
264}
265
266
267NodegroupDecl NodegroupSpecification() throws ParseException:
268{
Till Westmann14a20a72013-05-09 00:06:24 -0700269 String name = null;
270 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700271 boolean ifNotExists = false;
272 List<Identifier>ncNames = null;
273}
274{
Till Westmanna4242bc2013-05-08 17:49:55 -0700275 "nodegroup" name = Identifier()
276 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700277 {
278 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700279 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700280 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700281 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700282 {
Till Westmann14a20a72013-05-09 00:06:24 -0700283 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700284 }
285 )*
286 {
Till Westmann14a20a72013-05-09 00:06:24 -0700287 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700288 }
289}
290
291DatasetDecl DatasetSpecification() throws ParseException:
292{
293 Pair<Identifier,Identifier> nameComponents = null;
294 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700295 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700296 String adapterName = null;
297 Map<String,String> properties = null;
298 FunctionSignature appliedFunction = null;
299 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700300 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700301 Map<String,String> hints = new HashMap<String,String>();
302 DatasetDecl dsetDecl = null;
303}
304{
305 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700306 "external" <DATASET> nameComponents = QualifiedName()
307 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
308 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700309 "using" adapterName = AdapterName() properties = Configuration()
310 ( "hints" hints = Properties() )?
311 {
312 ExternalDetailsDecl edd = new ExternalDetailsDecl();
313 edd.setAdapter(adapterName);
314 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700315 dsetDecl = new DatasetDecl(nameComponents.first,
316 nameComponents.second,
317 new Identifier(typeName),
318 hints,
319 DatasetType.EXTERNAL,
320 edd,
321 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700322 }
323
Till Westmanna4242bc2013-05-08 17:49:55 -0700324 | "feed" <DATASET> nameComponents = QualifiedName()
325 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
326 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700327 "using" adapterName = AdapterName() properties = Configuration()
328 (appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
Till Westmanna4242bc2013-05-08 17:49:55 -0700329 ( "on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700330 ( "hints" hints = Properties() )?
331 {
Till Westmann14a20a72013-05-09 00:06:24 -0700332 FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
333 properties,
334 appliedFunction,
335 nodeGroupName != null
336 ? new Identifier(nodeGroupName)
337 : null,
338 primaryKeyFields);
339 dsetDecl = new DatasetDecl(nameComponents.first,
340 nameComponents.second,
341 new Identifier(typeName),
342 hints,
343 DatasetType.FEED,
344 fdd,
345 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700346 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700347 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700348 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
349 ifNotExists = IfNotExists()
350 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700351 ( "hints" hints = Properties() )?
352 {
Till Westmann14a20a72013-05-09 00:06:24 -0700353 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
354 ? new Identifier(nodeGroupName)
355 : null,
356 primaryKeyFields);
357 dsetDecl = new DatasetDecl(nameComponents.first,
358 nameComponents.second,
359 new Identifier(typeName),
360 hints,
361 DatasetType.INTERNAL,
362 idd,
363 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700364 }
365 )
366 {
367 return dsetDecl;
368 }
369}
370
371CreateIndexStatement IndexSpecification() throws ParseException:
372{
373 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700374 String indexName = null;
375 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700376 boolean ifNotExists = false;
377 Pair<Identifier,Identifier> nameComponents = null;
378 IndexParams indexType = null;
379}
380{
Till Westmanna4242bc2013-05-08 17:49:55 -0700381 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700382 ifNotExists = IfNotExists()
383 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700384 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700385 {
Till Westmann14a20a72013-05-09 00:06:24 -0700386 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700387 }
Till Westmann14a20a72013-05-09 00:06:24 -0700388 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700389 {
Till Westmann14a20a72013-05-09 00:06:24 -0700390 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700391 }
392 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
393 {
Till Westmann14a20a72013-05-09 00:06:24 -0700394 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700395 cis.setIfNotExists(ifNotExists);
396 cis.setDataverseName(nameComponents.first);
397 cis.setDatasetName(nameComponents.second);
398 if (indexType != null) {
399 cis.setIndexType(indexType.type);
400 cis.setGramLength(indexType.gramLength);
401 }
402 return cis;
403 }
404}
405
406IndexParams IndexType() throws ParseException:
407{
408 IndexType type = null;
409 int gramLength = 0;
410}
411{
412 ("btree"
413 {
414 type = IndexType.BTREE;
415 }
416 | "rtree"
417 {
418 type = IndexType.RTREE;
419 }
420 | "keyword"
421 {
JIMAHNb75446d2013-06-03 08:35:27 -0700422 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700423 }
424 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
425 {
JIMAHNb75446d2013-06-03 08:35:27 -0700426 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700427 gramLength = Integer.valueOf(token.image);
428 }
429 <RIGHTPAREN>)
430 {
431 return new IndexParams(type, gramLength);
432 }
433}
434
435CreateDataverseStatement DataverseSpecification() throws ParseException :
436{
Till Westmann14a20a72013-05-09 00:06:24 -0700437 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700438 boolean ifNotExists = false;
439 String format = null;
440}
441{
Till Westmanna4242bc2013-05-08 17:49:55 -0700442 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700443 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700444 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700445 {
Till Westmann14a20a72013-05-09 00:06:24 -0700446 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700447 }
448}
449
450CreateFunctionStatement FunctionSpecification() throws ParseException:
451{
452 FunctionSignature signature;
453 boolean ifNotExists = false;
454 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
455 String functionBody;
Till Westmann31c21f92013-05-08 09:21:53 -0700456 Expression functionBodyExpr;
457 Token beginPos;
458 Token endPos;
459 Pair<Identifier,Identifier> nameComponents=null;
460
461 createNewScope();
462}
463{
464 "function" nameComponents = FunctionOrTypeName()
465 ifNotExists = IfNotExists()
Till Westmannd7dcb122013-05-16 13:19:09 -0700466 paramList = ParameterList()
467 "{"
468 {
469 beginPos = token;
470 }
471 functionBodyExpr = Expression() "}"
472 {
473 endPos = token;
474 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
475 String dataverse = nameComponents.first.getValue();
476 String functionName = nameComponents.second.getValue();
477 signature = new FunctionSignature(dataverse, functionName, paramList.size());
478 getCurrentScope().addFunctionDescriptor(signature, false);
479 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
480 }
481}
482
483List<VarIdentifier> ParameterList() throws ParseException:
484{
485 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
486 VarIdentifier var = null;
487}
488{
Till Westmann31c21f92013-05-08 09:21:53 -0700489 <LEFTPAREN> (<VARIABLE>
490 {
491 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700492 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700493 paramList.add(var);
494 getCurrentScope().addNewVarSymbolToScope(var);
495 }
496 ("," <VARIABLE>
497 {
498 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700499 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700500 paramList.add(var);
501 getCurrentScope().addNewVarSymbolToScope(var);
502 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700503 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700504 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700505 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700506 }
507}
508
509boolean IfNotExists() throws ParseException:
510{
511}
512{
513 ( "if not exists"
514 {
515 return true;
516 }
517 )?
518 {
519 return false;
520 }
521}
522
523FunctionSignature ApplyFunction() throws ParseException:
524{
525 FunctionSignature funcSig = null;
526}
527{
528 "apply" "function" funcSig = FunctionSignature()
529 {
530 return funcSig;
531 }
532}
533
534FunctionSignature FunctionSignature() throws ParseException:
535{
536 Pair<Identifier,Identifier> pairId = null;
537 int arity = 0;
538}
539{
540 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
541 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700542 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700543 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
544 throw new ParseException(" invalid arity:" + arity);
545 }
546
547 String dataverse = pairId.first.getValue();
548 String functionName = pairId.second.getValue();
549 return new FunctionSignature(dataverse, functionName, arity);
550 }
551}
552
553List<String> PrimaryKey() throws ParseException:
554{
Till Westmann14a20a72013-05-09 00:06:24 -0700555 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700556 List<String> primaryKeyFields = new ArrayList<String>();
557}
558{
Till Westmann14a20a72013-05-09 00:06:24 -0700559 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700560 {
Till Westmann14a20a72013-05-09 00:06:24 -0700561 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700562 }
Till Westmann14a20a72013-05-09 00:06:24 -0700563 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700564 {
Till Westmann14a20a72013-05-09 00:06:24 -0700565 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700566 }
567 )*
568 {
569 return primaryKeyFields;
570 }
571}
572
573Statement DropStatement() throws ParseException:
574{
Till Westmann14a20a72013-05-09 00:06:24 -0700575 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700576 Pair<Identifier,Identifier> pairId = null;
577 Triple<Identifier,Identifier,Identifier> tripleId = null;
578 FunctionSignature funcSig = null;
579 boolean ifExists = false;
580 Statement stmt = null;
581}
582{
583 "drop"
584 (
585 <DATASET> pairId = QualifiedName() ifExists = IfExists()
586 {
587 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
588 }
589 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
590 {
591 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
592 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700593 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700594 {
Till Westmann14a20a72013-05-09 00:06:24 -0700595 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700596 }
597 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
598 {
599 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
600 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700601 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700602 {
Till Westmann14a20a72013-05-09 00:06:24 -0700603 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700604 }
605 | "function" funcSig = FunctionSignature() ifExists = IfExists()
606 {
607 stmt = new FunctionDropStatement(funcSig, ifExists);
608 }
609 )
610 {
611 return stmt;
612 }
613}
614
615boolean IfExists() throws ParseException :
616{
617}
618{
619 ( "if" "exists"
620 {
621 return true;
622 }
623 )?
624 {
625 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000626 }
627}
628
629InsertStatement InsertStatement() throws ParseException:
630{
Till Westmann31c21f92013-05-08 09:21:53 -0700631 Pair<Identifier,Identifier> nameComponents = null;
632 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000633}
634{
Till Westmann31c21f92013-05-08 09:21:53 -0700635 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
636 {
637 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
638 }
vinayakb38b7ca42012-03-05 05:44:15 +0000639}
640
641DeleteStatement DeleteStatement() throws ParseException:
642{
Till Westmann31c21f92013-05-08 09:21:53 -0700643 VariableExpr var = null;
644 Expression condition = null;
645 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000646}
647{
Till Westmann31c21f92013-05-08 09:21:53 -0700648 "delete" var = Variable()
649 {
650 getCurrentScope().addNewVarSymbolToScope(var.getVar());
651 }
652 "from" <DATASET> nameComponents = QualifiedName()
653 ("where" condition = Expression())?
654 {
655 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
656 }
vinayakb38b7ca42012-03-05 05:44:15 +0000657}
658
659UpdateStatement UpdateStatement() throws ParseException:
660{
Till Westmann31c21f92013-05-08 09:21:53 -0700661 VariableExpr vars;
662 Expression target;
663 Expression condition;
664 UpdateClause uc;
665 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000666}
667{
Till Westmann31c21f92013-05-08 09:21:53 -0700668 "update" vars = Variable() "in" target = Expression()
669 "where" condition = Expression()
670 <LEFTPAREN> (uc = UpdateClause()
671 {
672 ucs.add(uc);
673 }
674 ("," uc = UpdateClause()
675 {
676 ucs.add(uc);
677 }
678 )*) <RIGHTPAREN>
679 {
680 return new UpdateStatement(vars, target, condition, ucs);
681 }
vinayakb38b7ca42012-03-05 05:44:15 +0000682}
683
vinayakb38b7ca42012-03-05 05:44:15 +0000684UpdateClause UpdateClause() throws ParseException:
685{
Till Westmann31c21f92013-05-08 09:21:53 -0700686 Expression target = null;
687 Expression value = null ;
688 InsertStatement is = null;
689 DeleteStatement ds = null;
690 UpdateStatement us = null;
691 Expression condition = null;
692 UpdateClause ifbranch = null;
693 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000694}
695{
696 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700697 | is = InsertStatement()
698 | ds = DeleteStatement()
699 | us = UpdateStatement()
700 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
701 "then" ifbranch = UpdateClause()
702 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
703 {
704 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
705 }
vinayakb38b7ca42012-03-05 05:44:15 +0000706}
707
vinayakb38b7ca42012-03-05 05:44:15 +0000708Statement SetStatement() throws ParseException:
709{
710 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700711 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000712}
713{
Till Westmann7d535322013-05-09 00:40:02 -0700714 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700715 {
Till Westmann31c21f92013-05-08 09:21:53 -0700716 return new SetStatement(pn, pv);
717 }
vinayakb38b7ca42012-03-05 05:44:15 +0000718}
719
720Statement WriteStatement() throws ParseException:
721{
Till Westmann14a20a72013-05-09 00:06:24 -0700722 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000723 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000724 Query query;
725 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000726 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000727}
728{
Till Westmann35a0f702013-07-01 14:06:34 -0700729 "write" "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
Till Westmann7d535322013-05-09 00:40:02 -0700730 ( "using" writerClass = StringLiteral() )?
Till Westmann35a0f702013-07-01 14:06:34 -0700731 {
732 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
vinayakb38b7ca42012-03-05 05:44:15 +0000733 }
734}
735
vinayakb38b7ca42012-03-05 05:44:15 +0000736LoadFromFileStatement LoadStatement() throws ParseException:
737{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000738 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000739 Identifier datasetName = null;
740 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000741 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000742 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000743 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000744}
745{
Till Westmann31c21f92013-05-08 09:21:53 -0700746 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000747 {
Till Westmann31c21f92013-05-08 09:21:53 -0700748 dataverseName = nameComponents.first;
749 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000750 }
Till Westmann31c21f92013-05-08 09:21:53 -0700751 "using" adapterName = AdapterName() properties = Configuration()
752 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000753 {
Till Westmann31c21f92013-05-08 09:21:53 -0700754 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000755 }
756 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700757 {
758 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
759 }
vinayakb38b7ca42012-03-05 05:44:15 +0000760}
761
vinayakb38b7ca42012-03-05 05:44:15 +0000762
Till Westmann31c21f92013-05-08 09:21:53 -0700763String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000764{
ramangrover29669d8f62013-02-11 06:03:32 +0000765 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000766}
767{
Till Westmann68d99652013-05-09 11:15:21 -0700768 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000769 {
Till Westmann7d535322013-05-09 00:40:02 -0700770 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000771 }
vinayakb38b7ca42012-03-05 05:44:15 +0000772}
773
Till Westmann31c21f92013-05-08 09:21:53 -0700774Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000775{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000776 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700777 Map<String,String> configuration = null;
778 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000779}
780{
Till Westmann31c21f92013-05-08 09:21:53 -0700781 (
782 "begin" "feed" nameComponents = QualifiedName()
783 {
784 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
785 }
786 | "suspend" "feed" nameComponents = QualifiedName()
787 {
788 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
789 }
790 | "resume" "feed" nameComponents = QualifiedName()
791 {
792 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
793 }
794 | "end" "feed" nameComponents = QualifiedName()
795 {
796 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
797 }
798 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
799 {
800 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
801 }
802 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000803 {
Till Westmann31c21f92013-05-08 09:21:53 -0700804 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000805 }
806}
807
Till Westmann31c21f92013-05-08 09:21:53 -0700808Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000809{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000810 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700811 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000812}
813{
Till Westmann31c21f92013-05-08 09:21:53 -0700814 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000815 {
Till Westmann31c21f92013-05-08 09:21:53 -0700816 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000817 }
Till Westmann31c21f92013-05-08 09:21:53 -0700818 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000819 {
Till Westmann31c21f92013-05-08 09:21:53 -0700820 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000821 }
Till Westmann31c21f92013-05-08 09:21:53 -0700822 )* )? <RIGHTPAREN>
823 {
824 return configuration;
825 }
826}
827
828Pair<String, String> KeyValuePair() throws ParseException:
829{
830 String key;
831 String value;
832}
833{
Till Westmann7d535322013-05-09 00:40:02 -0700834 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700835 {
836 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000837 }
Till Westmann31c21f92013-05-08 09:21:53 -0700838}
839
840Map<String,String> Properties() throws ParseException:
841{
842 Map<String,String> properties = new HashMap<String,String>();
843 Pair<String, String> property;
844}
845{
846 ( <LEFTPAREN> property = Property()
847 {
848 properties.put(property.first, property.second);
849 }
850 ( "," property = Property()
851 {
852 properties.put(property.first, property.second);
853 }
854 )* <RIGHTPAREN> )?
855 {
856 return properties;
857 }
858}
859
860Pair<String, String> Property() throws ParseException:
861{
862 String key;
863 String value;
864}
865{
Till Westmann7d535322013-05-09 00:40:02 -0700866 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700867 {
868 try {
869 value = "" + Long.valueOf(token.image);
870 } catch (NumberFormatException nfe) {
871 throw new ParseException("inapproriate value: " + token.image);
872 }
873 }
874 )
875 {
876 return new Pair<String, String>(key.toUpperCase(), value);
877 }
vinayakb38b7ca42012-03-05 05:44:15 +0000878}
879
880TypeExpression TypeExpr() throws ParseException:
881{
882 TypeExpression typeExpr = null;
883}
884{
885 (
886 typeExpr = RecordTypeDef()
887 | typeExpr = TypeReference()
888 | typeExpr = OrderedListTypeDef()
889 | typeExpr = UnorderedListTypeDef()
890 )
891 {
892 return typeExpr;
893 }
894}
895
896RecordTypeDefinition RecordTypeDef() throws ParseException:
897{
898 RecordTypeDefinition recType = new RecordTypeDefinition();
899 RecordTypeDefinition.RecordKind recordKind = null;
900}
901{
902 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
903 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
904 "{"
905 {
906 String hint = getHint(token);
907 if (hint != null) {
908 String splits[] = hint.split(" +");
909 if (splits[0].equals(GEN_FIELDS_HINT)) {
910 if (splits.length != 5) {
911 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
912 }
913 if (!splits[1].equals("int")) {
914 throw new ParseException("The only supported type for gen-fields is int.");
915 }
916 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
917 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
918 recType.setUndeclaredFieldsDataGen(ufdg);
919 }
920 }
921
922 }
923 (
924 RecordField(recType)
925 ( "," RecordField(recType) )*
926 )?
927 "}"
928 {
929 if (recordKind == null) {
930 recordKind = RecordTypeDefinition.RecordKind.OPEN;
931 }
932 recType.setRecordKind(recordKind);
933 return recType;
934 }
935}
936
937void RecordField(RecordTypeDefinition recType) throws ParseException:
938{
Till Westmann77cb2f42013-07-15 16:44:19 -0700939 String fieldName;
940 TypeExpression type = null;
941 boolean nullable = false;
vinayakb38b7ca42012-03-05 05:44:15 +0000942}
943{
Till Westmann77cb2f42013-07-15 16:44:19 -0700944 fieldName = Identifier()
945 {
946 String hint = getHint(token);
947 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
948 }
949 ":" type = TypeExpr() ("?" { nullable = true; } )?
950 {
951 recType.addField(fieldName, type, nullable, rfdg);
952 }
vinayakb38b7ca42012-03-05 05:44:15 +0000953}
954
955TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000956{
Till Westmann14a20a72013-05-09 00:06:24 -0700957 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700958}
959{
960 id = Identifier()
961 {
Till Westmann14a20a72013-05-09 00:06:24 -0700962 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700963 }
vinayakb38b7ca42012-03-05 05:44:15 +0000964}
965
966OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
967{
968 TypeExpression type = null;
969}
970{
971 "["
972 ( type = TypeExpr() )
973 "]"
974 {
975 return new OrderedListTypeDefinition(type);
976 }
977}
978
979
980UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
981{
982 TypeExpression type = null;
983}
984{
985 "{{"
986 ( type = TypeExpr() )
987 "}}"
988 {
989 return new UnorderedListTypeDefinition(type);
990 }
991}
992
Till Westmann31c21f92013-05-08 09:21:53 -0700993
994Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000995{
Till Westmann31c21f92013-05-08 09:21:53 -0700996 Pair<Identifier,Identifier> name = null;
997}
998{
999 name = QualifiedName()
1000 {
1001 if (name.first == null) {
1002 name.first = new Identifier(defaultDataverse);
1003 }
1004 return name;
1005 }
1006}
1007
Till Westmann14a20a72013-05-09 00:06:24 -07001008String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001009{
Till Westmann68d99652013-05-09 11:15:21 -07001010 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001011}
1012{
1013 <IDENTIFIER>
1014 {
Till Westmann14a20a72013-05-09 00:06:24 -07001015 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001016 }
Till Westmann68d99652013-05-09 11:15:21 -07001017 | lit = StringLiteral()
1018 {
1019 return lit;
1020 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001021}
1022
Till Westmann7d535322013-05-09 00:40:02 -07001023String StringLiteral() throws ParseException:
1024{
1025}
1026{
1027 <STRING_LITERAL>
1028 {
1029 return removeQuotesAndEscapes(token.image);
1030 }
1031}
1032
Till Westmann31c21f92013-05-08 09:21:53 -07001033Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1034{
Till Westmann14a20a72013-05-09 00:06:24 -07001035 String first = null;
1036 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001037}
1038{
Till Westmanna4242bc2013-05-08 17:49:55 -07001039 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001040 {
Till Westmann14a20a72013-05-09 00:06:24 -07001041 Identifier id1 = null;
1042 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001043 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001044 id2 = new Identifier(first);
1045 } else
1046 {
1047 id1 = new Identifier(first);
1048 id2 = new Identifier(second);
1049 }
1050 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001051 }
1052}
1053
Till Westmann31c21f92013-05-08 09:21:53 -07001054Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001055{
Till Westmann14a20a72013-05-09 00:06:24 -07001056 String first = null;
1057 String second = null;
1058 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001059}
1060{
Till Westmanna4242bc2013-05-08 17:49:55 -07001061 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001062 {
Till Westmann14a20a72013-05-09 00:06:24 -07001063 Identifier id1 = null;
1064 Identifier id2 = null;
1065 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001066 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001067 id2 = new Identifier(first);
1068 id3 = new Identifier(second);
1069 } else {
1070 id1 = new Identifier(first);
1071 id2 = new Identifier(second);
1072 id3 = new Identifier(third);
1073 }
1074 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001075 }
1076}
1077
vinayakb38b7ca42012-03-05 05:44:15 +00001078FunctionDecl FunctionDeclaration() throws ParseException:
1079{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001080 FunctionDecl funcDecl;
1081 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001082 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001083 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1084 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001085 createNewScope();
1086}
1087{
Till Westmannd7dcb122013-05-16 13:19:09 -07001088 "declare" "function" functionName = Identifier()
1089 paramList = ParameterList()
1090 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001091 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001092 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001093 getCurrentScope().addFunctionDescriptor(signature, false);
1094 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001095 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001096 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001097 }
1098}
1099
vinayakb38b7ca42012-03-05 05:44:15 +00001100
Till Westmann31c21f92013-05-08 09:21:53 -07001101Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001102{
1103 Query query = new Query();
1104 Expression expr;
1105}
1106{
Till Westmann31c21f92013-05-08 09:21:53 -07001107 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001108 {
1109 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001110 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001111 return query;
1112 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001113
vinayakb38b7ca42012-03-05 05:44:15 +00001114}
1115
1116
1117
1118Expression Expression():
1119{
1120 Expression expr = null;
1121 Expression exprP = null;
1122}
1123{
1124(
1125
1126//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1127 expr = OperatorExpr()
1128 | expr = IfThenElse()
1129 | expr = FLWOGR()
1130 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001131
vinayakb38b7ca42012-03-05 05:44:15 +00001132
1133)
1134 {
1135 return (exprP==null) ? expr : exprP;
1136 }
1137}
1138
1139
1140
1141Expression OperatorExpr()throws ParseException:
1142{
1143 OperatorExpr op = null;
1144 Expression operand = null;
1145}
1146{
1147 operand = AndExpr()
1148 (
1149
1150 "or"
1151 {
1152 if (op == null) {
1153 op = new OperatorExpr();
1154 op.addOperand(operand);
1155 op.setCurrentop(true);
1156 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001157 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001158 }
1159
1160 operand = AndExpr()
1161 {
1162 op.addOperand(operand);
1163 }
1164
1165 )*
1166
1167 {
1168 return op==null? operand: op;
1169 }
1170}
1171
1172Expression AndExpr()throws ParseException:
1173{
1174 OperatorExpr op = null;
1175 Expression operand = null;
1176}
1177{
1178 operand = RelExpr()
1179 (
1180
1181 "and"
1182 {
1183 if (op == null) {
1184 op = new OperatorExpr();
1185 op.addOperand(operand);
1186 op.setCurrentop(true);
1187 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001188 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001189 }
1190
1191 operand = RelExpr()
1192 {
1193 op.addOperand(operand);
1194 }
1195
1196 )*
1197
1198 {
1199 return op==null? operand: op;
1200 }
1201}
1202
1203
1204
1205Expression RelExpr()throws ParseException:
1206{
1207 OperatorExpr op = null;
1208 Expression operand = null;
1209 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001210 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001211}
1212{
1213 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001214 {
1215 if (operand instanceof VariableExpr) {
1216 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001217 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001218 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001219 }
1220 }
1221 }
1222
1223 (
1224 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1225 {
alexander.behm07617fd2012-07-25 10:13:50 +00001226 String mhint = getHint(token);
1227 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1228 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1229 }
vinayakb38b7ca42012-03-05 05:44:15 +00001230 if (op == null) {
1231 op = new OperatorExpr();
1232 op.addOperand(operand, broadcast);
1233 op.setCurrentop(true);
1234 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001235 }
1236 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001237 }
1238
1239 operand = AddExpr()
1240 {
alexander.behm07617fd2012-07-25 10:13:50 +00001241 broadcast = false;
1242 if (operand instanceof VariableExpr) {
1243 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001244 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1245 broadcast = true;
1246 }
alexander.behm07617fd2012-07-25 10:13:50 +00001247 }
vinayakb38b7ca42012-03-05 05:44:15 +00001248 op.addOperand(operand, broadcast);
1249 }
1250 )?
1251
1252 {
alexander.behm07617fd2012-07-25 10:13:50 +00001253 if (annotation != null) {
1254 op.addHint(annotation);
1255 }
vinayakb38b7ca42012-03-05 05:44:15 +00001256 return op==null? operand: op;
1257 }
1258}
1259
1260Expression AddExpr()throws ParseException:
1261{
1262 OperatorExpr op = null;
1263 Expression operand = null;
1264}
1265{
1266 operand = MultExpr()
1267
1268 ( ("+" | "-")
1269 {
1270 if (op == null) {
1271 op = new OperatorExpr();
1272 op.addOperand(operand);
1273 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001274 }
1275 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001276 }
1277
1278 operand = MultExpr()
1279 {
1280 op.addOperand(operand);
1281 }
1282 )*
1283
1284 {
1285 return op==null? operand: op;
1286 }
1287}
1288
1289Expression MultExpr()throws ParseException:
1290{
1291 OperatorExpr op = null;
1292 Expression operand = null;
1293}
1294{
1295 operand = UnionExpr()
1296
1297 (( "*" | "/" | "%" | <CARET> | "idiv")
1298 {
1299 if (op == null) {
1300 op = new OperatorExpr();
1301 op.addOperand(operand);
1302 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001303 }
1304 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001305 }
1306 operand = UnionExpr()
1307 {
1308 op.addOperand(operand);
1309 }
1310 )*
1311
1312 {
1313 return op==null?operand:op;
1314 }
1315}
1316
1317Expression UnionExpr() throws ParseException:
1318{
1319 UnionExpr union = null;
1320 Expression operand1 = null;
1321 Expression operand2 = null;
1322}
1323{
1324 operand1 = UnaryExpr()
1325 ("union"
1326 (operand2 = UnaryExpr()) {
1327 if (union == null) {
1328 union = new UnionExpr();
1329 union.addExpr(operand1);
1330 }
1331 union.addExpr(operand2);
1332 } )*
1333 {
1334 return (union == null)? operand1: union;
1335 }
1336}
1337
1338Expression UnaryExpr() throws ParseException:
1339{
1340 Expression uexpr = null;
1341 Expression expr = null;
1342}
1343{
1344 (( "+"|"-")
1345 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001346 uexpr = new UnaryExpr();
1347 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001348 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001349 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001350 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1351 else
1352 throw new ParseException();
1353 }
1354 )?
1355
1356 expr = ValueExpr()
1357 {
1358 if(uexpr!=null){
1359 ((UnaryExpr)uexpr).setExpr(expr);
1360 return uexpr;
1361 }
1362 else{
1363 return expr;
1364 }
1365 }
1366}
1367
Till Westmann04478e72013-05-13 23:01:34 -07001368Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001369{
1370 Expression expr = null;
1371 Identifier ident = null;
1372 AbstractAccessor fa = null;
1373 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001374}
1375{
Till Westmann04478e72013-05-13 23:01:34 -07001376 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001377 {
Till Westmann04478e72013-05-13 23:01:34 -07001378 fa = (fa == null ? new FieldAccessor(expr, ident)
1379 : new FieldAccessor(fa, ident));
1380 }
1381 | index = Index()
1382 {
1383 fa = (fa == null ? new IndexAccessor(expr, index)
1384 : new IndexAccessor(fa, index));
1385 }
1386 )*
1387 {
1388 return fa == null ? expr : fa;
1389 }
vinayakb38b7ca42012-03-05 05:44:15 +00001390}
1391
1392Identifier Field() throws ParseException:
1393{
Till Westmann14a20a72013-05-09 00:06:24 -07001394 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001395}
1396{
Till Westmanna4242bc2013-05-08 17:49:55 -07001397 "." ident = Identifier()
1398 {
Till Westmann14a20a72013-05-09 00:06:24 -07001399 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001400 }
vinayakb38b7ca42012-03-05 05:44:15 +00001401}
1402
1403int Index() throws ParseException:
1404{
1405 Expression expr = null;
1406 int idx = -2;
1407}
1408{
1409 "[" ( expr = Expression()
1410 {
1411 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1412 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001413 Literal lit = ((LiteralExpr)expr).getValue();
1414 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1415 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001416 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001417 }
vinayakb38b7ca42012-03-05 05:44:15 +00001418 else {
1419 throw new ParseException("Index should be an INTEGER");
1420 }
1421 }
1422
1423 }
1424
1425 | "?"
1426 {
1427 idx = IndexAccessor.ANY;
1428 // ANY
1429 }
1430
1431 )
1432
1433 "]"
1434 {
1435 return idx;
1436 }
1437}
1438
1439
1440Expression PrimaryExpr()throws ParseException:
1441{
1442 Expression expr = null;
1443}
1444{
Till Westmann68d99652013-05-09 11:15:21 -07001445 ( LOOKAHEAD(2)
1446 expr = FunctionCallExpr()
1447 | expr = Literal()
1448 | expr = DatasetAccessExpression()
1449 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001450 {
1451 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001452 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001453 }
Till Westmann68d99652013-05-09 11:15:21 -07001454 | expr = ListConstructor()
1455 | expr = RecordConstructor()
1456 | expr = ParenthesizedExpression()
1457 )
1458 {
1459 return expr;
1460 }
vinayakb38b7ca42012-03-05 05:44:15 +00001461}
1462
1463Expression Literal() throws ParseException:
1464{
vinayakb38b7ca42012-03-05 05:44:15 +00001465 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001466 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001467}
1468{
Till Westmann7d535322013-05-09 00:40:02 -07001469 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001470 {
Till Westmann7d535322013-05-09 00:40:02 -07001471 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001472 }
1473 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001474 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001475 try {
1476 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1477 } catch(NumberFormatException ex) {
1478 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1479 }
1480 }
1481 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001482 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001483 lit.setValue(new FloatLiteral(new Float(token.image)));
1484 }
1485 | <DOUBLE_LITERAL>
1486 {
1487 lit.setValue(new DoubleLiteral(new Double(token.image)));
1488 }
1489 | <NULL>
1490 {
1491 lit.setValue(NullLiteral.INSTANCE);
1492 }
1493 | <TRUE>
1494 {
1495 lit.setValue(TrueLiteral.INSTANCE);
1496 }
1497 | <FALSE>
1498 {
1499 lit.setValue(FalseLiteral.INSTANCE);
1500 }
1501 )
vinayakb38b7ca42012-03-05 05:44:15 +00001502 {
1503 return lit;
1504 }
1505}
1506
1507
1508VariableExpr VariableRef() throws ParseException:
1509{
1510 VariableExpr varExp = new VariableExpr();
1511 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001512}
1513{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001514 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001515 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001516 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001517 Identifier ident = lookupSymbol(varName);
1518 if (isInForbiddenScopes(varName)) {
1519 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.");
1520 }
1521 if(ident != null) { // exist such ident
1522 varExp.setIsNewVar(false);
1523 varExp.setVar((VarIdentifier)ident);
1524 } else {
1525 varExp.setVar(var);
1526 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001527 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001528 return varExp;
1529 }
1530}
1531
1532
1533VariableExpr Variable() throws ParseException:
1534{
1535 VariableExpr varExp = new VariableExpr();
1536 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001537}
1538{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001539 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001540 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001541 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001542 if(ident != null) { // exist such ident
1543 varExp.setIsNewVar(false);
1544 }
1545 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001546 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001547 return varExp;
1548 }
1549}
1550
1551Expression ListConstructor() throws ParseException:
1552{
1553 Expression expr = null;
1554}
1555{
1556 (
1557 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1558 )
1559
1560 {
1561 return expr;
1562 }
1563}
1564
1565
1566ListConstructor OrderedListConstructor() throws ParseException:
1567{
1568 ListConstructor expr = new ListConstructor();
1569 Expression tmp = null;
1570 List<Expression> exprList = new ArrayList<Expression>();
1571 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1572}
1573{
1574
1575 "["
1576 ( tmp = Expression()
1577 {
1578 exprList.add(tmp);
1579 }
1580
1581 ("," tmp = Expression() { exprList.add(tmp); })*
1582 )?
1583
1584 "]"
1585
1586 {
1587 expr.setExprList(exprList);
1588 return expr;
1589 }
1590}
1591
1592ListConstructor UnorderedListConstructor() throws ParseException:
1593{
1594 ListConstructor expr = new ListConstructor();
1595 Expression tmp = null;
1596 List<Expression> exprList = new ArrayList<Expression>();
1597 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1598}
1599{
1600
1601 "{{" ( tmp = Expression()
1602 {
1603 exprList.add(tmp);
1604 }
1605 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1606 {
1607 expr.setExprList(exprList);
1608 return expr;
1609 }
1610}
1611
1612RecordConstructor RecordConstructor() throws ParseException:
1613{
1614 RecordConstructor expr = new RecordConstructor();
1615 FieldBinding tmp = null;
1616 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1617}
1618{
1619 "{" (tmp = FieldBinding()
1620 {
1621 fbList.add(tmp);
1622 }
1623 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1624 {
1625 expr.setFbList(fbList);
1626 return expr;
1627 }
1628}
1629
1630FieldBinding FieldBinding() throws ParseException:
1631{
1632 FieldBinding fb = new FieldBinding();
1633 Expression left, right;
1634}
1635{
1636 left = Expression() ":" right = Expression()
1637 {
1638 fb.setLeftExpr(left);
1639 fb.setRightExpr(right);
1640 return fb;
1641 }
1642}
1643
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001644
vinayakb38b7ca42012-03-05 05:44:15 +00001645Expression FunctionCallExpr() throws ParseException:
1646{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001647 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001648 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001649 Expression tmp;
1650 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001651 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001652 String funcName;
1653 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001654 String hint = null;
1655 String id1 = null;
1656 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001657}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001658{
Till Westmann31c21f92013-05-08 09:21:53 -07001659 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001660 {
Till Westmann31c21f92013-05-08 09:21:53 -07001661 dataverse = funcId.first.getValue();
1662 funcName = funcId.second.getValue();
1663 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001664 }
Till Westmann31c21f92013-05-08 09:21:53 -07001665 <LEFTPAREN> (tmp = Expression()
1666 {
1667 argList.add(tmp);
1668 arity ++;
1669 }
1670 ("," tmp = Expression()
1671 {
1672 argList.add(tmp);
1673 arity++;
1674 }
1675 )*)? <RIGHTPAREN>
1676 {
1677 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1678 if (signature == null) {
1679 signature = new FunctionSignature(dataverse, funcName, arity);
1680 }
1681 callExpr = new CallExpr(signature,argList);
1682 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1683 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1684 }
1685 return callExpr;
1686 }
vinayakb38b7ca42012-03-05 05:44:15 +00001687}
1688
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001689Expression DatasetAccessExpression() throws ParseException:
1690{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001691 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001692 String arg1 = null;
1693 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001694 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001695}
1696{
Till Westmann14a20a72013-05-09 00:06:24 -07001697 <DATASET>
1698 {
Till Westmann14a20a72013-05-09 00:06:24 -07001699 funcName = token.image;
1700 }
1701 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1702 {
1703 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001704 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001705 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001706 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001707 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001708 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001709 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001710 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1711 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1712 if (signature == null) {
1713 signature = new FunctionSignature(dataverse, funcName, 1);
1714 }
1715 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001716 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001717 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001718 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001719}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001720
vinayakb38b7ca42012-03-05 05:44:15 +00001721Expression ParenthesizedExpression() throws ParseException:
1722{
1723 Expression expr;
1724}
1725{
1726 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1727 {
1728 return expr;
1729 }
1730}
1731
1732Expression IfThenElse() throws ParseException:
1733{
1734 Expression condExpr;
1735 Expression thenExpr;
1736 Expression elseExpr;
1737 IfExpr ifExpr = new IfExpr();
1738}
1739{
1740 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1741
1742 {
1743 ifExpr.setCondExpr(condExpr);
1744 ifExpr.setThenExpr(thenExpr);
1745 ifExpr.setElseExpr(elseExpr);
1746 return ifExpr;
1747 }
1748}
1749
1750Expression FLWOGR() throws ParseException:
1751{
1752 FLWOGRExpression flworg = new FLWOGRExpression();
1753 List<Clause> clauseList = new ArrayList<Clause>();
1754 Expression returnExpr;
1755 Clause tmp;
1756 createNewScope();
1757}
1758{
1759 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1760 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1761
1762 {
1763 flworg.setClauseList(clauseList);
1764 flworg.setReturnExpr(returnExpr);
1765 removeCurrentScope();
1766 return flworg;
1767 }
1768}
1769
1770Clause Clause()throws ParseException :
1771{
1772 Clause clause;
1773}
1774{
1775 (
1776 clause = ForClause()
1777 | clause = LetClause()
1778 | clause = WhereClause()
1779 | clause = OrderbyClause()
1780 | clause = GroupClause()
1781 | clause = LimitClause()
1782 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001783 )
1784 {
1785 return clause;
1786 }
1787}
1788
1789Clause ForClause()throws ParseException :
1790{
1791 ForClause fc = new ForClause();
1792 VariableExpr varExp;
1793 VariableExpr varPos = null;
1794 Expression inExp;
1795 extendCurrentScope();
1796}
1797{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001798 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001799 {
1800 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001801 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001802 fc.setInExpr(inExp);
1803 if (varPos != null) {
1804 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001805 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001806 }
1807 return fc;
1808 }
1809}
1810
1811Clause LetClause() throws ParseException:
1812{
1813 LetClause lc = new LetClause();
1814 VariableExpr varExp;
1815 Expression beExp;
1816 extendCurrentScope();
1817}
1818{
ilovesoupb2527c12012-07-12 03:21:13 +00001819 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001820 {
1821 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001822 lc.setVarExpr(varExp);
1823 lc.setBeExpr(beExp);
1824 return lc;
1825 }
1826}
1827
1828Clause WhereClause()throws ParseException :
1829{
1830 WhereClause wc = new WhereClause();
1831 Expression whereExpr;
1832}
1833{
1834 "where" whereExpr = Expression()
1835 {
1836 wc.setWhereExpr(whereExpr);
1837 return wc;
1838 }
1839}
1840
1841Clause OrderbyClause()throws ParseException :
1842{
1843 OrderbyClause oc = new OrderbyClause();
1844 Expression orderbyExpr;
1845 List<Expression> orderbyList = new ArrayList<Expression>();
1846 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1847 int numOfOrderby = 0;
1848}
1849{
1850 (
1851 "order"
1852 {
1853 String hint = getHint(token);
1854 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1855 String splits[] = hint.split(" +");
1856 int numFrames = Integer.parseInt(splits[1]);
1857 int numTuples = Integer.parseInt(splits[2]);
1858 oc.setNumFrames(numFrames);
1859 oc.setNumTuples(numTuples);
1860 }
1861 }
1862 "by" orderbyExpr = Expression()
1863 {
1864 orderbyList.add(orderbyExpr);
1865 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1866 }
1867 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1868 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1869 {
1870 modifierList.add(modif);
1871 }
1872
1873 ("," orderbyExpr = Expression()
1874 {
1875 orderbyList.add(orderbyExpr);
1876 modif = OrderbyClause.OrderModifier.ASC;
1877 }
1878 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1879 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1880 {
1881 modifierList.add(modif);
1882 }
1883 )*
1884)
1885 {
1886 oc.setModifierList(modifierList);
1887 oc.setOrderbyList(orderbyList);
1888 return oc;
1889 }
1890}
1891Clause GroupClause()throws ParseException :
1892{
1893 GroupbyClause gbc = new GroupbyClause();
1894 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1895 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1896 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1897 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1898 VariableExpr var = null;
1899 VariableExpr withVar = null;
1900 Expression expr = null;
1901 VariableExpr decorVar = null;
1902 Expression decorExpr = null;
1903}
1904{
1905 {
1906 Scope newScope = extendCurrentScopeNoPush(true);
1907 // extendCurrentScope(true);
1908 }
1909 "group"
1910 {
1911 String hint = getHint(token);
1912 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1913 gbc.setHashGroupByHint(true);
1914 }
1915 }
1916 "by" (LOOKAHEAD(2) var = Variable()
1917 {
1918 newScope.addNewVarSymbolToScope(var.getVar());
1919 } ":=")?
1920 expr = Expression()
1921 {
1922 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1923 vePairList.add(pair1);
1924 }
1925 ("," ( LOOKAHEAD(2) var = Variable()
1926 {
1927 newScope.addNewVarSymbolToScope(var.getVar());
1928 } ":=")?
1929 expr = Expression()
1930 {
1931 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1932 vePairList.add(pair2);
1933 }
1934 )*
1935 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1936 {
1937 newScope.addNewVarSymbolToScope(decorVar.getVar());
1938 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1939 decorPairList.add(pair3);
1940 }
1941 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1942 {
1943 newScope.addNewVarSymbolToScope(decorVar.getVar());
1944 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1945 decorPairList.add(pair4);
1946 }
1947 )*
1948 )?
1949 "with" withVar = VariableRef()
1950 {
1951 if(withVar.getIsNewVar()==true)
1952 throw new ParseException("can't find variable " + withVar.getVar());
1953 withVarList.add(withVar);
1954 newScope.addNewVarSymbolToScope(withVar.getVar());
1955 }
1956 ("," withVar = VariableRef()
1957 {
1958 if(withVar.getIsNewVar()==true)
1959 throw new ParseException("can't find variable " + withVar.getVar());
1960 withVarList.add(withVar);
1961 newScope.addNewVarSymbolToScope(withVar.getVar());
1962 })*
1963 {
1964 gbc.setGbyPairList(vePairList);
1965 gbc.setDecorPairList(decorPairList);
1966 gbc.setWithVarList(withVarList);
1967 replaceCurrentScope(newScope);
1968 return gbc;
1969 }
1970}
1971
1972
1973LimitClause LimitClause() throws ParseException:
1974{
1975 LimitClause lc = new LimitClause();
1976 Expression expr;
1977 pushForbiddenScope(getCurrentScope());
1978}
1979{
1980 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1981 ("offset" expr = Expression() { lc.setOffset(expr); })?
1982
1983 {
1984 popForbiddenScope();
1985 return lc;
1986 }
1987}
1988
1989DistinctClause DistinctClause() throws ParseException:
1990{
1991 List<Expression> exprs = new ArrayList<Expression>();
1992 Expression expr;
1993}
1994{
1995 "distinct" "by" expr = Expression()
1996 {
1997 exprs.add(expr);
1998 }
1999 ("," expr = Expression()
2000 {
2001 exprs.add(expr);
2002 }
2003 )*
2004 {
2005 return new DistinctClause(exprs);
2006 }
2007}
2008
vinayakb38b7ca42012-03-05 05:44:15 +00002009QuantifiedExpression QuantifiedExpression()throws ParseException:
2010{
2011 QuantifiedExpression qc = new QuantifiedExpression();
2012 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2013 Expression satisfiesExpr;
2014 VariableExpr var;
2015 Expression inExpr;
2016 QuantifiedPair pair;
2017}
2018{
2019 {
2020 createNewScope();
2021 }
2022
2023 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2024 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2025 var = Variable() "in" inExpr = Expression()
2026 {
2027 pair = new QuantifiedPair(var, inExpr);
2028 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2029 quantifiedList.add(pair);
2030 }
2031 (
2032 "," var = Variable() "in" inExpr = Expression()
2033 {
2034 pair = new QuantifiedPair(var, inExpr);
2035 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2036 quantifiedList.add(pair);
2037 }
2038 )*
2039 "satisfies" satisfiesExpr = Expression()
2040 {
2041 qc.setSatisfiesExpr(satisfiesExpr);
2042 qc.setQuantifiedList(quantifiedList);
2043 removeCurrentScope();
2044 return qc;
2045 }
2046}
2047
2048TOKEN_MGR_DECLS:
2049{
2050 public int commentDepth = 0;
2051}
2052
2053<DEFAULT>
2054TOKEN :
2055{
2056 <CARET : "^" >
2057}
2058
2059<DEFAULT>
2060TOKEN :
2061{
2062 <DATASET : "dataset" >
2063}
2064
2065<DEFAULT>
2066TOKEN :
2067{
2068 <LEFTPAREN : "(" >
2069}
2070
2071<DEFAULT>
2072TOKEN :
2073{
2074 <RIGHTPAREN : ")" >
2075}
2076
2077
2078<DEFAULT>
2079TOKEN :
2080{
2081 <INTEGER_LITERAL : (<DIGIT>)+ >
2082}
2083
2084
2085<DEFAULT>
2086TOKEN :
2087{
2088 <NULL : "null">
2089}
2090
2091<DEFAULT>
2092TOKEN :
2093{
2094 <TRUE : "true">
2095}
2096
2097<DEFAULT>
2098TOKEN :
2099{
2100 <FALSE : "false">
2101}
2102
2103<DEFAULT>
2104TOKEN :
2105{
2106 <#DIGIT : ["0" - "9"]>
2107}
2108
2109
2110TOKEN:
2111{
2112 < DOUBLE_LITERAL: <INTEGER>
2113 | <INTEGER> ( "." <INTEGER> )?
2114 | "." <INTEGER>
2115 >
2116 |
2117 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2118 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2119 | "." <INTEGER> ( "f" | "F" )
2120 >
2121 |
2122 <INTEGER : (<DIGIT>)+ >
2123}
2124
2125<DEFAULT>
2126TOKEN :
2127{
2128 <#LETTER : ["A" - "Z", "a" - "z"]>
2129}
2130
2131<DEFAULT>
2132TOKEN :
2133{
2134 <SPECIALCHARS : ["$", "_", "-"] >
2135}
2136
2137<DEFAULT>
2138TOKEN :
2139{
2140 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2141 |
2142 < #EscapeQuot: "\\\"" >
2143 |
2144 < #EscapeApos: "\\\'" >
2145}
2146
2147<DEFAULT>
2148TOKEN :
2149{
2150 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2151}
2152
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002153
vinayakb38b7ca42012-03-05 05:44:15 +00002154<DEFAULT>
2155TOKEN :
2156{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002157 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002158}
2159
2160SKIP:
2161{
2162 " "
2163| "\t"
2164| "\r"
2165| "\n"
2166}
2167
2168SKIP:
2169{
2170 <"//" (~["\n"])* "\n">
2171}
2172
2173SKIP:
2174{
2175 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2176}
2177
2178
2179SKIP:
2180{
2181 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2182}
2183
2184<INSIDE_COMMENT>
2185SPECIAL_TOKEN:
2186{
2187 <"+"(" ")*(~["*"])*>
2188}
2189
2190<INSIDE_COMMENT>
2191SKIP:
2192{
2193 <"/*"> {commentDepth++;}
2194}
2195
2196<INSIDE_COMMENT>
2197SKIP:
2198{
2199 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2200| <~[]>
2201}