1 This module deals with printing (a) C string literals and (b) C labels.
4 #include "HsVersions.h"
17 CHK_Ubiq() -- debugging consistency check
20 import Unpretty( uppChar )
22 IMPORT_1_3(Char (isAlphanum))
23 #ifdef REALLY_HASKELL_1_3
24 ord = fromEnum :: Char -> Int
25 chr = toEnum :: Int -> Char
31 _ is the main separator
37 <funny char> Z[hex-digit][hex-digit]
44 cSEP = SLIT("_") -- official C separator
47 identToC :: FAST_STRING -> Pretty
48 modnameToC :: FAST_STRING -> FAST_STRING
49 stringToC :: String -> String
50 charToC, charToEasyHaskell :: Char -> String
52 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
55 stringToC [c] = charToC c
57 -- if we have something "octifiable" in "c", we'd better "octify"
58 -- the rest of the string, too.
59 = if (c < ' ' || c > '~')
60 then (charToC c) ++ (concat (map char_to_C cs))
61 else (charToC c) ++ (stringToC cs)
63 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
65 | c == '\b' = "\\b" -- ToDo: chk some of these...
70 | otherwise = '\\' : (octify (ord c))
72 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
85 else '\\' : (octify (ord c))
87 -- really: charToSimpleHaskell
90 = if (c >= 'a' && c <= 'z')
91 || (c >= 'A' && c <= 'Z')
92 || (c >= '0' && c <= '9')
95 _ -> '\\' : 'o' : (octify (ord c))
97 octify :: Int -> String
102 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]
110 's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
114 (if (all isAlphanum str) -- we gamble that this test will succeed...
116 else ppIntersperse ppNil (map char_to_c str))
118 char_to_c 'Z' = ppPStr SLIT("ZZ")
119 char_to_c '&' = ppPStr SLIT("Za")
120 char_to_c '|' = ppPStr SLIT("Zb")
121 char_to_c ':' = ppPStr SLIT("Zc")
122 char_to_c '/' = ppPStr SLIT("Zd")
123 char_to_c '=' = ppPStr SLIT("Ze")
124 char_to_c '>' = ppPStr SLIT("Zg")
125 char_to_c '#' = ppPStr SLIT("Zh")
126 char_to_c '<' = ppPStr SLIT("Zl")
127 char_to_c '-' = ppPStr SLIT("Zm")
128 char_to_c '!' = ppPStr SLIT("Zn")
129 char_to_c '.' = ppPStr SLIT("Zo")
130 char_to_c '+' = ppPStr SLIT("Zp")
131 char_to_c '\'' = ppPStr SLIT("Zq")
132 char_to_c '*' = ppPStr SLIT("Zt")
133 char_to_c '_' = ppPStr SLIT("Zu")
135 char_to_c c = if isAlphanum c
137 else ppBeside (ppChar 'Z') (ppInt (ord c))
140 For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
141 chars) in the name. Rare.
147 if not (any quote_here str) then
150 _PK_ (concat (map char_to_c str))
152 quote_here '\'' = True
156 = if isAlphanum c then [c] else 'Z' : (show (ord c))