Debian Codegraph @

c2hs/base/syms/Idents.hs

raw symbols SCIP
1 -- Compiler Toolkit: identifiers
2 --
3 -- Author : Manuel M. T. Chakravarty
4 -- Created: 14 February 95
5 --
6 -- Version $Revision: 1.2 $ from $Date: 2004/11/13 17:26:50 $
7 --
8 -- Copyright (c) [1995..1999] Manuel M. T. Chakravarty
9 --
10 -- This file is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; either version 2 of the License, or
13 -- (at your option) any later version.
14 --
15 -- This file is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 --- DESCRIPTION ---------------------------------------------------------------
21 --
22 -- This module provides an abstract notion of identifiers.
23 --
24 --- DOCU ----------------------------------------------------------------------
25 --
26 -- language: Haskell 98
27 --
28 -- * We speed up the equality test between identifiers by assigning an
29 -- identification number to each of them, and providing a special equality
30 -- that compares the lexemes only if the identification numbers are equal.
31 --
32 -- * The ordering relation on identifiers is also oriented at the
33 -- identification number and, hence, does *not* follow the alphanumerical
34 -- ordering of the lexemes of the identifiers. Instead, it provides a fast
35 -- ordering when identifiers are used as keys in a `Map'.
36 --
37 -- * The ambiguousness resolving number of an identifier is `-1' when no such
38 -- number is present (so, such identifiers are distinguished from
39 -- identifiers that share the front part of the lexeme while having an
40 -- ambiguousness resolving number).
41 --
42 -- The ambiguousness resolving number of primitive identifiers (`pid' in the
43 -- grammar contained in the KCode definition) is `-2' (this gives primitive
44 -- identifiers a distinct name space).
45 --
46 -- * Attributes may be associated to identifiers, except with `OnlyPos'
47 -- identifiers, which have a position as their only attribute (they do not
48 -- carry an attribute identifier, which can be used to index attribute
49 -- tables).
50 --
51 -- * Internal identifiers that are forming a completely unique name space are
52 -- supported. But note, they do not have a proper lexeme, i.e., they are not
53 -- suited for code generation.
54 --
55 --- TODO ----------------------------------------------------------------------
56 --
57 -- * Hashing is not 8bit clean.
58 --
59
60 module Idents (Ident, noARNum, isLegalIdent, lexemeToIdent, internalIdent,
61 onlyPosIdent, cloneIdent, identToLexeme, isIdentSimple,
62 isIdentPrim, stripIdentARNum, getIdentARNum, newIdentARNum,
63 getIdentAttrs, dumpIdent)
64 where
65
66 import Data.Char
67 import Position (Position, Pos(posOf), nopos)
68 import UNames (Name)
69 import Errors (interr)
70 import Attributes (Attrs, newAttrsOnlyPos, newAttrs,
71 Attributed(attrsOf), posOfAttrsOf)
72 import Binary (Binary(..), putSharedString, getSharedString)
73
74
75 -- simple identifier representation (EXPORTED)
76 --
77 -- identifiers without an ambiguousness resolving number get `noARNum' as
78 -- number
79 --
80 data Ident = Ident String -- lexeme
81 !Int -- ambiguousness resolving number
82 !Int -- id. number to speed up equality check
83 !Attrs -- attributes of this ident. incl. position
84
85 -- the definition of the equality allows identifiers to be equal that are
86 -- defined at different source text positions, and aims at speeding up the
87 -- equality test, by comparing the lexemes only if the two numbers are equal
88 --
89 instance Eq Ident where
90 (Ident s k id _) == (Ident s' k' id' _) = (k == k')
91 && (id == id')
92 && (s == s')
93
94 -- this does *not* follow the alphanumerical ordering of the lexemes
95 --
96 instance Ord Ident where
97 (Ident s k id _) < (Ident s' k' id' _) = (k < k')
98 || ((k == k') && (id < id'))
99 || ((k == k') && (id == id')
100 && (s < s'))
101 id1 <= id2 = (id1 < id2) || (id1 == id2)
102
103 -- for displaying identifiers
104 --
105 instance Show Ident where
106 showsPrec _ ide = showString ("`" ++ identToLexeme ide ++ "'")
107
108 -- identifiers are attributed
109 --
110 instance Attributed Ident where
111 attrsOf (Ident _ _ _ at) = at
112
113 -- identifiers have a canonical position
114 --
115 instance Pos Ident where
116 posOf = posOfAttrsOf
117
118 -- to speed up the equality test we compute some hash-like value for each
119 -- identifiers lexeme and store it in the identifiers representation
120
121 -- hash function from the dragon book pp437; assumes 7 bit characters and needs
122 -- the (nearly) full range of values guaranteed for `Int' by the Haskell
123 -- language definition; can handle 8 bit characters provided we have 29 bit
124 -- for the `Int's without sign
125 --
126 quad :: String -> Int
127 quad (c1:c2:c3:c4:s) = ((ord c4 * bits21
128 + ord c3 * bits14
129 + ord c2 * bits7
130 + ord c1)
131 `mod` bits28)
132 + (quad s `mod` bits28)
133 quad (c1:c2:c3:[] ) = ord c3 * bits14 + ord c2 * bits7 + ord c1
134 quad (c1:c2:[] ) = ord c2 * bits7 + ord c1
135 quad (c1:[] ) = ord c1
136 quad ([] ) = 0
137
138 bits7 = 2^7
139 bits14 = 2^14
140 bits21 = 2^21
141 bits28 = 2^28
142
143 -- used as a substitute for the ambiguousness resolving number if it is not
144 -- present (EXPORTED)
145 --
146 noARNum :: Int
147 noARNum = -1
148
149 -- used as the ambiguousness resolving number for primitive identifiers
150 --
151 primARNum :: Int
152 primARNum = -2
153
154 -- used as the ambiguousness resolving number for internal identifiers
155 --
156 internARNum :: Int
157 internARNum = -3
158
159 -- checks whether the given lexeme is a legal identifier (EXPORTED)
160 --
161 isLegalIdent :: String -> Bool
162 isLegalIdent [] = False
163 isLegalIdent (c:cs) = if c == '`' then isQualIdent cs
164 else (isAlpha c || c == '_') && isIdent (c:cs)
165 where
166 isIdent = checkTail . (dropWhile isAlphaNumOrUS)
167
168 checkTail [] = True
169 checkTail ("##") = True
170 checkTail ('#':cs') = all isDigit cs'
171 checkTail _ = False
172
173 isAlphaNumOrUS c = isAlphaNum c || (c == '_')
174 isAlphaNum c = isAlpha c || isNum c
175 isAlpha c = c `elem` ['a'..'z'] ++ ['A'..'Z']
176 isNum c = c `elem` ['0'..'9']
177
178 isQualIdent cs = let
179 cs' = skip cs
180 in
181 (not . null) cs'
182 && (checkTail . tail) cs'
183
184 skip [] = []
185 skip ('\'':cs) = '\'':cs
186 skip ('\\':cs) = case cs of
187 ('\'':cs') -> skip cs'
188 ('\\':cs') -> skip cs'
189 _ -> skip cs
190 skip (c :cs) = skip cs
191
192 -- given the lexeme of an identifier, yield the abstract identifier (EXPORTED)
193 --
194 -- * the only attribute of the resulting identifier is its source text
195 -- position; as provided in the first argument of this function
196 --
197 -- * only minimal error checking, e.g., the characters of the identifier are
198 -- not checked for being alphanumerical only; the correct lexis of the
199 -- identifier should be ensured by the caller, e.g., the scanner or
200 -- `isLegalIdent'
201 --
202 -- * for reasons of simplicity the complete lexeme is hashed (with `quad')
203 --
204 lexemeToIdent :: Position -> String -> Name -> Ident
205 lexemeToIdent pos l name = Ident s k (quad s) (newAttrs pos name)
206 where
207 (s, k) = parseIdent pos l
208
209 -- generate an internal identifier (has no position and cannot be asccociated
210 -- with attributes) (EXPORTED)
211 --
212 internalIdent :: String -> Ident
213 internalIdent s = Ident s internARNum (quad s) (newAttrsOnlyPos nopos)
214
215 -- generate a `only pos' identifier (may not be used to index attribute
216 -- tables, but has a position value) (EXPORTED)
217 --
218 onlyPosIdent :: Position -> String -> Ident
219 onlyPosIdent pos l = Ident s k (quad s) (newAttrsOnlyPos pos)
220 where
221 (s, k) = parseIdent pos l
222
223 -- Extract the name and ambiguousness resolving number from a lexeme.
224 --
225 parseIdent :: Position -> String -> (String, Int)
226 parseIdent pos l
227 = if (null l)
228 then
229 interr $ "Idents: lexemeToIdent: Empty lexeme! " ++ show pos
230 else
231 if (head l == '\'')
232 then
233 parseQuoted (tail l)
234 else
235 parseNorm l
236 where
237 -- parse lexeme without quotes
238 --
239 parseNorm [] = ([], noARNum)
240 parseNorm ("##") = ([], primARNum)
241 parseNorm ('#':cs) = ([], ((read . check) cs)::Int)
242 parseNorm (c :cs) = let
243 (cs', k) = parseNorm cs
244 in
245 (c:cs', k)
246
247 check [] = interr "Idents: lexemeToIdent: Missing\
248 \ number!"
249 check ('-':cs) = interr "Idents: lexemeToIdent: Illegal\
250 \ negative number!"
251 check s = s
252
253 -- parse lexeme with quotes
254 --
255 parseQuoted [] = interr endInQuotes
256 parseQuoted ('\\':cs) = parseSpecial cs
257 parseQuoted ('\'':cs) = let
258 (rmd, k) = parseNorm cs
259 in
260 if (null rmd) then ([], k)
261 else interr afterQuotes
262 parseQuoted (c :cs) = let
263 (cs', k) = parseQuoted cs
264 in
265 (c:cs', k)
266
267 endInQuotes = "Idents: lexemeToIdent: Unexpected end of\
268 \ lexeme (in quotes)!"
269
270 afterQuotes = "Idents: lexemeToIdent: Superfluous\
271 \ characters after quotes!"
272
273 endInSpecial = "Idents: lexemeToIdent: Unexpected end of\
274 \ lexeme (in escape sequence)!"
275
276 illegalSpecial = "Idents: lexemeToIdent: Illegal escape\
277 \ sequence!"
278
279 -- parse single escaped character, then continue with
280 -- `parseQuoted'
281 --
282 parseSpecial [] = interr endInSpecial
283 parseSpecial (c1:c2:c3:cs)
284 | isDigit c1
285 && isDigit c2
286 && isDigit c3 = let
287 (cs', k) = parseQuoted cs
288 ord0 = ord '0'
289 d1 = ord c1 - ord0
290 d2 = ord c2 - ord0
291 d3 = ord c3 - ord0
292 in
293 (chr (100*d1 + 10*d2 + d3)
294 :cs', k)
295 parseSpecial (c:cs)
296 | c == '\\' = ('\\':cs', k)
297 | c == '\"' = ('\"':cs', k)
298 | c == '\'' = ('\'':cs', k)
299 | c == 'n' = ('\n':cs', k)
300 | c == 't' = ('\t':cs', k)
301 | c == 'r' = ('\r':cs', k)
302 where
303 (cs', k) = parseQuoted cs
304 parseSpecial _ = interr illegalSpecial
305
306 -- create an identifier identical to the given one, but with its own set of
307 -- attributes (EXPORTED)
308 --
309 cloneIdent :: Ident -> Name -> Ident
310 cloneIdent (Ident s k idnum at) name =
311 Ident s k idnum (newAttrs (posOf at) name)
312
313 -- given an abstract identifier, yield its lexeme (EXPORTED)
314 --
315 identToLexeme :: Ident -> String
316 identToLexeme (Ident s k _ _) = s ++ suffix
317 where
318 suffix = if (k == noARNum)
319 then ""
320 else if (k == primARNum)
321 then "##"
322 else if (k == internARNum)
323 then "<internal>"
324 else "#" ++ show k
325
326 -- test if the given identifier is simple, i.e., has no ambiguousness
327 -- resolving number and is not a primitive identifier (EXPORTED)
328 --
329 isIdentSimple :: Ident -> Bool
330 isIdentSimple (Ident _ k _ _) = k == noARNum
331
332 -- test if the given identifier is a primitive identifier (EXPORTED)
333 --
334 isIdentPrim :: Ident -> Bool
335 isIdentPrim (Ident _ k _ _) = k == primARNum
336
337 -- remove ambiguousness resolving of an identifier (EXPORTED)
338 --
339 -- NOTE: The new identifier will not be equal (==) to the old one!
340 --
341 stripIdentARNum :: Ident -> Ident
342 stripIdentARNum (Ident s k id at)
343 | k == primARNum || k == internARNum = interr "Idents: stripIdentARNum: \
344 \Not allowed!"
345 | otherwise = Ident s noARNum id at
346
347 -- get the ambiguousness resolving of an identifier (EXPORTED)
348 --
349 getIdentARNum :: Ident -> Int
350 getIdentARNum (Ident s k id at)
351 | k == primARNum || k == internARNum = interr "Idents: getIdentARNum: \
352 \Not allowed!"
353 | otherwise = k
354
355 -- enter a new ambiguousness resolving into the identifier (EXPORTED)
356 --
357 -- NOTE: The new identifier will not be equal (==) to the old one!
358 --
359 newIdentARNum :: Ident -> Int -> Ident
360 newIdentARNum (Ident s k id at) k'
361 | k' < 0 = interr "Idents: newIdentARNum: \
362 \Negative number!"
363 | k == primARNum || k == internARNum = interr "Idents: newIdentARNum: \
364 \Not allowed!"
365 | otherwise = Ident s k' id at
366
367 -- get the attribute identifier associated with the given identifier (EXPORTED)
368 --
369 getIdentAttrs :: Ident -> Attrs
370 getIdentAttrs (Ident _ _ _ as) = as
371
372 -- dump the lexeme and its positions into a string for debugging purposes
373 -- (EXPORTED)
374 --
375 dumpIdent :: Ident -> String
376 dumpIdent ide = identToLexeme ide ++ " at " ++ show (posOf ide)
377
378
379 {-! for Ident derive : GhcBinary !-}
380 {-* Generated by DrIFT : Look, but Don't Touch. *-}
381 instance Binary Ident where
382 put_ bh (Ident aa ab ac ad) = do
383 putSharedString bh aa
384 -- put_ bh aa
385 put_ bh ab
386 put_ bh ac
387 put_ bh ad
388 get bh = do
389 aa <- getSharedString bh
390 -- aa <- get bh
391 ab <- get bh
392 ac <- get bh
393 ad <- get bh
394 return (Ident aa ab ac ad)