PEG.js ====== PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily. Features -------- * Simple and expressive grammar syntax * Integrates both lexical and syntactical analysis * Parsers have excellent error reporting out of the box * Based on [parsing expression grammar](http://en.wikipedia.org/wiki/Parsing_expression_grammar) formalism — more powerful than traditional LL(*k*) and LR(*k*) parsers * Usable [from your browser](http://pegjs.org/online), from the command line, or via JavaScript API Getting Started --------------- [Online version](http://pegjs.org/online) is the easiest way to generate a parser. Just enter your grammar, try parsing few inputs, and download generated parser code. Installation ------------ ### Node.js To use the `pegjs` command, install PEG.js globally: $ npm install -g pegjs To use the JavaScript API, install PEG.js locally: $ npm install pegjs If you need both the `pegjs` command and the JavaScript API, install PEG.js both ways. ### Browser [Download](http://pegjs.org/#download) the PEG.js library (regular or minified version) or install it using Bower: $ bower install pegjs Generating a Parser ------------------- PEG.js 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. ### Command Line To generate a parser from your grammar, use the `pegjs` command: $ pegjs arithmetics.pegjs This writes parser source code into a file with the same name as the grammar file but with “.js” extension. You can also specify the output file explicitly: $ pegjs arithmetics.pegjs arithmetics-parser.js If you omit both input and output file, standard input and output are used. By default, the parser object is assigned to `module.exports`, which makes the output a Node.js module. You can assign it to another variable by passing a variable name using the `-e`/`--export-var` option. This may be helpful if you want to use the parser in browser environment. You can tweak the generated parser with several options: * `--cache` — makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower * `--allowed-start-rules` — comma-separated list of rules the parser will be allowed to start parsing from (default: the first rule in the grammar) * `--plugin` — makes PEG.js use a specified plugin (can be specified multiple times) * `--extra-options` — additional options (in JSON format) to pass to `PEG.buildParser` * `--extra-options-file` — file with additional options (in JSON format) to pass to `PEG.buildParser` * `--trace` — makes the parser trace its progress ### JavaScript API In Node.js, require the PEG.js parser generator module: var PEG = require("pegjs"); In browser, include the PEG.js library in your web page or application using the `