grammar Java options language CSharp3 backtrack true memoize true outp

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
grammar Java;
options {
language=CSharp3;
backtrack = true;
memoize = true;
output = AST;
ASTLabelType = CommonTree;
}
tokens {
// operators and other special chars
AND = '&' ;
AND_ASSIGN = '&=' ;
ASSIGN = '=' ;
AT = '@' ;
BIT_SHIFT_RIGHT = '>>>' ;
BIT_SHIFT_RIGHT_ASSIGN = '>>>=' ;
COLON = ':' ;
COMMA = ',' ;
DEC = '--' ;
DIV = '/' ;
DIV_ASSIGN = '/=' ;
DOT = '.' ;
DOTSTAR = '.*' ;
ELLIPSIS = '...' ;
EQUAL = '==' ;
GREATER_OR_EQUAL = '>=' ;
GREATER_THAN = '>' ;
INC = '++' ;
LBRACK = '[' ;
LCURLY = '{' ;
LESS_OR_EQUAL = '<=' ;
LESS_THAN = '<' ;
LOGICAL_AND = '&&' ;
LOGICAL_NOT = '!' ;
LOGICAL_OR = '||' ;
LPAREN = '(' ;
MINUS = '-' ;
MINUS_ASSIGN = '-=' ;
MOD = '%' ;
MOD_ASSIGN = '%=' ;
NOT = '~' ;
NOT_EQUAL = '!=' ;
OR = '|' ;
OR_ASSIGN = '|=' ;
PLUS = '+' ;
PLUS_ASSIGN = '+=' ;
QUESTION = '?' ;
RBRACK = ']' ;
RCURLY = '}' ;
RPAREN = ')' ;
SEMI = ';' ;
SHIFT_LEFT = '<<' ;
SHIFT_LEFT_ASSIGN = '<<=' ;
SHIFT_RIGHT = '>>' ;
SHIFT_RIGHT_ASSIGN = '>>=' ;
STAR = '*' ;
STAR_ASSIGN = '*=' ;
XOR = '^' ;
XOR_ASSIGN = '^=' ;
// keywords
ABSTRACT = 'abstract' ;
// ASSERT = 'assert' ;
BOOLEAN = 'boolean#' ;
BREAK = 'break' ;
BYTE = 'byte#' ;
CASE = 'case' ;
CATCH = 'catch' ;
CHAR = 'char#' ;
CLASS = 'class' ;
CONTINUE = 'continue' ;
DEFAULT = 'default' ;
DO = 'do' ;
DOUBLE = 'double#' ;
ELSE = 'else' ;
ENUM = 'enum' ;
EXTENDS = 'extends' ;
FALSE = 'false' ;
FINAL = 'final' ;
FINALLY = 'finally' ;
FLOAT = 'float#' ;
FOR = 'for' ;
IF = 'if' ;
IMPLEMENTS = 'implements' ;
INSTANCEOF = 'instanceof' ;
INTERFACE = 'interface' ;
IMPORT = 'import' ;
INT = 'int#' ;
LONG = 'long#' ;
NATIVE = 'native' ;
NEW = 'new' ;
NULL = 'null' ;
PACKAGE = 'package' ;
PRIVATE = 'private' ;
PROTECTED = 'protected' ;
PUBLIC = 'public' ;
WITH_SHARING = 'with sharing' ;
WITHOUT_SHARING = 'without sharing' ;
VIRTUAL = 'virtual';
OVERRIDE = 'override';
RETURN = 'return' ;
SHORT = 'short#' ;
STATIC = 'static' ;
STRICTFP = 'strictfp' ;
SUPER = 'super' ;
SWITCH = 'switch' ;
SYNCHRONIZED = 'synchronized' ;
THIS = 'this' ;
THROW = 'throw' ;
THROWS = 'throws' ;
TRANSIENT = 'transient' ;
TRUE = 'true' ;
TRY = 'try' ;
VOID = 'void' ;
VOLATILE = 'volatile' ;
WHILE = 'while' ;
GLOBAL = 'global';
TEST_METHOD= 'testmethod';
// tokens for imaginary nodes
ANNOTATION_INIT_ARRAY_ELEMENT;
ANNOTATION_INIT_BLOCK;
ANNOTATION_INIT_DEFAULT_KEY;
ANNOTATION_INIT_KEY_LIST;
ANNOTATION_LIST;
ANNOTATION_METHOD_DECL;
ANNOTATION_SCOPE;
ANNOTATION_TOP_LEVEL_SCOPE;
ARGUMENT_LIST;
ARRAY_DECLARATOR;
ARRAY_DECLARATOR_LIST;
ARRAY_ELEMENT_ACCESS;
ARRAY_INITIALIZER;
MAP_INITIALIZER;
SET_INITIALIZER;
BLOCK_SCOPE;
CAST_EXPR;
CATCH_CLAUSE_LIST;
CLASS_CONSTRUCTOR_CALL;
CLASS_INSTANCE_INITIALIZER;
CLASS_STATIC_INITIALIZER;
CLASS_TOP_LEVEL_SCOPE;
CONSTRUCTOR_DECL;
ENUM_TOP_LEVEL_SCOPE;
EXPR;
EXTENDS_BOUND_LIST;
EXTENDS_CLAUSE;
FOR_CONDITION;
FOR_EACH;
FOR_INIT;
FOR_UPDATE;
FORMAL_PARAM_LIST;
FORMAL_PARAM_STD_DECL;
FORMAL_PARAM_VARARG_DECL;
FUNCTION_METHOD_DECL;
GENERIC_TYPE_ARG_LIST;
GENERIC_TYPE_PARAM_LIST;
INTERFACE_TOP_LEVEL_SCOPE;
IMPLEMENTS_CLAUSE;
LABELED_STATEMENT;
LOCAL_MODIFIER_LIST;
JAVA_SOURCE;
METHOD_CALL;
MODIFIER_LIST;
PARENTESIZED_EXPR;
POST_DEC;
POST_INC;
PRE_DEC;
PRE_INC;
QUALIFIED_TYPE_IDENT;
STATIC_ARRAY_CREATOR;
SUPER_CONSTRUCTOR_CALL;
SWITCH_BLOCK_LABEL_LIST;
THIS_CONSTRUCTOR_CALL;
THROWS_CLAUSE;
TYPE;
UNARY_MINUS;
UNARY_PLUS;
VAR_DECLARATION;
VAR_DECLARATOR;
VAR_DECLARATOR_LIST;
VOID_METHOD_DECL;
PROPERTY_DECL;
BROKEN_EXPRESSION;
BROKEN_DECLARATION;
}
@header {
//package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@members {
private bool mMessageCollectionEnabled = true;
private List<string> mMessages;
public void enableErrorMessageCollection(bool pNewState) {
mMessageCollectionEnabled = pNewState;
if (mMessages == null && mMessageCollectionEnabled) {
mMessages = new List<string>();
}
}
public override void EmitErrorMessage(string pMessage)
{
if (mMessageCollectionEnabled) {
mMessages.Add(pMessage);
} else {
base.EmitErrorMessage(pMessage);
}
}
public List<string> getMessages() {
return mMessages;
}
public bool hasErrors() {
return mMessages.Count>0;
}
}
@lexer::header {
//package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@lexer::members {
/**
* Determines if whitespaces and comments should be preserved or thrown away.
*
* If <code>true</code> whitespaces and comments will be preserved within the
* hidden channel, otherwise the appropriate tokens will be skiped. This is
* a 'little bit' expensive, of course. If only one of the two behaviours is
* needed forever the lexer part of the grammar should be changed by replacing
* the 'if-else' stuff within the approprate lexer grammar actions.
*/
public bool preserveWhitespacesAndComments = false;
}
// Starting point for parsing a Java file.
javaSource
: compilationUnit
-> ^(JAVA_SOURCE compilationUnit)
;
compilationUnit
: annotationList
packageDeclaration?
importDeclaration*
typeDecls*
;
typeDecls
: typeDeclaration
| SEMI!
;
packageDeclaration
: PACKAGE^ qualifiedIdentifier SEMI!
;
importDeclaration
: IMPORT^ STATIC? qualifiedIdentifier DOTSTAR? SEMI!
;
typeDeclaration
: modifierList!
( classTypeDeclaration[$modifierList.tree]
| interfaceTypeDeclaration[$modifierList.tree]
| enumTypeDeclaration[$modifierList.tree]
| annotationTypeDeclaration[$modifierList.tree]
)
;
classTypeDeclaration[CommonTree modifiers]
: CLASS IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody
-> ^(CLASS {$modifiers} IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody)
;
classExtendsClause
: EXTENDS type
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] type)
;
interfaceExtendsClause
: EXTENDS typeList
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] typeList)
;
implementsClause
: IMPLEMENTS typeList
-> ^(IMPLEMENTS_CLAUSE[$IMPLEMENTS, "IMPLEMENTS_CLAUSE"] typeList)
;
genericTypeParameterList
: LESS_THAN genericTypeParameter (COMMA genericTypeParameter)* genericTypeListClosing
-> ^(GENERIC_TYPE_PARAM_LIST[$LESS_THAN, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
;
genericTypeListClosing // This 'trick' is fairly dirty - if there's some time a better solution should
// be found to resolve the problem with nested generic type parameter lists
// (i.e. <T1 extends AnyType<T2>> for generic type parameters or <T1<T2>> for
// generic type arguments etc).
: GREATER_THAN
| SHIFT_RIGHT
| BIT_SHIFT_RIGHT
| // nothing
;
genericTypeParameter
: IDENT bound?
-> ^(IDENT bound?)
;
bound
: EXTENDS type (AND type)*
-> ^(EXTENDS_BOUND_LIST[$EXTENDS, "EXTENDS_BOUND_LIST"] type+)
;
enumTypeDeclaration[CommonTree modifiers]
: ENUM IDENT implementsClause? enumBody
-> ^(ENUM {$modifiers} IDENT implementsClause? enumBody)
;
enumBody
: LCURLY enumScopeDeclarations RCURLY
-> ^(ENUM_TOP_LEVEL_SCOPE[$LCURLY, "ENUM_TOP_LEVEL_SCOPE"] enumScopeDeclarations)
;
enumScopeDeclarations
: enumConstants (COMMA!)? enumClassScopeDeclarations?
;
enumClassScopeDeclarations
: SEMI classScopeDeclarations*
-> ^(CLASS_TOP_LEVEL_SCOPE[$SEMI, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
;
enumConstants
: enumConstant (COMMA! enumConstant)*
;
enumConstant
: annotationList IDENT^ arguments? classBody?
;
interfaceTypeDeclaration[CommonTree modifiers]
: INTERFACE IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody
-> ^(INTERFACE {$modifiers} IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody)
;
typeList
: type (COMMA! type)*
;
classBody
: LCURLY classScopeDeclarations* RCURLY
-> ^(CLASS_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
;
interfaceBody
: LCURLY interfaceScopeDeclarations* RCURLY
-> ^(INTERFACE_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] interfaceScopeDeclarations*)
;
classScopeDeclarations
: block -> ^(CLASS_INSTANCE_INITIALIZER block)
| STATIC block -> ^(CLASS_STATIC_INITIALIZER[$STATIC, "CLASS_STATIC_INITIALIZER"] block)
| modifierList
(
type IDENT propertyDeclaration -> ^(PROPERTY_DECL modifierList type IDENT propertyDeclaration)
| genericTypeParameterList?
(
type IDENT formalParameterList arrayDeclaratorList? throwsClause? (block | SEMI)
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause? block?)
| VOID IDENT formalParameterList throwsClause? (block | SEMI)
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause? block?)
| ident=IDENT formalParameterList throwsClause? block
-> ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList genericTypeParameterList? formalParameterList throwsClause? block)
)
| type classFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
| type classFieldDeclaratorList DOT? SEMI?
-> ^(BROKEN_DECLARATION modifierList type classFieldDeclaratorList)
)
| typeDeclaration
| SEMI!
;
interfaceScopeDeclarations
: modifierList
( genericTypeParameterList?
( type IDENT formalParameterList arrayDeclaratorList? throwsClause? SEMI
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?)
| VOID IDENT formalParameterList throwsClause? SEMI
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?)
)
| type interfaceFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type interfaceFieldDeclaratorList)
)
| typeDeclaration
| SEMI!
;
classFieldDeclaratorList
: classFieldDeclarator (COMMA classFieldDeclarator)*
-> ^(VAR_DECLARATOR_LIST classFieldDeclarator+)
;
classFieldDeclarator
: variableDeclaratorId (ASSIGN variableInitializer)?
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer?)
;
interfaceFieldDeclaratorList
: interfaceFieldDeclarator (COMMA interfaceFieldDeclarator)*
-> ^(VAR_DECLARATOR_LIST interfaceFieldDeclarator+)
;
interfaceFieldDeclarator
: variableDeclaratorId ASSIGN variableInitializer
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer)
;
variableDeclaratorId
: IDENT^ arrayDeclaratorList?
;
variableInitializer
: arrayInitializer
| expression
| brokenExpression -> ^(BROKEN_EXPRESSION brokenExpression)
;
arrayDeclarator
: LBRACK RBRACK
-> ^(ARRAY_DECLARATOR)
;
arrayDeclaratorList
: arrayDeclarator+
-> ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)
;
arrayInitializer
: LCURLY (variableInitializer (COMMA variableInitializer)* COMMA?)? RCURLY
-> ^(ARRAY_INITIALIZER[$LCURLY, "ARRAY_INITIALIZER"] variableInitializer*)
;
throwsClause
: THROWS qualifiedIdentList
-> ^(THROWS_CLAUSE[$THROWS, "THROWS_CLAUSE"] qualifiedIdentList)
;
modifierList
: modifier*
-> ^(MODIFIER_LIST modifier*)
;
modifier
: PUBLIC
| OVERRIDE
| VIRTUAL
| WITH_SHARING
| WITHOUT_SHARING
| PROTECTED
| PRIVATE
| STATIC
| ABSTRACT
| NATIVE
| SYNCHRONIZED
| TRANSIENT
| VOLATILE
| STRICTFP
| GLOBAL
| TEST_METHOD
| localModifier
;
localModifierList
: localModifier*
-> ^(LOCAL_MODIFIER_LIST localModifier*)
;
localModifier
: FINAL
| annotation
;
type
: simpleType
| objectType
;
simpleType // including static arrays of simple type elements
: primitiveType arrayDeclaratorList?
-> ^(TYPE primitiveType arrayDeclaratorList?)
;
objectType // including static arrays of object type reference elements
: qualifiedTypeIdent arrayDeclaratorList?
-> ^(TYPE qualifiedTypeIdent arrayDeclaratorList?)
;
objectTypeSimplified
: qualifiedTypeIdentSimplified arrayDeclaratorList?
-> ^(TYPE qualifiedTypeIdentSimplified arrayDeclaratorList?)
;
qualifiedTypeIdent
: typeIdent (DOT typeIdent)*
-> ^(QUALIFIED_TYPE_IDENT typeIdent+)
;
qualifiedTypeIdentSimplified
: typeIdentSimplified (DOT typeIdentSimplified)*
-> ^(QUALIFIED_TYPE_IDENT typeIdentSimplified+)
;
typeIdent
: IDENT^ genericTypeArgumentList?
;
typeIdentSimplified
: IDENT^ genericTypeArgumentListSimplified?
;
primitiveType
:
BOOLEAN
| CHAR
| BYTE
| SHORT
| INT
| LONG
| FLOAT
| DOUBLE
;
genericTypeArgumentList
: LESS_THAN genericTypeArgument (COMMA genericTypeArgument)* genericTypeListClosing
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
;
genericTypeArgument
: type
| QUESTION genericWildcardBoundType?
-> ^(QUESTION genericWildcardBoundType?)
;
genericWildcardBoundType
: (EXTENDS | SUPER)^ type
;
genericTypeArgumentListSimplified
: LESS_THAN genericTypeArgumentSimplified (COMMA genericTypeArgumentSimplified)* genericTypeListClosing
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgumentSimplified+)
;
genericTypeArgumentSimplified
: type
| QUESTION
;
qualifiedIdentList
: qualifiedIdentifier (COMMA! qualifiedIdentifier)*
;
formalParameterList
: LPAREN
( // Contains at least one standard argument declaration and optionally a variable argument declaration.
formalParameterStandardDecl (COMMA formalParameterStandardDecl)* (COMMA formalParameterVarArgDecl)?
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterStandardDecl+ formalParameterVarArgDecl?)
// Contains a variable argument declaration only.
| formalParameterVarArgDecl
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterVarArgDecl)
// Contains nothing.
| -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"])
)
RPAREN
;
formalParameterStandardDecl
: localModifierList type variableDeclaratorId
-> ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId)
;
formalParameterVarArgDecl
: localModifierList type ELLIPSIS variableDeclaratorId
-> ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId)
;
qualifiedIdentifier
: ( IDENT -> IDENT
)
( DOT ident=IDENT -> ^(DOT $qualifiedIdentifier $ident)
)*
;
// ANNOTATIONS
annotationList
: annotation*
-> ^(ANNOTATION_LIST annotation*)
;
annotation
: AT^ qualifiedIdentifier annotationInit?
;
annotationInit
: LPAREN annotationInitializers RPAREN
-> ^(ANNOTATION_INIT_BLOCK[$LPAREN, "ANNOTATION_INIT_BLOCK"] annotationInitializers)
;
annotationInitializers
: annotationInitializer (COMMA annotationInitializer)*
-> ^(ANNOTATION_INIT_KEY_LIST annotationInitializer+)
| annotationElementValue // implicite initialization of the annotation field 'value'
-> ^(ANNOTATION_INIT_DEFAULT_KEY annotationElementValue)
;
annotationInitializer
: IDENT^ ASSIGN! annotationElementValue
;
annotationElementValue
: annotationElementValueExpression
| annotation
| annotationElementValueArrayInitializer
;
annotationElementValueExpression
: conditionalExpression
-> ^(EXPR conditionalExpression)
;
annotationElementValueArrayInitializer
: LCURLY (annotationElementValue (COMMA annotationElementValue)*)? (COMMA)? RCURLY
-> ^(ANNOTATION_INIT_ARRAY_ELEMENT[$LCURLY, "ANNOTATION_ELEM_VALUE_ARRAY_INIT"] annotationElementValue*)
;
annotationTypeDeclaration[CommonTree modifiers]
: AT INTERFACE IDENT annotationBody
-> ^(AT {$modifiers} IDENT annotationBody)
;
annotationBody
: LCURLY annotationScopeDeclarations* RCURLY
-> ^(ANNOTATION_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] annotationScopeDeclarations*)
;
annotationScopeDeclarations
: modifierList type
( IDENT LPAREN RPAREN annotationDefaultValue? SEMI
-> ^(ANNOTATION_METHOD_DECL modifierList type IDENT annotationDefaultValue?)
| classFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
)
| typeDeclaration
;
annotationDefaultValue
: DEFAULT^ annotationElementValue
;
// STATEMENTS / BLOCKS
blockStatement
: localVariableDeclaration SEMI!
| typeDeclaration
| statement
| brokenExpression -> ^(BROKEN_EXPRESSION brokenExpression)
;
brokenExpression
:
LPAREN* expression DOT? SEMI?
;
dmlOperation
:
dmlOperator expression SEMI
;
dmlOperator
:
( {input.LT(1).Text == "insert"}? IDENT )
|( {input.LT(1).Text == "update"}? IDENT )
|( {input.LT(1).Text == "delete"}? IDENT )
|( {input.LT(1).Text == "upsert"}? IDENT )
;
block
: LCURLY
(
blockStatement*-> ^(BLOCK_SCOPE[$LCURLY, "BLOCK_SCOPE"] blockStatement*)
)
RCURLY
;
localVariableDeclaration
: localModifierList type classFieldDeclaratorList
-> ^(VAR_DECLARATION localModifierList type classFieldDeclaratorList)
;
statement
: block
| IF parenthesizedExpression ifStat=statement
( ELSE elseStat=statement -> ^(IF parenthesizedExpression $ifStat $elseStat)
| -> ^(IF parenthesizedExpression $ifStat)
)
| FOR LPAREN
( forInit SEMI forCondition SEMI forUpdater RPAREN statement -> ^(FOR forInit forCondition forUpdater statement)
| localModifierList type IDENT COLON expression RPAREN statement -> ^(FOR_EACH[$FOR, "FOR_EACH"] localModifierList type IDENT expression statement)
)
| WHILE parenthesizedExpression statement -> ^(WHILE parenthesizedExpression statement)
| DO statement WHILE parenthesizedExpression SEMI -> ^(DO statement parenthesizedExpression)
| TRY block (catches finallyClause? | finallyClause) -> ^(TRY block catches? finallyClause?)
| SWITCH parenthesizedExpression LCURLY switchBlockLabels RCURLY -> ^(SWITCH parenthesizedExpression switchBlockLabels)
| SYNCHRONIZED parenthesizedExpression block -> ^(SYNCHRONIZED parenthesizedExpression block)
| RETURN expression? SEMI -> ^(RETURN expression?)
| THROW expression SEMI -> ^(THROW expression)
| BREAK IDENT? SEMI -> ^(BREAK IDENT?)
| CONTINUE IDENT? SEMI -> ^(CONTINUE IDENT?)
| IDENT COLON statement -> ^(LABELED_STATEMENT IDENT statement)
| dmlOperation
| expression SEMI!
| {input.LT(1).Text == "system" && input.LT(2).Text == "." && input.LT(3).Text == "runas"}? expression
| SEMI // Preserve empty statements.
;
catches
: catchClause+
-> ^(CATCH_CLAUSE_LIST catchClause+)
;
catchClause
: CATCH^ LPAREN! formalParameterStandardDecl RPAREN! block
;
finallyClause
: FINALLY block
-> block
;
switchBlockLabels
: switchCaseLabels switchDefaultLabel? switchCaseLabels
-> ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabels switchDefaultLabel? switchCaseLabels)
;
switchCaseLabels
: switchCaseLabel*
;
switchCaseLabel
: CASE^ expression COLON! blockStatement*
;
switchDefaultLabel
: DEFAULT^ COLON! blockStatement*
;
forInit
: localVariableDeclaration -> ^(FOR_INIT localVariableDeclaration)
| expressionList -> ^(FOR_INIT expressionList)
| -> ^(FOR_INIT)
;
forCondition
: expression?
-> ^(FOR_CONDITION expression?)
;
forUpdater
: expressionList?
-> ^(FOR_UPDATE expressionList?)
;
// EXPRESSIONS
parenthesizedExpression
: LPAREN expression RPAREN
-> ^(PARENTESIZED_EXPR[$LPAREN, "PARENTESIZED_EXPR"] expression)
;
expressionList
: expression (COMMA! expression)*
;
expression
:
(assignmentExpression -> ^(EXPR assignmentExpression))
;
assignmentExpression
: conditionalExpression
( ( ASSIGN^
| PLUS_ASSIGN^
| MINUS_ASSIGN^
| STAR_ASSIGN^
| DIV_ASSIGN^
| AND_ASSIGN^
| OR_ASSIGN^
| XOR_ASSIGN^
| MOD_ASSIGN^
| SHIFT_LEFT_ASSIGN^
| SHIFT_RIGHT_ASSIGN^
| BIT_SHIFT_RIGHT_ASSIGN^
)
assignmentExpression)?
;
conditionalExpression
: logicalOrExpression (QUESTION^ assignmentExpression COLON! conditionalExpression)?
;
logicalOrExpression
: logicalAndExpression (LOGICAL_OR^ logicalAndExpression)*
;
logicalAndExpression
: inclusiveOrExpression (LOGICAL_AND^ inclusiveOrExpression)*
;
inclusiveOrExpression
: exclusiveOrExpression (OR^ exclusiveOrExpression)*
;
exclusiveOrExpression
: andExpression (XOR^ andExpression)*
;
andExpression
: equalityExpression (AND^ equalityExpression)*
;
equalityExpression
: instanceOfExpression
( ( EQUAL^
| NOT_EQUAL^
)
instanceOfExpression
)*
;
instanceOfExpression
: relationalExpression (INSTANCEOF^ type)?
;
relationalExpression
: shiftExpression
( ( LESS_OR_EQUAL^
| GREATER_OR_EQUAL^
| LESS_THAN^
| GREATER_THAN^
)
shiftExpression
)*
;
shiftExpression
: additiveExpression
( ( BIT_SHIFT_RIGHT^
| SHIFT_RIGHT^
| SHIFT_LEFT^
)
additiveExpression
)*
;
additiveExpression
: multiplicativeExpression
( ( PLUS^
| MINUS^
)
multiplicativeExpression
)*
;
multiplicativeExpression
: unaryExpression
( ( STAR^
| DIV^
| MOD^
)
unaryExpression
)*
;
unaryExpression
: PLUS unaryExpression -> ^(UNARY_PLUS[$PLUS, "UNARY_PLUS"] unaryExpression)
| MINUS unaryExpression -> ^(UNARY_MINUS[$MINUS, "UNARY_MINUS"] unaryExpression)
| INC postfixedExpression -> ^(PRE_INC[$INC, "PRE_INC"] postfixedExpression)
| DEC postfixedExpression -> ^(PRE_DEC[$DEC, "PRE_DEC"] postfixedExpression)
| unaryExpressionNotPlusMinus
;
unaryExpressionNotPlusMinus
: NOT unaryExpression -> ^(NOT unaryExpression)
| LOGICAL_NOT unaryExpression -> ^(LOGICAL_NOT unaryExpression)
| LPAREN type RPAREN unaryExpression -> ^(CAST_EXPR[$LPAREN, "CAST_EXPR"] type unaryExpression)
| postfixedExpression
;
postfixedExpression
// At first resolve the primary expression ...
: ( primaryExpression -> primaryExpression )
// ... and than the optional things that may follow a primary expression 0 or more times.
( outerDot=DOT
( ( genericTypeArgumentListSimplified? // Note: generic type arguments are only valid for method calls, i.e. if there
// is an argument list.
IDENT -> ^(DOT $postfixedExpression IDENT)
)
( arguments -> ^(METHOD_CALL $postfixedExpression genericTypeArgumentListSimplified? arguments)
)?
| THIS -> ^(DOT $postfixedExpression THIS)
| Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] $postfixedExpression arguments)
| ( SUPER innerDot=DOT IDENT -> ^($innerDot ^($outerDot $postfixedExpression SUPER) IDENT)
)
( arguments -> ^(METHOD_CALL $postfixedExpression arguments)
)?
| innerNewExpression -> ^(DOT $postfixedExpression innerNewExpression)
)
| LBRACK expression RBRACK -> ^(ARRAY_ELEMENT_ACCESS $postfixedExpression expression)
)*
// At the end there may follow a post increment/decrement.
( INC -> ^(POST_INC[$INC, "POST_INC"] $postfixedExpression)
| DEC -> ^(POST_DEC[$DEC, "POST_DEC"] $postfixedExpression)
)?
;
primaryExpression
: parenthesizedExpression
| literal
| newExpression
| qualifiedIdentExpression
| genericTypeArgumentListSimplified
( SUPER
( arguments -> ^(SUPER_CONSTRUCTOR_CALL[$SUPER, "SUPER_CONSTRUCTOR_CALL"] genericTypeArgumentListSimplified arguments)
| DOT IDENT arguments -> ^(METHOD_CALL ^(DOT SUPER IDENT) genericTypeArgumentListSimplified arguments)
)
| IDENT arguments -> ^(METHOD_CALL IDENT genericTypeArgumentListSimplified arguments)
| THIS arguments -> ^(THIS_CONSTRUCTOR_CALL[$THIS, "THIS_CONSTRUCTOR_CALL"] genericTypeArgumentListSimplified arguments)
)
| ( THIS -> THIS
)
( arguments -> ^(THIS_CONSTRUCTOR_CALL[$THIS, "THIS_CONSTRUCTOR_CALL"] arguments)
)?
| SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$SUPER, "SUPER_CONSTRUCTOR_CALL"] arguments)
| ( SUPER DOT IDENT
)
( arguments -> ^(METHOD_CALL ^(DOT SUPER IDENT) arguments)
| -> ^(DOT SUPER IDENT)
)
| ( primitiveType -> primitiveType
)
( arrayDeclarator -> ^(arrayDeclarator $primaryExpression)
)*
DOT CLASS -> ^(DOT $primaryExpression CLASS)
| VOID DOT CLASS -> ^(DOT VOID CLASS)
;
qualifiedIdentExpression
// The qualified identifier itself is the starting point for this rule.
: ( qualifiedIdentifier -> qualifiedIdentifier
)
// And now comes the stuff that may follow the qualified identifier.
( ( arrayDeclarator -> ^(arrayDeclarator $qualifiedIdentExpression)
)+
( DOT CLASS -> ^(DOT $qualifiedIdentExpression CLASS)
)
| arguments block? -> ^(METHOD_CALL qualifiedIdentifier arguments)
| outerDot=DOT
( CLASS -> ^(DOT qualifiedIdentifier CLASS)
| genericTypeArgumentListSimplified
( Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] qualifiedIdentifier genericTypeArgumentListSimplified arguments)
| SUPER innerDot=DOT IDENT arguments -> ^(METHOD_CALL ^($innerDot ^($outerDot qualifiedIdentifier SUPER) IDENT) genericTypeArgumentListSimplified arguments)
| IDENT arguments -> ^(METHOD_CALL ^(DOT qualifiedIdentifier IDENT) genericTypeArgumentListSimplified arguments)
)
| THIS -> ^(DOT qualifiedIdentifier THIS)
| Super=SUPER arguments -> ^(SUPER_CONSTRUCTOR_CALL[$Super, "SUPER_CONSTRUCTOR_CALL"] qualifiedIdentifier arguments)
| innerNewExpression -> ^(DOT qualifiedIdentifier innerNewExpression)
)
)?
;
mapDeclaration
:
LCURLY expression '=>' expression (COMMA expression '=>' expression )* RCURLY
;
setDeclaration
:
LCURLY expression? (COMMA expression )* RCURLY
;
newExpression
: NEW
(
primitiveType newArrayConstruction // new static array of primitive type elements
-> ^(STATIC_ARRAY_CREATOR[$NEW, "STATIC_ARRAY_CREATOR"] primitiveType newArrayConstruction)
| genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified
(
//todo:äîáàâèòü îãðàíè÷åíèå íà ìàïó è ñåò
mapDeclaration -> ^(MAP_INITIALIZER mapDeclaration)
| setDeclaration -> ^(SET_INITIALIZER setDeclaration)
| newArrayConstruction // new static array of object type reference elements
-> ^(STATIC_ARRAY_CREATOR[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified newArrayConstruction)
| arguments classBody? // new object type via constructor invocation
-> ^(CLASS_CONSTRUCTOR_CALL[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? qualifiedTypeIdentSimplified arguments classBody?)
)
)
;
innerNewExpression // something like 'InnerType innerType = outer.new InnerType();'
: NEW genericTypeArgumentListSimplified? IDENT arguments classBody?
-> ^(CLASS_CONSTRUCTOR_CALL[$NEW, "STATIC_ARRAY_CREATOR"] genericTypeArgumentListSimplified? IDENT arguments classBody?)
;
newArrayConstruction
: arrayDeclaratorList arrayInitializer
| LBRACK! expression RBRACK! (LBRACK! expression RBRACK!)* arrayDeclaratorList?
;
arguments
: LPAREN expressionList? RPAREN
-> ^(ARGUMENT_LIST[$LPAREN, "ARGUMENT_LIST"] expressionList?)
;
literal
: HEX_LITERAL
| OCTAL_LITERAL
| DECIMAL_LITERAL
| FLOATING_POINT_LITERAL
| CHARACTER_LITERAL
| STRING_LITERAL
| SOQL_LITERAL
| TRUE
| FALSE
| NULL
;
propertyDeclaration
:
('{' modifier? getRule (SEMI|block) (modifier? setRule (SEMI|block))? '}')
| ('{' modifier? setRule (SEMI|block) (modifier? getRule (SEMI|block))? '}')
//todo:add ovverite after debug this shit!
;
getRule
:
{input.LT(1).Text == "get"}? IDENT
;
setRule
:
{input.LT(1).Text== "set"}? IDENT
;
// LEXER
HEX_LITERAL : '0' ('x'|'X') HEX_DIGIT+ INTEGER_TYPE_SUFFIX? ;
DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) INTEGER_TYPE_SUFFIX? ;
OCTAL_LITERAL : '0' ('0'..'7')+ INTEGER_TYPE_SUFFIX? ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
INTEGER_TYPE_SUFFIX : ('l'|'L') ;
FLOATING_POINT_LITERAL
: ('0'..'9')+
(
DOT ('0'..'9')* EXPONENT? FLOAT_TYPE_SUFFIX?
| EXPONENT FLOAT_TYPE_SUFFIX?
| FLOAT_TYPE_SUFFIX
)
| DOT ('0'..'9')+ EXPONENT? FLOAT_TYPE_SUFFIX?
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
FLOAT_TYPE_SUFFIX : ('f'|'F'|'d'|'D') ;
CHARACTER_LITERAL
: '\'' ( ESCAPE_SEQUENCE | ~('\''|'\\') ) '\''
;
STRING_LITERAL
: ( '"' ( ESCAPE_SEQUENCE | ~('\\'|'"') )* '"')
| ('\'' ( ESCAPE_SEQUENCE | ~('\''|'\\') )* '\'')
;
SOQL_LITERAL
: LBRACK (' ')* ('select') ( (LBRACK ~('['|']'|'\\'|'"')* RBRACK)| ESCAPE_SEQUENCE | ~('['|']'|'\\'|'"') )* RBRACK
;
fragment
ESCAPE_SEQUENCE
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESCAPE
| OCTAL_ESCAPE
;
fragment
OCTAL_ESCAPE
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESCAPE
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
IDENT
: (JAVA_ID_START (JAVA_ID_PART)*)
;
fragment
JAVA_ID_START
: '\u0024'
| '\u0041'..'\u005a'
| '\u005f'
| '\u0061'..'\u007a'
| '\u00c0'..'\u00d6'
| '\u00d8'..'\u00f6'
| '\u00f8'..'\u00ff'
| '\u0100'..'\u1fff'
| '\u3040'..'\u318f'
| '\u3300'..'\u337f'
| '\u3400'..'\u3d2d'
| '\u4e00'..'\u9fff'
| '\uf900'..'\ufaff'
;
fragment
JAVA_ID_PART
: JAVA_ID_START
| '\u0030'..'\u0039'
;
WS : (' '|'\r'|'\t'|'\u000C'|'\n')
{
if (!preserveWhitespacesAndComments) {
Skip();
} else {
$channel = Hidden;
}
}
;
COMMENT
: '/*' ( options {greedy=false;} : . )* '*/'
{
if (!preserveWhitespacesAndComments) {
Skip();
} else {
$channel = Hidden;
}
}
;
LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n'
{
if (!preserveWhitespacesAndComments) {
Skip();
} else {
$channel = Hidden;
}
}
;