gdritter repos telml / a506495
Working on new hand-written parser (because why not?) Getty Ritter 9 years ago
1 changed file(s) with 43 addition(s) and 0 deletion(s). Collapse all Expand all
1 import Data.Char
2
3 type Document = [Chunk]
4 data Chunk
5 = Text String
6 | Tag String [Document]
7 deriving (Eq, Show)
8
9 isSpecial c = c `elem` "\\{}|"
10
11
12 pText :: String -> (String, Chunk)
13 pText = fmap Text . go
14 where go ('\\':x:xs)
15 | isSpecial x = fmap (x:) (go xs)
16 go i@(x:xs)
17 | isSpecial x = (i, "")
18 | otherwise = fmap (x:) (go xs)
19 go "" = ("", "")
20
21 pTagName :: String -> (String, String)
22 pTagName i@(x:xs)
23 | isAlpha x = fmap (x:) (pTagName xs)
24 | elem x "-_" = fmap (x:) (pTagName xs)
25 | otherwise = (i, "")
26
27 skipSpace :: String -> (String, ())
28 skipSpace i@(x:xs)
29 | isSpace x = skipSpace xs
30 | otherwise = (i, ())
31
32 pTag :: String -> (String, Chunk)
33 pTag i =
34 let (i', name) = pTagName i
35 in case skipSpace i' of
36 ('{':i'') -> fmap (Tag name) (pArgs i'')
37 otherwise -> error "expected start of block"
38
39 pArgs :: String -> (String, [Document])
40 pArgs ('|')
41
42 pChunk :: String -> (String, Chunk)
43 pChunk = undefined