LCOV - code coverage report
Current view: top level - gcc/rust/parse - rust-parse-impl.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 70.2 % 5850 4106
Test Date: 2025-03-22 13:13:03 Functions: 52.2 % 556 290
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             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 with rust-parse.h
      23                 :             :  * This is also the reason why there are no include guards. */
      24                 :             : 
      25                 :             : #include "rust-common.h"
      26                 :             : #include "rust-expr.h"
      27                 :             : #include "rust-item.h"
      28                 :             : #include "rust-common.h"
      29                 :             : #include "rust-token.h"
      30                 :             : #define INCLUDE_ALGORITHM
      31                 :             : #include "rust-diagnostics.h"
      32                 :             : #include "rust-dir-owner.h"
      33                 :             : #include "rust-attribute-values.h"
      34                 :             : #include "rust-keyword-values.h"
      35                 :             : #include "rust-session-manager.h"
      36                 :             : 
      37                 :             : #include "optional.h"
      38                 :             : 
      39                 :             : namespace Rust {
      40                 :             : // Left binding powers of operations.
      41                 :             : enum binding_powers
      42                 :             : {
      43                 :             :   // Highest priority
      44                 :             :   LBP_HIGHEST = 100,
      45                 :             : 
      46                 :             :   LBP_PATH = 95,
      47                 :             : 
      48                 :             :   LBP_METHOD_CALL = 90,
      49                 :             : 
      50                 :             :   LBP_FIELD_EXPR = 85,
      51                 :             : 
      52                 :             :   LBP_FUNCTION_CALL = 80,
      53                 :             :   LBP_ARRAY_REF = LBP_FUNCTION_CALL,
      54                 :             : 
      55                 :             :   LBP_QUESTION_MARK = 75, // unary postfix - counts as left
      56                 :             : 
      57                 :             :   LBP_UNARY_PLUS = 70,              // Used only when the null denotation is +
      58                 :             :   LBP_UNARY_MINUS = LBP_UNARY_PLUS, // Used only when the null denotation is -
      59                 :             :   LBP_UNARY_ASTERISK = LBP_UNARY_PLUS, // deref operator - unary prefix
      60                 :             :   LBP_UNARY_EXCLAM = LBP_UNARY_PLUS,
      61                 :             :   LBP_UNARY_AMP = LBP_UNARY_PLUS,
      62                 :             :   LBP_UNARY_AMP_MUT = LBP_UNARY_PLUS,
      63                 :             : 
      64                 :             :   LBP_AS = 65,
      65                 :             : 
      66                 :             :   LBP_MUL = 60,
      67                 :             :   LBP_DIV = LBP_MUL,
      68                 :             :   LBP_MOD = LBP_MUL,
      69                 :             : 
      70                 :             :   LBP_PLUS = 55,
      71                 :             :   LBP_MINUS = LBP_PLUS,
      72                 :             : 
      73                 :             :   LBP_L_SHIFT = 50,
      74                 :             :   LBP_R_SHIFT = LBP_L_SHIFT,
      75                 :             : 
      76                 :             :   LBP_AMP = 45,
      77                 :             : 
      78                 :             :   LBP_CARET = 40,
      79                 :             : 
      80                 :             :   LBP_PIPE = 35,
      81                 :             : 
      82                 :             :   LBP_EQUAL = 30,
      83                 :             :   LBP_NOT_EQUAL = LBP_EQUAL,
      84                 :             :   LBP_SMALLER_THAN = LBP_EQUAL,
      85                 :             :   LBP_SMALLER_EQUAL = LBP_EQUAL,
      86                 :             :   LBP_GREATER_THAN = LBP_EQUAL,
      87                 :             :   LBP_GREATER_EQUAL = LBP_EQUAL,
      88                 :             : 
      89                 :             :   LBP_LOGICAL_AND = 25,
      90                 :             : 
      91                 :             :   LBP_LOGICAL_OR = 20,
      92                 :             : 
      93                 :             :   LBP_DOT_DOT = 15,
      94                 :             :   LBP_DOT_DOT_EQ = LBP_DOT_DOT,
      95                 :             : 
      96                 :             :   // TODO: note all these assig operators are RIGHT associative!
      97                 :             :   LBP_ASSIG = 10,
      98                 :             :   LBP_PLUS_ASSIG = LBP_ASSIG,
      99                 :             :   LBP_MINUS_ASSIG = LBP_ASSIG,
     100                 :             :   LBP_MULT_ASSIG = LBP_ASSIG,
     101                 :             :   LBP_DIV_ASSIG = LBP_ASSIG,
     102                 :             :   LBP_MOD_ASSIG = LBP_ASSIG,
     103                 :             :   LBP_AMP_ASSIG = LBP_ASSIG,
     104                 :             :   LBP_PIPE_ASSIG = LBP_ASSIG,
     105                 :             :   LBP_CARET_ASSIG = LBP_ASSIG,
     106                 :             :   LBP_L_SHIFT_ASSIG = LBP_ASSIG,
     107                 :             :   LBP_R_SHIFT_ASSIG = LBP_ASSIG,
     108                 :             : 
     109                 :             :   // return, break, and closures as lowest priority?
     110                 :             :   LBP_RETURN = 5,
     111                 :             :   LBP_BREAK = LBP_RETURN,
     112                 :             :   LBP_CLOSURE = LBP_RETURN, // unary prefix operators
     113                 :             : 
     114                 :             : #if 0
     115                 :             :   // rust precedences
     116                 :             :   // used for closures
     117                 :             :   PREC_CLOSURE = -40,
     118                 :             :   // used for break, continue, return, and yield
     119                 :             :   PREC_JUMP = -30,
     120                 :             :   // used for range (although weird comment in rustc about this)
     121                 :             :   PREC_RANGE = -10,
     122                 :             :   // used for binary operators mentioned below - also cast, colon (type),
     123                 :             :   // assign, assign_op
     124                 :             :   PREC_BINOP = FROM_ASSOC_OP,
     125                 :             :   // used for box, address_of, let, unary (again, weird comment on let)
     126                 :             :   PREC_PREFIX = 50,
     127                 :             :   // used for await, call, method call, field, index, try,
     128                 :             :   // inline asm, macro invocation
     129                 :             :   PREC_POSTFIX = 60,
     130                 :             :   // used for array, repeat, tuple, literal, path, paren, if,
     131                 :             :   // while, for, 'loop', match, block, try block, async, struct
     132                 :             :   PREC_PAREN = 99,
     133                 :             :   PREC_FORCE_PAREN = 100,
     134                 :             : #endif
     135                 :             : 
     136                 :             :   // lowest priority
     137                 :             :   LBP_LOWEST = 0
     138                 :             : };
     139                 :             : 
     140                 :             : /* Returns whether the token can start a type (i.e. there is a valid type
     141                 :             :  * beginning with the token). */
     142                 :             : inline bool
     143                 :         384 : can_tok_start_type (TokenId id)
     144                 :             : {
     145                 :         384 :   switch (id)
     146                 :             :     {
     147                 :             :     case EXCLAM:
     148                 :             :     case LEFT_SQUARE:
     149                 :             :     case LEFT_ANGLE:
     150                 :             :     case UNDERSCORE:
     151                 :             :     case ASTERISK:
     152                 :             :     case AMP:
     153                 :             :     case LIFETIME:
     154                 :             :     case IDENTIFIER:
     155                 :             :     case SUPER:
     156                 :             :     case SELF:
     157                 :             :     case SELF_ALIAS:
     158                 :             :     case CRATE:
     159                 :             :     case DOLLAR_SIGN:
     160                 :             :     case SCOPE_RESOLUTION:
     161                 :             :     case LEFT_PAREN:
     162                 :             :     case FOR:
     163                 :             :     case ASYNC:
     164                 :             :     case CONST:
     165                 :             :     case UNSAFE:
     166                 :             :     case EXTERN_KW:
     167                 :             :     case FN_KW:
     168                 :             :     case IMPL:
     169                 :             :     case DYN:
     170                 :             :     case QUESTION_MARK:
     171                 :             :       return true;
     172                 :         181 :     default:
     173                 :         181 :       return false;
     174                 :             :     }
     175                 :             : }
     176                 :             : 
     177                 :             : /* Returns whether the token id is (or is likely to be) a right angle bracket.
     178                 :             :  * i.e. '>', '>>', '>=' and '>>=' tokens. */
     179                 :             : inline bool
     180                 :       15116 : is_right_angle_tok (TokenId id)
     181                 :             : {
     182                 :       15116 :   switch (id)
     183                 :             :     {
     184                 :             :     case RIGHT_ANGLE:
     185                 :             :     case RIGHT_SHIFT:
     186                 :             :     case GREATER_OR_EQUAL:
     187                 :             :     case RIGHT_SHIFT_EQ:
     188                 :             :       return true;
     189                 :        9201 :     default:
     190                 :        3665 :       return false;
     191                 :             :     }
     192                 :             : }
     193                 :             : 
     194                 :             : /* HACK-y special handling for skipping a right angle token at the end of
     195                 :             :  * generic arguments.
     196                 :             :  * Currently, this replaces the "current token" with one that is identical
     197                 :             :  * except has the leading '>' removed (e.g. '>>' becomes '>'). This is bad
     198                 :             :  * for several reasons - it modifies the token stream to something that
     199                 :             :  * actually doesn't make syntactic sense, it may not worked if the token
     200                 :             :  * has already been skipped, etc. It was done because it would not
     201                 :             :  * actually require inserting new items into the token stream (which I
     202                 :             :  * thought would take more work to not mess up) and because I wasn't sure
     203                 :             :  * if the "already seen right angle" flag in the parser would work
     204                 :             :  * correctly.
     205                 :             :  * Those two other approaches listed are in my opinion actually better
     206                 :             :  * long-term - insertion is probably best as it reflects syntactically
     207                 :             :  * what occurs. On the other hand, I need to do a code audit to make sure
     208                 :             :  * that insertion doesn't mess anything up. So that's a FIXME. */
     209                 :             : template <typename ManagedTokenSource>
     210                 :             : bool
     211                 :        5959 : Parser<ManagedTokenSource>::skip_generics_right_angle ()
     212                 :             : {
     213                 :             :   /* OK, new great idea. Have a lexer method called
     214                 :             :    * "split_current_token(TokenType newLeft, TokenType newRight)", which is
     215                 :             :    * called here with whatever arguments are appropriate. That lexer method
     216                 :             :    * handles "replacing" the current token with the "newLeft" and "inserting"
     217                 :             :    * the next token with the "newRight" (and creating a location, etc. for it)
     218                 :             :    */
     219                 :             : 
     220                 :             :   /* HACK: special handling for right shift '>>', greater or equal '>=', and
     221                 :             :    * right shift assig */
     222                 :             :   // '>>='
     223                 :        5959 :   const_TokenPtr tok = lexer.peek_token ();
     224                 :        5959 :   switch (tok->get_id ())
     225                 :             :     {
     226                 :        5546 :     case RIGHT_ANGLE:
     227                 :             :       // this is good - skip token
     228                 :        5546 :       lexer.skip_token ();
     229                 :        5546 :       return true;
     230                 :         411 :       case RIGHT_SHIFT: {
     231                 :             :         // new implementation that should be better
     232                 :         411 :         lexer.split_current_token (RIGHT_ANGLE, RIGHT_ANGLE);
     233                 :         411 :         lexer.skip_token ();
     234                 :         411 :         return true;
     235                 :             :       }
     236                 :           0 :       case GREATER_OR_EQUAL: {
     237                 :             :         // new implementation that should be better
     238                 :           0 :         lexer.split_current_token (RIGHT_ANGLE, EQUAL);
     239                 :           0 :         lexer.skip_token ();
     240                 :           0 :         return true;
     241                 :             :       }
     242                 :           0 :       case RIGHT_SHIFT_EQ: {
     243                 :             :         // new implementation that should be better
     244                 :           0 :         lexer.split_current_token (RIGHT_ANGLE, GREATER_OR_EQUAL);
     245                 :           0 :         lexer.skip_token ();
     246                 :           0 :         return true;
     247                 :             :       }
     248                 :           2 :     default:
     249                 :           2 :       add_error (Error (tok->get_locus (),
     250                 :             :                         "expected %<>%> at end of generic argument - found %qs",
     251                 :             :                         tok->get_token_description ()));
     252                 :           2 :       return false;
     253                 :             :     }
     254                 :        5959 : }
     255                 :             : 
     256                 :             : /* Gets left binding power for specified token.
     257                 :             :  * Not suitable for use at the moment or possibly ever because binding power
     258                 :             :  * cannot be purely determined from operator token with Rust grammar - e.g.
     259                 :             :  * method call and field access have
     260                 :             :  * different left binding powers but the same operator token. */
     261                 :             : template <typename ManagedTokenSource>
     262                 :             : int
     263                 :       70043 : Parser<ManagedTokenSource>::left_binding_power (const_TokenPtr token)
     264                 :             : {
     265                 :             :   // HACK: called with "peek_token()", so lookahead is "peek_token(1)"
     266                 :       70043 :   switch (token->get_id ())
     267                 :             :     {
     268                 :             :       /* TODO: issue here - distinguish between method calls and field access
     269                 :             :        * somehow? Also would have to distinguish between paths and function
     270                 :             :        * calls (:: operator), maybe more stuff. */
     271                 :             :       /* Current plan for tackling LBP - don't do it based on token, use
     272                 :             :        * lookahead. Or alternatively, only use Pratt parsing for OperatorExpr
     273                 :             :        * and handle other expressions without it. rustc only considers
     274                 :             :        * arithmetic, logical/relational, 'as',
     275                 :             :        * '?=', ranges, colons, and assignment to have operator precedence and
     276                 :             :        * associativity rules applicable. It then has
     277                 :             :        * a separate "ExprPrecedence" that also includes binary operators. */
     278                 :             : 
     279                 :             :       // TODO: handle operator overloading - have a function replace the
     280                 :             :       // operator?
     281                 :             : 
     282                 :             :       /*case DOT:
     283                 :             :           return LBP_DOT;*/
     284                 :             : 
     285                 :           0 :     case SCOPE_RESOLUTION:
     286                 :           0 :       rust_debug (
     287                 :             :         "possible error - looked up LBP of scope resolution operator. should "
     288                 :             :         "be handled elsewhere.");
     289                 :           0 :       return LBP_PATH;
     290                 :             : 
     291                 :             :     /* Resolved by lookahead HACK that should work with current code. If next
     292                 :             :      * token is identifier and token after that isn't parenthesised expression
     293                 :             :      * list, it is a field reference. */
     294                 :        3627 :     case DOT:
     295                 :        7254 :       if (lexer.peek_token (1)->get_id () == IDENTIFIER
     296                 :        6393 :           && lexer.peek_token (2)->get_id () != LEFT_PAREN)
     297                 :             :         {
     298                 :             :           return LBP_FIELD_EXPR;
     299                 :             :         }
     300                 :             :       return LBP_METHOD_CALL;
     301                 :             : 
     302                 :             :     case LEFT_PAREN:
     303                 :             :       return LBP_FUNCTION_CALL;
     304                 :             : 
     305                 :             :     case LEFT_SQUARE:
     306                 :             :       return LBP_ARRAY_REF;
     307                 :             : 
     308                 :             :     // postfix question mark (i.e. error propagation expression)
     309                 :           0 :     case QUESTION_MARK:
     310                 :           0 :       return LBP_QUESTION_MARK;
     311                 :             : 
     312                 :        3770 :     case AS:
     313                 :        3770 :       return LBP_AS;
     314                 :             : 
     315                 :             :     case ASTERISK:
     316                 :             :       return LBP_MUL;
     317                 :             :     case DIV:
     318                 :             :       return LBP_DIV;
     319                 :             :     case PERCENT:
     320                 :             :       return LBP_MOD;
     321                 :             : 
     322                 :             :     case PLUS:
     323                 :             :       return LBP_PLUS;
     324                 :             :     case MINUS:
     325                 :             :       return LBP_MINUS;
     326                 :             : 
     327                 :             :     case LEFT_SHIFT:
     328                 :             :       return LBP_L_SHIFT;
     329                 :             :     case RIGHT_SHIFT:
     330                 :             :       return LBP_R_SHIFT;
     331                 :             : 
     332                 :             :     // binary & operator
     333                 :          40 :     case AMP:
     334                 :          40 :       return LBP_AMP;
     335                 :             : 
     336                 :             :     // binary ^ operator
     337                 :          14 :     case CARET:
     338                 :          14 :       return LBP_CARET;
     339                 :             : 
     340                 :             :     // binary | operator
     341                 :          26 :     case PIPE:
     342                 :          26 :       return LBP_PIPE;
     343                 :             : 
     344                 :             :     case EQUAL_EQUAL:
     345                 :             :       return LBP_EQUAL;
     346                 :             :     case NOT_EQUAL:
     347                 :             :       return LBP_NOT_EQUAL;
     348                 :             :     case RIGHT_ANGLE:
     349                 :             :       return LBP_GREATER_THAN;
     350                 :             :     case GREATER_OR_EQUAL:
     351                 :             :       return LBP_GREATER_EQUAL;
     352                 :             :     case LEFT_ANGLE:
     353                 :             :       return LBP_SMALLER_THAN;
     354                 :             :     case LESS_OR_EQUAL:
     355                 :             :       return LBP_SMALLER_EQUAL;
     356                 :             : 
     357                 :         616 :     case LOGICAL_AND:
     358                 :         616 :       return LBP_LOGICAL_AND;
     359                 :             : 
     360                 :          91 :     case OR:
     361                 :          91 :       return LBP_LOGICAL_OR;
     362                 :             : 
     363                 :             :     case DOT_DOT:
     364                 :             :       return LBP_DOT_DOT;
     365                 :             : 
     366                 :             :     case DOT_DOT_EQ:
     367                 :             :       return LBP_DOT_DOT_EQ;
     368                 :             : 
     369                 :             :     case EQUAL:
     370                 :             :       return LBP_ASSIG;
     371                 :             :     case PLUS_EQ:
     372                 :             :       return LBP_PLUS_ASSIG;
     373                 :             :     case MINUS_EQ:
     374                 :             :       return LBP_MINUS_ASSIG;
     375                 :             :     case ASTERISK_EQ:
     376                 :             :       return LBP_MULT_ASSIG;
     377                 :             :     case DIV_EQ:
     378                 :             :       return LBP_DIV_ASSIG;
     379                 :             :     case PERCENT_EQ:
     380                 :             :       return LBP_MOD_ASSIG;
     381                 :             :     case AMP_EQ:
     382                 :             :       return LBP_AMP_ASSIG;
     383                 :             :     case PIPE_EQ:
     384                 :             :       return LBP_PIPE_ASSIG;
     385                 :             :     case CARET_EQ:
     386                 :             :       return LBP_CARET_ASSIG;
     387                 :             :     case LEFT_SHIFT_EQ:
     388                 :             :       return LBP_L_SHIFT_ASSIG;
     389                 :             :     case RIGHT_SHIFT_EQ:
     390                 :             :       return LBP_R_SHIFT_ASSIG;
     391                 :             : 
     392                 :             :     /* HACK: float literal due to lexer misidentifying a dot then an integer as
     393                 :             :      * a float */
     394                 :             :     case FLOAT_LITERAL:
     395                 :             :       return LBP_FIELD_EXPR;
     396                 :             :       // field expr is same as tuple expr in precedence, i imagine
     397                 :             :       // TODO: is this needed anymore? lexer shouldn't do that anymore
     398                 :             : 
     399                 :             :     // anything that can't appear in an infix position is given lowest priority
     400                 :       53157 :     default:
     401                 :       53157 :       return LBP_LOWEST;
     402                 :             :     }
     403                 :             : }
     404                 :             : 
     405                 :             : // Returns true when current token is EOF.
     406                 :             : template <typename ManagedTokenSource>
     407                 :             : bool
     408                 :             : Parser<ManagedTokenSource>::done_end_of_file ()
     409                 :             : {
     410                 :             :   return lexer.peek_token ()->get_id () == END_OF_FILE;
     411                 :             : }
     412                 :             : 
     413                 :             : // Parses a sequence of items within a module or the implicit top-level module
     414                 :             : // in a crate
     415                 :             : template <typename ManagedTokenSource>
     416                 :             : std::vector<std::unique_ptr<AST::Item>>
     417                 :        4663 : Parser<ManagedTokenSource>::parse_items ()
     418                 :             : {
     419                 :        4663 :   std::vector<std::unique_ptr<AST::Item>> items;
     420                 :             : 
     421                 :        4663 :   const_TokenPtr t = lexer.peek_token ();
     422                 :       20484 :   while (t->get_id () != END_OF_FILE)
     423                 :             :     {
     424                 :       15897 :       std::unique_ptr<AST::Item> item = parse_item (false);
     425                 :       15897 :       if (item == nullptr)
     426                 :             :         {
     427                 :          76 :           Error error (lexer.peek_token ()->get_locus (),
     428                 :             :                        "failed to parse item in crate");
     429                 :          76 :           add_error (std::move (error));
     430                 :             : 
     431                 :             :           // TODO: should all items be cleared?
     432                 :          76 :           items = std::vector<std::unique_ptr<AST::Item>> ();
     433                 :             :           break;
     434                 :          76 :         }
     435                 :             : 
     436                 :       15821 :       items.push_back (std::move (item));
     437                 :             : 
     438                 :       15821 :       t = lexer.peek_token ();
     439                 :             :     }
     440                 :             : 
     441                 :        4663 :   return items;
     442                 :        4663 : }
     443                 :             : 
     444                 :             : // Parses a crate (compilation unit) - entry point
     445                 :             : template <typename ManagedTokenSource>
     446                 :             : std::unique_ptr<AST::Crate>
     447                 :        4601 : Parser<ManagedTokenSource>::parse_crate ()
     448                 :             : {
     449                 :             :   // parse inner attributes
     450                 :        4601 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
     451                 :             : 
     452                 :             :   // parse items
     453                 :        4601 :   std::vector<std::unique_ptr<AST::Item>> items = parse_items ();
     454                 :             : 
     455                 :             :   // emit all errors
     456                 :        4918 :   for (const auto &error : error_table)
     457                 :         317 :     error.emit ();
     458                 :             : 
     459                 :             :   return std::unique_ptr<AST::Crate> (
     460                 :        4601 :     new AST::Crate (std::move (items), std::move (inner_attrs)));
     461                 :        4601 : }
     462                 :             : 
     463                 :             : // Parse a contiguous block of inner attributes.
     464                 :             : template <typename ManagedTokenSource>
     465                 :             : AST::AttrVec
     466                 :       38031 : Parser<ManagedTokenSource>::parse_inner_attributes ()
     467                 :             : {
     468                 :       38031 :   AST::AttrVec inner_attributes;
     469                 :             : 
     470                 :             :   // only try to parse it if it starts with "#!" not only "#"
     471                 :       34089 :   while ((lexer.peek_token ()->get_id () == HASH
     472                 :       10303 :           && lexer.peek_token (1)->get_id () == EXCLAM)
     473                 :      118297 :          || lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
     474                 :             :     {
     475                 :         822 :       AST::Attribute inner_attr = parse_inner_attribute ();
     476                 :             : 
     477                 :             :       /* Ensure only valid inner attributes are added to the inner_attributes
     478                 :             :        * list */
     479                 :         822 :       if (!inner_attr.is_empty ())
     480                 :             :         {
     481                 :         822 :           inner_attributes.push_back (std::move (inner_attr));
     482                 :             :         }
     483                 :             :       else
     484                 :             :         {
     485                 :             :           /* If no more valid inner attributes, break out of loop (only
     486                 :             :            * contiguous inner attributes parsed). */
     487                 :             :           break;
     488                 :             :         }
     489                 :             :     }
     490                 :             : 
     491                 :             :   inner_attributes.shrink_to_fit ();
     492                 :       38031 :   return inner_attributes;
     493                 :             : }
     494                 :             : 
     495                 :             : // Parse a inner or outer doc comment into an doc attribute
     496                 :             : template <typename ManagedTokenSource>
     497                 :             : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
     498                 :         658 : Parser<ManagedTokenSource>::parse_doc_comment ()
     499                 :             : {
     500                 :         658 :   const_TokenPtr token = lexer.peek_token ();
     501                 :         658 :   location_t locus = token->get_locus ();
     502                 :         658 :   AST::SimplePathSegment segment (Values::Attributes::DOC, locus);
     503                 :         658 :   std::vector<AST::SimplePathSegment> segments;
     504                 :         658 :   segments.push_back (std::move (segment));
     505                 :         658 :   AST::SimplePath attr_path (std::move (segments), false, locus);
     506                 :         658 :   AST::LiteralExpr lit_expr (token->get_str (), AST::Literal::STRING,
     507                 :             :                              PrimitiveCoreType::CORETYPE_STR, {}, locus);
     508                 :         658 :   std::unique_ptr<AST::AttrInput> attr_input (
     509                 :         658 :     new AST::AttrInputLiteral (std::move (lit_expr)));
     510                 :         658 :   lexer.skip_token ();
     511                 :        1316 :   return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
     512                 :         658 : }
     513                 :             : 
     514                 :             : // Parse a single inner attribute.
     515                 :             : template <typename ManagedTokenSource>
     516                 :             : AST::Attribute
     517                 :         822 : Parser<ManagedTokenSource>::parse_inner_attribute ()
     518                 :             : {
     519                 :        1644 :   if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
     520                 :             :     {
     521                 :         140 :       auto values = parse_doc_comment ();
     522                 :         140 :       auto path = std::move (std::get<0> (values));
     523                 :         140 :       auto input = std::move (std::get<1> (values));
     524                 :         140 :       auto loc = std::get<2> (values);
     525                 :         140 :       return AST::Attribute (std::move (path), std::move (input), loc, true);
     526                 :         280 :     }
     527                 :             : 
     528                 :        1364 :   if (lexer.peek_token ()->get_id () != HASH)
     529                 :             :     {
     530                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
     531                 :             :                    "BUG: token %<#%> is missing, but %<parse_inner_attribute%> "
     532                 :             :                    "was invoked");
     533                 :           0 :       add_error (std::move (error));
     534                 :             : 
     535                 :           0 :       return AST::Attribute::create_empty ();
     536                 :           0 :     }
     537                 :         682 :   lexer.skip_token ();
     538                 :             : 
     539                 :        1364 :   if (lexer.peek_token ()->get_id () != EXCLAM)
     540                 :             :     {
     541                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
     542                 :             :                    "expected %<!%> or %<[%> for inner attribute");
     543                 :           0 :       add_error (std::move (error));
     544                 :             : 
     545                 :           0 :       return AST::Attribute::create_empty ();
     546                 :           0 :     }
     547                 :         682 :   lexer.skip_token ();
     548                 :             : 
     549                 :         682 :   if (!skip_token (LEFT_SQUARE))
     550                 :           0 :     return AST::Attribute::create_empty ();
     551                 :             : 
     552                 :         682 :   auto values = parse_attribute_body ();
     553                 :             : 
     554                 :         682 :   auto path = std::move (std::get<0> (values));
     555                 :         682 :   auto input = std::move (std::get<1> (values));
     556                 :             :   auto loc = std::get<2> (values);
     557                 :         682 :   auto actual_attribute
     558                 :         682 :     = AST::Attribute (std::move (path), std::move (input), loc, true);
     559                 :             : 
     560                 :         682 :   if (!skip_token (RIGHT_SQUARE))
     561                 :           0 :     return AST::Attribute::create_empty ();
     562                 :             : 
     563                 :         682 :   return actual_attribute;
     564                 :        1364 : }
     565                 :             : 
     566                 :             : // Parses the body of an attribute (inner or outer).
     567                 :             : template <typename ManagedTokenSource>
     568                 :             : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
     569                 :        4452 : Parser<ManagedTokenSource>::parse_attribute_body ()
     570                 :             : {
     571                 :        4452 :   location_t locus = lexer.peek_token ()->get_locus ();
     572                 :             : 
     573                 :        4452 :   AST::SimplePath attr_path = parse_simple_path ();
     574                 :             :   // ensure path is valid to parse attribute input
     575                 :        4452 :   if (attr_path.is_empty ())
     576                 :             :     {
     577                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
     578                 :             :                    "empty simple path in attribute");
     579                 :           0 :       add_error (std::move (error));
     580                 :             : 
     581                 :             :       // Skip past potential further info in attribute (i.e. attr_input)
     582                 :           0 :       skip_after_end_attribute ();
     583                 :           0 :       return std::make_tuple (std::move (attr_path), nullptr, UNDEF_LOCATION);
     584                 :           0 :     }
     585                 :             : 
     586                 :        4452 :   std::unique_ptr<AST::AttrInput> attr_input = parse_attr_input ();
     587                 :             :   // AttrInput is allowed to be null, so no checks here
     588                 :             : 
     589                 :        4452 :   return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
     590                 :        4452 : }
     591                 :             : 
     592                 :             : /* Determines whether token is a valid simple path segment. This does not
     593                 :             :  * include scope resolution operators. */
     594                 :             : inline bool
     595                 :        8020 : is_simple_path_segment (TokenId id)
     596                 :             : {
     597                 :        8020 :   switch (id)
     598                 :             :     {
     599                 :             :     case IDENTIFIER:
     600                 :             :     case SUPER:
     601                 :             :     case SELF:
     602                 :             :     case CRATE:
     603                 :             :       return true;
     604                 :             :     case DOLLAR_SIGN:
     605                 :             :       // assume that dollar sign leads to $crate
     606                 :             :       return true;
     607                 :        2733 :     default:
     608                 :        2733 :       return false;
     609                 :             :     }
     610                 :             : }
     611                 :             : 
     612                 :             : // Parses a SimplePath AST node, if it exists. Does nothing otherwise.
     613                 :             : template <typename ManagedTokenSource>
     614                 :             : AST::SimplePath
     615                 :        5511 : Parser<ManagedTokenSource>::parse_simple_path ()
     616                 :             : {
     617                 :        5511 :   bool has_opening_scope_resolution = false;
     618                 :        5511 :   location_t locus = UNKNOWN_LOCATION;
     619                 :             : 
     620                 :             :   // don't parse anything if not a path upfront
     621                 :       11022 :   if (!is_simple_path_segment (lexer.peek_token ()->get_id ())
     622                 :        5797 :       && !is_simple_path_segment (lexer.peek_token (1)->get_id ()))
     623                 :         226 :     return AST::SimplePath::create_empty ();
     624                 :             : 
     625                 :             :   /* Checks for opening scope resolution (i.e. global scope fully-qualified
     626                 :             :    * path) */
     627                 :       10570 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
     628                 :             :     {
     629                 :           0 :       has_opening_scope_resolution = true;
     630                 :             : 
     631                 :           0 :       locus = lexer.peek_token ()->get_locus ();
     632                 :             : 
     633                 :           0 :       lexer.skip_token ();
     634                 :             :     }
     635                 :             : 
     636                 :             :   // Parse single required simple path segment
     637                 :        5285 :   AST::SimplePathSegment segment = parse_simple_path_segment ();
     638                 :             : 
     639                 :             :   // get location if not gotten already
     640                 :        5285 :   if (locus == UNKNOWN_LOCATION)
     641                 :        5285 :     locus = segment.get_locus ();
     642                 :             : 
     643                 :        5285 :   std::vector<AST::SimplePathSegment> segments;
     644                 :             : 
     645                 :             :   // Return empty vector if first, actually required segment is an error
     646                 :        5285 :   if (segment.is_error ())
     647                 :          60 :     return AST::SimplePath::create_empty ();
     648                 :             : 
     649                 :        5225 :   segments.push_back (std::move (segment));
     650                 :             : 
     651                 :             :   // Parse all other simple path segments
     652                 :       10980 :   while (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
     653                 :             :     {
     654                 :             :       // Skip scope resolution operator
     655                 :         288 :       lexer.skip_token ();
     656                 :             : 
     657                 :         288 :       AST::SimplePathSegment new_segment = parse_simple_path_segment ();
     658                 :             : 
     659                 :             :       // Return path as currently constructed if segment in error state.
     660                 :         288 :       if (new_segment.is_error ())
     661                 :             :         break;
     662                 :             : 
     663                 :         242 :       segments.push_back (std::move (new_segment));
     664                 :             :     }
     665                 :             : 
     666                 :             :   // DEBUG: check for any empty segments
     667                 :       10692 :   for (const auto &seg : segments)
     668                 :             :     {
     669                 :        5467 :       if (seg.is_error ())
     670                 :             :         {
     671                 :           0 :           rust_debug (
     672                 :             :             "when parsing simple path, somehow empty path segment was "
     673                 :             :             "not filtered out. Path begins with '%s'",
     674                 :             :             segments.at (0).as_string ().c_str ());
     675                 :             :         }
     676                 :             :     }
     677                 :             : 
     678                 :        5225 :   return AST::SimplePath (std::move (segments), has_opening_scope_resolution,
     679                 :        5225 :                           locus);
     680                 :             :   /* TODO: now that is_simple_path_segment exists, could probably start
     681                 :             :    * actually making errors upon parse failure of segments and whatever */
     682                 :        5285 : }
     683                 :             : 
     684                 :             : /* Parses a single SimplePathSegment (does not handle the scope resolution
     685                 :             :  * operators) */
     686                 :             : template <typename ManagedTokenSource>
     687                 :             : AST::SimplePathSegment
     688                 :        5573 : Parser<ManagedTokenSource>::parse_simple_path_segment ()
     689                 :             : {
     690                 :             :   using namespace Values;
     691                 :        5573 :   const_TokenPtr t = lexer.peek_token ();
     692                 :        5573 :   switch (t->get_id ())
     693                 :             :     {
     694                 :        5333 :     case IDENTIFIER:
     695                 :        5333 :       lexer.skip_token ();
     696                 :             : 
     697                 :        5333 :       return AST::SimplePathSegment (t->get_str (), t->get_locus ());
     698                 :         102 :     case SUPER:
     699                 :         102 :       lexer.skip_token ();
     700                 :             : 
     701                 :         102 :       return AST::SimplePathSegment (Keywords::SUPER, t->get_locus ());
     702                 :          28 :     case SELF:
     703                 :          28 :       lexer.skip_token ();
     704                 :             : 
     705                 :          28 :       return AST::SimplePathSegment (Keywords::SELF, t->get_locus ());
     706                 :           4 :     case CRATE:
     707                 :           4 :       lexer.skip_token ();
     708                 :             : 
     709                 :           4 :       return AST::SimplePathSegment (Keywords::CRATE, t->get_locus ());
     710                 :           0 :     case DOLLAR_SIGN:
     711                 :           0 :       if (lexer.peek_token (1)->get_id () == CRATE)
     712                 :             :         {
     713                 :           0 :           lexer.skip_token (1);
     714                 :             : 
     715                 :           0 :           return AST::SimplePathSegment ("$crate", t->get_locus ());
     716                 :             :         }
     717                 :             :       gcc_fallthrough ();
     718                 :             :     default:
     719                 :             :       // do nothing but inactivates warning from gcc when compiling
     720                 :             :       /* could put the rust_error_at thing here but fallthrough (from failing
     721                 :             :        * $crate condition) isn't completely obvious if it is. */
     722                 :             : 
     723                 :             :       // test prevent error
     724                 :         106 :       return AST::SimplePathSegment::create_error ();
     725                 :             :     }
     726                 :             :   rust_unreachable ();
     727                 :             :   /*rust_error_at(
     728                 :             :     t->get_locus(), "invalid token '%s' in simple path segment",
     729                 :             :     t->get_token_description());*/
     730                 :             :   // this is not necessarily an error, e.g. end of path
     731                 :             :   // return AST::SimplePathSegment::create_error();
     732                 :        5573 : }
     733                 :             : 
     734                 :             : // Parses a PathIdentSegment - an identifier segment of a non-SimplePath path.
     735                 :             : template <typename ManagedTokenSource>
     736                 :             : AST::PathIdentSegment
     737                 :       70506 : Parser<ManagedTokenSource>::parse_path_ident_segment ()
     738                 :             : {
     739                 :       70506 :   const_TokenPtr t = lexer.peek_token ();
     740                 :       70506 :   switch (t->get_id ())
     741                 :             :     {
     742                 :       65689 :     case IDENTIFIER:
     743                 :       65689 :       lexer.skip_token ();
     744                 :             : 
     745                 :       65689 :       return AST::PathIdentSegment (t->get_str (), t->get_locus ());
     746                 :          16 :     case SUPER:
     747                 :          16 :       lexer.skip_token ();
     748                 :             : 
     749                 :          16 :       return AST::PathIdentSegment (Values::Keywords::SUPER, t->get_locus ());
     750                 :         787 :     case SELF:
     751                 :         787 :       lexer.skip_token ();
     752                 :             : 
     753                 :         787 :       return AST::PathIdentSegment (Values::Keywords::SELF, t->get_locus ());
     754                 :        3797 :     case SELF_ALIAS:
     755                 :        3797 :       lexer.skip_token ();
     756                 :             : 
     757                 :        7594 :       return AST::PathIdentSegment (Values::Keywords::SELF_ALIAS,
     758                 :        7594 :                                     t->get_locus ());
     759                 :          38 :     case CRATE:
     760                 :          38 :       lexer.skip_token ();
     761                 :             : 
     762                 :          38 :       return AST::PathIdentSegment (Values::Keywords::CRATE, t->get_locus ());
     763                 :           4 :     case DOLLAR_SIGN:
     764                 :           8 :       if (lexer.peek_token (1)->get_id () == CRATE)
     765                 :             :         {
     766                 :           0 :           lexer.skip_token (1);
     767                 :             : 
     768                 :           0 :           return AST::PathIdentSegment ("$crate", t->get_locus ());
     769                 :             :         }
     770                 :             :       gcc_fallthrough ();
     771                 :             :     default:
     772                 :             :       /* do nothing but inactivates warning from gcc when compiling
     773                 :             :        * could put the error_at thing here but fallthrough (from failing $crate
     774                 :             :        * condition) isn't completely obvious if it is. */
     775                 :             : 
     776                 :             :       // test prevent error
     777                 :         179 :       return AST::PathIdentSegment::create_error ();
     778                 :             :     }
     779                 :             :   rust_unreachable ();
     780                 :             :   // not necessarily an error
     781                 :       70506 : }
     782                 :             : 
     783                 :             : // Parses an AttrInput AST node (polymorphic, as AttrInput is abstract)
     784                 :             : template <typename ManagedTokenSource>
     785                 :             : std::unique_ptr<AST::AttrInput>
     786                 :        4452 : Parser<ManagedTokenSource>::parse_attr_input ()
     787                 :             : {
     788                 :        4452 :   const_TokenPtr t = lexer.peek_token ();
     789                 :        4452 :   switch (t->get_id ())
     790                 :             :     {
     791                 :        1648 :     case LEFT_PAREN:
     792                 :             :     case LEFT_SQUARE:
     793                 :             :       case LEFT_CURLY: {
     794                 :             :         // must be a delimited token tree, so parse that
     795                 :        1648 :         std::unique_ptr<AST::AttrInput> input_tree (
     796                 :        1648 :           new AST::DelimTokenTree (parse_delim_token_tree ()));
     797                 :             : 
     798                 :             :         // TODO: potential checks on DelimTokenTree before returning
     799                 :             : 
     800                 :             :         return input_tree;
     801                 :             :       }
     802                 :        2223 :       case EQUAL: {
     803                 :             :         // = LiteralExpr
     804                 :        2223 :         lexer.skip_token ();
     805                 :             : 
     806                 :        2223 :         t = lexer.peek_token ();
     807                 :             : 
     808                 :             :         // attempt to parse macro
     809                 :             :         // TODO: macros may/may not be allowed in attributes
     810                 :             :         // this is needed for "#[doc = include_str!(...)]"
     811                 :        2223 :         if (is_simple_path_segment (t->get_id ()))
     812                 :             :           {
     813                 :           2 :             std::unique_ptr<AST::MacroInvocation> invoke
     814                 :           2 :               = parse_macro_invocation ({});
     815                 :             : 
     816                 :           2 :             if (!invoke)
     817                 :           0 :               return nullptr;
     818                 :             : 
     819                 :             :             return std::unique_ptr<AST::AttrInput> (
     820                 :           2 :               new AST::AttrInputMacro (std::move (invoke)));
     821                 :           2 :           }
     822                 :             : 
     823                 :             :         /* Ensure token is a "literal expression" (literally only a literal
     824                 :             :          * token of any type) */
     825                 :        2221 :         if (!t->is_literal ())
     826                 :             :           {
     827                 :           0 :             Error error (
     828                 :             :               t->get_locus (),
     829                 :             :               "unknown token %qs in attribute body - literal expected",
     830                 :             :               t->get_token_description ());
     831                 :           0 :             add_error (std::move (error));
     832                 :             : 
     833                 :           0 :             skip_after_end_attribute ();
     834                 :           0 :             return nullptr;
     835                 :           0 :           }
     836                 :             : 
     837                 :        2221 :         AST::Literal::LitType lit_type = AST::Literal::STRING;
     838                 :             :         // Crappy mapping of token type to literal type
     839                 :        2221 :         switch (t->get_id ())
     840                 :             :           {
     841                 :             :           case INT_LITERAL:
     842                 :             :             lit_type = AST::Literal::INT;
     843                 :             :             break;
     844                 :             :           case FLOAT_LITERAL:
     845                 :             :             lit_type = AST::Literal::FLOAT;
     846                 :             :             break;
     847                 :             :           case CHAR_LITERAL:
     848                 :             :             lit_type = AST::Literal::CHAR;
     849                 :             :             break;
     850                 :             :           case BYTE_CHAR_LITERAL:
     851                 :             :             lit_type = AST::Literal::BYTE;
     852                 :             :             break;
     853                 :             :           case BYTE_STRING_LITERAL:
     854                 :             :             lit_type = AST::Literal::BYTE_STRING;
     855                 :             :             break;
     856                 :             :           case RAW_STRING_LITERAL:
     857                 :             :             lit_type = AST::Literal::RAW_STRING;
     858                 :             :             break;
     859                 :             :           case STRING_LITERAL:
     860                 :             :           default:
     861                 :             :             lit_type = AST::Literal::STRING;
     862                 :             :             break; // TODO: raw string? don't eliminate it from lexer?
     863                 :             :           }
     864                 :             : 
     865                 :             :         // create actual LiteralExpr
     866                 :        4442 :         AST::LiteralExpr lit_expr (t->get_str (), lit_type, t->get_type_hint (),
     867                 :             :                                    {}, t->get_locus ());
     868                 :        2221 :         lexer.skip_token ();
     869                 :             : 
     870                 :        2221 :         std::unique_ptr<AST::AttrInput> attr_input_lit (
     871                 :        2221 :           new AST::AttrInputLiteral (std::move (lit_expr)));
     872                 :             : 
     873                 :             :         // do checks or whatever? none required, really
     874                 :             : 
     875                 :             :         // FIXME: shouldn't a skip token be required here?
     876                 :             : 
     877                 :        2221 :         return attr_input_lit;
     878                 :        2221 :       }
     879                 :             :       break;
     880                 :         581 :     case RIGHT_SQUARE:
     881                 :             :       // means AttrInput is missing, which is allowed
     882                 :         581 :       return nullptr;
     883                 :           0 :     default:
     884                 :           0 :       add_error (
     885                 :           0 :         Error (t->get_locus (),
     886                 :             :                "unknown token %qs in attribute body - attribute input or "
     887                 :             :                "none expected",
     888                 :             :                t->get_token_description ()));
     889                 :             : 
     890                 :           0 :       skip_after_end_attribute ();
     891                 :           0 :       return nullptr;
     892                 :             :     }
     893                 :             :   rust_unreachable ();
     894                 :             :   // TODO: find out how to stop gcc error on "no return value"
     895                 :        4452 : }
     896                 :             : 
     897                 :             : /* Returns true if the token id matches the delimiter type. Note that this only
     898                 :             :  * operates for END delimiter tokens. */
     899                 :             : inline bool
     900                 :       58608 : token_id_matches_delims (TokenId token_id, AST::DelimType delim_type)
     901                 :             : {
     902                 :       58608 :   return ((token_id == RIGHT_PAREN && delim_type == AST::PARENS)
     903                 :       41756 :           || (token_id == RIGHT_SQUARE && delim_type == AST::SQUARE)
     904                 :       99756 :           || (token_id == RIGHT_CURLY && delim_type == AST::CURLY));
     905                 :             : }
     906                 :             : 
     907                 :             : /* Returns true if the likely result of parsing the next few tokens is a path.
     908                 :             :  * Not guaranteed, though, especially in the case of syntax errors. */
     909                 :             : inline bool
     910                 :             : is_likely_path_next (TokenId next_token_id)
     911                 :             : {
     912                 :             :   switch (next_token_id)
     913                 :             :     {
     914                 :             :     case IDENTIFIER:
     915                 :             :     case SUPER:
     916                 :             :     case SELF:
     917                 :             :     case SELF_ALIAS:
     918                 :             :     case CRATE:
     919                 :             :     // maybe - maybe do extra check. But then requires another TokenId.
     920                 :             :     case DOLLAR_SIGN:
     921                 :             :     case SCOPE_RESOLUTION:
     922                 :             :       return true;
     923                 :             :     default:
     924                 :             :       return false;
     925                 :             :     }
     926                 :             : }
     927                 :             : 
     928                 :             : // Parses a delimited token tree
     929                 :             : template <typename ManagedTokenSource>
     930                 :             : AST::DelimTokenTree
     931                 :        9407 : Parser<ManagedTokenSource>::parse_delim_token_tree ()
     932                 :             : {
     933                 :        9407 :   const_TokenPtr t = lexer.peek_token ();
     934                 :        9407 :   lexer.skip_token ();
     935                 :        9407 :   location_t initial_loc = t->get_locus ();
     936                 :             : 
     937                 :             :   // save delim type to ensure it is reused later
     938                 :        9407 :   AST::DelimType delim_type = AST::PARENS;
     939                 :             : 
     940                 :             :   // Map tokens to DelimType
     941                 :        9407 :   switch (t->get_id ())
     942                 :             :     {
     943                 :             :     case LEFT_PAREN:
     944                 :             :       delim_type = AST::PARENS;
     945                 :             :       break;
     946                 :         280 :     case LEFT_SQUARE:
     947                 :         280 :       delim_type = AST::SQUARE;
     948                 :         280 :       break;
     949                 :        1908 :     case LEFT_CURLY:
     950                 :        1908 :       delim_type = AST::CURLY;
     951                 :        1908 :       break;
     952                 :           0 :     default:
     953                 :           0 :       add_error (Error (t->get_locus (),
     954                 :             :                         "unexpected token %qs - expecting delimiters (for a "
     955                 :             :                         "delimited token tree)",
     956                 :             :                         t->get_token_description ()));
     957                 :             : 
     958                 :           0 :       return AST::DelimTokenTree::create_empty ();
     959                 :             :     }
     960                 :             : 
     961                 :             :   // parse actual token tree vector - 0 or more
     962                 :        9407 :   std::vector<std::unique_ptr<AST::TokenTree>> token_trees_in_tree;
     963                 :        9407 :   auto delim_open
     964                 :        9407 :     = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
     965                 :        9407 :   token_trees_in_tree.push_back (std::move (delim_open));
     966                 :             : 
     967                 :             :   // repeat loop until finding the matching delimiter
     968                 :        9407 :   t = lexer.peek_token ();
     969                 :       40346 :   while (!token_id_matches_delims (t->get_id (), delim_type)
     970                 :       40346 :          && t->get_id () != END_OF_FILE)
     971                 :             :     {
     972                 :       30939 :       std::unique_ptr<AST::TokenTree> tok_tree = parse_token_tree ();
     973                 :             : 
     974                 :       30939 :       if (tok_tree == nullptr)
     975                 :             :         {
     976                 :             :           // TODO: is this error handling appropriate?
     977                 :           0 :           Error error (
     978                 :             :             t->get_locus (),
     979                 :             :             "failed to parse token tree in delimited token tree - found %qs",
     980                 :             :             t->get_token_description ());
     981                 :           0 :           add_error (std::move (error));
     982                 :             : 
     983                 :           0 :           return AST::DelimTokenTree::create_empty ();
     984                 :           0 :         }
     985                 :             : 
     986                 :       30939 :       token_trees_in_tree.push_back (std::move (tok_tree));
     987                 :             : 
     988                 :             :       // lexer.skip_token();
     989                 :       30939 :       t = lexer.peek_token ();
     990                 :             :     }
     991                 :        9407 :   auto delim_close
     992                 :        9407 :     = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
     993                 :        9407 :   token_trees_in_tree.push_back (std::move (delim_close));
     994                 :             : 
     995                 :        9407 :   AST::DelimTokenTree token_tree (delim_type, std::move (token_trees_in_tree),
     996                 :             :                                   initial_loc);
     997                 :             : 
     998                 :             :   // parse end delimiters
     999                 :        9407 :   t = lexer.peek_token ();
    1000                 :             : 
    1001                 :        9407 :   if (token_id_matches_delims (t->get_id (), delim_type))
    1002                 :             :     {
    1003                 :             :       // tokens match opening delimiter, so skip.
    1004                 :        9400 :       lexer.skip_token ();
    1005                 :             : 
    1006                 :             :       // DEBUG
    1007                 :       18800 :       rust_debug ("finished parsing new delim token tree - peeked token is now "
    1008                 :             :                   "'%s' while t is '%s'",
    1009                 :             :                   lexer.peek_token ()->get_token_description (),
    1010                 :             :                   t->get_token_description ());
    1011                 :             : 
    1012                 :        9400 :       return token_tree;
    1013                 :             :     }
    1014                 :             :   else
    1015                 :             :     {
    1016                 :             :       // tokens don't match opening delimiters, so produce error
    1017                 :           7 :       Error error (t->get_locus (),
    1018                 :             :                    "unexpected token %qs - expecting closing delimiter %qs "
    1019                 :             :                    "(for a delimited token tree)",
    1020                 :             :                    t->get_token_description (),
    1021                 :             :                    (delim_type == AST::PARENS
    1022                 :             :                       ? ")"
    1023                 :             :                       : (delim_type == AST::SQUARE ? "]" : "}")));
    1024                 :           7 :       add_error (std::move (error));
    1025                 :             : 
    1026                 :             :       /* return empty token tree despite possibly parsing valid token tree -
    1027                 :             :        * TODO is this a good idea? */
    1028                 :           7 :       return AST::DelimTokenTree::create_empty ();
    1029                 :           7 :     }
    1030                 :        9407 : }
    1031                 :             : 
    1032                 :             : // Parses an identifier/keyword as a Token
    1033                 :             : template <typename ManagedTokenSource>
    1034                 :             : std::unique_ptr<AST::Token>
    1035                 :         553 : Parser<ManagedTokenSource>::parse_identifier_or_keyword_token ()
    1036                 :             : {
    1037                 :         553 :   const_TokenPtr t = lexer.peek_token ();
    1038                 :             : 
    1039                 :         553 :   if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ()))
    1040                 :             :     {
    1041                 :         553 :       lexer.skip_token ();
    1042                 :         553 :       return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    1043                 :             :     }
    1044                 :             :   else
    1045                 :             :     {
    1046                 :           0 :       return nullptr;
    1047                 :             :     }
    1048                 :         553 : }
    1049                 :             : 
    1050                 :             : /* Parses a TokenTree syntactical production. This is either a delimited token
    1051                 :             :  * tree or a non-delimiter token. */
    1052                 :             : template <typename ManagedTokenSource>
    1053                 :             : std::unique_ptr<AST::TokenTree>
    1054                 :       34385 : Parser<ManagedTokenSource>::parse_token_tree ()
    1055                 :             : {
    1056                 :       34385 :   const_TokenPtr t = lexer.peek_token ();
    1057                 :             : 
    1058                 :       34385 :   switch (t->get_id ())
    1059                 :             :     {
    1060                 :        3174 :     case LEFT_PAREN:
    1061                 :             :     case LEFT_SQUARE:
    1062                 :             :     case LEFT_CURLY:
    1063                 :             :       // Parse delimited token tree
    1064                 :             :       // TODO: use move rather than copy constructor
    1065                 :        3174 :       return std::unique_ptr<AST::DelimTokenTree> (
    1066                 :        3174 :         new AST::DelimTokenTree (parse_delim_token_tree ()));
    1067                 :           2 :     case RIGHT_PAREN:
    1068                 :             :     case RIGHT_SQUARE:
    1069                 :             :     case RIGHT_CURLY:
    1070                 :             :       // error - should not be called when this a token
    1071                 :           2 :       add_error (
    1072                 :           2 :         Error (t->get_locus (),
    1073                 :             :                "unexpected closing delimiter %qs - token tree requires "
    1074                 :             :                "either paired delimiters or non-delimiter tokens",
    1075                 :             :                t->get_token_description ()));
    1076                 :             : 
    1077                 :           2 :       lexer.skip_token ();
    1078                 :           2 :       return nullptr;
    1079                 :       31209 :     default:
    1080                 :             :       // parse token itself as TokenTree
    1081                 :       31209 :       lexer.skip_token ();
    1082                 :       31209 :       return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    1083                 :             :     }
    1084                 :       34385 : }
    1085                 :             : 
    1086                 :             : template <typename ManagedTokenSource>
    1087                 :             : bool
    1088                 :        1420 : Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
    1089                 :             : {
    1090                 :        1420 :   auto macro_name = lexer.peek_token (2)->get_id ();
    1091                 :             : 
    1092                 :        1420 :   bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
    1093                 :             : 
    1094                 :        1420 :   return t->get_str () == Values::WeakKeywords::MACRO_RULES
    1095                 :        2280 :          && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
    1096                 :             : }
    1097                 :             : 
    1098                 :             : // Parses a single item
    1099                 :             : template <typename ManagedTokenSource>
    1100                 :             : std::unique_ptr<AST::Item>
    1101                 :       18880 : Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
    1102                 :             : {
    1103                 :             :   // has a "called_from_statement" parameter for better error message handling
    1104                 :             : 
    1105                 :             :   // parse outer attributes for item
    1106                 :       18880 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    1107                 :       18880 :   const_TokenPtr t = lexer.peek_token ();
    1108                 :             : 
    1109                 :       18880 :   switch (t->get_id ())
    1110                 :             :     {
    1111                 :           7 :     case END_OF_FILE:
    1112                 :             :       // not necessarily an error, unless we just read outer
    1113                 :             :       // attributes which needs to be attached
    1114                 :           7 :       if (!outer_attrs.empty ())
    1115                 :             :         {
    1116                 :           0 :           Rust::AST::Attribute attr = outer_attrs.back ();
    1117                 :           0 :           Error error (attr.get_locus (),
    1118                 :             :                        "expected item after outer attribute or doc comment");
    1119                 :           0 :           add_error (std::move (error));
    1120                 :           0 :         }
    1121                 :           7 :       return nullptr;
    1122                 :             : 
    1123                 :       17596 :     case ASYNC:
    1124                 :             :     case PUB:
    1125                 :             :     case MOD:
    1126                 :             :     case EXTERN_KW:
    1127                 :             :     case USE:
    1128                 :             :     case FN_KW:
    1129                 :             :     case TYPE:
    1130                 :             :     case STRUCT_KW:
    1131                 :             :     case ENUM_KW:
    1132                 :             :     case CONST:
    1133                 :             :     case STATIC_KW:
    1134                 :             :     case AUTO:
    1135                 :             :     case TRAIT:
    1136                 :             :     case IMPL:
    1137                 :             :     case MACRO:
    1138                 :             :     /* TODO: implement union keyword but not really because of
    1139                 :             :      * context-dependence crappy hack way to parse a union written below to
    1140                 :             :      * separate it from the good code. */
    1141                 :             :     // case UNION:
    1142                 :             :     case UNSAFE: // maybe - unsafe traits are a thing
    1143                 :             :       // if any of these (should be all possible VisItem prefixes), parse a
    1144                 :             :       // VisItem
    1145                 :       17596 :       return parse_vis_item (std::move (outer_attrs));
    1146                 :             :       break;
    1147                 :           6 :     case SUPER:
    1148                 :             :     case SELF:
    1149                 :             :     case CRATE:
    1150                 :             :     case DOLLAR_SIGN:
    1151                 :             :       // almost certainly macro invocation semi
    1152                 :          12 :       return parse_macro_invocation_semi (std::move (outer_attrs));
    1153                 :             :       break;
    1154                 :             :     // crappy hack to do union "keyword"
    1155                 :        1261 :     case IDENTIFIER:
    1156                 :             :       // TODO: ensure std::string and literal comparison works
    1157                 :        2404 :       if (t->get_str () == Values::WeakKeywords::UNION
    1158                 :        1336 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
    1159                 :             :         {
    1160                 :          75 :           return parse_vis_item (std::move (outer_attrs));
    1161                 :             :           // or should this go straight to parsing union?
    1162                 :             :         }
    1163                 :        2254 :       else if (t->get_str () == Values::WeakKeywords::DEFAULT
    1164                 :        1190 :                && lexer.peek_token (1)->get_id () != EXCLAM)
    1165                 :             :         {
    1166                 :           2 :           add_error (Error (t->get_locus (),
    1167                 :             :                             "%qs is only allowed on items within %qs blocks",
    1168                 :             :                             "default", "impl"));
    1169                 :           2 :           return nullptr;
    1170                 :             :         }
    1171                 :        2368 :       else if (is_macro_rules_def (t))
    1172                 :             :         {
    1173                 :             :           // macro_rules! macro item
    1174                 :         854 :           return parse_macro_rules_def (std::move (outer_attrs));
    1175                 :             :         }
    1176                 :         660 :       else if (lexer.peek_token (1)->get_id () == SCOPE_RESOLUTION
    1177                 :         658 :                || lexer.peek_token (1)->get_id () == EXCLAM)
    1178                 :             :         {
    1179                 :             :           /* path (probably) or macro invocation, so probably a macro invocation
    1180                 :             :            * semi */
    1181                 :         644 :           return parse_macro_invocation_semi (std::move (outer_attrs));
    1182                 :             :         }
    1183                 :             :       gcc_fallthrough ();
    1184                 :             :     default:
    1185                 :             :       // otherwise unrecognised
    1186                 :          36 :       add_error (Error (t->get_locus (),
    1187                 :             :                         "unrecognised token %qs for start of %s",
    1188                 :             :                         t->get_token_description (),
    1189                 :             :                         called_from_statement ? "statement" : "item"));
    1190                 :             : 
    1191                 :             :       // skip somewhere?
    1192                 :          18 :       return nullptr;
    1193                 :             :       break;
    1194                 :             :     }
    1195                 :       18880 : }
    1196                 :             : 
    1197                 :             : // Parses a contiguous block of outer attributes.
    1198                 :             : template <typename ManagedTokenSource>
    1199                 :             : AST::AttrVec
    1200                 :       75149 : Parser<ManagedTokenSource>::parse_outer_attributes ()
    1201                 :             : {
    1202                 :       75149 :   AST::AttrVec outer_attributes;
    1203                 :             : 
    1204                 :       74199 :   while (lexer.peek_token ()->get_id ()
    1205                 :             :            == HASH /* Can also be #!, which catches errors.  */
    1206                 :      155024 :          || lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT
    1207                 :      309530 :          || lexer.peek_token ()->get_id ()
    1208                 :             :               == INNER_DOC_COMMENT) /* For error handling.  */
    1209                 :             :     {
    1210                 :        4226 :       AST::Attribute outer_attr = parse_outer_attribute ();
    1211                 :             : 
    1212                 :             :       /* Ensure only valid outer attributes are added to the outer_attributes
    1213                 :             :        * list */
    1214                 :        4226 :       if (!outer_attr.is_empty ())
    1215                 :             :         {
    1216                 :        4215 :           outer_attributes.push_back (std::move (outer_attr));
    1217                 :             :         }
    1218                 :             :       else
    1219                 :             :         {
    1220                 :             :           /* If no more valid outer attributes, break out of loop (only
    1221                 :             :            * contiguous outer attributes parsed). */
    1222                 :             :           break;
    1223                 :             :         }
    1224                 :             :     }
    1225                 :             : 
    1226                 :             :   outer_attributes.shrink_to_fit ();
    1227                 :       75149 :   return outer_attributes;
    1228                 :             : 
    1229                 :             :   /* TODO: this shares basically all code with parse_inner_attributes except
    1230                 :             :    * function call - find way of making it more modular? function pointer? */
    1231                 :             : }
    1232                 :             : 
    1233                 :             : // Parse a single outer attribute.
    1234                 :             : template <typename ManagedTokenSource>
    1235                 :             : AST::Attribute
    1236                 :        4226 : Parser<ManagedTokenSource>::parse_outer_attribute ()
    1237                 :             : {
    1238                 :        8452 :   if (lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT)
    1239                 :             :     {
    1240                 :         518 :       auto values = parse_doc_comment ();
    1241                 :         518 :       auto path = std::move (std::get<0> (values));
    1242                 :         518 :       auto input = std::move (std::get<1> (values));
    1243                 :         518 :       auto loc = std::get<2> (values);
    1244                 :         518 :       return AST::Attribute (std::move (path), std::move (input), loc, false);
    1245                 :        1036 :     }
    1246                 :             : 
    1247                 :        7416 :   if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
    1248                 :             :     {
    1249                 :           4 :       Error error (
    1250                 :           4 :         lexer.peek_token ()->get_locus (), ErrorCode::E0753,
    1251                 :             :         "expected outer doc comment, inner doc (%<//!%> or %</*!%>) only "
    1252                 :             :         "allowed at start of item "
    1253                 :             :         "and before any outer attribute or doc (%<#[%>, %<///%> or %</**%>)");
    1254                 :           4 :       add_error (std::move (error));
    1255                 :           4 :       lexer.skip_token ();
    1256                 :           4 :       return AST::Attribute::create_empty ();
    1257                 :           4 :     }
    1258                 :             : 
    1259                 :             :   /* OuterAttribute -> '#' '[' Attr ']' */
    1260                 :             : 
    1261                 :        7408 :   if (lexer.peek_token ()->get_id () != HASH)
    1262                 :           0 :     return AST::Attribute::create_empty ();
    1263                 :             : 
    1264                 :        3704 :   lexer.skip_token ();
    1265                 :             : 
    1266                 :        3704 :   TokenId id = lexer.peek_token ()->get_id ();
    1267                 :        3704 :   if (id != LEFT_SQUARE)
    1268                 :             :     {
    1269                 :           0 :       if (id == EXCLAM)
    1270                 :             :         {
    1271                 :             :           // this is inner attribute syntax, so throw error
    1272                 :             :           // inner attributes were either already parsed or not allowed here.
    1273                 :           0 :           Error error (
    1274                 :           0 :             lexer.peek_token ()->get_locus (),
    1275                 :             :             "token %<!%> found, indicating inner attribute definition. Inner "
    1276                 :             :             "attributes are not possible at this location");
    1277                 :           0 :           add_error (std::move (error));
    1278                 :           0 :         }
    1279                 :           0 :       return AST::Attribute::create_empty ();
    1280                 :             :     }
    1281                 :             : 
    1282                 :        3704 :   lexer.skip_token ();
    1283                 :             : 
    1284                 :        3704 :   auto values = parse_attribute_body ();
    1285                 :        3704 :   auto path = std::move (std::get<0> (values));
    1286                 :        3704 :   auto input = std::move (std::get<1> (values));
    1287                 :        3704 :   auto loc = std::get<2> (values);
    1288                 :        3704 :   auto actual_attribute
    1289                 :        3704 :     = AST::Attribute (std::move (path), std::move (input), loc, false);
    1290                 :             : 
    1291                 :        7408 :   if (lexer.peek_token ()->get_id () != RIGHT_SQUARE)
    1292                 :           7 :     return AST::Attribute::create_empty ();
    1293                 :             : 
    1294                 :        3697 :   lexer.skip_token ();
    1295                 :             : 
    1296                 :        3697 :   return actual_attribute;
    1297                 :        7408 : }
    1298                 :             : 
    1299                 :             : // Parses a VisItem (item that can have non-default visibility).
    1300                 :             : template <typename ManagedTokenSource>
    1301                 :             : std::unique_ptr<AST::VisItem>
    1302                 :       18218 : Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
    1303                 :             : {
    1304                 :             :   // parse visibility, which may or may not exist
    1305                 :       18218 :   AST::Visibility vis = parse_visibility ();
    1306                 :             : 
    1307                 :             :   // select VisItem to create depending on keyword
    1308                 :       18218 :   const_TokenPtr t = lexer.peek_token ();
    1309                 :             : 
    1310                 :       18218 :   switch (t->get_id ())
    1311                 :             :     {
    1312                 :         777 :     case MOD:
    1313                 :         777 :       return parse_module (std::move (vis), std::move (outer_attrs));
    1314                 :        1180 :     case EXTERN_KW:
    1315                 :             :       // lookahead to resolve syntactical production
    1316                 :        1180 :       t = lexer.peek_token (1);
    1317                 :             : 
    1318                 :        1180 :       switch (t->get_id ())
    1319                 :             :         {
    1320                 :          30 :         case CRATE:
    1321                 :          30 :           return parse_extern_crate (std::move (vis), std::move (outer_attrs));
    1322                 :           0 :         case FN_KW: // extern function
    1323                 :           0 :           return parse_function (std::move (vis), std::move (outer_attrs));
    1324                 :           0 :         case LEFT_CURLY: // extern block
    1325                 :           0 :           return parse_extern_block (std::move (vis), std::move (outer_attrs));
    1326                 :        1150 :         case STRING_LITERAL: // for specifying extern ABI
    1327                 :             :           // could be extern block or extern function, so more lookahead
    1328                 :        1150 :           t = lexer.peek_token (2);
    1329                 :             : 
    1330                 :        1150 :           switch (t->get_id ())
    1331                 :             :             {
    1332                 :           3 :             case FN_KW:
    1333                 :           3 :               return parse_function (std::move (vis), std::move (outer_attrs));
    1334                 :        1147 :             case LEFT_CURLY:
    1335                 :        1147 :               return parse_extern_block (std::move (vis),
    1336                 :        1147 :                                          std::move (outer_attrs));
    1337                 :           0 :             default:
    1338                 :           0 :               add_error (
    1339                 :           0 :                 Error (t->get_locus (),
    1340                 :             :                        "unexpected token %qs in some sort of extern production",
    1341                 :             :                        t->get_token_description ()));
    1342                 :             : 
    1343                 :           0 :               lexer.skip_token (2); // TODO: is this right thing to do?
    1344                 :           0 :               return nullptr;
    1345                 :             :             }
    1346                 :           0 :         default:
    1347                 :           0 :           add_error (
    1348                 :           0 :             Error (t->get_locus (),
    1349                 :             :                    "unexpected token %qs in some sort of extern production",
    1350                 :             :                    t->get_token_description ()));
    1351                 :             : 
    1352                 :           0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    1353                 :           0 :           return nullptr;
    1354                 :             :         }
    1355                 :         200 :     case USE:
    1356                 :         200 :       return parse_use_decl (std::move (vis), std::move (outer_attrs));
    1357                 :        6136 :     case FN_KW:
    1358                 :        6136 :       return parse_function (std::move (vis), std::move (outer_attrs));
    1359                 :          58 :     case TYPE:
    1360                 :          58 :       return parse_type_alias (std::move (vis), std::move (outer_attrs));
    1361                 :        2316 :     case STRUCT_KW:
    1362                 :        2316 :       return parse_struct (std::move (vis), std::move (outer_attrs));
    1363                 :         319 :     case ENUM_KW:
    1364                 :         319 :       return parse_enum (std::move (vis), std::move (outer_attrs));
    1365                 :             :     // TODO: implement union keyword but not really because of
    1366                 :             :     // context-dependence case UNION: crappy hack to do union "keyword"
    1367                 :         114 :     case IDENTIFIER:
    1368                 :         228 :       if (t->get_str () == Values::WeakKeywords::UNION
    1369                 :         228 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
    1370                 :             :         {
    1371                 :         114 :           return parse_union (std::move (vis), std::move (outer_attrs));
    1372                 :             :           // or should item switch go straight to parsing union?
    1373                 :             :         }
    1374                 :             :       else
    1375                 :             :         {
    1376                 :             :           break;
    1377                 :             :         }
    1378                 :         575 :     case CONST:
    1379                 :             :       // lookahead to resolve syntactical production
    1380                 :         575 :       t = lexer.peek_token (1);
    1381                 :             : 
    1382                 :         575 :       switch (t->get_id ())
    1383                 :             :         {
    1384                 :         490 :         case IDENTIFIER:
    1385                 :             :         case UNDERSCORE:
    1386                 :         490 :           return parse_const_item (std::move (vis), std::move (outer_attrs));
    1387                 :           2 :         case ASYNC:
    1388                 :           2 :           return parse_async_item (std::move (vis), std::move (outer_attrs));
    1389                 :          83 :         case UNSAFE:
    1390                 :             :         case EXTERN_KW:
    1391                 :             :         case FN_KW:
    1392                 :          83 :           return parse_function (std::move (vis), std::move (outer_attrs));
    1393                 :           0 :         default:
    1394                 :           0 :           add_error (
    1395                 :           0 :             Error (t->get_locus (),
    1396                 :             :                    "unexpected token %qs in some sort of const production",
    1397                 :             :                    t->get_token_description ()));
    1398                 :             : 
    1399                 :           0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    1400                 :           0 :           return nullptr;
    1401                 :             :         }
    1402                 :             :     // for async functions
    1403                 :           4 :     case ASYNC:
    1404                 :           4 :       return parse_async_item (std::move (vis), std::move (outer_attrs));
    1405                 :             : 
    1406                 :          67 :     case STATIC_KW:
    1407                 :          67 :       return parse_static_item (std::move (vis), std::move (outer_attrs));
    1408                 :        2606 :     case AUTO:
    1409                 :             :     case TRAIT:
    1410                 :        2606 :       return parse_trait (std::move (vis), std::move (outer_attrs));
    1411                 :        3547 :     case IMPL:
    1412                 :        3547 :       return parse_impl (std::move (vis), std::move (outer_attrs));
    1413                 :         260 :     case UNSAFE: // unsafe traits, unsafe functions, unsafe impls (trait impls),
    1414                 :             :       // lookahead to resolve syntactical production
    1415                 :         260 :       t = lexer.peek_token (1);
    1416                 :             : 
    1417                 :         260 :       switch (t->get_id ())
    1418                 :             :         {
    1419                 :          70 :         case AUTO:
    1420                 :             :         case TRAIT:
    1421                 :          70 :           return parse_trait (std::move (vis), std::move (outer_attrs));
    1422                 :         113 :         case EXTERN_KW:
    1423                 :             :         case FN_KW:
    1424                 :         113 :           return parse_function (std::move (vis), std::move (outer_attrs));
    1425                 :          75 :         case IMPL:
    1426                 :          75 :           return parse_impl (std::move (vis), std::move (outer_attrs));
    1427                 :           2 :         case MOD:
    1428                 :           2 :           return parse_module (std::move (vis), std::move (outer_attrs));
    1429                 :           0 :         default:
    1430                 :           0 :           add_error (
    1431                 :           0 :             Error (t->get_locus (),
    1432                 :             :                    "unexpected token %qs in some sort of unsafe production",
    1433                 :             :                    t->get_token_description ()));
    1434                 :             : 
    1435                 :           0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    1436                 :           0 :           return nullptr;
    1437                 :             :         }
    1438                 :          59 :     case MACRO:
    1439                 :          59 :       return parse_decl_macro_def (std::move (vis), std::move (outer_attrs));
    1440                 :             :     default:
    1441                 :             :       // otherwise vis item clearly doesn't exist, which is not an error
    1442                 :             :       // has a catch-all post-switch return to allow other breaks to occur
    1443                 :             :       break;
    1444                 :             :     }
    1445                 :           0 :   return nullptr;
    1446                 :       18218 : }
    1447                 :             : 
    1448                 :             : template <typename ManagedTokenSource>
    1449                 :             : std::unique_ptr<AST::Function>
    1450                 :           8 : Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
    1451                 :             :                                               AST::AttrVec outer_attrs)
    1452                 :             : {
    1453                 :          14 :   auto offset = (lexer.peek_token ()->get_id () == CONST) ? 1 : 0;
    1454                 :           8 :   const_TokenPtr t = lexer.peek_token (offset);
    1455                 :             : 
    1456                 :           8 :   if (Session::get_instance ().options.get_edition ()
    1457                 :           8 :       == CompileOptions::Edition::E2015)
    1458                 :             :     {
    1459                 :           2 :       add_error (Error (t->get_locus (), ErrorCode::E0670,
    1460                 :             :                         "%<async fn%> is not permitted in Rust 2015"));
    1461                 :           2 :       add_error (
    1462                 :           4 :         Error::Hint (t->get_locus (),
    1463                 :             :                      "to use %<async fn%>, switch to Rust 2018 or later"));
    1464                 :             :     }
    1465                 :             : 
    1466                 :           8 :   t = lexer.peek_token (offset + 1);
    1467                 :             : 
    1468                 :           8 :   switch (t->get_id ())
    1469                 :             :     {
    1470                 :           8 :     case UNSAFE:
    1471                 :             :     case FN_KW:
    1472                 :           8 :       return parse_function (std::move (vis), std::move (outer_attrs));
    1473                 :             : 
    1474                 :           0 :     default:
    1475                 :           0 :       add_error (
    1476                 :           0 :         Error (t->get_locus (), "expected item, found keyword %<async%>"));
    1477                 :             : 
    1478                 :           0 :       lexer.skip_token (1);
    1479                 :           0 :       return nullptr;
    1480                 :             :     }
    1481                 :           8 : }
    1482                 :             : 
    1483                 :             : // Parses a macro rules definition syntax extension whatever thing.
    1484                 :             : template <typename ManagedTokenSource>
    1485                 :             : std::unique_ptr<AST::MacroRulesDefinition>
    1486                 :         866 : Parser<ManagedTokenSource>::parse_macro_rules_def (AST::AttrVec outer_attrs)
    1487                 :             : {
    1488                 :             :   // ensure that first token is identifier saying "macro_rules"
    1489                 :         866 :   const_TokenPtr t = lexer.peek_token ();
    1490                 :         866 :   if (t->get_id () != IDENTIFIER
    1491                 :         866 :       || t->get_str () != Values::WeakKeywords::MACRO_RULES)
    1492                 :             :     {
    1493                 :           0 :       Error error (
    1494                 :             :         t->get_locus (),
    1495                 :             :         "macro rules definition does not start with %<macro_rules%>");
    1496                 :           0 :       add_error (std::move (error));
    1497                 :             : 
    1498                 :             :       // skip after somewhere?
    1499                 :           0 :       return nullptr;
    1500                 :           0 :     }
    1501                 :         866 :   lexer.skip_token ();
    1502                 :         866 :   location_t macro_locus = t->get_locus ();
    1503                 :             : 
    1504                 :         866 :   if (!skip_token (EXCLAM))
    1505                 :             :     {
    1506                 :             :       // skip after somewhere?
    1507                 :           0 :       return nullptr;
    1508                 :             :     }
    1509                 :             : 
    1510                 :             :   // parse macro name
    1511                 :         866 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    1512                 :         866 :   if (ident_tok == nullptr)
    1513                 :             :     {
    1514                 :           2 :       return nullptr;
    1515                 :             :     }
    1516                 :         864 :   Identifier rule_name{ident_tok};
    1517                 :             : 
    1518                 :             :   // DEBUG
    1519                 :         864 :   rust_debug ("in macro rules def, about to parse parens.");
    1520                 :             : 
    1521                 :             :   // save delim type to ensure it is reused later
    1522                 :         864 :   AST::DelimType delim_type = AST::PARENS;
    1523                 :             : 
    1524                 :             :   // Map tokens to DelimType
    1525                 :         864 :   t = lexer.peek_token ();
    1526                 :         864 :   switch (t->get_id ())
    1527                 :             :     {
    1528                 :             :     case LEFT_PAREN:
    1529                 :             :       delim_type = AST::PARENS;
    1530                 :             :       break;
    1531                 :           0 :     case LEFT_SQUARE:
    1532                 :           0 :       delim_type = AST::SQUARE;
    1533                 :           0 :       break;
    1534                 :         864 :     case LEFT_CURLY:
    1535                 :         864 :       delim_type = AST::CURLY;
    1536                 :         864 :       break;
    1537                 :           0 :     default:
    1538                 :           0 :       add_error (Error (t->get_locus (),
    1539                 :             :                         "unexpected token %qs - expecting delimiters (for a "
    1540                 :             :                         "macro rules definition)",
    1541                 :             :                         t->get_token_description ()));
    1542                 :             : 
    1543                 :           0 :       return nullptr;
    1544                 :             :     }
    1545                 :         864 :   lexer.skip_token ();
    1546                 :             : 
    1547                 :             :   // parse actual macro rules
    1548                 :         864 :   std::vector<AST::MacroRule> macro_rules;
    1549                 :             : 
    1550                 :             :   // must be at least one macro rule, so parse it
    1551                 :         864 :   AST::MacroRule initial_rule = parse_macro_rule ();
    1552                 :         864 :   if (initial_rule.is_error ())
    1553                 :             :     {
    1554                 :          24 :       Error error (lexer.peek_token ()->get_locus (),
    1555                 :             :                    "required first macro rule in macro rules definition "
    1556                 :             :                    "could not be parsed");
    1557                 :          24 :       add_error (std::move (error));
    1558                 :             : 
    1559                 :             :       // skip after somewhere?
    1560                 :          24 :       return nullptr;
    1561                 :          24 :     }
    1562                 :         840 :   macro_rules.push_back (std::move (initial_rule));
    1563                 :             : 
    1564                 :             :   // DEBUG
    1565                 :         840 :   rust_debug ("successfully pushed back initial macro rule");
    1566                 :             : 
    1567                 :         840 :   t = lexer.peek_token ();
    1568                 :             :   // parse macro rules
    1569                 :         974 :   while (t->get_id () == SEMICOLON)
    1570                 :             :     {
    1571                 :             :       // skip semicolon
    1572                 :         669 :       lexer.skip_token ();
    1573                 :             : 
    1574                 :             :       // don't parse if end of macro rules
    1575                 :        1338 :       if (token_id_matches_delims (lexer.peek_token ()->get_id (), delim_type))
    1576                 :             :         {
    1577                 :             :           // DEBUG
    1578                 :         535 :           rust_debug (
    1579                 :             :             "broke out of parsing macro rules loop due to finding delim");
    1580                 :             : 
    1581                 :         535 :           break;
    1582                 :             :         }
    1583                 :             : 
    1584                 :             :       // try to parse next rule
    1585                 :         134 :       AST::MacroRule rule = parse_macro_rule ();
    1586                 :         134 :       if (rule.is_error ())
    1587                 :             :         {
    1588                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    1589                 :             :                        "failed to parse macro rule in macro rules definition");
    1590                 :           0 :           add_error (std::move (error));
    1591                 :             : 
    1592                 :           0 :           return nullptr;
    1593                 :           0 :         }
    1594                 :             : 
    1595                 :         134 :       macro_rules.push_back (std::move (rule));
    1596                 :             : 
    1597                 :             :       // DEBUG
    1598                 :         134 :       rust_debug ("successfully pushed back another macro rule");
    1599                 :             : 
    1600                 :         134 :       t = lexer.peek_token ();
    1601                 :             :     }
    1602                 :             : 
    1603                 :             :   // parse end delimiters
    1604                 :         840 :   t = lexer.peek_token ();
    1605                 :         840 :   if (token_id_matches_delims (t->get_id (), delim_type))
    1606                 :             :     {
    1607                 :             :       // tokens match opening delimiter, so skip.
    1608                 :         840 :       lexer.skip_token ();
    1609                 :             : 
    1610                 :         840 :       if (delim_type != AST::CURLY)
    1611                 :             :         {
    1612                 :             :           // skip semicolon at end of non-curly macro definitions
    1613                 :           0 :           if (!skip_token (SEMICOLON))
    1614                 :             :             {
    1615                 :             :               // as this is the end, allow recovery (probably) - may change
    1616                 :             :               return std::unique_ptr<AST::MacroRulesDefinition> (
    1617                 :           0 :                 AST::MacroRulesDefinition::mbe (
    1618                 :             :                   std::move (rule_name), delim_type, std::move (macro_rules),
    1619                 :           0 :                   std::move (outer_attrs), macro_locus));
    1620                 :             :             }
    1621                 :             :         }
    1622                 :             : 
    1623                 :             :       return std::unique_ptr<AST::MacroRulesDefinition> (
    1624                 :        1680 :         AST::MacroRulesDefinition::mbe (std::move (rule_name), delim_type,
    1625                 :             :                                         std::move (macro_rules),
    1626                 :         840 :                                         std::move (outer_attrs), macro_locus));
    1627                 :             :     }
    1628                 :             :   else
    1629                 :             :     {
    1630                 :             :       // tokens don't match opening delimiters, so produce error
    1631                 :           0 :       Error error (t->get_locus (),
    1632                 :             :                    "unexpected token %qs - expecting closing delimiter %qs "
    1633                 :             :                    "(for a macro rules definition)",
    1634                 :             :                    t->get_token_description (),
    1635                 :             :                    (delim_type == AST::PARENS
    1636                 :             :                       ? ")"
    1637                 :             :                       : (delim_type == AST::SQUARE ? "]" : "}")));
    1638                 :           0 :       add_error (std::move (error));
    1639                 :             : 
    1640                 :             :       /* return empty macro definiton despite possibly parsing mostly valid one
    1641                 :             :        * - TODO is this a good idea? */
    1642                 :           0 :       return nullptr;
    1643                 :           0 :     }
    1644                 :        2594 : }
    1645                 :             : 
    1646                 :             : // Parses a declarative macro 2.0 definition.
    1647                 :             : template <typename ManagedTokenSource>
    1648                 :             : std::unique_ptr<AST::MacroRulesDefinition>
    1649                 :          59 : Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis,
    1650                 :             :                                                   AST::AttrVec outer_attrs)
    1651                 :             : {
    1652                 :             :   // ensure that first token is identifier saying "macro"
    1653                 :          59 :   const_TokenPtr t = lexer.peek_token ();
    1654                 :          59 :   if (t->get_id () != MACRO)
    1655                 :             :     {
    1656                 :           0 :       Error error (
    1657                 :             :         t->get_locus (),
    1658                 :             :         "declarative macro definition does not start with %<macro%>");
    1659                 :           0 :       add_error (std::move (error));
    1660                 :             : 
    1661                 :             :       // skip after somewhere?
    1662                 :           0 :       return nullptr;
    1663                 :           0 :     }
    1664                 :          59 :   lexer.skip_token ();
    1665                 :          59 :   location_t macro_locus = t->get_locus ();
    1666                 :             : 
    1667                 :             :   // parse macro name
    1668                 :          59 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    1669                 :          59 :   if (ident_tok == nullptr)
    1670                 :             :     {
    1671                 :           0 :       return nullptr;
    1672                 :             :     }
    1673                 :          59 :   Identifier rule_name{ident_tok};
    1674                 :             : 
    1675                 :          59 :   t = lexer.peek_token ();
    1676                 :          59 :   if (t->get_id () == LEFT_PAREN)
    1677                 :             :     {
    1678                 :             :       // single definiton of macro rule
    1679                 :             :       // e.g. `macro foo($e:expr) {}`
    1680                 :             : 
    1681                 :             :       // parse macro matcher
    1682                 :          30 :       location_t locus = lexer.peek_token ()->get_locus ();
    1683                 :          30 :       AST::MacroMatcher matcher = parse_macro_matcher ();
    1684                 :          30 :       if (matcher.is_error ())
    1685                 :           0 :         return nullptr;
    1686                 :             : 
    1687                 :             :       // check delimiter of macro matcher
    1688                 :          30 :       if (matcher.get_delim_type () != AST::DelimType::PARENS)
    1689                 :             :         {
    1690                 :           0 :           Error error (locus, "only parenthesis can be used for a macro "
    1691                 :             :                               "matcher in declarative macro definition");
    1692                 :           0 :           add_error (std::move (error));
    1693                 :           0 :           return nullptr;
    1694                 :           0 :         }
    1695                 :             : 
    1696                 :          30 :       location_t transcriber_loc = lexer.peek_token ()->get_locus ();
    1697                 :          30 :       AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
    1698                 :          30 :       AST::MacroTranscriber transcriber (delim_tok_tree, transcriber_loc);
    1699                 :             : 
    1700                 :          30 :       if (transcriber.get_token_tree ().get_delim_type ()
    1701                 :             :           != AST::DelimType::CURLY)
    1702                 :             :         {
    1703                 :           2 :           Error error (transcriber_loc,
    1704                 :             :                        "only braces can be used for a macro transcriber "
    1705                 :             :                        "in declarative macro definition");
    1706                 :           2 :           add_error (std::move (error));
    1707                 :           2 :           return nullptr;
    1708                 :           2 :         }
    1709                 :             : 
    1710                 :          28 :       AST::MacroRule macro_rule
    1711                 :          56 :         = AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
    1712                 :          28 :       std::vector<AST::MacroRule> macro_rules;
    1713                 :          28 :       macro_rules.push_back (macro_rule);
    1714                 :             : 
    1715                 :             :       return std::unique_ptr<AST::MacroRulesDefinition> (
    1716                 :          56 :         AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
    1717                 :             :                                                macro_rules,
    1718                 :             :                                                std::move (outer_attrs),
    1719                 :          28 :                                                macro_locus, vis));
    1720                 :          88 :     }
    1721                 :          29 :   else if (t->get_id () == LEFT_CURLY)
    1722                 :             :     {
    1723                 :             :       // multiple definitions of macro rule separated by comma
    1724                 :             :       // e.g. `macro foo { () => {}, ($e:expr) => {}, }`
    1725                 :             : 
    1726                 :             :       // parse left curly
    1727                 :          29 :       const_TokenPtr left_curly = expect_token (LEFT_CURLY);
    1728                 :          29 :       if (left_curly == nullptr)
    1729                 :             :         {
    1730                 :           0 :           return nullptr;
    1731                 :             :         }
    1732                 :             : 
    1733                 :             :       // parse actual macro rules
    1734                 :          29 :       std::vector<AST::MacroRule> macro_rules;
    1735                 :             : 
    1736                 :             :       // must be at least one macro rule, so parse it
    1737                 :          29 :       AST::MacroRule initial_rule = parse_macro_rule ();
    1738                 :          29 :       if (initial_rule.is_error ())
    1739                 :             :         {
    1740                 :           2 :           Error error (
    1741                 :           2 :             lexer.peek_token ()->get_locus (),
    1742                 :             :             "required first macro rule in declarative macro definition "
    1743                 :             :             "could not be parsed");
    1744                 :           2 :           add_error (std::move (error));
    1745                 :             : 
    1746                 :             :           // skip after somewhere?
    1747                 :           2 :           return nullptr;
    1748                 :           2 :         }
    1749                 :          27 :       macro_rules.push_back (std::move (initial_rule));
    1750                 :             : 
    1751                 :          27 :       t = lexer.peek_token ();
    1752                 :             :       // parse macro rules
    1753                 :          45 :       while (t->get_id () == COMMA)
    1754                 :             :         {
    1755                 :             :           // skip comma
    1756                 :          36 :           lexer.skip_token ();
    1757                 :             : 
    1758                 :             :           // don't parse if end of macro rules
    1759                 :          72 :           if (token_id_matches_delims (lexer.peek_token ()->get_id (),
    1760                 :             :                                        AST::CURLY))
    1761                 :             :             {
    1762                 :             :               break;
    1763                 :             :             }
    1764                 :             : 
    1765                 :             :           // try to parse next rule
    1766                 :          18 :           AST::MacroRule rule = parse_macro_rule ();
    1767                 :          18 :           if (rule.is_error ())
    1768                 :             :             {
    1769                 :           0 :               Error error (
    1770                 :           0 :                 lexer.peek_token ()->get_locus (),
    1771                 :             :                 "failed to parse macro rule in declarative macro definition");
    1772                 :           0 :               add_error (std::move (error));
    1773                 :             : 
    1774                 :           0 :               return nullptr;
    1775                 :           0 :             }
    1776                 :             : 
    1777                 :          18 :           macro_rules.push_back (std::move (rule));
    1778                 :             : 
    1779                 :          18 :           t = lexer.peek_token ();
    1780                 :             :         }
    1781                 :             : 
    1782                 :             :       // parse right curly
    1783                 :          27 :       const_TokenPtr right_curly = expect_token (RIGHT_CURLY);
    1784                 :          27 :       if (right_curly == nullptr)
    1785                 :             :         {
    1786                 :           0 :           return nullptr;
    1787                 :             :         }
    1788                 :             : 
    1789                 :             :       return std::unique_ptr<AST::MacroRulesDefinition> (
    1790                 :          54 :         AST::MacroRulesDefinition::decl_macro (std::move (rule_name),
    1791                 :             :                                                std::move (macro_rules),
    1792                 :             :                                                std::move (outer_attrs),
    1793                 :          27 :                                                macro_locus, vis));
    1794                 :          58 :     }
    1795                 :             :   else
    1796                 :             :     {
    1797                 :           0 :       add_error (Error (t->get_locus (),
    1798                 :             :                         "unexpected token %qs - expecting delimiters "
    1799                 :             :                         "(for a declarative macro definiton)",
    1800                 :             :                         t->get_token_description ()));
    1801                 :           0 :       return nullptr;
    1802                 :             :     }
    1803                 :         118 : }
    1804                 :             : 
    1805                 :             : // Parses a semi-coloned (except for full block) macro invocation item.
    1806                 :             : template <typename ManagedTokenSource>
    1807                 :             : std::unique_ptr<AST::MacroInvocation>
    1808                 :         351 : Parser<ManagedTokenSource>::parse_macro_invocation_semi (
    1809                 :             :   AST::AttrVec outer_attrs)
    1810                 :             : {
    1811                 :         351 :   location_t macro_locus = lexer.peek_token ()->get_locus ();
    1812                 :         351 :   AST::SimplePath path = parse_simple_path ();
    1813                 :             : 
    1814                 :         351 :   if (!skip_token (EXCLAM))
    1815                 :             :     {
    1816                 :             :       // skip after somewhere?
    1817                 :           0 :       return nullptr;
    1818                 :             :     }
    1819                 :             : 
    1820                 :             :   // save delim type to ensure it is reused later
    1821                 :         351 :   AST::DelimType delim_type = AST::PARENS;
    1822                 :             : 
    1823                 :             :   // Map tokens to DelimType
    1824                 :         351 :   const_TokenPtr t = lexer.peek_token ();
    1825                 :         351 :   switch (t->get_id ())
    1826                 :             :     {
    1827                 :             :     case LEFT_PAREN:
    1828                 :             :       delim_type = AST::PARENS;
    1829                 :             :       break;
    1830                 :           0 :     case LEFT_SQUARE:
    1831                 :           0 :       delim_type = AST::SQUARE;
    1832                 :           0 :       break;
    1833                 :         202 :     case LEFT_CURLY:
    1834                 :         202 :       delim_type = AST::CURLY;
    1835                 :         202 :       break;
    1836                 :           0 :     default:
    1837                 :           0 :       add_error (Error (t->get_locus (),
    1838                 :             :                         "unexpected token %qs - expecting delimiters (for a "
    1839                 :             :                         "macro invocation semi body)",
    1840                 :             :                         t->get_token_description ()));
    1841                 :             : 
    1842                 :           0 :       return nullptr;
    1843                 :             :     }
    1844                 :         351 :   location_t tok_tree_locus = t->get_locus ();
    1845                 :         351 :   lexer.skip_token ();
    1846                 :             : 
    1847                 :             :   // parse actual token trees
    1848                 :         351 :   std::vector<std::unique_ptr<AST::TokenTree>> token_trees;
    1849                 :         351 :   auto delim_open
    1850                 :         351 :     = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    1851                 :         351 :   token_trees.push_back (std::move (delim_open));
    1852                 :             : 
    1853                 :         351 :   t = lexer.peek_token ();
    1854                 :             :   // parse token trees until the initial delimiter token is found again
    1855                 :        3501 :   while (!token_id_matches_delims (t->get_id (), delim_type))
    1856                 :             :     {
    1857                 :        3150 :       std::unique_ptr<AST::TokenTree> tree = parse_token_tree ();
    1858                 :             : 
    1859                 :        3150 :       if (tree == nullptr)
    1860                 :             :         {
    1861                 :           0 :           Error error (t->get_locus (),
    1862                 :             :                        "failed to parse token tree for macro invocation semi "
    1863                 :             :                        "- found %qs",
    1864                 :             :                        t->get_token_description ());
    1865                 :           0 :           add_error (std::move (error));
    1866                 :             : 
    1867                 :           0 :           return nullptr;
    1868                 :           0 :         }
    1869                 :             : 
    1870                 :        3150 :       token_trees.push_back (std::move (tree));
    1871                 :             : 
    1872                 :        3150 :       t = lexer.peek_token ();
    1873                 :             :     }
    1874                 :         351 :   auto delim_close
    1875                 :         351 :     = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    1876                 :         351 :   token_trees.push_back (std::move (delim_close));
    1877                 :             : 
    1878                 :         351 :   AST::DelimTokenTree delim_tok_tree (delim_type, std::move (token_trees),
    1879                 :             :                                       tok_tree_locus);
    1880                 :         351 :   AST::MacroInvocData invoc_data (std::move (path), std::move (delim_tok_tree));
    1881                 :             : 
    1882                 :             :   // parse end delimiters
    1883                 :         351 :   t = lexer.peek_token ();
    1884                 :         351 :   if (token_id_matches_delims (t->get_id (), delim_type))
    1885                 :             :     {
    1886                 :             :       // tokens match opening delimiter, so skip.
    1887                 :         351 :       lexer.skip_token ();
    1888                 :             : 
    1889                 :         351 :       if (delim_type != AST::CURLY)
    1890                 :             :         {
    1891                 :             :           // skip semicolon at end of non-curly macro invocation semis
    1892                 :         149 :           if (!skip_token (SEMICOLON))
    1893                 :             :             {
    1894                 :             :               // as this is the end, allow recovery (probably) - may change
    1895                 :             : 
    1896                 :           0 :               return AST::MacroInvocation::Regular (std::move (invoc_data),
    1897                 :             :                                                     std::move (outer_attrs),
    1898                 :           0 :                                                     macro_locus, true);
    1899                 :             :             }
    1900                 :             :         }
    1901                 :             : 
    1902                 :             :       // DEBUG:
    1903                 :         702 :       rust_debug ("skipped token is '%s', next token (current peek) is '%s'",
    1904                 :             :                   t->get_token_description (),
    1905                 :             :                   lexer.peek_token ()->get_token_description ());
    1906                 :             : 
    1907                 :         702 :       return AST::MacroInvocation::Regular (std::move (invoc_data),
    1908                 :             :                                             std::move (outer_attrs),
    1909                 :         351 :                                             macro_locus, true);
    1910                 :             :     }
    1911                 :             :   else
    1912                 :             :     {
    1913                 :             :       // tokens don't match opening delimiters, so produce error
    1914                 :           0 :       Error error (t->get_locus (),
    1915                 :             :                    "unexpected token %qs - expecting closing delimiter %qs "
    1916                 :             :                    "(for a macro invocation semi)",
    1917                 :             :                    t->get_token_description (),
    1918                 :             :                    (delim_type == AST::PARENS
    1919                 :             :                       ? ")"
    1920                 :             :                       : (delim_type == AST::SQUARE ? "]" : "}")));
    1921                 :           0 :       add_error (std::move (error));
    1922                 :             : 
    1923                 :             :       /* return empty macro invocation despite possibly parsing mostly valid one
    1924                 :             :        * - TODO is this a good idea? */
    1925                 :           0 :       return nullptr;
    1926                 :           0 :     }
    1927                 :         702 : }
    1928                 :             : 
    1929                 :             : // Parses a non-semicoloned macro invocation (i.e. as pattern or expression).
    1930                 :             : template <typename ManagedTokenSource>
    1931                 :             : std::unique_ptr<AST::MacroInvocation>
    1932                 :         388 : Parser<ManagedTokenSource>::parse_macro_invocation (AST::AttrVec outer_attrs)
    1933                 :             : {
    1934                 :             :   // parse macro path
    1935                 :         388 :   AST::SimplePath macro_path = parse_simple_path ();
    1936                 :         388 :   if (macro_path.is_empty ())
    1937                 :             :     {
    1938                 :         286 :       Error error (lexer.peek_token ()->get_locus (),
    1939                 :             :                    "failed to parse macro invocation path");
    1940                 :         286 :       add_error (std::move (error));
    1941                 :             : 
    1942                 :             :       // skip?
    1943                 :         286 :       return nullptr;
    1944                 :         286 :     }
    1945                 :             : 
    1946                 :         102 :   if (!skip_token (EXCLAM))
    1947                 :             :     {
    1948                 :             :       // skip after somewhere?
    1949                 :           6 :       return nullptr;
    1950                 :             :     }
    1951                 :             : 
    1952                 :             :   // parse internal delim token tree
    1953                 :          96 :   AST::DelimTokenTree delim_tok_tree = parse_delim_token_tree ();
    1954                 :             : 
    1955                 :          96 :   location_t macro_locus = macro_path.get_locus ();
    1956                 :             : 
    1957                 :         192 :   return AST::MacroInvocation::Regular (
    1958                 :         192 :     AST::MacroInvocData (std::move (macro_path), std::move (delim_tok_tree)),
    1959                 :          96 :     std::move (outer_attrs), macro_locus);
    1960                 :          96 : }
    1961                 :             : 
    1962                 :             : // Parses a macro rule definition - does not parse semicolons.
    1963                 :             : template <typename ManagedTokenSource>
    1964                 :             : AST::MacroRule
    1965                 :        1045 : Parser<ManagedTokenSource>::parse_macro_rule ()
    1966                 :             : {
    1967                 :        1045 :   location_t locus = lexer.peek_token ()->get_locus ();
    1968                 :             : 
    1969                 :             :   // parse macro matcher
    1970                 :        1045 :   AST::MacroMatcher matcher = parse_macro_matcher ();
    1971                 :             : 
    1972                 :        1045 :   if (matcher.is_error ())
    1973                 :          26 :     return AST::MacroRule::create_error (locus);
    1974                 :             : 
    1975                 :        1019 :   if (!skip_token (MATCH_ARROW))
    1976                 :             :     {
    1977                 :             :       // skip after somewhere?
    1978                 :           0 :       return AST::MacroRule::create_error (locus);
    1979                 :             :     }
    1980                 :             : 
    1981                 :             :   // parse transcriber (this is just a delim token tree)
    1982                 :        1019 :   location_t token_tree_loc = lexer.peek_token ()->get_locus ();
    1983                 :        1019 :   AST::MacroTranscriber transcriber (parse_delim_token_tree (), token_tree_loc);
    1984                 :             : 
    1985                 :        1019 :   return AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
    1986                 :        1019 : }
    1987                 :             : 
    1988                 :             : // Parses a macro matcher (part of a macro rule definition).
    1989                 :             : template <typename ManagedTokenSource>
    1990                 :             : AST::MacroMatcher
    1991                 :        1134 : Parser<ManagedTokenSource>::parse_macro_matcher ()
    1992                 :             : {
    1993                 :             :   // save delim type to ensure it is reused later
    1994                 :        1134 :   AST::DelimType delim_type = AST::PARENS;
    1995                 :             : 
    1996                 :             :   // DEBUG
    1997                 :        1134 :   rust_debug ("begun parsing macro matcher");
    1998                 :             : 
    1999                 :             :   // Map tokens to DelimType
    2000                 :        1134 :   const_TokenPtr t = lexer.peek_token ();
    2001                 :        1134 :   location_t locus = t->get_locus ();
    2002                 :        1134 :   switch (t->get_id ())
    2003                 :             :     {
    2004                 :             :     case LEFT_PAREN:
    2005                 :             :       delim_type = AST::PARENS;
    2006                 :             :       break;
    2007                 :          24 :     case LEFT_SQUARE:
    2008                 :          24 :       delim_type = AST::SQUARE;
    2009                 :          24 :       break;
    2010                 :          17 :     case LEFT_CURLY:
    2011                 :          17 :       delim_type = AST::CURLY;
    2012                 :          17 :       break;
    2013                 :           2 :     default:
    2014                 :           2 :       add_error (Error (
    2015                 :             :         t->get_locus (),
    2016                 :             :         "unexpected token %qs - expecting delimiters (for a macro matcher)",
    2017                 :             :         t->get_token_description ()));
    2018                 :             : 
    2019                 :           2 :       return AST::MacroMatcher::create_error (t->get_locus ());
    2020                 :             :     }
    2021                 :        1132 :   lexer.skip_token ();
    2022                 :             : 
    2023                 :             :   // parse actual macro matches
    2024                 :        1132 :   std::vector<std::unique_ptr<AST::MacroMatch>> matches;
    2025                 :             :   // Set of possible preceding macro matches to make sure follow-set
    2026                 :             :   // restrictions are respected.
    2027                 :             :   // TODO: Consider using std::reference_wrapper instead of raw pointers?
    2028                 :        1132 :   std::vector<const AST::MacroMatch *> last_matches;
    2029                 :             : 
    2030                 :        1132 :   t = lexer.peek_token ();
    2031                 :             :   // parse token trees until the initial delimiter token is found again
    2032                 :        2414 :   while (!token_id_matches_delims (t->get_id (), delim_type))
    2033                 :             :     {
    2034                 :        1282 :       std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
    2035                 :             : 
    2036                 :        1282 :       if (match == nullptr)
    2037                 :             :         {
    2038                 :           2 :           Error error (
    2039                 :             :             t->get_locus (),
    2040                 :             :             "failed to parse macro match for macro matcher - found %qs",
    2041                 :             :             t->get_token_description ());
    2042                 :           2 :           add_error (std::move (error));
    2043                 :             : 
    2044                 :           2 :           return AST::MacroMatcher::create_error (t->get_locus ());
    2045                 :           2 :         }
    2046                 :             : 
    2047                 :        1280 :       if (matches.size () > 0)
    2048                 :             :         {
    2049                 :         543 :           const auto *last_match = matches.back ().get ();
    2050                 :             : 
    2051                 :             :           // We want to check if we are dealing with a zeroable repetition
    2052                 :         543 :           bool zeroable = false;
    2053                 :         543 :           if (last_match->get_macro_match_type ()
    2054                 :             :               == AST::MacroMatch::MacroMatchType::Repetition)
    2055                 :             :             {
    2056                 :          32 :               auto repetition
    2057                 :             :                 = static_cast<const AST::MacroMatchRepetition *> (last_match);
    2058                 :             : 
    2059                 :          32 :               if (repetition->get_op ()
    2060                 :             :                   != AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE)
    2061                 :             :                 zeroable = true;
    2062                 :             :             }
    2063                 :             : 
    2064                 :             :           if (!zeroable)
    2065                 :         543 :             last_matches.clear ();
    2066                 :             : 
    2067                 :         543 :           last_matches.emplace_back (last_match);
    2068                 :             : 
    2069                 :        1068 :           for (auto last : last_matches)
    2070                 :         549 :             if (!is_match_compatible (*last, *match))
    2071                 :             :               return AST::MacroMatcher::create_error (
    2072                 :          24 :                 match->get_match_locus ());
    2073                 :             :         }
    2074                 :             : 
    2075                 :        1256 :       matches.push_back (std::move (match));
    2076                 :             : 
    2077                 :             :       // DEBUG
    2078                 :        1256 :       rust_debug ("pushed back a match in macro matcher");
    2079                 :             : 
    2080                 :        1256 :       t = lexer.peek_token ();
    2081                 :             :     }
    2082                 :             : 
    2083                 :             :   // parse end delimiters
    2084                 :        1106 :   t = lexer.peek_token ();
    2085                 :        1106 :   if (token_id_matches_delims (t->get_id (), delim_type))
    2086                 :             :     {
    2087                 :             :       // tokens match opening delimiter, so skip.
    2088                 :        1106 :       lexer.skip_token ();
    2089                 :             : 
    2090                 :        1106 :       return AST::MacroMatcher (delim_type, std::move (matches), locus);
    2091                 :             :     }
    2092                 :             :   else
    2093                 :             :     {
    2094                 :             :       // tokens don't match opening delimiters, so produce error
    2095                 :           0 :       Error error (t->get_locus (),
    2096                 :             :                    "unexpected token %qs - expecting closing delimiter %qs "
    2097                 :             :                    "(for a macro matcher)",
    2098                 :             :                    t->get_token_description (),
    2099                 :             :                    (delim_type == AST::PARENS
    2100                 :             :                       ? ")"
    2101                 :             :                       : (delim_type == AST::SQUARE ? "]" : "}")));
    2102                 :           0 :       add_error (std::move (error));
    2103                 :             : 
    2104                 :             :       /* return error macro matcher despite possibly parsing mostly correct one?
    2105                 :             :        * TODO is this the best idea? */
    2106                 :           0 :       return AST::MacroMatcher::create_error (t->get_locus ());
    2107                 :           0 :     }
    2108                 :        1132 : }
    2109                 :             : 
    2110                 :             : // Parses a macro match (syntax match inside a matcher in a macro rule).
    2111                 :             : template <typename ManagedTokenSource>
    2112                 :             : std::unique_ptr<AST::MacroMatch>
    2113                 :        1764 : Parser<ManagedTokenSource>::parse_macro_match ()
    2114                 :             : {
    2115                 :             :   // branch based on token available
    2116                 :        1764 :   const_TokenPtr t = lexer.peek_token ();
    2117                 :        1764 :   switch (t->get_id ())
    2118                 :             :     {
    2119                 :          59 :     case LEFT_PAREN:
    2120                 :             :     case LEFT_SQUARE:
    2121                 :             :       case LEFT_CURLY: {
    2122                 :             :         // must be macro matcher as delimited
    2123                 :          59 :         AST::MacroMatcher matcher = parse_macro_matcher ();
    2124                 :          59 :         if (matcher.is_error ())
    2125                 :             :           {
    2126                 :           2 :             Error error (lexer.peek_token ()->get_locus (),
    2127                 :             :                          "failed to parse macro matcher in macro match");
    2128                 :           2 :             add_error (std::move (error));
    2129                 :             : 
    2130                 :           2 :             return nullptr;
    2131                 :           2 :           }
    2132                 :          57 :         return std::unique_ptr<AST::MacroMatcher> (
    2133                 :          57 :           new AST::MacroMatcher (std::move (matcher)));
    2134                 :          59 :       }
    2135                 :        1211 :       case DOLLAR_SIGN: {
    2136                 :             :         // have to do more lookahead to determine if fragment or repetition
    2137                 :        1211 :         const_TokenPtr t2 = lexer.peek_token (1);
    2138                 :        1211 :         switch (t2->get_id ())
    2139                 :             :           {
    2140                 :         799 :           case IDENTIFIER:
    2141                 :             :           case UNDERSCORE:
    2142                 :             :             // macro fragment
    2143                 :         799 :             return parse_macro_match_fragment ();
    2144                 :         410 :           case LEFT_PAREN:
    2145                 :             :             // macro repetition
    2146                 :         410 :             return parse_macro_match_repetition ();
    2147                 :           2 :           default:
    2148                 :           2 :             if (token_id_is_keyword (t2->get_id ()) && t2->get_id () != CRATE)
    2149                 :             :               {
    2150                 :             :                 // keyword as macro fragment
    2151                 :           2 :                 return parse_macro_match_fragment ();
    2152                 :             :               }
    2153                 :             :             else
    2154                 :             :               {
    2155                 :             :                 // error: unrecognised
    2156                 :           0 :                 add_error (Error (
    2157                 :             :                   t2->get_locus (),
    2158                 :             :                   "unrecognised token combination %<$%s%> at start of "
    2159                 :             :                   "macro match - did you mean %<$identifier%> or %<$(%>?",
    2160                 :             :                   t2->get_token_description ()));
    2161                 :             : 
    2162                 :             :                 // skip somewhere?
    2163                 :           0 :                 return nullptr;
    2164                 :             :               }
    2165                 :             :           }
    2166                 :        1211 :       }
    2167                 :           0 :     case RIGHT_PAREN:
    2168                 :             :     case RIGHT_SQUARE:
    2169                 :             :     case RIGHT_CURLY:
    2170                 :             :       // not allowed
    2171                 :           0 :       add_error (Error (
    2172                 :             :         t->get_locus (),
    2173                 :             :         "closing delimiters like %qs are not allowed at the start of a macro "
    2174                 :             :         "match",
    2175                 :             :         t->get_token_description ()));
    2176                 :             : 
    2177                 :             :       // skip somewhere?
    2178                 :           0 :       return nullptr;
    2179                 :         494 :     default:
    2180                 :             :       // just the token
    2181                 :         494 :       lexer.skip_token ();
    2182                 :         494 :       return std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    2183                 :             :     }
    2184                 :        1764 : }
    2185                 :             : 
    2186                 :             : // Parses a fragment macro match.
    2187                 :             : template <typename ManagedTokenSource>
    2188                 :             : std::unique_ptr<AST::MacroMatchFragment>
    2189                 :         801 : Parser<ManagedTokenSource>::parse_macro_match_fragment ()
    2190                 :             : {
    2191                 :         801 :   location_t fragment_locus = lexer.peek_token ()->get_locus ();
    2192                 :         801 :   skip_token (DOLLAR_SIGN);
    2193                 :             : 
    2194                 :         801 :   Identifier ident;
    2195                 :         801 :   auto identifier = lexer.peek_token ();
    2196                 :         801 :   if (identifier->get_id () == UNDERSCORE)
    2197                 :           4 :     ident = {Values::Keywords::UNDERSCORE, identifier->get_locus ()};
    2198                 :             :   else
    2199                 :        1594 :     ident = {identifier};
    2200                 :             : 
    2201                 :         801 :   if (ident.empty ())
    2202                 :             :     {
    2203                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    2204                 :             :                    "missing identifier in macro match fragment");
    2205                 :           0 :       add_error (std::move (error));
    2206                 :             : 
    2207                 :           0 :       return nullptr;
    2208                 :           0 :     }
    2209                 :         801 :   skip_token (identifier->get_id ());
    2210                 :             : 
    2211                 :         801 :   if (!skip_token (COLON))
    2212                 :             :     {
    2213                 :             :       // skip after somewhere?
    2214                 :           0 :       return nullptr;
    2215                 :             :     }
    2216                 :             : 
    2217                 :             :   // get MacroFragSpec for macro
    2218                 :         801 :   const_TokenPtr t = expect_token (IDENTIFIER);
    2219                 :         801 :   if (t == nullptr)
    2220                 :           0 :     return nullptr;
    2221                 :             : 
    2222                 :             :   AST::MacroFragSpec frag
    2223                 :         801 :     = AST::MacroFragSpec::get_frag_spec_from_str (t->get_str ());
    2224                 :         801 :   if (frag.is_error ())
    2225                 :             :     {
    2226                 :           0 :       Error error (t->get_locus (),
    2227                 :             :                    "invalid fragment specifier %qs in fragment macro match",
    2228                 :           0 :                    t->get_str ().c_str ());
    2229                 :           0 :       add_error (std::move (error));
    2230                 :             : 
    2231                 :           0 :       return nullptr;
    2232                 :           0 :     }
    2233                 :             : 
    2234                 :             :   return std::unique_ptr<AST::MacroMatchFragment> (
    2235                 :         801 :     new AST::MacroMatchFragment (std::move (ident), frag, fragment_locus));
    2236                 :        1602 : }
    2237                 :             : 
    2238                 :             : // Parses a repetition macro match.
    2239                 :             : template <typename ManagedTokenSource>
    2240                 :             : std::unique_ptr<AST::MacroMatchRepetition>
    2241                 :         410 : Parser<ManagedTokenSource>::parse_macro_match_repetition ()
    2242                 :             : {
    2243                 :         410 :   skip_token (DOLLAR_SIGN);
    2244                 :         410 :   skip_token (LEFT_PAREN);
    2245                 :             : 
    2246                 :         410 :   std::vector<std::unique_ptr<AST::MacroMatch>> matches;
    2247                 :             : 
    2248                 :             :   // parse required first macro match
    2249                 :         410 :   std::unique_ptr<AST::MacroMatch> initial_match = parse_macro_match ();
    2250                 :         410 :   if (initial_match == nullptr)
    2251                 :             :     {
    2252                 :           0 :       Error error (
    2253                 :           0 :         lexer.peek_token ()->get_locus (),
    2254                 :             :         "could not parse required first macro match in macro match repetition");
    2255                 :           0 :       add_error (std::move (error));
    2256                 :             : 
    2257                 :             :       // skip after somewhere?
    2258                 :           0 :       return nullptr;
    2259                 :           0 :     }
    2260                 :         410 :   matches.push_back (std::move (initial_match));
    2261                 :             : 
    2262                 :             :   // parse optional later macro matches
    2263                 :         410 :   const_TokenPtr t = lexer.peek_token ();
    2264                 :         482 :   while (t->get_id () != RIGHT_PAREN)
    2265                 :             :     {
    2266                 :          72 :       std::unique_ptr<AST::MacroMatch> match = parse_macro_match ();
    2267                 :             : 
    2268                 :          72 :       if (match == nullptr)
    2269                 :             :         {
    2270                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    2271                 :             :                        "failed to parse macro match in macro match repetition");
    2272                 :           0 :           add_error (std::move (error));
    2273                 :             : 
    2274                 :           0 :           return nullptr;
    2275                 :           0 :         }
    2276                 :             : 
    2277                 :          72 :       matches.push_back (std::move (match));
    2278                 :             : 
    2279                 :          72 :       t = lexer.peek_token ();
    2280                 :             :     }
    2281                 :             : 
    2282                 :         410 :   if (!skip_token (RIGHT_PAREN))
    2283                 :             :     {
    2284                 :             :       // skip after somewhere?
    2285                 :           0 :       return nullptr;
    2286                 :             :     }
    2287                 :             : 
    2288                 :         410 :   t = lexer.peek_token ();
    2289                 :             :   // see if separator token exists
    2290                 :         410 :   std::unique_ptr<AST::Token> separator = nullptr;
    2291                 :         410 :   switch (t->get_id ())
    2292                 :             :     {
    2293                 :             :     // repetition operators
    2294                 :             :     case ASTERISK:
    2295                 :             :     case PLUS:
    2296                 :             :     case QUESTION_MARK:
    2297                 :             :     // delimiters
    2298                 :             :     case LEFT_PAREN:
    2299                 :             :     case LEFT_CURLY:
    2300                 :             :     case LEFT_SQUARE:
    2301                 :             :     case RIGHT_PAREN:
    2302                 :             :     case RIGHT_CURLY:
    2303                 :             :     case RIGHT_SQUARE:
    2304                 :             :       // separator does not exist, so still null and don't skip token
    2305                 :             :       break;
    2306                 :         121 :     default:
    2307                 :             :       // separator does exist
    2308                 :         121 :       separator = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
    2309                 :         121 :       lexer.skip_token ();
    2310                 :             :       break;
    2311                 :             :     }
    2312                 :             : 
    2313                 :             :   // parse repetition operator
    2314                 :         410 :   t = lexer.peek_token ();
    2315                 :         410 :   AST::MacroMatchRepetition::MacroRepOp op = AST::MacroMatchRepetition::NONE;
    2316                 :         410 :   switch (t->get_id ())
    2317                 :             :     {
    2318                 :         333 :     case ASTERISK:
    2319                 :         333 :       op = AST::MacroMatchRepetition::ANY;
    2320                 :         333 :       lexer.skip_token ();
    2321                 :             :       break;
    2322                 :          42 :     case PLUS:
    2323                 :          42 :       op = AST::MacroMatchRepetition::ONE_OR_MORE;
    2324                 :          42 :       lexer.skip_token ();
    2325                 :             :       break;
    2326                 :          35 :     case QUESTION_MARK:
    2327                 :          35 :       op = AST::MacroMatchRepetition::ZERO_OR_ONE;
    2328                 :          35 :       lexer.skip_token ();
    2329                 :             : 
    2330                 :          35 :       if (separator != nullptr)
    2331                 :             :         {
    2332                 :           2 :           add_error (
    2333                 :           4 :             Error (separator->get_locus (),
    2334                 :             :                    "the %<?%> macro repetition operator does not take a "
    2335                 :             :                    "separator"));
    2336                 :           2 :           separator = nullptr;
    2337                 :             :         }
    2338                 :             : 
    2339                 :             :       break;
    2340                 :           0 :     default:
    2341                 :           0 :       add_error (
    2342                 :           0 :         Error (t->get_locus (),
    2343                 :             :                "expected macro repetition operator (%<*%>, %<+%>, or %<?%>) in "
    2344                 :             :                "macro match - found %qs",
    2345                 :             :                t->get_token_description ()));
    2346                 :             : 
    2347                 :             :       // skip after somewhere?
    2348                 :           0 :       return nullptr;
    2349                 :             :     }
    2350                 :             : 
    2351                 :             :   return std::unique_ptr<AST::MacroMatchRepetition> (
    2352                 :         410 :     new AST::MacroMatchRepetition (std::move (matches), op,
    2353                 :         410 :                                    std::move (separator), t->get_locus ()));
    2354                 :         820 : }
    2355                 :             : 
    2356                 :             : /* Parses a visibility syntactical production (i.e. creating a non-default
    2357                 :             :  * visibility) */
    2358                 :             : template <typename ManagedTokenSource>
    2359                 :             : AST::Visibility
    2360                 :       27682 : Parser<ManagedTokenSource>::parse_visibility ()
    2361                 :             : {
    2362                 :             :   // check for no visibility
    2363                 :       55364 :   if (lexer.peek_token ()->get_id () != PUB)
    2364                 :             :     {
    2365                 :       21925 :       return AST::Visibility::create_private ();
    2366                 :             :     }
    2367                 :             : 
    2368                 :        5757 :   auto vis_loc = lexer.peek_token ()->get_locus ();
    2369                 :        5757 :   lexer.skip_token ();
    2370                 :             : 
    2371                 :             :   // create simple pub visibility if
    2372                 :             :   // - found no parentheses
    2373                 :             :   // - found unit type `()`
    2374                 :       11514 :   if (lexer.peek_token ()->get_id () != LEFT_PAREN
    2375                 :        5791 :       || lexer.peek_token (1)->get_id () == RIGHT_PAREN)
    2376                 :             :     {
    2377                 :        5725 :       return AST::Visibility::create_public (vis_loc);
    2378                 :             :       // or whatever
    2379                 :             :     }
    2380                 :             : 
    2381                 :          32 :   lexer.skip_token ();
    2382                 :             : 
    2383                 :          32 :   const_TokenPtr t = lexer.peek_token ();
    2384                 :          32 :   auto path_loc = t->get_locus ();
    2385                 :             : 
    2386                 :          32 :   switch (t->get_id ())
    2387                 :             :     {
    2388                 :           8 :     case CRATE:
    2389                 :           8 :       lexer.skip_token ();
    2390                 :             : 
    2391                 :           8 :       skip_token (RIGHT_PAREN);
    2392                 :             : 
    2393                 :           8 :       return AST::Visibility::create_crate (path_loc, vis_loc);
    2394                 :           0 :     case SELF:
    2395                 :           0 :       lexer.skip_token ();
    2396                 :             : 
    2397                 :           0 :       skip_token (RIGHT_PAREN);
    2398                 :             : 
    2399                 :           0 :       return AST::Visibility::create_self (path_loc, vis_loc);
    2400                 :           2 :     case SUPER:
    2401                 :           2 :       lexer.skip_token ();
    2402                 :             : 
    2403                 :           2 :       skip_token (RIGHT_PAREN);
    2404                 :             : 
    2405                 :           2 :       return AST::Visibility::create_super (path_loc, vis_loc);
    2406                 :          22 :       case IN: {
    2407                 :          22 :         lexer.skip_token ();
    2408                 :             : 
    2409                 :             :         // parse the "in" path as well
    2410                 :          22 :         AST::SimplePath path = parse_simple_path ();
    2411                 :          22 :         if (path.is_empty ())
    2412                 :             :           {
    2413                 :           0 :             Error error (lexer.peek_token ()->get_locus (),
    2414                 :             :                          "missing path in pub(in path) visibility");
    2415                 :           0 :             add_error (std::move (error));
    2416                 :             : 
    2417                 :             :             // skip after somewhere?
    2418                 :           0 :             return AST::Visibility::create_error ();
    2419                 :           0 :           }
    2420                 :             : 
    2421                 :          22 :         skip_token (RIGHT_PAREN);
    2422                 :             : 
    2423                 :          22 :         return AST::Visibility::create_in_path (std::move (path), vis_loc);
    2424                 :          22 :       }
    2425                 :           0 :     default:
    2426                 :           0 :       add_error (Error (t->get_locus (), "unexpected token %qs in visibility",
    2427                 :             :                         t->get_token_description ()));
    2428                 :             : 
    2429                 :           0 :       lexer.skip_token ();
    2430                 :           0 :       return AST::Visibility::create_error ();
    2431                 :             :     }
    2432                 :          32 : }
    2433                 :             : 
    2434                 :             : // Parses a module - either a bodied module or a module defined in another file.
    2435                 :             : template <typename ManagedTokenSource>
    2436                 :             : std::unique_ptr<AST::Module>
    2437                 :         779 : Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
    2438                 :             :                                           AST::AttrVec outer_attrs)
    2439                 :             : {
    2440                 :         779 :   location_t locus = lexer.peek_token ()->get_locus ();
    2441                 :             : 
    2442                 :         779 :   Unsafety safety = Unsafety::Normal;
    2443                 :        1558 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    2444                 :             :     {
    2445                 :           2 :       safety = Unsafety::Unsafe;
    2446                 :           2 :       skip_token (UNSAFE);
    2447                 :             :     }
    2448                 :             : 
    2449                 :         779 :   skip_token (MOD);
    2450                 :             : 
    2451                 :         779 :   const_TokenPtr module_name = expect_token (IDENTIFIER);
    2452                 :         779 :   if (module_name == nullptr)
    2453                 :             :     {
    2454                 :           0 :       return nullptr;
    2455                 :             :     }
    2456                 :         779 :   Identifier name{module_name};
    2457                 :             : 
    2458                 :         779 :   const_TokenPtr t = lexer.peek_token ();
    2459                 :             : 
    2460                 :         779 :   switch (t->get_id ())
    2461                 :             :     {
    2462                 :          70 :     case SEMICOLON:
    2463                 :          70 :       lexer.skip_token ();
    2464                 :             : 
    2465                 :             :       // Construct an external module
    2466                 :             :       return std::unique_ptr<AST::Module> (
    2467                 :         210 :         new AST::Module (std::move (name), std::move (vis),
    2468                 :             :                          std::move (outer_attrs), locus, safety,
    2469                 :         210 :                          lexer.get_filename (), inline_module_stack));
    2470                 :         709 :       case LEFT_CURLY: {
    2471                 :         709 :         lexer.skip_token ();
    2472                 :             : 
    2473                 :             :         // parse inner attributes
    2474                 :         709 :         AST::AttrVec inner_attrs = parse_inner_attributes ();
    2475                 :             : 
    2476                 :         709 :         std::string default_path = name.as_string ();
    2477                 :             : 
    2478                 :         709 :         if (inline_module_stack.empty ())
    2479                 :             :           {
    2480                 :         497 :             std::string filename = lexer.get_filename ();
    2481                 :         497 :             auto slash_idx = filename.rfind (file_separator);
    2482                 :         497 :             if (slash_idx == std::string::npos)
    2483                 :             :               slash_idx = 0;
    2484                 :             :             else
    2485                 :         497 :               slash_idx++;
    2486                 :         497 :             filename = filename.substr (slash_idx);
    2487                 :             : 
    2488                 :         497 :             std::string subdir;
    2489                 :         497 :             if (get_file_subdir (filename, subdir))
    2490                 :         497 :               default_path = subdir + file_separator + name.as_string ();
    2491                 :         497 :           }
    2492                 :             : 
    2493                 :         709 :         std::string module_path_name
    2494                 :             :           = extract_module_path (inner_attrs, outer_attrs, default_path);
    2495                 :         709 :         InlineModuleStackScope scope (*this, std::move (module_path_name));
    2496                 :             : 
    2497                 :             :         // parse items
    2498                 :         709 :         std::vector<std::unique_ptr<AST::Item>> items;
    2499                 :         709 :         const_TokenPtr tok = lexer.peek_token ();
    2500                 :        1842 :         while (tok->get_id () != RIGHT_CURLY)
    2501                 :             :           {
    2502                 :        1133 :             std::unique_ptr<AST::Item> item = parse_item (false);
    2503                 :        1133 :             if (item == nullptr)
    2504                 :             :               {
    2505                 :           0 :                 Error error (tok->get_locus (),
    2506                 :             :                              "failed to parse item in module");
    2507                 :           0 :                 add_error (std::move (error));
    2508                 :             : 
    2509                 :           0 :                 return nullptr;
    2510                 :           0 :               }
    2511                 :             : 
    2512                 :        1133 :             items.push_back (std::move (item));
    2513                 :             : 
    2514                 :        1133 :             tok = lexer.peek_token ();
    2515                 :             :           }
    2516                 :             : 
    2517                 :         709 :         if (!skip_token (RIGHT_CURLY))
    2518                 :             :           {
    2519                 :             :             // skip somewhere?
    2520                 :           0 :             return nullptr;
    2521                 :             :           }
    2522                 :             : 
    2523                 :             :         return std::unique_ptr<AST::Module> (
    2524                 :         709 :           new AST::Module (std::move (name), locus, std::move (items),
    2525                 :             :                            std::move (vis), safety, std::move (inner_attrs),
    2526                 :         709 :                            std::move (outer_attrs))); // module name?
    2527                 :         709 :       }
    2528                 :           0 :     default:
    2529                 :           0 :       add_error (
    2530                 :           0 :         Error (t->get_locus (),
    2531                 :             :                "unexpected token %qs in module declaration/definition item",
    2532                 :             :                t->get_token_description ()));
    2533                 :             : 
    2534                 :           0 :       lexer.skip_token ();
    2535                 :           0 :       return nullptr;
    2536                 :             :     }
    2537                 :         779 : }
    2538                 :             : 
    2539                 :             : // Parses an extern crate declaration (dependency on external crate)
    2540                 :             : template <typename ManagedTokenSource>
    2541                 :             : std::unique_ptr<AST::ExternCrate>
    2542                 :          30 : Parser<ManagedTokenSource>::parse_extern_crate (AST::Visibility vis,
    2543                 :             :                                                 AST::AttrVec outer_attrs)
    2544                 :             : {
    2545                 :          30 :   location_t locus = lexer.peek_token ()->get_locus ();
    2546                 :          30 :   if (!skip_token (EXTERN_KW))
    2547                 :             :     {
    2548                 :           0 :       skip_after_semicolon ();
    2549                 :           0 :       return nullptr;
    2550                 :             :     }
    2551                 :             : 
    2552                 :          30 :   if (!skip_token (CRATE))
    2553                 :             :     {
    2554                 :           0 :       skip_after_semicolon ();
    2555                 :           0 :       return nullptr;
    2556                 :             :     }
    2557                 :             : 
    2558                 :             :   /* parse crate reference name - this has its own syntactical rule in reference
    2559                 :             :    * but seems to not be used elsewhere, so i'm putting it here */
    2560                 :          30 :   const_TokenPtr crate_name_tok = lexer.peek_token ();
    2561                 :          30 :   std::string crate_name;
    2562                 :             : 
    2563                 :          30 :   switch (crate_name_tok->get_id ())
    2564                 :             :     {
    2565                 :          30 :     case IDENTIFIER:
    2566                 :          30 :       crate_name = crate_name_tok->get_str ();
    2567                 :          30 :       lexer.skip_token ();
    2568                 :             :       break;
    2569                 :           0 :     case SELF:
    2570                 :           0 :       crate_name = Values::Keywords::SELF;
    2571                 :           0 :       lexer.skip_token ();
    2572                 :             :       break;
    2573                 :           0 :     default:
    2574                 :           0 :       add_error (
    2575                 :           0 :         Error (crate_name_tok->get_locus (),
    2576                 :             :                "expecting crate name (identifier or %<self%>), found %qs",
    2577                 :             :                crate_name_tok->get_token_description ()));
    2578                 :             : 
    2579                 :           0 :       skip_after_semicolon ();
    2580                 :           0 :       return nullptr;
    2581                 :             :     }
    2582                 :             : 
    2583                 :             :   // don't parse as clause if it doesn't exist
    2584                 :          60 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    2585                 :             :     {
    2586                 :          30 :       lexer.skip_token ();
    2587                 :             : 
    2588                 :             :       return std::unique_ptr<AST::ExternCrate> (
    2589                 :          30 :         new AST::ExternCrate (std::move (crate_name), std::move (vis),
    2590                 :          30 :                               std::move (outer_attrs), locus));
    2591                 :             :     }
    2592                 :             : 
    2593                 :             :   /* parse as clause - this also has its own syntactical rule in reference and
    2594                 :             :    * also seems to not be used elsewhere, so including here again. */
    2595                 :           0 :   if (!skip_token (AS))
    2596                 :             :     {
    2597                 :           0 :       skip_after_semicolon ();
    2598                 :           0 :       return nullptr;
    2599                 :             :     }
    2600                 :             : 
    2601                 :           0 :   const_TokenPtr as_name_tok = lexer.peek_token ();
    2602                 :           0 :   std::string as_name;
    2603                 :             : 
    2604                 :           0 :   switch (as_name_tok->get_id ())
    2605                 :             :     {
    2606                 :           0 :     case IDENTIFIER:
    2607                 :           0 :       as_name = as_name_tok->get_str ();
    2608                 :           0 :       lexer.skip_token ();
    2609                 :             :       break;
    2610                 :           0 :     case UNDERSCORE:
    2611                 :           0 :       as_name = Values::Keywords::UNDERSCORE;
    2612                 :           0 :       lexer.skip_token ();
    2613                 :             :       break;
    2614                 :           0 :     default:
    2615                 :           0 :       add_error (
    2616                 :           0 :         Error (as_name_tok->get_locus (),
    2617                 :             :                "expecting as clause name (identifier or %<_%>), found %qs",
    2618                 :             :                as_name_tok->get_token_description ()));
    2619                 :             : 
    2620                 :           0 :       skip_after_semicolon ();
    2621                 :           0 :       return nullptr;
    2622                 :             :     }
    2623                 :             : 
    2624                 :           0 :   if (!skip_token (SEMICOLON))
    2625                 :             :     {
    2626                 :           0 :       skip_after_semicolon ();
    2627                 :           0 :       return nullptr;
    2628                 :             :     }
    2629                 :             : 
    2630                 :             :   return std::unique_ptr<AST::ExternCrate> (
    2631                 :           0 :     new AST::ExternCrate (std::move (crate_name), std::move (vis),
    2632                 :           0 :                           std::move (outer_attrs), locus, std::move (as_name)));
    2633                 :          60 : }
    2634                 :             : 
    2635                 :             : // Parses a use declaration.
    2636                 :             : template <typename ManagedTokenSource>
    2637                 :             : std::unique_ptr<AST::UseDeclaration>
    2638                 :         200 : Parser<ManagedTokenSource>::parse_use_decl (AST::Visibility vis,
    2639                 :             :                                             AST::AttrVec outer_attrs)
    2640                 :             : {
    2641                 :         200 :   location_t locus = lexer.peek_token ()->get_locus ();
    2642                 :         200 :   if (!skip_token (USE))
    2643                 :             :     {
    2644                 :           0 :       skip_after_semicolon ();
    2645                 :           0 :       return nullptr;
    2646                 :             :     }
    2647                 :             : 
    2648                 :             :   // parse use tree, which is required
    2649                 :         200 :   std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    2650                 :         200 :   if (use_tree == nullptr)
    2651                 :             :     {
    2652                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    2653                 :             :                    "could not parse use tree in use declaration");
    2654                 :           0 :       add_error (std::move (error));
    2655                 :             : 
    2656                 :           0 :       skip_after_semicolon ();
    2657                 :           0 :       return nullptr;
    2658                 :           0 :     }
    2659                 :             : 
    2660                 :         200 :   if (!skip_token (SEMICOLON))
    2661                 :             :     {
    2662                 :           0 :       skip_after_semicolon ();
    2663                 :           0 :       return nullptr;
    2664                 :             :     }
    2665                 :             : 
    2666                 :             :   return std::unique_ptr<AST::UseDeclaration> (
    2667                 :         200 :     new AST::UseDeclaration (std::move (use_tree), std::move (vis),
    2668                 :         200 :                              std::move (outer_attrs), locus));
    2669                 :         200 : }
    2670                 :             : 
    2671                 :             : // Parses a use tree (which can be recursive and is actually a base class).
    2672                 :             : template <typename ManagedTokenSource>
    2673                 :             : std::unique_ptr<AST::UseTree>
    2674                 :         298 : Parser<ManagedTokenSource>::parse_use_tree ()
    2675                 :             : {
    2676                 :             :   /* potential syntax definitions in attempt to get algorithm:
    2677                 :             :    *  Glob:
    2678                 :             :    *      <- SimplePath :: *
    2679                 :             :    *      <- :: *
    2680                 :             :    *      <- *
    2681                 :             :    *  Nested tree thing:
    2682                 :             :    *      <- SimplePath :: { COMPLICATED_INNER_TREE_THING }
    2683                 :             :    *      <- :: COMPLICATED_INNER_TREE_THING }
    2684                 :             :    *      <- { COMPLICATED_INNER_TREE_THING }
    2685                 :             :    *  Rebind thing:
    2686                 :             :    *      <- SimplePath as IDENTIFIER
    2687                 :             :    *      <- SimplePath as _
    2688                 :             :    *      <- SimplePath
    2689                 :             :    */
    2690                 :             : 
    2691                 :             :   /* current plan of attack: try to parse SimplePath first - if fails, one of
    2692                 :             :    * top two then try parse :: - if fails, one of top two. Next is deciding
    2693                 :             :    * character for top two. */
    2694                 :             : 
    2695                 :             :   /* Thus, parsing smaller parts of use tree may require feeding into function
    2696                 :             :    * via parameters (or could handle all in this single function because other
    2697                 :             :    * use tree types aren't recognised as separate in the spec) */
    2698                 :             : 
    2699                 :             :   // TODO: I think this function is too complex, probably should split it
    2700                 :             : 
    2701                 :         298 :   location_t locus = lexer.peek_token ()->get_locus ();
    2702                 :             : 
    2703                 :             :   // bool has_path = false;
    2704                 :         298 :   AST::SimplePath path = parse_simple_path ();
    2705                 :             : 
    2706                 :         298 :   if (path.is_empty ())
    2707                 :             :     {
    2708                 :             :       // has no path, so must be glob or nested tree UseTree type
    2709                 :             : 
    2710                 :           0 :       bool is_global = false;
    2711                 :             : 
    2712                 :             :       // check for global scope resolution operator
    2713                 :           0 :       if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
    2714                 :             :         {
    2715                 :           0 :           lexer.skip_token ();
    2716                 :           0 :           is_global = true;
    2717                 :             :         }
    2718                 :             : 
    2719                 :           0 :       const_TokenPtr t = lexer.peek_token ();
    2720                 :           0 :       switch (t->get_id ())
    2721                 :             :         {
    2722                 :           0 :         case ASTERISK:
    2723                 :             :           // glob UseTree type
    2724                 :           0 :           lexer.skip_token ();
    2725                 :             : 
    2726                 :           0 :           if (is_global)
    2727                 :           0 :             return std::unique_ptr<AST::UseTreeGlob> (
    2728                 :           0 :               new AST::UseTreeGlob (AST::UseTreeGlob::GLOBAL,
    2729                 :           0 :                                     AST::SimplePath::create_empty (), locus));
    2730                 :             :           else
    2731                 :           0 :             return std::unique_ptr<AST::UseTreeGlob> (
    2732                 :           0 :               new AST::UseTreeGlob (AST::UseTreeGlob::NO_PATH,
    2733                 :           0 :                                     AST::SimplePath::create_empty (), locus));
    2734                 :           0 :           case LEFT_CURLY: {
    2735                 :             :             // nested tree UseTree type
    2736                 :           0 :             lexer.skip_token ();
    2737                 :             : 
    2738                 :           0 :             std::vector<std::unique_ptr<AST::UseTree>> use_trees;
    2739                 :             : 
    2740                 :           0 :             const_TokenPtr t = lexer.peek_token ();
    2741                 :           0 :             while (t->get_id () != RIGHT_CURLY)
    2742                 :             :               {
    2743                 :           0 :                 std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    2744                 :           0 :                 if (use_tree == nullptr)
    2745                 :             :                   {
    2746                 :             :                     break;
    2747                 :             :                   }
    2748                 :             : 
    2749                 :           0 :                 use_trees.push_back (std::move (use_tree));
    2750                 :             : 
    2751                 :           0 :                 if (lexer.peek_token ()->get_id () != COMMA)
    2752                 :             :                   break;
    2753                 :             : 
    2754                 :           0 :                 lexer.skip_token ();
    2755                 :           0 :                 t = lexer.peek_token ();
    2756                 :             :               }
    2757                 :             : 
    2758                 :             :             // skip end curly delimiter
    2759                 :           0 :             if (!skip_token (RIGHT_CURLY))
    2760                 :             :               {
    2761                 :             :                 // skip after somewhere?
    2762                 :           0 :                 return nullptr;
    2763                 :             :               }
    2764                 :             : 
    2765                 :           0 :             if (is_global)
    2766                 :           0 :               return std::unique_ptr<AST::UseTreeList> (
    2767                 :           0 :                 new AST::UseTreeList (AST::UseTreeList::GLOBAL,
    2768                 :           0 :                                       AST::SimplePath::create_empty (),
    2769                 :           0 :                                       std::move (use_trees), locus));
    2770                 :             :             else
    2771                 :           0 :               return std::unique_ptr<AST::UseTreeList> (
    2772                 :           0 :                 new AST::UseTreeList (AST::UseTreeList::NO_PATH,
    2773                 :           0 :                                       AST::SimplePath::create_empty (),
    2774                 :           0 :                                       std::move (use_trees), locus));
    2775                 :           0 :           }
    2776                 :           0 :         case AS:
    2777                 :             :           // this is not allowed
    2778                 :           0 :           add_error (Error (
    2779                 :             :             t->get_locus (),
    2780                 :             :             "use declaration with rebind %<as%> requires a valid simple path - "
    2781                 :             :             "none found"));
    2782                 :             : 
    2783                 :           0 :           skip_after_semicolon ();
    2784                 :           0 :           return nullptr;
    2785                 :           0 :         default:
    2786                 :           0 :           add_error (Error (t->get_locus (),
    2787                 :             :                             "unexpected token %qs in use tree with "
    2788                 :             :                             "no valid simple path (i.e. list"
    2789                 :             :                             " or glob use tree)",
    2790                 :             :                             t->get_token_description ()));
    2791                 :             : 
    2792                 :           0 :           skip_after_semicolon ();
    2793                 :           0 :           return nullptr;
    2794                 :             :         }
    2795                 :           0 :     }
    2796                 :             :   else
    2797                 :             :     {
    2798                 :             :       /* Due to aforementioned implementation issues, the trailing :: token is
    2799                 :             :        * consumed by the path, so it can not be used as a disambiguator.
    2800                 :             :        * NOPE, not true anymore - TODO what are the consequences of this? */
    2801                 :             : 
    2802                 :         298 :       const_TokenPtr t = lexer.peek_token ();
    2803                 :         298 :       switch (t->get_id ())
    2804                 :             :         {
    2805                 :          12 :         case ASTERISK:
    2806                 :             :           // glob UseTree type
    2807                 :          12 :           lexer.skip_token ();
    2808                 :             : 
    2809                 :          12 :           return std::unique_ptr<AST::UseTreeGlob> (
    2810                 :          24 :             new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
    2811                 :          12 :                                   std::move (path), locus));
    2812                 :          34 :           case LEFT_CURLY: {
    2813                 :             :             // nested tree UseTree type
    2814                 :          34 :             lexer.skip_token ();
    2815                 :             : 
    2816                 :          34 :             std::vector<std::unique_ptr<AST::UseTree>> use_trees;
    2817                 :             : 
    2818                 :             :             // TODO: think of better control structure
    2819                 :          34 :             const_TokenPtr t = lexer.peek_token ();
    2820                 :         132 :             while (t->get_id () != RIGHT_CURLY)
    2821                 :             :               {
    2822                 :          98 :                 std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
    2823                 :          98 :                 if (use_tree == nullptr)
    2824                 :             :                   {
    2825                 :             :                     break;
    2826                 :             :                   }
    2827                 :             : 
    2828                 :          98 :                 use_trees.push_back (std::move (use_tree));
    2829                 :             : 
    2830                 :         196 :                 if (lexer.peek_token ()->get_id () != COMMA)
    2831                 :             :                   break;
    2832                 :             : 
    2833                 :          64 :                 lexer.skip_token ();
    2834                 :          64 :                 t = lexer.peek_token ();
    2835                 :             :               }
    2836                 :             : 
    2837                 :             :             // skip end curly delimiter
    2838                 :          34 :             if (!skip_token (RIGHT_CURLY))
    2839                 :             :               {
    2840                 :             :                 // skip after somewhere?
    2841                 :           0 :                 return nullptr;
    2842                 :             :               }
    2843                 :             : 
    2844                 :          34 :             return std::unique_ptr<AST::UseTreeList> (
    2845                 :          68 :               new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
    2846                 :             :                                     std::move (path), std::move (use_trees),
    2847                 :          34 :                                     locus));
    2848                 :          34 :           }
    2849                 :           4 :           case AS: {
    2850                 :             :             // rebind UseTree type
    2851                 :           4 :             lexer.skip_token ();
    2852                 :             : 
    2853                 :           4 :             const_TokenPtr t = lexer.peek_token ();
    2854                 :           4 :             switch (t->get_id ())
    2855                 :             :               {
    2856                 :           4 :               case IDENTIFIER:
    2857                 :             :                 // skip lexer token
    2858                 :           4 :                 lexer.skip_token ();
    2859                 :             : 
    2860                 :           4 :                 return std::unique_ptr<AST::UseTreeRebind> (
    2861                 :          16 :                   new AST::UseTreeRebind (AST::UseTreeRebind::IDENTIFIER,
    2862                 :           4 :                                           std::move (path), locus, t));
    2863                 :           0 :               case UNDERSCORE:
    2864                 :             :                 // skip lexer token
    2865                 :           0 :                 lexer.skip_token ();
    2866                 :             : 
    2867                 :           0 :                 return std::unique_ptr<AST::UseTreeRebind> (
    2868                 :           0 :                   new AST::UseTreeRebind (AST::UseTreeRebind::WILDCARD,
    2869                 :             :                                           std::move (path), locus,
    2870                 :             :                                           {Values::Keywords::UNDERSCORE,
    2871                 :           0 :                                            t->get_locus ()}));
    2872                 :           0 :               default:
    2873                 :           0 :                 add_error (Error (
    2874                 :             :                   t->get_locus (),
    2875                 :             :                   "unexpected token %qs in use tree with as clause - expected "
    2876                 :             :                   "identifier or %<_%>",
    2877                 :             :                   t->get_token_description ()));
    2878                 :             : 
    2879                 :           0 :                 skip_after_semicolon ();
    2880                 :           0 :                 return nullptr;
    2881                 :             :               }
    2882                 :           4 :           }
    2883                 :         150 :         case SEMICOLON:
    2884                 :             :           // rebind UseTree type without rebinding - path only
    2885                 :             : 
    2886                 :             :           // don't skip semicolon - handled in parse_use_tree
    2887                 :             :           // lexer.skip_token();
    2888                 :             : 
    2889                 :         150 :           return std::unique_ptr<AST::UseTreeRebind> (
    2890                 :         300 :             new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
    2891                 :         150 :                                     locus));
    2892                 :          98 :         case COMMA:
    2893                 :             :         case RIGHT_CURLY:
    2894                 :             :           // this may occur in recursive calls - assume it is ok and ignore it
    2895                 :          98 :           return std::unique_ptr<AST::UseTreeRebind> (
    2896                 :         196 :             new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
    2897                 :          98 :                                     locus));
    2898                 :           0 :         default:
    2899                 :           0 :           add_error (Error (t->get_locus (),
    2900                 :             :                             "unexpected token %qs in use tree with valid path",
    2901                 :             :                             t->get_token_description ()));
    2902                 :             : 
    2903                 :             :           // skip_after_semicolon();
    2904                 :           0 :           return nullptr;
    2905                 :             :         }
    2906                 :         298 :     }
    2907                 :         298 : }
    2908                 :             : 
    2909                 :             : // Parses a function (not a method).
    2910                 :             : template <typename ManagedTokenSource>
    2911                 :             : std::unique_ptr<AST::Function>
    2912                 :        9413 : Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
    2913                 :             :                                             AST::AttrVec outer_attrs,
    2914                 :             :                                             bool is_external)
    2915                 :             : {
    2916                 :        9413 :   location_t locus = lexer.peek_token ()->get_locus ();
    2917                 :             :   // Get qualifiers for function if they exist
    2918                 :        9413 :   AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
    2919                 :             : 
    2920                 :        9413 :   skip_token (FN_KW);
    2921                 :             : 
    2922                 :             :   // Save function name token
    2923                 :        9413 :   const_TokenPtr function_name_tok = expect_token (IDENTIFIER);
    2924                 :        9413 :   if (function_name_tok == nullptr)
    2925                 :             :     {
    2926                 :           0 :       skip_after_next_block ();
    2927                 :           0 :       return nullptr;
    2928                 :             :     }
    2929                 :        9413 :   Identifier function_name{function_name_tok};
    2930                 :             : 
    2931                 :             :   // parse generic params - if exist
    2932                 :        9413 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    2933                 :             :     = parse_generic_params_in_angles ();
    2934                 :             : 
    2935                 :        9413 :   if (!skip_token (LEFT_PAREN))
    2936                 :             :     {
    2937                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    2938                 :             :                    "function declaration missing opening parentheses before "
    2939                 :             :                    "parameter list");
    2940                 :           0 :       add_error (std::move (error));
    2941                 :             : 
    2942                 :           0 :       skip_after_next_block ();
    2943                 :           0 :       return nullptr;
    2944                 :           0 :     }
    2945                 :             : 
    2946                 :        9413 :   auto initial_param = parse_self_param ();
    2947                 :             : 
    2948                 :        9413 :   if (!initial_param.has_value ()
    2949                 :        9413 :       && initial_param.error () != ParseSelfError::NOT_SELF)
    2950                 :           0 :     return nullptr;
    2951                 :             : 
    2952                 :       10484 :   if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
    2953                 :         451 :     skip_token ();
    2954                 :             : 
    2955                 :             :   // parse function parameters (only if next token isn't right paren)
    2956                 :        9413 :   std::vector<std::unique_ptr<AST::Param>> function_params;
    2957                 :             : 
    2958                 :       18826 :   if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
    2959                 :             :     function_params
    2960                 :        3478 :       = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    2961                 :             : 
    2962                 :        9413 :   if (initial_param.has_value ())
    2963                 :        1071 :     function_params.insert (function_params.begin (),
    2964                 :        1071 :                             std::move (*initial_param));
    2965                 :             : 
    2966                 :        9413 :   if (!skip_token (RIGHT_PAREN))
    2967                 :             :     {
    2968                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    2969                 :             :                    "function declaration missing closing parentheses after "
    2970                 :             :                    "parameter list");
    2971                 :           0 :       add_error (std::move (error));
    2972                 :             : 
    2973                 :           0 :       skip_after_next_block ();
    2974                 :           0 :       return nullptr;
    2975                 :           0 :     }
    2976                 :             : 
    2977                 :             :   // parse function return type - if exists
    2978                 :        9413 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    2979                 :             : 
    2980                 :             :   // parse where clause - if exists
    2981                 :        9413 :   AST::WhereClause where_clause = parse_where_clause ();
    2982                 :             : 
    2983                 :        9413 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    2984                 :       18826 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    2985                 :        2858 :     lexer.skip_token ();
    2986                 :             :   else
    2987                 :             :     {
    2988                 :        6555 :       std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
    2989                 :        6555 :       if (block_expr != nullptr)
    2990                 :        6529 :         body = std::move (block_expr);
    2991                 :        6555 :     }
    2992                 :             : 
    2993                 :             :   return std::unique_ptr<AST::Function> (
    2994                 :       25355 :     new AST::Function (std::move (function_name), std::move (qualifiers),
    2995                 :             :                        std::move (generic_params), std::move (function_params),
    2996                 :             :                        std::move (return_type), std::move (where_clause),
    2997                 :             :                        std::move (body), std::move (vis),
    2998                 :        9413 :                        std::move (outer_attrs), locus, false, is_external));
    2999                 :       28239 : }
    3000                 :             : 
    3001                 :             : // Parses function or method qualifiers (i.e. const, unsafe, and extern).
    3002                 :             : template <typename ManagedTokenSource>
    3003                 :             : AST::FunctionQualifiers
    3004                 :       13411 : Parser<ManagedTokenSource>::parse_function_qualifiers ()
    3005                 :             : {
    3006                 :       13411 :   Async async_status = Async::No;
    3007                 :       13411 :   Const const_status = Const::No;
    3008                 :       13411 :   Unsafety unsafe_status = Unsafety::Normal;
    3009                 :       13411 :   bool has_extern = false;
    3010                 :       13411 :   std::string abi;
    3011                 :             : 
    3012                 :       13411 :   const_TokenPtr t;
    3013                 :             :   location_t locus;
    3014                 :             :   // Check in order of const, unsafe, then extern
    3015                 :       40233 :   for (int i = 0; i < 2; i++)
    3016                 :             :     {
    3017                 :       26822 :       t = lexer.peek_token ();
    3018                 :       26822 :       locus = t->get_locus ();
    3019                 :             : 
    3020                 :       26822 :       switch (t->get_id ())
    3021                 :             :         {
    3022                 :         650 :         case CONST:
    3023                 :         650 :           lexer.skip_token ();
    3024                 :         650 :           const_status = Const::Yes;
    3025                 :         650 :           break;
    3026                 :          16 :         case ASYNC:
    3027                 :          16 :           lexer.skip_token ();
    3028                 :          16 :           async_status = Async::Yes;
    3029                 :          16 :           break;
    3030                 :             :         default:
    3031                 :             :           // const status is still none
    3032                 :             :           break;
    3033                 :             :         }
    3034                 :             :     }
    3035                 :             : 
    3036                 :       26822 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    3037                 :             :     {
    3038                 :         382 :       lexer.skip_token ();
    3039                 :         382 :       unsafe_status = Unsafety::Unsafe;
    3040                 :             :     }
    3041                 :             : 
    3042                 :       26822 :   if (lexer.peek_token ()->get_id () == EXTERN_KW)
    3043                 :             :     {
    3044                 :          61 :       lexer.skip_token ();
    3045                 :          61 :       has_extern = true;
    3046                 :             : 
    3047                 :             :       // detect optional abi name
    3048                 :          61 :       const_TokenPtr next_tok = lexer.peek_token ();
    3049                 :          61 :       if (next_tok->get_id () == STRING_LITERAL)
    3050                 :             :         {
    3051                 :          61 :           lexer.skip_token ();
    3052                 :          61 :           abi = next_tok->get_str ();
    3053                 :             :         }
    3054                 :          61 :     }
    3055                 :             : 
    3056                 :       26822 :   return AST::FunctionQualifiers (locus, async_status, const_status,
    3057                 :       13411 :                                   unsafe_status, has_extern, std::move (abi));
    3058                 :       13411 : }
    3059                 :             : 
    3060                 :             : // Parses generic (lifetime or type) params inside angle brackets (optional).
    3061                 :             : template <typename ManagedTokenSource>
    3062                 :             : std::vector<std::unique_ptr<AST::GenericParam>>
    3063                 :       23393 : Parser<ManagedTokenSource>::parse_generic_params_in_angles ()
    3064                 :             : {
    3065                 :       46786 :   if (lexer.peek_token ()->get_id () != LEFT_ANGLE)
    3066                 :             :     {
    3067                 :             :       // seems to be no generic params, so exit with empty vector
    3068                 :       20110 :       return std::vector<std::unique_ptr<AST::GenericParam>> ();
    3069                 :             :     }
    3070                 :        3283 :   lexer.skip_token ();
    3071                 :             : 
    3072                 :             :   // DEBUG:
    3073                 :        3283 :   rust_debug ("skipped left angle in generic param");
    3074                 :             : 
    3075                 :        3283 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    3076                 :             :     = parse_generic_params (is_right_angle_tok);
    3077                 :             : 
    3078                 :             :   // DEBUG:
    3079                 :        3283 :   rust_debug ("finished parsing actual generic params (i.e. inside angles)");
    3080                 :             : 
    3081                 :        3283 :   if (!skip_generics_right_angle ())
    3082                 :             :     {
    3083                 :             :       // DEBUG
    3084                 :           2 :       rust_debug ("failed to skip generics right angle - returning empty "
    3085                 :             :                   "generic params");
    3086                 :             : 
    3087                 :           2 :       return std::vector<std::unique_ptr<AST::GenericParam>> ();
    3088                 :             :     }
    3089                 :             : 
    3090                 :        3281 :   return generic_params;
    3091                 :        3283 : }
    3092                 :             : 
    3093                 :             : template <typename ManagedTokenSource>
    3094                 :             : template <typename EndTokenPred>
    3095                 :             : std::unique_ptr<AST::GenericParam>
    3096                 :        3634 : Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
    3097                 :             : {
    3098                 :        3634 :   auto outer_attrs = parse_outer_attributes ();
    3099                 :        3634 :   std::unique_ptr<AST::GenericParam> param;
    3100                 :        3634 :   auto token = lexer.peek_token ();
    3101                 :             : 
    3102                 :        3634 :   switch (token->get_id ())
    3103                 :             :     {
    3104                 :         377 :       case LIFETIME: {
    3105                 :         377 :         auto lifetime = parse_lifetime (false);
    3106                 :         377 :         if (lifetime.is_error ())
    3107                 :             :           {
    3108                 :           0 :             rust_error_at (
    3109                 :             :               token->get_locus (),
    3110                 :             :               "failed to parse lifetime in generic parameter list");
    3111                 :           0 :             return nullptr;
    3112                 :             :           }
    3113                 :             : 
    3114                 :         377 :         std::vector<AST::Lifetime> lifetime_bounds;
    3115                 :         754 :         if (lexer.peek_token ()->get_id () == COLON)
    3116                 :             :           {
    3117                 :           2 :             lexer.skip_token ();
    3118                 :             :             // parse required bounds
    3119                 :             :             lifetime_bounds
    3120                 :           2 :               = parse_lifetime_bounds ([is_end_token] (TokenId id) {
    3121                 :           2 :                   return is_end_token (id) || id == COMMA;
    3122                 :             :                 });
    3123                 :             :           }
    3124                 :             : 
    3125                 :         377 :         param = std::unique_ptr<AST::LifetimeParam> (new AST::LifetimeParam (
    3126                 :             :           std::move (lifetime), std::move (lifetime_bounds),
    3127                 :         377 :           std::move (outer_attrs), token->get_locus ()));
    3128                 :             :         break;
    3129                 :         377 :       }
    3130                 :        3193 :       case IDENTIFIER: {
    3131                 :        3193 :         auto type_ident = token->get_str ();
    3132                 :        3193 :         lexer.skip_token ();
    3133                 :             : 
    3134                 :        3193 :         std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    3135                 :        6386 :         if (lexer.peek_token ()->get_id () == COLON)
    3136                 :             :           {
    3137                 :         325 :             lexer.skip_token ();
    3138                 :             : 
    3139                 :             :             // parse optional type param bounds
    3140                 :         325 :             type_param_bounds = parse_type_param_bounds ();
    3141                 :             :           }
    3142                 :             : 
    3143                 :        3193 :         std::unique_ptr<AST::Type> type = nullptr;
    3144                 :        6386 :         if (lexer.peek_token ()->get_id () == EQUAL)
    3145                 :             :           {
    3146                 :         130 :             lexer.skip_token ();
    3147                 :             : 
    3148                 :             :             // parse required type
    3149                 :         130 :             type = parse_type ();
    3150                 :         130 :             if (!type)
    3151                 :             :               {
    3152                 :           0 :                 rust_error_at (
    3153                 :           0 :                   lexer.peek_token ()->get_locus (),
    3154                 :             :                   "failed to parse type in type param in generic params");
    3155                 :           0 :                 return nullptr;
    3156                 :             :               }
    3157                 :             :           }
    3158                 :             : 
    3159                 :        3193 :         param = std::unique_ptr<AST::TypeParam> (
    3160                 :        6386 :           new AST::TypeParam (std::move (type_ident), token->get_locus (),
    3161                 :             :                               std::move (type_param_bounds), std::move (type),
    3162                 :        3193 :                               std::move (outer_attrs)));
    3163                 :             :         break;
    3164                 :        3193 :       }
    3165                 :          62 :       case CONST: {
    3166                 :          62 :         lexer.skip_token ();
    3167                 :             : 
    3168                 :          62 :         auto name_token = expect_token (IDENTIFIER);
    3169                 :             : 
    3170                 :         124 :         if (!name_token || !expect_token (COLON))
    3171                 :           2 :           return nullptr;
    3172                 :             : 
    3173                 :          60 :         auto type = parse_type ();
    3174                 :          60 :         if (!type)
    3175                 :           2 :           return nullptr;
    3176                 :             : 
    3177                 :             :         // optional default value
    3178                 :          58 :         auto default_expr = AST::GenericArg::create_error ();
    3179                 :         116 :         if (lexer.peek_token ()->get_id () == EQUAL)
    3180                 :             :           {
    3181                 :          34 :             lexer.skip_token ();
    3182                 :          34 :             auto tok = lexer.peek_token ();
    3183                 :          34 :             default_expr = parse_generic_arg ();
    3184                 :             : 
    3185                 :          34 :             if (default_expr.is_error ())
    3186                 :           2 :               rust_error_at (tok->get_locus (),
    3187                 :             :                              "invalid token for start of default value for "
    3188                 :             :                              "const generic parameter: expected %<block%>, "
    3189                 :             :                              "%<identifier%> or %<literal%>, got %qs",
    3190                 :             :                              token_id_to_str (tok->get_id ()));
    3191                 :             : 
    3192                 :             :             // At this point, we *know* that we are parsing a const
    3193                 :             :             // expression
    3194                 :          34 :             if (default_expr.get_kind () == AST::GenericArg::Kind::Either)
    3195                 :           2 :               default_expr = default_expr.disambiguate_to_const ();
    3196                 :          34 :           }
    3197                 :             : 
    3198                 :          58 :         param = std::unique_ptr<AST::ConstGenericParam> (
    3199                 :         116 :           new AST::ConstGenericParam (name_token->get_str (), std::move (type),
    3200                 :             :                                       default_expr, std::move (outer_attrs),
    3201                 :          58 :                                       token->get_locus ()));
    3202                 :             : 
    3203                 :             :         break;
    3204                 :         122 :       }
    3205                 :           2 :     default:
    3206                 :             :       // FIXME: Can we clean this last call with a method call?
    3207                 :           2 :       rust_error_at (token->get_locus (),
    3208                 :             :                      "unexpected token when parsing generic parameters: %qs",
    3209                 :           4 :                      token->as_string ().c_str ());
    3210                 :           2 :       return nullptr;
    3211                 :             :     }
    3212                 :             : 
    3213                 :        3628 :   return param;
    3214                 :        3634 : }
    3215                 :             : 
    3216                 :             : /* Parse generic (lifetime or type) params NOT INSIDE ANGLE BRACKETS!!! Almost
    3217                 :             :  * always parse_generic_params_in_angles is what is wanted. */
    3218                 :             : template <typename ManagedTokenSource>
    3219                 :             : template <typename EndTokenPred>
    3220                 :             : std::vector<std::unique_ptr<AST::GenericParam>>
    3221                 :        3283 : Parser<ManagedTokenSource>::parse_generic_params (EndTokenPred is_end_token)
    3222                 :             : {
    3223                 :        3283 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params;
    3224                 :             : 
    3225                 :             :   /* can't parse lifetime and type params separately due to lookahead issues
    3226                 :             :    * thus, parse them all here */
    3227                 :             : 
    3228                 :             :   /* HACK: used to retain attribute data if a lifetime param is tentatively
    3229                 :             :    * parsed but it turns out to be type param */
    3230                 :        3283 :   AST::Attribute parsed_outer_attr = AST::Attribute::create_empty ();
    3231                 :             : 
    3232                 :             :   // Did we parse a generic type param yet
    3233                 :        3283 :   auto type_seen = false;
    3234                 :             :   // Did the user write a lifetime parameter after a type one
    3235                 :        3283 :   auto order_error = false;
    3236                 :             : 
    3237                 :             :   // parse lifetime params
    3238                 :       17456 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3239                 :             :     {
    3240                 :        3634 :       auto param = parse_generic_param (is_end_token);
    3241                 :        3634 :       if (param)
    3242                 :             :         {
    3243                 :             :           // TODO: Handle `Const` here as well if necessary
    3244                 :        3628 :           if (param->get_kind () == AST::GenericParam::Kind::Type)
    3245                 :             :             type_seen = true;
    3246                 :         435 :           else if (param->get_kind () == AST::GenericParam::Kind::Lifetime
    3247                 :         435 :                    && type_seen)
    3248                 :             :             order_error = true;
    3249                 :             : 
    3250                 :        3628 :           generic_params.emplace_back (std::move (param));
    3251                 :        3628 :           maybe_skip_token (COMMA);
    3252                 :             :         }
    3253                 :             :       else
    3254                 :             :         break;
    3255                 :             :     }
    3256                 :             : 
    3257                 :             :   // FIXME: Add reordering hint
    3258                 :        3283 :   if (order_error)
    3259                 :             :     {
    3260                 :           2 :       Error error (generic_params.front ()->get_locus (),
    3261                 :             :                    "invalid order for generic parameters: lifetime parameters "
    3262                 :             :                    "must be declared prior to type and const parameters");
    3263                 :           2 :       add_error (std::move (error));
    3264                 :           2 :     }
    3265                 :             : 
    3266                 :             :   generic_params.shrink_to_fit ();
    3267                 :        3283 :   return generic_params;
    3268                 :        3283 : }
    3269                 :             : 
    3270                 :             : /* Parses lifetime generic parameters (pointers). Will also consume any
    3271                 :             :  * trailing comma. No extra checks for end token. */
    3272                 :             : template <typename ManagedTokenSource>
    3273                 :             : std::vector<std::unique_ptr<AST::LifetimeParam>>
    3274                 :           0 : Parser<ManagedTokenSource>::parse_lifetime_params ()
    3275                 :             : {
    3276                 :           0 :   std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
    3277                 :             : 
    3278                 :           0 :   while (lexer.peek_token ()->get_id () != END_OF_FILE)
    3279                 :             :     {
    3280                 :           0 :       AST::LifetimeParam lifetime_param = parse_lifetime_param ();
    3281                 :             : 
    3282                 :           0 :       if (lifetime_param.is_error ())
    3283                 :             :         {
    3284                 :             :           // can't treat as error as only way to get out with trailing comma
    3285                 :             :           break;
    3286                 :             :         }
    3287                 :             : 
    3288                 :           0 :       lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> (
    3289                 :           0 :         new AST::LifetimeParam (std::move (lifetime_param))));
    3290                 :             : 
    3291                 :           0 :       if (lexer.peek_token ()->get_id () != COMMA)
    3292                 :             :         break;
    3293                 :             : 
    3294                 :             :       // skip commas, including trailing commas
    3295                 :           0 :       lexer.skip_token ();
    3296                 :             :     }
    3297                 :             : 
    3298                 :             :   lifetime_params.shrink_to_fit ();
    3299                 :             : 
    3300                 :           0 :   return lifetime_params;
    3301                 :             : }
    3302                 :             : 
    3303                 :             : /* Parses lifetime generic parameters (pointers). Will also consume any
    3304                 :             :  * trailing comma. Has extra is_end_token predicate checking. */
    3305                 :             : template <typename ManagedTokenSource>
    3306                 :             : template <typename EndTokenPred>
    3307                 :             : std::vector<std::unique_ptr<AST::LifetimeParam>>
    3308                 :             : Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token)
    3309                 :             : {
    3310                 :             :   std::vector<std::unique_ptr<AST::LifetimeParam>> lifetime_params;
    3311                 :             : 
    3312                 :             :   // if end_token is not specified, it defaults to EOF, so should work fine
    3313                 :             :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3314                 :             :     {
    3315                 :             :       AST::LifetimeParam lifetime_param = parse_lifetime_param ();
    3316                 :             : 
    3317                 :             :       if (lifetime_param.is_error ())
    3318                 :             :         {
    3319                 :             :           /* TODO: is it worth throwing away all lifetime params just because
    3320                 :             :            * one failed? */
    3321                 :             :           Error error (lexer.peek_token ()->get_locus (),
    3322                 :             :                        "failed to parse lifetime param in lifetime params");
    3323                 :             :           add_error (std::move (error));
    3324                 :             : 
    3325                 :             :           return {};
    3326                 :             :         }
    3327                 :             : 
    3328                 :             :       lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> (
    3329                 :             :         new AST::LifetimeParam (std::move (lifetime_param))));
    3330                 :             : 
    3331                 :             :       if (lexer.peek_token ()->get_id () != COMMA)
    3332                 :             :         break;
    3333                 :             : 
    3334                 :             :       // skip commas, including trailing commas
    3335                 :             :       lexer.skip_token ();
    3336                 :             :     }
    3337                 :             : 
    3338                 :             :   lifetime_params.shrink_to_fit ();
    3339                 :             : 
    3340                 :             :   return lifetime_params;
    3341                 :             : }
    3342                 :             : 
    3343                 :             : /* Parses lifetime generic parameters (objects). Will also consume any
    3344                 :             :  * trailing comma. No extra checks for end token.
    3345                 :             :  * TODO: is this best solution? implements most of the same algorithm. */
    3346                 :             : template <typename ManagedTokenSource>
    3347                 :             : std::vector<AST::LifetimeParam>
    3348                 :             : Parser<ManagedTokenSource>::parse_lifetime_params_objs ()
    3349                 :             : {
    3350                 :             :   std::vector<AST::LifetimeParam> lifetime_params;
    3351                 :             : 
    3352                 :             :   // bad control structure as end token cannot be guaranteed
    3353                 :             :   while (true)
    3354                 :             :     {
    3355                 :             :       AST::LifetimeParam lifetime_param = parse_lifetime_param ();
    3356                 :             : 
    3357                 :             :       if (lifetime_param.is_error ())
    3358                 :             :         {
    3359                 :             :           // not an error as only way to exit if trailing comma
    3360                 :             :           break;
    3361                 :             :         }
    3362                 :             : 
    3363                 :             :       lifetime_params.push_back (std::move (lifetime_param));
    3364                 :             : 
    3365                 :             :       if (lexer.peek_token ()->get_id () != COMMA)
    3366                 :             :         break;
    3367                 :             : 
    3368                 :             :       // skip commas, including trailing commas
    3369                 :             :       lexer.skip_token ();
    3370                 :             :     }
    3371                 :             : 
    3372                 :             :   lifetime_params.shrink_to_fit ();
    3373                 :             : 
    3374                 :             :   return lifetime_params;
    3375                 :             : }
    3376                 :             : 
    3377                 :             : /* Parses lifetime generic parameters (objects). Will also consume any
    3378                 :             :  * trailing comma. Has extra is_end_token predicate checking.
    3379                 :             :  * TODO: is this best solution? implements most of the same algorithm. */
    3380                 :             : template <typename ManagedTokenSource>
    3381                 :             : template <typename EndTokenPred>
    3382                 :             : std::vector<AST::LifetimeParam>
    3383                 :          29 : Parser<ManagedTokenSource>::parse_lifetime_params_objs (
    3384                 :             :   EndTokenPred is_end_token)
    3385                 :             : {
    3386                 :          29 :   std::vector<AST::LifetimeParam> lifetime_params;
    3387                 :             : 
    3388                 :          87 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3389                 :             :     {
    3390                 :          29 :       AST::LifetimeParam lifetime_param = parse_lifetime_param ();
    3391                 :             : 
    3392                 :          29 :       if (lifetime_param.is_error ())
    3393                 :             :         {
    3394                 :             :           /* TODO: is it worth throwing away all lifetime params just because
    3395                 :             :            * one failed? */
    3396                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    3397                 :             :                        "failed to parse lifetime param in lifetime params");
    3398                 :           0 :           add_error (std::move (error));
    3399                 :             : 
    3400                 :           0 :           return {};
    3401                 :           0 :         }
    3402                 :             : 
    3403                 :          29 :       lifetime_params.push_back (std::move (lifetime_param));
    3404                 :             : 
    3405                 :          58 :       if (lexer.peek_token ()->get_id () != COMMA)
    3406                 :             :         break;
    3407                 :             : 
    3408                 :             :       // skip commas, including trailing commas
    3409                 :           0 :       lexer.skip_token ();
    3410                 :             :     }
    3411                 :             : 
    3412                 :          29 :   lifetime_params.shrink_to_fit ();
    3413                 :             : 
    3414                 :          29 :   return lifetime_params;
    3415                 :          29 : }
    3416                 :             : 
    3417                 :             : /* Parses a sequence of a certain grammar rule in object form (not pointer or
    3418                 :             :  * smart pointer), delimited by commas and ending when 'is_end_token' is
    3419                 :             :  * satisfied (templated). Will also consume any trailing comma.
    3420                 :             :  * FIXME: this cannot be used due to member function pointer problems (i.e.
    3421                 :             :  * parsing_function cannot be specified properly) */
    3422                 :             : template <typename ManagedTokenSource>
    3423                 :             : template <typename ParseFunction, typename EndTokenPred>
    3424                 :             : auto
    3425                 :             : Parser<ManagedTokenSource>::parse_non_ptr_sequence (
    3426                 :             :   ParseFunction parsing_function, EndTokenPred is_end_token,
    3427                 :             :   std::string error_msg) -> std::vector<decltype (parsing_function ())>
    3428                 :             : {
    3429                 :             :   std::vector<decltype (parsing_function ())> params;
    3430                 :             : 
    3431                 :             :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3432                 :             :     {
    3433                 :             :       auto param = parsing_function ();
    3434                 :             : 
    3435                 :             :       if (param.is_error ())
    3436                 :             :         {
    3437                 :             :           // TODO: is it worth throwing away all params just because one
    3438                 :             :           // failed?
    3439                 :             :           Error error (lexer.peek_token ()->get_locus (),
    3440                 :             :                        std::move (error_msg));
    3441                 :             :           add_error (std::move (error));
    3442                 :             : 
    3443                 :             :           return {};
    3444                 :             :         }
    3445                 :             : 
    3446                 :             :       params.push_back (std::move (param));
    3447                 :             : 
    3448                 :             :       if (lexer.peek_token ()->get_id () != COMMA)
    3449                 :             :         break;
    3450                 :             : 
    3451                 :             :       // skip commas, including trailing commas
    3452                 :             :       lexer.skip_token ();
    3453                 :             :     }
    3454                 :             : 
    3455                 :             :   params.shrink_to_fit ();
    3456                 :             : 
    3457                 :             :   return params;
    3458                 :             : }
    3459                 :             : 
    3460                 :             : /* Parses a single lifetime generic parameter (not including comma). */
    3461                 :             : template <typename ManagedTokenSource>
    3462                 :             : AST::LifetimeParam
    3463                 :          29 : Parser<ManagedTokenSource>::parse_lifetime_param ()
    3464                 :             : {
    3465                 :             :   // parse outer attributes, which are optional and may not exist
    3466                 :          29 :   auto outer_attrs = parse_outer_attributes ();
    3467                 :             : 
    3468                 :             :   // save lifetime token - required
    3469                 :          29 :   const_TokenPtr lifetime_tok = lexer.peek_token ();
    3470                 :          29 :   if (lifetime_tok->get_id () != LIFETIME)
    3471                 :             :     {
    3472                 :             :       // if lifetime is missing, must not be a lifetime param, so return null
    3473                 :           0 :       return AST::LifetimeParam::create_error ();
    3474                 :             :     }
    3475                 :          29 :   lexer.skip_token ();
    3476                 :          29 :   AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
    3477                 :             :                           lifetime_tok->get_locus ());
    3478                 :             : 
    3479                 :             :   // parse lifetime bounds, if it exists
    3480                 :          29 :   std::vector<AST::Lifetime> lifetime_bounds;
    3481                 :          58 :   if (lexer.peek_token ()->get_id () == COLON)
    3482                 :             :     {
    3483                 :             :       // parse lifetime bounds
    3484                 :           0 :       lifetime_bounds = parse_lifetime_bounds ();
    3485                 :             :       // TODO: have end token passed in?
    3486                 :             :     }
    3487                 :             : 
    3488                 :          58 :   return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
    3489                 :             :                              std::move (outer_attrs),
    3490                 :          29 :                              lifetime_tok->get_locus ());
    3491                 :          58 : }
    3492                 :             : 
    3493                 :             : // Parses type generic parameters. Will also consume any trailing comma.
    3494                 :             : template <typename ManagedTokenSource>
    3495                 :             : std::vector<std::unique_ptr<AST::TypeParam>>
    3496                 :             : Parser<ManagedTokenSource>::parse_type_params ()
    3497                 :             : {
    3498                 :             :   std::vector<std::unique_ptr<AST::TypeParam>> type_params;
    3499                 :             : 
    3500                 :             :   // infinite loop with break on failure as no info on ending token
    3501                 :             :   while (true)
    3502                 :             :     {
    3503                 :             :       std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
    3504                 :             : 
    3505                 :             :       if (type_param == nullptr)
    3506                 :             :         {
    3507                 :             :           // break if fails to parse
    3508                 :             :           break;
    3509                 :             :         }
    3510                 :             : 
    3511                 :             :       type_params.push_back (std::move (type_param));
    3512                 :             : 
    3513                 :             :       if (lexer.peek_token ()->get_id () != COMMA)
    3514                 :             :         break;
    3515                 :             : 
    3516                 :             :       // skip commas, including trailing commas
    3517                 :             :       lexer.skip_token ();
    3518                 :             :     }
    3519                 :             : 
    3520                 :             :   type_params.shrink_to_fit ();
    3521                 :             :   return type_params;
    3522                 :             : }
    3523                 :             : 
    3524                 :             : // Parses type generic parameters. Will also consume any trailing comma.
    3525                 :             : template <typename ManagedTokenSource>
    3526                 :             : template <typename EndTokenPred>
    3527                 :             : std::vector<std::unique_ptr<AST::TypeParam>>
    3528                 :             : Parser<ManagedTokenSource>::parse_type_params (EndTokenPred is_end_token)
    3529                 :             : {
    3530                 :             :   std::vector<std::unique_ptr<AST::TypeParam>> type_params;
    3531                 :             : 
    3532                 :             :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    3533                 :             :     {
    3534                 :             :       std::unique_ptr<AST::TypeParam> type_param = parse_type_param ();
    3535                 :             : 
    3536                 :             :       if (type_param == nullptr)
    3537                 :             :         {
    3538                 :             :           Error error (lexer.peek_token ()->get_locus (),
    3539                 :             :                        "failed to parse type param in type params");
    3540                 :             :           add_error (std::move (error));
    3541                 :             : 
    3542                 :             :           return {};
    3543                 :             :         }
    3544                 :             : 
    3545                 :             :       type_params.push_back (std::move (type_param));
    3546                 :             : 
    3547                 :             :       if (lexer.peek_token ()->get_id () != COMMA)
    3548                 :             :         break;
    3549                 :             : 
    3550                 :             :       // skip commas, including trailing commas
    3551                 :             :       lexer.skip_token ();
    3552                 :             :     }
    3553                 :             : 
    3554                 :             :   type_params.shrink_to_fit ();
    3555                 :             :   return type_params;
    3556                 :             :   /* TODO: this shares most code with parse_lifetime_params - good place to
    3557                 :             :    * use template (i.e. parse_non_ptr_sequence if doable) */
    3558                 :             : }
    3559                 :             : 
    3560                 :             : /* Parses a single type (generic) parameter, not including commas. May change
    3561                 :             :  * to return value. */
    3562                 :             : template <typename ManagedTokenSource>
    3563                 :             : std::unique_ptr<AST::TypeParam>
    3564                 :             : Parser<ManagedTokenSource>::parse_type_param ()
    3565                 :             : {
    3566                 :             :   // parse outer attributes, which are optional and may not exist
    3567                 :             :   auto outer_attrs = parse_outer_attributes ();
    3568                 :             : 
    3569                 :             :   const_TokenPtr identifier_tok = lexer.peek_token ();
    3570                 :             :   if (identifier_tok->get_id () != IDENTIFIER)
    3571                 :             :     {
    3572                 :             :       // return null as type param can't exist without this required
    3573                 :             :       // identifier
    3574                 :             :       return nullptr;
    3575                 :             :     }
    3576                 :             :   Identifier ident{identifier_tok};
    3577                 :             :   lexer.skip_token ();
    3578                 :             : 
    3579                 :             :   // parse type param bounds (if they exist)
    3580                 :             :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    3581                 :             :   if (lexer.peek_token ()->get_id () == COLON)
    3582                 :             :     {
    3583                 :             :       lexer.skip_token ();
    3584                 :             : 
    3585                 :             :       // parse type param bounds, which may or may not exist
    3586                 :             :       type_param_bounds = parse_type_param_bounds ();
    3587                 :             :     }
    3588                 :             : 
    3589                 :             :   // parse type (if it exists)
    3590                 :             :   std::unique_ptr<AST::Type> type = nullptr;
    3591                 :             :   if (lexer.peek_token ()->get_id () == EQUAL)
    3592                 :             :     {
    3593                 :             :       lexer.skip_token ();
    3594                 :             : 
    3595                 :             :       // parse type (now required)
    3596                 :             :       type = parse_type ();
    3597                 :             :       if (type == nullptr)
    3598                 :             :         {
    3599                 :             :           Error error (lexer.peek_token ()->get_locus (),
    3600                 :             :                        "failed to parse type in type param");
    3601                 :             :           add_error (std::move (error));
    3602                 :             : 
    3603                 :             :           return nullptr;
    3604                 :             :         }
    3605                 :             :     }
    3606                 :             : 
    3607                 :             :   return std::unique_ptr<AST::TypeParam> (
    3608                 :             :     new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
    3609                 :             :                         std::move (type_param_bounds), std::move (type),
    3610                 :             :                         std::move (outer_attrs)));
    3611                 :             : }
    3612                 :             : 
    3613                 :             : /* Parses regular (i.e. non-generic) parameters in functions or methods. Also
    3614                 :             :  * has end token handling. */
    3615                 :             : template <typename ManagedTokenSource>
    3616                 :             : template <typename EndTokenPred>
    3617                 :             : std::vector<std::unique_ptr<AST::Param>>
    3618                 :        6064 : Parser<ManagedTokenSource>::parse_function_params (EndTokenPred is_end_token)
    3619                 :             : {
    3620                 :        6064 :   std::vector<std::unique_ptr<AST::Param>> params;
    3621                 :             : 
    3622                 :       12128 :   if (is_end_token (lexer.peek_token ()->get_id ()))
    3623                 :         706 :     return params;
    3624                 :             : 
    3625                 :        5358 :   auto initial_param = parse_function_param ();
    3626                 :             : 
    3627                 :             :   // Return empty parameter list if no parameter there
    3628                 :        5358 :   if (initial_param == nullptr)
    3629                 :             :     {
    3630                 :             :       // TODO: is this an error?
    3631                 :           0 :       return params;
    3632                 :             :     }
    3633                 :             : 
    3634                 :        5358 :   params.push_back (std::move (initial_param));
    3635                 :             : 
    3636                 :             :   // maybe think of a better control structure here - do-while with an initial
    3637                 :             :   // error state? basically, loop through parameter list until can't find any
    3638                 :             :   // more params
    3639                 :        5358 :   const_TokenPtr t = lexer.peek_token ();
    3640                 :        7169 :   while (t->get_id () == COMMA)
    3641                 :             :     {
    3642                 :             :       // skip comma if applies
    3643                 :        1811 :       lexer.skip_token ();
    3644                 :             : 
    3645                 :             :       // TODO: strictly speaking, shouldn't there be no trailing comma?
    3646                 :        3622 :       if (is_end_token (lexer.peek_token ()->get_id ()))
    3647                 :             :         break;
    3648                 :             : 
    3649                 :             :       // now, as right paren would break, function param is required
    3650                 :        1811 :       auto param = parse_function_param ();
    3651                 :        1811 :       if (param == nullptr)
    3652                 :             :         {
    3653                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    3654                 :             :                        "failed to parse function param (in function params)");
    3655                 :           0 :           add_error (std::move (error));
    3656                 :             : 
    3657                 :             :           // skip somewhere?
    3658                 :           0 :           return std::vector<std::unique_ptr<AST::Param>> ();
    3659                 :           0 :         }
    3660                 :             : 
    3661                 :        1811 :       params.push_back (std::move (param));
    3662                 :             : 
    3663                 :        1811 :       t = lexer.peek_token ();
    3664                 :             :     }
    3665                 :             : 
    3666                 :        5358 :   params.shrink_to_fit ();
    3667                 :        5358 :   return params;
    3668                 :        6064 : }
    3669                 :             : 
    3670                 :             : /* Parses a single regular (i.e. non-generic) parameter in a function or
    3671                 :             :  * method, i.e. the "name: type" bit. Also handles it not existing. */
    3672                 :             : template <typename ManagedTokenSource>
    3673                 :             : std::unique_ptr<AST::Param>
    3674                 :        7169 : Parser<ManagedTokenSource>::parse_function_param ()
    3675                 :             : {
    3676                 :             :   // parse outer attributes if they exist
    3677                 :        7169 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    3678                 :             : 
    3679                 :             :   // TODO: should saved location be at start of outer attributes or pattern?
    3680                 :        7169 :   location_t locus = lexer.peek_token ()->get_locus ();
    3681                 :             : 
    3682                 :       14338 :   if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic
    3683                 :             :     {
    3684                 :         770 :       lexer.skip_token (); // Skip ellipsis
    3685                 :         770 :       return std::make_unique<AST::VariadicParam> (
    3686                 :        1540 :         AST::VariadicParam (std::move (outer_attrs), locus));
    3687                 :             :     }
    3688                 :             : 
    3689                 :        6399 :   std::unique_ptr<AST::Pattern> param_pattern = parse_pattern ();
    3690                 :             : 
    3691                 :             :   // create error function param if it doesn't exist
    3692                 :        6399 :   if (param_pattern == nullptr)
    3693                 :             :     {
    3694                 :             :       // skip after something
    3695                 :           0 :       return nullptr;
    3696                 :             :     }
    3697                 :             : 
    3698                 :        6399 :   if (!skip_token (COLON))
    3699                 :             :     {
    3700                 :             :       // skip after something
    3701                 :           0 :       return nullptr;
    3702                 :             :     }
    3703                 :             : 
    3704                 :       12798 :   if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic
    3705                 :             :     {
    3706                 :          15 :       lexer.skip_token (); // Skip ellipsis
    3707                 :          15 :       return std::make_unique<AST::VariadicParam> (
    3708                 :          30 :         AST::VariadicParam (std::move (param_pattern), std::move (outer_attrs),
    3709                 :          15 :                             locus));
    3710                 :             :     }
    3711                 :             :   else
    3712                 :             :     {
    3713                 :        6384 :       std::unique_ptr<AST::Type> param_type = parse_type ();
    3714                 :        6384 :       if (param_type == nullptr)
    3715                 :             :         {
    3716                 :           0 :           return nullptr;
    3717                 :             :         }
    3718                 :        6384 :       return std::make_unique<AST::FunctionParam> (
    3719                 :       12768 :         AST::FunctionParam (std::move (param_pattern), std::move (param_type),
    3720                 :        6384 :                             std::move (outer_attrs), locus));
    3721                 :        6384 :     }
    3722                 :        7169 : }
    3723                 :             : 
    3724                 :             : /* Parses a function or method return type syntactical construction. Also
    3725                 :             :  * handles a function return type not existing. */
    3726                 :             : template <typename ManagedTokenSource>
    3727                 :             : std::unique_ptr<AST::Type>
    3728                 :       13372 : Parser<ManagedTokenSource>::parse_function_return_type ()
    3729                 :             : {
    3730                 :       26744 :   if (lexer.peek_token ()->get_id () != RETURN_TYPE)
    3731                 :        5383 :     return nullptr;
    3732                 :             : 
    3733                 :             :   // skip return type, as it now obviously exists
    3734                 :        7989 :   lexer.skip_token ();
    3735                 :             : 
    3736                 :        7989 :   std::unique_ptr<AST::Type> type = parse_type ();
    3737                 :             : 
    3738                 :        7989 :   return type;
    3739                 :        7989 : }
    3740                 :             : 
    3741                 :             : /* Parses a "where clause" (in a function, struct, method, etc.). Also handles
    3742                 :             :  * a where clause not existing, in which it will return
    3743                 :             :  * WhereClause::create_empty(), which can be checked via
    3744                 :             :  * WhereClause::is_empty(). */
    3745                 :             : template <typename ManagedTokenSource>
    3746                 :             : AST::WhereClause
    3747                 :       23385 : Parser<ManagedTokenSource>::parse_where_clause ()
    3748                 :             : {
    3749                 :       23385 :   const_TokenPtr where_tok = lexer.peek_token ();
    3750                 :       23385 :   if (where_tok->get_id () != WHERE)
    3751                 :             :     {
    3752                 :             :       // where clause doesn't exist, so create empty one
    3753                 :       23285 :       return AST::WhereClause::create_empty ();
    3754                 :             :     }
    3755                 :             : 
    3756                 :         100 :   lexer.skip_token ();
    3757                 :             : 
    3758                 :             :   /* parse where clause items - this is not a separate rule in the reference
    3759                 :             :    * so won't be here */
    3760                 :         100 :   std::vector<std::unique_ptr<AST::WhereClauseItem>> where_clause_items;
    3761                 :             : 
    3762                 :         100 :   std::vector<AST::LifetimeParam> for_lifetimes;
    3763                 :         200 :   if (lexer.peek_token ()->get_id () == FOR)
    3764                 :           2 :     for_lifetimes = parse_for_lifetimes ();
    3765                 :             : 
    3766                 :             :   /* HACK: where clauses end with a right curly or semicolon or equals in all
    3767                 :             :    * uses currently */
    3768                 :         100 :   const_TokenPtr t = lexer.peek_token ();
    3769                 :         206 :   while (t->get_id () != LEFT_CURLY && t->get_id () != SEMICOLON
    3770                 :         194 :          && t->get_id () != EQUAL)
    3771                 :             :     {
    3772                 :         106 :       std::unique_ptr<AST::WhereClauseItem> where_clause_item
    3773                 :             :         = parse_where_clause_item (for_lifetimes);
    3774                 :             : 
    3775                 :         106 :       if (where_clause_item == nullptr)
    3776                 :             :         {
    3777                 :           0 :           Error error (t->get_locus (), "failed to parse where clause item");
    3778                 :           0 :           add_error (std::move (error));
    3779                 :             : 
    3780                 :           0 :           return AST::WhereClause::create_empty ();
    3781                 :           0 :         }
    3782                 :             : 
    3783                 :         106 :       where_clause_items.push_back (std::move (where_clause_item));
    3784                 :             : 
    3785                 :             :       // also skip comma if it exists
    3786                 :         212 :       if (lexer.peek_token ()->get_id () != COMMA)
    3787                 :             :         break;
    3788                 :             : 
    3789                 :          94 :       lexer.skip_token ();
    3790                 :          94 :       t = lexer.peek_token ();
    3791                 :             :     }
    3792                 :             : 
    3793                 :         100 :   where_clause_items.shrink_to_fit ();
    3794                 :         100 :   return AST::WhereClause (std::move (where_clause_items));
    3795                 :         100 : }
    3796                 :             : 
    3797                 :             : /* Parses a where clause item (lifetime or type bound). Does not parse any
    3798                 :             :  * commas. */
    3799                 :             : template <typename ManagedTokenSource>
    3800                 :             : std::unique_ptr<AST::WhereClauseItem>
    3801                 :         106 : Parser<ManagedTokenSource>::parse_where_clause_item (
    3802                 :             :   const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
    3803                 :             : {
    3804                 :             :   // shitty cheat way of determining lifetime or type bound - test for
    3805                 :             :   // lifetime
    3806                 :         106 :   const_TokenPtr t = lexer.peek_token ();
    3807                 :             : 
    3808                 :         106 :   if (t->get_id () == LIFETIME)
    3809                 :           2 :     return parse_lifetime_where_clause_item ();
    3810                 :             :   else
    3811                 :         104 :     return parse_type_bound_where_clause_item (outer_for_lifetimes);
    3812                 :         106 : }
    3813                 :             : 
    3814                 :             : // Parses a lifetime where clause item.
    3815                 :             : template <typename ManagedTokenSource>
    3816                 :             : std::unique_ptr<AST::LifetimeWhereClauseItem>
    3817                 :           2 : Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
    3818                 :             : {
    3819                 :           2 :   AST::Lifetime lifetime = parse_lifetime (false);
    3820                 :           0 :   if (lifetime.is_error ())
    3821                 :             :     {
    3822                 :             :       // TODO: error here?
    3823                 :           0 :       return nullptr;
    3824                 :             :     }
    3825                 :             : 
    3826                 :           2 :   if (!skip_token (COLON))
    3827                 :             :     {
    3828                 :             :       // TODO: skip after somewhere
    3829                 :           0 :       return nullptr;
    3830                 :             :     }
    3831                 :             : 
    3832                 :           2 :   std::vector<AST::Lifetime> lifetime_bounds = parse_lifetime_bounds ();
    3833                 :             :   // TODO: have end token passed in?
    3834                 :             : 
    3835                 :           2 :   location_t locus = lifetime.get_locus ();
    3836                 :             : 
    3837                 :             :   return std::unique_ptr<AST::LifetimeWhereClauseItem> (
    3838                 :           2 :     new AST::LifetimeWhereClauseItem (std::move (lifetime),
    3839                 :           2 :                                       std::move (lifetime_bounds), locus));
    3840                 :           2 : }
    3841                 :             : 
    3842                 :             : // Parses a type bound where clause item.
    3843                 :             : template <typename ManagedTokenSource>
    3844                 :             : std::unique_ptr<AST::TypeBoundWhereClauseItem>
    3845                 :         104 : Parser<ManagedTokenSource>::parse_type_bound_where_clause_item (
    3846                 :             :   const std::vector<AST::LifetimeParam> &outer_for_lifetimes)
    3847                 :             : {
    3848                 :         104 :   std::vector<AST::LifetimeParam> for_lifetimes = outer_for_lifetimes;
    3849                 :             : 
    3850                 :         104 :   std::unique_ptr<AST::Type> type = parse_type ();
    3851                 :         104 :   if (type == nullptr)
    3852                 :             :     {
    3853                 :           0 :       return nullptr;
    3854                 :             :     }
    3855                 :             : 
    3856                 :         104 :   if (!skip_token (COLON))
    3857                 :             :     {
    3858                 :             :       // TODO: skip after somewhere
    3859                 :           0 :       return nullptr;
    3860                 :             :     }
    3861                 :             : 
    3862                 :         208 :   if (lexer.peek_token ()->get_id () == FOR)
    3863                 :             :     {
    3864                 :           9 :       auto for_lifetimes_inner = parse_for_lifetimes ();
    3865                 :           9 :       for_lifetimes.insert (for_lifetimes.end (), for_lifetimes_inner.begin (),
    3866                 :             :                             for_lifetimes_inner.end ());
    3867                 :           9 :     }
    3868                 :             : 
    3869                 :             :   // parse type param bounds if they exist
    3870                 :         104 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds
    3871                 :             :     = parse_type_param_bounds ();
    3872                 :             : 
    3873                 :         104 :   location_t locus = lexer.peek_token ()->get_locus ();
    3874                 :             : 
    3875                 :             :   return std::unique_ptr<AST::TypeBoundWhereClauseItem> (
    3876                 :         104 :     new AST::TypeBoundWhereClauseItem (std::move (for_lifetimes),
    3877                 :             :                                        std::move (type),
    3878                 :         104 :                                        std::move (type_param_bounds), locus));
    3879                 :         104 : }
    3880                 :             : 
    3881                 :             : // Parses a for lifetimes clause, including the for keyword and angle
    3882                 :             : // brackets.
    3883                 :             : template <typename ManagedTokenSource>
    3884                 :             : std::vector<AST::LifetimeParam>
    3885                 :          29 : Parser<ManagedTokenSource>::parse_for_lifetimes ()
    3886                 :             : {
    3887                 :          29 :   std::vector<AST::LifetimeParam> params;
    3888                 :             : 
    3889                 :          29 :   if (!skip_token (FOR))
    3890                 :             :     {
    3891                 :             :       // skip after somewhere?
    3892                 :             :       return params;
    3893                 :             :     }
    3894                 :             : 
    3895                 :          29 :   if (!skip_token (LEFT_ANGLE))
    3896                 :             :     {
    3897                 :             :       // skip after somewhere?
    3898                 :             :       return params;
    3899                 :             :     }
    3900                 :             : 
    3901                 :             :   /* cannot specify end token due to parsing problems with '>' tokens being
    3902                 :             :    * nested */
    3903                 :          29 :   params = parse_lifetime_params_objs (is_right_angle_tok);
    3904                 :             : 
    3905                 :          29 :   if (!skip_generics_right_angle ())
    3906                 :             :     {
    3907                 :             :       // DEBUG
    3908                 :           0 :       rust_debug ("failed to skip generics right angle after (supposedly) "
    3909                 :             :                   "finished parsing where clause items");
    3910                 :             :       // ok, well this gets called.
    3911                 :             : 
    3912                 :             :       // skip after somewhere?
    3913                 :           0 :       return params;
    3914                 :             :     }
    3915                 :             : 
    3916                 :             :   return params;
    3917                 :             : }
    3918                 :             : 
    3919                 :             : // Parses type parameter bounds in where clause or generic arguments.
    3920                 :             : template <typename ManagedTokenSource>
    3921                 :             : std::vector<std::unique_ptr<AST::TypeParamBound>>
    3922                 :         431 : Parser<ManagedTokenSource>::parse_type_param_bounds ()
    3923                 :             : {
    3924                 :         431 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    3925                 :             : 
    3926                 :         431 :   std::unique_ptr<AST::TypeParamBound> initial_bound
    3927                 :             :     = parse_type_param_bound ();
    3928                 :             : 
    3929                 :             :   // quick exit if null
    3930                 :         431 :   if (initial_bound == nullptr)
    3931                 :             :     {
    3932                 :             :       /* error? type param bounds must have at least one term, but are bounds
    3933                 :             :        * optional? */
    3934                 :             :       return type_param_bounds;
    3935                 :             :     }
    3936                 :         431 :   type_param_bounds.push_back (std::move (initial_bound));
    3937                 :             : 
    3938                 :         870 :   while (lexer.peek_token ()->get_id () == PLUS)
    3939                 :             :     {
    3940                 :           4 :       lexer.skip_token ();
    3941                 :             : 
    3942                 :           4 :       std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
    3943                 :           4 :       if (bound == nullptr)
    3944                 :             :         {
    3945                 :             :           /* not an error: bound is allowed to be null as trailing plus is
    3946                 :             :            * allowed */
    3947                 :             :           return type_param_bounds;
    3948                 :             :         }
    3949                 :             : 
    3950                 :           4 :       type_param_bounds.push_back (std::move (bound));
    3951                 :             :     }
    3952                 :             : 
    3953                 :             :   type_param_bounds.shrink_to_fit ();
    3954                 :             :   return type_param_bounds;
    3955                 :         431 : }
    3956                 :             : 
    3957                 :             : /* Parses type parameter bounds in where clause or generic arguments, with end
    3958                 :             :  * token handling. */
    3959                 :             : template <typename ManagedTokenSource>
    3960                 :             : template <typename EndTokenPred>
    3961                 :             : std::vector<std::unique_ptr<AST::TypeParamBound>>
    3962                 :         252 : Parser<ManagedTokenSource>::parse_type_param_bounds (EndTokenPred is_end_token)
    3963                 :             : {
    3964                 :         252 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    3965                 :             : 
    3966                 :         252 :   std::unique_ptr<AST::TypeParamBound> initial_bound
    3967                 :             :     = parse_type_param_bound ();
    3968                 :             : 
    3969                 :             :   // quick exit if null
    3970                 :         252 :   if (initial_bound == nullptr)
    3971                 :             :     {
    3972                 :             :       /* error? type param bounds must have at least one term, but are bounds
    3973                 :             :        * optional? */
    3974                 :           0 :       return type_param_bounds;
    3975                 :             :     }
    3976                 :         252 :   type_param_bounds.push_back (std::move (initial_bound));
    3977                 :             : 
    3978                 :         508 :   while (lexer.peek_token ()->get_id () == PLUS)
    3979                 :             :     {
    3980                 :           2 :       lexer.skip_token ();
    3981                 :             : 
    3982                 :             :       // break if end token character
    3983                 :           4 :       if (is_end_token (lexer.peek_token ()->get_id ()))
    3984                 :             :         break;
    3985                 :             : 
    3986                 :           2 :       std::unique_ptr<AST::TypeParamBound> bound = parse_type_param_bound ();
    3987                 :           2 :       if (bound == nullptr)
    3988                 :             :         {
    3989                 :             :           // TODO how wise is it to ditch all bounds if only one failed?
    3990                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    3991                 :             :                        "failed to parse type param bound in type param bounds");
    3992                 :           0 :           add_error (std::move (error));
    3993                 :             : 
    3994                 :           0 :           return {};
    3995                 :           0 :         }
    3996                 :             : 
    3997                 :           2 :       type_param_bounds.push_back (std::move (bound));
    3998                 :             :     }
    3999                 :             : 
    4000                 :         252 :   type_param_bounds.shrink_to_fit ();
    4001                 :         252 :   return type_param_bounds;
    4002                 :         252 : }
    4003                 :             : 
    4004                 :             : /* Parses a single type parameter bound in a where clause or generic argument.
    4005                 :             :  * Does not parse the '+' between arguments. */
    4006                 :             : template <typename ManagedTokenSource>
    4007                 :             : std::unique_ptr<AST::TypeParamBound>
    4008                 :         726 : Parser<ManagedTokenSource>::parse_type_param_bound ()
    4009                 :             : {
    4010                 :             :   // shitty cheat way of determining lifetime or trait bound - test for
    4011                 :             :   // lifetime
    4012                 :         726 :   const_TokenPtr t = lexer.peek_token ();
    4013                 :         726 :   switch (t->get_id ())
    4014                 :             :     {
    4015                 :          17 :     case LIFETIME:
    4016                 :          17 :       return std::unique_ptr<AST::Lifetime> (
    4017                 :          17 :         new AST::Lifetime (parse_lifetime (false)));
    4018                 :         709 :     case LEFT_PAREN:
    4019                 :             :     case QUESTION_MARK:
    4020                 :             :     case FOR:
    4021                 :             :     case IDENTIFIER:
    4022                 :             :     case SUPER:
    4023                 :             :     case SELF:
    4024                 :             :     case SELF_ALIAS:
    4025                 :             :     case CRATE:
    4026                 :             :     case DOLLAR_SIGN:
    4027                 :             :     case SCOPE_RESOLUTION:
    4028                 :         709 :       return parse_trait_bound ();
    4029                 :           0 :     default:
    4030                 :             :       // don't error - assume this is fine TODO
    4031                 :           0 :       return nullptr;
    4032                 :             :     }
    4033                 :         726 : }
    4034                 :             : 
    4035                 :             : // Parses a trait bound type param bound.
    4036                 :             : template <typename ManagedTokenSource>
    4037                 :             : std::unique_ptr<AST::TraitBound>
    4038                 :         877 : Parser<ManagedTokenSource>::parse_trait_bound ()
    4039                 :             : {
    4040                 :         877 :   bool has_parens = false;
    4041                 :         877 :   bool has_question_mark = false;
    4042                 :             : 
    4043                 :        1754 :   location_t locus = lexer.peek_token ()->get_locus ();
    4044                 :             : 
    4045                 :             :   /* parse optional `for lifetimes`. */
    4046                 :         877 :   std::vector<AST::LifetimeParam> for_lifetimes;
    4047                 :        1754 :   if (lexer.peek_token ()->get_id () == FOR)
    4048                 :          14 :     for_lifetimes = parse_for_lifetimes ();
    4049                 :             : 
    4050                 :             :   // handle trait bound being in parentheses
    4051                 :        1754 :   if (lexer.peek_token ()->get_id () == LEFT_PAREN)
    4052                 :             :     {
    4053                 :           0 :       has_parens = true;
    4054                 :           0 :       lexer.skip_token ();
    4055                 :             :     }
    4056                 :             : 
    4057                 :             :   // handle having question mark (optional)
    4058                 :        1754 :   if (lexer.peek_token ()->get_id () == QUESTION_MARK)
    4059                 :             :     {
    4060                 :          42 :       has_question_mark = true;
    4061                 :          42 :       lexer.skip_token ();
    4062                 :             :     }
    4063                 :             : 
    4064                 :             :   // handle TypePath
    4065                 :         877 :   AST::TypePath type_path = parse_type_path ();
    4066                 :             : 
    4067                 :             :   // handle closing parentheses
    4068                 :         877 :   if (has_parens)
    4069                 :             :     {
    4070                 :           0 :       if (!skip_token (RIGHT_PAREN))
    4071                 :             :         {
    4072                 :           0 :           return nullptr;
    4073                 :             :         }
    4074                 :             :     }
    4075                 :             : 
    4076                 :             :   return std::unique_ptr<AST::TraitBound> (
    4077                 :         877 :     new AST::TraitBound (std::move (type_path), locus, has_parens,
    4078                 :         877 :                          has_question_mark, std::move (for_lifetimes)));
    4079                 :         877 : }
    4080                 :             : 
    4081                 :             : // Parses lifetime bounds.
    4082                 :             : template <typename ManagedTokenSource>
    4083                 :             : std::vector<AST::Lifetime>
    4084                 :           2 : Parser<ManagedTokenSource>::parse_lifetime_bounds ()
    4085                 :             : {
    4086                 :           2 :   std::vector<AST::Lifetime> lifetime_bounds;
    4087                 :             : 
    4088                 :           0 :   while (true)
    4089                 :             :     {
    4090                 :           2 :       AST::Lifetime lifetime = parse_lifetime (false);
    4091                 :             : 
    4092                 :             :       // quick exit for parsing failure
    4093                 :           4 :       if (lifetime.is_error ())
    4094                 :             :         break;
    4095                 :             : 
    4096                 :           2 :       lifetime_bounds.push_back (std::move (lifetime));
    4097                 :             : 
    4098                 :             :       /* plus is maybe not allowed at end - spec defines it weirdly, so
    4099                 :             :        * assuming allowed at end */
    4100                 :           4 :       if (lexer.peek_token ()->get_id () != PLUS)
    4101                 :             :         break;
    4102                 :             : 
    4103                 :           0 :       lexer.skip_token ();
    4104                 :             :     }
    4105                 :             : 
    4106                 :             :   lifetime_bounds.shrink_to_fit ();
    4107                 :           2 :   return lifetime_bounds;
    4108                 :             : }
    4109                 :             : 
    4110                 :             : // Parses lifetime bounds, with added check for ending token.
    4111                 :             : template <typename ManagedTokenSource>
    4112                 :             : template <typename EndTokenPred>
    4113                 :             : std::vector<AST::Lifetime>
    4114                 :           2 : Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
    4115                 :             : {
    4116                 :           2 :   std::vector<AST::Lifetime> lifetime_bounds;
    4117                 :             : 
    4118                 :           8 :   while (!is_end_token (lexer.peek_token ()->get_id ()))
    4119                 :             :     {
    4120                 :           2 :       AST::Lifetime lifetime = parse_lifetime (false);
    4121                 :             : 
    4122                 :           2 :       if (lifetime.is_error ())
    4123                 :             :         {
    4124                 :             :           /* TODO: is it worth throwing away all lifetime bound info just
    4125                 :             :            * because one failed? */
    4126                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    4127                 :             :                        "failed to parse lifetime in lifetime bounds");
    4128                 :           0 :           add_error (std::move (error));
    4129                 :             : 
    4130                 :           0 :           return {};
    4131                 :           0 :         }
    4132                 :             : 
    4133                 :           2 :       lifetime_bounds.push_back (std::move (lifetime));
    4134                 :             : 
    4135                 :             :       /* plus is maybe not allowed at end - spec defines it weirdly, so
    4136                 :             :        * assuming allowed at end */
    4137                 :           4 :       if (lexer.peek_token ()->get_id () != PLUS)
    4138                 :             :         break;
    4139                 :             : 
    4140                 :           0 :       lexer.skip_token ();
    4141                 :             :     }
    4142                 :             : 
    4143                 :           2 :   lifetime_bounds.shrink_to_fit ();
    4144                 :           2 :   return lifetime_bounds;
    4145                 :           2 : }
    4146                 :             : 
    4147                 :             : /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
    4148                 :             :  * existing. */
    4149                 :             : template <typename ManagedTokenSource>
    4150                 :             : AST::Lifetime
    4151                 :        3660 : Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
    4152                 :             : {
    4153                 :        3660 :   const_TokenPtr lifetime_tok = lexer.peek_token ();
    4154                 :        3660 :   if (lifetime_tok->get_id () != LIFETIME)
    4155                 :             :     {
    4156                 :        2610 :       return (allow_elided) ? AST::Lifetime::elided ()
    4157                 :        2610 :                             : AST::Lifetime::error ();
    4158                 :             :     }
    4159                 :        1050 :   lexer.skip_token ();
    4160                 :             : 
    4161                 :        2100 :   return lifetime_from_token (lifetime_tok);
    4162                 :        3660 : }
    4163                 :             : 
    4164                 :             : template <typename ManagedTokenSource>
    4165                 :             : AST::Lifetime
    4166                 :        1090 : Parser<ManagedTokenSource>::lifetime_from_token (const_TokenPtr tok)
    4167                 :             : {
    4168                 :        1090 :   location_t locus = tok->get_locus ();
    4169                 :        1090 :   std::string lifetime_ident = tok->get_str ();
    4170                 :             : 
    4171                 :        1090 :   if (lifetime_ident == "static")
    4172                 :             :     {
    4173                 :          58 :       return AST::Lifetime (AST::Lifetime::STATIC, "", locus);
    4174                 :             :     }
    4175                 :        1032 :   else if (lifetime_ident == "_")
    4176                 :             :     {
    4177                 :             :       // Explicitly and implicitly elided lifetimes follow the same rules.
    4178                 :          10 :       return AST::Lifetime (AST::Lifetime::WILDCARD, "", locus);
    4179                 :             :     }
    4180                 :             :   else
    4181                 :             :     {
    4182                 :        2044 :       return AST::Lifetime (AST::Lifetime::NAMED, std::move (lifetime_ident),
    4183                 :        1022 :                             locus);
    4184                 :             :     }
    4185                 :        1090 : }
    4186                 :             : 
    4187                 :             : template <typename ManagedTokenSource>
    4188                 :             : std::unique_ptr<AST::ExternalTypeItem>
    4189                 :           8 : Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
    4190                 :             :                                                       AST::AttrVec outer_attrs)
    4191                 :             : {
    4192                 :           8 :   location_t locus = lexer.peek_token ()->get_locus ();
    4193                 :           8 :   skip_token (TYPE);
    4194                 :             : 
    4195                 :           8 :   const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
    4196                 :           8 :   if (alias_name_tok == nullptr)
    4197                 :             :     {
    4198                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    4199                 :             :                    "could not parse identifier in external opaque type");
    4200                 :           0 :       add_error (std::move (error));
    4201                 :             : 
    4202                 :           0 :       skip_after_semicolon ();
    4203                 :           0 :       return nullptr;
    4204                 :           0 :     }
    4205                 :             : 
    4206                 :           8 :   if (!skip_token (SEMICOLON))
    4207                 :           2 :     return nullptr;
    4208                 :             : 
    4209                 :             :   return std::unique_ptr<AST::ExternalTypeItem> (
    4210                 :           6 :     new AST::ExternalTypeItem (alias_name_tok->get_str (), std::move (vis),
    4211                 :           6 :                                std::move (outer_attrs), std::move (locus)));
    4212                 :           8 : }
    4213                 :             : 
    4214                 :             : // Parses a "type alias" (typedef) item.
    4215                 :             : template <typename ManagedTokenSource>
    4216                 :             : std::unique_ptr<AST::TypeAlias>
    4217                 :         998 : Parser<ManagedTokenSource>::parse_type_alias (AST::Visibility vis,
    4218                 :             :                                               AST::AttrVec outer_attrs)
    4219                 :             : {
    4220                 :         998 :   location_t locus = lexer.peek_token ()->get_locus ();
    4221                 :         998 :   skip_token (TYPE);
    4222                 :             : 
    4223                 :             :   // TODO: use this token for identifier when finished that
    4224                 :         998 :   const_TokenPtr alias_name_tok = expect_token (IDENTIFIER);
    4225                 :         998 :   if (alias_name_tok == nullptr)
    4226                 :             :     {
    4227                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    4228                 :             :                    "could not parse identifier in type alias");
    4229                 :           0 :       add_error (std::move (error));
    4230                 :             : 
    4231                 :           0 :       skip_after_semicolon ();
    4232                 :           0 :       return nullptr;
    4233                 :           0 :     }
    4234                 :         998 :   Identifier alias_name{alias_name_tok};
    4235                 :             : 
    4236                 :             :   // parse generic params, which may not exist
    4237                 :         998 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4238                 :             :     = parse_generic_params_in_angles ();
    4239                 :             : 
    4240                 :             :   // parse where clause, which may not exist
    4241                 :         998 :   AST::WhereClause where_clause = parse_where_clause ();
    4242                 :             : 
    4243                 :         998 :   if (!skip_token (EQUAL))
    4244                 :             :     {
    4245                 :           0 :       skip_after_semicolon ();
    4246                 :           0 :       return nullptr;
    4247                 :             :     }
    4248                 :             : 
    4249                 :         998 :   std::unique_ptr<AST::Type> type_to_alias = parse_type ();
    4250                 :             : 
    4251                 :         998 :   if (!skip_token (SEMICOLON))
    4252                 :             :     {
    4253                 :             :       // should be skipping past this, not the next line
    4254                 :           0 :       return nullptr;
    4255                 :             :     }
    4256                 :             : 
    4257                 :             :   return std::unique_ptr<AST::TypeAlias> (
    4258                 :         998 :     new AST::TypeAlias (std::move (alias_name), std::move (generic_params),
    4259                 :             :                         std::move (where_clause), std::move (type_to_alias),
    4260                 :         998 :                         std::move (vis), std::move (outer_attrs), locus));
    4261                 :        1996 : }
    4262                 :             : 
    4263                 :             : // Parse a struct item AST node.
    4264                 :             : template <typename ManagedTokenSource>
    4265                 :             : std::unique_ptr<AST::Struct>
    4266                 :        2316 : Parser<ManagedTokenSource>::parse_struct (AST::Visibility vis,
    4267                 :             :                                           AST::AttrVec outer_attrs)
    4268                 :             : {
    4269                 :             :   /* TODO: determine best way to parse the proper struct vs tuple struct -
    4270                 :             :    * share most of initial constructs so lookahead might be impossible, and if
    4271                 :             :    * not probably too expensive. Best way is probably unified parsing for the
    4272                 :             :    * initial parts and then pass them in as params to more derived functions.
    4273                 :             :    * Alternatively, just parse everything in this one function - do this if
    4274                 :             :    * function not too long. */
    4275                 :             : 
    4276                 :             :   /* Proper struct <- 'struct' IDENTIFIER generic_params? where_clause? ( '{'
    4277                 :             :    * struct_fields? '}' | ';' ) */
    4278                 :             :   /* Tuple struct <- 'struct' IDENTIFIER generic_params? '(' tuple_fields? ')'
    4279                 :             :    * where_clause? ';' */
    4280                 :        2316 :   location_t locus = lexer.peek_token ()->get_locus ();
    4281                 :        2316 :   skip_token (STRUCT_KW);
    4282                 :             : 
    4283                 :             :   // parse struct name
    4284                 :        2316 :   const_TokenPtr name_tok = expect_token (IDENTIFIER);
    4285                 :        2316 :   if (name_tok == nullptr)
    4286                 :             :     {
    4287                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    4288                 :             :                    "could not parse struct or tuple struct identifier");
    4289                 :           0 :       add_error (std::move (error));
    4290                 :             : 
    4291                 :             :       // skip after somewhere?
    4292                 :           0 :       return nullptr;
    4293                 :           0 :     }
    4294                 :        2316 :   Identifier struct_name{name_tok};
    4295                 :             : 
    4296                 :             :   // parse generic params, which may or may not exist
    4297                 :        2316 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4298                 :             :     = parse_generic_params_in_angles ();
    4299                 :             : 
    4300                 :             :   // branch on next token - determines whether proper struct or tuple struct
    4301                 :        4632 :   if (lexer.peek_token ()->get_id () == LEFT_PAREN)
    4302                 :             :     {
    4303                 :             :       // tuple struct
    4304                 :             : 
    4305                 :             :       // skip left parenthesis
    4306                 :         894 :       lexer.skip_token ();
    4307                 :             : 
    4308                 :             :       // parse tuple fields
    4309                 :         894 :       std::vector<AST::TupleField> tuple_fields;
    4310                 :             :       // Might be empty tuple for unit tuple struct.
    4311                 :        1788 :       if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    4312                 :          23 :         tuple_fields = std::vector<AST::TupleField> ();
    4313                 :             :       else
    4314                 :         871 :         tuple_fields = parse_tuple_fields ();
    4315                 :             : 
    4316                 :             :       // tuple parameters must have closing parenthesis
    4317                 :         894 :       if (!skip_token (RIGHT_PAREN))
    4318                 :             :         {
    4319                 :           0 :           skip_after_semicolon ();
    4320                 :           0 :           return nullptr;
    4321                 :             :         }
    4322                 :             : 
    4323                 :             :       // parse where clause, which is optional
    4324                 :         894 :       AST::WhereClause where_clause = parse_where_clause ();
    4325                 :             : 
    4326                 :         894 :       if (!skip_token (SEMICOLON))
    4327                 :             :         {
    4328                 :             :           // can't skip after semicolon because it's meant to be here
    4329                 :           0 :           return nullptr;
    4330                 :             :         }
    4331                 :             : 
    4332                 :         894 :       return std::unique_ptr<AST::TupleStruct> (
    4333                 :         894 :         new AST::TupleStruct (std::move (tuple_fields), std::move (struct_name),
    4334                 :             :                               std::move (generic_params),
    4335                 :             :                               std::move (where_clause), std::move (vis),
    4336                 :         894 :                               std::move (outer_attrs), locus));
    4337                 :         894 :     }
    4338                 :             : 
    4339                 :             :   // assume it is a proper struct being parsed and continue outside of switch
    4340                 :             :   // - label only here to suppress warning
    4341                 :             : 
    4342                 :             :   // parse where clause, which is optional
    4343                 :        1422 :   AST::WhereClause where_clause = parse_where_clause ();
    4344                 :             : 
    4345                 :             :   // branch on next token - determines whether struct is a unit struct
    4346                 :        1422 :   const_TokenPtr t = lexer.peek_token ();
    4347                 :        1422 :   switch (t->get_id ())
    4348                 :             :     {
    4349                 :         789 :       case LEFT_CURLY: {
    4350                 :             :         // struct with body
    4351                 :             : 
    4352                 :             :         // skip curly bracket
    4353                 :         789 :         lexer.skip_token ();
    4354                 :             : 
    4355                 :             :         // parse struct fields, if any
    4356                 :         789 :         std::vector<AST::StructField> struct_fields
    4357                 :             :           = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    4358                 :             : 
    4359                 :         789 :         if (!skip_token (RIGHT_CURLY))
    4360                 :             :           {
    4361                 :             :             // skip somewhere?
    4362                 :           0 :             return nullptr;
    4363                 :             :           }
    4364                 :             : 
    4365                 :         789 :         return std::unique_ptr<AST::StructStruct> (new AST::StructStruct (
    4366                 :             :           std::move (struct_fields), std::move (struct_name),
    4367                 :             :           std::move (generic_params), std::move (where_clause), false,
    4368                 :         789 :           std::move (vis), std::move (outer_attrs), locus));
    4369                 :         789 :       }
    4370                 :         631 :     case SEMICOLON:
    4371                 :             :       // unit struct declaration
    4372                 :             : 
    4373                 :         631 :       lexer.skip_token ();
    4374                 :             : 
    4375                 :         631 :       return std::unique_ptr<AST::StructStruct> (
    4376                 :        1262 :         new AST::StructStruct (std::move (struct_name),
    4377                 :             :                                std::move (generic_params),
    4378                 :             :                                std::move (where_clause), std::move (vis),
    4379                 :         631 :                                std::move (outer_attrs), locus));
    4380                 :           2 :     default:
    4381                 :           2 :       add_error (Error (t->get_locus (),
    4382                 :             :                         "unexpected token %qs in struct declaration",
    4383                 :             :                         t->get_token_description ()));
    4384                 :             : 
    4385                 :             :       // skip somewhere?
    4386                 :           2 :       return nullptr;
    4387                 :             :     }
    4388                 :        2316 : }
    4389                 :             : 
    4390                 :             : // Parses struct fields in struct declarations.
    4391                 :             : template <typename ManagedTokenSource>
    4392                 :             : std::vector<AST::StructField>
    4393                 :             : Parser<ManagedTokenSource>::parse_struct_fields ()
    4394                 :             : {
    4395                 :             :   std::vector<AST::StructField> fields;
    4396                 :             : 
    4397                 :             :   AST::StructField initial_field = parse_struct_field ();
    4398                 :             : 
    4399                 :             :   // Return empty field list if no field there
    4400                 :             :   if (initial_field.is_error ())
    4401                 :             :     return fields;
    4402                 :             : 
    4403                 :             :   fields.push_back (std::move (initial_field));
    4404                 :             : 
    4405                 :             :   while (lexer.peek_token ()->get_id () == COMMA)
    4406                 :             :     {
    4407                 :             :       lexer.skip_token ();
    4408                 :             : 
    4409                 :             :       AST::StructField field = parse_struct_field ();
    4410                 :             : 
    4411                 :             :       if (field.is_error ())
    4412                 :             :         {
    4413                 :             :           // would occur with trailing comma, so allowed
    4414                 :             :           break;
    4415                 :             :         }
    4416                 :             : 
    4417                 :             :       fields.push_back (std::move (field));
    4418                 :             :     }
    4419                 :             : 
    4420                 :             :   fields.shrink_to_fit ();
    4421                 :             :   return fields;
    4422                 :             :   // TODO: template if possible (parse_non_ptr_seq)
    4423                 :             : }
    4424                 :             : 
    4425                 :             : // Parses struct fields in struct declarations.
    4426                 :             : template <typename ManagedTokenSource>
    4427                 :             : template <typename EndTokenPred>
    4428                 :             : std::vector<AST::StructField>
    4429                 :         981 : Parser<ManagedTokenSource>::parse_struct_fields (EndTokenPred is_end_tok)
    4430                 :             : {
    4431                 :         981 :   std::vector<AST::StructField> fields;
    4432                 :             : 
    4433                 :         981 :   AST::StructField initial_field = parse_struct_field ();
    4434                 :             : 
    4435                 :             :   // Return empty field list if no field there
    4436                 :         981 :   if (initial_field.is_error ())
    4437                 :          43 :     return fields;
    4438                 :             : 
    4439                 :         938 :   fields.push_back (std::move (initial_field));
    4440                 :             : 
    4441                 :        3976 :   while (lexer.peek_token ()->get_id () == COMMA)
    4442                 :             :     {
    4443                 :        1829 :       lexer.skip_token ();
    4444                 :             : 
    4445                 :        3658 :       if (is_end_tok (lexer.peek_token ()->get_id ()))
    4446                 :             :         break;
    4447                 :             : 
    4448                 :        1050 :       AST::StructField field = parse_struct_field ();
    4449                 :        1050 :       if (field.is_error ())
    4450                 :             :         {
    4451                 :             :           /* TODO: should every field be ditched just because one couldn't be
    4452                 :             :            * parsed? */
    4453                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    4454                 :             :                        "failed to parse struct field in struct fields");
    4455                 :           0 :           add_error (std::move (error));
    4456                 :             : 
    4457                 :           0 :           return {};
    4458                 :           0 :         }
    4459                 :             : 
    4460                 :        1050 :       fields.push_back (std::move (field));
    4461                 :             :     }
    4462                 :             : 
    4463                 :         938 :   fields.shrink_to_fit ();
    4464                 :         938 :   return fields;
    4465                 :             :   // TODO: template if possible (parse_non_ptr_seq)
    4466                 :         981 : }
    4467                 :             : 
    4468                 :             : // Parses a single struct field (in a struct definition). Does not parse
    4469                 :             : // commas.
    4470                 :             : template <typename ManagedTokenSource>
    4471                 :             : AST::StructField
    4472                 :        2031 : Parser<ManagedTokenSource>::parse_struct_field ()
    4473                 :             : {
    4474                 :             :   // parse outer attributes, if they exist
    4475                 :        2031 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4476                 :             : 
    4477                 :             :   // parse visibility, if it exists
    4478                 :        2031 :   AST::Visibility vis = parse_visibility ();
    4479                 :             : 
    4480                 :        2031 :   location_t locus = lexer.peek_token ()->get_locus ();
    4481                 :             : 
    4482                 :             :   // parse field name
    4483                 :        2031 :   const_TokenPtr field_name_tok = lexer.peek_token ();
    4484                 :        2031 :   if (field_name_tok->get_id () != IDENTIFIER)
    4485                 :             :     {
    4486                 :             :       // if not identifier, assumes there is no struct field and exits - not
    4487                 :             :       // necessarily error
    4488                 :          43 :       return AST::StructField::create_error ();
    4489                 :             :     }
    4490                 :        1988 :   Identifier field_name{field_name_tok};
    4491                 :        1988 :   lexer.skip_token ();
    4492                 :             : 
    4493                 :        1988 :   if (!skip_token (COLON))
    4494                 :             :     {
    4495                 :             :       // skip after somewhere?
    4496                 :           0 :       return AST::StructField::create_error ();
    4497                 :             :     }
    4498                 :             : 
    4499                 :             :   // parse field type - this is required
    4500                 :        1988 :   std::unique_ptr<AST::Type> field_type = parse_type ();
    4501                 :        1988 :   if (field_type == nullptr)
    4502                 :             :     {
    4503                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    4504                 :             :                    "could not parse type in struct field definition");
    4505                 :           0 :       add_error (std::move (error));
    4506                 :             : 
    4507                 :             :       // skip after somewhere
    4508                 :           0 :       return AST::StructField::create_error ();
    4509                 :           0 :     }
    4510                 :             : 
    4511                 :        3976 :   return AST::StructField (std::move (field_name), std::move (field_type),
    4512                 :        1988 :                            std::move (vis), locus, std::move (outer_attrs));
    4513                 :        6007 : }
    4514                 :             : 
    4515                 :             : // Parses tuple fields in tuple/tuple struct declarations.
    4516                 :             : template <typename ManagedTokenSource>
    4517                 :             : std::vector<AST::TupleField>
    4518                 :        1137 : Parser<ManagedTokenSource>::parse_tuple_fields ()
    4519                 :             : {
    4520                 :        1137 :   std::vector<AST::TupleField> fields;
    4521                 :             : 
    4522                 :        1137 :   AST::TupleField initial_field = parse_tuple_field ();
    4523                 :             : 
    4524                 :             :   // Return empty field list if no field there
    4525                 :        1137 :   if (initial_field.is_error ())
    4526                 :             :     {
    4527                 :           0 :       return fields;
    4528                 :             :     }
    4529                 :             : 
    4530                 :        1137 :   fields.push_back (std::move (initial_field));
    4531                 :             : 
    4532                 :             :   // maybe think of a better control structure here - do-while with an initial
    4533                 :             :   // error state? basically, loop through field list until can't find any more
    4534                 :             :   // params HACK: all current syntax uses of tuple fields have them ending
    4535                 :             :   // with a right paren token
    4536                 :        1137 :   const_TokenPtr t = lexer.peek_token ();
    4537                 :        1769 :   while (t->get_id () == COMMA)
    4538                 :             :     {
    4539                 :             :       // skip comma if applies - e.g. trailing comma
    4540                 :         632 :       lexer.skip_token ();
    4541                 :             : 
    4542                 :             :       // break out due to right paren if it exists
    4543                 :        1264 :       if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    4544                 :             :         {
    4545                 :             :           break;
    4546                 :             :         }
    4547                 :             : 
    4548                 :         632 :       AST::TupleField field = parse_tuple_field ();
    4549                 :         632 :       if (field.is_error ())
    4550                 :             :         {
    4551                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    4552                 :             :                        "failed to parse tuple field in tuple fields");
    4553                 :           0 :           add_error (std::move (error));
    4554                 :             : 
    4555                 :           0 :           return std::vector<AST::TupleField> ();
    4556                 :           0 :         }
    4557                 :             : 
    4558                 :         632 :       fields.push_back (std::move (field));
    4559                 :             : 
    4560                 :         632 :       t = lexer.peek_token ();
    4561                 :             :     }
    4562                 :             : 
    4563                 :        1137 :   fields.shrink_to_fit ();
    4564                 :        1137 :   return fields;
    4565                 :             : 
    4566                 :             :   // TODO: this shares basically all code with function params and struct
    4567                 :             :   // fields
    4568                 :             :   // - templates?
    4569                 :        1137 : }
    4570                 :             : 
    4571                 :             : /* Parses a single tuple struct field in a tuple struct definition. Does not
    4572                 :             :  * parse commas. */
    4573                 :             : template <typename ManagedTokenSource>
    4574                 :             : AST::TupleField
    4575                 :        1769 : Parser<ManagedTokenSource>::parse_tuple_field ()
    4576                 :             : {
    4577                 :             :   // parse outer attributes if they exist
    4578                 :        1769 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4579                 :             : 
    4580                 :             :   // parse visibility if it exists
    4581                 :        1769 :   AST::Visibility vis = parse_visibility ();
    4582                 :             : 
    4583                 :        1769 :   location_t locus = lexer.peek_token ()->get_locus ();
    4584                 :             : 
    4585                 :             :   // parse type, which is required
    4586                 :        1769 :   std::unique_ptr<AST::Type> field_type = parse_type ();
    4587                 :        1769 :   if (field_type == nullptr)
    4588                 :             :     {
    4589                 :             :       // error if null
    4590                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    4591                 :             :                    "could not parse type in tuple struct field");
    4592                 :           0 :       add_error (std::move (error));
    4593                 :             : 
    4594                 :             :       // skip after something
    4595                 :           0 :       return AST::TupleField::create_error ();
    4596                 :           0 :     }
    4597                 :             : 
    4598                 :        1769 :   return AST::TupleField (std::move (field_type), std::move (vis), locus,
    4599                 :        1769 :                           std::move (outer_attrs));
    4600                 :        1769 : }
    4601                 :             : 
    4602                 :             : // Parses a Rust "enum" tagged union item definition.
    4603                 :             : template <typename ManagedTokenSource>
    4604                 :             : std::unique_ptr<AST::Enum>
    4605                 :         319 : Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis,
    4606                 :             :                                         AST::AttrVec outer_attrs)
    4607                 :             : {
    4608                 :         319 :   location_t locus = lexer.peek_token ()->get_locus ();
    4609                 :         319 :   skip_token (ENUM_KW);
    4610                 :             : 
    4611                 :             :   // parse enum name
    4612                 :         319 :   const_TokenPtr enum_name_tok = expect_token (IDENTIFIER);
    4613                 :         319 :   if (enum_name_tok == nullptr)
    4614                 :           0 :     return nullptr;
    4615                 :             : 
    4616                 :         319 :   Identifier enum_name = {enum_name_tok};
    4617                 :             : 
    4618                 :             :   // parse generic params (of enum container, not enum variants) if they exist
    4619                 :         319 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4620                 :             :     = parse_generic_params_in_angles ();
    4621                 :             : 
    4622                 :             :   // parse where clause if it exists
    4623                 :         319 :   AST::WhereClause where_clause = parse_where_clause ();
    4624                 :             : 
    4625                 :         319 :   if (!skip_token (LEFT_CURLY))
    4626                 :             :     {
    4627                 :           0 :       skip_after_end_block ();
    4628                 :           0 :       return nullptr;
    4629                 :             :     }
    4630                 :             : 
    4631                 :             :   // parse actual enum variant definitions
    4632                 :         319 :   std::vector<std::unique_ptr<AST::EnumItem>> enum_items
    4633                 :             :     = parse_enum_items ([] (TokenId id) { return id == RIGHT_CURLY; });
    4634                 :             : 
    4635                 :         319 :   if (!skip_token (RIGHT_CURLY))
    4636                 :             :     {
    4637                 :           0 :       skip_after_end_block ();
    4638                 :           0 :       return nullptr;
    4639                 :             :     }
    4640                 :             : 
    4641                 :             :   return std::unique_ptr<AST::Enum> (
    4642                 :         319 :     new AST::Enum (std::move (enum_name), std::move (vis),
    4643                 :             :                    std::move (generic_params), std::move (where_clause),
    4644                 :         319 :                    std::move (enum_items), std::move (outer_attrs), locus));
    4645                 :         638 : }
    4646                 :             : 
    4647                 :             : // Parses the enum variants inside an enum definiton.
    4648                 :             : template <typename ManagedTokenSource>
    4649                 :             : std::vector<std::unique_ptr<AST::EnumItem>>
    4650                 :             : Parser<ManagedTokenSource>::parse_enum_items ()
    4651                 :             : {
    4652                 :             :   std::vector<std::unique_ptr<AST::EnumItem>> items;
    4653                 :             : 
    4654                 :             :   std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
    4655                 :             : 
    4656                 :             :   // Return empty item list if no field there
    4657                 :             :   if (initial_item == nullptr)
    4658                 :             :     return items;
    4659                 :             : 
    4660                 :             :   items.push_back (std::move (initial_item));
    4661                 :             : 
    4662                 :             :   while (lexer.peek_token ()->get_id () == COMMA)
    4663                 :             :     {
    4664                 :             :       lexer.skip_token ();
    4665                 :             : 
    4666                 :             :       std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
    4667                 :             :       if (item == nullptr)
    4668                 :             :         {
    4669                 :             :           // this would occur with a trailing comma, which is allowed
    4670                 :             :           break;
    4671                 :             :         }
    4672                 :             : 
    4673                 :             :       items.push_back (std::move (item));
    4674                 :             :     }
    4675                 :             : 
    4676                 :             :   items.shrink_to_fit ();
    4677                 :             :   return items;
    4678                 :             : 
    4679                 :             :   /* TODO: use template if doable (parse_non_ptr_sequence) */
    4680                 :             : }
    4681                 :             : 
    4682                 :             : // Parses the enum variants inside an enum definiton.
    4683                 :             : template <typename ManagedTokenSource>
    4684                 :             : template <typename EndTokenPred>
    4685                 :             : std::vector<std::unique_ptr<AST::EnumItem>>
    4686                 :         319 : Parser<ManagedTokenSource>::parse_enum_items (EndTokenPred is_end_tok)
    4687                 :             : {
    4688                 :         319 :   std::vector<std::unique_ptr<AST::EnumItem>> items;
    4689                 :             : 
    4690                 :         319 :   std::unique_ptr<AST::EnumItem> initial_item = parse_enum_item ();
    4691                 :             : 
    4692                 :             :   // Return empty item list if no field there
    4693                 :         319 :   if (initial_item == nullptr)
    4694                 :          20 :     return items;
    4695                 :             : 
    4696                 :         299 :   items.push_back (std::move (initial_item));
    4697                 :             : 
    4698                 :        1470 :   while (lexer.peek_token ()->get_id () == COMMA)
    4699                 :             :     {
    4700                 :         706 :       lexer.skip_token ();
    4701                 :             : 
    4702                 :        1412 :       if (is_end_tok (lexer.peek_token ()->get_id ()))
    4703                 :             :         break;
    4704                 :             : 
    4705                 :         436 :       std::unique_ptr<AST::EnumItem> item = parse_enum_item ();
    4706                 :         436 :       if (item == nullptr)
    4707                 :             :         {
    4708                 :             :           /* TODO should this ignore all successfully parsed enum items just
    4709                 :             :            * because one failed? */
    4710                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    4711                 :             :                        "failed to parse enum item in enum items");
    4712                 :           0 :           add_error (std::move (error));
    4713                 :             : 
    4714                 :           0 :           return {};
    4715                 :           0 :         }
    4716                 :             : 
    4717                 :         436 :       items.push_back (std::move (item));
    4718                 :             :     }
    4719                 :             : 
    4720                 :         299 :   items.shrink_to_fit ();
    4721                 :         299 :   return items;
    4722                 :             : 
    4723                 :             :   /* TODO: use template if doable (parse_non_ptr_sequence) */
    4724                 :         319 : }
    4725                 :             : 
    4726                 :             : /* Parses a single enum variant item in an enum definition. Does not parse
    4727                 :             :  * commas. */
    4728                 :             : template <typename ManagedTokenSource>
    4729                 :             : std::unique_ptr<AST::EnumItem>
    4730                 :         755 : Parser<ManagedTokenSource>::parse_enum_item ()
    4731                 :             : {
    4732                 :             :   // parse outer attributes if they exist
    4733                 :         755 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    4734                 :             : 
    4735                 :             :   // parse visibility, which may or may not exist
    4736                 :         755 :   AST::Visibility vis = parse_visibility ();
    4737                 :             : 
    4738                 :             :   // parse name for enum item, which is required
    4739                 :         755 :   const_TokenPtr item_name_tok = lexer.peek_token ();
    4740                 :         755 :   if (item_name_tok->get_id () != IDENTIFIER)
    4741                 :             :     {
    4742                 :             :       // this may not be an error but it means there is no enum item here
    4743                 :          20 :       return nullptr;
    4744                 :             :     }
    4745                 :         735 :   lexer.skip_token ();
    4746                 :         735 :   Identifier item_name{item_name_tok};
    4747                 :             : 
    4748                 :             :   // branch based on next token
    4749                 :         735 :   const_TokenPtr t = lexer.peek_token ();
    4750                 :         735 :   switch (t->get_id ())
    4751                 :             :     {
    4752                 :         291 :       case LEFT_PAREN: {
    4753                 :             :         // tuple enum item
    4754                 :         291 :         lexer.skip_token ();
    4755                 :             : 
    4756                 :         291 :         std::vector<AST::TupleField> tuple_fields;
    4757                 :             :         // Might be empty tuple for unit tuple enum variant.
    4758                 :         582 :         if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    4759                 :          25 :           tuple_fields = std::vector<AST::TupleField> ();
    4760                 :             :         else
    4761                 :         266 :           tuple_fields = parse_tuple_fields ();
    4762                 :             : 
    4763                 :         291 :         if (!skip_token (RIGHT_PAREN))
    4764                 :             :           {
    4765                 :             :             // skip after somewhere
    4766                 :           0 :             return nullptr;
    4767                 :             :           }
    4768                 :             : 
    4769                 :         291 :         return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
    4770                 :             :           std::move (item_name), std::move (vis), std::move (tuple_fields),
    4771                 :         291 :           std::move (outer_attrs), item_name_tok->get_locus ()));
    4772                 :         291 :       }
    4773                 :          78 :       case LEFT_CURLY: {
    4774                 :             :         // struct enum item
    4775                 :          78 :         lexer.skip_token ();
    4776                 :             : 
    4777                 :          78 :         std::vector<AST::StructField> struct_fields
    4778                 :             :           = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    4779                 :             : 
    4780                 :          78 :         if (!skip_token (RIGHT_CURLY))
    4781                 :             :           {
    4782                 :             :             // skip after somewhere
    4783                 :           0 :             return nullptr;
    4784                 :             :           }
    4785                 :             : 
    4786                 :          78 :         return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
    4787                 :             :           std::move (item_name), std::move (vis), std::move (struct_fields),
    4788                 :          78 :           std::move (outer_attrs), item_name_tok->get_locus ()));
    4789                 :          78 :       }
    4790                 :          32 :       case EQUAL: {
    4791                 :             :         // discriminant enum item
    4792                 :          32 :         lexer.skip_token ();
    4793                 :             : 
    4794                 :          32 :         std::unique_ptr<AST::Expr> discriminant_expr = parse_expr ();
    4795                 :             : 
    4796                 :          32 :         return std::unique_ptr<AST::EnumItemDiscriminant> (
    4797                 :          64 :           new AST::EnumItemDiscriminant (std::move (item_name), std::move (vis),
    4798                 :             :                                          std::move (discriminant_expr),
    4799                 :             :                                          std::move (outer_attrs),
    4800                 :          32 :                                          item_name_tok->get_locus ()));
    4801                 :          32 :       }
    4802                 :         334 :     default:
    4803                 :             :       // regular enum with just an identifier
    4804                 :             :       return std::unique_ptr<AST::EnumItem> (
    4805                 :         334 :         new AST::EnumItem (std::move (item_name), std::move (vis),
    4806                 :             :                            std::move (outer_attrs),
    4807                 :         334 :                            item_name_tok->get_locus ()));
    4808                 :             :     }
    4809                 :        1490 : }
    4810                 :             : 
    4811                 :             : // Parses a C-style (and C-compat) untagged union declaration.
    4812                 :             : template <typename ManagedTokenSource>
    4813                 :             : std::unique_ptr<AST::Union>
    4814                 :         114 : Parser<ManagedTokenSource>::parse_union (AST::Visibility vis,
    4815                 :             :                                          AST::AttrVec outer_attrs)
    4816                 :             : {
    4817                 :             :   /* hack - "weak keyword" by finding identifier called "union" (lookahead in
    4818                 :             :    * item switch) */
    4819                 :         114 :   const_TokenPtr union_keyword = expect_token (IDENTIFIER);
    4820                 :         114 :   rust_assert (union_keyword->get_str () == Values::WeakKeywords::UNION);
    4821                 :         114 :   location_t locus = union_keyword->get_locus ();
    4822                 :             : 
    4823                 :             :   // parse actual union name
    4824                 :         114 :   const_TokenPtr union_name_tok = expect_token (IDENTIFIER);
    4825                 :         114 :   if (union_name_tok == nullptr)
    4826                 :             :     {
    4827                 :           0 :       skip_after_next_block ();
    4828                 :           0 :       return nullptr;
    4829                 :             :     }
    4830                 :         114 :   Identifier union_name{union_name_tok};
    4831                 :             : 
    4832                 :             :   // parse optional generic parameters
    4833                 :         114 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    4834                 :             :     = parse_generic_params_in_angles ();
    4835                 :             : 
    4836                 :             :   // parse optional where clause
    4837                 :         114 :   AST::WhereClause where_clause = parse_where_clause ();
    4838                 :             : 
    4839                 :         114 :   if (!skip_token (LEFT_CURLY))
    4840                 :             :     {
    4841                 :           0 :       skip_after_end_block ();
    4842                 :           0 :       return nullptr;
    4843                 :             :     }
    4844                 :             : 
    4845                 :             :   /* parse union inner items as "struct fields" because hey, syntax reuse.
    4846                 :             :    * Spec said so. */
    4847                 :         114 :   std::vector<AST::StructField> union_fields
    4848                 :             :     = parse_struct_fields ([] (TokenId id) { return id == RIGHT_CURLY; });
    4849                 :             : 
    4850                 :         114 :   if (!skip_token (RIGHT_CURLY))
    4851                 :             :     {
    4852                 :             :       // skip after somewhere
    4853                 :           0 :       return nullptr;
    4854                 :             :     }
    4855                 :             : 
    4856                 :             :   return std::unique_ptr<AST::Union> (
    4857                 :         114 :     new AST::Union (std::move (union_name), std::move (vis),
    4858                 :             :                     std::move (generic_params), std::move (where_clause),
    4859                 :         114 :                     std::move (union_fields), std::move (outer_attrs), locus));
    4860                 :         342 : }
    4861                 :             : 
    4862                 :             : /* Parses a "constant item" (compile-time constant to maybe "inline"
    4863                 :             :  * throughout the program - like constexpr). */
    4864                 :             : template <typename ManagedTokenSource>
    4865                 :             : std::unique_ptr<AST::ConstantItem>
    4866                 :         540 : Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
    4867                 :             :                                               AST::AttrVec outer_attrs)
    4868                 :             : {
    4869                 :         540 :   location_t locus = lexer.peek_token ()->get_locus ();
    4870                 :         540 :   skip_token (CONST);
    4871                 :             : 
    4872                 :             :   /* get constant identifier - this is either a proper identifier or the _
    4873                 :             :    * wildcard */
    4874                 :         540 :   const_TokenPtr ident_tok = lexer.peek_token ();
    4875                 :             :   // make default identifier the underscore wildcard one
    4876                 :         540 :   std::string ident (Values::Keywords::UNDERSCORE);
    4877                 :         540 :   switch (ident_tok->get_id ())
    4878                 :             :     {
    4879                 :         540 :     case IDENTIFIER:
    4880                 :         540 :       ident = ident_tok->get_str ();
    4881                 :         540 :       lexer.skip_token ();
    4882                 :             :       break;
    4883                 :           0 :     case UNDERSCORE:
    4884                 :             :       // do nothing - identifier is already "_"
    4885                 :           0 :       lexer.skip_token ();
    4886                 :             :       break;
    4887                 :           0 :     default:
    4888                 :           0 :       add_error (
    4889                 :           0 :         Error (ident_tok->get_locus (),
    4890                 :             :                "expected item name (identifier or %<_%>) in constant item "
    4891                 :             :                "declaration - found %qs",
    4892                 :             :                ident_tok->get_token_description ()));
    4893                 :             : 
    4894                 :           0 :       skip_after_semicolon ();
    4895                 :           0 :       return nullptr;
    4896                 :             :     }
    4897                 :             : 
    4898                 :         540 :   if (!skip_token (COLON))
    4899                 :             :     {
    4900                 :           0 :       skip_after_semicolon ();
    4901                 :           0 :       return nullptr;
    4902                 :             :     }
    4903                 :             : 
    4904                 :             :   // parse constant type (required)
    4905                 :         540 :   std::unique_ptr<AST::Type> type = parse_type ();
    4906                 :             : 
    4907                 :             :   // A const with no given expression value
    4908                 :        1080 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    4909                 :             :     {
    4910                 :           4 :       lexer.skip_token ();
    4911                 :             :       return std::unique_ptr<AST::ConstantItem> (
    4912                 :           4 :         new AST::ConstantItem (std::move (ident), std::move (vis),
    4913                 :             :                                std::move (type), std::move (outer_attrs),
    4914                 :           4 :                                locus));
    4915                 :             :     }
    4916                 :             : 
    4917                 :         536 :   if (!skip_token (EQUAL))
    4918                 :             :     {
    4919                 :           0 :       skip_after_semicolon ();
    4920                 :           0 :       return nullptr;
    4921                 :             :     }
    4922                 :             : 
    4923                 :             :   // parse constant expression (required)
    4924                 :         536 :   std::unique_ptr<AST::Expr> expr = parse_expr ();
    4925                 :             : 
    4926                 :         536 :   if (!skip_token (SEMICOLON))
    4927                 :             :     {
    4928                 :             :       // skip somewhere?
    4929                 :           0 :       return nullptr;
    4930                 :             :     }
    4931                 :             : 
    4932                 :             :   return std::unique_ptr<AST::ConstantItem> (
    4933                 :         536 :     new AST::ConstantItem (std::move (ident), std::move (vis), std::move (type),
    4934                 :         536 :                            std::move (expr), std::move (outer_attrs), locus));
    4935                 :        1080 : }
    4936                 :             : 
    4937                 :             : // Parses a "static item" (static storage item, with 'static lifetime).
    4938                 :             : template <typename ManagedTokenSource>
    4939                 :             : std::unique_ptr<AST::StaticItem>
    4940                 :          67 : Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis,
    4941                 :             :                                                AST::AttrVec outer_attrs)
    4942                 :             : {
    4943                 :          67 :   location_t locus = lexer.peek_token ()->get_locus ();
    4944                 :          67 :   skip_token (STATIC_KW);
    4945                 :             : 
    4946                 :             :   // determine whether static item is mutable
    4947                 :          67 :   bool is_mut = false;
    4948                 :         134 :   if (lexer.peek_token ()->get_id () == MUT)
    4949                 :             :     {
    4950                 :           4 :       is_mut = true;
    4951                 :           4 :       lexer.skip_token ();
    4952                 :             :     }
    4953                 :             : 
    4954                 :          67 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    4955                 :          67 :   if (ident_tok == nullptr)
    4956                 :           0 :     return nullptr;
    4957                 :             : 
    4958                 :          67 :   Identifier ident{ident_tok};
    4959                 :             : 
    4960                 :          67 :   if (!skip_token (COLON))
    4961                 :             :     {
    4962                 :           2 :       skip_after_semicolon ();
    4963                 :           2 :       return nullptr;
    4964                 :             :     }
    4965                 :             : 
    4966                 :             :   // parse static item type (required)
    4967                 :          65 :   std::unique_ptr<AST::Type> type = parse_type ();
    4968                 :             : 
    4969                 :          65 :   if (!skip_token (EQUAL))
    4970                 :             :     {
    4971                 :           0 :       skip_after_semicolon ();
    4972                 :           0 :       return nullptr;
    4973                 :             :     }
    4974                 :             : 
    4975                 :             :   // parse static item expression (required)
    4976                 :          65 :   std::unique_ptr<AST::Expr> expr = parse_expr ();
    4977                 :             : 
    4978                 :          65 :   if (!skip_token (SEMICOLON))
    4979                 :             :     {
    4980                 :             :       // skip after somewhere
    4981                 :           0 :       return nullptr;
    4982                 :             :     }
    4983                 :             : 
    4984                 :             :   return std::unique_ptr<AST::StaticItem> (
    4985                 :          65 :     new AST::StaticItem (std::move (ident), is_mut, std::move (type),
    4986                 :             :                          std::move (expr), std::move (vis),
    4987                 :          65 :                          std::move (outer_attrs), locus));
    4988                 :         132 : }
    4989                 :             : 
    4990                 :             : // Parses a trait definition item, including unsafe ones.
    4991                 :             : template <typename ManagedTokenSource>
    4992                 :             : std::unique_ptr<AST::Trait>
    4993                 :        2676 : Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis,
    4994                 :             :                                          AST::AttrVec outer_attrs)
    4995                 :             : {
    4996                 :        2676 :   location_t locus = lexer.peek_token ()->get_locus ();
    4997                 :        2676 :   bool is_unsafe = false;
    4998                 :        2676 :   bool is_auto_trait = false;
    4999                 :             : 
    5000                 :        5352 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    5001                 :             :     {
    5002                 :          70 :       is_unsafe = true;
    5003                 :          70 :       lexer.skip_token ();
    5004                 :             :     }
    5005                 :             : 
    5006                 :        5352 :   if (lexer.peek_token ()->get_id () == AUTO)
    5007                 :             :     {
    5008                 :          40 :       is_auto_trait = true;
    5009                 :          40 :       lexer.skip_token ();
    5010                 :             :     }
    5011                 :             : 
    5012                 :        2676 :   skip_token (TRAIT);
    5013                 :             : 
    5014                 :             :   // parse trait name
    5015                 :        2676 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5016                 :        2676 :   if (ident_tok == nullptr)
    5017                 :           0 :     return nullptr;
    5018                 :             : 
    5019                 :        2676 :   Identifier ident{ident_tok};
    5020                 :             : 
    5021                 :             :   // parse generic parameters (if they exist)
    5022                 :        2676 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    5023                 :             :     = parse_generic_params_in_angles ();
    5024                 :             : 
    5025                 :             :   // create placeholder type param bounds in case they don't exist
    5026                 :        2676 :   std::vector<std::unique_ptr<AST::TypeParamBound>> type_param_bounds;
    5027                 :             : 
    5028                 :             :   // parse type param bounds (if they exist)
    5029                 :        5352 :   if (lexer.peek_token ()->get_id () == COLON)
    5030                 :             :     {
    5031                 :         224 :       lexer.skip_token ();
    5032                 :             : 
    5033                 :         224 :       type_param_bounds = parse_type_param_bounds (
    5034                 :           2 :         [] (TokenId id) { return id == WHERE || id == LEFT_CURLY; });
    5035                 :             :       // type_param_bounds = parse_type_param_bounds ();
    5036                 :             :     }
    5037                 :             : 
    5038                 :             :   // parse where clause (if it exists)
    5039                 :        2676 :   AST::WhereClause where_clause = parse_where_clause ();
    5040                 :             : 
    5041                 :        2676 :   if (!skip_token (LEFT_CURLY))
    5042                 :             :     {
    5043                 :           0 :       skip_after_end_block ();
    5044                 :           0 :       return nullptr;
    5045                 :             :     }
    5046                 :             : 
    5047                 :             :   // parse inner attrs (if they exist)
    5048                 :        2676 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    5049                 :             : 
    5050                 :             :   // parse trait items
    5051                 :        2676 :   std::vector<std::unique_ptr<AST::AssociatedItem>> trait_items;
    5052                 :             : 
    5053                 :        2676 :   const_TokenPtr t = lexer.peek_token ();
    5054                 :        4611 :   while (t->get_id () != RIGHT_CURLY)
    5055                 :             :     {
    5056                 :        1935 :       std::unique_ptr<AST::AssociatedItem> trait_item = parse_trait_item ();
    5057                 :             : 
    5058                 :        1935 :       if (trait_item == nullptr)
    5059                 :             :         {
    5060                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    5061                 :             :                        "failed to parse trait item in trait");
    5062                 :           0 :           add_error (std::move (error));
    5063                 :             : 
    5064                 :           0 :           return nullptr;
    5065                 :           0 :         }
    5066                 :        1935 :       trait_items.push_back (std::move (trait_item));
    5067                 :             : 
    5068                 :        1935 :       t = lexer.peek_token ();
    5069                 :             :     }
    5070                 :             : 
    5071                 :        2676 :   if (!skip_token (RIGHT_CURLY))
    5072                 :             :     {
    5073                 :             :       // skip after something
    5074                 :           0 :       return nullptr;
    5075                 :             :     }
    5076                 :             : 
    5077                 :             :   trait_items.shrink_to_fit ();
    5078                 :             :   return std::unique_ptr<AST::Trait> (
    5079                 :        2676 :     new AST::Trait (std::move (ident), is_unsafe, is_auto_trait,
    5080                 :             :                     std::move (generic_params), std::move (type_param_bounds),
    5081                 :             :                     std::move (where_clause), std::move (trait_items),
    5082                 :             :                     std::move (vis), std::move (outer_attrs),
    5083                 :        2676 :                     std::move (inner_attrs), locus));
    5084                 :        5352 : }
    5085                 :             : 
    5086                 :             : // Parses a trait item used inside traits (not trait, the Item).
    5087                 :             : template <typename ManagedTokenSource>
    5088                 :             : std::unique_ptr<AST::AssociatedItem>
    5089                 :        1939 : Parser<ManagedTokenSource>::parse_trait_item ()
    5090                 :             : {
    5091                 :             :   // parse outer attributes (if they exist)
    5092                 :        1939 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5093                 :             : 
    5094                 :        1939 :   AST::Visibility vis = parse_visibility ();
    5095                 :             : 
    5096                 :             :   // lookahead to determine what type of trait item to parse
    5097                 :        1939 :   const_TokenPtr tok = lexer.peek_token ();
    5098                 :        1939 :   switch (tok->get_id ())
    5099                 :             :     {
    5100                 :           0 :     case SUPER:
    5101                 :             :     case SELF:
    5102                 :             :     case CRATE:
    5103                 :             :     case DOLLAR_SIGN:
    5104                 :             :       // these seem to be SimplePath tokens, so this is a macro invocation
    5105                 :             :       // semi
    5106                 :           0 :       return parse_macro_invocation_semi (std::move (outer_attrs));
    5107                 :           2 :     case IDENTIFIER:
    5108                 :           4 :       if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
    5109                 :           0 :         return parse_function (std::move (vis), std::move (outer_attrs));
    5110                 :             :       else
    5111                 :           4 :         return parse_macro_invocation_semi (std::move (outer_attrs));
    5112                 :         561 :     case TYPE:
    5113                 :         561 :       return parse_trait_type (std::move (outer_attrs), vis);
    5114                 :          46 :     case CONST:
    5115                 :             :       // disambiguate with function qualifier
    5116                 :          92 :       if (lexer.peek_token (1)->get_id () == IDENTIFIER)
    5117                 :             :         {
    5118                 :          44 :           return parse_trait_const (std::move (outer_attrs));
    5119                 :             :         }
    5120                 :             :       // else, fallthrough to function
    5121                 :             :       // TODO: find out how to disable gcc "implicit fallthrough" error
    5122                 :             :       gcc_fallthrough ();
    5123                 :             :     case ASYNC:
    5124                 :             :     case UNSAFE:
    5125                 :             :     case EXTERN_KW:
    5126                 :             :     case FN_KW:
    5127                 :        2664 :       return parse_function (std::move (vis), std::move (outer_attrs));
    5128                 :             :     default:
    5129                 :             :       break;
    5130                 :             :     }
    5131                 :           0 :   add_error (Error (tok->get_locus (),
    5132                 :             :                     "unrecognised token %qs for item in trait",
    5133                 :             :                     tok->get_token_description ()));
    5134                 :             :   // skip?
    5135                 :           0 :   return nullptr;
    5136                 :        1939 : }
    5137                 :             : 
    5138                 :             : // Parse a typedef trait item.
    5139                 :             : template <typename ManagedTokenSource>
    5140                 :             : std::unique_ptr<AST::TraitItemType>
    5141                 :         561 : Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs,
    5142                 :             :                                               AST::Visibility vis)
    5143                 :             : {
    5144                 :         561 :   location_t locus = lexer.peek_token ()->get_locus ();
    5145                 :         561 :   skip_token (TYPE);
    5146                 :             : 
    5147                 :         561 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5148                 :         561 :   if (ident_tok == nullptr)
    5149                 :           0 :     return nullptr;
    5150                 :             : 
    5151                 :        1122 :   Identifier ident{ident_tok};
    5152                 :             : 
    5153                 :         561 :   std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    5154                 :             : 
    5155                 :             :   // parse optional colon
    5156                 :        1122 :   if (lexer.peek_token ()->get_id () == COLON)
    5157                 :             :     {
    5158                 :          28 :       lexer.skip_token ();
    5159                 :             : 
    5160                 :             :       // parse optional type param bounds
    5161                 :             :       bounds
    5162                 :          28 :         = parse_type_param_bounds ([] (TokenId id) { return id == SEMICOLON; });
    5163                 :             :       // bounds = parse_type_param_bounds ();
    5164                 :             :     }
    5165                 :             : 
    5166                 :         561 :   if (!skip_token (SEMICOLON))
    5167                 :             :     {
    5168                 :             :       // skip?
    5169                 :           0 :       return nullptr;
    5170                 :             :     }
    5171                 :             : 
    5172                 :             :   return std::unique_ptr<AST::TraitItemType> (
    5173                 :         561 :     new AST::TraitItemType (std::move (ident), std::move (bounds),
    5174                 :         561 :                             std::move (outer_attrs), vis, locus));
    5175                 :         561 : }
    5176                 :             : 
    5177                 :             : // Parses a constant trait item.
    5178                 :             : template <typename ManagedTokenSource>
    5179                 :             : std::unique_ptr<AST::TraitItemConst>
    5180                 :          44 : Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs)
    5181                 :             : {
    5182                 :          44 :   location_t locus = lexer.peek_token ()->get_locus ();
    5183                 :          44 :   skip_token (CONST);
    5184                 :             : 
    5185                 :             :   // parse constant item name
    5186                 :          44 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5187                 :          44 :   if (ident_tok == nullptr)
    5188                 :           0 :     return nullptr;
    5189                 :             : 
    5190                 :          44 :   Identifier ident{ident_tok};
    5191                 :             : 
    5192                 :          44 :   if (!skip_token (COLON))
    5193                 :             :     {
    5194                 :           0 :       skip_after_semicolon ();
    5195                 :           0 :       return nullptr;
    5196                 :             :     }
    5197                 :             : 
    5198                 :             :   // parse constant trait item type
    5199                 :          44 :   std::unique_ptr<AST::Type> type = parse_type ();
    5200                 :             : 
    5201                 :             :   // parse constant trait body expression, if it exists
    5202                 :          44 :   std::unique_ptr<AST::Expr> const_body = nullptr;
    5203                 :          88 :   if (lexer.peek_token ()->get_id () == EQUAL)
    5204                 :             :     {
    5205                 :          13 :       lexer.skip_token ();
    5206                 :             : 
    5207                 :             :       // expression must exist, so parse it
    5208                 :          13 :       const_body = parse_expr ();
    5209                 :             :     }
    5210                 :             : 
    5211                 :          44 :   if (!skip_token (SEMICOLON))
    5212                 :             :     {
    5213                 :             :       // skip after something?
    5214                 :           0 :       return nullptr;
    5215                 :             :     }
    5216                 :             : 
    5217                 :             :   return std::unique_ptr<AST::TraitItemConst> (
    5218                 :          44 :     new AST::TraitItemConst (std::move (ident), std::move (type),
    5219                 :             :                              std::move (const_body), std::move (outer_attrs),
    5220                 :          44 :                              locus));
    5221                 :          88 : }
    5222                 :             : 
    5223                 :             : /* Parses a struct "impl" item (both inherent impl and trait impl can be
    5224                 :             :  * parsed here), */
    5225                 :             : template <typename ManagedTokenSource>
    5226                 :             : std::unique_ptr<AST::Impl>
    5227                 :        3622 : Parser<ManagedTokenSource>::parse_impl (AST::Visibility vis,
    5228                 :             :                                         AST::AttrVec outer_attrs)
    5229                 :             : {
    5230                 :             :   /* Note that only trait impls are allowed to be unsafe. So if unsafe, it
    5231                 :             :    * must be a trait impl. However, this isn't enough for full disambiguation,
    5232                 :             :    * so don't branch here. */
    5233                 :        3622 :   location_t locus = lexer.peek_token ()->get_locus ();
    5234                 :        3622 :   bool is_unsafe = false;
    5235                 :        7244 :   if (lexer.peek_token ()->get_id () == UNSAFE)
    5236                 :             :     {
    5237                 :          75 :       lexer.skip_token ();
    5238                 :          75 :       is_unsafe = true;
    5239                 :             :     }
    5240                 :             : 
    5241                 :        3622 :   if (!skip_token (IMPL))
    5242                 :             :     {
    5243                 :           0 :       skip_after_next_block ();
    5244                 :           0 :       return nullptr;
    5245                 :             :     }
    5246                 :             : 
    5247                 :             :   // parse generic params (shared by trait and inherent impls)
    5248                 :        3622 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    5249                 :             :     = parse_generic_params_in_angles ();
    5250                 :             : 
    5251                 :             :   // Again, trait impl-only feature, but optional one, so can be used for
    5252                 :             :   // branching yet.
    5253                 :        3622 :   bool has_exclam = false;
    5254                 :        7244 :   if (lexer.peek_token ()->get_id () == EXCLAM)
    5255                 :             :     {
    5256                 :          12 :       lexer.skip_token ();
    5257                 :          12 :       has_exclam = true;
    5258                 :             :     }
    5259                 :             : 
    5260                 :             :   /* FIXME: code that doesn't look shit for TypePath. Also, make sure this
    5261                 :             :    * doesn't parse too much and not work. */
    5262                 :        3622 :   AST::TypePath type_path = parse_type_path ();
    5263                 :        7069 :   if (type_path.is_error () || lexer.peek_token ()->get_id () != FOR)
    5264                 :             :     {
    5265                 :             :       /* cannot parse type path (or not for token next, at least), so must be
    5266                 :             :        * inherent impl */
    5267                 :             : 
    5268                 :             :       // hacky conversion of TypePath stack object to Type pointer
    5269                 :         840 :       std::unique_ptr<AST::Type> type = nullptr;
    5270                 :         840 :       if (!type_path.is_error ())
    5271                 :         665 :         type = std::unique_ptr<AST::TypePath> (
    5272                 :         665 :           new AST::TypePath (std::move (type_path)));
    5273                 :             :       else
    5274                 :         175 :         type = parse_type ();
    5275                 :             : 
    5276                 :             :       // Type is required, so error if null
    5277                 :         840 :       if (type == nullptr)
    5278                 :             :         {
    5279                 :           2 :           Error error (lexer.peek_token ()->get_locus (),
    5280                 :             :                        "could not parse type in inherent impl");
    5281                 :           2 :           add_error (std::move (error));
    5282                 :             : 
    5283                 :           2 :           skip_after_next_block ();
    5284                 :           2 :           return nullptr;
    5285                 :           2 :         }
    5286                 :             : 
    5287                 :             :       // parse optional where clause
    5288                 :         838 :       AST::WhereClause where_clause = parse_where_clause ();
    5289                 :             : 
    5290                 :         838 :       if (!skip_token (LEFT_CURLY))
    5291                 :             :         {
    5292                 :             :           // TODO: does this still skip properly?
    5293                 :           0 :           skip_after_end_block ();
    5294                 :           0 :           return nullptr;
    5295                 :             :         }
    5296                 :             : 
    5297                 :             :       // parse inner attributes (optional)
    5298                 :         838 :       AST::AttrVec inner_attrs = parse_inner_attributes ();
    5299                 :             : 
    5300                 :             :       // parse inherent impl items
    5301                 :         838 :       std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
    5302                 :             : 
    5303                 :         838 :       const_TokenPtr t = lexer.peek_token ();
    5304                 :        2672 :       while (t->get_id () != RIGHT_CURLY)
    5305                 :             :         {
    5306                 :        1847 :           std::unique_ptr<AST::AssociatedItem> impl_item
    5307                 :             :             = parse_inherent_impl_item ();
    5308                 :             : 
    5309                 :        1847 :           if (impl_item == nullptr)
    5310                 :             :             {
    5311                 :          13 :               Error error (
    5312                 :          13 :                 lexer.peek_token ()->get_locus (),
    5313                 :             :                 "failed to parse inherent impl item in inherent impl");
    5314                 :          13 :               add_error (std::move (error));
    5315                 :             : 
    5316                 :          13 :               return nullptr;
    5317                 :          13 :             }
    5318                 :             : 
    5319                 :        1834 :           impl_items.push_back (std::move (impl_item));
    5320                 :             : 
    5321                 :        1834 :           t = lexer.peek_token ();
    5322                 :             :         }
    5323                 :             : 
    5324                 :         825 :       if (!skip_token (RIGHT_CURLY))
    5325                 :             :         {
    5326                 :             :           // skip somewhere
    5327                 :           0 :           return nullptr;
    5328                 :             :         }
    5329                 :             : 
    5330                 :             :       // DEBUG
    5331                 :         825 :       rust_debug ("successfully parsed inherent impl");
    5332                 :             : 
    5333                 :             :       impl_items.shrink_to_fit ();
    5334                 :             : 
    5335                 :         825 :       return std::unique_ptr<AST::InherentImpl> (new AST::InherentImpl (
    5336                 :             :         std::move (impl_items), std::move (generic_params), std::move (type),
    5337                 :             :         std::move (where_clause), std::move (vis), std::move (inner_attrs),
    5338                 :         825 :         std::move (outer_attrs), locus));
    5339                 :        1678 :     }
    5340                 :             :   else
    5341                 :             :     {
    5342                 :             :       // type path must both be valid and next token is for, so trait impl
    5343                 :        2782 :       if (!skip_token (FOR))
    5344                 :             :         {
    5345                 :           0 :           skip_after_next_block ();
    5346                 :           0 :           return nullptr;
    5347                 :             :         }
    5348                 :             : 
    5349                 :             :       // parse type
    5350                 :        2782 :       std::unique_ptr<AST::Type> type = parse_type ();
    5351                 :             :       // ensure type is included as it is required
    5352                 :        2782 :       if (type == nullptr)
    5353                 :             :         {
    5354                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    5355                 :             :                        "could not parse type in trait impl");
    5356                 :           0 :           add_error (std::move (error));
    5357                 :             : 
    5358                 :           0 :           skip_after_next_block ();
    5359                 :           0 :           return nullptr;
    5360                 :           0 :         }
    5361                 :             : 
    5362                 :             :       // parse optional where clause
    5363                 :        2782 :       AST::WhereClause where_clause = parse_where_clause ();
    5364                 :             : 
    5365                 :        2782 :       if (!skip_token (LEFT_CURLY))
    5366                 :             :         {
    5367                 :             :           // TODO: does this still skip properly?
    5368                 :           0 :           skip_after_end_block ();
    5369                 :           0 :           return nullptr;
    5370                 :             :         }
    5371                 :             : 
    5372                 :             :       // parse inner attributes (optional)
    5373                 :        2782 :       AST::AttrVec inner_attrs = parse_inner_attributes ();
    5374                 :             : 
    5375                 :             :       // parse trait impl items
    5376                 :        2782 :       std::vector<std::unique_ptr<AST::AssociatedItem>> impl_items;
    5377                 :             : 
    5378                 :        2782 :       const_TokenPtr t = lexer.peek_token ();
    5379                 :        8904 :       while (t->get_id () != RIGHT_CURLY)
    5380                 :             :         {
    5381                 :        3061 :           std::unique_ptr<AST::AssociatedItem> impl_item
    5382                 :             :             = parse_trait_impl_item ();
    5383                 :             : 
    5384                 :        3061 :           if (impl_item == nullptr)
    5385                 :             :             {
    5386                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
    5387                 :             :                            "failed to parse trait impl item in trait impl");
    5388                 :           0 :               add_error (std::move (error));
    5389                 :             : 
    5390                 :           0 :               return nullptr;
    5391                 :           0 :             }
    5392                 :             : 
    5393                 :        3061 :           impl_items.push_back (std::move (impl_item));
    5394                 :             : 
    5395                 :        3061 :           t = lexer.peek_token ();
    5396                 :             : 
    5397                 :             :           // DEBUG
    5398                 :        3061 :           rust_debug ("successfully parsed a trait impl item");
    5399                 :             :         }
    5400                 :             :       // DEBUG
    5401                 :        2782 :       rust_debug ("successfully finished trait impl items");
    5402                 :             : 
    5403                 :        2782 :       if (!skip_token (RIGHT_CURLY))
    5404                 :             :         {
    5405                 :             :           // skip somewhere
    5406                 :           0 :           return nullptr;
    5407                 :             :         }
    5408                 :             : 
    5409                 :             :       // DEBUG
    5410                 :        2782 :       rust_debug ("successfully parsed trait impl");
    5411                 :             : 
    5412                 :             :       impl_items.shrink_to_fit ();
    5413                 :             : 
    5414                 :        2782 :       return std::unique_ptr<AST::TraitImpl> (
    5415                 :        5564 :         new AST::TraitImpl (std::move (type_path), is_unsafe, has_exclam,
    5416                 :             :                             std::move (impl_items), std::move (generic_params),
    5417                 :             :                             std::move (type), std::move (where_clause),
    5418                 :             :                             std::move (vis), std::move (inner_attrs),
    5419                 :        2782 :                             std::move (outer_attrs), locus));
    5420                 :        5564 :     }
    5421                 :        3622 : }
    5422                 :             : 
    5423                 :             : // Parses a single inherent impl item (item inside an inherent impl block).
    5424                 :             : template <typename ManagedTokenSource>
    5425                 :             : std::unique_ptr<AST::AssociatedItem>
    5426                 :        1851 : Parser<ManagedTokenSource>::parse_inherent_impl_item ()
    5427                 :             : {
    5428                 :             :   // parse outer attributes (if they exist)
    5429                 :        1851 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5430                 :             : 
    5431                 :             :   // TODO: cleanup - currently an unreadable mess
    5432                 :             : 
    5433                 :             :   // branch on next token:
    5434                 :        1851 :   const_TokenPtr t = lexer.peek_token ();
    5435                 :        1851 :   switch (t->get_id ())
    5436                 :             :     {
    5437                 :           2 :     case IDENTIFIER:
    5438                 :             :       // FIXME: Arthur: Do we need to some lookahead here?
    5439                 :           4 :       return parse_macro_invocation_semi (outer_attrs);
    5440                 :        1218 :     case SUPER:
    5441                 :             :     case SELF:
    5442                 :             :     case CRATE:
    5443                 :             :       case PUB: {
    5444                 :             :         // visibility, so not a macro invocation semi - must be constant,
    5445                 :             :         // function, or method
    5446                 :        1218 :         AST::Visibility vis = parse_visibility ();
    5447                 :             : 
    5448                 :             :         // TODO: is a recursive call to parse_inherent_impl_item better?
    5449                 :        2436 :         switch (lexer.peek_token ()->get_id ())
    5450                 :             :           {
    5451                 :         655 :           case EXTERN_KW:
    5452                 :             :           case UNSAFE:
    5453                 :             :           case FN_KW:
    5454                 :             :             // function or method
    5455                 :        1310 :             return parse_inherent_impl_function_or_method (std::move (vis),
    5456                 :             :                                                            std::move (
    5457                 :         655 :                                                              outer_attrs));
    5458                 :         563 :           case CONST:
    5459                 :             :             // lookahead to resolve production - could be function/method or
    5460                 :             :             // const item
    5461                 :         563 :             t = lexer.peek_token (1);
    5462                 :             : 
    5463                 :         563 :             switch (t->get_id ())
    5464                 :             :               {
    5465                 :           2 :               case IDENTIFIER:
    5466                 :             :               case UNDERSCORE:
    5467                 :           4 :                 return parse_const_item (std::move (vis),
    5468                 :           2 :                                          std::move (outer_attrs));
    5469                 :         561 :               case UNSAFE:
    5470                 :             :               case EXTERN_KW:
    5471                 :             :               case FN_KW:
    5472                 :        1122 :                 return parse_inherent_impl_function_or_method (std::move (vis),
    5473                 :             :                                                                std::move (
    5474                 :         561 :                                                                  outer_attrs));
    5475                 :           0 :               default:
    5476                 :           0 :                 add_error (Error (t->get_locus (),
    5477                 :             :                                   "unexpected token %qs in some sort of const "
    5478                 :             :                                   "item in inherent impl",
    5479                 :             :                                   t->get_token_description ()));
    5480                 :             : 
    5481                 :           0 :                 lexer.skip_token (1); // TODO: is this right thing to do?
    5482                 :           0 :                 return nullptr;
    5483                 :             :               }
    5484                 :           0 :           default:
    5485                 :           0 :             add_error (
    5486                 :           0 :               Error (t->get_locus (),
    5487                 :             :                      "unrecognised token %qs for item in inherent impl",
    5488                 :             :                      t->get_token_description ()));
    5489                 :             :             // skip?
    5490                 :           0 :             return nullptr;
    5491                 :             :           }
    5492                 :        1218 :       }
    5493                 :         615 :     case ASYNC:
    5494                 :             :     case EXTERN_KW:
    5495                 :             :     case UNSAFE:
    5496                 :             :     case FN_KW:
    5497                 :             :       // function or method
    5498                 :         615 :       return parse_inherent_impl_function_or_method (
    5499                 :         615 :         AST::Visibility::create_private (), std::move (outer_attrs));
    5500                 :          16 :     case CONST:
    5501                 :             :       /* lookahead to resolve production - could be function/method or const
    5502                 :             :        * item */
    5503                 :          16 :       t = lexer.peek_token (1);
    5504                 :             : 
    5505                 :          16 :       switch (t->get_id ())
    5506                 :             :         {
    5507                 :          16 :         case IDENTIFIER:
    5508                 :             :         case UNDERSCORE:
    5509                 :          32 :           return parse_const_item (AST::Visibility::create_private (),
    5510                 :          16 :                                    std::move (outer_attrs));
    5511                 :           0 :         case UNSAFE:
    5512                 :             :         case EXTERN_KW:
    5513                 :             :         case FN_KW:
    5514                 :           0 :           return parse_inherent_impl_function_or_method (
    5515                 :           0 :             AST::Visibility::create_private (), std::move (outer_attrs));
    5516                 :           0 :         default:
    5517                 :           0 :           add_error (Error (t->get_locus (),
    5518                 :             :                             "unexpected token %qs in some sort of const item "
    5519                 :             :                             "in inherent impl",
    5520                 :             :                             t->get_token_description ()));
    5521                 :             : 
    5522                 :           0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    5523                 :           0 :           return nullptr;
    5524                 :             :         }
    5525                 :             :       rust_unreachable ();
    5526                 :           0 :     default:
    5527                 :           0 :       add_error (Error (t->get_locus (),
    5528                 :             :                         "unrecognised token %qs for item in inherent impl",
    5529                 :             :                         t->get_token_description ()));
    5530                 :             : 
    5531                 :             :       // skip?
    5532                 :           0 :       return nullptr;
    5533                 :             :     }
    5534                 :        1851 : }
    5535                 :             : 
    5536                 :             : /* For internal use only by parse_inherent_impl_item() - splits giant method
    5537                 :             :  * into smaller ones and prevents duplication of logic. Strictly, this parses
    5538                 :             :  * a function or method item inside an inherent impl item block. */
    5539                 :             : // TODO: make this a templated function with "return type" as type param -
    5540                 :             : // InherentImplItem is this specialisation of the template while TraitImplItem
    5541                 :             : // will be the other.
    5542                 :             : template <typename ManagedTokenSource>
    5543                 :             : std::unique_ptr<AST::AssociatedItem>
    5544                 :        1831 : Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
    5545                 :             :   AST::Visibility vis, AST::AttrVec outer_attrs)
    5546                 :             : {
    5547                 :        1831 :   location_t locus = lexer.peek_token ()->get_locus ();
    5548                 :             :   // parse function or method qualifiers
    5549                 :        1831 :   AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
    5550                 :             : 
    5551                 :        1831 :   skip_token (FN_KW);
    5552                 :             : 
    5553                 :             :   // parse function or method name
    5554                 :        1831 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5555                 :        1831 :   if (ident_tok == nullptr)
    5556                 :           7 :     return nullptr;
    5557                 :             : 
    5558                 :        1824 :   Identifier ident{ident_tok};
    5559                 :             : 
    5560                 :             :   // parse generic params
    5561                 :        1824 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    5562                 :             :     = parse_generic_params_in_angles ();
    5563                 :             : 
    5564                 :        1824 :   if (!skip_token (LEFT_PAREN))
    5565                 :             :     {
    5566                 :             :       // skip after somewhere?
    5567                 :           0 :       return nullptr;
    5568                 :             :     }
    5569                 :             : 
    5570                 :             :   // now for function vs method disambiguation - method has opening "self"
    5571                 :             :   // param
    5572                 :        1824 :   auto initial_param = parse_self_param ();
    5573                 :             : 
    5574                 :        1824 :   if (!initial_param.has_value () && initial_param.error () != NOT_SELF)
    5575                 :           6 :     return nullptr;
    5576                 :             : 
    5577                 :             :   /* FIXME: ensure that self param doesn't accidently consume tokens for a
    5578                 :             :    * function one idea is to lookahead up to 4 tokens to see whether self is
    5579                 :             :    * one of them */
    5580                 :        1818 :   bool is_method = false;
    5581                 :        1818 :   if (initial_param.has_value ())
    5582                 :             :     {
    5583                 :        1145 :       if ((*initial_param)->is_self ())
    5584                 :             :         is_method = true;
    5585                 :             : 
    5586                 :             :       /* skip comma so function and method regular params can be parsed in
    5587                 :             :        * same way */
    5588                 :        2290 :       if (lexer.peek_token ()->get_id () == COMMA)
    5589                 :         614 :         lexer.skip_token ();
    5590                 :             :     }
    5591                 :             : 
    5592                 :             :   // parse trait function params
    5593                 :        1818 :   std::vector<std::unique_ptr<AST::Param>> function_params
    5594                 :             :     = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    5595                 :             : 
    5596                 :        1818 :   if (initial_param.has_value ())
    5597                 :        1145 :     function_params.insert (function_params.begin (),
    5598                 :        1145 :                             std::move (*initial_param));
    5599                 :             : 
    5600                 :        1818 :   if (!skip_token (RIGHT_PAREN))
    5601                 :             :     {
    5602                 :           0 :       skip_after_end_block ();
    5603                 :           0 :       return nullptr;
    5604                 :             :     }
    5605                 :             : 
    5606                 :             :   // parse return type (optional)
    5607                 :        1818 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    5608                 :             : 
    5609                 :             :   // parse where clause (optional)
    5610                 :        1818 :   AST::WhereClause where_clause = parse_where_clause ();
    5611                 :             : 
    5612                 :        1818 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    5613                 :        3636 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    5614                 :           4 :     lexer.skip_token ();
    5615                 :             :   else
    5616                 :             :     {
    5617                 :        1814 :       auto result = parse_block_expr ();
    5618                 :             : 
    5619                 :        1814 :       if (result == nullptr)
    5620                 :             :         {
    5621                 :           0 :           Error error (
    5622                 :           0 :             lexer.peek_token ()->get_locus (),
    5623                 :             :             "could not parse definition in inherent impl %s definition",
    5624                 :             :             is_method ? "method" : "function");
    5625                 :           0 :           add_error (std::move (error));
    5626                 :             : 
    5627                 :           0 :           skip_after_end_block ();
    5628                 :           0 :           return nullptr;
    5629                 :           0 :         }
    5630                 :        1814 :       body = std::move (result);
    5631                 :        1814 :     }
    5632                 :             : 
    5633                 :        1818 :   return std::unique_ptr<AST::Function> (
    5634                 :        7268 :     new AST::Function (std::move (ident), std::move (qualifiers),
    5635                 :             :                        std::move (generic_params), std::move (function_params),
    5636                 :             :                        std::move (return_type), std::move (where_clause),
    5637                 :             :                        std::move (body), std::move (vis),
    5638                 :        1818 :                        std::move (outer_attrs), locus));
    5639                 :        5473 : }
    5640                 :             : 
    5641                 :             : // Parses a single trait impl item (item inside a trait impl block).
    5642                 :             : template <typename ManagedTokenSource>
    5643                 :             : std::unique_ptr<AST::AssociatedItem>
    5644                 :        3100 : Parser<ManagedTokenSource>::parse_trait_impl_item ()
    5645                 :             : {
    5646                 :             :   // parse outer attributes (if they exist)
    5647                 :        3100 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5648                 :             : 
    5649                 :        3100 :   auto visibility = AST::Visibility::create_private ();
    5650                 :        6200 :   if (lexer.peek_token ()->get_id () == PUB)
    5651                 :           0 :     visibility = parse_visibility ();
    5652                 :             : 
    5653                 :             :   // branch on next token:
    5654                 :        3100 :   const_TokenPtr t = lexer.peek_token ();
    5655                 :        3100 :   switch (t->get_id ())
    5656                 :             :     {
    5657                 :           0 :     case SUPER:
    5658                 :             :     case SELF:
    5659                 :             :     case CRATE:
    5660                 :             :     case DOLLAR_SIGN:
    5661                 :             :       // these seem to be SimplePath tokens, so this is a macro invocation
    5662                 :             :       // semi
    5663                 :           0 :       return parse_macro_invocation_semi (std::move (outer_attrs));
    5664                 :          19 :     case IDENTIFIER:
    5665                 :          38 :       if (lexer.peek_token ()->get_str () == Values::WeakKeywords::DEFAULT)
    5666                 :           8 :         return parse_trait_impl_function_or_method (visibility,
    5667                 :           4 :                                                     std::move (outer_attrs));
    5668                 :             :       else
    5669                 :          30 :         return parse_macro_invocation_semi (std::move (outer_attrs));
    5670                 :         940 :     case TYPE:
    5671                 :        1880 :       return parse_type_alias (visibility, std::move (outer_attrs));
    5672                 :        2105 :     case EXTERN_KW:
    5673                 :             :     case UNSAFE:
    5674                 :             :     case FN_KW:
    5675                 :             :       // function or method
    5676                 :        4210 :       return parse_trait_impl_function_or_method (visibility,
    5677                 :        2105 :                                                   std::move (outer_attrs));
    5678                 :           2 :     case ASYNC:
    5679                 :           4 :       return parse_async_item (visibility, std::move (outer_attrs));
    5680                 :          34 :     case CONST:
    5681                 :             :       // lookahead to resolve production - could be function/method or const
    5682                 :             :       // item
    5683                 :          34 :       t = lexer.peek_token (1);
    5684                 :             : 
    5685                 :          34 :       switch (t->get_id ())
    5686                 :             :         {
    5687                 :          32 :         case IDENTIFIER:
    5688                 :             :         case UNDERSCORE:
    5689                 :          64 :           return parse_const_item (visibility, std::move (outer_attrs));
    5690                 :           2 :         case UNSAFE:
    5691                 :             :         case EXTERN_KW:
    5692                 :             :         case FN_KW:
    5693                 :           4 :           return parse_trait_impl_function_or_method (visibility,
    5694                 :           2 :                                                       std::move (outer_attrs));
    5695                 :           0 :         default:
    5696                 :           0 :           add_error (Error (
    5697                 :             :             t->get_locus (),
    5698                 :             :             "unexpected token %qs in some sort of const item in trait impl",
    5699                 :             :             t->get_token_description ()));
    5700                 :             : 
    5701                 :           0 :           lexer.skip_token (1); // TODO: is this right thing to do?
    5702                 :           0 :           return nullptr;
    5703                 :             :         }
    5704                 :             :       rust_unreachable ();
    5705                 :             :     default:
    5706                 :             :       break;
    5707                 :             :     }
    5708                 :           0 :   add_error (Error (t->get_locus (),
    5709                 :             :                     "unrecognised token %qs for item in trait impl",
    5710                 :             :                     t->get_token_description ()));
    5711                 :             : 
    5712                 :             :   // skip?
    5713                 :           0 :   return nullptr;
    5714                 :        3100 : }
    5715                 :             : 
    5716                 :             : /* For internal use only by parse_trait_impl_item() - splits giant method into
    5717                 :             :  * smaller ones and prevents duplication of logic. Strictly, this parses a
    5718                 :             :  * function or method item inside a trait impl item block. */
    5719                 :             : template <typename ManagedTokenSource>
    5720                 :             : std::unique_ptr<AST::AssociatedItem>
    5721                 :        2111 : Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
    5722                 :             :   AST::Visibility vis, AST::AttrVec outer_attrs)
    5723                 :             : {
    5724                 :             :   // this shares virtually all logic with
    5725                 :             :   // parse_inherent_impl_function_or_method
    5726                 :             :   // - template?
    5727                 :        2111 :   location_t locus = lexer.peek_token ()->get_locus ();
    5728                 :             : 
    5729                 :        2111 :   auto is_default = false;
    5730                 :        2111 :   auto t = lexer.peek_token ();
    5731                 :        2111 :   if (t->get_id () == IDENTIFIER
    5732                 :        2111 :       && t->get_str () == Values::WeakKeywords::DEFAULT)
    5733                 :             :     {
    5734                 :           4 :       is_default = true;
    5735                 :           4 :       lexer.skip_token ();
    5736                 :             :     }
    5737                 :             : 
    5738                 :             :   // parse function or method qualifiers
    5739                 :        2111 :   AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
    5740                 :             : 
    5741                 :        2111 :   skip_token (FN_KW);
    5742                 :             : 
    5743                 :             :   // parse function or method name
    5744                 :        2111 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5745                 :        2111 :   if (ident_tok == nullptr)
    5746                 :             :     {
    5747                 :           0 :       return nullptr;
    5748                 :             :     }
    5749                 :        2111 :   Identifier ident{ident_tok};
    5750                 :             : 
    5751                 :             :   // DEBUG:
    5752                 :        2111 :   rust_debug (
    5753                 :             :     "about to start parsing generic params in trait impl function or method");
    5754                 :             : 
    5755                 :             :   // parse generic params
    5756                 :        2111 :   std::vector<std::unique_ptr<AST::GenericParam>> generic_params
    5757                 :             :     = parse_generic_params_in_angles ();
    5758                 :             : 
    5759                 :             :   // DEBUG:
    5760                 :        2111 :   rust_debug (
    5761                 :             :     "finished parsing generic params in trait impl function or method");
    5762                 :             : 
    5763                 :        2111 :   if (!skip_token (LEFT_PAREN))
    5764                 :             :     {
    5765                 :             :       // skip after somewhere?
    5766                 :           0 :       return nullptr;
    5767                 :             :     }
    5768                 :             : 
    5769                 :             :   // now for function vs method disambiguation - method has opening "self"
    5770                 :             :   // param
    5771                 :        2111 :   auto initial_param = parse_self_param ();
    5772                 :             : 
    5773                 :        2111 :   if (!initial_param.has_value () && initial_param.error () != NOT_SELF)
    5774                 :           0 :     return nullptr;
    5775                 :             : 
    5776                 :             :   // FIXME: ensure that self param doesn't accidently consume tokens for a
    5777                 :             :   // function
    5778                 :        2111 :   bool is_method = false;
    5779                 :        2111 :   if (initial_param.has_value ())
    5780                 :             :     {
    5781                 :        1964 :       if ((*initial_param)->is_self ())
    5782                 :             :         is_method = true;
    5783                 :             : 
    5784                 :             :       // skip comma so function and method regular params can be parsed in
    5785                 :             :       // same way
    5786                 :        3928 :       if (lexer.peek_token ()->get_id () == COMMA)
    5787                 :             :         {
    5788                 :         711 :           lexer.skip_token ();
    5789                 :             :         }
    5790                 :             : 
    5791                 :             :       // DEBUG
    5792                 :        1964 :       rust_debug ("successfully parsed self param in method trait impl item");
    5793                 :             :     }
    5794                 :             : 
    5795                 :             :   // DEBUG
    5796                 :        2111 :   rust_debug (
    5797                 :             :     "started to parse function params in function or method trait impl item");
    5798                 :             : 
    5799                 :             :   // parse trait function params (only if next token isn't right paren)
    5800                 :        2111 :   std::vector<std::unique_ptr<AST::Param>> function_params;
    5801                 :        4222 :   if (lexer.peek_token ()->get_id () != RIGHT_PAREN)
    5802                 :             :     {
    5803                 :             :       function_params
    5804                 :         768 :         = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; });
    5805                 :             : 
    5806                 :         768 :       if (function_params.empty ())
    5807                 :             :         {
    5808                 :           0 :           Error error (
    5809                 :           0 :             lexer.peek_token ()->get_locus (),
    5810                 :             :             "failed to parse function params in trait impl %s definition",
    5811                 :             :             is_method ? "method" : "function");
    5812                 :           0 :           add_error (std::move (error));
    5813                 :             : 
    5814                 :           0 :           skip_after_next_block ();
    5815                 :           0 :           return nullptr;
    5816                 :           0 :         }
    5817                 :             :     }
    5818                 :             : 
    5819                 :        2111 :   if (initial_param.has_value ())
    5820                 :        1964 :     function_params.insert (function_params.begin (),
    5821                 :        1964 :                             std::move (*initial_param));
    5822                 :             : 
    5823                 :             :   // DEBUG
    5824                 :        2111 :   rust_debug ("successfully parsed function params in function or method "
    5825                 :             :               "trait impl item");
    5826                 :             : 
    5827                 :        2111 :   if (!skip_token (RIGHT_PAREN))
    5828                 :             :     {
    5829                 :           0 :       skip_after_next_block ();
    5830                 :           0 :       return nullptr;
    5831                 :             :     }
    5832                 :             : 
    5833                 :             :   // parse return type (optional)
    5834                 :        2111 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    5835                 :             : 
    5836                 :             :   // DEBUG
    5837                 :        2111 :   rust_debug (
    5838                 :             :     "successfully parsed return type in function or method trait impl item");
    5839                 :             : 
    5840                 :             :   // parse where clause (optional)
    5841                 :        2111 :   AST::WhereClause where_clause = parse_where_clause ();
    5842                 :             : 
    5843                 :             :   // DEBUG
    5844                 :        2111 :   rust_debug (
    5845                 :             :     "successfully parsed where clause in function or method trait impl item");
    5846                 :             : 
    5847                 :             :   // parse function definition (in block) - semicolon not allowed
    5848                 :        2111 :   tl::optional<std::unique_ptr<AST::BlockExpr>> body = tl::nullopt;
    5849                 :             : 
    5850                 :        4222 :   if (lexer.peek_token ()->get_id () == SEMICOLON)
    5851                 :           2 :     lexer.skip_token ();
    5852                 :             :   else
    5853                 :             :     {
    5854                 :        2109 :       auto result = parse_block_expr ();
    5855                 :        2109 :       if (result == nullptr)
    5856                 :             :         {
    5857                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    5858                 :             :                        "could not parse definition in trait impl %s definition",
    5859                 :             :                        is_method ? "method" : "function");
    5860                 :           0 :           add_error (std::move (error));
    5861                 :             : 
    5862                 :           0 :           skip_after_end_block ();
    5863                 :           0 :           return nullptr;
    5864                 :           0 :         }
    5865                 :        2109 :       body = std::move (result);
    5866                 :        2109 :     }
    5867                 :             : 
    5868                 :        2111 :   return std::unique_ptr<AST::Function> (
    5869                 :        8442 :     new AST::Function (std::move (ident), std::move (qualifiers),
    5870                 :             :                        std::move (generic_params), std::move (function_params),
    5871                 :             :                        std::move (return_type), std::move (where_clause),
    5872                 :             :                        std::move (body), std::move (vis),
    5873                 :        2111 :                        std::move (outer_attrs), locus, is_default));
    5874                 :        6333 : }
    5875                 :             : 
    5876                 :             : // Parses an extern block of declarations.
    5877                 :             : template <typename ManagedTokenSource>
    5878                 :             : std::unique_ptr<AST::ExternBlock>
    5879                 :        1147 : Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
    5880                 :             :                                                 AST::AttrVec outer_attrs)
    5881                 :             : {
    5882                 :        1147 :   location_t locus = lexer.peek_token ()->get_locus ();
    5883                 :        1147 :   skip_token (EXTERN_KW);
    5884                 :             : 
    5885                 :             :   // detect optional abi name
    5886                 :        1147 :   std::string abi;
    5887                 :        1147 :   const_TokenPtr next_tok = lexer.peek_token ();
    5888                 :        1147 :   if (next_tok->get_id () == STRING_LITERAL)
    5889                 :             :     {
    5890                 :        1147 :       lexer.skip_token ();
    5891                 :        1147 :       abi = next_tok->get_str ();
    5892                 :             :     }
    5893                 :             : 
    5894                 :        1147 :   if (!skip_token (LEFT_CURLY))
    5895                 :             :     {
    5896                 :           0 :       skip_after_end_block ();
    5897                 :           0 :       return nullptr;
    5898                 :             :     }
    5899                 :             : 
    5900                 :        1147 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    5901                 :             : 
    5902                 :             :   // parse declarations inside extern block
    5903                 :        1147 :   std::vector<std::unique_ptr<AST::ExternalItem>> extern_items;
    5904                 :             : 
    5905                 :        1147 :   const_TokenPtr t = lexer.peek_token ();
    5906                 :        2891 :   while (t->get_id () != RIGHT_CURLY)
    5907                 :             :     {
    5908                 :        1746 :       std::unique_ptr<AST::ExternalItem> extern_item = parse_external_item ();
    5909                 :             : 
    5910                 :        1746 :       if (extern_item == nullptr)
    5911                 :             :         {
    5912                 :           2 :           Error error (t->get_locus (),
    5913                 :             :                        "failed to parse external item despite not reaching "
    5914                 :             :                        "end of extern block");
    5915                 :           2 :           add_error (std::move (error));
    5916                 :             : 
    5917                 :           2 :           return nullptr;
    5918                 :           2 :         }
    5919                 :             : 
    5920                 :        1744 :       extern_items.push_back (std::move (extern_item));
    5921                 :             : 
    5922                 :        1744 :       t = lexer.peek_token ();
    5923                 :             :     }
    5924                 :             : 
    5925                 :        1145 :   if (!skip_token (RIGHT_CURLY))
    5926                 :             :     {
    5927                 :             :       // skip somewhere
    5928                 :           0 :       return nullptr;
    5929                 :             :     }
    5930                 :             : 
    5931                 :             :   extern_items.shrink_to_fit ();
    5932                 :             : 
    5933                 :             :   return std::unique_ptr<AST::ExternBlock> (
    5934                 :        1145 :     new AST::ExternBlock (std::move (abi), std::move (extern_items),
    5935                 :             :                           std::move (vis), std::move (inner_attrs),
    5936                 :        1145 :                           std::move (outer_attrs), locus));
    5937                 :        2294 : }
    5938                 :             : 
    5939                 :             : // Parses a single extern block item (static or function declaration).
    5940                 :             : template <typename ManagedTokenSource>
    5941                 :             : std::unique_ptr<AST::ExternalItem>
    5942                 :        1752 : Parser<ManagedTokenSource>::parse_external_item ()
    5943                 :             : {
    5944                 :             :   // parse optional outer attributes
    5945                 :        1752 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    5946                 :             : 
    5947                 :        1752 :   location_t locus = lexer.peek_token ()->get_locus ();
    5948                 :             : 
    5949                 :             :   // parse optional visibility
    5950                 :        1752 :   AST::Visibility vis = parse_visibility ();
    5951                 :             : 
    5952                 :        1752 :   const_TokenPtr t = lexer.peek_token ();
    5953                 :        1752 :   switch (t->get_id ())
    5954                 :             :     {
    5955                 :           4 :     case IDENTIFIER:
    5956                 :           8 :       return parse_macro_invocation_semi (outer_attrs);
    5957                 :           2 :       case STATIC_KW: {
    5958                 :             :         // parse extern static item
    5959                 :           2 :         lexer.skip_token ();
    5960                 :             : 
    5961                 :             :         // parse mut (optional)
    5962                 :           2 :         bool has_mut = false;
    5963                 :           4 :         if (lexer.peek_token ()->get_id () == MUT)
    5964                 :             :           {
    5965                 :           0 :             lexer.skip_token ();
    5966                 :           0 :             has_mut = true;
    5967                 :             :           }
    5968                 :             : 
    5969                 :             :         // parse identifier
    5970                 :           2 :         const_TokenPtr ident_tok = expect_token (IDENTIFIER);
    5971                 :           2 :         if (ident_tok == nullptr)
    5972                 :             :           {
    5973                 :           0 :             skip_after_semicolon ();
    5974                 :           0 :             return nullptr;
    5975                 :             :           }
    5976                 :           2 :         Identifier ident{ident_tok};
    5977                 :             : 
    5978                 :           2 :         if (!skip_token (COLON))
    5979                 :             :           {
    5980                 :           0 :             skip_after_semicolon ();
    5981                 :           0 :             return nullptr;
    5982                 :             :           }
    5983                 :             : 
    5984                 :             :         // parse type (required)
    5985                 :           2 :         std::unique_ptr<AST::Type> type = parse_type ();
    5986                 :           2 :         if (type == nullptr)
    5987                 :             :           {
    5988                 :           0 :             Error error (lexer.peek_token ()->get_locus (),
    5989                 :             :                          "failed to parse type in external static item");
    5990                 :           0 :             add_error (std::move (error));
    5991                 :             : 
    5992                 :           0 :             skip_after_semicolon ();
    5993                 :           0 :             return nullptr;
    5994                 :           0 :           }
    5995                 :             : 
    5996                 :           2 :         if (!skip_token (SEMICOLON))
    5997                 :             :           {
    5998                 :             :             // skip after somewhere?
    5999                 :           0 :             return nullptr;
    6000                 :             :           }
    6001                 :             : 
    6002                 :           2 :         return std::unique_ptr<AST::ExternalStaticItem> (
    6003                 :           4 :           new AST::ExternalStaticItem (std::move (ident), std::move (type),
    6004                 :             :                                        has_mut, std::move (vis),
    6005                 :           2 :                                        std::move (outer_attrs), locus));
    6006                 :           6 :       }
    6007                 :        1738 :     case FN_KW:
    6008                 :        3476 :       return parse_function (std::move (vis), std::move (outer_attrs), true);
    6009                 :             : 
    6010                 :           8 :     case TYPE:
    6011                 :           8 :       return parse_external_type_item (std::move (vis),
    6012                 :           8 :                                        std::move (outer_attrs));
    6013                 :           0 :     default:
    6014                 :             :       // error
    6015                 :           0 :       add_error (
    6016                 :           0 :         Error (t->get_locus (),
    6017                 :             :                "unrecognised token %qs in extern block item declaration",
    6018                 :             :                t->get_token_description ()));
    6019                 :             : 
    6020                 :           0 :       skip_after_semicolon ();
    6021                 :           0 :       return nullptr;
    6022                 :             :     }
    6023                 :        1752 : }
    6024                 :             : 
    6025                 :             : // Parses a statement (will further disambiguate any statement).
    6026                 :             : template <typename ManagedTokenSource>
    6027                 :             : std::unique_ptr<AST::Stmt>
    6028                 :         718 : Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
    6029                 :             : {
    6030                 :             :   // quick exit for empty statement
    6031                 :             :   // FIXME: Can we have empty statements without semicolons? Just nothing?
    6032                 :         718 :   const_TokenPtr t = lexer.peek_token ();
    6033                 :         718 :   if (t->get_id () == SEMICOLON)
    6034                 :             :     {
    6035                 :          32 :       lexer.skip_token ();
    6036                 :          32 :       return std::unique_ptr<AST::EmptyStmt> (
    6037                 :          32 :         new AST::EmptyStmt (t->get_locus ()));
    6038                 :             :     }
    6039                 :             : 
    6040                 :             :   // parse outer attributes
    6041                 :         686 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    6042                 :             : 
    6043                 :             :   // parsing this will be annoying because of the many different possibilities
    6044                 :             :   /* best may be just to copy paste in parse_item switch, and failing that try
    6045                 :             :    * to parse outer attributes, and then pass them in to either a let
    6046                 :             :    * statement or (fallback) expression statement. */
    6047                 :             :   // FIXME: think of a way to do this without such a large switch?
    6048                 :         686 :   t = lexer.peek_token ();
    6049                 :         686 :   switch (t->get_id ())
    6050                 :             :     {
    6051                 :         218 :     case LET:
    6052                 :             :       // let statement
    6053                 :         218 :       return parse_let_stmt (std::move (outer_attrs), restrictions);
    6054                 :         188 :     case PUB:
    6055                 :             :     case MOD:
    6056                 :             :     case EXTERN_KW:
    6057                 :             :     case USE:
    6058                 :             :     case FN_KW:
    6059                 :             :     case TYPE:
    6060                 :             :     case STRUCT_KW:
    6061                 :             :     case ENUM_KW:
    6062                 :             :     case CONST:
    6063                 :             :     case STATIC_KW:
    6064                 :             :     case AUTO:
    6065                 :             :     case TRAIT:
    6066                 :             :     case IMPL:
    6067                 :             :     case MACRO:
    6068                 :             :     /* TODO: implement union keyword but not really because of
    6069                 :             :      * context-dependence crappy hack way to parse a union written below to
    6070                 :             :      * separate it from the good code. */
    6071                 :             :     // case UNION:
    6072                 :             :     case UNSAFE: // maybe - unsafe traits are a thing
    6073                 :             :       /* if any of these (should be all possible VisItem prefixes), parse a
    6074                 :             :        * VisItem can't parse item because would require reparsing outer
    6075                 :             :        * attributes */
    6076                 :             :       // may also be unsafe block
    6077                 :         376 :       if (lexer.peek_token (1)->get_id () == LEFT_CURLY)
    6078                 :             :         {
    6079                 :           2 :           return parse_expr_stmt (std::move (outer_attrs), restrictions);
    6080                 :             :         }
    6081                 :             :       else
    6082                 :             :         {
    6083                 :         186 :           return parse_vis_item (std::move (outer_attrs));
    6084                 :             :         }
    6085                 :             :       break;
    6086                 :             :     // crappy hack to do union "keyword"
    6087                 :         236 :     case IDENTIFIER:
    6088                 :         236 :       if (t->get_str () == Values::WeakKeywords::UNION
    6089                 :         236 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
    6090                 :             :         {
    6091                 :           0 :           return parse_vis_item (std::move (outer_attrs));
    6092                 :             :           // or should this go straight to parsing union?
    6093                 :             :         }
    6094                 :         472 :       else if (is_macro_rules_def (t))
    6095                 :             :         {
    6096                 :             :           // macro_rules! macro item
    6097                 :           2 :           return parse_macro_rules_def (std::move (outer_attrs));
    6098                 :             :         }
    6099                 :             :       gcc_fallthrough ();
    6100                 :             :       // TODO: find out how to disable gcc "implicit fallthrough" warning
    6101                 :             :     default:
    6102                 :             :       // fallback: expression statement
    6103                 :         278 :       return parse_expr_stmt (std::move (outer_attrs), restrictions);
    6104                 :             :       break;
    6105                 :             :     }
    6106                 :         686 : }
    6107                 :             : 
    6108                 :             : // Parses a let statement.
    6109                 :             : template <typename ManagedTokenSource>
    6110                 :             : std::unique_ptr<AST::LetStmt>
    6111                 :       11848 : Parser<ManagedTokenSource>::parse_let_stmt (AST::AttrVec outer_attrs,
    6112                 :             :                                             ParseRestrictions restrictions)
    6113                 :             : {
    6114                 :       11848 :   location_t locus = lexer.peek_token ()->get_locus ();
    6115                 :       11848 :   skip_token (LET);
    6116                 :             : 
    6117                 :             :   // parse pattern (required)
    6118                 :       11848 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    6119                 :       11848 :   if (pattern == nullptr)
    6120                 :             :     {
    6121                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    6122                 :             :                    "failed to parse pattern in let statement");
    6123                 :           0 :       add_error (std::move (error));
    6124                 :             : 
    6125                 :           0 :       skip_after_semicolon ();
    6126                 :           0 :       return nullptr;
    6127                 :           0 :     }
    6128                 :             : 
    6129                 :             :   // parse type declaration (optional)
    6130                 :       11848 :   std::unique_ptr<AST::Type> type = nullptr;
    6131                 :       23696 :   if (lexer.peek_token ()->get_id () == COLON)
    6132                 :             :     {
    6133                 :             :       // must have a type declaration
    6134                 :        1994 :       lexer.skip_token ();
    6135                 :             : 
    6136                 :        1994 :       type = parse_type ();
    6137                 :        1994 :       if (type == nullptr)
    6138                 :             :         {
    6139                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    6140                 :             :                        "failed to parse type in let statement");
    6141                 :           0 :           add_error (std::move (error));
    6142                 :             : 
    6143                 :           0 :           skip_after_semicolon ();
    6144                 :           0 :           return nullptr;
    6145                 :           0 :         }
    6146                 :             :     }
    6147                 :             : 
    6148                 :             :   // parse expression to set variable to (optional)
    6149                 :       11848 :   std::unique_ptr<AST::Expr> expr = nullptr;
    6150                 :       23696 :   if (lexer.peek_token ()->get_id () == EQUAL)
    6151                 :             :     {
    6152                 :             :       // must have an expression
    6153                 :       10756 :       lexer.skip_token ();
    6154                 :             : 
    6155                 :       10756 :       expr = parse_expr ();
    6156                 :       10756 :       if (expr == nullptr)
    6157                 :             :         {
    6158                 :          16 :           Error error (lexer.peek_token ()->get_locus (),
    6159                 :             :                        "failed to parse expression in let statement");
    6160                 :          16 :           add_error (std::move (error));
    6161                 :             : 
    6162                 :          16 :           skip_after_semicolon ();
    6163                 :          16 :           return nullptr;
    6164                 :          16 :         }
    6165                 :             :     }
    6166                 :             : 
    6167                 :       11832 :   if (restrictions.consume_semi)
    6168                 :             :     {
    6169                 :             :       // `stmt` macro variables are parsed without a semicolon, but should be
    6170                 :             :       // parsed as a full statement when interpolated. This should be handled
    6171                 :             :       // by having the interpolated statement be distinguishable from normal
    6172                 :             :       // tokens, e.g. by NT tokens.
    6173                 :       11682 :       if (restrictions.allow_close_after_expr_stmt)
    6174                 :          68 :         maybe_skip_token (SEMICOLON);
    6175                 :       11614 :       else if (!skip_token (SEMICOLON))
    6176                 :           2 :         return nullptr;
    6177                 :             :     }
    6178                 :             : 
    6179                 :             :   return std::unique_ptr<AST::LetStmt> (
    6180                 :       11830 :     new AST::LetStmt (std::move (pattern), std::move (expr), std::move (type),
    6181                 :       11830 :                       std::move (outer_attrs), locus));
    6182                 :       11848 : }
    6183                 :             : 
    6184                 :             : // Parses a type path.
    6185                 :             : template <typename ManagedTokenSource>
    6186                 :             : AST::TypePath
    6187                 :       36428 : Parser<ManagedTokenSource>::parse_type_path ()
    6188                 :             : {
    6189                 :       36428 :   bool has_opening_scope_resolution = false;
    6190                 :       36428 :   location_t locus = lexer.peek_token ()->get_locus ();
    6191                 :       72856 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
    6192                 :             :     {
    6193                 :           8 :       has_opening_scope_resolution = true;
    6194                 :           8 :       lexer.skip_token ();
    6195                 :             :     }
    6196                 :             : 
    6197                 :             :   // create segment vector
    6198                 :       36428 :   std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
    6199                 :             : 
    6200                 :             :   // parse required initial segment
    6201                 :       36428 :   std::unique_ptr<AST::TypePathSegment> initial_segment
    6202                 :             :     = parse_type_path_segment ();
    6203                 :       36428 :   if (initial_segment == nullptr)
    6204                 :             :     {
    6205                 :             :       // skip after somewhere?
    6206                 :             :       // don't necessarily throw error but yeah
    6207                 :         175 :       return AST::TypePath::create_error ();
    6208                 :             :     }
    6209                 :       36253 :   segments.push_back (std::move (initial_segment));
    6210                 :             : 
    6211                 :             :   // parse optional segments (as long as scope resolution operator exists)
    6212                 :       36253 :   const_TokenPtr t = lexer.peek_token ();
    6213                 :       37264 :   while (t->get_id () == SCOPE_RESOLUTION)
    6214                 :             :     {
    6215                 :             :       // skip scope resolution operator
    6216                 :        1011 :       lexer.skip_token ();
    6217                 :             : 
    6218                 :             :       // parse the actual segment - it is an error if it doesn't exist now
    6219                 :        1011 :       std::unique_ptr<AST::TypePathSegment> segment
    6220                 :             :         = parse_type_path_segment ();
    6221                 :        1011 :       if (segment == nullptr)
    6222                 :             :         {
    6223                 :             :           // skip after somewhere?
    6224                 :           0 :           Error error (t->get_locus (), "could not parse type path segment");
    6225                 :           0 :           add_error (std::move (error));
    6226                 :             : 
    6227                 :           0 :           return AST::TypePath::create_error ();
    6228                 :           0 :         }
    6229                 :             : 
    6230                 :        1011 :       segments.push_back (std::move (segment));
    6231                 :             : 
    6232                 :        1011 :       t = lexer.peek_token ();
    6233                 :             :     }
    6234                 :             : 
    6235                 :             :   segments.shrink_to_fit ();
    6236                 :             : 
    6237                 :       72506 :   return AST::TypePath (std::move (segments), locus,
    6238                 :       36253 :                         has_opening_scope_resolution);
    6239                 :       36428 : }
    6240                 :             : 
    6241                 :             : template <typename ManagedTokenSource>
    6242                 :             : AST::GenericArg
    6243                 :        2821 : Parser<ManagedTokenSource>::parse_generic_arg ()
    6244                 :             : {
    6245                 :        2821 :   auto tok = lexer.peek_token ();
    6246                 :        2821 :   std::unique_ptr<AST::Expr> expr = nullptr;
    6247                 :             : 
    6248                 :        2821 :   switch (tok->get_id ())
    6249                 :             :     {
    6250                 :        1972 :       case IDENTIFIER: {
    6251                 :             :         // This is a bit of a weird situation: With an identifier token, we
    6252                 :             :         // could either have a valid type or a macro (FIXME: anything else?). So
    6253                 :             :         // we need one bit of lookahead to differentiate if this is really
    6254                 :        1972 :         auto next_tok = lexer.peek_token (1);
    6255                 :        1972 :         if (next_tok->get_id () == LEFT_ANGLE
    6256                 :        1968 :             || next_tok->get_id () == SCOPE_RESOLUTION
    6257                 :        3934 :             || next_tok->get_id () == EXCLAM)
    6258                 :             :           {
    6259                 :          19 :             auto type = parse_type ();
    6260                 :          19 :             if (type)
    6261                 :          19 :               return AST::GenericArg::create_type (std::move (type));
    6262                 :             :             else
    6263                 :           0 :               return AST::GenericArg::create_error ();
    6264                 :          19 :           }
    6265                 :        1953 :         else if (next_tok->get_id () == COLON)
    6266                 :             :           {
    6267                 :           2 :             lexer.skip_token (); // skip ident
    6268                 :           2 :             lexer.skip_token (); // skip colon
    6269                 :             : 
    6270                 :           2 :             auto tok = lexer.peek_token ();
    6271                 :           2 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    6272                 :             :               = parse_type_param_bounds ();
    6273                 :             : 
    6274                 :           2 :             auto type = std::unique_ptr<AST::TraitObjectType> (
    6275                 :           2 :               new AST::TraitObjectType (std::move (bounds), tok->get_locus (),
    6276                 :             :                                         false));
    6277                 :           2 :             if (type)
    6278                 :           2 :               return AST::GenericArg::create_type (std::move (type));
    6279                 :             :             else
    6280                 :             :               return AST::GenericArg::create_error ();
    6281                 :           4 :           }
    6282                 :        1951 :         lexer.skip_token ();
    6283                 :        3902 :         return AST::GenericArg::create_ambiguous (tok->get_str (),
    6284                 :        3902 :                                                   tok->get_locus ());
    6285                 :        1972 :       }
    6286                 :          26 :     case LEFT_CURLY:
    6287                 :          26 :       expr = parse_block_expr ();
    6288                 :          26 :       break;
    6289                 :          36 :     case MINUS:
    6290                 :             :     case STRING_LITERAL:
    6291                 :             :     case CHAR_LITERAL:
    6292                 :             :     case INT_LITERAL:
    6293                 :             :     case FLOAT_LITERAL:
    6294                 :             :     case TRUE_LITERAL:
    6295                 :             :     case FALSE_LITERAL:
    6296                 :          36 :       expr = parse_literal_expr ();
    6297                 :          36 :       break;
    6298                 :             :       // FIXME: Because of this, error reporting is garbage for const generic
    6299                 :             :       // parameter's default values
    6300                 :         787 :       default: {
    6301                 :         787 :         auto type = parse_type ();
    6302                 :             :         // FIXME: Find a better way to do this?
    6303                 :         787 :         if (type)
    6304                 :         785 :           return AST::GenericArg::create_type (std::move (type));
    6305                 :             :         else
    6306                 :           2 :           return AST::GenericArg::create_error ();
    6307                 :         787 :       }
    6308                 :             :     }
    6309                 :             : 
    6310                 :          62 :   if (!expr)
    6311                 :           0 :     return AST::GenericArg::create_error ();
    6312                 :             : 
    6313                 :          62 :   return AST::GenericArg::create_const (std::move (expr));
    6314                 :        2821 : }
    6315                 :             : 
    6316                 :             : // Parses the generic arguments in each path segment.
    6317                 :             : template <typename ManagedTokenSource>
    6318                 :             : AST::GenericArgs
    6319                 :        2647 : Parser<ManagedTokenSource>::parse_path_generic_args ()
    6320                 :             : {
    6321                 :        5294 :   if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
    6322                 :           8 :     lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
    6323                 :             : 
    6324                 :        2647 :   if (!skip_token (LEFT_ANGLE))
    6325                 :             :     {
    6326                 :             :       // skip after somewhere?
    6327                 :           0 :       return AST::GenericArgs::create_empty ();
    6328                 :             :     }
    6329                 :             : 
    6330                 :             :   // We need to parse all lifetimes, then parse types and const generics in
    6331                 :             :   // any order.
    6332                 :             : 
    6333                 :             :   // try to parse lifetimes first
    6334                 :        2647 :   std::vector<AST::Lifetime> lifetime_args;
    6335                 :             : 
    6336                 :        2647 :   const_TokenPtr t = lexer.peek_token ();
    6337                 :        2647 :   location_t locus = t->get_locus ();
    6338                 :        5302 :   while (!is_right_angle_tok (t->get_id ()))
    6339                 :             :     {
    6340                 :        2655 :       AST::Lifetime lifetime = parse_lifetime (false);
    6341                 :        2692 :       if (lifetime.is_error ())
    6342                 :             :         {
    6343                 :             :           // not necessarily an error
    6344                 :             :           break;
    6345                 :             :         }
    6346                 :             : 
    6347                 :          45 :       lifetime_args.push_back (std::move (lifetime));
    6348                 :             : 
    6349                 :             :       // if next token isn't comma, then it must be end of list
    6350                 :          90 :       if (lexer.peek_token ()->get_id () != COMMA)
    6351                 :             :         {
    6352                 :             :           break;
    6353                 :             :         }
    6354                 :             :       // skip comma
    6355                 :           8 :       lexer.skip_token ();
    6356                 :             : 
    6357                 :           8 :       t = lexer.peek_token ();
    6358                 :             :     }
    6359                 :             : 
    6360                 :             :   // try to parse types and const generics second
    6361                 :        2647 :   std::vector<AST::GenericArg> generic_args;
    6362                 :             : 
    6363                 :             :   // TODO: think of better control structure
    6364                 :        2647 :   t = lexer.peek_token ();
    6365                 :        8044 :   while (!is_right_angle_tok (t->get_id ()))
    6366                 :             :     {
    6367                 :             :       // FIXME: Is it fine to break if there is one binding? Can't there be
    6368                 :             :       // bindings in between types?
    6369                 :             : 
    6370                 :             :       // ensure not binding being parsed as type accidently
    6371                 :        4461 :       if (t->get_id () == IDENTIFIER
    6372                 :        4849 :           && lexer.peek_token (1)->get_id () == EQUAL)
    6373                 :             :         break;
    6374                 :             : 
    6375                 :        2787 :       auto arg = parse_generic_arg ();
    6376                 :        2787 :       if (!arg.is_error ())
    6377                 :             :         {
    6378                 :        2787 :           generic_args.emplace_back (std::move (arg));
    6379                 :             :         }
    6380                 :             : 
    6381                 :             :       // FIXME: Do we need to break if we encounter an error?
    6382                 :             : 
    6383                 :             :       // if next token isn't comma, then it must be end of list
    6384                 :        5574 :       if (lexer.peek_token ()->get_id () != COMMA)
    6385                 :             :         break;
    6386                 :             : 
    6387                 :             :       // skip comma
    6388                 :         223 :       lexer.skip_token ();
    6389                 :         223 :       t = lexer.peek_token ();
    6390                 :             :     }
    6391                 :             : 
    6392                 :             :   // try to parse bindings third
    6393                 :        2647 :   std::vector<AST::GenericArgsBinding> binding_args;
    6394                 :             : 
    6395                 :             :   // TODO: think of better control structure
    6396                 :        2647 :   t = lexer.peek_token ();
    6397                 :        2695 :   while (!is_right_angle_tok (t->get_id ()))
    6398                 :             :     {
    6399                 :          48 :       AST::GenericArgsBinding binding = parse_generic_args_binding ();
    6400                 :          48 :       if (binding.is_error ())
    6401                 :             :         {
    6402                 :             :           // not necessarily an error
    6403                 :             :           break;
    6404                 :             :         }
    6405                 :             : 
    6406                 :          48 :       binding_args.push_back (std::move (binding));
    6407                 :             : 
    6408                 :             :       // if next token isn't comma, then it must be end of list
    6409                 :          96 :       if (lexer.peek_token ()->get_id () != COMMA)
    6410                 :             :         {
    6411                 :             :           break;
    6412                 :             :         }
    6413                 :             :       // skip comma
    6414                 :           2 :       lexer.skip_token ();
    6415                 :             : 
    6416                 :           2 :       t = lexer.peek_token ();
    6417                 :             :     }
    6418                 :             : 
    6419                 :             :   // skip any trailing commas
    6420                 :        5294 :   if (lexer.peek_token ()->get_id () == COMMA)
    6421                 :           0 :     lexer.skip_token ();
    6422                 :             : 
    6423                 :        2647 :   if (!skip_generics_right_angle ())
    6424                 :           0 :     return AST::GenericArgs::create_empty ();
    6425                 :             : 
    6426                 :             :   lifetime_args.shrink_to_fit ();
    6427                 :             :   generic_args.shrink_to_fit ();
    6428                 :        2647 :   binding_args.shrink_to_fit ();
    6429                 :             : 
    6430                 :        2647 :   return AST::GenericArgs (std::move (lifetime_args), std::move (generic_args),
    6431                 :        2647 :                            std::move (binding_args), locus);
    6432                 :        5294 : }
    6433                 :             : 
    6434                 :             : // Parses a binding in a generic args path segment.
    6435                 :             : template <typename ManagedTokenSource>
    6436                 :             : AST::GenericArgsBinding
    6437                 :          48 : Parser<ManagedTokenSource>::parse_generic_args_binding ()
    6438                 :             : {
    6439                 :          48 :   const_TokenPtr ident_tok = lexer.peek_token ();
    6440                 :          48 :   if (ident_tok->get_id () != IDENTIFIER)
    6441                 :             :     {
    6442                 :             :       // allow non error-inducing use
    6443                 :             :       // skip somewhere?
    6444                 :           0 :       return AST::GenericArgsBinding::create_error ();
    6445                 :             :     }
    6446                 :          48 :   lexer.skip_token ();
    6447                 :          48 :   Identifier ident{ident_tok};
    6448                 :             : 
    6449                 :          48 :   if (!skip_token (EQUAL))
    6450                 :             :     {
    6451                 :             :       // skip after somewhere?
    6452                 :           0 :       return AST::GenericArgsBinding::create_error ();
    6453                 :             :     }
    6454                 :             : 
    6455                 :             :   // parse type (required)
    6456                 :          48 :   std::unique_ptr<AST::Type> type = parse_type ();
    6457                 :          48 :   if (type == nullptr)
    6458                 :             :     {
    6459                 :             :       // skip somewhere?
    6460                 :           0 :       return AST::GenericArgsBinding::create_error ();
    6461                 :             :     }
    6462                 :             : 
    6463                 :          96 :   return AST::GenericArgsBinding (std::move (ident), std::move (type),
    6464                 :          96 :                                   ident_tok->get_locus ());
    6465                 :          96 : }
    6466                 :             : 
    6467                 :             : /* Parses a single type path segment (not including opening scope resolution,
    6468                 :             :  * but includes any internal ones). Includes generic args or type path
    6469                 :             :  * functions too. */
    6470                 :             : template <typename ManagedTokenSource>
    6471                 :             : std::unique_ptr<AST::TypePathSegment>
    6472                 :       37865 : Parser<ManagedTokenSource>::parse_type_path_segment ()
    6473                 :             : {
    6474                 :       37865 :   location_t locus = lexer.peek_token ()->get_locus ();
    6475                 :             :   // parse ident segment part
    6476                 :       37865 :   AST::PathIdentSegment ident_segment = parse_path_ident_segment ();
    6477                 :       37865 :   if (ident_segment.is_error ())
    6478                 :             :     {
    6479                 :             :       // not necessarily an error
    6480                 :         175 :       return nullptr;
    6481                 :             :     }
    6482                 :             : 
    6483                 :             :   /* lookahead to determine if variants exist - only consume scope resolution
    6484                 :             :    * then */
    6485                 :       37690 :   bool has_separating_scope_resolution = false;
    6486                 :       37690 :   const_TokenPtr next = lexer.peek_token (1);
    6487                 :       75380 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
    6488                 :       37690 :       && (next->get_id () == LEFT_ANGLE || next->get_id () == LEFT_PAREN))
    6489                 :             :     {
    6490                 :           0 :       has_separating_scope_resolution = true;
    6491                 :           0 :       lexer.skip_token ();
    6492                 :             :     }
    6493                 :             : 
    6494                 :             :   // branch into variants on next token
    6495                 :       37690 :   const_TokenPtr t = lexer.peek_token ();
    6496                 :       37690 :   switch (t->get_id ())
    6497                 :             :     {
    6498                 :        1993 :     case LEFT_SHIFT:
    6499                 :             :       case LEFT_ANGLE: {
    6500                 :             :         // parse generic args
    6501                 :        1993 :         AST::GenericArgs generic_args = parse_path_generic_args ();
    6502                 :             : 
    6503                 :        1993 :         return std::unique_ptr<AST::TypePathSegmentGeneric> (
    6504                 :        3986 :           new AST::TypePathSegmentGeneric (std::move (ident_segment),
    6505                 :             :                                            has_separating_scope_resolution,
    6506                 :        1993 :                                            std::move (generic_args), locus));
    6507                 :        1993 :       }
    6508                 :          30 :       case LEFT_PAREN: {
    6509                 :             :         // parse type path function
    6510                 :          30 :         AST::TypePathFunction type_path_function
    6511                 :             :           = parse_type_path_function (locus);
    6512                 :             : 
    6513                 :          30 :         if (type_path_function.is_error ())
    6514                 :             :           {
    6515                 :             :             // skip after somewhere?
    6516                 :           0 :             return nullptr;
    6517                 :             :           }
    6518                 :             : 
    6519                 :          30 :         return std::unique_ptr<AST::TypePathSegmentFunction> (
    6520                 :          90 :           new AST::TypePathSegmentFunction (std::move (ident_segment),
    6521                 :             :                                             has_separating_scope_resolution,
    6522                 :             :                                             std::move (type_path_function),
    6523                 :          30 :                                             locus));
    6524                 :          30 :       }
    6525                 :       35667 :     default:
    6526                 :             :       // neither of them
    6527                 :             :       return std::unique_ptr<AST::TypePathSegment> (
    6528                 :       35667 :         new AST::TypePathSegment (std::move (ident_segment),
    6529                 :       35667 :                                   has_separating_scope_resolution, locus));
    6530                 :             :     }
    6531                 :             :   rust_unreachable ();
    6532                 :       75380 : }
    6533                 :             : 
    6534                 :             : // Parses a function call representation inside a type path.
    6535                 :             : template <typename ManagedTokenSource>
    6536                 :             : AST::TypePathFunction
    6537                 :          30 : Parser<ManagedTokenSource>::parse_type_path_function (location_t id_location)
    6538                 :             : {
    6539                 :          30 :   if (!skip_token (LEFT_PAREN))
    6540                 :             :     {
    6541                 :             :       // skip somewhere?
    6542                 :             :       return AST::TypePathFunction::create_error ();
    6543                 :             :     }
    6544                 :             : 
    6545                 :             :   // parse function inputs
    6546                 :          30 :   std::vector<std::unique_ptr<AST::Type>> inputs;
    6547                 :             : 
    6548                 :          98 :   while (lexer.peek_token ()->get_id () != RIGHT_PAREN)
    6549                 :             :     {
    6550                 :          34 :       std::unique_ptr<AST::Type> type = parse_type ();
    6551                 :          34 :       if (type == nullptr)
    6552                 :             :         {
    6553                 :             :           /* this is an error as there should've been a ')' there if there
    6554                 :             :            * wasn't a type */
    6555                 :           0 :           Error error (
    6556                 :           0 :             lexer.peek_token ()->get_locus (),
    6557                 :             :             "failed to parse type in parameters of type path function");
    6558                 :           0 :           add_error (std::move (error));
    6559                 :             : 
    6560                 :             :           // skip somewhere?
    6561                 :           0 :           return AST::TypePathFunction::create_error ();
    6562                 :           0 :         }
    6563                 :             : 
    6564                 :          34 :       inputs.push_back (std::move (type));
    6565                 :             : 
    6566                 :             :       // skip commas, including trailing commas
    6567                 :          68 :       if (lexer.peek_token ()->get_id () != COMMA)
    6568                 :             :         break;
    6569                 :             : 
    6570                 :           4 :       lexer.skip_token ();
    6571                 :             :     }
    6572                 :             : 
    6573                 :          30 :   if (!skip_token (RIGHT_PAREN))
    6574                 :             :     {
    6575                 :             :       // skip somewhere?
    6576                 :             :       return AST::TypePathFunction::create_error ();
    6577                 :             :     }
    6578                 :             : 
    6579                 :             :   // parse optional return type
    6580                 :          30 :   std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
    6581                 :             : 
    6582                 :          30 :   inputs.shrink_to_fit ();
    6583                 :          30 :   return AST::TypePathFunction (std::move (inputs), id_location,
    6584                 :          30 :                                 std::move (return_type));
    6585                 :          30 : }
    6586                 :             : 
    6587                 :             : // Parses a path inside an expression that allows generic arguments.
    6588                 :             : template <typename ManagedTokenSource>
    6589                 :             : AST::PathInExpression
    6590                 :       28024 : Parser<ManagedTokenSource>::parse_path_in_expression ()
    6591                 :             : {
    6592                 :       28024 :   location_t locus = UNKNOWN_LOCATION;
    6593                 :       28024 :   bool has_opening_scope_resolution = false;
    6594                 :       56048 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
    6595                 :             :     {
    6596                 :           0 :       has_opening_scope_resolution = true;
    6597                 :             : 
    6598                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    6599                 :             : 
    6600                 :           0 :       lexer.skip_token ();
    6601                 :             :     }
    6602                 :             : 
    6603                 :             :   // create segment vector
    6604                 :       28024 :   std::vector<AST::PathExprSegment> segments;
    6605                 :             : 
    6606                 :       28024 :   if (locus == UNKNOWN_LOCATION)
    6607                 :             :     {
    6608                 :       56048 :       locus = lexer.peek_token ()->get_locus ();
    6609                 :             :     }
    6610                 :             : 
    6611                 :             :   // parse required initial segment
    6612                 :       28024 :   AST::PathExprSegment initial_segment = parse_path_expr_segment ();
    6613                 :       28024 :   if (initial_segment.is_error ())
    6614                 :             :     {
    6615                 :             :       // skip after somewhere?
    6616                 :             :       // don't necessarily throw error but yeah
    6617                 :           4 :       return AST::PathInExpression::create_error ();
    6618                 :             :     }
    6619                 :       28020 :   segments.push_back (std::move (initial_segment));
    6620                 :             : 
    6621                 :             :   // parse optional segments (as long as scope resolution operator exists)
    6622                 :       28020 :   const_TokenPtr t = lexer.peek_token ();
    6623                 :       29973 :   while (t->get_id () == SCOPE_RESOLUTION)
    6624                 :             :     {
    6625                 :             :       // skip scope resolution operator
    6626                 :        1953 :       lexer.skip_token ();
    6627                 :             : 
    6628                 :             :       // parse the actual segment - it is an error if it doesn't exist now
    6629                 :        1953 :       AST::PathExprSegment segment = parse_path_expr_segment ();
    6630                 :        1953 :       if (segment.is_error ())
    6631                 :             :         {
    6632                 :             :           // skip after somewhere?
    6633                 :           0 :           Error error (t->get_locus (),
    6634                 :             :                        "could not parse path expression segment");
    6635                 :           0 :           add_error (std::move (error));
    6636                 :             : 
    6637                 :           0 :           return AST::PathInExpression::create_error ();
    6638                 :           0 :         }
    6639                 :             : 
    6640                 :        1953 :       segments.push_back (std::move (segment));
    6641                 :             : 
    6642                 :        1953 :       t = lexer.peek_token ();
    6643                 :             :     }
    6644                 :             : 
    6645                 :             :   segments.shrink_to_fit ();
    6646                 :             : 
    6647                 :       28020 :   return AST::PathInExpression (std::move (segments), {}, locus,
    6648                 :       28020 :                                 has_opening_scope_resolution);
    6649                 :       56044 : }
    6650                 :             : 
    6651                 :             : /* Parses a single path in expression path segment (including generic
    6652                 :             :  * arguments). */
    6653                 :             : template <typename ManagedTokenSource>
    6654                 :             : AST::PathExprSegment
    6655                 :       32641 : Parser<ManagedTokenSource>::parse_path_expr_segment ()
    6656                 :             : {
    6657                 :       32641 :   location_t locus = lexer.peek_token ()->get_locus ();
    6658                 :             :   // parse ident segment
    6659                 :       32641 :   AST::PathIdentSegment ident = parse_path_ident_segment ();
    6660                 :       32641 :   if (ident.is_error ())
    6661                 :             :     {
    6662                 :             :       // not necessarily an error?
    6663                 :           4 :       return AST::PathExprSegment::create_error ();
    6664                 :             :     }
    6665                 :             : 
    6666                 :             :   // parse generic args (and turbofish), if they exist
    6667                 :             :   /* use lookahead to determine if they actually exist (don't want to
    6668                 :             :    * accidently parse over next ident segment) */
    6669                 :       65274 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
    6670                 :       39322 :       && (lexer.peek_token (1)->get_id () == LEFT_ANGLE
    6671                 :        7269 :           || lexer.peek_token (1)->get_id () == LEFT_SHIFT))
    6672                 :             :     {
    6673                 :             :       // skip scope resolution
    6674                 :         356 :       lexer.skip_token ();
    6675                 :             : 
    6676                 :             :       // Let parse_path_generic_args split "<<" tokens
    6677                 :         356 :       AST::GenericArgs generic_args = parse_path_generic_args ();
    6678                 :             : 
    6679                 :         712 :       return AST::PathExprSegment (std::move (ident), locus,
    6680                 :         356 :                                    std::move (generic_args));
    6681                 :         356 :     }
    6682                 :             : 
    6683                 :             :   // return a generic parameter-less expr segment if not found
    6684                 :       32281 :   return AST::PathExprSegment (std::move (ident), locus);
    6685                 :       32641 : }
    6686                 :             : 
    6687                 :             : /* Parses a fully qualified path in expression (i.e. a pattern). FIXME does
    6688                 :             :  * not parse outer attrs. */
    6689                 :             : template <typename ManagedTokenSource>
    6690                 :             : AST::QualifiedPathInExpression
    6691                 :          89 : Parser<ManagedTokenSource>::parse_qualified_path_in_expression (
    6692                 :             :   location_t pratt_parsed_loc)
    6693                 :             : {
    6694                 :             :   /* Note: the Rust grammar is defined in such a way that it is impossible to
    6695                 :             :    * determine whether a prospective qualified path is a
    6696                 :             :    * QualifiedPathInExpression or QualifiedPathInType in all cases by the
    6697                 :             :    * rules themselves (the only possible difference is a TypePathSegment with
    6698                 :             :    * function, and lookahead to find this is too difficult). However, as this
    6699                 :             :    * is a pattern and QualifiedPathInType is a type, I believe it that their
    6700                 :             :    * construction will not be confused (due to rules regarding patterns vs
    6701                 :             :    * types).
    6702                 :             :    * As such, this function will not attempt to minimise errors created by
    6703                 :             :    * their confusion. */
    6704                 :             : 
    6705                 :             :   // parse the qualified path type (required)
    6706                 :          89 :   AST::QualifiedPathType qual_path_type
    6707                 :             :     = parse_qualified_path_type (pratt_parsed_loc);
    6708                 :          89 :   if (qual_path_type.is_error ())
    6709                 :             :     {
    6710                 :             :       // TODO: should this create a parse error?
    6711                 :           0 :       return AST::QualifiedPathInExpression::create_error ();
    6712                 :             :     }
    6713                 :          89 :   location_t locus = qual_path_type.get_locus ();
    6714                 :             : 
    6715                 :             :   // parse path segments
    6716                 :          89 :   std::vector<AST::PathExprSegment> segments;
    6717                 :             : 
    6718                 :             :   // parse initial required segment
    6719                 :         178 :   if (!expect_token (SCOPE_RESOLUTION))
    6720                 :             :     {
    6721                 :             :       // skip after somewhere?
    6722                 :             : 
    6723                 :           0 :       return AST::QualifiedPathInExpression::create_error ();
    6724                 :             :     }
    6725                 :          89 :   AST::PathExprSegment initial_segment = parse_path_expr_segment ();
    6726                 :          89 :   if (initial_segment.is_error ())
    6727                 :             :     {
    6728                 :             :       // skip after somewhere?
    6729                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    6730                 :             :                    "required initial path expression segment in "
    6731                 :             :                    "qualified path in expression could not be parsed");
    6732                 :           0 :       add_error (std::move (error));
    6733                 :             : 
    6734                 :           0 :       return AST::QualifiedPathInExpression::create_error ();
    6735                 :           0 :     }
    6736                 :          89 :   segments.push_back (std::move (initial_segment));
    6737                 :             : 
    6738                 :             :   // parse optional segments (as long as scope resolution operator exists)
    6739                 :          89 :   const_TokenPtr t = lexer.peek_token ();
    6740                 :          89 :   while (t->get_id () == SCOPE_RESOLUTION)
    6741                 :             :     {
    6742                 :             :       // skip scope resolution operator
    6743                 :           0 :       lexer.skip_token ();
    6744                 :             : 
    6745                 :             :       // parse the actual segment - it is an error if it doesn't exist now
    6746                 :           0 :       AST::PathExprSegment segment = parse_path_expr_segment ();
    6747                 :           0 :       if (segment.is_error ())
    6748                 :             :         {
    6749                 :             :           // skip after somewhere?
    6750                 :           0 :           Error error (t->get_locus (),
    6751                 :             :                        "could not parse path expression segment in qualified "
    6752                 :             :                        "path in expression");
    6753                 :           0 :           add_error (std::move (error));
    6754                 :             : 
    6755                 :           0 :           return AST::QualifiedPathInExpression::create_error ();
    6756                 :           0 :         }
    6757                 :             : 
    6758                 :           0 :       segments.push_back (std::move (segment));
    6759                 :             : 
    6760                 :           0 :       t = lexer.peek_token ();
    6761                 :             :     }
    6762                 :             : 
    6763                 :             :   segments.shrink_to_fit ();
    6764                 :             : 
    6765                 :             :   // FIXME: outer attr parsing
    6766                 :         178 :   return AST::QualifiedPathInExpression (std::move (qual_path_type),
    6767                 :          89 :                                          std::move (segments), {}, locus);
    6768                 :         178 : }
    6769                 :             : 
    6770                 :             : // Parses the type syntactical construction at the start of a qualified path.
    6771                 :             : template <typename ManagedTokenSource>
    6772                 :             : AST::QualifiedPathType
    6773                 :         515 : Parser<ManagedTokenSource>::parse_qualified_path_type (
    6774                 :             :   location_t pratt_parsed_loc)
    6775                 :             : {
    6776                 :         515 :   location_t locus = pratt_parsed_loc;
    6777                 :             :   /* TODO: should this actually be error? is there anywhere where this could
    6778                 :             :    * be valid? */
    6779                 :         515 :   if (locus == UNKNOWN_LOCATION)
    6780                 :             :     {
    6781                 :         426 :       locus = lexer.peek_token ()->get_locus ();
    6782                 :             : 
    6783                 :         852 :       if (lexer.peek_token ()->get_id () == LEFT_SHIFT)
    6784                 :           2 :         lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
    6785                 :             : 
    6786                 :             :       // skip after somewhere?
    6787                 :         426 :       if (!skip_token (LEFT_ANGLE))
    6788                 :           0 :         return AST::QualifiedPathType::create_error ();
    6789                 :             :     }
    6790                 :             : 
    6791                 :             :   // parse type (required)
    6792                 :         515 :   std::unique_ptr<AST::Type> type = parse_type ();
    6793                 :         515 :   if (type == nullptr)
    6794                 :             :     {
    6795                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    6796                 :             :                    "could not parse type in qualified path type");
    6797                 :           0 :       add_error (std::move (error));
    6798                 :             : 
    6799                 :             :       // skip somewhere?
    6800                 :           0 :       return AST::QualifiedPathType::create_error ();
    6801                 :           0 :     }
    6802                 :             : 
    6803                 :             :   // parse optional as clause
    6804                 :         515 :   AST::TypePath as_type_path = AST::TypePath::create_error ();
    6805                 :        1030 :   if (lexer.peek_token ()->get_id () == AS)
    6806                 :             :     {
    6807                 :         494 :       lexer.skip_token ();
    6808                 :             : 
    6809                 :             :       // parse type path, which is required now
    6810                 :         494 :       as_type_path = parse_type_path ();
    6811                 :         494 :       if (as_type_path.is_error ())
    6812                 :             :         {
    6813                 :           0 :           Error error (
    6814                 :           0 :             lexer.peek_token ()->get_locus (),
    6815                 :             :             "could not parse type path in as clause in qualified path type");
    6816                 :           0 :           add_error (std::move (error));
    6817                 :             : 
    6818                 :             :           // skip somewhere?
    6819                 :           0 :           return AST::QualifiedPathType::create_error ();
    6820                 :           0 :         }
    6821                 :             :     }
    6822                 :             : 
    6823                 :             :   /* NOTE: should actually be a right-angle token, so
    6824                 :             :    * skip_generics_right_angle shouldn't be required */
    6825                 :         515 :   if (!skip_token (RIGHT_ANGLE))
    6826                 :             :     {
    6827                 :             :       // skip after somewhere?
    6828                 :           0 :       return AST::QualifiedPathType::create_error ();
    6829                 :             :     }
    6830                 :             : 
    6831                 :         515 :   return AST::QualifiedPathType (std::move (type), locus,
    6832                 :         515 :                                  std::move (as_type_path));
    6833                 :         515 : }
    6834                 :             : 
    6835                 :             : // Parses a fully qualified path in type (i.e. a type).
    6836                 :             : template <typename ManagedTokenSource>
    6837                 :             : AST::QualifiedPathInType
    6838                 :         426 : Parser<ManagedTokenSource>::parse_qualified_path_in_type ()
    6839                 :             : {
    6840                 :         426 :   location_t locus = lexer.peek_token ()->get_locus ();
    6841                 :             :   // parse the qualified path type (required)
    6842                 :         426 :   AST::QualifiedPathType qual_path_type = parse_qualified_path_type ();
    6843                 :         426 :   if (qual_path_type.is_error ())
    6844                 :             :     {
    6845                 :             :       // TODO: should this create a parse error?
    6846                 :           0 :       return AST::QualifiedPathInType::create_error ();
    6847                 :             :     }
    6848                 :             : 
    6849                 :             :   // parse initial required segment
    6850                 :         852 :   if (!expect_token (SCOPE_RESOLUTION))
    6851                 :             :     {
    6852                 :             :       // skip after somewhere?
    6853                 :             : 
    6854                 :           0 :       return AST::QualifiedPathInType::create_error ();
    6855                 :             :     }
    6856                 :         426 :   std::unique_ptr<AST::TypePathSegment> initial_segment
    6857                 :             :     = parse_type_path_segment ();
    6858                 :         426 :   if (initial_segment == nullptr)
    6859                 :             :     {
    6860                 :             :       // skip after somewhere?
    6861                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    6862                 :             :                    "required initial type path segment in qualified path in "
    6863                 :             :                    "type could not be parsed");
    6864                 :           0 :       add_error (std::move (error));
    6865                 :             : 
    6866                 :           0 :       return AST::QualifiedPathInType::create_error ();
    6867                 :           0 :     }
    6868                 :             : 
    6869                 :             :   // parse optional segments (as long as scope resolution operator exists)
    6870                 :         426 :   std::vector<std::unique_ptr<AST::TypePathSegment>> segments;
    6871                 :         426 :   const_TokenPtr t = lexer.peek_token ();
    6872                 :         426 :   while (t->get_id () == SCOPE_RESOLUTION)
    6873                 :             :     {
    6874                 :             :       // skip scope resolution operator
    6875                 :           0 :       lexer.skip_token ();
    6876                 :             : 
    6877                 :             :       // parse the actual segment - it is an error if it doesn't exist now
    6878                 :           0 :       std::unique_ptr<AST::TypePathSegment> segment
    6879                 :             :         = parse_type_path_segment ();
    6880                 :           0 :       if (segment == nullptr)
    6881                 :             :         {
    6882                 :             :           // skip after somewhere?
    6883                 :           0 :           Error error (
    6884                 :             :             t->get_locus (),
    6885                 :             :             "could not parse type path segment in qualified path in type");
    6886                 :           0 :           add_error (std::move (error));
    6887                 :             : 
    6888                 :           0 :           return AST::QualifiedPathInType::create_error ();
    6889                 :           0 :         }
    6890                 :             : 
    6891                 :           0 :       segments.push_back (std::move (segment));
    6892                 :             : 
    6893                 :           0 :       t = lexer.peek_token ();
    6894                 :             :     }
    6895                 :             : 
    6896                 :         426 :   segments.shrink_to_fit ();
    6897                 :             : 
    6898                 :         852 :   return AST::QualifiedPathInType (std::move (qual_path_type),
    6899                 :             :                                    std::move (initial_segment),
    6900                 :         426 :                                    std::move (segments), locus);
    6901                 :         426 : }
    6902                 :             : 
    6903                 :             : // Parses a self param. Also handles self param not existing.
    6904                 :             : template <typename ManagedTokenSource>
    6905                 :             : tl::expected<std::unique_ptr<AST::Param>, ParseSelfError>
    6906                 :       13348 : Parser<ManagedTokenSource>::parse_self_param ()
    6907                 :             : {
    6908                 :       13348 :   bool has_reference = false;
    6909                 :       13348 :   AST::Lifetime lifetime = AST::Lifetime::elided ();
    6910                 :             : 
    6911                 :       13348 :   location_t locus = lexer.peek_token ()->get_locus ();
    6912                 :             : 
    6913                 :             :   // TODO: Feels off, find a better way to clearly express this
    6914                 :       53392 :   std::vector<std::vector<TokenId>> ptrs
    6915                 :             :     = {{ASTERISK, SELF} /* *self */,
    6916                 :             :        {ASTERISK, CONST, SELF} /* *const self */,
    6917                 :             :        {ASTERISK, MUT, SELF} /* *mut self */};
    6918                 :             : 
    6919                 :       53380 :   for (auto &s : ptrs)
    6920                 :             :     {
    6921                 :             :       size_t i = 0;
    6922                 :       40060 :       for (i = 0; i < s.size (); i++)
    6923                 :       80108 :         if (lexer.peek_token (i)->get_id () != s[i])
    6924                 :             :           break;
    6925                 :       40038 :       if (i == s.size ())
    6926                 :             :         {
    6927                 :           6 :           rust_error_at (lexer.peek_token ()->get_locus (),
    6928                 :             :                          "cannot pass %<self%> by raw pointer");
    6929                 :           6 :           return tl::make_unexpected (ParseSelfError::SELF_PTR);
    6930                 :             :         }
    6931                 :             :     }
    6932                 :             : 
    6933                 :             :   // Trying to find those patterns:
    6934                 :             :   //
    6935                 :             :   // &'lifetime mut self
    6936                 :             :   // &'lifetime self
    6937                 :             :   // & mut self
    6938                 :             :   // & self
    6939                 :             :   // mut self
    6940                 :             :   // self
    6941                 :             :   //
    6942                 :             :   // If not found, it is probably a function, exit and let function parsing
    6943                 :             :   // handle it.
    6944                 :             :   bool is_self = false;
    6945                 :       80052 :   for (size_t i = 0; i < 5; i++)
    6946                 :      133420 :     if (lexer.peek_token (i)->get_id () == SELF)
    6947                 :        4188 :       is_self = true;
    6948                 :             : 
    6949                 :       13342 :   if (!is_self)
    6950                 :        9158 :     return tl::make_unexpected (ParseSelfError::NOT_SELF);
    6951                 :             : 
    6952                 :             :   // test if self is a reference parameter
    6953                 :        8368 :   if (lexer.peek_token ()->get_id () == AMP)
    6954                 :             :     {
    6955                 :        2141 :       has_reference = true;
    6956                 :        2141 :       lexer.skip_token ();
    6957                 :             : 
    6958                 :             :       // now test whether it has a lifetime
    6959                 :        4282 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    6960                 :             :         {
    6961                 :           0 :           lifetime = parse_lifetime (true);
    6962                 :             : 
    6963                 :             :           // something went wrong somehow
    6964                 :        4184 :           if (lifetime.is_error ())
    6965                 :             :             {
    6966                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
    6967                 :             :                            "failed to parse lifetime in self param");
    6968                 :           0 :               add_error (std::move (error));
    6969                 :             : 
    6970                 :             :               // skip after somewhere?
    6971                 :           0 :               return tl::make_unexpected (ParseSelfError::PARSING);
    6972                 :           0 :             }
    6973                 :             :         }
    6974                 :             :     }
    6975                 :             : 
    6976                 :             :   // test for mut
    6977                 :        4184 :   bool has_mut = false;
    6978                 :        8368 :   if (lexer.peek_token ()->get_id () == MUT)
    6979                 :             :     {
    6980                 :         234 :       has_mut = true;
    6981                 :         234 :       lexer.skip_token ();
    6982                 :             :     }
    6983                 :             : 
    6984                 :             :   // skip self token
    6985                 :        4184 :   const_TokenPtr self_tok = lexer.peek_token ();
    6986                 :        4184 :   if (self_tok->get_id () != SELF)
    6987                 :             :     {
    6988                 :             :       // skip after somewhere?
    6989                 :           4 :       return tl::make_unexpected (ParseSelfError::NOT_SELF);
    6990                 :             :     }
    6991                 :        4180 :   lexer.skip_token ();
    6992                 :             : 
    6993                 :             :   // parse optional type
    6994                 :        4180 :   std::unique_ptr<AST::Type> type = nullptr;
    6995                 :        8360 :   if (lexer.peek_token ()->get_id () == COLON)
    6996                 :             :     {
    6997                 :           2 :       lexer.skip_token ();
    6998                 :             : 
    6999                 :             :       // type is now required
    7000                 :           2 :       type = parse_type ();
    7001                 :           2 :       if (type == nullptr)
    7002                 :             :         {
    7003                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    7004                 :             :                        "could not parse type in self param");
    7005                 :           0 :           add_error (std::move (error));
    7006                 :             : 
    7007                 :             :           // skip after somewhere?
    7008                 :           0 :           return tl::make_unexpected (ParseSelfError::PARSING);
    7009                 :           0 :         }
    7010                 :             :     }
    7011                 :             : 
    7012                 :             :   // ensure that cannot have both type and reference
    7013                 :        4180 :   if (type != nullptr && has_reference)
    7014                 :             :     {
    7015                 :           0 :       Error error (
    7016                 :           0 :         lexer.peek_token ()->get_locus (),
    7017                 :             :         "cannot have both a reference and a type specified in a self param");
    7018                 :           0 :       add_error (std::move (error));
    7019                 :             : 
    7020                 :             :       // skip after somewhere?
    7021                 :           0 :       return tl::make_unexpected (ParseSelfError::PARSING);
    7022                 :           0 :     }
    7023                 :             : 
    7024                 :        4180 :   if (has_reference)
    7025                 :             :     {
    7026                 :        2141 :       return std::make_unique<AST::SelfParam> (std::move (lifetime), has_mut,
    7027                 :        2141 :                                                locus);
    7028                 :             :     }
    7029                 :             :   else
    7030                 :             :     {
    7031                 :             :       // note that type may be nullptr here and that's fine
    7032                 :        2039 :       return std::make_unique<AST::SelfParam> (std::move (type), has_mut,
    7033                 :        2039 :                                                locus);
    7034                 :             :     }
    7035                 :       17528 : }
    7036                 :             : 
    7037                 :             : /* Parses an expression or macro statement. */
    7038                 :             : template <typename ManagedTokenSource>
    7039                 :             : std::unique_ptr<AST::Stmt>
    7040                 :         280 : Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
    7041                 :             :                                              ParseRestrictions restrictions)
    7042                 :             : {
    7043                 :         280 :   location_t locus = lexer.peek_token ()->get_locus ();
    7044                 :             : 
    7045                 :         280 :   std::unique_ptr<AST::Expr> expr;
    7046                 :             : 
    7047                 :         560 :   switch (lexer.peek_token ()->get_id ())
    7048                 :             :     {
    7049                 :         238 :     case IDENTIFIER:
    7050                 :             :     case CRATE:
    7051                 :             :     case SUPER:
    7052                 :             :     case SELF:
    7053                 :             :     case SELF_ALIAS:
    7054                 :             :     case DOLLAR_SIGN:
    7055                 :             :       case SCOPE_RESOLUTION: {
    7056                 :         238 :         AST::PathInExpression path = parse_path_in_expression ();
    7057                 :         238 :         std::unique_ptr<AST::Expr> null_denotation;
    7058                 :             : 
    7059                 :         476 :         if (lexer.peek_token ()->get_id () == EXCLAM)
    7060                 :             :           {
    7061                 :             :             // Bind a reference to avoid -Wredundant-move on post-P1825R0
    7062                 :             :             // compilers. Change to non-reference type and remove the moves
    7063                 :             :             // below once C++20 is required to build gcc.
    7064                 :          64 :             std::unique_ptr<AST::MacroInvocation> &&invoc
    7065                 :         128 :               = parse_macro_invocation_partial (std::move (path),
    7066                 :             :                                                 std::move (outer_attrs));
    7067                 :             : 
    7068                 :          64 :             if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
    7069                 :             :               {
    7070                 :          62 :                 invoc->add_semicolon ();
    7071                 :             :                 // Macro invocation with semicolon.
    7072                 :          62 :                 return std::move (invoc);
    7073                 :             :               }
    7074                 :             : 
    7075                 :           2 :             TokenId after_macro = lexer.peek_token ()->get_id ();
    7076                 :             : 
    7077                 :           2 :             if (restrictions.allow_close_after_expr_stmt
    7078                 :           2 :                 && (after_macro == RIGHT_PAREN || after_macro == RIGHT_CURLY
    7079                 :             :                     || after_macro == RIGHT_SQUARE))
    7080                 :           2 :               return std::move (invoc);
    7081                 :             : 
    7082                 :           0 :             if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
    7083                 :             :                   == AST::CURLY
    7084                 :           0 :                 && after_macro != DOT && after_macro != QUESTION_MARK)
    7085                 :             :               {
    7086                 :           0 :                 rust_debug ("braced macro statement");
    7087                 :           0 :                 return std::move (invoc);
    7088                 :             :               }
    7089                 :             : 
    7090                 :           0 :             null_denotation = std::move (invoc);
    7091                 :          64 :           }
    7092                 :             :         else
    7093                 :             :           {
    7094                 :             :             null_denotation
    7095                 :         174 :               = null_denotation_path (std::move (path), {}, restrictions);
    7096                 :             :           }
    7097                 :             : 
    7098                 :         174 :         expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
    7099                 :             :                                  std::move (outer_attrs), restrictions);
    7100                 :             :         break;
    7101                 :         238 :       }
    7102                 :          42 :     default:
    7103                 :          42 :       restrictions.expr_can_be_stmt = true;
    7104                 :          42 :       expr = parse_expr (std::move (outer_attrs), restrictions);
    7105                 :          42 :       break;
    7106                 :             :     }
    7107                 :             : 
    7108                 :         216 :   if (expr == nullptr)
    7109                 :             :     {
    7110                 :             :       // expr is required, error
    7111                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    7112                 :             :                    "failed to parse expr in expr statement");
    7113                 :           0 :       add_error (std::move (error));
    7114                 :             : 
    7115                 :           0 :       skip_after_semicolon ();
    7116                 :           0 :       return nullptr;
    7117                 :           0 :     }
    7118                 :             : 
    7119                 :         216 :   bool has_semi = false;
    7120                 :             : 
    7121                 :         216 :   if (restrictions.consume_semi)
    7122                 :             :     {
    7123                 :         202 :       if (maybe_skip_token (SEMICOLON))
    7124                 :             :         {
    7125                 :             :           has_semi = true;
    7126                 :             :         }
    7127                 :          54 :       else if (expr->is_expr_without_block ())
    7128                 :             :         {
    7129                 :          14 :           if (restrictions.allow_close_after_expr_stmt)
    7130                 :             :             {
    7131                 :          14 :               TokenId id = lexer.peek_token ()->get_id ();
    7132                 :          14 :               if (id != RIGHT_PAREN && id != RIGHT_CURLY && id != RIGHT_SQUARE)
    7133                 :             :                 {
    7134                 :           4 :                   expect_token (SEMICOLON);
    7135                 :           4 :                   return nullptr;
    7136                 :             :                 }
    7137                 :             :             }
    7138                 :             :           else
    7139                 :             :             {
    7140                 :           0 :               expect_token (SEMICOLON);
    7141                 :           0 :               return nullptr;
    7142                 :             :             }
    7143                 :             :         }
    7144                 :             :     }
    7145                 :             : 
    7146                 :         212 :   return std::unique_ptr<AST::ExprStmt> (
    7147                 :         212 :     new AST::ExprStmt (std::move (expr), locus, has_semi));
    7148                 :         280 : }
    7149                 :             : 
    7150                 :             : // Parses a block expression, including the curly braces at start and end.
    7151                 :             : template <typename ManagedTokenSource>
    7152                 :             : std::unique_ptr<AST::BlockExpr>
    7153                 :       15368 : Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,
    7154                 :             :                                               AST::LoopLabel label,
    7155                 :             :                                               location_t pratt_parsed_loc)
    7156                 :             : {
    7157                 :       15368 :   location_t locus = pratt_parsed_loc;
    7158                 :       15368 :   if (locus == UNKNOWN_LOCATION)
    7159                 :             :     {
    7160                 :       14524 :       locus = lexer.peek_token ()->get_locus ();
    7161                 :       14524 :       if (!skip_token (LEFT_CURLY))
    7162                 :             :         {
    7163                 :           0 :           skip_after_end_block ();
    7164                 :           0 :           return nullptr;
    7165                 :             :         }
    7166                 :             :     }
    7167                 :             : 
    7168                 :       15368 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    7169                 :             : 
    7170                 :             :   // parse statements and expression
    7171                 :       15368 :   std::vector<std::unique_ptr<AST::Stmt>> stmts;
    7172                 :       15368 :   std::unique_ptr<AST::Expr> expr = nullptr;
    7173                 :             : 
    7174                 :       15368 :   const_TokenPtr t = lexer.peek_token ();
    7175                 :       44088 :   while (t->get_id () != RIGHT_CURLY)
    7176                 :             :     {
    7177                 :       28720 :       ExprOrStmt expr_or_stmt = parse_stmt_or_expr ();
    7178                 :       28720 :       if (expr_or_stmt.is_error ())
    7179                 :             :         {
    7180                 :          30 :           Error error (
    7181                 :             :             t->get_locus (),
    7182                 :             :             "failed to parse statement or expression in block expression");
    7183                 :          30 :           add_error (std::move (error));
    7184                 :             : 
    7185                 :          30 :           return nullptr;
    7186                 :          30 :         }
    7187                 :             : 
    7188                 :       28690 :       t = lexer.peek_token ();
    7189                 :             : 
    7190                 :       28690 :       if (expr_or_stmt.stmt != nullptr)
    7191                 :             :         {
    7192                 :       19126 :           stmts.push_back (std::move (expr_or_stmt.stmt));
    7193                 :             :         }
    7194                 :             :       else
    7195                 :             :         {
    7196                 :             :           // assign to expression and end parsing inside
    7197                 :        9564 :           expr = std::move (expr_or_stmt.expr);
    7198                 :             :           break;
    7199                 :             :         }
    7200                 :             :     }
    7201                 :             : 
    7202                 :       15338 :   location_t end_locus = t->get_locus ();
    7203                 :             : 
    7204                 :       15338 :   if (!skip_token (RIGHT_CURLY))
    7205                 :             :     {
    7206                 :           0 :       Error error (t->get_locus (),
    7207                 :             :                    "error may be from having an expression (as opposed to "
    7208                 :             :                    "statement) in the body of the function but not last");
    7209                 :           0 :       add_error (std::move (error));
    7210                 :             : 
    7211                 :           0 :       skip_after_end_block ();
    7212                 :           0 :       return nullptr;
    7213                 :           0 :     }
    7214                 :             : 
    7215                 :             :   // grammar allows for empty block expressions
    7216                 :             : 
    7217                 :             :   stmts.shrink_to_fit ();
    7218                 :             : 
    7219                 :             :   return std::unique_ptr<AST::BlockExpr> (
    7220                 :       15338 :     new AST::BlockExpr (std::move (stmts), std::move (expr),
    7221                 :             :                         std::move (inner_attrs), std::move (outer_attrs),
    7222                 :       15338 :                         std::move (label), locus, end_locus));
    7223                 :       15368 : }
    7224                 :             : 
    7225                 :             : /* Parses a "grouped" expression (expression in parentheses), used to control
    7226                 :             :  * precedence. */
    7227                 :             : template <typename ManagedTokenSource>
    7228                 :             : std::unique_ptr<AST::GroupedExpr>
    7229                 :             : Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
    7230                 :             : {
    7231                 :             :   location_t locus = lexer.peek_token ()->get_locus ();
    7232                 :             :   skip_token (LEFT_PAREN);
    7233                 :             : 
    7234                 :             :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    7235                 :             : 
    7236                 :             :   // parse required expr inside parentheses
    7237                 :             :   std::unique_ptr<AST::Expr> expr_in_parens = parse_expr ();
    7238                 :             :   if (expr_in_parens == nullptr)
    7239                 :             :     {
    7240                 :             :       // skip after somewhere?
    7241                 :             :       // error?
    7242                 :             :       return nullptr;
    7243                 :             :     }
    7244                 :             : 
    7245                 :             :   if (!skip_token (RIGHT_PAREN))
    7246                 :             :     {
    7247                 :             :       // skip after somewhere?
    7248                 :             :       return nullptr;
    7249                 :             :     }
    7250                 :             : 
    7251                 :             :   return std::unique_ptr<AST::GroupedExpr> (
    7252                 :             :     new AST::GroupedExpr (std::move (expr_in_parens), std::move (inner_attrs),
    7253                 :             :                           std::move (outer_attrs), locus));
    7254                 :             : }
    7255                 :             : 
    7256                 :             : // Parses a closure expression (closure definition).
    7257                 :             : template <typename ManagedTokenSource>
    7258                 :             : std::unique_ptr<AST::ClosureExpr>
    7259                 :             : Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
    7260                 :             : {
    7261                 :             :   location_t locus = lexer.peek_token ()->get_locus ();
    7262                 :             :   // detect optional "move"
    7263                 :             :   bool has_move = false;
    7264                 :             :   if (lexer.peek_token ()->get_id () == MOVE)
    7265                 :             :     {
    7266                 :             :       lexer.skip_token ();
    7267                 :             :       has_move = true;
    7268                 :             :     }
    7269                 :             : 
    7270                 :             :   // handle parameter list
    7271                 :             :   std::vector<AST::ClosureParam> params;
    7272                 :             : 
    7273                 :             :   const_TokenPtr t = lexer.peek_token ();
    7274                 :             :   switch (t->get_id ())
    7275                 :             :     {
    7276                 :             :     case OR:
    7277                 :             :       // skip token, no parameters
    7278                 :             :       lexer.skip_token ();
    7279                 :             :       break;
    7280                 :             :     case PIPE:
    7281                 :             :       // actually may have parameters
    7282                 :             :       lexer.skip_token ();
    7283                 :             :       t = lexer.peek_token ();
    7284                 :             : 
    7285                 :             :       while (t->get_id () != PIPE)
    7286                 :             :         {
    7287                 :             :           AST::ClosureParam param = parse_closure_param ();
    7288                 :             :           if (param.is_error ())
    7289                 :             :             {
    7290                 :             :               // TODO is this really an error?
    7291                 :             :               Error error (t->get_locus (), "could not parse closure param");
    7292                 :             :               add_error (std::move (error));
    7293                 :             : 
    7294                 :             :               break;
    7295                 :             :             }
    7296                 :             :           params.push_back (std::move (param));
    7297                 :             : 
    7298                 :             :           if (lexer.peek_token ()->get_id () != COMMA)
    7299                 :             :             {
    7300                 :             :               lexer.skip_token ();
    7301                 :             :               // not an error but means param list is done
    7302                 :             :               break;
    7303                 :             :             }
    7304                 :             :           // skip comma
    7305                 :             :           lexer.skip_token ();
    7306                 :             : 
    7307                 :             :           t = lexer.peek_token ();
    7308                 :             :         }
    7309                 :             :       params.shrink_to_fit ();
    7310                 :             :       break;
    7311                 :             :     default:
    7312                 :             :       add_error (Error (t->get_locus (),
    7313                 :             :                         "unexpected token %qs in closure expression - expected "
    7314                 :             :                         "%<|%> or %<||%>",
    7315                 :             :                         t->get_token_description ()));
    7316                 :             : 
    7317                 :             :       // skip somewhere?
    7318                 :             :       return nullptr;
    7319                 :             :     }
    7320                 :             : 
    7321                 :             :   // again branch based on next token
    7322                 :             :   t = lexer.peek_token ();
    7323                 :             :   if (t->get_id () == RETURN_TYPE)
    7324                 :             :     {
    7325                 :             :       // must be return type closure with block expr
    7326                 :             : 
    7327                 :             :       // skip "return type" token
    7328                 :             :       lexer.skip_token ();
    7329                 :             : 
    7330                 :             :       // parse actual type, which is required
    7331                 :             :       std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
    7332                 :             :       if (type == nullptr)
    7333                 :             :         {
    7334                 :             :           // error
    7335                 :             :           Error error (t->get_locus (), "failed to parse type for closure");
    7336                 :             :           add_error (std::move (error));
    7337                 :             : 
    7338                 :             :           // skip somewhere?
    7339                 :             :           return nullptr;
    7340                 :             :         }
    7341                 :             : 
    7342                 :             :       // parse block expr, which is required
    7343                 :             :       std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
    7344                 :             :       if (block == nullptr)
    7345                 :             :         {
    7346                 :             :           // error
    7347                 :             :           Error error (lexer.peek_token ()->get_locus (),
    7348                 :             :                        "failed to parse block expr in closure");
    7349                 :             :           add_error (std::move (error));
    7350                 :             : 
    7351                 :             :           // skip somewhere?
    7352                 :             :           return nullptr;
    7353                 :             :         }
    7354                 :             : 
    7355                 :             :       return std::unique_ptr<AST::ClosureExprInnerTyped> (
    7356                 :             :         new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
    7357                 :             :                                         std::move (params), locus, has_move,
    7358                 :             :                                         std::move (outer_attrs)));
    7359                 :             :     }
    7360                 :             :   else
    7361                 :             :     {
    7362                 :             :       // must be expr-only closure
    7363                 :             : 
    7364                 :             :       // parse expr, which is required
    7365                 :             :       std::unique_ptr<AST::Expr> expr = parse_expr ();
    7366                 :             :       if (expr == nullptr)
    7367                 :             :         {
    7368                 :             :           Error error (t->get_locus (),
    7369                 :             :                        "failed to parse expression in closure");
    7370                 :             :           add_error (std::move (error));
    7371                 :             : 
    7372                 :             :           // skip somewhere?
    7373                 :             :           return nullptr;
    7374                 :             :         }
    7375                 :             : 
    7376                 :             :       return std::unique_ptr<AST::ClosureExprInner> (
    7377                 :             :         new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
    7378                 :             :                                    has_move, std::move (outer_attrs)));
    7379                 :             :     }
    7380                 :             : }
    7381                 :             : 
    7382                 :             : // Parses a literal token (to literal expression).
    7383                 :             : template <typename ManagedTokenSource>
    7384                 :             : std::unique_ptr<AST::LiteralExpr>
    7385                 :        6592 : Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
    7386                 :             : {
    7387                 :             :   // TODO: change if literal representation in lexer changes
    7388                 :             : 
    7389                 :        6592 :   std::string literal_value;
    7390                 :        6592 :   AST::Literal::LitType type = AST::Literal::STRING;
    7391                 :             : 
    7392                 :             :   // branch based on token
    7393                 :        6592 :   const_TokenPtr t = lexer.peek_token ();
    7394                 :        6592 :   switch (t->get_id ())
    7395                 :             :     {
    7396                 :           3 :     case CHAR_LITERAL:
    7397                 :           3 :       type = AST::Literal::CHAR;
    7398                 :           3 :       literal_value = t->get_str ();
    7399                 :           3 :       lexer.skip_token ();
    7400                 :             :       break;
    7401                 :         184 :     case STRING_LITERAL:
    7402                 :         184 :       type = AST::Literal::STRING;
    7403                 :         184 :       literal_value = t->get_str ();
    7404                 :         184 :       lexer.skip_token ();
    7405                 :             :       break;
    7406                 :           0 :     case BYTE_CHAR_LITERAL:
    7407                 :           0 :       type = AST::Literal::BYTE;
    7408                 :           0 :       literal_value = t->get_str ();
    7409                 :           0 :       lexer.skip_token ();
    7410                 :             :       break;
    7411                 :           1 :     case BYTE_STRING_LITERAL:
    7412                 :           1 :       type = AST::Literal::BYTE_STRING;
    7413                 :           1 :       literal_value = t->get_str ();
    7414                 :           1 :       lexer.skip_token ();
    7415                 :             :       break;
    7416                 :           0 :     case RAW_STRING_LITERAL:
    7417                 :           0 :       type = AST::Literal::RAW_STRING;
    7418                 :           0 :       literal_value = t->get_str ();
    7419                 :           0 :       lexer.skip_token ();
    7420                 :             :       break;
    7421                 :        6395 :     case INT_LITERAL:
    7422                 :        6395 :       type = AST::Literal::INT;
    7423                 :        6395 :       literal_value = t->get_str ();
    7424                 :        6395 :       lexer.skip_token ();
    7425                 :             :       break;
    7426                 :           1 :     case FLOAT_LITERAL:
    7427                 :           1 :       type = AST::Literal::FLOAT;
    7428                 :           1 :       literal_value = t->get_str ();
    7429                 :           1 :       lexer.skip_token ();
    7430                 :             :       break;
    7431                 :             :     // case BOOL_LITERAL
    7432                 :             :     // use true and false keywords rather than "bool literal" Rust terminology
    7433                 :           0 :     case TRUE_LITERAL:
    7434                 :           0 :       type = AST::Literal::BOOL;
    7435                 :           0 :       literal_value = Values::Keywords::TRUE_LITERAL;
    7436                 :           0 :       lexer.skip_token ();
    7437                 :             :       break;
    7438                 :           0 :     case FALSE_LITERAL:
    7439                 :           0 :       type = AST::Literal::BOOL;
    7440                 :           0 :       literal_value = Values::Keywords::FALSE_LITERAL;
    7441                 :           0 :       lexer.skip_token ();
    7442                 :             :       break;
    7443                 :           8 :     default:
    7444                 :             :       // error - cannot be a literal expr
    7445                 :           8 :       add_error (Error (t->get_locus (),
    7446                 :             :                         "unexpected token %qs when parsing literal expression",
    7447                 :             :                         t->get_token_description ()));
    7448                 :             : 
    7449                 :             :       // skip?
    7450                 :           8 :       return nullptr;
    7451                 :             :     }
    7452                 :             : 
    7453                 :             :   // create literal based on stuff in switch
    7454                 :             :   return std::unique_ptr<AST::LiteralExpr> (
    7455                 :        6773 :     new AST::LiteralExpr (std::move (literal_value), std::move (type),
    7456                 :             :                           t->get_type_hint (), std::move (outer_attrs),
    7457                 :        6584 :                           t->get_locus ()));
    7458                 :        6592 : }
    7459                 :             : 
    7460                 :             : template <typename ManagedTokenSource>
    7461                 :             : std::unique_ptr<AST::BoxExpr>
    7462                 :           4 : Parser<ManagedTokenSource>::parse_box_expr (AST::AttrVec outer_attrs,
    7463                 :             :                                             location_t pratt_parsed_loc)
    7464                 :             : {
    7465                 :           4 :   location_t locus = pratt_parsed_loc;
    7466                 :           4 :   if (locus == UNKNOWN_LOCATION)
    7467                 :             :     {
    7468                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    7469                 :           0 :       skip_token (BOX);
    7470                 :             :     }
    7471                 :             : 
    7472                 :           4 :   ParseRestrictions restrictions;
    7473                 :             :   restrictions.expr_can_be_null = false;
    7474                 :             : 
    7475                 :           4 :   std::unique_ptr<AST::Expr> expr = parse_expr (AST::AttrVec (), restrictions);
    7476                 :             : 
    7477                 :             :   return std::unique_ptr<AST::BoxExpr> (
    7478                 :           4 :     new AST::BoxExpr (std::move (expr), std::move (outer_attrs), locus));
    7479                 :           4 : }
    7480                 :             : 
    7481                 :             : // Parses a return expression (including any expression to return).
    7482                 :             : template <typename ManagedTokenSource>
    7483                 :             : std::unique_ptr<AST::ReturnExpr>
    7484                 :         500 : Parser<ManagedTokenSource>::parse_return_expr (AST::AttrVec outer_attrs,
    7485                 :             :                                                location_t pratt_parsed_loc)
    7486                 :             : {
    7487                 :         500 :   location_t locus = pratt_parsed_loc;
    7488                 :         500 :   if (locus == UNKNOWN_LOCATION)
    7489                 :             :     {
    7490                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    7491                 :           0 :       skip_token (RETURN_KW);
    7492                 :             :     }
    7493                 :             : 
    7494                 :             :   // parse expression to return, if it exists
    7495                 :         500 :   ParseRestrictions restrictions;
    7496                 :         500 :   restrictions.expr_can_be_null = true;
    7497                 :         500 :   std::unique_ptr<AST::Expr> returned_expr
    7498                 :         500 :     = parse_expr (AST::AttrVec (), restrictions);
    7499                 :             : 
    7500                 :             :   return std::unique_ptr<AST::ReturnExpr> (
    7501                 :         500 :     new AST::ReturnExpr (std::move (returned_expr), std::move (outer_attrs),
    7502                 :         500 :                          locus));
    7503                 :         500 : }
    7504                 :             : 
    7505                 :             : /* Parses a break expression (including any label to break to AND any return
    7506                 :             :  * expression). */
    7507                 :             : template <typename ManagedTokenSource>
    7508                 :             : std::unique_ptr<AST::BreakExpr>
    7509                 :          89 : Parser<ManagedTokenSource>::parse_break_expr (AST::AttrVec outer_attrs,
    7510                 :             :                                               location_t pratt_parsed_loc)
    7511                 :             : {
    7512                 :          89 :   location_t locus = pratt_parsed_loc;
    7513                 :          89 :   if (locus == UNKNOWN_LOCATION)
    7514                 :             :     {
    7515                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    7516                 :           0 :       skip_token (BREAK);
    7517                 :             :     }
    7518                 :             : 
    7519                 :             :   // parse label (lifetime) if it exists - create dummy first
    7520                 :          89 :   AST::Lifetime label = AST::Lifetime::error ();
    7521                 :         178 :   if (lexer.peek_token ()->get_id () == LIFETIME)
    7522                 :             :     {
    7523                 :          28 :       label = parse_lifetime (false);
    7524                 :             :     }
    7525                 :             : 
    7526                 :             :   // parse break return expression if it exists
    7527                 :          89 :   ParseRestrictions restrictions;
    7528                 :          89 :   restrictions.expr_can_be_null = true;
    7529                 :          89 :   std::unique_ptr<AST::Expr> return_expr
    7530                 :          89 :     = parse_expr (AST::AttrVec (), restrictions);
    7531                 :             : 
    7532                 :             :   return std::unique_ptr<AST::BreakExpr> (
    7533                 :          89 :     new AST::BreakExpr (std::move (label), std::move (return_expr),
    7534                 :          89 :                         std::move (outer_attrs), locus));
    7535                 :          89 : }
    7536                 :             : 
    7537                 :             : // Parses a continue expression (including any label to continue from).
    7538                 :             : template <typename ManagedTokenSource>
    7539                 :             : std::unique_ptr<AST::ContinueExpr>
    7540                 :          13 : Parser<ManagedTokenSource>::parse_continue_expr (AST::AttrVec outer_attrs,
    7541                 :             :                                                  location_t pratt_parsed_loc)
    7542                 :             : {
    7543                 :          13 :   location_t locus = pratt_parsed_loc;
    7544                 :          13 :   if (locus == UNKNOWN_LOCATION)
    7545                 :             :     {
    7546                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    7547                 :           0 :       skip_token (CONTINUE);
    7548                 :             :     }
    7549                 :             : 
    7550                 :             :   // parse label (lifetime) if it exists - create dummy first
    7551                 :          13 :   AST::Lifetime label = AST::Lifetime::error ();
    7552                 :          26 :   if (lexer.peek_token ()->get_id () == LIFETIME)
    7553                 :             :     {
    7554                 :           2 :       label = parse_lifetime (false);
    7555                 :             :     }
    7556                 :             : 
    7557                 :             :   return std::unique_ptr<AST::ContinueExpr> (
    7558                 :          13 :     new AST::ContinueExpr (std::move (label), std::move (outer_attrs), locus));
    7559                 :          13 : }
    7560                 :             : 
    7561                 :             : // Parses a loop label used in loop expressions.
    7562                 :             : template <typename ManagedTokenSource>
    7563                 :             : AST::LoopLabel
    7564                 :          40 : Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
    7565                 :             : {
    7566                 :             :   // parse lifetime - if doesn't exist, assume no label
    7567                 :          40 :   if (tok->get_id () != LIFETIME)
    7568                 :             :     {
    7569                 :             :       // not necessarily an error
    7570                 :           0 :       return AST::LoopLabel::error ();
    7571                 :             :     }
    7572                 :             :   /* FIXME: check for named lifetime requirement here? or check in semantic
    7573                 :             :    * analysis phase? */
    7574                 :          40 :   AST::Lifetime label = lifetime_from_token (tok);
    7575                 :             : 
    7576                 :          40 :   if (!skip_token (COLON))
    7577                 :             :     {
    7578                 :             :       // skip somewhere?
    7579                 :           0 :       return AST::LoopLabel::error ();
    7580                 :             :     }
    7581                 :             : 
    7582                 :          40 :   return AST::LoopLabel (std::move (label), tok->get_locus ());
    7583                 :          40 : }
    7584                 :             : 
    7585                 :             : /* Parses an if expression of any kind, including with else, else if, else if
    7586                 :             :  * let, and neither. Note that any outer attributes will be ignored because if
    7587                 :             :  * expressions don't support them. */
    7588                 :             : template <typename ManagedTokenSource>
    7589                 :             : std::unique_ptr<AST::IfExpr>
    7590                 :         728 : Parser<ManagedTokenSource>::parse_if_expr (AST::AttrVec outer_attrs,
    7591                 :             :                                            location_t pratt_parsed_loc)
    7592                 :             : {
    7593                 :             :   // TODO: make having outer attributes an error?
    7594                 :         728 :   location_t locus = pratt_parsed_loc;
    7595                 :         728 :   if (locus == UNKNOWN_LOCATION)
    7596                 :             :     {
    7597                 :          43 :       locus = lexer.peek_token ()->get_locus ();
    7598                 :          43 :       if (!skip_token (IF))
    7599                 :             :         {
    7600                 :           0 :           skip_after_end_block ();
    7601                 :           0 :           return nullptr;
    7602                 :             :         }
    7603                 :             :     }
    7604                 :             : 
    7605                 :             :   // detect accidental if let
    7606                 :        1456 :   if (lexer.peek_token ()->get_id () == LET)
    7607                 :             :     {
    7608                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    7609                 :             :                    "if let expression probably exists, but is being parsed "
    7610                 :             :                    "as an if expression. This may be a parser error");
    7611                 :           0 :       add_error (std::move (error));
    7612                 :             : 
    7613                 :             :       // skip somewhere?
    7614                 :           0 :       return nullptr;
    7615                 :           0 :     }
    7616                 :             : 
    7617                 :             :   /* parse required condition expr - HACK to prevent struct expr from being
    7618                 :             :    * parsed */
    7619                 :         728 :   ParseRestrictions no_struct_expr;
    7620                 :         728 :   no_struct_expr.can_be_struct_expr = false;
    7621                 :         728 :   std::unique_ptr<AST::Expr> condition = parse_expr ({}, no_struct_expr);
    7622                 :         728 :   if (condition == nullptr)
    7623                 :             :     {
    7624                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    7625                 :             :                    "failed to parse condition expression in if expression");
    7626                 :           0 :       add_error (std::move (error));
    7627                 :             : 
    7628                 :             :       // skip somewhere?
    7629                 :           0 :       return nullptr;
    7630                 :           0 :     }
    7631                 :             : 
    7632                 :             :   // parse required block expr
    7633                 :         728 :   std::unique_ptr<AST::BlockExpr> if_body = parse_block_expr ();
    7634                 :         728 :   if (if_body == nullptr)
    7635                 :             :     {
    7636                 :           2 :       Error error (lexer.peek_token ()->get_locus (),
    7637                 :             :                    "failed to parse if body block expression in if expression");
    7638                 :           2 :       add_error (std::move (error));
    7639                 :             : 
    7640                 :             :       // skip somewhere?
    7641                 :           2 :       return nullptr;
    7642                 :           2 :     }
    7643                 :             : 
    7644                 :             :   // branch to parse end or else (and then else, else if, or else if let)
    7645                 :        1452 :   if (lexer.peek_token ()->get_id () != ELSE)
    7646                 :             :     {
    7647                 :             :       // single selection - end of if expression
    7648                 :             :       return std::unique_ptr<AST::IfExpr> (
    7649                 :         345 :         new AST::IfExpr (std::move (condition), std::move (if_body),
    7650                 :         345 :                          std::move (outer_attrs), locus));
    7651                 :             :     }
    7652                 :             :   else
    7653                 :             :     {
    7654                 :             :       // double or multiple selection - branch on end, else if, or else if let
    7655                 :             : 
    7656                 :             :       // skip "else"
    7657                 :         381 :       lexer.skip_token ();
    7658                 :             : 
    7659                 :             :       // branch on whether next token is '{' or 'if'
    7660                 :         381 :       const_TokenPtr t = lexer.peek_token ();
    7661                 :         381 :       switch (t->get_id ())
    7662                 :             :         {
    7663                 :         338 :           case LEFT_CURLY: {
    7664                 :             :             // double selection - else
    7665                 :             :             // parse else block expr (required)
    7666                 :         338 :             std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
    7667                 :         338 :             if (else_body == nullptr)
    7668                 :             :               {
    7669                 :           0 :                 Error error (lexer.peek_token ()->get_locus (),
    7670                 :             :                              "failed to parse else body block expression in "
    7671                 :             :                              "if expression");
    7672                 :           0 :                 add_error (std::move (error));
    7673                 :             : 
    7674                 :             :                 // skip somewhere?
    7675                 :           0 :                 return nullptr;
    7676                 :           0 :               }
    7677                 :             : 
    7678                 :         338 :             return std::unique_ptr<AST::IfExprConseqElse> (
    7679                 :         338 :               new AST::IfExprConseqElse (std::move (condition),
    7680                 :             :                                          std::move (if_body),
    7681                 :             :                                          std::move (else_body),
    7682                 :         338 :                                          std::move (outer_attrs), locus));
    7683                 :         338 :           }
    7684                 :          43 :           case IF: {
    7685                 :             :             // multiple selection - else if or else if let
    7686                 :             :             // branch on whether next token is 'let' or not
    7687                 :          86 :             if (lexer.peek_token (1)->get_id () == LET)
    7688                 :             :               {
    7689                 :             :                 // parse if let expr (required)
    7690                 :           2 :                 std::unique_ptr<AST::IfLetExpr> if_let_expr
    7691                 :           2 :                   = parse_if_let_expr ();
    7692                 :           2 :                 if (if_let_expr == nullptr)
    7693                 :             :                   {
    7694                 :           0 :                     Error error (lexer.peek_token ()->get_locus (),
    7695                 :             :                                  "failed to parse (else) if let expression "
    7696                 :             :                                  "after if expression");
    7697                 :           0 :                     add_error (std::move (error));
    7698                 :             : 
    7699                 :             :                     // skip somewhere?
    7700                 :           0 :                     return nullptr;
    7701                 :           0 :                   }
    7702                 :             : 
    7703                 :           2 :                 return std::unique_ptr<AST::IfExprConseqElse> (
    7704                 :           2 :                   new AST::IfExprConseqElse (std::move (condition),
    7705                 :             :                                              std::move (if_body),
    7706                 :             :                                              std::move (if_let_expr),
    7707                 :           2 :                                              std::move (outer_attrs), locus));
    7708                 :           2 :               }
    7709                 :             :             else
    7710                 :             :               {
    7711                 :             :                 // parse if expr (required)
    7712                 :          41 :                 std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
    7713                 :          41 :                 if (if_expr == nullptr)
    7714                 :             :                   {
    7715                 :           0 :                     Error error (lexer.peek_token ()->get_locus (),
    7716                 :             :                                  "failed to parse (else) if expression after "
    7717                 :             :                                  "if expression");
    7718                 :           0 :                     add_error (std::move (error));
    7719                 :             : 
    7720                 :             :                     // skip somewhere?
    7721                 :           0 :                     return nullptr;
    7722                 :           0 :                   }
    7723                 :             : 
    7724                 :          41 :                 return std::unique_ptr<AST::IfExprConseqElse> (
    7725                 :          41 :                   new AST::IfExprConseqElse (std::move (condition),
    7726                 :             :                                              std::move (if_body),
    7727                 :             :                                              std::move (if_expr),
    7728                 :          41 :                                              std::move (outer_attrs), locus));
    7729                 :          41 :               }
    7730                 :             :           }
    7731                 :           0 :         default:
    7732                 :             :           // error - invalid token
    7733                 :           0 :           add_error (Error (t->get_locus (),
    7734                 :             :                             "unexpected token %qs after else in if expression",
    7735                 :             :                             t->get_token_description ()));
    7736                 :             : 
    7737                 :             :           // skip somewhere?
    7738                 :           0 :           return nullptr;
    7739                 :             :         }
    7740                 :         381 :     }
    7741                 :         728 : }
    7742                 :             : 
    7743                 :             : /* Parses an if let expression of any kind, including with else, else if, else
    7744                 :             :  * if let, and none. Note that any outer attributes will be ignored as if let
    7745                 :             :  * expressions don't support them. */
    7746                 :             : template <typename ManagedTokenSource>
    7747                 :             : std::unique_ptr<AST::IfLetExpr>
    7748                 :          38 : Parser<ManagedTokenSource>::parse_if_let_expr (AST::AttrVec outer_attrs,
    7749                 :             :                                                location_t pratt_parsed_loc)
    7750                 :             : {
    7751                 :             :   // TODO: make having outer attributes an error?
    7752                 :          38 :   location_t locus = pratt_parsed_loc;
    7753                 :          38 :   if (locus == UNKNOWN_LOCATION)
    7754                 :             :     {
    7755                 :           2 :       locus = lexer.peek_token ()->get_locus ();
    7756                 :           2 :       if (!skip_token (IF))
    7757                 :             :         {
    7758                 :           0 :           skip_after_end_block ();
    7759                 :           0 :           return nullptr;
    7760                 :             :         }
    7761                 :             :     }
    7762                 :             : 
    7763                 :             :   // detect accidental if expr parsed as if let expr
    7764                 :          76 :   if (lexer.peek_token ()->get_id () != LET)
    7765                 :             :     {
    7766                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    7767                 :             :                    "if expression probably exists, but is being parsed as an "
    7768                 :             :                    "if let expression. This may be a parser error");
    7769                 :           0 :       add_error (std::move (error));
    7770                 :             : 
    7771                 :             :       // skip somewhere?
    7772                 :           0 :       return nullptr;
    7773                 :           0 :     }
    7774                 :          38 :   lexer.skip_token ();
    7775                 :             : 
    7776                 :             :   // parse match arm patterns (which are required)
    7777                 :          38 :   std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
    7778                 :             :     = parse_match_arm_patterns (EQUAL);
    7779                 :          38 :   if (match_arm_patterns.empty ())
    7780                 :             :     {
    7781                 :           0 :       Error error (
    7782                 :           0 :         lexer.peek_token ()->get_locus (),
    7783                 :             :         "failed to parse any match arm patterns in if let expression");
    7784                 :           0 :       add_error (std::move (error));
    7785                 :             : 
    7786                 :             :       // skip somewhere?
    7787                 :           0 :       return nullptr;
    7788                 :           0 :     }
    7789                 :             : 
    7790                 :          38 :   if (!skip_token (EQUAL))
    7791                 :             :     {
    7792                 :             :       // skip somewhere?
    7793                 :           0 :       return nullptr;
    7794                 :             :     }
    7795                 :             : 
    7796                 :             :   // parse expression (required) - HACK to prevent struct expr being parsed
    7797                 :          38 :   ParseRestrictions no_struct_expr;
    7798                 :          38 :   no_struct_expr.can_be_struct_expr = false;
    7799                 :          38 :   std::unique_ptr<AST::Expr> scrutinee_expr = parse_expr ({}, no_struct_expr);
    7800                 :          38 :   if (scrutinee_expr == nullptr)
    7801                 :             :     {
    7802                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    7803                 :             :                    "failed to parse scrutinee expression in if let expression");
    7804                 :           0 :       add_error (std::move (error));
    7805                 :             : 
    7806                 :             :       // skip somewhere?
    7807                 :           0 :       return nullptr;
    7808                 :           0 :     }
    7809                 :             :   /* TODO: check for expression not being a struct expression or lazy boolean
    7810                 :             :    * expression here? or actually probably in semantic analysis. */
    7811                 :             : 
    7812                 :             :   // parse block expression (required)
    7813                 :          38 :   std::unique_ptr<AST::BlockExpr> if_let_body = parse_block_expr ();
    7814                 :          38 :   if (if_let_body == nullptr)
    7815                 :             :     {
    7816                 :           0 :       Error error (
    7817                 :           0 :         lexer.peek_token ()->get_locus (),
    7818                 :             :         "failed to parse if let body block expression in if let expression");
    7819                 :           0 :       add_error (std::move (error));
    7820                 :             : 
    7821                 :             :       // skip somewhere?
    7822                 :           0 :       return nullptr;
    7823                 :           0 :     }
    7824                 :             : 
    7825                 :             :   // branch to parse end or else (and then else, else if, or else if let)
    7826                 :          76 :   if (lexer.peek_token ()->get_id () != ELSE)
    7827                 :             :     {
    7828                 :             :       // single selection - end of if let expression
    7829                 :             :       return std::unique_ptr<AST::IfLetExpr> (
    7830                 :          22 :         new AST::IfLetExpr (std::move (match_arm_patterns),
    7831                 :             :                             std::move (scrutinee_expr), std::move (if_let_body),
    7832                 :          22 :                             std::move (outer_attrs), locus));
    7833                 :             :     }
    7834                 :             :   else
    7835                 :             :     {
    7836                 :             :       // double or multiple selection - branch on end, else if, or else if let
    7837                 :             : 
    7838                 :             :       // skip "else"
    7839                 :          16 :       lexer.skip_token ();
    7840                 :             : 
    7841                 :             :       // branch on whether next token is '{' or 'if'
    7842                 :          16 :       const_TokenPtr t = lexer.peek_token ();
    7843                 :          16 :       switch (t->get_id ())
    7844                 :             :         {
    7845                 :          14 :           case LEFT_CURLY: {
    7846                 :             :             // double selection - else
    7847                 :             :             // parse else block expr (required)
    7848                 :          14 :             std::unique_ptr<AST::BlockExpr> else_body = parse_block_expr ();
    7849                 :          14 :             if (else_body == nullptr)
    7850                 :             :               {
    7851                 :           0 :                 Error error (lexer.peek_token ()->get_locus (),
    7852                 :             :                              "failed to parse else body block expression in "
    7853                 :             :                              "if let expression");
    7854                 :           0 :                 add_error (std::move (error));
    7855                 :             : 
    7856                 :             :                 // skip somewhere?
    7857                 :           0 :                 return nullptr;
    7858                 :           0 :               }
    7859                 :             : 
    7860                 :          14 :             return std::unique_ptr<AST::IfLetExprConseqElse> (
    7861                 :          14 :               new AST::IfLetExprConseqElse (std::move (match_arm_patterns),
    7862                 :             :                                             std::move (scrutinee_expr),
    7863                 :             :                                             std::move (if_let_body),
    7864                 :             :                                             std::move (else_body),
    7865                 :          14 :                                             std::move (outer_attrs), locus));
    7866                 :          14 :           }
    7867                 :           2 :           case IF: {
    7868                 :             :             // multiple selection - else if or else if let
    7869                 :             :             // branch on whether next token is 'let' or not
    7870                 :           4 :             if (lexer.peek_token (1)->get_id () == LET)
    7871                 :             :               {
    7872                 :             :                 // parse if let expr (required)
    7873                 :           0 :                 std::unique_ptr<AST::IfLetExpr> if_let_expr
    7874                 :           0 :                   = parse_if_let_expr ();
    7875                 :           0 :                 if (if_let_expr == nullptr)
    7876                 :             :                   {
    7877                 :           0 :                     Error error (lexer.peek_token ()->get_locus (),
    7878                 :             :                                  "failed to parse (else) if let expression "
    7879                 :             :                                  "after if let expression");
    7880                 :           0 :                     add_error (std::move (error));
    7881                 :             : 
    7882                 :             :                     // skip somewhere?
    7883                 :           0 :                     return nullptr;
    7884                 :           0 :                   }
    7885                 :             : 
    7886                 :           0 :                 return std::unique_ptr<AST::IfLetExprConseqElse> (
    7887                 :           0 :                   new AST::IfLetExprConseqElse (
    7888                 :             :                     std::move (match_arm_patterns), std::move (scrutinee_expr),
    7889                 :             :                     std::move (if_let_body), std::move (if_let_expr),
    7890                 :           0 :                     std::move (outer_attrs), locus));
    7891                 :           0 :               }
    7892                 :             :             else
    7893                 :             :               {
    7894                 :             :                 // parse if expr (required)
    7895                 :           2 :                 std::unique_ptr<AST::IfExpr> if_expr = parse_if_expr ();
    7896                 :           2 :                 if (if_expr == nullptr)
    7897                 :             :                   {
    7898                 :           0 :                     Error error (lexer.peek_token ()->get_locus (),
    7899                 :             :                                  "failed to parse (else) if expression after "
    7900                 :             :                                  "if let expression");
    7901                 :           0 :                     add_error (std::move (error));
    7902                 :             : 
    7903                 :             :                     // skip somewhere?
    7904                 :           0 :                     return nullptr;
    7905                 :           0 :                   }
    7906                 :             : 
    7907                 :           2 :                 return std::unique_ptr<AST::IfLetExprConseqElse> (
    7908                 :           2 :                   new AST::IfLetExprConseqElse (
    7909                 :             :                     std::move (match_arm_patterns), std::move (scrutinee_expr),
    7910                 :             :                     std::move (if_let_body), std::move (if_expr),
    7911                 :           2 :                     std::move (outer_attrs), locus));
    7912                 :           2 :               }
    7913                 :             :           }
    7914                 :           0 :         default:
    7915                 :             :           // error - invalid token
    7916                 :           0 :           add_error (
    7917                 :           0 :             Error (t->get_locus (),
    7918                 :             :                    "unexpected token %qs after else in if let expression",
    7919                 :             :                    t->get_token_description ()));
    7920                 :             : 
    7921                 :             :           // skip somewhere?
    7922                 :           0 :           return nullptr;
    7923                 :             :         }
    7924                 :          16 :     }
    7925                 :          38 : }
    7926                 :             : 
    7927                 :             : /* TODO: possibly decide on different method of handling label (i.e. not
    7928                 :             :  * parameter) */
    7929                 :             : 
    7930                 :             : /* Parses a "loop" infinite loop expression. Label is not parsed and should be
    7931                 :             :  * parsed via parse_labelled_loop_expr, which would call this. */
    7932                 :             : template <typename ManagedTokenSource>
    7933                 :             : std::unique_ptr<AST::LoopExpr>
    7934                 :         112 : Parser<ManagedTokenSource>::parse_loop_expr (AST::AttrVec outer_attrs,
    7935                 :             :                                              AST::LoopLabel label,
    7936                 :             :                                              location_t pratt_parsed_loc)
    7937                 :             : {
    7938                 :         112 :   location_t locus = pratt_parsed_loc;
    7939                 :         112 :   if (locus == UNKNOWN_LOCATION)
    7940                 :             :     {
    7941                 :          36 :       if (label.is_error ())
    7942                 :           0 :         locus = lexer.peek_token ()->get_locus ();
    7943                 :             :       else
    7944                 :          36 :         locus = label.get_locus ();
    7945                 :             : 
    7946                 :          36 :       if (!skip_token (LOOP))
    7947                 :             :         {
    7948                 :           0 :           skip_after_end_block ();
    7949                 :           0 :           return nullptr;
    7950                 :             :         }
    7951                 :             :     }
    7952                 :             :   else
    7953                 :             :     {
    7954                 :           0 :       if (!label.is_error ())
    7955                 :           0 :         locus = label.get_locus ();
    7956                 :             :     }
    7957                 :             : 
    7958                 :             :   // parse loop body, which is required
    7959                 :         112 :   std::unique_ptr<AST::BlockExpr> loop_body = parse_block_expr ();
    7960                 :         112 :   if (loop_body == nullptr)
    7961                 :             :     {
    7962                 :           2 :       Error error (lexer.peek_token ()->get_locus (),
    7963                 :             :                    "could not parse loop body in (infinite) loop expression");
    7964                 :           2 :       add_error (std::move (error));
    7965                 :             : 
    7966                 :           2 :       return nullptr;
    7967                 :           2 :     }
    7968                 :             : 
    7969                 :             :   return std::unique_ptr<AST::LoopExpr> (
    7970                 :         110 :     new AST::LoopExpr (std::move (loop_body), locus, std::move (label),
    7971                 :         110 :                        std::move (outer_attrs)));
    7972                 :         112 : }
    7973                 :             : 
    7974                 :             : /* Parses a "while" loop expression. Label is not parsed and should be parsed
    7975                 :             :  * via parse_labelled_loop_expr, which would call this. */
    7976                 :             : template <typename ManagedTokenSource>
    7977                 :             : std::unique_ptr<AST::WhileLoopExpr>
    7978                 :          50 : Parser<ManagedTokenSource>::parse_while_loop_expr (AST::AttrVec outer_attrs,
    7979                 :             :                                                    AST::LoopLabel label,
    7980                 :             :                                                    location_t pratt_parsed_loc)
    7981                 :             : {
    7982                 :          50 :   location_t locus = pratt_parsed_loc;
    7983                 :          50 :   if (locus == UNKNOWN_LOCATION)
    7984                 :             :     {
    7985                 :           4 :       if (label.is_error ())
    7986                 :           0 :         locus = lexer.peek_token ()->get_locus ();
    7987                 :             :       else
    7988                 :           4 :         locus = label.get_locus ();
    7989                 :             : 
    7990                 :           4 :       if (!skip_token (WHILE))
    7991                 :             :         {
    7992                 :           0 :           skip_after_end_block ();
    7993                 :           0 :           return nullptr;
    7994                 :             :         }
    7995                 :             :     }
    7996                 :             :   else
    7997                 :             :     {
    7998                 :           0 :       if (!label.is_error ())
    7999                 :           0 :         locus = label.get_locus ();
    8000                 :             :     }
    8001                 :             : 
    8002                 :             :   // ensure it isn't a while let loop
    8003                 :         100 :   if (lexer.peek_token ()->get_id () == LET)
    8004                 :             :     {
    8005                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8006                 :             :                    "appears to be while let loop but is being parsed by "
    8007                 :             :                    "while loop - this may be a compiler issue");
    8008                 :           0 :       add_error (std::move (error));
    8009                 :             : 
    8010                 :             :       // skip somewhere?
    8011                 :           0 :       return nullptr;
    8012                 :           0 :     }
    8013                 :             : 
    8014                 :             :   // parse loop predicate (required) with HACK to prevent struct expr parsing
    8015                 :          50 :   ParseRestrictions no_struct_expr;
    8016                 :          50 :   no_struct_expr.can_be_struct_expr = false;
    8017                 :          50 :   std::unique_ptr<AST::Expr> predicate = parse_expr ({}, no_struct_expr);
    8018                 :          50 :   if (predicate == nullptr)
    8019                 :             :     {
    8020                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8021                 :             :                    "failed to parse predicate expression in while loop");
    8022                 :           0 :       add_error (std::move (error));
    8023                 :             : 
    8024                 :             :       // skip somewhere?
    8025                 :           0 :       return nullptr;
    8026                 :           0 :     }
    8027                 :             :   /* TODO: check that it isn't struct expression here? actually, probably in
    8028                 :             :    * semantic analysis */
    8029                 :             : 
    8030                 :             :   // parse loop body (required)
    8031                 :          50 :   std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
    8032                 :          50 :   if (body == nullptr)
    8033                 :             :     {
    8034                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8035                 :             :                    "failed to parse loop body block expression in while loop");
    8036                 :           0 :       add_error (std::move (error));
    8037                 :             : 
    8038                 :             :       // skip somewhere
    8039                 :           0 :       return nullptr;
    8040                 :           0 :     }
    8041                 :             : 
    8042                 :             :   return std::unique_ptr<AST::WhileLoopExpr> (
    8043                 :          50 :     new AST::WhileLoopExpr (std::move (predicate), std::move (body), locus,
    8044                 :          50 :                             std::move (label), std::move (outer_attrs)));
    8045                 :          50 : }
    8046                 :             : 
    8047                 :             : /* Parses a "while let" loop expression. Label is not parsed and should be
    8048                 :             :  * parsed via parse_labelled_loop_expr, which would call this. */
    8049                 :             : template <typename ManagedTokenSource>
    8050                 :             : std::unique_ptr<AST::WhileLetLoopExpr>
    8051                 :           2 : Parser<ManagedTokenSource>::parse_while_let_loop_expr (AST::AttrVec outer_attrs,
    8052                 :             :                                                        AST::LoopLabel label)
    8053                 :             : {
    8054                 :           2 :   location_t locus = UNKNOWN_LOCATION;
    8055                 :           0 :   if (label.is_error ())
    8056                 :           4 :     locus = lexer.peek_token ()->get_locus ();
    8057                 :             :   else
    8058                 :           0 :     locus = label.get_locus ();
    8059                 :           2 :   maybe_skip_token (WHILE);
    8060                 :             : 
    8061                 :             :   /* check for possible accidental recognition of a while loop as a while let
    8062                 :             :    * loop */
    8063                 :           4 :   if (lexer.peek_token ()->get_id () != LET)
    8064                 :             :     {
    8065                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8066                 :             :                    "appears to be a while loop but is being parsed by "
    8067                 :             :                    "while let loop - this may be a compiler issue");
    8068                 :           0 :       add_error (std::move (error));
    8069                 :             : 
    8070                 :             :       // skip somewhere
    8071                 :           0 :       return nullptr;
    8072                 :           0 :     }
    8073                 :             :   // as this token is definitely let now, save the computation of comparison
    8074                 :           2 :   lexer.skip_token ();
    8075                 :             : 
    8076                 :             :   // parse predicate patterns
    8077                 :           2 :   std::vector<std::unique_ptr<AST::Pattern>> predicate_patterns
    8078                 :             :     = parse_match_arm_patterns (EQUAL);
    8079                 :             :   // TODO: have to ensure that there is at least 1 pattern?
    8080                 :             : 
    8081                 :           2 :   if (!skip_token (EQUAL))
    8082                 :             :     {
    8083                 :             :       // skip somewhere?
    8084                 :           0 :       return nullptr;
    8085                 :             :     }
    8086                 :             : 
    8087                 :             :   /* parse predicate expression, which is required (and HACK to prevent struct
    8088                 :             :    * expr) */
    8089                 :           2 :   ParseRestrictions no_struct_expr;
    8090                 :           2 :   no_struct_expr.can_be_struct_expr = false;
    8091                 :           2 :   std::unique_ptr<AST::Expr> predicate_expr = parse_expr ({}, no_struct_expr);
    8092                 :           2 :   if (predicate_expr == nullptr)
    8093                 :             :     {
    8094                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8095                 :             :                    "failed to parse predicate expression in while let loop");
    8096                 :           0 :       add_error (std::move (error));
    8097                 :             : 
    8098                 :             :       // skip somewhere?
    8099                 :           0 :       return nullptr;
    8100                 :           0 :     }
    8101                 :             :   /* TODO: ensure that struct expression is not parsed? Actually, probably in
    8102                 :             :    * semantic analysis. */
    8103                 :             : 
    8104                 :             :   // parse loop body, which is required
    8105                 :           2 :   std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
    8106                 :           2 :   if (body == nullptr)
    8107                 :             :     {
    8108                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8109                 :             :                    "failed to parse block expr (loop body) of while let loop");
    8110                 :           0 :       add_error (std::move (error));
    8111                 :             : 
    8112                 :             :       // skip somewhere?
    8113                 :           0 :       return nullptr;
    8114                 :           0 :     }
    8115                 :             : 
    8116                 :           2 :   return std::unique_ptr<AST::WhileLetLoopExpr> (new AST::WhileLetLoopExpr (
    8117                 :             :     std::move (predicate_patterns), std::move (predicate_expr),
    8118                 :           2 :     std::move (body), locus, std::move (label), std::move (outer_attrs)));
    8119                 :           2 : }
    8120                 :             : 
    8121                 :             : /* Parses a "for" iterative loop. Label is not parsed and should be parsed via
    8122                 :             :  * parse_labelled_loop_expr, which would call this. */
    8123                 :             : template <typename ManagedTokenSource>
    8124                 :             : std::unique_ptr<AST::ForLoopExpr>
    8125                 :           6 : Parser<ManagedTokenSource>::parse_for_loop_expr (AST::AttrVec outer_attrs,
    8126                 :             :                                                  AST::LoopLabel label)
    8127                 :             : {
    8128                 :           6 :   location_t locus = UNKNOWN_LOCATION;
    8129                 :           0 :   if (label.is_error ())
    8130                 :          12 :     locus = lexer.peek_token ()->get_locus ();
    8131                 :             :   else
    8132                 :           0 :     locus = label.get_locus ();
    8133                 :           6 :   maybe_skip_token (FOR);
    8134                 :             : 
    8135                 :             :   // parse pattern, which is required
    8136                 :           6 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    8137                 :           6 :   if (pattern == nullptr)
    8138                 :             :     {
    8139                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8140                 :             :                    "failed to parse iterator pattern in for loop");
    8141                 :           0 :       add_error (std::move (error));
    8142                 :             : 
    8143                 :             :       // skip somewhere?
    8144                 :           0 :       return nullptr;
    8145                 :           0 :     }
    8146                 :             : 
    8147                 :           6 :   if (!skip_token (IN))
    8148                 :             :     {
    8149                 :             :       // skip somewhere?
    8150                 :           0 :       return nullptr;
    8151                 :             :     }
    8152                 :             : 
    8153                 :             :   /* parse iterator expression, which is required - also HACK to prevent
    8154                 :             :    * struct expr */
    8155                 :           6 :   ParseRestrictions no_struct_expr;
    8156                 :           6 :   no_struct_expr.can_be_struct_expr = false;
    8157                 :           6 :   std::unique_ptr<AST::Expr> expr = parse_expr ({}, no_struct_expr);
    8158                 :           6 :   if (expr == nullptr)
    8159                 :             :     {
    8160                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8161                 :             :                    "failed to parse iterator expression in for loop");
    8162                 :           0 :       add_error (std::move (error));
    8163                 :             : 
    8164                 :             :       // skip somewhere?
    8165                 :           0 :       return nullptr;
    8166                 :           0 :     }
    8167                 :             :   // TODO: check to ensure this isn't struct expr? Or in semantic analysis.
    8168                 :             : 
    8169                 :             :   // parse loop body, which is required
    8170                 :           6 :   std::unique_ptr<AST::BlockExpr> body = parse_block_expr ();
    8171                 :           6 :   if (body == nullptr)
    8172                 :             :     {
    8173                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8174                 :             :                    "failed to parse loop body block expression in for loop");
    8175                 :           0 :       add_error (std::move (error));
    8176                 :             : 
    8177                 :             :       // skip somewhere?
    8178                 :           0 :       return nullptr;
    8179                 :           0 :     }
    8180                 :             : 
    8181                 :             :   return std::unique_ptr<AST::ForLoopExpr> (
    8182                 :           6 :     new AST::ForLoopExpr (std::move (pattern), std::move (expr),
    8183                 :             :                           std::move (body), locus, std::move (label),
    8184                 :           6 :                           std::move (outer_attrs)));
    8185                 :           6 : }
    8186                 :             : 
    8187                 :             : // Parses a loop expression with label (any kind of loop - disambiguates).
    8188                 :             : template <typename ManagedTokenSource>
    8189                 :             : std::unique_ptr<AST::Expr>
    8190                 :          40 : Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
    8191                 :             :                                                       AST::AttrVec outer_attrs)
    8192                 :             : {
    8193                 :             :   /* TODO: decide whether it should not work if there is no label, or parse it
    8194                 :             :    * with no label at the moment, I will make it not work with no label
    8195                 :             :    * because that's the implication. */
    8196                 :             : 
    8197                 :          40 :   if (tok->get_id () != LIFETIME)
    8198                 :             :     {
    8199                 :           0 :       Error error (tok->get_locus (),
    8200                 :             :                    "expected lifetime in labelled loop expr (to parse loop "
    8201                 :             :                    "label) - found %qs",
    8202                 :             :                    tok->get_token_description ());
    8203                 :           0 :       add_error (std::move (error));
    8204                 :             : 
    8205                 :             :       // skip?
    8206                 :           0 :       return nullptr;
    8207                 :           0 :     }
    8208                 :             : 
    8209                 :             :   // parse loop label (required)
    8210                 :          80 :   AST::LoopLabel label = parse_loop_label (tok);
    8211                 :          40 :   if (label.is_error ())
    8212                 :             :     {
    8213                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8214                 :             :                    "failed to parse loop label in labelled loop expr");
    8215                 :           0 :       add_error (std::move (error));
    8216                 :             : 
    8217                 :             :       // skip?
    8218                 :           0 :       return nullptr;
    8219                 :           0 :     }
    8220                 :             : 
    8221                 :             :   // branch on next token
    8222                 :          40 :   const_TokenPtr t = lexer.peek_token ();
    8223                 :          40 :   switch (t->get_id ())
    8224                 :             :     {
    8225                 :          36 :     case LOOP:
    8226                 :          36 :       return parse_loop_expr (std::move (outer_attrs), std::move (label));
    8227                 :           0 :     case FOR:
    8228                 :           0 :       return parse_for_loop_expr (std::move (outer_attrs), std::move (label));
    8229                 :           4 :     case WHILE:
    8230                 :             :       // further disambiguate into while vs while let
    8231                 :           8 :       if (lexer.peek_token (1)->get_id () == LET)
    8232                 :             :         {
    8233                 :           0 :           return parse_while_let_loop_expr (std::move (outer_attrs),
    8234                 :           0 :                                             std::move (label));
    8235                 :             :         }
    8236                 :             :       else
    8237                 :             :         {
    8238                 :           4 :           return parse_while_loop_expr (std::move (outer_attrs),
    8239                 :           4 :                                         std::move (label));
    8240                 :             :         }
    8241                 :           0 :     case LEFT_CURLY:
    8242                 :           0 :       return parse_block_expr (std::move (outer_attrs), std::move (label));
    8243                 :           0 :     default:
    8244                 :             :       // error
    8245                 :           0 :       add_error (Error (t->get_locus (),
    8246                 :             :                         "unexpected token %qs when parsing labelled loop",
    8247                 :             :                         t->get_token_description ()));
    8248                 :             : 
    8249                 :             :       // skip?
    8250                 :           0 :       return nullptr;
    8251                 :             :     }
    8252                 :          40 : }
    8253                 :             : 
    8254                 :             : // Parses a match expression.
    8255                 :             : template <typename ManagedTokenSource>
    8256                 :             : std::unique_ptr<AST::MatchExpr>
    8257                 :         362 : Parser<ManagedTokenSource>::parse_match_expr (AST::AttrVec outer_attrs,
    8258                 :             :                                               location_t pratt_parsed_loc)
    8259                 :             : {
    8260                 :         362 :   location_t locus = pratt_parsed_loc;
    8261                 :         362 :   if (locus == UNKNOWN_LOCATION)
    8262                 :             :     {
    8263                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    8264                 :           0 :       skip_token (MATCH_KW);
    8265                 :             :     }
    8266                 :             : 
    8267                 :             :   /* parse scrutinee expression, which is required (and HACK to prevent struct
    8268                 :             :    * expr) */
    8269                 :         362 :   ParseRestrictions no_struct_expr;
    8270                 :         362 :   no_struct_expr.can_be_struct_expr = false;
    8271                 :         362 :   std::unique_ptr<AST::Expr> scrutinee = parse_expr ({}, no_struct_expr);
    8272                 :         362 :   if (scrutinee == nullptr)
    8273                 :             :     {
    8274                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8275                 :             :                    "failed to parse scrutinee expression in match expression");
    8276                 :           0 :       add_error (std::move (error));
    8277                 :             : 
    8278                 :             :       // skip somewhere?
    8279                 :           0 :       return nullptr;
    8280                 :           0 :     }
    8281                 :             :   /* TODO: check for scrutinee expr not being struct expr? or do so in
    8282                 :             :    * semantic analysis */
    8283                 :             : 
    8284                 :         362 :   if (!skip_token (LEFT_CURLY))
    8285                 :             :     {
    8286                 :             :       // skip somewhere?
    8287                 :           0 :       return nullptr;
    8288                 :             :     }
    8289                 :             : 
    8290                 :             :   // parse inner attributes (if they exist)
    8291                 :         362 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    8292                 :             : 
    8293                 :             :   // parse match arms (if they exist)
    8294                 :             :   // std::vector<std::unique_ptr<AST::MatchCase> > match_arms;
    8295                 :         362 :   std::vector<AST::MatchCase> match_arms;
    8296                 :             : 
    8297                 :             :   // parse match cases
    8298                 :        3215 :   while (lexer.peek_token ()->get_id () != RIGHT_CURLY)
    8299                 :             :     {
    8300                 :             :       // parse match arm itself, which is required
    8301                 :         841 :       AST::MatchArm arm = parse_match_arm ();
    8302                 :         841 :       if (arm.is_error ())
    8303                 :             :         {
    8304                 :             :           // TODO is this worth throwing everything away?
    8305                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8306                 :             :                        "failed to parse match arm in match arms");
    8307                 :           0 :           add_error (std::move (error));
    8308                 :             : 
    8309                 :           0 :           return nullptr;
    8310                 :           0 :         }
    8311                 :             : 
    8312                 :         841 :       if (!skip_token (MATCH_ARROW))
    8313                 :             :         {
    8314                 :             :           // skip after somewhere?
    8315                 :             :           // TODO is returning here a good idea? or is break better?
    8316                 :           0 :           return nullptr;
    8317                 :             :         }
    8318                 :             : 
    8319                 :         841 :       ParseRestrictions restrictions;
    8320                 :         841 :       restrictions.expr_can_be_stmt = true;
    8321                 :             : 
    8322                 :         841 :       std::unique_ptr<AST::Expr> expr = parse_expr ({}, restrictions);
    8323                 :             : 
    8324                 :         841 :       if (expr == nullptr)
    8325                 :             :         {
    8326                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8327                 :             :                        "failed to parse expr in match arm in match expr");
    8328                 :           0 :           add_error (std::move (error));
    8329                 :             : 
    8330                 :             :           // skip somewhere?
    8331                 :           0 :           return nullptr;
    8332                 :           0 :         }
    8333                 :             : 
    8334                 :         841 :       bool is_expr_without_block = expr->is_expr_without_block ();
    8335                 :             : 
    8336                 :        1682 :       match_arms.push_back (AST::MatchCase (std::move (arm), std::move (expr)));
    8337                 :             : 
    8338                 :             :       // handle comma presence
    8339                 :        1682 :       if (lexer.peek_token ()->get_id () != COMMA)
    8340                 :             :         {
    8341                 :         419 :           if (!is_expr_without_block)
    8342                 :             :             {
    8343                 :             :               // allowed even if not final case
    8344                 :             :               continue;
    8345                 :             :             }
    8346                 :          16 :           else if (is_expr_without_block
    8347                 :          32 :                    && lexer.peek_token ()->get_id () != RIGHT_CURLY)
    8348                 :             :             {
    8349                 :             :               // not allowed if not final case
    8350                 :           2 :               Error error (lexer.peek_token ()->get_locus (),
    8351                 :             :                            "exprwithoutblock requires comma after match case "
    8352                 :             :                            "expression in match arm (if not final case)");
    8353                 :           2 :               add_error (std::move (error));
    8354                 :             : 
    8355                 :           2 :               return nullptr;
    8356                 :           2 :             }
    8357                 :             :           else
    8358                 :             :             {
    8359                 :             :               // otherwise, must be final case, so fine
    8360                 :             :               break;
    8361                 :             :             }
    8362                 :             :         }
    8363                 :         422 :       lexer.skip_token ();
    8364                 :             :     }
    8365                 :             : 
    8366                 :         360 :   if (!skip_token (RIGHT_CURLY))
    8367                 :             :     {
    8368                 :             :       // skip somewhere?
    8369                 :           0 :       return nullptr;
    8370                 :             :     }
    8371                 :             : 
    8372                 :             :   match_arms.shrink_to_fit ();
    8373                 :             : 
    8374                 :             :   return std::unique_ptr<AST::MatchExpr> (
    8375                 :         360 :     new AST::MatchExpr (std::move (scrutinee), std::move (match_arms),
    8376                 :             :                         std::move (inner_attrs), std::move (outer_attrs),
    8377                 :         360 :                         locus));
    8378                 :         362 : }
    8379                 :             : 
    8380                 :             : // Parses the "pattern" part of the match arm (the 'case x:' equivalent).
    8381                 :             : template <typename ManagedTokenSource>
    8382                 :             : AST::MatchArm
    8383                 :         841 : Parser<ManagedTokenSource>::parse_match_arm ()
    8384                 :             : {
    8385                 :             :   // parse optional outer attributes
    8386                 :         841 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    8387                 :             : 
    8388                 :             :   // DEBUG
    8389                 :         841 :   rust_debug ("about to start parsing match arm patterns");
    8390                 :             : 
    8391                 :             :   // break early if find right curly
    8392                 :        1682 :   if (lexer.peek_token ()->get_id () == RIGHT_CURLY)
    8393                 :             :     {
    8394                 :             :       // not an error
    8395                 :           0 :       return AST::MatchArm::create_error ();
    8396                 :             :     }
    8397                 :             : 
    8398                 :             :   // parse match arm patterns - at least 1 is required
    8399                 :         841 :   std::vector<std::unique_ptr<AST::Pattern>> match_arm_patterns
    8400                 :             :     = parse_match_arm_patterns (RIGHT_CURLY);
    8401                 :         841 :   if (match_arm_patterns.empty ())
    8402                 :             :     {
    8403                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8404                 :             :                    "failed to parse any patterns in match arm");
    8405                 :           0 :       add_error (std::move (error));
    8406                 :             : 
    8407                 :             :       // skip somewhere?
    8408                 :           0 :       return AST::MatchArm::create_error ();
    8409                 :           0 :     }
    8410                 :             : 
    8411                 :             :   // DEBUG
    8412                 :         841 :   rust_debug ("successfully parsed match arm patterns");
    8413                 :             : 
    8414                 :             :   // parse match arm guard expr if it exists
    8415                 :         841 :   std::unique_ptr<AST::Expr> guard_expr = nullptr;
    8416                 :        1682 :   if (lexer.peek_token ()->get_id () == IF)
    8417                 :             :     {
    8418                 :           2 :       lexer.skip_token ();
    8419                 :             : 
    8420                 :           2 :       guard_expr = parse_expr ();
    8421                 :           2 :       if (guard_expr == nullptr)
    8422                 :             :         {
    8423                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8424                 :             :                        "failed to parse guard expression in match arm");
    8425                 :           0 :           add_error (std::move (error));
    8426                 :             : 
    8427                 :             :           // skip somewhere?
    8428                 :           0 :           return AST::MatchArm::create_error ();
    8429                 :           0 :         }
    8430                 :             :     }
    8431                 :             : 
    8432                 :             :   // DEBUG
    8433                 :         841 :   rust_debug ("successfully parsed match arm");
    8434                 :             : 
    8435                 :        1682 :   return AST::MatchArm (std::move (match_arm_patterns),
    8436                 :        1682 :                         lexer.peek_token ()->get_locus (),
    8437                 :         841 :                         std::move (guard_expr), std::move (outer_attrs));
    8438                 :         841 : }
    8439                 :             : 
    8440                 :             : /* Parses the patterns used in a match arm. End token id is the id of the
    8441                 :             :  * token that would exist after the patterns are done (e.g. '}' for match
    8442                 :             :  * expr, '=' for if let and while let). */
    8443                 :             : template <typename ManagedTokenSource>
    8444                 :             : std::vector<std::unique_ptr<AST::Pattern>>
    8445                 :         881 : Parser<ManagedTokenSource>::parse_match_arm_patterns (TokenId end_token_id)
    8446                 :             : {
    8447                 :             :   // skip optional leading '|'
    8448                 :        1762 :   if (lexer.peek_token ()->get_id () == PIPE)
    8449                 :           0 :     lexer.skip_token ();
    8450                 :             :   /* TODO: do I even need to store the result of this? can't be used.
    8451                 :             :    * If semantically different, I need a wrapped "match arm patterns" object
    8452                 :             :    * for this. */
    8453                 :             : 
    8454                 :         881 :   std::vector<std::unique_ptr<AST::Pattern>> patterns;
    8455                 :             : 
    8456                 :             :   // quick break out if end_token_id
    8457                 :        1762 :   if (lexer.peek_token ()->get_id () == end_token_id)
    8458                 :           0 :     return patterns;
    8459                 :             : 
    8460                 :             :   // parse required pattern - if doesn't exist, return empty
    8461                 :         881 :   std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
    8462                 :         881 :   if (initial_pattern == nullptr)
    8463                 :             :     {
    8464                 :             :       // FIXME: should this be an error?
    8465                 :           0 :       return patterns;
    8466                 :             :     }
    8467                 :         881 :   patterns.push_back (std::move (initial_pattern));
    8468                 :             : 
    8469                 :             :   // DEBUG
    8470                 :         881 :   rust_debug ("successfully parsed initial match arm pattern");
    8471                 :             : 
    8472                 :             :   // parse new patterns as long as next char is '|'
    8473                 :         881 :   const_TokenPtr t = lexer.peek_token ();
    8474                 :         881 :   while (t->get_id () == PIPE)
    8475                 :             :     {
    8476                 :             :       // skip pipe token
    8477                 :           0 :       lexer.skip_token ();
    8478                 :             : 
    8479                 :             :       // break if hit end token id
    8480                 :           0 :       if (lexer.peek_token ()->get_id () == end_token_id)
    8481                 :             :         break;
    8482                 :             : 
    8483                 :             :       // parse pattern
    8484                 :           0 :       std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
    8485                 :           0 :       if (pattern == nullptr)
    8486                 :             :         {
    8487                 :             :           // this is an error
    8488                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8489                 :             :                        "failed to parse pattern in match arm patterns");
    8490                 :           0 :           add_error (std::move (error));
    8491                 :             : 
    8492                 :             :           // skip somewhere?
    8493                 :           0 :           return {};
    8494                 :           0 :         }
    8495                 :             : 
    8496                 :           0 :       patterns.push_back (std::move (pattern));
    8497                 :             : 
    8498                 :           0 :       t = lexer.peek_token ();
    8499                 :             :     }
    8500                 :             : 
    8501                 :         881 :   patterns.shrink_to_fit ();
    8502                 :             : 
    8503                 :         881 :   return patterns;
    8504                 :         881 : }
    8505                 :             : 
    8506                 :             : // Parses an async block expression.
    8507                 :             : template <typename ManagedTokenSource>
    8508                 :             : std::unique_ptr<AST::AsyncBlockExpr>
    8509                 :             : Parser<ManagedTokenSource>::parse_async_block_expr (AST::AttrVec outer_attrs)
    8510                 :             : {
    8511                 :             :   location_t locus = lexer.peek_token ()->get_locus ();
    8512                 :             :   skip_token (ASYNC);
    8513                 :             : 
    8514                 :             :   // detect optional move token
    8515                 :             :   bool has_move = false;
    8516                 :             :   if (lexer.peek_token ()->get_id () == MOVE)
    8517                 :             :     {
    8518                 :             :       lexer.skip_token ();
    8519                 :             :       has_move = true;
    8520                 :             :     }
    8521                 :             : 
    8522                 :             :   // parse block expression (required)
    8523                 :             :   std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
    8524                 :             :   if (block_expr == nullptr)
    8525                 :             :     {
    8526                 :             :       Error error (
    8527                 :             :         lexer.peek_token ()->get_locus (),
    8528                 :             :         "failed to parse block expression of async block expression");
    8529                 :             :       add_error (std::move (error));
    8530                 :             : 
    8531                 :             :       // skip somewhere?
    8532                 :             :       return nullptr;
    8533                 :             :     }
    8534                 :             : 
    8535                 :             :   return std::unique_ptr<AST::AsyncBlockExpr> (
    8536                 :             :     new AST::AsyncBlockExpr (std::move (block_expr), has_move,
    8537                 :             :                              std::move (outer_attrs), locus));
    8538                 :             : }
    8539                 :             : 
    8540                 :             : // Parses an unsafe block expression.
    8541                 :             : template <typename ManagedTokenSource>
    8542                 :             : std::unique_ptr<AST::UnsafeBlockExpr>
    8543                 :        2698 : Parser<ManagedTokenSource>::parse_unsafe_block_expr (
    8544                 :             :   AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
    8545                 :             : {
    8546                 :        2698 :   location_t locus = pratt_parsed_loc;
    8547                 :        2698 :   if (locus == UNKNOWN_LOCATION)
    8548                 :             :     {
    8549                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    8550                 :           0 :       skip_token (UNSAFE);
    8551                 :             :     }
    8552                 :             : 
    8553                 :             :   // parse block expression (required)
    8554                 :        2698 :   std::unique_ptr<AST::BlockExpr> block_expr = parse_block_expr ();
    8555                 :        2698 :   if (block_expr == nullptr)
    8556                 :             :     {
    8557                 :           0 :       Error error (
    8558                 :           0 :         lexer.peek_token ()->get_locus (),
    8559                 :             :         "failed to parse block expression of unsafe block expression");
    8560                 :           0 :       add_error (std::move (error));
    8561                 :             : 
    8562                 :             :       // skip somewhere?
    8563                 :           0 :       return nullptr;
    8564                 :           0 :     }
    8565                 :             : 
    8566                 :             :   return std::unique_ptr<AST::UnsafeBlockExpr> (
    8567                 :        2698 :     new AST::UnsafeBlockExpr (std::move (block_expr), std::move (outer_attrs),
    8568                 :        2698 :                               locus));
    8569                 :        2698 : }
    8570                 :             : 
    8571                 :             : // Parses an array definition expression.
    8572                 :             : template <typename ManagedTokenSource>
    8573                 :             : std::unique_ptr<AST::ArrayExpr>
    8574                 :         340 : Parser<ManagedTokenSource>::parse_array_expr (AST::AttrVec outer_attrs,
    8575                 :             :                                               location_t pratt_parsed_loc)
    8576                 :             : {
    8577                 :         340 :   location_t locus = pratt_parsed_loc;
    8578                 :         340 :   if (locus == UNKNOWN_LOCATION)
    8579                 :             :     {
    8580                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    8581                 :           0 :       skip_token (LEFT_SQUARE);
    8582                 :             :     }
    8583                 :             : 
    8584                 :             :   // parse optional inner attributes
    8585                 :         340 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    8586                 :             : 
    8587                 :             :   // parse the "array elements" section, which is optional
    8588                 :         680 :   if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
    8589                 :             :     {
    8590                 :             :       // no array elements
    8591                 :           2 :       lexer.skip_token ();
    8592                 :             : 
    8593                 :           2 :       std::vector<std::unique_ptr<AST::Expr>> exprs;
    8594                 :           2 :       auto array_elems
    8595                 :             :         = std::make_unique<AST::ArrayElemsValues> (std::move (exprs), locus);
    8596                 :             :       return std::make_unique<AST::ArrayExpr> (std::move (array_elems),
    8597                 :             :                                                std::move (inner_attrs),
    8598                 :           2 :                                                std::move (outer_attrs), locus);
    8599                 :           2 :     }
    8600                 :             :   else
    8601                 :             :     {
    8602                 :             :       // should have array elements
    8603                 :             :       // parse initial expression, which is required for either
    8604                 :         338 :       std::unique_ptr<AST::Expr> initial_expr = parse_expr ();
    8605                 :         338 :       if (initial_expr == nullptr)
    8606                 :             :         {
    8607                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8608                 :             :                        "could not parse expression in array expression "
    8609                 :             :                        "(even though arrayelems seems to be present)");
    8610                 :           0 :           add_error (std::move (error));
    8611                 :             : 
    8612                 :             :           // skip somewhere?
    8613                 :           0 :           return nullptr;
    8614                 :           0 :         }
    8615                 :             : 
    8616                 :         676 :       if (lexer.peek_token ()->get_id () == SEMICOLON)
    8617                 :             :         {
    8618                 :             :           // copy array elems
    8619                 :         113 :           lexer.skip_token ();
    8620                 :             : 
    8621                 :             :           // parse copy amount expression (required)
    8622                 :         113 :           std::unique_ptr<AST::Expr> copy_amount = parse_expr ();
    8623                 :         113 :           if (copy_amount == nullptr)
    8624                 :             :             {
    8625                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
    8626                 :             :                            "could not parse copy amount expression in array "
    8627                 :             :                            "expression (arrayelems)");
    8628                 :           0 :               add_error (std::move (error));
    8629                 :             : 
    8630                 :             :               // skip somewhere?
    8631                 :           0 :               return nullptr;
    8632                 :           0 :             }
    8633                 :             : 
    8634                 :         113 :           skip_token (RIGHT_SQUARE);
    8635                 :             : 
    8636                 :         113 :           std::unique_ptr<AST::ArrayElemsCopied> copied_array_elems (
    8637                 :         113 :             new AST::ArrayElemsCopied (std::move (initial_expr),
    8638                 :             :                                        std::move (copy_amount), locus));
    8639                 :             :           return std::unique_ptr<AST::ArrayExpr> (
    8640                 :         113 :             new AST::ArrayExpr (std::move (copied_array_elems),
    8641                 :             :                                 std::move (inner_attrs),
    8642                 :         113 :                                 std::move (outer_attrs), locus));
    8643                 :         113 :         }
    8644                 :         450 :       else if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
    8645                 :             :         {
    8646                 :             :           // single-element array expression
    8647                 :          35 :           std::vector<std::unique_ptr<AST::Expr>> exprs;
    8648                 :          35 :           exprs.reserve (1);
    8649                 :          35 :           exprs.push_back (std::move (initial_expr));
    8650                 :             :           exprs.shrink_to_fit ();
    8651                 :             : 
    8652                 :          35 :           skip_token (RIGHT_SQUARE);
    8653                 :             : 
    8654                 :          35 :           std::unique_ptr<AST::ArrayElemsValues> array_elems (
    8655                 :          35 :             new AST::ArrayElemsValues (std::move (exprs), locus));
    8656                 :             :           return std::unique_ptr<AST::ArrayExpr> (
    8657                 :          35 :             new AST::ArrayExpr (std::move (array_elems),
    8658                 :             :                                 std::move (inner_attrs),
    8659                 :          35 :                                 std::move (outer_attrs), locus));
    8660                 :          35 :         }
    8661                 :         380 :       else if (lexer.peek_token ()->get_id () == COMMA)
    8662                 :             :         {
    8663                 :             :           // multi-element array expression (or trailing comma)
    8664                 :         190 :           std::vector<std::unique_ptr<AST::Expr>> exprs;
    8665                 :         190 :           exprs.push_back (std::move (initial_expr));
    8666                 :             : 
    8667                 :         190 :           const_TokenPtr t = lexer.peek_token ();
    8668                 :         900 :           while (t->get_id () == COMMA)
    8669                 :             :             {
    8670                 :         717 :               lexer.skip_token ();
    8671                 :             : 
    8672                 :             :               // quick break if right square bracket
    8673                 :        1434 :               if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
    8674                 :             :                 break;
    8675                 :             : 
    8676                 :             :               // parse expression (required)
    8677                 :         710 :               std::unique_ptr<AST::Expr> expr = parse_expr ();
    8678                 :         710 :               if (expr == nullptr)
    8679                 :             :                 {
    8680                 :           0 :                   Error error (lexer.peek_token ()->get_locus (),
    8681                 :             :                                "failed to parse element in array expression");
    8682                 :           0 :                   add_error (std::move (error));
    8683                 :             : 
    8684                 :             :                   // skip somewhere?
    8685                 :           0 :                   return nullptr;
    8686                 :           0 :                 }
    8687                 :         710 :               exprs.push_back (std::move (expr));
    8688                 :             : 
    8689                 :         710 :               t = lexer.peek_token ();
    8690                 :             :             }
    8691                 :             : 
    8692                 :         190 :           skip_token (RIGHT_SQUARE);
    8693                 :             : 
    8694                 :             :           exprs.shrink_to_fit ();
    8695                 :             : 
    8696                 :         190 :           std::unique_ptr<AST::ArrayElemsValues> array_elems (
    8697                 :         190 :             new AST::ArrayElemsValues (std::move (exprs), locus));
    8698                 :             :           return std::unique_ptr<AST::ArrayExpr> (
    8699                 :         190 :             new AST::ArrayExpr (std::move (array_elems),
    8700                 :             :                                 std::move (inner_attrs),
    8701                 :         190 :                                 std::move (outer_attrs), locus));
    8702                 :         380 :         }
    8703                 :             :       else
    8704                 :             :         {
    8705                 :             :           // error
    8706                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8707                 :             :                        "unexpected token %qs in array expression (arrayelems)",
    8708                 :           0 :                        lexer.peek_token ()->get_token_description ());
    8709                 :           0 :           add_error (std::move (error));
    8710                 :             : 
    8711                 :             :           // skip somewhere?
    8712                 :           0 :           return nullptr;
    8713                 :           0 :         }
    8714                 :         338 :     }
    8715                 :         340 : }
    8716                 :             : 
    8717                 :             : // Parses a single parameter used in a closure definition.
    8718                 :             : template <typename ManagedTokenSource>
    8719                 :             : AST::ClosureParam
    8720                 :          79 : Parser<ManagedTokenSource>::parse_closure_param ()
    8721                 :             : {
    8722                 :          79 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
    8723                 :             : 
    8724                 :             :   // parse pattern (which is required)
    8725                 :          79 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
    8726                 :          79 :   if (pattern == nullptr)
    8727                 :             :     {
    8728                 :             :       // not necessarily an error
    8729                 :           0 :       return AST::ClosureParam::create_error ();
    8730                 :             :     }
    8731                 :             : 
    8732                 :             :   // parse optional type of param
    8733                 :          79 :   std::unique_ptr<AST::Type> type = nullptr;
    8734                 :         158 :   if (lexer.peek_token ()->get_id () == COLON)
    8735                 :             :     {
    8736                 :          63 :       lexer.skip_token ();
    8737                 :             : 
    8738                 :             :       // parse type, which is now required
    8739                 :          63 :       type = parse_type ();
    8740                 :          63 :       if (type == nullptr)
    8741                 :             :         {
    8742                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    8743                 :             :                        "failed to parse type in closure parameter");
    8744                 :           0 :           add_error (std::move (error));
    8745                 :             : 
    8746                 :             :           // skip somewhere?
    8747                 :           0 :           return AST::ClosureParam::create_error ();
    8748                 :           0 :         }
    8749                 :             :     }
    8750                 :             : 
    8751                 :          79 :   location_t loc = pattern->get_locus ();
    8752                 :          79 :   return AST::ClosureParam (std::move (pattern), loc, std::move (type),
    8753                 :          79 :                             std::move (outer_attrs));
    8754                 :          79 : }
    8755                 :             : 
    8756                 :             : // Parses a grouped or tuple expression (disambiguates).
    8757                 :             : template <typename ManagedTokenSource>
    8758                 :             : std::unique_ptr<AST::ExprWithoutBlock>
    8759                 :         670 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_expr (
    8760                 :             :   AST::AttrVec outer_attrs, location_t pratt_parsed_loc)
    8761                 :             : {
    8762                 :             :   // adjustment to allow Pratt parsing to reuse function without copy-paste
    8763                 :         670 :   location_t locus = pratt_parsed_loc;
    8764                 :         670 :   if (locus == UNKNOWN_LOCATION)
    8765                 :             :     {
    8766                 :           0 :       locus = lexer.peek_token ()->get_locus ();
    8767                 :           0 :       skip_token (LEFT_PAREN);
    8768                 :             :     }
    8769                 :             : 
    8770                 :             :   // parse optional inner attributes
    8771                 :         670 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
    8772                 :             : 
    8773                 :        1340 :   if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    8774                 :             :     {
    8775                 :             :       // must be empty tuple
    8776                 :         166 :       lexer.skip_token ();
    8777                 :             : 
    8778                 :             :       // create tuple with empty tuple elems
    8779                 :         166 :       return std::unique_ptr<AST::TupleExpr> (
    8780                 :         166 :         new AST::TupleExpr (std::vector<std::unique_ptr<AST::Expr>> (),
    8781                 :             :                             std::move (inner_attrs), std::move (outer_attrs),
    8782                 :         166 :                             locus));
    8783                 :             :     }
    8784                 :             : 
    8785                 :             :   // parse first expression (required)
    8786                 :         504 :   std::unique_ptr<AST::Expr> first_expr = parse_expr ();
    8787                 :         504 :   if (first_expr == nullptr)
    8788                 :             :     {
    8789                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    8790                 :             :                    "failed to parse expression in grouped or tuple expression");
    8791                 :           0 :       add_error (std::move (error));
    8792                 :             : 
    8793                 :             :       // skip after somewhere?
    8794                 :           0 :       return nullptr;
    8795                 :           0 :     }
    8796                 :             : 
    8797                 :             :   // detect whether grouped expression with right parentheses as next token
    8798                 :        1008 :   if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    8799                 :             :     {
    8800                 :             :       // must be grouped expr
    8801                 :         209 :       lexer.skip_token ();
    8802                 :             : 
    8803                 :             :       // create grouped expr
    8804                 :         209 :       return std::unique_ptr<AST::GroupedExpr> (
    8805                 :         209 :         new AST::GroupedExpr (std::move (first_expr), std::move (inner_attrs),
    8806                 :         209 :                               std::move (outer_attrs), locus));
    8807                 :             :     }
    8808                 :         590 :   else if (lexer.peek_token ()->get_id () == COMMA)
    8809                 :             :     {
    8810                 :             :       // tuple expr
    8811                 :         293 :       std::vector<std::unique_ptr<AST::Expr>> exprs;
    8812                 :         293 :       exprs.push_back (std::move (first_expr));
    8813                 :             : 
    8814                 :             :       // parse potential other tuple exprs
    8815                 :         293 :       const_TokenPtr t = lexer.peek_token ();
    8816                 :         716 :       while (t->get_id () == COMMA)
    8817                 :             :         {
    8818                 :         450 :           lexer.skip_token ();
    8819                 :             : 
    8820                 :             :           // break out if right paren
    8821                 :         900 :           if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
    8822                 :             :             break;
    8823                 :             : 
    8824                 :             :           // parse expr, which is now required
    8825                 :         423 :           std::unique_ptr<AST::Expr> expr = parse_expr ();
    8826                 :         423 :           if (expr == nullptr)
    8827                 :             :             {
    8828                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
    8829                 :             :                            "failed to parse expr in tuple expr");
    8830                 :           0 :               add_error (std::move (error));
    8831                 :             : 
    8832                 :             :               // skip somewhere?
    8833                 :           0 :               return nullptr;
    8834                 :           0 :             }
    8835                 :         423 :           exprs.push_back (std::move (expr));
    8836                 :             : 
    8837                 :         423 :           t = lexer.peek_token ();
    8838                 :             :         }
    8839                 :             : 
    8840                 :             :       // skip right paren
    8841                 :         293 :       skip_token (RIGHT_PAREN);
    8842                 :             : 
    8843                 :         293 :       return std::unique_ptr<AST::TupleExpr> (
    8844                 :         293 :         new AST::TupleExpr (std::move (exprs), std::move (inner_attrs),
    8845                 :         293 :                             std::move (outer_attrs), locus));
    8846                 :         293 :     }
    8847                 :             :   else
    8848                 :             :     {
    8849                 :             :       // error
    8850                 :           2 :       const_TokenPtr t = lexer.peek_token ();
    8851                 :           2 :       Error error (t->get_locus (),
    8852                 :             :                    "unexpected token %qs in grouped or tuple expression "
    8853                 :             :                    "(parenthesised expression) - expected %<)%> for grouped "
    8854                 :             :                    "expr and %<,%> for tuple expr",
    8855                 :             :                    t->get_token_description ());
    8856                 :           2 :       add_error (std::move (error));
    8857                 :             : 
    8858                 :             :       // skip somewhere?
    8859                 :           2 :       return nullptr;
    8860                 :           4 :     }
    8861                 :         670 : }
    8862                 :             : 
    8863                 :             : // Parses a type (will further disambiguate any type).
    8864                 :             : template <typename ManagedTokenSource>
    8865                 :             : std::unique_ptr<AST::Type>
    8866                 :       30010 : Parser<ManagedTokenSource>::parse_type (bool save_errors)
    8867                 :             : {
    8868                 :             :   /* rules for all types:
    8869                 :             :    * NeverType:               '!'
    8870                 :             :    * SliceType:               '[' Type ']'
    8871                 :             :    * InferredType:            '_'
    8872                 :             :    * MacroInvocation:         SimplePath '!' DelimTokenTree
    8873                 :             :    * ParenthesisedType:       '(' Type ')'
    8874                 :             :    * ImplTraitType:           'impl' TypeParamBounds
    8875                 :             :    *  TypeParamBounds (not type)  TypeParamBound ( '+' TypeParamBound )* '+'?
    8876                 :             :    *  TypeParamBound          Lifetime | TraitBound
    8877                 :             :    * ImplTraitTypeOneBound:   'impl' TraitBound
    8878                 :             :    * TraitObjectType:         'dyn'? TypeParamBounds
    8879                 :             :    * TraitObjectTypeOneBound: 'dyn'? TraitBound
    8880                 :             :    *  TraitBound              '?'? ForLifetimes? TypePath | '(' '?'?
    8881                 :             :    * ForLifetimes? TypePath ')' BareFunctionType:        ForLifetimes?
    8882                 :             :    * FunctionQualifiers 'fn' etc. ForLifetimes (not type) 'for' '<'
    8883                 :             :    * LifetimeParams '>' FunctionQualifiers      ( 'async' | 'const' )?
    8884                 :             :    * 'unsafe'?
    8885                 :             :    * ('extern' abi?)? QualifiedPathInType:     '<' Type ( 'as' TypePath )? '>'
    8886                 :             :    * (
    8887                 :             :    * '::' TypePathSegment )+ TypePath:                '::'? TypePathSegment (
    8888                 :             :    * '::' TypePathSegment)* ArrayType:               '[' Type ';' Expr ']'
    8889                 :             :    * ReferenceType:           '&' Lifetime? 'mut'? TypeNoBounds
    8890                 :             :    * RawPointerType:          '*' ( 'mut' | 'const' ) TypeNoBounds
    8891                 :             :    * TupleType:               '(' Type etc. - regular tuple stuff. Also
    8892                 :             :    * regular tuple vs parenthesised precedence
    8893                 :             :    *
    8894                 :             :    * Disambiguate between macro and type path via type path being parsed, and
    8895                 :             :    * then if '!' found, convert type path to simple path for macro. Usual
    8896                 :             :    * disambiguation for tuple vs parenthesised. For ImplTraitType and
    8897                 :             :    * TraitObjectType individual disambiguations, they seem more like "special
    8898                 :             :    * cases", so probably just try to parse the more general ImplTraitType or
    8899                 :             :    * TraitObjectType and return OneBound versions if they satisfy those
    8900                 :             :    * criteria. */
    8901                 :             : 
    8902                 :       30010 :   const_TokenPtr t = lexer.peek_token ();
    8903                 :       30010 :   switch (t->get_id ())
    8904                 :             :     {
    8905                 :          33 :     case EXCLAM:
    8906                 :             :       // never type - can't be macro as no path beforehand
    8907                 :          33 :       lexer.skip_token ();
    8908                 :          33 :       return std::unique_ptr<AST::NeverType> (
    8909                 :          33 :         new AST::NeverType (t->get_locus ()));
    8910                 :         622 :     case LEFT_SQUARE:
    8911                 :             :       // slice type or array type - requires further disambiguation
    8912                 :         622 :       return parse_slice_or_array_type ();
    8913                 :         405 :     case LEFT_SHIFT:
    8914                 :             :       case LEFT_ANGLE: {
    8915                 :             :         // qualified path in type
    8916                 :         405 :         AST::QualifiedPathInType path = parse_qualified_path_in_type ();
    8917                 :         405 :         if (path.is_error ())
    8918                 :             :           {
    8919                 :           0 :             if (save_errors)
    8920                 :             :               {
    8921                 :           0 :                 Error error (t->get_locus (),
    8922                 :             :                              "failed to parse qualified path in type");
    8923                 :           0 :                 add_error (std::move (error));
    8924                 :           0 :               }
    8925                 :             : 
    8926                 :           0 :             return nullptr;
    8927                 :             :           }
    8928                 :         405 :         return std::unique_ptr<AST::QualifiedPathInType> (
    8929                 :         405 :           new AST::QualifiedPathInType (std::move (path)));
    8930                 :         405 :       }
    8931                 :         101 :     case UNDERSCORE:
    8932                 :             :       // inferred type
    8933                 :         101 :       lexer.skip_token ();
    8934                 :         101 :       return std::unique_ptr<AST::InferredType> (
    8935                 :         101 :         new AST::InferredType (t->get_locus ()));
    8936                 :        2259 :     case ASTERISK:
    8937                 :             :       // raw pointer type
    8938                 :        2259 :       return parse_raw_pointer_type ();
    8939                 :        2395 :     case AMP: // does this also include AMP_AMP?
    8940                 :             :     case LOGICAL_AND:
    8941                 :             :       // reference type
    8942                 :        2395 :       return parse_reference_type ();
    8943                 :           0 :       case LIFETIME: {
    8944                 :             :         /* probably a lifetime bound, so probably type param bounds in
    8945                 :             :          * TraitObjectType */
    8946                 :           0 :         std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    8947                 :             :           = parse_type_param_bounds ();
    8948                 :             : 
    8949                 :           0 :         return std::unique_ptr<AST::TraitObjectType> (
    8950                 :           0 :           new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    8951                 :           0 :                                     false));
    8952                 :           0 :       }
    8953                 :       23827 :     case IDENTIFIER:
    8954                 :             :     case SUPER:
    8955                 :             :     case SELF:
    8956                 :             :     case SELF_ALIAS:
    8957                 :             :     case CRATE:
    8958                 :             :     case DOLLAR_SIGN:
    8959                 :             :       case SCOPE_RESOLUTION: {
    8960                 :             :         // macro invocation or type path - requires further disambiguation.
    8961                 :             :         /* for parsing path component of each rule, perhaps parse it as a
    8962                 :             :          * typepath and attempt conversion to simplepath if a trailing '!' is
    8963                 :             :          * found */
    8964                 :             :         /* Type path also includes TraitObjectTypeOneBound BUT if it starts
    8965                 :             :          * with it, it is exactly the same as a TypePath syntactically, so
    8966                 :             :          * this is a syntactical ambiguity. As such, the parser will parse it
    8967                 :             :          * as a TypePath. This, however, does not prevent TraitObjectType from
    8968                 :             :          * starting with a typepath. */
    8969                 :             : 
    8970                 :             :         // parse path as type path
    8971                 :       23827 :         AST::TypePath path = parse_type_path ();
    8972                 :       23827 :         if (path.is_error ())
    8973                 :             :           {
    8974                 :           0 :             if (save_errors)
    8975                 :             :               {
    8976                 :           0 :                 Error error (t->get_locus (),
    8977                 :             :                              "failed to parse path as first component of type");
    8978                 :           0 :                 add_error (std::move (error));
    8979                 :           0 :               }
    8980                 :             : 
    8981                 :           0 :             return nullptr;
    8982                 :             :           }
    8983                 :       23827 :         location_t locus = path.get_locus ();
    8984                 :             : 
    8985                 :             :         // branch on next token
    8986                 :       23827 :         t = lexer.peek_token ();
    8987                 :       23827 :         switch (t->get_id ())
    8988                 :             :           {
    8989                 :          31 :             case EXCLAM: {
    8990                 :             :               // macro invocation
    8991                 :             :               // convert to simple path
    8992                 :          31 :               AST::SimplePath macro_path = path.as_simple_path ();
    8993                 :          31 :               if (macro_path.is_empty ())
    8994                 :             :                 {
    8995                 :           0 :                   if (save_errors)
    8996                 :             :                     {
    8997                 :           0 :                       Error error (t->get_locus (),
    8998                 :             :                                    "failed to parse simple path in macro "
    8999                 :             :                                    "invocation (for type)");
    9000                 :           0 :                       add_error (std::move (error));
    9001                 :           0 :                     }
    9002                 :             : 
    9003                 :           0 :                   return nullptr;
    9004                 :             :                 }
    9005                 :             : 
    9006                 :          31 :               lexer.skip_token ();
    9007                 :             : 
    9008                 :          31 :               AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
    9009                 :             : 
    9010                 :          62 :               return AST::MacroInvocation::Regular (
    9011                 :          62 :                 AST::MacroInvocData (std::move (macro_path),
    9012                 :             :                                      std::move (tok_tree)),
    9013                 :          31 :                 {}, locus);
    9014                 :          62 :             }
    9015                 :           0 :             case PLUS: {
    9016                 :             :               // type param bounds
    9017                 :           0 :               std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    9018                 :             : 
    9019                 :             :               // convert type path to trait bound
    9020                 :           0 :               std::unique_ptr<AST::TraitBound> path_bound (
    9021                 :           0 :                 new AST::TraitBound (std::move (path), locus, false, false));
    9022                 :           0 :               bounds.push_back (std::move (path_bound));
    9023                 :             : 
    9024                 :             :               /* parse rest of bounds - FIXME: better way to find when to stop
    9025                 :             :                * parsing */
    9026                 :           0 :               while (t->get_id () == PLUS)
    9027                 :             :                 {
    9028                 :           0 :                   lexer.skip_token ();
    9029                 :             : 
    9030                 :             :                   // parse bound if it exists - if not, assume end of sequence
    9031                 :           0 :                   std::unique_ptr<AST::TypeParamBound> bound
    9032                 :             :                     = parse_type_param_bound ();
    9033                 :           0 :                   if (bound == nullptr)
    9034                 :             :                     {
    9035                 :             :                       break;
    9036                 :             :                     }
    9037                 :           0 :                   bounds.push_back (std::move (bound));
    9038                 :             : 
    9039                 :           0 :                   t = lexer.peek_token ();
    9040                 :             :                 }
    9041                 :             : 
    9042                 :           0 :               return std::unique_ptr<AST::TraitObjectType> (
    9043                 :           0 :                 new AST::TraitObjectType (std::move (bounds), locus, false));
    9044                 :           0 :             }
    9045                 :       23796 :           default:
    9046                 :             :             // assume that this is a type path and not an error
    9047                 :       23796 :             return std::unique_ptr<AST::TypePath> (
    9048                 :       23796 :               new AST::TypePath (std::move (path)));
    9049                 :             :           }
    9050                 :       23827 :       }
    9051                 :         273 :     case LEFT_PAREN:
    9052                 :             :       /* tuple type or parenthesised type - requires further disambiguation
    9053                 :             :        * (the usual). ok apparently can be a parenthesised TraitBound too, so
    9054                 :             :        * could be TraitObjectTypeOneBound or TraitObjectType */
    9055                 :         273 :       return parse_paren_prefixed_type ();
    9056                 :           4 :     case FOR:
    9057                 :             :       // TraitObjectTypeOneBound or BareFunctionType
    9058                 :           4 :       return parse_for_prefixed_type ();
    9059                 :          50 :     case ASYNC:
    9060                 :             :     case CONST:
    9061                 :             :     case UNSAFE:
    9062                 :             :     case EXTERN_KW:
    9063                 :             :     case FN_KW:
    9064                 :             :       // bare function type (with no for lifetimes)
    9065                 :          50 :       return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
    9066                 :           0 :     case IMPL:
    9067                 :           0 :       lexer.skip_token ();
    9068                 :           0 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    9069                 :             :         {
    9070                 :             :           /* cannot be one bound because lifetime prevents it from being
    9071                 :             :            * traitbound */
    9072                 :           0 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    9073                 :             :             = parse_type_param_bounds ();
    9074                 :             : 
    9075                 :           0 :           return std::unique_ptr<AST::ImplTraitType> (
    9076                 :           0 :             new AST::ImplTraitType (std::move (bounds), t->get_locus ()));
    9077                 :           0 :         }
    9078                 :             :       else
    9079                 :             :         {
    9080                 :             :           // should be trait bound, so parse trait bound
    9081                 :           0 :           std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    9082                 :           0 :           if (initial_bound == nullptr)
    9083                 :             :             {
    9084                 :           0 :               if (save_errors)
    9085                 :             :                 {
    9086                 :           0 :                   Error error (lexer.peek_token ()->get_locus (),
    9087                 :             :                                "failed to parse ImplTraitType initial bound");
    9088                 :           0 :                   add_error (std::move (error));
    9089                 :           0 :                 }
    9090                 :             : 
    9091                 :           0 :               return nullptr;
    9092                 :             :             }
    9093                 :             : 
    9094                 :           0 :           location_t locus = t->get_locus ();
    9095                 :             : 
    9096                 :             :           // short cut if next token isn't '+'
    9097                 :           0 :           t = lexer.peek_token ();
    9098                 :           0 :           if (t->get_id () != PLUS)
    9099                 :             :             {
    9100                 :             :               // convert trait bound to value object
    9101                 :           0 :               AST::TraitBound value_bound (*initial_bound);
    9102                 :             : 
    9103                 :             :               // DEBUG: removed as unique ptr, so should auto-delete
    9104                 :             :               // delete initial_bound;
    9105                 :             : 
    9106                 :           0 :               return std::unique_ptr<AST::ImplTraitTypeOneBound> (
    9107                 :           0 :                 new AST::ImplTraitTypeOneBound (std::move (value_bound),
    9108                 :           0 :                                                 locus));
    9109                 :           0 :             }
    9110                 :             : 
    9111                 :             :           // parse additional type param bounds
    9112                 :           0 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    9113                 :           0 :           bounds.push_back (std::move (initial_bound));
    9114                 :           0 :           while (t->get_id () == PLUS)
    9115                 :             :             {
    9116                 :           0 :               lexer.skip_token ();
    9117                 :             : 
    9118                 :             :               // parse bound if it exists
    9119                 :           0 :               std::unique_ptr<AST::TypeParamBound> bound
    9120                 :             :                 = parse_type_param_bound ();
    9121                 :           0 :               if (bound == nullptr)
    9122                 :             :                 {
    9123                 :             :                   // not an error as trailing plus may exist
    9124                 :             :                   break;
    9125                 :             :                 }
    9126                 :           0 :               bounds.push_back (std::move (bound));
    9127                 :             : 
    9128                 :           0 :               t = lexer.peek_token ();
    9129                 :             :             }
    9130                 :             : 
    9131                 :           0 :           return std::unique_ptr<AST::ImplTraitType> (
    9132                 :           0 :             new AST::ImplTraitType (std::move (bounds), locus));
    9133                 :           0 :         }
    9134                 :          35 :     case DYN:
    9135                 :             :       case QUESTION_MARK: {
    9136                 :             :         // either TraitObjectType or TraitObjectTypeOneBound
    9137                 :          35 :         bool has_dyn = false;
    9138                 :          35 :         if (t->get_id () == DYN)
    9139                 :             :           {
    9140                 :          35 :             lexer.skip_token ();
    9141                 :          35 :             has_dyn = true;
    9142                 :             :           }
    9143                 :             : 
    9144                 :          70 :         if (lexer.peek_token ()->get_id () == LIFETIME)
    9145                 :             :           {
    9146                 :             :             /* cannot be one bound because lifetime prevents it from being
    9147                 :             :              * traitbound */
    9148                 :           0 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds
    9149                 :             :               = parse_type_param_bounds ();
    9150                 :             : 
    9151                 :           0 :             return std::unique_ptr<AST::TraitObjectType> (
    9152                 :           0 :               new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    9153                 :           0 :                                         has_dyn));
    9154                 :           0 :           }
    9155                 :             :         else
    9156                 :             :           {
    9157                 :             :             // should be trait bound, so parse trait bound
    9158                 :          35 :             std::unique_ptr<AST::TraitBound> initial_bound
    9159                 :             :               = parse_trait_bound ();
    9160                 :          35 :             if (initial_bound == nullptr)
    9161                 :             :               {
    9162                 :           0 :                 if (save_errors)
    9163                 :             :                   {
    9164                 :           0 :                     Error error (
    9165                 :           0 :                       lexer.peek_token ()->get_locus (),
    9166                 :             :                       "failed to parse TraitObjectType initial bound");
    9167                 :           0 :                     add_error (std::move (error));
    9168                 :           0 :                   }
    9169                 :             : 
    9170                 :           0 :                 return nullptr;
    9171                 :             :               }
    9172                 :             : 
    9173                 :             :             // short cut if next token isn't '+'
    9174                 :          35 :             t = lexer.peek_token ();
    9175                 :          35 :             if (t->get_id () != PLUS)
    9176                 :             :               {
    9177                 :             :                 // convert trait bound to value object
    9178                 :          14 :                 AST::TraitBound value_bound (*initial_bound);
    9179                 :             : 
    9180                 :             :                 // DEBUG: removed as unique ptr, so should auto delete
    9181                 :             :                 // delete initial_bound;
    9182                 :             : 
    9183                 :          14 :                 return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    9184                 :          28 :                   new AST::TraitObjectTypeOneBound (std::move (value_bound),
    9185                 :          14 :                                                     t->get_locus (), has_dyn));
    9186                 :          14 :               }
    9187                 :             : 
    9188                 :             :             // parse additional type param bounds
    9189                 :          21 :             std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    9190                 :          21 :             bounds.push_back (std::move (initial_bound));
    9191                 :          58 :             while (t->get_id () == PLUS)
    9192                 :             :               {
    9193                 :          37 :                 lexer.skip_token ();
    9194                 :             : 
    9195                 :             :                 // parse bound if it exists
    9196                 :          37 :                 std::unique_ptr<AST::TypeParamBound> bound
    9197                 :             :                   = parse_type_param_bound ();
    9198                 :          37 :                 if (bound == nullptr)
    9199                 :             :                   {
    9200                 :             :                     // not an error as trailing plus may exist
    9201                 :             :                     break;
    9202                 :             :                   }
    9203                 :          37 :                 bounds.push_back (std::move (bound));
    9204                 :             : 
    9205                 :          37 :                 t = lexer.peek_token ();
    9206                 :             :               }
    9207                 :             : 
    9208                 :          21 :             return std::unique_ptr<AST::TraitObjectType> (
    9209                 :          21 :               new AST::TraitObjectType (std::move (bounds), t->get_locus (),
    9210                 :          21 :                                         has_dyn));
    9211                 :          35 :           }
    9212                 :             :       }
    9213                 :           6 :     default:
    9214                 :           6 :       if (save_errors)
    9215                 :           6 :         add_error (Error (t->get_locus (), "unrecognised token %qs in type",
    9216                 :             :                           t->get_token_description ()));
    9217                 :             : 
    9218                 :           6 :       return nullptr;
    9219                 :             :     }
    9220                 :       30010 : }
    9221                 :             : 
    9222                 :             : /* Parses a type that has '(' as its first character. Returns a tuple type,
    9223                 :             :  * parenthesised type, TraitObjectTypeOneBound, or TraitObjectType depending
    9224                 :             :  * on following characters. */
    9225                 :             : template <typename ManagedTokenSource>
    9226                 :             : std::unique_ptr<AST::Type>
    9227                 :         273 : Parser<ManagedTokenSource>::parse_paren_prefixed_type ()
    9228                 :             : {
    9229                 :             :   /* NOTE: Syntactical ambiguity of a parenthesised trait bound is considered
    9230                 :             :    * a trait bound, not a parenthesised type, so that it can still be used in
    9231                 :             :    * type param bounds. */
    9232                 :             : 
    9233                 :             :   /* NOTE: this implementation is really shit but I couldn't think of a better
    9234                 :             :    * one. It requires essentially breaking polymorphism and downcasting via
    9235                 :             :    * virtual method abuse, as it was copied from the rustc implementation (in
    9236                 :             :    * which types are reified due to tagged union), after a more OOP attempt by
    9237                 :             :    * me failed. */
    9238                 :         273 :   location_t left_delim_locus = lexer.peek_token ()->get_locus ();
    9239                 :             : 
    9240                 :             :   // skip left delim
    9241                 :         273 :   lexer.skip_token ();
    9242                 :             :   /* while next token isn't close delim, parse comma-separated types, saving
    9243                 :             :    * whether trailing comma happens */
    9244                 :         273 :   const_TokenPtr t = lexer.peek_token ();
    9245                 :         273 :   bool trailing_comma = true;
    9246                 :         273 :   std::vector<std::unique_ptr<AST::Type>> types;
    9247                 :             : 
    9248                 :         493 :   while (t->get_id () != RIGHT_PAREN)
    9249                 :             :     {
    9250                 :         424 :       std::unique_ptr<AST::Type> type = parse_type ();
    9251                 :         424 :       if (type == nullptr)
    9252                 :             :         {
    9253                 :           0 :           Error error (t->get_locus (),
    9254                 :             :                        "failed to parse type inside parentheses (probably "
    9255                 :             :                        "tuple or parenthesised)");
    9256                 :           0 :           add_error (std::move (error));
    9257                 :             : 
    9258                 :           0 :           return nullptr;
    9259                 :           0 :         }
    9260                 :         424 :       types.push_back (std::move (type));
    9261                 :             : 
    9262                 :         424 :       t = lexer.peek_token ();
    9263                 :         424 :       if (t->get_id () != COMMA)
    9264                 :             :         {
    9265                 :         204 :           trailing_comma = false;
    9266                 :             :           break;
    9267                 :             :         }
    9268                 :         220 :       lexer.skip_token ();
    9269                 :             : 
    9270                 :         220 :       t = lexer.peek_token ();
    9271                 :             :     }
    9272                 :             : 
    9273                 :         273 :   if (!skip_token (RIGHT_PAREN))
    9274                 :             :     {
    9275                 :           0 :       return nullptr;
    9276                 :             :     }
    9277                 :             : 
    9278                 :             :   // if only one type and no trailing comma, then not a tuple type
    9279                 :         273 :   if (types.size () == 1 && !trailing_comma)
    9280                 :             :     {
    9281                 :             :       // must be a TraitObjectType (with more than one bound)
    9282                 :           0 :       if (lexer.peek_token ()->get_id () == PLUS)
    9283                 :             :         {
    9284                 :             :           // create type param bounds vector
    9285                 :           0 :           std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    9286                 :             : 
    9287                 :             :           // HACK: convert type to traitbound and add to bounds
    9288                 :           0 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
    9289                 :           0 :           std::unique_ptr<AST::TraitBound> converted_bound (
    9290                 :           0 :             released_ptr->to_trait_bound (true));
    9291                 :           0 :           if (converted_bound == nullptr)
    9292                 :             :             {
    9293                 :           0 :               Error error (
    9294                 :           0 :                 lexer.peek_token ()->get_locus (),
    9295                 :             :                 "failed to hackily converted parsed type to trait bound");
    9296                 :           0 :               add_error (std::move (error));
    9297                 :             : 
    9298                 :           0 :               return nullptr;
    9299                 :           0 :             }
    9300                 :           0 :           bounds.push_back (std::move (converted_bound));
    9301                 :             : 
    9302                 :           0 :           t = lexer.peek_token ();
    9303                 :           0 :           while (t->get_id () == PLUS)
    9304                 :             :             {
    9305                 :           0 :               lexer.skip_token ();
    9306                 :             : 
    9307                 :             :               // attempt to parse typeparambound
    9308                 :           0 :               std::unique_ptr<AST::TypeParamBound> bound
    9309                 :             :                 = parse_type_param_bound ();
    9310                 :           0 :               if (bound == nullptr)
    9311                 :             :                 {
    9312                 :             :                   // not an error if null
    9313                 :             :                   break;
    9314                 :             :                 }
    9315                 :           0 :               bounds.push_back (std::move (bound));
    9316                 :             : 
    9317                 :           0 :               t = lexer.peek_token ();
    9318                 :             :             }
    9319                 :             : 
    9320                 :           0 :           return std::unique_ptr<AST::TraitObjectType> (
    9321                 :           0 :             new AST::TraitObjectType (std::move (bounds), left_delim_locus,
    9322                 :           0 :                                       false));
    9323                 :           0 :         }
    9324                 :             :       else
    9325                 :             :         {
    9326                 :             :           // release vector pointer
    9327                 :           0 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
    9328                 :             :           /* HACK: attempt to convert to trait bound. if fails, parenthesised
    9329                 :             :            * type */
    9330                 :           0 :           std::unique_ptr<AST::TraitBound> converted_bound (
    9331                 :           0 :             released_ptr->to_trait_bound (true));
    9332                 :           0 :           if (converted_bound == nullptr)
    9333                 :             :             {
    9334                 :             :               // parenthesised type
    9335                 :           0 :               return std::unique_ptr<AST::ParenthesisedType> (
    9336                 :           0 :                 new AST::ParenthesisedType (std::move (released_ptr),
    9337                 :           0 :                                             left_delim_locus));
    9338                 :             :             }
    9339                 :             :           else
    9340                 :             :             {
    9341                 :             :               // trait object type (one bound)
    9342                 :             : 
    9343                 :             :               // get value semantics trait bound
    9344                 :           0 :               AST::TraitBound value_bound (*converted_bound);
    9345                 :             : 
    9346                 :           0 :               return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    9347                 :           0 :                 new AST::TraitObjectTypeOneBound (value_bound,
    9348                 :           0 :                                                   left_delim_locus));
    9349                 :           0 :             }
    9350                 :           0 :         }
    9351                 :             :     }
    9352                 :             :   else
    9353                 :             :     {
    9354                 :         273 :       return std::unique_ptr<AST::TupleType> (
    9355                 :         273 :         new AST::TupleType (std::move (types), left_delim_locus));
    9356                 :             :     }
    9357                 :             :   /* TODO: ensure that this ensures that dynamic dispatch for traits is not
    9358                 :             :    * lost somehow */
    9359                 :         273 : }
    9360                 :             : 
    9361                 :             : /* Parses a type that has 'for' as its first character. This means it has a
    9362                 :             :  * "for lifetimes", so returns either a BareFunctionType, TraitObjectType, or
    9363                 :             :  * TraitObjectTypeOneBound depending on following characters. */
    9364                 :             : template <typename ManagedTokenSource>
    9365                 :             : std::unique_ptr<AST::Type>
    9366                 :           4 : Parser<ManagedTokenSource>::parse_for_prefixed_type ()
    9367                 :             : {
    9368                 :           4 :   location_t for_locus = lexer.peek_token ()->get_locus ();
    9369                 :             :   // parse for lifetimes in type
    9370                 :           4 :   std::vector<AST::LifetimeParam> for_lifetimes = parse_for_lifetimes ();
    9371                 :             : 
    9372                 :             :   // branch on next token - either function or a trait type
    9373                 :           4 :   const_TokenPtr t = lexer.peek_token ();
    9374                 :           4 :   switch (t->get_id ())
    9375                 :             :     {
    9376                 :           4 :     case ASYNC:
    9377                 :             :     case CONST:
    9378                 :             :     case UNSAFE:
    9379                 :             :     case EXTERN_KW:
    9380                 :             :     case FN_KW:
    9381                 :           4 :       return parse_bare_function_type (std::move (for_lifetimes));
    9382                 :           0 :     case SCOPE_RESOLUTION:
    9383                 :             :     case IDENTIFIER:
    9384                 :             :     case SUPER:
    9385                 :             :     case SELF:
    9386                 :             :     case SELF_ALIAS:
    9387                 :             :     case CRATE:
    9388                 :             :       case DOLLAR_SIGN: {
    9389                 :             :         // path, so trait type
    9390                 :             : 
    9391                 :             :         // parse type path to finish parsing trait bound
    9392                 :           0 :         AST::TypePath path = parse_type_path ();
    9393                 :             : 
    9394                 :           0 :         t = lexer.peek_token ();
    9395                 :           0 :         if (t->get_id () != PLUS)
    9396                 :             :           {
    9397                 :             :             // must be one-bound trait type
    9398                 :             :             // create trait bound value object
    9399                 :           0 :             AST::TraitBound bound (std::move (path), for_locus, false, false,
    9400                 :             :                                    std::move (for_lifetimes));
    9401                 :             : 
    9402                 :           0 :             return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    9403                 :           0 :               new AST::TraitObjectTypeOneBound (std::move (bound), for_locus));
    9404                 :           0 :           }
    9405                 :             : 
    9406                 :             :         /* more than one bound trait type (or at least parsed as it - could be
    9407                 :             :          * trailing '+') create trait bound pointer and bounds */
    9408                 :           0 :         std::unique_ptr<AST::TraitBound> initial_bound (
    9409                 :           0 :           new AST::TraitBound (std::move (path), for_locus, false, false,
    9410                 :             :                                std::move (for_lifetimes)));
    9411                 :           0 :         std::vector<std::unique_ptr<AST::TypeParamBound>> bounds;
    9412                 :           0 :         bounds.push_back (std::move (initial_bound));
    9413                 :             : 
    9414                 :           0 :         while (t->get_id () == PLUS)
    9415                 :             :           {
    9416                 :           0 :             lexer.skip_token ();
    9417                 :             : 
    9418                 :             :             // parse type param bound if it exists
    9419                 :           0 :             std::unique_ptr<AST::TypeParamBound> bound
    9420                 :             :               = parse_type_param_bound ();
    9421                 :           0 :             if (bound == nullptr)
    9422                 :             :               {
    9423                 :             :                 // not an error - e.g. trailing plus
    9424                 :           0 :                 return nullptr;
    9425                 :             :               }
    9426                 :           0 :             bounds.push_back (std::move (bound));
    9427                 :             : 
    9428                 :           0 :             t = lexer.peek_token ();
    9429                 :             :           }
    9430                 :             : 
    9431                 :           0 :         return std::unique_ptr<AST::TraitObjectType> (
    9432                 :           0 :           new AST::TraitObjectType (std::move (bounds), for_locus, false));
    9433                 :           0 :       }
    9434                 :           0 :     default:
    9435                 :             :       // error
    9436                 :           0 :       add_error (Error (t->get_locus (),
    9437                 :             :                         "unrecognised token %qs in bare function type or trait "
    9438                 :             :                         "object type or trait object type one bound",
    9439                 :             :                         t->get_token_description ()));
    9440                 :             : 
    9441                 :           0 :       return nullptr;
    9442                 :             :     }
    9443                 :           4 : }
    9444                 :             : 
    9445                 :             : // Parses a maybe named param used in bare function types.
    9446                 :             : template <typename ManagedTokenSource>
    9447                 :             : AST::MaybeNamedParam
    9448                 :          58 : Parser<ManagedTokenSource>::parse_maybe_named_param (AST::AttrVec outer_attrs)
    9449                 :             : {
    9450                 :             :   /* Basically guess that param is named if first token is identifier or
    9451                 :             :    * underscore and second token is semicolon. This should probably have no
    9452                 :             :    * exceptions. rustc uses backtracking to parse these, but at the time of
    9453                 :             :    * writing gccrs has no backtracking capabilities. */
    9454                 :          58 :   const_TokenPtr current = lexer.peek_token ();
    9455                 :          58 :   const_TokenPtr next = lexer.peek_token (1);
    9456                 :             : 
    9457                 :          58 :   Identifier name;
    9458                 :          58 :   AST::MaybeNamedParam::ParamKind kind = AST::MaybeNamedParam::UNNAMED;
    9459                 :             : 
    9460                 :          58 :   if (current->get_id () == IDENTIFIER && next->get_id () == COLON)
    9461                 :             :     {
    9462                 :             :       // named param
    9463                 :           2 :       name = {current};
    9464                 :           2 :       kind = AST::MaybeNamedParam::IDENTIFIER;
    9465                 :           2 :       lexer.skip_token (1);
    9466                 :             :     }
    9467                 :          56 :   else if (current->get_id () == UNDERSCORE && next->get_id () == COLON)
    9468                 :             :     {
    9469                 :             :       // wildcard param
    9470                 :          12 :       name = {Values::Keywords::UNDERSCORE, current->get_locus ()};
    9471                 :          12 :       kind = AST::MaybeNamedParam::WILDCARD;
    9472                 :          12 :       lexer.skip_token (1);
    9473                 :             :     }
    9474                 :             : 
    9475                 :             :   // parse type (required)
    9476                 :          58 :   std::unique_ptr<AST::Type> type = parse_type ();
    9477                 :          58 :   if (type == nullptr)
    9478                 :             :     {
    9479                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    9480                 :             :                    "failed to parse type in maybe named param");
    9481                 :           0 :       add_error (std::move (error));
    9482                 :             : 
    9483                 :           0 :       return AST::MaybeNamedParam::create_error ();
    9484                 :           0 :     }
    9485                 :             : 
    9486                 :         116 :   return AST::MaybeNamedParam (std::move (name), kind, std::move (type),
    9487                 :          58 :                                std::move (outer_attrs), current->get_locus ());
    9488                 :         116 : }
    9489                 :             : 
    9490                 :             : /* Parses a bare function type (with the given for lifetimes for convenience -
    9491                 :             :  * does not parse them itself). */
    9492                 :             : template <typename ManagedTokenSource>
    9493                 :             : std::unique_ptr<AST::BareFunctionType>
    9494                 :          56 : Parser<ManagedTokenSource>::parse_bare_function_type (
    9495                 :             :   std::vector<AST::LifetimeParam> for_lifetimes)
    9496                 :             : {
    9497                 :             :   // TODO: pass in for lifetime location as param
    9498                 :          56 :   location_t best_try_locus = lexer.peek_token ()->get_locus ();
    9499                 :             : 
    9500                 :          56 :   AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
    9501                 :             : 
    9502                 :          56 :   if (!skip_token (FN_KW))
    9503                 :           0 :     return nullptr;
    9504                 :             : 
    9505                 :          56 :   if (!skip_token (LEFT_PAREN))
    9506                 :           0 :     return nullptr;
    9507                 :             : 
    9508                 :             :   // parse function params, if they exist
    9509                 :          56 :   std::vector<AST::MaybeNamedParam> params;
    9510                 :          56 :   bool is_variadic = false;
    9511                 :          56 :   AST::AttrVec variadic_attrs;
    9512                 :             : 
    9513                 :          56 :   const_TokenPtr t = lexer.peek_token ();
    9514                 :         122 :   while (t->get_id () != RIGHT_PAREN)
    9515                 :             :     {
    9516                 :          58 :       AST::AttrVec temp_attrs = parse_outer_attributes ();
    9517                 :             : 
    9518                 :         116 :       if (lexer.peek_token ()->get_id () == ELLIPSIS)
    9519                 :             :         {
    9520                 :           0 :           lexer.skip_token ();
    9521                 :           0 :           is_variadic = true;
    9522                 :           0 :           variadic_attrs = std::move (temp_attrs);
    9523                 :             : 
    9524                 :           0 :           t = lexer.peek_token ();
    9525                 :             : 
    9526                 :           0 :           if (t->get_id () != RIGHT_PAREN)
    9527                 :             :             {
    9528                 :           0 :               Error error (t->get_locus (),
    9529                 :             :                            "expected right parentheses after variadic in maybe "
    9530                 :             :                            "named function "
    9531                 :             :                            "parameters, found %qs",
    9532                 :             :                            t->get_token_description ());
    9533                 :           0 :               add_error (std::move (error));
    9534                 :             : 
    9535                 :           0 :               return nullptr;
    9536                 :           0 :             }
    9537                 :             : 
    9538                 :             :           break;
    9539                 :             :         }
    9540                 :             : 
    9541                 :          58 :       AST::MaybeNamedParam param
    9542                 :          58 :         = parse_maybe_named_param (std::move (temp_attrs));
    9543                 :          58 :       if (param.is_error ())
    9544                 :             :         {
    9545                 :           0 :           Error error (
    9546                 :           0 :             lexer.peek_token ()->get_locus (),
    9547                 :             :             "failed to parse maybe named param in bare function type");
    9548                 :           0 :           add_error (std::move (error));
    9549                 :             : 
    9550                 :           0 :           return nullptr;
    9551                 :           0 :         }
    9552                 :          58 :       params.push_back (std::move (param));
    9553                 :             : 
    9554                 :         116 :       if (lexer.peek_token ()->get_id () != COMMA)
    9555                 :             :         break;
    9556                 :             : 
    9557                 :           8 :       lexer.skip_token ();
    9558                 :           8 :       t = lexer.peek_token ();
    9559                 :             :     }
    9560                 :             : 
    9561                 :          56 :   if (!skip_token (RIGHT_PAREN))
    9562                 :           0 :     return nullptr;
    9563                 :             : 
    9564                 :             :   // bare function return type, if exists
    9565                 :          56 :   std::unique_ptr<AST::TypeNoBounds> return_type = nullptr;
    9566                 :         112 :   if (lexer.peek_token ()->get_id () == RETURN_TYPE)
    9567                 :             :     {
    9568                 :          40 :       lexer.skip_token ();
    9569                 :             : 
    9570                 :             :       // parse required TypeNoBounds
    9571                 :          40 :       return_type = parse_type_no_bounds ();
    9572                 :          40 :       if (return_type == nullptr)
    9573                 :             :         {
    9574                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    9575                 :             :                        "failed to parse return type (type no bounds) in bare "
    9576                 :             :                        "function type");
    9577                 :           0 :           add_error (std::move (error));
    9578                 :             : 
    9579                 :           0 :           return nullptr;
    9580                 :           0 :         }
    9581                 :             :     }
    9582                 :             : 
    9583                 :             :   return std::unique_ptr<AST::BareFunctionType> (
    9584                 :          56 :     new AST::BareFunctionType (std::move (for_lifetimes),
    9585                 :             :                                std::move (qualifiers), std::move (params),
    9586                 :             :                                is_variadic, std::move (variadic_attrs),
    9587                 :          56 :                                std::move (return_type), best_try_locus));
    9588                 :         112 : }
    9589                 :             : 
    9590                 :             : template <typename ManagedTokenSource>
    9591                 :             : std::unique_ptr<AST::ReferenceType>
    9592                 :        2427 : Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
    9593                 :             : {
    9594                 :             :   // parse optional lifetime
    9595                 :        2427 :   AST::Lifetime lifetime = AST::Lifetime::elided ();
    9596                 :        4854 :   if (lexer.peek_token ()->get_id () == LIFETIME)
    9597                 :             :     {
    9598                 :         575 :       lifetime = parse_lifetime (true);
    9599                 :        2427 :       if (lifetime.is_error ())
    9600                 :             :         {
    9601                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
    9602                 :             :                        "failed to parse lifetime in reference type");
    9603                 :           0 :           add_error (std::move (error));
    9604                 :             : 
    9605                 :           0 :           return nullptr;
    9606                 :           0 :         }
    9607                 :             :     }
    9608                 :             : 
    9609                 :        2427 :   bool is_mut = false;
    9610                 :        4854 :   if (lexer.peek_token ()->get_id () == MUT)
    9611                 :             :     {
    9612                 :         245 :       lexer.skip_token ();
    9613                 :         245 :       is_mut = true;
    9614                 :             :     }
    9615                 :             : 
    9616                 :             :   // parse type no bounds, which is required
    9617                 :        2427 :   std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
    9618                 :        2427 :   if (type == nullptr)
    9619                 :             :     {
    9620                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    9621                 :             :                    "failed to parse referenced type in reference type");
    9622                 :           0 :       add_error (std::move (error));
    9623                 :             : 
    9624                 :           0 :       return nullptr;
    9625                 :           0 :     }
    9626                 :             : 
    9627                 :             :   return std::unique_ptr<AST::ReferenceType> (
    9628                 :        4854 :     new AST::ReferenceType (is_mut, std::move (type), locus,
    9629                 :        2427 :                             std::move (lifetime)));
    9630                 :        2427 : }
    9631                 :             : 
    9632                 :             : // Parses a reference type (mutable or immutable, with given lifetime).
    9633                 :             : template <typename ManagedTokenSource>
    9634                 :             : std::unique_ptr<AST::ReferenceType>
    9635                 :        2427 : Parser<ManagedTokenSource>::parse_reference_type ()
    9636                 :             : {
    9637                 :        2427 :   auto t = lexer.peek_token ();
    9638                 :        2427 :   auto locus = t->get_locus ();
    9639                 :             : 
    9640                 :        2427 :   switch (t->get_id ())
    9641                 :             :     {
    9642                 :        2406 :     case AMP:
    9643                 :        2406 :       skip_token (AMP);
    9644                 :        2406 :       return parse_reference_type_inner (locus);
    9645                 :          21 :     case LOGICAL_AND:
    9646                 :          21 :       skip_token (LOGICAL_AND);
    9647                 :             :       return std::unique_ptr<AST::ReferenceType> (
    9648                 :          42 :         new AST::ReferenceType (false, parse_reference_type_inner (locus),
    9649                 :          21 :                                 locus));
    9650                 :           0 :     default:
    9651                 :           0 :       rust_unreachable ();
    9652                 :             :     }
    9653                 :        2427 : }
    9654                 :             : 
    9655                 :             : // Parses a raw (unsafe) pointer type.
    9656                 :             : template <typename ManagedTokenSource>
    9657                 :             : std::unique_ptr<AST::RawPointerType>
    9658                 :        5243 : Parser<ManagedTokenSource>::parse_raw_pointer_type ()
    9659                 :             : {
    9660                 :        5243 :   location_t locus = lexer.peek_token ()->get_locus ();
    9661                 :        5243 :   skip_token (ASTERISK);
    9662                 :             : 
    9663                 :        5243 :   AST::RawPointerType::PointerType kind = AST::RawPointerType::CONST;
    9664                 :             : 
    9665                 :             :   // branch on next token for pointer kind info
    9666                 :        5243 :   const_TokenPtr t = lexer.peek_token ();
    9667                 :        5243 :   switch (t->get_id ())
    9668                 :             :     {
    9669                 :         467 :     case MUT:
    9670                 :         467 :       kind = AST::RawPointerType::MUT;
    9671                 :         467 :       lexer.skip_token ();
    9672                 :             :       break;
    9673                 :        4776 :     case CONST:
    9674                 :        4776 :       kind = AST::RawPointerType::CONST;
    9675                 :        4776 :       lexer.skip_token ();
    9676                 :             :       break;
    9677                 :           0 :     default:
    9678                 :           0 :       add_error (Error (t->get_locus (),
    9679                 :             :                         "unrecognised token %qs in raw pointer type",
    9680                 :             :                         t->get_token_description ()));
    9681                 :             : 
    9682                 :           0 :       return nullptr;
    9683                 :             :     }
    9684                 :             : 
    9685                 :             :   // parse type no bounds (required)
    9686                 :        5243 :   std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
    9687                 :        5243 :   if (type == nullptr)
    9688                 :             :     {
    9689                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    9690                 :             :                    "failed to parse pointed type of raw pointer type");
    9691                 :           0 :       add_error (std::move (error));
    9692                 :             : 
    9693                 :           0 :       return nullptr;
    9694                 :           0 :     }
    9695                 :             : 
    9696                 :             :   return std::unique_ptr<AST::RawPointerType> (
    9697                 :        5243 :     new AST::RawPointerType (kind, std::move (type), locus));
    9698                 :        5243 : }
    9699                 :             : 
    9700                 :             : /* Parses a slice or array type, depending on following arguments (as
    9701                 :             :  * lookahead is not possible). */
    9702                 :             : template <typename ManagedTokenSource>
    9703                 :             : std::unique_ptr<AST::TypeNoBounds>
    9704                 :        1232 : Parser<ManagedTokenSource>::parse_slice_or_array_type ()
    9705                 :             : {
    9706                 :        1232 :   location_t locus = lexer.peek_token ()->get_locus ();
    9707                 :        1232 :   skip_token (LEFT_SQUARE);
    9708                 :             : 
    9709                 :             :   // parse inner type (required)
    9710                 :        1232 :   std::unique_ptr<AST::Type> inner_type = parse_type ();
    9711                 :        1232 :   if (inner_type == nullptr)
    9712                 :             :     {
    9713                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
    9714                 :             :                    "failed to parse inner type in slice or array type");
    9715                 :           0 :       add_error (std::move (error));
    9716                 :             : 
    9717                 :           0 :       return nullptr;
    9718                 :           0 :     }
    9719                 :             : 
    9720                 :             :   // branch on next token
    9721                 :        1232 :   const_TokenPtr t = lexer.peek_token ();
    9722                 :        1232 :   switch (t->get_id ())
    9723                 :             :     {
    9724                 :         793 :     case RIGHT_SQUARE:
    9725                 :             :       // slice type
    9726                 :         793 :       lexer.skip_token ();
    9727                 :             : 
    9728                 :         793 :       return std::unique_ptr<AST::SliceType> (
    9729                 :         793 :         new AST::SliceType (std::move (inner_type), locus));
    9730                 :         439 :       case SEMICOLON: {
    9731                 :             :         // array type
    9732                 :         439 :         lexer.skip_token ();
    9733                 :             : 
    9734                 :             :         // parse required array size expression
    9735                 :         439 :         std::unique_ptr<AST::Expr> size = parse_expr ();
    9736                 :         439 :         if (size == nullptr)
    9737                 :             :           {
    9738                 :           0 :             Error error (lexer.peek_token ()->get_locus (),
    9739                 :             :                          "failed to parse size expression in array type");
    9740                 :           0 :             add_error (std::move (error));
    9741                 :             : 
    9742                 :           0 :             return nullptr;
    9743                 :           0 :           }
    9744                 :             : 
    9745                 :         439 :         if (!skip_token (RIGHT_SQUARE))
    9746                 :             :           {
    9747                 :           0 :             return nullptr;
    9748                 :             :           }
    9749                 :             : 
    9750                 :         439 :         return std::unique_ptr<AST::ArrayType> (
    9751                 :         439 :           new AST::ArrayType (std::move (inner_type), std::move (size), locus));
    9752                 :         439 :       }
    9753                 :           0 :     default:
    9754                 :             :       // error
    9755                 :           0 :       add_error (
    9756                 :           0 :         Error (t->get_locus (),
    9757                 :             :                "unrecognised token %qs in slice or array type after inner type",
    9758                 :             :                t->get_token_description ()));
    9759                 :             : 
    9760                 :           0 :       return nullptr;
    9761                 :             :     }
    9762                 :        1232 : }
    9763                 :             : 
    9764                 :             : // Parses a type, taking into account type boundary disambiguation.
    9765                 :             : template <typename ManagedTokenSource>
    9766                 :             : std::unique_ptr<AST::TypeNoBounds>
    9767                 :       11453 : Parser<ManagedTokenSource>::parse_type_no_bounds ()
    9768                 :             : {
    9769                 :       11453 :   const_TokenPtr t = lexer.peek_token ();
    9770                 :       11453 :   switch (t->get_id ())
    9771                 :             :     {
    9772                 :           0 :     case EXCLAM:
    9773                 :             :       // never type - can't be macro as no path beforehand
    9774                 :           0 :       lexer.skip_token ();
    9775                 :           0 :       return std::unique_ptr<AST::NeverType> (
    9776                 :           0 :         new AST::NeverType (t->get_locus ()));
    9777                 :         610 :     case LEFT_SQUARE:
    9778                 :             :       // slice type or array type - requires further disambiguation
    9779                 :         610 :       return parse_slice_or_array_type ();
    9780                 :          21 :     case LEFT_SHIFT:
    9781                 :             :       case LEFT_ANGLE: {
    9782                 :             :         // qualified path in type
    9783                 :          21 :         AST::QualifiedPathInType path = parse_qualified_path_in_type ();
    9784                 :          21 :         if (path.is_error ())
    9785                 :             :           {
    9786                 :           0 :             Error error (t->get_locus (),
    9787                 :             :                          "failed to parse qualified path in type");
    9788                 :           0 :             add_error (std::move (error));
    9789                 :             : 
    9790                 :           0 :             return nullptr;
    9791                 :           0 :           }
    9792                 :          21 :         return std::unique_ptr<AST::QualifiedPathInType> (
    9793                 :          21 :           new AST::QualifiedPathInType (std::move (path)));
    9794                 :          21 :       }
    9795                 :          46 :     case UNDERSCORE:
    9796                 :             :       // inferred type
    9797                 :          46 :       lexer.skip_token ();
    9798                 :          46 :       return std::unique_ptr<AST::InferredType> (
    9799                 :          46 :         new AST::InferredType (t->get_locus ()));
    9800                 :        2984 :     case ASTERISK:
    9801                 :             :       // raw pointer type
    9802                 :        2984 :       return parse_raw_pointer_type ();
    9803                 :          32 :     case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND?
    9804                 :             :     case LOGICAL_AND:
    9805                 :             :       // reference type
    9806                 :          32 :       return parse_reference_type ();
    9807                 :           0 :     case LIFETIME:
    9808                 :             :       /* probably a lifetime bound, so probably type param bounds in
    9809                 :             :        * TraitObjectType. this is not allowed, but detection here for error
    9810                 :             :        * message */
    9811                 :           0 :       add_error (Error (t->get_locus (),
    9812                 :             :                         "lifetime bounds (i.e. in type param bounds, in "
    9813                 :             :                         "TraitObjectType) are not allowed as TypeNoBounds"));
    9814                 :             : 
    9815                 :           0 :       return nullptr;
    9816                 :        7608 :     case IDENTIFIER:
    9817                 :             :     case SUPER:
    9818                 :             :     case SELF:
    9819                 :             :     case SELF_ALIAS:
    9820                 :             :     case CRATE:
    9821                 :             :     case DOLLAR_SIGN:
    9822                 :             :       case SCOPE_RESOLUTION: {
    9823                 :             :         // macro invocation or type path - requires further disambiguation.
    9824                 :             :         /* for parsing path component of each rule, perhaps parse it as a
    9825                 :             :          * typepath and attempt conversion to simplepath if a trailing '!' is
    9826                 :             :          * found */
    9827                 :             :         /* Type path also includes TraitObjectTypeOneBound BUT if it starts
    9828                 :             :          * with it, it is exactly the same as a TypePath syntactically, so
    9829                 :             :          * this is a syntactical ambiguity. As such, the parser will parse it
    9830                 :             :          * as a TypePath. This, however, does not prevent TraitObjectType from
    9831                 :             :          * starting with a typepath. */
    9832                 :             : 
    9833                 :             :         // parse path as type path
    9834                 :        7608 :         AST::TypePath path = parse_type_path ();
    9835                 :        7608 :         if (path.is_error ())
    9836                 :             :           {
    9837                 :           0 :             Error error (
    9838                 :             :               t->get_locus (),
    9839                 :             :               "failed to parse path as first component of type no bounds");
    9840                 :           0 :             add_error (std::move (error));
    9841                 :             : 
    9842                 :           0 :             return nullptr;
    9843                 :           0 :           }
    9844                 :        7608 :         location_t locus = path.get_locus ();
    9845                 :             : 
    9846                 :             :         // branch on next token
    9847                 :        7608 :         t = lexer.peek_token ();
    9848                 :        7608 :         switch (t->get_id ())
    9849                 :             :           {
    9850                 :           2 :             case EXCLAM: {
    9851                 :             :               // macro invocation
    9852                 :             :               // convert to simple path
    9853                 :           2 :               AST::SimplePath macro_path = path.as_simple_path ();
    9854                 :           2 :               if (macro_path.is_empty ())
    9855                 :             :                 {
    9856                 :           0 :                   Error error (t->get_locus (),
    9857                 :             :                                "failed to parse simple path in macro "
    9858                 :             :                                "invocation (for type)");
    9859                 :           0 :                   add_error (std::move (error));
    9860                 :             : 
    9861                 :           0 :                   return nullptr;
    9862                 :           0 :                 }
    9863                 :             : 
    9864                 :           2 :               lexer.skip_token ();
    9865                 :             : 
    9866                 :           2 :               AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
    9867                 :             : 
    9868                 :           4 :               return AST::MacroInvocation::Regular (
    9869                 :           4 :                 AST::MacroInvocData (std::move (macro_path),
    9870                 :             :                                      std::move (tok_tree)),
    9871                 :           2 :                 {}, locus);
    9872                 :           4 :             }
    9873                 :        7606 :           default:
    9874                 :             :             // assume that this is a type path and not an error
    9875                 :        7606 :             return std::unique_ptr<AST::TypePath> (
    9876                 :        7606 :               new AST::TypePath (std::move (path)));
    9877                 :             :           }
    9878                 :        7608 :       }
    9879                 :          17 :     case LEFT_PAREN:
    9880                 :             :       /* tuple type or parenthesised type - requires further disambiguation
    9881                 :             :        * (the usual). ok apparently can be a parenthesised TraitBound too, so
    9882                 :             :        * could be TraitObjectTypeOneBound */
    9883                 :          17 :       return parse_paren_prefixed_type_no_bounds ();
    9884                 :           2 :     case FOR:
    9885                 :             :     case ASYNC:
    9886                 :             :     case CONST:
    9887                 :             :     case UNSAFE:
    9888                 :             :     case EXTERN_KW:
    9889                 :             :     case FN_KW:
    9890                 :             :       // bare function type (with no for lifetimes)
    9891                 :           2 :       return parse_bare_function_type (std::vector<AST::LifetimeParam> ());
    9892                 :           0 :     case IMPL:
    9893                 :           0 :       lexer.skip_token ();
    9894                 :           0 :       if (lexer.peek_token ()->get_id () == LIFETIME)
    9895                 :             :         {
    9896                 :             :           /* cannot be one bound because lifetime prevents it from being
    9897                 :             :            * traitbound not allowed as type no bounds, only here for error
    9898                 :             :            * message */
    9899                 :           0 :           Error error (
    9900                 :           0 :             lexer.peek_token ()->get_locus (),
    9901                 :             :             "lifetime (probably lifetime bound, in type param "
    9902                 :             :             "bounds, in ImplTraitType) is not allowed in TypeNoBounds");
    9903                 :           0 :           add_error (std::move (error));
    9904                 :             : 
    9905                 :           0 :           return nullptr;
    9906                 :           0 :         }
    9907                 :             :       else
    9908                 :             :         {
    9909                 :             :           // should be trait bound, so parse trait bound
    9910                 :           0 :           std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    9911                 :           0 :           if (initial_bound == nullptr)
    9912                 :             :             {
    9913                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
    9914                 :             :                            "failed to parse ImplTraitTypeOneBound bound");
    9915                 :           0 :               add_error (std::move (error));
    9916                 :             : 
    9917                 :           0 :               return nullptr;
    9918                 :           0 :             }
    9919                 :             : 
    9920                 :           0 :           location_t locus = t->get_locus ();
    9921                 :             : 
    9922                 :             :           // ensure not a trait with multiple bounds
    9923                 :           0 :           t = lexer.peek_token ();
    9924                 :           0 :           if (t->get_id () == PLUS)
    9925                 :             :             {
    9926                 :           0 :               Error error (t->get_locus (),
    9927                 :             :                            "plus after trait bound means an ImplTraitType, "
    9928                 :             :                            "which is not allowed as a TypeNoBounds");
    9929                 :           0 :               add_error (std::move (error));
    9930                 :             : 
    9931                 :           0 :               return nullptr;
    9932                 :           0 :             }
    9933                 :             : 
    9934                 :             :           // convert trait bound to value object
    9935                 :           0 :           AST::TraitBound value_bound (*initial_bound);
    9936                 :             : 
    9937                 :           0 :           return std::unique_ptr<AST::ImplTraitTypeOneBound> (
    9938                 :           0 :             new AST::ImplTraitTypeOneBound (std::move (value_bound), locus));
    9939                 :           0 :         }
    9940                 :         133 :     case DYN:
    9941                 :             :       case QUESTION_MARK: {
    9942                 :             :         // either TraitObjectTypeOneBound
    9943                 :         133 :         bool has_dyn = false;
    9944                 :         133 :         if (t->get_id () == DYN)
    9945                 :             :           {
    9946                 :         133 :             lexer.skip_token ();
    9947                 :         133 :             has_dyn = true;
    9948                 :             :           }
    9949                 :             : 
    9950                 :         266 :         if (lexer.peek_token ()->get_id () == LIFETIME)
    9951                 :             :           {
    9952                 :             :             /* means that cannot be TraitObjectTypeOneBound - so here for
    9953                 :             :              * error message */
    9954                 :           0 :             Error error (lexer.peek_token ()->get_locus (),
    9955                 :             :                          "lifetime as bound in TraitObjectTypeOneBound "
    9956                 :             :                          "is not allowed, so cannot be TypeNoBounds");
    9957                 :           0 :             add_error (std::move (error));
    9958                 :             : 
    9959                 :           0 :             return nullptr;
    9960                 :           0 :           }
    9961                 :             : 
    9962                 :             :         // should be trait bound, so parse trait bound
    9963                 :         133 :         std::unique_ptr<AST::TraitBound> initial_bound = parse_trait_bound ();
    9964                 :         133 :         if (initial_bound == nullptr)
    9965                 :             :           {
    9966                 :           0 :             Error error (
    9967                 :           0 :               lexer.peek_token ()->get_locus (),
    9968                 :             :               "failed to parse TraitObjectTypeOneBound initial bound");
    9969                 :           0 :             add_error (std::move (error));
    9970                 :             : 
    9971                 :           0 :             return nullptr;
    9972                 :           0 :           }
    9973                 :             : 
    9974                 :         133 :         location_t locus = t->get_locus ();
    9975                 :             : 
    9976                 :             :         // detect error with plus as next token
    9977                 :         133 :         t = lexer.peek_token ();
    9978                 :         133 :         if (t->get_id () == PLUS)
    9979                 :             :           {
    9980                 :           0 :             Error error (t->get_locus (),
    9981                 :             :                          "plus after trait bound means a TraitObjectType, "
    9982                 :             :                          "which is not allowed as a TypeNoBounds");
    9983                 :           0 :             add_error (std::move (error));
    9984                 :             : 
    9985                 :           0 :             return nullptr;
    9986                 :           0 :           }
    9987                 :             : 
    9988                 :             :         // convert trait bound to value object
    9989                 :         133 :         AST::TraitBound value_bound (*initial_bound);
    9990                 :             : 
    9991                 :         133 :         return std::unique_ptr<AST::TraitObjectTypeOneBound> (
    9992                 :         266 :           new AST::TraitObjectTypeOneBound (std::move (value_bound), locus,
    9993                 :         133 :                                             has_dyn));
    9994                 :         133 :       }
    9995                 :           0 :     default:
    9996                 :           0 :       add_error (Error (t->get_locus (),
    9997                 :             :                         "unrecognised token %qs in type no bounds",
    9998                 :             :                         t->get_token_description ()));
    9999                 :             : 
   10000                 :           0 :       return nullptr;
   10001                 :             :     }
   10002                 :       11453 : }
   10003                 :             : 
   10004                 :             : // Parses a type no bounds beginning with '('.
   10005                 :             : template <typename ManagedTokenSource>
   10006                 :             : std::unique_ptr<AST::TypeNoBounds>
   10007                 :          17 : Parser<ManagedTokenSource>::parse_paren_prefixed_type_no_bounds ()
   10008                 :             : {
   10009                 :             :   /* NOTE: this could probably be parsed without the HACK solution of
   10010                 :             :    * parse_paren_prefixed_type, but I was lazy. So FIXME for future.*/
   10011                 :             : 
   10012                 :             :   /* NOTE: again, syntactical ambiguity of a parenthesised trait bound is
   10013                 :             :    * considered a trait bound, not a parenthesised type, so that it can still
   10014                 :             :    * be used in type param bounds. */
   10015                 :             : 
   10016                 :          17 :   location_t left_paren_locus = lexer.peek_token ()->get_locus ();
   10017                 :             : 
   10018                 :             :   // skip left delim
   10019                 :          17 :   lexer.skip_token ();
   10020                 :             :   /* while next token isn't close delim, parse comma-separated types, saving
   10021                 :             :    * whether trailing comma happens */
   10022                 :          17 :   const_TokenPtr t = lexer.peek_token ();
   10023                 :          17 :   bool trailing_comma = true;
   10024                 :          17 :   std::vector<std::unique_ptr<AST::Type>> types;
   10025                 :             : 
   10026                 :          17 :   while (t->get_id () != RIGHT_PAREN)
   10027                 :             :     {
   10028                 :           6 :       std::unique_ptr<AST::Type> type = parse_type ();
   10029                 :           6 :       if (type == nullptr)
   10030                 :             :         {
   10031                 :           0 :           Error error (t->get_locus (),
   10032                 :             :                        "failed to parse type inside parentheses (probably "
   10033                 :             :                        "tuple or parenthesised)");
   10034                 :           0 :           add_error (std::move (error));
   10035                 :             : 
   10036                 :           0 :           return nullptr;
   10037                 :           0 :         }
   10038                 :           6 :       types.push_back (std::move (type));
   10039                 :             : 
   10040                 :           6 :       t = lexer.peek_token ();
   10041                 :           6 :       if (t->get_id () != COMMA)
   10042                 :             :         {
   10043                 :           6 :           trailing_comma = false;
   10044                 :             :           break;
   10045                 :             :         }
   10046                 :           0 :       lexer.skip_token ();
   10047                 :             : 
   10048                 :           0 :       t = lexer.peek_token ();
   10049                 :             :     }
   10050                 :             : 
   10051                 :          17 :   if (!skip_token (RIGHT_PAREN))
   10052                 :             :     {
   10053                 :           0 :       return nullptr;
   10054                 :             :     }
   10055                 :             : 
   10056                 :             :   // if only one type and no trailing comma, then not a tuple type
   10057                 :          17 :   if (types.size () == 1 && !trailing_comma)
   10058                 :             :     {
   10059                 :             :       // must be a TraitObjectType (with more than one bound)
   10060                 :          12 :       if (lexer.peek_token ()->get_id () == PLUS)
   10061                 :             :         {
   10062                 :             :           // error - this is not allowed for type no bounds
   10063                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
   10064                 :             :                        "plus (implying TraitObjectType as type param "
   10065                 :             :                        "bounds) is not allowed in type no bounds");
   10066                 :           0 :           add_error (std::move (error));
   10067                 :             : 
   10068                 :           0 :           return nullptr;
   10069                 :           0 :         }
   10070                 :             :       else
   10071                 :             :         {
   10072                 :             :           // release vector pointer
   10073                 :           6 :           std::unique_ptr<AST::Type> released_ptr = std::move (types[0]);
   10074                 :             :           /* HACK: attempt to convert to trait bound. if fails, parenthesised
   10075                 :             :            * type */
   10076                 :           6 :           std::unique_ptr<AST::TraitBound> converted_bound (
   10077                 :           6 :             released_ptr->to_trait_bound (true));
   10078                 :           6 :           if (converted_bound == nullptr)
   10079                 :             :             {
   10080                 :             :               // parenthesised type
   10081                 :           6 :               return std::unique_ptr<AST::ParenthesisedType> (
   10082                 :           6 :                 new AST::ParenthesisedType (std::move (released_ptr),
   10083                 :           6 :                                             left_paren_locus));
   10084                 :             :             }
   10085                 :             :           else
   10086                 :             :             {
   10087                 :             :               // trait object type (one bound)
   10088                 :             : 
   10089                 :             :               // get value semantics trait bound
   10090                 :           0 :               AST::TraitBound value_bound (*converted_bound);
   10091                 :             : 
   10092                 :           0 :               return std::unique_ptr<AST::TraitObjectTypeOneBound> (
   10093                 :           0 :                 new AST::TraitObjectTypeOneBound (value_bound,
   10094                 :           0 :                                                   left_paren_locus));
   10095                 :           0 :             }
   10096                 :           6 :         }
   10097                 :             :     }
   10098                 :             :   else
   10099                 :             :     {
   10100                 :          11 :       return std::unique_ptr<AST::TupleType> (
   10101                 :          11 :         new AST::TupleType (std::move (types), left_paren_locus));
   10102                 :             :     }
   10103                 :             :   /* TODO: ensure that this ensures that dynamic dispatch for traits is not
   10104                 :             :    * lost somehow */
   10105                 :          17 : }
   10106                 :             : 
   10107                 :             : /* Parses a literal pattern or range pattern. Assumes that literals passed in
   10108                 :             :  * are valid range pattern bounds. Do not pass in paths in expressions, for
   10109                 :             :  * instance. */
   10110                 :             : template <typename ManagedTokenSource>
   10111                 :             : std::unique_ptr<AST::Pattern>
   10112                 :         190 : Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
   10113                 :             : {
   10114                 :         190 :   const_TokenPtr range_lower = lexer.peek_token ();
   10115                 :         190 :   AST::Literal::LitType type = AST::Literal::STRING;
   10116                 :         190 :   bool has_minus = false;
   10117                 :             : 
   10118                 :             :   // get lit type
   10119                 :         190 :   switch (range_lower->get_id ())
   10120                 :             :     {
   10121                 :          28 :     case CHAR_LITERAL:
   10122                 :          28 :       type = AST::Literal::CHAR;
   10123                 :          28 :       lexer.skip_token ();
   10124                 :             :       break;
   10125                 :          21 :     case BYTE_CHAR_LITERAL:
   10126                 :          21 :       type = AST::Literal::BYTE;
   10127                 :          21 :       lexer.skip_token ();
   10128                 :             :       break;
   10129                 :         139 :     case INT_LITERAL:
   10130                 :         139 :       type = AST::Literal::INT;
   10131                 :         139 :       lexer.skip_token ();
   10132                 :             :       break;
   10133                 :           2 :     case FLOAT_LITERAL:
   10134                 :           2 :       type = AST::Literal::FLOAT;
   10135                 :           2 :       lexer.skip_token ();
   10136                 :             :       break;
   10137                 :           0 :     case MINUS:
   10138                 :             :       // branch on next token
   10139                 :           0 :       range_lower = lexer.peek_token (1);
   10140                 :           0 :       switch (range_lower->get_id ())
   10141                 :             :         {
   10142                 :           0 :         case INT_LITERAL:
   10143                 :           0 :           type = AST::Literal::INT;
   10144                 :           0 :           has_minus = true;
   10145                 :           0 :           lexer.skip_token (1);
   10146                 :           0 :           break;
   10147                 :           0 :         case FLOAT_LITERAL:
   10148                 :           0 :           type = AST::Literal::FLOAT;
   10149                 :           0 :           has_minus = true;
   10150                 :           0 :           lexer.skip_token (1);
   10151                 :           0 :           break;
   10152                 :           0 :         default:
   10153                 :           0 :           add_error (Error (range_lower->get_locus (),
   10154                 :             :                             "token type %qs cannot be parsed as range pattern "
   10155                 :             :                             "bound or literal after minus symbol",
   10156                 :             :                             range_lower->get_token_description ()));
   10157                 :             : 
   10158                 :           0 :           return nullptr;
   10159                 :             :         }
   10160                 :             :       break;
   10161                 :           0 :     default:
   10162                 :           0 :       add_error (
   10163                 :           0 :         Error (range_lower->get_locus (),
   10164                 :             :                "token type %qs cannot be parsed as range pattern bound",
   10165                 :             :                range_lower->get_token_description ()));
   10166                 :             : 
   10167                 :           0 :       return nullptr;
   10168                 :             :     }
   10169                 :             : 
   10170                 :         190 :   const_TokenPtr next = lexer.peek_token ();
   10171                 :         190 :   if (next->get_id () == DOT_DOT_EQ || next->get_id () == ELLIPSIS
   10172                 :         366 :       || next->get_id () == DOT_DOT)
   10173                 :             :     {
   10174                 :          22 :       AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
   10175                 :             :       // range pattern
   10176                 :          22 :       lexer.skip_token ();
   10177                 :          22 :       std::unique_ptr<AST::RangePatternBound> lower (
   10178                 :          44 :         new AST::RangePatternBoundLiteral (
   10179                 :          44 :           AST::Literal (range_lower->get_str (), type,
   10180                 :             :                         PrimitiveCoreType::CORETYPE_UNKNOWN),
   10181                 :             :           range_lower->get_locus (), has_minus));
   10182                 :             : 
   10183                 :          22 :       std::unique_ptr<AST::RangePatternBound> upper
   10184                 :             :         = parse_range_pattern_bound ();
   10185                 :          22 :       if (upper == nullptr)
   10186                 :             :         {
   10187                 :           0 :           Error error (next->get_locus (),
   10188                 :             :                        "failed to parse range pattern bound in range pattern");
   10189                 :           0 :           add_error (std::move (error));
   10190                 :             : 
   10191                 :           0 :           return nullptr;
   10192                 :           0 :         }
   10193                 :             : 
   10194                 :          22 :       return std::unique_ptr<AST::RangePattern> (
   10195                 :          22 :         new AST::RangePattern (std::move (lower), std::move (upper), kind,
   10196                 :          22 :                                range_lower->get_locus ()));
   10197                 :          22 :     }
   10198                 :             :   else
   10199                 :             :     {
   10200                 :             :       // literal pattern
   10201                 :         168 :       return std::unique_ptr<AST::LiteralPattern> (
   10202                 :         380 :         new AST::LiteralPattern (range_lower->get_str (), type,
   10203                 :             :                                  range_lower->get_locus (),
   10204                 :         168 :                                  range_lower->get_type_hint ()));
   10205                 :             :     }
   10206                 :         190 : }
   10207                 :             : 
   10208                 :             : // Parses a range pattern bound (value only).
   10209                 :             : template <typename ManagedTokenSource>
   10210                 :             : std::unique_ptr<AST::RangePatternBound>
   10211                 :          31 : Parser<ManagedTokenSource>::parse_range_pattern_bound ()
   10212                 :             : {
   10213                 :          31 :   const_TokenPtr range_lower = lexer.peek_token ();
   10214                 :          31 :   location_t range_lower_locus = range_lower->get_locus ();
   10215                 :             : 
   10216                 :             :   // get lit type
   10217                 :          31 :   switch (range_lower->get_id ())
   10218                 :             :     {
   10219                 :           7 :     case CHAR_LITERAL:
   10220                 :           7 :       lexer.skip_token ();
   10221                 :           7 :       return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10222                 :          14 :         new AST::RangePatternBoundLiteral (
   10223                 :          21 :           AST::Literal (range_lower->get_str (), AST::Literal::CHAR,
   10224                 :             :                         range_lower->get_type_hint ()),
   10225                 :           7 :           range_lower_locus));
   10226                 :           0 :     case BYTE_CHAR_LITERAL:
   10227                 :           0 :       lexer.skip_token ();
   10228                 :           0 :       return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10229                 :           0 :         new AST::RangePatternBoundLiteral (
   10230                 :           0 :           AST::Literal (range_lower->get_str (), AST::Literal::BYTE,
   10231                 :             :                         range_lower->get_type_hint ()),
   10232                 :           0 :           range_lower_locus));
   10233                 :           8 :     case INT_LITERAL:
   10234                 :           8 :       lexer.skip_token ();
   10235                 :           8 :       return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10236                 :          16 :         new AST::RangePatternBoundLiteral (
   10237                 :          16 :           AST::Literal (range_lower->get_str (), AST::Literal::INT,
   10238                 :             :                         range_lower->get_type_hint ()),
   10239                 :           8 :           range_lower_locus));
   10240                 :           0 :     case FLOAT_LITERAL:
   10241                 :           0 :       lexer.skip_token ();
   10242                 :           0 :       rust_debug ("warning: used deprecated float range pattern bound");
   10243                 :           0 :       return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10244                 :           0 :         new AST::RangePatternBoundLiteral (
   10245                 :           0 :           AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
   10246                 :             :                         range_lower->get_type_hint ()),
   10247                 :           0 :           range_lower_locus));
   10248                 :           0 :     case MINUS:
   10249                 :             :       // branch on next token
   10250                 :           0 :       range_lower = lexer.peek_token (1);
   10251                 :           0 :       switch (range_lower->get_id ())
   10252                 :             :         {
   10253                 :           0 :         case INT_LITERAL:
   10254                 :           0 :           lexer.skip_token (1);
   10255                 :           0 :           return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10256                 :           0 :             new AST::RangePatternBoundLiteral (
   10257                 :           0 :               AST::Literal (range_lower->get_str (), AST::Literal::INT,
   10258                 :             :                             range_lower->get_type_hint ()),
   10259                 :           0 :               range_lower_locus, true));
   10260                 :           0 :         case FLOAT_LITERAL:
   10261                 :           0 :           lexer.skip_token (1);
   10262                 :           0 :           rust_debug ("warning: used deprecated float range pattern bound");
   10263                 :           0 :           return std::unique_ptr<AST::RangePatternBoundLiteral> (
   10264                 :           0 :             new AST::RangePatternBoundLiteral (
   10265                 :           0 :               AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
   10266                 :             :                             range_lower->get_type_hint ()),
   10267                 :           0 :               range_lower_locus, true));
   10268                 :           0 :         default:
   10269                 :           0 :           add_error (Error (range_lower->get_locus (),
   10270                 :             :                             "token type %qs cannot be parsed as range pattern "
   10271                 :             :                             "bound after minus symbol",
   10272                 :             :                             range_lower->get_token_description ()));
   10273                 :             : 
   10274                 :           0 :           return nullptr;
   10275                 :             :         }
   10276                 :          16 :     case IDENTIFIER:
   10277                 :             :     case SUPER:
   10278                 :             :     case SELF:
   10279                 :             :     case SELF_ALIAS:
   10280                 :             :     case CRATE:
   10281                 :             :     case SCOPE_RESOLUTION:
   10282                 :             :       case DOLLAR_SIGN: {
   10283                 :             :         // path in expression
   10284                 :          16 :         AST::PathInExpression path = parse_path_in_expression ();
   10285                 :          16 :         if (path.is_error ())
   10286                 :             :           {
   10287                 :           0 :             Error error (
   10288                 :             :               range_lower->get_locus (),
   10289                 :             :               "failed to parse path in expression range pattern bound");
   10290                 :           0 :             add_error (std::move (error));
   10291                 :             : 
   10292                 :           0 :             return nullptr;
   10293                 :           0 :           }
   10294                 :          16 :         return std::unique_ptr<AST::RangePatternBoundPath> (
   10295                 :          16 :           new AST::RangePatternBoundPath (std::move (path)));
   10296                 :          16 :       }
   10297                 :           0 :     case LEFT_SHIFT:
   10298                 :             :       case LEFT_ANGLE: {
   10299                 :             :         // qualified path in expression
   10300                 :           0 :         AST::QualifiedPathInExpression path
   10301                 :             :           = parse_qualified_path_in_expression ();
   10302                 :           0 :         if (path.is_error ())
   10303                 :             :           {
   10304                 :           0 :             Error error (range_lower->get_locus (),
   10305                 :             :                          "failed to parse qualified path in expression range "
   10306                 :             :                          "pattern bound");
   10307                 :           0 :             add_error (std::move (error));
   10308                 :             : 
   10309                 :           0 :             return nullptr;
   10310                 :           0 :           }
   10311                 :           0 :         return std::unique_ptr<AST::RangePatternBoundQualPath> (
   10312                 :           0 :           new AST::RangePatternBoundQualPath (std::move (path)));
   10313                 :           0 :       }
   10314                 :           0 :     default:
   10315                 :           0 :       add_error (
   10316                 :           0 :         Error (range_lower->get_locus (),
   10317                 :             :                "token type %qs cannot be parsed as range pattern bound",
   10318                 :             :                range_lower->get_token_description ()));
   10319                 :             : 
   10320                 :           0 :       return nullptr;
   10321                 :             :     }
   10322                 :          31 : }
   10323                 :             : 
   10324                 :             : template <typename ManagedTokenSource>
   10325                 :             : std::unique_ptr<AST::Pattern>
   10326                 :       19934 : Parser<ManagedTokenSource>::parse_pattern ()
   10327                 :             : {
   10328                 :       19934 :   location_t start_locus = lexer.peek_token ()->get_locus ();
   10329                 :             : 
   10330                 :             :   /* skip optional starting pipe */
   10331                 :       19934 :   maybe_skip_token (PIPE);
   10332                 :             : 
   10333                 :       19934 :   auto first = parse_pattern_no_alt ();
   10334                 :             : 
   10335                 :       39868 :   if (lexer.peek_token ()->get_id () != PIPE)
   10336                 :             :     /* no alternates */
   10337                 :       19880 :     return first;
   10338                 :             : 
   10339                 :          54 :   std::vector<std::unique_ptr<AST::Pattern>> alts;
   10340                 :          54 :   alts.push_back (std::move (first));
   10341                 :             : 
   10342                 :             :   do
   10343                 :             :     {
   10344                 :          70 :       lexer.skip_token ();
   10345                 :          70 :       alts.push_back (parse_pattern_no_alt ());
   10346                 :             :     }
   10347                 :             : 
   10348                 :         140 :   while (lexer.peek_token ()->get_id () == PIPE);
   10349                 :             : 
   10350                 :             :   /* alternates */
   10351                 :             :   return std::unique_ptr<AST::Pattern> (
   10352                 :          54 :     new AST::AltPattern (std::move (alts), start_locus));
   10353                 :       19934 : }
   10354                 :             : 
   10355                 :             : // Parses a pattern without alternates ('|')
   10356                 :             : // (will further disambiguate any pattern).
   10357                 :             : template <typename ManagedTokenSource>
   10358                 :             : std::unique_ptr<AST::Pattern>
   10359                 :       20126 : Parser<ManagedTokenSource>::parse_pattern_no_alt ()
   10360                 :             : {
   10361                 :       20126 :   const_TokenPtr t = lexer.peek_token ();
   10362                 :       20126 :   switch (t->get_id ())
   10363                 :             :     {
   10364                 :          20 :     case TRUE_LITERAL:
   10365                 :          20 :       lexer.skip_token ();
   10366                 :          20 :       return std::unique_ptr<AST::LiteralPattern> (
   10367                 :          60 :         new AST::LiteralPattern (Values::Keywords::TRUE_LITERAL,
   10368                 :             :                                  AST::Literal::BOOL, t->get_locus (),
   10369                 :          20 :                                  t->get_type_hint ()));
   10370                 :          11 :     case FALSE_LITERAL:
   10371                 :          11 :       lexer.skip_token ();
   10372                 :          11 :       return std::unique_ptr<AST::LiteralPattern> (
   10373                 :          33 :         new AST::LiteralPattern (Values::Keywords::FALSE_LITERAL,
   10374                 :             :                                  AST::Literal::BOOL, t->get_locus (),
   10375                 :          11 :                                  t->get_type_hint ()));
   10376                 :         190 :     case CHAR_LITERAL:
   10377                 :             :     case BYTE_CHAR_LITERAL:
   10378                 :             :     case INT_LITERAL:
   10379                 :             :     case FLOAT_LITERAL:
   10380                 :         190 :       return parse_literal_or_range_pattern ();
   10381                 :           0 :     case STRING_LITERAL:
   10382                 :           0 :       lexer.skip_token ();
   10383                 :           0 :       return std::unique_ptr<AST::LiteralPattern> (
   10384                 :           0 :         new AST::LiteralPattern (t->get_str (), AST::Literal::STRING,
   10385                 :           0 :                                  t->get_locus (), t->get_type_hint ()));
   10386                 :           0 :     case BYTE_STRING_LITERAL:
   10387                 :           0 :       lexer.skip_token ();
   10388                 :           0 :       return std::unique_ptr<AST::LiteralPattern> (
   10389                 :           0 :         new AST::LiteralPattern (t->get_str (), AST::Literal::BYTE_STRING,
   10390                 :           0 :                                  t->get_locus (), t->get_type_hint ()));
   10391                 :           0 :     case RAW_STRING_LITERAL:
   10392                 :           0 :       lexer.skip_token ();
   10393                 :           0 :       return std::unique_ptr<AST::LiteralPattern> (
   10394                 :           0 :         new AST::LiteralPattern (t->get_str (), AST::Literal::RAW_STRING,
   10395                 :           0 :                                  t->get_locus (), t->get_type_hint ()));
   10396                 :             :     // raw string and raw byte string literals too if they are readded to
   10397                 :             :     // lexer
   10398                 :           0 :     case MINUS:
   10399                 :           0 :       if (lexer.peek_token (1)->get_id () == INT_LITERAL)
   10400                 :             :         {
   10401                 :           0 :           return parse_literal_or_range_pattern ();
   10402                 :             :         }
   10403                 :           0 :       else if (lexer.peek_token (1)->get_id () == FLOAT_LITERAL)
   10404                 :             :         {
   10405                 :           0 :           return parse_literal_or_range_pattern ();
   10406                 :             :         }
   10407                 :             :       else
   10408                 :             :         {
   10409                 :           0 :           Error error (t->get_locus (), "unexpected token %<-%> in pattern - "
   10410                 :             :                                         "did you forget an integer literal");
   10411                 :           0 :           add_error (std::move (error));
   10412                 :             : 
   10413                 :           0 :           return nullptr;
   10414                 :           0 :         }
   10415                 :         594 :     case UNDERSCORE:
   10416                 :         594 :       lexer.skip_token ();
   10417                 :         594 :       return std::unique_ptr<AST::WildcardPattern> (
   10418                 :         594 :         new AST::WildcardPattern (t->get_locus ()));
   10419                 :           4 :     case DOT_DOT:
   10420                 :           4 :       lexer.skip_token ();
   10421                 :           4 :       return std::unique_ptr<AST::RestPattern> (
   10422                 :           4 :         new AST::RestPattern (t->get_locus ()));
   10423                 :         715 :     case REF:
   10424                 :             :     case MUT:
   10425                 :         715 :       return parse_identifier_pattern ();
   10426                 :       18299 :     case IDENTIFIER:
   10427                 :             :       /* if identifier with no scope resolution afterwards, identifier
   10428                 :             :        * pattern. if scope resolution afterwards, path pattern (or range
   10429                 :             :        * pattern or struct pattern or tuple struct pattern) or macro
   10430                 :             :        * invocation */
   10431                 :       18299 :       return parse_ident_leading_pattern ();
   10432                 :          43 :     case AMP:
   10433                 :             :     case LOGICAL_AND:
   10434                 :             :       // reference pattern
   10435                 :          43 :       return parse_reference_pattern ();
   10436                 :         232 :     case LEFT_PAREN:
   10437                 :             :       // tuple pattern or grouped pattern
   10438                 :         232 :       return parse_grouped_or_tuple_pattern ();
   10439                 :           6 :     case LEFT_SQUARE:
   10440                 :             :       // slice pattern
   10441                 :           6 :       return parse_slice_pattern ();
   10442                 :           0 :     case LEFT_SHIFT:
   10443                 :             :       case LEFT_ANGLE: {
   10444                 :             :         // qualified path in expression or qualified range pattern bound
   10445                 :           0 :         AST::QualifiedPathInExpression path
   10446                 :             :           = parse_qualified_path_in_expression ();
   10447                 :             : 
   10448                 :           0 :         if (lexer.peek_token ()->get_id () == DOT_DOT_EQ
   10449                 :           0 :             || lexer.peek_token ()->get_id () == ELLIPSIS
   10450                 :           0 :             || lexer.peek_token ()->get_id () == DOT_DOT)
   10451                 :             :           {
   10452                 :             :             // qualified range pattern bound, so parse rest of range pattern
   10453                 :             :             AST::RangeKind kind
   10454                 :           0 :               = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
   10455                 :           0 :             lexer.skip_token ();
   10456                 :             : 
   10457                 :           0 :             std::unique_ptr<AST::RangePatternBoundQualPath> lower_bound (
   10458                 :           0 :               new AST::RangePatternBoundQualPath (std::move (path)));
   10459                 :           0 :             std::unique_ptr<AST::RangePatternBound> upper_bound
   10460                 :             :               = parse_range_pattern_bound ();
   10461                 :             : 
   10462                 :           0 :             return std::unique_ptr<AST::RangePattern> (
   10463                 :           0 :               new AST::RangePattern (std::move (lower_bound),
   10464                 :             :                                      std::move (upper_bound), kind,
   10465                 :           0 :                                      t->get_locus ()));
   10466                 :           0 :           }
   10467                 :             :         else
   10468                 :             :           {
   10469                 :             :             // just qualified path in expression
   10470                 :           0 :             return std::unique_ptr<AST::QualifiedPathInExpression> (
   10471                 :           0 :               new AST::QualifiedPathInExpression (std::move (path)));
   10472                 :             :           }
   10473                 :           0 :       }
   10474                 :          12 :     case SUPER:
   10475                 :             :     case SELF:
   10476                 :             :     case SELF_ALIAS:
   10477                 :             :     case CRATE:
   10478                 :             :     case SCOPE_RESOLUTION:
   10479                 :             :       case DOLLAR_SIGN: {
   10480                 :             :         // path in expression or range pattern bound
   10481                 :          12 :         AST::PathInExpression path = parse_path_in_expression ();
   10482                 :             : 
   10483                 :          12 :         const_TokenPtr next = lexer.peek_token ();
   10484                 :          12 :         switch (next->get_id ())
   10485                 :             :           {
   10486                 :           0 :           case DOT_DOT_EQ:
   10487                 :             :           case DOT_DOT:
   10488                 :             :             case ELLIPSIS: {
   10489                 :             :               // qualified range pattern bound, so parse rest of range pattern
   10490                 :           0 :               AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
   10491                 :           0 :               lexer.skip_token ();
   10492                 :             : 
   10493                 :           0 :               std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
   10494                 :           0 :                 new AST::RangePatternBoundPath (std::move (path)));
   10495                 :           0 :               std::unique_ptr<AST::RangePatternBound> upper_bound
   10496                 :             :                 = parse_range_pattern_bound ();
   10497                 :             : 
   10498                 :           0 :               return std::unique_ptr<AST::RangePattern> (
   10499                 :           0 :                 new AST::RangePattern (std::move (lower_bound),
   10500                 :             :                                        std::move (upper_bound), kind,
   10501                 :           0 :                                        next->get_locus ()));
   10502                 :           0 :             }
   10503                 :           0 :           case EXCLAM:
   10504                 :           0 :             return parse_macro_invocation_partial (std::move (path),
   10505                 :           0 :                                                    AST::AttrVec ());
   10506                 :           0 :             case LEFT_PAREN: {
   10507                 :             :               // tuple struct
   10508                 :           0 :               lexer.skip_token ();
   10509                 :             : 
   10510                 :             :               // parse items
   10511                 :           0 :               std::unique_ptr<AST::TupleStructItems> items
   10512                 :             :                 = parse_tuple_struct_items ();
   10513                 :           0 :               if (items == nullptr)
   10514                 :             :                 {
   10515                 :           0 :                   Error error (lexer.peek_token ()->get_locus (),
   10516                 :             :                                "failed to parse tuple struct items");
   10517                 :           0 :                   add_error (std::move (error));
   10518                 :             : 
   10519                 :           0 :                   return nullptr;
   10520                 :           0 :                 }
   10521                 :             : 
   10522                 :           0 :               if (!skip_token (RIGHT_PAREN))
   10523                 :             :                 {
   10524                 :           0 :                   return nullptr;
   10525                 :             :                 }
   10526                 :             : 
   10527                 :           0 :               return std::unique_ptr<AST::TupleStructPattern> (
   10528                 :           0 :                 new AST::TupleStructPattern (std::move (path),
   10529                 :           0 :                                              std::move (items)));
   10530                 :           0 :             }
   10531                 :           0 :             case LEFT_CURLY: {
   10532                 :             :               // struct
   10533                 :           0 :               lexer.skip_token ();
   10534                 :             : 
   10535                 :             :               // parse elements (optional)
   10536                 :           0 :               AST::StructPatternElements elems = parse_struct_pattern_elems ();
   10537                 :             : 
   10538                 :           0 :               if (!skip_token (RIGHT_CURLY))
   10539                 :             :                 {
   10540                 :           0 :                   return nullptr;
   10541                 :             :                 }
   10542                 :             : 
   10543                 :           0 :               return std::unique_ptr<AST::StructPattern> (
   10544                 :           0 :                 new AST::StructPattern (std::move (path), t->get_locus (),
   10545                 :           0 :                                         std::move (elems)));
   10546                 :           0 :             }
   10547                 :          12 :           default:
   10548                 :             :             // assume path in expression
   10549                 :          12 :             return std::unique_ptr<AST::PathInExpression> (
   10550                 :          12 :               new AST::PathInExpression (std::move (path)));
   10551                 :             :           }
   10552                 :          12 :       }
   10553                 :           0 :     default:
   10554                 :           0 :       add_error (Error (t->get_locus (), "unexpected token %qs in pattern",
   10555                 :             :                         t->get_token_description ()));
   10556                 :             : 
   10557                 :           0 :       return nullptr;
   10558                 :             :     }
   10559                 :       20126 : }
   10560                 :             : 
   10561                 :             : // Parses a single or double reference pattern.
   10562                 :             : template <typename ManagedTokenSource>
   10563                 :             : std::unique_ptr<AST::ReferencePattern>
   10564                 :          43 : Parser<ManagedTokenSource>::parse_reference_pattern ()
   10565                 :             : {
   10566                 :             :   // parse double or single ref
   10567                 :          43 :   bool is_double_ref = false;
   10568                 :          43 :   const_TokenPtr t = lexer.peek_token ();
   10569                 :          43 :   switch (t->get_id ())
   10570                 :             :     {
   10571                 :          32 :     case AMP:
   10572                 :             :       // still false
   10573                 :          32 :       lexer.skip_token ();
   10574                 :             :       break;
   10575                 :          11 :     case LOGICAL_AND:
   10576                 :          11 :       is_double_ref = true;
   10577                 :          11 :       lexer.skip_token ();
   10578                 :             :       break;
   10579                 :           0 :     default:
   10580                 :           0 :       add_error (Error (t->get_locus (),
   10581                 :             :                         "unexpected token %qs in reference pattern",
   10582                 :             :                         t->get_token_description ()));
   10583                 :             : 
   10584                 :           0 :       return nullptr;
   10585                 :             :     }
   10586                 :             : 
   10587                 :             :   // parse mut (if it exists)
   10588                 :          43 :   bool is_mut = false;
   10589                 :          86 :   if (lexer.peek_token ()->get_id () == MUT)
   10590                 :             :     {
   10591                 :           4 :       is_mut = true;
   10592                 :           4 :       lexer.skip_token ();
   10593                 :             :     }
   10594                 :             : 
   10595                 :             :   // parse pattern to get reference of (required)
   10596                 :          43 :   std::unique_ptr<AST::Pattern> pattern = parse_pattern_no_alt ();
   10597                 :          43 :   if (pattern == nullptr)
   10598                 :             :     {
   10599                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
   10600                 :             :                    "failed to parse pattern in reference pattern");
   10601                 :           0 :       add_error (std::move (error));
   10602                 :             : 
   10603                 :             :       // skip somewhere?
   10604                 :           0 :       return nullptr;
   10605                 :           0 :     }
   10606                 :             : 
   10607                 :             :   return std::unique_ptr<AST::ReferencePattern> (
   10608                 :          43 :     new AST::ReferencePattern (std::move (pattern), is_mut, is_double_ref,
   10609                 :          43 :                                t->get_locus ()));
   10610                 :          43 : }
   10611                 :             : 
   10612                 :             : /* Parses a grouped pattern or tuple pattern. Prefers grouped over tuple if
   10613                 :             :  * only a single element with no commas. */
   10614                 :             : template <typename ManagedTokenSource>
   10615                 :             : std::unique_ptr<AST::Pattern>
   10616                 :         232 : Parser<ManagedTokenSource>::parse_grouped_or_tuple_pattern ()
   10617                 :             : {
   10618                 :         232 :   location_t paren_locus = lexer.peek_token ()->get_locus ();
   10619                 :         232 :   skip_token (LEFT_PAREN);
   10620                 :             : 
   10621                 :             :   // detect '..' token (ranged with no lower range)
   10622                 :         464 :   if (lexer.peek_token ()->get_id () == DOT_DOT)
   10623                 :             :     {
   10624                 :           0 :       lexer.skip_token ();
   10625                 :             : 
   10626                 :             :       // parse new patterns while next token is a comma
   10627                 :           0 :       std::vector<std::unique_ptr<AST::Pattern>> patterns;
   10628                 :             : 
   10629                 :           0 :       const_TokenPtr t = lexer.peek_token ();
   10630                 :           0 :       while (t->get_id () == COMMA)
   10631                 :             :         {
   10632                 :           0 :           lexer.skip_token ();
   10633                 :             : 
   10634                 :             :           // break if next token is ')'
   10635                 :           0 :           if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
   10636                 :             :             {
   10637                 :             :               break;
   10638                 :             :             }
   10639                 :             : 
   10640                 :             :           // parse pattern, which is required
   10641                 :           0 :           std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   10642                 :           0 :           if (pattern == nullptr)
   10643                 :             :             {
   10644                 :           0 :               Error error (
   10645                 :           0 :                 lexer.peek_token ()->get_locus (),
   10646                 :             :                 "failed to parse pattern inside ranged tuple pattern");
   10647                 :           0 :               add_error (std::move (error));
   10648                 :             : 
   10649                 :             :               // skip somewhere?
   10650                 :           0 :               return nullptr;
   10651                 :           0 :             }
   10652                 :           0 :           patterns.push_back (std::move (pattern));
   10653                 :             : 
   10654                 :           0 :           t = lexer.peek_token ();
   10655                 :             :         }
   10656                 :             : 
   10657                 :           0 :       if (!skip_token (RIGHT_PAREN))
   10658                 :             :         {
   10659                 :             :           // skip somewhere?
   10660                 :           0 :           return nullptr;
   10661                 :             :         }
   10662                 :             : 
   10663                 :             :       // create ranged tuple pattern items with only upper items
   10664                 :           0 :       std::unique_ptr<AST::TuplePatternItemsRanged> items (
   10665                 :           0 :         new AST::TuplePatternItemsRanged (
   10666                 :           0 :           std::vector<std::unique_ptr<AST::Pattern>> (), std::move (patterns)));
   10667                 :           0 :       return std::unique_ptr<AST::TuplePattern> (
   10668                 :           0 :         new AST::TuplePattern (std::move (items), paren_locus));
   10669                 :           0 :     }
   10670                 :         464 :   else if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
   10671                 :             :     {
   10672                 :           4 :       skip_token (RIGHT_PAREN);
   10673                 :           4 :       auto items = std::unique_ptr<AST::TuplePatternItemsMultiple> (
   10674                 :           4 :         new AST::TuplePatternItemsMultiple (
   10675                 :           4 :           std::vector<std::unique_ptr<AST::Pattern>> ()));
   10676                 :           4 :       return std::unique_ptr<AST::TuplePattern> (
   10677                 :           4 :         new AST::TuplePattern (std::move (items), paren_locus));
   10678                 :           4 :     }
   10679                 :             : 
   10680                 :             :   // parse initial pattern (required)
   10681                 :         228 :   std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
   10682                 :         228 :   if (initial_pattern == nullptr)
   10683                 :             :     {
   10684                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
   10685                 :             :                    "failed to parse pattern in grouped or tuple pattern");
   10686                 :           0 :       add_error (std::move (error));
   10687                 :             : 
   10688                 :           0 :       return nullptr;
   10689                 :           0 :     }
   10690                 :             : 
   10691                 :             :   // branch on whether next token is a comma or not
   10692                 :         228 :   const_TokenPtr t = lexer.peek_token ();
   10693                 :         228 :   switch (t->get_id ())
   10694                 :             :     {
   10695                 :          44 :     case RIGHT_PAREN:
   10696                 :             :       // grouped pattern
   10697                 :          44 :       lexer.skip_token ();
   10698                 :             : 
   10699                 :          44 :       return std::unique_ptr<AST::GroupedPattern> (
   10700                 :          44 :         new AST::GroupedPattern (std::move (initial_pattern), paren_locus));
   10701                 :         184 :       case COMMA: {
   10702                 :             :         // tuple pattern
   10703                 :         184 :         lexer.skip_token ();
   10704                 :             : 
   10705                 :             :         // create vector of patterns
   10706                 :         184 :         std::vector<std::unique_ptr<AST::Pattern>> patterns;
   10707                 :         184 :         patterns.push_back (std::move (initial_pattern));
   10708                 :             : 
   10709                 :         184 :         t = lexer.peek_token ();
   10710                 :         385 :         while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
   10711                 :             :           {
   10712                 :             :             // parse pattern (required)
   10713                 :         201 :             std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   10714                 :         201 :             if (pattern == nullptr)
   10715                 :             :               {
   10716                 :           0 :                 Error error (t->get_locus (),
   10717                 :             :                              "failed to parse pattern in tuple pattern");
   10718                 :           0 :                 add_error (std::move (error));
   10719                 :             : 
   10720                 :           0 :                 return nullptr;
   10721                 :           0 :               }
   10722                 :         201 :             patterns.push_back (std::move (pattern));
   10723                 :             : 
   10724                 :         402 :             if (lexer.peek_token ()->get_id () != COMMA)
   10725                 :             :               break;
   10726                 :             : 
   10727                 :          21 :             lexer.skip_token ();
   10728                 :          21 :             t = lexer.peek_token ();
   10729                 :             :           }
   10730                 :             : 
   10731                 :         184 :         t = lexer.peek_token ();
   10732                 :         184 :         if (t->get_id () == RIGHT_PAREN)
   10733                 :             :           {
   10734                 :             :             // non-ranged tuple pattern
   10735                 :         184 :             lexer.skip_token ();
   10736                 :             : 
   10737                 :         184 :             std::unique_ptr<AST::TuplePatternItemsMultiple> items (
   10738                 :         184 :               new AST::TuplePatternItemsMultiple (std::move (patterns)));
   10739                 :         184 :             return std::unique_ptr<AST::TuplePattern> (
   10740                 :         184 :               new AST::TuplePattern (std::move (items), paren_locus));
   10741                 :         184 :           }
   10742                 :           0 :         else if (t->get_id () == DOT_DOT)
   10743                 :             :           {
   10744                 :             :             // ranged tuple pattern
   10745                 :           0 :             lexer.skip_token ();
   10746                 :             : 
   10747                 :             :             // parse upper patterns
   10748                 :           0 :             std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
   10749                 :           0 :             t = lexer.peek_token ();
   10750                 :           0 :             while (t->get_id () == COMMA)
   10751                 :             :               {
   10752                 :           0 :                 lexer.skip_token ();
   10753                 :             : 
   10754                 :             :                 // break if end
   10755                 :           0 :                 if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
   10756                 :             :                   break;
   10757                 :             : 
   10758                 :             :                 // parse pattern (required)
   10759                 :           0 :                 std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   10760                 :           0 :                 if (pattern == nullptr)
   10761                 :             :                   {
   10762                 :           0 :                     Error error (lexer.peek_token ()->get_locus (),
   10763                 :             :                                  "failed to parse pattern in tuple pattern");
   10764                 :           0 :                     add_error (std::move (error));
   10765                 :             : 
   10766                 :           0 :                     return nullptr;
   10767                 :           0 :                   }
   10768                 :           0 :                 upper_patterns.push_back (std::move (pattern));
   10769                 :             : 
   10770                 :           0 :                 t = lexer.peek_token ();
   10771                 :             :               }
   10772                 :             : 
   10773                 :           0 :             if (!skip_token (RIGHT_PAREN))
   10774                 :             :               {
   10775                 :           0 :                 return nullptr;
   10776                 :             :               }
   10777                 :             : 
   10778                 :           0 :             std::unique_ptr<AST::TuplePatternItemsRanged> items (
   10779                 :           0 :               new AST::TuplePatternItemsRanged (std::move (patterns),
   10780                 :             :                                                 std::move (upper_patterns)));
   10781                 :           0 :             return std::unique_ptr<AST::TuplePattern> (
   10782                 :           0 :               new AST::TuplePattern (std::move (items), paren_locus));
   10783                 :           0 :           }
   10784                 :             :         else
   10785                 :             :           {
   10786                 :             :             // some kind of error
   10787                 :           0 :             Error error (t->get_locus (),
   10788                 :             :                          "failed to parse tuple pattern (probably) or maybe "
   10789                 :             :                          "grouped pattern");
   10790                 :           0 :             add_error (std::move (error));
   10791                 :             : 
   10792                 :           0 :             return nullptr;
   10793                 :           0 :           }
   10794                 :         184 :       }
   10795                 :           0 :     default:
   10796                 :             :       // error
   10797                 :           0 :       add_error (Error (t->get_locus (),
   10798                 :             :                         "unrecognised token %qs in grouped or tuple pattern "
   10799                 :             :                         "after first pattern",
   10800                 :             :                         t->get_token_description ()));
   10801                 :             : 
   10802                 :           0 :       return nullptr;
   10803                 :             :     }
   10804                 :         228 : }
   10805                 :             : 
   10806                 :             : /* Parses a slice pattern that can match arrays or slices. Parses the square
   10807                 :             :  * brackets too. */
   10808                 :             : template <typename ManagedTokenSource>
   10809                 :             : std::unique_ptr<AST::SlicePattern>
   10810                 :           6 : Parser<ManagedTokenSource>::parse_slice_pattern ()
   10811                 :             : {
   10812                 :          12 :   location_t square_locus = lexer.peek_token ()->get_locus ();
   10813                 :           6 :   std::vector<std::unique_ptr<AST::Pattern>> patterns;
   10814                 :           6 :   skip_token (LEFT_SQUARE);
   10815                 :             : 
   10816                 :          12 :   if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
   10817                 :             :     {
   10818                 :           2 :       skip_token (RIGHT_SQUARE);
   10819                 :             :       return std::unique_ptr<AST::SlicePattern> (
   10820                 :           2 :         new AST::SlicePattern (std::move (patterns), square_locus));
   10821                 :             :     }
   10822                 :             : 
   10823                 :             :   // parse initial pattern (required)
   10824                 :           4 :   std::unique_ptr<AST::Pattern> initial_pattern = parse_pattern ();
   10825                 :           4 :   if (initial_pattern == nullptr)
   10826                 :             :     {
   10827                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
   10828                 :             :                    "failed to parse initial pattern in slice pattern");
   10829                 :           0 :       add_error (std::move (error));
   10830                 :             : 
   10831                 :           0 :       return nullptr;
   10832                 :           0 :     }
   10833                 :             : 
   10834                 :           4 :   patterns.push_back (std::move (initial_pattern));
   10835                 :             : 
   10836                 :           4 :   const_TokenPtr t = lexer.peek_token ();
   10837                 :           8 :   while (t->get_id () == COMMA)
   10838                 :             :     {
   10839                 :           4 :       lexer.skip_token ();
   10840                 :             : 
   10841                 :             :       // break if end bracket
   10842                 :           8 :       if (lexer.peek_token ()->get_id () == RIGHT_SQUARE)
   10843                 :             :         break;
   10844                 :             : 
   10845                 :             :       // parse pattern (required)
   10846                 :           4 :       std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   10847                 :           4 :       if (pattern == nullptr)
   10848                 :             :         {
   10849                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
   10850                 :             :                        "failed to parse pattern in slice pattern");
   10851                 :           0 :           add_error (std::move (error));
   10852                 :             : 
   10853                 :           0 :           return nullptr;
   10854                 :           0 :         }
   10855                 :           4 :       patterns.push_back (std::move (pattern));
   10856                 :             : 
   10857                 :           4 :       t = lexer.peek_token ();
   10858                 :             :     }
   10859                 :             : 
   10860                 :           4 :   if (!skip_token (RIGHT_SQUARE))
   10861                 :             :     {
   10862                 :           0 :       return nullptr;
   10863                 :             :     }
   10864                 :             : 
   10865                 :             :   return std::unique_ptr<AST::SlicePattern> (
   10866                 :           4 :     new AST::SlicePattern (std::move (patterns), square_locus));
   10867                 :           6 : }
   10868                 :             : 
   10869                 :             : /* Parses an identifier pattern (pattern that binds a value matched to a
   10870                 :             :  * variable). */
   10871                 :             : template <typename ManagedTokenSource>
   10872                 :             : std::unique_ptr<AST::IdentifierPattern>
   10873                 :         715 : Parser<ManagedTokenSource>::parse_identifier_pattern ()
   10874                 :             : {
   10875                 :         715 :   location_t locus = lexer.peek_token ()->get_locus ();
   10876                 :             : 
   10877                 :         715 :   bool has_ref = false;
   10878                 :        1430 :   if (lexer.peek_token ()->get_id () == REF)
   10879                 :             :     {
   10880                 :           4 :       has_ref = true;
   10881                 :           4 :       lexer.skip_token ();
   10882                 :             : 
   10883                 :             :       // DEBUG
   10884                 :           4 :       rust_debug ("parsed ref in identifier pattern");
   10885                 :             :     }
   10886                 :             : 
   10887                 :         715 :   bool has_mut = false;
   10888                 :        1430 :   if (lexer.peek_token ()->get_id () == MUT)
   10889                 :             :     {
   10890                 :         713 :       has_mut = true;
   10891                 :         713 :       lexer.skip_token ();
   10892                 :             :     }
   10893                 :             : 
   10894                 :             :   // parse identifier (required)
   10895                 :         715 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
   10896                 :         715 :   if (ident_tok == nullptr)
   10897                 :             :     {
   10898                 :             :       // skip somewhere?
   10899                 :           0 :       return nullptr;
   10900                 :             :     }
   10901                 :         715 :   Identifier ident{ident_tok};
   10902                 :             : 
   10903                 :             :   // DEBUG
   10904                 :         715 :   rust_debug ("parsed identifier in identifier pattern");
   10905                 :             : 
   10906                 :             :   // parse optional pattern binding thing
   10907                 :         715 :   std::unique_ptr<AST::Pattern> bind_pattern = nullptr;
   10908                 :        1430 :   if (lexer.peek_token ()->get_id () == PATTERN_BIND)
   10909                 :             :     {
   10910                 :           0 :       lexer.skip_token ();
   10911                 :             : 
   10912                 :             :       // parse required pattern to bind
   10913                 :           0 :       bind_pattern = parse_pattern ();
   10914                 :           0 :       if (bind_pattern == nullptr)
   10915                 :             :         {
   10916                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
   10917                 :             :                        "failed to parse pattern to bind in identifier pattern");
   10918                 :           0 :           add_error (std::move (error));
   10919                 :             : 
   10920                 :           0 :           return nullptr;
   10921                 :           0 :         }
   10922                 :             :     }
   10923                 :             : 
   10924                 :             :   // DEBUG
   10925                 :         715 :   rust_debug ("about to return identifier pattern");
   10926                 :             : 
   10927                 :             :   return std::unique_ptr<AST::IdentifierPattern> (
   10928                 :         715 :     new AST::IdentifierPattern (std::move (ident), locus, has_ref, has_mut,
   10929                 :         715 :                                 std::move (bind_pattern)));
   10930                 :         715 : }
   10931                 :             : 
   10932                 :             : /* Parses a pattern that opens with an identifier. This includes identifier
   10933                 :             :  * patterns, path patterns (and derivatives such as struct patterns, tuple
   10934                 :             :  * struct patterns, and macro invocations), and ranges. */
   10935                 :             : template <typename ManagedTokenSource>
   10936                 :             : std::unique_ptr<AST::Pattern>
   10937                 :       18299 : Parser<ManagedTokenSource>::parse_ident_leading_pattern ()
   10938                 :             : {
   10939                 :             :   // ensure first token is actually identifier
   10940                 :       18299 :   const_TokenPtr initial_tok = lexer.peek_token ();
   10941                 :       18299 :   if (initial_tok->get_id () != IDENTIFIER)
   10942                 :             :     {
   10943                 :           0 :       return nullptr;
   10944                 :             :     }
   10945                 :             : 
   10946                 :             :   // save initial identifier as it may be useful (but don't skip)
   10947                 :       18299 :   std::string initial_ident = initial_tok->get_str ();
   10948                 :             : 
   10949                 :             :   // parse next tokens as a PathInExpression
   10950                 :       18299 :   AST::PathInExpression path = parse_path_in_expression ();
   10951                 :             : 
   10952                 :             :   // branch on next token
   10953                 :       18299 :   const_TokenPtr t = lexer.peek_token ();
   10954                 :       18299 :   switch (t->get_id ())
   10955                 :             :     {
   10956                 :           0 :     case EXCLAM:
   10957                 :           0 :       return parse_macro_invocation_partial (std::move (path), AST::AttrVec ());
   10958                 :         290 :       case LEFT_PAREN: {
   10959                 :             :         // tuple struct
   10960                 :         290 :         lexer.skip_token ();
   10961                 :             : 
   10962                 :             :         // DEBUG
   10963                 :         290 :         rust_debug ("parsing tuple struct pattern");
   10964                 :             : 
   10965                 :             :         // parse items
   10966                 :         290 :         std::unique_ptr<AST::TupleStructItems> items
   10967                 :             :           = parse_tuple_struct_items ();
   10968                 :         290 :         if (items == nullptr)
   10969                 :             :           {
   10970                 :           0 :             Error error (lexer.peek_token ()->get_locus (),
   10971                 :             :                          "failed to parse tuple struct items");
   10972                 :           0 :             add_error (std::move (error));
   10973                 :             : 
   10974                 :           0 :             return nullptr;
   10975                 :           0 :           }
   10976                 :             : 
   10977                 :             :         // DEBUG
   10978                 :         290 :         rust_debug ("successfully parsed tuple struct items");
   10979                 :             : 
   10980                 :         290 :         if (!skip_token (RIGHT_PAREN))
   10981                 :             :           {
   10982                 :           0 :             return nullptr;
   10983                 :             :           }
   10984                 :             : 
   10985                 :             :         // DEBUG
   10986                 :         290 :         rust_debug ("successfully parsed tuple struct pattern");
   10987                 :             : 
   10988                 :         290 :         return std::unique_ptr<AST::TupleStructPattern> (
   10989                 :         290 :           new AST::TupleStructPattern (std::move (path), std::move (items)));
   10990                 :         290 :       }
   10991                 :          86 :       case LEFT_CURLY: {
   10992                 :             :         // struct
   10993                 :          86 :         lexer.skip_token ();
   10994                 :             : 
   10995                 :             :         // parse elements (optional)
   10996                 :          86 :         AST::StructPatternElements elems = parse_struct_pattern_elems ();
   10997                 :             : 
   10998                 :          86 :         if (!skip_token (RIGHT_CURLY))
   10999                 :             :           {
   11000                 :           0 :             return nullptr;
   11001                 :             :           }
   11002                 :             : 
   11003                 :             :         // DEBUG
   11004                 :          86 :         rust_debug ("successfully parsed struct pattern");
   11005                 :             : 
   11006                 :          86 :         return std::unique_ptr<AST::StructPattern> (
   11007                 :         172 :           new AST::StructPattern (std::move (path), initial_tok->get_locus (),
   11008                 :          86 :                                   std::move (elems)));
   11009                 :          86 :       }
   11010                 :           9 :     case DOT_DOT_EQ:
   11011                 :             :     case DOT_DOT:
   11012                 :             :       case ELLIPSIS: {
   11013                 :             :         // range
   11014                 :             :         AST::RangeKind kind
   11015                 :           9 :           = AST::tokenid_to_rangekind (lexer.peek_token ()->get_id ());
   11016                 :             : 
   11017                 :           9 :         lexer.skip_token ();
   11018                 :             : 
   11019                 :           9 :         std::unique_ptr<AST::RangePatternBoundPath> lower_bound (
   11020                 :           9 :           new AST::RangePatternBoundPath (std::move (path)));
   11021                 :           9 :         std::unique_ptr<AST::RangePatternBound> upper_bound
   11022                 :             :           = parse_range_pattern_bound ();
   11023                 :             : 
   11024                 :           9 :         return std::unique_ptr<AST::RangePattern> (
   11025                 :           9 :           new AST::RangePattern (std::move (lower_bound),
   11026                 :             :                                  std::move (upper_bound), kind,
   11027                 :           9 :                                  t->get_locus ()));
   11028                 :           9 :       }
   11029                 :           0 :       case PATTERN_BIND: {
   11030                 :             :         // only allow on single-segment paths
   11031                 :           0 :         if (path.is_single_segment ())
   11032                 :             :           {
   11033                 :             :             // identifier with pattern bind
   11034                 :           0 :             lexer.skip_token ();
   11035                 :             : 
   11036                 :           0 :             std::unique_ptr<AST::Pattern> bind_pattern = parse_pattern ();
   11037                 :           0 :             if (bind_pattern == nullptr)
   11038                 :             :               {
   11039                 :           0 :                 Error error (
   11040                 :             :                   t->get_locus (),
   11041                 :             :                   "failed to parse pattern to bind to identifier pattern");
   11042                 :           0 :                 add_error (std::move (error));
   11043                 :             : 
   11044                 :           0 :                 return nullptr;
   11045                 :           0 :               }
   11046                 :           0 :             return std::unique_ptr<AST::IdentifierPattern> (
   11047                 :           0 :               new AST::IdentifierPattern (std::move (initial_ident),
   11048                 :             :                                           initial_tok->get_locus (), false,
   11049                 :           0 :                                           false, std::move (bind_pattern)));
   11050                 :           0 :           }
   11051                 :           0 :         Error error (
   11052                 :             :           t->get_locus (),
   11053                 :             :           "failed to parse pattern bind to a path, not an identifier");
   11054                 :           0 :         add_error (std::move (error));
   11055                 :             : 
   11056                 :           0 :         return nullptr;
   11057                 :           0 :       }
   11058                 :       17914 :     default:
   11059                 :             :       // assume identifier if single segment
   11060                 :       17914 :       if (path.is_single_segment ())
   11061                 :             :         {
   11062                 :       17635 :           return std::unique_ptr<AST::IdentifierPattern> (
   11063                 :       35270 :             new AST::IdentifierPattern (std::move (initial_ident),
   11064                 :       17635 :                                         initial_tok->get_locus ()));
   11065                 :             :         }
   11066                 :             :       // return path otherwise
   11067                 :         279 :       return std::unique_ptr<AST::PathInExpression> (
   11068                 :         279 :         new AST::PathInExpression (std::move (path)));
   11069                 :             :     }
   11070                 :       18299 : }
   11071                 :             : 
   11072                 :             : // Parses tuple struct items if they exist. Does not parse parentheses.
   11073                 :             : template <typename ManagedTokenSource>
   11074                 :             : std::unique_ptr<AST::TupleStructItems>
   11075                 :         290 : Parser<ManagedTokenSource>::parse_tuple_struct_items ()
   11076                 :             : {
   11077                 :         290 :   std::vector<std::unique_ptr<AST::Pattern>> lower_patterns;
   11078                 :             : 
   11079                 :             :   // DEBUG
   11080                 :         290 :   rust_debug ("started parsing tuple struct items");
   11081                 :             : 
   11082                 :             :   // check for '..' at front
   11083                 :         580 :   if (lexer.peek_token ()->get_id () == DOT_DOT)
   11084                 :             :     {
   11085                 :             :       // only parse upper patterns
   11086                 :           0 :       lexer.skip_token ();
   11087                 :             : 
   11088                 :             :       // DEBUG
   11089                 :           0 :       rust_debug ("'..' at front in tuple struct items detected");
   11090                 :             : 
   11091                 :           0 :       std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
   11092                 :             : 
   11093                 :           0 :       const_TokenPtr t = lexer.peek_token ();
   11094                 :           0 :       while (t->get_id () == COMMA)
   11095                 :             :         {
   11096                 :           0 :           lexer.skip_token ();
   11097                 :             : 
   11098                 :             :           // break if right paren
   11099                 :           0 :           if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
   11100                 :             :             break;
   11101                 :             : 
   11102                 :             :           // parse pattern, which is now required
   11103                 :           0 :           std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   11104                 :           0 :           if (pattern == nullptr)
   11105                 :             :             {
   11106                 :           0 :               Error error (lexer.peek_token ()->get_locus (),
   11107                 :             :                            "failed to parse pattern in tuple struct items");
   11108                 :           0 :               add_error (std::move (error));
   11109                 :             : 
   11110                 :           0 :               return nullptr;
   11111                 :           0 :             }
   11112                 :           0 :           upper_patterns.push_back (std::move (pattern));
   11113                 :             : 
   11114                 :           0 :           t = lexer.peek_token ();
   11115                 :             :         }
   11116                 :             : 
   11117                 :             :       // DEBUG
   11118                 :           0 :       rust_debug (
   11119                 :             :         "finished parsing tuple struct items ranged (upper/none only)");
   11120                 :             : 
   11121                 :           0 :       return std::unique_ptr<AST::TupleStructItemsRange> (
   11122                 :           0 :         new AST::TupleStructItemsRange (std::move (lower_patterns),
   11123                 :           0 :                                         std::move (upper_patterns)));
   11124                 :           0 :     }
   11125                 :             : 
   11126                 :             :   // has at least some lower patterns
   11127                 :         290 :   const_TokenPtr t = lexer.peek_token ();
   11128                 :         589 :   while (t->get_id () != RIGHT_PAREN && t->get_id () != DOT_DOT)
   11129                 :             :     {
   11130                 :             :       // DEBUG
   11131                 :         299 :       rust_debug ("about to parse pattern in tuple struct items");
   11132                 :             : 
   11133                 :             :       // parse pattern, which is required
   11134                 :         299 :       std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   11135                 :         299 :       if (pattern == nullptr)
   11136                 :             :         {
   11137                 :           0 :           Error error (t->get_locus (),
   11138                 :             :                        "failed to parse pattern in tuple struct items");
   11139                 :           0 :           add_error (std::move (error));
   11140                 :             : 
   11141                 :           0 :           return nullptr;
   11142                 :           0 :         }
   11143                 :         299 :       lower_patterns.push_back (std::move (pattern));
   11144                 :             : 
   11145                 :             :       // DEBUG
   11146                 :         299 :       rust_debug ("successfully parsed pattern in tuple struct items");
   11147                 :             : 
   11148                 :         598 :       if (lexer.peek_token ()->get_id () != COMMA)
   11149                 :             :         {
   11150                 :             :           // DEBUG
   11151                 :         246 :           rust_debug ("broke out of parsing patterns in tuple struct "
   11152                 :             :                       "items as no comma");
   11153                 :             : 
   11154                 :             :           break;
   11155                 :             :         }
   11156                 :          53 :       lexer.skip_token ();
   11157                 :          53 :       t = lexer.peek_token ();
   11158                 :             :     }
   11159                 :             : 
   11160                 :             :   // branch on next token
   11161                 :         290 :   t = lexer.peek_token ();
   11162                 :         290 :   switch (t->get_id ())
   11163                 :             :     {
   11164                 :         290 :     case RIGHT_PAREN:
   11165                 :         290 :       return std::unique_ptr<AST::TupleStructItemsNoRange> (
   11166                 :         290 :         new AST::TupleStructItemsNoRange (std::move (lower_patterns)));
   11167                 :           0 :       case DOT_DOT: {
   11168                 :             :         // has an upper range that must be parsed separately
   11169                 :           0 :         lexer.skip_token ();
   11170                 :             : 
   11171                 :           0 :         std::vector<std::unique_ptr<AST::Pattern>> upper_patterns;
   11172                 :             : 
   11173                 :           0 :         t = lexer.peek_token ();
   11174                 :           0 :         while (t->get_id () == COMMA)
   11175                 :             :           {
   11176                 :           0 :             lexer.skip_token ();
   11177                 :             : 
   11178                 :             :             // break if next token is right paren
   11179                 :           0 :             if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
   11180                 :             :               break;
   11181                 :             : 
   11182                 :             :             // parse pattern, which is required
   11183                 :           0 :             std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   11184                 :           0 :             if (pattern == nullptr)
   11185                 :             :               {
   11186                 :           0 :                 Error error (lexer.peek_token ()->get_locus (),
   11187                 :             :                              "failed to parse pattern in tuple struct items");
   11188                 :           0 :                 add_error (std::move (error));
   11189                 :             : 
   11190                 :           0 :                 return nullptr;
   11191                 :           0 :               }
   11192                 :           0 :             upper_patterns.push_back (std::move (pattern));
   11193                 :             : 
   11194                 :           0 :             t = lexer.peek_token ();
   11195                 :             :           }
   11196                 :             : 
   11197                 :           0 :         return std::unique_ptr<AST::TupleStructItemsRange> (
   11198                 :           0 :           new AST::TupleStructItemsRange (std::move (lower_patterns),
   11199                 :           0 :                                           std::move (upper_patterns)));
   11200                 :           0 :       }
   11201                 :           0 :     default:
   11202                 :             :       // error
   11203                 :           0 :       add_error (Error (t->get_locus (),
   11204                 :             :                         "unexpected token %qs in tuple struct items",
   11205                 :             :                         t->get_token_description ()));
   11206                 :             : 
   11207                 :           0 :       return nullptr;
   11208                 :             :     }
   11209                 :         290 : }
   11210                 :             : 
   11211                 :             : // Parses struct pattern elements if they exist.
   11212                 :             : template <typename ManagedTokenSource>
   11213                 :             : AST::StructPatternElements
   11214                 :          86 : Parser<ManagedTokenSource>::parse_struct_pattern_elems ()
   11215                 :             : {
   11216                 :          86 :   std::vector<std::unique_ptr<AST::StructPatternField>> fields;
   11217                 :             : 
   11218                 :          86 :   AST::AttrVec etc_attrs;
   11219                 :          86 :   bool has_etc = false;
   11220                 :             : 
   11221                 :             :   // try parsing struct pattern fields
   11222                 :          86 :   const_TokenPtr t = lexer.peek_token ();
   11223                 :         300 :   while (t->get_id () != RIGHT_CURLY)
   11224                 :             :     {
   11225                 :         143 :       AST::AttrVec outer_attrs = parse_outer_attributes ();
   11226                 :             : 
   11227                 :             :       // parse etc (must be last in struct pattern, so breaks)
   11228                 :         286 :       if (lexer.peek_token ()->get_id () == DOT_DOT)
   11229                 :             :         {
   11230                 :           0 :           lexer.skip_token ();
   11231                 :           0 :           etc_attrs = std::move (outer_attrs);
   11232                 :           0 :           has_etc = true;
   11233                 :           0 :           break;
   11234                 :             :         }
   11235                 :             : 
   11236                 :         143 :       std::unique_ptr<AST::StructPatternField> field
   11237                 :         143 :         = parse_struct_pattern_field_partial (std::move (outer_attrs));
   11238                 :         143 :       if (field == nullptr)
   11239                 :             :         {
   11240                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
   11241                 :             :                        "failed to parse struct pattern field");
   11242                 :           0 :           add_error (std::move (error));
   11243                 :             : 
   11244                 :             :           // skip after somewhere?
   11245                 :           0 :           return AST::StructPatternElements::create_empty ();
   11246                 :           0 :         }
   11247                 :         143 :       fields.push_back (std::move (field));
   11248                 :             : 
   11249                 :         286 :       if (lexer.peek_token ()->get_id () != COMMA)
   11250                 :             :         break;
   11251                 :             : 
   11252                 :             :       // skip comma
   11253                 :          71 :       lexer.skip_token ();
   11254                 :          71 :       t = lexer.peek_token ();
   11255                 :             :     }
   11256                 :             : 
   11257                 :          86 :   if (has_etc)
   11258                 :           0 :     return AST::StructPatternElements (std::move (fields),
   11259                 :           0 :                                        std::move (etc_attrs));
   11260                 :             :   else
   11261                 :          86 :     return AST::StructPatternElements (std::move (fields));
   11262                 :          86 : }
   11263                 :             : 
   11264                 :             : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
   11265                 :             :  * identifier). */
   11266                 :             : template <typename ManagedTokenSource>
   11267                 :             : std::unique_ptr<AST::StructPatternField>
   11268                 :             : Parser<ManagedTokenSource>::parse_struct_pattern_field ()
   11269                 :             : {
   11270                 :             :   // parse outer attributes (if they exist)
   11271                 :             :   AST::AttrVec outer_attrs = parse_outer_attributes ();
   11272                 :             : 
   11273                 :             :   return parse_struct_pattern_field_partial (std::move (outer_attrs));
   11274                 :             : }
   11275                 :             : 
   11276                 :             : /* Parses a struct pattern field (tuple index/pattern, identifier/pattern, or
   11277                 :             :  * identifier), with outer attributes passed in. */
   11278                 :             : template <typename ManagedTokenSource>
   11279                 :             : std::unique_ptr<AST::StructPatternField>
   11280                 :         143 : Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
   11281                 :             :   AST::AttrVec outer_attrs)
   11282                 :             : {
   11283                 :             :   // branch based on next token
   11284                 :         143 :   const_TokenPtr t = lexer.peek_token ();
   11285                 :         143 :   switch (t->get_id ())
   11286                 :             :     {
   11287                 :           6 :       case INT_LITERAL: {
   11288                 :             :         // tuple index
   11289                 :           6 :         std::string index_str = t->get_str ();
   11290                 :           6 :         int index = atoi (index_str.c_str ());
   11291                 :             : 
   11292                 :           6 :         lexer.skip_token ();
   11293                 :             : 
   11294                 :           6 :         if (!skip_token (COLON))
   11295                 :             :           {
   11296                 :           0 :             return nullptr;
   11297                 :             :           }
   11298                 :             : 
   11299                 :             :         // parse required pattern
   11300                 :           6 :         std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   11301                 :           6 :         if (pattern == nullptr)
   11302                 :             :           {
   11303                 :           0 :             Error error (
   11304                 :             :               t->get_locus (),
   11305                 :             :               "failed to parse pattern in tuple index struct pattern field");
   11306                 :           0 :             add_error (std::move (error));
   11307                 :             : 
   11308                 :           0 :             return nullptr;
   11309                 :           0 :           }
   11310                 :             : 
   11311                 :           6 :         return std::unique_ptr<AST::StructPatternFieldTuplePat> (
   11312                 :           6 :           new AST::StructPatternFieldTuplePat (index, std::move (pattern),
   11313                 :             :                                                std::move (outer_attrs),
   11314                 :           6 :                                                t->get_locus ()));
   11315                 :          12 :       }
   11316                 :         137 :     case IDENTIFIER:
   11317                 :             :       // identifier-pattern OR only identifier
   11318                 :             :       // branch on next token
   11319                 :         274 :       switch (lexer.peek_token (1)->get_id ())
   11320                 :             :         {
   11321                 :          44 :           case COLON: {
   11322                 :             :             // identifier-pattern
   11323                 :          44 :             Identifier ident{t};
   11324                 :          44 :             lexer.skip_token ();
   11325                 :             : 
   11326                 :          44 :             skip_token (COLON);
   11327                 :             : 
   11328                 :             :             // parse required pattern
   11329                 :          44 :             std::unique_ptr<AST::Pattern> pattern = parse_pattern ();
   11330                 :          44 :             if (pattern == nullptr)
   11331                 :             :               {
   11332                 :           0 :                 Error error (t->get_locus (),
   11333                 :             :                              "failed to parse pattern in struct pattern field");
   11334                 :           0 :                 add_error (std::move (error));
   11335                 :             : 
   11336                 :           0 :                 return nullptr;
   11337                 :           0 :               }
   11338                 :             : 
   11339                 :          44 :             return std::unique_ptr<AST::StructPatternFieldIdentPat> (
   11340                 :          88 :               new AST::StructPatternFieldIdentPat (std::move (ident),
   11341                 :             :                                                    std::move (pattern),
   11342                 :             :                                                    std::move (outer_attrs),
   11343                 :          44 :                                                    t->get_locus ()));
   11344                 :          44 :           }
   11345                 :          93 :         case COMMA:
   11346                 :             :           case RIGHT_CURLY: {
   11347                 :             :             // identifier only
   11348                 :          93 :             Identifier ident = {t};
   11349                 :          93 :             lexer.skip_token ();
   11350                 :             : 
   11351                 :          93 :             return std::unique_ptr<AST::StructPatternFieldIdent> (
   11352                 :         186 :               new AST::StructPatternFieldIdent (std::move (ident), false, false,
   11353                 :             :                                                 std::move (outer_attrs),
   11354                 :          93 :                                                 t->get_locus ()));
   11355                 :          93 :           }
   11356                 :           0 :         default:
   11357                 :             :           // error
   11358                 :           0 :           add_error (Error (t->get_locus (),
   11359                 :             :                             "unrecognised token %qs in struct pattern field",
   11360                 :             :                             t->get_token_description ()));
   11361                 :             : 
   11362                 :           0 :           return nullptr;
   11363                 :             :         }
   11364                 :           0 :     case REF:
   11365                 :             :       case MUT: {
   11366                 :             :         // only identifier
   11367                 :           0 :         bool has_ref = false;
   11368                 :           0 :         if (t->get_id () == REF)
   11369                 :             :           {
   11370                 :           0 :             has_ref = true;
   11371                 :           0 :             lexer.skip_token ();
   11372                 :             :           }
   11373                 :             : 
   11374                 :           0 :         bool has_mut = false;
   11375                 :           0 :         if (lexer.peek_token ()->get_id () == MUT)
   11376                 :             :           {
   11377                 :           0 :             has_mut = true;
   11378                 :           0 :             lexer.skip_token ();
   11379                 :             :           }
   11380                 :             : 
   11381                 :           0 :         const_TokenPtr ident_tok = expect_token (IDENTIFIER);
   11382                 :           0 :         if (ident_tok == nullptr)
   11383                 :             :           {
   11384                 :           0 :             return nullptr;
   11385                 :             :           }
   11386                 :           0 :         Identifier ident{ident_tok};
   11387                 :             : 
   11388                 :           0 :         return std::unique_ptr<AST::StructPatternFieldIdent> (
   11389                 :           0 :           new AST::StructPatternFieldIdent (std::move (ident), has_ref, has_mut,
   11390                 :             :                                             std::move (outer_attrs),
   11391                 :           0 :                                             t->get_locus ()));
   11392                 :           0 :       }
   11393                 :           0 :     default:
   11394                 :             :       // not necessarily an error
   11395                 :           0 :       return nullptr;
   11396                 :             :     }
   11397                 :         143 : }
   11398                 :             : 
   11399                 :             : /* Parses a statement or expression (depending on whether a trailing semicolon
   11400                 :             :  * exists). Useful for block expressions where it cannot be determined through
   11401                 :             :  * lookahead whether it is a statement or expression to be parsed. */
   11402                 :             : template <typename ManagedTokenSource>
   11403                 :             : ExprOrStmt
   11404                 :       28720 : Parser<ManagedTokenSource>::parse_stmt_or_expr ()
   11405                 :             : {
   11406                 :             :   // quick exit for empty statement
   11407                 :       28720 :   const_TokenPtr t = lexer.peek_token ();
   11408                 :       28720 :   if (t->get_id () == SEMICOLON)
   11409                 :             :     {
   11410                 :          20 :       lexer.skip_token ();
   11411                 :          20 :       std::unique_ptr<AST::EmptyStmt> stmt (
   11412                 :          20 :         new AST::EmptyStmt (t->get_locus ()));
   11413                 :          20 :       return ExprOrStmt (std::move (stmt));
   11414                 :          20 :     }
   11415                 :             : 
   11416                 :             :   // parse outer attributes
   11417                 :       28700 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
   11418                 :       28700 :   ParseRestrictions restrictions;
   11419                 :       28700 :   restrictions.expr_can_be_stmt = true;
   11420                 :       28700 :   std::unique_ptr<AST::Expr> expr;
   11421                 :             : 
   11422                 :             :   // parsing this will be annoying because of the many different possibilities
   11423                 :             :   /* best may be just to copy paste in parse_item switch, and failing that try
   11424                 :             :    * to parse outer attributes, and then pass them in to either a let
   11425                 :             :    * statement or (fallback) expression statement. */
   11426                 :             :   // FIXME: think of a way to do this without such a large switch?
   11427                 :             : 
   11428                 :             :   /* FIXME: for expressions at least, the only way that they can really be
   11429                 :             :    * parsed properly in this way is if they don't support operators on them.
   11430                 :             :    * They must be pratt-parsed otherwise. As such due to composability, only
   11431                 :             :    * explicit statements will have special cases here. This should roughly
   11432                 :             :    * correspond to "expr-with-block", but this warning is here in case it
   11433                 :             :    * isn't the case. */
   11434                 :       28700 :   t = lexer.peek_token ();
   11435                 :       28700 :   switch (t->get_id ())
   11436                 :             :     {
   11437                 :       11630 :       case LET: {
   11438                 :             :         // let statement
   11439                 :       11630 :         std::unique_ptr<AST::LetStmt> stmt (
   11440                 :       11630 :           parse_let_stmt (std::move (outer_attrs)));
   11441                 :       11630 :         return ExprOrStmt (std::move (stmt));
   11442                 :       11630 :       }
   11443                 :         345 :     case PUB:
   11444                 :             :     case MOD:
   11445                 :             :     case EXTERN_KW:
   11446                 :             :     case USE:
   11447                 :             :     case FN_KW:
   11448                 :             :     case TYPE:
   11449                 :             :     case STRUCT_KW:
   11450                 :             :     case ENUM_KW:
   11451                 :             :     case CONST:
   11452                 :             :     case STATIC_KW:
   11453                 :             :     case AUTO:
   11454                 :             :     case TRAIT:
   11455                 :             :       case IMPL: {
   11456                 :         345 :         std::unique_ptr<AST::VisItem> item (
   11457                 :         345 :           parse_vis_item (std::move (outer_attrs)));
   11458                 :         345 :         return ExprOrStmt (std::move (item));
   11459                 :         345 :       }
   11460                 :             :       /* TODO: implement union keyword but not really because of
   11461                 :             :        * context-dependence crappy hack way to parse a union written below to
   11462                 :             :        * separate it from the good code. */
   11463                 :             :       // case UNION:
   11464                 :        2344 :       case UNSAFE: { // maybe - unsafe traits are a thing
   11465                 :             :         /* if any of these (should be all possible VisItem prefixes), parse a
   11466                 :             :          * VisItem - can't parse item because would require reparsing outer
   11467                 :             :          * attributes */
   11468                 :        2344 :         const_TokenPtr t2 = lexer.peek_token (1);
   11469                 :        2344 :         switch (t2->get_id ())
   11470                 :             :           {
   11471                 :        2330 :             case LEFT_CURLY: {
   11472                 :             :               // unsafe block: parse as expression
   11473                 :        2330 :               expr = parse_expr (std::move (outer_attrs), restrictions);
   11474                 :             :               break;
   11475                 :             :             }
   11476                 :           0 :           case AUTO:
   11477                 :             :             case TRAIT: {
   11478                 :             :               // unsafe trait
   11479                 :           0 :               std::unique_ptr<AST::VisItem> item (
   11480                 :           0 :                 parse_vis_item (std::move (outer_attrs)));
   11481                 :           0 :               return ExprOrStmt (std::move (item));
   11482                 :           0 :             }
   11483                 :          14 :           case EXTERN_KW:
   11484                 :             :             case FN_KW: {
   11485                 :             :               // unsafe function
   11486                 :          14 :               std::unique_ptr<AST::VisItem> item (
   11487                 :          14 :                 parse_vis_item (std::move (outer_attrs)));
   11488                 :          14 :               return ExprOrStmt (std::move (item));
   11489                 :          14 :             }
   11490                 :           0 :             case IMPL: {
   11491                 :             :               // unsafe trait impl
   11492                 :           0 :               std::unique_ptr<AST::VisItem> item (
   11493                 :           0 :                 parse_vis_item (std::move (outer_attrs)));
   11494                 :           0 :               return ExprOrStmt (std::move (item));
   11495                 :           0 :             }
   11496                 :           0 :           default:
   11497                 :           0 :             add_error (Error (t2->get_locus (),
   11498                 :             :                               "unrecognised token %qs after parsing unsafe - "
   11499                 :             :                               "expected beginning of expression or statement",
   11500                 :             :                               t->get_token_description ()));
   11501                 :             : 
   11502                 :             :             // skip somewhere?
   11503                 :             :             return ExprOrStmt::create_error ();
   11504                 :             :           }
   11505                 :             :         break;
   11506                 :        2344 :       }
   11507                 :             :       /* FIXME: this is either a macro invocation or macro invocation semi.
   11508                 :             :        * start parsing to determine which one it is. */
   11509                 :             :       // FIXME: old code there
   11510                 :             : 
   11511                 :             :     // crappy hack to do union "keyword"
   11512                 :        8470 :     case IDENTIFIER:
   11513                 :       16119 :       if (t->get_str () == Values::WeakKeywords::UNION
   11514                 :        8507 :           && lexer.peek_token (1)->get_id () == IDENTIFIER)
   11515                 :             :         {
   11516                 :           2 :           std::unique_ptr<AST::VisItem> item (
   11517                 :           2 :             parse_vis_item (std::move (outer_attrs)));
   11518                 :           2 :           return ExprOrStmt (std::move (item));
   11519                 :             :           // or should this go straight to parsing union?
   11520                 :           2 :         }
   11521                 :       16115 :       else if (t->get_str () == Values::WeakKeywords::MACRO_RULES
   11522                 :        8478 :                && lexer.peek_token (1)->get_id () == EXCLAM)
   11523                 :             :         {
   11524                 :             :           // macro_rules! macro item
   11525                 :          10 :           std::unique_ptr<AST::Item> item (
   11526                 :          10 :             parse_macro_rules_def (std::move (outer_attrs)));
   11527                 :          10 :           return ExprOrStmt (std::move (item));
   11528                 :          10 :         }
   11529                 :             :       gcc_fallthrough ();
   11530                 :             :     case SUPER:
   11531                 :             :     case SELF:
   11532                 :             :     case SELF_ALIAS:
   11533                 :             :     case CRATE:
   11534                 :             :     case SCOPE_RESOLUTION:
   11535                 :             :       case DOLLAR_SIGN: {
   11536                 :        9459 :         AST::PathInExpression path = parse_path_in_expression ();
   11537                 :        9459 :         std::unique_ptr<AST::Expr> null_denotation;
   11538                 :             : 
   11539                 :       18918 :         if (lexer.peek_token ()->get_id () == EXCLAM)
   11540                 :             :           {
   11541                 :         535 :             std::unique_ptr<AST::MacroInvocation> invoc
   11542                 :        1070 :               = parse_macro_invocation_partial (std::move (path),
   11543                 :             :                                                 std::move (outer_attrs));
   11544                 :             : 
   11545                 :         535 :             if (restrictions.consume_semi && maybe_skip_token (SEMICOLON))
   11546                 :             :               {
   11547                 :         444 :                 invoc->add_semicolon ();
   11548                 :             :                 // Macro invocation with semicolon.
   11549                 :             :                 return ExprOrStmt (
   11550                 :         444 :                   std::unique_ptr<AST::Stmt> (std::move (invoc)));
   11551                 :             :               }
   11552                 :             : 
   11553                 :         182 :             TokenId after_macro = lexer.peek_token ()->get_id ();
   11554                 :             : 
   11555                 :          91 :             if (invoc->get_invoc_data ().get_delim_tok_tree ().get_delim_type ()
   11556                 :             :                   == AST::CURLY
   11557                 :          91 :                 && after_macro != DOT && after_macro != QUESTION_MARK)
   11558                 :             :               {
   11559                 :           8 :                 rust_debug ("braced macro statement");
   11560                 :             :                 return ExprOrStmt (
   11561                 :           8 :                   std::unique_ptr<AST::Stmt> (std::move (invoc)));
   11562                 :             :               }
   11563                 :             : 
   11564                 :          83 :             null_denotation = std::move (invoc);
   11565                 :         535 :           }
   11566                 :             :         else
   11567                 :             :           {
   11568                 :             :             null_denotation
   11569                 :        8924 :               = null_denotation_path (std::move (path), {}, restrictions);
   11570                 :             :           }
   11571                 :             : 
   11572                 :        9007 :         expr = left_denotations (std::move (null_denotation), LBP_LOWEST,
   11573                 :             :                                  std::move (outer_attrs), restrictions);
   11574                 :             :         break;
   11575                 :        9459 :       }
   11576                 :        4910 :     default:
   11577                 :             :       /* expression statement or expression itself - parse
   11578                 :             :        * expression then make it statement if semi afterwards */
   11579                 :        4910 :       expr = parse_expr (std::move (outer_attrs), restrictions);
   11580                 :        4910 :       break;
   11581                 :             :     }
   11582                 :             : 
   11583                 :       16247 :   const_TokenPtr after_expr = lexer.peek_token ();
   11584                 :       16247 :   if (after_expr->get_id () == SEMICOLON)
   11585                 :             :     {
   11586                 :             :       // must be expression statement
   11587                 :        5626 :       lexer.skip_token ();
   11588                 :             : 
   11589                 :        5626 :       if (expr)
   11590                 :             :         {
   11591                 :        5624 :           std::unique_ptr<AST::ExprStmt> stmt (
   11592                 :        5624 :             new AST::ExprStmt (std::move (expr), t->get_locus (), true));
   11593                 :        5624 :           return ExprOrStmt (std::move (stmt));
   11594                 :        5624 :         }
   11595                 :             :       else
   11596                 :             :         {
   11597                 :             :           return ExprOrStmt::create_error ();
   11598                 :             :         }
   11599                 :             :     }
   11600                 :             : 
   11601                 :       10611 :   if (expr && !expr->is_expr_without_block ()
   11602                 :       14250 :       && after_expr->get_id () != RIGHT_CURLY)
   11603                 :             :     {
   11604                 :             :       // block expression statement.
   11605                 :        1047 :       std::unique_ptr<AST::ExprStmt> stmt (
   11606                 :        1047 :         new AST::ExprStmt (std::move (expr), t->get_locus (), false));
   11607                 :        1047 :       return ExprOrStmt (std::move (stmt));
   11608                 :        1047 :     }
   11609                 :             : 
   11610                 :             :   // return expression
   11611                 :        9574 :   return ExprOrStmt (std::move (expr));
   11612                 :       28700 : }
   11613                 :             : 
   11614                 :             : // Parses a struct expression field.
   11615                 :             : template <typename ManagedTokenSource>
   11616                 :             : std::unique_ptr<AST::StructExprField>
   11617                 :        1733 : Parser<ManagedTokenSource>::parse_struct_expr_field ()
   11618                 :             : {
   11619                 :        1733 :   AST::AttrVec outer_attrs = parse_outer_attributes ();
   11620                 :        1733 :   const_TokenPtr t = lexer.peek_token ();
   11621                 :        1733 :   switch (t->get_id ())
   11622                 :             :     {
   11623                 :        1687 :     case IDENTIFIER:
   11624                 :        3374 :       if (lexer.peek_token (1)->get_id () == COLON)
   11625                 :             :         {
   11626                 :             :           // struct expr field with identifier and expr
   11627                 :        1469 :           Identifier ident = {t};
   11628                 :        1469 :           lexer.skip_token (1);
   11629                 :             : 
   11630                 :             :           // parse expression (required)
   11631                 :        1469 :           std::unique_ptr<AST::Expr> expr = parse_expr ();
   11632                 :        1469 :           if (expr == nullptr)
   11633                 :             :             {
   11634                 :           0 :               Error error (t->get_locus (),
   11635                 :             :                            "failed to parse struct expression field with "
   11636                 :             :                            "identifier and expression");
   11637                 :           0 :               add_error (std::move (error));
   11638                 :             : 
   11639                 :           0 :               return nullptr;
   11640                 :           0 :             }
   11641                 :             : 
   11642                 :        1469 :           return std::unique_ptr<AST::StructExprFieldIdentifierValue> (
   11643                 :        2938 :             new AST::StructExprFieldIdentifierValue (std::move (ident),
   11644                 :             :                                                      std::move (expr),
   11645                 :             :                                                      std::move (outer_attrs),
   11646                 :        1469 :                                                      t->get_locus ()));
   11647                 :        1469 :         }
   11648                 :             :       else
   11649                 :             :         {
   11650                 :             :           // struct expr field with identifier only
   11651                 :         218 :           Identifier ident{t};
   11652                 :         218 :           lexer.skip_token ();
   11653                 :             : 
   11654                 :         218 :           return std::unique_ptr<AST::StructExprFieldIdentifier> (
   11655                 :         436 :             new AST::StructExprFieldIdentifier (std::move (ident),
   11656                 :             :                                                 std::move (outer_attrs),
   11657                 :         218 :                                                 t->get_locus ()));
   11658                 :         218 :         }
   11659                 :          46 :       case INT_LITERAL: {
   11660                 :             :         // parse tuple index field
   11661                 :          46 :         int index = atoi (t->get_str ().c_str ());
   11662                 :          46 :         lexer.skip_token ();
   11663                 :             : 
   11664                 :          46 :         if (!skip_token (COLON))
   11665                 :             :           {
   11666                 :             :             // skip somewhere?
   11667                 :           0 :             return nullptr;
   11668                 :             :           }
   11669                 :             : 
   11670                 :             :         // parse field expression (required)
   11671                 :          46 :         std::unique_ptr<AST::Expr> expr = parse_expr ();
   11672                 :          46 :         if (expr == nullptr)
   11673                 :             :           {
   11674                 :           0 :             Error error (t->get_locus (),
   11675                 :             :                          "failed to parse expr in struct (or enum) expr "
   11676                 :             :                          "field with tuple index");
   11677                 :           0 :             add_error (std::move (error));
   11678                 :             : 
   11679                 :           0 :             return nullptr;
   11680                 :           0 :           }
   11681                 :             : 
   11682                 :          46 :         return std::unique_ptr<AST::StructExprFieldIndexValue> (
   11683                 :          46 :           new AST::StructExprFieldIndexValue (index, std::move (expr),
   11684                 :             :                                               std::move (outer_attrs),
   11685                 :          46 :                                               t->get_locus ()));
   11686                 :          46 :       }
   11687                 :           0 :     case DOT_DOT:
   11688                 :             :       /* this is a struct base and can't be parsed here, so just return
   11689                 :             :        * nothing without erroring */
   11690                 :             : 
   11691                 :           0 :       return nullptr;
   11692                 :           0 :     default:
   11693                 :           0 :       add_error (
   11694                 :           0 :         Error (t->get_locus (),
   11695                 :             :                "unrecognised token %qs as first token of struct expr field - "
   11696                 :             :                "expected identifier or integer literal",
   11697                 :             :                t->get_token_description ()));
   11698                 :             : 
   11699                 :           0 :       return nullptr;
   11700                 :             :     }
   11701                 :        1733 : }
   11702                 :             : 
   11703                 :             : // "Unexpected token" panic mode - flags gcc error at unexpected token
   11704                 :             : template <typename ManagedTokenSource>
   11705                 :             : void
   11706                 :             : Parser<ManagedTokenSource>::unexpected_token (const_TokenPtr t)
   11707                 :             : {
   11708                 :             :   Error error (t->get_locus (), "unexpected token %qs\n",
   11709                 :             :                t->get_token_description ());
   11710                 :             :   add_error (std::move (error));
   11711                 :             : }
   11712                 :             : 
   11713                 :             : /* Crappy "error recovery" performed after error by skipping tokens until a
   11714                 :             :  * semi-colon is found */
   11715                 :             : template <typename ManagedTokenSource>
   11716                 :             : void
   11717                 :          18 : Parser<ManagedTokenSource>::skip_after_semicolon ()
   11718                 :             : {
   11719                 :          18 :   const_TokenPtr t = lexer.peek_token ();
   11720                 :             : 
   11721                 :          26 :   while (t->get_id () != END_OF_FILE && t->get_id () != SEMICOLON)
   11722                 :             :     {
   11723                 :           8 :       lexer.skip_token ();
   11724                 :           8 :       t = lexer.peek_token ();
   11725                 :             :     }
   11726                 :             : 
   11727                 :          18 :   if (t->get_id () == SEMICOLON)
   11728                 :           4 :     lexer.skip_token ();
   11729                 :          18 : }
   11730                 :             : 
   11731                 :             : /* Skips the current token */
   11732                 :             : template <typename ManagedTokenSource>
   11733                 :             : void
   11734                 :        6401 : Parser<ManagedTokenSource>::skip_token ()
   11735                 :             : {
   11736                 :        6401 :   lexer.skip_token ();
   11737                 :         479 : }
   11738                 :             : 
   11739                 :             : /* Checks if current token has inputted id - skips it and returns true if so,
   11740                 :             :  * diagnoses an error and returns false otherwise. */
   11741                 :             : template <typename ManagedTokenSource>
   11742                 :             : bool
   11743                 :      210997 : Parser<ManagedTokenSource>::skip_token (TokenId token_id)
   11744                 :             : {
   11745                 :      210997 :   return expect_token (token_id) != const_TokenPtr ();
   11746                 :             : }
   11747                 :             : 
   11748                 :             : /* Checks if current token is similar to inputted token - skips it and returns
   11749                 :             :  * true if so, diagnoses an error and returns false otherwise. */
   11750                 :             : template <typename ManagedTokenSource>
   11751                 :             : bool
   11752                 :        3651 : Parser<ManagedTokenSource>::skip_token (const_TokenPtr token)
   11753                 :             : {
   11754                 :       10896 :   return expect_token (token) != const_TokenPtr ();
   11755                 :             : }
   11756                 :             : 
   11757                 :             : /* Checks if current token has inputted id - skips it and returns true if so,
   11758                 :             :  * returns false otherwise without diagnosing an error */
   11759                 :             : template <typename ManagedTokenSource>
   11760                 :             : bool
   11761                 :       24573 : Parser<ManagedTokenSource>::maybe_skip_token (TokenId token_id)
   11762                 :             : {
   11763                 :       49146 :   if (lexer.peek_token ()->get_id () != token_id)
   11764                 :             :     return false;
   11765                 :             :   else
   11766                 :        1124 :     return skip_token (token_id);
   11767                 :             : }
   11768                 :             : 
   11769                 :             : /* Checks the current token - if id is same as expected, skips and returns it,
   11770                 :             :  * otherwise diagnoses error and returns null. */
   11771                 :             : template <typename ManagedTokenSource>
   11772                 :             : const_TokenPtr
   11773                 :      237869 : Parser<ManagedTokenSource>::expect_token (TokenId token_id)
   11774                 :             : {
   11775                 :      237869 :   const_TokenPtr t = lexer.peek_token ();
   11776                 :      237869 :   if (t->get_id () == token_id)
   11777                 :             :     {
   11778                 :      235124 :       lexer.skip_token ();
   11779                 :      235124 :       return t;
   11780                 :             :     }
   11781                 :             :   else
   11782                 :             :     {
   11783                 :        2745 :       Error error (t->get_locus (), "expecting %qs but %qs found",
   11784                 :             :                    get_token_description (token_id),
   11785                 :             :                    t->get_token_description ());
   11786                 :        2745 :       add_error (std::move (error));
   11787                 :             : 
   11788                 :        2745 :       return const_TokenPtr ();
   11789                 :        2745 :     }
   11790                 :      237869 : }
   11791                 :             : 
   11792                 :             : /* Checks the current token - if same as expected, skips and returns it,
   11793                 :             :  * otherwise diagnoses error and returns null. */
   11794                 :             : template <typename ManagedTokenSource>
   11795                 :             : const_TokenPtr
   11796                 :        3651 : Parser<ManagedTokenSource>::expect_token (const_TokenPtr token_expect)
   11797                 :             : {
   11798                 :        3651 :   const_TokenPtr t = lexer.peek_token ();
   11799                 :        3651 :   if (t->get_id () == token_expect->get_id ()
   11800                 :        3781 :       && (!t->should_have_str () || t->get_str () == token_expect->get_str ()))
   11801                 :             :     {
   11802                 :        3594 :       lexer.skip_token ();
   11803                 :        3594 :       return t;
   11804                 :             :     }
   11805                 :             :   else
   11806                 :             :     {
   11807                 :          57 :       Error error (t->get_locus (), "expecting %qs but %qs found",
   11808                 :             :                    token_expect->get_token_description (),
   11809                 :             :                    t->get_token_description ());
   11810                 :          57 :       add_error (std::move (error));
   11811                 :             : 
   11812                 :          57 :       return const_TokenPtr ();
   11813                 :          57 :     }
   11814                 :        3651 : }
   11815                 :             : 
   11816                 :             : // Skips all tokens until EOF or }. Don't use.
   11817                 :             : template <typename ManagedTokenSource>
   11818                 :             : void
   11819                 :             : Parser<ManagedTokenSource>::skip_after_end ()
   11820                 :             : {
   11821                 :             :   const_TokenPtr t = lexer.peek_token ();
   11822                 :             : 
   11823                 :             :   while (t->get_id () != END_OF_FILE && t->get_id () != RIGHT_CURLY)
   11824                 :             :     {
   11825                 :             :       lexer.skip_token ();
   11826                 :             :       t = lexer.peek_token ();
   11827                 :             :     }
   11828                 :             : 
   11829                 :             :   if (t->get_id () == RIGHT_CURLY)
   11830                 :             :     {
   11831                 :             :       lexer.skip_token ();
   11832                 :             :     }
   11833                 :             : }
   11834                 :             : 
   11835                 :             : /* A slightly more aware error-handler that skips all tokens until it reaches
   11836                 :             :  * the end of the block scope (i.e. when left curly brackets = right curly
   11837                 :             :  * brackets). Note: assumes currently in the middle of a block. Use
   11838                 :             :  * skip_after_next_block to skip based on the assumption that the block
   11839                 :             :  * has not been entered yet. */
   11840                 :             : template <typename ManagedTokenSource>
   11841                 :             : void
   11842                 :           2 : Parser<ManagedTokenSource>::skip_after_end_block ()
   11843                 :             : {
   11844                 :           2 :   const_TokenPtr t = lexer.peek_token ();
   11845                 :           2 :   int curly_count = 1;
   11846                 :             : 
   11847                 :           4 :   while (curly_count > 0 && t->get_id () != END_OF_FILE)
   11848                 :             :     {
   11849                 :           2 :       switch (t->get_id ())
   11850                 :             :         {
   11851                 :           0 :         case LEFT_CURLY:
   11852                 :           0 :           curly_count++;
   11853                 :           0 :           break;
   11854                 :           2 :         case RIGHT_CURLY:
   11855                 :           2 :           curly_count--;
   11856                 :           2 :           break;
   11857                 :             :         default:
   11858                 :             :           break;
   11859                 :             :         }
   11860                 :           2 :       lexer.skip_token ();
   11861                 :           2 :       t = lexer.peek_token ();
   11862                 :             :     }
   11863                 :           2 : }
   11864                 :             : 
   11865                 :             : /* Skips tokens until the end of the next block. i.e. assumes that the block
   11866                 :             :  * has not been entered yet. */
   11867                 :             : template <typename ManagedTokenSource>
   11868                 :             : void
   11869                 :           2 : Parser<ManagedTokenSource>::skip_after_next_block ()
   11870                 :             : {
   11871                 :           2 :   const_TokenPtr t = lexer.peek_token ();
   11872                 :             : 
   11873                 :             :   // initial loop - skip until EOF if no left curlies encountered
   11874                 :           2 :   while (t->get_id () != END_OF_FILE && t->get_id () != LEFT_CURLY)
   11875                 :             :     {
   11876                 :           0 :       lexer.skip_token ();
   11877                 :             : 
   11878                 :           0 :       t = lexer.peek_token ();
   11879                 :             :     }
   11880                 :             : 
   11881                 :             :   // if next token is left, skip it and then skip after the block ends
   11882                 :           2 :   if (t->get_id () == LEFT_CURLY)
   11883                 :             :     {
   11884                 :           2 :       lexer.skip_token ();
   11885                 :             : 
   11886                 :           2 :       skip_after_end_block ();
   11887                 :             :     }
   11888                 :             :   // otherwise, do nothing as EOF
   11889                 :           2 : }
   11890                 :             : 
   11891                 :             : /* Skips all tokens until ] (the end of an attribute) - does not skip the ]
   11892                 :             :  * (as designed for attribute body use) */
   11893                 :             : template <typename ManagedTokenSource>
   11894                 :             : void
   11895                 :           0 : Parser<ManagedTokenSource>::skip_after_end_attribute ()
   11896                 :             : {
   11897                 :           0 :   const_TokenPtr t = lexer.peek_token ();
   11898                 :             : 
   11899                 :           0 :   while (t->get_id () != RIGHT_SQUARE)
   11900                 :             :     {
   11901                 :           0 :       lexer.skip_token ();
   11902                 :           0 :       t = lexer.peek_token ();
   11903                 :             :     }
   11904                 :             : 
   11905                 :             :   // Don't skip the RIGHT_SQUARE token
   11906                 :           0 : }
   11907                 :             : 
   11908                 :             : /* Pratt parser impl of parse_expr. FIXME: this is only provisional and
   11909                 :             :  * probably will be changed. */
   11910                 :             : template <typename ManagedTokenSource>
   11911                 :             : std::unique_ptr<AST::Expr>
   11912                 :       49425 : Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
   11913                 :             :                                         AST::AttrVec outer_attrs,
   11914                 :             :                                         ParseRestrictions restrictions)
   11915                 :             : {
   11916                 :       49425 :   const_TokenPtr current_token = lexer.peek_token ();
   11917                 :             :   // Special hack because we are allowed to return nullptr, in that case we
   11918                 :             :   // don't want to skip the token, since we don't actually parse it. But if
   11919                 :             :   // null isn't allowed it indicates an error, and we want to skip past that.
   11920                 :             :   // So return early if it is one of the tokens that ends an expression
   11921                 :             :   // (or at least cannot start a new expression).
   11922                 :       49425 :   if (restrictions.expr_can_be_null)
   11923                 :             :     {
   11924                 :        1361 :       TokenId id = current_token->get_id ();
   11925                 :        1361 :       if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY
   11926                 :             :           || id == RIGHT_SQUARE || id == COMMA || id == LEFT_CURLY)
   11927                 :         103 :         return nullptr;
   11928                 :             :     }
   11929                 :             : 
   11930                 :       49322 :   if (current_token->get_id () == LEFT_SHIFT)
   11931                 :             :     {
   11932                 :           4 :       lexer.split_current_token (LEFT_ANGLE, LEFT_ANGLE);
   11933                 :           4 :       current_token = lexer.peek_token ();
   11934                 :             :     }
   11935                 :             : 
   11936                 :       49322 :   lexer.skip_token ();
   11937                 :             : 
   11938                 :       49322 :   ParseRestrictions null_denotation_restrictions = restrictions;
   11939                 :       49322 :   null_denotation_restrictions.expr_can_be_stmt = false;
   11940                 :             : 
   11941                 :             :   // parse null denotation (unary part of expression)
   11942                 :       49322 :   std::unique_ptr<AST::Expr> expr
   11943                 :      147966 :     = null_denotation (current_token, {}, null_denotation_restrictions);
   11944                 :             : 
   11945                 :       49322 :   return left_denotations (std::move (expr), right_binding_power,
   11946                 :       49322 :                            std::move (outer_attrs), restrictions);
   11947                 :       49322 : }
   11948                 :             : 
   11949                 :             : template <typename ManagedTokenSource>
   11950                 :             : std::unique_ptr<AST::Expr>
   11951                 :       58503 : Parser<ManagedTokenSource>::left_denotations (std::unique_ptr<AST::Expr> expr,
   11952                 :             :                                               int right_binding_power,
   11953                 :             :                                               AST::AttrVec outer_attrs,
   11954                 :             :                                               ParseRestrictions restrictions)
   11955                 :             : {
   11956                 :       58503 :   if (expr == nullptr)
   11957                 :             :     {
   11958                 :             :       // DEBUG
   11959                 :          32 :       rust_debug ("null denotation is null; returning null for parse_expr");
   11960                 :          32 :       return nullptr;
   11961                 :             :     }
   11962                 :             : 
   11963                 :       58471 :   const_TokenPtr current_token = lexer.peek_token ();
   11964                 :             : 
   11965                 :       17122 :   if (restrictions.expr_can_be_stmt && !expr->is_expr_without_block ()
   11966                 :        4278 :       && current_token->get_id () != DOT
   11967                 :       62739 :       && current_token->get_id () != QUESTION_MARK)
   11968                 :             :     {
   11969                 :        4268 :       rust_debug ("statement expression with block");
   11970                 :        4268 :       expr->set_outer_attrs (std::move (outer_attrs));
   11971                 :        4268 :       return expr;
   11972                 :             :     }
   11973                 :             : 
   11974                 :       70043 :   restrictions.expr_can_be_stmt = false;
   11975                 :             : 
   11976                 :             :   // stop parsing if find lower priority token - parse higher priority first
   11977                 :      210129 :   while (right_binding_power < left_binding_power (current_token))
   11978                 :             :     {
   11979                 :       15844 :       lexer.skip_token ();
   11980                 :             : 
   11981                 :             :       // FIXME attributes should generally be applied to the null denotation.
   11982                 :       47532 :       expr = left_denotation (current_token, std::move (expr),
   11983                 :             :                               std::move (outer_attrs), restrictions);
   11984                 :             : 
   11985                 :       15844 :       if (expr == nullptr)
   11986                 :             :         {
   11987                 :             :           // DEBUG
   11988                 :           4 :           rust_debug ("left denotation is null; returning null for parse_expr");
   11989                 :             : 
   11990                 :           4 :           return nullptr;
   11991                 :             :         }
   11992                 :             : 
   11993                 :       15840 :       current_token = lexer.peek_token ();
   11994                 :             :     }
   11995                 :             : 
   11996                 :       54199 :   return expr;
   11997                 :       58471 : }
   11998                 :             : 
   11999                 :             : // Parse expression with lowest left binding power.
   12000                 :             : template <typename ManagedTokenSource>
   12001                 :             : std::unique_ptr<AST::Expr>
   12002                 :       38499 : Parser<ManagedTokenSource>::parse_expr (AST::AttrVec outer_attrs,
   12003                 :             :                                         ParseRestrictions restrictions)
   12004                 :             : {
   12005                 :       38499 :   return parse_expr (LBP_LOWEST, std::move (outer_attrs), restrictions);
   12006                 :             : }
   12007                 :             : 
   12008                 :             : /* Determines action to take when finding token at beginning of expression. */
   12009                 :             : template <typename ManagedTokenSource>
   12010                 :             : std::unique_ptr<AST::Expr>
   12011                 :       49322 : Parser<ManagedTokenSource>::null_denotation (const_TokenPtr tok,
   12012                 :             :                                              AST::AttrVec outer_attrs,
   12013                 :             :                                              ParseRestrictions restrictions)
   12014                 :             : {
   12015                 :             :   /* note: tok is previous character in input stream, not current one, as
   12016                 :             :    * parse_expr skips it before passing it in */
   12017                 :             : 
   12018                 :             :   /* as a Pratt parser (which works by decomposing expressions into a null
   12019                 :             :    * denotation and then a left denotation), null denotations handle primaries
   12020                 :             :    * and unary operands (but only prefix unary operands) */
   12021                 :             : 
   12022                 :       49322 :   switch (tok->get_id ())
   12023                 :             :     {
   12024                 :       22273 :     case IDENTIFIER:
   12025                 :             :     case SELF:
   12026                 :             :     case SELF_ALIAS:
   12027                 :             :     case DOLLAR_SIGN:
   12028                 :             :     case CRATE:
   12029                 :             :       case SUPER: {
   12030                 :             :         // DEBUG
   12031                 :       22273 :         rust_debug ("beginning null denotation identifier handling");
   12032                 :             : 
   12033                 :             :         /* best option: parse as path, then extract identifier, macro,
   12034                 :             :          * struct/enum, or just path info from it */
   12035                 :       44546 :         AST::PathInExpression path = parse_path_in_expression_pratt (tok);
   12036                 :             : 
   12037                 :       44546 :         return null_denotation_path (std::move (path), std::move (outer_attrs),
   12038                 :       22273 :                                      restrictions);
   12039                 :       22273 :       }
   12040                 :           0 :       case SCOPE_RESOLUTION: {
   12041                 :             :         // TODO: fix: this is for global paths, i.e. std::string::whatever
   12042                 :           0 :         Error error (tok->get_locus (),
   12043                 :             :                      "found null denotation scope resolution operator, and "
   12044                 :             :                      "have not written handling for it");
   12045                 :           0 :         add_error (std::move (error));
   12046                 :             : 
   12047                 :           0 :         return nullptr;
   12048                 :           0 :       }
   12049                 :       27049 :     default:
   12050                 :       54098 :       return null_denotation_not_path (std::move (tok), std::move (outer_attrs),
   12051                 :       27049 :                                        restrictions);
   12052                 :             :     }
   12053                 :             : }
   12054                 :             : 
   12055                 :             : // Handling of expresions that start with a path for `null_denotation`.
   12056                 :             : template <typename ManagedTokenSource>
   12057                 :             : std::unique_ptr<AST::Expr>
   12058                 :       31371 : Parser<ManagedTokenSource>::null_denotation_path (
   12059                 :             :   AST::PathInExpression path, AST::AttrVec outer_attrs,
   12060                 :             :   ParseRestrictions restrictions)
   12061                 :             : {
   12062                 :       31371 :   rust_debug ("parsing null denotation after path");
   12063                 :             : 
   12064                 :             :   // HACK: always make "self" by itself a path (regardless of next
   12065                 :             :   // tokens)
   12066                 :       31371 :   if (path.is_single_segment () && path.get_segments ()[0].is_lower_self_seg ())
   12067                 :             :     {
   12068                 :             :       // HACK: add outer attrs to path
   12069                 :        3237 :       path.set_outer_attrs (std::move (outer_attrs));
   12070                 :        3237 :       return std::unique_ptr<AST::PathInExpression> (
   12071                 :        3237 :         new AST::PathInExpression (std::move (path)));
   12072                 :             :     }
   12073                 :             : 
   12074                 :             :   // branch on next token
   12075                 :       28134 :   const_TokenPtr t = lexer.peek_token ();
   12076                 :       28134 :   switch (t->get_id ())
   12077                 :             :     {
   12078                 :        2808 :     case EXCLAM:
   12079                 :             :       // macro
   12080                 :        5616 :       return parse_macro_invocation_partial (std::move (path),
   12081                 :        2808 :                                              std::move (outer_attrs));
   12082                 :        1560 :       case LEFT_CURLY: {
   12083                 :        1560 :         bool not_a_block = lexer.peek_token (1)->get_id () == IDENTIFIER
   12084                 :        4224 :                            && (lexer.peek_token (2)->get_id () == COMMA
   12085                 :        3630 :                                || (lexer.peek_token (2)->get_id () == COLON
   12086                 :        2756 :                                    && (lexer.peek_token (4)->get_id () == COMMA
   12087                 :         384 :                                        || !can_tok_start_type (
   12088                 :         816 :                                          lexer.peek_token (3)->get_id ()))));
   12089                 :             : 
   12090                 :             :         /* definitely not a block:
   12091                 :             :          *  path '{' ident ','
   12092                 :             :          *  path '{' ident ':' [anything] ','
   12093                 :             :          *  path '{' ident ':' [not a type]
   12094                 :             :          * otherwise, assume block expr and thus path */
   12095                 :             :         // DEBUG
   12096                 :        6240 :         rust_debug ("values of lookahead: '%s' '%s' '%s' '%s' ",
   12097                 :             :                     lexer.peek_token (1)->get_token_description (),
   12098                 :             :                     lexer.peek_token (2)->get_token_description (),
   12099                 :             :                     lexer.peek_token (3)->get_token_description (),
   12100                 :             :                     lexer.peek_token (4)->get_token_description ());
   12101                 :             : 
   12102                 :        2979 :         rust_debug ("can be struct expr: '%s', not a block: '%s'",
   12103                 :             :                     restrictions.can_be_struct_expr ? "true" : "false",
   12104                 :             :                     not_a_block ? "true" : "false");
   12105                 :             : 
   12106                 :             :         // struct/enum expr struct
   12107                 :        1560 :         if (!restrictions.can_be_struct_expr && !not_a_block)
   12108                 :             :           {
   12109                 :             :             // HACK: add outer attrs to path
   12110                 :         561 :             path.set_outer_attrs (std::move (outer_attrs));
   12111                 :         561 :             return std::unique_ptr<AST::PathInExpression> (
   12112                 :         561 :               new AST::PathInExpression (std::move (path)));
   12113                 :             :           }
   12114                 :         999 :         return parse_struct_expr_struct_partial (std::move (path),
   12115                 :         999 :                                                  std::move (outer_attrs));
   12116                 :             :       }
   12117                 :        7636 :     case LEFT_PAREN:
   12118                 :             :       // struct/enum expr tuple
   12119                 :        7636 :       if (!restrictions.can_be_struct_expr)
   12120                 :             :         {
   12121                 :             :           // assume path is returned
   12122                 :             :           // HACK: add outer attributes to path
   12123                 :         153 :           path.set_outer_attrs (std::move (outer_attrs));
   12124                 :         153 :           return std::unique_ptr<AST::PathInExpression> (
   12125                 :         153 :             new AST::PathInExpression (std::move (path)));
   12126                 :             :         }
   12127                 :        7483 :       return parse_struct_expr_tuple_partial (std::move (path),
   12128                 :        7483 :                                               std::move (outer_attrs));
   12129                 :       16130 :     default:
   12130                 :             :       // assume path is returned if not single segment
   12131                 :       16130 :       if (path.is_single_segment ())
   12132                 :             :         {
   12133                 :             :           // FIXME: This should probably be returned as a path.
   12134                 :             :           /* HACK: may have to become permanent, but this is my current
   12135                 :             :            * identifier expression */
   12136                 :       47370 :           return std::unique_ptr<AST::IdentifierExpr> (new AST::IdentifierExpr (
   12137                 :       31580 :             path.get_segments ()[0].get_ident_segment ().as_string (), {},
   12138                 :       15790 :             path.get_locus ()));
   12139                 :             :         }
   12140                 :             :       // HACK: add outer attrs to path
   12141                 :         340 :       path.set_outer_attrs (std::move (outer_attrs));
   12142                 :         340 :       return std::unique_ptr<AST::PathInExpression> (
   12143                 :         340 :         new AST::PathInExpression (std::move (path)));
   12144                 :             :     }
   12145                 :             :   rust_unreachable ();
   12146                 :       28134 : }
   12147                 :             : 
   12148                 :             : // Handling of expresions that do not start with a path for `null_denotation`.
   12149                 :             : template <typename ManagedTokenSource>
   12150                 :             : std::unique_ptr<AST::Expr>
   12151                 :       27049 : Parser<ManagedTokenSource>::null_denotation_not_path (
   12152                 :             :   const_TokenPtr tok, AST::AttrVec outer_attrs, ParseRestrictions restrictions)
   12153                 :             : {
   12154                 :       27049 :   switch (tok->get_id ())
   12155                 :             :     {
   12156                 :             :     // FIXME: Handle in null_denotation_path?
   12157                 :          89 :     case LEFT_SHIFT:
   12158                 :             :       case LEFT_ANGLE: {
   12159                 :             :         // qualified path
   12160                 :             :         // HACK: add outer attrs to path
   12161                 :          89 :         AST::QualifiedPathInExpression path
   12162                 :             :           = parse_qualified_path_in_expression (tok->get_locus ());
   12163                 :          89 :         path.set_outer_attrs (std::move (outer_attrs));
   12164                 :          89 :         return std::unique_ptr<AST::QualifiedPathInExpression> (
   12165                 :          89 :           new AST::QualifiedPathInExpression (std::move (path)));
   12166                 :          89 :       }
   12167                 :             :     // FIXME: delegate to parse_literal_expr instead? would have to rejig
   12168                 :             :     // tokens and whatever.
   12169                 :             :     // FIXME: for literal exprs, outer attrs should be passed in, and later
   12170                 :             :     // error if it does not make up the entire statement.
   12171                 :       14362 :     case INT_LITERAL:
   12172                 :             :       // we should check the range, but ignore for now
   12173                 :             :       // encode as int?
   12174                 :       14362 :       return std::unique_ptr<AST::LiteralExpr> (
   12175                 :       29837 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::INT,
   12176                 :       14362 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12177                 :         363 :     case FLOAT_LITERAL:
   12178                 :             :       // encode as float?
   12179                 :         363 :       return std::unique_ptr<AST::LiteralExpr> (
   12180                 :        1089 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::FLOAT,
   12181                 :         363 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12182                 :        1704 :     case STRING_LITERAL:
   12183                 :        1704 :       return std::unique_ptr<AST::LiteralExpr> (
   12184                 :        5112 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::STRING,
   12185                 :        1704 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12186                 :         118 :     case BYTE_STRING_LITERAL:
   12187                 :         118 :       return std::unique_ptr<AST::LiteralExpr> (
   12188                 :         354 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE_STRING,
   12189                 :         118 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12190                 :          48 :     case RAW_STRING_LITERAL:
   12191                 :          48 :       return std::unique_ptr<AST::LiteralExpr> (
   12192                 :         144 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::RAW_STRING,
   12193                 :          48 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12194                 :         218 :     case CHAR_LITERAL:
   12195                 :         218 :       return std::unique_ptr<AST::LiteralExpr> (
   12196                 :         654 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::CHAR,
   12197                 :         218 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12198                 :          65 :     case BYTE_CHAR_LITERAL:
   12199                 :          65 :       return std::unique_ptr<AST::LiteralExpr> (
   12200                 :         195 :         new AST::LiteralExpr (tok->get_str (), AST::Literal::BYTE,
   12201                 :          65 :                               tok->get_type_hint (), {}, tok->get_locus ()));
   12202                 :         427 :     case TRUE_LITERAL:
   12203                 :         427 :       return std::unique_ptr<AST::LiteralExpr> (
   12204                 :        1281 :         new AST::LiteralExpr (Values::Keywords::TRUE_LITERAL,
   12205                 :             :                               AST::Literal::BOOL, tok->get_type_hint (), {},
   12206                 :         427 :                               tok->get_locus ()));
   12207                 :         260 :     case FALSE_LITERAL:
   12208                 :         260 :       return std::unique_ptr<AST::LiteralExpr> (
   12209                 :         780 :         new AST::LiteralExpr (Values::Keywords::FALSE_LITERAL,
   12210                 :             :                               AST::Literal::BOOL, tok->get_type_hint (), {},
   12211                 :         260 :                               tok->get_locus ()));
   12212                 :         670 :     case LEFT_PAREN:
   12213                 :         670 :       return parse_grouped_or_tuple_expr (std::move (outer_attrs),
   12214                 :         670 :                                           tok->get_locus ());
   12215                 :             : 
   12216                 :             :       /*case PLUS: { // unary plus operator
   12217                 :             :           // invoke parse_expr recursively with appropriate priority, etc. for
   12218                 :             :       below AST::Expr* expr = parse_expr(LBP_UNARY_PLUS);
   12219                 :             : 
   12220                 :             :           if (expr == nullptr)
   12221                 :             :               return nullptr;
   12222                 :             :           // can only apply to integer and float expressions
   12223                 :             :           if (expr->get_type() != integer_type_node || expr->get_type() !=
   12224                 :             :       float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
   12225                 :             :       plus must be int or float but it is %s", print_type(expr->get_type()));
   12226                 :             :       return nullptr;
   12227                 :             :           }
   12228                 :             : 
   12229                 :             :           return Tree(expr, tok->get_locus());
   12230                 :             :       }*/
   12231                 :             :       // Rust has no unary plus operator
   12232                 :         221 :       case MINUS: { // unary minus
   12233                 :         221 :         ParseRestrictions entered_from_unary;
   12234                 :         221 :         entered_from_unary.entered_from_unary = true;
   12235                 :         221 :         if (!restrictions.can_be_struct_expr)
   12236                 :          14 :           entered_from_unary.can_be_struct_expr = false;
   12237                 :         221 :         std::unique_ptr<AST::Expr> expr
   12238                 :         221 :           = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary);
   12239                 :             : 
   12240                 :         221 :         if (expr == nullptr)
   12241                 :           0 :           return nullptr;
   12242                 :             :         // can only apply to integer and float expressions
   12243                 :             :         /*if (expr.get_type() != integer_type_node || expr.get_type() !=
   12244                 :             :         float_type_node) { rust_error_at(tok->get_locus(), "operand of unary
   12245                 :             :         minus must be int or float but it is %s",
   12246                 :             :         print_type(expr.get_type())); return Tree::error();
   12247                 :             :         }*/
   12248                 :             :         /* FIXME: when implemented the "get type" method on expr, ensure it is
   12249                 :             :          * int or float type (except unsigned int). Actually, this would
   12250                 :             :          * probably have to be done in semantic analysis (as type checking).
   12251                 :             :          */
   12252                 :             : 
   12253                 :             :         /* FIXME: allow outer attributes on these expressions by having an
   12254                 :             :          * outer attrs parameter in function*/
   12255                 :         221 :         return std::unique_ptr<AST::NegationExpr> (
   12256                 :         221 :           new AST::NegationExpr (std::move (expr), NegationOperator::NEGATE,
   12257                 :         221 :                                  std::move (outer_attrs), tok->get_locus ()));
   12258                 :         221 :       }
   12259                 :         117 :       case EXCLAM: { // logical or bitwise not
   12260                 :         117 :         ParseRestrictions entered_from_unary;
   12261                 :         117 :         entered_from_unary.entered_from_unary = true;
   12262                 :         117 :         if (!restrictions.can_be_struct_expr)
   12263                 :          70 :           entered_from_unary.can_be_struct_expr = false;
   12264                 :         117 :         std::unique_ptr<AST::Expr> expr
   12265                 :         117 :           = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary);
   12266                 :             : 
   12267                 :         117 :         if (expr == nullptr)
   12268                 :           0 :           return nullptr;
   12269                 :             :         // can only apply to boolean expressions
   12270                 :             :         /*if (expr.get_type() != boolean_type_node) {
   12271                 :             :             rust_error_at(tok->get_locus(),
   12272                 :             :               "operand of logical not must be a boolean but it is %s",
   12273                 :             :               print_type(expr.get_type()));
   12274                 :             :             return Tree::error();
   12275                 :             :         }*/
   12276                 :             :         /* FIXME: type checking for boolean or integer expressions in semantic
   12277                 :             :          * analysis */
   12278                 :             : 
   12279                 :             :         // FIXME: allow outer attributes on these expressions
   12280                 :         117 :         return std::unique_ptr<AST::NegationExpr> (
   12281                 :         117 :           new AST::NegationExpr (std::move (expr), NegationOperator::NOT,
   12282                 :         117 :                                  std::move (outer_attrs), tok->get_locus ()));
   12283                 :         117 :       }
   12284                 :        1480 :       case ASTERISK: {
   12285                 :             :         /* pointer dereference only - HACK: as struct expressions should
   12286                 :             :          * always be value expressions, cannot be dereferenced */
   12287                 :        1480 :         ParseRestrictions entered_from_unary;
   12288                 :        1480 :         entered_from_unary.entered_from_unary = true;
   12289                 :        1480 :         entered_from_unary.can_be_struct_expr = false;
   12290                 :        1480 :         std::unique_ptr<AST::Expr> expr
   12291                 :        1480 :           = parse_expr (LBP_UNARY_ASTERISK, {}, entered_from_unary);
   12292                 :             :         // FIXME: allow outer attributes on expression
   12293                 :        1480 :         return std::unique_ptr<AST::DereferenceExpr> (
   12294                 :        1480 :           new AST::DereferenceExpr (std::move (expr), std::move (outer_attrs),
   12295                 :        1480 :                                     tok->get_locus ()));
   12296                 :        1480 :       }
   12297                 :        1033 :       case AMP: {
   12298                 :             :         // (single) "borrow" expression - shared (mutable) or immutable
   12299                 :        1033 :         std::unique_ptr<AST::Expr> expr = nullptr;
   12300                 :        1033 :         Mutability mutability = Mutability::Imm;
   12301                 :        1033 :         bool raw_borrow = false;
   12302                 :             : 
   12303                 :        1033 :         ParseRestrictions entered_from_unary;
   12304                 :        1033 :         entered_from_unary.entered_from_unary = true;
   12305                 :        1033 :         if (!restrictions.can_be_struct_expr)
   12306                 :           0 :           entered_from_unary.can_be_struct_expr = false;
   12307                 :             : 
   12308                 :          10 :         auto is_mutability = [] (const_TokenPtr token) {
   12309                 :          10 :           return token->get_id () == CONST || token->get_id () == MUT;
   12310                 :             :         };
   12311                 :             : 
   12312                 :        1033 :         auto t = lexer.peek_token ();
   12313                 :             :         // Weak raw keyword, we look (1) ahead and treat it as an identifier if
   12314                 :             :         // there is no mut nor const.
   12315                 :        2066 :         if (t->get_id () == IDENTIFIER
   12316                 :         495 :             && t->get_str () == Values::WeakKeywords::RAW
   12317                 :        1061 :             && is_mutability (lexer.peek_token (1)))
   12318                 :             :           {
   12319                 :           6 :             lexer.skip_token ();
   12320                 :          12 :             switch (lexer.peek_token ()->get_id ())
   12321                 :             :               {
   12322                 :             :               case MUT:
   12323                 :             :                 mutability = Mutability::Mut;
   12324                 :             :                 break;
   12325                 :             :               case CONST:
   12326                 :           2 :                 mutability = Mutability::Imm;
   12327                 :             :                 break;
   12328                 :           0 :               default:
   12329                 :           0 :                 rust_error_at (lexer.peek_token ()->get_locus (),
   12330                 :             :                                "raw borrow should be either const or mut");
   12331                 :             :               }
   12332                 :           6 :             lexer.skip_token ();
   12333                 :           6 :             expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
   12334                 :           6 :             raw_borrow = true;
   12335                 :             :           }
   12336                 :        1027 :         else if (t->get_id () == MUT)
   12337                 :             :           {
   12338                 :         257 :             lexer.skip_token ();
   12339                 :         257 :             expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
   12340                 :         257 :             mutability = Mutability::Mut;
   12341                 :         257 :             raw_borrow = false;
   12342                 :             :           }
   12343                 :             :         else
   12344                 :             :           {
   12345                 :         770 :             expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
   12346                 :         770 :             raw_borrow = false;
   12347                 :             :           }
   12348                 :             : 
   12349                 :             :         // FIXME: allow outer attributes on expression
   12350                 :        1033 :         return std::unique_ptr<AST::BorrowExpr> (
   12351                 :        1033 :           new AST::BorrowExpr (std::move (expr), mutability, raw_borrow, false,
   12352                 :        1033 :                                std::move (outer_attrs), tok->get_locus ()));
   12353                 :        1033 :       }
   12354                 :          25 :       case LOGICAL_AND: {
   12355                 :             :         // (double) "borrow" expression - shared (mutable) or immutable
   12356                 :          25 :         std::unique_ptr<AST::Expr> expr = nullptr;
   12357                 :          25 :         Mutability mutability = Mutability::Imm;
   12358                 :             : 
   12359                 :          25 :         ParseRestrictions entered_from_unary;
   12360                 :          25 :         entered_from_unary.entered_from_unary = true;
   12361                 :             : 
   12362                 :          50 :         if (lexer.peek_token ()->get_id () == MUT)
   12363                 :             :           {
   12364                 :           0 :             lexer.skip_token ();
   12365                 :           0 :             expr = parse_expr (LBP_UNARY_AMP_MUT, {}, entered_from_unary);
   12366                 :           0 :             mutability = Mutability::Mut;
   12367                 :             :           }
   12368                 :             :         else
   12369                 :             :           {
   12370                 :          25 :             expr = parse_expr (LBP_UNARY_AMP, {}, entered_from_unary);
   12371                 :          25 :             mutability = Mutability::Imm;
   12372                 :             :           }
   12373                 :             : 
   12374                 :             :         // FIXME: allow outer attributes on expression
   12375                 :          25 :         return std::unique_ptr<AST::BorrowExpr> (
   12376                 :          25 :           new AST::BorrowExpr (std::move (expr), mutability, false, true,
   12377                 :          25 :                                std::move (outer_attrs), tok->get_locus ()));
   12378                 :          25 :       }
   12379                 :          73 :     case OR:
   12380                 :             :     case PIPE:
   12381                 :             :     case MOVE:
   12382                 :             :       // closure expression
   12383                 :         219 :       return parse_closure_expr_pratt (tok, std::move (outer_attrs));
   12384                 :          11 :     case DOT_DOT:
   12385                 :             :       // either "range to" or "range full" expressions
   12386                 :          33 :       return parse_nud_range_exclusive_expr (tok, std::move (outer_attrs));
   12387                 :           0 :     case DOT_DOT_EQ:
   12388                 :             :       // range to inclusive expr
   12389                 :           0 :       return parse_range_to_inclusive_expr (tok, std::move (outer_attrs));
   12390                 :         500 :     case RETURN_KW:
   12391                 :             :       // FIXME: is this really a null denotation expression?
   12392                 :         500 :       return parse_return_expr (std::move (outer_attrs), tok->get_locus ());
   12393                 :          89 :     case BREAK:
   12394                 :             :       // FIXME: is this really a null denotation expression?
   12395                 :          89 :       return parse_break_expr (std::move (outer_attrs), tok->get_locus ());
   12396                 :          13 :     case CONTINUE:
   12397                 :          13 :       return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
   12398                 :         844 :     case LEFT_CURLY:
   12399                 :             :       // ok - this is an expression with block for once.
   12400                 :         844 :       return parse_block_expr (std::move (outer_attrs),
   12401                 :        1688 :                                AST::LoopLabel::error (), tok->get_locus ());
   12402                 :         721 :     case IF:
   12403                 :             :       // if or if let, so more lookahead to find out
   12404                 :        1442 :       if (lexer.peek_token ()->get_id () == LET)
   12405                 :             :         {
   12406                 :             :           // if let expr
   12407                 :          36 :           return parse_if_let_expr (std::move (outer_attrs), tok->get_locus ());
   12408                 :             :         }
   12409                 :             :       else
   12410                 :             :         {
   12411                 :             :           // if expr
   12412                 :         685 :           return parse_if_expr (std::move (outer_attrs), tok->get_locus ());
   12413                 :             :         }
   12414                 :          40 :     case LIFETIME:
   12415                 :         120 :       return parse_labelled_loop_expr (tok, std::move (outer_attrs));
   12416                 :          76 :     case LOOP:
   12417                 :          76 :       return parse_loop_expr (std::move (outer_attrs), AST::LoopLabel::error (),
   12418                 :          76 :                               tok->get_locus ());
   12419                 :          48 :     case WHILE:
   12420                 :          96 :       if (lexer.peek_token ()->get_id () == LET)
   12421                 :             :         {
   12422                 :           2 :           return parse_while_let_loop_expr (std::move (outer_attrs));
   12423                 :             :         }
   12424                 :             :       else
   12425                 :             :         {
   12426                 :          46 :           return parse_while_loop_expr (std::move (outer_attrs),
   12427                 :          92 :                                         AST::LoopLabel::error (),
   12428                 :          46 :                                         tok->get_locus ());
   12429                 :             :         }
   12430                 :           6 :     case FOR:
   12431                 :           6 :       return parse_for_loop_expr (std::move (outer_attrs),
   12432                 :          12 :                                   AST::LoopLabel::error ());
   12433                 :         362 :     case MATCH_KW:
   12434                 :             :       // also an expression with block
   12435                 :         362 :       return parse_match_expr (std::move (outer_attrs), tok->get_locus ());
   12436                 :         340 :     case LEFT_SQUARE:
   12437                 :             :       // array definition expr (not indexing)
   12438                 :         340 :       return parse_array_expr (std::move (outer_attrs), tok->get_locus ());
   12439                 :        2698 :     case UNSAFE:
   12440                 :        2698 :       return parse_unsafe_block_expr (std::move (outer_attrs),
   12441                 :        2698 :                                       tok->get_locus ());
   12442                 :           4 :     case BOX:
   12443                 :           4 :       return parse_box_expr (std::move (outer_attrs), tok->get_locus ());
   12444                 :           2 :     case UNDERSCORE:
   12445                 :           2 :       add_error (
   12446                 :           4 :         Error (tok->get_locus (),
   12447                 :             :                "use of %qs is not allowed on the right-side of an assignment",
   12448                 :             :                tok->get_token_description ()));
   12449                 :           2 :       return nullptr;
   12450                 :          22 :     default:
   12451                 :          22 :       if (!restrictions.expr_can_be_null)
   12452                 :          22 :         add_error (Error (tok->get_locus (),
   12453                 :             :                           "found unexpected token %qs in null denotation",
   12454                 :             :                           tok->get_token_description ()));
   12455                 :          22 :       return nullptr;
   12456                 :             :     }
   12457                 :             : }
   12458                 :             : 
   12459                 :             : /* Called for each token that can appear in infix (between) position. Can be
   12460                 :             :  * operators or other punctuation. Returns a function pointer to member
   12461                 :             :  * function that implements the left denotation for the token given. */
   12462                 :             : template <typename ManagedTokenSource>
   12463                 :             : std::unique_ptr<AST::Expr>
   12464                 :       15844 : Parser<ManagedTokenSource>::left_denotation (const_TokenPtr tok,
   12465                 :             :                                              std::unique_ptr<AST::Expr> left,
   12466                 :             :                                              AST::AttrVec outer_attrs,
   12467                 :             :                                              ParseRestrictions restrictions)
   12468                 :             : {
   12469                 :             :   // Token passed in has already been skipped, so peek gives "next" token
   12470                 :       15844 :   switch (tok->get_id ())
   12471                 :             :     {
   12472                 :             :       // FIXME: allow for outer attributes to be applied
   12473                 :           0 :       case QUESTION_MARK: {
   12474                 :           0 :         location_t left_locus = left->get_locus ();
   12475                 :             :         // error propagation expression - unary postfix
   12476                 :           0 :         return std::unique_ptr<AST::ErrorPropagationExpr> (
   12477                 :           0 :           new AST::ErrorPropagationExpr (std::move (left),
   12478                 :           0 :                                          std::move (outer_attrs), left_locus));
   12479                 :             :       }
   12480                 :        3756 :     case PLUS:
   12481                 :             :       // sum expression - binary infix
   12482                 :             :       /*return parse_binary_plus_expr (tok, std::move (left),
   12483                 :             :                                      std::move (outer_attrs), restrictions);*/
   12484                 :       11268 :       return parse_arithmetic_or_logical_expr (tok, std::move (left),
   12485                 :             :                                                std::move (outer_attrs),
   12486                 :             :                                                ArithmeticOrLogicalOperator::ADD,
   12487                 :        3756 :                                                restrictions);
   12488                 :         686 :     case MINUS:
   12489                 :             :       // difference expression - binary infix
   12490                 :             :       /*return parse_binary_minus_expr (tok, std::move (left),
   12491                 :             :                                       std::move (outer_attrs),
   12492                 :             :          restrictions);*/
   12493                 :        2058 :       return parse_arithmetic_or_logical_expr (
   12494                 :             :         tok, std::move (left), std::move (outer_attrs),
   12495                 :         686 :         ArithmeticOrLogicalOperator::SUBTRACT, restrictions);
   12496                 :         183 :     case ASTERISK:
   12497                 :             :       // product expression - binary infix
   12498                 :             :       /*return parse_binary_mult_expr (tok, std::move (left),
   12499                 :             :                                      std::move (outer_attrs), restrictions);*/
   12500                 :         549 :       return parse_arithmetic_or_logical_expr (
   12501                 :             :         tok, std::move (left), std::move (outer_attrs),
   12502                 :         183 :         ArithmeticOrLogicalOperator::MULTIPLY, restrictions);
   12503                 :          38 :     case DIV:
   12504                 :             :       // quotient expression - binary infix
   12505                 :             :       /*return parse_binary_div_expr (tok, std::move (left),
   12506                 :             :                                     std::move (outer_attrs), restrictions);*/
   12507                 :         114 :       return parse_arithmetic_or_logical_expr (
   12508                 :             :         tok, std::move (left), std::move (outer_attrs),
   12509                 :          38 :         ArithmeticOrLogicalOperator::DIVIDE, restrictions);
   12510                 :          37 :     case PERCENT:
   12511                 :             :       // modulo expression - binary infix
   12512                 :             :       /*return parse_binary_mod_expr (tok, std::move (left),
   12513                 :             :                                     std::move (outer_attrs), restrictions);*/
   12514                 :         111 :       return parse_arithmetic_or_logical_expr (
   12515                 :             :         tok, std::move (left), std::move (outer_attrs),
   12516                 :          37 :         ArithmeticOrLogicalOperator::MODULUS, restrictions);
   12517                 :          40 :     case AMP:
   12518                 :             :       // logical or bitwise and expression - binary infix
   12519                 :             :       /*return parse_bitwise_and_expr (tok, std::move (left),
   12520                 :             :                                      std::move (outer_attrs), restrictions);*/
   12521                 :         120 :       return parse_arithmetic_or_logical_expr (
   12522                 :             :         tok, std::move (left), std::move (outer_attrs),
   12523                 :          40 :         ArithmeticOrLogicalOperator::BITWISE_AND, restrictions);
   12524                 :          22 :     case PIPE:
   12525                 :             :       // logical or bitwise or expression - binary infix
   12526                 :             :       /*return parse_bitwise_or_expr (tok, std::move (left),
   12527                 :             :                                     std::move (outer_attrs), restrictions);*/
   12528                 :          66 :       return parse_arithmetic_or_logical_expr (
   12529                 :             :         tok, std::move (left), std::move (outer_attrs),
   12530                 :          22 :         ArithmeticOrLogicalOperator::BITWISE_OR, restrictions);
   12531                 :          14 :     case CARET:
   12532                 :             :       // logical or bitwise xor expression - binary infix
   12533                 :             :       /*return parse_bitwise_xor_expr (tok, std::move (left),
   12534                 :             :                                      std::move (outer_attrs), restrictions);*/
   12535                 :          42 :       return parse_arithmetic_or_logical_expr (
   12536                 :             :         tok, std::move (left), std::move (outer_attrs),
   12537                 :          14 :         ArithmeticOrLogicalOperator::BITWISE_XOR, restrictions);
   12538                 :          24 :     case LEFT_SHIFT:
   12539                 :             :       // left shift expression - binary infix
   12540                 :             :       /*return parse_left_shift_expr (tok, std::move (left),
   12541                 :             :                                     std::move (outer_attrs), restrictions);*/
   12542                 :          72 :       return parse_arithmetic_or_logical_expr (
   12543                 :             :         tok, std::move (left), std::move (outer_attrs),
   12544                 :          24 :         ArithmeticOrLogicalOperator::LEFT_SHIFT, restrictions);
   12545                 :          18 :     case RIGHT_SHIFT:
   12546                 :             :       // right shift expression - binary infix
   12547                 :             :       /*return parse_right_shift_expr (tok, std::move (left),
   12548                 :             :                                      std::move (outer_attrs), restrictions);*/
   12549                 :          54 :       return parse_arithmetic_or_logical_expr (
   12550                 :             :         tok, std::move (left), std::move (outer_attrs),
   12551                 :          18 :         ArithmeticOrLogicalOperator::RIGHT_SHIFT, restrictions);
   12552                 :         371 :     case EQUAL_EQUAL:
   12553                 :             :       // equal to expression - binary infix (no associativity)
   12554                 :             :       /*return parse_binary_equal_expr (tok, std::move (left),
   12555                 :             :                                       std::move (outer_attrs),
   12556                 :             :          restrictions);*/
   12557                 :        1113 :       return parse_comparison_expr (tok, std::move (left),
   12558                 :             :                                     std::move (outer_attrs),
   12559                 :         371 :                                     ComparisonOperator::EQUAL, restrictions);
   12560                 :         132 :     case NOT_EQUAL:
   12561                 :             :       // not equal to expression - binary infix (no associativity)
   12562                 :             :       /*return parse_binary_not_equal_expr (tok, std::move (left),
   12563                 :             :                                           std::move (outer_attrs),
   12564                 :             :                                           restrictions);*/
   12565                 :         396 :       return parse_comparison_expr (tok, std::move (left),
   12566                 :             :                                     std::move (outer_attrs),
   12567                 :             :                                     ComparisonOperator::NOT_EQUAL,
   12568                 :         132 :                                     restrictions);
   12569                 :         222 :     case RIGHT_ANGLE:
   12570                 :             :       // greater than expression - binary infix (no associativity)
   12571                 :             :       /*return parse_binary_greater_than_expr (tok, std::move (left),
   12572                 :             :                                              std::move (outer_attrs),
   12573                 :             :                                              restrictions);*/
   12574                 :         666 :       return parse_comparison_expr (tok, std::move (left),
   12575                 :             :                                     std::move (outer_attrs),
   12576                 :             :                                     ComparisonOperator::GREATER_THAN,
   12577                 :         222 :                                     restrictions);
   12578                 :          88 :     case LEFT_ANGLE:
   12579                 :             :       // less than expression - binary infix (no associativity)
   12580                 :             :       /*return parse_binary_less_than_expr (tok, std::move (left),
   12581                 :             :                                           std::move (outer_attrs),
   12582                 :             :                                           restrictions);*/
   12583                 :         264 :       return parse_comparison_expr (tok, std::move (left),
   12584                 :             :                                     std::move (outer_attrs),
   12585                 :             :                                     ComparisonOperator::LESS_THAN,
   12586                 :          88 :                                     restrictions);
   12587                 :          14 :     case GREATER_OR_EQUAL:
   12588                 :             :       // greater than or equal to expression - binary infix (no associativity)
   12589                 :             :       /*return parse_binary_greater_equal_expr (tok, std::move (left),
   12590                 :             :                                               std::move (outer_attrs),
   12591                 :             :                                               restrictions);*/
   12592                 :          42 :       return parse_comparison_expr (tok, std::move (left),
   12593                 :             :                                     std::move (outer_attrs),
   12594                 :             :                                     ComparisonOperator::GREATER_OR_EQUAL,
   12595                 :          14 :                                     restrictions);
   12596                 :          62 :     case LESS_OR_EQUAL:
   12597                 :             :       // less than or equal to expression - binary infix (no associativity)
   12598                 :             :       /*return parse_binary_less_equal_expr (tok, std::move (left),
   12599                 :             :                                            std::move (outer_attrs),
   12600                 :             :                                            restrictions);*/
   12601                 :         186 :       return parse_comparison_expr (tok, std::move (left),
   12602                 :             :                                     std::move (outer_attrs),
   12603                 :             :                                     ComparisonOperator::LESS_OR_EQUAL,
   12604                 :          62 :                                     restrictions);
   12605                 :          56 :     case OR:
   12606                 :             :       // lazy logical or expression - binary infix
   12607                 :         168 :       return parse_lazy_or_expr (tok, std::move (left), std::move (outer_attrs),
   12608                 :          56 :                                  restrictions);
   12609                 :         231 :     case LOGICAL_AND:
   12610                 :             :       // lazy logical and expression - binary infix
   12611                 :         693 :       return parse_lazy_and_expr (tok, std::move (left),
   12612                 :         231 :                                   std::move (outer_attrs), restrictions);
   12613                 :        3709 :     case AS:
   12614                 :             :       /* type cast expression - kind of binary infix (RHS is actually a
   12615                 :             :        * TypeNoBounds) */
   12616                 :       11127 :       return parse_type_cast_expr (tok, std::move (left),
   12617                 :        3709 :                                    std::move (outer_attrs), restrictions);
   12618                 :        1763 :     case EQUAL:
   12619                 :             :       // assignment expression - binary infix (note right-to-left
   12620                 :             :       // associativity)
   12621                 :        5289 :       return parse_assig_expr (tok, std::move (left), std::move (outer_attrs),
   12622                 :        1763 :                                restrictions);
   12623                 :          95 :     case PLUS_EQ:
   12624                 :             :       /* plus-assignment expression - binary infix (note right-to-left
   12625                 :             :        * associativity) */
   12626                 :             :       /*return parse_plus_assig_expr (tok, std::move (left),
   12627                 :             :                                     std::move (outer_attrs), restrictions);*/
   12628                 :         285 :       return parse_compound_assignment_expr (tok, std::move (left),
   12629                 :             :                                              std::move (outer_attrs),
   12630                 :             :                                              CompoundAssignmentOperator::ADD,
   12631                 :          95 :                                              restrictions);
   12632                 :          42 :     case MINUS_EQ:
   12633                 :             :       /* minus-assignment expression - binary infix (note right-to-left
   12634                 :             :        * associativity) */
   12635                 :             :       /*return parse_minus_assig_expr (tok, std::move (left),
   12636                 :             :                                      std::move (outer_attrs), restrictions);*/
   12637                 :         126 :       return parse_compound_assignment_expr (
   12638                 :             :         tok, std::move (left), std::move (outer_attrs),
   12639                 :          42 :         CompoundAssignmentOperator::SUBTRACT, restrictions);
   12640                 :           7 :     case ASTERISK_EQ:
   12641                 :             :       /* multiply-assignment expression - binary infix (note right-to-left
   12642                 :             :        * associativity) */
   12643                 :             :       /*return parse_mult_assig_expr (tok, std::move (left),
   12644                 :             :                                     std::move (outer_attrs), restrictions);*/
   12645                 :          21 :       return parse_compound_assignment_expr (
   12646                 :             :         tok, std::move (left), std::move (outer_attrs),
   12647                 :           7 :         CompoundAssignmentOperator::MULTIPLY, restrictions);
   12648                 :           7 :     case DIV_EQ:
   12649                 :             :       /* division-assignment expression - binary infix (note right-to-left
   12650                 :             :        * associativity) */
   12651                 :             :       /*return parse_div_assig_expr (tok, std::move (left),
   12652                 :             :                                    std::move (outer_attrs), restrictions);*/
   12653                 :          21 :       return parse_compound_assignment_expr (tok, std::move (left),
   12654                 :             :                                              std::move (outer_attrs),
   12655                 :             :                                              CompoundAssignmentOperator::DIVIDE,
   12656                 :           7 :                                              restrictions);
   12657                 :           7 :     case PERCENT_EQ:
   12658                 :             :       /* modulo-assignment expression - binary infix (note right-to-left
   12659                 :             :        * associativity) */
   12660                 :             :       /*return parse_mod_assig_expr (tok, std::move (left),
   12661                 :             :                                    std::move (outer_attrs), restrictions);*/
   12662                 :          21 :       return parse_compound_assignment_expr (
   12663                 :             :         tok, std::move (left), std::move (outer_attrs),
   12664                 :           7 :         CompoundAssignmentOperator::MODULUS, restrictions);
   12665                 :          21 :     case AMP_EQ:
   12666                 :             :       /* bitwise and-assignment expression - binary infix (note right-to-left
   12667                 :             :        * associativity) */
   12668                 :             :       /*return parse_and_assig_expr (tok, std::move (left),
   12669                 :             :                                    std::move (outer_attrs), restrictions);*/
   12670                 :          63 :       return parse_compound_assignment_expr (
   12671                 :             :         tok, std::move (left), std::move (outer_attrs),
   12672                 :          21 :         CompoundAssignmentOperator::BITWISE_AND, restrictions);
   12673                 :           7 :     case PIPE_EQ:
   12674                 :             :       /* bitwise or-assignment expression - binary infix (note right-to-left
   12675                 :             :        * associativity) */
   12676                 :             :       /*return parse_or_assig_expr (tok, std::move (left),
   12677                 :             :                                   std::move (outer_attrs), restrictions);*/
   12678                 :          21 :       return parse_compound_assignment_expr (
   12679                 :             :         tok, std::move (left), std::move (outer_attrs),
   12680                 :           7 :         CompoundAssignmentOperator::BITWISE_OR, restrictions);
   12681                 :           7 :     case CARET_EQ:
   12682                 :             :       /* bitwise xor-assignment expression - binary infix (note right-to-left
   12683                 :             :        * associativity) */
   12684                 :             :       /*return parse_xor_assig_expr (tok, std::move (left),
   12685                 :             :                                    std::move (outer_attrs), restrictions);*/
   12686                 :          21 :       return parse_compound_assignment_expr (
   12687                 :             :         tok, std::move (left), std::move (outer_attrs),
   12688                 :           7 :         CompoundAssignmentOperator::BITWISE_XOR, restrictions);
   12689                 :           7 :     case LEFT_SHIFT_EQ:
   12690                 :             :       /* left shift-assignment expression - binary infix (note right-to-left
   12691                 :             :        * associativity) */
   12692                 :             :       /*return parse_left_shift_assig_expr (tok, std::move (left),
   12693                 :             :                                           std::move (outer_attrs),
   12694                 :             :                                           restrictions);*/
   12695                 :          21 :       return parse_compound_assignment_expr (
   12696                 :             :         tok, std::move (left), std::move (outer_attrs),
   12697                 :           7 :         CompoundAssignmentOperator::LEFT_SHIFT, restrictions);
   12698                 :           7 :     case RIGHT_SHIFT_EQ:
   12699                 :             :       /* right shift-assignment expression - binary infix (note right-to-left
   12700                 :             :        * associativity) */
   12701                 :             :       /*return parse_right_shift_assig_expr (tok, std::move (left),
   12702                 :             :                                            std::move (outer_attrs),
   12703                 :             :                                            restrictions);*/
   12704                 :          21 :       return parse_compound_assignment_expr (
   12705                 :             :         tok, std::move (left), std::move (outer_attrs),
   12706                 :           7 :         CompoundAssignmentOperator::RIGHT_SHIFT, restrictions);
   12707                 :          68 :     case DOT_DOT:
   12708                 :             :       /* range exclusive expression - binary infix (no associativity)
   12709                 :             :        * either "range" or "range from" */
   12710                 :         204 :       return parse_led_range_exclusive_expr (tok, std::move (left),
   12711                 :             :                                              std::move (outer_attrs),
   12712                 :          68 :                                              restrictions);
   12713                 :           7 :     case DOT_DOT_EQ:
   12714                 :             :       /* range inclusive expression - binary infix (no associativity)
   12715                 :             :        * unambiguously RangeInclusiveExpr */
   12716                 :          21 :       return parse_range_inclusive_expr (tok, std::move (left),
   12717                 :           7 :                                          std::move (outer_attrs), restrictions);
   12718                 :           0 :     case SCOPE_RESOLUTION:
   12719                 :             :       // path expression - binary infix? FIXME should this even be parsed
   12720                 :             :       // here?
   12721                 :           0 :       add_error (
   12722                 :           0 :         Error (tok->get_locus (),
   12723                 :             :                "found scope resolution operator in left denotation "
   12724                 :             :                "function - this should probably be handled elsewhere"));
   12725                 :             : 
   12726                 :           0 :       return nullptr;
   12727                 :        3627 :       case DOT: {
   12728                 :             :         /* field expression or method call - relies on parentheses after next
   12729                 :             :          * identifier or await if token after is "await" (unary postfix) or
   12730                 :             :          * tuple index if token after is a decimal int literal */
   12731                 :             : 
   12732                 :        3627 :         const_TokenPtr next_tok = lexer.peek_token ();
   12733                 :        3627 :         if (next_tok->get_id () == IDENTIFIER
   12734                 :        3627 :             && next_tok->get_str () == Values::Keywords::AWAIT)
   12735                 :             :           {
   12736                 :             :             // await expression
   12737                 :           0 :             return parse_await_expr (tok, std::move (left),
   12738                 :           0 :                                      std::move (outer_attrs));
   12739                 :             :           }
   12740                 :        3627 :         else if (next_tok->get_id () == INT_LITERAL)
   12741                 :             :           {
   12742                 :             :             // tuple index expression - TODO check for decimal int literal
   12743                 :        2571 :             return parse_tuple_index_expr (tok, std::move (left),
   12744                 :             :                                            std::move (outer_attrs),
   12745                 :         857 :                                            restrictions);
   12746                 :             :           }
   12747                 :        2770 :         else if (next_tok->get_id () == FLOAT_LITERAL)
   12748                 :             :           {
   12749                 :             :             // Lexer has misidentified a tuple index as a float literal
   12750                 :             :             // eg: `(x, (y, z)).1.0` -> 1.0 has been identified as a float
   12751                 :             :             // literal. This means we should split it into three new separate
   12752                 :             :             // tokens, the first tuple index, the dot and the second tuple
   12753                 :             :             // index.
   12754                 :           4 :             auto current_loc = next_tok->get_locus ();
   12755                 :           4 :             auto str = next_tok->get_str ();
   12756                 :           4 :             auto dot_pos = str.find (".");
   12757                 :           4 :             auto prefix = str.substr (0, dot_pos);
   12758                 :           4 :             auto suffix = str.substr (dot_pos + 1);
   12759                 :           4 :             if (dot_pos == str.size () - 1)
   12760                 :           6 :               lexer.split_current_token (
   12761                 :             :                 {Token::make_int (current_loc, std::move (prefix),
   12762                 :             :                                   CORETYPE_PURE_DECIMAL),
   12763                 :             :                  Token::make (DOT, current_loc + 1)});
   12764                 :             :             else
   12765                 :           8 :               lexer.split_current_token (
   12766                 :             :                 {Token::make_int (current_loc, std::move (prefix),
   12767                 :             :                                   CORETYPE_PURE_DECIMAL),
   12768                 :             :                  Token::make (DOT, current_loc + 1),
   12769                 :             :                  Token::make_int (current_loc + 2, std::move (suffix),
   12770                 :             :                                   CORETYPE_PURE_DECIMAL)});
   12771                 :          12 :             return parse_tuple_index_expr (tok, std::move (left),
   12772                 :             :                                            std::move (outer_attrs),
   12773                 :           4 :                                            restrictions);
   12774                 :           4 :           }
   12775                 :        5484 :         else if (next_tok->get_id () == IDENTIFIER
   12776                 :        7073 :                  && lexer.peek_token (1)->get_id () != LEFT_PAREN
   12777                 :        5896 :                  && lexer.peek_token (1)->get_id () != SCOPE_RESOLUTION)
   12778                 :             :           {
   12779                 :             :             /* field expression (or should be) - FIXME: scope resolution right
   12780                 :             :              * after identifier should always be method, I'm pretty sure */
   12781                 :        4554 :             return parse_field_access_expr (tok, std::move (left),
   12782                 :             :                                             std::move (outer_attrs),
   12783                 :        1518 :                                             restrictions);
   12784                 :             :           }
   12785                 :             :         else
   12786                 :             :           {
   12787                 :             :             // method call (probably)
   12788                 :        3744 :             return parse_method_call_expr (tok, std::move (left),
   12789                 :             :                                            std::move (outer_attrs),
   12790                 :        1248 :                                            restrictions);
   12791                 :             :           }
   12792                 :        3627 :       }
   12793                 :         244 :     case LEFT_PAREN:
   12794                 :             :       // function call - method call is based on dot notation first
   12795                 :         732 :       return parse_function_call_expr (tok, std::move (left),
   12796                 :         244 :                                        std::move (outer_attrs), restrictions);
   12797                 :         225 :     case LEFT_SQUARE:
   12798                 :             :       // array or slice index expression (pseudo binary infix)
   12799                 :         675 :       return parse_index_expr (tok, std::move (left), std::move (outer_attrs),
   12800                 :         225 :                                restrictions);
   12801                 :           0 :     case FLOAT_LITERAL:
   12802                 :             :       /* HACK: get around lexer mis-identifying '.0' or '.1' or whatever as a
   12803                 :             :        * float literal - TODO does this happen anymore? It shouldn't. */
   12804                 :           0 :       return parse_tuple_index_expr_float (tok, std::move (left),
   12805                 :             :                                            std::move (outer_attrs),
   12806                 :           0 :                                            restrictions);
   12807                 :           0 :     default:
   12808                 :           0 :       add_error (Error (tok->get_locus (),
   12809                 :             :                         "found unexpected token %qs in left denotation",
   12810                 :             :                         tok->get_token_description ()));
   12811                 :             : 
   12812                 :           0 :       return nullptr;
   12813                 :             :     }
   12814                 :             : }
   12815                 :             : 
   12816                 :             : /* Returns the left binding power for the given ArithmeticOrLogicalExpr type.
   12817                 :             :  * TODO make constexpr? Would that even do anything useful? */
   12818                 :             : inline binding_powers
   12819                 :        4818 : get_lbp_for_arithmetic_or_logical_expr (
   12820                 :             :   AST::ArithmeticOrLogicalExpr::ExprType expr_type)
   12821                 :             : {
   12822                 :        4818 :   switch (expr_type)
   12823                 :             :     {
   12824                 :             :     case ArithmeticOrLogicalOperator::ADD:
   12825                 :             :       return LBP_PLUS;
   12826                 :             :     case ArithmeticOrLogicalOperator::SUBTRACT:
   12827                 :             :       return LBP_MINUS;
   12828                 :             :     case ArithmeticOrLogicalOperator::MULTIPLY:
   12829                 :             :       return LBP_MUL;
   12830                 :             :     case ArithmeticOrLogicalOperator::DIVIDE:
   12831                 :             :       return LBP_DIV;
   12832                 :             :     case ArithmeticOrLogicalOperator::MODULUS:
   12833                 :             :       return LBP_MOD;
   12834                 :             :     case ArithmeticOrLogicalOperator::BITWISE_AND:
   12835                 :             :       return LBP_AMP;
   12836                 :             :     case ArithmeticOrLogicalOperator::BITWISE_OR:
   12837                 :             :       return LBP_PIPE;
   12838                 :             :     case ArithmeticOrLogicalOperator::BITWISE_XOR:
   12839                 :             :       return LBP_CARET;
   12840                 :             :     case ArithmeticOrLogicalOperator::LEFT_SHIFT:
   12841                 :             :       return LBP_L_SHIFT;
   12842                 :             :     case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
   12843                 :             :       return LBP_R_SHIFT;
   12844                 :           0 :     default:
   12845                 :             :       // WTF? should not happen, this is an error
   12846                 :           0 :       rust_unreachable ();
   12847                 :             : 
   12848                 :             :       return LBP_PLUS;
   12849                 :             :     }
   12850                 :             : }
   12851                 :             : 
   12852                 :             : // Parses an arithmetic or logical expression (with Pratt parsing).
   12853                 :             : template <typename ManagedTokenSource>
   12854                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12855                 :        4818 : Parser<ManagedTokenSource>::parse_arithmetic_or_logical_expr (
   12856                 :             :   const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
   12857                 :             :   AST::ArithmeticOrLogicalExpr::ExprType expr_type,
   12858                 :             :   ParseRestrictions restrictions)
   12859                 :             : {
   12860                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12861                 :        4818 :   std::unique_ptr<AST::Expr> right
   12862                 :        4818 :     = parse_expr (get_lbp_for_arithmetic_or_logical_expr (expr_type),
   12863                 :        9636 :                   AST::AttrVec (), restrictions);
   12864                 :        4818 :   if (right == nullptr)
   12865                 :           4 :     return nullptr;
   12866                 :             : 
   12867                 :             :   // TODO: check types. actually, do so during semantic analysis
   12868                 :        4814 :   location_t locus = left->get_locus ();
   12869                 :             : 
   12870                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12871                 :        4814 :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12872                 :        4814 :                                       expr_type, locus));
   12873                 :        4818 : }
   12874                 :             : 
   12875                 :             : // Parses a binary addition expression (with Pratt parsing).
   12876                 :             : template <typename ManagedTokenSource>
   12877                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12878                 :             : Parser<ManagedTokenSource>::parse_binary_plus_expr (
   12879                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12880                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12881                 :             : {
   12882                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12883                 :             :   std::unique_ptr<AST::Expr> right
   12884                 :             :     = parse_expr (LBP_PLUS, AST::AttrVec (), restrictions);
   12885                 :             :   if (right == nullptr)
   12886                 :             :     return nullptr;
   12887                 :             : 
   12888                 :             :   // TODO: check types. actually, do so during semantic analysis
   12889                 :             :   location_t locus = left->get_locus ();
   12890                 :             : 
   12891                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12892                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12893                 :             :                                       ArithmeticOrLogicalOperator::ADD, locus));
   12894                 :             : }
   12895                 :             : 
   12896                 :             : // Parses a binary subtraction expression (with Pratt parsing).
   12897                 :             : template <typename ManagedTokenSource>
   12898                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12899                 :             : Parser<ManagedTokenSource>::parse_binary_minus_expr (
   12900                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12901                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12902                 :             : {
   12903                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12904                 :             :   std::unique_ptr<AST::Expr> right
   12905                 :             :     = parse_expr (LBP_MINUS, AST::AttrVec (), restrictions);
   12906                 :             :   if (right == nullptr)
   12907                 :             :     return nullptr;
   12908                 :             : 
   12909                 :             :   // TODO: check types. actually, do so during semantic analysis
   12910                 :             :   location_t locus = left->get_locus ();
   12911                 :             : 
   12912                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12913                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12914                 :             :                                       ArithmeticOrLogicalOperator::SUBTRACT,
   12915                 :             :                                       locus));
   12916                 :             : }
   12917                 :             : 
   12918                 :             : // Parses a binary multiplication expression (with Pratt parsing).
   12919                 :             : template <typename ManagedTokenSource>
   12920                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12921                 :             : Parser<ManagedTokenSource>::parse_binary_mult_expr (
   12922                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12923                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12924                 :             : {
   12925                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12926                 :             :   std::unique_ptr<AST::Expr> right
   12927                 :             :     = parse_expr (LBP_MUL, AST::AttrVec (), restrictions);
   12928                 :             :   if (right == nullptr)
   12929                 :             :     return nullptr;
   12930                 :             : 
   12931                 :             :   // TODO: check types. actually, do so during semantic analysis
   12932                 :             :   location_t locus = left->get_locus ();
   12933                 :             : 
   12934                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12935                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12936                 :             :                                       ArithmeticOrLogicalOperator::MULTIPLY,
   12937                 :             :                                       locus));
   12938                 :             : }
   12939                 :             : 
   12940                 :             : // Parses a binary division expression (with Pratt parsing).
   12941                 :             : template <typename ManagedTokenSource>
   12942                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12943                 :             : Parser<ManagedTokenSource>::parse_binary_div_expr (
   12944                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12945                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12946                 :             : {
   12947                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12948                 :             :   std::unique_ptr<AST::Expr> right
   12949                 :             :     = parse_expr (LBP_DIV, AST::AttrVec (), restrictions);
   12950                 :             :   if (right == nullptr)
   12951                 :             :     return nullptr;
   12952                 :             : 
   12953                 :             :   // TODO: check types. actually, do so during semantic analysis
   12954                 :             :   location_t locus = left->get_locus ();
   12955                 :             : 
   12956                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12957                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12958                 :             :                                       ArithmeticOrLogicalOperator::DIVIDE,
   12959                 :             :                                       locus));
   12960                 :             : }
   12961                 :             : 
   12962                 :             : // Parses a binary modulo expression (with Pratt parsing).
   12963                 :             : template <typename ManagedTokenSource>
   12964                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12965                 :             : Parser<ManagedTokenSource>::parse_binary_mod_expr (
   12966                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12967                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12968                 :             : {
   12969                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12970                 :             :   std::unique_ptr<AST::Expr> right
   12971                 :             :     = parse_expr (LBP_MOD, AST::AttrVec (), restrictions);
   12972                 :             :   if (right == nullptr)
   12973                 :             :     return nullptr;
   12974                 :             : 
   12975                 :             :   // TODO: check types. actually, do so during semantic analysis
   12976                 :             :   location_t locus = left->get_locus ();
   12977                 :             : 
   12978                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   12979                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   12980                 :             :                                       ArithmeticOrLogicalOperator::MODULUS,
   12981                 :             :                                       locus));
   12982                 :             : }
   12983                 :             : 
   12984                 :             : /* Parses a binary bitwise (or eager logical) and expression (with Pratt
   12985                 :             :  * parsing). */
   12986                 :             : template <typename ManagedTokenSource>
   12987                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   12988                 :             : Parser<ManagedTokenSource>::parse_bitwise_and_expr (
   12989                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   12990                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   12991                 :             : {
   12992                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   12993                 :             :   std::unique_ptr<AST::Expr> right
   12994                 :             :     = parse_expr (LBP_AMP, AST::AttrVec (), restrictions);
   12995                 :             :   if (right == nullptr)
   12996                 :             :     return nullptr;
   12997                 :             : 
   12998                 :             :   // TODO: check types. actually, do so during semantic analysis
   12999                 :             :   location_t locus = left->get_locus ();
   13000                 :             : 
   13001                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   13002                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   13003                 :             :                                       ArithmeticOrLogicalOperator::BITWISE_AND,
   13004                 :             :                                       locus));
   13005                 :             : }
   13006                 :             : 
   13007                 :             : /* Parses a binary bitwise (or eager logical) or expression (with Pratt
   13008                 :             :  * parsing). */
   13009                 :             : template <typename ManagedTokenSource>
   13010                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   13011                 :             : Parser<ManagedTokenSource>::parse_bitwise_or_expr (
   13012                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13013                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13014                 :             : {
   13015                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13016                 :             :   std::unique_ptr<AST::Expr> right
   13017                 :             :     = parse_expr (LBP_PIPE, AST::AttrVec (), restrictions);
   13018                 :             :   if (right == nullptr)
   13019                 :             :     return nullptr;
   13020                 :             : 
   13021                 :             :   // TODO: check types. actually, do so during semantic analysis
   13022                 :             :   location_t locus = left->get_locus ();
   13023                 :             : 
   13024                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   13025                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   13026                 :             :                                       ArithmeticOrLogicalOperator::BITWISE_OR,
   13027                 :             :                                       locus));
   13028                 :             : }
   13029                 :             : 
   13030                 :             : /* Parses a binary bitwise (or eager logical) xor expression (with Pratt
   13031                 :             :  * parsing). */
   13032                 :             : template <typename ManagedTokenSource>
   13033                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   13034                 :             : Parser<ManagedTokenSource>::parse_bitwise_xor_expr (
   13035                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13036                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13037                 :             : {
   13038                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13039                 :             :   std::unique_ptr<AST::Expr> right
   13040                 :             :     = parse_expr (LBP_CARET, AST::AttrVec (), restrictions);
   13041                 :             :   if (right == nullptr)
   13042                 :             :     return nullptr;
   13043                 :             : 
   13044                 :             :   // TODO: check types. actually, do so during semantic analysis
   13045                 :             :   location_t locus = left->get_locus ();
   13046                 :             : 
   13047                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   13048                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   13049                 :             :                                       ArithmeticOrLogicalOperator::BITWISE_XOR,
   13050                 :             :                                       locus));
   13051                 :             : }
   13052                 :             : 
   13053                 :             : // Parses a binary left shift expression (with Pratt parsing).
   13054                 :             : template <typename ManagedTokenSource>
   13055                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   13056                 :             : Parser<ManagedTokenSource>::parse_left_shift_expr (
   13057                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13058                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13059                 :             : {
   13060                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13061                 :             :   std::unique_ptr<AST::Expr> right
   13062                 :             :     = parse_expr (LBP_L_SHIFT, AST::AttrVec (), restrictions);
   13063                 :             :   if (right == nullptr)
   13064                 :             :     return nullptr;
   13065                 :             : 
   13066                 :             :   // TODO: check types. actually, do so during semantic analysis
   13067                 :             :   location_t locus = left->get_locus ();
   13068                 :             : 
   13069                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   13070                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   13071                 :             :                                       ArithmeticOrLogicalOperator::LEFT_SHIFT,
   13072                 :             :                                       locus));
   13073                 :             : }
   13074                 :             : 
   13075                 :             : // Parses a binary right shift expression (with Pratt parsing).
   13076                 :             : template <typename ManagedTokenSource>
   13077                 :             : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
   13078                 :             : Parser<ManagedTokenSource>::parse_right_shift_expr (
   13079                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13080                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13081                 :             : {
   13082                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13083                 :             :   std::unique_ptr<AST::Expr> right
   13084                 :             :     = parse_expr (LBP_R_SHIFT, AST::AttrVec (), restrictions);
   13085                 :             :   if (right == nullptr)
   13086                 :             :     return nullptr;
   13087                 :             : 
   13088                 :             :   // TODO: check types. actually, do so during semantic analysis
   13089                 :             :   location_t locus = left->get_locus ();
   13090                 :             : 
   13091                 :             :   return std::unique_ptr<AST::ArithmeticOrLogicalExpr> (
   13092                 :             :     new AST::ArithmeticOrLogicalExpr (std::move (left), std::move (right),
   13093                 :             :                                       ArithmeticOrLogicalOperator::RIGHT_SHIFT,
   13094                 :             :                                       locus));
   13095                 :             : }
   13096                 :             : 
   13097                 :             : /* Returns the left binding power for the given ComparisonExpr type.
   13098                 :             :  * TODO make constexpr? Would that even do anything useful? */
   13099                 :             : inline binding_powers
   13100                 :         889 : get_lbp_for_comparison_expr (AST::ComparisonExpr::ExprType expr_type)
   13101                 :             : {
   13102                 :         889 :   switch (expr_type)
   13103                 :             :     {
   13104                 :             :     case ComparisonOperator::EQUAL:
   13105                 :             :       return LBP_EQUAL;
   13106                 :             :     case ComparisonOperator::NOT_EQUAL:
   13107                 :             :       return LBP_NOT_EQUAL;
   13108                 :             :     case ComparisonOperator::GREATER_THAN:
   13109                 :             :       return LBP_GREATER_THAN;
   13110                 :             :     case ComparisonOperator::LESS_THAN:
   13111                 :             :       return LBP_SMALLER_THAN;
   13112                 :             :     case ComparisonOperator::GREATER_OR_EQUAL:
   13113                 :             :       return LBP_GREATER_EQUAL;
   13114                 :             :     case ComparisonOperator::LESS_OR_EQUAL:
   13115                 :             :       return LBP_SMALLER_EQUAL;
   13116                 :           0 :     default:
   13117                 :             :       // WTF? should not happen, this is an error
   13118                 :           0 :       rust_unreachable ();
   13119                 :             : 
   13120                 :             :       return LBP_EQUAL;
   13121                 :             :     }
   13122                 :             : }
   13123                 :             : 
   13124                 :             : /* Parses a ComparisonExpr of given type and LBP. TODO find a way to only
   13125                 :             :  * specify one and have the other looked up - e.g. specify ExprType and
   13126                 :             :  * binding power is looked up? */
   13127                 :             : template <typename ManagedTokenSource>
   13128                 :             : std::unique_ptr<AST::ComparisonExpr>
   13129                 :         889 : Parser<ManagedTokenSource>::parse_comparison_expr (
   13130                 :             :   const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
   13131                 :             :   AST::ComparisonExpr::ExprType expr_type, ParseRestrictions restrictions)
   13132                 :             : {
   13133                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13134                 :         889 :   std::unique_ptr<AST::Expr> right
   13135                 :         889 :     = parse_expr (get_lbp_for_comparison_expr (expr_type), AST::AttrVec (),
   13136                 :             :                   restrictions);
   13137                 :         889 :   if (right == nullptr)
   13138                 :           0 :     return nullptr;
   13139                 :             : 
   13140                 :             :   // TODO: check types. actually, do so during semantic analysis
   13141                 :         889 :   location_t locus = left->get_locus ();
   13142                 :             : 
   13143                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13144                 :         889 :     new AST::ComparisonExpr (std::move (left), std::move (right), expr_type,
   13145                 :         889 :                              locus));
   13146                 :         889 : }
   13147                 :             : 
   13148                 :             : // Parses a binary equal to expression (with Pratt parsing).
   13149                 :             : template <typename ManagedTokenSource>
   13150                 :             : std::unique_ptr<AST::ComparisonExpr>
   13151                 :             : Parser<ManagedTokenSource>::parse_binary_equal_expr (
   13152                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13153                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13154                 :             : {
   13155                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13156                 :             :   std::unique_ptr<AST::Expr> right
   13157                 :             :     = parse_expr (LBP_EQUAL, AST::AttrVec (), restrictions);
   13158                 :             :   if (right == nullptr)
   13159                 :             :     return nullptr;
   13160                 :             : 
   13161                 :             :   // TODO: check types. actually, do so during semantic analysis
   13162                 :             :   location_t locus = left->get_locus ();
   13163                 :             : 
   13164                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13165                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13166                 :             :                              ComparisonOperator::EQUAL, locus));
   13167                 :             : }
   13168                 :             : 
   13169                 :             : // Parses a binary not equal to expression (with Pratt parsing).
   13170                 :             : template <typename ManagedTokenSource>
   13171                 :             : std::unique_ptr<AST::ComparisonExpr>
   13172                 :             : Parser<ManagedTokenSource>::parse_binary_not_equal_expr (
   13173                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13174                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13175                 :             : {
   13176                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13177                 :             :   std::unique_ptr<AST::Expr> right
   13178                 :             :     = parse_expr (LBP_NOT_EQUAL, AST::AttrVec (), restrictions);
   13179                 :             :   if (right == nullptr)
   13180                 :             :     return nullptr;
   13181                 :             : 
   13182                 :             :   // TODO: check types. actually, do so during semantic analysis
   13183                 :             :   location_t locus = left->get_locus ();
   13184                 :             : 
   13185                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13186                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13187                 :             :                              ComparisonOperator::NOT_EQUAL, locus));
   13188                 :             : }
   13189                 :             : 
   13190                 :             : // Parses a binary greater than expression (with Pratt parsing).
   13191                 :             : template <typename ManagedTokenSource>
   13192                 :             : std::unique_ptr<AST::ComparisonExpr>
   13193                 :             : Parser<ManagedTokenSource>::parse_binary_greater_than_expr (
   13194                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13195                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13196                 :             : {
   13197                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13198                 :             :   std::unique_ptr<AST::Expr> right
   13199                 :             :     = parse_expr (LBP_GREATER_THAN, AST::AttrVec (), restrictions);
   13200                 :             :   if (right == nullptr)
   13201                 :             :     return nullptr;
   13202                 :             : 
   13203                 :             :   // TODO: check types. actually, do so during semantic analysis
   13204                 :             :   location_t locus = left->get_locus ();
   13205                 :             : 
   13206                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13207                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13208                 :             :                              ComparisonOperator::GREATER_THAN, locus));
   13209                 :             : }
   13210                 :             : 
   13211                 :             : // Parses a binary less than expression (with Pratt parsing).
   13212                 :             : template <typename ManagedTokenSource>
   13213                 :             : std::unique_ptr<AST::ComparisonExpr>
   13214                 :             : Parser<ManagedTokenSource>::parse_binary_less_than_expr (
   13215                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13216                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13217                 :             : {
   13218                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13219                 :             :   std::unique_ptr<AST::Expr> right
   13220                 :             :     = parse_expr (LBP_SMALLER_THAN, AST::AttrVec (), restrictions);
   13221                 :             :   if (right == nullptr)
   13222                 :             :     return nullptr;
   13223                 :             : 
   13224                 :             :   // TODO: check types. actually, do so during semantic analysis
   13225                 :             :   location_t locus = left->get_locus ();
   13226                 :             : 
   13227                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13228                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13229                 :             :                              ComparisonOperator::LESS_THAN, locus));
   13230                 :             : }
   13231                 :             : 
   13232                 :             : // Parses a binary greater than or equal to expression (with Pratt parsing).
   13233                 :             : template <typename ManagedTokenSource>
   13234                 :             : std::unique_ptr<AST::ComparisonExpr>
   13235                 :             : Parser<ManagedTokenSource>::parse_binary_greater_equal_expr (
   13236                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13237                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13238                 :             : {
   13239                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13240                 :             :   std::unique_ptr<AST::Expr> right
   13241                 :             :     = parse_expr (LBP_GREATER_EQUAL, AST::AttrVec (), restrictions);
   13242                 :             :   if (right == nullptr)
   13243                 :             :     return nullptr;
   13244                 :             : 
   13245                 :             :   // TODO: check types. actually, do so during semantic analysis
   13246                 :             :   location_t locus = left->get_locus ();
   13247                 :             : 
   13248                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13249                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13250                 :             :                              ComparisonOperator::GREATER_OR_EQUAL, locus));
   13251                 :             : }
   13252                 :             : 
   13253                 :             : // Parses a binary less than or equal to expression (with Pratt parsing).
   13254                 :             : template <typename ManagedTokenSource>
   13255                 :             : std::unique_ptr<AST::ComparisonExpr>
   13256                 :             : Parser<ManagedTokenSource>::parse_binary_less_equal_expr (
   13257                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13258                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13259                 :             : {
   13260                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13261                 :             :   std::unique_ptr<AST::Expr> right
   13262                 :             :     = parse_expr (LBP_SMALLER_EQUAL, AST::AttrVec (), restrictions);
   13263                 :             :   if (right == nullptr)
   13264                 :             :     return nullptr;
   13265                 :             : 
   13266                 :             :   // TODO: check types. actually, do so during semantic analysis
   13267                 :             :   location_t locus = left->get_locus ();
   13268                 :             : 
   13269                 :             :   return std::unique_ptr<AST::ComparisonExpr> (
   13270                 :             :     new AST::ComparisonExpr (std::move (left), std::move (right),
   13271                 :             :                              ComparisonOperator::LESS_OR_EQUAL, locus));
   13272                 :             : }
   13273                 :             : 
   13274                 :             : // Parses a binary lazy boolean or expression (with Pratt parsing).
   13275                 :             : template <typename ManagedTokenSource>
   13276                 :             : std::unique_ptr<AST::LazyBooleanExpr>
   13277                 :          56 : Parser<ManagedTokenSource>::parse_lazy_or_expr (
   13278                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13279                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13280                 :             : {
   13281                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13282                 :          56 :   std::unique_ptr<AST::Expr> right
   13283                 :          56 :     = parse_expr (LBP_LOGICAL_OR, AST::AttrVec (), restrictions);
   13284                 :          56 :   if (right == nullptr)
   13285                 :           0 :     return nullptr;
   13286                 :             : 
   13287                 :             :   // TODO: check types. actually, do so during semantic analysis
   13288                 :          56 :   location_t locus = left->get_locus ();
   13289                 :             : 
   13290                 :             :   return std::unique_ptr<AST::LazyBooleanExpr> (
   13291                 :          56 :     new AST::LazyBooleanExpr (std::move (left), std::move (right),
   13292                 :          56 :                               LazyBooleanOperator::LOGICAL_OR, locus));
   13293                 :          56 : }
   13294                 :             : 
   13295                 :             : // Parses a binary lazy boolean and expression (with Pratt parsing).
   13296                 :             : template <typename ManagedTokenSource>
   13297                 :             : std::unique_ptr<AST::LazyBooleanExpr>
   13298                 :         231 : Parser<ManagedTokenSource>::parse_lazy_and_expr (
   13299                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13300                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13301                 :             : {
   13302                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13303                 :         231 :   std::unique_ptr<AST::Expr> right
   13304                 :         231 :     = parse_expr (LBP_LOGICAL_AND, AST::AttrVec (), restrictions);
   13305                 :         231 :   if (right == nullptr)
   13306                 :           0 :     return nullptr;
   13307                 :             : 
   13308                 :             :   // TODO: check types. actually, do so during semantic analysis
   13309                 :         231 :   location_t locus = left->get_locus ();
   13310                 :             : 
   13311                 :             :   return std::unique_ptr<AST::LazyBooleanExpr> (
   13312                 :         231 :     new AST::LazyBooleanExpr (std::move (left), std::move (right),
   13313                 :         231 :                               LazyBooleanOperator::LOGICAL_AND, locus));
   13314                 :         231 : }
   13315                 :             : 
   13316                 :             : // Parses a pseudo-binary infix type cast expression (with Pratt parsing).
   13317                 :             : template <typename ManagedTokenSource>
   13318                 :             : std::unique_ptr<AST::TypeCastExpr>
   13319                 :        3709 : Parser<ManagedTokenSource>::parse_type_cast_expr (
   13320                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> expr_to_cast,
   13321                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED,
   13322                 :             :   ParseRestrictions restrictions ATTRIBUTE_UNUSED)
   13323                 :             : {
   13324                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13325                 :        3709 :   std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
   13326                 :        3709 :   if (type == nullptr)
   13327                 :           0 :     return nullptr;
   13328                 :             :   // FIXME: how do I get precedence put in here?
   13329                 :             : 
   13330                 :             :   // TODO: check types. actually, do so during semantic analysis
   13331                 :        3709 :   location_t locus = expr_to_cast->get_locus ();
   13332                 :             : 
   13333                 :             :   return std::unique_ptr<AST::TypeCastExpr> (
   13334                 :        3709 :     new AST::TypeCastExpr (std::move (expr_to_cast), std::move (type), locus));
   13335                 :        3709 : }
   13336                 :             : 
   13337                 :             : // Parses a binary assignment expression (with Pratt parsing).
   13338                 :             : template <typename ManagedTokenSource>
   13339                 :             : std::unique_ptr<AST::AssignmentExpr>
   13340                 :        1763 : Parser<ManagedTokenSource>::parse_assig_expr (
   13341                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13342                 :             :   AST::AttrVec outer_attrs, ParseRestrictions restrictions)
   13343                 :             : {
   13344                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13345                 :        1763 :   std::unique_ptr<AST::Expr> right
   13346                 :        1763 :     = parse_expr (LBP_ASSIG - 1, AST::AttrVec (), restrictions);
   13347                 :        1763 :   if (right == nullptr)
   13348                 :           0 :     return nullptr;
   13349                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13350                 :             : 
   13351                 :             :   // TODO: check types. actually, do so during semantic analysis
   13352                 :        1763 :   location_t locus = left->get_locus ();
   13353                 :             : 
   13354                 :             :   return std::unique_ptr<AST::AssignmentExpr> (
   13355                 :        1763 :     new AST::AssignmentExpr (std::move (left), std::move (right),
   13356                 :        1763 :                              std::move (outer_attrs), locus));
   13357                 :        1763 : }
   13358                 :             : 
   13359                 :             : /* Returns the left binding power for the given CompoundAssignmentExpr type.
   13360                 :             :  * TODO make constexpr? Would that even do anything useful? */
   13361                 :             : inline binding_powers
   13362                 :         207 : get_lbp_for_compound_assignment_expr (
   13363                 :             :   AST::CompoundAssignmentExpr::ExprType expr_type)
   13364                 :             : {
   13365                 :         207 :   switch (expr_type)
   13366                 :             :     {
   13367                 :             :     case CompoundAssignmentOperator::ADD:
   13368                 :             :       return LBP_PLUS;
   13369                 :             :     case CompoundAssignmentOperator::SUBTRACT:
   13370                 :             :       return LBP_MINUS;
   13371                 :             :     case CompoundAssignmentOperator::MULTIPLY:
   13372                 :             :       return LBP_MUL;
   13373                 :             :     case CompoundAssignmentOperator::DIVIDE:
   13374                 :             :       return LBP_DIV;
   13375                 :             :     case CompoundAssignmentOperator::MODULUS:
   13376                 :             :       return LBP_MOD;
   13377                 :             :     case CompoundAssignmentOperator::BITWISE_AND:
   13378                 :             :       return LBP_AMP;
   13379                 :             :     case CompoundAssignmentOperator::BITWISE_OR:
   13380                 :             :       return LBP_PIPE;
   13381                 :             :     case CompoundAssignmentOperator::BITWISE_XOR:
   13382                 :             :       return LBP_CARET;
   13383                 :             :     case CompoundAssignmentOperator::LEFT_SHIFT:
   13384                 :             :       return LBP_L_SHIFT;
   13385                 :             :     case CompoundAssignmentOperator::RIGHT_SHIFT:
   13386                 :             :       return LBP_R_SHIFT;
   13387                 :           0 :     default:
   13388                 :             :       // WTF? should not happen, this is an error
   13389                 :           0 :       rust_unreachable ();
   13390                 :             : 
   13391                 :             :       return LBP_PLUS;
   13392                 :             :     }
   13393                 :             : }
   13394                 :             : 
   13395                 :             : // Parses a compound assignment expression (with Pratt parsing).
   13396                 :             : template <typename ManagedTokenSource>
   13397                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13398                 :         207 : Parser<ManagedTokenSource>::parse_compound_assignment_expr (
   13399                 :             :   const_TokenPtr, std::unique_ptr<AST::Expr> left, AST::AttrVec,
   13400                 :             :   AST::CompoundAssignmentExpr::ExprType expr_type,
   13401                 :             :   ParseRestrictions restrictions)
   13402                 :             : {
   13403                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13404                 :         207 :   std::unique_ptr<AST::Expr> right
   13405                 :         207 :     = parse_expr (get_lbp_for_compound_assignment_expr (expr_type) - 1,
   13406                 :         414 :                   AST::AttrVec (), restrictions);
   13407                 :         207 :   if (right == nullptr)
   13408                 :           0 :     return nullptr;
   13409                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13410                 :             : 
   13411                 :             :   // TODO: check types. actually, do so during semantic analysis
   13412                 :         207 :   location_t locus = left->get_locus ();
   13413                 :             : 
   13414                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13415                 :         207 :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13416                 :         207 :                                      expr_type, locus));
   13417                 :         207 : }
   13418                 :             : 
   13419                 :             : // Parses a binary add-assignment expression (with Pratt parsing).
   13420                 :             : template <typename ManagedTokenSource>
   13421                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13422                 :             : Parser<ManagedTokenSource>::parse_plus_assig_expr (
   13423                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13424                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13425                 :             : {
   13426                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13427                 :             :   std::unique_ptr<AST::Expr> right
   13428                 :             :     = parse_expr (LBP_PLUS_ASSIG - 1, AST::AttrVec (), restrictions);
   13429                 :             :   if (right == nullptr)
   13430                 :             :     return nullptr;
   13431                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13432                 :             : 
   13433                 :             :   // TODO: check types. actually, do so during semantic analysis
   13434                 :             :   location_t locus = left->get_locus ();
   13435                 :             : 
   13436                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13437                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13438                 :             :                                      CompoundAssignmentOperator::ADD, locus));
   13439                 :             : }
   13440                 :             : 
   13441                 :             : // Parses a binary minus-assignment expression (with Pratt parsing).
   13442                 :             : template <typename ManagedTokenSource>
   13443                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13444                 :             : Parser<ManagedTokenSource>::parse_minus_assig_expr (
   13445                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13446                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13447                 :             : {
   13448                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13449                 :             :   std::unique_ptr<AST::Expr> right
   13450                 :             :     = parse_expr (LBP_MINUS_ASSIG - 1, AST::AttrVec (), restrictions);
   13451                 :             :   if (right == nullptr)
   13452                 :             :     return nullptr;
   13453                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13454                 :             : 
   13455                 :             :   // TODO: check types. actually, do so during semantic analysis
   13456                 :             :   location_t locus = left->get_locus ();
   13457                 :             : 
   13458                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13459                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13460                 :             :                                      CompoundAssignmentOperator::SUBTRACT,
   13461                 :             :                                      locus));
   13462                 :             : }
   13463                 :             : 
   13464                 :             : // Parses a binary multiplication-assignment expression (with Pratt parsing).
   13465                 :             : template <typename ManagedTokenSource>
   13466                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13467                 :             : Parser<ManagedTokenSource>::parse_mult_assig_expr (
   13468                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13469                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13470                 :             : {
   13471                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13472                 :             :   std::unique_ptr<AST::Expr> right
   13473                 :             :     = parse_expr (LBP_MULT_ASSIG - 1, AST::AttrVec (), restrictions);
   13474                 :             :   if (right == nullptr)
   13475                 :             :     return nullptr;
   13476                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13477                 :             : 
   13478                 :             :   // TODO: check types. actually, do so during semantic analysis
   13479                 :             :   location_t locus = left->get_locus ();
   13480                 :             : 
   13481                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13482                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13483                 :             :                                      CompoundAssignmentOperator::MULTIPLY,
   13484                 :             :                                      locus));
   13485                 :             : }
   13486                 :             : 
   13487                 :             : // Parses a binary division-assignment expression (with Pratt parsing).
   13488                 :             : template <typename ManagedTokenSource>
   13489                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13490                 :             : Parser<ManagedTokenSource>::parse_div_assig_expr (
   13491                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13492                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13493                 :             : {
   13494                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13495                 :             :   std::unique_ptr<AST::Expr> right
   13496                 :             :     = parse_expr (LBP_DIV_ASSIG - 1, AST::AttrVec (), restrictions);
   13497                 :             :   if (right == nullptr)
   13498                 :             :     return nullptr;
   13499                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13500                 :             : 
   13501                 :             :   // TODO: check types. actually, do so during semantic analysis
   13502                 :             :   location_t locus = left->get_locus ();
   13503                 :             : 
   13504                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13505                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13506                 :             :                                      CompoundAssignmentOperator::DIVIDE,
   13507                 :             :                                      locus));
   13508                 :             : }
   13509                 :             : 
   13510                 :             : // Parses a binary modulo-assignment expression (with Pratt parsing).
   13511                 :             : template <typename ManagedTokenSource>
   13512                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13513                 :             : Parser<ManagedTokenSource>::parse_mod_assig_expr (
   13514                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13515                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13516                 :             : {
   13517                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13518                 :             :   std::unique_ptr<AST::Expr> right
   13519                 :             :     = parse_expr (LBP_MOD_ASSIG - 1, AST::AttrVec (), restrictions);
   13520                 :             :   if (right == nullptr)
   13521                 :             :     return nullptr;
   13522                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13523                 :             : 
   13524                 :             :   // TODO: check types. actually, do so during semantic analysis
   13525                 :             :   location_t locus = left->get_locus ();
   13526                 :             : 
   13527                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13528                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13529                 :             :                                      CompoundAssignmentOperator::MODULUS,
   13530                 :             :                                      locus));
   13531                 :             : }
   13532                 :             : 
   13533                 :             : // Parses a binary and-assignment expression (with Pratt parsing).
   13534                 :             : template <typename ManagedTokenSource>
   13535                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13536                 :             : Parser<ManagedTokenSource>::parse_and_assig_expr (
   13537                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13538                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13539                 :             : {
   13540                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13541                 :             :   std::unique_ptr<AST::Expr> right
   13542                 :             :     = parse_expr (LBP_AMP_ASSIG - 1, AST::AttrVec (), restrictions);
   13543                 :             :   if (right == nullptr)
   13544                 :             :     return nullptr;
   13545                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13546                 :             : 
   13547                 :             :   // TODO: check types. actually, do so during semantic analysis
   13548                 :             :   location_t locus = left->get_locus ();
   13549                 :             : 
   13550                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13551                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13552                 :             :                                      CompoundAssignmentOperator::BITWISE_AND,
   13553                 :             :                                      locus));
   13554                 :             : }
   13555                 :             : 
   13556                 :             : // Parses a binary or-assignment expression (with Pratt parsing).
   13557                 :             : template <typename ManagedTokenSource>
   13558                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13559                 :             : Parser<ManagedTokenSource>::parse_or_assig_expr (
   13560                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13561                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13562                 :             : {
   13563                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13564                 :             :   std::unique_ptr<AST::Expr> right
   13565                 :             :     = parse_expr (LBP_PIPE_ASSIG - 1, AST::AttrVec (), restrictions);
   13566                 :             :   if (right == nullptr)
   13567                 :             :     return nullptr;
   13568                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13569                 :             : 
   13570                 :             :   // TODO: check types. actually, do so during semantic analysis
   13571                 :             :   location_t locus = left->get_locus ();
   13572                 :             : 
   13573                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13574                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13575                 :             :                                      CompoundAssignmentOperator::BITWISE_OR,
   13576                 :             :                                      locus));
   13577                 :             : }
   13578                 :             : 
   13579                 :             : // Parses a binary xor-assignment expression (with Pratt parsing).
   13580                 :             : template <typename ManagedTokenSource>
   13581                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13582                 :             : Parser<ManagedTokenSource>::parse_xor_assig_expr (
   13583                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13584                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13585                 :             : {
   13586                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13587                 :             :   std::unique_ptr<AST::Expr> right
   13588                 :             :     = parse_expr (LBP_CARET_ASSIG - 1, AST::AttrVec (), restrictions);
   13589                 :             :   if (right == nullptr)
   13590                 :             :     return nullptr;
   13591                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13592                 :             : 
   13593                 :             :   // TODO: check types. actually, do so during semantic analysis
   13594                 :             :   location_t locus = left->get_locus ();
   13595                 :             : 
   13596                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13597                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13598                 :             :                                      CompoundAssignmentOperator::BITWISE_XOR,
   13599                 :             :                                      locus));
   13600                 :             : }
   13601                 :             : 
   13602                 :             : // Parses a binary left shift-assignment expression (with Pratt parsing).
   13603                 :             : template <typename ManagedTokenSource>
   13604                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13605                 :             : Parser<ManagedTokenSource>::parse_left_shift_assig_expr (
   13606                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13607                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13608                 :             : {
   13609                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13610                 :             :   std::unique_ptr<AST::Expr> right
   13611                 :             :     = parse_expr (LBP_L_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
   13612                 :             :   if (right == nullptr)
   13613                 :             :     return nullptr;
   13614                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13615                 :             : 
   13616                 :             :   // TODO: check types. actually, do so during semantic analysis
   13617                 :             :   location_t locus = left->get_locus ();
   13618                 :             : 
   13619                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13620                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13621                 :             :                                      CompoundAssignmentOperator::LEFT_SHIFT,
   13622                 :             :                                      locus));
   13623                 :             : }
   13624                 :             : 
   13625                 :             : // Parses a binary right shift-assignment expression (with Pratt parsing).
   13626                 :             : template <typename ManagedTokenSource>
   13627                 :             : std::unique_ptr<AST::CompoundAssignmentExpr>
   13628                 :             : Parser<ManagedTokenSource>::parse_right_shift_assig_expr (
   13629                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13630                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13631                 :             : {
   13632                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13633                 :             :   std::unique_ptr<AST::Expr> right
   13634                 :             :     = parse_expr (LBP_R_SHIFT_ASSIG - 1, AST::AttrVec (), restrictions);
   13635                 :             :   if (right == nullptr)
   13636                 :             :     return nullptr;
   13637                 :             :   // FIXME: ensure right-associativity for this - 'LBP - 1' may do this?
   13638                 :             : 
   13639                 :             :   // TODO: check types. actually, do so during semantic analysis
   13640                 :             :   location_t locus = left->get_locus ();
   13641                 :             : 
   13642                 :             :   return std::unique_ptr<AST::CompoundAssignmentExpr> (
   13643                 :             :     new AST::CompoundAssignmentExpr (std::move (left), std::move (right),
   13644                 :             :                                      CompoundAssignmentOperator::RIGHT_SHIFT,
   13645                 :             :                                      locus));
   13646                 :             : }
   13647                 :             : 
   13648                 :             : // Parses a postfix unary await expression (with Pratt parsing).
   13649                 :             : template <typename ManagedTokenSource>
   13650                 :             : std::unique_ptr<AST::AwaitExpr>
   13651                 :           0 : Parser<ManagedTokenSource>::parse_await_expr (
   13652                 :             :   const_TokenPtr tok, std::unique_ptr<AST::Expr> expr_to_await,
   13653                 :             :   AST::AttrVec outer_attrs)
   13654                 :             : {
   13655                 :             :   /* skip "await" identifier (as "." has already been consumed in
   13656                 :             :    * parse_expression) this assumes that the identifier was already identified
   13657                 :             :    * as await */
   13658                 :           0 :   if (!skip_token (IDENTIFIER))
   13659                 :             :     {
   13660                 :           0 :       Error error (tok->get_locus (), "failed to skip %<await%> in await expr "
   13661                 :             :                                       "- this is probably a deep issue");
   13662                 :           0 :       add_error (std::move (error));
   13663                 :             : 
   13664                 :             :       // skip somewhere?
   13665                 :           0 :       return nullptr;
   13666                 :           0 :     }
   13667                 :             : 
   13668                 :             :   // TODO: check inside async block in semantic analysis
   13669                 :           0 :   location_t locus = expr_to_await->get_locus ();
   13670                 :             : 
   13671                 :             :   return std::unique_ptr<AST::AwaitExpr> (
   13672                 :           0 :     new AST::AwaitExpr (std::move (expr_to_await), std::move (outer_attrs),
   13673                 :           0 :                         locus));
   13674                 :             : }
   13675                 :             : 
   13676                 :             : /* Parses an exclusive range ('..') in left denotation position (i.e.
   13677                 :             :  * RangeFromExpr or RangeFromToExpr). */
   13678                 :             : template <typename ManagedTokenSource>
   13679                 :             : std::unique_ptr<AST::RangeExpr>
   13680                 :          68 : Parser<ManagedTokenSource>::parse_led_range_exclusive_expr (
   13681                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13682                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13683                 :             : {
   13684                 :             :   // FIXME: this probably parses expressions accidently or whatever
   13685                 :             :   // try parsing RHS (as tok has already been consumed in parse_expression)
   13686                 :             :   // Can be nullptr, in which case it is a RangeFromExpr, otherwise a
   13687                 :             :   // RangeFromToExpr.
   13688                 :          68 :   restrictions.expr_can_be_null = true;
   13689                 :          68 :   std::unique_ptr<AST::Expr> right
   13690                 :          68 :     = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
   13691                 :             : 
   13692                 :          68 :   location_t locus = left->get_locus ();
   13693                 :             : 
   13694                 :          68 :   if (right == nullptr)
   13695                 :             :     {
   13696                 :             :       // range from expr
   13697                 :          11 :       return std::unique_ptr<AST::RangeFromExpr> (
   13698                 :          11 :         new AST::RangeFromExpr (std::move (left), locus));
   13699                 :             :     }
   13700                 :             :   else
   13701                 :             :     {
   13702                 :          57 :       return std::unique_ptr<AST::RangeFromToExpr> (
   13703                 :          57 :         new AST::RangeFromToExpr (std::move (left), std::move (right), locus));
   13704                 :             :     }
   13705                 :             :   // FIXME: make non-associative
   13706                 :          68 : }
   13707                 :             : 
   13708                 :             : /* Parses an exclusive range ('..') in null denotation position (i.e.
   13709                 :             :  * RangeToExpr or RangeFullExpr). */
   13710                 :             : template <typename ManagedTokenSource>
   13711                 :             : std::unique_ptr<AST::RangeExpr>
   13712                 :          11 : Parser<ManagedTokenSource>::parse_nud_range_exclusive_expr (
   13713                 :             :   const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
   13714                 :             : {
   13715                 :          11 :   auto restrictions = ParseRestrictions ();
   13716                 :          11 :   restrictions.expr_can_be_null = true;
   13717                 :             : 
   13718                 :             :   // FIXME: this probably parses expressions accidently or whatever
   13719                 :             :   // try parsing RHS (as tok has already been consumed in parse_expression)
   13720                 :          11 :   std::unique_ptr<AST::Expr> right
   13721                 :          11 :     = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions);
   13722                 :             : 
   13723                 :          11 :   location_t locus = tok->get_locus ();
   13724                 :             : 
   13725                 :          11 :   if (right == nullptr)
   13726                 :             :     {
   13727                 :             :       // range from expr
   13728                 :           2 :       return std::unique_ptr<AST::RangeFullExpr> (
   13729                 :           2 :         new AST::RangeFullExpr (locus));
   13730                 :             :     }
   13731                 :             :   else
   13732                 :             :     {
   13733                 :           9 :       return std::unique_ptr<AST::RangeToExpr> (
   13734                 :           9 :         new AST::RangeToExpr (std::move (right), locus));
   13735                 :             :     }
   13736                 :             :   // FIXME: make non-associative
   13737                 :          11 : }
   13738                 :             : 
   13739                 :             : // Parses a full binary range inclusive expression.
   13740                 :             : template <typename ManagedTokenSource>
   13741                 :             : std::unique_ptr<AST::RangeFromToInclExpr>
   13742                 :           7 : Parser<ManagedTokenSource>::parse_range_inclusive_expr (
   13743                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
   13744                 :             :   AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
   13745                 :             : {
   13746                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13747                 :           7 :   std::unique_ptr<AST::Expr> right
   13748                 :           7 :     = parse_expr (LBP_DOT_DOT_EQ, AST::AttrVec (), restrictions);
   13749                 :           7 :   if (right == nullptr)
   13750                 :           0 :     return nullptr;
   13751                 :             :   // FIXME: make non-associative
   13752                 :             : 
   13753                 :             :   // TODO: check types. actually, do so during semantic analysis
   13754                 :           7 :   location_t locus = left->get_locus ();
   13755                 :             : 
   13756                 :             :   return std::unique_ptr<AST::RangeFromToInclExpr> (
   13757                 :           7 :     new AST::RangeFromToInclExpr (std::move (left), std::move (right), locus));
   13758                 :           7 : }
   13759                 :             : 
   13760                 :             : // Parses an inclusive range-to prefix unary expression.
   13761                 :             : template <typename ManagedTokenSource>
   13762                 :             : std::unique_ptr<AST::RangeToInclExpr>
   13763                 :           0 : Parser<ManagedTokenSource>::parse_range_to_inclusive_expr (
   13764                 :             :   const_TokenPtr tok, AST::AttrVec outer_attrs ATTRIBUTE_UNUSED)
   13765                 :             : {
   13766                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13767                 :           0 :   std::unique_ptr<AST::Expr> right = parse_expr (LBP_DOT_DOT_EQ);
   13768                 :           0 :   if (right == nullptr)
   13769                 :           0 :     return nullptr;
   13770                 :             :   // FIXME: make non-associative
   13771                 :             : 
   13772                 :             :   // TODO: check types. actually, do so during semantic analysis
   13773                 :             : 
   13774                 :             :   return std::unique_ptr<AST::RangeToInclExpr> (
   13775                 :           0 :     new AST::RangeToInclExpr (std::move (right), tok->get_locus ()));
   13776                 :           0 : }
   13777                 :             : 
   13778                 :             : // Parses a pseudo-binary infix tuple index expression.
   13779                 :             : template <typename ManagedTokenSource>
   13780                 :             : std::unique_ptr<AST::TupleIndexExpr>
   13781                 :         861 : Parser<ManagedTokenSource>::parse_tuple_index_expr (
   13782                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> tuple_expr,
   13783                 :             :   AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
   13784                 :             : {
   13785                 :             :   // parse int literal (as token already skipped)
   13786                 :         861 :   const_TokenPtr index_tok = expect_token (INT_LITERAL);
   13787                 :         861 :   if (index_tok == nullptr)
   13788                 :             :     {
   13789                 :           0 :       return nullptr;
   13790                 :             :     }
   13791                 :         861 :   std::string index = index_tok->get_str ();
   13792                 :             : 
   13793                 :             :   // convert to integer
   13794                 :         861 :   if (!index_tok->is_pure_decimal ())
   13795                 :             :     {
   13796                 :          54 :       Error error (index_tok->get_locus (),
   13797                 :             :                    "tuple index should be a pure decimal literal");
   13798                 :          54 :       add_error (std::move (error));
   13799                 :          54 :     }
   13800                 :         861 :   int index_int = atoi (index.c_str ());
   13801                 :             : 
   13802                 :         861 :   location_t locus = tuple_expr->get_locus ();
   13803                 :             : 
   13804                 :             :   return std::unique_ptr<AST::TupleIndexExpr> (
   13805                 :         861 :     new AST::TupleIndexExpr (std::move (tuple_expr), index_int,
   13806                 :         861 :                              std::move (outer_attrs), locus));
   13807                 :         861 : }
   13808                 :             : 
   13809                 :             : // Parses a pseudo-binary infix array (or slice) index expression.
   13810                 :             : template <typename ManagedTokenSource>
   13811                 :             : std::unique_ptr<AST::ArrayIndexExpr>
   13812                 :         225 : Parser<ManagedTokenSource>::parse_index_expr (
   13813                 :             :   const_TokenPtr, std::unique_ptr<AST::Expr> array_expr,
   13814                 :             :   AST::AttrVec outer_attrs, ParseRestrictions)
   13815                 :             : {
   13816                 :             :   // parse RHS (as tok has already been consumed in parse_expression)
   13817                 :             :   /*std::unique_ptr<AST::Expr> index_expr
   13818                 :             :     = parse_expr (LBP_ARRAY_REF, AST::AttrVec (),
   13819                 :             :     restrictions);*/
   13820                 :             :   // TODO: conceptually, should treat [] as brackets, so just parse all expr
   13821                 :         225 :   std::unique_ptr<AST::Expr> index_expr = parse_expr ();
   13822                 :         225 :   if (index_expr == nullptr)
   13823                 :           0 :     return nullptr;
   13824                 :             : 
   13825                 :             :   // skip ']' at end of array
   13826                 :         225 :   if (!skip_token (RIGHT_SQUARE))
   13827                 :             :     {
   13828                 :             :       // skip somewhere?
   13829                 :           0 :       return nullptr;
   13830                 :             :     }
   13831                 :             : 
   13832                 :             :   // TODO: check types. actually, do so during semantic analysis
   13833                 :         225 :   location_t locus = array_expr->get_locus ();
   13834                 :             : 
   13835                 :             :   return std::unique_ptr<AST::ArrayIndexExpr> (
   13836                 :         225 :     new AST::ArrayIndexExpr (std::move (array_expr), std::move (index_expr),
   13837                 :         225 :                              std::move (outer_attrs), locus));
   13838                 :         225 : }
   13839                 :             : 
   13840                 :             : // Parses a pseudo-binary infix struct field access expression.
   13841                 :             : template <typename ManagedTokenSource>
   13842                 :             : std::unique_ptr<AST::FieldAccessExpr>
   13843                 :        1518 : Parser<ManagedTokenSource>::parse_field_access_expr (
   13844                 :             :   const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> struct_expr,
   13845                 :             :   AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
   13846                 :             : {
   13847                 :             :   /* get field name identifier (assume that this is a field access expr and
   13848                 :             :    * not await, for instance) */
   13849                 :        1518 :   const_TokenPtr ident_tok = expect_token (IDENTIFIER);
   13850                 :        1518 :   if (ident_tok == nullptr)
   13851                 :           0 :     return nullptr;
   13852                 :             : 
   13853                 :        3036 :   Identifier ident{ident_tok};
   13854                 :             : 
   13855                 :        1518 :   location_t locus = struct_expr->get_locus ();
   13856                 :             : 
   13857                 :             :   // TODO: check types. actually, do so during semantic analysis
   13858                 :             :   return std::unique_ptr<AST::FieldAccessExpr> (
   13859                 :        1518 :     new AST::FieldAccessExpr (std::move (struct_expr), std::move (ident),
   13860                 :        1518 :                               std::move (outer_attrs), locus));
   13861                 :        1518 : }
   13862                 :             : 
   13863                 :             : // Parses a pseudo-binary infix method call expression.
   13864                 :             : template <typename ManagedTokenSource>
   13865                 :             : std::unique_ptr<AST::MethodCallExpr>
   13866                 :        1248 : Parser<ManagedTokenSource>::parse_method_call_expr (
   13867                 :             :   const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
   13868                 :             :   AST::AttrVec outer_attrs, ParseRestrictions)
   13869                 :             : {
   13870                 :             :   // parse path expr segment
   13871                 :        1248 :   AST::PathExprSegment segment = parse_path_expr_segment ();
   13872                 :        1248 :   if (segment.is_error ())
   13873                 :             :     {
   13874                 :           0 :       Error error (tok->get_locus (),
   13875                 :             :                    "failed to parse path expr segment of method call expr");
   13876                 :           0 :       add_error (std::move (error));
   13877                 :             : 
   13878                 :           0 :       return nullptr;
   13879                 :           0 :     }
   13880                 :             : 
   13881                 :             :   // skip left parentheses
   13882                 :        1248 :   if (!skip_token (LEFT_PAREN))
   13883                 :             :     {
   13884                 :           0 :       return nullptr;
   13885                 :             :     }
   13886                 :             : 
   13887                 :             :   // parse method params (if they exist)
   13888                 :        1248 :   std::vector<std::unique_ptr<AST::Expr>> params;
   13889                 :             : 
   13890                 :        1248 :   const_TokenPtr t = lexer.peek_token ();
   13891                 :        1749 :   while (t->get_id () != RIGHT_PAREN)
   13892                 :             :     {
   13893                 :         501 :       std::unique_ptr<AST::Expr> param = parse_expr ();
   13894                 :         501 :       if (param == nullptr)
   13895                 :             :         {
   13896                 :           0 :           Error error (t->get_locus (),
   13897                 :             :                        "failed to parse method param in method call");
   13898                 :           0 :           add_error (std::move (error));
   13899                 :             : 
   13900                 :           0 :           return nullptr;
   13901                 :           0 :         }
   13902                 :         501 :       params.push_back (std::move (param));
   13903                 :             : 
   13904                 :        1002 :       if (lexer.peek_token ()->get_id () != COMMA)
   13905                 :             :         break;
   13906                 :             : 
   13907                 :           0 :       lexer.skip_token ();
   13908                 :           0 :       t = lexer.peek_token ();
   13909                 :             :     }
   13910                 :             : 
   13911                 :             :   // skip right paren
   13912                 :        1248 :   if (!skip_token (RIGHT_PAREN))
   13913                 :             :     {
   13914                 :           0 :       return nullptr;
   13915                 :             :     }
   13916                 :             : 
   13917                 :             :   // TODO: check types. actually do so in semantic analysis pass.
   13918                 :        1248 :   location_t locus = receiver_expr->get_locus ();
   13919                 :             : 
   13920                 :             :   return std::unique_ptr<AST::MethodCallExpr> (
   13921                 :        2496 :     new AST::MethodCallExpr (std::move (receiver_expr), std::move (segment),
   13922                 :             :                              std::move (params), std::move (outer_attrs),
   13923                 :        1248 :                              locus));
   13924                 :        1248 : }
   13925                 :             : 
   13926                 :             : // Parses a pseudo-binary infix function call expression.
   13927                 :             : template <typename ManagedTokenSource>
   13928                 :             : std::unique_ptr<AST::CallExpr>
   13929                 :         244 : Parser<ManagedTokenSource>::parse_function_call_expr (
   13930                 :             :   const_TokenPtr, std::unique_ptr<AST::Expr> function_expr,
   13931                 :             :   AST::AttrVec outer_attrs, ParseRestrictions)
   13932                 :             : {
   13933                 :             :   // parse function params (if they exist)
   13934                 :         244 :   std::vector<std::unique_ptr<AST::Expr>> params;
   13935                 :             : 
   13936                 :         244 :   const_TokenPtr t = lexer.peek_token ();
   13937                 :         410 :   while (t->get_id () != RIGHT_PAREN)
   13938                 :             :     {
   13939                 :         166 :       std::unique_ptr<AST::Expr> param = parse_expr ();
   13940                 :         166 :       if (param == nullptr)
   13941                 :             :         {
   13942                 :           0 :           Error error (t->get_locus (),
   13943                 :             :                        "failed to parse function param in function call");
   13944                 :           0 :           add_error (std::move (error));
   13945                 :             : 
   13946                 :           0 :           return nullptr;
   13947                 :           0 :         }
   13948                 :         166 :       params.push_back (std::move (param));
   13949                 :             : 
   13950                 :         332 :       if (lexer.peek_token ()->get_id () != COMMA)
   13951                 :             :         break;
   13952                 :             : 
   13953                 :          27 :       lexer.skip_token ();
   13954                 :          27 :       t = lexer.peek_token ();
   13955                 :             :     }
   13956                 :             : 
   13957                 :             :   // skip ')' at end of param list
   13958                 :         244 :   if (!skip_token (RIGHT_PAREN))
   13959                 :             :     {
   13960                 :             :       // skip somewhere?
   13961                 :           0 :       return nullptr;
   13962                 :             :     }
   13963                 :             : 
   13964                 :             :   // TODO: check types. actually, do so during semantic analysis
   13965                 :         244 :   location_t locus = function_expr->get_locus ();
   13966                 :             : 
   13967                 :             :   return std::unique_ptr<AST::CallExpr> (
   13968                 :         244 :     new AST::CallExpr (std::move (function_expr), std::move (params),
   13969                 :         244 :                        std::move (outer_attrs), locus));
   13970                 :         244 : }
   13971                 :             : 
   13972                 :             : /* Parses a macro invocation with a path in expression already parsed (but not
   13973                 :             :  * '!' token). */
   13974                 :             : template <typename ManagedTokenSource>
   13975                 :             : std::unique_ptr<AST::MacroInvocation>
   13976                 :        3407 : Parser<ManagedTokenSource>::parse_macro_invocation_partial (
   13977                 :             :   AST::PathInExpression path, AST::AttrVec outer_attrs,
   13978                 :             :   ParseRestrictions restrictions)
   13979                 :             : {
   13980                 :             :   // macro invocation
   13981                 :        3407 :   if (!skip_token (EXCLAM))
   13982                 :             :     {
   13983                 :           0 :       return nullptr;
   13984                 :             :     }
   13985                 :             : 
   13986                 :             :   // convert PathInExpression to SimplePath - if this isn't possible, error
   13987                 :        3407 :   AST::SimplePath converted_path = path.as_simple_path ();
   13988                 :        3407 :   if (converted_path.is_empty ())
   13989                 :             :     {
   13990                 :           0 :       Error error (lexer.peek_token ()->get_locus (),
   13991                 :             :                    "failed to parse simple path in macro invocation");
   13992                 :           0 :       add_error (std::move (error));
   13993                 :             : 
   13994                 :           0 :       return nullptr;
   13995                 :           0 :     }
   13996                 :             : 
   13997                 :        3407 :   AST::DelimTokenTree tok_tree = parse_delim_token_tree ();
   13998                 :             : 
   13999                 :        3407 :   rust_debug ("successfully parsed macro invocation (via partial)");
   14000                 :             : 
   14001                 :        3407 :   location_t macro_locus = converted_path.get_locus ();
   14002                 :             : 
   14003                 :        6814 :   return AST::MacroInvocation::Regular (
   14004                 :        6814 :     AST::MacroInvocData (std::move (converted_path), std::move (tok_tree)),
   14005                 :        3407 :     std::move (outer_attrs), macro_locus);
   14006                 :        3407 : }
   14007                 :             : 
   14008                 :             : /* Parses a struct expr struct with a path in expression already parsed (but
   14009                 :             :  * not
   14010                 :             :  * '{' token). */
   14011                 :             : template <typename ManagedTokenSource>
   14012                 :             : std::unique_ptr<AST::StructExprStruct>
   14013                 :         999 : Parser<ManagedTokenSource>::parse_struct_expr_struct_partial (
   14014                 :             :   AST::PathInExpression path, AST::AttrVec outer_attrs)
   14015                 :             : {
   14016                 :             :   // assume struct expr struct (as struct-enum disambiguation requires name
   14017                 :             :   // lookup) again, make statement if final ';'
   14018                 :         999 :   if (!skip_token (LEFT_CURLY))
   14019                 :             :     {
   14020                 :           0 :       return nullptr;
   14021                 :             :     }
   14022                 :             : 
   14023                 :             :   // parse inner attributes
   14024                 :         999 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
   14025                 :             : 
   14026                 :             :   // branch based on next token
   14027                 :         999 :   const_TokenPtr t = lexer.peek_token ();
   14028                 :         999 :   location_t path_locus = path.get_locus ();
   14029                 :         999 :   switch (t->get_id ())
   14030                 :             :     {
   14031                 :          51 :     case RIGHT_CURLY:
   14032                 :             :       // struct with no body
   14033                 :          51 :       lexer.skip_token ();
   14034                 :             : 
   14035                 :             :       return std::unique_ptr<AST::StructExprStruct> (
   14036                 :          51 :         new AST::StructExprStruct (std::move (path), std::move (inner_attrs),
   14037                 :          51 :                                    std::move (outer_attrs), path_locus));
   14038                 :         948 :     case DOT_DOT:
   14039                 :             :       /* technically this would give a struct base-only struct, but this
   14040                 :             :        * algorithm should work too. As such, AST type not happening. */
   14041                 :             :     case IDENTIFIER:
   14042                 :             :     case HASH:
   14043                 :             :       case INT_LITERAL: {
   14044                 :             :         // struct with struct expr fields
   14045                 :             : 
   14046                 :             :         // parse struct expr fields
   14047                 :         948 :         std::vector<std::unique_ptr<AST::StructExprField>> fields;
   14048                 :             : 
   14049                 :        2681 :         while (t->get_id () != RIGHT_CURLY && t->get_id () != DOT_DOT)
   14050                 :             :           {
   14051                 :        1733 :             std::unique_ptr<AST::StructExprField> field
   14052                 :             :               = parse_struct_expr_field ();
   14053                 :        1733 :             if (field == nullptr)
   14054                 :             :               {
   14055                 :           0 :                 Error error (t->get_locus (),
   14056                 :             :                              "failed to parse struct (or enum) expr field");
   14057                 :           0 :                 add_error (std::move (error));
   14058                 :             : 
   14059                 :           0 :                 return nullptr;
   14060                 :           0 :               }
   14061                 :             : 
   14062                 :             :             // DEBUG:
   14063                 :        1733 :             rust_debug ("struct/enum expr field validated to not be null");
   14064                 :             : 
   14065                 :        1733 :             fields.push_back (std::move (field));
   14066                 :             : 
   14067                 :             :             // DEBUG:
   14068                 :        1733 :             rust_debug ("struct/enum expr field pushed back");
   14069                 :             : 
   14070                 :        3466 :             if (lexer.peek_token ()->get_id () != COMMA)
   14071                 :             :               {
   14072                 :             :                 // DEBUG:
   14073                 :         759 :                 rust_debug ("lack of comma detected in struct/enum expr "
   14074                 :             :                             "fields - break");
   14075                 :             :                 break;
   14076                 :             :               }
   14077                 :         974 :             lexer.skip_token ();
   14078                 :             : 
   14079                 :             :             // DEBUG:
   14080                 :         974 :             rust_debug ("struct/enum expr fields comma skipped ");
   14081                 :             : 
   14082                 :         974 :             t = lexer.peek_token ();
   14083                 :             :           }
   14084                 :             : 
   14085                 :             :         // DEBUG:
   14086                 :         948 :         rust_debug ("struct/enum expr about to parse struct base ");
   14087                 :             : 
   14088                 :             :         // parse struct base if it exists
   14089                 :         948 :         AST::StructBase struct_base = AST::StructBase::error ();
   14090                 :        1896 :         if (lexer.peek_token ()->get_id () == DOT_DOT)
   14091                 :             :           {
   14092                 :          63 :             location_t dot_dot_location = lexer.peek_token ()->get_locus ();
   14093                 :          63 :             lexer.skip_token ();
   14094                 :             : 
   14095                 :             :             // parse required struct base expr
   14096                 :          63 :             std::unique_ptr<AST::Expr> base_expr = parse_expr ();
   14097                 :          63 :             if (base_expr == nullptr)
   14098                 :             :               {
   14099                 :           0 :                 Error error (lexer.peek_token ()->get_locus (),
   14100                 :             :                              "failed to parse struct base expression in struct "
   14101                 :             :                              "expression");
   14102                 :           0 :                 add_error (std::move (error));
   14103                 :             : 
   14104                 :           0 :                 return nullptr;
   14105                 :           0 :               }
   14106                 :             : 
   14107                 :             :             // DEBUG:
   14108                 :          63 :             rust_debug ("struct/enum expr - parsed and validated base expr");
   14109                 :             : 
   14110                 :             :             struct_base
   14111                 :          63 :               = AST::StructBase (std::move (base_expr), dot_dot_location);
   14112                 :             : 
   14113                 :             :             // DEBUG:
   14114                 :          63 :             rust_debug ("assigned struct base to new struct base ");
   14115                 :          63 :           }
   14116                 :             : 
   14117                 :         948 :         if (!skip_token (RIGHT_CURLY))
   14118                 :             :           {
   14119                 :           0 :             return nullptr;
   14120                 :             :           }
   14121                 :             : 
   14122                 :             :         // DEBUG:
   14123                 :         948 :         rust_debug (
   14124                 :             :           "struct/enum expr skipped right curly - done and ready to return");
   14125                 :             : 
   14126                 :         948 :         return std::unique_ptr<AST::StructExprStructFields> (
   14127                 :         948 :           new AST::StructExprStructFields (std::move (path), std::move (fields),
   14128                 :             :                                            path_locus, std::move (struct_base),
   14129                 :             :                                            std::move (inner_attrs),
   14130                 :         948 :                                            std::move (outer_attrs)));
   14131                 :         948 :       }
   14132                 :           0 :     default:
   14133                 :           0 :       add_error (
   14134                 :           0 :         Error (t->get_locus (),
   14135                 :             :                "unrecognised token %qs in struct (or enum) expression - "
   14136                 :             :                "expected %<}%>, identifier, integer literal, or %<..%>",
   14137                 :             :                t->get_token_description ()));
   14138                 :             : 
   14139                 :           0 :       return nullptr;
   14140                 :             :     }
   14141                 :         999 : }
   14142                 :             : 
   14143                 :             : /* Parses a struct expr tuple with a path in expression already parsed (but
   14144                 :             :  * not
   14145                 :             :  * '(' token).
   14146                 :             :  * FIXME: this currently outputs a call expr, as they cannot be disambiguated.
   14147                 :             :  * A better solution would be to just get this to call that function directly.
   14148                 :             :  * */
   14149                 :             : template <typename ManagedTokenSource>
   14150                 :             : std::unique_ptr<AST::CallExpr>
   14151                 :        7483 : Parser<ManagedTokenSource>::parse_struct_expr_tuple_partial (
   14152                 :             :   AST::PathInExpression path, AST::AttrVec outer_attrs)
   14153                 :             : {
   14154                 :        7483 :   if (!skip_token (LEFT_PAREN))
   14155                 :             :     {
   14156                 :           0 :       return nullptr;
   14157                 :             :     }
   14158                 :             : 
   14159                 :        7483 :   AST::AttrVec inner_attrs = parse_inner_attributes ();
   14160                 :             : 
   14161                 :        7483 :   std::vector<std::unique_ptr<AST::Expr>> exprs;
   14162                 :             : 
   14163                 :        7483 :   const_TokenPtr t = lexer.peek_token ();
   14164                 :       16067 :   while (t->get_id () != RIGHT_PAREN)
   14165                 :             :     {
   14166                 :             :       // parse expression (required)
   14167                 :        8584 :       std::unique_ptr<AST::Expr> expr = parse_expr ();
   14168                 :        8584 :       if (expr == nullptr)
   14169                 :             :         {
   14170                 :           0 :           Error error (t->get_locus (), "failed to parse expression in "
   14171                 :             :                                         "struct (or enum) expression tuple");
   14172                 :           0 :           add_error (std::move (error));
   14173                 :             : 
   14174                 :           0 :           return nullptr;
   14175                 :           0 :         }
   14176                 :        8584 :       exprs.push_back (std::move (expr));
   14177                 :             : 
   14178                 :       17168 :       if (lexer.peek_token ()->get_id () != COMMA)
   14179                 :             :         break;
   14180                 :             : 
   14181                 :        2918 :       lexer.skip_token ();
   14182                 :             : 
   14183                 :        2918 :       t = lexer.peek_token ();
   14184                 :             :     }
   14185                 :             : 
   14186                 :        7483 :   if (!skip_token (RIGHT_PAREN))
   14187                 :             :     {
   14188                 :           0 :       return nullptr;
   14189                 :             :     }
   14190                 :             : 
   14191                 :        7483 :   location_t path_locus = path.get_locus ();
   14192                 :             : 
   14193                 :        7483 :   auto pathExpr = std::unique_ptr<AST::PathInExpression> (
   14194                 :        7483 :     new AST::PathInExpression (std::move (path)));
   14195                 :             : 
   14196                 :             :   return std::unique_ptr<AST::CallExpr> (
   14197                 :        7483 :     new AST::CallExpr (std::move (pathExpr), std::move (exprs),
   14198                 :        7483 :                        std::move (outer_attrs), path_locus));
   14199                 :       14966 : }
   14200                 :             : 
   14201                 :             : /* Parses a path in expression with the first token passed as a parameter (as
   14202                 :             :  * it is skipped in token stream). Note that this only parses segment-first
   14203                 :             :  * paths, not global ones. */
   14204                 :             : template <typename ManagedTokenSource>
   14205                 :             : AST::PathInExpression
   14206                 :       22273 : Parser<ManagedTokenSource>::parse_path_in_expression_pratt (const_TokenPtr tok)
   14207                 :             : {
   14208                 :             :   // HACK-y way of making up for pratt-parsing consuming first token
   14209                 :             : 
   14210                 :             :   // DEBUG
   14211                 :       44546 :   rust_debug ("current peek token when starting path pratt parse: '%s'",
   14212                 :             :               lexer.peek_token ()->get_token_description ());
   14213                 :             : 
   14214                 :             :   // create segment vector
   14215                 :       22273 :   std::vector<AST::PathExprSegment> segments;
   14216                 :             : 
   14217                 :       22273 :   std::string initial_str;
   14218                 :             : 
   14219                 :       22273 :   switch (tok->get_id ())
   14220                 :             :     {
   14221                 :       19718 :     case IDENTIFIER:
   14222                 :       19718 :       initial_str = tok->get_str ();
   14223                 :             :       break;
   14224                 :           0 :     case SUPER:
   14225                 :           0 :       initial_str = Values::Keywords::SUPER;
   14226                 :             :       break;
   14227                 :        2473 :     case SELF:
   14228                 :        2473 :       initial_str = Values::Keywords::SELF;
   14229                 :             :       break;
   14230                 :          82 :     case SELF_ALIAS:
   14231                 :          82 :       initial_str = Values::Keywords::SELF_ALIAS;
   14232                 :             :       break;
   14233                 :           0 :     case CRATE:
   14234                 :           0 :       initial_str = Values::Keywords::CRATE;
   14235                 :             :       break;
   14236                 :           0 :     case DOLLAR_SIGN:
   14237                 :           0 :       if (lexer.peek_token ()->get_id () == CRATE)
   14238                 :             :         {
   14239                 :           0 :           initial_str = "$crate";
   14240                 :             :           break;
   14241                 :             :         }
   14242                 :             :       gcc_fallthrough ();
   14243                 :             :     default:
   14244                 :           0 :       add_error (Error (tok->get_locus (),
   14245                 :             :                         "unrecognised token %qs in path in expression",
   14246                 :             :                         tok->get_token_description ()));
   14247                 :             : 
   14248                 :           0 :       return AST::PathInExpression::create_error ();
   14249                 :             :     }
   14250                 :             : 
   14251                 :             :   // parse required initial segment
   14252                 :       22273 :   AST::PathExprSegment initial_segment (initial_str, tok->get_locus ());
   14253                 :             :   // parse generic args (and turbofish), if they exist
   14254                 :             :   /* use lookahead to determine if they actually exist (don't want to
   14255                 :             :    * accidently parse over next ident segment) */
   14256                 :       44546 :   if (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION
   14257                 :       23742 :       && lexer.peek_token (1)->get_id () == LEFT_ANGLE)
   14258                 :             :     {
   14259                 :             :       // skip scope resolution
   14260                 :         298 :       lexer.skip_token ();
   14261                 :             : 
   14262                 :         298 :       AST::GenericArgs generic_args = parse_path_generic_args ();
   14263                 :             : 
   14264                 :             :       initial_segment
   14265                 :         596 :         = AST::PathExprSegment (AST::PathIdentSegment (initial_str,
   14266                 :             :                                                        tok->get_locus ()),
   14267                 :         298 :                                 tok->get_locus (), std::move (generic_args));
   14268                 :         298 :     }
   14269                 :       22273 :   if (initial_segment.is_error ())
   14270                 :             :     {
   14271                 :             :       // skip after somewhere?
   14272                 :             :       // don't necessarily throw error but yeah
   14273                 :             : 
   14274                 :             :       // DEBUG
   14275                 :           0 :       rust_debug ("initial segment is error - returning null");
   14276                 :             : 
   14277                 :           0 :       return AST::PathInExpression::create_error ();
   14278                 :             :     }
   14279                 :       22273 :   segments.push_back (std::move (initial_segment));
   14280                 :             : 
   14281                 :             :   // parse optional segments (as long as scope resolution operator exists)
   14282                 :       22273 :   const_TokenPtr t = lexer.peek_token ();
   14283                 :       23600 :   while (t->get_id () == SCOPE_RESOLUTION)
   14284                 :             :     {
   14285                 :             :       // skip scope resolution operator
   14286                 :        1327 :       lexer.skip_token ();
   14287                 :             : 
   14288                 :             :       // parse the actual segment - it is an error if it doesn't exist now
   14289                 :        1327 :       AST::PathExprSegment segment = parse_path_expr_segment ();
   14290                 :        1327 :       if (segment.is_error ())
   14291                 :             :         {
   14292                 :             :           // skip after somewhere?
   14293                 :           0 :           Error error (t->get_locus (),
   14294                 :             :                        "could not parse path expression segment");
   14295                 :           0 :           add_error (std::move (error));
   14296                 :             : 
   14297                 :           0 :           return AST::PathInExpression::create_error ();
   14298                 :           0 :         }
   14299                 :             : 
   14300                 :        1327 :       segments.push_back (std::move (segment));
   14301                 :             : 
   14302                 :        1327 :       t = lexer.peek_token ();
   14303                 :             :     }
   14304                 :             : 
   14305                 :             :   // DEBUG:
   14306                 :       44546 :   rust_debug (
   14307                 :             :     "current token (just about to return path to null denotation): '%s'",
   14308                 :             :     lexer.peek_token ()->get_token_description ());
   14309                 :             : 
   14310                 :       22273 :   return AST::PathInExpression (std::move (segments), {}, tok->get_locus (),
   14311                 :       22273 :                                 false);
   14312                 :       66819 : }
   14313                 :             : 
   14314                 :             : // Parses a closure expression with pratt parsing (from null denotation).
   14315                 :             : template <typename ManagedTokenSource>
   14316                 :             : std::unique_ptr<AST::ClosureExpr>
   14317                 :          73 : Parser<ManagedTokenSource>::parse_closure_expr_pratt (const_TokenPtr tok,
   14318                 :             :                                                       AST::AttrVec outer_attrs)
   14319                 :             : {
   14320                 :             :   // TODO: does this need pratt parsing (for precedence)? probably not, but
   14321                 :             :   // idk
   14322                 :          73 :   location_t locus = tok->get_locus ();
   14323                 :          73 :   bool has_move = false;
   14324                 :          73 :   if (tok->get_id () == MOVE)
   14325                 :             :     {
   14326                 :           2 :       has_move = true;
   14327                 :           2 :       tok = lexer.peek_token ();
   14328                 :           2 :       lexer.skip_token ();
   14329                 :             :       // skip token and reassign
   14330                 :             :     }
   14331                 :             : 
   14332                 :             :   // handle parameter list
   14333                 :          73 :   std::vector<AST::ClosureParam> params;
   14334                 :             : 
   14335                 :          73 :   switch (tok->get_id ())
   14336                 :             :     {
   14337                 :             :     case OR:
   14338                 :             :       // no parameters, don't skip token
   14339                 :             :       break;
   14340                 :          64 :       case PIPE: {
   14341                 :             :         // actually may have parameters
   14342                 :             :         // don't skip token
   14343                 :          64 :         const_TokenPtr t = lexer.peek_token ();
   14344                 :         143 :         while (t->get_id () != PIPE)
   14345                 :             :           {
   14346                 :          79 :             AST::ClosureParam param = parse_closure_param ();
   14347                 :          79 :             if (param.is_error ())
   14348                 :             :               {
   14349                 :             :                 // TODO is this really an error?
   14350                 :           0 :                 Error error (t->get_locus (), "could not parse closure param");
   14351                 :           0 :                 add_error (std::move (error));
   14352                 :             : 
   14353                 :           0 :                 return nullptr;
   14354                 :           0 :               }
   14355                 :          79 :             params.push_back (std::move (param));
   14356                 :             : 
   14357                 :         158 :             if (lexer.peek_token ()->get_id () != COMMA)
   14358                 :             :               {
   14359                 :         128 :                 if (lexer.peek_token ()->get_id () == OR)
   14360                 :           2 :                   lexer.split_current_token (PIPE, PIPE);
   14361                 :             :                 // not an error but means param list is done
   14362                 :             :                 break;
   14363                 :             :               }
   14364                 :             :             // skip comma
   14365                 :          15 :             lexer.skip_token ();
   14366                 :             : 
   14367                 :          30 :             if (lexer.peek_token ()->get_id () == OR)
   14368                 :           0 :               lexer.split_current_token (PIPE, PIPE);
   14369                 :             : 
   14370                 :          15 :             t = lexer.peek_token ();
   14371                 :             :           }
   14372                 :             : 
   14373                 :          64 :         if (!skip_token (PIPE))
   14374                 :             :           {
   14375                 :           0 :             return nullptr;
   14376                 :             :           }
   14377                 :             :         break;
   14378                 :          64 :       }
   14379                 :           0 :     default:
   14380                 :           0 :       add_error (Error (tok->get_locus (),
   14381                 :             :                         "unexpected token %qs in closure expression - expected "
   14382                 :             :                         "%<|%> or %<||%>",
   14383                 :             :                         tok->get_token_description ()));
   14384                 :             : 
   14385                 :             :       // skip somewhere?
   14386                 :           0 :       return nullptr;
   14387                 :             :     }
   14388                 :             : 
   14389                 :             :   // again branch based on next token
   14390                 :          73 :   tok = lexer.peek_token ();
   14391                 :          73 :   if (tok->get_id () == RETURN_TYPE)
   14392                 :             :     {
   14393                 :             :       // must be return type closure with block expr
   14394                 :             : 
   14395                 :             :       // skip "return type" token
   14396                 :          34 :       lexer.skip_token ();
   14397                 :             : 
   14398                 :             :       // parse actual type, which is required
   14399                 :          34 :       std::unique_ptr<AST::TypeNoBounds> type = parse_type_no_bounds ();
   14400                 :          34 :       if (type == nullptr)
   14401                 :             :         {
   14402                 :             :           // error
   14403                 :           0 :           Error error (tok->get_locus (), "failed to parse type for closure");
   14404                 :           0 :           add_error (std::move (error));
   14405                 :             : 
   14406                 :             :           // skip somewhere?
   14407                 :           0 :           return nullptr;
   14408                 :           0 :         }
   14409                 :             : 
   14410                 :             :       // parse block expr, which is required
   14411                 :          34 :       std::unique_ptr<AST::BlockExpr> block = parse_block_expr ();
   14412                 :          34 :       if (block == nullptr)
   14413                 :             :         {
   14414                 :             :           // error
   14415                 :           0 :           Error error (lexer.peek_token ()->get_locus (),
   14416                 :             :                        "failed to parse block expr in closure");
   14417                 :           0 :           add_error (std::move (error));
   14418                 :             : 
   14419                 :             :           // skip somewhere?
   14420                 :           0 :           return nullptr;
   14421                 :           0 :         }
   14422                 :             : 
   14423                 :          34 :       return std::unique_ptr<AST::ClosureExprInnerTyped> (
   14424                 :          34 :         new AST::ClosureExprInnerTyped (std::move (type), std::move (block),
   14425                 :             :                                         std::move (params), locus, has_move,
   14426                 :          34 :                                         std::move (outer_attrs)));
   14427                 :          34 :     }
   14428                 :             :   else
   14429                 :             :     {
   14430                 :             :       // must be expr-only closure
   14431                 :             : 
   14432                 :             :       // parse expr, which is required
   14433                 :          39 :       std::unique_ptr<AST::Expr> expr = parse_expr ();
   14434                 :          39 :       if (expr == nullptr)
   14435                 :             :         {
   14436                 :           0 :           Error error (tok->get_locus (),
   14437                 :             :                        "failed to parse expression in closure");
   14438                 :           0 :           add_error (std::move (error));
   14439                 :             : 
   14440                 :             :           // skip somewhere?
   14441                 :           0 :           return nullptr;
   14442                 :           0 :         }
   14443                 :             : 
   14444                 :          39 :       return std::unique_ptr<AST::ClosureExprInner> (
   14445                 :          39 :         new AST::ClosureExprInner (std::move (expr), std::move (params), locus,
   14446                 :          39 :                                    has_move, std::move (outer_attrs)));
   14447                 :          39 :     }
   14448                 :          73 : }
   14449                 :             : 
   14450                 :             : /* Parses a tuple index expression (pratt-parsed) from a 'float' token as a
   14451                 :             :  * result of lexer misidentification. */
   14452                 :             : template <typename ManagedTokenSource>
   14453                 :             : std::unique_ptr<AST::TupleIndexExpr>
   14454                 :           0 : Parser<ManagedTokenSource>::parse_tuple_index_expr_float (
   14455                 :             :   const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
   14456                 :             :   AST::AttrVec outer_attrs, ParseRestrictions restrictions ATTRIBUTE_UNUSED)
   14457                 :             : {
   14458                 :             :   // only works on float literals
   14459                 :           0 :   if (tok->get_id () != FLOAT_LITERAL)
   14460                 :           0 :     return nullptr;
   14461                 :             : 
   14462                 :             :   // DEBUG:
   14463                 :           0 :   rust_debug ("exact string form of float: '%s'", tok->get_str ().c_str ());
   14464                 :             : 
   14465                 :             :   // get float string and remove dot and initial 0
   14466                 :           0 :   std::string index_str = tok->get_str ();
   14467                 :           0 :   index_str.erase (index_str.begin ());
   14468                 :             : 
   14469                 :             :   // get int from string
   14470                 :           0 :   int index = atoi (index_str.c_str ());
   14471                 :             : 
   14472                 :           0 :   location_t locus = tuple_expr->get_locus ();
   14473                 :             : 
   14474                 :             :   return std::unique_ptr<AST::TupleIndexExpr> (
   14475                 :           0 :     new AST::TupleIndexExpr (std::move (tuple_expr), index,
   14476                 :           0 :                              std::move (outer_attrs), locus));
   14477                 :           0 : }
   14478                 :             : 
   14479                 :             : // Returns true if the next token is END, ELSE, or EOF;
   14480                 :             : template <typename ManagedTokenSource>
   14481                 :             : bool
   14482                 :             : Parser<ManagedTokenSource>::done_end_or_else ()
   14483                 :             : {
   14484                 :             :   const_TokenPtr t = lexer.peek_token ();
   14485                 :             :   return (t->get_id () == RIGHT_CURLY || t->get_id () == ELSE
   14486                 :             :           || t->get_id () == END_OF_FILE);
   14487                 :             : }
   14488                 :             : 
   14489                 :             : // Returns true if the next token is END or EOF.
   14490                 :             : template <typename ManagedTokenSource>
   14491                 :             : bool
   14492                 :             : Parser<ManagedTokenSource>::done_end ()
   14493                 :             : {
   14494                 :             :   const_TokenPtr t = lexer.peek_token ();
   14495                 :             :   return (t->get_id () == RIGHT_CURLY || t->get_id () == END_OF_FILE);
   14496                 :             : }
   14497                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.