mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
report line number of opening token if a pair (eg bracket) is left open
This commit is contained in:
parent
b269884f8d
commit
e87fa4293d
2 changed files with 16 additions and 4 deletions
|
@ -243,8 +243,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;
|
||||
|
@ -256,6 +257,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) {
|
||||
|
@ -282,7 +286,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:
|
||||
|
|
|
@ -174,16 +174,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:
|
||||
#
|
||||
|
|
Loading…
Reference in a new issue