Skip to content

Commit

Permalink
Report consistent errors on look ahead + cached results
Browse files Browse the repository at this point in the history
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
futagoza committed Jan 18, 2018
1 parent 9b90fa1 commit f5b323b
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 39 deletions.
29 changes: 23 additions & 6 deletions lib/compiler/passes/generate-js.js
Expand Up @@ -143,7 +143,11 @@ function generateJS( ast, options ) {

const parts = [];

parts.push( "" );
parts.push( [
"",
"var rule$expects = peg$expect;",
""
].join( "\n" ) );

if ( options.trace ) {

Expand All @@ -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.

Copy link
@Mingun

Mingun Jan 18, 2018

Contributor

I think that this need comment about why map used here instead of forEach (tip: because forEach may be not exist in target js version)

This comment has been minimized.

Copy link
@nikku

nikku Jan 18, 2018

@futagoza changed it to forEach in a later commit. Both are equally well supported starting from IE 9.

" }",
""
].join( "\n" ) );

Expand Down Expand Up @@ -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" ) );

}
Expand Down Expand Up @@ -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;",
"",
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit f5b323b

Please sign in to comment.