antlr3 1.2.4 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rake/tasklib'
5
+
6
+ module ANTLR3
7
+ class CompileTask < Rake::TaskLib
8
+
9
+
10
+
11
+
12
+ end
13
+ end
@@ -472,14 +472,17 @@ class TokenScheme < ::Module
472
472
  tk_class ||= Class.new(::ANTLR3::CommonToken)
473
473
  self.token_class = tk_class
474
474
 
475
- const_set(:TOKEN_NAMES, ::ANTLR3::Constants::BUILT_IN_TOKEN_NAMES.clone)
475
+ const_set( :TOKEN_NAMES, ::ANTLR3::Constants::BUILT_IN_TOKEN_NAMES.clone )
476
+
477
+ @types = ::ANTLR3::Constants::BUILT_IN_TOKEN_NAMES.invert
478
+ @unused = ::ANTLR3::Constants::MIN_TOKEN_TYPE
476
479
 
477
480
  scheme = self
478
- define_method(:token_scheme) { scheme }
479
- define_method(:token_names) { scheme::TOKEN_NAMES }
480
- define_method(:token_name) do |type|
481
+ define_method( :token_scheme ) { scheme }
482
+ define_method( :token_names ) { scheme::TOKEN_NAMES }
483
+ define_method( :token_name ) do |type|
481
484
  begin
482
- token_names[type] or super
485
+ token_names[ type ] or super
483
486
  rescue NoMethodError
484
487
  ::ANTLR3::CommonToken.token_name(type)
485
488
  end
@@ -492,31 +495,64 @@ class TokenScheme < ::Module
492
495
  end
493
496
  end
494
497
 
498
+ def self.build( *token_names )
499
+ token_names = [ token_names ].flatten!
500
+ token_names.compact!
501
+ token_names.uniq!
502
+ tk_class = Class === token_names.first ? token_names.shift : nil
503
+ value_maps, names = token_names.partition { |i| Hash === i }
504
+ new( tk_class ) do
505
+ for value_map in value_maps
506
+ define_tokens( value_map )
507
+ end
508
+
509
+ for name in names
510
+ define_token( name )
511
+ end
512
+ end
513
+ end
514
+
515
+
495
516
  def included(mod)
496
517
  super
497
518
  mod.extend(self)
498
519
  end
499
520
  private :included
521
+ attr_reader :unused, :types
500
522
 
501
- def define_tokens(token_map = {})
523
+ def define_tokens( token_map = {} )
502
524
  for token_name, token_value in token_map
503
525
  define_token(token_name, token_value)
504
526
  end
505
527
  return self
506
528
  end
507
529
 
508
- def define_token(name, value)
509
- if const_defined?(name)
510
- current_value = const_get(name)
530
+ def define_token( name, value = nil )
531
+ name = name.to_s
532
+
533
+ if current_value = @types[ name ]
534
+ # token type has already been defined
535
+ # raise an error unless value is the same as the current value
536
+ value ||= current_value
511
537
  unless current_value == value
512
- error = NameError.new("new token type definition ``#{name} = #{value}'' conflicts " <<
513
- "with existing type definition ``#{name} = #{current_value}''", name)
514
- raise error
538
+ raise NameError.new(
539
+ "new token type definition ``#{name} = #{value}'' conflicts " <<
540
+ "with existing type definition ``#{name} = #{current_value}''", name
541
+ )
515
542
  end
516
543
  else
517
- const_set(name, value)
544
+ value ||= @unused
545
+ if name =~ /^[A-Z]\w*$/
546
+ const_set( name, @types[ name ] = value )
547
+ else
548
+ constant = "T__#{ value }"
549
+ const_set( constant, @types[ constant ] = value )
550
+ @types[ name ] = value
551
+ end
552
+ register_name( value, name ) unless built_in_type?( value )
518
553
  end
519
- register_name(value, name) unless built_in_type?(value)
554
+
555
+ value >= @unused and @unused = value + 1
520
556
  return self
521
557
  end
522
558
 
@@ -533,13 +569,13 @@ class TokenScheme < ::Module
533
569
  end
534
570
  end
535
571
 
536
- def register_name(type_value, name)
572
+ def register_name( type_value, name )
537
573
  name = name.to_s.freeze
538
574
  if token_names.has_key?(type_value)
539
575
  current_name = token_names[type_value]
540
576
  current_name == name and return name
541
577
 
542
- if current_name == "T__#{type_value}"
578
+ if current_name == "T__#{ type_value }"
543
579
  # only an anonymous name is registered -- upgrade the name to the full literal name
544
580
  token_names[type_value] = name
545
581
  elsif name == "T__#{type_value}"
@@ -583,7 +619,7 @@ class TokenScheme < ::Module
583
619
  Class === klass or raise(TypeError, "token_class must be a Class")
584
620
  Util.silence_warnings do
585
621
  klass < self or klass.send(:include, self)
586
- const_set(:Token, klass)
622
+ const_set( :Token, klass )
587
623
  end
588
624
  end
589
625
 
@@ -102,132 +102,84 @@ See Tree and TreeAdaptor for more information.
102
102
  =end
103
103
 
104
104
  class TreeParser < BaseRecognizer
105
- def self.main(argv = ARGV, options = {})
106
- if argv.is_a?(::Hash) then argv, options = ARGV, argv end
105
+ def self.main( argv = ARGV, options = {} )
106
+ if ::Hash === argv then argv, options = ARGV, argv end
107
107
  main = ANTLR3::Main::WalkerMain.new(self, options)
108
- block_given? ? yield(main) : main.execute(argv)
108
+ block_given? ? yield( main ) : main.execute( argv )
109
109
  end
110
110
 
111
- def initialize(input, options = {})
112
- super(options)
111
+ def initialize( input, options = {} )
112
+ super( options )
113
113
  @input = input
114
114
  end
115
115
 
116
116
  alias tree_node_stream input
117
117
  alias tree_node_stream= input=
118
+
118
119
  def source_name
119
120
  @input.source_name
120
121
  end
121
122
 
122
- def current_input_symbol
123
- @input.look
124
- end
125
-
126
- def missing_symbol(error, expected_token_type, follow)
123
+ def missing_symbol( error, expected_token_type, follow )
127
124
  name = token_name(expected_token_type).to_s
128
125
  text = "<missing " << name << '>'
129
126
  tk = create_token do |t|
130
127
  t.text = text
131
128
  t.type = expected_token_type
132
129
  end
133
- return(CommonTree.new(tk))
134
- end
135
-
136
- DOT_DOT_PATTERN = /.*[^\.]\\.{2}[^\.].*/
137
- DOUBLE_ETC_PATTERN = /.*\.{3}\s+\.{3}.*/
138
-
139
- def in_context?(context)
140
- self.class.in_context?(
141
- @input.tree_adaptor, token_names, @input.look, context)
130
+ return( CommonTree.new( tk ) )
142
131
  end
143
132
 
144
- def self.in_context?(adaptor, token_names, tree, context)
145
- case context
146
- when DOT_DOT_PATTERN then raise ArgumentError, "invalid syntax: .."
147
- when DOUBLE_ETC_PATTERN then raise ArgumentError, "invalid syntax: ... ..."
148
- end
149
-
150
- context = context.gsub(/([^\.\s])\.{3}([^\.])/, '\1 ... \2')
151
- context.strip!
152
- nodes = context.split(/\s+/)
153
-
154
- ni = nodes.length.pred
155
- tree = adaptor.parent(tree)
156
- while ni >= 0 and tree
157
- if nodes[ni] == '...'
158
- ni.zero? and return true
159
- goal = nodes[ni.pred]
160
- ancestor = ancestor(adaptor, token_names, tree, goal) or return false
161
- tree = ancestor
162
- ni -= 1
163
- end
164
- name = token_names[adaptor.type_of(tree)]
165
- name == nodes[ni] or return false
166
- ni -= 1
167
- tree = adaptor.parent(tree)
168
- end
169
-
170
- tree.nil? && ni >= 0 and return false
171
- return true
172
- end
173
-
174
- def self.ancestor(adaptor, token_names, tree, goal)
175
- while tree
176
- name = token_names[adaptor.type_of(tree)]
177
- name == goal and return tree
178
- tree = adaptor.parent(tree)
179
- end
180
- return nil
181
- end
182
-
183
- def match_any(ignore = nil)
133
+ def match_any( ignore = nil )
184
134
  @state.error_recovery = false
