PEG.js import loader for webpack
A simple loader for PEG.js that supports importing multiple grammars from different PEG.js files
Install
npm install --save-dev pegjs-import-loader pegjs webpack
Setup
Apply via webpack config
webpack.config.js:
moduleexports =...module:loaders:test: /\.pegjs$/loader: 'pegjs-import-loader'options:...;
PEG.js options
You can pass options to PEG.js through options property in webpack config. See more about PEG.js options
Usage
Importing Syntax
parser.pegjs:
const str = 'This is just an example string'; { return a; }Expression = head:Term tail: _ Term* return tail; Factor = "(" _ expr:Expression _ ")" return expr; / Integer @@
- Import statement must be after the initializer block of PEG.js (initializer is a piece of JavaScript code in curly braces (“{” and “}”) that precedes the first rule)
- When importing grammars from other files, all the rules in those files are accessible in the current context
- All the JS variables and functions in initializers will also be accessible from the current context
- Due to the accessibility of all the rules, variables and functions from other pegjs files in current context, users must be aware of duplication of rule, variable and function names
- Import files in one-way flow to avoid circular dependencies
Generate a parser in JS code
const parser = ;const result = parser;
License
MIT (https://opensource.org/licenses/mit-license.php)