Move compiler/js.js to util/js.js

master
Futago-za Ryuu 6 years ago
parent 8d3dc109ed
commit 01555ebbac

@ -1,120 +0,0 @@
"use strict";
function hex( ch ) {
return ch.charCodeAt( 0 ).toString( 16 ).toUpperCase();
}
// JavaScript code generation helpers.
const js = {
stringEscape( s ) {
// ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
// literal except for the closing quote character, backslash, carriage
// return, line separator, paragraph separator, and line feed. Any character
// may appear in the form of an escape sequence.
//
// For portability, we also escape all control and non-ASCII characters.
return s
.replace( /\\/g, "\\\\" ) // backslash
.replace( /"/g, "\\\"" ) // closing double quote
.replace( /\0/g, "\\0" ) // null
.replace( /\x08/g, "\\b" ) // backspace
.replace( /\t/g, "\\t" ) // horizontal tab
.replace( /\n/g, "\\n" ) // line feed
.replace( /\v/g, "\\v" ) // vertical tab
.replace( /\f/g, "\\f" ) // form feed
.replace( /\r/g, "\\r" ) // carriage return
.replace( /[\x00-\x0F]/g, ch => "\\x0" + hex( ch ) )
.replace( /[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex( ch ) )
.replace( /[\u0100-\u0FFF]/g, ch => "\\u0" + hex( ch ) )
.replace( /[\u1000-\uFFFF]/g, ch => "\\u" + hex( ch ) );
},
regexpEscape( s ) {
// Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
//
// For portability, we also escape all control and non-ASCII characters.
return s
.replace( /\\/g, "\\\\" ) // backslash
.replace( /\//g, "\\/" ) // closing slash
.replace( /]/g, "\\]" ) // closing bracket
.replace( /\^/g, "\\^" ) // caret
.replace( /-/g, "\\-" ) // dash
.replace( /\0/g, "\\0" ) // null
.replace( /\x08/g, "\\b" ) // backspace
.replace( /\t/g, "\\t" ) // horizontal tab
.replace( /\n/g, "\\n" ) // line feed
.replace( /\v/g, "\\v" ) // vertical tab
.replace( /\f/g, "\\f" ) // form feed
.replace( /\r/g, "\\r" ) // carriage return
.replace( /[\x00-\x0F]/g, ch => "\\x0" + hex( ch ) )
.replace( /[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex( ch ) )
.replace( /[\u0100-\u0FFF]/g, ch => "\\u0" + hex( ch ) )
.replace( /[\u1000-\uFFFF]/g, ch => "\\u" + hex( ch ) );
},
// This is a list of reserved words for ECMA-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;

@ -2,7 +2,7 @@
"use strict";
const js = require( "../js" );
const util = require( "../../util" );
// Generates parser JavaScript code.
function generateJS( ast, session, options ) {
@ -43,7 +43,7 @@ function generateJS( ast, session, options ) {
function buildLiteral( literal ) {
return `"${ js.stringEscape( literal ) }"`;
return `"${ util.stringEscape( literal ) }"`;
}
@ -55,10 +55,10 @@ function generateJS( ast, session, options ) {
.map( part => (
Array.isArray( part )
? js.regexpEscape( part[ 0 ] )
? util.regexpEscape( part[ 0 ] )
+ "-"
+ js.regexpEscape( part[ 1 ] )
: js.regexpEscape( part )
+ util.regexpEscape( part[ 1 ] )
: util.regexpEscape( part )
) )
.join( "" )
@ -72,11 +72,11 @@ function generateJS( ast, session, options ) {
switch ( e.type ) {
case "rule":
return `peg$otherExpectation("${ js.stringEscape( e.value ) }")`;
return `peg$otherExpectation("${ util.stringEscape( e.value ) }")`;
case "literal":
return "peg$literalExpectation(\""
+ js.stringEscape( e.value )
+ util.stringEscape( e.value )
+ "\", "
+ e.ignoreCase
+ ")";
@ -85,8 +85,8 @@ function generateJS( ast, session, options ) {
const parts = e.value.map( part =>
( Array.isArray( part )
? `["${ js.stringEscape( part[ 0 ] ) }", "${ js.stringEscape( part[ 1 ] ) }"]`
: `"${ js.stringEscape( part ) }"` )
? `["${ util.stringEscape( part[ 0 ] ) }", "${ util.stringEscape( part[ 1 ] ) }"]`
: `"${ util.stringEscape( part ) }"` )
);
return "peg$classExpectation(["
@ -134,7 +134,7 @@ function generateJS( ast, session, options ) {
indent2( ast.rules
.map( rule =>
`peg$decode("${
js.stringEscape( rule.bytecode
util.stringEscape( rule.bytecode
.map( b => String.fromCharCode( b + 32 ) )
.join( "" )
)
@ -973,12 +973,12 @@ function generateJS( ast, session, options ) {
parts.push( " var " + stackVars.join( ", " ) + ";" );
parts.push( indent2( generateRuleHeader(
"\"" + js.stringEscape( rule.name ) + "\"",
"\"" + util.stringEscape( rule.name ) + "\"",
ast.indexOfRule( rule.name )
) ) );
parts.push( indent2( code ) );
parts.push( indent2( generateRuleFooter(
"\"" + js.stringEscape( rule.name ) + "\"",
"\"" + util.stringEscape( rule.name ) + "\"",
s( 0 )
) ) );
@ -1259,7 +1259,7 @@ function generateJS( ast, session, options ) {
const ruleNames = "["
+ ast.rules
.map( r => `"${ js.stringEscape( r.name ) }"` )
.map( r => `"${ util.stringEscape( r.name ) }"` )
.join( ", " )
+ "]";
@ -1687,7 +1687,7 @@ function generateJS( ast, session, options ) {
parts.push( "var " + variable
+ " = require(\""
+ js.stringEscape( options.dependencies[ variable ] )
+ util.stringEscape( options.dependencies[ variable ] )
+ "\");"
);
@ -1723,7 +1723,7 @@ function generateJS( ast, session, options ) {
parts.push( "import " + variable
+ " from \""
+ js.stringEscape( options.dependencies[ variable ] )
+ util.stringEscape( options.dependencies[ variable ] )
+ "\";"
);
@ -1751,7 +1751,7 @@ function generateJS( ast, session, options ) {
const dependencyIds = dependencyVars.map( v => options.dependencies[ v ] );
const dependencies = "["
+ dependencyIds
.map( id => `"${ js.stringEscape( id ) }"` )
.map( id => `"${ util.stringEscape( id ) }"` )
.join( ", " )
+ "]";
const params = dependencyVars.join( ", " );
@ -1793,11 +1793,11 @@ function generateJS( ast, session, options ) {
const dependencyIds = dependencyVars.map( v => options.dependencies[ v ] );
const dependencies = "["
+ dependencyIds
.map( id => `"${ js.stringEscape( id ) }"` )
.map( id => `"${ util.stringEscape( id ) }"` )
.join( ", " )
+ "]";
const requires = dependencyIds
.map( id => `require("${ js.stringEscape( id ) }")` )
.map( id => `require("${ util.stringEscape( id ) }")` )
.join( ", " );
const args = dependencyVars.map( v => "root." + v ).join( ", " );
const params = dependencyVars.join( ", " );

@ -536,6 +536,13 @@ declare namespace peg {
}
interface IJavaScriptUtils {
stringEscape( s: string ): string;
regexpEscape( s: string ): string;
reservedWords: string[];
}
interface IObjectUtils {
clone( source: {} ): {};
@ -546,7 +553,7 @@ declare namespace peg {
enforceFastProperties( o: {} ): {};
}
interface util extends IObjectUtils {
interface util extends IJavaScriptUtils, IObjectUtils {
noop(): void;
convertPasses( stages: IStageMap ): compiler.IPassesMap;

@ -60,19 +60,6 @@ declare module "pegjs/lib/compiler/index" {
}
declare module "pegjs/lib/compiler/js" {
namespace js {
function stringEscape( s: string ): string;
function regexpEscape( s: string ): string;
const reservedWords: string[];
}
export default js;
}
declare module "pegjs/lib/compiler/opcodes" {
const opcodes: peg.compiler.IOpcodes;
@ -170,6 +157,13 @@ declare module "pegjs/lib/util/index" {
}
declare module "pegjs/lib/util/js" {
const js: peg.IJavaScriptUtils;
export default js;
}
declare module "pegjs/lib/util/objects" {
const objects: peg.IObjectUtils;

@ -1,6 +1,9 @@
"use strict";
const js = require( "./js" );
const objects = require( "./objects" );
objects.extend( exports, js );
objects.extend( exports, objects );
exports.noop = function noop() { };
@ -39,7 +42,6 @@ exports.convertPasses = ( () => {
} )();
exports.processOptions = function processOptions( options, defaults ) {
const processedOptions = {};

@ -0,0 +1,123 @@
"use strict";
function hex( ch ) {
return ch.charCodeAt( 0 ).toString( 16 ).toUpperCase();
}
function sourceEscape( s ) {
return s
.replace( /\0/g, "\\0" ) // null
.replace( /\x08/g, "\\b" ) // backspace
.replace( /\t/g, "\\t" ) // horizontal tab
.replace( /\n/g, "\\n" ) // line feed
.replace( /\v/g, "\\v" ) // vertical tab
.replace( /\f/g, "\\f" ) // form feed
.replace( /\r/g, "\\r" ) // carriage return
.replace( /[\x00-\x0F]/g, ch => "\\x0" + hex( ch ) )
.replace( /[\x10-\x1F\x7F-\xFF]/g, ch => "\\x" + hex( ch ) )
.replace( /[\u0100-\u0FFF]/g, ch => "\\u0" + hex( ch ) )
.replace( /[\u1000-\uFFFF]/g, ch => "\\u" + hex( ch ) );
}
// JavaScript code generation helpers.
const js = {
/**
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
* literal except for the closing quote character, backslash, carriage
* return, line separator, paragraph separator, and line feed. Any character
* may appear in the form of an escape sequence.
*
* For portability, we also escape all control and non-ASCII characters.
*/
stringEscape( s ) {
return sourceEscape( s
.replace( /\\/g, "\\\\" ) // backslash
.replace( /"/g, "\\\"" ) // closing double quote
);
},
/**
* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
*
* For portability, we also escape all control and non-ASCII characters.
*/
regexpEscape( s ) {
return sourceEscape( s
.replace( /\\/g, "\\\\" ) // backslash
.replace( /\//g, "\\/" ) // closing slash
.replace( /]/g, "\\]" ) // closing bracket
.replace( /\^/g, "\\^" ) // caret
.replace( /-/g, "\\-" ) // dash
);
},
/**
* This is a list of reserved words for ECMA-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;
Loading…
Cancel
Save