Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change String not to be reassigned #428

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 32 additions & 66 deletions lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,41 +126,33 @@ 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)}}
#line [@oline@] [@ofile@]
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)}}
#line [@oline@] [@ofile@]
break;

STR
end

str
end.join
end

# b4_user_initial_action
Expand Down Expand Up @@ -238,50 +222,42 @@ 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)}}
#line [@oline@] [@ofile@]
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}}
#line [@oline@] [@ofile@]
break;

STR
end
end.join

str << <<-STR
action + <<-STR

#line [@oline@] [@ofile@]
STR

str
end

def omit_blanks(param)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down