185
-
186
- look = @input.look
187
- if @input.tree_adaptor.child_count(look) == 0
135
+
136
+ look, adaptor = @input.look, @input.tree_adaptor
137
+ if adaptor.child_count( look ) == 0
188
138
  @input.consume
189
139
  return
190
140
  end
141
+
191
142
  level = 0
192
- token_type = @input.tree_adaptor.type_of(look)
193
- until token_type == EOF or (token_type == UP && level == 0)
143
+ while type = @input.peek and type != EOF
144
+ #token_type == EOF or ( token_type == UP && level == 0 )
194
145
  @input.consume
195
- look = @input.look
196
- case token_type = @input.tree_adaptor.type_of(look)
146
+ case type
197
147
  when DOWN then level += 1
198
- when UP then level -= 1
148
+ when UP
149
+ level -= 1
150
+ level.zero? and break
199
151
  end
200
152
  end
201
- @input.consume # consume UP
202
153
  end
203
154
 
204
155
  def mismatch(input, type, follow = nil)
205
- raise MismatchedTreeNode.new(type, input)
156
+ raise MismatchedTreeNode.new( type, input )
206
157
  end
207
158
 
208
- def error_header(err)
209
- (<<-END.strip!)
210
- #{self.grammar_file_name}: node from #{
211
- err.approximate_line_info? ? 'after ' : ''
212
- } line #{err.line}:#{err.column}
159
+ def error_header( e )
160
+ <<-END.strip!
161
+ #{ grammar_file_name }: node from #{
162
+ e.approximate_line_info? ? 'after ' : ''
163
+ } line #{ e.line }:#{ e.column }
213
164
  END
214
165
  end
215
166
 
216
- def error_message(e)
217
- adaptor = e.input.tree_adaptor
218
- e.token = adaptor.token(e.node)
219
- e.token ||= create_token do |tok|
220
- tok.type = adaptor.type_of(e.node)
221
- tok.text = adaptor.text_of(e.node)
167
+ def error_message( e )
168
+ adaptor = e.input.adaptor
169
+ e.token = adaptor.token( e.node )
170
+ e.token ||= create_token do | tok |
171
+ tok.type = adaptor.type_of( e.node )
172
+ tok.text = adaptor.text_of( e.node )
222
173
  end
223
- return super(e)
174
+ return super( e )
224
175
  end
225
176
 
226
- def trace_in(rule_name, rule_index)
227
- super(rule_name, rule_index, @input.look)
177
+ def trace_in( rule_name, rule_index )
178
+ super( rule_name, rule_index, @input.look )
228
179
  end
229
- def trace_out(rule_name, rule_index)
230
- super(rule_name, rule_index, @input.look)
180
+
181
+ def trace_out( rule_name, rule_index )
182
+ super( rule_name, rule_index, @input.look )
231
183
  end
232
184
  end
233
185
 
@@ -267,116 +219,116 @@ throughout the AST library:
267
219
  =end
268
220
 
269
221
  module Tree
270
- attr_accessor :parent
271
- attr_accessor :token_start_index
272
- attr_accessor :token_stop_index
222
+ #attr_accessor :parent
223
+ attr_accessor :start_index
224
+ attr_accessor :stop_index
273
225
  attr_accessor :child_index
274
226
  attr_reader :type
275
227
  attr_reader :text
276
228
  attr_reader :line
277
229
  attr_reader :column
278
- attr_reader :children
230
+ #attr_reader :children
279
231
  attr_reader :token
280
232
 
281
- def [](index, length = nil) # getChild(index)
282
- length.nil? ? self.children[index] : self.children[index, length]
283
- end
284
-
285
- alias child []
286
-
287
- def child_count # getChildCount
288
- self.children.length
289
- end
290
-
291
- def push(node, *nodes)
292
- self.children.push(node, *nodes)
293
- end
294
-
295
- alias << push
296
-
297
- def shift
298
- self.children.shift
299
- end
300
-
301
- def unshift(node, *nodes)
302
- self.children.unshift(node, *nodes)
303
- end
304
-
305
- alias add_child shift
306
-
307
- def set_child(index, tree)
308
- self.children[index] = tree
309
- end
310
-
311
- alias []= set_child
312
-
313
- def delete_child(index)
314
- self.children.delete(index)
315
- end
316
-
317
- def replace_children(start, stop, trees)
318
- self.children[start..stop] = trees
319
- end
320
-
321
- def to_a
322
- child_arrays = children.map { |child| child.to_a }
323
- [token, *child_arrays]
324
- end
325
-
326
- include Enumerable
327
-
328
- def each_child
329
- block_given? or return enum_for(__method__)
330
- self.children.each { |child| yield(child) }
331
- end
332
-
333
- def each_ancestor
334
- block_given? or return enum_for(__method__)
335
- self.ancestors.each { |anc| yield(anc) }
336
- end
337
-
338
- def walk
339
- block_given? or return(enum_for(__method__))
340
- stack = []
341
- cursor = self
342
- loop do
343
- begin
344
- yield(cursor)
345
- stack.push(cursor.children.clone) unless cursor.leaf?
346
- rescue StopIteration
347
- # skips adding children to prune the node
348
- ensure
349
- break if stack.empty?
350
- cursor = stack.last.shift
351
- stack.pop if stack.last.empty?
352
- end
353
- end
354
- return self
355
- end
356
-
357
- def prune
358
- raise StopIteration
359
- end
360
-
361
- alias :each :walk
362
-
363
- def root?
364
- parent.nil?
365
- end
366
-
367
- def leaf?
368
- children.empty?
369
- end
370
-
371
- def ancestors
372
- a = []
373
- cursor = self
374
- until cursor.root?
375
- a.push(cursor.parent)
376
- cursor = cursor.parent
377
- end
378
- return a
379
- end
233
+ #def [](index, length = nil) # getChild(index)
234
+ # length.nil? ? self.children[index] : self.children[index, length]
235
+ #end
236
+ #
237
+ #alias child []
238
+ #
239
+ #def child_count # getChildCount
240
+ # self.children.length
241
+ #end
242
+ #
243
+ #def push(node, *nodes)
244
+ # self.children.push(node, *nodes)
245
+ #end
246
+ #
247
+ #alias << push
248
+ #
249
+ #def shift
250
+ # self.children.shift
251
+ #end
252
+ #
253
+ #def unshift(node, *nodes)
254
+ # self.children.unshift(node, *nodes)
255
+ #end
256
+ #
257
+ #alias add_child shift
258
+
259
+ #def set_child(index, tree)
260
+ # self.children[index] = tree
261
+ #end
262
+
263
+ #alias []= set_child
264
+
265
+ #def delete_child(index)
266
+ # self.children.delete(index)
267
+ #end
268
+ #
269
+ #def replace_children(start, stop, trees)
270
+ # self.children[start..stop] = trees
271
+ #end
272
+ #
273
+ #def to_a
274
+ # child_arrays = children.map { |child| child.to_a }
275
+ # [token, *child_arrays]
276
+ #end
277
+
278
+ #include Enumerable
279
+ #
280
+ #def each_child
281
+ # block_given? or return enum_for(__method__)
282
+ # self.children.each { |child| yield(child) }
283
+ #end
284
+ #
285
+ #def each_ancestor
286
+ # block_given? or return enum_for(__method__)
287
+ # self.ancestors.each { |anc| yield(anc) }
288
+ #end
289
+
290
+ #def walk
291
+ # block_given? or return( enum_for( :walk ) )
292
+ # stack = []
293
+ # cursor = self
294
+ # loop do
295
+ # begin
296
+ # yield(cursor)
297
+ # stack.push(cursor.children.clone) unless cursor.leaf?
298
+ # rescue StopIteration
299
+ # # skips adding children to prune the node
300
+ # ensure
301
+ # break if stack.empty?
302
+ # cursor = stack.last.shift
303
+ # stack.pop if stack.last.empty?
304
+ # end
305
+ # end
306
+ # return self
307
+ #end
308
+
309
+ #def prune
310
+ # raise StopIteration
311
+ #end
312
+
313
+ #alias :each :walk
314
+ #
315
+ #def root?
316
+ # parent.nil?
317
+ #end
318
+ #
319
+ #def leaf?
320
+ # children.empty?
321
+ #end
322
+ #
323
+ #def ancestors
324
+ # a = []
325
+ # cursor = self
326
+ # until cursor.root?
327
+ # a.push(cursor.parent)
328
+ # cursor = cursor.parent
329
+ # end
330
+ # return a
331
+ #end
380
332
  end
