Skip to content

Commit 7915ca5

Browse files
committed
made JS literal escape like Markdown (jashkenas/coffeescript#1504)
1 parent 8410f8c commit 7915ca5

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lib/lexer.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var string, TABS, unlines, enlines, enslash, reslash, camelize, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, JSTOKEN, BSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, INLINEDENT, NONASCII, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, BLOCK_USERS, slice$ = [].slice;
1+
var string, TABS, unlines, enlines, enslash, reslash, camelize, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, BSTOKEN, JSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, INLINEDENT, NONASCII, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, BLOCK_USERS, slice$ = [].slice;
22
exports.lex = function(code, options){
33
return (clone$(exports)).tokenize(code || '', options || {});
44
};
@@ -353,11 +353,12 @@ exports.doComment = function(code, index){
353353
return this.countLines(comment).length;
354354
};
355355
exports.doJS = function(code, lastIndex){
356-
var js, ref$;
356+
var ref$, lit, js;
357357
JSTOKEN.lastIndex = lastIndex;
358-
js = JSTOKEN.exec(code)[0] || this.carp('unterminated JS literal');
359-
this.token('LITERAL', (ref$ = Object(detab(js.slice(1, -1), this.dent)), ref$.js = true, ref$), true);
360-
return this.countLines(js).length;
358+
ref$ = JSTOKEN.exec(code), lit = ref$[0], js = ref$[2];
359+
lit || this.carp('unterminated JS literal');
360+
this.token('LITERAL', (ref$ = Object(detab(js, this.dent)), ref$.js = true, ref$), true);
361+
return this.countLines(lit).length;
361362
};
362363
exports.doRegex = function(code, index){
363364
var divisible, ref$, input, body, flag;
@@ -1455,8 +1456,8 @@ SYMBOL = /[-+*\/%&|^:]=|\.{1,3}|([-+&|@:])\1|[-~=|]>|[!=]==?|<(?:<(?:=|<{0,2})|[
14551456
SPACE = /[^\n\S]*(?:#.*)?/g;
14561457
MULTIDENT = /(?:\s*#.*)*(?:\n([^\n\S]*))+/g;
14571458
SIMPLESTR = /'[^\\']*(?:\\[\s\S][^\\']*)*'|/g;
1458-
JSTOKEN = /`[^\\`]*(?:\\[\s\S][^\\`]*)*`|/g;
14591459
BSTOKEN = /\\(?:(\S[^\s,;)}\]]*)|\s*)/g;
1460+
JSTOKEN = /(`+)([^`][\s\S]*?)\1|/g;
14601461
NUMBER = /0x[\dA-Fa-f][\dA-Fa-f_]*|([2-9]|[12]\d|3[0-6])r([\dA-Za-z]\w*)|((\d[\d_]*)(\.\d[\d_]*)?(?:e[+-]?\d[\d_]*)?)[$\w]*|/g;
14611462
NUMBER_OMIT = /_+/g;
14621463
REGEX = /\/([^[\/\n\\]*(?:(?:\\.|\[[^\]\n\\]*(?:\\.[^\]\n\\]*)*\])[^[\/\n\\]*)*)\/([gimy]{1,4}|\$?)|/g;

src/lexer.co

+5-4
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ exports import
244244

245245
# Matches embedded JavaScript.
246246
doJS: (code, JSTOKEN.lastIndex) ->
247-
js = JSTOKEN.exec code .0 or @carp 'unterminated JS literal'
248-
@token \LITERAL Object(detab js.slice(1 -1), @dent) <<< {+js}, true
249-
@countLines(js)length
247+
[lit,, js] = JSTOKEN.exec code
248+
lit or @carp 'unterminated JS literal'
249+
@token \LITERAL Object(detab js, @dent) <<< {+js}, true
250+
@countLines(lit)length
250251

251252
# Matches a regular expression literal aka _regex_,
252253
# disambiguating from division operators.
@@ -1004,8 +1005,8 @@ SYMBOL = //
10041005
SPACE = /[^\n\S]*(?:#.*)?/g
10051006
MULTIDENT = /(?:\s*#.*)*(?:\n([^\n\S]*))+/g
10061007
SIMPLESTR = /'[^\\']*(?:\\[\s\S][^\\']*)*'|/g
1007-
JSTOKEN = /`[^\\`]*(?:\\[\s\S][^\\`]*)*`|/g
10081008
BSTOKEN = // \\ (?: (\S[^\s,;)}\]]*) | \s* ) //g
1009+
JSTOKEN = /(`+)([^`][\s\S]*?)\1|/g
10091010

10101011
NUMBER = //
10111012
0x[\dA-Fa-f][\dA-Fa-f_]* # hex

test/literal.co

+6-8
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,14 @@ eq 11, ((, a) -> a)(, 11)
362362

363363

364364
### JS Literal
365+
ok `delete ! delete {}`
365366

366-
eq '\\`', `
367+
# multi{line,tick}
368+
eq \` ``'`'``
369+
eq '``' ```
367370
// Inline JS
368-
"\\\`"
369-
`
370-
371-
i = 3
372-
`LABEL:`
373-
while --i then while --i then `break LABEL`
374-
eq i, 1
371+
'``'
372+
```
375373

376374
`not` = -> !it
377375
eq false `not` true

0 commit comments

Comments
 (0)