Added 'header' option (#491)

master
Futago-za Ryuu 6 years ago
parent 247a0bf707
commit db70215c4a

@ -178,6 +178,9 @@ object to `peg.generate`. The following options are supported:
valid only when `format` is set to `"globals"` or `"umd"` valid only when `format` is set to `"globals"` or `"umd"`
* `format` — format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, `"es"`, `"globals"`, or `"umd"`); * `format` — format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, `"es"`, `"globals"`, or `"umd"`);
valid only when `output` is set to `"source"` (default: `"bare"`) valid only when `output` is set to `"source"` (default: `"bare"`)
* `header` — this option is only handled if it's an array or a string:
* `[ string1, string2, ... ]` will add each element (all expected to be strings) as a separate line comment
* `string` will simply append the string (e.g. `"/* eslint-disable */"`) after the `Generated by ...` comment
* `optimize`— selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) (default: `"speed"`) * `optimize`— selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) (default: `"speed"`)
* `output` — if set to `"parser"` (default), the method will return generated parser object; * `output` — if set to `"parser"` (default), the method will return generated parser object;
if set to `"source"`, it will return parser source code as a string if set to `"source"`, it will return parser source code as a string

@ -53,7 +53,7 @@ task( "benchmark", () => node( "test/benchmark/run" ) );
// Generate the grammar parser. // Generate the grammar parser.
task( "build:parser", () => task( "build:parser", () =>
node( "bin/peg src/parser.pegjs -o lib/parser.js" ) node( "bin/peg src/parser.pegjs -o lib/parser.js -c src/config.json" )
); );
// Create the browser build. // Create the browser build.

@ -1325,13 +1325,27 @@ function generateJS( ast, options ) {
function generateWrapper( toplevelCode ) { function generateWrapper( toplevelCode ) {
function generateGeneratedByComment() { function generateHeaderComment() {
return [ let comment = "// Generated by PEG.js v0.10.0, https://pegjs.org/";
"// Generated by PEG.js 0.10.0.", const header = options.header;
"//",
"// https://pegjs.org/" if ( typeof header === "string" ) {
].join( "\n" );
comment += "\n\n" + header;
} else if ( Array.isArray( header ) ) {
comment += "\n\n";
header.forEach( data => {
comment += "// " + data;
} );
}
return comment;
} }
@ -1377,7 +1391,7 @@ function generateJS( ast, options ) {
bare() { bare() {
return [ return [
generateGeneratedByComment(), generateHeaderComment(),
"(function() {", "(function() {",
" \"use strict\";", " \"use strict\";",
"", "",
@ -1395,7 +1409,7 @@ function generateJS( ast, options ) {
const dependencyVars = Object.keys( options.dependencies ); const dependencyVars = Object.keys( options.dependencies );
parts.push( [ parts.push( [
generateGeneratedByComment(), generateHeaderComment(),
"", "",
"\"use strict\";", "\"use strict\";",
"" ""
@ -1433,7 +1447,7 @@ function generateJS( ast, options ) {
const dependencyVars = Object.keys( options.dependencies ); const dependencyVars = Object.keys( options.dependencies );
parts.push( parts.push(
generateGeneratedByComment(), generateHeaderComment(),
"" ""
); );
@ -1477,7 +1491,7 @@ function generateJS( ast, options ) {
const params = dependencyVars.join( ", " ); const params = dependencyVars.join( ", " );
return [ return [
generateGeneratedByComment(), generateHeaderComment(),
"define(" + dependencies + ", function(" + params + ") {", "define(" + dependencies + ", function(" + params + ") {",
" \"use strict\";", " \"use strict\";",
"", "",
@ -1493,7 +1507,7 @@ function generateJS( ast, options ) {
globals() { globals() {
return [ return [
generateGeneratedByComment(), generateHeaderComment(),
"(function(root) {", "(function(root) {",
" \"use strict\";", " \"use strict\";",
"", "",
@ -1522,7 +1536,7 @@ function generateJS( ast, options ) {
const params = dependencyVars.join( ", " ); const params = dependencyVars.join( ", " );
parts.push( [ parts.push( [
generateGeneratedByComment(), generateHeaderComment(),
"(function(root, factory) {", "(function(root, factory) {",
" if (typeof define === \"function\" && define.amd) {", " if (typeof define === \"function\" && define.amd) {",
" define(" + dependencies + ", factory);", " define(" + dependencies + ", factory);",

@ -1,6 +1,6 @@
// Generated by PEG.js 0.10.0. // Generated by PEG.js v0.10.0, https://pegjs.org/
//
// https://pegjs.org/ /* eslint-disable */
"use strict"; "use strict";

@ -0,0 +1,3 @@
{
"header": "/* eslint-disable */"
}
Loading…
Cancel
Save