Skip to content

Commit

Permalink
Merge pull request #4490 from helixbass/iss4489_regex_octal_escape_se…
Browse files Browse the repository at this point in the history
…quence_bug

Fix #4489: Regex octal escape sequence bug
  • Loading branch information
lydell authored Apr 7, 2017
2 parents c1e3c02 + 050aaa4 commit 90ec761
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/coffee-script/lexer.js

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

18 changes: 15 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,14 @@ exports.Lexer = class Lexer

# Validates escapes in strings and regexes.
validateEscapes: (str, options = {}) ->
match = INVALID_ESCAPE.exec str
invalid_escape_regex =
if options.isRegex
REGEX_INVALID_ESCAPE
else
STRING_INVALID_ESCAPE
match = invalid_escape_regex.exec str
return unless match
[[], before, octal, hex, unicode] = match
return if options.isRegex and octal and octal.charAt(0) isnt '0'
message =
if octal
"octal escape sequences are not allowed"
Expand Down Expand Up @@ -978,14 +982,22 @@ HERECOMMENT_ILLEGAL = /\*\//
LINE_CONTINUER = /// ^ \s* (?: , | \??\.(?![.\d]) | :: ) ///
INVALID_ESCAPE = ///
STRING_INVALID_ESCAPE = ///
( (?:^|[^\\]) (?:\\\\)* ) # make sure the escape isn’t escaped
\\ (
?: (0[0-7]|[1-7]) # octal escape
| (x(?![\da-fA-F]{2}).{0,2}) # hex escape
| (u(?![\da-fA-F]{4}).{0,4}) # unicode escape
)
///
REGEX_INVALID_ESCAPE = ///
( (?:^|[^\\]) (?:\\\\)* ) # make sure the escape isn’t escaped
\\ (
?: (0[0-7]) # octal escape
| (x(?![\da-fA-F]{2}).{0,2}) # hex escape
| (u(?![\da-fA-F]{4}).{0,4}) # unicode escape
)
///
LEADING_BLANK_LINE = /^[^\n\S]*\n/
TRAILING_BLANK_LINE = /\n[^\n\S]*$/
Expand Down
7 changes: 7 additions & 0 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ test "octal escapes", ->
/a\\0\\tb\\\\\\07c/
\ \ \ \ ^\^^
'''
assertErrorFormat '''
/a\\1\\tb\\\\\\07c/
''', '''
[stdin]:1:10: error: octal escape sequences are not allowed \\07
/a\\1\\tb\\\\\\07c/
\ \ \ \ ^\^^
'''
assertErrorFormat '''
///a
#{b} \\01///
Expand Down

0 comments on commit 90ec761

Please sign in to comment.