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 ee8c121676 Use labeled expressions and variables instead of $1, $2, etc.
Labeled expressions lead to more maintainable code and also will allow
certain optimizations (we can ignore results of expressions not passed
to the actions).

This does not speed up the benchmark suite execution statistically
significantly on V8.

Detailed results (benchmark suite totals):

---------------------------------
 Test #     Before       After
---------------------------------
      1   28.43 kB/s   28.46 kB/s
      2   28.38 kB/s   28.56 kB/s
      3   28.22 kB/s   28.58 kB/s
      4   28.76 kB/s   28.55 kB/s
      5   28.57 kB/s   28.48 kB/s
---------------------------------
Average   28.47 kB/s   28.53 kB/s
---------------------------------

Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4
15 years ago
benchmark Removed trailing comma that caused error in IE. 15 years ago
bin Whitespace fixes 15 years ago
examples Use labeled expressions and variables instead of $1, $2, etc. 15 years ago
lib Use labeled expressions and variables instead of $1, $2, etc. 15 years ago
test Use labeled expressions and variables instead of $1, $2, etc. 15 years ago
vendor Added a benchmark suite. 15 years ago
.gitignore Replaced .hgignore with .gitignore. 15 years ago
CHANGELOG Added CHANGELOG. 15 years ago
LICENSE I don't like the .txt extension after all :-) 15 years ago
README.md README.md: Removed unnecessary "id" attribute from the example. 15 years ago
Rakefile Make the generated parsers standalone (no runtime is required). 15 years ago
VERSION Updated version to 0.4 15 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 is designed to be used either from your browser or from the command line (using Rhino JavaScript interpreter).

Features

  • Usable from your browser 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 (strict superset of LL(k) and LR(k))
  • Precise and human-friendly error reporting

Usage

To use PEG.js, you need to generate a parser from your grammar and then use the generated parser in your project.

Generating a Parser

To generate a parser, you can use the online generator or the command line. For the second option, you need to have Java installed. Than you can generate the parser using the bin/pegjs script on Unix or bin/pegjs.bat batch file on Windows:

$ bin/pegjs 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 generated parser will be available in the arithmeticsParser global variable. The generator has several useful options—to learn more about them, use --help.

Using the Generated Parser

To use the parser in a web page, follow the following example:

<!DOCTYPE html>
<head>
  <title>Arithmetics Parser Example</title>
  <script src="examples/arithmetics.js"></script> <!-- Include the parser. -->
  <script>
    function calculate(expression) {
      /* Use the parser to compute a value of an arithmetic expression. */
      var result;
      try {
        result = arithmeticsParser.parse(document.getElementById("expression").value);
      } catch (e) {
        result = e.message;
      }
      document.getElementById("result").innerText = result;
    }
  </script>
</head>
<body>
  <input type="text" id="expression" value="2*(3+4)">
  <input type="button" value="Calculate" onclick="calculate();">
  <div id="result"></div>
</body>

The parse method of the generated parser will return either the result of the parsing (dependent on the actions you specified in the grammar) or throw PEG.Parser.SyntaxError exception if the input contains a syntax error. The exception has properties message, line and column, which contain details about the error.

Grammar

For detailed description of the grammar see the online documentation.

Compatibility

Both the parser generator and generated parsers should run well in IE6+ and recent versions of Firefox, Chrome, Safari and Opera, as well as Rhino JavaScript engine.