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 : 631 : can_tok_start_type (TokenId id)
148 : : {
149 : 631 : 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 : 391 : default:
177 : 391 : 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 : 19570 : is_right_angle_tok (TokenId id)
185 : : {
186 : 19570 : 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 : 11949 : default:
194 : 4518 : 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 : 7691 : 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 : 7691 : const_TokenPtr tok = lexer.peek_token ();
228 : 7691 : switch (tok->get_id ())
229 : : {
230 : 7444 : case RIGHT_ANGLE:
231 : : // this is good - skip token
232 : 7444 : lexer.skip_token ();
233 : 7444 : return true;
234 : 246 : case RIGHT_SHIFT:
235 : : {
236 : : // new implementation that should be better
237 : 246 : lexer.split_current_token (RIGHT_ANGLE, RIGHT_ANGLE);
238 : 246 : lexer.skip_token ();
239 : 246 : 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 : 7691 : }
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 : 99565 : Parser<ManagedTokenSource>::left_binding_power (const_TokenPtr token)
271 : : {
272 : : // HACK: called with "peek_token()", so lookahead is "peek_token(1)"
273 : 99565 : 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 : 8769 : case DOT:
302 : 17538 : if (lexer.peek_token (1)->get_id () == IDENTIFIER
303 : 16638 : && 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 : 1 : case QUESTION_MARK:
317 : 1 : return LBP_QUESTION_MARK;
318 : :
319 : 5041 : case AS:
320 : 5041 : 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 : 48 : case AMP:
341 : 48 : return LBP_AMP;
342 : :
343 : : // binary ^ operator
344 : 77 : case CARET:
345 : 77 : return LBP_CARET;
346 : :
347 : : // binary | operator
348 : 27 : case PIPE:
349 : 27 : 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 : 658 : case LOGICAL_AND:
365 : 658 : return LBP_LOGICAL_AND;
366 : :
367 : 91 : case OR:
368 : 91 : 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 : 72522 : default:
408 : 72522 : 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 : 4478 : Parser<ManagedTokenSource>::parse_items ()
425 : : {
426 : 4478 : std::vector<std::unique_ptr<AST::Item>> items;
427 : :
428 : 4478 : const_TokenPtr t = lexer.peek_token ();
429 : 22400 : while (t->get_id () != END_OF_FILE)
430 : : {
431 : 17969 : std::unique_ptr<AST::Item> item = parse_item (false);
432 : 17969 : if (item == nullptr)
433 : : {
434 : 47 : Error error (lexer.peek_token ()->get_locus (),
435 : : "failed to parse item in crate");
436 : 47 : add_error (std::move (error));
437 : :
438 : : // TODO: should all items be cleared?
439 : 47 : items = std::vector<std::unique_ptr<AST::Item>> ();
440 : : break;
441 : 47 : }
442 : :
443 : 17922 : items.push_back (std::move (item));
444 : :
445 : 17922 : t = lexer.peek_token ();
446 : : }
447 : :
448 : 4478 : return items;
449 : 4478 : }
450 : :
451 : : // Parses a crate (compilation unit) - entry point
452 : : template <typename ManagedTokenSource>
453 : : std::unique_ptr<AST::Crate>
454 : 4426 : Parser<ManagedTokenSource>::parse_crate ()
455 : : {
456 : : // parse inner attributes
457 : 4426 : AST::AttrVec inner_attrs = parse_inner_attributes ();
458 : :
459 : : // parse items
460 : 4426 : std::vector<std::unique_ptr<AST::Item>> items = parse_items ();
461 : :
462 : : // emit all errors
463 : 4650 : for (const auto &error : error_table)
464 : 224 : error.emit ();
465 : :
466 : : return std::unique_ptr<AST::Crate> (
467 : 4426 : new AST::Crate (std::move (items), std::move (inner_attrs)));
468 : 4426 : }
469 : :
470 : : // Parse a contiguous block of inner attributes.
471 : : template <typename ManagedTokenSource>
472 : : AST::AttrVec
473 : 51233 : Parser<ManagedTokenSource>::parse_inner_attributes ()
474 : : {
475 : 51233 : AST::AttrVec inner_attributes;
476 : :
477 : : // only try to parse it if it starts with "#!" not only "#"
478 : 43091 : while ((lexer.peek_token ()->get_id () == HASH
479 : 15682 : && lexer.peek_token (1)->get_id () == EXCLAM)
480 : 158617 : || lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
481 : : {
482 : 855 : AST::Attribute inner_attr = parse_inner_attribute ();
483 : :
484 : : /* Ensure only valid inner attributes are added to the inner_attributes
485 : : * list */
486 : 855 : if (!inner_attr.is_empty ())
487 : : {
488 : 855 : 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 : 51233 : inner_attributes.shrink_to_fit ();
499 : 51233 : 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 : 8136 : Parser<ManagedTokenSource>::parse_doc_comment ()
506 : : {
507 : 8136 : const_TokenPtr token = lexer.peek_token ();
508 : 8136 : location_t locus = token->get_locus ();
509 : 8136 : AST::SimplePathSegment segment (Values::Attributes::DOC, locus);
510 : 8136 : std::vector<AST::SimplePathSegment> segments;
511 : 8136 : segments.push_back (std::move (segment));
512 : 8136 : AST::SimplePath attr_path (std::move (segments), false, locus);
513 : 16272 : AST::LiteralExpr lit_expr (token->get_str (), AST::Literal::STRING,
514 : : PrimitiveCoreType::CORETYPE_STR, {}, locus);
515 : 8136 : std::unique_ptr<AST::AttrInput> attr_input (
516 : 8136 : new AST::AttrInputLiteral (std::move (lit_expr)));
517 : 8136 : lexer.skip_token ();
518 : 16272 : return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
519 : 8136 : }
520 : :
521 : : // Parse a single inner attribute.
522 : : template <typename ManagedTokenSource>
523 : : AST::Attribute
524 : 855 : Parser<ManagedTokenSource>::parse_inner_attribute ()
525 : : {
526 : 1710 : if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
527 : : {
528 : 133 : auto values = parse_doc_comment ();
529 : 133 : auto path = std::move (std::get<0> (values));
530 : 133 : auto input = std::move (std::get<1> (values));
531 : 133 : auto loc = std::get<2> (values);
532 : 133 : return AST::Attribute (std::move (path), std::move (input), loc, true);
533 : 266 : }
534 : :
535 : 1444 : 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 : 722 : lexer.skip_token ();
545 : :
546 : 1444 : 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 : 722 : lexer.skip_token ();
555 : :
556 : 722 : if (!skip_token (LEFT_SQUARE))
557 : 0 : return AST::Attribute::create_empty ();
558 : :
559 : 722 : auto values = parse_attribute_body ();
560 : :
561 : 722 : auto path = std::move (std::get<0> (values));
562 : 722 : auto input = std::move (std::get<1> (values));
563 : 722 : auto loc = std::get<2> (values);
564 : 722 : auto actual_attribute
565 : 722 : = AST::Attribute (std::move (path), std::move (input), loc, true);
566 : :
567 : 722 : if (!skip_token (RIGHT_SQUARE))
568 : 0 : return AST::Attribute::create_empty ();
569 : :
570 : 722 : return actual_attribute;
571 : 1444 : }
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 : 9366 : Parser<ManagedTokenSource>::parse_attribute_body ()
577 : : {
578 : 9366 : location_t locus = lexer.peek_token ()->get_locus ();
579 : :
580 : 9366 : AST::SimplePath attr_path = parse_simple_path ();
581 : : // ensure path is valid to parse attribute input
582 : 9366 : 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 : 9366 : std::unique_ptr<AST::AttrInput> attr_input = parse_attr_input ();
594 : : // AttrInput is allowed to be null, so no checks here
595 : :
596 : 9366 : return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
597 : 9366 : }
598 : :
599 : : /* Determines whether token is a valid simple path segment. This does not
600 : : * include scope resolution operators. */
601 : : inline bool
602 : 15156 : is_simple_path_segment (TokenId id)
603 : : {
604 : 15156 : 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 : 3929 : default:
615 : 3929 : 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 : 11436 : Parser<ManagedTokenSource>::parse_simple_path ()
623 : : {
624 : 11436 : bool has_opening_scope_resolution = false;
625 : 11436 : location_t locus = UNKNOWN_LOCATION;
626 : :
627 : : // don't parse anything if not a path upfront
628 : 22872 : if (!is_simple_path_segment (lexer.peek_token ()->get_id ())
629 : 11698 : && !is_simple_path_segment (lexer.peek_token (1)->get_id ()))
630 : 211 : return AST::SimplePath::create_empty ();
631 : :
632 : : /* Checks for opening scope resolution (i.e. global scope fully-qualified
633 : : * path) */
634 : 22450 : 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 : 11225 : AST::SimplePathSegment segment = parse_simple_path_segment ();
645 : :
646 : : // get location if not gotten already
647 : 11225 : if (locus == UNKNOWN_LOCATION)
648 : 11225 : locus = segment.get_locus ();
649 : :
650 : 11225 : std::vector<AST::SimplePathSegment> segments;
651 : :
652 : : // Return empty vector if first, actually required segment is an error
653 : 11225 : if (segment.is_error ())
654 : 51 : return AST::SimplePath::create_empty ();
655 : :
656 : 11174 : segments.push_back (std::move (segment));
657 : :
658 : : // Parse all other simple path segments
659 : 24482 : while (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
660 : : {
661 : 1148 : AST::SimplePathSegment new_segment = parse_simple_path_segment (1);
662 : :
663 : : // Return path as currently constructed if segment in error state.
664 : 1148 : if (new_segment.is_error ())
665 : : break;
666 : :
667 : 986 : segments.push_back (std::move (new_segment));
668 : : }
669 : :
670 : : // DEBUG: check for any empty segments
671 : 23334 : for (const auto &seg : segments)
672 : : {
673 : 12160 : 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 : 11174 : return AST::SimplePath (std::move (segments), has_opening_scope_resolution,
683 : 11174 : 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 : 11225 : }
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 : 12373 : Parser<ManagedTokenSource>::parse_simple_path_segment (int base_peek)
694 : : {
695 : : using namespace Values;
696 : 12373 : const_TokenPtr t = lexer.peek_token (base_peek);
697 : 12373 : switch (t->get_id ())
698 : : {
699 : 11802 : case IDENTIFIER:
700 : 11802 : lexer.skip_token (base_peek);
701 : :
702 : 23604 : return AST::SimplePathSegment (t->get_str (), t->get_locus ());
703 : 295 : case SUPER:
704 : 295 : lexer.skip_token (base_peek);
705 : :
706 : 295 : return AST::SimplePathSegment (Keywords::SUPER, t->get_locus ());
707 : 57 : case SELF:
708 : 57 : lexer.skip_token (base_peek);
709 : :
710 : 57 : return AST::SimplePathSegment (Keywords::SELF, t->get_locus ());
711 : 6 : case CRATE:
712 : 6 : lexer.skip_token (base_peek);
713 : :
714 : 6 : 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 : 213 : 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 : 12373 : }
738 : :
739 : : // Parses a PathIdentSegment - an identifier segment of a non-SimplePath path.
740 : : template <typename ManagedTokenSource>
741 : : AST::PathIdentSegment
742 : 104116 : Parser<ManagedTokenSource>::parse_path_ident_segment ()
743 : : {
744 : 104116 : const_TokenPtr t = lexer.peek_token ();
745 : 104116 : switch (t->get_id ())
746 : : {
747 : 93457 : case IDENTIFIER:
748 : 93457 : lexer.skip_token ();
749 : :
750 : 186914 : return AST::PathIdentSegment (t->get_str (), t->get_locus ());
751 : 8 : case SUPER:
752 : 8 : lexer.skip_token ();
753 : :
754 : 8 : return AST::PathIdentSegment (Values::Keywords::SUPER, t->get_locus ());
755 : 1707 : case SELF:
756 : 1707 : lexer.skip_token ();
757 : :
758 : 1707 : return AST::PathIdentSegment (Values::Keywords::SELF, t->get_locus ());
759 : 8257 : case SELF_ALIAS:
760 : 8257 : lexer.skip_token ();
761 : :
762 : 16514 : return AST::PathIdentSegment (Values::Keywords::SELF_ALIAS,
763 : 16514 : t->get_locus ());
764 : 484 : case CRATE:
765 : 484 : lexer.skip_token ();
766 : :
767 : 484 : 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 : 203 : return AST::PathIdentSegment::create_error ();
783 : : }
784 : : rust_unreachable ();
785 : : // not necessarily an error
786 : 104116 : }
787 : :
788 : : // Parses an AttrInput AST node (polymorphic, as AttrInput is abstract)
789 : : template <typename ManagedTokenSource>
790 : : std::unique_ptr<AST::AttrInput>
791 : 9366 : Parser<ManagedTokenSource>::parse_attr_input ()
792 : : {
793 : 9366 : const_TokenPtr t = lexer.peek_token ();
794 : 9366 : switch (t->get_id ())
795 : : {
796 : 4238 : case LEFT_PAREN:
797 : : case LEFT_SQUARE:
798 : : case LEFT_CURLY:
799 : : {
800 : : // must be a delimited token tree, so parse that
801 : 4238 : std::unique_ptr<AST::AttrInput> input_tree (
802 : 4238 : new AST::DelimTokenTree (parse_delim_token_tree ()));
803 : :
804 : : // TODO: potential checks on DelimTokenTree before returning
805 : :
806 : : return input_tree;
807 : : }
808 : 3458 : case EQUAL:
809 : : {
810 : : // = LiteralExpr
811 : 3458 : lexer.skip_token ();
812 : :
813 : 3458 : 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 : 3458 : if (is_simple_path_segment (t->get_id ()))
819 : : {
820 : 2 : std::unique_ptr<AST::MacroInvocation> invoke
821 : 2 : = parse_macro_invocation ({});
822 : :
823 : 2 : if (!invoke)
824 : 0 : return nullptr;
825 : :
826 : : return std::unique_ptr<AST::AttrInput> (
827 : 2 : new AST::AttrInputMacro (std::move (invoke)));
828 : 2 : }
829 : :
830 : : /* Ensure token is a "literal expression" (literally only a literal
831 : : * token of any type) */
832 : 3456 : 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 : 3456 : AST::Literal::LitType lit_type = AST::Literal::STRING;
845 : : // Crappy mapping of token type to literal type
846 : 3456 : 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 : 10368 : AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (),
874 : : {}, t->get_locus ());
875 : 3456 : lexer.skip_token ();
876 : :
877 : 3456 : std::unique_ptr<AST::AttrInput> attr_input_lit (
878 : 3456 : 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 : 3456 : return attr_input_lit;
885 : 3456 : }
886 : : break;
887 : 1670 : 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 : 1670 : 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 : 9366 : }
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 : 91151 : token_id_matches_delims (TokenId token_id, AST::DelimType delim_type)
911 : : {
912 : 91151 : return ((token_id == RIGHT_PAREN && delim_type == AST::PARENS)
913 : 68403 : || (token_id == RIGHT_SQUARE && delim_type == AST::SQUARE)
914 : 158510 : || (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 : 13243 : Parser<ManagedTokenSource>::parse_delim_token_tree ()
942 : : {
943 : 13243 : const_TokenPtr t = lexer.peek_token ();
944 : 13243 : lexer.skip_token ();
945 : 13243 : location_t initial_loc = t->get_locus ();
946 : :
947 : : // save delim type to ensure it is reused later
948 : 13243 : AST::DelimType delim_type = AST::PARENS;
949 : :
950 : : // Map tokens to DelimType
951 : 13243 : switch (t->get_id ())
952 : : {
953 : : case LEFT_PAREN:
954 : : delim_type = AST::PARENS;
955 : : break;
956 : 483 : case LEFT_SQUARE:
957 : 483 : delim_type = AST::SQUARE;
958 : 483 : break;
959 : 2628 : case LEFT_CURLY:
960 : 2628 : delim_type = AST::CURLY;
961 : 2628 : 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 : 13243 : std::vector<std::unique_ptr<AST::TokenTree>> token_trees_in_tree;
973 : 13243 : auto delim_open
974 : 13243 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
975 : 13243 : token_trees_in_tree.push_back (std::move (delim_open));
976 : :
977 : : // repeat loop until finding the matching delimiter
978 : 13243 : t = lexer.peek_token ();
979 : 68323 : while (!token_id_matches_delims (t->get_id (), delim_type)
980 : 68323 : && t->get_id () != END_OF_FILE)
981 : : {
982 : 55080 : std::unique_ptr<AST::TokenTree> tok_tree = parse_token_tree ();
983 : :
984 : 55080 : 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 : 55080 : token_trees_in_tree.push_back (std::move (tok_tree));
997 : :
998 : : // lexer.skip_token();
999 : 55080 : t = lexer.peek_token ();
1000 : : }
1001 : 13243 : auto delim_close
1002 : 13243 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1003 : 13243 : token_trees_in_tree.push_back (std::move (delim_close));
1004 : :
1005 : 13243 : AST::DelimTokenTree token_tree (delim_type, std::move (token_trees_in_tree),
1006 : : initial_loc);
1007 : :
1008 : : // parse end delimiters
1009 : 13243 : t = lexer.peek_token ();
1010 : :
1011 : 13243 : if (token_id_matches_delims (t->get_id (), delim_type))
1012 : : {
1013 : : // tokens match opening delimiter, so skip.
1014 : 13236 : lexer.skip_token ();
1015 : :
1016 : : // DEBUG
1017 : 26472 : 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 : 13236 : 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 : 13243 : }
1041 : :
1042 : : // Parses an identifier/keyword as a Token
1043 : : template <typename ManagedTokenSource>
1044 : : std::unique_ptr<AST::Token>
1045 : 493 : Parser<ManagedTokenSource>::parse_identifier_or_keyword_token ()
1046 : : {
1047 : 493 : const_TokenPtr t = lexer.peek_token ();
1048 : :
1049 : 493 : if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ()))
1050 : : {
1051 : 492 : lexer.skip_token ();
1052 : 492 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1053 : : }
1054 : : else
1055 : : {
1056 : 1 : return nullptr;
1057 : : }
1058 : 493 : }
1059 : :
1060 : : /* Parses a TokenTree syntactical production. This is either a delimited token
1061 : : * tree or a non-delimiter token. */
1062 : : template <typename ManagedTokenSource>
1063 : : std::unique_ptr<AST::TokenTree>
1064 : 59023 : Parser<ManagedTokenSource>::parse_token_tree ()
1065 : : {
1066 : 59023 : const_TokenPtr t = lexer.peek_token ();
1067 : :
1068 : 59023 : switch (t->get_id ())
1069 : : {
1070 : 5399 : case LEFT_PAREN:
1071 : : case LEFT_SQUARE:
1072 : : case LEFT_CURLY:
1073 : : // Parse delimited token tree
1074 : : // TODO: use move rather than copy constructor
1075 : 5399 : return std::unique_ptr<AST::DelimTokenTree> (
1076 : 5399 : new AST::DelimTokenTree (parse_delim_token_tree ()));
1077 : 1 : case RIGHT_PAREN:
1078 : : case RIGHT_SQUARE:
1079 : : case RIGHT_CURLY:
1080 : : // error - should not be called when this a token
1081 : 1 : add_error (
1082 : 1 : Error (t->get_locus (),
1083 : : "unexpected closing delimiter %qs - token tree requires "
1084 : : "either paired delimiters or non-delimiter tokens",
1085 : : t->get_token_description ()));
1086 : :
1087 : 1 : lexer.skip_token ();
1088 : 1 : return nullptr;
1089 : 53623 : default:
1090 : : // parse token itself as TokenTree
1091 : 53623 : lexer.skip_token ();
1092 : 53623 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1093 : : }
1094 : 59023 : }
1095 : :
1096 : : template <typename ManagedTokenSource>
1097 : : bool
1098 : 1467 : Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
1099 : : {
1100 : 1467 : auto macro_name = lexer.peek_token (2)->get_id ();
1101 : :
1102 : 1467 : bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
1103 : :
1104 : 1467 : return t->get_str () == Values::WeakKeywords::MACRO_RULES
1105 : 2377 : && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
1106 : : }
1107 : :
1108 : : // Parses a single item
1109 : : template <typename ManagedTokenSource>
1110 : : std::unique_ptr<AST::Item>
1111 : 23317 : Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
1112 : : {
1113 : : // has a "called_from_statement" parameter for better error message handling
1114 : :
1115 : : // parse outer attributes for item
1116 : 23317 : AST::AttrVec outer_attrs = parse_outer_attributes ();
1117 : 23317 : const_TokenPtr t = lexer.peek_token ();
1118 : :
1119 : 23317 : switch (t->get_id ())
1120 : : {
1121 : 7 : case END_OF_FILE:
1122 : : // not necessarily an error, unless we just read outer
1123 : : // attributes which needs to be attached
1124 : 7 : if (!outer_attrs.empty ())
1125 : : {
1126 : 0 : Rust::AST::Attribute attr = outer_attrs.back ();
1127 : 0 : Error error (attr.get_locus (),
1128 : : "expected item after outer attribute or doc comment");
1129 : 0 : add_error (std::move (error));
1130 : 0 : }
1131 : 7 : return nullptr;
1132 : :
1133 : 21987 : case ASYNC:
1134 : : case PUB:
1135 : : case MOD:
1136 : : case EXTERN_KW:
1137 : : case USE:
1138 : : case FN_KW:
1139 : : case TYPE:
1140 : : case STRUCT_KW:
1141 : : case ENUM_KW:
1142 : : case CONST:
1143 : : case STATIC_KW:
1144 : : case AUTO:
1145 : : case TRAIT:
1146 : : case IMPL:
1147 : : case MACRO:
1148 : : /* TODO: implement union keyword but not really because of
1149 : : * context-dependence crappy hack way to parse a union written below to
1150 : : * separate it from the good code. */
1151 : : // case UNION:
1152 : : case UNSAFE: // maybe - unsafe traits are a thing
1153 : : // if any of these (should be all possible VisItem prefixes), parse a
1154 : : // VisItem
1155 : 21987 : return parse_vis_item (std::move (outer_attrs));
1156 : : break;
1157 : 3 : case SUPER:
1158 : : case SELF:
1159 : : case CRATE:
1160 : : case DOLLAR_SIGN:
1161 : : // almost certainly macro invocation semi
1162 : 6 : return parse_macro_invocation_semi (std::move (outer_attrs));
1163 : : break;
1164 : : // crappy hack to do union "keyword"
1165 : 1315 : case IDENTIFIER:
1166 : : // TODO: ensure std::string and literal comparison works
1167 : 2571 : if (t->get_str () == Values::WeakKeywords::UNION
1168 : 1378 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
1169 : : {
1170 : 63 : return parse_vis_item (std::move (outer_attrs));
1171 : : // or should this go straight to parsing union?
1172 : : }
1173 : 2445 : else if (t->get_str () == Values::WeakKeywords::DEFAULT
1174 : 1254 : && lexer.peek_token (1)->get_id () != EXCLAM)
1175 : : {
1176 : 1 : add_error (Error (t->get_locus (),
1177 : : "%qs is only allowed on items within %qs blocks",
1178 : : "default", "impl"));
1179 : 1 : return nullptr;
1180 : : }
1181 : 2502 : else if (is_macro_rules_def (t))
1182 : : {
1183 : : // macro_rules! macro item
1184 : 907 : return parse_macro_rules_def (std::move (outer_attrs));
1185 : : }
1186 : 688 : else if (lexer.peek_token (1)->get_id () == SCOPE_RESOLUTION
1187 : 687 : || lexer.peek_token (1)->get_id () == EXCLAM)
1188 : : {
1189 : : /* path (probably) or macro invocation, so probably a macro invocation
1190 : : * semi */
1191 : 680 : return parse_macro_invocation_semi (std::move (outer_attrs));
1192 : : }
1193 : : gcc_fallthrough ();
1194 : : default:
1195 : : // otherwise unrecognised
1196 : 18 : add_error (Error (t->get_locus (),
1197 : : "unrecognised token %qs for start of %s",
1198 : : t->get_token_description (),
1199 : : called_from_statement ? "statement" : "item"));
1200 : :
1201 : : // skip somewhere?
1202 : 9 : return nullptr;
1203 : : break;
1204 : : }
1205 : 23317 : }
1206 : :
1207 : : // Parses a contiguous block of outer attributes.
1208 : : template <typename ManagedTokenSource>
1209 : : AST::AttrVec
1210 : 101016 : Parser<ManagedTokenSource>::parse_outer_attributes ()
1211 : : {
1212 : 101016 : AST::AttrVec outer_attributes;
1213 : :
1214 : 117636 : while (lexer.peek_token ()->get_id ()
1215 : : == HASH /* Can also be #!, which catches errors. */
1216 : 226622 : || lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT
1217 : 445241 : || lexer.peek_token ()->get_id ()
1218 : : == INNER_DOC_COMMENT) /* For error handling. */
1219 : : {
1220 : 16603 : AST::Attribute outer_attr = parse_outer_attribute ();
1221 : :
1222 : : /* Ensure only valid outer attributes are added to the outer_attributes
1223 : : * list */
1224 : 16603 : if (!outer_attr.is_empty ())
1225 : : {
1226 : 16594 : outer_attributes.push_back (std::move (outer_attr));
1227 : : }
1228 : : else
1229 : : {
1230 : : /* If no more valid outer attributes, break out of loop (only
1231 : : * contiguous outer attributes parsed). */
1232 : : break;
1233 : : }
1234 : : }
1235 : :
1236 : 101016 : outer_attributes.shrink_to_fit ();
1237 : 101016 : return outer_attributes;
1238 : :
1239 : : /* TODO: this shares basically all code with parse_inner_attributes except
1240 : : * function call - find way of making it more modular? function pointer? */
1241 : : }
1242 : :
1243 : : // Parse a single outer attribute.
1244 : : template <typename ManagedTokenSource>
1245 : : AST::Attribute
1246 : 16603 : Parser<ManagedTokenSource>::parse_outer_attribute ()
1247 : : {
1248 : 33206 : if (lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT)
1249 : : {
1250 : 8003 : auto values = parse_doc_comment ();
1251 : 8003 : auto path = std::move (std::get<0> (values));
1252 : 8003 : auto input = std::move (std::get<1> (values));
1253 : 8003 : auto loc = std::get<2> (values);
1254 : 8003 : return AST::Attribute (std::move (path), std::move (input), loc, false);
1255 : 16006 : }
1256 : :
1257 : 17200 : if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
1258 : : {
1259 : 2 : Error error (
1260 : 2 : lexer.peek_token ()->get_locus (), ErrorCode::E0753,
1261 : : "expected outer doc comment, inner doc (%<//!%> or %</*!%>) only "
1262 : : "allowed at start of item "
1263 : : "and before any outer attribute or doc (%<#[%>, %<///%> or %</**%>)");
1264 : 2 : add_error (std::move (error));
1265 : 2 : lexer.skip_token ();
1266 : 2 : return AST::Attribute::create_empty ();
1267 : 2 : }
1268 : :
1269 : : /* OuterAttribute -> '#' '[' Attr ']' */
1270 : :
1271 : 17196 : if (lexer.peek_token ()->get_id () != HASH)
1272 : 0 : return AST::Attribute::create_empty ();
1273 : :
1274 : 8598 : lexer.skip_token ();
1275 : :
1276 : 8598 : TokenId id = lexer.peek_token ()->get_id ();
1277 : 8598 : if (id != LEFT_SQUARE)
1278 : : {
1279 : 0 : if (id == EXCLAM)
1280 : : {
1281 : : // this is inner attribute syntax, so throw error
1282 : : // inner attributes were either already parsed or not allowed here.
1283 : 0 : Error error (
1284 : 0 : lexer.peek_token ()->get_locus (),
1285 : : "token %<!%> found, indicating inner attribute definition. Inner "
1286 : : "attributes are not possible at this location");
1287 : 0 : add_error (std::move (error));
1288 : 0 : }
1289 : 0 : return AST::Attribute::create_empty ();
1290 : : }
1291 : :
1292 : 8598 : lexer.skip_token ();
1293 : :
1294 : 8598 : auto values = parse_attribute_body ();
1295 : 8598 : auto path = std::move (std::get<0> (values));
1296 : 8598 : auto input = std::move (std::get<1> (values));
1297 : 8598 : auto loc = std::get<2> (values);
1298 : 8598 : auto actual_attribute
1299 : 8598 : = AST::Attribute (std::move (path), std::move (input), loc, false);
1300 : :
1301 : 17196 : if (lexer.peek_token ()->get_id () != RIGHT_SQUARE)
1302 : 7 : return AST::Attribute::create_empty ();
1303 : :
1304 : 8591 : lexer.skip_token ();
1305 : :
1306 : 8591 : return actual_attribute;
1307 : 17196 : }
1308 : :
1309 : : // Parses a VisItem (item that can have non-default visibility).
1310 : : template <typename ManagedTokenSource>
1311 : : std::unique_ptr<AST::VisItem>
1312 : 22569 : Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
1313 : : {
1314 : : // parse visibility, which may or may not exist
1315 : 22569 : AST::Visibility vis = parse_visibility ();
1316 : :
1317 : : // select VisItem to create depending on keyword
1318 : 22569 : const_TokenPtr t = lexer.peek_token ();
1319 : :
1320 : 22569 : switch (t->get_id ())
1321 : : {
1322 : 1278 : case MOD:
1323 : 1278 : return parse_module (std::move (vis), std::move (outer_attrs));
1324 : 1466 : case EXTERN_KW:
1325 : : // lookahead to resolve syntactical production
1326 : 1466 : t = lexer.peek_token (1);
1327 : :
1328 : 1466 : switch (t->get_id ())
1329 : : {
1330 : 27 : case CRATE:
1331 : 27 : return parse_extern_crate (std::move (vis), std::move (outer_attrs));
1332 : 0 : case FN_KW: // extern function
1333 : 0 : return parse_function (std::move (vis), std::move (outer_attrs));
1334 : 0 : case LEFT_CURLY: // extern block
1335 : 0 : return parse_extern_block (std::move (vis), std::move (outer_attrs));
1336 : 1439 : case STRING_LITERAL: // for specifying extern ABI
1337 : : // could be extern block or extern function, so more lookahead
1338 : 1439 : t = lexer.peek_token (2);
1339 : :
1340 : 1439 : switch (t->get_id ())
1341 : : {
1342 : 2 : case FN_KW:
1343 : 2 : return parse_function (std::move (vis), std::move (outer_attrs));
1344 : 1437 : case LEFT_CURLY:
1345 : 1437 : return parse_extern_block (std::move (vis),
1346 : 1437 : std::move (outer_attrs));
1347 : 0 : default:
1348 : 0 : add_error (
1349 : 0 : Error (t->get_locus (),
1350 : : "unexpected token %qs in some sort of extern production",
1351 : : t->get_token_description ()));
1352 : :
1353 : 0 : lexer.skip_token (2); // TODO: is this right thing to do?
1354 : 0 : return nullptr;
1355 : : }
1356 : 0 : default:
1357 : 0 : add_error (
1358 : 0 : Error (t->get_locus (),
1359 : : "unexpected token %qs in some sort of extern production",
1360 : : t->get_token_description ()));
1361 : :
1362 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1363 : 0 : return nullptr;
1364 : : }
1365 : 647 : case USE:
1366 : 647 : return parse_use_decl (std::move (vis), std::move (outer_attrs));
1367 : 6141 : case FN_KW:
1368 : 6141 : return parse_function (std::move (vis), std::move (outer_attrs));
1369 : 53 : case TYPE:
1370 : 53 : return parse_type_alias (std::move (vis), std::move (outer_attrs));
1371 : 2600 : case STRUCT_KW:
1372 : 2600 : return parse_struct (std::move (vis), std::move (outer_attrs));
1373 : 534 : case ENUM_KW:
1374 : 534 : return parse_enum (std::move (vis), std::move (outer_attrs));
1375 : : // TODO: implement union keyword but not really because of
1376 : : // context-dependence case UNION: crappy hack to do union "keyword"
1377 : 107 : case IDENTIFIER:
1378 : 214 : if (t->get_str () == Values::WeakKeywords::UNION
1379 : 214 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
1380 : : {
1381 : 107 : return parse_union (std::move (vis), std::move (outer_attrs));
1382 : : // or should item switch go straight to parsing union?
1383 : : }
1384 : : else
1385 : : {
1386 : : break;
1387 : : }
1388 : 536 : case CONST:
1389 : : // lookahead to resolve syntactical production
1390 : 536 : t = lexer.peek_token (1);
1391 : :
1392 : 536 : switch (t->get_id ())
1393 : : {
1394 : 461 : case IDENTIFIER:
1395 : : case UNDERSCORE:
1396 : 461 : return parse_const_item (std::move (vis), std::move (outer_attrs));
1397 : 1 : case ASYNC:
1398 : 1 : return parse_async_item (std::move (vis), std::move (outer_attrs));
1399 : 74 : case UNSAFE:
1400 : : case EXTERN_KW:
1401 : : case FN_KW:
1402 : 74 : return parse_function (std::move (vis), std::move (outer_attrs));
1403 : 0 : default:
1404 : 0 : add_error (
1405 : 0 : Error (t->get_locus (),
1406 : : "unexpected token %qs in some sort of const production",
1407 : : t->get_token_description ()));
1408 : :
1409 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1410 : 0 : return nullptr;
1411 : : }
1412 : : // for async functions
1413 : 2 : case ASYNC:
1414 : 2 : return parse_async_item (std::move (vis), std::move (outer_attrs));
1415 : :
1416 : 58 : case STATIC_KW:
1417 : 58 : return parse_static_item (std::move (vis), std::move (outer_attrs));
1418 : 3612 : case AUTO:
1419 : : case TRAIT:
1420 : 3612 : return parse_trait (std::move (vis), std::move (outer_attrs));
1421 : 5174 : case IMPL:
1422 : 5174 : return parse_impl (std::move (vis), std::move (outer_attrs));
1423 : 315 : case UNSAFE: // unsafe traits, unsafe functions, unsafe impls (trait impls),
1424 : : // lookahead to resolve syntactical production
1425 : 315 : t = lexer.peek_token (1);
1426 : :
1427 : 315 : switch (t->get_id ())
1428 : : {
1429 : 56 : case AUTO:
1430 : : case TRAIT:
1431 : 56 : return parse_trait (std::move (vis), std::move (outer_attrs));
1432 : 191 : case EXTERN_KW:
1433 : : case FN_KW:
1434 : 191 : return parse_function (std::move (vis), std::move (outer_attrs));
1435 : 67 : case IMPL:
1436 : 67 : return parse_impl (std::move (vis), std::move (outer_attrs));
1437 : 1 : case MOD:
1438 : 1 : return parse_module (std::move (vis), std::move (outer_attrs));
1439 : 0 : default:
1440 : 0 : add_error (
1441 : 0 : Error (t->get_locus (),
1442 : : "unexpected token %qs in some sort of unsafe production",
1443 : : t->get_token_description ()));
1444 : :
1445 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
1446 : 0 : return nullptr;
1447 : : }
1448 : 45 : case MACRO:
1449 : 45 : return parse_decl_macro_def (std::move (vis), std::move (outer_attrs));
1450 : : default:
1451 : : // otherwise vis item clearly doesn't exist, which is not an error
1452 : : // has a catch-all post-switch return to allow other breaks to occur
1453 : : break;
1454 : : }
1455 : 1 : return nullptr;
1456 : 22569 : }
1457 : :
1458 : : template <typename ManagedTokenSource>
1459 : : std::unique_ptr<AST::Function>
1460 : 4 : Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
1461 : : AST::AttrVec outer_attrs)
1462 : : {
1463 : 4 : auto offset = (lexer.peek_token ()->get_id () == CONST) ? 1 : 0;
1464 : 4 : const_TokenPtr t = lexer.peek_token (offset);
1465 : :
1466 : 4 : if (get_rust_edition () == Edition::E2015)
1467 : : {
1468 : 1 : add_error (Error (t->get_locus (), ErrorCode::E0670,
1469 : : "%<async fn%> is not permitted in Rust 2015"));
1470 : 1 : add_error (
1471 : 2 : Error::Hint (t->get_locus (),
1472 : : "to use %<async fn%>, switch to Rust 2018 or later"));
1473 : : }
1474 : :
1475 : 4 : t = lexer.peek_token (offset + 1);
1476 : :
1477 : 4 : switch (t->get_id ())
1478 : : {
1479 : 4 : case UNSAFE:
1480 : : case FN_KW:
1481 : 4 : return parse_function (std::move (vis), std::move (outer_attrs));
1482 : :
1483 : 0 : default:
1484 : 0 : add_error (
1485 : 0 : Error (t->get_locus (), "expected item, found keyword %<async%>"));
1486 : :
1487 : 0 : lexer.skip_token (1);
1488 : 0 : return nullptr;
1489 : : }
1490 : 4 : }
1491 : :
1492 : : // Parses a macro rules definition syntax extension whatever thing.
1493 : : template <typename ManagedTokenSource>
1494 : : std::unique_ptr<AST::MacroRulesDefinition>
1495 : 913 : Parser<ManagedTokenSource>::parse_macro_rules_def (AST::AttrVec outer_attrs)
1496 : : {
1497 : : // ensure that first token is identifier saying "macro_rules"
1498 : 913 : const_TokenPtr t = lexer.peek_token ();
1499 : 913 : if (t->get_id () != IDENTIFIER
1500 : 913 : || t->get_str () != Values::WeakKeywords::MACRO_RULES)
1501 : : {
1502 : 0 : Error error (
1503 : : t->get_locus (),
1504 : : "macro rules definition does not start with %<macro_rules%>");
1505 : 0 : add_error (std::move (error));
1506 : :
1507 : : // skip after somewhere?
1508 : 0 : return nullptr;
1509 : 0 : }
1510 : 913 : lexer.skip_token ();
1511 : 913 : location_t macro_locus = t->get_locus ();
1512 : :
1513 : 913 : if (!skip_token (EXCLAM))
1514 : : {
1515 : : // skip after somewhere?
1516 : 0 : return nullptr;
1517 : : }
1518 : :
1519 : : // parse macro name
1520 : 913 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
1521 : 913 : if (ident_tok == nullptr)
1522 : : {
1523 : 1 : return nullptr;
1524 : : }
1525 : 912 : Identifier rule_name{ident_tok};
1526 : :
1527 : : // DEBUG
1528 : 912 : rust_debug ("in macro rules def, about to parse parens.");
1529 : :
1530 : : // save delim type to ensure it is reused later
1531 : 912 : AST::DelimType delim_type = AST::PARENS;
1532 : :
1533 : : // Map tokens to DelimType
1534 : 912 : t = lexer.peek_token ();
1535 : 912 : switch (t->get_id ())
1536 : : {
1537 : : case LEFT_PAREN:
1538 : : delim_type = AST::PARENS;
1539 : : break;
1540 : 0 : case LEFT_SQUARE:
1541 : 0 : delim_type = AST::SQUARE;
1542 : 0 : break;
1543 : 912 : case LEFT_CURLY:
1544 : 912 : delim_type = AST::CURLY;
1545 : 912 : break;
1546 : 0 : default:
1547 : 0 : add_error (Error (t->get_locus (),
1548 : : "unexpected token %qs - expecting delimiters (for a "
1549 : : "macro rules definition)",
1550 : : t->get_token_description ()));
1551 : :
1552 : 0 : return nullptr;
1553 : : }
1554 : 912 : lexer.skip_token ();
1555 : :
1556 : : // parse actual macro rules
1557 : 912 : std::vector<AST::MacroRule> macro_rules;
1558 : :
1559 : : // must be at least one macro rule, so parse it
1560 : 912 : AST::MacroRule initial_rule = parse_macro_rule ();
1561 : 912 : if (initial_rule.is_error ())
1562 : : {
1563 : 12 : Error error (lexer.peek_token ()->get_locus (),
1564 : : "required first macro rule in macro rules definition "
1565 : : "could not be parsed");
1566 : 12 : add_error (std::move (error));
1567 : :
1568 : : // skip after somewhere?
1569 : 12 : return nullptr;
1570 : 12 : }
1571 : 900 : macro_rules.push_back (std::move (initial_rule));
1572 : :
1573 : : // DEBUG
1574 : 900 : rust_debug ("successfully pushed back initial macro rule");
1575 : :
1576 : 900 : t = lexer.peek_token ();
1577 : : // parse macro rules
1578 : 1023 : while (t->get_id () == SEMICOLON)
1579 : : {
1580 : : // skip semicolon
1581 : 607 : lexer.skip_token ();
1582 : :
1583 : : // don't parse if end of macro rules
1584 : 1214 : if (token_id_matches_delims (lexer.peek_token ()->get_id (), delim_type))
1585 : : {
1586 : : // DEBUG
1587 : 484 : rust_debug (
1588 : : "broke out of parsing macro rules loop due to finding delim");
1589 : :
1590 : 484 : break;
1591 : : }
1592 : :
1593 : : // try to parse next rule
1594 : 123 : AST::MacroRule rule = parse_macro_rule ();
1595 : 123 : if (rule.is_error ())
1596 : : {
1597 : 0 : Error error (lexer.peek_token ()->get_locus (),
1598 : : "failed to parse macro rule in macro rules definition");
1599 : 0 : add_error (std::move (error));
1600 : :
1601 : 0 : return nullptr;
1602 : 0 : }
1603 : :
1604 : 123 : macro_rules.push_back (std::move (rule));
1605 : :
1606 : : // DEBUG
1607 : 123 : rust_debug ("successfully pushed back another macro rule");
1608 : :
1609 : 123 : t = lexer.peek_token ();
1610 : : }
1611 : :
1612 : : // parse end delimiters
1613 : 900 : t = lexer.peek_token ();
1614 : 900 : if (token_id_matches_delims (t->get_id (), delim_type))
1615 : : {
1616 : : // tokens match opening delimiter, so skip.
1617 : 900 : lexer.skip_token ();
1618 : :
1619 : 900 : if (delim_type != AST::CURLY)
1620 : : {
1621 : : // skip semicolon at end of non-curly macro definitions
1622 : 0 : if (!skip_token (SEMICOLON))
1623 : : {
1624 : : // as this is the end, allow recovery (probably) - may change
1625 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1626 : 0 : AST::MacroRulesDefinition::mbe (
1627 : : std::move (rule_name), delim_type, std::move (macro_rules),
1628 : 0 : std::move (outer_attrs), macro_locus));
1629 : : }
1630 : : }
1631 : :
1632 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1633 : 1800 : AST::MacroRulesDefinition::mbe (std::move (rule_name), delim_type,
1634 : : std::move (macro_rules),
1635 : 900 : std::move (outer_attrs), macro_locus));
1636 : : }
1637 : : else
1638 : : {
1639 : : // tokens don't match opening delimiters, so produce error
1640 : 0 : Error error (t->get_locus (),
1641 : : "unexpected token %qs - expecting closing delimiter %qs "
1642 : : "(for a macro rules definition)",
1643 : : t->get_token_description (),
1644 : : (delim_type == AST::PARENS
1645 : : ? ")"
1646 : : : (delim_type == AST::SQUARE ? "]" : "}")));
1647 : 0 : add_error (std::move (error));
1648 : :
1649 : : /* return empty macro definiton despite possibly parsing mostly valid one
1650 : : * - TODO is this a good idea? */
1651 : 0 : return nullptr;
1652 : 0 : }
1653 : 2737 : }
1654 : :
1655 : : // Parses a declarative macro 2.0 definition.
1656 : : template <typename ManagedTokenSource>
1657 : : std::unique_ptr<AST::MacroRulesDefinition>
1658 : 45 : Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis,
1659 : : AST::AttrVec outer_attrs)
1660 : : {
1661 : : // ensure that first token is identifier saying "macro"
1662 : 45 : const_TokenPtr t = lexer.peek_token ();
1663 : 45 : if (t->get_id () != MACRO)
1664 : : {
1665 : 0 : Error error (
1666 : : t->get_locus (),
1667 : : "declarative macro definition does not start with %<macro%>");
1668 : 0 : add_error (std::move (error));
1669 : :
1670 : : // skip after somewhere?
1671 : 0 : return nullptr;
1672 : 0 : }
1673 : 45 : lexer.skip_token ();
1674 : 45 : location_t macro_locus = t->get_locus ();
1675 : :
1676 : : // parse macro name
1677 : 45 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
1678 : 45 : if (ident_tok == nullptr)
1679 : : {
1680 : 0 : return nullptr;
1681 : : }
1682 : 45 : Identifier rule_name{ident_tok};
1683 : :
1684 : 45 : t = lexer.peek_token ();
1685 : 45 : if (t->get_id () == LEFT_PAREN)
1686 : : {
1687 : : // single definiton of macro rule
1688 : : // e.g. `macro foo($e:expr) {}`
1689 : :
1690 : : // parse macro matcher
1691 : 20 : location_t locus = lexer.peek_token ()->get_locus ();
1692 : 20 : AST::MacroMatcher matcher = parse_macro_matcher ();
1693 : 20 : if (matcher.is_error ())
1694 : 0 : return nullptr;
1695 : :
1696 : : // check delimiter of macro matcher
1697 : 20 : if (matcher.get_delim_type () != AST::DelimType::PARENS)
1698 : : {
1699 : 0 : Error error (locus, "only parenthesis can be used for a macro "
1700 : : "matcher in declarative macro definition");
1701 : 0 : add_error (std::move (error));
1702 : 0 : return nullptr;
1703 : 0 : }
1704 : :
1705 : 20 : location_t transcriber_loc = lexer.peek_token ()->get_locus ();
1706 : 20 : AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
1707 : 20 : AST::MacroTranscriber transcriber (delim_tok_tree, transcriber_loc);
1708 : :
1709 : 20 : if (transcriber.get_token_tree ().get_delim_type ()
1710 : : != AST::DelimType::CURLY)
1711 : : {
1712 : 1 : Error error (transcriber_loc,
1713 : : "only braces can be used for a macro transcriber "
1714 : : "in declarative macro definition");
1715 : 1 : add_error (std::move (error));
1716 : 1 : return nullptr;
1717 : 1 : }
1718 : :
1719 : 19 : AST::MacroRule macro_rule
1720 : 38 : = AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
1721 : 19 : std::vector<AST::MacroRule> macro_rules;
1722 : 19 : macro_rules.push_back (macro_rule);
1723 : :
1724 : : return std::unique_ptr<AST::MacroRulesDefinition> (
1725 : 38 : AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
1726 : : macro_rules,
1727 : : std::move (outer_attrs),
1728 : 19 : macro_locus, vis));
1729 : 59 : }
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 : 90 : }
1813 : :
1814 : : // Parses a semi-coloned (except for full block) macro invocation item.
1815 : : template <typename ManagedTokenSource>
1816 : : std::unique_ptr<AST::MacroInvocation>
1817 : 378 : Parser<ManagedTokenSource>::parse_macro_invocation_semi (
1818 : : AST::AttrVec outer_attrs)
1819 : : {
1820 : 378 : location_t macro_locus = lexer.peek_token ()->get_locus ();
1821 : 378 : AST::SimplePath path = parse_simple_path ();
1822 : :
1823 : 378 : 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 : 378 : AST::DelimType delim_type = AST::PARENS;
1831 : :
1832 : : // Map tokens to DelimType
1833 : 378 : const_TokenPtr t = lexer.peek_token ();
1834 : 378 : 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 : 226 : case LEFT_CURLY:
1843 : 226 : delim_type = AST::CURLY;
1844 : 226 : 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 : 378 : location_t tok_tree_locus = t->get_locus ();
1854 : 378 : lexer.skip_token ();
1855 : :
1856 : : // parse actual token trees
1857 : 378 : std::vector<std::unique_ptr<AST::TokenTree>> token_trees;
1858 : 378 : auto delim_open
1859 : 378 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1860 : 378 : token_trees.push_back (std::move (delim_open));
1861 : :
1862 : 378 : t = lexer.peek_token ();
1863 : : // parse token trees until the initial delimiter token is found again
1864 : 4047 : while (!token_id_matches_delims (t->get_id (), delim_type))
1865 : : {
1866 : 3669 : std::unique_ptr<AST::TokenTree> tree = parse_token_tree ();
1867 : :
1868 : 3669 : 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 : 3669 : token_trees.push_back (std::move (tree));
1880 : :
1881 : 3669 : t = lexer.peek_token ();
1882 : : }
1883 : 378 : auto delim_close
1884 : 378 : = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
1885 : 378 : token_trees.push_back (std::move (delim_close));
1886 : :
1887 : 378 : AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees),
1888 : : tok_tree_locus);
1889 : 378 : AST::MacroInvocData invoc_data (std::move (path), std::move (delim_tok_tree));
1890 : :
1891 : : // parse end delimiters
1892 : 378 : t = lexer.peek_token ();
1893 : 378 : if (token_id_matches_delims (t->get_id (), delim_type))
1894 : : {
1895 : : // tokens match opening delimiter, so skip.
1896 : 378 : lexer.skip_token ();
1897 : :
1898 : 378 : if (delim_type != AST::CURLY)
1899 : : {
1900 : : // skip semicolon at end of non-curly macro invocation semis
1901 : 152 : 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 : 756 : 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 : 756 : return AST::MacroInvocation::Regular (std::move (invoc_data),
1917 : : std::move (outer_attrs),
1918 : 378 : 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 : 756 : }
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 : 349 : Parser<ManagedTokenSource>::parse_macro_invocation (AST::AttrVec outer_attrs)
1942 : : {
1943 : : // parse macro path
1944 : 349 : AST::SimplePath macro_path = parse_simple_path ();
1945 : 349 : if (macro_path.is_empty ())
1946 : : {
1947 : 262 : Error error (lexer.peek_token ()->get_locus (),
1948 : : "failed to parse macro invocation path");
1949 : 262 : add_error (std::move (error));
1950 : :
1951 : : // skip?
1952 : 262 : return nullptr;
1953 : 262 : }
1954 : :
1955 : 87 : if (!skip_token (EXCLAM))
1956 : : {
1957 : : // skip after somewhere?
1958 : 3 : return nullptr;
1959 : : }
1960 : :
1961 : : // parse internal delim token tree
1962 : 84 : AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
1963 : :
1964 : 84 : location_t macro_locus = macro_path.get_locus ();
1965 : :
1966 : 168 : return AST::MacroInvocation::Regular (
1967 : 168 : AST::MacroInvocData (std::move (macro_path), std::move (delim_tok_tree)),
1968 : 84 : std::move (outer_attrs), macro_locus);
1969 : 84 : }
1970 : :
1971 : : // Parses a macro rule definition - does not parse semicolons.
1972 : : template <typename ManagedTokenSource>
1973 : : AST::MacroRule
1974 : 1076 : Parser<ManagedTokenSource>::parse_macro_rule ()
1975 : : {
1976 : 1076 : location_t locus = lexer.peek_token ()->get_locus ();
1977 : :
1978 : : // parse macro matcher
1979 : 1076 : AST::MacroMatcher matcher = parse_macro_matcher ();
1980 : :
1981 : 1076 : if (matcher.is_error ())
1982 : 13 : return AST::MacroRule::create_error (locus);
1983 : :
1984 : 1063 : 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 : 1063 : location_t token_tree_loc = lexer.peek_token ()->get_locus ();
1992 : 1063 : AST::MacroTranscriber transcriber (parse_delim_token_tree (), token_tree_loc);
1993 : :
1994 : 1063 : return AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
1995 : 1063 : }
1996 : :
1997 : : // Parses a macro matcher (part of a macro rule definition).
1998 : : template <typename ManagedTokenSource>
1999 : : AST::MacroMatcher
2000 : 1163 : Parser<ManagedTokenSource>::parse_macro_matcher ()
2001 : : {
2002 : : // save delim type to ensure it is reused later
2003 : 1163 : AST::DelimType delim_type = AST::PARENS;
2004 : :
2005 : : // DEBUG
2006 : 1163 : rust_debug ("begun parsing macro matcher");
2007 : :
2008 : : // Map tokens to DelimType
2009 : 1163 : const_TokenPtr t = lexer.peek_token ();
2010 : 1163 : location_t locus = t->get_locus ();
2011 : 1163 : switch (t->get_id ())
2012 : : {
2013 : : case LEFT_PAREN:
2014 : : delim_type = AST::PARENS;
2015 : : break;
2016 : 39 : case LEFT_SQUARE:
2017 : 39 : delim_type = AST::SQUARE;
2018 : 39 : break;
2019 : 13 : case LEFT_CURLY:
2020 : 13 : delim_type = AST::CURLY;
2021 : 13 : 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 : 1162 : lexer.skip_token ();
2031 : :
2032 : : // parse actual macro matches
2033 : 1162 : 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 : 1162 : std::vector<const AST::MacroMatch *> last_matches;
2038 : :
2039 : 1162 : t = lexer.peek_token ();
2040 : : // parse token trees until the initial delimiter token is found again
2041 : 2517 : while (!token_id_matches_delims (t->get_id (), delim_type))
2042 : : {
2043 : 1355 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
2044 : :
2045 : 1355 : 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 : 1354 : if (matches.size () > 0)
2057 : : {
2058 : 539 : const auto *last_match = matches.back ().get ();
2059 : :
2060 : : // We want to check if we are dealing with a zeroable repetition
2061 : 539 : bool zeroable = false;
2062 : 539 : if (last_match->get_macro_match_type ()
2063 : : == AST::MacroMatch::MacroMatchType::Repetition)
2064 : : {
2065 : 23 : auto repetition
2066 : : = static_cast<const AST::MacroMatchRepetition *> (last_match);
2067 : :
2068 : 23 : if (repetition->get_op ()
2069 : : != AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE)
2070 : : zeroable = true;
2071 : : }
2072 : :
2073 : : if (!zeroable)
2074 : 539 : last_matches.clear ();
2075 : :
2076 : 539 : last_matches.emplace_back (last_match);
2077 : :
2078 : 1069 : for (auto last : last_matches)
2079 : 542 : if (!is_match_compatible (*last, *match))
2080 : : return AST::MacroMatcher::create_error (
2081 : 12 : match->get_match_locus ());
2082 : : }
2083 : :
2084 : 1342 : matches.push_back (std::move (match));
2085 : :
2086 : : // DEBUG
2087 : 1342 : rust_debug ("pushed back a match in macro matcher");
2088 : :
2089 : 1342 : t = lexer.peek_token ();
2090 : : }
2091 : :
2092 : : // parse end delimiters
2093 : 1149 : t = lexer.peek_token ();
2094 : 1149 : if (token_id_matches_delims (t->get_id (), delim_type))
2095 : : {
2096 : : // tokens match opening delimiter, so skip.
2097 : 1149 : lexer.skip_token ();
2098 : :
2099 : 1149 : 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 : 1162 : }
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 : 2001 : Parser<ManagedTokenSource>::parse_macro_match ()
2123 : : {
2124 : : // branch based on token available
2125 : 2001 : const_TokenPtr t = lexer.peek_token ();
2126 : 2001 : switch (t->get_id ())
2127 : : {
2128 : 67 : case LEFT_PAREN:
2129 : : case LEFT_SQUARE:
2130 : : case LEFT_CURLY:
2131 : : {
2132 : : // must be macro matcher as delimited
2133 : 67 : AST::MacroMatcher matcher = parse_macro_matcher ();
2134 : 67 : 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 : 66 : return std::unique_ptr<AST::MacroMatcher> (
2143 : 66 : new AST::MacroMatcher (std::move (matcher)));
2144 : 67 : }
2145 : 1440 : case DOLLAR_SIGN:
2146 : : {
2147 : : // have to do more lookahead to determine if fragment or repetition
2148 : 1440 : const_TokenPtr t2 = lexer.peek_token (1);
2149 : 1440 : switch (t2->get_id ())
2150 : : {
2151 : 933 : case IDENTIFIER:
2152 : : case UNDERSCORE:
2153 : : // macro fragment
2154 : 933 : return parse_macro_match_fragment ();
2155 : 506 : case LEFT_PAREN:
2156 : : // macro repetition
2157 : 506 : return parse_macro_match_repetition ();
2158 : 1 : default:
2159 : 1 : if (token_id_is_keyword (t2->get_id ()) && t2->get_id () != CRATE)
2160 : : {
2161 : : // keyword as macro fragment
2162 : 1 : 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 : 1440 : }
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 : 494 : default:
2191 : : // just the token
2192 : 494 : lexer.skip_token ();
2193 : 494 : return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
2194 : : }
2195 : 2001 : }
2196 : :
2197 : : // Parses a fragment macro match.
2198 : : template <typename ManagedTokenSource>
2199 : : std::unique_ptr<AST::MacroMatchFragment>
2200 : 934 : Parser<ManagedTokenSource>::parse_macro_match_fragment ()
2201 : : {
2202 : 934 : location_t fragment_locus = lexer.peek_token ()->get_locus ();
2203 : 934 : skip_token (DOLLAR_SIGN);
2204 : :
2205 : 934 : Identifier ident;
2206 : 934 : auto identifier = lexer.peek_token ();
2207 : 934 : if (identifier->get_id () == UNDERSCORE)
2208 : 4 : ident = {Values::Keywords::UNDERSCORE, identifier->get_locus ()};
2209 : : else
2210 : 1864 : ident = {identifier};
2211 : :
2212 : 934 : 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 : 934 : skip_token (identifier->get_id ());
2221 : :
2222 : 934 : if (!skip_token (COLON))
2223 : : {
2224 : : // skip after somewhere?
2225 : 0 : return nullptr;
2226 : : }
2227 : :
2228 : : // get MacroFragSpec for macro
2229 : 934 : const_TokenPtr t = expect_token (IDENTIFIER);
2230 : 934 : if (t == nullptr)
2231 : 0 : return nullptr;
2232 : :
2233 : : AST::MacroFragSpec frag
2234 : 934 : = AST::MacroFragSpec::get_frag_spec_from_str (t->get_str ());
2235 : 934 : 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 : 934 : new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
2247 : 1868 : }
2248 : :
2249 : : // Parses a repetition macro match.
2250 : : template <typename ManagedTokenSource>
2251 : : std::unique_ptr<AST::MacroMatchRepetition>
2252 : 506 : Parser<ManagedTokenSource>::parse_macro_match_repetition ()
2253 : : {
2254 : 506 : skip_token (DOLLAR_SIGN);
2255 : 506 : skip_token (LEFT_PAREN);
2256 : :
2257 : 506 : std::vector<std::unique_ptr<AST::MacroMatch>> matches;
2258 : :
2259 : : // parse required first macro match
2260 : 506 : std::unique_ptr<AST::MacroMatch> initial_match = parse_macro_match ();
2261 : 506 : 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 : 506 : matches.push_back (std::move (initial_match));
2272 : :
2273 : : // parse optional later macro matches
2274 : 506 : const_TokenPtr t = lexer.peek_token ();
2275 : 646 : while (t->get_id () != RIGHT_PAREN)
2276 : : {
2277 : 140 : std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
2278 : :
2279 : 140 : 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 : 140 : matches.push_back (std::move (match));
2289 : :
2290 : 140 : t = lexer.peek_token ();
2291 : : }
2292 : :
2293 : 506 : if (!skip_token (RIGHT_PAREN))
2294 : : {
2295 : : // skip after somewhere?
2296 : 0 : return nullptr;
2297 : : }
2298 : :
2299 : 506 : t = lexer.peek_token ();
2300 : : // see if separator token exists
2301 : 506 : std::unique_ptr<AST::Token> separator = nullptr;
2302 : 506 : 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 : 133 : default:
2318 : : // separator does exist
2319 : 133 : separator = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
2320 : 133 : lexer.skip_token ();
2321 : : break;
2322 : : }
2323 : :
2324 : : // parse repetition operator
2325 : 506 : t = lexer.peek_token ();
2326 : 506 : AST::MacroMatchRepetition::MacroRepOp op = AST::MacroMatchRepetition::NONE;
2327 : 506 : switch (t->get_id ())
2328 : : {
2329 : 437 : case ASTERISK:
2330 : 437 : op = AST::MacroMatchRepetition::ANY;
2331 : 437 : lexer.skip_token ();
2332 : : break;
2333 : 39 : case PLUS:
2334 : 39 : op = AST::MacroMatchRepetition::ONE_OR_MORE;
2335 : 39 : lexer.skip_token ();
2336 : : break;
2337 : 30 : case QUESTION_MARK:
2338 : 30 : op = AST::MacroMatchRepetition::ZERO_OR_ONE;
2339 : 30 : lexer.skip_token ();
2340 : :
2341 : 30 : 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 : 506 : new AST::MacroMatchRepetition (std::move (matches), op,
2364 : 506 : std::move (separator), t->get_locus ()));
2365 : 1012 : }
2366 : :
2367 : : /* Parses a visibility syntactical production (i.e. creating a non-default
2368 : : * visibility) */
2369 : : template <typename ManagedTokenSource>
2370 : : AST::Visibility
2371 : 35830 : Parser<ManagedTokenSource>::parse_visibility ()
2372 : : {
2373 : : // check for no visibility
2374 : 71660 : if (lexer.peek_token ()->get_id () != PUB)
2375 : : {
2376 : 27705 : return AST::Visibility::create_private ();
2377 : : }
2378 : :
2379 : 8125 : auto vis_loc = lexer.peek_token ()->get_locus ();
2380 : 8125 : lexer.skip_token ();
2381 : :
2382 : : // create simple pub visibility if
2383 : : // - found no parentheses
2384 : : // - found unit type `()`
2385 : 16250 : if (lexer.peek_token ()->get_id () != LEFT_PAREN
2386 : 8192 : || lexer.peek_token (1)->get_id () == RIGHT_PAREN)
2387 : : {
2388 : 8059 : return AST::Visibility::create_public (vis_loc);
2389 : : // or whatever
2390 : : }
2391 : :
2392 : 66 : lexer.skip_token ();
2393 : :
2394 : 66 : const_TokenPtr t = lexer.peek_token ();
2395 : 66 : auto path_loc = t->get_locus ();
2396 : :
2397 : 66 : switch (t->get_id ())
2398 : : {
2399 : 53 : case CRATE:
2400 : 53 : lexer.skip_token ();
2401 : :
2402 : 53 : skip_token (RIGHT_PAREN);
2403 : :
2404 : 53 : 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 : 1 : case SUPER:
2412 : 1 : lexer.skip_token ();
2413 : :
2414 : 1 : skip_token (RIGHT_PAREN);
2415 : :
2416 : 1 : return AST::Visibility::create_super (path_loc, vis_loc);
2417 : 12 : case IN:
2418 : : {
2419 : 12 : lexer.skip_token ();
2420 : :
2421 : : // parse the "in" path as well
2422 : 12 : AST::SimplePath path = parse_simple_path ();
2423 : 12 : 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 : 12 : skip_token (RIGHT_PAREN);
2434 : :
2435 : 12 : return AST::Visibility::create_in_path (std::move (path), vis_loc);
2436 : 12 : }
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 : 66 : }
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 : 1279 : Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
2450 : : AST::AttrVec outer_attrs)
2451 : : {
2452 : 1279 : location_t locus = lexer.peek_token ()->get_locus ();
2453 : :
2454 : 1279 : Unsafety safety = Unsafety::Normal;
2455 : 2558 : if (lexer.peek_token ()->get_id () == UNSAFE)
2456 : : {
2457 : 1 : safety = Unsafety::Unsafe;
2458 : 1 : skip_token (UNSAFE);
2459 : : }
2460 : :
2461 : 1279 : skip_token (MOD);
2462 : :
2463 : 1279 : const_TokenPtr module_name = expect_token (IDENTIFIER);
2464 : 1279 : if (module_name == nullptr)
2465 : : {
2466 : 0 : return nullptr;
2467 : : }
2468 : 1279 : Identifier name{module_name};
2469 : :
2470 : 1279 : const_TokenPtr t = lexer.peek_token ();
2471 : :
2472 : 1279 : switch (t->get_id ())
2473 : : {
2474 : 77 : case SEMICOLON:
2475 : 77 : lexer.skip_token ();
2476 : :
2477 : : // Construct an external module
2478 : : return std::unique_ptr<AST::Module> (
2479 : 231 : new AST::Module (std::move (name), std::move (vis),
2480 : : std::move (outer_attrs), locus, safety,
2481 : 231 : lexer.get_filename (), inline_module_stack));
2482 : 1202 : case LEFT_CURLY:
2483 : : {
2484 : 1202 : lexer.skip_token ();
2485 : :
2486 : : // parse inner attributes
2487 : 1202 : AST::AttrVec inner_attrs = parse_inner_attributes ();
2488 : :
2489 : 1202 : std::string default_path = name.as_string ();
2490 : :
2491 : 1202 : if (inline_module_stack.empty ())
2492 : : {
2493 : 682 : std::string filename = lexer.get_filename ();
2494 : 682 : auto slash_idx = filename.rfind (file_separator);
2495 : 682 : if (slash_idx == std::string::npos)
2496 : : slash_idx = 0;
2497 : : else
2498 : 682 : slash_idx++;
2499 : 682 : filename = filename.substr (slash_idx);
2500 : :
2501 : 682 : std::string subdir;
2502 : 682 : if (get_file_subdir (filename, subdir))
2503 : 682 : default_path = subdir + file_separator + name.as_string ();
2504 : 682 : }
2505 : :
2506 : 1202 : std::string module_path_name
2507 : : = extract_module_path (inner_attrs, outer_attrs, default_path);
2508 : 1202 : InlineModuleStackScope scope (*this, std::move (module_path_name));
2509 : :
2510 : : // parse items
2511 : 1202 : std::vector<std::unique_ptr<AST::Item>> items;
2512 : 1202 : const_TokenPtr tok = lexer.peek_token ();
2513 : 3970 : while (tok->get_id () != RIGHT_CURLY)
2514 : : {
2515 : 2768 : std::unique_ptr<AST::Item> item = parse_item (false);
2516 : 2768 : 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 : 2768 : items.push_back (std::move (item));
2526 : :
2527 : 2768 : tok = lexer.peek_token ();
2528 : : }
2529 : :
2530 : 1202 : if (!skip_token (RIGHT_CURLY))
2531 : : {
2532 : : // skip somewhere?
2533 : 0 : return nullptr;
2534 : : }
2535 : :
2536 : : return std::unique_ptr<AST::Module> (
2537 : 1202 : new AST::Module (std::move (name), locus, std::move (items),
2538 : : std::move (vis), safety, std::move (inner_attrs),
2539 : 1202 : std::move (outer_attrs))); // module name?
2540 : 1202 : }
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 : 1279 : }
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 : 647 : Parser<ManagedTokenSource>::parse_use_decl (AST::Visibility vis,
2652 : : AST::AttrVec outer_attrs)
2653 : : {
2654 : 647 : location_t locus = lexer.peek_token ()->get_locus ();
2655 : 647 : if (!skip_token (USE))
2656 : : {
2657 : 0 : skip_after_semicolon ();
2658 : 0 : return nullptr;
2659 : : }
2660 : :
2661 : : // parse use tree, which is required
2662 : 647 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2663 : 647 : 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 : 646 : if (!skip_token (SEMICOLON))
2674 : : {
2675 : 0 : skip_after_semicolon ();
2676 : 0 : return nullptr;
2677 : : }
2678 : :
2679 : : return std::unique_ptr<AST::UseDeclaration> (
2680 : 646 : new AST::UseDeclaration (std::move (use_tree), std::move (vis),
2681 : 646 : std::move (outer_attrs), locus));
2682 : 647 : }
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 : 1223 : 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 : 1223 : location_t locus = lexer.peek_token ()->get_locus ();
2715 : :
2716 : : // bool has_path = false;
2717 : 1223 : AST::SimplePath path = parse_simple_path ();
2718 : :
2719 : 1223 : if (path.is_empty ())
2720 : : {
2721 : : // has no path, so must be glob or nested tree UseTree type
2722 : :
2723 : 0 : bool is_global = false;
2724 : :
2725 : : // check for global scope resolution operator
2726 : 0 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
2727 : : {
2728 : 0 : lexer.skip_token ();
2729 : 0 : is_global = true;
2730 : : }
2731 : :
2732 : 0 : const_TokenPtr t = lexer.peek_token ();
2733 : 0 : 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 : 0 : case LEFT_CURLY:
2748 : : {
2749 : : // nested tree UseTree type
2750 : 0 : lexer.skip_token ();
2751 : :
2752 : 0 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2753 : :
2754 : 0 : const_TokenPtr t = lexer.peek_token ();
2755 : 0 : while (t->get_id () != RIGHT_CURLY)
2756 : : {
2757 : 0 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2758 : 0 : if (use_tree == nullptr)
2759 : : {
2760 : : break;
2761 : : }
2762 : :
2763 : 0 : use_trees.push_back (std::move (use_tree));
2764 : :
2765 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
2766 : : break;
2767 : :
2768 : 0 : lexer.skip_token ();
2769 : 0 : t = lexer.peek_token ();
2770 : : }
2771 : :
2772 : : // skip end curly delimiter
2773 : 0 : if (!skip_token (RIGHT_CURLY))
2774 : : {
2775 : : // skip after somewhere?
2776 : 0 : return nullptr;
2777 : : }
2778 : :
2779 : 0 : 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 : 0 : return std::unique_ptr<AST::UseTreeList> (
2786 : 0 : new AST::UseTreeList (AST::UseTreeList::NO_PATH,
2787 : 0 : AST::SimplePath::create_empty (),
2788 : 0 : std::move (use_trees), locus));
2789 : 0 : }
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 : 0 : }
2810 : : else
2811 : : {
2812 : 1223 : const_TokenPtr t = lexer.peek_token ();
2813 : :
2814 : 1223 : switch (t->get_id ())
2815 : : {
2816 : 3 : case AS:
2817 : : {
2818 : : // rebind UseTree type
2819 : 3 : lexer.skip_token ();
2820 : :
2821 : 3 : const_TokenPtr t = lexer.peek_token ();
2822 : 3 : switch (t->get_id ())
2823 : : {
2824 : 3 : case IDENTIFIER:
2825 : : // skip lexer token
2826 : 3 : lexer.skip_token ();
2827 : :
2828 : 3 : return std::unique_ptr<AST::UseTreeRebind> (
2829 : 9 : new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER,
2830 : 9 : std::move (path), locus, t));
2831 : 0 : case UNDERSCORE:
2832 : : // skip lexer token
2833 : 0 : lexer.skip_token ();
2834 : :
2835 : 0 : return std::unique_ptr<AST::UseTreeRebind> (
2836 : 0 : new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
2837 : : std::move (path), locus,
2838 : : {Values::Keywords::UNDERSCORE,
2839 : 0 : 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 : 3 : }
2851 : 1058 : 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 : 1058 : return std::unique_ptr<AST::UseTreeRebind> (
2860 : 2116 : new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
2861 : 1058 : 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 : 161 : skip_token ();
2873 : 161 : t = lexer.peek_token ();
2874 : :
2875 : 161 : switch (t->get_id ())
2876 : : {
2877 : 10 : case ASTERISK:
2878 : : // glob UseTree type
2879 : 10 : lexer.skip_token ();
2880 : :
2881 : 10 : return std::unique_ptr<AST::UseTreeGlob> (
2882 : 20 : new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
2883 : 10 : std::move (path), locus));
2884 : 151 : case LEFT_CURLY:
2885 : : {
2886 : : // nested tree UseTree type
2887 : 151 : lexer.skip_token ();
2888 : :
2889 : 151 : std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2890 : :
2891 : : // TODO: think of better control structure
2892 : 151 : const_TokenPtr t = lexer.peek_token ();
2893 : 727 : while (t->get_id () != RIGHT_CURLY)
2894 : : {
2895 : 576 : std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2896 : 576 : if (use_tree == nullptr)
2897 : : {
2898 : : break;
2899 : : }
2900 : :
2901 : 576 : use_trees.push_back (std::move (use_tree));
2902 : :
2903 : 1152 : if (lexer.peek_token ()->get_id () != COMMA)
2904 : : break;
2905 : :
2906 : 425 : lexer.skip_token ();
2907 : 425 : t = lexer.peek_token ();
2908 : : }
2909 : :
2910 : : // skip end curly delimiter
2911 : 151 : if (!skip_token (RIGHT_CURLY))
2912 : : {
2913 : : // skip after somewhere?
2914 : 0 : return nullptr;
2915 : : }
2916 : :
2917 : 151 : return std::unique_ptr<AST::UseTreeList> (
2918 : 302 : new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
2919 : : std::move (path), std::move (use_trees),
2920 : 151 : locus));
2921 : 151 : }
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 : 1223 : }
2931 : 1223 : }
2932 : :
2933 : : // Parses a function (not a method).
2934 : : template <typename ManagedTokenSource>
2935 : : std::unique_ptr<AST::Function>
2936 : 11138 : Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
2937 : : AST::AttrVec outer_attrs,
2938 : : bool is_external)
2939 : : {
2940 : 11138 : location_t locus = lexer.peek_token ()->get_locus ();
2941 : : // Get qualifiers for function if they exist
2942 : 11138 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
2943 : :
2944 : 11138 : skip_token (FN_KW);
2945 : :
2946 : : // Save function name token
2947 : 11138 : const_TokenPtr function_name_tok = expect_token (IDENTIFIER);
2948 : 11138 : if (function_name_tok == nullptr)
2949 : : {
2950 : 0 : skip_after_next_block ();
2951 : 0 : return nullptr;
2952 : : }
2953 : 11138 : Identifier function_name{function_name_tok};
2954 : :
2955 : : // parse generic params - if exist
2956 : 11138 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
2957 : : = parse_generic_params_in_angles ();
2958 : :
2959 : 11138 : 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 : 11138 : auto initial_param = parse_self_param ();
2971 : :
2972 : 11138 : if (!initial_param.has_value ()
2973 : 11138 : && initial_param.error () != ParseSelfError::NOT_SELF)
2974 : 0 : return nullptr;
2975 : :
2976 : 13364 : if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
2977 : 1358 : skip_token ();
2978 : :
2979 : : // parse function parameters (only if next token isn't right paren)
2980 : 11138 : std::vector<std::unique_ptr<AST::Param>> function_params;
2981 : :
2982 : 22276 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
2983 : : function_params
2984 : 5054 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
2985 : :
2986 : 11138 : if (initial_param.has_value ())
2987 : 2226 : function_params.insert (function_params.begin (),
2988 : 2226 : std::move (*initial_param));
2989 : :
2990 : 11138 : 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 : 11138 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
3003 : :
3004 : : // parse where clause - if exists
3005 : 11138 : AST::WhereClause where_clause = parse_where_clause ();
3006 : :
3007 : 11138 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
3008 : 22276 : if (lexer.peek_token ()->get_id () == SEMICOLON)
3009 : 3867 : lexer.skip_token ();
3010 : : else
3011 : : {
3012 : 7271 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
3013 : 7271 : if (block_expr != nullptr)
3014 : 7244 : body = std::move (block_expr);
3015 : 7271 : }
3016 : :
3017 : : return std::unique_ptr<AST::Function> (
3018 : 29520 : 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 : 11138 : std::move (outer_attrs), locus, false, is_external));
3023 : 33414 : }
3024 : :
3025 : : // Parses function or method qualifiers (i.e. const, unsafe, and extern).
3026 : : template <typename ManagedTokenSource>
3027 : : AST::FunctionQualifiers
3028 : 17860 : Parser<ManagedTokenSource>::parse_function_qualifiers ()
3029 : : {
3030 : 17860 : Async async_status = Async::No;
3031 : 17860 : Const const_status = Const::No;
3032 : 17860 : Unsafety unsafe_status = Unsafety::Normal;
3033 : 17860 : bool has_extern = false;
3034 : 17860 : std::string abi;
3035 : :
3036 : 17860 : const_TokenPtr t;
3037 : : location_t locus;
3038 : : // Check in order of const, unsafe, then extern
3039 : 53580 : for (int i = 0; i < 2; i++)
3040 : : {
3041 : 35720 : t = lexer.peek_token ();
3042 : 35720 : locus = t->get_locus ();
3043 : :
3044 : 35720 : switch (t->get_id ())
3045 : : {
3046 : 853 : case CONST:
3047 : 853 : lexer.skip_token ();
3048 : 853 : const_status = Const::Yes;
3049 : 853 : 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 : 35720 : if (lexer.peek_token ()->get_id () == UNSAFE)
3061 : : {
3062 : 487 : lexer.skip_token ();
3063 : 487 : unsafe_status = Unsafety::Unsafe;
3064 : : }
3065 : :
3066 : 35720 : if (lexer.peek_token ()->get_id () == EXTERN_KW)
3067 : : {
3068 : 76 : lexer.skip_token ();
3069 : 76 : has_extern = true;
3070 : :
3071 : : // detect optional abi name
3072 : 76 : const_TokenPtr next_tok = lexer.peek_token ();
3073 : 76 : if (next_tok->get_id () == STRING_LITERAL)
3074 : : {
3075 : 76 : lexer.skip_token ();
3076 : 76 : abi = next_tok->get_str ();
3077 : : }
3078 : 76 : }
3079 : :
3080 : 35720 : return AST::FunctionQualifiers (locus, async_status, const_status,
3081 : 17860 : unsafe_status, has_extern, std::move (abi));
3082 : 17860 : }
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 : 31237 : Parser<ManagedTokenSource>::parse_generic_params_in_angles ()
3088 : : {
3089 : 62474 : if (lexer.peek_token ()->get_id () != LEFT_ANGLE)
3090 : : {
3091 : : // seems to be no generic params, so exit with empty vector
3092 : 27122 : return std::vector<std::unique_ptr<AST::GenericParam>> ();
3093 : : }
3094 : 4115 : lexer.skip_token ();
3095 : :
3096 : : // DEBUG:
3097 : 4115 : rust_debug ("skipped left angle in generic param");
3098 : :
3099 : 4115 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
3100 : : = parse_generic_params (is_right_angle_tok);
3101 : :
3102 : : // DEBUG:
3103 : 4115 : rust_debug ("finished parsing actual generic params (i.e. inside angles)");
3104 : :
3105 : 4115 : 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 : 4114 : return generic_params;
3115 : 4115 : }
3116 : :
3117 : : template <typename ManagedTokenSource>
3118 : : template <typename EndTokenPred>
3119 : : std::unique_ptr<AST::GenericParam>
3120 : 4491 : Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
3121 : : {
3122 : 4491 : auto outer_attrs = parse_outer_attributes ();
3123 : 4491 : std::unique_ptr<AST::GenericParam> param;
3124 : 4491 : auto token = lexer.peek_token ();
3125 : :
3126 : 4491 : switch (token->get_id ())
3127 : : {
3128 : 243 : case LIFETIME:
3129 : : {
3130 : 243 : auto lifetime = parse_lifetime (false);
3131 : 243 : 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 : 243 : std::vector<AST::Lifetime> lifetime_bounds;
3140 : 486 : if (lexer.peek_token ()->get_id () == COLON)
3141 : : {
3142 : 1 : lexer.skip_token ();
3143 : : // parse required bounds
3144 : : lifetime_bounds
3145 : 1 : = parse_lifetime_bounds ([is_end_token] (TokenId id) {
3146 : 1 : return is_end_token (id) || id == COMMA;
3147 : : });
3148 : : }
3149 : :
3150 : 486 : param = std::unique_ptr<AST::LifetimeParam> (new AST::LifetimeParam (
3151 : 243 : std::move (lifetime.value ()), std::move (lifetime_bounds),
3152 : 243 : std::move (outer_attrs), token->get_locus ()));
3153 : : break;
3154 : 486 : }
3155 : 4198 : case IDENTIFIER:
3156 : : {
3157 : 4198 : auto type_ident = token->get_str ();
3158 : 4198 : lexer.skip_token ();
3159 : :
3160 : 4198 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3161 : 8396 : if (lexer.peek_token ()->get_id () == COLON)
3162 : : {
3163 : 639 : lexer.skip_token ();
3164 : :
3165 : : // parse optional type param bounds
3166 : 639 : type_param_bounds = parse_type_param_bounds ();
3167 : : }
3168 : :
3169 : 4198 : std::unique_ptr<AST::Type> type = nullptr;
3170 : 8396 : if (lexer.peek_token ()->get_id () == EQUAL)
3171 : : {
3172 : 359 : lexer.skip_token ();
3173 : :
3174 : : // parse required type
3175 : 359 : type = parse_type ();
3176 : 359 : 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 : 4198 : param = std::unique_ptr<AST::TypeParam> (
3186 : 12594 : new AST::TypeParam (std::move (type_ident), token->get_locus (),
3187 : : std::move (type_param_bounds), std::move (type),
3188 : 4198 : std::move (outer_attrs)));
3189 : : break;
3190 : 4198 : }
3191 : 49 : case CONST:
3192 : : {
3193 : 49 : lexer.skip_token ();
3194 : :
3195 : 49 : auto name_token = expect_token (IDENTIFIER);
3196 : :
3197 : 98 : if (!name_token || !expect_token (COLON))
3198 : 1 : return nullptr;
3199 : :
3200 : 48 : auto type = parse_type ();
3201 : 48 : if (!type)
3202 : 1 : return nullptr;
3203 : :
3204 : : // optional default value
3205 : 47 : tl::optional<AST::GenericArg> default_expr = tl::nullopt;
3206 : 94 : if (lexer.peek_token ()->get_id () == EQUAL)
3207 : : {
3208 : 19 : lexer.skip_token ();
3209 : 19 : auto tok = lexer.peek_token ();
3210 : 37 : default_expr = parse_generic_arg ();
3211 : :
3212 : 19 : 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 : 18 : if (default_expr.value ().get_kind ()
3225 : : == AST::GenericArg::Kind::Either)
3226 : 1 : default_expr = default_expr.value ().disambiguate_to_const ();
3227 : 19 : }
3228 : :
3229 : 46 : param = std::unique_ptr<AST::ConstGenericParam> (
3230 : 202 : new AST::ConstGenericParam (name_token->get_str (), std::move (type),
3231 : : default_expr, std::move (outer_attrs),
3232 : 46 : token->get_locus ()));
3233 : :
3234 : : break;
3235 : 97 : }
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 : 4487 : return param;
3245 : 4491 : }
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 : 4115 : Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
3253 : : {
3254 : 4115 : 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 : 4115 : AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
3262 : :
3263 : : // Did we parse a generic type param yet
3264 : 4115 : auto type_seen = false;
3265 : : // Did the user write a lifetime parameter after a type one
3266 : 4115 : auto order_error = false;
3267 : :
3268 : : // parse lifetime params
3269 : 21695 : while (!is_end_token (lexer.peek_token ()->get_id ()))
3270 : : {
3271 : 4491 : auto param = parse_generic_param (is_end_token);
3272 : 4491 : if (param)
3273 : : {
3274 : : // TODO: Handle `Const` here as well if necessary
3275 : 4487 : if (param->get_kind () == AST::GenericParam::Kind::Type)
3276 : : type_seen = true;
3277 : 289 : else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
3278 : 289 : && type_seen)
3279 : : order_error = true;
3280 : :
3281 : 4487 : generic_params.emplace_back (std::move (param));
3282 : 4487 : maybe_skip_token (COMMA);
3283 : : }
3284 : : else
3285 : : break;
3286 : : }
3287 : :
3288 : : // FIXME: Add reordering hint
3289 : 4115 : if (order_error)
3290 : : {
3291 : 1 : Error error (generic_params.front ()->get_locus (),
3292 : : "invalid order for generic parameters: lifetime parameters "
3293 : : "must be declared prior to type and const parameters");
3294 : 1 : add_error (std::move (error));
3295 : 1 : }
3296 : :
3297 : 4115 : generic_params.shrink_to_fit ();
3298 : 4115 : return generic_params;
3299 : 4115 : }
3300 : :
3301 : : /* Parses lifetime generic parameters (pointers). Will also consume any
3302 : : * trailing comma. No extra checks for end token. */
3303 : : template <typename ManagedTokenSource>
3304 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
3305 : 4 : Parser<ManagedTokenSource>::parse_lifetime_params ()
3306 : : {
3307 : 4 : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
3308 : :
3309 : 12 : while (lexer.peek_token ()->get_id () != END_OF_FILE)
3310 : : {
3311 : 4 : auto lifetime_param = parse_lifetime_param ();
3312 : :
3313 : 4 : if (!lifetime_param)
3314 : : {
3315 : : // can't treat as error as only way to get out with trailing comma
3316 : : break;
3317 : : }
3318 : :
3319 : 2 : lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> (
3320 : 2 : new AST::LifetimeParam (std::move (lifetime_param.value ()))));
3321 : :
3322 : 4 : if (lexer.peek_token ()->get_id () != COMMA)
3323 : : break;
3324 : :
3325 : : // skip commas, including trailing commas
3326 : 0 : lexer.skip_token ();
3327 : : }
3328 : :
3329 : 4 : lifetime_params.shrink_to_fit ();
3330 : :
3331 : 4 : return lifetime_params;
3332 : : }
3333 : :
3334 : : /* Parses lifetime generic parameters (pointers). Will also consume any
3335 : : * trailing comma. Has extra is_end_token predicate checking. */
3336 : : template <typename ManagedTokenSource>
3337 : : template <typename EndTokenPred>
3338 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
3339 : : Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token)
3340 : : {
3341 : : std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
3342 : :
3343 : : // if end_token is not specified, it defaults to EOF, so should work fine
3344 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3345 : : {
3346 : : auto lifetime_param = parse_lifetime_param ();
3347 : :
3348 : : if (!lifetime_param)
3349 : : {
3350 : : /* TODO: is it worth throwing away all lifetime params just because
3351 : : * one failed? */
3352 : : Error error (lexer.peek_token ()->get_locus (),
3353 : : "failed to parse lifetime param in lifetime params");
3354 : : add_error (std::move (error));
3355 : :
3356 : : return {};
3357 : : }
3358 : :
3359 : : lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> (
3360 : : new AST::LifetimeParam (std::move (lifetime_param))));
3361 : :
3362 : : if (lexer.peek_token ()->get_id () != COMMA)
3363 : : break;
3364 : :
3365 : : // skip commas, including trailing commas
3366 : : lexer.skip_token ();
3367 : : }
3368 : :
3369 : : lifetime_params.shrink_to_fit ();
3370 : :
3371 : : return lifetime_params;
3372 : : }
3373 : :
3374 : : /* Parses lifetime generic parameters (objects). Will also consume any
3375 : : * trailing comma. No extra checks for end token.
3376 : : * TODO: is this best solution? implements most of the same algorithm.
3377 : : * TODO: seems to be unused, remove? */
3378 : : template <typename ManagedTokenSource>
3379 : : std::vector<AST::LifetimeParam>
3380 : 0 : Parser<ManagedTokenSource>::parse_lifetime_params_objs ()
3381 : : {
3382 : 0 : std::vector<AST::LifetimeParam> lifetime_params;
3383 : :
3384 : : // bad control structure as end token cannot be guaranteed
3385 : 0 : while (true)
3386 : : {
3387 : 0 : auto lifetime_param = parse_lifetime_param ();
3388 : :
3389 : 0 : if (!lifetime_param)
3390 : : {
3391 : : // not an error as only way to exit if trailing comma
3392 : : break;
3393 : : }
3394 : :
3395 : 0 : lifetime_params.push_back (std::move (lifetime_param.value ()));
3396 : :
3397 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
3398 : : break;
3399 : :
3400 : : // skip commas, including trailing commas
3401 : 0 : lexer.skip_token ();
3402 : : }
3403 : :
3404 : 0 : lifetime_params.shrink_to_fit ();
3405 : :
3406 : 0 : return lifetime_params;
3407 : : }
3408 : :
3409 : : /* Parses lifetime generic parameters (objects). Will also consume any
3410 : : * trailing comma. Has extra is_end_token predicate checking.
3411 : : * TODO: is this best solution? implements most of the same algorithm. */
3412 : : template <typename ManagedTokenSource>
3413 : : template <typename EndTokenPred>
3414 : : std::vector<AST::LifetimeParam>
3415 : 26 : Parser<ManagedTokenSource>::parse_lifetime_params_objs (
3416 : : EndTokenPred is_end_token)
3417 : : {
3418 : 26 : std::vector<AST::LifetimeParam> lifetime_params;
3419 : :
3420 : 78 : while (!is_end_token (lexer.peek_token ()->get_id ()))
3421 : : {
3422 : 26 : auto lifetime_param = parse_lifetime_param ();
3423 : :
3424 : 26 : if (!lifetime_param)
3425 : : {
3426 : : /* TODO: is it worth throwing away all lifetime params just because
3427 : : * one failed? */
3428 : 0 : Error error (lexer.peek_token ()->get_locus (),
3429 : : "failed to parse lifetime param in lifetime params");
3430 : 0 : add_error (std::move (error));
3431 : :
3432 : 0 : return {};
3433 : 0 : }
3434 : :
3435 : 26 : lifetime_params.push_back (std::move (lifetime_param.value ()));
3436 : :
3437 : 52 : if (lexer.peek_token ()->get_id () != COMMA)
3438 : : break;
3439 : :
3440 : : // skip commas, including trailing commas
3441 : 0 : lexer.skip_token ();
3442 : : }
3443 : :
3444 : 26 : lifetime_params.shrink_to_fit ();
3445 : :
3446 : 26 : return lifetime_params;
3447 : 26 : }
3448 : :
3449 : : /* Parses a sequence of a certain grammar rule in object form (not pointer or
3450 : : * smart pointer), delimited by commas and ending when 'is_end_token' is
3451 : : * satisfied (templated). Will also consume any trailing comma.
3452 : : * FIXME: this cannot be used due to member function pointer problems (i.e.
3453 : : * parsing_function cannot be specified properly) */
3454 : : template <typename ManagedTokenSource>
3455 : : template <typename ParseFunction, typename EndTokenPred>
3456 : : auto
3457 : : Parser<ManagedTokenSource>::parse_non_ptr_sequence (
3458 : : ParseFunction parsing_function, EndTokenPred is_end_token,
3459 : : std::string error_msg) -> std::vector<decltype (parsing_function ())>
3460 : : {
3461 : : std::vector<decltype (parsing_function ())> params;
3462 : :
3463 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3464 : : {
3465 : : auto param = parsing_function ();
3466 : :
3467 : : if (param.is_error ())
3468 : : {
3469 : : // TODO: is it worth throwing away all params just because one
3470 : : // failed?
3471 : : Error error (lexer.peek_token ()->get_locus (),
3472 : : std::move (error_msg));
3473 : : add_error (std::move (error));
3474 : :
3475 : : return {};
3476 : : }
3477 : :
3478 : : params.push_back (std::move (param));
3479 : :
3480 : : if (lexer.peek_token ()->get_id () != COMMA)
3481 : : break;
3482 : :
3483 : : // skip commas, including trailing commas
3484 : : lexer.skip_token ();
3485 : : }
3486 : :
3487 : : params.shrink_to_fit ();
3488 : :
3489 : : return params;
3490 : : }
3491 : :
3492 : : /* Parses a single lifetime generic parameter (not including comma). */
3493 : : template <typename ManagedTokenSource>
3494 : : tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
3495 : 30 : Parser<ManagedTokenSource>::parse_lifetime_param ()
3496 : : {
3497 : : // parse outer attributes, which are optional and may not exist
3498 : 30 : auto outer_attrs = parse_outer_attributes ();
3499 : :
3500 : : // save lifetime token - required
3501 : 30 : const_TokenPtr lifetime_tok = lexer.peek_token ();
3502 : 30 : if (lifetime_tok->get_id () != LIFETIME)
3503 : : {
3504 : : // if lifetime is missing, must not be a lifetime param, so return error
3505 : 2 : return tl::make_unexpected<ParseLifetimeParamError> ({});
3506 : : }
3507 : 28 : lexer.skip_token ();
3508 : 56 : AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
3509 : : lifetime_tok->get_locus ());
3510 : :
3511 : : // parse lifetime bounds, if it exists
3512 : 28 : std::vector<AST::Lifetime> lifetime_bounds;
3513 : 56 : if (lexer.peek_token ()->get_id () == COLON)
3514 : : {
3515 : : // parse lifetime bounds
3516 : 0 : lifetime_bounds = parse_lifetime_bounds ();
3517 : : // TODO: have end token passed in?
3518 : : }
3519 : :
3520 : 56 : return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
3521 : : std::move (outer_attrs),
3522 : 28 : lifetime_tok->get_locus ());
3523 : 58 : }
3524 : :
3525 : : // Parses type generic parameters. Will also consume any trailing comma.
3526 : : template <typename ManagedTokenSource>
3527 : : std::vector<std::unique_ptr<AST::TypeParam>>
3528 : 0 : Parser<ManagedTokenSource>::parse_type_params ()
3529 : : {
3530 : 0 : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
3531 : :
3532 : : // infinite loop with break on failure as no info on ending token
3533 : 0 : while (true)
3534 : : {
3535 : 0 : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
3536 : :
3537 : 0 : if (type_param == nullptr)
3538 : : {
3539 : : // break if fails to parse
3540 : : break;
3541 : : }
3542 : :
3543 : 0 : type_params.push_back (std::move (type_param));
3544 : :
3545 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
3546 : : break;
3547 : :
3548 : : // skip commas, including trailing commas
3549 : 0 : lexer.skip_token ();
3550 : : }
3551 : :
3552 : 0 : type_params.shrink_to_fit ();
3553 : 0 : return type_params;
3554 : : }
3555 : :
3556 : : // Parses type generic parameters. Will also consume any trailing comma.
3557 : : template <typename ManagedTokenSource>
3558 : : template <typename EndTokenPred>
3559 : : std::vector<std::unique_ptr<AST::TypeParam>>
3560 : : Parser<ManagedTokenSource>::parse_type_params (EndTokenPred is_end_token)
3561 : : {
3562 : : std::vector<std::unique_ptr<AST::TypeParam>> type_params;
3563 : :
3564 : : while (!is_end_token (lexer.peek_token ()->get_id ()))
3565 : : {
3566 : : std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
3567 : :
3568 : : if (type_param == nullptr)
3569 : : {
3570 : : Error error (lexer.peek_token ()->get_locus (),
3571 : : "failed to parse type param in type params");
3572 : : add_error (std::move (error));
3573 : :
3574 : : return {};
3575 : : }
3576 : :
3577 : : type_params.push_back (std::move (type_param));
3578 : :
3579 : : if (lexer.peek_token ()->get_id () != COMMA)
3580 : : break;
3581 : :
3582 : : // skip commas, including trailing commas
3583 : : lexer.skip_token ();
3584 : : }
3585 : :
3586 : : type_params.shrink_to_fit ();
3587 : : return type_params;
3588 : : /* TODO: this shares most code with parse_lifetime_params - good place to
3589 : : * use template (i.e. parse_non_ptr_sequence if doable) */
3590 : : }
3591 : :
3592 : : /* Parses a single type (generic) parameter, not including commas. May change
3593 : : * to return value. */
3594 : : template <typename ManagedTokenSource>
3595 : : std::unique_ptr<AST::TypeParam>
3596 : 0 : Parser<ManagedTokenSource>::parse_type_param ()
3597 : : {
3598 : : // parse outer attributes, which are optional and may not exist
3599 : 0 : auto outer_attrs = parse_outer_attributes ();
3600 : :
3601 : 0 : const_TokenPtr identifier_tok = lexer.peek_token ();
3602 : 0 : if (identifier_tok->get_id () != IDENTIFIER)
3603 : : {
3604 : : // return null as type param can't exist without this required
3605 : : // identifier
3606 : 0 : return nullptr;
3607 : : }
3608 : 0 : Identifier ident{identifier_tok};
3609 : 0 : lexer.skip_token ();
3610 : :
3611 : : // parse type param bounds (if they exist)
3612 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3613 : 0 : if (lexer.peek_token ()->get_id () == COLON)
3614 : : {
3615 : 0 : lexer.skip_token ();
3616 : :
3617 : : // parse type param bounds, which may or may not exist
3618 : 0 : type_param_bounds = parse_type_param_bounds ();
3619 : : }
3620 : :
3621 : : // parse type (if it exists)
3622 : 0 : std::unique_ptr<AST::Type> type = nullptr;
3623 : 0 : if (lexer.peek_token ()->get_id () == EQUAL)
3624 : : {
3625 : 0 : lexer.skip_token ();
3626 : :
3627 : : // parse type (now required)
3628 : 0 : type = parse_type ();
3629 : 0 : if (type == nullptr)
3630 : : {
3631 : 0 : Error error (lexer.peek_token ()->get_locus (),
3632 : : "failed to parse type in type param");
3633 : 0 : add_error (std::move (error));
3634 : :
3635 : 0 : return nullptr;
3636 : 0 : }
3637 : : }
3638 : :
3639 : : return std::unique_ptr<AST::TypeParam> (
3640 : 0 : new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
3641 : : std::move (type_param_bounds), std::move (type),
3642 : 0 : std::move (outer_attrs)));
3643 : 0 : }
3644 : :
3645 : : /* Parses regular (i.e. non-generic) parameters in functions or methods. Also
3646 : : * has end token handling. */
3647 : : template <typename ManagedTokenSource>
3648 : : template <typename EndTokenPred>
3649 : : std::vector<std::unique_ptr<AST::Param>>
3650 : 9937 : Parser<ManagedTokenSource>::parse_function_params (EndTokenPred is_end_token)
3651 : : {
3652 : 9937 : std::vector<std::unique_ptr<AST::Param>> params;
3653 : :
3654 : 19874 : if (is_end_token (lexer.peek_token ()->get_id ()))
3655 : 826 : return params;
3656 : :
3657 : 9111 : auto initial_param = parse_function_param ();
3658 : :
3659 : : // Return empty parameter list if no parameter there
3660 : 9111 : if (initial_param == nullptr)
3661 : : {
3662 : : // TODO: is this an error?
3663 : 0 : return params;
3664 : : }
3665 : :
3666 : 9111 : params.push_back (std::move (initial_param));
3667 : :
3668 : : // maybe think of a better control structure here - do-while with an initial
3669 : : // error state? basically, loop through parameter list until can't find any
3670 : : // more params
3671 : 9111 : const_TokenPtr t = lexer.peek_token ();
3672 : 11335 : while (t->get_id () == COMMA)
3673 : : {
3674 : : // skip comma if applies
3675 : 2224 : lexer.skip_token ();
3676 : :
3677 : : // TODO: strictly speaking, shouldn't there be no trailing comma?
3678 : 4448 : if (is_end_token (lexer.peek_token ()->get_id ()))
3679 : : break;
3680 : :
3681 : : // now, as right paren would break, function param is required
3682 : 2224 : auto param = parse_function_param ();
3683 : 2224 : if (param == nullptr)
3684 : : {
3685 : 0 : Error error (lexer.peek_token ()->get_locus (),
3686 : : "failed to parse function param (in function params)");
3687 : 0 : add_error (std::move (error));
3688 : :
3689 : : // skip somewhere?
3690 : 0 : return std::vector<std::unique_ptr<AST::Param>> ();
3691 : 0 : }
3692 : :
3693 : 2224 : params.push_back (std::move (param));
3694 : :
3695 : 2224 : t = lexer.peek_token ();
3696 : : }
3697 : :
3698 : 9111 : params.shrink_to_fit ();
3699 : 9111 : return params;
3700 : 9937 : }
3701 : :
3702 : : /* Parses a single regular (i.e. non-generic) parameter in a function or
3703 : : * method, i.e. the "name: type" bit. Also handles it not existing. */
3704 : : template <typename ManagedTokenSource>
3705 : : std::unique_ptr<AST::Param>
3706 : 11335 : Parser<ManagedTokenSource>::parse_function_param ()
3707 : : {
3708 : : // parse outer attributes if they exist
3709 : 11335 : AST::AttrVec outer_attrs = parse_outer_attributes ();
3710 : :
3711 : : // TODO: should saved location be at start of outer attributes or pattern?
3712 : 11335 : location_t locus = lexer.peek_token ()->get_locus ();
3713 : :
3714 : 22670 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic
3715 : : {
3716 : 819 : lexer.skip_token (); // Skip ellipsis
3717 : 819 : return std::make_unique<AST::VariadicParam> (
3718 : 1638 : AST::VariadicParam (std::move (outer_attrs), locus));
3719 : : }
3720 : :
3721 : 10516 : std::unique_ptr<AST::Pattern> param_pattern = parse_pattern ();
3722 : :
3723 : : // create error function param if it doesn't exist
3724 : 10516 : if (param_pattern == nullptr)
3725 : : {
3726 : : // skip after something
3727 : 0 : return nullptr;
3728 : : }
3729 : :
3730 : 10516 : if (!skip_token (COLON))
3731 : : {
3732 : : // skip after something
3733 : 0 : return nullptr;
3734 : : }
3735 : :
3736 : 21032 : if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic
3737 : : {
3738 : 11 : lexer.skip_token (); // Skip ellipsis
3739 : 11 : return std::make_unique<AST::VariadicParam> (
3740 : 22 : AST::VariadicParam (std::move (param_pattern), std::move (outer_attrs),
3741 : 11 : locus));
3742 : : }
3743 : : else
3744 : : {
3745 : 10505 : std::unique_ptr<AST::Type> param_type = parse_type ();
3746 : 10505 : if (param_type == nullptr)
3747 : : {
3748 : 0 : return nullptr;
3749 : : }
3750 : 10505 : return std::make_unique<AST::FunctionParam> (
3751 : 21010 : AST::FunctionParam (std::move (param_pattern), std::move (param_type),
3752 : 10505 : std::move (outer_attrs), locus));
3753 : 10505 : }
3754 : 11335 : }
3755 : :
3756 : : /* Parses a function or method return type syntactical construction. Also
3757 : : * handles a function return type not existing. */
3758 : : template <typename ManagedTokenSource>
3759 : : std::unique_ptr<AST::Type>
3760 : 17833 : Parser<ManagedTokenSource>::parse_function_return_type ()
3761 : : {
3762 : 35666 : if (lexer.peek_token ()->get_id () != RETURN_TYPE)
3763 : 5412 : return nullptr;
3764 : :
3765 : : // skip return type, as it now obviously exists
3766 : 12421 : lexer.skip_token ();
3767 : :
3768 : 12421 : std::unique_ptr<AST::Type> type = parse_type ();
3769 : :
3770 : 12421 : return type;
3771 : 12421 : }
3772 : :
3773 : : /* Parses a "where clause" (in a function, struct, method, etc.). Also handles
3774 : : * a where clause not existing, in which it will return
3775 : : * WhereClause::create_empty(), which can be checked via
3776 : : * WhereClause::is_empty(). */
3777 : : template <typename ManagedTokenSource>
3778 : : AST::WhereClause
3779 : 31233 : Parser<ManagedTokenSource>::parse_where_clause ()
3780 : : {
3781 : 31233 : const_TokenPtr where_tok = lexer.peek_token ();
3782 : 31233 : if (where_tok->get_id () != WHERE)
3783 : : {
3784 : : // where clause doesn't exist, so create empty one
3785 : 30944 : return AST::WhereClause::create_empty ();
3786 : : }
3787 : :
3788 : 289 : lexer.skip_token ();
3789 : :
3790 : : /* parse where clause items - this is not a separate rule in the reference
3791 : : * so won't be here */
3792 : 289 : std::vector<std::unique_ptr<AST::WhereClauseItem>> where_clause_items;
3793 : :
3794 : 289 : std::vector<AST::LifetimeParam> for_lifetimes;
3795 : 578 : if (lexer.peek_token ()->get_id () == FOR)
3796 : 1 : for_lifetimes = parse_for_lifetimes ();
3797 : :
3798 : : /* HACK: where clauses end with a right curly or semicolon or equals in all
3799 : : * uses currently */
3800 : 289 : const_TokenPtr t = lexer.peek_token ();
3801 : 588 : while (t->get_id () != LEFT_CURLY && t->get_id () != SEMICOLON
3802 : 580 : && t->get_id () != EQUAL)
3803 : : {
3804 : 299 : std::unique_ptr<AST::WhereClauseItem> where_clause_item
3805 : : = parse_where_clause_item (for_lifetimes);
3806 : :
3807 : 299 : if (where_clause_item == nullptr)
3808 : : {
3809 : 0 : Error error (t->get_locus (), "failed to parse where clause item");
3810 : 0 : add_error (std::move (error));
3811 : :
3812 : 0 : return AST::WhereClause::create_empty ();
3813 : 0 : }
3814 : :
3815 : 299 : where_clause_items.push_back (std::move (where_clause_item));
3816 : :
3817 : : // also skip comma if it exists
3818 : 598 : if (lexer.peek_token ()->get_id () != COMMA)
3819 : : break;
3820 : :
3821 : 291 : lexer.skip_token ();
3822 : 291 : t = lexer.peek_token ();
3823 : : }
3824 : :
3825 : 289 : where_clause_items.shrink_to_fit ();
3826 : 289 : return AST::WhereClause (std::move (where_clause_items));
3827 : 289 : }
3828 : :
3829 : : /* Parses a where clause item (lifetime or type bound). Does not parse any
3830 : : * commas. */
3831 : : template <typename ManagedTokenSource>
3832 : : std::unique_ptr<AST::WhereClauseItem>
3833 : 299 : Parser<ManagedTokenSource>::parse_where_clause_item (
3834 : : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
3835 : : {
3836 : : // shitty cheat way of determining lifetime or type bound - test for
3837 : : // lifetime
3838 : 299 : const_TokenPtr t = lexer.peek_token ();
3839 : :
3840 : 299 : if (t->get_id () == LIFETIME)
3841 : 2 : return parse_lifetime_where_clause_item ();
3842 : : else
3843 : 297 : return parse_type_bound_where_clause_item (outer_for_lifetimes);
3844 : 299 : }
3845 : :
3846 : : // Parses a lifetime where clause item.
3847 : : template <typename ManagedTokenSource>
3848 : : std::unique_ptr<AST::LifetimeWhereClauseItem>
3849 : 2 : Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
3850 : : {
3851 : 2 : auto parsed_lifetime = parse_lifetime (false);
3852 : 2 : if (!parsed_lifetime)
3853 : : {
3854 : : // TODO: error here?
3855 : 0 : return nullptr;
3856 : : }
3857 : 2 : auto lifetime = parsed_lifetime.value ();
3858 : :
3859 : 2 : if (!skip_token (COLON))
3860 : : {
3861 : : // TODO: skip after somewhere
3862 : 0 : return nullptr;
3863 : : }
3864 : :
3865 : 2 : std::vector<AST::Lifetime> lifetime_bounds = parse_lifetime_bounds ();
3866 : : // TODO: have end token passed in?
3867 : :
3868 : 2 : location_t locus = lifetime.get_locus ();
3869 : :
3870 : : return std::unique_ptr<AST::LifetimeWhereClauseItem> (
3871 : 2 : new AST::LifetimeWhereClauseItem (std::move (lifetime),
3872 : 2 : std::move (lifetime_bounds), locus));
3873 : 4 : }
3874 : :
3875 : : // Parses a type bound where clause item.
3876 : : template <typename ManagedTokenSource>
3877 : : std::unique_ptr<AST::TypeBoundWhereClauseItem>
3878 : 297 : Parser<ManagedTokenSource>::parse_type_bound_where_clause_item (
3879 : : const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
3880 : : {
3881 : 297 : std::vector<AST::LifetimeParam> for_lifetimes = outer_for_lifetimes;
3882 : :
3883 : 297 : std::unique_ptr<AST::Type> type = parse_type ();
3884 : 297 : if (type == nullptr)
3885 : : {
3886 : 0 : return nullptr;
3887 : : }
3888 : :
3889 : 297 : if (!skip_token (COLON))
3890 : : {
3891 : : // TODO: skip after somewhere
3892 : 0 : return nullptr;
3893 : : }
3894 : :
3895 : 594 : if (lexer.peek_token ()->get_id () == FOR)
3896 : : {
3897 : 8 : auto for_lifetimes_inner = parse_for_lifetimes ();
3898 : 8 : for_lifetimes.insert (for_lifetimes.end (), for_lifetimes_inner.begin (),
3899 : : for_lifetimes_inner.end ());
3900 : 8 : }
3901 : :
3902 : : // parse type param bounds if they exist
3903 : 297 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds
3904 : : = parse_type_param_bounds ();
3905 : :
3906 : 297 : location_t locus = lexer.peek_token ()->get_locus ();
3907 : :
3908 : : return std::unique_ptr<AST::TypeBoundWhereClauseItem> (
3909 : 297 : new AST::TypeBoundWhereClauseItem (std::move (for_lifetimes),
3910 : : std::move (type),
3911 : 297 : std::move (type_param_bounds), locus));
3912 : 297 : }
3913 : :
3914 : : // Parses a for lifetimes clause, including the for keyword and angle
3915 : : // brackets.
3916 : : template <typename ManagedTokenSource>
3917 : : std::vector<AST::LifetimeParam>
3918 : 26 : Parser<ManagedTokenSource>::parse_for_lifetimes ()
3919 : : {
3920 : 26 : std::vector<AST::LifetimeParam> params;
3921 : :
3922 : 26 : if (!skip_token (FOR))
3923 : : {
3924 : : // skip after somewhere?
3925 : : return params;
3926 : : }
3927 : :
3928 : 26 : if (!skip_token (LEFT_ANGLE))
3929 : : {
3930 : : // skip after somewhere?
3931 : : return params;
3932 : : }
3933 : :
3934 : : /* cannot specify end token due to parsing problems with '>' tokens being
3935 : : * nested */
3936 : 26 : params = parse_lifetime_params_objs (is_right_angle_tok);
3937 : :
3938 : 26 : if (!skip_generics_right_angle ())
3939 : : {
3940 : : // DEBUG
3941 : 0 : rust_debug ("failed to skip generics right angle after (supposedly) "
3942 : : "finished parsing where clause items");
3943 : : // ok, well this gets called.
3944 : :
3945 : : // skip after somewhere?
3946 : 0 : return params;
3947 : : }
3948 : :
3949 : : return params;
3950 : : }
3951 : :
3952 : : // Parses type parameter bounds in where clause or generic arguments.
3953 : : template <typename ManagedTokenSource>
3954 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
3955 : 937 : Parser<ManagedTokenSource>::parse_type_param_bounds ()
3956 : : {
3957 : 937 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3958 : :
3959 : 937 : std::unique_ptr<AST::TypeParamBound> initial_bound
3960 : : = parse_type_param_bound ();
3961 : :
3962 : : // quick exit if null
3963 : 937 : if (initial_bound == nullptr)
3964 : : {
3965 : : /* error? type param bounds must have at least one term, but are bounds
3966 : : * optional? */
3967 : : return type_param_bounds;
3968 : : }
3969 : 937 : type_param_bounds.push_back (std::move (initial_bound));
3970 : :
3971 : 1880 : while (lexer.peek_token ()->get_id () == PLUS)
3972 : : {
3973 : 3 : lexer.skip_token ();
3974 : :
3975 : 3 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
3976 : 3 : if (bound == nullptr)
3977 : : {
3978 : : /* not an error: bound is allowed to be null as trailing plus is
3979 : : * allowed */
3980 : : return type_param_bounds;
3981 : : }
3982 : :
3983 : 3 : type_param_bounds.push_back (std::move (bound));
3984 : : }
3985 : :
3986 : 937 : type_param_bounds.shrink_to_fit ();
3987 : : return type_param_bounds;
3988 : 937 : }
3989 : :
3990 : : /* Parses type parameter bounds in where clause or generic arguments, with end
3991 : : * token handling. */
3992 : : template <typename ManagedTokenSource>
3993 : : template <typename EndTokenPred>
3994 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
3995 : 571 : Parser<ManagedTokenSource>::parse_type_param_bounds (EndTokenPred is_end_token)
3996 : : {
3997 : 571 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
3998 : :
3999 : 571 : std::unique_ptr<AST::TypeParamBound> initial_bound
4000 : : = parse_type_param_bound ();
4001 : :
4002 : : // quick exit if null
4003 : 571 : if (initial_bound == nullptr)
4004 : : {
4005 : : /* error? type param bounds must have at least one term, but are bounds
4006 : : * optional? */
4007 : 0 : return type_param_bounds;
4008 : : }
4009 : 571 : type_param_bounds.push_back (std::move (initial_bound));
4010 : :
4011 : 1286 : while (lexer.peek_token ()->get_id () == PLUS)
4012 : : {
4013 : 72 : lexer.skip_token ();
4014 : :
4015 : : // break if end token character
4016 : 144 : if (is_end_token (lexer.peek_token ()->get_id ()))
4017 : : break;
4018 : :
4019 : 72 : std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
4020 : 72 : if (bound == nullptr)
4021 : : {
4022 : : // TODO how wise is it to ditch all bounds if only one failed?
4023 : 0 : Error error (lexer.peek_token ()->get_locus (),
4024 : : "failed to parse type param bound in type param bounds");
4025 : 0 : add_error (std::move (error));
4026 : :
4027 : 0 : return {};
4028 : 0 : }
4029 : :
4030 : 72 : type_param_bounds.push_back (std::move (bound));
4031 : : }
4032 : :
4033 : 571 : type_param_bounds.shrink_to_fit ();
4034 : 571 : return type_param_bounds;
4035 : 571 : }
4036 : :
4037 : : /* Parses a single type parameter bound in a where clause or generic argument.
4038 : : * Does not parse the '+' between arguments. */
4039 : : template <typename ManagedTokenSource>
4040 : : std::unique_ptr<AST::TypeParamBound>
4041 : 1603 : Parser<ManagedTokenSource>::parse_type_param_bound ()
4042 : : {
4043 : : // shitty cheat way of determining lifetime or trait bound - test for
4044 : : // lifetime
4045 : 1603 : const_TokenPtr t = lexer.peek_token ();
4046 : 1603 : switch (t->get_id ())
4047 : : {
4048 : 12 : case LIFETIME:
4049 : 12 : return std::unique_ptr<AST::Lifetime> (
4050 : 24 : new AST::Lifetime (parse_lifetime (false).value ()));
4051 : 1591 : case LEFT_PAREN:
4052 : : case QUESTION_MARK:
4053 : : case FOR:
4054 : : case IDENTIFIER:
4055 : : case SUPER:
4056 : : case SELF:
4057 : : case SELF_ALIAS:
4058 : : case CRATE:
4059 : : case DOLLAR_SIGN:
4060 : : case SCOPE_RESOLUTION:
4061 : 1591 : return parse_trait_bound ();
4062 : 0 : default:
4063 : : // don't error - assume this is fine TODO
4064 : 0 : return nullptr;
4065 : : }
4066 : 1603 : }
4067 : :
4068 : : // Parses a trait bound type param bound.
4069 : : template <typename ManagedTokenSource>
4070 : : std::unique_ptr<AST::TraitBound>
4071 : 1894 : Parser<ManagedTokenSource>::parse_trait_bound ()
4072 : : {
4073 : 1894 : bool has_parens = false;
4074 : 1894 : bool has_question_mark = false;
4075 : :
4076 : 3788 : location_t locus = lexer.peek_token ()->get_locus ();
4077 : :
4078 : : /* parse optional `for lifetimes`. */
4079 : 1894 : std::vector<AST::LifetimeParam> for_lifetimes;
4080 : 3788 : if (lexer.peek_token ()->get_id () == FOR)
4081 : 14 : for_lifetimes = parse_for_lifetimes ();
4082 : :
4083 : : // handle trait bound being in parentheses
4084 : 3788 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
4085 : : {
4086 : 0 : has_parens = true;
4087 : 0 : lexer.skip_token ();
4088 : : }
4089 : :
4090 : : // handle having question mark (optional)
4091 : 3788 : if (lexer.peek_token ()->get_id () == QUESTION_MARK)
4092 : : {
4093 : 304 : has_question_mark = true;
4094 : 304 : lexer.skip_token ();
4095 : : }
4096 : :
4097 : : // handle TypePath
4098 : 1894 : AST::TypePath type_path = parse_type_path ();
4099 : :
4100 : : // handle closing parentheses
4101 : 1894 : if (has_parens)
4102 : : {
4103 : 0 : if (!skip_token (RIGHT_PAREN))
4104 : : {
4105 : 0 : return nullptr;
4106 : : }
4107 : : }
4108 : :
4109 : : return std::unique_ptr<AST::TraitBound> (
4110 : 1894 : new AST::TraitBound (std::move (type_path), locus, has_parens,
4111 : 1894 : has_question_mark, std::move (for_lifetimes)));
4112 : 1894 : }
4113 : :
4114 : : // Parses lifetime bounds.
4115 : : template <typename ManagedTokenSource>
4116 : : std::vector<AST::Lifetime>
4117 : 2 : Parser<ManagedTokenSource>::parse_lifetime_bounds ()
4118 : : {
4119 : 2 : std::vector<AST::Lifetime> lifetime_bounds;
4120 : :
4121 : 2 : while (true)
4122 : : {
4123 : 2 : auto lifetime = parse_lifetime (false);
4124 : :
4125 : : // quick exit for parsing failure
4126 : 2 : if (!lifetime)
4127 : : break;
4128 : :
4129 : 2 : lifetime_bounds.push_back (std::move (lifetime.value ()));
4130 : :
4131 : : /* plus is maybe not allowed at end - spec defines it weirdly, so
4132 : : * assuming allowed at end */
4133 : 4 : if (lexer.peek_token ()->get_id () != PLUS)
4134 : : break;
4135 : :
4136 : 0 : lexer.skip_token ();
4137 : : }
4138 : :
4139 : 2 : lifetime_bounds.shrink_to_fit ();
4140 : 2 : return lifetime_bounds;
4141 : : }
4142 : :
4143 : : // Parses lifetime bounds, with added check for ending token.
4144 : : template <typename ManagedTokenSource>
4145 : : template <typename EndTokenPred>
4146 : : std::vector<AST::Lifetime>
4147 : 1 : Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
4148 : : {
4149 : 1 : std::vector<AST::Lifetime> lifetime_bounds;
4150 : :
4151 : 4 : while (!is_end_token (lexer.peek_token ()->get_id ()))
4152 : : {
4153 : 1 : auto lifetime = parse_lifetime (false);
4154 : :
4155 : 1 : if (!lifetime)
4156 : : {
4157 : : /* TODO: is it worth throwing away all lifetime bound info just
4158 : : * because one failed? */
4159 : 0 : Error error (lexer.peek_token ()->get_locus (),
4160 : : "failed to parse lifetime in lifetime bounds");
4161 : 0 : add_error (std::move (error));
4162 : :
4163 : 0 : return {};
4164 : 0 : }
4165 : :
4166 : 1 : lifetime_bounds.push_back (std::move (lifetime.value ()));
4167 : :
4168 : : /* plus is maybe not allowed at end - spec defines it weirdly, so
4169 : : * assuming allowed at end */
4170 : 2 : if (lexer.peek_token ()->get_id () != PLUS)
4171 : : break;
4172 : :
4173 : 0 : lexer.skip_token ();
4174 : : }
4175 : :
4176 : 1 : lifetime_bounds.shrink_to_fit ();
4177 : 1 : return lifetime_bounds;
4178 : 1 : }
4179 : :
4180 : : /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
4181 : : * existing. */
4182 : : template <typename ManagedTokenSource>
4183 : : tl::expected<AST::Lifetime, ParseLifetimeError>
4184 : 4354 : Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
4185 : : {
4186 : 4354 : const_TokenPtr lifetime_tok = lexer.peek_token ();
4187 : 4354 : if (lifetime_tok->get_id () != LIFETIME)
4188 : : {
4189 : 3576 : if (allow_elided)
4190 : : {
4191 : 0 : return AST::Lifetime::elided ();
4192 : : }
4193 : : else
4194 : : {
4195 : 3576 : return tl::make_unexpected<ParseLifetimeError> ({});
4196 : : }
4197 : : }
4198 : 778 : lexer.skip_token ();
4199 : :
4200 : 1556 : return lifetime_from_token (lifetime_tok);
4201 : 4354 : }
4202 : :
4203 : : template <typename ManagedTokenSource>
4204 : : AST::Lifetime
4205 : 812 : Parser<ManagedTokenSource>::lifetime_from_token (const_TokenPtr tok)
4206 : : {
4207 : 812 : location_t locus = tok->get_locus ();
4208 : 812 : std::string lifetime_ident = tok->get_str ();
4209 : :
4210 : 812 : if (lifetime_ident == "static")
4211 : : {
4212 : 84 : return AST::Lifetime (AST::Lifetime::STATIC, "", locus);
4213 : : }
4214 : 728 : else if (lifetime_ident == "_")
4215 : : {
4216 : : // Explicitly and implicitly elided lifetimes follow the same rules.
4217 : 43 : return AST::Lifetime (AST::Lifetime::WILDCARD, "", locus);
4218 : : }
4219 : : else
4220 : : {
4221 : 1370 : return AST::Lifetime (AST::Lifetime::NAMED, std::move (lifetime_ident),
4222 : 685 : locus);
4223 : : }
4224 : 812 : }
4225 : :
4226 : : template <typename ManagedTokenSource>
4227 : : std::unique_ptr<AST::ExternalTypeItem>
4228 : 4 : Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
4229 : : AST::AttrVec outer_attrs)
4230 : : {
4231 : 4 : location_t locus = lexer.peek_token ()->get_locus ();
4232 : 4 : skip_token (TYPE);
4233 : :
4234 : 4 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
4235 : 4 : if (alias_name_tok == nullptr)
4236 : : {
4237 : 0 : Error error (lexer.peek_token ()->get_locus (),
4238 : : "could not parse identifier in external opaque type");
4239 : 0 : add_error (std::move (error));
4240 : :
4241 : 0 : skip_after_semicolon ();
4242 : 0 : return nullptr;
4243 : 0 : }
4244 : :
4245 : 4 : if (!skip_token (SEMICOLON))
4246 : 1 : return nullptr;
4247 : :
4248 : : return std::unique_ptr<AST::ExternalTypeItem> (
4249 : 9 : new AST::ExternalTypeItem (alias_name_tok->get_str (), std::move (vis),
4250 : 3 : std::move (outer_attrs), std::move (locus)));
4251 : 4 : }
4252 : :
4253 : : // Parses a "type alias" (typedef) item.
4254 : : template <typename ManagedTokenSource>
4255 : : std::unique_ptr<AST::TypeAlias>
4256 : 1281 : Parser<ManagedTokenSource>::parse_type_alias (AST::Visibility vis,
4257 : : AST::AttrVec outer_attrs)
4258 : : {
4259 : 1281 : location_t locus = lexer.peek_token ()->get_locus ();
4260 : 1281 : skip_token (TYPE);
4261 : :
4262 : : // TODO: use this token for identifier when finished that
4263 : 1281 : const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
4264 : 1281 : if (alias_name_tok == nullptr)
4265 : : {
4266 : 0 : Error error (lexer.peek_token ()->get_locus (),
4267 : : "could not parse identifier in type alias");
4268 : 0 : add_error (std::move (error));
4269 : :
4270 : 0 : skip_after_semicolon ();
4271 : 0 : return nullptr;
4272 : 0 : }
4273 : 1281 : Identifier alias_name{alias_name_tok};
4274 : :
4275 : : // parse generic params, which may not exist
4276 : 1281 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4277 : : = parse_generic_params_in_angles ();
4278 : :
4279 : : // parse where clause, which may not exist
4280 : 1281 : AST::WhereClause where_clause = parse_where_clause ();
4281 : :
4282 : 1281 : if (!skip_token (EQUAL))
4283 : : {
4284 : 0 : skip_after_semicolon ();
4285 : 0 : return nullptr;
4286 : : }
4287 : :
4288 : 1281 : std::unique_ptr<AST::Type> type_to_alias = parse_type ();
4289 : :
4290 : 1281 : if (!skip_token (SEMICOLON))
4291 : : {
4292 : : // should be skipping past this, not the next line
4293 : 0 : return nullptr;
4294 : : }
4295 : :
4296 : : return std::unique_ptr<AST::TypeAlias> (
4297 : 1281 : new AST::TypeAlias (std::move (alias_name), std::move (generic_params),
4298 : : std::move (where_clause), std::move (type_to_alias),
4299 : 1281 : std::move (vis), std::move (outer_attrs), locus));
4300 : 2562 : }
4301 : :
4302 : : // Parse a struct item AST node.
4303 : : template <typename ManagedTokenSource>
4304 : : std::unique_ptr<AST::Struct>
4305 : 2600 : Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
4306 : : AST::AttrVec outer_attrs)
4307 : : {
4308 : : /* TODO: determine best way to parse the proper struct vs tuple struct -
4309 : : * share most of initial constructs so lookahead might be impossible, and if
4310 : : * not probably too expensive. Best way is probably unified parsing for the
4311 : : * initial parts and then pass them in as params to more derived functions.
4312 : : * Alternatively, just parse everything in this one function - do this if
4313 : : * function not too long. */
4314 : :
4315 : : /* Proper struct <- 'struct' IDENTIFIER generic_params? where_clause? ( '{'
4316 : : * struct_fields? '}' | ';' ) */
4317 : : /* Tuple struct <- 'struct' IDENTIFIER generic_params? '(' tuple_fields? ')'
4318 : : * where_clause? ';' */
4319 : 2600 : location_t locus = lexer.peek_token ()->get_locus ();
4320 : 2600 : skip_token (STRUCT_KW);
4321 : :
4322 : : // parse struct name
4323 : 2600 : const_TokenPtr name_tok = expect_token (IDENTIFIER);
4324 : 2600 : if (name_tok == nullptr)
4325 : : {
4326 : 0 : Error error (lexer.peek_token ()->get_locus (),
4327 : : "could not parse struct or tuple struct identifier");
4328 : 0 : add_error (std::move (error));
4329 : :
4330 : : // skip after somewhere?
4331 : 0 : return nullptr;
4332 : 0 : }
4333 : 2600 : Identifier struct_name{name_tok};
4334 : :
4335 : : // parse generic params, which may or may not exist
4336 : 2600 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4337 : : = parse_generic_params_in_angles ();
4338 : :
4339 : : // branch on next token - determines whether proper struct or tuple struct
4340 : 5200 : if (lexer.peek_token ()->get_id () == LEFT_PAREN)
4341 : : {
4342 : : // tuple struct
4343 : :
4344 : : // skip left parenthesis
4345 : 959 : lexer.skip_token ();
4346 : :
4347 : : // parse tuple fields
4348 : 959 : std::vector<AST::TupleField> tuple_fields;
4349 : : // Might be empty tuple for unit tuple struct.
4350 : 1918 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4351 : 22 : tuple_fields = std::vector<AST::TupleField> ();
4352 : : else
4353 : 937 : tuple_fields = parse_tuple_fields ();
4354 : :
4355 : : // tuple parameters must have closing parenthesis
4356 : 959 : if (!skip_token (RIGHT_PAREN))
4357 : : {
4358 : 0 : skip_after_semicolon ();
4359 : 0 : return nullptr;
4360 : : }
4361 : :
4362 : : // parse where clause, which is optional
4363 : 959 : AST::WhereClause where_clause = parse_where_clause ();
4364 : :
4365 : 959 : if (!skip_token (SEMICOLON))
4366 : : {
4367 : : // can't skip after semicolon because it's meant to be here
4368 : 0 : return nullptr;
4369 : : }
4370 : :
4371 : 959 : return std::unique_ptr<AST::TupleStruct> (
4372 : 959 : new AST::TupleStruct (std::move (tuple_fields), std::move (struct_name),
4373 : : std::move (generic_params),
4374 : : std::move (where_clause), std::move (vis),
4375 : 959 : std::move (outer_attrs), locus));
4376 : 959 : }
4377 : :
4378 : : // assume it is a proper struct being parsed and continue outside of switch
4379 : : // - label only here to suppress warning
4380 : :
4381 : : // parse where clause, which is optional
4382 : 1641 : AST::WhereClause where_clause = parse_where_clause ();
4383 : :
4384 : : // branch on next token - determines whether struct is a unit struct
4385 : 1641 : const_TokenPtr t = lexer.peek_token ();
4386 : 1641 : switch (t->get_id ())
4387 : : {
4388 : 935 : case LEFT_CURLY:
4389 : : {
4390 : : // struct with body
4391 : :
4392 : : // skip curly bracket
4393 : 935 : lexer.skip_token ();
4394 : :
4395 : : // parse struct fields, if any
4396 : 935 : std::vector<AST::StructField> struct_fields
4397 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4398 : :
4399 : 935 : if (!skip_token (RIGHT_CURLY))
4400 : : {
4401 : : // skip somewhere?
4402 : 0 : return nullptr;
4403 : : }
4404 : :
4405 : 935 : return std::unique_ptr<AST::StructStruct> (new AST::StructStruct (
4406 : : std::move (struct_fields), std::move (struct_name),
4407 : : std::move (generic_params), std::move (where_clause), false,
4408 : 935 : std::move (vis), std::move (outer_attrs), locus));
4409 : 935 : }
4410 : 705 : case SEMICOLON:
4411 : : // unit struct declaration
4412 : :
4413 : 705 : lexer.skip_token ();
4414 : :
4415 : 705 : return std::unique_ptr<AST::StructStruct> (
4416 : 1410 : new AST::StructStruct (std::move (struct_name),
4417 : : std::move (generic_params),
4418 : : std::move (where_clause), std::move (vis),
4419 : 705 : std::move (outer_attrs), locus));
4420 : 1 : default:
4421 : 1 : add_error (Error (t->get_locus (),
4422 : : "unexpected token %qs in struct declaration",
4423 : : t->get_token_description ()));
4424 : :
4425 : : // skip somewhere?
4426 : 1 : return nullptr;
4427 : : }
4428 : 2600 : }
4429 : :
4430 : : // Parses struct fields in struct declarations.
4431 : : template <typename ManagedTokenSource>
4432 : : std::vector<AST::StructField>
4433 : 0 : Parser<ManagedTokenSource>::parse_struct_fields ()
4434 : : {
4435 : 0 : std::vector<AST::StructField> fields;
4436 : :
4437 : 0 : AST::StructField initial_field = parse_struct_field ();
4438 : :
4439 : : // Return empty field list if no field there
4440 : 0 : if (initial_field.is_error ())
4441 : : return fields;
4442 : :
4443 : 0 : fields.push_back (std::move (initial_field));
4444 : :
4445 : 0 : while (lexer.peek_token ()->get_id () == COMMA)
4446 : : {
4447 : 0 : lexer.skip_token ();
4448 : :
4449 : 0 : AST::StructField field = parse_struct_field ();
4450 : :
4451 : 0 : if (field.is_error ())
4452 : : {
4453 : : // would occur with trailing comma, so allowed
4454 : : break;
4455 : : }
4456 : :
4457 : 0 : fields.push_back (std::move (field));
4458 : : }
4459 : :
4460 : 0 : fields.shrink_to_fit ();
4461 : : return fields;
4462 : : // TODO: template if possible (parse_non_ptr_seq)
4463 : 0 : }
4464 : :
4465 : : // Parses struct fields in struct declarations.
4466 : : template <typename ManagedTokenSource>
4467 : : template <typename EndTokenPred>
4468 : : std::vector<AST::StructField>
4469 : 1134 : Parser<ManagedTokenSource>::parse_struct_fields (EndTokenPred is_end_tok)
4470 : : {
4471 : 1134 : std::vector<AST::StructField> fields;
4472 : :
4473 : 1134 : AST::StructField initial_field = parse_struct_field ();
4474 : :
4475 : : // Return empty field list if no field there
4476 : 1134 : if (initial_field.is_error ())
4477 : 43 : return fields;
4478 : :
4479 : 1091 : fields.push_back (std::move (initial_field));
4480 : :
4481 : 4494 : while (lexer.peek_token ()->get_id () == COMMA)
4482 : : {
4483 : 2066 : lexer.skip_token ();
4484 : :
4485 : 4132 : if (is_end_tok (lexer.peek_token ()->get_id ()))
4486 : : break;
4487 : :
4488 : 1156 : AST::StructField field = parse_struct_field ();
4489 : 1156 : if (field.is_error ())
4490 : : {
4491 : : /* TODO: should every field be ditched just because one couldn't be
4492 : : * parsed? */
4493 : 0 : Error error (lexer.peek_token ()->get_locus (),
4494 : : "failed to parse struct field in struct fields");
4495 : 0 : add_error (std::move (error));
4496 : :
4497 : 0 : return {};
4498 : 0 : }
4499 : :
4500 : 1156 : fields.push_back (std::move (field));
4501 : : }
4502 : :
4503 : 1091 : fields.shrink_to_fit ();
4504 : 1091 : return fields;
4505 : : // TODO: template if possible (parse_non_ptr_seq)
4506 : 1134 : }
4507 : :
4508 : : // Parses a single struct field (in a struct definition). Does not parse
4509 : : // commas.
4510 : : template <typename ManagedTokenSource>
4511 : : AST::StructField
4512 : 2290 : Parser<ManagedTokenSource>::parse_struct_field ()
4513 : : {
4514 : : // parse outer attributes, if they exist
4515 : 2290 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4516 : :
4517 : : // parse visibility, if it exists
4518 : 2290 : AST::Visibility vis = parse_visibility ();
4519 : :
4520 : 2290 : location_t locus = lexer.peek_token ()->get_locus ();
4521 : :
4522 : : // parse field name
4523 : 2290 : const_TokenPtr field_name_tok = lexer.peek_token ();
4524 : 2290 : if (field_name_tok->get_id () != IDENTIFIER)
4525 : : {
4526 : : // if not identifier, assumes there is no struct field and exits - not
4527 : : // necessarily error
4528 : 43 : return AST::StructField::create_error ();
4529 : : }
4530 : 2247 : Identifier field_name{field_name_tok};
4531 : 2247 : lexer.skip_token ();
4532 : :
4533 : 2247 : if (!skip_token (COLON))
4534 : : {
4535 : : // skip after somewhere?
4536 : 0 : return AST::StructField::create_error ();
4537 : : }
4538 : :
4539 : : // parse field type - this is required
4540 : 2247 : std::unique_ptr<AST::Type> field_type = parse_type ();
4541 : 2247 : if (field_type == nullptr)
4542 : : {
4543 : 0 : Error error (lexer.peek_token ()->get_locus (),
4544 : : "could not parse type in struct field definition");
4545 : 0 : add_error (std::move (error));
4546 : :
4547 : : // skip after somewhere
4548 : 0 : return AST::StructField::create_error ();
4549 : 0 : }
4550 : :
4551 : 4494 : return AST::StructField (std::move (field_name), std::move (field_type),
4552 : 2247 : std::move (vis), locus, std::move (outer_attrs));
4553 : 6784 : }
4554 : :
4555 : : // Parses tuple fields in tuple/tuple struct declarations.
4556 : : template <typename ManagedTokenSource>
4557 : : std::vector<AST::TupleField>
4558 : 1356 : Parser<ManagedTokenSource>::parse_tuple_fields ()
4559 : : {
4560 : 1356 : std::vector<AST::TupleField> fields;
4561 : :
4562 : 1356 : AST::TupleField initial_field = parse_tuple_field ();
4563 : :
4564 : : // Return empty field list if no field there
4565 : 1356 : if (initial_field.is_error ())
4566 : : {
4567 : 0 : return fields;
4568 : : }
4569 : :
4570 : 1356 : fields.push_back (std::move (initial_field));
4571 : :
4572 : : // maybe think of a better control structure here - do-while with an initial
4573 : : // error state? basically, loop through field list until can't find any more
4574 : : // params HACK: all current syntax uses of tuple fields have them ending
4575 : : // with a right paren token
4576 : 1356 : const_TokenPtr t = lexer.peek_token ();
4577 : 2055 : while (t->get_id () == COMMA)
4578 : : {
4579 : : // skip comma if applies - e.g. trailing comma
4580 : 699 : lexer.skip_token ();
4581 : :
4582 : : // break out due to right paren if it exists
4583 : 1398 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4584 : : {
4585 : : break;
4586 : : }
4587 : :
4588 : 699 : AST::TupleField field = parse_tuple_field ();
4589 : 699 : if (field.is_error ())
4590 : : {
4591 : 0 : Error error (lexer.peek_token ()->get_locus (),
4592 : : "failed to parse tuple field in tuple fields");
4593 : 0 : add_error (std::move (error));
4594 : :
4595 : 0 : return std::vector<AST::TupleField> ();
4596 : 0 : }
4597 : :
4598 : 699 : fields.push_back (std::move (field));
4599 : :
4600 : 699 : t = lexer.peek_token ();
4601 : : }
4602 : :
4603 : 1356 : fields.shrink_to_fit ();
4604 : 1356 : return fields;
4605 : :
4606 : : // TODO: this shares basically all code with function params and struct
4607 : : // fields
4608 : : // - templates?
4609 : 1356 : }
4610 : :
4611 : : /* Parses a single tuple struct field in a tuple struct definition. Does not
4612 : : * parse commas. */
4613 : : template <typename ManagedTokenSource>
4614 : : AST::TupleField
4615 : 2055 : Parser<ManagedTokenSource>::parse_tuple_field ()
4616 : : {
4617 : : // parse outer attributes if they exist
4618 : 2055 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4619 : :
4620 : : // parse visibility if it exists
4621 : 2055 : AST::Visibility vis = parse_visibility ();
4622 : :
4623 : 2055 : location_t locus = lexer.peek_token ()->get_locus ();
4624 : :
4625 : : // parse type, which is required
4626 : 2055 : std::unique_ptr<AST::Type> field_type = parse_type ();
4627 : 2055 : if (field_type == nullptr)
4628 : : {
4629 : : // error if null
4630 : 0 : Error error (lexer.peek_token ()->get_locus (),
4631 : : "could not parse type in tuple struct field");
4632 : 0 : add_error (std::move (error));
4633 : :
4634 : : // skip after something
4635 : 0 : return AST::TupleField::create_error ();
4636 : 0 : }
4637 : :
4638 : 2055 : return AST::TupleField (std::move (field_type), std::move (vis), locus,
4639 : 2055 : std::move (outer_attrs));
4640 : 2055 : }
4641 : :
4642 : : // Parses a Rust "enum" tagged union item definition.
4643 : : template <typename ManagedTokenSource>
4644 : : std::unique_ptr<AST::Enum>
4645 : 534 : Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis,
4646 : : AST::AttrVec outer_attrs)
4647 : : {
4648 : 534 : location_t locus = lexer.peek_token ()->get_locus ();
4649 : 534 : skip_token (ENUM_KW);
4650 : :
4651 : : // parse enum name
4652 : 534 : const_TokenPtr enum_name_tok = expect_token (IDENTIFIER);
4653 : 534 : if (enum_name_tok == nullptr)
4654 : 0 : return nullptr;
4655 : :
4656 : 534 : Identifier enum_name = {enum_name_tok};
4657 : :
4658 : : // parse generic params (of enum container, not enum variants) if they exist
4659 : 534 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4660 : : = parse_generic_params_in_angles ();
4661 : :
4662 : : // parse where clause if it exists
4663 : 534 : AST::WhereClause where_clause = parse_where_clause ();
4664 : :
4665 : 534 : if (!skip_token (LEFT_CURLY))
4666 : : {
4667 : 0 : skip_after_end_block ();
4668 : 0 : return nullptr;
4669 : : }
4670 : :
4671 : : // parse actual enum variant definitions
4672 : 534 : std::vector<std::unique_ptr<AST::EnumItem>> enum_items
4673 : : = parse_enum_items ([] (TokenId id) { return id == RIGHT_CURLY; });
4674 : :
4675 : 534 : if (!skip_token (RIGHT_CURLY))
4676 : : {
4677 : 0 : skip_after_end_block ();
4678 : 0 : return nullptr;
4679 : : }
4680 : :
4681 : : return std::unique_ptr<AST::Enum> (
4682 : 534 : new AST::Enum (std::move (enum_name), std::move (vis),
4683 : : std::move (generic_params), std::move (where_clause),
4684 : 534 : std::move (enum_items), std::move (outer_attrs), locus));
4685 : 1068 : }
4686 : :
4687 : : // Parses the enum variants inside an enum definiton.
4688 : : template <typename ManagedTokenSource>
4689 : : std::vector<std::unique_ptr<AST::EnumItem>>
4690 : 0 : Parser<ManagedTokenSource>::parse_enum_items ()
4691 : : {
4692 : 0 : std::vector<std::unique_ptr<AST::EnumItem>> items;
4693 : :
4694 : 0 : std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
4695 : :
4696 : : // Return empty item list if no field there
4697 : 0 : if (initial_item == nullptr)
4698 : : return items;
4699 : :
4700 : 0 : items.push_back (std::move (initial_item));
4701 : :
4702 : 0 : while (lexer.peek_token ()->get_id () == COMMA)
4703 : : {
4704 : 0 : lexer.skip_token ();
4705 : :
4706 : 0 : std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
4707 : 0 : if (item == nullptr)
4708 : : {
4709 : : // this would occur with a trailing comma, which is allowed
4710 : : break;
4711 : : }
4712 : :
4713 : 0 : items.push_back (std::move (item));
4714 : : }
4715 : :
4716 : 0 : items.shrink_to_fit ();
4717 : : return items;
4718 : :
4719 : : /* TODO: use template if doable (parse_non_ptr_sequence) */
4720 : 0 : }
4721 : :
4722 : : // Parses the enum variants inside an enum definiton.
4723 : : template <typename ManagedTokenSource>
4724 : : template <typename EndTokenPred>
4725 : : std::vector<std::unique_ptr<AST::EnumItem>>
4726 : 534 : Parser<ManagedTokenSource>::parse_enum_items (EndTokenPred is_end_tok)
4727 : : {
4728 : 534 : std::vector<std::unique_ptr<AST::EnumItem>> items;
4729 : :
4730 : 534 : std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
4731 : :
4732 : : // Return empty item list if no field there
4733 : 534 : if (initial_item == nullptr)
4734 : 13 : return items;
4735 : :
4736 : 521 : items.push_back (std::move (initial_item));
4737 : :
4738 : 2510 : while (lexer.peek_token ()->get_id () == COMMA)
4739 : : {
4740 : 1223 : lexer.skip_token ();
4741 : :
4742 : 2446 : if (is_end_tok (lexer.peek_token ()->get_id ()))
4743 : : break;
4744 : :
4745 : 734 : std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
4746 : 734 : if (item == nullptr)
4747 : : {
4748 : : /* TODO should this ignore all successfully parsed enum items just
4749 : : * because one failed? */
4750 : 0 : Error error (lexer.peek_token ()->get_locus (),
4751 : : "failed to parse enum item in enum items");
4752 : 0 : add_error (std::move (error));
4753 : :
4754 : 0 : return {};
4755 : 0 : }
4756 : :
4757 : 734 : items.push_back (std::move (item));
4758 : : }
4759 : :
4760 : 521 : items.shrink_to_fit ();
4761 : 521 : return items;
4762 : :
4763 : : /* TODO: use template if doable (parse_non_ptr_sequence) */
4764 : 534 : }
4765 : :
4766 : : /* Parses a single enum variant item in an enum definition. Does not parse
4767 : : * commas. */
4768 : : template <typename ManagedTokenSource>
4769 : : std::unique_ptr<AST::EnumItem>
4770 : 1268 : Parser<ManagedTokenSource>::parse_enum_item ()
4771 : : {
4772 : : // parse outer attributes if they exist
4773 : 1268 : AST::AttrVec outer_attrs = parse_outer_attributes ();
4774 : :
4775 : : // parse visibility, which may or may not exist
4776 : 1268 : AST::Visibility vis = parse_visibility ();
4777 : :
4778 : : // parse name for enum item, which is required
4779 : 1268 : const_TokenPtr item_name_tok = lexer.peek_token ();
4780 : 1268 : if (item_name_tok->get_id () != IDENTIFIER)
4781 : : {
4782 : : // this may not be an error but it means there is no enum item here
4783 : 13 : return nullptr;
4784 : : }
4785 : 1255 : lexer.skip_token ();
4786 : 1255 : Identifier item_name{item_name_tok};
4787 : :
4788 : : // branch based on next token
4789 : 1255 : const_TokenPtr t = lexer.peek_token ();
4790 : 1255 : switch (t->get_id ())
4791 : : {
4792 : 435 : case LEFT_PAREN:
4793 : : {
4794 : : // tuple enum item
4795 : 435 : lexer.skip_token ();
4796 : :
4797 : 435 : std::vector<AST::TupleField> tuple_fields;
4798 : : // Might be empty tuple for unit tuple enum variant.
4799 : 870 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
4800 : 16 : tuple_fields = std::vector<AST::TupleField> ();
4801 : : else
4802 : 419 : tuple_fields = parse_tuple_fields ();
4803 : :
4804 : 435 : if (!skip_token (RIGHT_PAREN))
4805 : : {
4806 : : // skip after somewhere
4807 : 0 : return nullptr;
4808 : : }
4809 : :
4810 : 435 : return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
4811 : : std::move (item_name), std::move (vis), std::move (tuple_fields),
4812 : 435 : std::move (outer_attrs), item_name_tok->get_locus ()));
4813 : 435 : }
4814 : 92 : case LEFT_CURLY:
4815 : : {
4816 : : // struct enum item
4817 : 92 : lexer.skip_token ();
4818 : :
4819 : 92 : std::vector<AST::StructField> struct_fields
4820 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4821 : :
4822 : 92 : if (!skip_token (RIGHT_CURLY))
4823 : : {
4824 : : // skip after somewhere
4825 : 0 : return nullptr;
4826 : : }
4827 : :
4828 : 92 : return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
4829 : : std::move (item_name), std::move (vis), std::move (struct_fields),
4830 : 92 : std::move (outer_attrs), item_name_tok->get_locus ()));
4831 : 92 : }
4832 : 277 : case EQUAL:
4833 : : {
4834 : : // discriminant enum item
4835 : 277 : lexer.skip_token ();
4836 : :
4837 : 277 : std::unique_ptr<AST::Expr> discriminant_expr = parse_expr ();
4838 : :
4839 : 277 : return std::unique_ptr<AST::EnumItemDiscriminant> (
4840 : 554 : new AST::EnumItemDiscriminant (std::move (item_name), std::move (vis),
4841 : : std::move (discriminant_expr),
4842 : : std::move (outer_attrs),
4843 : 277 : item_name_tok->get_locus ()));
4844 : 277 : }
4845 : 451 : default:
4846 : : // regular enum with just an identifier
4847 : : return std::unique_ptr<AST::EnumItem> (
4848 : 451 : new AST::EnumItem (std::move (item_name), std::move (vis),
4849 : : std::move (outer_attrs),
4850 : 451 : item_name_tok->get_locus ()));
4851 : : }
4852 : 2523 : }
4853 : :
4854 : : // Parses a C-style (and C-compat) untagged union declaration.
4855 : : template <typename ManagedTokenSource>
4856 : : std::unique_ptr<AST::Union>
4857 : 107 : Parser<ManagedTokenSource>::parse_union (AST::Visibility vis,
4858 : : AST::AttrVec outer_attrs)
4859 : : {
4860 : : /* hack - "weak keyword" by finding identifier called "union" (lookahead in
4861 : : * item switch) */
4862 : 107 : const_TokenPtr union_keyword = expect_token (IDENTIFIER);
4863 : 107 : rust_assert (union_keyword->get_str () == Values::WeakKeywords::UNION);
4864 : 107 : location_t locus = union_keyword->get_locus ();
4865 : :
4866 : : // parse actual union name
4867 : 107 : const_TokenPtr union_name_tok = expect_token (IDENTIFIER);
4868 : 107 : if (union_name_tok == nullptr)
4869 : : {
4870 : 0 : skip_after_next_block ();
4871 : 0 : return nullptr;
4872 : : }
4873 : 107 : Identifier union_name{union_name_tok};
4874 : :
4875 : : // parse optional generic parameters
4876 : 107 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
4877 : : = parse_generic_params_in_angles ();
4878 : :
4879 : : // parse optional where clause
4880 : 107 : AST::WhereClause where_clause = parse_where_clause ();
4881 : :
4882 : 107 : if (!skip_token (LEFT_CURLY))
4883 : : {
4884 : 0 : skip_after_end_block ();
4885 : 0 : return nullptr;
4886 : : }
4887 : :
4888 : : /* parse union inner items as "struct fields" because hey, syntax reuse.
4889 : : * Spec said so. */
4890 : 107 : std::vector<AST::StructField> union_fields
4891 : : = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
4892 : :
4893 : 107 : if (!skip_token (RIGHT_CURLY))
4894 : : {
4895 : : // skip after somewhere
4896 : 0 : return nullptr;
4897 : : }
4898 : :
4899 : : return std::unique_ptr<AST::Union> (
4900 : 107 : new AST::Union (std::move (union_name), std::move (vis),
4901 : : std::move (generic_params), std::move (where_clause),
4902 : 107 : std::move (union_fields), std::move (outer_attrs), locus));
4903 : 321 : }
4904 : :
4905 : : /* Parses a "constant item" (compile-time constant to maybe "inline"
4906 : : * throughout the program - like constexpr). */
4907 : : template <typename ManagedTokenSource>
4908 : : std::unique_ptr<AST::ConstantItem>
4909 : 506 : Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
4910 : : AST::AttrVec outer_attrs)
4911 : : {
4912 : 506 : location_t locus = lexer.peek_token ()->get_locus ();
4913 : 506 : skip_token (CONST);
4914 : :
4915 : : /* get constant identifier - this is either a proper identifier or the _
4916 : : * wildcard */
4917 : 506 : const_TokenPtr ident_tok = lexer.peek_token ();
4918 : : // make default identifier the underscore wildcard one
4919 : 506 : std::string ident (Values::Keywords::UNDERSCORE);
4920 : 506 : switch (ident_tok->get_id ())
4921 : : {
4922 : 506 : case IDENTIFIER:
4923 : 506 : ident = ident_tok->get_str ();
4924 : 506 : lexer.skip_token ();
4925 : : break;
4926 : 0 : case UNDERSCORE:
4927 : : // do nothing - identifier is already "_"
4928 : 0 : lexer.skip_token ();
4929 : : break;
4930 : 0 : default:
4931 : 0 : add_error (
4932 : 0 : Error (ident_tok->get_locus (),
4933 : : "expected item name (identifier or %<_%>) in constant item "
4934 : : "declaration - found %qs",
4935 : : ident_tok->get_token_description ()));
4936 : :
4937 : 0 : skip_after_semicolon ();
4938 : 0 : return nullptr;
4939 : : }
4940 : :
4941 : 506 : if (!skip_token (COLON))
4942 : : {
4943 : 0 : skip_after_semicolon ();
4944 : 0 : return nullptr;
4945 : : }
4946 : :
4947 : : // parse constant type (required)
4948 : 506 : std::unique_ptr<AST::Type> type = parse_type ();
4949 : :
4950 : : // A const with no given expression value
4951 : 1012 : if (lexer.peek_token ()->get_id () == SEMICOLON)
4952 : : {
4953 : 4 : lexer.skip_token ();
4954 : : return std::unique_ptr<AST::ConstantItem> (
4955 : 8 : new AST::ConstantItem (std::move (ident), std::move (vis),
4956 : : std::move (type), std::move (outer_attrs),
4957 : 4 : locus));
4958 : : }
4959 : :
4960 : 502 : if (!skip_token (EQUAL))
4961 : : {
4962 : 0 : skip_after_semicolon ();
4963 : 0 : return nullptr;
4964 : : }
4965 : :
4966 : : // parse constant expression (required)
4967 : 502 : std::unique_ptr<AST::Expr> expr = parse_expr ();
4968 : :
4969 : 502 : if (!skip_token (SEMICOLON))
4970 : : {
4971 : : // skip somewhere?
4972 : 0 : return nullptr;
4973 : : }
4974 : :
4975 : : return std::unique_ptr<AST::ConstantItem> (
4976 : 1004 : new AST::ConstantItem (std::move (ident), std::move (vis), std::move (type),
4977 : 502 : std::move (expr), std::move (outer_attrs), locus));
4978 : 1012 : }
4979 : :
4980 : : // Parses a "static item" (static storage item, with 'static lifetime).
4981 : : template <typename ManagedTokenSource>
4982 : : std::unique_ptr<AST::StaticItem>
4983 : 58 : Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis,
4984 : : AST::AttrVec outer_attrs)
4985 : : {
4986 : 58 : location_t locus = lexer.peek_token ()->get_locus ();
4987 : 58 : skip_token (STATIC_KW);
4988 : :
4989 : : // determine whether static item is mutable
4990 : 58 : bool is_mut = false;
4991 : 116 : if (lexer.peek_token ()->get_id () == MUT)
4992 : : {
4993 : 4 : is_mut = true;
4994 : 4 : lexer.skip_token ();
4995 : : }
4996 : :
4997 : 58 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
4998 : 58 : if (ident_tok == nullptr)
4999 : 0 : return nullptr;
5000 : :
5001 : 58 : Identifier ident{ident_tok};
5002 : :
5003 : 58 : if (!skip_token (COLON))
5004 : : {
5005 : 1 : skip_after_semicolon ();
5006 : 1 : return nullptr;
5007 : : }
5008 : :
5009 : : // parse static item type (required)
5010 : 57 : std::unique_ptr<AST::Type> type = parse_type ();
5011 : :
5012 : 57 : if (!skip_token (EQUAL))
5013 : : {
5014 : 0 : skip_after_semicolon ();
5015 : 0 : return nullptr;
5016 : : }
5017 : :
5018 : : // parse static item expression (required)
5019 : 57 : std::unique_ptr<AST::Expr> expr = parse_expr ();
5020 : :
5021 : 57 : if (!skip_token (SEMICOLON))
5022 : : {
5023 : : // skip after somewhere
5024 : 0 : return nullptr;
5025 : : }
5026 : :
5027 : : return std::unique_ptr<AST::StaticItem> (
5028 : 57 : new AST::StaticItem (std::move (ident), is_mut, std::move (type),
5029 : : std::move (expr), std::move (vis),
5030 : 57 : std::move (outer_attrs), locus));
5031 : 115 : }
5032 : :
5033 : : // Parses a trait definition item, including unsafe ones.
5034 : : template <typename ManagedTokenSource>
5035 : : std::unique_ptr<AST::Trait>
5036 : 3668 : Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
5037 : : AST::AttrVec outer_attrs)
5038 : : {
5039 : 3668 : location_t locus = lexer.peek_token ()->get_locus ();
5040 : 3668 : bool is_unsafe = false;
5041 : 3668 : bool is_auto_trait = false;
5042 : :
5043 : 7336 : if (lexer.peek_token ()->get_id () == UNSAFE)
5044 : : {
5045 : 56 : is_unsafe = true;
5046 : 56 : lexer.skip_token ();
5047 : : }
5048 : :
5049 : 7336 : if (lexer.peek_token ()->get_id () == AUTO)
5050 : : {
5051 : 20 : is_auto_trait = true;
5052 : 20 : lexer.skip_token ();
5053 : : }
5054 : :
5055 : 3668 : skip_token (TRAIT);
5056 : :
5057 : : // parse trait name
5058 : 3668 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5059 : 3668 : if (ident_tok == nullptr)
5060 : 0 : return nullptr;
5061 : :
5062 : 3668 : Identifier ident{ident_tok};
5063 : :
5064 : : // parse generic parameters (if they exist)
5065 : 3668 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5066 : : = parse_generic_params_in_angles ();
5067 : :
5068 : : // create placeholder type param bounds in case they don't exist
5069 : 3668 : std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
5070 : :
5071 : : // parse type param bounds (if they exist)
5072 : 7336 : if (lexer.peek_token ()->get_id () == COLON)
5073 : : {
5074 : 534 : lexer.skip_token ();
5075 : :
5076 : 534 : type_param_bounds = parse_type_param_bounds (
5077 : 72 : [] (TokenId id) { return id == WHERE || id == LEFT_CURLY; });
5078 : : // type_param_bounds = parse_type_param_bounds ();
5079 : : }
5080 : :
5081 : : // parse where clause (if it exists)
5082 : 3668 : AST::WhereClause where_clause = parse_where_clause ();
5083 : :
5084 : 3668 : if (!skip_token (LEFT_CURLY))
5085 : : {
5086 : 0 : skip_after_end_block ();
5087 : 0 : return nullptr;
5088 : : }
5089 : :
5090 : : // parse inner attrs (if they exist)
5091 : 3668 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5092 : :
5093 : : // parse trait items
5094 : 3668 : std::vector<std::unique_ptr<AST::AssociatedItem>> trait_items;
5095 : :
5096 : 3668 : const_TokenPtr t = lexer.peek_token ();
5097 : 6992 : while (t->get_id () != RIGHT_CURLY)
5098 : : {
5099 : 3324 : std::unique_ptr<AST::AssociatedItem> trait_item = parse_trait_item ();
5100 : :
5101 : 3324 : if (trait_item == nullptr)
5102 : : {
5103 : 0 : Error error (lexer.peek_token ()->get_locus (),
5104 : : "failed to parse trait item in trait");
5105 : 0 : add_error (std::move (error));
5106 : :
5107 : 0 : return nullptr;
5108 : 0 : }
5109 : 3324 : trait_items.push_back (std::move (trait_item));
5110 : :
5111 : 3324 : t = lexer.peek_token ();
5112 : : }
5113 : :
5114 : 3668 : if (!skip_token (RIGHT_CURLY))
5115 : : {
5116 : : // skip after something
5117 : 0 : return nullptr;
5118 : : }
5119 : :
5120 : 3668 : trait_items.shrink_to_fit ();
5121 : : return std::unique_ptr<AST::Trait> (
5122 : 3668 : new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
5123 : : std::move (generic_params), std::move (type_param_bounds),
5124 : : std::move (where_clause), std::move (trait_items),
5125 : : std::move (vis), std::move (outer_attrs),
5126 : 3668 : std::move (inner_attrs), locus));
5127 : 7336 : }
5128 : :
5129 : : // Parses a trait item used inside traits (not trait, the Item).
5130 : : template <typename ManagedTokenSource>
5131 : : std::unique_ptr<AST::AssociatedItem>
5132 : 3326 : Parser<ManagedTokenSource>::parse_trait_item ()
5133 : : {
5134 : : // parse outer attributes (if they exist)
5135 : 3326 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5136 : :
5137 : 3326 : AST::Visibility vis = parse_visibility ();
5138 : :
5139 : : // lookahead to determine what type of trait item to parse
5140 : 3326 : const_TokenPtr tok = lexer.peek_token ();
5141 : 3326 : switch (tok->get_id ())
5142 : : {
5143 : 0 : case SUPER:
5144 : : case SELF:
5145 : : case CRATE:
5146 : : case DOLLAR_SIGN:
5147 : : // these seem to be SimplePath tokens, so this is a macro invocation
5148 : : // semi
5149 : 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
5150 : 1 : case IDENTIFIER:
5151 : 2 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
5152 : 0 : return parse_function (std::move (vis), std::move (outer_attrs));
5153 : : else
5154 : 2 : return parse_macro_invocation_semi (std::move (outer_attrs));
5155 : 735 : case TYPE:
5156 : 735 : return parse_trait_type (std::move (outer_attrs), vis);
5157 : 41 : case CONST:
5158 : : // disambiguate with function qualifier
5159 : 82 : if (lexer.peek_token (1)->get_id () == IDENTIFIER)
5160 : : {
5161 : 40 : return parse_trait_const (std::move (outer_attrs));
5162 : : }
5163 : : // else, fallthrough to function
5164 : : // TODO: find out how to disable gcc "implicit fallthrough" error
5165 : : gcc_fallthrough ();
5166 : : case ASYNC:
5167 : : case UNSAFE:
5168 : : case EXTERN_KW:
5169 : : case FN_KW:
5170 : 5100 : return parse_function (std::move (vis), std::move (outer_attrs));
5171 : : default:
5172 : : break;
5173 : : }
5174 : 0 : add_error (Error (tok->get_locus (),
5175 : : "unrecognised token %qs for item in trait",
5176 : : tok->get_token_description ()));
5177 : : // skip?
5178 : 0 : return nullptr;
5179 : 3326 : }
5180 : :
5181 : : // Parse a typedef trait item.
5182 : : template <typename ManagedTokenSource>
5183 : : std::unique_ptr<AST::TraitItemType>
5184 : 735 : Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs,
5185 : : AST::Visibility vis)
5186 : : {
5187 : 735 : location_t locus = lexer.peek_token ()->get_locus ();
5188 : 735 : skip_token (TYPE);
5189 : :
5190 : 735 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5191 : 735 : if (ident_tok == nullptr)
5192 : 0 : return nullptr;
5193 : :
5194 : 1470 : Identifier ident{ident_tok};
5195 : :
5196 : 735 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
5197 : :
5198 : : // parse optional colon
5199 : 1470 : if (lexer.peek_token ()->get_id () == COLON)
5200 : : {
5201 : 37 : lexer.skip_token ();
5202 : :
5203 : : // parse optional type param bounds
5204 : : bounds
5205 : 37 : = parse_type_param_bounds ([] (TokenId id) { return id == SEMICOLON; });
5206 : : // bounds = parse_type_param_bounds ();
5207 : : }
5208 : :
5209 : 735 : if (!skip_token (SEMICOLON))
5210 : : {
5211 : : // skip?
5212 : 0 : return nullptr;
5213 : : }
5214 : :
5215 : : return std::unique_ptr<AST::TraitItemType> (
5216 : 735 : new AST::TraitItemType (std::move (ident), std::move (bounds),
5217 : 735 : std::move (outer_attrs), vis, locus));
5218 : 735 : }
5219 : :
5220 : : // Parses a constant trait item.
5221 : : template <typename ManagedTokenSource>
5222 : : std::unique_ptr<AST::TraitItemConst>
5223 : 40 : Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
5224 : : {
5225 : 40 : location_t locus = lexer.peek_token ()->get_locus ();
5226 : 40 : skip_token (CONST);
5227 : :
5228 : : // parse constant item name
5229 : 40 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5230 : 40 : if (ident_tok == nullptr)
5231 : 0 : return nullptr;
5232 : :
5233 : 40 : Identifier ident{ident_tok};
5234 : :
5235 : 40 : if (!skip_token (COLON))
5236 : : {
5237 : 0 : skip_after_semicolon ();
5238 : 0 : return nullptr;
5239 : : }
5240 : :
5241 : : // parse constant trait item type
5242 : 40 : std::unique_ptr<AST::Type> type = parse_type ();
5243 : :
5244 : : // parse constant trait body expression, if it exists
5245 : 40 : std::unique_ptr<AST::Expr> const_body = nullptr;
5246 : 80 : if (lexer.peek_token ()->get_id () == EQUAL)
5247 : : {
5248 : 12 : lexer.skip_token ();
5249 : :
5250 : : // expression must exist, so parse it
5251 : 12 : const_body = parse_expr ();
5252 : : }
5253 : :
5254 : 40 : if (!skip_token (SEMICOLON))
5255 : : {
5256 : : // skip after something?
5257 : 0 : return nullptr;
5258 : : }
5259 : :
5260 : : return std::unique_ptr<AST::TraitItemConst> (
5261 : 40 : new AST::TraitItemConst (std::move (ident), std::move (type),
5262 : : std::move (const_body), std::move (outer_attrs),
5263 : 40 : locus));
5264 : 80 : }
5265 : :
5266 : : /* Parses a struct "impl" item (both inherent impl and trait impl can be
5267 : : * parsed here), */
5268 : : template <typename ManagedTokenSource>
5269 : : std::unique_ptr<AST::Impl>
5270 : 5241 : Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
5271 : : AST::AttrVec outer_attrs)
5272 : : {
5273 : : /* Note that only trait impls are allowed to be unsafe. So if unsafe, it
5274 : : * must be a trait impl. However, this isn't enough for full disambiguation,
5275 : : * so don't branch here. */
5276 : 5241 : location_t locus = lexer.peek_token ()->get_locus ();
5277 : 5241 : bool is_unsafe = false;
5278 : 10482 : if (lexer.peek_token ()->get_id () == UNSAFE)
5279 : : {
5280 : 67 : lexer.skip_token ();
5281 : 67 : is_unsafe = true;
5282 : : }
5283 : :
5284 : 5241 : if (!skip_token (IMPL))
5285 : : {
5286 : 0 : skip_after_next_block ();
5287 : 0 : return nullptr;
5288 : : }
5289 : :
5290 : : // parse generic params (shared by trait and inherent impls)
5291 : 5241 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5292 : : = parse_generic_params_in_angles ();
5293 : :
5294 : : // Again, trait impl-only feature, but optional one, so can be used for
5295 : : // branching yet.
5296 : 5241 : bool has_exclam = false;
5297 : 10482 : if (lexer.peek_token ()->get_id () == EXCLAM)
5298 : : {
5299 : 6 : lexer.skip_token ();
5300 : 6 : has_exclam = true;
5301 : : }
5302 : :
5303 : : /* FIXME: code that doesn't look shit for TypePath. Also, make sure this
5304 : : * doesn't parse too much and not work. */
5305 : 5241 : AST::TypePath type_path = parse_type_path ();
5306 : 10281 : if (type_path.is_error () || lexer.peek_token ()->get_id () != FOR)
5307 : : {
5308 : : /* cannot parse type path (or not for token next, at least), so must be
5309 : : * inherent impl */
5310 : :
5311 : : // hacky conversion of TypePath stack object to Type pointer
5312 : 959 : std::unique_ptr<AST::Type> type = nullptr;
5313 : 959 : if (!type_path.is_error ())
5314 : 758 : type = std::unique_ptr<AST::TypePath> (
5315 : 758 : new AST::TypePath (std::move (type_path)));
5316 : : else
5317 : 201 : type = parse_type ();
5318 : :
5319 : : // Type is required, so error if null
5320 : 959 : if (type == nullptr)
5321 : : {
5322 : 1 : Error error (lexer.peek_token ()->get_locus (),
5323 : : "could not parse type in inherent impl");
5324 : 1 : add_error (std::move (error));
5325 : :
5326 : 1 : skip_after_next_block ();
5327 : 1 : return nullptr;
5328 : 1 : }
5329 : :
5330 : : // parse optional where clause
5331 : 958 : AST::WhereClause where_clause = parse_where_clause ();
5332 : :
5333 : 958 : if (!skip_token (LEFT_CURLY))
5334 : : {
5335 : : // TODO: does this still skip properly?
5336 : 0 : skip_after_end_block ();
5337 : 0 : return nullptr;
5338 : : }
5339 : :
5340 : : // parse inner attributes (optional)
5341 : 958 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5342 : :
5343 : : // parse inherent impl items
5344 : 958 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
5345 : :
5346 : 958 : const_TokenPtr t = lexer.peek_token ();
5347 : 3705 : while (t->get_id () != RIGHT_CURLY)
5348 : : {
5349 : 2757 : std::unique_ptr<AST::AssociatedItem> impl_item
5350 : : = parse_inherent_impl_item ();
5351 : :
5352 : 2757 : if (impl_item == nullptr)
5353 : : {
5354 : 10 : Error error (
5355 : 10 : lexer.peek_token ()->get_locus (),
5356 : : "failed to parse inherent impl item in inherent impl");
5357 : 10 : add_error (std::move (error));
5358 : :
5359 : 10 : return nullptr;
5360 : 10 : }
5361 : :
5362 : 2747 : impl_items.push_back (std::move (impl_item));
5363 : :
5364 : 2747 : t = lexer.peek_token ();
5365 : : }
5366 : :
5367 : 948 : if (!skip_token (RIGHT_CURLY))
5368 : : {
5369 : : // skip somewhere
5370 : 0 : return nullptr;
5371 : : }
5372 : :
5373 : : // DEBUG
5374 : 948 : rust_debug ("successfully parsed inherent impl");
5375 : :
5376 : 948 : impl_items.shrink_to_fit ();
5377 : :
5378 : 948 : return std::unique_ptr<AST::InherentImpl> (new AST::InherentImpl (
5379 : : std::move (impl_items), std::move (generic_params), std::move (type),
5380 : : std::move (where_clause), std::move (vis), std::move (inner_attrs),
5381 : 948 : std::move (outer_attrs), locus));
5382 : 1917 : }
5383 : : else
5384 : : {
5385 : : // type path must both be valid and next token is for, so trait impl
5386 : 4282 : if (!skip_token (FOR))
5387 : : {
5388 : 0 : skip_after_next_block ();
5389 : 0 : return nullptr;
5390 : : }
5391 : :
5392 : : // parse type
5393 : 4282 : std::unique_ptr<AST::Type> type = parse_type ();
5394 : : // ensure type is included as it is required
5395 : 4282 : if (type == nullptr)
5396 : : {
5397 : 0 : Error error (lexer.peek_token ()->get_locus (),
5398 : : "could not parse type in trait impl");
5399 : 0 : add_error (std::move (error));
5400 : :
5401 : 0 : skip_after_next_block ();
5402 : 0 : return nullptr;
5403 : 0 : }
5404 : :
5405 : : // parse optional where clause
5406 : 4282 : AST::WhereClause where_clause = parse_where_clause ();
5407 : :
5408 : 4282 : if (!skip_token (LEFT_CURLY))
5409 : : {
5410 : : // TODO: does this still skip properly?
5411 : 0 : skip_after_end_block ();
5412 : 0 : return nullptr;
5413 : : }
5414 : :
5415 : : // parse inner attributes (optional)
5416 : 4282 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5417 : :
5418 : : // parse trait impl items
5419 : 4282 : std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
5420 : :
5421 : 4282 : const_TokenPtr t = lexer.peek_token ();
5422 : 14520 : while (t->get_id () != RIGHT_CURLY)
5423 : : {
5424 : 5119 : std::unique_ptr<AST::AssociatedItem> impl_item
5425 : : = parse_trait_impl_item ();
5426 : :
5427 : 5119 : if (impl_item == nullptr)
5428 : : {
5429 : 0 : Error error (lexer.peek_token ()->get_locus (),
5430 : : "failed to parse trait impl item in trait impl");
5431 : 0 : add_error (std::move (error));
5432 : :
5433 : 0 : return nullptr;
5434 : 0 : }
5435 : :
5436 : 5119 : impl_items.push_back (std::move (impl_item));
5437 : :
5438 : 5119 : t = lexer.peek_token ();
5439 : :
5440 : : // DEBUG
5441 : 5119 : rust_debug ("successfully parsed a trait impl item");
5442 : : }
5443 : : // DEBUG
5444 : 4282 : rust_debug ("successfully finished trait impl items");
5445 : :
5446 : 4282 : if (!skip_token (RIGHT_CURLY))
5447 : : {
5448 : : // skip somewhere
5449 : 0 : return nullptr;
5450 : : }
5451 : :
5452 : : // DEBUG
5453 : 4282 : rust_debug ("successfully parsed trait impl");
5454 : :
5455 : 4282 : impl_items.shrink_to_fit ();
5456 : :
5457 : 4282 : return std::unique_ptr<AST::TraitImpl> (
5458 : 8564 : new AST::TraitImpl (std::move (type_path), is_unsafe, has_exclam,
5459 : : std::move (impl_items), std::move (generic_params),
5460 : : std::move (type), std::move (where_clause),
5461 : : std::move (vis), std::move (inner_attrs),
5462 : 4282 : std::move (outer_attrs), locus));
5463 : 8564 : }
5464 : 5241 : }
5465 : :
5466 : : // Parses a single inherent impl item (item inside an inherent impl block).
5467 : : template <typename ManagedTokenSource>
5468 : : std::unique_ptr<AST::AssociatedItem>
5469 : 2759 : Parser<ManagedTokenSource>::parse_inherent_impl_item ()
5470 : : {
5471 : : // parse outer attributes (if they exist)
5472 : 2759 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5473 : :
5474 : : // TODO: cleanup - currently an unreadable mess
5475 : :
5476 : : // branch on next token:
5477 : 2759 : const_TokenPtr t = lexer.peek_token ();
5478 : 2759 : switch (t->get_id ())
5479 : : {
5480 : 1 : case IDENTIFIER:
5481 : : // FIXME: Arthur: Do we need to some lookahead here?
5482 : 2 : return parse_macro_invocation_semi (outer_attrs);
5483 : 2139 : case SUPER:
5484 : : case SELF:
5485 : : case CRATE:
5486 : : case PUB:
5487 : : {
5488 : : // visibility, so not a macro invocation semi - must be constant,
5489 : : // function, or method
5490 : 2139 : AST::Visibility vis = parse_visibility ();
5491 : :
5492 : : // TODO: is a recursive call to parse_inherent_impl_item better?
5493 : 4278 : switch (lexer.peek_token ()->get_id ())
5494 : : {
5495 : 1362 : case EXTERN_KW:
5496 : : case UNSAFE:
5497 : : case FN_KW:
5498 : : // function or method
5499 : 2724 : return parse_inherent_impl_function_or_method (std::move (vis),
5500 : : std::move (
5501 : 1362 : outer_attrs));
5502 : 777 : case CONST:
5503 : : // lookahead to resolve production - could be function/method or
5504 : : // const item
5505 : 777 : t = lexer.peek_token (1);
5506 : :
5507 : 777 : switch (t->get_id ())
5508 : : {
5509 : 1 : case IDENTIFIER:
5510 : : case UNDERSCORE:
5511 : 2 : return parse_const_item (std::move (vis),
5512 : 1 : std::move (outer_attrs));
5513 : 776 : case UNSAFE:
5514 : : case EXTERN_KW:
5515 : : case FN_KW:
5516 : 1552 : return parse_inherent_impl_function_or_method (std::move (vis),
5517 : : std::move (
5518 : 776 : outer_attrs));
5519 : 0 : default:
5520 : 0 : add_error (Error (t->get_locus (),
5521 : : "unexpected token %qs in some sort of const "
5522 : : "item in inherent impl",
5523 : : t->get_token_description ()));
5524 : :
5525 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5526 : 0 : return nullptr;
5527 : : }
5528 : 0 : default:
5529 : 0 : add_error (
5530 : 0 : Error (t->get_locus (),
5531 : : "unrecognised token %qs for item in inherent impl",
5532 : : t->get_token_description ()));
5533 : : // skip?
5534 : 0 : return nullptr;
5535 : : }
5536 : 2139 : }
5537 : 607 : case ASYNC:
5538 : : case EXTERN_KW:
5539 : : case UNSAFE:
5540 : : case FN_KW:
5541 : : // function or method
5542 : 607 : return parse_inherent_impl_function_or_method (
5543 : 607 : AST::Visibility::create_private (), std::move (outer_attrs));
5544 : 12 : case CONST:
5545 : : /* lookahead to resolve production - could be function/method or const
5546 : : * item */
5547 : 12 : t = lexer.peek_token (1);
5548 : :
5549 : 12 : switch (t->get_id ())
5550 : : {
5551 : 12 : case IDENTIFIER:
5552 : : case UNDERSCORE:
5553 : 24 : return parse_const_item (AST::Visibility::create_private (),
5554 : 12 : std::move (outer_attrs));
5555 : 0 : case UNSAFE:
5556 : : case EXTERN_KW:
5557 : : case FN_KW:
5558 : 0 : return parse_inherent_impl_function_or_method (
5559 : 0 : AST::Visibility::create_private (), std::move (outer_attrs));
5560 : 0 : default:
5561 : 0 : add_error (Error (t->get_locus (),
5562 : : "unexpected token %qs in some sort of const item "
5563 : : "in inherent impl",
5564 : : t->get_token_description ()));
5565 : :
5566 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5567 : 0 : return nullptr;
5568 : : }
5569 : : rust_unreachable ();
5570 : 0 : default:
5571 : 0 : add_error (Error (t->get_locus (),
5572 : : "unrecognised token %qs for item in inherent impl",
5573 : : t->get_token_description ()));
5574 : :
5575 : : // skip?
5576 : 0 : return nullptr;
5577 : : }
5578 : 2759 : }
5579 : :
5580 : : /* For internal use only by parse_inherent_impl_item() - splits giant method
5581 : : * into smaller ones and prevents duplication of logic. Strictly, this parses
5582 : : * a function or method item inside an inherent impl item block. */
5583 : : // TODO: make this a templated function with "return type" as type param -
5584 : : // InherentImplItem is this specialisation of the template while TraitImplItem
5585 : : // will be the other.
5586 : : template <typename ManagedTokenSource>
5587 : : std::unique_ptr<AST::AssociatedItem>
5588 : 2745 : Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
5589 : : AST::Visibility vis, AST::AttrVec outer_attrs)
5590 : : {
5591 : 2745 : location_t locus = lexer.peek_token ()->get_locus ();
5592 : : // parse function or method qualifiers
5593 : 2745 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
5594 : :
5595 : 2745 : skip_token (FN_KW);
5596 : :
5597 : : // parse function or method name
5598 : 2745 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5599 : 2745 : if (ident_tok == nullptr)
5600 : 7 : return nullptr;
5601 : :
5602 : 2738 : Identifier ident{ident_tok};
5603 : :
5604 : : // parse generic params
5605 : 2738 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5606 : : = parse_generic_params_in_angles ();
5607 : :
5608 : 2738 : if (!skip_token (LEFT_PAREN))
5609 : : {
5610 : : // skip after somewhere?
5611 : 0 : return nullptr;
5612 : : }
5613 : :
5614 : : // now for function vs method disambiguation - method has opening "self"
5615 : : // param
5616 : 2738 : auto initial_param = parse_self_param ();
5617 : :
5618 : 2738 : if (!initial_param.has_value ()
5619 : 2738 : && initial_param.error () != ParseSelfError::NOT_SELF)
5620 : 3 : return nullptr;
5621 : :
5622 : : /* FIXME: ensure that self param doesn't accidently consume tokens for a
5623 : : * function one idea is to lookahead up to 4 tokens to see whether self is
5624 : : * one of them */
5625 : 2735 : bool is_method = false;
5626 : 2735 : if (initial_param.has_value ())
5627 : : {
5628 : 1844 : if ((*initial_param)->is_self ())
5629 : : is_method = true;
5630 : :
5631 : : /* skip comma so function and method regular params can be parsed in
5632 : : * same way */
5633 : 3688 : if (lexer.peek_token ()->get_id () == COMMA)
5634 : 1193 : lexer.skip_token ();
5635 : : }
5636 : :
5637 : : // parse trait function params
5638 : 2735 : std::vector<std::unique_ptr<AST::Param>> function_params
5639 : : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
5640 : :
5641 : 2735 : if (initial_param.has_value ())
5642 : 1844 : function_params.insert (function_params.begin (),
5643 : 1844 : std::move (*initial_param));
5644 : :
5645 : 2735 : if (!skip_token (RIGHT_PAREN))
5646 : : {
5647 : 0 : skip_after_end_block ();
5648 : 0 : return nullptr;
5649 : : }
5650 : :
5651 : : // parse return type (optional)
5652 : 2735 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
5653 : :
5654 : : // parse where clause (optional)
5655 : 2735 : AST::WhereClause where_clause = parse_where_clause ();
5656 : :
5657 : 2735 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
5658 : 5470 : if (lexer.peek_token ()->get_id () == SEMICOLON)
5659 : 2 : lexer.skip_token ();
5660 : : else
5661 : : {
5662 : 2733 : auto result = parse_block_expr ();
5663 : :
5664 : 2733 : if (result == nullptr)
5665 : : {
5666 : 0 : Error error (
5667 : 0 : lexer.peek_token ()->get_locus (),
5668 : : "could not parse definition in inherent impl %s definition",
5669 : : is_method ? "method" : "function");
5670 : 0 : add_error (std::move (error));
5671 : :
5672 : 0 : skip_after_end_block ();
5673 : 0 : return nullptr;
5674 : 0 : }
5675 : 2733 : body = std::move (result);
5676 : 2733 : }
5677 : :
5678 : 2735 : return std::unique_ptr<AST::Function> (
5679 : 10938 : new AST::Function (std::move (ident), std::move (qualifiers),
5680 : : std::move (generic_params), std::move (function_params),
5681 : : std::move (return_type), std::move (where_clause),
5682 : : std::move (body), std::move (vis),
5683 : 2735 : std::move (outer_attrs), locus));
5684 : 8218 : }
5685 : :
5686 : : // Parses a single trait impl item (item inside a trait impl block).
5687 : : template <typename ManagedTokenSource>
5688 : : std::unique_ptr<AST::AssociatedItem>
5689 : 5222 : Parser<ManagedTokenSource>::parse_trait_impl_item ()
5690 : : {
5691 : : // parse outer attributes (if they exist)
5692 : 5222 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5693 : :
5694 : 5222 : auto visibility = AST::Visibility::create_private ();
5695 : 10444 : if (lexer.peek_token ()->get_id () == PUB)
5696 : 0 : visibility = parse_visibility ();
5697 : :
5698 : : // branch on next token:
5699 : 5222 : const_TokenPtr t = lexer.peek_token ();
5700 : 5222 : switch (t->get_id ())
5701 : : {
5702 : 0 : case SUPER:
5703 : : case SELF:
5704 : : case CRATE:
5705 : : case DOLLAR_SIGN:
5706 : : // these seem to be SimplePath tokens, so this is a macro invocation
5707 : : // semi
5708 : 0 : return parse_macro_invocation_semi (std::move (outer_attrs));
5709 : 55 : case IDENTIFIER:
5710 : 110 : if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
5711 : 48 : return parse_trait_impl_function_or_method (visibility,
5712 : 24 : std::move (outer_attrs));
5713 : : else
5714 : 62 : return parse_macro_invocation_semi (std::move (outer_attrs));
5715 : 1228 : case TYPE:
5716 : 2456 : return parse_type_alias (visibility, std::move (outer_attrs));
5717 : 3905 : case EXTERN_KW:
5718 : : case UNSAFE:
5719 : : case FN_KW:
5720 : : // function or method
5721 : 7810 : return parse_trait_impl_function_or_method (visibility,
5722 : 3905 : std::move (outer_attrs));
5723 : 1 : case ASYNC:
5724 : 2 : return parse_async_item (visibility, std::move (outer_attrs));
5725 : 33 : case CONST:
5726 : : // lookahead to resolve production - could be function/method or const
5727 : : // item
5728 : 33 : t = lexer.peek_token (1);
5729 : :
5730 : 33 : switch (t->get_id ())
5731 : : {
5732 : 32 : case IDENTIFIER:
5733 : : case UNDERSCORE:
5734 : 64 : return parse_const_item (visibility, std::move (outer_attrs));
5735 : 1 : case UNSAFE:
5736 : : case EXTERN_KW:
5737 : : case FN_KW:
5738 : 2 : return parse_trait_impl_function_or_method (visibility,
5739 : 1 : std::move (outer_attrs));
5740 : 0 : default:
5741 : 0 : add_error (Error (
5742 : : t->get_locus (),
5743 : : "unexpected token %qs in some sort of const item in trait impl",
5744 : : t->get_token_description ()));
5745 : :
5746 : 0 : lexer.skip_token (1); // TODO: is this right thing to do?
5747 : 0 : return nullptr;
5748 : : }
5749 : : rust_unreachable ();
5750 : : default:
5751 : : break;
5752 : : }
5753 : 0 : add_error (Error (t->get_locus (),
5754 : : "unrecognised token %qs for item in trait impl",
5755 : : t->get_token_description ()));
5756 : :
5757 : : // skip?
5758 : 0 : return nullptr;
5759 : 5222 : }
5760 : :
5761 : : /* For internal use only by parse_trait_impl_item() - splits giant method into
5762 : : * smaller ones and prevents duplication of logic. Strictly, this parses a
5763 : : * function or method item inside a trait impl item block. */
5764 : : template <typename ManagedTokenSource>
5765 : : std::unique_ptr<AST::AssociatedItem>
5766 : 3930 : Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
5767 : : AST::Visibility vis, AST::AttrVec outer_attrs)
5768 : : {
5769 : : // this shares virtually all logic with
5770 : : // parse_inherent_impl_function_or_method
5771 : : // - template?
5772 : 3930 : location_t locus = lexer.peek_token ()->get_locus ();
5773 : :
5774 : 3930 : auto is_default = false;
5775 : 3930 : auto t = lexer.peek_token ();
5776 : 3930 : if (t->get_id () == IDENTIFIER
5777 : 3930 : && t->get_str () == Values::WeakKeywords::DEFAULT)
5778 : : {
5779 : 24 : is_default = true;
5780 : 24 : lexer.skip_token ();
5781 : : }
5782 : :
5783 : : // parse function or method qualifiers
5784 : 3930 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
5785 : :
5786 : 3930 : skip_token (FN_KW);
5787 : :
5788 : : // parse function or method name
5789 : 3930 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
5790 : 3930 : if (ident_tok == nullptr)
5791 : : {
5792 : 0 : return nullptr;
5793 : : }
5794 : 3930 : Identifier ident{ident_tok};
5795 : :
5796 : : // DEBUG:
5797 : 3930 : rust_debug (
5798 : : "about to start parsing generic params in trait impl function or method");
5799 : :
5800 : : // parse generic params
5801 : 3930 : std::vector<std::unique_ptr<AST::GenericParam>> generic_params
5802 : : = parse_generic_params_in_angles ();
5803 : :
5804 : : // DEBUG:
5805 : 3930 : rust_debug (
5806 : : "finished parsing generic params in trait impl function or method");
5807 : :
5808 : 3930 : if (!skip_token (LEFT_PAREN))
5809 : : {
5810 : : // skip after somewhere?
5811 : 0 : return nullptr;
5812 : : }
5813 : :
5814 : : // now for function vs method disambiguation - method has opening "self"
5815 : : // param
5816 : 3930 : auto initial_param = parse_self_param ();
5817 : :
5818 : 3930 : if (!initial_param.has_value ()
5819 : 3930 : && initial_param.error () != ParseSelfError::NOT_SELF)
5820 : 0 : return nullptr;
5821 : :
5822 : : // FIXME: ensure that self param doesn't accidently consume tokens for a
5823 : : // function
5824 : 3930 : bool is_method = false;
5825 : 3930 : if (initial_param.has_value ())
5826 : : {
5827 : 3691 : if ((*initial_param)->is_self ())
5828 : : is_method = true;
5829 : :
5830 : : // skip comma so function and method regular params can be parsed in
5831 : : // same way
5832 : 7382 : if (lexer.peek_token ()->get_id () == COMMA)
5833 : : {
5834 : 1992 : lexer.skip_token ();
5835 : : }
5836 : :
5837 : : // DEBUG
5838 : 3691 : rust_debug ("successfully parsed self param in method trait impl item");
5839 : : }
5840 : :
5841 : : // DEBUG
5842 : 3930 : rust_debug (
5843 : : "started to parse function params in function or method trait impl item");
5844 : :
5845 : : // parse trait function params (only if next token isn't right paren)
5846 : 3930 : std::vector<std::unique_ptr<AST::Param>> function_params;
5847 : 7860 : if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
5848 : : {
5849 : : function_params
5850 : 2148 : = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
5851 : :
5852 : 2148 : if (function_params.empty ())
5853 : : {
5854 : 0 : Error error (
5855 : 0 : lexer.peek_token ()->get_locus (),
5856 : : "failed to parse function params in trait impl %s definition",
5857 : : is_method ? "method" : "function");
5858 : 0 : add_error (std::move (error));
5859 : :
5860 : 0 : skip_after_next_block ();
5861 : 0 : return nullptr;
5862 : 0 : }
5863 : : }
5864 : :
5865 : 3930 : if (initial_param.has_value ())
5866 : 3691 : function_params.insert (function_params.begin (),
5867 : 3691 : std::move (*initial_param));
5868 : :
5869 : : // DEBUG
5870 : 3930 : rust_debug ("successfully parsed function params in function or method "
5871 : : "trait impl item");
5872 : :
5873 : 3930 : if (!skip_token (RIGHT_PAREN))
5874 : : {
5875 : 0 : skip_after_next_block ();
5876 : 0 : return nullptr;
5877 : : }
5878 : :
5879 : : // parse return type (optional)
5880 : 3930 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
5881 : :
5882 : : // DEBUG
5883 : 3930 : rust_debug (
5884 : : "successfully parsed return type in function or method trait impl item");
5885 : :
5886 : : // parse where clause (optional)
5887 : 3930 : AST::WhereClause where_clause = parse_where_clause ();
5888 : :
5889 : : // DEBUG
5890 : 3930 : rust_debug (
5891 : : "successfully parsed where clause in function or method trait impl item");
5892 : :
5893 : : // parse function definition (in block) - semicolon not allowed
5894 : 3930 : tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
5895 : :
5896 : 7860 : if (lexer.peek_token ()->get_id () == SEMICOLON)
5897 : 1 : lexer.skip_token ();
5898 : : else
5899 : : {
5900 : 3929 : auto result = parse_block_expr ();
5901 : 3929 : if (result == nullptr)
5902 : : {
5903 : 0 : Error error (lexer.peek_token ()->get_locus (),
5904 : : "could not parse definition in trait impl %s definition",
5905 : : is_method ? "method" : "function");
5906 : 0 : add_error (std::move (error));
5907 : :
5908 : 0 : skip_after_end_block ();
5909 : 0 : return nullptr;
5910 : 0 : }
5911 : 3929 : body = std::move (result);
5912 : 3929 : }
5913 : :
5914 : 3930 : return std::unique_ptr<AST::Function> (
5915 : 15719 : new AST::Function (std::move (ident), std::move (qualifiers),
5916 : : std::move (generic_params), std::move (function_params),
5917 : : std::move (return_type), std::move (where_clause),
5918 : : std::move (body), std::move (vis),
5919 : 3930 : std::move (outer_attrs), locus, is_default));
5920 : 11790 : }
5921 : :
5922 : : // Parses an extern block of declarations.
5923 : : template <typename ManagedTokenSource>
5924 : : std::unique_ptr<AST::ExternBlock>
5925 : 1437 : Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
5926 : : AST::AttrVec outer_attrs)
5927 : : {
5928 : 1437 : location_t locus = lexer.peek_token ()->get_locus ();
5929 : 1437 : skip_token (EXTERN_KW);
5930 : :
5931 : : // detect optional abi name
5932 : 1437 : std::string abi;
5933 : 1437 : const_TokenPtr next_tok = lexer.peek_token ();
5934 : 1437 : if (next_tok->get_id () == STRING_LITERAL)
5935 : : {
5936 : 1437 : lexer.skip_token ();
5937 : 1437 : abi = next_tok->get_str ();
5938 : : }
5939 : :
5940 : 1437 : if (!skip_token (LEFT_CURLY))
5941 : : {
5942 : 0 : skip_after_end_block ();
5943 : 0 : return nullptr;
5944 : : }
5945 : :
5946 : 1437 : AST::AttrVec inner_attrs = parse_inner_attributes ();
5947 : :
5948 : : // parse declarations inside extern block
5949 : 1437 : std::vector<std::unique_ptr<AST::ExternalItem>> extern_items;
5950 : :
5951 : 1437 : const_TokenPtr t = lexer.peek_token ();
5952 : 3616 : while (t->get_id () != RIGHT_CURLY)
5953 : : {
5954 : 2180 : std::unique_ptr<AST::ExternalItem> extern_item = parse_external_item ();
5955 : :
5956 : 2180 : if (extern_item == nullptr)
5957 : : {
5958 : 1 : Error error (t->get_locus (),
5959 : : "failed to parse external item despite not reaching "
5960 : : "end of extern block");
5961 : 1 : add_error (std::move (error));
5962 : :
5963 : 1 : return nullptr;
5964 : 1 : }
5965 : :
5966 : 2179 : extern_items.push_back (std::move (extern_item));
5967 : :
5968 : 2179 : t = lexer.peek_token ();
5969 : : }
5970 : :
5971 : 1436 : if (!skip_token (RIGHT_CURLY))
5972 : : {
5973 : : // skip somewhere
5974 : 0 : return nullptr;
5975 : : }
5976 : :
5977 : 1436 : extern_items.shrink_to_fit ();
5978 : :
5979 : : return std::unique_ptr<AST::ExternBlock> (
5980 : 1436 : new AST::ExternBlock (std::move (abi), std::move (extern_items),
5981 : : std::move (vis), std::move (inner_attrs),
5982 : 1436 : std::move (outer_attrs), locus));
5983 : 2874 : }
5984 : :
5985 : : // Parses a single extern block item (static or function declaration).
5986 : : template <typename ManagedTokenSource>
5987 : : std::unique_ptr<AST::ExternalItem>
5988 : 2183 : Parser<ManagedTokenSource>::parse_external_item ()
5989 : : {
5990 : : // parse optional outer attributes
5991 : 2183 : AST::AttrVec outer_attrs = parse_outer_attributes ();
5992 : :
5993 : 2183 : location_t locus = lexer.peek_token ()->get_locus ();
5994 : :
5995 : : // parse optional visibility
5996 : 2183 : AST::Visibility vis = parse_visibility ();
5997 : :
5998 : 2183 : const_TokenPtr t = lexer.peek_token ();
5999 : 2183 : switch (t->get_id ())
6000 : : {
6001 : 2 : case IDENTIFIER:
6002 : 4 : return parse_macro_invocation_semi (outer_attrs);
6003 : 1 : case STATIC_KW:
6004 : : {
6005 : : // parse extern static item
6006 : 1 : lexer.skip_token ();
6007 : :
6008 : : // parse mut (optional)
6009 : 1 : bool has_mut = false;
6010 : 2 : if (lexer.peek_token ()->get_id () == MUT)
6011 : : {
6012 : 0 : lexer.skip_token ();
6013 : 0 : has_mut = true;
6014 : : }
6015 : :
6016 : : // parse identifier
6017 : 1 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
6018 : 1 : if (ident_tok == nullptr)
6019 : : {
6020 : 0 : skip_after_semicolon ();
6021 : 0 : return nullptr;
6022 : : }
6023 : 1 : Identifier ident{ident_tok};
6024 : :
6025 : 1 : if (!skip_token (COLON))
6026 : : {
6027 : 0 : skip_after_semicolon ();
6028 : 0 : return nullptr;
6029 : : }
6030 : :
6031 : : // parse type (required)
6032 : 1 : std::unique_ptr<AST::Type> type = parse_type ();
6033 : 1 : if (type == nullptr)
6034 : : {
6035 : 0 : Error error (lexer.peek_token ()->get_locus (),
6036 : : "failed to parse type in external static item");
6037 : 0 : add_error (std::move (error));
6038 : :
6039 : 0 : skip_after_semicolon ();
6040 : 0 : return nullptr;
6041 : 0 : }
6042 : :
6043 : 1 : if (!skip_token (SEMICOLON))
6044 : : {
6045 : : // skip after somewhere?
6046 : 0 : return nullptr;
6047 : : }
6048 : :
6049 : 1 : return std::unique_ptr<AST::ExternalStaticItem> (
6050 : 2 : new AST::ExternalStaticItem (std::move (ident), std::move (type),
6051 : : has_mut, std::move (vis),
6052 : 1 : std::move (outer_attrs), locus));
6053 : 3 : }
6054 : 2176 : case FN_KW:
6055 : 4352 : return parse_function (std::move (vis), std::move (outer_attrs), true);
6056 : :
6057 : 4 : case TYPE:
6058 : 4 : return parse_external_type_item (std::move (vis),
6059 : 4 : std::move (outer_attrs));
6060 : 0 : default:
6061 : : // error
6062 : 0 : add_error (
6063 : 0 : Error (t->get_locus (),
6064 : : "unrecognised token %qs in extern block item declaration",
6065 : : t->get_token_description ()));
6066 : :
6067 : 0 : skip_after_semicolon ();
6068 : 0 : return nullptr;
6069 : : }
6070 : 2183 : }
6071 : :
6072 : : // Parses a statement (will further disambiguate any statement).
6073 : : template <typename ManagedTokenSource>
6074 : : std::unique_ptr<AST::Stmt>
6075 : 737 : Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
6076 : : {
6077 : : // quick exit for empty statement
6078 : : // FIXME: Can we have empty statements without semicolons? Just nothing?
6079 : 737 : const_TokenPtr t = lexer.peek_token ();
6080 : 737 : if (t->get_id () == SEMICOLON)
6081 : : {
6082 : 30 : lexer.skip_token ();
6083 : 30 : return std::unique_ptr<AST::EmptyStmt> (
6084 : 30 : new AST::EmptyStmt (t->get_locus ()));
6085 : : }
6086 : :
6087 : : // parse outer attributes
6088 : 707 : AST::AttrVec outer_attrs = parse_outer_attributes ();
6089 : :
6090 : : // parsing this will be annoying because of the many different possibilities
6091 : : /* best may be just to copy paste in parse_item switch, and failing that try
6092 : : * to parse outer attributes, and then pass them in to either a let
6093 : : * statement or (fallback) expression statement. */
6094 : : // FIXME: think of a way to do this without such a large switch?
6095 : 707 : t = lexer.peek_token ();
6096 : 707 : switch (t->get_id ())
6097 : : {
6098 : 200 : case LET:
6099 : : // let statement
6100 : 200 : return parse_let_stmt (std::move (outer_attrs), restrictions);
6101 : 186 : case PUB:
6102 : : case MOD:
6103 : : case EXTERN_KW:
6104 : : case USE:
6105 : : case FN_KW:
6106 : : case TYPE:
6107 : : case STRUCT_KW:
6108 : : case ENUM_KW:
6109 : : case CONST:
6110 : : case STATIC_KW:
6111 : : case AUTO:
6112 : : case TRAIT:
6113 : : case IMPL:
6114 : : case MACRO:
6115 : : /* TODO: implement union keyword but not really because of
6116 : : * context-dependence crappy hack way to parse a union written below to
6117 : : * separate it from the good code. */
6118 : : // case UNION:
6119 : : case UNSAFE: // maybe - unsafe traits are a thing
6120 : : /* if any of these (should be all possible VisItem prefixes), parse a
6121 : : * VisItem can't parse item because would require reparsing outer
6122 : : * attributes */
6123 : : // may also be unsafe block
6124 : 372 : if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
6125 : : {
6126 : 1 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
6127 : : }
6128 : : else
6129 : : {
6130 : 185 : return parse_vis_item (std::move (outer_attrs));
6131 : : }
6132 : : break;
6133 : : // crappy hack to do union "keyword"
6134 : 216 : case IDENTIFIER:
6135 : 216 : if (t->get_str () == Values::WeakKeywords::UNION
6136 : 216 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
6137 : : {
6138 : 0 : return parse_vis_item (std::move (outer_attrs));
6139 : : // or should this go straight to parsing union?
6140 : : }
6141 : 432 : else if (is_macro_rules_def (t))
6142 : : {
6143 : : // macro_rules! macro item
6144 : 1 : return parse_macro_rules_def (std::move (outer_attrs));
6145 : : }
6146 : : gcc_fallthrough ();
6147 : : // TODO: find out how to disable gcc "implicit fallthrough" warning
6148 : : default:
6149 : : // fallback: expression statement
6150 : 320 : return parse_expr_stmt (std::move (outer_attrs), restrictions);
6151 : : break;
6152 : : }
6153 : 707 : }
6154 : :
6155 : : // Parses a let statement.
6156 : : template <typename ManagedTokenSource>
6157 : : std::unique_ptr<AST::LetStmt>
6158 : 12648 : Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
6159 : : ParseRestrictions restrictions)
6160 : : {
6161 : 12648 : location_t locus = lexer.peek_token ()->get_locus ();
6162 : 12648 : skip_token (LET);
6163 : :
6164 : : // parse pattern (required)
6165 : 12648 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
6166 : 12648 : if (pattern == nullptr)
6167 : : {
6168 : 0 : Error error (lexer.peek_token ()->get_locus (),
6169 : : "failed to parse pattern in let statement");
6170 : 0 : add_error (std::move (error));
6171 : :
6172 : 0 : skip_after_semicolon ();
6173 : 0 : return nullptr;
6174 : 0 : }
6175 : :
6176 : : // parse type declaration (optional)
6177 : 12648 : std::unique_ptr<AST::Type> type = nullptr;
6178 : 25296 : if (lexer.peek_token ()->get_id () == COLON)
6179 : : {
6180 : : // must have a type declaration
6181 : 1987 : lexer.skip_token ();
6182 : :
6183 : 1987 : type = parse_type ();
6184 : 1987 : if (type == nullptr)
6185 : : {
6186 : 0 : Error error (lexer.peek_token ()->get_locus (),
6187 : : "failed to parse type in let statement");
6188 : 0 : add_error (std::move (error));
6189 : :
6190 : 0 : skip_after_semicolon ();
6191 : 0 : return nullptr;
6192 : 0 : }
6193 : : }
6194 : :
6195 : : // parse expression to set variable to (optional)
6196 : 12648 : std::unique_ptr<AST::Expr> expr = nullptr;
6197 : 25296 : if (lexer.peek_token ()->get_id () == EQUAL)
6198 : : {
6199 : : // must have an expression
6200 : 11596 : lexer.skip_token ();
6201 : :
6202 : 11596 : expr = parse_expr ();
6203 : 11596 : if (expr == nullptr)
6204 : : {
6205 : 22 : Error error (lexer.peek_token ()->get_locus (),
6206 : : "failed to parse expression in let statement");
6207 : 22 : add_error (std::move (error));
6208 : :
6209 : 22 : skip_after_semicolon ();
6210 : 22 : return nullptr;
6211 : 22 : }
6212 : : }
6213 : :
6214 : 12626 : tl::optional<std::unique_ptr<AST::Expr>> else_expr = tl::nullopt;
6215 : 12626 : if (maybe_skip_token (ELSE))
6216 : 0 : else_expr = parse_block_expr ();
6217 : :
6218 : 12626 : if (restrictions.consume_semi)
6219 : : {
6220 : : // `stmt` macro variables are parsed without a semicolon, but should be
6221 : : // parsed as a full statement when interpolated. This should be handled
6222 : : // by having the interpolated statement be distinguishable from normal
6223 : : // tokens, e.g. by NT tokens.
6224 : 12481 : if (restrictions.allow_close_after_expr_stmt)
6225 : 55 : maybe_skip_token (SEMICOLON);
6226 : 12426 : else if (!skip_token (SEMICOLON))
6227 : 1 : return nullptr;
6228 : : }
6229 : :
6230 : : return std::unique_ptr<AST::LetStmt> (
6231 : 25250 : new AST::LetStmt (std::move (pattern), std::move (expr), std::move (type),
6232 : 12625 : std::move (else_expr), std::move (outer_attrs), locus));
6233 : 12648 : }
6234 : :
6235 : : // Parses a type path.
6236 : : template <typename ManagedTokenSource>
6237 : : AST::TypePath
6238 : 52422 : Parser<ManagedTokenSource>::parse_type_path ()
6239 : : {
6240 : 52422 : bool has_opening_scope_resolution = false;
6241 : 52422 : location_t locus = lexer.peek_token ()->get_locus ();
6242 : 104844 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
6243 : : {
6244 : 28 : has_opening_scope_resolution = true;
6245 : 28 : lexer.skip_token ();
6246 : : }
6247 : :
6248 : : // create segment vector
6249 : 52422 : std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
6250 : :
6251 : : // parse required initial segment
6252 : 52422 : std::unique_ptr<AST::TypePathSegment> initial_segment
6253 : : = parse_type_path_segment ();
6254 : 52422 : if (initial_segment == nullptr)
6255 : : {
6256 : : // skip after somewhere?
6257 : : // don't necessarily throw error but yeah
6258 : 201 : return AST::TypePath::create_error ();
6259 : : }
6260 : 52221 : segments.push_back (std::move (initial_segment));
6261 : :
6262 : : // parse optional segments (as long as scope resolution operator exists)
6263 : 52221 : const_TokenPtr t = lexer.peek_token ();
6264 : 53409 : while (t->get_id () == SCOPE_RESOLUTION)
6265 : : {
6266 : : // skip scope resolution operator
6267 : 1188 : lexer.skip_token ();
6268 : :
6269 : : // parse the actual segment - it is an error if it doesn't exist now
6270 : 1188 : std::unique_ptr<AST::TypePathSegment> segment
6271 : : = parse_type_path_segment ();
6272 : 1188 : if (segment == nullptr)
6273 : : {
6274 : : // skip after somewhere?
6275 : 0 : Error error (t->get_locus (), "could not parse type path segment");
6276 : 0 : add_error (std::move (error));
6277 : :
6278 : 0 : return AST::TypePath::create_error ();
6279 : 0 : }
6280 : :
6281 : 1188 : segments.push_back (std::move (segment));
6282 : :
6283 : 1188 : t = lexer.peek_token ();
6284 : : }
6285 : :
6286 : 52221 : segments.shrink_to_fit ();
6287 : :
6288 : 104442 : return AST::TypePath (std::move (segments), locus,
6289 : 52221 : has_opening_scope_resolution);
6290 : 52422 : }
6291 : :
6292 : : template <typename ManagedTokenSource>
6293 : : tl::optional<AST::GenericArg>
6294 : 3752 : Parser<ManagedTokenSource>::parse_generic_arg ()
6295 : : {
6296 : 3752 : auto tok = lexer.peek_token ();
6297 : 3752 : std::unique_ptr<AST::Expr> expr = nullptr;
6298 : :
6299 : 3752 : switch (tok->get_id ())
6300 : : {
6301 : 2421 : case IDENTIFIER:
6302 : : {
6303 : : // This is a bit of a weird situation: With an identifier token, we
6304 : : // could either have a valid type or a macro (FIXME: anything else?). So
6305 : : // we need one bit of lookahead to differentiate if this is really
6306 : 2421 : auto next_tok = lexer.peek_token (1);
6307 : 2421 : if (next_tok->get_id () == LEFT_ANGLE
6308 : 2411 : || next_tok->get_id () == SCOPE_RESOLUTION
6309 : 4829 : || next_tok->get_id () == EXCLAM)
6310 : : {
6311 : 21 : auto type = parse_type ();
6312 : 21 : if (type)
6313 : 21 : return AST::GenericArg::create_type (std::move (type));
6314 : : else
6315 : 0 : return tl::nullopt;
6316 : 21 : }
6317 : 2400 : else if (next_tok->get_id () == COLON)
6318 : : {
6319 : 1 : lexer.skip_token (); // skip ident
6320 : 1 : lexer.skip_token (); // skip colon
6321 : :
6322 : 1 : auto tok = lexer.peek_token ();
6323 : 1 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
6324 : : = parse_type_param_bounds ();
6325 : :
6326 : 1 : auto type = std::unique_ptr<AST::TraitObjectType> (
6327 : 1 : new AST::TraitObjectType (std::move (bounds), tok->get_locus (),
6328 : : false));
6329 : 1 : if (type)
6330 : 1 : return AST::GenericArg::create_type (std::move (type));
6331 : : else
6332 : : return tl::nullopt;
6333 : 2 : }
6334 : 2399 : lexer.skip_token ();
6335 : 9596 : return AST::GenericArg::create_ambiguous (tok->get_str (),
6336 : 2399 : tok->get_locus ());
6337 : 2421 : }
6338 : 19 : case LEFT_CURLY:
6339 : 19 : expr = parse_block_expr ();
6340 : 19 : break;
6341 : 58 : case MINUS:
6342 : : case STRING_LITERAL:
6343 : : case CHAR_LITERAL:
6344 : : case INT_LITERAL:
6345 : : case FLOAT_LITERAL:
6346 : : case TRUE_LITERAL:
6347 : : case FALSE_LITERAL:
6348 : 58 : expr = parse_literal_expr ();
6349 : 58 : break;
6350 : : // FIXME: Because of this, error reporting is garbage for const generic
6351 : : // parameter's default values
6352 : 1254 : default:
6353 : : {
6354 : 1254 : auto type = parse_type ();
6355 : : // FIXME: Find a better way to do this?
6356 : 1254 : if (type)
6357 : 1253 : return AST::GenericArg::create_type (std::move (type));
6358 : : else
6359 : 1 : return tl::nullopt;
6360 : 1254 : }
6361 : : }
6362 : :
6363 : 77 : if (!expr)
6364 : 0 : return tl::nullopt;
6365 : :
6366 : 77 : return AST::GenericArg::create_const (std::move (expr));
6367 : 3752 : }
6368 : :
6369 : : // Parses the generic arguments in each path segment.
6370 : : template <typename ManagedTokenSource>
6371 : : AST::GenericArgs
6372 : 3550 : Parser<ManagedTokenSource>::parse_path_generic_args ()
6373 : : {
6374 : 7100 : if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
6375 : 5 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
6376 : :
6377 : 3550 : if (!skip_token (LEFT_ANGLE))
6378 : : {
6379 : : // skip after somewhere?
6380 : 0 : return AST::GenericArgs::create_empty ();
6381 : : }
6382 : :
6383 : : // We need to parse all lifetimes, then parse types and const generics in
6384 : : // any order.
6385 : :
6386 : : // try to parse lifetimes first
6387 : 3550 : std::vector<AST::Lifetime> lifetime_args;
6388 : :
6389 : 3550 : const_TokenPtr t = lexer.peek_token ();
6390 : 3550 : location_t locus = t->get_locus ();
6391 : 7103 : while (!is_right_angle_tok (t->get_id ()))
6392 : : {
6393 : 3553 : auto lifetime = parse_lifetime (false);
6394 : 3553 : if (!lifetime)
6395 : : {
6396 : : // not necessarily an error
6397 : : break;
6398 : : }
6399 : :
6400 : 34 : lifetime_args.push_back (std::move (lifetime.value ()));
6401 : :
6402 : : // if next token isn't comma, then it must be end of list
6403 : 68 : if (lexer.peek_token ()->get_id () != COMMA)
6404 : : {
6405 : : break;
6406 : : }
6407 : : // skip comma
6408 : 4 : lexer.skip_token ();
6409 : :
6410 : 4 : t = lexer.peek_token ();
6411 : : }
6412 : :
6413 : : // try to parse types and const generics second
6414 : 3550 : std::vector<AST::GenericArg> generic_args;
6415 : :
6416 : : // TODO: think of better control structure
6417 : 3550 : t = lexer.peek_token ();
6418 : 10802 : while (!is_right_angle_tok (t->get_id ()))
6419 : : {
6420 : : // FIXME: Is it fine to break if there is one binding? Can't there be
6421 : : // bindings in between types?
6422 : :
6423 : : // ensure not binding being parsed as type accidently
6424 : 6068 : if (t->get_id () == IDENTIFIER
6425 : 6297 : && lexer.peek_token (1)->get_id () == EQUAL)
6426 : : break;
6427 : :
6428 : 3733 : auto arg = parse_generic_arg ();
6429 : 3733 : if (arg)
6430 : : {
6431 : 3733 : generic_args.emplace_back (std::move (arg.value ()));
6432 : : }
6433 : :
6434 : : // FIXME: Do we need to break if we encounter an error?
6435 : :
6436 : : // if next token isn't comma, then it must be end of list
6437 : 7466 : if (lexer.peek_token ()->get_id () != COMMA)
6438 : : break;
6439 : :
6440 : : // skip comma
6441 : 286 : lexer.skip_token ();
6442 : 286 : t = lexer.peek_token ();
6443 : : }
6444 : :
6445 : : // try to parse bindings third
6446 : 3550 : std::vector<AST::GenericArgsBinding> binding_args;
6447 : :
6448 : : // TODO: think of better control structure
6449 : 3550 : t = lexer.peek_token ();
6450 : 3623 : while (!is_right_angle_tok (t->get_id ()))
6451 : : {
6452 : 73 : AST::GenericArgsBinding binding = parse_generic_args_binding ();
6453 : 73 : if (binding.is_error ())
6454 : : {
6455 : : // not necessarily an error
6456 : : break;
6457 : : }
6458 : :
6459 : 73 : binding_args.push_back (std::move (binding));
6460 : :
6461 : : // if next token isn't comma, then it must be end of list
6462 : 146 : if (lexer.peek_token ()->get_id () != COMMA)
6463 : : {
6464 : : break;
6465 : : }
6466 : : // skip comma
6467 : 1 : lexer.skip_token ();
6468 : :
6469 : 1 : t = lexer.peek_token ();
6470 : : }
6471 : :
6472 : : // skip any trailing commas
6473 : 7100 : if (lexer.peek_token ()->get_id () == COMMA)
6474 : 0 : lexer.skip_token ();
6475 : :
6476 : 3550 : if (!skip_generics_right_angle ())
6477 : 0 : return AST::GenericArgs::create_empty ();
6478 : :
6479 : 3550 : lifetime_args.shrink_to_fit ();
6480 : 3550 : generic_args.shrink_to_fit ();
6481 : 3550 : binding_args.shrink_to_fit ();
6482 : :
6483 : 3550 : return AST::GenericArgs (std::move (lifetime_args), std::move (generic_args),
6484 : 3550 : std::move (binding_args), locus);
6485 : 7100 : }
6486 : :
6487 : : // Parses a binding in a generic args path segment.
6488 : : template <typename ManagedTokenSource>
6489 : : AST::GenericArgsBinding
6490 : 73 : Parser<ManagedTokenSource>::parse_generic_args_binding ()
6491 : : {
6492 : 73 : const_TokenPtr ident_tok = lexer.peek_token ();
6493 : 73 : if (ident_tok->get_id () != IDENTIFIER)
6494 : : {
6495 : : // allow non error-inducing use
6496 : : // skip somewhere?
6497 : 0 : return AST::GenericArgsBinding::create_error ();
6498 : : }
6499 : 73 : lexer.skip_token ();
6500 : 73 : Identifier ident{ident_tok};
6501 : :
6502 : 73 : if (!skip_token (EQUAL))
6503 : : {
6504 : : // skip after somewhere?
6505 : 0 : return AST::GenericArgsBinding::create_error ();
6506 : : }
6507 : :
6508 : : // parse type (required)
6509 : 73 : std::unique_ptr<AST::Type> type = parse_type ();
6510 : 73 : if (type == nullptr)
6511 : : {
6512 : : // skip somewhere?
6513 : 0 : return AST::GenericArgsBinding::create_error ();
6514 : : }
6515 : :
6516 : 146 : return AST::GenericArgsBinding (std::move (ident), std::move (type),
6517 : 146 : ident_tok->get_locus ());
6518 : 146 : }
6519 : :
6520 : : /* Parses a single type path segment (not including opening scope resolution,
6521 : : * but includes any internal ones). Includes generic args or type path
6522 : : * functions too. */
6523 : : template <typename ManagedTokenSource>
6524 : : std::unique_ptr<AST::TypePathSegment>
6525 : 53933 : Parser<ManagedTokenSource>::parse_type_path_segment ()
6526 : : {
6527 : 53933 : location_t locus = lexer.peek_token ()->get_locus ();
6528 : : // parse ident segment part
6529 : 53933 : AST::PathIdentSegment ident_segment = parse_path_ident_segment ();
6530 : 53933 : if (ident_segment.is_error ())
6531 : : {
6532 : : // not necessarily an error
6533 : 201 : return nullptr;
6534 : : }
6535 : :
6536 : : /* lookahead to determine if variants exist - only consume scope resolution
6537 : : * then */
6538 : 53732 : bool has_separating_scope_resolution = false;
6539 : 53732 : const_TokenPtr next = lexer.peek_token (1);
6540 : 107464 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
6541 : 53732 : && (next->get_id () == LEFT_ANGLE || next->get_id () == LEFT_PAREN))
6542 : : {
6543 : 0 : has_separating_scope_resolution = true;
6544 : 0 : lexer.skip_token ();
6545 : : }
6546 : :
6547 : : // branch into variants on next token
6548 : 53732 : const_TokenPtr t = lexer.peek_token ();
6549 : 53732 : switch (t->get_id ())
6550 : : {
6551 : 2688 : case LEFT_SHIFT:
6552 : : case LEFT_ANGLE:
6553 : : {
6554 : : // parse generic args
6555 : 2688 : AST::GenericArgs generic_args = parse_path_generic_args ();
6556 : :
6557 : 2688 : return std::unique_ptr<AST::TypePathSegmentGeneric> (
6558 : 5376 : new AST::TypePathSegmentGeneric (std::move (ident_segment),
6559 : : has_separating_scope_resolution,
6560 : 2688 : std::move (generic_args), locus));
6561 : 2688 : }
6562 : 30 : case LEFT_PAREN:
6563 : : {
6564 : : // parse type path function
6565 : 30 : AST::TypePathFunction type_path_function
6566 : : = parse_type_path_function (locus);
6567 : :
6568 : 30 : if (type_path_function.is_error ())
6569 : : {
6570 : : // skip after somewhere?
6571 : 0 : return nullptr;
6572 : : }
6573 : :
6574 : 30 : return std::unique_ptr<AST::TypePathSegmentFunction> (
6575 : 90 : new AST::TypePathSegmentFunction (std::move (ident_segment),
6576 : : has_separating_scope_resolution,
6577 : : std::move (type_path_function),
6578 : 30 : locus));
6579 : 30 : }
6580 : 51014 : default:
6581 : : // neither of them
6582 : : return std::unique_ptr<AST::TypePathSegment> (
6583 : 51014 : new AST::TypePathSegment (std::move (ident_segment),
6584 : 51014 : has_separating_scope_resolution, locus));
6585 : : }
6586 : : rust_unreachable ();
6587 : 107464 : }
6588 : :
6589 : : // Parses a function call representation inside a type path.
6590 : : template <typename ManagedTokenSource>
6591 : : AST::TypePathFunction
6592 : 30 : Parser<ManagedTokenSource>::parse_type_path_function (location_t id_location)
6593 : : {
6594 : 30 : if (!skip_token (LEFT_PAREN))
6595 : : {
6596 : : // skip somewhere?
6597 : : return AST::TypePathFunction::create_error ();
6598 : : }
6599 : :
6600 : : // parse function inputs
6601 : 30 : std::vector<std::unique_ptr<AST::Type>> inputs;
6602 : :
6603 : 94 : while (lexer.peek_token ()->get_id () != RIGHT_PAREN)
6604 : : {
6605 : 32 : std::unique_ptr<AST::Type> type = parse_type ();
6606 : 32 : if (type == nullptr)
6607 : : {
6608 : : /* this is an error as there should've been a ')' there if there
6609 : : * wasn't a type */
6610 : 0 : Error error (
6611 : 0 : lexer.peek_token ()->get_locus (),
6612 : : "failed to parse type in parameters of type path function");
6613 : 0 : add_error (std::move (error));
6614 : :
6615 : : // skip somewhere?
6616 : 0 : return AST::TypePathFunction::create_error ();
6617 : 0 : }
6618 : :
6619 : 32 : inputs.push_back (std::move (type));
6620 : :
6621 : : // skip commas, including trailing commas
6622 : 64 : if (lexer.peek_token ()->get_id () != COMMA)
6623 : : break;
6624 : :
6625 : 2 : lexer.skip_token ();
6626 : : }
6627 : :
6628 : 30 : if (!skip_token (RIGHT_PAREN))
6629 : : {
6630 : : // skip somewhere?
6631 : : return AST::TypePathFunction::create_error ();
6632 : : }
6633 : :
6634 : : // parse optional return type
6635 : 30 : std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
6636 : :
6637 : 30 : inputs.shrink_to_fit ();
6638 : 30 : return AST::TypePathFunction (std::move (inputs), id_location,
6639 : 30 : std::move (return_type));
6640 : 30 : }
6641 : :
6642 : : // Parses a path inside an expression that allows generic arguments.
6643 : : template <typename ManagedTokenSource>
6644 : : AST::PathInExpression
6645 : 39224 : Parser<ManagedTokenSource>::parse_path_in_expression ()
6646 : : {
6647 : 39224 : location_t locus = UNKNOWN_LOCATION;
6648 : 39224 : bool has_opening_scope_resolution = false;
6649 : 78448 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
6650 : : {
6651 : 8 : has_opening_scope_resolution = true;
6652 : :
6653 : 8 : locus = lexer.peek_token ()->get_locus ();
6654 : :
6655 : 8 : lexer.skip_token ();
6656 : : }
6657 : :
6658 : : // create segment vector
6659 : 39224 : std::vector<AST::PathExprSegment> segments;
6660 : :
6661 : 39224 : if (locus == UNKNOWN_LOCATION)
6662 : : {
6663 : 78432 : locus = lexer.peek_token ()->get_locus ();
6664 : : }
6665 : :
6666 : : // parse required initial segment
6667 : 39224 : AST::PathExprSegment initial_segment = parse_path_expr_segment ();
6668 : 39224 : if (initial_segment.is_error ())
6669 : : {
6670 : : // skip after somewhere?
6671 : : // don't necessarily throw error but yeah
6672 : 2 : return AST::PathInExpression::create_error ();
6673 : : }
6674 : 39222 : segments.push_back (std::move (initial_segment));
6675 : :
6676 : : // parse optional segments (as long as scope resolution operator exists)
6677 : 39222 : const_TokenPtr t = lexer.peek_token ();
6678 : 44604 : while (t->get_id () == SCOPE_RESOLUTION)
6679 : : {
6680 : : // skip scope resolution operator
6681 : 5382 : lexer.skip_token ();
6682 : :
6683 : : // parse the actual segment - it is an error if it doesn't exist now
6684 : 5382 : AST::PathExprSegment segment = parse_path_expr_segment ();
6685 : 5382 : if (segment.is_error ())
6686 : : {
6687 : : // skip after somewhere?
6688 : 0 : Error error (t->get_locus (),
6689 : : "could not parse path expression segment");
6690 : 0 : add_error (std::move (error));
6691 : :
6692 : 0 : return AST::PathInExpression::create_error ();
6693 : 0 : }
6694 : :
6695 : 5382 : segments.push_back (std::move (segment));
6696 : :
6697 : 5382 : t = lexer.peek_token ();
6698 : : }
6699 : :
6700 : 39222 : segments.shrink_to_fit ();
6701 : :
6702 : 39222 : return AST::PathInExpression (std::move (segments), {}, locus,
6703 : 39222 : has_opening_scope_resolution);
6704 : 78446 : }
6705 : :
6706 : : /* Parses a single path in expression path segment (including generic
6707 : : * arguments). */
6708 : : template <typename ManagedTokenSource>
6709 : : AST::PathExprSegment
6710 : 50183 : Parser<ManagedTokenSource>::parse_path_expr_segment ()
6711 : : {
6712 : 50183 : location_t locus = lexer.peek_token ()->get_locus ();
6713 : : // parse ident segment
6714 : 50183 : AST::PathIdentSegment ident = parse_path_ident_segment ();
6715 : 50183 : if (ident.is_error ())
6716 : : {
6717 : : // not necessarily an error?
6718 : 2 : return AST::PathExprSegment::create_error ();
6719 : : }
6720 : :
6721 : : // parse generic args (and turbofish), if they exist
6722 : : /* use lookahead to determine if they actually exist (don't want to
6723 : : * accidently parse over next ident segment) */
6724 : 100362 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
6725 : 68121 : && (lexer.peek_token (1)->get_id () == LEFT_ANGLE
6726 : 19469 : || lexer.peek_token (1)->get_id () == LEFT_SHIFT))
6727 : : {
6728 : : // skip scope resolution
6729 : 554 : lexer.skip_token ();
6730 : :
6731 : : // Let parse_path_generic_args split "<<" tokens
6732 : 554 : AST::GenericArgs generic_args = parse_path_generic_args ();
6733 : :
6734 : 1108 : return AST::PathExprSegment (std::move (ident), locus,
6735 : 554 : std::move (generic_args));
6736 : 554 : }
6737 : :
6738 : : // return a generic parameter-less expr segment if not found
6739 : 49627 : return AST::PathExprSegment (std::move (ident), locus);
6740 : 50183 : }
6741 : :
6742 : : /* Parses a fully qualified path in expression (i.e. a pattern). FIXME does
6743 : : * not parse outer attrs. */
6744 : : template <typename ManagedTokenSource>
6745 : : AST::QualifiedPathInExpression
6746 : 97 : Parser<ManagedTokenSource>::parse_qualified_path_in_expression (
6747 : : location_t pratt_parsed_loc)
6748 : : {
6749 : : /* Note: the Rust grammar is defined in such a way that it is impossible to
6750 : : * determine whether a prospective qualified path is a
6751 : : * QualifiedPathInExpression or QualifiedPathInType in all cases by the
6752 : : * rules themselves (the only possible difference is a TypePathSegment with
6753 : : * function, and lookahead to find this is too difficult). However, as this
6754 : : * is a pattern and QualifiedPathInType is a type, I believe it that their
6755 : : * construction will not be confused (due to rules regarding patterns vs
6756 : : * types).
6757 : : * As such, this function will not attempt to minimise errors created by
6758 : : * their confusion. */
6759 : :
6760 : : // parse the qualified path type (required)
6761 : 97 : AST::QualifiedPathType qual_path_type
6762 : : = parse_qualified_path_type (pratt_parsed_loc);
6763 : 97 : if (qual_path_type.is_error ())
6764 : : {
6765 : : // TODO: should this create a parse error?
6766 : 0 : return AST::QualifiedPathInExpression::create_error ();
6767 : : }
6768 : 97 : location_t locus = qual_path_type.get_locus ();
6769 : :
6770 : : // parse path segments
6771 : 97 : std::vector<AST::PathExprSegment> segments;
6772 : :
6773 : : // parse initial required segment
6774 : 194 : if (!expect_token (SCOPE_RESOLUTION))
6775 : : {
6776 : : // skip after somewhere?
6777 : :
6778 : 0 : return AST::QualifiedPathInExpression::create_error ();
6779 : : }
6780 : 97 : AST::PathExprSegment initial_segment = parse_path_expr_segment ();
6781 : 97 : if (initial_segment.is_error ())
6782 : : {
6783 : : // skip after somewhere?
6784 : 0 : Error error (lexer.peek_token ()->get_locus (),
6785 : : "required initial path expression segment in "
6786 : : "qualified path in expression could not be parsed");
6787 : 0 : add_error (std::move (error));
6788 : :
6789 : 0 : return AST::QualifiedPathInExpression::create_error ();
6790 : 0 : }
6791 : 97 : segments.push_back (std::move (initial_segment));
6792 : :
6793 : : // parse optional segments (as long as scope resolution operator exists)
6794 : 97 : const_TokenPtr t = lexer.peek_token ();
6795 : 97 : while (t->get_id () == SCOPE_RESOLUTION)
6796 : : {
6797 : : // skip scope resolution operator
6798 : 0 : lexer.skip_token ();
6799 : :
6800 : : // parse the actual segment - it is an error if it doesn't exist now
6801 : 0 : AST::PathExprSegment segment = parse_path_expr_segment ();
6802 : 0 : if (segment.is_error ())
6803 : : {
6804 : : // skip after somewhere?
6805 : 0 : Error error (t->get_locus (),
6806 : : "could not parse path expression segment in qualified "
6807 : : "path in expression");
6808 : 0 : add_error (std::move (error));
6809 : :
6810 : 0 : return AST::QualifiedPathInExpression::create_error ();
6811 : 0 : }
6812 : :
6813 : 0 : segments.push_back (std::move (segment));
6814 : :
6815 : 0 : t = lexer.peek_token ();
6816 : : }
6817 : :
6818 : 97 : segments.shrink_to_fit ();
6819 : :
6820 : : // FIXME: outer attr parsing
6821 : 194 : return AST::QualifiedPathInExpression (std::move (qual_path_type),
6822 : 97 : std::move (segments), {}, locus);
6823 : 194 : }
6824 : :
6825 : : // Parses the type syntactical construction at the start of a qualified path.
6826 : : template <typename ManagedTokenSource>
6827 : : AST::QualifiedPathType
6828 : 420 : Parser<ManagedTokenSource>::parse_qualified_path_type (
6829 : : location_t pratt_parsed_loc)
6830 : : {
6831 : 420 : location_t locus = pratt_parsed_loc;
6832 : : /* TODO: should this actually be error? is there anywhere where this could
6833 : : * be valid? */
6834 : 420 : if (locus == UNKNOWN_LOCATION)
6835 : : {
6836 : 323 : locus = lexer.peek_token ()->get_locus ();
6837 : :
6838 : 646 : if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
6839 : 1 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
6840 : :
6841 : : // skip after somewhere?
6842 : 323 : if (!skip_token (LEFT_ANGLE))
6843 : 0 : return AST::QualifiedPathType::create_error ();
6844 : : }
6845 : :
6846 : : // parse type (required)
6847 : 420 : std::unique_ptr<AST::Type> type = parse_type ();
6848 : 420 : if (type == nullptr)
6849 : : {
6850 : 0 : Error error (lexer.peek_token ()->get_locus (),
6851 : : "could not parse type in qualified path type");
6852 : 0 : add_error (std::move (error));
6853 : :
6854 : : // skip somewhere?
6855 : 0 : return AST::QualifiedPathType::create_error ();
6856 : 0 : }
6857 : :
6858 : : // parse optional as clause
6859 : 420 : AST::TypePath as_type_path = AST::TypePath::create_error ();
6860 : 840 : if (lexer.peek_token ()->get_id () == AS)
6861 : : {
6862 : 385 : lexer.skip_token ();
6863 : :
6864 : : // parse type path, which is required now
6865 : 385 : as_type_path = parse_type_path ();
6866 : 385 : if (as_type_path.is_error ())
6867 : : {
6868 : 0 : Error error (
6869 : 0 : lexer.peek_token ()->get_locus (),
6870 : : "could not parse type path in as clause in qualified path type");
6871 : 0 : add_error (std::move (error));
6872 : :
6873 : : // skip somewhere?
6874 : 0 : return AST::QualifiedPathType::create_error ();
6875 : 0 : }
6876 : : }
6877 : :
6878 : : /* NOTE: should actually be a right-angle token, so
6879 : : * skip_generics_right_angle shouldn't be required */
6880 : 420 : if (!skip_token (RIGHT_ANGLE))
6881 : : {
6882 : : // skip after somewhere?
6883 : 0 : return AST::QualifiedPathType::create_error ();
6884 : : }
6885 : :
6886 : 420 : return AST::QualifiedPathType (std::move (type), locus,
6887 : 420 : std::move (as_type_path));
6888 : 420 : }
6889 : :
6890 : : // Parses a fully qualified path in type (i.e. a type).
6891 : : template <typename ManagedTokenSource>
6892 : : AST::QualifiedPathInType
6893 : 323 : Parser<ManagedTokenSource>::parse_qualified_path_in_type ()
6894 : : {
6895 : 323 : location_t locus = lexer.peek_token ()->get_locus ();
6896 : : // parse the qualified path type (required)
6897 : 323 : AST::QualifiedPathType qual_path_type = parse_qualified_path_type ();
6898 : 323 : if (qual_path_type.is_error ())
6899 : : {
6900 : : // TODO: should this create a parse error?
6901 : 0 : return AST::QualifiedPathInType::create_error ();
6902 : : }
6903 : :
6904 : : // parse initial required segment
6905 : 646 : if (!expect_token (SCOPE_RESOLUTION))
6906 : : {
6907 : : // skip after somewhere?
6908 : :
6909 : 0 : return AST::QualifiedPathInType::create_error ();
6910 : : }
6911 : 323 : std::unique_ptr<AST::TypePathSegment> initial_segment
6912 : : = parse_type_path_segment ();
6913 : 323 : if (initial_segment == nullptr)
6914 : : {
6915 : : // skip after somewhere?
6916 : 0 : Error error (lexer.peek_token ()->get_locus (),
6917 : : "required initial type path segment in qualified path in "
6918 : : "type could not be parsed");
6919 : 0 : add_error (std::move (error));
6920 : :
6921 : 0 : return AST::QualifiedPathInType::create_error ();
6922 : 0 : }
6923 : :
6924 : : // parse optional segments (as long as scope resolution operator exists)
6925 : 323 : std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
6926 : 323 : const_TokenPtr t = lexer.peek_token ();
6927 : 323 : while (t->get_id () == SCOPE_RESOLUTION)
6928 : : {
6929 : : // skip scope resolution operator
6930 : 0 : lexer.skip_token ();
6931 : :
6932 : : // parse the actual segment - it is an error if it doesn't exist now
6933 : 0 : std::unique_ptr<AST::TypePathSegment> segment
6934 : : = parse_type_path_segment ();
6935 : 0 : if (segment == nullptr)
6936 : : {
6937 : : // skip after somewhere?
6938 : 0 : Error error (
6939 : : t->get_locus (),
6940 : : "could not parse type path segment in qualified path in type");
6941 : 0 : add_error (std::move (error));
6942 : :
6943 : 0 : return AST::QualifiedPathInType::create_error ();
6944 : 0 : }
6945 : :
6946 : 0 : segments.push_back (std::move (segment));
6947 : :
6948 : 0 : t = lexer.peek_token ();
6949 : : }
6950 : :
6951 : 323 : segments.shrink_to_fit ();
6952 : :
6953 : 646 : return AST::QualifiedPathInType (std::move (qual_path_type),
6954 : : std::move (initial_segment),
6955 : 323 : std::move (segments), locus);
6956 : 323 : }
6957 : :
6958 : : // Parses a self param. Also handles self param not existing.
6959 : : template <typename ManagedTokenSource>
6960 : : tl::expected<std::unique_ptr<AST::Param>, ParseSelfError>
6961 : 17806 : Parser<ManagedTokenSource>::parse_self_param ()
6962 : : {
6963 : 17806 : bool has_reference = false;
6964 : 17806 : AST::Lifetime lifetime = AST::Lifetime::elided ();
6965 : :
6966 : 17806 : location_t locus = lexer.peek_token ()->get_locus ();
6967 : :
6968 : : // TODO: Feels off, find a better way to clearly express this
6969 : 71224 : std::vector<std::vector<TokenId>> ptrs
6970 : : = {{ASTERISK, SELF} /* *self */,
6971 : : {ASTERISK, CONST, SELF} /* *const self */,
6972 : : {ASTERISK, MUT, SELF} /* *mut self */};
6973 : :
6974 : 71218 : for (auto &s : ptrs)
6975 : : {
6976 : : size_t i = 0;
6977 : 53426 : for (i = 0; i < s.size (); i++)
6978 : 106846 : if (lexer.peek_token (i)->get_id () != s[i])
6979 : : break;
6980 : 53415 : if (i == s.size ())
6981 : : {
6982 : 3 : rust_error_at (lexer.peek_token ()->get_locus (),
6983 : : "cannot pass %<self%> by raw pointer");
6984 : 3 : return tl::make_unexpected (ParseSelfError::SELF_PTR);
6985 : : }
6986 : : }
6987 : :
6988 : : // Trying to find those patterns:
6989 : : //
6990 : : // &'lifetime mut self
6991 : : // &'lifetime self
6992 : : // & mut self
6993 : : // & self
6994 : : // mut self
6995 : : // self
6996 : : //
6997 : : // If not found, it is probably a function, exit and let function parsing
6998 : : // handle it.
6999 : : bool is_self = false;
7000 : 106818 : for (size_t i = 0; i < 5; i++)
7001 : 178030 : if (lexer.peek_token (i)->get_id () == SELF)
7002 : 7766 : is_self = true;
7003 : :
7004 : 17803 : if (!is_self)
7005 : 10040 : return tl::make_unexpected (ParseSelfError::NOT_SELF);
7006 : :
7007 : : // test if self is a reference parameter
7008 : 15526 : if (lexer.peek_token ()->get_id () == AMP)
7009 : : {
7010 : 4600 : has_reference = true;
7011 : 4600 : lexer.skip_token ();
7012 : :
7013 : : // now test whether it has a lifetime
7014 : 9200 : if (lexer.peek_token ()->get_id () == LIFETIME)
7015 : : {
7016 : : // something went wrong somehow
7017 : 0 : if (auto parsed_lifetime = parse_lifetime (true))
7018 : : {
7019 : 0 : lifetime = parsed_lifetime.value ();
7020 : : }
7021 : : else
7022 : : {
7023 : 0 : Error error (lexer.peek_token ()->get_locus (),
7024 : : "failed to parse lifetime in self param");
7025 : 0 : add_error (std::move (error));
7026 : :
7027 : : // skip after somewhere?
7028 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7029 : 0 : }
7030 : : }
7031 : : }
7032 : :
7033 : : // test for mut
7034 : 7763 : bool has_mut = false;
7035 : 15526 : if (lexer.peek_token ()->get_id () == MUT)
7036 : : {
7037 : 347 : has_mut = true;
7038 : 347 : lexer.skip_token ();
7039 : : }
7040 : :
7041 : : // skip self token
7042 : 7763 : const_TokenPtr self_tok = lexer.peek_token ();
7043 : 7763 : if (self_tok->get_id () != SELF)
7044 : : {
7045 : : // skip after somewhere?
7046 : 2 : return tl::make_unexpected (ParseSelfError::NOT_SELF);
7047 : : }
7048 : 7761 : lexer.skip_token ();
7049 : :
7050 : : // parse optional type
7051 : 7761 : std::unique_ptr<AST::Type> type = nullptr;
7052 : 15522 : if (lexer.peek_token ()->get_id () == COLON)
7053 : : {
7054 : 1 : lexer.skip_token ();
7055 : :
7056 : : // type is now required
7057 : 1 : type = parse_type ();
7058 : 1 : if (type == nullptr)
7059 : : {
7060 : 0 : Error error (lexer.peek_token ()->get_locus (),
7061 : : "could not parse type in self param");
7062 : 0 : add_error (std::move (error));
7063 : :
7064 : : // skip after somewhere?
7065 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7066 : 0 : }
7067 : : }
7068 : :
7069 : : // ensure that cannot have both type and reference
7070 : 7761 : if (type != nullptr && has_reference)
7071 : : {
7072 : 0 : Error error (
7073 : 0 : lexer.peek_token ()->get_locus (),
7074 : : "cannot have both a reference and a type specified in a self param");
7075 : 0 : add_error (std::move (error));
7076 : :
7077 : : // skip after somewhere?
7078 : 0 : return tl::make_unexpected (ParseSelfError::PARSING);
7079 : 0 : }
7080 : :
7081 : 7761 : if (has_reference)
7082 : : {
7083 : 4600 : return std::make_unique<AST::SelfParam> (std::move (lifetime), has_mut,
7084 : 4600 : locus);
7085 : : }
7086 : : else
7087 : : {
7088 : : // note that type may be nullptr here and that's fine
7089 : 3161 : return std::make_unique<AST::SelfParam> (std::move (type), has_mut,
7090 : 3161 : locus);
7091 : : }
7092 : 25567 : }
7093 : :
7094 : : /* Parses an expression or macro statement. */
7095 : : template <typename ManagedTokenSource>
7096 : : std::unique_ptr<AST::Stmt>
7097 : 321 : Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
7098 : : ParseRestrictions restrictions)
7099 : : {
7100 : 321 : location_t locus = lexer.peek_token ()->get_locus ();
7101 : :
7102 : 321 : std::unique_ptr<AST::Expr> expr;
7103 : :
7104 : 642 : switch (lexer.peek_token ()->get_id ())
7105 : : {
7106 : 217 : case IDENTIFIER:
7107 : : case CRATE:
7108 : : case SUPER:
7109 : : case SELF:
7110 : : case SELF_ALIAS:
7111 : : case DOLLAR_SIGN:
7112 : : case SCOPE_RESOLUTION:
7113 : : {
7114 : 217 : AST::PathInExpression path = parse_path_in_expression ();
7115 : 217 : std::unique_ptr<AST::Expr> null_denotation;
7116 : :
7117 : 434 : if (lexer.peek_token ()->get_id () == EXCLAM)
7118 : : {
7119 : 60 : std::unique_ptr<AST::MacroInvocation> invoc
7120 : 120 : = parse_macro_invocation_partial (std::move (path),
7121 : : std::move (outer_attrs));
7122 : :
7123 : 60 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
7124 : : {
7125 : 59 : invoc->add_semicolon ();
7126 : : // Macro invocation with semicolon.
7127 : 59 : return invoc;
7128 : : }
7129 : :
7130 : 1 : TokenId after_macro = lexer.peek_token ()->get_id ();
7131 : :
7132 : 1 : if (restrictions.allow_close_after_expr_stmt
7133 : 1 : && (after_macro == RIGHT_PAREN || after_macro == RIGHT_CURLY
7134 : : || after_macro == RIGHT_SQUARE))
7135 : 1 : return invoc;
7136 : :
7137 : 0 : if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
7138 : : == AST::CURLY
7139 : 0 : && after_macro != DOT && after_macro != QUESTION_MARK)
7140 : : {
7141 : 0 : rust_debug ("braced macro statement");
7142 : 0 : return invoc;
7143 : : }
7144 : :
7145 : 0 : null_denotation = std::move (invoc);
7146 : 60 : }
7147 : : else
7148 : : {
7149 : : null_denotation
7150 : 157 : = null_denotation_path (std::move (path), {}, restrictions);
7151 : : }
7152 : :
7153 : 157 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
7154 : : std::move (outer_attrs), restrictions);
7155 : : break;
7156 : 217 : }
7157 : 104 : default:
7158 : 104 : restrictions.expr_can_be_stmt = true;
7159 : 104 : expr = parse_expr (std::move (outer_attrs), restrictions);
7160 : 104 : break;
7161 : : }
7162 : :
7163 : 261 : if (expr == nullptr)
7164 : : {
7165 : : // expr is required, error
7166 : 0 : Error error (lexer.peek_token ()->get_locus (),
7167 : : "failed to parse expr in expr statement");
7168 : 0 : add_error (std::move (error));
7169 : :
7170 : 0 : skip_after_semicolon ();
7171 : 0 : return nullptr;
7172 : 0 : }
7173 : :
7174 : 261 : bool has_semi = false;
7175 : :
7176 : 261 : if (restrictions.consume_semi)
7177 : : {
7178 : 247 : if (maybe_skip_token (SEMICOLON))
7179 : : {
7180 : : has_semi = true;
7181 : : }
7182 : 110 : else if (expr->is_expr_without_block ())
7183 : : {
7184 : 9 : if (restrictions.allow_close_after_expr_stmt)
7185 : : {
7186 : 9 : TokenId id = lexer.peek_token ()->get_id ();
7187 : 9 : if (id != RIGHT_PAREN && id != RIGHT_CURLY && id != RIGHT_SQUARE)
7188 : : {
7189 : 2 : expect_token (SEMICOLON);
7190 : 2 : return nullptr;
7191 : : }
7192 : : }
7193 : : else
7194 : : {
7195 : 0 : expect_token (SEMICOLON);
7196 : 0 : return nullptr;
7197 : : }
7198 : : }
7199 : : }
7200 : :
7201 : 259 : return std::unique_ptr<AST::ExprStmt> (
7202 : 259 : new AST::ExprStmt (std::move (expr), locus, has_semi));
7203 : 321 : }
7204 : :
7205 : : // Parses a block expression, including the curly braces at start and end.
7206 : : template <typename ManagedTokenSource>
7207 : : std::unique_ptr<AST::BlockExpr>
7208 : 21555 : Parser<ManagedTokenSource>::parse_block_expr (
7209 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
7210 : : location_t pratt_parsed_loc)
7211 : : {
7212 : 21555 : location_t locus = pratt_parsed_loc;
7213 : 21555 : if (locus == UNKNOWN_LOCATION)
7214 : : {
7215 : 20220 : locus = lexer.peek_token ()->get_locus ();
7216 : 20220 : if (!skip_token (LEFT_CURLY))
7217 : : {
7218 : 0 : skip_after_end_block ();
7219 : 0 : return nullptr;
7220 : : }
7221 : : }
7222 : :
7223 : 21555 : AST::AttrVec inner_attrs = parse_inner_attributes ();
7224 : :
7225 : : // parse statements and expression
7226 : 21555 : std::vector<std::unique_ptr<AST::Stmt>> stmts;
7227 : 21555 : std::unique_ptr<AST::Expr> expr = nullptr;
7228 : :
7229 : 21555 : const_TokenPtr t = lexer.peek_token ();
7230 : 59192 : while (t->get_id () != RIGHT_CURLY)
7231 : : {
7232 : 37637 : ExprOrStmt expr_or_stmt = parse_stmt_or_expr ();
7233 : 37637 : if (expr_or_stmt.is_error ())
7234 : : {
7235 : 29 : Error error (
7236 : : t->get_locus (),
7237 : : "failed to parse statement or expression in block expression");
7238 : 29 : add_error (std::move (error));
7239 : :
7240 : 29 : return nullptr;
7241 : 29 : }
7242 : :
7243 : 37608 : t = lexer.peek_token ();
7244 : :
7245 : 37608 : if (expr_or_stmt.stmt != nullptr)
7246 : : {
7247 : 22076 : stmts.push_back (std::move (expr_or_stmt.stmt));
7248 : : }
7249 : : else
7250 : : {
7251 : : // assign to expression and end parsing inside
7252 : 15532 : expr = std::move (expr_or_stmt.expr);
7253 : : break;
7254 : : }
7255 : : }
7256 : :
7257 : 21526 : location_t end_locus = t->get_locus ();
7258 : :
7259 : 21526 : if (!skip_token (RIGHT_CURLY))
7260 : : {
7261 : 0 : Error error (t->get_locus (),
7262 : : "error may be from having an expression (as opposed to "
7263 : : "statement) in the body of the function but not last");
7264 : 0 : add_error (std::move (error));
7265 : :
7266 : 0 : skip_after_end_block ();
7267 : 0 : return nullptr;
7268 : 0 : }
7269 : :
7270 : : // grammar allows for empty block expressions
7271 : :
7272 : 21526 : stmts.shrink_to_fit ();
7273 : :
7274 : : return std::unique_ptr<AST::BlockExpr> (
7275 : 21526 : new AST::BlockExpr (std::move (stmts), std::move (expr),
7276 : : std::move (inner_attrs), std::move (outer_attrs),
7277 : 21526 : std::move (label), locus, end_locus));
7278 : 21555 : }
7279 : :
7280 : : /* Parse an anonymous const expression. This can be a regular const expression
7281 : : * or an underscore for deferred const inference */
7282 : : template <typename ManagedTokenSource>
7283 : : tl::expected<AST::AnonConst, AnonConstError>
7284 : 598 : Parser<ManagedTokenSource>::parse_anon_const ()
7285 : : {
7286 : 598 : auto current = lexer.peek_token ();
7287 : 598 : auto locus = current->get_locus ();
7288 : :
7289 : : // Special case deferred inference constants
7290 : 598 : if (maybe_skip_token (UNDERSCORE))
7291 : 4 : return AST::AnonConst (locus);
7292 : :
7293 : 594 : auto expr = parse_expr ();
7294 : :
7295 : 594 : if (!expr)
7296 : 0 : return tl::make_unexpected (AnonConstError::InvalidSizeExpr);
7297 : :
7298 : 1188 : return AST::AnonConst (std::move (expr), locus);
7299 : 594 : }
7300 : :
7301 : : /* Parse a "const block", a block preceded by the `const` keyword whose
7302 : : * statements can be const evaluated and used in constant contexts */
7303 : : template <typename ManagedTokenSource>
7304 : : std::unique_ptr<AST::ConstBlock>
7305 : 7 : Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
7306 : : location_t locus)
7307 : : {
7308 : 7 : auto block = parse_block_expr ();
7309 : :
7310 : 7 : if (!block)
7311 : : {
7312 : 0 : add_error (Error (locus, "failed to parse inner block in const block"));
7313 : 0 : skip_after_end_block ();
7314 : :
7315 : 0 : return nullptr;
7316 : : }
7317 : :
7318 : 7 : auto block_locus = block->get_locus ();
7319 : :
7320 : 14 : return std::make_unique<AST::ConstBlock> (AST::AnonConst (std::move (block),
7321 : : block_locus),
7322 : 7 : locus, std::move (outer_attrs));
7323 : 7 : }
7324 : :
7325 : : /* Parses a "grouped" expression (expression in parentheses), used to control
7326 : : * precedence. */
7327 : : template <typename ManagedTokenSource>
7328 : : std::unique_ptr<AST::GroupedExpr>
7329 : 0 : Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
7330 : : {
7331 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
7332 : 0 : skip_token (LEFT_PAREN);
7333 : :
7334 : 0 : AST::AttrVec inner_attrs = parse_inner_attributes ();
7335 : :
7336 : : // parse required expr inside parentheses
7337 : 0 : std::unique_ptr<AST::Expr> expr_in_parens = parse_expr ();
7338 : 0 : if (expr_in_parens == nullptr)
7339 : : {
7340 : : // skip after somewhere?
7341 : : // error?
7342 : 0 : return nullptr;
7343 : : }
7344 : :
7345 : 0 : if (!skip_token (RIGHT_PAREN))
7346 : : {
7347 : : // skip after somewhere?
7348 : 0 : return nullptr;
7349 : : }
7350 : :
7351 : : return std::unique_ptr<AST::GroupedExpr> (
7352 : 0 : new AST::GroupedExpr (std::move (expr_in_parens), std::move (inner_attrs),
7353 : 0 : std::move (outer_attrs), locus));
7354 : 0 : }
7355 : :
7356 : : // Parses a closure expression (closure definition).
7357 : : template <typename ManagedTokenSource>
7358 : : std::unique_ptr<AST::ClosureExpr>
7359 : 0 : Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
7360 : : {
7361 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
7362 : : // detect optional "move"
7363 : 0 : bool has_move = false;
7364 : 0 : if (lexer.peek_token ()->get_id () == MOVE)
7365 : : {
7366 : 0 : lexer.skip_token ();
7367 : 0 : has_move = true;
7368 : : }
7369 : :
7370 : : // handle parameter list
7371 : 0 : std::vector<AST::ClosureParam> params;
7372 : :
7373 : 0 : const_TokenPtr t = lexer.peek_token ();
7374 : 0 : switch (t->get_id ())
7375 : : {
7376 : 0 : case OR:
7377 : : // skip token, no parameters
7378 : 0 : lexer.skip_token ();
7379 : : break;
7380 : 0 : case PIPE:
7381 : : // actually may have parameters
7382 : 0 : lexer.skip_token ();
7383 : 0 : t = lexer.peek_token ();
7384 : :
7385 : 0 : while (t->get_id () != PIPE)
7386 : : {
7387 : 0 : AST::ClosureParam param = parse_closure_param ();
7388 : 0 : if (param.is_error ())
7389 : : {
7390 : : // TODO is this really an error?
7391 : 0 : Error error (t->get_locus (), "could not parse closure param");
7392 : 0 : add_error (std::move (error));
7393 : :
7394 : : break;
7395 : 0 : }
7396 : 0 : params.push_back (std::move (param));
7397 : :
7398 : 0 : if (lexer.peek_token ()->get_id () != COMMA)
7399 : : {
7400 : 0 : lexer.skip_token ();
7401 : : // not an error but means param list is done
7402 : : break;
7403 : : }
7404 : : // skip comma
7405 : 0 : lexer.skip_token ();
7406 : :
7407 : 0 : t = lexer.peek_token ();
7408 : : }
7409 : 0 : params.shrink_to_fit ();
7410 : : break;
7411 : 0 : default:
7412 : 0 : add_error (Error (t->get_locus (),
7413 : : "unexpected token %qs in closure expression - expected "
7414 : : "%<|%> or %<||%>",
7415 : : t->get_token_description ()));
7416 : :
7417 : : // skip somewhere?
7418 : 0 : return nullptr;
7419 : : }
7420 : :
7421 : : // again branch based on next token
7422 : 0 : t = lexer.peek_token ();
7423 : 0 : if (t->get_id () == RETURN_TYPE)
7424 : : {
7425 : : // must be return type closure with block expr
7426 : :
7427 : : // skip "return type" token
7428 : 0 : lexer.skip_token ();
7429 : :
7430 : : // parse actual type, which is required
7431 : 0 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
7432 : 0 : if (type == nullptr)
7433 : : {
7434 : : // error
7435 : 0 : Error error (t->get_locus (), "failed to parse type for closure");
7436 : 0 : add_error (std::move (error));
7437 : :
7438 : : // skip somewhere?
7439 : 0 : return nullptr;
7440 : 0 : }
7441 : :
7442 : : // parse block expr, which is required
7443 : 0 : std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
7444 : 0 : if (block == nullptr)
7445 : : {
7446 : : // error
7447 : 0 : Error error (lexer.peek_token ()->get_locus (),
7448 : : "failed to parse block expr in closure");
7449 : 0 : add_error (std::move (error));
7450 : :
7451 : : // skip somewhere?
7452 : 0 : return nullptr;
7453 : 0 : }
7454 : :
7455 : 0 : return std::unique_ptr<AST::ClosureExprInnerTyped> (
7456 : 0 : new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
7457 : : std::move (params), locus, has_move,
7458 : 0 : std::move (outer_attrs)));
7459 : 0 : }
7460 : : else
7461 : : {
7462 : : // must be expr-only closure
7463 : :
7464 : : // parse expr, which is required
7465 : 0 : std::unique_ptr<AST::Expr> expr = parse_expr ();
7466 : 0 : if (expr == nullptr)
7467 : : {
7468 : 0 : Error error (t->get_locus (),
7469 : : "failed to parse expression in closure");
7470 : 0 : add_error (std::move (error));
7471 : :
7472 : : // skip somewhere?
7473 : 0 : return nullptr;
7474 : 0 : }
7475 : :
7476 : 0 : return std::unique_ptr<AST::ClosureExprInner> (
7477 : 0 : new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
7478 : 0 : has_move, std::move (outer_attrs)));
7479 : 0 : }
7480 : 0 : }
7481 : :
7482 : : // Parses a literal token (to literal expression).
7483 : : template <typename ManagedTokenSource>
7484 : : std::unique_ptr<AST::LiteralExpr>
7485 : 3632 : Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
7486 : : {
7487 : : // TODO: change if literal representation in lexer changes
7488 : :
7489 : 3632 : std::string literal_value;
7490 : 3632 : AST::Literal::LitType type = AST::Literal::STRING;
7491 : :
7492 : : // branch based on token
7493 : 3632 : const_TokenPtr t = lexer.peek_token ();
7494 : 3632 : switch (t->get_id ())
7495 : : {
7496 : 2 : case CHAR_LITERAL:
7497 : 2 : type = AST::Literal::CHAR;
7498 : 2 : literal_value = t->get_str ();
7499 : 2 : lexer.skip_token ();
7500 : : break;
7501 : 295 : case STRING_LITERAL:
7502 : 295 : type = AST::Literal::STRING;
7503 : 295 : literal_value = t->get_str ();
7504 : 295 : lexer.skip_token ();
7505 : : break;
7506 : 0 : case BYTE_CHAR_LITERAL:
7507 : 0 : type = AST::Literal::BYTE;
7508 : 0 : literal_value = t->get_str ();
7509 : 0 : lexer.skip_token ();
7510 : : break;
7511 : 1 : case BYTE_STRING_LITERAL:
7512 : 1 : type = AST::Literal::BYTE_STRING;
7513 : 1 : literal_value = t->get_str ();
7514 : 1 : lexer.skip_token ();
7515 : : break;
7516 : 0 : case RAW_STRING_LITERAL:
7517 : 0 : type = AST::Literal::RAW_STRING;
7518 : 0 : literal_value = t->get_str ();
7519 : 0 : lexer.skip_token ();
7520 : : break;
7521 : 3328 : case INT_LITERAL:
7522 : 3328 : type = AST::Literal::INT;
7523 : 3328 : literal_value = t->get_str ();
7524 : 3328 : lexer.skip_token ();
7525 : : break;
7526 : 1 : case FLOAT_LITERAL:
7527 : 1 : type = AST::Literal::FLOAT;
7528 : 1 : literal_value = t->get_str ();
7529 : 1 : lexer.skip_token ();
7530 : : break;
7531 : : // case BOOL_LITERAL
7532 : : // use true and false keywords rather than "bool literal" Rust terminology
7533 : 0 : case TRUE_LITERAL:
7534 : 0 : type = AST::Literal::BOOL;
7535 : 0 : literal_value = Values::Keywords::TRUE_LITERAL;
7536 : 0 : lexer.skip_token ();
7537 : : break;
7538 : 0 : case FALSE_LITERAL:
7539 : 0 : type = AST::Literal::BOOL;
7540 : 0 : literal_value = Values::Keywords::FALSE_LITERAL;
7541 : 0 : lexer.skip_token ();
7542 : : break;
7543 : 5 : default:
7544 : : // error - cannot be a literal expr
7545 : 5 : add_error (Error (t->get_locus (),
7546 : : "unexpected token %qs when parsing literal expression",
7547 : : t->get_token_description ()));
7548 : :
7549 : : // skip?
7550 : 5 : return nullptr;
7551 : : }
7552 : :
7553 : : // create literal based on stuff in switch
7554 : : return std::unique_ptr<AST::LiteralExpr> (
7555 : 3926 : new AST::LiteralExpr (std::move (literal_value), std::move (type),
7556 : : t->get_type_hint (), std::move (outer_attrs),
7557 : 3627 : t->get_locus ()));
7558 : 3632 : }
7559 : :
7560 : : template <typename ManagedTokenSource>
7561 : : std::unique_ptr<AST::BoxExpr>
7562 : 2 : Parser<ManagedTokenSource>::parse_box_expr (AST::AttrVec outer_attrs,
7563 : : location_t pratt_parsed_loc)
7564 : : {
7565 : 2 : location_t locus = pratt_parsed_loc;
7566 : 2 : if (locus == UNKNOWN_LOCATION)
7567 : : {
7568 : 0 : locus = lexer.peek_token ()->get_locus ();
7569 : 0 : skip_token (BOX);
7570 : : }
7571 : :
7572 : 2 : ParseRestrictions restrictions;
7573 : : restrictions.expr_can_be_null = false;
7574 : :
7575 : 2 : std::unique_ptr<AST::Expr> expr = parse_expr (AST::AttrVec (), restrictions);
7576 : :
7577 : : return std::unique_ptr<AST::BoxExpr> (
7578 : 2 : new AST::BoxExpr (std::move (expr), std::move (outer_attrs), locus));
7579 : 2 : }
7580 : :
7581 : : // Parses a return expression (including any expression to return).
7582 : : template <typename ManagedTokenSource>
7583 : : std::unique_ptr<AST::ReturnExpr>
7584 : 494 : Parser<ManagedTokenSource>::parse_return_expr (AST::AttrVec outer_attrs,
7585 : : location_t pratt_parsed_loc)
7586 : : {
7587 : 494 : location_t locus = pratt_parsed_loc;
7588 : 494 : if (locus == UNKNOWN_LOCATION)
7589 : : {
7590 : 0 : locus = lexer.peek_token ()->get_locus ();
7591 : 0 : skip_token (RETURN_KW);
7592 : : }
7593 : :
7594 : : // parse expression to return, if it exists
7595 : 494 : ParseRestrictions restrictions;
7596 : 494 : restrictions.expr_can_be_null = true;
7597 : 494 : std::unique_ptr<AST::Expr> returned_expr
7598 : 494 : = parse_expr (AST::AttrVec (), restrictions);
7599 : :
7600 : : return std::unique_ptr<AST::ReturnExpr> (
7601 : 494 : new AST::ReturnExpr (std::move (returned_expr), std::move (outer_attrs),
7602 : 494 : locus));
7603 : 494 : }
7604 : :
7605 : : // Parses a try expression.
7606 : : template <typename ManagedTokenSource>
7607 : : std::unique_ptr<AST::TryExpr>
7608 : 1 : Parser<ManagedTokenSource>::parse_try_expr (AST::AttrVec outer_attrs,
7609 : : location_t pratt_parsed_loc)
7610 : : {
7611 : 1 : location_t locus = pratt_parsed_loc;
7612 : 1 : if (locus == UNKNOWN_LOCATION)
7613 : : {
7614 : 0 : locus = lexer.peek_token ()->get_locus ();
7615 : 0 : skip_token (TRY);
7616 : : }
7617 : :
7618 : 1 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
7619 : :
7620 : 1 : if (!block_expr)
7621 : : {
7622 : 0 : Error error (lexer.peek_token ()->get_locus (),
7623 : : "failed to parse try block expression");
7624 : 0 : add_error (std::move (error));
7625 : :
7626 : 0 : return nullptr;
7627 : 0 : }
7628 : :
7629 : : return std::unique_ptr<AST::TryExpr> (
7630 : 1 : new AST::TryExpr (std::move (block_expr), std::move (outer_attrs), locus));
7631 : 1 : }
7632 : :
7633 : : /* Parses a break expression (including any label to break to AND any return
7634 : : * expression). */
7635 : : template <typename ManagedTokenSource>
7636 : : std::unique_ptr<AST::BreakExpr>
7637 : 69 : Parser<ManagedTokenSource>::parse_break_expr (AST::AttrVec outer_attrs,
7638 : : location_t pratt_parsed_loc)
7639 : : {
7640 : 69 : location_t locus = pratt_parsed_loc;
7641 : 69 : if (locus == UNKNOWN_LOCATION)
7642 : : {
7643 : 0 : locus = lexer.peek_token ()->get_locus ();
7644 : 0 : skip_token (BREAK);
7645 : : }
7646 : :
7647 : 69 : auto parsed_label = parse_lifetime (false);
7648 : 69 : auto label = (parsed_label)
7649 : 69 : ? tl::optional<AST::Lifetime> (parsed_label.value ())
7650 : : : tl::nullopt;
7651 : :
7652 : : // parse break return expression if it exists
7653 : 69 : ParseRestrictions restrictions;
7654 : 69 : restrictions.expr_can_be_null = true;
7655 : 69 : std::unique_ptr<AST::Expr> return_expr
7656 : 69 : = parse_expr (AST::AttrVec (), restrictions);
7657 : :
7658 : : return std::unique_ptr<AST::BreakExpr> (
7659 : 90 : new AST::BreakExpr (std::move (label), std::move (return_expr),
7660 : 69 : std::move (outer_attrs), locus));
7661 : 90 : }
7662 : :
7663 : : // Parses a continue expression (including any label to continue from).
7664 : : template <typename ManagedTokenSource>
7665 : : std::unique_ptr<AST::ContinueExpr>
7666 : 10 : Parser<ManagedTokenSource>::parse_continue_expr (AST::AttrVec outer_attrs,
7667 : : location_t pratt_parsed_loc)
7668 : : {
7669 : 10 : location_t locus = pratt_parsed_loc;
7670 : 10 : if (locus == UNKNOWN_LOCATION)
7671 : : {
7672 : 0 : locus = lexer.peek_token ()->get_locus ();
7673 : 0 : skip_token (CONTINUE);
7674 : : }
7675 : :
7676 : 10 : auto parsed_label = parse_lifetime (false);
7677 : 10 : auto label = (parsed_label)
7678 : 10 : ? tl::optional<AST::Lifetime> (parsed_label.value ())
7679 : : : tl::nullopt;
7680 : :
7681 : : return std::unique_ptr<AST::ContinueExpr> (
7682 : 11 : new AST::ContinueExpr (std::move (label), std::move (outer_attrs), locus));
7683 : 10 : }
7684 : :
7685 : : // Parses a loop label used in loop expressions.
7686 : : template <typename ManagedTokenSource>
7687 : : tl::expected<AST::LoopLabel, ParseLoopLabelError>
7688 : 34 : Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
7689 : : {
7690 : : // parse lifetime - if doesn't exist, assume no label
7691 : 34 : if (tok->get_id () != LIFETIME)
7692 : : {
7693 : : // not necessarily an error
7694 : : return tl::unexpected<ParseLoopLabelError> (
7695 : 0 : ParseLoopLabelError::NOT_LOOP_LABEL);
7696 : : }
7697 : : /* FIXME: check for named lifetime requirement here? or check in semantic
7698 : : * analysis phase? */
7699 : 34 : AST::Lifetime label = lifetime_from_token (tok);
7700 : :
7701 : 34 : if (!skip_token (COLON))
7702 : : {
7703 : : // skip somewhere?
7704 : : return tl::unexpected<ParseLoopLabelError> (
7705 : 0 : ParseLoopLabelError::MISSING_COLON);
7706 : : }
7707 : :
7708 : : return tl::expected<AST::LoopLabel, ParseLoopLabelError> (
7709 : 34 : AST::LoopLabel (std::move (label), tok->get_locus ()));
7710 : 34 : }
7711 : :
7712 : : /* Parses an if expression of any kind, including with else, else if, else if
7713 : : * let, and neither. Note that any outer attributes will be ignored because if
7714 : : * expressions don't support them. */
7715 : : template <typename ManagedTokenSource>
7716 : : std::unique_ptr<AST::IfExpr>
7717 : 1652 : Parser<ManagedTokenSource>::parse_if_expr (AST::AttrVec outer_attrs,
7718 : : location_t pratt_parsed_loc)
7719 : : {
7720 : : // TODO: make having outer attributes an error?
7721 : 1652 : location_t locus = pratt_parsed_loc;
7722 : 1652 : if (locus == UNKNOWN_LOCATION)
7723 : : {
7724 : 323 : locus = lexer.peek_token ()->get_locus ();
7725 : 323 : if (!skip_token (IF))
7726 : : {
7727 : 0 : skip_after_end_block ();
7728 : 0 : return nullptr;
7729 : : }
7730 : : }
7731 : :
7732 : : // detect accidental if let
7733 : 3304 : if (lexer.peek_token ()->get_id () == LET)
7734 : : {
7735 : 0 : Error error (lexer.peek_token ()->get_locus (),
7736 : : "if let expression probably exists, but is being parsed "
7737 : : "as an if expression. This may be a parser error");
7738 : 0 : add_error (std::move (error));
7739 : :
7740 : : // skip somewhere?
7741 : 0 : return nullptr;
7742 : 0 : }
7743 : :
7744 : : /* parse required condition expr - HACK to prevent struct expr from being
7745 : : * parsed */
7746 : 1652 : ParseRestrictions no_struct_expr;
7747 : 1652 : no_struct_expr.can_be_struct_expr = false;
7748 : 1652 : std::unique_ptr<AST::Expr> condition = parse_expr ({}, no_struct_expr);
7749 : 1652 : if (condition == nullptr)
7750 : : {
7751 : 0 : Error error (lexer.peek_token ()->get_locus (),
7752 : : "failed to parse condition expression in if expression");
7753 : 0 : add_error (std::move (error));
7754 : :
7755 : : // skip somewhere?
7756 : 0 : return nullptr;
7757 : 0 : }
7758 : :
7759 : : // parse required block expr
7760 : 1652 : std::unique_ptr<AST::BlockExpr> if_body = parse_block_expr ();
7761 : 1652 : if (if_body == nullptr)
7762 : : {
7763 : 1 : Error error (lexer.peek_token ()->get_locus (),
7764 : : "failed to parse if body block expression in if expression");
7765 : 1 : add_error (std::move (error));
7766 : :
7767 : : // skip somewhere?
7768 : 1 : return nullptr;
7769 : 1 : }
7770 : :
7771 : : // branch to parse end or else (and then else, else if, or else if let)
7772 : 3302 : if (lexer.peek_token ()->get_id () != ELSE)
7773 : : {
7774 : : // single selection - end of if expression
7775 : : return std::unique_ptr<AST::IfExpr> (
7776 : 441 : new AST::IfExpr (std::move (condition), std::move (if_body),
7777 : 441 : std::move (outer_attrs), locus));
7778 : : }
7779 : : else
7780 : : {
7781 : : // double or multiple selection - branch on end, else if, or else if let
7782 : :
7783 : : // skip "else"
7784 : 1210 : lexer.skip_token ();
7785 : :
7786 : : // branch on whether next token is '{' or 'if'
7787 : 1210 : const_TokenPtr t = lexer.peek_token ();
7788 : 1210 : switch (t->get_id ())
7789 : : {
7790 : 887 : case LEFT_CURLY:
7791 : : {
7792 : : // double selection - else
7793 : : // parse else block expr (required)
7794 : 887 : std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
7795 : 887 : if (else_body == nullptr)
7796 : : {
7797 : 0 : Error error (lexer.peek_token ()->get_locus (),
7798 : : "failed to parse else body block expression in "
7799 : : "if expression");
7800 : 0 : add_error (std::move (error));
7801 : :
7802 : : // skip somewhere?
7803 : 0 : return nullptr;
7804 : 0 : }
7805 : :
7806 : 887 : return std::unique_ptr<AST::IfExprConseqElse> (
7807 : 887 : new AST::IfExprConseqElse (std::move (condition),
7808 : : std::move (if_body),
7809 : : std::move (else_body),
7810 : 887 : std::move (outer_attrs), locus));
7811 : 887 : }
7812 : 323 : case IF:
7813 : : {
7814 : : // multiple selection - else if or else if let
7815 : : // branch on whether next token is 'let' or not
7816 : 646 : if (lexer.peek_token (1)->get_id () == LET)
7817 : : {
7818 : : // parse if let expr (required)
7819 : 1 : std::unique_ptr<AST::IfLetExpr> if_let_expr
7820 : 1 : = parse_if_let_expr ();
7821 : 1 : if (if_let_expr == nullptr)
7822 : : {
7823 : 0 : Error error (lexer.peek_token ()->get_locus (),
7824 : : "failed to parse (else) if let expression "
7825 : : "after if expression");
7826 : 0 : add_error (std::move (error));
7827 : :
7828 : : // skip somewhere?
7829 : 0 : return nullptr;
7830 : 0 : }
7831 : :
7832 : 1 : return std::unique_ptr<AST::IfExprConseqElse> (
7833 : 1 : new AST::IfExprConseqElse (std::move (condition),
7834 : : std::move (if_body),
7835 : : std::move (if_let_expr),
7836 : 1 : std::move (outer_attrs), locus));
7837 : 1 : }
7838 : : else
7839 : : {
7840 : : // parse if expr (required)
7841 : 322 : std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
7842 : 322 : if (if_expr == nullptr)
7843 : : {
7844 : 0 : Error error (lexer.peek_token ()->get_locus (),
7845 : : "failed to parse (else) if expression after "
7846 : : "if expression");
7847 : 0 : add_error (std::move (error));
7848 : :
7849 : : // skip somewhere?
7850 : 0 : return nullptr;
7851 : 0 : }
7852 : :
7853 : 322 : return std::unique_ptr<AST::IfExprConseqElse> (
7854 : 322 : new AST::IfExprConseqElse (std::move (condition),
7855 : : std::move (if_body),
7856 : : std::move (if_expr),
7857 : 322 : std::move (outer_attrs), locus));
7858 : 322 : }
7859 : : }
7860 : 0 : default:
7861 : : // error - invalid token
7862 : 0 : add_error (Error (t->get_locus (),
7863 : : "unexpected token %qs after else in if expression",
7864 : : t->get_token_description ()));
7865 : :
7866 : : // skip somewhere?
7867 : 0 : return nullptr;
7868 : : }
7869 : 1210 : }
7870 : 1652 : }
7871 : :
7872 : : /* Parses an if let expression of any kind, including with else, else if, else
7873 : : * if let, and none. Note that any outer attributes will be ignored as if let
7874 : : * expressions don't support them. */
7875 : : template <typename ManagedTokenSource>
7876 : : std::unique_ptr<AST::IfLetExpr>
7877 : 31 : Parser<ManagedTokenSource>::parse_if_let_expr (AST::AttrVec outer_attrs,
7878 : : location_t pratt_parsed_loc)
7879 : : {
7880 : : // TODO: make having outer attributes an error?
7881 : 31 : location_t locus = pratt_parsed_loc;
7882 : 31 : if (locus == UNKNOWN_LOCATION)
7883 : : {
7884 : 1 : locus = lexer.peek_token ()->get_locus ();
7885 : 1 : if (!skip_token (IF))
7886 : : {
7887 : 0 : skip_after_end_block ();
7888 : 0 : return nullptr;
7889 : : }
7890 : : }
7891 : :
7892 : : // detect accidental if expr parsed as if let expr
7893 : 62 : if (lexer.peek_token ()->get_id () != LET)
7894 : : {
7895 : 0 : Error error (lexer.peek_token ()->get_locus (),
7896 : : "if expression probably exists, but is being parsed as an "
7897 : : "if let expression. This may be a parser error");
7898 : 0 : add_error (std::move (error));
7899 : :
7900 : : // skip somewhere?
7901 : 0 : return nullptr;
7902 : 0 : }
7903 : 31 : lexer.skip_token ();
7904 : :
7905 : : // parse match arm patterns (which are required)
7906 : 31 : std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
7907 : : = parse_match_arm_patterns (EQUAL);
7908 : 31 : if (match_arm_patterns.empty ())
7909 : : {
7910 : 0 : Error error (
7911 : 0 : lexer.peek_token ()->get_locus (),
7912 : : "failed to parse any match arm patterns in if let expression");
7913 : 0 : add_error (std::move (error));
7914 : :
7915 : : // skip somewhere?
7916 : 0 : return nullptr;
7917 : 0 : }
7918 : :
7919 : 31 : if (!skip_token (EQUAL))
7920 : : {
7921 : : // skip somewhere?
7922 : 0 : return nullptr;
7923 : : }
7924 : :
7925 : : // parse expression (required) - HACK to prevent struct expr being parsed
7926 : 31 : ParseRestrictions no_struct_expr;
7927 : 31 : no_struct_expr.can_be_struct_expr = false;
7928 : 31 : std::unique_ptr<AST::Expr> scrutinee_expr = parse_expr ({}, no_struct_expr);
7929 : 31 : if (scrutinee_expr == nullptr)
7930 : : {
7931 : 0 : Error error (lexer.peek_token ()->get_locus (),
7932 : : "failed to parse scrutinee expression in if let expression");
7933 : 0 : add_error (std::move (error));
7934 : :
7935 : : // skip somewhere?
7936 : 0 : return nullptr;
7937 : 0 : }
7938 : : /* TODO: check for expression not being a struct expression or lazy boolean
7939 : : * expression here? or actually probably in semantic analysis. */
7940 : :
7941 : : // parse block expression (required)
7942 : 31 : std::unique_ptr<AST::BlockExpr> if_let_body = parse_block_expr ();
7943 : 31 : if (if_let_body == nullptr)
7944 : : {
7945 : 0 : Error error (
7946 : 0 : lexer.peek_token ()->get_locus (),
7947 : : "failed to parse if let body block expression in if let expression");
7948 : 0 : add_error (std::move (error));
7949 : :
7950 : : // skip somewhere?
7951 : 0 : return nullptr;
7952 : 0 : }
7953 : :
7954 : : // branch to parse end or else (and then else, else if, or else if let)
7955 : 62 : if (lexer.peek_token ()->get_id () != ELSE)
7956 : : {
7957 : : // single selection - end of if let expression
7958 : : return std::unique_ptr<AST::IfLetExpr> (
7959 : 19 : new AST::IfLetExpr (std::move (match_arm_patterns),
7960 : : std::move (scrutinee_expr), std::move (if_let_body),
7961 : 19 : std::move (outer_attrs), locus));
7962 : : }
7963 : : else
7964 : : {
7965 : : // double or multiple selection - branch on end, else if, or else if let
7966 : :
7967 : : // skip "else"
7968 : 12 : lexer.skip_token ();
7969 : :
7970 : : // branch on whether next token is '{' or 'if'
7971 : 12 : const_TokenPtr t = lexer.peek_token ();
7972 : 12 : switch (t->get_id ())
7973 : : {
7974 : 11 : case LEFT_CURLY:
7975 : : {
7976 : : // double selection - else
7977 : : // parse else block expr (required)
7978 : 11 : std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
7979 : 11 : if (else_body == nullptr)
7980 : : {
7981 : 0 : Error error (lexer.peek_token ()->get_locus (),
7982 : : "failed to parse else body block expression in "
7983 : : "if let expression");
7984 : 0 : add_error (std::move (error));
7985 : :
7986 : : // skip somewhere?
7987 : 0 : return nullptr;
7988 : 0 : }
7989 : :
7990 : 11 : return std::unique_ptr<AST::IfLetExprConseqElse> (
7991 : 11 : new AST::IfLetExprConseqElse (std::move (match_arm_patterns),
7992 : : std::move (scrutinee_expr),
7993 : : std::move (if_let_body),
7994 : : std::move (else_body),
7995 : 11 : std::move (outer_attrs), locus));
7996 : 11 : }
7997 : 1 : case IF:
7998 : : {
7999 : : // multiple selection - else if or else if let
8000 : : // branch on whether next token is 'let' or not
8001 : 2 : if (lexer.peek_token (1)->get_id () == LET)
8002 : : {
8003 : : // parse if let expr (required)
8004 : 0 : std::unique_ptr<AST::IfLetExpr> if_let_expr
8005 : 0 : = parse_if_let_expr ();
8006 : 0 : if (if_let_expr == nullptr)
8007 : : {
8008 : 0 : Error error (lexer.peek_token ()->get_locus (),
8009 : : "failed to parse (else) if let expression "
8010 : : "after if let expression");
8011 : 0 : add_error (std::move (error));
8012 : :
8013 : : // skip somewhere?
8014 : 0 : return nullptr;
8015 : 0 : }
8016 : :
8017 : 0 : return std::unique_ptr<AST::IfLetExprConseqElse> (
8018 : 0 : new AST::IfLetExprConseqElse (
8019 : : std::move (match_arm_patterns), std::move (scrutinee_expr),
8020 : : std::move (if_let_body), std::move (if_let_expr),
8021 : 0 : std::move (outer_attrs), locus));
8022 : 0 : }
8023 : : else
8024 : : {
8025 : : // parse if expr (required)
8026 : 1 : std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
8027 : 1 : if (if_expr == nullptr)
8028 : : {
8029 : 0 : Error error (lexer.peek_token ()->get_locus (),
8030 : : "failed to parse (else) if expression after "
8031 : : "if let expression");
8032 : 0 : add_error (std::move (error));
8033 : :
8034 : : // skip somewhere?
8035 : 0 : return nullptr;
8036 : 0 : }
8037 : :
8038 : 1 : return std::unique_ptr<AST::IfLetExprConseqElse> (
8039 : 1 : new AST::IfLetExprConseqElse (
8040 : : std::move (match_arm_patterns), std::move (scrutinee_expr),
8041 : : std::move (if_let_body), std::move (if_expr),
8042 : 1 : std::move (outer_attrs), locus));
8043 : 1 : }
8044 : : }
8045 : 0 : default:
8046 : : // error - invalid token
8047 : 0 : add_error (
8048 : 0 : Error (t->get_locus (),
8049 : : "unexpected token %qs after else in if let expression",
8050 : : t->get_token_description ()));
8051 : :
8052 : : // skip somewhere?
8053 : 0 : return nullptr;
8054 : : }
8055 : 12 : }
8056 : 31 : }
8057 : :
8058 : : /* TODO: possibly decide on different method of handling label (i.e. not
8059 : : * parameter) */
8060 : :
8061 : : /* Parses a "loop" infinite loop expression. Label is not parsed and should be
8062 : : * parsed via parse_labelled_loop_expr, which would call this. */
8063 : : template <typename ManagedTokenSource>
8064 : : std::unique_ptr<AST::LoopExpr>
8065 : 101 : Parser<ManagedTokenSource>::parse_loop_expr (AST::AttrVec outer_attrs,
8066 : : tl::optional<AST::LoopLabel> label,
8067 : : location_t pratt_parsed_loc)
8068 : : {
8069 : 101 : location_t locus = pratt_parsed_loc;
8070 : 101 : if (locus == UNKNOWN_LOCATION)
8071 : : {
8072 : 32 : if (label)
8073 : 32 : locus = label->get_locus ();
8074 : : else
8075 : 0 : locus = lexer.peek_token ()->get_locus ();
8076 : :
8077 : 32 : if (!skip_token (LOOP))
8078 : : {
8079 : 0 : skip_after_end_block ();
8080 : 0 : return nullptr;
8081 : : }
8082 : : }
8083 : : else
8084 : : {
8085 : 69 : if (label)
8086 : 0 : locus = label->get_locus ();
8087 : : }
8088 : :
8089 : : // parse loop body, which is required
8090 : 101 : std::unique_ptr<AST::BlockExpr> loop_body = parse_block_expr ();
8091 : 101 : if (loop_body == nullptr)
8092 : : {
8093 : 1 : Error error (lexer.peek_token ()->get_locus (),
8094 : : "could not parse loop body in (infinite) loop expression");
8095 : 1 : add_error (std::move (error));
8096 : :
8097 : 1 : return nullptr;
8098 : 1 : }
8099 : :
8100 : : return std::unique_ptr<AST::LoopExpr> (
8101 : 132 : new AST::LoopExpr (std::move (loop_body), locus, std::move (label),
8102 : 100 : std::move (outer_attrs)));
8103 : 101 : }
8104 : :
8105 : : /* Parses a "while" loop expression. Label is not parsed and should be parsed
8106 : : * via parse_labelled_loop_expr, which would call this. */
8107 : : template <typename ManagedTokenSource>
8108 : : std::unique_ptr<AST::WhileLoopExpr>
8109 : 66 : Parser<ManagedTokenSource>::parse_while_loop_expr (
8110 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
8111 : : location_t pratt_parsed_loc)
8112 : : {
8113 : 66 : location_t locus = pratt_parsed_loc;
8114 : 66 : if (locus == UNKNOWN_LOCATION)
8115 : : {
8116 : 2 : if (label)
8117 : 2 : locus = label->get_locus ();
8118 : : else
8119 : 0 : locus = lexer.peek_token ()->get_locus ();
8120 : :
8121 : 2 : if (!skip_token (WHILE))
8122 : : {
8123 : 0 : skip_after_end_block ();
8124 : 0 : return nullptr;
8125 : : }
8126 : : }
8127 : : else
8128 : : {
8129 : 64 : if (label)
8130 : 0 : locus = label->get_locus ();
8131 : : }
8132 : :
8133 : : // ensure it isn't a while let loop
8134 : 132 : if (lexer.peek_token ()->get_id () == LET)
8135 : : {
8136 : 0 : Error error (lexer.peek_token ()->get_locus (),
8137 : : "appears to be while let loop but is being parsed by "
8138 : : "while loop - this may be a compiler issue");
8139 : 0 : add_error (std::move (error));
8140 : :
8141 : : // skip somewhere?
8142 : 0 : return nullptr;
8143 : 0 : }
8144 : :
8145 : : // parse loop predicate (required) with HACK to prevent struct expr parsing
8146 : 66 : ParseRestrictions no_struct_expr;
8147 : 66 : no_struct_expr.can_be_struct_expr = false;
8148 : 66 : std::unique_ptr<AST::Expr> predicate = parse_expr ({}, no_struct_expr);
8149 : 66 : if (predicate == nullptr)
8150 : : {
8151 : 0 : Error error (lexer.peek_token ()->get_locus (),
8152 : : "failed to parse predicate expression in while loop");
8153 : 0 : add_error (std::move (error));
8154 : :
8155 : : // skip somewhere?
8156 : 0 : return nullptr;
8157 : 0 : }
8158 : : /* TODO: check that it isn't struct expression here? actually, probably in
8159 : : * semantic analysis */
8160 : :
8161 : : // parse loop body (required)
8162 : 66 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8163 : 66 : if (body == nullptr)
8164 : : {
8165 : 0 : Error error (lexer.peek_token ()->get_locus (),
8166 : : "failed to parse loop body block expression in while loop");
8167 : 0 : add_error (std::move (error));
8168 : :
8169 : : // skip somewhere
8170 : 0 : return nullptr;
8171 : 0 : }
8172 : :
8173 : : return std::unique_ptr<AST::WhileLoopExpr> (
8174 : 68 : new AST::WhileLoopExpr (std::move (predicate), std::move (body), locus,
8175 : 66 : std::move (label), std::move (outer_attrs)));
8176 : 66 : }
8177 : :
8178 : : /* Parses a "while let" loop expression. Label is not parsed and should be
8179 : : * parsed via parse_labelled_loop_expr, which would call this. */
8180 : : template <typename ManagedTokenSource>
8181 : : std::unique_ptr<AST::WhileLetLoopExpr>
8182 : 3 : Parser<ManagedTokenSource>::parse_while_let_loop_expr (
8183 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
8184 : : {
8185 : 3 : location_t locus = UNKNOWN_LOCATION;
8186 : 3 : if (label)
8187 : 0 : locus = label->get_locus ();
8188 : : else
8189 : 6 : locus = lexer.peek_token ()->get_locus ();
8190 : 3 : maybe_skip_token (WHILE);
8191 : :
8192 : : /* check for possible accidental recognition of a while loop as a while let
8193 : : * loop */
8194 : 6 : if (lexer.peek_token ()->get_id () != LET)
8195 : : {
8196 : 0 : Error error (lexer.peek_token ()->get_locus (),
8197 : : "appears to be a while loop but is being parsed by "
8198 : : "while let loop - this may be a compiler issue");
8199 : 0 : add_error (std::move (error));
8200 : :
8201 : : // skip somewhere
8202 : 0 : return nullptr;
8203 : 0 : }
8204 : : // as this token is definitely let now, save the computation of comparison
8205 : 3 : lexer.skip_token ();
8206 : :
8207 : : // parse predicate patterns
8208 : 3 : std::vector<std::unique_ptr<AST::Pattern>> predicate_patterns
8209 : : = parse_match_arm_patterns (EQUAL);
8210 : : // TODO: have to ensure that there is at least 1 pattern?
8211 : :
8212 : 3 : if (!skip_token (EQUAL))
8213 : : {
8214 : : // skip somewhere?
8215 : 0 : return nullptr;
8216 : : }
8217 : :
8218 : : /* parse predicate expression, which is required (and HACK to prevent struct
8219 : : * expr) */
8220 : 3 : ParseRestrictions no_struct_expr;
8221 : 3 : no_struct_expr.can_be_struct_expr = false;
8222 : 3 : std::unique_ptr<AST::Expr> predicate_expr = parse_expr ({}, no_struct_expr);
8223 : 3 : if (predicate_expr == nullptr)
8224 : : {
8225 : 0 : Error error (lexer.peek_token ()->get_locus (),
8226 : : "failed to parse predicate expression in while let loop");
8227 : 0 : add_error (std::move (error));
8228 : :
8229 : : // skip somewhere?
8230 : 0 : return nullptr;
8231 : 0 : }
8232 : : /* TODO: ensure that struct expression is not parsed? Actually, probably in
8233 : : * semantic analysis. */
8234 : :
8235 : : // parse loop body, which is required
8236 : 3 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8237 : 3 : if (body == nullptr)
8238 : : {
8239 : 0 : Error error (lexer.peek_token ()->get_locus (),
8240 : : "failed to parse block expr (loop body) of while let loop");
8241 : 0 : add_error (std::move (error));
8242 : :
8243 : : // skip somewhere?
8244 : 0 : return nullptr;
8245 : 0 : }
8246 : :
8247 : 3 : return std::unique_ptr<AST::WhileLetLoopExpr> (new AST::WhileLetLoopExpr (
8248 : : std::move (predicate_patterns), std::move (predicate_expr),
8249 : 3 : std::move (body), locus, std::move (label), std::move (outer_attrs)));
8250 : 3 : }
8251 : :
8252 : : /* Parses a "for" iterative loop. Label is not parsed and should be parsed via
8253 : : * parse_labelled_loop_expr, which would call this. */
8254 : : template <typename ManagedTokenSource>
8255 : : std::unique_ptr<AST::ForLoopExpr>
8256 : 19 : Parser<ManagedTokenSource>::parse_for_loop_expr (
8257 : : AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label)
8258 : : {
8259 : 19 : location_t locus = UNKNOWN_LOCATION;
8260 : 19 : if (label)
8261 : 0 : locus = label->get_locus ();
8262 : : else
8263 : 38 : locus = lexer.peek_token ()->get_locus ();
8264 : 19 : maybe_skip_token (FOR);
8265 : :
8266 : : // parse pattern, which is required
8267 : 19 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
8268 : 19 : if (pattern == nullptr)
8269 : : {
8270 : 0 : Error error (lexer.peek_token ()->get_locus (),
8271 : : "failed to parse iterator pattern in for loop");
8272 : 0 : add_error (std::move (error));
8273 : :
8274 : : // skip somewhere?
8275 : 0 : return nullptr;
8276 : 0 : }
8277 : :
8278 : 19 : if (!skip_token (IN))
8279 : : {
8280 : : // skip somewhere?
8281 : 0 : return nullptr;
8282 : : }
8283 : :
8284 : : /* parse iterator expression, which is required - also HACK to prevent
8285 : : * struct expr */
8286 : 19 : ParseRestrictions no_struct_expr;
8287 : 19 : no_struct_expr.can_be_struct_expr = false;
8288 : 19 : std::unique_ptr<AST::Expr> expr = parse_expr ({}, no_struct_expr);
8289 : 19 : if (expr == nullptr)
8290 : : {
8291 : 0 : Error error (lexer.peek_token ()->get_locus (),
8292 : : "failed to parse iterator expression in for loop");
8293 : 0 : add_error (std::move (error));
8294 : :
8295 : : // skip somewhere?
8296 : 0 : return nullptr;
8297 : 0 : }
8298 : : // TODO: check to ensure this isn't struct expr? Or in semantic analysis.
8299 : :
8300 : : // parse loop body, which is required
8301 : 19 : std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
8302 : 19 : if (body == nullptr)
8303 : : {
8304 : 0 : Error error (lexer.peek_token ()->get_locus (),
8305 : : "failed to parse loop body block expression in for loop");
8306 : 0 : add_error (std::move (error));
8307 : :
8308 : : // skip somewhere?
8309 : 0 : return nullptr;
8310 : 0 : }
8311 : :
8312 : : return std::unique_ptr<AST::ForLoopExpr> (
8313 : 19 : new AST::ForLoopExpr (std::move (pattern), std::move (expr),
8314 : : std::move (body), locus, std::move (label),
8315 : 19 : std::move (outer_attrs)));
8316 : 19 : }
8317 : :
8318 : : // Parses a loop expression with label (any kind of loop - disambiguates).
8319 : : template <typename ManagedTokenSource>
8320 : : std::unique_ptr<AST::Expr>
8321 : 34 : Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
8322 : : AST::AttrVec outer_attrs)
8323 : : {
8324 : : /* TODO: decide whether it should not work if there is no label, or parse it
8325 : : * with no label at the moment, I will make it not work with no label
8326 : : * because that's the implication. */
8327 : :
8328 : 34 : if (tok->get_id () != LIFETIME)
8329 : : {
8330 : 0 : Error error (tok->get_locus (),
8331 : : "expected lifetime in labelled loop expr (to parse loop "
8332 : : "label) - found %qs",
8333 : : tok->get_token_description ());
8334 : 0 : add_error (std::move (error));
8335 : :
8336 : : // skip?
8337 : 0 : return nullptr;
8338 : 0 : }
8339 : :
8340 : : // parse loop label (required)
8341 : : // TODO: Convert this return type to tl::expected instead of tl::optional
8342 : 68 : auto parsed_label = parse_loop_label (tok);
8343 : 34 : if (!parsed_label)
8344 : : {
8345 : 0 : Error error (lexer.peek_token ()->get_locus (),
8346 : : "failed to parse loop label in labelled loop expr");
8347 : 0 : add_error (std::move (error));
8348 : :
8349 : : // skip?
8350 : 0 : return nullptr;
8351 : 0 : }
8352 : :
8353 : 34 : auto label = parsed_label
8354 : : ? tl::optional<AST::LoopLabel> (parsed_label.value ())
8355 : : : tl::nullopt;
8356 : :
8357 : : // branch on next token
8358 : 34 : const_TokenPtr t = lexer.peek_token ();
8359 : 34 : switch (t->get_id ())
8360 : : {
8361 : 32 : case LOOP:
8362 : 64 : return parse_loop_expr (std::move (outer_attrs), std::move (label));
8363 : 0 : case FOR:
8364 : 0 : return parse_for_loop_expr (std::move (outer_attrs), std::move (label));
8365 : 2 : case WHILE:
8366 : : // further disambiguate into while vs while let
8367 : 4 : if (lexer.peek_token (1)->get_id () == LET)
8368 : : {
8369 : 0 : return parse_while_let_loop_expr (std::move (outer_attrs),
8370 : 0 : std::move (label));
8371 : : }
8372 : : else
8373 : : {
8374 : 4 : return parse_while_loop_expr (std::move (outer_attrs),
8375 : 2 : std::move (label));
8376 : : }
8377 : 0 : case LEFT_CURLY:
8378 : 0 : return parse_block_expr (std::move (outer_attrs), std::move (label));
8379 : 0 : default:
8380 : : // error
8381 : 0 : add_error (Error (t->get_locus (),
8382 : : "unexpected token %qs when parsing labelled loop",
8383 : : t->get_token_description ()));
8384 : :
8385 : : // skip?
8386 : 0 : return nullptr;
8387 : : }
8388 : 102 : }
8389 : :
8390 : : // Parses a match expression.
8391 : : template <typename ManagedTokenSource>
8392 : : std::unique_ptr<AST::MatchExpr>
8393 : 823 : Parser<ManagedTokenSource>::parse_match_expr (AST::AttrVec outer_attrs,
8394 : : location_t pratt_parsed_loc)
8395 : : {
8396 : 823 : location_t locus = pratt_parsed_loc;
8397 : 823 : if (locus == UNKNOWN_LOCATION)
8398 : : {
8399 : 0 : locus = lexer.peek_token ()->get_locus ();
8400 : 0 : skip_token (MATCH_KW);
8401 : : }
8402 : :
8403 : : /* parse scrutinee expression, which is required (and HACK to prevent struct
8404 : : * expr) */
8405 : 823 : ParseRestrictions no_struct_expr;
8406 : 823 : no_struct_expr.can_be_struct_expr = false;
8407 : 823 : std::unique_ptr<AST::Expr> scrutinee = parse_expr ({}, no_struct_expr);
8408 : 823 : if (scrutinee == nullptr)
8409 : : {
8410 : 0 : Error error (lexer.peek_token ()->get_locus (),
8411 : : "failed to parse scrutinee expression in match expression");
8412 : 0 : add_error (std::move (error));
8413 : :
8414 : : // skip somewhere?
8415 : 0 : return nullptr;
8416 : 0 : }
8417 : : /* TODO: check for scrutinee expr not being struct expr? or do so in
8418 : : * semantic analysis */
8419 : :
8420 : 823 : if (!skip_token (LEFT_CURLY))
8421 : : {
8422 : : // skip somewhere?
8423 : 0 : return nullptr;
8424 : : }
8425 : :
8426 : : // parse inner attributes (if they exist)
8427 : 823 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8428 : :
8429 : : // parse match arms (if they exist)
8430 : : // std::vector<std::unique_ptr<AST::MatchCase> > match_arms;
8431 : 823 : std::vector<AST::MatchCase> match_arms;
8432 : :
8433 : : // parse match cases
8434 : 7370 : while (lexer.peek_token ()->get_id () != RIGHT_CURLY)
8435 : : {
8436 : : // parse match arm itself, which is required
8437 : 1914 : AST::MatchArm arm = parse_match_arm ();
8438 : 1914 : if (arm.is_error ())
8439 : : {
8440 : : // TODO is this worth throwing everything away?
8441 : 0 : Error error (lexer.peek_token ()->get_locus (),
8442 : : "failed to parse match arm in match arms");
8443 : 0 : add_error (std::move (error));
8444 : :
8445 : 0 : return nullptr;
8446 : 0 : }
8447 : :
8448 : 1914 : if (!skip_token (MATCH_ARROW))
8449 : : {
8450 : : // skip after somewhere?
8451 : : // TODO is returning here a good idea? or is break better?
8452 : 0 : return nullptr;
8453 : : }
8454 : :
8455 : 1914 : ParseRestrictions restrictions;
8456 : 1914 : restrictions.expr_can_be_stmt = true;
8457 : :
8458 : 1914 : std::unique_ptr<AST::Expr> expr = parse_expr ({}, restrictions);
8459 : :
8460 : 1914 : if (expr == nullptr)
8461 : : {
8462 : 0 : Error error (lexer.peek_token ()->get_locus (),
8463 : : "failed to parse expr in match arm in match expr");
8464 : 0 : add_error (std::move (error));
8465 : :
8466 : : // skip somewhere?
8467 : 0 : return nullptr;
8468 : 0 : }
8469 : :
8470 : 1914 : bool is_expr_without_block = expr->is_expr_without_block ();
8471 : :
8472 : 3828 : match_arms.push_back (AST::MatchCase (std::move (arm), std::move (expr)));
8473 : :
8474 : : // handle comma presence
8475 : 3828 : if (lexer.peek_token ()->get_id () != COMMA)
8476 : : {
8477 : 436 : if (!is_expr_without_block)
8478 : : {
8479 : : // allowed even if not final case
8480 : : continue;
8481 : : }
8482 : 9 : else if (is_expr_without_block
8483 : 18 : && lexer.peek_token ()->get_id () != RIGHT_CURLY)
8484 : : {
8485 : : // not allowed if not final case
8486 : 1 : Error error (lexer.peek_token ()->get_locus (),
8487 : : "exprwithoutblock requires comma after match case "
8488 : : "expression in match arm (if not final case)");
8489 : 1 : add_error (std::move (error));
8490 : :
8491 : 1 : return nullptr;
8492 : 1 : }
8493 : : else
8494 : : {
8495 : : // otherwise, must be final case, so fine
8496 : : break;
8497 : : }
8498 : : }
8499 : 1478 : lexer.skip_token ();
8500 : : }
8501 : :
8502 : 822 : if (!skip_token (RIGHT_CURLY))
8503 : : {
8504 : : // skip somewhere?
8505 : 0 : return nullptr;
8506 : : }
8507 : :
8508 : 822 : match_arms.shrink_to_fit ();
8509 : :
8510 : : return std::unique_ptr<AST::MatchExpr> (
8511 : 822 : new AST::MatchExpr (std::move (scrutinee), std::move (match_arms),
8512 : : std::move (inner_attrs), std::move (outer_attrs),
8513 : 822 : locus));
8514 : 823 : }
8515 : :
8516 : : // Parses the "pattern" part of the match arm (the 'case x:' equivalent).
8517 : : template <typename ManagedTokenSource>
8518 : : AST::MatchArm
8519 : 1914 : Parser<ManagedTokenSource>::parse_match_arm ()
8520 : : {
8521 : : // parse optional outer attributes
8522 : 1914 : AST::AttrVec outer_attrs = parse_outer_attributes ();
8523 : :
8524 : : // DEBUG
8525 : 1914 : rust_debug ("about to start parsing match arm patterns");
8526 : :
8527 : : // break early if find right curly
8528 : 3828 : if (lexer.peek_token ()->get_id () == RIGHT_CURLY)
8529 : : {
8530 : : // not an error
8531 : 0 : return AST::MatchArm::create_error ();
8532 : : }
8533 : :
8534 : : // parse match arm patterns - at least 1 is required
8535 : 1914 : std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
8536 : : = parse_match_arm_patterns (RIGHT_CURLY);
8537 : 1914 : if (match_arm_patterns.empty ())
8538 : : {
8539 : 0 : Error error (lexer.peek_token ()->get_locus (),
8540 : : "failed to parse any patterns in match arm");
8541 : 0 : add_error (std::move (error));
8542 : :
8543 : : // skip somewhere?
8544 : 0 : return AST::MatchArm::create_error ();
8545 : 0 : }
8546 : :
8547 : : // DEBUG
8548 : 1914 : rust_debug ("successfully parsed match arm patterns");
8549 : :
8550 : : // parse match arm guard expr if it exists
8551 : 1914 : std::unique_ptr<AST::Expr> guard_expr = nullptr;
8552 : 3828 : if (lexer.peek_token ()->get_id () == IF)
8553 : : {
8554 : 1 : lexer.skip_token ();
8555 : :
8556 : 1 : guard_expr = parse_expr ();
8557 : 1 : if (guard_expr == nullptr)
8558 : : {
8559 : 0 : Error error (lexer.peek_token ()->get_locus (),
8560 : : "failed to parse guard expression in match arm");
8561 : 0 : add_error (std::move (error));
8562 : :
8563 : : // skip somewhere?
8564 : 0 : return AST::MatchArm::create_error ();
8565 : 0 : }
8566 : : }
8567 : :
8568 : : // DEBUG
8569 : 1914 : rust_debug ("successfully parsed match arm");
8570 : :
8571 : 3828 : return AST::MatchArm (std::move (match_arm_patterns),
8572 : 3828 : lexer.peek_token ()->get_locus (),
8573 : 1914 : std::move (guard_expr), std::move (outer_attrs));
8574 : 1914 : }
8575 : :
8576 : : /* Parses the patterns used in a match arm. End token id is the id of the
8577 : : * token that would exist after the patterns are done (e.g. '}' for match
8578 : : * expr, '=' for if let and while let). */
8579 : : template <typename ManagedTokenSource>
8580 : : std::vector<std::unique_ptr<AST::Pattern>>
8581 : 1948 : Parser<ManagedTokenSource>::parse_match_arm_patterns (TokenId end_token_id)
8582 : : {
8583 : : // skip optional leading '|'
8584 : 3896 : if (lexer.peek_token ()->get_id () == PIPE)
8585 : 0 : lexer.skip_token ();
8586 : : /* TODO: do I even need to store the result of this? can't be used.
8587 : : * If semantically different, I need a wrapped "match arm patterns" object
8588 : : * for this. */
8589 : :
8590 : 1948 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
8591 : :
8592 : : // quick break out if end_token_id
8593 : 3896 : if (lexer.peek_token ()->get_id () == end_token_id)
8594 : 0 : return patterns;
8595 : :
8596 : : // parse required pattern - if doesn't exist, return empty
8597 : 1948 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
8598 : 1948 : if (initial_pattern == nullptr)
8599 : : {
8600 : : // FIXME: should this be an error?
8601 : 0 : return patterns;
8602 : : }
8603 : 1948 : patterns.push_back (std::move (initial_pattern));
8604 : :
8605 : : // DEBUG
8606 : 1948 : rust_debug ("successfully parsed initial match arm pattern");
8607 : :
8608 : : // parse new patterns as long as next char is '|'
8609 : 1948 : const_TokenPtr t = lexer.peek_token ();
8610 : 1948 : while (t->get_id () == PIPE)
8611 : : {
8612 : : // skip pipe token
8613 : 0 : lexer.skip_token ();
8614 : :
8615 : : // break if hit end token id
8616 : 0 : if (lexer.peek_token ()->get_id () == end_token_id)
8617 : : break;
8618 : :
8619 : : // parse pattern
8620 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
8621 : 0 : if (pattern == nullptr)
8622 : : {
8623 : : // this is an error
8624 : 0 : Error error (lexer.peek_token ()->get_locus (),
8625 : : "failed to parse pattern in match arm patterns");
8626 : 0 : add_error (std::move (error));
8627 : :
8628 : : // skip somewhere?
8629 : 0 : return {};
8630 : 0 : }
8631 : :
8632 : 0 : patterns.push_back (std::move (pattern));
8633 : :
8634 : 0 : t = lexer.peek_token ();
8635 : : }
8636 : :
8637 : 1948 : patterns.shrink_to_fit ();
8638 : :
8639 : 1948 : return patterns;
8640 : 1948 : }
8641 : :
8642 : : // Parses an async block expression.
8643 : : template <typename ManagedTokenSource>
8644 : : std::unique_ptr<AST::AsyncBlockExpr>
8645 : 0 : Parser<ManagedTokenSource>::parse_async_block_expr (AST::AttrVec outer_attrs)
8646 : : {
8647 : 0 : location_t locus = lexer.peek_token ()->get_locus ();
8648 : 0 : skip_token (ASYNC);
8649 : :
8650 : : // detect optional move token
8651 : 0 : bool has_move = false;
8652 : 0 : if (lexer.peek_token ()->get_id () == MOVE)
8653 : : {
8654 : 0 : lexer.skip_token ();
8655 : 0 : has_move = true;
8656 : : }
8657 : :
8658 : : // parse block expression (required)
8659 : 0 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
8660 : 0 : if (block_expr == nullptr)
8661 : : {
8662 : 0 : Error error (
8663 : 0 : lexer.peek_token ()->get_locus (),
8664 : : "failed to parse block expression of async block expression");
8665 : 0 : add_error (std::move (error));
8666 : :
8667 : : // skip somewhere?
8668 : 0 : return nullptr;
8669 : 0 : }
8670 : :
8671 : : return std::unique_ptr<AST::AsyncBlockExpr> (
8672 : 0 : new AST::AsyncBlockExpr (std::move (block_expr), has_move,
8673 : 0 : std::move (outer_attrs), locus));
8674 : 0 : }
8675 : :
8676 : : // Parses an unsafe block expression.
8677 : : template <typename ManagedTokenSource>
8678 : : std::unique_ptr<AST::UnsafeBlockExpr>
8679 : 3457 : Parser<ManagedTokenSource>::parse_unsafe_block_expr (
8680 : : AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
8681 : : {
8682 : 3457 : location_t locus = pratt_parsed_loc;
8683 : 3457 : if (locus == UNKNOWN_LOCATION)
8684 : : {
8685 : 0 : locus = lexer.peek_token ()->get_locus ();
8686 : 0 : skip_token (UNSAFE);
8687 : : }
8688 : :
8689 : : // parse block expression (required)
8690 : 3457 : std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
8691 : 3457 : if (block_expr == nullptr)
8692 : : {
8693 : 0 : Error error (
8694 : 0 : lexer.peek_token ()->get_locus (),
8695 : : "failed to parse block expression of unsafe block expression");
8696 : 0 : add_error (std::move (error));
8697 : :
8698 : : // skip somewhere?
8699 : 0 : return nullptr;
8700 : 0 : }
8701 : :
8702 : : return std::unique_ptr<AST::UnsafeBlockExpr> (
8703 : 3457 : new AST::UnsafeBlockExpr (std::move (block_expr), std::move (outer_attrs),
8704 : 3457 : locus));
8705 : 3457 : }
8706 : :
8707 : : // Parses an array definition expression.
8708 : : template <typename ManagedTokenSource>
8709 : : std::unique_ptr<AST::ArrayExpr>
8710 : 352 : Parser<ManagedTokenSource>::parse_array_expr (AST::AttrVec outer_attrs,
8711 : : location_t pratt_parsed_loc)
8712 : : {
8713 : 352 : location_t locus = pratt_parsed_loc;
8714 : 352 : if (locus == UNKNOWN_LOCATION)
8715 : : {
8716 : 0 : locus = lexer.peek_token ()->get_locus ();
8717 : 0 : skip_token (LEFT_SQUARE);
8718 : : }
8719 : :
8720 : : // parse optional inner attributes
8721 : 352 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8722 : :
8723 : : // parse the "array elements" section, which is optional
8724 : 704 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8725 : : {
8726 : : // no array elements
8727 : 1 : lexer.skip_token ();
8728 : :
8729 : 1 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8730 : 1 : auto array_elems
8731 : : = std::make_unique<AST::ArrayElemsValues> (std::move (exprs), locus);
8732 : : return std::make_unique<AST::ArrayExpr> (std::move (array_elems),
8733 : : std::move (inner_attrs),
8734 : 1 : std::move (outer_attrs), locus);
8735 : 1 : }
8736 : : else
8737 : : {
8738 : : // should have array elements
8739 : : // parse initial expression, which is required for either
8740 : 351 : std::unique_ptr<AST::Expr> initial_expr = parse_expr ();
8741 : 351 : if (initial_expr == nullptr)
8742 : : {
8743 : 0 : Error error (lexer.peek_token ()->get_locus (),
8744 : : "could not parse expression in array expression "
8745 : : "(even though arrayelems seems to be present)");
8746 : 0 : add_error (std::move (error));
8747 : :
8748 : : // skip somewhere?
8749 : 0 : return nullptr;
8750 : 0 : }
8751 : :
8752 : 702 : if (lexer.peek_token ()->get_id () == SEMICOLON)
8753 : : {
8754 : : // copy array elems
8755 : 117 : lexer.skip_token ();
8756 : :
8757 : : // parse copy amount expression (required)
8758 : 117 : std::unique_ptr<AST::Expr> copy_amount = parse_expr ();
8759 : 117 : if (copy_amount == nullptr)
8760 : : {
8761 : 0 : Error error (lexer.peek_token ()->get_locus (),
8762 : : "could not parse copy amount expression in array "
8763 : : "expression (arrayelems)");
8764 : 0 : add_error (std::move (error));
8765 : :
8766 : : // skip somewhere?
8767 : 0 : return nullptr;
8768 : 0 : }
8769 : :
8770 : 117 : skip_token (RIGHT_SQUARE);
8771 : :
8772 : 117 : std::unique_ptr<AST::ArrayElemsCopied> copied_array_elems (
8773 : 117 : new AST::ArrayElemsCopied (std::move (initial_expr),
8774 : : std::move (copy_amount), locus));
8775 : : return std::unique_ptr<AST::ArrayExpr> (
8776 : 117 : new AST::ArrayExpr (std::move (copied_array_elems),
8777 : : std::move (inner_attrs),
8778 : 117 : std::move (outer_attrs), locus));
8779 : 117 : }
8780 : 468 : else if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8781 : : {
8782 : : // single-element array expression
8783 : 33 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8784 : 33 : exprs.reserve (1);
8785 : 33 : exprs.push_back (std::move (initial_expr));
8786 : 33 : exprs.shrink_to_fit ();
8787 : :
8788 : 33 : skip_token (RIGHT_SQUARE);
8789 : :
8790 : 33 : std::unique_ptr<AST::ArrayElemsValues> array_elems (
8791 : 33 : new AST::ArrayElemsValues (std::move (exprs), locus));
8792 : : return std::unique_ptr<AST::ArrayExpr> (
8793 : 33 : new AST::ArrayExpr (std::move (array_elems),
8794 : : std::move (inner_attrs),
8795 : 33 : std::move (outer_attrs), locus));
8796 : 33 : }
8797 : 402 : else if (lexer.peek_token ()->get_id () == COMMA)
8798 : : {
8799 : : // multi-element array expression (or trailing comma)
8800 : 201 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8801 : 201 : exprs.push_back (std::move (initial_expr));
8802 : :
8803 : 201 : const_TokenPtr t = lexer.peek_token ();
8804 : 905 : while (t->get_id () == COMMA)
8805 : : {
8806 : 711 : lexer.skip_token ();
8807 : :
8808 : : // quick break if right square bracket
8809 : 1422 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
8810 : : break;
8811 : :
8812 : : // parse expression (required)
8813 : 704 : std::unique_ptr<AST::Expr> expr = parse_expr ();
8814 : 704 : if (expr == nullptr)
8815 : : {
8816 : 0 : Error error (lexer.peek_token ()->get_locus (),
8817 : : "failed to parse element in array expression");
8818 : 0 : add_error (std::move (error));
8819 : :
8820 : : // skip somewhere?
8821 : 0 : return nullptr;
8822 : 0 : }
8823 : 704 : exprs.push_back (std::move (expr));
8824 : :
8825 : 704 : t = lexer.peek_token ();
8826 : : }
8827 : :
8828 : 201 : skip_token (RIGHT_SQUARE);
8829 : :
8830 : 201 : exprs.shrink_to_fit ();
8831 : :
8832 : 201 : std::unique_ptr<AST::ArrayElemsValues> array_elems (
8833 : 201 : new AST::ArrayElemsValues (std::move (exprs), locus));
8834 : : return std::unique_ptr<AST::ArrayExpr> (
8835 : 201 : new AST::ArrayExpr (std::move (array_elems),
8836 : : std::move (inner_attrs),
8837 : 201 : std::move (outer_attrs), locus));
8838 : 402 : }
8839 : : else
8840 : : {
8841 : : // error
8842 : 0 : Error error (lexer.peek_token ()->get_locus (),
8843 : : "unexpected token %qs in array expression (arrayelems)",
8844 : 0 : lexer.peek_token ()->get_token_description ());
8845 : 0 : add_error (std::move (error));
8846 : :
8847 : : // skip somewhere?
8848 : 0 : return nullptr;
8849 : 0 : }
8850 : 351 : }
8851 : 352 : }
8852 : :
8853 : : // Parses a single parameter used in a closure definition.
8854 : : template <typename ManagedTokenSource>
8855 : : AST::ClosureParam
8856 : 71 : Parser<ManagedTokenSource>::parse_closure_param ()
8857 : : {
8858 : 71 : AST::AttrVec outer_attrs = parse_outer_attributes ();
8859 : :
8860 : : // parse pattern (which is required)
8861 : 71 : std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
8862 : 71 : if (pattern == nullptr)
8863 : : {
8864 : : // not necessarily an error
8865 : 0 : return AST::ClosureParam::create_error ();
8866 : : }
8867 : :
8868 : : // parse optional type of param
8869 : 71 : std::unique_ptr<AST::Type> type = nullptr;
8870 : 142 : if (lexer.peek_token ()->get_id () == COLON)
8871 : : {
8872 : 63 : lexer.skip_token ();
8873 : :
8874 : : // parse type, which is now required
8875 : 63 : type = parse_type ();
8876 : 63 : if (type == nullptr)
8877 : : {
8878 : 0 : Error error (lexer.peek_token ()->get_locus (),
8879 : : "failed to parse type in closure parameter");
8880 : 0 : add_error (std::move (error));
8881 : :
8882 : : // skip somewhere?
8883 : 0 : return AST::ClosureParam::create_error ();
8884 : 0 : }
8885 : : }
8886 : :
8887 : 71 : location_t loc = pattern->get_locus ();
8888 : 71 : return AST::ClosureParam (std::move (pattern), loc, std::move (type),
8889 : 71 : std::move (outer_attrs));
8890 : 71 : }
8891 : :
8892 : : // Parses a grouped or tuple expression (disambiguates).
8893 : : template <typename ManagedTokenSource>
8894 : : std::unique_ptr<AST::ExprWithoutBlock>
8895 : 799 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_expr (
8896 : : AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
8897 : : {
8898 : : // adjustment to allow Pratt parsing to reuse function without copy-paste
8899 : 799 : location_t locus = pratt_parsed_loc;
8900 : 799 : if (locus == UNKNOWN_LOCATION)
8901 : : {
8902 : 0 : locus = lexer.peek_token ()->get_locus ();
8903 : 0 : skip_token (LEFT_PAREN);
8904 : : }
8905 : :
8906 : : // parse optional inner attributes
8907 : 799 : AST::AttrVec inner_attrs = parse_inner_attributes ();
8908 : :
8909 : 1598 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8910 : : {
8911 : : // must be empty tuple
8912 : 137 : lexer.skip_token ();
8913 : :
8914 : : // create tuple with empty tuple elems
8915 : 137 : return std::unique_ptr<AST::TupleExpr> (
8916 : 137 : new AST::TupleExpr (std::vector<std::unique_ptr<AST::Expr>> (),
8917 : : std::move (inner_attrs), std::move (outer_attrs),
8918 : 137 : locus));
8919 : : }
8920 : :
8921 : : // parse first expression (required)
8922 : 662 : std::unique_ptr<AST::Expr> first_expr = parse_expr ();
8923 : 662 : if (first_expr == nullptr)
8924 : : {
8925 : 0 : Error error (lexer.peek_token ()->get_locus (),
8926 : : "failed to parse expression in grouped or tuple expression");
8927 : 0 : add_error (std::move (error));
8928 : :
8929 : : // skip after somewhere?
8930 : 0 : return nullptr;
8931 : 0 : }
8932 : :
8933 : : // detect whether grouped expression with right parentheses as next token
8934 : 1324 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8935 : : {
8936 : : // must be grouped expr
8937 : 293 : lexer.skip_token ();
8938 : :
8939 : : // create grouped expr
8940 : 293 : return std::unique_ptr<AST::GroupedExpr> (
8941 : 293 : new AST::GroupedExpr (std::move (first_expr), std::move (inner_attrs),
8942 : 293 : std::move (outer_attrs), locus));
8943 : : }
8944 : 738 : else if (lexer.peek_token ()->get_id () == COMMA)
8945 : : {
8946 : : // tuple expr
8947 : 368 : std::vector<std::unique_ptr<AST::Expr>> exprs;
8948 : 368 : exprs.push_back (std::move (first_expr));
8949 : :
8950 : : // parse potential other tuple exprs
8951 : 368 : const_TokenPtr t = lexer.peek_token ();
8952 : 871 : while (t->get_id () == COMMA)
8953 : : {
8954 : 532 : lexer.skip_token ();
8955 : :
8956 : : // break out if right paren
8957 : 1064 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
8958 : : break;
8959 : :
8960 : : // parse expr, which is now required
8961 : 503 : std::unique_ptr<AST::Expr> expr = parse_expr ();
8962 : 503 : if (expr == nullptr)
8963 : : {
8964 : 0 : Error error (lexer.peek_token ()->get_locus (),
8965 : : "failed to parse expr in tuple expr");
8966 : 0 : add_error (std::move (error));
8967 : :
8968 : : // skip somewhere?
8969 : 0 : return nullptr;
8970 : 0 : }
8971 : 503 : exprs.push_back (std::move (expr));
8972 : :
8973 : 503 : t = lexer.peek_token ();
8974 : : }
8975 : :
8976 : : // skip right paren
8977 : 368 : skip_token (RIGHT_PAREN);
8978 : :
8979 : 368 : return std::unique_ptr<AST::TupleExpr> (
8980 : 368 : new AST::TupleExpr (std::move (exprs), std::move (inner_attrs),
8981 : 368 : std::move (outer_attrs), locus));
8982 : 368 : }
8983 : : else
8984 : : {
8985 : : // error
8986 : 1 : const_TokenPtr t = lexer.peek_token ();
8987 : 1 : Error error (t->get_locus (),
8988 : : "unexpected token %qs in grouped or tuple expression "
8989 : : "(parenthesised expression) - expected %<)%> for grouped "
8990 : : "expr and %<,%> for tuple expr",
8991 : : t->get_token_description ());
8992 : 1 : add_error (std::move (error));
8993 : :
8994 : : // skip somewhere?
8995 : 1 : return nullptr;
8996 : 2 : }
8997 : 799 : }
8998 : :
8999 : : // Parses a type (will further disambiguate any type).
9000 : : template <typename ManagedTokenSource>
9001 : : std::unique_ptr<AST::Type>
9002 : 42674 : Parser<ManagedTokenSource>::parse_type (bool save_errors)
9003 : : {
9004 : : /* rules for all types:
9005 : : * NeverType: '!'
9006 : : * SliceType: '[' Type ']'
9007 : : * InferredType: '_'
9008 : : * MacroInvocation: SimplePath '!' DelimTokenTree
9009 : : * ParenthesisedType: '(' Type ')'
9010 : : * ImplTraitType: 'impl' TypeParamBounds
9011 : : * TypeParamBounds (not type) TypeParamBound ( '+' TypeParamBound )* '+'?
9012 : : * TypeParamBound Lifetime | TraitBound
9013 : : * ImplTraitTypeOneBound: 'impl' TraitBound
9014 : : * TraitObjectType: 'dyn'? TypeParamBounds
9015 : : * TraitObjectTypeOneBound: 'dyn'? TraitBound
9016 : : * TraitBound '?'? ForLifetimes? TypePath | '(' '?'?
9017 : : * ForLifetimes? TypePath ')' BareFunctionType: ForLifetimes?
9018 : : * FunctionQualifiers 'fn' etc. ForLifetimes (not type) 'for' '<'
9019 : : * LifetimeParams '>' FunctionQualifiers ( 'async' | 'const' )?
9020 : : * 'unsafe'?
9021 : : * ('extern' abi?)? QualifiedPathInType: '<' Type ( 'as' TypePath )? '>'
9022 : : * (
9023 : : * '::' TypePathSegment )+ TypePath: '::'? TypePathSegment (
9024 : : * '::' TypePathSegment)* ArrayType: '[' Type ';' Expr ']'
9025 : : * ReferenceType: '&' Lifetime? 'mut'? TypeNoBounds
9026 : : * RawPointerType: '*' ( 'mut' | 'const' ) TypeNoBounds
9027 : : * TupleType: '(' Type etc. - regular tuple stuff. Also
9028 : : * regular tuple vs parenthesised precedence
9029 : : *
9030 : : * Disambiguate between macro and type path via type path being parsed, and
9031 : : * then if '!' found, convert type path to simple path for macro. Usual
9032 : : * disambiguation for tuple vs parenthesised. For ImplTraitType and
9033 : : * TraitObjectType individual disambiguations, they seem more like "special
9034 : : * cases", so probably just try to parse the more general ImplTraitType or
9035 : : * TraitObjectType and return OneBound versions if they satisfy those
9036 : : * criteria. */
9037 : :
9038 : 42674 : const_TokenPtr t = lexer.peek_token ();
9039 : 42674 : switch (t->get_id ())
9040 : : {
9041 : 45 : case EXCLAM:
9042 : : // never type - can't be macro as no path beforehand
9043 : 45 : lexer.skip_token ();
9044 : 45 : return std::unique_ptr<AST::NeverType> (
9045 : 45 : new AST::NeverType (t->get_locus ()));
9046 : 784 : case LEFT_SQUARE:
9047 : : // slice type or array type - requires further disambiguation
9048 : 784 : return parse_slice_or_array_type ();
9049 : 301 : case LEFT_SHIFT:
9050 : : case LEFT_ANGLE:
9051 : : {
9052 : : // qualified path in type
9053 : 301 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
9054 : 301 : if (path.is_error ())
9055 : : {
9056 : 0 : if (save_errors)
9057 : : {
9058 : 0 : Error error (t->get_locus (),
9059 : : "failed to parse qualified path in type");
9060 : 0 : add_error (std::move (error));
9061 : 0 : }
9062 : :
9063 : 0 : return nullptr;
9064 : : }
9065 : 301 : return std::unique_ptr<AST::QualifiedPathInType> (
9066 : 301 : new AST::QualifiedPathInType (std::move (path)));
9067 : 301 : }
9068 : 104 : case UNDERSCORE:
9069 : : // inferred type
9070 : 104 : lexer.skip_token ();
9071 : 104 : return std::unique_ptr<AST::InferredType> (
9072 : 104 : new AST::InferredType (t->get_locus ()));
9073 : 2691 : case ASTERISK:
9074 : : // raw pointer type
9075 : 2691 : return parse_raw_pointer_type ();
9076 : 4252 : case AMP: // does this also include AMP_AMP?
9077 : : case LOGICAL_AND:
9078 : : // reference type
9079 : 4252 : return parse_reference_type ();
9080 : 0 : case LIFETIME:
9081 : : {
9082 : : /* probably a lifetime bound, so probably type param bounds in
9083 : : * TraitObjectType */
9084 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9085 : : = parse_type_param_bounds ();
9086 : :
9087 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9088 : 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9089 : 0 : false));
9090 : 0 : }
9091 : 33916 : case IDENTIFIER:
9092 : : case SUPER:
9093 : : case SELF:
9094 : : case SELF_ALIAS:
9095 : : case CRATE:
9096 : : case DOLLAR_SIGN:
9097 : : case SCOPE_RESOLUTION:
9098 : : {
9099 : : // macro invocation or type path - requires further disambiguation.
9100 : : /* for parsing path component of each rule, perhaps parse it as a
9101 : : * typepath and attempt conversion to simplepath if a trailing '!' is
9102 : : * found */
9103 : : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
9104 : : * with it, it is exactly the same as a TypePath syntactically, so
9105 : : * this is a syntactical ambiguity. As such, the parser will parse it
9106 : : * as a TypePath. This, however, does not prevent TraitObjectType from
9107 : : * starting with a typepath. */
9108 : :
9109 : : // parse path as type path
9110 : 33916 : AST::TypePath path = parse_type_path ();
9111 : 33916 : if (path.is_error ())
9112 : : {
9113 : 0 : if (save_errors)
9114 : : {
9115 : 0 : Error error (t->get_locus (),
9116 : : "failed to parse path as first component of type");
9117 : 0 : add_error (std::move (error));
9118 : 0 : }
9119 : :
9120 : 0 : return nullptr;
9121 : : }
9122 : 33916 : location_t locus = path.get_locus ();
9123 : :
9124 : : // branch on next token
9125 : 33916 : t = lexer.peek_token ();
9126 : 33916 : switch (t->get_id ())
9127 : : {
9128 : 28 : case EXCLAM:
9129 : : {
9130 : : // macro invocation
9131 : : // convert to simple path
9132 : 28 : AST::SimplePath macro_path = path.as_simple_path ();
9133 : 28 : if (macro_path.is_empty ())
9134 : : {
9135 : 0 : if (save_errors)
9136 : : {
9137 : 0 : Error error (t->get_locus (),
9138 : : "failed to parse simple path in macro "
9139 : : "invocation (for type)");
9140 : 0 : add_error (std::move (error));
9141 : 0 : }
9142 : :
9143 : 0 : return nullptr;
9144 : : }
9145 : :
9146 : 28 : lexer.skip_token ();
9147 : :
9148 : 28 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
9149 : :
9150 : 56 : return AST::MacroInvocation::Regular (
9151 : 56 : AST::MacroInvocData (std::move (macro_path),
9152 : : std::move (tok_tree)),
9153 : 28 : {}, locus);
9154 : 56 : }
9155 : 0 : case PLUS:
9156 : : {
9157 : : // type param bounds
9158 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9159 : :
9160 : : // convert type path to trait bound
9161 : 0 : std::unique_ptr<AST::TraitBound> path_bound (
9162 : 0 : new AST::TraitBound (std::move (path), locus, false, false));
9163 : 0 : bounds.push_back (std::move (path_bound));
9164 : :
9165 : : /* parse rest of bounds - FIXME: better way to find when to stop
9166 : : * parsing */
9167 : 0 : while (t->get_id () == PLUS)
9168 : : {
9169 : 0 : lexer.skip_token ();
9170 : :
9171 : : // parse bound if it exists - if not, assume end of sequence
9172 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9173 : : = parse_type_param_bound ();
9174 : 0 : if (bound == nullptr)
9175 : : {
9176 : : break;
9177 : : }
9178 : 0 : bounds.push_back (std::move (bound));
9179 : :
9180 : 0 : t = lexer.peek_token ();
9181 : : }
9182 : :
9183 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9184 : 0 : new AST::TraitObjectType (std::move (bounds), locus, false));
9185 : 0 : }
9186 : 33888 : default:
9187 : : // assume that this is a type path and not an error
9188 : 33888 : return std::unique_ptr<AST::TypePath> (
9189 : 33888 : new AST::TypePath (std::move (path)));
9190 : : }
9191 : 33916 : }
9192 : 385 : case LEFT_PAREN:
9193 : : /* tuple type or parenthesised type - requires further disambiguation
9194 : : * (the usual). ok apparently can be a parenthesised TraitBound too, so
9195 : : * could be TraitObjectTypeOneBound or TraitObjectType */
9196 : 385 : return parse_paren_prefixed_type ();
9197 : 3 : case FOR:
9198 : : // TraitObjectTypeOneBound or BareFunctionType
9199 : 3 : return parse_for_prefixed_type ();
9200 : 42 : case ASYNC:
9201 : : case CONST:
9202 : : case UNSAFE:
9203 : : case EXTERN_KW:
9204 : : case FN_KW:
9205 : : // bare function type (with no for lifetimes)
9206 : 42 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
9207 : 120 : case IMPL:
9208 : 120 : lexer.skip_token ();
9209 : 240 : if (lexer.peek_token ()->get_id () == LIFETIME)
9210 : : {
9211 : : /* cannot be one bound because lifetime prevents it from being
9212 : : * traitbound */
9213 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9214 : : = parse_type_param_bounds ();
9215 : :
9216 : 0 : return std::unique_ptr<AST::ImplTraitType> (
9217 : 0 : new AST::ImplTraitType (std::move (bounds), t->get_locus ()));
9218 : 0 : }
9219 : : else
9220 : : {
9221 : : // should be trait bound, so parse trait bound
9222 : 120 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
9223 : 120 : if (initial_bound == nullptr)
9224 : : {
9225 : 0 : if (save_errors)
9226 : : {
9227 : 0 : Error error (lexer.peek_token ()->get_locus (),
9228 : : "failed to parse ImplTraitType initial bound");
9229 : 0 : add_error (std::move (error));
9230 : 0 : }
9231 : :
9232 : 0 : return nullptr;
9233 : : }
9234 : :
9235 : 120 : location_t locus = t->get_locus ();
9236 : :
9237 : : // short cut if next token isn't '+'
9238 : 120 : t = lexer.peek_token ();
9239 : 120 : if (t->get_id () != PLUS)
9240 : : {
9241 : 120 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
9242 : 120 : new AST::ImplTraitTypeOneBound (std::move (initial_bound),
9243 : 120 : locus));
9244 : : }
9245 : :
9246 : : // parse additional type param bounds
9247 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9248 : 0 : bounds.push_back (std::move (initial_bound));
9249 : 0 : while (t->get_id () == PLUS)
9250 : : {
9251 : 0 : lexer.skip_token ();
9252 : :
9253 : : // parse bound if it exists
9254 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9255 : : = parse_type_param_bound ();
9256 : 0 : if (bound == nullptr)
9257 : : {
9258 : : // not an error as trailing plus may exist
9259 : : break;
9260 : : }
9261 : 0 : bounds.push_back (std::move (bound));
9262 : :
9263 : 0 : t = lexer.peek_token ();
9264 : : }
9265 : :
9266 : 0 : return std::unique_ptr<AST::ImplTraitType> (
9267 : 0 : new AST::ImplTraitType (std::move (bounds), locus));
9268 : 120 : }
9269 : 27 : case DYN:
9270 : : case QUESTION_MARK:
9271 : : {
9272 : : // either TraitObjectType or TraitObjectTypeOneBound
9273 : 27 : bool has_dyn = false;
9274 : 27 : if (t->get_id () == DYN)
9275 : : {
9276 : 27 : lexer.skip_token ();
9277 : 27 : has_dyn = true;
9278 : : }
9279 : :
9280 : 54 : if (lexer.peek_token ()->get_id () == LIFETIME)
9281 : : {
9282 : : /* cannot be one bound because lifetime prevents it from being
9283 : : * traitbound */
9284 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
9285 : : = parse_type_param_bounds ();
9286 : :
9287 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9288 : 0 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9289 : 0 : has_dyn));
9290 : 0 : }
9291 : : else
9292 : : {
9293 : : // should be trait bound, so parse trait bound
9294 : 27 : std::unique_ptr<AST::TraitBound> initial_bound
9295 : : = parse_trait_bound ();
9296 : 27 : if (initial_bound == nullptr)
9297 : : {
9298 : 0 : if (save_errors)
9299 : : {
9300 : 0 : Error error (
9301 : 0 : lexer.peek_token ()->get_locus (),
9302 : : "failed to parse TraitObjectType initial bound");
9303 : 0 : add_error (std::move (error));
9304 : 0 : }
9305 : :
9306 : 0 : return nullptr;
9307 : : }
9308 : :
9309 : : // short cut if next token isn't '+'
9310 : 27 : t = lexer.peek_token ();
9311 : 27 : if (t->get_id () != PLUS)
9312 : : {
9313 : : // convert trait bound to value object
9314 : 14 : AST::TraitBound value_bound (*initial_bound);
9315 : :
9316 : : // DEBUG: removed as unique ptr, so should auto delete
9317 : : // delete initial_bound;
9318 : :
9319 : 14 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9320 : 28 : new AST::TraitObjectTypeOneBound (std::move (value_bound),
9321 : 14 : t->get_locus (), has_dyn));
9322 : 14 : }
9323 : :
9324 : : // parse additional type param bounds
9325 : 13 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9326 : 13 : bounds.push_back (std::move (initial_bound));
9327 : 33 : while (t->get_id () == PLUS)
9328 : : {
9329 : 20 : lexer.skip_token ();
9330 : :
9331 : : // parse bound if it exists
9332 : 20 : std::unique_ptr<AST::TypeParamBound> bound
9333 : : = parse_type_param_bound ();
9334 : 20 : if (bound == nullptr)
9335 : : {
9336 : : // not an error as trailing plus may exist
9337 : : break;
9338 : : }
9339 : 20 : bounds.push_back (std::move (bound));
9340 : :
9341 : 20 : t = lexer.peek_token ();
9342 : : }
9343 : :
9344 : 13 : return std::unique_ptr<AST::TraitObjectType> (
9345 : 13 : new AST::TraitObjectType (std::move (bounds), t->get_locus (),
9346 : 13 : has_dyn));
9347 : 27 : }
9348 : : }
9349 : 4 : default:
9350 : 4 : if (save_errors)
9351 : 4 : add_error (Error (t->get_locus (), "unrecognised token %qs in type",
9352 : : t->get_token_description ()));
9353 : :
9354 : 4 : return nullptr;
9355 : : }
9356 : 42674 : }
9357 : :
9358 : : /* Parses a type that has '(' as its first character. Returns a tuple type,
9359 : : * parenthesised type, TraitObjectTypeOneBound, or TraitObjectType depending
9360 : : * on following characters. */
9361 : : template <typename ManagedTokenSource>
9362 : : std::unique_ptr<AST::Type>
9363 : 385 : Parser<ManagedTokenSource>::parse_paren_prefixed_type ()
9364 : : {
9365 : : /* NOTE: Syntactical ambiguity of a parenthesised trait bound is considered
9366 : : * a trait bound, not a parenthesised type, so that it can still be used in
9367 : : * type param bounds. */
9368 : :
9369 : : /* NOTE: this implementation is really shit but I couldn't think of a better
9370 : : * one. It requires essentially breaking polymorphism and downcasting via
9371 : : * virtual method abuse, as it was copied from the rustc implementation (in
9372 : : * which types are reified due to tagged union), after a more OOP attempt by
9373 : : * me failed. */
9374 : 385 : location_t left_delim_locus = lexer.peek_token ()->get_locus ();
9375 : :
9376 : : // skip left delim
9377 : 385 : lexer.skip_token ();
9378 : : /* while next token isn't close delim, parse comma-separated types, saving
9379 : : * whether trailing comma happens */
9380 : 385 : const_TokenPtr t = lexer.peek_token ();
9381 : 385 : bool trailing_comma = true;
9382 : 385 : std::vector<std::unique_ptr<AST::Type>> types;
9383 : :
9384 : 710 : while (t->get_id () != RIGHT_PAREN)
9385 : : {
9386 : 625 : std::unique_ptr<AST::Type> type = parse_type ();
9387 : 625 : if (type == nullptr)
9388 : : {
9389 : 0 : Error error (t->get_locus (),
9390 : : "failed to parse type inside parentheses (probably "
9391 : : "tuple or parenthesised)");
9392 : 0 : add_error (std::move (error));
9393 : :
9394 : 0 : return nullptr;
9395 : 0 : }
9396 : 625 : types.push_back (std::move (type));
9397 : :
9398 : 625 : t = lexer.peek_token ();
9399 : 625 : if (t->get_id () != COMMA)
9400 : : {
9401 : 300 : trailing_comma = false;
9402 : : break;
9403 : : }
9404 : 325 : lexer.skip_token ();
9405 : :
9406 : 325 : t = lexer.peek_token ();
9407 : : }
9408 : :
9409 : 385 : if (!skip_token (RIGHT_PAREN))
9410 : : {
9411 : 0 : return nullptr;
9412 : : }
9413 : :
9414 : : // if only one type and no trailing comma, then not a tuple type
9415 : 385 : if (types.size () == 1 && !trailing_comma)
9416 : : {
9417 : : // must be a TraitObjectType (with more than one bound)
9418 : 4 : if (lexer.peek_token ()->get_id () == PLUS)
9419 : : {
9420 : : // create type param bounds vector
9421 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9422 : :
9423 : : // HACK: convert type to traitbound and add to bounds
9424 : 0 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
9425 : 0 : std::unique_ptr<AST::TraitBound> converted_bound (
9426 : 0 : released_ptr->to_trait_bound (true));
9427 : 0 : if (converted_bound == nullptr)
9428 : : {
9429 : 0 : Error error (
9430 : 0 : lexer.peek_token ()->get_locus (),
9431 : : "failed to hackily converted parsed type to trait bound");
9432 : 0 : add_error (std::move (error));
9433 : :
9434 : 0 : return nullptr;
9435 : 0 : }
9436 : 0 : bounds.push_back (std::move (converted_bound));
9437 : :
9438 : 0 : t = lexer.peek_token ();
9439 : 0 : while (t->get_id () == PLUS)
9440 : : {
9441 : 0 : lexer.skip_token ();
9442 : :
9443 : : // attempt to parse typeparambound
9444 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9445 : : = parse_type_param_bound ();
9446 : 0 : if (bound == nullptr)
9447 : : {
9448 : : // not an error if null
9449 : : break;
9450 : : }
9451 : 0 : bounds.push_back (std::move (bound));
9452 : :
9453 : 0 : t = lexer.peek_token ();
9454 : : }
9455 : :
9456 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9457 : 0 : new AST::TraitObjectType (std::move (bounds), left_delim_locus,
9458 : 0 : false));
9459 : 0 : }
9460 : : else
9461 : : {
9462 : : // release vector pointer
9463 : 2 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
9464 : : /* HACK: attempt to convert to trait bound. if fails, parenthesised
9465 : : * type */
9466 : 2 : std::unique_ptr<AST::TraitBound> converted_bound (
9467 : 2 : released_ptr->to_trait_bound (true));
9468 : 2 : if (converted_bound == nullptr)
9469 : : {
9470 : : // parenthesised type
9471 : 2 : return std::unique_ptr<AST::ParenthesisedType> (
9472 : 2 : new AST::ParenthesisedType (std::move (released_ptr),
9473 : 2 : left_delim_locus));
9474 : : }
9475 : : else
9476 : : {
9477 : : // trait object type (one bound)
9478 : :
9479 : : // get value semantics trait bound
9480 : 0 : AST::TraitBound value_bound (*converted_bound);
9481 : :
9482 : 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9483 : 0 : new AST::TraitObjectTypeOneBound (value_bound,
9484 : 0 : left_delim_locus));
9485 : 0 : }
9486 : 2 : }
9487 : : }
9488 : : else
9489 : : {
9490 : 383 : return std::unique_ptr<AST::TupleType> (
9491 : 383 : new AST::TupleType (std::move (types), left_delim_locus));
9492 : : }
9493 : : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
9494 : : * lost somehow */
9495 : 385 : }
9496 : :
9497 : : /* Parses a type that has 'for' as its first character. This means it has a
9498 : : * "for lifetimes", so returns either a BareFunctionType, TraitObjectType, or
9499 : : * TraitObjectTypeOneBound depending on following characters. */
9500 : : template <typename ManagedTokenSource>
9501 : : std::unique_ptr<AST::Type>
9502 : 3 : Parser<ManagedTokenSource>::parse_for_prefixed_type ()
9503 : : {
9504 : 3 : location_t for_locus = lexer.peek_token ()->get_locus ();
9505 : : // parse for lifetimes in type
9506 : 3 : std::vector<AST::LifetimeParam> for_lifetimes = parse_for_lifetimes ();
9507 : :
9508 : : // branch on next token - either function or a trait type
9509 : 3 : const_TokenPtr t = lexer.peek_token ();
9510 : 3 : switch (t->get_id ())
9511 : : {
9512 : 3 : case ASYNC:
9513 : : case CONST:
9514 : : case UNSAFE:
9515 : : case EXTERN_KW:
9516 : : case FN_KW:
9517 : 3 : return parse_bare_function_type (std::move (for_lifetimes));
9518 : 0 : case SCOPE_RESOLUTION:
9519 : : case IDENTIFIER:
9520 : : case SUPER:
9521 : : case SELF:
9522 : : case SELF_ALIAS:
9523 : : case CRATE:
9524 : : case DOLLAR_SIGN:
9525 : : {
9526 : : // path, so trait type
9527 : :
9528 : : // parse type path to finish parsing trait bound
9529 : 0 : AST::TypePath path = parse_type_path ();
9530 : :
9531 : 0 : t = lexer.peek_token ();
9532 : 0 : if (t->get_id () != PLUS)
9533 : : {
9534 : : // must be one-bound trait type
9535 : : // create trait bound value object
9536 : 0 : AST::TraitBound bound (std::move (path), for_locus, false, false,
9537 : : std::move (for_lifetimes));
9538 : :
9539 : 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
9540 : 0 : new AST::TraitObjectTypeOneBound (std::move (bound), for_locus));
9541 : 0 : }
9542 : :
9543 : : /* more than one bound trait type (or at least parsed as it - could be
9544 : : * trailing '+') create trait bound pointer and bounds */
9545 : 0 : std::unique_ptr<AST::TraitBound> initial_bound (
9546 : 0 : new AST::TraitBound (std::move (path), for_locus, false, false,
9547 : : std::move (for_lifetimes)));
9548 : 0 : std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
9549 : 0 : bounds.push_back (std::move (initial_bound));
9550 : :
9551 : 0 : while (t->get_id () == PLUS)
9552 : : {
9553 : 0 : lexer.skip_token ();
9554 : :
9555 : : // parse type param bound if it exists
9556 : 0 : std::unique_ptr<AST::TypeParamBound> bound
9557 : : = parse_type_param_bound ();
9558 : 0 : if (bound == nullptr)
9559 : : {
9560 : : // not an error - e.g. trailing plus
9561 : 0 : return nullptr;
9562 : : }
9563 : 0 : bounds.push_back (std::move (bound));
9564 : :
9565 : 0 : t = lexer.peek_token ();
9566 : : }
9567 : :
9568 : 0 : return std::unique_ptr<AST::TraitObjectType> (
9569 : 0 : new AST::TraitObjectType (std::move (bounds), for_locus, false));
9570 : 0 : }
9571 : 0 : default:
9572 : : // error
9573 : 0 : add_error (Error (t->get_locus (),
9574 : : "unrecognised token %qs in bare function type or trait "
9575 : : "object type or trait object type one bound",
9576 : : t->get_token_description ()));
9577 : :
9578 : 0 : return nullptr;
9579 : : }
9580 : 3 : }
9581 : :
9582 : : // Parses a maybe named param used in bare function types.
9583 : : template <typename ManagedTokenSource>
9584 : : AST::MaybeNamedParam
9585 : 46 : Parser<ManagedTokenSource>::parse_maybe_named_param (AST::AttrVec outer_attrs)
9586 : : {
9587 : : /* Basically guess that param is named if first token is identifier or
9588 : : * underscore and second token is semicolon. This should probably have no
9589 : : * exceptions. rustc uses backtracking to parse these, but at the time of
9590 : : * writing gccrs has no backtracking capabilities. */
9591 : 46 : const_TokenPtr current = lexer.peek_token ();
9592 : 46 : const_TokenPtr next = lexer.peek_token (1);
9593 : :
9594 : 46 : Identifier name;
9595 : 46 : AST::MaybeNamedParam::ParamKind kind = AST::MaybeNamedParam::UNNAMED;
9596 : :
9597 : 46 : if (current->get_id () == IDENTIFIER && next->get_id () == COLON)
9598 : : {
9599 : : // named param
9600 : 1 : name = {current};
9601 : 1 : kind = AST::MaybeNamedParam::IDENTIFIER;
9602 : 1 : lexer.skip_token (1);
9603 : : }
9604 : 45 : else if (current->get_id () == UNDERSCORE && next->get_id () == COLON)
9605 : : {
9606 : : // wildcard param
9607 : 12 : name = {Values::Keywords::UNDERSCORE, current->get_locus ()};
9608 : 6 : kind = AST::MaybeNamedParam::WILDCARD;
9609 : 6 : lexer.skip_token (1);
9610 : : }
9611 : :
9612 : : // parse type (required)
9613 : 46 : std::unique_ptr<AST::Type> type = parse_type ();
9614 : 46 : if (type == nullptr)
9615 : : {
9616 : 0 : Error error (lexer.peek_token ()->get_locus (),
9617 : : "failed to parse type in maybe named param");
9618 : 0 : add_error (std::move (error));
9619 : :
9620 : 0 : return AST::MaybeNamedParam::create_error ();
9621 : 0 : }
9622 : :
9623 : 92 : return AST::MaybeNamedParam (std::move (name), kind, std::move (type),
9624 : 46 : std::move (outer_attrs), current->get_locus ());
9625 : 92 : }
9626 : :
9627 : : /* Parses a bare function type (with the given for lifetimes for convenience -
9628 : : * does not parse them itself). */
9629 : : template <typename ManagedTokenSource>
9630 : : std::unique_ptr<AST::BareFunctionType>
9631 : 47 : Parser<ManagedTokenSource>::parse_bare_function_type (
9632 : : std::vector<AST::LifetimeParam> for_lifetimes)
9633 : : {
9634 : : // TODO: pass in for lifetime location as param
9635 : 47 : location_t best_try_locus = lexer.peek_token ()->get_locus ();
9636 : :
9637 : 47 : AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
9638 : :
9639 : 47 : if (!skip_token (FN_KW))
9640 : 0 : return nullptr;
9641 : :
9642 : 47 : if (!skip_token (LEFT_PAREN))
9643 : 0 : return nullptr;
9644 : :
9645 : : // parse function params, if they exist
9646 : 47 : std::vector<AST::MaybeNamedParam> params;
9647 : 47 : bool is_variadic = false;
9648 : 47 : AST::AttrVec variadic_attrs;
9649 : :
9650 : 47 : const_TokenPtr t = lexer.peek_token ();
9651 : 98 : while (t->get_id () != RIGHT_PAREN)
9652 : : {
9653 : 46 : AST::AttrVec temp_attrs = parse_outer_attributes ();
9654 : :
9655 : 92 : if (lexer.peek_token ()->get_id () == ELLIPSIS)
9656 : : {
9657 : 0 : lexer.skip_token ();
9658 : 0 : is_variadic = true;
9659 : 0 : variadic_attrs = std::move (temp_attrs);
9660 : :
9661 : 0 : t = lexer.peek_token ();
9662 : :
9663 : 0 : if (t->get_id () != RIGHT_PAREN)
9664 : : {
9665 : 0 : Error error (t->get_locus (),
9666 : : "expected right parentheses after variadic in maybe "
9667 : : "named function "
9668 : : "parameters, found %qs",
9669 : : t->get_token_description ());
9670 : 0 : add_error (std::move (error));
9671 : :
9672 : 0 : return nullptr;
9673 : 0 : }
9674 : :
9675 : : break;
9676 : : }
9677 : :
9678 : 46 : AST::MaybeNamedParam param
9679 : 46 : = parse_maybe_named_param (std::move (temp_attrs));
9680 : 46 : if (param.is_error ())
9681 : : {
9682 : 0 : Error error (
9683 : 0 : lexer.peek_token ()->get_locus (),
9684 : : "failed to parse maybe named param in bare function type");
9685 : 0 : add_error (std::move (error));
9686 : :
9687 : 0 : return nullptr;
9688 : 0 : }
9689 : 46 : params.push_back (std::move (param));
9690 : :
9691 : 92 : if (lexer.peek_token ()->get_id () != COMMA)
9692 : : break;
9693 : :
9694 : 5 : lexer.skip_token ();
9695 : 5 : t = lexer.peek_token ();
9696 : : }
9697 : :
9698 : 47 : if (!skip_token (RIGHT_PAREN))
9699 : 0 : return nullptr;
9700 : :
9701 : : // bare function return type, if exists
9702 : 47 : std::unique_ptr<AST::TypeNoBounds> return_type = nullptr;
9703 : 94 : if (lexer.peek_token ()->get_id () == RETURN_TYPE)
9704 : : {
9705 : 36 : lexer.skip_token ();
9706 : :
9707 : : // parse required TypeNoBounds
9708 : 36 : return_type = parse_type_no_bounds ();
9709 : 36 : if (return_type == nullptr)
9710 : : {
9711 : 0 : Error error (lexer.peek_token ()->get_locus (),
9712 : : "failed to parse return type (type no bounds) in bare "
9713 : : "function type");
9714 : 0 : add_error (std::move (error));
9715 : :
9716 : 0 : return nullptr;
9717 : 0 : }
9718 : : }
9719 : :
9720 : : return std::unique_ptr<AST::BareFunctionType> (
9721 : 47 : new AST::BareFunctionType (std::move (for_lifetimes),
9722 : : std::move (qualifiers), std::move (params),
9723 : : is_variadic, std::move (variadic_attrs),
9724 : 47 : std::move (return_type), best_try_locus));
9725 : 94 : }
9726 : :
9727 : : template <typename ManagedTokenSource>
9728 : : std::unique_ptr<AST::ReferenceType>
9729 : 4276 : Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
9730 : : {
9731 : : // parse optional lifetime
9732 : 4276 : AST::Lifetime lifetime = AST::Lifetime::elided ();
9733 : 8552 : if (lexer.peek_token ()->get_id () == LIFETIME)
9734 : : {
9735 : 462 : auto parsed_lifetime = parse_lifetime (true);
9736 : 462 : if (parsed_lifetime)
9737 : : {
9738 : 462 : lifetime = parsed_lifetime.value ();
9739 : : }
9740 : : else
9741 : : {
9742 : 0 : Error error (lexer.peek_token ()->get_locus (),
9743 : : "failed to parse lifetime in reference type");
9744 : 0 : add_error (std::move (error));
9745 : :
9746 : 0 : return nullptr;
9747 : 0 : }
9748 : 462 : }
9749 : :
9750 : 4276 : bool is_mut = false;
9751 : 8552 : if (lexer.peek_token ()->get_id () == MUT)
9752 : : {
9753 : 300 : lexer.skip_token ();
9754 : 300 : is_mut = true;
9755 : : }
9756 : :
9757 : : // parse type no bounds, which is required
9758 : 4276 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
9759 : 4276 : if (type == nullptr)
9760 : : {
9761 : 0 : Error error (lexer.peek_token ()->get_locus (),
9762 : : "failed to parse referenced type in reference type");
9763 : 0 : add_error (std::move (error));
9764 : :
9765 : 0 : return nullptr;
9766 : 0 : }
9767 : :
9768 : : return std::unique_ptr<AST::ReferenceType> (
9769 : 12828 : new AST::ReferenceType (is_mut, std::move (type), locus,
9770 : 4276 : std::move (lifetime)));
9771 : 4276 : }
9772 : :
9773 : : // Parses a reference type (mutable or immutable, with given lifetime).
9774 : : template <typename ManagedTokenSource>
9775 : : std::unique_ptr<AST::ReferenceType>
9776 : 4276 : Parser<ManagedTokenSource>::parse_reference_type ()
9777 : : {
9778 : 4276 : auto t = lexer.peek_token ();
9779 : 4276 : auto locus = t->get_locus ();
9780 : :
9781 : 4276 : switch (t->get_id ())
9782 : : {
9783 : 4262 : case AMP:
9784 : 4262 : skip_token (AMP);
9785 : 4262 : return parse_reference_type_inner (locus);
9786 : 14 : case LOGICAL_AND:
9787 : 14 : skip_token (LOGICAL_AND);
9788 : : return std::unique_ptr<AST::ReferenceType> (
9789 : 42 : new AST::ReferenceType (false, parse_reference_type_inner (locus),
9790 : 14 : locus));
9791 : 0 : default:
9792 : 0 : rust_unreachable ();
9793 : : }
9794 : 4276 : }
9795 : :
9796 : : // Parses a raw (unsafe) pointer type.
9797 : : template <typename ManagedTokenSource>
9798 : : std::unique_ptr<AST::RawPointerType>
9799 : 6317 : Parser<ManagedTokenSource>::parse_raw_pointer_type ()
9800 : : {
9801 : 6317 : location_t locus = lexer.peek_token ()->get_locus ();
9802 : 6317 : skip_token (ASTERISK);
9803 : :
9804 : 6317 : AST::RawPointerType::PointerType kind = AST::RawPointerType::CONST;
9805 : :
9806 : : // branch on next token for pointer kind info
9807 : 6317 : const_TokenPtr t = lexer.peek_token ();
9808 : 6317 : switch (t->get_id ())
9809 : : {
9810 : 721 : case MUT:
9811 : 721 : kind = AST::RawPointerType::MUT;
9812 : 721 : lexer.skip_token ();
9813 : : break;
9814 : 5596 : case CONST:
9815 : 5596 : kind = AST::RawPointerType::CONST;
9816 : 5596 : lexer.skip_token ();
9817 : : break;
9818 : 0 : default:
9819 : 0 : add_error (Error (t->get_locus (),
9820 : : "unrecognised token %qs in raw pointer type",
9821 : : t->get_token_description ()));
9822 : :
9823 : 0 : return nullptr;
9824 : : }
9825 : :
9826 : : // parse type no bounds (required)
9827 : 6317 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
9828 : 6317 : if (type == nullptr)
9829 : : {
9830 : 0 : Error error (lexer.peek_token ()->get_locus (),
9831 : : "failed to parse pointed type of raw pointer type");
9832 : 0 : add_error (std::move (error));
9833 : :
9834 : 0 : return nullptr;
9835 : 0 : }
9836 : :
9837 : : return std::unique_ptr<AST::RawPointerType> (
9838 : 6317 : new AST::RawPointerType (kind, std::move (type), locus));
9839 : 6317 : }
9840 : :
9841 : : /* Parses a slice or array type, depending on following arguments (as
9842 : : * lookahead is not possible). */
9843 : : template <typename ManagedTokenSource>
9844 : : std::unique_ptr<AST::TypeNoBounds>
9845 : 1430 : Parser<ManagedTokenSource>::parse_slice_or_array_type ()
9846 : : {
9847 : 1430 : location_t locus = lexer.peek_token ()->get_locus ();
9848 : 1430 : skip_token (LEFT_SQUARE);
9849 : :
9850 : : // parse inner type (required)
9851 : 1430 : std::unique_ptr<AST::Type> inner_type = parse_type ();
9852 : 1430 : if (inner_type == nullptr)
9853 : : {
9854 : 0 : Error error (lexer.peek_token ()->get_locus (),
9855 : : "failed to parse inner type in slice or array type");
9856 : 0 : add_error (std::move (error));
9857 : :
9858 : 0 : return nullptr;
9859 : 0 : }
9860 : :
9861 : : // branch on next token
9862 : 1430 : const_TokenPtr t = lexer.peek_token ();
9863 : 1430 : switch (t->get_id ())
9864 : : {
9865 : 832 : case RIGHT_SQUARE:
9866 : : // slice type
9867 : 832 : lexer.skip_token ();
9868 : :
9869 : 832 : return std::unique_ptr<AST::SliceType> (
9870 : 832 : new AST::SliceType (std::move (inner_type), locus));
9871 : 598 : case SEMICOLON:
9872 : : {
9873 : : // array type
9874 : 598 : lexer.skip_token ();
9875 : :
9876 : : // parse required array size expression
9877 : 598 : auto size = parse_anon_const ();
9878 : :
9879 : 598 : if (!size)
9880 : : {
9881 : 0 : Error error (lexer.peek_token ()->get_locus (),
9882 : : "failed to parse size expression in array type");
9883 : 0 : add_error (std::move (error));
9884 : :
9885 : 0 : return nullptr;
9886 : 0 : }
9887 : :
9888 : 598 : if (!skip_token (RIGHT_SQUARE))
9889 : : {
9890 : 0 : return nullptr;
9891 : : }
9892 : :
9893 : 598 : return std::unique_ptr<AST::ArrayType> (
9894 : 1192 : new AST::ArrayType (std::move (inner_type), std::move (*size),
9895 : 598 : locus));
9896 : 598 : }
9897 : 0 : default:
9898 : : // error
9899 : 0 : add_error (
9900 : 0 : Error (t->get_locus (),
9901 : : "unrecognised token %qs in slice or array type after inner type",
9902 : : t->get_token_description ()));
9903 : :
9904 : 0 : return nullptr;
9905 : : }
9906 : 1430 : }
9907 : :
9908 : : // Parses a type, taking into account type boundary disambiguation.
9909 : : template <typename ManagedTokenSource>
9910 : : std::unique_ptr<AST::TypeNoBounds>
9911 : 15585 : Parser<ManagedTokenSource>::parse_type_no_bounds ()
9912 : : {
9913 : 15585 : const_TokenPtr t = lexer.peek_token ();
9914 : 15585 : switch (t->get_id ())
9915 : : {
9916 : 1 : case EXCLAM:
9917 : : // never type - can't be macro as no path beforehand
9918 : 1 : lexer.skip_token ();
9919 : 1 : return std::unique_ptr<AST::NeverType> (
9920 : 1 : new AST::NeverType (t->get_locus ()));
9921 : 646 : case LEFT_SQUARE:
9922 : : // slice type or array type - requires further disambiguation
9923 : 646 : return parse_slice_or_array_type ();
9924 : 22 : case LEFT_SHIFT:
9925 : : case LEFT_ANGLE:
9926 : : {
9927 : : // qualified path in type
9928 : 22 : AST::QualifiedPathInType path = parse_qualified_path_in_type ();
9929 : 22 : if (path.is_error ())
9930 : : {
9931 : 0 : Error error (t->get_locus (),
9932 : : "failed to parse qualified path in type");
9933 : 0 : add_error (std::move (error));
9934 : :
9935 : 0 : return nullptr;
9936 : 0 : }
9937 : 22 : return std::unique_ptr<AST::QualifiedPathInType> (
9938 : 22 : new AST::QualifiedPathInType (std::move (path)));
9939 : 22 : }
9940 : 104 : case UNDERSCORE:
9941 : : // inferred type
9942 : 104 : lexer.skip_token ();
9943 : 104 : return std::unique_ptr<AST::InferredType> (
9944 : 104 : new AST::InferredType (t->get_locus ()));
9945 : 3626 : case ASTERISK:
9946 : : // raw pointer type
9947 : 3626 : return parse_raw_pointer_type ();
9948 : 24 : case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND?
9949 : : case LOGICAL_AND:
9950 : : // reference type
9951 : 24 : return parse_reference_type ();
9952 : 0 : case LIFETIME:
9953 : : /* probably a lifetime bound, so probably type param bounds in
9954 : : * TraitObjectType. this is not allowed, but detection here for error
9955 : : * message */
9956 : 0 : add_error (Error (t->get_locus (),
9957 : : "lifetime bounds (i.e. in type param bounds, in "
9958 : : "TraitObjectType) are not allowed as TypeNoBounds"));
9959 : :
9960 : 0 : return nullptr;
9961 : 10986 : case IDENTIFIER:
9962 : : case SUPER:
9963 : : case SELF:
9964 : : case SELF_ALIAS:
9965 : : case CRATE:
9966 : : case DOLLAR_SIGN:
9967 : : case SCOPE_RESOLUTION:
9968 : : {
9969 : : // macro invocation or type path - requires further disambiguation.
9970 : : /* for parsing path component of each rule, perhaps parse it as a
9971 : : * typepath and attempt conversion to simplepath if a trailing '!' is
9972 : : * found */
9973 : : /* Type path also includes TraitObjectTypeOneBound BUT if it starts
9974 : : * with it, it is exactly the same as a TypePath syntactically, so
9975 : : * this is a syntactical ambiguity. As such, the parser will parse it
9976 : : * as a TypePath. This, however, does not prevent TraitObjectType from
9977 : : * starting with a typepath. */
9978 : :
9979 : : // parse path as type path
9980 : 10986 : AST::TypePath path = parse_type_path ();
9981 : 10986 : if (path.is_error ())
9982 : : {
9983 : 0 : Error error (
9984 : : t->get_locus (),
9985 : : "failed to parse path as first component of type no bounds");
9986 : 0 : add_error (std::move (error));
9987 : :
9988 : 0 : return nullptr;
9989 : 0 : }
9990 : 10986 : location_t locus = path.get_locus ();
9991 : :
9992 : : // branch on next token
9993 : 10986 : t = lexer.peek_token ();
9994 : 10986 : switch (t->get_id ())
9995 : : {
9996 : 1 : case EXCLAM:
9997 : : {
9998 : : // macro invocation
9999 : : // convert to simple path
10000 : 1 : AST::SimplePath macro_path = path.as_simple_path ();
10001 : 1 : if (macro_path.is_empty ())
10002 : : {
10003 : 0 : Error error (t->get_locus (),
10004 : : "failed to parse simple path in macro "
10005 : : "invocation (for type)");
10006 : 0 : add_error (std::move (error));
10007 : :
10008 : 0 : return nullptr;
10009 : 0 : }
10010 : :
10011 : 1 : lexer.skip_token ();
10012 : :
10013 : 1 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
10014 : :
10015 : 2 : return AST::MacroInvocation::Regular (
10016 : 2 : AST::MacroInvocData (std::move (macro_path),
10017 : : std::move (tok_tree)),
10018 : 1 : {}, locus);
10019 : 2 : }
10020 : 10985 : default:
10021 : : // assume that this is a type path and not an error
10022 : 10985 : return std::unique_ptr<AST::TypePath> (
10023 : 10985 : new AST::TypePath (std::move (path)));
10024 : : }
10025 : 10986 : }
10026 : 18 : case LEFT_PAREN:
10027 : : /* tuple type or parenthesised type - requires further disambiguation
10028 : : * (the usual). ok apparently can be a parenthesised TraitBound too, so
10029 : : * could be TraitObjectTypeOneBound */
10030 : 18 : return parse_paren_prefixed_type_no_bounds ();
10031 : 2 : case FOR:
10032 : : case ASYNC:
10033 : : case CONST:
10034 : : case UNSAFE:
10035 : : case EXTERN_KW:
10036 : : case FN_KW:
10037 : : // bare function type (with no for lifetimes)
10038 : 2 : return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
10039 : 14 : case IMPL:
10040 : 14 : lexer.skip_token ();
10041 : 28 : if (lexer.peek_token ()->get_id () == LIFETIME)
10042 : : {
10043 : : /* cannot be one bound because lifetime prevents it from being
10044 : : * traitbound not allowed as type no bounds, only here for error
10045 : : * message */
10046 : 0 : Error error (
10047 : 0 : lexer.peek_token ()->get_locus (),
10048 : : "lifetime (probably lifetime bound, in type param "
10049 : : "bounds, in ImplTraitType) is not allowed in TypeNoBounds");
10050 : 0 : add_error (std::move (error));
10051 : :
10052 : 0 : return nullptr;
10053 : 0 : }
10054 : : else
10055 : : {
10056 : : // should be trait bound, so parse trait bound
10057 : 14 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
10058 : 14 : if (initial_bound == nullptr)
10059 : : {
10060 : 0 : Error error (lexer.peek_token ()->get_locus (),
10061 : : "failed to parse ImplTraitTypeOneBound bound");
10062 : 0 : add_error (std::move (error));
10063 : :
10064 : 0 : return nullptr;
10065 : 0 : }
10066 : :
10067 : 14 : location_t locus = t->get_locus ();
10068 : :
10069 : : // ensure not a trait with multiple bounds
10070 : 14 : t = lexer.peek_token ();
10071 : 14 : if (t->get_id () == PLUS)
10072 : : {
10073 : 0 : Error error (t->get_locus (),
10074 : : "plus after trait bound means an ImplTraitType, "
10075 : : "which is not allowed as a TypeNoBounds");
10076 : 0 : add_error (std::move (error));
10077 : :
10078 : 0 : return nullptr;
10079 : 0 : }
10080 : :
10081 : 14 : return std::unique_ptr<AST::ImplTraitTypeOneBound> (
10082 : 14 : new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus));
10083 : 14 : }
10084 : 142 : case DYN:
10085 : : case QUESTION_MARK:
10086 : : {
10087 : : // either TraitObjectTypeOneBound
10088 : 142 : bool has_dyn = false;
10089 : 142 : if (t->get_id () == DYN)
10090 : : {
10091 : 142 : lexer.skip_token ();
10092 : 142 : has_dyn = true;
10093 : : }
10094 : :
10095 : 284 : if (lexer.peek_token ()->get_id () == LIFETIME)
10096 : : {
10097 : : /* means that cannot be TraitObjectTypeOneBound - so here for
10098 : : * error message */
10099 : 0 : Error error (lexer.peek_token ()->get_locus (),
10100 : : "lifetime as bound in TraitObjectTypeOneBound "
10101 : : "is not allowed, so cannot be TypeNoBounds");
10102 : 0 : add_error (std::move (error));
10103 : :
10104 : 0 : return nullptr;
10105 : 0 : }
10106 : :
10107 : : // should be trait bound, so parse trait bound
10108 : 142 : std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
10109 : 142 : if (initial_bound == nullptr)
10110 : : {
10111 : 0 : Error error (
10112 : 0 : lexer.peek_token ()->get_locus (),
10113 : : "failed to parse TraitObjectTypeOneBound initial bound");
10114 : 0 : add_error (std::move (error));
10115 : :
10116 : 0 : return nullptr;
10117 : 0 : }
10118 : :
10119 : 142 : location_t locus = t->get_locus ();
10120 : :
10121 : : // detect error with plus as next token
10122 : 142 : t = lexer.peek_token ();
10123 : 142 : if (t->get_id () == PLUS)
10124 : : {
10125 : 0 : Error error (t->get_locus (),
10126 : : "plus after trait bound means a TraitObjectType, "
10127 : : "which is not allowed as a TypeNoBounds");
10128 : 0 : add_error (std::move (error));
10129 : :
10130 : 0 : return nullptr;
10131 : 0 : }
10132 : :
10133 : : // convert trait bound to value object
10134 : 142 : AST::TraitBound value_bound (*initial_bound);
10135 : :
10136 : 142 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
10137 : 284 : new AST::TraitObjectTypeOneBound (std::move (value_bound), locus,
10138 : 142 : has_dyn));
10139 : 142 : }
10140 : 0 : default:
10141 : 0 : add_error (Error (t->get_locus (),
10142 : : "unrecognised token %qs in type no bounds",
10143 : : t->get_token_description ()));
10144 : :
10145 : 0 : return nullptr;
10146 : : }
10147 : 15585 : }
10148 : :
10149 : : // Parses a type no bounds beginning with '('.
10150 : : template <typename ManagedTokenSource>
10151 : : std::unique_ptr<AST::TypeNoBounds>
10152 : 18 : Parser<ManagedTokenSource>::parse_paren_prefixed_type_no_bounds ()
10153 : : {
10154 : : /* NOTE: this could probably be parsed without the HACK solution of
10155 : : * parse_paren_prefixed_type, but I was lazy. So FIXME for future.*/
10156 : :
10157 : : /* NOTE: again, syntactical ambiguity of a parenthesised trait bound is
10158 : : * considered a trait bound, not a parenthesised type, so that it can still
10159 : : * be used in type param bounds. */
10160 : :
10161 : 18 : location_t left_paren_locus = lexer.peek_token ()->get_locus ();
10162 : :
10163 : : // skip left delim
10164 : 18 : lexer.skip_token ();
10165 : : /* while next token isn't close delim, parse comma-separated types, saving
10166 : : * whether trailing comma happens */
10167 : 18 : const_TokenPtr t = lexer.peek_token ();
10168 : 18 : bool trailing_comma = true;
10169 : 18 : std::vector<std::unique_ptr<AST::Type>> types;
10170 : :
10171 : 18 : while (t->get_id () != RIGHT_PAREN)
10172 : : {
10173 : 2 : std::unique_ptr<AST::Type> type = parse_type ();
10174 : 2 : if (type == nullptr)
10175 : : {
10176 : 0 : Error error (t->get_locus (),
10177 : : "failed to parse type inside parentheses (probably "
10178 : : "tuple or parenthesised)");
10179 : 0 : add_error (std::move (error));
10180 : :
10181 : 0 : return nullptr;
10182 : 0 : }
10183 : 2 : types.push_back (std::move (type));
10184 : :
10185 : 2 : t = lexer.peek_token ();
10186 : 2 : if (t->get_id () != COMMA)
10187 : : {
10188 : 2 : trailing_comma = false;
10189 : : break;
10190 : : }
10191 : 0 : lexer.skip_token ();
10192 : :
10193 : 0 : t = lexer.peek_token ();
10194 : : }
10195 : :
10196 : 18 : if (!skip_token (RIGHT_PAREN))
10197 : : {
10198 : 0 : return nullptr;
10199 : : }
10200 : :
10201 : : // if only one type and no trailing comma, then not a tuple type
10202 : 18 : if (types.size () == 1 && !trailing_comma)
10203 : : {
10204 : : // must be a TraitObjectType (with more than one bound)
10205 : 4 : if (lexer.peek_token ()->get_id () == PLUS)
10206 : : {
10207 : : // error - this is not allowed for type no bounds
10208 : 0 : Error error (lexer.peek_token ()->get_locus (),
10209 : : "plus (implying TraitObjectType as type param "
10210 : : "bounds) is not allowed in type no bounds");
10211 : 0 : add_error (std::move (error));
10212 : :
10213 : 0 : return nullptr;
10214 : 0 : }
10215 : : else
10216 : : {
10217 : : // release vector pointer
10218 : 2 : std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
10219 : : /* HACK: attempt to convert to trait bound. if fails, parenthesised
10220 : : * type */
10221 : 2 : std::unique_ptr<AST::TraitBound> converted_bound (
10222 : 2 : released_ptr->to_trait_bound (true));
10223 : 2 : if (converted_bound == nullptr)
10224 : : {
10225 : : // parenthesised type
10226 : 2 : return std::unique_ptr<AST::ParenthesisedType> (
10227 : 2 : new AST::ParenthesisedType (std::move (released_ptr),
10228 : 2 : left_paren_locus));
10229 : : }
10230 : : else
10231 : : {
10232 : : // trait object type (one bound)
10233 : :
10234 : : // get value semantics trait bound
10235 : 0 : AST::TraitBound value_bound (*converted_bound);
10236 : :
10237 : 0 : return std::unique_ptr<AST::TraitObjectTypeOneBound> (
10238 : 0 : new AST::TraitObjectTypeOneBound (value_bound,
10239 : 0 : left_paren_locus));
10240 : 0 : }
10241 : 2 : }
10242 : : }
10243 : : else
10244 : : {
10245 : 16 : return std::unique_ptr<AST::TupleType> (
10246 : 16 : new AST::TupleType (std::move (types), left_paren_locus));
10247 : : }
10248 : : /* TODO: ensure that this ensures that dynamic dispatch for traits is not
10249 : : * lost somehow */
10250 : 18 : }
10251 : :
10252 : : /* Parses a literal pattern or range pattern. Assumes that literals passed in
10253 : : * are valid range pattern bounds. Do not pass in paths in expressions, for
10254 : : * instance. */
10255 : : template <typename ManagedTokenSource>
10256 : : std::unique_ptr<AST::Pattern>
10257 : 293 : Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
10258 : : {
10259 : 293 : const_TokenPtr range_lower = lexer.peek_token ();
10260 : 293 : AST::Literal::LitType type = AST::Literal::STRING;
10261 : 293 : bool has_minus = false;
10262 : :
10263 : : // get lit type
10264 : 293 : switch (range_lower->get_id ())
10265 : : {
10266 : 28 : case CHAR_LITERAL:
10267 : 28 : type = AST::Literal::CHAR;
10268 : 28 : lexer.skip_token ();
10269 : : break;
10270 : 21 : case BYTE_CHAR_LITERAL:
10271 : 21 : type = AST::Literal::BYTE;
10272 : 21 : lexer.skip_token ();
10273 : : break;
10274 : 243 : case INT_LITERAL:
10275 : 243 : type = AST::Literal::INT;
10276 : 243 : lexer.skip_token ();
10277 : : break;
10278 : 1 : case FLOAT_LITERAL:
10279 : 1 : type = AST::Literal::FLOAT;
10280 : 1 : lexer.skip_token ();
10281 : : break;
10282 : 0 : case MINUS:
10283 : : // branch on next token
10284 : 0 : range_lower = lexer.peek_token (1);
10285 : 0 : switch (range_lower->get_id ())
10286 : : {
10287 : 0 : case INT_LITERAL:
10288 : 0 : type = AST::Literal::INT;
10289 : 0 : has_minus = true;
10290 : 0 : lexer.skip_token (1);
10291 : 0 : break;
10292 : 0 : case FLOAT_LITERAL:
10293 : 0 : type = AST::Literal::FLOAT;
10294 : 0 : has_minus = true;
10295 : 0 : lexer.skip_token (1);
10296 : 0 : break;
10297 : 0 : default:
10298 : 0 : add_error (Error (range_lower->get_locus (),
10299 : : "token type %qs cannot be parsed as range pattern "
10300 : : "bound or literal after minus symbol",
10301 : : range_lower->get_token_description ()));
10302 : :
10303 : 0 : return nullptr;
10304 : : }
10305 : : break;
10306 : 0 : default:
10307 : 0 : add_error (
10308 : 0 : Error (range_lower->get_locus (),
10309 : : "token type %qs cannot be parsed as range pattern bound",
10310 : : range_lower->get_token_description ()));
10311 : :
10312 : 0 : return nullptr;
10313 : : }
10314 : :
10315 : 293 : const_TokenPtr next = lexer.peek_token ();
10316 : 293 : if (next->get_id () == DOT_DOT_EQ || next->get_id () == ELLIPSIS
10317 : 572 : || next->get_id () == DOT_DOT)
10318 : : {
10319 : 18 : AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
10320 : : // range pattern
10321 : 18 : lexer.skip_token ();
10322 : 18 : std::unique_ptr<AST::RangePatternBound> lower (
10323 : 36 : new AST::RangePatternBoundLiteral (
10324 : 54 : AST::Literal (range_lower->get_str (), type,
10325 : : PrimitiveCoreType::CORETYPE_UNKNOWN),
10326 : : range_lower->get_locus (), has_minus));
10327 : :
10328 : 18 : std::unique_ptr<AST::RangePatternBound> upper
10329 : : = parse_range_pattern_bound ();
10330 : 18 : if (upper == nullptr)
10331 : : {
10332 : 0 : Error error (next->get_locus (),
10333 : : "failed to parse range pattern bound in range pattern");
10334 : 0 : add_error (std::move (error));
10335 : :
10336 : 0 : return nullptr;
10337 : 0 : }
10338 : :
10339 : 18 : return std::unique_ptr<AST::RangePattern> (
10340 : 18 : new AST::RangePattern (std::move (lower), std::move (upper), kind,
10341 : 18 : range_lower->get_locus ()));
10342 : 18 : }
10343 : : else
10344 : : {
10345 : : // literal pattern
10346 : 275 : return std::unique_ptr<AST::LiteralPattern> (
10347 : 868 : new AST::LiteralPattern (range_lower->get_str (), type,
10348 : : range_lower->get_locus (),
10349 : 275 : range_lower->get_type_hint ()));
10350 : : }
10351 : 293 : }
10352 : :
10353 : : // Parses a range pattern bound (value only).
10354 : : template <typename ManagedTokenSource>
10355 : : std::unique_ptr<AST::RangePatternBound>
10356 : 26 : Parser<ManagedTokenSource>::parse_range_pattern_bound ()
10357 : : {
10358 : 26 : const_TokenPtr range_lower = lexer.peek_token ();
10359 : 26 : location_t range_lower_locus = range_lower->get_locus ();
10360 : :
10361 : : // get lit type
10362 : 26 : switch (range_lower->get_id ())
10363 : : {
10364 : 7 : case CHAR_LITERAL:
10365 : 7 : lexer.skip_token ();
10366 : 7 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10367 : 14 : new AST::RangePatternBoundLiteral (
10368 : 28 : AST::Literal (range_lower->get_str (), AST::Literal::CHAR,
10369 : : range_lower->get_type_hint ()),
10370 : 7 : range_lower_locus));
10371 : 0 : case BYTE_CHAR_LITERAL:
10372 : 0 : lexer.skip_token ();
10373 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10374 : 0 : new AST::RangePatternBoundLiteral (
10375 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::BYTE,
10376 : : range_lower->get_type_hint ()),
10377 : 0 : range_lower_locus));
10378 : 4 : case INT_LITERAL:
10379 : 4 : lexer.skip_token ();
10380 : 4 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10381 : 8 : new AST::RangePatternBoundLiteral (
10382 : 12 : AST::Literal (range_lower->get_str (), AST::Literal::INT,
10383 : : range_lower->get_type_hint ()),
10384 : 4 : range_lower_locus));
10385 : 0 : case FLOAT_LITERAL:
10386 : 0 : lexer.skip_token ();
10387 : 0 : rust_debug ("warning: used deprecated float range pattern bound");
10388 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10389 : 0 : new AST::RangePatternBoundLiteral (
10390 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
10391 : : range_lower->get_type_hint ()),
10392 : 0 : range_lower_locus));
10393 : 0 : case MINUS:
10394 : : // branch on next token
10395 : 0 : range_lower = lexer.peek_token (1);
10396 : 0 : switch (range_lower->get_id ())
10397 : : {
10398 : 0 : case INT_LITERAL:
10399 : 0 : lexer.skip_token (1);
10400 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10401 : 0 : new AST::RangePatternBoundLiteral (
10402 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::INT,
10403 : : range_lower->get_type_hint ()),
10404 : 0 : range_lower_locus, true));
10405 : 0 : case FLOAT_LITERAL:
10406 : 0 : lexer.skip_token (1);
10407 : 0 : rust_debug ("warning: used deprecated float range pattern bound");
10408 : 0 : return std::unique_ptr<AST::RangePatternBoundLiteral> (
10409 : 0 : new AST::RangePatternBoundLiteral (
10410 : 0 : AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
10411 : : range_lower->get_type_hint ()),
10412 : 0 : range_lower_locus, true));
10413 : 0 : default:
10414 : 0 : add_error (Error (range_lower->get_locus (),
10415 : : "token type %qs cannot be parsed as range pattern "
10416 : : "bound after minus symbol",
10417 : : range_lower->get_token_description ()));
10418 : :
10419 : 0 : return nullptr;
10420 : : }
10421 : 15 : case IDENTIFIER:
10422 : : case SUPER:
10423 : : case SELF:
10424 : : case SELF_ALIAS:
10425 : : case CRATE:
10426 : : case SCOPE_RESOLUTION:
10427 : : case DOLLAR_SIGN:
10428 : : {
10429 : : // path in expression
10430 : 15 : AST::PathInExpression path = parse_path_in_expression ();
10431 : 15 : if (path.is_error ())
10432 : : {
10433 : 0 : Error error (
10434 : : range_lower->get_locus (),
10435 : : "failed to parse path in expression range pattern bound");
10436 : 0 : add_error (std::move (error));
10437 : :
10438 : 0 : return nullptr;
10439 : 0 : }
10440 : 15 : return std::unique_ptr<AST::RangePatternBoundPath> (
10441 : 15 : new AST::RangePatternBoundPath (std::move (path)));
10442 : 15 : }
10443 : 0 : case LEFT_SHIFT:
10444 : : case LEFT_ANGLE:
10445 : : {
10446 : : // qualified path in expression
10447 : 0 : AST::QualifiedPathInExpression path
10448 : : = parse_qualified_path_in_expression ();
10449 : 0 : if (path.is_error ())
10450 : : {
10451 : 0 : Error error (range_lower->get_locus (),
10452 : : "failed to parse qualified path in expression range "
10453 : : "pattern bound");
10454 : 0 : add_error (std::move (error));
10455 : :
10456 : 0 : return nullptr;
10457 : 0 : }
10458 : 0 : return std::unique_ptr<AST::RangePatternBoundQualPath> (
10459 : 0 : new AST::RangePatternBoundQualPath (std::move (path)));
10460 : 0 : }
10461 : 0 : default:
10462 : 0 : add_error (
10463 : 0 : Error (range_lower->get_locus (),
10464 : : "token type %qs cannot be parsed as range pattern bound",
10465 : : range_lower->get_token_description ()));
10466 : :
10467 : 0 : return nullptr;
10468 : : }
10469 : 26 : }
10470 : :
10471 : : template <typename ManagedTokenSource>
10472 : : std::unique_ptr<AST::Pattern>
10473 : 26968 : Parser<ManagedTokenSource>::parse_pattern ()
10474 : : {
10475 : 26968 : location_t start_locus = lexer.peek_token ()->get_locus ();
10476 : :
10477 : : /* skip optional starting pipe */
10478 : 26968 : maybe_skip_token (PIPE);
10479 : :
10480 : 26968 : auto first = parse_pattern_no_alt ();
10481 : :
10482 : 53936 : if (lexer.peek_token ()->get_id () != PIPE)
10483 : : /* no alternates */
10484 : 26778 : return first;
10485 : :
10486 : 190 : std::vector<std::unique_ptr<AST::Pattern>> alts;
10487 : 190 : alts.push_back (std::move (first));
10488 : :
10489 : : do
10490 : : {
10491 : 205 : lexer.skip_token ();
10492 : 205 : alts.push_back (parse_pattern_no_alt ());
10493 : : }
10494 : :
10495 : 410 : while (lexer.peek_token ()->get_id () == PIPE);
10496 : :
10497 : : /* alternates */
10498 : : return std::unique_ptr<AST::Pattern> (
10499 : 190 : new AST::AltPattern (std::move (alts), start_locus));
10500 : 26968 : }
10501 : :
10502 : : // Parses a pattern without alternates ('|')
10503 : : // (will further disambiguate any pattern).
10504 : : template <typename ManagedTokenSource>
10505 : : std::unique_ptr<AST::Pattern>
10506 : 27276 : Parser<ManagedTokenSource>::parse_pattern_no_alt ()
10507 : : {
10508 : 27276 : const_TokenPtr t = lexer.peek_token ();
10509 : 27276 : switch (t->get_id ())
10510 : : {
10511 : 17 : case TRUE_LITERAL:
10512 : 17 : lexer.skip_token ();
10513 : 17 : return std::unique_ptr<AST::LiteralPattern> (
10514 : 51 : new AST::LiteralPattern (Values::Keywords::TRUE_LITERAL,
10515 : : AST::Literal::BOOL, t->get_locus (),
10516 : 17 : t->get_type_hint ()));
10517 : 9 : case FALSE_LITERAL:
10518 : 9 : lexer.skip_token ();
10519 : 9 : return std::unique_ptr<AST::LiteralPattern> (
10520 : 27 : new AST::LiteralPattern (Values::Keywords::FALSE_LITERAL,
10521 : : AST::Literal::BOOL, t->get_locus (),
10522 : 9 : t->get_type_hint ()));
10523 : 293 : case CHAR_LITERAL:
10524 : : case BYTE_CHAR_LITERAL:
10525 : : case INT_LITERAL:
10526 : : case FLOAT_LITERAL:
10527 : 293 : return parse_literal_or_range_pattern ();
10528 : 0 : case STRING_LITERAL:
10529 : 0 : lexer.skip_token ();
10530 : 0 : return std::unique_ptr<AST::LiteralPattern> (
10531 : 0 : new AST::LiteralPattern (t->get_str (), AST::Literal::STRING,
10532 : 0 : t->get_locus (), t->get_type_hint ()));
10533 : 0 : case BYTE_STRING_LITERAL:
10534 : 0 : lexer.skip_token ();
10535 : 0 : return std::unique_ptr<AST::LiteralPattern> (
10536 : 0 : new AST::LiteralPattern (t->get_str (), AST::Literal::BYTE_STRING,
10537 : 0 : t->get_locus (), t->get_type_hint ()));
10538 : 0 : case RAW_STRING_LITERAL:
10539 : 0 : lexer.skip_token ();
10540 : 0 : return std::unique_ptr<AST::LiteralPattern> (
10541 : 0 : new AST::LiteralPattern (t->get_str (), AST::Literal::RAW_STRING,
10542 : 0 : t->get_locus (), t->get_type_hint ()));
10543 : : // raw string and raw byte string literals too if they are readded to
10544 : : // lexer
10545 : 0 : case MINUS:
10546 : 0 : if (lexer.peek_token (1)->get_id () == INT_LITERAL)
10547 : : {
10548 : 0 : return parse_literal_or_range_pattern ();
10549 : : }
10550 : 0 : else if (lexer.peek_token (1)->get_id () == FLOAT_LITERAL)
10551 : : {
10552 : 0 : return parse_literal_or_range_pattern ();
10553 : : }
10554 : : else
10555 : : {
10556 : 0 : Error error (t->get_locus (), "unexpected token %<-%> in pattern - "
10557 : : "did you forget an integer literal");
10558 : 0 : add_error (std::move (error));
10559 : :
10560 : 0 : return nullptr;
10561 : 0 : }
10562 : 962 : case UNDERSCORE:
10563 : 962 : lexer.skip_token ();
10564 : 962 : return std::unique_ptr<AST::WildcardPattern> (
10565 : 962 : new AST::WildcardPattern (t->get_locus ()));
10566 : 0 : case DOT_DOT:
10567 : 0 : lexer.skip_token ();
10568 : 0 : return std::unique_ptr<AST::RestPattern> (
10569 : 0 : new AST::RestPattern (t->get_locus ()));
10570 : 874 : case REF:
10571 : : case MUT:
10572 : 874 : return parse_identifier_pattern ();
10573 : 24646 : case IDENTIFIER:
10574 : : /* if identifier with no scope resolution afterwards, identifier
10575 : : * pattern. if scope resolution afterwards, path pattern (or range
10576 : : * pattern or struct pattern or tuple struct pattern) or macro
10577 : : * invocation */
10578 : 24646 : return parse_ident_leading_pattern ();
10579 : 32 : case AMP:
10580 : : case LOGICAL_AND:
10581 : : // reference pattern
10582 : 32 : return parse_reference_pattern ();
10583 : 402 : case LEFT_PAREN:
10584 : : // tuple pattern or grouped pattern
10585 : 402 : return parse_grouped_or_tuple_pattern ();
10586 : 35 : case LEFT_SQUARE:
10587 : : // slice pattern
10588 : 35 : return parse_slice_pattern ();
10589 : 0 : case LEFT_SHIFT:
10590 : : case LEFT_ANGLE:
10591 : : {
10592 : : // qualified path in expression or qualified range pattern bound
10593 : 0 : AST::QualifiedPathInExpression path
10594 : : = parse_qualified_path_in_expression ();
10595 : :
10596 : 0 : if (lexer.peek_token ()->get_id () == DOT_DOT_EQ
10597 : 0 : || lexer.peek_token ()->get_id () == ELLIPSIS
10598 : 0 : || lexer.peek_token ()->get_id () == DOT_DOT)
10599 : : {
10600 : : // qualified range pattern bound, so parse rest of range pattern
10601 : : AST::RangeKind kind
10602 : 0 : = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
10603 : 0 : lexer.skip_token ();
10604 : :
10605 : 0 : std::unique_ptr<AST::RangePatternBoundQualPath> lower_bound (
10606 : 0 : new AST::RangePatternBoundQualPath (std::move (path)));
10607 : 0 : std::unique_ptr<AST::RangePatternBound> upper_bound
10608 : : = parse_range_pattern_bound ();
10609 : :
10610 : 0 : return std::unique_ptr<AST::RangePattern> (
10611 : 0 : new AST::RangePattern (std::move (lower_bound),
10612 : : std::move (upper_bound), kind,
10613 : 0 : t->get_locus ()));
10614 : 0 : }
10615 : : else
10616 : : {
10617 : : // just qualified path in expression
10618 : 0 : return std::unique_ptr<AST::QualifiedPathInExpression> (
10619 : 0 : new AST::QualifiedPathInExpression (std::move (path)));
10620 : : }
10621 : 0 : }
10622 : 6 : case SUPER:
10623 : : case SELF:
10624 : : case SELF_ALIAS:
10625 : : case CRATE:
10626 : : case SCOPE_RESOLUTION:
10627 : : case DOLLAR_SIGN:
10628 : : {
10629 : : // path in expression or range pattern bound
10630 : 6 : AST::PathInExpression path = parse_path_in_expression ();
10631 : :
10632 : 6 : const_TokenPtr next = lexer.peek_token ();
10633 : 6 : switch (next->get_id ())
10634 : : {
10635 : 0 : case DOT_DOT_EQ:
10636 : : case DOT_DOT:
10637 : : case ELLIPSIS:
10638 : : {
10639 : : // qualified range pattern bound, so parse rest of range pattern
10640 : 0 : AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
10641 : 0 : lexer.skip_token ();
10642 : :
10643 : 0 : std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
10644 : 0 : new AST::RangePatternBoundPath (std::move (path)));
10645 : 0 : std::unique_ptr<AST::RangePatternBound> upper_bound
10646 : : = parse_range_pattern_bound ();
10647 : :
10648 : 0 : return std::unique_ptr<AST::RangePattern> (
10649 : 0 : new AST::RangePattern (std::move (lower_bound),
10650 : : std::move (upper_bound), kind,
10651 : 0 : next->get_locus ()));
10652 : 0 : }
10653 : 0 : case EXCLAM:
10654 : 0 : return parse_macro_invocation_partial (std::move (path),
10655 : 0 : AST::AttrVec ());
10656 : 0 : case LEFT_PAREN:
10657 : : {
10658 : : // tuple struct
10659 : 0 : lexer.skip_token ();
10660 : :
10661 : : // parse items
10662 : 0 : std::unique_ptr<AST::TupleStructItems> items
10663 : : = parse_tuple_struct_items ();
10664 : 0 : if (items == nullptr)
10665 : : {
10666 : 0 : Error error (lexer.peek_token ()->get_locus (),
10667 : : "failed to parse tuple struct items");
10668 : 0 : add_error (std::move (error));
10669 : :
10670 : 0 : return nullptr;
10671 : 0 : }
10672 : :
10673 : 0 : if (!skip_token (RIGHT_PAREN))
10674 : : {
10675 : 0 : return nullptr;
10676 : : }
10677 : :
10678 : 0 : return std::unique_ptr<AST::TupleStructPattern> (
10679 : 0 : new AST::TupleStructPattern (std::move (path),
10680 : 0 : std::move (items)));
10681 : 0 : }
10682 : 0 : case LEFT_CURLY:
10683 : : {
10684 : : // struct
10685 : 0 : lexer.skip_token ();
10686 : :
10687 : : // parse elements (optional)
10688 : 0 : AST::StructPatternElements elems = parse_struct_pattern_elems ();
10689 : :
10690 : 0 : if (!skip_token (RIGHT_CURLY))
10691 : : {
10692 : 0 : return nullptr;
10693 : : }
10694 : :
10695 : 0 : return std::unique_ptr<AST::StructPattern> (
10696 : 0 : new AST::StructPattern (std::move (path), t->get_locus (),
10697 : 0 : std::move (elems)));
10698 : 0 : }
10699 : 6 : default:
10700 : : // assume path in expression
10701 : 6 : return std::unique_ptr<AST::PathInExpression> (
10702 : 6 : new AST::PathInExpression (std::move (path)));
10703 : : }
10704 : 6 : }
10705 : 0 : default:
10706 : 0 : add_error (Error (t->get_locus (), "unexpected token %qs in pattern",
10707 : : t->get_token_description ()));
10708 : :
10709 : 0 : return nullptr;
10710 : : }
10711 : 27276 : }
10712 : :
10713 : : // Parses a single or double reference pattern.
10714 : : template <typename ManagedTokenSource>
10715 : : std::unique_ptr<AST::ReferencePattern>
10716 : 32 : Parser<ManagedTokenSource>::parse_reference_pattern ()
10717 : : {
10718 : : // parse double or single ref
10719 : 32 : bool is_double_ref = false;
10720 : 32 : const_TokenPtr t = lexer.peek_token ();
10721 : 32 : switch (t->get_id ())
10722 : : {
10723 : 23 : case AMP:
10724 : : // still false
10725 : 23 : lexer.skip_token ();
10726 : : break;
10727 : 9 : case LOGICAL_AND:
10728 : 9 : is_double_ref = true;
10729 : 9 : lexer.skip_token ();
10730 : : break;
10731 : 0 : default:
10732 : 0 : add_error (Error (t->get_locus (),
10733 : : "unexpected token %qs in reference pattern",
10734 : : t->get_token_description ()));
10735 : :
10736 : 0 : return nullptr;
10737 : : }
10738 : :
10739 : : // parse mut (if it exists)
10740 : 32 : bool is_mut = false;
10741 : 64 : if (lexer.peek_token ()->get_id () == MUT)
10742 : : {
10743 : 2 : is_mut = true;
10744 : 2 : lexer.skip_token ();
10745 : : }
10746 : :
10747 : : // parse pattern to get reference of (required)
10748 : 32 : std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
10749 : 32 : if (pattern == nullptr)
10750 : : {
10751 : 0 : Error error (lexer.peek_token ()->get_locus (),
10752 : : "failed to parse pattern in reference pattern");
10753 : 0 : add_error (std::move (error));
10754 : :
10755 : : // skip somewhere?
10756 : 0 : return nullptr;
10757 : 0 : }
10758 : :
10759 : : return std::unique_ptr<AST::ReferencePattern> (
10760 : 32 : new AST::ReferencePattern (std::move (pattern), is_mut, is_double_ref,
10761 : 32 : t->get_locus ()));
10762 : 32 : }
10763 : :
10764 : : /* Parses a grouped pattern or tuple pattern. Prefers grouped over tuple if
10765 : : * only a single element with no commas. */
10766 : : template <typename ManagedTokenSource>
10767 : : std::unique_ptr<AST::Pattern>
10768 : 402 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_pattern ()
10769 : : {
10770 : 402 : location_t paren_locus = lexer.peek_token ()->get_locus ();
10771 : 402 : skip_token (LEFT_PAREN);
10772 : :
10773 : : // detect '..' token (ranged with no lower range)
10774 : 804 : if (lexer.peek_token ()->get_id () == DOT_DOT)
10775 : : {
10776 : 0 : lexer.skip_token ();
10777 : :
10778 : : // parse new patterns while next token is a comma
10779 : 0 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
10780 : :
10781 : 0 : const_TokenPtr t = lexer.peek_token ();
10782 : 0 : while (t->get_id () == COMMA)
10783 : : {
10784 : 0 : lexer.skip_token ();
10785 : :
10786 : : // break if next token is ')'
10787 : 0 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10788 : : {
10789 : : break;
10790 : : }
10791 : :
10792 : : // parse pattern, which is required
10793 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10794 : 0 : if (pattern == nullptr)
10795 : : {
10796 : 0 : Error error (
10797 : 0 : lexer.peek_token ()->get_locus (),
10798 : : "failed to parse pattern inside ranged tuple pattern");
10799 : 0 : add_error (std::move (error));
10800 : :
10801 : : // skip somewhere?
10802 : 0 : return nullptr;
10803 : 0 : }
10804 : 0 : patterns.push_back (std::move (pattern));
10805 : :
10806 : 0 : t = lexer.peek_token ();
10807 : : }
10808 : :
10809 : 0 : if (!skip_token (RIGHT_PAREN))
10810 : : {
10811 : : // skip somewhere?
10812 : 0 : return nullptr;
10813 : : }
10814 : :
10815 : : // create ranged tuple pattern items with only upper items
10816 : 0 : std::unique_ptr<AST::TuplePatternItemsRanged> items (
10817 : 0 : new AST::TuplePatternItemsRanged (
10818 : 0 : std::vector<std::unique_ptr<AST::Pattern>> (), std::move (patterns)));
10819 : 0 : return std::unique_ptr<AST::TuplePattern> (
10820 : 0 : new AST::TuplePattern (std::move (items), paren_locus));
10821 : 0 : }
10822 : 804 : else if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10823 : : {
10824 : 2 : skip_token (RIGHT_PAREN);
10825 : 2 : auto items = std::unique_ptr<AST::TuplePatternItemsMultiple> (
10826 : 2 : new AST::TuplePatternItemsMultiple (
10827 : 2 : std::vector<std::unique_ptr<AST::Pattern>> ()));
10828 : 2 : return std::unique_ptr<AST::TuplePattern> (
10829 : 2 : new AST::TuplePattern (std::move (items), paren_locus));
10830 : 2 : }
10831 : :
10832 : : // parse initial pattern (required)
10833 : 400 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
10834 : 400 : if (initial_pattern == nullptr)
10835 : : {
10836 : 0 : Error error (lexer.peek_token ()->get_locus (),
10837 : : "failed to parse pattern in grouped or tuple pattern");
10838 : 0 : add_error (std::move (error));
10839 : :
10840 : 0 : return nullptr;
10841 : 0 : }
10842 : :
10843 : : // branch on whether next token is a comma or not
10844 : 400 : const_TokenPtr t = lexer.peek_token ();
10845 : 400 : switch (t->get_id ())
10846 : : {
10847 : 43 : case RIGHT_PAREN:
10848 : : // grouped pattern
10849 : 43 : lexer.skip_token ();
10850 : :
10851 : 43 : return std::unique_ptr<AST::GroupedPattern> (
10852 : 43 : new AST::GroupedPattern (std::move (initial_pattern), paren_locus));
10853 : 357 : case COMMA:
10854 : : {
10855 : : // tuple pattern
10856 : 357 : lexer.skip_token ();
10857 : :
10858 : : // create vector of patterns
10859 : 357 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
10860 : 357 : patterns.push_back (std::move (initial_pattern));
10861 : :
10862 : 357 : t = lexer.peek_token ();
10863 : 703 : while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
10864 : : {
10865 : : // parse pattern (required)
10866 : 346 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10867 : 346 : if (pattern == nullptr)
10868 : : {
10869 : 0 : Error error (t->get_locus (),
10870 : : "failed to parse pattern in tuple pattern");
10871 : 0 : add_error (std::move (error));
10872 : :
10873 : 0 : return nullptr;
10874 : 0 : }
10875 : 346 : patterns.push_back (std::move (pattern));
10876 : :
10877 : 692 : if (lexer.peek_token ()->get_id () != COMMA)
10878 : : break;
10879 : :
10880 : 21 : lexer.skip_token ();
10881 : 21 : t = lexer.peek_token ();
10882 : : }
10883 : :
10884 : 357 : t = lexer.peek_token ();
10885 : 357 : if (t->get_id () == RIGHT_PAREN)
10886 : : {
10887 : : // non-ranged tuple pattern
10888 : 333 : lexer.skip_token ();
10889 : :
10890 : 333 : std::unique_ptr<AST::TuplePatternItemsMultiple> items (
10891 : 333 : new AST::TuplePatternItemsMultiple (std::move (patterns)));
10892 : 333 : return std::unique_ptr<AST::TuplePattern> (
10893 : 333 : new AST::TuplePattern (std::move (items), paren_locus));
10894 : 333 : }
10895 : 24 : else if (t->get_id () == DOT_DOT)
10896 : : {
10897 : : // ranged tuple pattern
10898 : 24 : lexer.skip_token ();
10899 : :
10900 : : // parse upper patterns
10901 : 24 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
10902 : 24 : t = lexer.peek_token ();
10903 : 51 : while (t->get_id () == COMMA)
10904 : : {
10905 : 27 : lexer.skip_token ();
10906 : :
10907 : : // break if end
10908 : 54 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10909 : : break;
10910 : :
10911 : : // parse pattern (required)
10912 : 27 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
10913 : 27 : if (pattern == nullptr)
10914 : : {
10915 : 0 : Error error (lexer.peek_token ()->get_locus (),
10916 : : "failed to parse pattern in tuple pattern");
10917 : 0 : add_error (std::move (error));
10918 : :
10919 : 0 : return nullptr;
10920 : 0 : }
10921 : 27 : upper_patterns.push_back (std::move (pattern));
10922 : :
10923 : 27 : t = lexer.peek_token ();
10924 : : }
10925 : :
10926 : 24 : if (!skip_token (RIGHT_PAREN))
10927 : : {
10928 : 0 : return nullptr;
10929 : : }
10930 : :
10931 : 24 : std::unique_ptr<AST::TuplePatternItemsRanged> items (
10932 : 24 : new AST::TuplePatternItemsRanged (std::move (patterns),
10933 : : std::move (upper_patterns)));
10934 : 24 : return std::unique_ptr<AST::TuplePattern> (
10935 : 24 : new AST::TuplePattern (std::move (items), paren_locus));
10936 : 24 : }
10937 : : else
10938 : : {
10939 : : // some kind of error
10940 : 0 : Error error (t->get_locus (),
10941 : : "failed to parse tuple pattern (probably) or maybe "
10942 : : "grouped pattern");
10943 : 0 : add_error (std::move (error));
10944 : :
10945 : 0 : return nullptr;
10946 : 0 : }
10947 : 357 : }
10948 : 0 : default:
10949 : : // error
10950 : 0 : add_error (Error (t->get_locus (),
10951 : : "unrecognised token %qs in grouped or tuple pattern "
10952 : : "after first pattern",
10953 : : t->get_token_description ()));
10954 : :
10955 : 0 : return nullptr;
10956 : : }
10957 : 400 : }
10958 : :
10959 : : /* Parses a slice pattern that can match arrays or slices. Parses the square
10960 : : * brackets too. */
10961 : : template <typename ManagedTokenSource>
10962 : : std::unique_ptr<AST::SlicePattern>
10963 : 35 : Parser<ManagedTokenSource>::parse_slice_pattern ()
10964 : : {
10965 : 70 : location_t square_locus = lexer.peek_token ()->get_locus ();
10966 : 35 : std::vector<std::unique_ptr<AST::Pattern>> patterns;
10967 : 35 : tl::optional<std::vector<std::unique_ptr<AST::Pattern>>> upper_patterns
10968 : : = tl::nullopt;
10969 : :
10970 : : // lambda function to determine which vector to push new patterns into
10971 : 35 : auto get_pattern_ref
10972 : 33 : = [&] () -> std::vector<std::unique_ptr<AST::Pattern>> & {
10973 : 33 : return upper_patterns.has_value () ? upper_patterns.value () : patterns;
10974 : : };
10975 : :
10976 : 35 : skip_token (LEFT_SQUARE);
10977 : :
10978 : 70 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
10979 : : {
10980 : 1 : skip_token (RIGHT_SQUARE);
10981 : 1 : std::unique_ptr<AST::SlicePatternItemsNoRest> items (
10982 : 1 : new AST::SlicePatternItemsNoRest (std::move (patterns)));
10983 : : return std::unique_ptr<AST::SlicePattern> (
10984 : 1 : new AST::SlicePattern (std::move (items), square_locus));
10985 : 1 : }
10986 : :
10987 : : // parse initial pattern (required)
10988 : 68 : if (lexer.peek_token ()->get_id () == DOT_DOT)
10989 : : {
10990 : 1 : lexer.skip_token ();
10991 : 1 : upper_patterns = std::vector<std::unique_ptr<AST::Pattern>> ();
10992 : : }
10993 : : else
10994 : : {
10995 : : // Not a rest pattern `..`, parse normally
10996 : 33 : std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
10997 : 33 : if (initial_pattern == nullptr)
10998 : : {
10999 : 0 : Error error (lexer.peek_token ()->get_locus (),
11000 : : "failed to parse initial pattern in slice pattern");
11001 : 0 : add_error (std::move (error));
11002 : :
11003 : 0 : return nullptr;
11004 : 0 : }
11005 : :
11006 : 33 : patterns.push_back (std::move (initial_pattern));
11007 : 33 : }
11008 : :
11009 : 34 : const_TokenPtr t = lexer.peek_token ();
11010 : 68 : while (t->get_id () == COMMA)
11011 : : {
11012 : 34 : lexer.skip_token ();
11013 : :
11014 : : // break if end bracket
11015 : 68 : if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
11016 : : break;
11017 : :
11018 : 68 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11019 : : {
11020 : 1 : if (upper_patterns.has_value ())
11021 : : {
11022 : : // DOT_DOT has been parsed before
11023 : 0 : Error error (lexer.peek_token ()->get_locus (), "%s",
11024 : : "`..` can only be used once per slice pattern");
11025 : 0 : add_error (std::move (error));
11026 : :
11027 : 0 : return nullptr;
11028 : 0 : }
11029 : 1 : upper_patterns = std::vector<std::unique_ptr<AST::Pattern>> ();
11030 : 1 : lexer.skip_token ();
11031 : 1 : t = lexer.peek_token ();
11032 : 1 : continue;
11033 : 1 : }
11034 : :
11035 : : // parse pattern (required)
11036 : 33 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11037 : 33 : if (pattern == nullptr)
11038 : : {
11039 : 0 : Error error (lexer.peek_token ()->get_locus (),
11040 : : "failed to parse pattern in slice pattern");
11041 : 0 : add_error (std::move (error));
11042 : :
11043 : 0 : return nullptr;
11044 : 0 : }
11045 : 33 : get_pattern_ref ().push_back (std::move (pattern));
11046 : :
11047 : 33 : t = lexer.peek_token ();
11048 : : }
11049 : :
11050 : 34 : if (!skip_token (RIGHT_SQUARE))
11051 : : {
11052 : 0 : return nullptr;
11053 : : }
11054 : :
11055 : 34 : if (upper_patterns.has_value ())
11056 : : {
11057 : : // Slice pattern with rest
11058 : 2 : std::unique_ptr<AST::SlicePatternItemsHasRest> items (
11059 : 4 : new AST::SlicePatternItemsHasRest (
11060 : 2 : std::move (patterns), std::move (upper_patterns.value ())));
11061 : : return std::unique_ptr<AST::SlicePattern> (
11062 : 2 : new AST::SlicePattern (std::move (items), square_locus));
11063 : 2 : }
11064 : :
11065 : : // Rest-less slice pattern
11066 : 32 : std::unique_ptr<AST::SlicePatternItemsNoRest> items (
11067 : 32 : new AST::SlicePatternItemsNoRest (std::move (patterns)));
11068 : : return std::unique_ptr<AST::SlicePattern> (
11069 : 32 : new AST::SlicePattern (std::move (items), square_locus));
11070 : 101 : }
11071 : :
11072 : : /* Parses an identifier pattern (pattern that binds a value matched to a
11073 : : * variable). */
11074 : : template <typename ManagedTokenSource>
11075 : : std::unique_ptr<AST::IdentifierPattern>
11076 : 874 : Parser<ManagedTokenSource>::parse_identifier_pattern ()
11077 : : {
11078 : 874 : location_t locus = lexer.peek_token ()->get_locus ();
11079 : :
11080 : 874 : bool has_ref = false;
11081 : 1748 : if (lexer.peek_token ()->get_id () == REF)
11082 : : {
11083 : 3 : has_ref = true;
11084 : 3 : lexer.skip_token ();
11085 : :
11086 : : // DEBUG
11087 : 3 : rust_debug ("parsed ref in identifier pattern");
11088 : : }
11089 : :
11090 : 874 : bool has_mut = false;
11091 : 1748 : if (lexer.peek_token ()->get_id () == MUT)
11092 : : {
11093 : 873 : has_mut = true;
11094 : 873 : lexer.skip_token ();
11095 : : }
11096 : :
11097 : : // parse identifier (required)
11098 : 874 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
11099 : 874 : if (ident_tok == nullptr)
11100 : : {
11101 : : // skip somewhere?
11102 : 0 : return nullptr;
11103 : : }
11104 : 874 : Identifier ident{ident_tok};
11105 : :
11106 : : // DEBUG
11107 : 874 : rust_debug ("parsed identifier in identifier pattern");
11108 : :
11109 : : // parse optional pattern binding thing
11110 : 874 : std::unique_ptr<AST::Pattern> bind_pattern = nullptr;
11111 : 1748 : if (lexer.peek_token ()->get_id () == PATTERN_BIND)
11112 : : {
11113 : 0 : lexer.skip_token ();
11114 : :
11115 : : // parse required pattern to bind
11116 : 0 : bind_pattern = parse_pattern ();
11117 : 0 : if (bind_pattern == nullptr)
11118 : : {
11119 : 0 : Error error (lexer.peek_token ()->get_locus (),
11120 : : "failed to parse pattern to bind in identifier pattern");
11121 : 0 : add_error (std::move (error));
11122 : :
11123 : 0 : return nullptr;
11124 : 0 : }
11125 : : }
11126 : :
11127 : : // DEBUG
11128 : 874 : rust_debug ("about to return identifier pattern");
11129 : :
11130 : : return std::unique_ptr<AST::IdentifierPattern> (
11131 : 874 : new AST::IdentifierPattern (std::move (ident), locus, has_ref, has_mut,
11132 : 874 : std::move (bind_pattern)));
11133 : 874 : }
11134 : :
11135 : : /* Parses a pattern that opens with an identifier. This includes identifier
11136 : : * patterns, path patterns (and derivatives such as struct patterns, tuple
11137 : : * struct patterns, and macro invocations), and ranges. */
11138 : : template <typename ManagedTokenSource>
11139 : : std::unique_ptr<AST::Pattern>
11140 : 24646 : Parser<ManagedTokenSource>::parse_ident_leading_pattern ()
11141 : : {
11142 : : // ensure first token is actually identifier
11143 : 24646 : const_TokenPtr initial_tok = lexer.peek_token ();
11144 : 24646 : if (initial_tok->get_id () != IDENTIFIER)
11145 : : {
11146 : 0 : return nullptr;
11147 : : }
11148 : :
11149 : : // save initial identifier as it may be useful (but don't skip)
11150 : 24646 : std::string initial_ident = initial_tok->get_str ();
11151 : :
11152 : : // parse next tokens as a PathInExpression
11153 : 24646 : AST::PathInExpression path = parse_path_in_expression ();
11154 : :
11155 : : // branch on next token
11156 : 24646 : const_TokenPtr t = lexer.peek_token ();
11157 : 24646 : switch (t->get_id ())
11158 : : {
11159 : 0 : case EXCLAM:
11160 : 0 : return parse_macro_invocation_partial (std::move (path), AST::AttrVec ());
11161 : 878 : case LEFT_PAREN:
11162 : : {
11163 : : // tuple struct
11164 : 878 : lexer.skip_token ();
11165 : :
11166 : : // DEBUG
11167 : 878 : rust_debug ("parsing tuple struct pattern");
11168 : :
11169 : : // parse items
11170 : 878 : std::unique_ptr<AST::TupleStructItems> items
11171 : : = parse_tuple_struct_items ();
11172 : 878 : if (items == nullptr)
11173 : : {
11174 : 0 : Error error (lexer.peek_token ()->get_locus (),
11175 : : "failed to parse tuple struct items");
11176 : 0 : add_error (std::move (error));
11177 : :
11178 : 0 : return nullptr;
11179 : 0 : }
11180 : :
11181 : : // DEBUG
11182 : 878 : rust_debug ("successfully parsed tuple struct items");
11183 : :
11184 : 878 : if (!skip_token (RIGHT_PAREN))
11185 : : {
11186 : 0 : return nullptr;
11187 : : }
11188 : :
11189 : : // DEBUG
11190 : 878 : rust_debug ("successfully parsed tuple struct pattern");
11191 : :
11192 : 878 : return std::unique_ptr<AST::TupleStructPattern> (
11193 : 878 : new AST::TupleStructPattern (std::move (path), std::move (items)));
11194 : 878 : }
11195 : 93 : case LEFT_CURLY:
11196 : : {
11197 : : // struct
11198 : 93 : lexer.skip_token ();
11199 : :
11200 : : // parse elements (optional)
11201 : 93 : AST::StructPatternElements elems = parse_struct_pattern_elems ();
11202 : :
11203 : 93 : if (!skip_token (RIGHT_CURLY))
11204 : : {
11205 : 0 : return nullptr;
11206 : : }
11207 : :
11208 : : // DEBUG
11209 : 93 : rust_debug ("successfully parsed struct pattern");
11210 : :
11211 : 93 : return std::unique_ptr<AST::StructPattern> (
11212 : 186 : new AST::StructPattern (std::move (path), initial_tok->get_locus (),
11213 : 93 : std::move (elems)));
11214 : 93 : }
11215 : 8 : case DOT_DOT_EQ:
11216 : : case DOT_DOT:
11217 : : case ELLIPSIS:
11218 : : {
11219 : : // range
11220 : : AST::RangeKind kind
11221 : 8 : = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
11222 : :
11223 : 8 : lexer.skip_token ();
11224 : :
11225 : 8 : std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
11226 : 8 : new AST::RangePatternBoundPath (std::move (path)));
11227 : 8 : std::unique_ptr<AST::RangePatternBound> upper_bound
11228 : : = parse_range_pattern_bound ();
11229 : :
11230 : 8 : return std::unique_ptr<AST::RangePattern> (
11231 : 8 : new AST::RangePattern (std::move (lower_bound),
11232 : : std::move (upper_bound), kind,
11233 : 8 : t->get_locus ()));
11234 : 8 : }
11235 : 9 : case PATTERN_BIND:
11236 : : {
11237 : : // only allow on single-segment paths
11238 : 9 : if (path.is_single_segment ())
11239 : : {
11240 : : // identifier with pattern bind
11241 : 9 : lexer.skip_token ();
11242 : :
11243 : 9 : std::unique_ptr<AST::Pattern> bind_pattern = parse_pattern ();
11244 : 9 : if (bind_pattern == nullptr)
11245 : : {
11246 : 0 : Error error (
11247 : : t->get_locus (),
11248 : : "failed to parse pattern to bind to identifier pattern");
11249 : 0 : add_error (std::move (error));
11250 : :
11251 : 0 : return nullptr;
11252 : 0 : }
11253 : 9 : return std::unique_ptr<AST::IdentifierPattern> (
11254 : 27 : new AST::IdentifierPattern (std::move (initial_ident),
11255 : : initial_tok->get_locus (), false,
11256 : 9 : false, std::move (bind_pattern)));
11257 : 9 : }
11258 : 0 : Error error (
11259 : : t->get_locus (),
11260 : : "failed to parse pattern bind to a path, not an identifier");
11261 : 0 : add_error (std::move (error));
11262 : :
11263 : 0 : return nullptr;
11264 : 0 : }
11265 : 23658 : default:
11266 : : // assume identifier if single segment
11267 : 23658 : if (path.is_single_segment ())
11268 : : {
11269 : 22690 : return std::unique_ptr<AST::IdentifierPattern> (
11270 : 68070 : new AST::IdentifierPattern (std::move (initial_ident),
11271 : 22690 : initial_tok->get_locus ()));
11272 : : }
11273 : : // return path otherwise
11274 : 968 : return std::unique_ptr<AST::PathInExpression> (
11275 : 968 : new AST::PathInExpression (std::move (path)));
11276 : : }
11277 : 24646 : }
11278 : :
11279 : : // Parses tuple struct items if they exist. Does not parse parentheses.
11280 : : template <typename ManagedTokenSource>
11281 : : std::unique_ptr<AST::TupleStructItems>
11282 : 878 : Parser<ManagedTokenSource>::parse_tuple_struct_items ()
11283 : : {
11284 : 878 : std::vector<std::unique_ptr<AST::Pattern>> lower_patterns;
11285 : :
11286 : : // DEBUG
11287 : 878 : rust_debug ("started parsing tuple struct items");
11288 : :
11289 : : // check for '..' at front
11290 : 1756 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11291 : : {
11292 : : // only parse upper patterns
11293 : 0 : lexer.skip_token ();
11294 : :
11295 : : // DEBUG
11296 : 0 : rust_debug ("'..' at front in tuple struct items detected");
11297 : :
11298 : 0 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
11299 : :
11300 : 0 : const_TokenPtr t = lexer.peek_token ();
11301 : 0 : while (t->get_id () == COMMA)
11302 : : {
11303 : 0 : lexer.skip_token ();
11304 : :
11305 : : // break if right paren
11306 : 0 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
11307 : : break;
11308 : :
11309 : : // parse pattern, which is now required
11310 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11311 : 0 : if (pattern == nullptr)
11312 : : {
11313 : 0 : Error error (lexer.peek_token ()->get_locus (),
11314 : : "failed to parse pattern in tuple struct items");
11315 : 0 : add_error (std::move (error));
11316 : :
11317 : 0 : return nullptr;
11318 : 0 : }
11319 : 0 : upper_patterns.push_back (std::move (pattern));
11320 : :
11321 : 0 : t = lexer.peek_token ();
11322 : : }
11323 : :
11324 : : // DEBUG
11325 : 0 : rust_debug (
11326 : : "finished parsing tuple struct items ranged (upper/none only)");
11327 : :
11328 : 0 : return std::unique_ptr<AST::TupleStructItemsRange> (
11329 : 0 : new AST::TupleStructItemsRange (std::move (lower_patterns),
11330 : 0 : std::move (upper_patterns)));
11331 : 0 : }
11332 : :
11333 : : // has at least some lower patterns
11334 : 878 : const_TokenPtr t = lexer.peek_token ();
11335 : 1793 : while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
11336 : : {
11337 : : // DEBUG
11338 : 915 : rust_debug ("about to parse pattern in tuple struct items");
11339 : :
11340 : : // parse pattern, which is required
11341 : 915 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11342 : 915 : if (pattern == nullptr)
11343 : : {
11344 : 0 : Error error (t->get_locus (),
11345 : : "failed to parse pattern in tuple struct items");
11346 : 0 : add_error (std::move (error));
11347 : :
11348 : 0 : return nullptr;
11349 : 0 : }
11350 : 915 : lower_patterns.push_back (std::move (pattern));
11351 : :
11352 : : // DEBUG
11353 : 915 : rust_debug ("successfully parsed pattern in tuple struct items");
11354 : :
11355 : 1830 : if (lexer.peek_token ()->get_id () != COMMA)
11356 : : {
11357 : : // DEBUG
11358 : 856 : rust_debug ("broke out of parsing patterns in tuple struct "
11359 : : "items as no comma");
11360 : :
11361 : : break;
11362 : : }
11363 : 59 : lexer.skip_token ();
11364 : 59 : t = lexer.peek_token ();
11365 : : }
11366 : :
11367 : : // branch on next token
11368 : 878 : t = lexer.peek_token ();
11369 : 878 : switch (t->get_id ())
11370 : : {
11371 : 878 : case RIGHT_PAREN:
11372 : 878 : return std::unique_ptr<AST::TupleStructItemsNoRange> (
11373 : 878 : new AST::TupleStructItemsNoRange (std::move (lower_patterns)));
11374 : 0 : case DOT_DOT:
11375 : : {
11376 : : // has an upper range that must be parsed separately
11377 : 0 : lexer.skip_token ();
11378 : :
11379 : 0 : std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
11380 : :
11381 : 0 : t = lexer.peek_token ();
11382 : 0 : while (t->get_id () == COMMA)
11383 : : {
11384 : 0 : lexer.skip_token ();
11385 : :
11386 : : // break if next token is right paren
11387 : 0 : if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
11388 : : break;
11389 : :
11390 : : // parse pattern, which is required
11391 : 0 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11392 : 0 : if (pattern == nullptr)
11393 : : {
11394 : 0 : Error error (lexer.peek_token ()->get_locus (),
11395 : : "failed to parse pattern in tuple struct items");
11396 : 0 : add_error (std::move (error));
11397 : :
11398 : 0 : return nullptr;
11399 : 0 : }
11400 : 0 : upper_patterns.push_back (std::move (pattern));
11401 : :
11402 : 0 : t = lexer.peek_token ();
11403 : : }
11404 : :
11405 : 0 : return std::unique_ptr<AST::TupleStructItemsRange> (
11406 : 0 : new AST::TupleStructItemsRange (std::move (lower_patterns),
11407 : 0 : std::move (upper_patterns)));
11408 : 0 : }
11409 : 0 : default:
11410 : : // error
11411 : 0 : add_error (Error (t->get_locus (),
11412 : : "unexpected token %qs in tuple struct items",
11413 : : t->get_token_description ()));
11414 : :
11415 : 0 : return nullptr;
11416 : : }
11417 : 878 : }
11418 : :
11419 : : // Parses struct pattern elements if they exist.
11420 : : template <typename ManagedTokenSource>
11421 : : AST::StructPatternElements
11422 : 93 : Parser<ManagedTokenSource>::parse_struct_pattern_elems ()
11423 : : {
11424 : 93 : std::vector<std::unique_ptr<AST::StructPatternField>> fields;
11425 : :
11426 : 93 : AST::AttrVec etc_attrs;
11427 : 93 : bool has_etc = false;
11428 : :
11429 : : // try parsing struct pattern fields
11430 : 93 : const_TokenPtr t = lexer.peek_token ();
11431 : 306 : while (t->get_id () != RIGHT_CURLY)
11432 : : {
11433 : 146 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11434 : :
11435 : : // parse etc (must be last in struct pattern, so breaks)
11436 : 292 : if (lexer.peek_token ()->get_id () == DOT_DOT)
11437 : : {
11438 : 0 : lexer.skip_token ();
11439 : 0 : etc_attrs = std::move (outer_attrs);
11440 : 0 : has_etc = true;
11441 : 0 : break;
11442 : : }
11443 : :
11444 : 146 : std::unique_ptr<AST::StructPatternField> field
11445 : 146 : = parse_struct_pattern_field_partial (std::move (outer_attrs));
11446 : 146 : if (field == nullptr)
11447 : : {
11448 : 0 : Error error (lexer.peek_token ()->get_locus (),
11449 : : "failed to parse struct pattern field");
11450 : 0 : add_error (std::move (error));
11451 : :
11452 : : // skip after somewhere?
11453 : 0 : return AST::StructPatternElements::create_empty ();
11454 : 0 : }
11455 : 146 : fields.push_back (std::move (field));
11456 : :
11457 : 292 : if (lexer.peek_token ()->get_id () != COMMA)
11458 : : break;
11459 : :
11460 : : // skip comma
11461 : 67 : lexer.skip_token ();
11462 : 67 : t = lexer.peek_token ();
11463 : : }
11464 : :
11465 : 93 : if (has_etc)
11466 : 0 : return AST::StructPatternElements (std::move (fields),
11467 : 0 : std::move (etc_attrs));
11468 : : else
11469 : 93 : return AST::StructPatternElements (std::move (fields));
11470 : 93 : }
11471 : :
11472 : : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
11473 : : * identifier). */
11474 : : template <typename ManagedTokenSource>
11475 : : std::unique_ptr<AST::StructPatternField>
11476 : 0 : Parser<ManagedTokenSource>::parse_struct_pattern_field ()
11477 : : {
11478 : : // parse outer attributes (if they exist)
11479 : 0 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11480 : :
11481 : 0 : return parse_struct_pattern_field_partial (std::move (outer_attrs));
11482 : 0 : }
11483 : :
11484 : : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
11485 : : * identifier), with outer attributes passed in. */
11486 : : template <typename ManagedTokenSource>
11487 : : std::unique_ptr<AST::StructPatternField>
11488 : 146 : Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
11489 : : AST::AttrVec outer_attrs)
11490 : : {
11491 : : // branch based on next token
11492 : 146 : const_TokenPtr t = lexer.peek_token ();
11493 : 146 : switch (t->get_id ())
11494 : : {
11495 : 3 : case INT_LITERAL:
11496 : : {
11497 : : // tuple index
11498 : 6 : std::string index_str = t->get_str ();
11499 : 3 : int index = atoi (index_str.c_str ());
11500 : :
11501 : 3 : lexer.skip_token ();
11502 : :
11503 : 3 : if (!skip_token (COLON))
11504 : : {
11505 : 0 : return nullptr;
11506 : : }
11507 : :
11508 : : // parse required pattern
11509 : 3 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11510 : 3 : if (pattern == nullptr)
11511 : : {
11512 : 0 : Error error (
11513 : : t->get_locus (),
11514 : : "failed to parse pattern in tuple index struct pattern field");
11515 : 0 : add_error (std::move (error));
11516 : :
11517 : 0 : return nullptr;
11518 : 0 : }
11519 : :
11520 : 3 : return std::unique_ptr<AST::StructPatternFieldTuplePat> (
11521 : 3 : new AST::StructPatternFieldTuplePat (index, std::move (pattern),
11522 : : std::move (outer_attrs),
11523 : 3 : t->get_locus ()));
11524 : 6 : }
11525 : 143 : case IDENTIFIER:
11526 : : // identifier-pattern OR only identifier
11527 : : // branch on next token
11528 : 286 : switch (lexer.peek_token (1)->get_id ())
11529 : : {
11530 : 57 : case COLON:
11531 : : {
11532 : : // identifier-pattern
11533 : 57 : Identifier ident{t};
11534 : 57 : lexer.skip_token ();
11535 : :
11536 : 57 : skip_token (COLON);
11537 : :
11538 : : // parse required pattern
11539 : 57 : std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
11540 : 57 : if (pattern == nullptr)
11541 : : {
11542 : 0 : Error error (t->get_locus (),
11543 : : "failed to parse pattern in struct pattern field");
11544 : 0 : add_error (std::move (error));
11545 : :
11546 : 0 : return nullptr;
11547 : 0 : }
11548 : :
11549 : 57 : return std::unique_ptr<AST::StructPatternFieldIdentPat> (
11550 : 114 : new AST::StructPatternFieldIdentPat (std::move (ident),
11551 : : std::move (pattern),
11552 : : std::move (outer_attrs),
11553 : 57 : t->get_locus ()));
11554 : 57 : }
11555 : 86 : case COMMA:
11556 : : case RIGHT_CURLY:
11557 : : {
11558 : : // identifier only
11559 : 86 : Identifier ident = {t};
11560 : 86 : lexer.skip_token ();
11561 : :
11562 : 86 : return std::unique_ptr<AST::StructPatternFieldIdent> (
11563 : 172 : new AST::StructPatternFieldIdent (std::move (ident), false, false,
11564 : : std::move (outer_attrs),
11565 : 86 : t->get_locus ()));
11566 : 86 : }
11567 : 0 : default:
11568 : : // error
11569 : 0 : add_error (Error (t->get_locus (),
11570 : : "unrecognised token %qs in struct pattern field",
11571 : : t->get_token_description ()));
11572 : :
11573 : 0 : return nullptr;
11574 : : }
11575 : 0 : case REF:
11576 : : case MUT:
11577 : : {
11578 : : // only identifier
11579 : 0 : bool has_ref = false;
11580 : 0 : if (t->get_id () == REF)
11581 : : {
11582 : 0 : has_ref = true;
11583 : 0 : lexer.skip_token ();
11584 : : }
11585 : :
11586 : 0 : bool has_mut = false;
11587 : 0 : if (lexer.peek_token ()->get_id () == MUT)
11588 : : {
11589 : 0 : has_mut = true;
11590 : 0 : lexer.skip_token ();
11591 : : }
11592 : :
11593 : 0 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
11594 : 0 : if (ident_tok == nullptr)
11595 : : {
11596 : 0 : return nullptr;
11597 : : }
11598 : 0 : Identifier ident{ident_tok};
11599 : :
11600 : 0 : return std::unique_ptr<AST::StructPatternFieldIdent> (
11601 : 0 : new AST::StructPatternFieldIdent (std::move (ident), has_ref, has_mut,
11602 : : std::move (outer_attrs),
11603 : 0 : t->get_locus ()));
11604 : 0 : }
11605 : 0 : default:
11606 : : // not necessarily an error
11607 : 0 : return nullptr;
11608 : : }
11609 : 146 : }
11610 : :
11611 : : /* Parses a statement or expression (depending on whether a trailing semicolon
11612 : : * exists). Useful for block expressions where it cannot be determined through
11613 : : * lookahead whether it is a statement or expression to be parsed. */
11614 : : template <typename ManagedTokenSource>
11615 : : ExprOrStmt
11616 : 37637 : Parser<ManagedTokenSource>::parse_stmt_or_expr ()
11617 : : {
11618 : : // quick exit for empty statement
11619 : 37637 : const_TokenPtr t = lexer.peek_token ();
11620 : 37637 : if (t->get_id () == SEMICOLON)
11621 : : {
11622 : 17 : lexer.skip_token ();
11623 : 17 : std::unique_ptr<AST::EmptyStmt> stmt (
11624 : 17 : new AST::EmptyStmt (t->get_locus ()));
11625 : 17 : return ExprOrStmt (std::move (stmt));
11626 : 17 : }
11627 : :
11628 : : // parse outer attributes
11629 : 37620 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11630 : 37620 : ParseRestrictions restrictions;
11631 : 37620 : restrictions.expr_can_be_stmt = true;
11632 : 37620 : std::unique_ptr<AST::Expr> expr;
11633 : :
11634 : : // parsing this will be annoying because of the many different possibilities
11635 : : /* best may be just to copy paste in parse_item switch, and failing that try
11636 : : * to parse outer attributes, and then pass them in to either a let
11637 : : * statement or (fallback) expression statement. */
11638 : : // FIXME: think of a way to do this without such a large switch?
11639 : :
11640 : : /* FIXME: for expressions at least, the only way that they can really be
11641 : : * parsed properly in this way is if they don't support operators on them.
11642 : : * They must be pratt-parsed otherwise. As such due to composability, only
11643 : : * explicit statements will have special cases here. This should roughly
11644 : : * correspond to "expr-with-block", but this warning is here in case it
11645 : : * isn't the case. */
11646 : 37620 : t = lexer.peek_token ();
11647 : 37620 : switch (t->get_id ())
11648 : : {
11649 : 12448 : case LET:
11650 : : {
11651 : : // let statement
11652 : 12448 : std::unique_ptr<AST::LetStmt> stmt (
11653 : 12448 : parse_let_stmt (std::move (outer_attrs)));
11654 : 12448 : return ExprOrStmt (std::move (stmt));
11655 : 12448 : }
11656 : 319 : case PUB:
11657 : : case MOD:
11658 : : case EXTERN_KW:
11659 : : case USE:
11660 : : case FN_KW:
11661 : : case TYPE:
11662 : : case STRUCT_KW:
11663 : : case ENUM_KW:
11664 : : case CONST:
11665 : : case STATIC_KW:
11666 : : case AUTO:
11667 : : case TRAIT:
11668 : : case IMPL:
11669 : : {
11670 : 319 : std::unique_ptr<AST::VisItem> item (
11671 : 319 : parse_vis_item (std::move (outer_attrs)));
11672 : 319 : return ExprOrStmt (std::move (item));
11673 : 319 : }
11674 : : /* TODO: implement union keyword but not really because of
11675 : : * context-dependence crappy hack way to parse a union written below to
11676 : : * separate it from the good code. */
11677 : : // case UNION:
11678 : 2989 : case UNSAFE:
11679 : : { // maybe - unsafe traits are a thing
11680 : : /* if any of these (should be all possible VisItem prefixes), parse a
11681 : : * VisItem - can't parse item because would require reparsing outer
11682 : : * attributes */
11683 : 2989 : const_TokenPtr t2 = lexer.peek_token (1);
11684 : 2989 : switch (t2->get_id ())
11685 : : {
11686 : 2975 : case LEFT_CURLY:
11687 : : {
11688 : : // unsafe block: parse as expression
11689 : 2975 : expr = parse_expr (std::move (outer_attrs), restrictions);
11690 : : break;
11691 : : }
11692 : 0 : case AUTO:
11693 : : case TRAIT:
11694 : : {
11695 : : // unsafe trait
11696 : 0 : std::unique_ptr<AST::VisItem> item (
11697 : 0 : parse_vis_item (std::move (outer_attrs)));
11698 : 0 : return ExprOrStmt (std::move (item));
11699 : 0 : }
11700 : 14 : case EXTERN_KW:
11701 : : case FN_KW:
11702 : : {
11703 : : // unsafe function
11704 : 14 : std::unique_ptr<AST::VisItem> item (
11705 : 14 : parse_vis_item (std::move (outer_attrs)));
11706 : 14 : return ExprOrStmt (std::move (item));
11707 : 14 : }
11708 : 0 : case IMPL:
11709 : : {
11710 : : // unsafe trait impl
11711 : 0 : std::unique_ptr<AST::VisItem> item (
11712 : 0 : parse_vis_item (std::move (outer_attrs)));
11713 : 0 : return ExprOrStmt (std::move (item));
11714 : 0 : }
11715 : 0 : default:
11716 : 0 : add_error (Error (t2->get_locus (),
11717 : : "unrecognised token %qs after parsing unsafe - "
11718 : : "expected beginning of expression or statement",
11719 : : t->get_token_description ()));
11720 : :
11721 : : // skip somewhere?
11722 : : return ExprOrStmt::create_error ();
11723 : : }
11724 : : break;
11725 : 2989 : }
11726 : : /* FIXME: this is either a macro invocation or macro invocation semi.
11727 : : * start parsing to determine which one it is. */
11728 : : // FIXME: old code there
11729 : :
11730 : : // crappy hack to do union "keyword"
11731 : 11891 : case IDENTIFIER:
11732 : 21186 : if (t->get_str () == Values::WeakKeywords::UNION
11733 : 11927 : && lexer.peek_token (1)->get_id () == IDENTIFIER)
11734 : : {
11735 : 1 : std::unique_ptr<AST::VisItem> item (
11736 : 1 : parse_vis_item (std::move (outer_attrs)));
11737 : 1 : return ExprOrStmt (std::move (item));
11738 : : // or should this go straight to parsing union?
11739 : 1 : }
11740 : 21184 : else if (t->get_str () == Values::WeakKeywords::MACRO_RULES
11741 : 11895 : && lexer.peek_token (1)->get_id () == EXCLAM)
11742 : : {
11743 : : // macro_rules! macro item
11744 : 5 : std::unique_ptr<AST::Item> item (
11745 : 5 : parse_macro_rules_def (std::move (outer_attrs)));
11746 : 5 : return ExprOrStmt (std::move (item));
11747 : 5 : }
11748 : : gcc_fallthrough ();
11749 : : case SUPER:
11750 : : case SELF:
11751 : : case SELF_ALIAS:
11752 : : case CRATE:
11753 : : case SCOPE_RESOLUTION:
11754 : : case DOLLAR_SIGN:
11755 : : {
11756 : 14340 : AST::PathInExpression path = parse_path_in_expression ();
11757 : 14340 : std::unique_ptr<AST::Expr> null_denotation;
11758 : :
11759 : 28680 : if (lexer.peek_token ()->get_id () == EXCLAM)
11760 : : {
11761 : 572 : std::unique_ptr<AST::MacroInvocation> invoc
11762 : 1144 : = parse_macro_invocation_partial (std::move (path),
11763 : : std::move (outer_attrs));
11764 : :
11765 : 572 : if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
11766 : : {
11767 : 394 : invoc->add_semicolon ();
11768 : : // Macro invocation with semicolon.
11769 : : return ExprOrStmt (
11770 : 394 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
11771 : : }
11772 : :
11773 : 356 : TokenId after_macro = lexer.peek_token ()->get_id ();
11774 : :
11775 : 178 : if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
11776 : : == AST::CURLY
11777 : 178 : && after_macro != DOT && after_macro != QUESTION_MARK)
11778 : : {
11779 : 5 : rust_debug ("braced macro statement");
11780 : : return ExprOrStmt (
11781 : 5 : std::unique_ptr<AST::Stmt> (std::move (invoc)));
11782 : : }
11783 : :
11784 : 173 : null_denotation = std::move (invoc);
11785 : 572 : }
11786 : : else
11787 : : {
11788 : : null_denotation
11789 : 13768 : = null_denotation_path (std::move (path), {}, restrictions);
11790 : : }
11791 : :
11792 : 13941 : expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
11793 : : std::move (outer_attrs), restrictions);
11794 : : break;
11795 : 14340 : }
11796 : 7518 : default:
11797 : : /* expression statement or expression itself - parse
11798 : : * expression then make it statement if semi afterwards */
11799 : 7518 : expr = parse_expr (std::move (outer_attrs), restrictions);
11800 : 7518 : break;
11801 : : }
11802 : :
11803 : 24434 : const_TokenPtr after_expr = lexer.peek_token ();
11804 : 24434 : if (after_expr->get_id () == SEMICOLON)
11805 : : {
11806 : : // must be expression statement
11807 : 7457 : lexer.skip_token ();
11808 : :
11809 : 7457 : if (expr)
11810 : : {
11811 : 7456 : std::unique_ptr<AST::ExprStmt> stmt (
11812 : 7456 : new AST::ExprStmt (std::move (expr), t->get_locus (), true));
11813 : 7456 : return ExprOrStmt (std::move (stmt));
11814 : 7456 : }
11815 : : else
11816 : : {
11817 : : return ExprOrStmt::create_error ();
11818 : : }
11819 : : }
11820 : :
11821 : 16972 : if (expr && !expr->is_expr_without_block ()
11822 : 22584 : && after_expr->get_id () != RIGHT_CURLY)
11823 : : {
11824 : : // block expression statement.
11825 : 1440 : std::unique_ptr<AST::ExprStmt> stmt (
11826 : 1440 : new AST::ExprStmt (std::move (expr), t->get_locus (), false));
11827 : 1440 : return ExprOrStmt (std::move (stmt));
11828 : 1440 : }
11829 : :
11830 : : // return expression
11831 : 15537 : return ExprOrStmt (std::move (expr));
11832 : 37620 : }
11833 : :
11834 : : // Parses a struct expression field.
11835 : : template <typename ManagedTokenSource>
11836 : : std::unique_ptr<AST::StructExprField>
11837 : 2236 : Parser<ManagedTokenSource>::parse_struct_expr_field ()
11838 : : {
11839 : 2236 : AST::AttrVec outer_attrs = parse_outer_attributes ();
11840 : 2236 : const_TokenPtr t = lexer.peek_token ();
11841 : 2236 : switch (t->get_id ())
11842 : : {
11843 : 2192 : case IDENTIFIER:
11844 : 4384 : if (lexer.peek_token (1)->get_id () == COLON)
11845 : : {
11846 : : // struct expr field with identifier and expr
11847 : 1976 : Identifier ident = {t};
11848 : 1976 : lexer.skip_token (1);
11849 : :
11850 : : // parse expression (required)
11851 : 1976 : std::unique_ptr<AST::Expr> expr = parse_expr ();
11852 : 1976 : if (expr == nullptr)
11853 : : {
11854 : 0 : Error error (t->get_locus (),
11855 : : "failed to parse struct expression field with "
11856 : : "identifier and expression");
11857 : 0 : add_error (std::move (error));
11858 : :
11859 : 0 : return nullptr;
11860 : 0 : }
11861 : :
11862 : 1976 : return std::unique_ptr<AST::StructExprFieldIdentifierValue> (
11863 : 3952 : new AST::StructExprFieldIdentifierValue (std::move (ident),
11864 : : std::move (expr),
11865 : : std::move (outer_attrs),
11866 : 1976 : t->get_locus ()));
11867 : 1976 : }
11868 : : else
11869 : : {
11870 : : // struct expr field with identifier only
11871 : 216 : Identifier ident{t};
11872 : 216 : lexer.skip_token ();
11873 : :
11874 : 216 : return std::unique_ptr<AST::StructExprFieldIdentifier> (
11875 : 432 : new AST::StructExprFieldIdentifier (std::move (ident),
11876 : : std::move (outer_attrs),
11877 : 216 : t->get_locus ()));
11878 : 216 : }
11879 : 44 : case INT_LITERAL:
11880 : : {
11881 : : // parse tuple index field
11882 : 44 : int index = atoi (t->get_str ().c_str ());
11883 : 44 : lexer.skip_token ();
11884 : :
11885 : 44 : if (!skip_token (COLON))
11886 : : {
11887 : : // skip somewhere?
11888 : 0 : return nullptr;
11889 : : }
11890 : :
11891 : : // parse field expression (required)
11892 : 44 : std::unique_ptr<AST::Expr> expr = parse_expr ();
11893 : 44 : if (expr == nullptr)
11894 : : {
11895 : 0 : Error error (t->get_locus (),
11896 : : "failed to parse expr in struct (or enum) expr "
11897 : : "field with tuple index");
11898 : 0 : add_error (std::move (error));
11899 : :
11900 : 0 : return nullptr;
11901 : 0 : }
11902 : :
11903 : 44 : return std::unique_ptr<AST::StructExprFieldIndexValue> (
11904 : 44 : new AST::StructExprFieldIndexValue (index, std::move (expr),
11905 : : std::move (outer_attrs),
11906 : 44 : t->get_locus ()));
11907 : 44 : }
11908 : 0 : case DOT_DOT:
11909 : : /* this is a struct base and can't be parsed here, so just return
11910 : : * nothing without erroring */
11911 : :
11912 : 0 : return nullptr;
11913 : 0 : default:
11914 : 0 : add_error (
11915 : 0 : Error (t->get_locus (),
11916 : : "unrecognised token %qs as first token of struct expr field - "
11917 : : "expected identifier or integer literal",
11918 : : t->get_token_description ()));
11919 : :
11920 : 0 : return nullptr;
11921 : : }
11922 : 2236 : }
11923 : :
11924 : : // "Unexpected token" panic mode - flags gcc error at unexpected token
11925 : : // TODO: seems to be unused, remove?
11926 : : template <typename ManagedTokenSource>
11927 : : void
11928 : 0 : Parser<ManagedTokenSource>::unexpected_token (const_TokenPtr t)
11929 : : {
11930 : 0 : Error error (t->get_locus (), "unexpected token %qs",
11931 : : t->get_token_description ());
11932 : 0 : add_error (std::move (error));
11933 : 0 : }
11934 : :
11935 : : /* Crappy "error recovery" performed after error by skipping tokens until a
11936 : : * semi-colon is found */
11937 : : template <typename ManagedTokenSource>
11938 : : void
11939 : 24 : Parser<ManagedTokenSource>::skip_after_semicolon ()
11940 : : {
11941 : 24 : const_TokenPtr t = lexer.peek_token ();
11942 : :
11943 : 31 : while (t->get_id () != END_OF_FILE && t->get_id () != SEMICOLON)
11944 : : {
11945 : 7 : lexer.skip_token ();
11946 : 7 : t = lexer.peek_token ();
11947 : : }
11948 : :
11949 : 24 : if (t->get_id () == SEMICOLON)
11950 : 3 : lexer.skip_token ();
11951 : 24 : }
11952 : :
11953 : : /* Skips the current token */
11954 : : template <typename ManagedTokenSource>
11955 : : void
11956 : 5499 : Parser<ManagedTokenSource>::skip_token ()
11957 : : {
11958 : 1519 : lexer.skip_token ();
11959 : 1358 : }
11960 : :
11961 : : /* Checks if current token has inputted id - skips it and returns true if so,
11962 : : * diagnoses an error and returns false otherwise. */
11963 : : template <typename ManagedTokenSource>
11964 : : bool
11965 : 268437 : Parser<ManagedTokenSource>::skip_token (TokenId token_id)
11966 : : {
11967 : 268437 : return expect_token (token_id) != const_TokenPtr ();
11968 : : }
11969 : :
11970 : : /* Checks if current token is similar to inputted token - skips it and returns
11971 : : * true if so, diagnoses an error and returns false otherwise. */
11972 : : template <typename ManagedTokenSource>
11973 : : bool
11974 : 2669 : Parser<ManagedTokenSource>::skip_token (const_TokenPtr token)
11975 : : {
11976 : 7951 : return expect_token (token) != const_TokenPtr ();
11977 : : }
11978 : :
11979 : : /* Checks if current token has inputted id - skips it and returns true if so,
11980 : : * returns false otherwise without diagnosing an error */
11981 : : template <typename ManagedTokenSource>
11982 : : bool
11983 : 45753 : Parser<ManagedTokenSource>::maybe_skip_token (TokenId token_id)
11984 : : {
11985 : 91506 : if (lexer.peek_token ()->get_id () != token_id)
11986 : : return false;
11987 : : else
11988 : 1069 : return skip_token (token_id);
11989 : : }
11990 : :
11991 : : /* Checks the current token - if id is same as expected, skips and returns it,
11992 : : * otherwise diagnoses error and returns null. */
11993 : : template <typename ManagedTokenSource>
11994 : : const_TokenPtr
11995 : 305761 : Parser<ManagedTokenSource>::expect_token (TokenId token_id)
11996 : : {
11997 : 305761 : const_TokenPtr t = lexer.peek_token ();
11998 : 305761 : if (t->get_id () == token_id)
11999 : : {
12000 : 304084 : lexer.skip_token ();
12001 : 304084 : return t;
12002 : : }
12003 : : else
12004 : : {
12005 : 1677 : Error error (t->get_locus (), "expecting %qs but %qs found",
12006 : : get_token_description (token_id),
12007 : : t->get_token_description ());
12008 : 1677 : add_error (std::move (error));
12009 : :
12010 : 1677 : return const_TokenPtr ();
12011 : 1677 : }
12012 : 305761 : }
12013 : :
12014 : : /* Checks the current token - if same as expected, skips and returns it,
12015 : : * otherwise diagnoses error and returns null. */
12016 : : template <typename ManagedTokenSource>
12017 : : const_TokenPtr
12018 : 2669 : Parser<ManagedTokenSource>::expect_token (const_TokenPtr token_expect)
12019 : : {
12020 : 2669 : const_TokenPtr t = lexer.peek_token ();
12021 : 2669 : if (t->get_id () == token_expect->get_id ()
12022 : 2778 : && (!t->should_have_str () || t->get_str () == token_expect->get_str ()))
12023 : : {
12024 : 2613 : lexer.skip_token ();
12025 : 2613 : return t;
12026 : : }
12027 : : else
12028 : : {
12029 : 56 : Error error (t->get_locus (), "expecting %qs but %qs found",
12030 : : token_expect->get_token_description (),
12031 : : t->get_token_description ());
12032 : 56 : add_error (std::move (error));
12033 : :
12034 : 56 : return const_TokenPtr ();
12035 : 56 : }
12036 : 2669 : }
12037 : :
12038 : : // Skips all tokens until EOF or }. Don't use.
12039 : : template <typename ManagedTokenSource>
12040 : : void
12041 : 0 : Parser<ManagedTokenSource>::skip_after_end ()
12042 : : {
12043 : 0 : const_TokenPtr t = lexer.peek_token ();
12044 : :
12045 : 0 : while (t->get_id () != END_OF_FILE && t->get_id () != RIGHT_CURLY)
12046 : : {
12047 : 0 : lexer.skip_token ();
12048 : 0 : t = lexer.peek_token ();
12049 : : }
12050 : :
12051 : 0 : if (t->get_id () == RIGHT_CURLY)
12052 : : {
12053 : 0 : lexer.skip_token ();
12054 : : }
12055 : 0 : }
12056 : :
12057 : : /* A slightly more aware error-handler that skips all tokens until it reaches
12058 : : * the end of the block scope (i.e. when left curly brackets = right curly
12059 : : * brackets). Note: assumes currently in the middle of a block. Use
12060 : : * skip_after_next_block to skip based on the assumption that the block
12061 : : * has not been entered yet. */
12062 : : template <typename ManagedTokenSource>
12063 : : void
12064 : 1 : Parser<ManagedTokenSource>::skip_after_end_block ()
12065 : : {
12066 : 1 : const_TokenPtr t = lexer.peek_token ();
12067 : 1 : int curly_count = 1;
12068 : :
12069 : 2 : while (curly_count > 0 && t->get_id () != END_OF_FILE)
12070 : : {
12071 : 1 : switch (t->get_id ())
12072 : : {
12073 : 0 : case LEFT_CURLY:
12074 : 0 : curly_count++;
12075 : 0 : break;
12076 : 1 : case RIGHT_CURLY:
12077 : 1 : curly_count--;
12078 : 1 : break;
12079 : : default:
12080 : : break;
12081 : : }
12082 : 1 : lexer.skip_token ();
12083 : 1 : t = lexer.peek_token ();
12084 : : }
12085 : 1 : }
12086 : :
12087 : : /* Skips tokens until the end of the next block. i.e. assumes that the block
12088 : : * has not been entered yet. */
12089 : : template <typename ManagedTokenSource>
12090 : : void
12091 : 1 : Parser<ManagedTokenSource>::skip_after_next_block ()
12092 : : {
12093 : 1 : const_TokenPtr t = lexer.peek_token ();
12094 : :
12095 : : // initial loop - skip until EOF if no left curlies encountered
12096 : 1 : while (t->get_id () != END_OF_FILE && t->get_id () != LEFT_CURLY)
12097 : : {
12098 : 0 : lexer.skip_token ();
12099 : :
12100 : 0 : t = lexer.peek_token ();
12101 : : }
12102 : :
12103 : : // if next token is left, skip it and then skip after the block ends
12104 : 1 : if (t->get_id () == LEFT_CURLY)
12105 : : {
12106 : 1 : lexer.skip_token ();
12107 : :
12108 : 1 : skip_after_end_block ();
12109 : : }
12110 : : // otherwise, do nothing as EOF
12111 : 1 : }
12112 : :
12113 : : /* Skips all tokens until ] (the end of an attribute) - does not skip the ]
12114 : : * (as designed for attribute body use) */
12115 : : template <typename ManagedTokenSource>
12116 : : void
12117 : 0 : Parser<ManagedTokenSource>::skip_after_end_attribute ()
12118 : : {
12119 : 0 : const_TokenPtr t = lexer.peek_token ();
12120 : :
12121 : 0 : while (t->get_id () != RIGHT_SQUARE && t->get_id () != END_OF_FILE)
12122 : : {
12123 : 0 : lexer.skip_token ();
12124 : 0 : t = lexer.peek_token ();
12125 : : }
12126 : :
12127 : : // Don't skip the RIGHT_SQUARE token
12128 : 0 : }
12129 : :
12130 : : /* Pratt parser impl of parse_expr. FIXME: this is only provisional and
12131 : : * probably will be changed. */
12132 : : template <typename ManagedTokenSource>
12133 : : std::unique_ptr<AST::Expr>
12134 : 67302 : Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
12135 : : AST::AttrVec outer_attrs,
12136 : : ParseRestrictions restrictions)
12137 : : {
12138 : 67302 : const_TokenPtr current_token = lexer.peek_token ();
12139 : : // Special hack because we are allowed to return nullptr, in that case we
12140 : : // don't want to skip the token, since we don't actually parse it. But if
12141 : : // null isn't allowed it indicates an error, and we want to skip past that.
12142 : : // So return early if it is one of the tokens that ends an expression
12143 : : // (or at least cannot start a new expression).
12144 : 67302 : if (restrictions.expr_can_be_null)
12145 : : {
12146 : 1299 : TokenId id = current_token->get_id ();
12147 : 1299 : if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY
12148 : : || id == RIGHT_SQUARE || id == COMMA || id == LEFT_CURLY)
12149 : 90 : return nullptr;
12150 : : }
12151 : :
12152 : 67212 : if (current_token->get_id () == LEFT_SHIFT)
12153 : : {
12154 : 2 : lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
12155 : 2 : current_token = lexer.peek_token ();
12156 : : }
12157 : :
12158 : 67212 : lexer.skip_token ();
12159 : :
12160 : 67212 : ParseRestrictions null_denotation_restrictions = restrictions;
12161 : 67212 : null_denotation_restrictions.expr_can_be_stmt = false;
12162 : :
12163 : : // parse null denotation (unary part of expression)
12164 : 67212 : std::unique_ptr<AST::Expr> expr
12165 : 201636 : = null_denotation (current_token, {}, null_denotation_restrictions);
12166 : :
12167 : 67212 : return left_denotations (std::move (expr), right_binding_power,
12168 : 67212 : std::move (outer_attrs), restrictions);
12169 : 67212 : }
12170 : :
12171 : : template <typename ManagedTokenSource>
12172 : : std::unique_ptr<AST::Expr>
12173 : 81310 : Parser<ManagedTokenSource>::left_denotations (std::unique_ptr<AST::Expr> expr,
12174 : : int right_binding_power,
12175 : : AST::AttrVec outer_attrs,
12176 : : ParseRestrictions restrictions)
12177 : : {
12178 : 81310 : if (expr == nullptr)
12179 : : {
12180 : : // DEBUG
12181 : 31 : rust_debug ("null denotation is null; returning null for parse_expr");
12182 : 31 : return nullptr;
12183 : : }
12184 : :
12185 : 81279 : const_TokenPtr current_token = lexer.peek_token ();
12186 : :
12187 : 26448 : if (restrictions.expr_can_be_stmt && !expr->is_expr_without_block ()
12188 : 6425 : && current_token->get_id () != DOT
12189 : 87699 : && current_token->get_id () != QUESTION_MARK)
12190 : : {
12191 : 6420 : rust_debug ("statement expression with block");
12192 : 6420 : expr->set_outer_attrs (std::move (outer_attrs));
12193 : 6420 : return expr;
12194 : : }
12195 : :
12196 : 99565 : restrictions.expr_can_be_stmt = false;
12197 : :
12198 : : // stop parsing if find lower priority token - parse higher priority first
12199 : 298695 : while (right_binding_power < left_binding_power (current_token))
12200 : : {
12201 : 24708 : lexer.skip_token ();
12202 : :
12203 : : // FIXME attributes should generally be applied to the null denotation.
12204 : 74124 : expr = left_denotation (current_token, std::move (expr),
12205 : : std::move (outer_attrs), restrictions);
12206 : :
12207 : 24708 : if (expr == nullptr)
12208 : : {
12209 : : // DEBUG
12210 : 2 : rust_debug ("left denotation is null; returning null for parse_expr");
12211 : :
12212 : 2 : return nullptr;
12213 : : }
12214 : :
12215 : 24706 : current_token = lexer.peek_token ();
12216 : : }
12217 : :
12218 : 74857 : return expr;
12219 : 81279 : }
12220 : :
12221 : : // Parse expression with lowest left binding power.
12222 : : template <typename ManagedTokenSource>
12223 : : std::unique_ptr<AST::Expr>
12224 : 50857 : Parser<ManagedTokenSource>::parse_expr (AST::AttrVec outer_attrs,
12225 : : ParseRestrictions restrictions)
12226 : : {
12227 : 50857 : return parse_expr (LBP_LOWEST, std::move (outer_attrs), restrictions);
12228 : : }
12229 : :
12230 : : /* Determines action to take when finding token at beginning of expression. */
12231 : : template <typename ManagedTokenSource>
12232 : : std::unique_ptr<AST::Expr>
12233 : 67212 : Parser<ManagedTokenSource>::null_denotation (const_TokenPtr tok,
12234 : : AST::AttrVec outer_attrs,
12235 : : ParseRestrictions restrictions)
12236 : : {
12237 : : /* note: tok is previous character in input stream, not current one, as
12238 : : * parse_expr skips it before passing it in */
12239 : :
12240 : : /* as a Pratt parser (which works by decomposing expressions into a null
12241 : : * denotation and then a left denotation), null denotations handle primaries
12242 : : * and unary operands (but only prefix unary operands) */
12243 : :
12244 : 67212 : switch (tok->get_id ())
12245 : : {
12246 : 32788 : case IDENTIFIER:
12247 : : case SELF:
12248 : : case SELF_ALIAS:
12249 : : case DOLLAR_SIGN:
12250 : : case CRATE:
12251 : : case SUPER:
12252 : : {
12253 : : // DEBUG
12254 : 32788 : rust_debug ("beginning null denotation identifier handling");
12255 : :
12256 : : /* best option: parse as path, then extract identifier, macro,
12257 : : * struct/enum, or just path info from it */
12258 : 65576 : AST::PathInExpression path = parse_path_in_expression_pratt (tok);
12259 : :
12260 : 65576 : return null_denotation_path (std::move (path), std::move (outer_attrs),
12261 : 32788 : restrictions);
12262 : 32788 : }
12263 : 0 : case SCOPE_RESOLUTION:
12264 : : {
12265 : : // TODO: fix: this is for global paths, i.e. std::string::whatever
12266 : 0 : Error error (tok->get_locus (),
12267 : : "found null denotation scope resolution operator, and "
12268 : : "have not written handling for it");
12269 : 0 : add_error (std::move (error));
12270 : :
12271 : 0 : return nullptr;
12272 : 0 : }
12273 : 34424 : default:
12274 : 68848 : return null_denotation_not_path (std::move (tok), std::move (outer_attrs),
12275 : 34424 : restrictions);
12276 : : }
12277 : : }
12278 : :
12279 : : // Handling of expresions that start with a path for `null_denotation`.
12280 : : template <typename ManagedTokenSource>
12281 : : std::unique_ptr<AST::Expr>
12282 : 46713 : Parser<ManagedTokenSource>::null_denotation_path (
12283 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
12284 : : ParseRestrictions restrictions)
12285 : : {
12286 : 46713 : rust_debug ("parsing null denotation after path");
12287 : :
12288 : : // HACK: always make "self" by itself a path (regardless of next
12289 : : // tokens)
12290 : 46713 : if (path.is_single_segment () && path.get_segments ()[0].is_lower_self_seg ())
12291 : : {
12292 : : // HACK: add outer attrs to path
12293 : 6878 : path.set_outer_attrs (std::move (outer_attrs));
12294 : 6878 : return std::unique_ptr<AST::PathInExpression> (
12295 : 6878 : new AST::PathInExpression (std::move (path)));
12296 : : }
12297 : :
12298 : : // branch on next token
12299 : 39835 : const_TokenPtr t = lexer.peek_token ();
12300 : 39835 : switch (t->get_id ())
12301 : : {
12302 : 1778 : case EXCLAM:
12303 : : // macro
12304 : 3556 : return parse_macro_invocation_partial (std::move (path),
12305 : 1778 : std::move (outer_attrs));
12306 : 2880 : case LEFT_CURLY:
12307 : : {
12308 : 2880 : bool not_a_block = lexer.peek_token (1)->get_id () == IDENTIFIER
12309 : 7996 : && (lexer.peek_token (2)->get_id () == COMMA
12310 : 7312 : || (lexer.peek_token (2)->get_id () == COLON
12311 : 4668 : && (lexer.peek_token (4)->get_id () == COMMA
12312 : 631 : || !can_tok_start_type (
12313 : 1406 : lexer.peek_token (3)->get_id ()))));
12314 : :
12315 : : /* definitely not a block:
12316 : : * path '{' ident ','
12317 : : * path '{' ident ':' [anything] ','
12318 : : * path '{' ident ':' [not a type]
12319 : : * otherwise, assume block expr and thus path */
12320 : : // DEBUG
12321 : 11520 : rust_debug ("values of lookahead: '%s' '%s' '%s' '%s' ",
12322 : : lexer.peek_token (1)->get_token_description (),
12323 : : lexer.peek_token (2)->get_token_description (),
12324 : : lexer.peek_token (3)->get_token_description (),
12325 : : lexer.peek_token (4)->get_token_description ());
12326 : :
12327 : 6276 : rust_debug ("can be struct expr: '%s', not a block: '%s'",
12328 : : restrictions.can_be_struct_expr ? "true" : "false",
12329 : : not_a_block ? "true" : "false");
12330 : :
12331 : : // struct/enum expr struct
12332 : 2880 : if (!restrictions.can_be_struct_expr && !not_a_block)
12333 : : {
12334 : : // HACK: add outer attrs to path
12335 : 1525 : path.set_outer_attrs (std::move (outer_attrs));
12336 : 1525 : return std::unique_ptr<AST::PathInExpression> (
12337 : 1525 : new AST::PathInExpression (std::move (path)));
12338 : : }
12339 : 1355 : return parse_struct_expr_struct_partial (std::move (path),
12340 : 1355 : std::move (outer_attrs));
12341 : : }
12342 : 10495 : case LEFT_PAREN:
12343 : : // struct/enum expr tuple
12344 : 10495 : if (!restrictions.can_be_struct_expr)
12345 : : {
12346 : : // assume path is returned
12347 : : // HACK: add outer attributes to path
12348 : 168 : path.set_outer_attrs (std::move (outer_attrs));
12349 : 168 : return std::unique_ptr<AST::PathInExpression> (
12350 : 168 : new AST::PathInExpression (std::move (path)));
12351 : : }
12352 : 10327 : return parse_struct_expr_tuple_partial (std::move (path),
12353 : 10327 : std::move (outer_attrs));
12354 : 24682 : default:
12355 : : // assume path is returned if not single segment
12356 : 24682 : if (path.is_single_segment ())
12357 : : {
12358 : : // FIXME: This should probably be returned as a path.
12359 : : /* HACK: may have to become permanent, but this is my current
12360 : : * identifier expression */
12361 : 70611 : return std::unique_ptr<AST::IdentifierExpr> (new AST::IdentifierExpr (
12362 : 94148 : path.get_segments ()[0].get_ident_segment ().as_string (), {},
12363 : 23537 : path.get_locus ()));
12364 : : }
12365 : : // HACK: add outer attrs to path
12366 : 1145 : path.set_outer_attrs (std::move (outer_attrs));
12367 : 1145 : return std::unique_ptr<AST::PathInExpression> (
12368 : 1145 : new AST::PathInExpression (std::move (path)));
12369 : : }
12370 : : rust_unreachable ();
12371 : 39835 : }
12372 : :
12373 : : // Handling of expresions that do not start with a path for `null_denotation`.
12374 : : template <typename ManagedTokenSource>
12375 : : std::unique_ptr<AST::Expr>
12376 : 34424 : Parser<ManagedTokenSource>::null_denotation_not_path (
12377 : : const_TokenPtr tok, AST::AttrVec outer_attrs, ParseRestrictions restrictions)
12378 : : {
12379 : 34424 : switch (tok->get_id ())
12380 : : {
12381 : : // FIXME: Handle in null_denotation_path?
12382 : 97 : case LEFT_SHIFT:
12383 : : case LEFT_ANGLE:
12384 : : {
12385 : : // qualified path
12386 : : // HACK: add outer attrs to path
12387 : 97 : AST::QualifiedPathInExpression path
12388 : : = parse_qualified_path_in_expression (tok->get_locus ());
12389 : 97 : path.set_outer_attrs (std::move (outer_attrs));
12390 : 97 : return std::unique_ptr<AST::QualifiedPathInExpression> (
12391 : 97 : new AST::QualifiedPathInExpression (std::move (path)));
12392 : 97 : }
12393 : : // FIXME: delegate to parse_literal_expr instead? would have to rejig
12394 : : // tokens and whatever.
12395 : : // FIXME: for literal exprs, outer attrs should be passed in, and later
12396 : : // error if it does not make up the entire statement.
12397 : 15092 : case INT_LITERAL:
12398 : : // we should check the range, but ignore for now
12399 : : // encode as int?
12400 : 15092 : return std::unique_ptr<AST::LiteralExpr> (
12401 : 46555 : new AST::LiteralExpr (tok->get_str (), AST::Literal::INT,
12402 : 15092 : tok->get_type_hint (), {}, tok->get_locus ()));
12403 : 341 : case FLOAT_LITERAL:
12404 : : // encode as float?
12405 : 341 : return std::unique_ptr<AST::LiteralExpr> (
12406 : 1364 : new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT,
12407 : 341 : tok->get_type_hint (), {}, tok->get_locus ()));
12408 : 2389 : case STRING_LITERAL:
12409 : 2389 : return std::unique_ptr<AST::LiteralExpr> (
12410 : 9556 : new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING,
12411 : 2389 : tok->get_type_hint (), {}, tok->get_locus ()));
12412 : 80 : case BYTE_STRING_LITERAL:
12413 : 80 : return std::unique_ptr<AST::LiteralExpr> (
12414 : 320 : new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING,
12415 : 80 : tok->get_type_hint (), {}, tok->get_locus ()));
12416 : 25 : case RAW_STRING_LITERAL:
12417 : 25 : return std::unique_ptr<AST::LiteralExpr> (
12418 : 100 : new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING,
12419 : 25 : tok->get_type_hint (), {}, tok->get_locus ()));
12420 : 190 : case CHAR_LITERAL:
12421 : 190 : return std::unique_ptr<AST::LiteralExpr> (
12422 : 760 : new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,
12423 : 190 : tok->get_type_hint (), {}, tok->get_locus ()));
12424 : 57 : case BYTE_CHAR_LITERAL:
12425 : 57 : return std::unique_ptr<AST::LiteralExpr> (
12426 : 228 : new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE,
12427 : 57 : tok->get_type_hint (), {}, tok->get_locus ()));
12428 : 709 : case TRUE_LITERAL:
12429 : 709 : return std::unique_ptr<AST::LiteralExpr> (
12430 : 2127 : new AST::LiteralExpr (Values::Keywords::TRUE_LITERAL,
12431 : : AST::Literal::BOOL, tok->get_type_hint (), {},
12432 : 709 : tok->get_locus ()));
12433 : 532 : case FALSE_LITERAL:
12434 : 532 : return std::unique_ptr<AST::LiteralExpr> (
12435 : 1596 : new AST::LiteralExpr (Values::Keywords::FALSE_LITERAL,
12436 : : AST::Literal::BOOL, tok->get_type_hint (), {},
12437 : 532 : tok->get_locus ()));
12438 : 799 : case LEFT_PAREN:
12439 : 799 : return parse_grouped_or_tuple_expr (std::move (outer_attrs),
12440 : 799 : tok->get_locus ());
12441 : :
12442 : : /*case PLUS: { // unary plus operator
12443 : : // invoke parse_expr recursively with appropriate priority, etc. for
12444 : : below AST::Expr* expr = parse_expr(LBP_UNARY_PLUS);
12445 : :
12446 : : if (expr == nullptr)
12447 : : return nullptr;
12448 : : // can only apply to integer and float expressions
12449 : : if (expr->get_type() != integer_type_node || expr->get_type() !=
12450 : : float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
12451 : : plus must be int or float but it is %s", print_type(expr->get_type()));
12452 : : return nullptr;
12453 : : }
12454 : :
12455 : : return Tree(expr, tok->get_locus());
12456 : : }*/
12457 : : // Rust has no unary plus operator
12458 : 286 : case MINUS:
12459 : : { // unary minus
12460 : 286 : ParseRestrictions entered_from_unary;
12461 : 286 : entered_from_unary.entered_from_unary = true;
12462 : 286 : if (!restrictions.can_be_struct_expr)
12463 : 14 : entered_from_unary.can_be_struct_expr = false;
12464 : 286 : std::unique_ptr<AST::Expr> expr
12465 : 286 : = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary);
12466 : :
12467 : 286 : if (expr == nullptr)
12468 : 0 : return nullptr;
12469 : : // can only apply to integer and float expressions
12470 : : /*if (expr.get_type() != integer_type_node || expr.get_type() !=
12471 : : float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
12472 : : minus must be int or float but it is %s",
12473 : : print_type(expr.get_type())); return Tree::error();
12474 : : }*/
12475 : : /* FIXME: when implemented the "get type" method on expr, ensure it is
12476 : : * int or float type (except unsigned int). Actually, this would
12477 : : * probably have to be done in semantic analysis (as type checking).
12478 : : */
12479 : :
12480 : : /* FIXME: allow outer attributes on these expressions by having an
12481 : : * outer attrs parameter in function*/
12482 : 286 : return std::unique_ptr<AST::NegationExpr> (
12483 : 286 : new AST::NegationExpr (std::move (expr), NegationOperator::NEGATE,
12484 : 286 : std::move (outer_attrs), tok->get_locus ()));
12485 : 286 : }
12486 : 227 : case EXCLAM:
12487 : : { // logical or bitwise not
12488 : 227 : ParseRestrictions entered_from_unary;
12489 : 227 : entered_from_unary.entered_from_unary = true;
12490 : 227 : if (!restrictions.can_be_struct_expr)
12491 : 70 : entered_from_unary.can_be_struct_expr = false;
12492 : 227 : std::unique_ptr<AST::Expr> expr
12493 : 227 : = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary);
12494 : :
12495 : 227 : if (expr == nullptr)
12496 : 0 : return nullptr;
12497 : : // can only apply to boolean expressions
12498 : : /*if (expr.get_type() != boolean_type_node) {
12499 : : rust_error_at(tok->get_locus(),
12500 : : "operand of logical not must be a boolean but it is %s",
12501 : : print_type(expr.get_type()));
12502 : : return Tree::error();
12503 : : }*/
12504 : : /* FIXME: type checking for boolean or integer expressions in semantic
12505 : : * analysis */
12506 : :
12507 : : // FIXME: allow outer attributes on these expressions
12508 : 227 : return std::unique_ptr<AST::NegationExpr> (
12509 : 227 : new AST::NegationExpr (std::move (expr), NegationOperator::NOT,
12510 : 227 : std::move (outer_attrs), tok->get_locus ()));
12511 : 227 : }
12512 : 3951 : case ASTERISK:
12513 : : {
12514 : : /* pointer dereference only - HACK: as struct expressions should
12515 : : * always be value expressions, cannot be dereferenced */
12516 : 3951 : ParseRestrictions entered_from_unary;
12517 : 3951 : entered_from_unary.entered_from_unary = true;
12518 : 3951 : entered_from_unary.can_be_struct_expr = false;
12519 : 3951 : std::unique_ptr<AST::Expr> expr
12520 : 3951 : = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary);
12521 : : // FIXME: allow outer attributes on expression
12522 : 3951 : return std::unique_ptr<AST::DereferenceExpr> (
12523 : 3951 : new AST::DereferenceExpr (std::move (expr), std::move (outer_attrs),
12524 : 3951 : tok->get_locus ()));
12525 : 3951 : }
12526 : 1421 : case AMP:
12527 : : {
12528 : : // (single) "borrow" expression - shared (mutable) or immutable
12529 : 1421 : std::unique_ptr<AST::Expr> expr = nullptr;
12530 : 1421 : Mutability mutability = Mutability::Imm;
12531 : 1421 : bool raw_borrow = false;
12532 : :
12533 : 1421 : ParseRestrictions entered_from_unary;
12534 : 1421 : entered_from_unary.entered_from_unary = true;
12535 : 1421 : if (!restrictions.can_be_struct_expr)
12536 : 0 : entered_from_unary.can_be_struct_expr = false;
12537 : :
12538 : 9 : auto is_mutability = [] (const_TokenPtr token) {
12539 : 9 : return token->get_id () == CONST || token->get_id () == MUT;
12540 : : };
12541 : :
12542 : 1421 : auto t = lexer.peek_token ();
12543 : : // Weak raw keyword, we look (1) ahead and treat it as an identifier if
12544 : : // there is no mut nor const.
12545 : 2815 : if (t->get_id () == IDENTIFIER
12546 : 734 : && t->get_str () == Values::WeakKeywords::RAW
12547 : 1474 : && is_mutability (lexer.peek_token (1)))
12548 : : {
12549 : 7 : lexer.skip_token ();
12550 : 14 : switch (lexer.peek_token ()->get_id ())
12551 : : {
12552 : : case MUT:
12553 : : mutability = Mutability::Mut;
12554 : : break;
12555 : : case CONST:
12556 : 1 : mutability = Mutability::Imm;
12557 : : break;
12558 : 0 : default:
12559 : 0 : rust_error_at (lexer.peek_token ()->get_locus (),
12560 : : "raw borrow should be either const or mut");
12561 : : }
12562 : 7 : lexer.skip_token ();
12563 : 7 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12564 : 7 : raw_borrow = true;
12565 : : }
12566 : 1414 : else if (t->get_id () == MUT)
12567 : : {
12568 : 401 : lexer.skip_token ();
12569 : 401 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12570 : 401 : mutability = Mutability::Mut;
12571 : 401 : raw_borrow = false;
12572 : : }
12573 : : else
12574 : : {
12575 : 1013 : expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
12576 : 1013 : raw_borrow = false;
12577 : : }
12578 : :
12579 : : // FIXME: allow outer attributes on expression
12580 : 1421 : return std::unique_ptr<AST::BorrowExpr> (
12581 : 1421 : new AST::BorrowExpr (std::move (expr), mutability, raw_borrow, false,
12582 : 1421 : std::move (outer_attrs), tok->get_locus ()));
12583 : 1421 : }
12584 : 23 : case LOGICAL_AND:
12585 : : {
12586 : : // (double) "borrow" expression - shared (mutable) or immutable
12587 : 23 : std::unique_ptr<AST::Expr> expr = nullptr;
12588 : 23 : Mutability mutability = Mutability::Imm;
12589 : :
12590 : 23 : ParseRestrictions entered_from_unary;
12591 : 23 : entered_from_unary.entered_from_unary = true;
12592 : :
12593 : 46 : if (lexer.peek_token ()->get_id () == MUT)
12594 : : {
12595 : 0 : lexer.skip_token ();
12596 : 0 : expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
12597 : 0 : mutability = Mutability::Mut;
12598 : : }
12599 : : else
12600 : : {
12601 : 23 : expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
12602 : 23 : mutability = Mutability::Imm;
12603 : : }
12604 : :
12605 : : // FIXME: allow outer attributes on expression
12606 : 23 : return std::unique_ptr<AST::BorrowExpr> (
12607 : 23 : new AST::BorrowExpr (std::move (expr), mutability, false, true,
12608 : 23 : std::move (outer_attrs), tok->get_locus ()));
12609 : 23 : }
12610 : 71 : case OR:
12611 : : case PIPE:
12612 : : case MOVE:
12613 : : // closure expression
12614 : 213 : return parse_closure_expr_pratt (tok, std::move (outer_attrs));
12615 : 9 : case DOT_DOT:
12616 : : // either "range to" or "range full" expressions
12617 : 27 : return parse_nud_range_exclusive_expr (tok, std::move (outer_attrs));
12618 : 0 : case DOT_DOT_EQ:
12619 : : // range to inclusive expr
12620 : 0 : return parse_range_to_inclusive_expr (tok, std::move (outer_attrs));
12621 : 494 : case RETURN_KW:
12622 : : // FIXME: is this really a null denotation expression?
12623 : 494 : return parse_return_expr (std::move (outer_attrs), tok->get_locus ());
12624 : 1 : case TRY:
12625 : : // FIXME: is this really a null denotation expression?
12626 : 1 : return parse_try_expr (std::move (outer_attrs), tok->get_locus ());
12627 : 69 : case BREAK:
12628 : : // FIXME: is this really a null denotation expression?
12629 : 69 : return parse_break_expr (std::move (outer_attrs), tok->get_locus ());
12630 : 10 : case CONTINUE:
12631 : 10 : return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
12632 : 1335 : case LEFT_CURLY:
12633 : : // ok - this is an expression with block for once.
12634 : 1335 : return parse_block_expr (std::move (outer_attrs), tl::nullopt,
12635 : 1335 : tok->get_locus ());
12636 : 1359 : case IF:
12637 : : // if or if let, so more lookahead to find out
12638 : 2718 : if (lexer.peek_token ()->get_id () == LET)
12639 : : {
12640 : : // if let expr
12641 : 30 : return parse_if_let_expr (std::move (outer_attrs), tok->get_locus ());
12642 : : }
12643 : : else
12644 : : {
12645 : : // if expr
12646 : 1329 : return parse_if_expr (std::move (outer_attrs), tok->get_locus ());
12647 : : }
12648 : 34 : case LIFETIME:
12649 : 102 : return parse_labelled_loop_expr (tok, std::move (outer_attrs));
12650 : 69 : case LOOP:
12651 : 69 : return parse_loop_expr (std::move (outer_attrs), tl::nullopt,
12652 : 69 : tok->get_locus ());
12653 : 67 : case WHILE:
12654 : 134 : if (lexer.peek_token ()->get_id () == LET)
12655 : : {
12656 : 3 : return parse_while_let_loop_expr (std::move (outer_attrs));
12657 : : }
12658 : : else
12659 : : {
12660 : 64 : return parse_while_loop_expr (std::move (outer_attrs), tl::nullopt,
12661 : 64 : tok->get_locus ());
12662 : : }
12663 : 19 : case FOR:
12664 : 19 : return parse_for_loop_expr (std::move (outer_attrs), tl::nullopt);
12665 : 823 : case MATCH_KW:
12666 : : // also an expression with block
12667 : 823 : return parse_match_expr (std::move (outer_attrs), tok->get_locus ());
12668 : 352 : case LEFT_SQUARE:
12669 : : // array definition expr (not indexing)
12670 : 352 : return parse_array_expr (std::move (outer_attrs), tok->get_locus ());
12671 : 3457 : case UNSAFE:
12672 : 3457 : return parse_unsafe_block_expr (std::move (outer_attrs),
12673 : 3457 : tok->get_locus ());
12674 : 2 : case BOX:
12675 : 2 : return parse_box_expr (std::move (outer_attrs), tok->get_locus ());
12676 : 1 : case UNDERSCORE:
12677 : 1 : add_error (
12678 : 1 : Error (tok->get_locus (),
12679 : : "use of %qs is not allowed on the right-side of an assignment",
12680 : : tok->get_token_description ()));
12681 : 1 : return nullptr;
12682 : 7 : case CONST:
12683 : 7 : return parse_const_block_expr (std::move (outer_attrs),
12684 : 7 : tok->get_locus ());
12685 : 26 : default:
12686 : 26 : if (!restrictions.expr_can_be_null)
12687 : 26 : add_error (Error (tok->get_locus (),
12688 : : "found unexpected token %qs in null denotation",
12689 : : tok->get_token_description ()));
12690 : 26 : return nullptr;
12691 : : }
12692 : : }
12693 : :
12694 : : /* Called for each token that can appear in infix (between) position. Can be
12695 : : * operators or other punctuation. Returns a function pointer to member
12696 : : * function that implements the left denotation for the token given. */
12697 : : template <typename ManagedTokenSource>
12698 : : std::unique_ptr<AST::Expr>
12699 : 24708 : Parser<ManagedTokenSource>::left_denotation (const_TokenPtr tok,
12700 : : std::unique_ptr<AST::Expr> left,
12701 : : AST::AttrVec outer_attrs,
12702 : : ParseRestrictions restrictions)
12703 : : {
12704 : : // Token passed in has already been skipped, so peek gives "next" token
12705 : 24708 : switch (tok->get_id ())
12706 : : {
12707 : : // FIXME: allow for outer attributes to be applied
12708 : 1 : case QUESTION_MARK:
12709 : : {
12710 : 1 : location_t left_locus = left->get_locus ();
12711 : : // error propagation expression - unary postfix
12712 : 1 : return std::unique_ptr<AST::ErrorPropagationExpr> (
12713 : 1 : new AST::ErrorPropagationExpr (std::move (left),
12714 : 1 : std::move (outer_attrs), left_locus));
12715 : : }
12716 : 3025 : case PLUS:
12717 : : // sum expression - binary infix
12718 : : /*return parse_binary_plus_expr (tok, std::move (left),
12719 : : std::move (outer_attrs), restrictions);*/
12720 : 9075 : return parse_arithmetic_or_logical_expr (tok, std::move (left),
12721 : : std::move (outer_attrs),
12722 : : ArithmeticOrLogicalOperator::ADD,
12723 : 3025 : restrictions);
12724 : 991 : case MINUS:
12725 : : // difference expression - binary infix
12726 : : /*return parse_binary_minus_expr (tok, std::move (left),
12727 : : std::move (outer_attrs),
12728 : : restrictions);*/
12729 : 2973 : return parse_arithmetic_or_logical_expr (
12730 : : tok, std::move (left), std::move (outer_attrs),
12731 : 991 : ArithmeticOrLogicalOperator::SUBTRACT, restrictions);
12732 : 218 : case ASTERISK:
12733 : : // product expression - binary infix
12734 : : /*return parse_binary_mult_expr (tok, std::move (left),
12735 : : std::move (outer_attrs), restrictions);*/
12736 : 654 : return parse_arithmetic_or_logical_expr (
12737 : : tok, std::move (left), std::move (outer_attrs),
12738 : 218 : ArithmeticOrLogicalOperator::MULTIPLY, restrictions);
12739 : 33 : case DIV:
12740 : : // quotient expression - binary infix
12741 : : /*return parse_binary_div_expr (tok, std::move (left),
12742 : : std::move (outer_attrs), restrictions);*/
12743 : 99 : return parse_arithmetic_or_logical_expr (
12744 : : tok, std::move (left), std::move (outer_attrs),
12745 : 33 : ArithmeticOrLogicalOperator::DIVIDE, restrictions);
12746 : 36 : case PERCENT:
12747 : : // modulo expression - binary infix
12748 : : /*return parse_binary_mod_expr (tok, std::move (left),
12749 : : std::move (outer_attrs), restrictions);*/
12750 : 108 : return parse_arithmetic_or_logical_expr (
12751 : : tok, std::move (left), std::move (outer_attrs),
12752 : 36 : ArithmeticOrLogicalOperator::MODULUS, restrictions);
12753 : 48 : case AMP:
12754 : : // logical or bitwise and expression - binary infix
12755 : : /*return parse_bitwise_and_expr (tok, std::move (left),
12756 : : std::move (outer_attrs), restrictions);*/
12757 : 144 : return parse_arithmetic_or_logical_expr (
12758 : : tok, std::move (left), std::move (outer_attrs),
12759 : 48 : ArithmeticOrLogicalOperator::BITWISE_AND, restrictions);
12760 : 25 : case PIPE:
12761 : : // logical or bitwise or expression - binary infix
12762 : : /*return parse_bitwise_or_expr (tok, std::move (left),
12763 : : std::move (outer_attrs), restrictions);*/
12764 : 75 : return parse_arithmetic_or_logical_expr (
12765 : : tok, std::move (left), std::move (outer_attrs),
12766 : 25 : ArithmeticOrLogicalOperator::BITWISE_OR, restrictions);
12767 : 63 : case CARET:
12768 : : // logical or bitwise xor expression - binary infix
12769 : : /*return parse_bitwise_xor_expr (tok, std::move (left),
12770 : : std::move (outer_attrs), restrictions);*/
12771 : 189 : return parse_arithmetic_or_logical_expr (
12772 : : tok, std::move (left), std::move (outer_attrs),
12773 : 63 : ArithmeticOrLogicalOperator::BITWISE_XOR, restrictions);
12774 : 50 : case LEFT_SHIFT:
12775 : : // left shift expression - binary infix
12776 : : /*return parse_left_shift_expr (tok, std::move (left),
12777 : : std::move (outer_attrs), restrictions);*/
12778 : 150 : return parse_arithmetic_or_logical_expr (
12779 : : tok, std::move (left), std::move (outer_attrs),
12780 : 50 : ArithmeticOrLogicalOperator::LEFT_SHIFT, restrictions);
12781 : 16 : case RIGHT_SHIFT:
12782 : : // right shift expression - binary infix
12783 : : /*return parse_right_shift_expr (tok, std::move (left),
12784 : : std::move (outer_attrs), restrictions);*/
12785 : 48 : return parse_arithmetic_or_logical_expr (
12786 : : tok, std::move (left), std::move (outer_attrs),
12787 : 16 : ArithmeticOrLogicalOperator::RIGHT_SHIFT, restrictions);
12788 : 661 : case EQUAL_EQUAL:
12789 : : // equal to expression - binary infix (no associativity)
12790 : : /*return parse_binary_equal_expr (tok, std::move (left),
12791 : : std::move (outer_attrs),
12792 : : restrictions);*/
12793 : 1983 : return parse_comparison_expr (tok, std::move (left),
12794 : : std::move (outer_attrs),
12795 : 661 : ComparisonOperator::EQUAL, restrictions);
12796 : 159 : case NOT_EQUAL:
12797 : : // not equal to expression - binary infix (no associativity)
12798 : : /*return parse_binary_not_equal_expr (tok, std::move (left),
12799 : : std::move (outer_attrs),
12800 : : restrictions);*/
12801 : 477 : return parse_comparison_expr (tok, std::move (left),
12802 : : std::move (outer_attrs),
12803 : : ComparisonOperator::NOT_EQUAL,
12804 : 159 : restrictions);
12805 : 643 : case RIGHT_ANGLE:
12806 : : // greater than expression - binary infix (no associativity)
12807 : : /*return parse_binary_greater_than_expr (tok, std::move (left),
12808 : : std::move (outer_attrs),
12809 : : restrictions);*/
12810 : 1929 : return parse_comparison_expr (tok, std::move (left),
12811 : : std::move (outer_attrs),
12812 : : ComparisonOperator::GREATER_THAN,
12813 : 643 : restrictions);
12814 : 621 : case LEFT_ANGLE:
12815 : : // less than expression - binary infix (no associativity)
12816 : : /*return parse_binary_less_than_expr (tok, std::move (left),
12817 : : std::move (outer_attrs),
12818 : : restrictions);*/
12819 : 1863 : return parse_comparison_expr (tok, std::move (left),
12820 : : std::move (outer_attrs),
12821 : : ComparisonOperator::LESS_THAN,
12822 : 621 : restrictions);
12823 : 179 : case GREATER_OR_EQUAL:
12824 : : // greater than or equal to expression - binary infix (no associativity)
12825 : : /*return parse_binary_greater_equal_expr (tok, std::move (left),
12826 : : std::move (outer_attrs),
12827 : : restrictions);*/
12828 : 537 : return parse_comparison_expr (tok, std::move (left),
12829 : : std::move (outer_attrs),
12830 : : ComparisonOperator::GREATER_OR_EQUAL,
12831 : 179 : restrictions);
12832 : 224 : case LESS_OR_EQUAL:
12833 : : // less than or equal to expression - binary infix (no associativity)
12834 : : /*return parse_binary_less_equal_expr (tok, std::move (left),
12835 : : std::move (outer_attrs),
12836 : : restrictions);*/
12837 : 672 : return parse_comparison_expr (tok, std::move (left),
12838 : : std::move (outer_attrs),
12839 : : ComparisonOperator::LESS_OR_EQUAL,
12840 : 224 : restrictions);
12841 : 56 : case OR:
12842 : : // lazy logical or expression - binary infix
12843 : 168 : return parse_lazy_or_expr (tok, std::move (left), std::move (outer_attrs),
12844 : 56 : restrictions);
12845 : 259 : case LOGICAL_AND:
12846 : : // lazy logical and expression - binary infix
12847 : 777 : return parse_lazy_and_expr (tok, std::move (left),
12848 : 259 : std::move (outer_attrs), restrictions);
12849 : 4925 : case AS:
12850 : : /* type cast expression - kind of binary infix (RHS is actually a
12851 : : * TypeNoBounds) */
12852 : 14775 : return parse_type_cast_expr (tok, std::move (left),
12853 : 4925 : std::move (outer_attrs), restrictions);
12854 : 2497 : case EQUAL:
12855 : : // assignment expression - binary infix (note right-to-left
12856 : : // associativity)
12857 : 7491 : return parse_assig_expr (tok, std::move (left), std::move (outer_attrs),
12858 : 2497 : restrictions);
12859 : 149 : case PLUS_EQ:
12860 : : /* plus-assignment expression - binary infix (note right-to-left
12861 : : * associativity) */
12862 : : /*return parse_plus_assig_expr (tok, std::move (left),
12863 : : std::move (outer_attrs), restrictions);*/
12864 : 447 : return parse_compound_assignment_expr (tok, std::move (left),
12865 : : std::move (outer_attrs),
12866 : : CompoundAssignmentOperator::ADD,
12867 : 149 : restrictions);
12868 : 70 : case MINUS_EQ:
12869 : : /* minus-assignment expression - binary infix (note right-to-left
12870 : : * associativity) */
12871 : : /*return parse_minus_assig_expr (tok, std::move (left),
12872 : : std::move (outer_attrs), restrictions);*/
12873 : 210 : return parse_compound_assignment_expr (
12874 : : tok, std::move (left), std::move (outer_attrs),
12875 : 70 : CompoundAssignmentOperator::SUBTRACT, restrictions);
12876 : 7 : case ASTERISK_EQ:
12877 : : /* multiply-assignment expression - binary infix (note right-to-left
12878 : : * associativity) */
12879 : : /*return parse_mult_assig_expr (tok, std::move (left),
12880 : : std::move (outer_attrs), restrictions);*/
12881 : 21 : return parse_compound_assignment_expr (
12882 : : tok, std::move (left), std::move (outer_attrs),
12883 : 7 : CompoundAssignmentOperator::MULTIPLY, restrictions);
12884 : 7 : case DIV_EQ:
12885 : : /* division-assignment expression - binary infix (note right-to-left
12886 : : * associativity) */
12887 : : /*return parse_div_assig_expr (tok, std::move (left),
12888 : : std::move (outer_attrs), restrictions);*/
12889 : 21 : return parse_compound_assignment_expr (tok, std::move (left),
12890 : : std::move (outer_attrs),
12891 : : CompoundAssignmentOperator::DIVIDE,
12892 : 7 : restrictions);
12893 : 7 : case PERCENT_EQ:
12894 : : /* modulo-assignment expression - binary infix (note right-to-left
12895 : : * associativity) */
12896 : : /*return parse_mod_assig_expr (tok, std::move (left),
12897 : : std::move (outer_attrs), restrictions);*/
12898 : 21 : return parse_compound_assignment_expr (
12899 : : tok, std::move (left), std::move (outer_attrs),
12900 : 7 : CompoundAssignmentOperator::MODULUS, restrictions);
12901 : 21 : case AMP_EQ:
12902 : : /* bitwise and-assignment expression - binary infix (note right-to-left
12903 : : * associativity) */
12904 : : /*return parse_and_assig_expr (tok, std::move (left),
12905 : : std::move (outer_attrs), restrictions);*/
12906 : 63 : return parse_compound_assignment_expr (
12907 : : tok, std::move (left), std::move (outer_attrs),
12908 : 21 : CompoundAssignmentOperator::BITWISE_AND, restrictions);
12909 : 28 : case PIPE_EQ:
12910 : : /* bitwise or-assignment expression - binary infix (note right-to-left
12911 : : * associativity) */
12912 : : /*return parse_or_assig_expr (tok, std::move (left),
12913 : : std::move (outer_attrs), restrictions);*/
12914 : 84 : return parse_compound_assignment_expr (
12915 : : tok, std::move (left), std::move (outer_attrs),
12916 : 28 : CompoundAssignmentOperator::BITWISE_OR, restrictions);
12917 : 336 : case CARET_EQ:
12918 : : /* bitwise xor-assignment expression - binary infix (note right-to-left
12919 : : * associativity) */
12920 : : /*return parse_xor_assig_expr (tok, std::move (left),
12921 : : std::move (outer_attrs), restrictions);*/
12922 : 1008 : return parse_compound_assignment_expr (
12923 : : tok, std::move (left), std::move (outer_attrs),
12924 : 336 : CompoundAssignmentOperator::BITWISE_XOR, restrictions);
12925 : 7 : case LEFT_SHIFT_EQ:
12926 : : /* left shift-assignment expression - binary infix (note right-to-left
12927 : : * associativity) */
12928 : : /*return parse_left_shift_assig_expr (tok, std::move (left),
12929 : : std::move (outer_attrs),
12930 : : restrictions);*/
12931 : 21 : return parse_compound_assignment_expr (
12932 : : tok, std::move (left), std::move (outer_attrs),
12933 : 7 : CompoundAssignmentOperator::LEFT_SHIFT, restrictions);
12934 : 7 : case RIGHT_SHIFT_EQ:
12935 : : /* right shift-assignment expression - binary infix (note right-to-left
12936 : : * associativity) */
12937 : : /*return parse_right_shift_assig_expr (tok, std::move (left),
12938 : : std::move (outer_attrs),
12939 : : restrictions);*/
12940 : 21 : return parse_compound_assignment_expr (
12941 : : tok, std::move (left), std::move (outer_attrs),
12942 : 7 : CompoundAssignmentOperator::RIGHT_SHIFT, restrictions);
12943 : 78 : case DOT_DOT:
12944 : : /* range exclusive expression - binary infix (no associativity)
12945 : : * either "range" or "range from" */
12946 : 234 : return parse_led_range_exclusive_expr (tok, std::move (left),
12947 : : std::move (outer_attrs),
12948 : 78 : restrictions);
12949 : 7 : case DOT_DOT_EQ:
12950 : : /* range inclusive expression - binary infix (no associativity)
12951 : : * unambiguously RangeInclusiveExpr */
12952 : 21 : return parse_range_inclusive_expr (tok, std::move (left),
12953 : 7 : std::move (outer_attrs), restrictions);
12954 : 0 : case SCOPE_RESOLUTION:
12955 : : // path expression - binary infix? FIXME should this even be parsed
12956 : : // here?
12957 : 0 : add_error (
12958 : 0 : Error (tok->get_locus (),
12959 : : "found scope resolution operator in left denotation "
12960 : : "function - this should probably be handled elsewhere"));
12961 : :
12962 : 0 : return nullptr;
12963 : 8769 : case DOT:
12964 : : {
12965 : : /* field expression or method call - relies on parentheses after next
12966 : : * identifier or await if token after is "await" (unary postfix) or
12967 : : * tuple index if token after is a decimal int literal */
12968 : :
12969 : 8769 : const_TokenPtr next_tok = lexer.peek_token ();
12970 : 8769 : if (next_tok->get_id () == IDENTIFIER
12971 : 8769 : && next_tok->get_str () == Values::Keywords::AWAIT)
12972 : : {
12973 : : // await expression
12974 : 0 : return parse_await_expr (tok, std::move (left),
12975 : 0 : std::move (outer_attrs));
12976 : : }
12977 : 8769 : else if (next_tok->get_id () == INT_LITERAL)
12978 : : {
12979 : : // tuple index expression - TODO check for decimal int literal
12980 : 2694 : return parse_tuple_index_expr (tok, std::move (left),
12981 : : std::move (outer_attrs),
12982 : 898 : restrictions);
12983 : : }
12984 : 7871 : else if (next_tok->get_id () == FLOAT_LITERAL)
12985 : : {
12986 : : // Lexer has misidentified a tuple index as a float literal
12987 : : // eg: `(x, (y, z)).1.0` -> 1.0 has been identified as a float
12988 : : // literal. This means we should split it into three new separate
12989 : : // tokens, the first tuple index, the dot and the second tuple
12990 : : // index.
12991 : 2 : auto current_loc = next_tok->get_locus ();
12992 : 2 : auto str = next_tok->get_str ();
12993 : 2 : auto dot_pos = str.find (".");
12994 : 2 : auto prefix = str.substr (0, dot_pos);
12995 : 2 : auto suffix = str.substr (dot_pos + 1);
12996 : 2 : if (dot_pos == str.size () - 1)
12997 : 3 : lexer.split_current_token (
12998 : : {Token::make_int (current_loc, std::move (prefix),
12999 : : CORETYPE_PURE_DECIMAL),
13000 : : Token::make (DOT, current_loc + 1)});
13001 : : else
13002 : 4 : lexer.split_current_token (
13003 : : {Token::make_int (current_loc, std::move (prefix),
13004 : : CORETYPE_PURE_DECIMAL),
13005 : : Token::make (DOT, current_loc + 1),
13006 : : Token::make_int (current_loc + 2, std::move (suffix),
13007 : : CORETYPE_PURE_DECIMAL)});
13008 : 6 : return parse_tuple_index_expr (tok, std::move (left),
13009 : : std::move (outer_attrs),
13010 : 2 : restrictions);
13011 : 2 : }
13012 : 12199 : else if (next_tok->get_id () == IDENTIFIER
13013 : 18032 : && lexer.peek_token (1)->get_id () != LEFT_PAREN
13014 : 18586 : && lexer.peek_token (1)->get_id () != SCOPE_RESOLUTION)
13015 : : {
13016 : : /* field expression (or should be) - FIXME: scope resolution right
13017 : : * after identifier should always be method, I'm pretty sure */
13018 : 14586 : return parse_field_access_expr (tok, std::move (left),
13019 : : std::move (outer_attrs),
13020 : 4862 : restrictions);
13021 : : }
13022 : : else
13023 : : {
13024 : : // method call (probably)
13025 : 9021 : return parse_method_call_expr (tok, std::move (left),
13026 : : std::move (outer_attrs),
13027 : 3007 : restrictions);
13028 : : }
13029 : 8769 : }
13030 : 267 : case LEFT_PAREN:
13031 : : // function call - method call is based on dot notation first
13032 : 801 : return parse_function_call_expr (tok, std::move (left),
13033 : 267 : std::move (outer_attrs), restrictions);
13034 : 218 : case LEFT_SQUARE:
13035 : : // array or slice index expression (pseudo binary infix)
13036 : 654 : return parse_index_expr (tok, std::move (left), std::move (outer_attrs),
13037 : 218 : restrictions);
13038 : 0 : case FLOAT_LITERAL:
13039 : : /* HACK: get around lexer mis-identifying '.0' or '.1' or whatever as a
13040 : : * float literal - TODO does this happen anymore? It shouldn't. */
13041 : 0 : return parse_tuple_index_expr_float (tok, std::move (left),
13042 : : std::move (outer_attrs),
13043 : 0 : restrictions);
13044 : 0 : default:
13045 : 0 : add_error (Error (tok->get_locus (),
13046 : : "found unexpected token %qs in left denotation",
13047 : : tok->get_token_description ()));
13048 : :
13049 : 0 : return nullptr;
13050 : : }
13051 : : }
13052 : :
13053 : : /* Returns the left binding power for the given ArithmeticOrLogicalExpr type.
13054 : : * TODO make constexpr? Would that even do anything useful? */
13055 : : inline binding_powers
13056 : 4505 : get_lbp_for_arithmetic_or_logical_expr (
13057 : : AST::ArithmeticOrLogicalExpr::ExprType expr_type)
13058 : : {
13059 : 4505 : switch (expr_type)
13060 : : {
13061 : : case ArithmeticOrLogicalOperator::ADD:
13062 : : return LBP_PLUS;
13063 : : case ArithmeticOrLogicalOperator::SUBTRACT:
13064 : : return LBP_MINUS;
13065 : : case ArithmeticOrLogicalOperator::MULTIPLY:
13066 : : return LBP_MUL;
13067 : : case ArithmeticOrLogicalOperator::DIVIDE:
13068 : : return LBP_DIV;
13069 : : case ArithmeticOrLogicalOperator::MODULUS:
13070 : : return LBP_MOD;
13071 : : case ArithmeticOrLogicalOperator::BITWISE_AND:
13072 : : return LBP_AMP;
13073 : : case ArithmeticOrLogicalOperator::BITWISE_OR:
13074 : : return LBP_PIPE;
13075 : : case ArithmeticOrLogicalOperator::BITWISE_XOR:
13076 : : return LBP_CARET;
13077 : : case ArithmeticOrLogicalOperator::LEFT_SHIFT:
13078 : : return LBP_L_SHIFT;
13079 : : case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
13080 : : return LBP_R_SHIFT;
13081 : 0 : default:
13082 : : // WTF? should not happen, this is an error
13083 : 0 : rust_unreachable ();
13084 : :
13085 : : return LBP_PLUS;
13086 : : }
13087 : : }
13088 : :
13089 : : // Parses an arithmetic or logical expression (with Pratt parsing).
13090 : : template <typename ManagedTokenSource>
13091 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13092 : 4505 : Parser<ManagedTokenSource>::parse_arithmetic_or_logical_expr (
13093 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13094 : : AST::ArithmeticOrLogicalExpr::ExprType expr_type,
13095 : : ParseRestrictions restrictions)
13096 : : {
13097 : : // parse RHS (as tok has already been consumed in parse_expression)
13098 : 4505 : std::unique_ptr<AST::Expr> right
13099 : 4505 : = parse_expr (get_lbp_for_arithmetic_or_logical_expr (expr_type),
13100 : 9010 : AST::AttrVec (), restrictions);
13101 : 4505 : if (right == nullptr)
13102 : 2 : return nullptr;
13103 : :
13104 : : // TODO: check types. actually, do so during semantic analysis
13105 : 4503 : location_t locus = left->get_locus ();
13106 : :
13107 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13108 : 4503 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13109 : 4503 : expr_type, locus));
13110 : 4505 : }
13111 : :
13112 : : // Parses a binary addition expression (with Pratt parsing).
13113 : : template <typename ManagedTokenSource>
13114 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13115 : 0 : Parser<ManagedTokenSource>::parse_binary_plus_expr (
13116 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13117 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13118 : : {
13119 : : // parse RHS (as tok has already been consumed in parse_expression)
13120 : 0 : std::unique_ptr<AST::Expr> right
13121 : 0 : = parse_expr (LBP_PLUS, AST::AttrVec (), restrictions);
13122 : 0 : if (right == nullptr)
13123 : 0 : return nullptr;
13124 : :
13125 : : // TODO: check types. actually, do so during semantic analysis
13126 : 0 : location_t locus = left->get_locus ();
13127 : :
13128 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13129 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13130 : 0 : ArithmeticOrLogicalOperator::ADD, locus));
13131 : 0 : }
13132 : :
13133 : : // Parses a binary subtraction expression (with Pratt parsing).
13134 : : template <typename ManagedTokenSource>
13135 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13136 : 0 : Parser<ManagedTokenSource>::parse_binary_minus_expr (
13137 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13138 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13139 : : {
13140 : : // parse RHS (as tok has already been consumed in parse_expression)
13141 : 0 : std::unique_ptr<AST::Expr> right
13142 : 0 : = parse_expr (LBP_MINUS, AST::AttrVec (), restrictions);
13143 : 0 : if (right == nullptr)
13144 : 0 : return nullptr;
13145 : :
13146 : : // TODO: check types. actually, do so during semantic analysis
13147 : 0 : location_t locus = left->get_locus ();
13148 : :
13149 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13150 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13151 : : ArithmeticOrLogicalOperator::SUBTRACT,
13152 : 0 : locus));
13153 : 0 : }
13154 : :
13155 : : // Parses a binary multiplication expression (with Pratt parsing).
13156 : : template <typename ManagedTokenSource>
13157 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13158 : 0 : Parser<ManagedTokenSource>::parse_binary_mult_expr (
13159 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13160 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13161 : : {
13162 : : // parse RHS (as tok has already been consumed in parse_expression)
13163 : 0 : std::unique_ptr<AST::Expr> right
13164 : 0 : = parse_expr (LBP_MUL, AST::AttrVec (), restrictions);
13165 : 0 : if (right == nullptr)
13166 : 0 : return nullptr;
13167 : :
13168 : : // TODO: check types. actually, do so during semantic analysis
13169 : 0 : location_t locus = left->get_locus ();
13170 : :
13171 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13172 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13173 : : ArithmeticOrLogicalOperator::MULTIPLY,
13174 : 0 : locus));
13175 : 0 : }
13176 : :
13177 : : // Parses a binary division expression (with Pratt parsing).
13178 : : template <typename ManagedTokenSource>
13179 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13180 : 0 : Parser<ManagedTokenSource>::parse_binary_div_expr (
13181 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13182 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13183 : : {
13184 : : // parse RHS (as tok has already been consumed in parse_expression)
13185 : 0 : std::unique_ptr<AST::Expr> right
13186 : 0 : = parse_expr (LBP_DIV, AST::AttrVec (), restrictions);
13187 : 0 : if (right == nullptr)
13188 : 0 : return nullptr;
13189 : :
13190 : : // TODO: check types. actually, do so during semantic analysis
13191 : 0 : location_t locus = left->get_locus ();
13192 : :
13193 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13194 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13195 : : ArithmeticOrLogicalOperator::DIVIDE,
13196 : 0 : locus));
13197 : 0 : }
13198 : :
13199 : : // Parses a binary modulo expression (with Pratt parsing).
13200 : : template <typename ManagedTokenSource>
13201 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13202 : 0 : Parser<ManagedTokenSource>::parse_binary_mod_expr (
13203 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13204 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13205 : : {
13206 : : // parse RHS (as tok has already been consumed in parse_expression)
13207 : 0 : std::unique_ptr<AST::Expr> right
13208 : 0 : = parse_expr (LBP_MOD, AST::AttrVec (), restrictions);
13209 : 0 : if (right == nullptr)
13210 : 0 : return nullptr;
13211 : :
13212 : : // TODO: check types. actually, do so during semantic analysis
13213 : 0 : location_t locus = left->get_locus ();
13214 : :
13215 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13216 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13217 : : ArithmeticOrLogicalOperator::MODULUS,
13218 : 0 : locus));
13219 : 0 : }
13220 : :
13221 : : /* Parses a binary bitwise (or eager logical) and expression (with Pratt
13222 : : * parsing). */
13223 : : template <typename ManagedTokenSource>
13224 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13225 : 0 : Parser<ManagedTokenSource>::parse_bitwise_and_expr (
13226 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13227 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13228 : : {
13229 : : // parse RHS (as tok has already been consumed in parse_expression)
13230 : 0 : std::unique_ptr<AST::Expr> right
13231 : 0 : = parse_expr (LBP_AMP, AST::AttrVec (), restrictions);
13232 : 0 : if (right == nullptr)
13233 : 0 : return nullptr;
13234 : :
13235 : : // TODO: check types. actually, do so during semantic analysis
13236 : 0 : location_t locus = left->get_locus ();
13237 : :
13238 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13239 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13240 : : ArithmeticOrLogicalOperator::BITWISE_AND,
13241 : 0 : locus));
13242 : 0 : }
13243 : :
13244 : : /* Parses a binary bitwise (or eager logical) or expression (with Pratt
13245 : : * parsing). */
13246 : : template <typename ManagedTokenSource>
13247 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13248 : 0 : Parser<ManagedTokenSource>::parse_bitwise_or_expr (
13249 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13250 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13251 : : {
13252 : : // parse RHS (as tok has already been consumed in parse_expression)
13253 : 0 : std::unique_ptr<AST::Expr> right
13254 : 0 : = parse_expr (LBP_PIPE, AST::AttrVec (), restrictions);
13255 : 0 : if (right == nullptr)
13256 : 0 : return nullptr;
13257 : :
13258 : : // TODO: check types. actually, do so during semantic analysis
13259 : 0 : location_t locus = left->get_locus ();
13260 : :
13261 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13262 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13263 : : ArithmeticOrLogicalOperator::BITWISE_OR,
13264 : 0 : locus));
13265 : 0 : }
13266 : :
13267 : : /* Parses a binary bitwise (or eager logical) xor expression (with Pratt
13268 : : * parsing). */
13269 : : template <typename ManagedTokenSource>
13270 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13271 : 0 : Parser<ManagedTokenSource>::parse_bitwise_xor_expr (
13272 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13273 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13274 : : {
13275 : : // parse RHS (as tok has already been consumed in parse_expression)
13276 : 0 : std::unique_ptr<AST::Expr> right
13277 : 0 : = parse_expr (LBP_CARET, AST::AttrVec (), restrictions);
13278 : 0 : if (right == nullptr)
13279 : 0 : return nullptr;
13280 : :
13281 : : // TODO: check types. actually, do so during semantic analysis
13282 : 0 : location_t locus = left->get_locus ();
13283 : :
13284 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13285 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13286 : : ArithmeticOrLogicalOperator::BITWISE_XOR,
13287 : 0 : locus));
13288 : 0 : }
13289 : :
13290 : : // Parses a binary left shift expression (with Pratt parsing).
13291 : : template <typename ManagedTokenSource>
13292 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13293 : 0 : Parser<ManagedTokenSource>::parse_left_shift_expr (
13294 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13295 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13296 : : {
13297 : : // parse RHS (as tok has already been consumed in parse_expression)
13298 : 0 : std::unique_ptr<AST::Expr> right
13299 : 0 : = parse_expr (LBP_L_SHIFT, AST::AttrVec (), restrictions);
13300 : 0 : if (right == nullptr)
13301 : 0 : return nullptr;
13302 : :
13303 : : // TODO: check types. actually, do so during semantic analysis
13304 : 0 : location_t locus = left->get_locus ();
13305 : :
13306 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13307 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13308 : : ArithmeticOrLogicalOperator::LEFT_SHIFT,
13309 : 0 : locus));
13310 : 0 : }
13311 : :
13312 : : // Parses a binary right shift expression (with Pratt parsing).
13313 : : template <typename ManagedTokenSource>
13314 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
13315 : 0 : Parser<ManagedTokenSource>::parse_right_shift_expr (
13316 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13317 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13318 : : {
13319 : : // parse RHS (as tok has already been consumed in parse_expression)
13320 : 0 : std::unique_ptr<AST::Expr> right
13321 : 0 : = parse_expr (LBP_R_SHIFT, AST::AttrVec (), restrictions);
13322 : 0 : if (right == nullptr)
13323 : 0 : return nullptr;
13324 : :
13325 : : // TODO: check types. actually, do so during semantic analysis
13326 : 0 : location_t locus = left->get_locus ();
13327 : :
13328 : : return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
13329 : 0 : new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
13330 : : ArithmeticOrLogicalOperator::RIGHT_SHIFT,
13331 : 0 : locus));
13332 : 0 : }
13333 : :
13334 : : /* Returns the left binding power for the given ComparisonExpr type.
13335 : : * TODO make constexpr? Would that even do anything useful? */
13336 : : inline binding_powers
13337 : 2487 : get_lbp_for_comparison_expr (AST::ComparisonExpr::ExprType expr_type)
13338 : : {
13339 : 2487 : switch (expr_type)
13340 : : {
13341 : : case ComparisonOperator::EQUAL:
13342 : : return LBP_EQUAL;
13343 : : case ComparisonOperator::NOT_EQUAL:
13344 : : return LBP_NOT_EQUAL;
13345 : : case ComparisonOperator::GREATER_THAN:
13346 : : return LBP_GREATER_THAN;
13347 : : case ComparisonOperator::LESS_THAN:
13348 : : return LBP_SMALLER_THAN;
13349 : : case ComparisonOperator::GREATER_OR_EQUAL:
13350 : : return LBP_GREATER_EQUAL;
13351 : : case ComparisonOperator::LESS_OR_EQUAL:
13352 : : return LBP_SMALLER_EQUAL;
13353 : 0 : default:
13354 : : // WTF? should not happen, this is an error
13355 : 0 : rust_unreachable ();
13356 : :
13357 : : return LBP_EQUAL;
13358 : : }
13359 : : }
13360 : :
13361 : : /* Parses a ComparisonExpr of given type and LBP. TODO find a way to only
13362 : : * specify one and have the other looked up - e.g. specify ExprType and
13363 : : * binding power is looked up? */
13364 : : template <typename ManagedTokenSource>
13365 : : std::unique_ptr<AST::ComparisonExpr>
13366 : 2487 : Parser<ManagedTokenSource>::parse_comparison_expr (
13367 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13368 : : AST::ComparisonExpr::ExprType expr_type, ParseRestrictions restrictions)
13369 : : {
13370 : : // parse RHS (as tok has already been consumed in parse_expression)
13371 : 2487 : std::unique_ptr<AST::Expr> right
13372 : 2487 : = parse_expr (get_lbp_for_comparison_expr (expr_type), AST::AttrVec (),
13373 : : restrictions);
13374 : 2487 : if (right == nullptr)
13375 : 0 : return nullptr;
13376 : :
13377 : : // TODO: check types. actually, do so during semantic analysis
13378 : 2487 : location_t locus = left->get_locus ();
13379 : :
13380 : : return std::unique_ptr<AST::ComparisonExpr> (
13381 : 2487 : new AST::ComparisonExpr (std::move (left), std::move (right), expr_type,
13382 : 2487 : locus));
13383 : 2487 : }
13384 : :
13385 : : // Parses a binary equal to expression (with Pratt parsing).
13386 : : template <typename ManagedTokenSource>
13387 : : std::unique_ptr<AST::ComparisonExpr>
13388 : 0 : Parser<ManagedTokenSource>::parse_binary_equal_expr (
13389 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13390 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13391 : : {
13392 : : // parse RHS (as tok has already been consumed in parse_expression)
13393 : 0 : std::unique_ptr<AST::Expr> right
13394 : 0 : = parse_expr (LBP_EQUAL, AST::AttrVec (), restrictions);
13395 : 0 : if (right == nullptr)
13396 : 0 : return nullptr;
13397 : :
13398 : : // TODO: check types. actually, do so during semantic analysis
13399 : 0 : location_t locus = left->get_locus ();
13400 : :
13401 : : return std::unique_ptr<AST::ComparisonExpr> (
13402 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13403 : 0 : ComparisonOperator::EQUAL, locus));
13404 : 0 : }
13405 : :
13406 : : // Parses a binary not equal to expression (with Pratt parsing).
13407 : : template <typename ManagedTokenSource>
13408 : : std::unique_ptr<AST::ComparisonExpr>
13409 : 0 : Parser<ManagedTokenSource>::parse_binary_not_equal_expr (
13410 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13411 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13412 : : {
13413 : : // parse RHS (as tok has already been consumed in parse_expression)
13414 : 0 : std::unique_ptr<AST::Expr> right
13415 : 0 : = parse_expr (LBP_NOT_EQUAL, AST::AttrVec (), restrictions);
13416 : 0 : if (right == nullptr)
13417 : 0 : return nullptr;
13418 : :
13419 : : // TODO: check types. actually, do so during semantic analysis
13420 : 0 : location_t locus = left->get_locus ();
13421 : :
13422 : : return std::unique_ptr<AST::ComparisonExpr> (
13423 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13424 : 0 : ComparisonOperator::NOT_EQUAL, locus));
13425 : 0 : }
13426 : :
13427 : : // Parses a binary greater than expression (with Pratt parsing).
13428 : : template <typename ManagedTokenSource>
13429 : : std::unique_ptr<AST::ComparisonExpr>
13430 : 0 : Parser<ManagedTokenSource>::parse_binary_greater_than_expr (
13431 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13432 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13433 : : {
13434 : : // parse RHS (as tok has already been consumed in parse_expression)
13435 : 0 : std::unique_ptr<AST::Expr> right
13436 : 0 : = parse_expr (LBP_GREATER_THAN, AST::AttrVec (), restrictions);
13437 : 0 : if (right == nullptr)
13438 : 0 : return nullptr;
13439 : :
13440 : : // TODO: check types. actually, do so during semantic analysis
13441 : 0 : location_t locus = left->get_locus ();
13442 : :
13443 : : return std::unique_ptr<AST::ComparisonExpr> (
13444 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13445 : 0 : ComparisonOperator::GREATER_THAN, locus));
13446 : 0 : }
13447 : :
13448 : : // Parses a binary less than expression (with Pratt parsing).
13449 : : template <typename ManagedTokenSource>
13450 : : std::unique_ptr<AST::ComparisonExpr>
13451 : 0 : Parser<ManagedTokenSource>::parse_binary_less_than_expr (
13452 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13453 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13454 : : {
13455 : : // parse RHS (as tok has already been consumed in parse_expression)
13456 : 0 : std::unique_ptr<AST::Expr> right
13457 : 0 : = parse_expr (LBP_SMALLER_THAN, AST::AttrVec (), restrictions);
13458 : 0 : if (right == nullptr)
13459 : 0 : return nullptr;
13460 : :
13461 : : // TODO: check types. actually, do so during semantic analysis
13462 : 0 : location_t locus = left->get_locus ();
13463 : :
13464 : : return std::unique_ptr<AST::ComparisonExpr> (
13465 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13466 : 0 : ComparisonOperator::LESS_THAN, locus));
13467 : 0 : }
13468 : :
13469 : : // Parses a binary greater than or equal to expression (with Pratt parsing).
13470 : : template <typename ManagedTokenSource>
13471 : : std::unique_ptr<AST::ComparisonExpr>
13472 : 0 : Parser<ManagedTokenSource>::parse_binary_greater_equal_expr (
13473 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13474 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13475 : : {
13476 : : // parse RHS (as tok has already been consumed in parse_expression)
13477 : 0 : std::unique_ptr<AST::Expr> right
13478 : 0 : = parse_expr (LBP_GREATER_EQUAL, AST::AttrVec (), restrictions);
13479 : 0 : if (right == nullptr)
13480 : 0 : return nullptr;
13481 : :
13482 : : // TODO: check types. actually, do so during semantic analysis
13483 : 0 : location_t locus = left->get_locus ();
13484 : :
13485 : : return std::unique_ptr<AST::ComparisonExpr> (
13486 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13487 : 0 : ComparisonOperator::GREATER_OR_EQUAL, locus));
13488 : 0 : }
13489 : :
13490 : : // Parses a binary less than or equal to expression (with Pratt parsing).
13491 : : template <typename ManagedTokenSource>
13492 : : std::unique_ptr<AST::ComparisonExpr>
13493 : 0 : Parser<ManagedTokenSource>::parse_binary_less_equal_expr (
13494 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13495 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13496 : : {
13497 : : // parse RHS (as tok has already been consumed in parse_expression)
13498 : 0 : std::unique_ptr<AST::Expr> right
13499 : 0 : = parse_expr (LBP_SMALLER_EQUAL, AST::AttrVec (), restrictions);
13500 : 0 : if (right == nullptr)
13501 : 0 : return nullptr;
13502 : :
13503 : : // TODO: check types. actually, do so during semantic analysis
13504 : 0 : location_t locus = left->get_locus ();
13505 : :
13506 : : return std::unique_ptr<AST::ComparisonExpr> (
13507 : 0 : new AST::ComparisonExpr (std::move (left), std::move (right),
13508 : 0 : ComparisonOperator::LESS_OR_EQUAL, locus));
13509 : 0 : }
13510 : :
13511 : : // Parses a binary lazy boolean or expression (with Pratt parsing).
13512 : : template <typename ManagedTokenSource>
13513 : : std::unique_ptr<AST::LazyBooleanExpr>
13514 : 56 : Parser<ManagedTokenSource>::parse_lazy_or_expr (
13515 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13516 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13517 : : {
13518 : : // parse RHS (as tok has already been consumed in parse_expression)
13519 : 56 : std::unique_ptr<AST::Expr> right
13520 : 56 : = parse_expr (LBP_LOGICAL_OR, AST::AttrVec (), restrictions);
13521 : 56 : if (right == nullptr)
13522 : 0 : return nullptr;
13523 : :
13524 : : // TODO: check types. actually, do so during semantic analysis
13525 : 56 : location_t locus = left->get_locus ();
13526 : :
13527 : : return std::unique_ptr<AST::LazyBooleanExpr> (
13528 : 56 : new AST::LazyBooleanExpr (std::move (left), std::move (right),
13529 : 56 : LazyBooleanOperator::LOGICAL_OR, locus));
13530 : 56 : }
13531 : :
13532 : : // Parses a binary lazy boolean and expression (with Pratt parsing).
13533 : : template <typename ManagedTokenSource>
13534 : : std::unique_ptr<AST::LazyBooleanExpr>
13535 : 259 : Parser<ManagedTokenSource>::parse_lazy_and_expr (
13536 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13537 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13538 : : {
13539 : : // parse RHS (as tok has already been consumed in parse_expression)
13540 : 259 : std::unique_ptr<AST::Expr> right
13541 : 259 : = parse_expr (LBP_LOGICAL_AND, AST::AttrVec (), restrictions);
13542 : 259 : if (right == nullptr)
13543 : 0 : return nullptr;
13544 : :
13545 : : // TODO: check types. actually, do so during semantic analysis
13546 : 259 : location_t locus = left->get_locus ();
13547 : :
13548 : : return std::unique_ptr<AST::LazyBooleanExpr> (
13549 : 259 : new AST::LazyBooleanExpr (std::move (left), std::move (right),
13550 : 259 : LazyBooleanOperator::LOGICAL_AND, locus));
13551 : 259 : }
13552 : :
13553 : : // Parses a pseudo-binary infix type cast expression (with Pratt parsing).
13554 : : template <typename ManagedTokenSource>
13555 : : std::unique_ptr<AST::TypeCastExpr>
13556 : 4925 : Parser<ManagedTokenSource>::parse_type_cast_expr (
13557 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> expr_to_cast,
13558 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED,
13559 : : ParseRestrictions restrictions ATTRIBUTE_UNUSED)
13560 : : {
13561 : : // parse RHS (as tok has already been consumed in parse_expression)
13562 : 4925 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
13563 : 4925 : if (type == nullptr)
13564 : 0 : return nullptr;
13565 : : // FIXME: how do I get precedence put in here?
13566 : :
13567 : : // TODO: check types. actually, do so during semantic analysis
13568 : 4925 : location_t locus = expr_to_cast->get_locus ();
13569 : :
13570 : : return std::unique_ptr<AST::TypeCastExpr> (
13571 : 4925 : new AST::TypeCastExpr (std::move (expr_to_cast), std::move (type), locus));
13572 : 4925 : }
13573 : :
13574 : : // Parses a binary assignment expression (with Pratt parsing).
13575 : : template <typename ManagedTokenSource>
13576 : : std::unique_ptr<AST::AssignmentExpr>
13577 : 2497 : Parser<ManagedTokenSource>::parse_assig_expr (
13578 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13579 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions)
13580 : : {
13581 : : // parse RHS (as tok has already been consumed in parse_expression)
13582 : 2497 : std::unique_ptr<AST::Expr> right
13583 : 2497 : = parse_expr (LBP_ASSIG - 1, AST::AttrVec (), restrictions);
13584 : 2497 : if (right == nullptr)
13585 : 0 : return nullptr;
13586 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13587 : :
13588 : : // TODO: check types. actually, do so during semantic analysis
13589 : 2497 : location_t locus = left->get_locus ();
13590 : :
13591 : : return std::unique_ptr<AST::AssignmentExpr> (
13592 : 2497 : new AST::AssignmentExpr (std::move (left), std::move (right),
13593 : 2497 : std::move (outer_attrs), locus));
13594 : 2497 : }
13595 : :
13596 : : /* Returns the left binding power for the given CompoundAssignmentExpr type.
13597 : : * TODO make constexpr? Would that even do anything useful? */
13598 : : inline binding_powers
13599 : 639 : get_lbp_for_compound_assignment_expr (
13600 : : AST::CompoundAssignmentExpr::ExprType expr_type)
13601 : : {
13602 : 639 : switch (expr_type)
13603 : : {
13604 : : case CompoundAssignmentOperator::ADD:
13605 : : return LBP_PLUS;
13606 : : case CompoundAssignmentOperator::SUBTRACT:
13607 : : return LBP_MINUS;
13608 : : case CompoundAssignmentOperator::MULTIPLY:
13609 : : return LBP_MUL;
13610 : : case CompoundAssignmentOperator::DIVIDE:
13611 : : return LBP_DIV;
13612 : : case CompoundAssignmentOperator::MODULUS:
13613 : : return LBP_MOD;
13614 : : case CompoundAssignmentOperator::BITWISE_AND:
13615 : : return LBP_AMP;
13616 : : case CompoundAssignmentOperator::BITWISE_OR:
13617 : : return LBP_PIPE;
13618 : : case CompoundAssignmentOperator::BITWISE_XOR:
13619 : : return LBP_CARET;
13620 : : case CompoundAssignmentOperator::LEFT_SHIFT:
13621 : : return LBP_L_SHIFT;
13622 : : case CompoundAssignmentOperator::RIGHT_SHIFT:
13623 : : return LBP_R_SHIFT;
13624 : 0 : default:
13625 : : // WTF? should not happen, this is an error
13626 : 0 : rust_unreachable ();
13627 : :
13628 : : return LBP_PLUS;
13629 : : }
13630 : : }
13631 : :
13632 : : // Parses a compound assignment expression (with Pratt parsing).
13633 : : template <typename ManagedTokenSource>
13634 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13635 : 639 : Parser<ManagedTokenSource>::parse_compound_assignment_expr (
13636 : : const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
13637 : : AST::CompoundAssignmentExpr::ExprType expr_type,
13638 : : ParseRestrictions restrictions)
13639 : : {
13640 : : // parse RHS (as tok has already been consumed in parse_expression)
13641 : 639 : std::unique_ptr<AST::Expr> right
13642 : 639 : = parse_expr (get_lbp_for_compound_assignment_expr (expr_type) - 1,
13643 : 1278 : AST::AttrVec (), restrictions);
13644 : 639 : if (right == nullptr)
13645 : 0 : return nullptr;
13646 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13647 : :
13648 : : // TODO: check types. actually, do so during semantic analysis
13649 : 639 : location_t locus = left->get_locus ();
13650 : :
13651 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13652 : 639 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13653 : 639 : expr_type, locus));
13654 : 639 : }
13655 : :
13656 : : // Parses a binary add-assignment expression (with Pratt parsing).
13657 : : template <typename ManagedTokenSource>
13658 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13659 : 0 : Parser<ManagedTokenSource>::parse_plus_assig_expr (
13660 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13661 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13662 : : {
13663 : : // parse RHS (as tok has already been consumed in parse_expression)
13664 : 0 : std::unique_ptr<AST::Expr> right
13665 : 0 : = parse_expr (LBP_PLUS_ASSIG - 1, AST::AttrVec (), restrictions);
13666 : 0 : if (right == nullptr)
13667 : 0 : return nullptr;
13668 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13669 : :
13670 : : // TODO: check types. actually, do so during semantic analysis
13671 : 0 : location_t locus = left->get_locus ();
13672 : :
13673 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13674 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13675 : 0 : CompoundAssignmentOperator::ADD, locus));
13676 : 0 : }
13677 : :
13678 : : // Parses a binary minus-assignment expression (with Pratt parsing).
13679 : : template <typename ManagedTokenSource>
13680 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13681 : 0 : Parser<ManagedTokenSource>::parse_minus_assig_expr (
13682 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13683 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13684 : : {
13685 : : // parse RHS (as tok has already been consumed in parse_expression)
13686 : 0 : std::unique_ptr<AST::Expr> right
13687 : 0 : = parse_expr (LBP_MINUS_ASSIG - 1, AST::AttrVec (), restrictions);
13688 : 0 : if (right == nullptr)
13689 : 0 : return nullptr;
13690 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13691 : :
13692 : : // TODO: check types. actually, do so during semantic analysis
13693 : 0 : location_t locus = left->get_locus ();
13694 : :
13695 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13696 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13697 : : CompoundAssignmentOperator::SUBTRACT,
13698 : 0 : locus));
13699 : 0 : }
13700 : :
13701 : : // Parses a binary multiplication-assignment expression (with Pratt parsing).
13702 : : template <typename ManagedTokenSource>
13703 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13704 : 0 : Parser<ManagedTokenSource>::parse_mult_assig_expr (
13705 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13706 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13707 : : {
13708 : : // parse RHS (as tok has already been consumed in parse_expression)
13709 : 0 : std::unique_ptr<AST::Expr> right
13710 : 0 : = parse_expr (LBP_MULT_ASSIG - 1, AST::AttrVec (), restrictions);
13711 : 0 : if (right == nullptr)
13712 : 0 : return nullptr;
13713 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13714 : :
13715 : : // TODO: check types. actually, do so during semantic analysis
13716 : 0 : location_t locus = left->get_locus ();
13717 : :
13718 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13719 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13720 : : CompoundAssignmentOperator::MULTIPLY,
13721 : 0 : locus));
13722 : 0 : }
13723 : :
13724 : : // Parses a binary division-assignment expression (with Pratt parsing).
13725 : : template <typename ManagedTokenSource>
13726 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13727 : 0 : Parser<ManagedTokenSource>::parse_div_assig_expr (
13728 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13729 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13730 : : {
13731 : : // parse RHS (as tok has already been consumed in parse_expression)
13732 : 0 : std::unique_ptr<AST::Expr> right
13733 : 0 : = parse_expr (LBP_DIV_ASSIG - 1, AST::AttrVec (), restrictions);
13734 : 0 : if (right == nullptr)
13735 : 0 : return nullptr;
13736 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13737 : :
13738 : : // TODO: check types. actually, do so during semantic analysis
13739 : 0 : location_t locus = left->get_locus ();
13740 : :
13741 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13742 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13743 : : CompoundAssignmentOperator::DIVIDE,
13744 : 0 : locus));
13745 : 0 : }
13746 : :
13747 : : // Parses a binary modulo-assignment expression (with Pratt parsing).
13748 : : template <typename ManagedTokenSource>
13749 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13750 : 0 : Parser<ManagedTokenSource>::parse_mod_assig_expr (
13751 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13752 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13753 : : {
13754 : : // parse RHS (as tok has already been consumed in parse_expression)
13755 : 0 : std::unique_ptr<AST::Expr> right
13756 : 0 : = parse_expr (LBP_MOD_ASSIG - 1, AST::AttrVec (), restrictions);
13757 : 0 : if (right == nullptr)
13758 : 0 : return nullptr;
13759 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13760 : :
13761 : : // TODO: check types. actually, do so during semantic analysis
13762 : 0 : location_t locus = left->get_locus ();
13763 : :
13764 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13765 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13766 : : CompoundAssignmentOperator::MODULUS,
13767 : 0 : locus));
13768 : 0 : }
13769 : :
13770 : : // Parses a binary and-assignment expression (with Pratt parsing).
13771 : : template <typename ManagedTokenSource>
13772 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13773 : 0 : Parser<ManagedTokenSource>::parse_and_assig_expr (
13774 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13775 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13776 : : {
13777 : : // parse RHS (as tok has already been consumed in parse_expression)
13778 : 0 : std::unique_ptr<AST::Expr> right
13779 : 0 : = parse_expr (LBP_AMP_ASSIG - 1, AST::AttrVec (), restrictions);
13780 : 0 : if (right == nullptr)
13781 : 0 : return nullptr;
13782 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13783 : :
13784 : : // TODO: check types. actually, do so during semantic analysis
13785 : 0 : location_t locus = left->get_locus ();
13786 : :
13787 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13788 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13789 : : CompoundAssignmentOperator::BITWISE_AND,
13790 : 0 : locus));
13791 : 0 : }
13792 : :
13793 : : // Parses a binary or-assignment expression (with Pratt parsing).
13794 : : template <typename ManagedTokenSource>
13795 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13796 : 0 : Parser<ManagedTokenSource>::parse_or_assig_expr (
13797 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13798 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13799 : : {
13800 : : // parse RHS (as tok has already been consumed in parse_expression)
13801 : 0 : std::unique_ptr<AST::Expr> right
13802 : 0 : = parse_expr (LBP_PIPE_ASSIG - 1, AST::AttrVec (), restrictions);
13803 : 0 : if (right == nullptr)
13804 : 0 : return nullptr;
13805 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13806 : :
13807 : : // TODO: check types. actually, do so during semantic analysis
13808 : 0 : location_t locus = left->get_locus ();
13809 : :
13810 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13811 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13812 : : CompoundAssignmentOperator::BITWISE_OR,
13813 : 0 : locus));
13814 : 0 : }
13815 : :
13816 : : // Parses a binary xor-assignment expression (with Pratt parsing).
13817 : : template <typename ManagedTokenSource>
13818 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13819 : 0 : Parser<ManagedTokenSource>::parse_xor_assig_expr (
13820 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13821 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13822 : : {
13823 : : // parse RHS (as tok has already been consumed in parse_expression)
13824 : 0 : std::unique_ptr<AST::Expr> right
13825 : 0 : = parse_expr (LBP_CARET_ASSIG - 1, AST::AttrVec (), restrictions);
13826 : 0 : if (right == nullptr)
13827 : 0 : return nullptr;
13828 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13829 : :
13830 : : // TODO: check types. actually, do so during semantic analysis
13831 : 0 : location_t locus = left->get_locus ();
13832 : :
13833 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13834 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13835 : : CompoundAssignmentOperator::BITWISE_XOR,
13836 : 0 : locus));
13837 : 0 : }
13838 : :
13839 : : // Parses a binary left shift-assignment expression (with Pratt parsing).
13840 : : template <typename ManagedTokenSource>
13841 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13842 : 0 : Parser<ManagedTokenSource>::parse_left_shift_assig_expr (
13843 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13844 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13845 : : {
13846 : : // parse RHS (as tok has already been consumed in parse_expression)
13847 : 0 : std::unique_ptr<AST::Expr> right
13848 : 0 : = parse_expr (LBP_L_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
13849 : 0 : if (right == nullptr)
13850 : 0 : return nullptr;
13851 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13852 : :
13853 : : // TODO: check types. actually, do so during semantic analysis
13854 : 0 : location_t locus = left->get_locus ();
13855 : :
13856 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13857 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13858 : : CompoundAssignmentOperator::LEFT_SHIFT,
13859 : 0 : locus));
13860 : 0 : }
13861 : :
13862 : : // Parses a binary right shift-assignment expression (with Pratt parsing).
13863 : : template <typename ManagedTokenSource>
13864 : : std::unique_ptr<AST::CompoundAssignmentExpr>
13865 : 0 : Parser<ManagedTokenSource>::parse_right_shift_assig_expr (
13866 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13867 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13868 : : {
13869 : : // parse RHS (as tok has already been consumed in parse_expression)
13870 : 0 : std::unique_ptr<AST::Expr> right
13871 : 0 : = parse_expr (LBP_R_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
13872 : 0 : if (right == nullptr)
13873 : 0 : return nullptr;
13874 : : // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
13875 : :
13876 : : // TODO: check types. actually, do so during semantic analysis
13877 : 0 : location_t locus = left->get_locus ();
13878 : :
13879 : : return std::unique_ptr<AST::CompoundAssignmentExpr> (
13880 : 0 : new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
13881 : : CompoundAssignmentOperator::RIGHT_SHIFT,
13882 : 0 : locus));
13883 : 0 : }
13884 : :
13885 : : // Parses a postfix unary await expression (with Pratt parsing).
13886 : : template <typename ManagedTokenSource>
13887 : : std::unique_ptr<AST::AwaitExpr>
13888 : 0 : Parser<ManagedTokenSource>::parse_await_expr (
13889 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> expr_to_await,
13890 : : AST::AttrVec outer_attrs)
13891 : : {
13892 : : /* skip "await" identifier (as "." has already been consumed in
13893 : : * parse_expression) this assumes that the identifier was already identified
13894 : : * as await */
13895 : 0 : if (!skip_token (IDENTIFIER))
13896 : : {
13897 : 0 : Error error (tok->get_locus (), "failed to skip %<await%> in await expr "
13898 : : "- this is probably a deep issue");
13899 : 0 : add_error (std::move (error));
13900 : :
13901 : : // skip somewhere?
13902 : 0 : return nullptr;
13903 : 0 : }
13904 : :
13905 : : // TODO: check inside async block in semantic analysis
13906 : 0 : location_t locus = expr_to_await->get_locus ();
13907 : :
13908 : : return std::unique_ptr<AST::AwaitExpr> (
13909 : 0 : new AST::AwaitExpr (std::move (expr_to_await), std::move (outer_attrs),
13910 : 0 : locus));
13911 : : }
13912 : :
13913 : : /* Parses an exclusive range ('..') in left denotation position (i.e.
13914 : : * RangeFromExpr or RangeFromToExpr). */
13915 : : template <typename ManagedTokenSource>
13916 : : std::unique_ptr<AST::RangeExpr>
13917 : 78 : Parser<ManagedTokenSource>::parse_led_range_exclusive_expr (
13918 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13919 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13920 : : {
13921 : : // FIXME: this probably parses expressions accidently or whatever
13922 : : // try parsing RHS (as tok has already been consumed in parse_expression)
13923 : : // Can be nullptr, in which case it is a RangeFromExpr, otherwise a
13924 : : // RangeFromToExpr.
13925 : 78 : restrictions.expr_can_be_null = true;
13926 : 78 : std::unique_ptr<AST::Expr> right
13927 : 78 : = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
13928 : :
13929 : 78 : location_t locus = left->get_locus ();
13930 : :
13931 : 78 : if (right == nullptr)
13932 : : {
13933 : : // range from expr
13934 : 9 : return std::unique_ptr<AST::RangeFromExpr> (
13935 : 9 : new AST::RangeFromExpr (std::move (left), locus));
13936 : : }
13937 : : else
13938 : : {
13939 : 69 : return std::unique_ptr<AST::RangeFromToExpr> (
13940 : 69 : new AST::RangeFromToExpr (std::move (left), std::move (right), locus));
13941 : : }
13942 : : // FIXME: make non-associative
13943 : 78 : }
13944 : :
13945 : : /* Parses an exclusive range ('..') in null denotation position (i.e.
13946 : : * RangeToExpr or RangeFullExpr). */
13947 : : template <typename ManagedTokenSource>
13948 : : std::unique_ptr<AST::RangeExpr>
13949 : 9 : Parser<ManagedTokenSource>::parse_nud_range_exclusive_expr (
13950 : : const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
13951 : : {
13952 : 9 : auto restrictions = ParseRestrictions ();
13953 : 9 : restrictions.expr_can_be_null = true;
13954 : :
13955 : : // FIXME: this probably parses expressions accidently or whatever
13956 : : // try parsing RHS (as tok has already been consumed in parse_expression)
13957 : 9 : std::unique_ptr<AST::Expr> right
13958 : 9 : = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
13959 : :
13960 : 9 : location_t locus = tok->get_locus ();
13961 : :
13962 : 9 : if (right == nullptr)
13963 : : {
13964 : : // range from expr
13965 : 1 : return std::unique_ptr<AST::RangeFullExpr> (
13966 : 1 : new AST::RangeFullExpr (locus));
13967 : : }
13968 : : else
13969 : : {
13970 : 8 : return std::unique_ptr<AST::RangeToExpr> (
13971 : 8 : new AST::RangeToExpr (std::move (right), locus));
13972 : : }
13973 : : // FIXME: make non-associative
13974 : 9 : }
13975 : :
13976 : : // Parses a full binary range inclusive expression.
13977 : : template <typename ManagedTokenSource>
13978 : : std::unique_ptr<AST::RangeFromToInclExpr>
13979 : 7 : Parser<ManagedTokenSource>::parse_range_inclusive_expr (
13980 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13981 : : AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13982 : : {
13983 : : // parse RHS (as tok has already been consumed in parse_expression)
13984 : 7 : std::unique_ptr<AST::Expr> right
13985 : 7 : = parse_expr (LBP_DOT_DOT_EQ, AST::AttrVec (), restrictions);
13986 : 7 : if (right == nullptr)
13987 : 0 : return nullptr;
13988 : : // FIXME: make non-associative
13989 : :
13990 : : // TODO: check types. actually, do so during semantic analysis
13991 : 7 : location_t locus = left->get_locus ();
13992 : :
13993 : : return std::unique_ptr<AST::RangeFromToInclExpr> (
13994 : 7 : new AST::RangeFromToInclExpr (std::move (left), std::move (right), locus));
13995 : 7 : }
13996 : :
13997 : : // Parses an inclusive range-to prefix unary expression.
13998 : : template <typename ManagedTokenSource>
13999 : : std::unique_ptr<AST::RangeToInclExpr>
14000 : 0 : Parser<ManagedTokenSource>::parse_range_to_inclusive_expr (
14001 : : const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
14002 : : {
14003 : : // parse RHS (as tok has already been consumed in parse_expression)
14004 : 0 : std::unique_ptr<AST::Expr> right = parse_expr (LBP_DOT_DOT_EQ);
14005 : 0 : if (right == nullptr)
14006 : 0 : return nullptr;
14007 : : // FIXME: make non-associative
14008 : :
14009 : : // TODO: check types. actually, do so during semantic analysis
14010 : :
14011 : : return std::unique_ptr<AST::RangeToInclExpr> (
14012 : 0 : new AST::RangeToInclExpr (std::move (right), tok->get_locus ()));
14013 : 0 : }
14014 : :
14015 : : // Parses a pseudo-binary infix tuple index expression.
14016 : : template <typename ManagedTokenSource>
14017 : : std::unique_ptr<AST::TupleIndexExpr>
14018 : 900 : Parser<ManagedTokenSource>::parse_tuple_index_expr (
14019 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> tuple_expr,
14020 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
14021 : : {
14022 : : // parse int literal (as token already skipped)
14023 : 900 : const_TokenPtr index_tok = expect_token (INT_LITERAL);
14024 : 900 : if (index_tok == nullptr)
14025 : : {
14026 : 0 : return nullptr;
14027 : : }
14028 : 1800 : std::string index = index_tok->get_str ();
14029 : :
14030 : : // convert to integer
14031 : 900 : if (!index_tok->is_pure_decimal ())
14032 : : {
14033 : 27 : Error error (index_tok->get_locus (),
14034 : : "tuple index should be a pure decimal literal");
14035 : 27 : add_error (std::move (error));
14036 : 27 : }
14037 : 900 : int index_int = atoi (index.c_str ());
14038 : :
14039 : 900 : location_t locus = tuple_expr->get_locus ();
14040 : :
14041 : : return std::unique_ptr<AST::TupleIndexExpr> (
14042 : 900 : new AST::TupleIndexExpr (std::move (tuple_expr), index_int,
14043 : 900 : std::move (outer_attrs), locus));
14044 : 900 : }
14045 : :
14046 : : // Parses a pseudo-binary infix array (or slice) index expression.
14047 : : template <typename ManagedTokenSource>
14048 : : std::unique_ptr<AST::ArrayIndexExpr>
14049 : 218 : Parser<ManagedTokenSource>::parse_index_expr (
14050 : : const_TokenPtr, std::unique_ptr<AST::Expr> array_expr,
14051 : : AST::AttrVec outer_attrs, ParseRestrictions)
14052 : : {
14053 : : // parse RHS (as tok has already been consumed in parse_expression)
14054 : : /*std::unique_ptr<AST::Expr> index_expr
14055 : : = parse_expr (LBP_ARRAY_REF, AST::AttrVec (),
14056 : : restrictions);*/
14057 : : // TODO: conceptually, should treat [] as brackets, so just parse all expr
14058 : 218 : std::unique_ptr<AST::Expr> index_expr = parse_expr ();
14059 : 218 : if (index_expr == nullptr)
14060 : 0 : return nullptr;
14061 : :
14062 : : // skip ']' at end of array
14063 : 218 : if (!skip_token (RIGHT_SQUARE))
14064 : : {
14065 : : // skip somewhere?
14066 : 0 : return nullptr;
14067 : : }
14068 : :
14069 : : // TODO: check types. actually, do so during semantic analysis
14070 : 218 : location_t locus = array_expr->get_locus ();
14071 : :
14072 : : return std::unique_ptr<AST::ArrayIndexExpr> (
14073 : 218 : new AST::ArrayIndexExpr (std::move (array_expr), std::move (index_expr),
14074 : 218 : std::move (outer_attrs), locus));
14075 : 218 : }
14076 : :
14077 : : // Parses a pseudo-binary infix struct field access expression.
14078 : : template <typename ManagedTokenSource>
14079 : : std::unique_ptr<AST::FieldAccessExpr>
14080 : 4862 : Parser<ManagedTokenSource>::parse_field_access_expr (
14081 : : const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> struct_expr,
14082 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
14083 : : {
14084 : : /* get field name identifier (assume that this is a field access expr and
14085 : : * not await, for instance) */
14086 : 4862 : const_TokenPtr ident_tok = expect_token (IDENTIFIER);
14087 : 4862 : if (ident_tok == nullptr)
14088 : 0 : return nullptr;
14089 : :
14090 : 9724 : Identifier ident{ident_tok};
14091 : :
14092 : 4862 : location_t locus = struct_expr->get_locus ();
14093 : :
14094 : : // TODO: check types. actually, do so during semantic analysis
14095 : : return std::unique_ptr<AST::FieldAccessExpr> (
14096 : 4862 : new AST::FieldAccessExpr (std::move (struct_expr), std::move (ident),
14097 : 4862 : std::move (outer_attrs), locus));
14098 : 4862 : }
14099 : :
14100 : : // Parses a pseudo-binary infix method call expression.
14101 : : template <typename ManagedTokenSource>
14102 : : std::unique_ptr<AST::MethodCallExpr>
14103 : 3007 : Parser<ManagedTokenSource>::parse_method_call_expr (
14104 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
14105 : : AST::AttrVec outer_attrs, ParseRestrictions)
14106 : : {
14107 : : // parse path expr segment
14108 : 3007 : AST::PathExprSegment segment = parse_path_expr_segment ();
14109 : 3007 : if (segment.is_error ())
14110 : : {
14111 : 0 : Error error (tok->get_locus (),
14112 : : "failed to parse path expr segment of method call expr");
14113 : 0 : add_error (std::move (error));
14114 : :
14115 : 0 : return nullptr;
14116 : 0 : }
14117 : :
14118 : : // skip left parentheses
14119 : 3007 : if (!skip_token (LEFT_PAREN))
14120 : : {
14121 : 0 : return nullptr;
14122 : : }
14123 : :
14124 : : // parse method params (if they exist)
14125 : 3007 : std::vector<std::unique_ptr<AST::Expr>> params;
14126 : :
14127 : 3007 : const_TokenPtr t = lexer.peek_token ();
14128 : 5041 : while (t->get_id () != RIGHT_PAREN)
14129 : : {
14130 : 2034 : std::unique_ptr<AST::Expr> param = parse_expr ();
14131 : 2034 : if (param == nullptr)
14132 : : {
14133 : 0 : Error error (t->get_locus (),
14134 : : "failed to parse method param in method call");
14135 : 0 : add_error (std::move (error));
14136 : :
14137 : 0 : return nullptr;
14138 : 0 : }
14139 : 2034 : params.push_back (std::move (param));
14140 : :
14141 : 4068 : if (lexer.peek_token ()->get_id () != COMMA)
14142 : : break;
14143 : :
14144 : 0 : lexer.skip_token ();
14145 : 0 : t = lexer.peek_token ();
14146 : : }
14147 : :
14148 : : // skip right paren
14149 : 3007 : if (!skip_token (RIGHT_PAREN))
14150 : : {
14151 : 0 : return nullptr;
14152 : : }
14153 : :
14154 : : // TODO: check types. actually do so in semantic analysis pass.
14155 : 3007 : location_t locus = receiver_expr->get_locus ();
14156 : :
14157 : : return std::unique_ptr<AST::MethodCallExpr> (
14158 : 6014 : new AST::MethodCallExpr (std::move (receiver_expr), std::move (segment),
14159 : : std::move (params), std::move (outer_attrs),
14160 : 3007 : locus));
14161 : 3007 : }
14162 : :
14163 : : // Parses a pseudo-binary infix function call expression.
14164 : : template <typename ManagedTokenSource>
14165 : : std::unique_ptr<AST::CallExpr>
14166 : 267 : Parser<ManagedTokenSource>::parse_function_call_expr (
14167 : : const_TokenPtr, std::unique_ptr<AST::Expr> function_expr,
14168 : : AST::AttrVec outer_attrs, ParseRestrictions)
14169 : : {
14170 : : // parse function params (if they exist)
14171 : 267 : std::vector<std::unique_ptr<AST::Expr>> params;
14172 : :
14173 : 267 : const_TokenPtr t = lexer.peek_token ();
14174 : 437 : while (t->get_id () != RIGHT_PAREN)
14175 : : {
14176 : 170 : std::unique_ptr<AST::Expr> param = parse_expr ();
14177 : 170 : if (param == nullptr)
14178 : : {
14179 : 0 : Error error (t->get_locus (),
14180 : : "failed to parse function param in function call");
14181 : 0 : add_error (std::move (error));
14182 : :
14183 : 0 : return nullptr;
14184 : 0 : }
14185 : 170 : params.push_back (std::move (param));
14186 : :
14187 : 340 : if (lexer.peek_token ()->get_id () != COMMA)
14188 : : break;
14189 : :
14190 : 24 : lexer.skip_token ();
14191 : 24 : t = lexer.peek_token ();
14192 : : }
14193 : :
14194 : : // skip ')' at end of param list
14195 : 267 : if (!skip_token (RIGHT_PAREN))
14196 : : {
14197 : : // skip somewhere?
14198 : 0 : return nullptr;
14199 : : }
14200 : :
14201 : : // TODO: check types. actually, do so during semantic analysis
14202 : 267 : location_t locus = function_expr->get_locus ();
14203 : :
14204 : : return std::unique_ptr<AST::CallExpr> (
14205 : 267 : new AST::CallExpr (std::move (function_expr), std::move (params),
14206 : 267 : std::move (outer_attrs), locus));
14207 : 267 : }
14208 : :
14209 : : /* Parses a macro invocation with a path in expression already parsed (but not
14210 : : * '!' token). */
14211 : : template <typename ManagedTokenSource>
14212 : : std::unique_ptr<AST::MacroInvocation>
14213 : 2410 : Parser<ManagedTokenSource>::parse_macro_invocation_partial (
14214 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
14215 : : ParseRestrictions restrictions)
14216 : : {
14217 : : // macro invocation
14218 : 2410 : if (!skip_token (EXCLAM))
14219 : : {
14220 : 0 : return nullptr;
14221 : : }
14222 : :
14223 : : // convert PathInExpression to SimplePath - if this isn't possible, error
14224 : 2410 : AST::SimplePath converted_path = path.as_simple_path ();
14225 : 2410 : if (converted_path.is_empty ())
14226 : : {
14227 : 0 : Error error (lexer.peek_token ()->get_locus (),
14228 : : "failed to parse simple path in macro invocation");
14229 : 0 : add_error (std::move (error));
14230 : :
14231 : 0 : return nullptr;
14232 : 0 : }
14233 : :
14234 : 2410 : AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
14235 : :
14236 : 2410 : rust_debug ("successfully parsed macro invocation (via partial)");
14237 : :
14238 : 2410 : location_t macro_locus = converted_path.get_locus ();
14239 : :
14240 : 4820 : return AST::MacroInvocation::Regular (
14241 : 4820 : AST::MacroInvocData (std::move (converted_path), std::move (tok_tree)),
14242 : 2410 : std::move (outer_attrs), macro_locus);
14243 : 2410 : }
14244 : :
14245 : : /* Parses a struct expr struct with a path in expression already parsed (but
14246 : : * not
14247 : : * '{' token). */
14248 : : template <typename ManagedTokenSource>
14249 : : std::unique_ptr<AST::StructExprStruct>
14250 : 1355 : Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
14251 : : AST::PathInExpression path, AST::AttrVec outer_attrs)
14252 : : {
14253 : : // assume struct expr struct (as struct-enum disambiguation requires name
14254 : : // lookup) again, make statement if final ';'
14255 : 1355 : if (!skip_token (LEFT_CURLY))
14256 : : {
14257 : 0 : return nullptr;
14258 : : }
14259 : :
14260 : : // parse inner attributes
14261 : 1355 : AST::AttrVec inner_attrs = parse_inner_attributes ();
14262 : :
14263 : : // branch based on next token
14264 : 1355 : const_TokenPtr t = lexer.peek_token ();
14265 : 1355 : location_t path_locus = path.get_locus ();
14266 : 1355 : switch (t->get_id ())
14267 : : {
14268 : 65 : case RIGHT_CURLY:
14269 : : // struct with no body
14270 : 65 : lexer.skip_token ();
14271 : :
14272 : : return std::unique_ptr<AST::StructExprStruct> (
14273 : 65 : new AST::StructExprStruct (std::move (path), std::move (inner_attrs),
14274 : 65 : std::move (outer_attrs), path_locus));
14275 : 1290 : case DOT_DOT:
14276 : : /* technically this would give a struct base-only struct, but this
14277 : : * algorithm should work too. As such, AST type not happening. */
14278 : : case IDENTIFIER:
14279 : : case HASH:
14280 : : case INT_LITERAL:
14281 : : {
14282 : : // struct with struct expr fields
14283 : :
14284 : : // parse struct expr fields
14285 : 1290 : std::vector<std::unique_ptr<AST::StructExprField>> fields;
14286 : :
14287 : 3526 : while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
14288 : : {
14289 : 2236 : std::unique_ptr<AST::StructExprField> field
14290 : : = parse_struct_expr_field ();
14291 : 2236 : if (field == nullptr)
14292 : : {
14293 : 0 : Error error (t->get_locus (),
14294 : : "failed to parse struct (or enum) expr field");
14295 : 0 : add_error (std::move (error));
14296 : :
14297 : 0 : return nullptr;
14298 : 0 : }
14299 : :
14300 : : // DEBUG:
14301 : 2236 : rust_debug ("struct/enum expr field validated to not be null");
14302 : :
14303 : 2236 : fields.push_back (std::move (field));
14304 : :
14305 : : // DEBUG:
14306 : 2236 : rust_debug ("struct/enum expr field pushed back");
14307 : :
14308 : 4472 : if (lexer.peek_token ()->get_id () != COMMA)
14309 : : {
14310 : : // DEBUG:
14311 : 1059 : rust_debug ("lack of comma detected in struct/enum expr "
14312 : : "fields - break");
14313 : : break;
14314 : : }
14315 : 1177 : lexer.skip_token ();
14316 : :
14317 : : // DEBUG:
14318 : 1177 : rust_debug ("struct/enum expr fields comma skipped ");
14319 : :
14320 : 1177 : t = lexer.peek_token ();
14321 : : }
14322 : :
14323 : : // DEBUG:
14324 : 1290 : rust_debug ("struct/enum expr about to parse struct base ");
14325 : :
14326 : : // parse struct base if it exists
14327 : : AST::StructBase struct_base = AST::StructBase::error ();
14328 : 2580 : if (lexer.peek_token ()->get_id () == DOT_DOT)
14329 : : {
14330 : 63 : location_t dot_dot_location = lexer.peek_token ()->get_locus ();
14331 : 63 : lexer.skip_token ();
14332 : :
14333 : : // parse required struct base expr
14334 : 63 : std::unique_ptr<AST::Expr> base_expr = parse_expr ();
14335 : 63 : if (base_expr == nullptr)
14336 : : {
14337 : 0 : Error error (lexer.peek_token ()->get_locus (),
14338 : : "failed to parse struct base expression in struct "
14339 : : "expression");
14340 : 0 : add_error (std::move (error));
14341 : :
14342 : 0 : return nullptr;
14343 : 0 : }
14344 : :
14345 : : // DEBUG:
14346 : 63 : rust_debug ("struct/enum expr - parsed and validated base expr");
14347 : :
14348 : : struct_base
14349 : 63 : = AST::StructBase (std::move (base_expr), dot_dot_location);
14350 : :
14351 : : // DEBUG:
14352 : 63 : rust_debug ("assigned struct base to new struct base ");
14353 : 63 : }
14354 : :
14355 : 1290 : if (!skip_token (RIGHT_CURLY))
14356 : : {
14357 : 0 : return nullptr;
14358 : : }
14359 : :
14360 : : // DEBUG:
14361 : 1290 : rust_debug (
14362 : : "struct/enum expr skipped right curly - done and ready to return");
14363 : :
14364 : 1290 : return std::unique_ptr<AST::StructExprStructFields> (
14365 : 1290 : new AST::StructExprStructFields (std::move (path), std::move (fields),
14366 : : path_locus, std::move (struct_base),
14367 : : std::move (inner_attrs),
14368 : 1290 : std::move (outer_attrs)));
14369 : 1290 : }
14370 : 0 : default:
14371 : 0 : add_error (
14372 : 0 : Error (t->get_locus (),
14373 : : "unrecognised token %qs in struct (or enum) expression - "
14374 : : "expected %<}%>, identifier, integer literal, or %<..%>",
14375 : : t->get_token_description ()));
14376 : :
14377 : 0 : return nullptr;
14378 : : }
14379 : 1355 : }
14380 : :
14381 : : /* Parses a struct expr tuple with a path in expression already parsed (but
14382 : : * not
14383 : : * '(' token).
14384 : : * FIXME: this currently outputs a call expr, as they cannot be disambiguated.
14385 : : * A better solution would be to just get this to call that function directly.
14386 : : * */
14387 : : template <typename ManagedTokenSource>
14388 : : std::unique_ptr<AST::CallExpr>
14389 : 10327 : Parser<ManagedTokenSource>::parse_struct_expr_tuple_partial (
14390 : : AST::PathInExpression path, AST::AttrVec outer_attrs)
14391 : : {
14392 : 10327 : if (!skip_token (LEFT_PAREN))
14393 : : {
14394 : 0 : return nullptr;
14395 : : }
14396 : :
14397 : 10327 : AST::AttrVec inner_attrs = parse_inner_attributes ();
14398 : :
14399 : 10327 : std::vector<std::unique_ptr<AST::Expr>> exprs;
14400 : :
14401 : 10327 : const_TokenPtr t = lexer.peek_token ();
14402 : 22567 : while (t->get_id () != RIGHT_PAREN)
14403 : : {
14404 : : // parse expression (required)
14405 : 12240 : std::unique_ptr<AST::Expr> expr = parse_expr ();
14406 : 12240 : if (expr == nullptr)
14407 : : {
14408 : 0 : Error error (t->get_locus (), "failed to parse expression in "
14409 : : "struct (or enum) expression tuple");
14410 : 0 : add_error (std::move (error));
14411 : :
14412 : 0 : return nullptr;
14413 : 0 : }
14414 : 12240 : exprs.push_back (std::move (expr));
14415 : :
14416 : 24480 : if (lexer.peek_token ()->get_id () != COMMA)
14417 : : break;
14418 : :
14419 : 3933 : lexer.skip_token ();
14420 : :
14421 : 3933 : t = lexer.peek_token ();
14422 : : }
14423 : :
14424 : 10327 : if (!skip_token (RIGHT_PAREN))
14425 : : {
14426 : 0 : return nullptr;
14427 : : }
14428 : :
14429 : 10327 : location_t path_locus = path.get_locus ();
14430 : :
14431 : 10327 : auto pathExpr = std::unique_ptr<AST::PathInExpression> (
14432 : 10327 : new AST::PathInExpression (std::move (path)));
14433 : :
14434 : : return std::unique_ptr<AST::CallExpr> (
14435 : 10327 : new AST::CallExpr (std::move (pathExpr), std::move (exprs),
14436 : 10327 : std::move (outer_attrs), path_locus));
14437 : 20654 : }
14438 : :
14439 : : /* Parses a path in expression with the first token passed as a parameter (as
14440 : : * it is skipped in token stream). Note that this only parses segment-first
14441 : : * paths, not global ones. */
14442 : : template <typename ManagedTokenSource>
14443 : : AST::PathInExpression
14444 : 32788 : Parser<ManagedTokenSource>::parse_path_in_expression_pratt (const_TokenPtr tok)
14445 : : {
14446 : : // HACK-y way of making up for pratt-parsing consuming first token
14447 : :
14448 : : // DEBUG
14449 : 65576 : rust_debug ("current peek token when starting path pratt parse: '%s'",
14450 : : lexer.peek_token ()->get_token_description ());
14451 : :
14452 : : // create segment vector
14453 : 32788 : std::vector<AST::PathExprSegment> segments;
14454 : :
14455 : 32788 : std::string initial_str;
14456 : :
14457 : 32788 : switch (tok->get_id ())
14458 : : {
14459 : 27263 : case IDENTIFIER:
14460 : 27263 : initial_str = tok->get_str ();
14461 : : break;
14462 : 0 : case SUPER:
14463 : 0 : initial_str = Values::Keywords::SUPER;
14464 : : break;
14465 : 5186 : case SELF:
14466 : 5186 : initial_str = Values::Keywords::SELF;
14467 : : break;
14468 : 156 : case SELF_ALIAS:
14469 : 156 : initial_str = Values::Keywords::SELF_ALIAS;
14470 : : break;
14471 : 183 : case CRATE:
14472 : 183 : initial_str = Values::Keywords::CRATE;
14473 : : break;
14474 : 0 : case DOLLAR_SIGN:
14475 : 0 : if (lexer.peek_token ()->get_id () == CRATE)
14476 : : {
14477 : 0 : initial_str = "$crate";
14478 : : break;
14479 : : }
14480 : : gcc_fallthrough ();
14481 : : default:
14482 : 0 : add_error (Error (tok->get_locus (),
14483 : : "unrecognised token %qs in path in expression",
14484 : : tok->get_token_description ()));
14485 : :
14486 : 0 : return AST::PathInExpression::create_error ();
14487 : : }
14488 : :
14489 : : // parse required initial segment
14490 : 65576 : AST::PathExprSegment initial_segment (initial_str, tok->get_locus ());
14491 : : // parse generic args (and turbofish), if they exist
14492 : : /* use lookahead to determine if they actually exist (don't want to
14493 : : * accidently parse over next ident segment) */
14494 : 65576 : if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
14495 : 35204 : && lexer.peek_token (1)->get_id () == LEFT_ANGLE)
14496 : : {
14497 : : // skip scope resolution
14498 : 308 : lexer.skip_token ();
14499 : :
14500 : 308 : AST::GenericArgs generic_args = parse_path_generic_args ();
14501 : :
14502 : : initial_segment
14503 : 924 : = AST::PathExprSegment (AST::PathIdentSegment (initial_str,
14504 : : tok->get_locus ()),
14505 : 308 : tok->get_locus (), std::move (generic_args));
14506 : 308 : }
14507 : 32788 : if (initial_segment.is_error ())
14508 : : {
14509 : : // skip after somewhere?
14510 : : // don't necessarily throw error but yeah
14511 : :
14512 : : // DEBUG
14513 : 0 : rust_debug ("initial segment is error - returning null");
14514 : :
14515 : 0 : return AST::PathInExpression::create_error ();
14516 : : }
14517 : 32788 : segments.push_back (std::move (initial_segment));
14518 : :
14519 : : // parse optional segments (as long as scope resolution operator exists)
14520 : 32788 : const_TokenPtr t = lexer.peek_token ();
14521 : 35261 : while (t->get_id () == SCOPE_RESOLUTION)
14522 : : {
14523 : : // skip scope resolution operator
14524 : 2473 : lexer.skip_token ();
14525 : :
14526 : : // parse the actual segment - it is an error if it doesn't exist now
14527 : 2473 : AST::PathExprSegment segment = parse_path_expr_segment ();
14528 : 2473 : if (segment.is_error ())
14529 : : {
14530 : : // skip after somewhere?
14531 : 0 : Error error (t->get_locus (),
14532 : : "could not parse path expression segment");
14533 : 0 : add_error (std::move (error));
14534 : :
14535 : 0 : return AST::PathInExpression::create_error ();
14536 : 0 : }
14537 : :
14538 : 2473 : segments.push_back (std::move (segment));
14539 : :
14540 : 2473 : t = lexer.peek_token ();
14541 : : }
14542 : :
14543 : : // DEBUG:
14544 : 65576 : rust_debug (
14545 : : "current token (just about to return path to null denotation): '%s'",
14546 : : lexer.peek_token ()->get_token_description ());
14547 : :
14548 : 32788 : return AST::PathInExpression (std::move (segments), {}, tok->get_locus (),
14549 : 32788 : false);
14550 : 98364 : }
14551 : :
14552 : : // Parses a closure expression with pratt parsing (from null denotation).
14553 : : template <typename ManagedTokenSource>
14554 : : std::unique_ptr<AST::ClosureExpr>
14555 : 71 : Parser<ManagedTokenSource>::parse_closure_expr_pratt (const_TokenPtr tok,
14556 : : AST::AttrVec outer_attrs)
14557 : : {
14558 : : // TODO: does this need pratt parsing (for precedence)? probably not, but
14559 : : // idk
14560 : 71 : location_t locus = tok->get_locus ();
14561 : 71 : bool has_move = false;
14562 : 71 : if (tok->get_id () == MOVE)
14563 : : {
14564 : 1 : has_move = true;
14565 : 1 : tok = lexer.peek_token ();
14566 : 1 : lexer.skip_token ();
14567 : : // skip token and reassign
14568 : : }
14569 : :
14570 : : // handle parameter list
14571 : 71 : std::vector<AST::ClosureParam> params;
14572 : :
14573 : 71 : switch (tok->get_id ())
14574 : : {
14575 : : case OR:
14576 : : // no parameters, don't skip token
14577 : : break;
14578 : 60 : case PIPE:
14579 : : {
14580 : : // actually may have parameters
14581 : : // don't skip token
14582 : 60 : const_TokenPtr t = lexer.peek_token ();
14583 : 131 : while (t->get_id () != PIPE)
14584 : : {
14585 : 71 : AST::ClosureParam param = parse_closure_param ();
14586 : 71 : if (param.is_error ())
14587 : : {
14588 : : // TODO is this really an error?
14589 : 0 : Error error (t->get_locus (), "could not parse closure param");
14590 : 0 : add_error (std::move (error));
14591 : :
14592 : 0 : return nullptr;
14593 : 0 : }
14594 : 71 : params.push_back (std::move (param));
14595 : :
14596 : 142 : if (lexer.peek_token ()->get_id () != COMMA)
14597 : : {
14598 : 120 : if (lexer.peek_token ()->get_id () == OR)
14599 : 1 : lexer.split_current_token (PIPE, PIPE);
14600 : : // not an error but means param list is done
14601 : : break;
14602 : : }
14603 : : // skip comma
14604 : 11 : lexer.skip_token ();
14605 : :
14606 : 22 : if (lexer.peek_token ()->get_id () == OR)
14607 : 0 : lexer.split_current_token (PIPE, PIPE);
14608 : :
14609 : 11 : t = lexer.peek_token ();
14610 : : }
14611 : :
14612 : 60 : if (!skip_token (PIPE))
14613 : : {
14614 : 0 : return nullptr;
14615 : : }
14616 : : break;
14617 : 60 : }
14618 : 0 : default:
14619 : 0 : add_error (Error (tok->get_locus (),
14620 : : "unexpected token %qs in closure expression - expected "
14621 : : "%<|%> or %<||%>",
14622 : : tok->get_token_description ()));
14623 : :
14624 : : // skip somewhere?
14625 : 0 : return nullptr;
14626 : : }
14627 : :
14628 : : // again branch based on next token
14629 : 71 : tok = lexer.peek_token ();
14630 : 71 : if (tok->get_id () == RETURN_TYPE)
14631 : : {
14632 : : // must be return type closure with block expr
14633 : :
14634 : : // skip "return type" token
14635 : 31 : lexer.skip_token ();
14636 : :
14637 : : // parse actual type, which is required
14638 : 31 : std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
14639 : 31 : if (type == nullptr)
14640 : : {
14641 : : // error
14642 : 0 : Error error (tok->get_locus (), "failed to parse type for closure");
14643 : 0 : add_error (std::move (error));
14644 : :
14645 : : // skip somewhere?
14646 : 0 : return nullptr;
14647 : 0 : }
14648 : :
14649 : : // parse block expr, which is required
14650 : 31 : std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
14651 : 31 : if (block == nullptr)
14652 : : {
14653 : : // error
14654 : 0 : Error error (lexer.peek_token ()->get_locus (),
14655 : : "failed to parse block expr in closure");
14656 : 0 : add_error (std::move (error));
14657 : :
14658 : : // skip somewhere?
14659 : 0 : return nullptr;
14660 : 0 : }
14661 : :
14662 : 31 : return std::unique_ptr<AST::ClosureExprInnerTyped> (
14663 : 31 : new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
14664 : : std::move (params), locus, has_move,
14665 : 31 : std::move (outer_attrs)));
14666 : 31 : }
14667 : : else
14668 : : {
14669 : : // must be expr-only closure
14670 : :
14671 : : // parse expr, which is required
14672 : 40 : std::unique_ptr<AST::Expr> expr = parse_expr ();
14673 : 40 : if (expr == nullptr)
14674 : : {
14675 : 0 : Error error (tok->get_locus (),
14676 : : "failed to parse expression in closure");
14677 : 0 : add_error (std::move (error));
14678 : :
14679 : : // skip somewhere?
14680 : 0 : return nullptr;
14681 : 0 : }
14682 : :
14683 : 40 : return std::unique_ptr<AST::ClosureExprInner> (
14684 : 40 : new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
14685 : 40 : has_move, std::move (outer_attrs)));
14686 : 40 : }
14687 : 71 : }
14688 : :
14689 : : /* Parses a tuple index expression (pratt-parsed) from a 'float' token as a
14690 : : * result of lexer misidentification. */
14691 : : template <typename ManagedTokenSource>
14692 : : std::unique_ptr<AST::TupleIndexExpr>
14693 : 0 : Parser<ManagedTokenSource>::parse_tuple_index_expr_float (
14694 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
14695 : : AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
14696 : : {
14697 : : // only works on float literals
14698 : 0 : if (tok->get_id () != FLOAT_LITERAL)
14699 : 0 : return nullptr;
14700 : :
14701 : : // DEBUG:
14702 : 0 : rust_debug ("exact string form of float: '%s'", tok->get_str ().c_str ());
14703 : :
14704 : : // get float string and remove dot and initial 0
14705 : 0 : std::string index_str = tok->get_str ();
14706 : 0 : index_str.erase (index_str.begin ());
14707 : :
14708 : : // get int from string
14709 : 0 : int index = atoi (index_str.c_str ());
14710 : :
14711 : 0 : location_t locus = tuple_expr->get_locus ();
14712 : :
14713 : : return std::unique_ptr<AST::TupleIndexExpr> (
14714 : 0 : new AST::TupleIndexExpr (std::move (tuple_expr), index,
14715 : 0 : std::move (outer_attrs), locus));
14716 : 0 : }
14717 : :
14718 : : // Returns true if the next token is END, ELSE, or EOF;
14719 : : template <typename ManagedTokenSource>
14720 : : bool
14721 : 0 : Parser<ManagedTokenSource>::done_end_or_else ()
14722 : : {
14723 : 0 : const_TokenPtr t = lexer.peek_token ();
14724 : 0 : return (t->get_id () == RIGHT_CURLY || t->get_id () == ELSE
14725 : 0 : || t->get_id () == END_OF_FILE);
14726 : 0 : }
14727 : :
14728 : : // Returns true if the next token is END or EOF.
14729 : : template <typename ManagedTokenSource>
14730 : : bool
14731 : 0 : Parser<ManagedTokenSource>::done_end ()
14732 : : {
14733 : 0 : const_TokenPtr t = lexer.peek_token ();
14734 : 0 : return (t->get_id () == RIGHT_CURLY || t->get_id () == END_OF_FILE);
14735 : 0 : }
14736 : : } // namespace Rust
|