381
333
 
382
334
 
@@ -389,79 +341,77 @@ a node's token <i>payload</i>.
389
341
 
390
342
  =end
391
343
 
392
- class BaseTree
344
+ class BaseTree < ::Array
345
+ attr_accessor :parent
393
346
  extend ClassMacros
394
347
  include Tree
395
-
396
- def initialize(node = nil)
397
- @children = []
348
+
349
+ def initialize( node = nil )
350
+ super()
398
351
  @parent = nil
399
352
  @child_index = 0
400
353
  end
401
354
 
402
- def child(index)
403
- @children[index]
404
- end
355
+ def children() self end
405
356
 
406
- def first_child_with_type(tree_type)
407
- @children.find { |child| child.type == tree_type }
408
- end
357
+ alias child at
358
+ alias child_count length
409
359
 
410
- def child_count
411
- @children.length
360
+ def first_with_type( tree_type )
361
+ find { | child | child.type == tree_type }
412
362
  end
413
363
 
414
- def add_child(child_tree)
364
+ def add_child( child_tree )
415
365
  child_tree.nil? and return
416
366
  if child_tree.flat_list?
417
- if @children.equal?(child_tree.children)
367
+ if equal?( child_tree.children )
418
368
  raise ArgumentError, "attempt to add child list to itself"
419
369
  end
420
- child_tree.children.each_with_index do |child, index|
370
+ child_tree.each_with_index do | child, index |
421
371
  child.parent = self
422
- child.child_index = @children.length + index
372
+ child.child_index = length + index
423
373
  end
424
- @children.concat(child_tree.children)
374
+ concat( child_tree )
425
375
  else
426
- @children << child_tree
376
+ child_tree.child_index = length
427
377
  child_tree.parent = self
428
- child_tree.child_index = @children.length - 1
378
+ self << child_tree
429
379
  end
380
+ return( self )
430
381
  end
431
382
 
432
- def add_children(children)
433
- @children.concat(children)
383
+ def detach
384
+ @parent = nil
385
+ @child_index = -1
386
+ return( self )
434
387
  end
435
388
 
436
- def set_child(index, tree)
389
+ alias add_children concat
390
+
391
+ def set_child( index, tree )
437
392
  return if tree.nil?
438
393
  tree.flat_list? and raise ArgumentError, "Can't set single child to a list"
439
394
  tree.parent = self
440
395
  tree.child_index = index
441
- @children[index] = tree
396
+ self[ index ] = tree
442
397
  end
443
398
 
444
- def delete_child(index)
445
- if killed = @children[index]
446
- @children[index, 1] = nil
447
- @children[index..-1].each_with_index do |child, i|
448
- child.child_index = index + i
449
- end
450
- end
399
+ def delete_child( index )
400
+ killed = delete_at( index ) and freshen( index )
451
401
  return killed
452
402
  end
453
403
 
454
- def replace_children(start_index, stop_index, new_tree)
455
- start_index >= @children.length or stop_index >= @children.length and
404
+ def replace_children( start, stop, new_tree )
405
+ start >= length or stop >= length and
456
406
  raise IndexError, (<<-END).gsub!(/^\s+\| /,'')
457
407
  | indices span beyond the number of children:
458
- | children.length = #{@children.length}
408
+ | children.length = #{length}
459
409
  | start = #{start_index.inspect}
460
410
  | stop = #{stop_index.inspect}
461
411
  END
462
- new_children = new_tree.flat_list? ? new_tree.children : [new_tree]
463
- @children[start_index..stop_index] = new_children
464
- freshen_parent_and_child_indexes(start_index)
412
+ new_children = new_tree.flat_list? ? new_tree : [ new_tree ]
413
+ self[ start .. stop ] = new_children
414
+ freshen(start_index)
465
415
  return self
466
416
  end
467
417
 
@@ -469,38 +419,35 @@ class BaseTree
469
419
  false
470
420
  end
471
421
 
472
- def freshen_parent_and_child_indexes(offset = 0)
473
- @children[offset..-1].each_with_index do |child, index|
474
- child.child_index = index + offset
475
- child.parent = self
422
+ def freshen( offset = 0 )
423
+ for i in offset ... length
424
+ node = self[ i ]
425
+ node.child_index = i
426
+ node.parent = self
476
427
  end
477
428
  end
478
429
 
479
-
480
- def sanity_check_parent_and_child_indexes(parent = nil, i = -1)
430
+ def sanity_check( parent = nil, i = -1 )
481
431
  parent == @parent or
482
- raise TreeInconsistency.failed_parent_check!(parent, @parent)
432
+ raise TreeInconsistency.failed_parent_check!( parent, @parent )
483
433
  i == @child_index or
484
- raise TreeInconsistency.failed_index_check!(i, @child_index)
485
- @children.each_with_index do |child, index|
486
- child.sanity_check_parent_and_child_indexes(self, index)
434
+ raise TreeInconsistency.failed_index_check!( i, @child_index )
435
+ each_with_index do | child, index |
436
+ child.sanity_check( self, index )
487
437
  end
488
438
  end
489
-
490
- def to_string_tree
491
- @children.empty? and return self.to_s
492
- buffer = []
493
- buffer << '(' << self.to_s << ' ' unless self.flat_list?
494
- buffer << @children.map { |child| child.to_string_tree }.join(' ')
495
- buffer << ' '
496
- buffer << ')' unless self.flat_list?
497
- return buffer.join
498
- end
499
439
 
500
- def line; return 0; end
501
- def column; return 0; end
440
+ def inspect
441
+ empty? and return to_s
442
+ buffer = ''
443
+ buffer << '(' << to_s << ' ' unless flat_list?
444
+ buffer << map { | c | c.inspect }.join( ' ' )
445
+ buffer << ')' unless flat_list?
446
+ return( buffer )
447
+ end
502
448
 
503
449
  abstract :to_s
450
+ #protected :sanity_check, :freshen
504
451
  end
505
452
 
506
453
 
@@ -529,31 +476,31 @@ the customized lighter-weight trees.
529
476
  =end
530
477
 
531
478
  class CommonTree < BaseTree
