gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/gdxstudio/parser/Java.g4
a62d533 1
/*
a62d533 2
 [The "BSD licence"]
a62d533 3
 Copyright (c) 2013 Terence Parr, Sam Harwell
a62d533 4
 All rights reserved.
a62d533 5
a62d533 6
 Redistribution and use in source and binary forms, with or without
a62d533 7
 modification, are permitted provided that the following conditions
a62d533 8
 are met:
a62d533 9
 1. Redistributions of source code must retain the above copyright
a62d533 10
    notice, this list of conditions and the following disclaimer.
a62d533 11
 2. Redistributions in binary form must reproduce the above copyright
a62d533 12
    notice, this list of conditions and the following disclaimer in the
a62d533 13
    documentation and/or other materials provided with the distribution.
a62d533 14
 3. The name of the author may not be used to endorse or promote products
a62d533 15
    derived from this software without specific prior written permission.
a62d533 16
a62d533 17
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
a62d533 18
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a62d533 19
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
a62d533 20
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
a62d533 21
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
a62d533 22
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
a62d533 23
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
a62d533 24
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
a62d533 25
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
a62d533 26
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a62d533 27
*/
a62d533 28
a62d533 29
/** A Java 1.7 grammar for ANTLR v4 derived from ANTLR v3 Java grammar.
a62d533 30
 *  Uses ANTLR v4's left-recursive expression notation.
a62d533 31
 *  It parses ECJ, Netbeans, JDK etc...
a62d533 32
 *
a62d533 33
 *  Sam Harwell cleaned this up significantly and updated to 1.7!
a62d533 34
 *
a62d533 35
 *  You can test with
a62d533 36
 *
a62d533 37
 *  $ antlr4 Java.g4
a62d533 38
 *  $ javac *.java
a62d533 39
 *  $ grun Java compilationUnit *.java
a62d533 40
 */
a62d533 41
grammar Java;
a62d533 42
a62d533 43
// starting point for parsing a java file
a62d533 44
compilationUnit
a62d533 45
    :   packageDeclaration? importDeclaration* typeDeclaration* EOF
a62d533 46
    ;
a62d533 47
a62d533 48
packageDeclaration
a62d533 49
    :   annotation* 'package' qualifiedName ';'
a62d533 50
    ;
a62d533 51
a62d533 52
importDeclaration
a62d533 53
    :   'import' 'static'? qualifiedName ('.' '*')? ';'
a62d533 54
    ;
a62d533 55
a62d533 56
typeDeclaration
a62d533 57
    :   classOrInterfaceModifier* classDeclaration
a62d533 58
    |   classOrInterfaceModifier* enumDeclaration
a62d533 59
    |   classOrInterfaceModifier* interfaceDeclaration
a62d533 60
    |   classOrInterfaceModifier* annotationTypeDeclaration
a62d533 61
    |   ';'
a62d533 62
    ;
a62d533 63
a62d533 64
modifier
a62d533 65
    :   classOrInterfaceModifier
a62d533 66
    |   (   'native'
a62d533 67
        |   'synchronized'
a62d533 68
        |   'transient'
a62d533 69
        |   'volatile'
a62d533 70
        )
a62d533 71
    ;
a62d533 72
a62d533 73
classOrInterfaceModifier
a62d533 74
    :   annotation       // class or interface
a62d533 75
    |   (   'public'     // class or interface
a62d533 76
        |   'protected'  // class or interface
a62d533 77
        |   'private'    // class or interface
a62d533 78
        |   'static'     // class or interface
a62d533 79
        |   'abstract'   // class or interface
a62d533 80
        |   'final'      // class only -- does not apply to interfaces
a62d533 81
        |   'strictfp'   // class or interface
a62d533 82
        )
a62d533 83
    ;
a62d533 84
a62d533 85
variableModifier
a62d533 86
    :   'final'
a62d533 87
    |   annotation
a62d533 88
    ;
a62d533 89
a62d533 90
classDeclaration
a62d533 91
    :   'class' Identifier typeParameters?
a62d533 92
        ('extends' type)?
a62d533 93
        ('implements' typeList)?
a62d533 94
        classBody
a62d533 95
    ;
a62d533 96
a62d533 97
typeParameters
a62d533 98
    :   '<' typeParameter (',' typeParameter)* '>'
a62d533 99
    ;
a62d533 100
a62d533 101
typeParameter
a62d533 102
    :   Identifier ('extends' typeBound)?
a62d533 103
    ;
a62d533 104
a62d533 105
typeBound
a62d533 106
    :   type ('&' type)*
a62d533 107
    ;
a62d533 108
a62d533 109
enumDeclaration
a62d533 110
    :   ENUM Identifier ('implements' typeList)?
a62d533 111
        '{' enumConstants? ','? enumBodyDeclarations? '}'
a62d533 112
    ;
a62d533 113
a62d533 114
enumConstants
a62d533 115
    :   enumConstant (',' enumConstant)*
a62d533 116
    ;
a62d533 117
a62d533 118
enumConstant
a62d533 119
    :   annotation* Identifier arguments? classBody?
a62d533 120
    ;
