diff --git a/lib/racc/grammarfileparser.rb b/lib/racc/grammarfileparser.rb index f227e158..69870257 100644 --- a/lib/racc/grammarfileparser.rb +++ b/lib/racc/grammarfileparser.rb @@ -295,7 +295,7 @@ def _add_many_rule(prev) return target if target target = _gen_target_name("many", prev) @many_rule_registry[prev.to_s] = target - src = SourceText.new("result = val[1] ? val[1].unshift(val[0]) : val", __FILE__, __LINE__) + src = SourceText.new("result = val[1] ? val[1].unshift(val[0]) : val", @filename, @scanner.lineno + 1) act = UserAction.source_text(src) @grammar.add Rule.new(target, [], act) @grammar.add Rule.new(target, [prev, target], act) @@ -308,7 +308,7 @@ def _add_many1_rule(prev) return target if target target = _gen_target_name("many1", prev) @many1_rule_registry[prev.to_s] = target - src = SourceText.new("result = val[1] ? val[1].unshift(val[0]) : val", __FILE__, __LINE__) + src = SourceText.new("result = val[1] ? val[1].unshift(val[0]) : val", @filename, @scanner.lineno + 1) act = UserAction.source_text(src) @grammar.add Rule.new(target, [prev], act) @grammar.add Rule.new(target, [prev, target], act) @@ -323,7 +323,7 @@ def _add_group_rule(enum) unless target = @group_rule_registry[target_name] target = @grammar.intern("-group@#{target_name}", true) @group_rule_registry[target_name] = target - src = SourceText.new("result = val", __FILE__, __LINE__) + src = SourceText.new("result = val", @filename, @scanner.lineno + 1) act = UserAction.source_text(src) rules.each do |syms, sprec| rule = Rule.new(target, syms, act) diff --git a/test/assets/group.y b/test/assets/group.y new file mode 100644 index 00000000..3f4ad272 --- /dev/null +++ b/test/assets/group.y @@ -0,0 +1,15 @@ +class MyParser +rule +stmt: ('a') +end +---- header +require 'strscan' +---- inner +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end diff --git a/test/assets/many.y b/test/assets/many.y new file mode 100644 index 00000000..20ea4079 --- /dev/null +++ b/test/assets/many.y @@ -0,0 +1,15 @@ +class MyParser +rule +stmt: 'abc'* +end +---- header +require 'strscan' +---- inner +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end diff --git a/test/assets/many1.y b/test/assets/many1.y new file mode 100644 index 00000000..15ec02b5 --- /dev/null +++ b/test/assets/many1.y @@ -0,0 +1,15 @@ +class MyParser +rule +stmt: 'abc'+ +end +---- header +require 'strscan' +---- inner +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end diff --git a/test/assets/optional.y b/test/assets/optional.y new file mode 100644 index 00000000..e76d11af --- /dev/null +++ b/test/assets/optional.y @@ -0,0 +1,15 @@ +class MyParser +rule +stmt: 'abc'? +end +---- header +require 'strscan' +---- inner +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end diff --git a/test/regress/group b/test/regress/group new file mode 100644 index 00000000..b844f8c1 --- /dev/null +++ b/test/regress/group @@ -0,0 +1,113 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.8.0 +# from Racc grammar file "group.y". +# + +require 'racc/parser.rb' + +require 'strscan' +class MyParser < Racc::Parser + +module_eval(<<'...end group.y/module_eval...', 'group.y', 8) +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end +...end group.y/module_eval... +##### State transition tables begin ### + +racc_action_table = [ + 2, 4, 5 ] + +racc_action_check = [ + 0, 1, 4 ] + +racc_action_pointer = [ + -2, 1, nil, nil, 2, nil ] + +racc_action_default = [ + -3, -3, -1, -2, -3, 6 ] + +racc_goto_table = [ + 1, 3 ] + +racc_goto_check = [ + 1, 2 ] + +racc_goto_pointer = [ + nil, 0, 1 ] + +racc_goto_default = [ + nil, nil, nil ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 1, 6, :_reduce_1, + 1, 5, :_reduce_none ] + +racc_reduce_n = 3 + +racc_shift_n = 6 + +racc_token_table = { + false => 0, + :error => 1, + "a" => 2, + "-temp-group" => 3 } + +racc_nt_base = 4 + +racc_use_result_var = true + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] +Ractor.make_shareable(Racc_arg) if defined?(Ractor) + +Racc_token_to_s_table = [ + "$end", + "error", + "\"a\"", + "\"-temp-group\"", + "$start", + "stmt", + "\"-group@\\\"a\\\"\"" ] +Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor) + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +module_eval(<<'.,.,', 'group.y', 4) + def _reduce_1(val, _values, result) + result = val + result + end +.,., + +# reduce 2 omitted + +def _reduce_none(val, _values, result) + val[0] +end + +end # class MyParser diff --git a/test/regress/many b/test/regress/many new file mode 100644 index 00000000..64b3d8af --- /dev/null +++ b/test/regress/many @@ -0,0 +1,119 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.8.0 +# from Racc grammar file "many.y". +# + +require 'racc/parser.rb' + +require 'strscan' +class MyParser < Racc::Parser + +module_eval(<<'...end many.y/module_eval...', 'many.y', 8) +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end +...end many.y/module_eval... +##### State transition tables begin ### + +racc_action_table = [ + 2, 4, 2, 6 ] + +racc_action_check = [ + 0, 1, 2, 4 ] + +racc_action_pointer = [ + -2, 1, 0, nil, 3, nil, nil ] + +racc_action_default = [ + -1, -4, -1, -3, -4, -2, 7 ] + +racc_goto_table = [ + 3, 1, 5 ] + +racc_goto_check = [ + 2, 1, 2 ] + +racc_goto_pointer = [ + nil, 1, 0 ] + +racc_goto_default = [ + nil, nil, nil ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 0, 5, :_reduce_1, + 2, 5, :_reduce_2, + 1, 4, :_reduce_none ] + +racc_reduce_n = 4 + +racc_shift_n = 7 + +racc_token_table = { + false => 0, + :error => 1, + "abc" => 2 } + +racc_nt_base = 3 + +racc_use_result_var = true + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] +Ractor.make_shareable(Racc_arg) if defined?(Ractor) + +Racc_token_to_s_table = [ + "$end", + "error", + "\"abc\"", + "$start", + "stmt", + "\"-many@abc\"" ] +Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor) + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +module_eval(<<'.,.,', 'many.y', 4) + def _reduce_1(val, _values, result) + result = val[1] ? val[1].unshift(val[0]) : val + result + end +.,., + +module_eval(<<'.,.,', 'many.y', 4) + def _reduce_2(val, _values, result) + result = val[1] ? val[1].unshift(val[0]) : val + result + end +.,., + +# reduce 3 omitted + +def _reduce_none(val, _values, result) + val[0] +end + +end # class MyParser diff --git a/test/regress/many1 b/test/regress/many1 new file mode 100644 index 00000000..c0a279d0 --- /dev/null +++ b/test/regress/many1 @@ -0,0 +1,119 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.8.0 +# from Racc grammar file "many1.y". +# + +require 'racc/parser.rb' + +require 'strscan' +class MyParser < Racc::Parser + +module_eval(<<'...end many1.y/module_eval...', 'many1.y', 8) +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end +...end many1.y/module_eval... +##### State transition tables begin ### + +racc_action_table = [ + 2, 4, 2, 6 ] + +racc_action_check = [ + 0, 1, 2, 4 ] + +racc_action_pointer = [ + -2, 1, 0, nil, 3, nil, nil ] + +racc_action_default = [ + -4, -4, -1, -3, -4, -2, 7 ] + +racc_goto_table = [ + 3, 1, 5 ] + +racc_goto_check = [ + 2, 1, 2 ] + +racc_goto_pointer = [ + nil, 1, 0 ] + +racc_goto_default = [ + nil, nil, nil ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 1, 5, :_reduce_1, + 2, 5, :_reduce_2, + 1, 4, :_reduce_none ] + +racc_reduce_n = 4 + +racc_shift_n = 7 + +racc_token_table = { + false => 0, + :error => 1, + "abc" => 2 } + +racc_nt_base = 3 + +racc_use_result_var = true + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] +Ractor.make_shareable(Racc_arg) if defined?(Ractor) + +Racc_token_to_s_table = [ + "$end", + "error", + "\"abc\"", + "$start", + "stmt", + "\"-many1@abc\"" ] +Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor) + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +module_eval(<<'.,.,', 'many1.y', 4) + def _reduce_1(val, _values, result) + result = val[1] ? val[1].unshift(val[0]) : val + result + end +.,., + +module_eval(<<'.,.,', 'many1.y', 4) + def _reduce_2(val, _values, result) + result = val[1] ? val[1].unshift(val[0]) : val + result + end +.,., + +# reduce 3 omitted + +def _reduce_none(val, _values, result) + val[0] +end + +end # class MyParser diff --git a/test/regress/optional b/test/regress/optional new file mode 100644 index 00000000..93945e4d --- /dev/null +++ b/test/regress/optional @@ -0,0 +1,109 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.8.0 +# from Racc grammar file "optional.y". +# + +require 'racc/parser.rb' + +require 'strscan' +class MyParser < Racc::Parser + +module_eval(<<'...end optional.y/module_eval...', 'optional.y', 8) +def parse(str) + @ss = StringScanner.new(str) + do_parse +end +def next_token + @ss.skip(/\\s+/) + token = @ss.scan(/\\S+/) and [token, token] +end +...end optional.y/module_eval... +##### State transition tables begin ### + +racc_action_table = [ + 2, 4, 5 ] + +racc_action_check = [ + 0, 1, 4 ] + +racc_action_pointer = [ + -2, 1, nil, nil, 2, nil ] + +racc_action_default = [ + -1, -4, -2, -3, -4, 6 ] + +racc_goto_table = [ + 1, 3 ] + +racc_goto_check = [ + 1, 2 ] + +racc_goto_pointer = [ + nil, 0, 1 ] + +racc_goto_default = [ + nil, nil, nil ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 0, 5, :_reduce_none, + 1, 5, :_reduce_none, + 1, 4, :_reduce_none ] + +racc_reduce_n = 4 + +racc_shift_n = 6 + +racc_token_table = { + false => 0, + :error => 1, + "abc" => 2 } + +racc_nt_base = 3 + +racc_use_result_var = true + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] +Ractor.make_shareable(Racc_arg) if defined?(Ractor) + +Racc_token_to_s_table = [ + "$end", + "error", + "\"abc\"", + "$start", + "stmt", + "\"-option@abc\"" ] +Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor) + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +# reduce 1 omitted + +# reduce 2 omitted + +# reduce 3 omitted + +def _reduce_none(val, _values, result) + val[0] +end + +end # class MyParser diff --git a/test/test_racc_command.rb b/test/test_racc_command.rb index 4fd6ac38..3f3f9593 100644 --- a/test/test_racc_command.rb +++ b/test/test_racc_command.rb @@ -350,5 +350,29 @@ def test_ifelse Turn on logging with "-v" and check ".output" file for details STDERR end + + def test_optional + assert_compile 'optional.y' + assert_debugfile 'optional.y', [0, 0, 0, 0] + assert_output_unchanged 'optional.y' + end + + def test_many + assert_compile 'many.y' + assert_debugfile 'many.y', [0, 0, 0, 0] + assert_output_unchanged 'many.y' + end + + def test_many1 + assert_compile 'many1.y' + assert_debugfile 'many1.y', [0, 0, 0, 0] + assert_output_unchanged 'many1.y' + end + + def test_group + assert_compile 'group.y' + assert_debugfile 'group.y', [0, 0, 0, 0] + assert_output_unchanged 'group.y' + end end end