Added Optional 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.84 kB/s   28.75 kB/s
      2   28.76 kB/s   28.69 kB/s
      3   28.72 kB/s   28.69 kB/s
      4   28.84 kB/s   28.93 kB/s
      5   28.82 kB/s   28.70 kB/s
---------------------------------
Average   28.80 kB/s   28.75 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 e5df8284b5
commit b2f230fad2

@ -198,6 +198,8 @@ PEG.Grammar.Choice = function(alternatives) {
this._alternatives = alternatives;
};
PEG.Grammar.Optional = function(element) { this._element = element; };
PEG.Grammar.ZeroOrMore = function(element) { this._element = element; };
PEG.Grammar.OneOrMore = function(element) { this._element = element; };
@ -243,6 +245,9 @@ PEG.Grammar.extendNodes("checkReferencedRulesExist", {
});
},
Optional:
function(grammar) { this._element.checkReferencedRulesExist(grammar); },
ZeroOrMore:
function(grammar) { this._element.checkReferencedRulesExist(grammar); },
@ -295,6 +300,11 @@ PEG.Grammar.extendNodes("checkNoLeftRecursion", {
});
},
Optional:
function(grammar, appliedRules) {
this._element.checkNoLeftRecursion(grammar, appliedRules);
},
ZeroOrMore:
function(grammar, appliedRules) {
this._element.checkNoLeftRecursion(grammar, appliedRules);
@ -862,6 +872,20 @@ PEG.Grammar.Choice.prototype.compile = function(resultVar) {
return code;
};
PEG.Grammar.Optional.prototype.compile = function(resultVar) {
var elementResultVar = PEG.Compiler.generateUniqueIdentifier("result");
return PEG.Compiler.formatCode(
"${elementCode}",
"var ${resultVar} = ${elementResultVar} !== null ? ${elementResultVar} : '';",
{
elementCode: this._element.compile(elementResultVar),
elementResultVar: elementResultVar,
resultVar: resultVar
}
);
};
PEG.Grammar.ZeroOrMore.prototype.compile = function(resultVar) {
var elementResultVar = PEG.Compiler.generateUniqueIdentifier("result");

File diff suppressed because it is too large Load Diff

@ -34,11 +34,9 @@ prefixed
/ suffixed
suffixed
: primary question {
return new PEG.Grammar.Choice([$1, new PEG.Grammar.Literal("")]);
}
/ primary star { return new PEG.Grammar.ZeroOrMore($1); }
/ primary plus { return new PEG.Grammar.OneOrMore($1); }
: primary question { return new PEG.Grammar.Optional($1); }
/ primary star { return new PEG.Grammar.ZeroOrMore($1); }
/ primary plus { return new PEG.Grammar.OneOrMore($1); }
/ primary
primary

@ -191,6 +191,7 @@ test("buildParser reports missing referenced rules", function() {
'start: "a" "b" missing',
'start: missing / "a" / "b"',
'start: "a" / "b" / missing',
'start: missing?',
'start: missing*',
'start: missing+',
'start: &missing',
@ -214,6 +215,7 @@ test("buildParser reports left recursion", function() {
'start: start "a" "b"',
'start: start / "a" / "b"',
'start: "a" / "b" / start',
'start: start?',
'start: start*',
'start: start+',
'start: &start',

@ -17,12 +17,11 @@ global.grammarParserDoesNotParse = function(input) {
module("Grammar Parser");
with (PEG.Grammar) {
var literalEmpty = new Literal("");
var literalAbcd = new Literal("abcd");
var literalEfgh = new Literal("efgh");
var literalIjkl = new Literal("ijkl");
var choice = new Choice([literalAbcd, literalEmpty]);
var choice = new Optional(literalAbcd);
var notAbcd = new NotPredicate(literalAbcd);
var notEfgh = new NotPredicate(literalEfgh);

Loading…
Cancel
Save