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