a62d533 121
a62d533 122
enumBodyDeclarations
a62d533 123
    :   ';' classBodyDeclaration*
a62d533 124
    ;
a62d533 125
a62d533 126
interfaceDeclaration
a62d533 127
    :   'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody
a62d533 128
    ;
a62d533 129
a62d533 130
typeList
a62d533 131
    :   type (',' type)*
a62d533 132
    ;
a62d533 133
a62d533 134
classBody
a62d533 135
    :   '{' classBodyDeclaration* '}'
a62d533 136
    ;
a62d533 137
a62d533 138
interfaceBody
a62d533 139
    :   '{' interfaceBodyDeclaration* '}'
a62d533 140
    ;
a62d533 141
a62d533 142
classBodyDeclaration
a62d533 143
    :   ';'
a62d533 144
    |   'static'? block
a62d533 145
    |   modifier* memberDeclaration
a62d533 146
    ;
a62d533 147
a62d533 148
memberDeclaration
a62d533 149
    :   methodDeclaration
a62d533 150
    |   genericMethodDeclaration
a62d533 151
    |   fieldDeclaration
a62d533 152
    |   constructorDeclaration
a62d533 153
    |   genericConstructorDeclaration
a62d533 154
    |   interfaceDeclaration
a62d533 155
    |   annotationTypeDeclaration
a62d533 156
    |   classDeclaration
a62d533 157
    |   enumDeclaration
a62d533 158
    ;
a62d533 159
a62d533 160
/* We use rule this even for void methods which cannot have [] after parameters.
a62d533 161
   This simplifies grammar and we can consider void to be a type, which
a62d533 162
   renders the [] matching as a context-sensitive issue or a semantic check
a62d533 163
   for invalid return type after parsing.
a62d533 164
 */
a62d533 165
methodDeclaration
a62d533 166
    :   (type|'void') Identifier formalParameters ('[' ']')*
a62d533 167
        ('throws' qualifiedNameList)?
a62d533 168
        (   methodBody
a62d533 169
        |   ';'
a62d533 170
        )
a62d533 171
    ;
a62d533 172
a62d533 173
genericMethodDeclaration
a62d533 174
    :   typeParameters methodDeclaration
a62d533 175
    ;
a62d533 176
a62d533 177
constructorDeclaration
a62d533 178
    :   Identifier formalParameters ('throws' qualifiedNameList)?
a62d533 179
        constructorBody
a62d533 180
    ;
a62d533 181
a62d533 182
genericConstructorDeclaration
a62d533 183
    :   typeParameters constructorDeclaration
a62d533 184
    ;
a62d533 185
a62d533 186
fieldDeclaration
a62d533 187
    :   type variableDeclarators ';'
a62d533 188
    ;
a62d533 189
a62d533 190
interfaceBodyDeclaration
a62d533 191
    :   modifier* interfaceMemberDeclaration
a62d533 192
    |   ';'
a62d533 193
    ;
a62d533 194
a62d533 195
interfaceMemberDeclaration
a62d533 196
    :   constDeclaration
a62d533 197
    |   interfaceMethodDeclaration
a62d533 198
    |   genericInterfaceMethodDeclaration
a62d533 199
    |   interfaceDeclaration
a62d533 200
    |   annotationTypeDeclaration
a62d533 201
    |   classDeclaration
a62d533 202
    |   enumDeclaration
a62d533 203
    ;
a62d533 204
a62d533 205
constDeclaration
a62d533 206
    :   type constantDeclarator (',' constantDeclarator)* ';'
a62d533 207
    ;
a62d533 208
a62d533 209
constantDeclarator
a62d533 210
    :   Identifier ('[' ']')* '=' variableInitializer
a62d533 211
    ;
a62d533 212
a62d533 213
// see matching of [] comment in methodDeclaratorRest
a62d533 214
interfaceMethodDeclaration
a62d533 215
    :   (type|'void') Identifier formalParameters ('[' ']')*
a62d533 216
        ('throws' qualifiedNameList)?
a62d533 217
        ';'
a62d533 218
    ;
a62d533 219
a62d533 220
genericInterfaceMethodDeclaration
a62d533 221
    :   typeParameters interfaceMethodDeclaration
a62d533 222
    ;
a62d533 223
a62d533 224
variableDeclarators
a62d533 225
    :   variableDeclarator (',' variableDeclarator)*
a62d533 226
    ;
a62d533 227
a62d533 228
variableDeclarator
a62d533 229
    :   variableDeclaratorId ('=' variableInitializer)?
a62d533 230
    ;
a62d533 231
a62d533 232
variableDeclaratorId
a62d533 233
    :   Identifier ('[' ']')*
a62d533 234
    ;
a62d533 235
a62d533 236
variableInitializer
a62d533 237
    :   arrayInitializer
a62d533 238
    |   expression
a62d533 239
    ;
a62d533 240
a62d533 241
arrayInitializer
a62d533 242
    :   '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
a62d533 243
    ;
a62d533 244
a62d533 245
enumConstantName
a62d533 246
    :   Identifier
a62d533 247
    ;
a62d533 248
a62d533 249
type
a62d533 250
    :   classOrInterfaceType ('[' ']')*
