Class: Item

Inherits:
Object show all
Includes:
Indexable
Defined in:
lib/rpdf2txt-rockit/parsetable_generation.rb

Constant Summary collapse

@@empty_array =
Array.new

Instance Attribute Summary collapse

Attributes included from Indexable

#factory, #index_number

Instance Method Summary collapse

Constructor Details

#initialize(production, position, lookahead = nil, nextItem = nil) ⇒ Item

Returns a new instance of Item.



11
12
13
14
15
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 11

def initialize(production, position, lookahead = nil, nextItem = nil)
  @production, @position, @lookahead = production, position, lookahead
  @symbol = production.elements[position]
  @hash_value = [production, position, lookahead].hash
end

Instance Attribute Details

#lookaheadObject (readonly)

Returns the value of attribute lookahead.



9
10
11
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 9

def lookahead
  @lookahead
end

#positionObject (readonly)

Returns the value of attribute position.



9
10
11
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 9

def position
  @position
end

#productionObject (readonly)

Returns the value of attribute production.



9
10
11
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 9

def production
  @production
end

#symbolObject (readonly)

Returns the value of attribute symbol.



9
10
11
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 9

def symbol
  @symbol
end

Instance Method Details

#calc_followersObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 21

def calc_followers
  return @@empty_array if position >= production.elements.length-1
  elements = production.elements[position+1..-1]
  followers = Array.new
  elements.each do |e|
    followers.push e
    return followers unless e.derives_epsilon?
  end
  followers
end

#direct_following_symbolsObject



17
18
19
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 17

def direct_following_symbols
  @direct_following_symbols || (@direct_following_symbols = calc_followers)
end

#final?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 42

def final?
  next_item == nil
end

#inspectObject



54
55
56
57
58
59
60
61
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 54

def inspect
  endpos = ((@position >= production.elements.length) or 
     (@position == 0))
  production.nonterminal.inspect + "->" +
    inspect_elements(production.elements[0...@position]) +
    (endpos ? "." : " .") +
    inspect_elements(production.elements[@position..-1])
end

#lookahead_item(lookaheadSymbol) ⇒ Object



50
51
52
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 50

def lookahead_item(lookaheadSymbol)
  make_new_item(production, position, lookaheadSymbol, next_item)
end

#next_itemObject



46
47
48
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 46

def next_item
  @next_item || (@next_item = calc_next_item)
end

#suffixObject



34
35
36
37
38
39
40
# File 'lib/rpdf2txt-rockit/parsetable_generation.rb', line 34

def suffix
  if position < production.elements.length-1
    production.elements[position+1..-1]
  else
    nil
  end
end