Class: Lrama::Grammar::Rule
- Inherits:
-
Struct
- Object
- Struct
- Lrama::Grammar::Rule
- Defined in:
- lib/lrama/grammar/rule.rb
Overview
_rhs holds original RHS element. Use rhs to refer to Symbol.
Instance Attribute Summary collapse
-
#_lhs ⇒ Object
Returns the value of attribute _lhs.
-
#_rhs ⇒ Object
Returns the value of attribute _rhs.
-
#id ⇒ Object
Returns the value of attribute id.
-
#lhs ⇒ Object
Returns the value of attribute lhs.
-
#lhs_tag ⇒ Object
Returns the value of attribute lhs_tag.
-
#lineno ⇒ Object
Returns the value of attribute lineno.
-
#nullable ⇒ Object
Returns the value of attribute nullable.
-
#original_rule ⇒ Object
@rbs!.
-
#position_in_original_rule_rhs ⇒ Object
Returns the value of attribute position_in_original_rule_rhs.
-
#precedence_sym ⇒ Object
Returns the value of attribute precedence_sym.
-
#rhs ⇒ Object
Returns the value of attribute rhs.
-
#token_code ⇒ Object
Returns the value of attribute token_code.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#as_comment ⇒ Object
Used by #user_actions.
- #contains_at_reference? ⇒ Boolean
- #display_name ⇒ Object
- #display_name_without_action ⇒ Object
-
#empty_rule? ⇒ Boolean
opt_nl: ε <-- empty_rule | '\n' <-- not empty_rule.
- #initial_rule? ⇒ Boolean
- #precedence ⇒ Object
- #to_diagrams ⇒ Object
- #translated_code(grammar) ⇒ Object
- #with_actions ⇒ Object
Instance Attribute Details
#_lhs ⇒ Object
Returns the value of attribute _lhs
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def _lhs @_lhs end |
#_rhs ⇒ Object
Returns the value of attribute _rhs
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def _rhs @_rhs end |
#id ⇒ Object
Returns the value of attribute id
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def id @id end |
#lhs ⇒ Object
Returns the value of attribute lhs
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def lhs @lhs end |
#lhs_tag ⇒ Object
Returns the value of attribute lhs_tag
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def lhs_tag @lhs_tag end |
#lineno ⇒ Object
Returns the value of attribute lineno
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def lineno @lineno end |
#nullable ⇒ Object
Returns the value of attribute nullable
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def nullable @nullable end |
#original_rule ⇒ Object
@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_rhs ⇒ Object
Returns the value of attribute 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_sym ⇒ Object
Returns the value of attribute precedence_sym
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def precedence_sym @precedence_sym end |
#rhs ⇒ Object
Returns the value of attribute rhs
7 8 9 |
# File 'lib/lrama/grammar/rule.rb', line 7 def rhs @rhs end |
#token_code ⇒ Object
Returns the value of attribute 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_comment ⇒ Object
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
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_name ⇒ Object
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_action ⇒ Object
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
93 94 95 |
# File 'lib/lrama/grammar/rule.rb', line 93 def empty_rule? rhs.empty? end |
#initial_rule? ⇒ Boolean
103 104 105 |
# File 'lib/lrama/grammar/rule.rb', line 103 def initial_rule? id == 0 end |
#precedence ⇒ Object
98 99 100 |
# File 'lib/lrama/grammar/rule.rb', line 98 def precedence precedence_sym&.precedence end |
#to_diagrams ⇒ Object
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_actions ⇒ Object
85 86 87 |
# File 'lib/lrama/grammar/rule.rb', line 85 def with_actions "#{display_name} {#{token_code&.s_value}}" end |