antlr3 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ANTLR-LICENSE.txt +26 -0
- data/History.txt +66 -0
- data/README.txt +139 -0
- data/bin/antlr4ruby +33 -0
- data/java/RubyTarget.java +524 -0
- data/java/antlr-full-3.2.1.jar +0 -0
- data/lib/antlr3.rb +176 -0
- data/lib/antlr3/constants.rb +88 -0
- data/lib/antlr3/debug.rb +701 -0
- data/lib/antlr3/debug/event-hub.rb +210 -0
- data/lib/antlr3/debug/record-event-listener.rb +25 -0
- data/lib/antlr3/debug/rule-tracer.rb +55 -0
- data/lib/antlr3/debug/socket.rb +360 -0
- data/lib/antlr3/debug/trace-event-listener.rb +92 -0
- data/lib/antlr3/dfa.rb +247 -0
- data/lib/antlr3/dot.rb +174 -0
- data/lib/antlr3/error.rb +657 -0
- data/lib/antlr3/main.rb +561 -0
- data/lib/antlr3/modes/ast-builder.rb +41 -0
- data/lib/antlr3/modes/filter.rb +56 -0
- data/lib/antlr3/profile.rb +322 -0
- data/lib/antlr3/recognizers.rb +1280 -0
- data/lib/antlr3/streams.rb +985 -0
- data/lib/antlr3/streams/interactive.rb +91 -0
- data/lib/antlr3/streams/rewrite.rb +412 -0
- data/lib/antlr3/test/call-stack.rb +57 -0
- data/lib/antlr3/test/config.rb +23 -0
- data/lib/antlr3/test/core-extensions.rb +269 -0
- data/lib/antlr3/test/diff.rb +165 -0
- data/lib/antlr3/test/functional.rb +207 -0
- data/lib/antlr3/test/grammar.rb +371 -0
- data/lib/antlr3/token.rb +592 -0
- data/lib/antlr3/tree.rb +1415 -0
- data/lib/antlr3/tree/debug.rb +163 -0
- data/lib/antlr3/tree/visitor.rb +84 -0
- data/lib/antlr3/tree/wizard.rb +481 -0
- data/lib/antlr3/util.rb +149 -0
- data/lib/antlr3/version.rb +27 -0
- data/samples/ANTLRv3Grammar.g +621 -0
- data/samples/Cpp.g +749 -0
- data/templates/AST.stg +335 -0
- data/templates/ASTDbg.stg +40 -0
- data/templates/ASTParser.stg +153 -0
- data/templates/ASTTreeParser.stg +272 -0
- data/templates/Dbg.stg +192 -0
- data/templates/Ruby.stg +1514 -0
- data/test/functional/ast-output/auto-ast.rb +797 -0
- data/test/functional/ast-output/construction.rb +555 -0
- data/test/functional/ast-output/hetero-nodes.rb +753 -0
- data/test/functional/ast-output/rewrites.rb +1327 -0
- data/test/functional/ast-output/tree-rewrite.rb +1662 -0
- data/test/functional/debugging/debug-mode.rb +689 -0
- data/test/functional/debugging/profile-mode.rb +165 -0
- data/test/functional/debugging/rule-tracing.rb +74 -0
- data/test/functional/delegation/import.rb +379 -0
- data/test/functional/lexer/basic.rb +559 -0
- data/test/functional/lexer/filter-mode.rb +245 -0
- data/test/functional/lexer/nuances.rb +47 -0
- data/test/functional/lexer/properties.rb +104 -0
- data/test/functional/lexer/syn-pred.rb +32 -0
- data/test/functional/lexer/xml.rb +206 -0
- data/test/functional/main/main-scripts.rb +245 -0
- data/test/functional/parser/actions.rb +224 -0
- data/test/functional/parser/backtracking.rb +244 -0
- data/test/functional/parser/basic.rb +282 -0
- data/test/functional/parser/calc.rb +98 -0
- data/test/functional/parser/ll-star.rb +143 -0
- data/test/functional/parser/nuances.rb +165 -0
- data/test/functional/parser/predicates.rb +103 -0
- data/test/functional/parser/properties.rb +242 -0
- data/test/functional/parser/rule-methods.rb +132 -0
- data/test/functional/parser/scopes.rb +274 -0
- data/test/functional/token-rewrite/basic.rb +318 -0
- data/test/functional/token-rewrite/via-parser.rb +100 -0
- data/test/functional/tree-parser/basic.rb +750 -0
- data/test/unit/sample-input/file-stream-1 +2 -0
- data/test/unit/sample-input/teststreams.input2 +2 -0
- data/test/unit/test-dfa.rb +52 -0
- data/test/unit/test-exceptions.rb +44 -0
- data/test/unit/test-recognizers.rb +55 -0
- data/test/unit/test-scheme.rb +62 -0
- data/test/unit/test-streams.rb +459 -0
- data/test/unit/test-tree-wizard.rb +535 -0
- data/test/unit/test-trees.rb +854 -0
- metadata +205 -0
@@ -0,0 +1,753 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'antlr3/test/functional'
|
5
|
+
|
6
|
+
|
7
|
+
class TestHeterogeneousNodeTypes < ANTLR3::Test::Functional
|
8
|
+
|
9
|
+
inline_grammar(<<-'END')
|
10
|
+
grammar VToken;
|
11
|
+
options {
|
12
|
+
language=Ruby;
|
13
|
+
output=AST;
|
14
|
+
}
|
15
|
+
@members {
|
16
|
+
class V < ANTLR3::CommonTree
|
17
|
+
def to_s
|
18
|
+
return @token.text + "<V>"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
}
|
22
|
+
a : ID<V> ;
|
23
|
+
ID : 'a'..'z'+ ;
|
24
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
25
|
+
END
|
26
|
+
|
27
|
+
|
28
|
+
inline_grammar(<<-'END')
|
29
|
+
grammar TokenWithQualifiedType;
|
30
|
+
options {
|
31
|
+
language=Ruby;
|
32
|
+
output=AST;
|
33
|
+
}
|
34
|
+
@members {
|
35
|
+
class V < ANTLR3::CommonTree
|
36
|
+
def to_s
|
37
|
+
return @token.text + "<V>"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
}
|
41
|
+
a : ID<TokenWithQualifiedType.Parser.V> ;
|
42
|
+
ID : 'a'..'z'+ ;
|
43
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
44
|
+
END
|
45
|
+
|
46
|
+
|
47
|
+
inline_grammar(<<-'END')
|
48
|
+
grammar TokenWithLabel;
|
49
|
+
options {
|
50
|
+
language=Ruby;
|
51
|
+
output=AST;
|
52
|
+
}
|
53
|
+
@members {
|
54
|
+
class V < ANTLR3::CommonTree
|
55
|
+
def to_s
|
56
|
+
return @token.text + "<V>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
}
|
60
|
+
a : x=ID<V> ;
|
61
|
+
ID : 'a'..'z'+ ;
|
62
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
63
|
+
END
|
64
|
+
|
65
|
+
|
66
|
+
inline_grammar(<<-'END')
|
67
|
+
grammar TokenWithListLabel;
|
68
|
+
options {
|
69
|
+
language=Ruby;
|
70
|
+
output=AST;
|
71
|
+
}
|
72
|
+
@members {
|
73
|
+
class V < ANTLR3::CommonTree
|
74
|
+
def to_s
|
75
|
+
return @token.text + "<V>"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
}
|
79
|
+
a : x+=ID<V> ;
|
80
|
+
ID : 'a'..'z'+ ;
|
81
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
82
|
+
END
|
83
|
+
|
84
|
+
|
85
|
+
inline_grammar(<<-'END')
|
86
|
+
grammar TokenRoot;
|
87
|
+
options {
|
88
|
+
language=Ruby;
|
89
|
+
output=AST;
|
90
|
+
}
|
91
|
+
@members {
|
92
|
+
class V < ANTLR3::CommonTree
|
93
|
+
def to_s
|
94
|
+
return @token.text + "<V>"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
}
|
99
|
+
a : ID<V>^ ;
|
100
|
+
ID : 'a'..'z'+ ;
|
101
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
102
|
+
END
|
103
|
+
|
104
|
+
|
105
|
+
inline_grammar(<<-'END')
|
106
|
+
grammar TokenRootWithListLabel;
|
107
|
+
options {
|
108
|
+
language=Ruby;
|
109
|
+
output=AST;
|
110
|
+
}
|
111
|
+
@members {
|
112
|
+
class V < ANTLR3::CommonTree
|
113
|
+
def to_s
|
114
|
+
return @token.text + "<V>"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
}
|
119
|
+
a : x+=ID<V>^ ;
|
120
|
+
ID : 'a'..'z'+ ;
|
121
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
122
|
+
END
|
123
|
+
|
124
|
+
|
125
|
+
inline_grammar(<<-'END')
|
126
|
+
grammar FromString;
|
127
|
+
options {
|
128
|
+
language=Ruby;
|
129
|
+
output=AST;
|
130
|
+
}
|
131
|
+
@members {
|
132
|
+
class V < ANTLR3::CommonTree
|
133
|
+
def to_s
|
134
|
+
return @token.text + "<V>"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
}
|
139
|
+
a : 'begin'<V> ;
|
140
|
+
ID : 'a'..'z'+ ;
|
141
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
142
|
+
END
|
143
|
+
|
144
|
+
|
145
|
+
inline_grammar(<<-'END')
|
146
|
+
grammar StringRoot;
|
147
|
+
options {
|
148
|
+
language=Ruby;
|
149
|
+
output=AST;
|
150
|
+
}
|
151
|
+
@members {
|
152
|
+
class V < ANTLR3::CommonTree
|
153
|
+
def to_s
|
154
|
+
return @token.text + "<V>"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
}
|
159
|
+
a : 'begin'<V>^ ;
|
160
|
+
ID : 'a'..'z'+ ;
|
161
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
162
|
+
END
|
163
|
+
|
164
|
+
|
165
|
+
inline_grammar(<<-'END')
|
166
|
+
grammar RewriteToken;
|
167
|
+
options {
|
168
|
+
language=Ruby;
|
169
|
+
output=AST;
|
170
|
+
}
|
171
|
+
@members {
|
172
|
+
class V < ANTLR3::CommonTree
|
173
|
+
def to_s
|
174
|
+
return @token.text + "<V>"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
}
|
179
|
+
a : ID -> ID<V> ;
|
180
|
+
ID : 'a'..'z'+ ;
|
181
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
182
|
+
END
|
183
|
+
|
184
|
+
|
185
|
+
inline_grammar(<<-'END')
|
186
|
+
grammar RewriteTokenWithArgs;
|
187
|
+
options {
|
188
|
+
language=Ruby;
|
189
|
+
output=AST;
|
190
|
+
}
|
191
|
+
@members {
|
192
|
+
class V < ANTLR3::CommonTree
|
193
|
+
def initialize(*args)
|
194
|
+
case args.length
|
195
|
+
when 4
|
196
|
+
ttype, @x, @y, @z = args
|
197
|
+
token = ANTLR3::CommonToken.new(ttype, nil, '')
|
198
|
+
when 3
|
199
|
+
ttype, token, @x = args
|
200
|
+
@y = @z = 0
|
201
|
+
else raise ArgumentError, "invalid number of arguments: #{args.length} for 3-4"
|
202
|
+
end
|
203
|
+
super(token)
|
204
|
+
end
|
205
|
+
def to_s
|
206
|
+
(@token.text.to_s rescue '') << "<V>;\%d\%d\%d" % [@x, @y, @z]
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
}
|
211
|
+
a : ID -> ID<V>[42,19,30] ID<V>[$ID,99];
|
212
|
+
ID : 'a'..'z'+ ;
|
213
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
214
|
+
END
|
215
|
+
|
216
|
+
|
217
|
+
inline_grammar(<<-'END')
|
218
|
+
grammar RewriteTokenRoot;
|
219
|
+
options {
|
220
|
+
language=Ruby;
|
221
|
+
output=AST;
|
222
|
+
}
|
223
|
+
@members {
|
224
|
+
class V < ANTLR3::CommonTree
|
225
|
+
def to_s
|
226
|
+
return @token.text + "<V>"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
}
|
231
|
+
a : ID INT -> ^(ID<V> INT) ;
|
232
|
+
ID : 'a'..'z'+ ;
|
233
|
+
INT : '0'..'9'+ ;
|
234
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
235
|
+
END
|
236
|
+
|
237
|
+
|
238
|
+
inline_grammar(<<-'END')
|
239
|
+
grammar RewriteString;
|
240
|
+
options {
|
241
|
+
language=Ruby;
|
242
|
+
output=AST;
|
243
|
+
}
|
244
|
+
@members {
|
245
|
+
class V < ANTLR3::CommonTree
|
246
|
+
def to_s
|
247
|
+
return @token.text + "<V>"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
}
|
252
|
+
a : 'begin' -> 'begin'<V> ;
|
253
|
+
ID : 'a'..'z'+ ;
|
254
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
255
|
+
END
|
256
|
+
|
257
|
+
|
258
|
+
inline_grammar(<<-'END')
|
259
|
+
grammar RewriteStringRoot;
|
260
|
+
options {
|
261
|
+
language=Ruby;
|
262
|
+
output=AST;
|
263
|
+
}
|
264
|
+
@members {
|
265
|
+
class V < ANTLR3::CommonTree
|
266
|
+
def to_s
|
267
|
+
return @token.text + "<V>"
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
}
|
272
|
+
a : 'begin' INT -> ^('begin'<V> INT) ;
|
273
|
+
ID : 'a'..'z'+ ;
|
274
|
+
INT : '0'..'9'+ ;
|
275
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
276
|
+
END
|
277
|
+
|
278
|
+
|
279
|
+
inline_grammar(<<-'END')
|
280
|
+
grammar RewriteRuleResults;
|
281
|
+
options {
|
282
|
+
language=Ruby;
|
283
|
+
output=AST;
|
284
|
+
}
|
285
|
+
tokens {LIST;}
|
286
|
+
@members {
|
287
|
+
class V < ANTLR3::CommonTree
|
288
|
+
def to_s
|
289
|
+
return @token.text + "<V>"
|
290
|
+
end
|
291
|
+
end
|
292
|
+
class W < ANTLR3::CommonTree
|
293
|
+
def initialize(tok, text)
|
294
|
+
tok.text = text
|
295
|
+
super(tok)
|
296
|
+
end
|
297
|
+
def to_s
|
298
|
+
return @token.text + "<W>"
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
}
|
303
|
+
a : id (',' id)* -> ^(LIST<W>["LIST"] id+);
|
304
|
+
id : ID -> ID<V>;
|
305
|
+
ID : 'a'..'z'+ ;
|
306
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
307
|
+
END
|
308
|
+
|
309
|
+
|
310
|
+
inline_grammar(<<-'END')
|
311
|
+
grammar CopySemanticsWithHetero;
|
312
|
+
options {
|
313
|
+
language=Ruby;
|
314
|
+
output=AST;
|
315
|
+
}
|
316
|
+
@members {
|
317
|
+
class V < ANTLR3::CommonTree
|
318
|
+
def dup_node
|
319
|
+
return V.new(self)
|
320
|
+
end
|
321
|
+
def to_s
|
322
|
+
return @token.text + "<V>"
|
323
|
+
end
|
324
|
+
end
|
325
|
+
}
|
326
|
+
a : type ID (',' ID)* ';' -> ^(type ID)+;
|
327
|
+
type : 'int'<V> ;
|
328
|
+
ID : 'a'..'z'+ ;
|
329
|
+
INT : '0'..'9'+;
|
330
|
+
WS : (' '|'\\n') {$channel=HIDDEN;} ;
|
331
|
+
END
|
332
|
+
|
333
|
+
|
334
|
+
inline_grammar(<<-'END')
|
335
|
+
grammar TreeParserRewriteFlatList;
|
336
|
+
options {
|
337
|
+
language=Ruby;
|
338
|
+
output=AST;
|
339
|
+
}
|
340
|
+
a : ID INT;
|
341
|
+
ID : 'a'..'z'+ ;
|
342
|
+
INT : '0'..'9'+;
|
343
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
344
|
+
END
|
345
|
+
|
346
|
+
|
347
|
+
inline_grammar(<<-'END')
|
348
|
+
tree grammar TreeParserRewriteFlatListWalker;
|
349
|
+
options {
|
350
|
+
language=Ruby;
|
351
|
+
output=AST;
|
352
|
+
ASTLabelType=CommonTree;
|
353
|
+
tokenVocab=TreeParserRewriteFlatList;
|
354
|
+
}
|
355
|
+
@members {
|
356
|
+
class V < ANTLR3::CommonTree
|
357
|
+
def to_s
|
358
|
+
return @token.text + "<V>"
|
359
|
+
end
|
360
|
+
end
|
361
|
+
class W < ANTLR3::CommonTree
|
362
|
+
def to_s
|
363
|
+
return @token.text + "<W>"
|
364
|
+
end
|
365
|
+
end
|
366
|
+
}
|
367
|
+
a : ID INT -> INT<V> ID<W>
|
368
|
+
;
|
369
|
+
END
|
370
|
+
|
371
|
+
|
372
|
+
inline_grammar(<<-'END')
|
373
|
+
grammar TreeParserRewriteTree;
|
374
|
+
options {
|
375
|
+
language=Ruby;
|
376
|
+
output=AST;
|
377
|
+
}
|
378
|
+
a : ID INT;
|
379
|
+
ID : 'a'..'z'+ ;
|
380
|
+
INT : '0'..'9'+;
|
381
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
382
|
+
END
|
383
|
+
|
384
|
+
|
385
|
+
inline_grammar(<<-'END')
|
386
|
+
tree grammar TreeParserRewriteTreeWalker;
|
387
|
+
options {
|
388
|
+
language=Ruby;
|
389
|
+
output=AST;
|
390
|
+
ASTLabelType=CommonTree;
|
391
|
+
tokenVocab=TreeParserRewriteTree;
|
392
|
+
}
|
393
|
+
@members {
|
394
|
+
class V < ANTLR3::CommonTree
|
395
|
+
def to_s
|
396
|
+
return @token.text + "<V>"
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
class W < ANTLR3::CommonTree
|
401
|
+
def to_s
|
402
|
+
return @token.text + "<W>"
|
403
|
+
end
|
404
|
+
end
|
405
|
+
}
|
406
|
+
a : ID INT -> ^(INT<V> ID<W>)
|
407
|
+
;
|
408
|
+
END
|
409
|
+
|
410
|
+
|
411
|
+
inline_grammar(<<-'END')
|
412
|
+
grammar TreeParserRewriteImaginary;
|
413
|
+
options {
|
414
|
+
language=Ruby;
|
415
|
+
output=AST;
|
416
|
+
}
|
417
|
+
a : ID ;
|
418
|
+
ID : 'a'..'z'+ ;
|
419
|
+
INT : '0'..'9'+;
|
420
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
421
|
+
END
|
422
|
+
|
423
|
+
|
424
|
+
inline_grammar(<<-'END')
|
425
|
+
tree grammar TreeParserRewriteImaginaryWalker;
|
426
|
+
options {
|
427
|
+
language=Ruby;
|
428
|
+
output=AST;
|
429
|
+
ASTLabelType=CommonTree;
|
430
|
+
tokenVocab=TreeParserRewriteImaginary;
|
431
|
+
}
|
432
|
+
tokens { ROOT; }
|
433
|
+
@members {
|
434
|
+
class V < ANTLR3::CommonTree
|
435
|
+
def to_s
|
436
|
+
return @token.name + "<V>"
|
437
|
+
end
|
438
|
+
end
|
439
|
+
}
|
440
|
+
a : ID -> ROOT<V> ID
|
441
|
+
;
|
442
|
+
END
|
443
|
+
|
444
|
+
|
445
|
+
inline_grammar(<<-'END')
|
446
|
+
grammar TreeParserRewriteImaginaryWithArgs;
|
447
|
+
options {
|
448
|
+
language=Ruby;
|
449
|
+
output=AST;
|
450
|
+
}
|
451
|
+
a : ID ;
|
452
|
+
ID : 'a'..'z'+ ;
|
453
|
+
INT : '0'..'9'+;
|
454
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
455
|
+
END
|
456
|
+
|
457
|
+
|
458
|
+
inline_grammar(<<-'END')
|
459
|
+
tree grammar TreeParserRewriteImaginaryWithArgsWalker;
|
460
|
+
options {
|
461
|
+
language=Ruby;
|
462
|
+
output=AST;
|
463
|
+
ASTLabelType=CommonTree;
|
464
|
+
tokenVocab=TreeParserRewriteImaginaryWithArgs;
|
465
|
+
}
|
466
|
+
tokens { ROOT; }
|
467
|
+
@members {
|
468
|
+
class V < ANTLR3::CommonTree
|
469
|
+
def initialize(token_type, x)
|
470
|
+
super(token_type)
|
471
|
+
@x = x
|
472
|
+
end
|
473
|
+
def to_s
|
474
|
+
return @token.name + "<V>;#@x"
|
475
|
+
end
|
476
|
+
end
|
477
|
+
}
|
478
|
+
a : ID -> ROOT<V>[42] ID
|
479
|
+
;
|
480
|
+
END
|
481
|
+
|
482
|
+
|
483
|
+
inline_grammar(<<-'END')
|
484
|
+
grammar TreeParserRewriteImaginaryRoot;
|
485
|
+
options {
|
486
|
+
language=Ruby;
|
487
|
+
output=AST;
|
488
|
+
}
|
489
|
+
a : ID ;
|
490
|
+
ID : 'a'..'z'+ ;
|
491
|
+
INT : '0'..'9'+;
|
492
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
493
|
+
END
|
494
|
+
|
495
|
+
|
496
|
+
inline_grammar(<<-'END')
|
497
|
+
tree grammar TreeParserRewriteImaginaryRootWalker;
|
498
|
+
options {
|
499
|
+
language=Ruby;
|
500
|
+
output=AST;
|
501
|
+
ASTLabelType=CommonTree;
|
502
|
+
tokenVocab=TreeParserRewriteImaginaryRoot;
|
503
|
+
}
|
504
|
+
tokens { ROOT; }
|
505
|
+
@members {
|
506
|
+
class V < ANTLR3::CommonTree
|
507
|
+
def to_s
|
508
|
+
return @token.name + "<V>"
|
509
|
+
end
|
510
|
+
end
|
511
|
+
}
|
512
|
+
a : ID -> ^(ROOT<V> ID)
|
513
|
+
;
|
514
|
+
END
|
515
|
+
|
516
|
+
|
517
|
+
inline_grammar(<<-'END')
|
518
|
+
grammar TreeParserRewriteImaginaryFromReal;
|
519
|
+
options {
|
520
|
+
language=Ruby;
|
521
|
+
output=AST;
|
522
|
+
}
|
523
|
+
a : ID ;
|
524
|
+
ID : 'a'..'z'+ ;
|
525
|
+
INT : '0'..'9'+;
|
526
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
527
|
+
END
|
528
|
+
|
529
|
+
|
530
|
+
inline_grammar(<<-'END')
|
531
|
+
tree grammar TreeParserRewriteImaginaryFromRealWalker;
|
532
|
+
options {
|
533
|
+
language=Ruby;
|
534
|
+
output=AST;
|
535
|
+
ASTLabelType=CommonTree;
|
536
|
+
tokenVocab=TreeParserRewriteImaginaryFromReal;
|
537
|
+
}
|
538
|
+
tokens { ROOT; }
|
539
|
+
@members {
|
540
|
+
class V < ANTLR3::CommonTree
|
541
|
+
def initialize(token, tree = nil)
|
542
|
+
if tree.nil? then super(token)
|
543
|
+
else
|
544
|
+
super(tree)
|
545
|
+
@token = TokenData::Token.from_token(@token)
|
546
|
+
@token.type = token.type
|
547
|
+
end
|
548
|
+
end
|
549
|
+
def to_s
|
550
|
+
return @token.name + "<V>@" + @token.line.to_s
|
551
|
+
end
|
552
|
+
end
|
553
|
+
}
|
554
|
+
a : ID -> ROOT<V>[$ID]
|
555
|
+
;
|
556
|
+
END
|
557
|
+
|
558
|
+
|
559
|
+
inline_grammar(<<-'END')
|
560
|
+
grammar TreeParserAutoHeteroAST;
|
561
|
+
options {
|
562
|
+
language=Ruby;
|
563
|
+
output=AST;
|
564
|
+
}
|
565
|
+
a : ID ';' ;
|
566
|
+
ID : 'a'..'z'+ ;
|
567
|
+
INT : '0'..'9'+;
|
568
|
+
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
569
|
+
END
|
570
|
+
|
571
|
+
|
572
|
+
inline_grammar(<<-'END')
|
573
|
+
tree grammar TreeParserAutoHeteroASTWalker;
|
574
|
+
options {
|
575
|
+
language=Ruby;
|
576
|
+
output=AST;
|
577
|
+
ASTLabelType=CommonTree;
|
578
|
+
tokenVocab=TreeParserAutoHeteroAST;
|
579
|
+
}
|
580
|
+
tokens { ROOT; }
|
581
|
+
@members {
|
582
|
+
class V < ANTLR3::CommonTree
|
583
|
+
def to_s
|
584
|
+
return @token.text + "<V>"
|
585
|
+
end
|
586
|
+
end
|
587
|
+
}
|
588
|
+
|
589
|
+
a : ID<V> ';'<V>;
|
590
|
+
END
|
591
|
+
|
592
|
+
def parse(grammar_name, grammar_rule, input)
|
593
|
+
grammar_module = self.class.const_get(grammar_name.to_s)
|
594
|
+
lexer = grammar_module::Lexer.new( input )
|
595
|
+
tokens = ANTLR3::CommonTokenStream.new( lexer )
|
596
|
+
parser = grammar_module::Parser.new( tokens )
|
597
|
+
r = parser.send(grammar_rule)
|
598
|
+
|
599
|
+
return(r.tree.inspect rescue '')
|
600
|
+
end
|
601
|
+
|
602
|
+
def tree_parse(grammar_name, grammar_rule, tree_grammar_rule, input)
|
603
|
+
grammar_module = self.class.const_get(grammar_name.to_s)
|
604
|
+
tree_grammar_module = self.class.const_get(grammar_name.to_s + 'Walker')
|
605
|
+
|
606
|
+
lexer = grammar_module::Lexer.new( input )
|
607
|
+
tokens = ANTLR3::CommonTokenStream.new( lexer )
|
608
|
+
parser = grammar_module::Parser.new( tokens )
|
609
|
+
r = parser.send(grammar_rule)
|
610
|
+
|
611
|
+
nodes = ANTLR3::CommonTreeNodeStream.new(r.tree)
|
612
|
+
nodes.token_stream = tokens
|
613
|
+
tree_parser = tree_grammar_module::TreeParser.new(nodes)
|
614
|
+
r = tree_parser.send(tree_grammar_rule)
|
615
|
+
|
616
|
+
return(r.tree.inspect rescue '')
|
617
|
+
end
|
618
|
+
|
619
|
+
example "v token" do
|
620
|
+
result = parse(:VToken, :a, 'a')
|
621
|
+
result.should == 'a<V>'
|
622
|
+
end
|
623
|
+
|
624
|
+
|
625
|
+
example "token with qualified type" do
|
626
|
+
result = parse(:TokenWithQualifiedType, :a, 'a')
|
627
|
+
result.should == 'a<V>'
|
628
|
+
end
|
629
|
+
|
630
|
+
|
631
|
+
example "token with label" do
|
632
|
+
result = parse(:TokenWithLabel, :a, 'a')
|
633
|
+
result.should == 'a<V>'
|
634
|
+
end
|
635
|
+
|
636
|
+
|
637
|
+
example "token with list label" do
|
638
|
+
result = parse(:TokenWithListLabel, :a, 'a')
|
639
|
+
result.should == 'a<V>'
|
640
|
+
end
|
641
|
+
|
642
|
+
|
643
|
+
example "token root" do
|
644
|
+
result = parse(:TokenRoot, :a, 'a')
|
645
|
+
result.should == 'a<V>'
|
646
|
+
end
|
647
|
+
|
648
|
+
|
649
|
+
example "token root with list label" do
|
650
|
+
result = parse(:TokenRootWithListLabel, :a, 'a')
|
651
|
+
result.should == 'a<V>'
|
652
|
+
end
|
653
|
+
|
654
|
+
|
655
|
+
example "string" do
|
656
|
+
result = parse(:FromString, :a, 'begin')
|
657
|
+
result.should == 'begin<V>'
|
658
|
+
end
|
659
|
+
|
660
|
+
|
661
|
+
example "string root" do
|
662
|
+
result = parse(:StringRoot, :a, 'begin')
|
663
|
+
result.should == 'begin<V>'
|
664
|
+
end
|
665
|
+
|
666
|
+
|
667
|
+
example "rewrite token" do
|
668
|
+
result = parse(:RewriteToken, :a, 'a')
|
669
|
+
result.should == 'a<V>'
|
670
|
+
end
|
671
|
+
|
672
|
+
|
673
|
+
example "rewrite token with args" do
|
674
|
+
result = parse(:RewriteTokenWithArgs, :a, 'a')
|
675
|
+
result.should == '<V>;421930 a<V>;9900'
|
676
|
+
end
|
677
|
+
|
678
|
+
|
679
|
+
example "rewrite token root" do
|
680
|
+
result = parse(:RewriteTokenRoot, :a, 'a 2')
|
681
|
+
result.should == '(a<V> 2)'
|
682
|
+
end
|
683
|
+
|
684
|
+
|
685
|
+
example "rewrite string" do
|
686
|
+
result = parse(:RewriteString, :a, 'begin')
|
687
|
+
result.should == 'begin<V>'
|
688
|
+
end
|
689
|
+
|
690
|
+
|
691
|
+
example "rewrite string root" do
|
692
|
+
result = parse(:RewriteStringRoot, :a, 'begin 2')
|
693
|
+
result.should == '(begin<V> 2)'
|
694
|
+
end
|
695
|
+
|
696
|
+
|
697
|
+
example "rewrite rule results" do
|
698
|
+
result = parse(:RewriteRuleResults, :a, 'a,b,c')
|
699
|
+
result.should == '(LIST<W> a<V> b<V> c<V>)'
|
700
|
+
end
|
701
|
+
|
702
|
+
|
703
|
+
example "copy semantics with hetero" do
|
704
|
+
result = parse(:CopySemanticsWithHetero, :a, 'int a, b, c;')
|
705
|
+
result.should == '(int<V> a) (int<V> b) (int<V> c)'
|
706
|
+
end
|
707
|
+
|
708
|
+
|
709
|
+
example "tree parser rewrite flat list" do
|
710
|
+
result = tree_parse(:TreeParserRewriteFlatList, :a, :a, 'abc 34')
|
711
|
+
result.should == '34<V> abc<W>'
|
712
|
+
end
|
713
|
+
|
714
|
+
|
715
|
+
example "tree parser rewrite tree" do
|
716
|
+
result = tree_parse(:TreeParserRewriteTree, :a, :a, 'abc 34')
|
717
|
+
result.should == '(34<V> abc<W>)'
|
718
|
+
end
|
719
|
+
|
720
|
+
|
721
|
+
example "tree parser rewrite imaginary" do
|
722
|
+
result = tree_parse(:TreeParserRewriteImaginary, :a, :a, 'abc')
|
723
|
+
result.should == 'ROOT<V> abc'
|
724
|
+
end
|
725
|
+
|
726
|
+
|
727
|
+
example "tree parser rewrite imaginary with args" do
|
728
|
+
result = tree_parse(:TreeParserRewriteImaginaryWithArgs, :a, :a, 'abc')
|
729
|
+
result.should == 'ROOT<V>;42 abc'
|
730
|
+
end
|
731
|
+
|
732
|
+
|
733
|
+
example "tree parser rewrite imaginary root" do
|
734
|
+
result = tree_parse(:TreeParserRewriteImaginaryRoot, :a, :a, 'abc')
|
735
|
+
result.should == '(ROOT<V> abc)'
|
736
|
+
end
|
737
|
+
|
738
|
+
|
739
|
+
example "tree parser rewrite imaginary from real" do
|
740
|
+
result = tree_parse(:TreeParserRewriteImaginaryFromReal, :a, :a, 'abc')
|
741
|
+
result.should == 'ROOT<V>@1'
|
742
|
+
end
|
743
|
+
|
744
|
+
|
745
|
+
example "tree parser auto hetero ast" do
|
746
|
+
result = tree_parse(:TreeParserAutoHeteroAST, :a, :a, 'abc;')
|
747
|
+
result.should == 'abc<V> ;<V>'
|
748
|
+
end
|
749
|
+
|
750
|
+
end
|
751
|
+
|
752
|
+
|
753
|
+
|