Skip to content

Commit

Permalink
Add support for --depth. Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Feb 27, 2024
1 parent 5a0e9cc commit 19cd5ae
Show file tree
Hide file tree
Showing 12 changed files with 6,512 additions and 21 deletions.
13 changes: 13 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.js text eol=lf
*.ts text eol=lf
*.json text eol=lf
*.html text eol=lf
*.css text eol=lf
*.map text eol=lf
*.peggy text eol=lf
*.pegjs text eol=lf
*.svg text eol=lf
*.png binary
*.jpg binary
*.ico binary
*.snap binary
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Options:
-a,--action Wrap actions in a box
-c,--css [file name] With no file name, outputs the default CSS. With a
filename, substitutes the CSS in that file.
-d,--depth <number> Maximum depth to expand references (set to Infinity
with --expand).
-e,--expand Expand rule references
-o,--output <file name> File in which to save output
-s,--start <rule name> Rule to start with
Expand All @@ -35,7 +37,8 @@ Options:

## More control

To change the look and feel of the output, you can output the default CSS that will be placed in the SVG:
To change the look and feel of the output, you can output the default CSS that
will be placed in the SVG:

```sh
peggy-tracks -c > my.css
Expand All @@ -51,7 +54,9 @@ Generates this:

![comments-expanded](./test/output/action.svg)

Note that the `--action` flag has created extra boxes around parts of the grammar, that when moused over show the code that will be executed when the fragment matches.
Note that the `--action` flag has created extra boxes around parts of the
grammar, that when moused over show the code that will be executed when the
fragment matches.

## API

Expand Down
14 changes: 13 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command, Option } from "commander";
import { createReadStream, createWriteStream } from "fs";
import { defaultCSS, tracks } from "./index.js";
import { format, join, parse } from "path";
import { readFile, stat } from "fs/promises";
import { Command } from "commander";

function readStream(stream) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -63,6 +63,7 @@ export default class CliCommand extends Command {
.argument("[input_file]")
.option("-a,--action", "Wrap actions in a box")
.option("-c,--css [file name]", "With no file name, outputs the default CSS. With a filename, substitutes the CSS in that file.")
.addOption(new Option("-d,--depth <number>", "Maximum depth to expand references (set to Infinity with --expand).").conflicts("expand"))
.option("-e,--expand", "Expand rule references")
.option("-o,--output <file name>", "File in which to save output")
.option("-s,--start <rule name>", "Rule to start with");
Expand Down Expand Up @@ -93,6 +94,17 @@ export default class CliCommand extends Command {
delete opts.css;
}

let depth = 0;
if (opts.expand === true) {
depth = Infinity;
} else if (typeof opts.depth === "string") {
depth = parseInt(opts.depth, 10);
if (isNaN(depth) || depth < 0) {
this.error(`"--depth" must be positive integer, got "${opts.depth}"`);
}
}
opts.depth = depth;

const files = this.args.length ? this.args : ["-"];
for (const grammarSource of files) {
const stream = (grammarSource === "-")
Expand Down
16 changes: 10 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import peggy from "peggy";
* @property {string} [start] The rule name to start with.
* Defaults to first rule in the grammar.
* @property {boolean} [action=false] Create action blocks.
* @property {boolean} [expand=false] Expand rule references.
* @property {object} parserOptions Peggy parser options. Most important
* is "grammarSource".
*/
Expand Down Expand Up @@ -135,9 +134,15 @@ const visit = peggy.compiler.visitor.build({
});
},
rule_ref(node, context) {
return context.opts.expand
? visit(asts.findRule(context.grammar, node.name), context)
: new NonTerminal(node.name, { cls: "rule-ref" });
let res = null;
if (context.opts.depth > 0) {
context.opts.depth--;
res = visit(asts.findRule(context.grammar, node.name), context);
context.opts.depth++;
} else {
res = new NonTerminal(node.name, { cls: "rule-ref" });
}
return res;
},
library_ref(node) {
return new NonTerminal(`${node.library}.${node.name}`, { cls: "library_ref" });
Expand Down Expand Up @@ -166,8 +171,7 @@ const visit = peggy.compiler.visitor.build({
* Generate a railroad track diagram from a grammer
*
* @param {TrackOptions} opts
* @return {Diagram|ComplexDiagram
}
* @return {Diagram|ComplexDiagram}
*/
export function tracks(opts) {
const context = {
Expand Down
23 changes: 11 additions & 12 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ test("cli help", async t => {
defaultOutputStream,
});

t.is(defaultOutputStream.read(), `\
Usage: peggy-tracks [options] [input_file]
Options:
-a,--action Wrap actions in a box
-c,--css [file name] With no file name, outputs the default CSS. With a
filename, substitutes the CSS in that file.
-e,--expand Expand rule references
-o,--output <file name> File in which to save output
-s,--start <rule name> Rule to start with
-h, --help display help for command
`);
t.snapshot(defaultOutputStream.read());

const errorStream = new Buf();
await exec(t, ["-s"], "commander.optionMissingArgument", {
Expand Down Expand Up @@ -178,3 +167,13 @@ test("more examples", async t => {
await exec(t, ["-e", "-o", fullURL, inp]);
t.snapshot(await readFile(fullURL, "utf8"));
});

test("depth", async t => {
const inp = url.fileURLToPath(new URL("test.peggy", import.meta.url));

for (let i = 0; i < 5; i++) {
const fullURL = url.fileURLToPath(new URL(`output/depth${i}.svg`, import.meta.url));
await exec(t, ["-d", String(i), "-o", fullURL, inp]);
t.snapshot(await readFile(fullURL, "utf8"));
}
});
116 changes: 116 additions & 0 deletions test/output/depth0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 19cd5ae

Please sign in to comment.