Mercurial > projects > dil
comparison trunk/src/dil/lexer/IdTable.d @ 786:3b34f6a95a27
Added and revised documenation comments.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 24 Feb 2008 02:41:11 +0100 |
parents | 5e3ef1b2011c |
children | cf2ad5df025c |
comparison
equal
deleted
inserted
replaced
785:57ef69eced96 | 786:3b34f6a95a27 |
---|---|
31 { | 31 { |
32 static: | 32 static: |
33 /// A set of common, predefined identifiers for fast lookups. | 33 /// A set of common, predefined identifiers for fast lookups. |
34 private Identifier*[string] staticTable; | 34 private Identifier*[string] staticTable; |
35 /// A table that grows with every newly found, unique identifier. | 35 /// A table that grows with every newly found, unique identifier. |
36 /// Access must be synchronized. | |
37 private Identifier*[string] growingTable; | 36 private Identifier*[string] growingTable; |
38 | 37 |
39 /// Initializes the static table. | 38 /// Loads keywords and predefined identifiers into the static table. |
40 static this() | 39 static this() |
41 { | 40 { |
42 // Load keywords and pre-defined identifiers into the static table. | |
43 foreach (ref k; keywords) | 41 foreach (ref k; keywords) |
44 staticTable[k.str] = &k; | 42 staticTable[k.str] = &k; |
45 foreach (id; Ident.allIds()) | 43 foreach (id; Ident.allIds()) |
46 staticTable[id.str] = id; | 44 staticTable[id.str] = id; |
47 staticTable.rehash; | 45 staticTable.rehash; |
48 } | 46 } |
49 | 47 |
50 /// Looks in both tables. | 48 /// Looks up idString in both tables. |
51 Identifier* lookup(string idString) | 49 Identifier* lookup(string idString) |
52 { | 50 { |
53 auto id = inStatic(idString); | 51 auto id = inStatic(idString); |
54 if (id) | 52 if (id) |
55 return id; | 53 return id; |
56 return inGrowing(idString); | 54 return inGrowing(idString); |
57 } | 55 } |
58 | 56 |
59 /// Look up idString in the static table. | 57 /// Looks up idString in the static table. |
60 Identifier* inStatic(string idString) | 58 Identifier* inStatic(string idString) |
61 { | 59 { |
62 auto id = idString in staticTable; | 60 auto id = idString in staticTable; |
63 return id ? *id : null; | 61 return id ? *id : null; |
64 } | 62 } |
65 | 63 |
66 alias Identifier* function(string idString) LookupFunction; | 64 alias Identifier* function(string idString) LookupFunction; |
67 /// Look up idString in the growing table. | 65 /// Looks up idString in the growing table. |
68 LookupFunction inGrowing = &_inGrowing_unsafe; // Default to unsafe function. | 66 LookupFunction inGrowing = &_inGrowing_unsafe; // Default to unsafe function. |
69 | 67 |
70 /++ | 68 /// Sets the thread safety mode of the growing table. |
71 Set the thread safety mode of this table. | |
72 Call this function only if you can be sure | |
73 that this table is not being accessed | |
74 (like during lexing, parsing and semantic phase.) | |
75 +/ | |
76 void setThreadsafe(bool b) | 69 void setThreadsafe(bool b) |
77 { | 70 { |
78 if (b) | 71 if (b) |
79 IdTable.inGrowing = &_inGrowing_safe; | 72 inGrowing = &_inGrowing_safe; |
80 else | 73 else |
81 IdTable.inGrowing = &_inGrowing_unsafe; | 74 inGrowing = &_inGrowing_unsafe; |
82 } | 75 } |
83 | 76 |
84 /++ | 77 /// Returns true if access to the growing table is thread-safe. |
85 Returns the Identifier for idString. | 78 bool isThreadsafe() |
86 Adds idString to the table if not found. | 79 { |
87 +/ | 80 return inGrowing is &_inGrowing_safe; |
81 } | |
82 | |
83 /// Looks up idString in the table. | |
84 /// | |
85 /// Adds idString to the table if not found. | |
88 private Identifier* _inGrowing_unsafe(string idString) | 86 private Identifier* _inGrowing_unsafe(string idString) |
89 out(id) | 87 out(id) |
90 { assert(id !is null); } | 88 { assert(id !is null); } |
91 body | 89 body |
92 { | 90 { |
96 auto newID = Identifier(idString, TOK.Identifier); | 94 auto newID = Identifier(idString, TOK.Identifier); |
97 growingTable[idString] = newID; | 95 growingTable[idString] = newID; |
98 return newID; | 96 return newID; |
99 } | 97 } |
100 | 98 |
101 /++ | 99 /// Looks up idString in the table. |
102 Returns the Identifier for idString. | 100 /// |
103 Adds idString to the table if not found. | 101 /// Adds idString to the table if not found. |
104 Access to the data structure is synchronized. | 102 /// Access to the data structure is synchronized. |
105 +/ | |
106 private Identifier* _inGrowing_safe(string idString) | 103 private Identifier* _inGrowing_safe(string idString) |
107 { | 104 { |
108 synchronized | 105 synchronized |
109 return _inGrowing_unsafe(idString); | 106 return _inGrowing_unsafe(idString); |
110 } | 107 } |
125 ids[i] = *id; | 122 ids[i] = *id; |
126 } | 123 } |
127 } | 124 } |
128 +/ | 125 +/ |
129 | 126 |
130 static uint anonCount; | 127 static uint anonCount; /// Counter for anonymous identifiers. |
131 Identifier* genAnonymousID(char[] prefix) | 128 |
129 /// Generates an anonymous identifier. | |
130 /// | |
131 /// Concatenates prefix with anonCount. | |
132 /// The identifier is not inserted into the table. | |
133 Identifier* genAnonymousID(string prefix) | |
132 { | 134 { |
133 ++anonCount; | 135 ++anonCount; |
134 auto x = anonCount; | 136 auto x = anonCount; |
135 // Convert count to a string and append it to str. | 137 // Convert count to a string and append it to str. |
136 char[] num; | 138 char[] num; |
138 num = cast(char)('0' + (x % 10)) ~ num; | 140 num = cast(char)('0' + (x % 10)) ~ num; |
139 while (x /= 10) | 141 while (x /= 10) |
140 return Identifier(prefix ~ num, TOK.Identifier); | 142 return Identifier(prefix ~ num, TOK.Identifier); |
141 } | 143 } |
142 | 144 |
145 /// Generates an identifier for an anonymous enum. | |
143 Identifier* genAnonEnumID() | 146 Identifier* genAnonEnumID() |
144 { | 147 { |
145 return genAnonymousID("__anonenum"); | 148 return genAnonymousID("__anonenum"); |
146 } | 149 } |
147 } | 150 } |