This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
forked from j-mie6/parsley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinatorTests.scala
237 lines (218 loc) · 7.9 KB
/
CombinatorTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package org.http4s
import parsley.{Failure, Success, Var, runParser}
import parsley.Combinator._
import parsley.Parsley._
import parsley.Implicits.{charLift, stringLift}
class CombinatorTests extends ParsleyTest
{
"choice" should "fail if given the empty list" in
{
runParser(choice(), "") shouldBe a [Failure]
}
it should "behave like p for List(p)" in
{
runParser(choice('a'), "") should equal (runParser('a', ""))
runParser(choice('a'), "a") should equal (runParser('a', "a"))
}
it should "parse in order" in
{
runParser(choice("a", "b", "bc", "bcd"), "bcd") should be (Success("b"))
}
it should "fail if none of the parsers succeed" in
{
runParser(choice("a", "b", "bc", "bcd"), "c") shouldBe a [Failure]
}
"repeat" should "be pure(Nil) for n <= 0" in
{
runParser(repeat(0, 'a'), "a") should be (Success(Nil))
runParser(repeat(-1, 'a'), "a") should be (Success(Nil))
}
it should "parse n times for n > 0" in
{
for (n <- 0 to 100) runParser(repeat(n, 'a'), "a"*n) should be (Success(("a" * n).toList))
}
it must "fail if n inputs are not present" in
{
runParser(repeat(2, 'a'), "a") shouldBe a [Failure]
}
"option" should "succeed with Some if p succeeds" in
{
runParser(option(pure(7)), "") should be (Success(Some(7)))
}
it should "succeed with None if p fails without consumption" in
{
runParser(option('a'), "") should be (Success(None))
}
it should "fail if p fails with consumption" in
{
runParser(option("ab"), "a") shouldBe a [Failure]
}
"decide" must "succeed for Some" in
{
runParser(decide('a' <#> (Option(_))), "a") should be (Success('a'))
}
it must "fail for None" in
{
runParser(decide(pure(None)), "") shouldBe a [Failure]
}
it must "compose with option to become identity" in
{
runParser(decide(option(pure(7))), "") should be (runParser(pure(7), ""))
runParser(decide(option('a')), "") should be (runParser('a', ""))
runParser(decide(option("ab")), "a") should be (runParser("ab", "a"))
}
"optional" must "succeed if p succeeds" in
{
runParser(optional('a'), "a") should be (Success(()))
}
it must "also succeed if p fails without consumption" in
{
runParser(optional('a'), "b") should be (Success(()))
}
it must "fail if p failed with consumption" in
{
runParser(optional("ab"), "a") shouldBe a [Failure]
}
"manyN" must "ensure that n are parsed" in
{
for (n <- 0 to 10) runParser(manyN(n, 'a'), "a"*n) should be (Success(("a"*n).toList))
for (n <- 0 to 10) runParser(manyN(n+1, 'a'), "a"*n) shouldBe a [Failure]
}
it should "not care if more are present" in
{
for (n <- 0 to 10) runParser(manyN(n/2, 'a'), "a"*n) should be (Success(("a"*n).toList))
}
"skipManyN" must "ensure that n are parsed" in
{
for (n <- 0 to 10) runParser(skipManyN(n, 'a'), "a"*n) should be (Success(()))
for (n <- 0 to 10) runParser(skipManyN(n+1, 'a'), "a"*n) shouldBe a [Failure]
}
it should "not care if more are present" in
{
for (n <- 0 to 10) runParser(skipManyN(n/2, 'a'), "a"*n) should be (Success(()))
}
"sepBy" must "accept empty input" in
{
runParser(sepBy('a', 'b'), "") should be (Success(Nil))
}
it must "not allow sep at the end of chain" in
{
runParser(sepBy('a', 'b'), "ab") shouldBe a [Failure]
}
it should "be able to parse 2 or more p" in
{
runParser(sepBy('a', 'b'), "aba") should be (Success(List('a', 'a')))
runParser(sepBy('a', 'b'), "ababa") should be (Success(List('a', 'a', 'a')))
runParser(sepBy('a', 'b'), "abababa") should be (Success(List('a', 'a', 'a', 'a')))
}
"sepBy1" must "require a p" in
{
runParser(sepBy1('a', 'b'), "a") should not be a [Failure]
runParser(sepBy1('a', 'b'), input = "") shouldBe a [Failure]
}
"sepEndBy" must "accept empty input" in
{
runParser(sepEndBy('a', 'b'), "") should be (Success(Nil))
}
it should "not require sep at the end of chain" in
{
runParser(sepEndBy('a', 'b'), "a") should be (Success(List('a')))
}
it should "be able to parse 2 or more p" in
{
runParser(sepEndBy('a', 'b'), "aba") should be (Success(List('a', 'a')))
runParser(sepEndBy('a', 'b'), "ababa") should be (Success(List('a', 'a', 'a')))
}
it should "be able to parse a final sep" in
{
runParser(sepEndBy('a', 'b'), "ab") should be (Success(List('a')))
runParser(sepEndBy('a', 'b'), "abab") should be (Success(List('a', 'a')))
runParser(sepEndBy('a', 'b'), "ababab") should be (Success(List('a', 'a', 'a')))
}
it should "fail if p fails after consuming input" in
{
runParser(sepEndBy("aa", 'b'), "ab") shouldBe a [Failure]
}
it should "fail if sep fails after consuming input" in
{
runParser(sepEndBy('a', "bb"), "ab") shouldBe a [Failure]
}
it must "not corrupt the stack on sep hard-fail" in
{
runParser('c' <::> attempt(sepEndBy('a', "bb")).getOrElse(List('d')), "cab") should be (Success(List('c', 'd')))
}
"sepEndBy1" must "require a p" in
{
runParser(sepEndBy1('a', 'b'), "a") should not be a [Failure]
runParser(sepEndBy1('a', 'b'), input = "") shouldBe a [Failure]
}
"endBy" must "accept empty input" in
{
runParser(endBy('a', 'b'), "") should be (Success(Nil))
}
it must "require sep at end of chain" in
{
runParser(endBy('a', 'b'), "a") shouldBe a [Failure]
runParser(endBy('a', 'b'), "ab") should be (Success(List('a')))
}
it should "be able to parse 2 or more p" in
{
runParser(endBy('a', 'b'), "abab") should be (Success(List('a', 'a')))
runParser(endBy('a', 'b'), "ababab") should be (Success(List('a', 'a', 'a')))
}
"endBy1" must "require a p" in
{
runParser(endBy1('a', 'b'), "ab") should not be a [Failure]
runParser(endBy1('a', 'b'), input = "") shouldBe a [Failure]
}
"eof" must "succeed at the end of input" in
{
runParser(eof, "") should not be a [Failure]
}
it must "fail if input remains" in
{
runParser(eof, "a") shouldBe a [Failure]
}
"notFollowedBy" must "succeed if p fails" in
{
runParser(notFollowedBy('a'), "") should be (Success(()))
}
it must "succeed even if p consumed input" in
{
runParser(notFollowedBy("aa"), "a") should be (Success(()))
}
it must "fail if p succeeds" in
{
runParser(notFollowedBy('a'), "a") shouldBe a [Failure]
}
"manyUntil" must "require an end" in
{
runParser(manyUntil('a', 'b'), "aa") shouldBe a [Failure]
runParser(manyUntil('a', 'b'), "ab") should be (Success(List('a')))
}
it should "parse the end without result" in
{
runParser(manyUntil('a', 'b'), "b") should be (Success(Nil))
}
it should "parse p until that end is found" in
{
runParser(manyUntil('a', 'b'), "aaaaaaaaaaaab") should not be a [Failure]
runParser(manyUntil("aa", 'b'), "ab") shouldBe a [Failure]
}
"someUntil" must "parse at least 1 p" in
{
runParser(someUntil('a', 'b'), "ab") should be (Success(List('a')))
runParser(someUntil('a', 'b'), "b") shouldBe a [Failure]
}
"forP" should "be able to parse context-sensitive grammars" in
{
val v1 = Var(0)
val v2 = Var(1)
val abc = put(v1, 0) *>
many('a' *> modify[Int](v1, _ + 1)) *>
forP[Int](v2, get[Int](v1), pure(_ != 0), pure(_ - 1), 'b') *>
forP[Int](v2, get[Int](v1), pure(_ != 0), pure(_ - 1), 'c')
runParser(abc, "aaabbbccc") should be (Success(()))
runParser(abc, "aaaabc") shouldBe a [Failure]
}
}