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