a62d533 251
    |   primitiveType ('[' ']')*
a62d533 252
    ;
a62d533 253
a62d533 254
classOrInterfaceType
a62d533 255
    :   Identifier typeArguments? ('.' Identifier typeArguments? )*
a62d533 256
    ;
a62d533 257
a62d533 258
primitiveType
a62d533 259
    :   'boolean'
a62d533 260
    |   'char'
a62d533 261
    |   'byte'
a62d533 262
    |   'short'
a62d533 263
    |   'int'
a62d533 264
    |   'long'
a62d533 265
    |   'float'
a62d533 266
    |   'double'
a62d533 267
    ;
a62d533 268
a62d533 269
typeArguments
a62d533 270
    :   '<' typeArgument (',' typeArgument)* '>'
a62d533 271
    ;
a62d533 272
a62d533 273
typeArgument
a62d533 274
    :   type
a62d533 275
    |   '?' (('extends' | 'super') type)?
a62d533 276
    ;
a62d533 277
a62d533 278
qualifiedNameList
a62d533 279
    :   qualifiedName (',' qualifiedName)*
a62d533 280
    ;
a62d533 281
a62d533 282
formalParameters
a62d533 283
    :   '(' formalParameterList? ')'
a62d533 284
    ;
a62d533 285
a62d533 286
formalParameterList
a62d533 287
    :   formalParameter (',' formalParameter)* (',' lastFormalParameter)?
a62d533 288
    |   lastFormalParameter
a62d533 289
    ;
a62d533 290
a62d533 291
formalParameter
a62d533 292
    :   variableModifier* type variableDeclaratorId
a62d533 293
    ;
a62d533 294
a62d533 295
lastFormalParameter
a62d533 296
    :   variableModifier* type '...' variableDeclaratorId
a62d533 297
    ;
a62d533 298
a62d533 299
methodBody
a62d533 300
    :   block
a62d533 301
    ;
a62d533 302
a62d533 303
constructorBody
a62d533 304
    :   block
a62d533 305
    ;
a62d533 306
a62d533 307
qualifiedName
a62d533 308
    :   Identifier ('.' Identifier)*
a62d533 309
    ;
a62d533 310
a62d533 311
literal
a62d533 312
    :   IntegerLiteral
a62d533 313
    |   FloatingPointLiteral
a62d533 314
    |   CharacterLiteral
a62d533 315
    |   StringLiteral
a62d533 316
    |   BooleanLiteral
a62d533 317
    |   'null'
a62d533 318
    ;
a62d533 319
a62d533 320
// ANNOTATIONS
a62d533 321
a62d533 322
annotation
a62d533 323
    :   '@' annotationName ( '(' ( elementValuePairs | elementValue )? ')' )?
a62d533 324
    ;
a62d533 325
a62d533 326
annotationName : qualifiedName ;
a62d533 327
a62d533 328
elementValuePairs
a62d533 329
    :   elementValuePair (',' elementValuePair)*
a62d533 330
    ;
a62d533 331
a62d533 332
elementValuePair
a62d533 333
    :   Identifier '=' elementValue
a62d533 334
    ;
a62d533 335
a62d533 336
elementValue
a62d533 337
    :   expression
a62d533 338
    |   annotation
a62d533 339
    |   elementValueArrayInitializer
a62d533 340
    ;
a62d533 341
a62d533 342
elementValueArrayInitializer
a62d533 343
    :   '{' (elementValue (',' elementValue)*)? (',')? '}'
a62d533 344
    ;
a62d533 345
a62d533 346
annotationTypeDeclaration
a62d533 347
    :   '@' 'interface' Identifier annotationTypeBody
a62d533 348
    ;
a62d533 349
a62d533 350
annotationTypeBody
a62d533 351
    :   '{' (annotationTypeElementDeclaration)* '}'
a62d533 352
    ;
a62d533 353
a62d533 354
annotationTypeElementDeclaration
a62d533 355
    :   modifier* annotationTypeElementRest
a62d533 356
    |   ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler
a62d533 357
    ;
a62d533 358
a62d533 359
annotationTypeElementRest
a62d533 360
    :   type annotationMethodOrConstantRest ';'
a62d533 361
    |   classDeclaration ';'?
a62d533 362
    |   interfaceDeclaration ';'?
a62d533 363
    |   enumDeclaration ';'?
a62d533 364
    |   annotationTypeDeclaration ';'?
a62d533 365
    ;
a62d533 366
a62d533 367
annotationMethodOrConstantRest
a62d533 368
    :   annotationMethodRest
a62d533 369
    |   annotationConstantRest
a62d533 370
    ;
a62d533 371
a62d533 372
annotationMethodRest
a62d533 373
    :   Identifier '(' ')' defaultValue?
a62d533 374
    ;
a62d533 375
a62d533 376
annotationConstantRest
a62d533 377
    :   variableDeclarators
a62d533 378
    ;
a62d533 379
a62d533 380
defaultValue
a62d533 381
    :   'default' elementValue
a62d533 382
    ;
