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

Add support for escape sequences in double quoted rbs strings #501

Merged
merged 1 commit into from
Dec 3, 2020
Merged
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/rbs/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion lib/rbs/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,10 @@ ANNOTATION_RE = Regexp.union(/%a\{.*?\}/,
/%a\(.*?\)/,
/%a\<.*?\>/,
/%a\|.*?\|/)

escape_sequences = %w[a b e f n r s t v "].map { |l| "\\\\#{l}" }
DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE = /(#{escape_sequences.join("|")})/

def next_token
if @type
type = @type
Expand Down Expand Up @@ -1373,7 +1377,21 @@ def next_token
when input.scan(/[a-z_]\w*\b/)
new_token(:tLIDENT)
when input.scan(/"(\\"|[^"])*"/)
s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\"/, '"')
s = input.matched.yield_self {|s| s[1, s.length - 2] }
.gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match|
case match
when '\\a' then "\a"
when '\\b' then "\b"
when '\\e' then "\e"
when '\\f' then "\f"
when '\\n' then "\n"
when '\\r' then "\r"
when '\\s' then "\s"
when '\\t' then "\t"
when '\\v' then "\v"
when '\\"' then '"'
end
end
new_token(:tSTRING, s)
when input.scan(/'(\\'|[^'])*'/)
s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'")
Expand Down
10 changes: 10 additions & 0 deletions test/rbs/type_parsing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,16 @@ def test_literal
assert_instance_of Types::Literal, type
assert_equal "super \" duper", type.literal
end

Parser.parse_type('"escape sequences \a\b\e\f\n\r\s\t\v\""').yield_self do |type|
assert_instance_of Types::Literal, type
assert_equal "escape sequences \a\b\e\f\n\r\s\t\v\"", type.literal
end

Parser.parse_type(%q{'not escape sequences \a\b\e\f\n\r\s\t\v\"'}).yield_self do |type|
assert_instance_of Types::Literal, type
assert_equal 'not escape sequences \a\b\e\f\n\r\s\t\v\"', type.literal
end
end

def test_record
Expand Down
2 changes: 2 additions & 0 deletions test/rbs/types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def test_to_s
assert_equal "Array[Integer]", parse_type("Array[Integer]").to_s
assert_equal "Array[Integer]?", parse_type("Array[Integer]?").to_s
assert_equal '"foo"?', parse_type('"foo" ?').to_s
assert_equal '"foo\\\\n"', parse_type(%q{'foo\n'}).to_s
assert_equal '"foo\\n"', parse_type('"foo\n"').to_s
assert_equal ":foo ?", parse_type(":foo ?").to_s
assert_equal "[ Integer, bool? ]", parse_type("[Integer, bool?]").to_s
assert_equal "[ ]", parse_type("[ ]").to_s
Expand Down