antlr3 1.4.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -176,7 +176,7 @@ end
176
176
 
177
177
  class File
178
178
 
179
- # Given some target path string, and an optional reference path
179
+ # given some target path string, and an optional reference path
180
180
  # (Dir.pwd by default), this method returns a string containing
181
181
  # the relative path of the target path from the reference path
182
182
  #
data/lib/antlr3/token.rb CHANGED
@@ -281,21 +281,21 @@ module TokenSource
281
281
 
282
282
  def next
283
283
  token = next_token()
284
- raise StopIteration if token.nil? or token.type == EOF
284
+ raise StopIteration if token.nil? || token.type == EOF
285
285
  return token
286
286
  end
287
287
 
288
288
  def to_stream(options = {})
289
289
  if block_given?
290
- CommonTokenStream.new(self, options) { |t| yield(t) }
290
+ CommonTokenStream.new( self, options ) { | t, stream | yield( t, stream ) }
291
291
  else
292
- CommonTokenStream.new(self, options)
292
+ CommonTokenStream.new( self, options )
293
293
  end
294
294
  end
295
295
 
296
296
  def each
297
297
  block_given? or return enum_for(:each)
298
- loop { yield(self.next) }
298
+ loop { yield( self.next ) }
299
299
  rescue StopIteration
300
300
  return self
301
301
  end
data/lib/antlr3/tree.rb CHANGED
@@ -287,28 +287,8 @@ module Tree
287
287
  # self.ancestors.each { |anc| yield(anc) }
288
288
  #end
289
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
290
 
309
- #def prune
310
- # raise StopIteration
311
- #end
291
+
312
292
 
313
293
  #alias :each :walk
314
294
  #
@@ -319,7 +299,7 @@ module Tree
319
299
  #def leaf?
320
300
  # children.empty?
321
301
  #end
322
- #
302
+
323
303
  #def ancestors
324
304
  # a = []
325
305
  # cursor = self
@@ -400,7 +380,7 @@ class BaseTree < ::Array
400
380
  killed = delete_at( index ) and freshen( index )
401
381
  return killed
402
382
  end
403
-
383
+
404
384
  def replace_children( start, stop, new_tree )
405
385
  start >= length or stop >= length and
406
386
  raise IndexError, (<<-END).gsub!(/^\s+\| /,'')
@@ -446,6 +426,29 @@ class BaseTree < ::Array
446
426
  return( buffer )
447
427
  end
448
428
 
429
+ def walk
430
+ block_given? or return( enum_for( :walk ) )
431
+ stack = []
432
+ cursor = self
433
+ while true
434
+ begin
435
+ yield( cursor )
436
+ stack.push( Array[ *cursor ] ) unless cursor.empty?
437
+ rescue StopIteration
438
+ # skips adding children to prune the node
439
+ ensure
440
+ break if stack.empty?
441
+ cursor = stack.last.shift
442
+ stack.pop if stack.last.empty?
443
+ end
444
+ end
445
+ return self
446
+ end
447
+
448
+ def prune
449
+ raise StopIteration
450
+ end
451
+
449
452
  abstract :to_s
450
453
  #protected :sanity_check, :freshen
451
454
  end
@@ -498,7 +501,6 @@ class CommonTree < BaseTree
498
501
  @parent = nil
499
502
  end
500
503
 
501
-
502
504
  def copy_node
503
505
  return self.class.new( @token )
504
506
  end
@@ -528,7 +530,6 @@ class CommonTree < BaseTree
528
530
  end
529
531
  return @token.column
530
532
  end
531
-
532
533
 
533
534
  def start_index
534
535
  @start_index == -1 and @token and return @token.index
@@ -843,7 +844,7 @@ class CommonTreeAdaptor
843
844
  end
844
845
 
845
846
  def create_flat_list!
846
- return self.create_with_payload!( nil )
847
+ return create_with_payload!( nil )
847
848
  end
