1 /* Rust expression parsing for GDB, the GNU debugger.
3 Copyright (C) 2016-2022 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 #define UTF32 "UTF-32BE"
39 #define UTF32 "UTF-32LE"
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
46 static const char number_regex_text
[] =
47 /* subexpression 1: allows use of alternation, otherwise uninteresting */
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
55 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
58 /* Recognize exponent without decimal point, with optional type
60 subexpression 4: if present, type suffix
63 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
65 /* "23." is a valid floating point number, but "23.e5" and
66 "23.f32" are not. So, handle the trailing-. case
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
77 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
78 "([iu](size|8|16|32|64))?"
80 /* The number of subexpressions to allocate space for, including the
81 "0th" whole match subexpression. */
82 #define NUM_SUBEXPRESSIONS 8
84 /* The compiled number-matching regex. */
86 static regex_t number_regex
;
88 /* The kinds of tokens. Note that single-character tokens are
89 represented by themselves, so for instance '[' is a token. */
92 /* Make sure to start after any ASCII character. */
103 /* Keyword tokens. */
116 /* Operator tokens. */
131 /* A typed integer constant. */
139 /* A typed floating point constant. */
141 struct typed_val_float
147 /* A struct of this type is used to describe a token. */
153 enum exp_opcode opcode
;
156 /* Identifier tokens. */
158 static const struct token_info identifier_tokens
[] =
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
},
173 /* Operator tokens, sorted longest first. */
175 static const struct token_info operator_tokens
[] =
177 { ">>=", COMPOUND_ASSIGN
, BINOP_RSH
},
178 { "<<=", COMPOUND_ASSIGN
, BINOP_LSH
},
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
},
198 { "::", COLONCOLON
, OP_NULL
},
199 { "..", DOTDOT
, OP_NULL
},
200 { "->", ARROW
, OP_NULL
}
203 /* An instance of this is created before parsing, and destroyed when
204 parsing is finished. */
208 explicit rust_parser (struct parser_state
*state
)
213 DISABLE_COPY_AND_ASSIGN (rust_parser
);
215 /* Return the parser's language. */
216 const struct language_defn
*language () const
218 return pstate
->language ();
221 /* Return the parser's gdbarch. */
222 struct gdbarch
*arch () const
224 return pstate
->gdbarch ();
227 /* A helper to look up a Rust type, or fail. This only works for
228 types defined by rust_language_arch_info. */
230 struct type
*get_type (const char *name
)
234 type
= language_lookup_primitive_type (language (), arch (), name
);
236 error (_("Could not find Rust type %s"), name
);
240 std::string
crate_name (const std::string
&name
);
241 std::string
super_name (const std::string
&ident
, unsigned int n_supers
);
243 int lex_character ();
246 int lex_identifier ();
247 uint32_t lex_hex (int min
, int max
);
248 uint32_t lex_escape (int is_byte
);
250 int lex_one_token ();
251 void push_back (char c
);
253 /* The main interface to lexing. Lexes one token and updates the
257 current_token
= lex_one_token ();
260 /* Assuming the current token is TYPE, lex the next token. */
261 void assume (int type
)
263 gdb_assert (current_token
== type
);
267 /* Require the single-character token C, and lex the next token; or
268 throw an exception. */
269 void require (char type
)
271 if (current_token
!= type
)
272 error (_("'%c' expected"), type
);
276 /* Entry point for all parsing. */
277 operation_up
parse_entry_point ()
280 return parse_expr ();
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
);
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
);
316 /* Clear some state. This is only used for testing. */
318 void reset (const char *input
)
320 pstate
->prev_lexptr
= nullptr;
321 pstate
->lexptr
= input
;
324 current_int_val
= {};
325 current_float_val
= {};
326 current_string_val
= {};
327 current_opcode
= OP_NULL
;
329 #endif /* GDB_SELF_TEST */
331 /* Return the token's string value as a string. */
332 std::string
get_string () const
334 return std::string (current_string_val
.ptr
, current_string_val
.length
);
337 /* A pointer to this is installed globally. */
338 auto_obstack obstack
;
340 /* The parser state gdb gave us. */
341 struct parser_state
*pstate
;
343 /* Depth of parentheses. */
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
;
354 /* When completing, this may be set to the field operation to
356 operation_up completion_op
;
359 /* Return an string referring to NAME, but relative to the crate's
363 rust_parser::crate_name (const std::string
&name
)
365 std::string crate
= rust_crate_for_block (pstate
->expression_context_block
);
368 error (_("Could not find crate for current location"));
369 return "::" + crate
+ "::" + name
;
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. */
377 rust_parser::super_name (const std::string
&ident
, unsigned int n_supers
)
379 const char *scope
= block_scope (pstate
->expression_context_block
);
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 (*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 (), UTF32
, (const gdb_byte
*) text
,
605 quote
, 1, &result
, translit_none
);
607 int size
= obstack_object_size (&result
);
609 error (_("overlong character literal"));
611 memcpy (&value
, obstack_finish (&result
), size
);
615 /* Lex a character constant. */
618 rust_parser::lex_character ()
623 if (pstate
->lexptr
[0] == 'b')
628 gdb_assert (pstate
->lexptr
[0] == '\'');
630 if (pstate
->lexptr
[0] == '\'')
631 error (_("empty character literal"));
632 else if (pstate
->lexptr
[0] == '\\')
633 value
= lex_escape (is_byte
);
637 value
= lex_multibyte_char (&pstate
->lexptr
[0], &len
);
638 pstate
->lexptr
+= len
;
641 if (pstate
->lexptr
[0] != '\'')
642 error (_("Unterminated character literal"));
645 current_int_val
.val
= value
;
646 current_int_val
.type
= get_type (is_byte
? "u8" : "char");
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. */
655 starts_raw_string (const char *str
)
657 const char *save
= str
;
662 while (str
[0] == '#')
669 /* Return true if STR looks like the end of a raw string that had N
670 hashes at the start. */
673 ends_raw_string (const char *str
, int n
)
677 gdb_assert (str
[0] == '"');
678 for (i
= 0; i
< n
; ++i
)
679 if (str
[i
+ 1] != '#')
684 /* Lex a string constant. */
687 rust_parser::lex_string ()
689 int is_byte
= pstate
->lexptr
[0] == 'b';
694 raw_length
= starts_raw_string (pstate
->lexptr
);
695 pstate
->lexptr
+= raw_length
;
696 gdb_assert (pstate
->lexptr
[0] == '"');
705 if (pstate
->lexptr
[0] == '"' && ends_raw_string (pstate
->lexptr
,
708 /* Exit with lexptr pointing after the final "#". */
709 pstate
->lexptr
+= raw_length
;
712 else if (pstate
->lexptr
[0] == '\0')
713 error (_("Unexpected EOF in string"));
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
);
722 else if (pstate
->lexptr
[0] == '"')
724 /* Make sure to skip the quote. */
728 else if (pstate
->lexptr
[0] == '\\')
730 value
= lex_escape (is_byte
);
733 obstack_1grow (&obstack
, value
);
735 convert_between_encodings (UTF32
, "UTF-8", (gdb_byte
*) &value
,
736 sizeof (value
), sizeof (value
),
737 &obstack
, translit_none
);
739 else if (pstate
->lexptr
[0] == '\0')
740 error (_("Unexpected EOF in string"));
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
);
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
;
756 /* Return true if STRING starts with whitespace followed by a digit. */
759 space_then_number (const char *string
)
761 const char *p
= string
;
763 while (p
[0] == ' ' || p
[0] == '\t')
768 return *p
>= '0' && *p
<= '9';
771 /* Return true if C can start an identifier. */
774 rust_identifier_start_p (char c
)
776 return ((c
>= 'a' && c
<= 'z')
777 || (c
>= 'A' && c
<= 'Z')
780 /* Allow any non-ASCII character as an identifier. There
781 doesn't seem to be a need to be picky about this. */
785 /* Lex an identifier. */
788 rust_parser::lex_identifier ()
791 const struct token_info
*token
;
792 int is_gdb_var
= pstate
->lexptr
[0] == '$';
795 if (pstate
->lexptr
[0] == 'r'
796 && pstate
->lexptr
[1] == '#'
797 && rust_identifier_start_p (pstate
->lexptr
[2]))
803 const char *start
= pstate
->lexptr
;
804 gdb_assert (rust_identifier_start_p (pstate
->lexptr
[0]));
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)
819 length
= pstate
->lexptr
- start
;
823 for (const auto &candidate
: identifier_tokens
)
825 if (length
== strlen (candidate
.name
)
826 && strncmp (candidate
.name
, start
, length
) == 0)
836 if (token
->value
== 0)
838 /* Leave the terminating token alone. */
839 pstate
->lexptr
= start
;
843 else if (token
== NULL
845 && (strncmp (start
, "thread", length
) == 0
846 || strncmp (start
, "task", length
) == 0)
847 && space_then_number (pstate
->lexptr
))
849 /* "task" or "thread" followed by a number terminates the
850 parse, per gdb rules. */
851 pstate
->lexptr
= start
;
855 if (token
== NULL
|| (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0'))
857 current_string_val
.length
= length
;
858 current_string_val
.ptr
= start
;
861 if (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0')
863 /* Prevent rustyylex from returning two COMPLETE tokens. */
864 pstate
->prev_lexptr
= pstate
->lexptr
;
875 /* Lex an operator. */
878 rust_parser::lex_operator ()
880 const struct token_info
*token
= NULL
;
882 for (const auto &candidate
: operator_tokens
)
884 if (strncmp (candidate
.name
, pstate
->lexptr
,
885 strlen (candidate
.name
)) == 0)
887 pstate
->lexptr
+= strlen (candidate
.name
);
895 current_opcode
= token
->opcode
;
899 return *pstate
->lexptr
++;
905 rust_parser::lex_number ()
907 regmatch_t subexps
[NUM_SUBEXPRESSIONS
];
910 int could_be_decimal
= 1;
911 int implicit_i32
= 0;
912 const char *type_name
= NULL
;
918 match
= regexec (&number_regex
, pstate
->lexptr
, ARRAY_SIZE (subexps
),
920 /* Failure means the regexp is broken. */
921 gdb_assert (match
== 0);
923 if (subexps
[INT_TEXT
].rm_so
!= -1)
925 /* Integer part matched. */
927 end_index
= subexps
[INT_TEXT
].rm_eo
;
928 if (subexps
[INT_TYPE
].rm_so
== -1)
935 type_index
= INT_TYPE
;
936 could_be_decimal
= 0;
939 else if (subexps
[FLOAT_TYPE1
].rm_so
!= -1)
941 /* Found floating point type suffix. */
942 end_index
= subexps
[FLOAT_TYPE1
].rm_so
;
943 type_index
= FLOAT_TYPE1
;
945 else if (subexps
[FLOAT_TYPE2
].rm_so
!= -1)
947 /* Found floating point type suffix. */
948 end_index
= subexps
[FLOAT_TYPE2
].rm_so
;
949 type_index
= FLOAT_TYPE2
;
953 /* Any other floating point match. */
954 end_index
= subexps
[0].rm_eo
;
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] == '.')
965 const char *next
= skip_spaces (&pstate
->lexptr
[subexps
[0].rm_eo
]);
967 if (rust_identifier_start_p (*next
) || *next
== '.')
971 end_index
= subexps
[0].rm_eo
;
973 could_be_decimal
= 1;
978 /* Compute the type name if we haven't already. */
979 std::string type_name_holder
;
980 if (type_name
== NULL
)
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 ();
990 /* Look up the type. */
991 type
= get_type (type_name
);
993 /* Copy the text of the number and remove the "_"s. */
995 for (i
= 0; i
< end_index
&& pstate
->lexptr
[i
]; ++i
)
997 if (pstate
->lexptr
[i
] == '_')
998 could_be_decimal
= 0;
1000 number
.push_back (pstate
->lexptr
[i
]);
1003 /* Advance past the match. */
1004 pstate
->lexptr
+= subexps
[0].rm_eo
;
1006 /* Parse the number. */
1013 if (number
[0] == '0')
1015 if (number
[1] == 'x')
1017 else if (number
[1] == 'o')
1019 else if (number
[1] == 'b')
1024 could_be_decimal
= 0;
1028 value
= strtoulst (number
.c_str () + offset
, NULL
, radix
);
1029 if (implicit_i32
&& value
>= ((uint64_t) 1) << 31)
1030 type
= get_type ("i64");
1032 current_int_val
.val
= value
;
1033 current_int_val
.type
= type
;
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
);
1044 return is_integer
? (could_be_decimal
? DECIMAL_INTEGER
: INTEGER
) : FLOAT
;
1050 rust_parser::lex_one_token ()
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')
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
)
1064 pstate
->prev_lexptr
= pstate
->lexptr
;
1065 if (pstate
->lexptr
[0] == '\0')
1067 if (pstate
->parse_completion
)
1069 current_string_val
.length
=0;
1070 current_string_val
.ptr
= "";
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] == ']')
1094 /* Falls through to lex_operator. */
1097 else if (pstate
->lexptr
[0] == '(' || pstate
->lexptr
[0] == '{')
1099 /* Falls through to lex_operator. */
1102 else if (pstate
->lexptr
[0] == ',' && pstate
->comma_terminates
1103 && paren_depth
== 0)
1106 return lex_operator ();
1109 /* Push back a single character to be re-lexed. */
1112 rust_parser::push_back (char c
)
1114 /* Can't be called before any lexing. */
1115 gdb_assert (pstate
->prev_lexptr
!= NULL
);
1118 gdb_assert (*pstate
->lexptr
== c
);
1123 /* Parse a tuple or paren expression. */
1126 rust_parser::parse_tuple ()
1130 if (current_token
== ')')
1133 struct type
*unit
= get_type ("()");
1134 return make_operation
<long_const_operation
> (unit
, 0);
1137 operation_up expr
= parse_expr ();
1138 if (current_token
== ')')
1140 /* Parenthesized expression. */
1142 return make_operation
<rust_parenthesized_operation
> (std::move (expr
));
1145 std::vector
<operation_up
> ops
;
1146 ops
.push_back (std::move (expr
));
1147 while (current_token
!= ')')
1149 if (current_token
!= ',')
1150 error (_("',' or ')' expected"));
1153 /* A trailing "," is ok. */
1154 if (current_token
!= ')')
1155 ops
.push_back (parse_expr ());
1160 error (_("Tuple expressions not supported yet"));
1163 /* Parse an array expression. */
1166 rust_parser::parse_array ()
1170 if (current_token
== KW_MUT
)
1173 operation_up result
;
1174 operation_up expr
= parse_expr ();
1175 if (current_token
== ';')
1178 operation_up rhs
= parse_expr ();
1179 result
= make_operation
<rust_array_operation
> (std::move (expr
),
1182 else if (current_token
== ',')
1184 std::vector
<operation_up
> ops
;
1185 ops
.push_back (std::move (expr
));
1186 while (current_token
!= ']')
1188 if (current_token
!= ',')
1189 error (_("',' or ']' expected"));
1191 ops
.push_back (parse_expr ());
1193 ops
.shrink_to_fit ();
1194 int len
= ops
.size () - 1;
1195 result
= make_operation
<array_operation
> (0, len
, std::move (ops
));
1197 else if (current_token
!= ']')
1198 error (_("',', ';', or ']' expected"));
1205 /* Turn a name into an operation. */
1208 rust_parser::name_to_operation (const std::string
&name
)
1210 struct block_symbol sym
= lookup_symbol (name
.c_str (),
1211 pstate
->expression_context_block
,
1213 if (sym
.symbol
!= nullptr && sym
.symbol
->aclass () != LOC_TYPEDEF
)
1214 return make_operation
<var_value_operation
> (sym
);
1216 struct type
*type
= nullptr;
1218 if (sym
.symbol
!= nullptr)
1220 gdb_assert (sym
.symbol
->aclass () == LOC_TYPEDEF
);
1221 type
= sym
.symbol
->type ();
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 ());
1228 if (type
->code () == TYPE_CODE_STRUCT
&& type
->num_fields () == 0)
1230 /* A unit-like struct. */
1231 operation_up
result (new rust_aggregate_operation (type
, {}, {}));
1235 return make_operation
<type_operation
> (type
);
1238 /* Parse a struct expression. */
1241 rust_parser::parse_struct_expr (struct type
*type
)
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"));
1250 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1251 while (current_token
!= '}' && current_token
!= DOTDOT
)
1253 if (current_token
!= IDENT
)
1254 error (_("'}', '..', or identifier expected"));
1256 std::string name
= get_string ();
1260 if (current_token
== ',' || current_token
== '}'
1261 || current_token
== DOTDOT
)
1262 expr
= name_to_operation (name
);
1266 expr
= parse_expr ();
1268 field_v
.emplace_back (std::move (name
), std::move (expr
));
1270 /* A trailing "," is ok. */
1271 if (current_token
== ',')
1275 operation_up others
;
1276 if (current_token
== DOTDOT
)
1279 others
= parse_expr ();
1284 return make_operation
<rust_aggregate_operation
> (type
,
1286 std::move (field_v
));
1289 /* Used by the operator precedence parser. */
1292 rustop_item (int token_
, int precedence_
, enum exp_opcode opcode_
,
1295 precedence (precedence_
),
1297 op (std::move (op_
))
1301 /* The token value. */
1303 /* Precedence of this operator. */
1305 /* This is used only for assign-modify. */
1306 enum exp_opcode opcode
;
1307 /* The right hand side of this operation. */
1311 /* An operator precedence parser for binary operations, including
1315 rust_parser::parse_binop (bool required
)
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. */
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)
1345 operation_up start
= parse_atom (required
);
1346 if (start
== nullptr)
1348 gdb_assert (!required
);
1352 std::vector
<rustop_item
> operator_stack
;
1353 operator_stack
.emplace_back (0, -1, OP_NULL
, std::move (start
));
1357 int this_token
= current_token
;
1358 enum exp_opcode compound_assign_op
= OP_NULL
;
1359 int precedence
= -2;
1363 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1365 precedence = PRECEDENCE; \
1373 case COMPOUND_ASSIGN
:
1374 compound_assign_op
= current_opcode
;
1381 /* "as" must be handled specially. */
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
),
1390 /* Bypass the rest of the loop. */
1394 /* Arrange to pop the entire stack. */
1399 while (precedence
< operator_stack
.back ().precedence
1400 && operator_stack
.size () > 1)
1402 rustop_item rhs
= std::move (operator_stack
.back ());
1403 operator_stack
.pop_back ();
1405 rustop_item
&lhs
= operator_stack
.back ();
1409 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1411 lhs.op = make_operation<TYPE> (std::move (lhs.op), \
1412 std::move (rhs.op)); \
1420 case COMPOUND_ASSIGN
:
1422 if (rhs
.token
== '=')
1423 lhs
.op
= (make_operation
<assign_operation
>
1424 (std::move (lhs
.op
), std::move (rhs
.op
)));
1426 lhs
.op
= (make_operation
<assign_modify_operation
>
1427 (rhs
.opcode
, std::move (lhs
.op
),
1428 std::move (rhs
.op
)));
1430 struct type
*unit_type
= get_type ("()");
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
)));
1439 gdb_assert_not_reached ("bad binary operator");
1443 if (precedence
== -2)
1446 operator_stack
.emplace_back (this_token
, precedence
, compound_assign_op
,
1450 gdb_assert (operator_stack
.size () == 1);
1451 return std::move (operator_stack
[0].op
);
1455 /* Parse a range expression. */
1458 rust_parser::parse_range ()
1460 enum range_flag kind
= (RANGE_HIGH_BOUND_DEFAULT
1461 | RANGE_LOW_BOUND_DEFAULT
);
1464 if (current_token
!= DOTDOT
&& current_token
!= DOTDOTEQ
)
1466 lhs
= parse_binop (true);
1467 kind
&= ~RANGE_LOW_BOUND_DEFAULT
;
1470 if (current_token
== DOTDOT
)
1471 kind
|= RANGE_HIGH_BOUND_EXCLUSIVE
;
1472 else if (current_token
!= DOTDOTEQ
)
1476 /* A "..=" range requires a high bound, but otherwise it is
1478 operation_up rhs
= parse_binop ((kind
& RANGE_HIGH_BOUND_EXCLUSIVE
) == 0);
1480 kind
&= ~RANGE_HIGH_BOUND_DEFAULT
;
1482 return make_operation
<rust_range_operation
> (kind
,
1487 /* Parse an expression. */
1490 rust_parser::parse_expr ()
1492 return parse_range ();
1495 /* Parse a sizeof expression. */
1498 rust_parser::parse_sizeof ()
1503 operation_up result
= make_operation
<unop_sizeof_operation
> (parse_expr ());
1508 /* Parse an address-of operation. */
1511 rust_parser::parse_addr ()
1515 if (current_token
== KW_MUT
)
1518 return make_operation
<rust_unop_addr_operation
> (parse_atom (true));
1521 /* Parse a field expression. */
1524 rust_parser::parse_field (operation_up
&&lhs
)
1528 operation_up result
;
1529 switch (current_token
)
1534 bool is_complete
= current_token
== COMPLETE
;
1535 auto struct_op
= new rust_structop (std::move (lhs
), get_string ());
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"));
1544 result
.reset (struct_op
);
1548 case DECIMAL_INTEGER
:
1549 result
= make_operation
<rust_struct_anon
> (current_int_val
.val
,
1555 error (_("'_' not allowed in integers in anonymous field references"));
1558 error (_("field name expected"));
1564 /* Parse an index expression. */
1567 rust_parser::parse_index (operation_up
&&lhs
)
1570 operation_up rhs
= parse_expr ();
1573 return make_operation
<rust_subscript_operation
> (std::move (lhs
),
1577 /* Parse a sequence of comma-separated expressions in parens. */
1579 std::vector
<operation_up
>
1580 rust_parser::parse_paren_args ()
1584 std::vector
<operation_up
> args
;
1585 while (current_token
!= ')')
1589 if (current_token
!= ',')
1590 error (_("',' or ')' expected"));
1594 args
.push_back (parse_expr ());
1602 /* Parse the parenthesized part of a function call. */
1605 rust_parser::parse_call (operation_up
&&lhs
)
1607 std::vector
<operation_up
> args
= parse_paren_args ();
1609 return make_operation
<funcall_operation
> (std::move (lhs
),
1613 /* Parse a list of types. */
1615 std::vector
<struct type
*>
1616 rust_parser::parse_type_list ()
1618 std::vector
<struct type
*> result
;
1619 result
.push_back (parse_type ());
1620 while (current_token
== ',')
1623 result
.push_back (parse_type ());
1628 /* Parse a possibly-empty list of types, surrounded in parens. */
1630 std::vector
<struct type
*>
1631 rust_parser::parse_maybe_type_list ()
1634 std::vector
<struct type
*> types
;
1635 if (current_token
!= ')')
1636 types
= parse_type_list ();
1641 /* Parse an array type. */
1644 rust_parser::parse_array_type ()
1647 struct type
*elt_type
= parse_type ();
1650 if (current_token
!= INTEGER
&& current_token
!= DECIMAL_INTEGER
)
1651 error (_("integer expected"));
1652 ULONGEST val
= current_int_val
.val
;
1656 return lookup_array_range_type (elt_type
, 0, val
- 1);
1659 /* Parse a slice type. */
1662 rust_parser::parse_slice_type ()
1666 bool is_slice
= current_token
== '[';
1670 struct type
*target
= parse_type ();
1675 return rust_slice_type ("&[*gdb*]", target
, get_type ("usize"));
1678 /* For now we treat &x and *x identically. */
1679 return lookup_pointer_type (target
);
1682 /* Parse a pointer type. */
1685 rust_parser::parse_pointer_type ()
1689 if (current_token
== KW_MUT
|| current_token
== KW_CONST
)
1692 struct type
*target
= parse_type ();
1693 /* For the time being we ignore mut/const. */
1694 return lookup_pointer_type (target
);
1697 /* Parse a function type. */
1700 rust_parser::parse_function_type ()
1704 if (current_token
!= '(')
1705 error (_("'(' expected"));
1707 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1709 if (current_token
!= ARROW
)
1710 error (_("'->' expected"));
1713 struct type
*result_type
= parse_type ();
1715 struct type
**argtypes
= nullptr;
1716 if (!types
.empty ())
1717 argtypes
= types
.data ();
1719 result_type
= lookup_function_type_with_arguments (result_type
,
1722 return lookup_pointer_type (result_type
);
1725 /* Parse a tuple type. */
1728 rust_parser::parse_tuple_type ()
1730 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1732 auto_obstack obstack
;
1733 obstack_1grow (&obstack
, '(');
1734 for (int i
= 0; i
< types
.size (); ++i
)
1736 std::string type_name
= type_to_string (types
[i
]);
1739 obstack_1grow (&obstack
, ',');
1740 obstack_grow_str (&obstack
, type_name
.c_str ());
1743 obstack_grow_str0 (&obstack
, ")");
1744 const char *name
= (const char *) obstack_finish (&obstack
);
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
);
1758 rust_parser::parse_type ()
1760 switch (current_token
)
1763 return parse_array_type ();
1765 return parse_slice_type ();
1767 return parse_pointer_type ();
1769 return parse_function_type ();
1771 return parse_tuple_type ();
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 ());
1785 error (_("type expected"));
1792 rust_parser::parse_path (bool for_expr
)
1794 unsigned n_supers
= 0;
1795 int first_token
= current_token
;
1797 switch (current_token
)
1801 if (current_token
!= COLONCOLON
)
1806 while (current_token
== KW_SUPER
)
1810 if (current_token
!= COLONCOLON
)
1811 error (_("'::' expected"));
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. */
1828 if (current_token
!= IDENT
)
1829 error (_("identifier expected"));
1830 std::string path
= get_string ();
1831 bool saw_ident
= true;
1834 /* The condition here lets us enter the loop even if we see
1836 while (current_token
== COLONCOLON
|| current_token
== '<')
1838 if (current_token
== COLONCOLON
)
1843 if (current_token
== IDENT
)
1845 path
= path
+ "::" + get_string ();
1849 else if (current_token
== COLONCOLON
)
1851 /* The code below won't detect this scenario. */
1852 error (_("unexpected '::'"));
1856 if (current_token
!= '<')
1859 /* Expression use name::<...>, whereas types use name<...>. */
1862 /* Expressions use "name::<...>", so if we saw an identifier
1863 after the "::", we ignore the "<" here. */
1869 /* Types use "name<...>", so we need to have seen the
1876 std::vector
<struct type
*> types
= parse_type_list ();
1877 if (current_token
== '>')
1879 else if (current_token
== RSH
)
1885 error (_("'>' expected"));
1888 for (int i
= 0; i
< types
.size (); ++i
)
1892 path
+= type_to_string (types
[i
]);
1898 switch (first_token
)
1902 return super_name (path
, n_supers
);
1905 return crate_name (path
);
1914 gdb_assert_not_reached ("missing case in path parsing");
1918 /* Handle the parsing for a string expression. */
1921 rust_parser::parse_string ()
1923 gdb_assert (current_token
== STRING
);
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'"));
1930 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1932 size_t len
= current_string_val
.length
;
1933 operation_up str
= make_operation
<string_operation
> (get_string ());
1935 = make_operation
<rust_unop_addr_operation
> (std::move (str
));
1936 field_v
.emplace_back ("data_ptr", std::move (addr
));
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
));
1942 return make_operation
<rust_aggregate_operation
> (type
,
1944 std::move (field_v
));
1947 /* Parse a tuple struct expression. */
1950 rust_parser::parse_tuple_struct (struct type
*type
)
1952 std::vector
<operation_up
> args
= parse_paren_args ();
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
]) };
1958 return (make_operation
<rust_aggregate_operation
>
1959 (type
, operation_up (), std::move (field_v
)));
1962 /* Parse a path expression. */
1965 rust_parser::parse_path_expr ()
1967 std::string path
= parse_path (true);
1969 if (current_token
== '{')
1971 struct type
*type
= rust_lookup_type (path
.c_str ());
1972 if (type
== nullptr)
1973 error (_("Could not find type '%s'"), path
.c_str ());
1975 return parse_struct_expr (type
);
1977 else if (current_token
== '(')
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)
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
);
1990 return name_to_operation (path
);
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. */
1998 rust_parser::parse_atom (bool required
)
2000 operation_up result
;
2002 switch (current_token
)
2005 result
= parse_tuple ();
2009 result
= parse_array ();
2013 case DECIMAL_INTEGER
:
2014 result
= make_operation
<long_const_operation
> (current_int_val
.type
,
2015 current_int_val
.val
);
2020 result
= make_operation
<float_const_operation
> (current_float_val
.type
,
2021 current_float_val
.val
);
2026 result
= parse_string ();
2030 result
= make_operation
<string_operation
> (get_string ());
2036 result
= make_operation
<bool_operation
> (current_token
== KW_TRUE
);
2041 /* This is kind of a hacky approach. */
2043 pstate
->push_dollar (current_string_val
);
2044 result
= pstate
->pop ();
2054 result
= parse_path_expr ();
2059 result
= make_operation
<rust_unop_ind_operation
> (parse_atom (true));
2063 result
= make_operation
<unary_plus_operation
> (parse_atom (true));
2067 result
= make_operation
<unary_neg_operation
> (parse_atom (true));
2071 result
= make_operation
<rust_unop_compl_operation
> (parse_atom (true));
2074 result
= parse_sizeof ();
2077 result
= parse_addr ();
2083 error (_("unexpected token"));
2086 /* Now parse suffixes. */
2089 switch (current_token
)
2092 result
= parse_field (std::move (result
));
2096 result
= parse_index (std::move (result
));
2100 result
= parse_call (std::move (result
));
2111 /* The parser as exposed to gdb. */
2114 rust_language::parser (struct parser_state
*state
) const
2116 rust_parser
parser (state
);
2118 operation_up result
;
2121 result
= parser
.parse_entry_point ();
2123 catch (const gdb_exception
&exc
)
2125 if (state
->parse_completion
)
2127 result
= std::move (parser
.completion_op
);
2128 if (result
== nullptr)
2135 state
->set_operation (std::move (result
));
2144 /* A test helper that lexes a string, expecting a single token. */
2147 rust_lex_test_one (rust_parser
*parser
, const char *input
, int expected
)
2151 parser
->reset (input
);
2153 token
= parser
->lex_one_token ();
2154 SELF_CHECK (token
== expected
);
2158 token
= parser
->lex_one_token ();
2159 SELF_CHECK (token
== 0);
2163 /* Test that INPUT lexes as the integer VALUE. */
2166 rust_lex_int_test (rust_parser
*parser
, const char *input
,
2167 ULONGEST value
, int kind
)
2169 rust_lex_test_one (parser
, input
, kind
);
2170 SELF_CHECK (parser
->current_int_val
.val
== value
);
2173 /* Test that INPUT throws an exception with text ERR. */
2176 rust_lex_exception_test (rust_parser
*parser
, const char *input
,
2181 /* The "kind" doesn't matter. */
2182 rust_lex_test_one (parser
, input
, DECIMAL_INTEGER
);
2185 catch (const gdb_exception_error
&except
)
2187 SELF_CHECK (strcmp (except
.what (), err
) == 0);
2191 /* Test that INPUT lexes as the identifier, string, or byte-string
2192 VALUE. KIND holds the expected token kind. */
2195 rust_lex_stringish_test (rust_parser
*parser
, const char *input
,
2196 const char *value
, int kind
)
2198 rust_lex_test_one (parser
, input
, kind
);
2199 SELF_CHECK (parser
->get_string () == value
);
2202 /* Helper to test that a string parses as a given token sequence. */
2205 rust_lex_test_sequence (rust_parser
*parser
, const char *input
, int len
,
2206 const int expected
[])
2210 parser
->reset (input
);
2212 for (i
= 0; i
< len
; ++i
)
2214 int token
= parser
->lex_one_token ();
2215 SELF_CHECK (token
== expected
[i
]);
2219 /* Tests for an integer-parsing corner case. */
2222 rust_lex_test_trailing_dot (rust_parser
*parser
)
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 };
2229 rust_lex_test_sequence (parser
, "23.g()", ARRAY_SIZE (expected1
), expected1
);
2230 rust_lex_test_sequence (parser
, "23_0.g()", ARRAY_SIZE (expected2
),
2232 rust_lex_test_sequence (parser
, "23.==()", ARRAY_SIZE (expected3
),
2234 rust_lex_test_sequence (parser
, "23..25", ARRAY_SIZE (expected4
), expected4
);
2237 /* Tests of completion. */
2240 rust_lex_test_completion (rust_parser
*parser
)
2242 const int expected
[] = { IDENT
, '.', COMPLETE
, 0 };
2244 parser
->pstate
->parse_completion
= 1;
2246 rust_lex_test_sequence (parser
, "something.wha", ARRAY_SIZE (expected
),
2248 rust_lex_test_sequence (parser
, "something.", ARRAY_SIZE (expected
),
2251 parser
->pstate
->parse_completion
= 0;
2254 /* Test pushback. */
2257 rust_lex_test_push_back (rust_parser
*parser
)
2261 parser
->reset (">>=");
2263 token
= parser
->lex_one_token ();
2264 SELF_CHECK (token
== COMPOUND_ASSIGN
);
2265 SELF_CHECK (parser
->current_opcode
== BINOP_RSH
);
2267 parser
->push_back ('=');
2269 token
= parser
->lex_one_token ();
2270 SELF_CHECK (token
== '=');
2272 token
= parser
->lex_one_token ();
2273 SELF_CHECK (token
== 0);
2276 /* Unit test the lexer. */
2279 rust_lex_tests (void)
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
);
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);
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
);
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
);
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
);
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");
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
);
2340 rust_lex_test_trailing_dot (&parser
);
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
);
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
);
2355 const int expected1
[] = { IDENT
, DECIMAL_INTEGER
, 0 };
2356 rust_lex_test_sequence (&parser
, "r#thread 23", ARRAY_SIZE (expected1
),
2358 const int expected2
[] = { IDENT
, '#', 0 };
2359 rust_lex_test_sequence (&parser
, "r#", ARRAY_SIZE (expected2
), expected2
);
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",
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",
2375 for (const auto &candidate
: identifier_tokens
)
2376 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2378 for (const auto &candidate
: operator_tokens
)
2379 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2381 rust_lex_test_completion (&parser
);
2382 rust_lex_test_push_back (&parser
);
2385 #endif /* GDB_SELF_TEST */
2389 void _initialize_rust_exp ();
2391 _initialize_rust_exp ()
2393 int code
= regcomp (&number_regex
, number_regex_text
, REG_EXTENDED
);
2394 /* If the regular expression was incorrect, it was a programming
2396 gdb_assert (code
== 0);
2399 selftests::register_test ("rust-lex", rust_lex_tests
);