Debian Codegraph @

gcc/d/dmd/common/charactertables.d

raw
1 /**
2 * Character tables related to identifiers.
3 *
4 * Supports UAX31, C99, C11 and least restrictive (All).
5 *
6 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://cattermole.co.nz, Richard (Rikki) Andrew Cattermole)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/common/charactertables.d, common/charactertables.d)
10 * Documentation: https://dlang.org/phobos/dmd_common_charactertables.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/common/charactertables.d
12 */
13 module dmd.common.charactertables;
14
15 @safe nothrow @nogc pure:
16
17 extern(C++):
18
19 ///
20 enum IdentifierTable {
21 UAX31, ///
22 C99, ///
23 C11, ///
24 LR, /// Least Restrictive aka All
25 }
26
27 ///
28 struct IdentifierCharLookup
29 {
30 @safe nothrow @nogc pure:
31
32 ///
33 extern(C++) bool function(dchar) isStart;
34 ///
35 extern(C++) bool function(dchar) isContinue;
36
37 /// Lookup the table given the table name
38 extern(C++) static IdentifierCharLookup forTable(IdentifierTable table)
39 {
40 import dmd.common.identifiertables;
41
42 // Awful solution to require these lambdas.
43 // However without them the extern(C++) ABI issues crop up for isInRange,
44 // and then it can't access the tables.
45 final switch(table)
46 {
47 case IdentifierTable.UAX31:
48 return IdentifierCharLookup(
49 (c) => isInRange!UAX31_Start(c),
50 (c) => isInRange!UAX31_Continue(c));
51 case IdentifierTable.C99:
52 return IdentifierCharLookup(
53 (c) => isInRange!FixedTable_C99_Start(c),
54 (c) => isInRange!FixedTable_C99_Continue(c));
55 case IdentifierTable.C11:
56 return IdentifierCharLookup(
57 (c) => isInRange!FixedTable_C11_Start(c),
58 (c) => isInRange!FixedTable_C11_Continue(c));
59 case IdentifierTable.LR:
60 return IdentifierCharLookup(
61 (c) => isInRange!LeastRestrictive_Start(c),
62 (c) => isInRange!LeastRestrictive_Continue(c));
63 }
64 }
65 }
66
67 /**
68 Convenience function for use in places where we just don't care,
69 what the identifier ranges are, or if it is start/continue.
70
71 Returns: is character a member of least restrictive of all.
72 */
73 bool isAnyIdentifierCharacter(dchar c)
74 {
75 import dmd.common.identifiertables;
76 return isInRange!LeastRestrictive_OfAll(c);
77 }
78
79 ///
80 unittest
81 {
82 assert(isAnyIdentifierCharacter('ğ'));
83 }
84
85 /**
86 Convenience function for use in places where we just don't care,
87 what the identifier ranges are.
88
89 Returns: is character a member of restrictive Start
90 */
91 bool isAnyStart(dchar c)
92 {
93 import dmd.common.identifiertables;
94 return isInRange!LeastRestrictive_Start(c);
95 }
96
97 ///
98 unittest
99 {
100 assert(isAnyStart('ğ'));
101 }
102
103 /**
104 Convenience function for use in places where we just don't care,
105 what the identifier ranges are.
106
107 Returns: is character a member of least restrictive Continue
108 */
109 bool isAnyContinue(dchar c)
110 {
111 import dmd.common.identifiertables;
112 return isInRange!LeastRestrictive_Continue(c);
113 }
114
115 ///
116 unittest
117 {
118 assert(isAnyContinue('ğ'));
119 }
120
121 /// UTF line separator
122 enum LS = 0x2028;
123 /// UTF paragraph separator
124 enum PS = 0x2029;
125
126 private
127 {
128 enum CMoctal = 0x1;
129 enum CMhex = 0x2;
130 enum CMidchar = 0x4;
131 enum CMzerosecond = 0x8;
132 enum CMdigitsecond = 0x10;
133 enum CMsinglechar = 0x20;
134 }
135
136 ///
137 bool isoctal(const char c)
138 {
139 return (cmtable[c] & CMoctal) != 0;
140 }
141
142 ///
143 bool ishex(const char c)
144 {
145 return (cmtable[c] & CMhex) != 0;
146 }
147
148 ///
149 bool isidchar(const char c)
150 {
151 return (cmtable[c] & CMidchar) != 0;
152 }
153
154 ///
155 bool isZeroSecond(const char c)
156 {
157 return (cmtable[c] & CMzerosecond) != 0;
158 }
159
160 ///
161 bool isDigitSecond(const char c)
162 {
163 return (cmtable[c] & CMdigitsecond) != 0;
164 }
165
166 ///
167 bool issinglechar(const char c)
168 {
169 return (cmtable[c] & CMsinglechar) != 0;
170 }
171
172 ///
173 bool c_isxdigit(const int c)
174 {
175 return (( c >= '0' && c <= '9') ||
176 ( c >= 'a' && c <= 'f') ||
177 ( c >= 'A' && c <= 'F'));
178 }
179
180 ///
181 bool c_isalnum(const int c)
182 {
183 return (( c >= '0' && c <= '9') ||
184 ( c >= 'a' && c <= 'z') ||
185 ( c >= 'A' && c <= 'Z'));
186 }
187
188 extern(D) private:
189
190 // originally from dmd.root.utf
191 bool isInRange(alias Ranges)(dchar c)
192 {
193 size_t high = Ranges.length - 1;
194 // Shortcut search if c is out of range
195 size_t low = (c < Ranges[0][0] || Ranges[high][1] < c) ? high + 1 : 0;
196 // Binary search
197 while (low <= high)
198 {
199 const size_t mid = low + ((high - low) >> 1);
200 if (c < Ranges[mid][0])
201 high = mid - 1;
202 else if (Ranges[mid][1] < c)
203 low = mid + 1;
204 else
205 {
206 assert(Ranges[mid][0] <= c && c <= Ranges[mid][1]);
207 return true;
208 }
209 }
210 return false;
211 }
212
213 /********************************************
214 * Do our own char maps
215 */
216 // originally from dmd.lexer (was private)
217 static immutable cmtable = ()
218 {
219 ubyte[256] table;
220 foreach (const c; 0 .. table.length)
221 {
222 if ('0' <= c && c <= '7')
223 table[c] |= CMoctal;
224 if (c_isxdigit(c))
225 table[c] |= CMhex;
226 if (c_isalnum(c) || c == '_')
227 table[c] |= CMidchar;
228
229 switch (c)
230 {
231 case 'x': case 'X':
232 case 'b': case 'B':
233 table[c] |= CMzerosecond;
234 break;
235
236 case '0': .. case '9':
237 case 'e': case 'E':
238 case 'f': case 'F':
239 case 'l': case 'L':
240 case 'p': case 'P':
241 case 'u': case 'U':
242 case 'i':
243 case '.':
244 case '_':
245 table[c] |= CMzerosecond | CMdigitsecond;
246 break;
247
248 default:
249 break;
250 }
251
252 switch (c)
253 {
254 case '\\':
255 case '\n':
256 case '\r':
257 case 0:
258 case 0x1A:
259 case '\'':
260 break;
261 default:
262 if (!(c & 0x80))
263 table[c] |= CMsinglechar;
264 break;
265 }
266 }
267 return table;
268 }();