532
- def initialize(payload = nil)
479
+ def initialize( payload = nil )
533
480
  super()
534
481
  @start_index = -1
535
482
  @stop_index = -1
536
483
  @child_index = -1
537
484
  case payload
538
- when CommonTree then
539
- @token = payload.token
485
+ when CommonTree then # copy-constructor style init
486
+ @token = payload.token
540
487
  @start_index = payload.start_index
541
- @stop_index = payload.stop_index
488
+ @stop_index = payload.stop_index
542
489
  when nil, Token then @token = payload
543
- else raise ArgumentError, "Invalid argument type: #{payload.class} (#{payload.inspect})"
490
+ else raise ArgumentError,
491
+ "Invalid argument type: %s (%p)" % [ payload.class, payload ]
544
492
  end
545
493
  end
546
494
 
547
- def initialize_copy(orig)
495
+ def initialize_copy( orig )
548
496
  super
497
+ clear
549
498
  @parent = nil
550
- @children = []
551
499
  end
552
500
 
553
- attr_reader :token
554
501
 
555
502
  def copy_node
556
- return CommonTree.new(@token)
503
+ return self.class.new( @token )
557
504
  end
558
505
 
559
506
  def flat_list?
@@ -561,7 +508,7 @@ class CommonTree < BaseTree
561
508
  end
562
509
 
563
510
  def type
564
- @token.nil? ? 0 : @token.type
511
+ @token ? @token.type : 0
565
512
  end
566
513
 
567
514
  def text
@@ -570,109 +517,97 @@ class CommonTree < BaseTree
570
517
 
571
518
  def line
572
519
  if @token.nil? or @token.line == 0
573
- return (child_count > 0 ? child(0).line : 0)
520
+ return ( empty? ? 0 : first.line )
574
521
  end
575
522
  return @token.line
576
523
  end
577
524
 
578
525
  def column
579
526
  if @token.nil? or @token.column == -1
580
- return(child_count > 0 ? child(0).column : 0)
527
+ return( empty? ? 0 : first.column )
581
528
  end
582
529
  return @token.column
583
530
  end
584
531
 
532
+
585
533
  def start_index
586
- return @token.index if @start_index == -1 and !@token.nil?
534
+ @start_index == -1 and @token and return @token.index
587
535
  return @start_index
588
536
  end
589
537
 
590
538
  def stop_index
591
- return @token.index if @stop_index == -1 and !@token.nil?
539
+ @stop_index == -1 and @token and return @token.index
592
540
  return @stop_index
593
541
  end
594
542
 
595
- attr_writer :start_index, :stop_index
596
543
  alias token_start_index= start_index=
597
544
  alias token_stop_index= stop_index=
598
545
  alias token_start_index start_index
599
546
  alias token_stop_index stop_index
600
547
 
601
548
  def name
602
- @token.nil? ? 'INVALID' : @token.name
549
+ @token.name rescue 'INVALID'
603
550
  end
604
551
 
605
552
  def token_range
606
- unknown_token_boundaries? and set_unknown_token_boundaries
607
- @start_index..@stop_index
553
+ unknown_boundaries? and infer_boundaries
554
+ @start_index .. @stop_index
608
555
  end
556
+
609
557
  def source_range
610
- unknown_token_boundaries? and set_unknown_token_boundaries
611
- tokens = self.map do |node|
612
- tk = node.token
613
- (tk && tk.index >= 0) ? tk : nil
558
+ unknown_boundaries? and infer_boundaries
559
+ tokens = map do | node |
560
+ tk = node.token and tk.index >= 0 ? tk : nil
614
561
  end
615
562
  tokens.compact!
616
- first, last = tokens.minmax_by { |tk| tk.index }
617
- (first.start)..(last.stop)
563
+ first, last = tokens.minmax_by { |t| t.index }
564
+ first.start .. last.stop
618
565
  end
619
566
 
620
- def set_unknown_token_boundaries
621
- if @children.nil? || @children.empty? and (@start_index < 0 or @stop_index < 0)
567
+ def infer_boundaries
568
+ if empty? and @start_index < 0 || @stop_index < 0
622
569
  @start_index = @stop_index = @token.index rescue -1
623
570
  return
624
571
  end
625
- for child in @children do child.set_unknown_token_boundaries end
572
+ for child in self do child.infer_boundaries end
626
573
  return if @start_index >= 0 and @stop_index >= 0
627
574
 
628
- @start_index = @children.first.token_start_index
629
- @stop_index = @children.last.token_stop_index
575
+ @start_index = first.start_index
576
+ @stop_index = last.stop_index
630
577
  return nil
631
578
  end
632
579
 
633
- def unknown_token_boundaries?
634
- @start_index < 0 || @stop_index < 0
580
+ def unknown_boundaries?
581
+ @start_index < 0 or @stop_index < 0
635
582
  end
636
583
 
637
584
  def to_s
638
585
  flat_list? ? 'nil' : @token.text.to_s
639
586
  end
640
587
 
641
- def to_string_tree
642
- children.empty? and return self.to_s
643
-
644
- out = ''
645
- out << "(#{self} " unless flat_list?
646
-
647
- out << children.map { |child| child.to_string_tree }.join(' ')
648
- out << ')' unless flat_list?
649
- return out
650
- end
651
- alias inspect to_string_tree
652
-
653
588
  def pretty_print(printer)
654
- if children.empty?
655
- lines = self.to_s.split(/\r?\n/, -1)
656
- for line in lines[0...-1]
657
- printer.text(line)
658
- printer.text(printer.newline)
659
- printer.text(printer.genspace[printer.indent])
589
+ if empty?
590
+ to_s.each_line do | line |
591
+ nl = line.chomp!
592
+ printer.text( line )
593
+ if nl
594
+ printer.text( printer.newline )
595
+ printer.text( printer.genspace[ printer.indent ] )
596
+ end
660
597
  end
661
- return(printer.text(lines.last))
662
- end
663
-
664
- endpoints = flat_list? ? ['', ''] : ["(#{self}", ')']
665
- printer.group(1, *endpoints) do
666
- for child in self.children
667
- printer.breakable
668
- printer.pp(child)
598
+ else
599
+ endpoints = flat_list? ? ['', ''] : ["(#{self}", ')']
600
+ printer.group(1, *endpoints) do
601
+ for child in self
602
+ printer.breakable
603
+ printer.pp(child)
604
+ end
669
605
  end
670
606
  end
671
607
  end
672
608
 
673
609
  end
674
610
 
675
-
676
611
  =begin rdoc ANTLR3::AST::CommonErrorNode
677
612
 
678
613
  Represents a series of erroneous tokens from a token stream input
@@ -685,7 +620,7 @@ class CommonErrorNode < CommonTree
685
620
 
686
621
  attr_accessor :input, :start, :stop, :error
687
622
 
688
- def initialize(input, start, stop, error)
623
+ def initialize( input, start, stop, error )
689
624
  super(nil)
690
625
  stop = start if stop.nil? or
691
626
  (stop.token_index < start.token_index and stop.type != EOF)
@@ -708,9 +643,9 @@ class CommonErrorNode < CommonTree
708
643
  when Token
709
644
  i = @start.token_index
710
645
  j = (@stop.type == EOF) ? @input.size : @stop.token_index
711
- @input.to_s(i,j) # <- the bad text
646
+ @input.to_s( i, j ) # <- the bad text
712
647
  when Tree
713
- @input.to_s(@start, @stop) # <- the bad text
648
+ @input.to_s( @start, @stop ) # <- the bad text
714
649
  else
715
650
  "<unknown>"
716
651
  end
@@ -721,18 +656,18 @@ class CommonErrorNode < CommonTree
721
656
  when MissingToken
722
657
  "<missing type: #{@error.missing_type}>"
