gdritter repos telml / 704922f
Fixed error with grouping and cleaned up some error msgs Getty Ritter 9 years ago
1 changed file(s) with 20 addition(s) and 8 deletion(s). Collapse all Expand all
3232 go "" = return ("", "")
3333
3434 pTagName :: Parse String
35 pTagName i@(x:xs)
36 | isAlpha x = (x:) `over` pTagName xs
37 | elem x "-_" = (x:) `over` pTagName xs
38 | otherwise = return (i, "")
35 pTagName s = go s `bind` ensureLen
36 where go i@(x:xs)
37 | isAlpha x = (x:) `over` pTagName xs
38 | elem x "-_" = (x:) `over` pTagName xs
39 | otherwise = return (i, "")
40 go [] = throw "unexpected end-of-document while parsing tag"
41 ensureLen (xs, name)
42 | length name > 0 = return (xs, name)
43 | otherwise = throw "expected tag name after `\\'"
3944
4045 skipSpace :: Parse ()
4146 skipSpace i@(x:xs)
4247 | isSpace x = skipSpace xs
4348 | otherwise = return (i, ())
49 skipSpace _ = return ("", ())
4450
4551 pTag :: Parse Fragment
4652 pTag i =
4753 bind (pTagName i) $ \ (i', name) ->
4854 bind (skipSpace i') $ \case
4955 ('{':i'', ()) -> Tag name `over` pArgs i''
56 ("",_) -> throw "unexpected end-of-document while parsing tag"
5057 _ -> throw "expected start of block"
5158
5259 pArgs :: Parse [Document]
6471
6572 pFragments :: Parse Document
6673 pFragments "" = return ("", [])
74 pFragments ('{':s) = bind (pFragments s) $ \case
75 ('}':xs, cs) -> bind (pFragments xs) $ \(xs', cs') -> return (xs', cs ++ cs')
76 (x:_, _) -> throw ("unexpected " ++ show x ++ "; expected '}'")
77 ([], _) -> throw ("unexpected end-of-document while parsing block")
6778 pFragments s@(x:_)
6879 | x `elem` "}|" = return (s, [])
6980 | otherwise =
7182 (s', c) -> (c:) `over` pFragments s'
7283
7384 parse :: String -> Either String Document
74 parse s = case pFragments s of
75 Right ("", r) -> return r
76 Right (s, _) -> throw ("expected end of document but found " ++ show s)
77 Left err -> throw err
85 parse str = case pFragments str of
86 Right ("", r) -> return r
87 Right ('}':_, _) -> throw ("Found unmatched '}' in document")
88 Right (s, _) -> throw ("expected end of document but found " ++ show s)
89 Left err -> throw err