pegjs/spec/unit/compiler/passes/report-left-recursion.spec.js
David Majda 0847a69643 Rename the "PEG" variable to "peg"
So far, PEG.js was exported in a "PEG" global variable when no module
loader was detected. The same variable name was also conventionally used
when requiring it in Node.js or otherwise referring to it. This was
reflected in various places in the code, documentation, examples, etc.

This commit changes the variable name to "peg" and fixes all relevant
occurrences. The main reason for the change is that in Node.js, modules
are generally referred to by lower-case variable names, so "PEG" was
sticking out when used in Node.js projects.
2016-05-04 12:37:13 +02:00

116 lines
3.9 KiB
JavaScript

/* global peg */
"use strict";
describe("compiler pass |reportLeftRecursion|", function() {
var pass = peg.compiler.passes.check.reportLeftRecursion;
it("reports direct left recursion", function() {
expect(pass).toReportError('start = start', {
message: 'Possible left recursion detected (start -> start).',
location: {
start: { offset: 8, line: 1, column: 9 },
end: { offset: 13, line: 1, column: 14 }
}
});
});
it("reports indirect left recursion", function() {
expect(pass).toReportError([
'start = stop',
'stop = start'
].join("\n"), {
message: 'Possible left recursion detected (start -> stop -> start).',
location: {
start: { offset: 21, line: 2, column: 9 },
end: { offset: 26, line: 2, column: 14 }
}
});
});
describe("in sequences", function() {
it("reports left recursion if all preceding elements match empty string", function() {
expect(pass).toReportError('start = "" "" "" start');
});
it("doesn't report left recursion if some preceding element doesn't match empty string", function() {
expect(pass).not.toReportError('start = "a" "" "" start');
expect(pass).not.toReportError('start = "" "a" "" start');
expect(pass).not.toReportError('start = "" "" "a" start');
});
/* Regression test for #359. */
it("reports left recursion when rule reference is wrapped in an expression", function() {
expect(pass).toReportError('start = "" start?');
});
it("computes expressions that always consume input on success correctly", function() {
expect(pass).toReportError([
'start = a start',
'a "a" = ""'
].join('\n'));
expect(pass).not.toReportError([
'start = a start',
'a "a" = "a"'
].join('\n'));
expect(pass).toReportError('start = ("" / "a" / "b") start');
expect(pass).toReportError('start = ("a" / "" / "b") start');
expect(pass).toReportError('start = ("a" / "b" / "") start');
expect(pass).not.toReportError('start = ("a" / "b" / "c") start');
expect(pass).toReportError('start = ("" { }) start');
expect(pass).not.toReportError('start = ("a" { }) start');
expect(pass).toReportError('start = ("" "" "") start');
expect(pass).not.toReportError('start = ("a" "" "") start');
expect(pass).not.toReportError('start = ("" "a" "") start');
expect(pass).not.toReportError('start = ("" "" "a") start');
expect(pass).toReportError('start = a:"" start');
expect(pass).not.toReportError('start = a:"a" start');
expect(pass).toReportError('start = $"" start');
expect(pass).not.toReportError('start = $"a" start');
expect(pass).toReportError('start = &"" start');
expect(pass).toReportError('start = &"a" start');
expect(pass).toReportError('start = !"" start');
expect(pass).toReportError('start = !"a" start');
expect(pass).toReportError('start = ""? start');
expect(pass).toReportError('start = "a"? start');
expect(pass).toReportError('start = ""* start');
expect(pass).toReportError('start = "a"* start');
expect(pass).toReportError('start = ""+ start');
expect(pass).not.toReportError('start = "a"+ start');
expect(pass).toReportError('start = ("") start');
expect(pass).not.toReportError('start = ("a") start');
expect(pass).toReportError('start = &{ } start');
expect(pass).toReportError('start = !{ } start');
expect(pass).toReportError([
'start = a start',
'a = ""'
].join('\n'));
expect(pass).not.toReportError([
'start = a start',
'a = "a"'
].join('\n'));
expect(pass).toReportError('start = "" start');
expect(pass).not.toReportError('start = "a" start');
expect(pass).not.toReportError('start = [a-d] start');
expect(pass).not.toReportError('start = . start');
});
});
});