1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

fixing the nested-implicit-parens-on-a-single-line

This commit is contained in:
Jeremy Ashkenas 2010-03-09 21:54:44 -05:00
parent b297510d2b
commit 472e027463
3 changed files with 34 additions and 8 deletions

View file

@ -144,19 +144,28 @@
// 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 stack;
var calls, stack;
stack = [0];
calls = 0;
return this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) {
var _a, _b, _c, _d, idx, last, size, stack_pointer, tag, tmp;
tag = token[0];
if (tag === 'INDENT') {
if (tag === 'CALL_START') {
calls += 1;
} else if (tag === 'CALL_END') {
calls -= 1;
} else if (tag === 'INDENT') {
stack.push(0);
}
if (tag === 'OUTDENT') {
} else if (tag === 'OUTDENT') {
last = stack.pop();
stack[stack.length - 1] += last;
}
if (tag === 'CALL_END' && calls < 0) {
stack[stack.length - 1] -= 1;
this.tokens.splice(i, 0, ['CALL_END', ')', token[2]]);
return 2;
}
if (!(typeof post !== "undefined" && post !== null) || include(IMPLICIT_END, tag)) {
if (tag === 'INDENT' && prev && include(IMPLICIT_BLOCK, prev[0])) {
return 1;
@ -176,6 +185,7 @@
if (!(prev && include(IMPLICIT_FUNC, prev[0]) && include(IMPLICIT_CALL, tag))) {
return 1;
}
calls = 0;
this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
stack[stack.length - 1] += 1;
return 2;

View file

@ -106,12 +106,20 @@ exports.Rewriter: class Rewriter
# deal with them.
add_implicit_parentheses: ->
stack: [0]
calls: 0
@scan_tokens (prev, token, post, i) =>
tag: token[0]
stack.push(0) if tag is 'INDENT'
if tag is 'OUTDENT'
last: stack.pop()
stack[stack.length - 1] += last
switch tag
when 'CALL_START' then calls += 1
when 'CALL_END' then calls -= 1
when 'INDENT' then stack.push(0)
when 'OUTDENT'
last: stack.pop()
stack[stack.length - 1] += last
if tag is 'CALL_END' and calls < 0
stack[stack.length - 1] -= 1
@tokens.splice(i, 0, ['CALL_END', ')', token[2]])
return 2
if !post? or include IMPLICIT_END, tag
return 1 if tag is 'INDENT' and prev and include IMPLICIT_BLOCK, prev[0]
if stack[stack.length - 1] > 0 or tag is 'INDENT'
@ -123,6 +131,7 @@ exports.Rewriter: class Rewriter
stack[stack.length - stack_pointer]: 0
return size
return 1 unless prev and include(IMPLICIT_FUNC, prev[0]) and include IMPLICIT_CALL, tag
calls: 0
@tokens.splice(i, 0, ['CALL_START', '(', token[2]])
stack[stack.length - 1] += 1
return 2

View file

@ -73,6 +73,13 @@ result: call ->
ok result is 10
# More fun with optional parens.
fn: (arg) -> arg
ok fn(fn {prop: 101}).prop is 101
# And even with strange things like this:
funcs: [((x) -> x), ((x) -> x * x)]