Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : : /* Template implementation for Rust::Parser. Previously in rust-parse.cc (before
20 : : * Parser was template). Separated from rust-parse.h for readability. */
21 : :
22 : : /* DO NOT INCLUDE ANYWHERE - this is automatically included
23 : : * by rust-parse-impl-*.cc
24 : : * This is also the reason why there are no include guards. */
25 : :
26 : : #include "expected.h"
27 : : #include "rust-ast.h"
28 : : #include "rust-common.h"
29 : : #include "rust-expr.h"
30 : : #include "rust-item.h"
31 : : #include "rust-common.h"
32 : : #include "rust-parse.h"
33 : : #include "rust-token.h"
34 : : #define INCLUDE_ALGORITHM
35 : : #include "rust-diagnostics.h"
36 : : #include "rust-dir-owner.h"
37 : : #include "rust-attribute-values.h"
38 : : #include "rust-keyword-values.h"
39 : : #include "rust-edition.h"
40 : :
41 : : #include "optional.h"
42 : :
43 : : namespace Rust {
44 : : // Left binding powers of operations.
45 : : enum binding_powers
46 : : {
47 : : // Highest priority
48 : : LBP_HIGHEST = 100,
49 : :
50 : : LBP_PATH = 95,
51 : :
52 : : LBP_METHOD_CALL = 90,
53 : :
54 : : LBP_FIELD_EXPR = 85,
55 : :
56 : : LBP_FUNCTION_CALL = 80,
57 : : LBP_ARRAY_REF = LBP_FUNCTION_CALL,
58 : :
59 : : LBP_QUESTION_MARK = 75, // unary postfix - counts as left
60 : :
61 : : LBP_UNARY_PLUS = 70, // Used only when the null denotation is +
62 : : LBP_UNARY_MINUS = LBP_UNARY_PLUS, // Used only when the null denotation is -
63 : : LBP_UNARY_ASTERISK = LBP_UNARY_PLUS, // deref operator - unary prefix
64 : : LBP_UNARY_EXCLAM = LBP_UNARY_PLUS,
65 : : LBP_UNARY_AMP = LBP_UNARY_PLUS,
66 : : LBP_UNARY_AMP_MUT = LBP_UNARY_PLUS,
67 : :
68 : : LBP_AS = 65,
69 : :
70 : : LBP_MUL = 60,
71 : : LBP_DIV = LBP_MUL,
72 : : LBP_MOD = LBP_MUL,
73 : :
74 : : LBP_PLUS = 55,
75 : : LBP_MINUS = LBP_PLUS,
76 : :
77 : : LBP_L_SHIFT = 50,
78 : : LBP_R_SHIFT = LBP_L_SHIFT,
79 : :
80 : : LBP_AMP = 45,
81 : :
82 : : LBP_CARET = 40,
83 : :
84 : : LBP_PIPE = 35,
85 : :
86 : : LBP_EQUAL = 30,
87 : : LBP_NOT_EQUAL = LBP_EQUAL,
88 : : LBP_SMALLER_THAN = LBP_EQUAL,
89 : : LBP_SMALLER_EQUAL = LBP_EQUAL,
90 : : LBP_GREATER_THAN = LBP_EQUAL,
91 : : LBP_GREATER_EQUAL = LBP_EQUAL,
92 : :
93 : : LBP_LOGICAL_AND = 25,
94 : :
95 : : LBP_LOGICAL_OR = 20,
96 : :
97 : : LBP_DOT_DOT = 15,
98 : : LBP_DOT_DOT_EQ = LBP_DOT_DOT,
99 : :
100 : : // TODO: note all these assig operators are RIGHT associative!
101 : : LBP_ASSIG = 10,
102 : : LBP_PLUS_ASSIG = LBP_ASSIG,
103 : : LBP_MINUS_ASSIG = LBP_ASSIG,
104 : : LBP_MULT_ASSIG = LBP_ASSIG,
105 : : LBP_DIV_ASSIG = LBP_ASSIG,
106 : : LBP_MOD_ASSIG = LBP_ASSIG,
107 : : LBP_AMP_ASSIG = LBP_ASSIG,
108 : : LBP_PIPE_ASSIG = LBP_ASSIG,
109 : : LBP_CARET_ASSIG = LBP_ASSIG,
110 : : LBP_L_SHIFT_ASSIG = LBP_ASSIG,
111 : : LBP_R_SHIFT_ASSIG = LBP_ASSIG,
112 : :
113 : : // return, break, and closures as lowest priority?
114 : : LBP_RETURN = 5,
115 : : LBP_BREAK = LBP_RETURN,
116 : : LBP_CLOSURE = LBP_RETURN, // unary prefix operators
117 : :
118 : : #if 0
119 : : // rust precedences
120 : : // used for closures
121 : : PREC_CLOSURE = -40,
122 : : // used for break, continue, return, and yield
123 : : PREC_JUMP = -30,
124 : : // used for range (although weird comment in rustc about this)
125 : : PREC_RANGE = -10,
126 : : // used for binary operators mentioned below - also cast, colon (type),
127 : : // assign, assign_op
128 : : PREC_BINOP = FROM_ASSOC_OP,
129 : : // used for box, address_of, let, unary (again, weird comment on let)
130 : : PREC_PREFIX = 50,
131 : : // used for await, call, method call, field, index, try,
132 : : // inline asm, macro invocation
133 : : PREC_POSTFIX = 60,
134 : : // used for array, repeat, tuple, literal, path, paren, if,
135 : : // while, for, 'loop', match, block, try block, async, struct
136 : : PREC_PAREN = 99,
137 : : PREC_FORCE_PAREN = 100,
138 : : #endif
139 : :
140 : : // lowest priority
141 : : LBP_LOWEST = 0
142 : : };
143 : :
144 : : /* Returns whether the token can start a type (i.e. there is a valid type
145 : : * beginning with the token). */
146 : : inline bool
147 : 815 : can_tok_start_type (TokenId id)
148 : : {
149 : 815 : switch (id)
150 : : {
151 : : case EXCLAM:
152 : : case LEFT_SQUARE:
153 : : case LEFT_ANGLE:
154 : : case UNDERSCORE:
155 : : case ASTERISK:
156 : : case AMP:
157 : : case LIFETIME:
158 : : case IDENTIFIER:
159 : : case SUPER:
160 : : case SELF:
161 : : case SELF_ALIAS:
162 : : case CRATE:
163 : : case DOLLAR_SIGN:
164 : : case SCOPE_RESOLUTION:
165 : : case LEFT_PAREN:
166 : : case FOR:
167 : : case ASYNC:
168 : : case CONST:
169 : : case UNSAFE:
170 : : case EXTERN_KW:
171 : : case FN_KW:
172 : : case IMPL:
173 : : case DYN:
174 : : case QUESTION_MARK:
175 : : return true;
176 : 395 : default:
177 : 395 : return false;
178 : : }
179 : : }
180 : :
181 : : /* Returns whether the token id is (or is likely to be) a right angle bracket.
182 : : * i.e. '>', '>>', '>=' and '>>=' tokens. */
183 : : inline bool
184 : 79924 : is_right_angle_tok (TokenId id)
185 : : {
186 : 79924 : switch (id)
187 : : {
188 : : case RIGHT_ANGLE:
189 : : case RIGHT_SHIFT:
190 : : case GREATER_OR_EQUAL:
191 : : case RIGHT_SHIFT_EQ:
192 : : return true;
193 : 52438 : default:
194 : 13009 : return false;
195 : : }
196 : : }
197 : :
198 : : /* HACK-y special handling for skipping a right angle token at the end of
199 : : * generic arguments.
200 : : * Currently, this replaces the "current token" with one that is identical
201 : : * except has the leading '>' removed (e.g. '>>' becomes '>'). This is bad
202 : : * for several reasons - it modifies the token stream to something that
203 : : * actually doesn't make syntactic sense, it may not worked if the token
204 : : * has already been skipped, etc. It was done because it would not
205 : : * actually require inserting new items into the token stream (which I
206 : : * thought would take more work to not mess up) and because I wasn't sure
207 : : * if the "already seen right angle" flag in the parser would work
208 : : * correctly.
209 : : * Those two other approaches listed are in my opinion actually better
210 : : * long-term - insertion is probably best as it reflects syntactically
211 : : * what occurs. On the other hand, I need to do a code audit to make sure
212 : : * that insertion doesn't mess anything up. So that's a FIXME. */
213 : : template <typename ManagedTokenSource>
214 : : bool
215 : 26923 : Parser<ManagedTokenSource>::skip_generics_right_angle ()
216 : : {
217 : : /* OK, new great idea. Have a lexer method called
218 : : * "split_current_token(TokenType newLeft, TokenType newRight)", which is
219 : : * called here with whatever arguments are appropriate. That lexer method
220 : : * handles "replacing" the current token with the "newLeft" and "inserting"
221 : : * the next token with the "newRight" (and creating a location, etc. for it)
222 : : */
223 : :
224 : : /* HACK: special handling for right shift '>>', greater or equal '>=', and
225 : : * right shift assig */
226 : : // '>>='
227 : 26923 : const_TokenPtr tok = lexer.peek_token ();
228 : 26923 : switch (tok->get_id ())
229 : : {
230 : 23236 : case RIGHT_ANGLE:
231 : : // this is good - skip token
232 : 23236 : lexer.skip_token ();
233 : 23236 : return true;
234 : 3686 : case RIGHT_SHIFT:
235 : : {
236 : : // new implementation that should be better
237 : 3686 : lexer.split_current_token (RIGHT_ANGLE, RIGHT_ANGLE);
238 : 3686 : lexer.skip_token ();
239 : 3686 : return true;
240 : : }
241 : 0 : case GREATER_OR_EQUAL:
242 : : {
243 : : // new implementation that should be better
244 : 0 : lexer.split_current_token (RIGHT_ANGLE, EQUAL);
245 : 0 : lexer.skip_token ();
246 : 0 : return true;
247 : : }
248 : 0 : case RIGHT_SHIFT_EQ:
249 : : {
250 : : // new implementation that should be better
251 : 0 : lexer.split_current_token (RIGHT_ANGLE, GREATER_OR_EQUAL);
252 : 0 : lexer.skip_token ();
253 : 0 : return true;
254 : : }
255 : 1 : default:
256 : 1 : add_error (Error (tok->get_locus (),
257 : : "expected %<>%> at end of generic argument - found %qs",
258 : : tok->get_token_description ()));
259 : 1 : return false;
260 : : }
261 : 26923 : }
262 : :
263 : : /* Gets left binding power for specified token.
264 : : * Not suitable for use at the moment or possibly ever because binding power
265 : : * cannot be purely determined from operator token with Rust grammar - e.g.
266 : : * method call and field access have
267 : : * different left binding powers but the same operator token. */
268 : : template <typename ManagedTokenSource>
269 : : int
270 : 840192 : Parser<ManagedTokenSource>::left_binding_power (const_TokenPtr token)
271 : : {
272 : : // HACK: called with "peek_token()", so lookahead is "peek_token(1)"
273 : 840192 : switch (token->get_id ())
274 : : {
275 : : /* TODO: issue here - distinguish between method calls and field access
276 : : * somehow? Also would have to distinguish between paths and function
277 : : * calls (:: operator), maybe more stuff. */
278 : : /* Current plan for tackling LBP - don't do it based on token, use
279 : : * lookahead. Or alternatively, only use Pratt parsing for OperatorExpr
280 : : * and handle other expressions without it. rustc only considers
281 : : * arithmetic, logical/relational, 'as',
282 : : * '?=', ranges, colons, and assignment to have operator precedence and
283 : : * associativity rules applicable. It then has
284 : : * a separate "ExprPrecedence" that also includes binary operators. */
285 : :
286 : : // TODO: handle operator overloading - have a function replace the
287 : : // operator?
288 : :
289 : : /*case DOT:
290 : : return LBP_DOT;*/
291 : :
292 : 0 : case SCOPE_RESOLUTION:
293 : 0 : rust_debug (
294 : : "possible error - looked up LBP of scope resolution operator. should "
295 : : "be handled elsewhere.");
296 : 0 : return LBP_PATH;
297 : :
298 : : /* Resolved by lookahead HACK that should work with current code. If next
299 : : * token is identifier and token after that isn't parenthesised expression
300 : : * list, it is a field reference. */
301 : 46259 : case DOT:
302 : 92518 : if (lexer.peek_token (1)->get_id () == IDENTIFIER
303 : 83573 : && lexer.peek_token (2)->get_id () != LEFT_PAREN)
304 : : {
305 : : return LBP_FIELD_EXPR;
306 : : }
307 : : return LBP_METHOD_CALL;
308 : :
309 : : case LEFT_PAREN:
310 : : return LBP_FUNCTION_CALL;
311 : :
312 : : case LEFT_SQUARE:
313 : : return LBP_ARRAY_REF;
314 : :
315 : : // postfix question mark (i.e. error propagation expression)
316 : 203 : case QUESTION_MARK:
317 : 203 : return LBP_QUESTION_MARK;
318 : :
319 : 9982 : case AS:
320 : 9982 : return LBP_AS;
321 : :
322 : : case ASTERISK:
323 : : return LBP_MUL;
324 : : case DIV:
325 : : return LBP_DIV;
326 : : case PERCENT:
327 : : return LBP_MOD;
328 : :
329 : : case PLUS:
330 : : return LBP_PLUS;
331 : : case MINUS:
332 : : return LBP_MINUS;
333 : :
334 : : case LEFT_SHIFT:
335 : : return LBP_L_SHIFT;
336 : : case RIGHT_SHIFT:
337 : : return LBP_R_SHIFT;
338 : :
339 : : // binary & operator
340 : 4373 : case AMP:
341 : 4373 : return LBP_AMP;
342 : :
343 : : // binary ^ operator
344 : 159 : case CARET:
345 : 159 : return LBP_CARET;
346 : :
347 : : // binary | operator
348 : 3228 : case PIPE:
349 : 3228 : return LBP_PIPE;
350 : :
351 : : case EQUAL_EQUAL:
352 : : return LBP_EQUAL;
353 : : case NOT_EQUAL:
354 : : return LBP_NOT_EQUAL;
355 : : case RIGHT_ANGLE:
356 : : return LBP_GREATER_THAN;
357 : : case GREATER_OR_EQUAL:
358 : : return LBP_GREATER_EQUAL;
359 : : case LEFT_ANGLE:
360 : : return LBP_SMALLER_THAN;
361 : : case LESS_OR_EQUAL:
362 : : return LBP_SMALLER_EQUAL;
363 : :
364 : 1242 : case LOGICAL_AND:
365 : 1242 : return LBP_LOGICAL_AND;
366 : :
367 : 510 : case OR:
368 : 510 : return LBP_LOGICAL_OR;
369 : :
370 : : case DOT_DOT:
371 : : return LBP_DOT_DOT;
372 : :
373 : : case DOT_DOT_EQ:
374 : : return LBP_DOT_DOT_EQ;
375 : :
376 : : case EQUAL:
377 : : return LBP_ASSIG;
378 : : case PLUS_EQ:
379 : : return LBP_PLUS_ASSIG;
380 : : case MINUS_EQ:
381 : : return LBP_MINUS_ASSIG;
382 : : case ASTERISK_EQ:
383 : : return LBP_MULT_ASSIG;
384 : : case DIV_EQ:
385 : : return LBP_DIV_ASSIG;
386 : : case PERCENT_EQ:
387 : : return LBP_MOD_ASSIG;
388 : : case AMP_EQ:
389 : : return LBP_AMP_ASSIG;
390 : : case PIPE_EQ:
391 : : return LBP_PIPE_ASSIG;
392 : : case CARET_EQ:
393 : : return LBP_CARET_ASSIG;
394 : : case LEFT_SHIFT_EQ:
395 : : return LBP_L_SHIFT_ASSIG;
396 : : case RIGHT_SHIFT_EQ:
397 : : return LBP_R_SHIFT_ASSIG;
398 : :
399 : : /* HACK: float literal due to lexer misidentifying a dot then an integer as
400 : : * a float */
401 : : case FLOAT_LITERAL:
402 : : return LBP_FIELD_EXPR;
403 : : // field expr is same as tuple expr in precedence, i imagine
404 : : // TODO: is this needed anymore? lexer shouldn't do that anymore
405 : :
406 : : // anything that can't appear in an infix position is given lowest priority
407 : 737908 : default:
408 : 737908 : return LBP_LOWEST;
409 : : }
410 : : }
411 : :
412 : : // Returns true when current token is EOF.
413 : : template <typename ManagedTokenSource>
414 : : bool
415 : 0 : Parser<ManagedTokenSource>::done_end_of_file ()
416 : : {
417 : 0 : return lexer.peek_token ()->get_id () == END_OF_FILE;
418 : : }
419 : :
420 : : // Parses a sequence of items within a module or the implicit top-level module
421 : : // in a crate
422 : : template <typename ManagedTokenSource>
423 : : std::vector<std::unique_ptr<AST::Item>>
424 : 4844 : Parser<ManagedTokenSource>::parse_items ()
425 : : {
426 : 4844 : std::vector<std::unique_ptr<AST::Item>> items;
427 : :
428 : 4844 : const_TokenPtr t = lexer.peek_token ();
429 : 28531 : while (t->get_id () != END_OF_FILE)
430 : : {
431 : 23736 : std::unique_ptr<AST::Item> item = parse_item (false);
432 : 23736 : if (item == nullptr)
433 : : {
434 : 49 : Error error (lexer.peek_token ()->get_locus (),
435 : : "failed to parse item in crate");
436 : 49 : add_error (std::move (error));
437 : :
438 : : // TODO: should all items be cleared?
439 : 49 : items = std::vector<std::unique_ptr<AST::Item>> ();
440 : : break;
441 : 49 : }
442 : :
443 : 23687 : items.push_back (std::move (item));
444 : :
445 : 23687 : t = lexer.peek_token ();
446 : : }
447 : :
448 : 4844 : return items;
449 : 4844 : }
450 : :
451 : : // Parses a crate (compilation unit) - entry point
452 : : template <typename ManagedTokenSource>
453 : : std::unique_ptr<AST::Crate>
454 : 4591 : Parser<ManagedTokenSource>::parse_crate ()
455 : : {
456 : : // parse inner attributes
457 : 4591 : AST::AttrVec inner_attrs = parse_inner_attributes ();
458 : :
459 : : // parse items
460 : 4591 : std::vector<std::unique_ptr<AST::Item>> items = parse_items ();
461 : :
462 : : // emit all errors
463 : 4826 : for (const auto &error : error_table)
464 : 235 : error.emit ();
465 : :
466 : : return std::unique_ptr<AST::Crate> (
467 : 4591 : new AST::Crate (std::move (items), std::move (inner_attrs)));
468 : 4591 : }
469 : :
470 : : // Parse a contiguous block of inner attributes.
471 : : template <typename ManagedTokenSource>
472 : : AST::AttrVec
473 : 174629 : Parser<ManagedTokenSource>::parse_inner_attributes ()
474 : : {
475 : 174629 : AST::AttrVec inner_attributes;
476 : :
477 : : // only try to parse it if it starts with "#!" not only "#"
478 : 81897 : while ((lexer.peek_token ()->get_id () == HASH
479 : 109489 : && lexer.peek_token (1)->get_id () == EXCLAM)
480 : 539764 : || lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
481 : : {
482 : 3604 : AST::Attribute inner_attr = parse_inner_attribute ();
483 : :
484 : : /* Ensure only valid inner attributes are added to the inner_attributes
485 : : * list */
486 : 3604 : if (!inner_attr.is_empty ())
487 : : {
488 : 3604 : inner_attributes.push_back (std::move (inner_attr));
489 : : }
490 : : else
491 : : {
492 : : /* If no more valid inner attributes, break out of loop (only
493 : : * contiguous inner attributes parsed). */
494 : : break;
495 : : }
496 : : }
497 : :
498 : 174629 : inner_attributes.shrink_to_fit ();
499 : 174629 : return inner_attributes;
500 : : }
501 : :
502 : : // Parse a inner or outer doc comment into an doc attribute
503 : : template <typename ManagedTokenSource>
504 : : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
505 : 53771 : Parser<ManagedTokenSource>::parse_doc_comment ()
506 : : {
507 : 53771 : const_TokenPtr token = lexer.peek_token ();
508 : 53771 : location_t locus = token->get_locus ();
509 : 53771 : AST::SimplePathSegment segment (Values::Attributes::DOC, locus);
510 : 53771 : std::vector<AST::SimplePathSegment> segments;
511 : 53771 : segments.push_back (std::move (segment));
512 : 53771 : AST::SimplePath attr_path (std::move (segments), false, locus);
513 : 107542 : AST::LiteralExpr lit_expr (token->get_str (), AST::Literal::STRING,
514 : : PrimitiveCoreType::CORETYPE_STR, {}, locus);
515 : 53771 : std::unique_ptr<AST::AttrInput> attr_input (
516 : 53771 : new AST::AttrInputLiteral (std::move (lit_expr)));
517 : 53771 : lexer.skip_token ();
518 : 107542 : return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
519 : 53771 : }
520 : :
521 : : // Parse a single inner attribute.
522 : : template <typename ManagedTokenSource>
523 : : AST::Attribute
524 : 3604 : Parser<ManagedTokenSource>::parse_inner_attribute ()
525 : : {
526 : 7208 : if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
527 : : {
528 : 2648 : auto values = parse_doc_comment ();
529 : 2648 : auto path = std::move (std::get<0> (values));
530 : 2648 : auto input = std::move (std::get<1> (values));
531 : 2648 : auto loc = std::get<2> (values);
532 : 2648 : return AST::Attribute (std::move (path), std::move (input), loc, true);
533 : 5296 : }
534 : :
535 : 1912 : if (lexer.peek_token ()->get_id () != HASH)
536 : : {
537 : 0 : Error error (lexer.peek_token ()->get_locus (),
538 : : "BUG: token %<#%> is missing, but %<parse_inner_attribute%> "
539 : : "was invoked");
540 : 0 : add_error (std::move (error));
541 : :
542 : 0 : return AST::Attribute::create_empty ();
543 : 0 : }
544 : 956 : lexer.skip_token ();
545 : :
546 : 1912 : if (lexer.peek_token ()->get_id () != EXCLAM)
547 : : {
548 : 0 : Error error (lexer.peek_token ()->get_locus (),
549 : : "expected %<!%> or %<[%> for inner attribute");
550 : 0 : add_error (std::move (error));
551 : :
552 : 0 : return AST::Attribute::create_empty ();
553 : 0 : }
554 : 956 : lexer.skip_token ();
555 : :
556 : 956 : if (!skip_token (LEFT_SQUARE))
557 : 0 : return AST::Attribute::create_empty ();
558 : :
559 : 956 : auto values = parse_attribute_body ();
560 : :
561 : 956 : auto path = std::move (std::get<0> (values));
562 : 956 : auto input = std::move (std::get<1> (values));
563 : 956 : auto loc = std::get<2> (values);
564 : 956 : auto actual_attribute
565 : 956 : = AST::Attribute (std::move (path), std::move (input), loc, true);
566 : :
567 : 956 : if (!skip_token (RIGHT_SQUARE))
568 : 0 : return AST::Attribute::create_empty ();
569 : :
570 : 956 : return actual_attribute;
571 : 1912 : }
572 : :
573 : : // Parses the body of an attribute (inner or outer).
574 : : template <typename ManagedTokenSource>
575 : : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
576 : 43368 : Parser<ManagedTokenSource>::parse_attribute_body ()
577 : : {
578 : 43368 : location_t locus = lexer.peek_token ()->get_locus ();
579 : :
580 : 43368 : AST::SimplePath attr_path = parse_simple_path ();
581 : : // ensure path is valid to parse attribute input
582 : 43368 : if (attr_path.is_empty ())
583 : : {
584 : 0 : Error error (lexer.peek_token ()->get_locus (),
585 : : "empty simple path in attribute");
586 : 0 : add_error (std::move (error));
587 : :
588 : : // Skip past potential further info in attribute (i.e. attr_input)
589 : 0 : skip_after_end_attribute ();
590 : 0 : return std::make_tuple (std::move (attr_path), nullptr, UNDEF_LOCATION);
591 : 0 : }
592 : :
593 : 43368 : std::unique_ptr<AST::AttrInput> attr_input = parse_attr_input ();
594 : : // AttrInput is allowed to be null, so no checks here
595 : :
596 : 43368 : return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
597 : 43368 : }
598 : :
599 : : /* Determines whether token is a valid simple path segment. This does not
600 : : * include scope resolution operators. */
601 : : inline bool
602 : 61255 : is_simple_path_segment (TokenId id)
603 : : {
604 : 61255 : switch (id)
605 : : {
606 : : case IDENTIFIER:
607 : : case SUPER:
608 : : case SELF:
609 : : case CRATE:
610 : : return true;
611 : : case DOLLAR_SIGN:
612 : : // assume that dollar sign leads to $crate
613 : : return true;
614 : 5957 : default:
615 : 5957 : return false;
616 : : }
617 : : }
618 : :
619 : : // Parses a SimplePath AST node, if it exists. Does nothing otherwise.
620 : : template <typename ManagedTokenSource>
621 : : AST::SimplePath
622 : 54214 : Parser<ManagedTokenSource>::parse_simple_path ()
623 : : {
624 : 54214 : bool has_opening_scope_resolution = false;
625 : 54214 : location_t locus = UNKNOWN_LOCATION;
626 : :
627 : : // don't parse anything if not a path upfront
628 : 108428 : if (!is_simple_path_segment (lexer.peek_token ()->get_id ())
629 : 54512 : && !is_simple_path_segment (lexer.peek_token (1)->get_id ()))
630 : 242 : return AST::SimplePath::create_empty ();
631 : :
632 : : /* Checks for opening scope resolution (i.e. global scope fully-qualified
633 : : * path) */
634 : 107944 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
635 : : {
636 : 0 : has_opening_scope_resolution = true;
637 : :
638 : 0 : locus = lexer.peek_token ()->get_locus ();
639 : :
640 : 0 : lexer.skip_token ();
641 : : }
642 : :
643 : : // Parse single required simple path segment
644 : 53972 : AST::SimplePathSegment segment = parse_simple_path_segment ();
645 : :
646 : : // get location if not gotten already
647 : 53972 : if (locus == UNKNOWN_LOCATION)
648 : 53972 : locus = segment.get_locus ();
649 : :
650 : 53972 : std::vector<AST::SimplePathSegment> segments;
651 : :
652 : : // Return empty vector if first, actually required segment is an error
653 : 53972 : if (segment.is_error ())
654 : 56 : return AST::SimplePath::create_empty ();
655 : :
656 : 53916 : segments.push_back (std::move (segment));
657 : :
658 : : // Parse all other simple path segments
659 : 114296 : while (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
660 : : {
661 : 3522 : AST::SimplePathSegment new_segment = parse_simple_path_segment (1);
662 : :
663 : : // Return path as currently constructed if segment in error state.
664 : 3522 : if (new_segment.is_error ())
665 : : break;
666 : :
667 : 2942 : segments.push_back (std::move (new_segment));
668 : : }
669 : :
670 : : // DEBUG: check for any empty segments
671 : 110774 : for (const auto &seg : segments)
672 : : {
673 : 56858 : if (seg.is_error ())
674 : : {
675 : 0 : rust_debug (
676 : : "when parsing simple path, somehow empty path segment was "
677 : : "not filtered out. Path begins with '%s'",
678 : : segments.at (0).as_string ().c_str ());
679 : : }
680 : : }
681 : :
682 : 53916 : return AST::SimplePath (std::move (segments), has_opening_scope_resolution,
683 : 53916 : locus);
684 : : /* TODO: now that is_simple_path_segment exists, could probably start
685 : : * actually making errors upon parse failure of segments and whatever */
686 : 53972 : }
687 : :
688 : : /* Parses a single SimplePathSegment (does not handle the scope resolution
689 : : * operators)
690 : : * Starts parsing at an offset of base_peek */
691 : : template <typename ManagedTokenSource>
692 : : AST::SimplePathSegment
693 : 57494 : Parser<ManagedTokenSource>::parse_simple_path_segment (int base_peek)
694 : : {
695 : : using namespace Values;
696 : 57494 : const_TokenPtr t = lexer.peek_token (base_peek);
697 : 57494 : switch (t->get_id ())
698 : : {
699 : 55771 : case IDENTIFIER:
700 : 55771 : lexer.skip_token (base_peek);
701 : :
702 : 111542 : return AST::SimplePathSegment (t->get_str (), t->get_locus ());
703 : 365 : case SUPER:
704 : 365 : lexer.skip_token (base_peek);
705 : :
706 : 365 : return AST::SimplePathSegment (Keywords::SUPER, t->get_locus ());
707 : 237 : case SELF:
708 : 237 : lexer.skip_token (base_peek);
709 : :
710 : 237 : return AST::SimplePathSegment (Keywords::SELF, t->get_locus ());
711 : 485 : case CRATE:
712 : 485 : lexer.skip_token (base_peek);
713 : :
714 : 485 : return AST::SimplePathSegment (Keywords::CRATE, t->get_locus ());
715 : 0 : case DOLLAR_SIGN:
716 : 0 : if (lexer.peek_token (base_peek + 1)->get_id () == CRATE)
717 : : {
718 : 0 : lexer.skip_token (base_peek + 1);
719 : :
720 : 0 : return AST::SimplePathSegment ("$crate", t->get_locus ());
721 : : }
722 : : gcc_fallthrough ();
723 : : default:
724 : : // do nothing but inactivates warning from gcc when compiling
725 : : /* could put the rust_error_at thing here but fallthrough (from failing
726 : : * $crate condition) isn't completely obvious if it is. */
727 : :
728 : : // test prevent error
729 : 636 : return AST::SimplePathSegment::create_error ();
730 : : }
731 : : rust_unreachable ();
732 : : /*rust_error_at(
733 : : t->get_locus(), "invalid token '%s' in simple path segment",
734 : : t->get_token_description());*/
735 : : // this is not necessarily an error, e.g. end of path
736 : : // return AST::SimplePathSegment::create_error();
737 : 57494 : }
738 : :
739 : : // Parses a PathIdentSegment - an identifier segment of a non-SimplePath path.
740 : : template <typename ManagedTokenSource>
741 : : AST::PathIdentSegment
742 : 544566 : Parser<ManagedTokenSource>::parse_path_ident_segment ()
743 : : {
744 : 544566 : const_TokenPtr t = lexer.peek_token ();
745 : 544566 : switch (t->get_id ())
746 : : {
747 : 509978 : case IDENTIFIER:
748 : 509978 : lexer.skip_token ();
749 : :
750 : 1019956 : return AST::PathIdentSegment (t->get_str (), t->get_locus ());
751 : 41 : case SUPER:
752 : 41 : lexer.skip_token ();
753 : :
754 : 41 : return AST::PathIdentSegment (Values::Keywords::SUPER, t->get_locus ());
755 : 20050 : case SELF:
756 : 20050 : lexer.skip_token ();
757 : :
758 : 20050 : return AST::PathIdentSegment (Values::Keywords::SELF, t->get_locus ());
759 : 13467 : case SELF_ALIAS:
760 : 13467 : lexer.skip_token ();
761 : :
762 : 26934 : return AST::PathIdentSegment (Values::Keywords::SELF_ALIAS,
763 : 26934 : t->get_locus ());
764 : 817 : case CRATE:
765 : 817 : lexer.skip_token ();
766 : :
767 : 817 : return AST::PathIdentSegment (Values::Keywords::CRATE, t->get_locus ());
768 : 2 : case DOLLAR_SIGN:
769 : 4 : if (lexer.peek_token (1)->get_id () == CRATE)
770 : : {
771 : 0 : lexer.skip_token (1);
772 : :
773 : 0 : return AST::PathIdentSegment ("$crate", t->get_locus ());
774 : : }
775 : : gcc_fallthrough ();
776 : : default:
777 : : /* do nothing but inactivates warning from gcc when compiling
778 : : * could put the error_at thing here but fallthrough (from failing $crate
779 : : * condition) isn't completely obvious if it is. */
780 : :
781 : : // test prevent error
782 : 213 : return AST::PathIdentSegment::create_error ();
783 : : }
784 : : rust_unreachable ();
785 : : // not necessarily an error
786 : 544566 : }
787 : :
788 : : // Parses an AttrInput AST node (polymorphic, as AttrInput is abstract)
789 : : template <typename ManagedTokenSource>
790 : : std::unique_ptr<AST::AttrInput>
791 : 43368 : Parser<ManagedTokenSource>::parse_attr_input ()
792 : : {
793 : 43368 : const_TokenPtr t = lexer.peek_token ();
794 : 43368 : switch (t->get_id ())
795 : : {
796 : 24269 : case LEFT_PAREN:
797 : : case LEFT_SQUARE:
798 : : case LEFT_CURLY:
799 : : {
800 : : // must be a delimited token tree, so parse that
801 : 24269 : std::unique_ptr<AST::AttrInput> input_tree (
802 : 24269 : new AST::DelimTokenTree (parse_delim_token_tree ()));
803 : :
804 : : // TODO: potential checks on DelimTokenTree before returning
805 : :
806 : : return input_tree;
807 : : }
808 : 6743 : case EQUAL:
809 : : {
810 : : // = LiteralExpr
811 : 6743 : lexer.skip_token ();
812 : :
813 : 6743 : t = lexer.peek_token ();
814 : :
815 : : // attempt to parse macro
816 : : // TODO: macros may/may not be allowed in attributes
817 : : // this is needed for "#[doc = include_str!(...)]"
818 : 6743 : if (is_simple_path_segment (t->get_id ()))
819 : : {
820 : 1326 : std::unique_ptr<AST::MacroInvocation> invoke
821 : 1326 : = parse_macro_invocation ({});
822 : :
823 : 1326 : if (!invoke)
824 : 0 : return nullptr;
825 : :
826 : : return std::unique_ptr<AST::AttrInput> (
827 : 1326 : new AST::AttrInputMacro (std::move (invoke)));
828 : 1326 : }
829 : :
830 : : /* Ensure token is a "literal expression" (literally only a literal
831 : : * token of any type) */
832 : 5417 : if (!t->is_literal ())
833 : : {
834 : 0 : Error error (
835 : : t->get_locus (),
836 : : "unknown token %qs in attribute body - literal expected",
837 : : t->get_token_description ());
838 : 0 : add_error (std::move (error));
839 : :
840 : 0 : skip_after_end_attribute ();
841 : 0 : return nullptr;
842 : 0 : }
843 : :
844 : 5417 : AST::Literal::LitType lit_type = AST::Literal::STRING;
845 : : // Crappy mapping of token type to literal type
846 : 5417 : switch (t->get_id ())
847 : : {
848 : : case INT_LITERAL:
849 : : lit_type = AST::Literal::INT;
850 : : break;
851 : : case FLOAT_LITERAL:
852 : : lit_type = AST::Literal::FLOAT;
853 : : break;
854 : : case CHAR_LITERAL:
855 : : lit_type = AST::Literal::CHAR;
856 : : break;
857 : : case BYTE_CHAR_LITERAL:
858 : : lit_type = AST::Literal::BYTE;
859 : : break;
860 : : case BYTE_STRING_LITERAL:
861 : : lit_type = AST::Literal::BYTE_STRING;
862 : : break;
863 : : case RAW_STRING_LITERAL:
864 : : lit_type = AST::Literal::RAW_STRING;
865 : : break;
866 : : case STRING_LITERAL:
867 : : default:
868 : : lit_type = AST::Literal::STRING;
869 : : break; // TODO: raw string? don't eliminate it from lexer?
870 : : }
871 : :
872 : : // create actual LiteralExpr
873 : 16251 : AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (),
874 : : {}, t->get_locus ());
875 : 5417 : lexer.skip_token ();
876 : :
877 : 5417 : std::unique_ptr<AST::AttrInput> attr_input_lit (
878 : 5417 : new AST::AttrInputLiteral (std::move (lit_expr)));
879 : :
880 : : // do checks or whatever? none required, really
881 : :
882 : : // FIXME: shouldn't a skip token be required here?
883 : :
884 : 5417 : return attr_input_lit;
885 : 5417 : }
886 : : break;
887 : 12356 : case RIGHT_PAREN:
888 : : case RIGHT_SQUARE:
889 : : case RIGHT_CURLY:
890 : : case END_OF_FILE:
891 : : // means AttrInput is missing, which is allowed
892 : 12356 : return nullptr;
893 : 0 : default:
894 : 0 : add_error (
895 : 0 : Error (t->get_locus (),
896 : : "unknown token %qs in attribute body - attribute input or "
897 : : "none expected",
898 : : t->get_token_description ()));
899 : :
900 : 0 : skip_after_end_attribute ();
901 : 0 : return nullptr;
902 : : }
903 : : rust_unreachable ();
904 : : // TODO: find out how to stop gcc error on "no return value"
905 : 43368 : }
906 : :
907 : : /* Returns true if the token id matches the delimiter type. Note that this only
908 : : * operates for END delimiter tokens. */
909 : : inline bool
910 : 1048474 : token_id_matches_delims (TokenId token_id, AST::DelimType delim_type)
911 : : {
912 : 1048474 : return ((token_id == RIGHT_PAREN && delim_type == AST::PARENS)
913 : 814085 : || (token_id == RIGHT_SQUARE && delim_type == AST::SQUARE)
914 : 1840581 : || (token_id == RIGHT_CURLY && delim_type == AST::CURLY));
915 : : }
916 : :
917 : : /* Returns true if the likely result of parsing the next few tokens is a path.
918 : : * Not guaranteed, though, especially in the case of syntax errors. */
919 : : inline bool
920 : : is_likely_path_next (TokenId next_token_id)
921 : : {
922 : : switch (next_token_id)
923 : : {
924 : : case IDENTIFIER:
925 : : case SUPER:
926 : : case SELF:
927 : : case SELF_ALIAS:
928 : : case CRATE:
929 : : // maybe - maybe do extra check. But then requires another TokenId.
930 : : case DOLLAR_SIGN:
931 : : case SCOPE_RESOLUTION:
932 : : return true;
933 : : default:
934 : : return false;
935 : : }
936 : : }
937 : :
938 : : // Parses a delimited token tree
939 : : template <typename ManagedTokenSource>
940 : : AST::DelimTokenTree
941 : 133335 : Parser<ManagedTokenSource>::parse_delim_token_tree ()
942 : : {
943 : 133335 : const_TokenPtr t = lexer.peek_token ();
944 : 133335 : lexer.skip_token ();
945 : 133335 : location_t initial_loc = t->get_locus ();
946 : :
947 : : // save delim type to ensure it is reused later
948 : 133335 : AST::DelimType delim_type = AST::PARENS;
949 : :
950 : : // Map tokens to DelimType
951 : 133335 : switch (t->get_id ())
952 : : {
953 : : case LEFT_PAREN:
954 : : delim_type = AST::PARENS;
955 : : break;
956 : 10924 : case LEFT_SQUARE:
957 : 10924 : delim_type = AST::SQUARE;
958 : 10924 : break;
959 : 8558 : case LEFT_CURLY:
960 : 8558 : delim_type = AST::CURLY;
961 : 8558 : break;
962 : 0 : default:
963 : 0 : add_error (Error (t->get_locus (),
964 : : "unexpected token %qs - expecting delimiters (for a "
965 : : "delimited token tree)",
966 : : t->get_token_description ()));
967 : :
968 : 0 : return AST::DelimTokenTree::create_empty ();
969 : : }
970 : :
971 : : // parse actual token tree vector - 0 or more
972 : 133335 : std::vector<std::unique_ptr<AST::TokenTree>> token_trees_in_tree;
973 : 133335 : auto delim_open
974 : 133335 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
975 : 133335 : token_trees_in_tree.push_back (std::move (delim_open));
976 : :
977 : : // repeat loop until finding the matching delimiter
978 : 133335 : t = lexer.peek_token ();
979 : 835017 : while (!token_id_matches_delims (t->get_id (), delim_type)
980 : 835017 : && t->get_id () != END_OF_FILE)
981 : : {
982 : 701682 : std::unique_ptr<AST::TokenTree> tok_tree = parse_token_tree ();
983 : :
984 : 701682 : if (tok_tree == nullptr)
985 : : {
986 : : // TODO: is this error handling appropriate?
987 : 0 : Error error (
988 : : t->get_locus (),
989 : : "failed to parse token tree in delimited token tree - found %qs",
990 : : t->get_token_description ());
991 : 0 : add_error (std::move (error));
992 : :
993 : 0 : return AST::DelimTokenTree::create_empty ();
994 : 0 : }
995 : :
996 : 701682 : token_trees_in_tree.push_back (std::move (tok_tree));
997 : :
998 : : // lexer.skip_token();
999 : 701682 : t = lexer.peek_token ();
1000 : : }
1001 : 133335 : auto delim_close
1002 : 133335 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1003 : 133335 : token_trees_in_tree.push_back (std::move (delim_close));
1004 : :
1005 : 133335 : AST::DelimTokenTree token_tree (delim_type, std::move (token_trees_in_tree),
1006 : : initial_loc);
1007 : :
1008 : : // parse end delimiters
1009 : 133335 : t = lexer.peek_token ();
1010 : :
1011 : 133335 : if (token_id_matches_delims (t->get_id (), delim_type))
1012 : : {
1013 : : // tokens match opening delimiter, so skip.
1014 : 133328 : lexer.skip_token ();
1015 : :
1016 : : // DEBUG
1017 : 266656 : rust_debug ("finished parsing new delim token tree - peeked token is now "
1018 : : "'%s' while t is '%s'",
1019 : : lexer.peek_token ()->get_token_description (),
1020 : : t->get_token_description ());
1021 : :
1022 : 133328 : return token_tree;
1023 : : }
1024 : : else
1025 : : {
1026 : : // tokens don't match opening delimiters, so produce error
1027 : 7 : Error error (t->get_locus (),
1028 : : "unexpected token %qs - expecting closing delimiter %qs "
1029 : : "(for a delimited token tree)",
1030 : : t->get_token_description (),
1031 : : (delim_type == AST::PARENS
1032 : : ? ")"
1033 : : : (delim_type == AST::SQUARE ? "]" : "}")));
1034 : 7 : add_error (std::move (error));
1035 : :
1036 : : /* return empty token tree despite possibly parsing valid token tree -
1037 : : * TODO is this a good idea? */
1038 : 7 : return AST::DelimTokenTree::create_empty ();
1039 : 7 : }
1040 : 133335 : }
1041 : :
1042 : : // Parses an identifier/keyword as a Token
1043 : : template <typename ManagedTokenSource>
1044 : : std::unique_ptr<AST::Token>
1045 : 13336 : Parser<ManagedTokenSource>::parse_identifier_or_keyword_token ()
1046 : : {
1047 : 13336 : const_TokenPtr t = lexer.peek_token ();
1048 : :
1049 : 13336 : if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ()))
1050 : : {
1051 : 13333 : lexer.skip_token ();
1052 : 13333 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1053 : : }
1054 : : else
1055 : : {
1056 : 3 : add_error (Error (t->get_locus (), "expected keyword or identifier"));
1057 : 3 : return nullptr;
1058 : : }
1059 : 13336 : }
1060 : :
1061 : : /* Parses a TokenTree syntactical production. This is either a delimited token
1062 : : * tree or a non-delimiter token. */
1063 : : template <typename ManagedTokenSource>
1064 : : std::unique_ptr<AST::TokenTree>
1065 : 778831 : Parser<ManagedTokenSource>::parse_token_tree ()
1066 : : {
1067 : 778831 : const_TokenPtr t = lexer.peek_token ();
1068 : :
1069 : 778831 : switch (t->get_id ())
1070 : : {
1071 : 52754 : case LEFT_PAREN:
1072 : : case LEFT_SQUARE:
1073 : : case LEFT_CURLY:
1074 : : // Parse delimited token tree
1075 : : // TODO: use move rather than copy constructor
1076 : 52754 : return std::unique_ptr<AST::DelimTokenTree> (
1077 : 52754 : new AST::DelimTokenTree (parse_delim_token_tree ()));
1078 : 1 : case RIGHT_PAREN:
1079 : : case RIGHT_SQUARE:
1080 : : case RIGHT_CURLY:
1081 : : // error - should not be called when this a token
1082 : 1 : add_error (
1083 : 1 : Error (t->get_locus (),
1084 : : "unexpected closing delimiter %qs - token tree requires "
1085 : : "either paired delimiters or non-delimiter tokens",
1086 : : t->get_token_description ()));
1087 : :
1088 : 1 : lexer.skip_token ();
1089 : 1 : return nullptr;
1090 : 726076 : default:
1091 : : // parse token itself as TokenTree
1092 : 726076 : lexer.skip_token ();
1093 : 726076 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1094 : : }
1095 : 778831 : }
1096 : :
1097 : : template <typename ManagedTokenSource>
1098 : : bool
1099 : 5173 : Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
1100 : : {
1101 : 5173 : auto macro_name = lexer.peek_token (2)->get_id ();
1102 : :
1103 : 5173 : bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
1104 : :
1105 : 5173 : return t->get_str () == Values::WeakKeywords::MACRO_RULES
1106 : 6291 : && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
1107 : : }
1108 : :
1109 : : // Parses a single item
1110 : : template <typename ManagedTokenSource>
1111 : : std::unique_ptr<AST::Item>
1112 : 39427 : Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
1113 : : {
1114 : : // has a "called_from_statement" parameter for better error message handling
1115 : :
1116 : : // parse outer attributes for item
1117 : 39427 : AST::AttrVec outer_attrs = parse_outer_attributes ();
1118 : 39427 : const_TokenPtr t = lexer.peek_token ();
1119 : :
1120 : 39427 : switch (t->get_id ())
1121 : : {
1122 : 7 : case END_OF_FILE:
1123 : : // not necessarily an error, unless we just read outer
1124 : : // attributes which needs to be attached
1125 : 7 : if (!outer_attrs.empty ())
1126 : : {
1127 : 0 : Rust::AST::Attribute attr = outer_attrs.back ();
1128 : 0 : Error error (attr.get_locus (),
1129 : : "expected item after outer attribute or doc comment");
1130 : 0 : add_error (std::move (error));
1131 : 0 : }
1132 : 7 : return nullptr;
1133 : :
1134 : 34414 : case ASYNC:
1135 : : case PUB:
1136 : : case MOD:
1137 : : case EXTERN_KW:
1138 : : case USE:
1139 : : case FN_KW:
1140 : : case TYPE:
1141 : : case STRUCT_KW:
1142 : : case ENUM_KW:
1143 : : case CONST:
1144 : : case STATIC_KW:
1145 : : case AUTO:
1146 : : case TRAIT:
1147 : : case IMPL:
1148 : : case MACRO:
1149 : : /* TODO: implement union keyword but not really because of
1150 : : * context-dependence crappy hack way to parse a union written below to
1151 : : * separate it from the good code. */
1152 : : // case UNION:
1153 : : case UNSAFE: // maybe - unsafe traits are a thing
1154 : : // if any of these (should be all possible VisItem prefixes), parse a
1155 : : // VisItem
1156 : 34414 : return parse_vis_item (std::move (outer_attrs));
1157 : : break;
1158 : 3 : case SUPER:
1159 : : case SELF:
1160 : : case CRATE:
1161 : : case DOLLAR_SIGN:
1162 : : // almost certainly macro invocation semi
1163 : 6 : return parse_macro_invocation_semi (std::move (outer_attrs));
1164 : : break;
1165 : : // crappy hack to do union "keyword"
1166 : 4997 : case IDENTIFIER:
1167 : : // TODO: ensure std::string and literal comparison works
1168 : 6859 : if (t->get_str () == Values::WeakKeywords::UNION
1169 : 5060 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
1170 : : {
1171 : 63 : return parse_vis_item (std::move (outer_attrs));
1172 : : // or should this go straight to parsing union?
1173 : : }
1174 : 6733 : else if (t->get_str () == Values::WeakKeywords::DEFAULT
1175 : 4936 : && lexer.peek_token (1)->get_id () != EXCLAM)
1176 : : {
1177 : 1 : add_error (Error (t->get_locus (),
1178 : : "%qs is only allowed on items within %qs blocks",
1179 : : "default", "impl"));
1180 : 1 : return nullptr;
1181 : : }
1182 : 9866 : else if (is_macro_rules_def (t))
1183 : : {
1184 : : // macro_rules! macro item
1185 : 1114 : return parse_macro_rules_def (std::move (outer_attrs));
1186 : : }
1187 : 7638 : else if (lexer.peek_token (1)->get_id () == SCOPE_RESOLUTION
1188 : 7637 : || lexer.peek_token (1)->get_id () == EXCLAM)
1189 : : {
1190 : : /* path (probably) or macro invocation, so probably a macro invocation
1191 : : * semi */
1192 : 7630 : return parse_macro_invocation_semi (std::move (outer_attrs));
1193 : : }
1194 : : gcc_fallthrough ();
1195 : : default:
1196 : : // otherwise unrecognised
1197 : 20 : add_error (Error (t->get_locus (),
1198 : : "unrecognised token %qs for start of %s",
1199 : : t->get_token_description (),
1200 : : called_from_statement ? "statement" : "item"));
1201 : :
1202 : : // skip somewhere?
1203 : 10 : return nullptr;
1204 : : break;
1205 : : }
1206 : 39427 : }
1207 : :
1208 : : // Parses a contiguous block of outer attributes.
1209 : : template <typename ManagedTokenSource>
1210 : : AST::AttrVec
1211 : 289964 : Parser<ManagedTokenSource>::parse_outer_attributes ()
1212 : : {
1213 : 289964 : AST::AttrVec outer_attributes;
1214 : :
1215 : 295665 : while (lexer.peek_token ()->get_id ()
1216 : : == HASH /* Can also be #!, which catches errors. */
1217 : 722786 : || lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT
1218 : 1394449 : || lexer.peek_token ()->get_id ()
1219 : : == INNER_DOC_COMMENT) /* For error handling. */
1220 : : {
1221 : 91751 : AST::Attribute outer_attr = parse_outer_attribute ();
1222 : :
1223 : : /* Ensure only valid outer attributes are added to the outer_attributes
1224 : : * list */
1225 : 91751 : if (!outer_attr.is_empty ())
1226 : : {
1227 : 91742 : outer_attributes.push_back (std::move (outer_attr));
1228 : : }
1229 : : else
1230 : : {
1231 : : /* If no more valid outer attributes, break out of loop (only
1232 : : * contiguous outer attributes parsed). */
1233 : : break;
1234 : : }
1235 : : }
1236 : :
1237 : 289964 : outer_attributes.shrink_to_fit ();
1238 : 289964 : return outer_attributes;
1239 : :
1240 : : /* TODO: this shares basically all code with parse_inner_attributes except
1241 : : * function call - find way of making it more modular? function pointer? */
1242 : : }
1243 : :
1244 : : // Parse a single outer attribute.
1245 : : template <typename ManagedTokenSource>
1246 : : AST::Attribute
1247 : 91751 : Parser<ManagedTokenSource>::parse_outer_attribute ()
1248 : : {
1249 : 183502 : if (lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT)
1250 : : {
1251 : 51123 : auto values = parse_doc_comment ();
1252 : 51123 : auto path = std::move (std::get<0> (values));
1253 : 51123 : auto input = std::move (std::get<1> (values));
1254 : 51123 : auto loc = std::get<2> (values);
1255 : 51123 : return AST::Attribute (std::move (path), std::move (input), loc, false);
1256 : 102246 : }
1257 : :
1258 : 81256 : if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
1259 : : {
1260 : 2 : Error error (
1261 : 2 : lexer.peek_token ()->get_locus (), ErrorCode::E0753,
1262 : : "expected outer doc comment, inner doc (%<//!%> or %</*!%>) only "
1263 : : "allowed at start of item "
1264 : : "and before any outer attribute or doc (%<#[%>, %<///%> or %</**%>)");
1265 : 2 : add_error (std::move (error));
1266 : 2 : lexer.skip_token ();
1267 : 2 : return AST::Attribute::create_empty ();
1268 : 2 : }
1269 : :
1270 : : /* OuterAttribute -> '#' '[' Attr ']' */
1271 : :
1272 : 81252 : if (lexer.peek_token ()->get_id () != HASH)
1273 : 0 : return AST::Attribute::create_empty ();
1274 : :
1275 : 40626 : lexer.skip_token ();
1276 : :
1277 : 40626 : TokenId id = lexer.peek_token ()->get_id ();
1278 : 40626 : if (id != LEFT_SQUARE)
1279 : : {
1280 : 0 : if (id == EXCLAM)
1281 : : {
1282 : : // this is inner attribute syntax, so throw error
1283 : : // inner attributes were either already parsed or not allowed here.
1284 : 0 : Error error (
1285 : 0 : lexer.peek_token ()->get_locus (),
1286 : : "token %<!%> found, indicating inner attribute definition. Inner "
1287 : : "attributes are not possible at this location");
1288 : 0 : add_error (std::move (error));
1289 : 0 : }
1290 : 0 : return AST::Attribute::create_empty ();
1291 : : }
1292 : :
1293 : 40626 : lexer.skip_token ();
1294 : :
1295 : 40626 : auto values = parse_attribute_body ();
1296 : 40626 : auto path = std::move (std::get<0> (values));
1297 : 40626 : auto input = std::move (std::get<1> (values));
1298 : 40626 : auto loc = std::get<2> (values);
1299 : 40626 : auto actual_attribute
1300 : 40626 : = AST::Attribute (std::move (path), std::move (input), loc, false);
1301 : :
1302 : 81252 : if (lexer.peek_token ()->get_id () != RIGHT_SQUARE)
1303 : 7 : return AST::Attribute::create_empty ();
1304 : :
1305 : 40619 : lexer.skip_token ();
1306 : :
1307 : 40619 : return actual_attribute;
1308 : 81252 : }
1309 : :
1310 : : // Parses a VisItem (item that can have non-default visibility).
1311 : : template <typename ManagedTokenSource>
1312 : : std::unique_ptr<AST::VisItem>
1313 : 35518 : Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
1314 : : {
1315 : : // parse visibility, which may or may not exist
1316 : 35518 : AST::Visibility vis = parse_visibility ();
1317 : :
1318 : : // select VisItem to create depending on keyword
1319 : 35518 : const_TokenPtr t = lexer.peek_token ();
1320 : :
1321 : 35518 : switch (t->get_id ())
1322 : : {
1323 : 1605 : case MOD:
1324 : 1605 : return parse_module (std::move (vis), std::move (outer_attrs));
1325 : 1551 : case EXTERN_KW:
1326 : : // lookahead to resolve syntactical production
1327 : 1551 : t = lexer.peek_token (1);
1328 : :
1329 : 1551 : switch (t->get_id ())
1330 : : {
1331 : 27 : case CRATE:
1332 : 27 : return parse_extern_crate (std::move (vis), std::move (outer_attrs));
1333 : 0 : case FN_KW: // extern function
1334 : 0 : return parse_function (std::move (vis), std::move (outer_attrs));
1335 : 0 : case LEFT_CURLY: // extern block
1336 : 0 : return parse_extern_block (std::move (vis), std::move (outer_attrs));
1337 : 1524 : case STRING_LITERAL: // for specifying extern ABI
1338 : : // could be extern block or extern function, so more lookahead
1339 : 1524 : t = lexer.peek_token (2);
1340 : :
1341 : 1524 : switch (t->get_id ())
1342 : : {
1343 : 2 : case FN_KW:
1344 : 2 : return parse_function (std::move (vis), std::move (outer_attrs));
1345 : 1522 : case LEFT_CURLY:
1346 : 1522 : return parse_extern_block (std::move (vis),
1347 : 1522 : std::move (outer_attrs));
1348 : 0 : default:
1349 : 0 : add_error (
1350 : 0 : Error (t->get_locus (),
1351 : : "unexpected token %qs in some sort of extern production",
1352 : : t->get_token_description ()));
1353 : :
1354 : 0 : lexer.skip_token (2); // TODO: is this right thing to do?
1355 : 0 : return nullptr;
1356 : : }
1357 : 0 : default:
1358 : 0 : add_error (
1359 : 0 : Error (t->get_locus (),
1360 : : "unexpected token %qs in some sort of extern production",
1361 : : t->get_token_description ()));
1362 : :
1363 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1364 : 0 : return nullptr;
1365 : : }
1366 : 1495 : case USE:
1367 : 1495 : return parse_use_decl (std::move (vis), std::move (outer_attrs));
1368 : 6586 : case FN_KW:
1369 : 6586 : return parse_function (std::move (vis), std::move (outer_attrs));
1370 : 66 : case TYPE:
1371 : 66 : return parse_type_alias (std::move (vis), std::move (outer_attrs));
1372 : 3311 : case STRUCT_KW:
1373 : 3311 : return parse_struct (std::move (vis), std::move (outer_attrs));
1374 : 572 : case ENUM_KW:
1375 : 572 : return parse_enum (std::move (vis), std::move (outer_attrs));
1376 : : // TODO: implement union keyword but not really because of
1377 : : // context-dependence case UNION: crappy hack to do union "keyword"
1378 : 109 : case IDENTIFIER:
1379 : 218 : if (t->get_str () == Values::WeakKeywords::UNION
1380 : 218 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
1381 : : {
1382 : 109 : return parse_union (std::move (vis), std::move (outer_attrs));
1383 : : // or should item switch go straight to parsing union?
1384 : : }
1385 : : else
1386 : : {
1387 : : break;
1388 : : }
1389 : 1129 : case CONST:
1390 : : // lookahead to resolve syntactical production
1391 : 1129 : t = lexer.peek_token (1);
1392 : :
1393 : 1129 : switch (t->get_id ())
1394 : : {
1395 : 1023 : case IDENTIFIER:
1396 : : case UNDERSCORE:
1397 : 1023 : return parse_const_item (std::move (vis), std::move (outer_attrs));
1398 : 1 : case ASYNC:
1399 : 1 : return parse_async_item (std::move (vis), std::move (outer_attrs));
1400 : 105 : case UNSAFE:
1401 : : case EXTERN_KW:
1402 : : case FN_KW:
1403 : 105 : return parse_function (std::move (vis), std::move (outer_attrs));
1404 : 0 : default:
1405 : 0 : add_error (
1406 : 0 : Error (t->get_locus (),
1407 : : "unexpected token %qs in some sort of const production",
1408 : : t->get_token_description ()));
1409 : :
1410 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1411 : 0 : return nullptr;
1412 : : }
1413 : : // for async functions
1414 : 2 : case ASYNC:
1415 : 2 : return parse_async_item (std::move (vis), std::move (outer_attrs));
1416 : :
1417 : 93 : case STATIC_KW:
1418 : 93 : return parse_static_item (std::move (vis), std::move (outer_attrs));
1419 : 3804 : case AUTO:
1420 : : case TRAIT:
1421 : 3804 : return parse_trait (std::move (vis), std::move (outer_attrs));
1422 : 11203 : case IMPL:
1423 : 11203 : return parse_impl (std::move (vis), std::move (outer_attrs));
1424 : 3927 : case UNSAFE: // unsafe traits, unsafe functions, unsafe impls (trait impls),
1425 : : // lookahead to resolve syntactical production
1426 : 3927 : t = lexer.peek_token (1);
1427 : :
1428 : 3927 : switch (t->get_id ())
1429 : : {
1430 : 72 : case AUTO:
1431 : : case TRAIT:
1432 : 72 : return parse_trait (std::move (vis), std::move (outer_attrs));
1433 : 3635 : case EXTERN_KW:
1434 : : case FN_KW:
1435 : 3635 : return parse_function (std::move (vis), std::move (outer_attrs));
1436 : 219 : case IMPL:
1437 : 219 : return parse_impl (std::move (vis), std::move (outer_attrs));
1438 : 1 : case MOD:
1439 : 1 : return parse_module (std::move (vis), std::move (outer_attrs));
1440 : 0 : default:
1441 : 0 : add_error (
1442 : 0 : Error (t->get_locus (),
1443 : : "unexpected token %qs in some sort of unsafe production",
1444 : : t->get_token_description ()));
1445 : :
1446 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1447 : 0 : return nullptr;
1448 : : }
1449 : 64 : case MACRO:
1450 : 64 : return parse_decl_macro_def (std::move (vis), std::move (outer_attrs));
1451 : : default:
1452 : : // otherwise vis item clearly doesn't exist, which is not an error
1453 : : // has a catch-all post-switch return to allow other breaks to occur
1454 : : break;
1455 : : }
1456 : 1 : return nullptr;
1457 : 35518 : }
1458 : :
1459 : : template <typename ManagedTokenSource>
1460 : : std::unique_ptr<AST::Function>
1461 : 4 : Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
1462 : : AST::AttrVec outer_attrs)
1463 : : {
1464 : 4 : auto offset = (lexer.peek_token ()->get_id () == CONST) ? 1 : 0;
1465 : 4 : const_TokenPtr t = lexer.peek_token (offset);
1466 : :
1467 : 4 : if (get_rust_edition () == Edition::E2015)
1468 : : {
1469 : 1 : add_error (Error (t->get_locus (), ErrorCode::E0670,
1470 : : "%<async fn%> is not permitted in Rust 2015"));
1471 : 1 : add_error (
1472 : 2 : Error::Hint (t->get_locus (),
1473 : : "to use %<async fn%>, switch to Rust 2018 or later"));
1474 : : }
1475 : :
1476 : 4 : t = lexer.peek_token (offset + 1);
1477 : :
1478 : 4 : switch (t->get_id ())
1479 : : {
1480 : 4 : case UNSAFE:
1481 : : case FN_KW:
1482 : 4 : return parse_function (std::move (vis), std::move (outer_attrs));
1483 : :
1484 : 0 : default:
1485 : 0 : add_error (
1486 : 0 : Error (t->get_locus (), "expected item, found keyword %<async%>"));
1487 : :
1488 : 0 : lexer.skip_token (1);
1489 : 0 : return nullptr;
1490 : : }
1491 : 4 : }
1492 : :
1493 : : // Parses a macro rules definition syntax extension whatever thing.
1494 : : template <typename ManagedTokenSource>
1495 : : std::unique_ptr<AST::MacroRulesDefinition>
1496 : 1632 : Parser<ManagedTokenSource>::parse_macro_rules_def (AST::AttrVec outer_attrs)
1497 : : {
1498 : : // ensure that first token is identifier saying "macro_rules"
1499 : 1632 : const_TokenPtr t = lexer.peek_token ();
1500 : 1632 : if (t->get_id () != IDENTIFIER
1501 : 1632 : || t->get_str () != Values::WeakKeywords::MACRO_RULES)
1502 : : {
1503 : 0 : Error error (
1504 : : t->get_locus (),
1505 : : "macro rules definition does not start with %<macro_rules%>");
1506 : 0 : add_error (std::move (error));
1507 : :
1508 : : // skip after somewhere?
1509 : 0 : return nullptr;
1510 : 0 : }
1511 : 1632 : lexer.skip_token ();
1512 : 1632 : location_t macro_locus = t->get_locus ();
1513 : :
1514 : 1632 : if (!skip_token (EXCLAM))
1515 : : {
1516 : : // skip after somewhere?
1517 : 0 : return nullptr;
1518 : : }
1519 : :
1520 : : // parse macro name
1521 : 1632 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
1522 : 1632 : if (ident_tok == nullptr)
1523 : : {
1524 : 1 : return nullptr;
1525 : : }
1526 : 1631 : Identifier rule_name{ident_tok};
1527 : :
1528 : : // DEBUG
1529 : 1631 : rust_debug ("in macro rules def, about to parse parens.");
1530 : :
1531 : : // save delim type to ensure it is reused later
1532 : 1631 : AST::DelimType delim_type = AST::PARENS;
1533 : :
1534 : : // Map tokens to DelimType
1535 : 1631 : t = lexer.peek_token ();
1536 : 1631 : switch (t->get_id ())
1537 : : {
1538 : : case LEFT_PAREN:
1539 : : delim_type = AST::PARENS;
1540 : : break;
1541 : 0 : case LEFT_SQUARE:
1542 : 0 : delim_type = AST::SQUARE;
1543 : 0 : break;
1544 : 1630 : case LEFT_CURLY:
1545 : 1630 : delim_type = AST::CURLY;
1546 : 1630 : break;
1547 : 0 : default:
1548 : 0 : add_error (Error (t->get_locus (),
1549 : : "unexpected token %qs - expecting delimiters (for a "
1550 : : "macro rules definition)",
1551 : : t->get_token_description ()));
1552 : :
1553 : 0 : return nullptr;
1554 : : }
1555 : 1631 : lexer.skip_token ();
1556 : :
1557 : : // parse actual macro rules
1558 : 1631 : std::vector<AST::MacroRule> macro_rules;
1559 : :
1560 : : // must be at least one macro rule, so parse it
1561 : 1631 : AST::MacroRule initial_rule = parse_macro_rule ();
1562 : 1631 : if (initial_rule.is_error ())
1563 : : {
1564 : 12 : Error error (lexer.peek_token ()->get_locus (),
1565 : : "required first macro rule in macro rules definition "
1566 : : "could not be parsed");
1567 : 12 : add_error (std::move (error));
1568 : :
1569 : : // skip after somewhere?
1570 : 12 : return nullptr;
1571 : 12 : }
1572 : 1619 : macro_rules.push_back (std::move (initial_rule));
1573 : :
1574 : : // DEBUG
1575 : 1619 : rust_debug ("successfully pushed back initial macro rule");
1576 : :
1577 : 1619 : t = lexer.peek_token ();
1578 : : // parse macro rules
1579 : 1782 : while (t->get_id () == SEMICOLON)
1580 : : {
1581 : : // skip semicolon
1582 : 1269 : lexer.skip_token ();
1583 : :
1584 : : // don't parse if end of macro rules
1585 : 2538 : if (token_id_matches_delims (lexer.peek_token ()->get_id (), delim_type))
1586 : : {
1587 : : // DEBUG
1588 : 1106 : rust_debug (
1589 : : "broke out of parsing macro rules loop due to finding delim");
1590 : :
1591 : 1106 : break;
1592 : : }
1593 : :
1594 : : // try to parse next rule
1595 : 163 : AST::MacroRule rule = parse_macro_rule ();
1596 : 163 : if (rule.is_error ())
1597 : : {
1598 : 0 : Error error (lexer.peek_token ()->get_locus (),
1599 : : "failed to parse macro rule in macro rules definition");
1600 : 0 : add_error (std::move (error));
1601 : :
1602 : 0 : return nullptr;
1603 : 0 : }
1604 : :
1605 : 163 : macro_rules.push_back (std::move (rule));
1606 : :
1607 : : // DEBUG
1608 : 163 : rust_debug ("successfully pushed back another macro rule");
1609 : :
1610 : 163 : t = lexer.peek_token ();
1611 : : }
1612 : :
1613 : : // parse end delimiters
1614 : 1619 : t = lexer.peek_token ();
1615 : 1619 : if (token_id_matches_delims (t->get_id (), delim_type))
1616 : : {
1617 : : // tokens match opening delimiter, so skip.
1618 : 1619 : lexer.skip_token ();
1619 : :
1620 : 1619 : if (delim_type != AST::CURLY)
1621 : : {
1622 : : // skip semicolon at end of non-curly macro definitions
1623 : 1 : if (!skip_token (SEMICOLON))
1624 : : {
1625 : : // as this is the end, allow recovery (probably) - may change
1626 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1627 : 0 : AST::MacroRulesDefinition::mbe (
1628 : : std::move (rule_name), delim_type, std::move (macro_rules),
1629 : 0 : std::move (outer_attrs), macro_locus));
1630 : : }
1631 : : }
1632 : :
1633 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1634 : 3238 : AST::MacroRulesDefinition::mbe (std::move (rule_name), delim_type,
1635 : : std::move (macro_rules),
1636 : 1619 : std::move (outer_attrs), macro_locus));
1637 : : }
1638 : : else
1639 : : {
1640 : : // tokens don't match opening delimiters, so produce error
1641 : 0 : Error error (t->get_locus (),
1642 : : "unexpected token %qs - expecting closing delimiter %qs "
1643 : : "(for a macro rules definition)",
1644 : : t->get_token_description (),
1645 : : (delim_type == AST::PARENS
1646 : : ? ")"
1647 : : : (delim_type == AST::SQUARE ? "]" : "}")));
1648 : 0 : add_error (std::move (error));
1649 : :
1650 : : /* return empty macro definiton despite possibly parsing mostly valid one
1651 : : * - TODO is this a good idea? */
1652 : 0 : return nullptr;
1653 : 0 : }
1654 : 4894 : }
1655 : :
1656 : : // Parses a declarative macro 2.0 definition.
1657 : : template <typename ManagedTokenSource>
1658 : : std::unique_ptr<AST::MacroRulesDefinition>
1659 : 64 : Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis,
1660 : : AST::AttrVec outer_attrs)
1661 : : {
1662 : : // ensure that first token is identifier saying "macro"
1663 : 64 : const_TokenPtr t = lexer.peek_token ();
1664 : 64 : if (t->get_id () != MACRO)
1665 : : {
1666 : 0 : Error error (
1667 : : t->get_locus (),
1668 : : "declarative macro definition does not start with %<macro%>");
1669 : 0 : add_error (std::move (error));
1670 : :
1671 : : // skip after somewhere?
1672 : 0 : return nullptr;
1673 : 0 : }
1674 : 64 : lexer.skip_token ();
1675 : 64 : location_t macro_locus = t->get_locus ();
1676 : :
1677 : : // parse macro name
1678 : 64 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
1679 : 64 : if (ident_tok == nullptr)
1680 : : {
1681 : 0 : return nullptr;
1682 : : }
1683 : 64 : Identifier rule_name{ident_tok};
1684 : :
1685 : 64 : t = lexer.peek_token ();
1686 : 64 : if (t->get_id () == LEFT_PAREN)
1687 : : {
1688 : : // single definiton of macro rule
1689 : : // e.g. `macro foo($e:expr) {}`
1690 : :
1691 : : // parse macro matcher
1692 : 39 : location_t locus = lexer.peek_token ()->get_locus ();
1693 : 39 : AST::MacroMatcher matcher = parse_macro_matcher ();
1694 : 39 : if (matcher.is_error ())
1695 : 0 : return nullptr;
1696 : :
1697 : : // check delimiter of macro matcher
1698 : 39 : if (matcher.get_delim_type () != AST::DelimType::PARENS)
1699 : : {
1700 : 0 : Error error (locus, "only parenthesis can be used for a macro "
1701 : : "matcher in declarative macro definition");
1702 : 0 : add_error (std::move (error));
1703 : 0 : return nullptr;
1704 : 0 : }
1705 : :
1706 : 39 : location_t transcriber_loc = lexer.peek_token ()->get_locus ();
1707 : 39 : AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
1708 : 39 : AST::MacroTranscriber transcriber (delim_tok_tree, transcriber_loc);
1709 : :
1710 : 39 : if (transcriber.get_token_tree ().get_delim_type ()
1711 : : != AST::DelimType::CURLY)
1712 : : {
1713 : 1 : Error error (transcriber_loc,
1714 : : "only braces can be used for a macro transcriber "
1715 : : "in declarative macro definition");
1716 : 1 : add_error (std::move (error));
1717 : 1 : return nullptr;
1718 : 1 : }
1719 : :
1720 : 38 : std::vector<AST::MacroRule> macro_rules;
1721 : 38 : macro_rules.emplace_back (std::move (matcher), std::move (transcriber),
1722 : : locus);
1723 : :
1724 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1725 : 76 : AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
1726 : : macro_rules,
1727 : : std::move (outer_attrs),
1728 : 38 : macro_locus, vis));
1729 : 116 : }
1730 : 25 : else if (t->get_id () == LEFT_CURLY)
1731 : : {
1732 : : // multiple definitions of macro rule separated by comma
1733 : : // e.g. `macro foo { () => {}, ($e:expr) => {}, }`
1734 : :
1735 : : // parse left curly
1736 : 25 : const_TokenPtr left_curly = expect_token (LEFT_CURLY);
1737 : 25 : if (left_curly == nullptr)
1738 : : {
1739 : 0 : return nullptr;
1740 : : }
1741 : :
1742 : : // parse actual macro rules
1743 : 25 : std::vector<AST::MacroRule> macro_rules;
1744 : :
1745 : : // must be at least one macro rule, so parse it
1746 : 25 : AST::MacroRule initial_rule = parse_macro_rule ();
1747 : 25 : if (initial_rule.is_error ())
1748 : : {
1749 : 1 : Error error (
1750 : 1 : lexer.peek_token ()->get_locus (),
1751 : : "required first macro rule in declarative macro definition "
1752 : : "could not be parsed");
1753 : 1 : add_error (std::move (error));
1754 : :
1755 : : // skip after somewhere?
1756 : 1 : return nullptr;
1757 : 1 : }
1758 : 24 : macro_rules.push_back (std::move (initial_rule));
1759 : :
1760 : 24 : t = lexer.peek_token ();
1761 : : // parse macro rules
1762 : 40 : while (t->get_id () == COMMA)
1763 : : {
1764 : : // skip comma
1765 : 32 : lexer.skip_token ();
1766 : :
1767 : : // don't parse if end of macro rules
1768 : 64 : if (token_id_matches_delims (lexer.peek_token ()->get_id (),
1769 : : AST::CURLY))
1770 : : {
1771 : : break;
1772 : : }
1773 : :
1774 : : // try to parse next rule
1775 : 16 : AST::MacroRule rule = parse_macro_rule ();
1776 : 16 : if (rule.is_error ())
1777 : : {
1778 : 0 : Error error (
1779 : 0 : lexer.peek_token ()->get_locus (),
1780 : : "failed to parse macro rule in declarative macro definition");
1781 : 0 : add_error (std::move (error));
1782 : :
1783 : 0 : return nullptr;
1784 : 0 : }
1785 : :
1786 : 16 : macro_rules.push_back (std::move (rule));
1787 : :
1788 : 16 : t = lexer.peek_token ();
1789 : : }
1790 : :
1791 : : // parse right curly
1792 : 24 : const_TokenPtr right_curly = expect_token (RIGHT_CURLY);
1793 : 24 : if (right_curly == nullptr)
1794 : : {
1795 : 0 : return nullptr;
1796 : : }
1797 : :
1798 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1799 : 48 : AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
1800 : : std::move (macro_rules),
1801 : : std::move (outer_attrs),
1802 : 24 : macro_locus, vis));
1803 : 50 : }
1804 : : else
1805 : : {
1806 : 0 : add_error (Error (t->get_locus (),
1807 : : "unexpected token %qs - expecting delimiters "
1808 : : "(for a declarative macro definiton)",
1809 : : t->get_token_description ()));
1810 : 0 : return nullptr;
1811 : : }
1812 : 128 : }
1813 : :
1814 : : // Parses a semi-coloned (except for full block) macro invocation item.
1815 : : template <typename ManagedTokenSource>
1816 : : std::unique_ptr<AST::MacroInvocation>
1817 : 4976 : Parser<ManagedTokenSource>::parse_macro_invocation_semi (
1818 : : AST::AttrVec outer_attrs)
1819 : : {
1820 : 4976 : location_t macro_locus = lexer.peek_token ()->get_locus ();
1821 : 4976 : AST::SimplePath path = parse_simple_path ();
1822 : :
1823 : 4976 : if (!skip_token (EXCLAM))
1824 : : {
1825 : : // skip after somewhere?
1826 : 0 : return nullptr;
1827 : : }
1828 : :
1829 : : // save delim type to ensure it is reused later
1830 : 4976 : AST::DelimType delim_type = AST::PARENS;
1831 : :
1832 : : // Map tokens to DelimType
1833 : 4976 : const_TokenPtr t = lexer.peek_token ();
1834 : 4976 : switch (t->get_id ())
1835 : : {
1836 : : case LEFT_PAREN:
1837 : : delim_type = AST::PARENS;
1838 : : break;
1839 : 0 : case LEFT_SQUARE:
1840 : 0 : delim_type = AST::SQUARE;
1841 : 0 : break;
1842 : 3506 : case LEFT_CURLY:
1843 : 3506 : delim_type = AST::CURLY;
1844 : 3506 : break;
1845 : 0 : default:
1846 : 0 : add_error (Error (t->get_locus (),
1847 : : "unexpected token %qs - expecting delimiters (for a "
1848 : : "macro invocation semi body)",
1849 : : t->get_token_description ()));
1850 : :
1851 : 0 : return nullptr;
1852 : : }
1853 : 4976 : location_t tok_tree_locus = t->get_locus ();
1854 : 4976 : lexer.skip_token ();
1855 : :
1856 : : // parse actual token trees
1857 : 4976 : std::vector<std::unique_ptr<AST::TokenTree>> token_trees;
1858 : 4976 : auto delim_open
1859 : 4976 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1860 : 4976 : token_trees.push_back (std::move (delim_open));
1861 : :
1862 : 4976 : t = lexer.peek_token ();
1863 : : // parse token trees until the initial delimiter token is found again
1864 : 63963 : while (!token_id_matches_delims (t->get_id (), delim_type))
1865 : : {
1866 : 58987 : std::unique_ptr<AST::TokenTree> tree = parse_token_tree ();
1867 : :
1868 : 58987 : if (tree == nullptr)
1869 : : {
1870 : 0 : Error error (t->get_locus (),
1871 : : "failed to parse token tree for macro invocation semi "
1872 : : "- found %qs",
1873 : : t->get_token_description ());
1874 : 0 : add_error (std::move (error));
1875 : :
1876 : 0 : return nullptr;
1877 : 0 : }
1878 : :
1879 : 58987 : token_trees.push_back (std::move (tree));
1880 : :
1881 : 58987 : t = lexer.peek_token ();
1882 : : }
1883 : 4976 : auto delim_close
1884 : 4976 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1885 : 4976 : token_trees.push_back (std::move (delim_close));
1886 : :
1887 : 4976 : AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees),
1888 : : tok_tree_locus);
1889 : 4976 : AST::MacroInvocData invoc_data (std::move (path), std::move (delim_tok_tree));
1890 : :
1891 : : // parse end delimiters
1892 : 4976 : t = lexer.peek_token ();
1893 : 4976 : if (token_id_matches_delims (t->get_id (), delim_type))
1894 : : {
1895 : : // tokens match opening delimiter, so skip.
1896 : 4976 : lexer.skip_token ();
1897 : :
1898 : 4976 : if (delim_type != AST::CURLY)
1899 : : {
1900 : : // skip semicolon at end of non-curly macro invocation semis
1901 : 1470 : if (!skip_token (SEMICOLON))
1902 : : {
1903 : : // as this is the end, allow recovery (probably) - may change
1904 : :
1905 : 0 : return AST::MacroInvocation::Regular (std::move (invoc_data),
1906 : : std::move (outer_attrs),
1907 : 0 : macro_locus, true);
1908 : : }
1909 : : }
1910 : :
1911 : : // DEBUG:
1912 : 9952 : rust_debug ("skipped token is '%s', next token (current peek) is '%s'",
1913 : : t->get_token_description (),
1914 : : lexer.peek_token ()->get_token_description ());
1915 : :
1916 : 9952 : return AST::MacroInvocation::Regular (std::move (invoc_data),
1917 : : std::move (outer_attrs),
1918 : 4976 : macro_locus, true);
1919 : : }
1920 : : else
1921 : : {
1922 : : // tokens don't match opening delimiters, so produce error
1923 : 0 : Error error (t->get_locus (),
1924 : : "unexpected token %qs - expecting closing delimiter %qs "
1925 : : "(for a macro invocation semi)",
1926 : : t->get_token_description (),
1927 : : (delim_type == AST::PARENS
1928 : : ? ")"
1929 : : : (delim_type == AST::SQUARE ? "]" : "}")));
1930 : 0 : add_error (std::move (error));
1931 : :
1932 : : /* return empty macro invocation despite possibly parsing mostly valid one
1933 : : * - TODO is this a good idea? */
1934 : 0 : return nullptr;
1935 : 0 : }
1936 : 9952 : }
1937 : :
1938 : : // Parses a non-semicoloned macro invocation (i.e. as pattern or expression).
1939 : : template <typename ManagedTokenSource>
1940 : : std::unique_ptr<AST::MacroInvocation>
1941 : 1710 : Parser<ManagedTokenSource>::parse_macro_invocation (AST::AttrVec outer_attrs)
1942 : : {
1943 : : // parse macro path
1944 : 1710 : AST::SimplePath macro_path = parse_simple_path ();
1945 : 1710 : if (macro_path.is_empty ())
1946 : : {
1947 : 296 : Error error (lexer.peek_token ()->get_locus (),
1948 : : "failed to parse macro invocation path");
1949 : 296 : add_error (std::move (error));
1950 : :
1951 : : // skip?
1952 : 296 : return nullptr;
1953 : 296 : }
1954 : :
1955 : 1414 : if (!skip_token (EXCLAM))
1956 : : {
1957 : : // skip after somewhere?
1958 : 3 : return nullptr;
1959 : : }
1960 : :
1961 : : // parse internal delim token tree
1962 : 1411 : AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
1963 : :
1964 : 1411 : location_t macro_locus = macro_path.get_locus ();
1965 : :
1966 : 2822 : return AST::MacroInvocation::Regular (
1967 : 2822 : AST::MacroInvocData (std::move (macro_path), std::move (delim_tok_tree)),
1968 : 1411 : std::move (outer_attrs), macro_locus);
1969 : 1411 : }
1970 : :
1971 : : // Parses a macro rule definition - does not parse semicolons.
1972 : : template <typename ManagedTokenSource>
1973 : : AST::MacroRule
1974 : 1835 : Parser<ManagedTokenSource>::parse_macro_rule ()
1975 : : {
1976 : 1835 : location_t locus = lexer.peek_token ()->get_locus ();
1977 : :
1978 : : // parse macro matcher
1979 : 1835 : AST::MacroMatcher matcher = parse_macro_matcher ();
1980 : :
1981 : 1835 : if (matcher.is_error ())
1982 : 13 : return AST::MacroRule::create_error (locus);
1983 : :
1984 : 1822 : if (!skip_token (MATCH_ARROW))
1985 : : {
1986 : : // skip after somewhere?
1987 : 0 : return AST::MacroRule::create_error (locus);
1988 : : }
1989 : :
1990 : : // parse transcriber (this is just a delim token tree)
1991 : 1822 : location_t token_tree_loc = lexer.peek_token ()->get_locus ();
1992 : 1822 : AST::MacroTranscriber transcriber (parse_delim_token_tree (), token_tree_loc);
1993 : :
1994 : 1822 : return AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
1995 : 1822 : }
1996 : :
1997 : : // Parses a macro matcher (part of a macro rule definition).
1998 : : template <typename ManagedTokenSource>
1999 : : AST::MacroMatcher
2000 : 1981 : Parser<ManagedTokenSource>::parse_macro_matcher ()
2001 : : {
2002 : : // save delim type to ensure it is reused later
2003 : 1981 : AST::DelimType delim_type = AST::PARENS;
2004 : :
2005 : : // DEBUG
2006 : 1981 : rust_debug ("begun parsing macro matcher");
2007 : :
2008 : : // Map tokens to DelimType
2009 : 1981 : const_TokenPtr t = lexer.peek_token ();
2010 : 1981 : location_t locus = t->get_locus ();
2011 : 1981 : switch (t->get_id ())
2012 : : {
2013 : : case LEFT_PAREN:
2014 : : delim_type = AST::PARENS;
2015 : : break;
2016 : 65 : case LEFT_SQUARE:
2017 : 65 : delim_type = AST::SQUARE;
2018 : 65 : break;
2019 : 24 : case LEFT_CURLY:
2020 : 24 : delim_type = AST::CURLY;
2021 : 24 : break;
2022 : 1 : default:
2023 : 1 : add_error (Error (
2024 : : t->get_locus (),
2025 : : "unexpected token %qs - expecting delimiters (for a macro matcher)",
2026 : : t->get_token_description ()));
2027 : :
2028 : 1 : return AST::MacroMatcher::create_error (t->get_locus ());
2029 : : }
2030 : 1980 : lexer.skip_token ();
2031 : :
2032 : : // parse actual macro matches
2033 : 1980 : std::vector<std::unique_ptr<AST::MacroMatch>> matches;
2034 : : // Set of possible preceding macro matches to make sure follow-set
2035 : : // restrictions are respected.
2036 : : // TODO: Consider using std::reference_wrapper instead of raw pointers?
2037 : 1980 : std::vector<const AST::MacroMatch *> last_matches;
2038 : :
2039 : 1980 : t = lexer.peek_token ();
2040 : : // parse token trees until the initial delimiter token is found again
2041 : 6341 : while (!token_id_matches_delims (t->get_id (), delim_type))
2042 : : {
2043 : 4361 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
2044 : :
2045 : 4361 : if (match == nullptr)
2046 : : {
2047 : 1 : Error error (
2048 : : t->get_locus (),
2049 : : "failed to parse macro match for macro matcher - found %qs",
2050 : : t->get_token_description ());
2051 : 1 : add_error (std::move (error));
2052 : :
2053 : 1 : return AST::MacroMatcher::create_error (t->get_locus ());
2054 : 1 : }
2055 : :
2056 : 4360 : if (matches.size () > 0)
2057 : : {
2058 : 2752 : const auto *last_match = matches.back ().get ();
2059 : :
2060 : : // We want to check if we are dealing with a zeroable repetition
2061 : 2752 : bool zeroable = false;
2062 : 2752 : if (last_match->get_macro_match_type ()
2063 : : == AST::MacroMatch::MacroMatchType::Repetition)
2064 : : {
2065 : 42 : auto repetition
2066 : : = static_cast<const AST::MacroMatchRepetition *> (last_match);
2067 : :
2068 : 42 : if (repetition->get_op ()
2069 : : != AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE)
2070 : : zeroable = true;
2071 : : }
2072 : :
2073 : : if (!zeroable)
2074 : 2752 : last_matches.clear ();
2075 : :
2076 : 2752 : last_matches.emplace_back (last_match);
2077 : :
2078 : 5507 : for (auto last : last_matches)
2079 : 2767 : if (!is_match_compatible (*last, *match))
2080 : : return AST::MacroMatcher::create_error (
2081 : 12 : match->get_match_locus ());
2082 : : }
2083 : :
2084 : 4348 : matches.push_back (std::move (match));
2085 : :
2086 : : // DEBUG
2087 : 4348 : rust_debug ("pushed back a match in macro matcher");
2088 : :
2089 : 4348 : t = lexer.peek_token ();
2090 : : }
2091 : :
2092 : : // parse end delimiters
2093 : 1967 : t = lexer.peek_token ();
2094 : 1967 : if (token_id_matches_delims (t->get_id (), delim_type))
2095 : : {
2096 : : // tokens match opening delimiter, so skip.
2097 : 1967 : lexer.skip_token ();
2098 : :
2099 : 1967 : return AST::MacroMatcher (delim_type, std::move (matches), locus);
2100 : : }
2101 : : else
2102 : : {
2103 : : // tokens don't match opening delimiters, so produce error
2104 : 0 : Error error (t->get_locus (),
2105 : : "unexpected token %qs - expecting closing delimiter %qs "
2106 : : "(for a macro matcher)",
2107 : : t->get_token_description (),
2108 : : (delim_type == AST::PARENS
2109 : : ? ")"
2110 : : : (delim_type == AST::SQUARE ? "]" : "}")));
2111 : 0 : add_error (std::move (error));
2112 : :
2113 : : /* return error macro matcher despite possibly parsing mostly correct one?
2114 : : * TODO is this the best idea? */
2115 : 0 : return AST::MacroMatcher::create_error (t->get_locus ());
2116 : 0 : }
2117 : 1980 : }
2118 : :
2119 : : // Parses a macro match (syntax match inside a matcher in a macro rule).
2120 : : template <typename ManagedTokenSource>
2121 : : std::unique_ptr<AST::MacroMatch>
2122 : 5245 : Parser<ManagedTokenSource>::parse_macro_match ()
2123 : : {
2124 : : // branch based on token available
2125 : 5245 : const_TokenPtr t = lexer.peek_token ();
2126 : 5245 : switch (t->get_id ())
2127 : : {
2128 : 107 : case LEFT_PAREN:
2129 : : case LEFT_SQUARE:
2130 : : case LEFT_CURLY:
2131 : : {
2132 : : // must be macro matcher as delimited
2133 : 107 : AST::MacroMatcher matcher = parse_macro_matcher ();
2134 : 107 : if (matcher.is_error ())
2135 : : {
2136 : 1 : Error error (lexer.peek_token ()->get_locus (),
2137 : : "failed to parse macro matcher in macro match");
2138 : 1 : add_error (std::move (error));
2139 : :
2140 : 1 : return nullptr;
2141 : 1 : }
2142 : 106 : return std::unique_ptr<AST::MacroMatcher> (
2143 : 106 : new AST::MacroMatcher (std::move (matcher)));
2144 : 107 : }
2145 : 3432 : case DOLLAR_SIGN:
2146 : : {
2147 : : // have to do more lookahead to determine if fragment or repetition
2148 : 3432 : const_TokenPtr t2 = lexer.peek_token (1);
2149 : 3432 : switch (t2->get_id ())
2150 : : {
2151 : 2745 : case IDENTIFIER:
2152 : : case UNDERSCORE:
2153 : : // macro fragment
2154 : 2745 : return parse_macro_match_fragment ();
2155 : 673 : case LEFT_PAREN:
2156 : : // macro repetition
2157 : 673 : return parse_macro_match_repetition ();
2158 : 14 : default:
2159 : 14 : if (token_id_is_keyword (t2->get_id ()) && t2->get_id () != CRATE)
2160 : : {
2161 : : // keyword as macro fragment
2162 : 14 : return parse_macro_match_fragment ();
2163 : : }
2164 : : else
2165 : : {
2166 : : // error: unrecognised
2167 : 0 : add_error (Error (
2168 : : t2->get_locus (),
2169 : : "unrecognised token combination %<$%s%> at start of "
2170 : : "macro match - did you mean %<$identifier%> or %<$(%>?",
2171 : : t2->get_token_description ()));
2172 : :
2173 : : // skip somewhere?
2174 : 0 : return nullptr;
2175 : : }
2176 : : }
2177 : 3432 : }
2178 : 0 : case RIGHT_PAREN:
2179 : : case RIGHT_SQUARE:
2180 : : case RIGHT_CURLY:
2181 : : // not allowed
2182 : 0 : add_error (Error (
2183 : : t->get_locus (),
2184 : : "closing delimiters like %qs are not allowed at the start of a macro "
2185 : : "match",
2186 : : t->get_token_description ()));
2187 : :
2188 : : // skip somewhere?
2189 : 0 : return nullptr;
2190 : 1706 : default:
2191 : : // just the token
2192 : 1706 : lexer.skip_token ();
2193 : 1706 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
2194 : : }
2195 : 5245 : }
2196 : :
2197 : : // Parses a fragment macro match.
2198 : : template <typename ManagedTokenSource>
2199 : : std::unique_ptr<AST::MacroMatchFragment>
2200 : 2759 : Parser<ManagedTokenSource>::parse_macro_match_fragment ()
2201 : : {
2202 : 2759 : location_t fragment_locus = lexer.peek_token ()->get_locus ();
2203 : 2759 : skip_token (DOLLAR_SIGN);
2204 : :
2205 : 2759 : Identifier ident;
2206 : 2759 : auto identifier = lexer.peek_token ();
2207 : 2759 : if (identifier->get_id () == UNDERSCORE)
2208 : 6 : ident = {Values::Keywords::UNDERSCORE, identifier->get_locus ()};
2209 : : else
2210 : 5512 : ident = {identifier};
2211 : :
2212 : 2759 : if (ident.empty ())
2213 : : {
2214 : 0 : Error error (lexer.peek_token ()->get_locus (),
2215 : : "missing identifier in macro match fragment");
2216 : 0 : add_error (std::move (error));
2217 : :
2218 : 0 : return nullptr;
2219 : 0 : }
2220 : 2759 : skip_token (identifier->get_id ());
2221 : :
2222 : 2759 : if (!skip_token (COLON))
2223 : : {
2224 : : // skip after somewhere?
2225 : 0 : return nullptr;
2226 : : }
2227 : :
2228 : : // get MacroFragSpec for macro
2229 : 2759 : const_TokenPtr t = expect_token (IDENTIFIER);
2230 : 2759 : if (t == nullptr)
2231 : 0 : return nullptr;
2232 : :
2233 : : AST::MacroFragSpec frag
2234 : 2759 : = AST::MacroFragSpec::get_frag_spec_from_str (t->get_str ());
2235 : 2759 : if (frag.is_error ())
2236 : : {
2237 : 0 : Error error (t->get_locus (),
2238 : : "invalid fragment specifier %qs in fragment macro match",
2239 : 0 : t->get_str ().c_str ());
2240 : 0 : add_error (std::move (error));
2241 : :
2242 : 0 : return nullptr;
2243 : 0 : }
2244 : :
2245 : : return std::unique_ptr<AST::MacroMatchFragment> (
2246 : 2759 : new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
2247 : 5518 : }
2248 : :
2249 : : // Parses a repetition macro match.
2250 : : template <typename ManagedTokenSource>
2251 : : std::unique_ptr<AST::MacroMatchRepetition>
2252 : 673 : Parser<ManagedTokenSource>::parse_macro_match_repetition ()
2253 : : {
2254 : 673 : skip_token (DOLLAR_SIGN);
2255 : 673 : skip_token (LEFT_PAREN);
2256 : :
2257 : 673 : std::vector<std::unique_ptr<AST::MacroMatch>> matches;
2258 : :
2259 : : // parse required first macro match
2260 : 673 : std::unique_ptr<AST::MacroMatch> initial_match = parse_macro_match ();
2261 : 673 : if (initial_match == nullptr)
2262 : : {
2263 : 0 : Error error (
2264 : 0 : lexer.peek_token ()->get_locus (),
2265 : : "could not parse required first macro match in macro match repetition");
2266 : 0 : add_error (std::move (error));
2267 : :
2268 : : // skip after somewhere?
2269 : 0 : return nullptr;
2270 : 0 : }
2271 : 673 : matches.push_back (std::move (initial_match));
2272 : :
2273 : : // parse optional later macro matches
2274 : 673 : const_TokenPtr t = lexer.peek_token ();
2275 : 884 : while (t->get_id () != RIGHT_PAREN)
2276 : : {
2277 : 211 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
2278 : :
2279 : 211 : if (match == nullptr)
2280 : : {
2281 : 0 : Error error (lexer.peek_token ()->get_locus (),
2282 : : "failed to parse macro match in macro match repetition");
2283 : 0 : add_error (std::move (error));
2284 : :
2285 : 0 : return nullptr;
2286 : 0 : }
2287 : :
2288 : 211 : matches.push_back (std::move (match));
2289 : :
2290 : 211 : t = lexer.peek_token ();
2291 : : }
2292 : :
2293 : 673 : if (!skip_token (RIGHT_PAREN))
2294 : : {
2295 : : // skip after somewhere?
2296 : 0 : return nullptr;
2297 : : }
2298 : :
2299 : 673 : t = lexer.peek_token ();
2300 : : // see if separator token exists
2301 : 673 : std::unique_ptr<AST::Token> separator = nullptr;
2302 : 673 : switch (t->get_id ())
2303 : : {
2304 : : // repetition operators
2305 : : case ASTERISK:
2306 : : case PLUS:
2307 : : case QUESTION_MARK:
2308 : : // delimiters
2309 : : case LEFT_PAREN:
2310 : : case LEFT_CURLY:
2311 : : case LEFT_SQUARE:
2312 : : case RIGHT_PAREN:
2313 : : case RIGHT_CURLY:
2314 : : case RIGHT_SQUARE:
2315 : : // separator does not exist, so still null and don't skip token
2316 : : break;
2317 : 165 : default:
2318 : : // separator does exist
2319 : 165 : separator = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
2320 : 165 : lexer.skip_token ();
2321 : : break;
2322 : : }
2323 : :
2324 : : // parse repetition operator
2325 : 673 : t = lexer.peek_token ();
2326 : 673 : AST::MacroMatchRepetition::MacroRepOp op = AST::MacroMatchRepetition::NONE;
2327 : 673 : switch (t->get_id ())
2328 : : {
2329 : 536 : case ASTERISK:
2330 : 536 : op = AST::MacroMatchRepetition::ANY;
2331 : 536 : lexer.skip_token ();
2332 : : break;
2333 : 79 : case PLUS:
2334 : 79 : op = AST::MacroMatchRepetition::ONE_OR_MORE;
2335 : 79 : lexer.skip_token ();
2336 : : break;
2337 : 58 : case QUESTION_MARK:
2338 : 58 : op = AST::MacroMatchRepetition::ZERO_OR_ONE;
2339 : 58 : lexer.skip_token ();
2340 : :
2341 : 58 : if (separator != nullptr)
2342 : : {
2343 : 1 : add_error (
2344 : 2 : Error (separator->get_locus (),
2345 : : "the %<?%> macro repetition operator does not take a "
2346 : : "separator"));
2347 : 1 : separator = nullptr;
2348 : : }
2349 : :
2350 : : break;
2351 : 0 : default:
2352 : 0 : add_error (
2353 : 0 : Error (t->get_locus (),
2354 : : "expected macro repetition operator (%<*%>, %<+%>, or %<?%>) in "
2355 : : "macro match - found %qs",
2356 : : t->get_token_description ()));
2357 : :
2358 : : // skip after somewhere?
2359 : 0 : return nullptr;
2360 : : }
2361 : :
2362 : : return std::unique_ptr<AST::MacroMatchRepetition> (
2363 : 673 : new AST::MacroMatchRepetition (std::move (matches), op,
2364 : 673 : std::move (separator), t->get_locus ()));
2365 : 1346 : }
2366 : :
2367 : : /* Parses a visibility syntactical production (i.e. creating a non-default
2368 : : * visibility) */
2369 : : template <typename ManagedTokenSource>
2370 : : AST::Visibility
2371 : 53186 : Parser<ManagedTokenSource>::parse_visibility ()
2372 : : {
2373 : : // check for no visibility
2374 : 106372 : if (lexer.peek_token ()->get_id () != PUB)
2375 : : {
2376 : 39026 : return AST::Visibility::create_private ();
2377 : : }
2378 : :
2379 : 14160 : auto vis_loc = lexer.peek_token ()->get_locus ();
2380 : 14160 : lexer.skip_token ();
2381 : :
2382 : : // create simple pub visibility if
2383 : : // - found no parentheses
2384 : : // - found unit type `()`
2385 : 28320 : if (lexer.peek_token ()->get_id () != LEFT_PAREN
2386 : 14541 : || lexer.peek_token (1)->get_id () == RIGHT_PAREN)
2387 : : {
2388 : 13780 : return AST::Visibility::create_public (vis_loc);
2389 : : // or whatever
2390 : : }
2391 : :
2392 : 380 : lexer.skip_token ();
2393 : :
2394 : 380 : const_TokenPtr t = lexer.peek_token ();
2395 : 380 : auto path_loc = t->get_locus ();
2396 : :
2397 : 380 : switch (t->get_id ())
2398 : : {
2399 : 266 : case CRATE:
2400 : 266 : lexer.skip_token ();
2401 : :
2402 : 266 : skip_token (RIGHT_PAREN);
2403 : :
2404 : 266 : return AST::Visibility::create_crate (path_loc, vis_loc);
2405 : 0 : case SELF:
2406 : 0 : lexer.skip_token ();
2407 : :
2408 : 0 : skip_token (RIGHT_PAREN);
2409 : :
2410 : 0 : return AST::Visibility::create_self (path_loc, vis_loc);
2411 : 96 : case SUPER:
2412 : 96 : lexer.skip_token ();
2413 : :
2414 : 96 : skip_token (RIGHT_PAREN);
2415 : :
2416 : 96 : return AST::Visibility::create_super (path_loc, vis_loc);
2417 : 18 : case IN:
2418 : : {
2419 : 18 : lexer.skip_token ();
2420 : :
2421 : : // parse the "in" path as well
2422 : 18 : AST::SimplePath path = parse_simple_path ();
2423 : 18 : if (path.is_empty ())
2424 : : {
2425 : 0 : Error error (lexer.peek_token ()->get_locus (),
2426 : : "missing path in pub(in path) visibility");
2427 : 0 : add_error (std::move (error));
2428 : :
2429 : : // skip after somewhere?
2430 : 0 : return AST::Visibility::create_error ();
2431 : 0 : }
2432 : :
2433 : 18 : skip_token (RIGHT_PAREN);
2434 : :
2435 : 18 : return AST::Visibility::create_in_path (std::move (path), vis_loc);
2436 : 18 : }
2437 : 0 : default:
2438 : 0 : add_error (Error (t->get_locus (), "unexpected token %qs in visibility",
2439 : : t->get_token_description ()));
2440 : :
2441 : 0 : lexer.skip_token ();
2442 : 0 : return AST::Visibility::create_error ();
2443 : : }
2444 : 380 : }
2445 : :
2446 : : // Parses a module - either a bodied module or a module defined in another file.
2447 : : template <typename ManagedTokenSource>
2448 : : std::unique_ptr<AST::Module>
2449 : 1606 : Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
2450 : : AST::AttrVec outer_attrs)
2451 : : {
2452 : 1606 : location_t locus = lexer.peek_token ()->get_locus ();
2453 : :
2454 : 1606 : Unsafety safety = Unsafety::Normal;
2455 : 3212 : if (lexer.peek_token ()->get_id () == UNSAFE)
2456 : : {
2457 : 1 : safety = Unsafety::Unsafe;
2458 : 1 : skip_token (UNSAFE);
2459 : : }
2460 : :
2461 : 1606 : skip_token (MOD);
2462 : :
2463 : 1606 : const_TokenPtr module_name = expect_token (IDENTIFIER);
2464 : 1606 : if (module_name == nullptr)
2465 : : {
2466 : 0 : return nullptr;
2467 : : }
2468 : 1606 : Identifier name{module_name};
2469 : :
2470 : 1606 : const_TokenPtr t = lexer.peek_token ();
2471 : :
2472 : 1606 : switch (t->get_id ())
2473 : : {
2474 : 288 : case SEMICOLON:
2475 : 288 : lexer.skip_token ();
2476 : :
2477 : : // Construct an external module
2478 : : return std::unique_ptr<AST::Module> (
2479 : 864 : new AST::Module (std::move (name), std::move (vis),
2480 : : std::move (outer_attrs), locus, safety,
2481 : 864 : lexer.get_filename (), inline_module_stack));
2482 : 1318 : case LEFT_CURLY:
2483 : : {
2484 : 1318 : lexer.skip_token ();
2485 : :
2486 : : // parse inner attributes
2487 : 1318 : AST::AttrVec inner_attrs = parse_inner_attributes ();
2488 : :
2489 : 1318 : std::string default_path = name.as_string ();
2490 : :
2491 : 1318 : if (inline_module_stack.empty ())
2492 : : {
2493 : 778 : std::string filename = lexer.get_filename ();
2494 : 778 : auto slash_idx = filename.rfind (file_separator);
2495 : 778 : if (slash_idx == std::string::npos)
2496 : : slash_idx = 0;
2497 : : else
2498 : 778 : slash_idx++;
2499 : 778 : filename = filename.substr (slash_idx);
2500 : :
2501 : 778 : std::string subdir;
2502 : 778 : if (get_file_subdir (filename, subdir))
2503 : 770 : default_path = subdir + file_separator + name.as_string ();
2504 : 778 : }
2505 : :
2506 : 1318 : std::string module_path_name
2507 : : = extract_module_path (inner_attrs, outer_attrs, default_path);
2508 : 1318 : InlineModuleStackScope scope (*this, std::move (module_path_name));
2509 : :
2510 : : // parse items
2511 : 1318 : std::vector<std::unique_ptr<AST::Item>> items;
2512 : 1318 : const_TokenPtr tok = lexer.peek_token ();
2513 : 6240 : while (tok->get_id () != RIGHT_CURLY)
2514 : : {
2515 : 4922 : std::unique_ptr<AST::Item> item = parse_item (false);
2516 : 4922 : if (item == nullptr)
2517 : : {
2518 : 0 : Error error (tok->get_locus (),
2519 : : "failed to parse item in module");
2520 : 0 : add_error (std::move (error));
2521 : :
2522 : 0 : return nullptr;
2523 : 0 : }
2524 : :
2525 : 4922 : items.push_back (std::move (item));
2526 : :
2527 : 4922 : tok = lexer.peek_token ();
2528 : : }
2529 : :
2530 : 1318 : if (!skip_token (RIGHT_CURLY))
2531 : : {
2532 : : // skip somewhere?
2533 : 0 : return nullptr;
2534 : : }
2535 : :
2536 : : return std::unique_ptr<AST::Module> (
2537 : 1318 : new AST::Module (std::move (name), locus, std::move (items),
2538 : : std::move (vis), safety, std::move (inner_attrs),
2539 : 1318 : std::move (outer_attrs))); // module name?
2540 : 1318 : }
2541 : 0 : default:
2542 : 0 : add_error (
2543 : 0 : Error (t->get_locus (),
2544 : : "unexpected token %qs in module declaration/definition item",
2545 : : t->get_token_description ()));
2546 : :
2547 : 0 : lexer.skip_token ();
2548 : 0 : return nullptr;
2549 : : }
2550 : 1606 : }
2551 : :
2552 : : // Parses an extern crate declaration (dependency on external crate)
2553 : : template <typename ManagedTokenSource>
2554 : : std::unique_ptr<AST::ExternCrate>
2555 : 27 : Parser<ManagedTokenSource>::parse_extern_crate (AST::Visibility vis,
2556 : : AST::AttrVec outer_attrs)
2557 : : {
2558 : 27 : location_t locus = lexer.peek_token ()->get_locus ();
2559 : 27 : if (!skip_token (EXTERN_KW))
2560 : : {
2561 : 0 : skip_after_semicolon ();
2562 : 0 : return nullptr;
2563 : : }
2564 : :
2565 : 27 : if (!skip_token (CRATE))
2566 : : {
2567 : 0 : skip_after_semicolon ();
2568 : 0 : return nullptr;
2569 : : }
2570 : :
2571 : : /* parse crate reference name - this has its own syntactical rule in reference
2572 : : * but seems to not be used elsewhere, so i'm putting it here */
2573 : 27 : const_TokenPtr crate_name_tok = lexer.peek_token ();
2574 : 27 : std::string crate_name;
2575 : :
2576 : 27 : switch (crate_name_tok->get_id ())
2577 : : {
2578 : 27 : case IDENTIFIER:
2579 : 27 : crate_name = crate_name_tok->get_str ();
2580 : 27 : lexer.skip_token ();
2581 : : break;
2582 : 0 : case SELF:
2583 : 0 : crate_name = Values::Keywords::SELF;
2584 : 0 : lexer.skip_token ();
2585 : : break;
2586 : 0 : default:
2587 : 0 : add_error (
2588 : 0 : Error (crate_name_tok->get_locus (),
2589 : : "expecting crate name (identifier or %<self%>), found %qs",
2590 : : crate_name_tok->get_token_description ()));
2591 : :
2592 : 0 : skip_after_semicolon ();
2593 : 0 : return nullptr;
2594 : : }
2595 : :
2596 : : // don't parse as clause if it doesn't exist
2597 : 54 : if (lexer.peek_token ()->get_id () == SEMICOLON)
2598 : : {
2599 : 27 : lexer.skip_token ();
2600 : :
2601 : : return std::unique_ptr<AST::ExternCrate> (
2602 : 27 : new AST::ExternCrate (std::move (crate_name), std::move (vis),
2603 : 27 : std::move (outer_attrs), locus));
2604 : : }
2605 : :
2606 : : /* parse as clause - this also has its own syntactical rule in reference and
2607 : : * also seems to not be used elsewhere, so including here again. */
2608 : 0 : if (!skip_token (AS))
2609 : : {
2610 : 0 : skip_after_semicolon ();
2611 : 0 : return nullptr;
2612 : : }
2613 : :
2614 : 0 : const_TokenPtr as_name_tok = lexer.peek_token ();
2615 : 0 : std::string as_name;
2616 : :
2617 : 0 : switch (as_name_tok->get_id ())
2618 : : {
2619 : 0 : case IDENTIFIER:
2620 : 0 : as_name = as_name_tok->get_str ();
2621 : 0 : lexer.skip_token ();
2622 : : break;
2623 : 0 : case UNDERSCORE:
2624 : 0 : as_name = Values::Keywords::UNDERSCORE;
2625 : 0 : lexer.skip_token ();
2626 : : break;
2627 : 0 : default:
2628 : 0 : add_error (
2629 : 0 : Error (as_name_tok->get_locus (),
2630 : : "expecting as clause name (identifier or %<_%>), found %qs",
2631 : : as_name_tok->get_token_description ()));
2632 : :
2633 : 0 : skip_after_semicolon ();
2634 : 0 : return nullptr;
2635 : : }
2636 : :
2637 : 0 : if (!skip_token (SEMICOLON))
2638 : : {
2639 : 0 : skip_after_semicolon ();
2640 : 0 : return nullptr;
2641 : : }
2642 : :
2643 : : return std::unique_ptr<AST::ExternCrate> (
2644 : 0 : new AST::ExternCrate (std::move (crate_name), std::move (vis),
2645 : 0 : std::move (outer_attrs), locus, std::move (as_name)));
2646 : 54 : }
2647 : :
2648 : : // Parses a use declaration.
2649 : : template <typename ManagedTokenSource>
2650 : : std::unique_ptr<AST::UseDeclaration>
2651 : 1495 : Parser<ManagedTokenSource>::parse_use_decl (AST::Visibility vis,
2652 : : AST::AttrVec outer_attrs)
2653 : : {
2654 : 1495 : location_t locus = lexer.peek_token ()->get_locus ();
2655 : 1495 : if (!skip_token (USE))
2656 : : {
2657 : 0 : skip_after_semicolon ();
2658 : 0 : return nullptr;
2659 : : }
2660 : :
2661 : : // parse use tree, which is required
2662 : 1495 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2663 : 1495 : if (use_tree == nullptr)
2664 : : {
2665 : 1 : Error error (lexer.peek_token ()->get_locus (),
2666 : : "could not parse use tree in use declaration");
2667 : 1 : add_error (std::move (error));
2668 : :
2669 : 1 : skip_after_semicolon ();
2670 : 1 : return nullptr;
2671 : 1 : }
2672 : :
2673 : 1494 : if (!skip_token (SEMICOLON))
2674 : : {
2675 : 0 : skip_after_semicolon ();
2676 : 0 : return nullptr;
2677 : : }
2678 : :
2679 : : return std::unique_ptr<AST::UseDeclaration> (
2680 : 1494 : new AST::UseDeclaration (std::move (use_tree), std::move (vis),
2681 : 1494 : std::move (outer_attrs), locus));
2682 : 1495 : }
2683 : :
2684 : : // Parses a use tree (which can be recursive and is actually a base class).
2685 : : template <typename ManagedTokenSource>
2686 : : std::unique_ptr<AST::UseTree>
2687 : 2836 : Parser<ManagedTokenSource>::parse_use_tree ()
2688 : : {
2689 : : /* potential syntax definitions in attempt to get algorithm:
2690 : : * Glob:
2691 : : * <- SimplePath :: *
2692 : : * <- :: *
2693 : : * <- *
2694 : : * Nested tree thing:
2695 : : * <- SimplePath :: { COMPLICATED_INNER_TREE_THING }
2696 : : * <- :: COMPLICATED_INNER_TREE_THING }
2697 : : * <- { COMPLICATED_INNER_TREE_THING }
2698 : : * Rebind thing:
2699 : : * <- SimplePath as IDENTIFIER
2700 : : * <- SimplePath as _
2701 : : * <- SimplePath
2702 : : */
2703 : :
2704 : : /* current plan of attack: try to parse SimplePath first - if fails, one of
2705 : : * top two then try parse :: - if fails, one of top two. Next is deciding
2706 : : * character for top two. */
2707 : :
2708 : : /* Thus, parsing smaller parts of use tree may require feeding into function
2709 : : * via parameters (or could handle all in this single function because other
2710 : : * use tree types aren't recognised as separate in the spec) */
2711 : :
2712 : : // TODO: I think this function is too complex, probably should split it
2713 : :
2714 : 2836 : location_t locus = lexer.peek_token ()->get_locus ();
2715 : :
2716 : : // bool has_path = false;
2717 : 2836 : AST::SimplePath path = parse_simple_path ();
2718 : :
2719 : 2836 : if (path.is_empty ())
2720 : : {
2721 : : // has no path, so must be glob or nested tree UseTree type
2722 : :
2723 : 2 : bool is_global = false;
2724 : :
2725 : : // check for global scope resolution operator
2726 : 4 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
2727 : : {
2728 : 0 : lexer.skip_token ();
2729 : 0 : is_global = true;
2730 : : }
2731 : :
2732 : 2 : const_TokenPtr t = lexer.peek_token ();
2733 : 2 : switch (t->get_id ())
2734 : : {
2735 : 0 : case ASTERISK:
2736 : : // glob UseTree type
2737 : 0 : lexer.skip_token ();
2738 : :
2739 : 0 : if (is_global)
2740 : 0 : return std::unique_ptr<AST::UseTreeGlob> (
2741 : 0 : new AST::UseTreeGlob (AST::UseTreeGlob::GLOBAL,
2742 : 0 : AST::SimplePath::create_empty (), locus));
2743 : : else
2744 : 0 : return std::unique_ptr<AST::UseTreeGlob> (
2745 : 0 : new AST::UseTreeGlob (AST::UseTreeGlob::NO_PATH,
2746 : 0 : AST::SimplePath::create_empty (), locus));
2747 : 2 : case LEFT_CURLY:
2748 : : {
2749 : : // nested tree UseTree type
2750 : 2 : lexer.skip_token ();
2751 : :
2752 : 2 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2753 : :
2754 : 2 : const_TokenPtr t = lexer.peek_token ();
2755 : 5 : while (t->get_id () != RIGHT_CURLY)
2756 : : {
2757 : 3 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2758 : 3 : if (use_tree == nullptr)
2759 : : {
2760 : : break;
2761 : : }
2762 : :
2763 : 3 : use_trees.push_back (std::move (use_tree));
2764 : :
2765 : 6 : if (lexer.peek_token ()->get_id () != COMMA)
2766 : : break;
2767 : :
2768 : 1 : lexer.skip_token ();
2769 : 1 : t = lexer.peek_token ();
2770 : : }
2771 : :
2772 : : // skip end curly delimiter
2773 : 2 : if (!skip_token (RIGHT_CURLY))
2774 : : {
2775 : : // skip after somewhere?
2776 : 0 : return nullptr;
2777 : : }
2778 : :
2779 : 2 : if (is_global)
2780 : 0 : return std::unique_ptr<AST::UseTreeList> (
2781 : 0 : new AST::UseTreeList (AST::UseTreeList::GLOBAL,
2782 : 0 : AST::SimplePath::create_empty (),
2783 : 0 : std::move (use_trees), locus));
2784 : : else
2785 : 2 : return std::unique_ptr<AST::UseTreeList> (
2786 : 4 : new AST::UseTreeList (AST::UseTreeList::NO_PATH,
2787 : 4 : AST::SimplePath::create_empty (),
2788 : 2 : std::move (use_trees), locus));
2789 : 2 : }
2790 : 0 : case AS:
2791 : : // this is not allowed
2792 : 0 : add_error (Error (
2793 : : t->get_locus (),
2794 : : "use declaration with rebind %<as%> requires a valid simple path - "
2795 : : "none found"));
2796 : :
2797 : 0 : skip_after_semicolon ();
2798 : 0 : return nullptr;
2799 : 0 : default:
2800 : 0 : add_error (Error (t->get_locus (),
2801 : : "unexpected token %qs in use tree with "
2802 : : "no valid simple path (i.e. list"
2803 : : " or glob use tree)",
2804 : : t->get_token_description ()));
2805 : :
2806 : 0 : skip_after_semicolon ();
2807 : 0 : return nullptr;
2808 : : }
2809 : 2 : }
2810 : : else
2811 : : {
2812 : 2834 : const_TokenPtr t = lexer.peek_token ();
2813 : :
2814 : 2834 : switch (t->get_id ())
2815 : : {
2816 : 22 : case AS:
2817 : : {
2818 : : // rebind UseTree type
2819 : 22 : lexer.skip_token ();
2820 : :
2821 : 22 : const_TokenPtr t = lexer.peek_token ();
2822 : 22 : switch (t->get_id ())
2823 : : {
2824 : 20 : case IDENTIFIER:
2825 : : // skip lexer token
2826 : 20 : lexer.skip_token ();
2827 : :
2828 : 20 : return std::unique_ptr<AST::UseTreeRebind> (
2829 : 60 : new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER,
2830 : 60 : std::move (path), locus, t));
2831 : 2 : case UNDERSCORE:
2832 : : // skip lexer token
2833 : 2 : lexer.skip_token ();
2834 : :
2835 : 2 : return std::unique_ptr<AST::UseTreeRebind> (
2836 : 6 : new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
2837 : : std::move (path), locus,
2838 : : {Values::Keywords::UNDERSCORE,
2839 : 2 : t->get_locus ()}));
2840 : 0 : default:
2841 : 0 : add_error (Error (
2842 : : t->get_locus (),
2843 : : "unexpected token %qs in use tree with as clause - expected "
2844 : : "identifier or %<_%>",
2845 : : t->get_token_description ()));
2846 : :
2847 : 0 : skip_after_semicolon ();
2848 : 0 : return nullptr;
2849 : : }
2850 : 22 : }
2851 : 2232 : case SEMICOLON:
2852 : : // rebind UseTree type without rebinding - path only
2853 : :
2854 : : // don't skip semicolon - handled in parse_use_tree
2855 : : // lexer.skip_token();
2856 : : case COMMA:
2857 : : case RIGHT_CURLY:
2858 : : // this may occur in recursive calls - assume it is ok and ignore it
2859 : 2232 : return std::unique_ptr<AST::UseTreeRebind> (
2860 : 4464 : new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
2861 : 2232 : locus));
2862 : : case SCOPE_RESOLUTION:
2863 : : // keep going
2864 : : break;
2865 : 1 : default:
2866 : 1 : add_error (Error (t->get_locus (),
2867 : : "unexpected token %qs in use tree with valid path",
2868 : : t->get_token_description ()));
2869 : 1 : return nullptr;
2870 : : }
2871 : :
2872 : 579 : skip_token ();
2873 : 579 : t = lexer.peek_token ();
2874 : :
2875 : 579 : switch (t->get_id ())
2876 : : {
2877 : 179 : case ASTERISK:
2878 : : // glob UseTree type
2879 : 179 : lexer.skip_token ();
2880 : :
2881 : 179 : return std::unique_ptr<AST::UseTreeGlob> (
2882 : 358 : new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
2883 : 179 : std::move (path), locus));
2884 : 400 : case LEFT_CURLY:
2885 : : {
2886 : : // nested tree UseTree type
2887 : 400 : lexer.skip_token ();
2888 : :
2889 : 400 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2890 : :
2891 : : // TODO: think of better control structure
2892 : 400 : const_TokenPtr t = lexer.peek_token ();
2893 : 1738 : while (t->get_id () != RIGHT_CURLY)
2894 : : {
2895 : 1338 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2896 : 1338 : if (use_tree == nullptr)
2897 : : {
2898 : : break;
2899 : : }
2900 : :
2901 : 1338 : use_trees.push_back (std::move (use_tree));
2902 : :
2903 : 2676 : if (lexer.peek_token ()->get_id () != COMMA)
2904 : : break;
2905 : :
2906 : 969 : lexer.skip_token ();
2907 : 969 : t = lexer.peek_token ();
2908 : : }
2909 : :
2910 : : // skip end curly delimiter
2911 : 400 : if (!skip_token (RIGHT_CURLY))
2912 : : {
2913 : : // skip after somewhere?
2914 : 0 : return nullptr;
2915 : : }
2916 : :
2917 : 400 : return std::unique_ptr<AST::UseTreeList> (
2918 : 800 : new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
2919 : : std::move (path), std::move (use_trees),
2920 : 400 : locus));
2921 : 400 : }
2922 : 0 : default:
2923 : 0 : add_error (Error (t->get_locus (),
2924 : : "unexpected token %qs in use tree with valid path",
2925 : : t->get_token_description ()));
2926 : :
2927 : : // skip_after_semicolon();
2928 : 0 : return nullptr;
2929 : : }
2930 : 2834 : }
2931 : 2836 : }
2932 : :
2933 : : // Parses a function (not a method).
2934 : : template <typename ManagedTokenSource>
2935 : : std::unique_ptr<AST::Function>
2936 : 16253 : Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
2937 : : AST::AttrVec outer_attrs,
2938 : : bool is_external)
2939 : : {
2940 : 16253 : location_t locus = lexer.peek_token ()->get_locus ();
2941 : : // Get qualifiers for function if they exist
2942 : 16253 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
2943 : :
2944 : 16253 : skip_token (FN_KW);
2945 : :
2946 : : // Save function name token
2947 : 16253 : const_TokenPtr function_name_tok = expect_token (IDENTIFIER);
2948 : 16253 : if (function_name_tok == nullptr)
2949 : : {
2950 : 0 : skip_after_next_block ();
2951 : 0 : return nullptr;
2952 : : }
2953 : 16253 : Identifier function_name{function_name_tok};
2954 : :
2955 : : // parse generic params - if exist
2956 : 16253 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
2957 : : = parse_generic_params_in_angles ();
2958 : :
2959 : 16253 : if (!skip_token (LEFT_PAREN))
2960 : : {
2961 : 0 : Error error (lexer.peek_token ()->get_locus (),
2962 : : "function declaration missing opening parentheses before "
2963 : : "parameter list");
2964 : 0 : add_error (std::move (error));
2965 : :
2966 : 0 : skip_after_next_block ();
2967 : 0 : return nullptr;
2968 : 0 : }
2969 : :
2970 : 16253 : auto initial_param = parse_self_param ();
2971 : :
2972 : 16253 : if (!initial_param.has_value ()
2973 : 16253 : && initial_param.error () != ParseSelfError::NOT_SELF)
2974 : 0 : return nullptr;
2975 : :
2976 : 18781 : if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
2977 : 1538 : skip_token ();
2978 : :
2979 : : // parse function parameters (only if next token isn't right paren)
2980 : 16253 : std::vector<std::unique_ptr<AST::Param>> function_params;
2981 : :
2982 : 32506 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
2983 : : function_params
2984 : 8109 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
2985 : :
2986 : 16253 : if (initial_param.has_value ())
2987 : 2528 : function_params.insert (function_params.begin (),
2988 : 2528 : std::move (*initial_param));
2989 : :
2990 : 16253 : if (!skip_token (RIGHT_PAREN))
2991 : : {
2992 : 0 : Error error (lexer.peek_token ()->get_locus (),
2993 : : "function declaration missing closing parentheses after "
2994 : : "parameter list");
2995 : 0 : add_error (std::move (error));
2996 : :
2997 : 0 : skip_after_next_block ();
2998 : 0 : return nullptr;
2999 : 0 : }
3000 : :
3001 : : // parse function return type - if exists
3002 : 16253 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
3003 : :
3004 : : // parse where clause - if exists
3005 : 16253 : AST::WhereClause where_clause = parse_where_clause ();
3006 : :
3007 : 16253 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
3008 : 32506 : if (lexer.peek_token ()->get_id () == SEMICOLON)
3009 : 4904 : lexer.skip_token ();
3010 : : else
3011 : : {
3012 : 11349 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
3013 : 11349 : if (block_expr != nullptr)
3014 : 11321 : body = std::move (block_expr);
3015 : 11349 : }
3016 : :
3017 : : return std::unique_ptr<AST::Function> (
3018 : 43827 : new AST::Function (std::move (function_name), std::move (qualifiers),
3019 : : std::move (generic_params), std::move (function_params),
3020 : : std::move (return_type), std::move (where_clause),
3021 : : std::move (body), std::move (vis),
3022 : 16253 : std::move (outer_attrs), locus, false, is_external));
3023 : 48759 : }
3024 : :
3025 : : // Parses function or method qualifiers (i.e. const, unsafe, and extern).
3026 : : template <typename ManagedTokenSource>
3027 : : AST::FunctionQualifiers
3028 : 32115 : Parser<ManagedTokenSource>::parse_function_qualifiers ()
3029 : : {
3030 : 32115 : Async async_status = Async::No;
3031 : 32115 : Const const_status = Const::No;
3032 : 32115 : Unsafety unsafe_status = Unsafety::Normal;
3033 : 32115 : bool has_extern = false;
3034 : 32115 : std::string abi;
3035 : :
3036 : 32115 : const_TokenPtr t;
3037 : : location_t locus;
3038 : : // Check in order of const, unsafe, then extern
3039 : 96345 : for (int i = 0; i < 2; i++)
3040 : : {
3041 : 64230 : t = lexer.peek_token ();
3042 : 64230 : locus = t->get_locus ();
3043 : :
3044 : 64230 : switch (t->get_id ())
3045 : : {
3046 : 2192 : case CONST:
3047 : 2192 : lexer.skip_token ();
3048 : 2192 : const_status = Const::Yes;
3049 : 2192 : break;
3050 : 8 : case ASYNC:
3051 : 8 : lexer.skip_token ();
3052 : 8 : async_status = Async::Yes;
3053 : 8 : break;
3054 : : default:
3055 : : // const status is still none
3056 : : break;
3057 : : }
3058 : : }
3059 : :
3060 : 64230 : if (lexer.peek_token ()->get_id () == UNSAFE)
3061 : : {
3062 : 4522 : lexer.skip_token ();
3063 : 4522 : unsafe_status = Unsafety::Unsafe;
3064 : : }
3065 : :
3066 : 64230 : if (lexer.peek_token ()->get_id () == EXTERN_KW)
3067 : : {
3068 : 740 : lexer.skip_token ();
3069 : 740 : has_extern = true;
3070 : :
3071 : : // detect optional abi name
3072 : 740 : const_TokenPtr next_tok = lexer.peek_token ();
3073 : 740 : if (next_tok->get_id () == STRING_LITERAL)
3074 : : {
3075 : 740 : lexer.skip_token ();
3076 : 740 : abi = next_tok->get_str ();
3077 : : }
3078 : 740 : }
3079 : :
3080 : 64230 : return AST::FunctionQualifiers (locus, async_status, const_status,
3081 : 32115 : unsafe_status, has_extern, std::move (abi));
3082 : 32115 : }
3083 : :
3084 : : // Parses generic (lifetime or type) params inside angle brackets (optional).
3085 : : template <typename ManagedTokenSource>
3086 : : std::vector<std::unique_ptr<AST::GenericParam>>
3087 : 54646 : Parser<ManagedTokenSource>::parse_generic_params_in_angles ()
3088 : : {
3089 : 109292 : if (lexer.peek_token ()->get_id () != LEFT_ANGLE)
3090 : : {
3091 : : // seems to be no generic params, so exit with empty vector
3092 : 46918 : return std::vector<std::unique_ptr<AST::GenericParam>> ();
3093 : : }
3094 : 7728 : lexer.skip_token ();
3095 : :
3096 : : // DEBUG:
3097 : 7728 : rust_debug ("skipped left angle in generic param");
3098 : :
3099 : 7728 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3100 : : = parse_generic_params (is_right_angle_tok);
3101 : :
3102 : : // DEBUG:
3103 : 7728 : rust_debug ("finished parsing actual generic params (i.e. inside angles)");
3104 : :
3105 : 7728 : if (!skip_generics_right_angle ())
3106 : : {
3107 : : // DEBUG
3108 : 1 : rust_debug ("failed to skip generics right angle - returning empty "
3109 : : "generic params");
3110 : :
3111 : 1 : return std::vector<std::unique_ptr<AST::GenericParam>> ();
3112 : : }
3113 : :
3114 : 7727 : return generic_params;
3115 : 7728 : }
3116 : :
3117 : : template <typename ManagedTokenSource>
3118 : : template <typename EndTokenPred>
3119 : : std::unique_ptr<AST::GenericParam>
3120 : 12961 : Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
3121 : : {
3122 : 12961 : auto outer_attrs = parse_outer_attributes ();
3123 : 12961 : std::unique_ptr<AST::GenericParam> param;
3124 : 12961 : auto token = lexer.peek_token ();
3125 : :
3126 : 12961 : switch (token->get_id ())
3127 : : {
3128 : 1358 : case LIFETIME:
3129 : : {
3130 : 1358 : auto lifetime = parse_lifetime (false);
3131 : 1358 : if (!lifetime)
3132 : : {
3133 : 0 : rust_error_at (
3134 : : token->get_locus (),
3135 : : "failed to parse lifetime in generic parameter list");
3136 : 0 : return nullptr;
3137 : : }
3138 : :
3139 : 1358 : std::vector<AST::Lifetime> lifetime_bounds;
3140 : 2716 : if (lexer.peek_token ()->get_id () == COLON)
3141 : : {
3142 : 19 : lexer.skip_token ();
3143 : : // parse required bounds
3144 : : lifetime_bounds
3145 : 19 : = parse_lifetime_bounds ([is_end_token] (TokenId id) {
3146 : 20 : return is_end_token (id) || id == COMMA;
3147 : : });
3148 : : }
3149 : :
3150 : 2716 : param = std::unique_ptr<AST::LifetimeParam> (new AST::LifetimeParam (
3151 : 1358 : std::move (lifetime.value ()), std::move (lifetime_bounds),
3152 : 1358 : std::move (outer_attrs), token->get_locus ()));
3153 : : break;
3154 : 2716 : }
3155 : 11428 : case IDENTIFIER:
3156 : : {
3157 : 11428 : auto type_ident = token->get_str ();
3158 : 11428 : lexer.skip_token ();
3159 : :
3160 : 11428 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3161 : 22856 : if (lexer.peek_token ()->get_id () == COLON)
3162 : : {
3163 : 2309 : lexer.skip_token ();
3164 : :
3165 : : // parse optional type param bounds
3166 : 2309 : type_param_bounds = parse_type_param_bounds ();
3167 : : }
3168 : :
3169 : 11428 : std::unique_ptr<AST::Type> type = nullptr;
3170 : 22856 : if (lexer.peek_token ()->get_id () == EQUAL)
3171 : : {
3172 : 389 : lexer.skip_token ();
3173 : :
3174 : : // parse required type
3175 : 389 : type = parse_type ();
3176 : 389 : if (!type)
3177 : : {
3178 : 0 : rust_error_at (
3179 : 0 : lexer.peek_token ()->get_locus (),
3180 : : "failed to parse type in type param in generic params");
3181 : 0 : return nullptr;
3182 : : }
3183 : : }
3184 : :
3185 : 11428 : param = std::unique_ptr<AST::TypeParam> (
3186 : 34284 : new AST::TypeParam (std::move (type_ident), token->get_locus (),
3187 : : std::move (type_param_bounds), std::move (type),
3188 : 11428 : std::move (outer_attrs)));
3189 : : break;
3190 : 11428 : }
3191 : 174 : case CONST:
3192 : : {
3193 : 174 : lexer.skip_token ();
3194 : :
3195 : 174 : auto name_token = expect_token (IDENTIFIER);
3196 : :
3197 : 348 : if (!name_token || !expect_token (COLON))
3198 : 1 : return nullptr;
3199 : :
3200 : 173 : auto type = parse_type ();
3201 : 173 : if (!type)
3202 : 1 : return nullptr;
3203 : :
3204 : : // optional default value
3205 : 172 : tl::optional<AST::GenericArg> default_expr = tl::nullopt;
3206 : 344 : if (lexer.peek_token ()->get_id () == EQUAL)
3207 : : {
3208 : 20 : lexer.skip_token ();
3209 : 20 : auto tok = lexer.peek_token ();
3210 : 39 : default_expr = parse_generic_arg ();
3211 : :
3212 : 20 : if (!default_expr)
3213 : : {
3214 : 1 : rust_error_at (tok->get_locus (),
3215 : : "invalid token for start of default value for "
3216 : : "const generic parameter: expected %<block%>, "
3217 : : "%<identifier%> or %<literal%>, got %qs",
3218 : : token_id_to_str (tok->get_id ()));
3219 : 1 : return nullptr;
3220 : : }
3221 : :
3222 : : // At this point, we *know* that we are parsing a const
3223 : : // expression
3224 : 19 : if (default_expr.value ().get_kind ()
3225 : : == AST::GenericArg::Kind::Either)
3226 : 1 : default_expr = default_expr.value ().disambiguate_to_const ();
3227 : 20 : }
3228 : :
3229 : 171 : param = std::unique_ptr<AST::ConstGenericParam> (
3230 : 703 : new AST::ConstGenericParam (name_token->get_str (), std::move (type),
3231 : : default_expr, std::move (outer_attrs),
3232 : 171 : token->get_locus ()));
3233 : :
3234 : : break;
3235 : 347 : }
3236 : 1 : default:
3237 : : // FIXME: Can we clean this last call with a method call?
3238 : 1 : rust_error_at (token->get_locus (),
3239 : : "unexpected token when parsing generic parameters: %qs",
3240 : 1 : token->as_string ().c_str ());
3241 : 1 : return nullptr;
3242 : : }
3243 : :
3244 : 12957 : return param;
3245 : 12961 : }
3246 : :
3247 : : /* Parse generic (lifetime or type) params NOT INSIDE ANGLE BRACKETS!!! Almost
3248 : : * always parse_generic_params_in_angles is what is wanted. */
3249 : : template <typename ManagedTokenSource>
3250 : : template <typename EndTokenPred>
3251 : : std::vector<std::unique_ptr<AST::GenericParam>>
3252 : 7728 : Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
3253 : : {
3254 : 7728 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
3255 : :
3256 : : /* can't parse lifetime and type params separately due to lookahead issues
3257 : : * thus, parse them all here */
3258 : :
3259 : : /* HACK: used to retain attribute data if a lifetime param is tentatively
3260 : : * parsed but it turns out to be type param */
3261 : 7728 : AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
3262 : :
3263 : : // Did we parse a generic type param yet
3264 : 7728 : auto type_seen = false;
3265 : : // Did we parse a const param with a default value yet
3266 : 7728 : auto const_with_default_seen = false;
3267 : : // Did the user write a lifetime parameter after a type one
3268 : 7728 : auto order_error = false;
3269 : : // Did the user write a const param with a default value after a type one
3270 : 7728 : auto const_with_default_order_error = false;
3271 : :
3272 : : // parse lifetime params
3273 : 54331 : while (!is_end_token (lexer.peek_token ()->get_id ()))
3274 : : {
3275 : 12961 : auto param = parse_generic_param (is_end_token);
3276 : 12961 : if (param)
3277 : : {
3278 : 12957 : if (param->get_kind () == AST::GenericParam::Kind::Type)
3279 : : {
3280 : 11428 : type_seen = true;
3281 : 11428 : if (const_with_default_seen)
3282 : 12957 : const_with_default_order_error = true;
3283 : : }
3284 : 1529 : else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
3285 : 1529 : && type_seen)
3286 : : {
3287 : 2 : order_error = true;
3288 : 2 : if (const_with_default_seen)
3289 : 0 : const_with_default_order_error = true;
3290 : : }
3291 : 1527 : else if (param->get_kind () == AST::GenericParam::Kind::Const)
3292 : : {
3293 : 171 : type_seen = true;
3294 : : AST::ConstGenericParam *const_param
3295 : 171 : = static_cast<AST::ConstGenericParam *> (param.get ());
3296 : 171 : if (const_param->has_default_value ())
3297 : : const_with_default_seen = true;
3298 : 152 : else if (const_with_default_seen)
3299 : 12957 : const_with_default_order_error = true;
3300 : : }
3301 : :
3302 : 12957 : generic_params.emplace_back (std::move (param));
3303 : 12957 : maybe_skip_token (COMMA);
3304 : : }
3305 : : else
3306 : : break;
3307 : : }
3308 : :
3309 : : // FIXME: Add reordering hint
3310 : 7728 : if (order_error)
3311 : : {
3312 : 2 : Error error (generic_params.front ()->get_locus (),
3313 : : "invalid order for generic parameters: lifetime parameters "
3314 : : "must be declared prior to type and const parameters");
3315 : 2 : add_error (std::move (error));
3316 : 2 : }
3317 : 7728 : if (const_with_default_order_error)
3318 : : {
3319 : 1 : Error error (generic_params.front ()->get_locus (),
3320 : : "invalid order for generic parameters: generic parameters "
3321 : : "with a default must be trailing");
3322 : 1 : add_error (std::move (error));
3323 : 1 : }
3324 : :
3325 : 7728 : generic_params.shrink_to_fit ();
3326 : 7728 : return generic_params;
3327 : 7728 : }
3328 : :
3329 : : /* Parses lifetime generic parameters (pointers). Will also consume any
3330 : : * trailing comma. No extra checks for end token. */
3331 : : template <typename ManagedTokenSource>
3332 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
3333 : 17 : Parser<ManagedTokenSource>::parse_lifetime_params ()
3334 : : {
3335 : 17 : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
3336 : :
3337 : 55 : while (lexer.peek_token ()->get_id () != END_OF_FILE)
3338 : : {
3339 : 19 : auto lifetime_param = parse_lifetime_param ();
3340 : :
3341 : 19 : if (!lifetime_param)
3342 : : {
3343 : : // can't treat as error as only way to get out with trailing comma
3344 : : break;
3345 : : }
3346 : :
3347 : 8 : lifetime_params.emplace_back (
3348 : 8 : new AST::LifetimeParam (std::move (lifetime_param.value ())));
3349 : :
3350 : 16 : if (lexer.peek_token ()->get_id () != COMMA)
3351 : : break;
3352 : :
3353 : : // skip commas, including trailing commas
3354 : 2 : lexer.skip_token ();
3355 : : }
3356 : :
3357 : 17 : lifetime_params.shrink_to_fit ();
3358 : :
3359 : 17 : return lifetime_params;
3360 : : }
3361 : :
3362 : : /* Parses lifetime generic parameters (pointers). Will also consume any
3363 : : * trailing comma. Has extra is_end_token predicate checking. */
3364 : : template <typename ManagedTokenSource>
3365 : : template <typename EndTokenPred>
3366 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
3367 : : Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token)
3368 : : {
3369 : : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
3370 : :
3371 : : // if end_token is not specified, it defaults to EOF, so should work fine
3372 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3373 : : {
3374 : : auto lifetime_param = parse_lifetime_param ();
3375 : :
3376 : : if (!lifetime_param)
3377 : : {
3378 : : /* TODO: is it worth throwing away all lifetime params just because
3379 : : * one failed? */
3380 : : Error error (lexer.peek_token ()->get_locus (),
3381 : : "failed to parse lifetime param in lifetime params");
3382 : : add_error (std::move (error));
3383 : :
3384 : : return {};
3385 : : }
3386 : :
3387 : : lifetime_params.emplace_back (
3388 : : new AST::LifetimeParam (std::move (lifetime_param)));
3389 : :
3390 : : if (lexer.peek_token ()->get_id () != COMMA)
3391 : : break;
3392 : :
3393 : : // skip commas, including trailing commas
3394 : : lexer.skip_token ();
3395 : : }
3396 : :
3397 : : lifetime_params.shrink_to_fit ();
3398 : :
3399 : : return lifetime_params;
3400 : : }
3401 : :
3402 : : /* Parses lifetime generic parameters (objects). Will also consume any
3403 : : * trailing comma. No extra checks for end token.
3404 : : * TODO: is this best solution? implements most of the same algorithm.
3405 : : * TODO: seems to be unused, remove? */
3406 : : template <typename ManagedTokenSource>
3407 : : std::vector<AST::LifetimeParam>
3408 : 0 : Parser<ManagedTokenSource>::parse_lifetime_params_objs ()
3409 : : {
3410 : 0 : std::vector<AST::LifetimeParam> lifetime_params;
3411 : :
3412 : : // bad control structure as end token cannot be guaranteed
3413 : 0 : while (true)
3414 : : {
3415 : 0 : auto lifetime_param = parse_lifetime_param ();
3416 : :
3417 : 0 : if (!lifetime_param)
3418 : : {
3419 : : // not an error as only way to exit if trailing comma
3420 : : break;
3421 : : }
3422 : :
3423 : 0 : lifetime_params.push_back (std::move (lifetime_param.value ()));
3424 : :
3425 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
3426 : : break;
3427 : :
3428 : : // skip commas, including trailing commas
3429 : 0 : lexer.skip_token ();
3430 : : }
3431 : :
3432 : 0 : lifetime_params.shrink_to_fit ();
3433 : :
3434 : 0 : return lifetime_params;
3435 : : }
3436 : :
3437 : : /* Parses lifetime generic parameters (objects). Will also consume any
3438 : : * trailing comma. Has extra is_end_token predicate checking.
3439 : : * TODO: is this best solution? implements most of the same algorithm. */
3440 : : template <typename ManagedTokenSource>
3441 : : template <typename EndTokenPred>
3442 : : std::vector<AST::LifetimeParam>
3443 : 28 : Parser<ManagedTokenSource>::parse_lifetime_params_objs (
3444 : : EndTokenPred is_end_token)
3445 : : {
3446 : 28 : std::vector<AST::LifetimeParam> lifetime_params;
3447 : :
3448 : 84 : while (!is_end_token (lexer.peek_token ()->get_id ()))
3449 : : {
3450 : 28 : auto lifetime_param = parse_lifetime_param ();
3451 : :
3452 : 28 : if (!lifetime_param)
3453 : : {
3454 : : /* TODO: is it worth throwing away all lifetime params just because
3455 : : * one failed? */
3456 : 0 : Error error (lexer.peek_token ()->get_locus (),
3457 : : "failed to parse lifetime param in lifetime params");
3458 : 0 : add_error (std::move (error));
3459 : :
3460 : 0 : return {};
3461 : 0 : }
3462 : :
3463 : 28 : lifetime_params.push_back (std::move (lifetime_param.value ()));
3464 : :
3465 : 56 : if (lexer.peek_token ()->get_id () != COMMA)
3466 : : break;
3467 : :
3468 : : // skip commas, including trailing commas
3469 : 0 : lexer.skip_token ();
3470 : : }
3471 : :
3472 : 28 : lifetime_params.shrink_to_fit ();
3473 : :
3474 : 28 : return lifetime_params;
3475 : 28 : }
3476 : :
3477 : : /* Parses a sequence of a certain grammar rule in object form (not pointer or
3478 : : * smart pointer), delimited by commas and ending when 'is_end_token' is
3479 : : * satisfied (templated). Will also consume any trailing comma.
3480 : : * FIXME: this cannot be used due to member function pointer problems (i.e.
3481 : : * parsing_function cannot be specified properly) */
3482 : : template <typename ManagedTokenSource>
3483 : : template <typename ParseFunction, typename EndTokenPred>
3484 : : auto
3485 : : Parser<ManagedTokenSource>::parse_non_ptr_sequence (
3486 : : ParseFunction parsing_function, EndTokenPred is_end_token,
3487 : : std::string error_msg) -> std::vector<decltype (parsing_function ())>
3488 : : {
3489 : : std::vector<decltype (parsing_function ())> params;
3490 : :
3491 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3492 : : {
3493 : : auto param = parsing_function ();
3494 : :
3495 : : if (param.is_error ())
3496 : : {
3497 : : // TODO: is it worth throwing away all params just because one
3498 : : // failed?
3499 : : Error error (lexer.peek_token ()->get_locus (),
3500 : : std::move (error_msg));
3501 : : add_error (std::move (error));
3502 : :
3503 : : return {};
3504 : : }
3505 : :
3506 : : params.push_back (std::move (param));
3507 : :
3508 : : if (lexer.peek_token ()->get_id () != COMMA)
3509 : : break;
3510 : :
3511 : : // skip commas, including trailing commas
3512 : : lexer.skip_token ();
3513 : : }
3514 : :
3515 : : params.shrink_to_fit ();
3516 : :
3517 : : return params;
3518 : : }
3519 : :
3520 : : /* Parses a single lifetime generic parameter (not including comma). */
3521 : : template <typename ManagedTokenSource>
3522 : : tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
3523 : 47 : Parser<ManagedTokenSource>::parse_lifetime_param ()
3524 : : {
3525 : : // parse outer attributes, which are optional and may not exist
3526 : 47 : auto outer_attrs = parse_outer_attributes ();
3527 : :
3528 : : // save lifetime token - required
3529 : 47 : const_TokenPtr lifetime_tok = lexer.peek_token ();
3530 : 47 : if (lifetime_tok->get_id () != LIFETIME)
3531 : : {
3532 : : // if lifetime is missing, must not be a lifetime param, so return error
3533 : 11 : return tl::make_unexpected<ParseLifetimeParamError> ({});
3534 : : }
3535 : 36 : lexer.skip_token ();
3536 : 72 : AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
3537 : : lifetime_tok->get_locus ());
3538 : :
3539 : : // parse lifetime bounds, if it exists
3540 : 36 : std::vector<AST::Lifetime> lifetime_bounds;
3541 : 72 : if (lexer.peek_token ()->get_id () == COLON)
3542 : : {
3543 : : // parse lifetime bounds
3544 : 0 : lifetime_bounds = parse_lifetime_bounds ();
3545 : : // TODO: have end token passed in?
3546 : : }
3547 : :
3548 : 72 : return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
3549 : : std::move (outer_attrs),
3550 : 36 : lifetime_tok->get_locus ());
3551 : 83 : }
3552 : :
3553 : : // Parses type generic parameters. Will also consume any trailing comma.
3554 : : template <typename ManagedTokenSource>
3555 : : std::vector<std::unique_ptr<AST::TypeParam>>
3556 : 0 : Parser<ManagedTokenSource>::parse_type_params ()
3557 : : {
3558 : 0 : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
3559 : :
3560 : : // infinite loop with break on failure as no info on ending token
3561 : 0 : while (true)
3562 : : {
3563 : 0 : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
3564 : :
3565 : 0 : if (type_param == nullptr)
3566 : : {
3567 : : // break if fails to parse
3568 : : break;
3569 : : }
3570 : :
3571 : 0 : type_params.push_back (std::move (type_param));
3572 : :
3573 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
3574 : : break;
3575 : :
3576 : : // skip commas, including trailing commas
3577 : 0 : lexer.skip_token ();
3578 : : }
3579 : :
3580 : 0 : type_params.shrink_to_fit ();
3581 : 0 : return type_params;
3582 : : }
3583 : :
3584 : : // Parses type generic parameters. Will also consume any trailing comma.
3585 : : template <typename ManagedTokenSource>
3586 : : template <typename EndTokenPred>
3587 : : std::vector<std::unique_ptr<AST::TypeParam>>
3588 : : Parser<ManagedTokenSource>::parse_type_params (EndTokenPred is_end_token)
3589 : : {
3590 : : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
3591 : :
3592 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3593 : : {
3594 : : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
3595 : :
3596 : : if (type_param == nullptr)
3597 : : {
3598 : : Error error (lexer.peek_token ()->get_locus (),
3599 : : "failed to parse type param in type params");
3600 : : add_error (std::move (error));
3601 : :
3602 : : return {};
3603 : : }
3604 : :
3605 : : type_params.push_back (std::move (type_param));
3606 : :
3607 : : if (lexer.peek_token ()->get_id () != COMMA)
3608 : : break;
3609 : :
3610 : : // skip commas, including trailing commas
3611 : : lexer.skip_token ();
3612 : : }
3613 : :
3614 : : type_params.shrink_to_fit ();
3615 : : return type_params;
3616 : : /* TODO: this shares most code with parse_lifetime_params - good place to
3617 : : * use template (i.e. parse_non_ptr_sequence if doable) */
3618 : : }
3619 : :
3620 : : /* Parses a single type (generic) parameter, not including commas. May change
3621 : : * to return value. */
3622 : : template <typename ManagedTokenSource>
3623 : : std::unique_ptr<AST::TypeParam>
3624 : 0 : Parser<ManagedTokenSource>::parse_type_param ()
3625 : : {
3626 : : // parse outer attributes, which are optional and may not exist
3627 : 0 : auto outer_attrs = parse_outer_attributes ();
3628 : :
3629 : 0 : const_TokenPtr identifier_tok = lexer.peek_token ();
3630 : 0 : if (identifier_tok->get_id () != IDENTIFIER)
3631 : : {
3632 : : // return null as type param can't exist without this required
3633 : : // identifier
3634 : 0 : return nullptr;
3635 : : }
3636 : 0 : Identifier ident{identifier_tok};
3637 : 0 : lexer.skip_token ();
3638 : :
3639 : : // parse type param bounds (if they exist)
3640 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3641 : 0 : if (lexer.peek_token ()->get_id () == COLON)
3642 : : {
3643 : 0 : lexer.skip_token ();
3644 : :
3645 : : // parse type param bounds, which may or may not exist
3646 : 0 : type_param_bounds = parse_type_param_bounds ();
3647 : : }
3648 : :
3649 : : // parse type (if it exists)
3650 : 0 : std::unique_ptr<AST::Type> type = nullptr;
3651 : 0 : if (lexer.peek_token ()->get_id () == EQUAL)
3652 : : {
3653 : 0 : lexer.skip_token ();
3654 : :
3655 : : // parse type (now required)
3656 : 0 : type = parse_type ();
3657 : 0 : if (type == nullptr)
3658 : : {
3659 : 0 : Error error (lexer.peek_token ()->get_locus (),
3660 : : "failed to parse type in type param");
3661 : 0 : add_error (std::move (error));
3662 : :
3663 : 0 : return nullptr;
3664 : 0 : }
3665 : : }
3666 : :
3667 : : return std::unique_ptr<AST::TypeParam> (
3668 : 0 : new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
3669 : : std::move (type_param_bounds), std::move (type),
3670 : 0 : std::move (outer_attrs)));
3671 : 0 : }
3672 : :
3673 : : /* Parses regular (i.e. non-generic) parameters in functions or methods. Also
3674 : : * has end token handling. */
3675 : : template <typename ManagedTokenSource>
3676 : : template <typename EndTokenPred>
3677 : : std::vector<std::unique_ptr<AST::Param>>
3678 : 20575 : Parser<ManagedTokenSource>::parse_function_params (EndTokenPred is_end_token)
3679 : : {
3680 : 20575 : std::vector<std::unique_ptr<AST::Param>> params;
3681 : :
3682 : 41150 : if (is_end_token (lexer.peek_token ()->get_id ()))
3683 : 1700 : return params;
3684 : :
3685 : 18875 : auto initial_param = parse_function_param ();
3686 : :
3687 : : // Return empty parameter list if no parameter there
3688 : 18875 : if (initial_param == nullptr)
3689 : : {
3690 : : // TODO: is this an error?
3691 : 0 : return params;
3692 : : }
3693 : :
3694 : 18875 : params.push_back (std::move (initial_param));
3695 : :
3696 : : // maybe think of a better control structure here - do-while with an initial
3697 : : // error state? basically, loop through parameter list until can't find any
3698 : : // more params
3699 : 18875 : const_TokenPtr t = lexer.peek_token ();
3700 : 25850 : while (t->get_id () == COMMA)
3701 : : {
3702 : : // skip comma if applies
3703 : 7262 : lexer.skip_token ();
3704 : :
3705 : : // TODO: strictly speaking, shouldn't there be no trailing comma?
3706 : 14524 : if (is_end_token (lexer.peek_token ()->get_id ()))
3707 : : break;
3708 : :
3709 : : // now, as right paren would break, function param is required
3710 : 6975 : auto param = parse_function_param ();
3711 : 6975 : if (param == nullptr)
3712 : : {
3713 : 0 : Error error (lexer.peek_token ()->get_locus (),
3714 : : "failed to parse function param (in function params)");
3715 : 0 : add_error (std::move (error));
3716 : :
3717 : : // skip somewhere?
3718 : 0 : return std::vector<std::unique_ptr<AST::Param>> ();
3719 : 0 : }
3720 : :
3721 : 6975 : params.push_back (std::move (param));
3722 : :
3723 : 6975 : t = lexer.peek_token ();
3724 : : }
3725 : :
3726 : 18875 : params.shrink_to_fit ();
3727 : 18875 : return params;
3728 : 20575 : }
3729 : :
3730 : : /* Parses a single regular (i.e. non-generic) parameter in a function or
3731 : : * method, i.e. the "name: type" bit. Also handles it not existing. */
3732 : : template <typename ManagedTokenSource>
3733 : : std::unique_ptr<AST::Param>
3734 : 25850 : Parser<ManagedTokenSource>::parse_function_param ()
3735 : : {
3736 : : // parse outer attributes if they exist
3737 : 25850 : AST::AttrVec outer_attrs = parse_outer_attributes ();
3738 : :
3739 : : // TODO: should saved location be at start of outer attributes or pattern?
3740 : 25850 : location_t locus = lexer.peek_token ()->get_locus ();
3741 : :
3742 : 51700 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic
3743 : : {
3744 : 826 : lexer.skip_token (); // Skip ellipsis
3745 : 826 : return std::make_unique<AST::VariadicParam> (
3746 : 1652 : AST::VariadicParam (std::move (outer_attrs), locus));
3747 : : }
3748 : :
3749 : 25024 : std::unique_ptr<AST::Pattern> param_pattern = parse_pattern ();
3750 : :
3751 : : // create error function param if it doesn't exist
3752 : 25024 : if (param_pattern == nullptr)
3753 : : {
3754 : : // skip after something
3755 : 0 : return nullptr;
3756 : : }
3757 : :
3758 : 25024 : if (!skip_token (COLON))
3759 : : {
3760 : : // skip after something
3761 : 0 : return nullptr;
3762 : : }
3763 : :
3764 : 50048 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic
3765 : : {
3766 : 11 : lexer.skip_token (); // Skip ellipsis
3767 : 11 : return std::make_unique<AST::VariadicParam> (
3768 : 22 : AST::VariadicParam (std::move (param_pattern), std::move (outer_attrs),
3769 : 11 : locus));
3770 : : }
3771 : : else
3772 : : {
3773 : 25013 : std::unique_ptr<AST::Type> param_type = parse_type ();
3774 : 25013 : if (param_type == nullptr)
3775 : : {
3776 : 0 : return nullptr;
3777 : : }
3778 : 25013 : return std::make_unique<AST::FunctionParam> (
3779 : 50026 : AST::FunctionParam (std::move (param_pattern), std::move (param_type),
3780 : 25013 : std::move (outer_attrs), locus));
3781 : 25013 : }
3782 : 25850 : }
3783 : :
3784 : : /* Parses a function or method return type syntactical construction. Also
3785 : : * handles a function return type not existing. */
3786 : : template <typename ManagedTokenSource>
3787 : : std::unique_ptr<AST::Type>
3788 : 31952 : Parser<ManagedTokenSource>::parse_function_return_type ()
3789 : : {
3790 : 63904 : if (lexer.peek_token ()->get_id () != RETURN_TYPE)
3791 : 8707 : return nullptr;
3792 : :
3793 : : // skip return type, as it now obviously exists
3794 : 23245 : lexer.skip_token ();
3795 : :
3796 : 23245 : std::unique_ptr<AST::Type> type = parse_type ();
3797 : :
3798 : 23245 : return type;
3799 : 23245 : }
3800 : :
3801 : : /* Parses a "where clause" (in a function, struct, method, etc.). Also handles
3802 : : * a where clause not existing, in which it will return
3803 : : * WhereClause::create_empty(), which can be checked via
3804 : : * WhereClause::is_empty(). */
3805 : : template <typename ManagedTokenSource>
3806 : : AST::WhereClause
3807 : 54642 : Parser<ManagedTokenSource>::parse_where_clause ()
3808 : : {
3809 : 54642 : const_TokenPtr where_tok = lexer.peek_token ();
3810 : 54642 : if (where_tok->get_id () != WHERE)
3811 : : {
3812 : : // where clause doesn't exist, so create empty one
3813 : 53547 : return AST::WhereClause::create_empty ();
3814 : : }
3815 : :
3816 : 1095 : lexer.skip_token ();
3817 : :
3818 : : /* parse where clause items - this is not a separate rule in the reference
3819 : : * so won't be here */
3820 : 1095 : std::vector<std::unique_ptr<AST::WhereClauseItem>> where_clause_items;
3821 : :
3822 : 1095 : std::vector<AST::LifetimeParam> for_lifetimes;
3823 : 2190 : if (lexer.peek_token ()->get_id () == FOR)
3824 : 1 : for_lifetimes = parse_for_lifetimes ();
3825 : :
3826 : : /* HACK: where clauses end with a right curly or semicolon or equals in all
3827 : : * uses currently */
3828 : 1095 : const_TokenPtr t = lexer.peek_token ();
3829 : 2481 : while (t->get_id () != LEFT_CURLY && t->get_id () != SEMICOLON
3830 : 2304 : && t->get_id () != EQUAL)
3831 : : {
3832 : 1386 : std::unique_ptr<AST::WhereClauseItem> where_clause_item
3833 : : = parse_where_clause_item (for_lifetimes);
3834 : :
3835 : 1386 : if (where_clause_item == nullptr)
3836 : : {
3837 : 0 : Error error (t->get_locus (), "failed to parse where clause item");
3838 : 0 : add_error (std::move (error));
3839 : :
3840 : 0 : return AST::WhereClause::create_empty ();
3841 : 0 : }
3842 : :
3843 : 1386 : where_clause_items.push_back (std::move (where_clause_item));
3844 : :
3845 : : // also skip comma if it exists
3846 : 2772 : if (lexer.peek_token ()->get_id () != COMMA)
3847 : : break;
3848 : :
3849 : 1209 : lexer.skip_token ();
3850 : 1209 : t = lexer.peek_token ();
3851 : : }
3852 : :
3853 : 1095 : where_clause_items.shrink_to_fit ();
3854 : 1095 : return AST::WhereClause (std::move (where_clause_items));
3855 : 1095 : }
3856 : :
3857 : : /* Parses a where clause item (lifetime or type bound). Does not parse any
3858 : : * commas. */
3859 : : template <typename ManagedTokenSource>
3860 : : std::unique_ptr<AST::WhereClauseItem>
3861 : 1386 : Parser<ManagedTokenSource>::parse_where_clause_item (
3862 : : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
3863 : : {
3864 : : // shitty cheat way of determining lifetime or type bound - test for
3865 : : // lifetime
3866 : 1386 : const_TokenPtr t = lexer.peek_token ();
3867 : :
3868 : 1386 : if (t->get_id () == LIFETIME)
3869 : 3 : return parse_lifetime_where_clause_item ();
3870 : : else
3871 : 1383 : return parse_type_bound_where_clause_item (outer_for_lifetimes);
3872 : 1386 : }
3873 : :
3874 : : // Parses a lifetime where clause item.
3875 : : template <typename ManagedTokenSource>
3876 : : std::unique_ptr<AST::LifetimeWhereClauseItem>
3877 : 3 : Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
3878 : : {
3879 : 3 : auto parsed_lifetime = parse_lifetime (false);
3880 : 3 : if (!parsed_lifetime)
3881 : : {
3882 : : // TODO: error here?
3883 : 0 : return nullptr;
3884 : : }
3885 : 3 : auto lifetime = parsed_lifetime.value ();
3886 : :
3887 : 3 : if (!skip_token (COLON))
3888 : : {
3889 : : // TODO: skip after somewhere
3890 : 0 : return nullptr;
3891 : : }
3892 : :
3893 : 3 : std::vector<AST::Lifetime> lifetime_bounds = parse_lifetime_bounds ();
3894 : : // TODO: have end token passed in?
3895 : :
3896 : 3 : location_t locus = lifetime.get_locus ();
3897 : :
3898 : : return std::unique_ptr<AST::LifetimeWhereClauseItem> (
3899 : 3 : new AST::LifetimeWhereClauseItem (std::move (lifetime),
3900 : 3 : std::move (lifetime_bounds), locus));
3901 : 6 : }
3902 : :
3903 : : // Parses a type bound where clause item.
3904 : : template <typename ManagedTokenSource>
3905 : : std::unique_ptr<AST::TypeBoundWhereClauseItem>
3906 : 1383 : Parser<ManagedTokenSource>::parse_type_bound_where_clause_item (
3907 : : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
3908 : : {
3909 : 1383 : std::vector<AST::LifetimeParam> for_lifetimes = outer_for_lifetimes;
3910 : :
3911 : 1383 : std::unique_ptr<AST::Type> type = parse_type ();
3912 : 1383 : if (type == nullptr)
3913 : : {
3914 : 0 : return nullptr;
3915 : : }
3916 : :
3917 : 1383 : if (!skip_token (COLON))
3918 : : {
3919 : : // TODO: skip after somewhere
3920 : 0 : return nullptr;
3921 : : }
3922 : :
3923 : 2766 : if (lexer.peek_token ()->get_id () == FOR)
3924 : : {
3925 : 9 : auto for_lifetimes_inner = parse_for_lifetimes ();
3926 : 9 : for_lifetimes.insert (for_lifetimes.end (), for_lifetimes_inner.begin (),
3927 : : for_lifetimes_inner.end ());
3928 : 9 : }
3929 : :
3930 : : // parse type param bounds if they exist
3931 : 1383 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds
3932 : : = parse_type_param_bounds ();
3933 : :
3934 : 1383 : location_t locus = lexer.peek_token ()->get_locus ();
3935 : :
3936 : : return std::unique_ptr<AST::TypeBoundWhereClauseItem> (
3937 : 1383 : new AST::TypeBoundWhereClauseItem (std::move (for_lifetimes),
3938 : : std::move (type),
3939 : 1383 : std::move (type_param_bounds), locus));
3940 : 1383 : }
3941 : :
3942 : : // Parses a for lifetimes clause, including the for keyword and angle
3943 : : // brackets.
3944 : : template <typename ManagedTokenSource>
3945 : : std::vector<AST::LifetimeParam>
3946 : 28 : Parser<ManagedTokenSource>::parse_for_lifetimes ()
3947 : : {
3948 : 28 : std::vector<AST::LifetimeParam> params;
3949 : :
3950 : 28 : if (!skip_token (FOR))
3951 : : {
3952 : : // skip after somewhere?
3953 : : return params;
3954 : : }
3955 : :
3956 : 28 : if (!skip_token (LEFT_ANGLE))
3957 : : {
3958 : : // skip after somewhere?
3959 : : return params;
3960 : : }
3961 : :
3962 : : /* cannot specify end token due to parsing problems with '>' tokens being
3963 : : * nested */
3964 : 28 : params = parse_lifetime_params_objs (is_right_angle_tok);
3965 : :
3966 : 28 : if (!skip_generics_right_angle ())
3967 : : {
3968 : : // DEBUG
3969 : 0 : rust_debug ("failed to skip generics right angle after (supposedly) "
3970 : : "finished parsing where clause items");
3971 : : // ok, well this gets called.
3972 : :
3973 : : // skip after somewhere?
3974 : 0 : return params;
3975 : : }
3976 : :
3977 : : return params;
3978 : : }
3979 : :
3980 : : // Parses type parameter bounds in where clause or generic arguments.
3981 : : template <typename ManagedTokenSource>
3982 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
3983 : 3771 : Parser<ManagedTokenSource>::parse_type_param_bounds ()
3984 : : {
3985 : 3771 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3986 : :
3987 : 3771 : std::unique_ptr<AST::TypeParamBound> initial_bound
3988 : : = parse_type_param_bound ();
3989 : :
3990 : : // quick exit if null
3991 : 3771 : if (initial_bound == nullptr)
3992 : : {
3993 : : /* error? type param bounds must have at least one term, but are bounds
3994 : : * optional? */
3995 : : return type_param_bounds;
3996 : : }
3997 : 3771 : type_param_bounds.push_back (std::move (initial_bound));
3998 : :
3999 : 7962 : while (lexer.peek_token ()->get_id () == PLUS)
4000 : : {
4001 : 210 : lexer.skip_token ();
4002 : :
4003 : 210 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
4004 : 210 : if (bound == nullptr)
4005 : : {
4006 : : /* not an error: bound is allowed to be null as trailing plus is
4007 : : * allowed */
4008 : : return type_param_bounds;
4009 : : }
4010 : :
4011 : 210 : type_param_bounds.push_back (std::move (bound));
4012 : : }
4013 : :
4014 : 3771 : type_param_bounds.shrink_to_fit ();
4015 : : return type_param_bounds;
4016 : 3771 : }
4017 : :
4018 : : /* Parses type parameter bounds in where clause or generic arguments, with end
4019 : : * token handling. */
4020 : : template <typename ManagedTokenSource>
4021 : : template <typename EndTokenPred>
4022 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
4023 : 636 : Parser<ManagedTokenSource>::parse_type_param_bounds (EndTokenPred is_end_token)
4024 : : {
4025 : 636 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
4026 : :
4027 : 636 : std::unique_ptr<AST::TypeParamBound> initial_bound
4028 : : = parse_type_param_bound ();
4029 : :
4030 : : // quick exit if null
4031 : 636 : if (initial_bound == nullptr)
4032 : : {
4033 : : /* error? type param bounds must have at least one term, but are bounds
4034 : : * optional? */
4035 : 0 : return type_param_bounds;
4036 : : }
4037 : 636 : type_param_bounds.push_back (std::move (initial_bound));
4038 : :
4039 : 1472 : while (lexer.peek_token ()->get_id () == PLUS)
4040 : : {
4041 : 100 : lexer.skip_token ();
4042 : :
4043 : : // break if end token character
4044 : 200 : if (is_end_token (lexer.peek_token ()->get_id ()))
4045 : : break;
4046 : :
4047 : 100 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
4048 : 100 : if (bound == nullptr)
4049 : : {
4050 : : // TODO how wise is it to ditch all bounds if only one failed?
4051 : 0 : Error error (lexer.peek_token ()->get_locus (),
4052 : : "failed to parse type param bound in type param bounds");
4053 : 0 : add_error (std::move (error));
4054 : :
4055 : 0 : return {};
4056 : 0 : }
4057 : :
4058 : 100 : type_param_bounds.push_back (std::move (bound));
4059 : : }
4060 : :
4061 : 636 : type_param_bounds.shrink_to_fit ();
4062 : 636 : return type_param_bounds;
4063 : 636 : }
4064 : :
4065 : : /* Parses a single type parameter bound in a where clause or generic argument.
4066 : : * Does not parse the '+' between arguments. */
4067 : : template <typename ManagedTokenSource>
4068 : : std::unique_ptr<AST::TypeParamBound>
4069 : 4783 : Parser<ManagedTokenSource>::parse_type_param_bound ()
4070 : : {
4071 : : // shitty cheat way of determining lifetime or trait bound - test for
4072 : : // lifetime
4073 : 4783 : const_TokenPtr t = lexer.peek_token ();
4074 : 4783 : switch (t->get_id ())
4075 : : {
4076 : 113 : case LIFETIME:
4077 : 113 : return std::unique_ptr<AST::Lifetime> (
4078 : 226 : new AST::Lifetime (parse_lifetime (false).value ()));
4079 : 4670 : case LEFT_PAREN:
4080 : : case QUESTION_MARK:
4081 : : case FOR:
4082 : : case IDENTIFIER:
4083 : : case SUPER:
4084 : : case SELF:
4085 : : case SELF_ALIAS:
4086 : : case CRATE:
4087 : : case DOLLAR_SIGN:
4088 : : case SCOPE_RESOLUTION:
4089 : 4670 : return parse_trait_bound ();
4090 : 0 : default:
4091 : : // don't error - assume this is fine TODO
4092 : 0 : return nullptr;
4093 : : }
4094 : 4783 : }
4095 : :
4096 : : // Parses a trait bound type param bound.
4097 : : template <typename ManagedTokenSource>
4098 : : std::unique_ptr<AST::TraitBound>
4099 : 5130 : Parser<ManagedTokenSource>::parse_trait_bound ()
4100 : : {
4101 : 5130 : bool has_parens = false;
4102 : 5130 : bool has_question_mark = false;
4103 : :
4104 : 10260 : location_t locus = lexer.peek_token ()->get_locus ();
4105 : :
4106 : : /* parse optional `for lifetimes`. */
4107 : 5130 : std::vector<AST::LifetimeParam> for_lifetimes;
4108 : 10260 : if (lexer.peek_token ()->get_id () == FOR)
4109 : 14 : for_lifetimes = parse_for_lifetimes ();
4110 : :
4111 : : // handle trait bound being in parentheses
4112 : 10260 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
4113 : : {
4114 : 0 : has_parens = true;
4115 : 0 : lexer.skip_token ();
4116 : : }
4117 : :
4118 : : // handle having question mark (optional)
4119 : 10260 : if (lexer.peek_token ()->get_id () == QUESTION_MARK)
4120 : : {
4121 : 657 : has_question_mark = true;
4122 : 657 : lexer.skip_token ();
4123 : : }
4124 : :
4125 : : // handle TypePath
4126 : 5130 : AST::TypePath type_path = parse_type_path ();
4127 : :
4128 : : // handle closing parentheses
4129 : 5130 : if (has_parens)
4130 : : {
4131 : 0 : if (!skip_token (RIGHT_PAREN))
4132 : : {
4133 : 0 : return nullptr;
4134 : : }
4135 : : }
4136 : :
4137 : : return std::unique_ptr<AST::TraitBound> (
4138 : 5130 : new AST::TraitBound (std::move (type_path), locus, has_parens,
4139 : 5130 : has_question_mark, std::move (for_lifetimes)));
4140 : 5130 : }
4141 : :
4142 : : // Parses lifetime bounds.
4143 : : template <typename ManagedTokenSource>
4144 : : std::vector<AST::Lifetime>
4145 : 3 : Parser<ManagedTokenSource>::parse_lifetime_bounds ()
4146 : : {
4147 : 3 : std::vector<AST::Lifetime> lifetime_bounds;
4148 : :
4149 : 3 : while (true)
4150 : : {
4151 : 3 : auto lifetime = parse_lifetime (false);
4152 : :
4153 : : // quick exit for parsing failure
4154 : 3 : if (!lifetime)
4155 : : break;
4156 : :
4157 : 3 : lifetime_bounds.push_back (std::move (lifetime.value ()));
4158 : :
4159 : : /* plus is maybe not allowed at end - spec defines it weirdly, so
4160 : : * assuming allowed at end */
4161 : 6 : if (lexer.peek_token ()->get_id () != PLUS)
4162 : : break;
4163 : :
4164 : 0 : lexer.skip_token ();
4165 : : }
4166 : :
4167 : 3 : lifetime_bounds.shrink_to_fit ();
4168 : 3 : return lifetime_bounds;
4169 : : }
4170 : :
4171 : : // Parses lifetime bounds, with added check for ending token.
4172 : : template <typename ManagedTokenSource>
4173 : : template <typename EndTokenPred>
4174 : : std::vector<AST::Lifetime>
4175 : 19 : Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
4176 : : {
4177 : 19 : std::vector<AST::Lifetime> lifetime_bounds;
4178 : :
4179 : 79 : while (!is_end_token (lexer.peek_token ()->get_id ()))
4180 : : {
4181 : 20 : auto lifetime = parse_lifetime (false);
4182 : :
4183 : 20 : if (!lifetime)
4184 : : {
4185 : : /* TODO: is it worth throwing away all lifetime bound info just
4186 : : * because one failed? */
4187 : 0 : Error error (lexer.peek_token ()->get_locus (),
4188 : : "failed to parse lifetime in lifetime bounds");
4189 : 0 : add_error (std::move (error));
4190 : :
4191 : 0 : return {};
4192 : 0 : }
4193 : :
4194 : 20 : lifetime_bounds.push_back (std::move (lifetime.value ()));
4195 : :
4196 : : /* plus is maybe not allowed at end - spec defines it weirdly, so
4197 : : * assuming allowed at end */
4198 : 40 : if (lexer.peek_token ()->get_id () != PLUS)
4199 : : break;
4200 : :
4201 : 1 : lexer.skip_token ();
4202 : : }
4203 : :
4204 : 19 : lifetime_bounds.shrink_to_fit ();
4205 : 19 : return lifetime_bounds;
4206 : 19 : }
4207 : :
4208 : : /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
4209 : : * existing. */
4210 : : template <typename ManagedTokenSource>
4211 : : tl::expected<AST::Lifetime, ParseLifetimeError>
4212 : 23045 : Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
4213 : : {
4214 : 23045 : const_TokenPtr lifetime_tok = lexer.peek_token ();
4215 : 23045 : if (lifetime_tok->get_id () != LIFETIME)
4216 : : {
4217 : 18352 : if (allow_elided)
4218 : : {
4219 : 0 : return AST::Lifetime::elided ();
4220 : : }
4221 : : else
4222 : : {
4223 : 18352 : return tl::make_unexpected<ParseLifetimeError> ({});
4224 : : }
4225 : : }
4226 : 4693 : lexer.skip_token ();
4227 : :
4228 : 9386 : return lifetime_from_token (lifetime_tok);
4229 : 23045 : }
4230 : :
4231 : : template <typename ManagedTokenSource>
4232 : : AST::Lifetime
4233 : 4729 : Parser<ManagedTokenSource>::lifetime_from_token (const_TokenPtr tok)
4234 : : {
4235 : 4729 : location_t locus = tok->get_locus ();
4236 : 4729 : std::string lifetime_ident = tok->get_str ();
4237 : :
4238 : 4729 : if (lifetime_ident == "static")
4239 : : {
4240 : 119 : return AST::Lifetime (AST::Lifetime::STATIC, "", locus);
4241 : : }
4242 : 4610 : else if (lifetime_ident == "_")
4243 : : {
4244 : : // Explicitly and implicitly elided lifetimes follow the same rules.
4245 : 779 : return AST::Lifetime (AST::Lifetime::WILDCARD, "", locus);
4246 : : }
4247 : : else
4248 : : {
4249 : 7662 : return AST::Lifetime (AST::Lifetime::NAMED, std::move (lifetime_ident),
4250 : 3831 : locus);
4251 : : }
4252 : 4729 : }
4253 : :
4254 : : template <typename ManagedTokenSource>
4255 : : std::unique_ptr<AST::ExternalTypeItem>
4256 : 5 : Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
4257 : : AST::AttrVec outer_attrs)
4258 : : {
4259 : 5 : location_t locus = lexer.peek_token ()->get_locus ();
4260 : 5 : skip_token (TYPE);
4261 : :
4262 : 5 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
4263 : 5 : if (alias_name_tok == nullptr)
4264 : : {
4265 : 0 : Error error (lexer.peek_token ()->get_locus (),
4266 : : "could not parse identifier in external opaque type");
4267 : 0 : add_error (std::move (error));
4268 : :
4269 : 0 : skip_after_semicolon ();
4270 : 0 : return nullptr;
4271 : 0 : }
4272 : :
4273 : 5 : if (!skip_token (SEMICOLON))
4274 : 1 : return nullptr;
4275 : :
4276 : : return std::unique_ptr<AST::ExternalTypeItem> (
4277 : 12 : new AST::ExternalTypeItem (alias_name_tok->get_str (), std::move (vis),
4278 : 4 : std::move (outer_attrs), std::move (locus)));
4279 : 5 : }
4280 : :
4281 : : // Parses a "type alias" (typedef) item.
4282 : : template <typename ManagedTokenSource>
4283 : : std::unique_ptr<AST::TypeAlias>
4284 : 3937 : Parser<ManagedTokenSource>::parse_type_alias (AST::Visibility vis,
4285 : : AST::AttrVec outer_attrs)
4286 : : {
4287 : 3937 : location_t locus = lexer.peek_token ()->get_locus ();
4288 : 3937 : skip_token (TYPE);
4289 : :
4290 : : // TODO: use this token for identifier when finished that
4291 : 3937 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
4292 : 3937 : if (alias_name_tok == nullptr)
4293 : : {
4294 : 0 : Error error (lexer.peek_token ()->get_locus (),
4295 : : "could not parse identifier in type alias");
4296 : 0 : add_error (std::move (error));
4297 : :
4298 : 0 : skip_after_semicolon ();
4299 : 0 : return nullptr;
4300 : 0 : }
4301 : 3937 : Identifier alias_name{alias_name_tok};
4302 : :
4303 : : // parse generic params, which may not exist
4304 : 3937 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4305 : : = parse_generic_params_in_angles ();
4306 : :
4307 : : // parse where clause, which may not exist
4308 : 3937 : AST::WhereClause where_clause = parse_where_clause ();
4309 : :
4310 : 3937 : if (!skip_token (EQUAL))
4311 : : {
4312 : 0 : skip_after_semicolon ();
4313 : 0 : return nullptr;
4314 : : }
4315 : :
4316 : 3937 : std::unique_ptr<AST::Type> type_to_alias = parse_type ();
4317 : :
4318 : 3937 : if (!skip_token (SEMICOLON))
4319 : : {
4320 : : // should be skipping past this, not the next line
4321 : 0 : return nullptr;
4322 : : }
4323 : :
4324 : : return std::unique_ptr<AST::TypeAlias> (
4325 : 3937 : new AST::TypeAlias (std::move (alias_name), std::move (generic_params),
4326 : : std::move (where_clause), std::move (type_to_alias),
4327 : 3937 : std::move (vis), std::move (outer_attrs), locus));
4328 : 7874 : }
4329 : :
4330 : : // Parse a struct item AST node.
4331 : : template <typename ManagedTokenSource>
4332 : : std::unique_ptr<AST::Struct>
4333 : 3311 : Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
4334 : : AST::AttrVec outer_attrs)
4335 : : {
4336 : : /* TODO: determine best way to parse the proper struct vs tuple struct -
4337 : : * share most of initial constructs so lookahead might be impossible, and if
4338 : : * not probably too expensive. Best way is probably unified parsing for the
4339 : : * initial parts and then pass them in as params to more derived functions.
4340 : : * Alternatively, just parse everything in this one function - do this if
4341 : : * function not too long. */
4342 : :
4343 : : /* Proper struct <- 'struct' IDENTIFIER generic_params? where_clause? ( '{'
4344 : : * struct_fields? '}' | ';' ) */
4345 : : /* Tuple struct <- 'struct' IDENTIFIER generic_params? '(' tuple_fields? ')'
4346 : : * where_clause? ';' */
4347 : 3311 : location_t locus = lexer.peek_token ()->get_locus ();
4348 : 3311 : skip_token (STRUCT_KW);
4349 : :
4350 : : // parse struct name
4351 : 3311 : const_TokenPtr name_tok = expect_token (IDENTIFIER);
4352 : 3311 : if (name_tok == nullptr)
4353 : : {
4354 : 0 : Error error (lexer.peek_token ()->get_locus (),
4355 : : "could not parse struct or tuple struct identifier");
4356 : 0 : add_error (std::move (error));
4357 : :
4358 : : // skip after somewhere?
4359 : 0 : return nullptr;
4360 : 0 : }
4361 : 3311 : Identifier struct_name{name_tok};
4362 : :
4363 : : // parse generic params, which may or may not exist
4364 : 3311 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4365 : : = parse_generic_params_in_angles ();
4366 : :
4367 : : // branch on next token - determines whether proper struct or tuple struct
4368 : 6622 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
4369 : : {
4370 : : // tuple struct
4371 : :
4372 : : // skip left parenthesis
4373 : 1082 : lexer.skip_token ();
4374 : :
4375 : : // parse tuple fields
4376 : 1082 : std::vector<AST::TupleField> tuple_fields;
4377 : : // Might be empty tuple for unit tuple struct.
4378 : 2164 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4379 : 23 : tuple_fields = std::vector<AST::TupleField> ();
4380 : : else
4381 : 1059 : tuple_fields = parse_tuple_fields ();
4382 : :
4383 : : // tuple parameters must have closing parenthesis
4384 : 1082 : if (!skip_token (RIGHT_PAREN))
4385 : : {
4386 : 0 : skip_after_semicolon ();
4387 : 0 : return nullptr;
4388 : : }
4389 : :
4390 : : // parse where clause, which is optional
4391 : 1082 : AST::WhereClause where_clause = parse_where_clause ();
4392 : :
4393 : 1082 : if (!skip_token (SEMICOLON))
4394 : : {
4395 : : // can't skip after semicolon because it's meant to be here
4396 : 0 : return nullptr;
4397 : : }
4398 : :
4399 : 1082 : return std::unique_ptr<AST::TupleStruct> (
4400 : 1082 : new AST::TupleStruct (std::move (tuple_fields), std::move (struct_name),
4401 : : std::move (generic_params),
4402 : : std::move (where_clause), std::move (vis),
4403 : 1082 : std::move (outer_attrs), locus));
4404 : 1082 : }
4405 : :
4406 : : // assume it is a proper struct being parsed and continue outside of switch
4407 : : // - label only here to suppress warning
4408 : :
4409 : : // parse where clause, which is optional
4410 : 2229 : AST::WhereClause where_clause = parse_where_clause ();
4411 : :
4412 : : // branch on next token - determines whether struct is a unit struct
4413 : 2229 : const_TokenPtr t = lexer.peek_token ();
4414 : 2229 : switch (t->get_id ())
4415 : : {
4416 : 1115 : case LEFT_CURLY:
4417 : : {
4418 : : // struct with body
4419 : :
4420 : : // skip curly bracket
4421 : 1115 : lexer.skip_token ();
4422 : :
4423 : : // parse struct fields, if any
4424 : 1115 : std::vector<AST::StructField> struct_fields
4425 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4426 : :
4427 : 1115 : if (!skip_token (RIGHT_CURLY))
4428 : : {
4429 : : // skip somewhere?
4430 : 0 : return nullptr;
4431 : : }
4432 : :
4433 : 1115 : return std::unique_ptr<AST::StructStruct> (new AST::StructStruct (
4434 : : std::move (struct_fields), std::move (struct_name),
4435 : : std::move (generic_params), std::move (where_clause), false,
4436 : 1115 : std::move (vis), std::move (outer_attrs), locus));
4437 : 1115 : }
4438 : 1113 : case SEMICOLON:
4439 : : // unit struct declaration
4440 : :
4441 : 1113 : lexer.skip_token ();
4442 : :
4443 : 1113 : return std::unique_ptr<AST::StructStruct> (
4444 : 2226 : new AST::StructStruct (std::move (struct_name),
4445 : : std::move (generic_params),
4446 : : std::move (where_clause), std::move (vis),
4447 : 1113 : std::move (outer_attrs), locus));
4448 : 1 : default:
4449 : 1 : add_error (Error (t->get_locus (),
4450 : : "unexpected token %qs in struct declaration",
4451 : : t->get_token_description ()));
4452 : :
4453 : : // skip somewhere?
4454 : 1 : return nullptr;
4455 : : }
4456 : 3311 : }
4457 : :
4458 : : // Parses struct fields in struct declarations.
4459 : : template <typename ManagedTokenSource>
4460 : : std::vector<AST::StructField>
4461 : 0 : Parser<ManagedTokenSource>::parse_struct_fields ()
4462 : : {
4463 : 0 : std::vector<AST::StructField> fields;
4464 : :
4465 : 0 : AST::StructField initial_field = parse_struct_field ();
4466 : :
4467 : : // Return empty field list if no field there
4468 : 0 : if (initial_field.is_error ())
4469 : : return fields;
4470 : :
4471 : 0 : fields.push_back (std::move (initial_field));
4472 : :
4473 : 0 : while (lexer.peek_token ()->get_id () == COMMA)
4474 : : {
4475 : 0 : lexer.skip_token ();
4476 : :
4477 : 0 : AST::StructField field = parse_struct_field ();
4478 : :
4479 : 0 : if (field.is_error ())
4480 : : {
4481 : : // would occur with trailing comma, so allowed
4482 : : break;
4483 : : }
4484 : :
4485 : 0 : fields.push_back (std::move (field));
4486 : : }
4487 : :
4488 : 0 : fields.shrink_to_fit ();
4489 : : return fields;
4490 : : // TODO: template if possible (parse_non_ptr_seq)
4491 : 0 : }
4492 : :
4493 : : // Parses struct fields in struct declarations.
4494 : : template <typename ManagedTokenSource>
4495 : : template <typename EndTokenPred>
4496 : : std::vector<AST::StructField>
4497 : 1316 : Parser<ManagedTokenSource>::parse_struct_fields (EndTokenPred is_end_tok)
4498 : : {
4499 : 1316 : std::vector<AST::StructField> fields;
4500 : :
4501 : 1316 : AST::StructField initial_field = parse_struct_field ();
4502 : :
4503 : : // Return empty field list if no field there
4504 : 1316 : if (initial_field.is_error ())
4505 : 43 : return fields;
4506 : :
4507 : 1273 : fields.push_back (std::move (initial_field));
4508 : :
4509 : 5226 : while (lexer.peek_token ()->get_id () == COMMA)
4510 : : {
4511 : 2431 : lexer.skip_token ();
4512 : :
4513 : 4862 : if (is_end_tok (lexer.peek_token ()->get_id ()))
4514 : : break;
4515 : :
4516 : 1340 : AST::StructField field = parse_struct_field ();
4517 : 1340 : if (field.is_error ())
4518 : : {
4519 : : /* TODO: should every field be ditched just because one couldn't be
4520 : : * parsed? */
4521 : 0 : Error error (lexer.peek_token ()->get_locus (),
4522 : : "failed to parse struct field in struct fields");
4523 : 0 : add_error (std::move (error));
4524 : :
4525 : 0 : return {};
4526 : 0 : }
4527 : :
4528 : 1340 : fields.push_back (std::move (field));
4529 : : }
4530 : :
4531 : 1273 : fields.shrink_to_fit ();
4532 : 1273 : return fields;
4533 : : // TODO: template if possible (parse_non_ptr_seq)
4534 : 1316 : }
4535 : :
4536 : : // Parses a single struct field (in a struct definition). Does not parse
4537 : : // commas.
4538 : : template <typename ManagedTokenSource>
4539 : : AST::StructField
4540 : 2656 : Parser<ManagedTokenSource>::parse_struct_field ()
4541 : : {
4542 : : // parse outer attributes, if they exist
4543 : 2656 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4544 : :
4545 : : // parse visibility, if it exists
4546 : 2656 : AST::Visibility vis = parse_visibility ();
4547 : :
4548 : 2656 : location_t locus = lexer.peek_token ()->get_locus ();
4549 : :
4550 : : // parse field name
4551 : 2656 : const_TokenPtr field_name_tok = lexer.peek_token ();
4552 : 2656 : if (field_name_tok->get_id () != IDENTIFIER)
4553 : : {
4554 : : // if not identifier, assumes there is no struct field and exits - not
4555 : : // necessarily error
4556 : 43 : return AST::StructField::create_error ();
4557 : : }
4558 : 2613 : Identifier field_name{field_name_tok};
4559 : 2613 : lexer.skip_token ();
4560 : :
4561 : 2613 : if (!skip_token (COLON))
4562 : : {
4563 : : // skip after somewhere?
4564 : 0 : return AST::StructField::create_error ();
4565 : : }
4566 : :
4567 : : // parse field type - this is required
4568 : 2613 : std::unique_ptr<AST::Type> field_type = parse_type ();
4569 : 2613 : if (field_type == nullptr)
4570 : : {
4571 : 0 : Error error (lexer.peek_token ()->get_locus (),
4572 : : "could not parse type in struct field definition");
4573 : 0 : add_error (std::move (error));
4574 : :
4575 : : // skip after somewhere
4576 : 0 : return AST::StructField::create_error ();
4577 : 0 : }
4578 : :
4579 : 5226 : return AST::StructField (std::move (field_name), std::move (field_type),
4580 : 2613 : std::move (vis), locus, std::move (outer_attrs));
4581 : 7882 : }
4582 : :
4583 : : // Parses tuple fields in tuple/tuple struct declarations.
4584 : : template <typename ManagedTokenSource>
4585 : : std::vector<AST::TupleField>
4586 : 1510 : Parser<ManagedTokenSource>::parse_tuple_fields ()
4587 : : {
4588 : 1510 : std::vector<AST::TupleField> fields;
4589 : :
4590 : 1510 : AST::TupleField initial_field = parse_tuple_field ();
4591 : :
4592 : : // Return empty field list if no field there
4593 : 1510 : if (initial_field.is_error ())
4594 : : {
4595 : 0 : return fields;
4596 : : }
4597 : :
4598 : 1510 : fields.push_back (std::move (initial_field));
4599 : :
4600 : : // maybe think of a better control structure here - do-while with an initial
4601 : : // error state? basically, loop through field list until can't find any more
4602 : : // params HACK: all current syntax uses of tuple fields have them ending
4603 : : // with a right paren token
4604 : 1510 : const_TokenPtr t = lexer.peek_token ();
4605 : 2597 : while (t->get_id () == COMMA)
4606 : : {
4607 : : // skip comma if applies - e.g. trailing comma
4608 : 1089 : lexer.skip_token ();
4609 : :
4610 : : // break out due to right paren if it exists
4611 : 2178 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4612 : : {
4613 : : break;
4614 : : }
4615 : :
4616 : 1087 : AST::TupleField field = parse_tuple_field ();
4617 : 1087 : if (field.is_error ())
4618 : : {
4619 : 0 : Error error (lexer.peek_token ()->get_locus (),
4620 : : "failed to parse tuple field in tuple fields");
4621 : 0 : add_error (std::move (error));
4622 : :
4623 : 0 : return std::vector<AST::TupleField> ();
4624 : 0 : }
4625 : :
4626 : 1087 : fields.push_back (std::move (field));
4627 : :
4628 : 1087 : t = lexer.peek_token ();
4629 : : }
4630 : :
4631 : 1510 : fields.shrink_to_fit ();
4632 : 1510 : return fields;
4633 : :
4634 : : // TODO: this shares basically all code with function params and struct
4635 : : // fields
4636 : : // - templates?
4637 : 1510 : }
4638 : :
4639 : : /* Parses a single tuple struct field in a tuple struct definition. Does not
4640 : : * parse commas. */
4641 : : template <typename ManagedTokenSource>
4642 : : AST::TupleField
4643 : 2597 : Parser<ManagedTokenSource>::parse_tuple_field ()
4644 : : {
4645 : : // parse outer attributes if they exist
4646 : 2597 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4647 : :
4648 : : // parse visibility if it exists
4649 : 2597 : AST::Visibility vis = parse_visibility ();
4650 : :
4651 : 2597 : location_t locus = lexer.peek_token ()->get_locus ();
4652 : :
4653 : : // parse type, which is required
4654 : 2597 : std::unique_ptr<AST::Type> field_type = parse_type ();
4655 : 2597 : if (field_type == nullptr)
4656 : : {
4657 : : // error if null
4658 : 0 : Error error (lexer.peek_token ()->get_locus (),
4659 : : "could not parse type in tuple struct field");
4660 : 0 : add_error (std::move (error));
4661 : :
4662 : : // skip after something
4663 : 0 : return AST::TupleField::create_error ();
4664 : 0 : }
4665 : :
4666 : 2597 : return AST::TupleField (std::move (field_type), std::move (vis), locus,
4667 : 2597 : std::move (outer_attrs));
4668 : 2597 : }
4669 : :
4670 : : // Parses a Rust "enum" tagged union item definition.
4671 : : template <typename ManagedTokenSource>
4672 : : std::unique_ptr<AST::Enum>
4673 : 572 : Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis,
4674 : : AST::AttrVec outer_attrs)
4675 : : {
4676 : 572 : location_t locus = lexer.peek_token ()->get_locus ();
4677 : 572 : skip_token (ENUM_KW);
4678 : :
4679 : : // parse enum name
4680 : 572 : const_TokenPtr enum_name_tok = expect_token (IDENTIFIER);
4681 : 572 : if (enum_name_tok == nullptr)
4682 : 0 : return nullptr;
4683 : :
4684 : 572 : Identifier enum_name = {enum_name_tok};
4685 : :
4686 : : // parse generic params (of enum container, not enum variants) if they exist
4687 : 572 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4688 : : = parse_generic_params_in_angles ();
4689 : :
4690 : : // parse where clause if it exists
4691 : 572 : AST::WhereClause where_clause = parse_where_clause ();
4692 : :
4693 : 572 : if (!skip_token (LEFT_CURLY))
4694 : : {
4695 : 0 : skip_after_end_block ();
4696 : 0 : return nullptr;
4697 : : }
4698 : :
4699 : : // parse actual enum variant definitions
4700 : 572 : std::vector<std::unique_ptr<AST::EnumItem>> enum_items
4701 : : = parse_enum_items ([] (TokenId id) { return id == RIGHT_CURLY; });
4702 : :
4703 : 572 : if (!skip_token (RIGHT_CURLY))
4704 : : {
4705 : 0 : skip_after_end_block ();
4706 : 0 : return nullptr;
4707 : : }
4708 : :
4709 : : return std::unique_ptr<AST::Enum> (
4710 : 572 : new AST::Enum (std::move (enum_name), std::move (vis),
4711 : : std::move (generic_params), std::move (where_clause),
4712 : 572 : std::move (enum_items), std::move (outer_attrs), locus));
4713 : 1144 : }
4714 : :
4715 : : // Parses the enum variants inside an enum definiton.
4716 : : template <typename ManagedTokenSource>
4717 : : std::vector<std::unique_ptr<AST::EnumItem>>
4718 : 0 : Parser<ManagedTokenSource>::parse_enum_items ()
4719 : : {
4720 : 0 : std::vector<std::unique_ptr<AST::EnumItem>> items;
4721 : :
4722 : 0 : std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
4723 : :
4724 : : // Return empty item list if no field there
4725 : 0 : if (initial_item == nullptr)
4726 : : return items;
4727 : :
4728 : 0 : items.push_back (std::move (initial_item));
4729 : :
4730 : 0 : while (lexer.peek_token ()->get_id () == COMMA)
4731 : : {
4732 : 0 : lexer.skip_token ();
4733 : :
4734 : 0 : std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
4735 : 0 : if (item == nullptr)
4736 : : {
4737 : : // this would occur with a trailing comma, which is allowed
4738 : : break;
4739 : : }
4740 : :
4741 : 0 : items.push_back (std::move (item));
4742 : : }
4743 : :
4744 : 0 : items.shrink_to_fit ();
4745 : : return items;
4746 : :
4747 : : /* TODO: use template if doable (parse_non_ptr_sequence) */
4748 : 0 : }
4749 : :
4750 : : // Parses the enum variants inside an enum definiton.
4751 : : template <typename ManagedTokenSource>
4752 : : template <typename EndTokenPred>
4753 : : std::vector<std::unique_ptr<AST::EnumItem>>
4754 : 572 : Parser<ManagedTokenSource>::parse_enum_items (EndTokenPred is_end_tok)
4755 : : {
4756 : 572 : std::vector<std::unique_ptr<AST::EnumItem>> items;
4757 : :
4758 : 572 : std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
4759 : :
4760 : : // Return empty item list if no field there
4761 : 572 : if (initial_item == nullptr)
4762 : 17 : return items;
4763 : :
4764 : 555 : items.push_back (std::move (initial_item));
4765 : :
4766 : 2706 : while (lexer.peek_token ()->get_id () == COMMA)
4767 : : {
4768 : 1321 : lexer.skip_token ();
4769 : :
4770 : 2642 : if (is_end_tok (lexer.peek_token ()->get_id ()))
4771 : : break;
4772 : :
4773 : 798 : std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
4774 : 798 : if (item == nullptr)
4775 : : {
4776 : : /* TODO should this ignore all successfully parsed enum items just
4777 : : * because one failed? */
4778 : 0 : Error error (lexer.peek_token ()->get_locus (),
4779 : : "failed to parse enum item in enum items");
4780 : 0 : add_error (std::move (error));
4781 : :
4782 : 0 : return {};
4783 : 0 : }
4784 : :
4785 : 798 : items.push_back (std::move (item));
4786 : : }
4787 : :
4788 : 555 : items.shrink_to_fit ();
4789 : 555 : return items;
4790 : :
4791 : : /* TODO: use template if doable (parse_non_ptr_sequence) */
4792 : 572 : }
4793 : :
4794 : : /* Parses a single enum variant item in an enum definition. Does not parse
4795 : : * commas. */
4796 : : template <typename ManagedTokenSource>
4797 : : std::unique_ptr<AST::EnumItem>
4798 : 1370 : Parser<ManagedTokenSource>::parse_enum_item ()
4799 : : {
4800 : : // parse outer attributes if they exist
4801 : 1370 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4802 : :
4803 : : // parse visibility, which may or may not exist
4804 : 1370 : AST::Visibility vis = parse_visibility ();
4805 : :
4806 : : // parse name for enum item, which is required
4807 : 1370 : const_TokenPtr item_name_tok = lexer.peek_token ();
4808 : 1370 : if (item_name_tok->get_id () != IDENTIFIER)
4809 : : {
4810 : : // this may not be an error but it means there is no enum item here
4811 : 17 : return nullptr;
4812 : : }
4813 : 1353 : lexer.skip_token ();
4814 : 1353 : Identifier item_name{item_name_tok};
4815 : :
4816 : : // branch based on next token
4817 : 1353 : const_TokenPtr t = lexer.peek_token ();
4818 : 1353 : switch (t->get_id ())
4819 : : {
4820 : 468 : case LEFT_PAREN:
4821 : : {
4822 : : // tuple enum item
4823 : 468 : lexer.skip_token ();
4824 : :
4825 : 468 : std::vector<AST::TupleField> tuple_fields;
4826 : : // Might be empty tuple for unit tuple enum variant.
4827 : 936 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4828 : 17 : tuple_fields = std::vector<AST::TupleField> ();
4829 : : else
4830 : 451 : tuple_fields = parse_tuple_fields ();
4831 : :
4832 : 468 : if (!skip_token (RIGHT_PAREN))
4833 : : {
4834 : : // skip after somewhere
4835 : 0 : return nullptr;
4836 : : }
4837 : :
4838 : 468 : return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
4839 : : std::move (item_name), std::move (vis), std::move (tuple_fields),
4840 : 468 : std::move (outer_attrs), item_name_tok->get_locus ()));
4841 : 468 : }
4842 : 92 : case LEFT_CURLY:
4843 : : {
4844 : : // struct enum item
4845 : 92 : lexer.skip_token ();
4846 : :
4847 : 92 : std::vector<AST::StructField> struct_fields
4848 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4849 : :
4850 : 92 : if (!skip_token (RIGHT_CURLY))
4851 : : {
4852 : : // skip after somewhere
4853 : 0 : return nullptr;
4854 : : }
4855 : :
4856 : 92 : return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
4857 : : std::move (item_name), std::move (vis), std::move (struct_fields),
4858 : 92 : std::move (outer_attrs), item_name_tok->get_locus ()));
4859 : 92 : }
4860 : 281 : case EQUAL:
4861 : : {
4862 : : // discriminant enum item
4863 : 281 : lexer.skip_token ();
4864 : :
4865 : 281 : std::unique_ptr<AST::Expr> discriminant_expr = parse_expr ();
4866 : :
4867 : 281 : return std::unique_ptr<AST::EnumItemDiscriminant> (
4868 : 562 : new AST::EnumItemDiscriminant (std::move (item_name), std::move (vis),
4869 : : std::move (discriminant_expr),
4870 : : std::move (outer_attrs),
4871 : 281 : item_name_tok->get_locus ()));
4872 : 281 : }
4873 : 512 : default:
4874 : : // regular enum with just an identifier
4875 : : return std::unique_ptr<AST::EnumItem> (
4876 : 512 : new AST::EnumItem (std::move (item_name), std::move (vis),
4877 : : std::move (outer_attrs),
4878 : 512 : item_name_tok->get_locus ()));
4879 : : }
4880 : 2723 : }
4881 : :
4882 : : // Parses a C-style (and C-compat) untagged union declaration.
4883 : : template <typename ManagedTokenSource>
4884 : : std::unique_ptr<AST::Union>
4885 : 109 : Parser<ManagedTokenSource>::parse_union (AST::Visibility vis,
4886 : : AST::AttrVec outer_attrs)
4887 : : {
4888 : : /* hack - "weak keyword" by finding identifier called "union" (lookahead in
4889 : : * item switch) */
4890 : 109 : const_TokenPtr union_keyword = expect_token (IDENTIFIER);
4891 : 109 : rust_assert (union_keyword->get_str () == Values::WeakKeywords::UNION);
4892 : 109 : location_t locus = union_keyword->get_locus ();
4893 : :
4894 : : // parse actual union name
4895 : 109 : const_TokenPtr union_name_tok = expect_token (IDENTIFIER);
4896 : 109 : if (union_name_tok == nullptr)
4897 : : {
4898 : 0 : skip_after_next_block ();
4899 : 0 : return nullptr;
4900 : : }
4901 : 109 : Identifier union_name{union_name_tok};
4902 : :
4903 : : // parse optional generic parameters
4904 : 109 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4905 : : = parse_generic_params_in_angles ();
4906 : :
4907 : : // parse optional where clause
4908 : 109 : AST::WhereClause where_clause = parse_where_clause ();
4909 : :
4910 : 109 : if (!skip_token (LEFT_CURLY))
4911 : : {
4912 : 0 : skip_after_end_block ();
4913 : 0 : return nullptr;
4914 : : }
4915 : :
4916 : : /* parse union inner items as "struct fields" because hey, syntax reuse.
4917 : : * Spec said so. */
4918 : 109 : std::vector<AST::StructField> union_fields
4919 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4920 : :
4921 : 109 : if (!skip_token (RIGHT_CURLY))
4922 : : {
4923 : : // skip after somewhere
4924 : 0 : return nullptr;
4925 : : }
4926 : :
4927 : : return std::unique_ptr<AST::Union> (
4928 : 109 : new AST::Union (std::move (union_name), std::move (vis),
4929 : : std::move (generic_params), std::move (where_clause),
4930 : 109 : std::move (union_fields), std::move (outer_attrs), locus));
4931 : 327 : }
4932 : :
4933 : : /* Parses a "constant item" (compile-time constant to maybe "inline"
4934 : : * throughout the program - like constexpr). */
4935 : : template <typename ManagedTokenSource>
4936 : : std::unique_ptr<AST::ConstantItem>
4937 : 1237 : Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
4938 : : AST::AttrVec outer_attrs)
4939 : : {
4940 : 1237 : location_t locus = lexer.peek_token ()->get_locus ();
4941 : 1237 : skip_token (CONST);
4942 : :
4943 : : /* get constant identifier - this is either a proper identifier or the _
4944 : : * wildcard */
4945 : 1237 : const_TokenPtr ident_tok = lexer.peek_token ();
4946 : : // make default identifier the underscore wildcard one
4947 : 1237 : std::string ident (Values::Keywords::UNDERSCORE);
4948 : 1237 : switch (ident_tok->get_id ())
4949 : : {
4950 : 1237 : case IDENTIFIER:
4951 : 1237 : ident = ident_tok->get_str ();
4952 : 1237 : lexer.skip_token ();
4953 : : break;
4954 : 0 : case UNDERSCORE:
4955 : : // do nothing - identifier is already "_"
4956 : 0 : lexer.skip_token ();
4957 : : break;
4958 : 0 : default:
4959 : 0 : add_error (
4960 : 0 : Error (ident_tok->get_locus (),
4961 : : "expected item name (identifier or %<_%>) in constant item "
4962 : : "declaration - found %qs",
4963 : : ident_tok->get_token_description ()));
4964 : :
4965 : 0 : skip_after_semicolon ();
4966 : 0 : return nullptr;
4967 : : }
4968 : :
4969 : 1237 : if (!skip_token (COLON))
4970 : : {
4971 : 0 : skip_after_semicolon ();
4972 : 0 : return nullptr;
4973 : : }
4974 : :
4975 : : // parse constant type (required)
4976 : 1237 : std::unique_ptr<AST::Type> type = parse_type ();
4977 : :
4978 : : // A const with no given expression value
4979 : 2474 : if (lexer.peek_token ()->get_id () == SEMICOLON)
4980 : : {
4981 : 4 : lexer.skip_token ();
4982 : : return std::unique_ptr<AST::ConstantItem> (
4983 : 8 : new AST::ConstantItem (std::move (ident), std::move (vis),
4984 : : std::move (type), std::move (outer_attrs),
4985 : 4 : locus));
4986 : : }
4987 : :
4988 : 1233 : if (!skip_token (EQUAL))
4989 : : {
4990 : 0 : skip_after_semicolon ();
4991 : 0 : return nullptr;
4992 : : }
4993 : :
4994 : : // parse constant expression (required)
4995 : 1233 : std::unique_ptr<AST::Expr> expr = parse_expr ();
4996 : :
4997 : 1233 : if (!skip_token (SEMICOLON))
4998 : : {
4999 : : // skip somewhere?
5000 : 1 : return nullptr;
5001 : : }
5002 : :
5003 : : return std::unique_ptr<AST::ConstantItem> (
5004 : 2464 : new AST::ConstantItem (std::move (ident), std::move (vis), std::move (type),
5005 : 1232 : std::move (expr), std::move (outer_attrs), locus));
5006 : 2474 : }
5007 : :
5008 : : // Parses a "static item" (static storage item, with 'static lifetime).
5009 : : template <typename ManagedTokenSource>
5010 : : std::unique_ptr<AST::StaticItem>
5011 : 93 : Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis,
5012 : : AST::AttrVec outer_attrs)
5013 : : {
5014 : 93 : location_t locus = lexer.peek_token ()->get_locus ();
5015 : 93 : skip_token (STATIC_KW);
5016 : :
5017 : : // determine whether static item is mutable
5018 : 93 : bool is_mut = false;
5019 : 186 : if (lexer.peek_token ()->get_id () == MUT)
5020 : : {
5021 : 4 : is_mut = true;
5022 : 4 : lexer.skip_token ();
5023 : : }
5024 : :
5025 : 93 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5026 : 93 : if (ident_tok == nullptr)
5027 : 0 : return nullptr;
5028 : :
5029 : 93 : Identifier ident{ident_tok};
5030 : :
5031 : 93 : if (!skip_token (COLON))
5032 : : {
5033 : 1 : skip_after_semicolon ();
5034 : 1 : return nullptr;
5035 : : }
5036 : :
5037 : : // parse static item type (required)
5038 : 92 : std::unique_ptr<AST::Type> type = parse_type ();
5039 : :
5040 : 92 : if (!skip_token (EQUAL))
5041 : : {
5042 : 0 : skip_after_semicolon ();
5043 : 0 : return nullptr;
5044 : : }
5045 : :
5046 : : // parse static item expression (required)
5047 : 92 : std::unique_ptr<AST::Expr> expr = parse_expr ();
5048 : :
5049 : 92 : if (!skip_token (SEMICOLON))
5050 : : {
5051 : : // skip after somewhere
5052 : 0 : return nullptr;
5053 : : }
5054 : :
5055 : : return std::unique_ptr<AST::StaticItem> (
5056 : 92 : new AST::StaticItem (std::move (ident), is_mut, std::move (type),
5057 : : std::move (expr), std::move (vis),
5058 : 92 : std::move (outer_attrs), locus));
5059 : 185 : }
5060 : :
5061 : : // Parses a trait definition item, including unsafe ones.
5062 : : template <typename ManagedTokenSource>
5063 : : std::unique_ptr<AST::Trait>
5064 : 3876 : Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
5065 : : AST::AttrVec outer_attrs)
5066 : : {
5067 : 3876 : location_t locus = lexer.peek_token ()->get_locus ();
5068 : 3876 : bool is_unsafe = false;
5069 : 3876 : bool is_auto_trait = false;
5070 : :
5071 : 7752 : if (lexer.peek_token ()->get_id () == UNSAFE)
5072 : : {
5073 : 72 : is_unsafe = true;
5074 : 72 : lexer.skip_token ();
5075 : : }
5076 : :
5077 : 7752 : if (lexer.peek_token ()->get_id () == AUTO)
5078 : : {
5079 : 24 : is_auto_trait = true;
5080 : 24 : lexer.skip_token ();
5081 : : }
5082 : :
5083 : 3876 : skip_token (TRAIT);
5084 : :
5085 : : // parse trait name
5086 : 3876 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5087 : 3876 : if (ident_tok == nullptr)
5088 : 0 : return nullptr;
5089 : :
5090 : 3876 : Identifier ident{ident_tok};
5091 : :
5092 : : // parse generic parameters (if they exist)
5093 : 3876 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5094 : : = parse_generic_params_in_angles ();
5095 : :
5096 : : // create placeholder type param bounds in case they don't exist
5097 : 3876 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
5098 : :
5099 : : // parse type param bounds (if they exist)
5100 : 7752 : if (lexer.peek_token ()->get_id () == COLON)
5101 : : {
5102 : 589 : lexer.skip_token ();
5103 : :
5104 : 589 : type_param_bounds = parse_type_param_bounds (
5105 : 90 : [] (TokenId id) { return id == WHERE || id == LEFT_CURLY; });
5106 : : // type_param_bounds = parse_type_param_bounds ();
5107 : : }
5108 : :
5109 : : // parse where clause (if it exists)
5110 : 3876 : AST::WhereClause where_clause = parse_where_clause ();
5111 : :
5112 : 3876 : if (!skip_token (LEFT_CURLY))
5113 : : {
5114 : 0 : skip_after_end_block ();
5115 : 0 : return nullptr;
5116 : : }
5117 : :
5118 : : // parse inner attrs (if they exist)
5119 : 3876 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5120 : :
5121 : : // parse trait items
5122 : 3876 : std::vector<std::unique_ptr<AST::AssociatedItem>> trait_items;
5123 : :
5124 : 3876 : const_TokenPtr t = lexer.peek_token ();
5125 : 7597 : while (t->get_id () != RIGHT_CURLY)
5126 : : {
5127 : 3721 : std::unique_ptr<AST::AssociatedItem> trait_item = parse_trait_item ();
5128 : :
5129 : 3721 : if (trait_item == nullptr)
5130 : : {
5131 : 0 : Error error (lexer.peek_token ()->get_locus (),
5132 : : "failed to parse trait item in trait");
5133 : 0 : add_error (std::move (error));
5134 : :
5135 : 0 : return nullptr;
5136 : 0 : }
5137 : 3721 : trait_items.push_back (std::move (trait_item));
5138 : :
5139 : 3721 : t = lexer.peek_token ();
5140 : : }
5141 : :
5142 : 3876 : if (!skip_token (RIGHT_CURLY))
5143 : : {
5144 : : // skip after something
5145 : 0 : return nullptr;
5146 : : }
5147 : :
5148 : 3876 : trait_items.shrink_to_fit ();
5149 : : return std::unique_ptr<AST::Trait> (
5150 : 3876 : new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
5151 : : std::move (generic_params), std::move (type_param_bounds),
5152 : : std::move (where_clause), std::move (trait_items),
5153 : : std::move (vis), std::move (outer_attrs),
5154 : 3876 : std::move (inner_attrs), locus));
5155 : 7752 : }
5156 : :
5157 : : // Parses a trait item used inside traits (not trait, the Item).
5158 : : template <typename ManagedTokenSource>
5159 : : std::unique_ptr<AST::AssociatedItem>
5160 : 3723 : Parser<ManagedTokenSource>::parse_trait_item ()
5161 : : {
5162 : : // parse outer attributes (if they exist)
5163 : 3723 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5164 : :
5165 : 3723 : AST::Visibility vis = parse_visibility ();
5166 : :
5167 : : // lookahead to determine what type of trait item to parse
5168 : 3723 : const_TokenPtr tok = lexer.peek_token ();
5169 : 3723 : switch (tok->get_id ())
5170 : : {
5171 : 0 : case SUPER:
5172 : : case SELF:
5173 : : case CRATE:
5174 : : case DOLLAR_SIGN:
5175 : : // these seem to be SimplePath tokens, so this is a macro invocation
5176 : : // semi
5177 : 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
5178 : 1 : case IDENTIFIER:
5179 : 2 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
5180 : 0 : return parse_function (std::move (vis), std::move (outer_attrs));
5181 : : else
5182 : 2 : return parse_macro_invocation_semi (std::move (outer_attrs));
5183 : 775 : case TYPE:
5184 : 775 : return parse_trait_type (std::move (outer_attrs), vis);
5185 : 60 : case CONST:
5186 : : // disambiguate with function qualifier
5187 : 120 : if (lexer.peek_token (1)->get_id () == IDENTIFIER)
5188 : : {
5189 : 118 : return parse_trait_const (std::move (outer_attrs));
5190 : : }
5191 : : // else, fallthrough to function
5192 : : // TODO: find out how to disable gcc "implicit fallthrough" error
5193 : : gcc_fallthrough ();
5194 : : case ASYNC:
5195 : : case UNSAFE:
5196 : : case EXTERN_KW:
5197 : : case FN_KW:
5198 : 5776 : return parse_function (std::move (vis), std::move (outer_attrs));
5199 : : default:
5200 : : break;
5201 : : }
5202 : 0 : add_error (Error (tok->get_locus (),
5203 : : "unrecognised token %qs for item in trait",
5204 : : tok->get_token_description ()));
5205 : : // skip?
5206 : 0 : return nullptr;
5207 : 3723 : }
5208 : :
5209 : : // Parse a typedef trait item.
5210 : : template <typename ManagedTokenSource>
5211 : : std::unique_ptr<AST::TraitItemType>
5212 : 775 : Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs,
5213 : : AST::Visibility vis)
5214 : : {
5215 : 775 : location_t locus = lexer.peek_token ()->get_locus ();
5216 : 775 : skip_token (TYPE);
5217 : :
5218 : 775 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5219 : 775 : if (ident_tok == nullptr)
5220 : 0 : return nullptr;
5221 : :
5222 : 1550 : Identifier ident{ident_tok};
5223 : :
5224 : 775 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
5225 : :
5226 : : // parse optional colon
5227 : 1550 : if (lexer.peek_token ()->get_id () == COLON)
5228 : : {
5229 : 47 : lexer.skip_token ();
5230 : :
5231 : : // parse optional type param bounds
5232 : : bounds
5233 : 47 : = parse_type_param_bounds ([] (TokenId id) { return id == SEMICOLON; });
5234 : : // bounds = parse_type_param_bounds ();
5235 : : }
5236 : :
5237 : 775 : if (!skip_token (SEMICOLON))
5238 : : {
5239 : : // skip?
5240 : 0 : return nullptr;
5241 : : }
5242 : :
5243 : : return std::unique_ptr<AST::TraitItemType> (
5244 : 775 : new AST::TraitItemType (std::move (ident), std::move (bounds),
5245 : 775 : std::move (outer_attrs), vis, locus));
5246 : 775 : }
5247 : :
5248 : : // Parses a constant trait item.
5249 : : template <typename ManagedTokenSource>
5250 : : std::unique_ptr<AST::ConstantItem>
5251 : 59 : Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
5252 : : {
5253 : 59 : location_t locus = lexer.peek_token ()->get_locus ();
5254 : 59 : skip_token (CONST);
5255 : :
5256 : : // parse constant item name
5257 : 59 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5258 : 59 : if (ident_tok == nullptr)
5259 : 0 : return nullptr;
5260 : :
5261 : 59 : Identifier ident{ident_tok};
5262 : :
5263 : 59 : if (!skip_token (COLON))
5264 : : {
5265 : 0 : skip_after_semicolon ();
5266 : 0 : return nullptr;
5267 : : }
5268 : :
5269 : : // parse constant trait item type
5270 : 59 : std::unique_ptr<AST::Type> type = parse_type ();
5271 : :
5272 : : // parse constant trait body expression, if it exists
5273 : 59 : std::unique_ptr<AST::Expr> const_body = nullptr;
5274 : 118 : if (lexer.peek_token ()->get_id () == EQUAL)
5275 : : {
5276 : 12 : lexer.skip_token ();
5277 : :
5278 : : // expression must exist, so parse it
5279 : 12 : const_body = parse_expr ();
5280 : : }
5281 : :
5282 : 59 : if (!skip_token (SEMICOLON))
5283 : : {
5284 : : // skip after something?
5285 : 0 : return nullptr;
5286 : : }
5287 : :
5288 : 177 : return std::unique_ptr<AST::ConstantItem> (new AST::ConstantItem (
5289 : 118 : std::move (ident), AST::Visibility::create_private (), std::move (type),
5290 : 59 : std::move (const_body), std::move (outer_attrs), locus));
5291 : 118 : }
5292 : :
5293 : : /* Parses a struct "impl" item (both inherent impl and trait impl can be
5294 : : * parsed here), */
5295 : : template <typename ManagedTokenSource>
5296 : : std::unique_ptr<AST::Impl>
5297 : 11422 : Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
5298 : : AST::AttrVec outer_attrs)
5299 : : {
5300 : : /* Note that only trait impls are allowed to be unsafe. So if unsafe, it
5301 : : * must be a trait impl. However, this isn't enough for full disambiguation,
5302 : : * so don't branch here. */
5303 : 11422 : location_t locus = lexer.peek_token ()->get_locus ();
5304 : 11422 : bool is_unsafe = false;
5305 : 22844 : if (lexer.peek_token ()->get_id () == UNSAFE)
5306 : : {
5307 : 219 : lexer.skip_token ();
5308 : 219 : is_unsafe = true;
5309 : : }
5310 : :
5311 : 11422 : if (!skip_token (IMPL))
5312 : : {
5313 : 0 : skip_after_next_block ();
5314 : 0 : return nullptr;
5315 : : }
5316 : :
5317 : : // parse generic params (shared by trait and inherent impls)
5318 : 11422 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5319 : : = parse_generic_params_in_angles ();
5320 : :
5321 : : // Again, trait impl-only feature, but optional one, so can be used for
5322 : : // branching yet.
5323 : 11422 : bool has_exclam = false;
5324 : 22844 : if (lexer.peek_token ()->get_id () == EXCLAM)
5325 : : {
5326 : 20 : lexer.skip_token ();
5327 : 20 : has_exclam = true;
5328 : : }
5329 : :
5330 : : /* FIXME: code that doesn't look shit for TypePath. Also, make sure this
5331 : : * doesn't parse too much and not work. */
5332 : 11422 : AST::TypePath type_path = parse_type_path ();
5333 : 22633 : if (type_path.is_error () || lexer.peek_token ()->get_id () != FOR)
5334 : : {
5335 : : /* cannot parse type path (or not for token next, at least), so must be
5336 : : * inherent impl */
5337 : :
5338 : : // hacky conversion of TypePath stack object to Type pointer
5339 : 1284 : std::unique_ptr<AST::Type> type = nullptr;
5340 : 1284 : if (!type_path.is_error ())
5341 : 1073 : type = std::unique_ptr<AST::TypePath> (
5342 : 1073 : new AST::TypePath (std::move (type_path)));
5343 : : else
5344 : 211 : type = parse_type ();
5345 : :
5346 : : // Type is required, so error if null
5347 : 1284 : if (type == nullptr)
5348 : : {
5349 : 1 : Error error (lexer.peek_token ()->get_locus (),
5350 : : "could not parse type in inherent impl");
5351 : 1 : add_error (std::move (error));
5352 : :
5353 : 1 : skip_after_next_block ();
5354 : 1 : return nullptr;
5355 : 1 : }
5356 : :
5357 : : // parse optional where clause
5358 : 1283 : AST::WhereClause where_clause = parse_where_clause ();
5359 : :
5360 : 1283 : if (!skip_token (LEFT_CURLY))
5361 : : {
5362 : : // TODO: does this still skip properly?
5363 : 0 : skip_after_end_block ();
5364 : 0 : return nullptr;
5365 : : }
5366 : :
5367 : : // parse inner attributes (optional)
5368 : 1283 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5369 : :
5370 : : // parse inherent impl items
5371 : 1283 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
5372 : :
5373 : 1283 : const_TokenPtr t = lexer.peek_token ();
5374 : 5362 : while (t->get_id () != RIGHT_CURLY)
5375 : : {
5376 : 4089 : std::unique_ptr<AST::AssociatedItem> impl_item
5377 : : = parse_inherent_impl_item ();
5378 : :
5379 : 4089 : if (impl_item == nullptr)
5380 : : {
5381 : 10 : Error error (
5382 : 10 : lexer.peek_token ()->get_locus (),
5383 : : "failed to parse inherent impl item in inherent impl");
5384 : 10 : add_error (std::move (error));
5385 : :
5386 : 10 : return nullptr;
5387 : 10 : }
5388 : :
5389 : 4079 : impl_items.push_back (std::move (impl_item));
5390 : :
5391 : 4079 : t = lexer.peek_token ();
5392 : : }
5393 : :
5394 : 1273 : if (!skip_token (RIGHT_CURLY))
5395 : : {
5396 : : // skip somewhere
5397 : 0 : return nullptr;
5398 : : }
5399 : :
5400 : : // DEBUG
5401 : 1273 : rust_debug ("successfully parsed inherent impl");
5402 : :
5403 : 1273 : impl_items.shrink_to_fit ();
5404 : :
5405 : 1273 : return std::unique_ptr<AST::InherentImpl> (new AST::InherentImpl (
5406 : : std::move (impl_items), std::move (generic_params), std::move (type),
5407 : : std::move (where_clause), std::move (vis), std::move (inner_attrs),
5408 : 1273 : std::move (outer_attrs), locus));
5409 : 2567 : }
5410 : : else
5411 : : {
5412 : : // type path must both be valid and next token is for, so trait impl
5413 : 10138 : if (!skip_token (FOR))
5414 : : {
5415 : 0 : skip_after_next_block ();
5416 : 0 : return nullptr;
5417 : : }
5418 : :
5419 : : // parse type
5420 : 10138 : std::unique_ptr<AST::Type> type = parse_type ();
5421 : : // ensure type is included as it is required
5422 : 10138 : if (type == nullptr)
5423 : : {
5424 : 0 : Error error (lexer.peek_token ()->get_locus (),
5425 : : "could not parse type in trait impl");
5426 : 0 : add_error (std::move (error));
5427 : :
5428 : 0 : skip_after_next_block ();
5429 : 0 : return nullptr;
5430 : 0 : }
5431 : :
5432 : : // parse optional where clause
5433 : 10138 : AST::WhereClause where_clause = parse_where_clause ();
5434 : :
5435 : 10138 : if (!skip_token (LEFT_CURLY))
5436 : : {
5437 : : // TODO: does this still skip properly?
5438 : 0 : skip_after_end_block ();
5439 : 0 : return nullptr;
5440 : : }
5441 : :
5442 : : // parse inner attributes (optional)
5443 : 10138 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5444 : :
5445 : : // parse trait impl items
5446 : 10138 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
5447 : :
5448 : 10138 : const_TokenPtr t = lexer.peek_token ();
5449 : 38336 : while (t->get_id () != RIGHT_CURLY)
5450 : : {
5451 : 14099 : std::unique_ptr<AST::AssociatedItem> impl_item
5452 : : = parse_trait_impl_item ();
5453 : :
5454 : 14099 : if (impl_item == nullptr)
5455 : : {
5456 : 0 : Error error (lexer.peek_token ()->get_locus (),
5457 : : "failed to parse trait impl item in trait impl");
5458 : 0 : add_error (std::move (error));
5459 : :
5460 : 0 : return nullptr;
5461 : 0 : }
5462 : :
5463 : 14099 : impl_items.push_back (std::move (impl_item));
5464 : :
5465 : 14099 : t = lexer.peek_token ();
5466 : :
5467 : : // DEBUG
5468 : 14099 : rust_debug ("successfully parsed a trait impl item");
5469 : : }
5470 : : // DEBUG
5471 : 10138 : rust_debug ("successfully finished trait impl items");
5472 : :
5473 : 10138 : if (!skip_token (RIGHT_CURLY))
5474 : : {
5475 : : // skip somewhere
5476 : 0 : return nullptr;
5477 : : }
5478 : :
5479 : : // DEBUG
5480 : 10138 : rust_debug ("successfully parsed trait impl");
5481 : :
5482 : 10138 : impl_items.shrink_to_fit ();
5483 : :
5484 : 10138 : return std::unique_ptr<AST::TraitImpl> (
5485 : 20276 : new AST::TraitImpl (std::move (type_path), is_unsafe, has_exclam,
5486 : : std::move (impl_items), std::move (generic_params),
5487 : : std::move (type), std::move (where_clause),
5488 : : std::move (vis), std::move (inner_attrs),
5489 : 10138 : std::move (outer_attrs), locus));
5490 : 20276 : }
5491 : 11422 : }
5492 : :
5493 : : // Parses a single inherent impl item (item inside an inherent impl block).
5494 : : template <typename ManagedTokenSource>
5495 : : std::unique_ptr<AST::AssociatedItem>
5496 : 6095 : Parser<ManagedTokenSource>::parse_inherent_impl_item ()
5497 : : {
5498 : : // parse outer attributes (if they exist)
5499 : 6095 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5500 : :
5501 : : // TODO: cleanup - currently an unreadable mess
5502 : :
5503 : : // branch on next token:
5504 : 6095 : const_TokenPtr t = lexer.peek_token ();
5505 : 6095 : switch (t->get_id ())
5506 : : {
5507 : 1091 : case IDENTIFIER:
5508 : : // FIXME: Arthur: Do we need to some lookahead here?
5509 : 2182 : return parse_macro_invocation_semi (outer_attrs);
5510 : 4281 : case SUPER:
5511 : : case SELF:
5512 : : case CRATE:
5513 : : case PUB:
5514 : : {
5515 : : // visibility, so not a macro invocation semi - must be constant,
5516 : : // function, or method
5517 : 4281 : AST::Visibility vis = parse_visibility ();
5518 : :
5519 : : // TODO: is a recursive call to parse_inherent_impl_item better?
5520 : 8562 : switch (lexer.peek_token ()->get_id ())
5521 : : {
5522 : 2109 : case EXTERN_KW:
5523 : : case UNSAFE:
5524 : : case FN_KW:
5525 : : // function or method
5526 : 4218 : return parse_inherent_impl_function_or_method (std::move (vis),
5527 : : std::move (
5528 : 2109 : outer_attrs));
5529 : 2172 : case CONST:
5530 : : // lookahead to resolve production - could be function/method or
5531 : : // const item
5532 : 2172 : t = lexer.peek_token (1);
5533 : :
5534 : 2172 : switch (t->get_id ())
5535 : : {
5536 : 100 : case IDENTIFIER:
5537 : : case UNDERSCORE:
5538 : 200 : return parse_const_item (std::move (vis),
5539 : 100 : std::move (outer_attrs));
5540 : 2072 : case UNSAFE:
5541 : : case EXTERN_KW:
5542 : : case FN_KW:
5543 : 4144 : return parse_inherent_impl_function_or_method (std::move (vis),
5544 : : std::move (
5545 : 2072 : outer_attrs));
5546 : 0 : default:
5547 : 0 : add_error (Error (t->get_locus (),
5548 : : "unexpected token %qs in some sort of const "
5549 : : "item in inherent impl",
5550 : : t->get_token_description ()));
5551 : :
5552 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5553 : 0 : return nullptr;
5554 : : }
5555 : 0 : default:
5556 : 0 : add_error (
5557 : 0 : Error (t->get_locus (),
5558 : : "unrecognised token %qs for item in inherent impl",
5559 : : t->get_token_description ()));
5560 : : // skip?
5561 : 0 : return nullptr;
5562 : : }
5563 : 4281 : }
5564 : 671 : case ASYNC:
5565 : : case EXTERN_KW:
5566 : : case UNSAFE:
5567 : : case FN_KW:
5568 : : // function or method
5569 : 671 : return parse_inherent_impl_function_or_method (
5570 : 671 : AST::Visibility::create_private (), std::move (outer_attrs));
5571 : 52 : case CONST:
5572 : : /* lookahead to resolve production - could be function/method or const
5573 : : * item */
5574 : 52 : t = lexer.peek_token (1);
5575 : :
5576 : 52 : switch (t->get_id ())
5577 : : {
5578 : 40 : case IDENTIFIER:
5579 : : case UNDERSCORE:
5580 : 80 : return parse_const_item (AST::Visibility::create_private (),
5581 : 40 : std::move (outer_attrs));
5582 : 12 : case UNSAFE:
5583 : : case EXTERN_KW:
5584 : : case FN_KW:
5585 : 12 : return parse_inherent_impl_function_or_method (
5586 : 12 : AST::Visibility::create_private (), std::move (outer_attrs));
5587 : 0 : default:
5588 : 0 : add_error (Error (t->get_locus (),
5589 : : "unexpected token %qs in some sort of const item "
5590 : : "in inherent impl",
5591 : : t->get_token_description ()));
5592 : :
5593 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5594 : 0 : return nullptr;
5595 : : }
5596 : : rust_unreachable ();
5597 : 0 : default:
5598 : 0 : add_error (Error (t->get_locus (),
5599 : : "unrecognised token %qs for item in inherent impl",
5600 : : t->get_token_description ()));
5601 : :
5602 : : // skip?
5603 : 0 : return nullptr;
5604 : : }
5605 : 6095 : }
5606 : :
5607 : : /* For internal use only by parse_inherent_impl_item() - splits giant method
5608 : : * into smaller ones and prevents duplication of logic. Strictly, this parses
5609 : : * a function or method item inside an inherent impl item block. */
5610 : : // TODO: make this a templated function with "return type" as type param -
5611 : : // InherentImplItem is this specialisation of the template while TraitImplItem
5612 : : // will be the other.
5613 : : template <typename ManagedTokenSource>
5614 : : std::unique_ptr<AST::AssociatedItem>
5615 : 4864 : Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
5616 : : AST::Visibility vis, AST::AttrVec outer_attrs)
5617 : : {
5618 : 4864 : location_t locus = lexer.peek_token ()->get_locus ();
5619 : : // parse function or method qualifiers
5620 : 4864 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
5621 : :
5622 : 4864 : skip_token (FN_KW);
5623 : :
5624 : : // parse function or method name
5625 : 4864 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5626 : 4864 : if (ident_tok == nullptr)
5627 : 7 : return nullptr;
5628 : :
5629 : 4857 : Identifier ident{ident_tok};
5630 : :
5631 : : // parse generic params
5632 : 4857 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5633 : : = parse_generic_params_in_angles ();
5634 : :
5635 : 4857 : if (!skip_token (LEFT_PAREN))
5636 : : {
5637 : : // skip after somewhere?
5638 : 0 : return nullptr;
5639 : : }
5640 : :
5641 : : // now for function vs method disambiguation - method has opening "self"
5642 : : // param
5643 : 4857 : auto initial_param = parse_self_param ();
5644 : :
5645 : 4857 : if (!initial_param.has_value ()
5646 : 4857 : && initial_param.error () != ParseSelfError::NOT_SELF)
5647 : 3 : return nullptr;
5648 : :
5649 : : /* FIXME: ensure that self param doesn't accidently consume tokens for a
5650 : : * function one idea is to lookahead up to 4 tokens to see whether self is
5651 : : * one of them */
5652 : 4854 : bool is_method = false;
5653 : 4854 : if (initial_param.has_value ())
5654 : : {
5655 : 3567 : if ((*initial_param)->is_self ())
5656 : : is_method = true;
5657 : :
5658 : : /* skip comma so function and method regular params can be parsed in
5659 : : * same way */
5660 : 7134 : if (lexer.peek_token ()->get_id () == COMMA)
5661 : 2081 : lexer.skip_token ();
5662 : : }
5663 : :
5664 : : // parse trait function params
5665 : 4854 : std::vector<std::unique_ptr<AST::Param>> function_params
5666 : : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
5667 : :
5668 : 4854 : if (initial_param.has_value ())
5669 : 3567 : function_params.insert (function_params.begin (),
5670 : 3567 : std::move (*initial_param));
5671 : :
5672 : 4854 : if (!skip_token (RIGHT_PAREN))
5673 : : {
5674 : 0 : skip_after_end_block ();
5675 : 0 : return nullptr;
5676 : : }
5677 : :
5678 : : // parse return type (optional)
5679 : 4854 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
5680 : :
5681 : : // parse where clause (optional)
5682 : 4854 : AST::WhereClause where_clause = parse_where_clause ();
5683 : :
5684 : 4854 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
5685 : 9708 : if (lexer.peek_token ()->get_id () == SEMICOLON)
5686 : 2 : lexer.skip_token ();
5687 : : else
5688 : : {
5689 : 4852 : auto result = parse_block_expr ();
5690 : :
5691 : 4852 : if (result == nullptr)
5692 : : {
5693 : 0 : Error error (
5694 : 0 : lexer.peek_token ()->get_locus (),
5695 : : "could not parse definition in inherent impl %s definition",
5696 : : is_method ? "method" : "function");
5697 : 0 : add_error (std::move (error));
5698 : :
5699 : 0 : skip_after_end_block ();
5700 : 0 : return nullptr;
5701 : 0 : }
5702 : 4852 : body = std::move (result);
5703 : 4852 : }
5704 : :
5705 : 4854 : return std::unique_ptr<AST::Function> (
5706 : 19414 : new AST::Function (std::move (ident), std::move (qualifiers),
5707 : : std::move (generic_params), std::move (function_params),
5708 : : std::move (return_type), std::move (where_clause),
5709 : : std::move (body), std::move (vis),
5710 : 4854 : std::move (outer_attrs), locus));
5711 : 14575 : }
5712 : :
5713 : : // Parses a single trait impl item (item inside a trait impl block).
5714 : : template <typename ManagedTokenSource>
5715 : : std::unique_ptr<AST::AssociatedItem>
5716 : 14319 : Parser<ManagedTokenSource>::parse_trait_impl_item ()
5717 : : {
5718 : : // parse outer attributes (if they exist)
5719 : 14319 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5720 : :
5721 : 14319 : auto visibility = AST::Visibility::create_private ();
5722 : 28638 : if (lexer.peek_token ()->get_id () == PUB)
5723 : 0 : visibility = parse_visibility ();
5724 : :
5725 : : // branch on next token:
5726 : 14319 : const_TokenPtr t = lexer.peek_token ();
5727 : 14319 : switch (t->get_id ())
5728 : : {
5729 : 0 : case SUPER:
5730 : : case SELF:
5731 : : case CRATE:
5732 : : case DOLLAR_SIGN:
5733 : : // these seem to be SimplePath tokens, so this is a macro invocation
5734 : : // semi
5735 : 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
5736 : 116 : case IDENTIFIER:
5737 : 232 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
5738 : 104 : return parse_trait_impl_function_or_method (visibility,
5739 : 52 : std::move (outer_attrs));
5740 : : else
5741 : 128 : return parse_macro_invocation_semi (std::move (outer_attrs));
5742 : 3871 : case TYPE:
5743 : 7742 : return parse_type_alias (visibility, std::move (outer_attrs));
5744 : 10256 : case EXTERN_KW:
5745 : : case UNSAFE:
5746 : : case FN_KW:
5747 : : // function or method
5748 : 20512 : return parse_trait_impl_function_or_method (visibility,
5749 : 10256 : std::move (outer_attrs));
5750 : 1 : case ASYNC:
5751 : 2 : return parse_async_item (visibility, std::move (outer_attrs));
5752 : 75 : case CONST:
5753 : : // lookahead to resolve production - could be function/method or const
5754 : : // item
5755 : 75 : t = lexer.peek_token (1);
5756 : :
5757 : 75 : switch (t->get_id ())
5758 : : {
5759 : 74 : case IDENTIFIER:
5760 : : case UNDERSCORE:
5761 : 148 : return parse_const_item (visibility, std::move (outer_attrs));
5762 : 1 : case UNSAFE:
5763 : : case EXTERN_KW:
5764 : : case FN_KW:
5765 : 2 : return parse_trait_impl_function_or_method (visibility,
5766 : 1 : std::move (outer_attrs));
5767 : 0 : default:
5768 : 0 : add_error (Error (
5769 : : t->get_locus (),
5770 : : "unexpected token %qs in some sort of const item in trait impl",
5771 : : t->get_token_description ()));
5772 : :
5773 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5774 : 0 : return nullptr;
5775 : : }
5776 : : rust_unreachable ();
5777 : : default:
5778 : : break;
5779 : : }
5780 : 0 : add_error (Error (t->get_locus (),
5781 : : "unrecognised token %qs for item in trait impl",
5782 : : t->get_token_description ()));
5783 : :
5784 : : // skip?
5785 : 0 : return nullptr;
5786 : 14319 : }
5787 : :
5788 : : /* For internal use only by parse_trait_impl_item() - splits giant method into
5789 : : * smaller ones and prevents duplication of logic. Strictly, this parses a
5790 : : * function or method item inside a trait impl item block. */
5791 : : template <typename ManagedTokenSource>
5792 : : std::unique_ptr<AST::AssociatedItem>
5793 : 10309 : Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
5794 : : AST::Visibility vis, AST::AttrVec outer_attrs)
5795 : : {
5796 : : // this shares virtually all logic with
5797 : : // parse_inherent_impl_function_or_method
5798 : : // - template?
5799 : 10309 : location_t locus = lexer.peek_token ()->get_locus ();
5800 : :
5801 : 10309 : auto is_default = false;
5802 : 10309 : auto t = lexer.peek_token ();
5803 : 10309 : if (t->get_id () == IDENTIFIER
5804 : 10309 : && t->get_str () == Values::WeakKeywords::DEFAULT)
5805 : : {
5806 : 52 : is_default = true;
5807 : 52 : lexer.skip_token ();
5808 : : }
5809 : :
5810 : : // parse function or method qualifiers
5811 : 10309 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
5812 : :
5813 : 10309 : skip_token (FN_KW);
5814 : :
5815 : : // parse function or method name
5816 : 10309 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5817 : 10309 : if (ident_tok == nullptr)
5818 : : {
5819 : 0 : return nullptr;
5820 : : }
5821 : 10309 : Identifier ident{ident_tok};
5822 : :
5823 : : // DEBUG:
5824 : 10309 : rust_debug (
5825 : : "about to start parsing generic params in trait impl function or method");
5826 : :
5827 : : // parse generic params
5828 : 10309 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5829 : : = parse_generic_params_in_angles ();
5830 : :
5831 : : // DEBUG:
5832 : 10309 : rust_debug (
5833 : : "finished parsing generic params in trait impl function or method");
5834 : :
5835 : 10309 : if (!skip_token (LEFT_PAREN))
5836 : : {
5837 : : // skip after somewhere?
5838 : 0 : return nullptr;
5839 : : }
5840 : :
5841 : : // now for function vs method disambiguation - method has opening "self"
5842 : : // param
5843 : 10309 : auto initial_param = parse_self_param ();
5844 : :
5845 : 10309 : if (!initial_param.has_value ()
5846 : 10309 : && initial_param.error () != ParseSelfError::NOT_SELF)
5847 : 0 : return nullptr;
5848 : :
5849 : : // FIXME: ensure that self param doesn't accidently consume tokens for a
5850 : : // function
5851 : 10309 : bool is_method = false;
5852 : 10309 : if (initial_param.has_value ())
5853 : : {
5854 : 9293 : if ((*initial_param)->is_self ())
5855 : : is_method = true;
5856 : :
5857 : : // skip comma so function and method regular params can be parsed in
5858 : : // same way
5859 : 18586 : if (lexer.peek_token ()->get_id () == COMMA)
5860 : : {
5861 : 6818 : lexer.skip_token ();
5862 : : }
5863 : :
5864 : : // DEBUG
5865 : 9293 : rust_debug ("successfully parsed self param in method trait impl item");
5866 : : }
5867 : :
5868 : : // DEBUG
5869 : 10309 : rust_debug (
5870 : : "started to parse function params in function or method trait impl item");
5871 : :
5872 : : // parse trait function params (only if next token isn't right paren)
5873 : 10309 : std::vector<std::unique_ptr<AST::Param>> function_params;
5874 : 20618 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
5875 : : {
5876 : : function_params
5877 : 7612 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
5878 : :
5879 : 7612 : if (function_params.empty ())
5880 : : {
5881 : 0 : Error error (
5882 : 0 : lexer.peek_token ()->get_locus (),
5883 : : "failed to parse function params in trait impl %s definition",
5884 : : is_method ? "method" : "function");
5885 : 0 : add_error (std::move (error));
5886 : :
5887 : 0 : skip_after_next_block ();
5888 : 0 : return nullptr;
5889 : 0 : }
5890 : : }
5891 : :
5892 : 10309 : if (initial_param.has_value ())
5893 : 9293 : function_params.insert (function_params.begin (),
5894 : 9293 : std::move (*initial_param));
5895 : :
5896 : : // DEBUG
5897 : 10309 : rust_debug ("successfully parsed function params in function or method "
5898 : : "trait impl item");
5899 : :
5900 : 10309 : if (!skip_token (RIGHT_PAREN))
5901 : : {
5902 : 0 : skip_after_next_block ();
5903 : 0 : return nullptr;
5904 : : }
5905 : :
5906 : : // parse return type (optional)
5907 : 10309 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
5908 : :
5909 : : // DEBUG
5910 : 10309 : rust_debug (
5911 : : "successfully parsed return type in function or method trait impl item");
5912 : :
5913 : : // parse where clause (optional)
5914 : 10309 : AST::WhereClause where_clause = parse_where_clause ();
5915 : :
5916 : : // DEBUG
5917 : 10309 : rust_debug (
5918 : : "successfully parsed where clause in function or method trait impl item");
5919 : :
5920 : : // parse function definition (in block) - semicolon not allowed
5921 : 10309 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
5922 : :
5923 : 20618 : if (lexer.peek_token ()->get_id () == SEMICOLON)
5924 : 1 : lexer.skip_token ();
5925 : : else
5926 : : {
5927 : 10308 : auto result = parse_block_expr ();
5928 : 10308 : if (result == nullptr)
5929 : : {
5930 : 0 : Error error (lexer.peek_token ()->get_locus (),
5931 : : "could not parse definition in trait impl %s definition",
5932 : : is_method ? "method" : "function");
5933 : 0 : add_error (std::move (error));
5934 : :
5935 : 0 : skip_after_end_block ();
5936 : 0 : return nullptr;
5937 : 0 : }
5938 : 10308 : body = std::move (result);
5939 : 10308 : }
5940 : :
5941 : 10309 : return std::unique_ptr<AST::Function> (
5942 : 41235 : new AST::Function (std::move (ident), std::move (qualifiers),
5943 : : std::move (generic_params), std::move (function_params),
5944 : : std::move (return_type), std::move (where_clause),
5945 : : std::move (body), std::move (vis),
5946 : 10309 : std::move (outer_attrs), locus, is_default));
5947 : 30927 : }
5948 : :
5949 : : // Parses an extern block of declarations.
5950 : : template <typename ManagedTokenSource>
5951 : : std::unique_ptr<AST::ExternBlock>
5952 : 1522 : Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
5953 : : AST::AttrVec outer_attrs)
5954 : : {
5955 : 1522 : location_t locus = lexer.peek_token ()->get_locus ();
5956 : 1522 : skip_token (EXTERN_KW);
5957 : :
5958 : : // detect optional abi name
5959 : 1522 : std::string abi;
5960 : 1522 : const_TokenPtr next_tok = lexer.peek_token ();
5961 : 1522 : if (next_tok->get_id () == STRING_LITERAL)
5962 : : {
5963 : 1522 : lexer.skip_token ();
5964 : 1522 : abi = next_tok->get_str ();
5965 : : }
5966 : :
5967 : 1522 : if (!skip_token (LEFT_CURLY))
5968 : : {
5969 : 0 : skip_after_end_block ();
5970 : 0 : return nullptr;
5971 : : }
5972 : :
5973 : 1522 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5974 : :
5975 : : // parse declarations inside extern block
5976 : 1522 : std::vector<std::unique_ptr<AST::ExternalItem>> extern_items;
5977 : :
5978 : 1522 : const_TokenPtr t = lexer.peek_token ();
5979 : 4559 : while (t->get_id () != RIGHT_CURLY)
5980 : : {
5981 : 3038 : std::unique_ptr<AST::ExternalItem> extern_item = parse_external_item ();
5982 : :
5983 : 3038 : if (extern_item == nullptr)
5984 : : {
5985 : 1 : Error error (t->get_locus (),
5986 : : "failed to parse external item despite not reaching "
5987 : : "end of extern block");
5988 : 1 : add_error (std::move (error));
5989 : :
5990 : 1 : return nullptr;
5991 : 1 : }
5992 : :
5993 : 3037 : extern_items.push_back (std::move (extern_item));
5994 : :
5995 : 3037 : t = lexer.peek_token ();
5996 : : }
5997 : :
5998 : 1521 : if (!skip_token (RIGHT_CURLY))
5999 : : {
6000 : : // skip somewhere
6001 : 0 : return nullptr;
6002 : : }
6003 : :
6004 : 1521 : extern_items.shrink_to_fit ();
6005 : :
6006 : : return std::unique_ptr<AST::ExternBlock> (
6007 : 1521 : new AST::ExternBlock (std::move (abi), std::move (extern_items),
6008 : : std::move (vis), std::move (inner_attrs),
6009 : 1521 : std::move (outer_attrs), locus));
6010 : 3044 : }
6011 : :
6012 : : // Parses a single extern block item (static or function declaration).
6013 : : template <typename ManagedTokenSource>
6014 : : std::unique_ptr<AST::ExternalItem>
6015 : 3041 : Parser<ManagedTokenSource>::parse_external_item ()
6016 : : {
6017 : : // parse optional outer attributes
6018 : 3041 : AST::AttrVec outer_attrs = parse_outer_attributes ();
6019 : :
6020 : 3041 : location_t locus = lexer.peek_token ()->get_locus ();
6021 : :
6022 : : // parse optional visibility
6023 : 3041 : AST::Visibility vis = parse_visibility ();
6024 : :
6025 : 3041 : const_TokenPtr t = lexer.peek_token ();
6026 : 3041 : switch (t->get_id ())
6027 : : {
6028 : 2 : case IDENTIFIER:
6029 : 4 : return parse_macro_invocation_semi (outer_attrs);
6030 : 1 : case STATIC_KW:
6031 : : {
6032 : : // parse extern static item
6033 : 1 : lexer.skip_token ();
6034 : :
6035 : : // parse mut (optional)
6036 : 1 : bool has_mut = false;
6037 : 2 : if (lexer.peek_token ()->get_id () == MUT)
6038 : : {
6039 : 0 : lexer.skip_token ();
6040 : 0 : has_mut = true;
6041 : : }
6042 : :
6043 : : // parse identifier
6044 : 1 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
6045 : 1 : if (ident_tok == nullptr)
6046 : : {
6047 : 0 : skip_after_semicolon ();
6048 : 0 : return nullptr;
6049 : : }
6050 : 1 : Identifier ident{ident_tok};
6051 : :
6052 : 1 : if (!skip_token (COLON))
6053 : : {
6054 : 0 : skip_after_semicolon ();
6055 : 0 : return nullptr;
6056 : : }
6057 : :
6058 : : // parse type (required)
6059 : 1 : std::unique_ptr<AST::Type> type = parse_type ();
6060 : 1 : if (type == nullptr)
6061 : : {
6062 : 0 : Error error (lexer.peek_token ()->get_locus (),
6063 : : "failed to parse type in external static item");
6064 : 0 : add_error (std::move (error));
6065 : :
6066 : 0 : skip_after_semicolon ();
6067 : 0 : return nullptr;
6068 : 0 : }
6069 : :
6070 : 1 : if (!skip_token (SEMICOLON))
6071 : : {
6072 : : // skip after somewhere?
6073 : 0 : return nullptr;
6074 : : }
6075 : :
6076 : 1 : return std::unique_ptr<AST::ExternalStaticItem> (
6077 : 2 : new AST::ExternalStaticItem (std::move (ident), std::move (type),
6078 : : has_mut, std::move (vis),
6079 : 1 : std::move (outer_attrs), locus));
6080 : 3 : }
6081 : 3033 : case FN_KW:
6082 : 6066 : return parse_function (std::move (vis), std::move (outer_attrs), true);
6083 : :
6084 : 5 : case TYPE:
6085 : 5 : return parse_external_type_item (std::move (vis),
6086 : 5 : std::move (outer_attrs));
6087 : 0 : default:
6088 : : // error
6089 : 0 : add_error (
6090 : 0 : Error (t->get_locus (),
6091 : : "unrecognised token %qs in extern block item declaration",
6092 : : t->get_token_description ()));
6093 : :
6094 : 0 : skip_after_semicolon ();
6095 : 0 : return nullptr;
6096 : : }
6097 : 3041 : }
6098 : :
6099 : : // Parses a statement (will further disambiguate any statement).
6100 : : template <typename ManagedTokenSource>
6101 : : std::unique_ptr<AST::Stmt>
6102 : 900 : Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
6103 : : {
6104 : : // quick exit for empty statement
6105 : : // FIXME: Can we have empty statements without semicolons? Just nothing?
6106 : 900 : const_TokenPtr t = lexer.peek_token ();
6107 : 900 : if (t->get_id () == SEMICOLON)
6108 : : {
6109 : 30 : lexer.skip_token ();
6110 : 30 : return std::unique_ptr<AST::EmptyStmt> (
6111 : 30 : new AST::EmptyStmt (t->get_locus ()));
6112 : : }
6113 : :
6114 : : // parse outer attributes
6115 : 870 : AST::AttrVec outer_attrs = parse_outer_attributes ();
6116 : :
6117 : : // parsing this will be annoying because of the many different possibilities
6118 : : /* best may be just to copy paste in parse_item switch, and failing that try
6119 : : * to parse outer attributes, and then pass them in to either a let
6120 : : * statement or (fallback) expression statement. */
6121 : : // FIXME: think of a way to do this without such a large switch?
6122 : 870 : t = lexer.peek_token ();
6123 : 870 : switch (t->get_id ())
6124 : : {
6125 : 200 : case LET:
6126 : : // let statement
6127 : 200 : return parse_let_stmt (std::move (outer_attrs), restrictions);
6128 : 186 : case PUB:
6129 : : case MOD:
6130 : : case EXTERN_KW:
6131 : : case USE:
6132 : : case FN_KW:
6133 : : case TYPE:
6134 : : case STRUCT_KW:
6135 : : case ENUM_KW:
6136 : : case CONST:
6137 : : case STATIC_KW:
6138 : : case AUTO:
6139 : : case TRAIT:
6140 : : case IMPL:
6141 : : case MACRO:
6142 : : /* TODO: implement union keyword but not really because of
6143 : : * context-dependence crappy hack way to parse a union written below to
6144 : : * separate it from the good code. */
6145 : : // case UNION:
6146 : : case UNSAFE: // maybe - unsafe traits are a thing
6147 : : /* if any of these (should be all possible VisItem prefixes), parse a
6148 : : * VisItem can't parse item because would require reparsing outer
6149 : : * attributes */
6150 : : // may also be unsafe block
6151 : 372 : if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
6152 : : {
6153 : 1 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
6154 : : }
6155 : : else
6156 : : {
6157 : 185 : return parse_vis_item (std::move (outer_attrs));
6158 : : }
6159 : : break;
6160 : : // crappy hack to do union "keyword"
6161 : 240 : case IDENTIFIER:
6162 : 240 : if (t->get_str () == Values::WeakKeywords::UNION
6163 : 240 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
6164 : : {
6165 : 0 : return parse_vis_item (std::move (outer_attrs));
6166 : : // or should this go straight to parsing union?
6167 : : }
6168 : 480 : else if (is_macro_rules_def (t))
6169 : : {
6170 : : // macro_rules! macro item
6171 : 2 : return parse_macro_rules_def (std::move (outer_attrs));
6172 : : }
6173 : : gcc_fallthrough ();
6174 : : // TODO: find out how to disable gcc "implicit fallthrough" warning
6175 : : default:
6176 : : // fallback: expression statement
6177 : 482 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
6178 : : break;
6179 : : }
6180 : 870 : }
6181 : :
6182 : : // Parses a let statement.
6183 : : template <typename ManagedTokenSource>
6184 : : std::unique_ptr<AST::LetStmt>
6185 : 22742 : Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
6186 : : ParseRestrictions restrictions)
6187 : : {
6188 : 22742 : location_t locus = lexer.peek_token ()->get_locus ();
6189 : 22742 : skip_token (LET);
6190 : :
6191 : : // parse pattern (required)
6192 : 22742 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
6193 : 22742 : if (pattern == nullptr)
6194 : : {
6195 : 0 : Error error (lexer.peek_token ()->get_locus (),
6196 : : "failed to parse pattern in let statement");
6197 : 0 : add_error (std::move (error));
6198 : :
6199 : 0 : skip_after_semicolon ();
6200 : 0 : return nullptr;
6201 : 0 : }
6202 : :
6203 : : // parse type declaration (optional)
6204 : 22742 : std::unique_ptr<AST::Type> type = nullptr;
6205 : 45484 : if (lexer.peek_token ()->get_id () == COLON)
6206 : : {
6207 : : // must have a type declaration
6208 : 2491 : lexer.skip_token ();
6209 : :
6210 : 2491 : type = parse_type ();
6211 : 2491 : if (type == nullptr)
6212 : : {
6213 : 0 : Error error (lexer.peek_token ()->get_locus (),
6214 : : "failed to parse type in let statement");
6215 : 0 : add_error (std::move (error));
6216 : :
6217 : 0 : skip_after_semicolon ();
6218 : 0 : return nullptr;
6219 : 0 : }
6220 : : }
6221 : :
6222 : : // parse expression to set variable to (optional)
6223 : 22742 : std::unique_ptr<AST::Expr> expr = nullptr;
6224 : 45484 : if (lexer.peek_token ()->get_id () == EQUAL)
6225 : : {
6226 : : // must have an expression
6227 : 21654 : lexer.skip_token ();
6228 : :
6229 : 21654 : expr = parse_expr ();
6230 : 21654 : if (expr == nullptr)
6231 : : {
6232 : 22 : Error error (lexer.peek_token ()->get_locus (),
6233 : : "failed to parse expression in let statement");
6234 : 22 : add_error (std::move (error));
6235 : :
6236 : 22 : skip_after_semicolon ();
6237 : 22 : return nullptr;
6238 : 22 : }
6239 : : }
6240 : :
6241 : 22720 : tl::optional<std::unique_ptr<AST::Expr>> else_expr = tl::nullopt;
6242 : 22720 : if (maybe_skip_token (ELSE))
6243 : 0 : else_expr = parse_block_expr ();
6244 : :
6245 : 22720 : if (restrictions.consume_semi)
6246 : : {
6247 : : // `stmt` macro variables are parsed without a semicolon, but should be
6248 : : // parsed as a full statement when interpolated. This should be handled
6249 : : // by having the interpolated statement be distinguishable from normal
6250 : : // tokens, e.g. by NT tokens.
6251 : 22575 : if (restrictions.allow_close_after_expr_stmt)
6252 : 55 : maybe_skip_token (SEMICOLON);
6253 : 22520 : else if (!skip_token (SEMICOLON))
6254 : 1 : return nullptr;
6255 : : }
6256 : :
6257 : : return std::unique_ptr<AST::LetStmt> (
6258 : 45438 : new AST::LetStmt (std::move (pattern), std::move (expr), std::move (type),
6259 : 22719 : std::move (else_expr), std::move (outer_attrs), locus));
6260 : 22742 : }
6261 : :
6262 : : // Parses a type path.
6263 : : template <typename ManagedTokenSource>
6264 : : AST::TypePath
6265 : 125542 : Parser<ManagedTokenSource>::parse_type_path ()
6266 : : {
6267 : 125542 : bool has_opening_scope_resolution = false;
6268 : 125542 : location_t locus = lexer.peek_token ()->get_locus ();
6269 : 251084 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
6270 : : {
6271 : 28 : has_opening_scope_resolution = true;
6272 : 28 : lexer.skip_token ();
6273 : : }
6274 : :
6275 : : // create segment vector
6276 : 125542 : std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
6277 : :
6278 : : // parse required initial segment
6279 : 125542 : std::unique_ptr<AST::TypePathSegment> initial_segment
6280 : : = parse_type_path_segment ();
6281 : 125542 : if (initial_segment == nullptr)
6282 : : {
6283 : : // skip after somewhere?
6284 : : // don't necessarily throw error but yeah
6285 : 211 : return AST::TypePath::create_error ();
6286 : : }
6287 : 125331 : segments.push_back (std::move (initial_segment));
6288 : :
6289 : : // parse optional segments (as long as scope resolution operator exists)
6290 : 125331 : const_TokenPtr t = lexer.peek_token ();
6291 : 129285 : while (t->get_id () == SCOPE_RESOLUTION)
6292 : : {
6293 : : // skip scope resolution operator
6294 : 3954 : lexer.skip_token ();
6295 : :
6296 : : // parse the actual segment - it is an error if it doesn't exist now
6297 : 3954 : std::unique_ptr<AST::TypePathSegment> segment
6298 : : = parse_type_path_segment ();
6299 : 3954 : if (segment == nullptr)
6300 : : {
6301 : : // skip after somewhere?
6302 : 0 : Error error (t->get_locus (), "could not parse type path segment");
6303 : 0 : add_error (std::move (error));
6304 : :
6305 : 0 : return AST::TypePath::create_error ();
6306 : 0 : }
6307 : :
6308 : 3954 : segments.push_back (std::move (segment));
6309 : :
6310 : 3954 : t = lexer.peek_token ();
6311 : : }
6312 : :
6313 : 125331 : segments.shrink_to_fit ();
6314 : :
6315 : 250662 : return AST::TypePath (std::move (segments), locus,
6316 : 125331 : has_opening_scope_resolution);
6317 : 125542 : }
6318 : :
6319 : : template <typename ManagedTokenSource>
6320 : : tl::optional<AST::GenericArg>
6321 : 19129 : Parser<ManagedTokenSource>::parse_generic_arg ()
6322 : : {
6323 : 19129 : auto tok = lexer.peek_token ();
6324 : 19129 : std::unique_ptr<AST::Expr> expr = nullptr;
6325 : :
6326 : 19129 : switch (tok->get_id ())
6327 : : {
6328 : 14491 : case IDENTIFIER:
6329 : : {
6330 : : // This is a bit of a weird situation: With an identifier token, we
6331 : : // could either have a valid type or a macro (FIXME: anything else?). So
6332 : : // we need one bit of lookahead to differentiate if this is really
6333 : 14491 : auto next_tok = lexer.peek_token (1);
6334 : 14491 : if (next_tok->get_id () == LEFT_ANGLE
6335 : 13720 : || next_tok->get_id () == SCOPE_RESOLUTION
6336 : 28135 : || next_tok->get_id () == EXCLAM)
6337 : : {
6338 : 855 : auto type = parse_type ();
6339 : 855 : if (type)
6340 : 855 : return AST::GenericArg::create_type (std::move (type));
6341 : : else
6342 : 0 : return tl::nullopt;
6343 : 855 : }
6344 : 13636 : else if (next_tok->get_id () == COLON)
6345 : : {
6346 : 79 : lexer.skip_token (); // skip ident
6347 : 79 : lexer.skip_token (); // skip colon
6348 : :
6349 : 79 : auto tok = lexer.peek_token ();
6350 : 79 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
6351 : : = parse_type_param_bounds ();
6352 : :
6353 : 79 : auto type = std::unique_ptr<AST::TraitObjectType> (
6354 : 79 : new AST::TraitObjectType (std::move (bounds), tok->get_locus (),
6355 : : false));
6356 : 79 : if (type)
6357 : 79 : return AST::GenericArg::create_type (std::move (type));
6358 : : else
6359 : : return tl::nullopt;
6360 : 158 : }
6361 : 13557 : lexer.skip_token ();
6362 : 54228 : return AST::GenericArg::create_ambiguous (tok->get_str (),
6363 : 13557 : tok->get_locus ());
6364 : 14491 : }
6365 : 19 : case LEFT_CURLY:
6366 : 19 : expr = parse_block_expr ();
6367 : 19 : break;
6368 : 91 : case MINUS:
6369 : : case STRING_LITERAL:
6370 : : case CHAR_LITERAL:
6371 : : case INT_LITERAL:
6372 : : case FLOAT_LITERAL:
6373 : : case TRUE_LITERAL:
6374 : : case FALSE_LITERAL:
6375 : 91 : expr = parse_literal_expr ();
6376 : 91 : break;
6377 : : // FIXME: Because of this, error reporting is garbage for const generic
6378 : : // parameter's default values
6379 : 4528 : default:
6380 : : {
6381 : 4528 : auto type = parse_type ();
6382 : : // FIXME: Find a better way to do this?
6383 : 4528 : if (type)
6384 : 4527 : return AST::GenericArg::create_type (std::move (type));
6385 : : else
6386 : 1 : return tl::nullopt;
6387 : 4528 : }
6388 : : }
6389 : :
6390 : 110 : if (!expr)
6391 : 0 : return tl::nullopt;
6392 : :
6393 : 110 : return AST::GenericArg::create_const (std::move (expr));
6394 : 19129 : }
6395 : :
6396 : : // Parses the generic arguments in each path segment.
6397 : : template <typename ManagedTokenSource>
6398 : : AST::GenericArgs
6399 : 19167 : Parser<ManagedTokenSource>::parse_path_generic_args ()
6400 : : {
6401 : 38334 : if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
6402 : 19 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
6403 : :
6404 : 19167 : if (!skip_token (LEFT_ANGLE))
6405 : : {
6406 : : // skip after somewhere?
6407 : 0 : return AST::GenericArgs::create_empty ();
6408 : : }
6409 : :
6410 : : // We need to parse all lifetimes, then parse types and const generics in
6411 : : // any order.
6412 : :
6413 : : // try to parse lifetimes first
6414 : 19167 : std::vector<AST::Lifetime> lifetime_args;
6415 : :
6416 : 19167 : const_TokenPtr t = lexer.peek_token ();
6417 : 19167 : location_t locus = t->get_locus ();
6418 : 38833 : while (!is_right_angle_tok (t->get_id ()))
6419 : : {
6420 : 19666 : auto lifetime = parse_lifetime (false);
6421 : 19666 : if (!lifetime)
6422 : : {
6423 : : // not necessarily an error
6424 : : break;
6425 : : }
6426 : :
6427 : 1415 : lifetime_args.push_back (std::move (lifetime.value ()));
6428 : :
6429 : : // if next token isn't comma, then it must be end of list
6430 : 2830 : if (lexer.peek_token ()->get_id () != COMMA)
6431 : : {
6432 : : break;
6433 : : }
6434 : : // skip comma
6435 : 500 : lexer.skip_token ();
6436 : :
6437 : 500 : t = lexer.peek_token ();
6438 : : }
6439 : :
6440 : : // try to parse types and const generics second
6441 : 19167 : std::vector<AST::GenericArg> generic_args;
6442 : :
6443 : : // TODO: think of better control structure
6444 : 19167 : t = lexer.peek_token ();
6445 : 56526 : while (!is_right_angle_tok (t->get_id ()))
6446 : : {
6447 : : // FIXME: Is it fine to break if there is one binding? Can't there be
6448 : : // bindings in between types?
6449 : :
6450 : : // ensure not binding being parsed as type accidently
6451 : 24220 : if (t->get_id () == IDENTIFIER
6452 : 34245 : && lexer.peek_token (1)->get_id () == EQUAL)
6453 : : break;
6454 : :
6455 : 19109 : auto arg = parse_generic_arg ();
6456 : 19109 : if (arg)
6457 : : {
6458 : 19109 : generic_args.emplace_back (std::move (arg.value ()));
6459 : : }
6460 : :
6461 : : // FIXME: Do we need to break if we encounter an error?
6462 : :
6463 : : // if next token isn't comma, then it must be end of list
6464 : 38218 : if (lexer.peek_token ()->get_id () != COMMA)
6465 : : break;
6466 : :
6467 : : // skip comma
6468 : 1182 : lexer.skip_token ();
6469 : 1182 : t = lexer.peek_token ();
6470 : : }
6471 : :
6472 : : // try to parse bindings third
6473 : 19167 : std::vector<AST::GenericArgsBinding> binding_args;
6474 : :
6475 : : // TODO: think of better control structure
6476 : 19167 : t = lexer.peek_token ();
6477 : 19498 : while (!is_right_angle_tok (t->get_id ()))
6478 : : {
6479 : 331 : AST::GenericArgsBinding binding = parse_generic_args_binding ();
6480 : 331 : if (binding.is_error ())
6481 : : {
6482 : : // not necessarily an error
6483 : : break;
6484 : : }
6485 : :
6486 : 331 : binding_args.push_back (std::move (binding));
6487 : :
6488 : : // if next token isn't comma, then it must be end of list
6489 : 662 : if (lexer.peek_token ()->get_id () != COMMA)
6490 : : {
6491 : : break;
6492 : : }
6493 : : // skip comma
6494 : 8 : lexer.skip_token ();
6495 : :
6496 : 8 : t = lexer.peek_token ();
6497 : : }
6498 : :
6499 : : // skip any trailing commas
6500 : 38334 : if (lexer.peek_token ()->get_id () == COMMA)
6501 : 0 : lexer.skip_token ();
6502 : :
6503 : 19167 : if (!skip_generics_right_angle ())
6504 : 0 : return AST::GenericArgs::create_empty ();
6505 : :
6506 : 19167 : lifetime_args.shrink_to_fit ();
6507 : 19167 : generic_args.shrink_to_fit ();
6508 : 19167 : binding_args.shrink_to_fit ();
6509 : :
6510 : 19167 : return AST::GenericArgs (std::move (lifetime_args), std::move (generic_args),
6511 : 19167 : std::move (binding_args), locus);
6512 : 38334 : }
6513 : :
6514 : : // Parses a binding in a generic args path segment.
6515 : : template <typename ManagedTokenSource>
6516 : : AST::GenericArgsBinding
6517 : 331 : Parser<ManagedTokenSource>::parse_generic_args_binding ()
6518 : : {
6519 : 331 : const_TokenPtr ident_tok = lexer.peek_token ();
6520 : 331 : if (ident_tok->get_id () != IDENTIFIER)
6521 : : {
6522 : : // allow non error-inducing use
6523 : : // skip somewhere?
6524 : 0 : return AST::GenericArgsBinding::create_error ();
6525 : : }
6526 : 331 : lexer.skip_token ();
6527 : 331 : Identifier ident{ident_tok};
6528 : :
6529 : 331 : if (!skip_token (EQUAL))
6530 : : {
6531 : : // skip after somewhere?
6532 : 0 : return AST::GenericArgsBinding::create_error ();
6533 : : }
6534 : :
6535 : : // parse type (required)
6536 : 331 : std::unique_ptr<AST::Type> type = parse_type ();
6537 : 331 : if (type == nullptr)
6538 : : {
6539 : : // skip somewhere?
6540 : 0 : return AST::GenericArgsBinding::create_error ();
6541 : : }
6542 : :
6543 : 662 : return AST::GenericArgsBinding (std::move (ident), std::move (type),
6544 : 662 : ident_tok->get_locus ());
6545 : 662 : }
6546 : :
6547 : : /* Parses a single type path segment (not including opening scope resolution,
6548 : : * but includes any internal ones). Includes generic args or type path
6549 : : * functions too. */
6550 : : template <typename ManagedTokenSource>
6551 : : std::unique_ptr<AST::TypePathSegment>
6552 : 133049 : Parser<ManagedTokenSource>::parse_type_path_segment ()
6553 : : {
6554 : 133049 : location_t locus = lexer.peek_token ()->get_locus ();
6555 : : // parse ident segment part
6556 : 133049 : AST::PathIdentSegment ident_segment = parse_path_ident_segment ();
6557 : 133049 : if (ident_segment.is_error ())
6558 : : {
6559 : : // not necessarily an error
6560 : 211 : return nullptr;
6561 : : }
6562 : :
6563 : : /* lookahead to determine if variants exist - only consume scope resolution
6564 : : * then */
6565 : 132838 : bool has_separating_scope_resolution = false;
6566 : 132838 : const_TokenPtr next = lexer.peek_token (1);
6567 : 265676 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
6568 : 132838 : && (next->get_id () == LEFT_ANGLE || next->get_id () == LEFT_PAREN))
6569 : : {
6570 : 0 : has_separating_scope_resolution = true;
6571 : 0 : lexer.skip_token ();
6572 : : }
6573 : :
6574 : : // branch into variants on next token
6575 : 132838 : const_TokenPtr t = lexer.peek_token ();
6576 : 132838 : switch (t->get_id ())
6577 : : {
6578 : 17786 : case LEFT_SHIFT:
6579 : : case LEFT_ANGLE:
6580 : : {
6581 : : // parse generic args
6582 : 17786 : AST::GenericArgs generic_args = parse_path_generic_args ();
6583 : :
6584 : 17786 : return std::unique_ptr<AST::TypePathSegmentGeneric> (
6585 : 35572 : new AST::TypePathSegmentGeneric (std::move (ident_segment),
6586 : : has_separating_scope_resolution,
6587 : 17786 : std::move (generic_args), locus));
6588 : 17786 : }
6589 : 536 : case LEFT_PAREN:
6590 : : {
6591 : : // parse type path function
6592 : 536 : AST::TypePathFunction type_path_function
6593 : : = parse_type_path_function (locus);
6594 : :
6595 : 536 : if (type_path_function.is_error ())
6596 : : {
6597 : : // skip after somewhere?
6598 : 0 : return nullptr;
6599 : : }
6600 : :
6601 : 536 : return std::unique_ptr<AST::TypePathSegmentFunction> (
6602 : 1608 : new AST::TypePathSegmentFunction (std::move (ident_segment),
6603 : : has_separating_scope_resolution,
6604 : : std::move (type_path_function),
6605 : 536 : locus));
6606 : 536 : }
6607 : 114516 : default:
6608 : : // neither of them
6609 : : return std::unique_ptr<AST::TypePathSegment> (
6610 : 114516 : new AST::TypePathSegment (std::move (ident_segment),
6611 : 114516 : has_separating_scope_resolution, locus));
6612 : : }
6613 : : rust_unreachable ();
6614 : 265676 : }
6615 : :
6616 : : // Parses a function call representation inside a type path.
6617 : : template <typename ManagedTokenSource>
6618 : : AST::TypePathFunction
6619 : 536 : Parser<ManagedTokenSource>::parse_type_path_function (location_t id_location)
6620 : : {
6621 : 536 : if (!skip_token (LEFT_PAREN))
6622 : : {
6623 : : // skip somewhere?
6624 : : return AST::TypePathFunction::create_error ();
6625 : : }
6626 : :
6627 : : // parse function inputs
6628 : 536 : std::vector<std::unique_ptr<AST::Type>> inputs;
6629 : :
6630 : 2040 : while (lexer.peek_token ()->get_id () != RIGHT_PAREN)
6631 : : {
6632 : 735 : std::unique_ptr<AST::Type> type = parse_type ();
6633 : 735 : if (type == nullptr)
6634 : : {
6635 : : /* this is an error as there should've been a ')' there if there
6636 : : * wasn't a type */
6637 : 0 : Error error (
6638 : 0 : lexer.peek_token ()->get_locus (),
6639 : : "failed to parse type in parameters of type path function");
6640 : 0 : add_error (std::move (error));
6641 : :
6642 : : // skip somewhere?
6643 : 0 : return AST::TypePathFunction::create_error ();
6644 : 0 : }
6645 : :
6646 : 735 : inputs.push_back (std::move (type));
6647 : :
6648 : : // skip commas, including trailing commas
6649 : 1470 : if (lexer.peek_token ()->get_id () != COMMA)
6650 : : break;
6651 : :
6652 : 226 : lexer.skip_token ();
6653 : : }
6654 : :
6655 : 536 : if (!skip_token (RIGHT_PAREN))
6656 : : {
6657 : : // skip somewhere?
6658 : : return AST::TypePathFunction::create_error ();
6659 : : }
6660 : :
6661 : : // parse optional return type
6662 : 536 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
6663 : :
6664 : 536 : inputs.shrink_to_fit ();
6665 : 536 : return AST::TypePathFunction (std::move (inputs), id_location,
6666 : 536 : std::move (return_type));
6667 : 536 : }
6668 : :
6669 : : // Parses a path inside an expression that allows generic arguments.
6670 : : template <typename ManagedTokenSource>
6671 : : AST::PathInExpression
6672 : 365107 : Parser<ManagedTokenSource>::parse_path_in_expression ()
6673 : : {
6674 : 365107 : location_t locus = UNKNOWN_LOCATION;
6675 : 365107 : bool has_opening_scope_resolution = false;
6676 : 730214 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
6677 : : {
6678 : 9 : has_opening_scope_resolution = true;
6679 : :
6680 : 9 : locus = lexer.peek_token ()->get_locus ();
6681 : :
6682 : 9 : lexer.skip_token ();
6683 : : }
6684 : :
6685 : : // create segment vector
6686 : 365107 : std::vector<AST::PathExprSegment> segments;
6687 : :
6688 : 365107 : if (locus == UNKNOWN_LOCATION)
6689 : : {
6690 : 730196 : locus = lexer.peek_token ()->get_locus ();
6691 : : }
6692 : :
6693 : : // parse required initial segment
6694 : 365107 : AST::PathExprSegment initial_segment = parse_path_expr_segment ();
6695 : 365107 : if (initial_segment.is_error ())
6696 : : {
6697 : : // skip after somewhere?
6698 : : // don't necessarily throw error but yeah
6699 : 2 : return AST::PathInExpression::create_error ();
6700 : : }
6701 : 365105 : segments.push_back (std::move (initial_segment));
6702 : :
6703 : : // parse optional segments (as long as scope resolution operator exists)
6704 : 365105 : const_TokenPtr t = lexer.peek_token ();
6705 : 381723 : while (t->get_id () == SCOPE_RESOLUTION)
6706 : : {
6707 : : // skip scope resolution operator
6708 : 16618 : lexer.skip_token ();
6709 : :
6710 : : // parse the actual segment - it is an error if it doesn't exist now
6711 : 16618 : AST::PathExprSegment segment = parse_path_expr_segment ();
6712 : 16618 : if (segment.is_error ())
6713 : : {
6714 : : // skip after somewhere?
6715 : 0 : Error error (t->get_locus (),
6716 : : "could not parse path expression segment");
6717 : 0 : add_error (std::move (error));
6718 : :
6719 : 0 : return AST::PathInExpression::create_error ();
6720 : 0 : }
6721 : :
6722 : 16618 : segments.push_back (std::move (segment));
6723 : :
6724 : 16618 : t = lexer.peek_token ();
6725 : : }
6726 : :
6727 : 365105 : segments.shrink_to_fit ();
6728 : :
6729 : 365105 : return AST::PathInExpression (std::move (segments), {}, locus,
6730 : 365105 : has_opening_scope_resolution);
6731 : 730212 : }
6732 : :
6733 : : /* Parses a single path in expression path segment (including generic
6734 : : * arguments). */
6735 : : template <typename ManagedTokenSource>
6736 : : AST::PathExprSegment
6737 : 411517 : Parser<ManagedTokenSource>::parse_path_expr_segment ()
6738 : : {
6739 : 411517 : location_t locus = lexer.peek_token ()->get_locus ();
6740 : : // parse ident segment
6741 : 411517 : AST::PathIdentSegment ident = parse_path_ident_segment ();
6742 : 411517 : if (ident.is_error ())
6743 : : {
6744 : : // not necessarily an error?
6745 : 2 : return AST::PathExprSegment::create_error ();
6746 : : }
6747 : :
6748 : : // parse generic args (and turbofish), if they exist
6749 : : /* use lookahead to determine if they actually exist (don't want to
6750 : : * accidently parse over next ident segment) */
6751 : 823030 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
6752 : 706705 : && (lexer.peek_token (1)->get_id () == LEFT_ANGLE
6753 : 302426 : || lexer.peek_token (1)->get_id () == LEFT_SHIFT))
6754 : : {
6755 : : // skip scope resolution
6756 : 1381 : lexer.skip_token ();
6757 : :
6758 : : // Let parse_path_generic_args split "<<" tokens
6759 : 1381 : AST::GenericArgs generic_args = parse_path_generic_args ();
6760 : :
6761 : 2762 : return AST::PathExprSegment (std::move (ident), locus,
6762 : 1381 : std::move (generic_args));
6763 : 1381 : }
6764 : :
6765 : : // return a generic parameter-less expr segment if not found
6766 : 410134 : return AST::PathExprSegment (std::move (ident), locus);
6767 : 411517 : }
6768 : :
6769 : : /* Parses a fully qualified path in expression (i.e. a pattern). FIXME does
6770 : : * not parse outer attrs. */
6771 : : template <typename ManagedTokenSource>
6772 : : AST::QualifiedPathInExpression
6773 : 187 : Parser<ManagedTokenSource>::parse_qualified_path_in_expression (
6774 : : location_t pratt_parsed_loc)
6775 : : {
6776 : : /* Note: the Rust grammar is defined in such a way that it is impossible to
6777 : : * determine whether a prospective qualified path is a
6778 : : * QualifiedPathInExpression or QualifiedPathInType in all cases by the
6779 : : * rules themselves (the only possible difference is a TypePathSegment with
6780 : : * function, and lookahead to find this is too difficult). However, as this
6781 : : * is a pattern and QualifiedPathInType is a type, I believe it that their
6782 : : * construction will not be confused (due to rules regarding patterns vs
6783 : : * types).
6784 : : * As such, this function will not attempt to minimise errors created by
6785 : : * their confusion. */
6786 : :
6787 : : // parse the qualified path type (required)
6788 : 187 : AST::QualifiedPathType qual_path_type
6789 : : = parse_qualified_path_type (pratt_parsed_loc);
6790 : 187 : if (qual_path_type.is_error ())
6791 : : {
6792 : : // TODO: should this create a parse error?
6793 : 0 : return AST::QualifiedPathInExpression::create_error ();
6794 : : }
6795 : 187 : location_t locus = qual_path_type.get_locus ();
6796 : :
6797 : : // parse path segments
6798 : 187 : std::vector<AST::PathExprSegment> segments;
6799 : :
6800 : : // parse initial required segment
6801 : 374 : if (!expect_token (SCOPE_RESOLUTION))
6802 : : {
6803 : : // skip after somewhere?
6804 : :
6805 : 0 : return AST::QualifiedPathInExpression::create_error ();
6806 : : }
6807 : 187 : AST::PathExprSegment initial_segment = parse_path_expr_segment ();
6808 : 187 : if (initial_segment.is_error ())
6809 : : {
6810 : : // skip after somewhere?
6811 : 0 : Error error (lexer.peek_token ()->get_locus (),
6812 : : "required initial path expression segment in "
6813 : : "qualified path in expression could not be parsed");
6814 : 0 : add_error (std::move (error));
6815 : :
6816 : 0 : return AST::QualifiedPathInExpression::create_error ();
6817 : 0 : }
6818 : 187 : segments.push_back (std::move (initial_segment));
6819 : :
6820 : : // parse optional segments (as long as scope resolution operator exists)
6821 : 187 : const_TokenPtr t = lexer.peek_token ();
6822 : 187 : while (t->get_id () == SCOPE_RESOLUTION)
6823 : : {
6824 : : // skip scope resolution operator
6825 : 0 : lexer.skip_token ();
6826 : :
6827 : : // parse the actual segment - it is an error if it doesn't exist now
6828 : 0 : AST::PathExprSegment segment = parse_path_expr_segment ();
6829 : 0 : if (segment.is_error ())
6830 : : {
6831 : : // skip after somewhere?
6832 : 0 : Error error (t->get_locus (),
6833 : : "could not parse path expression segment in qualified "
6834 : : "path in expression");
6835 : 0 : add_error (std::move (error));
6836 : :
6837 : 0 : return AST::QualifiedPathInExpression::create_error ();
6838 : 0 : }
6839 : :
6840 : 0 : segments.push_back (std::move (segment));
6841 : :
6842 : 0 : t = lexer.peek_token ();
6843 : : }
6844 : :
6845 : 187 : segments.shrink_to_fit ();
6846 : :
6847 : : // FIXME: outer attr parsing
6848 : 374 : return AST::QualifiedPathInExpression (std::move (qual_path_type),
6849 : 187 : std::move (segments), {}, locus);
6850 : 374 : }
6851 : :
6852 : : // Parses the type syntactical construction at the start of a qualified path.
6853 : : template <typename ManagedTokenSource>
6854 : : AST::QualifiedPathType
6855 : 3740 : Parser<ManagedTokenSource>::parse_qualified_path_type (
6856 : : location_t pratt_parsed_loc)
6857 : : {
6858 : 3740 : location_t locus = pratt_parsed_loc;
6859 : : /* TODO: should this actually be error? is there anywhere where this could
6860 : : * be valid? */
6861 : 3740 : if (locus == UNKNOWN_LOCATION)
6862 : : {
6863 : 3553 : locus = lexer.peek_token ()->get_locus ();
6864 : :
6865 : 7106 : if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
6866 : 2 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
6867 : :
6868 : : // skip after somewhere?
6869 : 3553 : if (!skip_token (LEFT_ANGLE))
6870 : 0 : return AST::QualifiedPathType::create_error ();
6871 : : }
6872 : :
6873 : : // parse type (required)
6874 : 3740 : std::unique_ptr<AST::Type> type = parse_type ();
6875 : 3740 : if (type == nullptr)
6876 : : {
6877 : 0 : Error error (lexer.peek_token ()->get_locus (),
6878 : : "could not parse type in qualified path type");
6879 : 0 : add_error (std::move (error));
6880 : :
6881 : : // skip somewhere?
6882 : 0 : return AST::QualifiedPathType::create_error ();
6883 : 0 : }
6884 : :
6885 : : // parse optional as clause
6886 : 3740 : AST::TypePath as_type_path = AST::TypePath::create_error ();
6887 : 7480 : if (lexer.peek_token ()->get_id () == AS)
6888 : : {
6889 : 3629 : lexer.skip_token ();
6890 : :
6891 : : // parse type path, which is required now
6892 : 3629 : as_type_path = parse_type_path ();
6893 : 3629 : if (as_type_path.is_error ())
6894 : : {
6895 : 0 : Error error (
6896 : 0 : lexer.peek_token ()->get_locus (),
6897 : : "could not parse type path in as clause in qualified path type");
6898 : 0 : add_error (std::move (error));
6899 : :
6900 : : // skip somewhere?
6901 : 0 : return AST::QualifiedPathType::create_error ();
6902 : 0 : }
6903 : : }
6904 : :
6905 : : /* NOTE: should actually be a right-angle token, so
6906 : : * skip_generics_right_angle shouldn't be required */
6907 : 3740 : if (!skip_token (RIGHT_ANGLE))
6908 : : {
6909 : : // skip after somewhere?
6910 : 0 : return AST::QualifiedPathType::create_error ();
6911 : : }
6912 : :
6913 : 3740 : return AST::QualifiedPathType (std::move (type), locus,
6914 : 3740 : std::move (as_type_path));
6915 : 3740 : }
6916 : :
6917 : : // Parses a fully qualified path in type (i.e. a type).
6918 : : template <typename ManagedTokenSource>
6919 : : AST::QualifiedPathInType
6920 : 3553 : Parser<ManagedTokenSource>::parse_qualified_path_in_type ()
6921 : : {
6922 : 3553 : location_t locus = lexer.peek_token ()->get_locus ();
6923 : : // parse the qualified path type (required)
6924 : 3553 : AST::QualifiedPathType qual_path_type = parse_qualified_path_type ();
6925 : 3553 : if (qual_path_type.is_error ())
6926 : : {
6927 : : // TODO: should this create a parse error?
6928 : 0 : return AST::QualifiedPathInType::create_error ();
6929 : : }
6930 : :
6931 : : // parse initial required segment
6932 : 7106 : if (!expect_token (SCOPE_RESOLUTION))
6933 : : {
6934 : : // skip after somewhere?
6935 : :
6936 : 0 : return AST::QualifiedPathInType::create_error ();
6937 : : }
6938 : 3553 : std::unique_ptr<AST::TypePathSegment> initial_segment
6939 : : = parse_type_path_segment ();
6940 : 3553 : if (initial_segment == nullptr)
6941 : : {
6942 : : // skip after somewhere?
6943 : 0 : Error error (lexer.peek_token ()->get_locus (),
6944 : : "required initial type path segment in qualified path in "
6945 : : "type could not be parsed");
6946 : 0 : add_error (std::move (error));
6947 : :
6948 : 0 : return AST::QualifiedPathInType::create_error ();
6949 : 0 : }
6950 : :
6951 : : // parse optional segments (as long as scope resolution operator exists)
6952 : 3553 : std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
6953 : 3553 : const_TokenPtr t = lexer.peek_token ();
6954 : 3553 : while (t->get_id () == SCOPE_RESOLUTION)
6955 : : {
6956 : : // skip scope resolution operator
6957 : 0 : lexer.skip_token ();
6958 : :
6959 : : // parse the actual segment - it is an error if it doesn't exist now
6960 : 0 : std::unique_ptr<AST::TypePathSegment> segment
6961 : : = parse_type_path_segment ();
6962 : 0 : if (segment == nullptr)
6963 : : {
6964 : : // skip after somewhere?
6965 : 0 : Error error (
6966 : : t->get_locus (),
6967 : : "could not parse type path segment in qualified path in type");
6968 : 0 : add_error (std::move (error));
6969 : :
6970 : 0 : return AST::QualifiedPathInType::create_error ();
6971 : 0 : }
6972 : :
6973 : 0 : segments.push_back (std::move (segment));
6974 : :
6975 : 0 : t = lexer.peek_token ();
6976 : : }
6977 : :
6978 : 3553 : segments.shrink_to_fit ();
6979 : :
6980 : 7106 : return AST::QualifiedPathInType (std::move (qual_path_type),
6981 : : std::move (initial_segment),
6982 : 3553 : std::move (segments), locus);
6983 : 3553 : }
6984 : :
6985 : : // Parses a self param. Also handles self param not existing.
6986 : : template <typename ManagedTokenSource>
6987 : : tl::expected<std::unique_ptr<AST::Param>, ParseSelfError>
6988 : 31419 : Parser<ManagedTokenSource>::parse_self_param ()
6989 : : {
6990 : 31419 : bool has_reference = false;
6991 : 31419 : AST::Lifetime lifetime = AST::Lifetime::elided ();
6992 : :
6993 : 31419 : location_t locus = lexer.peek_token ()->get_locus ();
6994 : :
6995 : : // TODO: Feels off, find a better way to clearly express this
6996 : 125676 : std::vector<std::vector<TokenId>> ptrs
6997 : : = {{ASTERISK, SELF} /* *self */,
6998 : : {ASTERISK, CONST, SELF} /* *const self */,
6999 : : {ASTERISK, MUT, SELF} /* *mut self */};
7000 : :
7001 : 125670 : for (auto &s : ptrs)
7002 : : {
7003 : : size_t i = 0;
7004 : 94265 : for (i = 0; i < s.size (); i++)
7005 : 188524 : if (lexer.peek_token (i)->get_id () != s[i])
7006 : : break;
7007 : 94254 : if (i == s.size ())
7008 : : {
7009 : 3 : rust_error_at (lexer.peek_token ()->get_locus (),
7010 : : "cannot pass %<self%> by raw pointer");
7011 : 3 : return tl::make_unexpected (ParseSelfError::SELF_PTR);
7012 : : }
7013 : : }
7014 : :
7015 : : // Trying to find those patterns:
7016 : : //
7017 : : // &'lifetime mut self
7018 : : // &'lifetime self
7019 : : // & mut self
7020 : : // & self
7021 : : // mut self
7022 : : // self
7023 : : //
7024 : : // If not found, it is probably a function, exit and let function parsing
7025 : : // handle it.
7026 : : bool is_self = false;
7027 : 188496 : for (size_t i = 0; i < 5; i++)
7028 : 314160 : if (lexer.peek_token (i)->get_id () == SELF)
7029 : 15393 : is_self = true;
7030 : :
7031 : 31416 : if (!is_self)
7032 : 16026 : return tl::make_unexpected (ParseSelfError::NOT_SELF);
7033 : :
7034 : : // test if self is a reference parameter
7035 : 30780 : if (lexer.peek_token ()->get_id () == AMP)
7036 : : {
7037 : 8339 : has_reference = true;
7038 : 8339 : lexer.skip_token ();
7039 : :
7040 : : // now test whether it has a lifetime
7041 : 16678 : if (lexer.peek_token ()->get_id () == LIFETIME)
7042 : : {
7043 : : // something went wrong somehow
7044 : 86 : if (auto parsed_lifetime = parse_lifetime (true))
7045 : : {
7046 : 43 : lifetime = parsed_lifetime.value ();
7047 : : }
7048 : : else
7049 : : {
7050 : 0 : Error error (lexer.peek_token ()->get_locus (),
7051 : : "failed to parse lifetime in self param");
7052 : 0 : add_error (std::move (error));
7053 : :
7054 : : // skip after somewhere?
7055 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7056 : 0 : }
7057 : : }
7058 : : }
7059 : :
7060 : : // test for mut
7061 : 15390 : bool has_mut = false;
7062 : 30780 : if (lexer.peek_token ()->get_id () == MUT)
7063 : : {
7064 : 2159 : has_mut = true;
7065 : 2159 : lexer.skip_token ();
7066 : : }
7067 : :
7068 : : // skip self token
7069 : 15390 : const_TokenPtr self_tok = lexer.peek_token ();
7070 : 15390 : if (self_tok->get_id () != SELF)
7071 : : {
7072 : : // skip after somewhere?
7073 : 2 : return tl::make_unexpected (ParseSelfError::NOT_SELF);
7074 : : }
7075 : 15388 : lexer.skip_token ();
7076 : :
7077 : : // parse optional type
7078 : 15388 : std::unique_ptr<AST::Type> type = nullptr;
7079 : 30776 : if (lexer.peek_token ()->get_id () == COLON)
7080 : : {
7081 : 14 : lexer.skip_token ();
7082 : :
7083 : : // type is now required
7084 : 14 : type = parse_type ();
7085 : 14 : if (type == nullptr)
7086 : : {
7087 : 0 : Error error (lexer.peek_token ()->get_locus (),
7088 : : "could not parse type in self param");
7089 : 0 : add_error (std::move (error));
7090 : :
7091 : : // skip after somewhere?
7092 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7093 : 0 : }
7094 : : }
7095 : :
7096 : : // ensure that cannot have both type and reference
7097 : 15388 : if (type != nullptr && has_reference)
7098 : : {
7099 : 0 : Error error (
7100 : 0 : lexer.peek_token ()->get_locus (),
7101 : : "cannot have both a reference and a type specified in a self param");
7102 : 0 : add_error (std::move (error));
7103 : :
7104 : : // skip after somewhere?
7105 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7106 : 0 : }
7107 : :
7108 : 15388 : if (has_reference)
7109 : : {
7110 : 8339 : return std::make_unique<AST::SelfParam> (std::move (lifetime), has_mut,
7111 : 8339 : locus);
7112 : : }
7113 : : else
7114 : : {
7115 : : // note that type may be nullptr here and that's fine
7116 : 7049 : return std::make_unique<AST::SelfParam> (std::move (type), has_mut,
7117 : 7049 : locus);
7118 : : }
7119 : 46807 : }
7120 : :
7121 : : /* Parses an expression or macro statement. */
7122 : : template <typename ManagedTokenSource>
7123 : : std::unique_ptr<AST::Stmt>
7124 : 483 : Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
7125 : : ParseRestrictions restrictions)
7126 : : {
7127 : 483 : location_t locus = lexer.peek_token ()->get_locus ();
7128 : :
7129 : 483 : std::unique_ptr<AST::Expr> expr;
7130 : :
7131 : 966 : switch (lexer.peek_token ()->get_id ())
7132 : : {
7133 : 244 : case IDENTIFIER:
7134 : : case CRATE:
7135 : : case SUPER:
7136 : : case SELF:
7137 : : case SELF_ALIAS:
7138 : : case DOLLAR_SIGN:
7139 : : case SCOPE_RESOLUTION:
7140 : : {
7141 : 244 : AST::PathInExpression path = parse_path_in_expression ();
7142 : 244 : std::unique_ptr<AST::Expr> null_denotation;
7143 : :
7144 : 488 : if (lexer.peek_token ()->get_id () == EXCLAM)
7145 : : {
7146 : 61 : std::unique_ptr<AST::MacroInvocation> invoc
7147 : 122 : = parse_macro_invocation_partial (std::move (path),
7148 : : std::move (outer_attrs));
7149 : :
7150 : 61 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
7151 : : {
7152 : 59 : invoc->add_semicolon ();
7153 : : // Macro invocation with semicolon.
7154 : 59 : return invoc;
7155 : : }
7156 : :
7157 : 2 : TokenId after_macro = lexer.peek_token ()->get_id ();
7158 : :
7159 : 2 : if (restrictions.allow_close_after_expr_stmt
7160 : 2 : && (after_macro == RIGHT_PAREN || after_macro == RIGHT_CURLY
7161 : : || after_macro == RIGHT_SQUARE))
7162 : 2 : return invoc;
7163 : :
7164 : 0 : if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
7165 : : == AST::CURLY
7166 : 0 : && after_macro != DOT && after_macro != QUESTION_MARK)
7167 : : {
7168 : 0 : rust_debug ("braced macro statement");
7169 : 0 : return invoc;
7170 : : }
7171 : :
7172 : 0 : null_denotation = std::move (invoc);
7173 : 61 : }
7174 : : else
7175 : : {
7176 : : null_denotation
7177 : 183 : = null_denotation_path (std::move (path), {}, restrictions);
7178 : : }
7179 : :
7180 : 183 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
7181 : : std::move (outer_attrs), restrictions);
7182 : : break;
7183 : 244 : }
7184 : 239 : default:
7185 : 239 : restrictions.expr_can_be_stmt = true;
7186 : 239 : expr = parse_expr (std::move (outer_attrs), restrictions);
7187 : 239 : break;
7188 : : }
7189 : :
7190 : 422 : if (expr == nullptr)
7191 : : {
7192 : : // expr is required, error
7193 : 0 : Error error (lexer.peek_token ()->get_locus (),
7194 : : "failed to parse expr in expr statement");
7195 : 0 : add_error (std::move (error));
7196 : :
7197 : 0 : skip_after_semicolon ();
7198 : 0 : return nullptr;
7199 : 0 : }
7200 : :
7201 : 422 : bool has_semi = false;
7202 : :
7203 : 422 : if (restrictions.consume_semi)
7204 : : {
7205 : 408 : if (maybe_skip_token (SEMICOLON))
7206 : : {
7207 : : has_semi = true;
7208 : : }
7209 : 266 : else if (expr->is_expr_without_block ())
7210 : : {
7211 : 30 : if (restrictions.allow_close_after_expr_stmt)
7212 : : {
7213 : 30 : TokenId id = lexer.peek_token ()->get_id ();
7214 : 30 : if (id != RIGHT_PAREN && id != RIGHT_CURLY && id != RIGHT_SQUARE)
7215 : : {
7216 : 3 : expect_token (SEMICOLON);
7217 : 3 : return nullptr;
7218 : : }
7219 : : }
7220 : : else
7221 : : {
7222 : 0 : expect_token (SEMICOLON);
7223 : 0 : return nullptr;
7224 : : }
7225 : : }
7226 : : }
7227 : :
7228 : 419 : return std::unique_ptr<AST::ExprStmt> (
7229 : 419 : new AST::ExprStmt (std::move (expr), locus, has_semi));
7230 : 483 : }
7231 : :
7232 : : // Parses a block expression, including the curly braces at start and end.
7233 : : template <typename ManagedTokenSource>
7234 : : std::unique_ptr<AST::BlockExpr>
7235 : 39925 : Parser<ManagedTokenSource>::parse_block_expr (
7236 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
7237 : : location_t pratt_parsed_loc)
7238 : : {
7239 : 39925 : location_t locus = pratt_parsed_loc;
7240 : 39925 : if (locus == UNKNOWN_LOCATION)
7241 : : {
7242 : 37336 : locus = lexer.peek_token ()->get_locus ();
7243 : 37336 : if (!skip_token (LEFT_CURLY))
7244 : : {
7245 : 0 : skip_after_end_block ();
7246 : 0 : return nullptr;
7247 : : }
7248 : : }
7249 : :
7250 : 39925 : AST::AttrVec inner_attrs = parse_inner_attributes ();
7251 : :
7252 : : // parse statements and expression
7253 : 39925 : std::vector<std::unique_ptr<AST::Stmt>> stmts;
7254 : 39925 : std::unique_ptr<AST::Expr> expr = nullptr;
7255 : :
7256 : 39925 : const_TokenPtr t = lexer.peek_token ();
7257 : 110316 : while (t->get_id () != RIGHT_CURLY)
7258 : : {
7259 : 70391 : ExprOrStmt expr_or_stmt = parse_stmt_or_expr ();
7260 : 70391 : if (expr_or_stmt.is_error ())
7261 : : {
7262 : 30 : Error error (
7263 : : t->get_locus (),
7264 : : "failed to parse statement or expression in block expression");
7265 : 30 : add_error (std::move (error));
7266 : :
7267 : 30 : return nullptr;
7268 : 30 : }
7269 : :
7270 : 70361 : t = lexer.peek_token ();
7271 : :
7272 : 70361 : if (expr_or_stmt.stmt != nullptr)
7273 : : {
7274 : 40150 : stmts.push_back (std::move (expr_or_stmt.stmt));
7275 : : }
7276 : : else
7277 : : {
7278 : : // assign to expression and end parsing inside
7279 : 30211 : expr = std::move (expr_or_stmt.expr);
7280 : : break;
7281 : : }
7282 : : }
7283 : :
7284 : 39895 : location_t end_locus = t->get_locus ();
7285 : :
7286 : 39895 : if (!skip_token (RIGHT_CURLY))
7287 : : {
7288 : 0 : Error error (t->get_locus (),
7289 : : "error may be from having an expression (as opposed to "
7290 : : "statement) in the body of the function but not last");
7291 : 0 : add_error (std::move (error));
7292 : :
7293 : 0 : skip_after_end_block ();
7294 : 0 : return nullptr;
7295 : 0 : }
7296 : :
7297 : : // grammar allows for empty block expressions
7298 : :
7299 : 39895 : stmts.shrink_to_fit ();
7300 : :
7301 : : return std::unique_ptr<AST::BlockExpr> (
7302 : 39895 : new AST::BlockExpr (std::move (stmts), std::move (expr),
7303 : : std::move (inner_attrs), std::move (outer_attrs),
7304 : 39895 : std::move (label), locus, end_locus));
7305 : 39925 : }
7306 : :
7307 : : /* Parse an anonymous const expression. This can be a regular const expression
7308 : : * or an underscore for deferred const inference */
7309 : : template <typename ManagedTokenSource>
7310 : : tl::expected<AST::AnonConst, AnonConstError>
7311 : 999 : Parser<ManagedTokenSource>::parse_anon_const ()
7312 : : {
7313 : 999 : auto current = lexer.peek_token ();
7314 : 999 : auto locus = current->get_locus ();
7315 : :
7316 : : // Special case deferred inference constants
7317 : 999 : if (maybe_skip_token (UNDERSCORE))
7318 : 11 : return AST::AnonConst (locus);
7319 : :
7320 : 988 : auto expr = parse_expr ();
7321 : :
7322 : 988 : if (!expr)
7323 : 0 : return tl::make_unexpected (AnonConstError::InvalidSizeExpr);
7324 : :
7325 : 1976 : return AST::AnonConst (std::move (expr), locus);
7326 : 988 : }
7327 : :
7328 : : /* Parse a "const block", a block preceded by the `const` keyword whose
7329 : : * statements can be const evaluated and used in constant contexts */
7330 : : template <typename ManagedTokenSource>
7331 : : std::unique_ptr<AST::ConstBlock>
7332 : 15 : Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
7333 : : location_t locus)
7334 : : {
7335 : 15 : auto block = parse_block_expr ();
7336 : :
7337 : 15 : if (!block)
7338 : : {
7339 : 0 : add_error (Error (locus, "failed to parse inner block in const block"));
7340 : 0 : skip_after_end_block ();
7341 : :
7342 : 0 : return nullptr;
7343 : : }
7344 : :
7345 : 15 : auto block_locus = block->get_locus ();
7346 : :
7347 : 30 : return std::make_unique<AST::ConstBlock> (AST::AnonConst (std::move (block),
7348 : : block_locus),
7349 : 15 : locus, std::move (outer_attrs));
7350 : 15 : }
7351 : :
7352 : : /* Parses a "grouped" expression (expression in parentheses), used to control
7353 : : * precedence. */
7354 : : template <typename ManagedTokenSource>
7355 : : std::unique_ptr<AST::GroupedExpr>
7356 : 0 : Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
7357 : : {
7358 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
7359 : 0 : skip_token (LEFT_PAREN);
7360 : :
7361 : 0 : AST::AttrVec inner_attrs = parse_inner_attributes ();
7362 : :
7363 : : // parse required expr inside parentheses
7364 : 0 : std::unique_ptr<AST::Expr> expr_in_parens = parse_expr ();
7365 : 0 : if (expr_in_parens == nullptr)
7366 : : {
7367 : : // skip after somewhere?
7368 : : // error?
7369 : 0 : return nullptr;
7370 : : }
7371 : :
7372 : 0 : if (!skip_token (RIGHT_PAREN))
7373 : : {
7374 : : // skip after somewhere?
7375 : 0 : return nullptr;
7376 : : }
7377 : :
7378 : : return std::unique_ptr<AST::GroupedExpr> (
7379 : 0 : new AST::GroupedExpr (std::move (expr_in_parens), std::move (inner_attrs),
7380 : 0 : std::move (outer_attrs), locus));
7381 : 0 : }
7382 : :
7383 : : // Parses a closure expression (closure definition).
7384 : : template <typename ManagedTokenSource>
7385 : : std::unique_ptr<AST::ClosureExpr>
7386 : 0 : Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
7387 : : {
7388 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
7389 : : // detect optional "move"
7390 : 0 : bool has_move = false;
7391 : 0 : if (lexer.peek_token ()->get_id () == MOVE)
7392 : : {
7393 : 0 : lexer.skip_token ();
7394 : 0 : has_move = true;
7395 : : }
7396 : :
7397 : : // handle parameter list
7398 : 0 : std::vector<AST::ClosureParam> params;
7399 : :
7400 : 0 : const_TokenPtr t = lexer.peek_token ();
7401 : 0 : switch (t->get_id ())
7402 : : {
7403 : 0 : case OR:
7404 : : // skip token, no parameters
7405 : 0 : lexer.skip_token ();
7406 : : break;
7407 : 0 : case PIPE:
7408 : : // actually may have parameters
7409 : 0 : lexer.skip_token ();
7410 : 0 : t = lexer.peek_token ();
7411 : :
7412 : 0 : while (t->get_id () != PIPE)
7413 : : {
7414 : 0 : AST::ClosureParam param = parse_closure_param ();
7415 : 0 : if (param.is_error ())
7416 : : {
7417 : : // TODO is this really an error?
7418 : 0 : Error error (t->get_locus (), "could not parse closure param");
7419 : 0 : add_error (std::move (error));
7420 : :
7421 : : break;
7422 : 0 : }
7423 : 0 : params.push_back (std::move (param));
7424 : :
7425 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
7426 : : {
7427 : 0 : lexer.skip_token ();
7428 : : // not an error but means param list is done
7429 : : break;
7430 : : }
7431 : : // skip comma
7432 : 0 : lexer.skip_token ();
7433 : :
7434 : 0 : t = lexer.peek_token ();
7435 : : }
7436 : 0 : params.shrink_to_fit ();
7437 : : break;
7438 : 0 : default:
7439 : 0 : add_error (Error (t->get_locus (),
7440 : : "unexpected token %qs in closure expression - expected "
7441 : : "%<|%> or %<||%>",
7442 : : t->get_token_description ()));
7443 : :
7444 : : // skip somewhere?
7445 : 0 : return nullptr;
7446 : : }
7447 : :
7448 : : // again branch based on next token
7449 : 0 : t = lexer.peek_token ();
7450 : 0 : if (t->get_id () == RETURN_TYPE)
7451 : : {
7452 : : // must be return type closure with block expr
7453 : :
7454 : : // skip "return type" token
7455 : 0 : lexer.skip_token ();
7456 : :
7457 : : // parse actual type, which is required
7458 : 0 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
7459 : 0 : if (type == nullptr)
7460 : : {
7461 : : // error
7462 : 0 : Error error (t->get_locus (), "failed to parse type for closure");
7463 : 0 : add_error (std::move (error));
7464 : :
7465 : : // skip somewhere?
7466 : 0 : return nullptr;
7467 : 0 : }
7468 : :
7469 : : // parse block expr, which is required
7470 : 0 : std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
7471 : 0 : if (block == nullptr)
7472 : : {
7473 : : // error
7474 : 0 : Error error (lexer.peek_token ()->get_locus (),
7475 : : "failed to parse block expr in closure");
7476 : 0 : add_error (std::move (error));
7477 : :
7478 : : // skip somewhere?
7479 : 0 : return nullptr;
7480 : 0 : }
7481 : :
7482 : 0 : return std::unique_ptr<AST::ClosureExprInnerTyped> (
7483 : 0 : new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
7484 : : std::move (params), locus, has_move,
7485 : 0 : std::move (outer_attrs)));
7486 : 0 : }
7487 : : else
7488 : : {
7489 : : // must be expr-only closure
7490 : :
7491 : : // parse expr, which is required
7492 : 0 : std::unique_ptr<AST::Expr> expr = parse_expr ();
7493 : 0 : if (expr == nullptr)
7494 : : {
7495 : 0 : Error error (t->get_locus (),
7496 : : "failed to parse expression in closure");
7497 : 0 : add_error (std::move (error));
7498 : :
7499 : : // skip somewhere?
7500 : 0 : return nullptr;
7501 : 0 : }
7502 : :
7503 : 0 : return std::unique_ptr<AST::ClosureExprInner> (
7504 : 0 : new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
7505 : 0 : has_move, std::move (outer_attrs)));
7506 : 0 : }
7507 : 0 : }
7508 : :
7509 : : // Parses a literal token (to literal expression).
7510 : : template <typename ManagedTokenSource>
7511 : : std::unique_ptr<AST::LiteralExpr>
7512 : 3989 : Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
7513 : : {
7514 : : // TODO: change if literal representation in lexer changes
7515 : :
7516 : 3989 : std::string literal_value;
7517 : 3989 : AST::Literal::LitType type = AST::Literal::STRING;
7518 : :
7519 : : // branch based on token
7520 : 3989 : const_TokenPtr t = lexer.peek_token ();
7521 : 3989 : switch (t->get_id ())
7522 : : {
7523 : 2 : case CHAR_LITERAL:
7524 : 2 : type = AST::Literal::CHAR;
7525 : 2 : literal_value = t->get_str ();
7526 : 2 : lexer.skip_token ();
7527 : : break;
7528 : 579 : case STRING_LITERAL:
7529 : 579 : type = AST::Literal::STRING;
7530 : 579 : literal_value = t->get_str ();
7531 : 579 : lexer.skip_token ();
7532 : : break;
7533 : 0 : case BYTE_CHAR_LITERAL:
7534 : 0 : type = AST::Literal::BYTE;
7535 : 0 : literal_value = t->get_str ();
7536 : 0 : lexer.skip_token ();
7537 : : break;
7538 : 1 : case BYTE_STRING_LITERAL:
7539 : 1 : type = AST::Literal::BYTE_STRING;
7540 : 1 : literal_value = t->get_str ();
7541 : 1 : lexer.skip_token ();
7542 : : break;
7543 : 26 : case RAW_STRING_LITERAL:
7544 : 26 : type = AST::Literal::RAW_STRING;
7545 : 26 : literal_value = t->get_str ();
7546 : 26 : lexer.skip_token ();
7547 : : break;
7548 : 3371 : case INT_LITERAL:
7549 : 3371 : type = AST::Literal::INT;
7550 : 3371 : literal_value = t->get_str ();
7551 : 3371 : lexer.skip_token ();
7552 : : break;
7553 : 1 : case FLOAT_LITERAL:
7554 : 1 : type = AST::Literal::FLOAT;
7555 : 1 : literal_value = t->get_str ();
7556 : 1 : lexer.skip_token ();
7557 : : break;
7558 : : // case BOOL_LITERAL
7559 : : // use true and false keywords rather than "bool literal" Rust terminology
7560 : 0 : case TRUE_LITERAL:
7561 : 0 : type = AST::Literal::BOOL;
7562 : 0 : literal_value = Values::Keywords::TRUE_LITERAL;
7563 : 0 : lexer.skip_token ();
7564 : : break;
7565 : 2 : case FALSE_LITERAL:
7566 : 2 : type = AST::Literal::BOOL;
7567 : 2 : literal_value = Values::Keywords::FALSE_LITERAL;
7568 : 2 : lexer.skip_token ();
7569 : : break;
7570 : 7 : default:
7571 : : // error - cannot be a literal expr
7572 : 7 : add_error (Error (t->get_locus (),
7573 : : "unexpected token %qs when parsing literal expression",
7574 : : t->get_token_description ()));
7575 : :
7576 : : // skip?
7577 : 7 : return nullptr;
7578 : : }
7579 : :
7580 : : // create literal based on stuff in switch
7581 : : return std::unique_ptr<AST::LiteralExpr> (
7582 : 4593 : new AST::LiteralExpr (std::move (literal_value), std::move (type),
7583 : : t->get_type_hint (), std::move (outer_attrs),
7584 : 3982 : t->get_locus ()));
7585 : 3989 : }
7586 : :
7587 : : template <typename ManagedTokenSource>
7588 : : std::unique_ptr<AST::BoxExpr>
7589 : 2 : Parser<ManagedTokenSource>::parse_box_expr (AST::AttrVec outer_attrs,
7590 : : location_t pratt_parsed_loc)
7591 : : {
7592 : 2 : location_t locus = pratt_parsed_loc;
7593 : 2 : if (locus == UNKNOWN_LOCATION)
7594 : : {
7595 : 0 : locus = lexer.peek_token ()->get_locus ();
7596 : 0 : skip_token (BOX);
7597 : : }
7598 : :
7599 : 2 : ParseRestrictions restrictions;
7600 : : restrictions.expr_can_be_null = false;
7601 : :
7602 : 2 : std::unique_ptr<AST::Expr> expr = parse_expr (AST::AttrVec (), restrictions);
7603 : :
7604 : : return std::unique_ptr<AST::BoxExpr> (
7605 : 2 : new AST::BoxExpr (std::move (expr), std::move (outer_attrs), locus));
7606 : 2 : }
7607 : :
7608 : : // Parses a return expression (including any expression to return).
7609 : : template <typename ManagedTokenSource>
7610 : : std::unique_ptr<AST::ReturnExpr>
7611 : 896 : Parser<ManagedTokenSource>::parse_return_expr (AST::AttrVec outer_attrs,
7612 : : location_t pratt_parsed_loc)
7613 : : {
7614 : 896 : location_t locus = pratt_parsed_loc;
7615 : 896 : if (locus == UNKNOWN_LOCATION)
7616 : : {
7617 : 0 : locus = lexer.peek_token ()->get_locus ();
7618 : 0 : skip_token (RETURN_KW);
7619 : : }
7620 : :
7621 : : // parse expression to return, if it exists
7622 : 896 : ParseRestrictions restrictions;
7623 : 896 : restrictions.expr_can_be_null = true;
7624 : 896 : std::unique_ptr<AST::Expr> returned_expr
7625 : 896 : = parse_expr (AST::AttrVec (), restrictions);
7626 : :
7627 : : return std::unique_ptr<AST::ReturnExpr> (
7628 : 896 : new AST::ReturnExpr (std::move (returned_expr), std::move (outer_attrs),
7629 : 896 : locus));
7630 : 896 : }
7631 : :
7632 : : // Parses a try expression.
7633 : : template <typename ManagedTokenSource>
7634 : : std::unique_ptr<AST::TryExpr>
7635 : 31 : Parser<ManagedTokenSource>::parse_try_expr (AST::AttrVec outer_attrs,
7636 : : location_t pratt_parsed_loc)
7637 : : {
7638 : 31 : location_t locus = pratt_parsed_loc;
7639 : 31 : if (locus == UNKNOWN_LOCATION)
7640 : : {
7641 : 0 : locus = lexer.peek_token ()->get_locus ();
7642 : 0 : skip_token (TRY);
7643 : : }
7644 : :
7645 : 31 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
7646 : :
7647 : 31 : if (!block_expr)
7648 : : {
7649 : 0 : Error error (lexer.peek_token ()->get_locus (),
7650 : : "failed to parse try block expression");
7651 : 0 : add_error (std::move (error));
7652 : :
7653 : 0 : return nullptr;
7654 : 0 : }
7655 : :
7656 : : return std::unique_ptr<AST::TryExpr> (
7657 : 31 : new AST::TryExpr (std::move (block_expr), std::move (outer_attrs), locus));
7658 : 31 : }
7659 : :
7660 : : /* Parses a break expression (including any label to break to AND any return
7661 : : * expression). */
7662 : : template <typename ManagedTokenSource>
7663 : : std::unique_ptr<AST::BreakExpr>
7664 : 103 : Parser<ManagedTokenSource>::parse_break_expr (AST::AttrVec outer_attrs,
7665 : : location_t pratt_parsed_loc)
7666 : : {
7667 : 103 : location_t locus = pratt_parsed_loc;
7668 : 103 : if (locus == UNKNOWN_LOCATION)
7669 : : {
7670 : 0 : locus = lexer.peek_token ()->get_locus ();
7671 : 0 : skip_token (BREAK);
7672 : : }
7673 : :
7674 : 103 : auto parsed_label = parse_lifetime (false);
7675 : 103 : auto label = (parsed_label)
7676 : 103 : ? tl::optional<AST::Lifetime> (parsed_label.value ())
7677 : : : tl::nullopt;
7678 : :
7679 : : // parse break return expression if it exists
7680 : 103 : ParseRestrictions restrictions;
7681 : 103 : restrictions.expr_can_be_null = true;
7682 : 103 : std::unique_ptr<AST::Expr> return_expr
7683 : 103 : = parse_expr (AST::AttrVec (), restrictions);
7684 : :
7685 : : return std::unique_ptr<AST::BreakExpr> (
7686 : 124 : new AST::BreakExpr (std::move (label), std::move (return_expr),
7687 : 103 : std::move (outer_attrs), locus));
7688 : 124 : }
7689 : :
7690 : : // Parses a continue expression (including any label to continue from).
7691 : : template <typename ManagedTokenSource>
7692 : : std::unique_ptr<AST::ContinueExpr>
7693 : 26 : Parser<ManagedTokenSource>::parse_continue_expr (AST::AttrVec outer_attrs,
7694 : : location_t pratt_parsed_loc)
7695 : : {
7696 : 26 : location_t locus = pratt_parsed_loc;
7697 : 26 : if (locus == UNKNOWN_LOCATION)
7698 : : {
7699 : 0 : locus = lexer.peek_token ()->get_locus ();
7700 : 0 : skip_token (CONTINUE);
7701 : : }
7702 : :
7703 : 26 : auto parsed_label = parse_lifetime (false);
7704 : 26 : auto label = (parsed_label)
7705 : 26 : ? tl::optional<AST::Lifetime> (parsed_label.value ())
7706 : : : tl::nullopt;
7707 : :
7708 : : return std::unique_ptr<AST::ContinueExpr> (
7709 : 33 : new AST::ContinueExpr (std::move (label), std::move (outer_attrs), locus));
7710 : 26 : }
7711 : :
7712 : : // Parses a loop label used in loop expressions.
7713 : : template <typename ManagedTokenSource>
7714 : : tl::expected<AST::LoopLabel, ParseLoopLabelError>
7715 : 36 : Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
7716 : : {
7717 : : // parse lifetime - if doesn't exist, assume no label
7718 : 36 : if (tok->get_id () != LIFETIME)
7719 : : {
7720 : : // not necessarily an error
7721 : : return tl::unexpected<ParseLoopLabelError> (
7722 : 0 : ParseLoopLabelError::NOT_LOOP_LABEL);
7723 : : }
7724 : : /* FIXME: check for named lifetime requirement here? or check in semantic
7725 : : * analysis phase? */
7726 : 36 : AST::Lifetime label = lifetime_from_token (tok);
7727 : :
7728 : 36 : if (!skip_token (COLON))
7729 : : {
7730 : : // skip somewhere?
7731 : : return tl::unexpected<ParseLoopLabelError> (
7732 : 0 : ParseLoopLabelError::MISSING_COLON);
7733 : : }
7734 : :
7735 : : return tl::expected<AST::LoopLabel, ParseLoopLabelError> (
7736 : 36 : AST::LoopLabel (std::move (label), tok->get_locus ()));
7737 : 36 : }
7738 : :
7739 : : /* Parses an if expression of any kind, including with else, else if, else if
7740 : : * let, and neither. Note that any outer attributes will be ignored because if
7741 : : * expressions don't support them. */
7742 : : template <typename ManagedTokenSource>
7743 : : std::unique_ptr<AST::IfExpr>
7744 : 3451 : Parser<ManagedTokenSource>::parse_if_expr (AST::AttrVec outer_attrs,
7745 : : location_t pratt_parsed_loc)
7746 : : {
7747 : : // TODO: make having outer attributes an error?
7748 : 3451 : location_t locus = pratt_parsed_loc;
7749 : 3451 : if (locus == UNKNOWN_LOCATION)
7750 : : {
7751 : 394 : locus = lexer.peek_token ()->get_locus ();
7752 : 394 : if (!skip_token (IF))
7753 : : {
7754 : 0 : skip_after_end_block ();
7755 : 0 : return nullptr;
7756 : : }
7757 : : }
7758 : :
7759 : : // detect accidental if let
7760 : 6902 : if (lexer.peek_token ()->get_id () == LET)
7761 : : {
7762 : 0 : Error error (lexer.peek_token ()->get_locus (),
7763 : : "if let expression probably exists, but is being parsed "
7764 : : "as an if expression. This may be a parser error");
7765 : 0 : add_error (std::move (error));
7766 : :
7767 : : // skip somewhere?
7768 : 0 : return nullptr;
7769 : 0 : }
7770 : :
7771 : : /* parse required condition expr - HACK to prevent struct expr from being
7772 : : * parsed */
7773 : 3451 : ParseRestrictions no_struct_expr;
7774 : 3451 : no_struct_expr.can_be_struct_expr = false;
7775 : 3451 : std::unique_ptr<AST::Expr> condition = parse_expr ({}, no_struct_expr);
7776 : 3451 : if (condition == nullptr)
7777 : : {
7778 : 0 : Error error (lexer.peek_token ()->get_locus (),
7779 : : "failed to parse condition expression in if expression");
7780 : 0 : add_error (std::move (error));
7781 : :
7782 : : // skip somewhere?
7783 : 0 : return nullptr;
7784 : 0 : }
7785 : :
7786 : : // parse required block expr
7787 : 3451 : std::unique_ptr<AST::BlockExpr> if_body = parse_block_expr ();
7788 : 3451 : if (if_body == nullptr)
7789 : : {
7790 : 1 : Error error (lexer.peek_token ()->get_locus (),
7791 : : "failed to parse if body block expression in if expression");
7792 : 1 : add_error (std::move (error));
7793 : :
7794 : : // skip somewhere?
7795 : 1 : return nullptr;
7796 : 1 : }
7797 : :
7798 : : // branch to parse end or else (and then else, else if, or else if let)
7799 : 6900 : if (lexer.peek_token ()->get_id () != ELSE)
7800 : : {
7801 : : // single selection - end of if expression
7802 : : return std::unique_ptr<AST::IfExpr> (
7803 : 1133 : new AST::IfExpr (std::move (condition), std::move (if_body),
7804 : 1133 : std::move (outer_attrs), locus));
7805 : : }
7806 : : else
7807 : : {
7808 : : // double or multiple selection - branch on end, else if, or else if let
7809 : :
7810 : : // skip "else"
7811 : 2317 : lexer.skip_token ();
7812 : :
7813 : : // branch on whether next token is '{' or 'if'
7814 : 2317 : const_TokenPtr t = lexer.peek_token ();
7815 : 2317 : switch (t->get_id ())
7816 : : {
7817 : 1923 : case LEFT_CURLY:
7818 : : {
7819 : : // double selection - else
7820 : : // parse else block expr (required)
7821 : 1923 : std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
7822 : 1923 : if (else_body == nullptr)
7823 : : {
7824 : 0 : Error error (lexer.peek_token ()->get_locus (),
7825 : : "failed to parse else body block expression in "
7826 : : "if expression");
7827 : 0 : add_error (std::move (error));
7828 : :
7829 : : // skip somewhere?
7830 : 0 : return nullptr;
7831 : 0 : }
7832 : :
7833 : 1923 : return std::unique_ptr<AST::IfExprConseqElse> (
7834 : 1923 : new AST::IfExprConseqElse (std::move (condition),
7835 : : std::move (if_body),
7836 : : std::move (else_body),
7837 : 1923 : std::move (outer_attrs), locus));
7838 : 1923 : }
7839 : 394 : case IF:
7840 : : {
7841 : : // multiple selection - else if or else if let
7842 : : // branch on whether next token is 'let' or not
7843 : 788 : if (lexer.peek_token (1)->get_id () == LET)
7844 : : {
7845 : : // parse if let expr (required)
7846 : 1 : std::unique_ptr<AST::IfLetExpr> if_let_expr
7847 : 1 : = parse_if_let_expr ();
7848 : 1 : if (if_let_expr == nullptr)
7849 : : {
7850 : 0 : Error error (lexer.peek_token ()->get_locus (),
7851 : : "failed to parse (else) if let expression "
7852 : : "after if expression");
7853 : 0 : add_error (std::move (error));
7854 : :
7855 : : // skip somewhere?
7856 : 0 : return nullptr;
7857 : 0 : }
7858 : :
7859 : 1 : return std::unique_ptr<AST::IfExprConseqElse> (
7860 : 1 : new AST::IfExprConseqElse (std::move (condition),
7861 : : std::move (if_body),
7862 : : std::move (if_let_expr),
7863 : 1 : std::move (outer_attrs), locus));
7864 : 1 : }
7865 : : else
7866 : : {
7867 : : // parse if expr (required)
7868 : 393 : std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
7869 : 393 : if (if_expr == nullptr)
7870 : : {
7871 : 0 : Error error (lexer.peek_token ()->get_locus (),
7872 : : "failed to parse (else) if expression after "
7873 : : "if expression");
7874 : 0 : add_error (std::move (error));
7875 : :
7876 : : // skip somewhere?
7877 : 0 : return nullptr;
7878 : 0 : }
7879 : :
7880 : 393 : return std::unique_ptr<AST::IfExprConseqElse> (
7881 : 393 : new AST::IfExprConseqElse (std::move (condition),
7882 : : std::move (if_body),
7883 : : std::move (if_expr),
7884 : 393 : std::move (outer_attrs), locus));
7885 : 393 : }
7886 : : }
7887 : 0 : default:
7888 : : // error - invalid token
7889 : 0 : add_error (Error (t->get_locus (),
7890 : : "unexpected token %qs after else in if expression",
7891 : : t->get_token_description ()));
7892 : :
7893 : : // skip somewhere?
7894 : 0 : return nullptr;
7895 : : }
7896 : 2317 : }
7897 : 3451 : }
7898 : :
7899 : : /* Parses an if let expression of any kind, including with else, else if, else
7900 : : * if let, and none. Note that any outer attributes will be ignored as if let
7901 : : * expressions don't support them. */
7902 : : template <typename ManagedTokenSource>
7903 : : std::unique_ptr<AST::IfLetExpr>
7904 : 122 : Parser<ManagedTokenSource>::parse_if_let_expr (AST::AttrVec outer_attrs,
7905 : : location_t pratt_parsed_loc)
7906 : : {
7907 : : // TODO: make having outer attributes an error?
7908 : 122 : location_t locus = pratt_parsed_loc;
7909 : 122 : if (locus == UNKNOWN_LOCATION)
7910 : : {
7911 : 2 : locus = lexer.peek_token ()->get_locus ();
7912 : 2 : if (!skip_token (IF))
7913 : : {
7914 : 0 : skip_after_end_block ();
7915 : 0 : return nullptr;
7916 : : }
7917 : : }
7918 : :
7919 : : // detect accidental if expr parsed as if let expr
7920 : 244 : if (lexer.peek_token ()->get_id () != LET)
7921 : : {
7922 : 0 : Error error (lexer.peek_token ()->get_locus (),
7923 : : "if expression probably exists, but is being parsed as an "
7924 : : "if let expression. This may be a parser error");
7925 : 0 : add_error (std::move (error));
7926 : :
7927 : : // skip somewhere?
7928 : 0 : return nullptr;
7929 : 0 : }
7930 : 122 : lexer.skip_token ();
7931 : :
7932 : : // parse match arm patterns (which are required)
7933 : 122 : std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
7934 : : = parse_match_arm_patterns (EQUAL);
7935 : 122 : if (match_arm_patterns.empty ())
7936 : : {
7937 : 0 : Error error (
7938 : 0 : lexer.peek_token ()->get_locus (),
7939 : : "failed to parse any match arm patterns in if let expression");
7940 : 0 : add_error (std::move (error));
7941 : :
7942 : : // skip somewhere?
7943 : 0 : return nullptr;
7944 : 0 : }
7945 : :
7946 : 122 : if (!skip_token (EQUAL))
7947 : : {
7948 : : // skip somewhere?
7949 : 0 : return nullptr;
7950 : : }
7951 : :
7952 : : // parse expression (required) - HACK to prevent struct expr being parsed
7953 : 122 : ParseRestrictions no_struct_expr;
7954 : 122 : no_struct_expr.can_be_struct_expr = false;
7955 : 122 : std::unique_ptr<AST::Expr> scrutinee_expr = parse_expr ({}, no_struct_expr);
7956 : 122 : if (scrutinee_expr == nullptr)
7957 : : {
7958 : 0 : Error error (lexer.peek_token ()->get_locus (),
7959 : : "failed to parse scrutinee expression in if let expression");
7960 : 0 : add_error (std::move (error));
7961 : :
7962 : : // skip somewhere?
7963 : 0 : return nullptr;
7964 : 0 : }
7965 : : /* TODO: check for expression not being a struct expression or lazy boolean
7966 : : * expression here? or actually probably in semantic analysis. */
7967 : :
7968 : : // parse block expression (required)
7969 : 122 : std::unique_ptr<AST::BlockExpr> if_let_body = parse_block_expr ();
7970 : 122 : if (if_let_body == nullptr)
7971 : : {
7972 : 0 : Error error (
7973 : 0 : lexer.peek_token ()->get_locus (),
7974 : : "failed to parse if let body block expression in if let expression");
7975 : 0 : add_error (std::move (error));
7976 : :
7977 : : // skip somewhere?
7978 : 0 : return nullptr;
7979 : 0 : }
7980 : :
7981 : : // branch to parse end or else (and then else, else if, or else if let)
7982 : 244 : if (lexer.peek_token ()->get_id () != ELSE)
7983 : : {
7984 : : // single selection - end of if let expression
7985 : : return std::unique_ptr<AST::IfLetExpr> (
7986 : 80 : new AST::IfLetExpr (std::move (match_arm_patterns),
7987 : : std::move (scrutinee_expr), std::move (if_let_body),
7988 : 80 : std::move (outer_attrs), locus));
7989 : : }
7990 : : else
7991 : : {
7992 : : // double or multiple selection - branch on end, else if, or else if let
7993 : :
7994 : : // skip "else"
7995 : 42 : lexer.skip_token ();
7996 : :
7997 : : // branch on whether next token is '{' or 'if'
7998 : 42 : const_TokenPtr t = lexer.peek_token ();
7999 : 42 : switch (t->get_id ())
8000 : : {
8001 : 40 : case LEFT_CURLY:
8002 : : {
8003 : : // double selection - else
8004 : : // parse else block expr (required)
8005 : 40 : std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
8006 : 40 : if (else_body == nullptr)
8007 : : {
8008 : 0 : Error error (lexer.peek_token ()->get_locus (),
8009 : : "failed to parse else body block expression in "
8010 : : "if let expression");
8011 : 0 : add_error (std::move (error));
8012 : :
8013 : : // skip somewhere?
8014 : 0 : return nullptr;
8015 : 0 : }
8016 : :
8017 : 40 : return std::unique_ptr<AST::IfLetExprConseqElse> (
8018 : 40 : new AST::IfLetExprConseqElse (std::move (match_arm_patterns),
8019 : : std::move (scrutinee_expr),
8020 : : std::move (if_let_body),
8021 : : std::move (else_body),
8022 : 40 : std::move (outer_attrs), locus));
8023 : 40 : }
8024 : 2 : case IF:
8025 : : {
8026 : : // multiple selection - else if or else if let
8027 : : // branch on whether next token is 'let' or not
8028 : 4 : if (lexer.peek_token (1)->get_id () == LET)
8029 : : {
8030 : : // parse if let expr (required)
8031 : 1 : std::unique_ptr<AST::IfLetExpr> if_let_expr
8032 : 1 : = parse_if_let_expr ();
8033 : 1 : if (if_let_expr == nullptr)
8034 : : {
8035 : 0 : Error error (lexer.peek_token ()->get_locus (),
8036 : : "failed to parse (else) if let expression "
8037 : : "after if let expression");
8038 : 0 : add_error (std::move (error));
8039 : :
8040 : : // skip somewhere?
8041 : 0 : return nullptr;
8042 : 0 : }
8043 : :
8044 : 1 : return std::unique_ptr<AST::IfLetExprConseqElse> (
8045 : 1 : new AST::IfLetExprConseqElse (
8046 : : std::move (match_arm_patterns), std::move (scrutinee_expr),
8047 : : std::move (if_let_body), std::move (if_let_expr),
8048 : 1 : std::move (outer_attrs), locus));
8049 : 1 : }
8050 : : else
8051 : : {
8052 : : // parse if expr (required)
8053 : 1 : std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
8054 : 1 : if (if_expr == nullptr)
8055 : : {
8056 : 0 : Error error (lexer.peek_token ()->get_locus (),
8057 : : "failed to parse (else) if expression after "
8058 : : "if let expression");
8059 : 0 : add_error (std::move (error));
8060 : :
8061 : : // skip somewhere?
8062 : 0 : return nullptr;
8063 : 0 : }
8064 : :
8065 : 1 : return std::unique_ptr<AST::IfLetExprConseqElse> (
8066 : 1 : new AST::IfLetExprConseqElse (
8067 : : std::move (match_arm_patterns), std::move (scrutinee_expr),
8068 : : std::move (if_let_body), std::move (if_expr),
8069 : 1 : std::move (outer_attrs), locus));
8070 : 1 : }
8071 : : }
8072 : 0 : default:
8073 : : // error - invalid token
8074 : 0 : add_error (
8075 : 0 : Error (t->get_locus (),
8076 : : "unexpected token %qs after else in if let expression",
8077 : : t->get_token_description ()));
8078 : :
8079 : : // skip somewhere?
8080 : 0 : return nullptr;
8081 : : }
8082 : 42 : }
8083 : 122 : }
8084 : :
8085 : : /* TODO: possibly decide on different method of handling label (i.e. not
8086 : : * parameter) */
8087 : :
8088 : : /* Parses a "loop" infinite loop expression. Label is not parsed and should be
8089 : : * parsed via parse_labelled_loop_expr, which would call this. */
8090 : : template <typename ManagedTokenSource>
8091 : : std::unique_ptr<AST::LoopExpr>
8092 : 139 : Parser<ManagedTokenSource>::parse_loop_expr (AST::AttrVec outer_attrs,
8093 : : tl::optional<AST::LoopLabel> label,
8094 : : location_t pratt_parsed_loc)
8095 : : {
8096 : 139 : location_t locus = pratt_parsed_loc;
8097 : 139 : if (locus == UNKNOWN_LOCATION)
8098 : : {
8099 : 34 : if (label)
8100 : 34 : locus = label->get_locus ();
8101 : : else
8102 : 0 : locus = lexer.peek_token ()->get_locus ();
8103 : :
8104 : 34 : if (!skip_token (LOOP))
8105 : : {
8106 : 0 : skip_after_end_block ();
8107 : 0 : return nullptr;
8108 : : }
8109 : : }
8110 : : else
8111 : : {
8112 : 105 : if (label)
8113 : 0 : locus = label->get_locus ();
8114 : : }
8115 : :
8116 : : // parse loop body, which is required
8117 : 139 : std::unique_ptr<AST::BlockExpr> loop_body = parse_block_expr ();
8118 : 139 : if (loop_body == nullptr)
8119 : : {
8120 : 1 : Error error (lexer.peek_token ()->get_locus (),
8121 : : "could not parse loop body in (infinite) loop expression");
8122 : 1 : add_error (std::move (error));
8123 : :
8124 : 1 : return nullptr;
8125 : 1 : }
8126 : :
8127 : : return std::unique_ptr<AST::LoopExpr> (
8128 : 172 : new AST::LoopExpr (std::move (loop_body), locus, std::move (label),
8129 : 138 : std::move (outer_attrs)));
8130 : 139 : }
8131 : :
8132 : : /* Parses a "while" loop expression. Label is not parsed and should be parsed
8133 : : * via parse_labelled_loop_expr, which would call this. */
8134 : : template <typename ManagedTokenSource>
8135 : : std::unique_ptr<AST::WhileLoopExpr>
8136 : 173 : Parser<ManagedTokenSource>::parse_while_loop_expr (
8137 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
8138 : : location_t pratt_parsed_loc)
8139 : : {
8140 : 173 : location_t locus = pratt_parsed_loc;
8141 : 173 : if (locus == UNKNOWN_LOCATION)
8142 : : {
8143 : 2 : if (label)
8144 : 2 : locus = label->get_locus ();
8145 : : else
8146 : 0 : locus = lexer.peek_token ()->get_locus ();
8147 : :
8148 : 2 : if (!skip_token (WHILE))
8149 : : {
8150 : 0 : skip_after_end_block ();
8151 : 0 : return nullptr;
8152 : : }
8153 : : }
8154 : : else
8155 : : {
8156 : 171 : if (label)
8157 : 0 : locus = label->get_locus ();
8158 : : }
8159 : :
8160 : : // ensure it isn't a while let loop
8161 : 346 : if (lexer.peek_token ()->get_id () == LET)
8162 : : {
8163 : 0 : Error error (lexer.peek_token ()->get_locus (),
8164 : : "appears to be while let loop but is being parsed by "
8165 : : "while loop - this may be a compiler issue");
8166 : 0 : add_error (std::move (error));
8167 : :
8168 : : // skip somewhere?
8169 : 0 : return nullptr;
8170 : 0 : }
8171 : :
8172 : : // parse loop predicate (required) with HACK to prevent struct expr parsing
8173 : 173 : ParseRestrictions no_struct_expr;
8174 : 173 : no_struct_expr.can_be_struct_expr = false;
8175 : 173 : std::unique_ptr<AST::Expr> predicate = parse_expr ({}, no_struct_expr);
8176 : 173 : if (predicate == nullptr)
8177 : : {
8178 : 0 : Error error (lexer.peek_token ()->get_locus (),
8179 : : "failed to parse predicate expression in while loop");
8180 : 0 : add_error (std::move (error));
8181 : :
8182 : : // skip somewhere?
8183 : 0 : return nullptr;
8184 : 0 : }
8185 : : /* TODO: check that it isn't struct expression here? actually, probably in
8186 : : * semantic analysis */
8187 : :
8188 : : // parse loop body (required)
8189 : 173 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8190 : 173 : if (body == nullptr)
8191 : : {
8192 : 0 : Error error (lexer.peek_token ()->get_locus (),
8193 : : "failed to parse loop body block expression in while loop");
8194 : 0 : add_error (std::move (error));
8195 : :
8196 : : // skip somewhere
8197 : 0 : return nullptr;
8198 : 0 : }
8199 : :
8200 : : return std::unique_ptr<AST::WhileLoopExpr> (
8201 : 175 : new AST::WhileLoopExpr (std::move (predicate), std::move (body), locus,
8202 : 173 : std::move (label), std::move (outer_attrs)));
8203 : 173 : }
8204 : :
8205 : : /* Parses a "while let" loop expression. Label is not parsed and should be
8206 : : * parsed via parse_labelled_loop_expr, which would call this. */
8207 : : template <typename ManagedTokenSource>
8208 : : std::unique_ptr<AST::WhileLetLoopExpr>
8209 : 29 : Parser<ManagedTokenSource>::parse_while_let_loop_expr (
8210 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
8211 : : {
8212 : 29 : location_t locus = UNKNOWN_LOCATION;
8213 : 29 : if (label)
8214 : 0 : locus = label->get_locus ();
8215 : : else
8216 : 58 : locus = lexer.peek_token ()->get_locus ();
8217 : 29 : maybe_skip_token (WHILE);
8218 : :
8219 : : /* check for possible accidental recognition of a while loop as a while let
8220 : : * loop */
8221 : 58 : if (lexer.peek_token ()->get_id () != LET)
8222 : : {
8223 : 0 : Error error (lexer.peek_token ()->get_locus (),
8224 : : "appears to be a while loop but is being parsed by "
8225 : : "while let loop - this may be a compiler issue");
8226 : 0 : add_error (std::move (error));
8227 : :
8228 : : // skip somewhere
8229 : 0 : return nullptr;
8230 : 0 : }
8231 : : // as this token is definitely let now, save the computation of comparison
8232 : 29 : lexer.skip_token ();
8233 : :
8234 : : // parse predicate patterns
8235 : 29 : std::vector<std::unique_ptr<AST::Pattern>> predicate_patterns
8236 : : = parse_match_arm_patterns (EQUAL);
8237 : : // ensure that there is at least 1 pattern
8238 : 29 : if (predicate_patterns.empty ())
8239 : : {
8240 : 1 : Error error (lexer.peek_token ()->get_locus (),
8241 : : "should be at least 1 pattern");
8242 : 1 : add_error (std::move (error));
8243 : 1 : return nullptr;
8244 : 1 : }
8245 : :
8246 : 28 : if (!skip_token (EQUAL))
8247 : : {
8248 : : // skip somewhere?
8249 : 0 : return nullptr;
8250 : : }
8251 : :
8252 : : /* parse predicate expression, which is required (and HACK to prevent struct
8253 : : * expr) */
8254 : 28 : ParseRestrictions no_struct_expr;
8255 : 28 : no_struct_expr.can_be_struct_expr = false;
8256 : 28 : std::unique_ptr<AST::Expr> predicate_expr = parse_expr ({}, no_struct_expr);
8257 : 28 : if (predicate_expr == nullptr)
8258 : : {
8259 : 0 : Error error (lexer.peek_token ()->get_locus (),
8260 : : "failed to parse predicate expression in while let loop");
8261 : 0 : add_error (std::move (error));
8262 : :
8263 : : // skip somewhere?
8264 : 0 : return nullptr;
8265 : 0 : }
8266 : : /* TODO: ensure that struct expression is not parsed? Actually, probably in
8267 : : * semantic analysis. */
8268 : :
8269 : : // parse loop body, which is required
8270 : 28 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8271 : 28 : if (body == nullptr)
8272 : : {
8273 : 0 : Error error (lexer.peek_token ()->get_locus (),
8274 : : "failed to parse block expr (loop body) of while let loop");
8275 : 0 : add_error (std::move (error));
8276 : :
8277 : : // skip somewhere?
8278 : 0 : return nullptr;
8279 : 0 : }
8280 : :
8281 : 28 : return std::unique_ptr<AST::WhileLetLoopExpr> (new AST::WhileLetLoopExpr (
8282 : : std::move (predicate_patterns), std::move (predicate_expr),
8283 : 28 : std::move (body), locus, std::move (label), std::move (outer_attrs)));
8284 : 29 : }
8285 : :
8286 : : /* Parses a "for" iterative loop. Label is not parsed and should be parsed via
8287 : : * parse_labelled_loop_expr, which would call this. */
8288 : : template <typename ManagedTokenSource>
8289 : : std::unique_ptr<AST::ForLoopExpr>
8290 : 221 : Parser<ManagedTokenSource>::parse_for_loop_expr (
8291 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
8292 : : {
8293 : 221 : location_t locus = UNKNOWN_LOCATION;
8294 : 221 : if (label)
8295 : 0 : locus = label->get_locus ();
8296 : : else
8297 : 442 : locus = lexer.peek_token ()->get_locus ();
8298 : 221 : maybe_skip_token (FOR);
8299 : :
8300 : : // parse pattern, which is required
8301 : 221 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
8302 : 221 : if (pattern == nullptr)
8303 : : {
8304 : 0 : Error error (lexer.peek_token ()->get_locus (),
8305 : : "failed to parse iterator pattern in for loop");
8306 : 0 : add_error (std::move (error));
8307 : :
8308 : : // skip somewhere?
8309 : 0 : return nullptr;
8310 : 0 : }
8311 : :
8312 : 221 : if (!skip_token (IN))
8313 : : {
8314 : : // skip somewhere?
8315 : 0 : return nullptr;
8316 : : }
8317 : :
8318 : : /* parse iterator expression, which is required - also HACK to prevent
8319 : : * struct expr */
8320 : 221 : ParseRestrictions no_struct_expr;
8321 : 221 : no_struct_expr.can_be_struct_expr = false;
8322 : 221 : std::unique_ptr<AST::Expr> expr = parse_expr ({}, no_struct_expr);
8323 : 221 : if (expr == nullptr)
8324 : : {
8325 : 0 : Error error (lexer.peek_token ()->get_locus (),
8326 : : "failed to parse iterator expression in for loop");
8327 : 0 : add_error (std::move (error));
8328 : :
8329 : : // skip somewhere?
8330 : 0 : return nullptr;
8331 : 0 : }
8332 : : // TODO: check to ensure this isn't struct expr? Or in semantic analysis.
8333 : :
8334 : : // parse loop body, which is required
8335 : 221 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8336 : 221 : if (body == nullptr)
8337 : : {
8338 : 0 : Error error (lexer.peek_token ()->get_locus (),
8339 : : "failed to parse loop body block expression in for loop");
8340 : 0 : add_error (std::move (error));
8341 : :
8342 : : // skip somewhere?
8343 : 0 : return nullptr;
8344 : 0 : }
8345 : :
8346 : : return std::unique_ptr<AST::ForLoopExpr> (
8347 : 221 : new AST::ForLoopExpr (std::move (pattern), std::move (expr),
8348 : : std::move (body), locus, std::move (label),
8349 : 221 : std::move (outer_attrs)));
8350 : 221 : }
8351 : :
8352 : : // Parses a loop expression with label (any kind of loop - disambiguates).
8353 : : template <typename ManagedTokenSource>
8354 : : std::unique_ptr<AST::Expr>
8355 : 36 : Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
8356 : : AST::AttrVec outer_attrs)
8357 : : {
8358 : : /* TODO: decide whether it should not work if there is no label, or parse it
8359 : : * with no label at the moment, I will make it not work with no label
8360 : : * because that's the implication. */
8361 : :
8362 : 36 : if (tok->get_id () != LIFETIME)
8363 : : {
8364 : 0 : Error error (tok->get_locus (),
8365 : : "expected lifetime in labelled loop expr (to parse loop "
8366 : : "label) - found %qs",
8367 : : tok->get_token_description ());
8368 : 0 : add_error (std::move (error));
8369 : :
8370 : : // skip?
8371 : 0 : return nullptr;
8372 : 0 : }
8373 : :
8374 : : // parse loop label (required)
8375 : : // TODO: Convert this return type to tl::expected instead of tl::optional
8376 : 72 : auto parsed_label = parse_loop_label (tok);
8377 : 36 : if (!parsed_label)
8378 : : {
8379 : 0 : Error error (lexer.peek_token ()->get_locus (),
8380 : : "failed to parse loop label in labelled loop expr");
8381 : 0 : add_error (std::move (error));
8382 : :
8383 : : // skip?
8384 : 0 : return nullptr;
8385 : 0 : }
8386 : :
8387 : 36 : auto label = parsed_label
8388 : : ? tl::optional<AST::LoopLabel> (parsed_label.value ())
8389 : : : tl::nullopt;
8390 : :
8391 : : // branch on next token
8392 : 36 : const_TokenPtr t = lexer.peek_token ();
8393 : 36 : switch (t->get_id ())
8394 : : {
8395 : 34 : case LOOP:
8396 : 68 : return parse_loop_expr (std::move (outer_attrs), std::move (label));
8397 : 0 : case FOR:
8398 : 0 : return parse_for_loop_expr (std::move (outer_attrs), std::move (label));
8399 : 2 : case WHILE:
8400 : : // further disambiguate into while vs while let
8401 : 4 : if (lexer.peek_token (1)->get_id () == LET)
8402 : : {
8403 : 0 : return parse_while_let_loop_expr (std::move (outer_attrs),
8404 : 0 : std::move (label));
8405 : : }
8406 : : else
8407 : : {
8408 : 4 : return parse_while_loop_expr (std::move (outer_attrs),
8409 : 2 : std::move (label));
8410 : : }
8411 : 0 : case LEFT_CURLY:
8412 : 0 : return parse_block_expr (std::move (outer_attrs), std::move (label));
8413 : 0 : default:
8414 : : // error
8415 : 0 : add_error (Error (t->get_locus (),
8416 : : "unexpected token %qs when parsing labelled loop",
8417 : : t->get_token_description ()));
8418 : :
8419 : : // skip?
8420 : 0 : return nullptr;
8421 : : }
8422 : 108 : }
8423 : :
8424 : : // Parses a match expression.
8425 : : template <typename ManagedTokenSource>
8426 : : std::unique_ptr<AST::MatchExpr>
8427 : 5533 : Parser<ManagedTokenSource>::parse_match_expr (AST::AttrVec outer_attrs,
8428 : : location_t pratt_parsed_loc)
8429 : : {
8430 : 5533 : location_t locus = pratt_parsed_loc;
8431 : 5533 : if (locus == UNKNOWN_LOCATION)
8432 : : {
8433 : 0 : locus = lexer.peek_token ()->get_locus ();
8434 : 0 : skip_token (MATCH_KW);
8435 : : }
8436 : :
8437 : : /* parse scrutinee expression, which is required (and HACK to prevent struct
8438 : : * expr) */
8439 : 5533 : ParseRestrictions no_struct_expr;
8440 : 5533 : no_struct_expr.can_be_struct_expr = false;
8441 : 5533 : std::unique_ptr<AST::Expr> scrutinee = parse_expr ({}, no_struct_expr);
8442 : 5533 : if (scrutinee == nullptr)
8443 : : {
8444 : 0 : Error error (lexer.peek_token ()->get_locus (),
8445 : : "failed to parse scrutinee expression in match expression");
8446 : 0 : add_error (std::move (error));
8447 : :
8448 : : // skip somewhere?
8449 : 0 : return nullptr;
8450 : 0 : }
8451 : : /* TODO: check for scrutinee expr not being struct expr? or do so in
8452 : : * semantic analysis */
8453 : :
8454 : 5533 : if (!skip_token (LEFT_CURLY))
8455 : : {
8456 : : // skip somewhere?
8457 : 0 : return nullptr;
8458 : : }
8459 : :
8460 : : // parse inner attributes (if they exist)
8461 : 5533 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8462 : :
8463 : : // parse match arms (if they exist)
8464 : : // std::vector<std::unique_ptr<AST::MatchCase> > match_arms;
8465 : 5533 : std::vector<AST::MatchCase> match_arms;
8466 : :
8467 : : // parse match cases
8468 : 160319 : while (lexer.peek_token ()->get_id () != RIGHT_CURLY)
8469 : : {
8470 : : // parse match arm itself, which is required
8471 : 49875 : AST::MatchArm arm = parse_match_arm ();
8472 : 49875 : if (arm.is_error ())
8473 : : {
8474 : : // TODO is this worth throwing everything away?
8475 : 0 : Error error (lexer.peek_token ()->get_locus (),
8476 : : "failed to parse match arm in match arms");
8477 : 0 : add_error (std::move (error));
8478 : :
8479 : 0 : return nullptr;
8480 : 0 : }
8481 : :
8482 : 49875 : if (!skip_token (MATCH_ARROW))
8483 : : {
8484 : : // skip after somewhere?
8485 : : // TODO is returning here a good idea? or is break better?
8486 : 0 : return nullptr;
8487 : : }
8488 : :
8489 : 49875 : ParseRestrictions restrictions;
8490 : 49875 : restrictions.expr_can_be_stmt = true;
8491 : :
8492 : 49875 : std::unique_ptr<AST::Expr> expr = parse_expr ({}, restrictions);
8493 : :
8494 : 49875 : if (expr == nullptr)
8495 : : {
8496 : 0 : Error error (lexer.peek_token ()->get_locus (),
8497 : : "failed to parse expr in match arm in match expr");
8498 : 0 : add_error (std::move (error));
8499 : :
8500 : : // skip somewhere?
8501 : 0 : return nullptr;
8502 : 0 : }
8503 : :
8504 : 49875 : bool is_expr_without_block = expr->is_expr_without_block ();
8505 : :
8506 : 99750 : match_arms.push_back (AST::MatchCase (std::move (arm), std::move (expr)));
8507 : :
8508 : : // handle comma presence
8509 : 99750 : if (lexer.peek_token ()->get_id () != COMMA)
8510 : : {
8511 : 1006 : if (!is_expr_without_block)
8512 : : {
8513 : : // allowed even if not final case
8514 : : continue;
8515 : : }
8516 : 186 : else if (is_expr_without_block
8517 : 372 : && lexer.peek_token ()->get_id () != RIGHT_CURLY)
8518 : : {
8519 : : // not allowed if not final case
8520 : 1 : Error error (lexer.peek_token ()->get_locus (),
8521 : : "exprwithoutblock requires comma after match case "
8522 : : "expression in match arm (if not final case)");
8523 : 1 : add_error (std::move (error));
8524 : :
8525 : 1 : return nullptr;
8526 : 1 : }
8527 : : else
8528 : : {
8529 : : // otherwise, must be final case, so fine
8530 : : break;
8531 : : }
8532 : : }
8533 : 48869 : lexer.skip_token ();
8534 : : }
8535 : :
8536 : 5532 : if (!skip_token (RIGHT_CURLY))
8537 : : {
8538 : : // skip somewhere?
8539 : 0 : return nullptr;
8540 : : }
8541 : :
8542 : 5532 : match_arms.shrink_to_fit ();
8543 : :
8544 : : return std::unique_ptr<AST::MatchExpr> (
8545 : 5532 : new AST::MatchExpr (std::move (scrutinee), std::move (match_arms),
8546 : : std::move (inner_attrs), std::move (outer_attrs),
8547 : 5532 : locus));
8548 : 5533 : }
8549 : :
8550 : : // Parses the "pattern" part of the match arm (the 'case x:' equivalent).
8551 : : template <typename ManagedTokenSource>
8552 : : AST::MatchArm
8553 : 49875 : Parser<ManagedTokenSource>::parse_match_arm ()
8554 : : {
8555 : : // parse optional outer attributes
8556 : 49875 : AST::AttrVec outer_attrs = parse_outer_attributes ();
8557 : :
8558 : : // DEBUG
8559 : 49875 : rust_debug ("about to start parsing match arm patterns");
8560 : :
8561 : : // break early if find right curly
8562 : 99750 : if (lexer.peek_token ()->get_id () == RIGHT_CURLY)
8563 : : {
8564 : : // not an error
8565 : 0 : return AST::MatchArm::create_error ();
8566 : : }
8567 : :
8568 : : // parse match arm patterns - at least 1 is required
8569 : 49875 : std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
8570 : : = parse_match_arm_patterns (RIGHT_CURLY);
8571 : 49875 : if (match_arm_patterns.empty ())
8572 : : {
8573 : 0 : Error error (lexer.peek_token ()->get_locus (),
8574 : : "failed to parse any patterns in match arm");
8575 : 0 : add_error (std::move (error));
8576 : :
8577 : : // skip somewhere?
8578 : 0 : return AST::MatchArm::create_error ();
8579 : 0 : }
8580 : :
8581 : : // DEBUG
8582 : 49875 : rust_debug ("successfully parsed match arm patterns");
8583 : :
8584 : : // parse match arm guard expr if it exists
8585 : 49875 : std::unique_ptr<AST::Expr> guard_expr = nullptr;
8586 : 99750 : if (lexer.peek_token ()->get_id () == IF)
8587 : : {
8588 : 34 : lexer.skip_token ();
8589 : :
8590 : 34 : guard_expr = parse_expr ();
8591 : 34 : if (guard_expr == nullptr)
8592 : : {
8593 : 0 : Error error (lexer.peek_token ()->get_locus (),
8594 : : "failed to parse guard expression in match arm");
8595 : 0 : add_error (std::move (error));
8596 : :
8597 : : // skip somewhere?
8598 : 0 : return AST::MatchArm::create_error ();
8599 : 0 : }
8600 : : }
8601 : :
8602 : : // DEBUG
8603 : 49875 : rust_debug ("successfully parsed match arm");
8604 : :
8605 : 99750 : return AST::MatchArm (std::move (match_arm_patterns),
8606 : 99750 : lexer.peek_token ()->get_locus (),
8607 : 49875 : std::move (guard_expr), std::move (outer_attrs));
8608 : 49875 : }
8609 : :
8610 : : /* Parses the patterns used in a match arm. End token id is the id of the
8611 : : * token that would exist after the patterns are done (e.g. '}' for match
8612 : : * expr, '=' for if let and while let). */
8613 : : template <typename ManagedTokenSource>
8614 : : std::vector<std::unique_ptr<AST::Pattern>>
8615 : 50026 : Parser<ManagedTokenSource>::parse_match_arm_patterns (TokenId end_token_id)
8616 : : {
8617 : : // skip optional leading '|'
8618 : 100052 : if (lexer.peek_token ()->get_id () == PIPE)
8619 : 0 : lexer.skip_token ();
8620 : : /* TODO: do I even need to store the result of this? can't be used.
8621 : : * If semantically different, I need a wrapped "match arm patterns" object
8622 : : * for this. */
8623 : :
8624 : 50026 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
8625 : :
8626 : : // quick break out if end_token_id
8627 : 100052 : if (lexer.peek_token ()->get_id () == end_token_id)
8628 : 1 : return patterns;
8629 : :
8630 : : // parse required pattern - if doesn't exist, return empty
8631 : 50025 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
8632 : 50025 : if (initial_pattern == nullptr)
8633 : : {
8634 : : // FIXME: should this be an error?
8635 : 0 : return patterns;
8636 : : }
8637 : 50025 : patterns.push_back (std::move (initial_pattern));
8638 : :
8639 : : // DEBUG
8640 : 50025 : rust_debug ("successfully parsed initial match arm pattern");
8641 : :
8642 : : // parse new patterns as long as next char is '|'
8643 : 50025 : const_TokenPtr t = lexer.peek_token ();
8644 : 50025 : while (t->get_id () == PIPE)
8645 : : {
8646 : : // skip pipe token
8647 : 0 : lexer.skip_token ();
8648 : :
8649 : : // break if hit end token id
8650 : 0 : if (lexer.peek_token ()->get_id () == end_token_id)
8651 : : break;
8652 : :
8653 : : // parse pattern
8654 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
8655 : 0 : if (pattern == nullptr)
8656 : : {
8657 : : // this is an error
8658 : 0 : Error error (lexer.peek_token ()->get_locus (),
8659 : : "failed to parse pattern in match arm patterns");
8660 : 0 : add_error (std::move (error));
8661 : :
8662 : : // skip somewhere?
8663 : 0 : return {};
8664 : 0 : }
8665 : :
8666 : 0 : patterns.push_back (std::move (pattern));
8667 : :
8668 : 0 : t = lexer.peek_token ();
8669 : : }
8670 : :
8671 : 50025 : patterns.shrink_to_fit ();
8672 : :
8673 : 50025 : return patterns;
8674 : 50026 : }
8675 : :
8676 : : // Parses an async block expression.
8677 : : template <typename ManagedTokenSource>
8678 : : std::unique_ptr<AST::AsyncBlockExpr>
8679 : 0 : Parser<ManagedTokenSource>::parse_async_block_expr (AST::AttrVec outer_attrs)
8680 : : {
8681 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
8682 : 0 : skip_token (ASYNC);
8683 : :
8684 : : // detect optional move token
8685 : 0 : bool has_move = false;
8686 : 0 : if (lexer.peek_token ()->get_id () == MOVE)
8687 : : {
8688 : 0 : lexer.skip_token ();
8689 : 0 : has_move = true;
8690 : : }
8691 : :
8692 : : // parse block expression (required)
8693 : 0 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
8694 : 0 : if (block_expr == nullptr)
8695 : : {
8696 : 0 : Error error (
8697 : 0 : lexer.peek_token ()->get_locus (),
8698 : : "failed to parse block expression of async block expression");
8699 : 0 : add_error (std::move (error));
8700 : :
8701 : : // skip somewhere?
8702 : 0 : return nullptr;
8703 : 0 : }
8704 : :
8705 : : return std::unique_ptr<AST::AsyncBlockExpr> (
8706 : 0 : new AST::AsyncBlockExpr (std::move (block_expr), has_move,
8707 : 0 : std::move (outer_attrs), locus));
8708 : 0 : }
8709 : :
8710 : : // Parses an unsafe block expression.
8711 : : template <typename ManagedTokenSource>
8712 : : std::unique_ptr<AST::UnsafeBlockExpr>
8713 : 4622 : Parser<ManagedTokenSource>::parse_unsafe_block_expr (
8714 : : AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
8715 : : {
8716 : 4622 : location_t locus = pratt_parsed_loc;
8717 : 4622 : if (locus == UNKNOWN_LOCATION)
8718 : : {
8719 : 0 : locus = lexer.peek_token ()->get_locus ();
8720 : 0 : skip_token (UNSAFE);
8721 : : }
8722 : :
8723 : : // parse block expression (required)
8724 : 4622 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
8725 : 4622 : if (block_expr == nullptr)
8726 : : {
8727 : 0 : Error error (
8728 : 0 : lexer.peek_token ()->get_locus (),
8729 : : "failed to parse block expression of unsafe block expression");
8730 : 0 : add_error (std::move (error));
8731 : :
8732 : : // skip somewhere?
8733 : 0 : return nullptr;
8734 : 0 : }
8735 : :
8736 : : return std::unique_ptr<AST::UnsafeBlockExpr> (
8737 : 4622 : new AST::UnsafeBlockExpr (std::move (block_expr), std::move (outer_attrs),
8738 : 4622 : locus));
8739 : 4622 : }
8740 : :
8741 : : // Parses an array definition expression.
8742 : : template <typename ManagedTokenSource>
8743 : : std::unique_ptr<AST::ArrayExpr>
8744 : 13205 : Parser<ManagedTokenSource>::parse_array_expr (AST::AttrVec outer_attrs,
8745 : : location_t pratt_parsed_loc)
8746 : : {
8747 : 13205 : location_t locus = pratt_parsed_loc;
8748 : 13205 : if (locus == UNKNOWN_LOCATION)
8749 : : {
8750 : 0 : locus = lexer.peek_token ()->get_locus ();
8751 : 0 : skip_token (LEFT_SQUARE);
8752 : : }
8753 : :
8754 : : // parse optional inner attributes
8755 : 13205 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8756 : :
8757 : : // parse the "array elements" section, which is optional
8758 : 26410 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8759 : : {
8760 : : // no array elements
8761 : 57 : lexer.skip_token ();
8762 : :
8763 : 57 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8764 : 57 : auto array_elems
8765 : : = std::make_unique<AST::ArrayElemsValues> (std::move (exprs), locus);
8766 : : return std::make_unique<AST::ArrayExpr> (std::move (array_elems),
8767 : : std::move (inner_attrs),
8768 : 57 : std::move (outer_attrs), locus);
8769 : 57 : }
8770 : : else
8771 : : {
8772 : : // should have array elements
8773 : : // parse initial expression, which is required for either
8774 : 13148 : std::unique_ptr<AST::Expr> initial_expr = parse_expr ();
8775 : 13148 : if (initial_expr == nullptr)
8776 : : {
8777 : 0 : Error error (lexer.peek_token ()->get_locus (),
8778 : : "could not parse expression in array expression "
8779 : : "(even though arrayelems seems to be present)");
8780 : 0 : add_error (std::move (error));
8781 : :
8782 : : // skip somewhere?
8783 : 0 : return nullptr;
8784 : 0 : }
8785 : :
8786 : 26296 : if (lexer.peek_token ()->get_id () == SEMICOLON)
8787 : : {
8788 : : // copy array elems
8789 : 260 : lexer.skip_token ();
8790 : :
8791 : : // parse copy amount expression (required)
8792 : 260 : std::unique_ptr<AST::Expr> copy_amount = parse_expr ();
8793 : 260 : if (copy_amount == nullptr)
8794 : : {
8795 : 0 : Error error (lexer.peek_token ()->get_locus (),
8796 : : "could not parse copy amount expression in array "
8797 : : "expression (arrayelems)");
8798 : 0 : add_error (std::move (error));
8799 : :
8800 : : // skip somewhere?
8801 : 0 : return nullptr;
8802 : 0 : }
8803 : :
8804 : 260 : skip_token (RIGHT_SQUARE);
8805 : :
8806 : 260 : std::unique_ptr<AST::ArrayElemsCopied> copied_array_elems (
8807 : 260 : new AST::ArrayElemsCopied (std::move (initial_expr),
8808 : : std::move (copy_amount), locus));
8809 : : return std::unique_ptr<AST::ArrayExpr> (
8810 : 260 : new AST::ArrayExpr (std::move (copied_array_elems),
8811 : : std::move (inner_attrs),
8812 : 260 : std::move (outer_attrs), locus));
8813 : 260 : }
8814 : 25776 : else if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8815 : : {
8816 : : // single-element array expression
8817 : 36 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8818 : 36 : exprs.reserve (1);
8819 : 36 : exprs.push_back (std::move (initial_expr));
8820 : 36 : exprs.shrink_to_fit ();
8821 : :
8822 : 36 : skip_token (RIGHT_SQUARE);
8823 : :
8824 : 36 : std::unique_ptr<AST::ArrayElemsValues> array_elems (
8825 : 36 : new AST::ArrayElemsValues (std::move (exprs), locus));
8826 : : return std::unique_ptr<AST::ArrayExpr> (
8827 : 36 : new AST::ArrayExpr (std::move (array_elems),
8828 : : std::move (inner_attrs),
8829 : 36 : std::move (outer_attrs), locus));
8830 : 36 : }
8831 : 25704 : else if (lexer.peek_token ()->get_id () == COMMA)
8832 : : {
8833 : : // multi-element array expression (or trailing comma)
8834 : 12852 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8835 : 12852 : exprs.push_back (std::move (initial_expr));
8836 : :
8837 : 12852 : const_TokenPtr t = lexer.peek_token ();
8838 : 123455 : while (t->get_id () == COMMA)
8839 : : {
8840 : 114568 : lexer.skip_token ();
8841 : :
8842 : : // quick break if right square bracket
8843 : 229136 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8844 : : break;
8845 : :
8846 : : // parse expression (required)
8847 : 110603 : std::unique_ptr<AST::Expr> expr = parse_expr ();
8848 : 110603 : if (expr == nullptr)
8849 : : {
8850 : 0 : Error error (lexer.peek_token ()->get_locus (),
8851 : : "failed to parse element in array expression");
8852 : 0 : add_error (std::move (error));
8853 : :
8854 : : // skip somewhere?
8855 : 0 : return nullptr;
8856 : 0 : }
8857 : 110603 : exprs.push_back (std::move (expr));
8858 : :
8859 : 110603 : t = lexer.peek_token ();
8860 : : }
8861 : :
8862 : 12852 : skip_token (RIGHT_SQUARE);
8863 : :
8864 : 12852 : exprs.shrink_to_fit ();
8865 : :
8866 : 12852 : std::unique_ptr<AST::ArrayElemsValues> array_elems (
8867 : 12852 : new AST::ArrayElemsValues (std::move (exprs), locus));
8868 : : return std::unique_ptr<AST::ArrayExpr> (
8869 : 12852 : new AST::ArrayExpr (std::move (array_elems),
8870 : : std::move (inner_attrs),
8871 : 12852 : std::move (outer_attrs), locus));
8872 : 25704 : }
8873 : : else
8874 : : {
8875 : : // error
8876 : 0 : Error error (lexer.peek_token ()->get_locus (),
8877 : : "unexpected token %qs in array expression (arrayelems)",
8878 : 0 : lexer.peek_token ()->get_token_description ());
8879 : 0 : add_error (std::move (error));
8880 : :
8881 : : // skip somewhere?
8882 : 0 : return nullptr;
8883 : 0 : }
8884 : 13148 : }
8885 : 13205 : }
8886 : :
8887 : : // Parses a single parameter used in a closure definition.
8888 : : template <typename ManagedTokenSource>
8889 : : AST::ClosureParam
8890 : 434 : Parser<ManagedTokenSource>::parse_closure_param ()
8891 : : {
8892 : 434 : AST::AttrVec outer_attrs = parse_outer_attributes ();
8893 : :
8894 : : // parse pattern (which is required)
8895 : 434 : std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
8896 : 434 : if (pattern == nullptr)
8897 : : {
8898 : : // not necessarily an error
8899 : 0 : return AST::ClosureParam::create_error ();
8900 : : }
8901 : :
8902 : : // parse optional type of param
8903 : 434 : std::unique_ptr<AST::Type> type = nullptr;
8904 : 868 : if (lexer.peek_token ()->get_id () == COLON)
8905 : : {
8906 : 80 : lexer.skip_token ();
8907 : :
8908 : : // parse type, which is now required
8909 : 80 : type = parse_type ();
8910 : 80 : if (type == nullptr)
8911 : : {
8912 : 0 : Error error (lexer.peek_token ()->get_locus (),
8913 : : "failed to parse type in closure parameter");
8914 : 0 : add_error (std::move (error));
8915 : :
8916 : : // skip somewhere?
8917 : 0 : return AST::ClosureParam::create_error ();
8918 : 0 : }
8919 : : }
8920 : :
8921 : 434 : location_t loc = pattern->get_locus ();
8922 : 434 : return AST::ClosureParam (std::move (pattern), loc, std::move (type),
8923 : 434 : std::move (outer_attrs));
8924 : 434 : }
8925 : :
8926 : : // Parses a grouped or tuple expression (disambiguates).
8927 : : template <typename ManagedTokenSource>
8928 : : std::unique_ptr<AST::ExprWithoutBlock>
8929 : 12550 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_expr (
8930 : : AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
8931 : : {
8932 : : // adjustment to allow Pratt parsing to reuse function without copy-paste
8933 : 12550 : location_t locus = pratt_parsed_loc;
8934 : 12550 : if (locus == UNKNOWN_LOCATION)
8935 : : {
8936 : 0 : locus = lexer.peek_token ()->get_locus ();
8937 : 0 : skip_token (LEFT_PAREN);
8938 : : }
8939 : :
8940 : : // parse optional inner attributes
8941 : 12550 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8942 : :
8943 : 25100 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8944 : : {
8945 : : // must be empty tuple
8946 : 320 : lexer.skip_token ();
8947 : :
8948 : : // create tuple with empty tuple elems
8949 : 320 : return std::unique_ptr<AST::TupleExpr> (
8950 : 320 : new AST::TupleExpr (std::vector<std::unique_ptr<AST::Expr>> (),
8951 : : std::move (inner_attrs), std::move (outer_attrs),
8952 : 320 : locus));
8953 : : }
8954 : :
8955 : : // parse first expression (required)
8956 : 12230 : std::unique_ptr<AST::Expr> first_expr = parse_expr ();
8957 : 12230 : if (first_expr == nullptr)
8958 : : {
8959 : 0 : Error error (lexer.peek_token ()->get_locus (),
8960 : : "failed to parse expression in grouped or tuple expression");
8961 : 0 : add_error (std::move (error));
8962 : :
8963 : : // skip after somewhere?
8964 : 0 : return nullptr;
8965 : 0 : }
8966 : :
8967 : : // detect whether grouped expression with right parentheses as next token
8968 : 24460 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8969 : : {
8970 : : // must be grouped expr
8971 : 8161 : lexer.skip_token ();
8972 : :
8973 : : // create grouped expr
8974 : 8161 : return std::unique_ptr<AST::GroupedExpr> (
8975 : 8161 : new AST::GroupedExpr (std::move (first_expr), std::move (inner_attrs),
8976 : 8161 : std::move (outer_attrs), locus));
8977 : : }
8978 : 8138 : else if (lexer.peek_token ()->get_id () == COMMA)
8979 : : {
8980 : : // tuple expr
8981 : 4068 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8982 : 4068 : exprs.push_back (std::move (first_expr));
8983 : :
8984 : : // parse potential other tuple exprs
8985 : 4068 : const_TokenPtr t = lexer.peek_token ();
8986 : 8421 : while (t->get_id () == COMMA)
8987 : : {
8988 : 4416 : lexer.skip_token ();
8989 : :
8990 : : // break out if right paren
8991 : 8832 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8992 : : break;
8993 : :
8994 : : // parse expr, which is now required
8995 : 4353 : std::unique_ptr<AST::Expr> expr = parse_expr ();
8996 : 4353 : if (expr == nullptr)
8997 : : {
8998 : 0 : Error error (lexer.peek_token ()->get_locus (),
8999 : : "failed to parse expr in tuple expr");
9000 : 0 : add_error (std::move (error));
9001 : :
9002 : : // skip somewhere?
9003 : 0 : return nullptr;
9004 : 0 : }
9005 : 4353 : exprs.push_back (std::move (expr));
9006 : :
9007 : 4353 : t = lexer.peek_token ();
9008 : : }
9009 : :
9010 : : // skip right paren
9011 : 4068 : skip_token (RIGHT_PAREN);
9012 : :
9013 : 4068 : return std::unique_ptr<AST::TupleExpr> (
9014 : 4068 : new AST::TupleExpr (std::move (exprs), std::move (inner_attrs),
9015 : 4068 : std::move (outer_attrs), locus));
9016 : 4068 : }
9017 : : else
9018 : : {
9019 : : // error
9020 : 1 : const_TokenPtr t = lexer.peek_token ();
9021 : 1 : Error error (t->get_locus (),
9022 : : "unexpected token %qs in grouped or tuple expression "
9023 : : "(parenthesised expression) - expected %<)%> for grouped "
9024 : : "expr and %<,%> for tuple expr",
9025 : : t->get_token_description ());
9026 : 1 : add_error (std::move (error));
9027 : :
9028 : : // skip somewhere?
9029 : 1 : return nullptr;
9030 : 2 : }
9031 : 12550 : }
9032 : :
9033 : : // Parses a type (will further disambiguate any type).
9034 : : template <typename ManagedTokenSource>
9035 : : std::unique_ptr<AST::Type>
9036 : 104794 : Parser<ManagedTokenSource>::parse_type (bool save_errors)
9037 : : {
9038 : : /* rules for all types:
9039 : : * NeverType: '!'
9040 : : * SliceType: '[' Type ']'
9041 : : * InferredType: '_'
9042 : : * MacroInvocation: SimplePath '!' DelimTokenTree
9043 : : * ParenthesisedType: '(' Type ')'
9044 : : * ImplTraitType: 'impl' TypeParamBounds
9045 : : * TypeParamBounds (not type) TypeParamBound ( '+' TypeParamBound )* '+'?
9046 : : * TypeParamBound Lifetime | TraitBound
9047 : : * ImplTraitTypeOneBound: 'impl' TraitBound
9048 : : * TraitObjectType: 'dyn'? TypeParamBounds
9049 : : * TraitObjectTypeOneBound: 'dyn'? TraitBound
9050 : : * TraitBound '?'? ForLifetimes? TypePath | '(' '?'?
9051 : : * ForLifetimes? TypePath ')' BareFunctionType: ForLifetimes?
9052 : : * FunctionQualifiers 'fn' etc. ForLifetimes (not type) 'for' '<'
9053 : : * LifetimeParams '>' FunctionQualifiers ( 'async' | 'const' )?
9054 : : * 'unsafe'?
9055 : : * ('extern' abi?)? QualifiedPathInType: '<' Type ( 'as' TypePath )? '>'
9056 : : * (
9057 : : * '::' TypePathSegment )+ TypePath: '::'? TypePathSegment (
9058 : : * '::' TypePathSegment)* ArrayType: '[' Type ';' Expr ']'
9059 : : * ReferenceType: '&' Lifetime? 'mut'? TypeNoBounds
9060 : : * RawPointerType: '*' ( 'mut' | 'const' ) TypeNoBounds
9061 : : * TupleType: '(' Type etc. - regular tuple stuff. Also
9062 : : * regular tuple vs parenthesised precedence
9063 : : *
9064 : : * Disambiguate between macro and type path via type path being parsed, and
9065 : : * then if '!' found, convert type path to simple path for macro. Usual
9066 : : * disambiguation for tuple vs parenthesised. For ImplTraitType and
9067 : : * TraitObjectType individual disambiguations, they seem more like "special
9068 : : * cases", so probably just try to parse the more general ImplTraitType or
9069 : : * TraitObjectType and return OneBound versions if they satisfy those
9070 : : * criteria. */
9071 : :
9072 : 104794 : const_TokenPtr t = lexer.peek_token ();
9073 : 104794 : switch (t->get_id ())
9074 : : {
9075 : 90 : case EXCLAM:
9076 : : // never type - can't be macro as no path beforehand
9077 : 90 : lexer.skip_token ();
9078 : 90 : return std::unique_ptr<AST::NeverType> (
9079 : 90 : new AST::NeverType (t->get_locus ()));
9080 : 1184 : case LEFT_SQUARE:
9081 : : // slice type or array type - requires further disambiguation
9082 : 1184 : return parse_slice_or_array_type ();
9083 : 3531 : case LEFT_SHIFT:
9084 : : case LEFT_ANGLE:
9085 : : {
9086 : : // qualified path in type
9087 : 3531 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
9088 : 3531 : if (path.is_error ())
9089 : : {
9090 : 0 : if (save_errors)
9091 : : {
9092 : 0 : Error error (t->get_locus (),
9093 : : "failed to parse qualified path in type");
9094 : 0 : add_error (std::move (error));
9095 : 0 : }
9096 : :
9097 : 0 : return nullptr;
9098 : : }
9099 : 3531 : return std::unique_ptr<AST::QualifiedPathInType> (
9100 : 3531 : new AST::QualifiedPathInType (std::move (path)));
9101 : 3531 : }
9102 : 315 : case UNDERSCORE:
9103 : : // inferred type
9104 : 315 : lexer.skip_token ();
9105 : 315 : return std::unique_ptr<AST::InferredType> (
9106 : 315 : new AST::InferredType (t->get_locus ()));
9107 : 3403 : case ASTERISK:
9108 : : // raw pointer type
9109 : 3403 : return parse_raw_pointer_type ();
9110 : 11612 : case AMP: // does this also include AMP_AMP?
9111 : : case LOGICAL_AND:
9112 : : // reference type
9113 : 11612 : return parse_reference_type ();
9114 : 0 : case LIFETIME:
9115 : : {
9116 : : /* probably a lifetime bound, so probably type param bounds in
9117 : : * TraitObjectType */
9118 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9119 : : = parse_type_param_bounds ();
9120 : :
9121 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9122 : 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9123 : 0 : false));
9124 : 0 : }
9125 : 82660 : case IDENTIFIER:
9126 : : case SUPER:
9127 : : case SELF:
9128 : : case SELF_ALIAS:
9129 : : case CRATE:
9130 : : case DOLLAR_SIGN:
9131 : : case SCOPE_RESOLUTION:
9132 : : {
9133 : : // macro invocation or type path - requires further disambiguation.
9134 : : /* for parsing path component of each rule, perhaps parse it as a
9135 : : * typepath and attempt conversion to simplepath if a trailing '!' is
9136 : : * found */
9137 : : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
9138 : : * with it, it is exactly the same as a TypePath syntactically, so
9139 : : * this is a syntactical ambiguity. As such, the parser will parse it
9140 : : * as a TypePath. This, however, does not prevent TraitObjectType from
9141 : : * starting with a typepath. */
9142 : :
9143 : : // parse path as type path
9144 : 82660 : AST::TypePath path = parse_type_path ();
9145 : 82660 : if (path.is_error ())
9146 : : {
9147 : 0 : if (save_errors)
9148 : : {
9149 : 0 : Error error (t->get_locus (),
9150 : : "failed to parse path as first component of type");
9151 : 0 : add_error (std::move (error));
9152 : 0 : }
9153 : :
9154 : 0 : return nullptr;
9155 : : }
9156 : 82660 : location_t locus = path.get_locus ();
9157 : :
9158 : : // branch on next token
9159 : 82660 : t = lexer.peek_token ();
9160 : 82660 : switch (t->get_id ())
9161 : : {
9162 : 496 : case EXCLAM:
9163 : : {
9164 : : // macro invocation
9165 : : // convert to simple path
9166 : 496 : AST::SimplePath macro_path = path.as_simple_path ();
9167 : 496 : if (macro_path.is_empty ())
9168 : : {
9169 : 0 : if (save_errors)
9170 : : {
9171 : 0 : Error error (t->get_locus (),
9172 : : "failed to parse simple path in macro "
9173 : : "invocation (for type)");
9174 : 0 : add_error (std::move (error));
9175 : 0 : }
9176 : :
9177 : 0 : return nullptr;
9178 : : }
9179 : :
9180 : 496 : lexer.skip_token ();
9181 : :
9182 : 496 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
9183 : :
9184 : 992 : return AST::MacroInvocation::Regular (
9185 : 992 : AST::MacroInvocData (std::move (macro_path),
9186 : : std::move (tok_tree)),
9187 : 496 : {}, locus);
9188 : 992 : }
9189 : 29 : case PLUS:
9190 : : {
9191 : : // type param bounds
9192 : 29 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9193 : :
9194 : : // convert type path to trait bound
9195 : 29 : std::unique_ptr<AST::TraitBound> path_bound (
9196 : 29 : new AST::TraitBound (std::move (path), locus, false, false));
9197 : 29 : bounds.push_back (std::move (path_bound));
9198 : :
9199 : : /* parse rest of bounds - FIXME: better way to find when to stop
9200 : : * parsing */
9201 : 58 : while (t->get_id () == PLUS)
9202 : : {
9203 : 29 : lexer.skip_token ();
9204 : :
9205 : : // parse bound if it exists - if not, assume end of sequence
9206 : 29 : std::unique_ptr<AST::TypeParamBound> bound
9207 : : = parse_type_param_bound ();
9208 : 29 : if (bound == nullptr)
9209 : : {
9210 : : break;
9211 : : }
9212 : 29 : bounds.push_back (std::move (bound));
9213 : :
9214 : 29 : t = lexer.peek_token ();
9215 : : }
9216 : :
9217 : 29 : return std::unique_ptr<AST::TraitObjectType> (
9218 : 29 : new AST::TraitObjectType (std::move (bounds), locus, false));
9219 : 29 : }
9220 : 82135 : default:
9221 : : // assume that this is a type path and not an error
9222 : 82135 : return std::unique_ptr<AST::TypePath> (
9223 : 82135 : new AST::TypePath (std::move (path)));
9224 : : }
9225 : 82660 : }
9226 : 1039 : case LEFT_PAREN:
9227 : : /* tuple type or parenthesised type - requires further disambiguation
9228 : : * (the usual). ok apparently can be a parenthesised TraitBound too, so
9229 : : * could be TraitObjectTypeOneBound or TraitObjectType */
9230 : 1039 : return parse_paren_prefixed_type ();
9231 : 4 : case FOR:
9232 : : // TraitObjectTypeOneBound or BareFunctionType
9233 : 4 : return parse_for_prefixed_type ();
9234 : 684 : case ASYNC:
9235 : : case CONST:
9236 : : case UNSAFE:
9237 : : case EXTERN_KW:
9238 : : case FN_KW:
9239 : : // bare function type (with no for lifetimes)
9240 : 684 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
9241 : 226 : case IMPL:
9242 : 226 : lexer.skip_token ();
9243 : 452 : if (lexer.peek_token ()->get_id () == LIFETIME)
9244 : : {
9245 : : /* cannot be one bound because lifetime prevents it from being
9246 : : * traitbound */
9247 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9248 : : = parse_type_param_bounds ();
9249 : :
9250 : 0 : return std::unique_ptr<AST::ImplTraitType> (
9251 : 0 : new AST::ImplTraitType (std::move (bounds), t->get_locus ()));
9252 : 0 : }
9253 : : else
9254 : : {
9255 : : // should be trait bound, so parse trait bound
9256 : 226 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
9257 : 226 : if (initial_bound == nullptr)
9258 : : {
9259 : 0 : if (save_errors)
9260 : : {
9261 : 0 : Error error (lexer.peek_token ()->get_locus (),
9262 : : "failed to parse ImplTraitType initial bound");
9263 : 0 : add_error (std::move (error));
9264 : 0 : }
9265 : :
9266 : 0 : return nullptr;
9267 : : }
9268 : :
9269 : 226 : location_t locus = t->get_locus ();
9270 : :
9271 : : // short cut if next token isn't '+'
9272 : 226 : t = lexer.peek_token ();
9273 : 226 : if (t->get_id () != PLUS)
9274 : : {
9275 : 224 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
9276 : 224 : new AST::ImplTraitTypeOneBound (std::move (initial_bound),
9277 : 224 : locus));
9278 : : }
9279 : :
9280 : : // parse additional type param bounds
9281 : 2 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9282 : 2 : bounds.push_back (std::move (initial_bound));
9283 : 4 : while (t->get_id () == PLUS)
9284 : : {
9285 : 2 : lexer.skip_token ();
9286 : :
9287 : : // parse bound if it exists
9288 : 2 : std::unique_ptr<AST::TypeParamBound> bound
9289 : : = parse_type_param_bound ();
9290 : 2 : if (bound == nullptr)
9291 : : {
9292 : : // not an error as trailing plus may exist
9293 : : break;
9294 : : }
9295 : 2 : bounds.push_back (std::move (bound));
9296 : :
9297 : 2 : t = lexer.peek_token ();
9298 : : }
9299 : :
9300 : 2 : return std::unique_ptr<AST::ImplTraitType> (
9301 : 2 : new AST::ImplTraitType (std::move (bounds), locus));
9302 : 226 : }
9303 : 42 : case DYN:
9304 : : case QUESTION_MARK:
9305 : : {
9306 : : // either TraitObjectType or TraitObjectTypeOneBound
9307 : 42 : bool has_dyn = false;
9308 : 42 : if (t->get_id () == DYN)
9309 : : {
9310 : 42 : lexer.skip_token ();
9311 : 42 : has_dyn = true;
9312 : : }
9313 : :
9314 : 84 : if (lexer.peek_token ()->get_id () == LIFETIME)
9315 : : {
9316 : : /* cannot be one bound because lifetime prevents it from being
9317 : : * traitbound */
9318 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9319 : : = parse_type_param_bounds ();
9320 : :
9321 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9322 : 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9323 : 0 : has_dyn));
9324 : 0 : }
9325 : : else
9326 : : {
9327 : : // should be trait bound, so parse trait bound
9328 : 42 : std::unique_ptr<AST::TraitBound> initial_bound
9329 : : = parse_trait_bound ();
9330 : 42 : if (initial_bound == nullptr)
9331 : : {
9332 : 0 : if (save_errors)
9333 : : {
9334 : 0 : Error error (
9335 : 0 : lexer.peek_token ()->get_locus (),
9336 : : "failed to parse TraitObjectType initial bound");
9337 : 0 : add_error (std::move (error));
9338 : 0 : }
9339 : :
9340 : 0 : return nullptr;
9341 : : }
9342 : :
9343 : : // short cut if next token isn't '+'
9344 : 42 : t = lexer.peek_token ();
9345 : 42 : if (t->get_id () != PLUS)
9346 : : {
9347 : : // convert trait bound to value object
9348 : 16 : AST::TraitBound value_bound (*initial_bound);
9349 : :
9350 : : // DEBUG: removed as unique ptr, so should auto delete
9351 : : // delete initial_bound;
9352 : :
9353 : 16 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9354 : 32 : new AST::TraitObjectTypeOneBound (std::move (value_bound),
9355 : 16 : t->get_locus (), has_dyn));
9356 : 16 : }
9357 : :
9358 : : // parse additional type param bounds
9359 : 26 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9360 : 26 : bounds.push_back (std::move (initial_bound));
9361 : 61 : while (t->get_id () == PLUS)
9362 : : {
9363 : 35 : lexer.skip_token ();
9364 : :
9365 : : // parse bound if it exists
9366 : 35 : std::unique_ptr<AST::TypeParamBound> bound
9367 : : = parse_type_param_bound ();
9368 : 35 : if (bound == nullptr)
9369 : : {
9370 : : // not an error as trailing plus may exist
9371 : : break;
9372 : : }
9373 : 35 : bounds.push_back (std::move (bound));
9374 : :
9375 : 35 : t = lexer.peek_token ();
9376 : : }
9377 : :
9378 : 26 : return std::unique_ptr<AST::TraitObjectType> (
9379 : 26 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9380 : 26 : has_dyn));
9381 : 42 : }
9382 : : }
9383 : 4 : default:
9384 : 4 : if (save_errors)
9385 : 4 : add_error (Error (t->get_locus (), "unrecognised token %qs in type",
9386 : : t->get_token_description ()));
9387 : :
9388 : 4 : return nullptr;
9389 : : }
9390 : 104794 : }
9391 : :
9392 : : /* Parses a type that has '(' as its first character. Returns a tuple type,
9393 : : * parenthesised type, TraitObjectTypeOneBound, or TraitObjectType depending
9394 : : * on following characters. */
9395 : : template <typename ManagedTokenSource>
9396 : : std::unique_ptr<AST::Type>
9397 : 1039 : Parser<ManagedTokenSource>::parse_paren_prefixed_type ()
9398 : : {
9399 : : /* NOTE: Syntactical ambiguity of a parenthesised trait bound is considered
9400 : : * a trait bound, not a parenthesised type, so that it can still be used in
9401 : : * type param bounds. */
9402 : :
9403 : : /* NOTE: this implementation is really shit but I couldn't think of a better
9404 : : * one. It requires essentially breaking polymorphism and downcasting via
9405 : : * virtual method abuse, as it was copied from the rustc implementation (in
9406 : : * which types are reified due to tagged union), after a more OOP attempt by
9407 : : * me failed. */
9408 : 1039 : location_t left_delim_locus = lexer.peek_token ()->get_locus ();
9409 : :
9410 : : // skip left delim
9411 : 1039 : lexer.skip_token ();
9412 : : /* while next token isn't close delim, parse comma-separated types, saving
9413 : : * whether trailing comma happens */
9414 : 1039 : const_TokenPtr t = lexer.peek_token ();
9415 : 1039 : bool trailing_comma = true;
9416 : 1039 : std::vector<std::unique_ptr<AST::Type>> types;
9417 : :
9418 : 2476 : while (t->get_id () != RIGHT_PAREN)
9419 : : {
9420 : 2158 : std::unique_ptr<AST::Type> type = parse_type ();
9421 : 2158 : if (type == nullptr)
9422 : : {
9423 : 0 : Error error (t->get_locus (),
9424 : : "failed to parse type inside parentheses (probably "
9425 : : "tuple or parenthesised)");
9426 : 0 : add_error (std::move (error));
9427 : :
9428 : 0 : return nullptr;
9429 : 0 : }
9430 : 2158 : types.push_back (std::move (type));
9431 : :
9432 : 2158 : t = lexer.peek_token ();
9433 : 2158 : if (t->get_id () != COMMA)
9434 : : {
9435 : 721 : trailing_comma = false;
9436 : : break;
9437 : : }
9438 : 1437 : lexer.skip_token ();
9439 : :
9440 : 1437 : t = lexer.peek_token ();
9441 : : }
9442 : :
9443 : 1039 : if (!skip_token (RIGHT_PAREN))
9444 : : {
9445 : 0 : return nullptr;
9446 : : }
9447 : :
9448 : : // if only one type and no trailing comma, then not a tuple type
9449 : 1039 : if (types.size () == 1 && !trailing_comma)
9450 : : {
9451 : : // must be a TraitObjectType (with more than one bound)
9452 : 10 : if (lexer.peek_token ()->get_id () == PLUS)
9453 : : {
9454 : : // create type param bounds vector
9455 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9456 : :
9457 : : // HACK: convert type to traitbound and add to bounds
9458 : 0 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
9459 : 0 : std::unique_ptr<AST::TraitBound> converted_bound (
9460 : 0 : released_ptr->to_trait_bound (true));
9461 : 0 : if (converted_bound == nullptr)
9462 : : {
9463 : 0 : Error error (
9464 : 0 : lexer.peek_token ()->get_locus (),
9465 : : "failed to hackily converted parsed type to trait bound");
9466 : 0 : add_error (std::move (error));
9467 : :
9468 : 0 : return nullptr;
9469 : 0 : }
9470 : 0 : bounds.push_back (std::move (converted_bound));
9471 : :
9472 : 0 : t = lexer.peek_token ();
9473 : 0 : while (t->get_id () == PLUS)
9474 : : {
9475 : 0 : lexer.skip_token ();
9476 : :
9477 : : // attempt to parse typeparambound
9478 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9479 : : = parse_type_param_bound ();
9480 : 0 : if (bound == nullptr)
9481 : : {
9482 : : // not an error if null
9483 : : break;
9484 : : }
9485 : 0 : bounds.push_back (std::move (bound));
9486 : :
9487 : 0 : t = lexer.peek_token ();
9488 : : }
9489 : :
9490 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9491 : 0 : new AST::TraitObjectType (std::move (bounds), left_delim_locus,
9492 : 0 : false));
9493 : 0 : }
9494 : : else
9495 : : {
9496 : : // release vector pointer
9497 : 5 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
9498 : : /* HACK: attempt to convert to trait bound. if fails, parenthesised
9499 : : * type */
9500 : 5 : std::unique_ptr<AST::TraitBound> converted_bound (
9501 : 5 : released_ptr->to_trait_bound (true));
9502 : 5 : if (converted_bound == nullptr)
9503 : : {
9504 : : // parenthesised type
9505 : 2 : return std::unique_ptr<AST::ParenthesisedType> (
9506 : 2 : new AST::ParenthesisedType (std::move (released_ptr),
9507 : 2 : left_delim_locus));
9508 : : }
9509 : : else
9510 : : {
9511 : : // trait object type (one bound)
9512 : :
9513 : : // get value semantics trait bound
9514 : 3 : AST::TraitBound value_bound (*converted_bound);
9515 : :
9516 : 3 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9517 : 6 : new AST::TraitObjectTypeOneBound (value_bound,
9518 : 3 : left_delim_locus));
9519 : 3 : }
9520 : 5 : }
9521 : : }
9522 : : else
9523 : : {
9524 : 1034 : return std::unique_ptr<AST::TupleType> (
9525 : 1034 : new AST::TupleType (std::move (types), left_delim_locus));
9526 : : }
9527 : : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
9528 : : * lost somehow */
9529 : 1039 : }
9530 : :
9531 : : /* Parses a type that has 'for' as its first character. This means it has a
9532 : : * "for lifetimes", so returns either a BareFunctionType, TraitObjectType, or
9533 : : * TraitObjectTypeOneBound depending on following characters. */
9534 : : template <typename ManagedTokenSource>
9535 : : std::unique_ptr<AST::Type>
9536 : 4 : Parser<ManagedTokenSource>::parse_for_prefixed_type ()
9537 : : {
9538 : 4 : location_t for_locus = lexer.peek_token ()->get_locus ();
9539 : : // parse for lifetimes in type
9540 : 4 : std::vector<AST::LifetimeParam> for_lifetimes = parse_for_lifetimes ();
9541 : :
9542 : : // branch on next token - either function or a trait type
9543 : 4 : const_TokenPtr t = lexer.peek_token ();
9544 : 4 : switch (t->get_id ())
9545 : : {
9546 : 3 : case ASYNC:
9547 : : case CONST:
9548 : : case UNSAFE:
9549 : : case EXTERN_KW:
9550 : : case FN_KW:
9551 : 3 : return parse_bare_function_type (std::move (for_lifetimes));
9552 : 1 : case SCOPE_RESOLUTION:
9553 : : case IDENTIFIER:
9554 : : case SUPER:
9555 : : case SELF:
9556 : : case SELF_ALIAS:
9557 : : case CRATE:
9558 : : case DOLLAR_SIGN:
9559 : : {
9560 : : // path, so trait type
9561 : :
9562 : : // parse type path to finish parsing trait bound
9563 : 1 : AST::TypePath path = parse_type_path ();
9564 : :
9565 : 1 : t = lexer.peek_token ();
9566 : 1 : if (t->get_id () != PLUS)
9567 : : {
9568 : : // must be one-bound trait type
9569 : : // create trait bound value object
9570 : 1 : AST::TraitBound bound (std::move (path), for_locus, false, false,
9571 : : std::move (for_lifetimes));
9572 : :
9573 : 1 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9574 : 2 : new AST::TraitObjectTypeOneBound (std::move (bound), for_locus));
9575 : 1 : }
9576 : :
9577 : : /* more than one bound trait type (or at least parsed as it - could be
9578 : : * trailing '+') create trait bound pointer and bounds */
9579 : 0 : std::unique_ptr<AST::TraitBound> initial_bound (
9580 : 0 : new AST::TraitBound (std::move (path), for_locus, false, false,
9581 : : std::move (for_lifetimes)));
9582 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9583 : 0 : bounds.push_back (std::move (initial_bound));
9584 : :
9585 : 0 : while (t->get_id () == PLUS)
9586 : : {
9587 : 0 : lexer.skip_token ();
9588 : :
9589 : : // parse type param bound if it exists
9590 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9591 : : = parse_type_param_bound ();
9592 : 0 : if (bound == nullptr)
9593 : : {
9594 : : // not an error - e.g. trailing plus
9595 : 0 : return nullptr;
9596 : : }
9597 : 0 : bounds.push_back (std::move (bound));
9598 : :
9599 : 0 : t = lexer.peek_token ();
9600 : : }
9601 : :
9602 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9603 : 0 : new AST::TraitObjectType (std::move (bounds), for_locus, false));
9604 : 1 : }
9605 : 0 : default:
9606 : : // error
9607 : 0 : add_error (Error (t->get_locus (),
9608 : : "unrecognised token %qs in bare function type or trait "
9609 : : "object type or trait object type one bound",
9610 : : t->get_token_description ()));
9611 : :
9612 : 0 : return nullptr;
9613 : : }
9614 : 4 : }
9615 : :
9616 : : // Parses a maybe named param used in bare function types.
9617 : : template <typename ManagedTokenSource>
9618 : : AST::MaybeNamedParam
9619 : 3810 : Parser<ManagedTokenSource>::parse_maybe_named_param (AST::AttrVec outer_attrs)
9620 : : {
9621 : : /* Basically guess that param is named if first token is identifier or
9622 : : * underscore and second token is semicolon. This should probably have no
9623 : : * exceptions. rustc uses backtracking to parse these, but at the time of
9624 : : * writing gccrs has no backtracking capabilities. */
9625 : 3810 : const_TokenPtr current = lexer.peek_token ();
9626 : 3810 : const_TokenPtr next = lexer.peek_token (1);
9627 : :
9628 : 3810 : Identifier name;
9629 : 3810 : AST::MaybeNamedParam::ParamKind kind = AST::MaybeNamedParam::UNNAMED;
9630 : :
9631 : 3810 : if (current->get_id () == IDENTIFIER && next->get_id () == COLON)
9632 : : {
9633 : : // named param
9634 : 1 : name = {current};
9635 : 1 : kind = AST::MaybeNamedParam::IDENTIFIER;
9636 : 1 : lexer.skip_token (1);
9637 : : }
9638 : 3809 : else if (current->get_id () == UNDERSCORE && next->get_id () == COLON)
9639 : : {
9640 : : // wildcard param
9641 : 12 : name = {Values::Keywords::UNDERSCORE, current->get_locus ()};
9642 : 6 : kind = AST::MaybeNamedParam::WILDCARD;
9643 : 6 : lexer.skip_token (1);
9644 : : }
9645 : :
9646 : : // parse type (required)
9647 : 3810 : std::unique_ptr<AST::Type> type = parse_type ();
9648 : 3810 : if (type == nullptr)
9649 : : {
9650 : 0 : Error error (lexer.peek_token ()->get_locus (),
9651 : : "failed to parse type in maybe named param");
9652 : 0 : add_error (std::move (error));
9653 : :
9654 : 0 : return AST::MaybeNamedParam::create_error ();
9655 : 0 : }
9656 : :
9657 : 7620 : return AST::MaybeNamedParam (std::move (name), kind, std::move (type),
9658 : 3810 : std::move (outer_attrs), current->get_locus ());
9659 : 7620 : }
9660 : :
9661 : : /* Parses a bare function type (with the given for lifetimes for convenience -
9662 : : * does not parse them itself). */
9663 : : template <typename ManagedTokenSource>
9664 : : std::unique_ptr<AST::BareFunctionType>
9665 : 689 : Parser<ManagedTokenSource>::parse_bare_function_type (
9666 : : std::vector<AST::LifetimeParam> for_lifetimes)
9667 : : {
9668 : : // TODO: pass in for lifetime location as param
9669 : 689 : location_t best_try_locus = lexer.peek_token ()->get_locus ();
9670 : :
9671 : 689 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
9672 : :
9673 : 689 : if (!skip_token (FN_KW))
9674 : 0 : return nullptr;
9675 : :
9676 : 689 : if (!skip_token (LEFT_PAREN))
9677 : 0 : return nullptr;
9678 : :
9679 : : // parse function params, if they exist
9680 : 689 : std::vector<AST::MaybeNamedParam> params;
9681 : 689 : bool is_variadic = false;
9682 : 689 : AST::AttrVec variadic_attrs;
9683 : :
9684 : 689 : const_TokenPtr t = lexer.peek_token ();
9685 : 8061 : while (t->get_id () != RIGHT_PAREN)
9686 : : {
9687 : 4002 : AST::AttrVec temp_attrs = parse_outer_attributes ();
9688 : :
9689 : 8004 : if (lexer.peek_token ()->get_id () == ELLIPSIS)
9690 : : {
9691 : 192 : lexer.skip_token ();
9692 : 192 : is_variadic = true;
9693 : 192 : variadic_attrs = std::move (temp_attrs);
9694 : :
9695 : 192 : t = lexer.peek_token ();
9696 : :
9697 : 192 : if (t->get_id () != RIGHT_PAREN)
9698 : : {
9699 : 0 : Error error (t->get_locus (),
9700 : : "expected right parentheses after variadic in maybe "
9701 : : "named function "
9702 : : "parameters, found %qs",
9703 : : t->get_token_description ());
9704 : 0 : add_error (std::move (error));
9705 : :
9706 : 0 : return nullptr;
9707 : 0 : }
9708 : :
9709 : : break;
9710 : : }
9711 : :
9712 : 3810 : AST::MaybeNamedParam param
9713 : 3810 : = parse_maybe_named_param (std::move (temp_attrs));
9714 : 3810 : if (param.is_error ())
9715 : : {
9716 : 0 : Error error (
9717 : 0 : lexer.peek_token ()->get_locus (),
9718 : : "failed to parse maybe named param in bare function type");
9719 : 0 : add_error (std::move (error));
9720 : :
9721 : 0 : return nullptr;
9722 : 0 : }
9723 : 3810 : params.push_back (std::move (param));
9724 : :
9725 : 7620 : if (lexer.peek_token ()->get_id () != COMMA)
9726 : : break;
9727 : :
9728 : 3370 : lexer.skip_token ();
9729 : 3370 : t = lexer.peek_token ();
9730 : : }
9731 : :
9732 : 689 : if (!skip_token (RIGHT_PAREN))
9733 : 0 : return nullptr;
9734 : :
9735 : : // bare function return type, if exists
9736 : 689 : std::unique_ptr<AST::TypeNoBounds> return_type = nullptr;
9737 : 1378 : if (lexer.peek_token ()->get_id () == RETURN_TYPE)
9738 : : {
9739 : 668 : lexer.skip_token ();
9740 : :
9741 : : // parse required TypeNoBounds
9742 : 668 : return_type = parse_type_no_bounds ();
9743 : 668 : if (return_type == nullptr)
9744 : : {
9745 : 0 : Error error (lexer.peek_token ()->get_locus (),
9746 : : "failed to parse return type (type no bounds) in bare "
9747 : : "function type");
9748 : 0 : add_error (std::move (error));
9749 : :
9750 : 0 : return nullptr;
9751 : 0 : }
9752 : : }
9753 : :
9754 : : return std::unique_ptr<AST::BareFunctionType> (
9755 : 689 : new AST::BareFunctionType (std::move (for_lifetimes),
9756 : : std::move (qualifiers), std::move (params),
9757 : : is_variadic, std::move (variadic_attrs),
9758 : 689 : std::move (return_type), best_try_locus));
9759 : 1378 : }
9760 : :
9761 : : template <typename ManagedTokenSource>
9762 : : std::unique_ptr<AST::ReferenceType>
9763 : 11656 : Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
9764 : : {
9765 : : // parse optional lifetime
9766 : 11656 : AST::Lifetime lifetime = AST::Lifetime::elided ();
9767 : 23312 : if (lexer.peek_token ()->get_id () == LIFETIME)
9768 : : {
9769 : 1710 : auto parsed_lifetime = parse_lifetime (true);
9770 : 1710 : if (parsed_lifetime)
9771 : : {
9772 : 1710 : lifetime = parsed_lifetime.value ();
9773 : : }
9774 : : else
9775 : : {
9776 : 0 : Error error (lexer.peek_token ()->get_locus (),
9777 : : "failed to parse lifetime in reference type");
9778 : 0 : add_error (std::move (error));
9779 : :
9780 : 0 : return nullptr;
9781 : 0 : }
9782 : 1710 : }
9783 : :
9784 : 11656 : bool is_mut = false;
9785 : 23312 : if (lexer.peek_token ()->get_id () == MUT)
9786 : : {
9787 : 1573 : lexer.skip_token ();
9788 : 1573 : is_mut = true;
9789 : : }
9790 : :
9791 : : // parse type no bounds, which is required
9792 : 11656 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
9793 : 11656 : if (type == nullptr)
9794 : : {
9795 : 0 : Error error (lexer.peek_token ()->get_locus (),
9796 : : "failed to parse referenced type in reference type");
9797 : 0 : add_error (std::move (error));
9798 : :
9799 : 0 : return nullptr;
9800 : 0 : }
9801 : :
9802 : : return std::unique_ptr<AST::ReferenceType> (
9803 : 34968 : new AST::ReferenceType (is_mut, std::move (type), locus,
9804 : 11656 : std::move (lifetime)));
9805 : 11656 : }
9806 : :
9807 : : // Parses a reference type (mutable or immutable, with given lifetime).
9808 : : template <typename ManagedTokenSource>
9809 : : std::unique_ptr<AST::ReferenceType>
9810 : 11656 : Parser<ManagedTokenSource>::parse_reference_type ()
9811 : : {
9812 : 11656 : auto t = lexer.peek_token ();
9813 : 11656 : auto locus = t->get_locus ();
9814 : :
9815 : 11656 : switch (t->get_id ())
9816 : : {
9817 : 11617 : case AMP:
9818 : 11617 : skip_token (AMP);
9819 : 11617 : return parse_reference_type_inner (locus);
9820 : 39 : case LOGICAL_AND:
9821 : 39 : skip_token (LOGICAL_AND);
9822 : : return std::unique_ptr<AST::ReferenceType> (
9823 : 117 : new AST::ReferenceType (false, parse_reference_type_inner (locus),
9824 : 39 : locus));
9825 : 0 : default:
9826 : 0 : rust_unreachable ();
9827 : : }
9828 : 11656 : }
9829 : :
9830 : : // Parses a raw (unsafe) pointer type.
9831 : : template <typename ManagedTokenSource>
9832 : : std::unique_ptr<AST::RawPointerType>
9833 : 7861 : Parser<ManagedTokenSource>::parse_raw_pointer_type ()
9834 : : {
9835 : 7861 : location_t locus = lexer.peek_token ()->get_locus ();
9836 : 7861 : skip_token (ASTERISK);
9837 : :
9838 : 7861 : AST::RawPointerType::PointerType kind = AST::RawPointerType::CONST;
9839 : :
9840 : : // branch on next token for pointer kind info
9841 : 7861 : const_TokenPtr t = lexer.peek_token ();
9842 : 7861 : switch (t->get_id ())
9843 : : {
9844 : 1353 : case MUT:
9845 : 1353 : kind = AST::RawPointerType::MUT;
9846 : 1353 : lexer.skip_token ();
9847 : : break;
9848 : 6508 : case CONST:
9849 : 6508 : kind = AST::RawPointerType::CONST;
9850 : 6508 : lexer.skip_token ();
9851 : : break;
9852 : 0 : default:
9853 : 0 : add_error (Error (t->get_locus (),
9854 : : "unrecognised token %qs in raw pointer type",
9855 : : t->get_token_description ()));
9856 : :
9857 : 0 : return nullptr;
9858 : : }
9859 : :
9860 : : // parse type no bounds (required)
9861 : 7861 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
9862 : 7861 : if (type == nullptr)
9863 : : {
9864 : 0 : Error error (lexer.peek_token ()->get_locus (),
9865 : : "failed to parse pointed type of raw pointer type");
9866 : 0 : add_error (std::move (error));
9867 : :
9868 : 0 : return nullptr;
9869 : 0 : }
9870 : :
9871 : : return std::unique_ptr<AST::RawPointerType> (
9872 : 7861 : new AST::RawPointerType (kind, std::move (type), locus));
9873 : 7861 : }
9874 : :
9875 : : /* Parses a slice or array type, depending on following arguments (as
9876 : : * lookahead is not possible). */
9877 : : template <typename ManagedTokenSource>
9878 : : std::unique_ptr<AST::TypeNoBounds>
9879 : 2416 : Parser<ManagedTokenSource>::parse_slice_or_array_type ()
9880 : : {
9881 : 2416 : location_t locus = lexer.peek_token ()->get_locus ();
9882 : 2416 : skip_token (LEFT_SQUARE);
9883 : :
9884 : : // parse inner type (required)
9885 : 2416 : std::unique_ptr<AST::Type> inner_type = parse_type ();
9886 : 2416 : if (inner_type == nullptr)
9887 : : {
9888 : 0 : Error error (lexer.peek_token ()->get_locus (),
9889 : : "failed to parse inner type in slice or array type");
9890 : 0 : add_error (std::move (error));
9891 : :
9892 : 0 : return nullptr;
9893 : 0 : }
9894 : :
9895 : : // branch on next token
9896 : 2416 : const_TokenPtr t = lexer.peek_token ();
9897 : 2416 : switch (t->get_id ())
9898 : : {
9899 : 1417 : case RIGHT_SQUARE:
9900 : : // slice type
9901 : 1417 : lexer.skip_token ();
9902 : :
9903 : 1417 : return std::unique_ptr<AST::SliceType> (
9904 : 1417 : new AST::SliceType (std::move (inner_type), locus));
9905 : 999 : case SEMICOLON:
9906 : : {
9907 : : // array type
9908 : 999 : lexer.skip_token ();
9909 : :
9910 : : // parse required array size expression
9911 : 999 : auto size = parse_anon_const ();
9912 : :
9913 : 999 : if (!size)
9914 : : {
9915 : 0 : Error error (lexer.peek_token ()->get_locus (),
9916 : : "failed to parse size expression in array type");
9917 : 0 : add_error (std::move (error));
9918 : :
9919 : 0 : return nullptr;
9920 : 0 : }
9921 : :
9922 : 999 : if (!skip_token (RIGHT_SQUARE))
9923 : : {
9924 : 0 : return nullptr;
9925 : : }
9926 : :
9927 : 999 : return std::unique_ptr<AST::ArrayType> (
9928 : 1987 : new AST::ArrayType (std::move (inner_type), std::move (*size),
9929 : 999 : locus));
9930 : 999 : }
9931 : 0 : default:
9932 : : // error
9933 : 0 : add_error (
9934 : 0 : Error (t->get_locus (),
9935 : : "unrecognised token %qs in slice or array type after inner type",
9936 : : t->get_token_description ()));
9937 : :
9938 : 0 : return nullptr;
9939 : : }
9940 : 2416 : }
9941 : :
9942 : : // Parses a type, taking into account type boundary disambiguation.
9943 : : template <typename ManagedTokenSource>
9944 : : std::unique_ptr<AST::TypeNoBounds>
9945 : 29146 : Parser<ManagedTokenSource>::parse_type_no_bounds ()
9946 : : {
9947 : 29146 : const_TokenPtr t = lexer.peek_token ();
9948 : 29146 : switch (t->get_id ())
9949 : : {
9950 : 4 : case EXCLAM:
9951 : : // never type - can't be macro as no path beforehand
9952 : 4 : lexer.skip_token ();
9953 : 4 : return std::unique_ptr<AST::NeverType> (
9954 : 4 : new AST::NeverType (t->get_locus ()));
9955 : 1232 : case LEFT_SQUARE:
9956 : : // slice type or array type - requires further disambiguation
9957 : 1232 : return parse_slice_or_array_type ();
9958 : 22 : case LEFT_SHIFT:
9959 : : case LEFT_ANGLE:
9960 : : {
9961 : : // qualified path in type
9962 : 22 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
9963 : 22 : if (path.is_error ())
9964 : : {
9965 : 0 : Error error (t->get_locus (),
9966 : : "failed to parse qualified path in type");
9967 : 0 : add_error (std::move (error));
9968 : :
9969 : 0 : return nullptr;
9970 : 0 : }
9971 : 22 : return std::unique_ptr<AST::QualifiedPathInType> (
9972 : 22 : new AST::QualifiedPathInType (std::move (path)));
9973 : 22 : }
9974 : 189 : case UNDERSCORE:
9975 : : // inferred type
9976 : 189 : lexer.skip_token ();
9977 : 189 : return std::unique_ptr<AST::InferredType> (
9978 : 189 : new AST::InferredType (t->get_locus ()));
9979 : 4458 : case ASTERISK:
9980 : : // raw pointer type
9981 : 4458 : return parse_raw_pointer_type ();
9982 : 44 : case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND?
9983 : : case LOGICAL_AND:
9984 : : // reference type
9985 : 44 : return parse_reference_type ();
9986 : 0 : case LIFETIME:
9987 : : /* probably a lifetime bound, so probably type param bounds in
9988 : : * TraitObjectType. this is not allowed, but detection here for error
9989 : : * message */
9990 : 0 : add_error (Error (t->get_locus (),
9991 : : "lifetime bounds (i.e. in type param bounds, in "
9992 : : "TraitObjectType) are not allowed as TypeNoBounds"));
9993 : :
9994 : 0 : return nullptr;
9995 : 22700 : case IDENTIFIER:
9996 : : case SUPER:
9997 : : case SELF:
9998 : : case SELF_ALIAS:
9999 : : case CRATE:
10000 : : case DOLLAR_SIGN:
10001 : : case SCOPE_RESOLUTION:
10002 : : {
10003 : : // macro invocation or type path - requires further disambiguation.
10004 : : /* for parsing path component of each rule, perhaps parse it as a
10005 : : * typepath and attempt conversion to simplepath if a trailing '!' is
10006 : : * found */
10007 : : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
10008 : : * with it, it is exactly the same as a TypePath syntactically, so
10009 : : * this is a syntactical ambiguity. As such, the parser will parse it
10010 : : * as a TypePath. This, however, does not prevent TraitObjectType from
10011 : : * starting with a typepath. */
10012 : :
10013 : : // parse path as type path
10014 : 22700 : AST::TypePath path = parse_type_path ();
10015 : 22700 : if (path.is_error ())
10016 : : {
10017 : 0 : Error error (
10018 : : t->get_locus (),
10019 : : "failed to parse path as first component of type no bounds");
10020 : 0 : add_error (std::move (error));
10021 : :
10022 : 0 : return nullptr;
10023 : 0 : }
10024 : 22700 : location_t locus = path.get_locus ();
10025 : :
10026 : : // branch on next token
10027 : 22700 : t = lexer.peek_token ();
10028 : 22700 : switch (t->get_id ())
10029 : : {
10030 : 1 : case EXCLAM:
10031 : : {
10032 : : // macro invocation
10033 : : // convert to simple path
10034 : 1 : AST::SimplePath macro_path = path.as_simple_path ();
10035 : 1 : if (macro_path.is_empty ())
10036 : : {
10037 : 0 : Error error (t->get_locus (),
10038 : : "failed to parse simple path in macro "
10039 : : "invocation (for type)");
10040 : 0 : add_error (std::move (error));
10041 : :
10042 : 0 : return nullptr;
10043 : 0 : }
10044 : :
10045 : 1 : lexer.skip_token ();
10046 : :
10047 : 1 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
10048 : :
10049 : 2 : return AST::MacroInvocation::Regular (
10050 : 2 : AST::MacroInvocData (std::move (macro_path),
10051 : : std::move (tok_tree)),
10052 : 1 : {}, locus);
10053 : 2 : }
10054 : 22699 : default:
10055 : : // assume that this is a type path and not an error
10056 : 22699 : return std::unique_ptr<AST::TypePath> (
10057 : 22699 : new AST::TypePath (std::move (path)));
10058 : : }
10059 : 22700 : }
10060 : 303 : case LEFT_PAREN:
10061 : : /* tuple type or parenthesised type - requires further disambiguation
10062 : : * (the usual). ok apparently can be a parenthesised TraitBound too, so
10063 : : * could be TraitObjectTypeOneBound */
10064 : 303 : return parse_paren_prefixed_type_no_bounds ();
10065 : 2 : case FOR:
10066 : : case ASYNC:
10067 : : case CONST:
10068 : : case UNSAFE:
10069 : : case EXTERN_KW:
10070 : : case FN_KW:
10071 : : // bare function type (with no for lifetimes)
10072 : 2 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
10073 : 30 : case IMPL:
10074 : 30 : lexer.skip_token ();
10075 : 60 : if (lexer.peek_token ()->get_id () == LIFETIME)
10076 : : {
10077 : : /* cannot be one bound because lifetime prevents it from being
10078 : : * traitbound not allowed as type no bounds, only here for error
10079 : : * message */
10080 : 0 : Error error (
10081 : 0 : lexer.peek_token ()->get_locus (),
10082 : : "lifetime (probably lifetime bound, in type param "
10083 : : "bounds, in ImplTraitType) is not allowed in TypeNoBounds");
10084 : 0 : add_error (std::move (error));
10085 : :
10086 : 0 : return nullptr;
10087 : 0 : }
10088 : : else
10089 : : {
10090 : : // should be trait bound, so parse trait bound
10091 : 30 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
10092 : 30 : if (initial_bound == nullptr)
10093 : : {
10094 : 0 : Error error (lexer.peek_token ()->get_locus (),
10095 : : "failed to parse ImplTraitTypeOneBound bound");
10096 : 0 : add_error (std::move (error));
10097 : :
10098 : 0 : return nullptr;
10099 : 0 : }
10100 : :
10101 : 30 : location_t locus = t->get_locus ();
10102 : :
10103 : : // ensure not a trait with multiple bounds
10104 : 30 : t = lexer.peek_token ();
10105 : 30 : if (t->get_id () == PLUS)
10106 : : {
10107 : 0 : Error error (t->get_locus (),
10108 : : "plus after trait bound means an ImplTraitType, "
10109 : : "which is not allowed as a TypeNoBounds");
10110 : 0 : add_error (std::move (error));
10111 : :
10112 : 0 : return nullptr;
10113 : 0 : }
10114 : :
10115 : 30 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
10116 : 30 : new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus));
10117 : 30 : }
10118 : 162 : case DYN:
10119 : : case QUESTION_MARK:
10120 : : {
10121 : : // either TraitObjectTypeOneBound
10122 : 162 : bool has_dyn = false;
10123 : 162 : if (t->get_id () == DYN)
10124 : : {
10125 : 162 : lexer.skip_token ();
10126 : 162 : has_dyn = true;
10127 : : }
10128 : :
10129 : 324 : if (lexer.peek_token ()->get_id () == LIFETIME)
10130 : : {
10131 : : /* means that cannot be TraitObjectTypeOneBound - so here for
10132 : : * error message */
10133 : 0 : Error error (lexer.peek_token ()->get_locus (),
10134 : : "lifetime as bound in TraitObjectTypeOneBound "
10135 : : "is not allowed, so cannot be TypeNoBounds");
10136 : 0 : add_error (std::move (error));
10137 : :
10138 : 0 : return nullptr;
10139 : 0 : }
10140 : :
10141 : : // should be trait bound, so parse trait bound
10142 : 162 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
10143 : 162 : if (initial_bound == nullptr)
10144 : : {
10145 : 0 : Error error (
10146 : 0 : lexer.peek_token ()->get_locus (),
10147 : : "failed to parse TraitObjectTypeOneBound initial bound");
10148 : 0 : add_error (std::move (error));
10149 : :
10150 : 0 : return nullptr;
10151 : 0 : }
10152 : :
10153 : 162 : location_t locus = t->get_locus ();
10154 : :
10155 : : // detect error with plus as next token
10156 : 162 : t = lexer.peek_token ();
10157 : 162 : if (t->get_id () == PLUS)
10158 : : {
10159 : 0 : Error error (t->get_locus (),
10160 : : "plus after trait bound means a TraitObjectType, "
10161 : : "which is not allowed as a TypeNoBounds");
10162 : 0 : add_error (std::move (error));
10163 : :
10164 : 0 : return nullptr;
10165 : 0 : }
10166 : :
10167 : : // convert trait bound to value object
10168 : 162 : AST::TraitBound value_bound (*initial_bound);
10169 : :
10170 : 162 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
10171 : 324 : new AST::TraitObjectTypeOneBound (std::move (value_bound), locus,
10172 : 162 : has_dyn));
10173 : 162 : }
10174 : 0 : default:
10175 : 0 : add_error (Error (t->get_locus (),
10176 : : "unrecognised token %qs in type no bounds",
10177 : : t->get_token_description ()));
10178 : :
10179 : 0 : return nullptr;
10180 : : }
10181 : 29146 : }
10182 : :
10183 : : // Parses a type no bounds beginning with '('.
10184 : : template <typename ManagedTokenSource>
10185 : : std::unique_ptr<AST::TypeNoBounds>
10186 : 303 : Parser<ManagedTokenSource>::parse_paren_prefixed_type_no_bounds ()
10187 : : {
10188 : : /* NOTE: this could probably be parsed without the HACK solution of
10189 : : * parse_paren_prefixed_type, but I was lazy. So FIXME for future.*/
10190 : :
10191 : : /* NOTE: again, syntactical ambiguity of a parenthesised trait bound is
10192 : : * considered a trait bound, not a parenthesised type, so that it can still
10193 : : * be used in type param bounds. */
10194 : :
10195 : 303 : location_t left_paren_locus = lexer.peek_token ()->get_locus ();
10196 : :
10197 : : // skip left delim
10198 : 303 : lexer.skip_token ();
10199 : : /* while next token isn't close delim, parse comma-separated types, saving
10200 : : * whether trailing comma happens */
10201 : 303 : const_TokenPtr t = lexer.peek_token ();
10202 : 303 : bool trailing_comma = true;
10203 : 303 : std::vector<std::unique_ptr<AST::Type>> types;
10204 : :
10205 : 933 : while (t->get_id () != RIGHT_PAREN)
10206 : : {
10207 : 647 : std::unique_ptr<AST::Type> type = parse_type ();
10208 : 647 : if (type == nullptr)
10209 : : {
10210 : 0 : Error error (t->get_locus (),
10211 : : "failed to parse type inside parentheses (probably "
10212 : : "tuple or parenthesised)");
10213 : 0 : add_error (std::move (error));
10214 : :
10215 : 0 : return nullptr;
10216 : 0 : }
10217 : 647 : types.push_back (std::move (type));
10218 : :
10219 : 647 : t = lexer.peek_token ();
10220 : 647 : if (t->get_id () != COMMA)
10221 : : {
10222 : 17 : trailing_comma = false;
10223 : : break;
10224 : : }
10225 : 630 : lexer.skip_token ();
10226 : :
10227 : 630 : t = lexer.peek_token ();
10228 : : }
10229 : :
10230 : 303 : if (!skip_token (RIGHT_PAREN))
10231 : : {
10232 : 0 : return nullptr;
10233 : : }
10234 : :
10235 : : // if only one type and no trailing comma, then not a tuple type
10236 : 303 : if (types.size () == 1 && !trailing_comma)
10237 : : {
10238 : : // must be a TraitObjectType (with more than one bound)
10239 : 22 : if (lexer.peek_token ()->get_id () == PLUS)
10240 : : {
10241 : : // error - this is not allowed for type no bounds
10242 : 0 : Error error (lexer.peek_token ()->get_locus (),
10243 : : "plus (implying TraitObjectType as type param "
10244 : : "bounds) is not allowed in type no bounds");
10245 : 0 : add_error (std::move (error));
10246 : :
10247 : 0 : return nullptr;
10248 : 0 : }
10249 : : else
10250 : : {
10251 : : // release vector pointer
10252 : 11 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
10253 : : /* HACK: attempt to convert to trait bound. if fails, parenthesised
10254 : : * type */
10255 : 11 : std::unique_ptr<AST::TraitBound> converted_bound (
10256 : 11 : released_ptr->to_trait_bound (true));
10257 : 11 : if (converted_bound == nullptr)
10258 : : {
10259 : : // parenthesised type
10260 : 11 : return std::unique_ptr<AST::ParenthesisedType> (
10261 : 11 : new AST::ParenthesisedType (std::move (released_ptr),
10262 : 11 : left_paren_locus));
10263 : : }
10264 : : else
10265 : : {
10266 : : // trait object type (one bound)
10267 : :
10268 : : // get value semantics trait bound
10269 : 0 : AST::TraitBound value_bound (*converted_bound);
10270 : :
10271 : 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
10272 : 0 : new AST::TraitObjectTypeOneBound (value_bound,
10273 : 0 : left_paren_locus));
10274 : 0 : }
10275 : 11 : }
10276 : : }
10277 : : else
10278 : : {
10279 : 292 : return std::unique_ptr<AST::TupleType> (
10280 : 292 : new AST::TupleType (std::move (types), left_paren_locus));
10281 : : }
10282 : : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
10283 : : * lost somehow */
10284 : 303 : }
10285 : :
10286 : : /* Parses a literal pattern or range pattern. Assumes that literals passed in
10287 : : * are valid range pattern bounds. Do not pass in paths in expressions, for
10288 : : * instance. */
10289 : : template <typename ManagedTokenSource>
10290 : : std::unique_ptr<AST::Pattern>
10291 : 45623 : Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
10292 : : {
10293 : 45623 : const_TokenPtr range_lower = lexer.peek_token ();
10294 : 45623 : AST::Literal::LitType type = AST::Literal::STRING;
10295 : 45623 : bool has_minus = false;
10296 : :
10297 : : // get lit type
10298 : 45623 : switch (range_lower->get_id ())
10299 : : {
10300 : 98 : case CHAR_LITERAL:
10301 : 98 : type = AST::Literal::CHAR;
10302 : 98 : lexer.skip_token ();
10303 : : break;
10304 : 87 : case BYTE_CHAR_LITERAL:
10305 : 87 : type = AST::Literal::BYTE;
10306 : 87 : lexer.skip_token ();
10307 : : break;
10308 : 45413 : case INT_LITERAL:
10309 : 45413 : type = AST::Literal::INT;
10310 : 45413 : lexer.skip_token ();
10311 : : break;
10312 : 1 : case FLOAT_LITERAL:
10313 : 1 : type = AST::Literal::FLOAT;
10314 : 1 : lexer.skip_token ();
10315 : : break;
10316 : 24 : case MINUS:
10317 : : // branch on next token
10318 : 24 : range_lower = lexer.peek_token (1);
10319 : 24 : switch (range_lower->get_id ())
10320 : : {
10321 : 24 : case INT_LITERAL:
10322 : 24 : type = AST::Literal::INT;
10323 : 24 : has_minus = true;
10324 : 24 : lexer.skip_token (1);
10325 : 24 : break;
10326 : 0 : case FLOAT_LITERAL:
10327 : 0 : type = AST::Literal::FLOAT;
10328 : 0 : has_minus = true;
10329 : 0 : lexer.skip_token (1);
10330 : 0 : break;
10331 : 0 : default:
10332 : 0 : add_error (Error (range_lower->get_locus (),
10333 : : "token type %qs cannot be parsed as range pattern "
10334 : : "bound or literal after minus symbol",
10335 : : range_lower->get_token_description ()));
10336 : :
10337 : 0 : return nullptr;
10338 : : }
10339 : : break;
10340 : 0 : default:
10341 : 0 : add_error (
10342 : 0 : Error (range_lower->get_locus (),
10343 : : "token type %qs cannot be parsed as range pattern bound",
10344 : : range_lower->get_token_description ()));
10345 : :
10346 : 0 : return nullptr;
10347 : : }
10348 : :
10349 : 45623 : const_TokenPtr next = lexer.peek_token ();
10350 : 45623 : if (next->get_id () == DOT_DOT_EQ || next->get_id () == ELLIPSIS
10351 : 91109 : || next->get_id () == DOT_DOT)
10352 : : {
10353 : 159 : AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
10354 : : // range pattern
10355 : 159 : lexer.skip_token ();
10356 : 159 : std::unique_ptr<AST::RangePatternBound> lower (
10357 : 318 : new AST::RangePatternBoundLiteral (
10358 : 477 : AST::Literal (range_lower->get_str (), type,
10359 : : PrimitiveCoreType::CORETYPE_UNKNOWN),
10360 : : range_lower->get_locus (), has_minus));
10361 : :
10362 : 159 : std::unique_ptr<AST::RangePatternBound> upper
10363 : : = parse_range_pattern_bound ();
10364 : 159 : if (upper == nullptr)
10365 : : {
10366 : 0 : Error error (next->get_locus (),
10367 : : "failed to parse range pattern bound in range pattern");
10368 : 0 : add_error (std::move (error));
10369 : :
10370 : 0 : return nullptr;
10371 : 0 : }
10372 : :
10373 : 159 : return std::unique_ptr<AST::RangePattern> (
10374 : 159 : new AST::RangePattern (std::move (lower), std::move (upper), kind,
10375 : 159 : range_lower->get_locus ()));
10376 : 159 : }
10377 : : else
10378 : : {
10379 : : // literal pattern
10380 : 45464 : return std::unique_ptr<AST::LiteralPattern> (
10381 : 140091 : new AST::LiteralPattern (range_lower->get_str (), type,
10382 : : range_lower->get_locus (),
10383 : 45464 : range_lower->get_type_hint (), has_minus));
10384 : : }
10385 : 45623 : }
10386 : :
10387 : : // Parses a range pattern bound (value only).
10388 : : template <typename ManagedTokenSource>
10389 : : std::unique_ptr<AST::RangePatternBound>
10390 : 167 : Parser<ManagedTokenSource>::parse_range_pattern_bound ()
10391 : : {
10392 : 167 : const_TokenPtr range_lower = lexer.peek_token ();
10393 : 167 : location_t range_lower_locus = range_lower->get_locus ();
10394 : :
10395 : : // get lit type
10396 : 167 : switch (range_lower->get_id ())
10397 : : {
10398 : 52 : case CHAR_LITERAL:
10399 : 52 : lexer.skip_token ();
10400 : 52 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10401 : 104 : new AST::RangePatternBoundLiteral (
10402 : 208 : AST::Literal (range_lower->get_str (), AST::Literal::CHAR,
10403 : : range_lower->get_type_hint ()),
10404 : 52 : range_lower_locus));
10405 : 35 : case BYTE_CHAR_LITERAL:
10406 : 35 : lexer.skip_token ();
10407 : 35 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10408 : 70 : new AST::RangePatternBoundLiteral (
10409 : 140 : AST::Literal (range_lower->get_str (), AST::Literal::BYTE,
10410 : : range_lower->get_type_hint ()),
10411 : 35 : range_lower_locus));
10412 : 55 : case INT_LITERAL:
10413 : 55 : lexer.skip_token ();
10414 : 55 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10415 : 110 : new AST::RangePatternBoundLiteral (
10416 : 195 : AST::Literal (range_lower->get_str (), AST::Literal::INT,
10417 : : range_lower->get_type_hint ()),
10418 : 55 : range_lower_locus));
10419 : 0 : case FLOAT_LITERAL:
10420 : 0 : lexer.skip_token ();
10421 : 0 : rust_debug ("warning: used deprecated float range pattern bound");
10422 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10423 : 0 : new AST::RangePatternBoundLiteral (
10424 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
10425 : : range_lower->get_type_hint ()),
10426 : 0 : range_lower_locus));
10427 : 10 : case MINUS:
10428 : : // branch on next token
10429 : 10 : range_lower = lexer.peek_token (1);
10430 : 10 : switch (range_lower->get_id ())
10431 : : {
10432 : 10 : case INT_LITERAL:
10433 : 10 : lexer.skip_token (1);
10434 : 10 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10435 : 20 : new AST::RangePatternBoundLiteral (
10436 : 30 : AST::Literal (range_lower->get_str (), AST::Literal::INT,
10437 : : range_lower->get_type_hint ()),
10438 : 10 : range_lower_locus, true));
10439 : 0 : case FLOAT_LITERAL:
10440 : 0 : lexer.skip_token (1);
10441 : 0 : rust_debug ("warning: used deprecated float range pattern bound");
10442 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10443 : 0 : new AST::RangePatternBoundLiteral (
10444 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
10445 : : range_lower->get_type_hint ()),
10446 : 0 : range_lower_locus, true));
10447 : 0 : default:
10448 : 0 : add_error (Error (range_lower->get_locus (),
10449 : : "token type %qs cannot be parsed as range pattern "
10450 : : "bound after minus symbol",
10451 : : range_lower->get_token_description ()));
10452 : :
10453 : 0 : return nullptr;
10454 : : }
10455 : 15 : case IDENTIFIER:
10456 : : case SUPER:
10457 : : case SELF:
10458 : : case SELF_ALIAS:
10459 : : case CRATE:
10460 : : case SCOPE_RESOLUTION:
10461 : : case DOLLAR_SIGN:
10462 : : {
10463 : : // path in expression
10464 : 15 : AST::PathInExpression path = parse_path_in_expression ();
10465 : 15 : if (path.is_error ())
10466 : : {
10467 : 0 : Error error (
10468 : : range_lower->get_locus (),
10469 : : "failed to parse path in expression range pattern bound");
10470 : 0 : add_error (std::move (error));
10471 : :
10472 : 0 : return nullptr;
10473 : 0 : }
10474 : 15 : return std::unique_ptr<AST::RangePatternBoundPath> (
10475 : 15 : new AST::RangePatternBoundPath (std::move (path)));
10476 : 15 : }
10477 : 0 : case LEFT_SHIFT:
10478 : : case LEFT_ANGLE:
10479 : : {
10480 : : // qualified path in expression
10481 : 0 : AST::QualifiedPathInExpression path
10482 : : = parse_qualified_path_in_expression ();
10483 : 0 : if (path.is_error ())
10484 : : {
10485 : 0 : Error error (range_lower->get_locus (),
10486 : : "failed to parse qualified path in expression range "
10487 : : "pattern bound");
10488 : 0 : add_error (std::move (error));
10489 : :
10490 : 0 : return nullptr;
10491 : 0 : }
10492 : 0 : return std::unique_ptr<AST::RangePatternBoundQualPath> (
10493 : 0 : new AST::RangePatternBoundQualPath (std::move (path)));
10494 : 0 : }
10495 : 0 : default:
10496 : 0 : add_error (
10497 : 0 : Error (range_lower->get_locus (),
10498 : : "token type %qs cannot be parsed as range pattern bound",
10499 : : range_lower->get_token_description ()));
10500 : :
10501 : 0 : return nullptr;
10502 : : }
10503 : 167 : }
10504 : :
10505 : : template <typename ManagedTokenSource>
10506 : : std::unique_ptr<AST::Pattern>
10507 : 106837 : Parser<ManagedTokenSource>::parse_pattern ()
10508 : : {
10509 : 106837 : location_t start_locus = lexer.peek_token ()->get_locus ();
10510 : :
10511 : : /* skip optional starting pipe */
10512 : 106837 : maybe_skip_token (PIPE);
10513 : :
10514 : 106837 : auto first = parse_pattern_no_alt ();
10515 : :
10516 : 213674 : if (lexer.peek_token ()->get_id () != PIPE)
10517 : : /* no alternates */
10518 : 106593 : return first;
10519 : :
10520 : 244 : std::vector<std::unique_ptr<AST::Pattern>> alts;
10521 : 244 : if (first != nullptr)
10522 : 243 : alts.push_back (std::move (first));
10523 : :
10524 : : do
10525 : : {
10526 : 299 : lexer.skip_token ();
10527 : 299 : auto follow = parse_pattern_no_alt ();
10528 : 299 : if (follow != nullptr)
10529 : 299 : alts.push_back (std::move (follow));
10530 : 299 : }
10531 : :
10532 : 598 : while (lexer.peek_token ()->get_id () == PIPE);
10533 : :
10534 : 244 : if (alts.empty ())
10535 : 0 : return nullptr;
10536 : :
10537 : : /* alternates */
10538 : : return std::unique_ptr<AST::Pattern> (
10539 : 244 : new AST::AltPattern (std::move (alts), start_locus));
10540 : 106837 : }
10541 : :
10542 : : // Parses a pattern without alternates ('|')
10543 : : // (will further disambiguate any pattern).
10544 : : template <typename ManagedTokenSource>
10545 : : std::unique_ptr<AST::Pattern>
10546 : 107701 : Parser<ManagedTokenSource>::parse_pattern_no_alt ()
10547 : : {
10548 : 107701 : const_TokenPtr t = lexer.peek_token ();
10549 : 107701 : switch (t->get_id ())
10550 : : {
10551 : 31 : case TRUE_LITERAL:
10552 : 31 : lexer.skip_token ();
10553 : 31 : return std::unique_ptr<AST::LiteralPattern> (
10554 : 93 : new AST::LiteralPattern (Values::Keywords::TRUE_LITERAL,
10555 : : AST::Literal::BOOL, t->get_locus (),
10556 : 31 : t->get_type_hint ()));
10557 : 23 : case FALSE_LITERAL:
10558 : 23 : lexer.skip_token ();
10559 : 23 : return std::unique_ptr<AST::LiteralPattern> (
10560 : 69 : new AST::LiteralPattern (Values::Keywords::FALSE_LITERAL,
10561 : : AST::Literal::BOOL, t->get_locus (),
10562 : 23 : t->get_type_hint ()));
10563 : 45599 : case CHAR_LITERAL:
10564 : : case BYTE_CHAR_LITERAL:
10565 : : case INT_LITERAL:
10566 : : case FLOAT_LITERAL:
10567 : 45599 : return parse_literal_or_range_pattern ();
10568 : 4 : case STRING_LITERAL:
10569 : 4 : lexer.skip_token ();
10570 : 4 : return std::unique_ptr<AST::LiteralPattern> (
10571 : 16 : new AST::LiteralPattern (t->get_str (), AST::Literal::STRING,
10572 : 4 : t->get_locus (), t->get_type_hint ()));
10573 : 0 : case BYTE_STRING_LITERAL:
10574 : 0 : lexer.skip_token ();
10575 : 0 : return std::unique_ptr<AST::LiteralPattern> (
10576 : 0 : new AST::LiteralPattern (t->get_str (), AST::Literal::BYTE_STRING,
10577 : 0 : t->get_locus (), t->get_type_hint ()));
10578 : 0 : case RAW_STRING_LITERAL:
10579 : 0 : lexer.skip_token ();
10580 : 0 : return std::unique_ptr<AST::LiteralPattern> (
10581 : 0 : new AST::LiteralPattern (t->get_str (), AST::Literal::RAW_STRING,
10582 : 0 : t->get_locus (), t->get_type_hint ()));
10583 : : // raw string and raw byte string literals too if they are readded to
10584 : : // lexer
10585 : 24 : case MINUS:
10586 : 48 : if (lexer.peek_token (1)->get_id () == INT_LITERAL)
10587 : : {
10588 : 24 : return parse_literal_or_range_pattern ();
10589 : : }
10590 : 0 : else if (lexer.peek_token (1)->get_id () == FLOAT_LITERAL)
10591 : : {
10592 : 0 : return parse_literal_or_range_pattern ();
10593 : : }
10594 : : else
10595 : : {
10596 : 0 : Error error (t->get_locus (), "unexpected token %<-%> in pattern - "
10597 : : "did you forget an integer literal");
10598 : 0 : add_error (std::move (error));
10599 : :
10600 : 0 : return nullptr;
10601 : 0 : }
10602 : 5355 : case UNDERSCORE:
10603 : 5355 : lexer.skip_token ();
10604 : 5355 : return std::unique_ptr<AST::WildcardPattern> (
10605 : 5355 : new AST::WildcardPattern (t->get_locus ()));
10606 : 4 : case DOT_DOT:
10607 : 4 : lexer.skip_token ();
10608 : 4 : return std::unique_ptr<AST::RestPattern> (
10609 : 4 : new AST::RestPattern (t->get_locus ()));
10610 : 2027 : case REF:
10611 : : case MUT:
10612 : 2027 : return parse_identifier_pattern ();
10613 : 51459 : case IDENTIFIER:
10614 : : /* if identifier with no scope resolution afterwards, identifier
10615 : : * pattern. if scope resolution afterwards, path pattern (or range
10616 : : * pattern or struct pattern or tuple struct pattern) or macro
10617 : : * invocation */
10618 : 51459 : return parse_ident_leading_pattern ();
10619 : 89 : case AMP:
10620 : : case LOGICAL_AND:
10621 : : // reference pattern
10622 : 89 : return parse_reference_pattern ();
10623 : 2984 : case LEFT_PAREN:
10624 : : // tuple pattern or grouped pattern
10625 : 2984 : return parse_grouped_or_tuple_pattern ();
10626 : 94 : case LEFT_SQUARE:
10627 : : // slice pattern
10628 : 94 : return parse_slice_pattern ();
10629 : 0 : case LEFT_SHIFT:
10630 : : case LEFT_ANGLE:
10631 : : {
10632 : : // qualified path in expression or qualified range pattern bound
10633 : 0 : AST::QualifiedPathInExpression path
10634 : : = parse_qualified_path_in_expression ();
10635 : :
10636 : 0 : if (lexer.peek_token ()->get_id () == DOT_DOT_EQ
10637 : 0 : || lexer.peek_token ()->get_id () == ELLIPSIS
10638 : 0 : || lexer.peek_token ()->get_id () == DOT_DOT)
10639 : : {
10640 : : // qualified range pattern bound, so parse rest of range pattern
10641 : : AST::RangeKind kind
10642 : 0 : = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
10643 : 0 : lexer.skip_token ();
10644 : :
10645 : 0 : std::unique_ptr<AST::RangePatternBoundQualPath> lower_bound (
10646 : 0 : new AST::RangePatternBoundQualPath (std::move (path)));
10647 : 0 : std::unique_ptr<AST::RangePatternBound> upper_bound
10648 : : = parse_range_pattern_bound ();
10649 : :
10650 : 0 : return std::unique_ptr<AST::RangePattern> (
10651 : 0 : new AST::RangePattern (std::move (lower_bound),
10652 : : std::move (upper_bound), kind,
10653 : 0 : t->get_locus ()));
10654 : 0 : }
10655 : : else
10656 : : {
10657 : : // just qualified path in expression
10658 : 0 : return std::unique_ptr<AST::QualifiedPathInExpression> (
10659 : 0 : new AST::QualifiedPathInExpression (std::move (path)));
10660 : : }
10661 : 0 : }
10662 : 7 : case SUPER:
10663 : : case SELF:
10664 : : case SELF_ALIAS:
10665 : : case CRATE:
10666 : : case SCOPE_RESOLUTION:
10667 : : case DOLLAR_SIGN:
10668 : : {
10669 : : // path in expression or range pattern bound
10670 : 7 : AST::PathInExpression path = parse_path_in_expression ();
10671 : :
10672 : 7 : const_TokenPtr next = lexer.peek_token ();
10673 : 7 : switch (next->get_id ())
10674 : : {
10675 : 0 : case DOT_DOT_EQ:
10676 : : case DOT_DOT:
10677 : : case ELLIPSIS:
10678 : : {
10679 : : // qualified range pattern bound, so parse rest of range pattern
10680 : 0 : AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
10681 : 0 : lexer.skip_token ();
10682 : :
10683 : 0 : std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
10684 : 0 : new AST::RangePatternBoundPath (std::move (path)));
10685 : 0 : std::unique_ptr<AST::RangePatternBound> upper_bound
10686 : : = parse_range_pattern_bound ();
10687 : :
10688 : 0 : return std::unique_ptr<AST::RangePattern> (
10689 : 0 : new AST::RangePattern (std::move (lower_bound),
10690 : : std::move (upper_bound), kind,
10691 : 0 : next->get_locus ()));
10692 : 0 : }
10693 : 0 : case EXCLAM:
10694 : 0 : return parse_macro_invocation_partial (std::move (path),
10695 : 0 : AST::AttrVec ());
10696 : 0 : case LEFT_PAREN:
10697 : : {
10698 : : // tuple struct
10699 : 0 : lexer.skip_token ();
10700 : :
10701 : : // parse items
10702 : 0 : std::unique_ptr<AST::TupleStructItems> items
10703 : : = parse_tuple_struct_items ();
10704 : 0 : if (items == nullptr)
10705 : : {
10706 : 0 : Error error (lexer.peek_token ()->get_locus (),
10707 : : "failed to parse tuple struct items");
10708 : 0 : add_error (std::move (error));
10709 : :
10710 : 0 : return nullptr;
10711 : 0 : }
10712 : :
10713 : 0 : if (!skip_token (RIGHT_PAREN))
10714 : : {
10715 : 0 : return nullptr;
10716 : : }
10717 : :
10718 : 0 : return std::unique_ptr<AST::TupleStructPattern> (
10719 : 0 : new AST::TupleStructPattern (std::move (path),
10720 : 0 : std::move (items)));
10721 : 0 : }
10722 : 1 : case LEFT_CURLY:
10723 : : {
10724 : : // struct
10725 : 1 : lexer.skip_token ();
10726 : :
10727 : : // parse elements (optional)
10728 : 1 : AST::StructPatternElements elems = parse_struct_pattern_elems ();
10729 : :
10730 : 1 : if (!skip_token (RIGHT_CURLY))
10731 : : {
10732 : 0 : return nullptr;
10733 : : }
10734 : :
10735 : 1 : return std::unique_ptr<AST::StructPattern> (
10736 : 2 : new AST::StructPattern (std::move (path), t->get_locus (),
10737 : 1 : std::move (elems)));
10738 : 1 : }
10739 : 6 : default:
10740 : : // assume path in expression
10741 : 6 : return std::unique_ptr<AST::PathInExpression> (
10742 : 6 : new AST::PathInExpression (std::move (path)));
10743 : : }
10744 : 7 : }
10745 : 1 : default:
10746 : 1 : add_error (Error (t->get_locus (), "unexpected token %qs in pattern",
10747 : : t->get_token_description ()));
10748 : :
10749 : 1 : return nullptr;
10750 : : }
10751 : 107701 : }
10752 : :
10753 : : // Parses a single or double reference pattern.
10754 : : template <typename ManagedTokenSource>
10755 : : std::unique_ptr<AST::ReferencePattern>
10756 : 89 : Parser<ManagedTokenSource>::parse_reference_pattern ()
10757 : : {
10758 : : // parse double or single ref
10759 : 89 : bool is_double_ref = false;
10760 : 89 : const_TokenPtr t = lexer.peek_token ();
10761 : 89 : switch (t->get_id ())
10762 : : {
10763 : 77 : case AMP:
10764 : : // still false
10765 : 77 : lexer.skip_token ();
10766 : : break;
10767 : 12 : case LOGICAL_AND:
10768 : 12 : is_double_ref = true;
10769 : 12 : lexer.skip_token ();
10770 : : break;
10771 : 0 : default:
10772 : 0 : add_error (Error (t->get_locus (),
10773 : : "unexpected token %qs in reference pattern",
10774 : : t->get_token_description ()));
10775 : :
10776 : 0 : return nullptr;
10777 : : }
10778 : :
10779 : : // parse mut (if it exists)
10780 : 89 : bool is_mut = false;
10781 : 178 : if (lexer.peek_token ()->get_id () == MUT)
10782 : : {
10783 : 4 : is_mut = true;
10784 : 4 : lexer.skip_token ();
10785 : : }
10786 : :
10787 : : // parse pattern to get reference of (required)
10788 : 89 : std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
10789 : 89 : if (pattern == nullptr)
10790 : : {
10791 : 0 : Error error (lexer.peek_token ()->get_locus (),
10792 : : "failed to parse pattern in reference pattern");
10793 : 0 : add_error (std::move (error));
10794 : :
10795 : : // skip somewhere?
10796 : 0 : return nullptr;
10797 : 0 : }
10798 : :
10799 : : return std::unique_ptr<AST::ReferencePattern> (
10800 : 89 : new AST::ReferencePattern (std::move (pattern), is_mut, is_double_ref,
10801 : 89 : t->get_locus ()));
10802 : 89 : }
10803 : :
10804 : : /* Parses a grouped pattern or tuple pattern. Prefers grouped over tuple if
10805 : : * only a single element with no commas. */
10806 : : template <typename ManagedTokenSource>
10807 : : std::unique_ptr<AST::Pattern>
10808 : 2984 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_pattern ()
10809 : : {
10810 : 2984 : location_t paren_locus = lexer.peek_token ()->get_locus ();
10811 : 2984 : skip_token (LEFT_PAREN);
10812 : :
10813 : : // detect '..' token (ranged with no lower range)
10814 : 5968 : if (lexer.peek_token ()->get_id () == DOT_DOT)
10815 : : {
10816 : 0 : lexer.skip_token ();
10817 : :
10818 : : // parse new patterns while next token is a comma
10819 : 0 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
10820 : :
10821 : 0 : const_TokenPtr t = lexer.peek_token ();
10822 : 0 : while (t->get_id () == COMMA)
10823 : : {
10824 : 0 : lexer.skip_token ();
10825 : :
10826 : : // break if next token is ')'
10827 : 0 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10828 : : {
10829 : : break;
10830 : : }
10831 : :
10832 : : // parse pattern, which is required
10833 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10834 : 0 : if (pattern == nullptr)
10835 : : {
10836 : 0 : Error error (
10837 : 0 : lexer.peek_token ()->get_locus (),
10838 : : "failed to parse pattern inside ranged tuple pattern");
10839 : 0 : add_error (std::move (error));
10840 : :
10841 : : // skip somewhere?
10842 : 0 : return nullptr;
10843 : 0 : }
10844 : 0 : patterns.push_back (std::move (pattern));
10845 : :
10846 : 0 : t = lexer.peek_token ();
10847 : : }
10848 : :
10849 : 0 : if (!skip_token (RIGHT_PAREN))
10850 : : {
10851 : : // skip somewhere?
10852 : 0 : return nullptr;
10853 : : }
10854 : :
10855 : : // create tuple pattern items with only upper pattern items
10856 : 0 : std::unique_ptr<AST::TuplePatternItemsHasRest> items (
10857 : 0 : new AST::TuplePatternItemsHasRest (
10858 : 0 : std::vector<std::unique_ptr<AST::Pattern>> (), std::move (patterns)));
10859 : 0 : return std::unique_ptr<AST::TuplePattern> (
10860 : 0 : new AST::TuplePattern (std::move (items), paren_locus));
10861 : 0 : }
10862 : 5968 : else if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10863 : : {
10864 : 23 : skip_token (RIGHT_PAREN);
10865 : 23 : auto items = std::unique_ptr<AST::TuplePatternItemsNoRest> (
10866 : 23 : new AST::TuplePatternItemsNoRest (
10867 : 23 : std::vector<std::unique_ptr<AST::Pattern>> ()));
10868 : 23 : return std::unique_ptr<AST::TuplePattern> (
10869 : 23 : new AST::TuplePattern (std::move (items), paren_locus));
10870 : 23 : }
10871 : :
10872 : : // parse initial pattern (required)
10873 : 2961 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
10874 : 2961 : if (initial_pattern == nullptr)
10875 : : {
10876 : 0 : Error error (lexer.peek_token ()->get_locus (),
10877 : : "failed to parse pattern in grouped or tuple pattern");
10878 : 0 : add_error (std::move (error));
10879 : :
10880 : 0 : return nullptr;
10881 : 0 : }
10882 : :
10883 : : // branch on whether next token is a comma or not
10884 : 2961 : const_TokenPtr t = lexer.peek_token ();
10885 : 2961 : switch (t->get_id ())
10886 : : {
10887 : 44 : case RIGHT_PAREN:
10888 : : // grouped pattern
10889 : 44 : lexer.skip_token ();
10890 : :
10891 : 44 : return std::unique_ptr<AST::GroupedPattern> (
10892 : 44 : new AST::GroupedPattern (std::move (initial_pattern), paren_locus));
10893 : 2917 : case COMMA:
10894 : : {
10895 : : // tuple pattern
10896 : 2917 : lexer.skip_token ();
10897 : :
10898 : : // create vector of patterns
10899 : 2917 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
10900 : 2917 : patterns.push_back (std::move (initial_pattern));
10901 : :
10902 : 2917 : t = lexer.peek_token ();
10903 : 6701 : while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
10904 : : {
10905 : : // parse pattern (required)
10906 : 3784 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10907 : 3784 : if (pattern == nullptr)
10908 : : {
10909 : 0 : Error error (t->get_locus (),
10910 : : "failed to parse pattern in tuple pattern");
10911 : 0 : add_error (std::move (error));
10912 : :
10913 : 0 : return nullptr;
10914 : 0 : }
10915 : 3784 : patterns.push_back (std::move (pattern));
10916 : :
10917 : 7568 : if (lexer.peek_token ()->get_id () != COMMA)
10918 : : break;
10919 : :
10920 : 954 : lexer.skip_token ();
10921 : 954 : t = lexer.peek_token ();
10922 : : }
10923 : :
10924 : 2917 : t = lexer.peek_token ();
10925 : 2917 : if (t->get_id () == RIGHT_PAREN)
10926 : : {
10927 : : // non-ranged tuple pattern
10928 : 2890 : lexer.skip_token ();
10929 : :
10930 : 2890 : std::unique_ptr<AST::TuplePatternItemsNoRest> items (
10931 : 2890 : new AST::TuplePatternItemsNoRest (std::move (patterns)));
10932 : 2890 : return std::unique_ptr<AST::TuplePattern> (
10933 : 2890 : new AST::TuplePattern (std::move (items), paren_locus));
10934 : 2890 : }
10935 : 27 : else if (t->get_id () == DOT_DOT)
10936 : : {
10937 : : // ranged tuple pattern
10938 : 27 : lexer.skip_token ();
10939 : :
10940 : : // parse upper patterns
10941 : 27 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
10942 : 27 : t = lexer.peek_token ();
10943 : 59 : while (t->get_id () == COMMA)
10944 : : {
10945 : 32 : lexer.skip_token ();
10946 : :
10947 : : // break if end
10948 : 64 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10949 : : break;
10950 : :
10951 : : // parse pattern (required)
10952 : 32 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10953 : 32 : if (pattern == nullptr)
10954 : : {
10955 : 0 : Error error (lexer.peek_token ()->get_locus (),
10956 : : "failed to parse pattern in tuple pattern");
10957 : 0 : add_error (std::move (error));
10958 : :
10959 : 0 : return nullptr;
10960 : 0 : }
10961 : 32 : upper_patterns.push_back (std::move (pattern));
10962 : :
10963 : 32 : t = lexer.peek_token ();
10964 : : }
10965 : :
10966 : 27 : if (!skip_token (RIGHT_PAREN))
10967 : : {
10968 : 0 : return nullptr;
10969 : : }
10970 : :
10971 : 27 : std::unique_ptr<AST::TuplePatternItemsHasRest> items (
10972 : 27 : new AST::TuplePatternItemsHasRest (std::move (patterns),
10973 : : std::move (upper_patterns)));
10974 : 27 : return std::unique_ptr<AST::TuplePattern> (
10975 : 27 : new AST::TuplePattern (std::move (items), paren_locus));
10976 : 27 : }
10977 : : else
10978 : : {
10979 : : // some kind of error
10980 : 0 : Error error (t->get_locus (),
10981 : : "failed to parse tuple pattern (probably) or maybe "
10982 : : "grouped pattern");
10983 : 0 : add_error (std::move (error));
10984 : :
10985 : 0 : return nullptr;
10986 : 0 : }
10987 : 2917 : }
10988 : 0 : default:
10989 : : // error
10990 : 0 : add_error (Error (t->get_locus (),
10991 : : "unrecognised token %qs in grouped or tuple pattern "
10992 : : "after first pattern",
10993 : : t->get_token_description ()));
10994 : :
10995 : 0 : return nullptr;
10996 : : }
10997 : 2961 : }
10998 : :
10999 : : /* Parses a slice pattern that can match arrays or slices. Parses the square
11000 : : * brackets too. */
11001 : : template <typename ManagedTokenSource>
11002 : : std::unique_ptr<AST::SlicePattern>
11003 : 94 : Parser<ManagedTokenSource>::parse_slice_pattern ()
11004 : : {
11005 : 188 : location_t square_locus = lexer.peek_token ()->get_locus ();
11006 : 94 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
11007 : 94 : tl::optional<std::vector<std::unique_ptr<AST::Pattern>>> upper_patterns
11008 : : = tl::nullopt;
11009 : :
11010 : : // lambda function to determine which vector to push new patterns into
11011 : 94 : auto get_pattern_ref
11012 : 87 : = [&] () -> std::vector<std::unique_ptr<AST::Pattern>> & {
11013 : 87 : return upper_patterns.has_value () ? upper_patterns.value () : patterns;
11014 : : };
11015 : :
11016 : 94 : skip_token (LEFT_SQUARE);
11017 : :
11018 : 188 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
11019 : : {
11020 : 5 : skip_token (RIGHT_SQUARE);
11021 : 5 : std::unique_ptr<AST::SlicePatternItemsNoRest> items (
11022 : 5 : new AST::SlicePatternItemsNoRest (std::move (patterns)));
11023 : : return std::unique_ptr<AST::SlicePattern> (
11024 : 5 : new AST::SlicePattern (std::move (items), square_locus));
11025 : 5 : }
11026 : :
11027 : : // parse initial pattern (required)
11028 : 178 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11029 : : {
11030 : 3 : lexer.skip_token ();
11031 : 3 : upper_patterns = std::vector<std::unique_ptr<AST::Pattern>> ();
11032 : : }
11033 : : else
11034 : : {
11035 : : // Not a rest pattern `..`, parse normally
11036 : 86 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
11037 : 86 : if (initial_pattern == nullptr)
11038 : : {
11039 : 0 : Error error (lexer.peek_token ()->get_locus (),
11040 : : "failed to parse initial pattern in slice pattern");
11041 : 0 : add_error (std::move (error));
11042 : :
11043 : 0 : return nullptr;
11044 : 0 : }
11045 : :
11046 : 86 : patterns.push_back (std::move (initial_pattern));
11047 : 86 : }
11048 : :
11049 : 89 : const_TokenPtr t = lexer.peek_token ();
11050 : 225 : while (t->get_id () == COMMA)
11051 : : {
11052 : 136 : lexer.skip_token ();
11053 : :
11054 : : // break if end bracket
11055 : 272 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
11056 : : break;
11057 : :
11058 : 272 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11059 : : {
11060 : 49 : if (upper_patterns.has_value ())
11061 : : {
11062 : : // DOT_DOT has been parsed before
11063 : 0 : Error error (lexer.peek_token ()->get_locus (), "%s",
11064 : : "`..` can only be used once per slice pattern");
11065 : 0 : add_error (std::move (error));
11066 : :
11067 : 0 : return nullptr;
11068 : 0 : }
11069 : 49 : upper_patterns = std::vector<std::unique_ptr<AST::Pattern>> ();
11070 : 49 : lexer.skip_token ();
11071 : 49 : t = lexer.peek_token ();
11072 : 49 : continue;
11073 : 49 : }
11074 : :
11075 : : // parse pattern (required)
11076 : 87 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11077 : 87 : if (pattern == nullptr)
11078 : : {
11079 : 0 : Error error (lexer.peek_token ()->get_locus (),
11080 : : "failed to parse pattern in slice pattern");
11081 : 0 : add_error (std::move (error));
11082 : :
11083 : 0 : return nullptr;
11084 : 0 : }
11085 : 87 : get_pattern_ref ().push_back (std::move (pattern));
11086 : :
11087 : 87 : t = lexer.peek_token ();
11088 : : }
11089 : :
11090 : 89 : if (!skip_token (RIGHT_SQUARE))
11091 : : {
11092 : 0 : return nullptr;
11093 : : }
11094 : :
11095 : 89 : if (upper_patterns.has_value ())
11096 : : {
11097 : : // Slice pattern with rest
11098 : 52 : std::unique_ptr<AST::SlicePatternItemsHasRest> items (
11099 : 104 : new AST::SlicePatternItemsHasRest (
11100 : 52 : std::move (patterns), std::move (upper_patterns.value ())));
11101 : : return std::unique_ptr<AST::SlicePattern> (
11102 : 52 : new AST::SlicePattern (std::move (items), square_locus));
11103 : 52 : }
11104 : :
11105 : : // Rest-less slice pattern
11106 : 37 : std::unique_ptr<AST::SlicePatternItemsNoRest> items (
11107 : 37 : new AST::SlicePatternItemsNoRest (std::move (patterns)));
11108 : : return std::unique_ptr<AST::SlicePattern> (
11109 : 37 : new AST::SlicePattern (std::move (items), square_locus));
11110 : 220 : }
11111 : :
11112 : : /* Parses an identifier pattern (pattern that binds a value matched to a
11113 : : * variable). */
11114 : : template <typename ManagedTokenSource>
11115 : : std::unique_ptr<AST::IdentifierPattern>
11116 : 2027 : Parser<ManagedTokenSource>::parse_identifier_pattern ()
11117 : : {
11118 : 2027 : location_t locus = lexer.peek_token ()->get_locus ();
11119 : :
11120 : 2027 : bool has_ref = false;
11121 : 4054 : if (lexer.peek_token ()->get_id () == REF)
11122 : : {
11123 : 232 : has_ref = true;
11124 : 232 : lexer.skip_token ();
11125 : :
11126 : : // DEBUG
11127 : 232 : rust_debug ("parsed ref in identifier pattern");
11128 : : }
11129 : :
11130 : 2027 : bool has_mut = false;
11131 : 4054 : if (lexer.peek_token ()->get_id () == MUT)
11132 : : {
11133 : 1849 : has_mut = true;
11134 : 1849 : lexer.skip_token ();
11135 : : }
11136 : :
11137 : : // parse identifier (required)
11138 : 2027 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
11139 : 2027 : if (ident_tok == nullptr)
11140 : : {
11141 : : // skip somewhere?
11142 : 0 : return nullptr;
11143 : : }
11144 : 2027 : Identifier ident{ident_tok};
11145 : :
11146 : : // DEBUG
11147 : 2027 : rust_debug ("parsed identifier in identifier pattern");
11148 : :
11149 : : // parse optional pattern binding thing
11150 : 2027 : std::unique_ptr<AST::Pattern> bind_pattern = nullptr;
11151 : 4054 : if (lexer.peek_token ()->get_id () == PATTERN_BIND)
11152 : : {
11153 : 1 : lexer.skip_token ();
11154 : :
11155 : : // parse required pattern to bind
11156 : 1 : bind_pattern = parse_pattern_no_alt ();
11157 : 1 : if (bind_pattern == nullptr)
11158 : : {
11159 : 0 : Error error (lexer.peek_token ()->get_locus (),
11160 : : "failed to parse pattern to bind in identifier pattern");
11161 : 0 : add_error (std::move (error));
11162 : :
11163 : 0 : return nullptr;
11164 : 0 : }
11165 : : }
11166 : :
11167 : : // DEBUG
11168 : 2027 : rust_debug ("about to return identifier pattern");
11169 : :
11170 : : return std::unique_ptr<AST::IdentifierPattern> (
11171 : 2027 : new AST::IdentifierPattern (std::move (ident), locus, has_ref, has_mut,
11172 : 2027 : std::move (bind_pattern)));
11173 : 2027 : }
11174 : :
11175 : : /* Parses a pattern that opens with an identifier. This includes identifier
11176 : : * patterns, path patterns (and derivatives such as struct patterns, tuple
11177 : : * struct patterns, and macro invocations), and ranges. */
11178 : : template <typename ManagedTokenSource>
11179 : : std::unique_ptr<AST::Pattern>
11180 : 51459 : Parser<ManagedTokenSource>::parse_ident_leading_pattern ()
11181 : : {
11182 : : // ensure first token is actually identifier
11183 : 51459 : const_TokenPtr initial_tok = lexer.peek_token ();
11184 : 51459 : if (initial_tok->get_id () != IDENTIFIER)
11185 : : {
11186 : 0 : return nullptr;
11187 : : }
11188 : :
11189 : : // save initial identifier as it may be useful (but don't skip)
11190 : 51459 : std::string initial_ident = initial_tok->get_str ();
11191 : :
11192 : : // parse next tokens as a PathInExpression
11193 : 51459 : AST::PathInExpression path = parse_path_in_expression ();
11194 : :
11195 : : // branch on next token
11196 : 51459 : const_TokenPtr t = lexer.peek_token ();
11197 : 51459 : switch (t->get_id ())
11198 : : {
11199 : 1 : case EXCLAM:
11200 : 2 : return parse_macro_invocation_partial (std::move (path), AST::AttrVec ());
11201 : 1654 : case LEFT_PAREN:
11202 : : {
11203 : : // tuple struct
11204 : 1654 : lexer.skip_token ();
11205 : :
11206 : : // DEBUG
11207 : 1654 : rust_debug ("parsing tuple struct pattern");
11208 : :
11209 : : // parse items
11210 : 1654 : std::unique_ptr<AST::TupleStructItems> items
11211 : : = parse_tuple_struct_items ();
11212 : 1654 : if (items == nullptr)
11213 : : {
11214 : 0 : Error error (lexer.peek_token ()->get_locus (),
11215 : : "failed to parse tuple struct items");
11216 : 0 : add_error (std::move (error));
11217 : :
11218 : 0 : return nullptr;
11219 : 0 : }
11220 : :
11221 : : // DEBUG
11222 : 1654 : rust_debug ("successfully parsed tuple struct items");
11223 : :
11224 : 1654 : if (!skip_token (RIGHT_PAREN))
11225 : : {
11226 : 0 : return nullptr;
11227 : : }
11228 : :
11229 : : // DEBUG
11230 : 1654 : rust_debug ("successfully parsed tuple struct pattern");
11231 : :
11232 : 1654 : return std::unique_ptr<AST::TupleStructPattern> (
11233 : 1654 : new AST::TupleStructPattern (std::move (path), std::move (items)));
11234 : 1654 : }
11235 : 135 : case LEFT_CURLY:
11236 : : {
11237 : : // struct
11238 : 135 : lexer.skip_token ();
11239 : :
11240 : : // parse elements (optional)
11241 : 135 : AST::StructPatternElements elems = parse_struct_pattern_elems ();
11242 : :
11243 : 135 : if (!skip_token (RIGHT_CURLY))
11244 : : {
11245 : 0 : return nullptr;
11246 : : }
11247 : :
11248 : : // DEBUG
11249 : 135 : rust_debug ("successfully parsed struct pattern");
11250 : :
11251 : 135 : return std::unique_ptr<AST::StructPattern> (
11252 : 270 : new AST::StructPattern (std::move (path), initial_tok->get_locus (),
11253 : 135 : std::move (elems)));
11254 : 135 : }
11255 : 8 : case DOT_DOT_EQ:
11256 : : case DOT_DOT:
11257 : : case ELLIPSIS:
11258 : : {
11259 : : // range
11260 : : AST::RangeKind kind
11261 : 8 : = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
11262 : :
11263 : 8 : lexer.skip_token ();
11264 : :
11265 : 8 : std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
11266 : 8 : new AST::RangePatternBoundPath (std::move (path)));
11267 : 8 : std::unique_ptr<AST::RangePatternBound> upper_bound
11268 : : = parse_range_pattern_bound ();
11269 : :
11270 : 8 : return std::unique_ptr<AST::RangePattern> (
11271 : 8 : new AST::RangePattern (std::move (lower_bound),
11272 : : std::move (upper_bound), kind,
11273 : 8 : t->get_locus ()));
11274 : 8 : }
11275 : 41 : case PATTERN_BIND:
11276 : : {
11277 : : // only allow on single-segment paths
11278 : 41 : if (path.is_single_segment ())
11279 : : {
11280 : : // identifier with pattern bind
11281 : 41 : lexer.skip_token ();
11282 : :
11283 : 41 : std::unique_ptr<AST::Pattern> bind_pattern
11284 : : = parse_pattern_no_alt ();
11285 : 41 : if (bind_pattern == nullptr)
11286 : : {
11287 : 1 : Error error (
11288 : : t->get_locus (),
11289 : : "failed to parse pattern to bind to identifier pattern");
11290 : 1 : add_error (std::move (error));
11291 : :
11292 : 1 : return nullptr;
11293 : 1 : }
11294 : 40 : return std::unique_ptr<AST::IdentifierPattern> (
11295 : 120 : new AST::IdentifierPattern (std::move (initial_ident),
11296 : : initial_tok->get_locus (), false,
11297 : 40 : false, std::move (bind_pattern)));
11298 : 41 : }
11299 : 0 : Error error (
11300 : : t->get_locus (),
11301 : : "failed to parse pattern bind to a path, not an identifier");
11302 : 0 : add_error (std::move (error));
11303 : :
11304 : 0 : return nullptr;
11305 : 0 : }
11306 : 49620 : default:
11307 : : // assume identifier if single segment
11308 : 49620 : if (path.is_single_segment ())
11309 : : {
11310 : 48528 : return std::unique_ptr<AST::IdentifierPattern> (
11311 : 145584 : new AST::IdentifierPattern (std::move (initial_ident),
11312 : 48528 : initial_tok->get_locus ()));
11313 : : }
11314 : : // return path otherwise
11315 : 1092 : return std::unique_ptr<AST::PathInExpression> (
11316 : 1092 : new AST::PathInExpression (std::move (path)));
11317 : : }
11318 : 51459 : }
11319 : :
11320 : : // Parses tuple struct items if they exist. Does not parse parentheses.
11321 : : template <typename ManagedTokenSource>
11322 : : std::unique_ptr<AST::TupleStructItems>
11323 : 1654 : Parser<ManagedTokenSource>::parse_tuple_struct_items ()
11324 : : {
11325 : 1654 : std::vector<std::unique_ptr<AST::Pattern>> lower_patterns;
11326 : :
11327 : : // DEBUG
11328 : 1654 : rust_debug ("started parsing tuple struct items");
11329 : :
11330 : : // check for '..' at front
11331 : 3308 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11332 : : {
11333 : : // only parse upper patterns
11334 : 23 : lexer.skip_token ();
11335 : :
11336 : : // DEBUG
11337 : 23 : rust_debug ("'..' at front in tuple struct items detected");
11338 : :
11339 : 23 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
11340 : :
11341 : 23 : const_TokenPtr t = lexer.peek_token ();
11342 : 40 : while (t->get_id () == COMMA)
11343 : : {
11344 : 17 : lexer.skip_token ();
11345 : :
11346 : : // break if right paren
11347 : 34 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
11348 : : break;
11349 : :
11350 : : // parse pattern, which is now required
11351 : 17 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11352 : 17 : if (pattern == nullptr)
11353 : : {
11354 : 0 : Error error (lexer.peek_token ()->get_locus (),
11355 : : "failed to parse pattern in tuple struct items");
11356 : 0 : add_error (std::move (error));
11357 : :
11358 : 0 : return nullptr;
11359 : 0 : }
11360 : 17 : upper_patterns.push_back (std::move (pattern));
11361 : :
11362 : 17 : t = lexer.peek_token ();
11363 : : }
11364 : :
11365 : : // DEBUG
11366 : 23 : rust_debug (
11367 : : "finished parsing tuple struct items ranged (upper/none only)");
11368 : :
11369 : 23 : return std::unique_ptr<AST::TupleStructItemsHasRest> (
11370 : 23 : new AST::TupleStructItemsHasRest (std::move (lower_patterns),
11371 : 23 : std::move (upper_patterns)));
11372 : 23 : }
11373 : :
11374 : : // has at least some lower patterns
11375 : 1631 : const_TokenPtr t = lexer.peek_token ();
11376 : 3329 : while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
11377 : : {
11378 : : // DEBUG
11379 : 1698 : rust_debug ("about to parse pattern in tuple struct items");
11380 : :
11381 : : // parse pattern, which is required
11382 : 1698 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11383 : 1698 : if (pattern == nullptr)
11384 : : {
11385 : 0 : Error error (t->get_locus (),
11386 : : "failed to parse pattern in tuple struct items");
11387 : 0 : add_error (std::move (error));
11388 : :
11389 : 0 : return nullptr;
11390 : 0 : }
11391 : 1698 : lower_patterns.push_back (std::move (pattern));
11392 : :
11393 : : // DEBUG
11394 : 1698 : rust_debug ("successfully parsed pattern in tuple struct items");
11395 : :
11396 : 3396 : if (lexer.peek_token ()->get_id () != COMMA)
11397 : : {
11398 : : // DEBUG
11399 : 1585 : rust_debug ("broke out of parsing patterns in tuple struct "
11400 : : "items as no comma");
11401 : :
11402 : : break;
11403 : : }
11404 : 113 : lexer.skip_token ();
11405 : 113 : t = lexer.peek_token ();
11406 : : }
11407 : :
11408 : : // branch on next token
11409 : 1631 : t = lexer.peek_token ();
11410 : 1631 : switch (t->get_id ())
11411 : : {
11412 : 1608 : case RIGHT_PAREN:
11413 : 1608 : return std::unique_ptr<AST::TupleStructItemsNoRest> (
11414 : 1608 : new AST::TupleStructItemsNoRest (std::move (lower_patterns)));
11415 : 23 : case DOT_DOT:
11416 : : {
11417 : : // has an upper range that must be parsed separately
11418 : 23 : lexer.skip_token ();
11419 : :
11420 : 23 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
11421 : :
11422 : 23 : t = lexer.peek_token ();
11423 : 25 : while (t->get_id () == COMMA)
11424 : : {
11425 : 2 : lexer.skip_token ();
11426 : :
11427 : : // break if next token is right paren
11428 : 4 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
11429 : : break;
11430 : :
11431 : : // parse pattern, which is required
11432 : 2 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11433 : 2 : if (pattern == nullptr)
11434 : : {
11435 : 0 : Error error (lexer.peek_token ()->get_locus (),
11436 : : "failed to parse pattern in tuple struct items");
11437 : 0 : add_error (std::move (error));
11438 : :
11439 : 0 : return nullptr;
11440 : 0 : }
11441 : 2 : upper_patterns.push_back (std::move (pattern));
11442 : :
11443 : 2 : t = lexer.peek_token ();
11444 : : }
11445 : :
11446 : 23 : return std::unique_ptr<AST::TupleStructItemsHasRest> (
11447 : 23 : new AST::TupleStructItemsHasRest (std::move (lower_patterns),
11448 : 23 : std::move (upper_patterns)));
11449 : 23 : }
11450 : 0 : default:
11451 : : // error
11452 : 0 : add_error (Error (t->get_locus (),
11453 : : "unexpected token %qs in tuple struct items",
11454 : : t->get_token_description ()));
11455 : :
11456 : 0 : return nullptr;
11457 : : }
11458 : 1654 : }
11459 : :
11460 : : // Parses struct pattern elements if they exist.
11461 : : template <typename ManagedTokenSource>
11462 : : AST::StructPatternElements
11463 : 136 : Parser<ManagedTokenSource>::parse_struct_pattern_elems ()
11464 : : {
11465 : 136 : std::vector<std::unique_ptr<AST::StructPatternField>> fields;
11466 : :
11467 : 136 : AST::AttrVec etc_attrs;
11468 : 136 : bool has_rest = false;
11469 : :
11470 : : // try parsing struct pattern fields
11471 : 136 : const_TokenPtr t = lexer.peek_token ();
11472 : 418 : while (t->get_id () != RIGHT_CURLY)
11473 : : {
11474 : 202 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11475 : :
11476 : : // parse etc (must be last in struct pattern, so breaks)
11477 : 404 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11478 : : {
11479 : 3 : lexer.skip_token ();
11480 : 3 : etc_attrs = std::move (outer_attrs);
11481 : 3 : has_rest = true;
11482 : 3 : break;
11483 : : }
11484 : :
11485 : 199 : std::unique_ptr<AST::StructPatternField> field
11486 : 199 : = parse_struct_pattern_field_partial (std::move (outer_attrs));
11487 : 199 : if (field == nullptr)
11488 : : {
11489 : 0 : Error error (lexer.peek_token ()->get_locus (),
11490 : : "failed to parse struct pattern field");
11491 : 0 : add_error (std::move (error));
11492 : :
11493 : : // skip after somewhere?
11494 : 0 : return AST::StructPatternElements::create_empty ();
11495 : 0 : }
11496 : 199 : fields.push_back (std::move (field));
11497 : :
11498 : 398 : if (lexer.peek_token ()->get_id () != COMMA)
11499 : : break;
11500 : :
11501 : : // skip comma
11502 : 80 : lexer.skip_token ();
11503 : 80 : t = lexer.peek_token ();
11504 : : }
11505 : :
11506 : 136 : if (has_rest)
11507 : 3 : return AST::StructPatternElements (std::move (fields),
11508 : 3 : std::move (etc_attrs));
11509 : : else
11510 : 133 : return AST::StructPatternElements (std::move (fields));
11511 : 136 : }
11512 : :
11513 : : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
11514 : : * identifier). */
11515 : : template <typename ManagedTokenSource>
11516 : : std::unique_ptr<AST::StructPatternField>
11517 : 0 : Parser<ManagedTokenSource>::parse_struct_pattern_field ()
11518 : : {
11519 : : // parse outer attributes (if they exist)
11520 : 0 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11521 : :
11522 : 0 : return parse_struct_pattern_field_partial (std::move (outer_attrs));
11523 : 0 : }
11524 : :
11525 : : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
11526 : : * identifier), with outer attributes passed in. */
11527 : : template <typename ManagedTokenSource>
11528 : : std::unique_ptr<AST::StructPatternField>
11529 : 199 : Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
11530 : : AST::AttrVec outer_attrs)
11531 : : {
11532 : : // branch based on next token
11533 : 199 : const_TokenPtr t = lexer.peek_token ();
11534 : 199 : switch (t->get_id ())
11535 : : {
11536 : 3 : case INT_LITERAL:
11537 : : {
11538 : : // tuple index
11539 : 6 : std::string index_str = t->get_str ();
11540 : 3 : int index = atoi (index_str.c_str ());
11541 : :
11542 : 3 : lexer.skip_token ();
11543 : :
11544 : 3 : if (!skip_token (COLON))
11545 : : {
11546 : 0 : return nullptr;
11547 : : }
11548 : :
11549 : : // parse required pattern
11550 : 3 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11551 : 3 : if (pattern == nullptr)
11552 : : {
11553 : 0 : Error error (
11554 : : t->get_locus (),
11555 : : "failed to parse pattern in tuple index struct pattern field");
11556 : 0 : add_error (std::move (error));
11557 : :
11558 : 0 : return nullptr;
11559 : 0 : }
11560 : :
11561 : 3 : return std::unique_ptr<AST::StructPatternFieldTuplePat> (
11562 : 3 : new AST::StructPatternFieldTuplePat (index, std::move (pattern),
11563 : : std::move (outer_attrs),
11564 : 3 : t->get_locus ()));
11565 : 6 : }
11566 : 196 : case IDENTIFIER:
11567 : : // identifier-pattern OR only identifier
11568 : : // branch on next token
11569 : 392 : switch (lexer.peek_token (1)->get_id ())
11570 : : {
11571 : 98 : case COLON:
11572 : : {
11573 : : // identifier-pattern
11574 : 98 : Identifier ident{t};
11575 : 98 : lexer.skip_token ();
11576 : :
11577 : 98 : skip_token (COLON);
11578 : :
11579 : : // parse required pattern
11580 : 98 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11581 : 98 : if (pattern == nullptr)
11582 : : {
11583 : 0 : Error error (t->get_locus (),
11584 : : "failed to parse pattern in struct pattern field");
11585 : 0 : add_error (std::move (error));
11586 : :
11587 : 0 : return nullptr;
11588 : 0 : }
11589 : :
11590 : 98 : return std::unique_ptr<AST::StructPatternFieldIdentPat> (
11591 : 196 : new AST::StructPatternFieldIdentPat (std::move (ident),
11592 : : std::move (pattern),
11593 : : std::move (outer_attrs),
11594 : 98 : t->get_locus ()));
11595 : 98 : }
11596 : 98 : case COMMA:
11597 : : case RIGHT_CURLY:
11598 : : {
11599 : : // identifier only
11600 : 98 : Identifier ident = {t};
11601 : 98 : lexer.skip_token ();
11602 : :
11603 : 98 : return std::unique_ptr<AST::StructPatternFieldIdent> (
11604 : 196 : new AST::StructPatternFieldIdent (std::move (ident), false, false,
11605 : : std::move (outer_attrs),
11606 : 98 : t->get_locus ()));
11607 : 98 : }
11608 : 0 : default:
11609 : : // error
11610 : 0 : add_error (Error (t->get_locus (),
11611 : : "unrecognised token %qs in struct pattern field",
11612 : : t->get_token_description ()));
11613 : :
11614 : 0 : return nullptr;
11615 : : }
11616 : 0 : case REF:
11617 : : case MUT:
11618 : : {
11619 : : // only identifier
11620 : 0 : bool has_ref = false;
11621 : 0 : if (t->get_id () == REF)
11622 : : {
11623 : 0 : has_ref = true;
11624 : 0 : lexer.skip_token ();
11625 : : }
11626 : :
11627 : 0 : bool has_mut = false;
11628 : 0 : if (lexer.peek_token ()->get_id () == MUT)
11629 : : {
11630 : 0 : has_mut = true;
11631 : 0 : lexer.skip_token ();
11632 : : }
11633 : :
11634 : 0 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
11635 : 0 : if (ident_tok == nullptr)
11636 : : {
11637 : 0 : return nullptr;
11638 : : }
11639 : 0 : Identifier ident{ident_tok};
11640 : :
11641 : 0 : return std::unique_ptr<AST::StructPatternFieldIdent> (
11642 : 0 : new AST::StructPatternFieldIdent (std::move (ident), has_ref, has_mut,
11643 : : std::move (outer_attrs),
11644 : 0 : t->get_locus ()));
11645 : 0 : }
11646 : 0 : default:
11647 : : // not necessarily an error
11648 : 0 : return nullptr;
11649 : : }
11650 : 199 : }
11651 : :
11652 : : /* Parses a statement or expression (depending on whether a trailing semicolon
11653 : : * exists). Useful for block expressions where it cannot be determined through
11654 : : * lookahead whether it is a statement or expression to be parsed. */
11655 : : template <typename ManagedTokenSource>
11656 : : ExprOrStmt
11657 : 70391 : Parser<ManagedTokenSource>::parse_stmt_or_expr ()
11658 : : {
11659 : : // quick exit for empty statement
11660 : 70391 : const_TokenPtr t = lexer.peek_token ();
11661 : 70391 : if (t->get_id () == SEMICOLON)
11662 : : {
11663 : 17 : lexer.skip_token ();
11664 : 17 : std::unique_ptr<AST::EmptyStmt> stmt (
11665 : 17 : new AST::EmptyStmt (t->get_locus ()));
11666 : 17 : return ExprOrStmt (std::move (stmt));
11667 : 17 : }
11668 : :
11669 : : // parse outer attributes
11670 : 70374 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11671 : 70374 : ParseRestrictions restrictions;
11672 : 70374 : restrictions.expr_can_be_stmt = true;
11673 : 70374 : std::unique_ptr<AST::Expr> expr;
11674 : :
11675 : : // parsing this will be annoying because of the many different possibilities
11676 : : /* best may be just to copy paste in parse_item switch, and failing that try
11677 : : * to parse outer attributes, and then pass them in to either a let
11678 : : * statement or (fallback) expression statement. */
11679 : : // FIXME: think of a way to do this without such a large switch?
11680 : :
11681 : : /* FIXME: for expressions at least, the only way that they can really be
11682 : : * parsed properly in this way is if they don't support operators on them.
11683 : : * They must be pratt-parsed otherwise. As such due to composability, only
11684 : : * explicit statements will have special cases here. This should roughly
11685 : : * correspond to "expr-with-block", but this warning is here in case it
11686 : : * isn't the case. */
11687 : 70374 : t = lexer.peek_token ();
11688 : 70374 : switch (t->get_id ())
11689 : : {
11690 : 22542 : case LET:
11691 : : {
11692 : : // let statement
11693 : 22542 : std::unique_ptr<AST::LetStmt> stmt (
11694 : 22542 : parse_let_stmt (std::move (outer_attrs)));
11695 : 22542 : return ExprOrStmt (std::move (stmt));
11696 : 22542 : }
11697 : 838 : case PUB:
11698 : : case MOD:
11699 : : case EXTERN_KW:
11700 : : case USE:
11701 : : case FN_KW:
11702 : : case TYPE:
11703 : : case STRUCT_KW:
11704 : : case ENUM_KW:
11705 : : case CONST:
11706 : : case STATIC_KW:
11707 : : case AUTO:
11708 : : case TRAIT:
11709 : : case IMPL:
11710 : : {
11711 : 838 : std::unique_ptr<AST::VisItem> item (
11712 : 838 : parse_vis_item (std::move (outer_attrs)));
11713 : 838 : return ExprOrStmt (std::move (item));
11714 : 838 : }
11715 : : /* TODO: implement union keyword but not really because of
11716 : : * context-dependence crappy hack way to parse a union written below to
11717 : : * separate it from the good code. */
11718 : : // case UNION:
11719 : 3878 : case UNSAFE:
11720 : : { // maybe - unsafe traits are a thing
11721 : : /* if any of these (should be all possible VisItem prefixes), parse a
11722 : : * VisItem - can't parse item because would require reparsing outer
11723 : : * attributes */
11724 : 3878 : const_TokenPtr t2 = lexer.peek_token (1);
11725 : 3878 : switch (t2->get_id ())
11726 : : {
11727 : 3861 : case LEFT_CURLY:
11728 : : {
11729 : : // unsafe block: parse as expression
11730 : 3861 : expr = parse_expr (std::move (outer_attrs), restrictions);
11731 : : break;
11732 : : }
11733 : 0 : case AUTO:
11734 : : case TRAIT:
11735 : : {
11736 : : // unsafe trait
11737 : 0 : std::unique_ptr<AST::VisItem> item (
11738 : 0 : parse_vis_item (std::move (outer_attrs)));
11739 : 0 : return ExprOrStmt (std::move (item));
11740 : 0 : }
11741 : 17 : case EXTERN_KW:
11742 : : case FN_KW:
11743 : : {
11744 : : // unsafe function
11745 : 17 : std::unique_ptr<AST::VisItem> item (
11746 : 17 : parse_vis_item (std::move (outer_attrs)));
11747 : 17 : return ExprOrStmt (std::move (item));
11748 : 17 : }
11749 : 0 : case IMPL:
11750 : : {
11751 : : // unsafe trait impl
11752 : 0 : std::unique_ptr<AST::VisItem> item (
11753 : 0 : parse_vis_item (std::move (outer_attrs)));
11754 : 0 : return ExprOrStmt (std::move (item));
11755 : 0 : }
11756 : 0 : default:
11757 : 0 : add_error (Error (t2->get_locus (),
11758 : : "unrecognised token %qs after parsing unsafe - "
11759 : : "expected beginning of expression or statement",
11760 : : t->get_token_description ()));
11761 : :
11762 : : // skip somewhere?
11763 : : return ExprOrStmt::create_error ();
11764 : : }
11765 : : break;
11766 : 3878 : }
11767 : : /* FIXME: this is either a macro invocation or macro invocation semi.
11768 : : * start parsing to determine which one it is. */
11769 : : // FIXME: old code there
11770 : :
11771 : : // crappy hack to do union "keyword"
11772 : 25909 : case IDENTIFIER:
11773 : 42873 : if (t->get_str () == Values::WeakKeywords::UNION
11774 : 25945 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
11775 : : {
11776 : 1 : std::unique_ptr<AST::VisItem> item (
11777 : 1 : parse_vis_item (std::move (outer_attrs)));
11778 : 1 : return ExprOrStmt (std::move (item));
11779 : : // or should this go straight to parsing union?
11780 : 1 : }
11781 : 42871 : else if (t->get_str () == Values::WeakKeywords::MACRO_RULES
11782 : 26424 : && lexer.peek_token (1)->get_id () == EXCLAM)
11783 : : {
11784 : : // macro_rules! macro item
11785 : 516 : std::unique_ptr<AST::Item> item (
11786 : 516 : parse_macro_rules_def (std::move (outer_attrs)));
11787 : 516 : return ExprOrStmt (std::move (item));
11788 : 516 : }
11789 : : gcc_fallthrough ();
11790 : : case SUPER:
11791 : : case SELF:
11792 : : case SELF_ALIAS:
11793 : : case CRATE:
11794 : : case SCOPE_RESOLUTION:
11795 : : case DOLLAR_SIGN:
11796 : : {
11797 : 30290 : AST::PathInExpression path = parse_path_in_expression ();
11798 : 30290 : std::unique_ptr<AST::Expr> null_denotation;
11799 : :
11800 : 60580 : if (lexer.peek_token ()->get_id () == EXCLAM)
11801 : : {
11802 : 2529 : std::unique_ptr<AST::MacroInvocation> invoc
11803 : 5058 : = parse_macro_invocation_partial (std::move (path),
11804 : : std::move (outer_attrs));
11805 : 2529 : if (invoc == nullptr)
11806 : : return ExprOrStmt::create_error ();
11807 : :
11808 : 2529 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
11809 : : {
11810 : 1362 : invoc->add_semicolon ();
11811 : : // Macro invocation with semicolon.
11812 : : return ExprOrStmt (
11813 : 1362 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
11814 : : }
11815 : :
11816 : 2334 : TokenId after_macro = lexer.peek_token ()->get_id ();
11817 : :
11818 : 1167 : AST::DelimType delim_type = invoc->get_invoc_data ()
11819 : 1167 : .get_delim_tok_tree ()
11820 : 1167 : .get_delim_type ();
11821 : :
11822 : 1167 : if (delim_type == AST::CURLY && after_macro != DOT
11823 : 6 : && after_macro != QUESTION_MARK)
11824 : : {
11825 : 6 : rust_debug ("braced macro statement");
11826 : : return ExprOrStmt (
11827 : 6 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
11828 : : }
11829 : :
11830 : 1161 : null_denotation = std::move (invoc);
11831 : 2529 : }
11832 : : else
11833 : : {
11834 : : null_denotation
11835 : 27761 : = null_denotation_path (std::move (path), {}, restrictions);
11836 : : }
11837 : :
11838 : 28922 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
11839 : : std::move (outer_attrs), restrictions);
11840 : : break;
11841 : 30290 : }
11842 : 12309 : default:
11843 : : /* expression statement or expression itself - parse
11844 : : * expression then make it statement if semi afterwards */
11845 : 12309 : expr = parse_expr (std::move (outer_attrs), restrictions);
11846 : 12309 : break;
11847 : : }
11848 : :
11849 : 45092 : const_TokenPtr after_expr = lexer.peek_token ();
11850 : 45092 : if (after_expr->get_id () == SEMICOLON)
11851 : : {
11852 : : // must be expression statement
11853 : 12426 : lexer.skip_token ();
11854 : :
11855 : 12426 : if (expr)
11856 : : {
11857 : 12425 : std::unique_ptr<AST::ExprStmt> stmt (
11858 : 12425 : new AST::ExprStmt (std::move (expr), t->get_locus (), true));
11859 : 12425 : return ExprOrStmt (std::move (stmt));
11860 : 12425 : }
11861 : : else
11862 : : {
11863 : : return ExprOrStmt::create_error ();
11864 : : }
11865 : : }
11866 : :
11867 : 32660 : if (expr && !expr->is_expr_without_block ()
11868 : 41422 : && after_expr->get_id () != RIGHT_CURLY)
11869 : : {
11870 : : // block expression statement.
11871 : 2449 : std::unique_ptr<AST::ExprStmt> stmt (
11872 : 2449 : new AST::ExprStmt (std::move (expr), t->get_locus (), false));
11873 : 2449 : return ExprOrStmt (std::move (stmt));
11874 : 2449 : }
11875 : :
11876 : : // return expression
11877 : 30217 : return ExprOrStmt (std::move (expr));
11878 : 70374 : }
11879 : :
11880 : : // Parses a struct expression field.
11881 : : template <typename ManagedTokenSource>
11882 : : std::unique_ptr<AST::StructExprField>
11883 : 2860 : Parser<ManagedTokenSource>::parse_struct_expr_field ()
11884 : : {
11885 : 2860 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11886 : 2860 : const_TokenPtr t = lexer.peek_token ();
11887 : 2860 : switch (t->get_id ())
11888 : : {
11889 : 2816 : case IDENTIFIER:
11890 : 5632 : if (lexer.peek_token (1)->get_id () == COLON)
11891 : : {
11892 : : // struct expr field with identifier and expr
11893 : 2429 : Identifier ident = {t};
11894 : 2429 : lexer.skip_token (1);
11895 : :
11896 : : // parse expression (required)
11897 : 2429 : std::unique_ptr<AST::Expr> expr = parse_expr ();
11898 : 2429 : if (expr == nullptr)
11899 : : {
11900 : 0 : Error error (t->get_locus (),
11901 : : "failed to parse struct expression field with "
11902 : : "identifier and expression");
11903 : 0 : add_error (std::move (error));
11904 : :
11905 : 0 : return nullptr;
11906 : 0 : }
11907 : :
11908 : 2429 : return std::unique_ptr<AST::StructExprFieldIdentifierValue> (
11909 : 4858 : new AST::StructExprFieldIdentifierValue (std::move (ident),
11910 : : std::move (expr),
11911 : : std::move (outer_attrs),
11912 : 2429 : t->get_locus ()));
11913 : 2429 : }
11914 : : else
11915 : : {
11916 : : // struct expr field with identifier only
11917 : 387 : Identifier ident{t};
11918 : 387 : lexer.skip_token ();
11919 : :
11920 : 387 : return std::unique_ptr<AST::StructExprFieldIdentifier> (
11921 : 774 : new AST::StructExprFieldIdentifier (std::move (ident),
11922 : : std::move (outer_attrs),
11923 : 387 : t->get_locus ()));
11924 : 387 : }
11925 : 44 : case INT_LITERAL:
11926 : : {
11927 : : // parse tuple index field
11928 : 44 : int index = atoi (t->get_str ().c_str ());
11929 : 44 : lexer.skip_token ();
11930 : :
11931 : 44 : if (!skip_token (COLON))
11932 : : {
11933 : : // skip somewhere?
11934 : 0 : return nullptr;
11935 : : }
11936 : :
11937 : : // parse field expression (required)
11938 : 44 : std::unique_ptr<AST::Expr> expr = parse_expr ();
11939 : 44 : if (expr == nullptr)
11940 : : {
11941 : 0 : Error error (t->get_locus (),
11942 : : "failed to parse expr in struct (or enum) expr "
11943 : : "field with tuple index");
11944 : 0 : add_error (std::move (error));
11945 : :
11946 : 0 : return nullptr;
11947 : 0 : }
11948 : :
11949 : 44 : return std::unique_ptr<AST::StructExprFieldIndexValue> (
11950 : 44 : new AST::StructExprFieldIndexValue (index, std::move (expr),
11951 : : std::move (outer_attrs),
11952 : 44 : t->get_locus ()));
11953 : 44 : }
11954 : 0 : case DOT_DOT:
11955 : : /* this is a struct base and can't be parsed here, so just return
11956 : : * nothing without erroring */
11957 : :
11958 : 0 : return nullptr;
11959 : 0 : default:
11960 : 0 : add_error (
11961 : 0 : Error (t->get_locus (),
11962 : : "unrecognised token %qs as first token of struct expr field - "
11963 : : "expected identifier or integer literal",
11964 : : t->get_token_description ()));
11965 : :
11966 : 0 : return nullptr;
11967 : : }
11968 : 2860 : }
11969 : :
11970 : : // "Unexpected token" panic mode - flags gcc error at unexpected token
11971 : : // TODO: seems to be unused, remove?
11972 : : template <typename ManagedTokenSource>
11973 : : void
11974 : 0 : Parser<ManagedTokenSource>::unexpected_token (const_TokenPtr t)
11975 : : {
11976 : 0 : Error error (t->get_locus (), "unexpected token %qs",
11977 : : t->get_token_description ());
11978 : 0 : add_error (std::move (error));
11979 : 0 : }
11980 : :
11981 : : /* Crappy "error recovery" performed after error by skipping tokens until a
11982 : : * semi-colon is found */
11983 : : template <typename ManagedTokenSource>
11984 : : void
11985 : 24 : Parser<ManagedTokenSource>::skip_after_semicolon ()
11986 : : {
11987 : 24 : const_TokenPtr t = lexer.peek_token ();
11988 : :
11989 : 31 : while (t->get_id () != END_OF_FILE && t->get_id () != SEMICOLON)
11990 : : {
11991 : 7 : lexer.skip_token ();
11992 : 7 : t = lexer.peek_token ();
11993 : : }
11994 : :
11995 : 24 : if (t->get_id () == SEMICOLON)
11996 : 3 : lexer.skip_token ();
11997 : 24 : }
11998 : :
11999 : : /* Skips the current token */
12000 : : template <typename ManagedTokenSource>
12001 : : void
12002 : 70228 : Parser<ManagedTokenSource>::skip_token ()
12003 : : {
12004 : 2117 : lexer.skip_token ();
12005 : 1538 : }
12006 : :
12007 : : /* Checks if current token has inputted id - skips it and returns true if so,
12008 : : * diagnoses an error and returns false otherwise. */
12009 : : template <typename ManagedTokenSource>
12010 : : bool
12011 : 1067176 : Parser<ManagedTokenSource>::skip_token (TokenId token_id)
12012 : : {
12013 : 1067176 : return expect_token (token_id) != const_TokenPtr ();
12014 : : }
12015 : :
12016 : : /* Checks if current token is similar to inputted token - skips it and returns
12017 : : * true if so, diagnoses an error and returns false otherwise. */
12018 : : template <typename ManagedTokenSource>
12019 : : bool
12020 : 134761 : Parser<ManagedTokenSource>::skip_token (const_TokenPtr token)
12021 : : {
12022 : 403562 : return expect_token (token) != const_TokenPtr ();
12023 : : }
12024 : :
12025 : : /* Checks if current token has inputted id - skips it and returns true if so,
12026 : : * returns false otherwise without diagnosing an error */
12027 : : template <typename ManagedTokenSource>
12028 : : bool
12029 : 146955 : Parser<ManagedTokenSource>::maybe_skip_token (TokenId token_id)
12030 : : {
12031 : 293910 : if (lexer.peek_token ()->get_id () != token_id)
12032 : : return false;
12033 : : else
12034 : 6968 : return skip_token (token_id);
12035 : : }
12036 : :
12037 : : /* Checks the current token - if id is same as expected, skips and returns it,
12038 : : * otherwise diagnoses error and returns null. */
12039 : : template <typename ManagedTokenSource>
12040 : : const_TokenPtr
12041 : 1140331 : Parser<ManagedTokenSource>::expect_token (TokenId token_id)
12042 : : {
12043 : 1140331 : const_TokenPtr t = lexer.peek_token ();
12044 : 1140331 : if (t->get_id () == token_id)
12045 : : {
12046 : 1136718 : lexer.skip_token ();
12047 : 1136718 : return t;
12048 : : }
12049 : : else
12050 : : {
12051 : 3613 : Error error (t->get_locus (), "expecting %qs but %qs found",
12052 : : get_token_description (token_id),
12053 : : t->get_token_description ());
12054 : 3613 : add_error (std::move (error));
12055 : :
12056 : 3613 : return const_TokenPtr ();
12057 : 3613 : }
12058 : 1140331 : }
12059 : :
12060 : : /* Checks the current token - if same as expected, skips and returns it,
12061 : : * otherwise diagnoses error and returns null. */
12062 : : template <typename ManagedTokenSource>
12063 : : const_TokenPtr
12064 : 134761 : Parser<ManagedTokenSource>::expect_token (const_TokenPtr token_expect)
12065 : : {
12066 : 134761 : const_TokenPtr t = lexer.peek_token ();
12067 : 134761 : if (t->get_id () == token_expect->get_id ()
12068 : 134761 : && (!t->should_have_str () || t->get_str () == token_expect->get_str ()))
12069 : : {
12070 : 134040 : lexer.skip_token ();
12071 : 134040 : return t;
12072 : : }
12073 : : else
12074 : : {
12075 : 721 : Error error (t->get_locus (), "expecting %qs but %qs found",
12076 : : token_expect->get_token_description (),
12077 : : t->get_token_description ());
12078 : 721 : add_error (std::move (error));
12079 : :
12080 : 721 : return const_TokenPtr ();
12081 : 721 : }
12082 : 134761 : }
12083 : :
12084 : : // Skips all tokens until EOF or }. Don't use.
12085 : : template <typename ManagedTokenSource>
12086 : : void
12087 : 0 : Parser<ManagedTokenSource>::skip_after_end ()
12088 : : {
12089 : 0 : const_TokenPtr t = lexer.peek_token ();
12090 : :
12091 : 0 : while (t->get_id () != END_OF_FILE && t->get_id () != RIGHT_CURLY)
12092 : : {
12093 : 0 : lexer.skip_token ();
12094 : 0 : t = lexer.peek_token ();
12095 : : }
12096 : :
12097 : 0 : if (t->get_id () == RIGHT_CURLY)
12098 : : {
12099 : 0 : lexer.skip_token ();
12100 : : }
12101 : 0 : }
12102 : :
12103 : : /* A slightly more aware error-handler that skips all tokens until it reaches
12104 : : * the end of the block scope (i.e. when left curly brackets = right curly
12105 : : * brackets). Note: assumes currently in the middle of a block. Use
12106 : : * skip_after_next_block to skip based on the assumption that the block
12107 : : * has not been entered yet. */
12108 : : template <typename ManagedTokenSource>
12109 : : void
12110 : 1 : Parser<ManagedTokenSource>::skip_after_end_block ()
12111 : : {
12112 : 1 : const_TokenPtr t = lexer.peek_token ();
12113 : 1 : int curly_count = 1;
12114 : :
12115 : 2 : while (curly_count > 0 && t->get_id () != END_OF_FILE)
12116 : : {
12117 : 1 : switch (t->get_id ())
12118 : : {
12119 : 0 : case LEFT_CURLY:
12120 : 0 : curly_count++;
12121 : 0 : break;
12122 : 1 : case RIGHT_CURLY:
12123 : 1 : curly_count--;
12124 : 1 : break;
12125 : : default:
12126 : : break;
12127 : : }
12128 : 1 : lexer.skip_token ();
12129 : 1 : t = lexer.peek_token ();
12130 : : }
12131 : 1 : }
12132 : :
12133 : : /* Skips tokens until the end of the next block. i.e. assumes that the block
12134 : : * has not been entered yet. */
12135 : : template <typename ManagedTokenSource>
12136 : : void
12137 : 1 : Parser<ManagedTokenSource>::skip_after_next_block ()
12138 : : {
12139 : 1 : const_TokenPtr t = lexer.peek_token ();
12140 : :
12141 : : // initial loop - skip until EOF if no left curlies encountered
12142 : 1 : while (t->get_id () != END_OF_FILE && t->get_id () != LEFT_CURLY)
12143 : : {
12144 : 0 : lexer.skip_token ();
12145 : :
12146 : 0 : t = lexer.peek_token ();
12147 : : }
12148 : :
12149 : : // if next token is left, skip it and then skip after the block ends
12150 : 1 : if (t->get_id () == LEFT_CURLY)
12151 : : {
12152 : 1 : lexer.skip_token ();
12153 : :
12154 : 1 : skip_after_end_block ();
12155 : : }
12156 : : // otherwise, do nothing as EOF
12157 : 1 : }
12158 : :
12159 : : /* Skips all tokens until ] (the end of an attribute) - does not skip the ]
12160 : : * (as designed for attribute body use) */
12161 : : template <typename ManagedTokenSource>
12162 : : void
12163 : 0 : Parser<ManagedTokenSource>::skip_after_end_attribute ()
12164 : : {
12165 : 0 : const_TokenPtr t = lexer.peek_token ();
12166 : :
12167 : 0 : while (t->get_id () != RIGHT_SQUARE && t->get_id () != END_OF_FILE)
12168 : : {
12169 : 0 : lexer.skip_token ();
12170 : 0 : t = lexer.peek_token ();
12171 : : }
12172 : :
12173 : : // Don't skip the RIGHT_SQUARE token
12174 : 0 : }
12175 : :
12176 : : /* Pratt parser impl of parse_expr. FIXME: this is only provisional and
12177 : : * probably will be changed. */
12178 : : template <typename ManagedTokenSource>
12179 : : std::unique_ptr<AST::Expr>
12180 : 725769 : Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
12181 : : AST::AttrVec outer_attrs,
12182 : : ParseRestrictions restrictions)
12183 : : {
12184 : 725769 : const_TokenPtr current_token = lexer.peek_token ();
12185 : : // Special hack because we are allowed to return nullptr, in that case we
12186 : : // don't want to skip the token, since we don't actually parse it. But if
12187 : : // null isn't allowed it indicates an error, and we want to skip past that.
12188 : : // So return early if it is one of the tokens that ends an expression
12189 : : // (or at least cannot start a new expression).
12190 : 725769 : if (restrictions.expr_can_be_null)
12191 : : {
12192 : 2207 : TokenId id = current_token->get_id ();
12193 : 2207 : if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY
12194 : : || id == RIGHT_SQUARE || id == COMMA || id == LEFT_CURLY)
12195 : 244 : return nullptr;
12196 : : }
12197 : :
12198 : 725525 : ParseRestrictions null_denotation_restrictions = restrictions;
12199 : 725525 : null_denotation_restrictions.expr_can_be_stmt = false;
12200 : :
12201 : : // parse null denotation (unary part of expression)
12202 : 725525 : std::unique_ptr<AST::Expr> expr
12203 : 725525 : = null_denotation ({}, null_denotation_restrictions);
12204 : :
12205 : 725525 : return left_denotations (std::move (expr), right_binding_power,
12206 : 725525 : std::move (outer_attrs), restrictions);
12207 : 725525 : }
12208 : :
12209 : : template <typename ManagedTokenSource>
12210 : : std::unique_ptr<AST::Expr>
12211 : 754630 : Parser<ManagedTokenSource>::left_denotations (std::unique_ptr<AST::Expr> expr,
12212 : : int right_binding_power,
12213 : : AST::AttrVec outer_attrs,
12214 : : ParseRestrictions restrictions)
12215 : : {
12216 : 754630 : if (expr == nullptr)
12217 : : {
12218 : : // DEBUG
12219 : 33 : rust_debug ("null denotation is null; returning null for parse_expr");
12220 : 33 : return nullptr;
12221 : : }
12222 : :
12223 : 754597 : const_TokenPtr current_token = lexer.peek_token ();
12224 : :
12225 : 95201 : if (restrictions.expr_can_be_stmt && !expr->is_expr_without_block ()
12226 : 10230 : && current_token->get_id () != DOT
12227 : 764818 : && current_token->get_id () != QUESTION_MARK)
12228 : : {
12229 : 10221 : rust_debug ("statement expression with block");
12230 : 10221 : expr->set_outer_attrs (std::move (outer_attrs));
12231 : 10221 : return expr;
12232 : : }
12233 : :
12234 : 840192 : restrictions.expr_can_be_stmt = false;
12235 : :
12236 : : // stop parsing if find lower priority token - parse higher priority first
12237 : 2520576 : while (right_binding_power < left_binding_power (current_token))
12238 : : {
12239 : 95818 : lexer.skip_token ();
12240 : :
12241 : : // FIXME attributes should generally be applied to the null denotation.
12242 : 287454 : expr = left_denotation (current_token, std::move (expr),
12243 : : std::move (outer_attrs), restrictions);
12244 : :
12245 : 95818 : if (expr == nullptr)
12246 : : {
12247 : : // DEBUG
12248 : 2 : rust_debug ("left denotation is null; returning null for parse_expr");
12249 : :
12250 : 2 : return nullptr;
12251 : : }
12252 : :
12253 : 95816 : current_token = lexer.peek_token ();
12254 : : }
12255 : :
12256 : 744374 : return expr;
12257 : 754597 : }
12258 : :
12259 : : // Parse expression with lowest left binding power.
12260 : : template <typename ManagedTokenSource>
12261 : : std::unique_ptr<AST::Expr>
12262 : 669600 : Parser<ManagedTokenSource>::parse_expr (AST::AttrVec outer_attrs,
12263 : : ParseRestrictions restrictions)
12264 : : {
12265 : 669600 : return parse_expr (LBP_LOWEST, std::move (outer_attrs), restrictions);
12266 : : }
12267 : :
12268 : : /* Determines action to take when finding token at beginning of expression. */
12269 : : template <typename ManagedTokenSource>
12270 : : std::unique_ptr<AST::Expr>
12271 : 725525 : Parser<ManagedTokenSource>::null_denotation (AST::AttrVec outer_attrs,
12272 : : ParseRestrictions restrictions)
12273 : : {
12274 : : /* note: tok is previous character in input stream, not current one, as
12275 : : * parse_expr skips it before passing it in */
12276 : :
12277 : : /* as a Pratt parser (which works by decomposing expressions into a null
12278 : : * denotation and then a left denotation), null denotations handle primaries
12279 : : * and unary operands (but only prefix unary operands) */
12280 : :
12281 : 725525 : auto tok = lexer.peek_token ();
12282 : :
12283 : 725525 : switch (tok->get_id ())
12284 : : {
12285 : 283089 : case IDENTIFIER:
12286 : : case SELF:
12287 : : case SELF_ALIAS:
12288 : : case DOLLAR_SIGN:
12289 : : case CRATE:
12290 : : case SUPER:
12291 : : case SCOPE_RESOLUTION:
12292 : : {
12293 : : // DEBUG
12294 : 283089 : rust_debug ("beginning null denotation identifier handling");
12295 : :
12296 : : /* best option: parse as path, then extract identifier, macro,
12297 : : * struct/enum, or just path info from it */
12298 : 283089 : AST::PathInExpression path = parse_path_in_expression ();
12299 : :
12300 : 566178 : return null_denotation_path (std::move (path), std::move (outer_attrs),
12301 : 283089 : restrictions);
12302 : 283089 : }
12303 : 442436 : default:
12304 : 442436 : if (tok->get_id () == LEFT_SHIFT)
12305 : : {
12306 : 2 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
12307 : 2 : tok = lexer.peek_token ();
12308 : : }
12309 : :
12310 : 442436 : lexer.skip_token ();
12311 : 884872 : return null_denotation_not_path (std::move (tok), std::move (outer_attrs),
12312 : 442436 : restrictions);
12313 : : }
12314 : 725525 : }
12315 : :
12316 : : // Handling of expresions that start with a path for `null_denotation`.
12317 : : template <typename ManagedTokenSource>
12318 : : std::unique_ptr<AST::Expr>
12319 : 311033 : Parser<ManagedTokenSource>::null_denotation_path (
12320 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
12321 : : ParseRestrictions restrictions)
12322 : : {
12323 : 311033 : rust_debug ("parsing null denotation after path");
12324 : :
12325 : : // HACK: always make "self" by itself a path (regardless of next
12326 : : // tokens)
12327 : 311033 : if (path.is_single_segment () && path.get_segments ()[0].is_lower_self_seg ())
12328 : : {
12329 : : // HACK: add outer attrs to path
12330 : 20011 : path.set_outer_attrs (std::move (outer_attrs));
12331 : 20011 : return std::unique_ptr<AST::PathInExpression> (
12332 : 20011 : new AST::PathInExpression (std::move (path)));
12333 : : }
12334 : :
12335 : : // branch on next token
12336 : 291022 : const_TokenPtr t = lexer.peek_token ();
12337 : 291022 : switch (t->get_id ())
12338 : : {
12339 : 49952 : case EXCLAM:
12340 : : // macro
12341 : 99904 : return parse_macro_invocation_partial (std::move (path),
12342 : 49952 : std::move (outer_attrs));
12343 : 3794 : case LEFT_CURLY:
12344 : : {
12345 : 3794 : bool not_a_block = lexer.peek_token (1)->get_id () == IDENTIFIER
12346 : 10294 : && (lexer.peek_token (2)->get_id () == COMMA
12347 : 9172 : || (lexer.peek_token (2)->get_id () == COLON
12348 : 5980 : && (lexer.peek_token (4)->get_id () == COMMA
12349 : 815 : || !can_tok_start_type (
12350 : 1960 : lexer.peek_token (3)->get_id ()))));
12351 : :
12352 : : /* definitely not a block:
12353 : : * path '{' ident ','
12354 : : * path '{' ident ':' [anything] ','
12355 : : * path '{' ident ':' [not a type]
12356 : : * otherwise, assume block expr and thus path */
12357 : : // DEBUG
12358 : 15176 : rust_debug ("values of lookahead: '%s' '%s' '%s' '%s' ",
12359 : : lexer.peek_token (1)->get_token_description (),
12360 : : lexer.peek_token (2)->get_token_description (),
12361 : : lexer.peek_token (3)->get_token_description (),
12362 : : lexer.peek_token (4)->get_token_description ());
12363 : :
12364 : 8543 : rust_debug ("can be struct expr: '%s', not a block: '%s'",
12365 : : restrictions.can_be_struct_expr ? "true" : "false",
12366 : : not_a_block ? "true" : "false");
12367 : :
12368 : : // struct/enum expr struct
12369 : 3794 : if (!restrictions.can_be_struct_expr && !not_a_block)
12370 : : {
12371 : : // HACK: add outer attrs to path
12372 : 2100 : path.set_outer_attrs (std::move (outer_attrs));
12373 : 2100 : return std::unique_ptr<AST::PathInExpression> (
12374 : 2100 : new AST::PathInExpression (std::move (path)));
12375 : : }
12376 : 1694 : return parse_struct_expr_struct_partial (std::move (path),
12377 : 1694 : std::move (outer_attrs));
12378 : : }
12379 : 79124 : case LEFT_PAREN:
12380 : : // struct/enum expr tuple
12381 : 79124 : if (!restrictions.can_be_struct_expr)
12382 : : {
12383 : : // assume path is returned
12384 : : // HACK: add outer attributes to path
12385 : 380 : path.set_outer_attrs (std::move (outer_attrs));
12386 : 380 : return std::unique_ptr<AST::PathInExpression> (
12387 : 380 : new AST::PathInExpression (std::move (path)));
12388 : : }
12389 : 78744 : return parse_struct_expr_tuple_partial (std::move (path),
12390 : 78744 : std::move (outer_attrs));
12391 : 158152 : default:
12392 : : // assume path is returned if not single segment
12393 : 158152 : if (path.is_single_segment ())
12394 : : {
12395 : : // FIXME: This should probably be returned as a path.
12396 : : /* HACK: may have to become permanent, but this is my current
12397 : : * identifier expression */
12398 : 465765 : return std::unique_ptr<AST::IdentifierExpr> (new AST::IdentifierExpr (
12399 : 621020 : path.get_segments ()[0].get_ident_segment ().as_string (), {},
12400 : 155255 : path.get_locus ()));
12401 : : }
12402 : : // HACK: add outer attrs to path
12403 : 2897 : path.set_outer_attrs (std::move (outer_attrs));
12404 : 2897 : return std::unique_ptr<AST::PathInExpression> (
12405 : 2897 : new AST::PathInExpression (std::move (path)));
12406 : : }
12407 : : rust_unreachable ();
12408 : 291022 : }
12409 : :
12410 : : // Handling of expresions that do not start with a path for `null_denotation`.
12411 : : template <typename ManagedTokenSource>
12412 : : std::unique_ptr<AST::Expr>
12413 : 442436 : Parser<ManagedTokenSource>::null_denotation_not_path (
12414 : : const_TokenPtr tok, AST::AttrVec outer_attrs, ParseRestrictions restrictions)
12415 : : {
12416 : 442436 : switch (tok->get_id ())
12417 : : {
12418 : : // FIXME: Handle in null_denotation_path?
12419 : 187 : case LEFT_SHIFT:
12420 : : case LEFT_ANGLE:
12421 : : {
12422 : : // qualified path
12423 : : // HACK: add outer attrs to path
12424 : 187 : AST::QualifiedPathInExpression path
12425 : : = parse_qualified_path_in_expression (tok->get_locus ());
12426 : 187 : path.set_outer_attrs (std::move (outer_attrs));
12427 : 187 : return std::unique_ptr<AST::QualifiedPathInExpression> (
12428 : 187 : new AST::QualifiedPathInExpression (std::move (path)));
12429 : 187 : }
12430 : : // FIXME: delegate to parse_literal_expr instead? would have to rejig
12431 : : // tokens and whatever.
12432 : : // FIXME: for literal exprs, outer attrs should be passed in, and later
12433 : : // error if it does not make up the entire statement.
12434 : 350717 : case INT_LITERAL:
12435 : : // we should check the range, but ignore for now
12436 : : // encode as int?
12437 : 350717 : return std::unique_ptr<AST::LiteralExpr> (
12438 : 1061702 : new AST::LiteralExpr (tok->get_str (), AST::Literal::INT,
12439 : 350717 : tok->get_type_hint (), {}, tok->get_locus ()));
12440 : 13761 : case FLOAT_LITERAL:
12441 : : // encode as float?
12442 : 13761 : return std::unique_ptr<AST::LiteralExpr> (
12443 : 55044 : new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT,
12444 : 13761 : tok->get_type_hint (), {}, tok->get_locus ()));
12445 : 3193 : case STRING_LITERAL:
12446 : 3193 : return std::unique_ptr<AST::LiteralExpr> (
12447 : 12772 : new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING,
12448 : 3193 : tok->get_type_hint (), {}, tok->get_locus ()));
12449 : 137 : case BYTE_STRING_LITERAL:
12450 : 137 : return std::unique_ptr<AST::LiteralExpr> (
12451 : 548 : new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING,
12452 : 137 : tok->get_type_hint (), {}, tok->get_locus ()));
12453 : 103 : case RAW_STRING_LITERAL:
12454 : 103 : return std::unique_ptr<AST::LiteralExpr> (
12455 : 412 : new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING,
12456 : 103 : tok->get_type_hint (), {}, tok->get_locus ()));
12457 : 11747 : case CHAR_LITERAL:
12458 : 11747 : return std::unique_ptr<AST::LiteralExpr> (
12459 : 46988 : new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,
12460 : 11747 : tok->get_type_hint (), {}, tok->get_locus ()));
12461 : 132 : case BYTE_CHAR_LITERAL:
12462 : 132 : return std::unique_ptr<AST::LiteralExpr> (
12463 : 528 : new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE,
12464 : 132 : tok->get_type_hint (), {}, tok->get_locus ()));
12465 : 882 : case TRUE_LITERAL:
12466 : 882 : return std::unique_ptr<AST::LiteralExpr> (
12467 : 2646 : new AST::LiteralExpr (Values::Keywords::TRUE_LITERAL,
12468 : : AST::Literal::BOOL, tok->get_type_hint (), {},
12469 : 882 : tok->get_locus ()));
12470 : 755 : case FALSE_LITERAL:
12471 : 755 : return std::unique_ptr<AST::LiteralExpr> (
12472 : 2265 : new AST::LiteralExpr (Values::Keywords::FALSE_LITERAL,
12473 : : AST::Literal::BOOL, tok->get_type_hint (), {},
12474 : 755 : tok->get_locus ()));
12475 : 12550 : case LEFT_PAREN:
12476 : 12550 : return parse_grouped_or_tuple_expr (std::move (outer_attrs),
12477 : 12550 : tok->get_locus ());
12478 : :
12479 : : /*case PLUS: { // unary plus operator
12480 : : // invoke parse_expr recursively with appropriate priority, etc. for
12481 : : below AST::Expr* expr = parse_expr(LBP_UNARY_PLUS);
12482 : :
12483 : : if (expr == nullptr)
12484 : : return nullptr;
12485 : : // can only apply to integer and float expressions
12486 : : if (expr->get_type() != integer_type_node || expr->get_type() !=
12487 : : float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
12488 : : plus must be int or float but it is %s", print_type(expr->get_type()));
12489 : : return nullptr;
12490 : : }
12491 : :
12492 : : return Tree(expr, tok->get_locus());
12493 : : }*/
12494 : : // Rust has no unary plus operator
12495 : 3447 : case MINUS:
12496 : : { // unary minus
12497 : 3447 : ParseRestrictions entered_from_unary;
12498 : 3447 : entered_from_unary.entered_from_unary = true;
12499 : 3447 : if (!restrictions.can_be_struct_expr)
12500 : 14 : entered_from_unary.can_be_struct_expr = false;
12501 : 3447 : std::unique_ptr<AST::Expr> expr
12502 : 3447 : = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary);
12503 : :
12504 : 3447 : if (expr == nullptr)
12505 : 0 : return nullptr;
12506 : : // can only apply to integer and float expressions
12507 : : /*if (expr.get_type() != integer_type_node || expr.get_type() !=
12508 : : float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
12509 : : minus must be int or float but it is %s",
12510 : : print_type(expr.get_type())); return Tree::error();
12511 : : }*/
12512 : : /* FIXME: when implemented the "get type" method on expr, ensure it is
12513 : : * int or float type (except unsigned int). Actually, this would
12514 : : * probably have to be done in semantic analysis (as type checking).
12515 : : */
12516 : :
12517 : : /* FIXME: allow outer attributes on these expressions by having an
12518 : : * outer attrs parameter in function*/
12519 : 3447 : return std::unique_ptr<AST::NegationExpr> (
12520 : 3447 : new AST::NegationExpr (std::move (expr), NegationOperator::NEGATE,
12521 : 3447 : std::move (outer_attrs), tok->get_locus ()));
12522 : 3447 : }
12523 : 615 : case EXCLAM:
12524 : : { // logical or bitwise not
12525 : 615 : ParseRestrictions entered_from_unary;
12526 : 615 : entered_from_unary.entered_from_unary = true;
12527 : 615 : if (!restrictions.can_be_struct_expr)
12528 : 159 : entered_from_unary.can_be_struct_expr = false;
12529 : 615 : std::unique_ptr<AST::Expr> expr
12530 : 615 : = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary);
12531 : :
12532 : 615 : if (expr == nullptr)
12533 : 0 : return nullptr;
12534 : : // can only apply to boolean expressions
12535 : : /*if (expr.get_type() != boolean_type_node) {
12536 : : rust_error_at(tok->get_locus(),
12537 : : "operand of logical not must be a boolean but it is %s",
12538 : : print_type(expr.get_type()));
12539 : : return Tree::error();
12540 : : }*/
12541 : : /* FIXME: type checking for boolean or integer expressions in semantic
12542 : : * analysis */
12543 : :
12544 : : // FIXME: allow outer attributes on these expressions
12545 : 615 : return std::unique_ptr<AST::NegationExpr> (
12546 : 615 : new AST::NegationExpr (std::move (expr), NegationOperator::NOT,
12547 : 615 : std::move (outer_attrs), tok->get_locus ()));
12548 : 615 : }
12549 : 9320 : case ASTERISK:
12550 : : {
12551 : : /* pointer dereference only - HACK: as struct expressions should
12552 : : * always be value expressions, cannot be dereferenced */
12553 : 9320 : ParseRestrictions entered_from_unary;
12554 : 9320 : entered_from_unary.entered_from_unary = true;
12555 : 9320 : entered_from_unary.can_be_struct_expr = false;
12556 : 9320 : std::unique_ptr<AST::Expr> expr
12557 : 9320 : = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary);
12558 : : // FIXME: allow outer attributes on expression
12559 : 9320 : return std::unique_ptr<AST::DereferenceExpr> (
12560 : 9320 : new AST::DereferenceExpr (std::move (expr), std::move (outer_attrs),
12561 : 9320 : tok->get_locus ()));
12562 : 9320 : }
12563 : 3514 : case AMP:
12564 : : {
12565 : : // (single) "borrow" expression - shared (mutable) or immutable
12566 : 3514 : std::unique_ptr<AST::Expr> expr = nullptr;
12567 : 3514 : Mutability mutability = Mutability::Imm;
12568 : 3514 : bool raw_borrow = false;
12569 : :
12570 : 3514 : ParseRestrictions entered_from_unary;
12571 : 3514 : entered_from_unary.entered_from_unary = true;
12572 : 3514 : if (!restrictions.can_be_struct_expr)
12573 : 14 : entered_from_unary.can_be_struct_expr = false;
12574 : :
12575 : 9 : auto is_mutability = [] (const_TokenPtr token) {
12576 : 9 : return token->get_id () == CONST || token->get_id () == MUT;
12577 : : };
12578 : :
12579 : 3514 : auto t = lexer.peek_token ();
12580 : : // Weak raw keyword, we look (1) ahead and treat it as an identifier if
12581 : : // there is no mut nor const.
12582 : 5917 : if (t->get_id () == IDENTIFIER
12583 : 1602 : && t->get_str () == Values::WeakKeywords::RAW
12584 : 4651 : && is_mutability (lexer.peek_token (1)))
12585 : : {
12586 : 7 : lexer.skip_token ();
12587 : 14 : switch (lexer.peek_token ()->get_id ())
12588 : : {
12589 : : case MUT:
12590 : : mutability = Mutability::Mut;
12591 : : break;
12592 : : case CONST:
12593 : 1 : mutability = Mutability::Imm;
12594 : : break;
12595 : 0 : default:
12596 : 0 : rust_error_at (lexer.peek_token ()->get_locus (),
12597 : : "raw borrow should be either const or mut");
12598 : : }
12599 : 7 : lexer.skip_token ();
12600 : 7 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12601 : 7 : raw_borrow = true;
12602 : : }
12603 : 3507 : else if (t->get_id () == MUT)
12604 : : {
12605 : 855 : lexer.skip_token ();
12606 : 855 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12607 : 855 : mutability = Mutability::Mut;
12608 : 855 : raw_borrow = false;
12609 : : }
12610 : : else
12611 : : {
12612 : 2652 : expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
12613 : 2652 : raw_borrow = false;
12614 : : }
12615 : :
12616 : : // FIXME: allow outer attributes on expression
12617 : 3514 : return std::unique_ptr<AST::BorrowExpr> (
12618 : 3514 : new AST::BorrowExpr (std::move (expr), mutability, raw_borrow, false,
12619 : 3514 : std::move (outer_attrs), tok->get_locus ()));
12620 : 3514 : }
12621 : 38 : case LOGICAL_AND:
12622 : : {
12623 : : // (double) "borrow" expression - shared (mutable) or immutable
12624 : 38 : std::unique_ptr<AST::Expr> expr = nullptr;
12625 : 38 : Mutability mutability = Mutability::Imm;
12626 : :
12627 : 38 : ParseRestrictions entered_from_unary;
12628 : 38 : entered_from_unary.entered_from_unary = true;
12629 : :
12630 : 76 : if (lexer.peek_token ()->get_id () == MUT)
12631 : : {
12632 : 0 : lexer.skip_token ();
12633 : 0 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12634 : 0 : mutability = Mutability::Mut;
12635 : : }
12636 : : else
12637 : : {
12638 : 38 : expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
12639 : 38 : mutability = Mutability::Imm;
12640 : : }
12641 : :
12642 : : // FIXME: allow outer attributes on expression
12643 : 38 : return std::unique_ptr<AST::BorrowExpr> (
12644 : 38 : new AST::BorrowExpr (std::move (expr), mutability, false, true,
12645 : 38 : std::move (outer_attrs), tok->get_locus ()));
12646 : 38 : }
12647 : 376 : case OR:
12648 : : case PIPE:
12649 : : case MOVE:
12650 : : // closure expression
12651 : 1128 : return parse_closure_expr_pratt (tok, std::move (outer_attrs));
12652 : 173 : case DOT_DOT:
12653 : : // either "range to" or "range full" expressions
12654 : 519 : return parse_nud_range_exclusive_expr (tok, std::move (outer_attrs));
12655 : 0 : case DOT_DOT_EQ:
12656 : : // range to inclusive expr
12657 : 0 : return parse_range_to_inclusive_expr (tok, std::move (outer_attrs));
12658 : 896 : case RETURN_KW:
12659 : : // FIXME: is this really a null denotation expression?
12660 : 896 : return parse_return_expr (std::move (outer_attrs), tok->get_locus ());
12661 : 31 : case TRY:
12662 : : // FIXME: is this really a null denotation expression?
12663 : 31 : return parse_try_expr (std::move (outer_attrs), tok->get_locus ());
12664 : 103 : case BREAK:
12665 : : // FIXME: is this really a null denotation expression?
12666 : 103 : return parse_break_expr (std::move (outer_attrs), tok->get_locus ());
12667 : 26 : case CONTINUE:
12668 : 26 : return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
12669 : 2589 : case LEFT_CURLY:
12670 : : // ok - this is an expression with block for once.
12671 : 2589 : return parse_block_expr (std::move (outer_attrs), tl::nullopt,
12672 : 2589 : tok->get_locus ());
12673 : 3177 : case IF:
12674 : : // if or if let, so more lookahead to find out
12675 : 6354 : if (lexer.peek_token ()->get_id () == LET)
12676 : : {
12677 : : // if let expr
12678 : 120 : return parse_if_let_expr (std::move (outer_attrs), tok->get_locus ());
12679 : : }
12680 : : else
12681 : : {
12682 : : // if expr
12683 : 3057 : return parse_if_expr (std::move (outer_attrs), tok->get_locus ());
12684 : : }
12685 : 36 : case LIFETIME:
12686 : 108 : return parse_labelled_loop_expr (tok, std::move (outer_attrs));
12687 : 105 : case LOOP:
12688 : 105 : return parse_loop_expr (std::move (outer_attrs), tl::nullopt,
12689 : 105 : tok->get_locus ());
12690 : 200 : case WHILE:
12691 : 400 : if (lexer.peek_token ()->get_id () == LET)
12692 : : {
12693 : 29 : return parse_while_let_loop_expr (std::move (outer_attrs));
12694 : : }
12695 : : else
12696 : : {
12697 : 171 : return parse_while_loop_expr (std::move (outer_attrs), tl::nullopt,
12698 : 171 : tok->get_locus ());
12699 : : }
12700 : 221 : case FOR:
12701 : 221 : return parse_for_loop_expr (std::move (outer_attrs), tl::nullopt);
12702 : 5533 : case MATCH_KW:
12703 : : // also an expression with block
12704 : 5533 : return parse_match_expr (std::move (outer_attrs), tok->get_locus ());
12705 : 13205 : case LEFT_SQUARE:
12706 : : // array definition expr (not indexing)
12707 : 13205 : return parse_array_expr (std::move (outer_attrs), tok->get_locus ());
12708 : 4622 : case UNSAFE:
12709 : 4622 : return parse_unsafe_block_expr (std::move (outer_attrs),
12710 : 4622 : tok->get_locus ());
12711 : 2 : case BOX:
12712 : 2 : return parse_box_expr (std::move (outer_attrs), tok->get_locus ());
12713 : 1 : case UNDERSCORE:
12714 : 1 : add_error (
12715 : 1 : Error (tok->get_locus (),
12716 : : "use of %qs is not allowed on the right-side of an assignment",
12717 : : tok->get_token_description ()));
12718 : 1 : return nullptr;
12719 : 15 : case CONST:
12720 : 15 : return parse_const_block_expr (std::move (outer_attrs),
12721 : 15 : tok->get_locus ());
12722 : 27 : default:
12723 : 27 : if (!restrictions.expr_can_be_null)
12724 : 27 : add_error (Error (tok->get_locus (),
12725 : : "found unexpected token %qs in null denotation",
12726 : : tok->get_token_description ()));
12727 : 27 : return nullptr;
12728 : : }
12729 : : }
12730 : :
12731 : : /* Called for each token that can appear in infix (between) position. Can be
12732 : : * operators or other punctuation. Returns a function pointer to member
12733 : : * function that implements the left denotation for the token given. */
12734 : : template <typename ManagedTokenSource>
12735 : : std::unique_ptr<AST::Expr>
12736 : 95818 : Parser<ManagedTokenSource>::left_denotation (const_TokenPtr tok,
12737 : : std::unique_ptr<AST::Expr> left,
12738 : : AST::AttrVec outer_attrs,
12739 : : ParseRestrictions restrictions)
12740 : : {
12741 : : // Token passed in has already been skipped, so peek gives "next" token
12742 : 95818 : switch (tok->get_id ())
12743 : : {
12744 : : // FIXME: allow for outer attributes to be applied
12745 : 203 : case QUESTION_MARK:
12746 : : {
12747 : 203 : location_t left_locus = left->get_locus ();
12748 : : // error propagation expression - unary postfix
12749 : 203 : return std::unique_ptr<AST::ErrorPropagationExpr> (
12750 : 203 : new AST::ErrorPropagationExpr (std::move (left),
12751 : 203 : std::move (outer_attrs), left_locus));
12752 : : }
12753 : 11184 : case PLUS:
12754 : : // sum expression - binary infix
12755 : : /*return parse_binary_plus_expr (tok, std::move (left),
12756 : : std::move (outer_attrs), restrictions);*/
12757 : 33552 : return parse_arithmetic_or_logical_expr (tok, std::move (left),
12758 : : std::move (outer_attrs),
12759 : : ArithmeticOrLogicalOperator::ADD,
12760 : 11184 : restrictions);
12761 : 3411 : case MINUS:
12762 : : // difference expression - binary infix
12763 : : /*return parse_binary_minus_expr (tok, std::move (left),
12764 : : std::move (outer_attrs),
12765 : : restrictions);*/
12766 : 10233 : return parse_arithmetic_or_logical_expr (
12767 : : tok, std::move (left), std::move (outer_attrs),
12768 : 3411 : ArithmeticOrLogicalOperator::SUBTRACT, restrictions);
12769 : 423 : case ASTERISK:
12770 : : // product expression - binary infix
12771 : : /*return parse_binary_mult_expr (tok, std::move (left),
12772 : : std::move (outer_attrs), restrictions);*/
12773 : 1269 : return parse_arithmetic_or_logical_expr (
12774 : : tok, std::move (left), std::move (outer_attrs),
12775 : 423 : ArithmeticOrLogicalOperator::MULTIPLY, restrictions);
12776 : 219 : case DIV:
12777 : : // quotient expression - binary infix
12778 : : /*return parse_binary_div_expr (tok, std::move (left),
12779 : : std::move (outer_attrs), restrictions);*/
12780 : 657 : return parse_arithmetic_or_logical_expr (
12781 : : tok, std::move (left), std::move (outer_attrs),
12782 : 219 : ArithmeticOrLogicalOperator::DIVIDE, restrictions);
12783 : 202 : case PERCENT:
12784 : : // modulo expression - binary infix
12785 : : /*return parse_binary_mod_expr (tok, std::move (left),
12786 : : std::move (outer_attrs), restrictions);*/
12787 : 606 : return parse_arithmetic_or_logical_expr (
12788 : : tok, std::move (left), std::move (outer_attrs),
12789 : 202 : ArithmeticOrLogicalOperator::MODULUS, restrictions);
12790 : 4341 : case AMP:
12791 : : // logical or bitwise and expression - binary infix
12792 : : /*return parse_bitwise_and_expr (tok, std::move (left),
12793 : : std::move (outer_attrs), restrictions);*/
12794 : 13023 : return parse_arithmetic_or_logical_expr (
12795 : : tok, std::move (left), std::move (outer_attrs),
12796 : 4341 : ArithmeticOrLogicalOperator::BITWISE_AND, restrictions);
12797 : 1732 : case PIPE:
12798 : : // logical or bitwise or expression - binary infix
12799 : : /*return parse_bitwise_or_expr (tok, std::move (left),
12800 : : std::move (outer_attrs), restrictions);*/
12801 : 5196 : return parse_arithmetic_or_logical_expr (
12802 : : tok, std::move (left), std::move (outer_attrs),
12803 : 1732 : ArithmeticOrLogicalOperator::BITWISE_OR, restrictions);
12804 : 125 : case CARET:
12805 : : // logical or bitwise xor expression - binary infix
12806 : : /*return parse_bitwise_xor_expr (tok, std::move (left),
12807 : : std::move (outer_attrs), restrictions);*/
12808 : 375 : return parse_arithmetic_or_logical_expr (
12809 : : tok, std::move (left), std::move (outer_attrs),
12810 : 125 : ArithmeticOrLogicalOperator::BITWISE_XOR, restrictions);
12811 : 2495 : case LEFT_SHIFT:
12812 : : // left shift expression - binary infix
12813 : : /*return parse_left_shift_expr (tok, std::move (left),
12814 : : std::move (outer_attrs), restrictions);*/
12815 : 7485 : return parse_arithmetic_or_logical_expr (
12816 : : tok, std::move (left), std::move (outer_attrs),
12817 : 2495 : ArithmeticOrLogicalOperator::LEFT_SHIFT, restrictions);
12818 : 3839 : case RIGHT_SHIFT:
12819 : : // right shift expression - binary infix
12820 : : /*return parse_right_shift_expr (tok, std::move (left),
12821 : : std::move (outer_attrs), restrictions);*/
12822 : 11517 : return parse_arithmetic_or_logical_expr (
12823 : : tok, std::move (left), std::move (outer_attrs),
12824 : 3839 : ArithmeticOrLogicalOperator::RIGHT_SHIFT, restrictions);
12825 : 1569 : case EQUAL_EQUAL:
12826 : : // equal to expression - binary infix (no associativity)
12827 : : /*return parse_binary_equal_expr (tok, std::move (left),
12828 : : std::move (outer_attrs),
12829 : : restrictions);*/
12830 : 4707 : return parse_comparison_expr (tok, std::move (left),
12831 : : std::move (outer_attrs),
12832 : 1569 : ComparisonOperator::EQUAL, restrictions);
12833 : 674 : case NOT_EQUAL:
12834 : : // not equal to expression - binary infix (no associativity)
12835 : : /*return parse_binary_not_equal_expr (tok, std::move (left),
12836 : : std::move (outer_attrs),
12837 : : restrictions);*/
12838 : 2022 : return parse_comparison_expr (tok, std::move (left),
12839 : : std::move (outer_attrs),
12840 : : ComparisonOperator::NOT_EQUAL,
12841 : 674 : restrictions);
12842 : 952 : case RIGHT_ANGLE:
12843 : : // greater than expression - binary infix (no associativity)
12844 : : /*return parse_binary_greater_than_expr (tok, std::move (left),
12845 : : std::move (outer_attrs),
12846 : : restrictions);*/
12847 : 2856 : return parse_comparison_expr (tok, std::move (left),
12848 : : std::move (outer_attrs),
12849 : : ComparisonOperator::GREATER_THAN,
12850 : 952 : restrictions);
12851 : 902 : case LEFT_ANGLE:
12852 : : // less than expression - binary infix (no associativity)
12853 : : /*return parse_binary_less_than_expr (tok, std::move (left),
12854 : : std::move (outer_attrs),
12855 : : restrictions);*/
12856 : 2706 : return parse_comparison_expr (tok, std::move (left),
12857 : : std::move (outer_attrs),
12858 : : ComparisonOperator::LESS_THAN,
12859 : 902 : restrictions);
12860 : 390 : case GREATER_OR_EQUAL:
12861 : : // greater than or equal to expression - binary infix (no associativity)
12862 : : /*return parse_binary_greater_equal_expr (tok, std::move (left),
12863 : : std::move (outer_attrs),
12864 : : restrictions);*/
12865 : 1170 : return parse_comparison_expr (tok, std::move (left),
12866 : : std::move (outer_attrs),
12867 : : ComparisonOperator::GREATER_OR_EQUAL,
12868 : 390 : restrictions);
12869 : 323 : case LESS_OR_EQUAL:
12870 : : // less than or equal to expression - binary infix (no associativity)
12871 : : /*return parse_binary_less_equal_expr (tok, std::move (left),
12872 : : std::move (outer_attrs),
12873 : : restrictions);*/
12874 : 969 : return parse_comparison_expr (tok, std::move (left),
12875 : : std::move (outer_attrs),
12876 : : ComparisonOperator::LESS_OR_EQUAL,
12877 : 323 : restrictions);
12878 : 246 : case OR:
12879 : : // lazy logical or expression - binary infix
12880 : 738 : return parse_lazy_or_expr (tok, std::move (left), std::move (outer_attrs),
12881 : 246 : restrictions);
12882 : 536 : case LOGICAL_AND:
12883 : : // lazy logical and expression - binary infix
12884 : 1608 : return parse_lazy_and_expr (tok, std::move (left),
12885 : 536 : std::move (outer_attrs), restrictions);
12886 : 8929 : case AS:
12887 : : /* type cast expression - kind of binary infix (RHS is actually a
12888 : : * TypeNoBounds) */
12889 : 26787 : return parse_type_cast_expr (tok, std::move (left),
12890 : 8929 : std::move (outer_attrs), restrictions);
12891 : 3616 : case EQUAL:
12892 : : // assignment expression - binary infix (note right-to-left
12893 : : // associativity)
12894 : 10848 : return parse_assig_expr (tok, std::move (left), std::move (outer_attrs),
12895 : 3616 : restrictions);
12896 : 326 : case PLUS_EQ:
12897 : : /* plus-assignment expression - binary infix (note right-to-left
12898 : : * associativity) */
12899 : : /*return parse_plus_assig_expr (tok, std::move (left),
12900 : : std::move (outer_attrs), restrictions);*/
12901 : 978 : return parse_compound_assignment_expr (tok, std::move (left),
12902 : : std::move (outer_attrs),
12903 : : CompoundAssignmentOperator::ADD,
12904 : 326 : restrictions);
12905 : 209 : case MINUS_EQ:
12906 : : /* minus-assignment expression - binary infix (note right-to-left
12907 : : * associativity) */
12908 : : /*return parse_minus_assig_expr (tok, std::move (left),
12909 : : std::move (outer_attrs), restrictions);*/
12910 : 627 : return parse_compound_assignment_expr (
12911 : : tok, std::move (left), std::move (outer_attrs),
12912 : 209 : CompoundAssignmentOperator::SUBTRACT, restrictions);
12913 : 28 : case ASTERISK_EQ:
12914 : : /* multiply-assignment expression - binary infix (note right-to-left
12915 : : * associativity) */
12916 : : /*return parse_mult_assig_expr (tok, std::move (left),
12917 : : std::move (outer_attrs), restrictions);*/
12918 : 84 : return parse_compound_assignment_expr (
12919 : : tok, std::move (left), std::move (outer_attrs),
12920 : 28 : CompoundAssignmentOperator::MULTIPLY, restrictions);
12921 : 92 : case DIV_EQ:
12922 : : /* division-assignment expression - binary infix (note right-to-left
12923 : : * associativity) */
12924 : : /*return parse_div_assig_expr (tok, std::move (left),
12925 : : std::move (outer_attrs), restrictions);*/
12926 : 276 : return parse_compound_assignment_expr (tok, std::move (left),
12927 : : std::move (outer_attrs),
12928 : : CompoundAssignmentOperator::DIVIDE,
12929 : 92 : restrictions);
12930 : 22 : case PERCENT_EQ:
12931 : : /* modulo-assignment expression - binary infix (note right-to-left
12932 : : * associativity) */
12933 : : /*return parse_mod_assig_expr (tok, std::move (left),
12934 : : std::move (outer_attrs), restrictions);*/
12935 : 66 : return parse_compound_assignment_expr (
12936 : : tok, std::move (left), std::move (outer_attrs),
12937 : 22 : CompoundAssignmentOperator::MODULUS, restrictions);
12938 : 34 : case AMP_EQ:
12939 : : /* bitwise and-assignment expression - binary infix (note right-to-left
12940 : : * associativity) */
12941 : : /*return parse_and_assig_expr (tok, std::move (left),
12942 : : std::move (outer_attrs), restrictions);*/
12943 : 102 : return parse_compound_assignment_expr (
12944 : : tok, std::move (left), std::move (outer_attrs),
12945 : 34 : CompoundAssignmentOperator::BITWISE_AND, restrictions);
12946 : 86 : case PIPE_EQ:
12947 : : /* bitwise or-assignment expression - binary infix (note right-to-left
12948 : : * associativity) */
12949 : : /*return parse_or_assig_expr (tok, std::move (left),
12950 : : std::move (outer_attrs), restrictions);*/
12951 : 258 : return parse_compound_assignment_expr (
12952 : : tok, std::move (left), std::move (outer_attrs),
12953 : 86 : CompoundAssignmentOperator::BITWISE_OR, restrictions);
12954 : 403 : case CARET_EQ:
12955 : : /* bitwise xor-assignment expression - binary infix (note right-to-left
12956 : : * associativity) */
12957 : : /*return parse_xor_assig_expr (tok, std::move (left),
12958 : : std::move (outer_attrs), restrictions);*/
12959 : 1209 : return parse_compound_assignment_expr (
12960 : : tok, std::move (left), std::move (outer_attrs),
12961 : 403 : CompoundAssignmentOperator::BITWISE_XOR, restrictions);
12962 : 159 : case LEFT_SHIFT_EQ:
12963 : : /* left shift-assignment expression - binary infix (note right-to-left
12964 : : * associativity) */
12965 : : /*return parse_left_shift_assig_expr (tok, std::move (left),
12966 : : std::move (outer_attrs),
12967 : : restrictions);*/
12968 : 477 : return parse_compound_assignment_expr (
12969 : : tok, std::move (left), std::move (outer_attrs),
12970 : 159 : CompoundAssignmentOperator::LEFT_SHIFT, restrictions);
12971 : 155 : case RIGHT_SHIFT_EQ:
12972 : : /* right shift-assignment expression - binary infix (note right-to-left
12973 : : * associativity) */
12974 : : /*return parse_right_shift_assig_expr (tok, std::move (left),
12975 : : std::move (outer_attrs),
12976 : : restrictions);*/
12977 : 465 : return parse_compound_assignment_expr (
12978 : : tok, std::move (left), std::move (outer_attrs),
12979 : 155 : CompoundAssignmentOperator::RIGHT_SHIFT, restrictions);
12980 : 356 : case DOT_DOT:
12981 : : /* range exclusive expression - binary infix (no associativity)
12982 : : * either "range" or "range from" */
12983 : 1068 : return parse_led_range_exclusive_expr (tok, std::move (left),
12984 : : std::move (outer_attrs),
12985 : 356 : restrictions);
12986 : 13 : case DOT_DOT_EQ:
12987 : : /* range inclusive expression - binary infix (no associativity)
12988 : : * unambiguously RangeInclusiveExpr */
12989 : 39 : return parse_range_inclusive_expr (tok, std::move (left),
12990 : 13 : std::move (outer_attrs), restrictions);
12991 : 0 : case SCOPE_RESOLUTION:
12992 : : // path expression - binary infix? FIXME should this even be parsed
12993 : : // here?
12994 : 0 : add_error (
12995 : 0 : Error (tok->get_locus (),
12996 : : "found scope resolution operator in left denotation "
12997 : : "function - this should probably be handled elsewhere"));
12998 : :
12999 : 0 : return nullptr;
13000 : 46259 : case DOT:
13001 : : {
13002 : : /* field expression or method call - relies on parentheses after next
13003 : : * identifier or await if token after is "await" (unary postfix) or
13004 : : * tuple index if token after is a decimal int literal */
13005 : :
13006 : 46259 : const_TokenPtr next_tok = lexer.peek_token ();
13007 : 46259 : if (next_tok->get_id () == IDENTIFIER
13008 : 46259 : && next_tok->get_str () == Values::Keywords::AWAIT)
13009 : : {
13010 : : // await expression
13011 : 0 : return parse_await_expr (tok, std::move (left),
13012 : 0 : std::move (outer_attrs));
13013 : : }
13014 : 46259 : else if (next_tok->get_id () == INT_LITERAL)
13015 : : {
13016 : : // tuple index expression - TODO check for decimal int literal
13017 : 26829 : return parse_tuple_index_expr (tok, std::move (left),
13018 : : std::move (outer_attrs),
13019 : 8943 : restrictions);
13020 : : }
13021 : 37316 : else if (next_tok->get_id () == FLOAT_LITERAL)
13022 : : {
13023 : : // Lexer has misidentified a tuple index as a float literal
13024 : : // eg: `(x, (y, z)).1.0` -> 1.0 has been identified as a float
13025 : : // literal. This means we should split it into three new separate
13026 : : // tokens, the first tuple index, the dot and the second tuple
13027 : : // index.
13028 : 2 : auto current_loc = next_tok->get_locus ();
13029 : 2 : auto str = next_tok->get_str ();
13030 : 2 : auto dot_pos = str.find (".");
13031 : 2 : auto prefix = str.substr (0, dot_pos);
13032 : 2 : auto suffix = str.substr (dot_pos + 1);
13033 : 2 : if (dot_pos == str.size () - 1)
13034 : 3 : lexer.split_current_token (
13035 : 2 : {Token::make_int (current_loc, std::move (prefix),
13036 : : CORETYPE_PURE_DECIMAL),
13037 : : Token::make (DOT, current_loc + 1)});
13038 : : else
13039 : 5 : lexer.split_current_token (
13040 : 2 : {Token::make_int (current_loc, std::move (prefix),
13041 : : CORETYPE_PURE_DECIMAL),
13042 : : Token::make (DOT, current_loc + 1),
13043 : 2 : Token::make_int (current_loc + 2, std::move (suffix),
13044 : : CORETYPE_PURE_DECIMAL)});
13045 : 6 : return parse_tuple_index_expr (tok, std::move (left),
13046 : : std::move (outer_attrs),
13047 : 2 : restrictions);
13048 : 2 : }
13049 : 48812 : else if (next_tok->get_id () == IDENTIFIER
13050 : 79084 : && lexer.peek_token (1)->get_id () != LEFT_PAREN
13051 : 75339 : && lexer.peek_token (1)->get_id () != SCOPE_RESOLUTION)
13052 : : {
13053 : : /* field expression (or should be) - FIXME: scope resolution right
13054 : : * after identifier should always be method, I'm pretty sure */
13055 : 23127 : return parse_field_access_expr (tok, std::move (left),
13056 : : std::move (outer_attrs),
13057 : 7709 : restrictions);
13058 : : }
13059 : : else
13060 : : {
13061 : : // method call (probably)
13062 : 88815 : return parse_method_call_expr (tok, std::move (left),
13063 : : std::move (outer_attrs),
13064 : 29605 : restrictions);
13065 : : }
13066 : 46259 : }
13067 : 558 : case LEFT_PAREN:
13068 : : // function call - method call is based on dot notation first
13069 : 1674 : return parse_function_call_expr (tok, std::move (left),
13070 : 558 : std::move (outer_attrs), restrictions);
13071 : 807 : case LEFT_SQUARE:
13072 : : // array or slice index expression (pseudo binary infix)
13073 : 2421 : return parse_index_expr (tok, std::move (left), std::move (outer_attrs),
13074 : 807 : restrictions);
13075 : 0 : default:
13076 : 0 : add_error (Error (tok->get_locus (),
13077 : : "found unexpected token %qs in left denotation",
13078 : : tok->get_token_description ()));
13079 : :
13080 : 0 : return nullptr;
13081 : : }
13082 : : }
13083 : :
13084 : : /* Returns the left binding power for the given ArithmeticOrLogicalExpr type.
13085 : : * TODO make constexpr? Would that even do anything useful? */
13086 : : inline binding_powers
13087 : 27971 : get_lbp_for_arithmetic_or_logical_expr (
13088 : : AST::ArithmeticOrLogicalExpr::ExprType expr_type)
13089 : : {
13090 : 27971 : switch (expr_type)
13091 : : {
13092 : : case ArithmeticOrLogicalOperator::ADD:
13093 : : return LBP_PLUS;
13094 : : case ArithmeticOrLogicalOperator::SUBTRACT:
13095 : : return LBP_MINUS;
13096 : : case ArithmeticOrLogicalOperator::MULTIPLY:
13097 : : return LBP_MUL;
13098 : : case ArithmeticOrLogicalOperator::DIVIDE:
13099 : : return LBP_DIV;
13100 : : case ArithmeticOrLogicalOperator::MODULUS:
13101 : : return LBP_MOD;
13102 : : case ArithmeticOrLogicalOperator::BITWISE_AND:
13103 : : return LBP_AMP;
13104 : : case ArithmeticOrLogicalOperator::BITWISE_OR:
13105 : : return LBP_PIPE;
13106 : : case ArithmeticOrLogicalOperator::BITWISE_XOR:
13107 : : return LBP_CARET;
13108 : : case ArithmeticOrLogicalOperator::LEFT_SHIFT:
13109 : : return LBP_L_SHIFT;
13110 : : case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
13111 : : return LBP_R_SHIFT;
13112 : 0 : default:
13113 : : // WTF? should not happen, this is an error
13114 : 0 : rust_unreachable ();
13115 : :
13116 : : return LBP_PLUS;
13117 : : }
13118 : : }
13119 : :
13120 : : // Parses an arithmetic or logical expression (with Pratt parsing).
13121 : : template <typename ManagedTokenSource>
13122 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13123 : 27971 : Parser<ManagedTokenSource>::parse_arithmetic_or_logical_expr (
13124 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13125 : : AST::ArithmeticOrLogicalExpr::ExprType expr_type,
13126 : : ParseRestrictions restrictions)
13127 : : {
13128 : : // parse RHS (as tok has already been consumed in parse_expression)
13129 : 27971 : std::unique_ptr<AST::Expr> right
13130 : 27971 : = parse_expr (get_lbp_for_arithmetic_or_logical_expr (expr_type),
13131 : 55942 : AST::AttrVec (), restrictions);
13132 : 27971 : if (right == nullptr)
13133 : 2 : return nullptr;
13134 : :
13135 : : // TODO: check types. actually, do so during semantic analysis
13136 : 27969 : location_t locus = left->get_locus ();
13137 : :
13138 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13139 : 27969 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13140 : 27969 : expr_type, locus));
13141 : 27971 : }
13142 : :
13143 : : // Parses a binary addition expression (with Pratt parsing).
13144 : : template <typename ManagedTokenSource>
13145 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13146 : 0 : Parser<ManagedTokenSource>::parse_binary_plus_expr (
13147 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13148 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13149 : : {
13150 : : // parse RHS (as tok has already been consumed in parse_expression)
13151 : 0 : std::unique_ptr<AST::Expr> right
13152 : 0 : = parse_expr (LBP_PLUS, AST::AttrVec (), restrictions);
13153 : 0 : if (right == nullptr)
13154 : 0 : return nullptr;
13155 : :
13156 : : // TODO: check types. actually, do so during semantic analysis
13157 : 0 : location_t locus = left->get_locus ();
13158 : :
13159 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13160 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13161 : 0 : ArithmeticOrLogicalOperator::ADD, locus));
13162 : 0 : }
13163 : :
13164 : : // Parses a binary subtraction expression (with Pratt parsing).
13165 : : template <typename ManagedTokenSource>
13166 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13167 : 0 : Parser<ManagedTokenSource>::parse_binary_minus_expr (
13168 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13169 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13170 : : {
13171 : : // parse RHS (as tok has already been consumed in parse_expression)
13172 : 0 : std::unique_ptr<AST::Expr> right
13173 : 0 : = parse_expr (LBP_MINUS, AST::AttrVec (), restrictions);
13174 : 0 : if (right == nullptr)
13175 : 0 : return nullptr;
13176 : :
13177 : : // TODO: check types. actually, do so during semantic analysis
13178 : 0 : location_t locus = left->get_locus ();
13179 : :
13180 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13181 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13182 : : ArithmeticOrLogicalOperator::SUBTRACT,
13183 : 0 : locus));
13184 : 0 : }
13185 : :
13186 : : // Parses a binary multiplication expression (with Pratt parsing).
13187 : : template <typename ManagedTokenSource>
13188 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13189 : 0 : Parser<ManagedTokenSource>::parse_binary_mult_expr (
13190 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13191 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13192 : : {
13193 : : // parse RHS (as tok has already been consumed in parse_expression)
13194 : 0 : std::unique_ptr<AST::Expr> right
13195 : 0 : = parse_expr (LBP_MUL, AST::AttrVec (), restrictions);
13196 : 0 : if (right == nullptr)
13197 : 0 : return nullptr;
13198 : :
13199 : : // TODO: check types. actually, do so during semantic analysis
13200 : 0 : location_t locus = left->get_locus ();
13201 : :
13202 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13203 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13204 : : ArithmeticOrLogicalOperator::MULTIPLY,
13205 : 0 : locus));
13206 : 0 : }
13207 : :
13208 : : // Parses a binary division expression (with Pratt parsing).
13209 : : template <typename ManagedTokenSource>
13210 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13211 : 0 : Parser<ManagedTokenSource>::parse_binary_div_expr (
13212 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13213 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13214 : : {
13215 : : // parse RHS (as tok has already been consumed in parse_expression)
13216 : 0 : std::unique_ptr<AST::Expr> right
13217 : 0 : = parse_expr (LBP_DIV, AST::AttrVec (), restrictions);
13218 : 0 : if (right == nullptr)
13219 : 0 : return nullptr;
13220 : :
13221 : : // TODO: check types. actually, do so during semantic analysis
13222 : 0 : location_t locus = left->get_locus ();
13223 : :
13224 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13225 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13226 : : ArithmeticOrLogicalOperator::DIVIDE,
13227 : 0 : locus));
13228 : 0 : }
13229 : :
13230 : : // Parses a binary modulo expression (with Pratt parsing).
13231 : : template <typename ManagedTokenSource>
13232 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13233 : 0 : Parser<ManagedTokenSource>::parse_binary_mod_expr (
13234 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13235 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13236 : : {
13237 : : // parse RHS (as tok has already been consumed in parse_expression)
13238 : 0 : std::unique_ptr<AST::Expr> right
13239 : 0 : = parse_expr (LBP_MOD, AST::AttrVec (), restrictions);
13240 : 0 : if (right == nullptr)
13241 : 0 : return nullptr;
13242 : :
13243 : : // TODO: check types. actually, do so during semantic analysis
13244 : 0 : location_t locus = left->get_locus ();
13245 : :
13246 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13247 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13248 : : ArithmeticOrLogicalOperator::MODULUS,
13249 : 0 : locus));
13250 : 0 : }
13251 : :
13252 : : /* Parses a binary bitwise (or eager logical) and expression (with Pratt
13253 : : * parsing). */
13254 : : template <typename ManagedTokenSource>
13255 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13256 : 0 : Parser<ManagedTokenSource>::parse_bitwise_and_expr (
13257 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13258 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13259 : : {
13260 : : // parse RHS (as tok has already been consumed in parse_expression)
13261 : 0 : std::unique_ptr<AST::Expr> right
13262 : 0 : = parse_expr (LBP_AMP, AST::AttrVec (), restrictions);
13263 : 0 : if (right == nullptr)
13264 : 0 : return nullptr;
13265 : :
13266 : : // TODO: check types. actually, do so during semantic analysis
13267 : 0 : location_t locus = left->get_locus ();
13268 : :
13269 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13270 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13271 : : ArithmeticOrLogicalOperator::BITWISE_AND,
13272 : 0 : locus));
13273 : 0 : }
13274 : :
13275 : : /* Parses a binary bitwise (or eager logical) or expression (with Pratt
13276 : : * parsing). */
13277 : : template <typename ManagedTokenSource>
13278 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13279 : 0 : Parser<ManagedTokenSource>::parse_bitwise_or_expr (
13280 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13281 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13282 : : {
13283 : : // parse RHS (as tok has already been consumed in parse_expression)
13284 : 0 : std::unique_ptr<AST::Expr> right
13285 : 0 : = parse_expr (LBP_PIPE, AST::AttrVec (), restrictions);
13286 : 0 : if (right == nullptr)
13287 : 0 : return nullptr;
13288 : :
13289 : : // TODO: check types. actually, do so during semantic analysis
13290 : 0 : location_t locus = left->get_locus ();
13291 : :
13292 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13293 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13294 : : ArithmeticOrLogicalOperator::BITWISE_OR,
13295 : 0 : locus));
13296 : 0 : }
13297 : :
13298 : : /* Parses a binary bitwise (or eager logical) xor expression (with Pratt
13299 : : * parsing). */
13300 : : template <typename ManagedTokenSource>
13301 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13302 : 0 : Parser<ManagedTokenSource>::parse_bitwise_xor_expr (
13303 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13304 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13305 : : {
13306 : : // parse RHS (as tok has already been consumed in parse_expression)
13307 : 0 : std::unique_ptr<AST::Expr> right
13308 : 0 : = parse_expr (LBP_CARET, AST::AttrVec (), restrictions);
13309 : 0 : if (right == nullptr)
13310 : 0 : return nullptr;
13311 : :
13312 : : // TODO: check types. actually, do so during semantic analysis
13313 : 0 : location_t locus = left->get_locus ();
13314 : :
13315 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13316 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13317 : : ArithmeticOrLogicalOperator::BITWISE_XOR,
13318 : 0 : locus));
13319 : 0 : }
13320 : :
13321 : : // Parses a binary left shift expression (with Pratt parsing).
13322 : : template <typename ManagedTokenSource>
13323 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13324 : 0 : Parser<ManagedTokenSource>::parse_left_shift_expr (
13325 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13326 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13327 : : {
13328 : : // parse RHS (as tok has already been consumed in parse_expression)
13329 : 0 : std::unique_ptr<AST::Expr> right
13330 : 0 : = parse_expr (LBP_L_SHIFT, AST::AttrVec (), restrictions);
13331 : 0 : if (right == nullptr)
13332 : 0 : return nullptr;
13333 : :
13334 : : // TODO: check types. actually, do so during semantic analysis
13335 : 0 : location_t locus = left->get_locus ();
13336 : :
13337 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13338 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13339 : : ArithmeticOrLogicalOperator::LEFT_SHIFT,
13340 : 0 : locus));
13341 : 0 : }
13342 : :
13343 : : // Parses a binary right shift expression (with Pratt parsing).
13344 : : template <typename ManagedTokenSource>
13345 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13346 : 0 : Parser<ManagedTokenSource>::parse_right_shift_expr (
13347 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13348 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13349 : : {
13350 : : // parse RHS (as tok has already been consumed in parse_expression)
13351 : 0 : std::unique_ptr<AST::Expr> right
13352 : 0 : = parse_expr (LBP_R_SHIFT, AST::AttrVec (), restrictions);
13353 : 0 : if (right == nullptr)
13354 : 0 : return nullptr;
13355 : :
13356 : : // TODO: check types. actually, do so during semantic analysis
13357 : 0 : location_t locus = left->get_locus ();
13358 : :
13359 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13360 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13361 : : ArithmeticOrLogicalOperator::RIGHT_SHIFT,
13362 : 0 : locus));
13363 : 0 : }
13364 : :
13365 : : /* Returns the left binding power for the given ComparisonExpr type.
13366 : : * TODO make constexpr? Would that even do anything useful? */
13367 : : inline binding_powers
13368 : 4810 : get_lbp_for_comparison_expr (AST::ComparisonExpr::ExprType expr_type)
13369 : : {
13370 : 4810 : switch (expr_type)
13371 : : {
13372 : : case ComparisonOperator::EQUAL:
13373 : : return LBP_EQUAL;
13374 : : case ComparisonOperator::NOT_EQUAL:
13375 : : return LBP_NOT_EQUAL;
13376 : : case ComparisonOperator::GREATER_THAN:
13377 : : return LBP_GREATER_THAN;
13378 : : case ComparisonOperator::LESS_THAN:
13379 : : return LBP_SMALLER_THAN;
13380 : : case ComparisonOperator::GREATER_OR_EQUAL:
13381 : : return LBP_GREATER_EQUAL;
13382 : : case ComparisonOperator::LESS_OR_EQUAL:
13383 : : return LBP_SMALLER_EQUAL;
13384 : 0 : default:
13385 : : // WTF? should not happen, this is an error
13386 : 0 : rust_unreachable ();
13387 : :
13388 : : return LBP_EQUAL;
13389 : : }
13390 : : }
13391 : :
13392 : : /* Parses a ComparisonExpr of given type and LBP. TODO find a way to only
13393 : : * specify one and have the other looked up - e.g. specify ExprType and
13394 : : * binding power is looked up? */
13395 : : template <typename ManagedTokenSource>
13396 : : std::unique_ptr<AST::ComparisonExpr>
13397 : 4810 : Parser<ManagedTokenSource>::parse_comparison_expr (
13398 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13399 : : AST::ComparisonExpr::ExprType expr_type, ParseRestrictions restrictions)
13400 : : {
13401 : : // parse RHS (as tok has already been consumed in parse_expression)
13402 : 4810 : std::unique_ptr<AST::Expr> right
13403 : 4810 : = parse_expr (get_lbp_for_comparison_expr (expr_type), AST::AttrVec (),
13404 : : restrictions);
13405 : 4810 : if (right == nullptr)
13406 : 0 : return nullptr;
13407 : :
13408 : : // TODO: check types. actually, do so during semantic analysis
13409 : 4810 : location_t locus = left->get_locus ();
13410 : :
13411 : : return std::unique_ptr<AST::ComparisonExpr> (
13412 : 4810 : new AST::ComparisonExpr (std::move (left), std::move (right), expr_type,
13413 : 4810 : locus));
13414 : 4810 : }
13415 : :
13416 : : // Parses a binary equal to expression (with Pratt parsing).
13417 : : template <typename ManagedTokenSource>
13418 : : std::unique_ptr<AST::ComparisonExpr>
13419 : 0 : Parser<ManagedTokenSource>::parse_binary_equal_expr (
13420 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13421 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13422 : : {
13423 : : // parse RHS (as tok has already been consumed in parse_expression)
13424 : 0 : std::unique_ptr<AST::Expr> right
13425 : 0 : = parse_expr (LBP_EQUAL, AST::AttrVec (), restrictions);
13426 : 0 : if (right == nullptr)
13427 : 0 : return nullptr;
13428 : :
13429 : : // TODO: check types. actually, do so during semantic analysis
13430 : 0 : location_t locus = left->get_locus ();
13431 : :
13432 : : return std::unique_ptr<AST::ComparisonExpr> (
13433 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13434 : 0 : ComparisonOperator::EQUAL, locus));
13435 : 0 : }
13436 : :
13437 : : // Parses a binary not equal to expression (with Pratt parsing).
13438 : : template <typename ManagedTokenSource>
13439 : : std::unique_ptr<AST::ComparisonExpr>
13440 : 0 : Parser<ManagedTokenSource>::parse_binary_not_equal_expr (
13441 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13442 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13443 : : {
13444 : : // parse RHS (as tok has already been consumed in parse_expression)
13445 : 0 : std::unique_ptr<AST::Expr> right
13446 : 0 : = parse_expr (LBP_NOT_EQUAL, AST::AttrVec (), restrictions);
13447 : 0 : if (right == nullptr)
13448 : 0 : return nullptr;
13449 : :
13450 : : // TODO: check types. actually, do so during semantic analysis
13451 : 0 : location_t locus = left->get_locus ();
13452 : :
13453 : : return std::unique_ptr<AST::ComparisonExpr> (
13454 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13455 : 0 : ComparisonOperator::NOT_EQUAL, locus));
13456 : 0 : }
13457 : :
13458 : : // Parses a binary greater than expression (with Pratt parsing).
13459 : : template <typename ManagedTokenSource>
13460 : : std::unique_ptr<AST::ComparisonExpr>
13461 : 0 : Parser<ManagedTokenSource>::parse_binary_greater_than_expr (
13462 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13463 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13464 : : {
13465 : : // parse RHS (as tok has already been consumed in parse_expression)
13466 : 0 : std::unique_ptr<AST::Expr> right
13467 : 0 : = parse_expr (LBP_GREATER_THAN, AST::AttrVec (), restrictions);
13468 : 0 : if (right == nullptr)
13469 : 0 : return nullptr;
13470 : :
13471 : : // TODO: check types. actually, do so during semantic analysis
13472 : 0 : location_t locus = left->get_locus ();
13473 : :
13474 : : return std::unique_ptr<AST::ComparisonExpr> (
13475 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13476 : 0 : ComparisonOperator::GREATER_THAN, locus));
13477 : 0 : }
13478 : :
13479 : : // Parses a binary less than expression (with Pratt parsing).
13480 : : template <typename ManagedTokenSource>
13481 : : std::unique_ptr<AST::ComparisonExpr>
13482 : 0 : Parser<ManagedTokenSource>::parse_binary_less_than_expr (
13483 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13484 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13485 : : {
13486 : : // parse RHS (as tok has already been consumed in parse_expression)
13487 : 0 : std::unique_ptr<AST::Expr> right
13488 : 0 : = parse_expr (LBP_SMALLER_THAN, AST::AttrVec (), restrictions);
13489 : 0 : if (right == nullptr)
13490 : 0 : return nullptr;
13491 : :
13492 : : // TODO: check types. actually, do so during semantic analysis
13493 : 0 : location_t locus = left->get_locus ();
13494 : :
13495 : : return std::unique_ptr<AST::ComparisonExpr> (
13496 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13497 : 0 : ComparisonOperator::LESS_THAN, locus));
13498 : 0 : }
13499 : :
13500 : : // Parses a binary greater than or equal to expression (with Pratt parsing).
13501 : : template <typename ManagedTokenSource>
13502 : : std::unique_ptr<AST::ComparisonExpr>
13503 : 0 : Parser<ManagedTokenSource>::parse_binary_greater_equal_expr (
13504 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13505 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13506 : : {
13507 : : // parse RHS (as tok has already been consumed in parse_expression)
13508 : 0 : std::unique_ptr<AST::Expr> right
13509 : 0 : = parse_expr (LBP_GREATER_EQUAL, AST::AttrVec (), restrictions);
13510 : 0 : if (right == nullptr)
13511 : 0 : return nullptr;
13512 : :
13513 : : // TODO: check types. actually, do so during semantic analysis
13514 : 0 : location_t locus = left->get_locus ();
13515 : :
13516 : : return std::unique_ptr<AST::ComparisonExpr> (
13517 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13518 : 0 : ComparisonOperator::GREATER_OR_EQUAL, locus));
13519 : 0 : }
13520 : :
13521 : : // Parses a binary less than or equal to expression (with Pratt parsing).
13522 : : template <typename ManagedTokenSource>
13523 : : std::unique_ptr<AST::ComparisonExpr>
13524 : 0 : Parser<ManagedTokenSource>::parse_binary_less_equal_expr (
13525 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13526 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13527 : : {
13528 : : // parse RHS (as tok has already been consumed in parse_expression)
13529 : 0 : std::unique_ptr<AST::Expr> right
13530 : 0 : = parse_expr (LBP_SMALLER_EQUAL, AST::AttrVec (), restrictions);
13531 : 0 : if (right == nullptr)
13532 : 0 : return nullptr;
13533 : :
13534 : : // TODO: check types. actually, do so during semantic analysis
13535 : 0 : location_t locus = left->get_locus ();
13536 : :
13537 : : return std::unique_ptr<AST::ComparisonExpr> (
13538 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13539 : 0 : ComparisonOperator::LESS_OR_EQUAL, locus));
13540 : 0 : }
13541 : :
13542 : : // Parses a binary lazy boolean or expression (with Pratt parsing).
13543 : : template <typename ManagedTokenSource>
13544 : : std::unique_ptr<AST::LazyBooleanExpr>
13545 : 246 : Parser<ManagedTokenSource>::parse_lazy_or_expr (
13546 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13547 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13548 : : {
13549 : : // parse RHS (as tok has already been consumed in parse_expression)
13550 : 246 : std::unique_ptr<AST::Expr> right
13551 : 246 : = parse_expr (LBP_LOGICAL_OR, AST::AttrVec (), restrictions);
13552 : 246 : if (right == nullptr)
13553 : 0 : return nullptr;
13554 : :
13555 : : // TODO: check types. actually, do so during semantic analysis
13556 : 246 : location_t locus = left->get_locus ();
13557 : :
13558 : : return std::unique_ptr<AST::LazyBooleanExpr> (
13559 : 246 : new AST::LazyBooleanExpr (std::move (left), std::move (right),
13560 : 246 : LazyBooleanOperator::LOGICAL_OR, locus));
13561 : 246 : }
13562 : :
13563 : : // Parses a binary lazy boolean and expression (with Pratt parsing).
13564 : : template <typename ManagedTokenSource>
13565 : : std::unique_ptr<AST::LazyBooleanExpr>
13566 : 536 : Parser<ManagedTokenSource>::parse_lazy_and_expr (
13567 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13568 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13569 : : {
13570 : : // parse RHS (as tok has already been consumed in parse_expression)
13571 : 536 : std::unique_ptr<AST::Expr> right
13572 : 536 : = parse_expr (LBP_LOGICAL_AND, AST::AttrVec (), restrictions);
13573 : 536 : if (right == nullptr)
13574 : 0 : return nullptr;
13575 : :
13576 : : // TODO: check types. actually, do so during semantic analysis
13577 : 536 : location_t locus = left->get_locus ();
13578 : :
13579 : : return std::unique_ptr<AST::LazyBooleanExpr> (
13580 : 536 : new AST::LazyBooleanExpr (std::move (left), std::move (right),
13581 : 536 : LazyBooleanOperator::LOGICAL_AND, locus));
13582 : 536 : }
13583 : :
13584 : : // Parses a pseudo-binary infix type cast expression (with Pratt parsing).
13585 : : template <typename ManagedTokenSource>
13586 : : std::unique_ptr<AST::TypeCastExpr>
13587 : 8929 : Parser<ManagedTokenSource>::parse_type_cast_expr (
13588 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> expr_to_cast,
13589 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED,
13590 : : ParseRestrictions restrictions ATTRIBUTE_UNUSED)
13591 : : {
13592 : : // parse RHS (as tok has already been consumed in parse_expression)
13593 : 8929 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
13594 : 8929 : if (type == nullptr)
13595 : 0 : return nullptr;
13596 : : // FIXME: how do I get precedence put in here?
13597 : :
13598 : : // TODO: check types. actually, do so during semantic analysis
13599 : 8929 : location_t locus = expr_to_cast->get_locus ();
13600 : :
13601 : : return std::unique_ptr<AST::TypeCastExpr> (
13602 : 8929 : new AST::TypeCastExpr (std::move (expr_to_cast), std::move (type), locus));
13603 : 8929 : }
13604 : :
13605 : : // Parses a binary assignment expression (with Pratt parsing).
13606 : : template <typename ManagedTokenSource>
13607 : : std::unique_ptr<AST::AssignmentExpr>
13608 : 3616 : Parser<ManagedTokenSource>::parse_assig_expr (
13609 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13610 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions)
13611 : : {
13612 : : // parse RHS (as tok has already been consumed in parse_expression)
13613 : 3616 : std::unique_ptr<AST::Expr> right
13614 : 3616 : = parse_expr (LBP_ASSIG - 1, AST::AttrVec (), restrictions);
13615 : 3616 : if (right == nullptr)
13616 : 0 : return nullptr;
13617 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13618 : :
13619 : : // TODO: check types. actually, do so during semantic analysis
13620 : 3616 : location_t locus = left->get_locus ();
13621 : :
13622 : : return std::unique_ptr<AST::AssignmentExpr> (
13623 : 3616 : new AST::AssignmentExpr (std::move (left), std::move (right),
13624 : 3616 : std::move (outer_attrs), locus));
13625 : 3616 : }
13626 : :
13627 : : /* Returns the left binding power for the given CompoundAssignmentExpr type.
13628 : : * TODO make constexpr? Would that even do anything useful? */
13629 : : inline binding_powers
13630 : 1514 : get_lbp_for_compound_assignment_expr (
13631 : : AST::CompoundAssignmentExpr::ExprType expr_type)
13632 : : {
13633 : 1514 : switch (expr_type)
13634 : : {
13635 : : case CompoundAssignmentOperator::ADD:
13636 : : return LBP_PLUS;
13637 : : case CompoundAssignmentOperator::SUBTRACT:
13638 : : return LBP_MINUS;
13639 : : case CompoundAssignmentOperator::MULTIPLY:
13640 : : return LBP_MUL;
13641 : : case CompoundAssignmentOperator::DIVIDE:
13642 : : return LBP_DIV;
13643 : : case CompoundAssignmentOperator::MODULUS:
13644 : : return LBP_MOD;
13645 : : case CompoundAssignmentOperator::BITWISE_AND:
13646 : : return LBP_AMP;
13647 : : case CompoundAssignmentOperator::BITWISE_OR:
13648 : : return LBP_PIPE;
13649 : : case CompoundAssignmentOperator::BITWISE_XOR:
13650 : : return LBP_CARET;
13651 : : case CompoundAssignmentOperator::LEFT_SHIFT:
13652 : : return LBP_L_SHIFT;
13653 : : case CompoundAssignmentOperator::RIGHT_SHIFT:
13654 : : return LBP_R_SHIFT;
13655 : 0 : default:
13656 : : // WTF? should not happen, this is an error
13657 : 0 : rust_unreachable ();
13658 : :
13659 : : return LBP_PLUS;
13660 : : }
13661 : : }
13662 : :
13663 : : // Parses a compound assignment expression (with Pratt parsing).
13664 : : template <typename ManagedTokenSource>
13665 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13666 : 1514 : Parser<ManagedTokenSource>::parse_compound_assignment_expr (
13667 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13668 : : AST::CompoundAssignmentExpr::ExprType expr_type,
13669 : : ParseRestrictions restrictions)
13670 : : {
13671 : : // parse RHS (as tok has already been consumed in parse_expression)
13672 : 1514 : std::unique_ptr<AST::Expr> right
13673 : 1514 : = parse_expr (get_lbp_for_compound_assignment_expr (expr_type) - 1,
13674 : 3028 : AST::AttrVec (), restrictions);
13675 : 1514 : if (right == nullptr)
13676 : 0 : return nullptr;
13677 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13678 : :
13679 : : // TODO: check types. actually, do so during semantic analysis
13680 : 1514 : location_t locus = left->get_locus ();
13681 : :
13682 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13683 : 1514 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13684 : 1514 : expr_type, locus));
13685 : 1514 : }
13686 : :
13687 : : // Parses a binary add-assignment expression (with Pratt parsing).
13688 : : template <typename ManagedTokenSource>
13689 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13690 : 0 : Parser<ManagedTokenSource>::parse_plus_assig_expr (
13691 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13692 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13693 : : {
13694 : : // parse RHS (as tok has already been consumed in parse_expression)
13695 : 0 : std::unique_ptr<AST::Expr> right
13696 : 0 : = parse_expr (LBP_PLUS_ASSIG - 1, AST::AttrVec (), restrictions);
13697 : 0 : if (right == nullptr)
13698 : 0 : return nullptr;
13699 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13700 : :
13701 : : // TODO: check types. actually, do so during semantic analysis
13702 : 0 : location_t locus = left->get_locus ();
13703 : :
13704 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13705 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13706 : 0 : CompoundAssignmentOperator::ADD, locus));
13707 : 0 : }
13708 : :
13709 : : // Parses a binary minus-assignment expression (with Pratt parsing).
13710 : : template <typename ManagedTokenSource>
13711 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13712 : 0 : Parser<ManagedTokenSource>::parse_minus_assig_expr (
13713 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13714 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13715 : : {
13716 : : // parse RHS (as tok has already been consumed in parse_expression)
13717 : 0 : std::unique_ptr<AST::Expr> right
13718 : 0 : = parse_expr (LBP_MINUS_ASSIG - 1, AST::AttrVec (), restrictions);
13719 : 0 : if (right == nullptr)
13720 : 0 : return nullptr;
13721 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13722 : :
13723 : : // TODO: check types. actually, do so during semantic analysis
13724 : 0 : location_t locus = left->get_locus ();
13725 : :
13726 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13727 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13728 : : CompoundAssignmentOperator::SUBTRACT,
13729 : 0 : locus));
13730 : 0 : }
13731 : :
13732 : : // Parses a binary multiplication-assignment expression (with Pratt parsing).
13733 : : template <typename ManagedTokenSource>
13734 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13735 : 0 : Parser<ManagedTokenSource>::parse_mult_assig_expr (
13736 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13737 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13738 : : {
13739 : : // parse RHS (as tok has already been consumed in parse_expression)
13740 : 0 : std::unique_ptr<AST::Expr> right
13741 : 0 : = parse_expr (LBP_MULT_ASSIG - 1, AST::AttrVec (), restrictions);
13742 : 0 : if (right == nullptr)
13743 : 0 : return nullptr;
13744 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13745 : :
13746 : : // TODO: check types. actually, do so during semantic analysis
13747 : 0 : location_t locus = left->get_locus ();
13748 : :
13749 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13750 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13751 : : CompoundAssignmentOperator::MULTIPLY,
13752 : 0 : locus));
13753 : 0 : }
13754 : :
13755 : : // Parses a binary division-assignment expression (with Pratt parsing).
13756 : : template <typename ManagedTokenSource>
13757 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13758 : 0 : Parser<ManagedTokenSource>::parse_div_assig_expr (
13759 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13760 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13761 : : {
13762 : : // parse RHS (as tok has already been consumed in parse_expression)
13763 : 0 : std::unique_ptr<AST::Expr> right
13764 : 0 : = parse_expr (LBP_DIV_ASSIG - 1, AST::AttrVec (), restrictions);
13765 : 0 : if (right == nullptr)
13766 : 0 : return nullptr;
13767 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13768 : :
13769 : : // TODO: check types. actually, do so during semantic analysis
13770 : 0 : location_t locus = left->get_locus ();
13771 : :
13772 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13773 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13774 : : CompoundAssignmentOperator::DIVIDE,
13775 : 0 : locus));
13776 : 0 : }
13777 : :
13778 : : // Parses a binary modulo-assignment expression (with Pratt parsing).
13779 : : template <typename ManagedTokenSource>
13780 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13781 : 0 : Parser<ManagedTokenSource>::parse_mod_assig_expr (
13782 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13783 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13784 : : {
13785 : : // parse RHS (as tok has already been consumed in parse_expression)
13786 : 0 : std::unique_ptr<AST::Expr> right
13787 : 0 : = parse_expr (LBP_MOD_ASSIG - 1, AST::AttrVec (), restrictions);
13788 : 0 : if (right == nullptr)
13789 : 0 : return nullptr;
13790 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13791 : :
13792 : : // TODO: check types. actually, do so during semantic analysis
13793 : 0 : location_t locus = left->get_locus ();
13794 : :
13795 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13796 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13797 : : CompoundAssignmentOperator::MODULUS,
13798 : 0 : locus));
13799 : 0 : }
13800 : :
13801 : : // Parses a binary and-assignment expression (with Pratt parsing).
13802 : : template <typename ManagedTokenSource>
13803 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13804 : 0 : Parser<ManagedTokenSource>::parse_and_assig_expr (
13805 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13806 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13807 : : {
13808 : : // parse RHS (as tok has already been consumed in parse_expression)
13809 : 0 : std::unique_ptr<AST::Expr> right
13810 : 0 : = parse_expr (LBP_AMP_ASSIG - 1, AST::AttrVec (), restrictions);
13811 : 0 : if (right == nullptr)
13812 : 0 : return nullptr;
13813 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13814 : :
13815 : : // TODO: check types. actually, do so during semantic analysis
13816 : 0 : location_t locus = left->get_locus ();
13817 : :
13818 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13819 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13820 : : CompoundAssignmentOperator::BITWISE_AND,
13821 : 0 : locus));
13822 : 0 : }
13823 : :
13824 : : // Parses a binary or-assignment expression (with Pratt parsing).
13825 : : template <typename ManagedTokenSource>
13826 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13827 : 0 : Parser<ManagedTokenSource>::parse_or_assig_expr (
13828 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13829 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13830 : : {
13831 : : // parse RHS (as tok has already been consumed in parse_expression)
13832 : 0 : std::unique_ptr<AST::Expr> right
13833 : 0 : = parse_expr (LBP_PIPE_ASSIG - 1, AST::AttrVec (), restrictions);
13834 : 0 : if (right == nullptr)
13835 : 0 : return nullptr;
13836 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13837 : :
13838 : : // TODO: check types. actually, do so during semantic analysis
13839 : 0 : location_t locus = left->get_locus ();
13840 : :
13841 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13842 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13843 : : CompoundAssignmentOperator::BITWISE_OR,
13844 : 0 : locus));
13845 : 0 : }
13846 : :
13847 : : // Parses a binary xor-assignment expression (with Pratt parsing).
13848 : : template <typename ManagedTokenSource>
13849 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13850 : 0 : Parser<ManagedTokenSource>::parse_xor_assig_expr (
13851 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13852 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13853 : : {
13854 : : // parse RHS (as tok has already been consumed in parse_expression)
13855 : 0 : std::unique_ptr<AST::Expr> right
13856 : 0 : = parse_expr (LBP_CARET_ASSIG - 1, AST::AttrVec (), restrictions);
13857 : 0 : if (right == nullptr)
13858 : 0 : return nullptr;
13859 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13860 : :
13861 : : // TODO: check types. actually, do so during semantic analysis
13862 : 0 : location_t locus = left->get_locus ();
13863 : :
13864 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13865 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13866 : : CompoundAssignmentOperator::BITWISE_XOR,
13867 : 0 : locus));
13868 : 0 : }
13869 : :
13870 : : // Parses a binary left shift-assignment expression (with Pratt parsing).
13871 : : template <typename ManagedTokenSource>
13872 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13873 : 0 : Parser<ManagedTokenSource>::parse_left_shift_assig_expr (
13874 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13875 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13876 : : {
13877 : : // parse RHS (as tok has already been consumed in parse_expression)
13878 : 0 : std::unique_ptr<AST::Expr> right
13879 : 0 : = parse_expr (LBP_L_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
13880 : 0 : if (right == nullptr)
13881 : 0 : return nullptr;
13882 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13883 : :
13884 : : // TODO: check types. actually, do so during semantic analysis
13885 : 0 : location_t locus = left->get_locus ();
13886 : :
13887 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13888 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13889 : : CompoundAssignmentOperator::LEFT_SHIFT,
13890 : 0 : locus));
13891 : 0 : }
13892 : :
13893 : : // Parses a binary right shift-assignment expression (with Pratt parsing).
13894 : : template <typename ManagedTokenSource>
13895 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13896 : 0 : Parser<ManagedTokenSource>::parse_right_shift_assig_expr (
13897 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13898 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13899 : : {
13900 : : // parse RHS (as tok has already been consumed in parse_expression)
13901 : 0 : std::unique_ptr<AST::Expr> right
13902 : 0 : = parse_expr (LBP_R_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
13903 : 0 : if (right == nullptr)
13904 : 0 : return nullptr;
13905 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13906 : :
13907 : : // TODO: check types. actually, do so during semantic analysis
13908 : 0 : location_t locus = left->get_locus ();
13909 : :
13910 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13911 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13912 : : CompoundAssignmentOperator::RIGHT_SHIFT,
13913 : 0 : locus));
13914 : 0 : }
13915 : :
13916 : : // Parses a postfix unary await expression (with Pratt parsing).
13917 : : template <typename ManagedTokenSource>
13918 : : std::unique_ptr<AST::AwaitExpr>
13919 : 0 : Parser<ManagedTokenSource>::parse_await_expr (
13920 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> expr_to_await,
13921 : : AST::AttrVec outer_attrs)
13922 : : {
13923 : : /* skip "await" identifier (as "." has already been consumed in
13924 : : * parse_expression) this assumes that the identifier was already identified
13925 : : * as await */
13926 : 0 : if (!skip_token (IDENTIFIER))
13927 : : {
13928 : 0 : Error error (tok->get_locus (), "failed to skip %<await%> in await expr "
13929 : : "- this is probably a deep issue");
13930 : 0 : add_error (std::move (error));
13931 : :
13932 : : // skip somewhere?
13933 : 0 : return nullptr;
13934 : 0 : }
13935 : :
13936 : : // TODO: check inside async block in semantic analysis
13937 : 0 : location_t locus = expr_to_await->get_locus ();
13938 : :
13939 : : return std::unique_ptr<AST::AwaitExpr> (
13940 : 0 : new AST::AwaitExpr (std::move (expr_to_await), std::move (outer_attrs),
13941 : 0 : locus));
13942 : : }
13943 : :
13944 : : /* Parses an exclusive range ('..') in left denotation position (i.e.
13945 : : * RangeFromExpr or RangeFromToExpr). */
13946 : : template <typename ManagedTokenSource>
13947 : : std::unique_ptr<AST::RangeExpr>
13948 : 356 : Parser<ManagedTokenSource>::parse_led_range_exclusive_expr (
13949 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13950 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13951 : : {
13952 : : // FIXME: this probably parses expressions accidently or whatever
13953 : : // try parsing RHS (as tok has already been consumed in parse_expression)
13954 : : // Can be nullptr, in which case it is a RangeFromExpr, otherwise a
13955 : : // RangeFromToExpr.
13956 : 356 : restrictions.expr_can_be_null = true;
13957 : 356 : std::unique_ptr<AST::Expr> right
13958 : 356 : = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
13959 : :
13960 : 356 : location_t locus = left->get_locus ();
13961 : :
13962 : 356 : if (right == nullptr)
13963 : : {
13964 : : // range from expr
13965 : 65 : return std::unique_ptr<AST::RangeFromExpr> (
13966 : 65 : new AST::RangeFromExpr (std::move (left), locus));
13967 : : }
13968 : : else
13969 : : {
13970 : 291 : return std::unique_ptr<AST::RangeFromToExpr> (
13971 : 291 : new AST::RangeFromToExpr (std::move (left), std::move (right), locus));
13972 : : }
13973 : : // FIXME: make non-associative
13974 : 356 : }
13975 : :
13976 : : /* Parses an exclusive range ('..') in null denotation position (i.e.
13977 : : * RangeToExpr or RangeFullExpr). */
13978 : : template <typename ManagedTokenSource>
13979 : : std::unique_ptr<AST::RangeExpr>
13980 : 173 : Parser<ManagedTokenSource>::parse_nud_range_exclusive_expr (
13981 : : const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
13982 : : {
13983 : 173 : auto restrictions = ParseRestrictions ();
13984 : 173 : restrictions.expr_can_be_null = true;
13985 : :
13986 : : // FIXME: this probably parses expressions accidently or whatever
13987 : : // try parsing RHS (as tok has already been consumed in parse_expression)
13988 : 173 : std::unique_ptr<AST::Expr> right
13989 : 173 : = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
13990 : :
13991 : 173 : location_t locus = tok->get_locus ();
13992 : :
13993 : 173 : if (right == nullptr)
13994 : : {
13995 : : // range from expr
13996 : 54 : return std::unique_ptr<AST::RangeFullExpr> (
13997 : 54 : new AST::RangeFullExpr (locus));
13998 : : }
13999 : : else
14000 : : {
14001 : 119 : return std::unique_ptr<AST::RangeToExpr> (
14002 : 119 : new AST::RangeToExpr (std::move (right), locus));
14003 : : }
14004 : : // FIXME: make non-associative
14005 : 173 : }
14006 : :
14007 : : // Parses a full binary range inclusive expression.
14008 : : template <typename ManagedTokenSource>
14009 : : std::unique_ptr<AST::RangeFromToInclExpr>
14010 : 13 : Parser<ManagedTokenSource>::parse_range_inclusive_expr (
14011 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
14012 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
14013 : : {
14014 : : // parse RHS (as tok has already been consumed in parse_expression)
14015 : 13 : std::unique_ptr<AST::Expr> right
14016 : 13 : = parse_expr (LBP_DOT_DOT_EQ, AST::AttrVec (), restrictions);
14017 : 13 : if (right == nullptr)
14018 : 0 : return nullptr;
14019 : : // FIXME: make non-associative
14020 : :
14021 : : // TODO: check types. actually, do so during semantic analysis
14022 : 13 : location_t locus = left->get_locus ();
14023 : :
14024 : : return std::unique_ptr<AST::RangeFromToInclExpr> (
14025 : 13 : new AST::RangeFromToInclExpr (std::move (left), std::move (right), locus));
14026 : 13 : }
14027 : :
14028 : : // Parses an inclusive range-to prefix unary expression.
14029 : : template <typename ManagedTokenSource>
14030 : : std::unique_ptr<AST::RangeToInclExpr>
14031 : 0 : Parser<ManagedTokenSource>::parse_range_to_inclusive_expr (
14032 : : const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
14033 : : {
14034 : : // parse RHS (as tok has already been consumed in parse_expression)
14035 : 0 : std::unique_ptr<AST::Expr> right = parse_expr (LBP_DOT_DOT_EQ);
14036 : 0 : if (right == nullptr)
14037 : 0 : return nullptr;
14038 : : // FIXME: make non-associative
14039 : :
14040 : : // TODO: check types. actually, do so during semantic analysis
14041 : :
14042 : : return std::unique_ptr<AST::RangeToInclExpr> (
14043 : 0 : new AST::RangeToInclExpr (std::move (right), tok->get_locus ()));
14044 : 0 : }
14045 : :
14046 : : // Parses a pseudo-binary infix tuple index expression.
14047 : : template <typename ManagedTokenSource>
14048 : : std::unique_ptr<AST::TupleIndexExpr>
14049 : 8945 : Parser<ManagedTokenSource>::parse_tuple_index_expr (
14050 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> tuple_expr,
14051 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
14052 : : {
14053 : : // parse int literal (as token already skipped)
14054 : 8945 : const_TokenPtr index_tok = expect_token (INT_LITERAL);
14055 : 8945 : if (index_tok == nullptr)
14056 : : {
14057 : 0 : return nullptr;
14058 : : }
14059 : 17890 : std::string index = index_tok->get_str ();
14060 : :
14061 : : // convert to integer
14062 : 8945 : if (!index_tok->is_pure_decimal ())
14063 : : {
14064 : 27 : Error error (index_tok->get_locus (),
14065 : : "tuple index should be a pure decimal literal");
14066 : 27 : add_error (std::move (error));
14067 : 27 : }
14068 : 8945 : int index_int = atoi (index.c_str ());
14069 : :
14070 : 8945 : location_t locus = tuple_expr->get_locus ();
14071 : :
14072 : : return std::unique_ptr<AST::TupleIndexExpr> (
14073 : 8945 : new AST::TupleIndexExpr (std::move (tuple_expr), index_int,
14074 : 8945 : std::move (outer_attrs), locus));
14075 : 8945 : }
14076 : :
14077 : : // Parses a pseudo-binary infix array (or slice) index expression.
14078 : : template <typename ManagedTokenSource>
14079 : : std::unique_ptr<AST::ArrayIndexExpr>
14080 : 807 : Parser<ManagedTokenSource>::parse_index_expr (
14081 : : const_TokenPtr, std::unique_ptr<AST::Expr> array_expr,
14082 : : AST::AttrVec outer_attrs, ParseRestrictions)
14083 : : {
14084 : : // parse RHS (as tok has already been consumed in parse_expression)
14085 : : /*std::unique_ptr<AST::Expr> index_expr
14086 : : = parse_expr (LBP_ARRAY_REF, AST::AttrVec (),
14087 : : restrictions);*/
14088 : : // TODO: conceptually, should treat [] as brackets, so just parse all expr
14089 : 807 : std::unique_ptr<AST::Expr> index_expr = parse_expr ();
14090 : 807 : if (index_expr == nullptr)
14091 : 0 : return nullptr;
14092 : :
14093 : : // skip ']' at end of array
14094 : 807 : if (!skip_token (RIGHT_SQUARE))
14095 : : {
14096 : : // skip somewhere?
14097 : 0 : return nullptr;
14098 : : }
14099 : :
14100 : : // TODO: check types. actually, do so during semantic analysis
14101 : 807 : location_t locus = array_expr->get_locus ();
14102 : :
14103 : : return std::unique_ptr<AST::ArrayIndexExpr> (
14104 : 807 : new AST::ArrayIndexExpr (std::move (array_expr), std::move (index_expr),
14105 : 807 : std::move (outer_attrs), locus));
14106 : 807 : }
14107 : :
14108 : : // Parses a pseudo-binary infix struct field access expression.
14109 : : template <typename ManagedTokenSource>
14110 : : std::unique_ptr<AST::FieldAccessExpr>
14111 : 7709 : Parser<ManagedTokenSource>::parse_field_access_expr (
14112 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> struct_expr,
14113 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
14114 : : {
14115 : : /* get field name identifier (assume that this is a field access expr and
14116 : : * not await, for instance) */
14117 : 7709 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
14118 : 7709 : if (ident_tok == nullptr)
14119 : 0 : return nullptr;
14120 : :
14121 : 15418 : Identifier ident{ident_tok};
14122 : :
14123 : 7709 : location_t locus = struct_expr->get_locus ();
14124 : :
14125 : : // TODO: check types. actually, do so during semantic analysis
14126 : : return std::unique_ptr<AST::FieldAccessExpr> (
14127 : 7709 : new AST::FieldAccessExpr (std::move (struct_expr), std::move (ident),
14128 : 7709 : std::move (outer_attrs), locus));
14129 : 7709 : }
14130 : :
14131 : : // Parses a pseudo-binary infix method call expression.
14132 : : template <typename ManagedTokenSource>
14133 : : std::unique_ptr<AST::MethodCallExpr>
14134 : 29605 : Parser<ManagedTokenSource>::parse_method_call_expr (
14135 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
14136 : : AST::AttrVec outer_attrs, ParseRestrictions)
14137 : : {
14138 : : // parse path expr segment
14139 : 29605 : AST::PathExprSegment segment = parse_path_expr_segment ();
14140 : 29605 : if (segment.is_error ())
14141 : : {
14142 : 0 : Error error (tok->get_locus (),
14143 : : "failed to parse path expr segment of method call expr");
14144 : 0 : add_error (std::move (error));
14145 : :
14146 : 0 : return nullptr;
14147 : 0 : }
14148 : :
14149 : : // skip left parentheses
14150 : 29605 : if (!skip_token (LEFT_PAREN))
14151 : : {
14152 : 0 : return nullptr;
14153 : : }
14154 : :
14155 : : // parse method params (if they exist)
14156 : 29605 : std::vector<std::unique_ptr<AST::Expr>> params;
14157 : :
14158 : 29605 : const_TokenPtr t = lexer.peek_token ();
14159 : 35738 : while (t->get_id () != RIGHT_PAREN)
14160 : : {
14161 : 6133 : std::unique_ptr<AST::Expr> param = parse_expr ();
14162 : 6133 : if (param == nullptr)
14163 : : {
14164 : 0 : Error error (t->get_locus (),
14165 : : "failed to parse method param in method call");
14166 : 0 : add_error (std::move (error));
14167 : :
14168 : 0 : return nullptr;
14169 : 0 : }
14170 : 6133 : params.push_back (std::move (param));
14171 : :
14172 : 12266 : if (lexer.peek_token ()->get_id () != COMMA)
14173 : : break;
14174 : :
14175 : 418 : lexer.skip_token ();
14176 : 418 : t = lexer.peek_token ();
14177 : : }
14178 : :
14179 : : // skip right paren
14180 : 29605 : if (!skip_token (RIGHT_PAREN))
14181 : : {
14182 : 0 : return nullptr;
14183 : : }
14184 : :
14185 : : // TODO: check types. actually do so in semantic analysis pass.
14186 : 29605 : location_t locus = receiver_expr->get_locus ();
14187 : :
14188 : : return std::unique_ptr<AST::MethodCallExpr> (
14189 : 59210 : new AST::MethodCallExpr (std::move (receiver_expr), std::move (segment),
14190 : : std::move (params), std::move (outer_attrs),
14191 : 29605 : locus));
14192 : 29605 : }
14193 : :
14194 : : // Parses a pseudo-binary infix function call expression.
14195 : : template <typename ManagedTokenSource>
14196 : : std::unique_ptr<AST::CallExpr>
14197 : 558 : Parser<ManagedTokenSource>::parse_function_call_expr (
14198 : : const_TokenPtr, std::unique_ptr<AST::Expr> function_expr,
14199 : : AST::AttrVec outer_attrs, ParseRestrictions)
14200 : : {
14201 : : // parse function params (if they exist)
14202 : 558 : std::vector<std::unique_ptr<AST::Expr>> params;
14203 : :
14204 : 558 : const_TokenPtr t = lexer.peek_token ();
14205 : 1062 : while (t->get_id () != RIGHT_PAREN)
14206 : : {
14207 : 504 : std::unique_ptr<AST::Expr> param = parse_expr ();
14208 : 504 : if (param == nullptr)
14209 : : {
14210 : 0 : Error error (t->get_locus (),
14211 : : "failed to parse function param in function call");
14212 : 0 : add_error (std::move (error));
14213 : :
14214 : 0 : return nullptr;
14215 : 0 : }
14216 : 504 : params.push_back (std::move (param));
14217 : :
14218 : 1008 : if (lexer.peek_token ()->get_id () != COMMA)
14219 : : break;
14220 : :
14221 : 107 : lexer.skip_token ();
14222 : 107 : t = lexer.peek_token ();
14223 : : }
14224 : :
14225 : : // skip ')' at end of param list
14226 : 558 : if (!skip_token (RIGHT_PAREN))
14227 : : {
14228 : : // skip somewhere?
14229 : 0 : return nullptr;
14230 : : }
14231 : :
14232 : : // TODO: check types. actually, do so during semantic analysis
14233 : 558 : location_t locus = function_expr->get_locus ();
14234 : :
14235 : : return std::unique_ptr<AST::CallExpr> (
14236 : 558 : new AST::CallExpr (std::move (function_expr), std::move (params),
14237 : 558 : std::move (outer_attrs), locus));
14238 : 558 : }
14239 : :
14240 : : /* Parses a macro invocation with a path in expression already parsed (but not
14241 : : * '!' token). */
14242 : : template <typename ManagedTokenSource>
14243 : : std::unique_ptr<AST::MacroInvocation>
14244 : 52543 : Parser<ManagedTokenSource>::parse_macro_invocation_partial (
14245 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
14246 : : ParseRestrictions restrictions)
14247 : : {
14248 : : // macro invocation
14249 : 52543 : if (!skip_token (EXCLAM))
14250 : : {
14251 : 0 : return nullptr;
14252 : : }
14253 : :
14254 : : // convert PathInExpression to SimplePath - if this isn't possible, error
14255 : 52543 : AST::SimplePath converted_path = path.as_simple_path ();
14256 : 52543 : if (converted_path.is_empty ())
14257 : : {
14258 : 0 : Error error (lexer.peek_token ()->get_locus (),
14259 : : "failed to parse simple path in macro invocation");
14260 : 0 : add_error (std::move (error));
14261 : :
14262 : 0 : return nullptr;
14263 : 0 : }
14264 : :
14265 : 52543 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
14266 : :
14267 : 52543 : rust_debug ("successfully parsed macro invocation (via partial)");
14268 : :
14269 : 52543 : location_t macro_locus = converted_path.get_locus ();
14270 : :
14271 : 105086 : return AST::MacroInvocation::Regular (
14272 : 105086 : AST::MacroInvocData (std::move (converted_path), std::move (tok_tree)),
14273 : 52543 : std::move (outer_attrs), macro_locus);
14274 : 52543 : }
14275 : :
14276 : : /* Parses a struct expr struct with a path in expression already parsed (but
14277 : : * not
14278 : : * '{' token). */
14279 : : template <typename ManagedTokenSource>
14280 : : std::unique_ptr<AST::StructExprStruct>
14281 : 1694 : Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
14282 : : AST::PathInExpression path, AST::AttrVec outer_attrs)
14283 : : {
14284 : : // assume struct expr struct (as struct-enum disambiguation requires name
14285 : : // lookup) again, make statement if final ';'
14286 : 1694 : if (!skip_token (LEFT_CURLY))
14287 : : {
14288 : 0 : return nullptr;
14289 : : }
14290 : :
14291 : : // parse inner attributes
14292 : 1694 : AST::AttrVec inner_attrs = parse_inner_attributes ();
14293 : :
14294 : : // branch based on next token
14295 : 1694 : const_TokenPtr t = lexer.peek_token ();
14296 : 1694 : location_t path_locus = path.get_locus ();
14297 : 1694 : switch (t->get_id ())
14298 : : {
14299 : 75 : case RIGHT_CURLY:
14300 : : // struct with no body
14301 : 75 : lexer.skip_token ();
14302 : :
14303 : : return std::unique_ptr<AST::StructExprStruct> (
14304 : 75 : new AST::StructExprStruct (std::move (path), std::move (inner_attrs),
14305 : 75 : std::move (outer_attrs), path_locus));
14306 : 1619 : case DOT_DOT:
14307 : : /* technically this would give a struct base-only struct, but this
14308 : : * algorithm should work too. As such, AST type not happening. */
14309 : : case IDENTIFIER:
14310 : : case HASH:
14311 : : case INT_LITERAL:
14312 : : {
14313 : : // struct with struct expr fields
14314 : :
14315 : : // parse struct expr fields
14316 : 1619 : std::vector<std::unique_ptr<AST::StructExprField>> fields;
14317 : :
14318 : 4479 : while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
14319 : : {
14320 : 2860 : std::unique_ptr<AST::StructExprField> field
14321 : : = parse_struct_expr_field ();
14322 : 2860 : if (field == nullptr)
14323 : : {
14324 : 0 : Error error (t->get_locus (),
14325 : : "failed to parse struct (or enum) expr field");
14326 : 0 : add_error (std::move (error));
14327 : :
14328 : 0 : return nullptr;
14329 : 0 : }
14330 : :
14331 : : // DEBUG:
14332 : 2860 : rust_debug ("struct/enum expr field validated to not be null");
14333 : :
14334 : 2860 : fields.push_back (std::move (field));
14335 : :
14336 : : // DEBUG:
14337 : 2860 : rust_debug ("struct/enum expr field pushed back");
14338 : :
14339 : 5720 : if (lexer.peek_token ()->get_id () != COMMA)
14340 : : {
14341 : : // DEBUG:
14342 : 1338 : rust_debug ("lack of comma detected in struct/enum expr "
14343 : : "fields - break");
14344 : : break;
14345 : : }
14346 : 1522 : lexer.skip_token ();
14347 : :
14348 : : // DEBUG:
14349 : 1522 : rust_debug ("struct/enum expr fields comma skipped ");
14350 : :
14351 : 1522 : t = lexer.peek_token ();
14352 : : }
14353 : :
14354 : : // DEBUG:
14355 : 1619 : rust_debug ("struct/enum expr about to parse struct base ");
14356 : :
14357 : : // parse struct base if it exists
14358 : : AST::StructBase struct_base = AST::StructBase::error ();
14359 : 3238 : if (lexer.peek_token ()->get_id () == DOT_DOT)
14360 : : {
14361 : 69 : location_t dot_dot_location = lexer.peek_token ()->get_locus ();
14362 : 69 : lexer.skip_token ();
14363 : :
14364 : : // parse required struct base expr
14365 : 69 : std::unique_ptr<AST::Expr> base_expr = parse_expr ();
14366 : 69 : if (base_expr == nullptr)
14367 : : {
14368 : 0 : Error error (lexer.peek_token ()->get_locus (),
14369 : : "failed to parse struct base expression in struct "
14370 : : "expression");
14371 : 0 : add_error (std::move (error));
14372 : :
14373 : 0 : return nullptr;
14374 : 0 : }
14375 : :
14376 : : // DEBUG:
14377 : 69 : rust_debug ("struct/enum expr - parsed and validated base expr");
14378 : :
14379 : : struct_base
14380 : 69 : = AST::StructBase (std::move (base_expr), dot_dot_location);
14381 : :
14382 : : // DEBUG:
14383 : 69 : rust_debug ("assigned struct base to new struct base ");
14384 : 69 : }
14385 : :
14386 : 1619 : if (!skip_token (RIGHT_CURLY))
14387 : : {
14388 : 0 : return nullptr;
14389 : : }
14390 : :
14391 : : // DEBUG:
14392 : 1619 : rust_debug (
14393 : : "struct/enum expr skipped right curly - done and ready to return");
14394 : :
14395 : 1619 : return std::unique_ptr<AST::StructExprStructFields> (
14396 : 1619 : new AST::StructExprStructFields (std::move (path), std::move (fields),
14397 : : path_locus, std::move (struct_base),
14398 : : std::move (inner_attrs),
14399 : 1619 : std::move (outer_attrs)));
14400 : 1619 : }
14401 : 0 : default:
14402 : 0 : add_error (
14403 : 0 : Error (t->get_locus (),
14404 : : "unrecognised token %qs in struct (or enum) expression - "
14405 : : "expected %<}%>, identifier, integer literal, or %<..%>",
14406 : : t->get_token_description ()));
14407 : :
14408 : 0 : return nullptr;
14409 : : }
14410 : 1694 : }
14411 : :
14412 : : /* Parses a struct expr tuple with a path in expression already parsed (but
14413 : : * not
14414 : : * '(' token).
14415 : : * FIXME: this currently outputs a call expr, as they cannot be disambiguated.
14416 : : * A better solution would be to just get this to call that function directly.
14417 : : * */
14418 : : template <typename ManagedTokenSource>
14419 : : std::unique_ptr<AST::CallExpr>
14420 : 78744 : Parser<ManagedTokenSource>::parse_struct_expr_tuple_partial (
14421 : : AST::PathInExpression path, AST::AttrVec outer_attrs)
14422 : : {
14423 : 78744 : if (!skip_token (LEFT_PAREN))
14424 : : {
14425 : 0 : return nullptr;
14426 : : }
14427 : :
14428 : 78744 : AST::AttrVec inner_attrs = parse_inner_attributes ();
14429 : :
14430 : 78744 : std::vector<std::unique_ptr<AST::Expr>> exprs;
14431 : :
14432 : 78744 : const_TokenPtr t = lexer.peek_token ();
14433 : 288893 : while (t->get_id () != RIGHT_PAREN)
14434 : : {
14435 : : // parse expression (required)
14436 : 210149 : std::unique_ptr<AST::Expr> expr = parse_expr ();
14437 : 210149 : if (expr == nullptr)
14438 : : {
14439 : 0 : Error error (t->get_locus (), "failed to parse expression in "
14440 : : "struct (or enum) expression tuple");
14441 : 0 : add_error (std::move (error));
14442 : :
14443 : 0 : return nullptr;
14444 : 0 : }
14445 : 210149 : exprs.push_back (std::move (expr));
14446 : :
14447 : 420298 : if (lexer.peek_token ()->get_id () != COMMA)
14448 : : break;
14449 : :
14450 : 142344 : lexer.skip_token ();
14451 : :
14452 : 142344 : t = lexer.peek_token ();
14453 : : }
14454 : :
14455 : 78744 : if (!skip_token (RIGHT_PAREN))
14456 : : {
14457 : 0 : return nullptr;
14458 : : }
14459 : :
14460 : 78744 : location_t path_locus = path.get_locus ();
14461 : :
14462 : 78744 : auto pathExpr = std::unique_ptr<AST::PathInExpression> (
14463 : 78744 : new AST::PathInExpression (std::move (path)));
14464 : :
14465 : : return std::unique_ptr<AST::CallExpr> (
14466 : 78744 : new AST::CallExpr (std::move (pathExpr), std::move (exprs),
14467 : 78744 : std::move (outer_attrs), path_locus));
14468 : 157488 : }
14469 : :
14470 : : // Parses a closure expression with pratt parsing (from null denotation).
14471 : : template <typename ManagedTokenSource>
14472 : : std::unique_ptr<AST::ClosureExpr>
14473 : 376 : Parser<ManagedTokenSource>::parse_closure_expr_pratt (const_TokenPtr tok,
14474 : : AST::AttrVec outer_attrs)
14475 : : {
14476 : : // TODO: does this need pratt parsing (for precedence)? probably not, but
14477 : : // idk
14478 : 376 : location_t locus = tok->get_locus ();
14479 : 376 : bool has_move = false;
14480 : 376 : if (tok->get_id () == MOVE)
14481 : : {
14482 : 60 : has_move = true;
14483 : 60 : tok = lexer.peek_token ();
14484 : 60 : lexer.skip_token ();
14485 : : // skip token and reassign
14486 : : }
14487 : :
14488 : : // handle parameter list
14489 : 376 : std::vector<AST::ClosureParam> params;
14490 : :
14491 : 376 : switch (tok->get_id ())
14492 : : {
14493 : : case OR:
14494 : : // no parameters, don't skip token
14495 : : break;
14496 : 351 : case PIPE:
14497 : : {
14498 : : // actually may have parameters
14499 : : // don't skip token
14500 : 351 : const_TokenPtr t = lexer.peek_token ();
14501 : 785 : while (t->get_id () != PIPE)
14502 : : {
14503 : 434 : AST::ClosureParam param = parse_closure_param ();
14504 : 434 : if (param.is_error ())
14505 : : {
14506 : : // TODO is this really an error?
14507 : 0 : Error error (t->get_locus (), "could not parse closure param");
14508 : 0 : add_error (std::move (error));
14509 : :
14510 : 0 : return nullptr;
14511 : 0 : }
14512 : 434 : params.push_back (std::move (param));
14513 : :
14514 : 868 : if (lexer.peek_token ()->get_id () != COMMA)
14515 : : {
14516 : 702 : if (lexer.peek_token ()->get_id () == OR)
14517 : 1 : lexer.split_current_token (PIPE, PIPE);
14518 : : // not an error but means param list is done
14519 : : break;
14520 : : }
14521 : : // skip comma
14522 : 83 : lexer.skip_token ();
14523 : :
14524 : 166 : if (lexer.peek_token ()->get_id () == OR)
14525 : 0 : lexer.split_current_token (PIPE, PIPE);
14526 : :
14527 : 83 : t = lexer.peek_token ();
14528 : : }
14529 : :
14530 : 351 : if (!skip_token (PIPE))
14531 : : {
14532 : 0 : return nullptr;
14533 : : }
14534 : : break;
14535 : 351 : }
14536 : 0 : default:
14537 : 0 : add_error (Error (tok->get_locus (),
14538 : : "unexpected token %qs in closure expression - expected "
14539 : : "%<|%> or %<||%>",
14540 : : tok->get_token_description ()));
14541 : :
14542 : : // skip somewhere?
14543 : 0 : return nullptr;
14544 : : }
14545 : :
14546 : : // again branch based on next token
14547 : 376 : tok = lexer.peek_token ();
14548 : 376 : if (tok->get_id () == RETURN_TYPE)
14549 : : {
14550 : : // must be return type closure with block expr
14551 : :
14552 : : // skip "return type" token
14553 : 32 : lexer.skip_token ();
14554 : :
14555 : : // parse actual type, which is required
14556 : 32 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
14557 : 32 : if (type == nullptr)
14558 : : {
14559 : : // error
14560 : 0 : Error error (tok->get_locus (), "failed to parse type for closure");
14561 : 0 : add_error (std::move (error));
14562 : :
14563 : : // skip somewhere?
14564 : 0 : return nullptr;
14565 : 0 : }
14566 : :
14567 : : // parse block expr, which is required
14568 : 32 : std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
14569 : 32 : if (block == nullptr)
14570 : : {
14571 : : // error
14572 : 0 : Error error (lexer.peek_token ()->get_locus (),
14573 : : "failed to parse block expr in closure");
14574 : 0 : add_error (std::move (error));
14575 : :
14576 : : // skip somewhere?
14577 : 0 : return nullptr;
14578 : 0 : }
14579 : :
14580 : 32 : return std::unique_ptr<AST::ClosureExprInnerTyped> (
14581 : 32 : new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
14582 : : std::move (params), locus, has_move,
14583 : 32 : std::move (outer_attrs)));
14584 : 32 : }
14585 : : else
14586 : : {
14587 : : // must be expr-only closure
14588 : :
14589 : : // parse expr, which is required
14590 : 344 : std::unique_ptr<AST::Expr> expr = parse_expr ();
14591 : 344 : if (expr == nullptr)
14592 : : {
14593 : 0 : Error error (tok->get_locus (),
14594 : : "failed to parse expression in closure");
14595 : 0 : add_error (std::move (error));
14596 : :
14597 : : // skip somewhere?
14598 : 0 : return nullptr;
14599 : 0 : }
14600 : :
14601 : 344 : return std::unique_ptr<AST::ClosureExprInner> (
14602 : 344 : new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
14603 : 344 : has_move, std::move (outer_attrs)));
14604 : 344 : }
14605 : 376 : }
14606 : :
14607 : : // Returns true if the next token is END, ELSE, or EOF;
14608 : : template <typename ManagedTokenSource>
14609 : : bool
14610 : 0 : Parser<ManagedTokenSource>::done_end_or_else ()
14611 : : {
14612 : 0 : const_TokenPtr t = lexer.peek_token ();
14613 : 0 : return (t->get_id () == RIGHT_CURLY || t->get_id () == ELSE
14614 : 0 : || t->get_id () == END_OF_FILE);
14615 : 0 : }
14616 : :
14617 : : // Returns true if the next token is END or EOF.
14618 : : template <typename ManagedTokenSource>
14619 : : bool
14620 : 0 : Parser<ManagedTokenSource>::done_end ()
14621 : : {
14622 : 0 : const_TokenPtr t = lexer.peek_token ();
14623 : 0 : return (t->get_id () == RIGHT_CURLY || t->get_id () == END_OF_FILE);
14624 : 0 : }
14625 : : } // namespace Rust
|