The cache does not remember match length but next position after the match.

This leads to simpler and faster code because it avoids one addition and
subtraction.
redux
David Majda 14 years ago
parent 8f2aa1e834
commit 6f510a0336

@ -267,7 +267,7 @@ PEG.Grammar.Rule.prototype.compile = function() {
" this._cache[${name|string}] = this._cache[${name|string}] || [];",
" var cachedResult = this._cache[${name|string}][this._pos];",
" if (cachedResult !== undefined) {",
" this._pos += cachedResult.length;",
" this._pos = cachedResult.nextPos;",
" return cachedResult.result;",
" }",
" ",
@ -279,8 +279,8 @@ PEG.Grammar.Rule.prototype.compile = function() {
" ${reportMatchFailureCode}",
" ",
" this._cache[${name|string}][pos] = {",
" length: this._pos - pos,",
" result: ${resultVar}",
" nextPos: this._pos,",
" result: ${resultVar}",
" };",
" return ${resultVar};",
"};",

Loading…
Cancel
Save