Branch data Line data Source code
1 : : /* This file is part of GCC.
2 : :
3 : : GCC is free software; you can redistribute it and/or modify
4 : : it under the terms of the GNU General Public License as published by
5 : : the Free Software Foundation; either version 3, or (at your option)
6 : : any later version.
7 : :
8 : : GCC is distributed in the hope that it will be useful,
9 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
10 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 : : GNU General Public License for more details.
12 : :
13 : : You should have received a copy of the GNU General Public License
14 : : along with GCC; see the file COPYING3. If not see
15 : : <http://www.gnu.org/licenses/>. */
16 : :
17 : : #ifndef RUST_PARSE_H
18 : : #define RUST_PARSE_H
19 : :
20 : : #include "rust-ast.h"
21 : : #include "rust-item.h"
22 : : #include "rust-lex.h"
23 : : #include "rust-ast-full.h"
24 : : #include "rust-diagnostics.h"
25 : :
26 : : #include "expected.h"
27 : :
28 : : namespace Rust {
29 : :
30 : : class ParseLifetimeParamError
31 : : {
32 : : };
33 : :
34 : : class ParseLifetimeError
35 : : {
36 : : };
37 : :
38 : : enum class AnonConstError
39 : : {
40 : : InvalidSizeExpr,
41 : : };
42 : :
43 : : enum class ParseLoopLabelError
44 : : {
45 : : NOT_LOOP_LABEL,
46 : : MISSING_COLON,
47 : : };
48 : :
49 : : enum class ParseSelfError
50 : : {
51 : : SELF_PTR,
52 : : PARSING,
53 : : NOT_SELF,
54 : : };
55 : :
56 : : /* HACK: used to resolve the expression-or-statement problem at the end of a
57 : : * block by allowing either to be returned (technically). Tagged union would
58 : : * probably take up the same amount of space. */
59 : : struct ExprOrStmt
60 : : {
61 : : std::unique_ptr<AST::Expr> expr;
62 : : std::unique_ptr<AST::Stmt> stmt;
63 : :
64 : : /* I was going to resist the urge to make this a real class and make it POD,
65 : : * but construction in steps is too difficult. So it'll just also have a
66 : : * constructor. */
67 : :
68 : : // expression constructor
69 : 30217 : ExprOrStmt (std::unique_ptr<AST::Expr> expr) : expr (std::move (expr)) {}
70 : :
71 : : // statement constructor
72 : 40173 : ExprOrStmt (std::unique_ptr<AST::Stmt> stmt) : stmt (std::move (stmt)) {}
73 : :
74 : : // macro constructor
75 : : ExprOrStmt (std::unique_ptr<AST::MacroInvocation> macro)
76 : : : expr (std::move (macro))
77 : : {}
78 : :
79 : : // Returns whether this object is in an error state.
80 : 70391 : bool is_error () const
81 : : {
82 : 70391 : return (expr == nullptr && stmt == nullptr)
83 : 110541 : || (expr != nullptr && stmt != nullptr);
84 : : }
85 : :
86 : : // Returns an error state object.
87 : 1 : static ExprOrStmt create_error () { return ExprOrStmt (nullptr, nullptr); }
88 : :
89 : 70391 : ~ExprOrStmt () = default;
90 : :
91 : : /* no copy constructors/assignment as simple object like this shouldn't
92 : : * require it */
93 : :
94 : : // move constructors
95 : : ExprOrStmt (ExprOrStmt &&other) = default;
96 : : ExprOrStmt &operator= (ExprOrStmt &&other) = default;
97 : :
98 : : private:
99 : : // private constructor only used for creating error state expr or stmt objects
100 : 1 : ExprOrStmt (AST::Expr *expr, AST::Stmt *stmt) : expr (expr), stmt (stmt) {}
101 : :
102 : : // make this work: have a disambiguation specifically for known statements
103 : : // (i.e. ';' and 'let'). then, have a special "parse expr or stmt" function
104 : : // that returns this type. inside it, it parses an expression, and then
105 : : // determines whether to return expr or stmt via whether the next token is a
106 : : // semicolon. should be able to disambiguate inside that function between
107 : : // stmts with blocks and without blocks.
108 : : };
109 : :
110 : : /* Restrictions on parsing used to signal that certain ambiguous grammar
111 : : * features should be parsed in a certain way. */
112 : : struct ParseRestrictions
113 : : {
114 : : bool can_be_struct_expr = true;
115 : : /* Whether the expression was entered from a unary expression - prevents stuff
116 : : * like struct exprs being parsed from a dereference. */
117 : : bool entered_from_unary = false;
118 : : bool expr_can_be_null = false;
119 : : bool expr_can_be_stmt = false;
120 : : bool consume_semi = true;
121 : : /* Macro invocations that are statements can expand without a semicolon after
122 : : * the final statement, if it's an expression statement. */
123 : : bool allow_close_after_expr_stmt = false;
124 : : };
125 : :
126 : : // Parser implementation for gccrs.
127 : : // TODO: if updated to C++20, ManagedTokenSource would be useful as a concept
128 : 125885 : template <typename ManagedTokenSource> class Parser
129 : : {
130 : : public:
131 : : /**
132 : : * Consume a token
133 : : */
134 : : void skip_token ();
135 : :
136 : : /**
137 : : * Consume a token, reporting an error if it isn't the next token
138 : : *
139 : : * @param t ID of the token to consume
140 : : *
141 : : * @return true if the token was next, false if it wasn't found
142 : : */
143 : : bool skip_token (TokenId t);
144 : :
145 : : /**
146 : : * Consume a token, reporting an error if it isn't the next token
147 : : *
148 : : * @param token pointer to similar token to consume
149 : : *
150 : : * @return true if the token was next, false if it wasn't found
151 : : */
152 : : bool skip_token (const_TokenPtr token);
153 : :
154 : : /**
155 : : * Same as `skip_token` but allows for failure without necessarily reporting
156 : : * an error
157 : : *
158 : : * @param t ID of the token to consume
159 : : *
160 : : * @return true if the token was next, false if it wasn't found
161 : : */
162 : : bool maybe_skip_token (TokenId t);
163 : :
164 : : std::unique_ptr<AST::Expr>
165 : : parse_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
166 : : ParseRestrictions restrictions = ParseRestrictions ());
167 : :
168 : : std::unique_ptr<AST::LiteralExpr> parse_literal_expr (AST::AttrVec outer_attrs
169 : : = AST::AttrVec ());
170 : :
171 : : std::unique_ptr<AST::BlockExpr>
172 : : parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
173 : 74672 : tl::optional<AST::LoopLabel> = tl::nullopt,
174 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
175 : :
176 : : tl::expected<AST::AnonConst, AnonConstError> parse_anon_const ();
177 : :
178 : : std::unique_ptr<AST::ConstBlock>
179 : : parse_const_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
180 : : location_t loc = UNKNOWN_LOCATION);
181 : :
182 : : bool is_macro_rules_def (const_TokenPtr t);
183 : : std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
184 : : std::unique_ptr<AST::Pattern> parse_pattern ();
185 : : std::unique_ptr<AST::Pattern> parse_pattern_no_alt ();
186 : :
187 : : /**
188 : : * Parse a statement
189 : : *
190 : : * Statement : ';'
191 : : * | Item
192 : : * | LetStatement
193 : : * | ExpressionStatement
194 : : * | MacroInvocationSemi
195 : : */
196 : : std::unique_ptr<AST::Stmt> parse_stmt (ParseRestrictions restrictions
197 : : = ParseRestrictions ());
198 : : std::unique_ptr<AST::Type> parse_type (bool save_errors = true);
199 : : std::unique_ptr<AST::ExternalItem> parse_external_item ();
200 : : std::unique_ptr<AST::AssociatedItem> parse_trait_item ();
201 : : std::unique_ptr<AST::AssociatedItem> parse_inherent_impl_item ();
202 : : std::unique_ptr<AST::AssociatedItem> parse_trait_impl_item ();
203 : : AST::PathInExpression parse_path_in_expression ();
204 : : std::vector<std::unique_ptr<AST::LifetimeParam>> parse_lifetime_params ();
205 : : AST::Visibility parse_visibility ();
206 : : std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern ();
207 : : std::unique_ptr<AST::Token> parse_identifier_or_keyword_token ();
208 : : std::unique_ptr<AST::TokenTree> parse_token_tree ();
209 : : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
210 : : parse_attribute_body ();
211 : : AST::AttrVec parse_inner_attributes ();
212 : : std::unique_ptr<AST::MacroInvocation>
213 : : parse_macro_invocation (AST::AttrVec outer_attrs);
214 : :
215 : : /*
216 : : * This has to be public for parsing expressions with outer attributes
217 : : */
218 : : AST::AttrVec parse_outer_attributes ();
219 : :
220 : : private:
221 : : void skip_after_semicolon ();
222 : : void skip_after_end ();
223 : : void skip_after_end_block ();
224 : : void skip_after_next_block ();
225 : : void skip_after_end_attribute ();
226 : :
227 : : const_TokenPtr expect_token (TokenId t);
228 : : const_TokenPtr expect_token (const_TokenPtr token_expect);
229 : : void unexpected_token (const_TokenPtr t);
230 : : bool skip_generics_right_angle ();
231 : :
232 : : void parse_statement_seq (bool (Parser::*done) ());
233 : :
234 : : // AST-related stuff - maybe move or something?
235 : : AST::Attribute parse_inner_attribute ();
236 : : AST::Attribute parse_outer_attribute ();
237 : : std::unique_ptr<AST::AttrInput> parse_attr_input ();
238 : : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
239 : : parse_doc_comment ();
240 : :
241 : : // Path-related
242 : : AST::SimplePath parse_simple_path ();
243 : : AST::SimplePathSegment parse_simple_path_segment (int base_peek = 0);
244 : : AST::TypePath parse_type_path ();
245 : : std::unique_ptr<AST::TypePathSegment> parse_type_path_segment ();
246 : : AST::PathIdentSegment parse_path_ident_segment ();
247 : : tl::optional<AST::GenericArg> parse_generic_arg ();
248 : : AST::GenericArgs parse_path_generic_args ();
249 : : AST::GenericArgsBinding parse_generic_args_binding ();
250 : : AST::TypePathFunction parse_type_path_function (location_t locus);
251 : : AST::PathExprSegment parse_path_expr_segment ();
252 : : AST::QualifiedPathInExpression
253 : : // When given a pratt_parsed_loc, use it as the location of the
254 : : // first token parsed in the expression (the parsing of that first
255 : : // token should be skipped).
256 : : parse_qualified_path_in_expression (location_t pratt_parsed_loc
257 : : = UNKNOWN_LOCATION);
258 : : AST::QualifiedPathType parse_qualified_path_type (location_t pratt_parsed_loc
259 : : = UNKNOWN_LOCATION);
260 : : AST::QualifiedPathInType parse_qualified_path_in_type ();
261 : :
262 : : // Token tree or macro related
263 : : AST::DelimTokenTree parse_delim_token_tree ();
264 : : std::unique_ptr<AST::MacroRulesDefinition>
265 : : parse_macro_rules_def (AST::AttrVec outer_attrs);
266 : : std::unique_ptr<AST::MacroRulesDefinition>
267 : : parse_decl_macro_def (AST::Visibility vis, AST::AttrVec outer_attrs);
268 : : std::unique_ptr<AST::MacroInvocation>
269 : : parse_macro_invocation_semi (AST::AttrVec outer_attrs);
270 : : AST::MacroRule parse_macro_rule ();
271 : : AST::MacroMatcher parse_macro_matcher ();
272 : : std::unique_ptr<AST::MacroMatch> parse_macro_match ();
273 : : std::unique_ptr<AST::MacroMatchFragment> parse_macro_match_fragment ();
274 : : std::unique_ptr<AST::MacroMatchRepetition> parse_macro_match_repetition ();
275 : :
276 : : // Top-level item-related
277 : : std::unique_ptr<AST::VisItem> parse_vis_item (AST::AttrVec outer_attrs);
278 : :
279 : : // VisItem subclass-related
280 : : std::unique_ptr<AST::Module> parse_module (AST::Visibility vis,
281 : : AST::AttrVec outer_attrs);
282 : : std::unique_ptr<AST::ExternCrate>
283 : : parse_extern_crate (AST::Visibility vis, AST::AttrVec outer_attrs);
284 : : std::unique_ptr<AST::UseDeclaration>
285 : : parse_use_decl (AST::Visibility vis, AST::AttrVec outer_attrs);
286 : : std::unique_ptr<AST::UseTree> parse_use_tree ();
287 : : std::unique_ptr<AST::Function> parse_function (AST::Visibility vis,
288 : : AST::AttrVec outer_attrs,
289 : : bool is_external = false);
290 : : AST::FunctionQualifiers parse_function_qualifiers ();
291 : : std::vector<std::unique_ptr<AST::GenericParam>>
292 : : parse_generic_params_in_angles ();
293 : : template <typename EndTokenPred>
294 : : std::vector<std::unique_ptr<AST::GenericParam>>
295 : : parse_generic_params (EndTokenPred is_end_token);
296 : : template <typename EndTokenPred>
297 : : std::unique_ptr<AST::GenericParam>
298 : : parse_generic_param (EndTokenPred is_end_token);
299 : :
300 : : template <typename EndTokenPred>
301 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
302 : : parse_lifetime_params (EndTokenPred is_end_token);
303 : : std::vector<AST::LifetimeParam> parse_lifetime_params_objs ();
304 : : template <typename EndTokenPred>
305 : : std::vector<AST::LifetimeParam>
306 : : parse_lifetime_params_objs (EndTokenPred is_end_token);
307 : : template <typename ParseFunction, typename EndTokenPred>
308 : : auto parse_non_ptr_sequence (
309 : : ParseFunction parsing_function, EndTokenPred is_end_token,
310 : : std::string error_msg = "failed to parse generic param in generic params")
311 : : -> std::vector<decltype (parsing_function ())>;
312 : : tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
313 : : parse_lifetime_param ();
314 : : std::vector<std::unique_ptr<AST::TypeParam>> parse_type_params ();
315 : : template <typename EndTokenPred>
316 : : std::vector<std::unique_ptr<AST::TypeParam>>
317 : : parse_type_params (EndTokenPred is_end_token);
318 : : std::unique_ptr<AST::TypeParam> parse_type_param ();
319 : : template <typename EndTokenPred>
320 : : std::vector<std::unique_ptr<AST::Param>>
321 : : parse_function_params (EndTokenPred is_end_token);
322 : : std::unique_ptr<AST::Param> parse_function_param ();
323 : : std::unique_ptr<AST::Type> parse_function_return_type ();
324 : : AST::WhereClause parse_where_clause ();
325 : : std::unique_ptr<AST::WhereClauseItem> parse_where_clause_item (
326 : : const std::vector<AST::LifetimeParam> &global_for_lifetimes);
327 : : std::unique_ptr<AST::LifetimeWhereClauseItem>
328 : : parse_lifetime_where_clause_item ();
329 : : std::unique_ptr<AST::TypeBoundWhereClauseItem>
330 : : parse_type_bound_where_clause_item (
331 : : const std::vector<AST::LifetimeParam> &global_for_lifetimes);
332 : : std::vector<AST::LifetimeParam> parse_for_lifetimes ();
333 : : template <typename EndTokenPred>
334 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
335 : : parse_type_param_bounds (EndTokenPred is_end_token);
336 : : std::vector<std::unique_ptr<AST::TypeParamBound>> parse_type_param_bounds ();
337 : : std::unique_ptr<AST::TypeParamBound> parse_type_param_bound ();
338 : : std::unique_ptr<AST::TraitBound> parse_trait_bound ();
339 : : std::vector<AST::Lifetime> parse_lifetime_bounds ();
340 : : template <typename EndTokenPred>
341 : : std::vector<AST::Lifetime> parse_lifetime_bounds (EndTokenPred is_end_token);
342 : : tl::expected<AST::Lifetime, ParseLifetimeError>
343 : : parse_lifetime (bool allow_elided);
344 : : AST::Lifetime lifetime_from_token (const_TokenPtr tok);
345 : : std::unique_ptr<AST::ExternalTypeItem>
346 : : parse_external_type_item (AST::Visibility vis, AST::AttrVec outer_attrs);
347 : :
348 : : std::unique_ptr<AST::TypeAlias> parse_type_alias (AST::Visibility vis,
349 : : AST::AttrVec outer_attrs);
350 : : std::unique_ptr<AST::Struct> parse_struct (AST::Visibility vis,
351 : : AST::AttrVec outer_attrs);
352 : : std::vector<AST::StructField> parse_struct_fields ();
353 : : template <typename EndTokenPred>
354 : : std::vector<AST::StructField> parse_struct_fields (EndTokenPred is_end_token);
355 : : AST::StructField parse_struct_field ();
356 : : std::vector<AST::TupleField> parse_tuple_fields ();
357 : : AST::TupleField parse_tuple_field ();
358 : : std::unique_ptr<AST::Enum> parse_enum (AST::Visibility vis,
359 : : AST::AttrVec outer_attrs);
360 : : std::vector<std::unique_ptr<AST::EnumItem>> parse_enum_items ();
361 : : template <typename EndTokenPred>
362 : : std::vector<std::unique_ptr<AST::EnumItem>>
363 : : parse_enum_items (EndTokenPred is_end_token);
364 : : std::unique_ptr<AST::EnumItem> parse_enum_item ();
365 : : std::unique_ptr<AST::Union> parse_union (AST::Visibility vis,
366 : : AST::AttrVec outer_attrs);
367 : : std::unique_ptr<AST::ConstantItem>
368 : : parse_const_item (AST::Visibility vis, AST::AttrVec outer_attrs);
369 : : std::unique_ptr<AST::StaticItem> parse_static_item (AST::Visibility vis,
370 : : AST::AttrVec outer_attrs);
371 : : std::unique_ptr<AST::Trait> parse_trait (AST::Visibility vis,
372 : : AST::AttrVec outer_attrs);
373 : : std::unique_ptr<AST::TraitItemType>
374 : : parse_trait_type (AST::AttrVec outer_attrs, AST::Visibility);
375 : : std::unique_ptr<AST::ConstantItem>
376 : : parse_trait_const (AST::AttrVec outer_attrs);
377 : :
378 : : tl::expected<std::unique_ptr<AST::Param>, ParseSelfError> parse_self_param ();
379 : :
380 : : std::unique_ptr<AST::Impl> parse_impl (AST::Visibility vis,
381 : : AST::AttrVec outer_attrs);
382 : : std::unique_ptr<AST::AssociatedItem>
383 : : parse_inherent_impl_function_or_method (AST::Visibility vis,
384 : : AST::AttrVec outer_attrs);
385 : : std::unique_ptr<AST::AssociatedItem>
386 : : parse_trait_impl_function_or_method (AST::Visibility vis,
387 : : AST::AttrVec outer_attrs);
388 : : std::unique_ptr<AST::ExternBlock>
389 : : parse_extern_block (AST::Visibility vis, AST::AttrVec outer_attrs);
390 : : std::unique_ptr<AST::Function> parse_method ();
391 : : std::unique_ptr<AST::Function> parse_async_item (AST::Visibility vis,
392 : : AST::AttrVec outer_attrs);
393 : :
394 : : // Expression-related (Pratt parsed)
395 : : std::unique_ptr<AST::Expr>
396 : : parse_expr (int right_binding_power,
397 : : AST::AttrVec outer_attrs = AST::AttrVec (),
398 : : ParseRestrictions restrictions = ParseRestrictions ());
399 : : std::unique_ptr<AST::Expr>
400 : : null_denotation (AST::AttrVec outer_attrs = AST::AttrVec (),
401 : : ParseRestrictions restrictions = ParseRestrictions ());
402 : : std::unique_ptr<AST::Expr>
403 : : null_denotation_path (AST::PathInExpression path, AST::AttrVec outer_attrs,
404 : : ParseRestrictions restrictions = ParseRestrictions ());
405 : : std::unique_ptr<AST::Expr>
406 : : null_denotation_not_path (const_TokenPtr t, AST::AttrVec outer_attrs,
407 : : ParseRestrictions restrictions
408 : : = ParseRestrictions ());
409 : : std::unique_ptr<AST::Expr>
410 : : left_denotations (std::unique_ptr<AST::Expr> null_denotation,
411 : : int right_binding_power, AST::AttrVec outer_attrs,
412 : : ParseRestrictions restrictions = ParseRestrictions ());
413 : : std::unique_ptr<AST::Expr>
414 : : left_denotation (const_TokenPtr t, std::unique_ptr<AST::Expr> left,
415 : : AST::AttrVec outer_attrs = AST::AttrVec (),
416 : : ParseRestrictions restrictions = ParseRestrictions ());
417 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
418 : : parse_arithmetic_or_logical_expr (
419 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
420 : : AST::AttrVec outer_attrs, AST::ArithmeticOrLogicalExpr::ExprType expr_type,
421 : : ParseRestrictions restrictions = ParseRestrictions ());
422 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
423 : : parse_binary_plus_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
424 : : AST::AttrVec outer_attrs,
425 : : ParseRestrictions restrictions
426 : : = ParseRestrictions ());
427 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
428 : : parse_binary_minus_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
429 : : AST::AttrVec outer_attrs,
430 : : ParseRestrictions restrictions
431 : : = ParseRestrictions ());
432 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
433 : : parse_binary_mult_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
434 : : AST::AttrVec outer_attrs,
435 : : ParseRestrictions restrictions
436 : : = ParseRestrictions ());
437 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
438 : : parse_binary_div_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
439 : : AST::AttrVec outer_attrs,
440 : : ParseRestrictions restrictions = ParseRestrictions ());
441 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
442 : : parse_binary_mod_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
443 : : AST::AttrVec outer_attrs,
444 : : ParseRestrictions restrictions = ParseRestrictions ());
445 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
446 : : parse_bitwise_and_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
447 : : AST::AttrVec outer_attrs,
448 : : ParseRestrictions restrictions
449 : : = ParseRestrictions ());
450 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
451 : : parse_bitwise_or_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
452 : : AST::AttrVec outer_attrs,
453 : : ParseRestrictions restrictions = ParseRestrictions ());
454 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
455 : : parse_bitwise_xor_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
456 : : AST::AttrVec outer_attrs,
457 : : ParseRestrictions restrictions
458 : : = ParseRestrictions ());
459 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
460 : : parse_left_shift_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
461 : : AST::AttrVec outer_attrs,
462 : : ParseRestrictions restrictions = ParseRestrictions ());
463 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
464 : : parse_right_shift_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
465 : : AST::AttrVec outer_attrs,
466 : : ParseRestrictions restrictions
467 : : = ParseRestrictions ());
468 : : std::unique_ptr<AST::ComparisonExpr>
469 : : parse_comparison_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
470 : : AST::AttrVec outer_attrs,
471 : : AST::ComparisonExpr::ExprType expr_type,
472 : : ParseRestrictions restrictions = ParseRestrictions ());
473 : : std::unique_ptr<AST::ComparisonExpr>
474 : : parse_binary_equal_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
475 : : AST::AttrVec outer_attrs,
476 : : ParseRestrictions restrictions
477 : : = ParseRestrictions ());
478 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_not_equal_expr (
479 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
480 : : AST::AttrVec outer_attrs,
481 : : ParseRestrictions restrictions = ParseRestrictions ());
482 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_greater_than_expr (
483 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
484 : : AST::AttrVec outer_attrs,
485 : : ParseRestrictions restrictions = ParseRestrictions ());
486 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_less_than_expr (
487 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
488 : : AST::AttrVec outer_attrs,
489 : : ParseRestrictions restrictions = ParseRestrictions ());
490 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_greater_equal_expr (
491 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
492 : : AST::AttrVec outer_attrs,
493 : : ParseRestrictions restrictions = ParseRestrictions ());
494 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_less_equal_expr (
495 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
496 : : AST::AttrVec outer_attrs,
497 : : ParseRestrictions restrictions = ParseRestrictions ());
498 : : std::unique_ptr<AST::LazyBooleanExpr>
499 : : parse_lazy_or_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
500 : : AST::AttrVec outer_attrs,
501 : : ParseRestrictions restrictions = ParseRestrictions ());
502 : : std::unique_ptr<AST::LazyBooleanExpr>
503 : : parse_lazy_and_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
504 : : AST::AttrVec outer_attrs,
505 : : ParseRestrictions restrictions = ParseRestrictions ());
506 : : std::unique_ptr<AST::TypeCastExpr>
507 : : parse_type_cast_expr (const_TokenPtr tok,
508 : : std::unique_ptr<AST::Expr> expr_to_cast,
509 : : AST::AttrVec outer_attrs,
510 : : ParseRestrictions restrictions = ParseRestrictions ());
511 : : std::unique_ptr<AST::AssignmentExpr>
512 : : parse_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
513 : : AST::AttrVec outer_attrs,
514 : : ParseRestrictions restrictions = ParseRestrictions ());
515 : : std::unique_ptr<AST::CompoundAssignmentExpr> parse_compound_assignment_expr (
516 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
517 : : AST::AttrVec outer_attrs, AST::CompoundAssignmentExpr::ExprType expr_type,
518 : : ParseRestrictions restrictions = ParseRestrictions ());
519 : : std::unique_ptr<AST::CompoundAssignmentExpr>
520 : : parse_plus_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
521 : : AST::AttrVec outer_attrs,
522 : : ParseRestrictions restrictions = ParseRestrictions ());
523 : : std::unique_ptr<AST::CompoundAssignmentExpr>
524 : : parse_minus_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
525 : : AST::AttrVec outer_attrs,
526 : : ParseRestrictions restrictions
527 : : = ParseRestrictions ());
528 : : std::unique_ptr<AST::CompoundAssignmentExpr>
529 : : parse_mult_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
530 : : AST::AttrVec outer_attrs,
531 : : ParseRestrictions restrictions = ParseRestrictions ());
532 : : std::unique_ptr<AST::CompoundAssignmentExpr>
533 : : parse_div_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
534 : : AST::AttrVec outer_attrs,
535 : : ParseRestrictions restrictions = ParseRestrictions ());
536 : : std::unique_ptr<AST::CompoundAssignmentExpr>
537 : : parse_mod_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
538 : : AST::AttrVec outer_attrs,
539 : : ParseRestrictions restrictions = ParseRestrictions ());
540 : : std::unique_ptr<AST::CompoundAssignmentExpr>
541 : : parse_and_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
542 : : AST::AttrVec outer_attrs,
543 : : ParseRestrictions restrictions = ParseRestrictions ());
544 : : std::unique_ptr<AST::CompoundAssignmentExpr>
545 : : parse_or_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
546 : : AST::AttrVec outer_attrs,
547 : : ParseRestrictions restrictions = ParseRestrictions ());
548 : : std::unique_ptr<AST::CompoundAssignmentExpr>
549 : : parse_xor_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
550 : : AST::AttrVec outer_attrs,
551 : : ParseRestrictions restrictions = ParseRestrictions ());
552 : : std::unique_ptr<AST::CompoundAssignmentExpr> parse_left_shift_assig_expr (
553 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
554 : : AST::AttrVec outer_attrs,
555 : : ParseRestrictions restrictions = ParseRestrictions ());
556 : : std::unique_ptr<AST::CompoundAssignmentExpr> parse_right_shift_assig_expr (
557 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
558 : : AST::AttrVec outer_attrs,
559 : : ParseRestrictions restrictions = ParseRestrictions ());
560 : : std::unique_ptr<AST::AwaitExpr>
561 : : parse_await_expr (const_TokenPtr tok,
562 : : std::unique_ptr<AST::Expr> expr_to_await,
563 : : AST::AttrVec outer_attrs);
564 : : std::unique_ptr<AST::MethodCallExpr> parse_method_call_expr (
565 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
566 : : AST::AttrVec outer_attrs,
567 : : ParseRestrictions restrictions = ParseRestrictions ());
568 : : std::unique_ptr<AST::CallExpr> parse_function_call_expr (
569 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> function_expr,
570 : : AST::AttrVec outer_attrs,
571 : : ParseRestrictions restrictions = ParseRestrictions ());
572 : : std::unique_ptr<AST::RangeExpr> parse_led_range_exclusive_expr (
573 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
574 : : AST::AttrVec outer_attrs,
575 : : ParseRestrictions restrictions = ParseRestrictions ());
576 : : std::unique_ptr<AST::RangeExpr>
577 : : parse_nud_range_exclusive_expr (const_TokenPtr tok, AST::AttrVec outer_attrs);
578 : : std::unique_ptr<AST::RangeFromToInclExpr> parse_range_inclusive_expr (
579 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
580 : : AST::AttrVec outer_attrs,
581 : : ParseRestrictions restrictions = ParseRestrictions ());
582 : : std::unique_ptr<AST::RangeToInclExpr>
583 : : parse_range_to_inclusive_expr (const_TokenPtr tok, AST::AttrVec outer_attrs);
584 : : std::unique_ptr<AST::TupleIndexExpr> parse_tuple_index_expr (
585 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
586 : : AST::AttrVec outer_attrs,
587 : : ParseRestrictions restrictions = ParseRestrictions ());
588 : : std::unique_ptr<AST::FieldAccessExpr> parse_field_access_expr (
589 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> struct_expr,
590 : : AST::AttrVec outer_attrs,
591 : : ParseRestrictions restrictions = ParseRestrictions ());
592 : : std::unique_ptr<AST::ArrayIndexExpr>
593 : : parse_index_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> array_expr,
594 : : AST::AttrVec outer_attrs,
595 : : ParseRestrictions restrictions = ParseRestrictions ());
596 : : std::unique_ptr<AST::MacroInvocation> parse_macro_invocation_partial (
597 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
598 : : ParseRestrictions restrictions = ParseRestrictions ());
599 : : std::unique_ptr<AST::StructExprStruct>
600 : : parse_struct_expr_struct_partial (AST::PathInExpression path,
601 : : AST::AttrVec outer_attrs);
602 : : std::unique_ptr<AST::CallExpr>
603 : : parse_struct_expr_tuple_partial (AST::PathInExpression path,
604 : : AST::AttrVec outer_attrs);
605 : : std::unique_ptr<AST::ClosureExpr>
606 : : parse_closure_expr_pratt (const_TokenPtr tok,
607 : : AST::AttrVec outer_attrs = AST::AttrVec ());
608 : : std::unique_ptr<AST::TupleIndexExpr> parse_tuple_index_expr_float (
609 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
610 : : AST::AttrVec outer_attrs,
611 : : ParseRestrictions restrictions = ParseRestrictions ());
612 : :
613 : : // When given a pratt_parsed_loc, use it as the location of the
614 : : // first token parsed in the expression (the parsing of that first
615 : : // token should be skipped).
616 : : std::unique_ptr<AST::IfExpr>
617 : : parse_if_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
618 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
619 : : std::unique_ptr<AST::IfLetExpr>
620 : : parse_if_let_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
621 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
622 : : std::unique_ptr<AST::LoopExpr>
623 : : parse_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
624 : : tl::optional<AST::LoopLabel> label = tl::nullopt,
625 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
626 : : std::unique_ptr<AST::WhileLoopExpr>
627 : : parse_while_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
628 : : tl::optional<AST::LoopLabel> label = tl::nullopt,
629 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
630 : : std::unique_ptr<AST::WhileLetLoopExpr>
631 : : parse_while_let_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
632 : 58 : tl::optional<AST::LoopLabel> label = tl::nullopt);
633 : : std::unique_ptr<AST::ForLoopExpr>
634 : : parse_for_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
635 : : tl::optional<AST::LoopLabel> label = tl::nullopt);
636 : : std::unique_ptr<AST::MatchExpr>
637 : : parse_match_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
638 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
639 : : AST::MatchArm parse_match_arm ();
640 : : std::vector<std::unique_ptr<AST::Pattern>>
641 : : parse_match_arm_patterns (TokenId end_token_id);
642 : : std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
643 : : AST::AttrVec outer_attrs
644 : : = AST::AttrVec ());
645 : : tl::expected<AST::LoopLabel, ParseLoopLabelError>
646 : : parse_loop_label (const_TokenPtr tok);
647 : : std::unique_ptr<AST::AsyncBlockExpr>
648 : : parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());
649 : : std::unique_ptr<AST::GroupedExpr> parse_grouped_expr (AST::AttrVec outer_attrs
650 : : = AST::AttrVec ());
651 : : std::unique_ptr<AST::ClosureExpr> parse_closure_expr (AST::AttrVec outer_attrs
652 : : = AST::AttrVec ());
653 : : AST::ClosureParam parse_closure_param ();
654 : :
655 : : std::unique_ptr<AST::BoxExpr> parse_box_expr (AST::AttrVec outer_attrs,
656 : : location_t pratt_parsed_loc
657 : : = UNKNOWN_LOCATION);
658 : : // When given a pratt_parsed_loc, use it as the location of the
659 : : // first token parsed in the expression (the parsing of that first
660 : : // token should be skipped).
661 : : std::unique_ptr<AST::ReturnExpr>
662 : : parse_return_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
663 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
664 : : std::unique_ptr<AST::TryExpr>
665 : : parse_try_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
666 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
667 : : std::unique_ptr<AST::BreakExpr>
668 : : parse_break_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
669 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
670 : : std::unique_ptr<AST::ContinueExpr>
671 : : parse_continue_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
672 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
673 : : std::unique_ptr<AST::UnsafeBlockExpr>
674 : : parse_unsafe_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
675 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
676 : : std::unique_ptr<AST::ArrayExpr>
677 : : parse_array_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
678 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
679 : : std::unique_ptr<AST::ExprWithoutBlock>
680 : : parse_grouped_or_tuple_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
681 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
682 : : std::unique_ptr<AST::StructExprField> parse_struct_expr_field ();
683 : : bool will_be_expr_with_block ();
684 : :
685 : : // Type-related
686 : : std::unique_ptr<AST::TypeNoBounds> parse_type_no_bounds ();
687 : : std::unique_ptr<AST::TypeNoBounds> parse_slice_or_array_type ();
688 : : std::unique_ptr<AST::RawPointerType> parse_raw_pointer_type ();
689 : : std::unique_ptr<AST::ReferenceType>
690 : : parse_reference_type_inner (location_t locus);
691 : : std::unique_ptr<AST::ReferenceType> parse_reference_type ();
692 : : std::unique_ptr<AST::BareFunctionType>
693 : : parse_bare_function_type (std::vector<AST::LifetimeParam> for_lifetimes);
694 : : std::unique_ptr<AST::Type> parse_paren_prefixed_type ();
695 : : std::unique_ptr<AST::TypeNoBounds> parse_paren_prefixed_type_no_bounds ();
696 : : std::unique_ptr<AST::Type> parse_for_prefixed_type ();
697 : : AST::MaybeNamedParam parse_maybe_named_param (AST::AttrVec outer_attrs);
698 : :
699 : : // Statement-related
700 : :
701 : : /**
702 : : *Parse a let-statement
703 : : * LetStatement :
704 : : * OuterAttribute*
705 : : * 'let' PatternNoTopAlt ( ':' Type )? ('=' Expression )? ';'
706 : : *
707 : : * @param allow_no_semi Allow parsing a let-statement without expecting a
708 : : * semicolon to follow it
709 : : */
710 : : std::unique_ptr<AST::LetStmt> parse_let_stmt (AST::AttrVec outer_attrs,
711 : : ParseRestrictions restrictions
712 : : = ParseRestrictions ());
713 : : std::unique_ptr<AST::Stmt> parse_expr_stmt (AST::AttrVec outer_attrs,
714 : : ParseRestrictions restrictions
715 : : = ParseRestrictions ());
716 : : ExprOrStmt parse_stmt_or_expr ();
717 : :
718 : : // Pattern-related
719 : : std::unique_ptr<AST::Pattern> parse_literal_or_range_pattern ();
720 : : std::unique_ptr<AST::RangePatternBound> parse_range_pattern_bound ();
721 : : std::unique_ptr<AST::ReferencePattern> parse_reference_pattern ();
722 : : std::unique_ptr<AST::Pattern> parse_grouped_or_tuple_pattern ();
723 : : std::unique_ptr<AST::SlicePattern> parse_slice_pattern ();
724 : : std::unique_ptr<AST::Pattern> parse_ident_leading_pattern ();
725 : : std::unique_ptr<AST::TupleStructItems> parse_tuple_struct_items ();
726 : : AST::StructPatternElements parse_struct_pattern_elems ();
727 : : std::unique_ptr<AST::StructPatternField> parse_struct_pattern_field ();
728 : : std::unique_ptr<AST::StructPatternField>
729 : : parse_struct_pattern_field_partial (AST::AttrVec outer_attrs);
730 : :
731 : : int left_binding_power (const_TokenPtr token);
732 : :
733 : : bool done_end ();
734 : : bool done_end_or_else ();
735 : : bool done_end_of_file ();
736 : :
737 : 4867 : void add_error (Error error) { error_table.push_back (std::move (error)); }
738 : :
739 : : public:
740 : : // Construct parser with specified "managed" token source.
741 : 125887 : Parser (ManagedTokenSource &tokenSource) : lexer (tokenSource) {}
742 : :
743 : : // Parse items without parsing an entire crate. This function is the main
744 : : // parsing loop of AST::Crate::parse_crate().
745 : : std::vector<std::unique_ptr<AST::Item>> parse_items ();
746 : :
747 : : // Main entry point for parser.
748 : : std::unique_ptr<AST::Crate> parse_crate ();
749 : :
750 : : void debug_dump_ast_output (AST::Crate &crate, std::ostream &out);
751 : :
752 : : // Returns whether any parsing errors have occurred.
753 : 261460 : bool has_errors () const { return !error_table.empty (); }
754 : : // Remove all parsing errors from the table
755 : 4766 : void clear_errors () { error_table.clear (); }
756 : :
757 : : // Get a reference to the list of errors encountered
758 : 503 : std::vector<Error> &get_errors () { return error_table; }
759 : :
760 : 121017 : const ManagedTokenSource &get_token_source () const { return lexer; }
761 : :
762 : 173726 : const_TokenPtr peek_current_token () { return lexer.peek_token (0); }
763 : 52 : const_TokenPtr peek (int n) { return lexer.peek_token (n); }
764 : :
765 : : private:
766 : : // The token source (usually lexer) associated with the parser.
767 : : ManagedTokenSource &lexer;
768 : : // The error list.
769 : : std::vector<Error> error_table;
770 : : // The names of inline modules while parsing.
771 : : std::vector<std::string> inline_module_stack;
772 : :
773 : : class InlineModuleStackScope
774 : : {
775 : : private:
776 : : Parser &parser;
777 : :
778 : : public:
779 : 1318 : InlineModuleStackScope (Parser &parser, std::string name) : parser (parser)
780 : : {
781 : 1318 : parser.inline_module_stack.emplace_back (std::move (name));
782 : 0 : }
783 : 1318 : ~InlineModuleStackScope () { parser.inline_module_stack.pop_back (); }
784 : : };
785 : :
786 : : // don't want to make things *only* AttributeParser uses public
787 : : // TODO: fold more of AttributeParser into Parser?
788 : : friend struct ::Rust::AST::AttributeParser;
789 : : };
790 : :
791 : : std::string extract_module_path (const AST::AttrVec &inner_attrs,
792 : : const AST::AttrVec &outer_attrs,
793 : : const std::string &name);
794 : :
795 : : /**
796 : : * Check if a MacroMatch is allowed to follow the last parsed MacroMatch.
797 : : *
798 : : * @param last_match Last matcher parsed before the current match
799 : : * @param match Current matcher to check
800 : : *
801 : : * @return true if the follow-up is valid, false otherwise
802 : : */
803 : : bool is_match_compatible (const AST::MacroMatch &last_match,
804 : : const AST::MacroMatch ¤t_match);
805 : : } // namespace Rust
806 : :
807 : : #endif // RUST_PARSE_H
|