848
849
 
849
850
  def become_root( new_root, old_root )
@@ -884,6 +885,7 @@ class CommonTreeAdaptor
884
885
  def create_with_payload!( payload )
885
886
  return CommonTree.new( payload )
886
887
  end
888
+
887
889
  def create!( *args )
888
890
  n = args.length
889
891
  if n == 1 and args.first.is_a?( Token ) then create_with_payload!( args[0] )
@@ -911,7 +913,7 @@ class CommonTreeAdaptor
911
913
  end
912
914
 
913
915
  def each_child( tree )
914
- block_given? or return enum_for( __method__, tree )
916
+ block_given? or return enum_for( :each_child, tree )
915
917
  tree.each do | child |
916
918
  yield( child )
917
919
  end
@@ -979,14 +981,14 @@ class CommonTreeNodeStream
979
981
  attr_reader :adaptor, :position
980
982
 
981
983
  def initialize(*args)
984
+ options = args.last.is_a?( ::Hash ) ? args.pop : {}
982
985
  case n = args.length
983
986
  when 1
984
- @adaptor = CommonTreeAdaptor.new
985
987
  @root = args.first
986
- @nodes = @down = @up = @eof = nil
988
+ @token_stream = @adaptor = @nodes = @down = @up = @eof = nil
987
989
  when 2
988
990
  @adaptor, @root = args
989
- @nodes = @down = @up = @eof = nil
991
+ @token_stream = @nodes = @down = @up = @eof = nil
990
992
  when 3
991
993
  parent, start, stop = *args
992
994
  @adaptor = parent.adaptor
@@ -995,17 +997,19 @@ class CommonTreeNodeStream
995
997
  @down = parent.down
996
998
  @up = parent.up
997
999
  @eof = parent.eof
1000
+ @token_stream = parent.token_stream
998
1001
  when 0
999
1002
  raise ArgumentError, "wrong number of arguments (0 for 1)"
1000
1003
  else raise ArgumentError, "wrong number of arguments (#{ n } for 3)"
1001
1004
  end
1002
- @down ||= @adaptor.create_from_type!( DOWN, 'DOWN' )
1003
- @up ||= @adaptor.create_from_type!( UP, 'UP' )
1004
- @eof ||= @adaptor.create_from_type!( EOF, 'EOF' )
1005
+ @adaptor ||= options.fetch( :adaptor ) { CommonTreeAdaptor.new }
1006
+ @token_stream ||= options[ :token_stream ]
1007
+ @down ||= options.fetch( :down ) { @adaptor.create_from_type!( DOWN, 'DOWN' ) }
1008
+ @up ||= options.fetch( :up ) { @adaptor.create_from_type!( UP, 'UP' ) }
1009
+ @eof ||= options.fetch( :eof ) { @adaptor.create_from_type!( EOF, 'EOF' ) }
1005
1010
  @nodes ||= []
1006
- @token_stream = nil
1007
1011
 
1008
- @unique_navigation_nodes = false
1012
+ @unique_navigation_nodes = options.fetch( :unique_navigation_nodes, false )
1009
1013
  @position = -1
1010
1014
  @last_marker = nil
1011
1015
  @calls = []
@@ -9,10 +9,11 @@ Adds debugging event hooks to TreeAdaptor objects
9
9
 
10
10
  =end
11
11
  module TreeAdaptor
12
- def self.wrap(adaptor, debug_listener = nil)
12
+
13
+ def self.wrap( adaptor, debug_listener = nil )
13
14
  adaptor.extend(self)
14
15
  adaptor.debug_listener = debug_listener
15
- return(adaptor)
16
+ return( adaptor )
16
17
  end
17
18
 
18
19
  attr_accessor :debug_listener
@@ -19,7 +19,7 @@ module ANTLR3
19
19
  # The version data for the current state the library itself
20
20
  #
21
21
  MAJOR_VERSION = 1
