the rewriter is done

This commit is contained in:
Jeremy Ashkenas
2010-01-30 18:29:53 -05:00
parent c6457e010d
commit bad50c9aee
4 changed files with 193 additions and 55 deletions

View File

@@ -1,5 +1,6 @@
(function(){
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_START, EXPRESSION_TAIL, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, SINGLE_CLOSERS, SINGLE_LINERS, __a, __b, __c, __d, __e, __f, __g, __h, pair, re;
var __hasProp = Object.prototype.hasOwnProperty;
// In order to keep the grammar simple, the stream of tokens that the Lexer
// emits is rewritten by the Rewriter, smoothing out ambiguities, mis-nested
// indentation, and single-line flavors of expressions.
@@ -55,8 +56,8 @@
this.close_open_calls_and_indexes();
this.add_implicit_parentheses();
this.add_implicit_indentation();
// this.ensure_balance(BALANCED_PAIRS)
// this.rewrite_closing_parens()
this.ensure_balance(BALANCED_PAIRS);
this.rewrite_closing_parens();
return this.tokens;
};
// Rewrite the token stream, looking one token ahead and behind.
@@ -157,12 +158,12 @@
} else if (token[0] === 'INDEX_START') {
brackets.push(0);
} else if (token[0] === '(') {
parens[-1] += 1;
parens[parens.length - 1] += 1;
} else if (token[0] === '[') {
brackets[-1] += 1;
brackets[brackets.length - 1] += 1;
} else if (token[0] === ')') {
if (parens[parens.length - 1] === 0) {
parens.pop;
parens.pop();
token[0] = 'CALL_END';
} else {
parens[parens.length - 1] -= 1;
@@ -261,4 +262,116 @@
});
})(this));
};
// Ensure that all listed pairs of tokens are correctly balanced throughout
// the course of the token stream.
re.prototype.ensure_balance = function ensure_balance(pairs) {
var __i, __j, key, levels, unclosed, value;
levels = {
};
this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) {
var __i, __j, __k, close, open;
__i = pairs;
for (__j = 0; __j < __i.length; __j++) {
pair = __i[__j];
__k = pair;
open = __k[0];
close = __k[1];
levels[open] = levels[open] || 0;
if (token[0] === open) {
levels[open] += 1;
}
if (token[0] === close) {
levels[open] -= 1;
}
if (levels[open] < 0) {
throw "too many " + token[1];
}
}
return 1;
};
return (function() {
return __func.apply(__this, arguments);
});
})(this));
unclosed = (function() {
__i = []; __j = levels;
for (key in __j) {
value = __j[key];
if (__hasProp.call(__j, key)) {
if (value > 0) {
__i.push(key);
}
}
}
return __i;
}).call(this);
if (unclosed.length) {
throw "unclosed " + unclosed[0];
}
};
// We'd like to support syntax like this:
// el.click((event) ->
// el.hide())
// In order to accomplish this, move outdents that follow closing parens
// inwards, safely. The steps to accomplish this are:
//
// 1. Check that all paired tokens are balanced and in order.
// 2. Rewrite the stream with a stack: if you see an '(' or INDENT, add it
// to the stack. If you see an ')' or OUTDENT, pop the stack and replace
// it with the inverse of what we've just popped.
// 3. Keep track of "debt" for tokens that we fake, to make sure we end
// up balanced in the end.
re.prototype.rewrite_closing_parens = function rewrite_closing_parens() {
var __i, debt, key, stack, val;
stack = [];
debt = {
};
__i = INVERSES;
for (key in __i) {
val = __i[key];
if (__hasProp.call(__i, key)) {
((debt[key] = 0));
}
}
return this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) {
var inv, match, mtag, tag;
tag = token[0];
inv = INVERSES[token[0]];
// Push openers onto the stack.
if (EXPRESSION_START.indexOf(tag) >= 0) {
stack.push(token);
return 1;
// The end of an expression, check stack and debt for a pair.
} else if (EXPRESSION_TAIL.indexOf(tag) >= 0) {
// If the tag is already in our debt, swallow it.
if (debt[inv] > 0) {
debt[inv] -= 1;
this.tokens.splice(i, 1);
return 0;
} else {
// Pop the stack of open delimiters.
match = stack.pop();
mtag = match[0];
// Continue onwards if it's the expected tag.
if (tag === INVERSES[mtag]) {
return 1;
} else {
// Unexpected close, insert correct close, adding to the debt.
debt[mtag] += 1;
val = mtag === 'INDENT' ? match[1] : INVERSES[mtag];
this.tokens.splice(i, 0, [INVERSES[mtag], val]);
return 1;
}
}
} else {
return 1;
}
};
return (function() {
return __func.apply(__this, arguments);
});
})(this));
};
})();