1 This module deals with printing (a) C string literals and (b) C labels.
15 #include "HsVersions.h"
17 import Char ( isAlphanum, ord, chr )
23 _ is the main separator
29 <funny char> Z[hex-digit][hex-digit]
36 cSEP = SLIT("_") -- official C separator
39 identToC :: FAST_STRING -> SDoc
40 modnameToC :: FAST_STRING -> FAST_STRING
41 stringToC :: String -> String
42 charToC, charToEasyHaskell :: Char -> String
44 -- stringToC: the hassle is what to do w/ strings like "ESC 0"...
47 stringToC [c] = charToC c
49 -- if we have something "octifiable" in "c", we'd better "octify"
50 -- the rest of the string, too.
51 = if (c < ' ' || c > '~')
52 then (charToC c) ++ (concat (map char_to_C cs))
53 else (charToC c) ++ (stringToC cs)
55 char_to_C c | c == '\n' = "\\n" -- use C escapes when we can
57 | c == '\b' = "\\b" -- ToDo: chk some of these...
62 | otherwise = '\\' : (octify (ord c))
64 charToC c = if (c >= ' ' && c <= '~') -- non-portable...
77 else '\\' : (octify (ord c))
79 -- really: charToSimpleHaskell
82 = if (c >= 'a' && c <= 'z')
83 || (c >= 'A' && c <= 'Z')
84 || (c >= '0' && c <= '9')
87 _ -> '\\' : show (ord c)
89 octify :: Int -> String
94 octify (n `quot` 8) ++ [chr (n `rem` 8 + ord '0')]
102 's':'t':'d':_ -> -- avoid "stdin", "stdout", and "stderr"...
106 (if (all isAlphanum str) -- we gamble that this test will succeed...
108 else hcat (map char_to_c str))
110 char_to_c 'Z' = ptext SLIT("ZZ")
111 char_to_c '&' = ptext SLIT("Za")
112 char_to_c '|' = ptext SLIT("Zb")
113 char_to_c ':' = ptext SLIT("Zc")
114 char_to_c '/' = ptext SLIT("Zd")
115 char_to_c '=' = ptext SLIT("Ze")
116 char_to_c '>' = ptext SLIT("Zg")
117 char_to_c '#' = ptext SLIT("Zh")
118 char_to_c '<' = ptext SLIT("Zl")
119 char_to_c '-' = ptext SLIT("Zm")
120 char_to_c '!' = ptext SLIT("Zn")
121 char_to_c '.' = ptext SLIT("_")
122 char_to_c '+' = ptext SLIT("Zp")
123 char_to_c '\'' = ptext SLIT("Zq")
124 char_to_c '*' = ptext SLIT("Zt")
125 char_to_c '_' = ptext SLIT("Zu")
127 char_to_c c = if isAlphanum c
129 else (<>) (char 'Z') (int (ord c))
132 For \tr{modnameToC}, we really only have to worry about \tr{'}s (quote
133 chars) in the name. Rare.
139 if not (any quote_here str) then
142 _PK_ (concat (map char_to_c str))
144 quote_here '\'' = True
148 = if isAlphanum c then [c] else 'Z' : (show (ord c))