rust-parse.c revision 1.1.1.3 1 /* Rust expression parsing for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "block.h"
22 #include "charset.h"
23 #include "cp-support.h"
24 #include "gdbsupport/gdb_obstack.h"
25 #include "gdbsupport/gdb_regex.h"
26 #include "rust-lang.h"
27 #include "parser-defs.h"
28 #include "gdbsupport/selftest.h"
29 #include "value.h"
30 #include "gdbarch.h"
31 #include "rust-exp.h"
32 #include "inferior.h"
33
34 using namespace expr;
35
36 /* A regular expression for matching Rust numbers. This is split up
37 since it is very long and this gives us a way to comment the
38 sections. */
39
40 static const char number_regex_text[] =
41 /* subexpression 1: allows use of alternation, otherwise uninteresting */
42 "^("
43 /* First comes floating point. */
44 /* Recognize number after the decimal point, with optional
45 exponent and optional type suffix.
46 subexpression 2: allows "?", otherwise uninteresting
47 subexpression 3: if present, type suffix
48 */
49 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
50 #define FLOAT_TYPE1 3
51 "|"
52 /* Recognize exponent without decimal point, with optional type
53 suffix.
54 subexpression 4: if present, type suffix
55 */
56 #define FLOAT_TYPE2 4
57 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
58 "|"
59 /* "23." is a valid floating point number, but "23.e5" and
60 "23.f32" are not. So, handle the trailing-. case
61 separately. */
62 "[0-9][0-9_]*\\."
63 "|"
64 /* Finally come integers.
65 subexpression 5: text of integer
66 subexpression 6: if present, type suffix
67 subexpression 7: allows use of alternation, otherwise uninteresting
68 */
69 #define INT_TEXT 5
70 #define INT_TYPE 6
71 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
72 "([iu](size|8|16|32|64|128))?"
73 ")";
74 /* The number of subexpressions to allocate space for, including the
75 "0th" whole match subexpression. */
76 #define NUM_SUBEXPRESSIONS 8
77
78 /* The compiled number-matching regex. */
79
80 static regex_t number_regex;
81
82 /* The kinds of tokens. Note that single-character tokens are
83 represented by themselves, so for instance '[' is a token. */
84 enum token_type : int
85 {
86 /* Make sure to start after any ASCII character. */
87 GDBVAR = 256,
88 IDENT,
89 COMPLETE,
90 INTEGER,
91 DECIMAL_INTEGER,
92 STRING,
93 BYTESTRING,
94 FLOAT,
95 COMPOUND_ASSIGN,
96
97 /* Keyword tokens. */
98 KW_AS,
99 KW_IF,
100 KW_TRUE,
101 KW_FALSE,
102 KW_SUPER,
103 KW_SELF,
104 KW_MUT,
105 KW_EXTERN,
106 KW_CONST,
107 KW_FN,
108 KW_SIZEOF,
109
110 /* Operator tokens. */
111 DOTDOT,
112 DOTDOTEQ,
113 OROR,
114 ANDAND,
115 EQEQ,
116 NOTEQ,
117 LTEQ,
118 GTEQ,
119 LSH,
120 RSH,
121 COLONCOLON,
122 ARROW,
123 };
124
125 /* A typed integer constant. */
126
127 struct typed_val_int
128 {
129 gdb_mpz val;
130 struct type *type;
131 };
132
133 /* A typed floating point constant. */
134
135 struct typed_val_float
136 {
137 float_data val;
138 struct type *type;
139 };
140
141 /* A struct of this type is used to describe a token. */
142
143 struct token_info
144 {
145 const char *name;
146 int value;
147 enum exp_opcode opcode;
148 };
149
150 /* Identifier tokens. */
151
152 static const struct token_info identifier_tokens[] =
153 {
154 { "as", KW_AS, OP_NULL },
155 { "false", KW_FALSE, OP_NULL },
156 { "if", 0, OP_NULL },
157 { "mut", KW_MUT, OP_NULL },
158 { "const", KW_CONST, OP_NULL },
159 { "self", KW_SELF, OP_NULL },
160 { "super", KW_SUPER, OP_NULL },
161 { "true", KW_TRUE, OP_NULL },
162 { "extern", KW_EXTERN, OP_NULL },
163 { "fn", KW_FN, OP_NULL },
164 { "sizeof", KW_SIZEOF, OP_NULL },
165 };
166
167 /* Operator tokens, sorted longest first. */
168
169 static const struct token_info operator_tokens[] =
170 {
171 { ">>=", COMPOUND_ASSIGN, BINOP_RSH },
172 { "<<=", COMPOUND_ASSIGN, BINOP_LSH },
173
174 { "<<", LSH, OP_NULL },
175 { ">>", RSH, OP_NULL },
176 { "&&", ANDAND, OP_NULL },
177 { "||", OROR, OP_NULL },
178 { "==", EQEQ, OP_NULL },
179 { "!=", NOTEQ, OP_NULL },
180 { "<=", LTEQ, OP_NULL },
181 { ">=", GTEQ, OP_NULL },
182 { "+=", COMPOUND_ASSIGN, BINOP_ADD },
183 { "-=", COMPOUND_ASSIGN, BINOP_SUB },
184 { "*=", COMPOUND_ASSIGN, BINOP_MUL },
185 { "/=", COMPOUND_ASSIGN, BINOP_DIV },
186 { "%=", COMPOUND_ASSIGN, BINOP_REM },
187 { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
188 { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
189 { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
190 { "..=", DOTDOTEQ, OP_NULL },
191
192 { "::", COLONCOLON, OP_NULL },
193 { "..", DOTDOT, OP_NULL },
194 { "->", ARROW, OP_NULL }
195 };
196
197 /* An instance of this is created before parsing, and destroyed when
198 parsing is finished. */
199
200 struct rust_parser
201 {
202 explicit rust_parser (struct parser_state *state)
203 : pstate (state)
204 {
205 }
206
207 DISABLE_COPY_AND_ASSIGN (rust_parser);
208
209 /* Return the parser's language. */
210 const struct language_defn *language () const
211 {
212 return pstate->language ();
213 }
214
215 /* Return the parser's gdbarch. */
216 struct gdbarch *arch () const
217 {
218 return pstate->gdbarch ();
219 }
220
221 /* A helper to look up a Rust type, or fail. This only works for
222 types defined by rust_language_arch_info. */
223
224 struct type *get_type (const char *name)
225 {
226 struct type *type;
227
228 type = language_lookup_primitive_type (language (), arch (), name);
229 if (type == NULL)
230 error (_("Could not find Rust type %s"), name);
231 return type;
232 }
233
234 std::string crate_name (const std::string &name);
235 std::string super_name (const std::string &ident, unsigned int n_supers);
236
237 int lex_character ();
238 int lex_number ();
239 int lex_string ();
240 int lex_identifier ();
241 uint32_t lex_hex (int min, int max);
242 uint32_t lex_escape (bool is_byte);
243 int lex_operator ();
244 int lex_one_token ();
245 void push_back (char c);
246
247 /* The main interface to lexing. Lexes one token and updates the
248 internal state. */
249 void lex ()
250 {
251 current_token = lex_one_token ();
252 }
253
254 /* Assuming the current token is TYPE, lex the next token. */
255 void assume (int type)
256 {
257 gdb_assert (current_token == type);
258 lex ();
259 }
260
261 /* Require the single-character token C, and lex the next token; or
262 throw an exception. */
263 void require (char type)
264 {
265 if (current_token != type)
266 error (_("'%c' expected"), type);
267 lex ();
268 }
269
270 /* Entry point for all parsing. */
271 operation_up parse_entry_point ()
272 {
273 lex ();
274 operation_up result = parse_expr ();
275 if (current_token != 0)
276 error (_("Syntax error near '%s'"), pstate->prev_lexptr);
277 return result;
278 }
279
280 operation_up parse_tuple ();
281 operation_up parse_array ();
282 operation_up name_to_operation (const std::string &name);
283 operation_up parse_struct_expr (struct type *type);
284 operation_up parse_binop (bool required);
285 operation_up parse_range ();
286 operation_up parse_expr ();
287 operation_up parse_sizeof ();
288 operation_up parse_addr ();
289 operation_up parse_field (operation_up &&);
290 operation_up parse_index (operation_up &&);
291 std::vector<operation_up> parse_paren_args ();
292 operation_up parse_call (operation_up &&);
293 std::vector<struct type *> parse_type_list ();
294 std::vector<struct type *> parse_maybe_type_list ();
295 struct type *parse_array_type ();
296 struct type *parse_slice_type ();
297 struct type *parse_pointer_type ();
298 struct type *parse_function_type ();
299 struct type *parse_tuple_type ();
300 struct type *parse_type ();
301 std::string parse_path (bool for_expr);
302 operation_up parse_string ();
303 operation_up parse_tuple_struct (struct type *type);
304 operation_up parse_path_expr ();
305 operation_up parse_atom (bool required);
306
307 void update_innermost_block (struct block_symbol sym);
308 struct block_symbol lookup_symbol (const char *name,
309 const struct block *block,
310 const domain_search_flags domain);
311 struct type *rust_lookup_type (const char *name);
312
313 /* Clear some state. This is only used for testing. */
314 #if GDB_SELF_TEST
315 void reset (const char *input)
316 {
317 pstate->prev_lexptr = nullptr;
318 pstate->lexptr = input;
319 paren_depth = 0;
320 current_token = 0;
321 current_int_val = {};
322 current_float_val = {};
323 current_string_val = {};
324 current_opcode = OP_NULL;
325 }
326 #endif /* GDB_SELF_TEST */
327
328 /* Return the token's string value as a string. */
329 std::string get_string () const
330 {
331 return std::string (current_string_val.ptr, current_string_val.length);
332 }
333
334 /* A pointer to this is installed globally. */
335 auto_obstack obstack;
336
337 /* The parser state gdb gave us. */
338 struct parser_state *pstate;
339
340 /* Depth of parentheses. */
341 int paren_depth = 0;
342
343 /* The current token's type. */
344 int current_token = 0;
345 /* The current token's payload, if any. */
346 typed_val_int current_int_val {};
347 typed_val_float current_float_val {};
348 struct stoken current_string_val {};
349 enum exp_opcode current_opcode = OP_NULL;
350
351 /* When completing, this may be set to the field operation to
352 complete. */
353 operation_up completion_op;
354 };
355
356 /* Return an string referring to NAME, but relative to the crate's
357 name. */
358
359 std::string
360 rust_parser::crate_name (const std::string &name)
361 {
362 std::string crate = rust_crate_for_block (pstate->expression_context_block);
363
364 if (crate.empty ())
365 error (_("Could not find crate for current location"));
366 return "::" + crate + "::" + name;
367 }
368
369 /* Return a string referring to a "super::" qualified name. IDENT is
370 the base name and N_SUPERS is how many "super::"s were provided.
371 N_SUPERS can be zero. */
372
373 std::string
374 rust_parser::super_name (const std::string &ident, unsigned int n_supers)
375 {
376 const char *scope = "";
377 if (pstate->expression_context_block != nullptr)
378 scope = pstate->expression_context_block->scope ();
379 int offset;
380
381 if (scope[0] == '\0')
382 error (_("Couldn't find namespace scope for self::"));
383
384 if (n_supers > 0)
385 {
386 int len;
387 std::vector<int> offsets;
388 unsigned int current_len;
389
390 current_len = cp_find_first_component (scope);
391 while (scope[current_len] != '\0')
392 {
393 offsets.push_back (current_len);
394 gdb_assert (scope[current_len] == ':');
395 /* The "::". */
396 current_len += 2;
397 current_len += cp_find_first_component (scope
398 + current_len);
399 }
400
401 len = offsets.size ();
402 if (n_supers >= len)
403 error (_("Too many super:: uses from '%s'"), scope);
404
405 offset = offsets[len - n_supers];
406 }
407 else
408 offset = strlen (scope);
409
410 return "::" + std::string (scope, offset) + "::" + ident;
411 }
412
413 /* A helper to appropriately munge NAME and BLOCK depending on the
414 presence of a leading "::". */
415
416 static void
417 munge_name_and_block (const char **name, const struct block **block)
418 {
419 /* If it is a global reference, skip the current block in favor of
420 the static block. */
421 if (startswith (*name, "::"))
422 {
423 *name += 2;
424 *block = (*block)->static_block ();
425 }
426 }
427
428 /* Like lookup_symbol, but handles Rust namespace conventions, and
429 doesn't require field_of_this_result. */
430
431 struct block_symbol
432 rust_parser::lookup_symbol (const char *name, const struct block *block,
433 const domain_search_flags domain)
434 {
435 struct block_symbol result;
436
437 munge_name_and_block (&name, &block);
438
439 result = ::lookup_symbol (name, block, domain, NULL);
440 if (result.symbol != NULL)
441 update_innermost_block (result);
442 return result;
443 }
444
445 /* Look up a type, following Rust namespace conventions. */
446
447 struct type *
448 rust_parser::rust_lookup_type (const char *name)
449 {
450 struct block_symbol result;
451 struct type *type;
452
453 const struct block *block = pstate->expression_context_block;
454 munge_name_and_block (&name, &block);
455
456 result = ::lookup_symbol (name, block, SEARCH_TYPE_DOMAIN, nullptr);
457 if (result.symbol != NULL)
458 {
459 update_innermost_block (result);
460 return result.symbol->type ();
461 }
462
463 type = lookup_typename (language (), name, NULL, 1);
464 if (type != NULL)
465 return type;
466
467 /* Last chance, try a built-in type. */
468 return language_lookup_primitive_type (language (), arch (), name);
469 }
470
471 /* A helper that updates the innermost block as appropriate. */
472
473 void
474 rust_parser::update_innermost_block (struct block_symbol sym)
475 {
476 if (symbol_read_needs_frame (sym.symbol))
477 pstate->block_tracker->update (sym);
478 }
479
480 /* Lex a hex number with at least MIN digits and at most MAX
481 digits. */
482
483 uint32_t
484 rust_parser::lex_hex (int min, int max)
485 {
486 uint32_t result = 0;
487 int len = 0;
488 /* We only want to stop at MAX if we're lexing a byte escape. */
489 int check_max = min == max;
490
491 while ((check_max ? len <= max : 1)
492 && ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
493 || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
494 || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')))
495 {
496 result *= 16;
497 if (pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
498 result = result + 10 + pstate->lexptr[0] - 'a';
499 else if (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
500 result = result + 10 + pstate->lexptr[0] - 'A';
501 else
502 result = result + pstate->lexptr[0] - '0';
503 ++pstate->lexptr;
504 ++len;
505 }
506
507 if (len < min)
508 error (_("Not enough hex digits seen"));
509 if (len > max)
510 {
511 gdb_assert (min != max);
512 error (_("Overlong hex escape"));
513 }
514
515 return result;
516 }
517
518 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
519 otherwise we're lexing a character escape. */
520
521 uint32_t
522 rust_parser::lex_escape (bool is_byte)
523 {
524 uint32_t result;
525
526 gdb_assert (pstate->lexptr[0] == '\\');
527 ++pstate->lexptr;
528 switch (pstate->lexptr[0])
529 {
530 case 'x':
531 ++pstate->lexptr;
532 result = lex_hex (2, 2);
533 break;
534
535 case 'u':
536 if (is_byte)
537 error (_("Unicode escape in byte literal"));
538 ++pstate->lexptr;
539 if (pstate->lexptr[0] != '{')
540 error (_("Missing '{' in Unicode escape"));
541 ++pstate->lexptr;
542 result = lex_hex (1, 6);
543 /* Could do range checks here. */
544 if (pstate->lexptr[0] != '}')
545 error (_("Missing '}' in Unicode escape"));
546 ++pstate->lexptr;
547 break;
548
549 case 'n':
550 result = '\n';
551 ++pstate->lexptr;
552 break;
553 case 'r':
554 result = '\r';
555 ++pstate->lexptr;
556 break;
557 case 't':
558 result = '\t';
559 ++pstate->lexptr;
560 break;
561 case '\\':
562 result = '\\';
563 ++pstate->lexptr;
564 break;
565 case '0':
566 result = '\0';
567 ++pstate->lexptr;
568 break;
569 case '\'':
570 result = '\'';
571 ++pstate->lexptr;
572 break;
573 case '"':
574 result = '"';
575 ++pstate->lexptr;
576 break;
577
578 default:
579 error (_("Invalid escape \\%c in literal"), pstate->lexptr[0]);
580 }
581
582 return result;
583 }
584
585 /* A helper for lex_character. Search forward for the closing single
586 quote, then convert the bytes from the host charset to UTF-32. */
587
588 static uint32_t
589 lex_multibyte_char (const char *text, int *len)
590 {
591 /* Only look a maximum of 5 bytes for the closing quote. This is
592 the maximum for UTF-8. */
593 int quote;
594 gdb_assert (text[0] != '\'');
595 for (quote = 1; text[quote] != '\0' && text[quote] != '\''; ++quote)
596 ;
597 *len = quote;
598 /* The caller will issue an error. */
599 if (text[quote] == '\0')
600 return 0;
601
602 auto_obstack result;
603 convert_between_encodings (host_charset (), HOST_UTF32,
604 (const gdb_byte *) text,
605 quote, 1, &result, translit_none);
606
607 int size = obstack_object_size (&result);
608 if (size > 4)
609 error (_("overlong character literal"));
610 uint32_t value;
611 memcpy (&value, obstack_finish (&result), size);
612 return value;
613 }
614
615 /* Lex a character constant. */
616
617 int
618 rust_parser::lex_character ()
619 {
620 bool is_byte = false;
621 uint32_t value;
622
623 if (pstate->lexptr[0] == 'b')
624 {
625 is_byte = true;
626 ++pstate->lexptr;
627 }
628 gdb_assert (pstate->lexptr[0] == '\'');
629 ++pstate->lexptr;
630 if (pstate->lexptr[0] == '\'')
631 error (_("empty character literal"));
632 else if (pstate->lexptr[0] == '\\')
633 value = lex_escape (is_byte);
634 else
635 {
636 int len;
637 value = lex_multibyte_char (&pstate->lexptr[0], &len);
638 pstate->lexptr += len;
639 }
640
641 if (pstate->lexptr[0] != '\'')
642 error (_("Unterminated character literal"));
643 ++pstate->lexptr;
644
645 current_int_val.val = value;
646 current_int_val.type = get_type (is_byte ? "u8" : "char");
647
648 return INTEGER;
649 }
650
651 /* Return the offset of the double quote if STR looks like the start
652 of a raw string, or 0 if STR does not start a raw string. */
653
654 static int
655 starts_raw_string (const char *str)
656 {
657 const char *save = str;
658
659 if (str[0] != 'r')
660 return 0;
661 ++str;
662 while (str[0] == '#')
663 ++str;
664 if (str[0] == '"')
665 return str - save;
666 return 0;
667 }
668
669 /* Return true if STR looks like the end of a raw string that had N
670 hashes at the start. */
671
672 static bool
673 ends_raw_string (const char *str, int n)
674 {
675 gdb_assert (str[0] == '"');
676 for (int i = 0; i < n; ++i)
677 if (str[i + 1] != '#')
678 return false;
679 return true;
680 }
681
682 /* Lex a string constant. */
683
684 int
685 rust_parser::lex_string ()
686 {
687 int is_byte = pstate->lexptr[0] == 'b';
688 int raw_length;
689
690 if (is_byte)
691 ++pstate->lexptr;
692 raw_length = starts_raw_string (pstate->lexptr);
693 pstate->lexptr += raw_length;
694 gdb_assert (pstate->lexptr[0] == '"');
695 ++pstate->lexptr;
696
697 while (1)
698 {
699 uint32_t value;
700
701 if (raw_length > 0)
702 {
703 if (pstate->lexptr[0] == '"' && ends_raw_string (pstate->lexptr,
704 raw_length - 1))
705 {
706 /* Exit with lexptr pointing after the final "#". */
707 pstate->lexptr += raw_length;
708 break;
709 }
710 else if (pstate->lexptr[0] == '\0')
711 error (_("Unexpected EOF in string"));
712
713 value = pstate->lexptr[0] & 0xff;
714 if (is_byte && value > 127)
715 error (_("Non-ASCII value in raw byte string"));
716 obstack_1grow (&obstack, value);
717
718 ++pstate->lexptr;
719 }
720 else if (pstate->lexptr[0] == '"')
721 {
722 /* Make sure to skip the quote. */
723 ++pstate->lexptr;
724 break;
725 }
726 else if (pstate->lexptr[0] == '\\')
727 {
728 value = lex_escape (is_byte);
729
730 if (is_byte)
731 obstack_1grow (&obstack, value);
732 else
733 convert_between_encodings (HOST_UTF32, "UTF-8",
734 (gdb_byte *) &value,
735 sizeof (value), sizeof (value),
736 &obstack, translit_none);
737 }
738 else if (pstate->lexptr[0] == '\0')
739 error (_("Unexpected EOF in string"));
740 else
741 {
742 value = pstate->lexptr[0] & 0xff;
743 if (is_byte && value > 127)
744 error (_("Non-ASCII value in byte string"));
745 obstack_1grow (&obstack, value);
746 ++pstate->lexptr;
747 }
748 }
749
750 current_string_val.length = obstack_object_size (&obstack);
751 current_string_val.ptr = (const char *) obstack_finish (&obstack);
752 return is_byte ? BYTESTRING : STRING;
753 }
754
755 /* Return true if STRING starts with whitespace followed by a digit. */
756
757 static bool
758 space_then_number (const char *string)
759 {
760 const char *p = string;
761
762 while (p[0] == ' ' || p[0] == '\t')
763 ++p;
764 if (p == string)
765 return false;
766
767 return *p >= '0' && *p <= '9';
768 }
769
770 /* Return true if C can start an identifier. */
771
772 static bool
773 rust_identifier_start_p (char c)
774 {
775 return ((c >= 'a' && c <= 'z')
776 || (c >= 'A' && c <= 'Z')
777 || c == '_'
778 || c == '$'
779 /* Allow any non-ASCII character as an identifier. There
780 doesn't seem to be a need to be picky about this. */
781 || (c & 0x80) != 0);
782 }
783
784 /* Lex an identifier. */
785
786 int
787 rust_parser::lex_identifier ()
788 {
789 unsigned int length;
790 const struct token_info *token;
791 int is_gdb_var = pstate->lexptr[0] == '$';
792
793 bool is_raw = false;
794 if (pstate->lexptr[0] == 'r'
795 && pstate->lexptr[1] == '#'
796 && rust_identifier_start_p (pstate->lexptr[2]))
797 {
798 is_raw = true;
799 pstate->lexptr += 2;
800 }
801
802 const char *start = pstate->lexptr;
803 gdb_assert (rust_identifier_start_p (pstate->lexptr[0]));
804
805 ++pstate->lexptr;
806
807 /* Allow any non-ASCII character here. This "handles" UTF-8 by
808 passing it through. */
809 while ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'z')
810 || (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'Z')
811 || pstate->lexptr[0] == '_'
812 || (is_gdb_var && pstate->lexptr[0] == '$')
813 || (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
814 || (pstate->lexptr[0] & 0x80) != 0)
815 ++pstate->lexptr;
816
817
818 length = pstate->lexptr - start;
819 token = NULL;
820 if (!is_raw)
821 {
822 for (const auto &candidate : identifier_tokens)
823 {
824 if (length == strlen (candidate.name)
825 && strncmp (candidate.name, start, length) == 0)
826 {
827 token = &candidate;
828 break;
829 }
830 }
831 }
832
833 if (token != NULL)
834 {
835 if (token->value == 0)
836 {
837 /* Leave the terminating token alone. */
838 pstate->lexptr = start;
839 return 0;
840 }
841 }
842 else if (token == NULL
843 && !is_raw
844 && (strncmp (start, "thread", length) == 0
845 || strncmp (start, "task", length) == 0)
846 && space_then_number (pstate->lexptr))
847 {
848 /* "task" or "thread" followed by a number terminates the
849 parse, per gdb rules. */
850 pstate->lexptr = start;
851 return 0;
852 }
853
854 if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0'))
855 {
856 current_string_val.length = length;
857 current_string_val.ptr = start;
858 }
859
860 if (pstate->parse_completion && pstate->lexptr[0] == '\0')
861 {
862 /* Prevent rustyylex from returning two COMPLETE tokens. */
863 pstate->prev_lexptr = pstate->lexptr;
864 return COMPLETE;
865 }
866
867 if (token != NULL)
868 return token->value;
869 if (is_gdb_var)
870 return GDBVAR;
871 return IDENT;
872 }
873
874 /* Lex an operator. */
875
876 int
877 rust_parser::lex_operator ()
878 {
879 const struct token_info *token = NULL;
880
881 for (const auto &candidate : operator_tokens)
882 {
883 if (strncmp (candidate.name, pstate->lexptr,
884 strlen (candidate.name)) == 0)
885 {
886 pstate->lexptr += strlen (candidate.name);
887 token = &candidate;
888 break;
889 }
890 }
891
892 if (token != NULL)
893 {
894 current_opcode = token->opcode;
895 return token->value;
896 }
897
898 return *pstate->lexptr++;
899 }
900
901 /* Lex a number. */
902
903 int
904 rust_parser::lex_number ()
905 {
906 regmatch_t subexps[NUM_SUBEXPRESSIONS];
907 int match;
908 bool is_integer = false;
909 bool could_be_decimal = true;
910 bool implicit_i32 = false;
911 const char *type_name = NULL;
912 struct type *type;
913 int end_index;
914 int type_index = -1;
915
916 match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps),
917 subexps, 0);
918 /* Failure means the regexp is broken. */
919 gdb_assert (match == 0);
920
921 if (subexps[INT_TEXT].rm_so != -1)
922 {
923 /* Integer part matched. */
924 is_integer = true;
925 end_index = subexps[INT_TEXT].rm_eo;
926 if (subexps[INT_TYPE].rm_so == -1)
927 {
928 type_name = "i32";
929 implicit_i32 = true;
930 }
931 else
932 {
933 type_index = INT_TYPE;
934 could_be_decimal = false;
935 }
936 }
937 else if (subexps[FLOAT_TYPE1].rm_so != -1)
938 {
939 /* Found floating point type suffix. */
940 end_index = subexps[FLOAT_TYPE1].rm_so;
941 type_index = FLOAT_TYPE1;
942 }
943 else if (subexps[FLOAT_TYPE2].rm_so != -1)
944 {
945 /* Found floating point type suffix. */
946 end_index = subexps[FLOAT_TYPE2].rm_so;
947 type_index = FLOAT_TYPE2;
948 }
949 else
950 {
951 /* Any other floating point match. */
952 end_index = subexps[0].rm_eo;
953 type_name = "f64";
954 }
955
956 /* We need a special case if the final character is ".". In this
957 case we might need to parse an integer. For example, "23.f()" is
958 a request for a trait method call, not a syntax error involving
959 the floating point number "23.". */
960 gdb_assert (subexps[0].rm_eo > 0);
961 if (pstate->lexptr[subexps[0].rm_eo - 1] == '.')
962 {
963 const char *next = skip_spaces (&pstate->lexptr[subexps[0].rm_eo]);
964
965 if (rust_identifier_start_p (*next) || *next == '.')
966 {
967 --subexps[0].rm_eo;
968 is_integer = true;
969 end_index = subexps[0].rm_eo;
970 type_name = "i32";
971 could_be_decimal = true;
972 implicit_i32 = true;
973 }
974 }
975
976 /* Compute the type name if we haven't already. */
977 std::string type_name_holder;
978 if (type_name == NULL)
979 {
980 gdb_assert (type_index != -1);
981 type_name_holder = std::string ((pstate->lexptr
982 + subexps[type_index].rm_so),
983 (subexps[type_index].rm_eo
984 - subexps[type_index].rm_so));
985 type_name = type_name_holder.c_str ();
986 }
987
988 /* Look up the type. */
989 type = get_type (type_name);
990
991 /* Copy the text of the number and remove the "_"s. */
992 std::string number;
993 for (int i = 0; i < end_index && pstate->lexptr[i]; ++i)
994 {
995 if (pstate->lexptr[i] == '_')
996 could_be_decimal = false;
997 else
998 number.push_back (pstate->lexptr[i]);
999 }
1000
1001 /* Advance past the match. */
1002 pstate->lexptr += subexps[0].rm_eo;
1003
1004 /* Parse the number. */
1005 if (is_integer)
1006 {
1007 int radix = 10;
1008 int offset = 0;
1009
1010 if (number[0] == '0')
1011 {
1012 if (number[1] == 'x')
1013 radix = 16;
1014 else if (number[1] == 'o')
1015 radix = 8;
1016 else if (number[1] == 'b')
1017 radix = 2;
1018 if (radix != 10)
1019 {
1020 offset = 2;
1021 could_be_decimal = false;
1022 }
1023 }
1024
1025 if (!current_int_val.val.set (number.c_str () + offset, radix))
1026 {
1027 /* Shouldn't be possible. */
1028 error (_("Invalid integer"));
1029 }
1030 if (implicit_i32)
1031 {
1032 static gdb_mpz sixty_three_bit = gdb_mpz::pow (2, 63);
1033 static gdb_mpz thirty_one_bit = gdb_mpz::pow (2, 31);
1034
1035 if (current_int_val.val >= sixty_three_bit)
1036 type = get_type ("i128");
1037 else if (current_int_val.val >= thirty_one_bit)
1038 type = get_type ("i64");
1039 }
1040
1041 current_int_val.type = type;
1042 }
1043 else
1044 {
1045 current_float_val.type = type;
1046 bool parsed = parse_float (number.c_str (), number.length (),
1047 current_float_val.type,
1048 current_float_val.val.data ());
1049 gdb_assert (parsed);
1050 }
1051
1052 return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
1053 }
1054
1055 /* The lexer. */
1056
1057 int
1058 rust_parser::lex_one_token ()
1059 {
1060 /* Skip all leading whitespace. */
1061 while (pstate->lexptr[0] == ' '
1062 || pstate->lexptr[0] == '\t'
1063 || pstate->lexptr[0] == '\r'
1064 || pstate->lexptr[0] == '\n')
1065 ++pstate->lexptr;
1066
1067 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1068 we're completing an empty string at the end of a field_expr.
1069 But, we don't want to return two COMPLETE tokens in a row. */
1070 if (pstate->lexptr[0] == '\0' && pstate->lexptr == pstate->prev_lexptr)
1071 return 0;
1072 pstate->prev_lexptr = pstate->lexptr;
1073 if (pstate->lexptr[0] == '\0')
1074 {
1075 if (pstate->parse_completion)
1076 {
1077 current_string_val.length =0;
1078 current_string_val.ptr = "";
1079 return COMPLETE;
1080 }
1081 return 0;
1082 }
1083
1084 if (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
1085 return lex_number ();
1086 else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '\'')
1087 return lex_character ();
1088 else if (pstate->lexptr[0] == 'b' && pstate->lexptr[1] == '"')
1089 return lex_string ();
1090 else if (pstate->lexptr[0] == 'b' && starts_raw_string (pstate->lexptr + 1))
1091 return lex_string ();
1092 else if (starts_raw_string (pstate->lexptr))
1093 return lex_string ();
1094 else if (rust_identifier_start_p (pstate->lexptr[0]))
1095 return lex_identifier ();
1096 else if (pstate->lexptr[0] == '"')
1097 return lex_string ();
1098 else if (pstate->lexptr[0] == '\'')
1099 return lex_character ();
1100 else if (pstate->lexptr[0] == '}' || pstate->lexptr[0] == ']')
1101 {
1102 /* Falls through to lex_operator. */
1103 --paren_depth;
1104 }
1105 else if (pstate->lexptr[0] == '(' || pstate->lexptr[0] == '{')
1106 {
1107 /* Falls through to lex_operator. */
1108 ++paren_depth;
1109 }
1110 else if (pstate->lexptr[0] == ',' && pstate->comma_terminates
1111 && paren_depth == 0)
1112 return 0;
1113
1114 return lex_operator ();
1115 }
1116
1117 /* Push back a single character to be re-lexed. */
1118
1119 void
1120 rust_parser::push_back (char c)
1121 {
1122 /* Can't be called before any lexing. */
1123 gdb_assert (pstate->prev_lexptr != NULL);
1124
1125 --pstate->lexptr;
1126 gdb_assert (*pstate->lexptr == c);
1127 }
1128
1129
1130
1132 /* Parse a tuple or paren expression. */
1133
1134 operation_up
1135 rust_parser::parse_tuple ()
1136 {
1137 assume ('(');
1138
1139 if (current_token == ')')
1140 {
1141 lex ();
1142 struct type *unit = get_type ("()");
1143 return make_operation<long_const_operation> (unit, 0);
1144 }
1145
1146 operation_up expr = parse_expr ();
1147 if (current_token == ')')
1148 {
1149 /* Parenthesized expression. */
1150 lex ();
1151 return make_operation<rust_parenthesized_operation> (std::move (expr));
1152 }
1153
1154 std::vector<operation_up> ops;
1155 ops.push_back (std::move (expr));
1156 while (current_token != ')')
1157 {
1158 if (current_token != ',')
1159 error (_("',' or ')' expected"));
1160 lex ();
1161
1162 /* A trailing "," is ok. */
1163 if (current_token != ')')
1164 ops.push_back (parse_expr ());
1165 }
1166
1167 assume (')');
1168
1169 error (_("Tuple expressions not supported yet"));
1170 }
1171
1172 /* Parse an array expression. */
1173
1174 operation_up
1175 rust_parser::parse_array ()
1176 {
1177 assume ('[');
1178
1179 if (current_token == KW_MUT)
1180 lex ();
1181
1182 operation_up result;
1183 operation_up expr = parse_expr ();
1184 if (current_token == ';')
1185 {
1186 lex ();
1187 operation_up rhs = parse_expr ();
1188 result = make_operation<rust_array_operation> (std::move (expr),
1189 std::move (rhs));
1190 }
1191 else if (current_token == ',' || current_token == ']')
1192 {
1193 std::vector<operation_up> ops;
1194 ops.push_back (std::move (expr));
1195 while (current_token != ']')
1196 {
1197 if (current_token != ',')
1198 error (_("',' or ']' expected"));
1199 lex ();
1200 ops.push_back (parse_expr ());
1201 }
1202 ops.shrink_to_fit ();
1203 int len = ops.size () - 1;
1204 result = make_operation<array_operation> (0, len, std::move (ops));
1205 }
1206 else
1207 error (_("',', ';', or ']' expected"));
1208
1209 require (']');
1210
1211 return result;
1212 }
1213
1214 /* Turn a name into an operation. */
1215
1216 operation_up
1217 rust_parser::name_to_operation (const std::string &name)
1218 {
1219 struct block_symbol sym = lookup_symbol (name.c_str (),
1220 pstate->expression_context_block,
1221 SEARCH_VFT);
1222 if (sym.symbol != nullptr && sym.symbol->aclass () != LOC_TYPEDEF)
1223 return make_operation<var_value_operation> (sym);
1224
1225 struct type *type = nullptr;
1226
1227 if (sym.symbol != nullptr)
1228 {
1229 gdb_assert (sym.symbol->aclass () == LOC_TYPEDEF);
1230 type = sym.symbol->type ();
1231 }
1232 if (type == nullptr)
1233 type = rust_lookup_type (name.c_str ());
1234 if (type == nullptr)
1235 error (_("No symbol '%s' in current context"), name.c_str ());
1236
1237 if (type->code () == TYPE_CODE_STRUCT && type->num_fields () == 0)
1238 {
1239 /* A unit-like struct. */
1240 operation_up result (new rust_aggregate_operation (type, {}, {}));
1241 return result;
1242 }
1243 else
1244 return make_operation<type_operation> (type);
1245 }
1246
1247 /* Parse a struct expression. */
1248
1249 operation_up
1250 rust_parser::parse_struct_expr (struct type *type)
1251 {
1252 assume ('{');
1253
1254 if (type->code () != TYPE_CODE_STRUCT
1255 || rust_tuple_type_p (type)
1256 || rust_tuple_struct_type_p (type))
1257 error (_("Struct expression applied to non-struct type"));
1258
1259 std::vector<std::pair<std::string, operation_up>> field_v;
1260 while (current_token != '}' && current_token != DOTDOT)
1261 {
1262 if (current_token != IDENT)
1263 error (_("'}', '..', or identifier expected"));
1264
1265 std::string name = get_string ();
1266 lex ();
1267
1268 operation_up expr;
1269 if (current_token == ',' || current_token == '}'
1270 || current_token == DOTDOT)
1271 expr = name_to_operation (name);
1272 else
1273 {
1274 require (':');
1275 expr = parse_expr ();
1276 }
1277 field_v.emplace_back (std::move (name), std::move (expr));
1278
1279 /* A trailing "," is ok. */
1280 if (current_token == ',')
1281 lex ();
1282 }
1283
1284 operation_up others;
1285 if (current_token == DOTDOT)
1286 {
1287 lex ();
1288 others = parse_expr ();
1289 }
1290
1291 require ('}');
1292
1293 return make_operation<rust_aggregate_operation> (type,
1294 std::move (others),
1295 std::move (field_v));
1296 }
1297
1298 /* Used by the operator precedence parser. */
1299 struct rustop_item
1300 {
1301 rustop_item (int token_, int precedence_, enum exp_opcode opcode_,
1302 operation_up &&op_)
1303 : token (token_),
1304 precedence (precedence_),
1305 opcode (opcode_),
1306 op (std::move (op_))
1307 {
1308 }
1309
1310 /* The token value. */
1311 int token;
1312 /* Precedence of this operator. */
1313 int precedence;
1314 /* This is used only for assign-modify. */
1315 enum exp_opcode opcode;
1316 /* The right hand side of this operation. */
1317 operation_up op;
1318 };
1319
1320 /* An operator precedence parser for binary operations, including
1321 "as". */
1322
1323 operation_up
1324 rust_parser::parse_binop (bool required)
1325 {
1326 /* All the binary operators. Each one is of the form
1327 OPERATION(TOKEN, PRECEDENCE, TYPE)
1328 TOKEN is the corresponding operator token.
1329 PRECEDENCE is a value indicating relative precedence.
1330 TYPE is the operation type corresponding to the operator.
1331 Assignment operations are handled specially, not via this
1332 table; they have precedence 0. */
1333 #define ALL_OPS \
1334 OPERATION ('*', 10, mul_operation) \
1335 OPERATION ('/', 10, div_operation) \
1336 OPERATION ('%', 10, rem_operation) \
1337 OPERATION ('@', 9, repeat_operation) \
1338 OPERATION ('+', 8, add_operation) \
1339 OPERATION ('-', 8, sub_operation) \
1340 OPERATION (LSH, 7, lsh_operation) \
1341 OPERATION (RSH, 7, rsh_operation) \
1342 OPERATION ('&', 6, bitwise_and_operation) \
1343 OPERATION ('^', 5, bitwise_xor_operation) \
1344 OPERATION ('|', 4, bitwise_ior_operation) \
1345 OPERATION (EQEQ, 3, equal_operation) \
1346 OPERATION (NOTEQ, 3, notequal_operation) \
1347 OPERATION ('<', 3, less_operation) \
1348 OPERATION (LTEQ, 3, leq_operation) \
1349 OPERATION ('>', 3, gtr_operation) \
1350 OPERATION (GTEQ, 3, geq_operation) \
1351 OPERATION (ANDAND, 2, logical_and_operation) \
1352 OPERATION (OROR, 1, logical_or_operation)
1353
1354 #define ASSIGN_PREC 0
1355
1356 operation_up start = parse_atom (required);
1357 if (start == nullptr)
1358 {
1359 gdb_assert (!required);
1360 return start;
1361 }
1362
1363 std::vector<rustop_item> operator_stack;
1364 operator_stack.emplace_back (0, -1, OP_NULL, std::move (start));
1365
1366 while (true)
1367 {
1368 int this_token = current_token;
1369 enum exp_opcode compound_assign_op = OP_NULL;
1370 int precedence = -2;
1371
1372 switch (this_token)
1373 {
1374 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1375 case TOKEN: \
1376 precedence = PRECEDENCE; \
1377 lex (); \
1378 break;
1379
1380 ALL_OPS
1381
1382 #undef OPERATION
1383
1384 case COMPOUND_ASSIGN:
1385 compound_assign_op = current_opcode;
1386 [[fallthrough]];
1387 case '=':
1388 precedence = ASSIGN_PREC;
1389 lex ();
1390 break;
1391
1392 /* "as" must be handled specially. */
1393 case KW_AS:
1394 {
1395 lex ();
1396 rustop_item &lhs = operator_stack.back ();
1397 struct type *type = parse_type ();
1398 lhs.op = make_operation<unop_cast_operation> (std::move (lhs.op),
1399 type);
1400 }
1401 /* Bypass the rest of the loop. */
1402 continue;
1403
1404 default:
1405 /* Arrange to pop the entire stack. */
1406 precedence = -2;
1407 break;
1408 }
1409
1410 /* Make sure that assignments are right-associative while other
1411 operations are left-associative. */
1412 while ((precedence == ASSIGN_PREC
1413 ? precedence < operator_stack.back ().precedence
1414 : precedence <= operator_stack.back ().precedence)
1415 && operator_stack.size () > 1)
1416 {
1417 rustop_item rhs = std::move (operator_stack.back ());
1418 operator_stack.pop_back ();
1419
1420 rustop_item &lhs = operator_stack.back ();
1421
1422 switch (rhs.token)
1423 {
1424 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1425 case TOKEN: \
1426 lhs.op = make_operation<TYPE> (std::move (lhs.op), \
1427 std::move (rhs.op)); \
1428 break;
1429
1430 ALL_OPS
1431
1432 #undef OPERATION
1433
1434 case '=':
1435 case COMPOUND_ASSIGN:
1436 {
1437 if (rhs.token == '=')
1438 lhs.op = (make_operation<assign_operation>
1439 (std::move (lhs.op), std::move (rhs.op)));
1440 else
1441 lhs.op = (make_operation<assign_modify_operation>
1442 (rhs.opcode, std::move (lhs.op),
1443 std::move (rhs.op)));
1444
1445 struct type *unit_type = get_type ("()");
1446
1447 operation_up nil (new long_const_operation (unit_type, 0));
1448 lhs.op = (make_operation<comma_operation>
1449 (std::move (lhs.op), std::move (nil)));
1450 }
1451 break;
1452
1453 default:
1454 gdb_assert_not_reached ("bad binary operator");
1455 }
1456 }
1457
1458 if (precedence == -2)
1459 break;
1460
1461 operator_stack.emplace_back (this_token, precedence, compound_assign_op,
1462 parse_atom (true));
1463 }
1464
1465 gdb_assert (operator_stack.size () == 1);
1466 return std::move (operator_stack[0].op);
1467 #undef ALL_OPS
1468 }
1469
1470 /* Parse a range expression. */
1471
1472 operation_up
1473 rust_parser::parse_range ()
1474 {
1475 enum range_flag kind = (RANGE_HIGH_BOUND_DEFAULT
1476 | RANGE_LOW_BOUND_DEFAULT);
1477
1478 operation_up lhs;
1479 if (current_token != DOTDOT && current_token != DOTDOTEQ)
1480 {
1481 lhs = parse_binop (true);
1482 kind &= ~RANGE_LOW_BOUND_DEFAULT;
1483 }
1484
1485 if (current_token == DOTDOT)
1486 kind |= RANGE_HIGH_BOUND_EXCLUSIVE;
1487 else if (current_token != DOTDOTEQ)
1488 return lhs;
1489 lex ();
1490
1491 /* A "..=" range requires a high bound, but otherwise it is
1492 optional. */
1493 operation_up rhs = parse_binop ((kind & RANGE_HIGH_BOUND_EXCLUSIVE) == 0);
1494 if (rhs != nullptr)
1495 kind &= ~RANGE_HIGH_BOUND_DEFAULT;
1496
1497 return make_operation<rust_range_operation> (kind,
1498 std::move (lhs),
1499 std::move (rhs));
1500 }
1501
1502 /* Parse an expression. */
1503
1504 operation_up
1505 rust_parser::parse_expr ()
1506 {
1507 return parse_range ();
1508 }
1509
1510 /* Parse a sizeof expression. */
1511
1512 operation_up
1513 rust_parser::parse_sizeof ()
1514 {
1515 assume (KW_SIZEOF);
1516
1517 require ('(');
1518 operation_up result = make_operation<unop_sizeof_operation> (parse_expr ());
1519 require (')');
1520 return result;
1521 }
1522
1523 /* Parse an address-of operation. */
1524
1525 operation_up
1526 rust_parser::parse_addr ()
1527 {
1528 assume ('&');
1529
1530 if (current_token == KW_MUT)
1531 lex ();
1532
1533 return make_operation<rust_unop_addr_operation> (parse_atom (true));
1534 }
1535
1536 /* Parse a field expression. */
1537
1538 operation_up
1539 rust_parser::parse_field (operation_up &&lhs)
1540 {
1541 assume ('.');
1542
1543 operation_up result;
1544 switch (current_token)
1545 {
1546 case IDENT:
1547 case COMPLETE:
1548 {
1549 bool is_complete = current_token == COMPLETE;
1550 auto struct_op = new rust_structop (std::move (lhs), get_string ());
1551 lex ();
1552 if (is_complete)
1553 {
1554 completion_op.reset (struct_op);
1555 pstate->mark_struct_expression (struct_op);
1556 /* Throw to the outermost level of the parser. */
1557 error (_("not really an error"));
1558 }
1559 result.reset (struct_op);
1560 }
1561 break;
1562
1563 case DECIMAL_INTEGER:
1564 {
1565 int idx = current_int_val.val.as_integer<int> ();
1566 result = make_operation<rust_struct_anon> (idx, std::move (lhs));
1567 lex ();
1568 }
1569 break;
1570
1571 case INTEGER:
1572 error (_("'_' not allowed in integers in anonymous field references"));
1573
1574 default:
1575 error (_("field name expected"));
1576 }
1577
1578 return result;
1579 }
1580
1581 /* Parse an index expression. */
1582
1583 operation_up
1584 rust_parser::parse_index (operation_up &&lhs)
1585 {
1586 assume ('[');
1587 operation_up rhs = parse_expr ();
1588 require (']');
1589
1590 return make_operation<rust_subscript_operation> (std::move (lhs),
1591 std::move (rhs));
1592 }
1593
1594 /* Parse a sequence of comma-separated expressions in parens. */
1595
1596 std::vector<operation_up>
1597 rust_parser::parse_paren_args ()
1598 {
1599 assume ('(');
1600
1601 std::vector<operation_up> args;
1602 while (current_token != ')')
1603 {
1604 if (!args.empty ())
1605 {
1606 if (current_token != ',')
1607 error (_("',' or ')' expected"));
1608 lex ();
1609 }
1610
1611 args.push_back (parse_expr ());
1612 }
1613
1614 assume (')');
1615
1616 return args;
1617 }
1618
1619 /* Parse the parenthesized part of a function call. */
1620
1621 operation_up
1622 rust_parser::parse_call (operation_up &&lhs)
1623 {
1624 std::vector<operation_up> args = parse_paren_args ();
1625
1626 return make_operation<funcall_operation> (std::move (lhs),
1627 std::move (args));
1628 }
1629
1630 /* Parse a list of types. */
1631
1632 std::vector<struct type *>
1633 rust_parser::parse_type_list ()
1634 {
1635 std::vector<struct type *> result;
1636 result.push_back (parse_type ());
1637 while (current_token == ',')
1638 {
1639 lex ();
1640 result.push_back (parse_type ());
1641 }
1642 return result;
1643 }
1644
1645 /* Parse a possibly-empty list of types, surrounded in parens. */
1646
1647 std::vector<struct type *>
1648 rust_parser::parse_maybe_type_list ()
1649 {
1650 assume ('(');
1651 std::vector<struct type *> types;
1652 if (current_token != ')')
1653 types = parse_type_list ();
1654 require (')');
1655 return types;
1656 }
1657
1658 /* Parse an array type. */
1659
1660 struct type *
1661 rust_parser::parse_array_type ()
1662 {
1663 assume ('[');
1664 struct type *elt_type = parse_type ();
1665 require (';');
1666
1667 if (current_token != INTEGER && current_token != DECIMAL_INTEGER)
1668 error (_("integer expected"));
1669 ULONGEST val = current_int_val.val.as_integer<ULONGEST> ();
1670 lex ();
1671 require (']');
1672
1673 return lookup_array_range_type (elt_type, 0, val - 1);
1674 }
1675
1676 /* Parse a slice type. */
1677
1678 struct type *
1679 rust_parser::parse_slice_type ()
1680 {
1681 assume ('&');
1682
1683 /* Handle &str specially. This is an important type in Rust. While
1684 the compiler does emit the "&str" type in the DWARF, just "str"
1685 itself isn't always available -- but it's handy if this works
1686 seamlessly. */
1687 if (current_token == IDENT && get_string () == "str")
1688 {
1689 lex ();
1690 return rust_slice_type ("&str", get_type ("u8"), get_type ("usize"));
1691 }
1692
1693 bool is_slice = current_token == '[';
1694 if (is_slice)
1695 lex ();
1696
1697 struct type *target = parse_type ();
1698
1699 if (is_slice)
1700 {
1701 require (']');
1702 return rust_slice_type ("&[*gdb*]", target, get_type ("usize"));
1703 }
1704
1705 /* For now we treat &x and *x identically. */
1706 return lookup_pointer_type (target);
1707 }
1708
1709 /* Parse a pointer type. */
1710
1711 struct type *
1712 rust_parser::parse_pointer_type ()
1713 {
1714 assume ('*');
1715
1716 if (current_token == KW_MUT || current_token == KW_CONST)
1717 lex ();
1718
1719 struct type *target = parse_type ();
1720 /* For the time being we ignore mut/const. */
1721 return lookup_pointer_type (target);
1722 }
1723
1724 /* Parse a function type. */
1725
1726 struct type *
1727 rust_parser::parse_function_type ()
1728 {
1729 assume (KW_FN);
1730
1731 if (current_token != '(')
1732 error (_("'(' expected"));
1733
1734 std::vector<struct type *> types = parse_maybe_type_list ();
1735
1736 if (current_token != ARROW)
1737 error (_("'->' expected"));
1738 lex ();
1739
1740 struct type *result_type = parse_type ();
1741
1742 struct type **argtypes = nullptr;
1743 if (!types.empty ())
1744 argtypes = types.data ();
1745
1746 result_type = lookup_function_type_with_arguments (result_type,
1747 types.size (),
1748 argtypes);
1749 return lookup_pointer_type (result_type);
1750 }
1751
1752 /* Parse a tuple type. */
1753
1754 struct type *
1755 rust_parser::parse_tuple_type ()
1756 {
1757 std::vector<struct type *> types = parse_maybe_type_list ();
1758
1759 auto_obstack obstack;
1760 obstack_1grow (&obstack, '(');
1761 for (int i = 0; i < types.size (); ++i)
1762 {
1763 std::string type_name = type_to_string (types[i]);
1764
1765 if (i > 0)
1766 obstack_1grow (&obstack, ',');
1767 obstack_grow_str (&obstack, type_name.c_str ());
1768 }
1769
1770 obstack_grow_str0 (&obstack, ")");
1771 const char *name = (const char *) obstack_finish (&obstack);
1772
1773 /* We don't allow creating new tuple types (yet), but we do allow
1774 looking up existing tuple types. */
1775 struct type *result = rust_lookup_type (name);
1776 if (result == nullptr)
1777 error (_("could not find tuple type '%s'"), name);
1778
1779 return result;
1780 }
1781
1782 /* Parse a type. */
1783
1784 struct type *
1785 rust_parser::parse_type ()
1786 {
1787 switch (current_token)
1788 {
1789 case '[':
1790 return parse_array_type ();
1791 case '&':
1792 return parse_slice_type ();
1793 case '*':
1794 return parse_pointer_type ();
1795 case KW_FN:
1796 return parse_function_type ();
1797 case '(':
1798 return parse_tuple_type ();
1799 case KW_SELF:
1800 case KW_SUPER:
1801 case COLONCOLON:
1802 case KW_EXTERN:
1803 case IDENT:
1804 {
1805 std::string path = parse_path (false);
1806 struct type *result = rust_lookup_type (path.c_str ());
1807 if (result == nullptr)
1808 error (_("No type name '%s' in current context"), path.c_str ());
1809 return result;
1810 }
1811 default:
1812 error (_("type expected"));
1813 }
1814 }
1815
1816 /* Parse a path. */
1817
1818 std::string
1819 rust_parser::parse_path (bool for_expr)
1820 {
1821 unsigned n_supers = 0;
1822 int first_token = current_token;
1823
1824 switch (current_token)
1825 {
1826 case KW_SELF:
1827 lex ();
1828 if (current_token != COLONCOLON)
1829 return "self";
1830 lex ();
1831 [[fallthrough]];
1832 case KW_SUPER:
1833 while (current_token == KW_SUPER)
1834 {
1835 ++n_supers;
1836 lex ();
1837 if (current_token != COLONCOLON)
1838 error (_("'::' expected"));
1839 lex ();
1840 }
1841 break;
1842
1843 case COLONCOLON:
1844 lex ();
1845 break;
1846
1847 case KW_EXTERN:
1848 /* This is a gdb extension to make it possible to refer to items
1849 in other crates. It just bypasses adding the current crate
1850 to the front of the name. */
1851 lex ();
1852 break;
1853 }
1854
1855 if (current_token != IDENT)
1856 error (_("identifier expected"));
1857 std::string path = get_string ();
1858 bool saw_ident = true;
1859 lex ();
1860
1861 /* The condition here lets us enter the loop even if we see
1862 "ident<...>". */
1863 while (current_token == COLONCOLON || current_token == '<')
1864 {
1865 if (current_token == COLONCOLON)
1866 {
1867 lex ();
1868 saw_ident = false;
1869
1870 if (current_token == IDENT)
1871 {
1872 path = path + "::" + get_string ();
1873 lex ();
1874 saw_ident = true;
1875 }
1876 else if (current_token == COLONCOLON)
1877 {
1878 /* The code below won't detect this scenario. */
1879 error (_("unexpected '::'"));
1880 }
1881 }
1882
1883 if (current_token != '<')
1884 continue;
1885
1886 /* Expression use name::<...>, whereas types use name<...>. */
1887 if (for_expr)
1888 {
1889 /* Expressions use "name::<...>", so if we saw an identifier
1890 after the "::", we ignore the "<" here. */
1891 if (saw_ident)
1892 break;
1893 }
1894 else
1895 {
1896 /* Types use "name<...>", so we need to have seen the
1897 identifier. */
1898 if (!saw_ident)
1899 break;
1900 }
1901
1902 lex ();
1903 std::vector<struct type *> types = parse_type_list ();
1904 if (current_token == '>')
1905 lex ();
1906 else if (current_token == RSH)
1907 {
1908 push_back ('>');
1909 lex ();
1910 }
1911 else
1912 error (_("'>' expected"));
1913
1914 path += "<";
1915 for (int i = 0; i < types.size (); ++i)
1916 {
1917 if (i > 0)
1918 path += ",";
1919 path += type_to_string (types[i]);
1920 }
1921 path += ">";
1922 break;
1923 }
1924
1925 switch (first_token)
1926 {
1927 case KW_SELF:
1928 case KW_SUPER:
1929 return super_name (path, n_supers);
1930
1931 case COLONCOLON:
1932 return crate_name (path);
1933
1934 case KW_EXTERN:
1935 return "::" + path;
1936
1937 case IDENT:
1938 return path;
1939
1940 default:
1941 gdb_assert_not_reached ("missing case in path parsing");
1942 }
1943 }
1944
1945 /* Handle the parsing for a string expression. */
1946
1947 operation_up
1948 rust_parser::parse_string ()
1949 {
1950 gdb_assert (current_token == STRING);
1951
1952 /* Wrap the raw string in the &str struct. */
1953 struct type *type = rust_lookup_type ("&str");
1954 if (type == nullptr)
1955 error (_("Could not find type '&str'"));
1956
1957 std::vector<std::pair<std::string, operation_up>> field_v;
1958
1959 size_t len = current_string_val.length;
1960 operation_up str = make_operation<string_operation> (get_string ());
1961 operation_up addr
1962 = make_operation<rust_unop_addr_operation> (std::move (str));
1963 field_v.emplace_back ("data_ptr", std::move (addr));
1964
1965 struct type *valtype = get_type ("usize");
1966 operation_up lenop = make_operation<long_const_operation> (valtype, len);
1967 field_v.emplace_back ("length", std::move (lenop));
1968
1969 return make_operation<rust_aggregate_operation> (type,
1970 operation_up (),
1971 std::move (field_v));
1972 }
1973
1974 /* Parse a tuple struct expression. */
1975
1976 operation_up
1977 rust_parser::parse_tuple_struct (struct type *type)
1978 {
1979 std::vector<operation_up> args = parse_paren_args ();
1980
1981 std::vector<std::pair<std::string, operation_up>> field_v (args.size ());
1982 for (int i = 0; i < args.size (); ++i)
1983 field_v[i] = { string_printf ("__%d", i), std::move (args[i]) };
1984
1985 return (make_operation<rust_aggregate_operation>
1986 (type, operation_up (), std::move (field_v)));
1987 }
1988
1989 /* Parse a path expression. */
1990
1991 operation_up
1992 rust_parser::parse_path_expr ()
1993 {
1994 std::string path = parse_path (true);
1995
1996 if (current_token == '{')
1997 {
1998 struct type *type = rust_lookup_type (path.c_str ());
1999 if (type == nullptr)
2000 error (_("Could not find type '%s'"), path.c_str ());
2001
2002 return parse_struct_expr (type);
2003 }
2004 else if (current_token == '(')
2005 {
2006 struct type *type = rust_lookup_type (path.c_str ());
2007 /* If this is actually a tuple struct expression, handle it
2008 here. If it is a call, it will be handled elsewhere. */
2009 if (type != nullptr)
2010 {
2011 if (!rust_tuple_struct_type_p (type))
2012 error (_("Type %s is not a tuple struct"), path.c_str ());
2013 return parse_tuple_struct (type);
2014 }
2015 }
2016
2017 return name_to_operation (path);
2018 }
2019
2020 /* Parse an atom. "Atom" isn't a Rust term, but this refers to a
2021 single unitary item in the grammar; but here including some unary
2022 prefix and postfix expressions. */
2023
2024 operation_up
2025 rust_parser::parse_atom (bool required)
2026 {
2027 operation_up result;
2028
2029 switch (current_token)
2030 {
2031 case '(':
2032 result = parse_tuple ();
2033 break;
2034
2035 case '[':
2036 result = parse_array ();
2037 break;
2038
2039 case INTEGER:
2040 case DECIMAL_INTEGER:
2041 result = make_operation<long_const_operation> (current_int_val.type,
2042 current_int_val.val);
2043 lex ();
2044 break;
2045
2046 case FLOAT:
2047 result = make_operation<float_const_operation> (current_float_val.type,
2048 current_float_val.val);
2049 lex ();
2050 break;
2051
2052 case STRING:
2053 result = parse_string ();
2054 lex ();
2055 break;
2056
2057 case BYTESTRING:
2058 result = make_operation<string_operation> (get_string ());
2059 lex ();
2060 break;
2061
2062 case KW_TRUE:
2063 case KW_FALSE:
2064 result = make_operation<bool_operation> (current_token == KW_TRUE);
2065 lex ();
2066 break;
2067
2068 case GDBVAR:
2069 /* This is kind of a hacky approach. */
2070 {
2071 pstate->push_dollar (current_string_val);
2072 result = pstate->pop ();
2073 lex ();
2074 }
2075 break;
2076
2077 case KW_SELF:
2078 case KW_SUPER:
2079 case COLONCOLON:
2080 case KW_EXTERN:
2081 case IDENT:
2082 result = parse_path_expr ();
2083 break;
2084
2085 case '*':
2086 lex ();
2087 result = make_operation<rust_unop_ind_operation> (parse_atom (true));
2088 break;
2089 case '+':
2090 lex ();
2091 result = make_operation<unary_plus_operation> (parse_atom (true));
2092 break;
2093 case '-':
2094 lex ();
2095 result = make_operation<unary_neg_operation> (parse_atom (true));
2096 break;
2097 case '!':
2098 lex ();
2099 result = make_operation<rust_unop_compl_operation> (parse_atom (true));
2100 break;
2101 case KW_SIZEOF:
2102 result = parse_sizeof ();
2103 break;
2104 case '&':
2105 result = parse_addr ();
2106 break;
2107
2108 default:
2109 if (!required)
2110 return {};
2111 error (_("unexpected token"));
2112 }
2113
2114 /* Now parse suffixes. */
2115 while (true)
2116 {
2117 switch (current_token)
2118 {
2119 case '.':
2120 result = parse_field (std::move (result));
2121 break;
2122
2123 case '[':
2124 result = parse_index (std::move (result));
2125 break;
2126
2127 case '(':
2128 result = parse_call (std::move (result));
2129 break;
2130
2131 default:
2132 return result;
2133 }
2134 }
2135 }
2136
2137
2138
2140 /* The parser as exposed to gdb. */
2141
2142 int
2143 rust_language::parser (struct parser_state *state) const
2144 {
2145 rust_parser parser (state);
2146
2147 operation_up result;
2148 try
2149 {
2150 result = parser.parse_entry_point ();
2151 }
2152 catch (const gdb_exception &exc)
2153 {
2154 if (state->parse_completion)
2155 {
2156 result = std::move (parser.completion_op);
2157 if (result == nullptr)
2158 throw;
2159 }
2160 else
2161 throw;
2162 }
2163
2164 state->set_operation (std::move (result));
2165
2166 return 0;
2167 }
2168
2169
2170
2172 #if GDB_SELF_TEST
2173
2174 /* A test helper that lexes a string, expecting a single token. */
2175
2176 static void
2177 rust_lex_test_one (rust_parser *parser, const char *input, int expected)
2178 {
2179 int token;
2180
2181 parser->reset (input);
2182
2183 token = parser->lex_one_token ();
2184 SELF_CHECK (token == expected);
2185
2186 if (token)
2187 {
2188 token = parser->lex_one_token ();
2189 SELF_CHECK (token == 0);
2190 }
2191 }
2192
2193 /* Test that INPUT lexes as the integer VALUE. */
2194
2195 static void
2196 rust_lex_int_test (rust_parser *parser, const char *input,
2197 ULONGEST value, int kind)
2198 {
2199 rust_lex_test_one (parser, input, kind);
2200 SELF_CHECK (parser->current_int_val.val == value);
2201 }
2202
2203 /* Test that INPUT throws an exception with text ERR. */
2204
2205 static void
2206 rust_lex_exception_test (rust_parser *parser, const char *input,
2207 const char *err)
2208 {
2209 try
2210 {
2211 /* The "kind" doesn't matter. */
2212 rust_lex_test_one (parser, input, DECIMAL_INTEGER);
2213 SELF_CHECK (0);
2214 }
2215 catch (const gdb_exception_error &except)
2216 {
2217 SELF_CHECK (strcmp (except.what (), err) == 0);
2218 }
2219 }
2220
2221 /* Test that INPUT lexes as the identifier, string, or byte-string
2222 VALUE. KIND holds the expected token kind. */
2223
2224 static void
2225 rust_lex_stringish_test (rust_parser *parser, const char *input,
2226 const char *value, int kind)
2227 {
2228 rust_lex_test_one (parser, input, kind);
2229 SELF_CHECK (parser->get_string () == value);
2230 }
2231
2232 /* Helper to test that a string parses as a given token sequence. */
2233
2234 static void
2235 rust_lex_test_sequence (rust_parser *parser, const char *input, int len,
2236 const int expected[])
2237 {
2238 parser->reset (input);
2239
2240 for (int i = 0; i < len; ++i)
2241 {
2242 int token = parser->lex_one_token ();
2243 SELF_CHECK (token == expected[i]);
2244 }
2245 }
2246
2247 /* Tests for an integer-parsing corner case. */
2248
2249 static void
2250 rust_lex_test_trailing_dot (rust_parser *parser)
2251 {
2252 const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
2253 const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
2254 const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
2255 const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
2256
2257 rust_lex_test_sequence (parser, "23.g()", ARRAY_SIZE (expected1), expected1);
2258 rust_lex_test_sequence (parser, "23_0.g()", ARRAY_SIZE (expected2),
2259 expected2);
2260 rust_lex_test_sequence (parser, "23.==()", ARRAY_SIZE (expected3),
2261 expected3);
2262 rust_lex_test_sequence (parser, "23..25", ARRAY_SIZE (expected4), expected4);
2263 }
2264
2265 /* Tests of completion. */
2266
2267 static void
2268 rust_lex_test_completion (rust_parser *parser)
2269 {
2270 const int expected[] = { IDENT, '.', COMPLETE, 0 };
2271
2272 parser->pstate->parse_completion = true;
2273
2274 rust_lex_test_sequence (parser, "something.wha", ARRAY_SIZE (expected),
2275 expected);
2276 rust_lex_test_sequence (parser, "something.", ARRAY_SIZE (expected),
2277 expected);
2278
2279 parser->pstate->parse_completion = false;
2280 }
2281
2282 /* Test pushback. */
2283
2284 static void
2285 rust_lex_test_push_back (rust_parser *parser)
2286 {
2287 int token;
2288
2289 parser->reset (">>=");
2290
2291 token = parser->lex_one_token ();
2292 SELF_CHECK (token == COMPOUND_ASSIGN);
2293 SELF_CHECK (parser->current_opcode == BINOP_RSH);
2294
2295 parser->push_back ('=');
2296
2297 token = parser->lex_one_token ();
2298 SELF_CHECK (token == '=');
2299
2300 token = parser->lex_one_token ();
2301 SELF_CHECK (token == 0);
2302 }
2303
2304 /* Unit test the lexer. */
2305
2306 static void
2307 rust_lex_tests (void)
2308 {
2309 /* Set up dummy "parser", so that rust_type works. */
2310 parser_state ps (language_def (language_rust), current_inferior ()->arch (),
2311 nullptr, 0, 0, nullptr, 0, nullptr);
2312 rust_parser parser (&ps);
2313
2314 rust_lex_test_one (&parser, "", 0);
2315 rust_lex_test_one (&parser, " \t \n \r ", 0);
2316 rust_lex_test_one (&parser, "thread 23", 0);
2317 rust_lex_test_one (&parser, "task 23", 0);
2318 rust_lex_test_one (&parser, "th 104", 0);
2319 rust_lex_test_one (&parser, "ta 97", 0);
2320
2321 rust_lex_int_test (&parser, "'z'", 'z', INTEGER);
2322 rust_lex_int_test (&parser, "'\\xff'", 0xff, INTEGER);
2323 rust_lex_int_test (&parser, "'\\u{1016f}'", 0x1016f, INTEGER);
2324 rust_lex_int_test (&parser, "b'z'", 'z', INTEGER);
2325 rust_lex_int_test (&parser, "b'\\xfe'", 0xfe, INTEGER);
2326 rust_lex_int_test (&parser, "b'\\xFE'", 0xfe, INTEGER);
2327 rust_lex_int_test (&parser, "b'\\xfE'", 0xfe, INTEGER);
2328
2329 /* Test all escapes in both modes. */
2330 rust_lex_int_test (&parser, "'\\n'", '\n', INTEGER);
2331 rust_lex_int_test (&parser, "'\\r'", '\r', INTEGER);
2332 rust_lex_int_test (&parser, "'\\t'", '\t', INTEGER);
2333 rust_lex_int_test (&parser, "'\\\\'", '\\', INTEGER);
2334 rust_lex_int_test (&parser, "'\\0'", '\0', INTEGER);
2335 rust_lex_int_test (&parser, "'\\''", '\'', INTEGER);
2336 rust_lex_int_test (&parser, "'\\\"'", '"', INTEGER);
2337
2338 rust_lex_int_test (&parser, "b'\\n'", '\n', INTEGER);
2339 rust_lex_int_test (&parser, "b'\\r'", '\r', INTEGER);
2340 rust_lex_int_test (&parser, "b'\\t'", '\t', INTEGER);
2341 rust_lex_int_test (&parser, "b'\\\\'", '\\', INTEGER);
2342 rust_lex_int_test (&parser, "b'\\0'", '\0', INTEGER);
2343 rust_lex_int_test (&parser, "b'\\''", '\'', INTEGER);
2344 rust_lex_int_test (&parser, "b'\\\"'", '"', INTEGER);
2345
2346 rust_lex_exception_test (&parser, "'z", "Unterminated character literal");
2347 rust_lex_exception_test (&parser, "b'\\x0'", "Not enough hex digits seen");
2348 rust_lex_exception_test (&parser, "b'\\u{0}'",
2349 "Unicode escape in byte literal");
2350 rust_lex_exception_test (&parser, "'\\x0'", "Not enough hex digits seen");
2351 rust_lex_exception_test (&parser, "'\\u0'", "Missing '{' in Unicode escape");
2352 rust_lex_exception_test (&parser, "'\\u{0", "Missing '}' in Unicode escape");
2353 rust_lex_exception_test (&parser, "'\\u{0000007}", "Overlong hex escape");
2354 rust_lex_exception_test (&parser, "'\\u{}", "Not enough hex digits seen");
2355 rust_lex_exception_test (&parser, "'\\Q'", "Invalid escape \\Q in literal");
2356 rust_lex_exception_test (&parser, "b'\\Q'", "Invalid escape \\Q in literal");
2357
2358 rust_lex_int_test (&parser, "23", 23, DECIMAL_INTEGER);
2359 rust_lex_int_test (&parser, "2_344__29", 234429, INTEGER);
2360 rust_lex_int_test (&parser, "0x1f", 0x1f, INTEGER);
2361 rust_lex_int_test (&parser, "23usize", 23, INTEGER);
2362 rust_lex_int_test (&parser, "23i32", 23, INTEGER);
2363 rust_lex_int_test (&parser, "0x1_f", 0x1f, INTEGER);
2364 rust_lex_int_test (&parser, "0b1_101011__", 0x6b, INTEGER);
2365 rust_lex_int_test (&parser, "0o001177i64", 639, INTEGER);
2366 rust_lex_int_test (&parser, "0x123456789u64", 0x123456789ull, INTEGER);
2367
2368 rust_lex_test_trailing_dot (&parser);
2369
2370 rust_lex_test_one (&parser, "23.", FLOAT);
2371 rust_lex_test_one (&parser, "23.99f32", FLOAT);
2372 rust_lex_test_one (&parser, "23e7", FLOAT);
2373 rust_lex_test_one (&parser, "23E-7", FLOAT);
2374 rust_lex_test_one (&parser, "23e+7", FLOAT);
2375 rust_lex_test_one (&parser, "23.99e+7f64", FLOAT);
2376 rust_lex_test_one (&parser, "23.82f32", FLOAT);
2377
2378 rust_lex_stringish_test (&parser, "hibob", "hibob", IDENT);
2379 rust_lex_stringish_test (&parser, "hibob__93", "hibob__93", IDENT);
2380 rust_lex_stringish_test (&parser, "thread", "thread", IDENT);
2381 rust_lex_stringish_test (&parser, "r#true", "true", IDENT);
2382
2383 const int expected1[] = { IDENT, DECIMAL_INTEGER, 0 };
2384 rust_lex_test_sequence (&parser, "r#thread 23", ARRAY_SIZE (expected1),
2385 expected1);
2386 const int expected2[] = { IDENT, '#', 0 };
2387 rust_lex_test_sequence (&parser, "r#", ARRAY_SIZE (expected2), expected2);
2388
2389 rust_lex_stringish_test (&parser, "\"string\"", "string", STRING);
2390 rust_lex_stringish_test (&parser, "\"str\\ting\"", "str\ting", STRING);
2391 rust_lex_stringish_test (&parser, "\"str\\\"ing\"", "str\"ing", STRING);
2392 rust_lex_stringish_test (&parser, "r\"str\\ing\"", "str\\ing", STRING);
2393 rust_lex_stringish_test (&parser, "r#\"str\\ting\"#", "str\\ting", STRING);
2394 rust_lex_stringish_test (&parser, "r###\"str\\\"ing\"###", "str\\\"ing",
2395 STRING);
2396
2397 rust_lex_stringish_test (&parser, "b\"string\"", "string", BYTESTRING);
2398 rust_lex_stringish_test (&parser, "b\"\x73tring\"", "string", BYTESTRING);
2399 rust_lex_stringish_test (&parser, "b\"str\\\"ing\"", "str\"ing", BYTESTRING);
2400 rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring",
2401 BYTESTRING);
2402
2403 for (const auto &candidate : identifier_tokens)
2404 rust_lex_test_one (&parser, candidate.name, candidate.value);
2405
2406 for (const auto &candidate : operator_tokens)
2407 rust_lex_test_one (&parser, candidate.name, candidate.value);
2408
2409 rust_lex_test_completion (&parser);
2410 rust_lex_test_push_back (&parser);
2411 }
2412
2413 #endif /* GDB_SELF_TEST */
2414
2415
2416
2418 void _initialize_rust_exp ();
2419 void
2420 _initialize_rust_exp ()
2421 {
2422 int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
2423 /* If the regular expression was incorrect, it was a programming
2424 error. */
2425 gdb_assert (code == 0);
2426
2427 #if GDB_SELF_TEST
2428 selftests::register_test ("rust-lex", rust_lex_tests);
2429 #endif
2430 }
2431