Line data Source code
1 : // Copyright (C) 2025-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : /* DO NOT INCLUDE ANYWHERE - this is automatically included
20 : * by rust-parse-impl.h
21 : * This is also the reason why there are no include guards. */
22 :
23 : #include "rust-parse.h"
24 :
25 : namespace Rust {
26 :
27 : // Parses a semi-coloned (except for full block) macro invocation item.
28 : template <typename ManagedTokenSource>
29 : std::unique_ptr<AST::MacroInvocation>
30 37373 : Parser<ManagedTokenSource>::parse_macro_invocation_semi (
31 : AST::AttrVec outer_attrs)
32 : {
33 37373 : location_t macro_locus = lexer.peek_token ()->get_locus ();
34 37373 : auto path = parse_simple_path ();
35 37373 : if (!path)
36 : {
37 1 : rust_error_at (lexer.peek_token ()->get_locus (),
38 : "failed to parse macro invocation path");
39 1 : return nullptr;
40 : }
41 :
42 37372 : if (!skip_token (EXCLAM))
43 : {
44 : // skip after somewhere?
45 0 : return nullptr;
46 : }
47 :
48 : // save delim type to ensure it is reused later
49 37372 : AST::DelimType delim_type = AST::PARENS;
50 :
51 : // Map tokens to DelimType
52 37372 : const_TokenPtr t = lexer.peek_token ();
53 37372 : switch (t->get_id ())
54 : {
55 : case LEFT_PAREN:
56 : delim_type = AST::PARENS;
57 : break;
58 0 : case LEFT_SQUARE:
59 0 : delim_type = AST::SQUARE;
60 0 : break;
61 26635 : case LEFT_CURLY:
62 26635 : delim_type = AST::CURLY;
63 26635 : break;
64 0 : default:
65 0 : add_error (Error (t->get_locus (),
66 : "unexpected token %qs - expecting delimiters (for a "
67 : "macro invocation semi body)",
68 : t->get_token_description ()));
69 :
70 0 : return nullptr;
71 : }
72 37372 : location_t tok_tree_locus = t->get_locus ();
73 37372 : lexer.skip_token ();
74 :
75 : // parse actual token trees
76 37372 : std::vector<std::unique_ptr<AST::TokenTree>> token_trees;
77 37372 : auto delim_open
78 37372 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
79 37372 : token_trees.push_back (std::move (delim_open));
80 :
81 37372 : t = lexer.peek_token ();
82 : // parse token trees until the initial delimiter token is found again
83 486086 : while (!Parse::Utils::token_id_matches_delims (t->get_id (), delim_type)
84 486084 : && t->get_id () != END_OF_FILE)
85 : {
86 448714 : auto tree = parse_token_tree ();
87 448714 : if (!tree)
88 2 : return nullptr;
89 :
90 448712 : token_trees.push_back (std::move (tree.value ()));
91 :
92 448712 : t = lexer.peek_token ();
93 : }
94 37370 : auto delim_close
95 37370 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
96 37370 : token_trees.push_back (std::move (delim_close));
97 :
98 37370 : AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees),
99 : tok_tree_locus);
100 37370 : AST::MacroInvocData invoc_data (std::move (path.value ()),
101 : std::move (delim_tok_tree));
102 :
103 : // parse end delimiters
104 37370 : t = lexer.peek_token ();
105 37370 : if (Parse::Utils::token_id_matches_delims (t->get_id (), delim_type))
106 : {
107 : // tokens match opening delimiter, so skip.
108 37370 : lexer.skip_token ();
109 :
110 37370 : if (delim_type != AST::CURLY)
111 : {
112 : // skip semicolon at end of non-curly macro invocation semis
113 10735 : if (!skip_token (SEMICOLON))
114 : {
115 : // as this is the end, allow recovery (probably) - may change
116 :
117 0 : return AST::MacroInvocation::Regular (std::move (invoc_data),
118 : std::move (outer_attrs),
119 0 : macro_locus, true);
120 : }
121 : }
122 :
123 : // DEBUG:
124 37370 : rust_debug ("skipped token is '%s', next token (current peek) is '%s'",
125 : t->get_token_description (),
126 : lexer.peek_token ()->get_token_description ());
127 :
128 74740 : return AST::MacroInvocation::Regular (std::move (invoc_data),
129 : std::move (outer_attrs),
130 37370 : macro_locus, true);
131 : }
132 : else
133 : {
134 : // tokens don't match opening delimiters, so produce error
135 0 : Error error (t->get_locus (),
136 : "unexpected token %qs - expecting closing delimiter %qs "
137 : "(for a macro invocation semi)",
138 : t->get_token_description (),
139 : (delim_type == AST::PARENS
140 : ? ")"
141 : : (delim_type == AST::SQUARE ? "]" : "}")));
142 0 : add_error (std::move (error));
143 :
144 : /* return empty macro invocation despite possibly parsing mostly valid one
145 : * - TODO is this a good idea? */
146 0 : return nullptr;
147 0 : }
148 74744 : }
149 :
150 : // Parses a non-semicoloned macro invocation (i.e. as pattern or expression).
151 : template <typename ManagedTokenSource>
152 : std::unique_ptr<AST::MacroInvocation>
153 288900 : Parser<ManagedTokenSource>::parse_macro_invocation (AST::AttrVec outer_attrs)
154 : {
155 : // parse macro path
156 288900 : auto macro_path = parse_simple_path ();
157 288900 : if (!macro_path)
158 : {
159 254137 : Error error (lexer.peek_token ()->get_locus (),
160 : "failed to parse macro invocation path");
161 254137 : add_error (std::move (error));
162 :
163 : // skip?
164 254137 : return nullptr;
165 254137 : }
166 :
167 34763 : if (!skip_token (EXCLAM))
168 : {
169 : // skip after somewhere?
170 3 : return nullptr;
171 : }
172 :
173 : // parse internal delim token tree
174 34760 : auto delim_tok_tree = parse_delim_token_tree ();
175 34760 : if (!delim_tok_tree)
176 0 : return nullptr;
177 :
178 34760 : location_t macro_locus = macro_path->get_locus ();
179 :
180 34760 : return AST::MacroInvocation::Regular (
181 69520 : AST::MacroInvocData (std::move (macro_path.value ()),
182 34760 : std::move (delim_tok_tree.value ())),
183 34760 : std::move (outer_attrs), macro_locus);
184 34760 : }
185 :
186 : // Parses a macro rule definition - does not parse semicolons.
187 : template <typename ManagedTokenSource>
188 : AST::MacroRule
189 7274 : Parser<ManagedTokenSource>::parse_macro_rule ()
190 : {
191 7274 : location_t locus = lexer.peek_token ()->get_locus ();
192 :
193 : // parse macro matcher
194 7274 : AST::MacroMatcher matcher = parse_macro_matcher ();
195 :
196 7274 : if (matcher.is_error ())
197 13 : return AST::MacroRule::create_error (locus);
198 :
199 7261 : if (!skip_token (MATCH_ARROW))
200 : {
201 : // skip after somewhere?
202 0 : return AST::MacroRule::create_error (locus);
203 : }
204 :
205 : // parse transcriber (this is just a delim token tree)
206 7261 : location_t token_tree_loc = lexer.peek_token ()->get_locus ();
207 7261 : auto delim_token_tree = parse_delim_token_tree ();
208 7261 : if (!delim_token_tree)
209 0 : return AST::MacroRule::create_error (token_tree_loc);
210 :
211 7261 : AST::MacroTranscriber transcriber (delim_token_tree.value (), token_tree_loc);
212 :
213 7261 : return AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
214 7261 : }
215 :
216 : // Parses a macro matcher (part of a macro rule definition).
217 : template <typename ManagedTokenSource>
218 : AST::MacroMatcher
219 7853 : Parser<ManagedTokenSource>::parse_macro_matcher ()
220 : {
221 : // save delim type to ensure it is reused later
222 7853 : AST::DelimType delim_type = AST::PARENS;
223 :
224 : // DEBUG
225 7853 : rust_debug ("begun parsing macro matcher");
226 :
227 : // Map tokens to DelimType
228 7853 : const_TokenPtr t = lexer.peek_token ();
229 7853 : location_t locus = t->get_locus ();
230 7853 : switch (t->get_id ())
231 : {
232 : case LEFT_PAREN:
233 : delim_type = AST::PARENS;
234 : break;
235 258 : case LEFT_SQUARE:
236 258 : delim_type = AST::SQUARE;
237 258 : break;
238 88 : case LEFT_CURLY:
239 88 : delim_type = AST::CURLY;
240 88 : break;
241 1 : default:
242 1 : add_error (Error (
243 : t->get_locus (),
244 : "unexpected token %qs - expecting delimiters (for a macro matcher)",
245 : t->get_token_description ()));
246 :
247 1 : return AST::MacroMatcher::create_error (t->get_locus ());
248 : }
249 7852 : lexer.skip_token ();
250 :
251 : // parse actual macro matches
252 7852 : std::vector<std::unique_ptr<AST::MacroMatch>> matches;
253 : // Set of possible preceding macro matches to make sure follow-set
254 : // restrictions are respected.
255 : // TODO: Consider using std::reference_wrapper instead of raw pointers?
256 7852 : std::vector<const AST::MacroMatch *> last_matches;
257 :
258 7852 : t = lexer.peek_token ();
259 : // parse token trees until the initial delimiter token is found again
260 33223 : while (!Parse::Utils::token_id_matches_delims (t->get_id (), delim_type))
261 : {
262 25371 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
263 :
264 25371 : if (match == nullptr)
265 : {
266 1 : Error error (
267 : t->get_locus (),
268 : "failed to parse macro match for macro matcher - found %qs",
269 : t->get_token_description ());
270 1 : add_error (std::move (error));
271 :
272 1 : return AST::MacroMatcher::create_error (t->get_locus ());
273 1 : }
274 :
275 25370 : if (matches.size () > 0)
276 : {
277 18042 : const auto *last_match = matches.back ().get ();
278 :
279 : // We want to check if we are dealing with a zeroable repetition
280 18042 : bool zeroable = false;
281 18042 : if (last_match->get_macro_match_type ()
282 : == AST::MacroMatch::MacroMatchType::Repetition)
283 : {
284 178 : auto repetition
285 : = static_cast<const AST::MacroMatchRepetition *> (last_match);
286 :
287 178 : if (repetition->get_op ()
288 : != AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE)
289 : zeroable = true;
290 : }
291 :
292 : if (!zeroable)
293 18042 : last_matches.clear ();
294 :
295 18042 : last_matches.emplace_back (last_match);
296 :
297 36172 : for (auto last : last_matches)
298 18142 : if (!is_match_compatible (*last, *match))
299 : return AST::MacroMatcher::create_error (
300 12 : match->get_match_locus ());
301 : }
302 :
303 25358 : matches.push_back (std::move (match));
304 :
305 : // DEBUG
306 25358 : rust_debug ("pushed back a match in macro matcher");
307 :
308 25358 : t = lexer.peek_token ();
309 : }
310 :
311 : // parse end delimiters
312 7839 : t = lexer.peek_token ();
313 7839 : if (Parse::Utils::token_id_matches_delims (t->get_id (), delim_type))
314 : {
315 : // tokens match opening delimiter, so skip.
316 7839 : lexer.skip_token ();
317 :
318 7839 : return AST::MacroMatcher (delim_type, std::move (matches), locus);
319 : }
320 : else
321 : {
322 : // tokens don't match opening delimiters, so produce error
323 0 : Error error (t->get_locus (),
324 : "unexpected token %qs - expecting closing delimiter %qs "
325 : "(for a macro matcher)",
326 : t->get_token_description (),
327 : (delim_type == AST::PARENS
328 : ? ")"
329 : : (delim_type == AST::SQUARE ? "]" : "}")));
330 0 : add_error (std::move (error));
331 :
332 : /* return error macro matcher despite possibly parsing mostly correct one?
333 : * TODO is this the best idea? */
334 0 : return AST::MacroMatcher::create_error (t->get_locus ());
335 0 : }
336 7852 : }
337 :
338 : // Parses a macro match (syntax match inside a matcher in a macro rule).
339 : template <typename ManagedTokenSource>
340 : std::unique_ptr<AST::MacroMatch>
341 28093 : Parser<ManagedTokenSource>::parse_macro_match ()
342 : {
343 : // branch based on token available
344 28093 : const_TokenPtr t = lexer.peek_token ();
345 28093 : switch (t->get_id ())
346 : {
347 398 : case LEFT_PAREN:
348 : case LEFT_SQUARE:
349 : case LEFT_CURLY:
350 : {
351 : // must be macro matcher as delimited
352 398 : AST::MacroMatcher matcher = parse_macro_matcher ();
353 398 : if (matcher.is_error ())
354 : {
355 1 : Error error (lexer.peek_token ()->get_locus (),
356 : "failed to parse macro matcher in macro match");
357 1 : add_error (std::move (error));
358 :
359 1 : return nullptr;
360 1 : }
361 397 : return std::unique_ptr<AST::MacroMatcher> (
362 397 : new AST::MacroMatcher (std::move (matcher)));
363 398 : }
364 17614 : case DOLLAR_SIGN:
365 : {
366 : // have to do more lookahead to determine if fragment or repetition
367 17614 : const_TokenPtr t2 = lexer.peek_token (1);
368 17614 : switch (t2->get_id ())
369 : {
370 15515 : case IDENTIFIER:
371 : case UNDERSCORE:
372 : // macro fragment
373 15515 : return parse_macro_match_fragment ();
374 1994 : case LEFT_PAREN:
375 : // macro repetition
376 1994 : return parse_macro_match_repetition ();
377 105 : default:
378 105 : if (token_id_is_keyword (t2->get_id ()) && t2->get_id () != CRATE)
379 : {
380 : // keyword as macro fragment
381 105 : return parse_macro_match_fragment ();
382 : }
383 : else
384 : {
385 : // error: unrecognised
386 0 : add_error (Error (
387 : t2->get_locus (),
388 : "unrecognised token combination %<$%s%> at start of "
389 : "macro match - did you mean %<$identifier%> or %<$(%>?",
390 : t2->get_token_description ()));
391 :
392 : // skip somewhere?
393 0 : return nullptr;
394 : }
395 : }
396 17614 : }
397 0 : case RIGHT_PAREN:
398 : case RIGHT_SQUARE:
399 : case RIGHT_CURLY:
400 : // not allowed
401 0 : add_error (Error (
402 : t->get_locus (),
403 : "closing delimiters like %qs are not allowed at the start of a macro "
404 : "match",
405 : t->get_token_description ()));
406 :
407 : // skip somewhere?
408 0 : return nullptr;
409 10081 : default:
410 : // just the token
411 10081 : lexer.skip_token ();
412 10081 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
413 : }
414 28093 : }
415 :
416 : // Parses a fragment macro match.
417 : template <typename ManagedTokenSource>
418 : std::unique_ptr<AST::MacroMatchFragment>
419 15620 : Parser<ManagedTokenSource>::parse_macro_match_fragment ()
420 : {
421 15620 : location_t fragment_locus = lexer.peek_token ()->get_locus ();
422 15620 : skip_token (DOLLAR_SIGN);
423 :
424 15620 : Identifier ident;
425 15620 : auto identifier = lexer.peek_token ();
426 15620 : if (identifier->get_id () == UNDERSCORE)
427 20 : ident = {Values::Keywords::UNDERSCORE, identifier->get_locus ()};
428 : else
429 31220 : ident = {identifier};
430 :
431 15620 : if (ident.empty ())
432 : {
433 0 : Error error (lexer.peek_token ()->get_locus (),
434 : "missing identifier in macro match fragment");
435 0 : add_error (std::move (error));
436 :
437 0 : return nullptr;
438 0 : }
439 15620 : skip_token (identifier->get_id ());
440 :
441 15620 : if (!skip_token (COLON))
442 : {
443 : // skip after somewhere?
444 0 : return nullptr;
445 : }
446 :
447 : // get MacroFragSpec for macro
448 15620 : const_TokenPtr t = expect_token (IDENTIFIER);
449 15620 : if (t == nullptr)
450 0 : return nullptr;
451 :
452 15620 : AST::MacroFragSpec frag
453 15620 : = AST::MacroFragSpec::get_frag_spec_from_str (t->get_str ());
454 15620 : if (frag.is_error ())
455 : {
456 0 : Error error (t->get_locus (),
457 : "invalid fragment specifier %qs in fragment macro match",
458 0 : t->get_str ().c_str ());
459 0 : add_error (std::move (error));
460 :
461 0 : return nullptr;
462 0 : }
463 :
464 : return std::unique_ptr<AST::MacroMatchFragment> (
465 15620 : new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
466 31240 : }
467 :
468 : // Parses a repetition macro match.
469 : template <typename ManagedTokenSource>
470 : std::unique_ptr<AST::MacroMatchRepetition>
471 1994 : Parser<ManagedTokenSource>::parse_macro_match_repetition ()
472 : {
473 1994 : skip_token (DOLLAR_SIGN);
474 1994 : skip_token (LEFT_PAREN);
475 :
476 1994 : std::vector<std::unique_ptr<AST::MacroMatch>> matches;
477 :
478 : // parse required first macro match
479 1994 : std::unique_ptr<AST::MacroMatch> initial_match = parse_macro_match ();
480 1994 : if (initial_match == nullptr)
481 : {
482 0 : Error error (
483 0 : lexer.peek_token ()->get_locus (),
484 : "could not parse required first macro match in macro match repetition");
485 0 : add_error (std::move (error));
486 :
487 : // skip after somewhere?
488 0 : return nullptr;
489 0 : }
490 1994 : matches.push_back (std::move (initial_match));
491 :
492 : // parse optional later macro matches
493 1994 : const_TokenPtr t = lexer.peek_token ();
494 2722 : while (t->get_id () != RIGHT_PAREN)
495 : {
496 728 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
497 :
498 728 : if (match == nullptr)
499 : {
500 0 : Error error (lexer.peek_token ()->get_locus (),
501 : "failed to parse macro match in macro match repetition");
502 0 : add_error (std::move (error));
503 :
504 0 : return nullptr;
505 0 : }
506 :
507 728 : matches.push_back (std::move (match));
508 :
509 728 : t = lexer.peek_token ();
510 : }
511 :
512 1994 : if (!skip_token (RIGHT_PAREN))
513 : {
514 : // skip after somewhere?
515 0 : return nullptr;
516 : }
517 :
518 1994 : t = lexer.peek_token ();
519 : // see if separator token exists
520 1994 : std::unique_ptr<AST::Token> separator = nullptr;
521 1994 : switch (t->get_id ())
522 : {
523 : // repetition operators
524 : case ASTERISK:
525 : case PLUS:
526 : case QUESTION_MARK:
527 : // delimiters
528 : case LEFT_PAREN:
529 : case LEFT_CURLY:
530 : case LEFT_SQUARE:
531 : case RIGHT_PAREN:
532 : case RIGHT_CURLY:
533 : case RIGHT_SQUARE:
534 : // separator does not exist, so still null and don't skip token
535 : break;
536 387 : default:
537 : // separator does exist
538 387 : separator = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
539 387 : lexer.skip_token ();
540 387 : break;
541 : }
542 :
543 : // parse repetition operator
544 1994 : t = lexer.peek_token ();
545 1994 : AST::MacroMatchRepetition::MacroRepOp op = AST::MacroMatchRepetition::NONE;
546 1994 : switch (t->get_id ())
547 : {
548 1420 : case ASTERISK:
549 1420 : op = AST::MacroMatchRepetition::ANY;
550 1420 : lexer.skip_token ();
551 1420 : break;
552 363 : case PLUS:
553 363 : op = AST::MacroMatchRepetition::ONE_OR_MORE;
554 363 : lexer.skip_token ();
555 363 : break;
556 211 : case QUESTION_MARK:
557 211 : op = AST::MacroMatchRepetition::ZERO_OR_ONE;
558 211 : lexer.skip_token ();
559 :
560 211 : if (separator != nullptr)
561 : {
562 1 : add_error (
563 2 : Error (separator->get_locus (),
564 : "the %<?%> macro repetition operator does not take a "
565 : "separator"));
566 1994 : separator = nullptr;
567 : }
568 :
569 : break;
570 0 : default:
571 0 : add_error (
572 0 : Error (t->get_locus (),
573 : "expected macro repetition operator (%<*%>, %<+%>, or %<?%>) in "
574 : "macro match - found %qs",
575 : t->get_token_description ()));
576 :
577 : // skip after somewhere?
578 0 : return nullptr;
579 : }
580 :
581 : return std::unique_ptr<AST::MacroMatchRepetition> (
582 1994 : new AST::MacroMatchRepetition (std::move (matches), op,
583 1994 : std::move (separator), t->get_locus ()));
584 3988 : }
585 :
586 : /* Parses a macro invocation with a path in expression already parsed (but not
587 : * '!' token). */
588 : template <typename ManagedTokenSource>
589 : std::unique_ptr<AST::MacroInvocation>
590 425988 : Parser<ManagedTokenSource>::parse_macro_invocation_partial (
591 : AST::PathInExpression path, AST::AttrVec outer_attrs,
592 : ParseRestrictions restrictions)
593 : {
594 : // macro invocation
595 425988 : if (!skip_token (EXCLAM))
596 : {
597 0 : return nullptr;
598 : }
599 :
600 : // convert PathInExpression to SimplePath - if this isn't possible, error
601 425988 : AST::SimplePath converted_path = path.as_simple_path ();
602 425988 : if (converted_path.is_empty ())
603 : {
604 0 : Error error (lexer.peek_token ()->get_locus (),
605 : "failed to parse simple path in macro invocation");
606 0 : add_error (std::move (error));
607 :
608 0 : return nullptr;
609 0 : }
610 :
611 425988 : auto tok_tree = parse_delim_token_tree ();
612 425988 : if (!tok_tree)
613 3 : return nullptr;
614 :
615 425985 : rust_debug ("successfully parsed macro invocation (via partial)");
616 :
617 425985 : location_t macro_locus = converted_path.get_locus ();
618 :
619 425985 : return AST::MacroInvocation::Regular (
620 851970 : AST::MacroInvocData (std::move (converted_path),
621 425985 : std::move (tok_tree.value ())),
622 425985 : std::move (outer_attrs), macro_locus);
623 425988 : }
624 :
625 : } // namespace Rust
|