2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
let chai = require("chai");
|
|
|
|
let helpers = require("./helpers");
|
2016-12-16 13:21:48 +01:00
|
|
|
let pass = require("../../../../lib/compiler/passes/report-infinite-repetition");
|
2016-09-08 13:29:06 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
chai.use(helpers);
|
|
|
|
|
|
|
|
let expect = chai.expect;
|
|
|
|
|
2016-07-29 18:06:16 +02:00
|
|
|
describe("compiler pass |reportInfiniteRepetition|", function() {
|
2015-04-01 12:16:27 +02:00
|
|
|
it("reports infinite loops for zero_or_more", function() {
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('')*", {
|
2016-09-22 05:25:09 +02:00
|
|
|
message: "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
2015-04-06 10:34:07 +02:00
|
|
|
location: {
|
2016-09-22 05:25:09 +02:00
|
|
|
start: { offset: 8, line: 1, column: 9 },
|
|
|
|
end: { offset: 13, line: 1, column: 14 }
|
2015-04-06 10:34:07 +02:00
|
|
|
}
|
2015-04-01 12:16:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("reports infinite loops for one_or_more", function() {
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('')+", {
|
2016-09-22 05:25:09 +02:00
|
|
|
message: "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
2015-04-06 10:34:07 +02:00
|
|
|
location: {
|
2016-09-22 05:25:09 +02:00
|
|
|
start: { offset: 8, line: 1, column: 9 },
|
|
|
|
end: { offset: 13, line: 1, column: 14 }
|
2015-04-06 10:34:07 +02:00
|
|
|
}
|
2015-04-01 12:16:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-04 17:35:37 +02:00
|
|
|
it("computes expressions that always consume input on success correctly", function() {
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError([
|
2016-09-21 15:06:56 +02:00
|
|
|
"start = a*",
|
|
|
|
"a 'a' = ''"
|
|
|
|
].join("\n"));
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.not.reportError([
|
2016-09-21 15:06:56 +02:00
|
|
|
"start = a*",
|
|
|
|
"a 'a' = 'a'"
|
|
|
|
].join("\n"));
|
2015-08-03 16:56:57 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('' / 'a' / 'b')*");
|
|
|
|
expect(pass).to.reportError("start = ('a' / '' / 'b')*");
|
|
|
|
expect(pass).to.reportError("start = ('a' / 'b' / '')*");
|
|
|
|
expect(pass).to.not.reportError("start = ('a' / 'b' / 'c')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('' { })*");
|
|
|
|
expect(pass).to.not.reportError("start = ('a' { })*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('' '' '')*");
|
|
|
|
expect(pass).to.not.reportError("start = ('a' '' '')*");
|
|
|
|
expect(pass).to.not.reportError("start = ('' 'a' '')*");
|
|
|
|
expect(pass).to.not.reportError("start = ('' '' 'a')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (a:'')*");
|
|
|
|
expect(pass).to.not.reportError("start = (a:'a')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ($'')*");
|
|
|
|
expect(pass).to.not.reportError("start = ($'a')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (&'')*");
|
|
|
|
expect(pass).to.reportError("start = (&'a')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (!'')*");
|
|
|
|
expect(pass).to.reportError("start = (!'a')*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (''?)*");
|
|
|
|
expect(pass).to.reportError("start = ('a'?)*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (''*)*");
|
|
|
|
expect(pass).to.reportError("start = ('a'*)*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (''+)*");
|
|
|
|
expect(pass).to.not.reportError("start = ('a'+)*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ('')*");
|
|
|
|
expect(pass).to.not.reportError("start = ('a')*");
|
2016-03-11 16:18:29 +01:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (&{ })*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = (!{ })*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError([
|
2016-09-21 15:06:56 +02:00
|
|
|
"start = a*",
|
|
|
|
"a = ''"
|
|
|
|
].join("\n"));
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.not.reportError([
|
2016-09-21 15:06:56 +02:00
|
|
|
"start = a*",
|
|
|
|
"a = 'a'"
|
|
|
|
].join("\n"));
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.reportError("start = ''*");
|
|
|
|
expect(pass).to.not.reportError("start = 'a'*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.not.reportError("start = [a-d]*");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-12-08 08:59:04 +01:00
|
|
|
expect(pass).to.not.reportError("start = .*");
|
2015-04-01 12:16:27 +02:00
|
|
|
});
|
|
|
|
});
|