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