1 This module deals with printing (a) C string literals and (b) C labels.
4 #include "HsVersions.h"
17 IMPORT_1_3(Char (isAlphanum,ord,chr))
18 CHK_Ubiq() -- debugging consistency check
21 #if __GLASGOW_HASKELL__ >= 209
29 _ is the main separator
35 <funny char> Z[hex-digit][hex-digit]
42 cSEP = SLIT("_") -- official C separator
45 identToC :: FAST_STRING -> Doc
46 modnameToC :: FAST_STRING -> FAST_STRING
47 stringToC :: String -> String
48 charToC, charToEasyHaskell :: Char -> String
50 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
53 stringToC [c] = charToC c
55 -- if we have something "octifiable" in "c", we'd better "octify"
56 -- the rest of the string, too.
57 = if (c < ' ' || c > '~')
58 then (charToC c) ++ (concat (map char_to_C cs))
59 else (charToC c) ++ (stringToC cs)
61 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
63 | c == '\b' = "\\b" -- ToDo: chk some of these...
68 | otherwise = '\\' : (octify (ord c))
70 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
83 else '\\' : (octify (ord c))
85 -- really: charToSimpleHaskell
88 = if (c >= 'a' && c <= 'z')
89 || (c >= 'A' && c <= 'Z')
90 || (c >= '0' && c <= '9')
93 _ -> '\\' : show (ord c)
95 octify :: Int -> String
100 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]
108 's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
112 (if (all isAlphanum str) -- we gamble that this test will succeed...
114 else hcat (map char_to_c str))
116 char_to_c 'Z' = ptext SLIT("ZZ")
117 char_to_c '&' = ptext SLIT("Za")
118 char_to_c '|' = ptext SLIT("Zb")
119 char_to_c ':' = ptext SLIT("Zc")
120 char_to_c '/' = ptext SLIT("Zd")
121 char_to_c '=' = ptext SLIT("Ze")
122 char_to_c '>' = ptext SLIT("Zg")
123 char_to_c '#' = ptext SLIT("Zh")
124 char_to_c '<' = ptext SLIT("Zl")
125 char_to_c '-' = ptext SLIT("Zm")
126 char_to_c '!' = ptext SLIT("Zn")
127 char_to_c '.' = ptext SLIT("_")
128 char_to_c '+' = ptext SLIT("Zp")
129 char_to_c '\'' = ptext SLIT("Zq")
130 char_to_c '*' = ptext SLIT("Zt")
131 char_to_c '_' = ptext SLIT("Zu")
133 char_to_c c = if isAlphanum c
135 else (<>) (char 'Z') (int (ord c))
138 For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
139 chars) in the name. Rare.
145 if not (any quote_here str) then
148 _PK_ (concat (map char_to_c str))
150 quote_here '\'' = True
154 = if isAlphanum c then [c] else 'Z' : (show (ord c))