A Prettier plugin for formatting Pegjs and Peggy-js grammars
npm install prettier-plugin-pegjsA prettier plugin for formatting Pegjs grammars. You can try it out online in the playground
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
This plugin adds support for the Pegjs language to Prettier.
```
Expression = head:Term tail:(_("+"/"-")_ Term) * {
return tail.reduce(function(result, element) {if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head)}
``
Expression
= head:Term tail:(_ "+" / "-" _ Term)* {
return tail.reduce(function (result, element) {
if (element[1] === "+") {
return result + element[3];
}
if (element[1] === "-") {
return result - element[3];
}
}, head);
}
yarn:
`bash`
yarn add --dev prettier prettier-plugin-pegjsor globally
yarn global add prettier prettier-plugin-pegjs
npm:
`bash`
npm install --save-dev prettier prettier-plugin-pegjsor globally
npm install --global prettier prettier-plugin-pegjs
If you installed prettier as a local dependency, you can add prettier as a
script in your package.json,
`json`
{
"scripts": {
"prettier": "prettier"
}
}
and then run it via
`bash`
yarn run prettier path/to/grammar.pegjs --writeor
npm run prettier path/to/grammar.pegjs --write
If you installed globally, run
`bash`
prettier path/to/grammar.pegjs --write
This package exposes a standalone.ts that wraps prettier and exports aprintPrettier function that can be called as
`js`
printPrettier(YOUR_CODE, {
// example option
tabWidth: 2,
});
The standard Prettier options (such as tabWidth) can be used. Additionally,actionParser
you may set to specify how the code inside a Pegjs action isactionParser
printed. can be the parser from any valid Prettier plugin."babel-ts"
It defaults to for Javascript and Typescript, but it could be set
to a different parser if your actions are written in a different language/dialect.
To make a production build, run
``
npm run build
To develop, run
``
npm run watch
You can then execute Prettier with
``
prettier --plugin-search-dir=./ ...
or
``
prettier --plugin=./build/prettier-plugin-pegjs.js ...
and the Pegjs plugin will load from the current directory.
prettier-plugin-pegjs uses a Pegjs grammar (located in grammars/)=
to parse Pegjs grammars! This grammar is slightly modified from Pegjs's
official grammar to include delimiters and strings as AST nodes.
For example, the in Rule = a / b is assigned an AST node.prettier-plugin-pegjs
This is so that can use Prettier's automaticstart
comment placement algorithm, which searches through the AST and places comments
based on an AST node's and end position.
prettier-plugin-pegjs uses webpack to dynamically compile imported
Pegjs grammars, so they can be used _like_ native ES6 imports, though
of course they are not.
The plugin is organized as follows:
- prettier-plugin-pegjs.ts This file exports the objects required of astandalone.ts
Prettier plugin.
- This file wraps the Prettier parser and pre-loadsprettier-plugin-pegjs
as a plugin.grammars/pegjs.peggy
- The Pegjs grammar that parsers Pegjs/Peggy grammars.libs/parser.ts
- The parser which loads a Pegjs-created parser and createslibs/printer.ts` Printers take an AST and produce a Doc (the intermediate
an AST from a string.
-
format that Prettier uses). This is where most of the details of the plugin lie.