— more powerful than traditional LL(*k*) and LR(*k*) parsers
— more powerful than traditional LL(*k*) and LR(*k*) parsers
* Usable [from your browser](https://pegjs.org/online), from the command line,
* Usable from your browser, from the command line, or via JavaScript API
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
* 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.
parser. Just enter your grammar, try parsing few inputs, and download generated
* Bower and stand-alone browser builds have been discontinued. Please use a bundler (see below) instead.
parser code.
* 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
Installation
------------
------------
### Node.js
### Node.js
To use the `pegjs` command, install PEG.js globally:
To use the `pegjs` command, install PEG-Redux globally:
```console
```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
```console
$ npm install pegjs
$ npm install peg-redux
```
```
If you need both the `pegjs` command and the JavaScript API, install PEG.js both
If you need both the `pegjs` command and the JavaScript API, install PEG-Redux
ways.
both ways.
### Browser
### Browser
[Download](https://pegjs.org/#download) the PEG.js library (regular or minified
PEG-Redux works with bundlers such as [Browserify](http://browserify.org/), [Parcel](https://parceljs.org/) and [Webpack](https://webpack.js.org/).
version) or install it using Bower:
```console
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 install pegjs
```
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
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
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.
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`
`peg.generate`
* `--extra-options-file` — file with additional options (in JSON format) to
* `--extra-options-file` — file with additional options (in JSON format) to
pass to `peg.generate`
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
* `--optimize` — selects between optimizing the generated parser for parsing
speed (`speed`) or code size (`size`) (default: `speed`)
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)
times)
* `--trace` — makes the parser trace its progress
* `--trace` — makes the parser trace its progress
### JavaScript API
### JavaScript API
In Node.js, require the PEG.js parser generator module:
Require the PEG-Redux parser generator module:
```javascript
```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
To generate a parser, call the `peg.generate` method and pass your grammar as a
parameter:
parameter:
@ -142,14 +136,7 @@ object to `peg.generate`. The following options are supported:
`false`)
`false`)
* `dependencies` — parser dependencies, the value is an object which maps
* `dependencies` — parser dependencies, the value is an object which maps
variables used to access the dependencies in the parser to module IDs used
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
to load them.
`"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"`)
* `optimize`— selects between optimizing the generated parser for parsing
* `optimize`— selects between optimizing the generated parser for parsing
speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
* `output` — if set to `"parser"`, the method will return generated parser
* `output` — if set to `"parser"`, the method will return generated parser
@ -503,25 +490,14 @@ environments:
* Safari
* Safari
* Opera
* Opera
However, please note that it is currently only actively tested in Node.js and Firefox. This will likely change in the future.
Development
Development
-----------
-----------
* [Project website](https://pegjs.org/)
PEG-Redux is maintained by [Sven Slootweg (joepie91)](http://cryto.net/~joepie91).
* [Wiki](https://github.com/pegjs/pegjs/wiki)
The original PEG.js was developed by [David Majda](http://majda.cz/) ([@dmajda](http://twitter.com/dmajda)).