From 7cd291cbe0fe59af98bc6aee15cac7f81239d9f2 Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Wed, 21 Mar 2018 02:13:47 +0000 Subject: [PATCH] Add doc for plugins (closes #544) --- docs/guides/plugins.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/guides/plugins.md diff --git a/docs/guides/plugins.md b/docs/guides/plugins.md new file mode 100644 index 0000000..db320c4 --- /dev/null +++ b/docs/guides/plugins.md @@ -0,0 +1,34 @@ +## Plugins + +You can use plugins by simply passing them to the generate method (e.g. [`peg.generate(grammar, { plugins: [plugin1, plugin2, etc] })`](./javascript-api.md#peggenerategrammar-options)). + + +#### Creating a Plugin + +Plugins are expected to be an object that contains at least one method: `use( session, options )` + +- `session` - See the docs for [peg.compiler.Session](./javascript-api.md#new-pegcompilersessionoptions) +- `options` - see the docs for [`peg.generate`](./javascript-api.md#peggenerategrammar-options) and [`peg.compiler.compile`](./javascript-api.md#pegcompilercompileast-session-options) + +Here is a simple example: + +```js +import customGrammarParser from "./parser"; + +export function use( session, options ) { + + // Replace the grammar parser + config.parser = customGrammarParser; + + // always output the source + options.output = "source"; + +} +``` + +#### Resources + +You can find some plugins to use on + +- the [PEG.js wiki](https://github.com/pegjs/pegjs/wiki/Plugins) (or add yours to the list for other's to find) +- NPM: https://www.npmjs.com/browse/keyword/pegjs