Added AndPredicate AST node.

This does not speed up the benchmark suite execution statistically significantly
on V8.

Detailed results (benchmark suite totals):

---------------------------------
 Test #     Before       After
---------------------------------
      1   28.72 kB/s   28.84 kB/s
      2   28.84 kB/s   28.76 kB/s
      3   28.83 kB/s   28.72 kB/s
      4   28.81 kB/s   28.84 kB/s
      5   28.76 kB/s   28.82 kB/s
---------------------------------
Average   28.79 kB/s   28.80 kB/s
---------------------------------

Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2
redux
David Majda 15 years ago
parent c3c1c79665
commit e5df8284b5

@ -202,6 +202,10 @@ PEG.Grammar.ZeroOrMore = function(element) { this._element = element; };
PEG.Grammar.OneOrMore = function(element) { this._element = element; };
PEG.Grammar.AndPredicate = function(expression) {
this._expression = expression;
};
PEG.Grammar.NotPredicate = function(expression) {
this._expression = expression;
};
@ -245,6 +249,9 @@ PEG.Grammar.extendNodes("checkReferencedRulesExist", {
OneOrMore:
function(grammar) { this._element.checkReferencedRulesExist(grammar); },
AndPredicate:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
NotPredicate:
function(grammar) { this._expression.checkReferencedRulesExist(grammar); },
@ -298,6 +305,11 @@ PEG.Grammar.extendNodes("checkNoLeftRecursion", {
this._element.checkNoLeftRecursion(grammar, appliedRules);
},
AndPredicate:
function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules);
},
NotPredicate:
function(grammar, appliedRules) {
this._expression.checkNoLeftRecursion(grammar, appliedRules);
@ -917,6 +929,33 @@ PEG.Grammar.NotPredicate.prototype.compile = function(resultVar) {
);
};
PEG.Grammar.AndPredicate.prototype.compile = function(resultVar) {
var savedPosVar = PEG.Compiler.generateUniqueIdentifier("savedPos");
var savedReportMatchFailuresVar = PEG.Compiler.generateUniqueIdentifier("savedReportMatchFailuresVar");
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result");
return PEG.Compiler.formatCode(
"var ${savedPosVar} = this._pos;",
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
"context.reportMatchFailures = false;",
"${expressionCode}",
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
"if (${expressionResultVar} !== null) {",
" var ${resultVar} = '';",
" this._pos = ${savedPosVar};",
"} else {",
" var ${resultVar} = null;",
"}",
{
expressionCode: this._expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar,
savedPosVar: savedPosVar,
savedReportMatchFailuresVar: savedReportMatchFailuresVar,
resultVar: resultVar
}
);
};
PEG.Grammar.RuleRef.prototype.compile = function(resultVar) {
return PEG.Compiler.formatCode(
"var ${resultVar} = this.${ruleMethod}(context);",

@ -357,9 +357,7 @@ PEG.grammarParser = (function(){
this._pos = savedPos6;
}
var result36 = result37 !== null
? (function() {
return new PEG.Grammar.NotPredicate(new PEG.Grammar.NotPredicate((arguments[1])));
}).apply(this, result37)
? (function() { return new PEG.Grammar.AndPredicate((arguments[1])); }).apply(this, result37)
: null;
if (result36 !== null) {
var result30 = result36;

@ -29,9 +29,7 @@ sequence
/ prefixed* { return $1.length != 1 ? new PEG.Grammar.Sequence($1) : $1[0]; }
prefixed
: and suffixed {
return new PEG.Grammar.NotPredicate(new PEG.Grammar.NotPredicate($2));
}
: and suffixed { return new PEG.Grammar.AndPredicate($2); }
/ not suffixed { return new PEG.Grammar.NotPredicate($2); }
/ suffixed

@ -193,8 +193,8 @@ test("buildParser reports missing referenced rules", function() {
'start: "a" / "b" / missing',
'start: missing*',
'start: missing+',
'start: !missing',
'start: &missing',
'start: !missing',
'start: missing { }'
];
@ -216,8 +216,8 @@ test("buildParser reports left recursion", function() {
'start: "a" / "b" / start',
'start: start*',
'start: start+',
'start: !start',
'start: &start',
'start: !start',
'start: start { }',
/* Indirect */

@ -137,10 +137,7 @@ with (PEG.Grammar) {
/* Canonical prefixed is "!\"abcd\"". */
test("parses prefixed", function() {
grammarParserParses(
'start: &"abcd"?',
oneRuleGrammar(new NotPredicate(new NotPredicate(choice)))
);
grammarParserParses('start: &"abcd"?', oneRuleGrammar(new AndPredicate(choice)));
grammarParserParses('start: !"abcd"?', oneRuleGrammar(new NotPredicate(choice)));
grammarParserParses('start: "abcd"?', oneRuleGrammar(choice));
});

Loading…
Cancel
Save