making the Rewriter's add_implicit_calls more sensitive of parenthetical arguments.

This commit is contained in:
Jeremy Ashkenas 2010-03-28 17:12:30 -04:00
parent 1e315b5a33
commit 83c0e77ca8
4 changed files with 24 additions and 7 deletions

View File

@ -54,8 +54,8 @@
exports.run = (function(code, options) {
var __dirname, __filename;
module.filename = (__filename = options.source);
__dirname = path.dirname(__filename);
return eval(exports.compile(code, options));
__dirname = path.dirname(__filename, eval(exports.compile(code, options)));
return __dirname;
});
// Extend CoffeeScript with a custom language extension. It should hook in to
// the **Lexer** (as a peer of any of the lexer's tokenizing methods), and

View File

@ -150,9 +150,10 @@
// Insert the implicit parentheses here, so that the parser doesn't have to
// deal with them.
Rewriter.prototype.add_implicit_parentheses = function add_implicit_parentheses() {
var calls, stack;
var calls, parens, stack;
stack = [0];
calls = 0;
parens = 0;
return this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) {
var _a, _b, _c, idx, last, open, size, stack_pointer, tag, tmp;
@ -161,6 +162,10 @@
calls += 1;
} else if (tag === 'CALL_END') {
calls -= 1;
} else if (tag === '(') {
parens += 1;
} else if (tag === ')') {
parens -= 1;
} else if (tag === 'INDENT') {
stack.push(0);
} else if (tag === 'OUTDENT') {
@ -173,7 +178,7 @@
this.tokens.splice(i, 0, ['CALL_END', ')', token[2]]);
return 2;
}
if (!(typeof post !== "undefined" && post !== null) || include(IMPLICIT_END, tag)) {
if (!(typeof post !== "undefined" && post !== null) || (parens === 0 && include(IMPLICIT_END, tag))) {
if (tag === 'INDENT' && prev && include(IMPLICIT_BLOCK, prev[0])) {
return 1;
}

View File

@ -110,11 +110,14 @@ exports.Rewriter: class Rewriter
add_implicit_parentheses: ->
stack: [0]
calls: 0
parens: 0
@scan_tokens (prev, token, post, i) =>
tag: token[0]
switch tag
when 'CALL_START' then calls: + 1
when 'CALL_END' then calls: - 1
when '(' then parens: + 1
when ')' then parens: - 1
when 'INDENT' then stack.push(0)
when 'OUTDENT'
last: stack.pop()
@ -124,7 +127,7 @@ exports.Rewriter: class Rewriter
stack[stack.length - 1]: - 1
@tokens.splice(i, 0, ['CALL_END', ')', token[2]])
return 2
if !post? or include IMPLICIT_END, tag
if !post? or (parens is 0 and include IMPLICIT_END, tag)
return 1 if tag is 'INDENT' and prev and include IMPLICIT_BLOCK, prev[0]
return 1 if tag is 'OUTDENT' and token.generated
if open or tag is 'INDENT'
@ -160,7 +163,7 @@ exports.Rewriter: class Rewriter
pre: @tokens[idx - 1]
if (not tok or
(include(SINGLE_CLOSERS, tok[0]) and tok[1] isnt ';') or
(tok[0] is ')' && parens is 0)) and
(tok[0] is ')' and parens is 0)) and
not (starter is 'ELSE' and tok[0] is 'ELSE')
insertion: if pre[0] is "," then idx - 1 else idx
outdent: ['OUTDENT', 2, token[2]]

View File

@ -9,3 +9,12 @@ single_liner:
ok multi_liner.length is single_liner.length
ok 5 is multi_liner[2][2][1]
ok 5 is single_liner[2][2][1]
# Test comprehensions within parentheses.
result: null
store: (obj) -> result: obj
store (x * 2 for x in [3, 2, 1])
ok result.join(' ') is '6 4 2'