Class: Dhaka::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/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/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:



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

def lookaheadset
  @lookaheadset
end

#next_item_indexObject (readonly)

:nodoc:



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

def next_item_index
  @next_item_index
end

#productionObject (readonly)

:nodoc:



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

def production
  @production
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  @production.hash ^ @next_item_index.hash
end

#next_itemObject



18
19
20
# File 'lib/parser/item.rb', line 18

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

#next_symbolObject



11
12
13
14
15
16
17
# File 'lib/parser/item.rb', line 11

def next_symbol
  if @next_item_index < @production.expansion.size
    @production.expansion[@next_item_index]
  else
    nil
  end
end

#to_s(options = {}) ⇒ Object



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

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(' ')
  if options[:hide_lookaheads] 
    "#{@production.symbol} ::= #{expansion_repr}"
  else
    "#{@production.symbol} ::= #{expansion_repr} [#{@lookaheadset.collect.sort}]"
  end
end