From fe6f09238a398d3b4a0a3d4574b023cff4eb69b8 Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Sun, 28 Jan 2018 23:42:16 +0000 Subject: [PATCH] Relay parser opts from peg.generate (#553) --- lib/compiler/js.js | 60 +++++++++++++++++++++++++++++++++++++++++++- lib/parser.js | 41 +++--------------------------- lib/peg.js | 2 +- lib/typings/api.d.ts | 10 +++++++- src/parser.pegjs | 40 ++--------------------------- src/pegjs.config.js | 1 + 6 files changed, 75 insertions(+), 79 deletions(-) diff --git a/lib/compiler/js.js b/lib/compiler/js.js index f09251a..952ba58 100644 --- a/lib/compiler/js.js +++ b/lib/compiler/js.js @@ -8,6 +8,7 @@ function hex( ch ) { // JavaScript code generation helpers. const js = { + stringEscape( s ) { // ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string @@ -56,7 +57,64 @@ const js = { .replace( /[\u0100-\u0FFF]/g, ch => "\\u0" + hex( ch ) ) .replace( /[\u1000-\uFFFF]/g, ch => "\\u" + hex( ch ) ); - } + }, + + // This is a list of reserved words for ESMA-262, 5th ed., 7.6.1 (strict mode) + reservedWords: [ + + // Keyword + "break", + "case", + "catch", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "finally", + "for", + "function", + "if", + "in", + "instanceof", + "new", + "return", + "switch", + "this", + "throw", + "try", + "typeof", + "var", + "void", + "while", + "with", + + // FutureReservedWord + "class", + "const", + "enum", + "export", + "extends", + "implements", + "import", + "interface", + "let", + "package", + "private", + "protected", + "public", + "static", + "super", + "yield", + + // Literal + "false", + "null", + "true", + + ], + }; module.exports = js; diff --git a/lib/parser.js b/lib/parser.js index 33a62d1..cad3a6f 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -5,6 +5,7 @@ "use strict"; var ast = require("./ast"); +var js = require("./compiler/js"); var util = require("./util"); function peg$subclass(child, parent) { @@ -3320,44 +3321,8 @@ function peg$parse(input, options) { "!": "semantic_not" }; - const RESERVED_WORDS = [ - "break", - "case", - "catch", - "class", - "const", - "continue", - "debugger", - "default", - "delete", - "do", - "else", - "enum", - "export", - "extends", - "false", - "finally", - "for", - "function", - "if", - "import", - "instanceof", - "in", - "new", - "null", - "return", - "super", - "switch", - "this", - "throw", - "true", - "try", - "typeof", - "var", - "void", - "while", - "with" - ]; + let RESERVED_WORDS = options.reservedWords || js.reservedWords; + if ( !Array.isArray(RESERVED_WORDS) ) RESERVED_WORDS = []; function extractOptional(optional, index) { return optional ? optional[index] : null; diff --git a/lib/peg.js b/lib/peg.js index 00df315..66b2d70 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -42,7 +42,7 @@ const peg = { } ); return compiler.compile( - config.parser.parse( grammar ), + config.parser.parse( grammar, options.parser || {} ), config.passes, options ); diff --git a/lib/typings/api.d.ts b/lib/typings/api.d.ts index 32ea1ed..fde5e2e 100644 --- a/lib/typings/api.d.ts +++ b/lib/typings/api.d.ts @@ -319,8 +319,15 @@ declare namespace peg { */ namespace parser { + interface IOptions extends gp.IOptions { + + extractComments?: boolean; + reservedWords?: string[]; + + } + const SyntaxError: SyntaxError; - function parse( input: string, options?: gp.IOptions ): Grammar; + function parse( input: string, options?: IOptions ): Grammar; } @@ -464,6 +471,7 @@ declare namespace peg { interface IBuildOptions extends compiler.ICompilerOptions { plugins?: IPlugin[]; + parser?: parser.IOptions; } diff --git a/src/parser.pegjs b/src/parser.pegjs index 23a15fa..a9fbf58 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -39,44 +39,8 @@ "!": "semantic_not" }; - const RESERVED_WORDS = [ - "break", - "case", - "catch", - "class", - "const", - "continue", - "debugger", - "default", - "delete", - "do", - "else", - "enum", - "export", - "extends", - "false", - "finally", - "for", - "function", - "if", - "import", - "instanceof", - "in", - "new", - "null", - "return", - "super", - "switch", - "this", - "throw", - "true", - "try", - "typeof", - "var", - "void", - "while", - "with" - ]; + let RESERVED_WORDS = options.reservedWords || js.reservedWords; + if ( !Array.isArray(RESERVED_WORDS) ) RESERVED_WORDS = []; function extractOptional(optional, index) { return optional ? optional[index] : null; diff --git a/src/pegjs.config.js b/src/pegjs.config.js index 2f14601..d3750fd 100644 --- a/src/pegjs.config.js +++ b/src/pegjs.config.js @@ -7,6 +7,7 @@ module.exports = { dependencies: { ast: "./ast", + js: "./compiler/js", util: "./util" },