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 )
25 _ is the main separator
31 <funny char> Z[hex-digit][hex-digit]
38 cSEP = SLIT("_") -- official C separator
41 identToC :: FAST_STRING -> Pretty
42 modnameToC :: FAST_STRING -> FAST_STRING
43 stringToC :: String -> String
44 charToC, charToEasyHaskell :: Char -> String
46 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
49 stringToC [c] = charToC c
51 -- if we have something "octifiable" in "c", we'd better "octify"
52 -- the rest of the string, too.
53 = if (c < ' ' || c > '~')
54 then (charToC c) ++ (concat (map char_to_C cs))
55 else (charToC c) ++ (stringToC cs)
57 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
59 | c == '\b' = "\\b" -- ToDo: chk some of these...
64 | otherwise = '\\' : (octify (ord c))
66 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
79 else '\\' : (octify (ord c))
81 -- really: charToSimpleHaskell
84 = if (c >= 'a' && c <= 'z')
85 || (c >= 'A' && c <= 'Z')
86 || (c >= '0' && c <= '9')
89 _ -> '\\' : 'o' : (octify (ord c))
91 octify :: Int -> String
96 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]
104 's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
108 (if (all isAlphanum str) -- we gamble that this test will succeed...
110 else ppIntersperse ppNil (map char_to_c str))
112 char_to_c 'Z' = ppPStr SLIT("ZZ")
113 char_to_c '&' = ppPStr SLIT("Za")
114 char_to_c '|' = ppPStr SLIT("Zb")
115 char_to_c ':' = ppPStr SLIT("Zc")
116 char_to_c '/' = ppPStr SLIT("Zd")
117 char_to_c '=' = ppPStr SLIT("Ze")
118 char_to_c '>' = ppPStr SLIT("Zg")
119 char_to_c '#' = ppPStr SLIT("Zh")
120 char_to_c '<' = ppPStr SLIT("Zl")
121 char_to_c '-' = ppPStr SLIT("Zm")
122 char_to_c '!' = ppPStr SLIT("Zn")
123 char_to_c '.' = ppPStr SLIT("Zo")
124 char_to_c '+' = ppPStr SLIT("Zp")
125 char_to_c '\'' = ppPStr SLIT("Zq")
126 char_to_c '*' = ppPStr SLIT("Zt")
127 char_to_c '_' = ppPStr SLIT("Zu")
129 char_to_c c = if isAlphanum c
131 else ppBeside (ppChar 'Z') (ppInt (ord c))
134 For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
135 chars) in the name. Rare.
141 if not (any quote_here str) then
144 _PK_ (concat (map char_to_c str))
146 quote_here '\'' = True
150 = if isAlphanum c then [c] else 'Z' : (show (ord c))