Skip to content

Commit

Permalink
Merge pull request #51 from alitaso345/refactor_state_rb
Browse files Browse the repository at this point in the history
Refactor lrama/state.rb
  • Loading branch information
yui-knk authored Jul 8, 2023
2 parents ea94519 + 9a23a9b commit 5cf6cd9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
27 changes: 1 addition & 26 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
require "lrama/state/reduce"
require "lrama/state/shift"
require "lrama/state/resolved_conflict"

module Lrama
class State
# * symbol: A symbol under discussion
# * reduce: A reduce under discussion
# * which: For which a conflict is resolved. :shift, :reduce or :error (for nonassociative)
ResolvedConflict = Struct.new(:symbol, :reduce, :which, :same_prec, keyword_init: true) do
def report_message
s = symbol.display_name
r = reduce.rule.precedence_sym.display_name
case
when which == :shift && same_prec
msg = "resolved as #{which} (%right #{s})"
when which == :shift
msg = "resolved as #{which} (#{r} < #{s})"
when which == :reduce && same_prec
msg = "resolved as #{which} (%left #{s})"
when which == :reduce
msg = "resolved as #{which} (#{s} < #{r})"
when which == :error
msg = "resolved as an #{which} (%nonassoc #{s})"
else
raise "Unknown direction. #{self}"
end

"Conflict between rule #{reduce.rule.id} and token #{s} #{msg}."
end
end

Conflict = Struct.new(:symbols, :reduce, :type, keyword_init: true)

attr_reader :id, :accessing_symbol, :kernels, :conflicts, :resolved_conflicts,
Expand Down
29 changes: 29 additions & 0 deletions lib/lrama/state/resolved_conflict.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Lrama
class State
# * symbol: A symbol under discussion
# * reduce: A reduce under discussion
# * which: For which a conflict is resolved. :shift, :reduce or :error (for nonassociative)
class ResolvedConflict < Struct.new(:symbol, :reduce, :which, :same_prec, keyword_init: true)
def report_message
s = symbol.display_name
r = reduce.rule.precedence_sym.display_name
case
when which == :shift && same_prec
msg = "resolved as #{which} (%right #{s})"
when which == :shift
msg = "resolved as #{which} (#{r} < #{s})"
when which == :reduce && same_prec
msg = "resolved as #{which} (%left #{s})"
when which == :reduce
msg = "resolved as #{which} (#{s} < #{r})"
when which == :error
msg = "resolved as an #{which} (%nonassoc #{s})"
else
raise "Unknown direction. #{self}"
end

"Conflict between rule #{reduce.rule.id} and token #{s} #{msg}."
end
end
end
end

0 comments on commit 5cf6cd9

Please sign in to comment.