Class: Dhaka::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/dhaka/parser/item.rb

Overview

Represents parser state items

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(production, next_item_index) ⇒ Item

Returns a new instance of Item.



6
7
8
9
10
# File 'lib/dhaka/parser/item.rb', line 6

def initialize(production, next_item_index)
  @production      = production
  @next_item_index = next_item_index
  @lookaheadset    = Set.new
end

Instance Attribute Details

#lookaheadsetObject (readonly)

:nodoc:



4
5
6
# File 'lib/dhaka/parser/item.rb', line 4

def lookaheadset
  @lookaheadset
end

#next_item_indexObject (readonly)

:nodoc:



4
5
6
# File 'lib/dhaka/parser/item.rb', line 4

def next_item_index
  @next_item_index
end

#productionObject (readonly)

:nodoc:



4
5
6
# File 'lib/dhaka/parser/item.rb', line 4

def production
  @production
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dhaka/parser/item.rb', line 35

def eql?(other)
  production == other.production && next_item_index == other.next_item_index
end

#hashObject



39
40
41
# File 'lib/dhaka/parser/item.rb', line 39

def hash
  production.hash ^ next_item_index.hash
end

#next_itemObject



16
17
18
# File 'lib/dhaka/parser/item.rb', line 16

def next_item
  Item.new(production, @next_item_index + 1)
end

#next_symbolObject



12
13
14
# File 'lib/dhaka/parser/item.rb', line 12

def next_symbol
  production.expansion[next_item_index]
end

#to_s(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dhaka/parser/item.rb', line 20

def to_s(options = {})
  
  expansion_symbols = production.expansion.collect {|symbol| symbol.name}
  if next_item_index < expansion_symbols.size
    expansion_symbols.insert(next_item_index, '->')
  else
    expansion_symbols << '->'
  end

  expansion_repr = expansion_symbols.join(' ')
  item = "#{production.symbol} ::= #{expansion_repr}"
  item << " [#{lookaheadset.collect.sort.join('')}]"  unless options[:hide_lookaheads]
  item
end