antlr3 1.8.0 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/History.txt +35 -0
  2. data/Manifest.txt +73 -0
  3. data/README.txt +6 -13
  4. data/java/RubyTarget.java +43 -19
  5. data/java/antlr-full-3.2.1.jar +0 -0
  6. data/lib/antlr3/debug.rb +2 -0
  7. data/lib/antlr3/debug/event-hub.rb +55 -55
  8. data/lib/antlr3/debug/record-event-listener.rb +2 -2
  9. data/lib/antlr3/debug/rule-tracer.rb +14 -14
  10. data/lib/antlr3/debug/socket.rb +47 -47
  11. data/lib/antlr3/debug/trace-event-listener.rb +8 -8
  12. data/lib/antlr3/main.rb +29 -9
  13. data/lib/antlr3/modes/ast-builder.rb +7 -7
  14. data/lib/antlr3/modes/filter.rb +19 -17
  15. data/lib/antlr3/profile.rb +34 -6
  16. data/lib/antlr3/recognizers.rb +50 -1
  17. data/lib/antlr3/streams.rb +19 -15
  18. data/lib/antlr3/streams/rewrite.rb +241 -229
  19. data/lib/antlr3/template/group-file-lexer.rb +6 -8
  20. data/lib/antlr3/template/group-file-parser.rb +16 -16
  21. data/lib/antlr3/template/group-file.rb +1 -1
  22. data/lib/antlr3/test/call-stack.rb +13 -13
  23. data/lib/antlr3/test/core-extensions.rb +69 -69
  24. data/lib/antlr3/test/functional.rb +0 -4
  25. data/lib/antlr3/test/grammar.rb +70 -70
  26. data/lib/antlr3/token.rb +41 -17
  27. data/lib/antlr3/tree.rb +11 -14
  28. data/lib/antlr3/tree/debug.rb +53 -53
  29. data/lib/antlr3/tree/visitor.rb +11 -11
  30. data/lib/antlr3/tree/wizard.rb +35 -35
  31. data/lib/antlr3/util.rb +18 -0
  32. data/lib/antlr3/version.rb +1 -1
  33. data/rakefile +1 -0
  34. data/samples/ANTLRv3Grammar.g +3 -3
  35. data/samples/JavaScript.g +702 -0
  36. data/samples/standard/C/C.g +543 -0
  37. data/samples/standard/C/C.tokens +175 -0
  38. data/samples/standard/C/C__testrig.st +0 -0
  39. data/samples/standard/C/c.rb +12 -0
  40. data/samples/standard/C/input +3479 -0
  41. data/samples/standard/C/output +171 -0
  42. data/samples/standard/LL-star/LLStar.g +101 -0
  43. data/samples/standard/LL-star/input +12 -0
  44. data/samples/standard/LL-star/ll-star.rb +12 -0
  45. data/samples/standard/LL-star/output +2 -0
  46. data/samples/standard/calc/Calculator.g +47 -0
  47. data/samples/standard/calc/Calculator.py +16 -0
  48. data/samples/standard/calc/Calculator.rb +28 -0
  49. data/samples/standard/cminus/CMinus.g +141 -0
  50. data/samples/standard/cminus/bytecode.group +80 -0
  51. data/samples/standard/cminus/cminus.rb +16 -0
  52. data/samples/standard/cminus/input +9 -0
  53. data/samples/standard/cminus/java.group +91 -0
  54. data/samples/standard/cminus/output +11 -0
  55. data/samples/standard/cminus/python.group +48 -0
  56. data/samples/standard/dynamic-scope/DynamicScopes.g +50 -0
  57. data/samples/standard/dynamic-scope/dynamic-scopes.rb +12 -0
  58. data/samples/standard/dynamic-scope/input +7 -0
  59. data/samples/standard/dynamic-scope/output +4 -0
  60. data/samples/standard/fuzzy/FuzzyJava.g +89 -0
  61. data/samples/standard/fuzzy/fuzzy.py +11 -0
  62. data/samples/standard/fuzzy/fuzzy.rb +9 -0
  63. data/samples/standard/fuzzy/input +13 -0
  64. data/samples/standard/fuzzy/output +12 -0
  65. data/samples/standard/hoisted-predicates/HoistedPredicates.g +40 -0
  66. data/samples/standard/hoisted-predicates/hoisted-predicates.rb +13 -0
  67. data/samples/standard/hoisted-predicates/input +1 -0
  68. data/samples/standard/hoisted-predicates/output +1 -0
  69. data/samples/standard/island-grammar/Javadoc.g +46 -0
  70. data/samples/standard/island-grammar/Simple.g +104 -0
  71. data/samples/standard/island-grammar/input +11 -0
  72. data/samples/standard/island-grammar/island.rb +12 -0
  73. data/samples/standard/island-grammar/output +16 -0
  74. data/samples/standard/java/Java.g +827 -0
  75. data/samples/standard/java/input +80 -0
  76. data/samples/standard/java/java.rb +13 -0
  77. data/samples/standard/java/output +1 -0
  78. data/samples/standard/python/Python.g +718 -0
  79. data/samples/standard/python/PythonTokenSource.rb +107 -0
  80. data/samples/standard/python/input +210 -0
  81. data/samples/standard/python/output +24 -0
  82. data/samples/standard/python/python.rb +14 -0
  83. data/samples/standard/rakefile +18 -0
  84. data/samples/standard/scopes/SymbolTable.g +66 -0
  85. data/samples/standard/scopes/input +12 -0
  86. data/samples/standard/scopes/output +3 -0
  87. data/samples/standard/scopes/scopes.rb +12 -0
  88. data/samples/standard/simplecTreeParser/SimpleC.g +113 -0
  89. data/samples/standard/simplecTreeParser/SimpleCWalker.g +64 -0
  90. data/samples/standard/simplecTreeParser/input +12 -0
  91. data/samples/standard/simplecTreeParser/output +1 -0
  92. data/samples/standard/simplecTreeParser/simplec.rb +18 -0
  93. data/samples/standard/treeparser/Lang.g +24 -0
  94. data/samples/standard/treeparser/LangDumpDecl.g +17 -0
  95. data/samples/standard/treeparser/input +1 -0
  96. data/samples/standard/treeparser/output +2 -0
  97. data/samples/standard/treeparser/treeparser.rb +18 -0
  98. data/samples/standard/tweak/Tweak.g +68 -0
  99. data/samples/standard/tweak/input +9 -0
  100. data/samples/standard/tweak/output +16 -0
  101. data/samples/standard/tweak/tweak.rb +13 -0
  102. data/samples/standard/xml/README +16 -0
  103. data/samples/standard/xml/XML.g +123 -0
  104. data/samples/standard/xml/input +21 -0
  105. data/samples/standard/xml/output +39 -0
  106. data/samples/standard/xml/xml.rb +9 -0
  107. data/templates/Ruby.stg +4 -4
  108. data/test/functional/ast-output/auto-ast.rb +0 -5
  109. data/test/functional/ast-output/rewrites.rb +4 -4
  110. data/test/unit/test-scope.rb +45 -0
  111. metadata +96 -8
