[project @ 2005-10-20 23:28:42 by ross]
[haskell-directory.git] / Data / Sequence.hs
index 6f01e0d..6b53985 100644 (file)
@@ -87,8 +87,13 @@ import Data.Typeable
 
 #ifdef __GLASGOW_HASKELL__
 import GHC.Exts (build)
+import Text.ParserCombinators.ReadP (char, skipSpaces, between, sepBy, (+++))
+import Text.Read (readPrec, readPrec_to_P, readP_to_Prec, minPrec)
+import Control.Monad (liftM)
 import Data.Generics.Basics (Data(..), Fixity(..),
                        constrIndex, mkConstr, mkDataType)
+#else
+import Data.Char (isSpace)
 #endif
 
 #if TESTING
@@ -146,6 +151,32 @@ instance Show a => Show (Seq a) where
                showChar '>'
 #endif
 
+instance Read a => Read (Seq a) where
+       -- avoid lex, as < or > might be followed by symbol characters.
+#ifdef __GLASGOW_HASKELL__
+       readPrec = readP_to_Prec $ const $
+               parens $
+               between (litChar '<') (litChar '>') $
+               liftM fromList $
+               readPrec_to_P readPrec minPrec `sepBy` litChar ','
+         where parens p = p +++ between (litChar '(') (litChar ')') (parens p)
+               litChar c = skipSpaces >> char c
+#else
+       readsPrec _ = readParen False $ \ r ->
+               case dropWhile isSpace r of
+                       ('<':s) -> case dropWhile isSpace s of
+                               ('>':t) -> return (empty,t)
+                               _       -> readRest empty s
+                       _ -> []
+         where readRest xs s = do
+                       (x,t) <- reads s
+                       let xs' = xs |> x
+                       case dropWhile isSpace t of
+                               ('>':u) -> return (xs',u)
+                               (',':u) -> readRest xs' u
+                               _     -> []
+#endif
+
 #include "Typeable.h"
 INSTANCE_TYPEABLE1(Seq,seqTc,"Seq")