diff --git a/lib/lexer.js b/lib/lexer.js index 161ae53b..fab26120 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -471,17 +471,21 @@ tokens.push(['IDENTIFIER', interp]); i += group.length - 1; pi = i + 1; - } else if (((expr = this.balanced_string(str.substring(i), ['${', '}']))) && expr.length > 3) { - inner = expr.substring(2, expr.length - 1); - nested = lexer.tokenize("(" + inner + ")", { - rewrite: false, - line: this.line - }); - nested.pop(); + } else if (((expr = this.balanced_string(str.substring(i), ['${', '}'])))) { if (pi < i) { tokens.push(['STRING', quote + (str.substring(pi, i)) + quote]); } - tokens.push(['TOKENS', nested]); + inner = expr.substring(2, expr.length - 1); + if (inner.length) { + nested = lexer.tokenize("(" + inner + ")", { + rewrite: false, + line: this.line + }); + nested.pop(); + tokens.push(['TOKENS', nested]); + } else { + tokens.push(['STRING', quote + quote]); + } i += expr.length - 1; pi = i + 1; } diff --git a/src/lexer.coffee b/src/lexer.coffee index 74c96d97..30ba0d9b 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -349,12 +349,15 @@ exports.Lexer: class Lexer tokens.push ['IDENTIFIER', interp] i += group.length - 1 pi: i + 1 - else if (expr: @balanced_string str.substring(i), ['${', '}']) and expr.length > 3 - inner: expr.substring(2, expr.length - 1) - nested: lexer.tokenize "($inner)", {rewrite: no, line: @line} - nested.pop() + else if (expr: @balanced_string str.substring(i), ['${', '}']) tokens.push ['STRING', "$quote${ str.substring(pi, i) }$quote"] if pi < i - tokens.push ['TOKENS', nested] + inner: expr.substring(2, expr.length - 1) + if inner.length + nested: lexer.tokenize "($inner)", {rewrite: no, line: @line} + nested.pop() + tokens.push ['TOKENS', nested] + else + tokens.push ['STRING', "$quote$quote"] i += expr.length - 1 pi: i + 1 i += 1 diff --git a/test/test_string_interpolation.coffee b/test/test_string_interpolation.coffee index 031ad543..5c454544 100644 --- a/test/test_string_interpolation.coffee +++ b/test/test_string_interpolation.coffee @@ -28,7 +28,8 @@ ok "Escaping \$last" is 'Escaping $last' ok "Escaping \${last}" is 'Escaping ${last}' ok "$$" is '$$' -ok "${}" is '${}' +ok "${}" is '' +ok "${}A${} ${} ${}B${}" is 'A B' ok "\\\\\$$" is '\\\\\$$' ok "\\\${}" is '\\${}'