-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.hs
334 lines (291 loc) · 8.17 KB
/
Parser.hs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
module Parser (parseString) where
import Prelude hiding (pi)
import Control.Applicative hiding (many, some)
import Control.Monad
import Data.Char
import Data.Void
import Data.Foldable
import Data.Set (Set)
import System.Exit
import Text.Megaparsec
import Data.IORef
import qualified Data.Set as Set
import qualified Text.Megaparsec.Char as C
import qualified Text.Megaparsec.Char.Lexer as L
import Common
import Presyntax
import ElabState
--------------------------------------------------------------------------------
type Parser = Parsec Void String
ws :: Parser ()
ws = L.space C.space1 (L.skipLineComment "--") (L.skipBlockComment "{-" "-}")
withPos :: Parser Tm -> Parser Tm
withPos p = do
pos <- getSourcePos
a <- p
case a of
SrcPos{} -> pure a
_ -> pure $ SrcPos pos a
lexeme = L.lexeme ws
symbol s = lexeme (C.string s)
char c = lexeme (C.char c)
parens p = char '(' *> p <* char ')'
braces p = char '{' *> p <* char '}'
arrow = symbol "→" <|> symbol "->"
bind = ident <|> symbol "_"
natural :: Parser Integer
natural = lexeme $ do
ds <- takeWhile1P Nothing isDigit
pure $! foldl' (\acc d -> 10*acc + fromIntegral (digitToInt d)) 0 ds
keywords :: Set String
keywords = Set.fromList [
"Code"
, "Eff"
, "Nat"
, "NatElim"
, "Ref"
, "read"
, "U"
, "do"
, "let"
, "new"
, "Rec"
, "readℕ"
, "readNat"
, "printℕ"
, "printNat"
, "log"
, "return"
, "suc"
, "write"
, "zero"
, "Σ"
, "λ"
, "ℕ"
, "ℕElim"
, "open"
]
stringLiteral :: Parser String
stringLiteral = char '"' >> manyTill L.charLiteral (char '"')
isIdentRestChar :: Char -> Bool
isIdentRestChar c = c == '\'' || c == '-' || isAlphaNum c
ident :: Parser Name
ident = try $ do
x <- (:) <$> C.letterChar <*> takeWhileP Nothing isIdentRestChar
guard (Set.notMember x keywords)
x <$ ws
keyword :: String -> Parser ()
keyword kw = try $ do
C.string kw
(satisfy isIdentRestChar *> empty) <|> ws
recTy :: Parser [(Name, Tm)]
recTy = do
keyword "Σ" <|> keyword "Rec"
char '('
res <- sepBy ((,) <$> ident <*> (char ':' *> lamLet)) (char ',')
char ')'
pure res
atom :: Parser Tm
atom =
withPos ( (Var <$> ident)
<|> (NatLit <$> natural)
<|> (U <$ char 'U')
<|> (Rec [] <$ try (char '(' *> char ')'))
<|> (Box <$ ((() <$ char '□') <|> keyword "Code"))
<|> (Eff <$ (keyword "Eff" ))
<|> (Return <$ (keyword "return" ))
<|> (Ref <$ (keyword "Ref" ))
<|> (New <$ (keyword "new" ))
<|> (Write <$ (keyword "write" ))
<|> (Read <$ (keyword "read" ))
<|> (ReadNat <$ (keyword "readNat" <|> keyword "readℕ"))
<|> (PrintNat <$ (keyword "printNat" <|> keyword "printℕ"))
<|> (Suc <$ (keyword "suc" ))
<|> (NatElim <$ (keyword "ℕElim" <|> keyword "NatElim"))
<|> (Nat <$ (keyword "Nat" <|> keyword "ℕ"))
<|> (Zero <$ keyword "zero")
<|> (Quote <$> (char '<' *> tm <* char '>'))
<|> (Log <$> (keyword "log" *> stringLiteral))
<|> (RecTy <$> recTy)
<|> (Hole <$ char '_'))
<|> parens tm
splice :: Parser Tm
splice =
(do p <- getSourcePos
Splice <$> (char '~' *> splice) <*> pure p)
<|> atom
proj :: Parser Tm
proj = do
let go t = do {char '.'; x <- ident; go (Proj t x)} <|> pure t
go =<< splice
arg :: Parser (Either Name Icit, Tm)
arg = (try $ braces $ do {x <- ident; char '='; t <- tm; pure (Left x, t)})
<|> ((Right Impl,) <$> (char '{' *> tm <* char '}'))
<|> ((Right Expl,) <$> (proj <|> lam <|> pLet <|> pDo))
spine :: Parser Tm
spine = do
h <- proj
args <- many arg
pure $! foldl' (\t (i, u) -> App t u i) h args
lamBinder :: Parser (Name, Either Name Icit, Maybe Tm)
lamBinder =
parens ((,Right Expl,) <$> bind <*> optional (char ':' *> tm))
<|> try (braces ((,Right Impl,) <$> bind <*> optional (char ':' *> tm)))
<|> braces (do {x <- ident; char '='; y <- bind; pure (y, Left x, Nothing)})
<|> ((,Right Expl,Nothing) <$> bind)
lam :: Parser Tm
lam = do
char 'λ' <|> char '\\'
xs <- some lamBinder
char '.'
t <- lamLet
pure $ foldr' (\(!x, !i, !ma) -> Lam x i ma) t xs
piBinder :: Parser ([Name], Tm, Icit)
piBinder =
braces ((,,Impl) <$> some bind
<*> ((char ':' *> tm) <|> pure Hole))
<|> parens ((,,Expl) <$> some bind
<*> (char ':' *> tm))
pi :: Parser Tm
pi = do
dom <- some piBinder
arrow
cod <- lamLet
pure $! foldr' (\(!xs, !a, !i) t -> foldr' (\x -> Pi x i a) t xs) cod dom
apps :: Parser Tm
apps = do
t <- spine
(char '$' *> (App t <$> lamLet <*> pure (Right Expl))) <|> pure t
funOrApps :: Parser Tm
funOrApps = do
sp <- apps
optional arrow >>= \case
Nothing -> pure sp
Just _ -> Pi "_" Expl sp <$> lamLet
-- | Desugar Coq-style definition arguments.
desugarIdentArgs :: [([Name], Tm, Icit)] -> Maybe Tm -> Tm -> (Tm, Maybe Tm)
desugarIdentArgs args mty rhs = case mty of
-- if there's no return type annotation, we desugar to annotated lambdas
Nothing -> let
tm = foldr' (\(xs, a, i) t -> foldr' (\x -> Lam x (Right i) (Just a)) t xs) rhs args
in (tm, Nothing)
-- otherwise we pull out the iterated Pi type to get an annotation on a "let".
Just a -> let
tm = foldr' (\(xs, a, i) t -> foldr' (\x -> Lam x (Right i) Nothing) t xs) rhs args
ty = foldr' (\(xs, a, i) b -> foldr' (\x -> Pi x i a) b xs) a args
in (tm, Just ty)
letArg :: Parser ([Name], Tm, Icit)
letArg =
braces ((,,Impl) <$> some bind
<*> ((char ':' *> tm) <|> pure Hole))
<|> parens ((,,Expl) <$> some bind
<*> (char ':' *> tm))
<|> (do {x <- ident; pure ([x], Hole, Expl)})
pLet :: Parser Tm
pLet = do
keyword "let"
x <- ident
args <- many letArg
ann <- optional (char ':' *> lamLet)
char '='
t <- lamLet
(t, ann) <- pure $ desugarIdentArgs args ann t
char ';'
u <- lamLet
pure $ Let x ann t u
pDo :: Parser Tm
pDo = do
keyword "do"
optional (try (ident <* (symbol "<-" <|> symbol "←"))) >>= \case
Nothing -> do
t <- tm
char ';'
u <- lamLet
pure $ Seq t u
Just x -> do
t <- tm
char ';'
u <- lamLet
pure $ Bind x t u
open :: Parser Tm
open = do
keyword "open"
t <- tm
char ';'
u <- lamLet
pure $ Open t u
lamLet :: Parser Tm
lamLet = do
withPos (
lam
<|> pLet
<|> open
<|> pDo
<|> try pi
<|> funOrApps)
recField :: Parser (Maybe Name, Tm)
recField = (,) <$> optional (try (ident <* char '=')) <*> lamLet
tm :: Parser Tm
tm = do
optional (try (ident <* char '=')) >>= \case
Nothing -> do
t <- lamLet
let mkRec = do char ','
rest <- sepBy recField (char ',')
pure (Rec ((Nothing, t):rest))
mkRec <|> pure t
Just x -> do
t <- lamLet
rest <- many (char ',' *> recField)
pure $ Rec ((Just x, t):rest)
-- dbg :: String -> Parser ()
-- dbg msg = do
-- l <- lookAhead takeRest
-- traceM (msg ++ "|" ++ l)
topLet :: Parser Tm
topLet = do
(x, args, ann) <- try do
x <- ident
args <- many letArg
ann <- optional (char ':' *> lamLet)
char '='
pure (x, args, ann)
t <- tm
(t, ann) <- pure $ desugarIdentArgs args ann t
char ';'
u <- top
pure $ Let x ann t u
topDo :: Parser Tm
topDo = do
keyword "do"
optional (try (ident <* (symbol "<-" <|> symbol "←"))) >>= \case
Nothing -> do
t <- tm
char ';'
u <- top
pure $ Seq t u
Just x -> do
t <- tm
char ';'
u <- top
pure $ Bind x t u
topen :: Parser Tm
topen = do
keyword "open"
t <- tm
char ';'
u <- top
pure $ Open t u
top :: Parser Tm
top = withPos (topLet <|> topDo <|> topen <|> lamLet)
src :: Parser Tm
src = ws *> top <* eof
parseString :: FilePath -> String -> IO Tm
parseString path str =
case parse src path str of
Left e -> do
putStrLn $ errorBundlePretty e
exitFailure
Right t -> do
writeIORef sourceCode str
pure t