/
Parser.purs
244 lines (205 loc) · 6.42 KB
/
Parser.purs
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
244
module Bookhound.Parser
( Parser
, ParseResult(..)
, ParseError(..)
, Input
, parse
, runParser
, errorParser
, andThen
, exactly
, isMatch
, check
, anyOf
, allOf
, char
, withTransform
, withError
, withErrorN
, except
) where
import Bookhound.FatPrelude
import Data.Set as Set
type Input = String
newtype Parser a = P
{ parse :: Input -> ParseResult a
, transform :: Transform
, errors :: Set (Int /\ ParseError)
}
type Transform = forall b. Maybe (Parser b -> Parser b)
data ParseResult a
= Result Input a
| Error ParseError
derive instance Eq a => Eq (ParseResult a)
data ParseError
= UnexpectedEof
| ExpectedEof Input
| UnexpectedChar Char
| UnexpectedString String
| NoMatch String
| ErrorAt String
derive instance Eq ParseError
derive instance Ord ParseError
instance (Show a) => Show (ParseResult a) where
show (Result i a) =
"Pending: " <> " >" <> i <> "< " <> "\n\nResult: \n" <> show a
show (Error err) = show err
instance Show ParseError where
show (UnexpectedEof) = "Unexpected end of stream"
show (ExpectedEof i) =
"Expected end of stream, but got " <> ">" <> i <> "<"
show (UnexpectedChar c) = "Unexpected char: " <> "[" <> show c <> "]"
show (UnexpectedString s) = "Unexpected string: " <> "[" <> s <> "]"
show (NoMatch s) = "Did not match condition: " <> s
show (ErrorAt s) = "Error at " <> s
instance Functor ParseResult where
map f (Result i a) = Result i (f a)
map _ (Error pe) = Error pe
instance Functor Parser where
map f (P { parse: p, transform: (t :: Transform), errors: e }) =
applyTransformError t e $ mkParser (map f <<< p)
instance Apply Parser where
apply
(P { parse: p, transform: (t :: Transform), errors: e })
(P { parse: p', transform: (t' :: Transform), errors: e' }) =
applyTransformsErrors [ t, t' ] [ e, e' ] $ mkParser
( \x -> case p x of
Error pe -> Error pe
Result i f -> case p' i of
Error pe -> Error pe
Result i' a -> Result i' (f a)
)
instance Applicative Parser where
pure a = mkParser (_ `Result` a)
instance Bind Parser where
bind (P { parse: p, transform: (t :: Transform), errors: e }) f =
applyTransformError t e $ mkParser
( \x -> case p x of
Result i a -> parse (f a) i
Error pe -> Error pe
)
parse :: forall a. Parser a -> Input -> ParseResult a
parse (P x) = x.parse
runParser :: forall a. Parser a -> Input -> Either (Array ParseError) a
runParser (p@(P { errors: e })) i =
toEither $ parse (exactly p) i
where
toEither = case _ of
Result _ a -> Right a
Error pe -> Left $ filter hasPriorityError [ pe ]
<> (snd <$> reverse (Set.toUnfoldable e))
<> filter (not <<< hasPriorityError) [ pe ]
hasPriorityError :: ParseError -> Boolean
hasPriorityError (ErrorAt _) = true
hasPriorityError _ = false
errorParser :: forall a. ParseError -> Parser a
errorParser = mkParser <<< const <<< Error
andThen :: forall a. Parser String -> Parser a -> Parser a
andThen p1 (p2@(P { transform: (t :: Transform), errors: e })) =
applyTransformError t e $
mkParser (\i -> parse p2 $ fromRight i $ runParser p1 i)
char :: Parser Char
char = mkParser
$ maybe (Error UnexpectedEof)
(\x -> Result (fromCharArray x.tail) x.head)
<<< uncons
<<< toCharArray
exactly :: forall a. Parser a -> Parser a
exactly (P { parse: p, transform: (t :: Transform), errors: e }) =
applyTransformError t e $ mkParser
( \x -> case p x of
result@(Result i _) | i == mempty -> result
Result i _ -> Error $ ExpectedEof i
err -> err
)
anyOf :: forall a. Array (Parser a) -> Parser a
anyOf ps = anyOfHelper (toUnfoldable ps) Nothing mempty
allOf :: forall a. Array (Parser a) -> Parser a
allOf ps = allOfHelper (toUnfoldable ps) Nothing mempty
anyOfHelper
:: forall a
. List (Parser a)
-> Transform
-> Set (Int /\ ParseError)
-> Parser a
anyOfHelper Nil _ _ = errorParser $ NoMatch "anyOf"
anyOfHelper (p : Nil) _ _ = p
anyOfHelper
(P { parse: p, transform: (t :: Transform), errors: e } : rest)
t'
e' =
applyTransformsErrors [ t, t' ] [ e, e' ] $
mkParser
( \x -> case p x of
Error _ -> parse (anyOfHelper rest t e) x
result -> result
)
allOfHelper
:: forall a
. List (Parser a)
-> Transform
-> Set (Int /\ ParseError)
-> Parser a
allOfHelper Nil _ _ = errorParser $ NoMatch "allOf"
allOfHelper (p : Nil) _ _ = p
allOfHelper
(P { parse: p, transform: (t :: Transform), errors: e } : rest)
t'
e' =
applyTransformsErrors [ t, t' ] [ e, e' ] $ mkParser
( \x -> case p x of
Result _ _ -> parse (allOfHelper rest t e) x
err -> err
)
isMatch :: (Char -> Char -> Boolean) -> Parser Char -> Char -> Parser Char
isMatch cond parser c1 = do
c2 <- parser
if cond c1 c2 then
pure c2
else
errorParser $ UnexpectedChar c2
check :: forall a. String -> (a -> Boolean) -> Parser a -> Parser a
check condName cond parser = do
c2 <- parser
if cond c2 then
pure c2
else
errorParser $ NoMatch condName
except :: forall a. Parser a -> Parser a -> Parser a
except
(P { parse: p, transform: (t :: Transform), errors: e })
(P { parse: p' }) =
applyTransformError t e $ mkParser
( \x -> case p' x of
Result _ _ -> Error $ NoMatch "except"
Error _ -> p x
)
withError :: forall a. String -> Parser a -> Parser a
withError = withErrorN 0
withErrorN :: forall a. Int -> String -> Parser a -> Parser a
withErrorN n str = applyError <<< Set.singleton $ (n /\ ErrorAt str)
withTransform :: forall a. (forall b. Parser b -> Parser b) -> Parser a -> Parser a
withTransform t = applyTransform (Just t)
applyTransformsErrors
:: forall a
. (forall b. Array (Maybe (Parser b -> Parser b)))
-> Array (Set (Int /\ ParseError))
-> Parser a
-> Parser a
applyTransformsErrors ts es = applyTransformError (findJust ts) (fold es)
applyTransformError
:: forall a
. Transform
-> Set (Int /\ ParseError)
-> Parser a
-> Parser a
applyTransformError t e = applyTransform t <<< applyError e
applyTransform :: forall a. Transform -> Parser a -> Parser a
applyTransform f p =
maybe p (\f' -> P ((extract $ f' p) { transform = f })) f
where
extract (P x) = x
applyError :: forall a. Set (Int /\ ParseError) -> Parser a -> Parser a
applyError e (P x) = P (x { errors = e <> x.errors })
mkParser :: forall a. (Input -> ParseResult a) -> Parser a
mkParser p = P { parse: p, transform: Nothing, errors: mempty }