-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.roc
234 lines (172 loc) · 5.26 KB
/
Parser.roc
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
interface Parser
exposes
[ Parser, Res, State
, initial, repeated, validate
, singleVal, fixedVals, remainingVals
, fixedStr, remainingStr
, fixedIgnore, remainingIgnore
, singleWord
, uint, int
]
imports [ ListZip ]
# Types
State : ListZip.Zip
Res a : { val : a, state : State }
Parser a : List I64, State -> Res a
# Building Blocks
initial : List I64 -> State
initial = \input ->
ListZip.newAtFirst input 0
repeated : Parser a -> Parser (List a)
repeated = \parser ->
(\input, state ->
repeatedHelper input state parser [])
repeatedHelper : List I64, State, Parser a, List a -> Res (List a)
repeatedHelper = \input, state, parser, val ->
if ListZip.afterLast state then
{ val, state }
else if state.val == 10 then
newState = ListZip.forward state input
repeatedHelper input newState parser val
else
res = parser input state
newVal = List.join [ val, [ res.val ] ]
repeatedHelper input res.state parser newVal
validate : Parser a, List I64 -> Result a {}
validate = \parser, input ->
init = initial input
res = parser input init
if ListZip.afterLast res.state then
Ok res.val
else
Err {}
# Collect unmodified values
singleVal : Parser I64
singleVal =
(\input, state ->
newState = ListZip.forward state input
{ val: state.val, state: newState }
)
fixedVals : I64 -> Parser (List I64)
fixedVals = \len ->
(\input, state ->
fixedValsHelper input state len [])
fixedValsHelper : List I64, State, I64, List I64 -> Res (List I64)
fixedValsHelper = \input, state, len, val ->
if len > 0 then
newState = ListZip.forward state input
newLen = len - 1
newVal = List.append val state.val
fixedValsHelper input newState newLen newVal
else
{ val, state }
remainingVals : Parser (List I64)
remainingVals =
(\input, state ->
remainingValsHelper input state [])
remainingValsHelper : List I64, State, List I64 -> Res (List I64)
remainingValsHelper = \input, state, val ->
if ListZip.afterLast state then
{ val, state }
else
newState = ListZip.forward state input
if state.val == 10 then
{ val, state: newState }
else
newVal = List.append val state.val
remainingValsHelper input newState newVal
# Parse Strings as Hashes
fixedStr : I64 -> Parser I64
fixedStr = \len ->
(\input, state ->
fixedStrHelper input state len 5381)
fixedStrHelper : List I64, State, I64, I64 -> Res I64
fixedStrHelper = \input, state, len, val ->
if len > 0 then
newState = ListZip.forward state input
newLen = len - 1
newVal = val * 33 + state.val
fixedStrHelper input newState newLen newVal
else
{ val, state }
remainingStr : Parser I64
remainingStr =
(\input, state ->
remainingStrHelper input state 5381)
remainingStrHelper : List I64, State, I64 -> Res I64
remainingStrHelper = \input, state, val ->
if ListZip.afterLast state then
{ val, state }
else
newState = ListZip.forward state input
if state.val == 10 then
{ val, state: newState }
else
newVal = val * 33 + state.val
remainingStrHelper input newState newVal
# Skipping Parsers
fixedIgnore : I64 -> Parser I64
fixedIgnore = \len ->
(\input, state ->
newState = ListZip.move state input len
{ val: 0, state: newState }
)
remainingIgnore : Parser I64
remainingIgnore =
(\input, state ->
remainingIgnoreHelper input state)
remainingIgnoreHelper : List I64, State -> Res I64
remainingIgnoreHelper = \input, state ->
if ListZip.afterLast state then
{ val: 0, state }
else
newState = ListZip.forward state input
if state.val == 10 then
{ val: 0, state: newState }
else
remainingIgnoreHelper input newState
# Space-delimited Words
singleWord : Parser (List I64)
singleWord =
(\input, state ->
singleWordHelper input state [])
singleWordHelper : List I64, State, List I64 -> Res (List I64)
singleWordHelper = \input, state, val ->
if ListZip.afterLast state then
{ val, state }
else
newState = ListZip.forward state input
if isWhitespace state.val then
{ val, state: newState }
else
newVal = List.append val state.val
singleWordHelper input newState newVal
isWhitespace : I64 -> Bool
isWhitespace = \val ->
when val is
10 -> True
32 -> True
_ -> False
# Integer Parsers
uint : Parser I64
uint =
(\input, state ->
uintHelper input state 0)
uintHelper : List I64, State, I64 -> Res I64
uintHelper = \input, state, val ->
if 48 <= state.val && state.val <= 57 then
newState = ListZip.forward state input
newVal = val * 10 + state.val - 48
uintHelper input newState newVal
else
{ val, state }
int : Parser I64
int =
(\input, state ->
if state.val == 45 then
newState = ListZip.forward state input
res = uint input newState
{ res & val: -res.val }
else
uint input state
)