22
- MINOR_VERSION = 4
22
+ MINOR_VERSION = 6
23
23
  PATCH_VERSION = 0
24
24
  VERSION = [ MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION ]
25
25
  VERSION_STRING = VERSION.join('.').freeze
data/templates/Ruby.stg CHANGED
@@ -44,7 +44,7 @@ Current load path:
44
44
  END
45
45
  end
46
46
 
47
- defined?(ANTLR3) or begin
47
+ defined?( ANTLR3 ) or begin
48
48
 
49
49
  # 1: try to load the ruby antlr3 runtime library from the system path
50
50
  require 'antlr3'
@@ -52,7 +52,7 @@ defined?(ANTLR3) or begin
52
52
  rescue LoadError
53
53
 
54
54
  # 2: try to load rubygems if it isn't already loaded
55
- defined?(Gem) or begin
55
+ defined?( Gem ) or begin
56
56
  require 'rubygems'
57
57
  rescue LoadError
58
58
  antlr_load_failed.call
@@ -60,7 +60,7 @@ rescue LoadError
60
60
 
61
61
  # 3: try to activate the antlr3 gem
62
62
  begin
63
- Gem.activate( 'antlr3', '= <runtimeLibraryVersion()>' )
63
+ Gem.activate( 'antlr3', '~> <runtimeLibraryVersion()>' )
64
64
  rescue Gem::LoadError
65
65
  antlr_load_failed.call
66
66
  end
@@ -97,7 +97,7 @@ module TokenData
97
97
  <if(tokens)>
98
98
 
99
99
  # define the token constants
100
- define_tokens(<tokens:{:<it.name> => <it.type>}; anchor, wrap="\n", separator=", ">)
100
+ define_tokens( <tokens:{:<it.name> => <it.type>}; anchor, wrap="\n", separator=", "> )
101
101
 
102
102
  <endif>
103
103
  <if(tokenNames)>
@@ -108,7 +108,7 @@ module TokenData
108
108
  # this is necessary because anonymous tokens, which are
109
109
  # created from literal values in the grammar, do not
110
110
  # have descriptive names
111
- register_names(<tokenNames:{<it>}; separator=", ", anchor, wrap="\n">)
111
+ register_names( <tokenNames:{<it>}; separator=", ", anchor, wrap="\n"> )
112
112
 
113
113
  <endif>
114
114
 
@@ -189,7 +189,7 @@ parserBody(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
189
189
  <if(!grammar.grammarIsRoot)><autoloadDelegates()><\n><endif>
190
190
  <@mixins()>
191
191
 
192
- RULE_METHODS = [<rules:{r|:<r.ruleName>}; separator=", ", wrap="\n", anchor>].freeze
192
+ RULE_METHODS = [ <rules:{r|:<r.ruleName>}; separator=", ", wrap="\n", anchor> ].freeze
193
193
 
194
194
  <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeClass(scope=it)><\n><endif>}>
195
195
  <rules:{<ruleAttributeScopeClass(scope=it.ruleDescriptor.ruleScope)>}>
@@ -234,8 +234,8 @@ end
234
234
  >>
235
235
 
236
236
  parserConstructor() ::= <<
237
- def initialize(<grammar.delegators:{g|<g:delegateName()>, }>input, options = {})
238
- super(input, options)
237
+ def initialize( <grammar.delegators:{g|<g:delegateName()>, }>input, options = {} )
238
+ super( input, options )
239
239
  <if(memoize)><if(grammar.grammarIsRoot)>
240
240
  @state.rule_memory = {}
241
241
  <endif><endif>
@@ -257,10 +257,15 @@ end
257
257
  * need to be in a rule by themselves.
258
258
  */
259
259
  synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::= <<
260
- # parser rule <ruleName; format="lexerRule"> (<ruleName>)
260
+ #
261
+ # syntactic predicate <ruleName; format="lexerRule">
261
262
  #
