From 45b71936741ec30abab3c84c9704d29293337ccd Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Tue, 15 Nov 2016 14:39:54 -0800 Subject: [PATCH] Updated JavaScript token regexes per @lydell --- lib/coffee-script/lexer.js | 8 +++++--- src/lexer.coffee | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 831f8cf161..bdbb308809 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.11.1 (function() { - var BOM, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HERECOMMENT_ILLEGAL, HEREDOC_DOUBLE, HEREDOC_INDENT, HEREDOC_SINGLE, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDENTABLE_CLOSERS, INDEXABLE, INVALID_ESCAPE, INVERSES, JSTOKEN, JS_KEYWORDS, LEADING_BLANK_LINE, LINE_BREAK, LINE_CONTINUER, Lexer, MATH, MULTI_DENT, NOT_REGEX, NUMBER, OPERATOR, POSSIBLY_DIVISION, REGEX, REGEX_FLAGS, REGEX_ILLEGAL, RELATION, RESERVED, Rewriter, SHIFT, SIMPLE_STRING_OMIT, STRICT_PROSCRIBED, STRING_DOUBLE, STRING_OMIT, STRING_SINGLE, STRING_START, TRAILING_BLANK_LINE, TRAILING_SPACES, UNARY, UNARY_MATH, VALID_FLAGS, WHITESPACE, compact, count, invertLiterate, isUnassignable, key, locationDataToString, ref, ref1, repeat, starts, throwSyntaxError, + var BOM, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HERECOMMENT_ILLEGAL, HEREDOC_DOUBLE, HEREDOC_INDENT, HEREDOC_SINGLE, HEREGEX, HEREGEX_OMIT, HERE_JSTOKEN, IDENTIFIER, INDENTABLE_CLOSERS, INDEXABLE, INVALID_ESCAPE, INVERSES, JSTOKEN, JS_KEYWORDS, LEADING_BLANK_LINE, LINE_BREAK, LINE_CONTINUER, Lexer, MATH, MULTI_DENT, NOT_REGEX, NUMBER, OPERATOR, POSSIBLY_DIVISION, REGEX, REGEX_FLAGS, REGEX_ILLEGAL, RELATION, RESERVED, Rewriter, SHIFT, SIMPLE_STRING_OMIT, STRICT_PROSCRIBED, STRING_DOUBLE, STRING_OMIT, STRING_SINGLE, STRING_START, TRAILING_BLANK_LINE, TRAILING_SPACES, UNARY, UNARY_MATH, VALID_FLAGS, WHITESPACE, compact, count, invertLiterate, isUnassignable, key, locationDataToString, ref, ref1, repeat, starts, throwSyntaxError, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, slice = [].slice; @@ -337,7 +337,7 @@ Lexer.prototype.jsToken = function() { var js, match, script; - if (!(this.chunk.charAt(0) === '`' && (match = JSTOKEN.exec(this.chunk)))) { + if (!(this.chunk.charAt(0) === '`' && (match = HERE_JSTOKEN.exec(this.chunk) || JSTOKEN.exec(this.chunk)))) { return 0; } js = match[0]; @@ -1012,7 +1012,9 @@ MULTI_DENT = /^(?:\n[^\n\S]*)+/; - JSTOKEN = /^```([\s\S]*?)(?:\\`(```)|```)|^`[^\\`]*(?:\\.[^\\`]*)*`/; + JSTOKEN = /^`(?!``)((?:[^`\\]|\\[\s\S])*)`/; + + HERE_JSTOKEN = /^```((?:[^`\\]|\\[\s\S]|`(?!``))*)```/; STRING_START = /^(?:'''|"""|'|")/; diff --git a/src/lexer.coffee b/src/lexer.coffee index d55bcc4ab4..18356a62fa 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -292,7 +292,8 @@ exports.Lexer = class Lexer # Matches JavaScript interpolated directly into the source via backticks. jsToken: -> - return 0 unless @chunk.charAt(0) is '`' and match = JSTOKEN.exec @chunk + return 0 unless @chunk.charAt(0) is '`' and + (match = HERE_JSTOKEN.exec(@chunk) or JSTOKEN.exec(@chunk)) [js] = match script = if js[0..2] is '```' then js[3...-3] else js[1...-1] script = script.replace /\\`/g, '`' # Convert escaped backticks to backticks @@ -903,7 +904,10 @@ CODE = /^[-=]>/ MULTI_DENT = /^(?:\n[^\n\S]*)+/ -JSTOKEN = /^```([\s\S]*?)(?:\\`(```)|```)|^`[^\\`]*(?:\\.[^\\`]*)*`/ +JSTOKEN = ///^ `(?!``) ((?: [^`\\] | \\[\s\S] )*) ` /// +HERE_JSTOKEN = ///^ ``` ((?: [^`\\] | \\[\s\S] | `(?!``) )*) ``` /// + + # String-matching-regexes. STRING_START = /^(?:'''|"""|'|")/