diff --git a/README.md b/README.md index 7570971..b569b22 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,9 @@ object to `peg.generate`. The following options are supported: valid only when `format` is set to `"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"`) + * `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"`) * `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 diff --git a/gulpfile.js b/gulpfile.js index 4d6428a..3c7e9a6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -53,7 +53,7 @@ task( "benchmark", () => node( "test/benchmark/run" ) ); // Generate the grammar 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. diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 8e9c7ce..d952821 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1325,13 +1325,27 @@ function generateJS( ast, options ) { function generateWrapper( toplevelCode ) { - function generateGeneratedByComment() { + function generateHeaderComment() { - return [ - "// Generated by PEG.js 0.10.0.", - "//", - "// https://pegjs.org/" - ].join( "\n" ); + let comment = "// Generated by PEG.js v0.10.0, https://pegjs.org/"; + const header = options.header; + + if ( typeof header === "string" ) { + + 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() { return [ - generateGeneratedByComment(), + generateHeaderComment(), "(function() {", " \"use strict\";", "", @@ -1395,7 +1409,7 @@ function generateJS( ast, options ) { const dependencyVars = Object.keys( options.dependencies ); parts.push( [ - generateGeneratedByComment(), + generateHeaderComment(), "", "\"use strict\";", "" @@ -1433,7 +1447,7 @@ function generateJS( ast, options ) { const dependencyVars = Object.keys( options.dependencies ); parts.push( - generateGeneratedByComment(), + generateHeaderComment(), "" ); @@ -1477,7 +1491,7 @@ function generateJS( ast, options ) { const params = dependencyVars.join( ", " ); return [ - generateGeneratedByComment(), + generateHeaderComment(), "define(" + dependencies + ", function(" + params + ") {", " \"use strict\";", "", @@ -1493,7 +1507,7 @@ function generateJS( ast, options ) { globals() { return [ - generateGeneratedByComment(), + generateHeaderComment(), "(function(root) {", " \"use strict\";", "", @@ -1522,7 +1536,7 @@ function generateJS( ast, options ) { const params = dependencyVars.join( ", " ); parts.push( [ - generateGeneratedByComment(), + generateHeaderComment(), "(function(root, factory) {", " if (typeof define === \"function\" && define.amd) {", " define(" + dependencies + ", factory);", diff --git a/lib/parser.js b/lib/parser.js index b05dd01..f400b8c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1,6 +1,6 @@ -// Generated by PEG.js 0.10.0. -// -// https://pegjs.org/ +// Generated by PEG.js v0.10.0, https://pegjs.org/ + +/* eslint-disable */ "use strict"; diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..99165cc --- /dev/null +++ b/src/config.json @@ -0,0 +1,3 @@ +{ + "header": "/* eslint-disable */" +}