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
|
module AST.ASM where
import AST.AST
import AST.Span (Span)
import AST.Token
type SpanTokens = [(Span, Token)]
type ASM a = SpanTokens -> (Either String a, SpanTokens)
returnP :: a -> ASM a
returnP x ts = (Right x, ts)
failP :: String -> ASM a
failP s ts = (Left s, ts)
(>+=) :: ASM a -> (a -> ASM b) -> ASM b
m >+= f = \ts -> case m ts of
(Left s, ts') -> (Left s, ts')
(Right a, ts') -> f a ts'
(>+) :: ASM a -> ASM b -> ASM b
m >+ m' = m >+= \ _ -> m'
(<$>) :: (a -> b) -> ASM a -> ASM b
f <$> m = m >+= \x -> returnP (f x)
(<*>) :: ASM (a -> b) -> ASM a -> ASM b
mf <*> mx = mf >+= \f -> mx >+= \x -> returnP (f x)
mapM :: (a -> ASM b) -> [a] -> ASM [b]
mapM _ [] = returnP []
mapM f (x:xs) = f x >+= \ y ->
mapM f xs >+= \ ys ->
returnP (y : ys)
readSpan :: ASM Span
readSpan ts@((p, _) : _) = returnP p ts
readSpan [] = failP "Token stream is empty." []
readToken :: ASM Token
readToken ts@((_, t) : _) = returnP t ts
readToken [] = failP "Token stream is empty." []
ensure :: Show a => Bool -> a -> ASM a
ensure b x = if b
then returnP x
else failP $ "Function `ensure` failed (AST.ASM). "
++ "This should have been returned: " ++ show x
tokenSpan :: Token -> ASM Span
tokenSpan t = getTokenSpan >+= \(p, t') -> ensure (t == t') p
getTokenSpan :: ASM (Span, Token)
getTokenSpan (t : ts) = returnP t ts
getTokenSpan [] = failP "Token stream is empty." []
tokenSpanOneOf :: Token -> Token -> ASM Span
tokenSpanOneOf t1 t2 ((p,t') : ts)
| t1 == t' = returnP p ts
| t2 == t' = returnP p ts
| otherwise = failP ("expected " ++ show t1 ++ " or "
++ show t2 ++ ", got " ++ show t') ts
tokenSpanOneOf _ _ [] = failP "Token stream is empty." []
sepBy :: (a -> ASM b) -> ASM Span -> [a] -> ASM ([b], [Span])
sepBy f s ys = case ys of
[] -> returnP ([], [])
[x] -> f x >+= \ px -> returnP ([px], [])
(x:xs) -> f x >+= \ px ->
s >+= \ ps ->
sepBy f s xs >+= \ (pxs, pss) ->
returnP (px : pxs, ps : pss)
between :: ASM a -> ASM b -> ASM c -> ASM (a, b, c)
between open f close =
open >+= \ po ->
f >+= \ pf ->
close >+= \ pc ->
returnP (po, pf, pc)
parens :: ASM a -> ASM (Span, a, Span)
parens f = between (tokenSpan LeftParen) f (tokenSpan RightParen)
brackets :: ASM a -> ASM (Span, a, Span)
brackets f = between (tokenSpan LeftBracket) f (tokenSpan RightBracket)
braces :: ASM a -> ASM (Span, a, Span)
braces f = between (tokenSpan LeftBrace) f (tokenSpan RightBrace)
optional :: (a -> ASM b) -> Maybe a -> ASM (Maybe b)
optional _ Nothing = returnP Nothing
optional f (Just x) = Just <$> f x
maybeOneOf :: Token -> Token -> ASM (Maybe Span)
maybeOneOf t1 t2 ts@((p,t) : ts')
| t1 == t = returnP (Just p) ts'
| t2 == t = returnP (Just p) ts'
| otherwise = returnP Nothing ts
maybeOneOf _ _ [] = failP "Token stream is empty." []
maybeTokenSpan :: Token -> ASM (Maybe Span)
maybeTokenSpan t ts@((p,t') : ts')
| t == t' = returnP (Just p) ts'
| otherwise = returnP Nothing ts
maybeTokenSpan _ [] = failP "Token stream is empty." []
maybeEnclosedBy :: [(Token, Token)] -> ASM a
-> ASM ((Maybe Span), a, (Maybe Span))
maybeEnclosedBy [] f = f >+= \ pf -> returnP (Nothing, pf, Nothing)
maybeEnclosedBy ((o, c):ocs) f =
readToken >+= \t -> if (t == o)
then between (maybeTokenSpan o) f (maybeTokenSpan c)
else maybeEnclosedBy ocs f
maybeParens :: ASM a -> ASM ((Maybe Span), a, (Maybe Span))
maybeParens f = maybeEnclosedBy [(LeftParen, RightParen)] f
choose :: ASM a -> ASM a -> ASM a
choose f g = \ts -> case f ts of
(Left _, _) -> g ts
res -> res
|