From 8b43c8419f566267a65620d1a62d7be60925fd1d Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Fri, 14 Sep 2018 03:43:09 +0100 Subject: [PATCH] Use input/output from the config file --- docs/guides/command-line.md | 10 ++++++++++ gulpfile.js | 2 +- packages/pegjs/bin/options.js | 36 +++++++++++++++++++++++++++++++++++ src/pegjs.config.js | 3 +++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/guides/command-line.md b/docs/guides/command-line.md index e259ac3..6ae93c6 100644 --- a/docs/guides/command-line.md +++ b/docs/guides/command-line.md @@ -4,6 +4,16 @@ The CLI `pegjs` can be used from terminals to generate a parser from the given g It's usage signature is: `pegjs [options] [] [--]` +### Configuration file + +If you provide a configuration file (via `-c` or `--config`) then you can use all the options used by the [PEG.js API](./javascript-api.md), as well as: + +* `input` — The grammar used as the input file +* `output` — Output's generated parser to the provided destination + +> 1. Will throw if neither are strings, or the input has already been provided +> 2. the `output` option for the compiler is not usable from the config + ### Input File Only one grammar is accepted as the given input file, otherwise the CLI aborts with an error. diff --git a/gulpfile.js b/gulpfile.js index 68d1b06..4cefddb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -59,7 +59,7 @@ task( "benchmark", cb => { // Generate the grammar parser. task( "build:parser", cb => { - node( "packages/pegjs/bin/peg src/parser.pegjs -o packages/pegjs/lib/parser.js -c src/pegjs.config.js", cb ); + node( "packages/pegjs/bin/peg -c src/pegjs.config.js", cb ); } ); diff --git a/packages/pegjs/bin/options.js b/packages/pegjs/bin/options.js index a4e9adc..31af1f5 100644 --- a/packages/pegjs/bin/options.js +++ b/packages/pegjs/bin/options.js @@ -58,6 +58,42 @@ function addExtraOptions( config ) { } + if ( config.input !== null || config.output !== null ) { + + // We don't want to touch the orignal config, just in case it comes from + // a javascript file, in which case its possible the same object is used + // for something else, somewhere else. + config = util.clone( config ); + const { input, output } = config; + + if ( input !== null ) { + + if ( typeof input !== "string" ) + + abort( "The option `input` must be a string." ); + + if ( inputFile !== null ) + + abort( `The input file is already set, cannot use: "${ input }".` ); + + inputFile = input; + delete config.input; + + } + + if ( output !== null ) { + + if ( typeof output !== "string" ) + + abort( "The option `output` must be a string." ); + + outputFile = output; + delete config.output; + + } + + } + options = util.processOptions( config, options ); } diff --git a/src/pegjs.config.js b/src/pegjs.config.js index ec9d1d1..d5468b8 100644 --- a/src/pegjs.config.js +++ b/src/pegjs.config.js @@ -2,6 +2,9 @@ module.exports = { + input: "src/parser.pegjs", + output: "packages/pegjs/lib/parser.js", + header: "/* eslint-disable */", dependencies: {