From 72781c8b0371483e45158b458938e45614c408df Mon Sep 17 00:00:00 2001 From: ydah Date: Mon, 3 Jun 2024 23:37:04 +0900 Subject: [PATCH] Change String not to be reassigned Follow up: #424 --- lib/lrama/output.rb | 98 +++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 66 deletions(-) diff --git a/lib/lrama/output.rb b/lib/lrama/output.rb index a5fa7cc8..ad7f7c51 100644 --- a/lib/lrama/output.rb +++ b/lib/lrama/output.rb @@ -65,37 +65,29 @@ def render # A part of b4_token_enums def token_enums - str = "".dup - - @context.yytokentype.each do |s_value, token_id, display_name| + @context.yytokentype.map do |s_value, token_id, display_name| s = sprintf("%s = %d%s", s_value, token_id, token_id == yymaxutok ? "" : ",") if display_name - str << sprintf(" %-30s /* %s */\n", s, display_name) + sprintf(" %-30s /* %s */\n", s, display_name) else - str << sprintf(" %s\n", s) + sprintf(" %s\n", s) end - end - - str + end.join end # b4_symbol_enum def symbol_enum - str = "".dup - last_sym_number = @context.yysymbol_kind_t.last[1] - @context.yysymbol_kind_t.each do |s_value, sym_number, display_name| + @context.yysymbol_kind_t.map do |s_value, sym_number, display_name| s = sprintf("%s = %d%s", s_value, sym_number, (sym_number == last_sym_number) ? "" : ",") if display_name - str << sprintf(" %-40s /* %s */\n", s, display_name) + sprintf(" %-40s /* %s */\n", s, display_name) else - str << sprintf(" %s\n", s) + sprintf(" %s\n", s) end - end - - str + end.join end def yytranslate @@ -134,12 +126,10 @@ def int_type_for(ary) end def symbol_actions_for_printer - str = "".dup - - @grammar.symbols.each do |sym| + @grammar.symbols.map do |sym| next unless sym.printer - str << <<-STR + <<-STR case #{sym.enum_name}: /* #{sym.comment} */ #line #{sym.printer.lineno} "#{@grammar_file_path}" {#{sym.printer.translated_code(sym.tag)}} @@ -147,18 +137,14 @@ def symbol_actions_for_printer break; STR - end - - str + end.join end def symbol_actions_for_destructor - str = "".dup - - @grammar.symbols.each do |sym| + @grammar.symbols.map do |sym| next unless sym.destructor - str << <<-STR + <<-STR case #{sym.enum_name}: /* #{sym.comment} */ #line #{sym.destructor.lineno} "#{@grammar_file_path}" {#{sym.destructor.translated_code(sym.tag)}} @@ -166,9 +152,7 @@ def symbol_actions_for_destructor break; STR - end - - str + end.join end # b4_user_initial_action @@ -238,12 +222,10 @@ def after_pop_stack_function(len, comment = "") end def symbol_actions_for_error_token - str = "".dup - - @grammar.symbols.each do |sym| + @grammar.symbols.map do |sym| next unless sym.error_token - str << <<-STR + <<-STR case #{sym.enum_name}: /* #{sym.comment} */ #line #{sym.error_token.lineno} "#{@grammar_file_path}" {#{sym.error_token.translated_code(sym.tag)}} @@ -251,22 +233,18 @@ def symbol_actions_for_error_token break; STR - end - - str + end.join end # b4_user_actions def user_actions - str = "".dup - - @context.states.rules.each do |rule| + action = @context.states.rules.map do |rule| next unless rule.token_code code = rule.token_code spaces = " " * (code.column - 1) - str << <<-STR + <<-STR case #{rule.id + 1}: /* #{rule.as_comment} */ #line #{code.line} "#{@grammar_file_path}" #{spaces}{#{rule.translated_code}} @@ -274,14 +252,12 @@ def user_actions break; STR - end + end.join - str << <<-STR + action + <<-STR #line [@oline@] [@ofile@] STR - - str end def omit_blanks(param) @@ -399,17 +375,9 @@ def aux def int_array_to_string(ary) last = ary.count - 1 - s = ary.each_with_index.each_slice(10).map do |slice| - str = " ".dup - - slice.each do |e, i| - str << sprintf("%6d%s", e, (i == last) ? "" : ",") - end - - str - end - - s.join("\n") + ary.each_with_index.each_slice(10).map do |slice| + " " + slice.map { |e, i| sprintf("%6d%s", e, (i == last) ? "" : ",") }.join + end.join("\n") end def spec_mapped_header_file @@ -463,22 +431,20 @@ def template_dir end def string_array_to_string(ary) - str = "".dup - tmp = " ".dup + result = "" + tmp = " " ary.each do |s| - s = s.gsub('\\', '\\\\\\\\') - s = s.gsub('"', '\\"') - - if (tmp + s + " \"\",").length > 75 - str << tmp << "\n" - tmp = " \"#{s}\",".dup + replaced = s.gsub('\\', '\\\\\\\\').gsub('"', '\\"') + if (tmp + replaced + " \"\",").length > 75 + result = "#{result}#{tmp}\n" + tmp = " \"#{replaced}\"," else - tmp << " \"#{s}\"," + tmp = "#{tmp} \"#{replaced}\"," end end - str << tmp + result + tmp end def replace_special_variables(str, ofile)