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 : 15537 : ExprOrStmt (std::unique_ptr<AST::Expr> expr) : expr (std::move (expr)) {}
70 : :
71 : : // statement constructor
72 : 22099 : 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 : 37637 : bool is_error () const
81 : : {
82 : 37637 : return (expr == nullptr && stmt == nullptr)
83 : 59713 : || (expr != nullptr && stmt != nullptr);
84 : : }
85 : :
86 : : // Returns an error state object.
87 : 1 : static ExprOrStmt create_error () { return ExprOrStmt (nullptr, nullptr); }
88 : :
89 : 37637 : ~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 : 13204 : 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 : 40440 : 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 : : private:
216 : : void skip_after_semicolon ();
217 : : void skip_after_end ();
218 : : void skip_after_end_block ();
219 : : void skip_after_next_block ();
220 : : void skip_after_end_attribute ();
221 : :
222 : : const_TokenPtr expect_token (TokenId t);
223 : : const_TokenPtr expect_token (const_TokenPtr token_expect);
224 : : void unexpected_token (const_TokenPtr t);
225 : : bool skip_generics_right_angle ();
226 : :
227 : : void parse_statement_seq (bool (Parser::*done) ());
228 : :
229 : : // AST-related stuff - maybe move or something?
230 : : AST::Attribute parse_inner_attribute ();
231 : : AST::AttrVec parse_outer_attributes ();
232 : : AST::Attribute parse_outer_attribute ();
233 : : std::unique_ptr<AST::AttrInput> parse_attr_input ();
234 : : std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, location_t>
235 : : parse_doc_comment ();
236 : :
237 : : // Path-related
238 : : AST::SimplePath parse_simple_path ();
239 : : AST::SimplePathSegment parse_simple_path_segment (int base_peek = 0);
240 : : AST::TypePath parse_type_path ();
241 : : std::unique_ptr<AST::TypePathSegment> parse_type_path_segment ();
242 : : AST::PathIdentSegment parse_path_ident_segment ();
243 : : tl::optional<AST::GenericArg> parse_generic_arg ();
244 : : AST::GenericArgs parse_path_generic_args ();
245 : : AST::GenericArgsBinding parse_generic_args_binding ();
246 : : AST::TypePathFunction parse_type_path_function (location_t locus);
247 : : AST::PathExprSegment parse_path_expr_segment ();
248 : : AST::QualifiedPathInExpression
249 : : // When given a pratt_parsed_loc, use it as the location of the
250 : : // first token parsed in the expression (the parsing of that first
251 : : // token should be skipped).
252 : : parse_qualified_path_in_expression (location_t pratt_parsed_loc
253 : : = UNKNOWN_LOCATION);
254 : : AST::QualifiedPathType parse_qualified_path_type (location_t pratt_parsed_loc
255 : : = UNKNOWN_LOCATION);
256 : : AST::QualifiedPathInType parse_qualified_path_in_type ();
257 : :
258 : : // Token tree or macro related
259 : : AST::DelimTokenTree parse_delim_token_tree ();
260 : : std::unique_ptr<AST::MacroRulesDefinition>
261 : : parse_macro_rules_def (AST::AttrVec outer_attrs);
262 : : std::unique_ptr<AST::MacroRulesDefinition>
263 : : parse_decl_macro_def (AST::Visibility vis, AST::AttrVec outer_attrs);
264 : : std::unique_ptr<AST::MacroInvocation>
265 : : parse_macro_invocation_semi (AST::AttrVec outer_attrs);
266 : : AST::MacroRule parse_macro_rule ();
267 : : AST::MacroMatcher parse_macro_matcher ();
268 : : std::unique_ptr<AST::MacroMatch> parse_macro_match ();
269 : : std::unique_ptr<AST::MacroMatchFragment> parse_macro_match_fragment ();
270 : : std::unique_ptr<AST::MacroMatchRepetition> parse_macro_match_repetition ();
271 : :
272 : : // Top-level item-related
273 : : std::unique_ptr<AST::VisItem> parse_vis_item (AST::AttrVec outer_attrs);
274 : :
275 : : // VisItem subclass-related
276 : : std::unique_ptr<AST::Module> parse_module (AST::Visibility vis,
277 : : AST::AttrVec outer_attrs);
278 : : std::unique_ptr<AST::ExternCrate>
279 : : parse_extern_crate (AST::Visibility vis, AST::AttrVec outer_attrs);
280 : : std::unique_ptr<AST::UseDeclaration>
281 : : parse_use_decl (AST::Visibility vis, AST::AttrVec outer_attrs);
282 : : std::unique_ptr<AST::UseTree> parse_use_tree ();
283 : : std::unique_ptr<AST::Function> parse_function (AST::Visibility vis,
284 : : AST::AttrVec outer_attrs,
285 : : bool is_external = false);
286 : : AST::FunctionQualifiers parse_function_qualifiers ();
287 : : std::vector<std::unique_ptr<AST::GenericParam>>
288 : : parse_generic_params_in_angles ();
289 : : template <typename EndTokenPred>
290 : : std::vector<std::unique_ptr<AST::GenericParam>>
291 : : parse_generic_params (EndTokenPred is_end_token);
292 : : template <typename EndTokenPred>
293 : : std::unique_ptr<AST::GenericParam>
294 : : parse_generic_param (EndTokenPred is_end_token);
295 : :
296 : : template <typename EndTokenPred>
297 : : std::vector<std::unique_ptr<AST::LifetimeParam>>
298 : : parse_lifetime_params (EndTokenPred is_end_token);
299 : : std::vector<AST::LifetimeParam> parse_lifetime_params_objs ();
300 : : template <typename EndTokenPred>
301 : : std::vector<AST::LifetimeParam>
302 : : parse_lifetime_params_objs (EndTokenPred is_end_token);
303 : : template <typename ParseFunction, typename EndTokenPred>
304 : : auto parse_non_ptr_sequence (
305 : : ParseFunction parsing_function, EndTokenPred is_end_token,
306 : : std::string error_msg = "failed to parse generic param in generic params")
307 : : -> std::vector<decltype (parsing_function ())>;
308 : : tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
309 : : parse_lifetime_param ();
310 : : std::vector<std::unique_ptr<AST::TypeParam>> parse_type_params ();
311 : : template <typename EndTokenPred>
312 : : std::vector<std::unique_ptr<AST::TypeParam>>
313 : : parse_type_params (EndTokenPred is_end_token);
314 : : std::unique_ptr<AST::TypeParam> parse_type_param ();
315 : : template <typename EndTokenPred>
316 : : std::vector<std::unique_ptr<AST::Param>>
317 : : parse_function_params (EndTokenPred is_end_token);
318 : : std::unique_ptr<AST::Param> parse_function_param ();
319 : : std::unique_ptr<AST::Type> parse_function_return_type ();
320 : : AST::WhereClause parse_where_clause ();
321 : : std::unique_ptr<AST::WhereClauseItem> parse_where_clause_item (
322 : : const std::vector<AST::LifetimeParam> &global_for_lifetimes);
323 : : std::unique_ptr<AST::LifetimeWhereClauseItem>
324 : : parse_lifetime_where_clause_item ();
325 : : std::unique_ptr<AST::TypeBoundWhereClauseItem>
326 : : parse_type_bound_where_clause_item (
327 : : const std::vector<AST::LifetimeParam> &global_for_lifetimes);
328 : : std::vector<AST::LifetimeParam> parse_for_lifetimes ();
329 : : template <typename EndTokenPred>
330 : : std::vector<std::unique_ptr<AST::TypeParamBound>>
331 : : parse_type_param_bounds (EndTokenPred is_end_token);
332 : : std::vector<std::unique_ptr<AST::TypeParamBound>> parse_type_param_bounds ();
333 : : std::unique_ptr<AST::TypeParamBound> parse_type_param_bound ();
334 : : std::unique_ptr<AST::TraitBound> parse_trait_bound ();
335 : : std::vector<AST::Lifetime> parse_lifetime_bounds ();
336 : : template <typename EndTokenPred>
337 : : std::vector<AST::Lifetime> parse_lifetime_bounds (EndTokenPred is_end_token);
338 : : tl::expected<AST::Lifetime, ParseLifetimeError>
339 : : parse_lifetime (bool allow_elided);
340 : : AST::Lifetime lifetime_from_token (const_TokenPtr tok);
341 : : std::unique_ptr<AST::ExternalTypeItem>
342 : : parse_external_type_item (AST::Visibility vis, AST::AttrVec outer_attrs);
343 : :
344 : : std::unique_ptr<AST::TypeAlias> parse_type_alias (AST::Visibility vis,
345 : : AST::AttrVec outer_attrs);
346 : : std::unique_ptr<AST::Struct> parse_struct (AST::Visibility vis,
347 : : AST::AttrVec outer_attrs);
348 : : std::vector<AST::StructField> parse_struct_fields ();
349 : : template <typename EndTokenPred>
350 : : std::vector<AST::StructField> parse_struct_fields (EndTokenPred is_end_token);
351 : : AST::StructField parse_struct_field ();
352 : : std::vector<AST::TupleField> parse_tuple_fields ();
353 : : AST::TupleField parse_tuple_field ();
354 : : std::unique_ptr<AST::Enum> parse_enum (AST::Visibility vis,
355 : : AST::AttrVec outer_attrs);
356 : : std::vector<std::unique_ptr<AST::EnumItem>> parse_enum_items ();
357 : : template <typename EndTokenPred>
358 : : std::vector<std::unique_ptr<AST::EnumItem>>
359 : : parse_enum_items (EndTokenPred is_end_token);
360 : : std::unique_ptr<AST::EnumItem> parse_enum_item ();
361 : : std::unique_ptr<AST::Union> parse_union (AST::Visibility vis,
362 : : AST::AttrVec outer_attrs);
363 : : std::unique_ptr<AST::ConstantItem>
364 : : parse_const_item (AST::Visibility vis, AST::AttrVec outer_attrs);
365 : : std::unique_ptr<AST::StaticItem> parse_static_item (AST::Visibility vis,
366 : : AST::AttrVec outer_attrs);
367 : : std::unique_ptr<AST::Trait> parse_trait (AST::Visibility vis,
368 : : AST::AttrVec outer_attrs);
369 : : std::unique_ptr<AST::TraitItemType>
370 : : parse_trait_type (AST::AttrVec outer_attrs, AST::Visibility);
371 : : std::unique_ptr<AST::TraitItemConst>
372 : : parse_trait_const (AST::AttrVec outer_attrs);
373 : :
374 : : tl::expected<std::unique_ptr<AST::Param>, ParseSelfError> parse_self_param ();
375 : :
376 : : std::unique_ptr<AST::Impl> parse_impl (AST::Visibility vis,
377 : : AST::AttrVec outer_attrs);
378 : : std::unique_ptr<AST::AssociatedItem>
379 : : parse_inherent_impl_function_or_method (AST::Visibility vis,
380 : : AST::AttrVec outer_attrs);
381 : : std::unique_ptr<AST::AssociatedItem>
382 : : parse_trait_impl_function_or_method (AST::Visibility vis,
383 : : AST::AttrVec outer_attrs);
384 : : std::unique_ptr<AST::ExternBlock>
385 : : parse_extern_block (AST::Visibility vis, AST::AttrVec outer_attrs);
386 : : std::unique_ptr<AST::Function> parse_method ();
387 : : std::unique_ptr<AST::Function> parse_async_item (AST::Visibility vis,
388 : : AST::AttrVec outer_attrs);
389 : :
390 : : // Expression-related (Pratt parsed)
391 : : std::unique_ptr<AST::Expr>
392 : : parse_expr (int right_binding_power,
393 : : AST::AttrVec outer_attrs = AST::AttrVec (),
394 : : ParseRestrictions restrictions = ParseRestrictions ());
395 : : std::unique_ptr<AST::Expr>
396 : : null_denotation (const_TokenPtr t, AST::AttrVec outer_attrs = AST::AttrVec (),
397 : : ParseRestrictions restrictions = ParseRestrictions ());
398 : : std::unique_ptr<AST::Expr>
399 : : null_denotation_path (AST::PathInExpression path, AST::AttrVec outer_attrs,
400 : : ParseRestrictions restrictions = ParseRestrictions ());
401 : : std::unique_ptr<AST::Expr>
402 : : null_denotation_not_path (const_TokenPtr t, AST::AttrVec outer_attrs,
403 : : ParseRestrictions restrictions
404 : : = ParseRestrictions ());
405 : : std::unique_ptr<AST::Expr>
406 : : left_denotations (std::unique_ptr<AST::Expr> null_denotation,
407 : : int right_binding_power, AST::AttrVec outer_attrs,
408 : : ParseRestrictions restrictions = ParseRestrictions ());
409 : : std::unique_ptr<AST::Expr>
410 : : left_denotation (const_TokenPtr t, std::unique_ptr<AST::Expr> left,
411 : : AST::AttrVec outer_attrs = AST::AttrVec (),
412 : : ParseRestrictions restrictions = ParseRestrictions ());
413 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
414 : : parse_arithmetic_or_logical_expr (
415 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
416 : : AST::AttrVec outer_attrs, AST::ArithmeticOrLogicalExpr::ExprType expr_type,
417 : : ParseRestrictions restrictions = ParseRestrictions ());
418 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
419 : : parse_binary_plus_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
420 : : AST::AttrVec outer_attrs,
421 : : ParseRestrictions restrictions
422 : : = ParseRestrictions ());
423 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
424 : : parse_binary_minus_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
425 : : AST::AttrVec outer_attrs,
426 : : ParseRestrictions restrictions
427 : : = ParseRestrictions ());
428 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
429 : : parse_binary_mult_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
430 : : AST::AttrVec outer_attrs,
431 : : ParseRestrictions restrictions
432 : : = ParseRestrictions ());
433 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
434 : : parse_binary_div_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
435 : : AST::AttrVec outer_attrs,
436 : : ParseRestrictions restrictions = ParseRestrictions ());
437 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
438 : : parse_binary_mod_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_bitwise_and_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
443 : : AST::AttrVec outer_attrs,
444 : : ParseRestrictions restrictions
445 : : = ParseRestrictions ());
446 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
447 : : parse_bitwise_or_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
448 : : AST::AttrVec outer_attrs,
449 : : ParseRestrictions restrictions = ParseRestrictions ());
450 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
451 : : parse_bitwise_xor_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
452 : : AST::AttrVec outer_attrs,
453 : : ParseRestrictions restrictions
454 : : = ParseRestrictions ());
455 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
456 : : parse_left_shift_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
457 : : AST::AttrVec outer_attrs,
458 : : ParseRestrictions restrictions = ParseRestrictions ());
459 : : std::unique_ptr<AST::ArithmeticOrLogicalExpr>
460 : : parse_right_shift_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
461 : : AST::AttrVec outer_attrs,
462 : : ParseRestrictions restrictions
463 : : = ParseRestrictions ());
464 : : std::unique_ptr<AST::ComparisonExpr>
465 : : parse_comparison_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
466 : : AST::AttrVec outer_attrs,
467 : : AST::ComparisonExpr::ExprType expr_type,
468 : : ParseRestrictions restrictions = ParseRestrictions ());
469 : : std::unique_ptr<AST::ComparisonExpr>
470 : : parse_binary_equal_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
471 : : AST::AttrVec outer_attrs,
472 : : ParseRestrictions restrictions
473 : : = ParseRestrictions ());
474 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_not_equal_expr (
475 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
476 : : AST::AttrVec outer_attrs,
477 : : ParseRestrictions restrictions = ParseRestrictions ());
478 : : std::unique_ptr<AST::ComparisonExpr> parse_binary_greater_than_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_less_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_greater_equal_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_less_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::LazyBooleanExpr>
495 : : parse_lazy_or_expr (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_and_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
500 : : AST::AttrVec outer_attrs,
501 : : ParseRestrictions restrictions = ParseRestrictions ());
502 : : std::unique_ptr<AST::TypeCastExpr>
503 : : parse_type_cast_expr (const_TokenPtr tok,
504 : : std::unique_ptr<AST::Expr> expr_to_cast,
505 : : AST::AttrVec outer_attrs,
506 : : ParseRestrictions restrictions = ParseRestrictions ());
507 : : std::unique_ptr<AST::AssignmentExpr>
508 : : parse_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
509 : : AST::AttrVec outer_attrs,
510 : : ParseRestrictions restrictions = ParseRestrictions ());
511 : : std::unique_ptr<AST::CompoundAssignmentExpr> parse_compound_assignment_expr (
512 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
513 : : AST::AttrVec outer_attrs, AST::CompoundAssignmentExpr::ExprType expr_type,
514 : : ParseRestrictions restrictions = ParseRestrictions ());
515 : : std::unique_ptr<AST::CompoundAssignmentExpr>
516 : : parse_plus_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
517 : : AST::AttrVec outer_attrs,
518 : : ParseRestrictions restrictions = ParseRestrictions ());
519 : : std::unique_ptr<AST::CompoundAssignmentExpr>
520 : : parse_minus_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
521 : : AST::AttrVec outer_attrs,
522 : : ParseRestrictions restrictions
523 : : = ParseRestrictions ());
524 : : std::unique_ptr<AST::CompoundAssignmentExpr>
525 : : parse_mult_assig_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
526 : : AST::AttrVec outer_attrs,
527 : : ParseRestrictions restrictions = ParseRestrictions ());
528 : : std::unique_ptr<AST::CompoundAssignmentExpr>
529 : : parse_div_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_mod_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_and_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_or_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_xor_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> parse_left_shift_assig_expr (
549 : : 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_right_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::AwaitExpr>
557 : : parse_await_expr (const_TokenPtr tok,
558 : : std::unique_ptr<AST::Expr> expr_to_await,
559 : : AST::AttrVec outer_attrs);
560 : : std::unique_ptr<AST::MethodCallExpr> parse_method_call_expr (
561 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> receiver_expr,
562 : : AST::AttrVec outer_attrs,
563 : : ParseRestrictions restrictions = ParseRestrictions ());
564 : : std::unique_ptr<AST::CallExpr> parse_function_call_expr (
565 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> function_expr,
566 : : AST::AttrVec outer_attrs,
567 : : ParseRestrictions restrictions = ParseRestrictions ());
568 : : std::unique_ptr<AST::RangeExpr> parse_led_range_exclusive_expr (
569 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
570 : : AST::AttrVec outer_attrs,
571 : : ParseRestrictions restrictions = ParseRestrictions ());
572 : : std::unique_ptr<AST::RangeExpr>
573 : : parse_nud_range_exclusive_expr (const_TokenPtr tok, AST::AttrVec outer_attrs);
574 : : std::unique_ptr<AST::RangeFromToInclExpr> parse_range_inclusive_expr (
575 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> left,
576 : : AST::AttrVec outer_attrs,
577 : : ParseRestrictions restrictions = ParseRestrictions ());
578 : : std::unique_ptr<AST::RangeToInclExpr>
579 : : parse_range_to_inclusive_expr (const_TokenPtr tok, AST::AttrVec outer_attrs);
580 : : std::unique_ptr<AST::TupleIndexExpr> parse_tuple_index_expr (
581 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
582 : : AST::AttrVec outer_attrs,
583 : : ParseRestrictions restrictions = ParseRestrictions ());
584 : : std::unique_ptr<AST::FieldAccessExpr> parse_field_access_expr (
585 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> struct_expr,
586 : : AST::AttrVec outer_attrs,
587 : : ParseRestrictions restrictions = ParseRestrictions ());
588 : : std::unique_ptr<AST::ArrayIndexExpr>
589 : : parse_index_expr (const_TokenPtr tok, std::unique_ptr<AST::Expr> array_expr,
590 : : AST::AttrVec outer_attrs,
591 : : ParseRestrictions restrictions = ParseRestrictions ());
592 : : std::unique_ptr<AST::MacroInvocation> parse_macro_invocation_partial (
593 : : AST::PathInExpression path, AST::AttrVec outer_attrs,
594 : : ParseRestrictions restrictions = ParseRestrictions ());
595 : : std::unique_ptr<AST::StructExprStruct>
596 : : parse_struct_expr_struct_partial (AST::PathInExpression path,
597 : : AST::AttrVec outer_attrs);
598 : : std::unique_ptr<AST::CallExpr>
599 : : parse_struct_expr_tuple_partial (AST::PathInExpression path,
600 : : AST::AttrVec outer_attrs);
601 : : AST::PathInExpression parse_path_in_expression_pratt (const_TokenPtr tok);
602 : : std::unique_ptr<AST::ClosureExpr>
603 : : parse_closure_expr_pratt (const_TokenPtr tok,
604 : : AST::AttrVec outer_attrs = AST::AttrVec ());
605 : : std::unique_ptr<AST::TupleIndexExpr> parse_tuple_index_expr_float (
606 : : const_TokenPtr tok, std::unique_ptr<AST::Expr> tuple_expr,
607 : : AST::AttrVec outer_attrs,
608 : : ParseRestrictions restrictions = ParseRestrictions ());
609 : :
610 : : // When given a pratt_parsed_loc, use it as the location of the
611 : : // first token parsed in the expression (the parsing of that first
612 : : // token should be skipped).
613 : : std::unique_ptr<AST::IfExpr>
614 : : parse_if_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
615 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
616 : : std::unique_ptr<AST::IfLetExpr>
617 : : parse_if_let_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
618 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
619 : : std::unique_ptr<AST::LoopExpr>
620 : : parse_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
621 : : tl::optional<AST::LoopLabel> label = tl::nullopt,
622 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
623 : : std::unique_ptr<AST::WhileLoopExpr>
624 : : parse_while_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
625 : : tl::optional<AST::LoopLabel> label = tl::nullopt,
626 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
627 : : std::unique_ptr<AST::WhileLetLoopExpr>
628 : : parse_while_let_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
629 : 6 : tl::optional<AST::LoopLabel> label = tl::nullopt);
630 : : std::unique_ptr<AST::ForLoopExpr>
631 : : parse_for_loop_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
632 : : tl::optional<AST::LoopLabel> label = tl::nullopt);
633 : : std::unique_ptr<AST::MatchExpr>
634 : : parse_match_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
635 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
636 : : AST::MatchArm parse_match_arm ();
637 : : std::vector<std::unique_ptr<AST::Pattern>>
638 : : parse_match_arm_patterns (TokenId end_token_id);
639 : : std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
640 : : AST::AttrVec outer_attrs
641 : : = AST::AttrVec ());
642 : : tl::expected<AST::LoopLabel, ParseLoopLabelError>
643 : : parse_loop_label (const_TokenPtr tok);
644 : : std::unique_ptr<AST::AsyncBlockExpr>
645 : : parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());
646 : : std::unique_ptr<AST::GroupedExpr> parse_grouped_expr (AST::AttrVec outer_attrs
647 : : = AST::AttrVec ());
648 : : std::unique_ptr<AST::ClosureExpr> parse_closure_expr (AST::AttrVec outer_attrs
649 : : = AST::AttrVec ());
650 : : AST::ClosureParam parse_closure_param ();
651 : :
652 : : std::unique_ptr<AST::BoxExpr> parse_box_expr (AST::AttrVec outer_attrs,
653 : : location_t pratt_parsed_loc
654 : : = UNKNOWN_LOCATION);
655 : : // When given a pratt_parsed_loc, use it as the location of the
656 : : // first token parsed in the expression (the parsing of that first
657 : : // token should be skipped).
658 : : std::unique_ptr<AST::ReturnExpr>
659 : : parse_return_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
660 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
661 : : std::unique_ptr<AST::TryExpr>
662 : : parse_try_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
663 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
664 : : std::unique_ptr<AST::BreakExpr>
665 : : parse_break_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
666 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
667 : : std::unique_ptr<AST::ContinueExpr>
668 : : parse_continue_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
669 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
670 : : std::unique_ptr<AST::UnsafeBlockExpr>
671 : : parse_unsafe_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
672 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
673 : : std::unique_ptr<AST::ArrayExpr>
674 : : parse_array_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
675 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
676 : : std::unique_ptr<AST::ExprWithoutBlock>
677 : : parse_grouped_or_tuple_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
678 : : location_t pratt_parsed_loc = UNKNOWN_LOCATION);
679 : : std::unique_ptr<AST::StructExprField> parse_struct_expr_field ();
680 : : bool will_be_expr_with_block ();
681 : :
682 : : // Type-related
683 : : std::unique_ptr<AST::TypeNoBounds> parse_type_no_bounds ();
684 : : std::unique_ptr<AST::TypeNoBounds> parse_slice_or_array_type ();
685 : : std::unique_ptr<AST::RawPointerType> parse_raw_pointer_type ();
686 : : std::unique_ptr<AST::ReferenceType>
687 : : parse_reference_type_inner (location_t locus);
688 : : std::unique_ptr<AST::ReferenceType> parse_reference_type ();
689 : : std::unique_ptr<AST::BareFunctionType>
690 : : parse_bare_function_type (std::vector<AST::LifetimeParam> for_lifetimes);
691 : : std::unique_ptr<AST::Type> parse_paren_prefixed_type ();
692 : : std::unique_ptr<AST::TypeNoBounds> parse_paren_prefixed_type_no_bounds ();
693 : : std::unique_ptr<AST::Type> parse_for_prefixed_type ();
694 : : AST::MaybeNamedParam parse_maybe_named_param (AST::AttrVec outer_attrs);
695 : :
696 : : // Statement-related
697 : :
698 : : /**
699 : : *Parse a let-statement
700 : : * LetStatement :
701 : : * OuterAttribute*
702 : : * 'let' PatternNoTopAlt ( ':' Type )? ('=' Expression )? ';'
703 : : *
704 : : * @param allow_no_semi Allow parsing a let-statement without expecting a
705 : : * semicolon to follow it
706 : : */
707 : : std::unique_ptr<AST::LetStmt> parse_let_stmt (AST::AttrVec outer_attrs,
708 : : ParseRestrictions restrictions
709 : : = ParseRestrictions ());
710 : : std::unique_ptr<AST::Stmt> parse_expr_stmt (AST::AttrVec outer_attrs,
711 : : ParseRestrictions restrictions
712 : : = ParseRestrictions ());
713 : : ExprOrStmt parse_stmt_or_expr ();
714 : :
715 : : // Pattern-related
716 : : std::unique_ptr<AST::Pattern> parse_literal_or_range_pattern ();
717 : : std::unique_ptr<AST::RangePatternBound> parse_range_pattern_bound ();
718 : : std::unique_ptr<AST::ReferencePattern> parse_reference_pattern ();
719 : : std::unique_ptr<AST::Pattern> parse_grouped_or_tuple_pattern ();
720 : : std::unique_ptr<AST::SlicePattern> parse_slice_pattern ();
721 : : std::unique_ptr<AST::Pattern> parse_ident_leading_pattern ();
722 : : std::unique_ptr<AST::TupleStructItems> parse_tuple_struct_items ();
723 : : AST::StructPatternElements parse_struct_pattern_elems ();
724 : : std::unique_ptr<AST::StructPatternField> parse_struct_pattern_field ();
725 : : std::unique_ptr<AST::StructPatternField>
726 : : parse_struct_pattern_field_partial (AST::AttrVec outer_attrs);
727 : :
728 : : int left_binding_power (const_TokenPtr token);
729 : :
730 : : bool done_end ();
731 : : bool done_end_or_else ();
732 : : bool done_end_of_file ();
733 : :
734 : 2217 : void add_error (Error error) { error_table.push_back (std::move (error)); }
735 : :
736 : : public:
737 : : // Construct parser with specified "managed" token source.
738 : 13206 : Parser (ManagedTokenSource &tokenSource) : lexer (tokenSource) {}
739 : :
740 : : // Parse items without parsing an entire crate. This function is the main
741 : : // parsing loop of AST::Crate::parse_crate().
742 : : std::vector<std::unique_ptr<AST::Item>> parse_items ();
743 : :
744 : : // Main entry point for parser.
745 : : std::unique_ptr<AST::Crate> parse_crate ();
746 : :
747 : : void debug_dump_ast_output (AST::Crate &crate, std::ostream &out);
748 : :
749 : : // Returns whether any parsing errors have occurred.
750 : 10501 : bool has_errors () const { return !error_table.empty (); }
751 : : // Remove all parsing errors from the table
752 : 1703 : void clear_errors () { error_table.clear (); }
753 : :
754 : : // Get a reference to the list of errors encountered
755 : 31 : std::vector<Error> &get_errors () { return error_table; }
756 : :
757 : 8018 : const ManagedTokenSource &get_token_source () const { return lexer; }
758 : :
759 : 18077 : const_TokenPtr peek_current_token () { return lexer.peek_token (0); }
760 : 0 : const_TokenPtr peek (int n) { return lexer.peek_token (n); }
761 : :
762 : : private:
763 : : // The token source (usually lexer) associated with the parser.
764 : : ManagedTokenSource &lexer;
765 : : // The error list.
766 : : std::vector<Error> error_table;
767 : : // The names of inline modules while parsing.
768 : : std::vector<std::string> inline_module_stack;
769 : :
770 : : class InlineModuleStackScope
771 : : {
772 : : private:
773 : : Parser &parser;
774 : :
775 : : public:
776 : 1202 : InlineModuleStackScope (Parser &parser, std::string name) : parser (parser)
777 : : {
778 : 1202 : parser.inline_module_stack.emplace_back (std::move (name));
779 : 0 : }
780 : 1202 : ~InlineModuleStackScope () { parser.inline_module_stack.pop_back (); }
781 : : };
782 : :
783 : : // don't want to make things *only* AttributeParser uses public
784 : : // TODO: fold more of AttributeParser into Parser?
785 : : friend class ::Rust::AST::AttributeParser;
786 : : };
787 : :
788 : : std::string extract_module_path (const AST::AttrVec &inner_attrs,
789 : : const AST::AttrVec &outer_attrs,
790 : : const std::string &name);
791 : :
792 : : /**
793 : : * Check if a MacroMatch is allowed to follow the last parsed MacroMatch.
794 : : *
795 : : * @param last_match Last matcher parsed before the current match
796 : : * @param match Current matcher to check
797 : : *
798 : : * @return true if the follow-up is valid, false otherwise
799 : : */
800 : : bool is_match_compatible (const AST::MacroMatch &last_match,
801 : : const AST::MacroMatch ¤t_match);
802 : : } // namespace Rust
803 : :
804 : : #endif // RUST_PARSE_H
|