723
658
  when UnwantedToken
724
- "<extraneous: #{@error.token.inspect}, resync = #{self.text}>"
659
+ "<extraneous: #{@error.token.inspect}, resync = #{ text }>"
725
660
  when MismatchedToken
726
- "<mismatched token: #{@error.token.inspect}, resync = #{self.text}>"
661
+ "<mismatched token: #{@error.token.inspect}, resync = #{ text }>"
727
662
  when NoViableAlternative
728
- "<unexpected: #{@error.token.inspect}, resync = #{self.text}>"
729
- else "<error: #{text}>"
663
+ "<unexpected: #{@error.token.inspect}, resync = #{ text }>"
664
+ else "<error: #{ text }>"
730
665
  end
731
666
  end
732
667
 
733
668
  end
734
669
 
735
- Constants::INVALID_NODE = CommonTree.new(ANTLR3::INVALID_TOKEN)
670
+ Constants::INVALID_NODE = CommonTree.new( ANTLR3::INVALID_TOKEN )
736
671
 
737
672
  ####################################################################################################
738
673
  ########################################### Tree Adaptors ##########################################
@@ -762,127 +697,135 @@ module TreeAdaptor
762
697
  include TokenFactory
763
698
  include Constants
764
699
 
765
- def add_child(tree, child)
766
- tree.add_child(child) if tree and child
700
+ def add_child( tree, child )
701
+ tree.add_child( child ) if tree and child
767
702
  end
768
703
 
769
- def child_count(tree)
704
+ def child_count( tree )
770
705
  tree.child_count
771
706
  end
772
707
 
773
- def child_index(tree)
708
+ def child_index( tree )
774
709
  tree.child_index rescue 0
775
710
  end
776
711
 
777
- def child_of(tree, index)
778
- tree.nil? ? nil : tree.child(index)
712
+ def child_of( tree, index )
713
+ tree.nil? ? nil : tree.child( index )
779
714
  end
780
715
 
781
- def copy_node(tree_node)
782
- tree_node.dup unless tree_node.nil?
716
+ def copy_node( tree_node )
717
+ tree_node and tree_node.dup
783
718
  end
784
719
 
785
- def copy_tree(tree, parent = nil)
720
+ def copy_tree( tree, parent = nil )
786
721
  tree or return nil
787
- new_tree = copy_node(tree)
788
- set_child_index(new_tree, child_index(tree))
789
- set_parent(new_tree, parent)
790
- each_child(tree) do |child|
791
- new_sub_tree = copy_tree(child, tree)
792
- add_child(new_tree, new_sub_tree)
722
+ new_tree = copy_node( tree )
723
+ set_child_index( new_tree, child_index( tree ) )
724
+ set_parent( new_tree, parent )
725
+ each_child( tree ) do | child |
726
+ new_sub_tree = copy_tree( child, new_tree )
727
+ add_child( new_tree, new_sub_tree )
793
728
  end
794
729
  return new_tree
795
730
  end
796
731
 
797
- def delete_child(tree, index)
798
- tree.delete_child(index)
732
+ def delete_child( tree, index )
733
+ tree.delete_child( index )
799
734
  end
800
735
 
801
- def each_child(tree)
802
- block_given? or return enum_for(__method__)
803
- i = 0
804
- n = child_count(tree)
805
- while i < n
806
- yield child_of(tree, i)
807
- i += 1
736
+
737
+ def each_child( tree )
738
+ block_given? or return enum_for( :each_child, tree )
739
+ for i in 0 ... child_count( tree )
740
+ yield( child_of( tree, i ) )
808
741
  end
742
+ return tree
809
743
  end
810
744
 
811
- def flat_list?(tree)
745
+ def each_ancestor( tree, include_tree = true )
746
+ block_given? or return enum_for( :each_ancestor, tree, include_tree )
747
+ if include_tree
748
+ begin yield( tree ) end while tree = parent_of( tree )
749
+ else
750
+ while tree = parent_of( tree ) do yield( tree ) end
751
+ end
752
+ end
753
+
754
+ def flat_list?( tree )
812
755
  tree.flat_list?
813
756
  end
814
-
815
- def parent(tree)
757
+
758
+ def empty?( tree )
759
+ child_count( tree ).zero?
760
+ end
761
+
762
+ def parent( tree )
816
763
  tree.parent
817
764
  end
818
765
 
819
- def replace_children(parent, start_index, stop_index, replacement_tree)
820
- parent.nil? or
821
- parent.replace_children(start_index, stop_index, replacement_tree)
766
+ def replace_children( parent, start, stop, replacement )
767
+ parent and parent.replace_children( start, stop, replacement )
822
768
  end
823
769
 
824
- def rule_post_processing(root)
770
+ def rule_post_processing( root )
825
771
  if root and root.flat_list?
826
772
  case root.child_count
827
773
  when 0 then root = nil
828
774
  when 1
829
- root = root.child(0)
830
- root.parent = nil
831
- root.child_index = -1
775
+ root = root.child( 0 ).detach
832
776
  end
833
777
  end
834
778
  return root
835
779
  end
836
780
 
837
- def set_child_index(tree, index)
781
+ def set_child_index( tree, index )
838
782
  tree.child_index = index
839
783
  end
840
784
 
841
- def set_parent(tree, parent)
785
+ def set_parent( tree, parent )
842
786
  tree.parent = parent
843
787
  end
844
788
 
845
- def set_token_boundaries(tree, start_token = nil, stop_token = nil)
846
- return(nil) if tree.nil?
789
+ def set_token_boundaries( tree, start_token = nil, stop_token = nil )
790
+ return unless tree
847
791
  start = stop = 0
848
- start = start_token.index unless start_token.nil?
849
- stop = stop_token.index unless stop_token.nil?
850
- tree.token_start_index = start
851
- tree.token_stop_index = stop
792
+ start_token and start = start_token.index
793
+ stop_token and stop = stop_token.index
794
+ tree.start_index = start
795
+ tree.stop_index = stop
852
796
  return tree
853
797
  end
854
798
 
855
- def text_of(tree)
799
+ def text_of( tree )
856
800
  tree.text rescue nil
857
801
  end
858
802
 
859
- def token(tree)
860
- tree.is_a?(CommonTree) ? tree.token : nil
803
+ def token( tree )
804
+ CommonTree === tree ? tree.token : nil
861
805
  end
862
806
 
863
- def token_start_index(tree)
864
- tree.nil? ? -1 : tree.token_start_index
807
+ def token_start_index( tree )
808
+ tree ? tree.token_start_index : -1
865
809
  end
866
810
 
867
- def token_stop_index(tree)
868
- tree.nil? ? -1 : tree.token_stop_index
811
+ def token_stop_index( tree )
812
+ tree ? tree.token_stop_index : -1
869
813
  end
870
814
 
871
- def type_name(tree)
872
- tree.nil? ? 'INVALID' : tree.name
815
+ def type_name( tree )
816
+ tree.name rescue 'INVALID'
873
817
  end
874
818
 
875
- def type_of(tree)
876
- tree.nil? ? INVALID_TOKEN_TYPE : tree.type
819
+ def type_of( tree )
820
+ tree.type rescue INVALID_TOKEN_TYPE
877
821
  end
878
822
 
879
- def unique_id(node)
823
+ def unique_id( node )
880
824
  node.hash
881
825
  end
826
+
882
827
  end
883
828
 
884
-
885
-
886
829
  =begin rdoc ANTLR3::AST::CommonTreeAdaptor
887
830
 
888
831
  The default tree adaptor used by ANTLR-generated tree code. It, of course,
@@ -894,65 +837,85 @@ class CommonTreeAdaptor
894
837
  include TreeAdaptor
