From e47bea34d597be78a52432861905da71be589d90 Mon Sep 17 00:00:00 2001 From: gfxmonk Date: Fri, 12 Mar 2010 14:55:35 +1100 Subject: [PATCH 1/3] report line number of opening token if a pair (eg bracket) is left open --- lib/rewriter.js | 10 ++++++++-- src/rewriter.coffee | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/rewriter.js b/lib/rewriter.js index 98f5b5f1..b9917fae 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -241,8 +241,9 @@ // Ensure that all listed pairs of tokens are correctly balanced throughout // the course of the token stream. Rewriter.prototype.ensure_balance = function ensure_balance(pairs) { - var _a, _b, key, levels, unclosed, value; + var _a, _b, key, levels, line, open, open_line, unclosed, value; levels = {}; + open_line = {}; this.scan_tokens((function(__this) { var __func = function(prev, token, post, i) { var _a, _b, _c, _d, close, open, pair; @@ -254,6 +255,9 @@ close = _d[1]; levels[open] = levels[open] || 0; if (token[0] === open) { + if (levels[open] === 0) { + open_line[open] = token[2]; + } levels[open] += 1; } if (token[0] === close) { @@ -280,7 +284,9 @@ return _a; }).call(this); if (unclosed.length) { - throw new Error("unclosed " + (unclosed[0])); + open = unclosed[0]; + line = open_line[open] + 1; + throw new Error("unclosed " + (open) + " on line " + (line)); } }; // We'd like to support syntax like this: diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 065ab5cf..b590fe7d 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -171,16 +171,22 @@ exports.Rewriter: class Rewriter # the course of the token stream. ensure_balance: (pairs) -> levels: {} + open_line: {} @scan_tokens (prev, token, post, i) => for pair in pairs [open, close]: pair levels[open] ||= 0 - levels[open] += 1 if token[0] is open + if token[0] is open + open_line[open]: token[2] if levels[open] == 0 + levels[open] += 1 levels[open] -= 1 if token[0] is close throw new Error("too many ${token[1]} on line ${token[2] + 1}") if levels[open] < 0 return 1 unclosed: key for key, value of levels when value > 0 - throw new Error("unclosed ${unclosed[0]}") if unclosed.length + if unclosed.length + open: unclosed[0] + line: open_line[open] + 1 + throw new Error("unclosed ${open} on line ${line}") # We'd like to support syntax like this: # From 5809a1637f3a7e535dd0b9896eafc3793ff71706 Mon Sep 17 00:00:00 2001 From: gfxmonk Date: Sat, 13 Mar 2010 23:49:51 +1100 Subject: [PATCH 2/3] fixed non-recursive traverse() for CodeNode --- lib/nodes.js | 2 +- src/nodes.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index ddb5f1fd..70b9391e 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -957,7 +957,7 @@ _a = []; _b = this.real_children(); for (_c = 0, _d = _b.length; _c < _d; _c++) { child = _b[_c]; - _a.push(block(child)); + _a.push(child.traverse(block)); } return _a; }; diff --git a/src/nodes.coffee b/src/nodes.coffee index 15523dd2..8b7b3bb1 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -722,7 +722,7 @@ exports.CodeNode: class CodeNode extends BaseNode # Custom `traverse` implementation that uses the `real_children`. traverse: (block) -> block this - block(child) for child in @real_children() + child.traverse block for child in @real_children() toString: (idt) -> idt ||= '' From 1da00f0ee371f4c76677a768efa9b4fa01369ac9 Mon Sep 17 00:00:00 2001 From: gfxmonk Date: Sun, 14 Mar 2010 11:29:44 +1100 Subject: [PATCH 3/3] added a test case for broken "break" statement --- test/test_break.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/test_break.coffee diff --git a/test/test_break.coffee b/test/test_break.coffee new file mode 100644 index 00000000..e3b55c85 --- /dev/null +++ b/test/test_break.coffee @@ -0,0 +1,9 @@ +a: [1,2,3] +call_with_lambda: (l) -> null +for i in a + a: call_with_lambda(->) + if i == 2 + puts "i = 2" + else + break +