From a307e39d47eae349ced1358c85dfa592a76983ec Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 2 Apr 2009 15:07:17 +0000 Subject: [PATCH] Added a Read instance for Meta (taken from my benchmarks for the ICFP paper) --- data/Metadata.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/data/Metadata.hs b/data/Metadata.hs index 597921e..76a46fa 100644 --- a/data/Metadata.hs +++ b/data/Metadata.hs @@ -22,6 +22,7 @@ module Metadata where {-! global : Haskell2Xml !-} import Data.Generics +import Data.List import Numeric import Text.Printf import Text.Regex @@ -46,6 +47,20 @@ instance Show Meta where Just s -> s ++ ":" ++ show (metaLine m) ++ ":" ++ show (metaColumn m) Nothing -> "no source position" +dropSpaces :: String -> String +dropSpaces = dropWhile (== ' ') + +instance Read Meta where + readsPrec n str + | show emptyMeta `isPrefixOf` dropSpaces str + = [(Meta Nothing 0 0, drop (length $ show emptyMeta) $ dropSpaces str)] + | otherwise + = [ (Meta (Just fn) (read l) (read n), r'') ] + where + (fn, ':':r) = span (/= ':') $ dropSpaces str + (l, ':':r') = span (/= ':') r + (n, r'') = span (`elem` ['0'..'9']) r' + -- | Encode a Meta as the prefix of a string. packMeta :: Meta -> String -> String packMeta m s