Class: Lrama::Grammar::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def code
  @code
end

#idObject

Returns the value of attribute id



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def id
  @id
end

#lhsObject

Returns the value of attribute lhs



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def lhs
  @lhs
end

#linenoObject

Returns the value of attribute lineno



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def lineno
  @lineno
end

#nullableObject

Returns the value of attribute nullable



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def nullable
  @nullable
end

#precedence_symObject

Returns the value of attribute precedence_sym



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def precedence_sym
  @precedence_sym
end

#rhsObject

Returns the value of attribute rhs



3
4
5
# File 'lib/lrama/grammar/rule.rb', line 3

def rhs
  @rhs
end

Instance Method Details

#as_commentObject

Used by #user_actions



13
14
15
16
17
18
# File 'lib/lrama/grammar/rule.rb', line 13

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

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

#empty_rule?Boolean

opt_nl: ε <– empty_rule

| '\n'  <-- not empty_rule


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

def empty_rule?
  rhs.empty?
end

#initial_rule?Boolean



30
31
32
# File 'lib/lrama/grammar/rule.rb', line 30

def initial_rule?
  id == 0
end

#precedenceObject



26
27
28
# File 'lib/lrama/grammar/rule.rb', line 26

def precedence
  precedence_sym&.precedence
end

#to_sObject

TODO: Change this to display_name



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

def to_s
  l = lhs.id.s_value
  r = rhs.empty? ? "ε" : rhs.map {|r| r.id.s_value }.join(", ")

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

#translated_codeObject



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

def translated_code
  code&.translated_code
end