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



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

def _lhs
  @_lhs
end

#_rhsObject

Returns the value of attribute _rhs

Returns:

  • (Object)

    the current value of _rhs



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

def _rhs
  @_rhs
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



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

def id
  @id
end

#lhsObject

Returns the value of attribute lhs

Returns:

  • (Object)

    the current value of lhs



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

def lhs
  @lhs
end

#lhs_tagObject

Returns the value of attribute lhs_tag

Returns:

  • (Object)

    the current value of lhs_tag



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

def lhs_tag
  @lhs_tag
end

#linenoObject

Returns the value of attribute lineno

Returns:

  • (Object)

    the current value of lineno



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

def lineno
  @lineno
end

#nullableObject

Returns the value of attribute nullable

Returns:

  • (Object)

    the current value of nullable



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

def nullable
  @nullable
end

#original_ruleObject

@rbs!

interface _DelegatedMethods
def lhs: -> Grammar::Symbol
def rhs: -> Array[Grammar::Symbol]
end

attr_accessor id: Integer
attr_accessor _lhs: Lexer::Token::Base
attr_accessor lhs: Grammar::Symbol
attr_accessor lhs_tag: Lexer::Token::Tag?
attr_accessor _rhs: Array[Lexer::Token::Base]
attr_accessor rhs: Array[Grammar::Symbol]
attr_accessor token_code: Lexer::Token::UserCode?
attr_accessor position_in_original_rule_rhs: Integer
attr_accessor nullable: bool
attr_accessor precedence_sym: Grammar::Symbol?
attr_accessor lineno: Integer?

def initialize: (
?id: Integer, ?_lhs: Lexer::Token::Base?, ?lhs: Lexer::Token::Base, ?lhs_tag: Lexer::Token::Tag?, ?_rhs: Array[Lexer::Token::Base], ?rhs: Array[Grammar::Symbol],
?token_code: Lexer::Token::UserCode?, ?position_in_original_rule_rhs: Integer?, ?nullable: bool,
?precedence_sym: Grammar::Symbol?, ?lineno: Integer?
) -> void


33
34
35
# File 'lib/lrama/grammar/rule.rb', line 33

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



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

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



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

def precedence_sym
  @precedence_sym
end

#rhsObject

Returns the value of attribute rhs

Returns:

  • (Object)

    the current value of rhs



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

def rhs
  @rhs
end

#token_codeObject

Returns the value of attribute token_code

Returns:

  • (Object)

    the current value of token_code



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

def token_code
  @token_code
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lrama/grammar/rule.rb', line 36

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



77
78
79
80
81
82
# File 'lib/lrama/grammar/rule.rb', line 77

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)


115
116
117
118
119
# File 'lib/lrama/grammar/rule.rb', line 115

def contains_at_reference?
  return false unless token_code

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

#display_nameObject



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

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



56
57
58
59
60
61
62
63
# File 'lib/lrama/grammar/rule.rb', line 56

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)


93
94
95
# File 'lib/lrama/grammar/rule.rb', line 93

def empty_rule?
  rhs.empty?
end

#initial_rule?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/lrama/grammar/rule.rb', line 103

def initial_rule?
  id == 0
end

#precedenceObject



98
99
100
# File 'lib/lrama/grammar/rule.rb', line 98

def precedence
  precedence_sym&.precedence
end

#to_diagramsObject



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

def to_diagrams
  if rhs.empty?
    RailroadDiagrams::Skip.new
  else
    RailroadDiagrams::Sequence.new(*rhs_to_diagram)
  end
end

#translated_code(grammar) ⇒ Object



108
109
110
111
112
# File 'lib/lrama/grammar/rule.rb', line 108

def translated_code(grammar)
  return nil unless token_code

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

#with_actionsObject



85
86
87
# File 'lib/lrama/grammar/rule.rb', line 85

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