a62d533 383
a62d533 384
// STATEMENTS / BLOCKS
a62d533 385
a62d533 386
block
a62d533 387
    :   '{' blockStatement* '}'
a62d533 388
    ;
a62d533 389
a62d533 390
blockStatement
a62d533 391
    :   localVariableDeclarationStatement
a62d533 392
    |   statement
a62d533 393
    |   typeDeclaration
a62d533 394
    ;
a62d533 395
a62d533 396
localVariableDeclarationStatement
a62d533 397
    :    localVariableDeclaration ';'
a62d533 398
    ;
a62d533 399
a62d533 400
localVariableDeclaration
a62d533 401
    :   variableModifier* type variableDeclarators
a62d533 402
    ;
a62d533 403
a62d533 404
statement
a62d533 405
    :   block
a62d533 406
    |   ASSERT expression (':' expression)? ';'
a62d533 407
    |   'if' parExpression statement ('else' statement)?
a62d533 408
    |   'for' '(' forControl ')' statement
a62d533 409
    |   'while' parExpression statement
a62d533 410
    |   'do' statement 'while' parExpression ';'
a62d533 411
    |   'try' block (catchClause+ finallyBlock? | finallyBlock)
a62d533 412
    |   'try' resourceSpecification block catchClause* finallyBlock?
a62d533 413
    |   'switch' parExpression '{' switchBlockStatementGroup* switchLabel* '}'
a62d533 414
    |   'synchronized' parExpression block
a62d533 415
    |   'return' expression? ';'
a62d533 416
    |   'throw' expression ';'
a62d533 417
    |   'break' Identifier? ';'
a62d533 418
    |   'continue' Identifier? ';'
a62d533 419
    |   ';'
a62d533 420
    |   statementExpression ';'
a62d533 421
    |   Identifier ':' statement
a62d533 422
    ;
a62d533 423
a62d533 424
catchClause
a62d533 425
    :   'catch' '(' variableModifier* catchType Identifier ')' block
a62d533 426
    ;
a62d533 427
a62d533 428
catchType
a62d533 429
    :   qualifiedName ('|' qualifiedName)*
a62d533 430
    ;
a62d533 431
a62d533 432
finallyBlock
a62d533 433
    :   'finally' block
a62d533 434
    ;
a62d533 435
a62d533 436
resourceSpecification
a62d533 437
    :   '(' resources ';'? ')'
a62d533 438
    ;
a62d533 439
a62d533 440
resources
a62d533 441
    :   resource (';' resource)*
a62d533 442
    ;
a62d533 443
a62d533 444
resource
a62d533 445
    :   variableModifier* classOrInterfaceType variableDeclaratorId '=' expression
a62d533 446
    ;
a62d533 447
a62d533 448
/** Matches cases then statements, both of which are mandatory.
a62d533 449
 *  To handle empty cases at the end, we add switchLabel* to statement.
a62d533 450
 */
a62d533 451
switchBlockStatementGroup
a62d533 452
    :   switchLabel+ blockStatement+
a62d533 453
    ;
a62d533 454
a62d533 455
switchLabel
a62d533 456
    :   'case' constantExpression ':'
a62d533 457
    |   'case' enumConstantName ':'
a62d533 458
    |   'default' ':'
a62d533 459
    ;
a62d533 460
a62d533 461
forControl
a62d533 462
    :   enhancedForControl
a62d533 463
    |   forInit? ';' expression? ';' forUpdate?
a62d533 464
    ;
a62d533 465
a62d533 466
forInit
a62d533 467
    :   localVariableDeclaration
a62d533 468
    |   expressionList
a62d533 469
    ;
a62d533 470
a62d533 471
enhancedForControl
a62d533 472
    :   variableModifier* type variableDeclaratorId ':' expression
a62d533 473
    ;
a62d533 474
a62d533 475
forUpdate
a62d533 476
    :   expressionList
a62d533 477
    ;
a62d533 478
a62d533 479
// EXPRESSIONS
a62d533 480
a62d533 481
parExpression
a62d533 482
    :   '(' expression ')'
a62d533 483
    ;
a62d533 484
a62d533 485
expressionList
a62d533 486
    :   expression (',' expression)*
a62d533 487
    ;
a62d533 488
a62d533 489
statementExpression
a62d533 490
    :   expression
a62d533 491
    ;
a62d533 492
a62d533 493
constantExpression
a62d533 494
    :   expression
a62d533 495
    ;
a62d533 496
a62d533 497
expression
a62d533 498
    :   primary
a62d533 499
    |   expression '.' Identifier
a62d533 500
    |   expression '.' 'this'
a62d533 501
    |   expression '.' 'new' nonWildcardTypeArguments? innerCreator
a62d533 502
    |   expression '.' 'super' superSuffix
a62d533 503
    |   expression '.' explicitGenericInvocation
a62d533 504
    |   expression '[' expression ']'
a62d533 505
    |   expression '(' expressionList? ')'
a62d533 506
    |   'new' creator
a62d533 507
    |   '(' type ')' expression
a62d533 508
    |   expression ('++' | '--')
a62d533 509
    |   ('+'|'-'|'++'|'--') expression
a62d533 510
    |   ('~'|'!') expression
