minor refactors to balanced_string

This commit is contained in:
Jeremy Ashkenas
2010-03-08 22:48:14 -05:00
parent b33b688373
commit dcb00b4fe8
2 changed files with 40 additions and 41 deletions

View File

@@ -169,7 +169,8 @@
};
// Matches regular expression literals. Lexing regular expressions is difficult
// to distinguish from division, so we borrow some basic heuristics from
// JavaScript and Ruby.
// JavaScript and Ruby, borrow slash balancing from `@balanced_token`, and
// borrow interpolation from `@interpolate_string`.
Lexer.prototype.regex_token = function regex_token() {
var flags, regex, str;
if (!(this.chunk.match(REGEX_START))) {
@@ -412,26 +413,27 @@
levels = [];
i = 0;
while (i < str.length) {
_a = delimited;
for (_b = 0, _c = _a.length; _b < _c; _b++) {
pair = _a[_b];
_d = pair;
open = _d[0];
close = _d[1];
if (levels.length && starts(str, '\\', i)) {
i += 1;
break;
} else if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
levels.pop();
i += close.length - 1;
if (!(levels.length)) {
i += 1;
if (levels.length && starts(str, '\\', i)) {
i += 1;
} else {
_a = delimited;
for (_b = 0, _c = _a.length; _b < _c; _b++) {
pair = _a[_b];
_d = pair;
open = _d[0];
close = _d[1];
if (levels.length && starts(str, close, i) && levels[levels.length - 1] === pair) {
levels.pop();
i += close.length - 1;
if (!(levels.length)) {
i += 1;
}
break;
} else if (starts(str, open, i)) {
levels.push(pair);
i += open.length - 1;
break;
}
break;
} else if (starts(str, open, i)) {
levels.push(pair);
i += open.length - 1;
break;
}
}
if (!levels.length || slash && starts(str, '\n', i)) {
@@ -445,10 +447,7 @@
}
throw new Error("SyntaxError: Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
}
if (i === 0) {
return false;
}
return str.substring(0, i);
return !i ? false : str.substring(0, i);
};
// Expand variables and expressions inside double-quoted strings using
// [ECMA Harmony's interpolation syntax](http://wiki.ecmascript.org/doku.php?id=strawman:string_interpolation)