diff options
| author | bjorn <bjorn@bringert.net> | 2008-10-20 11:59:31 +0000 |
|---|---|---|
| committer | bjorn <bjorn@bringert.net> | 2008-10-20 11:59:31 +0000 |
| commit | d41616dd6aa52c813edf7dabef4c0684fae4e4e0 (patch) | |
| tree | 0d6490e8978902b32e55b631567fa99eef43159f /src/PGF/CId.hs | |
| parent | 2174690c5e60667955bbf9fd6f0a8d3beb1734a9 (diff) | |
Added Read and Show instances for CId. Also added readCId :: String -> Maybe CId, and use that for readLanguage.
Diffstat (limited to 'src/PGF/CId.hs')
| -rw-r--r-- | src/PGF/CId.hs | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/src/PGF/CId.hs b/src/PGF/CId.hs index 161529308..99325975e 100644 --- a/src/PGF/CId.hs +++ b/src/PGF/CId.hs @@ -1,10 +1,17 @@ -module PGF.CId (CId(..), wildCId, mkCId, prCId) where +module PGF.CId (CId(..), + mkCId, readCId, prCId, + wildCId, + pCId, pIdent) where + +import Control.Monad +import qualified Data.ByteString.Char8 as BS +import Data.Char +import qualified Text.ParserCombinators.ReadP as RP -import Data.ByteString.Char8 as BS -- | An abstract data type that represents -- function identifier in PGF. -newtype CId = CId BS.ByteString deriving (Eq,Ord,Show) +newtype CId = CId BS.ByteString deriving (Eq,Ord) wildCId :: CId wildCId = CId (BS.singleton '_') @@ -13,6 +20,26 @@ wildCId = CId (BS.singleton '_') mkCId :: String -> CId mkCId s = CId (BS.pack s) +readCId :: String -> Maybe CId +readCId s = case [x | (x,cs) <- RP.readP_to_S pCId s, all isSpace cs] of + [x] -> Just x + _ -> Nothing + -- | Renders the identifier as 'String' prCId :: CId -> String prCId (CId x) = BS.unpack x + +instance Show CId where + showsPrec _ = showString . prCId + +instance Read CId where + readsPrec _ = RP.readP_to_S pCId + +pCId :: RP.ReadP CId +pCId = fmap mkCId pIdent + +pIdent :: RP.ReadP String +pIdent = liftM2 (:) (RP.satisfy isIdentFirst) (RP.munch isIdentRest) + where + isIdentFirst c = c == '_' || isLetter c + isIdentRest c = c == '_' || c == '\'' || isAlphaNum c
\ No newline at end of file |
