You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
David Majda cc7ad9739f Add ability to start parsing from any grammar rule
Calling the parsing function could have been done without the ugly table
using |eval|, but this seemed to degrade performance significantly (by
about 3 %). This is probably because engines optimize badly in presence
of |eval|.

The method used in this patch does not change the benchmark suite
execution speed statistically significantly on V8.

Detailed results (benchmark suite totals):

---------------------------------
 Test #     Before       After
---------------------------------
      1   38.24 kB/s   38.28 kB/s
      2   38.35 kB/s   38.15 kB/s
      3   38.43 kB/s   38.40 kB/s
      4   38.53 kB/s   38.20 kB/s
      5   38.25 kB/s   38.39 kB/s
---------------------------------
Average   38.36 kB/s   38.39 kB/s
---------------------------------

Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.1
14 years ago
benchmark Use Unicode box-drawing characters for command-line benchmark table 14 years ago
bin bin/pegjs: Default parser variable name is "module.exports" 14 years ago
examples Make example grammars compatible with Rhino 15 years ago
src Add ability to start parsing from any grammar rule 14 years ago
test Add ability to start parsing from any grammar rule 14 years ago
.gitignore Split the source code into several files, introduce build system 14 years ago
CHANGELOG Update CHANGELOG 14 years ago
Jakefile Add "jake test" and "jake benchmark" 14 years ago
LICENSE Update copyright year in LICENSE 14 years ago
README.md Rakefile -> Jakefile 14 years ago
VERSION Bump version to 0.5+ 14 years ago
package.json Require Node.js 0.4 or higher 14 years ago

README.md

PEG.js: Parser Generator for JavaScript

http://pegjs.majda.cz/

PEG.js is a parser generator for JavaScript based on the parsing expression grammar formalism. It enables you to easily build fast parsers which process complex data or computer languages. You can use it as an underlying tool when writing various data processors, transformers, interpreters, or compilers.

Features

  • Usable from your browser, from JavaScript code, or from a command-line
  • Simple and expressive grammar syntax
  • No separate lexical analysis step — both lexical and syntactical analysis are handled by one tool
  • Handles wide class of grammars (superset of LL(k) and LR(k))
  • Precise and human-friendly error reporting

Building

To build PEG.js, you need to have Node installed together with the jake package. Then simply run the jake command:

$ jake

This command creates the PEG.js library in lib/peg.js by processing files in the src directory.

Usage

Using PEG.js is easy:

var parser = PEG.buildParser("start = ('a' / 'b')+");
parser.parse("abba"); // returns ["a", "b", "b", "a"]
parser.parse("abcd"); // throws an exception with details about the error

Basically, you need to generate a parser from your grammar and then use it to parse the input.

Generating a Parser

There are three ways how to generate the parser:

  1. Using the online generator
  2. Using the PEG.buildParser function from JavaScript code
  3. Using the command line

The online generator is easiest to use — you just enter your grammar and download the generated parser code. The parser object will be available in a global variable you specify (parser by default).

To generate the parser from JavaScript code, include the lib/compiler.js file and use the PEG.buildParser function. This function accepts a string with a grammar and either returns the built parser object or throws an exception if the grammar is invalid.

To generate the parser from a command line, you need to have Node installed. Then use the bin/pegjs script:

$ bin/pegjs --export-var arithmeticsParser examples/arithmetics.pegjs

This command will create the parser from the examples/arithmetics.pegjs file and put in into the examples/arithmetics.js file. The parser object will be available in the arithmeticsParser global variable. To learn more about the generator usage, use the --help option.

Using the Generated Parser

To use the generated parser, include the generated file (unless you built the parser straight from the JavaScript code using PEG.buildParser) and use the parse method on the parser object. This method accepts an input string and either returns the parse result (dependent on the actions you specified in the grammar) or throws PEG.grammarParser.SyntaxError exception if the input contains a syntax error. The exception has properties message, line and column, which contain details about the error.

The parser object also has the toSource method that returns its textual representation.

Grammar

For detailed description of the grammar see the online documentation.

Compatibility

Both the parser generator and generated parsers should run well in the following environments:

  • IE6+
  • Firefox
  • Chrome
  • Safari
  • Opera
  • Node