895
838
  include ANTLR3::Constants
896
839
 
897
- def initialize(token_class = ANTLR3::CommonToken)
840
+ def initialize( token_class = ANTLR3::CommonToken )
898
841
  @token_class = token_class
899
842
  end
900
843
 
901
844
  def create_flat_list!
902
- return self.create_with_payload!(nil)
845
+ return self.create_with_payload!( nil )
903
846
  end
904
847
 
905
- def become_root(new_root, old_root)
906
- new_root = create!(new_root) if new_root.is_a?(Token)
907
- old_root.nil? and return(new_root)
848
+ def become_root( new_root, old_root )
849
+ new_root = create!( new_root ) if new_root.is_a?( Token )
850
+ old_root or return( new_root )
908
851
 
909
- new_root = create_with_payload!(new_root) unless new_root.is_a?(CommonTree)
852
+ new_root = create_with_payload!( new_root ) unless CommonTree === new_root
910
853
  if new_root.flat_list?
911
854
  count = new_root.child_count
912
855
  if count == 1
913
- new_root = new_root.child(0)
856
+ new_root = new_root.child( 0 )
914
857
  elsif count > 1
915
858
  raise TreeInconsistency.multiple_roots!
916
859
  end
917
860
  end
918
861
 
919
- new_root.add_child(old_root)
862
+ new_root.add_child( old_root )
920
863
  return new_root
921
864
  end
922
865
 
923
- def create_from_token!(token_type, from_token, text = nil)
924
- from_token = from_token.clone
866
+ def create_from_token!( token_type, from_token, text = nil )
867
+ from_token = from_token.dup
925
868
  from_token.type = token_type
926
- from_token.text = text unless text.nil?
927
- tree = create_with_payload!(from_token)
869
+ from_token.text = text.to_s if text
870
+ tree = create_with_payload!( from_token )
928
871
  return tree
929
872
  end
930
873
 
931
- def create_from_type!(token_type, text)
932
- from_token = create_token(token_type, DEFAULT_CHANNEL, text)
933
- create_with_payload!(from_token)
874
+ def create_from_type!( token_type, text )
875
+ from_token = create_token( token_type, DEFAULT_CHANNEL, text )
876
+ create_with_payload!( from_token )
934
877
  end
935
878
 
936
- def create_error_node!(input, start, stop, exc)
937
- CommonErrorNode.new(input, start, stop, exc)
879
+ def create_error_node!( input, start, stop, exc )
880
+ CommonErrorNode.new( input, start, stop, exc )
938
881
  end
939
882
 
940
- def create_with_payload!(payload)
941
- return CommonTree.new(payload)
883
+ def create_with_payload!( payload )
884
+ return CommonTree.new( payload )
942
885
  end
943
-
944
- def create!(*args)
886
+ def create!( *args )
945
887
  n = args.length
946
- if n == 1 and args.first.is_a?(Token) then create_with_payload!(args[0])
947
- elsif n >= 2 and args[0].is_a?(Integer) and args[1].is_a?(Token)
948
- create_from_token!(*args)
949
- elsif n == 2 and args[0].is_a?(Integer) and args[1].is_a?(String)
888
+ if n == 1 and args.first.is_a?( Token ) then create_with_payload!( args[0] )
889
+ elsif n == 2 and Integer === args.first and String === args[1]
950
890
  create_from_type!(*args)
891
+ elsif n >= 2 and Integer === args.first
892
+ create_from_token!( *args )
951
893
  else
952
894
  sig = args.map { |f| f.class }.join(', ')
953
- raise TypeError, "No create method with this signature found: (#{sig})"
895
+ raise TypeError, "No create method with this signature found: (#{ sig })"
896
+ end
897
+ end
898
+
899
+ def rule_post_processing( root )
900
+ if root and root.flat_list?
901
+ if root.empty? then root = nil
902
+ elsif root.child_count == 1 then root = root.first.detach
903
+ end
954
904
  end
955
- end
905
+ return root
906
+ end
907
+
908
+ def empty?( tree )
909
+ tree.empty?
910
+ end
911
+
912
+ def each_child( tree )
913
+ block_given? or return enum_for( __method__, tree )
914
+ tree.each do | child |
915
+ yield( child )
916
+ end
917
+ end
918
+
956
919
  end
957
920
 
958
921
 
@@ -1011,82 +974,91 @@ objects. CommonTreeNodeStreams are the default input streams for tree parsers.
1011
974
  class CommonTreeNodeStream
1012
975
  include TreeNodeStream
1013
976
 
977
+ attr_accessor :token_stream
978
+ attr_reader :adaptor, :position
979
+
1014
980
  def initialize(*args)
1015
-
1016
- case args.length
981
+ case n = args.length
1017
982
  when 1
1018
- adaptor = CommonTreeAdaptor.new
1019
- tree = args[0]
983
+ @adaptor = CommonTreeAdaptor.new
984
+ @root = args.first
1020
985
  @nodes = @down = @up = @eof = nil
1021
986
  when 2
1022
- adaptor, tree = *args
987
+ @adaptor, @root = args
1023
988
  @nodes = @down = @up = @eof = nil
1024
989
  when 3
1025
990
  parent, start, stop = *args
1026
- adaptor = parent.adaptor
1027
- tree = parent.root
1028
- @nodes = parent.nodes[start...stop]
991
+ @adaptor = parent.adaptor
992
+ @root = parent.root
993
+ @nodes = parent.nodes[ start ... stop ]
1029
994
  @down = parent.down
1030
995
  @up = parent.up
1031
996
  @eof = parent.eof
1032
- else raise ArgumentError, "Invalid arguments"
997
+ when 0
998
+ raise ArgumentError, "wrong number of arguments (0 for 1)"
999
+ else raise ArgumentError, "wrong number of arguments (#{ n } for 3)"
1033
1000
  end
1034
- @down ||= adaptor.create_from_type!(DOWN, 'DOWN')
1035
- @up ||= adaptor.create_from_type!(UP, 'UP')
1036
- @eof ||= adaptor.create_from_type!(EOF, 'EOF')
1001
+ @down ||= @adaptor.create_from_type!( DOWN, 'DOWN' )
1002
+ @up ||= @adaptor.create_from_type!( UP, 'UP' )
1003
+ @eof ||= @adaptor.create_from_type!( EOF, 'EOF' )
1037
1004
  @nodes ||= []
1038
- @root = tree
1039
- @tokens = nil
1040
- @adaptor = adaptor
1005
+ @token_stream = nil
1006
+
1041
1007
  @unique_navigation_nodes = false
1042
1008
  @position = -1
1043
1009
  @last_marker = nil
1044
1010
  @calls = []
1045
1011
  end
1046
1012
 
1047
- def fill_buffer
1048
- __fill_buffer__(@root)
1049
- @position = 0
1013
+ def fill_buffer( tree = @root )
1014
+ @nodes << tree unless nil_tree = @adaptor.flat_list?( tree )
1015
+ unless @adaptor.empty?( tree )
1016
+ add_navigation_node( DOWN ) unless nil_tree
1017
+ @adaptor.each_child( tree ) { | c | fill_buffer( c ) }
1018
+ add_navigation_node( UP ) unless nil_tree
1019
+ end
1020
+ @position = 0 if tree == @root
1021
+ return( self )
1050
1022
  end
1051
1023
 
1052
- def node_index(node)
1024
+ def node_index( node )
1053
1025
  @position == -1 and fill_buffer
1054
- return @nodes.index(node)
1026
+ return @nodes.index( node )
1055
1027
  end
1056
1028
 
1057
- def add_navigation_node(type)
1029
+ def add_navigation_node( type )
1058
1030
  navigation_node =
1059
1031
  case type
