summaryrefslogtreecommitdiff
path: root/src/PGF/CId.hs
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2009-09-11 13:45:34 +0000
committerkrasimir <krasimir@chalmers.se>2009-09-11 13:45:34 +0000
commit1cdf171251a56baf0867b65a95c9bd59801ff912 (patch)
tree837e65fa23f3041c3bbf4b7f1dbfcf63990e09a1 /src/PGF/CId.hs
parent28a7c4b5c7659dc18166e06e914fb0a81c1c43bc (diff)
polish the PGF API and make Expr and Type abstract types. Tree is a type synonym of Expr
Diffstat (limited to 'src/PGF/CId.hs')
-rw-r--r--src/PGF/CId.hs23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/PGF/CId.hs b/src/PGF/CId.hs
index 0d1a2f5c6..fea304d9d 100644
--- a/src/PGF/CId.hs
+++ b/src/PGF/CId.hs
@@ -1,16 +1,19 @@
module PGF.CId (CId(..),
- mkCId, readCId, prCId,
- wildCId,
- pCId, pIdent) where
+ mkCId, wildCId,
+ readCId, showCId,
+
+ -- utils
+ pCId, pIdent, ppCId) where
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import qualified Text.ParserCombinators.ReadP as RP
+import qualified Text.PrettyPrint as PP
-- | An abstract data type that represents
--- function identifier in PGF.
+-- identifiers for functions and categories in PGF.
newtype CId = CId BS.ByteString deriving (Eq,Ord)
wildCId :: CId
@@ -20,17 +23,18 @@ wildCId = CId (BS.singleton '_')
mkCId :: String -> CId
mkCId s = CId (BS.pack s)
+-- | Reads an identifier from 'String'. The function returns 'Nothing' if the string is not valid identifier.
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
+showCId :: CId -> String
+showCId (CId x) = BS.unpack x
instance Show CId where
- showsPrec _ = showString . prCId
+ showsPrec _ = showString . showCId
instance Read CId where
readsPrec _ = RP.readP_to_S pCId
@@ -45,4 +49,7 @@ 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
+ isIdentRest c = c == '_' || c == '\'' || isAlphaNum c
+
+ppCId :: CId -> PP.Doc
+ppCId = PP.text . showCId