You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pegjs/test/unit/compiler/passes/report-infinite-recursion.s...

120 lines
4.1 KiB
JavaScript

"use strict";
let chai = require("chai");
let helpers = require("./helpers");
let pass = require("../../../../lib/compiler/passes/report-infinite-recursion");
chai.use(helpers);
let expect = chai.expect;
describe("compiler pass |reportInfiniteRecursion|", function() {
it("reports direct left recursion", function() {
expect(pass).to.reportError("start = start", {
message: "Possible infinite loop when parsing (left recursion: 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).to.reportError([
"start = stop",
"stop = start"
].join("\n"), {
message: "Possible infinite loop when parsing (left recursion: start -> stop -> start).",
location: {
start: { offset: 20, line: 2, column: 8 },
end: { offset: 25, line: 2, column: 13 }
}
});
});
describe("in sequences", function() {
it("reports left recursion if all preceding elements match empty string", function() {
expect(pass).to.reportError("start = '' '' '' start");
});
it("doesn't report left recursion if some preceding element doesn't match empty string", function() {
expect(pass).to.not.reportError("start = 'a' '' '' start");
expect(pass).to.not.reportError("start = '' 'a' '' start");
expect(pass).to.not.reportError("start = '' '' 'a' start");
});
// Regression test for #359.
it("reports left recursion when rule reference is wrapped in an expression", function() {
expect(pass).to.reportError("start = '' start?");
});
it("computes expressions that always consume input on success correctly", function() {
expect(pass).to.reportError([
"start = a start",
"a 'a' = ''"
].join("\n"));
expect(pass).to.not.reportError([
"start = a start",
"a 'a' = 'a'"
].join("\n"));
expect(pass).to.reportError("start = ('' / 'a' / 'b') start");
expect(pass).to.reportError("start = ('a' / '' / 'b') start");
expect(pass).to.reportError("start = ('a' / 'b' / '') start");
expect(pass).to.not.reportError("start = ('a' / 'b' / 'c') start");
expect(pass).to.reportError("start = ('' { }) start");
expect(pass).to.not.reportError("start = ('a' { }) start");
expect(pass).to.reportError("start = ('' '' '') start");
expect(pass).to.not.reportError("start = ('a' '' '') start");
expect(pass).to.not.reportError("start = ('' 'a' '') start");
expect(pass).to.not.reportError("start = ('' '' 'a') start");
expect(pass).to.reportError("start = a:'' start");
expect(pass).to.not.reportError("start = a:'a' start");
expect(pass).to.reportError("start = $'' start");
expect(pass).to.not.reportError("start = $'a' start");
expect(pass).to.reportError("start = &'' start");
expect(pass).to.reportError("start = &'a' start");
expect(pass).to.reportError("start = !'' start");
expect(pass).to.reportError("start = !'a' start");
expect(pass).to.reportError("start = ''? start");
expect(pass).to.reportError("start = 'a'? start");
expect(pass).to.reportError("start = ''* start");
expect(pass).to.reportError("start = 'a'* start");
expect(pass).to.reportError("start = ''+ start");
expect(pass).to.not.reportError("start = 'a'+ start");
expect(pass).to.reportError("start = ('') start");
expect(pass).to.not.reportError("start = ('a') start");
expect(pass).to.reportError("start = &{ } start");
expect(pass).to.reportError("start = !{ } start");
expect(pass).to.reportError([
"start = a start",
"a = ''"
].join("\n"));
expect(pass).to.not.reportError([
"start = a start",
"a = 'a'"
].join("\n"));
expect(pass).to.reportError("start = '' start");
expect(pass).to.not.reportError("start = 'a' start");
expect(pass).to.not.reportError("start = [a-d] start");
expect(pass).to.not.reportError("start = . start");
});
});
});