Browse Source

Added 'header' option (#491)

master
Futago-za Ryuu 4 years ago
parent
commit
db70215c4a
  1. 3
      README.md
  2. 2
      gulpfile.js
  3. 38
      lib/compiler/passes/generate-js.js
  4. 6
      lib/parser.js
  5. 3
      src/config.json

3
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

2
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.

38
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);",

6
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";

3
src/config.json

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