merging in gfodor's excellent Rewriter patch.

This commit is contained in:
Jeremy Ashkenas 2010-04-19 23:18:39 -04:00
parent 90472685e8
commit aac9679282
3 changed files with 23 additions and 17 deletions

View File

@ -285,6 +285,8 @@
// it with the inverse of what we've just popped.
// 3. Keep track of "debt" for tokens that we manufacture, to make sure we end
// up balanced in the end.
// 4. Be careful not to alter array or parentheses delimiters with overzealous
// rewriting.
Rewriter.prototype.rewrite_closing_parens = function rewrite_closing_parens() {
var _a, debt, key, stack, val;
stack = [];
@ -295,7 +297,7 @@
(debt[key] = 0);
}}
return this.scan_tokens(__bind(function(prev, token, post, i) {
var inv, match, mtag, tag;
var inv, match, mtag, oppos, tag;
tag = token[0];
inv = INVERSES[token[0]];
if (include(EXPRESSION_START, tag)) {
@ -309,12 +311,18 @@
} else {
match = stack.pop();
mtag = match[0];
if (tag === INVERSES[mtag]) {
oppos = INVERSES[mtag];
if (tag === oppos) {
return 1;
}
debt[mtag] += 1;
val = mtag === 'INDENT' ? match[1] : INVERSES[mtag];
this.tokens.splice(i, 0, [INVERSES[mtag], val]);
val = [oppos, mtag === 'INDENT' ? match[1] : oppos];
if ((this.tokens[i + 2] == undefined ? undefined : this.tokens[i + 2][0]) === mtag) {
this.tokens.splice(i + 3, 0, val);
stack.push(match);
} else {
this.tokens.splice(i, 0, val);
}
return 1;
}
} else {

View File

@ -209,7 +209,8 @@ exports.Rewriter: class Rewriter
# it with the inverse of what we've just popped.
# 3. Keep track of "debt" for tokens that we manufacture, to make sure we end
# up balanced in the end.
#
# 4. Be careful not to alter array or parentheses delimiters with overzealous
# rewriting.
rewrite_closing_parens: ->
stack: []
debt: {}
@ -228,19 +229,15 @@ exports.Rewriter: class Rewriter
else
match: stack.pop()
mtag: match[0]
return 1 if tag is INVERSES[mtag]
oppos: INVERSES[mtag]
return 1 if tag is oppos
debt[mtag]: + 1
val: if mtag is 'INDENT' then match[1] else INVERSES[mtag]
# Edge case, lookahead and if we are inserting in front of a
# subsequent opening token, insert ahead of it and re-queue onto
# stack.
if @tokens[i + 2]?[0] == mtag
@tokens.splice i + 3, 0, [INVERSES[mtag], val]
val: [oppos, if mtag is 'INDENT' then match[1] else oppos]
if @tokens[i + 2]?[0] is mtag
@tokens.splice i + 3, 0, val
stack.push(match)
else
@tokens.splice i, 0, [INVERSES[mtag], val]
@tokens.splice i, 0, val
return 1
else
return 1

View File

@ -35,12 +35,13 @@ six:
ok six is 6
# Bug due to rewriting issue with indented array literals
# Ensure that indented array literals don't trigger whitespace rewriting.
func: () ->
ok arguments.length is 1
func(
[[[[[],
[[[[[],
[]],
[[]]]],
[]])