Skip to content

Commit

Permalink
Merge pull request #273 from ydah/fix-filepath-lineno
Browse files Browse the repository at this point in the history
Fix file path and line number errors when using `+`, `*` and `()`
  • Loading branch information
yui-knk authored Jul 30, 2024
2 parents d27b781 + 93b7447 commit a71dd38
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/racc/grammarfileparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions test/assets/group.y
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/assets/many.y
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/assets/many1.y
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/assets/optional.y
Original file line number Diff line number Diff line change
@@ -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
113 changes: 113 additions & 0 deletions test/regress/group
Original file line number Diff line number Diff line change
@@ -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
119 changes: 119 additions & 0 deletions test/regress/many
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a71dd38

Please sign in to comment.