Avoid passing |context| in the generated parser
Passing the context is not necessary, global variable is good enough (passing the context would make more sense if each AST node was translated into a function call, but this isn't the case). The performance gain is very small, on the border of statstical error. Results of benchmark with 100 runs on V8: Before: 31.49 kB/s After: 31.57 kB/s Speedup: 0.254 % Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Safari/533.4
This commit is contained in:
parent
a42b957573
commit
7b03f164b8
|
@ -114,6 +114,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
" */",
|
" */",
|
||||||
" parse: function(input) {",
|
" parse: function(input) {",
|
||||||
" var pos = 0;",
|
" var pos = 0;",
|
||||||
|
" var reportMatchFailures = true;",
|
||||||
" var rightmostMatchFailuresPos = 0;",
|
" var rightmostMatchFailuresPos = 0;",
|
||||||
" var rightmostMatchFailuresExpected = [];",
|
" var rightmostMatchFailuresExpected = [];",
|
||||||
" var cache = {};",
|
" var cache = {};",
|
||||||
|
@ -225,7 +226,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
" ",
|
" ",
|
||||||
" ${initializerCode}",
|
" ${initializerCode}",
|
||||||
" ",
|
" ",
|
||||||
" var result = parse_${startRule}({ reportMatchFailures: true });",
|
" var result = parse_${startRule}();",
|
||||||
" ",
|
" ",
|
||||||
" /*",
|
" /*",
|
||||||
" * The parser is now in one of the following three states:",
|
" * The parser is now in one of the following three states:",
|
||||||
|
@ -306,14 +307,14 @@ PEG.compiler.emitter = function(ast) {
|
||||||
|
|
||||||
if (node.displayName !== null) {
|
if (node.displayName !== null) {
|
||||||
var setReportMatchFailuresCode = formatCode(
|
var setReportMatchFailuresCode = formatCode(
|
||||||
"var savedReportMatchFailures = context.reportMatchFailures;",
|
"var savedReportMatchFailures = reportMatchFailures;",
|
||||||
"context.reportMatchFailures = false;"
|
"reportMatchFailures = false;"
|
||||||
);
|
);
|
||||||
var restoreReportMatchFailuresCode = formatCode(
|
var restoreReportMatchFailuresCode = formatCode(
|
||||||
"context.reportMatchFailures = savedReportMatchFailures;"
|
"reportMatchFailures = savedReportMatchFailures;"
|
||||||
);
|
);
|
||||||
var reportMatchFailureCode = formatCode(
|
var reportMatchFailureCode = formatCode(
|
||||||
"if (context.reportMatchFailures && ${resultVar} === null) {",
|
"if (reportMatchFailures && ${resultVar} === null) {",
|
||||||
" matchFailed(${displayName|string});",
|
" matchFailed(${displayName|string});",
|
||||||
"}",
|
"}",
|
||||||
{
|
{
|
||||||
|
@ -328,7 +329,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatCode(
|
return formatCode(
|
||||||
"function parse_${name}(context) {",
|
"function parse_${name}() {",
|
||||||
" var cacheKey = ${name|string} + '@' + pos;",
|
" var cacheKey = ${name|string} + '@' + pos;",
|
||||||
" var cachedResult = cache[cacheKey];",
|
" var cachedResult = cache[cacheKey];",
|
||||||
" if (cachedResult) {",
|
" if (cachedResult) {",
|
||||||
|
@ -456,10 +457,10 @@ PEG.compiler.emitter = function(ast) {
|
||||||
|
|
||||||
return formatCode(
|
return formatCode(
|
||||||
"var ${savedPosVar} = pos;",
|
"var ${savedPosVar} = pos;",
|
||||||
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
|
"var ${savedReportMatchFailuresVar} = reportMatchFailures;",
|
||||||
"context.reportMatchFailures = false;",
|
"reportMatchFailures = false;",
|
||||||
"${expressionCode}",
|
"${expressionCode}",
|
||||||
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
|
"reportMatchFailures = ${savedReportMatchFailuresVar};",
|
||||||
"if (${expressionResultVar} !== null) {",
|
"if (${expressionResultVar} !== null) {",
|
||||||
" var ${resultVar} = '';",
|
" var ${resultVar} = '';",
|
||||||
" pos = ${savedPosVar};",
|
" pos = ${savedPosVar};",
|
||||||
|
@ -483,10 +484,10 @@ PEG.compiler.emitter = function(ast) {
|
||||||
|
|
||||||
return formatCode(
|
return formatCode(
|
||||||
"var ${savedPosVar} = pos;",
|
"var ${savedPosVar} = pos;",
|
||||||
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
|
"var ${savedReportMatchFailuresVar} = reportMatchFailures;",
|
||||||
"context.reportMatchFailures = false;",
|
"reportMatchFailures = false;",
|
||||||
"${expressionCode}",
|
"${expressionCode}",
|
||||||
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
|
"reportMatchFailures = ${savedReportMatchFailuresVar};",
|
||||||
"if (${expressionResultVar} === null) {",
|
"if (${expressionResultVar} === null) {",
|
||||||
" var ${resultVar} = '';",
|
" var ${resultVar} = '';",
|
||||||
"} else {",
|
"} else {",
|
||||||
|
@ -631,7 +632,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
|
|
||||||
rule_ref: function(node, resultVar) {
|
rule_ref: function(node, resultVar) {
|
||||||
return formatCode(
|
return formatCode(
|
||||||
"var ${resultVar} = ${ruleMethod}(context);",
|
"var ${resultVar} = ${ruleMethod}();",
|
||||||
{
|
{
|
||||||
ruleMethod: "parse_" + node.name,
|
ruleMethod: "parse_" + node.name,
|
||||||
resultVar: resultVar
|
resultVar: resultVar
|
||||||
|
@ -646,7 +647,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
" pos += ${length};",
|
" pos += ${length};",
|
||||||
"} else {",
|
"} else {",
|
||||||
" var ${resultVar} = null;",
|
" var ${resultVar} = null;",
|
||||||
" if (context.reportMatchFailures) {",
|
" if (reportMatchFailures) {",
|
||||||
" matchFailed(quote(${value|string}));",
|
" matchFailed(quote(${value|string}));",
|
||||||
" }",
|
" }",
|
||||||
"}",
|
"}",
|
||||||
|
@ -665,7 +666,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
" pos++;",
|
" pos++;",
|
||||||
"} else {",
|
"} else {",
|
||||||
" var ${resultVar} = null;",
|
" var ${resultVar} = null;",
|
||||||
" if (context.reportMatchFailures) {",
|
" if (reportMatchFailures) {",
|
||||||
" matchFailed('any character');",
|
" matchFailed('any character');",
|
||||||
" }",
|
" }",
|
||||||
"}",
|
"}",
|
||||||
|
@ -699,7 +700,7 @@ PEG.compiler.emitter = function(ast) {
|
||||||
" pos++;",
|
" pos++;",
|
||||||
"} else {",
|
"} else {",
|
||||||
" var ${resultVar} = null;",
|
" var ${resultVar} = null;",
|
||||||
" if (context.reportMatchFailures) {",
|
" if (reportMatchFailures) {",
|
||||||
" matchFailed(${rawText|string});",
|
" matchFailed(${rawText|string});",
|
||||||
" }",
|
" }",
|
||||||
"}",
|
"}",
|
||||||
|
|
633
src/parser.js
633
src/parser.js
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue