Plugin API: Implement the |plugins| option for |PEG.buildParser|

The |plugins| option allows users to use plugins that change how PEG.js
operates.

A plugin is any JavaScript object with a |use| method. After the user
calls |PEG.buildParser|, this method is called for each plugin with the
following two parameters:

  * PEG.js config that describes used grammar parser and compiler
    passes used to generate the parser

  * options passed by user to |PEG.buildParser|

The plugin is expected to change the config as needed, possibly based on
the options passed by user. It can e.g. change the used grammar parser,
change the compiler passes (including adding its own), etc. This way it
can extend PEG.js in a flexible way.

Implements part of GH-106.
redux
David Majda 11 years ago
parent d02098eebe
commit 76f5c88073

@ -110,6 +110,7 @@ object to `PEG.buildParser`. The following options are supported:
(default: `"parser"`)
* `optimize`— selects between optimizing the generated parser for parsing
speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
* `plugins` — plugins to use
Using the Parser
----------------

@ -19,10 +19,19 @@ module.exports = {
* errors are detected during the generation and some may protrude to the
* generated parser and cause its malfunction.
*/
buildParser: function(grammar, options) {
buildParser: function(grammar) {
var options = arguments.length > 1 ? utils.clone(arguments[1]) : {},
plugins = "plugins" in options ? options.plugins : [],
config = {
parser: this.parser,
passes: utils.values(this.compiler.passes)
};
utils.each(plugins, function(p) { p.use(config, options); });
return this.compiler.compile(
this.parser.parse(grammar),
utils.values(this.compiler.passes),
config.parser.parse(grammar),
config.passes,
options
);
}

Loading…
Cancel
Save