-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.hs
More file actions
39 lines (29 loc) · 715 Bytes
/
Parser.hs
File metadata and controls
39 lines (29 loc) · 715 Bytes
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
{-# LANGUAGE FlexibleContexts #-}
module Parser where
import LC
import Text.Parsec
import Text.Parsec.Text
import Data.Text as T
import Control.Applicative hiding ((<|>), many)
type TermT = Term Text
parens :: Stream s m Char => ParsecT s u m a -> ParsecT s u m a
parens = between (char '(') (char ')')
expr :: Parser TermT
expr = var <|> lam <|> app
ident :: Parser Text
ident = T.pack <$> ((:) <$> letter <*> many alphaNum)
var :: Parser TermT
var = Var <$> ident
lam :: Parser TermT
lam = parens $ do
_ <- char '\\'
v <- spaces *> (ident <* spaces)
_ <- char '.'
exp' <- spaces *> expr
return (Lam v exp')
app :: Parser TermT
app = do
e1 <- expr
spaces
e2 <- expr
return (App e1 e2)