Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion lib/racc/grammarfileparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def add_user_code(label, src)
class GrammarFileScanner

def initialize(str, filename = '-')
@encoding = str.encoding
@lines = str.b.split(/\n|\r\n|\r/)
@filename = filename
@lineno = -1
Expand Down Expand Up @@ -456,7 +457,24 @@ def yylex0
elsif ch = reads(/\A./)
case ch
when '"', "'"
yield [:STRING, eval(scan_quoted(ch))]
string_literal = scan_quoted(ch)
if ch == "'"
# We can't use String#undump for '...'.
string = string_literal[1..-2].gsub(/\\\\|\\'/) do |matched|
matched[1]
end
else
# String#undump rejects non-ASCII
# characters. string_literal is ASCII-8BIT because
# @lines is ASCII-8BIT. We can use \xHH here to
# convert non-ASCII characters to ASCII characters.
string_literal = string_literal.gsub(/[\x80-\xff]/n) do |c|
"\\x%02x" % c.ord
end
string = string_literal.undump
end
string.force_encoding(@encoding)
yield [:STRING, string]
when '{'
lineno = lineno()
yield [:ACTION, SourceText.new(scan_action(), @filename, lineno)]
Expand Down
45 changes: 45 additions & 0 deletions test/test_grammar_file_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8

require File.expand_path(File.join(__dir__, 'case'))

module Racc
Expand Down Expand Up @@ -68,5 +70,48 @@ class Parse

assert_equal "4: terminal and nonterminal symbols cannot start with ':', but got :TERM2", error.message
end

def test_non_ascii_string
parser = Racc::GrammarFileParser.new

result = parser.parse(<<~RACC, 'non_ascii.y')
class Parse
rule
target : "あ" 'い' "\\u3046" "\\n"
end
RACC

strings = result.grammar.symbols.map(&:value).grep(String)
assert_equal ["あ", "い", "う", "\n"], strings
end

def test_non_ascii_string_in_non_utf8_source
parser = Racc::GrammarFileParser.new

result = parser.parse(<<~RACC.encode(Encoding::EUC_JP), 'non_ascii_euc.y')
class Parse
rule
target : "あ"
end
RACC

strings = result.grammar.symbols.map(&:value).grep(String)
assert_equal ["あ".encode(Encoding::EUC_JP)], strings
end

def test_non_ascii_string_interned_consistently
parser = Racc::GrammarFileParser.new

result = parser.parse(<<~RACC, 'non_ascii_intern.y')
class Parse
rule
target : "あ" other
other : 'あ'
end
RACC

strings = result.grammar.symbols.map(&:value).grep(String)
assert_equal ["あ"], strings
end
end
end
Loading