vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 1 | options { |
| 2 | |
| 3 | |
| 4 | STATIC = false; |
| 5 | |
| 6 | } |
| 7 | |
| 8 | |
| 9 | PARSER_BEGIN(AQLPlusParser) |
| 10 | |
| 11 | package edu.uci.ics.asterix.aqlplus.parser; |
| 12 | |
| 13 | import java.io.*; |
| 14 | import java.util.List; |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Stack; |
| 17 | import java.util.Map; |
| 18 | import java.util.HashMap; |
| 19 | |
| 20 | import edu.uci.ics.asterix.aql.literal.FloatLiteral; |
| 21 | import edu.uci.ics.asterix.aql.literal.DoubleLiteral; |
| 22 | import edu.uci.ics.asterix.aql.literal.FalseLiteral; |
ilovesoup | c9fef1d | 2012-07-08 19:30:42 +0000 | [diff] [blame] | 23 | import edu.uci.ics.asterix.aql.base.Literal; |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 24 | import edu.uci.ics.asterix.aql.literal.IntegerLiteral; |
ilovesoup | c9fef1d | 2012-07-08 19:30:42 +0000 | [diff] [blame] | 25 | import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral; |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 26 | import edu.uci.ics.asterix.aql.literal.NullLiteral; |
| 27 | import edu.uci.ics.asterix.aql.literal.StringLiteral; |
| 28 | import edu.uci.ics.asterix.aql.literal.TrueLiteral; |
| 29 | |
| 30 | import edu.uci.ics.asterix.aql.parser.ScopeChecker; |
| 31 | import edu.uci.ics.asterix.aql.base.*; |
| 32 | import edu.uci.ics.asterix.aql.expression.*; |
| 33 | import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor; |
| 34 | import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign; |
| 35 | import edu.uci.ics.asterix.aql.base.Statement.Kind; |
| 36 | import edu.uci.ics.asterix.aql.context.Scope; |
| 37 | import edu.uci.ics.asterix.aql.context.RootScopeFactory; |
| 38 | import edu.uci.ics.asterix.common.exceptions.AsterixException; |
| 39 | |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 40 | import edu.uci.ics.asterix.om.functions.AsterixFunction; |
| 41 | |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 42 | public class AQLPlusParser extends ScopeChecker { |
| 43 | |
| 44 | /* |
| 45 | private void printHints(Token t) { |
| 46 | //System.err.println("token="+t.image+"\t special="+t.specialToken); |
| 47 | if (t.specialToken == null) return; |
| 48 | Token tmp_t = t.specialToken; |
| 49 | while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken; |
| 50 | while (tmp_t != null) { |
| 51 | System.out.println(tmp_t.image); |
| 52 | tmp_t = tmp_t.next; |
| 53 | } |
| 54 | } |
| 55 | */ |
| 56 | |
| 57 | private static final String HASH_GROUP_BY_HINT = "hash"; |
| 58 | private static final String BROADCAST_JOIN_HINT = "bcast"; |
| 59 | private static final String INMEMORY_HINT = "inmem"; |
| 60 | |
| 61 | |
| 62 | private static String getHint(Token t) { |
| 63 | if (t.specialToken == null) { |
| 64 | return null; |
| 65 | } |
| 66 | String s = t.specialToken.image; |
| 67 | int n = s.length(); |
| 68 | if (n < 2) { |
| 69 | return null; |
| 70 | } |
| 71 | return s.substring(1).trim(); |
| 72 | } |
| 73 | |
| 74 | public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException { |
| 75 | File file = new File(args[0]); |
| 76 | Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); |
| 77 | AQLPlusParser parser = new AQLPlusParser(fis); |
| 78 | Statement st = parser.Statement(); |
| 79 | st.accept(new AQLPrintVisitor(), 0); |
| 80 | |
| 81 | // System.out.println("FunctionCalls not found:"); |
| 82 | // for(FunctionDescriptor fd: notFoundFunctionList) |
| 83 | // { |
| 84 | // if(lookupFunctionSignature(fd.getValue(), fd.getArity())!=null) |
| 85 | // notFoundFunctionList.remove(fd); |
| 86 | // } |
| 87 | // System.out.println(notFoundFunctionList.toString()); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | public void initScope() { |
| 92 | scopeStack.push(RootScopeFactory.createRootScope(this)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | PARSER_END(AQLPlusParser) |
| 97 | |
| 98 | |
| 99 | Statement Statement() throws ParseException: |
| 100 | { |
| 101 | Query query = null; |
| 102 | // scopeStack.push(RootScopeFactory.createRootScope(this)); |
| 103 | initScope(); |
| 104 | List<Statement> decls = new ArrayList<Statement>(); |
| 105 | } |
| 106 | { |
| 107 | ( |
| 108 | ( |
| 109 | ( |
| 110 | "use" |
| 111 | { |
| 112 | decls.add(DataverseDeclaration()); |
| 113 | } |
| 114 | | "declare" |
| 115 | ( "function" { |
| 116 | decls.add(FunctionDeclaration()); |
| 117 | } |
| 118 | | "type" { |
| 119 | decls.add(TypeDeclaration()); |
| 120 | } |
| 121 | ) |
| 122 | | "load" { |
| 123 | decls.add(LoadStatement()); |
| 124 | } |
| 125 | |
| 126 | | "write" { |
| 127 | decls.add(WriteStatement()); |
| 128 | } |
| 129 | | "set" { |
| 130 | decls.add(SetStatement()); |
| 131 | } |
| 132 | |
| 133 | )* |
| 134 | (query = Query())? |
| 135 | ) |
| 136 | |
| 137 | <EOF> |
| 138 | ) |
| 139 | { |
| 140 | if (query == null) { |
| 141 | query = new Query(true); |
| 142 | } |
| 143 | query.setPrologDeclList(decls); |
| 144 | |
| 145 | // for(FunctionDecl fdc : fdList) |
| 146 | // { |
| 147 | // FunctionDescriptor fd = (FunctionDescriptor) fdc.getIdent(); |
| 148 | // notFoundFunctionList.remove(fd); |
| 149 | // } |
| 150 | // } |
| 151 | return query; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | Statement SetStatement() throws ParseException: |
| 156 | { |
| 157 | String pn = null; |
| 158 | Statement stmt = null; |
| 159 | } |
| 160 | { |
| 161 | <IDENTIFIER> { pn = token.image; } |
| 162 | <STRING_LITERAL> |
| 163 | { String pv = removeQuotesAndEscapes(token.image); } |
| 164 | ";" |
| 165 | { |
| 166 | return new SetStatement(pn, pv); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Statement WriteStatement() throws ParseException: |
| 171 | { |
| 172 | Identifier nodeName = null; |
| 173 | String fileName = null; |
| 174 | Identifier datasetName = null; |
| 175 | Statement stmt = null; |
| 176 | Query query; |
| 177 | } |
| 178 | { |
| 179 | (( "output" "to" |
| 180 | <IDENTIFIER> { nodeName = new Identifier(token.image); } |
| 181 | ":" <STRING_LITERAL> { |
| 182 | fileName = removeQuotesAndEscapes(token.image); |
| 183 | stmt = new WriteStatement(nodeName, fileName, null); |
| 184 | } ) |
| 185 | | |
| 186 | ( "into" |
| 187 | <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); } |
| 188 | <LEFTPAREN> query = Query() <RIGHTPAREN> |
| 189 | { |
| 190 | stmt = new WriteFromQueryResultStatement(datasetName, query, getVarCounter()); |
| 191 | } )) |
| 192 | |
| 193 | ";" |
| 194 | { |
| 195 | return stmt; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | DataverseDecl DataverseDeclaration() throws ParseException: |
| 200 | { |
| 201 | Identifier dvName = null; |
| 202 | } |
| 203 | { |
| 204 | "dataverse" <IDENTIFIER> { dvName = new Identifier(token.image); } |
| 205 | ";" |
| 206 | { |
| 207 | return new DataverseDecl(dvName); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | LoadFromFileStatement LoadStatement() throws ParseException: |
| 212 | { |
| 213 | Identifier datasetName = null; |
| 214 | boolean alreadySorted = false; |
| 215 | String adapter; |
| 216 | Map<String,String> properties = new HashMap<String,String>(); |
| 217 | String name; |
| 218 | String value; |
| 219 | } |
| 220 | { |
| 221 | <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); } |
| 222 | |
| 223 | "using" |
| 224 | ( |
| 225 | <STRING_LITERAL> |
| 226 | { |
| 227 | adapter = removeQuotesAndEscapes(token.image); |
| 228 | } |
| 229 | <LEFTPAREN> |
| 230 | ( |
| 231 | ( |
| 232 | <LEFTPAREN> |
| 233 | ( |
| 234 | <STRING_LITERAL> |
| 235 | { |
| 236 | name = removeQuotesAndEscapes(token.image); |
| 237 | } |
| 238 | "=" <STRING_LITERAL> |
| 239 | { |
| 240 | value = removeQuotesAndEscapes(token.image); |
| 241 | } |
| 242 | ) |
| 243 | <RIGHTPAREN> |
| 244 | { |
| 245 | properties.put(name, value); |
| 246 | } |
| 247 | ) |
| 248 | ( |
| 249 | "," <LEFTPAREN> |
| 250 | ( |
| 251 | <STRING_LITERAL> |
| 252 | { |
| 253 | name = removeQuotesAndEscapes(token.image); |
| 254 | } |
| 255 | "=" <STRING_LITERAL> |
| 256 | { |
| 257 | value = removeQuotesAndEscapes(token.image); |
| 258 | } |
| 259 | ) |
| 260 | <RIGHTPAREN> |
| 261 | { |
| 262 | properties.put(name, value); |
| 263 | } |
| 264 | )* |
| 265 | )? |
| 266 | <RIGHTPAREN> |
| 267 | ) |
| 268 | |
| 269 | ("pre-sorted" |
| 270 | { alreadySorted = true; } |
| 271 | )? |
| 272 | |
| 273 | ";" |
| 274 | { |
| 275 | return new LoadFromFileStatement(datasetName, adapter, properties, alreadySorted); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | TypeDecl TypeDeclaration() throws ParseException: |
| 280 | { |
| 281 | Identifier ident; |
| 282 | TypeExpression typeExpr; |
| 283 | } |
| 284 | { |
| 285 | <IDENTIFIER> |
| 286 | { |
| 287 | ident = new Identifier(token.image.toString()); |
| 288 | } |
| 289 | "as" |
| 290 | ( typeExpr = TypeExpr() ) |
| 291 | { |
| 292 | return new TypeDecl(ident, typeExpr); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | TypeExpression TypeExpr() throws ParseException: |
| 297 | { |
| 298 | TypeExpression typeExpr = null; |
| 299 | } |
| 300 | { |
| 301 | ( |
| 302 | typeExpr = RecordTypeDef() |
| 303 | | typeExpr = TypeReference() |
| 304 | | typeExpr = OrderedListTypeDef() |
| 305 | | typeExpr = UnorderedListTypeDef() |
| 306 | ) |
| 307 | { |
| 308 | return typeExpr; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | RecordTypeDefinition RecordTypeDef() throws ParseException: |
| 313 | { |
| 314 | RecordTypeDefinition recType = new RecordTypeDefinition(); |
| 315 | RecordTypeDefinition.RecordKind recordKind = null; |
| 316 | } |
| 317 | { |
| 318 | ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; } |
| 319 | | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )? |
| 320 | "{" |
| 321 | ( |
| 322 | RecordField(recType) |
| 323 | ( "," RecordField(recType) )* |
| 324 | )? |
| 325 | "}" |
| 326 | { |
| 327 | if (recordKind == null) { |
| 328 | recordKind = RecordTypeDefinition.RecordKind.OPEN; |
| 329 | } |
| 330 | recType.setRecordKind(recordKind); |
| 331 | return recType; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void RecordField(RecordTypeDefinition recType) throws ParseException: |
| 336 | { |
| 337 | String fieldName; |
| 338 | TypeExpression type = null; |
| 339 | boolean nullable = false; |
| 340 | } |
| 341 | { |
| 342 | <IDENTIFIER> |
| 343 | { |
| 344 | Token t = getToken(0); |
| 345 | fieldName = t.toString(); |
| 346 | } |
| 347 | ":" |
| 348 | ( type = TypeExpr() ) |
| 349 | ("?" { nullable = true; } )? |
| 350 | { |
| 351 | recType.addField(fieldName, type, nullable); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | TypeReferenceExpression TypeReference() throws ParseException: |
| 356 | {} |
| 357 | { |
| 358 | <IDENTIFIER> |
| 359 | { |
| 360 | Token t = getToken(0); |
| 361 | Identifier id = new Identifier(t.toString()); |
| 362 | return new TypeReferenceExpression(id); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | OrderedListTypeDefinition OrderedListTypeDef() throws ParseException: |
| 367 | { |
| 368 | TypeExpression type = null; |
| 369 | } |
| 370 | { |
| 371 | "[" |
| 372 | ( type = TypeExpr() ) |
| 373 | "]" |
| 374 | { |
| 375 | return new OrderedListTypeDefinition(type); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
| 380 | UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException: |
| 381 | { |
| 382 | TypeExpression type = null; |
| 383 | } |
| 384 | { |
| 385 | "<" |
| 386 | ( type = TypeExpr() ) |
| 387 | ">" |
| 388 | { |
| 389 | return new UnorderedListTypeDefinition(type); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | FunctionDecl FunctionDeclaration() throws ParseException: |
| 395 | { |
| 396 | FunctionDecl func = new FunctionDecl(); |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 397 | AsterixFunction ident; |
| 398 | String functionName; |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 399 | int arity = 0; |
| 400 | List<VarIdentifier> paramList = new ArrayList<VarIdentifier>(); |
| 401 | Expression funcBody; |
| 402 | VarIdentifier var = null; |
| 403 | createNewScope(); |
| 404 | } |
| 405 | { |
| 406 | |
| 407 | <IDENTIFIER> |
| 408 | { |
| 409 | Token t = getToken(0); |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 410 | functionName = t.toString(); |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 411 | } |
| 412 | <LEFTPAREN> (<VARIABLE> |
| 413 | { |
| 414 | var = new VarIdentifier(); |
| 415 | var.setValue(getToken(0).toString()); |
| 416 | paramList.add(var); |
| 417 | getCurrentScope().addNewVarSymbolToScope(var); |
| 418 | arity++; |
| 419 | } |
| 420 | ("," <VARIABLE> |
| 421 | { |
| 422 | var = new VarIdentifier(); |
| 423 | var.setValue(getToken(0).toString()); |
| 424 | paramList.add(var); |
| 425 | getCurrentScope().addNewVarSymbolToScope(var); |
| 426 | arity++; |
| 427 | })*)? <RIGHTPAREN> "{" funcBody = Expression() "}" |
| 428 | |
| 429 | { |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 430 | ident = new AsterixFunction(functionName, arity); |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 431 | getCurrentScope().addFunctionDescriptor(ident, false); |
| 432 | func.setIdent(ident); |
| 433 | func.setFuncBody(funcBody); |
| 434 | func.setParamList(paramList); |
| 435 | return func; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | Query Query()throws ParseException: |
| 440 | { |
| 441 | Query query = new Query(); |
| 442 | Expression expr; |
| 443 | } |
| 444 | { |
| 445 | expr = Expression() |
| 446 | |
| 447 | { |
| 448 | query.setBody(expr); |
| 449 | return query; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | |
| 455 | Expression Expression(): |
| 456 | { |
| 457 | Expression expr = null; |
| 458 | Expression exprP = null; |
| 459 | } |
| 460 | { |
| 461 | ( |
| 462 | |
| 463 | //OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression |
| 464 | expr = OperatorExpr() |
| 465 | | expr = IfThenElse() |
| 466 | | expr = FLWOGR() |
| 467 | | expr = QuantifiedExpression() |
| 468 | |
| 469 | |
| 470 | ) |
| 471 | { |
| 472 | return (exprP==null) ? expr : exprP; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | |
| 478 | Expression OperatorExpr()throws ParseException: |
| 479 | { |
| 480 | OperatorExpr op = null; |
| 481 | Expression operand = null; |
| 482 | } |
| 483 | { |
| 484 | operand = AndExpr() |
| 485 | ( |
| 486 | |
| 487 | "or" |
| 488 | { |
| 489 | if (op == null) { |
| 490 | op = new OperatorExpr(); |
| 491 | op.addOperand(operand); |
| 492 | op.setCurrentop(true); |
| 493 | } |
| 494 | Token t = getToken(0); |
| 495 | op.addOperator(t.toString()); |
| 496 | } |
| 497 | |
| 498 | operand = AndExpr() |
| 499 | { |
| 500 | op.addOperand(operand); |
| 501 | } |
| 502 | |
| 503 | )* |
| 504 | |
| 505 | { |
| 506 | return op==null? operand: op; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | Expression AndExpr()throws ParseException: |
| 511 | { |
| 512 | OperatorExpr op = null; |
| 513 | Expression operand = null; |
| 514 | } |
| 515 | { |
| 516 | operand = RelExpr() |
| 517 | ( |
| 518 | |
| 519 | "and" |
| 520 | { |
| 521 | if (op == null) { |
| 522 | op = new OperatorExpr(); |
| 523 | op.addOperand(operand); |
| 524 | op.setCurrentop(true); |
| 525 | } |
| 526 | Token t = getToken(0); |
| 527 | op.addOperator(t.toString()); |
| 528 | } |
| 529 | |
| 530 | operand = RelExpr() |
| 531 | { |
| 532 | op.addOperand(operand); |
| 533 | } |
| 534 | |
| 535 | )* |
| 536 | |
| 537 | { |
| 538 | return op==null? operand: op; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | |
| 544 | Expression RelExpr()throws ParseException: |
| 545 | { |
| 546 | OperatorExpr op = null; |
| 547 | Expression operand = null; |
| 548 | boolean broadcast = false; |
| 549 | } |
| 550 | { |
| 551 | operand = AddExpr() |
| 552 | { |
| 553 | if (operand instanceof VariableExpr) { |
| 554 | String hint = getHint(token); |
| 555 | if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) { |
| 556 | broadcast = true; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | ( |
| 562 | LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=") |
| 563 | { |
| 564 | if (op == null) { |
| 565 | op = new OperatorExpr(); |
| 566 | op.addOperand(operand, broadcast); |
| 567 | op.setCurrentop(true); |
| 568 | broadcast = false; |
| 569 | } |
| 570 | Token t = getToken(0); |
| 571 | op.addOperator(t.toString()); |
| 572 | } |
| 573 | |
| 574 | operand = AddExpr() |
| 575 | { |
| 576 | broadcast = false; |
| 577 | if (operand instanceof VariableExpr) { |
| 578 | String hint = getHint(token); |
| 579 | if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) { |
| 580 | broadcast = true; |
| 581 | } |
| 582 | } |
| 583 | op.addOperand(operand, broadcast); |
| 584 | } |
| 585 | )? |
| 586 | |
| 587 | { |
| 588 | return op==null? operand: op; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | Expression AddExpr()throws ParseException: |
| 593 | { |
| 594 | OperatorExpr op = null; |
| 595 | Expression operand = null; |
| 596 | } |
| 597 | { |
| 598 | operand = MultExpr() |
| 599 | |
| 600 | ( ("+" | "-") |
| 601 | { |
| 602 | if (op == null) { |
| 603 | op = new OperatorExpr(); |
| 604 | op.addOperand(operand); |
| 605 | op.setCurrentop(true); |
| 606 | } |
| 607 | Token t = getToken(0); |
| 608 | ((OperatorExpr)op).addOperator(t.toString()); |
| 609 | } |
| 610 | |
| 611 | operand = MultExpr() |
| 612 | { |
| 613 | op.addOperand(operand); |
| 614 | } |
| 615 | )* |
| 616 | |
| 617 | { |
| 618 | return op==null? operand: op; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | Expression MultExpr()throws ParseException: |
| 623 | { |
| 624 | OperatorExpr op = null; |
| 625 | Expression operand = null; |
| 626 | } |
| 627 | { |
| 628 | operand = UnionExpr() |
| 629 | |
| 630 | (( "*" | "/" | "%" | <CARET> | "idiv") |
| 631 | { |
| 632 | if (op == null) { |
| 633 | op = new OperatorExpr(); |
| 634 | op.addOperand(operand); |
| 635 | op.setCurrentop(true); |
| 636 | } |
| 637 | Token t = getToken(0); |
| 638 | op.addOperator(t.toString()); |
| 639 | } |
| 640 | operand = UnionExpr() |
| 641 | { |
| 642 | op.addOperand(operand); |
| 643 | } |
| 644 | )* |
| 645 | |
| 646 | { |
| 647 | return op==null?operand:op; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | Expression UnionExpr() throws ParseException: |
| 652 | { |
| 653 | UnionExpr union = null; |
| 654 | Expression operand1 = null; |
| 655 | Expression operand2 = null; |
| 656 | } |
| 657 | { |
| 658 | operand1 = UnaryExpr() |
| 659 | ("union" |
| 660 | (operand2 = UnaryExpr()) { |
| 661 | if (union == null) { |
| 662 | union = new UnionExpr(); |
| 663 | union.addExpr(operand1); |
| 664 | } |
| 665 | union.addExpr(operand2); |
| 666 | } )* |
| 667 | { |
| 668 | return (union == null)? operand1: union; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | Expression UnaryExpr() throws ParseException: |
| 673 | { |
| 674 | Expression uexpr = null; |
| 675 | Expression expr = null; |
| 676 | } |
| 677 | { |
| 678 | (( "+"|"-") |
| 679 | { |
| 680 | uexpr = new UnaryExpr(); |
| 681 | Token t = getToken(0); |
| 682 | if("+".equals(t.toString())) |
| 683 | ((UnaryExpr)uexpr).setSign(Sign.POSITIVE); |
| 684 | else if("-".equals(t.toString())) |
| 685 | ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE); |
| 686 | else |
| 687 | throw new ParseException(); |
| 688 | } |
| 689 | )? |
| 690 | |
| 691 | expr = ValueExpr() |
| 692 | { |
| 693 | if(uexpr!=null){ |
| 694 | ((UnaryExpr)uexpr).setExpr(expr); |
| 695 | return uexpr; |
| 696 | } |
| 697 | else{ |
| 698 | return expr; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | Expression ValueExpr() throws ParseException: |
| 704 | { |
| 705 | Expression expr; |
| 706 | } |
| 707 | { |
| 708 | expr = FieldOrIndexAccessor() |
| 709 | { |
| 710 | return expr; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | |
| 715 | Expression FieldOrIndexAccessor()throws ParseException: |
| 716 | { |
| 717 | Expression expr = null; |
| 718 | Identifier ident = null; |
| 719 | AbstractAccessor fa = null; |
| 720 | int index; |
| 721 | |
| 722 | } |
| 723 | { |
| 724 | ( expr = PrimaryExpr() |
| 725 | |
| 726 | ) |
| 727 | |
| 728 | |
| 729 | ( |
| 730 | ( |
| 731 | ident = Field() |
| 732 | { |
| 733 | if(fa == null) |
| 734 | fa = new FieldAccessor(expr, ident); |
| 735 | else |
| 736 | fa = new FieldAccessor(fa, ident); |
| 737 | } |
| 738 | ) |
| 739 | | ( |
| 740 | index = Index() |
| 741 | { |
| 742 | if(fa == null) |
| 743 | fa = new IndexAccessor(expr, index); |
| 744 | else |
| 745 | fa = new IndexAccessor(fa, index); |
| 746 | } |
| 747 | ) |
| 748 | )* |
| 749 | |
| 750 | |
| 751 | { |
| 752 | return fa==null?expr:fa; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | Identifier Field() throws ParseException: |
| 757 | { |
| 758 | Identifier ident = null; |
| 759 | |
| 760 | } |
| 761 | { |
| 762 | "." < IDENTIFIER > |
| 763 | { |
| 764 | |
| 765 | ident = new Identifier(); |
| 766 | ident.setValue(getToken(0).toString()); |
| 767 | |
| 768 | return ident; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | int Index() throws ParseException: |
| 773 | { |
| 774 | Expression expr = null; |
| 775 | int idx = -2; |
| 776 | } |
| 777 | { |
| 778 | "[" ( expr = Expression() |
| 779 | { |
| 780 | if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION) |
| 781 | { |
ilovesoup | c9fef1d | 2012-07-08 19:30:42 +0000 | [diff] [blame] | 782 | Literal lit = ((LiteralExpr)expr).getValue(); |
| 783 | if(lit.getLiteralType() == Literal.Type.INTEGER || |
| 784 | lit.getLiteralType() == Literal.Type.LONG) { |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 785 | idx = Integer.valueOf(lit.getStringValue()); |
| 786 | } |
| 787 | else { |
| 788 | throw new ParseException("Index should be an INTEGER"); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | } |
| 793 | |
| 794 | | "?" |
| 795 | { |
| 796 | idx = IndexAccessor.ANY; |
| 797 | // ANY |
| 798 | } |
| 799 | |
| 800 | ) |
| 801 | |
| 802 | "]" |
| 803 | { |
| 804 | return idx; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
| 809 | Expression PrimaryExpr()throws ParseException: |
| 810 | { |
| 811 | Expression expr = null; |
| 812 | } |
| 813 | { |
ilovesoup | c9fef1d | 2012-07-08 19:30:42 +0000 | [diff] [blame] | 814 | //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 815 | ( |
| 816 | expr =Literal() |
| 817 | | expr = FunctionCallExpr() |
| 818 | | expr =VariableRef() |
| 819 | |
| 820 | { |
| 821 | if(((VariableExpr)expr).getIsNewVar() == true) |
| 822 | throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar()); |
| 823 | } |
| 824 | | expr = ListConstructor() |
| 825 | | expr = RecordConstructor() |
| 826 | | expr = ParenthesizedExpression() |
| 827 | | expr = MetaVariableRef() |
| 828 | ) |
| 829 | { |
| 830 | return expr; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | Expression Literal() throws ParseException: |
| 835 | { |
| 836 | |
| 837 | LiteralExpr lit = new LiteralExpr(); |
| 838 | Token t; |
| 839 | } |
| 840 | { |
| 841 | ( |
| 842 | <STRING_LITERAL> |
| 843 | { |
| 844 | t= getToken(0); |
| 845 | lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image))); |
| 846 | } |
| 847 | |
| 848 | | <INTEGER_LITERAL> |
| 849 | { |
| 850 | t= getToken(0); |
ilovesoup | c9fef1d | 2012-07-08 19:30:42 +0000 | [diff] [blame] | 851 | try { |
| 852 | lit.setValue(new IntegerLiteral(new Integer(t.image))); |
| 853 | } catch(NumberFormatException ex) { |
| 854 | lit.setValue(new LongIntegerLiteral(new Long(t.image))); |
| 855 | } |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 856 | } |
| 857 | | < FLOAT_LITERAL > |
| 858 | { |
| 859 | t= getToken(0); |
| 860 | lit.setValue(new FloatLiteral(new Float(t.image))); |
| 861 | } |
| 862 | | < DOUBLE_LITERAL > |
| 863 | { |
| 864 | t= getToken(0); |
| 865 | lit.setValue(new DoubleLiteral(new Double(t.image))); |
| 866 | } |
| 867 | | <NULL> |
| 868 | { |
| 869 | t= getToken(0); |
| 870 | lit.setValue(NullLiteral.INSTANCE); |
| 871 | } |
| 872 | | <TRUE> |
| 873 | { |
| 874 | t= getToken(0); |
| 875 | lit.setValue(TrueLiteral.INSTANCE); |
| 876 | } |
| 877 | | <FALSE> |
| 878 | { |
| 879 | t= getToken(0); |
| 880 | lit.setValue(FalseLiteral.INSTANCE); |
| 881 | } |
| 882 | ) |
| 883 | { |
| 884 | return lit; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | |
| 889 | VariableExpr VariableRef() throws ParseException: |
| 890 | { |
| 891 | VariableExpr varExp = new VariableExpr(); |
| 892 | VarIdentifier var = new VarIdentifier(); |
| 893 | Token t; |
| 894 | } |
| 895 | { |
| 896 | <VARIABLE> |
| 897 | { |
| 898 | t = getToken(0);//get current token |
| 899 | String varName = t.toString(); |
| 900 | Identifier ident = lookupSymbol(varName); |
| 901 | if (isInForbiddenScopes(varName)) { |
| 902 | 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."); |
| 903 | } |
| 904 | if(ident != null) { // exist such ident |
| 905 | varExp.setIsNewVar(false); |
| 906 | varExp.setVar((VarIdentifier)ident); |
| 907 | } else { |
| 908 | varExp.setVar(var); |
| 909 | } |
| 910 | var.setValue(t.toString()); |
| 911 | return varExp; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
| 916 | VariableExpr Variable() throws ParseException: |
| 917 | { |
| 918 | VariableExpr varExp = new VariableExpr(); |
| 919 | VarIdentifier var = new VarIdentifier(); |
| 920 | Token t; |
| 921 | } |
| 922 | { |
| 923 | <VARIABLE> |
| 924 | { |
| 925 | t = getToken(0);//get current token |
| 926 | Identifier ident = lookupSymbol(t.toString()); |
| 927 | if(ident != null) { // exist such ident |
| 928 | varExp.setIsNewVar(false); |
| 929 | } |
| 930 | varExp.setVar(var); |
| 931 | var.setValue(t.toString()); |
| 932 | return varExp; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | MetaVariableExpr MetaVariableRef() throws ParseException: |
| 937 | { |
| 938 | MetaVariableExpr metaVarExp = new MetaVariableExpr(); |
| 939 | VarIdentifier var = new VarIdentifier(); |
| 940 | Token t; |
| 941 | } |
| 942 | { |
| 943 | <METAVARIABLE> |
| 944 | { |
| 945 | t = getToken(0);//get current token |
| 946 | metaVarExp.setVar(var); |
| 947 | var.setValue(t.toString()); |
| 948 | return metaVarExp; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | Expression ListConstructor() throws ParseException: |
| 953 | { |
| 954 | Expression expr = null; |
| 955 | } |
| 956 | { |
| 957 | ( |
| 958 | expr = OrderedListConstructor() | expr = UnorderedListConstructor() |
| 959 | ) |
| 960 | |
| 961 | { |
| 962 | return expr; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | |
| 967 | ListConstructor OrderedListConstructor() throws ParseException: |
| 968 | { |
| 969 | ListConstructor expr = new ListConstructor(); |
| 970 | Expression tmp = null; |
| 971 | List<Expression> exprList = new ArrayList<Expression>(); |
| 972 | expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR); |
| 973 | } |
| 974 | { |
| 975 | |
| 976 | "[" |
| 977 | ( tmp = Expression() |
| 978 | { |
| 979 | exprList.add(tmp); |
| 980 | } |
| 981 | |
| 982 | ("," tmp = Expression() { exprList.add(tmp); })* |
| 983 | )? |
| 984 | |
| 985 | "]" |
| 986 | |
| 987 | { |
| 988 | expr.setExprList(exprList); |
| 989 | return expr; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | ListConstructor UnorderedListConstructor() throws ParseException: |
| 994 | { |
| 995 | ListConstructor expr = new ListConstructor(); |
| 996 | Expression tmp = null; |
| 997 | List<Expression> exprList = new ArrayList<Expression>(); |
| 998 | expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR); |
| 999 | } |
| 1000 | { |
| 1001 | |
| 1002 | "{{" ( tmp = Expression() |
| 1003 | { |
| 1004 | exprList.add(tmp); |
| 1005 | } |
| 1006 | ("," tmp = Expression() { exprList.add(tmp); })*)? "}}" |
| 1007 | { |
| 1008 | expr.setExprList(exprList); |
| 1009 | return expr; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | RecordConstructor RecordConstructor() throws ParseException: |
| 1014 | { |
| 1015 | RecordConstructor expr = new RecordConstructor(); |
| 1016 | FieldBinding tmp = null; |
| 1017 | List<FieldBinding> fbList = new ArrayList<FieldBinding>(); |
| 1018 | } |
| 1019 | { |
| 1020 | "{" (tmp = FieldBinding() |
| 1021 | { |
| 1022 | fbList.add(tmp); |
| 1023 | } |
| 1024 | ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}" |
| 1025 | { |
| 1026 | expr.setFbList(fbList); |
| 1027 | return expr; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | FieldBinding FieldBinding() throws ParseException: |
| 1032 | { |
| 1033 | FieldBinding fb = new FieldBinding(); |
| 1034 | Expression left, right; |
| 1035 | } |
| 1036 | { |
| 1037 | left = Expression() ":" right = Expression() |
| 1038 | { |
| 1039 | fb.setLeftExpr(left); |
| 1040 | fb.setRightExpr(right); |
| 1041 | return fb; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | Expression FunctionCallExpr() throws ParseException: |
| 1046 | { |
| 1047 | CallExpr pf = new CallExpr(); |
| 1048 | List<Expression > argList = new ArrayList<Expression >(); |
| 1049 | Expression tmp; |
| 1050 | int arity = 0; |
| 1051 | Token funcName; |
| 1052 | } |
| 1053 | { |
| 1054 | ( <IDENTIFIER> | <DATASET> ) |
| 1055 | { |
| 1056 | funcName = getToken(0); |
| 1057 | } |
| 1058 | <LEFTPAREN> (tmp = Expression() |
| 1059 | { |
| 1060 | argList.add(tmp); |
| 1061 | arity ++; |
| 1062 | } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN> |
| 1063 | |
| 1064 | { |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 1065 | AsterixFunction fd = lookupFunctionSignature(funcName.toString(), arity); |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 1066 | if(fd == null) |
| 1067 | { |
ramangrover29 | a13f242 | 2012-03-15 23:01:27 +0000 | [diff] [blame] | 1068 | fd = new AsterixFunction(funcName.toString(), arity); |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 1069 | // notFoundFunctionList.add(fd); |
| 1070 | } |
| 1071 | // throw new ParseException("can't find function "+ funcName.toString() + "@" + arity); |
| 1072 | pf.setIdent(fd); |
| 1073 | pf.setExprList(argList); |
| 1074 | return pf; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | Expression ParenthesizedExpression() throws ParseException: |
| 1080 | { |
| 1081 | Expression expr; |
| 1082 | } |
| 1083 | { |
| 1084 | <LEFTPAREN> expr = Expression() <RIGHTPAREN> |
| 1085 | { |
| 1086 | return expr; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | Expression IfThenElse() throws ParseException: |
| 1091 | { |
| 1092 | Expression condExpr; |
| 1093 | Expression thenExpr; |
| 1094 | Expression elseExpr; |
| 1095 | IfExpr ifExpr = new IfExpr(); |
| 1096 | } |
| 1097 | { |
| 1098 | "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression() |
| 1099 | |
| 1100 | { |
| 1101 | ifExpr.setCondExpr(condExpr); |
| 1102 | ifExpr.setThenExpr(thenExpr); |
| 1103 | ifExpr.setElseExpr(elseExpr); |
| 1104 | return ifExpr; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | Expression FLWOGR() throws ParseException: |
| 1109 | { |
| 1110 | FLWOGRExpression flworg = new FLWOGRExpression(); |
| 1111 | List<Clause> clauseList = new ArrayList<Clause>(); |
| 1112 | Expression returnExpr; |
| 1113 | Clause tmp; |
| 1114 | createNewScope(); |
| 1115 | } |
| 1116 | { |
| 1117 | (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);}) |
| 1118 | (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression() |
| 1119 | |
| 1120 | { |
| 1121 | flworg.setClauseList(clauseList); |
| 1122 | flworg.setReturnExpr(returnExpr); |
| 1123 | removeCurrentScope(); |
| 1124 | return flworg; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | List<Clause> Clauses() throws ParseException: |
| 1129 | { |
| 1130 | List<Clause> clauses = new ArrayList<Clause>(); |
| 1131 | Clause c = null; |
| 1132 | } |
| 1133 | { |
| 1134 | ( |
| 1135 | ( |
| 1136 | c = Clause() { |
| 1137 | clauses.add(c); |
| 1138 | } |
| 1139 | )* |
| 1140 | ) |
| 1141 | { |
| 1142 | return clauses; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | Clause Clause() throws ParseException : |
| 1147 | { |
| 1148 | Clause clause; |
| 1149 | } |
| 1150 | { |
| 1151 | ( |
| 1152 | clause = ForClause() |
| 1153 | | clause = LetClause() |
| 1154 | | clause = WhereClause() |
| 1155 | | clause = OrderbyClause() |
| 1156 | | clause = GroupClause() |
| 1157 | | clause = LimitClause() |
| 1158 | | clause = DistinctClause() |
| 1159 | | clause = MetaVariableClause() |
| 1160 | | clause = JoinClause() |
| 1161 | ) |
| 1162 | { |
| 1163 | return clause; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | Clause MetaVariableClause() throws ParseException : |
| 1168 | { |
| 1169 | MetaVariableClause mc = new MetaVariableClause(); |
| 1170 | VarIdentifier var = new VarIdentifier(); |
| 1171 | Token t; |
| 1172 | } |
| 1173 | { |
| 1174 | <METAVARIABLECLAUSE> |
| 1175 | { |
| 1176 | t = getToken(0); |
| 1177 | mc.setVar(var); |
| 1178 | var.setValue(t.toString()); |
| 1179 | return mc; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | Clause JoinClause() throws ParseException : |
| 1184 | { |
| 1185 | Expression whereExpr; |
| 1186 | List<Clause> leftClauses, rightClauses; |
| 1187 | JoinClause.JoinKind kind = JoinClause.JoinKind.INNER; |
| 1188 | } |
| 1189 | { |
| 1190 | ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } ) |
| 1191 | <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> "," |
| 1192 | <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> "," |
| 1193 | whereExpr = Expression() <RIGHTPAREN> |
| 1194 | { |
| 1195 | JoinClause jc = new JoinClause(kind); |
| 1196 | jc.setLeftClauses(leftClauses); |
| 1197 | jc.setRightClauses(rightClauses); |
| 1198 | jc.setWhereExpr(whereExpr); |
| 1199 | return jc; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | Clause ForClause()throws ParseException : |
| 1204 | { |
| 1205 | ForClause fc = new ForClause(); |
| 1206 | VariableExpr varExp; |
| 1207 | VariableExpr varPos = null; |
| 1208 | Expression inExp; |
| 1209 | extendCurrentScope(); |
| 1210 | } |
| 1211 | { |
| 1212 | "for" varExp = Variable() |
| 1213 | { |
| 1214 | getCurrentScope().addNewVarSymbolToScope(varExp.getVar()); |
| 1215 | } |
| 1216 | ("at" varPos = Variable() |
| 1217 | { |
| 1218 | getCurrentScope().addNewVarSymbolToScope(varPos.getVar()); |
| 1219 | } |
| 1220 | )? |
| 1221 | "in" ( inExp = Expression() ) |
| 1222 | { |
| 1223 | fc.setVarExpr(varExp); |
| 1224 | fc.setInExpr(inExp); |
| 1225 | if (varPos != null) { |
| 1226 | fc.setPosExpr(varPos); |
| 1227 | } |
| 1228 | return fc; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | Clause LetClause() throws ParseException: |
| 1233 | { |
| 1234 | LetClause lc = new LetClause(); |
| 1235 | VariableExpr varExp; |
| 1236 | Expression beExp; |
| 1237 | extendCurrentScope(); |
| 1238 | } |
| 1239 | { |
ilovesoup | b2527c1 | 2012-07-12 03:21:13 +0000 | [diff] [blame^] | 1240 | "let" varExp = Variable() ":=" beExp = Expression() |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 1241 | { |
| 1242 | getCurrentScope().addNewVarSymbolToScope(varExp.getVar()); |
vinayakb | 38b7ca4 | 2012-03-05 05:44:15 +0000 | [diff] [blame] | 1243 | lc.setVarExpr(varExp); |
| 1244 | lc.setBeExpr(beExp); |
| 1245 | return lc; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | Clause WhereClause()throws ParseException : |
| 1250 | { |
| 1251 | WhereClause wc = new WhereClause(); |
| 1252 | Expression whereExpr; |
| 1253 | } |
| 1254 | { |
| 1255 | "where" whereExpr = Expression() |
| 1256 | { |
| 1257 | wc.setWhereExpr(whereExpr); |
| 1258 | return wc; |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | Clause OrderbyClause()throws ParseException : |
| 1263 | { |
| 1264 | OrderbyClause oc = new OrderbyClause(); |
| 1265 | Expression orderbyExpr; |
| 1266 | List<Expression> orderbyList = new ArrayList<Expression>(); |
| 1267 | List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >(); |
| 1268 | int numOfOrderby = 0; |
| 1269 | } |
| 1270 | { |
| 1271 | ( |
| 1272 | "order" |
| 1273 | { |
| 1274 | String hint = getHint(token); |
| 1275 | if (hint != null && hint.startsWith(INMEMORY_HINT)) { |
| 1276 | String splits[] = hint.split(" +"); |
| 1277 | int numFrames = Integer.parseInt(splits[1]); |
| 1278 | int numTuples = Integer.parseInt(splits[2]); |
| 1279 | oc.setNumFrames(numFrames); |
| 1280 | oc.setNumTuples(numTuples); |
| 1281 | } |
| 1282 | } |
| 1283 | "by" orderbyExpr = Expression() |
| 1284 | { |
| 1285 | orderbyList.add(orderbyExpr); |
| 1286 | OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC; |
| 1287 | } |
| 1288 | ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; }) |
| 1289 | | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))? |
| 1290 | { |
| 1291 | modifierList.add(modif); |
| 1292 | } |
| 1293 | |
| 1294 | ("," orderbyExpr = Expression() |
| 1295 | { |
| 1296 | orderbyList.add(orderbyExpr); |
| 1297 | modif = OrderbyClause.OrderModifier.ASC; |
| 1298 | } |
| 1299 | ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; }) |
| 1300 | | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))? |
| 1301 | { |
| 1302 | modifierList.add(modif); |
| 1303 | } |
| 1304 | )* |
| 1305 | ) |
| 1306 | { |
| 1307 | oc.setModifierList(modifierList); |
| 1308 | oc.setOrderbyList(orderbyList); |
| 1309 | return oc; |
| 1310 | } |
| 1311 | } |
| 1312 | Clause GroupClause()throws ParseException : |
| 1313 | { |
| 1314 | GroupbyClause gbc = new GroupbyClause(); |
| 1315 | // GbyVariableExpressionPair pair = new GbyVariableExpressionPair(); |
| 1316 | List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>(); |
| 1317 | List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>(); |
| 1318 | List<VariableExpr> withVarList= new ArrayList<VariableExpr>(); |
| 1319 | VariableExpr var = null; |
| 1320 | VariableExpr withVar = null; |
| 1321 | Expression expr = null; |
| 1322 | VariableExpr decorVar = null; |
| 1323 | Expression decorExpr = null; |
| 1324 | } |
| 1325 | { |
| 1326 | { |
| 1327 | Scope newScope = extendCurrentScopeNoPush(true); |
| 1328 | // extendCurrentScope(true); |
| 1329 | } |
| 1330 | "group" |
| 1331 | { |
| 1332 | String hint = getHint(token); |
| 1333 | if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) { |
| 1334 | gbc.setHashGroupByHint(true); |
| 1335 | } |
| 1336 | } |
| 1337 | "by" (LOOKAHEAD(2) var = Variable() |
| 1338 | { |
| 1339 | newScope.addNewVarSymbolToScope(var.getVar()); |
| 1340 | } ":=")? |
| 1341 | expr = Expression() |
| 1342 | { |
| 1343 | GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr); |
| 1344 | vePairList.add(pair1); |
| 1345 | } |
| 1346 | ("," ( LOOKAHEAD(2) var = Variable() |
| 1347 | { |
| 1348 | newScope.addNewVarSymbolToScope(var.getVar()); |
| 1349 | } ":=")? |
| 1350 | expr = Expression() |
| 1351 | { |
| 1352 | GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr); |
| 1353 | vePairList.add(pair2); |
| 1354 | } |
| 1355 | )* |
| 1356 | ("decor" decorVar = Variable() ":=" decorExpr = Expression() |
| 1357 | { |
| 1358 | newScope.addNewVarSymbolToScope(decorVar.getVar()); |
| 1359 | GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr); |
| 1360 | decorPairList.add(pair3); |
| 1361 | } |
| 1362 | ("," "decor" decorVar = Variable() ":=" decorExpr = Expression() |
| 1363 | { |
| 1364 | newScope.addNewVarSymbolToScope(decorVar.getVar()); |
| 1365 | GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr); |
| 1366 | decorPairList.add(pair4); |
| 1367 | } |
| 1368 | )* |
| 1369 | )? |
| 1370 | "with" withVar = VariableRef() |
| 1371 | { |
| 1372 | if(withVar.getIsNewVar()==true) |
| 1373 | throw new ParseException("can't find variable " + withVar.getVar()); |
| 1374 | withVarList.add(withVar); |
| 1375 | newScope.addNewVarSymbolToScope(withVar.getVar()); |
| 1376 | } |
| 1377 | ("," withVar = VariableRef() |
| 1378 | { |
| 1379 | if(withVar.getIsNewVar()==true) |
| 1380 | throw new ParseException("can't find variable " + withVar.getVar()); |
| 1381 | withVarList.add(withVar); |
| 1382 | newScope.addNewVarSymbolToScope(withVar.getVar()); |
| 1383 | })* |
| 1384 | { |
| 1385 | gbc.setGbyPairList(vePairList); |
| 1386 | gbc.setDecorPairList(decorPairList); |
| 1387 | gbc.setWithVarList(withVarList); |
| 1388 | replaceCurrentScope(newScope); |
| 1389 | return gbc; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | LimitClause LimitClause() throws ParseException: |
| 1395 | { |
| 1396 | LimitClause lc = new LimitClause(); |
| 1397 | Expression expr; |
| 1398 | pushForbiddenScope(getCurrentScope()); |
| 1399 | } |
| 1400 | { |
| 1401 | "limit" expr = Expression() { lc.setLimitExpr(expr); } |
| 1402 | ("offset" expr = Expression() { lc.setOffset(expr); })? |
| 1403 | |
| 1404 | { |
| 1405 | popForbiddenScope(); |
| 1406 | return lc; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | DistinctClause DistinctClause() throws ParseException: |
| 1411 | { |
| 1412 | List<Expression> exprs = new ArrayList<Expression>(); |
| 1413 | Expression expr; |
| 1414 | } |
| 1415 | { |
| 1416 | "distinct" "by" expr = Expression() |
| 1417 | { |
| 1418 | exprs.add(expr); |
| 1419 | } |
| 1420 | ("," expr = Expression() |
| 1421 | { |
| 1422 | exprs.add(expr); |
| 1423 | } |
| 1424 | )* |
| 1425 | { |
| 1426 | return new DistinctClause(exprs); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | QuantifiedExpression QuantifiedExpression()throws ParseException: |
| 1432 | { |
| 1433 | QuantifiedExpression qc = new QuantifiedExpression(); |
| 1434 | List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>(); |
| 1435 | Expression satisfiesExpr; |
| 1436 | VariableExpr var; |
| 1437 | Expression inExpr; |
| 1438 | QuantifiedPair pair; |
| 1439 | } |
| 1440 | { |
| 1441 | { |
| 1442 | createNewScope(); |
| 1443 | } |
| 1444 | |
| 1445 | ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); }) |
| 1446 | | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); })) |
| 1447 | var = Variable() "in" inExpr = Expression() |
| 1448 | { |
| 1449 | pair = new QuantifiedPair(var, inExpr); |
| 1450 | getCurrentScope().addNewVarSymbolToScope(var.getVar()); |
| 1451 | quantifiedList.add(pair); |
| 1452 | } |
| 1453 | ( |
| 1454 | "," var = Variable() "in" inExpr = Expression() |
| 1455 | { |
| 1456 | pair = new QuantifiedPair(var, inExpr); |
| 1457 | getCurrentScope().addNewVarSymbolToScope(var.getVar()); |
| 1458 | quantifiedList.add(pair); |
| 1459 | } |
| 1460 | )* |
| 1461 | "satisfies" satisfiesExpr = Expression() |
| 1462 | { |
| 1463 | qc.setSatisfiesExpr(satisfiesExpr); |
| 1464 | qc.setQuantifiedList(quantifiedList); |
| 1465 | removeCurrentScope(); |
| 1466 | return qc; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | TOKEN_MGR_DECLS: |
| 1471 | { |
| 1472 | public int commentDepth = 0; |
| 1473 | } |
| 1474 | |
| 1475 | <DEFAULT> |
| 1476 | TOKEN : |
| 1477 | { |
| 1478 | <CARET : "^" > |
| 1479 | } |
| 1480 | |
| 1481 | <DEFAULT> |
| 1482 | TOKEN : |
| 1483 | { |
| 1484 | <DATASET : "dataset" > |
| 1485 | } |
| 1486 | |
| 1487 | <DEFAULT> |
| 1488 | TOKEN : |
| 1489 | { |
| 1490 | <LEFTPAREN : "(" > |
| 1491 | } |
| 1492 | |
| 1493 | <DEFAULT> |
| 1494 | TOKEN : |
| 1495 | { |
| 1496 | <RIGHTPAREN : ")" > |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | <DEFAULT> |
| 1501 | TOKEN : |
| 1502 | { |
| 1503 | <INTEGER_LITERAL : (<DIGIT>)+ > |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | <DEFAULT> |
| 1508 | TOKEN : |
| 1509 | { |
| 1510 | <NULL : "null"> |
| 1511 | } |
| 1512 | |
| 1513 | <DEFAULT> |
| 1514 | TOKEN : |
| 1515 | { |
| 1516 | <TRUE : "true"> |
| 1517 | } |
| 1518 | |
| 1519 | <DEFAULT> |
| 1520 | TOKEN : |
| 1521 | { |
| 1522 | <FALSE : "false"> |
| 1523 | } |
| 1524 | |
| 1525 | <DEFAULT> |
| 1526 | TOKEN : |
| 1527 | { |
| 1528 | <#DIGIT : ["0" - "9"]> |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | TOKEN: |
| 1533 | { |
| 1534 | < DOUBLE_LITERAL: <INTEGER> |
| 1535 | | <INTEGER> ( "." <INTEGER> )? |
| 1536 | | "." <INTEGER> |
| 1537 | > |
| 1538 | | |
| 1539 | < FLOAT_LITERAL: <INTEGER> ( "f" | "F" ) |
| 1540 | | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )? |
| 1541 | | "." <INTEGER> ( "f" | "F" ) |
| 1542 | > |
| 1543 | | |
| 1544 | <INTEGER : (<DIGIT>)+ > |
| 1545 | } |
| 1546 | |
| 1547 | <DEFAULT> |
| 1548 | TOKEN : |
| 1549 | { |
| 1550 | <#LETTER : ["A" - "Z", "a" - "z"]> |
| 1551 | } |
| 1552 | |
| 1553 | <DEFAULT> |
| 1554 | TOKEN : |
| 1555 | { |
| 1556 | <SPECIALCHARS : ["$", "_", "-"] > |
| 1557 | } |
| 1558 | |
| 1559 | <DEFAULT> |
| 1560 | TOKEN : |
| 1561 | { |
| 1562 | <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")> |
| 1563 | | |
| 1564 | < #EscapeQuot: "\\\"" > |
| 1565 | | |
| 1566 | < #EscapeApos: "\\\'" > |
| 1567 | } |
| 1568 | |
| 1569 | <DEFAULT> |
| 1570 | TOKEN : |
| 1571 | { |
| 1572 | <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*> |
| 1573 | } |
| 1574 | |
| 1575 | <DEFAULT> |
| 1576 | TOKEN : |
| 1577 | { |
| 1578 | <VARIABLE : "$" <IDENTIFIER> > |
| 1579 | } |
| 1580 | |
| 1581 | <DEFAULT> |
| 1582 | TOKEN : |
| 1583 | { |
| 1584 | <METAVARIABLECLAUSE : "#" <IDENTIFIER> > |
| 1585 | } |
| 1586 | |
| 1587 | <DEFAULT> |
| 1588 | TOKEN : |
| 1589 | { |
| 1590 | <METAVARIABLE : "$$" <IDENTIFIER> > |
| 1591 | } |
| 1592 | |
| 1593 | SKIP: |
| 1594 | { |
| 1595 | " " |
| 1596 | | "\t" |
| 1597 | | "\r" |
| 1598 | | "\n" |
| 1599 | } |
| 1600 | |
| 1601 | SKIP: |
| 1602 | { |
| 1603 | <"//" (~["\n"])* "\n"> |
| 1604 | } |
| 1605 | |
| 1606 | SKIP: |
| 1607 | { |
| 1608 | <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?> |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | SKIP: |
| 1613 | { |
| 1614 | <"/*"> {commentDepth=1;}: INSIDE_COMMENT |
| 1615 | } |
| 1616 | |
| 1617 | <INSIDE_COMMENT> |
| 1618 | SPECIAL_TOKEN: |
| 1619 | { |
| 1620 | <"+"(" ")*(~["/","*"])*> |
| 1621 | } |
| 1622 | |
| 1623 | <INSIDE_COMMENT> |
| 1624 | SKIP: |
| 1625 | { |
| 1626 | <"/*"> {commentDepth++;} |
| 1627 | } |
| 1628 | |
| 1629 | <INSIDE_COMMENT> |
| 1630 | SKIP: |
| 1631 | { |
| 1632 | <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);} |
| 1633 | | <~[]> |
| 1634 | } |