peggy-tracks
TypeScript icon, indicating that this package has built-in type declarations

3.2.0 • Public • Published

peggy-tracks

peggy-tracks creates railroad track diagrams of peggy grammars.

Example

A quick example, generated with peggy-tracks -s comment:

comments

Alternately, you can have rule references expanded using -e:

comments-expanded

Installation

npm install -g peggy-tracks

Command line

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
  -d,--depth <number>      Maximum depth to expand references
  -e,--expand              Expand rule references without depth limit
  -o,--output <file name>  File in which to save output
  -q,--quote <string>      Quote character for literals (default: "\"")
  -s,--start <rule name>   Rule to start with
  -h, --help               display help for command

More control

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

peggy-tracks -c > my.css

Edit my.css, and pass it back in to peggy-tracks:

peggy-tracks -c my.css --action -o action.svg -s number test.peggy

Generates this:

comments-expanded

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

Only supports module-style:

import {tracks, defaultCSS} from "peggy-tracks";

const diagram = tracks({
  // "text" is the only required option
  text: `
foo = bar*
bar = n:[0-9] { return parseInt(n) }
`,
  start: "bar",
  action: true,
  expand: false,
  parserOptions: {
    grammarSource: "filename"
  },
});

console.log(diagram.toStandalone(`
/* Optional: Full CSS desired.  If none specified, uses defaultCSS */
svg {
  background-color: hsl(30,20%,95%);
}
...
`));

Tests codecov

Package Sidebar

Install

npm i peggy-tracks

Weekly Downloads

4

Version

3.2.0

License

MIT

Unpacked Size

78.2 kB

Total Files

11

Last publish

Collaborators

  • hildjj