Class: Rley::Parser::ParseState

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aDottedRule, theOrigin) ⇒ ParseState

Returns a new instance of ParseState.



12
13
14
15
# File 'lib/rley/parser/parse_state.rb', line 12

def initialize(aDottedRule, theOrigin)
  @dotted_rule = valid_dotted_rule(aDottedRule)
  @origin = theOrigin
end

Instance Attribute Details

#dotted_ruleObject (readonly)

Returns the value of attribute dotted_rule.



6
7
8
# File 'lib/rley/parser/parse_state.rb', line 6

def dotted_rule
  @dotted_rule
end

#originObject (readonly)

the position in the input that matches the beginning of the rhs of the production.



10
11
12
# File 'lib/rley/parser/parse_state.rb', line 10

def origin
  @origin
end

Instance Method Details

#==(other) ⇒ Object

Equality comparison. A parse state behaves as a value object.



18
19
20
21
22
23
# File 'lib/rley/parser/parse_state.rb', line 18

def ==(other)
  return true if equal?(other)

  (dotted_rule == other.dotted_rule) &&
    (origin == other.origin)
end

#complete?Boolean

Returns true if the dot is at the end of the rhs of the production. In other words, the complete rhs matches the input.

Returns:

  • (Boolean)


27
28
29
# File 'lib/rley/parser/parse_state.rb', line 27

def complete?
  dotted_rule.reduce_item?
end

#next_symbolObject

Next expected symbol in the production



37
38
39
# File 'lib/rley/parser/parse_state.rb', line 37

def next_symbol
  dotted_rule.next_symbol
end

#precedes?(other) ⇒ Boolean

Does this parse state have the 'other' as successor?

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rley/parser/parse_state.rb', line 42

def precedes?(other)
  return false if other == self

  return false unless origin == other.origin

  other_production = other.dotted_rule.production
  return false unless dotted_rule.production == other_production

  prev_position = other.dotted_rule.prev_position
  if prev_position.nil?
    false
  else
    dotted_rule.position == prev_position
  end
end

#predicted?Boolean

Returns true if the dot is at the start of the rhs of the production.

Returns:

  • (Boolean)


32
33
34
# File 'lib/rley/parser/parse_state.rb', line 32

def predicted?
  dotted_rule.predicted_item?
end

#to_sString

Give a String representation of itself. The format of the text representation is "format of dotted rule" + " | " + origin

Returns:

  • (String)


62
63
64
# File 'lib/rley/parser/parse_state.rb', line 62

def to_s
  dotted_rule.to_s + " | #{origin}"
end