262
263
  # (in <fileName>)
263
264
  # <description>
265
+ #
266
+ # This is an imaginary rule inserted by ANTLR to
267
+ # implement a syntactic predicate decision
268
+ #
264
269
  def <ruleName; format="lexerRule"><if(ruleDescriptor.parameterScope)>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)<endif>
265
270
  <traceIn()><ruleLabelDefs()>
266
271
  <block>
@@ -276,10 +281,12 @@ end
276
281
  rule(ruleName, ruleDescriptor, block, emptyRule, description, exceptions, finally, memoize) ::= <<
277
282
  <returnScope(scope=ruleDescriptor.returnScope)>
278
283
 
284
+ #
279
285
  # parser rule <ruleName>
280
286
  #
281
287
  # (in <fileName>)
282
288
  # <description>
289
+ #
283
290
  def <ruleName><if(ruleDescriptor.parameterScope)>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)<endif>
284
291
  <traceIn()><ruleScopeSetUp()><ruleDeclarations()><ruleLabelDefs()><action(name="init", code=ruleDescriptor.actions.init)>
285
292
  <@body><ruleBody()><@end>
@@ -292,7 +299,6 @@ end
292
299
  <endif>
293
300
  >>
294
301
 
295
-
296
302
  delegateRule(ruleDescriptor) ::= <<
297
303
  # delegated rule <ruleDescriptor.name>
298
304
  def <ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
@@ -333,7 +339,7 @@ rescue <e.decl>
333
339
  >>
334
340
 
335
341
  closureBlockLoop() ::= <<
336
- loop do # decision <decisionNumber>
342
+ while true # decision <decisionNumber>
337
343
  alt_<decisionNumber> = <maxAlt>
338
344
  <@decisionBody><decision><@end>
339
345
  case alt_<decisionNumber>
@@ -356,12 +362,12 @@ element() ::= <<
356
362
  execForcedAction(action) ::= "<action>"
357
363
 
358
364
  globalAttributeScopeClass(scope) ::= <<
359
- <if(scope.attributes)>Scope<scope.name> = Struct.new(<scope.attributes:{:<it.decl>}; separator=", ">)<endif>
365
+ <if(scope.attributes)>@@<scope.name> = Struct.new( <scope.attributes:{:<it.decl>}; separator=", "> )<\n><endif>
360
366
  >>
361
367
 
362
368
 
363
369
  globalAttributeScopeStack(scope) ::= <<
364
- <if(scope.attributes)>@<scope.name>_stack = []<endif>
370
+ <if(scope.attributes)>@<scope.name>_stack = []<\n><endif>
365
371
  >>
366
372
 
367
373
 
@@ -375,7 +381,7 @@ parameterScope(scope) ::= <<
375
381
 
376
382
  positiveClosureBlockLoop() ::= <<
377
383
  match_count_<decisionNumber> = 0
378
- loop do
384
+ while true
379
385
  alt_<decisionNumber> = <maxAlt>
380
386
  <@decisionBody><decision><@end>
381
387
  case alt_<decisionNumber>
@@ -390,6 +396,7 @@ loop do
390
396
  match_count_<decisionNumber> += 1
391
397
  end<\n>
392
398
  >>
399
+
393
400
  returnScope(scope) ::= <<
394
401
  <if(ruleDescriptor.hasMultipleReturnValues)>
395
402
  <ruleDescriptor:returnStructName(r=it)> = define_return_scope <scope.attributes:{:<it.decl>}; separator=", ">
@@ -398,21 +405,8 @@ returnScope(scope) ::= <<
398
405
 
399
406
 
400
407
  returnStructName(r) ::= "<r.name; format=\"camelcase\">ReturnValue"
