Skip to content

Commit

Permalink
made JS literal escape like Markdown (jashkenas/coffeescript#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Feb 1, 2013
1 parent 8410f8c commit 7915ca5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
13 changes: 7 additions & 6 deletions lib/lexer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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;
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;
exports.lex = function(code, options){
return (clone$(exports)).tokenize(code || '', options || {});
};
Expand Down Expand Up @@ -353,11 +353,12 @@ exports.doComment = function(code, index){
return this.countLines(comment).length;
};
exports.doJS = function(code, lastIndex){
var js, ref$;
var ref$, lit, js;
JSTOKEN.lastIndex = lastIndex;
js = JSTOKEN.exec(code)[0] || this.carp('unterminated JS literal');
this.token('LITERAL', (ref$ = Object(detab(js.slice(1, -1), this.dent)), ref$.js = true, ref$), true);
return this.countLines(js).length;
ref$ = JSTOKEN.exec(code), lit = ref$[0], js = ref$[2];
lit || this.carp('unterminated JS literal');
this.token('LITERAL', (ref$ = Object(detab(js, this.dent)), ref$.js = true, ref$), true);
return this.countLines(lit).length;
};
exports.doRegex = function(code, index){
var divisible, ref$, input, body, flag;
Expand Down Expand Up @@ -1455,8 +1456,8 @@ SYMBOL = /[-+*\/%&|^:]=|\.{1,3}|([-+&|@:])\1|[-~=|]>|[!=]==?|<(?:<(?:=|<{0,2})|[
SPACE = /[^\n\S]*(?:#.*)?/g;
MULTIDENT = /(?:\s*#.*)*(?:\n([^\n\S]*))+/g;
SIMPLESTR = /'[^\\']*(?:\\[\s\S][^\\']*)*'|/g;
JSTOKEN = /`[^\\`]*(?:\\[\s\S][^\\`]*)*`|/g;
BSTOKEN = /\\(?:(\S[^\s,;)}\]]*)|\s*)/g;
JSTOKEN = /(`+)([^`][\s\S]*?)\1|/g;
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;
NUMBER_OMIT = /_+/g;
REGEX = /\/([^[\/\n\\]*(?:(?:\\.|\[[^\]\n\\]*(?:\\.[^\]\n\\]*)*\])[^[\/\n\\]*)*)\/([gimy]{1,4}|\$?)|/g;
Expand Down
9 changes: 5 additions & 4 deletions src/lexer.co
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ exports import

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

# Matches a regular expression literal aka _regex_,
# disambiguating from division operators.
Expand Down Expand Up @@ -1004,8 +1005,8 @@ SYMBOL = //
SPACE = /[^\n\S]*(?:#.*)?/g
MULTIDENT = /(?:\s*#.*)*(?:\n([^\n\S]*))+/g
SIMPLESTR = /'[^\\']*(?:\\[\s\S][^\\']*)*'|/g
JSTOKEN = /`[^\\`]*(?:\\[\s\S][^\\`]*)*`|/g
BSTOKEN = // \\ (?: (\S[^\s,;)}\]]*) | \s* ) //g
JSTOKEN = /(`+)([^`][\s\S]*?)\1|/g

NUMBER = //
0x[\dA-Fa-f][\dA-Fa-f_]* # hex
Expand Down
14 changes: 6 additions & 8 deletions test/literal.co
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,14 @@ eq 11, ((, a) -> a)(, 11)


### JS Literal
ok `delete ! delete {}`

eq '\\`', `
# multi{line,tick}
eq \` ``'`'``
eq '``' ```
// Inline JS
"\\\`"
`

i = 3
`LABEL:`
while --i then while --i then `break LABEL`
eq i, 1
'``'
```

`not` = -> !it
eq false `not` true
Expand Down

0 comments on commit 7915ca5

Please sign in to comment.