-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree-parser.hs
51 lines (41 loc) · 1.37 KB
/
tree-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
import Control.Applicative (Alternative ((<|>)))
import Parser
data Tree a
= Empty
| Node a (Tree a) (Tree a)
deriving (Show)
singleton :: a -> Tree a
singleton a = Node a Empty Empty
parseEmpty :: Parser (Tree a)
parseEmpty = Empty <$ char '*'
-- Monadic sequencing of parsers
parseNode' :: Parser Int -> Parser (Tree Int)
parseNode' parserA = do
root <- char '{' *> ws *> parserA <* ws
left <- parseTree parserA <* ws
right <- parseTree parserA <* ws <* char '}'
if root < 5
then abortParser "root value should be always >= 5"
else return $ Node root left right
-- Applicative sequencing of parsers
parseNode :: Parser a -> Parser (Tree a)
parseNode parserA =
Node
<$> (char '{' *> ws *> parserA <* ws)
<*> (parseTree parserA <* ws)
<*> parseTree parserA
parseTree :: Parser a -> Parser (Tree a)
parseTree parserA = parseEmpty <|> parseNode parserA
wholeTree :: Parser a -> Parser (Tree a)
wholeTree p = ws *> parseTree p <* end
instance (Parseable a) => Parseable (Tree a) where
parser = wholeTree parser
main :: IO ()
main = do
print (parse "*" :: Tree Int)
print (parse "{5 * *}" :: Tree Int)
print (parse "{1 {3 * *} *}" :: Tree Int)
print (parse "{1 * {3 * *}}" :: Tree Int)
print (parse "{1{4{8**}*}{3*{2**}}} " :: Tree Int)
print $ runParser (wholeTree nonWS) "{root {left * *} {right * *}}"
print (parse "{1 * *} end?" :: Tree Int)