| 1 |
/* |
| 2 |
[The "BSD licence"] |
| 3 |
Copyright (c) 2013 Terence Parr, Sam Harwell |
| 4 |
All rights reserved. |
| 5 |
|
| 6 |
Redistribution and use in source and binary forms, with or without |
| 7 |
modification, are permitted provided that the following conditions |
| 8 |
are met: |
| 9 |
1. Redistributions of source code must retain the above copyright |
| 10 |
notice, this list of conditions and the following disclaimer. |
| 11 |
2. Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
3. The name of the author may not be used to endorse or promote products |
| 15 |
derived from this software without specific prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
parser grammar Java7Parser; |
| 30 |
|
| 31 |
options { tokenVocab=Java7Lexer; } |
| 32 |
|
| 33 |
// starting point for parsing a java file |
| 34 |
compilationUnit |
| 35 |
: packageDeclaration? importDeclaration* typeDeclaration* EOF |
| 36 |
; |
| 37 |
|
| 38 |
packageDeclaration |
| 39 |
: annotation* 'package' qualifiedName ';' |
| 40 |
; |
| 41 |
|
| 42 |
importDeclaration |
| 43 |
: 'import' 'static'? qualifiedName ('.' '*')? ';' |
| 44 |
; |
| 45 |
|
| 46 |
typeDeclaration |
| 47 |
: classOrInterfaceModifier* classDeclaration |
| 48 |
| classOrInterfaceModifier* enumDeclaration |
| 49 |
| classOrInterfaceModifier* interfaceDeclaration |
| 50 |
| classOrInterfaceModifier* annotationTypeDeclaration |
| 51 |
| ';' |
| 52 |
; |
| 53 |
|
| 54 |
modifier |
| 55 |
: classOrInterfaceModifier |
| 56 |
| ( 'native' |
| 57 |
| 'synchronized' |
| 58 |
| 'transient' |
| 59 |
| 'volatile' |
| 60 |
) |
| 61 |
; |
| 62 |
|
| 63 |
classOrInterfaceModifier |
| 64 |
: annotation // class or interface |
| 65 |
| ( 'public' // class or interface |
| 66 |
| 'protected' // class or interface |
| 67 |
| 'private' // class or interface |
| 68 |
| 'static' // class or interface |
| 69 |
| 'abstract' // class or interface |
| 70 |
| 'final' // class only -- does not apply to interfaces |
| 71 |
| 'strictfp' // class or interface |
| 72 |
) |
| 73 |
; |
| 74 |
|
| 75 |
variableModifier |
| 76 |
: 'final' |
| 77 |
| annotation |
| 78 |
; |
| 79 |
|
| 80 |
classDeclaration |
| 81 |
: 'class' Identifier typeParameters? |
| 82 |
('extends' type)? |
| 83 |
('implements' typeList)? |
| 84 |
classBody |
| 85 |
; |
| 86 |
|
| 87 |
typeParameters |
| 88 |
: '<' typeParameter (',' typeParameter)* '>' |
| 89 |
; |
| 90 |
|
| 91 |
typeParameter |
| 92 |
: Identifier ('extends' typeBound)? |
| 93 |
; |
| 94 |
|
| 95 |
typeBound |
| 96 |
: type ('&' type)* |
| 97 |
; |
| 98 |
|
| 99 |
enumDeclaration |
| 100 |
: ENUM Identifier ('implements' typeList)? |
| 101 |
'{' enumConstants? ','? enumBodyDeclarations? '}' |
| 102 |
; |
| 103 |
|
| 104 |
enumConstants |
| 105 |
: enumConstant (',' enumConstant)* |
| 106 |
; |
| 107 |
|
| 108 |
enumConstant |
| 109 |
: annotation* Identifier arguments? classBody? |
| 110 |
; |
| 111 |
|
| 112 |
enumBodyDeclarations |
| 113 |
: ';' classBodyDeclaration* |
| 114 |
; |
| 115 |
|
| 116 |
interfaceDeclaration |
| 117 |
: 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody |
| 118 |
; |
| 119 |
|
| 120 |
typeList |
| 121 |
: type (',' type)* |
| 122 |
; |
| 123 |
|
| 124 |
classBody |
| 125 |
: '{' classBodyDeclaration* '}' |
| 126 |
; |
| 127 |
|
| 128 |
interfaceBody |
| 129 |
: '{' interfaceBodyDeclaration* '}' |
| 130 |
; |
| 131 |
|
| 132 |
classBodyDeclaration |
| 133 |
: ';' |
| 134 |
| 'static'? block |
| 135 |
| modifier* memberDeclaration |
| 136 |
; |
| 137 |
|
| 138 |
memberDeclaration |
| 139 |
: methodDeclaration |
| 140 |
| genericMethodDeclaration |
| 141 |
| fieldDeclaration |
| 142 |
| constructorDeclaration |
| 143 |
| genericConstructorDeclaration |
| 144 |
| interfaceDeclaration |
| 145 |
| annotationTypeDeclaration |
| 146 |
| classDeclaration |
| 147 |
| enumDeclaration |
| 148 |
; |
| 149 |
|
| 150 |
/* We use rule this even for void methods which cannot have [] after parameters. |
| 151 |
This simplifies grammar and we can consider void to be a type, which |
| 152 |
renders the [] matching as a context-sensitive issue or a semantic check |
| 153 |
for invalid return type after parsing. |
| 154 |
*/ |
| 155 |
methodDeclaration |
| 156 |
: (type|'void') Identifier formalParameters ('[' ']')* |
| 157 |
('throws' qualifiedNameList)? |
| 158 |
( methodBody |
| 159 |
| ';' |
| 160 |
) |
| 161 |
; |
| 162 |
|
| 163 |
genericMethodDeclaration |
| 164 |
: typeParameters methodDeclaration |
| 165 |
; |
| 166 |
|
| 167 |
constructorDeclaration |
| 168 |
: Identifier formalParameters ('throws' qualifiedNameList)? |
| 169 |
constructorBody |
| 170 |
; |
| 171 |
|
| 172 |
genericConstructorDeclaration |
| 173 |
: typeParameters constructorDeclaration |
| 174 |
; |
| 175 |
|
| 176 |
fieldDeclaration |
| 177 |
: type variableDeclarators ';' |
| 178 |
; |
| 179 |
|
| 180 |
interfaceBodyDeclaration |
| 181 |
: modifier* interfaceMemberDeclaration |
| 182 |
| ';' |
| 183 |
; |
| 184 |
|
| 185 |
interfaceMemberDeclaration |
| 186 |
: constDeclaration |
| 187 |
| interfaceMethodDeclaration |
| 188 |
| genericInterfaceMethodDeclaration |
| 189 |
| interfaceDeclaration |
| 190 |
| annotationTypeDeclaration |
| 191 |
| classDeclaration |
| 192 |
| enumDeclaration |
| 193 |
; |
| 194 |
|
| 195 |
constDeclaration |
| 196 |
: type constantDeclarator (',' constantDeclarator)* ';' |
| 197 |
; |
| 198 |
|
| 199 |
constantDeclarator |
| 200 |
: Identifier ('[' ']')* '=' variableInitializer |
| 201 |
; |
| 202 |
|
| 203 |
// see matching of [] comment in methodDeclaratorRest |
| 204 |
interfaceMethodDeclaration |
| 205 |
: (type|'void') Identifier formalParameters ('[' ']')* |
| 206 |
('throws' qualifiedNameList)? |
| 207 |
';' |
| 208 |
; |
| 209 |
|
| 210 |
genericInterfaceMethodDeclaration |
| 211 |
: typeParameters interfaceMethodDeclaration |
| 212 |
; |
| 213 |
|
| 214 |
variableDeclarators |
| 215 |
: variableDeclarator (',' variableDeclarator)* |
| 216 |
; |
| 217 |
|
| 218 |
variableDeclarator |
| 219 |
: variableDeclaratorId ('=' variableInitializer)? |
| 220 |
; |
| 221 |
|
| 222 |
variableDeclaratorId |
| 223 |
: Identifier ('[' ']')* |
| 224 |
; |
| 225 |
|
| 226 |
variableInitializer |
| 227 |
: arrayInitializer |
| 228 |
| expression |
| 229 |
; |
| 230 |
|
| 231 |
arrayInitializer |
| 232 |
: '{' (variableInitializer (',' variableInitializer)* (',')? )? '}' |
| 233 |
; |
| 234 |
|
| 235 |
enumConstantName |
| 236 |
: Identifier |
| 237 |
; |
| 238 |
|
| 239 |
type |
| 240 |
: classOrInterfaceType ('[' ']')* |
| 241 |
| primitiveType ('[' ']')* |
| 242 |
; |
| 243 |
|
| 244 |
classOrInterfaceType |
| 245 |
: identifierTypePair ('.' identifierTypePair )* |
| 246 |
; |
| 247 |
|
| 248 |
identifierTypePair |
| 249 |
: Identifier typeArguments? |
| 250 |
; |
| 251 |
|
| 252 |
primitiveType |
| 253 |
: 'boolean' |
| 254 |
| 'char' |
| 255 |
| 'byte' |
| 256 |
| 'short' |
| 257 |
| 'int' |
| 258 |
| 'long' |
| 259 |
| 'float' |
| 260 |
| 'double' |
| 261 |
; |
| 262 |
|
| 263 |
typeArguments |
| 264 |
: '<' typeArgument (',' typeArgument)* '>' |
| 265 |
; |
| 266 |
|
| 267 |
typeArgument |
| 268 |
: type |
| 269 |
| '?' (('extends' | 'super') type)? |
| 270 |
; |
| 271 |
|
| 272 |
qualifiedNameList |
| 273 |
: qualifiedName (',' qualifiedName)* |
| 274 |
; |
| 275 |
|
| 276 |
formalParameters |
| 277 |
: '(' formalParameterList? ')' |
| 278 |
; |
| 279 |
|
| 280 |
formalParameterList |
| 281 |
: formalParameter (',' formalParameter)* (',' lastFormalParameter)? |
| 282 |
| lastFormalParameter |
| 283 |
; |
| 284 |
|
| 285 |
formalParameter |
| 286 |
: variableModifier* type variableDeclaratorId |
| 287 |
; |
| 288 |
|
| 289 |
lastFormalParameter |
| 290 |
: variableModifier* type '...' variableDeclaratorId |
| 291 |
; |
| 292 |
|
| 293 |
methodBody |
| 294 |
: block |
| 295 |
; |
| 296 |
|
| 297 |
constructorBody |
| 298 |
: block |
| 299 |
; |
| 300 |
|
| 301 |
qualifiedName |
| 302 |
: Identifier ('.' Identifier)* |
| 303 |
; |
| 304 |
|
| 305 |
literal |
| 306 |
: IntegerLiteral |
| 307 |
| FloatingPointLiteral |
| 308 |
| CharacterLiteral |
| 309 |
| StringLiteral |
| 310 |
| BooleanLiteral |
| 311 |
| 'null' |
| 312 |
; |
| 313 |
|
| 314 |
// ANNOTATIONS |
| 315 |
|
| 316 |
annotation |
| 317 |
: '@' annotationName ( '(' ( elementValuePairs | elementValue )? ')' )? |
| 318 |
; |
| 319 |
|
| 320 |
annotationName : qualifiedName ; |
| 321 |
|
| 322 |
elementValuePairs |
| 323 |
: elementValuePair (',' elementValuePair)* |
| 324 |
; |
| 325 |
|
| 326 |
elementValuePair |
| 327 |
: Identifier '=' elementValue |
| 328 |
; |
| 329 |
|
| 330 |
elementValue |
| 331 |
: expression |
| 332 |
| annotation |
| 333 |
| elementValueArrayInitializer |
| 334 |
; |
| 335 |
|
| 336 |
elementValueArrayInitializer |
| 337 |
: '{' (elementValue (',' elementValue)*)? (',')? '}' |
| 338 |
; |
| 339 |
|
| 340 |
annotationTypeDeclaration |
| 341 |
: '@' 'interface' Identifier annotationTypeBody |
| 342 |
; |
| 343 |
|
| 344 |
annotationTypeBody |
| 345 |
: '{' (annotationTypeElementDeclaration)* '}' |
| 346 |
; |
| 347 |
|
| 348 |
annotationTypeElementDeclaration |
| 349 |
: modifier* annotationTypeElementRest |
| 350 |
| ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler |
| 351 |
; |
| 352 |
|
| 353 |
annotationTypeElementRest |
| 354 |
: type annotationMethodOrConstantRest ';' |
| 355 |
| classDeclaration ';'? |
| 356 |
| interfaceDeclaration ';'? |
| 357 |
| enumDeclaration ';'? |
| 358 |
| annotationTypeDeclaration ';'? |
| 359 |
; |
| 360 |
|
| 361 |
annotationMethodOrConstantRest |
| 362 |
: annotationMethodRest |
| 363 |
| annotationConstantRest |
| 364 |
; |
| 365 |
|
| 366 |
annotationMethodRest |
| 367 |
: Identifier '(' ')' defaultValue? |
| 368 |
; |
| 369 |
|
| 370 |
annotationConstantRest |
| 371 |
: variableDeclarators |
| 372 |
; |
| 373 |
|
| 374 |
defaultValue |
| 375 |
: 'default' elementValue |
| 376 |
; |
| 377 |
|
| 378 |
// STATEMENTS / BLOCKS |
| 379 |
|
| 380 |
block |
| 381 |
: '{' blockStatement* '}' |
| 382 |
; |
| 383 |
|
| 384 |
blockStatement |
| 385 |
: localVariableDeclarationStatement |
| 386 |
| statement |
| 387 |
| typeDeclaration |
| 388 |
; |
| 389 |
|
| 390 |
localVariableDeclarationStatement |
| 391 |
: localVariableDeclaration ';' |
| 392 |
; |
| 393 |
|
| 394 |
localVariableDeclaration |
| 395 |
: variableModifier* type variableDeclarators |
| 396 |
; |
| 397 |
|
| 398 |
statement |
| 399 |
: block |
| 400 |
| ASSERT expression (':' expression)? ';' |
| 401 |
| 'if' parExpression statement ('else' statement)? |
| 402 |
| 'for' '(' forControl ')' statement |
| 403 |
| 'while' parExpression statement |
| 404 |
| 'do' statement 'while' parExpression ';' |
| 405 |
| 'try' block (catchClause+ finallyBlock? | finallyBlock) |
| 406 |
| 'try' resourceSpecification block catchClause* finallyBlock? |
| 407 |
| 'switch' parExpression '{' switchBlockStatementGroup* switchLabel* '}' |
| 408 |
| 'synchronized' parExpression block |
| 409 |
| 'return' expression? ';' |
| 410 |
| 'throw' expression ';' |
| 411 |
| 'break' Identifier? ';' |
| 412 |
| 'continue' Identifier? ';' |
| 413 |
| ';' |
| 414 |
| statementExpression ';' |
| 415 |
| Identifier ':' statement |
| 416 |
; |
| 417 |
|
| 418 |
catchClause |
| 419 |
: 'catch' '(' variableModifier* catchType Identifier ')' block |
| 420 |
; |
| 421 |
|
| 422 |
catchType |
| 423 |
: qualifiedName ('|' qualifiedName)* |
| 424 |
; |
| 425 |
|
| 426 |
finallyBlock |
| 427 |
: 'finally' block |
| 428 |
; |
| 429 |
|
| 430 |
resourceSpecification |
| 431 |
: '(' resources ';'? ')' |
| 432 |
; |
| 433 |
|
| 434 |
resources |
| 435 |
: resource (';' resource)* |
| 436 |
; |
| 437 |
|
| 438 |
resource |
| 439 |
: variableModifier* classOrInterfaceType variableDeclaratorId '=' expression |
| 440 |
; |
| 441 |
|
| 442 |
/** Matches cases then statements, both of which are mandatory. |
| 443 |
* To handle empty cases at the end, we add switchLabel* to statement. |
| 444 |
*/ |
| 445 |
switchBlockStatementGroup |
| 446 |
: switchLabel+ blockStatement+ |
| 447 |
; |
| 448 |
|
| 449 |
switchLabel |
| 450 |
: 'case' constantExpression ':' |
| 451 |
| 'case' enumConstantName ':' |
| 452 |
| 'default' ':' |
| 453 |
; |
| 454 |
|
| 455 |
forControl |
| 456 |
: enhancedForControl |
| 457 |
| forInit? ';' expression? ';' forUpdate? |
| 458 |
; |
| 459 |
|
| 460 |
forInit |
| 461 |
: localVariableDeclaration |
| 462 |
| expressionList |
| 463 |
; |
| 464 |
|
| 465 |
enhancedForControl |
| 466 |
: variableModifier* type variableDeclaratorId ':' expression |
| 467 |
; |
| 468 |
|
| 469 |
forUpdate |
| 470 |
: expressionList |
| 471 |
; |
| 472 |
|
| 473 |
// EXPRESSIONS |
| 474 |
|
| 475 |
parExpression |
| 476 |
: '(' expression ')' |
| 477 |
; |
| 478 |
|
| 479 |
expressionList |
| 480 |
: expression (',' expression)* |
| 481 |
; |
| 482 |
|
| 483 |
statementExpression |
| 484 |
: expression |
| 485 |
; |
| 486 |
|
| 487 |
constantExpression |
| 488 |
: expression |
| 489 |
; |
| 490 |
|
| 491 |
expression |
| 492 |
: primary |
| 493 |
| expression '.' Identifier |
| 494 |
| expression '.' 'this' |
| 495 |
| expression '.' 'new' nonWildcardTypeArguments? innerCreator |
| 496 |
| expression '.' 'super' superSuffix |
| 497 |
| expression '.' explicitGenericInvocation |
| 498 |
| expression '[' expression ']' |
| 499 |
| expression '(' expressionList? ')' |
| 500 |
| 'new' creator |
| 501 |
| '(' type ')' expression |
| 502 |
| expression ('++' | '--') |
| 503 |
| ('+'|'-'|'++'|'--') expression |
| 504 |
| ('~'|'!') expression |
| 505 |
| expression ('*'|'/'|'%') expression |
| 506 |
| expression ('+'|'-') expression |
| 507 |
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression |
| 508 |
| expression ('<=' | '>=' | '>' | '<') expression |
| 509 |
| expression 'instanceof' type |
| 510 |
| expression ('==' | '!=') expression |
| 511 |
| expression '&' expression |
| 512 |
| expression '^' expression |
| 513 |
| expression '|' expression |
| 514 |
| expression '&&' expression |
| 515 |
| expression '||' expression |
| 516 |
| expression '?' expression ':' expression |
| 517 |
| <assoc=right> expression |
| 518 |
( '=' |
| 519 |
| '+=' |
| 520 |
| '-=' |
| 521 |
| '*=' |
| 522 |
| '/=' |
| 523 |
| '&=' |
| 524 |
| '|=' |
| 525 |
| '^=' |
| 526 |
| '>>=' |
| 527 |
| '>>>=' |
| 528 |
| '<<=' |
| 529 |
| '%=' |
| 530 |
) |
| 531 |
expression |
| 532 |
; |
| 533 |
|
| 534 |
primary |
| 535 |
: '(' expression ')' |
| 536 |
| 'this' |
| 537 |
| 'super' |
| 538 |
| literal |
| 539 |
| Identifier |
| 540 |
| type '.' 'class' |
| 541 |
| 'void' '.' 'class' |
| 542 |
| nonWildcardTypeArguments (explicitGenericInvocationSuffix | 'this' arguments) |
| 543 |
; |
| 544 |
|
| 545 |
creator |
| 546 |
: nonWildcardTypeArguments createdName classCreatorRest |
| 547 |
| createdName (arrayCreatorRest | classCreatorRest) |
| 548 |
; |
| 549 |
|
| 550 |
createdName |
| 551 |
: identifierTypeOrDiamondPair ('.' identifierTypeOrDiamondPair)* |
| 552 |
| primitiveType |
| 553 |
; |
| 554 |
|
| 555 |
identifierTypeOrDiamondPair |
| 556 |
: Identifier typeArgumentsOrDiamond? |
| 557 |
; |
| 558 |
|
| 559 |
innerCreator |
| 560 |
: Identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest |
| 561 |
; |
| 562 |
|
| 563 |
arrayCreatorRest |
| 564 |
: '[' |
| 565 |
( ']' ('[' ']')* arrayInitializer |
| 566 |
| expression ']' ('[' expression ']')* ('[' ']')* |
| 567 |
) |
| 568 |
; |
| 569 |
|
| 570 |
classCreatorRest |
| 571 |
: arguments classBody? |
| 572 |
; |
| 573 |
|
| 574 |
explicitGenericInvocation |
| 575 |
: nonWildcardTypeArguments explicitGenericInvocationSuffix |
| 576 |
; |
| 577 |
|
| 578 |
nonWildcardTypeArguments |
| 579 |
: '<' typeList '>' |
| 580 |
; |
| 581 |
|
| 582 |
typeArgumentsOrDiamond |
| 583 |
: '<' '>' |
| 584 |
| typeArguments |
| 585 |
; |
| 586 |
|
| 587 |
nonWildcardTypeArgumentsOrDiamond |
| 588 |
: '<' '>' |
| 589 |
| nonWildcardTypeArguments |
| 590 |
; |
| 591 |
|
| 592 |
superSuffix |
| 593 |
: arguments |
| 594 |
| '.' Identifier arguments? |
| 595 |
; |
| 596 |
|
| 597 |
explicitGenericInvocationSuffix |
| 598 |
: 'super' superSuffix |
| 599 |
| Identifier arguments |
| 600 |
; |
| 601 |
|
| 602 |
arguments |
| 603 |
: '(' expressionList? ')' |
| 604 |
; |