Use .js files with -c option on CLI

This commit adds support for '.js' files to be passed to the '-c', '--config'  or '--extra-options-file' options on the CLI, allowing the developer to do some extra work before the parser is generated (if they wish), or dynamically set options based on the enviroment.
This commit is contained in:
Futago-za Ryuu 2018-01-19 08:55:04 +00:00
parent d06a5b52ef
commit b6bc0d905e
6 changed files with 49 additions and 34 deletions

View file

@ -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)

View file

@ -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 ) {
let extraOptions;
if ( typeof config === "string" ) {
try {
try {
extraOptions = JSON.parse( json );
config = JSON.parse( config );
} 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":

View file

@ -26,7 +26,7 @@ Options:
(note: repeatable option)
--extra-options-file <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> Format of the generated parser:

View file

@ -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 );
} );

View file

@ -1,7 +0,0 @@
{
"header": "/* eslint-disable */",
"dependencies": {
"ast": "./ast",
"util": "../util"
}
}

14
src/pegjs.config.js Normal file
View file

@ -0,0 +1,14 @@
"use strict";
module.exports = {
header: "/* eslint-disable */",
dependencies: {
ast: "./ast",
util: "../util"
},
};