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