1 /* Rust expression parsing for GDB, the GNU debugger.
3 Copyright (C) 2016-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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"
37 /* A regular expression for matching Rust numbers. This is split up
38 since it is very long and this gives us a way to comment the
41 static const char number_regex_text
[] =
42 /* subexpression 1: allows use of alternation, otherwise uninteresting */
44 /* First comes floating point. */
45 /* Recognize number after the decimal point, with optional
46 exponent and optional type suffix.
47 subexpression 2: allows "?", otherwise uninteresting
48 subexpression 3: if present, type suffix
50 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
53 /* Recognize exponent without decimal point, with optional type
55 subexpression 4: if present, type suffix
58 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
60 /* "23." is a valid floating point number, but "23.e5" and
61 "23.f32" are not. So, handle the trailing-. case
65 /* Finally come integers.
66 subexpression 5: text of integer
67 subexpression 6: if present, type suffix
68 subexpression 7: allows use of alternation, otherwise uninteresting
72 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
73 "([iu](size|8|16|32|64|128))?"
75 /* The number of subexpressions to allocate space for, including the
76 "0th" whole match subexpression. */
77 #define NUM_SUBEXPRESSIONS 8
79 /* The compiled number-matching regex. */
81 static regex_t number_regex
;
83 /* The kinds of tokens. Note that single-character tokens are
84 represented by themselves, so for instance '[' is a token. */
87 /* Make sure to start after any ASCII character. */
111 /* Operator tokens. */
126 /* A typed integer constant. */
134 /* A typed floating point constant. */
136 struct typed_val_float
142 /* A struct of this type is used to describe a token. */
148 enum exp_opcode opcode
;
151 /* Identifier tokens. */
153 static const struct token_info identifier_tokens
[] =
155 { "as", KW_AS
, OP_NULL
},
156 { "false", KW_FALSE
, OP_NULL
},
157 { "if", 0, OP_NULL
},
158 { "mut", KW_MUT
, OP_NULL
},
159 { "const", KW_CONST
, OP_NULL
},
160 { "self", KW_SELF
, OP_NULL
},
161 { "super", KW_SUPER
, OP_NULL
},
162 { "true", KW_TRUE
, OP_NULL
},
163 { "extern", KW_EXTERN
, OP_NULL
},
164 { "fn", KW_FN
, OP_NULL
},
165 { "sizeof", KW_SIZEOF
, OP_NULL
},
168 /* Operator tokens, sorted longest first. */
170 static const struct token_info operator_tokens
[] =
172 { ">>=", COMPOUND_ASSIGN
, BINOP_RSH
},
173 { "<<=", COMPOUND_ASSIGN
, BINOP_LSH
},
175 { "<<", LSH
, OP_NULL
},
176 { ">>", RSH
, OP_NULL
},
177 { "&&", ANDAND
, OP_NULL
},
178 { "||", OROR
, OP_NULL
},
179 { "==", EQEQ
, OP_NULL
},
180 { "!=", NOTEQ
, OP_NULL
},
181 { "<=", LTEQ
, OP_NULL
},
182 { ">=", GTEQ
, OP_NULL
},
183 { "+=", COMPOUND_ASSIGN
, BINOP_ADD
},
184 { "-=", COMPOUND_ASSIGN
, BINOP_SUB
},
185 { "*=", COMPOUND_ASSIGN
, BINOP_MUL
},
186 { "/=", COMPOUND_ASSIGN
, BINOP_DIV
},
187 { "%=", COMPOUND_ASSIGN
, BINOP_REM
},
188 { "&=", COMPOUND_ASSIGN
, BINOP_BITWISE_AND
},
189 { "|=", COMPOUND_ASSIGN
, BINOP_BITWISE_IOR
},
190 { "^=", COMPOUND_ASSIGN
, BINOP_BITWISE_XOR
},
191 { "..=", DOTDOTEQ
, OP_NULL
},
193 { "::", COLONCOLON
, OP_NULL
},
194 { "..", DOTDOT
, OP_NULL
},
195 { "->", ARROW
, OP_NULL
}
198 /* An instance of this is created before parsing, and destroyed when
199 parsing is finished. */
203 explicit rust_parser (struct parser_state
*state
)
208 DISABLE_COPY_AND_ASSIGN (rust_parser
);
210 /* Return the parser's language. */
211 const struct language_defn
*language () const
213 return pstate
->language ();
216 /* Return the parser's gdbarch. */
217 struct gdbarch
*arch () const
219 return pstate
->gdbarch ();
222 /* A helper to look up a Rust type, or fail. This only works for
223 types defined by rust_language_arch_info. */
225 struct type
*get_type (const char *name
)
229 type
= language_lookup_primitive_type (language (), arch (), name
);
231 error (_("Could not find Rust type %s"), name
);
235 std::string
crate_name (const std::string
&name
);
236 std::string
super_name (const std::string
&ident
, unsigned int n_supers
);
238 int lex_character ();
241 int lex_identifier ();
242 uint32_t lex_hex (int min
, int max
);
243 uint32_t lex_escape (int is_byte
);
245 int lex_one_token ();
246 void push_back (char c
);
248 /* The main interface to lexing. Lexes one token and updates the
252 current_token
= lex_one_token ();
255 /* Assuming the current token is TYPE, lex the next token. */
256 void assume (int type
)
258 gdb_assert (current_token
== type
);
262 /* Require the single-character token C, and lex the next token; or
263 throw an exception. */
264 void require (char type
)
266 if (current_token
!= type
)
267 error (_("'%c' expected"), type
);
271 /* Entry point for all parsing. */
272 operation_up
parse_entry_point ()
275 operation_up result
= parse_expr ();
276 if (current_token
!= 0)
277 error (_("Syntax error near '%s'"), pstate
->prev_lexptr
);
281 operation_up
parse_tuple ();
282 operation_up
parse_array ();
283 operation_up
name_to_operation (const std::string
&name
);
284 operation_up
parse_struct_expr (struct type
*type
);
285 operation_up
parse_binop (bool required
);
286 operation_up
parse_range ();
287 operation_up
parse_expr ();
288 operation_up
parse_sizeof ();
289 operation_up
parse_addr ();
290 operation_up
parse_field (operation_up
&&);
291 operation_up
parse_index (operation_up
&&);
292 std::vector
<operation_up
> parse_paren_args ();
293 operation_up
parse_call (operation_up
&&);
294 std::vector
<struct type
*> parse_type_list ();
295 std::vector
<struct type
*> parse_maybe_type_list ();
296 struct type
*parse_array_type ();
297 struct type
*parse_slice_type ();
298 struct type
*parse_pointer_type ();
299 struct type
*parse_function_type ();
300 struct type
*parse_tuple_type ();
301 struct type
*parse_type ();
302 std::string
parse_path (bool for_expr
);
303 operation_up
parse_string ();
304 operation_up
parse_tuple_struct (struct type
*type
);
305 operation_up
parse_path_expr ();
306 operation_up
parse_atom (bool required
);
308 void update_innermost_block (struct block_symbol sym
);
309 struct block_symbol
lookup_symbol (const char *name
,
310 const struct block
*block
,
311 const domain_enum domain
);
312 struct type
*rust_lookup_type (const char *name
);
314 /* Clear some state. This is only used for testing. */
316 void reset (const char *input
)
318 pstate
->prev_lexptr
= nullptr;
319 pstate
->lexptr
= input
;
322 current_int_val
= {};
323 current_float_val
= {};
324 current_string_val
= {};
325 current_opcode
= OP_NULL
;
327 #endif /* GDB_SELF_TEST */
329 /* Return the token's string value as a string. */
330 std::string
get_string () const
332 return std::string (current_string_val
.ptr
, current_string_val
.length
);
335 /* A pointer to this is installed globally. */
336 auto_obstack obstack
;
338 /* The parser state gdb gave us. */
339 struct parser_state
*pstate
;
341 /* Depth of parentheses. */
344 /* The current token's type. */
345 int current_token
= 0;
346 /* The current token's payload, if any. */
347 typed_val_int current_int_val
{};
348 typed_val_float current_float_val
{};
349 struct stoken current_string_val
{};
350 enum exp_opcode current_opcode
= OP_NULL
;
352 /* When completing, this may be set to the field operation to
354 operation_up completion_op
;
357 /* Return an string referring to NAME, but relative to the crate's
361 rust_parser::crate_name (const std::string
&name
)
363 std::string crate
= rust_crate_for_block (pstate
->expression_context_block
);
366 error (_("Could not find crate for current location"));
367 return "::" + crate
+ "::" + name
;
370 /* Return a string referring to a "super::" qualified name. IDENT is
371 the base name and N_SUPERS is how many "super::"s were provided.
372 N_SUPERS can be zero. */
375 rust_parser::super_name (const std::string
&ident
, unsigned int n_supers
)
377 const char *scope
= "";
378 if (pstate
->expression_context_block
!= nullptr)
379 scope
= pstate
->expression_context_block
->scope ();
382 if (scope
[0] == '\0')
383 error (_("Couldn't find namespace scope for self::"));
388 std::vector
<int> offsets
;
389 unsigned int current_len
;
391 current_len
= cp_find_first_component (scope
);
392 while (scope
[current_len
] != '\0')
394 offsets
.push_back (current_len
);
395 gdb_assert (scope
[current_len
] == ':');
398 current_len
+= cp_find_first_component (scope
402 len
= offsets
.size ();
404 error (_("Too many super:: uses from '%s'"), scope
);
406 offset
= offsets
[len
- n_supers
];
409 offset
= strlen (scope
);
411 return "::" + std::string (scope
, offset
) + "::" + ident
;
414 /* A helper to appropriately munge NAME and BLOCK depending on the
415 presence of a leading "::". */
418 munge_name_and_block (const char **name
, const struct block
**block
)
420 /* If it is a global reference, skip the current block in favor of
422 if (startswith (*name
, "::"))
425 *block
= (*block
)->static_block ();
429 /* Like lookup_symbol, but handles Rust namespace conventions, and
430 doesn't require field_of_this_result. */
433 rust_parser::lookup_symbol (const char *name
, const struct block
*block
,
434 const domain_enum domain
)
436 struct block_symbol result
;
438 munge_name_and_block (&name
, &block
);
440 result
= ::lookup_symbol (name
, block
, domain
, NULL
);
441 if (result
.symbol
!= NULL
)
442 update_innermost_block (result
);
446 /* Look up a type, following Rust namespace conventions. */
449 rust_parser::rust_lookup_type (const char *name
)
451 struct block_symbol result
;
454 const struct block
*block
= pstate
->expression_context_block
;
455 munge_name_and_block (&name
, &block
);
457 result
= ::lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
458 if (result
.symbol
!= NULL
)
460 update_innermost_block (result
);
461 return result
.symbol
->type ();
464 type
= lookup_typename (language (), name
, NULL
, 1);
468 /* Last chance, try a built-in type. */
469 return language_lookup_primitive_type (language (), arch (), name
);
472 /* A helper that updates the innermost block as appropriate. */
475 rust_parser::update_innermost_block (struct block_symbol sym
)
477 if (symbol_read_needs_frame (sym
.symbol
))
478 pstate
->block_tracker
->update (sym
);
481 /* Lex a hex number with at least MIN digits and at most MAX
485 rust_parser::lex_hex (int min
, int max
)
489 /* We only want to stop at MAX if we're lexing a byte escape. */
490 int check_max
= min
== max
;
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')))
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';
503 result
= result
+ pstate
->lexptr
[0] - '0';
509 error (_("Not enough hex digits seen"));
512 gdb_assert (min
!= max
);
513 error (_("Overlong hex escape"));
519 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
520 otherwise we're lexing a character escape. */
523 rust_parser::lex_escape (int is_byte
)
527 gdb_assert (pstate
->lexptr
[0] == '\\');
529 switch (pstate
->lexptr
[0])
533 result
= lex_hex (2, 2);
538 error (_("Unicode escape in byte literal"));
540 if (pstate
->lexptr
[0] != '{')
541 error (_("Missing '{' in Unicode escape"));
543 result
= lex_hex (1, 6);
544 /* Could do range checks here. */
545 if (pstate
->lexptr
[0] != '}')
546 error (_("Missing '}' in Unicode escape"));
580 error (_("Invalid escape \\%c in literal"), pstate
->lexptr
[0]);
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. */
590 lex_multibyte_char (const char *text
, int *len
)
592 /* Only look a maximum of 5 bytes for the closing quote. This is
593 the maximum for UTF-8. */
595 gdb_assert (text
[0] != '\'');
596 for (quote
= 1; text
[quote
] != '\0' && text
[quote
] != '\''; ++quote
)
599 /* The caller will issue an error. */
600 if (text
[quote
] == '\0')
604 convert_between_encodings (host_charset (), HOST_UTF32
,
605 (const gdb_byte
*) text
,
606 quote
, 1, &result
, translit_none
);
608 int size
= obstack_object_size (&result
);
610 error (_("overlong character literal"));
612 memcpy (&value
, obstack_finish (&result
), size
);
616 /* Lex a character constant. */
619 rust_parser::lex_character ()
624 if (pstate
->lexptr
[0] == 'b')
629 gdb_assert (pstate
->lexptr
[0] == '\'');
631 if (pstate
->lexptr
[0] == '\'')
632 error (_("empty character literal"));
633 else if (pstate
->lexptr
[0] == '\\')
634 value
= lex_escape (is_byte
);
638 value
= lex_multibyte_char (&pstate
->lexptr
[0], &len
);
639 pstate
->lexptr
+= len
;
642 if (pstate
->lexptr
[0] != '\'')
643 error (_("Unterminated character literal"));
646 current_int_val
.val
= value
;
647 current_int_val
.type
= get_type (is_byte
? "u8" : "char");
652 /* Return the offset of the double quote if STR looks like the start
653 of a raw string, or 0 if STR does not start a raw string. */
656 starts_raw_string (const char *str
)
658 const char *save
= str
;
663 while (str
[0] == '#')
670 /* Return true if STR looks like the end of a raw string that had N
671 hashes at the start. */
674 ends_raw_string (const char *str
, int n
)
678 gdb_assert (str
[0] == '"');
679 for (i
= 0; i
< n
; ++i
)
680 if (str
[i
+ 1] != '#')
685 /* Lex a string constant. */
688 rust_parser::lex_string ()
690 int is_byte
= pstate
->lexptr
[0] == 'b';
695 raw_length
= starts_raw_string (pstate
->lexptr
);
696 pstate
->lexptr
+= raw_length
;
697 gdb_assert (pstate
->lexptr
[0] == '"');
706 if (pstate
->lexptr
[0] == '"' && ends_raw_string (pstate
->lexptr
,
709 /* Exit with lexptr pointing after the final "#". */
710 pstate
->lexptr
+= raw_length
;
713 else if (pstate
->lexptr
[0] == '\0')
714 error (_("Unexpected EOF in string"));
716 value
= pstate
->lexptr
[0] & 0xff;
717 if (is_byte
&& value
> 127)
718 error (_("Non-ASCII value in raw byte string"));
719 obstack_1grow (&obstack
, value
);
723 else if (pstate
->lexptr
[0] == '"')
725 /* Make sure to skip the quote. */
729 else if (pstate
->lexptr
[0] == '\\')
731 value
= lex_escape (is_byte
);
734 obstack_1grow (&obstack
, value
);
736 convert_between_encodings (HOST_UTF32
, "UTF-8",
738 sizeof (value
), sizeof (value
),
739 &obstack
, translit_none
);
741 else if (pstate
->lexptr
[0] == '\0')
742 error (_("Unexpected EOF in string"));
745 value
= pstate
->lexptr
[0] & 0xff;
746 if (is_byte
&& value
> 127)
747 error (_("Non-ASCII value in byte string"));
748 obstack_1grow (&obstack
, value
);
753 current_string_val
.length
= obstack_object_size (&obstack
);
754 current_string_val
.ptr
= (const char *) obstack_finish (&obstack
);
755 return is_byte
? BYTESTRING
: STRING
;
758 /* Return true if STRING starts with whitespace followed by a digit. */
761 space_then_number (const char *string
)
763 const char *p
= string
;
765 while (p
[0] == ' ' || p
[0] == '\t')
770 return *p
>= '0' && *p
<= '9';
773 /* Return true if C can start an identifier. */
776 rust_identifier_start_p (char c
)
778 return ((c
>= 'a' && c
<= 'z')
779 || (c
>= 'A' && c
<= 'Z')
782 /* Allow any non-ASCII character as an identifier. There
783 doesn't seem to be a need to be picky about this. */
787 /* Lex an identifier. */
790 rust_parser::lex_identifier ()
793 const struct token_info
*token
;
794 int is_gdb_var
= pstate
->lexptr
[0] == '$';
797 if (pstate
->lexptr
[0] == 'r'
798 && pstate
->lexptr
[1] == '#'
799 && rust_identifier_start_p (pstate
->lexptr
[2]))
805 const char *start
= pstate
->lexptr
;
806 gdb_assert (rust_identifier_start_p (pstate
->lexptr
[0]));
810 /* Allow any non-ASCII character here. This "handles" UTF-8 by
811 passing it through. */
812 while ((pstate
->lexptr
[0] >= 'a' && pstate
->lexptr
[0] <= 'z')
813 || (pstate
->lexptr
[0] >= 'A' && pstate
->lexptr
[0] <= 'Z')
814 || pstate
->lexptr
[0] == '_'
815 || (is_gdb_var
&& pstate
->lexptr
[0] == '$')
816 || (pstate
->lexptr
[0] >= '0' && pstate
->lexptr
[0] <= '9')
817 || (pstate
->lexptr
[0] & 0x80) != 0)
821 length
= pstate
->lexptr
- start
;
825 for (const auto &candidate
: identifier_tokens
)
827 if (length
== strlen (candidate
.name
)
828 && strncmp (candidate
.name
, start
, length
) == 0)
838 if (token
->value
== 0)
840 /* Leave the terminating token alone. */
841 pstate
->lexptr
= start
;
845 else if (token
== NULL
847 && (strncmp (start
, "thread", length
) == 0
848 || strncmp (start
, "task", length
) == 0)
849 && space_then_number (pstate
->lexptr
))
851 /* "task" or "thread" followed by a number terminates the
852 parse, per gdb rules. */
853 pstate
->lexptr
= start
;
857 if (token
== NULL
|| (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0'))
859 current_string_val
.length
= length
;
860 current_string_val
.ptr
= start
;
863 if (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0')
865 /* Prevent rustyylex from returning two COMPLETE tokens. */
866 pstate
->prev_lexptr
= pstate
->lexptr
;
877 /* Lex an operator. */
880 rust_parser::lex_operator ()
882 const struct token_info
*token
= NULL
;
884 for (const auto &candidate
: operator_tokens
)
886 if (strncmp (candidate
.name
, pstate
->lexptr
,
887 strlen (candidate
.name
)) == 0)
889 pstate
->lexptr
+= strlen (candidate
.name
);
897 current_opcode
= token
->opcode
;
901 return *pstate
->lexptr
++;
907 rust_parser::lex_number ()
909 regmatch_t subexps
[NUM_SUBEXPRESSIONS
];
912 int could_be_decimal
= 1;
913 int implicit_i32
= 0;
914 const char *type_name
= NULL
;
920 match
= regexec (&number_regex
, pstate
->lexptr
, ARRAY_SIZE (subexps
),
922 /* Failure means the regexp is broken. */
923 gdb_assert (match
== 0);
925 if (subexps
[INT_TEXT
].rm_so
!= -1)
927 /* Integer part matched. */
929 end_index
= subexps
[INT_TEXT
].rm_eo
;
930 if (subexps
[INT_TYPE
].rm_so
== -1)
937 type_index
= INT_TYPE
;
938 could_be_decimal
= 0;
941 else if (subexps
[FLOAT_TYPE1
].rm_so
!= -1)
943 /* Found floating point type suffix. */
944 end_index
= subexps
[FLOAT_TYPE1
].rm_so
;
945 type_index
= FLOAT_TYPE1
;
947 else if (subexps
[FLOAT_TYPE2
].rm_so
!= -1)
949 /* Found floating point type suffix. */
950 end_index
= subexps
[FLOAT_TYPE2
].rm_so
;
951 type_index
= FLOAT_TYPE2
;
955 /* Any other floating point match. */
956 end_index
= subexps
[0].rm_eo
;
960 /* We need a special case if the final character is ".". In this
961 case we might need to parse an integer. For example, "23.f()" is
962 a request for a trait method call, not a syntax error involving
963 the floating point number "23.". */
964 gdb_assert (subexps
[0].rm_eo
> 0);
965 if (pstate
->lexptr
[subexps
[0].rm_eo
- 1] == '.')
967 const char *next
= skip_spaces (&pstate
->lexptr
[subexps
[0].rm_eo
]);
969 if (rust_identifier_start_p (*next
) || *next
== '.')
973 end_index
= subexps
[0].rm_eo
;
975 could_be_decimal
= 1;
980 /* Compute the type name if we haven't already. */
981 std::string type_name_holder
;
982 if (type_name
== NULL
)
984 gdb_assert (type_index
!= -1);
985 type_name_holder
= std::string ((pstate
->lexptr
986 + subexps
[type_index
].rm_so
),
987 (subexps
[type_index
].rm_eo
988 - subexps
[type_index
].rm_so
));
989 type_name
= type_name_holder
.c_str ();
992 /* Look up the type. */
993 type
= get_type (type_name
);
995 /* Copy the text of the number and remove the "_"s. */
997 for (i
= 0; i
< end_index
&& pstate
->lexptr
[i
]; ++i
)
999 if (pstate
->lexptr
[i
] == '_')
1000 could_be_decimal
= 0;
1002 number
.push_back (pstate
->lexptr
[i
]);
1005 /* Advance past the match. */
1006 pstate
->lexptr
+= subexps
[0].rm_eo
;
1008 /* Parse the number. */
1014 if (number
[0] == '0')
1016 if (number
[1] == 'x')
1018 else if (number
[1] == 'o')
1020 else if (number
[1] == 'b')
1025 could_be_decimal
= 0;
1029 if (!current_int_val
.val
.set (number
.c_str () + offset
, radix
))
1031 /* Shouldn't be possible. */
1032 error (_("Invalid integer"));
1036 static gdb_mpz sixty_three_bit
= gdb_mpz::pow (2, 63);
1037 static gdb_mpz thirty_one_bit
= gdb_mpz::pow (2, 31);
1039 if (current_int_val
.val
>= sixty_three_bit
)
1040 type
= get_type ("i128");
1041 else if (current_int_val
.val
>= thirty_one_bit
)
1042 type
= get_type ("i64");
1045 current_int_val
.type
= type
;
1049 current_float_val
.type
= type
;
1050 bool parsed
= parse_float (number
.c_str (), number
.length (),
1051 current_float_val
.type
,
1052 current_float_val
.val
.data ());
1053 gdb_assert (parsed
);
1056 return is_integer
? (could_be_decimal
? DECIMAL_INTEGER
: INTEGER
) : FLOAT
;
1062 rust_parser::lex_one_token ()
1064 /* Skip all leading whitespace. */
1065 while (pstate
->lexptr
[0] == ' '
1066 || pstate
->lexptr
[0] == '\t'
1067 || pstate
->lexptr
[0] == '\r'
1068 || pstate
->lexptr
[0] == '\n')
1071 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1072 we're completing an empty string at the end of a field_expr.
1073 But, we don't want to return two COMPLETE tokens in a row. */
1074 if (pstate
->lexptr
[0] == '\0' && pstate
->lexptr
== pstate
->prev_lexptr
)
1076 pstate
->prev_lexptr
= pstate
->lexptr
;
1077 if (pstate
->lexptr
[0] == '\0')
1079 if (pstate
->parse_completion
)
1081 current_string_val
.length
=0;
1082 current_string_val
.ptr
= "";
1088 if (pstate
->lexptr
[0] >= '0' && pstate
->lexptr
[0] <= '9')
1089 return lex_number ();
1090 else if (pstate
->lexptr
[0] == 'b' && pstate
->lexptr
[1] == '\'')
1091 return lex_character ();
1092 else if (pstate
->lexptr
[0] == 'b' && pstate
->lexptr
[1] == '"')
1093 return lex_string ();
1094 else if (pstate
->lexptr
[0] == 'b' && starts_raw_string (pstate
->lexptr
+ 1))
1095 return lex_string ();
1096 else if (starts_raw_string (pstate
->lexptr
))
1097 return lex_string ();
1098 else if (rust_identifier_start_p (pstate
->lexptr
[0]))
1099 return lex_identifier ();
1100 else if (pstate
->lexptr
[0] == '"')
1101 return lex_string ();
1102 else if (pstate
->lexptr
[0] == '\'')
1103 return lex_character ();
1104 else if (pstate
->lexptr
[0] == '}' || pstate
->lexptr
[0] == ']')
1106 /* Falls through to lex_operator. */
1109 else if (pstate
->lexptr
[0] == '(' || pstate
->lexptr
[0] == '{')
1111 /* Falls through to lex_operator. */
1114 else if (pstate
->lexptr
[0] == ',' && pstate
->comma_terminates
1115 && paren_depth
== 0)
1118 return lex_operator ();
1121 /* Push back a single character to be re-lexed. */
1124 rust_parser::push_back (char c
)
1126 /* Can't be called before any lexing. */
1127 gdb_assert (pstate
->prev_lexptr
!= NULL
);
1130 gdb_assert (*pstate
->lexptr
== c
);
1135 /* Parse a tuple or paren expression. */
1138 rust_parser::parse_tuple ()
1142 if (current_token
== ')')
1145 struct type
*unit
= get_type ("()");
1146 return make_operation
<long_const_operation
> (unit
, 0);
1149 operation_up expr
= parse_expr ();
1150 if (current_token
== ')')
1152 /* Parenthesized expression. */
1154 return make_operation
<rust_parenthesized_operation
> (std::move (expr
));
1157 std::vector
<operation_up
> ops
;
1158 ops
.push_back (std::move (expr
));
1159 while (current_token
!= ')')
1161 if (current_token
!= ',')
1162 error (_("',' or ')' expected"));
1165 /* A trailing "," is ok. */
1166 if (current_token
!= ')')
1167 ops
.push_back (parse_expr ());
1172 error (_("Tuple expressions not supported yet"));
1175 /* Parse an array expression. */
1178 rust_parser::parse_array ()
1182 if (current_token
== KW_MUT
)
1185 operation_up result
;
1186 operation_up expr
= parse_expr ();
1187 if (current_token
== ';')
1190 operation_up rhs
= parse_expr ();
1191 result
= make_operation
<rust_array_operation
> (std::move (expr
),
1194 else if (current_token
== ',' || current_token
== ']')
1196 std::vector
<operation_up
> ops
;
1197 ops
.push_back (std::move (expr
));
1198 while (current_token
!= ']')
1200 if (current_token
!= ',')
1201 error (_("',' or ']' expected"));
1203 ops
.push_back (parse_expr ());
1205 ops
.shrink_to_fit ();
1206 int len
= ops
.size () - 1;
1207 result
= make_operation
<array_operation
> (0, len
, std::move (ops
));
1210 error (_("',', ';', or ']' expected"));
1217 /* Turn a name into an operation. */
1220 rust_parser::name_to_operation (const std::string
&name
)
1222 struct block_symbol sym
= lookup_symbol (name
.c_str (),
1223 pstate
->expression_context_block
,
1225 if (sym
.symbol
!= nullptr && sym
.symbol
->aclass () != LOC_TYPEDEF
)
1226 return make_operation
<var_value_operation
> (sym
);
1228 struct type
*type
= nullptr;
1230 if (sym
.symbol
!= nullptr)
1232 gdb_assert (sym
.symbol
->aclass () == LOC_TYPEDEF
);
1233 type
= sym
.symbol
->type ();
1235 if (type
== nullptr)
1236 type
= rust_lookup_type (name
.c_str ());
1237 if (type
== nullptr)
1238 error (_("No symbol '%s' in current context"), name
.c_str ());
1240 if (type
->code () == TYPE_CODE_STRUCT
&& type
->num_fields () == 0)
1242 /* A unit-like struct. */
1243 operation_up
result (new rust_aggregate_operation (type
, {}, {}));
1247 return make_operation
<type_operation
> (type
);
1250 /* Parse a struct expression. */
1253 rust_parser::parse_struct_expr (struct type
*type
)
1257 if (type
->code () != TYPE_CODE_STRUCT
1258 || rust_tuple_type_p (type
)
1259 || rust_tuple_struct_type_p (type
))
1260 error (_("Struct expression applied to non-struct type"));
1262 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1263 while (current_token
!= '}' && current_token
!= DOTDOT
)
1265 if (current_token
!= IDENT
)
1266 error (_("'}', '..', or identifier expected"));
1268 std::string name
= get_string ();
1272 if (current_token
== ',' || current_token
== '}'
1273 || current_token
== DOTDOT
)
1274 expr
= name_to_operation (name
);
1278 expr
= parse_expr ();
1280 field_v
.emplace_back (std::move (name
), std::move (expr
));
1282 /* A trailing "," is ok. */
1283 if (current_token
== ',')
1287 operation_up others
;
1288 if (current_token
== DOTDOT
)
1291 others
= parse_expr ();
1296 return make_operation
<rust_aggregate_operation
> (type
,
1298 std::move (field_v
));
1301 /* Used by the operator precedence parser. */
1304 rustop_item (int token_
, int precedence_
, enum exp_opcode opcode_
,
1307 precedence (precedence_
),
1309 op (std::move (op_
))
1313 /* The token value. */
1315 /* Precedence of this operator. */
1317 /* This is used only for assign-modify. */
1318 enum exp_opcode opcode
;
1319 /* The right hand side of this operation. */
1323 /* An operator precedence parser for binary operations, including
1327 rust_parser::parse_binop (bool required
)
1329 /* All the binary operators. Each one is of the form
1330 OPERATION(TOKEN, PRECEDENCE, TYPE)
1331 TOKEN is the corresponding operator token.
1332 PRECEDENCE is a value indicating relative precedence.
1333 TYPE is the operation type corresponding to the operator.
1334 Assignment operations are handled specially, not via this
1335 table; they have precedence 0. */
1337 OPERATION ('*', 10, mul_operation) \
1338 OPERATION ('/', 10, div_operation) \
1339 OPERATION ('%', 10, rem_operation) \
1340 OPERATION ('@', 9, repeat_operation) \
1341 OPERATION ('+', 8, add_operation) \
1342 OPERATION ('-', 8, sub_operation) \
1343 OPERATION (LSH, 7, lsh_operation) \
1344 OPERATION (RSH, 7, rsh_operation) \
1345 OPERATION ('&', 6, bitwise_and_operation) \
1346 OPERATION ('^', 5, bitwise_xor_operation) \
1347 OPERATION ('|', 4, bitwise_ior_operation) \
1348 OPERATION (EQEQ, 3, equal_operation) \
1349 OPERATION (NOTEQ, 3, notequal_operation) \
1350 OPERATION ('<', 3, less_operation) \
1351 OPERATION (LTEQ, 3, leq_operation) \
1352 OPERATION ('>', 3, gtr_operation) \
1353 OPERATION (GTEQ, 3, geq_operation) \
1354 OPERATION (ANDAND, 2, logical_and_operation) \
1355 OPERATION (OROR, 1, logical_or_operation)
1357 #define ASSIGN_PREC 0
1359 operation_up start
= parse_atom (required
);
1360 if (start
== nullptr)
1362 gdb_assert (!required
);
1366 std::vector
<rustop_item
> operator_stack
;
1367 operator_stack
.emplace_back (0, -1, OP_NULL
, std::move (start
));
1371 int this_token
= current_token
;
1372 enum exp_opcode compound_assign_op
= OP_NULL
;
1373 int precedence
= -2;
1377 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1379 precedence = PRECEDENCE; \
1387 case COMPOUND_ASSIGN
:
1388 compound_assign_op
= current_opcode
;
1391 precedence
= ASSIGN_PREC
;
1395 /* "as" must be handled specially. */
1399 rustop_item
&lhs
= operator_stack
.back ();
1400 struct type
*type
= parse_type ();
1401 lhs
.op
= make_operation
<unop_cast_operation
> (std::move (lhs
.op
),
1404 /* Bypass the rest of the loop. */
1408 /* Arrange to pop the entire stack. */
1413 /* Make sure that assignments are right-associative while other
1414 operations are left-associative. */
1415 while ((precedence
== ASSIGN_PREC
1416 ? precedence
< operator_stack
.back ().precedence
1417 : precedence
<= operator_stack
.back ().precedence
)
1418 && operator_stack
.size () > 1)
1420 rustop_item rhs
= std::move (operator_stack
.back ());
1421 operator_stack
.pop_back ();
1423 rustop_item
&lhs
= operator_stack
.back ();
1427 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1429 lhs.op = make_operation<TYPE> (std::move (lhs.op), \
1430 std::move (rhs.op)); \
1438 case COMPOUND_ASSIGN
:
1440 if (rhs
.token
== '=')
1441 lhs
.op
= (make_operation
<assign_operation
>
1442 (std::move (lhs
.op
), std::move (rhs
.op
)));
1444 lhs
.op
= (make_operation
<assign_modify_operation
>
1445 (rhs
.opcode
, std::move (lhs
.op
),
1446 std::move (rhs
.op
)));
1448 struct type
*unit_type
= get_type ("()");
1450 operation_up
nil (new long_const_operation (unit_type
, 0));
1451 lhs
.op
= (make_operation
<comma_operation
>
1452 (std::move (lhs
.op
), std::move (nil
)));
1457 gdb_assert_not_reached ("bad binary operator");
1461 if (precedence
== -2)
1464 operator_stack
.emplace_back (this_token
, precedence
, compound_assign_op
,
1468 gdb_assert (operator_stack
.size () == 1);
1469 return std::move (operator_stack
[0].op
);
1473 /* Parse a range expression. */
1476 rust_parser::parse_range ()
1478 enum range_flag kind
= (RANGE_HIGH_BOUND_DEFAULT
1479 | RANGE_LOW_BOUND_DEFAULT
);
1482 if (current_token
!= DOTDOT
&& current_token
!= DOTDOTEQ
)
1484 lhs
= parse_binop (true);
1485 kind
&= ~RANGE_LOW_BOUND_DEFAULT
;
1488 if (current_token
== DOTDOT
)
1489 kind
|= RANGE_HIGH_BOUND_EXCLUSIVE
;
1490 else if (current_token
!= DOTDOTEQ
)
1494 /* A "..=" range requires a high bound, but otherwise it is
1496 operation_up rhs
= parse_binop ((kind
& RANGE_HIGH_BOUND_EXCLUSIVE
) == 0);
1498 kind
&= ~RANGE_HIGH_BOUND_DEFAULT
;
1500 return make_operation
<rust_range_operation
> (kind
,
1505 /* Parse an expression. */
1508 rust_parser::parse_expr ()
1510 return parse_range ();
1513 /* Parse a sizeof expression. */
1516 rust_parser::parse_sizeof ()
1521 operation_up result
= make_operation
<unop_sizeof_operation
> (parse_expr ());
1526 /* Parse an address-of operation. */
1529 rust_parser::parse_addr ()
1533 if (current_token
== KW_MUT
)
1536 return make_operation
<rust_unop_addr_operation
> (parse_atom (true));
1539 /* Parse a field expression. */
1542 rust_parser::parse_field (operation_up
&&lhs
)
1546 operation_up result
;
1547 switch (current_token
)
1552 bool is_complete
= current_token
== COMPLETE
;
1553 auto struct_op
= new rust_structop (std::move (lhs
), get_string ());
1557 completion_op
.reset (struct_op
);
1558 pstate
->mark_struct_expression (struct_op
);
1559 /* Throw to the outermost level of the parser. */
1560 error (_("not really an error"));
1562 result
.reset (struct_op
);
1566 case DECIMAL_INTEGER
:
1568 int idx
= current_int_val
.val
.as_integer
<int> ();
1569 result
= make_operation
<rust_struct_anon
> (idx
, std::move (lhs
));
1575 error (_("'_' not allowed in integers in anonymous field references"));
1578 error (_("field name expected"));
1584 /* Parse an index expression. */
1587 rust_parser::parse_index (operation_up
&&lhs
)
1590 operation_up rhs
= parse_expr ();
1593 return make_operation
<rust_subscript_operation
> (std::move (lhs
),
1597 /* Parse a sequence of comma-separated expressions in parens. */
1599 std::vector
<operation_up
>
1600 rust_parser::parse_paren_args ()
1604 std::vector
<operation_up
> args
;
1605 while (current_token
!= ')')
1609 if (current_token
!= ',')
1610 error (_("',' or ')' expected"));
1614 args
.push_back (parse_expr ());
1622 /* Parse the parenthesized part of a function call. */
1625 rust_parser::parse_call (operation_up
&&lhs
)
1627 std::vector
<operation_up
> args
= parse_paren_args ();
1629 return make_operation
<funcall_operation
> (std::move (lhs
),
1633 /* Parse a list of types. */
1635 std::vector
<struct type
*>
1636 rust_parser::parse_type_list ()
1638 std::vector
<struct type
*> result
;
1639 result
.push_back (parse_type ());
1640 while (current_token
== ',')
1643 result
.push_back (parse_type ());
1648 /* Parse a possibly-empty list of types, surrounded in parens. */
1650 std::vector
<struct type
*>
1651 rust_parser::parse_maybe_type_list ()
1654 std::vector
<struct type
*> types
;
1655 if (current_token
!= ')')
1656 types
= parse_type_list ();
1661 /* Parse an array type. */
1664 rust_parser::parse_array_type ()
1667 struct type
*elt_type
= parse_type ();
1670 if (current_token
!= INTEGER
&& current_token
!= DECIMAL_INTEGER
)
1671 error (_("integer expected"));
1672 ULONGEST val
= current_int_val
.val
.as_integer
<ULONGEST
> ();
1676 return lookup_array_range_type (elt_type
, 0, val
- 1);
1679 /* Parse a slice type. */
1682 rust_parser::parse_slice_type ()
1686 /* Handle &str specially. This is an important type in Rust. While
1687 the compiler does emit the "&str" type in the DWARF, just "str"
1688 itself isn't always available -- but it's handy if this works
1690 if (current_token
== IDENT
&& get_string () == "str")
1693 return rust_slice_type ("&str", get_type ("u8"), get_type ("usize"));
1696 bool is_slice
= current_token
== '[';
1700 struct type
*target
= parse_type ();
1705 return rust_slice_type ("&[*gdb*]", target
, get_type ("usize"));
1708 /* For now we treat &x and *x identically. */
1709 return lookup_pointer_type (target
);
1712 /* Parse a pointer type. */
1715 rust_parser::parse_pointer_type ()
1719 if (current_token
== KW_MUT
|| current_token
== KW_CONST
)
1722 struct type
*target
= parse_type ();
1723 /* For the time being we ignore mut/const. */
1724 return lookup_pointer_type (target
);
1727 /* Parse a function type. */
1730 rust_parser::parse_function_type ()
1734 if (current_token
!= '(')
1735 error (_("'(' expected"));
1737 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1739 if (current_token
!= ARROW
)
1740 error (_("'->' expected"));
1743 struct type
*result_type
= parse_type ();
1745 struct type
**argtypes
= nullptr;
1746 if (!types
.empty ())
1747 argtypes
= types
.data ();
1749 result_type
= lookup_function_type_with_arguments (result_type
,
1752 return lookup_pointer_type (result_type
);
1755 /* Parse a tuple type. */
1758 rust_parser::parse_tuple_type ()
1760 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1762 auto_obstack obstack
;
1763 obstack_1grow (&obstack
, '(');
1764 for (int i
= 0; i
< types
.size (); ++i
)
1766 std::string type_name
= type_to_string (types
[i
]);
1769 obstack_1grow (&obstack
, ',');
1770 obstack_grow_str (&obstack
, type_name
.c_str ());
1773 obstack_grow_str0 (&obstack
, ")");
1774 const char *name
= (const char *) obstack_finish (&obstack
);
1776 /* We don't allow creating new tuple types (yet), but we do allow
1777 looking up existing tuple types. */
1778 struct type
*result
= rust_lookup_type (name
);
1779 if (result
== nullptr)
1780 error (_("could not find tuple type '%s'"), name
);
1788 rust_parser::parse_type ()
1790 switch (current_token
)
1793 return parse_array_type ();
1795 return parse_slice_type ();
1797 return parse_pointer_type ();
1799 return parse_function_type ();
1801 return parse_tuple_type ();
1808 std::string path
= parse_path (false);
1809 struct type
*result
= rust_lookup_type (path
.c_str ());
1810 if (result
== nullptr)
1811 error (_("No type name '%s' in current context"), path
.c_str ());
1815 error (_("type expected"));
1822 rust_parser::parse_path (bool for_expr
)
1824 unsigned n_supers
= 0;
1825 int first_token
= current_token
;
1827 switch (current_token
)
1831 if (current_token
!= COLONCOLON
)
1836 while (current_token
== KW_SUPER
)
1840 if (current_token
!= COLONCOLON
)
1841 error (_("'::' expected"));
1851 /* This is a gdb extension to make it possible to refer to items
1852 in other crates. It just bypasses adding the current crate
1853 to the front of the name. */
1858 if (current_token
!= IDENT
)
1859 error (_("identifier expected"));
1860 std::string path
= get_string ();
1861 bool saw_ident
= true;
1864 /* The condition here lets us enter the loop even if we see
1866 while (current_token
== COLONCOLON
|| current_token
== '<')
1868 if (current_token
== COLONCOLON
)
1873 if (current_token
== IDENT
)
1875 path
= path
+ "::" + get_string ();
1879 else if (current_token
== COLONCOLON
)
1881 /* The code below won't detect this scenario. */
1882 error (_("unexpected '::'"));
1886 if (current_token
!= '<')
1889 /* Expression use name::<...>, whereas types use name<...>. */
1892 /* Expressions use "name::<...>", so if we saw an identifier
1893 after the "::", we ignore the "<" here. */
1899 /* Types use "name<...>", so we need to have seen the
1906 std::vector
<struct type
*> types
= parse_type_list ();
1907 if (current_token
== '>')
1909 else if (current_token
== RSH
)
1915 error (_("'>' expected"));
1918 for (int i
= 0; i
< types
.size (); ++i
)
1922 path
+= type_to_string (types
[i
]);
1928 switch (first_token
)
1932 return super_name (path
, n_supers
);
1935 return crate_name (path
);
1944 gdb_assert_not_reached ("missing case in path parsing");
1948 /* Handle the parsing for a string expression. */
1951 rust_parser::parse_string ()
1953 gdb_assert (current_token
== STRING
);
1955 /* Wrap the raw string in the &str struct. */
1956 struct type
*type
= rust_lookup_type ("&str");
1957 if (type
== nullptr)
1958 error (_("Could not find type '&str'"));
1960 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1962 size_t len
= current_string_val
.length
;
1963 operation_up str
= make_operation
<string_operation
> (get_string ());
1965 = make_operation
<rust_unop_addr_operation
> (std::move (str
));
1966 field_v
.emplace_back ("data_ptr", std::move (addr
));
1968 struct type
*valtype
= get_type ("usize");
1969 operation_up lenop
= make_operation
<long_const_operation
> (valtype
, len
);
1970 field_v
.emplace_back ("length", std::move (lenop
));
1972 return make_operation
<rust_aggregate_operation
> (type
,
1974 std::move (field_v
));
1977 /* Parse a tuple struct expression. */
1980 rust_parser::parse_tuple_struct (struct type
*type
)
1982 std::vector
<operation_up
> args
= parse_paren_args ();
1984 std::vector
<std::pair
<std::string
, operation_up
>> field_v (args
.size ());
1985 for (int i
= 0; i
< args
.size (); ++i
)
1986 field_v
[i
] = { string_printf ("__%d", i
), std::move (args
[i
]) };
1988 return (make_operation
<rust_aggregate_operation
>
1989 (type
, operation_up (), std::move (field_v
)));
1992 /* Parse a path expression. */
1995 rust_parser::parse_path_expr ()
1997 std::string path
= parse_path (true);
1999 if (current_token
== '{')
2001 struct type
*type
= rust_lookup_type (path
.c_str ());
2002 if (type
== nullptr)
2003 error (_("Could not find type '%s'"), path
.c_str ());
2005 return parse_struct_expr (type
);
2007 else if (current_token
== '(')
2009 struct type
*type
= rust_lookup_type (path
.c_str ());
2010 /* If this is actually a tuple struct expression, handle it
2011 here. If it is a call, it will be handled elsewhere. */
2012 if (type
!= nullptr)
2014 if (!rust_tuple_struct_type_p (type
))
2015 error (_("Type %s is not a tuple struct"), path
.c_str ());
2016 return parse_tuple_struct (type
);
2020 return name_to_operation (path
);
2023 /* Parse an atom. "Atom" isn't a Rust term, but this refers to a
2024 single unitary item in the grammar; but here including some unary
2025 prefix and postfix expressions. */
2028 rust_parser::parse_atom (bool required
)
2030 operation_up result
;
2032 switch (current_token
)
2035 result
= parse_tuple ();
2039 result
= parse_array ();
2043 case DECIMAL_INTEGER
:
2044 result
= make_operation
<long_const_operation
> (current_int_val
.type
,
2045 current_int_val
.val
);
2050 result
= make_operation
<float_const_operation
> (current_float_val
.type
,
2051 current_float_val
.val
);
2056 result
= parse_string ();
2061 result
= make_operation
<string_operation
> (get_string ());
2067 result
= make_operation
<bool_operation
> (current_token
== KW_TRUE
);
2072 /* This is kind of a hacky approach. */
2074 pstate
->push_dollar (current_string_val
);
2075 result
= pstate
->pop ();
2085 result
= parse_path_expr ();
2090 result
= make_operation
<rust_unop_ind_operation
> (parse_atom (true));
2094 result
= make_operation
<unary_plus_operation
> (parse_atom (true));
2098 result
= make_operation
<unary_neg_operation
> (parse_atom (true));
2102 result
= make_operation
<rust_unop_compl_operation
> (parse_atom (true));
2105 result
= parse_sizeof ();
2108 result
= parse_addr ();
2114 error (_("unexpected token"));
2117 /* Now parse suffixes. */
2120 switch (current_token
)
2123 result
= parse_field (std::move (result
));
2127 result
= parse_index (std::move (result
));
2131 result
= parse_call (std::move (result
));
2142 /* The parser as exposed to gdb. */
2145 rust_language::parser (struct parser_state
*state
) const
2147 rust_parser
parser (state
);
2149 operation_up result
;
2152 result
= parser
.parse_entry_point ();
2154 catch (const gdb_exception
&exc
)
2156 if (state
->parse_completion
)
2158 result
= std::move (parser
.completion_op
);
2159 if (result
== nullptr)
2166 state
->set_operation (std::move (result
));
2175 /* A test helper that lexes a string, expecting a single token. */
2178 rust_lex_test_one (rust_parser
*parser
, const char *input
, int expected
)
2182 parser
->reset (input
);
2184 token
= parser
->lex_one_token ();
2185 SELF_CHECK (token
== expected
);
2189 token
= parser
->lex_one_token ();
2190 SELF_CHECK (token
== 0);
2194 /* Test that INPUT lexes as the integer VALUE. */
2197 rust_lex_int_test (rust_parser
*parser
, const char *input
,
2198 ULONGEST value
, int kind
)
2200 rust_lex_test_one (parser
, input
, kind
);
2201 SELF_CHECK (parser
->current_int_val
.val
== value
);
2204 /* Test that INPUT throws an exception with text ERR. */
2207 rust_lex_exception_test (rust_parser
*parser
, const char *input
,
2212 /* The "kind" doesn't matter. */
2213 rust_lex_test_one (parser
, input
, DECIMAL_INTEGER
);
2216 catch (const gdb_exception_error
&except
)
2218 SELF_CHECK (strcmp (except
.what (), err
) == 0);
2222 /* Test that INPUT lexes as the identifier, string, or byte-string
2223 VALUE. KIND holds the expected token kind. */
2226 rust_lex_stringish_test (rust_parser
*parser
, const char *input
,
2227 const char *value
, int kind
)
2229 rust_lex_test_one (parser
, input
, kind
);
2230 SELF_CHECK (parser
->get_string () == value
);
2233 /* Helper to test that a string parses as a given token sequence. */
2236 rust_lex_test_sequence (rust_parser
*parser
, const char *input
, int len
,
2237 const int expected
[])
2241 parser
->reset (input
);
2243 for (i
= 0; i
< len
; ++i
)
2245 int token
= parser
->lex_one_token ();
2246 SELF_CHECK (token
== expected
[i
]);
2250 /* Tests for an integer-parsing corner case. */
2253 rust_lex_test_trailing_dot (rust_parser
*parser
)
2255 const int expected1
[] = { DECIMAL_INTEGER
, '.', IDENT
, '(', ')', 0 };
2256 const int expected2
[] = { INTEGER
, '.', IDENT
, '(', ')', 0 };
2257 const int expected3
[] = { FLOAT
, EQEQ
, '(', ')', 0 };
2258 const int expected4
[] = { DECIMAL_INTEGER
, DOTDOT
, DECIMAL_INTEGER
, 0 };
2260 rust_lex_test_sequence (parser
, "23.g()", ARRAY_SIZE (expected1
), expected1
);
2261 rust_lex_test_sequence (parser
, "23_0.g()", ARRAY_SIZE (expected2
),
2263 rust_lex_test_sequence (parser
, "23.==()", ARRAY_SIZE (expected3
),
2265 rust_lex_test_sequence (parser
, "23..25", ARRAY_SIZE (expected4
), expected4
);
2268 /* Tests of completion. */
2271 rust_lex_test_completion (rust_parser
*parser
)
2273 const int expected
[] = { IDENT
, '.', COMPLETE
, 0 };
2275 parser
->pstate
->parse_completion
= 1;
2277 rust_lex_test_sequence (parser
, "something.wha", ARRAY_SIZE (expected
),
2279 rust_lex_test_sequence (parser
, "something.", ARRAY_SIZE (expected
),
2282 parser
->pstate
->parse_completion
= 0;
2285 /* Test pushback. */
2288 rust_lex_test_push_back (rust_parser
*parser
)
2292 parser
->reset (">>=");
2294 token
= parser
->lex_one_token ();
2295 SELF_CHECK (token
== COMPOUND_ASSIGN
);
2296 SELF_CHECK (parser
->current_opcode
== BINOP_RSH
);
2298 parser
->push_back ('=');
2300 token
= parser
->lex_one_token ();
2301 SELF_CHECK (token
== '=');
2303 token
= parser
->lex_one_token ();
2304 SELF_CHECK (token
== 0);
2307 /* Unit test the lexer. */
2310 rust_lex_tests (void)
2312 /* Set up dummy "parser", so that rust_type works. */
2313 parser_state
ps (language_def (language_rust
), current_inferior ()->arch (),
2314 nullptr, 0, 0, nullptr, 0, nullptr);
2315 rust_parser
parser (&ps
);
2317 rust_lex_test_one (&parser
, "", 0);
2318 rust_lex_test_one (&parser
, " \t \n \r ", 0);
2319 rust_lex_test_one (&parser
, "thread 23", 0);
2320 rust_lex_test_one (&parser
, "task 23", 0);
2321 rust_lex_test_one (&parser
, "th 104", 0);
2322 rust_lex_test_one (&parser
, "ta 97", 0);
2324 rust_lex_int_test (&parser
, "'z'", 'z', INTEGER
);
2325 rust_lex_int_test (&parser
, "'\\xff'", 0xff, INTEGER
);
2326 rust_lex_int_test (&parser
, "'\\u{1016f}'", 0x1016f, INTEGER
);
2327 rust_lex_int_test (&parser
, "b'z'", 'z', INTEGER
);
2328 rust_lex_int_test (&parser
, "b'\\xfe'", 0xfe, INTEGER
);
2329 rust_lex_int_test (&parser
, "b'\\xFE'", 0xfe, INTEGER
);
2330 rust_lex_int_test (&parser
, "b'\\xfE'", 0xfe, INTEGER
);
2332 /* Test all escapes in both modes. */
2333 rust_lex_int_test (&parser
, "'\\n'", '\n', INTEGER
);
2334 rust_lex_int_test (&parser
, "'\\r'", '\r', INTEGER
);
2335 rust_lex_int_test (&parser
, "'\\t'", '\t', INTEGER
);
2336 rust_lex_int_test (&parser
, "'\\\\'", '\\', INTEGER
);
2337 rust_lex_int_test (&parser
, "'\\0'", '\0', INTEGER
);
2338 rust_lex_int_test (&parser
, "'\\''", '\'', INTEGER
);
2339 rust_lex_int_test (&parser
, "'\\\"'", '"', INTEGER
);
2341 rust_lex_int_test (&parser
, "b'\\n'", '\n', INTEGER
);
2342 rust_lex_int_test (&parser
, "b'\\r'", '\r', INTEGER
);
2343 rust_lex_int_test (&parser
, "b'\\t'", '\t', INTEGER
);
2344 rust_lex_int_test (&parser
, "b'\\\\'", '\\', INTEGER
);
2345 rust_lex_int_test (&parser
, "b'\\0'", '\0', INTEGER
);
2346 rust_lex_int_test (&parser
, "b'\\''", '\'', INTEGER
);
2347 rust_lex_int_test (&parser
, "b'\\\"'", '"', INTEGER
);
2349 rust_lex_exception_test (&parser
, "'z", "Unterminated character literal");
2350 rust_lex_exception_test (&parser
, "b'\\x0'", "Not enough hex digits seen");
2351 rust_lex_exception_test (&parser
, "b'\\u{0}'",
2352 "Unicode escape in byte literal");
2353 rust_lex_exception_test (&parser
, "'\\x0'", "Not enough hex digits seen");
2354 rust_lex_exception_test (&parser
, "'\\u0'", "Missing '{' in Unicode escape");
2355 rust_lex_exception_test (&parser
, "'\\u{0", "Missing '}' in Unicode escape");
2356 rust_lex_exception_test (&parser
, "'\\u{0000007}", "Overlong hex escape");
2357 rust_lex_exception_test (&parser
, "'\\u{}", "Not enough hex digits seen");
2358 rust_lex_exception_test (&parser
, "'\\Q'", "Invalid escape \\Q in literal");
2359 rust_lex_exception_test (&parser
, "b'\\Q'", "Invalid escape \\Q in literal");
2361 rust_lex_int_test (&parser
, "23", 23, DECIMAL_INTEGER
);
2362 rust_lex_int_test (&parser
, "2_344__29", 234429, INTEGER
);
2363 rust_lex_int_test (&parser
, "0x1f", 0x1f, INTEGER
);
2364 rust_lex_int_test (&parser
, "23usize", 23, INTEGER
);
2365 rust_lex_int_test (&parser
, "23i32", 23, INTEGER
);
2366 rust_lex_int_test (&parser
, "0x1_f", 0x1f, INTEGER
);
2367 rust_lex_int_test (&parser
, "0b1_101011__", 0x6b, INTEGER
);
2368 rust_lex_int_test (&parser
, "0o001177i64", 639, INTEGER
);
2369 rust_lex_int_test (&parser
, "0x123456789u64", 0x123456789ull
, INTEGER
);
2371 rust_lex_test_trailing_dot (&parser
);
2373 rust_lex_test_one (&parser
, "23.", FLOAT
);
2374 rust_lex_test_one (&parser
, "23.99f32", FLOAT
);
2375 rust_lex_test_one (&parser
, "23e7", FLOAT
);
2376 rust_lex_test_one (&parser
, "23E-7", FLOAT
);
2377 rust_lex_test_one (&parser
, "23e+7", FLOAT
);
2378 rust_lex_test_one (&parser
, "23.99e+7f64", FLOAT
);
2379 rust_lex_test_one (&parser
, "23.82f32", FLOAT
);
2381 rust_lex_stringish_test (&parser
, "hibob", "hibob", IDENT
);
2382 rust_lex_stringish_test (&parser
, "hibob__93", "hibob__93", IDENT
);
2383 rust_lex_stringish_test (&parser
, "thread", "thread", IDENT
);
2384 rust_lex_stringish_test (&parser
, "r#true", "true", IDENT
);
2386 const int expected1
[] = { IDENT
, DECIMAL_INTEGER
, 0 };
2387 rust_lex_test_sequence (&parser
, "r#thread 23", ARRAY_SIZE (expected1
),
2389 const int expected2
[] = { IDENT
, '#', 0 };
2390 rust_lex_test_sequence (&parser
, "r#", ARRAY_SIZE (expected2
), expected2
);
2392 rust_lex_stringish_test (&parser
, "\"string\"", "string", STRING
);
2393 rust_lex_stringish_test (&parser
, "\"str\\ting\"", "str\ting", STRING
);
2394 rust_lex_stringish_test (&parser
, "\"str\\\"ing\"", "str\"ing", STRING
);
2395 rust_lex_stringish_test (&parser
, "r\"str\\ing\"", "str\\ing", STRING
);
2396 rust_lex_stringish_test (&parser
, "r#\"str\\ting\"#", "str\\ting", STRING
);
2397 rust_lex_stringish_test (&parser
, "r###\"str\\\"ing\"###", "str\\\"ing",
2400 rust_lex_stringish_test (&parser
, "b\"string\"", "string", BYTESTRING
);
2401 rust_lex_stringish_test (&parser
, "b\"\x73tring\"", "string", BYTESTRING
);
2402 rust_lex_stringish_test (&parser
, "b\"str\\\"ing\"", "str\"ing", BYTESTRING
);
2403 rust_lex_stringish_test (&parser
, "br####\"\\x73tring\"####", "\\x73tring",
2406 for (const auto &candidate
: identifier_tokens
)
2407 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2409 for (const auto &candidate
: operator_tokens
)
2410 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2412 rust_lex_test_completion (&parser
);
2413 rust_lex_test_push_back (&parser
);
2416 #endif /* GDB_SELF_TEST */
2420 void _initialize_rust_exp ();
2422 _initialize_rust_exp ()
2424 int code
= regcomp (&number_regex
, number_regex_text
, REG_EXTENDED
);
2425 /* If the regular expression was incorrect, it was a programming
2427 gdb_assert (code
== 0);
2430 selftests::register_test ("rust-lex", rust_lex_tests
);