antlr3 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +35 -0
- data/Manifest.txt +73 -0
- data/README.txt +6 -13
- data/java/RubyTarget.java +43 -19
- data/java/antlr-full-3.2.1.jar +0 -0
- data/lib/antlr3/debug.rb +2 -0
- data/lib/antlr3/debug/event-hub.rb +55 -55
- data/lib/antlr3/debug/record-event-listener.rb +2 -2
- data/lib/antlr3/debug/rule-tracer.rb +14 -14
- data/lib/antlr3/debug/socket.rb +47 -47
- data/lib/antlr3/debug/trace-event-listener.rb +8 -8
- data/lib/antlr3/main.rb +29 -9
- data/lib/antlr3/modes/ast-builder.rb +7 -7
- data/lib/antlr3/modes/filter.rb +19 -17
- data/lib/antlr3/profile.rb +34 -6
- data/lib/antlr3/recognizers.rb +50 -1
- data/lib/antlr3/streams.rb +19 -15
- data/lib/antlr3/streams/rewrite.rb +241 -229
- data/lib/antlr3/template/group-file-lexer.rb +6 -8
- data/lib/antlr3/template/group-file-parser.rb +16 -16
- data/lib/antlr3/template/group-file.rb +1 -1
- data/lib/antlr3/test/call-stack.rb +13 -13
- data/lib/antlr3/test/core-extensions.rb +69 -69
- data/lib/antlr3/test/functional.rb +0 -4
- data/lib/antlr3/test/grammar.rb +70 -70
- data/lib/antlr3/token.rb +41 -17
- data/lib/antlr3/tree.rb +11 -14
- data/lib/antlr3/tree/debug.rb +53 -53
- data/lib/antlr3/tree/visitor.rb +11 -11
- data/lib/antlr3/tree/wizard.rb +35 -35
- data/lib/antlr3/util.rb +18 -0
- data/lib/antlr3/version.rb +1 -1
- data/rakefile +1 -0
- data/samples/ANTLRv3Grammar.g +3 -3
- data/samples/JavaScript.g +702 -0
- data/samples/standard/C/C.g +543 -0
- data/samples/standard/C/C.tokens +175 -0
- data/samples/standard/C/C__testrig.st +0 -0
- data/samples/standard/C/c.rb +12 -0
- data/samples/standard/C/input +3479 -0
- data/samples/standard/C/output +171 -0
- data/samples/standard/LL-star/LLStar.g +101 -0
- data/samples/standard/LL-star/input +12 -0
- data/samples/standard/LL-star/ll-star.rb +12 -0
- data/samples/standard/LL-star/output +2 -0
- data/samples/standard/calc/Calculator.g +47 -0
- data/samples/standard/calc/Calculator.py +16 -0
- data/samples/standard/calc/Calculator.rb +28 -0
- data/samples/standard/cminus/CMinus.g +141 -0
- data/samples/standard/cminus/bytecode.group +80 -0
- data/samples/standard/cminus/cminus.rb +16 -0
- data/samples/standard/cminus/input +9 -0
- data/samples/standard/cminus/java.group +91 -0
- data/samples/standard/cminus/output +11 -0
- data/samples/standard/cminus/python.group +48 -0
- data/samples/standard/dynamic-scope/DynamicScopes.g +50 -0
- data/samples/standard/dynamic-scope/dynamic-scopes.rb +12 -0
- data/samples/standard/dynamic-scope/input +7 -0
- data/samples/standard/dynamic-scope/output +4 -0
- data/samples/standard/fuzzy/FuzzyJava.g +89 -0
- data/samples/standard/fuzzy/fuzzy.py +11 -0
- data/samples/standard/fuzzy/fuzzy.rb +9 -0
- data/samples/standard/fuzzy/input +13 -0
- data/samples/standard/fuzzy/output +12 -0
- data/samples/standard/hoisted-predicates/HoistedPredicates.g +40 -0
- data/samples/standard/hoisted-predicates/hoisted-predicates.rb +13 -0
- data/samples/standard/hoisted-predicates/input +1 -0
- data/samples/standard/hoisted-predicates/output +1 -0
- data/samples/standard/island-grammar/Javadoc.g +46 -0
- data/samples/standard/island-grammar/Simple.g +104 -0
- data/samples/standard/island-grammar/input +11 -0
- data/samples/standard/island-grammar/island.rb +12 -0
- data/samples/standard/island-grammar/output +16 -0
- data/samples/standard/java/Java.g +827 -0
- data/samples/standard/java/input +80 -0
- data/samples/standard/java/java.rb +13 -0
- data/samples/standard/java/output +1 -0
- data/samples/standard/python/Python.g +718 -0
- data/samples/standard/python/PythonTokenSource.rb +107 -0
- data/samples/standard/python/input +210 -0
- data/samples/standard/python/output +24 -0
- data/samples/standard/python/python.rb +14 -0
- data/samples/standard/rakefile +18 -0
- data/samples/standard/scopes/SymbolTable.g +66 -0
- data/samples/standard/scopes/input +12 -0
- data/samples/standard/scopes/output +3 -0
- data/samples/standard/scopes/scopes.rb +12 -0
- data/samples/standard/simplecTreeParser/SimpleC.g +113 -0
- data/samples/standard/simplecTreeParser/SimpleCWalker.g +64 -0
- data/samples/standard/simplecTreeParser/input +12 -0
- data/samples/standard/simplecTreeParser/output +1 -0
- data/samples/standard/simplecTreeParser/simplec.rb +18 -0
- data/samples/standard/treeparser/Lang.g +24 -0
- data/samples/standard/treeparser/LangDumpDecl.g +17 -0
- data/samples/standard/treeparser/input +1 -0
- data/samples/standard/treeparser/output +2 -0
- data/samples/standard/treeparser/treeparser.rb +18 -0
- data/samples/standard/tweak/Tweak.g +68 -0
- data/samples/standard/tweak/input +9 -0
- data/samples/standard/tweak/output +16 -0
- data/samples/standard/tweak/tweak.rb +13 -0
- data/samples/standard/xml/README +16 -0
- data/samples/standard/xml/XML.g +123 -0
- data/samples/standard/xml/input +21 -0
- data/samples/standard/xml/output +39 -0
- data/samples/standard/xml/xml.rb +9 -0
- data/templates/Ruby.stg +4 -4
- data/test/functional/ast-output/auto-ast.rb +0 -5
- data/test/functional/ast-output/rewrites.rb +4 -4
- data/test/unit/test-scope.rb +45 -0
- metadata +96 -8
|
@@ -16,10 +16,10 @@ class RecordEventListener < TraceEventListener
|
|
|
16
16
|
@events = []
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
def record(event_message, *interpolation_arguments)
|
|
19
|
+
def record( event_message, *interpolation_arguments )
|
|
20
20
|
event_message = event_message.to_s
|
|
21
21
|
@events << event_message % interpolation_arguments
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
end # module Debug
|
|
25
|
-
end # module ANTLR3
|
|
25
|
+
end # module ANTLR3
|
|
@@ -18,38 +18,38 @@ class RuleTracer
|
|
|
18
18
|
attr_reader :level
|
|
19
19
|
attr_accessor :spaces_per_indent, :device
|
|
20
20
|
|
|
21
|
-
def initialize(options = {})
|
|
22
|
-
@input = options[:input]
|
|
21
|
+
def initialize( options = {} )
|
|
22
|
+
@input = options[ :input ]
|
|
23
23
|
@level = 0
|
|
24
|
-
@spaces_per_indent = options[:spaces_per_indent] || 2
|
|
25
|
-
@device = options[:device] || options[:output] || $stderr
|
|
24
|
+
@spaces_per_indent = options[ :spaces_per_indent ] || 2
|
|
25
|
+
@device = options[ :device ] || options[ :output ] || $stderr
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def enter_rule(grammar_file, name)
|
|
28
|
+
def enter_rule( grammar_file, name )
|
|
29
29
|
indent = @level * @spaces_per_indent
|
|
30
30
|
|
|
31
|
-
@device.print(' ' * indent, ARROW_IN, name)
|
|
31
|
+
@device.print( ' ' * indent, ARROW_IN, name )
|
|
32
32
|
if @input
|
|
33
33
|
input_symbol = @input.look || :EOF
|
|
34
|
-
@device.puts(" look = %p" % input_symbol)
|
|
35
|
-
else @device.print("\n")
|
|
34
|
+
@device.puts( " look = %p" % input_symbol )
|
|
35
|
+
else @device.print( "\n" )
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
@level += 1
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
def exit_rule(grammar_file, name)
|
|
41
|
+
def exit_rule( grammar_file, name )
|
|
42
42
|
@level -= 1
|
|
43
43
|
|
|
44
44
|
indent = @level * @spaces_per_indent
|
|
45
45
|
|
|
46
|
-
@device.print(' ' * indent, ARROW_OUT, name)
|
|
46
|
+
@device.print( ' ' * indent, ARROW_OUT, name )
|
|
47
47
|
if @input
|
|
48
|
-
input_symbol = (@input.look || :EOF)
|
|
49
|
-
@device.puts(" look = %p" % input_symbol)
|
|
50
|
-
else @device.print("\n")
|
|
48
|
+
input_symbol = ( @input.look || :EOF )
|
|
49
|
+
@device.puts( " look = %p" % input_symbol )
|
|
50
|
+
else @device.print( "\n" )
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
|
-
end
|
|
55
|
+
end
|
data/lib/antlr3/debug/socket.rb
CHANGED
|
@@ -21,43 +21,43 @@ class EventSocketProxy
|
|
|
21
21
|
|
|
22
22
|
SOCKET_ADDR_PACK = 'snCCCCa8'.freeze
|
|
23
23
|
|
|
24
|
-
def initialize(recognizer, options = {})
|
|
24
|
+
def initialize( recognizer, options = {} )
|
|
25
25
|
super()
|
|
26
26
|
@grammar_file_name = recognizer.grammar_file_name
|
|
27
|
-
@adaptor = options[:adaptor]
|
|
28
|
-
@port = options[:port] || DEFAULT_PORT
|
|
29
|
-
@log = options[:log]
|
|
27
|
+
@adaptor = options[ :adaptor ]
|
|
28
|
+
@port = options[ :port ] || DEFAULT_PORT
|
|
29
|
+
@log = options[ :log ]
|
|
30
30
|
@socket = nil
|
|
31
31
|
@connection = nil
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
def log!(message, *interpolation_arguments)
|
|
35
|
-
@log and @log.printf(message, *interpolation_arguments)
|
|
34
|
+
def log!( message, *interpolation_arguments )
|
|
35
|
+
@log and @log.printf( message, *interpolation_arguments )
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def handshake
|
|
39
39
|
unless @socket
|
|
40
40
|
begin
|
|
41
|
-
@socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
41
|
+
@socket = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
|
42
42
|
@socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
|
|
43
|
-
@socket.bind(Socket.pack_sockaddr_in( @port, '' ))
|
|
44
|
-
@socket.listen(1)
|
|
45
|
-
log!("waiting for incoming connection on port %i\n", @port)
|
|
43
|
+
@socket.bind( Socket.pack_sockaddr_in( @port, '' ) )
|
|
44
|
+
@socket.listen( 1 )
|
|
45
|
+
log!( "waiting for incoming connection on port %i\n", @port )
|
|
46
46
|
|
|
47
47
|
@connection, addr = @socket.accept
|
|
48
|
-
port, host = Socket.unpack_sockaddr_in(addr)
|
|
49
|
-
log!("Accepted connection from %s:%s\n", host, port)
|
|
48
|
+
port, host = Socket.unpack_sockaddr_in( addr )
|
|
49
|
+
log!( "Accepted connection from %s:%s\n", host, port )
|
|
50
50
|
|
|
51
|
-
@connection.setsockopt(Socket::SOL_TCP, Socket::TCP_NODELAY, 1)
|
|
51
|
+
@connection.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
|
52
52
|
|
|
53
|
-
write('ANTLR %s', PROTOCOL_VERSION)
|
|
54
|
-
write('grammar %p', @grammar_file_name)
|
|
53
|
+
write( 'ANTLR %s', PROTOCOL_VERSION )
|
|
54
|
+
write( 'grammar %p', @grammar_file_name )
|
|
55
55
|
ack
|
|
56
56
|
rescue IOError => error
|
|
57
|
-
log!("handshake failed due to an IOError:\n")
|
|
58
|
-
log!(" %s: %s", error.class, error.message)
|
|
59
|
-
log!(" Backtrace: ")
|
|
60
|
-
log!(" - %s", error.backtrace.join("\n - "))
|
|
57
|
+
log!( "handshake failed due to an IOError:\n" )
|
|
58
|
+
log!( " %s: %s", error.class, error.message )
|
|
59
|
+
log!( " Backtrace: " )
|
|
60
|
+
log!( " - %s", error.backtrace.join( "\n - " ) )
|
|
61
61
|
@connection and @connection.close
|
|
62
62
|
@socket and @socket.close
|
|
63
63
|
@socket = nil
|
|
@@ -67,21 +67,21 @@ class EventSocketProxy
|
|
|
67
67
|
return self
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
def write(message, *interpolation_arguments)
|
|
70
|
+
def write( message, *interpolation_arguments )
|
|
71
71
|
message << ?\n
|
|
72
|
-
log!("---> #{message}", *interpolation_arguments)
|
|
73
|
-
@connection.printf(message, *interpolation_arguments)
|
|
72
|
+
log!( "---> #{ message }", *interpolation_arguments )
|
|
73
|
+
@connection.printf( message, *interpolation_arguments )
|
|
74
74
|
@connection.flush
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
def ack
|
|
78
78
|
line = @connection.readline
|
|
79
|
-
log!("<--- %s", line)
|
|
79
|
+
log!( "<--- %s", line )
|
|
80
80
|
line
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
-
def transmit(event, *interpolation_arguments)
|
|
84
|
-
write(event, *interpolation_arguments)
|
|
83
|
+
def transmit( event, *interpolation_arguments )
|
|
84
|
+
write( event, *interpolation_arguments )
|
|
85
85
|
ack()
|
|
86
86
|
rescue IOError
|
|
87
87
|
@connection.close
|
|
@@ -134,21 +134,21 @@ class EventSocketProxy
|
|
|
134
134
|
transmit "%s\t%s", :consume_hidden_token, serialize_token( token )
|
|
135
135
|
end
|
|
136
136
|
|
|
137
|
-
def look(i, item)
|
|
137
|
+
def look( i, item )
|
|
138
138
|
case item
|
|
139
139
|
when AST::Tree
|
|
140
140
|
look_tree( i, item )
|
|
141
141
|
when nil
|
|
142
142
|
else
|
|
143
|
-
transmit "%s\t%i\t%s", :look, i, serialize_token(item)
|
|
143
|
+
transmit "%s\t%i\t%s", :look, i, serialize_token( item )
|
|
144
144
|
end
|
|
145
145
|
end
|
|
146
146
|
|
|
147
|
-
def mark(i)
|
|
147
|
+
def mark( i )
|
|
148
148
|
transmit "%s\t%i", :mark, i
|
|
149
149
|
end
|
|
150
150
|
|
|
151
|
-
def rewind(i = nil)
|
|
151
|
+
def rewind( i = nil )
|
|
152
152
|
i ? transmit( "%s\t%i", :rewind, i ) : transmit( '%s', :rewind )
|
|
153
153
|
end
|
|
154
154
|
|
|
@@ -156,7 +156,7 @@ class EventSocketProxy
|
|
|
156
156
|
transmit "%s\t%i", :begin_backtrack, level
|
|
157
157
|
end
|
|
158
158
|
def end_backtrack( level, successful )
|
|
159
|
-
transmit "%s\t%i\t%p", :end_backtrack, level, (successful ? true : false)
|
|
159
|
+
transmit "%s\t%i\t%p", :end_backtrack, level, ( successful ? true : false )
|
|
160
160
|
end
|
|
161
161
|
|
|
162
162
|
def location( line, position )
|
|
@@ -177,7 +177,7 @@ class EventSocketProxy
|
|
|
177
177
|
end
|
|
178
178
|
|
|
179
179
|
def semantic_predicate( result, predicate )
|
|
180
|
-
pure_boolean = !(!result)
|
|
180
|
+
pure_boolean = !( !result )
|
|
181
181
|
transmit "%s\t%s\t%s", :semantic_predicate, pure_boolean, escape_newlines( predicate )
|
|
182
182
|
end
|
|
183
183
|
|
|
@@ -230,9 +230,9 @@ class EventSocketProxy
|
|
|
230
230
|
attr_accessor :adaptor
|
|
231
231
|
|
|
232
232
|
def serialize_token( token )
|
|
233
|
-
[token.token_index, token.type, token.channel,
|
|
233
|
+
[ token.token_index, token.type, token.channel,
|
|
234
234
|
token.line, token.column,
|
|
235
|
-
escape_newlines( token.text )].join( "\t" )
|
|
235
|
+
escape_newlines( token.text ) ].join( "\t" )
|
|
236
236
|
end
|
|
237
237
|
|
|
238
238
|
def serialize_node( node )
|
|
@@ -249,7 +249,7 @@ class EventSocketProxy
|
|
|
249
249
|
|
|
250
250
|
def escape_newlines( text )
|
|
251
251
|
text.inspect.tap do |t|
|
|
252
|
-
t.gsub!(/%/, '%%')
|
|
252
|
+
t.gsub!( /%/, '%%' )
|
|
253
253
|
end
|
|
254
254
|
end
|
|
255
255
|
end
|
|
@@ -263,7 +263,7 @@ over an IP socket.
|
|
|
263
263
|
class RemoteEventSocketListener < ::Thread
|
|
264
264
|
autoload :StringIO, 'stringio'
|
|
265
265
|
ESCAPE_MAP = Hash.new { |h, k| k }
|
|
266
|
-
ESCAPE_MAP.update(
|
|
266
|
+
ESCAPE_MAP.update(
|
|
267
267
|
?n => ?\n, ?t => ?\t, ?a => ?\a, ?b => ?\b, ?e => ?\e,
|
|
268
268
|
?f => ?\f, ?r => ?\r, ?v => ?\v
|
|
269
269
|
)
|
|
@@ -288,33 +288,33 @@ class RemoteEventSocketListener < ::Thread
|
|
|
288
288
|
private
|
|
289
289
|
|
|
290
290
|
def handshake
|
|
291
|
-
@version = @socket.readline.split("\t")[-1]
|
|
292
|
-
@grammar_file = @socket.readline.split("\t")[-1]
|
|
291
|
+
@version = @socket.readline.split( "\t" )[ -1 ]
|
|
292
|
+
@grammar_file = @socket.readline.split( "\t" )[ -1 ]
|
|
293
293
|
ack
|
|
294
294
|
end
|
|
295
295
|
|
|
296
296
|
def ack
|
|
297
|
-
@socket.puts("ack")
|
|
297
|
+
@socket.puts( "ack" )
|
|
298
298
|
@socket.flush
|
|
299
299
|
end
|
|
300
300
|
|
|
301
|
-
def unpack_event(event)
|
|
301
|
+
def unpack_event( event )
|
|
302
302
|
event.nil? and raise( StopIteration )
|
|
303
303
|
event.chomp!
|
|
304
|
-
name, *elements = event.split("\t",-1)
|
|
304
|
+
name, *elements = event.split( "\t",-1 )
|
|
305
305
|
name = name.to_sym
|
|
306
306
|
name == :terminate and raise StopIteration
|
|
307
307
|
elements.map! do |elem|
|
|
308
|
-
elem.empty? and next(nil)
|
|
308
|
+
elem.empty? and next( nil )
|
|
309
309
|
case elem
|
|
310
|
-
when /^\d+$/ then Integer(elem)
|
|
311
|
-
when /^\d+\.\d+$/ then Float(elem)
|
|
310
|
+
when /^\d+$/ then Integer( elem )
|
|
311
|
+
when /^\d+\.\d+$/ then Float( elem )
|
|
312
312
|
when /^true$/ then true
|
|
313
313
|
when /^false$/ then false
|
|
314
|
-
when /^"(.*)"$/ then parse_string($1)
|
|
314
|
+
when /^"(.*)"$/ then parse_string( $1 )
|
|
315
315
|
end
|
|
316
316
|
end
|
|
317
|
-
elements.unshift(name)
|
|
317
|
+
elements.unshift( name )
|
|
318
318
|
return( elements )
|
|
319
319
|
end
|
|
320
320
|
|
|
@@ -325,7 +325,7 @@ private
|
|
|
325
325
|
end
|
|
326
326
|
|
|
327
327
|
def connect
|
|
328
|
-
TCPSocket.open(@host, @port) do |socket|
|
|
328
|
+
TCPSocket.open( @host, @port ) do |socket|
|
|
329
329
|
@socket = socket
|
|
330
330
|
yield
|
|
331
331
|
end
|
|
@@ -357,4 +357,4 @@ private
|
|
|
357
357
|
|
|
358
358
|
end # class RemoteEventSocketListener
|
|
359
359
|
end # module Debug
|
|
360
|
-
end # module ANTLR3
|
|
360
|
+
end # module ANTLR3
|
|
@@ -14,15 +14,15 @@ stdout) to record the events in a different way.
|
|
|
14
14
|
class TraceEventListener
|
|
15
15
|
include EventListener
|
|
16
16
|
|
|
17
|
-
def initialize(adaptor = nil, device = $stderr)
|
|
17
|
+
def initialize( adaptor = nil, device = $stderr )
|
|
18
18
|
super()
|
|
19
19
|
@device = device
|
|
20
20
|
@adaptor = adaptor ||= ANTLR3::AST::CommonTreeAdaptor.new
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
def record(event_message, *interpolation_arguments)
|
|
23
|
+
def record( event_message, *interpolation_arguments )
|
|
24
24
|
event_message = event_message.to_s << "\n"
|
|
25
|
-
@device.printf(event_message, *interpolation_arguments)
|
|
25
|
+
@device.printf( event_message, *interpolation_arguments )
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def enter_alternative( alt_number )
|
|
@@ -50,11 +50,11 @@ class TraceEventListener
|
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
def consume_node( tree )
|
|
53
|
-
record '(%s) unique_id=%s text=%p type=%s[%s]', __method__, @adaptor.unique_id(tree),
|
|
54
|
-
@adaptor.text_of(tree), @adaptor.type_name( tree ), @adaptor.type_of(tree)
|
|
53
|
+
record '(%s) unique_id=%s text=%p type=%s[%s]', __method__, @adaptor.unique_id( tree ),
|
|
54
|
+
@adaptor.text_of( tree ), @adaptor.type_name( tree ), @adaptor.type_of( tree )
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
def look(i, tree)
|
|
57
|
+
def look( i, tree )
|
|
58
58
|
record '(%s): k=%s unique_id=%s text=%p type=%s[%s]', __method__, i, @adaptor.unique_id( tree ),
|
|
59
59
|
@adaptor.text_of( tree ), @adaptor.type_name( tree ), @adaptor.type_of( tree )
|
|
60
60
|
end
|
|
@@ -63,7 +63,7 @@ class TraceEventListener
|
|
|
63
63
|
record '(%s): unique_id=%s', __method__, @adaptor.unique_id( tree )
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
-
def create_node(tree, token = nil)
|
|
66
|
+
def create_node( tree, token = nil )
|
|
67
67
|
unless token
|
|
68
68
|
record '(%s): unique_id=%s text=%p type=%s[%s]', __method__, @adaptor.unique_id( tree ),
|
|
69
69
|
@adaptor.text_of( tree ), @adaptor.type_name( tree ), @adaptor.type_of( tree )
|
|
@@ -89,4 +89,4 @@ class TraceEventListener
|
|
|
89
89
|
end
|
|
90
90
|
end # class TraceEventListener
|
|
91
91
|
end # module Debug
|
|
92
|
-
end # module ANTLR3
|
|
92
|
+
end # module ANTLR3
|
data/lib/antlr3/main.rb
CHANGED
|
@@ -378,6 +378,7 @@ class ParserMain < Main
|
|
|
378
378
|
@parser_class = parser_class
|
|
379
379
|
@parser_rule = options[ :parser_rule ]
|
|
380
380
|
if @debug = ( @parser_class.debug? rescue false )
|
|
381
|
+
@trace = options.fetch( :trace, nil )
|
|
381
382
|
@port = options.fetch( :port, ANTLR3::Debug::DEFAULT_PORT )
|
|
382
383
|
@log = options.fetch( :log, @error )
|
|
383
384
|
end
|
|
@@ -408,6 +409,10 @@ class ParserMain < Main
|
|
|
408
409
|
opt.separator ''
|
|
409
410
|
opt.separator "Debug Mode Options:"
|
|
410
411
|
|
|
412
|
+
opt.on( '--trace', '-t', "print rule trace instead of opening a debug socket" ) do
|
|
413
|
+
@trace = true
|
|
414
|
+
end
|
|
415
|
+
|
|
411
416
|
opt.on( '--port NUMBER', Integer, "port number to use for the debug socket" ) do |number|
|
|
412
417
|
@port = number
|
|
413
418
|
end
|
|
@@ -440,8 +445,12 @@ class ParserMain < Main
|
|
|
440
445
|
def recognize( in_stream )
|
|
441
446
|
parser_options = {}
|
|
442
447
|
if @debug
|
|
443
|
-
|
|
444
|
-
|
|
448
|
+
if @trace
|
|
449
|
+
parser_options[ :debug_listener ] = ANTLR3::Debug::RuleTracer.new
|
|
450
|
+
else
|
|
451
|
+
parser_options[ :port ] = @port
|
|
452
|
+
parser_options[ :log ] = @log
|
|
453
|
+
end
|
|
445
454
|
end
|
|
446
455
|
lexer = @lexer_class.new( in_stream )
|
|
447
456
|
# token_stream = CommonTokenStream.new( lexer )
|
|
@@ -494,24 +503,35 @@ class WalkerMain < Main
|
|
|
494
503
|
opt.separator ''
|
|
495
504
|
opt.separator "Tree Parser Configuration:"
|
|
496
505
|
|
|
497
|
-
opt.on( '--lexer-name CLASS_NAME' ) { |val| @lexer_class_name = val }
|
|
498
|
-
opt.on(
|
|
506
|
+
opt.on( '--lexer-name CLASS_NAME', 'full name of the lexer class to use' ) { |val| @lexer_class_name = val }
|
|
507
|
+
opt.on(
|
|
508
|
+
'--lexer-file PATH_TO_LIBRARY',
|
|
509
|
+
'path to load to make the lexer class available'
|
|
510
|
+
) { |val|
|
|
499
511
|
begin
|
|
500
512
|
test( ?f, val ) ? load( val ) : require( val )
|
|
501
513
|
rescue LoadError
|
|
502
|
-
warn( "unable to load the library specified by --lexer-file: #{ $! }" )
|
|
514
|
+
warn( "unable to load the library `#{ val }' specified by --lexer-file: #{ $! }" )
|
|
503
515
|
end
|
|
504
516
|
}
|
|
505
|
-
|
|
506
|
-
opt.on(
|
|
517
|
+
|
|
518
|
+
opt.on(
|
|
519
|
+
'--parser-name CLASS_NAME',
|
|
520
|
+
'full name of the parser class to use'
|
|
521
|
+
) { |val| @parser_class_name = val }
|
|
522
|
+
opt.on(
|
|
523
|
+
'--parser-file PATH_TO_LIBRARY',
|
|
524
|
+
'path to load to make the parser class available'
|
|
525
|
+
) { |val|
|
|
507
526
|
begin
|
|
508
527
|
test( ?f, val ) ? load( val ) : require( val )
|
|
509
528
|
rescue LoadError
|
|
510
529
|
warn( "unable to load the library specified by --parser-file: #{ $! }" )
|
|
511
530
|
end
|
|
512
531
|
}
|
|
513
|
-
|
|
514
|
-
opt.on( '--rule NAME' ) { |val| @
|
|
532
|
+
|
|
533
|
+
opt.on( '--parser-rule NAME', "name of the parser rule to use on the input" ) { |val| @parser_rule = val }
|
|
534
|
+
opt.on( '--rule NAME', "name of the rule to invoke in the tree parser" ) { |val| @walker_rule = val }
|
|
515
535
|
|
|
516
536
|
if @debug
|
|
517
537
|
opt.separator ''
|
|
@@ -7,15 +7,15 @@ module ANTLR3
|
|
|
7
7
|
module ASTBuilder
|
|
8
8
|
extend ClassMacros
|
|
9
9
|
|
|
10
|
-
def self.included(klass)
|
|
10
|
+
def self.included( klass )
|
|
11
11
|
def klass.return_scope_members
|
|
12
|
-
super.push(:tree)
|
|
12
|
+
super.push( :tree )
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def initialize( input, options = {} )
|
|
17
|
-
@adaptor = options[:adaptor] ||= begin
|
|
18
|
-
(input.adaptor rescue nil) or
|
|
17
|
+
@adaptor = options[ :adaptor ] ||= begin
|
|
18
|
+
( input.adaptor rescue nil ) or
|
|
19
19
|
AST::CommonTreeAdaptor.new( token_class )
|
|
20
20
|
end
|
|
21
21
|
super( input, options )
|
|
@@ -25,15 +25,15 @@ module ASTBuilder
|
|
|
25
25
|
|
|
26
26
|
private
|
|
27
27
|
|
|
28
|
-
def subtree_stream(desc, element = nil)
|
|
28
|
+
def subtree_stream( desc, element = nil )
|
|
29
29
|
AST::RewriteRuleSubtreeStream.new( @adaptor, desc, element )
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
def token_stream(desc, element = nil)
|
|
32
|
+
def token_stream( desc, element = nil )
|
|
33
33
|
AST::RewriteRuleTokenStream.new( @adaptor, desc, element )
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
def node_stream(desc, element = nil)
|
|
36
|
+
def node_stream( desc, element = nil )
|
|
37
37
|
AST::RewriteRuleNodeStream.new( @adaptor, desc, element )
|
|
38
38
|
end
|
|
39
39
|
end
|
data/lib/antlr3/modes/filter.rb
CHANGED
|
@@ -17,40 +17,42 @@ lexer filter mode.
|
|
|
17
17
|
=end
|
|
18
18
|
module FilterMode
|
|
19
19
|
def next_token
|
|
20
|
+
# if at end-of-file, return the EOF token
|
|
20
21
|
@input.peek == ANTLR3::EOF and return ANTLR3::EOF_TOKEN
|
|
21
|
-
|
|
22
|
-
@state.
|
|
22
|
+
|
|
23
|
+
@state.token = nil
|
|
24
|
+
@state.channel = ANTLR3::DEFAULT_CHANNEL
|
|
23
25
|
@state.token_start_position = @input.index
|
|
24
|
-
@state.token_start_column
|
|
25
|
-
@state.token_start_line
|
|
26
|
-
@state.text
|
|
27
|
-
@state.backtracking
|
|
26
|
+
@state.token_start_column = @input.column
|
|
27
|
+
@state.token_start_line = @input.line
|
|
28
|
+
@state.text = nil
|
|
29
|
+
@state.backtracking = 1
|
|
30
|
+
|
|
28
31
|
m = @input.mark
|
|
29
|
-
# means we won't throw slow exception
|
|
30
32
|
token!
|
|
31
|
-
@input.release(m)
|
|
33
|
+
@input.release( m )
|
|
32
34
|
emit
|
|
33
35
|
return @state.token
|
|
34
36
|
rescue ANTLR3::BacktrackingFailed
|
|
35
37
|
# token! backtracks with synpred at backtracking==2
|
|
36
38
|
# and we set the synpredgate to allow actions at level 1.
|
|
37
|
-
@input.rewind(m)
|
|
38
|
-
@input.consume
|
|
39
|
+
@input.rewind( m )
|
|
40
|
+
@input.consume # advance one char and try again
|
|
39
41
|
retry
|
|
40
42
|
rescue ANTLR3::Error::RecognitionError => re
|
|
41
43
|
# shouldn't happen in backtracking mode, but...
|
|
42
|
-
report_error(re)
|
|
43
|
-
recover(re)
|
|
44
|
+
report_error( re )
|
|
45
|
+
recover( re )
|
|
44
46
|
ensure
|
|
45
47
|
@state.backtracking = 0
|
|
46
48
|
end
|
|
47
49
|
|
|
48
|
-
def memoize(rule, start_index, success)
|
|
49
|
-
super(rule, start_index, success) if @state.backtracking > 1
|
|
50
|
+
def memoize( rule, start_index, success )
|
|
51
|
+
super( rule, start_index, success ) if @state.backtracking > 1
|
|
50
52
|
end
|
|
51
53
|
|
|
52
|
-
def already_parsed_rule?(rule)
|
|
53
|
-
@state.backtracking > 1 ? super(rule) : false
|
|
54
|
+
def already_parsed_rule?( rule )
|
|
55
|
+
@state.backtracking > 1 ? super( rule ) : false
|
|
54
56
|
end
|
|
55
57
|
end
|
|
56
|
-
end
|
|
58
|
+
end
|