Peggy Documentation
repository·main·Indexed 22 days ago
https://github.com/peggyjs/peggyPeggy is a parser generator for JavaScript based on Parsing Expression Grammars (PEG), designed to create fast, reliable parsers with high-quality error reporting. The successor to PEG.js, it is available via a JavaScript API, command line interface, and online editor. It supports features such as TypeScript definition generation, plugin extensions, source maps, and AST output.
What's inside Peggy
- Peggy is a parser generator for JavaScript that produces fast parsers with high-quality error reporting. It is based on the Parsing Expression Grammar (PEG) formalism, making it more powerful than traditional LL(k) and LR(k) parsers. Peggy can be used to build transformers, interpreters, compilers, and other tools by processing complex data or computer languages. It is available via a JavaScript API, the command line, or an online editor.
Getting Started with Peggy
mainThe fastest way to generate a parser is to use the Online version. You can enter your grammar, test it against various inputs, and then download the generated parser code directly.Run the Peggy benchmark suite in the Browser
mainTo run the Peggy benchmark suite in a web browser, follow these steps from the Peggy root directory:
- Ensure Node.js is installed.
- Install all dependencies (including development dependencies):
npm install - Start the benchmark suite web server:
benchmark/server - Open your browser and navigate to
http://localhost:8000/. - Click the Run button and wait for the results.
$ npm install $ benchmark/serverMigrating from PEG.js to Peggy
mainPeggy version 1.x.x is API compatible with the most recent PEG.js release. To upgrade, follow these steps:
- Uninstall
pegjsand@types/pegjs(Peggy includes its own type definitions). - Replace all
require("pegjs")orimport ... from "pegjs"withrequire("peggy")orimport ... from "peggy". - Update any scripts using the
pegjsCLI to usepeggyinstead.
- Uninstall
Build the Peggy documentation site
mainThe Peggy documentation site is built using the Eleventy static site generator. To generate a production build of the documentation, use the
buildscript.npm run buildRun the Peggy benchmark suite in Node.js
mainTo measure the speed of parsers generated by Peggy on various inputs using Node.js, follow these steps from the Peggy root directory:
- Install all dependencies (including development dependencies):
npm install - Execute the benchmark suite:
npm run benchmark - Wait for the results to be generated.
$ npm install $ npm run benchmark- Install all dependencies (including development dependencies):
Run the Peggy documentation development server
mainFor local development, you can run a development server that watches for file changes and automatically rebuilds the documentation site. Use the
startscript to begin development.npm run startUse plugins in Peggy
mainPeggy supports plugins to extend its functionality. You can provide plugins via the
--pluginflag. Plugins can be specified as:- An NPM module name.
- An absolute or relative file path.
Plugins must export a
use()function (or be thedefaultexport containing ause()function).# Example: Using an NPM plugin peggy --plugin my-peggy-plugin grammar.peggy # Example: Using a local plugin file peggy --plugin ./plugins/my-local-plugin.js grammar.peggyUse Peggy plugins
mainYou can extend the Peggy compilation process by providing plugins via the
options.pluginsarray in thegeneratemethod.Each plugin is expected to have a
.use(config, options)method. Theconfigobject passed to the plugin contains:parser: The Peggy parser.passes: A copy of the compiler passes.reservedWords: A copy of the reserved words list.
The
optionsobject (the same one passed togenerate) is also passed to the plugin's.usemethod.const peg = require("peggy"); const myPlugin = { use: (config, options) => { // Implement plugin logic using config and options } }; const parser = peg.generate(grammar, { plugins: [myPlugin] });Configure Peggy via extra options files
mainPeggy allows you to provide additional configuration options through external files. This is useful for complex setups that are cumbersome to pass via CLI flags.
Supported file types:
- JavaScript files (
.js,.mjs,.cjs): The file should export a default object containing the options. - JSON files: A standard JSON object containing the options.
Precedence Rules:
- Options provided directly on the command line take precedence over those in an extra options file.
- For multi-value options (like
plugin), the values in the extra options file are additive (appended to the CLI values). - For object-type options, the values from the file are merged into the existing object.
# Example: Using a JavaScript config file # config.mjs export default { format: 'es', output: 'parser.js' }; # Command line peggy --extra-options-file config.mjs grammar.peggy- JavaScript files (
Use the Peggy CLI to generate parsers
mainThe Peggy CLI allows you to compile grammar files into JavaScript parsers. You can provide one or multiple grammar files, which will be combined in the order provided. You can also read grammars from
stdinusing-or import them directly from npm packages using thenpm:<packageName>/file.peggysyntax.Basic Usage:
# Generate a parser from a file (outputs to <input_file>.js) peggy grammar.peggy # Generate a parser and specify output file peggy grammar.peggy -o parser.js # Read grammar from stdin cat grammar.peggy | peggy - -o parser.js # Import grammar from an npm dependency peggy npm:my-package/grammar.peggy -o parser.jspeggy grammar.peggyWatch mode for grammar development
mainUse the
-wor--watchflag to monitor your grammar files. Peggy will generate the output immediately and then re-generate it whenever the input file (or the test file, if provided) changes.peggy grammar.peggy -o parser.js --watchpeggy grammar.peggy -o parser.js --watch