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
238
239
240
241
242
243
|
require "./spec_helper"
require "../src/parcom.cr"
include Parcom
describe Tokens do
describe ".from_string" do
it "constructs a Tokens(Char) from a String" do
tokens = Tokens.from_string("abcd")
tokens.tokens.should eq("abcd".chars)
end
end
describe "#initialize" do
it "wraps an array with the contents of the given iterable" do
set = Set{'a', 'b', 'c', 'd'}
tokens = Tokens.new(set)
tokens.tokens.should eq(set.to_a)
arr = "abcd".chars
tokens = Tokens.new(arr)
tokens.tokens.should eq(arr)
end
end
context do
tokens_empty = Tokens.new([] of Char)
tokens = Tokens.from_string("abcd")
describe "#[]" do
it "returns the token at the given index" do
tokens[2].should eq('c')
expect_raises(IndexError) { tokens_empty[2] }
end
it "returns a new Tokens similar to Array#[](Int, Int)" do
tokens[1, 5].should eq(Tokens.new(['b', 'c', 'd']))
expect_raises(IndexError) { tokens_empty[1, 5] }
end
it "returns a new Tokens similar to Array#[](Range)" do
tokens[1..3].should eq(Tokens.new(['b', 'c', 'd']))
expect_raises(IndexError) { tokens_empty[1..3] }
end
end
describe "#[]?" do
it "analogous to `Array#[]?`" do
# we should only need to check the nil-returning cases
tokens_empty[2]?.should be_nil
tokens_empty[1, 5]?.should be_nil
tokens_empty[1..3]?.should be_nil
end
end
describe "#empty?" do
it "exposes the `#empty?` method of the wrapped array" do
tokens.empty?.should be_false
tokens_empty.empty?.should be_true
end
end
end
end
describe Result do
describe "#map" do
r = Result.new(Tokens.from_string("abcd"), 'x')
r_expected = Result.new(Tokens.from_string("abcd"), 'x'.ord)
it "accepts a proc" do
f = ->(c : Char) { c.ord }
r.map(f).should eq(r_expected)
end
it "accepts a block" do
r.map { |c| c.ord }.should eq(r_expected)
end
end
end
describe Parser do
describe "self.pure" do
v = 'a'
p = Parser(Char, Char).pure(v)
tokens = Tokens.from_string("____")
result = p.parse(tokens)
it "returns a value of whatever it was initialized with" do
result.value.should eq(v)
end
it "does not modify the input" do
result.tokens.should eq(tokens)
end
end
describe "self.flunk" do
p = Parser(Char, Char).flunk
it "always fails" do
expect_raises(ParserFail) { p.parse(Tokens.from_string("arbitrary")) }
end
end
describe "self.any_token" do
p = Parser(Char, Nil).any_token
it "parses the first token in the input stream" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the input stream is empty" do
tokens = Tokens.from_string("")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.eof" do
p = Parser(Char, Nil).eof
it "succeeds with nil if the input stream is empty" do
tokens = Tokens.from_string("")
result = p.parse(tokens)
result.value.should be_nil
result.tokens.empty?.should be_true
end
it "fails if the input stream is not empty" do
tokens = Tokens.from_string("____")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.satisfy" do
p = Parser(Char, Char).satisfy { |c| c == 'a' }
it "succeeds if the token passes the predicate" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the token fails the predicate" do
tokens = Tokens.from_string("bbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "self.token" do
a = Parser(Char, Char).token('a')
it "succeeds with the given token if it is at the start" do
tokens = Tokens.from_string("abc")
result = a.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "fails if the given token is not present" do
tokens = Tokens.from_string("bbc")
expect_raises(ParserFail) { a.parse(tokens) }
end
end
describe "#assert" do
p = Parser(Char, Char).any_token.assert { |c| c == 'a' }
it "succeeds if the parser succeeds and if the predicate passes" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq(tokens[0])
result.tokens.should eq(tokens[1..])
end
it "fails if the predicate fails" do
tokens = Tokens.from_string("bbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
describe "#map" do
p = Parser(Char, Char).any_token
p_mapped = p.map { |c| c.ord }
tokens = Tokens.from_string("abcd")
normal_result = p.parse(tokens)
mapped_result = p_mapped.parse(tokens)
it "parses the same amount from the input" do
mapped_result.tokens.should eq(normal_result.tokens)
end
it "changes the value of the result" do
mapped_result.value.should eq('a'.ord)
end
end
describe "#|" do
a = Parser(Char, Char).token('a')
b = Parser(Char, Char).token('b')
p = a | b
it "succeeds with the first value if both parsers succeed" do
p2 = a | Parser(Char, Char).pure('z')
tokens = Tokens.from_string("abcd")
result = p2.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "succeeds with the first value if only the first parser succeeds" do
tokens = Tokens.from_string("abcd")
result = p.parse(tokens)
result.value.should eq('a')
result.tokens.should eq(tokens[1..])
end
it "succeeds with the second value if only the second parser succeeds" do
tokens = Tokens.from_string("bbcd")
result = p.parse(tokens)
result.value.should eq('b')
result.tokens.should eq(tokens[1..])
end
it "fails if both parsers fail" do
tokens = Tokens.from_string("cbcd")
expect_raises(ParserFail) { p.parse(tokens) }
end
end
end
|