X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FabsCSyn%2FCStrings.lhs;fp=ghc%2Fcompiler%2FabsCSyn%2FCStrings.lhs;h=628b54056851850e09030360e219e73ee9515daa;hb=111cee3f1ad93816cb828e38b38521d85c3bcebb;hp=dcbf165aa175ea8c738bbda2373a08a27ae344a4;hpb=290e7896a6785ba5dcfbc7045438f382afd447ff;p=ghc-hetmet.git diff --git a/ghc/compiler/absCSyn/CStrings.lhs b/ghc/compiler/absCSyn/CStrings.lhs index dcbf165..628b540 100644 --- a/ghc/compiler/absCSyn/CStrings.lhs +++ b/ghc/compiler/absCSyn/CStrings.lhs @@ -2,6 +2,7 @@ This module deals with printing C string literals \begin{code} module CStrings( + CLabelString, isCLabelString, cSEP, pp_cSEP, stringToC, charToC, pprFSInCStyle, @@ -10,23 +11,33 @@ module CStrings( #include "HsVersions.h" -import Char ( ord, chr ) +import Char ( ord, chr, isAlphaNum ) import Outputable \end{code} \begin{code} +type CLabelString = FAST_STRING -- A C label, completely unencoded + +isCLabelString :: CLabelString -> Bool -- Checks to see if this is a valid C label +isCLabelString lbl + = all ok (_UNPK_ lbl) + where + ok c = isAlphaNum c || c == '_' || c == '.' + -- The '.' appears in e.g. "foo.so" in the + -- module part of a ExtName. Maybe it should be separate + cSEP = SLIT("_") -- official C separator pp_cSEP = char '_' +\end{code} -stringToC :: String -> String -charToC, charToEasyHaskell :: Char -> String - +\begin{code} pprFSInCStyle :: FAST_STRING -> SDoc pprFSInCStyle fs = doubleQuotes (text (stringToC (_UNPK_ fs))) --- stringToC: the hassle is what to do w/ strings like "ESC 0"... - +stringToC :: String -> String +-- Convert a string to the form required by C in a C literal string +-- Tthe hassle is what to do w/ strings like "ESC 0"... stringToC "" = "" stringToC [c] = charToC c stringToC (c:cs) @@ -45,6 +56,8 @@ stringToC (c:cs) | c == '\v' = "\\v" | otherwise = '\\' : (octify (ord c)) +charToC :: Char -> String +-- Convert a character to the form reqd in a C character literal charToC c = if (c >= ' ' && c <= '~') -- non-portable... then case c of '\'' -> "\\'" @@ -60,8 +73,8 @@ charToC c = if (c >= ' ' && c <= '~') -- non-portable... _ -> [c] else '\\' : (octify (ord c)) --- really: charToSimpleHaskell - +charToEasyHaskell :: Char -> String +-- Convert a character to the form reqd in a Haskell character literal charToEasyHaskell c = if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')