Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'. This closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
eploko committed Feb 10, 2016
2 parents dd617ef + 52447a5 commit e3e3c39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ Then you only need to write: `require("./parser.pegjs")`.

You can pass options to PEG.js as [query parameters](http://webpack.github.io/docs/using-loaders.html#query-parameters). The following options are supported:

* `cache`if `true`, makes the parser cache results, avoiding exponential
* `cache`If `true`, makes the parser cache results, avoiding exponential
parsing time in pathological cases but making the parser slower (default:
`false`)
`false`).

* `optimize` - whether to optimize the built parser either for `speed` or
`size` (default: `speed`)
* `optimize` - Whether to optimize the built parser either for `speed` or
`size` (default: `speed`).

* `allowedStartRules` - The rules the built parser will be allowed to start
parsing from (default: the first rule in the grammar).

* `trace` - If `true`, the tracing support in the built parser is enabled
(default: `false`).

``` js
module.exports = {
Expand All @@ -58,7 +64,7 @@ module.exports = {
loaders: [
{
test: /\.pegjs$/,
loader: 'pegjs-loader?cache=true&optimize=size'
loader: 'pegjs-loader?cache=true&optimize=size&allowedStartRules[]=RuleA,allowedStartRules[]=RuleB&trace=true'
}
]
}
Expand All @@ -74,6 +80,7 @@ Every release, along with the migration instructions, if any, is documented on t

* [Victor Homyakov](https://github.com/victor-homyakov) for the propagation of the `cache` option.
* [VladimirTechMan](https://github.com/VladimirTechMan) for the propagation of the `optimize` option.
* [ragtime](https://github.com/ragtime) for the propagation of the `allowedStartRules` and `trace` options.

## License

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pegjs-loader",
"version": "0.3.0",
"version": "0.4.0",
"description": "PEG.js loader for webpack",
"authors": [
"Andrey Subbotin <andrey@subbotin.me> (https://github.com/eploko)",
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ export default function loader(source) {
const query = loaderUtils.parseQuery(this.query);
const cacheParserResults = !!query.cache;
const optimizeParser = query.optimize || 'speed';
const trace = !!query.trace;

let allowedStartRules;
if (typeof query.allowedStartRules === 'string') {
allowedStartRules = [ query.allowedStartRules ];
} else if (Array.isArray(query.allowedStartRules)) {
allowedStartRules = query.allowedStartRules;
} else {
allowedStartRules = [];
}

// Description of PEG.js options: https://github.com/pegjs/pegjs#javascript-api
const pegOptions = {
output: 'source',
cache: cacheParserResults,
optimize: optimizeParser,
trace: trace,
};
if (allowedStartRules.length > 0) {
pegOptions.allowedStartRules = allowedStartRules;
}

return `module.exports = ${pegjs.buildParser(source, pegOptions)};`;
}

0 comments on commit e3e3c39

Please sign in to comment.