a62d533 511
    |   expression ('*'|'/'|'%') expression
a62d533 512
    |   expression ('+'|'-') expression
a62d533 513
    |   expression ('<' '<' | '>' '>' '>' | '>' '>') expression
a62d533 514
    |   expression ('<=' | '>=' | '>' | '<') expression
a62d533 515
    |   expression 'instanceof' type
a62d533 516
    |   expression ('==' | '!=') expression
a62d533 517
    |   expression '&' expression
a62d533 518
    |   expression '^' expression
a62d533 519
    |   expression '|' expression
a62d533 520
    |   expression '&&' expression
a62d533 521
    |   expression '||' expression
a62d533 522
    |   expression '?' expression ':' expression
a62d533 523
    |   expression
a62d533 524
        (   '='<assoc=right>
a62d533 525
        |   '+='<assoc=right>
a62d533 526
        |   '-='<assoc=right>
a62d533 527
        |   '*='<assoc=right>
a62d533 528
        |   '/='<assoc=right>
a62d533 529
        |   '&='<assoc=right>
a62d533 530
        |   '|='<assoc=right>
a62d533 531
        |   '^='<assoc=right>
a62d533 532
        |   '>>='<assoc=right>
a62d533 533
        |   '>>>='<assoc=right>
a62d533 534
        |   '<<='<assoc=right>
a62d533 535
        |   '%='<assoc=right>
a62d533 536
        )
a62d533 537
        expression
a62d533 538
    ;
a62d533 539
a62d533 540
primary
a62d533 541
    :   '(' expression ')'
a62d533 542
    |   'this'
a62d533 543
    |   'super'
a62d533 544
    |   literal
a62d533 545
    |   Identifier
a62d533 546
    |   type '.' 'class'
a62d533 547
    |   'void' '.' 'class'
a62d533 548
    |   nonWildcardTypeArguments (explicitGenericInvocationSuffix | 'this' arguments)
a62d533 549
    ;
a62d533 550
a62d533 551
creator
a62d533 552
    :   nonWildcardTypeArguments createdName classCreatorRest
a62d533 553
    |   createdName (arrayCreatorRest | classCreatorRest)
a62d533 554
    ;
a62d533 555
a62d533 556
createdName
a62d533 557
    :   Identifier typeArgumentsOrDiamond? ('.' Identifier typeArgumentsOrDiamond?)*
a62d533 558
    |   primitiveType
a62d533 559
    ;
a62d533 560
a62d533 561
innerCreator
a62d533 562
    :   Identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest
a62d533 563
    ;
a62d533 564
a62d533 565
arrayCreatorRest
a62d533 566
    :   '['
a62d533 567
        (   ']' ('[' ']')* arrayInitializer
a62d533 568
        |   expression ']' ('[' expression ']')* ('[' ']')*
a62d533 569
        )
a62d533 570
    ;
a62d533 571
a62d533 572
classCreatorRest
a62d533 573
    :   arguments classBody?
a62d533 574
    ;
a62d533 575
a62d533 576
explicitGenericInvocation
a62d533 577
    :   nonWildcardTypeArguments explicitGenericInvocationSuffix
a62d533 578
    ;
a62d533 579
a62d533 580
nonWildcardTypeArguments
a62d533 581
    :   '<' typeList '>'
a62d533 582
    ;
a62d533 583
a62d533 584
typeArgumentsOrDiamond
a62d533 585
    :   '<' '>'
a62d533 586
    |   typeArguments
a62d533 587
    ;
a62d533 588
a62d533 589
nonWildcardTypeArgumentsOrDiamond
a62d533 590
    :   '<' '>'
a62d533 591
    |   nonWildcardTypeArguments
a62d533 592
    ;
a62d533 593
a62d533 594
superSuffix
a62d533 595
    :   arguments
a62d533 596
    |   '.' Identifier arguments?
a62d533 597
    ;
a62d533 598
a62d533 599
explicitGenericInvocationSuffix
a62d533 600
    :   'super' superSuffix
a62d533 601
    |   Identifier arguments
a62d533 602
    ;
a62d533 603
a62d533 604
arguments
a62d533 605
    :   '(' expressionList? ')'
a62d533 606
    ;
