1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
9 attr_reader :name, :opts, :children
11 def initialize(name, opts = { })
22 if @opts[:merge_point]
23 @children.insert(@opts[:merge_point], desc)
24 @opts[:merge_point] += 1
31 "(#{@name} #{@opts.inspect}#{@children.map{|c| ' ' + c.to_sexp}.join})"
34 def merge!(other, prefix = "")
35 if name == other.name and
36 opts[:name] == other.opts[:name]
37 other.children.each do |child2|
39 children.each do |child|
40 if child.merge!(child2, prefix + " ")
45 merge_child(child2.dup) unless merged
61 def method_missing(name, *args, &blk)
65 if args.first.is_a? Hash
68 { :name => args.first }
71 args[-1].merge(:name => args.first)
73 child = Descriptor.new(name, opts)
74 blk[self.class.new(child)] if block_given?
75 __desc__.add_child(child)
79 @__desc__.opts[:merge_point] = @__desc__.children.size