1060
1032
  when DOWN
1061
- has_unique_navigation_nodes? ? @adaptor.create_from_type!(DOWN, 'DOWN') : @down
1033
+ has_unique_navigation_nodes? ? @adaptor.create_from_type!( DOWN, 'DOWN' ) : @down
1062
1034
  else
1063
- has_unique_navigation_nodes? ? @adaptor.create_from_type!(UP, 'UP') : @up
1035
+ has_unique_navigation_nodes? ? @adaptor.create_from_type!( UP, 'UP' ) : @up
1064
1036
  end
1065
1037
  @nodes << navigation_node
1066
1038
  end
1067
1039
 
1068
- def at(index)
1040
+ def at( index )
1069
1041
  @position == -1 and fill_buffer
1070
- @nodes[index]
1042
+ @nodes.at( index )
1071
1043
  end
1072
1044
 
1073
- def look(k = 1)
1045
+ def look( k = 1 )
1074
1046
  @position == -1 and fill_buffer
1075
1047
  k == 0 and return nil
1076
- k < 0 and return self.lookbehind(-k)
1048
+ k < 0 and return self.look_behind( -k )
1077
1049
 
1078
1050
  absolute = @position + k - 1
1079
- return(absolute >= @nodes.length ? @eof : @nodes[absolute])
1051
+ @nodes.fetch( absolute, @eof )
1080
1052
  end
1081
1053
 
1082
1054
  def current_symbol
1083
1055
  look
1084
1056
  end
1085
1057
 
1086
- def lookbehind(k = 1)
1058
+ def look_behind( k = 1 )
1087
1059
  k == 0 and return nil
1088
1060
  absolute = @position - k
1089
- return((absolute < 0) ? nil : @nodes[absolute])
1061
+ return( absolute < 0 ? nil : @nodes.fetch( absolute, @eof ) )
1090
1062
  end
1091
1063
 
1092
1064
  def tree_source
@@ -1097,14 +1069,6 @@ class CommonTreeNodeStream
1097
1069
  self.token_stream.source_name
1098
1070
  end
1099
1071
 
1100
- def token_stream
1101
- @tokens
1102
- end
1103
-
1104
- def token_stream=(tokens)
1105
- @tokens = tokens
1106
- end
1107
-
1108
1072
  def tree_adaptor
1109
1073
  @adaptor
1110
1074
  end
@@ -1119,8 +1083,8 @@ class CommonTreeNodeStream
1119
1083
  @position += 1
1120
1084
  end
1121
1085
 
1122
- def peek(i = 1)
1123
- @adaptor.type_of self.look(i)
1086
+ def peek( i = 1 )
1087
+ @adaptor.type_of look( i )
1124
1088
  end
1125
1089
 
1126
1090
  alias >> peek
@@ -1130,35 +1094,33 @@ class CommonTreeNodeStream
1130
1094
 
1131
1095
  def mark
1132
1096
  @position == -1 and fill_buffer
1133
- @last_marker = self.index
1097
+ @last_marker = @position
1134
1098
  return @last_marker
1135
1099
  end
1136
1100
 
1137
- def release(marker = nil)
1101
+ def release( marker = nil )
1138
1102
  # do nothing?
1139
1103
  end
1140
1104
 
1141
- def index
1142
- @position
1143
- end
1105
+ alias index position
1144
1106
 
1145
- def rewind(marker = @last_marker, release = true)
1146
- seek(marker)
1107
+ def rewind( marker = @last_marker, release = true )
1108
+ seek( marker )
1147
1109
  end
1148
1110
 
1149
- def seek(index)
1111
+ def seek( index )
1150
1112
  @position == -1 and fill_buffer
1151
1113
  @position = index
1152
1114
  end
1153
1115
 
1154
- def push(index)
1116
+ def push( index )
1155
1117
  @calls << @position
1156
- seek(index)
1118
+ seek( index )
1157
1119
  end
1158
1120
 
1159
1121
  def pop
1160
- ret = @calls.pop and seek(ret)
1161
- return ret
1122
+ pos = @calls.pop and seek( pos )
1123
+ return pos
1162
1124
  end
1163
1125
 
1164
1126
  def reset
@@ -1167,10 +1129,8 @@ class CommonTreeNodeStream
1167
1129
  @calls = []
1168
1130
  end
1169
1131
 
1170
- def replace_children(parent, start_index, stop_index, replacement_tree)
1171
- parent.nil? or
1172
- @adaptor.replace_children(parent, start_index,
1173
- stop_index, replacement_tree)
1132
+ def replace_children( parent, start, stop, replacement )
1133
+ parent and @adaptor.replace_children(parent, start, stop, replacement)
1174
1134
  end
1175
1135
 
1176
1136
  def size
@@ -1180,45 +1140,38 @@ class CommonTreeNodeStream
1180
1140
 
1181
1141
  def inspect
1182
1142
  @position == -1 and fill_buffer
1183
- @nodes.map { |nd| @adaptor.type_of(nd) }.join(' ')
1143
+ @nodes.map { |nd| @adaptor.type_name( nd ) }.join(' ')
1184
1144
  end
1185
1145
 
1186
- def to_s(start = nil, stop = nil)
1146
+ def extract_text( start = nil, stop = nil )
1187
1147
  start.nil? || stop.nil? and return nil
1188
1148
  @position == -1 and fill_buffer
1189
1149
 
1190
- unless @tokens.nil?
1191
- begin_token_index = @adaptor.token_start_index(start)
1192
- end_token_index = @adaptor.token_stop_index(stop)
1193
-
1194
- if @adaptor.type_of(stop) == UP
1195
- end_token_index = @adaptor.token_stop_index(start)
1196
- elsif @adaptor.type_of(stop) == EOF
1197
- end_token_index = size() - 2
1198
- end
1199
- return @tokens.to_string(begin_token_index, end_token_index)
1150
+ if @token_stream
1151
+ from = @adaptor.token_start_index( start )
1152
+ to =
1153
+ case @adaptor.type_of( stop )
1154
+ when UP then @adaptor.token_stop_index( start )
1155
+ when EOF then to = @nodes.length - 2
1156
+ else @adaptor.token_stop_index( stop )
1157
+ end
1158
+ return @token_stream.extract_text( from, to )
1200
1159
  end
1201
1160
 
1202
- t, ind = @nodes.each_with_index.find { |nd, ind| nd == start }
1203
-
1204
- buffer = []
1205
- until t == stop
1206
- text = (@adaptor.text_of(t) || ' ' << @adaptor.type_of(t).to_s)
1207
- buffer << text
1208
- ind += 1
1209
- t = @nodes[ind]
1161
+ buffer = ''
1162
+ for node in @nodes
1163
+ if node == start ... node == stop # <-- hey look, it's the flip flop operator
1164
+ buffer << @adaptor.text_of( node ) #|| ' ' << @adaptor.type_of( node ).to_s )
1165
+ end
1210
1166
  end
1211
-
1212
- text = (@adaptor.text_of(stop) || ' ' << @adaptor.type_of(stop).to_s)
1213
- buffer << text
1214
-
1215
- return buffer,join
1167
+ return( buffer )
1216
1168
  end
1217
1169
 
1218
1170
  def each
1219
1171
  @position == -1 and fill_buffer
1220
- block_given? or return enum_for(:each)
1221
- @nodes.each { |node| yield(node) }
1172
+ block_given? or return enum_for( :each )
1173
+ for node in @nodes do yield( node ) end
1174
+ self
1222
1175
  end
1223
1176
 
1224
1177
  include Enumerable
@@ -1227,29 +1180,15 @@ class CommonTreeNodeStream
1227
1180
  return @nodes.dup
1228
1181
  end
