Skip to content

Commit

Permalink
Merge pull request #501 from johansenja/master
Browse files Browse the repository at this point in the history
Add support for escape sequences in double quoted rbs strings
  • Loading branch information
soutaro authored Dec 3, 2020
2 parents b504b93 + 20c018f commit 5e7cf2a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
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

0 comments on commit 5e7cf2a

Please sign in to comment.