Class: Antelope::Generation::Recognizer::State

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/antelope/generation/recognizer/state.rb

Overview

A state within the parser. A state has a set of rules, as well as transitions on those rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Initialize the state.



35
36
37
38
39
# File 'lib/antelope/generation/recognizer/state.rb', line 35

def initialize
  @rules = Set.new
  @transitions = {}
  @id = SecureRandom.hex
end

Instance Attribute Details

#idString, Numeric

The id of this state. This starts off as a string of hexadecmial characters, but after all of the states are finalized, this becomes a numeric.



27
28
29
# File 'lib/antelope/generation/recognizer/state.rb', line 27

def id
  @id
end

#rulesSet<Rule> (readonly)

All of the rules in this state.



15
16
17
# File 'lib/antelope/generation/recognizer/state.rb', line 15

def rules
  @rules
end

#transitionsHash<(Symbol, State)> (readonly)

All of the transitions that can be made on this state.



20
21
22
# File 'lib/antelope/generation/recognizer/state.rb', line 20

def transitions
  @transitions
end

Instance Method Details

#<<(rule) ⇒ self Also known as: push

Appends the given object to this state. The given object must be a state or a rule. If it's a state, it appends all of the rules in the state to this state. If it's a rule, it adds the rule to our rules.

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/antelope/generation/recognizer/state.rb', line 86

def <<(rule)
  if rule.is_a? State
    rule.rules.each { |r| self << r }
  elsif rule.is_a? Rule
    rules << rule unless rules.include? rule
  else
    raise ArgumentError, "Expected #{State} or #{Rule}, " \
      "got #{rule.class}"
  end

  self
end

#inspectString

Gives a nice string representation of the state.



44
45
46
47
48
# File 'lib/antelope/generation/recognizer/state.rb', line 44

def inspect
  "#<#{self.class} id=#{id} " \
    "transitions=[#{transitions.keys.join(", ")}] " \
    "rules=[{#{rules.to_a.join("} {")}}]>"
end

#merge!(other) ⇒ self

Merges another state with this state. It copies all of the rules into this state, and then merges the transitions on the given state to this state. It then returns self.

Raises:

  • (ArgumentError)

    if the given argument is not a state.



57
58
59
60
61
62
63
64
65
# File 'lib/antelope/generation/recognizer/state.rb', line 57

def merge!(other)
  raise ArgumentError, "Expected #{self.class}, " \
    "got #{other.class}" unless other.is_a? State

  self << other
  self.transitions.merge! other.transitions

  self
end

#rule_for(production) ⇒ Rule?

Finds the rule that match the given production. It uses fuzzy equality checking. It returns the first rule that matches.



73
74
75
# File 'lib/antelope/generation/recognizer/state.rb', line 73

def rule_for(production)
  rules.find { |rule| production === rule }
end