6 -- tjc: TODO: Copied straight out of Encoding.hs.
7 -- Ugh, maybe we can avoid this copy-pasta...
9 -- -----------------------------------------------------------------------------
13 This is the main name-encoding and decoding function. It encodes any
14 string into a string that is acceptable as a C name. This is done
15 right before we emit a symbol name into the compiled C or asm code.
16 Z-encoding of strings is cached in the FastString interface, so we
17 never encode the same string more than once.
19 The basic encoding scheme is this.
21 * Tuples (,,,) are coded as Z3T
23 * Alphabetic characters (upper and lower) and digits
24 all translate to themselves;
25 except 'Z', which translates to 'ZZ'
26 and 'z', which translates to 'zz'
27 We need both so that we can preserve the variable/tycon distinction
29 * Most other printable characters translate to 'zx' or 'Zx' for some
30 alphabetic character x
32 * The others translate as 'znnnU' where 'nnn' is the decimal number
36 --------------------------
48 (# #) Z1H unboxed 1-tuple (note the space)
49 (#,,,,#) Z5H unboxed 5-tuple
50 (NB: There is no Z1T nor Z0H.)
53 type UserString = String -- As the user typed it
54 type EncodedString = String -- Encoded form
57 zEncodeString :: UserString -> EncodedString
58 zEncodeString cs = case maybe_tuple cs of
59 Just n -> n -- Tuples go to Z2T etc
63 go (c:cs) = encode_ch c ++ go cs
65 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
66 unencodedChar 'Z' = False
67 unencodedChar 'z' = False
68 unencodedChar c = c >= 'a' && c <= 'z'
69 || c >= 'A' && c <= 'Z'
70 || c >= '0' && c <= '9'
72 encode_ch :: Char -> EncodedString
73 encode_ch c | unencodedChar c = [c] -- Common case first
76 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
77 encode_ch ')' = "ZR" -- For symmetry with (
103 encode_ch c = 'z' : if isDigit (head hex_str) then hex_str
105 where hex_str = showHex (ord c) "U"
106 -- ToDo: we could improve the encoding here in various ways.
107 -- eg. strings of unicode characters come out as 'z1234Uz5678U', we
108 -- could remove the 'U' in the middle (the 'z' works as a separator).
110 showHex = showIntAtBase 16 intToDigit
111 -- needed because prior to GHC 6.2, Numeric.showHex added a "0x" prefix
113 zDecodeString :: EncodedString -> UserString
114 zDecodeString [] = []
115 zDecodeString ('Z' : d : rest)
116 | isDigit d = decode_tuple d rest
117 | otherwise = decode_upper d : zDecodeString rest
118 zDecodeString ('z' : d : rest)
119 | isDigit d = decode_num_esc d rest
120 | otherwise = decode_lower d : zDecodeString rest
121 zDecodeString (c : rest) = c : zDecodeString rest
123 decode_upper, decode_lower :: Char -> Char
125 decode_upper 'L' = '('
126 decode_upper 'R' = ')'
127 decode_upper 'M' = '['
128 decode_upper 'N' = ']'
129 decode_upper 'C' = ':'
130 decode_upper 'Z' = 'Z'
131 decode_upper ch = {-pprTrace "decode_upper" (char ch)-} ch
133 decode_lower 'z' = 'z'
134 decode_lower 'a' = '&'
135 decode_lower 'b' = '|'
136 decode_lower 'c' = '^'
137 decode_lower 'd' = '$'
138 decode_lower 'e' = '='
139 decode_lower 'g' = '>'
140 decode_lower 'h' = '#'
141 decode_lower 'i' = '.'
142 decode_lower 'l' = '<'
143 decode_lower 'm' = '-'
144 decode_lower 'n' = '!'
145 decode_lower 'p' = '+'
146 decode_lower 'q' = '\''
147 decode_lower 'r' = '\\'
148 decode_lower 's' = '/'
149 decode_lower 't' = '*'
150 decode_lower 'u' = '_'
151 decode_lower 'v' = '%'
152 decode_lower ch = {-pprTrace "decode_lower" (char ch)-} ch
154 -- Characters not having a specific code are coded as z224U (in hex)
155 decode_num_esc :: Char -> EncodedString -> UserString
156 decode_num_esc d rest
157 = go (digitToInt d) rest
159 go n (c : rest) | isHexDigit c = go (16*n + digitToInt c) rest
160 go n ('U' : rest) = chr n : zDecodeString rest
161 go n other = error ("decode_num_esc: " ++ show n ++ ' ':other)
163 decode_tuple :: Char -> EncodedString -> UserString
165 = go (digitToInt d) rest
167 -- NB. recurse back to zDecodeString after decoding the tuple, because
168 -- the tuple might be embedded in a longer name.
169 go n (c : rest) | isDigit c = go (10*n + digitToInt c) rest
170 go 0 ('T':rest) = "()" ++ zDecodeString rest
171 go n ('T':rest) = '(' : replicate (n-1) ',' ++ ")" ++ zDecodeString rest
172 go 1 ('H':rest) = "(# #)" ++ zDecodeString rest
173 go n ('H':rest) = '(' : '#' : replicate (n-1) ',' ++ "#)" ++ zDecodeString rest
174 go n other = error ("decode_tuple: " ++ show n ++ ' ':other)
177 Tuples are encoded as
179 for 3-tuples or unboxed 3-tuples respectively. No other encoding starts
182 * "(# #)" is the tycon for an unboxed 1-tuple (not 0-tuple)
183 There are no unboxed 0-tuples.
185 * "()" is the tycon for a boxed 0-tuple.
186 There are no boxed 1-tuples.
189 maybe_tuple :: UserString -> Maybe EncodedString
191 maybe_tuple "(# #)" = Just("Z1H")
192 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
193 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
195 maybe_tuple "()" = Just("Z0T")
196 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
197 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
199 maybe_tuple _ = Nothing
201 count_commas :: Int -> String -> (Int, String)
202 count_commas n (',' : cs) = count_commas (n+1) cs
203 count_commas n cs = (n,cs)