Class: Lrama::Grammar::Rule

Inherits:
Struct
  • Object
show all
Defined in:
lib/lrama/grammar/rule.rb

Overview

_rhs holds original RHS element. Use rhs to refer to Symbol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_lhsObject

Returns the value of attribute _lhs

Returns:

  • (Object)

    the current value of _lhs



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def _lhs
  @_lhs
end

#_rhsObject

Returns the value of attribute _rhs

Returns:

  • (Object)

    the current value of _rhs



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def _rhs
  @_rhs
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def id
  @id
end

#lhsObject

Returns the value of attribute lhs

Returns:

  • (Object)

    the current value of lhs



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def lhs
  @lhs
end

#lhs_tagObject

Returns the value of attribute lhs_tag

Returns:

  • (Object)

    the current value of lhs_tag



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def lhs_tag
  @lhs_tag
end

#linenoObject

Returns the value of attribute lineno

Returns:

  • (Object)

    the current value of lineno



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def lineno
  @lineno
end

#nullableObject

Returns the value of attribute nullable

Returns:

  • (Object)

    the current value of nullable



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def nullable
  @nullable
end

#original_ruleObject

Returns the value of attribute original_rule.



7
8
9
# File 'lib/lrama/grammar/rule.rb', line 7

def original_rule
  @original_rule
end

#position_in_original_rule_rhsObject

Returns the value of attribute position_in_original_rule_rhs

Returns:

  • (Object)

    the current value of position_in_original_rule_rhs



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def position_in_original_rule_rhs
  @position_in_original_rule_rhs
end

#precedence_symObject

Returns the value of attribute precedence_sym

Returns:

  • (Object)

    the current value of precedence_sym



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def precedence_sym
  @precedence_sym
end

#rhsObject

Returns the value of attribute rhs

Returns:

  • (Object)

    the current value of rhs



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def rhs
  @rhs
end

#token_codeObject

Returns the value of attribute token_code

Returns:

  • (Object)

    the current value of token_code



6
7
8
# File 'lib/lrama/grammar/rule.rb', line 6

def token_code
  @token_code
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lrama/grammar/rule.rb', line 9

def ==(other)
  self.class == other.class &&
  self.lhs == other.lhs &&
  self.lhs_tag == other.lhs_tag &&
  self.rhs == other.rhs &&
  self.token_code == other.token_code &&
  self.position_in_original_rule_rhs == other.position_in_original_rule_rhs &&
  self.nullable == other.nullable &&
  self.precedence_sym == other.precedence_sym &&
  self.lineno == other.lineno
end

#as_commentObject

Used by #user_actions



37
38
39
40
41
42
# File 'lib/lrama/grammar/rule.rb', line 37

def as_comment
  l = lhs.id.s_value
  r = empty_rule? ? "%empty" : rhs.map(&:display_name).join(" ")

  "#{l}: #{r}"
end

#contains_at_reference?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/lrama/grammar/rule.rb', line 68

def contains_at_reference?
  return false unless token_code

  token_code.references.any? {|r| r.type == :at }
end

#display_nameObject



21
22
23
24
25
# File 'lib/lrama/grammar/rule.rb', line 21

def display_name
  l = lhs.id.s_value
  r = empty_rule? ? "ε" : rhs.map {|r| r.id.s_value }.join(" ")
  "#{l} -> #{r}"
end

#display_name_without_actionObject



27
28
29
30
31
32
33
34
# File 'lib/lrama/grammar/rule.rb', line 27

def display_name_without_action
  l = lhs.id.s_value
  r = empty_rule? ? "ε" : rhs.map do |r|
    r.id.s_value if r.first_set.any?
  end.compact.join(" ")

  "#{l} -> #{r}"
end

#empty_rule?Boolean

opt_nl: ε <– empty_rule

| '\n'  <-- not empty_rule

Returns:

  • (Boolean)


50
51
52
# File 'lib/lrama/grammar/rule.rb', line 50

def empty_rule?
  rhs.empty?
end

#initial_rule?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/lrama/grammar/rule.rb', line 58

def initial_rule?
  id == 0
end

#precedenceObject



54
55
56
# File 'lib/lrama/grammar/rule.rb', line 54

def precedence
  precedence_sym&.precedence
end

#translated_codeObject



62
63
64
65
66
# File 'lib/lrama/grammar/rule.rb', line 62

def translated_code
  return nil unless token_code

  Code::RuleAction.new(type: :rule_action, token_code: token_code, rule: self).translated_code
end

#with_actionsObject



44
45
46
# File 'lib/lrama/grammar/rule.rb', line 44

def with_actions
  "#{display_name} {#{token_code&.s_value}}"
end