From 9a23a9b6e6ffa2e29e2c03a2a987665eb8240156 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sat, 8 Jul 2023 20:02:33 +0900 Subject: [PATCH] Refactor state.rb that separate ResolvedConflict class from lrama/state.rb --- lib/lrama/state.rb | 27 +------------------------- lib/lrama/state/resolved_conflict.rb | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 lib/lrama/state/resolved_conflict.rb diff --git a/lib/lrama/state.rb b/lib/lrama/state.rb index 9780ade0..b868035e 100644 --- a/lib/lrama/state.rb +++ b/lib/lrama/state.rb @@ -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, diff --git a/lib/lrama/state/resolved_conflict.rb b/lib/lrama/state/resolved_conflict.rb new file mode 100644 index 00000000..02ea8921 --- /dev/null +++ b/lib/lrama/state/resolved_conflict.rb @@ -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