Home | History | Annotate | Download | only in template
      1 #!/usr/bin/ruby
      2 
      3 module ANTLR3
      4 module Template
      5 Parameter = Struct.new( :name, :default )
      6 class 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
     14 end
     15 
     16 class 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
     54 end
     55 end
     56 end
     57