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