From 188ad03c1739d1b72343f20d707bfb6e78980f6d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 9 Nov 2010 00:36:13 -0500 Subject: [PATCH] coco b615379: optimized @ensureBalance --- lib/rewriter.js | 40 +++++++++++++++------------------------- src/rewriter.coffee | 17 +++++++---------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/lib/rewriter.js b/lib/rewriter.js index 14a3b644..30c818cf 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -253,42 +253,32 @@ }); }; exports.Rewriter.prototype.ensureBalance = function(pairs) { - var key, levels, open, openLine, unclosed, value, _results; + var close, level, levels, open, openLine, tag, token, _i, _j, _len, _len2, _ref, _ref2; levels = {}; openLine = {}; - this.scanTokens(function(token, i) { - var close, open, tag, _i, _len, _ref; + _ref = this.tokens; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + token = _ref[_i]; tag = token[0]; - for (_i = 0, _len = pairs.length; _i < _len; _i++) { - _ref = pairs[_i], open = _ref[0], close = _ref[1]; + for (_j = 0, _len2 = pairs.length; _j < _len2; _j++) { + _ref2 = pairs[_j], open = _ref2[0], close = _ref2[1]; levels[open] |= 0; if (tag === open) { - if (levels[open] === 0) { + if (levels[open]++ === 0) { openLine[open] = token[2]; } - levels[open] += 1; - } else if (tag === close) { - levels[open] -= 1; - } - if (levels[open] < 0) { + } else if (tag === close && --levels[open] < 0) { throw Error("too many " + token[1] + " on line " + (token[2] + 1)); } } - return 1; - }); - unclosed = ((function() { - _results = []; - for (key in levels) { - value = levels[key]; - if (value > 0) { - _results.push(key); - } - } - return _results; - })()); - if (unclosed.length) { - throw Error("unclosed " + (open = unclosed[0]) + " on line " + (openLine[open] + 1)); } + for (open in levels) { + level = levels[open]; + if (level > 0) { + throw Error("unclosed " + open + " on line " + (openLine[open] + 1)); + } + } + return this; }; exports.Rewriter.prototype.rewriteClosingParens = function() { var debt, key, stack; diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 41cd3fc7..50e9ec77 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -204,20 +204,17 @@ class exports.Rewriter ensureBalance: (pairs) -> levels = {} openLine = {} - @scanTokens (token, i) -> + for token in @tokens [tag] = token for [open, close] in pairs levels[open] |= 0 if tag is open - openLine[open] = token[2] if levels[open] is 0 - levels[open] += 1 - else if tag is close - levels[open] -= 1 - throw Error "too many #{token[1]} on line #{token[2] + 1}" if levels[open] < 0 - 1 - unclosed = (key for all key, value of levels when value > 0) - if unclosed.length - throw Error "unclosed #{ open = unclosed[0] } on line #{openLine[open] + 1}" + openLine[open] = token[2] if levels[open]++ is 0 + else if tag is close and --levels[open] < 0 + throw Error "too many #{token[1]} on line #{token[2] + 1}" + for all open, level of levels when level > 0 + throw Error "unclosed #{ open } on line #{openLine[open] + 1}" + this # We'd like to support syntax like this: #