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