|
|
|
@ -1,8 +1,8 @@
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
let asts = require("../asts");
|
|
|
|
|
let op = require("../opcodes");
|
|
|
|
|
let js = require("../js");
|
|
|
|
|
let asts = require("../asts");
|
|
|
|
|
let op = require("../opcodes");
|
|
|
|
|
let js = require("../js");
|
|
|
|
|
|
|
|
|
|
// Generates parser JavaScript code.
|
|
|
|
|
function generateJS(ast, options) {
|
|
|
|
@ -50,7 +50,7 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
if (options.cache) {
|
|
|
|
|
parts.push([
|
|
|
|
|
"var key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";",
|
|
|
|
|
"var key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";",
|
|
|
|
|
"var cached = peg$resultsCache[key];",
|
|
|
|
|
"",
|
|
|
|
|
"if (cached) {",
|
|
|
|
@ -130,7 +130,7 @@ function generateJS(ast, options) {
|
|
|
|
|
let parts = [];
|
|
|
|
|
|
|
|
|
|
function generateCondition(cond, argsLength) {
|
|
|
|
|
let baseLength = argsLength + 3;
|
|
|
|
|
let baseLength = argsLength + 3;
|
|
|
|
|
let thenLengthCode = "bc[ip + " + (baseLength - 2) + "]";
|
|
|
|
|
let elseLengthCode = "bc[ip + " + (baseLength - 1) + "]";
|
|
|
|
|
|
|
|
|
@ -151,7 +151,7 @@ function generateJS(ast, options) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateLoop(cond) {
|
|
|
|
|
let baseLength = 2;
|
|
|
|
|
let baseLength = 2;
|
|
|
|
|
let bodyLengthCode = "bc[ip + " + (baseLength - 1) + "]";
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
@ -170,7 +170,7 @@ function generateJS(ast, options) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateCall() {
|
|
|
|
|
let baseLength = 4;
|
|
|
|
|
let baseLength = 4;
|
|
|
|
|
let paramsLengthCode = "bc[ip + " + (baseLength - 1) + "]";
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
@ -198,22 +198,22 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
if (options.trace) {
|
|
|
|
|
parts.push([
|
|
|
|
|
" var bc = peg$bytecode[index];",
|
|
|
|
|
" var ip = 0;",
|
|
|
|
|
" var ips = [];",
|
|
|
|
|
" var end = bc.length;",
|
|
|
|
|
" var ends = [];",
|
|
|
|
|
" var stack = [];",
|
|
|
|
|
" var bc = peg$bytecode[index];",
|
|
|
|
|
" var ip = 0;",
|
|
|
|
|
" var ips = [];",
|
|
|
|
|
" var end = bc.length;",
|
|
|
|
|
" var ends = [];",
|
|
|
|
|
" var stack = [];",
|
|
|
|
|
" var startPos = peg$currPos;",
|
|
|
|
|
" var params;"
|
|
|
|
|
].join("\n"));
|
|
|
|
|
} else {
|
|
|
|
|
parts.push([
|
|
|
|
|
" var bc = peg$bytecode[index];",
|
|
|
|
|
" var ip = 0;",
|
|
|
|
|
" var ips = [];",
|
|
|
|
|
" var end = bc.length;",
|
|
|
|
|
" var ends = [];",
|
|
|
|
|
" var bc = peg$bytecode[index];",
|
|
|
|
|
" var ip = 0;",
|
|
|
|
|
" var ips = [];",
|
|
|
|
|
" var end = bc.length;",
|
|
|
|
|
" var ends = [];",
|
|
|
|
|
" var stack = [];",
|
|
|
|
|
" var params;"
|
|
|
|
|
].join("\n"));
|
|
|
|
@ -403,7 +403,7 @@ function generateJS(ast, options) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateRuleFunction(rule) {
|
|
|
|
|
let parts = [];
|
|
|
|
|
let parts = [];
|
|
|
|
|
let stackVars = [];
|
|
|
|
|
let code;
|
|
|
|
|
|
|
|
|
@ -448,8 +448,8 @@ function generateJS(ast, options) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function compile(bc) {
|
|
|
|
|
let ip = 0;
|
|
|
|
|
let end = bc.length;
|
|
|
|
|
let ip = 0;
|
|
|
|
|
let end = bc.length;
|
|
|
|
|
let parts = [];
|
|
|
|
|
let value;
|
|
|
|
|
|
|
|
|
@ -457,7 +457,7 @@ function generateJS(ast, options) {
|
|
|
|
|
let baseLength = argCount + 3;
|
|
|
|
|
let thenLength = bc[ip + baseLength - 2];
|
|
|
|
|
let elseLength = bc[ip + baseLength - 1];
|
|
|
|
|
let baseSp = stack.sp;
|
|
|
|
|
let baseSp = stack.sp;
|
|
|
|
|
let thenCode, elseCode, thenSp, elseSp;
|
|
|
|
|
|
|
|
|
|
ip += baseLength;
|
|
|
|
@ -490,7 +490,7 @@ function generateJS(ast, options) {
|
|
|
|
|
function compileLoop(cond) {
|
|
|
|
|
let baseLength = 2;
|
|
|
|
|
let bodyLength = bc[ip + baseLength - 1];
|
|
|
|
|
let baseSp = stack.sp;
|
|
|
|
|
let baseSp = stack.sp;
|
|
|
|
|
let bodyCode, bodySp;
|
|
|
|
|
|
|
|
|
|
ip += baseLength;
|
|
|
|
@ -508,7 +508,7 @@ function generateJS(ast, options) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compileCall() {
|
|
|
|
|
let baseLength = 4;
|
|
|
|
|
let baseLength = 4;
|
|
|
|
|
let paramsLength = bc[ip + baseLength - 1];
|
|
|
|
|
|
|
|
|
|
let value = c(bc[ip + 1]) + "("
|
|
|
|
@ -752,11 +752,11 @@ function generateJS(ast, options) {
|
|
|
|
|
"}",
|
|
|
|
|
"",
|
|
|
|
|
"function peg$SyntaxError(message, expected, found, location) {",
|
|
|
|
|
" this.message = message;",
|
|
|
|
|
" this.message = message;",
|
|
|
|
|
" this.expected = expected;",
|
|
|
|
|
" this.found = found;",
|
|
|
|
|
" this.found = found;",
|
|
|
|
|
" this.location = location;",
|
|
|
|
|
" this.name = \"SyntaxError\";",
|
|
|
|
|
" this.name = \"SyntaxError\";",
|
|
|
|
|
"",
|
|
|
|
|
" if (typeof Error.captureStackTrace === \"function\") {",
|
|
|
|
|
" Error.captureStackTrace(this, peg$SyntaxError);",
|
|
|
|
@ -943,7 +943,7 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
parts.push([
|
|
|
|
|
" var peg$startRuleIndices = " + startRuleIndices + ";",
|
|
|
|
|
" var peg$startRuleIndex = " + startRuleIndex + ";"
|
|
|
|
|
" var peg$startRuleIndex = " + startRuleIndex + ";"
|
|
|
|
|
].join("\n"));
|
|
|
|
|
} else {
|
|
|
|
|
let startRuleFunctions = "{ "
|
|
|
|
@ -955,7 +955,7 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
parts.push([
|
|
|
|
|
" var peg$startRuleFunctions = " + startRuleFunctions + ";",
|
|
|
|
|
" var peg$startRuleFunction = " + startRuleFunction + ";"
|
|
|
|
|
" var peg$startRuleFunction = " + startRuleFunction + ";"
|
|
|
|
|
].join("\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -965,12 +965,12 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
parts.push([
|
|
|
|
|
"",
|
|
|
|
|
" var peg$currPos = 0;",
|
|
|
|
|
" var peg$savedPos = 0;",
|
|
|
|
|
" var peg$posDetailsCache = [{ line: 1, column: 1 }];",
|
|
|
|
|
" var peg$maxFailPos = 0;",
|
|
|
|
|
" var peg$maxFailExpected = [];",
|
|
|
|
|
" var peg$silentFails = 0;", // 0 = report failures, > 0 = silence failures
|
|
|
|
|
" var peg$currPos = 0;",
|
|
|
|
|
" var peg$savedPos = 0;",
|
|
|
|
|
" var peg$posDetailsCache = [{ line: 1, column: 1 }];",
|
|
|
|
|
" var peg$maxFailPos = 0;",
|
|
|
|
|
" var peg$maxFailExpected = [];",
|
|
|
|
|
" var peg$silentFails = 0;", // 0 = report failures, > 0 = silence failures
|
|
|
|
|
""
|
|
|
|
|
].join("\n"));
|
|
|
|
|
|
|
|
|
@ -1110,7 +1110,7 @@ function generateJS(ast, options) {
|
|
|
|
|
"",
|
|
|
|
|
" function peg$computeLocation(startPos, endPos) {",
|
|
|
|
|
" var startPosDetails = peg$computePosDetails(startPos);",
|
|
|
|
|
" var endPosDetails = peg$computePosDetails(endPos);",
|
|
|
|
|
" var endPosDetails = peg$computePosDetails(endPos);",
|
|
|
|
|
"",
|
|
|
|
|
" return {",
|
|
|
|
|
" start: {",
|
|
|
|
@ -1237,7 +1237,7 @@ function generateJS(ast, options) {
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
commonjs: function() {
|
|
|
|
|
let parts = [];
|
|
|
|
|
let parts = [];
|
|
|
|
|
let dependencyVars = Object.keys(options.dependencies);
|
|
|
|
|
|
|
|
|
|
parts.push([
|
|
|
|
@ -1270,13 +1270,13 @@ function generateJS(ast, options) {
|
|
|
|
|
|
|
|
|
|
amd: function() {
|
|
|
|
|
let dependencyVars = Object.keys(options.dependencies);
|
|
|
|
|
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
|
|
|
|
|
let dependencies = "["
|
|
|
|
|
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
|
|
|
|
|
let dependencies = "["
|
|
|
|
|
+ dependencyIds.map(
|
|
|
|
|
id => "\"" + js.stringEscape(id) + "\""
|
|
|
|
|
).join(", ")
|
|
|
|
|
+ "]";
|
|
|
|
|
let params = dependencyVars.join(", ");
|
|
|
|
|
let params = dependencyVars.join(", ");
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
generateGeneratedByComment(),
|
|
|
|
@ -1306,18 +1306,18 @@ function generateJS(ast, options) {
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
umd: function() {
|
|
|
|
|
let parts = [];
|
|
|
|
|
let parts = [];
|
|
|
|
|
let dependencyVars = Object.keys(options.dependencies);
|
|
|
|
|
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
|
|
|
|
|
let dependencies = "["
|
|
|
|
|
let dependencyIds = dependencyIds.map(v => options.dependencies[v]);
|
|
|
|
|
let dependencies = "["
|
|
|
|
|
+ dependencyIds.map(
|
|
|
|
|
id => "\"" + js.stringEscape(id) + "\""
|
|
|
|
|
).join(", ")
|
|
|
|
|
+ "]";
|
|
|
|
|
let requires = dependencyIds.map(
|
|
|
|
|
let requires = dependencyIds.map(
|
|
|
|
|
id => "require(\"" + js.stringEscape(id) + "\")"
|
|
|
|
|
).join(", ");
|
|
|
|
|
let params = dependencyVars.join(", ");
|
|
|
|
|
let params = dependencyVars.join(", ");
|
|
|
|
|
|
|
|
|
|
parts.push([
|
|
|
|
|
generateGeneratedByComment(),
|
|
|
|
|