@@ -0,0 +1,13 @@
1
+ import org.antlr.runtime.*;
2
+
3
+ public class Main {
4
+ public static void main(String[] args) throws Exception {
5
+ for (int i=0; i<args.length; i++) {
6
+ CharStream input = new ANTLRFileStream(args[i]);
7
+ FuzzyJava lex = new FuzzyJava(input);
8
+ TokenStream tokens = new CommonTokenStream(lex);
9
+ tokens.toString();
10
+ //System.out.println(tokens);
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,12 @@
1
+ found class Main
2
+ found method main
3
+ found var i
4
+ found var input
5
+ found call ANTLRFileStream
6
+ found var lex
7
+ found call FuzzyJava
8
+ found var tokens
9
+ found call CommonTokenStream
10
+ found call tokens.toString
11
+ found // comment //System.out.println(tokens);
12
+
@@ -0,0 +1,40 @@
1
+ /** Demonstrates how semantic predicates get hoisted out of the rule in
2
+ * which they are found and used in other decisions. This grammar illustrates
3
+ * how predicates can be used to distinguish between enum as a keyword and
4
+ * an ID *dynamically*. :)
5
+
6
+ * Run "java org.antlr.Tool -dfa t.g" to generate DOT (graphviz) files. See
7
+ * the T_dec-1.dot file to see the predicates in action.
8
+ */
9
+ grammar HoistedPredicates;
10
+ options { language = Ruby; }
11
+
12
+ /* With this true, enum is seen as a keyword. False, it's an identifier */
13
+ @init { @enable_enum = false }
14
+
15
+ stat: identifier { puts( "enum is an ID" ) }
16
+ | enum_as_keyword { puts( "enum is a keyword" ) }
17
+ ;
18
+
19
+ identifier
20
+ : ID
21
+ | enum_as_id
22
+ ;
23
+
24
+ enum_as_keyword : { @enable_enum }? 'enum' ;
25
+
26
+ enum_as_id : { ! @enableEnum }? 'enum' ;
27
+
28
+ ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
29
+ ;
30
+
31
+ INT : ('0'..'9')+
32
+ ;
33
+
34
+ WS : ( ' '
35
+ | '\t'
36
+ | '\r'
37
+ | '\n'
38
+ )+
39
+ {$channel=HIDDEN}
40
+ ;
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ $:.unshift( File.dirname( __FILE__ ) )
5
+ require 'HoistedPredicatesLexer'
6
+ require 'HoistedPredicatesParser'
7
+
8
+ for file in ARGV
9
+ input = ANTLR3::FileStream.new( file )
10
+ lexer = HoistedPredicates::Lexer.new( input )
11
+ parser = HoistedPredicates::Parser.new( lexer )
12
+ parser.stat
13
+ end
@@ -0,0 +1 @@
1
+ enum is an ID
@@ -0,0 +1,46 @@
1
+ grammar Javadoc;
2
+
3
+ options {
4
+ language = Ruby;
5
+ }
6
+
7
+ @lexer::members {
8
+ def error_header( e )
9
+ "#{ self.class }: #{ super }"
10
+ end
11
+ }
12
+
13
+ comment : ( author )* ;
14
+
15
+ author : '@author' ID { puts( "author #{ $ID.text }" ) };
16
+
17
+ ID : ('a'..'z'|'A'..'Z')+
18
+ ;
19
+
20
+ SIMPLE : '{'
21
+ {
22
+ puts( "enter embedded Simple escape" )
23
+ require 'SimpleLexer'
24
+ require 'SimpleParser'
25
+ lexer = Simple::Lexer.new( @input, :embedded => true )
26
+ parser = Simple::Parser.new( lexer )
27
+ parser.statement
28
+ $channel = HIDDEN
29
+ }
30
+ ;
31
+
32
+ /** When the javadoc parser sees end-of-comment it just says 'I'm done', which
33
+ * consumes the tokens and forces this javadoc parser (feeding
34
+ * off the input stream currently) to exit. It returns from
35
+ * method comment(), which was called from JAVADOC action in the
36
+ * Simple parser's lexer.
37
+ */
38
+ STOP : '*/'
39
+ {
40
+ $type = EOF
41
+ puts( "exit javadoc" )
42
+ }
43
+ ;
44
+
45
+ WS : (' '|'\t'|'\n')+ { $channel = HIDDEN }
46
+ ;
@@ -0,0 +1,104 @@
1
+ grammar Simple;
2
+
3
+ options { language = Ruby; }
4
+
5
+ @lexer::members {
6
+ def error_header( e )
7
+ "#{ self.class }: #{ super }"
8
+ end
9
+ }
10
+
11
+ @lexer::header {
12
+ JAVADOC_CHANNEL = 1
13
+ }
14
+
15
+ @lexer::init {
16
+ @embedded = options[ :embedded ]
17
+ @nesting = @embedded ? 1 : 0
18
+ }
19
+
20
+ /** This example is meant to illustrate how ANTLR can handle so-called "island
21
+ * grammars", which are just embedded languages. I chose a particularly
22
+ * nasty problem. A simple programming language with javadoc-style
23
+ * comments with the usual embedded @author tag but also with embedded
24
+ * Simple code actions in curlies such as {x=3}. Now that is a stupid
25
+ * thing maybe from a language design point of view, but is hard because it's
26
+ * a language (Simple) embedded within another language (JavaDoc) embedded
27
+ * within the innermost language (Simple) again. See the input file.
28
+ * So the Simple lexer invokes the javadoc lexer which invokes the Simple
29
+ * lexer again. The key seems to be returning an EOF token when you
30
+ * see the "final" token.
31
+ *
32
+ * This example is made nasty further by using valid characters of Simple
33
+ * (the curlies) to delimit it inside the Javadoc comments. The problem is
34
+ * that '}' may be a regular curly inside a Simple statement or it could
35
+ * be the signal that the embedded action is over. You must count the
36
+ * curly nesting level to decide if it's time to stop the embedded action.
37
+ * consider every "island grammar input chunk" such as javadoc or
38
+ * embedded Simple statements as a separate "file". So, when I hit the
39
+ * last delimiter token that says to bail out, I just return EOF. Then
40
+ * there is no need for an explicit stack of input streams.
41
+ *
42
+ * Finally, this grammar illustrates how to share input streams as all
43
+ * the grammars pull from the same input stream.
44
+ *
45
+ * A key point to notice is that since I create a new token stream when
46
+ * I go off to recognize javadoc comments, the lookahead for the Simple
47
+ * parser is not messed up in any way.
48
+ */
49
+ program : (variable)*
50
+ (method)+
51
+ ;
52
+ variable: 'int' ID ('=' expr)? ';'
53
+ ;
54
+ method : 'method' ID '(' ')' { puts( "enter method #{ $ID.text }" ) }
55
+ block
56
+ ;
57
+
58
+ block : LCURLY
59
+ (variable)*
60
+ (statement)+
61
+ RCURLY
62
+ ;
63
+
64
+ statement
65
+ : ID '=' expr ';' { puts( "assignment to #{ $ID.text }" ) }
66
+ | 'return' expr ';'
67
+ | block
68
+ ;
69
+
70
+ expr : ID
71
+ | INT
72
+ ;
73
+
74
+ ID : ('a'..'z'|'A'..'Z')+ ;
75
+ INT : ('0'..'9')+ ;
76
+ WS : (' '|'\t'|'\n')+ {$channel=HIDDEN}
77
+ ;
78
+ LCURLY : '{'
79
+ { @nesting += 1 }
80
+ ;
81
+ /** If we have a '}' with nesting level 0 then it must match the '{'
82
+ * (unseen by this grammar) that started an embedded Simple statement
83
+ * block within a javadoc comment.
84
+ */
85
+ RCURLY : '}'
86
+ {
87
+ @nesting -= 1
88
+ if @nesting <= 0
89
+ @embedded and $type = EOF
90
+ puts "exiting embedded simple"
91
+ end
92
+ }
93
+ ;
94
+
95
+ JAVADOC : '/**'
96
+ {
97
+ puts( "enter javadoc" )
98
+ require 'JavadocLexer'
99
+ require 'JavadocParser'
100
+ lexer = Javadoc::Lexer.new( @input )
101
+ Javadoc::Parser.new( lexer ).comment
102
+ $channel = JAVADOC_CHANNEL
103
+ }
104
+ ;
@@ -0,0 +1,11 @@
1
+ int i = 0;
2
+ /** @author foo {{z=3; q=4;}} {yy=33;}*/
3
+ method foo() {
4
+ int j = i;
5
+ i = 4;
6
+ }
7
+ /** @author bar
8
+ */
9
+ method zero() {
10
+ return 0;
11
+ }
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ $:.unshift( File.dirname( __FILE__ ) )
4
+ require 'SimpleLexer'
5
+ require 'SimpleParser'
6
+
7
+ for file in ARGV
8
+ input = ANTLR3::FileStream.new( file )
9
+ lexer = Simple::Lexer.new( input )
10
+ parser = Simple::Parser.new( lexer )
11
+ parser.program
12
+ end
@@ -0,0 +1,16 @@
1
+ enter javadoc
2
+ enter embedded Simple escape
3
+ exiting embedded simple
4
+ assignment to z
5
+ assignment to q
6
+ enter embedded Simple escape
7
+ exiting embedded simple
8
+ assignment to yy
9
+ exit javadoc
10
+ author foo
11
+ enter javadoc
12
+ exit javadoc
13
+ author bar
14
+ enter method foo
15
+ assignment to i
16
+ enter method zero
@@ -0,0 +1,827 @@
1
+ /** A Java 1.5 grammar for ANTLR v3 derived from the spec
2
+ *
3
+ * This is a very close representation of the spec; the changes
4
+ * are comestic (remove left recursion) and also fixes (the spec
5
+ * isn't exactly perfect). I have run this on the 1.4.2 source
6
+ * and some nasty looking enums from 1.5, but have not really
7
+ * tested for 1.5 compatibility.
8
+ *
9
+ * I built this with: java -Xmx100M org.antlr.Tool java.g
10
+ * and got two errors that are ok (for now):
11
+ * java.g:691:9: Decision can match input such as
12
+ * "'0'..'9'{'E', 'e'}{'+', '-'}'0'..'9'{'D', 'F', 'd', 'f'}"
13
+ * using multiple alternatives: 3, 4
14
+ * As a result, alternative(s) 4 were disabled for that input
15
+ * java.g:734:35: Decision can match input such as "{'$', 'A'..'Z',
16
+ * '_', 'a'..'z', '\u00C0'..'\u00D6', '\u00D8'..'\u00F6',
17
+ * '\u00F8'..'\u1FFF', '\u3040'..'\u318F', '\u3300'..'\u337F',
18
+ * '\u3400'..'\u3D2D', '\u4E00'..'\u9FFF', '\uF900'..'\uFAFF'}"
19
+ * using multiple alternatives: 1, 2
20
+ * As a result, alternative(s) 2 were disabled for that input
21
+ *
22
+ * You can turn enum on/off as a keyword :)
23
+ *
24
+ * Version 1.0 -- initial release July 5, 2006 (requires 3.0b2 or higher)
25
+ *
26
+ * Primary author: Terence Parr, July 2006
27
+ *
28
+ * Version 1.0.1 -- corrections by Koen Vanderkimpen & Marko van Dooren,
29
+ * October 25, 2006;
30
+ * fixed normalInterfaceDeclaration: now uses typeParameters instead
31
+ * of typeParameter (according to JLS, 3rd edition)
32
+ * fixed castExpression: no longer allows expression next to type
33
+ * (according to semantics in JLS, in contrast with syntax in JLS)
34
+ *
35
+ * Version 1.0.2 -- Terence Parr, Nov 27, 2006
36
+ * java spec I built this from had some bizarre for-loop control.
37
+ * Looked weird and so I looked elsewhere...Yep, it's messed up.
38
+ * simplified.
39
+ */
40
+ grammar Java;
41
+ options {
42
+ language=Ruby;
43
+ k=2;
44
+ backtrack=true;
45
+ memoize=true;
46
+ }
47
+
48
+ @lexer::init {
49
+ @enum_is_keyword = false
50
+ }
51
+
52
+ // starting point for parsing a java file
53
+ compilationUnit
54
+ : annotations?
55
+ packageDeclaration?
56
+ importDeclaration*
57
+ typeDeclaration*
58
+ ;
59
+
60
+ packageDeclaration
61
+ : 'package' qualifiedName ';'
62
+ ;
63
+
64
+ importDeclaration
65
+ : 'import' 'static'? Identifier ('.' Identifier)* ('.' '*')? ';'
66
+ ;
67
+
68
+ typeDeclaration
69
+ : classOrInterfaceDeclaration
70
+ | ';'
71
+ ;
72
+
73
+ classOrInterfaceDeclaration
74
+ : modifier* (classDeclaration | interfaceDeclaration)
75
+ ;
76
+
77
+ classDeclaration
78
+ : normalClassDeclaration
79
+ | enumDeclaration
80
+ ;
81
+
82
+ normalClassDeclaration
83
+ : 'class' Identifier (typeParameters)?
84
+ ('extends' type)?
85
+ ('implements' typeList)?
86
+ classBody
87
+ ;
88
+
89
+ typeParameters
90
+ : '<' typeParameter (',' typeParameter)* '>'
91
+ ;
92
+
93
+ typeParameter
94
+ : Identifier ('extends' bound)?
95
+ ;
96
+
97
+ bound
98
+ : type ('&' type)*
99
+ ;
100
+
101
+ enumDeclaration
102
+ : ENUM Identifier ('implements' typeList)? enumBody
103
+ ;
104
+
105
+ enumBody
106
+ : '{' enumConstants? ','? enumBodyDeclarations? '}'
107
+ ;
108
+
109
+ enumConstants
110
+ : enumConstant (',' enumConstant)*
111
+ ;
112
+
113
+ enumConstant
114
+ : annotations? Identifier (arguments)? (classBody)?
115
+ ;
116
+
117
+ enumBodyDeclarations
118
+ : ';' (classBodyDeclaration)*
119
+ ;
120
+
121
+ interfaceDeclaration
122
+ : normalInterfaceDeclaration
123
+ | annotationTypeDeclaration
124
+ ;
125
+
126
+ normalInterfaceDeclaration
127
+ : 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody
128
+ ;
129
+
130
+ typeList
131
+ : type (',' type)*
132
+ ;
133
+
134
+ classBody
135
+ : '{' classBodyDeclaration* '}'
136
+ ;
137
+
138
+ interfaceBody
139
+ : '{' interfaceBodyDeclaration* '}'
140
+ ;
141
+
142
+ classBodyDeclaration
143
+ : ';'
144
+ | 'static'? block
145
+ | modifier* memberDecl
146
+ ;
147
+
148
+ memberDecl
149
+ : genericMethodOrConstructorDecl
150
+ | methodDeclaration
151
+ | fieldDeclaration
152
+ | 'void' Identifier voidMethodDeclaratorRest
153
+ | Identifier constructorDeclaratorRest
154
+ | interfaceDeclaration
155
+ | classDeclaration
156
+ ;
157
+
158
+ genericMethodOrConstructorDecl
159
+ : typeParameters genericMethodOrConstructorRest
160
+ ;
161
+
162
+ genericMethodOrConstructorRest
163
+ : (type | 'void') Identifier methodDeclaratorRest
164
+ | Identifier constructorDeclaratorRest
165
+ ;
166
+
167
+ methodDeclaration
168
+ : type Identifier methodDeclaratorRest
169
+ ;
170
+
171
+ fieldDeclaration
172
+ : type variableDeclarators ';'
173
+ ;
174
+
175
+ interfaceBodyDeclaration
176
+ : modifier* interfaceMemberDecl
177
+ | ';'
178
+ ;
179
+
180
+ interfaceMemberDecl
181
+ : interfaceMethodOrFieldDecl
182
+ | interfaceGenericMethodDecl
183
+ | 'void' Identifier voidInterfaceMethodDeclaratorRest
184
+ | interfaceDeclaration
185
+ | classDeclaration
186
+ ;
187
+
188
+ interfaceMethodOrFieldDecl
189
+ : type Identifier interfaceMethodOrFieldRest
190
+ ;
191
+
192
+ interfaceMethodOrFieldRest
193
+ : constantDeclaratorsRest ';'
194
+ | interfaceMethodDeclaratorRest
195
+ ;
196
+
197
+ methodDeclaratorRest
198
+ : formalParameters ('[' ']')*
199
+ ('throws' qualifiedNameList)?
200
+ ( methodBody
201
+ | ';'
202
+ )
203
+ ;
204
+
205
+ voidMethodDeclaratorRest
206
+ : formalParameters ('throws' qualifiedNameList)?
207
+ ( methodBody
208
+ | ';'
209
+ )
210
+ ;
211
+
212
+ interfaceMethodDeclaratorRest
213
+ : formalParameters ('[' ']')* ('throws' qualifiedNameList)? ';'
214
+ ;
215
+
216
+ interfaceGenericMethodDecl
217
+ : typeParameters (type | 'void') Identifier
218
+ interfaceMethodDeclaratorRest
219
+ ;
220
+
221
+ voidInterfaceMethodDeclaratorRest
222
+ : formalParameters ('throws' qualifiedNameList)? ';'
223
+ ;
224
+
225
+ constructorDeclaratorRest
226
+ : formalParameters ('throws' qualifiedNameList)? methodBody
227
+ ;
228
+
229
+ constantDeclarator
230
+ : Identifier constantDeclaratorRest
231
+ ;
232
+
233
+ variableDeclarators
234
+ : variableDeclarator (',' variableDeclarator)*
235
+ ;
236
+
237
+ variableDeclarator
238
+ : Identifier variableDeclaratorRest
239
+ ;
240
+
241
+ variableDeclaratorRest
242
+ : ('[' ']')+ ('=' variableInitializer)?
243
+ | '=' variableInitializer
244
+ |
245
+ ;
246
+
247
+ constantDeclaratorsRest
248
+ : constantDeclaratorRest (',' constantDeclarator)*
249
+ ;
250
+
251
+ constantDeclaratorRest
252
+ : ('[' ']')* '=' variableInitializer
253
+ ;
254
+
255
+ variableDeclaratorId
256
+ : Identifier ('[' ']')*
257
+ ;
258
+
259
+ variableInitializer
260
+ : arrayInitializer
261
+ | expression
262
+ ;
263
+
264
+ arrayInitializer
265
+ : '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
266
+ ;
267
+
268
+ modifier
269
+ : annotation
270
+ | 'public'
271
+ | 'protected'
272
+ | 'private'
273
+ | 'static'
274
+ | 'abstract'
275
+ | 'final'
276
+ | 'native'
277
+ | 'synchronized'
278
+ | 'transient'
279
+ | 'volatile'
280
+ | 'strictfp'
281
+ ;
282
+
283
+ packageOrTypeName
284
+ : Identifier ('.' Identifier)*
285
+ ;
286
+
287
+ enumConstantName
288
+ : Identifier
289
+ ;
290
+
291
+ typeName
292
+ : Identifier
293
+ | packageOrTypeName '.' Identifier
294
+ ;
295
+
296
+ type
297
+ : Identifier (typeArguments)? ('.' Identifier (typeArguments)? )* ('[' ']')*
298
+ | primitiveType ('[' ']')*
299
+ ;
300
+
301
+ primitiveType
302
+ : 'boolean'
303
+ | 'char'
304
+ | 'byte'
305
+ | 'short'
306
+ | 'int'
307
+ | 'long'
308
+ | 'float'
309
+ | 'double'
310
+ ;
311
+
312
+ variableModifier
313
+ : 'final'
314
+ | annotation
315
+ ;
316
+
317
+ typeArguments
318
+ : '<' typeArgument (',' typeArgument)* '>'
319
+ ;
320
+
321
+ typeArgument
322
+ : type
323
+ | '?' (('extends' | 'super') type)?
324
+ ;
325
+
326
+ qualifiedNameList
327
+ : qualifiedName (',' qualifiedName)*
328
+ ;
329
+
330
+ formalParameters
331
+ : '(' formalParameterDecls? ')'
332
+ ;
333
+
334
+ formalParameterDecls
335
+ : 'final'? annotations? type formalParameterDeclsRest?
336
+ ;
337
+
338
+ formalParameterDeclsRest
339
+ : variableDeclaratorId (',' formalParameterDecls)?
340
+ | '...' variableDeclaratorId
341
+ ;
342
+
343
+ methodBody
344
+ : block
345
+ ;
346
+
347
+ qualifiedName
348
+ : Identifier ('.' Identifier)*
349
+ ;
350
+
351
+ literal
352
+ : integerLiteral
353
+ | FloatingPointLiteral
354
+ | CharacterLiteral
355
+ | StringLiteral
356
+ | booleanLiteral
357
+ | 'null'
358
+ ;
359
+
360
+ integerLiteral
361
+ : HexLiteral
362
+ | OctalLiteral
363
+ | DecimalLiteral
364
+ ;
365
+
366
+ booleanLiteral
367
+ : 'true'
368
+ | 'false'
369
+ ;
370
+
371
+ // ANNOTATIONS
372
+
373
+ annotations
374
+ : annotation+
375
+ ;
376
+
377
+ annotation
378
+ : '@' typeName ('(' (Identifier '=')? elementValue ')')?
379
+ ;
380
+
381
+ elementValue
382
+ : conditionalExpression
383
+ | annotation
384
+ | elementValueArrayInitializer
385
+ ;
386
+
387
+ elementValueArrayInitializer
388
+ : '{' (elementValue)? (',')? '}'
389
+ ;
390
+
391
+ annotationTypeDeclaration
392
+ : '@' 'interface' Identifier annotationTypeBody
393
+ ;
394
+
395
+ annotationTypeBody
396
+ : '{' (annotationTypeElementDeclarations)? '}'
397
+ ;
398
+
399
+ annotationTypeElementDeclarations
400
+ : (annotationTypeElementDeclaration) (annotationTypeElementDeclaration)*
401
+ ;
402
+
403
+ annotationTypeElementDeclaration
404
+ : (modifier)* annotationTypeElementRest
405
+ ;
406
+
407
+ annotationTypeElementRest
408
+ : type Identifier annotationMethodOrConstantRest ';'
409
+ | classDeclaration
410
+ | interfaceDeclaration
411
+ | enumDeclaration
412
+ | annotationTypeDeclaration
413
+ ;
414
+
415
+ annotationMethodOrConstantRest
416
+ : annotationMethodRest
417
+ | annotationConstantRest
418
+ ;
419
+
420
+ annotationMethodRest
421
+ : '(' ')' (defaultValue)?
422
+ ;
423
+
424
+ annotationConstantRest
425
+ : variableDeclarators
426
+ ;
427
+
428
+ defaultValue
429
+ : 'default' elementValue
430
+ ;
431
+
432
+ // STATEMENTS / BLOCKS
433
+
434
+ block
435
+ : '{' blockStatement* '}'
436
+ ;
437
+
438
+ blockStatement
439
+ : localVariableDeclaration
440
+ | classOrInterfaceDeclaration
441
+ | statement
442
+ ;
443
+
444
+ localVariableDeclaration
445
+ : ('final')? type variableDeclarators ';'
446
+ ;
447
+
448
+ statement
449
+ : block
450
+ | 'assert' expression (':' expression)? ';'
451
+ | 'if' parExpression statement ('else' statement)?
452
+ | 'for' '(' forControl ')' statement
453
+ | 'while' parExpression statement
454
+ | 'do' statement 'while' parExpression ';'
455
+ | 'try' block
456
+ ( catches 'finally' block
457
+ | catches
458
+ | 'finally' block
459
+ )
460
+ | 'switch' parExpression '{' switchBlockStatementGroups '}'
461
+ | 'synchronized' parExpression block
462
+ | 'return' expression? ';'
463
+ | 'throw' expression ';'
464
+ | 'break' Identifier? ';'
465
+ | 'continue' Identifier? ';'
466
+ | ';'
467
+ | statementExpression ';'
468
+ | Identifier ':' statement
469
+ ;
470
+
471
+ catches
472
+ : catchClause (catchClause)*
473
+ ;
474
+
475
+ catchClause
476
+ : 'catch' '(' formalParameter ')' block
477
+ ;
478
+
479
+ formalParameter
480
+ : variableModifier* type variableDeclaratorId
481
+ ;
482
+
483
+ switchBlockStatementGroups
484
+ : (switchBlockStatementGroup)*
485
+ ;
486
+
487
+ switchBlockStatementGroup
488
+ : switchLabel blockStatement*
489
+ ;
490
+
491
+ switchLabel
492
+ : 'case' constantExpression ':'
493
+ | 'case' enumConstantName ':'
494
+ | 'default' ':'
495
+ ;
496
+
497
+ moreStatementExpressions
498
+ : (',' statementExpression)*
499
+ ;
500
+
501
+ forControl
502
+ options {k=3;} // be efficient for common case: for (ID ID : ID) ...
503
+ : forVarControl
504
+ | forInit? ';' expression? ';' forUpdate?
505
+ ;
506
+
507
+ forInit
508
+ : 'final'? (annotation)? type variableDeclarators
509
+ | expressionList
510
+ ;
511
+
512
+ forVarControl
513
+ : 'final'? type Identifier ':' expression
514
+ ;
515
+
516
+ forUpdate
517
+ : expressionList
518
+ ;
519
+
520
+ // EXPRESSIONS
521
+
522
+ parExpression
523
+ : '(' expression ')'
524
+ ;
525
+
526
+ expressionList
527
+ : expression (',' expression)*
528
+ ;
529
+
530
+ statementExpression
531
+ : expression
532
+ ;
533
+
534
+ constantExpression
535
+ : expression
536
+ ;
537
+
538
+ expression
539
+ : conditionalExpression (assignmentOperator expression)?
540
+ ;
541
+
542
+ assignmentOperator
543
+ : '='
544
+ | '+='
545
+ | '-='
546
+ | '*='
547
+ | '/='
548
+ | '&='
549
+ | '|='
550
+ | '^='
551
+ | '%='
552
+ | '<' '<' '='
553
+ | '>' '>' '='
554
+ | '>' '>' '>' '='
555
+ ;
556
+
557
+ conditionalExpression
558
+ : conditionalOrExpression ( '?' expression ':' expression )?
559
+ ;
560
+
561
+ conditionalOrExpression
562
+ : conditionalAndExpression ( '||' conditionalAndExpression )*
563
+ ;
564
+
565
+ conditionalAndExpression
566
+ : inclusiveOrExpression ( '&&' inclusiveOrExpression )*
567
+ ;
568
+
569
+ inclusiveOrExpression
570
+ : exclusiveOrExpression ( '|' exclusiveOrExpression )*
571
+ ;
572
+
573
+ exclusiveOrExpression
574
+ : andExpression ( '^' andExpression )*
575
+ ;
576
+
577
+ andExpression
578
+ : equalityExpression ( '&' equalityExpression )*
579
+ ;
580
+
581
+ equalityExpression
582
+ : instanceOfExpression ( ('==' | '!=') instanceOfExpression )*
583
+ ;
584
+
585
+ instanceOfExpression
586
+ : relationalExpression ('instanceof' type)?
587
+ ;
588
+
589
+ relationalExpression
590
+ : shiftExpression ( relationalOp shiftExpression )*
591
+ ;
592
+
593
+ relationalOp
594
+ : ('<' '=' | '>' '=' | '<' | '>')
595
+ ;
596
+
597
+ shiftExpression
598
+ : additiveExpression ( shiftOp additiveExpression )*
599
+ ;
600
+
601
+ // TODO: need a sem pred to check column on these >>>
602
+ shiftOp
603
+ : ('<' '<' | '>' '>' '>' | '>' '>')
604
+ ;
605
+
606
+
607
+ additiveExpression
608
+ : multiplicativeExpression ( ('+' | '-') multiplicativeExpression )*
609
+ ;
610
+
611
+ multiplicativeExpression
612
+ : unaryExpression ( ( '*' | '/' | '%' ) unaryExpression )*
613
+ ;
614
+
615
+ unaryExpression
616
+ : '+' unaryExpression
617
+ | '-' unaryExpression
618
+ | '++' primary
619
+ | '--' primary
620
+ | unaryExpressionNotPlusMinus
621
+ ;
622
+
623
+ unaryExpressionNotPlusMinus
624
+ : '~' unaryExpression
625
+ | '!' unaryExpression
626
+ | castExpression
627
+ | primary selector* ('++'|'--')?
628
+ ;
629
+
630
+ castExpression
631
+ : '(' primitiveType ')' unaryExpression
632
+ | '(' (expression | type) ')' unaryExpressionNotPlusMinus
633
+ ;
634
+
635
+ primary
636
+ : parExpression
637
+ | nonWildcardTypeArguments
638
+ (explicitGenericInvocationSuffix | 'this' arguments)
639
+ | 'this' (arguments)?
640
+ | 'super' superSuffix
641
+ | literal
642
+ | 'new' creator
643
+ | Identifier ('.' Identifier)* (identifierSuffix)?
644
+ | primitiveType ('[' ']')* '.' 'class'
645
+ | 'void' '.' 'class'
646
+ ;
647
+
648
+ identifierSuffix
649
+ : ('[' ']')+ '.' 'class'
650
+ | ('[' expression ']')+ // can also be matched by selector, but do here
651
+ | arguments
652
+ | '.' 'class'
653
+ | '.' explicitGenericInvocation
654
+ | '.' 'this'
655
+ | '.' 'super' arguments
656
+ | '.' 'new' (nonWildcardTypeArguments)? innerCreator
657
+ ;
658
+
659
+ creator
660
+ : nonWildcardTypeArguments? createdName
661
+ (arrayCreatorRest | classCreatorRest)
662
+ ;
663
+
664
+ createdName
665
+ : Identifier nonWildcardTypeArguments?
666
+ ('.' Identifier nonWildcardTypeArguments?)*
667
+ | primitiveType
668
+ ;
669
+
670
+ innerCreator
671
+ : Identifier classCreatorRest
672
+ ;
673
+
674
+ arrayCreatorRest
675
+ : '['
676
+ ( ']' ('[' ']')* arrayInitializer
677
+ | expression ']' ('[' expression ']')* ('[' ']')*
678
+ )
679
+ ;
680
+
681
+ classCreatorRest
682
+ : arguments classBody?
683
+ ;
684
+
685
+ explicitGenericInvocation
686
+ : nonWildcardTypeArguments explicitGenericInvocationSuffix
687
+ ;
688
+
689
+ nonWildcardTypeArguments
690
+ : '<' typeList '>'
691
+ ;
692
+
693
+ explicitGenericInvocationSuffix
694
+ : 'super' superSuffix
695
+ | Identifier arguments
696
+ ;
697
+
698
+ selector
699
+ : '.' Identifier (arguments)?
700
+ | '.' 'this'
701
+ | '.' 'super' superSuffix
702
+ | '.' 'new' (nonWildcardTypeArguments)? innerCreator
703
+ | '[' expression ']'
704
+ ;
705
+
706
+ superSuffix
707
+ : arguments
708
+ | '.' Identifier (arguments)?
709
+ ;
710
+
711
+ arguments
712
+ : '(' expressionList? ')'
713
+ ;
714
+
715
+ // LEXER
716
+
717
+ HexLiteral : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
718
+
719
+ DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
720
+
721
+ OctalLiteral : '0' ('0'..'7')+ IntegerTypeSuffix? ;
722
+
723
+ fragment
724
+ HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
725
+
726
+ fragment
727
+ IntegerTypeSuffix : ('l'|'L') ;
728
+
729
+ FloatingPointLiteral
730
+ : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
731
+ | '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
732
+ | ('0'..'9')+ Exponent FloatTypeSuffix?
733
+ | ('0'..'9')+ Exponent? FloatTypeSuffix
734
+ ;
735
+
736
+ fragment
737
+ Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
738
+
739
+ fragment
740
+ FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
741
+
742
+ CharacterLiteral
743
+ : '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
744
+ ;
745
+
746
+ StringLiteral
747
+ : '"' ( EscapeSequence | ~('\\'|'"') )* '"'
748
+ ;
749
+
750
+ fragment
751
+ EscapeSequence
752
+ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
753
+ | UnicodeEscape
754
+ | OctalEscape
755
+ ;
756
+
757
+ fragment
758
+ OctalEscape
759
+ : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
760
+ | '\\' ('0'..'7') ('0'..'7')
761
+ | '\\' ('0'..'7')
762
+ ;
763
+
764
+ fragment
765
+ UnicodeEscape
766
+ : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
767
+ ;
768
+
769
+ ENUM: 'enum'
770
+ {
771
+ @enum_is_keyword or $type = Identifier
772
+ }
773
+ ;
774
+
775
+ Identifier
776
+ : Letter (Letter|JavaIDDigit)*
777
+ ;
778
+
779
+ /**I found this char range in JavaCC's grammar, but Letter and Digit overlap.
780
+ Still works, but...
781
+ */
782
+ fragment
783
+ Letter
784
+ : '\u0024' |
785
+ '\u0041'..'\u005a' |
786
+ '\u005f' |
787
+ '\u0061'..'\u007a' |
788
+ '\u00c0'..'\u00d6' |
789
+ '\u00d8'..'\u00f6' |
790
+ '\u00f8'..'\u00ff' |
791
+ '\u0100'..'\u1fff' |
792
+ '\u3040'..'\u318f' |
793
+ '\u3300'..'\u337f' |
794
+ '\u3400'..'\u3d2d' |
795
+ '\u4e00'..'\u9fff' |
796
+ '\uf900'..'\ufaff'
797
+ ;
798
+
799
+ fragment
800
+ JavaIDDigit
801
+ : '\u0030'..'\u0039' |
802
+ '\u0660'..'\u0669' |
803
+ '\u06f0'..'\u06f9' |
804
+ '\u0966'..'\u096f' |
805
+ '\u09e6'..'\u09ef' |
806
+ '\u0a66'..'\u0a6f' |
807
+ '\u0ae6'..'\u0aef' |
808
+ '\u0b66'..'\u0b6f' |
809
+ '\u0be7'..'\u0bef' |
810
+ '\u0c66'..'\u0c6f' |
811
+ '\u0ce6'..'\u0cef' |
812
+ '\u0d66'..'\u0d6f' |
813
+ '\u0e50'..'\u0e59' |
814
+ '\u0ed0'..'\u0ed9' |
815
+ '\u1040'..'\u1049'
816
+ ;
817
+
818
+ WS : (' '|'\r'|'\t'|'\u000C'|'\n') { $channel=HIDDEN }
819
+ ;
820
+
821
+ COMMENT
822
+ : '/*' ( options {greedy=false;} : . )* '*/' { $channel=HIDDEN }
823
+ ;
824
+
825
+ LINE_COMMENT
826
+ : '//' ~('\n'|'\r')* '\r'? '\n' { $channel=HIDDEN }
827
+ ;