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