Improving performance by removing call to bound function for every character in a string. Added a third option to \`delimited\` to allow custom escape sequences.

This commit is contained in:
Stan Angeloff 2010-03-06 22:52:28 +02:00 committed by Jeremy Ashkenas
parent 03bc897b39
commit f9cde1b46d
2 changed files with 36 additions and 42 deletions

View File

@ -217,43 +217,38 @@
};
// Matches a balanced group such as a single or double-quoted string.
Lexer.prototype.balanced_group = function balanced_group() {
var _a, _b, _c, _d, _e, _f, delimited, each, escaped, i, levels, next, type;
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, delimited, each, i, levels, type;
delimited = Array.prototype.slice.call(arguments, 0);
_a = delimited;
for (_b = 0, _c = _a.length; _b < _c; _b++) {
each = _a[_b];
!(typeof (_d = each[1]) !== "undefined" && _d !== null) ? ((each[1] = each[0])) : null;
}
escaped = '\\';
next = (function(__this) {
var __func = function(length) {
return this.chunk.substring(i, i + length);
};
return (function next() {
return __func.apply(__this, arguments);
});
})(this);
_e = delimited;
for (_f = 0, _g = _e.length; _f < _g; _f++) {
each = _e[_f];
!(typeof (_h = each[2]) !== "undefined" && _h !== null) ? ((each[2] = '\\')) : null;
}
levels = [];
i = 0;
while (i < this.chunk.length) {
if (next(1) === escaped) {
i += 1;
} else {
_e = delimited;
for (type = 0, _f = _e.length; type < _f; type++) {
each = _e[type];
if (levels.length && next(each[1].length) === each[1] && levels[levels.length - 1] === type) {
levels.pop();
i += each[1].length - 1;
if (!(levels.length)) {
i += 1;
}
break;
} else if (next(each[0].length) === each[0]) {
levels.push(type);
i += each[0].length - 1;
break;
_i = delimited;
for (type = 0, _j = _i.length; type < _j; type++) {
each = _i[type];
if (each[2] !== false && this.chunk.substring(i, i + each[2].length) === each[2]) {
i += each[2].length;
break;
} else if (levels.length && this.chunk.substring(i, i + each[1].length) === each[1] && levels[levels.length - 1] === type) {
levels.pop();
i += each[1].length - 1;
if (!(levels.length)) {
i += 1;
}
break;
} else if (this.chunk.substring(i, i + each[0].length) === each[0]) {
levels.push(type);
i += each[0].length - 1;
break;
}
}
if (!(levels.length)) {

View File

@ -201,24 +201,23 @@ exports.Lexer: class Lexer
# Matches a balanced group such as a single or double-quoted string.
balanced_group: (delimited...) ->
(each[1]: each[0]) for each in delimited when not each[1]?
escaped: '\\'
next: (length) => @chunk.substring i, i + length
(each[2]: '\\') for each in delimited when not each[2]?
levels: []
i: 0
while i < @chunk.length
if next(1) is escaped
i += 1
else
for each, type in delimited
if levels.length and next(each[1].length) is each[1] and levels[levels.length - 1] is type
levels.pop()
i += each[1].length - 1
i += 1 unless levels.length
break
else if next(each[0].length) is each[0]
levels.push(type)
i += each[0].length - 1
break
for each, type in delimited
if each[2] isnt false and @chunk.substring(i, i + each[2].length) is each[2]
i += each[2].length
break
else if levels.length and @chunk.substring(i, i + each[1].length) is each[1] and levels[levels.length - 1] is type
levels.pop()
i += each[1].length - 1
i += 1 unless levels.length
break
else if @chunk.substring(i, i + each[0].length) is each[0]
levels.push(type)
i += each[0].length - 1
break
break unless levels.length
i += 1
throw new Error "SyntaxError: Unterminated ${delimited[levels.pop()][0]} starting on line $@line" if levels.length