aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Ruby/lib/antlr3/template/parameter.rb
blob: c3341c6ab26f5b88437958880ced64cbc6a54118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/ruby

module ANTLR3
module Template
Parameter = Struct.new( :name, :default )
class Parameter
  def to_s
    if block then "&#{ name }"
    elsif splat then "*#{ name }"
    elsif default then "#{ name } = #{ default }"
    else name.dup
    end
  end
end

class ParameterList < ::Array
  attr_accessor :splat, :block
  
  def self.default
    new.add( :values ) do | p |
      p.default = '{}'
    end
  end
  
  def names
    names = map { | param | param.name.to_s }
    @splat and names << @splat.to_s
    @block and names << @block.to_s
    return( names )
  end
  
  def add( name, default = nil )
    param =
      case name
      when Parameter then name
      else Parameter.new( name.to_s )
      end
    if options
      default = options[ :default ] and param.default = default
      param.splat = options.fetch( :splat, false )
      param.block = options.fetch( :block, false )
    end
    block_given? and yield( param )
    push( param )
    return( self )
  end
  
  def to_s
    signature = join( ', ' )
    @splat and signature << ", *" << @splat.to_s
    @block and signature << ", &" << @block.to_s
    return( signature )
  end
end
end
end