Skip to content

Commit

Permalink
Fixes peggyjs#493. Allows null, undefined, or empty array as allowedS…
Browse files Browse the repository at this point in the history
…tartRules, all of which mean the default (first) rule
  • Loading branch information
hildjj committed Jun 19, 2024
1 parent fa8fe65 commit 9b7b3c3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ Unreleased
- [#509](https://github.com/peggyjs/peggy/pull/509) Add and implement ES6 export button

### Bug fixes
- [#493](https://github.com/peggyjs/peggy/issues/493) Allow use of an empty
array, null, or undefined as allowedStartRules option
- [#507](https://github.com/peggyjs/peggy/pull/507) Remove stray semicolon in CSS
- [#508](https://github.com/peggyjs/peggy/pull/508) Fix broken text input
- [#512](https://github.com/peggyjs/peggy/pull/512) Add "StartRules" to peg.d.ts.
- [#513](https://github.com/peggyjs/peggy/pull/513) Allow whitespace between
- [#512](https://github.com/peggyjs/peggy/issues/512) Add "StartRules" to peg.d.ts.
- [#513](https://github.com/peggyjs/peggy/issues/513) Allow whitespace between
plucked word and its pattern.
- [#520](https://github.com/peggyjs/peggy/pull/520) Grammar with token "constructor" fails to generate
- [#522](https://github.com/peggyjs/peggy/pull/522) Switched from puppeteer
- [#520](https://github.com/peggyjs/peggy/issues/520) Grammar with token "constructor" fails to generate
- [#522](https://github.com/peggyjs/peggy/issues/522) Switched from puppeteer
to playwright for web tests, and added them to CI.

### Documentation
Expand Down
2 changes: 1 addition & 1 deletion docs/js/benchmark-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/test-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/vendor/peggy/peggy.min.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ const compiler = {
compile(ast, passes, options) {
options = options !== undefined ? options : {};

const defaultStartRules = [ast.rules[0].name];
options = processOptions(options, {
allowedStartRules: [ast.rules[0].name],
allowedStartRules: defaultStartRules,
cache: false,
dependencies: {},
exportVar: null,
Expand All @@ -93,11 +94,16 @@ const compiler = {
trace: false,
});

if (options.allowedStartRules === null
|| options.allowedStartRules === undefined) {
options.allowedStartRules = defaultStartRules;
}

if (!Array.isArray(options.allowedStartRules)) {
throw new Error("allowedStartRules must be an array");
}
if (options.allowedStartRules.length === 0) {
throw new Error("Must have at least one start rule");
options.allowedStartRules = defaultStartRules;
}
const allRules = ast.rules.map(r => r.name);
// "*" means all rules are start rules. "*" is not a valid rule name.
Expand Down
14 changes: 10 additions & 4 deletions test/unit/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ describe("Peggy compiler", () => {
it("checks start rules", () => {
const ast = parser.parse("foo='1'");
expect(compiler.compile(ast, compiler.passes)).to.be.an("object");
expect(() => compiler.compile(ast, compiler.passes, {
expect(compiler.compile(ast, compiler.passes, {
allowedStartRules: null,
})).to.throw("allowedStartRules must be an array");
expect(() => compiler.compile(ast, compiler.passes, {
})).to.be.an("object");
expect(compiler.compile(ast, compiler.passes, {
allowedStartRules: undefined,
})).to.be.an("object");
expect(compiler.compile(ast, compiler.passes, {
allowedStartRules: [],
})).to.throw("Must have at least one start rule");
})).to.be.an("object");
expect(() => compiler.compile(ast, compiler.passes, {
allowedStartRules: {},
})).to.throw("allowedStartRules must be an array");
expect(() => compiler.compile(ast, compiler.passes, {
allowedStartRules: ["bar"],
})).to.throw('Unknown start rule "bar"');
Expand Down

0 comments on commit 9b7b3c3

Please sign in to comment.