Debian Codegraph @

spec/unit/compiler/passes/report-infinite-recursion.spec.js

raw symbols SCIP
1 /* global peg */
2
3 "use strict";
4
5 describe("compiler pass |reportInfiniteRecursion|", function() {
6 var pass = peg.compiler.passes.check.reportInfiniteRecursion;
7
8 it("reports direct left recursion", function() {
9 expect(pass).toReportError('start = start', {
10 message: 'Possible infinite loop when parsing (left recursion: start -> start).',
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 indirect left recursion", function() {
19 expect(pass).toReportError([
20 'start = stop',
21 'stop = start'
22 ].join("\n"), {
23 message: 'Possible infinite loop when parsing (left recursion: start -> stop -> start).',
24 location: {
25 start: { offset: 21, line: 2, column: 9 },
26 end: { offset: 26, line: 2, column: 14 }
27 }
28 });
29 });
30
31 describe("in sequences", function() {
32 it("reports left recursion if all preceding elements match empty string", function() {
33 expect(pass).toReportError('start = "" "" "" start');
34 });
35
36 it("doesn't report left recursion if some preceding element doesn't match empty string", function() {
37 expect(pass).not.toReportError('start = "a" "" "" start');
38 expect(pass).not.toReportError('start = "" "a" "" start');
39 expect(pass).not.toReportError('start = "" "" "a" start');
40 });
41
42 /* Regression test for #359. */
43 it("reports left recursion when rule reference is wrapped in an expression", function() {
44 expect(pass).toReportError('start = "" start?');
45 });
46
47 it("computes expressions that always consume input on success correctly", function() {
48 expect(pass).toReportError([
49 'start = a start',
50 'a "a" = ""'
51 ].join('\n'));
52 expect(pass).not.toReportError([
53 'start = a start',
54 'a "a" = "a"'
55 ].join('\n'));
56
57 expect(pass).toReportError('start = ("" / "a" / "b") start');
58 expect(pass).toReportError('start = ("a" / "" / "b") start');
59 expect(pass).toReportError('start = ("a" / "b" / "") start');
60 expect(pass).not.toReportError('start = ("a" / "b" / "c") start');
61
62 expect(pass).toReportError('start = ("" { }) start');
63 expect(pass).not.toReportError('start = ("a" { }) start');
64
65 expect(pass).toReportError('start = ("" "" "") start');
66 expect(pass).not.toReportError('start = ("a" "" "") start');
67 expect(pass).not.toReportError('start = ("" "a" "") start');
68 expect(pass).not.toReportError('start = ("" "" "a") start');
69
70 expect(pass).toReportError('start = a:"" start');
71 expect(pass).not.toReportError('start = a:"a" start');
72
73 expect(pass).toReportError('start = $"" start');
74 expect(pass).not.toReportError('start = $"a" start');
75
76 expect(pass).toReportError('start = &"" start');
77 expect(pass).toReportError('start = &"a" start');
78
79 expect(pass).toReportError('start = !"" start');
80 expect(pass).toReportError('start = !"a" start');
81
82 expect(pass).toReportError('start = ""? start');
83 expect(pass).toReportError('start = "a"? start');
84
85 expect(pass).toReportError('start = ""* start');
86 expect(pass).toReportError('start = "a"* start');
87
88 expect(pass).toReportError('start = ""+ start');
89 expect(pass).not.toReportError('start = "a"+ start');
90
91 expect(pass).toReportError('start = ("") start');
92 expect(pass).not.toReportError('start = ("a") start');
93
94 expect(pass).toReportError('start = &{ } start');
95
96 expect(pass).toReportError('start = !{ } start');
97
98 expect(pass).toReportError([
99 'start = a start',
100 'a = ""'
101 ].join('\n'));
102 expect(pass).not.toReportError([
103 'start = a start',
104 'a = "a"'
105 ].join('\n'));
106
107 expect(pass).toReportError('start = "" start');
108 expect(pass).not.toReportError('start = "a" start');
109
110 expect(pass).not.toReportError('start = [a-d] start');
111
112 expect(pass).not.toReportError('start = . start');
113 });
114 });
115 });