-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.hs
33 lines (27 loc) · 898 Bytes
/
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
module Parser where
import Text.ParserCombinators.Parsec
import Expression
variableIdentifier :: Parser String
variableIdentifier = many1 (noneOf " .\\()")
singleLambdaParser :: Parser Expression
singleLambdaParser =
(do char '\\'
spaces
vl <- (endBy1 variableIdentifier spaces)
char '.'
spaces
e <- lambdaParser
return $ foldr (\x f -> abstraction x f) e vl)
<|> (do char '('
e <- lambdaParser
char ')'
return e)
<|> do v <- variableIdentifier
return $ variable v
lambdaParser :: Parser Expression
lambdaParser = do l <- endBy1 singleLambdaParser spaces
return $ foldl1 (\f x -> application f x) l
parseLambda :: String -> Maybe Expression
parseLambda s = case parse lambdaParser "(unknown)" s of
Right a -> Just a
Left _ -> Nothing