— more powerful than traditional LL(*k*) and LR(*k*) parsers
* Usable [from your browser](https://pegjs.org/online), from the command line,
or via JavaScript API
* Usable from your browser, from the command line, or via JavaScript API
Getting Started
---------------
Differences from the original PEG.js
--------
[Online version](https://pegjs.org/online) is the easiest way to generate a
parser. Just enter your grammar, try parsing few inputs, and download generated
parser code.
* The plugin API has been dropped for now, as it was underspecified and not very commonly used. A new, more robust and extensive plugin API may come to exist in the future, if it turns out that there is a high demand for customizations that wouldn't fit into the PEG-Redux project itself.
* Bower and stand-alone browser builds have been discontinued. Please use a bundler (see below) instead.
* AMD, UMD and globals support have been discontinued. The generated parsers now only support CommonJS.
* Module support. Both for importing other PEGRedux files, and for `require()`ing JS modules.
Installation
------------
### Node.js
To use the `pegjs` command, install PEG.js globally:
To use the `pegjs` command, install PEG-Redux globally:
```console
$ npm install -g pegjs
$ npm install -g peg-redux
```
To use the JavaScript API, install PEG.js locally:
To use the JavaScript API, install PEG-Redux locally:
```console
$ npm install pegjs
$ npm install peg-redux
```
If you need both the `pegjs` command and the JavaScript API, install PEG.js both
ways.
If you need both the `pegjs` command and the JavaScript API, install PEG-Redux
both ways.
### Browser
[Download](https://pegjs.org/#download) the PEG.js library (regular or minified
version) or install it using Bower:
PEG-Redux works with bundlers such as [Browserify](http://browserify.org/), [Parcel](https://parceljs.org/) and [Webpack](https://webpack.js.org/).
```console
$ bower install pegjs
```
Simply `require()` and use the module like you would in Node.js. The one exception is that modules (either PEG-Redux or Javascript modules) are not currently supported in browser environments.
Bower and standalone builds have been discontinued in this fork. Getting started with Browserify will only take a few minutes, and give you a better developer experience.
Generating a Parser
-------------------
PEG.js generates parser from a grammar that describes expected input and can
PEG-Redux generates parser from a grammar that describes expected input and can
specify what the parser returns (using semantic actions on matched parts of the
input). Generated parser itself is a JavaScript object with a simple API.
@ -100,26 +100,20 @@ You can tweak the generated parser with several options:
`peg.generate`
* `--extra-options-file` — file with additional options (in JSON format) to
pass to `peg.generate`
* `--format` — format of the generated parser: `amd`, `commonjs`, `globals`,
`umd` (default: `commonjs`)
* `--optimize` — selects between optimizing the generated parser for parsing
speed (`speed`) or code size (`size`) (default: `speed`)
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple
* `--plugin` — makes PEG-Redux use a specified plugin (can be specified multiple
times)
* `--trace` — makes the parser trace its progress
### JavaScript API
In Node.js, require the PEG.js parser generator module:
Require the PEG-Redux parser generator module:
```javascript
var peg = require("pegjs");
var peg = require("peg-redux");
```
In browser, include the PEG.js library in your web page or application using the
`<script>` tag. If PEG.js detects an AMD loader, it will define itself as a
module, otherwise the API will be available in the `peg` global object.
To generate a parser, call the `peg.generate` method and pass your grammar as a
parameter:
@ -142,14 +136,7 @@ object to `peg.generate`. The following options are supported:
`false`)
* `dependencies` — parser dependencies, the value is an object which maps
variables used to access the dependencies in the parser to module IDs used
to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, or
`"umd"` (default: `{}`)
* `exportVar` — name of a global variable into which the parser object is
assigned to when no module loader is detected; valid only when `format` is
set to `"globals"` or `"umd"` (default: `null`)
* `format` — format of the genreated parser (`"amd"`, `"bare"`, `"commonjs"`,
`"globals"`, or `"umd"`); valid only when `output` is set to `"source"`
(default: `"bare"`)
to load them.
* `optimize`— selects between optimizing the generated parser for parsing
speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
* `output` — if set to `"parser"`, the method will return generated parser
@ -503,25 +490,14 @@ environments:
* Safari
* Opera
However, please note that it is currently only actively tested in Node.js and Firefox. This will likely change in the future.