a62d533 607
a62d533 608
// LEXER
a62d533 609
a62d533 610
// §3.9 Keywords
a62d533 611
a62d533 612
ABSTRACT      : 'abstract';
a62d533 613
ASSERT        : 'assert';
a62d533 614
BOOLEAN       : 'boolean';
a62d533 615
BREAK         : 'break';
a62d533 616
BYTE          : 'byte';
a62d533 617
CASE          : 'case';
a62d533 618
CATCH         : 'catch';
a62d533 619
CHAR          : 'char';
a62d533 620
CLASS         : 'class';
a62d533 621
CONST         : 'const';
a62d533 622
CONTINUE      : 'continue';
a62d533 623
DEFAULT       : 'default';
a62d533 624
DO            : 'do';
a62d533 625
DOUBLE        : 'double';
a62d533 626
ELSE          : 'else';
a62d533 627
ENUM          : 'enum';
a62d533 628
EXTENDS       : 'extends';
a62d533 629
FINAL         : 'final';
a62d533 630
FINALLY       : 'finally';
a62d533 631
FLOAT         : 'float';
a62d533 632
FOR           : 'for';
a62d533 633
IF            : 'if';
a62d533 634
GOTO          : 'goto';
a62d533 635
IMPLEMENTS    : 'implements';
a62d533 636
IMPORT        : 'import';
a62d533 637
INSTANCEOF    : 'instanceof';
a62d533 638
INT           : 'int';
a62d533 639
INTERFACE     : 'interface';
a62d533 640
LONG          : 'long';
a62d533 641
NATIVE        : 'native';
a62d533 642
NEW           : 'new';
a62d533 643
PACKAGE       : 'package';
a62d533 644
PRIVATE       : 'private';
a62d533 645
PROTECTED     : 'protected';
a62d533 646
PUBLIC        : 'public';
a62d533 647
RETURN        : 'return';
a62d533 648
SHORT         : 'short';
a62d533 649
STATIC        : 'static';
a62d533 650
STRICTFP      : 'strictfp';
a62d533 651
SUPER         : 'super';
a62d533 652
SWITCH        : 'switch';
a62d533 653
SYNCHRONIZED  : 'synchronized';
a62d533 654
THIS          : 'this';
a62d533 655
THROW         : 'throw';
a62d533 656
THROWS        : 'throws';
a62d533 657
TRANSIENT     : 'transient';
a62d533 658
TRY           : 'try';
a62d533 659
VOID          : 'void';
a62d533 660
VOLATILE      : 'volatile';
a62d533 661
WHILE         : 'while';
a62d533 662
a62d533 663
// §3.10.1 Integer Literals
a62d533 664
a62d533 665
IntegerLiteral
a62d533 666
    :   DecimalIntegerLiteral
a62d533 667
    |   HexIntegerLiteral
a62d533 668
    |   OctalIntegerLiteral
a62d533 669
    |   BinaryIntegerLiteral
a62d533 670
    ;
a62d533 671
a62d533 672
fragment
a62d533 673
DecimalIntegerLiteral
a62d533 674
    :   DecimalNumeral IntegerTypeSuffix?
a62d533 675
    ;
a62d533 676
a62d533 677
fragment
a62d533 678
HexIntegerLiteral
a62d533 679
    :   HexNumeral IntegerTypeSuffix?
a62d533 680
    ;
a62d533 681
a62d533 682
fragment
a62d533 683
OctalIntegerLiteral
a62d533 684
    :   OctalNumeral IntegerTypeSuffix?
a62d533 685
    ;
a62d533 686
a62d533 687
fragment
a62d533 688
BinaryIntegerLiteral
a62d533 689
    :   BinaryNumeral IntegerTypeSuffix?
a62d533 690
    ;
a62d533 691
a62d533 692
fragment
a62d533 693
IntegerTypeSuffix
a62d533 694
    :   [lL]
a62d533 695
    ;
a62d533 696
a62d533 697
fragment
a62d533 698
DecimalNumeral
a62d533 699
    :   '0'
a62d533 700
    |   NonZeroDigit (Digits? | Underscores Digits)
a62d533 701
    ;
a62d533 702
a62d533 703
fragment
a62d533 704
Digits
a62d533 705
    :   Digit (DigitOrUnderscore* Digit)?
a62d533 706
    ;
a62d533 707
a62d533 708
fragment
a62d533 709
Digit
a62d533 710
    :   '0'
a62d533 711
    |   NonZeroDigit
a62d533 712
    ;
a62d533 713
a62d533 714
fragment
a62d533 715
NonZeroDigit
a62d533 716
    :   [1-9]
a62d533 717
    ;
a62d533 718
a62d533 719
fragment
a62d533 720
DigitOrUnderscore
a62d533 721
    :   Digit
a62d533 722
    |   '_'
a62d533 723
    ;
a62d533 724
a62d533 725
fragment
a62d533 726
Underscores
a62d533 727
    :   '_'+
a62d533 728
    ;
a62d533 729
a62d533 730
fragment
a62d533 731
HexNumeral
a62d533 732
    :   '0' [xX] HexDigits
a62d533 733
    ;
a62d533 734
a62d533 735
fragment
a62d533 736
HexDigits
a62d533 737
    :   HexDigit (HexDigitOrUnderscore* HexDigit)?
a62d533 738
    ;
a62d533 739
a62d533 740
fragment
a62d533 741
HexDigit
a62d533 742
    :   [0-9a-fA-F]
a62d533 743
    ;
a62d533 744
a62d533 745
fragment
a62d533 746
HexDigitOrUnderscore
a62d533 747
    :   HexDigit
a62d533 748
    |   '_'
a62d533 749
    ;
a62d533 750
a62d533 751
fragment
a62d533 752
OctalNumeral
a62d533 753
    :   '0' Underscores? OctalDigits
a62d533 754
    ;
a62d533 755
a62d533 756
fragment
a62d533 757
OctalDigits
a62d533 758
    :   OctalDigit (OctalDigitOrUnderscore* OctalDigit)?
a62d533 759
    ;
