Browse Source

Relay parser opts from peg.generate (#553)

master
Futago-za Ryuu 4 years ago
parent
commit
fe6f09238a
  1. 60
      lib/compiler/js.js
  2. 41
      lib/parser.js
  3. 2
      lib/peg.js
  4. 10
      lib/typings/api.d.ts
  5. 40
      src/parser.pegjs
  6. 1
      src/pegjs.config.js

60
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;

41
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;

2
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
);

10
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<T = compiler.OutputOptions> extends compiler.ICompilerOptions<T> {
plugins?: IPlugin<T>[];
parser?: parser.IOptions;
}

40
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;

1
src/pegjs.config.js

@ -7,6 +7,7 @@ module.exports = {
dependencies: {
ast: "./ast",
js: "./compiler/js",
util: "./util"
},

Loading…
Cancel
Save