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.
master
Futago-za Ryuu 6 years ago
parent d06a5b52ef
commit b6bc0d905e

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

@ -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,13 +35,13 @@ function abort( message ) {
}
function addExtraOptions( json ) {
function addExtraOptions( config ) {
let extraOptions;
if ( typeof config === "string" ) {
try {
extraOptions = JSON.parse( json );
config = JSON.parse( config );
} catch ( e ) {
@ -49,19 +49,18 @@ function addExtraOptions( json ) {
abort( "Error parsing JSON: " + e.message );
}
if ( typeof extraOptions !== "object" ) {
abort( "The JSON with extra options has to represent an object." );
}
if ( typeof config !== "object" ) {
Object
.keys( extraOptions )
.forEach( key => {
abort( "The JSON with extra options has to represent an object." );
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" );
if ( path.extname( argument ) === ".js" ) {
config = require( path.resolve( argument ) );
} else {
try {
json = fs.readFileSync( argument, "utf8" );
config = fs.readFileSync( argument, "utf8" );
} catch ( e ) {
abort( `Can't read from file "${ argument }".` );
}
addExtraOptions( json );
}
addExtraOptions( config );
break;
case "-f":

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

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

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

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