| 1 |
#ifndef SOURCETOOLS_TOKENIZATION_TOKENIZER_H |
| 2 |
#define SOURCETOOLS_TOKENIZATION_TOKENIZER_H |
| 3 |
|
| 4 |
#include <sourcetools/core/core.h> |
| 5 |
#include <sourcetools/tokenization/Token.h> |
| 6 |
#include <sourcetools/cursor/TextCursor.h> |
| 7 |
|
| 8 |
#include <vector> |
| 9 |
#include <stack> |
| 10 |
#include <sstream> |
| 11 |
|
| 12 |
namespace sourcetools { |
| 13 |
namespace tokenizer { |
| 14 |
|
| 15 |
class Tokenizer |
| 16 |
{ |
| 17 |
private: |
| 18 |
typedef tokens::Token Token; |
| 19 |
typedef cursors::TextCursor TextCursor; |
| 20 |
typedef tokens::TokenType TokenType; |
| 21 |
|
| 22 |
private: |
| 23 |
|
| 24 |
// Tokenization ---- |
| 25 |
|
| 26 |
void consumeToken(TokenType type, |
| 27 |
std::size_t length, |
| 28 |
Token* pToken) |
| 29 |
{ |
| 30 |
*pToken = Token(cursor_, type, length); |
| 31 |
cursor_.advance(length); |
| 32 |
} |
| 33 |
|
| 34 |
template <bool SkipEscaped, bool InvalidOnError> |
| 35 |
void consumeUntil(char ch, |
| 36 |
TokenType type, |
| 37 |
Token* pToken) |
| 38 |
{ |
| 39 |
TextCursor lookahead = cursor_; |
| 40 |
|
| 41 |
bool success = false; |
| 42 |
std::size_t distance = 0; |
| 43 |
|
| 44 |
while (lookahead != lookahead.end()) { |
| 45 |
lookahead.advance(); |
| 46 |
++distance; |
| 47 |
|
| 48 |
if (SkipEscaped && lookahead.peek() == '\\') { |
| 49 |
lookahead.advance(); |
| 50 |
++distance; |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
if (lookahead.peek() == ch) { |
| 55 |
success = true; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if (success) { |
| 61 |
consumeToken(type, distance + 1, pToken); |
| 62 |
} else { |
| 63 |
consumeToken( |
| 64 |
InvalidOnError ? tokens::INVALID : type, |
| 65 |
distance, |
| 66 |
pToken |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void consumeUserOperator(Token* pToken) |
| 72 |
{ |
| 73 |
consumeUntil<false, true>('%', tokens::OPERATOR_USER, pToken); |
| 74 |
} |
| 75 |
|
| 76 |
void consumeComment(Token* pToken) |
| 77 |
{ |
| 78 |
consumeUntil<false, false>('\n', tokens::COMMENT, pToken); |
| 79 |
} |
| 80 |
|
| 81 |
void consumeQuotedSymbol(Token* pToken) |
| 82 |
{ |
| 83 |
consumeUntil<true, true>('`', tokens::SYMBOL, pToken); |
| 84 |
} |
| 85 |
|
| 86 |
void consumeQString(Token* pToken) |
| 87 |
{ |
| 88 |
consumeUntil<true, true>('\'', tokens::STRING, pToken); |
| 89 |
} |
| 90 |
|
| 91 |
void consumeQQString(Token* pToken) |
| 92 |
{ |
| 93 |
consumeUntil<true, true>('"', tokens::STRING, pToken); |
| 94 |
} |
| 95 |
|
| 96 |
// NOTE: Don't tokenize '-' or '+' as part of number; instead |
| 97 |
// it's parsed as a unary operator. |
| 98 |
bool isStartOfNumber() |
| 99 |
{ |
| 100 |
char ch = cursor_.peek(); |
| 101 |
if (utils::isDigit(ch)) |
| 102 |
return true; |
| 103 |
if (ch == '.') |
| 104 |
return utils::isDigit(cursor_.peek(1)); |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
bool isStartOfSymbol() |
| 109 |
{ |
| 110 |
return utils::isValidForStartOfRSymbol(cursor_.peek()); |
| 111 |
} |
| 112 |
|
| 113 |
bool consumeHexadecimalNumber(Token* pToken) |
| 114 |
{ |
| 115 |
std::size_t distance = 0; |
| 116 |
|
| 117 |
// Detect the leading '0'. |
| 118 |
if (cursor_.peek(distance) != '0') |
| 119 |
return false; |
| 120 |
++distance; |
| 121 |
|
| 122 |
// Detect a 'x' or 'X'. |
| 123 |
if (!(cursor_.peek(distance) == 'x' || cursor_.peek(distance) == 'X')) |
| 124 |
return false; |
| 125 |
++distance; |
| 126 |
|
| 127 |
// Check and consume all alphanumeric characters. |
| 128 |
// The number is valid if the characters are valid |
| 129 |
// hexadecimal characters (0-9, a-f, A-F). The number |
| 130 |
// can also end with an 'i' (for an imaginary number) |
| 131 |
// or with an 'L' for an integer. |
| 132 |
if (!utils::isHexDigit(cursor_.peek(distance))) |
| 133 |
{ |
| 134 |
consumeToken(tokens::INVALID, distance, pToken); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
bool success = true; |
| 139 |
char peek = cursor_.peek(distance); |
| 140 |
while (utils::isAlphaNumeric(peek) && peek != '\0') { |
| 141 |
|
| 142 |
// If we encounter an 'i' or an 'L', assume |
| 143 |
// that this ends the identifier. |
| 144 |
if (peek == 'i' || peek == 'L') |
| 145 |
{ |
| 146 |
++distance; |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
if (!utils::isHexDigit(peek)) |
| 151 |
success = false; |
| 152 |
|
| 153 |
++distance; |
| 154 |
peek = cursor_.peek(distance); |
| 155 |
} |
| 156 |
|
| 157 |
consumeToken(success ? tokens::NUMBER : tokens::INVALID, distance, pToken); |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
void consumeNumber(Token* pToken) |
| 162 |
{ |
| 163 |
bool success = true; |
| 164 |
std::size_t distance = 0; |
| 165 |
|
| 166 |
// NOTE: A leading '-' or '+' is not consumed as part of |
| 167 |
// the number. |
| 168 |
|
| 169 |
// Try parsing this as a hexadecimal number first (e.g. '0xabc'). |
| 170 |
if (consumeHexadecimalNumber(pToken)) |
| 171 |
return; |
| 172 |
|
| 173 |
// Consume digits |
| 174 |
while (utils::isDigit(cursor_.peek(distance))) |
| 175 |
++distance; |
| 176 |
|
| 177 |
// Consume a dot for decimals |
| 178 |
// Note: '.5' is a valid specification for a number |
| 179 |
// So is '100.'; ie, with a trailing decimal. |
| 180 |
if (cursor_.peek(distance) == '.') { |
| 181 |
++distance; |
| 182 |
while (utils::isDigit(cursor_.peek(distance))) |
| 183 |
++distance; |
| 184 |
} |
| 185 |
|
| 186 |
// Consume 'e', 'E' for exponential notation |
| 187 |
if (cursor_.peek(distance) == 'e' || cursor_.peek(distance) == 'E') { |
| 188 |
++distance; |
| 189 |
|
| 190 |
// Consume a '-' or a '+' for a negative number |
| 191 |
if (cursor_.peek(distance) == '-' || cursor_.peek(distance) == '+') |
| 192 |
++distance; |
| 193 |
|
| 194 |
// Parse another set of numbers following the E |
| 195 |
success = utils::isDigit(cursor_.peek(distance)); |
| 196 |
while (utils::isDigit(cursor_.peek(distance))) |
| 197 |
++distance; |
| 198 |
|
| 199 |
// Consume '.' and following numbers. Note that this is |
| 200 |
// not really a valid number for R but it's better to tokenize |
| 201 |
// this is a single entity (and then report failure later) |
| 202 |
if (cursor_.peek(distance) == '.') { |
| 203 |
success = false; |
| 204 |
++distance; |
| 205 |
while (utils::isDigit(cursor_.peek(distance))) |
| 206 |
++distance; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// Consume a final 'L' for integer literals |
| 211 |
if (cursor_.peek(distance) == 'L') |
| 212 |
++distance; |
| 213 |
|
| 214 |
consumeToken(success ? tokens::NUMBER : tokens::INVALID, distance, pToken); |
| 215 |
} |
| 216 |
|
| 217 |
void consumeSymbol(Token* pToken) |
| 218 |
{ |
| 219 |
std::size_t distance = 1; |
| 220 |
char ch = cursor_.peek(distance); |
| 221 |
while (utils::isValidForRSymbol(ch)) { |
| 222 |
++distance; |
| 223 |
ch = cursor_.peek(distance); |
| 224 |
} |
| 225 |
|
| 226 |
const char* ptr = &*(cursor_.begin() + cursor_.offset()); |
| 227 |
consumeToken(tokens::symbolType(ptr, distance), distance, pToken); |
| 228 |
} |
| 229 |
|
| 230 |
public: |
| 231 |
|
| 232 |
Tokenizer(const char* code, std::size_t n) |
| 233 |
: cursor_(code, n) |
| 234 |
{ |
| 235 |
} |
| 236 |
|
| 237 |
bool tokenize(Token* pToken) |
| 238 |
{ |
| 239 |
if (cursor_ >= cursor_.end()) |
| 240 |
{ |
| 241 |
*pToken = Token(tokens::END); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
char ch = cursor_.peek(); |
| 246 |
int n = 0; |
| 247 |
|
| 248 |
// Block-related tokens |
| 249 |
if (ch == '{') |
| 250 |
consumeToken(tokens::LBRACE, 1, pToken); |
| 251 |
else if (ch == '}') |
| 252 |
consumeToken(tokens::RBRACE, 1, pToken); |
| 253 |
else if (ch == '(') |
| 254 |
consumeToken(tokens::LPAREN, 1, pToken); |
| 255 |
else if (ch == ')') |
| 256 |
consumeToken(tokens::RPAREN, 1, pToken); |
| 257 |
else if (ch == '[') { |
| 258 |
if (cursor_.peek(1) == '[') { |
| 259 |
tokenStack_.push(tokens::LDBRACKET); |
| 260 |
consumeToken(tokens::LDBRACKET, 2, pToken); |
| 261 |
} else { |
| 262 |
tokenStack_.push(tokens::LBRACKET); |
| 263 |
consumeToken(tokens::LBRACKET, 1, pToken); |
| 264 |
} |
| 265 |
} else if (ch == ']') { |
| 266 |
if (tokenStack_.empty()) { |
| 267 |
consumeToken(tokens::INVALID, 1, pToken); |
| 268 |
} else if (tokenStack_.top() == tokens::LDBRACKET) { |
| 269 |
tokenStack_.pop(); |
| 270 |
if (cursor_.peek(1) == ']') |
| 271 |
consumeToken(tokens::RDBRACKET, 2, pToken); |
| 272 |
else |
| 273 |
consumeToken(tokens::INVALID, 1, pToken); |
| 274 |
} else { |
| 275 |
tokenStack_.pop(); |
| 276 |
consumeToken(tokens::RBRACKET, 1, pToken); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// Operators |
| 281 |
else if (ch == '<') // <<-, <=, <-, < |
| 282 |
{ |
| 283 |
char next = cursor_.peek(1); |
| 284 |
if (next == '-') // <- |
| 285 |
consumeToken(tokens::OPERATOR_ASSIGN_LEFT, 2, pToken); |
| 286 |
else if (next == '=') // <= |
| 287 |
consumeToken(tokens::OPERATOR_LESS_OR_EQUAL, 2, pToken); |
| 288 |
else if (next == '<' && cursor_.peek(2) == '-') |
| 289 |
consumeToken(tokens::OPERATOR_ASSIGN_LEFT_PARENT, 3, pToken); |
| 290 |
else |
| 291 |
consumeToken(tokens::OPERATOR_LESS, 1, pToken); |
| 292 |
} |
| 293 |
|
| 294 |
else if (ch == '>') // >=, > |
| 295 |
{ |
| 296 |
if (cursor_.peek(1) == '=') |
| 297 |
consumeToken(tokens::OPERATOR_GREATER_OR_EQUAL, 2, pToken); |
| 298 |
else |
| 299 |
consumeToken(tokens::OPERATOR_GREATER, 1, pToken); |
| 300 |
} |
| 301 |
else if (ch == '=') // '==', '=' |
| 302 |
{ |
| 303 |
if (cursor_.peek(1) == '=') |
| 304 |
consumeToken(tokens::OPERATOR_EQUAL, 2, pToken); |
| 305 |
else |
| 306 |
consumeToken(tokens::OPERATOR_ASSIGN_LEFT_EQUALS, 1, pToken); |
| 307 |
} |
| 308 |
else if (ch == '|') // '||', '|' |
| 309 |
{ |
| 310 |
if (cursor_.peek(1) == '|') |
| 311 |
consumeToken(tokens::OPERATOR_OR_SCALAR, 2, pToken); |
| 312 |
else |
| 313 |
consumeToken(tokens::OPERATOR_OR_VECTOR, 1, pToken); |
| 314 |
} |
| 315 |
else if (ch == '&') // '&&', '&' |
| 316 |
{ |
| 317 |
if (cursor_.peek(1) == '&') |
| 318 |
consumeToken(tokens::OPERATOR_AND_SCALAR, 2, pToken); |
| 319 |
else |
| 320 |
consumeToken(tokens::OPERATOR_AND_VECTOR, 1, pToken); |
| 321 |
} |
| 322 |
else if (ch == '*') // **, * |
| 323 |
{ |
| 324 |
if (cursor_.peek(1) == '*') |
| 325 |
consumeToken(tokens::OPERATOR_EXPONENTATION_STARS, 2, pToken); |
| 326 |
else |
| 327 |
consumeToken(tokens::OPERATOR_MULTIPLY, 1, pToken); |
| 328 |
} |
| 329 |
else if (ch == ':') // ':::', '::', ':=', ':' |
| 330 |
{ |
| 331 |
if (cursor_.peek(1) == ':') |
| 332 |
{ |
| 333 |
if (cursor_.peek(2) == ':') |
| 334 |
consumeToken(tokens::OPERATOR_NAMESPACE_ALL, 3, pToken); |
| 335 |
else |
| 336 |
consumeToken(tokens::OPERATOR_NAMESPACE_EXPORTS, 2, pToken); |
| 337 |
} |
| 338 |
else if (cursor_.peek(1) == '=') |
| 339 |
consumeToken(tokens::OPERATOR_ASSIGN_LEFT_COLON, 2, pToken); |
| 340 |
else |
| 341 |
consumeToken(tokens::OPERATOR_SEQUENCE, 1, pToken); |
| 342 |
} |
| 343 |
else if (ch == '!') |
| 344 |
{ |
| 345 |
if (cursor_.peek(1) == '=') |
| 346 |
consumeToken(tokens::OPERATOR_NOT_EQUAL, 2, pToken); |
| 347 |
else |
| 348 |
consumeToken(tokens::OPERATOR_NEGATION, 1, pToken); |
| 349 |
} |
| 350 |
else if (ch == '-') // '->>', '->', '-' |
| 351 |
{ |
| 352 |
if (cursor_.peek(1) == '>') |
| 353 |
{ |
| 354 |
if (cursor_.peek(2) == '>') |
| 355 |
consumeToken(tokens::OPERATOR_ASSIGN_RIGHT_PARENT, 3, pToken); |
| 356 |
else |
| 357 |
consumeToken(tokens::OPERATOR_ASSIGN_RIGHT, 2, pToken); |
| 358 |
} |
| 359 |
else |
| 360 |
consumeToken(tokens::OPERATOR_MINUS, 1, pToken); |
| 361 |
} |
| 362 |
else if (ch == '+') |
| 363 |
consumeToken(tokens::OPERATOR_PLUS, 1, pToken); |
| 364 |
else if (ch == '~') |
| 365 |
consumeToken(tokens::OPERATOR_FORMULA, 1, pToken); |
| 366 |
else if (ch == '?') |
| 367 |
consumeToken(tokens::OPERATOR_HELP, 1, pToken); |
| 368 |
else if (ch == '/') |
| 369 |
consumeToken(tokens::OPERATOR_DIVIDE, 1, pToken); |
| 370 |
else if (ch == '@') |
| 371 |
consumeToken(tokens::OPERATOR_AT, 1, pToken); |
| 372 |
else if (ch == '$') |
| 373 |
consumeToken(tokens::OPERATOR_DOLLAR, 1, pToken); |
| 374 |
else if (ch == '^') |
| 375 |
consumeToken(tokens::OPERATOR_HAT, 1, pToken); |
| 376 |
|
| 377 |
// User operators |
| 378 |
else if (ch == '%') |
| 379 |
consumeUserOperator(pToken); |
| 380 |
|
| 381 |
// Punctuation-related tokens |
| 382 |
else if (ch == ',') |
| 383 |
consumeToken(tokens::COMMA, 1, pToken); |
| 384 |
else if (ch == ';') |
| 385 |
consumeToken(tokens::SEMI, 1, pToken); |
| 386 |
|
| 387 |
// Whitespace |
| 388 |
else if (utils::countWhitespaceBytes(cursor_, &n)) |
| 389 |
consumeToken(tokens::WHITESPACE, n, pToken); |
| 390 |
|
| 391 |
// Strings and symbols |
| 392 |
else if (ch == '\'') |
| 393 |
consumeQString(pToken); |
| 394 |
else if (ch == '"') |
| 395 |
consumeQQString(pToken); |
| 396 |
else if (ch == '`') |
| 397 |
consumeQuotedSymbol(pToken); |
| 398 |
|
| 399 |
// Comments |
| 400 |
else if (ch == '#') |
| 401 |
consumeComment(pToken); |
| 402 |
|
| 403 |
// Number |
| 404 |
else if (isStartOfNumber()) |
| 405 |
consumeNumber(pToken); |
| 406 |
|
| 407 |
// Symbol |
| 408 |
else if (isStartOfSymbol()) |
| 409 |
consumeSymbol(pToken); |
| 410 |
|
| 411 |
// Nothing matched -- error |
| 412 |
else |
| 413 |
consumeToken(tokens::INVALID, 1, pToken); |
| 414 |
|
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
Token peek(std::size_t lookahead = 1) |
| 419 |
{ |
| 420 |
Tokenizer clone(*this); |
| 421 |
|
| 422 |
Token result(tokens::END); |
| 423 |
for (std::size_t i = 0; i < lookahead; ++i) { |
| 424 |
if (!clone.tokenize(&result)) { |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
return result; |
| 430 |
} |
| 431 |
|
| 432 |
private: |
| 433 |
TextCursor cursor_; |
| 434 |
std::stack<TokenType, std::vector<TokenType> > tokenStack_; |
| 435 |
}; |
| 436 |
|
| 437 |
} // namespace tokenizer |
| 438 |
|
| 439 |
inline std::vector<tokens::Token> tokenize(const char* code, std::size_t n) |
| 440 |
{ |
| 441 |
typedef tokenizer::Tokenizer Tokenizer; |
| 442 |
typedef tokens::Token Token; |
| 443 |
|
| 444 |
std::vector<Token> tokens; |
| 445 |
if (n == 0) |
| 446 |
return tokens; |
| 447 |
|
| 448 |
Token token; |
| 449 |
Tokenizer tokenizer(code, n); |
| 450 |
while (tokenizer.tokenize(&token)) |
| 451 |
tokens.push_back(token); |
| 452 |
|
| 453 |
return tokens; |
| 454 |
} |
| 455 |
|
| 456 |
inline std::vector<tokens::Token> tokenize(const std::string& code) |
| 457 |
{ |
| 458 |
return tokenize(code.data(), code.size()); |
| 459 |
} |
| 460 |
|
| 461 |
} // namespace sourcetools |
| 462 |
|
| 463 |
#endif /* SOURCETOOLS_TOKENIZATION_TOKENIZER_H */ |