Debian Codegraph @

Text/Encoding/Z.hs

raw
1 {-# LANGUAGE Safe #-}
2 {- | The Z-encoding
3
4 This is the main name-encoding and decoding function. It encodes any
5 string into a string that is acceptable as a C name. This is code was
6 originally part of GHC and used right before emitting a symbol name
7 into the compiled C or asm code. This library was created as this
8 encoding is useful when working with GHC compiled code or generally
9 when C-compatible name mangling is desired.
10
11 -}
12 module Text.Encoding.Z (
13 zEncodeString,
14 zDecodeString,
15 UserString,
16 EncodedString
17 ) where
18
19 import Data.Char
20 import Numeric
21
22 type UserString = String -- ^ As the user typed it
23 type EncodedString = String -- ^ Encoded form
24
25 -- | The basic encoding scheme is this:
26 --
27 -- * Tuples (,,,) are coded as Z3T
28 --
29 -- * Alphabetic characters (upper and lower) and digits
30 -- all translate to themselves;
31 -- except 'Z', which translates to 'ZZ'
32 -- and 'z', which translates to 'zz'
33 -- We need both so that we can preserve the variable/tycon distinction
34 --
35 -- * Most other printable characters translate to 'zx' or 'Zx' for some
36 -- alphabetic character x
37 --
38 -- * The others translate as 'znnnU' where 'nnn' is the decimal number
39 -- of the character
40 --
41 -- @
42 -- Before After
43 -- --------------------------
44 -- Trak Trak
45 -- foo_wib foozuwib
46 -- \> zg
47 -- \>1 zg1
48 -- foo\# foozh
49 -- foo\#\# foozhzh
50 -- foo\#\#1 foozhzh1
51 -- fooZ fooZZ
52 -- :+ ZCzp
53 -- () Z0T 0-tuple
54 -- (,,,,) Z5T 5-tuple
55 -- (\# \#) Z1H unboxed 1-tuple (note the space)
56 -- (\#,,,,\#) Z5H unboxed 5-tuple
57 -- (NB: There is no Z1T nor Z0H.)
58 -- @
59 zEncodeString :: UserString -> EncodedString
60 zEncodeString cs = case maybe_tuple cs of
61 Just n -> n -- Tuples go to Z2T etc
62 Nothing -> go cs
63 where
64 go [] = []
65 go (c:cs) = encode_digit_ch c ++ go' cs
66 go' [] = []
67 go' (c:cs) = encode_ch c ++ go' cs
68
69 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
70 unencodedChar 'Z' = False
71 unencodedChar 'z' = False
72 unencodedChar c = c >= 'a' && c <= 'z'
73 || c >= 'A' && c <= 'Z'
74 || c >= '0' && c <= '9'
75
76 -- If a digit is at the start of a symbol then we need to encode it.
77 -- Otherwise package names like 9pH-0.1 give linker errors.
78 encode_digit_ch :: Char -> EncodedString
79 encode_digit_ch c | c >= '0' && c <= '9' = encode_as_unicode_char c
80 encode_digit_ch c | otherwise = encode_ch c
81
82 encode_ch :: Char -> EncodedString
83 encode_ch c | unencodedChar c = [c] -- Common case first
84
85 -- Constructors
86 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
87 encode_ch ')' = "ZR" -- For symmetry with (
88 encode_ch '[' = "ZM"
89 encode_ch ']' = "ZN"
90 encode_ch ':' = "ZC"
91 encode_ch 'Z' = "ZZ"
92
93 -- Variables
94 encode_ch 'z' = "zz"
95 encode_ch '&' = "za"
96 encode_ch '|' = "zb"
97 encode_ch '^' = "zc"
98 encode_ch '$' = "zd"
99 encode_ch '=' = "ze"
100 encode_ch '>' = "zg"
101 encode_ch '#' = "zh"
102 encode_ch '.' = "zi"
103 encode_ch '<' = "zl"
104 encode_ch '-' = "zm"
105 encode_ch '!' = "zn"
106 encode_ch '+' = "zp"
107 encode_ch '\'' = "zq"
108 encode_ch '\\' = "zr"
109 encode_ch '/' = "zs"
110 encode_ch '*' = "zt"
111 encode_ch '_' = "zu"
112 encode_ch '%' = "zv"
113 encode_ch c = encode_as_unicode_char c
114
115 encode_as_unicode_char :: Char -> EncodedString
116 encode_as_unicode_char c = 'z' : if isDigit (head hex_str) then hex_str
117 else '0':hex_str
118 where hex_str = showHex (ord c) "U"
119 -- ToDo: we could improve the encoding here in various ways.
120 -- eg. strings of unicode characters come out as 'z1234Uz5678U', we
121 -- could remove the 'U' in the middle (the 'z' works as a separator).
122
123 -- | The inverse of 'zEncodeString'
124 zDecodeString :: EncodedString -> UserString
125 zDecodeString [] = []
126 zDecodeString ('Z' : d : rest)
127 | isDigit d = decode_tuple d rest
128 | otherwise = decode_upper d : zDecodeString rest
129 zDecodeString ('z' : d : rest)
130 | isDigit d = decode_num_esc d rest
131 | otherwise = decode_lower d : zDecodeString rest
132 zDecodeString (c : rest) = c : zDecodeString rest
133
134 decode_upper, decode_lower :: Char -> Char
135
136 decode_upper 'L' = '('
137 decode_upper 'R' = ')'
138 decode_upper 'M' = '['
139 decode_upper 'N' = ']'
140 decode_upper 'C' = ':'
141 decode_upper 'Z' = 'Z'
142 decode_upper ch = {-pprTrace "decode_upper" (char ch)-} ch
143
144 decode_lower 'z' = 'z'
145 decode_lower 'a' = '&'
146 decode_lower 'b' = '|'
147 decode_lower 'c' = '^'
148 decode_lower 'd' = '$'
149 decode_lower 'e' = '='
150 decode_lower 'g' = '>'
151 decode_lower 'h' = '#'
152 decode_lower 'i' = '.'
153 decode_lower 'l' = '<'
154 decode_lower 'm' = '-'
155 decode_lower 'n' = '!'
156 decode_lower 'p' = '+'
157 decode_lower 'q' = '\''
158 decode_lower 'r' = '\\'
159 decode_lower 's' = '/'
160 decode_lower 't' = '*'
161 decode_lower 'u' = '_'
162 decode_lower 'v' = '%'
163 decode_lower ch = {-pprTrace "decode_lower" (char ch)-} ch
164
165 -- Characters not having a specific code are coded as z224U (in hex)
166 decode_num_esc :: Char -> EncodedString -> UserString
167 decode_num_esc d rest
168 = go (digitToInt d) rest
169 where
170 go n (c : rest) | isHexDigit c = go (16*n + digitToInt c) rest
171 go n ('U' : rest) = chr n : zDecodeString rest
172 go n other = error ("decode_num_esc: " ++ show n ++ ' ':other)
173
174 decode_tuple :: Char -> EncodedString -> UserString
175 decode_tuple d rest
176 = go (digitToInt d) rest
177 where
178 -- NB. recurse back to zDecodeString after decoding the tuple, because
179 -- the tuple might be embedded in a longer name.
180 go n (c : rest) | isDigit c = go (10*n + digitToInt c) rest
181 go 0 ('T':rest) = "()" ++ zDecodeString rest
182 go n ('T':rest) = '(' : replicate (n-1) ',' ++ ")" ++ zDecodeString rest
183 go 1 ('H':rest) = "(# #)" ++ zDecodeString rest
184 go n ('H':rest) = '(' : '#' : replicate (n-1) ',' ++ "#)" ++ zDecodeString rest
185 go n other = error ("decode_tuple: " ++ show n ++ ' ':other)
186
187 {-
188 Tuples are encoded as
189 Z3T or Z3H
190 for 3-tuples or unboxed 3-tuples respectively. No other encoding starts
191 Z<digit>
192
193 * "(# #)" is the tycon for an unboxed 1-tuple (not 0-tuple)
194 There are no unboxed 0-tuples.
195
196 * "()" is the tycon for a boxed 0-tuple.
197 There are no boxed 1-tuples.
198 -}
199
200 maybe_tuple :: UserString -> Maybe EncodedString
201
202 maybe_tuple "(# #)" = Just("Z1H")
203 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
204 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
205 _ -> Nothing
206 maybe_tuple "()" = Just("Z0T")
207 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
208 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
209 _ -> Nothing
210 maybe_tuple _ = Nothing
211
212 count_commas :: Int -> String -> (Int, String)
213 count_commas n (',' : cs) = count_commas (n+1) cs
214 count_commas n cs = (n,cs)