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.



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

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.

Returns:

  • (String, Numeric)


29
30
31
# File 'lib/antelope/generation/recognizer/state.rb', line 29

def id
  @id
end

#rulesSet<Rule> (readonly)

All of the rules in this state.

Returns:



17
18
19
# File 'lib/antelope/generation/recognizer/state.rb', line 17

def rules
  @rules
end

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

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

Returns:

  • (Hash<(Symbol, State)>)


22
23
24
# File 'lib/antelope/generation/recognizer/state.rb', line 22

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.

Parameters:

  • rule (State, Rule)

    the object to append.

Returns:

  • (self)

Raises:



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

def <<(rule)
  if rule.is_a? State
    rule.rules.map(&:clone).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

#===(other) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/antelope/generation/recognizer/state.rb', line 103

def ===(other)
  return super unless other.is_a? State

  other_rules = other.rules.to_a
  other.transitions == transitions and
    rules.each_with_index.
    all? { |rule, i| rule === other_rules[i] }
end

#inspectString

Gives a nice string representation of the state.

Returns:

  • (String)


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

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.

Parameters:

  • other (State)

    the state to merge.

Returns:

  • (self)

Raises:

  • (ArgumentError)

    if the given argument is not a state.



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

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.

Parameters:

  • production (Rule)

    the rule to compare.

Returns:



75
76
77
# File 'lib/antelope/generation/recognizer/state.rb', line 75

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