1 This module deals with printing (a) C string literals and (b) C labels.
4 #include "HsVersions.h"
17 CHK_Ubiq() -- debugging consistency check
21 IMPORT_1_3(Char (isAlphanum,ord,chr))
26 _ is the main separator
32 <funny char> Z[hex-digit][hex-digit]
39 cSEP = SLIT("_") -- official C separator
42 identToC :: FAST_STRING -> Doc
43 modnameToC :: FAST_STRING -> FAST_STRING
44 stringToC :: String -> String
45 charToC, charToEasyHaskell :: Char -> String
47 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
50 stringToC [c] = charToC c
52 -- if we have something "octifiable" in "c", we'd better "octify"
53 -- the rest of the string, too.
54 = if (c < ' ' || c > '~')
55 then (charToC c) ++ (concat (map char_to_C cs))
56 else (charToC c) ++ (stringToC cs)
58 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
60 | c == '\b' = "\\b" -- ToDo: chk some of these...
65 | otherwise = '\\' : (octify (ord c))
67 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
80 else '\\' : (octify (ord c))
82 -- really: charToSimpleHaskell
85 = if (c >= 'a' && c <= 'z')
86 || (c >= 'A' && c <= 'Z')
87 || (c >= '0' && c <= '9')
90 _ -> '\\' : show (ord c)
92 octify :: Int -> String
97 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]
105 's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
109 (if (all isAlphanum str) -- we gamble that this test will succeed...
111 else hcat (map char_to_c str))
113 char_to_c 'Z' = ptext SLIT("ZZ")
114 char_to_c '&' = ptext SLIT("Za")
115 char_to_c '|' = ptext SLIT("Zb")
116 char_to_c ':' = ptext SLIT("Zc")
117 char_to_c '/' = ptext SLIT("Zd")
118 char_to_c '=' = ptext SLIT("Ze")
119 char_to_c '>' = ptext SLIT("Zg")
120 char_to_c '#' = ptext SLIT("Zh")
121 char_to_c '<' = ptext SLIT("Zl")
122 char_to_c '-' = ptext SLIT("Zm")
123 char_to_c '!' = ptext SLIT("Zn")
124 char_to_c '.' = ptext SLIT("_")
125 char_to_c '+' = ptext SLIT("Zp")
126 char_to_c '\'' = ptext SLIT("Zq")
127 char_to_c '*' = ptext SLIT("Zt")
128 char_to_c '_' = ptext SLIT("Zu")
130 char_to_c c = if isAlphanum c
132 else (<>) (char 'Z') (int (ord c))
135 For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
136 chars) in the name. Rare.
142 if not (any quote_here str) then
145 _PK_ (concat (map char_to_c str))
147 quote_here '\'' = True
151 = if isAlphanum c then [c] else 'Z' : (show (ord c))