Debian Codegraph @

inst/include/sourcetools/tokenization/Token.h

raw
1 #ifndef SOURCETOOLS_TOKENIZATION_TOKEN_H
2 #define SOURCETOOLS_TOKENIZATION_TOKEN_H
3
4 #include <cstring>
5 #include <cstdio>
6
7 #include <vector>
8 #include <string>
9 #include <map>
10 #include <sstream>
11
12 #include <sourcetools/core/core.h>
13 #include <sourcetools/tokenization/Registration.h>
14 #include <sourcetools/collection/Position.h>
15 #include <sourcetools/cursor/TextCursor.h>
16
17 namespace sourcetools {
18 namespace tokens {
19
20 class Token
21 {
22 private:
23 typedef cursors::TextCursor TextCursor;
24 typedef collections::Position Position;
25
26 public:
27
28 Token()
29 : begin_(NULL),
30 end_(NULL),
31 offset_(0),
32 type_(INVALID)
33 {
34 }
35
36 explicit Token(TokenType type)
37 : begin_(NULL),
38 end_(NULL),
39 offset_(0),
40 type_(type)
41 {
42 }
43
44 Token(const Position& position)
45 : begin_(NULL),
46 end_(NULL),
47 offset_(0),
48 position_(position),
49 type_(INVALID)
50 {
51 }
52
53 Token(const TextCursor& cursor, TokenType type, std::size_t length)
54 : begin_(cursor.begin() + cursor.offset()),
55 end_(cursor.begin() + cursor.offset() + length),
56 offset_(cursor.offset()),
57 position_(cursor.position()),
58 type_(type)
59 {
60 }
61
62 const char* begin() const { return begin_; }
63 const char* end() const { return end_; }
64 std::size_t offset() const { return offset_; }
65 std::size_t size() const { return end_ - begin_; }
66
67 std::string contents() const
68 {
69 return std::string(begin_, end_);
70 }
71
72 bool contentsEqual(const char* string)
73 {
74 return std::strcmp(begin_, string);
75 }
76
77 bool contentsEqual(const std::string& string) const
78 {
79 if (string.size() != size())
80 return false;
81
82 return std::memcmp(begin_, string.c_str(), size()) == 0;
83 }
84
85 const Position& position() const { return position_; }
86 std::size_t row() const { return position_.row; }
87 std::size_t column() const { return position_.column; }
88
89 TokenType type() const { return type_; }
90 bool isType(TokenType type) const { return type_ == type; }
91
92 private:
93 const char* begin_;
94 const char* end_;
95 std::size_t offset_;
96
97 Position position_;
98 TokenType type_;
99 };
100
101 inline bool isBracket(const Token& token)
102 {
103 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_BRACKET_MASK);
104 }
105
106 inline bool isLeftBracket(const Token& token)
107 {
108 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_BRACKET_LEFT_MASK);
109 }
110
111 inline bool isRightBracket(const Token& token)
112 {
113 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_BRACKET_RIGHT_MASK);
114 }
115
116 inline bool isComplement(TokenType lhs, TokenType rhs)
117 {
118 static const TokenType mask =
119 SOURCE_TOOLS_BRACKET_BIT | SOURCE_TOOLS_BRACKET_LEFT_BIT | SOURCE_TOOLS_BRACKET_RIGHT_BIT;
120
121 if (SOURCE_TOOLS_CHECK_MASK((lhs | rhs), mask))
122 return SOURCE_TOOLS_LOWER_BITS(lhs, 4) == SOURCE_TOOLS_LOWER_BITS(rhs, 4);
123
124 return false;
125 }
126
127 inline TokenType complement(TokenType type)
128 {
129 static const TokenType mask =
130 SOURCE_TOOLS_BRACKET_LEFT_BIT | SOURCE_TOOLS_BRACKET_RIGHT_BIT;
131
132 return type ^ mask;
133 }
134
135 inline bool isKeyword(const Token& token)
136 {
137 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_KEYWORD_MASK);
138 }
139
140 inline bool isControlFlowKeyword(const Token& token)
141 {
142 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_KEYWORD_CONTROL_FLOW_MASK);
143 }
144
145 inline bool isOperator(const Token& token)
146 {
147 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_OPERATOR_MASK);
148 }
149
150 inline bool isUnaryOperator(const Token& token)
151 {
152 return SOURCE_TOOLS_CHECK_MASK(token.type(), SOURCE_TOOLS_OPERATOR_UNARY_MASK);
153 }
154
155 inline bool isNonUnaryOperator(const Token& token)
156 {
157 return isOperator(token) && !isUnaryOperator(token);
158 }
159
160 inline bool isComparisonOperator(const Token& token)
161 {
162 switch (token.type())
163 {
164 case OPERATOR_AND_SCALAR:
165 case OPERATOR_AND_VECTOR:
166 case OPERATOR_OR_SCALAR:
167 case OPERATOR_OR_VECTOR:
168 case OPERATOR_EQUAL:
169 case OPERATOR_NOT_EQUAL:
170 case OPERATOR_LESS:
171 case OPERATOR_LESS_OR_EQUAL:
172 case OPERATOR_GREATER:
173 case OPERATOR_GREATER_OR_EQUAL:
174 return true;
175 default:
176 return false;
177 }
178 }
179
180 inline bool isWhitespace(const Token& token)
181 {
182 return token.type() == WHITESPACE;
183 }
184
185 inline bool isComment(const Token& token)
186 {
187 return token.type() == COMMENT;
188 }
189
190 inline bool isSymbol(const Token& token)
191 {
192 return token.type() == SYMBOL;
193 }
194
195 inline bool isEnd(const Token& token)
196 {
197 return token.type() == END;
198 }
199
200 inline bool isString(const Token& token)
201 {
202 return token.type() == STRING;
203 }
204
205 inline bool isSymbolic(const Token& token)
206 {
207 static const TokenType mask = SYMBOL | NUMBER | STRING;
208 return (token.type() & mask) != 0;
209 }
210
211 inline bool isNumeric(const Token& token)
212 {
213 return (token.type() & NUMBER) != 0;
214 }
215
216 inline bool isCallOperator(const Token& token)
217 {
218 return token.type() == LPAREN ||
219 token.type() == LBRACKET ||
220 token.type() == LDBRACKET;
221 }
222
223 inline bool isAssignmentOperator(const Token& token)
224 {
225 switch (token.type())
226 {
227 case OPERATOR_ASSIGN_LEFT:
228 case OPERATOR_ASSIGN_LEFT_COLON:
229 case OPERATOR_ASSIGN_LEFT_EQUALS:
230 case OPERATOR_ASSIGN_LEFT_PARENT:
231 case OPERATOR_ASSIGN_RIGHT:
232 case OPERATOR_ASSIGN_RIGHT_PARENT:
233 return true;
234 default:
235 return false;
236 }
237 }
238
239 namespace detail {
240
241 inline bool isHexDigit(char c)
242 {
243 if (c >= '0' && c <= '9')
244 return true;
245 else if (c >= 'a' && c <= 'f')
246 return true;
247 else if (c >= 'A' && c <= 'F')
248 return true;
249 return false;
250 }
251
252 inline int hexValue(char c)
253 {
254 if (c >= '0' && c <= '9')
255 return c - '0';
256 else if (c >= 'a' && c <= 'f')
257 return c - 'a' + 10;
258 else if (c >= 'A' && c <= 'F')
259 return c - 'A' + 10;
260
261 return 0;
262 }
263
264 // Parses an octal escape sequence, e.g. '\012'.
265 inline bool parseOctal(const char*& it, char*& output)
266 {
267 // Check for opening escape
268 if (*it != '\\')
269 return false;
270
271 // Check for number following
272 char lookahead = *(it + 1);
273 if (lookahead < '0' || lookahead > '7')
274 return false;
275 ++it;
276
277 // Begin parsing. Consume up to three numbers.
278 unsigned char result = 0;
279 const char* end = it + 3;
280 for (; it != end; ++it)
281 {
282 char ch = *it;
283 if ('0' <= ch && ch <= '7')
284 result = 8 * result + ch - '0';
285 else
286 break;
287 }
288
289 // Assign result, and return.
290 *output++ = result;
291 return true;
292 }
293
294 // Parse a hex escape sequence, e.g. '\xFF'.
295 inline bool parseHex(const char*& it, char*& output)
296 {
297 // Check for opening escape.
298 if (*it != '\\')
299 return false;
300
301 if (*(it + 1) != 'x')
302 return false;
303
304 if (!isHexDigit(*(it + 2)))
305 return false;
306
307 // Begin parsing.
308 it += 2;
309 unsigned char value = 0;
310 const char* end = it + 2;
311 for (; it != end; ++it)
312 {
313 int result = hexValue(*it);
314 if (result == 0)
315 break;
316 value = 16 * value + result;
317 }
318
319 *output++ = value;
320 return true;
321 }
322
323 // Parse a unicode escape sequence.
324 inline bool parseUnicode(const char*& it, char*& output)
325 {
326 if (*it != '\\')
327 return false;
328
329 char lookahead = *(it + 1);
330 int size;
331 if (lookahead == 'u')
332 size = 4;
333 else if (lookahead == 'U')
334 size = 8;
335 else
336 return false;
337
338 // Clone the input iterator (only set it on success)
339 const char* clone = it;
340 clone += 2;
341
342 // Check for e.g. '\u{...}'
343 // ^
344 bool delimited = *clone == '{';
345 clone += delimited;
346
347 // Check for a hex digit.
348 if (!isHexDigit(*clone))
349 return false;
350
351 // Begin parsing hex digits
352 wchar_t value = 0;
353 const char* end = clone + size;
354 for (; clone != end; ++clone)
355 {
356 if (!isHexDigit(*clone))
357 break;
358
359 int hex = hexValue(*clone);
360 value = 16 * value + hex;
361 }
362
363 // Eat a closing '}' if we had a starting '{'.
364 if (delimited)
365 {
366 if (*clone != '}')
367 return false;
368 ++clone;
369 }
370
371 std::mbstate_t state;
372 std::memset(&state, 0, sizeof(state));
373 std::size_t bytes = std::wcrtomb(output, value, &state);
374 if (bytes == static_cast<std::size_t>(-1))
375 return false;
376
377 // Update iterator state
378 it = clone;
379 output += bytes;
380 return true;
381 }
382
383 } // namespace detail
384
385 inline std::string stringValue(const char* begin, const char* end)
386 {
387 if (begin == end)
388 return std::string();
389
390 std::size_t n = end - begin;
391 scoped_array<char> buffer(new char[n + 1]);
392
393 const char* it = begin;
394 char* output = buffer;
395
396 while (it < end)
397 {
398 if (*it == '\\')
399 {
400 if (detail::parseOctal(it, output) ||
401 detail::parseHex(it, output) ||
402 detail::parseUnicode(it, output))
403 {
404 continue;
405 }
406
407 // Handle the rest
408 ++it;
409 switch (*it)
410 {
411 case 'a': *output++ = '\a'; break;
412 case 'b': *output++ = '\b'; break;
413 case 'f': *output++ = '\f'; break;
414 case 'n': *output++ = '\n'; break;
415 case 'r': *output++ = '\r'; break;
416 case 't': *output++ = '\t'; break;
417 case 'v': *output++ = '\v'; break;
418 case '\\': *output++ = '\\'; break;
419 default: *output++ = *it; break;
420 }
421 ++it;
422 }
423 else
424 {
425 *output++ = *it++;
426 }
427 }
428
429 // Ensure null termination, just in case
430 *output++ = '\0';
431
432 // Construct the result string and return
433 std::string result(buffer, output - buffer);
434 return result;
435 }
436
437 inline std::string stringValue(const Token& token)
438 {
439 switch (token.type())
440 {
441 case STRING:
442 return stringValue(token.begin() + 1, token.end() - 1);
443 case SYMBOL:
444 if (*token.begin() == '`')
445 return stringValue(token.begin() + 1, token.end() - 1);
446 default:
447 return stringValue(token.begin(), token.end());
448 }
449 }
450
451 } // namespace tokens
452
453 inline std::string toString(tokens::TokenType type)
454 {
455 using namespace tokens;
456
457 if (type == INVALID) return "invalid";
458 else if (type == END) return "end";
459 else if (type == EMPTY) return "empty";
460 else if (type == MISSING) return "missing";
461 else if (type == SEMI) return "semi";
462 else if (type == COMMA) return "comma";
463 else if (type == SYMBOL) return "symbol";
464 else if (type == COMMENT) return "comment";
465 else if (type == WHITESPACE) return "whitespace";
466 else if (type == STRING) return "string";
467 else if (type == NUMBER) return "number";
468
469 else if (SOURCE_TOOLS_CHECK_MASK(type, SOURCE_TOOLS_BRACKET_MASK))
470 return "bracket";
471 else if (SOURCE_TOOLS_CHECK_MASK(type, SOURCE_TOOLS_KEYWORD_MASK))
472 return "keyword";
473 else if (SOURCE_TOOLS_CHECK_MASK(type, SOURCE_TOOLS_OPERATOR_MASK))
474 return "operator";
475
476 return "unknown";
477 }
478
479 inline std::string toString(const tokens::Token& token)
480 {
481 std::string contents;
482 if (token.isType(tokens::END))
483 contents = "<END>";
484 else if (token.isType(tokens::EMPTY))
485 contents = "<empty>";
486 else if (token.isType(tokens::MISSING))
487 contents = "<missing>";
488 else
489 contents = token.contents();
490
491 static const int N = 1024;
492 if (contents.size() > N / 2)
493 contents = contents.substr(0, N / 2);
494
495 char buff[N];
496 std::snprintf(buff, N,
497 "[%4lu:%4lu]: %s",
498 static_cast<unsigned long>(token.row()),
499 static_cast<unsigned long>(token.column()),
500 contents.c_str());
501 return buff;
502 }
503
504 inline std::ostream& operator<<(std::ostream& os, const tokens::Token& token)
505 {
506 return os << toString(token);
507 }
508
509 inline std::ostream& operator<<(std::ostream& os, const std::vector<tokens::Token>& tokens)
510 {
511 for (std::vector<tokens::Token>::const_iterator it = tokens.begin();
512 it != tokens.end();
513 ++it)
514 {
515 os << *it << std::endl;
516 }
517
518 return os;
519 }
520
521 } // namespace sourcetools
522
523 #endif /* SOURCETOOLS_TOKENIZATION_TOKEN_H */