diff options
| author | inari <inari@chalmers.se> | 2013-12-10 15:27:28 +0000 |
|---|---|---|
| committer | inari <inari@chalmers.se> | 2013-12-10 15:27:28 +0000 |
| commit | d7847f2f1378ca2a472daea23fc70ce87bae4867 (patch) | |
| tree | e1ee8f7a3d7cdc2ae6c073db2c8c9477d5199c9c /src/runtime/haskell/CRuntimeFFI/CId.hs | |
| parent | 0ece8f19abf4dba1d3c568aca60d2c960d75efa6 (diff) | |
Haskell bindings for the C runtime
Added Haskell bindings for the C runtime. Work in progress, the files are not (and should not be) included in makefiles or anything.
Diffstat (limited to 'src/runtime/haskell/CRuntimeFFI/CId.hs')
| -rw-r--r-- | src/runtime/haskell/CRuntimeFFI/CId.hs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/runtime/haskell/CRuntimeFFI/CId.hs b/src/runtime/haskell/CRuntimeFFI/CId.hs new file mode 100644 index 000000000..74db63c2c --- /dev/null +++ b/src/runtime/haskell/CRuntimeFFI/CId.hs @@ -0,0 +1,56 @@ +module CId (CId(..), + 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 +-- identifiers for functions and categories in PGF. +newtype CId = CId BS.ByteString deriving (Eq,Ord) + +wildCId :: CId +wildCId = CId (BS.singleton '_') + +-- | Creates a new identifier from 'String' +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' +showCId :: CId -> String +showCId (CId x) = BS.unpack x + +instance Show CId where + showsPrec _ = showString . showCId + +instance Read CId where + readsPrec _ = RP.readP_to_S pCId + +pCId :: RP.ReadP CId +pCId = do s <- pIdent + if s == "_" + then RP.pfail + else return (mkCId s) + +pIdent :: RP.ReadP String +pIdent = liftM2 (:) (RP.satisfy isIdentFirst) (RP.munch isIdentRest) + where + isIdentFirst c = c == '_' || isLetter c + isIdentRest c = c == '_' || c == '\'' || isAlphaNum c + +ppCId :: CId -> PP.Doc +ppCId = PP.text . showCId |
