| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#ifndef RUST_TOKEN_H |
| 20 |
#define RUST_TOKEN_H |
| 21 |
|
| 22 |
#include "rust-system.h" |
| 23 |
#include "rust-linemap.h" |
| 24 |
#include "rust-unicode.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
// "Primitive core types" in Rust - the different int and float types, as well |
| 28 |
// as some others |
| 29 |
enum PrimitiveCoreType |
| 30 |
{ |
| 31 |
CORETYPE_UNKNOWN, |
| 32 |
// named primitives |
| 33 |
CORETYPE_BOOL, |
| 34 |
CORETYPE_CHAR, |
| 35 |
CORETYPE_STR, |
| 36 |
// okay technically int and uint are arch-dependent (pointer size) |
| 37 |
CORETYPE_INT, |
| 38 |
CORETYPE_UINT, |
| 39 |
// numbered number primitives |
| 40 |
CORETYPE_F32, |
| 41 |
CORETYPE_F64, |
| 42 |
CORETYPE_I8, |
| 43 |
CORETYPE_I16, |
| 44 |
CORETYPE_I32, |
| 45 |
CORETYPE_I64, |
| 46 |
CORETYPE_I128, |
| 47 |
CORETYPE_U8, |
| 48 |
CORETYPE_U16, |
| 49 |
CORETYPE_U32, |
| 50 |
CORETYPE_U64, |
| 51 |
CORETYPE_U128, |
| 52 |
// Pure decimals are used for tuple index. |
| 53 |
// Also means there is no type hint. |
| 54 |
CORETYPE_PURE_DECIMAL, |
| 55 |
// arch-dependent pointer sizes |
| 56 |
CORETYPE_ISIZE = CORETYPE_INT, |
| 57 |
CORETYPE_USIZE = CORETYPE_UINT |
| 58 |
}; |
| 59 |
|
| 60 |
// RS_TOKEN(name, description) |
| 61 |
// RS_TOKEN_KEYWORD_{2015,2018}(name, identifier) |
| 62 |
|
| 63 |
// Keep RS_TOKEN_KEYWORD sorted |
| 64 |
|
| 65 |
/* note that abstract, async, become, box, do, final, macro, override, priv, |
| 66 |
* try, typeof, unsized, virtual, and yield are unused */ |
| 67 |
#define RS_TOKEN_LIST \ |
| 68 |
RS_TOKEN (FIRST_TOKEN, "<first-token-marker>") \ |
| 69 |
RS_TOKEN (END_OF_FILE, "end of file") \ |
| 70 |
RS_TOKEN (EXCLAM, "!") \ |
| 71 |
RS_TOKEN (NOT_EQUAL, "!=") \ |
| 72 |
RS_TOKEN (PERCENT, "%") \ |
| 73 |
RS_TOKEN (PERCENT_EQ, "%=") \ |
| 74 |
RS_TOKEN (AMP, "&") \ |
| 75 |
RS_TOKEN (AMP_EQ, "&=") \ |
| 76 |
RS_TOKEN (LOGICAL_AND, "&&") \ |
| 77 |
RS_TOKEN (ASTERISK, "*") \ |
| 78 |
RS_TOKEN (ASTERISK_EQ, "*=") \ |
| 79 |
RS_TOKEN (PLUS, "+") \ |
| 80 |
RS_TOKEN (PLUS_EQ, "+=") \ |
| 81 |
RS_TOKEN (COMMA, ",") \ |
| 82 |
RS_TOKEN (MINUS, "-") \ |
| 83 |
RS_TOKEN (MINUS_EQ, "-=") \ |
| 84 |
RS_TOKEN (RETURN_TYPE, "->") \ |
| 85 |
RS_TOKEN (DOT, ".") \ |
| 86 |
RS_TOKEN (DOT_DOT, "..") \ |
| 87 |
RS_TOKEN (DOT_DOT_EQ, "..=") \ |
| 88 |
RS_TOKEN (ELLIPSIS, "...") \ |
| 89 |
RS_TOKEN (DIV, "/") \ |
| 90 |
RS_TOKEN (DIV_EQ, "/=") \ |
| 91 |
RS_TOKEN (COLON, ":") \ |
| 92 |
RS_TOKEN (SEMICOLON, ";") \ |
| 93 |
RS_TOKEN (LEFT_SHIFT, "<<") \ |
| 94 |
RS_TOKEN (LEFT_SHIFT_EQ, "<<=") \ |
| 95 |
RS_TOKEN (LEFT_ANGLE, "<") \ |
| 96 |
RS_TOKEN (LESS_OR_EQUAL, "<=") \ |
| 97 |
RS_TOKEN (EQUAL, "=") \ |
| 98 |
RS_TOKEN (EQUAL_EQUAL, "==") \ |
| 99 |
RS_TOKEN (MATCH_ARROW, "=>") \ |
| 100 |
RS_TOKEN (RIGHT_ANGLE, ">") \ |
| 101 |
RS_TOKEN (GREATER_OR_EQUAL, ">=") \ |
| 102 |
RS_TOKEN (RIGHT_SHIFT, ">>") \ |
| 103 |
RS_TOKEN (RIGHT_SHIFT_EQ, ">>=") \ |
| 104 |
RS_TOKEN (PATTERN_BIND, "@") \ |
| 105 |
RS_TOKEN (TILDE, "~") \ |
| 106 |
RS_TOKEN (BACKSLASH, "\\") \ |
| 107 |
RS_TOKEN (BACKTICK, "`") \ |
| 108 |
RS_TOKEN (CARET, "^") \ |
| 109 |
RS_TOKEN (CARET_EQ, "^=") \ |
| 110 |
RS_TOKEN (PIPE, "|") \ |
| 111 |
RS_TOKEN (PIPE_EQ, "|=") \ |
| 112 |
RS_TOKEN (OR, "||") \ |
| 113 |
RS_TOKEN (QUESTION_MARK, "?") \ |
| 114 |
RS_TOKEN (HASH, "#") \ |
| 115 |
/* from here on, dodgy and may not be correct. not operators and may be \ |
| 116 |
* symbols */ \ |
| 117 |
/* RS_TOKEN(SPACE, " ") probably too dodgy */ \ |
| 118 |
/* RS_TOKEN(NEWLINE, "\n")*/ \ |
| 119 |
RS_TOKEN (SCOPE_RESOLUTION, "::") /* dodgy */ \ |
| 120 |
RS_TOKEN (SINGLE_QUOTE, "'") /* should i differentiate from lifetime? */ \ |
| 121 |
RS_TOKEN (DOUBLE_QUOTE, "\"") \ |
| 122 |
RS_TOKEN (IDENTIFIER, "identifier") \ |
| 123 |
RS_TOKEN (INT_LITERAL, \ |
| 124 |
"integer literal") /* do different int and float types need \ |
| 125 |
different literal types? */ \ |
| 126 |
RS_TOKEN (FLOAT_LITERAL, "float literal") \ |
| 127 |
RS_TOKEN (STRING_LITERAL, "string literal") \ |
| 128 |
RS_TOKEN (CHAR_LITERAL, "character literal") \ |
| 129 |
RS_TOKEN (BYTE_STRING_LITERAL, "byte string literal") \ |
| 130 |
RS_TOKEN (RAW_STRING_LITERAL, "raw string literal") \ |
| 131 |
RS_TOKEN (BYTE_CHAR_LITERAL, "byte character literal") \ |
| 132 |
RS_TOKEN (LIFETIME, "lifetime") /* TODO: improve token type */ \ |
| 133 |
/* Have "interpolated" tokens (whatever that means)? identifer, path, type, \ |
| 134 |
* pattern, */ \ |
| 135 |
/* expression, statement, block, meta, item in mrustc (but not directly in \ |
| 136 |
* lexer). */ \ |
| 137 |
RS_TOKEN (LEFT_PAREN, "(") \ |
| 138 |
RS_TOKEN (RIGHT_PAREN, ")") \ |
| 139 |
RS_TOKEN (LEFT_CURLY, "{") \ |
| 140 |
RS_TOKEN (RIGHT_CURLY, "}") \ |
| 141 |
RS_TOKEN (LEFT_SQUARE, "[") \ |
| 142 |
RS_TOKEN (RIGHT_SQUARE, "]") \ |
| 143 |
/* Macros */ \ |
| 144 |
RS_TOKEN (DOLLAR_SIGN, "$") \ |
| 145 |
/* Doc Comments */ \ |
| 146 |
RS_TOKEN (INNER_DOC_COMMENT, "#![doc]") \ |
| 147 |
RS_TOKEN (OUTER_DOC_COMMENT, "#[doc]") \ |
| 148 |
RS_TOKEN_KEYWORD_2015 (ABSTRACT, "abstract") /* unused */ \ |
| 149 |
RS_TOKEN_KEYWORD_2015 (AS, "as") \ |
| 150 |
RS_TOKEN_KEYWORD_2018 (ASYNC, "async") /* unused */ \ |
| 151 |
RS_TOKEN_KEYWORD_2015 (AUTO, "auto") \ |
| 152 |
RS_TOKEN_KEYWORD_2018 (AWAIT, "await") \ |
| 153 |
RS_TOKEN_KEYWORD_2015 (BECOME, "become") /* unused */ \ |
| 154 |
RS_TOKEN_KEYWORD_2015 (BOX, "box") /* unused */ \ |
| 155 |
RS_TOKEN_KEYWORD_2015 (BREAK, "break") \ |
| 156 |
RS_TOKEN_KEYWORD_2015 (CONST, "const") \ |
| 157 |
RS_TOKEN_KEYWORD_2015 (CONTINUE, "continue") \ |
| 158 |
RS_TOKEN_KEYWORD_2015 (CRATE, "crate") \ |
| 159 |
RS_TOKEN_KEYWORD_2015 (DO, "do") /* unused */ \ |
| 160 |
RS_TOKEN_KEYWORD_2018 (DYN, "dyn") \ |
| 161 |
RS_TOKEN_KEYWORD_2015 (ELSE, "else") \ |
| 162 |
RS_TOKEN_KEYWORD_2015 (ENUM_KW, "enum") \ |
| 163 |
RS_TOKEN_KEYWORD_2015 (EXTERN_KW, "extern") \ |
| 164 |
RS_TOKEN_KEYWORD_2015 (FALSE_LITERAL, "false") \ |
| 165 |
RS_TOKEN_KEYWORD_2015 (FINAL_KW, "final") /* unused */ \ |
| 166 |
RS_TOKEN_KEYWORD_2015 (FN_KW, "fn") \ |
| 167 |
RS_TOKEN_KEYWORD_2015 (FOR, "for") \ |
| 168 |
RS_TOKEN_KEYWORD_2015 (IF, "if") \ |
| 169 |
RS_TOKEN_KEYWORD_2015 (IMPL, "impl") \ |
| 170 |
RS_TOKEN_KEYWORD_2015 (IN, "in") \ |
| 171 |
RS_TOKEN_KEYWORD_2015 (LET, "let") \ |
| 172 |
RS_TOKEN_KEYWORD_2015 (LOOP, "loop") \ |
| 173 |
RS_TOKEN_KEYWORD_2015 (MACRO, "macro") \ |
| 174 |
RS_TOKEN_KEYWORD_2015 (MATCH_KW, "match") \ |
| 175 |
RS_TOKEN_KEYWORD_2015 (MOD, "mod") \ |
| 176 |
RS_TOKEN_KEYWORD_2015 (MOVE, "move") \ |
| 177 |
RS_TOKEN_KEYWORD_2015 (MUT, "mut") \ |
| 178 |
RS_TOKEN_KEYWORD_2015 (OVERRIDE_KW, "override") /* unused */ \ |
| 179 |
RS_TOKEN_KEYWORD_2015 (PRIV, "priv") /* unused */ \ |
| 180 |
RS_TOKEN_KEYWORD_2015 (PUB, "pub") \ |
| 181 |
RS_TOKEN_KEYWORD_2015 (REF, "ref") \ |
| 182 |
RS_TOKEN_KEYWORD_2015 (RETURN_KW, "return") \ |
| 183 |
RS_TOKEN_KEYWORD_2015 ( \ |
| 184 |
SELF_ALIAS, "Self") /* mrustc does not treat this as a reserved word*/ \ |
| 185 |
RS_TOKEN_KEYWORD_2015 (SELF, "self") \ |
| 186 |
RS_TOKEN_KEYWORD_2015 (STATIC_KW, "static") \ |
| 187 |
RS_TOKEN_KEYWORD_2015 (STRUCT_KW, "struct") \ |
| 188 |
RS_TOKEN_KEYWORD_2015 (SUPER, "super") \ |
| 189 |
RS_TOKEN_KEYWORD_2015 (TRAIT, "trait") \ |
| 190 |
RS_TOKEN_KEYWORD_2015 (TRUE_LITERAL, "true") \ |
| 191 |
RS_TOKEN_KEYWORD_2015 (TRY, "try") /* unused */ \ |
| 192 |
RS_TOKEN_KEYWORD_2015 (TYPE, "type") \ |
| 193 |
RS_TOKEN_KEYWORD_2015 (TYPEOF, "typeof") /* unused */ \ |
| 194 |
RS_TOKEN_KEYWORD_2015 (UNDERSCORE, "_") \ |
| 195 |
RS_TOKEN_KEYWORD_2015 (UNSAFE, "unsafe") \ |
| 196 |
RS_TOKEN_KEYWORD_2015 (UNSIZED, "unsized") /* unused */ \ |
| 197 |
RS_TOKEN_KEYWORD_2015 (USE, "use") \ |
| 198 |
RS_TOKEN_KEYWORD_2015 (VIRTUAL, "virtual") /* unused */ \ |
| 199 |
RS_TOKEN_KEYWORD_2015 (WHERE, "where") \ |
| 200 |
RS_TOKEN_KEYWORD_2015 (WHILE, "while") \ |
| 201 |
RS_TOKEN_KEYWORD_2015 (YIELD, "yield") /* unused */ \ |
| 202 |
RS_TOKEN (LAST_TOKEN, "<last-token-marker>") |
| 203 |
|
| 204 |
// Contains all token types. Crappy implementation via x-macros. |
| 205 |
enum TokenId |
| 206 |
{ |
| 207 |
#define RS_TOKEN(name, _) name, |
| 208 |
#define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y) |
| 209 |
#define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015 |
| 210 |
RS_TOKEN_LIST |
| 211 |
#undef RS_TOKEN_KEYWORD_2015 |
| 212 |
#undef RS_TOKEN_KEYWORD_2018 |
| 213 |
#undef RS_TOKEN |
| 214 |
}; |
| 215 |
|
| 216 |
// dodgy "TokenPtr" declaration with Token forward declaration |
| 217 |
class Token; |
| 218 |
// A smart pointer (shared_ptr) to Token. |
| 219 |
typedef std::shared_ptr<Token> TokenPtr; |
| 220 |
// A smart pointer (shared_ptr) to a constant Token. |
| 221 |
typedef std::shared_ptr<const Token> const_TokenPtr; |
| 222 |
|
| 223 |
// Hackily defined way to get token description for enum value using x-macros |
| 224 |
const char * |
| 225 |
get_token_description (TokenId id); |
| 226 |
/* Hackily defined way to get token description as a string for enum value using |
| 227 |
* x-macros */ |
| 228 |
const char * |
| 229 |
token_id_to_str (TokenId id); |
| 230 |
/* checks if a token is a keyword */ |
| 231 |
bool |
| 232 |
token_id_is_keyword (TokenId id); |
| 233 |
/* gets the string associated with a keyword */ |
| 234 |
const std::string & |
| 235 |
token_id_keyword_string (TokenId id); |
| 236 |
// Get type hint description as a string. |
| 237 |
const char * |
| 238 |
get_type_hint_string (PrimitiveCoreType type); |
| 239 |
|
| 240 |
/* Normalize string if a token is a identifier */ |
| 241 |
std::string |
| 242 |
nfc_normalize_token_string (location_t loc, TokenId id, const std::string &str); |
| 243 |
|
| 244 |
// Represents a single token. Create using factory static methods. |
| 245 |
class Token |
| 246 |
{ |
| 247 |
private: |
| 248 |
// Token kind. |
| 249 |
TokenId token_id; |
| 250 |
// Token location. |
| 251 |
location_t locus; |
| 252 |
// Associated text (if any) of token. |
| 253 |
std::unique_ptr<std::string> str; |
| 254 |
// TODO: maybe remove issues and just store std::string as value? |
| 255 |
/* Type hint for token based on lexer data (e.g. type suffix). Does not exist |
| 256 |
* for most tokens. */ |
| 257 |
PrimitiveCoreType type_hint; |
| 258 |
|
| 259 |
// Token constructor from token id and location. Has a null string. |
| 260 |
Token (TokenId token_id, location_t location) |
| 261 |
: token_id (token_id), locus (location), str (nullptr), |
| 262 |
type_hint (CORETYPE_UNKNOWN) |
| 263 |
{} |
| 264 |
|
| 265 |
// Token constructor from token id, location, and a string. |
| 266 |
Token (TokenId token_id, location_t location, std::string &¶mStr) |
| 267 |
: token_id (token_id), locus (location), type_hint (CORETYPE_UNKNOWN) |
| 268 |
{ |
| 269 |
// Normalize identifier tokens |
| 270 |
str = std::make_unique<std::string> ( |
| 271 |
nfc_normalize_token_string (location, token_id, paramStr)); |
| 272 |
} |
| 273 |
|
| 274 |
// Token constructor from token id, location, and a char. |
| 275 |
Token (TokenId token_id, location_t location, char paramChar) |
| 276 |
: token_id (token_id), locus (location), |
| 277 |
str (new std::string (1, paramChar)), type_hint (CORETYPE_UNKNOWN) |
| 278 |
{ |
| 279 |
// Do not need to normalize 1byte char |
| 280 |
} |
| 281 |
|
| 282 |
// Token constructor from token id, location, and a "codepoint". |
| 283 |
Token (TokenId token_id, location_t location, Codepoint paramCodepoint) |
| 284 |
: token_id (token_id), locus (location), type_hint (CORETYPE_UNKNOWN) |
| 285 |
{ |
| 286 |
// Normalize identifier tokens |
| 287 |
str = std::make_unique<std::string> ( |
| 288 |
nfc_normalize_token_string (location, token_id, |
| 289 |
paramCodepoint.as_string ())); |
| 290 |
} |
| 291 |
|
| 292 |
// Token constructor from token id, location, a string, and type hint. |
| 293 |
Token (TokenId token_id, location_t location, std::string &¶mStr, |
| 294 |
PrimitiveCoreType parType) |
| 295 |
: token_id (token_id), locus (location), type_hint (parType) |
| 296 |
{ |
| 297 |
// Normalize identifier tokens |
| 298 |
str = std::make_unique<std::string> ( |
| 299 |
nfc_normalize_token_string (location, token_id, paramStr)); |
| 300 |
} |
| 301 |
|
| 302 |
public: |
| 303 |
// No default constructor. |
| 304 |
Token () = delete; |
| 305 |
// Do not copy/assign tokens. |
| 306 |
Token (const Token &) = delete; |
| 307 |
Token &operator= (const Token &) = delete; |
| 308 |
|
| 309 |
// Allow moving tokens. |
| 310 |
Token (Token &&other) = default; |
| 311 |
Token &operator= (Token &&other) = default; |
| 312 |
|
| 313 |
~Token () = default; |
| 314 |
|
| 315 |
/* TODO: make_shared (which saves a heap allocation) does not work with the |
| 316 |
* private constructor */ |
| 317 |
|
| 318 |
// Makes and returns a new TokenPtr (with null string). |
| 319 |
static TokenPtr make (TokenId token_id, location_t locus) |
| 320 |
{ |
| 321 |
// return std::make_shared<Token> (token_id, locus); |
| 322 |
return TokenPtr (new Token (token_id, locus)); |
| 323 |
} |
| 324 |
|
| 325 |
// Makes and returns a new TokenPtr of type IDENTIFIER. |
| 326 |
static TokenPtr make_identifier (location_t locus, std::string &&str) |
| 327 |
{ |
| 328 |
// return std::make_shared<Token> (IDENTIFIER, locus, str); |
| 329 |
return TokenPtr (new Token (IDENTIFIER, locus, std::move (str))); |
| 330 |
} |
| 331 |
|
| 332 |
// Makes and returns a new TokenPtr of type INT_LITERAL. |
| 333 |
static TokenPtr make_int (location_t locus, std::string &&str, |
| 334 |
PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) |
| 335 |
{ |
| 336 |
// return std::make_shared<Token> (INT_LITERAL, locus, str, type_hint); |
| 337 |
return TokenPtr ( |
| 338 |
new Token (INT_LITERAL, locus, std::move (str), type_hint)); |
| 339 |
} |
| 340 |
|
| 341 |
// Makes and returns a new TokenPtr of type FLOAT_LITERAL. |
| 342 |
static TokenPtr make_float (location_t locus, std::string &&str, |
| 343 |
PrimitiveCoreType type_hint = CORETYPE_UNKNOWN) |
| 344 |
{ |
| 345 |
// return std::make_shared<Token> (FLOAT_LITERAL, locus, str, type_hint); |
| 346 |
return TokenPtr ( |
| 347 |
new Token (FLOAT_LITERAL, locus, std::move (str), type_hint)); |
| 348 |
} |
| 349 |
|
| 350 |
// Makes and returns a new TokenPtr of type STRING_LITERAL. |
| 351 |
static TokenPtr make_string (location_t locus, std::string &&str) |
| 352 |
{ |
| 353 |
// return std::make_shared<Token> (STRING_LITERAL, locus, str, |
| 354 |
// CORETYPE_STR); |
| 355 |
return TokenPtr ( |
| 356 |
new Token (STRING_LITERAL, locus, std::move (str), CORETYPE_STR)); |
| 357 |
} |
| 358 |
|
| 359 |
// Makes and returns a new TokenPtr of type CHAR_LITERAL. |
| 360 |
static TokenPtr make_char (location_t locus, Codepoint char_lit) |
| 361 |
{ |
| 362 |
// return std::make_shared<Token> (CHAR_LITERAL, locus, char_lit); |
| 363 |
return TokenPtr (new Token (CHAR_LITERAL, locus, char_lit)); |
| 364 |
} |
| 365 |
|
| 366 |
// Makes and returns a new TokenPtr of type BYTE_CHAR_LITERAL. |
| 367 |
static TokenPtr make_byte_char (location_t locus, char byte_char) |
| 368 |
{ |
| 369 |
// return std::make_shared<Token> (BYTE_CHAR_LITERAL, locus, byte_char); |
| 370 |
return TokenPtr (new Token (BYTE_CHAR_LITERAL, locus, byte_char)); |
| 371 |
} |
| 372 |
|
| 373 |
// Makes and returns a new TokenPtr of type BYTE_STRING_LITERAL (fix). |
| 374 |
static TokenPtr make_byte_string (location_t locus, std::string &&str) |
| 375 |
{ |
| 376 |
// return std::make_shared<Token> (BYTE_STRING_LITERAL, locus, str); |
| 377 |
return TokenPtr (new Token (BYTE_STRING_LITERAL, locus, std::move (str))); |
| 378 |
} |
| 379 |
|
| 380 |
// Makes and returns a new TokenPtr of type RAW_STRING_LITERAL. |
| 381 |
static TokenPtr make_raw_string (location_t locus, std::string &&str) |
| 382 |
{ |
| 383 |
return TokenPtr (new Token (RAW_STRING_LITERAL, locus, std::move (str))); |
| 384 |
} |
| 385 |
|
| 386 |
// Makes and returns a new TokenPtr of type INNER_DOC_COMMENT. |
| 387 |
static TokenPtr make_inner_doc_comment (location_t locus, std::string &&str) |
| 388 |
{ |
| 389 |
return TokenPtr (new Token (INNER_DOC_COMMENT, locus, std::move (str))); |
| 390 |
} |
| 391 |
|
| 392 |
// Makes and returns a new TokenPtr of type OUTER_DOC_COMMENT. |
| 393 |
static TokenPtr make_outer_doc_comment (location_t locus, std::string &&str) |
| 394 |
{ |
| 395 |
return TokenPtr (new Token (OUTER_DOC_COMMENT, locus, std::move (str))); |
| 396 |
} |
| 397 |
|
| 398 |
// Makes and returns a new TokenPtr of type LIFETIME. |
| 399 |
static TokenPtr make_lifetime (location_t locus, std::string &&str) |
| 400 |
{ |
| 401 |
// return std::make_shared<Token> (LIFETIME, locus, str); |
| 402 |
return TokenPtr (new Token (LIFETIME, locus, std::move (str))); |
| 403 |
} |
| 404 |
|
| 405 |
// Gets id of the token. |
| 406 |
TokenId get_id () const { return token_id; } |
| 407 |
|
| 408 |
// Gets location of the token. |
| 409 |
location_t get_locus () const { return locus; } |
| 410 |
|
| 411 |
// Set location of the token. |
| 412 |
void set_locus (location_t locus) { this->locus = locus; } |
| 413 |
|
| 414 |
// Gets string description of the token. |
| 415 |
const std::string & |
| 416 |
get_str () const; /*{ |
| 417 |
// FIXME: put in header again when fix null problem |
| 418 |
//gcc_assert(str != nullptr); |
| 419 |
if (str == nullptr) { |
| 420 |
error_at(get_locus(), "attempted to get string for '%s', which has no string. |
| 421 |
returning empty string instead.", get_token_description()); return ""; |
| 422 |
} |
| 423 |
return *str; |
| 424 |
}*/ |
| 425 |
|
| 426 |
// Gets token's type hint info. |
| 427 |
PrimitiveCoreType get_type_hint () const |
| 428 |
{ |
| 429 |
return type_hint == CORETYPE_PURE_DECIMAL ? CORETYPE_UNKNOWN : type_hint; |
| 430 |
} |
| 431 |
|
| 432 |
// diagnostics (error reporting) |
| 433 |
const char *get_token_description () const |
| 434 |
{ |
| 435 |
return Rust::get_token_description (token_id); |
| 436 |
} |
| 437 |
|
| 438 |
// debugging |
| 439 |
const char *token_id_to_str () const |
| 440 |
{ |
| 441 |
return Rust::token_id_to_str (token_id); |
| 442 |
} |
| 443 |
|
| 444 |
// debugging |
| 445 |
const char *get_type_hint_str () const; |
| 446 |
|
| 447 |
/* Returns whether the token is a literal of any type (int, float, char, |
| 448 |
* string, byte char, byte string). */ |
| 449 |
bool is_literal () const |
| 450 |
{ |
| 451 |
switch (token_id) |
| 452 |
{ |
| 453 |
case INT_LITERAL: |
| 454 |
case FLOAT_LITERAL: |
| 455 |
case CHAR_LITERAL: |
| 456 |
case STRING_LITERAL: |
| 457 |
case BYTE_CHAR_LITERAL: |
| 458 |
case BYTE_STRING_LITERAL: |
| 459 |
case RAW_STRING_LITERAL: |
| 460 |
return true; |
| 461 |
default: |
| 462 |
return false; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/* Returns whether the token actually has a string (regardless of whether it |
| 467 |
* should or not). */ |
| 468 |
bool has_str () const { return str != nullptr; } |
| 469 |
|
| 470 |
// Returns whether the token should have a string. |
| 471 |
bool should_have_str () const |
| 472 |
{ |
| 473 |
return is_literal () || token_id == IDENTIFIER || token_id == LIFETIME; |
| 474 |
} |
| 475 |
|
| 476 |
// Returns whether the token is a pure decimal int literal |
| 477 |
bool is_pure_decimal () const { return type_hint == CORETYPE_PURE_DECIMAL; } |
| 478 |
|
| 479 |
// Return the token representation as someone would find it in the original |
| 480 |
// source code file. |
| 481 |
std::string as_string () const; |
| 482 |
}; |
| 483 |
} // namespace Rust |
| 484 |
|
| 485 |
namespace std { |
| 486 |
template <> struct hash<Rust::PrimitiveCoreType> |
| 487 |
{ |
| 488 |
size_t operator() (const Rust::PrimitiveCoreType &coretype) const noexcept |
| 489 |
{ |
| 490 |
return hash<std::underlying_type<Rust::PrimitiveCoreType>::type> () ( |
| 491 |
static_cast<std::underlying_type<Rust::PrimitiveCoreType>::type> ( |
| 492 |
coretype)); |
| 493 |
} |
| 494 |
}; |
| 495 |
} // namespace std |
| 496 |
|
| 497 |
#endif |