Class: Antelope::Generation::Recognizer::Rule

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/antelope/generation/recognizer/rule.rb

Overview

Defines a rule. A rule has a corresponding production, and a position in that production. It also contains extra information for other reasons.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(production, position, inherited = false) ⇒ Rule

Initialize the rule.

Parameters:

  • production (Ace::Production)

    the production that this rule is based off of.

  • position (Numeric)

    the position that this rule is in the production.

  • inherited (nil) (defaults to: false)

    do not use.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/antelope/generation/recognizer/rule.rb', line 67

def initialize(production, position, inherited = false)
  @left       = production.label.dup
  @position   = position
  @lookahead  = Set.new
  @precedence = production.prec
  @production = production
  @block      = production.block
  @id         = SecureRandom.hex

  if inherited
    @left, @right = inherited
  else
    @right = production.items.map(&:dup)
  end
end

Instance Attribute Details

#blockString (readonly)

Deprecated.

Use Ace::Production#block instead.

The block to be executed on production match.

Returns:

  • (String)


33
34
35
# File 'lib/antelope/generation/recognizer/rule.rb', line 33

def block
  @block
end

#idString, Numeric

The id for this rule. Initialy, this is set to a string of hexadecimal characters; after construction of all states, however, it is a number.

Returns:

  • (String, Numeric)


46
47
48
# File 'lib/antelope/generation/recognizer/rule.rb', line 46

def id
  @id
end

#leftAce::Token::Nonterminal (readonly)

The left-hand side of the rule.



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

def left
  @left
end

#lookaheadSet<Symbol>

The lookahead set for this specific rule. Contains nothing unless #final? returns true.

Returns:

  • (Set<Symbol>)


39
40
41
# File 'lib/antelope/generation/recognizer/rule.rb', line 39

def lookahead
  @lookahead
end

#positionNumeric (readonly)

The current position inside of the rule.

Returns:

  • (Numeric)


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

def position
  @position
end

#precedenceAce::Precedence

The precedence for this rule.

Returns:



51
52
53
# File 'lib/antelope/generation/recognizer/rule.rb', line 51

def precedence
  @precedence
end

#productionAce::Production (readonly)

The associated production.

Returns:



56
57
58
# File 'lib/antelope/generation/recognizer/rule.rb', line 56

def production
  @production
end

#rightArray<Ace::Token>

The right-hand side of the rule.

Returns:



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

def right
  @right
end

Instance Method Details

#<=>(other) ⇒ Numeric

Compares this rule to another object. If the other object is not a rule, it delegates the comparison. Otherwise, it converts both this and the other rule into arrays and compares the result.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (Numeric)


155
156
157
158
159
160
161
# File 'lib/antelope/generation/recognizer/rule.rb', line 155

def <=>(other)
  if other.is_a? Rule
    to_a <=> other.to_a
  else
    super
  end
end

#===(other) ⇒ Numeric

Fuzzily compares this object to another object. If the other object is not a rule, it delegates the comparison. Otherwise, it fuzzily compares the left and right sides.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (Numeric)


169
170
171
172
173
174
175
176
# File 'lib/antelope/generation/recognizer/rule.rb', line 169

def ===(other)
  if other.is_a? Rule
    left === other.left and right.each_with_index.
      all? { |e, i| e === other.right[i] }
  else
    super
  end
end

#activeAce::Token

Returns the active token. If there is no active token, it returns a blank Ace::Token.

Returns:



108
109
110
# File 'lib/antelope/generation/recognizer/rule.rb', line 108

def active
  right[position] or Ace::Token.new(nil)
end

#cloneRule

Produces a clone of the rule; any modifications made to the contents of that rule do not reflect the contents of this rule.

Returns:



183
184
185
# File 'lib/antelope/generation/recognizer/rule.rb', line 183

def clone
  Rule.new(production, position)
end

#final?Boolean

Checks to see if this is the final rule, as in no rule can exist after this one; i.e. the position is equal to the size of the right side.

Returns:

  • (Boolean)


135
136
137
# File 'lib/antelope/generation/recognizer/rule.rb', line 135

def final?
  !succ?
end

#hashObject

Note:

This is not intended for use. It is only defined to be compatible with Hashs (and by extension, Sets).

Generates a hash for this class.

Returns:

  • (Object)


193
194
195
# File 'lib/antelope/generation/recognizer/rule.rb', line 193

def hash
  to_a.hash
end

#inspectString

Give a nice representation of the rule as a string.

Returns:

  • (String)


86
87
88
89
# File 'lib/antelope/generation/recognizer/rule.rb', line 86

def inspect
  "#<#{self.class} id=#{id} left=#{left} " \
    "right=[#{right.join(" ")}] position=#{position}>"
end

#start?Boolean

The complete opposite of #final? - it checks to see if this is the first rule, as in no rule can exist before this one; i.e. the position is zero.

Returns:

  • (Boolean)


144
145
146
# File 'lib/antelope/generation/recognizer/rule.rb', line 144

def start?
  position.zero?
end

#succRule

Creates the rule after this one by incrementing the position by one. #succ? should be called to make sure that this rule exists.

Returns:



117
118
119
# File 'lib/antelope/generation/recognizer/rule.rb', line 117

def succ
  Rule.new(production, position + 1, [left, right])
end

#succ?Boolean

Checks to see if a rule can exist after this one; i.e. the position is not equal to the size of the right side of the rule.

Returns:

  • (Boolean)


126
127
128
# File 'lib/antelope/generation/recognizer/rule.rb', line 126

def succ?
  right.size > position
end

#to_aArray<(Ace::Token::Nonterminal, Array<Ace::Token>, Numeric)>

Note:

This is not intended for use. It is only defined to make equality checking easier, and to create a hash.

Creates an array representation of this class.

Returns:



205
206
207
# File 'lib/antelope/generation/recognizer/rule.rb', line 205

def to_a
  [left, right, position].flatten
end

#to_s(dot = true) ⇒ String

Give a nicer representation of the rule as a string. Shows the id of the rule, the precedence, and the actual production; if the given argument is true, it will show a dot to show the position of the rule.

Parameters:

  • dot (Boolean) (defaults to: true)

    show the current position of the rule.

Returns:

  • (String)


98
99
100
101
102
# File 'lib/antelope/generation/recognizer/rule.rb', line 98

def to_s(dot = true)
  "#{id}/#{precedence.type.to_s[0]}#{precedence.level}: " \
    "#{left}#{right[0, position].join(" ")}" \
    "#{"" if dot}#{right[position..-1].join(" ")}"
end