Class: Rley::Parser::DottedItem

Inherits:
Object
  • Object
show all
Defined in:
lib/rley/parser/dotted_item.rb

Overview

This module is used as a namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aProduction, aPosition) ⇒ DottedItem

Returns a new instance of DottedItem.

Parameters:

  • aProduction


28
29
30
31
# File 'lib/rley/parser/dotted_item.rb', line 28

def initialize(aProduction, aPosition)
  @production = aProduction
  @position = valid_position(aPosition)
end

Instance Attribute Details

#positionObject (readonly)

Index of the next symbol (from the rhs) after the 'dot'. If the dot is at the end of the rhs (i.e.) there is no next symbol, then the position takes the value -1. It the rhs is empty, then the position is -2



25
26
27
# File 'lib/rley/parser/dotted_item.rb', line 25

def position
  @position
end

#productionObject (readonly)

Production rule



19
20
21
# File 'lib/rley/parser/dotted_item.rb', line 19

def production
  @production
end

Instance Method Details

#at_start?Boolean Also known as: predicted_item?

Return true if the dot position is at the start of the rhs.

Returns:

  • (Boolean)


49
50
51
# File 'lib/rley/parser/dotted_item.rb', line 49

def at_start?()
  return position.zero? || position == -2
end

#lhsObject

The non-terminal symbol that is on the left-side of the production



63
64
65
# File 'lib/rley/parser/dotted_item.rb', line 63

def lhs()
  return production.lhs
end

#next_symbolObject

Return the symbol after the dot. nil is returned if the dot is at the end



82
83
84
# File 'lib/rley/parser/dotted_item.rb', line 82

def next_symbol()
  return position < 0 ? nil : production.rhs[position]
end

#prev_positionObject

Calculate the position of the dot if were moved by one step on the left.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rley/parser/dotted_item.rb', line 88

def prev_position()
  case position
    when -2, 0
      result = nil
    when -1
      result = production.rhs.size == 1 ? 0 : production.rhs.size - 1
    else
      result = position - 1
  end

  return result
end

#prev_symbolObject

Return the symbol before the dot. nil is returned if the dot is at the start of the rhs



69
70
71
72
73
74
75
76
77
78
# File 'lib/rley/parser/dotted_item.rb', line 69

def prev_symbol()
  before_position = prev_position
  result = if before_position.nil?
             nil
           else
             production.rhs[before_position]
           end

  return result
end

#reduce_item?Boolean

A dotted item is called a reduce item if the dot is at the end.

Returns:

  • (Boolean)


58
59
60
# File 'lib/rley/parser/dotted_item.rb', line 58

def reduce_item?()
  return position < 0 # Either -1 or -2
end

#successor_of?(another) ⇒ Boolean

Return true if this dotted item has a dot one place to the right compared to the dotted item argument.

Returns:

  • (Boolean)


103
104
105
106
107
108
# File 'lib/rley/parser/dotted_item.rb', line 103

def successor_of?(another)
  return false if production != another.production
  to_the_left = prev_position
  return false if to_the_left.nil?
  return to_the_left == another.position
end

#to_sString

Return a String representation of the dotted item.

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rley/parser/dotted_item.rb', line 35

def to_s()
  prefix = "#{production.lhs} => "
  text_values = production.rhs.map(&:to_s)
  if position < 0
    text_values << '.'
  else
    text_values.insert(position, '.')
  end
  suffix = text_values.join(' ')

  return prefix + suffix
end