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"
36 /* A regular expression for matching Rust numbers. This is split up
37 since it is very long and this gives us a way to comment the
40 static const char number_regex_text
[] =
41 /* subexpression 1: allows use of alternation, otherwise uninteresting */
43 /* First comes floating point. */
44 /* Recognize number after the decimal point, with optional
45 exponent and optional type suffix.
46 subexpression 2: allows "?", otherwise uninteresting
47 subexpression 3: if present, type suffix
49 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
52 /* Recognize exponent without decimal point, with optional type
54 subexpression 4: if present, type suffix
57 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
59 /* "23." is a valid floating point number, but "23.e5" and
60 "23.f32" are not. So, handle the trailing-. case
64 /* Finally come integers.
65 subexpression 5: text of integer
66 subexpression 6: if present, type suffix
67 subexpression 7: allows use of alternation, otherwise uninteresting
71 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
72 "([iu](size|8|16|32|64))?"
74 /* The number of subexpressions to allocate space for, including the
75 "0th" whole match subexpression. */
76 #define NUM_SUBEXPRESSIONS 8
78 /* The compiled number-matching regex. */
80 static regex_t number_regex
;
82 /* The kinds of tokens. Note that single-character tokens are
83 represented by themselves, so for instance '[' is a token. */
86 /* Make sure to start after any ASCII character. */
110 /* Operator tokens. */
125 /* A typed integer constant. */
133 /* A typed floating point constant. */
135 struct typed_val_float
141 /* A struct of this type is used to describe a token. */
147 enum exp_opcode opcode
;
150 /* Identifier tokens. */
152 static const struct token_info identifier_tokens
[] =
154 { "as", KW_AS
, OP_NULL
},
155 { "false", KW_FALSE
, OP_NULL
},
156 { "if", 0, OP_NULL
},
157 { "mut", KW_MUT
, OP_NULL
},
158 { "const", KW_CONST
, OP_NULL
},
159 { "self", KW_SELF
, OP_NULL
},
160 { "super", KW_SUPER
, OP_NULL
},
161 { "true", KW_TRUE
, OP_NULL
},
162 { "extern", KW_EXTERN
, OP_NULL
},
163 { "fn", KW_FN
, OP_NULL
},
164 { "sizeof", KW_SIZEOF
, OP_NULL
},
167 /* Operator tokens, sorted longest first. */
169 static const struct token_info operator_tokens
[] =
171 { ">>=", COMPOUND_ASSIGN
, BINOP_RSH
},
172 { "<<=", COMPOUND_ASSIGN
, BINOP_LSH
},
174 { "<<", LSH
, OP_NULL
},
175 { ">>", RSH
, OP_NULL
},
176 { "&&", ANDAND
, OP_NULL
},
177 { "||", OROR
, OP_NULL
},
178 { "==", EQEQ
, OP_NULL
},
179 { "!=", NOTEQ
, OP_NULL
},
180 { "<=", LTEQ
, OP_NULL
},
181 { ">=", GTEQ
, OP_NULL
},
182 { "+=", COMPOUND_ASSIGN
, BINOP_ADD
},
183 { "-=", COMPOUND_ASSIGN
, BINOP_SUB
},
184 { "*=", COMPOUND_ASSIGN
, BINOP_MUL
},
185 { "/=", COMPOUND_ASSIGN
, BINOP_DIV
},
186 { "%=", COMPOUND_ASSIGN
, BINOP_REM
},
187 { "&=", COMPOUND_ASSIGN
, BINOP_BITWISE_AND
},
188 { "|=", COMPOUND_ASSIGN
, BINOP_BITWISE_IOR
},
189 { "^=", COMPOUND_ASSIGN
, BINOP_BITWISE_XOR
},
190 { "..=", DOTDOTEQ
, OP_NULL
},
192 { "::", COLONCOLON
, OP_NULL
},
193 { "..", DOTDOT
, OP_NULL
},
194 { "->", ARROW
, OP_NULL
}
197 /* An instance of this is created before parsing, and destroyed when
198 parsing is finished. */
202 explicit rust_parser (struct parser_state
*state
)
207 DISABLE_COPY_AND_ASSIGN (rust_parser
);
209 /* Return the parser's language. */
210 const struct language_defn
*language () const
212 return pstate
->language ();
215 /* Return the parser's gdbarch. */
216 struct gdbarch
*arch () const
218 return pstate
->gdbarch ();
221 /* A helper to look up a Rust type, or fail. This only works for
222 types defined by rust_language_arch_info. */
224 struct type
*get_type (const char *name
)
228 type
= language_lookup_primitive_type (language (), arch (), name
);
230 error (_("Could not find Rust type %s"), name
);
234 std::string
crate_name (const std::string
&name
);
235 std::string
super_name (const std::string
&ident
, unsigned int n_supers
);
237 int lex_character ();
240 int lex_identifier ();
241 uint32_t lex_hex (int min
, int max
);
242 uint32_t lex_escape (int is_byte
);
244 int lex_one_token ();
245 void push_back (char c
);
247 /* The main interface to lexing. Lexes one token and updates the
251 current_token
= lex_one_token ();
254 /* Assuming the current token is TYPE, lex the next token. */
255 void assume (int type
)
257 gdb_assert (current_token
== type
);
261 /* Require the single-character token C, and lex the next token; or
262 throw an exception. */
263 void require (char type
)
265 if (current_token
!= type
)
266 error (_("'%c' expected"), type
);
270 /* Entry point for all parsing. */
271 operation_up
parse_entry_point ()
274 return parse_expr ();
277 operation_up
parse_tuple ();
278 operation_up
parse_array ();
279 operation_up
name_to_operation (const std::string
&name
);
280 operation_up
parse_struct_expr (struct type
*type
);
281 operation_up
parse_binop (bool required
);
282 operation_up
parse_range ();
283 operation_up
parse_expr ();
284 operation_up
parse_sizeof ();
285 operation_up
parse_addr ();
286 operation_up
parse_field (operation_up
&&);
287 operation_up
parse_index (operation_up
&&);
288 std::vector
<operation_up
> parse_paren_args ();
289 operation_up
parse_call (operation_up
&&);
290 std::vector
<struct type
*> parse_type_list ();
291 std::vector
<struct type
*> parse_maybe_type_list ();
292 struct type
*parse_array_type ();
293 struct type
*parse_slice_type ();
294 struct type
*parse_pointer_type ();
295 struct type
*parse_function_type ();
296 struct type
*parse_tuple_type ();
297 struct type
*parse_type ();
298 std::string
parse_path (bool for_expr
);
299 operation_up
parse_string ();
300 operation_up
parse_tuple_struct (struct type
*type
);
301 operation_up
parse_path_expr ();
302 operation_up
parse_atom (bool required
);
304 void update_innermost_block (struct block_symbol sym
);
305 struct block_symbol
lookup_symbol (const char *name
,
306 const struct block
*block
,
307 const domain_enum domain
);
308 struct type
*rust_lookup_type (const char *name
);
310 /* Clear some state. This is only used for testing. */
312 void reset (const char *input
)
314 pstate
->prev_lexptr
= nullptr;
315 pstate
->lexptr
= input
;
318 current_int_val
= {};
319 current_float_val
= {};
320 current_string_val
= {};
321 current_opcode
= OP_NULL
;
323 #endif /* GDB_SELF_TEST */
325 /* Return the token's string value as a string. */
326 std::string
get_string () const
328 return std::string (current_string_val
.ptr
, current_string_val
.length
);
331 /* A pointer to this is installed globally. */
332 auto_obstack obstack
;
334 /* The parser state gdb gave us. */
335 struct parser_state
*pstate
;
337 /* Depth of parentheses. */
340 /* The current token's type. */
341 int current_token
= 0;
342 /* The current token's payload, if any. */
343 typed_val_int current_int_val
{};
344 typed_val_float current_float_val
{};
345 struct stoken current_string_val
{};
346 enum exp_opcode current_opcode
= OP_NULL
;
348 /* When completing, this may be set to the field operation to
350 operation_up completion_op
;
353 /* Return an string referring to NAME, but relative to the crate's
357 rust_parser::crate_name (const std::string
&name
)
359 std::string crate
= rust_crate_for_block (pstate
->expression_context_block
);
362 error (_("Could not find crate for current location"));
363 return "::" + crate
+ "::" + name
;
366 /* Return a string referring to a "super::" qualified name. IDENT is
367 the base name and N_SUPERS is how many "super::"s were provided.
368 N_SUPERS can be zero. */
371 rust_parser::super_name (const std::string
&ident
, unsigned int n_supers
)
373 const char *scope
= block_scope (pstate
->expression_context_block
);
376 if (scope
[0] == '\0')
377 error (_("Couldn't find namespace scope for self::"));
382 std::vector
<int> offsets
;
383 unsigned int current_len
;
385 current_len
= cp_find_first_component (scope
);
386 while (scope
[current_len
] != '\0')
388 offsets
.push_back (current_len
);
389 gdb_assert (scope
[current_len
] == ':');
392 current_len
+= cp_find_first_component (scope
396 len
= offsets
.size ();
398 error (_("Too many super:: uses from '%s'"), scope
);
400 offset
= offsets
[len
- n_supers
];
403 offset
= strlen (scope
);
405 return "::" + std::string (scope
, offset
) + "::" + ident
;
408 /* A helper to appropriately munge NAME and BLOCK depending on the
409 presence of a leading "::". */
412 munge_name_and_block (const char **name
, const struct block
**block
)
414 /* If it is a global reference, skip the current block in favor of
416 if (startswith (*name
, "::"))
419 *block
= block_static_block (*block
);
423 /* Like lookup_symbol, but handles Rust namespace conventions, and
424 doesn't require field_of_this_result. */
427 rust_parser::lookup_symbol (const char *name
, const struct block
*block
,
428 const domain_enum domain
)
430 struct block_symbol result
;
432 munge_name_and_block (&name
, &block
);
434 result
= ::lookup_symbol (name
, block
, domain
, NULL
);
435 if (result
.symbol
!= NULL
)
436 update_innermost_block (result
);
440 /* Look up a type, following Rust namespace conventions. */
443 rust_parser::rust_lookup_type (const char *name
)
445 struct block_symbol result
;
448 const struct block
*block
= pstate
->expression_context_block
;
449 munge_name_and_block (&name
, &block
);
451 result
= ::lookup_symbol (name
, block
, STRUCT_DOMAIN
, NULL
);
452 if (result
.symbol
!= NULL
)
454 update_innermost_block (result
);
455 return SYMBOL_TYPE (result
.symbol
);
458 type
= lookup_typename (language (), name
, NULL
, 1);
462 /* Last chance, try a built-in type. */
463 return language_lookup_primitive_type (language (), arch (), name
);
466 /* A helper that updates the innermost block as appropriate. */
469 rust_parser::update_innermost_block (struct block_symbol sym
)
471 if (symbol_read_needs_frame (sym
.symbol
))
472 pstate
->block_tracker
->update (sym
);
475 /* Lex a hex number with at least MIN digits and at most MAX
479 rust_parser::lex_hex (int min
, int max
)
483 /* We only want to stop at MAX if we're lexing a byte escape. */
484 int check_max
= min
== max
;
486 while ((check_max
? len
<= max
: 1)
487 && ((pstate
->lexptr
[0] >= 'a' && pstate
->lexptr
[0] <= 'f')
488 || (pstate
->lexptr
[0] >= 'A' && pstate
->lexptr
[0] <= 'F')
489 || (pstate
->lexptr
[0] >= '0' && pstate
->lexptr
[0] <= '9')))
492 if (pstate
->lexptr
[0] >= 'a' && pstate
->lexptr
[0] <= 'f')
493 result
= result
+ 10 + pstate
->lexptr
[0] - 'a';
494 else if (pstate
->lexptr
[0] >= 'A' && pstate
->lexptr
[0] <= 'F')
495 result
= result
+ 10 + pstate
->lexptr
[0] - 'A';
497 result
= result
+ pstate
->lexptr
[0] - '0';
503 error (_("Not enough hex digits seen"));
506 gdb_assert (min
!= max
);
507 error (_("Overlong hex escape"));
513 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
514 otherwise we're lexing a character escape. */
517 rust_parser::lex_escape (int is_byte
)
521 gdb_assert (pstate
->lexptr
[0] == '\\');
523 switch (pstate
->lexptr
[0])
527 result
= lex_hex (2, 2);
532 error (_("Unicode escape in byte literal"));
534 if (pstate
->lexptr
[0] != '{')
535 error (_("Missing '{' in Unicode escape"));
537 result
= lex_hex (1, 6);
538 /* Could do range checks here. */
539 if (pstate
->lexptr
[0] != '}')
540 error (_("Missing '}' in Unicode escape"));
574 error (_("Invalid escape \\%c in literal"), pstate
->lexptr
[0]);
580 /* Lex a character constant. */
583 rust_parser::lex_character ()
588 if (pstate
->lexptr
[0] == 'b')
593 gdb_assert (pstate
->lexptr
[0] == '\'');
595 /* This should handle UTF-8 here. */
596 if (pstate
->lexptr
[0] == '\\')
597 value
= lex_escape (is_byte
);
600 value
= pstate
->lexptr
[0] & 0xff;
604 if (pstate
->lexptr
[0] != '\'')
605 error (_("Unterminated character literal"));
608 current_int_val
.val
= value
;
609 current_int_val
.type
= get_type (is_byte
? "u8" : "char");
614 /* Return the offset of the double quote if STR looks like the start
615 of a raw string, or 0 if STR does not start a raw string. */
618 starts_raw_string (const char *str
)
620 const char *save
= str
;
625 while (str
[0] == '#')
632 /* Return true if STR looks like the end of a raw string that had N
633 hashes at the start. */
636 ends_raw_string (const char *str
, int n
)
640 gdb_assert (str
[0] == '"');
641 for (i
= 0; i
< n
; ++i
)
642 if (str
[i
+ 1] != '#')
647 /* Lex a string constant. */
650 rust_parser::lex_string ()
652 int is_byte
= pstate
->lexptr
[0] == 'b';
657 raw_length
= starts_raw_string (pstate
->lexptr
);
658 pstate
->lexptr
+= raw_length
;
659 gdb_assert (pstate
->lexptr
[0] == '"');
668 if (pstate
->lexptr
[0] == '"' && ends_raw_string (pstate
->lexptr
,
671 /* Exit with lexptr pointing after the final "#". */
672 pstate
->lexptr
+= raw_length
;
675 else if (pstate
->lexptr
[0] == '\0')
676 error (_("Unexpected EOF in string"));
678 value
= pstate
->lexptr
[0] & 0xff;
679 if (is_byte
&& value
> 127)
680 error (_("Non-ASCII value in raw byte string"));
681 obstack_1grow (&obstack
, value
);
685 else if (pstate
->lexptr
[0] == '"')
687 /* Make sure to skip the quote. */
691 else if (pstate
->lexptr
[0] == '\\')
693 value
= lex_escape (is_byte
);
696 obstack_1grow (&obstack
, value
);
700 #define UTF32 "UTF-32BE"
702 #define UTF32 "UTF-32LE"
704 convert_between_encodings (UTF32
, "UTF-8", (gdb_byte
*) &value
,
705 sizeof (value
), sizeof (value
),
706 &obstack
, translit_none
);
709 else if (pstate
->lexptr
[0] == '\0')
710 error (_("Unexpected EOF in string"));
713 value
= pstate
->lexptr
[0] & 0xff;
714 if (is_byte
&& value
> 127)
715 error (_("Non-ASCII value in byte string"));
716 obstack_1grow (&obstack
, value
);
721 current_string_val
.length
= obstack_object_size (&obstack
);
722 current_string_val
.ptr
= (const char *) obstack_finish (&obstack
);
723 return is_byte
? BYTESTRING
: STRING
;
726 /* Return true if STRING starts with whitespace followed by a digit. */
729 space_then_number (const char *string
)
731 const char *p
= string
;
733 while (p
[0] == ' ' || p
[0] == '\t')
738 return *p
>= '0' && *p
<= '9';
741 /* Return true if C can start an identifier. */
744 rust_identifier_start_p (char c
)
746 return ((c
>= 'a' && c
<= 'z')
747 || (c
>= 'A' && c
<= 'Z')
752 /* Lex an identifier. */
755 rust_parser::lex_identifier ()
758 const struct token_info
*token
;
759 int is_gdb_var
= pstate
->lexptr
[0] == '$';
762 if (pstate
->lexptr
[0] == 'r'
763 && pstate
->lexptr
[1] == '#'
764 && rust_identifier_start_p (pstate
->lexptr
[2]))
770 const char *start
= pstate
->lexptr
;
771 gdb_assert (rust_identifier_start_p (pstate
->lexptr
[0]));
775 /* For the time being this doesn't handle Unicode rules. Non-ASCII
776 identifiers are gated anyway. */
777 while ((pstate
->lexptr
[0] >= 'a' && pstate
->lexptr
[0] <= 'z')
778 || (pstate
->lexptr
[0] >= 'A' && pstate
->lexptr
[0] <= 'Z')
779 || pstate
->lexptr
[0] == '_'
780 || (is_gdb_var
&& pstate
->lexptr
[0] == '$')
781 || (pstate
->lexptr
[0] >= '0' && pstate
->lexptr
[0] <= '9'))
785 length
= pstate
->lexptr
- start
;
789 for (const auto &candidate
: identifier_tokens
)
791 if (length
== strlen (candidate
.name
)
792 && strncmp (candidate
.name
, start
, length
) == 0)
802 if (token
->value
== 0)
804 /* Leave the terminating token alone. */
805 pstate
->lexptr
= start
;
809 else if (token
== NULL
811 && (strncmp (start
, "thread", length
) == 0
812 || strncmp (start
, "task", length
) == 0)
813 && space_then_number (pstate
->lexptr
))
815 /* "task" or "thread" followed by a number terminates the
816 parse, per gdb rules. */
817 pstate
->lexptr
= start
;
821 if (token
== NULL
|| (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0'))
823 current_string_val
.length
= length
;
824 current_string_val
.ptr
= start
;
827 if (pstate
->parse_completion
&& pstate
->lexptr
[0] == '\0')
829 /* Prevent rustyylex from returning two COMPLETE tokens. */
830 pstate
->prev_lexptr
= pstate
->lexptr
;
841 /* Lex an operator. */
844 rust_parser::lex_operator ()
846 const struct token_info
*token
= NULL
;
848 for (const auto &candidate
: operator_tokens
)
850 if (strncmp (candidate
.name
, pstate
->lexptr
,
851 strlen (candidate
.name
)) == 0)
853 pstate
->lexptr
+= strlen (candidate
.name
);
861 current_opcode
= token
->opcode
;
865 return *pstate
->lexptr
++;
871 rust_parser::lex_number ()
873 regmatch_t subexps
[NUM_SUBEXPRESSIONS
];
876 int could_be_decimal
= 1;
877 int implicit_i32
= 0;
878 const char *type_name
= NULL
;
884 match
= regexec (&number_regex
, pstate
->lexptr
, ARRAY_SIZE (subexps
),
886 /* Failure means the regexp is broken. */
887 gdb_assert (match
== 0);
889 if (subexps
[INT_TEXT
].rm_so
!= -1)
891 /* Integer part matched. */
893 end_index
= subexps
[INT_TEXT
].rm_eo
;
894 if (subexps
[INT_TYPE
].rm_so
== -1)
901 type_index
= INT_TYPE
;
902 could_be_decimal
= 0;
905 else if (subexps
[FLOAT_TYPE1
].rm_so
!= -1)
907 /* Found floating point type suffix. */
908 end_index
= subexps
[FLOAT_TYPE1
].rm_so
;
909 type_index
= FLOAT_TYPE1
;
911 else if (subexps
[FLOAT_TYPE2
].rm_so
!= -1)
913 /* Found floating point type suffix. */
914 end_index
= subexps
[FLOAT_TYPE2
].rm_so
;
915 type_index
= FLOAT_TYPE2
;
919 /* Any other floating point match. */
920 end_index
= subexps
[0].rm_eo
;
924 /* We need a special case if the final character is ".". In this
925 case we might need to parse an integer. For example, "23.f()" is
926 a request for a trait method call, not a syntax error involving
927 the floating point number "23.". */
928 gdb_assert (subexps
[0].rm_eo
> 0);
929 if (pstate
->lexptr
[subexps
[0].rm_eo
- 1] == '.')
931 const char *next
= skip_spaces (&pstate
->lexptr
[subexps
[0].rm_eo
]);
933 if (rust_identifier_start_p (*next
) || *next
== '.')
937 end_index
= subexps
[0].rm_eo
;
939 could_be_decimal
= 1;
944 /* Compute the type name if we haven't already. */
945 std::string type_name_holder
;
946 if (type_name
== NULL
)
948 gdb_assert (type_index
!= -1);
949 type_name_holder
= std::string ((pstate
->lexptr
950 + subexps
[type_index
].rm_so
),
951 (subexps
[type_index
].rm_eo
952 - subexps
[type_index
].rm_so
));
953 type_name
= type_name_holder
.c_str ();
956 /* Look up the type. */
957 type
= get_type (type_name
);
959 /* Copy the text of the number and remove the "_"s. */
961 for (i
= 0; i
< end_index
&& pstate
->lexptr
[i
]; ++i
)
963 if (pstate
->lexptr
[i
] == '_')
964 could_be_decimal
= 0;
966 number
.push_back (pstate
->lexptr
[i
]);
969 /* Advance past the match. */
970 pstate
->lexptr
+= subexps
[0].rm_eo
;
972 /* Parse the number. */
979 if (number
[0] == '0')
981 if (number
[1] == 'x')
983 else if (number
[1] == 'o')
985 else if (number
[1] == 'b')
990 could_be_decimal
= 0;
994 value
= strtoulst (number
.c_str () + offset
, NULL
, radix
);
995 if (implicit_i32
&& value
>= ((uint64_t) 1) << 31)
996 type
= get_type ("i64");
998 current_int_val
.val
= value
;
999 current_int_val
.type
= type
;
1003 current_float_val
.type
= type
;
1004 bool parsed
= parse_float (number
.c_str (), number
.length (),
1005 current_float_val
.type
,
1006 current_float_val
.val
.data ());
1007 gdb_assert (parsed
);
1010 return is_integer
? (could_be_decimal
? DECIMAL_INTEGER
: INTEGER
) : FLOAT
;
1016 rust_parser::lex_one_token ()
1018 /* Skip all leading whitespace. */
1019 while (pstate
->lexptr
[0] == ' '
1020 || pstate
->lexptr
[0] == '\t'
1021 || pstate
->lexptr
[0] == '\r'
1022 || pstate
->lexptr
[0] == '\n')
1025 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1026 we're completing an empty string at the end of a field_expr.
1027 But, we don't want to return two COMPLETE tokens in a row. */
1028 if (pstate
->lexptr
[0] == '\0' && pstate
->lexptr
== pstate
->prev_lexptr
)
1030 pstate
->prev_lexptr
= pstate
->lexptr
;
1031 if (pstate
->lexptr
[0] == '\0')
1033 if (pstate
->parse_completion
)
1035 current_string_val
.length
=0;
1036 current_string_val
.ptr
= "";
1042 if (pstate
->lexptr
[0] >= '0' && pstate
->lexptr
[0] <= '9')
1043 return lex_number ();
1044 else if (pstate
->lexptr
[0] == 'b' && pstate
->lexptr
[1] == '\'')
1045 return lex_character ();
1046 else if (pstate
->lexptr
[0] == 'b' && pstate
->lexptr
[1] == '"')
1047 return lex_string ();
1048 else if (pstate
->lexptr
[0] == 'b' && starts_raw_string (pstate
->lexptr
+ 1))
1049 return lex_string ();
1050 else if (starts_raw_string (pstate
->lexptr
))
1051 return lex_string ();
1052 else if (rust_identifier_start_p (pstate
->lexptr
[0]))
1053 return lex_identifier ();
1054 else if (pstate
->lexptr
[0] == '"')
1055 return lex_string ();
1056 else if (pstate
->lexptr
[0] == '\'')
1057 return lex_character ();
1058 else if (pstate
->lexptr
[0] == '}' || pstate
->lexptr
[0] == ']')
1060 /* Falls through to lex_operator. */
1063 else if (pstate
->lexptr
[0] == '(' || pstate
->lexptr
[0] == '{')
1065 /* Falls through to lex_operator. */
1068 else if (pstate
->lexptr
[0] == ',' && pstate
->comma_terminates
1069 && paren_depth
== 0)
1072 return lex_operator ();
1075 /* Push back a single character to be re-lexed. */
1078 rust_parser::push_back (char c
)
1080 /* Can't be called before any lexing. */
1081 gdb_assert (pstate
->prev_lexptr
!= NULL
);
1084 gdb_assert (*pstate
->lexptr
== c
);
1089 /* Parse a tuple or paren expression. */
1092 rust_parser::parse_tuple ()
1096 if (current_token
== ')')
1099 struct type
*unit
= get_type ("()");
1100 return make_operation
<long_const_operation
> (unit
, 0);
1103 operation_up expr
= parse_expr ();
1104 if (current_token
== ')')
1106 /* Parenthesized expression. */
1111 std::vector
<operation_up
> ops
;
1112 ops
.push_back (std::move (expr
));
1113 while (current_token
!= ')')
1115 if (current_token
!= ',')
1116 error (_("',' or ')' expected"));
1119 /* A trailing "," is ok. */
1120 if (current_token
!= ')')
1121 ops
.push_back (parse_expr ());
1126 error (_("Tuple expressions not supported yet"));
1129 /* Parse an array expression. */
1132 rust_parser::parse_array ()
1136 if (current_token
== KW_MUT
)
1139 operation_up result
;
1140 operation_up expr
= parse_expr ();
1141 if (current_token
== ';')
1144 operation_up rhs
= parse_expr ();
1145 result
= make_operation
<rust_array_operation
> (std::move (expr
),
1148 else if (current_token
== ',')
1150 std::vector
<operation_up
> ops
;
1151 ops
.push_back (std::move (expr
));
1152 while (current_token
!= ']')
1154 if (current_token
!= ',')
1155 error (_("',' or ']' expected"));
1157 ops
.push_back (parse_expr ());
1159 ops
.shrink_to_fit ();
1160 int len
= ops
.size () - 1;
1161 result
= make_operation
<array_operation
> (0, len
, std::move (ops
));
1163 else if (current_token
!= ']')
1164 error (_("',', ';', or ']' expected"));
1171 /* Turn a name into an operation. */
1174 rust_parser::name_to_operation (const std::string
&name
)
1176 struct block_symbol sym
= lookup_symbol (name
.c_str (),
1177 pstate
->expression_context_block
,
1179 if (sym
.symbol
!= nullptr && SYMBOL_CLASS (sym
.symbol
) != LOC_TYPEDEF
)
1180 return make_operation
<var_value_operation
> (sym
);
1182 struct type
*type
= nullptr;
1184 if (sym
.symbol
!= nullptr)
1186 gdb_assert (SYMBOL_CLASS (sym
.symbol
) == LOC_TYPEDEF
);
1187 type
= SYMBOL_TYPE (sym
.symbol
);
1189 if (type
== nullptr)
1190 type
= rust_lookup_type (name
.c_str ());
1191 if (type
== nullptr)
1192 error (_("No symbol '%s' in current context"), name
.c_str ());
1194 if (type
->code () == TYPE_CODE_STRUCT
&& type
->num_fields () == 0)
1196 /* A unit-like struct. */
1197 operation_up
result (new rust_aggregate_operation (type
, {}, {}));
1201 return make_operation
<type_operation
> (type
);
1204 /* Parse a struct expression. */
1207 rust_parser::parse_struct_expr (struct type
*type
)
1211 if (type
->code () != TYPE_CODE_STRUCT
1212 || rust_tuple_type_p (type
)
1213 || rust_tuple_struct_type_p (type
))
1214 error (_("Struct expression applied to non-struct type"));
1216 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1217 while (current_token
!= '}' && current_token
!= DOTDOT
)
1219 if (current_token
!= IDENT
)
1220 error (_("'}', '..', or identifier expected"));
1222 std::string name
= get_string ();
1226 if (current_token
== ',' || current_token
== '}'
1227 || current_token
== DOTDOT
)
1228 expr
= name_to_operation (name
);
1232 expr
= parse_expr ();
1234 field_v
.emplace_back (std::move (name
), std::move (expr
));
1236 /* A trailing "," is ok. */
1237 if (current_token
== ',')
1241 operation_up others
;
1242 if (current_token
== DOTDOT
)
1245 others
= parse_expr ();
1250 return make_operation
<rust_aggregate_operation
> (type
,
1252 std::move (field_v
));
1255 /* Used by the operator precedence parser. */
1258 rustop_item (int token_
, int precedence_
, enum exp_opcode opcode_
,
1261 precedence (precedence_
),
1263 op (std::move (op_
))
1267 /* The token value. */
1269 /* Precedence of this operator. */
1271 /* This is used only for assign-modify. */
1272 enum exp_opcode opcode
;
1273 /* The right hand side of this operation. */
1277 /* An operator precedence parser for binary operations, including
1281 rust_parser::parse_binop (bool required
)
1283 /* All the binary operators. Each one is of the form
1284 OPERATION(TOKEN, PRECEDENCE, TYPE)
1285 TOKEN is the corresponding operator token.
1286 PRECEDENCE is a value indicating relative precedence.
1287 TYPE is the operation type corresponding to the operator.
1288 Assignment operations are handled specially, not via this
1289 table; they have precedence 0. */
1291 OPERATION ('*', 10, mul_operation) \
1292 OPERATION ('/', 10, div_operation) \
1293 OPERATION ('%', 10, rem_operation) \
1294 OPERATION ('@', 9, repeat_operation) \
1295 OPERATION ('+', 8, add_operation) \
1296 OPERATION ('-', 8, sub_operation) \
1297 OPERATION (LSH, 7, lsh_operation) \
1298 OPERATION (RSH, 7, rsh_operation) \
1299 OPERATION ('&', 6, bitwise_and_operation) \
1300 OPERATION ('^', 5, bitwise_xor_operation) \
1301 OPERATION ('|', 4, bitwise_ior_operation) \
1302 OPERATION (EQEQ, 3, equal_operation) \
1303 OPERATION (NOTEQ, 3, notequal_operation) \
1304 OPERATION ('<', 3, less_operation) \
1305 OPERATION (LTEQ, 3, leq_operation) \
1306 OPERATION ('>', 3, gtr_operation) \
1307 OPERATION (GTEQ, 3, geq_operation) \
1308 OPERATION (ANDAND, 2, logical_and_operation) \
1309 OPERATION (OROR, 1, logical_or_operation)
1311 operation_up start
= parse_atom (required
);
1312 if (start
== nullptr)
1314 gdb_assert (!required
);
1318 std::vector
<rustop_item
> operator_stack
;
1319 operator_stack
.emplace_back (0, -1, OP_NULL
, std::move (start
));
1323 int this_token
= current_token
;
1324 enum exp_opcode compound_assign_op
= OP_NULL
;
1325 int precedence
= -2;
1329 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1331 precedence = PRECEDENCE; \
1339 case COMPOUND_ASSIGN
:
1340 compound_assign_op
= current_opcode
;
1347 /* "as" must be handled specially. */
1351 rustop_item
&lhs
= operator_stack
.back ();
1352 struct type
*type
= parse_type ();
1353 lhs
.op
= make_operation
<unop_cast_operation
> (std::move (lhs
.op
),
1356 /* Bypass the rest of the loop. */
1360 /* Arrange to pop the entire stack. */
1365 while (precedence
< operator_stack
.back ().precedence
1366 && operator_stack
.size () > 1)
1368 rustop_item rhs
= std::move (operator_stack
.back ());
1369 operator_stack
.pop_back ();
1371 rustop_item
&lhs
= operator_stack
.back ();
1375 #define OPERATION(TOKEN, PRECEDENCE, TYPE) \
1377 lhs.op = make_operation<TYPE> (std::move (lhs.op), \
1378 std::move (rhs.op)); \
1386 case COMPOUND_ASSIGN
:
1388 if (rhs
.token
== '=')
1389 lhs
.op
= (make_operation
<assign_operation
>
1390 (std::move (lhs
.op
), std::move (rhs
.op
)));
1392 lhs
.op
= (make_operation
<assign_modify_operation
>
1393 (rhs
.opcode
, std::move (lhs
.op
),
1394 std::move (rhs
.op
)));
1396 struct type
*unit_type
= get_type ("()");
1398 operation_up
nil (new long_const_operation (unit_type
, 0));
1399 lhs
.op
= (make_operation
<comma_operation
>
1400 (std::move (lhs
.op
), std::move (nil
)));
1405 gdb_assert_not_reached ("bad binary operator");
1409 if (precedence
== -2)
1412 operator_stack
.emplace_back (this_token
, precedence
, compound_assign_op
,
1416 gdb_assert (operator_stack
.size () == 1);
1417 return std::move (operator_stack
[0].op
);
1421 /* Parse a range expression. */
1424 rust_parser::parse_range ()
1426 enum range_flag kind
= (RANGE_HIGH_BOUND_DEFAULT
1427 | RANGE_LOW_BOUND_DEFAULT
);
1430 if (current_token
!= DOTDOT
&& current_token
!= DOTDOTEQ
)
1432 lhs
= parse_binop (true);
1433 kind
&= ~RANGE_LOW_BOUND_DEFAULT
;
1436 if (current_token
== DOTDOT
)
1437 kind
|= RANGE_HIGH_BOUND_EXCLUSIVE
;
1438 else if (current_token
!= DOTDOTEQ
)
1442 /* A "..=" range requires a high bound, but otherwise it is
1444 operation_up rhs
= parse_binop ((kind
& RANGE_HIGH_BOUND_EXCLUSIVE
) == 0);
1446 kind
&= ~RANGE_HIGH_BOUND_DEFAULT
;
1448 return make_operation
<rust_range_operation
> (kind
,
1453 /* Parse an expression. */
1456 rust_parser::parse_expr ()
1458 return parse_range ();
1461 /* Parse a sizeof expression. */
1464 rust_parser::parse_sizeof ()
1469 operation_up result
= make_operation
<unop_sizeof_operation
> (parse_expr ());
1474 /* Parse an address-of operation. */
1477 rust_parser::parse_addr ()
1481 if (current_token
== KW_MUT
)
1484 return make_operation
<rust_unop_addr_operation
> (parse_atom (true));
1487 /* Parse a field expression. */
1490 rust_parser::parse_field (operation_up
&&lhs
)
1494 operation_up result
;
1495 switch (current_token
)
1500 bool is_complete
= current_token
== COMPLETE
;
1501 auto struct_op
= new rust_structop (std::move (lhs
), get_string ());
1505 completion_op
.reset (struct_op
);
1506 pstate
->mark_struct_expression (struct_op
);
1507 /* Throw to the outermost level of the parser. */
1508 error (_("not really an error"));
1510 result
.reset (struct_op
);
1514 case DECIMAL_INTEGER
:
1515 result
= make_operation
<rust_struct_anon
> (current_int_val
.val
,
1521 error (_("'_' not allowed in integers in anonymous field references"));
1524 error (_("field name expected"));
1530 /* Parse an index expression. */
1533 rust_parser::parse_index (operation_up
&&lhs
)
1536 operation_up rhs
= parse_expr ();
1539 return make_operation
<rust_subscript_operation
> (std::move (lhs
),
1543 /* Parse a sequence of comma-separated expressions in parens. */
1545 std::vector
<operation_up
>
1546 rust_parser::parse_paren_args ()
1550 std::vector
<operation_up
> args
;
1551 while (current_token
!= ')')
1555 if (current_token
!= ',')
1556 error (_("',' or ')' expected"));
1560 args
.push_back (parse_expr ());
1568 /* Parse the parenthesized part of a function call. */
1571 rust_parser::parse_call (operation_up
&&lhs
)
1573 std::vector
<operation_up
> args
= parse_paren_args ();
1575 return make_operation
<funcall_operation
> (std::move (lhs
),
1579 /* Parse a list of types. */
1581 std::vector
<struct type
*>
1582 rust_parser::parse_type_list ()
1584 std::vector
<struct type
*> result
;
1585 result
.push_back (parse_type ());
1586 while (current_token
== ',')
1589 result
.push_back (parse_type ());
1594 /* Parse a possibly-empty list of types, surrounded in parens. */
1596 std::vector
<struct type
*>
1597 rust_parser::parse_maybe_type_list ()
1600 std::vector
<struct type
*> types
;
1601 if (current_token
!= ')')
1602 types
= parse_type_list ();
1607 /* Parse an array type. */
1610 rust_parser::parse_array_type ()
1613 struct type
*elt_type
= parse_type ();
1616 if (current_token
!= INTEGER
&& current_token
!= DECIMAL_INTEGER
)
1617 error (_("integer expected"));
1618 ULONGEST val
= current_int_val
.val
;
1622 return lookup_array_range_type (elt_type
, 0, val
- 1);
1625 /* Parse a slice type. */
1628 rust_parser::parse_slice_type ()
1632 bool is_slice
= current_token
== '[';
1636 struct type
*target
= parse_type ();
1641 return rust_slice_type ("&[*gdb*]", target
, get_type ("usize"));
1644 /* For now we treat &x and *x identically. */
1645 return lookup_pointer_type (target
);
1648 /* Parse a pointer type. */
1651 rust_parser::parse_pointer_type ()
1655 if (current_token
== KW_MUT
|| current_token
== KW_CONST
)
1658 struct type
*target
= parse_type ();
1659 /* For the time being we ignore mut/const. */
1660 return lookup_pointer_type (target
);
1663 /* Parse a function type. */
1666 rust_parser::parse_function_type ()
1670 if (current_token
!= '(')
1671 error (_("'(' expected"));
1673 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1675 if (current_token
!= ARROW
)
1676 error (_("'->' expected"));
1679 struct type
*result_type
= parse_type ();
1681 struct type
**argtypes
= nullptr;
1682 if (!types
.empty ())
1683 argtypes
= types
.data ();
1685 result_type
= lookup_function_type_with_arguments (result_type
,
1688 return lookup_pointer_type (result_type
);
1691 /* Parse a tuple type. */
1694 rust_parser::parse_tuple_type ()
1696 std::vector
<struct type
*> types
= parse_maybe_type_list ();
1698 auto_obstack obstack
;
1699 obstack_1grow (&obstack
, '(');
1700 for (int i
= 0; i
< types
.size (); ++i
)
1702 std::string type_name
= type_to_string (types
[i
]);
1705 obstack_1grow (&obstack
, ',');
1706 obstack_grow_str (&obstack
, type_name
.c_str ());
1709 obstack_grow_str0 (&obstack
, ")");
1710 const char *name
= (const char *) obstack_finish (&obstack
);
1712 /* We don't allow creating new tuple types (yet), but we do allow
1713 looking up existing tuple types. */
1714 struct type
*result
= rust_lookup_type (name
);
1715 if (result
== nullptr)
1716 error (_("could not find tuple type '%s'"), name
);
1724 rust_parser::parse_type ()
1726 switch (current_token
)
1729 return parse_array_type ();
1731 return parse_slice_type ();
1733 return parse_pointer_type ();
1735 return parse_function_type ();
1737 return parse_tuple_type ();
1744 std::string path
= parse_path (false);
1745 struct type
*result
= rust_lookup_type (path
.c_str ());
1746 if (result
== nullptr)
1747 error (_("No type name '%s' in current context"), path
.c_str ());
1751 error (_("type expected"));
1758 rust_parser::parse_path (bool for_expr
)
1760 unsigned n_supers
= 0;
1761 int first_token
= current_token
;
1763 switch (current_token
)
1767 if (current_token
!= COLONCOLON
)
1772 while (current_token
== KW_SUPER
)
1776 if (current_token
!= COLONCOLON
)
1777 error (_("'::' expected"));
1787 /* This is a gdb extension to make it possible to refer to items
1788 in other crates. It just bypasses adding the current crate
1789 to the front of the name. */
1794 if (current_token
!= IDENT
)
1795 error (_("identifier expected"));
1796 std::string path
= get_string ();
1797 bool saw_ident
= true;
1800 /* The condition here lets us enter the loop even if we see
1802 while (current_token
== COLONCOLON
|| current_token
== '<')
1804 if (current_token
== COLONCOLON
)
1809 if (current_token
== IDENT
)
1811 path
= path
+ "::" + get_string ();
1815 else if (current_token
== COLONCOLON
)
1817 /* The code below won't detect this scenario. */
1818 error (_("unexpected '::'"));
1822 if (current_token
!= '<')
1825 /* Expression use name::<...>, whereas types use name<...>. */
1828 /* Expressions use "name::<...>", so if we saw an identifier
1829 after the "::", we ignore the "<" here. */
1835 /* Types use "name<...>", so we need to have seen the
1842 std::vector
<struct type
*> types
= parse_type_list ();
1843 if (current_token
== '>')
1845 else if (current_token
== RSH
)
1851 error (_("'>' expected"));
1854 for (int i
= 0; i
< types
.size (); ++i
)
1858 path
+= type_to_string (types
[i
]);
1864 switch (first_token
)
1868 return super_name (path
, n_supers
);
1871 return crate_name (path
);
1880 gdb_assert_not_reached ("missing case in path parsing");
1884 /* Handle the parsing for a string expression. */
1887 rust_parser::parse_string ()
1889 gdb_assert (current_token
== STRING
);
1891 /* Wrap the raw string in the &str struct. */
1892 struct type
*type
= rust_lookup_type ("&str");
1893 if (type
== nullptr)
1894 error (_("Could not find type '&str'"));
1896 std::vector
<std::pair
<std::string
, operation_up
>> field_v
;
1898 size_t len
= current_string_val
.length
;
1899 operation_up str
= make_operation
<string_operation
> (get_string ());
1901 = make_operation
<rust_unop_addr_operation
> (std::move (str
));
1902 field_v
.emplace_back ("data_ptr", std::move (addr
));
1904 struct type
*valtype
= get_type ("usize");
1905 operation_up lenop
= make_operation
<long_const_operation
> (valtype
, len
);
1906 field_v
.emplace_back ("length", std::move (lenop
));
1908 return make_operation
<rust_aggregate_operation
> (type
,
1910 std::move (field_v
));
1913 /* Parse a tuple struct expression. */
1916 rust_parser::parse_tuple_struct (struct type
*type
)
1918 std::vector
<operation_up
> args
= parse_paren_args ();
1920 std::vector
<std::pair
<std::string
, operation_up
>> field_v (args
.size ());
1921 for (int i
= 0; i
< args
.size (); ++i
)
1922 field_v
[i
] = { string_printf ("__%d", i
), std::move (args
[i
]) };
1924 return (make_operation
<rust_aggregate_operation
>
1925 (type
, operation_up (), std::move (field_v
)));
1928 /* Parse a path expression. */
1931 rust_parser::parse_path_expr ()
1933 std::string path
= parse_path (true);
1935 if (current_token
== '{')
1937 struct type
*type
= rust_lookup_type (path
.c_str ());
1938 if (type
== nullptr)
1939 error (_("Could not find type '%s'"), path
.c_str ());
1941 return parse_struct_expr (type
);
1943 else if (current_token
== '(')
1945 struct type
*type
= rust_lookup_type (path
.c_str ());
1946 /* If this is actually a tuple struct expression, handle it
1947 here. If it is a call, it will be handled elsewhere. */
1948 if (type
!= nullptr)
1950 if (!rust_tuple_struct_type_p (type
))
1951 error (_("Type %s is not a tuple struct"), path
.c_str ());
1952 return parse_tuple_struct (type
);
1956 return name_to_operation (path
);
1959 /* Parse an atom. "Atom" isn't a Rust term, but this refers to a
1960 single unitary item in the grammar; but here including some unary
1961 prefix and postfix expressions. */
1964 rust_parser::parse_atom (bool required
)
1966 operation_up result
;
1968 switch (current_token
)
1971 result
= parse_tuple ();
1975 result
= parse_array ();
1979 case DECIMAL_INTEGER
:
1980 result
= make_operation
<long_const_operation
> (current_int_val
.type
,
1981 current_int_val
.val
);
1986 result
= make_operation
<float_const_operation
> (current_float_val
.type
,
1987 current_float_val
.val
);
1992 result
= parse_string ();
1996 result
= make_operation
<string_operation
> (get_string ());
2002 result
= make_operation
<bool_operation
> (current_token
== KW_TRUE
);
2007 /* This is kind of a hacky approach. */
2009 pstate
->push_dollar (current_string_val
);
2010 result
= pstate
->pop ();
2020 result
= parse_path_expr ();
2025 result
= make_operation
<rust_unop_ind_operation
> (parse_atom (true));
2029 result
= make_operation
<unary_plus_operation
> (parse_atom (true));
2033 result
= make_operation
<unary_neg_operation
> (parse_atom (true));
2037 result
= make_operation
<rust_unop_compl_operation
> (parse_atom (true));
2040 result
= parse_sizeof ();
2043 result
= parse_addr ();
2049 error (_("unexpected token"));
2052 /* Now parse suffixes. */
2055 switch (current_token
)
2058 result
= parse_field (std::move (result
));
2062 result
= parse_index (std::move (result
));
2066 result
= parse_call (std::move (result
));
2077 /* The parser as exposed to gdb. */
2080 rust_language::parser (struct parser_state
*state
) const
2082 rust_parser
parser (state
);
2084 operation_up result
;
2087 result
= parser
.parse_entry_point ();
2089 catch (const gdb_exception
&exc
)
2091 if (state
->parse_completion
)
2093 result
= std::move (parser
.completion_op
);
2094 if (result
== nullptr)
2101 state
->set_operation (std::move (result
));
2110 /* A test helper that lexes a string, expecting a single token. */
2113 rust_lex_test_one (rust_parser
*parser
, const char *input
, int expected
)
2117 parser
->reset (input
);
2119 token
= parser
->lex_one_token ();
2120 SELF_CHECK (token
== expected
);
2124 token
= parser
->lex_one_token ();
2125 SELF_CHECK (token
== 0);
2129 /* Test that INPUT lexes as the integer VALUE. */
2132 rust_lex_int_test (rust_parser
*parser
, const char *input
,
2133 ULONGEST value
, int kind
)
2135 rust_lex_test_one (parser
, input
, kind
);
2136 SELF_CHECK (parser
->current_int_val
.val
== value
);
2139 /* Test that INPUT throws an exception with text ERR. */
2142 rust_lex_exception_test (rust_parser
*parser
, const char *input
,
2147 /* The "kind" doesn't matter. */
2148 rust_lex_test_one (parser
, input
, DECIMAL_INTEGER
);
2151 catch (const gdb_exception_error
&except
)
2153 SELF_CHECK (strcmp (except
.what (), err
) == 0);
2157 /* Test that INPUT lexes as the identifier, string, or byte-string
2158 VALUE. KIND holds the expected token kind. */
2161 rust_lex_stringish_test (rust_parser
*parser
, const char *input
,
2162 const char *value
, int kind
)
2164 rust_lex_test_one (parser
, input
, kind
);
2165 SELF_CHECK (parser
->get_string () == value
);
2168 /* Helper to test that a string parses as a given token sequence. */
2171 rust_lex_test_sequence (rust_parser
*parser
, const char *input
, int len
,
2172 const int expected
[])
2176 parser
->reset (input
);
2178 for (i
= 0; i
< len
; ++i
)
2180 int token
= parser
->lex_one_token ();
2181 SELF_CHECK (token
== expected
[i
]);
2185 /* Tests for an integer-parsing corner case. */
2188 rust_lex_test_trailing_dot (rust_parser
*parser
)
2190 const int expected1
[] = { DECIMAL_INTEGER
, '.', IDENT
, '(', ')', 0 };
2191 const int expected2
[] = { INTEGER
, '.', IDENT
, '(', ')', 0 };
2192 const int expected3
[] = { FLOAT
, EQEQ
, '(', ')', 0 };
2193 const int expected4
[] = { DECIMAL_INTEGER
, DOTDOT
, DECIMAL_INTEGER
, 0 };
2195 rust_lex_test_sequence (parser
, "23.g()", ARRAY_SIZE (expected1
), expected1
);
2196 rust_lex_test_sequence (parser
, "23_0.g()", ARRAY_SIZE (expected2
),
2198 rust_lex_test_sequence (parser
, "23.==()", ARRAY_SIZE (expected3
),
2200 rust_lex_test_sequence (parser
, "23..25", ARRAY_SIZE (expected4
), expected4
);
2203 /* Tests of completion. */
2206 rust_lex_test_completion (rust_parser
*parser
)
2208 const int expected
[] = { IDENT
, '.', COMPLETE
, 0 };
2210 parser
->pstate
->parse_completion
= 1;
2212 rust_lex_test_sequence (parser
, "something.wha", ARRAY_SIZE (expected
),
2214 rust_lex_test_sequence (parser
, "something.", ARRAY_SIZE (expected
),
2217 parser
->pstate
->parse_completion
= 0;
2220 /* Test pushback. */
2223 rust_lex_test_push_back (rust_parser
*parser
)
2227 parser
->reset (">>=");
2229 token
= parser
->lex_one_token ();
2230 SELF_CHECK (token
== COMPOUND_ASSIGN
);
2231 SELF_CHECK (parser
->current_opcode
== BINOP_RSH
);
2233 parser
->push_back ('=');
2235 token
= parser
->lex_one_token ();
2236 SELF_CHECK (token
== '=');
2238 token
= parser
->lex_one_token ();
2239 SELF_CHECK (token
== 0);
2242 /* Unit test the lexer. */
2245 rust_lex_tests (void)
2247 /* Set up dummy "parser", so that rust_type works. */
2248 struct parser_state
ps (language_def (language_rust
), target_gdbarch (),
2249 nullptr, 0, 0, nullptr, 0, nullptr, false);
2250 rust_parser
parser (&ps
);
2252 rust_lex_test_one (&parser
, "", 0);
2253 rust_lex_test_one (&parser
, " \t \n \r ", 0);
2254 rust_lex_test_one (&parser
, "thread 23", 0);
2255 rust_lex_test_one (&parser
, "task 23", 0);
2256 rust_lex_test_one (&parser
, "th 104", 0);
2257 rust_lex_test_one (&parser
, "ta 97", 0);
2259 rust_lex_int_test (&parser
, "'z'", 'z', INTEGER
);
2260 rust_lex_int_test (&parser
, "'\\xff'", 0xff, INTEGER
);
2261 rust_lex_int_test (&parser
, "'\\u{1016f}'", 0x1016f, INTEGER
);
2262 rust_lex_int_test (&parser
, "b'z'", 'z', INTEGER
);
2263 rust_lex_int_test (&parser
, "b'\\xfe'", 0xfe, INTEGER
);
2264 rust_lex_int_test (&parser
, "b'\\xFE'", 0xfe, INTEGER
);
2265 rust_lex_int_test (&parser
, "b'\\xfE'", 0xfe, INTEGER
);
2267 /* Test all escapes in both modes. */
2268 rust_lex_int_test (&parser
, "'\\n'", '\n', INTEGER
);
2269 rust_lex_int_test (&parser
, "'\\r'", '\r', INTEGER
);
2270 rust_lex_int_test (&parser
, "'\\t'", '\t', INTEGER
);
2271 rust_lex_int_test (&parser
, "'\\\\'", '\\', INTEGER
);
2272 rust_lex_int_test (&parser
, "'\\0'", '\0', INTEGER
);
2273 rust_lex_int_test (&parser
, "'\\''", '\'', INTEGER
);
2274 rust_lex_int_test (&parser
, "'\\\"'", '"', INTEGER
);
2276 rust_lex_int_test (&parser
, "b'\\n'", '\n', INTEGER
);
2277 rust_lex_int_test (&parser
, "b'\\r'", '\r', INTEGER
);
2278 rust_lex_int_test (&parser
, "b'\\t'", '\t', INTEGER
);
2279 rust_lex_int_test (&parser
, "b'\\\\'", '\\', INTEGER
);
2280 rust_lex_int_test (&parser
, "b'\\0'", '\0', INTEGER
);
2281 rust_lex_int_test (&parser
, "b'\\''", '\'', INTEGER
);
2282 rust_lex_int_test (&parser
, "b'\\\"'", '"', INTEGER
);
2284 rust_lex_exception_test (&parser
, "'z", "Unterminated character literal");
2285 rust_lex_exception_test (&parser
, "b'\\x0'", "Not enough hex digits seen");
2286 rust_lex_exception_test (&parser
, "b'\\u{0}'",
2287 "Unicode escape in byte literal");
2288 rust_lex_exception_test (&parser
, "'\\x0'", "Not enough hex digits seen");
2289 rust_lex_exception_test (&parser
, "'\\u0'", "Missing '{' in Unicode escape");
2290 rust_lex_exception_test (&parser
, "'\\u{0", "Missing '}' in Unicode escape");
2291 rust_lex_exception_test (&parser
, "'\\u{0000007}", "Overlong hex escape");
2292 rust_lex_exception_test (&parser
, "'\\u{}", "Not enough hex digits seen");
2293 rust_lex_exception_test (&parser
, "'\\Q'", "Invalid escape \\Q in literal");
2294 rust_lex_exception_test (&parser
, "b'\\Q'", "Invalid escape \\Q in literal");
2296 rust_lex_int_test (&parser
, "23", 23, DECIMAL_INTEGER
);
2297 rust_lex_int_test (&parser
, "2_344__29", 234429, INTEGER
);
2298 rust_lex_int_test (&parser
, "0x1f", 0x1f, INTEGER
);
2299 rust_lex_int_test (&parser
, "23usize", 23, INTEGER
);
2300 rust_lex_int_test (&parser
, "23i32", 23, INTEGER
);
2301 rust_lex_int_test (&parser
, "0x1_f", 0x1f, INTEGER
);
2302 rust_lex_int_test (&parser
, "0b1_101011__", 0x6b, INTEGER
);
2303 rust_lex_int_test (&parser
, "0o001177i64", 639, INTEGER
);
2304 rust_lex_int_test (&parser
, "0x123456789u64", 0x123456789ull
, INTEGER
);
2306 rust_lex_test_trailing_dot (&parser
);
2308 rust_lex_test_one (&parser
, "23.", FLOAT
);
2309 rust_lex_test_one (&parser
, "23.99f32", FLOAT
);
2310 rust_lex_test_one (&parser
, "23e7", FLOAT
);
2311 rust_lex_test_one (&parser
, "23E-7", FLOAT
);
2312 rust_lex_test_one (&parser
, "23e+7", FLOAT
);
2313 rust_lex_test_one (&parser
, "23.99e+7f64", FLOAT
);
2314 rust_lex_test_one (&parser
, "23.82f32", FLOAT
);
2316 rust_lex_stringish_test (&parser
, "hibob", "hibob", IDENT
);
2317 rust_lex_stringish_test (&parser
, "hibob__93", "hibob__93", IDENT
);
2318 rust_lex_stringish_test (&parser
, "thread", "thread", IDENT
);
2319 rust_lex_stringish_test (&parser
, "r#true", "true", IDENT
);
2321 const int expected1
[] = { IDENT
, DECIMAL_INTEGER
, 0 };
2322 rust_lex_test_sequence (&parser
, "r#thread 23", ARRAY_SIZE (expected1
),
2324 const int expected2
[] = { IDENT
, '#', 0 };
2325 rust_lex_test_sequence (&parser
, "r#", ARRAY_SIZE (expected2
), expected2
);
2327 rust_lex_stringish_test (&parser
, "\"string\"", "string", STRING
);
2328 rust_lex_stringish_test (&parser
, "\"str\\ting\"", "str\ting", STRING
);
2329 rust_lex_stringish_test (&parser
, "\"str\\\"ing\"", "str\"ing", STRING
);
2330 rust_lex_stringish_test (&parser
, "r\"str\\ing\"", "str\\ing", STRING
);
2331 rust_lex_stringish_test (&parser
, "r#\"str\\ting\"#", "str\\ting", STRING
);
2332 rust_lex_stringish_test (&parser
, "r###\"str\\\"ing\"###", "str\\\"ing",
2335 rust_lex_stringish_test (&parser
, "b\"string\"", "string", BYTESTRING
);
2336 rust_lex_stringish_test (&parser
, "b\"\x73tring\"", "string", BYTESTRING
);
2337 rust_lex_stringish_test (&parser
, "b\"str\\\"ing\"", "str\"ing", BYTESTRING
);
2338 rust_lex_stringish_test (&parser
, "br####\"\\x73tring\"####", "\\x73tring",
2341 for (const auto &candidate
: identifier_tokens
)
2342 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2344 for (const auto &candidate
: operator_tokens
)
2345 rust_lex_test_one (&parser
, candidate
.name
, candidate
.value
);
2347 rust_lex_test_completion (&parser
);
2348 rust_lex_test_push_back (&parser
);
2351 #endif /* GDB_SELF_TEST */
2355 void _initialize_rust_exp ();
2357 _initialize_rust_exp ()
2359 int code
= regcomp (&number_regex
, number_regex_text
, REG_EXTENDED
);
2360 /* If the regular expression was incorrect, it was a programming
2362 gdb_assert (code
== 0);
2365 selftests::register_test ("rust-lex", rust_lex_tests
);