comparison trunk/src/dil/IdTable.d @ 502:4e14cd1b24da

Refactored code and added modules related to tabulated Identifiers. Rearranged members of struct Identifier and added new member ID identID. Moved idTableLookup to module dil.IdTable. Renamed module TokenIDs to TokensEnum. Added member Identifier* ident to struct Token. Changed string switchtes in Parser to integer switches using enum ID.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 11 Dec 2007 14:19:30 +0100
parents
children 9076c4cea2a4
comparison
equal deleted inserted replaced
501:949a53332c66 502:4e14cd1b24da
1 /++
2 Author: Aziz Köksal
3 License: GPL3
4 +/
5 module dil.IdTable;
6 import dil.Identifier;
7 import dil.IdentsEnum;
8 import dil.TokensEnum;
9 import dil.IdentsGenerator;
10 import dil.Keywords;
11 import common;
12
13 struct Ident
14 {
15 const static
16 {
17 mixin(generateIdentMembers());
18 }
19
20 static Identifier*[] allIds()
21 {
22 return __allIds;
23 }
24 }
25
26 struct IdTable
27 {
28 static:
29 /// A set of common, predefined identifiers for fast lookups.
30 private Identifier*[string] staticTable;
31 /// A table that grows with every newly found, unique identifier.
32 /// Access must be synchronized.
33 private Identifier*[string] growingTable;
34
35 /// Initializes the static table.
36 static this()
37 {
38 foreach (ref k; keywords)
39 staticTable[k.str] = &k;
40 foreach (id; Ident.allIds())
41 staticTable[id.str] = id;
42 staticTable.rehash;
43 }
44
45 /// Looks in both tables.
46 Identifier* lookup(string idString)
47 {
48 auto id = inStatic(idString);
49 if (id)
50 return id;
51 return inGrowing(idString);
52 }
53
54 /// Look up idString in the static table.
55 Identifier* inStatic(string idString)
56 {
57 auto id = idString in staticTable;
58 return id ? *id : null;
59 }
60
61 /++
62 Returns the Identifier for idString.
63 Adds idString to the table if not found.
64 Access to the data structure is synchronized.
65 +/
66 Identifier* inGrowing(string idString)
67 out(id)
68 { assert(id !is null); }
69 body
70 {
71 synchronized
72 {
73 auto id = idString in growingTable;
74 if (id)
75 return *id;
76 auto newID = Identifier(idString, TOK.Identifier);
77 growingTable[idString] = newID;
78 return newID;
79 }
80 }
81
82 /+
83 Identifier* addIdentifiers(char[][] idStrings)
84 {
85 auto ids = new Identifier*[idStrings.length];
86 foreach (i, idString; idStrings)
87 {
88 Identifier** id = idString in tabulatedIds;
89 if (!id)
90 {
91 auto newID = Identifier(TOK.Identifier, idString);
92 tabulatedIds[idString] = newID;
93 id = &newID;
94 }
95 ids[i] = *id;
96 }
97 }
98 +/
99 }