diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index ed40b02..bb73189 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -463,6 +463,65 @@ describe("generated parser behavior", function() { expect(parser).toFailToParse("a"); }); + + it("cannot access variables defined by subexpressions", function() { + var testcases = [ + // TODO: Fix #396. + // + // { + // grammar: 'start = (a:"a") &{ return a === "a"; }', + // input: "a" + // }, + { + grammar: 'start = (a:"a")? &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = (a:"a")* &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = (a:"a")+ &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = $(a:"a") &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = &(a:"a") "a" &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = !(a:"a") "b" &{ return a === "a"; }', + input: "b" + }, + { + grammar: 'start = b:(a:"a") &{ return a === "a"; }', + input: "a" + }, + // TODO: Fix #396. + // + // { + // grammar: 'start = ("a" b:"b" "c") &{ return b === "b"; }', + // input: "abc" + // }, + { + grammar: 'start = (a:"a" { return a; }) &{ return a === "a"; }', + input: "a" + }, + { + grammar: 'start = ("a" / b:"b" / "c") &{ return b === "b"; }', + input: "b" + } + ], + parser, i; + + for (i = 0; i < testcases.length; i++) { + parser = PEG.buildParser(testcases[i].grammar, options); + expect(parser).toFailToParse(testcases[i].input); + } + }); }); describe("in outer sequence", function() { @@ -608,6 +667,65 @@ describe("generated parser behavior", function() { expect(parser).toFailToParse("a"); }); + + it("cannot access variables defined by subexpressions", function() { + var testcases = [ + // TODO: Fix #396. + // + // { + // grammar: 'start = (a:"a") !{ return a !== "a"; }', + // input: "a" + // }, + { + grammar: 'start = (a:"a")? !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = (a:"a")* !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = (a:"a")+ !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = $(a:"a") !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = &(a:"a") "a" !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = !(a:"a") "b" !{ return a !== "a"; }', + input: "b" + }, + { + grammar: 'start = b:(a:"a") !{ return a !== "a"; }', + input: "a" + }, + // TODO: Fix #396. + // + // { + // grammar: 'start = ("a" b:"b" "c") !{ return b !== "b"; }', + // input: "abc" + // }, + { + grammar: 'start = (a:"a" { return a; }) !{ return a !== "a"; }', + input: "a" + }, + { + grammar: 'start = ("a" / b:"b" / "c") !{ return b !== "b"; }', + input: "b" + } + ], + parser, i; + + for (i = 0; i < testcases.length; i++) { + parser = PEG.buildParser(testcases[i].grammar, options); + expect(parser).toFailToParse(testcases[i].input); + } + }); }); describe("in outer sequence", function() { @@ -931,6 +1049,65 @@ describe("generated parser behavior", function() { expect(parser).toParse("abc", ["a", "b", "c"]); }); + + it("cannot access variables defined by subexpressions", function() { + var testcases = [ + // TODO: Fix #396. + // + // { + // grammar: 'start = (a:"a") { return a; }', + // input: "a" + // }, + { + grammar: 'start = (a:"a")? { return a; }', + input: "a" + }, + { + grammar: 'start = (a:"a")* { return a; }', + input: "a" + }, + { + grammar: 'start = (a:"a")+ { return a; }', + input: "a" + }, + { + grammar: 'start = $(a:"a") { return a; }', + input: "a" + }, + { + grammar: 'start = &(a:"a") "a" { return a; }', + input: "a" + }, + { + grammar: 'start = !(a:"a") "b" { return a; }', + input: "b" + }, + { + grammar: 'start = b:(a:"a") { return a; }', + input: "a" + }, + // TODO: Fix #396. + // + // { + // grammar: 'start = ("a" b:"b" "c") { return b; }', + // input: "abc" + // }, + { + grammar: 'start = (a:"a" { return a; }) { return a; }', + input: "a" + }, + { + grammar: 'start = ("a" / b:"b" / "c") { return b; }', + input: "b" + } + ], + parser, i; + + for (i = 0; i < testcases.length; i++) { + parser = PEG.buildParser(testcases[i].grammar, options); + expect(parser).toFailToParse(testcases[i].input); + } + }); }); describe("in outer sequence", function() {