Skip to content

Commit

Permalink
Fix backtracking string regexes in JavascriptLexer und TypescriptLexer.
Browse files Browse the repository at this point in the history
fixes #1637
  • Loading branch information
birkenfeld committed Dec 17, 2020
1 parent a507918 commit 7958cd6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -17,6 +17,7 @@ Version 2.7.4
(not released yet)

- Fixed infinite loop in SML lexer (#1625)
- Fixed backtracking string regexes in JavaScript/TypeScript lexers (#1637)


Version 2.7.3
Expand Down
29 changes: 24 additions & 5 deletions pygments/lexers/javascript.py
Expand Up @@ -95,10 +95,20 @@ class JavascriptLexer(RegexLexer):
r'Error|eval|isFinite|isNaN|isSafeInteger|parseFloat|parseInt|'
r'document|this|window)\b', Name.Builtin),
(JS_IDENT, Name.Other),
(r'"(\\\\|\\"|[^"])*"', String.Double),
(r"'(\\\\|\\'|[^'])*'", String.Single),
(r'"', String.Double, 'string-double'),
(r"'", String.Single, 'string-single'),
(r'`', String.Backtick, 'interp'),
],
'string-double': [
(r'\\.', String.Double),
(r'[^\\"]+', String.Double),
(r'"', String.Double, '#pop'),
],
'string-single': [
(r'\\.', String.Single),
(r"[^\\']+", String.Single),
(r"'", String.Single, '#pop'),
],
'interp': [
(r'`', String.Backtick, '#pop'),
(r'\\\\', String.Backtick),
Expand All @@ -112,7 +122,6 @@ class JavascriptLexer(RegexLexer):
(r'\}', String.Interpol, '#pop'),
include('root'),
],
# (\\\\|\\`|[^`])*`', String.Backtick),
}


Expand Down Expand Up @@ -522,12 +531,22 @@ class TypeScriptLexer(RegexLexer):
(r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
(r'0x[0-9a-fA-F]+', Number.Hex),
(r'[0-9]+', Number.Integer),
(r'"(\\\\|\\"|[^"])*"', String.Double),
(r"'(\\\\|\\'|[^'])*'", String.Single),
(r'"', String.Double, 'string-double'),
(r"'", String.Single, 'string-single'),
(r'`', String.Backtick, 'interp'),
# Match stuff like: Decorators
(r'@\w+', Keyword.Declaration),
],
'string-double': [
(r'\\.', String.Double),
(r'[^\\"]+', String.Double),
(r'"', String.Double, '#pop'),
],
'string-single': [
(r'\\.', String.Single),
(r"[^\\']+", String.Single),
(r"'", String.Single, '#pop'),
],

# The 'interp*' rules match those in JavascriptLexer. Changes made
# there should be reflected here as well.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_html_lexer.py
Expand Up @@ -65,7 +65,9 @@ def test_long_unclosed_javascript_fragment(lexer_html):
tokens_body = [
(Token.Name.Other, 'alert'),
(Token.Punctuation, '('),
(Token.Literal.String.Double, '"hi"'),
(Token.Literal.String.Double, '"'),
(Token.Literal.String.Double, 'hi'),
(Token.Literal.String.Double, '"'),
(Token.Punctuation, ')'),
(Token.Punctuation, ';'),
]
Expand Down

0 comments on commit 7958cd6

Please sign in to comment.