diff --git a/README.md b/README.md index 1c29045..5888d86 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ You can tweak the generated parser with several options: * `-d`, `--dependency` — makes the parser require a specified dependency (can be specified multiple times) * `-e`, `--export-var` — name of a global variable into which the parser object is assigned to when no module loader is detected * `--extra-options` — additional options (in JSON format) to pass to `peg.generate` - * `-c`, `--config`, `--extra-options-file` — file with additional options (in JSON format) to pass to `peg.generate` + * `-c`, `--config`, `--extra-options-file` — file with additional options (in JSON or JavaScript) to pass to `peg.generate` * `-f`, `--format` — format of the generated parser: `amd`, `bare`, `commonjs`, `es`, `globals`, `umd` (default: `commonjs`) * `-O`, `--optimize` — selects between optimizing the generated parser for parsing speed (`speed`) or code size (`size`) (default: `speed`) * `-p`, `--plugin` — makes PEG.js use a specified plugin (can be specified multiple times) diff --git a/bin/options.js b/bin/options.js index db44485..976e82a 100644 --- a/bin/options.js +++ b/bin/options.js @@ -3,13 +3,13 @@ const fs = require( "fs" ); const path = require( "path" ); const peg = require( "../lib/peg" ); +const util = peg.util; // Options let inputFile = null; let outputFile = null; - -const options = { +let options = { "--": [], "cache": false, "dependencies": {}, @@ -35,33 +35,32 @@ function abort( message ) { } -function addExtraOptions( json ) { +function addExtraOptions( config ) { + + if ( typeof config === "string" ) { - let extraOptions; + try { - try { + config = JSON.parse( config ); - extraOptions = JSON.parse( json ); + } catch ( e ) { - } catch ( e ) { + if ( ! ( e instanceof SyntaxError ) ) throw e; + abort( "Error parsing JSON: " + e.message ); - if ( ! ( e instanceof SyntaxError ) ) throw e; - abort( "Error parsing JSON: " + e.message ); + } } - if ( typeof extraOptions !== "object" ) { + if ( typeof config !== "object" ) { abort( "The JSON with extra options has to represent an object." ); } - Object - .keys( extraOptions ) - .forEach( key => { - - options[ key ] = extraOptions[ key ]; - - } ); + const extraOptions = {}; + util.extend( extraOptions, config ); + util.extend( extraOptions, options ); + options = extraOptions; } @@ -112,7 +111,7 @@ function nextArg( option ) { while ( args.length > 0 ) { - let json, mod; + let config, mod; let argument = args.shift(); if ( argument.indexOf( "-" ) === 0 && argument.indexOf( "=" ) > 1 ) { @@ -168,16 +167,24 @@ while ( args.length > 0 ) { case "--config": case "--extra-options-file": argument = nextArg( "-c/--config/--extra-options-file" ); - try { + if ( path.extname( argument ) === ".js" ) { - json = fs.readFileSync( argument, "utf8" ); + config = require( path.resolve( argument ) ); - } catch ( e ) { + } else { - abort( `Can't read from file "${ argument }".` ); + try { + + config = fs.readFileSync( argument, "utf8" ); + + } catch ( e ) { + + abort( `Can't read from file "${ argument }".` ); + + } } - addExtraOptions( json ); + addExtraOptions( config ); break; case "-f": diff --git a/bin/usage.js b/bin/usage.js index f7e7f34..e908e93 100644 --- a/bin/usage.js +++ b/bin/usage.js @@ -26,7 +26,7 @@ Options: (note: repeatable option) --extra-options-file File with additional options (in JSON - format) to pass to peg.generate + or JavaScript) to pass to peg.generate (note: repeatable option) -f, --format Format of the generated parser: diff --git a/gulpfile.js b/gulpfile.js index 745a00c..e23a574 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -34,6 +34,7 @@ function node( args, cb ) { task( "lint", () => gulp .src( [ "**/.*rc.js", + "bin/*.js", "lib/**/*.js", "!lib/parser/index.js", "test/benchmark/**/*.js", @@ -41,7 +42,7 @@ task( "lint", () => gulp "test/impact", "test/spec/**/*.js", "test/server/run", - "bin/*.js", + "src/*.js", "gulpfile.js" ] ) .pipe( eslint( { dotfiles: true } ) ) @@ -65,7 +66,7 @@ task( "benchmark", cb => { // Generate the grammar parser. task( "build:parser", cb => { - node( "bin/peg src/parser.pegjs -o lib/parser/index.js -c src/config.json", cb ); + node( "bin/peg src/parser.pegjs -o lib/parser/index.js -c src/pegjs.config.js", cb ); } ); diff --git a/src/config.json b/src/config.json deleted file mode 100644 index 6f3be12..0000000 --- a/src/config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "header": "/* eslint-disable */", - "dependencies": { - "ast": "./ast", - "util": "../util" - } -} diff --git a/src/pegjs.config.js b/src/pegjs.config.js new file mode 100644 index 0000000..d0d1620 --- /dev/null +++ b/src/pegjs.config.js @@ -0,0 +1,14 @@ +"use strict"; + +module.exports = { + + header: "/* eslint-disable */", + + dependencies: { + + ast: "./ast", + util: "../util" + + }, + +};