401
-
402
-
403
- ruleAttributeScopeClass(scope) ::= <<
404
- <if(scope.attributes)>
405
- Scope<scope.name> = Struct.new(<scope.attributes:{:<it.decl>}; separator=", ">)<\n>
406
- <endif>
407
- >>
408
-
409
-
410
- ruleAttributeScopeStack(scope) ::= <<
411
- <if(scope.attributes)>
412
- @<scope.name>_stack = []<\n>
413
- <endif>
414
- >>
415
-
408
+ ruleAttributeScopeClass ::= globalAttributeScopeClass
409
+ ruleAttributeScopeStack ::= globalAttributeScopeStack
416
410
 
417
411
  ruleBacktrackFailure() ::= <<
418
412
  <if(backtracking)>
@@ -420,7 +414,6 @@ ruleBacktrackFailure() ::= <<
420
414
  <endif>
421
415
  >>
422
416
 
423
-
424
417
  ruleBody() ::= <<
425
418
  <if(memoize)><if(backtracking)>
426
419
  success = false # flag used for memoization<\n>
@@ -527,7 +520,7 @@ return_value.stop = @input.look(-1)<\n>
527
520
  ruleMemoization(name) ::= <<
528
521
  <if(memoize)>
529
522
  # rule memoization
530
- if @state.backtracking > 0 and already_parsed_rule?(__method__)
523
+ if @state.backtracking > 0 and already_parsed_rule?( __method__ )
531
524
  success = true
532
525
  return <ruleReturnValue()>
533
526
  end<\n>
@@ -536,7 +529,7 @@ end<\n>
536
529
 
537
530
 
538
531
  ruleScopeSetUp() ::= <<
539
- <ruleDescriptor.useScopes:{@<it>_stack.push(Scope<it>.new)<\n>}><ruleDescriptor.ruleScope:{@<it.name>_stack.push(Scope<it.name>.new)<\n>}>
532
+ <ruleDescriptor.useScopes:{@<it>_stack.push(@@<it>.new)<\n>}><ruleDescriptor.ruleScope:{@<it.name>_stack.push(@@<it.name>.new)<\n>}>
540
533
  >>
541
534
 
542
535
 
@@ -751,6 +744,7 @@ match(UP, nil)
751
744
  */
752
745
  validateSemanticPredicate(pred,description) ::= <<
753
746
  unless (<evalPredicate(...)>)
747
+ <ruleBacktrackFailure()>
754
748
  raise FailedPredicate("<ruleName>", "<description>")
755
749
  end
756
750
  >>
@@ -1497,4 +1491,4 @@ placeAction(scope, name) ::= <<
1497
1491
  <endif>
1498
1492
  >>
1499
1493
 
1500
- runtimeLibraryVersion() ::= "1.4.0"
1494
+ runtimeLibraryVersion() ::= "1.6.0"
data/templates/ST.stg CHANGED
@@ -123,6 +123,3 @@ rulePropertyRef_text(scope, attr) ::= <<
123
123
  argumentMap(args) ::= <<
124
124
  <args:{a|:<a.name> => <a.value>}; separator=",\n">
125
125
  >>
126
-
127
-
128
-
@@ -5,7 +5,7 @@ require 'antlr3/test/functional'
5
5
 
6
6
  class TestASTConstructingParser < ANTLR3::Test::Functional
7
7
 
8
- inline_grammar(<<-'END')
8
+ compile inline_grammar( <<-'END' )
9
9
  grammar ASTBuilder;
10
10
  options {
11
11
  language = Ruby;
@@ -6,6 +6,18 @@ require 'test/unit'
6
6
  require 'spec'
7
7
 
8
8
  include ANTLR3::Error
9
+
10
+ describe( ANTLR3::Error ) do
11
+
12
+ example "raising an ANTLR bug exception" do
13
+ proc {
14
+ ANTLR3.bug!( 'whateva' )
15
+ }.should raise_error( ANTLR3::Bug )
16
+ end
17
+
18
+
19
+ end
20
+
9
21
  #
10
22
  #class TestRecognitionError < Test::Unit::TestCase
11
23
  # def test_init_none