Class: Treetop::Compiler::SequenceElementAccessorModule

Inherits:
Object
  • Object
show all
Includes:
InlineModuleMixin
Defined in:
lib/treetop/compiler/node_classes/sequence.rb

Instance Attribute Summary collapse

Attributes included from InlineModuleMixin

#module_name

Instance Method Summary collapse

Constructor Details

#initialize(sequence_elements) ⇒ SequenceElementAccessorModule

Returns a new instance of SequenceElementAccessorModule.



49
50
51
# File 'lib/treetop/compiler/node_classes/sequence.rb', line 49

def initialize(sequence_elements)
  @sequence_elements = sequence_elements
end

Instance Attribute Details

#sequence_elementsObject (readonly)

Returns the value of attribute sequence_elements.



47
48
49
# File 'lib/treetop/compiler/node_classes/sequence.rb', line 47

def sequence_elements
  @sequence_elements
end

Instance Method Details

#compile(idx, builder, rule) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/treetop/compiler/node_classes/sequence.rb', line 53

def compile(idx, builder, rule)
  super
  builder.module_declaration(module_name) do
    elements_by_name = sequence_elements.inject({}){|h,e| (h[e.label_name.to_s] ||= []) << e; h}
    sequence_elements.each_with_index do |element, index|
      if element.label_name
        repetitions = elements_by_name[element.label_name.to_s]
        label_name = element.label_name + (repetitions.size > 1 ? (repetitions.index(element)+1).to_s : "")
        builder.method_declaration(label_name) do
          builder << "elements[#{index}]"
        end
        builder.newline unless index == sequence_elements.size - 1
      end
    end
  end
end