| 1 |
|
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
describe("compiler pass |reportInfiniteRepetition|", function() { |
| 6 |
var pass = peg.compiler.passes.check.reportInfiniteRepetition; |
| 7 |
|
| 8 |
it("reports infinite loops for zero_or_more", function() { |
| 9 |
expect(pass).toReportError('start = ("")*', { |
| 10 |
message: "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).", |
| 11 |
location: { |
| 12 |
start: { offset: 8, line: 1, column: 9 }, |
| 13 |
end: { offset: 13, line: 1, column: 14 } |
| 14 |
} |
| 15 |
}); |
| 16 |
}); |
| 17 |
|
| 18 |
it("reports infinite loops for one_or_more", function() { |
| 19 |
expect(pass).toReportError('start = ("")+', { |
| 20 |
message: "Possible infinite loop when parsing (repetition used with an expression that may not consume any input).", |
| 21 |
location: { |
| 22 |
start: { offset: 8, line: 1, column: 9 }, |
| 23 |
end: { offset: 13, line: 1, column: 14 } |
| 24 |
} |
| 25 |
}); |
| 26 |
}); |
| 27 |
|
| 28 |
it("computes expressions that always consume input on success correctly", function() { |
| 29 |
expect(pass).toReportError([ |
| 30 |
'start = a*', |
| 31 |
'a "a" = ""' |
| 32 |
].join('\n')); |
| 33 |
expect(pass).not.toReportError([ |
| 34 |
'start = a*', |
| 35 |
'a "a" = "a"' |
| 36 |
].join('\n')); |
| 37 |
|
| 38 |
expect(pass).toReportError('start = ("" / "a" / "b")*'); |
| 39 |
expect(pass).toReportError('start = ("a" / "" / "b")*'); |
| 40 |
expect(pass).toReportError('start = ("a" / "b" / "")*'); |
| 41 |
expect(pass).not.toReportError('start = ("a" / "b" / "c")*'); |
| 42 |
|
| 43 |
expect(pass).toReportError('start = ("" { })*'); |
| 44 |
expect(pass).not.toReportError('start = ("a" { })*'); |
| 45 |
|
| 46 |
expect(pass).toReportError('start = ("" "" "")*'); |
| 47 |
expect(pass).not.toReportError('start = ("a" "" "")*'); |
| 48 |
expect(pass).not.toReportError('start = ("" "a" "")*'); |
| 49 |
expect(pass).not.toReportError('start = ("" "" "a")*'); |
| 50 |
|
| 51 |
expect(pass).toReportError('start = (a:"")*'); |
| 52 |
expect(pass).not.toReportError('start = (a:"a")*'); |
| 53 |
|
| 54 |
expect(pass).toReportError('start = ($"")*'); |
| 55 |
expect(pass).not.toReportError('start = ($"a")*'); |
| 56 |
|
| 57 |
expect(pass).toReportError('start = (&"")*'); |
| 58 |
expect(pass).toReportError('start = (&"a")*'); |
| 59 |
|
| 60 |
expect(pass).toReportError('start = (!"")*'); |
| 61 |
expect(pass).toReportError('start = (!"a")*'); |
| 62 |
|
| 63 |
expect(pass).toReportError('start = (""?)*'); |
| 64 |
expect(pass).toReportError('start = ("a"?)*'); |
| 65 |
|
| 66 |
expect(pass).toReportError('start = (""*)*'); |
| 67 |
expect(pass).toReportError('start = ("a"*)*'); |
| 68 |
|
| 69 |
expect(pass).toReportError('start = (""+)*'); |
| 70 |
expect(pass).not.toReportError('start = ("a"+)*'); |
| 71 |
|
| 72 |
expect(pass).toReportError('start = ("")*'); |
| 73 |
expect(pass).not.toReportError('start = ("a")*'); |
| 74 |
|
| 75 |
expect(pass).toReportError('start = (&{ })*'); |
| 76 |
|
| 77 |
expect(pass).toReportError('start = (!{ })*'); |
| 78 |
|
| 79 |
expect(pass).toReportError([ |
| 80 |
'start = a*', |
| 81 |
'a = ""' |
| 82 |
].join('\n')); |
| 83 |
expect(pass).not.toReportError([ |
| 84 |
'start = a*', |
| 85 |
'a = "a"' |
| 86 |
].join('\n')); |
| 87 |
|
| 88 |
expect(pass).toReportError('start = ""*'); |
| 89 |
expect(pass).not.toReportError('start = "a"*'); |
| 90 |
|
| 91 |
expect(pass).not.toReportError('start = [a-d]*'); |
| 92 |
|
| 93 |
expect(pass).not.toReportError('start = .*'); |
| 94 |
}); |
| 95 |
}); |