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.majda.cz/online), from the command line, or via JavaScript API Getting Started --------------- [Online version](http://pegjs.majda.cz/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.majda.cz/#download) the PEG.js library (regular or minified version). 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 ouptut 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 two options: * `--cache` — makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower * `--track-line-and-column` — makes the parser track line and column (available as `line` and `column` variables in the actions and predicates) ### 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 `