PEG.js: Parser generator for JavaScript
Parsers can be generated in multiple module formats. The available formats are: CommonJS (the default), AMD, UMD, globals, and bare (not available from the command-line).
The format can be specified using the format
option of the peg.generate
function or the --format
option on the command-line.
It is also possible to specify parser dependencies using the dependencies
option of the peg.generate
function or the --dependency
/-d
option on
the command-line. This is mainly useful for the UMD format, where the
dependencies are translated into both AMD dependencies and CommonJS
require
calls.
Browser version of PEG.js is now in the UMD format. This means it will try to detect AMD or Node.js/CommonJS module loader and define itself as a module. If no loader is found, it will export itself using a global variable.
API polishing. The peg.buildParser
function was renamed to
peg.generate
. The global variable the browser version of PEG.js is
available in when no loader is detected was renamed from PEG
to peg
.
CLI improvements. There is new --output
/-o
command-line option which
allows to specify the output file. The old way of specifying the output file
using a second argument was removed. To make room for the new -o
option
the old one (a shortcut for --optimize
) was renamed to -O
. All these
changes make PEG.js conform to traditional compiler command-line interface.
It is now also possible to use -
as a file name on the command-line (with
the usual meaning of standard input/output).
Improved error messages. Both messages produced by PEG.js and generated parsers were improved.
Duplicate rule definitions are reported as errors.
Duplicate labels are reported as errors.
peg.compiler.visitor
. This is
useful mainly for plugins which manipulate the AST.SyntaxError.buildMessage
. This is useful mainly for
customizing these error messages.error
and expected
functions now accept an optional location
parameter. This allows to customize the location in which the resulting
syntax error is reported.expected
property of exceptions
produced by generated parsers. They are no longer de-duplicated and sorted,
their format changed to be more machine-readable, and they no longer contain
human-readable descriptions.found
property of exceptions produced by the error
function is now
set to null
.parser
variable.undefined
as a parameter value is now equivalent to not passing
the parameter at all.\r
, \u2028
, and \u2029
as
newlines (only \n
and \r\n
).first
/rest
to head
/tail
in PEG.js grammar and example
grammars.bin/pegjs
so that invoking it with one non-option argument which is
an extension-less file doesn’t cause that file to be overwritten.(a:"a")
or (a:"a" b:"b" c:"c")
aren’t visible from the outside.tools/impact
on OS X.line
, column
, and offset
functions available in parser code were replaced by a single location
function which returns an object describing the current location. Similarly,
the line
, column
, and offset
properties of exceptions were replaced by
a single location
property. The location is no longer a single point but a
character range, which is meaningful mainly in actions where the range
covers action’s expression.
More details
stack
property) in
environments that support Error.captureStackTrace
.;
or a newline (until now, any
whitespace was enough).parser
in parser code.parse
and SyntaxError
to peg$parse
and peg$SyntaxError
to
mark them as internal identifiers.optimize
option of the PEG.buildParser
method or the --optimize
/-o
option on the command-line. All internal identifiers in generated code now also have a peg$
prefix to discourage their use and avoid conflicts. [#35, #92]null
inside actions to indicate match failure, new expected
and error
functions can be called to trigger an error. Also, expectation inside the SyntaxError
exceptions are now structured to allow easier machine processing. [#198]plugins
option of the PEG.buildParser
method or the --plugin
option on the command-line. Also implemented the --extra-options
and --extra-options-file
command-line options, which are mainly useful to pass additional options to plugins. [#106]offset
, line
and column
functions, not variables. They are now available in all parsers and return lazily-computed position data. Removed now useless trackLineAndColumn
option of the PEG.buildParser
method and the --track-line-and-column
option on the command-line.text
function. When called inside an action, it returns the text matched by action's expression. [#131]$
operator. It extracts matched strings from expressions.?
operator now returns null
on unsuccessful match.undefined
.startRule
parameter of the parse
method in generated parsers with more generic options
parameter. The start rule can now be specified as the startRule
option. The options
parameter can be also used to pass custom options to the parser because it is visible as the options
variable inside parser code. [#37]allowedStartRules
option of the PEG.buildParser
method or the --allowed-start-rule
option on the command-line. This will make certain optimizations like rule inlining easier in the future.toSource
method of generated parsers and introduced a new output
option of the PEG.buildParser
method. It allows callers to specify whether they want to get back the parser object or its source code.bin/pegjs
now outputs just the parser source if the value of the --export-var
option is empty. This makes embedding generated parsers into other files easier. [#143]name
property of PEG.GrammarError
instances from “PEG.GrammarError” to just “GrammarError”. This better reflects the fact that PEG.js can get required with different variable name than PEG
.PEG.GrammarError
correctly.SyntaxError
in generated parsers correctly.\^
. [#125]README.md
.CHANGELOG
to Markdown.