1 --module Parse(Parse(..),whiteSpace,seperatedBy) where
3 module ShouldSucceed where
8 parseFile :: String -> [a]
9 parseLine :: String -> a
10 parseType :: String -> (a,String)
11 parse :: String -> (a,String)
14 parseFile string | all forced x = x
15 where x = map parseLine (lines' string)
16 parseLine = pl.parse where pl (a,_) = a
17 parse = parseType.whiteSpace
20 instance Parse Int where
21 parseType str = pl (span' isDigit str)
22 where pl (l,r) = (strToInt l,r)
23 forced n | n>=0 = True
25 instance Parse Char where
26 parseType (ch:str) = (ch,str)
29 instance (Parse a) => Parse [a] where
30 parseType more = (map parseLine (seperatedBy ',' (l++",")),out)
31 where (l,']':out) = span' (\x->x/=']') (tail more)
34 seperatedBy :: Char -> String -> [String]
35 seperatedBy ch [] = []
36 seperatedBy ch xs = twaddle ch (span' (\x->x/=ch) xs)
37 where twaddle ch (l,_:r) = l:seperatedBy ch r
39 whiteSpace :: String -> String
40 whiteSpace = dropWhile isSpace
42 span' :: (a->Bool) -> [a] -> ([a],[a])
44 span' p (x:xs') | p x = fixLeak x (span' p xs') where fixLeak x (xs,ys) = (x:xs,ys)
47 lines' :: [Char] -> [[Char]]
49 lines' s = plumb (span' ((/=) '\n') s)
50 where plumb (l,s') = l:if null s' then [] else lines' (tail s')
52 strToInt :: String -> Int
53 strToInt x = strToInt' (length x-1) x
54 where strToInt' _ [] = 0
55 strToInt' x (a:l) = (charToInt a)*(10^x) + (strToInt' (x-1) l)
57 charToInt :: Char -> Int
58 charToInt x = (ord x - ord '0')