forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust-parse.c
2436 lines (2036 loc) · 59.2 KB
/
rust-parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Rust expression parsing for GDB, the GNU debugger.
Copyright (C) 2016-2024 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "block.h"
#include "charset.h"
#include "cp-support.h"
#include "gdbsupport/gdb_obstack.h"
#include "gdbsupport/gdb_regex.h"
#include "rust-lang.h"
#include "parser-defs.h"
#include "gdbsupport/selftest.h"
#include "value.h"
#include "gdbarch.h"
#include "rust-exp.h"
#include "inferior.h"
using namespace expr;
/* A regular expression for matching Rust numbers. This is split up
since it is very long and this gives us a way to comment the
sections. */
static const char number_regex_text[] =
/* subexpression 1: allows use of alternation, otherwise uninteresting */
"^("
/* First comes floating point. */
/* Recognize number after the decimal point, with optional
exponent and optional type suffix.
subexpression 2: allows "?", otherwise uninteresting
subexpression 3: if present, type suffix
*/
"[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
#define FLOAT_TYPE1 3
"|"
/* Recognize exponent without decimal point, with optional type
suffix.
subexpression 4: if present, type suffix
*/
#define FLOAT_TYPE2 4
"[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
"|"
/* "23." is a valid floating point number, but "23.e5" and
"23.f32" are not. So, handle the trailing-. case
separately. */
"[0-9][0-9_]*\\."
"|"
/* Finally come integers.
subexpression 5: text of integer
subexpression 6: if present, type suffix
subexpression 7: allows use of alternation, otherwise uninteresting
*/
#define INT_TEXT 5
#define INT_TYPE 6
"(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
"([iu](size|8|16|32|64|128))?"
")";
/* The number of subexpressions to allocate space for, including the
"0th" whole match subexpression. */
#define NUM_SUBEXPRESSIONS 8
/* The compiled number-matching regex. */
static regex_t number_regex;
/* The kinds of tokens. Note that single-character tokens are
represented by themselves, so for instance '[' is a token. */
enum token_type : int
{
/* Make sure to start after any ASCII character. */
GDBVAR = 256,
IDENT,
COMPLETE,
INTEGER,
DECIMAL_INTEGER,
STRING,
BYTESTRING,
FLOAT,
COMPOUND_ASSIGN,
/* Keyword tokens. */
KW_AS,
KW_IF,
KW_TRUE,
KW_FALSE,
KW_SUPER,
KW_SELF,
KW_MUT,
KW_EXTERN,
KW_CONST,
KW_FN,
KW_SIZEOF,
/* Operator tokens. */
DOTDOT,
DOTDOTEQ,
OROR,
ANDAND,
EQEQ,
NOTEQ,
LTEQ,
GTEQ,
LSH,
RSH,
COLONCOLON,
ARROW,
};
/* A typed integer constant. */
struct typed_val_int
{
gdb_mpz val;
struct type *type;
};
/* A typed floating point constant. */
struct typed_val_float
{
float_data val;
struct type *type;
};
/* A struct of this type is used to describe a token. */
struct token_info
{
const char *name;
int value;
enum exp_opcode opcode;
};
/* Identifier tokens. */
static const struct token_info identifier_tokens[] =
{
{ "as", KW_AS, OP_NULL },
{ "false", KW_FALSE, OP_NULL },
{ "if", 0, OP_NULL },
{ "mut", KW_MUT, OP_NULL },
{ "const", KW_CONST, OP_NULL },
{ "self", KW_SELF, OP_NULL },
{ "super", KW_SUPER, OP_NULL },
{ "true", KW_TRUE, OP_NULL },
{ "extern", KW_EXTERN, OP_NULL },
{ "fn", KW_FN, OP_NULL },
{ "sizeof", KW_SIZEOF, OP_NULL },
};
/* Operator tokens, sorted longest first. */
static const struct token_info operator_tokens[] =
{
{ ">>=", COMPOUND_ASSIGN, BINOP_RSH },
{ "<<=", COMPOUND_ASSIGN, BINOP_LSH },
{ "<<", LSH, OP_NULL },
{ ">>", RSH, OP_NULL },
{ "&&", ANDAND, OP_NULL },
{ "||", OROR, OP_NULL },
{ "==", EQEQ, OP_NULL },
{ "!=", NOTEQ, OP_NULL },
{ "<=", LTEQ, OP_NULL },
{ ">=", GTEQ, OP_NULL },
{ "+=", COMPOUND_ASSIGN, BINOP_ADD },
{ "-=", COMPOUND_ASSIGN, BINOP_SUB },
{ "*=", COMPOUND_ASSIGN, BINOP_MUL },
{ "/=", COMPOUND_ASSIGN, BINOP_DIV },
{ "%=", COMPOUND_ASSIGN, BINOP_REM },
{ "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
{ "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
{ "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
{ "..=", DOTDOTEQ, OP_NULL },
{ "::", COLONCOLON, OP_NULL },
{ "..", DOTDOT, OP_NULL },
{ "->", ARROW, OP_NULL }
};
/* An instance of this is created before parsing, and destroyed when
parsing is finished. */
struct rust_parser
{
explicit rust_parser (struct parser_state *state)
: pstate (state)
{
}
DISABLE_COPY_AND_ASSIGN (rust_parser);
/* Return the parser's language. */
const struct language_defn *language () const
{
return pstate->language ();
}
/* Return the parser's gdbarch. */
struct gdbarch *arch () const
{
return pstate->gdbarch ();
}
/* A helper to look up a Rust type, or fail. This only works for
types defined by rust_language_arch_info. */
struct type *get_type (const char *name)
{
struct type *type;
type = language_lookup_primitive_type (language (), arch (), name);
if (type == NULL)
error (_("Could not find Rust type %s"), name);
return type;
}
std::string crate_name (const std::string &name);
std::string super_name (const std::string &ident, unsigned int n_supers);
int lex_character ();
int lex_decimal_integer ();
int lex_number ();
int lex_string ();
int lex_identifier ();
uint32_t lex_hex (int min, int max);
uint32_t lex_escape (bool is_byte);
int lex_operator ();
int lex_one_token (bool decimal_only);
void push_back (char c);
/* The main interface to lexing. Lexes one token and updates the
internal state. DECIMAL_ONLY is true in the special case where
we want to tell the lexer not to parse a number as a float, but
instead only as a decimal integer. See parse_field. */
void lex (bool decimal_only = false)
{
current_token = lex_one_token (decimal_only);
}
/* Assuming the current token is TYPE, lex the next token.
DECIMAL_ONLY is passed to 'lex', which see. */
void assume (int type, bool decimal_only = false)
{
gdb_assert (current_token == type);
lex (decimal_only);
}
/* Require the single-character token C, and lex the next token; or
throw an exception. */
void require (char type)
{
if (current_token != type)
error (_("'%c' expected"), type);
lex ();
}
/* Entry point for all parsing. */
operation_up parse_entry_point ()
{
lex ();
operation_up result = parse_expr ();
if (current_token != 0)
error (_("Syntax error near '%s'"), pstate->prev_lexptr);
return result;
}
operation_up parse_tuple ();
operation_up parse_array ();
operation_up name_to_operation (const std::string &name);
operation_up parse_struct_expr (struct type *type);
operation_up parse_binop (bool required);
operation_up parse_range ();
operation_up parse_expr ();
operation_up parse_sizeof ();
operation_up parse_addr ();
operation_up parse_field (operation_up &&);
operation_up parse_index (operation_up &&);
std::vector<operation_up> parse_paren_args ();
operation_up parse_call (operation_up &&);
std::vector<struct type *> parse_type_list ();
std::vector<struct type *> parse_maybe_type_list ();
struct type *parse_array_type ();
struct type *parse_slice_type ();
struct type *parse_pointer_type ();
struct type *parse_function_type ();
struct type *parse_tuple_type ();
struct type *parse_type ();
std::string parse_path (bool for_expr);
operation_up parse_string ();
operation_up parse_tuple_struct (struct type *type);
operation_up parse_path_expr ();
operation_up parse_atom (bool required);
void update_innermost_block (struct block_symbol sym);
struct block_symbol lookup_symbol (const char *name,
const struct block *block,
const domain_search_flags domain);
struct type *rust_lookup_type (const char *name);
/* Clear some state. This is only used for testing. */
#if GDB_SELF_TEST
void reset (const char *input)
{
pstate->prev_lexptr = nullptr;
pstate->lexptr = input;
paren_depth = 0;
current_token = 0;
current_int_val = {};
current_float_val = {};
current_string_val = {};
current_opcode = OP_NULL;
}
#endif /* GDB_SELF_TEST */
/* Return the token's string value as a string. */
std::string get_string () const
{
return std::string (current_string_val.ptr, current_string_val.length);
}
/* A pointer to this is installed globally. */
auto_obstack obstack;
/* The parser state gdb gave us. */
struct parser_state *pstate;
/* Depth of parentheses. */
int paren_depth = 0;
/* The current token's type. */
int current_token = 0;
/* The current token's payload, if any. */
typed_val_int current_int_val {};
typed_val_float current_float_val {};
struct stoken current_string_val {};
enum exp_opcode current_opcode = OP_NULL;
/* When completing, this may be set to the field operation to
complete. */
operation_up completion_op;
};
/* Return an string referring to NAME, but relative to the crate's
name. */
std::string
rust_parser::crate_name (const std::string &name)
{
std::string crate = rust_crate_for_block (pstate->expression_context_block);
if (crate.empty ())
error (_("Could not find crate for current location"));
return "::" + crate + "::" + name;
}
/* Return a string referring to a "super::" qualified name. IDENT is
the base name and N_SUPERS is how many "super::"s were provided.
N_SUPERS can be zero. */
std::string
rust_parser::super_name (const std::string &ident, unsigned int n_supers)
{
const char *scope = "";
if (pstate->expression_context_block != nullptr)
scope = pstate->expression_context_block->scope ();
int offset;
if (scope[0] == '\0')
error (_("Couldn't find namespace scope for self::"));
if (n_supers > 0)
{
int len;
std::vector<int> offsets;
unsigned int current_len;
current_len = cp_find_first_component (scope);
while (scope[current_len] != '\0')
{
offsets.push_back (current_len);
gdb_assert (scope[current_len] == ':');
/* The "::". */
current_len += 2;
current_len += cp_find_first_component (scope
+ current_len);
}
len = offsets.size ();
if (n_supers >= len)
error (_("Too many super:: uses from '%s'"), scope);
offset = offsets[len - n_supers];
}
else
offset = strlen (scope);
return "::" + std::string (scope, offset) + "::" + ident;
}
/* A helper to appropriately munge NAME and BLOCK depending on the
presence of a leading "::". */
static void
munge_name_and_block (const char **name, const struct block **block)
{
/* If it is a global reference, skip the current block in favor of
the static block. */
if (startswith (*name, "::"))
{
*name += 2;
*block = (*block)->static_block ();
}
}
/* Like lookup_symbol, but handles Rust namespace conventions, and
doesn't require field_of_this_result. */
struct block_symbol
rust_parser::lookup_symbol (const char *name, const struct block *block,
const domain_search_flags domain)
{
struct block_symbol result;
munge_name_and_block (&name, &block);
result = ::lookup_symbol (name, block, domain, NULL);
if (result.symbol != NULL)
update_innermost_block (result);
return result;
}
/* Look up a type, following Rust namespace conventions. */
struct type *
rust_parser::rust_lookup_type (const char *name)
{
struct block_symbol result;
struct type *type;
const struct block *block = pstate->expression_context_block;
munge_name_and_block (&name, &block);
result = ::lookup_symbol (name, block, SEARCH_TYPE_DOMAIN, nullptr);
if (result.symbol != NULL)
{
update_innermost_block (result);
return result.symbol->type ();
}
type = lookup_typename (language (), name, NULL, 1);
if (type != NULL)
return type;
/* Last chance, try a built-in type. */
return language_lookup_primitive_type (language (), arch (), name);
}
/* A helper that updates the innermost block as appropriate. */
void
rust_parser::update_innermost_block (struct block_symbol sym)
{
if (symbol_read_needs_frame (sym.symbol))
pstate->block_tracker->update (sym);
}
/* Lex a hex number with at least MIN digits and at most MAX
digits. */
uint32_t
rust_parser::lex_hex (int min, int max)
{
uint32_t result = 0;
int len = 0;
/* We only want to stop at MAX if we're lexing a byte escape. */
int check_max = min == max;
while ((check_max ? len <= max : 1)
&& ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
|| (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
|| (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')))
{
result *= 16;
if (pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'f')
result = result + 10 + pstate->lexptr[0] - 'a';
else if (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'F')
result = result + 10 + pstate->lexptr[0] - 'A';
else
result = result + pstate->lexptr[0] - '0';
++pstate->lexptr;
++len;
}
if (len < min)
error (_("Not enough hex digits seen"));
if (len > max)
{
gdb_assert (min != max);
error (_("Overlong hex escape"));
}
return result;
}
/* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
otherwise we're lexing a character escape. */
uint32_t
rust_parser::lex_escape (bool is_byte)
{
uint32_t result;
gdb_assert (pstate->lexptr[0] == '\\');
++pstate->lexptr;
switch (pstate->lexptr[0])
{
case 'x':
++pstate->lexptr;
result = lex_hex (2, 2);
break;
case 'u':
if (is_byte)
error (_("Unicode escape in byte literal"));
++pstate->lexptr;
if (pstate->lexptr[0] != '{')
error (_("Missing '{' in Unicode escape"));
++pstate->lexptr;
result = lex_hex (1, 6);
/* Could do range checks here. */
if (pstate->lexptr[0] != '}')
error (_("Missing '}' in Unicode escape"));
++pstate->lexptr;
break;
case 'n':
result = '\n';
++pstate->lexptr;
break;
case 'r':
result = '\r';
++pstate->lexptr;
break;
case 't':
result = '\t';
++pstate->lexptr;
break;
case '\\':
result = '\\';
++pstate->lexptr;
break;
case '0':
result = '\0';
++pstate->lexptr;
break;
case '\'':
result = '\'';
++pstate->lexptr;
break;
case '"':
result = '"';
++pstate->lexptr;
break;
default:
error (_("Invalid escape \\%c in literal"), pstate->lexptr[0]);
}
return result;
}
/* A helper for lex_character. Search forward for the closing single
quote, then convert the bytes from the host charset to UTF-32. */
static uint32_t
lex_multibyte_char (const char *text, int *len)
{
/* Only look a maximum of 5 bytes for the closing quote. This is
the maximum for UTF-8. */
int quote;
gdb_assert (text[0] != '\'');
for (quote = 1; text[quote] != '\0' && text[quote] != '\''; ++quote)
;
*len = quote;
/* The caller will issue an error. */
if (text[quote] == '\0')
return 0;
auto_obstack result;
convert_between_encodings (host_charset (), HOST_UTF32,
(const gdb_byte *) text,
quote, 1, &result, translit_none);
int size = obstack_object_size (&result);
if (size > 4)
error (_("overlong character literal"));
uint32_t value;
memcpy (&value, obstack_finish (&result), size);
return value;
}
/* Lex a character constant. */
int
rust_parser::lex_character ()
{
bool is_byte = false;
uint32_t value;
if (pstate->lexptr[0] == 'b')
{
is_byte = true;
++pstate->lexptr;
}
gdb_assert (pstate->lexptr[0] == '\'');
++pstate->lexptr;
if (pstate->lexptr[0] == '\'')
error (_("empty character literal"));
else if (pstate->lexptr[0] == '\\')
value = lex_escape (is_byte);
else
{
int len;
value = lex_multibyte_char (&pstate->lexptr[0], &len);
pstate->lexptr += len;
}
if (pstate->lexptr[0] != '\'')
error (_("Unterminated character literal"));
++pstate->lexptr;
current_int_val.val = value;
current_int_val.type = get_type (is_byte ? "u8" : "char");
return INTEGER;
}
/* Return the offset of the double quote if STR looks like the start
of a raw string, or 0 if STR does not start a raw string. */
static int
starts_raw_string (const char *str)
{
const char *save = str;
if (str[0] != 'r')
return 0;
++str;
while (str[0] == '#')
++str;
if (str[0] == '"')
return str - save;
return 0;
}
/* Return true if STR looks like the end of a raw string that had N
hashes at the start. */
static bool
ends_raw_string (const char *str, int n)
{
gdb_assert (str[0] == '"');
for (int i = 0; i < n; ++i)
if (str[i + 1] != '#')
return false;
return true;
}
/* Lex a string constant. */
int
rust_parser::lex_string ()
{
int is_byte = pstate->lexptr[0] == 'b';
int raw_length;
if (is_byte)
++pstate->lexptr;
raw_length = starts_raw_string (pstate->lexptr);
pstate->lexptr += raw_length;
gdb_assert (pstate->lexptr[0] == '"');
++pstate->lexptr;
while (1)
{
uint32_t value;
if (raw_length > 0)
{
if (pstate->lexptr[0] == '"' && ends_raw_string (pstate->lexptr,
raw_length - 1))
{
/* Exit with lexptr pointing after the final "#". */
pstate->lexptr += raw_length;
break;
}
else if (pstate->lexptr[0] == '\0')
error (_("Unexpected EOF in string"));
value = pstate->lexptr[0] & 0xff;
if (is_byte && value > 127)
error (_("Non-ASCII value in raw byte string"));
obstack_1grow (&obstack, value);
++pstate->lexptr;
}
else if (pstate->lexptr[0] == '"')
{
/* Make sure to skip the quote. */
++pstate->lexptr;
break;
}
else if (pstate->lexptr[0] == '\\')
{
value = lex_escape (is_byte);
if (is_byte)
obstack_1grow (&obstack, value);
else
convert_between_encodings (HOST_UTF32, "UTF-8",
(gdb_byte *) &value,
sizeof (value), sizeof (value),
&obstack, translit_none);
}
else if (pstate->lexptr[0] == '\0')
error (_("Unexpected EOF in string"));
else
{
value = pstate->lexptr[0] & 0xff;
if (is_byte && value > 127)
error (_("Non-ASCII value in byte string"));
obstack_1grow (&obstack, value);
++pstate->lexptr;
}
}
current_string_val.length = obstack_object_size (&obstack);
current_string_val.ptr = (const char *) obstack_finish (&obstack);
return is_byte ? BYTESTRING : STRING;
}
/* Return true if STRING starts with whitespace followed by a digit. */
static bool
space_then_number (const char *string)
{
const char *p = string;
while (p[0] == ' ' || p[0] == '\t')
++p;
if (p == string)
return false;
return *p >= '0' && *p <= '9';
}
/* Return true if C can start an identifier. */
static bool
rust_identifier_start_p (char c)
{
return ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| c == '$'
/* Allow any non-ASCII character as an identifier. There
doesn't seem to be a need to be picky about this. */
|| (c & 0x80) != 0);
}
/* Lex an identifier. */
int
rust_parser::lex_identifier ()
{
unsigned int length;
const struct token_info *token;
int is_gdb_var = pstate->lexptr[0] == '$';
bool is_raw = false;
if (pstate->lexptr[0] == 'r'
&& pstate->lexptr[1] == '#'
&& rust_identifier_start_p (pstate->lexptr[2]))
{
is_raw = true;
pstate->lexptr += 2;
}
const char *start = pstate->lexptr;
gdb_assert (rust_identifier_start_p (pstate->lexptr[0]));
++pstate->lexptr;
/* Allow any non-ASCII character here. This "handles" UTF-8 by
passing it through. */
while ((pstate->lexptr[0] >= 'a' && pstate->lexptr[0] <= 'z')
|| (pstate->lexptr[0] >= 'A' && pstate->lexptr[0] <= 'Z')
|| pstate->lexptr[0] == '_'
|| (is_gdb_var && pstate->lexptr[0] == '$')
|| (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
|| (pstate->lexptr[0] & 0x80) != 0)
++pstate->lexptr;
length = pstate->lexptr - start;
token = NULL;
if (!is_raw)
{
for (const auto &candidate : identifier_tokens)
{
if (length == strlen (candidate.name)
&& strncmp (candidate.name, start, length) == 0)
{
token = &candidate;
break;
}
}
}
if (token != NULL)
{
if (token->value == 0)
{
/* Leave the terminating token alone. */
pstate->lexptr = start;
return 0;
}
}
else if (token == NULL
&& !is_raw
&& (strncmp (start, "thread", length) == 0
|| strncmp (start, "task", length) == 0)
&& space_then_number (pstate->lexptr))
{
/* "task" or "thread" followed by a number terminates the
parse, per gdb rules. */
pstate->lexptr = start;
return 0;
}
if (token == NULL || (pstate->parse_completion && pstate->lexptr[0] == '\0'))
{
current_string_val.length = length;
current_string_val.ptr = start;
}
if (pstate->parse_completion && pstate->lexptr[0] == '\0')
{
/* Prevent rustyylex from returning two COMPLETE tokens. */
pstate->prev_lexptr = pstate->lexptr;
return COMPLETE;
}
if (token != NULL)
return token->value;
if (is_gdb_var)
return GDBVAR;
return IDENT;
}
/* Lex an operator. */
int
rust_parser::lex_operator ()
{
const struct token_info *token = NULL;
for (const auto &candidate : operator_tokens)
{
if (strncmp (candidate.name, pstate->lexptr,
strlen (candidate.name)) == 0)
{
pstate->lexptr += strlen (candidate.name);
token = &candidate;
break;
}
}
if (token != NULL)
{
current_opcode = token->opcode;
return token->value;
}
return *pstate->lexptr++;
}
/* Lex a decimal integer. */
int
rust_parser::lex_decimal_integer ()
{
gdb_assert (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9');
std::string copy;
while (pstate->lexptr[0] >= '0' && pstate->lexptr[0] <= '9')
{
copy.push_back (pstate->lexptr[0]);
++pstate->lexptr;
}
/* No need to set the value's type in this situation. */
current_int_val.val.set (copy.c_str (), 10);
return DECIMAL_INTEGER;
}
/* Lex a number. */
int
rust_parser::lex_number ()
{
regmatch_t subexps[NUM_SUBEXPRESSIONS];
int match;
bool is_integer = false;
bool implicit_i32 = false;
const char *type_name = NULL;
struct type *type;
int end_index;
int type_index = -1;
match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps),
subexps, 0);
/* Failure means the regexp is broken. */
gdb_assert (match == 0);
if (subexps[INT_TEXT].rm_so != -1)
{
/* Integer part matched. */
is_integer = true;
end_index = subexps[INT_TEXT].rm_eo;
if (subexps[INT_TYPE].rm_so == -1)
{
type_name = "i32";
implicit_i32 = true;
}
else
type_index = INT_TYPE;
}
else if (subexps[FLOAT_TYPE1].rm_so != -1)
{
/* Found floating point type suffix. */
end_index = subexps[FLOAT_TYPE1].rm_so;
type_index = FLOAT_TYPE1;
}
else if (subexps[FLOAT_TYPE2].rm_so != -1)
{
/* Found floating point type suffix. */
end_index = subexps[FLOAT_TYPE2].rm_so;
type_index = FLOAT_TYPE2;
}
else
{
/* Any other floating point match. */
end_index = subexps[0].rm_eo;
type_name = "f64";
}
/* We need a special case if the final character is ".". In this
case we might need to parse an integer. For example, "23.f()" is
a request for a trait method call, not a syntax error involving
the floating point number "23.". */
gdb_assert (subexps[0].rm_eo > 0);
if (pstate->lexptr[subexps[0].rm_eo - 1] == '.')
{
const char *next = skip_spaces (&pstate->lexptr[subexps[0].rm_eo]);
if (rust_identifier_start_p (*next) || *next == '.')
{
--subexps[0].rm_eo;
is_integer = true;
end_index = subexps[0].rm_eo;
type_name = "i32";
implicit_i32 = true;
}
}
/* Compute the type name if we haven't already. */
std::string type_name_holder;
if (type_name == NULL)
{
gdb_assert (type_index != -1);
type_name_holder = std::string ((pstate->lexptr