a62d533 760
a62d533 761
fragment
a62d533 762
OctalDigit
a62d533 763
    :   [0-7]
a62d533 764
    ;
a62d533 765
a62d533 766
fragment
a62d533 767
OctalDigitOrUnderscore
a62d533 768
    :   OctalDigit
a62d533 769
    |   '_'
a62d533 770
    ;
a62d533 771
a62d533 772
fragment
a62d533 773
BinaryNumeral
a62d533 774
    :   '0' [bB] BinaryDigits
a62d533 775
    ;
a62d533 776
a62d533 777
fragment
a62d533 778
BinaryDigits
a62d533 779
    :   BinaryDigit (BinaryDigitOrUnderscore* BinaryDigit)?
a62d533 780
    ;
a62d533 781
a62d533 782
fragment
a62d533 783
BinaryDigit
a62d533 784
    :   [01]
a62d533 785
    ;
a62d533 786
a62d533 787
fragment
a62d533 788
BinaryDigitOrUnderscore
a62d533 789
    :   BinaryDigit
a62d533 790
    |   '_'
a62d533 791
    ;
a62d533 792
a62d533 793
// §3.10.2 Floating-Point Literals
a62d533 794
a62d533 795
FloatingPointLiteral
a62d533 796
    :   DecimalFloatingPointLiteral
a62d533 797
    |   HexadecimalFloatingPointLiteral
a62d533 798
    ;
a62d533 799
a62d533 800
fragment
a62d533 801
DecimalFloatingPointLiteral
a62d533 802
    :   Digits '.' Digits? ExponentPart? FloatTypeSuffix?
a62d533 803
    |   '.' Digits ExponentPart? FloatTypeSuffix?
a62d533 804
    |   Digits ExponentPart FloatTypeSuffix?
a62d533 805
    |   Digits FloatTypeSuffix
a62d533 806
    ;
a62d533 807
a62d533 808
fragment
a62d533 809
ExponentPart
a62d533 810
    :   ExponentIndicator SignedInteger
a62d533 811
    ;
a62d533 812
a62d533 813
fragment
a62d533 814
ExponentIndicator
a62d533 815
    :   [eE]
a62d533 816
    ;
a62d533 817
a62d533 818
fragment
a62d533 819
SignedInteger
a62d533 820
    :   Sign? Digits
a62d533 821
    ;
a62d533 822
a62d533 823
fragment
a62d533 824
Sign
a62d533 825
    :   [+-]
a62d533 826
    ;
a62d533 827
a62d533 828
fragment
a62d533 829
FloatTypeSuffix
a62d533 830
    :   [fFdD]
a62d533 831
    ;
a62d533 832
a62d533 833
fragment
a62d533 834
HexadecimalFloatingPointLiteral
a62d533 835
    :   HexSignificand BinaryExponent FloatTypeSuffix?
a62d533 836
    ;
a62d533 837
a62d533 838
fragment
a62d533 839
HexSignificand
a62d533 840
    :   HexNumeral '.'?
a62d533 841
    |   '0' [xX] HexDigits? '.' HexDigits
a62d533 842
    ;
a62d533 843
a62d533 844
fragment
a62d533 845
BinaryExponent
a62d533 846
    :   BinaryExponentIndicator SignedInteger
a62d533 847
    ;
a62d533 848
a62d533 849
fragment
a62d533 850
BinaryExponentIndicator
a62d533 851
    :   [pP]
a62d533 852
    ;
a62d533 853
a62d533 854
// §3.10.3 Boolean Literals
a62d533 855
a62d533 856
BooleanLiteral
a62d533 857
    :   'true'
a62d533 858
    |   'false'
a62d533 859
    ;
a62d533 860
a62d533 861
// §3.10.4 Character Literals
a62d533 862
a62d533 863
CharacterLiteral
a62d533 864
    :   '\'' SingleCharacter '\''
a62d533 865
    |   '\'' EscapeSequence '\''
a62d533 866
    ;
