Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report consistent errors on look ahead + cached results
This should resolve issue #452, and is based entirely on a fix @nikku did on a local branch of PEG.js v0.10.0 (Currently at https://github.com/nikku/pegjs/tree/452-peg-js-0.10-fix). Also his test case from #555 is included. Fixes #452, Closes #555
- Loading branch information
Showing
3 changed files
with
235 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,7 +143,11 @@ function generateJS( ast, options ) { | |
|
||
const parts = []; | ||
|
||
parts.push( "" ); | ||
parts.push( [ | ||
"", | ||
"var rule$expects = peg$expect;", | ||
"" | ||
].join( "\n" ) ); | ||
|
||
if ( options.trace ) { | ||
|
||
|
@@ -163,9 +167,20 @@ function generateJS( ast, options ) { | |
parts.push( [ | ||
"var key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";", | ||
"var cached = peg$resultsCache[key];", | ||
"var rule$expectations = [];", | ||
"", | ||
"rule$expects = function (expected) {", | ||
" if (peg$silentFails === 0) peg$expect(expected);", | ||
" rule$expectations.push(expected);", | ||
"}", | ||
"", | ||
"if (cached) {", | ||
" peg$currPos = cached.nextPos;", | ||
"", | ||
" rule$expectations = cached.expectations;", | ||
" if (peg$silentFails === 0) {", | ||
" rule$expectations.map(peg$expect);", | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nikku
|
||
" }", | ||
"" | ||
].join( "\n" ) ); | ||
|
||
|
@@ -211,7 +226,11 @@ function generateJS( ast, options ) { | |
|
||
parts.push( [ | ||
"", | ||
"peg$resultsCache[key] = { nextPos: peg$currPos, result: " + resultCode + " };" | ||
"peg$resultsCache[key] = {", | ||
" nextPos: peg$currPos,", | ||
" result: " + resultCode + ",", | ||
" expectations: rule$expectations", | ||
"};" | ||
].join( "\n" ) ); | ||
|
||
} | ||
|
@@ -479,9 +498,7 @@ function generateJS( ast, options ) { | |
" break;", | ||
"", | ||
" case " + op.EXPECT + ":", // EXPECT e | ||
" if (peg$silentFails === 0) {", | ||
" peg$expect(peg$expectations[bc[ip + 1]]);", | ||
" }", | ||
" rule$expects(peg$expectations[bc[ip + 1]]);", | ||
" ip += 2;", | ||
" break;", | ||
"", | ||
|
@@ -840,7 +857,7 @@ function generateJS( ast, options ) { | |
break; | ||
|
||
case op.EXPECT: // EXPECT e | ||
parts.push( "if (peg$silentFails === 0) { peg$expect(" + e( bc[ ip + 1 ] ) + "); }" ); | ||
parts.push( "rule$expects(" + e( bc[ ip + 1 ] ) + ");" ); | ||
ip += 2; | ||
break; | ||
|
||
|
Oops, something went wrong.
I think that this need comment about why
map
used here instead offorEach
(tip: becauseforEach
may be not exist in target js version)