Skip to content

Commit

Permalink
Support hex escape sequences in strings (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Dec 3, 2013
1 parent 71843bc commit 6ca583c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -3,7 +3,7 @@
* Fix parsing of escapes in single-strings ('foo\n'). Only ' and \
characters now get escaped in single quoted strings. Also, more escape
sequences in double-quoted strings are now supported: `\a`, `\v`, `\f`,
`\e`, `\s`, octal (`\nnn`).
`\e`, `\s`, octal (`\314`), hex (`\xff`).

* Sourcemaps revamp. Lexer now tracks column and line info for ever token to
produce much more accurate sourcemaps. All method calls are now located on
Expand Down
2 changes: 2 additions & 0 deletions lib/opal/parser/lexer.rb
Expand Up @@ -208,6 +208,8 @@ def read_escape
" "
elsif scan(/[0-7]{1,3}/)
(matched.to_i(8) % 0x100).chr
elsif scan(/x([0-9a-fA-F]{1,2})/)
scanner[1].to_i(16).chr
else
# escaped char doesnt need escaping, so just return it
scan(/./)
Expand Down
4 changes: 4 additions & 0 deletions spec/cli/lexer_spec.rb
Expand Up @@ -65,5 +65,9 @@
it "can parse octal escape sequences" do
expect_parsed_string('"\\101\\103\\102"').to eq("ACB")
end

it "can parse hex escape sequences" do
expect_parsed_string('"\\x61\\x63\\x62"').to eq('acb')
end
end
end
4 changes: 4 additions & 0 deletions spec/opal/filters/bugs/string.rb
Expand Up @@ -232,4 +232,8 @@

fails "String#sub with pattern, replacement replaces \\\\\\+ with \\\\+"
fails "String#sub with pattern, replacement replaces \\\\\1 with \\"
fails "String#sub with pattern, replacement replaces \\\1 with \1"

fails "String#casecmp independent of case for non-ASCII characters returns -1 when numerically less than other"
fails "String#casecmp independent of case for non-ASCII characters returns 1 when numerically greater than other"
end

0 comments on commit 6ca583c

Please sign in to comment.