Skip to content

Commit

Permalink
Merge pull request #22 from hildjj/multiple-starts
Browse files Browse the repository at this point in the history
Allow for multiple start rules
  • Loading branch information
hildjj authored Nov 9, 2024
2 parents 5336c90 + 63884a7 commit 5137f5d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
8 changes: 7 additions & 1 deletion bin/abnf_gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function genFormat(rules, opts) {
}
}

function append(v, p = []) {
p.push(v);
return p;
}

const program = new Command();
program
.argument("[abnfFile...]", "ABNF files to turn into grammars.")
Expand All @@ -31,7 +36,8 @@ program
)
.option(
"-s, --startRule <ruleName>",
"Start rule for generated grammar. Defaults to first rule in ABNF grammar."
"Start rule for generated grammar. Defaults to first rule in ABNF grammar. Can be specified multiple times.",
append
)
.option("--stubs", "Generate stubs for rules that do not exist, rather than failing.")
.option(
Expand Down
9 changes: 6 additions & 3 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @typedef {object} FormatOptions
* @prop {string} [format='peggy']
* @prop {string} [startRule] Defaults to first
* @prop {string|string[]} [startRule] Defaults to first
* @prop {boolean} [stubs=false]
* @prop {boolean} [unused=false]
*/
Expand Down Expand Up @@ -369,10 +369,13 @@ export class Rules extends Base {
if (!["peggy", "pest"].includes(opts.format)) {
badFormat(opts);
}
if (!opts.startRule) {
if (opts.startRule === null) {
return "";
}
const needed = [opts.startRule];
if (typeof opts.startRule === "string") {
opts.startRule = [opts.startRule];
}
const needed = [...opts.startRule];
const done = new Set();
let res = "";
while (needed.length > 0) {
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
"typescript": "^5.6.3"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"braces": "^3.0.3",
"micromatch": "^4.0.8"
}
},
"engines": {
"node": ">=18"
}
Expand Down
31 changes: 5 additions & 26 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion test/parse.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ binarySep = %b1100001 %b1100010 %b1100011

test("parser edges", t => {
t.throws(() => parse("", {
startRule: "___INVALID____",
startRule: ["___INVALID____"],
}));

parse("foo = %x20\n", {
Expand Down

0 comments on commit 5137f5d

Please sign in to comment.