1  module Parse(Parse(..),whiteSpace,seperatedBy) where
    2  import Char -- 1.3
    3  import StdLib
    4  class Parse a where
    5         parseFile :: String -> [a]
    6         parseLine :: String -> a
    7         parse :: String -> (a,String)
    8         parseType :: String -> (a,String)
    9         forced :: a -> Bool
   10 
   11         parseFile string | all forced x = x
   12                         where x = map parseLine (lines' string)
   13         parseLine = pl.parse where pl (a,_) = a
   14         parse = parseType.whiteSpace
   15         forced x = True
   16 
   17  instance Parse Int where
   18         parseType str = pl (span' isDigit str)
   19                 where         pl (l,r) = (strToInt l,r)
   20         forced n | n>=0 = True
   21  instance Parse Char where
   22         parseType (ch:str) = (ch,str)
   23         forced n = True
   24  instance (Parse a) => Parse [a] where
   25          parseType more = (map parseLine (seperatedBy ',' (l++",")),out)
   26                         where       (l,']':out) = span' (\x->x/=']') (tail more)
   27          forced = all forced
   28  seperatedBy :: Char -> String -> [String]
   29  seperatedBy ch [] = []
   30  seperatedBy ch xs = twaddle ch (span' (\x->x/=ch) xs)
   31                 where twaddle ch (l,_:r) = l:seperatedBy ch r
   32  whiteSpace :: String -> String
   33  whiteSpace = dropWhile isSpace