• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/ruby
2
3module ANTLR3
4module Template
5Parameter = Struct.new( :name, :default )
6class Parameter
7  def to_s
8    if block then "&#{ name }"
9    elsif splat then "*#{ name }"
10    elsif default then "#{ name } = #{ default }"
11    else name.dup
12    end
13  end
14end
15
16class ParameterList < ::Array
17  attr_accessor :splat, :block
18
19  def self.default
20    new.add( :values ) do | p |
21      p.default = '{}'
22    end
23  end
24
25  def names
26    names = map { | param | param.name.to_s }
27    @splat and names << @splat.to_s
28    @block and names << @block.to_s
29    return( names )
30  end
31
32  def add( name, default = nil )
33    param =
34      case name
35      when Parameter then name
36      else Parameter.new( name.to_s )
37      end
38    if options
39      default = options[ :default ] and param.default = default
40      param.splat = options.fetch( :splat, false )
41      param.block = options.fetch( :block, false )
42    end
43    block_given? and yield( param )
44    push( param )
45    return( self )
46  end
47
48  def to_s
49    signature = join( ', ' )
50    @splat and signature << ", *" << @splat.to_s
51    @block and signature << ", &" << @block.to_s
52    return( signature )
53  end
54end
55end
56end
57