Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft mode #502

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Unreleased
### Breaking changes

### New features
- [#501](https://github.com/peggyjs/peggy/issues/501) Implement soft-mode for generated parsers with access to partial results on syntax errors.

### Bug fixes

Expand Down
3 changes: 2 additions & 1 deletion lib/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"commonjs": true
"commonjs": true,
"es6": true
},
"parserOptions": {
"project": ["lib/tsconfig.json"]
Expand Down
13 changes: 10 additions & 3 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,9 +1393,8 @@ function generateJS(ast, options) {
" peg$maxFailPos",
" });",
" }",
" if (peg$result !== peg$FAILED && peg$currPos === input.length) {",
" return peg$result;",
" } else {",
" var success = (peg$result !== peg$FAILED && peg$currPos === input.length);",
" function fail() {",
" if (peg$result !== peg$FAILED && peg$currPos < input.length) {",
" peg$fail(peg$endExpectation());",
" }",
Expand All @@ -1408,6 +1407,14 @@ function generateJS(ast, options) {
" : peg$computeLocation(peg$maxFailPos, peg$maxFailPos)",
" );",
" }",
" if (options.soft) {",
" return {result: peg$result, success, fail};",
" }",
" if (success) {",
" return peg$result;",
" } else {",
" fail();",
" }",
"}"
);

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/api/generated-parser-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ describe("generated parser API", () => {
expect(parser.parse("a")).to.equal("a");
});

it("parses input in soft-mode", () => {
const parser = peg.generate("start = 'a'");

const result = parser.parse("a", { soft: true });
expect(result.result).to.equal("a");
expect(result.success).to.equal(true);
});

it("throws an exception on syntax error", () => {
const parser = peg.generate("start = 'a'");

expect(() => { parser.parse("b"); }).to.throw();
});

it("gives partial result on syntax error in soft-mode", () => {
const parser = peg.generate("start = 'a'+");

const result = parser.parse("aab", { soft: true });
expect(result.result).to.deep.equal(["a", "a"]);
expect(result.success).to.equal(false);
expect(result.fail).to.throw('Expected "a" or end of input but "b" found.');
});

// Regression: https://github.com/peggyjs/peggy/pull/197
it("correctly describe character class in syntax error", () => {
const parser = peg.generate("start = [123-5]");
Expand Down
13 changes: 10 additions & 3 deletions test/cli/fixtures/imports_peggy.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,8 @@ function peg$parse(input, options) {
peg$maxFailPos
});
}
if (peg$result !== peg$FAILED && peg$currPos === input.length) {
return peg$result;
} else {
var success = (peg$result !== peg$FAILED && peg$currPos === input.length);
function fail() {
if (peg$result !== peg$FAILED && peg$currPos < input.length) {
peg$fail(peg$endExpectation());
}
Expand All @@ -516,6 +515,14 @@ function peg$parse(input, options) {
: peg$computeLocation(peg$maxFailPos, peg$maxFailPos)
);
}
if (options.soft) {
return {result: peg$result, success, fail};
}
if (success) {
return peg$result;
} else {
fail();
}
}

module.exports = {
Expand Down
13 changes: 10 additions & 3 deletions test/cli/fixtures/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,8 @@ function peg$parse(input, options) {
peg$maxFailPos
});
}
if (peg$result !== peg$FAILED && peg$currPos === input.length) {
return peg$result;
} else {
var success = (peg$result !== peg$FAILED && peg$currPos === input.length);
function fail() {
if (peg$result !== peg$FAILED && peg$currPos < input.length) {
peg$fail(peg$endExpectation());
}
Expand All @@ -486,6 +485,14 @@ function peg$parse(input, options) {
: peg$computeLocation(peg$maxFailPos, peg$maxFailPos)
);
}
if (options.soft) {
return {result: peg$result, success, fail};
}
if (success) {
return peg$result;
} else {
fail();
}
}

module.exports = {
Expand Down