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
redux
David Majda 14 years ago
parent a42b957573
commit 7b03f164b8

@ -114,6 +114,7 @@ PEG.compiler.emitter = function(ast) {
" */",
" parse: function(input) {",
" var pos = 0;",
" var reportMatchFailures = true;",
" var rightmostMatchFailuresPos = 0;",
" var rightmostMatchFailuresExpected = [];",
" var cache = {};",
@ -225,7 +226,7 @@ PEG.compiler.emitter = function(ast) {
" ",
" ${initializerCode}",
" ",
" var result = parse_${startRule}({ reportMatchFailures: true });",
" var result = parse_${startRule}();",
" ",
" /*",
" * The parser is now in one of the following three states:",
@ -306,14 +307,14 @@ PEG.compiler.emitter = function(ast) {
if (node.displayName !== null) {
var setReportMatchFailuresCode = formatCode(
"var savedReportMatchFailures = context.reportMatchFailures;",
"context.reportMatchFailures = false;"
"var savedReportMatchFailures = reportMatchFailures;",
"reportMatchFailures = false;"
);
var restoreReportMatchFailuresCode = formatCode(
"context.reportMatchFailures = savedReportMatchFailures;"
"reportMatchFailures = savedReportMatchFailures;"
);
var reportMatchFailureCode = formatCode(
"if (context.reportMatchFailures && ${resultVar} === null) {",
"if (reportMatchFailures && ${resultVar} === null) {",
" matchFailed(${displayName|string});",
"}",
{
@ -328,7 +329,7 @@ PEG.compiler.emitter = function(ast) {
}
return formatCode(
"function parse_${name}(context) {",
"function parse_${name}() {",
" var cacheKey = ${name|string} + '@' + pos;",
" var cachedResult = cache[cacheKey];",
" if (cachedResult) {",
@ -456,10 +457,10 @@ PEG.compiler.emitter = function(ast) {
return formatCode(
"var ${savedPosVar} = pos;",
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
"context.reportMatchFailures = false;",
"var ${savedReportMatchFailuresVar} = reportMatchFailures;",
"reportMatchFailures = false;",
"${expressionCode}",
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
"reportMatchFailures = ${savedReportMatchFailuresVar};",
"if (${expressionResultVar} !== null) {",
" var ${resultVar} = '';",
" pos = ${savedPosVar};",
@ -483,10 +484,10 @@ PEG.compiler.emitter = function(ast) {
return formatCode(
"var ${savedPosVar} = pos;",
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
"context.reportMatchFailures = false;",
"var ${savedReportMatchFailuresVar} = reportMatchFailures;",
"reportMatchFailures = false;",
"${expressionCode}",
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
"reportMatchFailures = ${savedReportMatchFailuresVar};",
"if (${expressionResultVar} === null) {",
" var ${resultVar} = '';",
"} else {",
@ -631,7 +632,7 @@ PEG.compiler.emitter = function(ast) {
rule_ref: function(node, resultVar) {
return formatCode(
"var ${resultVar} = ${ruleMethod}(context);",
"var ${resultVar} = ${ruleMethod}();",
{
ruleMethod: "parse_" + node.name,
resultVar: resultVar
@ -646,7 +647,7 @@ PEG.compiler.emitter = function(ast) {
" pos += ${length};",
"} else {",
" var ${resultVar} = null;",
" if (context.reportMatchFailures) {",
" if (reportMatchFailures) {",
" matchFailed(quote(${value|string}));",
" }",
"}",
@ -665,7 +666,7 @@ PEG.compiler.emitter = function(ast) {
" pos++;",
"} else {",
" var ${resultVar} = null;",
" if (context.reportMatchFailures) {",
" if (reportMatchFailures) {",
" matchFailed('any character');",
" }",
"}",
@ -699,7 +700,7 @@ PEG.compiler.emitter = function(ast) {
" pos++;",
"} else {",
" var ${resultVar} = null;",
" if (context.reportMatchFailures) {",
" if (reportMatchFailures) {",
" matchFailed(${rawText|string});",
" }",
"}",

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save