Module: Rley::Base::GrmItemsBuilder

Included in:
BaseParser
Defined in:
lib/rley/base/grm_items_builder.rb

Overview

Mix-in module. Builds the dotted items for a given grammar

Instance Method Summary collapse

Instance Method Details

#build_dotted_items(aGrammar) ⇒ Array<DottedItem>

Build an array of dotted items from the productions of passed grammar.

Parameters:

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rley/base/grm_items_builder.rb', line 12

def build_dotted_items(aGrammar)
  items = []
  aGrammar.rules.each do |prod|
    index_prev = items.size
    rhs_size = prod.rhs.size
    if rhs_size.zero?
      items << DottedItem.new(prod, 0)
    else
      items += (0..rhs_size).map { |i| DottedItem.new(prod, i) }
    end

    prod.constraints.each do |cs|
      # Attach constraint to dotted item n + 1
      items[index_prev + cs.idx_symbol + 1].constraint = cs
    end
  end

  return items
end