1229
1182
 
1230
- private
1231
- def linear_node_index(node)
1232
- @position == -1 and fill_buffer
1233
- @nodes.each_with_index do |n, i|
1234
- node == n and return(i)
1235
- end
1236
- return -1
1237
- end
1238
-
1239
- def __fill_buffer__(tree)
1240
- nil_tree = @adaptor.flat_list?(tree)
1241
- unless nil_tree
1242
- @nodes << tree
1243
- end
1244
-
1245
- n = @adaptor.child_count(tree)
1246
- add_navigation_node(DOWN) if not nil_tree and n > 0
1247
-
1248
- n.times { |c| __fill_buffer__ @adaptor.child_of(tree, c) }
1249
-
1250
- add_navigation_node(UP) if not nil_tree and n > 0
1251
- end
1252
-
1183
+ #private
1184
+ #
1185
+ # def linear_node_index( node )
1186
+ # @position == -1 and fill_buffer
1187
+ # @nodes.each_with_index do |n, i|
1188
+ # node == n and return(i)
1189
+ # end
1190
+ # return -1
1191
+ # end
1253
1192
  end
1254
1193
 
1255
1194
  =begin rdoc ANTLR3::AST::RewriteRuleElementStream
@@ -1259,22 +1198,21 @@ rewriting parsers.
1259
1198
 
1260
1199
  =end
1261
1200
 
1262
- class RewriteRuleElementStream
1201
+ class RewriteRuleElementStream # < Array
1263
1202
  extend ClassMacros
1264
1203
  include Error
1265
1204
 
1266
- def initialize(adaptor, element_description, elements = nil)
1205
+ def initialize( adaptor, element_description, elements = nil )
1267
1206
  @cursor = 0
1268
1207
  @single_element = nil
1269
1208
  @elements = nil
1270
1209
  @dirty = false
1271
1210
  @element_description = element_description
1272
1211
  @adaptor = adaptor
1273
- if elements.is_a?(Array)
1274
- @single_element = nil
1212
+ if elements.instance_of?( Array )
1275
1213
  @elements = elements
1276
1214
  else
1277
- add(elements)
1215
+ add( elements )
1278
1216
  end
1279
1217
  end
1280
1218
 
@@ -1283,38 +1221,35 @@ class RewriteRuleElementStream
1283
1221
  @dirty = true
1284
1222
  end
1285
1223
 
1286
- def add(el)
1287
- return(nil) unless el
1288
- unless @elements.nil?
1289
- @elements << el
1290
- return
1291
- end
1292
- if @single_element.nil?
1293
- @single_element = el
1294
- return
1224
+ def add( el )
1225
+ return( nil ) unless el
1226
+ case
1227
+ when ! el then return( nil )
1228
+ when @elements then @elements << el
1229
+ when @single_element.nil? then @single_element = el
1230
+ else
1231
+ @elements = [ @single_element, el ]
1232
+ @single_element = nil
1233
+ return( @elements )
1295
1234
  end
1296
- @elements = [@single_element, el]
1297
- @single_element = nil
1298
- return(@elements)
1299
1235
  end
1300
1236
 
1301
1237
  def next_tree
1302
- if @dirty or (@cursor >= self.length && self.length == 1)
1303
- el = __next__()
1304
- return self.dup(el)
1238
+ if @dirty or @cursor >= length && length == 1
1239
+ return dup( __next__ )
1305
1240
  end
1306
- el = __next__()
1307
- return el
1241
+ __next__
1308
1242
  end
1309
1243
 
1310
1244
  abstract :dup
1311
1245
 
1312
- def to_tree(el)
1246
+ def to_tree( el )
1313
1247
  return el
1314
1248
  end
1315
1249
 
1316
1250
  def has_next?
1317
- return(!@single_element.nil? && @cursor < 1 or !@elements.nil? && @cursor < @elements.length)
1251
+ return( @single_element && @cursor < 1 or
1252
+ @elements && @cursor < @elements.length )
1318
1253
  end
1319
1254
 
1320
1255
  def size
@@ -1322,24 +1257,27 @@ class RewriteRuleElementStream
1322
1257
  @elements and return @elements.length
1323
1258
  return 0
1324
1259
  end
1260
+
1325
1261
  alias length size
1326
1262
 
1327
- private
1263
+ private
1264
+
1328
1265
  def __next__
1329
- if self.length == 0
1330
- raise Error::RewriteEmptyStream.new(@element_description)
1331
- end
1332
- if @cursor >= self.length
1333
- self.length == 1 and return self.to_tree(@single_element)
1334
- raise RewriteCardinalityError.new(@element_description)
1335
- end
1336
- unless @single_element.nil?
1266
+ l = length
1267
+ case
1268
+ when l.zero?
1269
+ raise Error::RewriteEmptyStream.new( @element_description )
1270
+ when @cursor >= l
1271
+ l == 1 and return to_tree( @single_element )
1272
+ raise RewriteCardinalityError.new( @element_description )
1273
+ when @single_element
1274
+ @cursor += 1
1275
+ return( to_tree( @single_element ) )
1276
+ else
1277
+ out = to_tree( @elements.at( @cursor ) )
1337
1278
  @cursor += 1
1338
- return self.to_tree(@single_element)
1279
+ return( out )
1339
1280
  end
1340
- out = self.to_tree(@elements[@cursor])
1341
- @cursor += 1
1342
- return out
1343
1281
  end
1344
1282
  end
1345
1283
 
@@ -1351,20 +1289,14 @@ rewriting parsers.
1351
1289
 
1352
1290
  =end
1353
1291
  class RewriteRuleTokenStream < RewriteRuleElementStream
1354
- def to_tree(el)
1355
- return el
1356
- end
1357
-
1358
1292
  def next_node
1359
- t = __next__
1360
- return @adaptor.create_with_payload!(t)
1293
+ return @adaptor.create_with_payload!( __next__ )
1361
1294
  end
1362
1295
 
1363
- def next
1364
- return __next__()
1365
- end
1296
+ alias :next :__next__
1297
+ public :next
1366
1298
 
1367
- def dup(el)
1299
+ def dup( el )
1368
1300
  raise TypeError, "dup can't be called for a token stream"
1369
1301
  end
1370
1302
  end
@@ -1378,16 +1310,14 @@ rewriting parsers.
1378
1310
 
1379
1311
  class RewriteRuleSubtreeStream < RewriteRuleElementStream
1380
1312
  def next_node
1381
- if @dirty or (@cursor >= self.length) && (self.length == 1)
1382
- el = __next__
1383
- return @adaptor.copy_node(el)
1313
+ if @dirty or @cursor >= length && length == 1
1314
+ return @adaptor.copy_node( __next__ )
1384
1315
  end
1385
- el = __next__
1386
- return el
1316
+ return __next__
1387
1317
  end
1388
1318
 
1389
- def dup(el)
1390
- @adaptor.copy_tree(el)
1319
+ def dup( el )
1320
+ @adaptor.copy_tree( el )
1391
1321
  end
1392
1322
  end
1393
1323
 
@@ -1399,13 +1329,13 @@ rewriting parsers.
1399
1329
  =end
1400
1330
 
1401
1331
  class RewriteRuleNodeStream < RewriteRuleElementStream
1402
- def next_node
1403
- __next__
1332
+ alias next_node __next__
1333
+ public :next_node
1334
+ def to_tree( el )
1335
+ @adaptor.copy_node( el )
1404
1336
  end
1405
- def to_tree(el)
1406
- @adaptor.copy_node(el)
1407
- end
1408
- def dup(el)
1337
+
1338
+ def dup( el )
1409
1339
  raise TypeError, "dup can't be called for a node stream"
1410
1340
  end
1411
1341
  end