Behavior specs cleanup: Improve semantic predicate specs

Note that use of |text| inside semantic predicate code is no longer
tested and officially supported.
redux
David Majda 10 years ago
parent 3d9600b81b
commit e9d038547d

@ -415,202 +415,186 @@ describe("generated parser behavior", function() {
}); });
describe("positive semantic predicate", function() { describe("positive semantic predicate", function() {
it("causes successful match by returning |true|", function() { describe("when the code returns a truthy value", function() {
var parser = PEG.buildParser('start = &{ return true; }', options); it("returns |undefined|", function() {
var parser = PEG.buildParser('start = &{ return true; }', options);
expect(parser).toParse("", undefined);
});
it("causes match failure by returning |false|", function() {
var parser = PEG.buildParser('start = &{ return false; }', options);
expect(parser).toFailToParse("");
});
it("can use label variables", function() {
var parser = PEG.buildParser(
'start = a:"a" &{ return a === "a"; }',
options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", undefined);
});
}); });
it("can use the |text| function", function() { describe("when the code returns a falsey value", function() {
var parser = PEG.buildParser( it("reports match failure", function() {
'start = "a" &{ return text() === ""; }', var parser = PEG.buildParser('start = &{ return false; }', options);
options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toFailToParse("");
});
}); });
it("can use the |offset| function to get the current parse position", function() { describe("label variables", function() {
var parser = PEG.buildParser( it("can access label variables from preceding labeled elements in a sequence", function() {
'start = "a" &{ return offset() === 1; }', var parser = PEG.buildParser(
options 'start = a:"a" b:"b" c:"c" &{ return a === "a" && b === "b" && c === "c"; }',
); options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("abc");
});
}); });
it("can use the |line| and |column| functions to get the current line and column", function() { describe("initializer variables & functions", function() {
var parser = PEG.buildParser([ it("can access variables defined in the initializer", function() {
'{ var result; }', var parser = PEG.buildParser([
'start = line (nl+ line)* { return result; }', '{ var v = 42 }',
'line = thing (" "+ thing)*', 'start = &{ return v === 42; }'
'thing = digit / mark', ].join("\n"), options);
'digit = [0-9]',
'mark = &{ result = [line(), column()]; return true; } "x"',
'nl = ("\\r" / "\\n" / "\\u2028" / "\\u2029")'
].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); expect(parser).toParse("");
});
/* Non-Unix newlines */ it("can access functions defined in the initializer", function() {
expect(parser).toParse("1\rx", [2, 1]); // Old Mac var parser = PEG.buildParser([
expect(parser).toParse("1\r\nx", [2, 1]); // Windows '{ function f() { return 42; } }',
expect(parser).toParse("1\n\rx", [3, 1]); // mismatched 'start = &{ return f() === 42; }'
].join("\n"), options);
/* Strange newlines */ expect(parser).toParse("");
expect(parser).toParse("1\u2028x", [2, 1]); // line separator });
expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator
}); });
it("can use variables defined in the initializer", function() { describe("available variables & functions", function() {
var parser = PEG.buildParser([ it("|parser| contains the parser object", function() {
'{ var v = 42 }', var parser = PEG.buildParser([
'start = "a" &{ return v === 42; }' '{ var result; }',
].join("\n"), options); 'start = &{ result = parser; return true; } { return result; }'
].join("\n"), options);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", parser);
}); });
it("can use functions defined in the initializer", function() { it("|options| contains options", function() {
var parser = PEG.buildParser([ var parser = PEG.buildParser([
'{ function f() { return 42; } }', '{ var result; }',
'start = "a" &{ return f() === 42; }' 'start = &{ result = options; return true; } { return result; }'
].join("\n"), options); ].join("\n"), options);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", { a: 42 }, { a: 42 });
}); });
it("can use the |parser| variable to access the parser object", function() { it("|offset|, |line|, and |column| return current parse position, line, and column", function() {
var parser = PEG.buildParser([ var parser = PEG.buildParser([
'{ var result; }', '{ var result; }',
'start = "a" &{ result = parser; return true; } { return result; }' 'start = line (nl+ line)* { return result; }',
].join("\n"), options); 'line = thing (" "+ thing)*',
'thing = digit / mark',
'digit = [0-9]',
'mark = &{ result = [offset(), line(), column()]; return true; } "x"',
'nl = [\\r"\\n\\u2028\\u2029]'
].join("\n"), options);
expect(parser).toParse("a", parser); expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [13, 7, 5]);
});
it("can use options passed to the parser", function() { /* Non-Unix newlines */
var parser = PEG.buildParser([ expect(parser).toParse("1\rx", [2, 2, 1]); // Old Mac
'{ var result; }', expect(parser).toParse("1\r\nx", [3, 2, 1]); // Windows
'start = "a" &{ result = options; return true; } { return result; }' expect(parser).toParse("1\n\rx", [3, 3, 1]); // mismatched
].join("\n"), options);
expect(parser).toParse("a", { a: 42 }, { a: 42 }); /* Strange newlines */
expect(parser).toParse("1\u2028x", [2, 2, 1]); // line separator
expect(parser).toParse("1\u2029x", [2, 2, 1]); // paragraph separator
});
}); });
}); });
describe("negative semantic predicate", function() { describe("negative semantic predicate", function() {
it("causes successful match by returning |false|", function() { describe("when the code returns a falsey value", function() {
var parser = PEG.buildParser('start = !{ return false; }', options); it("returns |undefined|", function() {
var parser = PEG.buildParser('start = !{ return false; }', options);
expect(parser).toParse("", undefined);
});
it("causes match failure by returning |true|", function() {
var parser = PEG.buildParser('start = !{ return true; }', options);
expect(parser).toFailToParse();
});
it("can use label variables", function() {
var parser = PEG.buildParser(
'start = a:"a" !{ return a !== "a"; }',
options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", undefined);
});
}); });
it("can use the |text| function", function() { describe("when the code returns a truthy value", function() {
var parser = PEG.buildParser( it("reports match failure", function() {
'start = "a" !{ return text() !== ""; }', var parser = PEG.buildParser('start = !{ return true; }', options);
options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toFailToParse("");
});
}); });
it("can use the |offset| function to get the current parse position", function() { describe("label variables", function() {
var parser = PEG.buildParser( it("can access label variables from preceding labeled elements in a sequence", function() {
'start = "a" !{ return offset() !== 1; }', var parser = PEG.buildParser(
options 'start = a:"a" b:"b" c:"c" !{ return a !== "a" || b !== "b" || c !== "c"; }',
); options
);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("abc");
});
}); });
it("can use the |line| and |column| functions to get the current line and column", function() { describe("initializer variables & functions", function() {
var parser = PEG.buildParser([ it("can access variables defined in the initializer", function() {
'{ var result; }', var parser = PEG.buildParser([
'start = line (nl+ line)* { return result; }', '{ var v = 42 }',
'line = thing (" "+ thing)*', 'start = !{ return v !== 42; }'
'thing = digit / mark', ].join("\n"), options);
'digit = [0-9]',
'mark = !{ result = [line(), column()]; return false; } "x"',
'nl = ("\\r" / "\\n" / "\\u2028" / "\\u2029")'
].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); expect(parser).toParse("");
});
/* Non-Unix newlines */ it("can access functions defined in the initializer", function() {
expect(parser).toParse("1\rx", [2, 1]); // Old Mac var parser = PEG.buildParser([
expect(parser).toParse("1\r\nx", [2, 1]); // Windows '{ function f() { return 42; } }',
expect(parser).toParse("1\n\rx", [3, 1]); // mismatched 'start = !{ return f() !== 42; }'
].join("\n"), options);
/* Strange newlines */ expect(parser).toParse("");
expect(parser).toParse("1\u2028x", [2, 1]); // line separator });
expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator
}); });
it("can use variables defined in the initializer", function() { describe("available variables & functions", function() {
var parser = PEG.buildParser([ it("|parser| contains the parser object", function() {
'{ var v = 42 }', var parser = PEG.buildParser([
'start = "a" !{ return v !== 42; }' '{ var result; }',
].join("\n"), options); 'start = !{ result = parser; return false; } { return result; }'
].join("\n"), options);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", parser);
}); });
it("can use functions defined in the initializer", function() { it("|options| contains options", function() {
var parser = PEG.buildParser([ var parser = PEG.buildParser([
'{ function f() { return 42; } }', '{ var result; }',
'start = "a" !{ return f() !== 42; }' 'start = !{ result = options; return false; } { return result; }'
].join("\n"), options); ].join("\n"), options);
expect(parser).toParse("a", ["a", undefined]); expect(parser).toParse("", { a: 42 }, { a: 42 });
}); });
it("can use the |parser| variable to access the parser object", function() { it("|offset|, |line|, and |column| return current parse position, line, and column", function() {
var parser = PEG.buildParser([ var parser = PEG.buildParser([
'{ var result; }', '{ var result; }',
'start = "a" !{ result = parser; return false; } { return result; }' 'start = line (nl+ line)* { return result; }',
].join("\n"), options); 'line = thing (" "+ thing)*',
'thing = digit / mark',
'digit = [0-9]',
'mark = !{ result = [offset(), line(), column()]; return false; } "x"',
'nl = [\\r"\\n\\u2028\\u2029]'
].join("\n"), options);
expect(parser).toParse("a", parser); expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [13, 7, 5]);
});
it("can use options passed to the parser", function() { /* Non-Unix newlines */
var parser = PEG.buildParser([ expect(parser).toParse("1\rx", [2, 2, 1]); // Old Mac
'{ var result; }', expect(parser).toParse("1\r\nx", [3, 2, 1]); // Windows
'start = "a" !{ result = options; return false; } { return result; }' expect(parser).toParse("1\n\rx", [3, 3, 1]); // mismatched
].join("\n"), options);
expect(parser).toParse("a", { a: 42 }, { a: 42 }); /* Strange newlines */
expect(parser).toParse("1\u2028x", [2, 2, 1]); // line separator
expect(parser).toParse("1\u2029x", [2, 2, 1]); // paragraph separator
});
}); });
}); });

Loading…
Cancel
Save