From 7b707262641602dca59365b84b47cd2a37d0598f Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:05:09 +0900 Subject: [PATCH 1/9] Refactor states.rb Separate Item class from lrama/states.rb --- lib/lrama/states.rb | 40 +------------------------------------ lib/lrama/states/item.rb | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 lib/lrama/states/item.rb diff --git a/lib/lrama/states.rb b/lib/lrama/states.rb index df5e0a7c..baba2c9b 100644 --- a/lib/lrama/states.rb +++ b/lib/lrama/states.rb @@ -1,5 +1,6 @@ require "forwardable" require "lrama/report" +require "lrama/states/item" module Lrama # States is passed to a template file @@ -13,45 +14,6 @@ class States def_delegators "@grammar", :symbols, :terms, :nterms, :rules, :accept_symbol, :eof_symbol, :undef_symbol, :find_symbol_by_s_value! - # TODO: Validate position is not over rule rhs - Item = Struct.new(:rule, :position, keyword_init: true) do - # Optimization for States#setup_state - def hash - [rule.id, position].hash - end - - def rule_id - rule.id - end - - def next_sym - rule.rhs[position] - end - - def end_of_rule? - rule.rhs.count == position - end - - def new_by_next_position - Item.new(rule: rule, position: position + 1) - end - - def previous_sym - rule.rhs[position - 1] - end - - def display_name - r = rule.rhs.map(&:display_name).insert(position, "•").join(" ") - "#{r} (rule #{rule.id})" - end - - # Right after position - def display_rest - r = rule.rhs[position..-1].map(&:display_name).join(" ") - ". #{r} (rule #{rule.id})" - end - end - attr_reader :states, :reads_relation, :includes_relation, :lookback_relation def initialize(grammar, warning, trace_state: false) diff --git a/lib/lrama/states/item.rb b/lib/lrama/states/item.rb new file mode 100644 index 00000000..5c3696cc --- /dev/null +++ b/lib/lrama/states/item.rb @@ -0,0 +1,43 @@ +# TODO: Validate position is not over rule rhs + +module Lrama + class States + class Item < Struct.new(:rule, :position, keyword_init: true) + # Optimization for States#setup_state + def hash + [rule.id, position].hash + end + + def rule_id + rule.id + end + + def next_sym + rule.rhs[position] + end + + def end_of_rule? + rule.rhs.count == position + end + + def new_by_next_position + Item.new(rule: rule, position: position + 1) + end + + def previous_sym + rule.rhs[position - 1] + end + + def display_name + r = rule.rhs.map(&:display_name).insert(position, "•").join(" ") + "#{r} (rule #{rule.id})" + end + + # Right after position + def display_rest + r = rule.rhs[position..-1].map(&:display_name).join(" ") + ". #{r} (rule #{rule.id})" + end + end + end +end From 98bcb38e21ce3ddfa7a5e60b8a6c66a91efb0c19 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:16:48 +0900 Subject: [PATCH 2/9] Refactor grammar.rb Separate Rule class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 34 ---------------------------------- lib/lrama/rule.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 lib/lrama/rule.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 19f579c3..06192bd3 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -7,6 +7,7 @@ require "lrama/output" require "lrama/parser" require "lrama/report" +require "lrama/rule" require "lrama/state" require "lrama/states" require "lrama/states_reporter" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index a1148c13..b4afa7d3 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,40 +2,6 @@ require "lrama/lexer" module Lrama - Rule = Struct.new(:id, :lhs, :rhs, :code, :nullable, :precedence_sym, :lineno, keyword_init: true) do - # TODO: Change this to display_name - def to_s - l = lhs.id.s_value - r = rhs.empty? ? "ε" : rhs.map {|r| r.id.s_value }.join(", ") - - "#{l} -> #{r}" - end - - # Used by #user_actions - def as_comment - l = lhs.id.s_value - r = rhs.empty? ? "%empty" : rhs.map {|r| r.display_name }.join(" ") - - "#{l}: #{r}" - end - - def precedence - precedence_sym && precedence_sym.precedence - end - - def initial_rule? - id == 0 - end - - def translated_code - if code - code.translated_code - else - nil - end - end - end - # Symbol is both of nterm and term # `number` is both for nterm and term # `token_id` is tokentype for term, internal sequence number for nterm diff --git a/lib/lrama/rule.rb b/lib/lrama/rule.rb new file mode 100644 index 00000000..1095965a --- /dev/null +++ b/lib/lrama/rule.rb @@ -0,0 +1,35 @@ +module Lrama + class Rule < Struct.new(:id, :lhs, :rhs, :code, :nullable, :precedence_sym, :lineno, keyword_init: true) + # TODO: Change this to display_name + def to_s + l = lhs.id.s_value + r = rhs.empty? ? "ε" : rhs.map {|r| r.id.s_value }.join(", ") + + "#{l} -> #{r}" + end + + # Used by #user_actions + def as_comment + l = lhs.id.s_value + r = rhs.empty? ? "%empty" : rhs.map {|r| r.display_name }.join(" ") + + "#{l}: #{r}" + end + + def precedence + precedence_sym && precedence_sym.precedence + end + + def initial_rule? + id == 0 + end + + def translated_code + if code + code.translated_code + else + nil + end + end + end +end From b3fd1efa6cb32b2536dd06890400becef2ad6ebb Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:23:42 +0900 Subject: [PATCH 3/9] Refactor grammar.rb that separate Symbol class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 90 ------------------------------------------- lib/lrama/symbol.rb | 92 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 90 deletions(-) create mode 100644 lib/lrama/symbol.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 06192bd3..5d80b6e4 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -11,5 +11,6 @@ require "lrama/state" require "lrama/states" require "lrama/states_reporter" +require "lrama/symbol" require "lrama/version" require "lrama/warning" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index b4afa7d3..eb4fe6ca 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,96 +2,6 @@ require "lrama/lexer" module Lrama - # Symbol is both of nterm and term - # `number` is both for nterm and term - # `token_id` is tokentype for term, internal sequence number for nterm - # - # TODO: Add validation for ASCII code range for Token::Char - Symbol = Struct.new(:id, :alias_name, :number, :tag, :term, :token_id, :nullable, :precedence, :printer, :error_token, keyword_init: true) do - attr_writer :eof_symbol, :error_symbol, :undef_symbol, :accept_symbol - - def term? - term - end - - def nterm? - !term - end - - def eof_symbol? - !!@eof_symbol - end - - def error_symbol? - !!@error_symbol - end - - def undef_symbol? - !!@undef_symbol - end - - def accept_symbol? - !!@accept_symbol - end - - def display_name - if alias_name - alias_name - else - id.s_value - end - end - - # name for yysymbol_kind_t - # - # See: b4_symbol_kind_base - def enum_name - case - when accept_symbol? - name = "YYACCEPT" - when eof_symbol? - name = "YYEOF" - when term? && id.type == Token::Char - if alias_name - name = number.to_s + alias_name - else - name = number.to_s + id.s_value - end - when term? && id.type == Token::Ident - name = id.s_value - when nterm? && (id.s_value.include?("$") || id.s_value.include?("@")) - name = number.to_s + id.s_value - when nterm? - name = id.s_value - else - raise "Unexpected #{self}" - end - - "YYSYMBOL_" + name.gsub(/[^a-zA-Z_0-9]+/, "_") - end - - # comment for yysymbol_kind_t - def comment - case - when accept_symbol? - # YYSYMBOL_YYACCEPT - id.s_value - when eof_symbol? - # YYEOF - alias_name - when (term? && 0 < token_id && token_id < 128) - # YYSYMBOL_3_backslash_, YYSYMBOL_14_ - alias_name || id.s_value - when id.s_value.include?("$") || id.s_value.include?("@") - # YYSYMBOL_21_1 - id.s_value - else - # YYSYMBOL_keyword_class, YYSYMBOL_strings_1 - alias_name || id.s_value - end - end - end - Type = Struct.new(:id, :tag, keyword_init: true) Code = Struct.new(:type, :token_code, keyword_init: true) do diff --git a/lib/lrama/symbol.rb b/lib/lrama/symbol.rb new file mode 100644 index 00000000..9207de4c --- /dev/null +++ b/lib/lrama/symbol.rb @@ -0,0 +1,92 @@ +# Symbol is both of nterm and term +# `number` is both for nterm and term +# `token_id` is tokentype for term, internal sequence number for nterm +# +# TODO: Add validation for ASCII code range for Token::Char + +module Lrama + class Symbol < Struct.new(:id, :alias_name, :number, :tag, :term, :token_id, :nullable, :precedence, :printer, :error_token, keyword_init: true) + attr_writer :eof_symbol, :error_symbol, :undef_symbol, :accept_symbol + + def term? + term + end + + def nterm? + !term + end + + def eof_symbol? + !!@eof_symbol + end + + def error_symbol? + !!@error_symbol + end + + def undef_symbol? + !!@undef_symbol + end + + def accept_symbol? + !!@accept_symbol + end + + def display_name + if alias_name + alias_name + else + id.s_value + end + end + + # name for yysymbol_kind_t + # + # See: b4_symbol_kind_base + def enum_name + case + when accept_symbol? + name = "YYACCEPT" + when eof_symbol? + name = "YYEOF" + when term? && id.type == Token::Char + if alias_name + name = number.to_s + alias_name + else + name = number.to_s + id.s_value + end + when term? && id.type == Token::Ident + name = id.s_value + when nterm? && (id.s_value.include?("$") || id.s_value.include?("@")) + name = number.to_s + id.s_value + when nterm? + name = id.s_value + else + raise "Unexpected #{self}" + end + + "YYSYMBOL_" + name.gsub(/[^a-zA-Z_0-9]+/, "_") + end + + # comment for yysymbol_kind_t + def comment + case + when accept_symbol? + # YYSYMBOL_YYACCEPT + id.s_value + when eof_symbol? + # YYEOF + alias_name + when (term? && 0 < token_id && token_id < 128) + # YYSYMBOL_3_backslash_, YYSYMBOL_14_ + alias_name || id.s_value + when id.s_value.include?("$") || id.s_value.include?("@") + # YYSYMBOL_21_1 + id.s_value + else + # YYSYMBOL_keyword_class, YYSYMBOL_strings_1 + alias_name || id.s_value + end + end + end +end From afada9cb5dfb3adab36225eb8475aa4bbe8ae4af Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:30:19 +0900 Subject: [PATCH 4/9] Refactor grammar.rb that separate Code class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/code.rb | 121 +++++++++++++++++++++++++++++++++++++++++++ lib/lrama/grammar.rb | 120 ------------------------------------------ 3 files changed, 122 insertions(+), 120 deletions(-) create mode 100644 lib/lrama/code.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 5d80b6e4..47ee1d5c 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -1,4 +1,5 @@ require "lrama/bitmap" +require "lrama/code" require "lrama/command" require "lrama/context" require "lrama/digraph" diff --git a/lib/lrama/code.rb b/lib/lrama/code.rb new file mode 100644 index 00000000..2eea90dc --- /dev/null +++ b/lib/lrama/code.rb @@ -0,0 +1,121 @@ +require "forwardable" + +module Lrama + class Code < Struct.new(:type, :token_code, keyword_init: true) + extend Forwardable + + def_delegators "token_code", :s_value, :line, :column, :references + + # $$, $n, @$, @n is translated to C code + def translated_code + case type + when :user_code + translated_user_code + when :initial_action + translated_initial_action_code + end + end + + # * ($1) error + # * ($$) *yyvaluep + # * (@1) error + # * (@$) *yylocationp + def translated_printer_code(tag) + t_code = s_value.dup + + references.reverse.each do |ref| + first_column = ref.first_column + last_column = ref.last_column + + case + when ref.value == "$" && ref.type == :dollar # $$ + # Omit "<>" + member = tag.s_value[1..-2] + str = "((*yyvaluep).#{member})" + when ref.value == "$" && ref.type == :at # @$ + str = "(*yylocationp)" + when ref.type == :dollar # $n + raise "$#{ref.value} can not be used in %printer." + when ref.type == :at # @n + raise "@#{ref.value} can not be used in %printer." + else + raise "Unexpected. #{self}, #{ref}" + end + + t_code[first_column..last_column] = str + end + + return t_code + end + alias :translated_error_token_code :translated_printer_code + + + private + + # * ($1) yyvsp[i] + # * ($$) yyval + # * (@1) yylsp[i] + # * (@$) yyloc + def translated_user_code + t_code = s_value.dup + + references.reverse.each do |ref| + first_column = ref.first_column + last_column = ref.last_column + + case + when ref.value == "$" && ref.type == :dollar # $$ + # Omit "<>" + member = ref.tag.s_value[1..-2] + str = "(yyval.#{member})" + when ref.value == "$" && ref.type == :at # @$ + str = "(yyloc)" + when ref.type == :dollar # $n + i = -ref.position_in_rhs + ref.value + # Omit "<>" + member = ref.tag.s_value[1..-2] + str = "(yyvsp[#{i}].#{member})" + when ref.type == :at # @n + i = -ref.position_in_rhs + ref.value + str = "(yylsp[#{i}])" + else + raise "Unexpected. #{self}, #{ref}" + end + + t_code[first_column..last_column] = str + end + + return t_code + end + + # * ($1) error + # * ($$) yylval + # * (@1) error + # * (@$) yylloc + def translated_initial_action_code + t_code = s_value.dup + + references.reverse.each do |ref| + first_column = ref.first_column + last_column = ref.last_column + + case + when ref.value == "$" && ref.type == :dollar # $$ + str = "yylval" + when ref.value == "$" && ref.type == :at # @$ + str = "yylloc" + when ref.type == :dollar # $n + raise "$#{ref.value} can not be used in initial_action." + when ref.type == :at # @n + raise "@#{ref.value} can not be used in initial_action." + else + raise "Unexpected. #{self}, #{ref}" + end + + t_code[first_column..last_column] = str + end + + return t_code + end + end +end diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index eb4fe6ca..cdf7df20 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -1,127 +1,7 @@ -require "forwardable" require "lrama/lexer" - module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - Code = Struct.new(:type, :token_code, keyword_init: true) do - extend Forwardable - - def_delegators "token_code", :s_value, :line, :column, :references - - # $$, $n, @$, @n is translated to C code - def translated_code - case type - when :user_code - translated_user_code - when :initial_action - translated_initial_action_code - end - end - - # * ($1) error - # * ($$) *yyvaluep - # * (@1) error - # * (@$) *yylocationp - def translated_printer_code(tag) - t_code = s_value.dup - - references.reverse.each do |ref| - first_column = ref.first_column - last_column = ref.last_column - - case - when ref.value == "$" && ref.type == :dollar # $$ - # Omit "<>" - member = tag.s_value[1..-2] - str = "((*yyvaluep).#{member})" - when ref.value == "$" && ref.type == :at # @$ - str = "(*yylocationp)" - when ref.type == :dollar # $n - raise "$#{ref.value} can not be used in %printer." - when ref.type == :at # @n - raise "@#{ref.value} can not be used in %printer." - else - raise "Unexpected. #{self}, #{ref}" - end - - t_code[first_column..last_column] = str - end - - return t_code - end - alias :translated_error_token_code :translated_printer_code - - - private - - # * ($1) yyvsp[i] - # * ($$) yyval - # * (@1) yylsp[i] - # * (@$) yyloc - def translated_user_code - t_code = s_value.dup - - references.reverse.each do |ref| - first_column = ref.first_column - last_column = ref.last_column - - case - when ref.value == "$" && ref.type == :dollar # $$ - # Omit "<>" - member = ref.tag.s_value[1..-2] - str = "(yyval.#{member})" - when ref.value == "$" && ref.type == :at # @$ - str = "(yyloc)" - when ref.type == :dollar # $n - i = -ref.position_in_rhs + ref.value - # Omit "<>" - member = ref.tag.s_value[1..-2] - str = "(yyvsp[#{i}].#{member})" - when ref.type == :at # @n - i = -ref.position_in_rhs + ref.value - str = "(yylsp[#{i}])" - else - raise "Unexpected. #{self}, #{ref}" - end - - t_code[first_column..last_column] = str - end - - return t_code - end - - # * ($1) error - # * ($$) yylval - # * (@1) error - # * (@$) yylloc - def translated_initial_action_code - t_code = s_value.dup - - references.reverse.each do |ref| - first_column = ref.first_column - last_column = ref.last_column - - case - when ref.value == "$" && ref.type == :dollar # $$ - str = "yylval" - when ref.value == "$" && ref.type == :at # @$ - str = "yylloc" - when ref.type == :dollar # $n - raise "$#{ref.value} can not be used in initial_action." - when ref.type == :at # @n - raise "@#{ref.value} can not be used in initial_action." - else - raise "Unexpected. #{self}, #{ref}" - end - - t_code[first_column..last_column] = str - end - - return t_code - end - end - # type: :dollar or :at # ex_tag: "$1" (Optional) Reference = Struct.new(:type, :value, :ex_tag, :first_column, :last_column, :referring_symbol, :position_in_rhs, keyword_init: true) do From c97d1316ff0dc57ba18e47bb837077d13405298a Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:36:27 +0900 Subject: [PATCH 5/9] Refactor grammar.rb that separate Reference class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 12 ------------ lib/lrama/reference.rb | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 lib/lrama/reference.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 47ee1d5c..75519ba3 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -7,6 +7,7 @@ require "lrama/lexer" require "lrama/output" require "lrama/parser" +require "lrama/reference" require "lrama/report" require "lrama/rule" require "lrama/state" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index cdf7df20..cd09a07b 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,18 +2,6 @@ module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - # type: :dollar or :at - # ex_tag: "$1" (Optional) - Reference = Struct.new(:type, :value, :ex_tag, :first_column, :last_column, :referring_symbol, :position_in_rhs, keyword_init: true) do - def tag - if ex_tag - ex_tag - else - referring_symbol.tag - end - end - end - Precedence = Struct.new(:type, :precedence, keyword_init: true) do include Comparable diff --git a/lib/lrama/reference.rb b/lib/lrama/reference.rb new file mode 100644 index 00000000..461e5921 --- /dev/null +++ b/lib/lrama/reference.rb @@ -0,0 +1,14 @@ +# type: :dollar or :at +# ex_tag: "$1" (Optional) + +module Lrama + class Reference < Struct.new(:type, :value, :ex_tag, :first_column, :last_column, :referring_symbol, :position_in_rhs, keyword_init: true) + def tag + if ex_tag + ex_tag + else + referring_symbol.tag + end + end + end +end From a7c6cca697321c6e81e6145a3de392b6791235c2 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:41:30 +0900 Subject: [PATCH 6/9] Refactor grammar.rb that separate Precedence class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 8 -------- lib/lrama/precedence.rb | 9 +++++++++ 3 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 lib/lrama/precedence.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 75519ba3..e47aa891 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -7,6 +7,7 @@ require "lrama/lexer" require "lrama/output" require "lrama/parser" +require "lrama/precedence" require "lrama/reference" require "lrama/report" require "lrama/rule" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index cd09a07b..ab8b6470 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,14 +2,6 @@ module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - Precedence = Struct.new(:type, :precedence, keyword_init: true) do - include Comparable - - def <=>(other) - self.precedence <=> other.precedence - end - end - Printer = Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) do def translated_code(member) code.translated_printer_code(member) diff --git a/lib/lrama/precedence.rb b/lib/lrama/precedence.rb new file mode 100644 index 00000000..a189bef9 --- /dev/null +++ b/lib/lrama/precedence.rb @@ -0,0 +1,9 @@ +module Lrama + class Precedence < Struct.new(:type, :precedence, keyword_init: true) + include Comparable + + def <=>(other) + self.precedence <=> other.precedence + end + end +end From 4a503476eae6369a801ab3c417bad4d08c6b7054 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:43:50 +0900 Subject: [PATCH 7/9] Refactor grammar.rb that separate Printer class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 6 ------ lib/lrama/printer.rb | 7 +++++++ 3 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 lib/lrama/printer.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index e47aa891..6222ab97 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -8,6 +8,7 @@ require "lrama/output" require "lrama/parser" require "lrama/precedence" +require "lrama/printer" require "lrama/reference" require "lrama/report" require "lrama/rule" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index ab8b6470..11716a51 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,12 +2,6 @@ module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - Printer = Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) do - def translated_code(member) - code.translated_printer_code(member) - end - end - ErrorToken = Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) do def translated_code(member) code.translated_error_token_code(member) diff --git a/lib/lrama/printer.rb b/lib/lrama/printer.rb new file mode 100644 index 00000000..7623c8b4 --- /dev/null +++ b/lib/lrama/printer.rb @@ -0,0 +1,7 @@ +module Lrama + class Printer < Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) + def translated_code(member) + code.translated_printer_code(member) + end + end +end From 1f5a4215b4a00565e2dcfb53ae62fd605a3aacb8 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 00:49:45 +0900 Subject: [PATCH 8/9] Refactor grammar.rb that separate ErrorToken class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/error_token.rb | 7 +++++++ lib/lrama/grammar.rb | 6 ------ 3 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 lib/lrama/error_token.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 6222ab97..87824c8b 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -3,6 +3,7 @@ require "lrama/command" require "lrama/context" require "lrama/digraph" +require "lrama/error_token" require "lrama/grammar" require "lrama/lexer" require "lrama/output" diff --git a/lib/lrama/error_token.rb b/lib/lrama/error_token.rb new file mode 100644 index 00000000..3e4d96e0 --- /dev/null +++ b/lib/lrama/error_token.rb @@ -0,0 +1,7 @@ +module Lrama + class ErrorToken < Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) + def translated_code(member) + code.translated_error_token_code(member) + end + end +end diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index 11716a51..347e40d2 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -2,12 +2,6 @@ module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - ErrorToken = Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true) do - def translated_code(member) - code.translated_error_token_code(member) - end - end - Union = Struct.new(:code, :lineno, keyword_init: true) do def braces_less_code # Remove braces From 83520287948ea2aefa785b19443452d6122cb4ee Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Thu, 6 Jul 2023 23:34:51 +0900 Subject: [PATCH 9/9] Refactor grammar.rb that separate Union class from lrama/grammar.rb --- lib/lrama.rb | 1 + lib/lrama/grammar.rb | 8 -------- lib/lrama/union.rb | 8 ++++++++ 3 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 lib/lrama/union.rb diff --git a/lib/lrama.rb b/lib/lrama.rb index 87824c8b..ab8a034a 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -17,5 +17,6 @@ require "lrama/states" require "lrama/states_reporter" require "lrama/symbol" +require "lrama/union" require "lrama/version" require "lrama/warning" diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index 347e40d2..c7d88987 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -1,14 +1,6 @@ require "lrama/lexer" module Lrama Type = Struct.new(:id, :tag, keyword_init: true) - - Union = Struct.new(:code, :lineno, keyword_init: true) do - def braces_less_code - # Remove braces - code.s_value[1..-2] - end - end - Token = Lrama::Lexer::Token # Grammar is the result of parsing an input grammar file diff --git a/lib/lrama/union.rb b/lib/lrama/union.rb new file mode 100644 index 00000000..6d8b01a6 --- /dev/null +++ b/lib/lrama/union.rb @@ -0,0 +1,8 @@ +module Lrama + class Union < Struct.new(:code, :lineno, keyword_init: true) + def braces_less_code + # Remove braces + code.s_value[1..-2] + end + end +end