a62d533 867
a62d533 868
fragment
a62d533 869
SingleCharacter
a62d533 870
    :   ~['\\]
a62d533 871
    ;
a62d533 872
a62d533 873
// §3.10.5 String Literals
a62d533 874
a62d533 875
StringLiteral
a62d533 876
    :   '"' StringCharacters? '"'
a62d533 877
    ;
a62d533 878
a62d533 879
fragment
a62d533 880
StringCharacters
a62d533 881
    :   StringCharacter+
a62d533 882
    ;
a62d533 883
a62d533 884
fragment
a62d533 885
StringCharacter
a62d533 886
    :   ~["\\]
a62d533 887
    |   EscapeSequence
a62d533 888
    ;
a62d533 889
a62d533 890
// §3.10.6 Escape Sequences for Character and String Literals
a62d533 891
a62d533 892
fragment
a62d533 893
EscapeSequence
a62d533 894
    :   '\\' [btnfr"'\\]
a62d533 895
    |   OctalEscape
a62d533 896
    |   UnicodeEscape
a62d533 897
    ;
a62d533 898
a62d533 899
fragment
a62d533 900
OctalEscape
a62d533 901
    :   '\\' OctalDigit
a62d533 902
    |   '\\' OctalDigit OctalDigit
a62d533 903
    |   '\\' ZeroToThree OctalDigit OctalDigit
a62d533 904
    ;
a62d533 905
a62d533 906
fragment
a62d533 907
UnicodeEscape
a62d533 908
    :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit
a62d533 909
    ;
a62d533 910
a62d533 911
fragment
a62d533 912
ZeroToThree
a62d533 913
    :   [0-3]
a62d533 914
    ;
a62d533 915
a62d533 916
// §3.10.7 The Null Literal
a62d533 917
a62d533 918
NullLiteral
a62d533 919
    :   'null'
a62d533 920
    ;
a62d533 921
a62d533 922
// §3.11 Separators
a62d533 923
a62d533 924
LPAREN          : '(';
a62d533 925
RPAREN          : ')';
a62d533 926
LBRACE          : '{';
a62d533 927
RBRACE          : '}';
a62d533 928
LBRACK          : '[';
a62d533 929
RBRACK          : ']';
a62d533 930
SEMI            : ';';
a62d533 931
COMMA           : ',';
a62d533 932
DOT             : '.';
a62d533 933
a62d533 934
// §3.12 Operators
a62d533 935
a62d533 936
ASSIGN          : '=';
a62d533 937
GT              : '>';
a62d533 938
LT              : '<';
a62d533 939
BANG            : '!';
a62d533 940
TILDE           : '~';
a62d533 941
QUESTION        : '?';
a62d533 942
COLON           : ':';
a62d533 943
EQUAL           : '==';
a62d533 944
LE              : '<=';
a62d533 945
GE              : '>=';
a62d533 946
NOTEQUAL        : '!=';
a62d533 947
AND             : '&&';
a62d533 948
OR              : '||';
a62d533 949
INC             : '++';
a62d533 950
DEC             : '--';
a62d533 951
ADD             : '+';
a62d533 952
SUB             : '-';
a62d533 953
MUL             : '*';
a62d533 954
DIV             : '/';
a62d533 955
BITAND          : '&';
a62d533 956
BITOR           : '|';
a62d533 957
CARET           : '^';
a62d533 958
MOD             : '%';
a62d533 959
a62d533 960
ADD_ASSIGN      : '+=';
a62d533 961
SUB_ASSIGN      : '-=';
a62d533 962
MUL_ASSIGN      : '*=';
a62d533 963
DIV_ASSIGN      : '/=';
a62d533 964
AND_ASSIGN      : '&=';
a62d533 965
OR_ASSIGN       : '|=';
a62d533 966
XOR_ASSIGN      : '^=';
a62d533 967
MOD_ASSIGN      : '%=';
a62d533 968
LSHIFT_ASSIGN   : '<<=';
a62d533 969
RSHIFT_ASSIGN   : '>>=';
a62d533 970
URSHIFT_ASSIGN  : '>>>=';
a62d533 971
a62d533 972
// §3.8 Identifiers (must appear after all keywords in the grammar)
a62d533 973
a62d533 974
Identifier
a62d533 975
    :   JavaLetter JavaLetterOrDigit*
a62d533 976
    ;
a62d533 977
a62d533 978
fragment
a62d533 979
JavaLetter
a62d533 980
    :   [a-zA-Z$_] // these are the "java letters" below 0xFF
a62d533 981
    |   // covers all characters above 0xFF which are not a surrogate
a62d533 982
        ~[\u0000-\u00FF\uD800-\uDBFF]
a62d533 983
        {Character.isJavaIdentifierStart(_input.LA(-1))}?
a62d533 984
    |   // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
a62d533 985
        [\uD800-\uDBFF] [\uDC00-\uDFFF]
a62d533 986
        {Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
a62d533 987
    ;
a62d533 988
a62d533 989
fragment
a62d533 990
JavaLetterOrDigit
a62d533 991
    :   [a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
a62d533 992
    |   // covers all characters above 0xFF which are not a surrogate
a62d533 993
        ~[\u0000-\u00FF\uD800-\uDBFF]
a62d533 994
        {Character.isJavaIdentifierPart(_input.LA(-1))}?
a62d533 995
    |   // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
a62d533 996
        [\uD800-\uDBFF] [\uDC00-\uDFFF]
a62d533 997
        {Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
a62d533 998
    ;
a62d533 999
a62d533 1000
//
a62d533 1001
// Additional symbols not defined in the lexical specification
a62d533 1002
//
a62d533 1003
a62d533 1004
AT : '@';
a62d533 1005
ELLIPSIS : '...';
a62d533 1006
a62d533 1007
//
a62d533 1008
// Whitespace and comments
a62d533 1009
//
a62d533 1010
a62d533 1011
WS  :  [ \t\r\n\u000C]+ -> skip
a62d533 1012
    ;
a62d533 1013
a62d533 1014
COMMENT
a62d533 1015
    :   '/*' .*? '*/' -> skip
a62d533 1016
    ;
a62d533 1017
a62d533 1018
LINE_COMMENT
a62d533 1019
    :   '//' ~[\r\n]* -> skip
a62d533 1020
    ;