| 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 |
#include "rust-codepoint.h" |
| 20 |
#include "rust-system.h" |
| 21 |
#include "rust-lex.h" |
| 22 |
#include "rust-diagnostics.h" |
| 23 |
#include "rust-linemap.h" |
| 24 |
#include "rust-edition.h" |
| 25 |
#include "safe-ctype.h" |
| 26 |
#include "cpplib.h" |
| 27 |
#include "rust-keyword-values.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
// TODO: move to separate compilation unit? |
| 31 |
// overload += for uint32_t to allow 32-bit encoded utf-8 to be added |
| 32 |
std::string & |
| 33 |
operator+= (std::string &str, Codepoint char32) |
| 34 |
{ |
| 35 |
if (char32.value < 0x80) |
| 36 |
{ |
| 37 |
str += static_cast<char> (char32.value); |
| 38 |
} |
| 39 |
else if (char32.value < (0x1F + 1) << (1 * 6)) |
| 40 |
{ |
| 41 |
str += static_cast<char> (0xC0 | ((char32.value >> 6) & 0x1F)); |
| 42 |
str += static_cast<char> (0x80 | ((char32.value >> 0) & 0x3F)); |
| 43 |
} |
| 44 |
else if (char32.value < (0x0F + 1) << (2 * 6)) |
| 45 |
{ |
| 46 |
str += static_cast<char> (0xE0 | ((char32.value >> 12) & 0x0F)); |
| 47 |
str += static_cast<char> (0x80 | ((char32.value >> 6) & 0x3F)); |
| 48 |
str += static_cast<char> (0x80 | ((char32.value >> 0) & 0x3F)); |
| 49 |
} |
| 50 |
else if (char32.value < (0x07 + 1) << (3 * 6)) |
| 51 |
{ |
| 52 |
str += static_cast<char> (0xF0 | ((char32.value >> 18) & 0x07)); |
| 53 |
str += static_cast<char> (0x80 | ((char32.value >> 12) & 0x3F)); |
| 54 |
str += static_cast<char> (0x80 | ((char32.value >> 6) & 0x3F)); |
| 55 |
str += static_cast<char> (0x80 | ((char32.value >> 0) & 0x3F)); |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
rust_debug ("Invalid unicode codepoint found: '%u' ", char32.value); |
| 60 |
} |
| 61 |
return str; |
| 62 |
} |
| 63 |
|
| 64 |
std::string |
| 65 |
Codepoint::as_string () |
| 66 |
{ |
| 67 |
std::string str; |
| 68 |
|
| 69 |
// str += Codepoint (value); |
| 70 |
str += *this; |
| 71 |
|
| 72 |
return str; |
| 73 |
} |
| 74 |
|
| 75 |
/* Includes all allowable float digits EXCEPT _ and . as that needs lookahead |
| 76 |
* for handling. */ |
| 77 |
bool |
| 78 |
is_float_digit (uint32_t number) |
| 79 |
{ |
| 80 |
return ISDIGIT (number) || number == 'E' || number == 'e'; |
| 81 |
} |
| 82 |
|
| 83 |
/* Basically ISXDIGIT from safe-ctype but may change if Rust's encoding or |
| 84 |
* whatever is different */ |
| 85 |
bool |
| 86 |
is_x_digit (uint32_t number) |
| 87 |
{ |
| 88 |
return ISXDIGIT (number); |
| 89 |
} |
| 90 |
|
| 91 |
bool |
| 92 |
is_octal_digit (uint32_t number) |
| 93 |
{ |
| 94 |
return number >= '0' && number <= '7'; |
| 95 |
} |
| 96 |
|
| 97 |
bool |
| 98 |
is_bin_digit (uint32_t number) |
| 99 |
{ |
| 100 |
return number == '0' || number == '1'; |
| 101 |
} |
| 102 |
|
| 103 |
bool |
| 104 |
check_valid_float_dot_end (uint32_t character) |
| 105 |
{ |
| 106 |
return character != '.' && character != '_' && !ISALPHA (character); |
| 107 |
} |
| 108 |
|
| 109 |
bool |
| 110 |
is_whitespace (uint32_t character) |
| 111 |
{ |
| 112 |
// https://doc.rust-lang.org/reference/whitespace.html |
| 113 |
return character == '\t' || character == '\n' || character == '\v' |
| 114 |
|| character == '\f' || character == '\r' || character == ' ' |
| 115 |
|| character == 0x0085 // next line |
| 116 |
|| character == 0x200e // left-to-right mark |
| 117 |
|| character == 0x200f // right-to-left mark |
| 118 |
|| character == 0x2028 // line separator |
| 119 |
|| character == 0x2029; // pragraph separator |
| 120 |
} |
| 121 |
|
| 122 |
bool |
| 123 |
is_non_decimal_int_literal_separator (uint32_t character) |
| 124 |
{ |
| 125 |
return character == 'x' || character == 'o' || character == 'b'; |
| 126 |
} |
| 127 |
|
| 128 |
bool |
| 129 |
is_identifier_start (uint32_t codepoint) |
| 130 |
{ |
| 131 |
return (cpp_check_xid_property (codepoint) & CPP_XID_START) |
| 132 |
|| codepoint == '_'; |
| 133 |
} |
| 134 |
|
| 135 |
bool |
| 136 |
is_identifier_continue (uint32_t codepoint) |
| 137 |
{ |
| 138 |
return cpp_check_xid_property (codepoint) & CPP_XID_CONTINUE; |
| 139 |
} |
| 140 |
|
| 141 |
Lexer::Lexer (const std::string &input, Linemap *linemap) |
| 142 |
: input (RAIIFile::create_error ()), current_line (1), current_column (1), |
| 143 |
line_map (linemap), dump_lex_out ({}), |
| 144 |
raw_input_source (new BufferInputSource (input, 0)), |
| 145 |
input_queue{*raw_input_source}, token_queue (TokenSource (this)) |
| 146 |
{} |
| 147 |
|
| 148 |
Lexer::Lexer (const char *filename, RAIIFile file_input, Linemap *linemap, |
| 149 |
tl::optional<std::ofstream &> dump_lex_opt) |
| 150 |
: input (std::move (file_input)), current_line (1), current_column (1), |
| 151 |
line_map (linemap), dump_lex_out (dump_lex_opt), |
| 152 |
raw_input_source (new FileInputSource (input.get_raw ())), |
| 153 |
input_queue{*raw_input_source}, token_queue (TokenSource (this)) |
| 154 |
{ |
| 155 |
// inform line_table that file is being entered and is in line 1 |
| 156 |
if (linemap) |
| 157 |
line_map->start_file (filename, current_line); |
| 158 |
} |
| 159 |
|
| 160 |
Lexer::~Lexer () |
| 161 |
{ |
| 162 |
/* ok apparently stop (which is equivalent of original code in destructor) is |
| 163 |
* meant to be called after all files have finished parsing, for cleanup. On |
| 164 |
* the other hand, actual code that it calls to leave a certain line map is |
| 165 |
* mentioned in GCC docs as being useful for "just leaving an included header" |
| 166 |
* and stuff like that, so this line mapping functionality may need fixing. |
| 167 |
* FIXME: find out whether this occurs. */ |
| 168 |
|
| 169 |
// line_map->stop(); |
| 170 |
} |
| 171 |
|
| 172 |
bool |
| 173 |
Lexer::input_source_is_valid_utf8 () |
| 174 |
{ |
| 175 |
return raw_input_source->is_valid (); |
| 176 |
} |
| 177 |
|
| 178 |
location_t |
| 179 |
Lexer::get_current_location () |
| 180 |
{ |
| 181 |
if (line_map) |
| 182 |
return linemap_position_for_column (line_table, current_column); |
| 183 |
else |
| 184 |
// If we have no linemap, we're lexing something without proper locations |
| 185 |
return UNDEF_LOCATION; |
| 186 |
} |
| 187 |
|
| 188 |
Codepoint |
| 189 |
Lexer::peek_input (int n) |
| 190 |
{ |
| 191 |
return input_queue.peek (n); |
| 192 |
} |
| 193 |
|
| 194 |
Codepoint |
| 195 |
Lexer::peek_input () |
| 196 |
{ |
| 197 |
return peek_input (0); |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
Lexer::skip_input (int n) |
| 202 |
{ |
| 203 |
input_queue.skip (n); |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
Lexer::skip_input () |
| 208 |
{ |
| 209 |
skip_input (0); |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
Lexer::skip_token (int n) |
| 214 |
{ |
| 215 |
// dump tokens if dump-lex option is enabled |
| 216 |
if (dump_lex_out.has_value ()) |
| 217 |
dump_and_skip (n); |
| 218 |
else |
| 219 |
token_queue.skip (n); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
Lexer::dump_and_skip (int n) |
| 224 |
{ |
| 225 |
std::ofstream &out = dump_lex_out.value (); |
| 226 |
bool found_eof = false; |
| 227 |
const_TokenPtr tok; |
| 228 |
for (int i = 0; i < n + 1; i++) |
| 229 |
{ |
| 230 |
if (!found_eof) |
| 231 |
{ |
| 232 |
tok = peek_token (); |
| 233 |
found_eof |= tok->get_id () == Rust::END_OF_FILE; |
| 234 |
|
| 235 |
location_t loc = tok->get_locus (); |
| 236 |
|
| 237 |
out << "<id="; |
| 238 |
out << tok->token_id_to_str (); |
| 239 |
out << (tok->has_str () ? (std::string (", text=") + tok->get_str () |
| 240 |
+ std::string (", typehint=") |
| 241 |
+ std::string (tok->get_type_hint_str ())) |
| 242 |
: "") |
| 243 |
<< " "; |
| 244 |
out << Linemap::location_to_string (loc) << '\n'; |
| 245 |
} |
| 246 |
|
| 247 |
token_queue.skip (0); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void |
| 252 |
Lexer::replace_current_token (TokenPtr replacement) |
| 253 |
{ |
| 254 |
token_queue.replace_current_value (replacement); |
| 255 |
|
| 256 |
rust_debug ("called 'replace_current_token' - this is deprecated"); |
| 257 |
} |
| 258 |
|
| 259 |
/* Determines whether the string passed in is a keyword or not. If it is, it |
| 260 |
* returns the keyword name. */ |
| 261 |
TokenId |
| 262 |
Lexer::classify_keyword (const std::string &str) |
| 263 |
{ |
| 264 |
auto &keywords = Rust::Values::Keywords::keywords_tokens; |
| 265 |
auto keyword = keywords.find (str); |
| 266 |
|
| 267 |
if (keyword == keywords.end ()) |
| 268 |
return IDENTIFIER; |
| 269 |
|
| 270 |
auto id = keyword->second; |
| 271 |
|
| 272 |
// We now have the expected token ID of the reserved keyword. However, some |
| 273 |
// keywords are reserved starting in certain editions. For example, `try` is |
| 274 |
// only a reserved keyword in editions >=2018. The language might gain new |
| 275 |
// reserved keywords in the future. |
| 276 |
// |
| 277 |
// https://doc.rust-lang.org/reference/keywords.html#reserved-keywords |
| 278 |
|
| 279 |
// `try` is not a reserved keyword before 2018 |
| 280 |
if (get_rust_edition () == Edition::E2015 && id == TRY) |
| 281 |
return IDENTIFIER; |
| 282 |
|
| 283 |
return id; |
| 284 |
} |
| 285 |
|
| 286 |
TokenPtr |
| 287 |
Lexer::build_token () |
| 288 |
{ |
| 289 |
// loop to go through multiple characters to build a single token |
| 290 |
while (true) |
| 291 |
{ |
| 292 |
location_t loc = get_current_location (); |
| 293 |
|
| 294 |
current_char = peek_input (); |
| 295 |
skip_input (); |
| 296 |
|
| 297 |
// detect shebang |
| 298 |
// Must be the first thing on the first line, starting with #! |
| 299 |
// But since an attribute can also start with an #! we don't count it as a |
| 300 |
// shebang line when after any whitespace or comments there is a [. If it |
| 301 |
// is a shebang line we simple drop the line. Otherwise we don't consume |
| 302 |
// any characters and fall through to the real tokenizer. |
| 303 |
if (current_line == 1 && current_column == 1 && current_char == '#' |
| 304 |
&& peek_input () == '!') |
| 305 |
{ |
| 306 |
int n = 1; |
| 307 |
while (true) |
| 308 |
{ |
| 309 |
Codepoint next_char = peek_input (n); |
| 310 |
if (is_whitespace (next_char.value)) |
| 311 |
n++; |
| 312 |
else if ((next_char == '/' && peek_input (n + 1) == '/' |
| 313 |
&& peek_input (n + 2) != '!' |
| 314 |
&& peek_input (n + 2) != '/') |
| 315 |
|| (next_char == '/' && peek_input (n + 1) == '/' |
| 316 |
&& peek_input (n + 2) == '/' |
| 317 |
&& peek_input (n + 3) == '/')) |
| 318 |
{ |
| 319 |
// two // or four //// |
| 320 |
// A single line comment |
| 321 |
// (but not an inner or outer doc comment) |
| 322 |
n += 2; |
| 323 |
next_char = peek_input (n); |
| 324 |
while (next_char != '\n' && !next_char.is_eof ()) |
| 325 |
{ |
| 326 |
n++; |
| 327 |
next_char = peek_input (n); |
| 328 |
} |
| 329 |
if (next_char == '\n') |
| 330 |
n++; |
| 331 |
} |
| 332 |
else if (next_char == '/' && peek_input (n + 1) == '*' |
| 333 |
&& peek_input (n + 2) == '*' |
| 334 |
&& peek_input (n + 3) == '/') |
| 335 |
{ |
| 336 |
/**/ |
| 337 |
n += 4; |
| 338 |
} |
| 339 |
else if (next_char == '/' && peek_input (n + 1) == '*' |
| 340 |
&& peek_input (n + 2) == '*' && peek_input (n + 3) == '*' |
| 341 |
&& peek_input (n + 4) == '/') |
| 342 |
{ |
| 343 |
/***/ |
| 344 |
n += 5; |
| 345 |
} |
| 346 |
else if ((next_char == '/' && peek_input (n + 1) == '*' |
| 347 |
&& peek_input (n + 2) != '*' |
| 348 |
&& peek_input (n + 2) != '!') |
| 349 |
|| (next_char == '/' && peek_input (n + 1) == '*' |
| 350 |
&& peek_input (n + 2) == '*' |
| 351 |
&& peek_input (n + 3) == '*')) |
| 352 |
{ |
| 353 |
// one /* or three /*** |
| 354 |
// Start of a block comment |
| 355 |
// (but not an inner or outer doc comment) |
| 356 |
n += 2; |
| 357 |
int level = 1; |
| 358 |
while (level > 0) |
| 359 |
{ |
| 360 |
if (peek_input (n).is_eof ()) |
| 361 |
break; |
| 362 |
else if (peek_input (n) == '/' |
| 363 |
&& peek_input (n + 1) == '*') |
| 364 |
{ |
| 365 |
n += 2; |
| 366 |
level += 1; |
| 367 |
} |
| 368 |
else if (peek_input (n) == '*' |
| 369 |
&& peek_input (n + 1) == '/') |
| 370 |
{ |
| 371 |
n += 2; |
| 372 |
level -= 1; |
| 373 |
} |
| 374 |
else |
| 375 |
n++; |
| 376 |
} |
| 377 |
} |
| 378 |
else if (next_char != '[') |
| 379 |
{ |
| 380 |
// definitely shebang, ignore the first line |
| 381 |
while (current_char != '\n' && !current_char.is_eof ()) |
| 382 |
{ |
| 383 |
current_char = peek_input (); |
| 384 |
skip_input (); |
| 385 |
} |
| 386 |
|
| 387 |
// newline |
| 388 |
current_line++; |
| 389 |
current_column = 1; |
| 390 |
// tell line_table that new line starts |
| 391 |
start_line (current_line, max_column_hint); |
| 392 |
break; |
| 393 |
} |
| 394 |
else |
| 395 |
break; /* Definitely not a shebang line. */ |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
// return end of file token if end of file |
| 400 |
if (current_char.is_eof ()) |
| 401 |
return Token::make (END_OF_FILE, loc); |
| 402 |
|
| 403 |
// if not end of file, start tokenising |
| 404 |
switch (current_char.value) |
| 405 |
{ |
| 406 |
/* ignore whitespace characters for tokens but continue updating |
| 407 |
* location */ |
| 408 |
case '\n': // newline |
| 409 |
case 0x0085: // next line |
| 410 |
case 0x2028: // line separator |
| 411 |
case 0x2029: // paragraph separator |
| 412 |
current_line++; |
| 413 |
current_column = 1; |
| 414 |
// tell line_table that new line starts |
| 415 |
start_line (current_line, max_column_hint); |
| 416 |
continue; |
| 417 |
case '\r': // cr |
| 418 |
// Ignore, we expect a newline (lf) soon. |
| 419 |
continue; |
| 420 |
case ' ': // space |
| 421 |
current_column++; |
| 422 |
continue; |
| 423 |
case '\t': // horizontal tab |
| 424 |
// width of a tab is not well-defined, assume 8 spaces |
| 425 |
current_column += 8; |
| 426 |
continue; |
| 427 |
case '\v': // vertical tab |
| 428 |
case 0x000c: // form feed |
| 429 |
case 0x200e: // left-to-right mark |
| 430 |
case 0x200f: // right-to-left mark |
| 431 |
// Ignored. |
| 432 |
continue; |
| 433 |
|
| 434 |
// punctuation - actual tokens |
| 435 |
case '=': |
| 436 |
if (peek_input () == '>') |
| 437 |
{ |
| 438 |
// match arm arrow |
| 439 |
skip_input (); |
| 440 |
current_column += 2; |
| 441 |
loc += 1; |
| 442 |
|
| 443 |
return Token::make (MATCH_ARROW, loc); |
| 444 |
} |
| 445 |
else if (peek_input () == '=') |
| 446 |
{ |
| 447 |
// equality operator |
| 448 |
skip_input (); |
| 449 |
current_column += 2; |
| 450 |
loc += 1; |
| 451 |
|
| 452 |
return Token::make (EQUAL_EQUAL, loc); |
| 453 |
} |
| 454 |
else |
| 455 |
{ |
| 456 |
// assignment operator |
| 457 |
current_column++; |
| 458 |
return Token::make (EQUAL, loc); |
| 459 |
} |
| 460 |
case '(': |
| 461 |
current_column++; |
| 462 |
return Token::make (LEFT_PAREN, loc); |
| 463 |
case '-': |
| 464 |
if (peek_input () == '>') |
| 465 |
{ |
| 466 |
// return type specifier |
| 467 |
skip_input (); |
| 468 |
current_column += 2; |
| 469 |
loc += 1; |
| 470 |
|
| 471 |
return Token::make (RETURN_TYPE, loc); |
| 472 |
} |
| 473 |
else if (peek_input () == '=') |
| 474 |
{ |
| 475 |
// minus-assign |
| 476 |
skip_input (); |
| 477 |
current_column += 2; |
| 478 |
loc += 1; |
| 479 |
|
| 480 |
return Token::make (MINUS_EQ, loc); |
| 481 |
} |
| 482 |
else |
| 483 |
{ |
| 484 |
// minus |
| 485 |
current_column++; |
| 486 |
return Token::make (MINUS, loc); |
| 487 |
} |
| 488 |
case '+': |
| 489 |
if (peek_input () == '=') |
| 490 |
{ |
| 491 |
// add-assign |
| 492 |
skip_input (); |
| 493 |
current_column += 2; |
| 494 |
loc += 1; |
| 495 |
|
| 496 |
return Token::make (PLUS_EQ, loc); |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
// add |
| 501 |
current_column++; |
| 502 |
return Token::make (PLUS, loc); |
| 503 |
} |
| 504 |
case ')': |
| 505 |
current_column++; |
| 506 |
return Token::make (RIGHT_PAREN, loc); |
| 507 |
case ';': |
| 508 |
current_column++; |
| 509 |
return Token::make (SEMICOLON, loc); |
| 510 |
case '*': |
| 511 |
if (peek_input () == '=') |
| 512 |
{ |
| 513 |
// multiplication-assign |
| 514 |
skip_input (); |
| 515 |
current_column += 2; |
| 516 |
loc += 1; |
| 517 |
|
| 518 |
return Token::make (ASTERISK_EQ, loc); |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
// multiplication |
| 523 |
current_column++; |
| 524 |
return Token::make (ASTERISK, loc); |
| 525 |
} |
| 526 |
case ',': |
| 527 |
current_column++; |
| 528 |
return Token::make (COMMA, loc); |
| 529 |
case '/': |
| 530 |
if (peek_input () == '=') |
| 531 |
{ |
| 532 |
// division-assign |
| 533 |
skip_input (); |
| 534 |
current_column += 2; |
| 535 |
loc += 1; |
| 536 |
|
| 537 |
return Token::make (DIV_EQ, loc); |
| 538 |
} |
| 539 |
else if ((peek_input () == '/' && peek_input (1) != '!' |
| 540 |
&& peek_input (1) != '/') |
| 541 |
|| (peek_input () == '/' && peek_input (1) == '/' |
| 542 |
&& peek_input (2) == '/')) |
| 543 |
{ |
| 544 |
// two // or four //// |
| 545 |
// single line comment |
| 546 |
// (but not an inner or outer doc comment) |
| 547 |
skip_input (); |
| 548 |
current_column += 2; |
| 549 |
current_char = peek_input (); |
| 550 |
|
| 551 |
// basically ignore until line finishes |
| 552 |
while (current_char != '\n' && !current_char.is_eof ()) |
| 553 |
{ |
| 554 |
skip_input (); |
| 555 |
current_column++; // not used |
| 556 |
current_char = peek_input (); |
| 557 |
} |
| 558 |
continue; |
| 559 |
} |
| 560 |
else if (peek_input () == '/' |
| 561 |
&& (peek_input (1) == '!' || peek_input (1) == '/')) |
| 562 |
{ |
| 563 |
/* single line doc comment, inner or outer. */ |
| 564 |
bool is_inner = peek_input (1) == '!'; |
| 565 |
skip_input (1); |
| 566 |
current_column += 3; |
| 567 |
|
| 568 |
std::string str; |
| 569 |
str.reserve (32); |
| 570 |
current_char = peek_input (); |
| 571 |
while (current_char != '\n') |
| 572 |
{ |
| 573 |
skip_input (); |
| 574 |
if (current_char == '\r') |
| 575 |
{ |
| 576 |
Codepoint next_char = peek_input (); |
| 577 |
if (next_char == '\n') |
| 578 |
{ |
| 579 |
current_char = '\n'; |
| 580 |
break; |
| 581 |
} |
| 582 |
rust_error_at ( |
| 583 |
loc, "Isolated CR %<\\r%> not allowed in doc comment"); |
| 584 |
current_char = next_char; |
| 585 |
continue; |
| 586 |
} |
| 587 |
if (current_char.is_eof ()) |
| 588 |
{ |
| 589 |
rust_error_at ( |
| 590 |
loc, ErrorCode::E0758, |
| 591 |
"unexpected EOF while looking for end of comment"); |
| 592 |
break; |
| 593 |
} |
| 594 |
str += current_char; |
| 595 |
current_char = peek_input (); |
| 596 |
} |
| 597 |
skip_input (); |
| 598 |
current_line++; |
| 599 |
current_column = 1; |
| 600 |
// tell line_table that new line starts |
| 601 |
start_line (current_line, max_column_hint); |
| 602 |
|
| 603 |
str.shrink_to_fit (); |
| 604 |
|
| 605 |
loc += str.size () - 1; |
| 606 |
if (is_inner) |
| 607 |
return Token::make_inner_doc_comment (loc, std::move (str)); |
| 608 |
else |
| 609 |
return Token::make_outer_doc_comment (loc, std::move (str)); |
| 610 |
} |
| 611 |
else if (peek_input () == '*' && peek_input (1) == '*' |
| 612 |
&& peek_input (2) == '/') |
| 613 |
{ |
| 614 |
/**/ |
| 615 |
skip_input (2); |
| 616 |
current_column += 4; |
| 617 |
continue; |
| 618 |
} |
| 619 |
else if (peek_input () == '*' && peek_input (1) == '*' |
| 620 |
&& peek_input (2) == '*' && peek_input (3) == '/') |
| 621 |
{ |
| 622 |
/***/ |
| 623 |
skip_input (3); |
| 624 |
current_column += 5; |
| 625 |
continue; |
| 626 |
} |
| 627 |
else if ((peek_input () == '*' && peek_input (1) != '!' |
| 628 |
&& peek_input (1) != '*') |
| 629 |
|| (peek_input () == '*' && peek_input (1) == '*' |
| 630 |
&& peek_input (2) == '*')) |
| 631 |
{ |
| 632 |
// one /* or three /*** |
| 633 |
// block comment |
| 634 |
// (but not an inner or outer doc comment) |
| 635 |
skip_input (); |
| 636 |
current_column += 2; |
| 637 |
|
| 638 |
int level = 1; |
| 639 |
while (level > 0) |
| 640 |
{ |
| 641 |
current_char = peek_input (); |
| 642 |
|
| 643 |
if (current_char.is_eof ()) |
| 644 |
{ |
| 645 |
rust_error_at ( |
| 646 |
loc, ErrorCode::E0758, |
| 647 |
"unexpected EOF while looking for end of comment"); |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
// if /* found |
| 652 |
if (current_char == '/' && peek_input (1) == '*') |
| 653 |
{ |
| 654 |
// skip /* characters |
| 655 |
skip_input (1); |
| 656 |
|
| 657 |
current_column += 2; |
| 658 |
|
| 659 |
level += 1; |
| 660 |
continue; |
| 661 |
} |
| 662 |
|
| 663 |
// ignore until */ is found |
| 664 |
if (current_char == '*' && peek_input (1) == '/') |
| 665 |
{ |
| 666 |
// skip */ characters |
| 667 |
skip_input (1); |
| 668 |
|
| 669 |
current_column += 2; |
| 670 |
|
| 671 |
level -= 1; |
| 672 |
continue; |
| 673 |
} |
| 674 |
|
| 675 |
if (current_char == '\n') |
| 676 |
{ |
| 677 |
skip_input (); |
| 678 |
current_line++; |
| 679 |
current_column = 1; |
| 680 |
// tell line_table that new line starts |
| 681 |
start_line (current_line, max_column_hint); |
| 682 |
continue; |
| 683 |
} |
| 684 |
|
| 685 |
skip_input (); |
| 686 |
current_column++; |
| 687 |
} |
| 688 |
|
| 689 |
// refresh new token |
| 690 |
continue; |
| 691 |
} |
| 692 |
else if (peek_input () == '*' |
| 693 |
&& (peek_input (1) == '!' || peek_input (1) == '*')) |
| 694 |
{ |
| 695 |
// block doc comment, inner /*! or outer /** |
| 696 |
bool is_inner = peek_input (1) == '!'; |
| 697 |
skip_input (1); |
| 698 |
current_column += 3; |
| 699 |
|
| 700 |
std::string str; |
| 701 |
str.reserve (96); |
| 702 |
|
| 703 |
int level = 1; |
| 704 |
while (level > 0) |
| 705 |
{ |
| 706 |
current_char = peek_input (); |
| 707 |
|
| 708 |
if (current_char.is_eof ()) |
| 709 |
{ |
| 710 |
rust_error_at ( |
| 711 |
loc, ErrorCode::E0758, |
| 712 |
"unexpected EOF while looking for end of comment"); |
| 713 |
break; |
| 714 |
} |
| 715 |
|
| 716 |
// if /* found |
| 717 |
if (current_char == '/' && peek_input (1) == '*') |
| 718 |
{ |
| 719 |
// skip /* characters |
| 720 |
skip_input (1); |
| 721 |
current_column += 2; |
| 722 |
|
| 723 |
level += 1; |
| 724 |
str += "/*"; |
| 725 |
continue; |
| 726 |
} |
| 727 |
|
| 728 |
// ignore until */ is found |
| 729 |
if (current_char == '*' && peek_input (1) == '/') |
| 730 |
{ |
| 731 |
// skip */ characters |
| 732 |
skip_input (1); |
| 733 |
current_column += 2; |
| 734 |
|
| 735 |
level -= 1; |
| 736 |
if (level > 0) |
| 737 |
str += "*/"; |
| 738 |
continue; |
| 739 |
} |
| 740 |
|
| 741 |
if (current_char == '\r' && peek_input (1) != '\n') |
| 742 |
rust_error_at ( |
| 743 |
loc, "Isolated CR %<\\r%> not allowed in doc comment"); |
| 744 |
|
| 745 |
if (current_char == '\n') |
| 746 |
{ |
| 747 |
skip_input (); |
| 748 |
current_line++; |
| 749 |
current_column = 1; |
| 750 |
// tell line_table that new line starts |
| 751 |
start_line (current_line, max_column_hint); |
| 752 |
str += '\n'; |
| 753 |
continue; |
| 754 |
} |
| 755 |
|
| 756 |
str += current_char; |
| 757 |
skip_input (); |
| 758 |
current_column++; |
| 759 |
} |
| 760 |
|
| 761 |
str.shrink_to_fit (); |
| 762 |
|
| 763 |
loc += str.size () - 1; |
| 764 |
if (is_inner) |
| 765 |
return Token::make_inner_doc_comment (loc, std::move (str)); |
| 766 |
else |
| 767 |
return Token::make_outer_doc_comment (loc, std::move (str)); |
| 768 |
} |
| 769 |
else |
| 770 |
{ |
| 771 |
// division |
| 772 |
current_column++; |
| 773 |
return Token::make (DIV, loc); |
| 774 |
} |
| 775 |
case '%': |
| 776 |
if (peek_input () == '=') |
| 777 |
{ |
| 778 |
// modulo-assign |
| 779 |
skip_input (); |
| 780 |
current_column += 2; |
| 781 |
loc += 1; |
| 782 |
|
| 783 |
return Token::make (PERCENT_EQ, loc); |
| 784 |
} |
| 785 |
else |
| 786 |
{ |
| 787 |
// modulo |
| 788 |
current_column++; |
| 789 |
return Token::make (PERCENT, loc); |
| 790 |
} |
| 791 |
case '^': |
| 792 |
if (peek_input () == '=') |
| 793 |
{ |
| 794 |
// xor-assign? |
| 795 |
skip_input (); |
| 796 |
current_column += 2; |
| 797 |
loc += 1; |
| 798 |
|
| 799 |
return Token::make (CARET_EQ, loc); |
| 800 |
} |
| 801 |
else |
| 802 |
{ |
| 803 |
// xor? |
| 804 |
current_column++; |
| 805 |
return Token::make (CARET, loc); |
| 806 |
} |
| 807 |
case '<': |
| 808 |
if (peek_input () == '<') |
| 809 |
{ |
| 810 |
if (peek_input (1) == '=') |
| 811 |
{ |
| 812 |
// left-shift assign |
| 813 |
skip_input (1); |
| 814 |
current_column += 3; |
| 815 |
loc += 2; |
| 816 |
|
| 817 |
return Token::make (LEFT_SHIFT_EQ, loc); |
| 818 |
} |
| 819 |
else |
| 820 |
{ |
| 821 |
// left-shift |
| 822 |
skip_input (); |
| 823 |
current_column += 2; |
| 824 |
loc += 1; |
| 825 |
|
| 826 |
return Token::make (LEFT_SHIFT, loc); |
| 827 |
} |
| 828 |
} |
| 829 |
else if (peek_input () == '=') |
| 830 |
{ |
| 831 |
// smaller than or equal to |
| 832 |
skip_input (); |
| 833 |
current_column += 2; |
| 834 |
loc += 1; |
| 835 |
|
| 836 |
return Token::make (LESS_OR_EQUAL, loc); |
| 837 |
} |
| 838 |
else |
| 839 |
{ |
| 840 |
// smaller than |
| 841 |
current_column++; |
| 842 |
return Token::make (LEFT_ANGLE, loc); |
| 843 |
} |
| 844 |
break; |
| 845 |
case '>': |
| 846 |
if (peek_input () == '>') |
| 847 |
{ |
| 848 |
if (peek_input (1) == '=') |
| 849 |
{ |
| 850 |
// right-shift-assign |
| 851 |
skip_input (1); |
| 852 |
current_column += 3; |
| 853 |
loc += 2; |
| 854 |
|
| 855 |
return Token::make (RIGHT_SHIFT_EQ, loc); |
| 856 |
} |
| 857 |
else |
| 858 |
{ |
| 859 |
// right-shift |
| 860 |
skip_input (); |
| 861 |
current_column += 2; |
| 862 |
loc += 1; |
| 863 |
|
| 864 |
return Token::make (RIGHT_SHIFT, loc); |
| 865 |
} |
| 866 |
} |
| 867 |
else if (peek_input () == '=') |
| 868 |
{ |
| 869 |
// larger than or equal to |
| 870 |
skip_input (); |
| 871 |
current_column += 2; |
| 872 |
loc += 1; |
| 873 |
|
| 874 |
return Token::make (GREATER_OR_EQUAL, loc); |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
// larger than |
| 879 |
current_column++; |
| 880 |
return Token::make (RIGHT_ANGLE, loc); |
| 881 |
} |
| 882 |
case ':': |
| 883 |
if (peek_input () == ':') |
| 884 |
{ |
| 885 |
// scope resolution :: |
| 886 |
skip_input (); |
| 887 |
current_column += 2; |
| 888 |
loc += 1; |
| 889 |
|
| 890 |
return Token::make (SCOPE_RESOLUTION, loc); |
| 891 |
} |
| 892 |
else |
| 893 |
{ |
| 894 |
// single colon : |
| 895 |
current_column++; |
| 896 |
return Token::make (COLON, loc); |
| 897 |
} |
| 898 |
case '!': |
| 899 |
// no special handling for macros in lexer? |
| 900 |
if (peek_input () == '=') |
| 901 |
{ |
| 902 |
// not equal boolean operator |
| 903 |
skip_input (); |
| 904 |
current_column += 2; |
| 905 |
loc += 1; |
| 906 |
|
| 907 |
return Token::make (NOT_EQUAL, loc); |
| 908 |
} |
| 909 |
else |
| 910 |
{ |
| 911 |
// not equal unary operator |
| 912 |
current_column++; |
| 913 |
|
| 914 |
return Token::make (EXCLAM, loc); |
| 915 |
} |
| 916 |
case '?': |
| 917 |
current_column++; |
| 918 |
return Token::make (QUESTION_MARK, loc); |
| 919 |
case '#': |
| 920 |
current_column++; |
| 921 |
return Token::make (HASH, loc); |
| 922 |
case '[': |
| 923 |
current_column++; |
| 924 |
return Token::make (LEFT_SQUARE, loc); |
| 925 |
case ']': |
| 926 |
current_column++; |
| 927 |
return Token::make (RIGHT_SQUARE, loc); |
| 928 |
case '{': |
| 929 |
current_column++; |
| 930 |
return Token::make (LEFT_CURLY, loc); |
| 931 |
case '}': |
| 932 |
current_column++; |
| 933 |
return Token::make (RIGHT_CURLY, loc); |
| 934 |
case '@': |
| 935 |
current_column++; |
| 936 |
return Token::make (PATTERN_BIND, loc); |
| 937 |
case '$': |
| 938 |
current_column++; |
| 939 |
return Token::make (DOLLAR_SIGN, loc); |
| 940 |
case '~': |
| 941 |
current_column++; |
| 942 |
return Token::make (TILDE, loc); |
| 943 |
case '\\': |
| 944 |
current_column++; |
| 945 |
return Token::make (BACKSLASH, loc); |
| 946 |
case '`': |
| 947 |
current_column++; |
| 948 |
return Token::make (BACKTICK, loc); |
| 949 |
case '|': |
| 950 |
if (peek_input () == '=') |
| 951 |
{ |
| 952 |
// bitwise or-assign? |
| 953 |
skip_input (); |
| 954 |
current_column += 2; |
| 955 |
loc += 1; |
| 956 |
|
| 957 |
return Token::make (PIPE_EQ, loc); |
| 958 |
} |
| 959 |
else if (peek_input () == '|') |
| 960 |
{ |
| 961 |
// logical or |
| 962 |
skip_input (); |
| 963 |
current_column += 2; |
| 964 |
loc += 1; |
| 965 |
|
| 966 |
return Token::make (OR, loc); |
| 967 |
} |
| 968 |
else |
| 969 |
{ |
| 970 |
// bitwise or |
| 971 |
current_column++; |
| 972 |
|
| 973 |
return Token::make (PIPE, loc); |
| 974 |
} |
| 975 |
case '&': |
| 976 |
if (peek_input () == '=') |
| 977 |
{ |
| 978 |
// bitwise and-assign? |
| 979 |
skip_input (); |
| 980 |
current_column += 2; |
| 981 |
loc += 1; |
| 982 |
|
| 983 |
return Token::make (AMP_EQ, loc); |
| 984 |
} |
| 985 |
else if (peek_input () == '&') |
| 986 |
{ |
| 987 |
// logical and |
| 988 |
skip_input (); |
| 989 |
current_column += 2; |
| 990 |
loc += 1; |
| 991 |
|
| 992 |
return Token::make (LOGICAL_AND, loc); |
| 993 |
} |
| 994 |
else |
| 995 |
{ |
| 996 |
// bitwise and/reference |
| 997 |
current_column++; |
| 998 |
|
| 999 |
return Token::make (AMP, loc); |
| 1000 |
} |
| 1001 |
case '.': |
| 1002 |
if (peek_input () == '.') |
| 1003 |
{ |
| 1004 |
if (peek_input (1) == '.') |
| 1005 |
{ |
| 1006 |
// ellipsis |
| 1007 |
skip_input (1); |
| 1008 |
current_column += 3; |
| 1009 |
loc += 2; |
| 1010 |
|
| 1011 |
return Token::make (ELLIPSIS, loc); |
| 1012 |
} |
| 1013 |
else if (peek_input (1) == '=') |
| 1014 |
{ |
| 1015 |
// ..= |
| 1016 |
skip_input (1); |
| 1017 |
current_column += 3; |
| 1018 |
loc += 2; |
| 1019 |
|
| 1020 |
return Token::make (DOT_DOT_EQ, loc); |
| 1021 |
} |
| 1022 |
else |
| 1023 |
{ |
| 1024 |
// .. |
| 1025 |
skip_input (); |
| 1026 |
current_column += 2; |
| 1027 |
loc += 1; |
| 1028 |
|
| 1029 |
return Token::make (DOT_DOT, loc); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
else /*if (!ISDIGIT (peek_input ()))*/ |
| 1033 |
{ |
| 1034 |
// single dot . |
| 1035 |
// Only if followed by a non-number - otherwise is float |
| 1036 |
// nope, float cannot start with '.'. |
| 1037 |
current_column++; |
| 1038 |
return Token::make (DOT, loc); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
// TODO: special handling of _ in the lexer? instead of being identifier |
| 1042 |
|
| 1043 |
// byte character, byte string and raw byte string literals |
| 1044 |
if (current_char == 'b') |
| 1045 |
{ |
| 1046 |
if (peek_input () == '\'') |
| 1047 |
return parse_byte_char (loc); |
| 1048 |
else if (peek_input () == '"') |
| 1049 |
return parse_byte_string (loc); |
| 1050 |
else if (peek_input () == 'r' |
| 1051 |
&& (peek_input (1) == '#' || peek_input (1) == '"')) |
| 1052 |
return parse_raw_byte_string (loc); |
| 1053 |
} |
| 1054 |
|
| 1055 |
// raw identifiers and raw strings |
| 1056 |
if (current_char == 'r') |
| 1057 |
{ |
| 1058 |
Codepoint peek = peek_input (); |
| 1059 |
Codepoint peek1 = peek_input (1); |
| 1060 |
|
| 1061 |
// TODO (tamaron) parse Unicode ident |
| 1062 |
if (peek == '#' && is_identifier_start (peek1.value)) |
| 1063 |
{ |
| 1064 |
TokenPtr raw_ident_ptr = parse_raw_identifier (loc); |
| 1065 |
if (raw_ident_ptr != nullptr) |
| 1066 |
return raw_ident_ptr; |
| 1067 |
else |
| 1068 |
continue; /* input got parsed, it just wasn't valid. An error |
| 1069 |
was produced. */ |
| 1070 |
} |
| 1071 |
else |
| 1072 |
{ |
| 1073 |
TokenPtr maybe_raw_string_ptr = maybe_parse_raw_string (loc); |
| 1074 |
if (maybe_raw_string_ptr != nullptr) |
| 1075 |
return maybe_raw_string_ptr; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
// find identifiers and keywords. |
| 1080 |
if (is_identifier_start (current_char.value)) |
| 1081 |
return parse_identifier_or_keyword (loc); |
| 1082 |
|
| 1083 |
// int and float literals |
| 1084 |
if (ISDIGIT (current_char.value)) |
| 1085 |
{ // _ not allowed as first char |
| 1086 |
if (current_char == '0' |
| 1087 |
&& is_non_decimal_int_literal_separator (peek_input ().value)) |
| 1088 |
{ |
| 1089 |
// handle binary, octal, hex literals |
| 1090 |
TokenPtr non_dec_int_lit_ptr |
| 1091 |
= parse_non_decimal_int_literals (loc); |
| 1092 |
if (non_dec_int_lit_ptr != nullptr) |
| 1093 |
return non_dec_int_lit_ptr; |
| 1094 |
} |
| 1095 |
else |
| 1096 |
{ |
| 1097 |
// handle decimals (integer or float) |
| 1098 |
TokenPtr decimal_or_float_ptr = parse_decimal_int_or_float (loc); |
| 1099 |
if (decimal_or_float_ptr != nullptr) |
| 1100 |
return decimal_or_float_ptr; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
// string literals |
| 1105 |
if (current_char == '"') |
| 1106 |
return parse_string (loc); |
| 1107 |
|
| 1108 |
// char literals and lifetime names |
| 1109 |
if (current_char == '\'') |
| 1110 |
{ |
| 1111 |
TokenPtr char_or_lifetime_ptr = parse_char_or_lifetime (loc); |
| 1112 |
if (char_or_lifetime_ptr != nullptr) |
| 1113 |
return char_or_lifetime_ptr; |
| 1114 |
} |
| 1115 |
|
| 1116 |
// DEBUG: check for specific character problems: |
| 1117 |
if (current_char == '0') |
| 1118 |
rust_debug ("'0' uncaught before unexpected character"); |
| 1119 |
else if (current_char == ']') |
| 1120 |
rust_debug ("']' uncaught before unexpected character"); |
| 1121 |
else if (current_char == 0x5d) |
| 1122 |
rust_debug ("whatever 0x5d is (not '0' or ']') uncaught before " |
| 1123 |
"unexpected character"); |
| 1124 |
|
| 1125 |
// didn't match anything so error |
| 1126 |
rust_error_at (loc, "unexpected character %<%x%>", current_char.value); |
| 1127 |
current_column++; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
// Parses in a type suffix. |
| 1132 |
std::pair<PrimitiveCoreType, int> |
| 1133 |
Lexer::parse_in_type_suffix () |
| 1134 |
{ |
| 1135 |
std::string suffix; |
| 1136 |
suffix.reserve (5); |
| 1137 |
|
| 1138 |
int additional_length_offset = 0; |
| 1139 |
|
| 1140 |
// get suffix |
| 1141 |
while (ISALPHA (current_char.value) || ISDIGIT (current_char.value) |
| 1142 |
|| current_char == '_') |
| 1143 |
{ |
| 1144 |
if (current_char == '_') |
| 1145 |
{ |
| 1146 |
// don't add _ to suffix |
| 1147 |
skip_input (); |
| 1148 |
current_char = peek_input (); |
| 1149 |
|
| 1150 |
additional_length_offset++; |
| 1151 |
|
| 1152 |
continue; |
| 1153 |
} |
| 1154 |
|
| 1155 |
additional_length_offset++; |
| 1156 |
|
| 1157 |
suffix += current_char; |
| 1158 |
skip_input (); |
| 1159 |
current_char = peek_input (); |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (suffix.empty ()) |
| 1163 |
{ |
| 1164 |
// no type suffix: do nothing but also no error |
| 1165 |
return std::make_pair (CORETYPE_UNKNOWN, additional_length_offset); |
| 1166 |
} |
| 1167 |
else if (suffix == "f32") |
| 1168 |
{ |
| 1169 |
return std::make_pair (CORETYPE_F32, additional_length_offset); |
| 1170 |
} |
| 1171 |
else if (suffix == "f64") |
| 1172 |
{ |
| 1173 |
return std::make_pair (CORETYPE_F64, additional_length_offset); |
| 1174 |
} |
| 1175 |
else if (suffix == "i8") |
| 1176 |
{ |
| 1177 |
return std::make_pair (CORETYPE_I8, additional_length_offset); |
| 1178 |
} |
| 1179 |
else if (suffix == "i16") |
| 1180 |
{ |
| 1181 |
return std::make_pair (CORETYPE_I16, additional_length_offset); |
| 1182 |
} |
| 1183 |
else if (suffix == "i32") |
| 1184 |
{ |
| 1185 |
return std::make_pair (CORETYPE_I32, additional_length_offset); |
| 1186 |
} |
| 1187 |
else if (suffix == "i64") |
| 1188 |
{ |
| 1189 |
return std::make_pair (CORETYPE_I64, additional_length_offset); |
| 1190 |
} |
| 1191 |
else if (suffix == "i128") |
| 1192 |
{ |
| 1193 |
return std::make_pair (CORETYPE_I128, additional_length_offset); |
| 1194 |
} |
| 1195 |
else if (suffix == "isize") |
| 1196 |
{ |
| 1197 |
return std::make_pair (CORETYPE_ISIZE, additional_length_offset); |
| 1198 |
} |
| 1199 |
else if (suffix == "u8") |
| 1200 |
{ |
| 1201 |
return std::make_pair (CORETYPE_U8, additional_length_offset); |
| 1202 |
} |
| 1203 |
else if (suffix == "u16") |
| 1204 |
{ |
| 1205 |
return std::make_pair (CORETYPE_U16, additional_length_offset); |
| 1206 |
} |
| 1207 |
else if (suffix == "u32") |
| 1208 |
{ |
| 1209 |
return std::make_pair (CORETYPE_U32, additional_length_offset); |
| 1210 |
} |
| 1211 |
else if (suffix == "u64") |
| 1212 |
{ |
| 1213 |
return std::make_pair (CORETYPE_U64, additional_length_offset); |
| 1214 |
} |
| 1215 |
else if (suffix == "u128") |
| 1216 |
{ |
| 1217 |
return std::make_pair (CORETYPE_U128, additional_length_offset); |
| 1218 |
} |
| 1219 |
else if (suffix == "usize") |
| 1220 |
{ |
| 1221 |
return std::make_pair (CORETYPE_USIZE, additional_length_offset); |
| 1222 |
} |
| 1223 |
else |
| 1224 |
{ |
| 1225 |
rust_error_at (get_current_location (), "unknown number suffix %qs", |
| 1226 |
suffix.c_str ()); |
| 1227 |
|
| 1228 |
return std::make_pair (CORETYPE_UNKNOWN, additional_length_offset); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
// Parses in the exponent part (if any) of a float literal. |
| 1233 |
std::pair<std::string, int> |
| 1234 |
Lexer::parse_in_exponent_part () |
| 1235 |
{ |
| 1236 |
int additional_length_offset = 0; |
| 1237 |
std::string str; |
| 1238 |
if (current_char == 'E' || current_char == 'e') |
| 1239 |
{ |
| 1240 |
// add exponent to string as strtod works with it |
| 1241 |
str += current_char; |
| 1242 |
skip_input (); |
| 1243 |
current_char = peek_input (); |
| 1244 |
|
| 1245 |
additional_length_offset++; |
| 1246 |
|
| 1247 |
// special - and + handling |
| 1248 |
if (current_char == '-') |
| 1249 |
{ |
| 1250 |
str += '-'; |
| 1251 |
|
| 1252 |
skip_input (); |
| 1253 |
current_char = peek_input (); |
| 1254 |
|
| 1255 |
additional_length_offset++; |
| 1256 |
} |
| 1257 |
else if (current_char == '+') |
| 1258 |
{ |
| 1259 |
// don't add + but still skip input |
| 1260 |
skip_input (); |
| 1261 |
current_char = peek_input (); |
| 1262 |
|
| 1263 |
additional_length_offset++; |
| 1264 |
} |
| 1265 |
|
| 1266 |
// parse another decimal number for exponent |
| 1267 |
auto str_length = parse_in_decimal (); |
| 1268 |
str += std::get<0> (str_length); |
| 1269 |
additional_length_offset += std::get<1> (str_length); |
| 1270 |
} |
| 1271 |
return std::make_pair (str, additional_length_offset); |
| 1272 |
} |
| 1273 |
|
| 1274 |
// Parses a decimal integer. |
| 1275 |
std::tuple<std::string, int, bool> |
| 1276 |
Lexer::parse_in_decimal () |
| 1277 |
{ |
| 1278 |
/* A pure decimal contains only digits. */ |
| 1279 |
bool pure_decimal = true; |
| 1280 |
int additional_length_offset = 0; |
| 1281 |
std::string str; |
| 1282 |
while (ISDIGIT (current_char.value) || current_char.value == '_') |
| 1283 |
{ |
| 1284 |
if (current_char == '_') |
| 1285 |
{ |
| 1286 |
pure_decimal = false; |
| 1287 |
// don't add _ to number |
| 1288 |
skip_input (); |
| 1289 |
current_char = peek_input (); |
| 1290 |
|
| 1291 |
additional_length_offset++; |
| 1292 |
|
| 1293 |
continue; |
| 1294 |
} |
| 1295 |
|
| 1296 |
additional_length_offset++; |
| 1297 |
|
| 1298 |
str += current_char; |
| 1299 |
skip_input (); |
| 1300 |
current_char = peek_input (); |
| 1301 |
} |
| 1302 |
return std::make_tuple (str, additional_length_offset, pure_decimal); |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* Parses escapes (and string continues) in "byte" strings and characters. Does |
| 1306 |
* not support unicode. */ |
| 1307 |
std::tuple<char, int, bool> |
| 1308 |
Lexer::parse_escape (char opening_char) |
| 1309 |
{ |
| 1310 |
int additional_length_offset = 0; |
| 1311 |
char output_char = 0; |
| 1312 |
|
| 1313 |
// skip to actual letter |
| 1314 |
skip_input (); |
| 1315 |
current_char = peek_input (); |
| 1316 |
additional_length_offset++; |
| 1317 |
|
| 1318 |
switch (current_char.value) |
| 1319 |
{ |
| 1320 |
case 'x': { |
| 1321 |
auto hex_escape_pair = parse_partial_hex_escape (); |
| 1322 |
long hexLong = hex_escape_pair.first; |
| 1323 |
additional_length_offset += hex_escape_pair.second; |
| 1324 |
|
| 1325 |
if (hexLong > 255 || hexLong < 0) |
| 1326 |
rust_error_at ( |
| 1327 |
get_current_location (), |
| 1328 |
"byte \\x escape %<\\x%x%> out of range - allows up to %<\\xFF%>", |
| 1329 |
static_cast<unsigned int> (hexLong)); |
| 1330 |
/* TODO: restore capital for escape output - gcc pretty-printer doesn't |
| 1331 |
* support %X directly */ |
| 1332 |
char hexChar = static_cast<char> (hexLong); |
| 1333 |
|
| 1334 |
output_char = hexChar; |
| 1335 |
} |
| 1336 |
break; |
| 1337 |
case 'n': |
| 1338 |
output_char = '\n'; |
| 1339 |
break; |
| 1340 |
case 'r': |
| 1341 |
output_char = '\r'; |
| 1342 |
break; |
| 1343 |
case 't': |
| 1344 |
output_char = '\t'; |
| 1345 |
break; |
| 1346 |
case '\\': |
| 1347 |
output_char = '\\'; |
| 1348 |
break; |
| 1349 |
case '0': |
| 1350 |
output_char = '\0'; |
| 1351 |
break; |
| 1352 |
case '\'': |
| 1353 |
output_char = '\''; |
| 1354 |
break; |
| 1355 |
case '"': |
| 1356 |
output_char = '"'; |
| 1357 |
break; |
| 1358 |
case 'u': |
| 1359 |
rust_error_at (get_current_location (), |
| 1360 |
"cannot have a unicode escape \\u in a byte %s", |
| 1361 |
opening_char == '\'' ? "character" : "string"); |
| 1362 |
// Try to parse it anyway, just to skip it |
| 1363 |
parse_partial_unicode_escape (); |
| 1364 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1365 |
case '\r': |
| 1366 |
case '\n': |
| 1367 |
// string continue |
| 1368 |
return std::make_tuple (0, parse_partial_string_continue (), true); |
| 1369 |
default: |
| 1370 |
rust_error_at (get_current_location (), |
| 1371 |
"unknown escape sequence %<\\%s%>", |
| 1372 |
current_char.as_string ().c_str ()); |
| 1373 |
// returns false if no parsing could be done |
| 1374 |
// return false; |
| 1375 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1376 |
break; |
| 1377 |
} |
| 1378 |
// all non-special cases (string continue) should skip their used char |
| 1379 |
skip_input (); |
| 1380 |
current_char = peek_input (); |
| 1381 |
additional_length_offset++; |
| 1382 |
|
| 1383 |
// returns true if parsing was successful |
| 1384 |
// return true; |
| 1385 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* Parses an escape (or string continue) in a string or character. Supports |
| 1389 |
* unicode escapes. */ |
| 1390 |
std::tuple<Codepoint, int, bool> |
| 1391 |
Lexer::parse_utf8_escape () |
| 1392 |
{ |
| 1393 |
Codepoint output_char; |
| 1394 |
int additional_length_offset = 0; |
| 1395 |
|
| 1396 |
// skip to actual letter |
| 1397 |
skip_input (); |
| 1398 |
current_char = peek_input (); |
| 1399 |
additional_length_offset++; |
| 1400 |
|
| 1401 |
switch (current_char.value) |
| 1402 |
{ |
| 1403 |
case 'x': { |
| 1404 |
auto hex_escape_pair = parse_partial_hex_escape (); |
| 1405 |
long hexLong = hex_escape_pair.first; |
| 1406 |
additional_length_offset += hex_escape_pair.second; |
| 1407 |
|
| 1408 |
if (hexLong > 127 || hexLong < 0) |
| 1409 |
rust_error_at ( |
| 1410 |
get_current_location (), |
| 1411 |
"ascii \\x escape %<\\x%x%> out of range - allows up to %<\\x7F%>", |
| 1412 |
static_cast<unsigned int> (hexLong)); |
| 1413 |
/* TODO: restore capital for escape output - gcc pretty-printer doesn't |
| 1414 |
* support %X directly */ |
| 1415 |
char hexChar = static_cast<char> (hexLong); |
| 1416 |
|
| 1417 |
output_char = hexChar; |
| 1418 |
} |
| 1419 |
break; |
| 1420 |
case 'n': |
| 1421 |
output_char = '\n'; |
| 1422 |
break; |
| 1423 |
case 'r': |
| 1424 |
output_char = '\r'; |
| 1425 |
break; |
| 1426 |
case 't': |
| 1427 |
output_char = '\t'; |
| 1428 |
break; |
| 1429 |
case '\\': |
| 1430 |
output_char = '\\'; |
| 1431 |
break; |
| 1432 |
case '0': |
| 1433 |
output_char = '\0'; |
| 1434 |
break; |
| 1435 |
case '\'': |
| 1436 |
output_char = '\''; |
| 1437 |
break; |
| 1438 |
case '"': |
| 1439 |
output_char = '"'; |
| 1440 |
break; |
| 1441 |
case 'u': { |
| 1442 |
auto unicode_escape_pair = parse_partial_unicode_escape (); |
| 1443 |
output_char = unicode_escape_pair.first; |
| 1444 |
additional_length_offset += unicode_escape_pair.second; |
| 1445 |
|
| 1446 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1447 |
} |
| 1448 |
break; |
| 1449 |
case '\r': |
| 1450 |
case '\n': |
| 1451 |
// string continue |
| 1452 |
return std::make_tuple (0, parse_partial_string_continue (), true); |
| 1453 |
default: |
| 1454 |
rust_error_at (get_current_location (), |
| 1455 |
"unknown escape sequence %<\\%s%>", |
| 1456 |
current_char.as_string ().c_str ()); |
| 1457 |
// returns false if no parsing could be done |
| 1458 |
// return false; |
| 1459 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1460 |
break; |
| 1461 |
} |
| 1462 |
/* all non-special cases (unicode, string continue) should skip their used |
| 1463 |
* char */ |
| 1464 |
skip_input (); |
| 1465 |
current_char = peek_input (); |
| 1466 |
additional_length_offset++; |
| 1467 |
|
| 1468 |
// returns true if parsing was successful |
| 1469 |
// return true; |
| 1470 |
return std::make_tuple (output_char, additional_length_offset, false); |
| 1471 |
} |
| 1472 |
|
| 1473 |
// Parses the body of a string continue that has been found in an escape. |
| 1474 |
int |
| 1475 |
Lexer::parse_partial_string_continue () |
| 1476 |
{ |
| 1477 |
int additional_length_offset = 1; |
| 1478 |
|
| 1479 |
// string continue |
| 1480 |
// TODO use utf-8 codepoint to skip whitespaces |
| 1481 |
while (is_whitespace (current_char.value)) |
| 1482 |
{ |
| 1483 |
if (current_char == '\n') |
| 1484 |
{ |
| 1485 |
current_line++; |
| 1486 |
current_column = 1; |
| 1487 |
// tell line_table that new line starts |
| 1488 |
start_line (current_line, max_column_hint); |
| 1489 |
|
| 1490 |
// reset "length" |
| 1491 |
additional_length_offset = 1; |
| 1492 |
|
| 1493 |
// get next char |
| 1494 |
skip_input (); |
| 1495 |
current_char = peek_input (); |
| 1496 |
|
| 1497 |
continue; |
| 1498 |
} |
| 1499 |
|
| 1500 |
skip_input (); |
| 1501 |
current_char = peek_input (); |
| 1502 |
additional_length_offset++; |
| 1503 |
} |
| 1504 |
|
| 1505 |
return additional_length_offset; |
| 1506 |
} |
| 1507 |
|
| 1508 |
/* Parses the body of a '\x' escape. Note that it does not check that the number |
| 1509 |
* is valid and smaller than 255. */ |
| 1510 |
std::pair<long, int> |
| 1511 |
Lexer::parse_partial_hex_escape () |
| 1512 |
{ |
| 1513 |
// hex char string (null-terminated) |
| 1514 |
char hexNum[3] = {0, 0, 0}; |
| 1515 |
|
| 1516 |
// first hex char |
| 1517 |
current_char = peek_input (1); |
| 1518 |
int additional_length_offset = 1; |
| 1519 |
|
| 1520 |
if (!is_x_digit (current_char.value)) |
| 1521 |
{ |
| 1522 |
rust_error_at (get_current_location (), |
| 1523 |
"invalid character %<\\x%s%> in \\x sequence", |
| 1524 |
current_char.as_string ().c_str ()); |
| 1525 |
return std::make_pair (0, 0); |
| 1526 |
} |
| 1527 |
hexNum[0] = current_char.value; |
| 1528 |
|
| 1529 |
// second hex char |
| 1530 |
skip_input (); |
| 1531 |
current_char = peek_input (1); |
| 1532 |
additional_length_offset++; |
| 1533 |
|
| 1534 |
if (!is_x_digit (current_char.value)) |
| 1535 |
{ |
| 1536 |
rust_error_at (get_current_location (), |
| 1537 |
"invalid character %<\\x%c%s%> in \\x sequence", hexNum[0], |
| 1538 |
current_char.as_string ().c_str ()); |
| 1539 |
return std::make_pair (0, 1); |
| 1540 |
} |
| 1541 |
skip_input (); |
| 1542 |
hexNum[1] = current_char.value; |
| 1543 |
|
| 1544 |
long hexLong = std::strtol (hexNum, nullptr, 16); |
| 1545 |
|
| 1546 |
return std::make_pair (hexLong, additional_length_offset); |
| 1547 |
} |
| 1548 |
|
| 1549 |
// Parses the body of a unicode escape. |
| 1550 |
std::pair<Codepoint, int> |
| 1551 |
Lexer::parse_partial_unicode_escape () |
| 1552 |
{ |
| 1553 |
skip_input (); |
| 1554 |
current_char = peek_input (); |
| 1555 |
int additional_length_offset = 0; |
| 1556 |
|
| 1557 |
if (current_char != '{') |
| 1558 |
{ |
| 1559 |
rust_error_at (get_current_location (), |
| 1560 |
"unicode escape should start with %<{%>"); |
| 1561 |
/* Skip what should probaby have been between brackets. */ |
| 1562 |
while (is_x_digit (current_char.value) || current_char == '_') |
| 1563 |
{ |
| 1564 |
skip_input (); |
| 1565 |
current_char = peek_input (); |
| 1566 |
additional_length_offset++; |
| 1567 |
} |
| 1568 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1569 |
} |
| 1570 |
|
| 1571 |
skip_input (); |
| 1572 |
current_char = peek_input (); |
| 1573 |
additional_length_offset++; |
| 1574 |
|
| 1575 |
if (current_char == '_') |
| 1576 |
{ |
| 1577 |
rust_error_at (get_current_location (), |
| 1578 |
"unicode escape cannot start with %<_%>"); |
| 1579 |
skip_input (); |
| 1580 |
current_char = peek_input (); |
| 1581 |
additional_length_offset++; |
| 1582 |
// fallthrough and try to parse the rest anyway |
| 1583 |
} |
| 1584 |
|
| 1585 |
// parse unicode escape - 1-6 hex digits |
| 1586 |
std::string num_str; |
| 1587 |
num_str.reserve (6); |
| 1588 |
|
| 1589 |
// loop through to add entire hex number to string |
| 1590 |
while (is_x_digit (current_char.value) || current_char.value == '_') |
| 1591 |
{ |
| 1592 |
if (current_char == '_') |
| 1593 |
{ |
| 1594 |
// don't add _ to number |
| 1595 |
skip_input (); |
| 1596 |
current_char = peek_input (); |
| 1597 |
|
| 1598 |
additional_length_offset++; |
| 1599 |
|
| 1600 |
continue; |
| 1601 |
} |
| 1602 |
|
| 1603 |
additional_length_offset++; |
| 1604 |
|
| 1605 |
// add raw hex numbers |
| 1606 |
num_str += current_char; |
| 1607 |
|
| 1608 |
skip_input (); |
| 1609 |
current_char = peek_input (); |
| 1610 |
} |
| 1611 |
|
| 1612 |
if (current_char == '}') |
| 1613 |
{ |
| 1614 |
skip_input (); |
| 1615 |
current_char = peek_input (); |
| 1616 |
additional_length_offset++; |
| 1617 |
} |
| 1618 |
else |
| 1619 |
{ |
| 1620 |
// actually an error, but allow propagation anyway Assume that |
| 1621 |
// wrong bracketm whitespace or single/double quotes are wrong |
| 1622 |
// termination, otherwise it is a wrong character, then skip to the actual |
| 1623 |
// terminator. |
| 1624 |
// TODO use utf-8 codepoint to skip whitespaces |
| 1625 |
if (current_char == '{' || is_whitespace (current_char.value) |
| 1626 |
|| current_char == '\'' || current_char == '"') |
| 1627 |
{ |
| 1628 |
rust_error_at (get_current_location (), |
| 1629 |
"expected terminating %<}%> in unicode escape"); |
| 1630 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1631 |
} |
| 1632 |
else |
| 1633 |
{ |
| 1634 |
rust_error_at (get_current_location (), |
| 1635 |
"invalid character %qs in unicode escape", |
| 1636 |
current_char.as_string ().c_str ()); |
| 1637 |
// TODO use utf-8 codepoint to skip whitespaces |
| 1638 |
while (current_char != '}' && current_char != '{' |
| 1639 |
&& !is_whitespace (current_char.value) && current_char != '\'' |
| 1640 |
&& current_char != '"') |
| 1641 |
{ |
| 1642 |
skip_input (); |
| 1643 |
current_char = peek_input (); |
| 1644 |
additional_length_offset++; |
| 1645 |
} |
| 1646 |
// Consume the actual closing bracket if found |
| 1647 |
if (current_char == '}') |
| 1648 |
{ |
| 1649 |
skip_input (); |
| 1650 |
current_char = peek_input (); |
| 1651 |
additional_length_offset++; |
| 1652 |
} |
| 1653 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
// ensure 1-6 hex characters |
| 1658 |
if (num_str.length () > 6 || num_str.length () < 1) |
| 1659 |
{ |
| 1660 |
rust_error_at (get_current_location (), |
| 1661 |
"unicode escape should be between 1 and 6 hex " |
| 1662 |
"characters; it is %lu", |
| 1663 |
(unsigned long) num_str.length ()); |
| 1664 |
// return false; |
| 1665 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1666 |
} |
| 1667 |
|
| 1668 |
unsigned long hex_num = std::strtoul (num_str.c_str (), nullptr, 16); |
| 1669 |
|
| 1670 |
if (hex_num > 0xd7ff && hex_num < 0xe000) |
| 1671 |
{ |
| 1672 |
rust_error_at ( |
| 1673 |
get_current_location (), |
| 1674 |
"unicode escape cannot be a surrogate value (D800 to DFFF)"); |
| 1675 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1676 |
} |
| 1677 |
|
| 1678 |
if (hex_num > 0x10ffff) |
| 1679 |
{ |
| 1680 |
rust_error_at (get_current_location (), |
| 1681 |
"unicode escape cannot be larger than 10FFFF"); |
| 1682 |
return std::make_pair (Codepoint (0), additional_length_offset); |
| 1683 |
} |
| 1684 |
|
| 1685 |
// return true; |
| 1686 |
return std::make_pair (Codepoint (static_cast<uint32_t> (hex_num)), |
| 1687 |
additional_length_offset); |
| 1688 |
} |
| 1689 |
|
| 1690 |
// Parses a byte character. |
| 1691 |
TokenPtr |
| 1692 |
Lexer::parse_byte_char (location_t loc) |
| 1693 |
{ |
| 1694 |
skip_input (); |
| 1695 |
current_column++; |
| 1696 |
// make current char the next character |
| 1697 |
current_char = peek_input (); |
| 1698 |
|
| 1699 |
int length = 1; |
| 1700 |
|
| 1701 |
// char to save |
| 1702 |
Codepoint byte_char = 0; |
| 1703 |
|
| 1704 |
// detect escapes |
| 1705 |
if (current_char == '\\') |
| 1706 |
{ |
| 1707 |
auto escape_length_pair = parse_escape ('\''); |
| 1708 |
byte_char = std::get<0> (escape_length_pair); |
| 1709 |
length += std::get<1> (escape_length_pair); |
| 1710 |
|
| 1711 |
current_char = peek_input (); |
| 1712 |
|
| 1713 |
if (current_char != '\'') |
| 1714 |
{ |
| 1715 |
rust_error_at (get_current_location (), "unclosed %<byte char%>"); |
| 1716 |
} |
| 1717 |
|
| 1718 |
skip_input (); |
| 1719 |
current_char = peek_input (); |
| 1720 |
length++; // go to next char |
| 1721 |
} |
| 1722 |
else if (current_char != '\'') |
| 1723 |
{ |
| 1724 |
// otherwise, get character from direct input character |
| 1725 |
byte_char = current_char; |
| 1726 |
|
| 1727 |
if (!byte_char.is_ascii ()) |
| 1728 |
{ |
| 1729 |
rust_error_at (get_current_location (), |
| 1730 |
"non-ASCII character in %<byte char%>"); |
| 1731 |
} |
| 1732 |
|
| 1733 |
skip_input (); |
| 1734 |
current_char = peek_input (); |
| 1735 |
length++; |
| 1736 |
|
| 1737 |
if (current_char != '\'') |
| 1738 |
{ |
| 1739 |
rust_error_at (get_current_location (), "unclosed %<byte char%>"); |
| 1740 |
} |
| 1741 |
|
| 1742 |
skip_input (); |
| 1743 |
current_char = peek_input (); |
| 1744 |
length++; // go to next char |
| 1745 |
} |
| 1746 |
else |
| 1747 |
{ |
| 1748 |
rust_error_at (get_current_location (), |
| 1749 |
"no character inside %<%> for %<byte char%>"); |
| 1750 |
} |
| 1751 |
|
| 1752 |
current_column += length; |
| 1753 |
|
| 1754 |
loc += length - 1; |
| 1755 |
return Token::make_byte_char (loc, byte_char.value); |
| 1756 |
} |
| 1757 |
|
| 1758 |
// Parses a byte string. |
| 1759 |
TokenPtr |
| 1760 |
Lexer::parse_byte_string (location_t loc) |
| 1761 |
{ |
| 1762 |
// byte string |
| 1763 |
|
| 1764 |
// skip quote character |
| 1765 |
skip_input (); |
| 1766 |
current_column++; |
| 1767 |
|
| 1768 |
std::string str; |
| 1769 |
str.reserve (16); // some sensible default |
| 1770 |
|
| 1771 |
current_char = peek_input (); |
| 1772 |
|
| 1773 |
const location_t string_begin_locus = get_current_location (); |
| 1774 |
|
| 1775 |
while (current_char != '"' && !current_char.is_eof ()) |
| 1776 |
{ |
| 1777 |
if (current_char == '\\') |
| 1778 |
{ |
| 1779 |
int length = 1; |
| 1780 |
auto escape_length_pair = parse_escape ('"'); |
| 1781 |
char output_char = std::get<0> (escape_length_pair); |
| 1782 |
|
| 1783 |
if (output_char == 0 && std::get<2> (escape_length_pair)) |
| 1784 |
length = std::get<1> (escape_length_pair) - 1; |
| 1785 |
else |
| 1786 |
length += std::get<1> (escape_length_pair); |
| 1787 |
|
| 1788 |
if (output_char != 0 || !std::get<2> (escape_length_pair)) |
| 1789 |
str += output_char; |
| 1790 |
|
| 1791 |
current_column += length; |
| 1792 |
|
| 1793 |
continue; |
| 1794 |
} |
| 1795 |
|
| 1796 |
current_column++; |
| 1797 |
if (current_char.value == '\n') |
| 1798 |
{ |
| 1799 |
current_line++; |
| 1800 |
current_column = 1; |
| 1801 |
// tell line_table that new line starts |
| 1802 |
start_line (current_line, max_column_hint); |
| 1803 |
} |
| 1804 |
|
| 1805 |
str += current_char; |
| 1806 |
skip_input (); |
| 1807 |
current_char = peek_input (); |
| 1808 |
} |
| 1809 |
|
| 1810 |
if (current_char == '"') |
| 1811 |
{ |
| 1812 |
current_column++; |
| 1813 |
|
| 1814 |
skip_input (); |
| 1815 |
current_char = peek_input (); |
| 1816 |
} |
| 1817 |
else if (current_char.is_eof ()) |
| 1818 |
{ |
| 1819 |
rust_error_at (string_begin_locus, "unended byte string literal"); |
| 1820 |
return Token::make (END_OF_FILE, get_current_location ()); |
| 1821 |
} |
| 1822 |
else |
| 1823 |
{ |
| 1824 |
rust_unreachable (); |
| 1825 |
} |
| 1826 |
|
| 1827 |
str.shrink_to_fit (); |
| 1828 |
loc += str.size () - 1; |
| 1829 |
|
| 1830 |
return Token::make_byte_string (loc, std::move (str)); |
| 1831 |
} |
| 1832 |
|
| 1833 |
// Parses a raw byte string. |
| 1834 |
TokenPtr |
| 1835 |
Lexer::parse_raw_byte_string (location_t loc) |
| 1836 |
{ |
| 1837 |
// raw byte string literals |
| 1838 |
std::string str; |
| 1839 |
str.reserve (16); // some sensible default |
| 1840 |
|
| 1841 |
int length = 1; |
| 1842 |
int hash_count = 0; |
| 1843 |
|
| 1844 |
const location_t string_begin_locus = get_current_location (); |
| 1845 |
|
| 1846 |
// get hash count at beginnning |
| 1847 |
skip_input (); |
| 1848 |
current_char = peek_input (); |
| 1849 |
length++; |
| 1850 |
current_column++; |
| 1851 |
while (current_char == '#') |
| 1852 |
{ |
| 1853 |
hash_count++; |
| 1854 |
length++; |
| 1855 |
current_column++; |
| 1856 |
|
| 1857 |
skip_input (); |
| 1858 |
current_char = peek_input (); |
| 1859 |
} |
| 1860 |
|
| 1861 |
if (current_char != '"') |
| 1862 |
{ |
| 1863 |
rust_error_at (get_current_location (), |
| 1864 |
"raw byte string has no opening %<\"%>"); |
| 1865 |
} |
| 1866 |
|
| 1867 |
skip_input (); |
| 1868 |
current_char = peek_input (); |
| 1869 |
length++; |
| 1870 |
current_column++; |
| 1871 |
|
| 1872 |
while (true) |
| 1873 |
{ |
| 1874 |
if (current_char == '"') |
| 1875 |
{ |
| 1876 |
bool enough_hashes = true; |
| 1877 |
|
| 1878 |
for (int i = 0; i < hash_count; i++) |
| 1879 |
{ |
| 1880 |
if (peek_input (i + 1) != '#') |
| 1881 |
{ |
| 1882 |
enough_hashes = false; |
| 1883 |
break; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (enough_hashes) |
| 1888 |
{ |
| 1889 |
// skip enough input and peek enough input |
| 1890 |
skip_input (hash_count); |
| 1891 |
current_char = peek_input (); |
| 1892 |
length += hash_count + 1; |
| 1893 |
current_column += hash_count + 1; |
| 1894 |
break; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
else if (current_char.value > 127) |
| 1898 |
{ |
| 1899 |
rust_error_at (get_current_location (), |
| 1900 |
"character %qs in raw byte string out of range", |
| 1901 |
current_char.as_string ().c_str ()); |
| 1902 |
current_char = 0; |
| 1903 |
} |
| 1904 |
else if (current_char.is_eof ()) |
| 1905 |
{ |
| 1906 |
rust_error_at (string_begin_locus, "unended raw byte string literal"); |
| 1907 |
return Token::make (END_OF_FILE, get_current_location ()); |
| 1908 |
} |
| 1909 |
|
| 1910 |
length++; |
| 1911 |
current_column++; |
| 1912 |
if (current_char == '\n') |
| 1913 |
{ |
| 1914 |
current_line++; |
| 1915 |
current_column = 1; |
| 1916 |
start_line (current_line, max_column_hint); |
| 1917 |
} |
| 1918 |
|
| 1919 |
str += current_char; |
| 1920 |
skip_input (); |
| 1921 |
current_char = peek_input (); |
| 1922 |
} |
| 1923 |
|
| 1924 |
loc += length - 1; |
| 1925 |
|
| 1926 |
str.shrink_to_fit (); |
| 1927 |
|
| 1928 |
return Token::make_byte_string (loc, std::move (str)); |
| 1929 |
} |
| 1930 |
|
| 1931 |
// Parses a raw identifier. |
| 1932 |
TokenPtr |
| 1933 |
Lexer::parse_raw_identifier (location_t loc) |
| 1934 |
{ |
| 1935 |
// raw identifier |
| 1936 |
std::string str; |
| 1937 |
str.reserve (16); // default |
| 1938 |
|
| 1939 |
skip_input (); |
| 1940 |
current_char = peek_input (); |
| 1941 |
|
| 1942 |
current_column += 2; |
| 1943 |
|
| 1944 |
bool first_is_underscore = current_char == '_'; |
| 1945 |
|
| 1946 |
int length = 0; |
| 1947 |
current_char = peek_input (); |
| 1948 |
// loop through entire name |
| 1949 |
while (is_identifier_continue (current_char.value)) |
| 1950 |
{ |
| 1951 |
length++; |
| 1952 |
|
| 1953 |
str += current_char; |
| 1954 |
skip_input (); |
| 1955 |
current_char = peek_input (); |
| 1956 |
} |
| 1957 |
|
| 1958 |
current_column += length; |
| 1959 |
|
| 1960 |
rust_debug ("raw ident: %s", str.c_str ()); |
| 1961 |
|
| 1962 |
// if just a single underscore, not an identifier |
| 1963 |
if (first_is_underscore && length == 1) |
| 1964 |
rust_error_at (get_current_location (), |
| 1965 |
"%<_%> is not a valid raw identifier"); |
| 1966 |
|
| 1967 |
using namespace Rust::Values; |
| 1968 |
std::set<std::string> invalid{ |
| 1969 |
Keywords::CRATE, Keywords::EXTERN_KW, Keywords::SELF, |
| 1970 |
Keywords::SUPER, Keywords::SELF_ALIAS, |
| 1971 |
}; |
| 1972 |
|
| 1973 |
if (invalid.find (str) != invalid.end ()) |
| 1974 |
{ |
| 1975 |
rust_error_at (get_current_location (), |
| 1976 |
"%qs is a forbidden raw identifier", str.c_str ()); |
| 1977 |
|
| 1978 |
return nullptr; |
| 1979 |
} |
| 1980 |
else |
| 1981 |
{ |
| 1982 |
str.shrink_to_fit (); |
| 1983 |
loc += length - 1; |
| 1984 |
|
| 1985 |
return Token::make_identifier (loc, std::move (str)); |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
// skip broken string input (unterminated strings) |
| 1990 |
void |
| 1991 |
Lexer::skip_broken_string_input (Codepoint current_char) |
| 1992 |
{ |
| 1993 |
while (current_char != '"' && !current_char.is_eof ()) |
| 1994 |
{ |
| 1995 |
if (current_char == '\n') |
| 1996 |
{ |
| 1997 |
current_line++; |
| 1998 |
current_column = 1; |
| 1999 |
} |
| 2000 |
else |
| 2001 |
{ |
| 2002 |
current_column++; |
| 2003 |
} |
| 2004 |
skip_input (); |
| 2005 |
current_char = peek_input (); |
| 2006 |
} |
| 2007 |
if (current_char == '"') |
| 2008 |
{ |
| 2009 |
current_column++; |
| 2010 |
|
| 2011 |
skip_input (); |
| 2012 |
current_char = peek_input (); |
| 2013 |
} |
| 2014 |
rust_debug ("skipped to %d:%d due to bad quotes", current_line, |
| 2015 |
current_column); |
| 2016 |
} |
| 2017 |
|
| 2018 |
// Parses a string. |
| 2019 |
TokenPtr |
| 2020 |
Lexer::parse_string (location_t loc) |
| 2021 |
{ |
| 2022 |
std::string str; |
| 2023 |
str.reserve (16); // some sensible default |
| 2024 |
|
| 2025 |
current_char = peek_input (); |
| 2026 |
|
| 2027 |
const location_t string_begin_locus = get_current_location (); |
| 2028 |
|
| 2029 |
// FIXME: This fails if the input ends. How do we check for EOF? |
| 2030 |
while (current_char.value != '"' && !current_char.is_eof ()) |
| 2031 |
{ |
| 2032 |
if (current_char.value == '\\') |
| 2033 |
{ |
| 2034 |
int length = 1; |
| 2035 |
|
| 2036 |
// parse escape |
| 2037 |
auto utf8_escape_pair = parse_utf8_escape (); |
| 2038 |
current_char = std::get<0> (utf8_escape_pair); |
| 2039 |
|
| 2040 |
if (current_char == Codepoint (0) && std::get<2> (utf8_escape_pair)) |
| 2041 |
length = std::get<1> (utf8_escape_pair) - 1; |
| 2042 |
else |
| 2043 |
length += std::get<1> (utf8_escape_pair); |
| 2044 |
|
| 2045 |
if (current_char != Codepoint (0) || !std::get<2> (utf8_escape_pair)) |
| 2046 |
str += current_char.as_string (); |
| 2047 |
|
| 2048 |
current_column += length; |
| 2049 |
|
| 2050 |
// FIXME: should remove this but can't. |
| 2051 |
// `parse_utf8_escape` does not update `current_char` correctly. |
| 2052 |
current_char = peek_input (); |
| 2053 |
continue; |
| 2054 |
} |
| 2055 |
|
| 2056 |
current_column++; |
| 2057 |
if (current_char.value == '\n') |
| 2058 |
{ |
| 2059 |
current_line++; |
| 2060 |
current_column = 1; |
| 2061 |
// tell line_table that new line starts |
| 2062 |
start_line (current_line, max_column_hint); |
| 2063 |
} |
| 2064 |
|
| 2065 |
str += current_char; |
| 2066 |
skip_input (); |
| 2067 |
current_char = peek_input (); |
| 2068 |
} |
| 2069 |
|
| 2070 |
if (current_char.value == '"') |
| 2071 |
{ |
| 2072 |
current_column++; |
| 2073 |
|
| 2074 |
skip_input (); |
| 2075 |
current_char = peek_input (); |
| 2076 |
} |
| 2077 |
else if (current_char.is_eof ()) |
| 2078 |
{ |
| 2079 |
rust_error_at (string_begin_locus, "unended string literal"); |
| 2080 |
return Token::make (END_OF_FILE, get_current_location ()); |
| 2081 |
} |
| 2082 |
else |
| 2083 |
{ |
| 2084 |
rust_unreachable (); |
| 2085 |
} |
| 2086 |
|
| 2087 |
str.shrink_to_fit (); |
| 2088 |
|
| 2089 |
return Token::make_string (loc, std::move (str)); |
| 2090 |
} |
| 2091 |
|
| 2092 |
// Parses an identifier or keyword. |
| 2093 |
TokenPtr |
| 2094 |
Lexer::parse_identifier_or_keyword (location_t loc) |
| 2095 |
{ |
| 2096 |
std::string str; |
| 2097 |
str.reserve (16); // default |
| 2098 |
str += current_char.as_string (); |
| 2099 |
|
| 2100 |
bool first_is_underscore = current_char == '_'; |
| 2101 |
|
| 2102 |
int length = 1; |
| 2103 |
current_char = peek_input (); |
| 2104 |
|
| 2105 |
// loop through entire name |
| 2106 |
while (is_identifier_continue (current_char.value)) |
| 2107 |
{ |
| 2108 |
auto s = current_char.as_string (); |
| 2109 |
length++; |
| 2110 |
|
| 2111 |
str += current_char.as_string (); |
| 2112 |
skip_input (); |
| 2113 |
current_char = peek_input (); |
| 2114 |
} |
| 2115 |
|
| 2116 |
current_column += length; |
| 2117 |
|
| 2118 |
// if just a single underscore, not an identifier |
| 2119 |
if (first_is_underscore && length == 1) |
| 2120 |
return Token::make (UNDERSCORE, loc); |
| 2121 |
|
| 2122 |
str.shrink_to_fit (); |
| 2123 |
|
| 2124 |
loc += length - 1; |
| 2125 |
|
| 2126 |
TokenId keyword = classify_keyword (str); |
| 2127 |
if (keyword == IDENTIFIER) |
| 2128 |
return Token::make_identifier (loc, std::move (str)); |
| 2129 |
else |
| 2130 |
return Token::make (keyword, loc); |
| 2131 |
} |
| 2132 |
|
| 2133 |
// Possibly returns a raw string token if it exists - otherwise returns null. |
| 2134 |
TokenPtr |
| 2135 |
Lexer::maybe_parse_raw_string (location_t loc) |
| 2136 |
{ |
| 2137 |
int peek_index = 0; |
| 2138 |
while (peek_input (peek_index) == '#') |
| 2139 |
peek_index++; |
| 2140 |
|
| 2141 |
if (peek_input (peek_index) == '"') |
| 2142 |
return parse_raw_string (loc, peek_index); |
| 2143 |
else |
| 2144 |
return nullptr; |
| 2145 |
} |
| 2146 |
|
| 2147 |
// Returns a raw string token. |
| 2148 |
TokenPtr |
| 2149 |
Lexer::parse_raw_string (location_t loc, int initial_hash_count) |
| 2150 |
{ |
| 2151 |
// raw string literals |
| 2152 |
std::string str; |
| 2153 |
str.reserve (16); // some sensible default |
| 2154 |
|
| 2155 |
int length = 1 + initial_hash_count; |
| 2156 |
current_column += length; |
| 2157 |
|
| 2158 |
const location_t string_begin_locus = get_current_location (); |
| 2159 |
|
| 2160 |
if (initial_hash_count > 0) |
| 2161 |
skip_input (initial_hash_count - 1); |
| 2162 |
|
| 2163 |
current_char = peek_input (); |
| 2164 |
|
| 2165 |
if (current_char != '"') |
| 2166 |
rust_error_at (get_current_location (), "raw string has no opening %<\"%>"); |
| 2167 |
|
| 2168 |
length++; |
| 2169 |
current_column++; |
| 2170 |
skip_input (); |
| 2171 |
current_char = peek_input (); |
| 2172 |
|
| 2173 |
while (true) |
| 2174 |
{ |
| 2175 |
if (current_char.value == '"') |
| 2176 |
{ |
| 2177 |
bool enough_hashes = true; |
| 2178 |
|
| 2179 |
for (int i = 0; i < initial_hash_count; i++) |
| 2180 |
{ |
| 2181 |
if (peek_input (i + 1) != '#') |
| 2182 |
{ |
| 2183 |
enough_hashes = false; |
| 2184 |
break; |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
if (enough_hashes) |
| 2189 |
{ |
| 2190 |
// skip enough input and peek enough input |
| 2191 |
skip_input (initial_hash_count); |
| 2192 |
current_char = peek_input (); |
| 2193 |
length += initial_hash_count + 1; |
| 2194 |
current_column += initial_hash_count + 1; |
| 2195 |
break; |
| 2196 |
} |
| 2197 |
} |
| 2198 |
else if (current_char.is_eof ()) |
| 2199 |
{ |
| 2200 |
rust_error_at (string_begin_locus, "unended raw string literal"); |
| 2201 |
return Token::make (END_OF_FILE, get_current_location ()); |
| 2202 |
} |
| 2203 |
|
| 2204 |
length++; |
| 2205 |
current_column++; |
| 2206 |
if (current_char == '\n') |
| 2207 |
{ |
| 2208 |
current_line++; |
| 2209 |
current_column = 1; |
| 2210 |
start_line (current_line, max_column_hint); |
| 2211 |
} |
| 2212 |
|
| 2213 |
str += current_char.as_string (); |
| 2214 |
skip_input (); |
| 2215 |
current_char = peek_input (); |
| 2216 |
} |
| 2217 |
|
| 2218 |
loc += length - 1; |
| 2219 |
|
| 2220 |
str.shrink_to_fit (); |
| 2221 |
|
| 2222 |
return Token::make_raw_string (loc, std::move (str)); |
| 2223 |
} |
| 2224 |
|
| 2225 |
template <typename IsDigitFunc> |
| 2226 |
TokenPtr |
| 2227 |
Lexer::parse_non_decimal_int_literal (location_t loc, IsDigitFunc is_digit_func, |
| 2228 |
std::string existent_str, int base) |
| 2229 |
{ |
| 2230 |
int length = 1; |
| 2231 |
|
| 2232 |
skip_input (); |
| 2233 |
current_char = peek_input (); |
| 2234 |
|
| 2235 |
length++; |
| 2236 |
|
| 2237 |
// loop through to add entire number to string |
| 2238 |
while (is_digit_func (current_char.value) || current_char == '_') |
| 2239 |
{ |
| 2240 |
if (current_char == '_') |
| 2241 |
{ |
| 2242 |
// don't add _ to number |
| 2243 |
skip_input (); |
| 2244 |
current_char = peek_input (); |
| 2245 |
|
| 2246 |
length++; |
| 2247 |
|
| 2248 |
continue; |
| 2249 |
} |
| 2250 |
|
| 2251 |
length++; |
| 2252 |
|
| 2253 |
// add raw numbers |
| 2254 |
existent_str += current_char; |
| 2255 |
skip_input (); |
| 2256 |
current_char = peek_input (); |
| 2257 |
} |
| 2258 |
|
| 2259 |
// convert value to decimal representation |
| 2260 |
long dec_num = std::strtol (existent_str.c_str (), nullptr, base); |
| 2261 |
|
| 2262 |
existent_str = std::to_string (dec_num); |
| 2263 |
|
| 2264 |
// parse in type suffix if it exists |
| 2265 |
auto type_suffix_pair = parse_in_type_suffix (); |
| 2266 |
PrimitiveCoreType type_hint = type_suffix_pair.first; |
| 2267 |
length += type_suffix_pair.second; |
| 2268 |
|
| 2269 |
current_column += length; |
| 2270 |
|
| 2271 |
if (type_hint == CORETYPE_F32 || type_hint == CORETYPE_F64) |
| 2272 |
{ |
| 2273 |
rust_error_at (get_current_location (), |
| 2274 |
"invalid type suffix %qs for integer (%s) literal", |
| 2275 |
get_type_hint_string (type_hint), |
| 2276 |
base == 16 |
| 2277 |
? "hex" |
| 2278 |
: (base == 8 ? "octal" |
| 2279 |
: (base == 2 ? "binary" |
| 2280 |
: "<insert unknown base>"))); |
| 2281 |
return nullptr; |
| 2282 |
} |
| 2283 |
|
| 2284 |
loc += length - 1; |
| 2285 |
|
| 2286 |
return Token::make_int (loc, std::move (existent_str), type_hint); |
| 2287 |
} |
| 2288 |
|
| 2289 |
// Parses a hex, binary or octal int literal. |
| 2290 |
TokenPtr |
| 2291 |
Lexer::parse_non_decimal_int_literals (location_t loc) |
| 2292 |
{ |
| 2293 |
std::string str; |
| 2294 |
str.reserve (16); // some sensible default |
| 2295 |
str += current_char; |
| 2296 |
|
| 2297 |
current_char = peek_input (); |
| 2298 |
|
| 2299 |
if (current_char == 'x') |
| 2300 |
{ |
| 2301 |
// hex (integer only) |
| 2302 |
return parse_non_decimal_int_literal (loc, is_x_digit, str + "x", 16); |
| 2303 |
} |
| 2304 |
else if (current_char == 'o') |
| 2305 |
{ |
| 2306 |
// octal (integer only) |
| 2307 |
return parse_non_decimal_int_literal (loc, is_octal_digit, |
| 2308 |
std::move (str), 8); |
| 2309 |
} |
| 2310 |
else if (current_char == 'b') |
| 2311 |
{ |
| 2312 |
// binary (integer only) |
| 2313 |
return parse_non_decimal_int_literal (loc, is_bin_digit, std::move (str), |
| 2314 |
2); |
| 2315 |
} |
| 2316 |
else |
| 2317 |
{ |
| 2318 |
return nullptr; |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
// Parses a decimal-based int literal or float literal. |
| 2323 |
TokenPtr |
| 2324 |
Lexer::parse_decimal_int_or_float (location_t loc) |
| 2325 |
{ |
| 2326 |
std::string str; |
| 2327 |
str.reserve (16); // some sensible default |
| 2328 |
str += current_char; |
| 2329 |
|
| 2330 |
int length = 1; |
| 2331 |
bool first_zero = current_char == '0'; |
| 2332 |
|
| 2333 |
current_char = peek_input (); |
| 2334 |
|
| 2335 |
// parse initial decimal integer (or first integer part of float) literal |
| 2336 |
auto initial_decimal = parse_in_decimal (); |
| 2337 |
str += std::get<0> (initial_decimal); |
| 2338 |
length += std::get<1> (initial_decimal); |
| 2339 |
|
| 2340 |
// detect float literal |
| 2341 |
// |
| 2342 |
// Note: |
| 2343 |
// |
| 2344 |
// We should not use is_float_digit () for this verification but instead |
| 2345 |
// directly ISDIGIT because rust does not support non digit values right after |
| 2346 |
// a dot. |
| 2347 |
// The following value is not legal in rust: |
| 2348 |
// let a = 3.e1; |
| 2349 |
// A `0` should be put between the dot and the exponent to be valid |
| 2350 |
// (eg. 3.0e1). |
| 2351 |
if (current_char == '.' && ISDIGIT (peek_input (1).value)) |
| 2352 |
{ |
| 2353 |
// float with a '.', parse another decimal into it |
| 2354 |
|
| 2355 |
// add . to str |
| 2356 |
str += current_char; |
| 2357 |
skip_input (); |
| 2358 |
current_char = peek_input (); |
| 2359 |
length++; |
| 2360 |
|
| 2361 |
// parse another decimal number for float |
| 2362 |
auto second_decimal = parse_in_decimal (); |
| 2363 |
str += std::get<0> (second_decimal); |
| 2364 |
length += std::get<1> (second_decimal); |
| 2365 |
|
| 2366 |
// parse in exponent part if it exists |
| 2367 |
auto exponent_pair = parse_in_exponent_part (); |
| 2368 |
str += exponent_pair.first; |
| 2369 |
length += exponent_pair.second; |
| 2370 |
|
| 2371 |
// parse in type suffix if it exists |
| 2372 |
auto type_suffix_pair = parse_in_type_suffix (); |
| 2373 |
PrimitiveCoreType type_hint = type_suffix_pair.first; |
| 2374 |
length += type_suffix_pair.second; |
| 2375 |
|
| 2376 |
if (type_hint != CORETYPE_F32 && type_hint != CORETYPE_F64 |
| 2377 |
&& type_hint != CORETYPE_UNKNOWN) |
| 2378 |
{ |
| 2379 |
rust_error_at (get_current_location (), |
| 2380 |
"invalid type suffix %qs for floating-point literal", |
| 2381 |
get_type_hint_string (type_hint)); |
| 2382 |
// ignore invalid type suffix as everything else seems fine |
| 2383 |
type_hint = CORETYPE_UNKNOWN; |
| 2384 |
} |
| 2385 |
|
| 2386 |
current_column += length; |
| 2387 |
|
| 2388 |
loc += length - 1; |
| 2389 |
|
| 2390 |
str.shrink_to_fit (); |
| 2391 |
return Token::make_float (loc, std::move (str), type_hint); |
| 2392 |
} |
| 2393 |
else if (current_char == '.' |
| 2394 |
&& check_valid_float_dot_end (peek_input (1).value)) |
| 2395 |
{ |
| 2396 |
// float that is just an integer with a terminating '.' character |
| 2397 |
|
| 2398 |
// add . to str |
| 2399 |
str += current_char; |
| 2400 |
skip_input (); |
| 2401 |
current_char = peek_input (); |
| 2402 |
length++; |
| 2403 |
|
| 2404 |
// type hint not allowed |
| 2405 |
|
| 2406 |
current_column += length; |
| 2407 |
|
| 2408 |
loc += length - 1; |
| 2409 |
|
| 2410 |
str.shrink_to_fit (); |
| 2411 |
return Token::make_float (loc, std::move (str), CORETYPE_UNKNOWN); |
| 2412 |
} |
| 2413 |
else if (current_char == 'E' || current_char == 'e') |
| 2414 |
{ |
| 2415 |
// exponent float with no '.' character |
| 2416 |
|
| 2417 |
// parse exponent part |
| 2418 |
auto exponent_pair = parse_in_exponent_part (); |
| 2419 |
str += exponent_pair.first; |
| 2420 |
length += exponent_pair.second; |
| 2421 |
|
| 2422 |
// parse in type suffix if it exists |
| 2423 |
auto type_suffix_pair = parse_in_type_suffix (); |
| 2424 |
PrimitiveCoreType type_hint = type_suffix_pair.first; |
| 2425 |
length += type_suffix_pair.second; |
| 2426 |
|
| 2427 |
if (type_hint != CORETYPE_F32 && type_hint != CORETYPE_F64 |
| 2428 |
&& type_hint != CORETYPE_UNKNOWN) |
| 2429 |
{ |
| 2430 |
rust_error_at (get_current_location (), |
| 2431 |
"invalid type suffix %qs for floating-point literal", |
| 2432 |
get_type_hint_string (type_hint)); |
| 2433 |
// ignore invalid type suffix as everything else seems fine |
| 2434 |
type_hint = CORETYPE_UNKNOWN; |
| 2435 |
} |
| 2436 |
|
| 2437 |
current_column += length; |
| 2438 |
|
| 2439 |
loc += length - 1; |
| 2440 |
|
| 2441 |
str.shrink_to_fit (); |
| 2442 |
return Token::make_float (loc, std::move (str), type_hint); |
| 2443 |
} |
| 2444 |
else |
| 2445 |
{ |
| 2446 |
// is an integer |
| 2447 |
|
| 2448 |
// parse in type suffix if it exists |
| 2449 |
auto type_suffix_pair = parse_in_type_suffix (); |
| 2450 |
PrimitiveCoreType type_hint = type_suffix_pair.first; |
| 2451 |
/* A "real" pure decimal doesn't have a suffix and no zero prefix. */ |
| 2452 |
if (type_hint == CORETYPE_UNKNOWN) |
| 2453 |
{ |
| 2454 |
bool pure_decimal = std::get<2> (initial_decimal); |
| 2455 |
if (pure_decimal && (!first_zero || str.size () == 1)) |
| 2456 |
type_hint = CORETYPE_PURE_DECIMAL; |
| 2457 |
} |
| 2458 |
length += type_suffix_pair.second; |
| 2459 |
|
| 2460 |
current_column += length; |
| 2461 |
|
| 2462 |
loc += length - 1; |
| 2463 |
|
| 2464 |
str.shrink_to_fit (); |
| 2465 |
return Token::make_int (loc, std::move (str), type_hint); |
| 2466 |
} |
| 2467 |
} |
| 2468 |
|
| 2469 |
TokenPtr |
| 2470 |
Lexer::parse_char_or_lifetime (location_t loc) |
| 2471 |
{ |
| 2472 |
int length = 1; |
| 2473 |
|
| 2474 |
current_char = peek_input (); |
| 2475 |
if (current_char.is_eof ()) |
| 2476 |
return nullptr; |
| 2477 |
|
| 2478 |
// parse escaped char literal |
| 2479 |
if (current_char.value == '\\') |
| 2480 |
{ |
| 2481 |
// parse escape |
| 2482 |
auto utf8_escape_pair = parse_utf8_escape (); |
| 2483 |
Codepoint escaped_char = std::get<0> (utf8_escape_pair); |
| 2484 |
length += std::get<1> (utf8_escape_pair); |
| 2485 |
|
| 2486 |
if (peek_input ().value != '\'') |
| 2487 |
{ |
| 2488 |
rust_error_at (get_current_location (), "unended character literal"); |
| 2489 |
} |
| 2490 |
else |
| 2491 |
{ |
| 2492 |
skip_input (); |
| 2493 |
current_char = peek_input (); |
| 2494 |
length++; |
| 2495 |
} |
| 2496 |
|
| 2497 |
current_column += length; |
| 2498 |
|
| 2499 |
loc += length - 1; |
| 2500 |
|
| 2501 |
return Token::make_char (loc, escaped_char); |
| 2502 |
} |
| 2503 |
else |
| 2504 |
{ |
| 2505 |
skip_input (); |
| 2506 |
|
| 2507 |
if (peek_input ().value == '\'') |
| 2508 |
{ |
| 2509 |
// parse non-escaped char literal |
| 2510 |
Codepoint non_escaped_char = current_char; |
| 2511 |
|
| 2512 |
// skip the ' character |
| 2513 |
skip_input (); |
| 2514 |
current_char = peek_input (); |
| 2515 |
|
| 2516 |
// TODO fix due to different widths of utf-8 chars? |
| 2517 |
current_column += 3; |
| 2518 |
|
| 2519 |
loc += 2; |
| 2520 |
|
| 2521 |
return Token::make_char (loc, non_escaped_char); |
| 2522 |
} |
| 2523 |
else if (is_identifier_start (current_char.value)) |
| 2524 |
{ |
| 2525 |
// parse lifetime name |
| 2526 |
std::string str; |
| 2527 |
str += current_char.as_string (); |
| 2528 |
length++; |
| 2529 |
|
| 2530 |
current_char = peek_input (); |
| 2531 |
while (is_identifier_continue (current_char.value)) |
| 2532 |
{ |
| 2533 |
str += current_char.as_string (); |
| 2534 |
skip_input (); |
| 2535 |
current_char = peek_input (); |
| 2536 |
length++; |
| 2537 |
} |
| 2538 |
|
| 2539 |
current_column += length; |
| 2540 |
|
| 2541 |
loc += length - 1; |
| 2542 |
|
| 2543 |
// TODO some keywords cannot be used for a lifetime label #2306 |
| 2544 |
// https://doc.rust-lang.org/reference/tokens.html |
| 2545 |
|
| 2546 |
str.shrink_to_fit (); |
| 2547 |
return Token::make_lifetime (loc, std::move (str)); |
| 2548 |
} |
| 2549 |
else |
| 2550 |
{ |
| 2551 |
rust_error_at ( |
| 2552 |
get_current_location (), |
| 2553 |
"expected %' after character constant in character literal"); |
| 2554 |
return nullptr; |
| 2555 |
} |
| 2556 |
} |
| 2557 |
} |
| 2558 |
|
| 2559 |
void |
| 2560 |
Lexer::split_current_token (TokenId new_left, TokenId new_right) |
| 2561 |
{ |
| 2562 |
/* TODO: assert that this TokenId is a "simple token" like punctuation and not |
| 2563 |
* like "IDENTIFIER"? */ |
| 2564 |
location_t current_loc = peek_token ()->get_locus (); |
| 2565 |
TokenPtr new_left_tok = Token::make (new_left, current_loc); |
| 2566 |
TokenPtr new_right_tok = Token::make (new_right, current_loc + 1); |
| 2567 |
|
| 2568 |
token_queue.replace_current_value (std::move (new_left_tok)); |
| 2569 |
token_queue.insert (1, std::move (new_right_tok)); |
| 2570 |
} |
| 2571 |
|
| 2572 |
void |
| 2573 |
Lexer::split_current_token (std::vector<TokenPtr> new_tokens) |
| 2574 |
{ |
| 2575 |
rust_assert (new_tokens.size () > 0); |
| 2576 |
token_queue.replace_current_value (new_tokens[0]); |
| 2577 |
|
| 2578 |
for (size_t i = 1; i < new_tokens.size (); i++) |
| 2579 |
{ |
| 2580 |
token_queue.insert (i, new_tokens[i]); |
| 2581 |
} |
| 2582 |
} |
| 2583 |
|
| 2584 |
void |
| 2585 |
Lexer::start_line (int current_line, int current_column) |
| 2586 |
{ |
| 2587 |
if (line_map) |
| 2588 |
linemap_line_start (line_table, current_line, current_column); |
| 2589 |
} |
| 2590 |
|
| 2591 |
} // namespace Rust |
| 2592 |
|
| 2593 |
#if CHECKING_P |
| 2594 |
|
| 2595 |
namespace selftest { |
| 2596 |
|
| 2597 |
// Checks if `src` has the same contents as the given characters |
| 2598 |
static void |
| 2599 |
assert_source_content (Rust::InputSource &src, |
| 2600 |
const std::vector<uint32_t> &expected) |
| 2601 |
{ |
| 2602 |
Rust::Codepoint src_char = src.next (); |
| 2603 |
for (auto expected_char : expected) |
| 2604 |
{ |
| 2605 |
// Make sure that `src` is not shorter than `expected` |
| 2606 |
ASSERT_FALSE (src_char.is_eof ()); |
| 2607 |
// Checks skipped character is expeceted one. |
| 2608 |
ASSERT_EQ (src_char.value, expected_char); |
| 2609 |
src_char = src.next (); |
| 2610 |
} |
| 2611 |
// Checks if `src` and `chars` has the same length. |
| 2612 |
ASSERT_TRUE (src_char.is_eof ()); |
| 2613 |
} |
| 2614 |
|
| 2615 |
static void |
| 2616 |
test_buffer_input_source (std::string str, |
| 2617 |
const std::vector<uint32_t> &expected) |
| 2618 |
{ |
| 2619 |
Rust::BufferInputSource source (str, 0); |
| 2620 |
assert_source_content (source, expected); |
| 2621 |
} |
| 2622 |
|
| 2623 |
static void |
| 2624 |
test_file_input_source (std::string str, const std::vector<uint32_t> &expected) |
| 2625 |
{ |
| 2626 |
FILE *tmpf = tmpfile (); |
| 2627 |
// Moves to the first character |
| 2628 |
fputs (str.c_str (), tmpf); |
| 2629 |
std::rewind (tmpf); |
| 2630 |
Rust::FileInputSource source (tmpf); |
| 2631 |
assert_source_content (source, expected); |
| 2632 |
} |
| 2633 |
|
| 2634 |
void |
| 2635 |
rust_input_source_test () |
| 2636 |
{ |
| 2637 |
// ASCII |
| 2638 |
std::string src = u8"_abcde\tXYZ\v\f"; |
| 2639 |
std::vector<uint32_t> expected |
| 2640 |
= {'_', 'a', 'b', 'c', 'd', 'e', '\t', 'X', 'Y', 'Z', '\v', '\f'}; |
| 2641 |
test_buffer_input_source (src, expected); |
| 2642 |
|
| 2643 |
// BOM |
| 2644 |
src = u8"\xef\xbb\xbfOK"; |
| 2645 |
expected = {'O', 'K'}; |
| 2646 |
test_buffer_input_source (src, expected); |
| 2647 |
|
| 2648 |
// Russian |
| 2649 |
src = u8"приве́т"; |
| 2650 |
expected = {L'п', |
| 2651 |
L'р', |
| 2652 |
L'и', |
| 2653 |
L'в', |
| 2654 |
0x0435 /* CYRILLIC SMALL LETTER IE е */, |
| 2655 |
0x301 /* COMBINING ACUTE ACCENT ́ */, |
| 2656 |
L'т'}; |
| 2657 |
test_buffer_input_source (src, expected); |
| 2658 |
|
| 2659 |
src = u8"❤️🦀"; |
| 2660 |
expected = {0x2764 /* HEAVY BLACK HEART */, |
| 2661 |
0xfe0f /* VARIATION SELECTOR-16 */, L'🦀'}; |
| 2662 |
test_buffer_input_source (src, expected); |
| 2663 |
|
| 2664 |
src = u8"こんにちは"; |
| 2665 |
expected = {L'こ', L'ん', L'に', L'ち', L'は'}; |
| 2666 |
test_file_input_source (src, expected); |
| 2667 |
|
| 2668 |
src = u8"👮♂👩⚕"; |
| 2669 |
expected |
| 2670 |
= {0x1f46e /* POLICE OFFICER */, 0x200d /* ZERO WIDTH JOINER */, |
| 2671 |
0x2642 /* MALE SIGN */, 0x1f469 /* WOMAN */, |
| 2672 |
0x200d /* ZERO WIDTH JOINER */, 0x2695 /* STAFF OF AESCULAPIUS */}; |
| 2673 |
test_file_input_source (src, expected); |
| 2674 |
} |
| 2675 |
|
| 2676 |
} // namespace selftest |
| 2677 |
|
| 2678 |
#endif // CHECKING_P |