-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.hs
45 lines (31 loc) · 1.14 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
{-# LANGUAGE OverloadedStrings #-}
module Parser where
import Data.Void
import Control.Monad (void)
import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import Lambda
type Parser = Parsec Void String
sc :: Parser ()
sc = L.space (void $ some (char ' ' <|> char '\t')) empty empty
lexeme :: Parser a -> Parser a
lexeme = L.lexeme sc
symbol :: String -> Parser String
symbol = L.symbol sc
parseVar :: Parser Term
parseVar = lexeme $ Var <$> some (alphaNumChar <|> char '/')
parseLambda :: Parser Term
parseLambda = lexeme $ symbol "\\" *> (mulLambda <$> some parseVar <* symbol ".") <*> parseTerm
parseParens :: Parser Term
parseParens = symbol "(" *> parseTerm <* symbol ")"
parseAtom :: Parser Term
parseAtom = parseCont <|> parseVar <|> parseLambda <|> parseParens
parseCont :: Parser Term
parseCont = lexeme $ (optional $ symbol "continuation") *> parseCont1
parseCont1 :: Parser Term
parseCont1 = Cont <$> (symbol "[" *> sepBy parseTerm (symbol ",") <* symbol "]")
parseApply :: Parser Term
parseApply = foldl1 App <$> some parseAtom
parseTerm :: Parser Term
parseTerm = (many $ char ' ') *> parseApply