Struct rustc_parse::parser::Parser

source ·
pub struct Parser<'a> {
Show 18 fields pub sess: &'a ParseSess, pub token: Token, pub token_spacing: Spacing, pub prev_token: Token, pub capture_cfg: bool, restrictions: Restrictions, expected_tokens: Vec<TokenType>, token_cursor: TokenCursor, desugar_doc_comments: bool, unmatched_angle_bracket_count: u32, max_angle_bracket_count: u32, pub(crate) unclosed_delims: Vec<UnmatchedBrace>, last_unexpected_token_span: Option<Span>, pub last_type_ascription: Option<(Span, bool)>, subparser_name: Option<&'static str>, capture_state: CaptureState, pub current_closure: Option<ClosureSpans>, pub recovery: Recovery,
}

Fields§

§sess: &'a ParseSess§token: Token

The current token.

§token_spacing: Spacing

The spacing for the current token

§prev_token: Token

The previous token.

§capture_cfg: bool§restrictions: Restrictions§expected_tokens: Vec<TokenType>§token_cursor: TokenCursor§desugar_doc_comments: bool§unmatched_angle_bracket_count: u32

This field is used to keep track of how many left angle brackets we have seen. This is required in order to detect extra leading left angle brackets (< characters) and error appropriately.

See the comments in the parse_path_segment function for more details.

§max_angle_bracket_count: u32§unclosed_delims: Vec<UnmatchedBrace>

A list of all unclosed delimiters found by the lexer. If an entry is used for error recovery it gets removed from here. Every entry left at the end gets emitted as an independent error.

§last_unexpected_token_span: Option<Span>§last_type_ascription: Option<(Span, bool)>

Span pointing at the : for the last type ascription the parser has seen, and whether it looked like it could have been a mistyped path or literal Option:Some(42)).

§subparser_name: Option<&'static str>

If present, this Parser is not parsing Rust code but rather a macro call.

§capture_state: CaptureState§current_closure: Option<ClosureSpans>

This allows us to recover when the user forget to add braces around multiple statements in the closure body.

§recovery: Recovery

Whether the parser is allowed to do recovery. This is disabled when parsing macro arguments, see #103534

Implementations§

Parses attributes that appear before an item.

Matches attribute = # ! [ meta_item ]. inner_parse_policy prescribes how to handle inner attributes.

Parses an inner part of an attribute (the path and following tokens). The tokens must be either a delimited token stream, or empty token stream, or the “legacy” key-value form. PATH ( TOKEN_STREAM ) PATH [ TOKEN_STREAM ] PATH { TOKEN_STREAM } PATH PATH = UNSUFFIXED_LIT The delimiters or = are still put into the resulting token stream.

Parses attributes that appear after the opening of an item. These should be preceded by an exclamation mark, but we accept and warn about one terminated by a semicolon.

Matches inner_attrs*.

Parses cfg_attr(pred, attr_item_list) where attr_item_list is comma-delimited.

Matches COMMASEP(meta_item_inner).

Matches the following grammar (per RFC 1559).

meta_item : PATH ( '=' UNSUFFIXED_LIT | '(' meta_item_inner? ')' )? ;
meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;

Matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;.

Records all tokens consumed by the provided callback, including the current token. These tokens are collected into a LazyAttrTokenStream, and returned along with the result of the callback.

Note: If your callback consumes an opening delimiter (including the case where you call collect_tokens when the current token is an opening delimiter), you must also consume the corresponding closing delimiter.

That is, you can consume something ([{ }]) or ([{}]), but not ([{}]

This restriction shouldn’t be an issue in practice, since this function is used to record the tokens for a parsed AST item, which always has matching delimiters.

Replace self with snapshot.parser and extend unclosed_delims with snapshot.unclosed_delims. This is to avoid losing unclosed delims errors create_snapshot_for_diagnostic clears.

Create a snapshot of the Parser.

Eats and discards tokens until one of kets is encountered. Respects token trees, passes through any errors encountered. Used for error recovery.

This function checks if there are trailing angle brackets and produces a diagnostic to suggest removing them.

let _ = [1, 2, 3].into_iter().collect::<Vec<usize>>>>();
                                                   ^^ help: remove extra angle brackets

If true is returned, then trailing brackets were recovered, tokens were consumed up until one of the tokens in ‘end’ was encountered, and an error was emitted.

Check if a method call with an intended turbofish has been written without surrounding angle brackets.

When writing a turbofish with multiple type parameters missing the leading ::, we will encounter a parse error when encountering the first ,.

Check to see if a pair of chained operators looks like an attempt at chained comparison, e.g. 1 < x <= 3. If so, suggest either splitting the comparison into two, or parenthesising the leftmost comparison.

Produces an error if comparison operators are chained (RFC #558). We only need to check the LHS, not the RHS, because all comparison ops have same precedence (see fn precedence) and are left-associative (see fn fixity).

This can also be hit if someone incorrectly writes foo<bar>() when they should have used the turbofish (foo::<bar>()) syntax. We attempt some heuristic recovery if that is the case.

Keep in mind that given that outer_op.is_comparison() holds and comparison ops are left associative we can infer that we have:

          outer_op
          /   \
    inner_op   r2
       /  \
     l1    r1

Swift lets users write Ty? to mean Option<Ty>. Parse the construct and recover from it.

Tries to recover from associated item paths like [T]::AssocItem / (T, U)::AssocItem. Attempts to convert the base expression/pattern/type into a type, parses the ::AssocItem tail, and combines them into a <Ty>::AssocItem expression/pattern/type.

Given an already parsed Ty, parses the ::AssocItem tail and combines them into a <Ty>::AssocItem expression/pattern/type.

Creates a DiagnosticBuilder for an unexpected token t and tries to recover if it is a closing delimiter.

Consumes alternative await syntaxes like await!(<expr>), await <expr>, await? <expr>, await(<expr>), and await { <expr> }.

If encountering future.await(), consumes and emits an error.

Recovers a situation like for ( $pat in $expr ) and suggest writing for $pat in $expr instead.

This should be called before parsing the $block.

Eats tokens until we can be relatively sure we reached the end of the statement. This is something of a best-effort heuristic.

We terminate when we find an unmatched } (without consuming it).

If break_on_semi is Break, then we will stop consuming tokens after finding (and consuming) a ; outside of {} or [] (note that this is approximate – it can mean we break too early due to macros, but that should only lead to sub-optimal recovery, not inaccurate parsing).

If break_on_block is Break, then we will stop consuming tokens after finding (and consuming) a brace-delimited block.

Replace duplicated recovered parameters with _ pattern to avoid unnecessary errors.

This is necessary because at this point we don’t know whether we parsed a function with anonymous parameters or a function with names but no types. In order to minimize unnecessary errors, we assume the parameters are in the shape of fn foo(a, b, c) where the parameters are names (so we don’t emit errors about not being able to find b in the local scope), but if we find the same name multiple times, like in fn foo(i8, i8), we deduplicate them to not complain about duplicated parameter names.

Handle encountering a symbol in a generic argument list that is not a , or >. In this case, we emit an error and try to suggest enclosing a const argument in braces if it looks like the user has forgotten them.

Attempt to parse a generic const argument that has not been enclosed in braces. There are a limited number of expressions that are permitted without being encoded in braces:

  • Literals.
  • Single-segment paths (i.e. standalone generic const parameters). All other expressions that can be parsed will emit an error suggesting the expression be wrapped in braces.

Try to recover from possible generic const argument without { and }.

When encountering code like foo::< bar + 3 > or foo::< bar - baz > we suggest foo::<{ bar + 3 }> and foo::<{ bar - baz }>, respectively. We only provide a suggestion if we think that the resulting expression would be well formed.

Creates a dummy const argument, and reports that the expression must be enclosed in braces

Some special error handling for the “top-level” patterns in a match arm, for loop, let, &c. (in contrast to subpatterns within such).

Some special error handling for the “top-level” patterns in a match arm, for loop, let, &c. (in contrast to subpatterns within such).

Parse and throw away a parenthesized comma separated sequence of patterns until ) is reached.

Parses an expression.

Parses an expression, forcing tokens to be collected

Parses a sequence of expressions delimited by parentheses.

Parses an expression, subject to the given restrictions.

Parses an associative expression.

This parses an expression accounting for associativity and precedence of the operators in the expression.

Parses an associative expression with operators of at least min_prec precedence.

We’ve found an expression that would be parsed as a statement, but the next token implies this should be parsed as an expression. For example: if let Some(x) = x { x } else { 0 } / 2.

Possibly translate the current token to an associative operator. The method does not advance the current token.

Also performs recovery for and / or which are mistaken for && and || respectively.

Checks if this expression is a successfully parsed statement.

Parses x..y, x..=y, and x../x..=. The other two variants are handled in parse_prefix_range_expr below.

Parses prefix-forms of range notation: ..expr, .., ..=expr.

Parses a prefix-unary-operator expr.

Parse box expr.

Recover on not expr in favor of !expr.

Returns the span of expr, if it was not interpolated or the span of the interpolated token.

Parses a postfix operators such as ., ?, or index ([]) after a cast, then emits an error and returns the newly parsed tree. The resulting parse tree for &x as T[0] has a precedence of ((&x) as T)[0].

Parse & mut? <expr> or & raw [ const | mut ] <expr>.

Parse mut? or raw [ const | mut ].

Parses a.b or a(13) or a[4] or just a.

Parse a function call expression, expr(...).

If we encounter a parser state that looks like the user has written a struct literal with parentheses instead of braces, recover the parser state and provide suggestions.

Parse an indexing expression expr[...].

Assuming we have just parsed ., continue parsing into an expression.

At the bottom (top?) of the precedence hierarchy, Parses things like parenthesized exprs, macros, return, etc.

N.B., this does not parse outer attributes, and is private because it only works correctly if called from parse_dot_or_call_expr().

Parse 'label: $expr. The label is already parsed.

Emit an error when a char is parsed as a lifetime because of a missing quote.

Recover on the syntax do catch { ... } suggesting try { ... } instead.

Parse an expression if the token can begin one.

Parse "return" expr?.

Parse "do" "yeet" expr?.

Parse "break" (('label (:? expr)?) | expr?) with "break" token already eaten. If the label is followed immediately by a : token, the label and : are parsed as part of the expression (i.e. a labeled loop). The language team has decided in #87026 to require parentheses as a visual aid to avoid confusion if the break expression of an unlabeled break is a labeled loop (as in break 'lbl: loop {}); a labeled break with an unlabeled loop as its value expression only gets a warning for compatibility reasons; and a labeled break with a labeled loop does not even get a warning because there is no ambiguity.

Parse "yield" expr?.

Returns a string literal if the next token is a string literal. In case of error returns Some(lit) if the next token is a literal with a wrong kind, and returns None if the next token is not literal at all.

Matches lit = true | false | token_lit. Returns None if the next token is not a literal.

Matches lit = true | false | token_lit. Returns None if the next token is not a literal.

Matches '-' lit | lit (cf. ast_validation::AstValidator::check_expr_within_pat). Keep this in sync with Token::can_begin_literal_maybe_minus.

Emits a suggestion if it looks like the user meant an array but accidentally used braces, causing the code to be interpreted as a block expression.

Parses a block or unsafe block.

Parse a block which takes no attributes and has no label

Parses a closure expression (e.g., move |args| expr).

Parses an optional move prefix to a closure-like construct.

Parses the |arg, arg| header of a closure.

Parses a parameter in a closure header (e.g., |arg, arg|).

Parses an if expression (if token already eaten).

Parses the condition of a if or while expression.

Parses a let $pat = $expr pseudo-expression.

Parses an else { ... } expression (else token already eaten).

Parses for <src_pat> in <src_expr> <src_loop_block> (for token already eaten).

Parses a while or while let expression (while token already eaten).

Parses loop { ... } (loop token already eaten).

Parses a match ... { ... } expression (match token already eaten).

Attempt to recover from match arm body with statements and no surrounding braces.

Parses a try {...} expression (try token already eaten).

Parses an async move? {...} expression.

Precondition: already parsed the ‘{’.

Use in case of error after field-looking code: S { foo: () with a }.

Parses ident (COLON expr)?.

Check for =. This means the source incorrectly attempts to initialize a field with an eq rather than a colon.

Create expression span ensuring the span of the parent node is larger than the span of lhs and rhs, including the attributes.

Parses bounds of a lifetime parameter BOUND + BOUND + BOUND, possibly with trailing +.

BOUND = LT_BOUND (e.g., `'a`)

Matches typaram = IDENT (? unbound)? optbounds ( EQ ty )?.

Parses a (possibly empty) list of lifetime and type parameters, possibly including a trailing comma and erroneous trailing attributes.

Parses a set of optional generic type parameter declarations. Where clauses are not parsed here, and must be added later via parse_where_clause().

matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > ) | ( < lifetimes , typaramseq ( , )? > ) where typaramseq = ( typaram ) | ( typaram , typaramseq )

Parses an optional where-clause and places it in generics.

where T : Trait<U, V> + 'b, 'a : 'b

Parses a source module as a crate. This is the main entry point for the parser.

Parses a mod <foo> { ... } or mod <foo>; item.

Parses the contents of a module (inner attributes followed by module items).

Error in-case a non-inherited visibility was parsed but no item followed.

Error in-case a default was parsed but no item followed.

Error in-case default was parsed in an in-appropriate context.

Parses one of the items allowed by the flags.

When parsing a statement, would the start of a path be an item?

Are we sure this could not possibly be a macro invocation?

Recover on encountering a struct or method definition where the user forgot to add the struct or fn keyword after writing pub: pub S {}.

Parses an item macro, e.g., item!();.

Recover if we parsed attributes and expected an item but there was none.

Parses an implementation item.

impl<'a, T> TYPE { /* impl items */ }
impl<'a, T> TRAIT for TYPE { /* impl items */ }
impl<'a, T> !TRAIT for TYPE { /* impl items */ }
impl<'a, T> const TRAIT for TYPE { /* impl items */ }

We actually parse slightly more relaxed grammar for better error reporting and recovery.

"impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
"impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"

Recover on a doc comment before }.

Parses defaultness (i.e., default or nothing).

Is this an (unsafe auto? | auto) trait item?

Parses unsafe? auto? trait Foo { ... } or trait Foo = Bar;.

Parses associated items.

Parses a type alias with the following grammar:

TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;

The "type" has already been eaten.

Parses a UseTree.

USE_TREE = [`::`] `*` |
           [`::`] `{` USE_TREE_LIST `}` |
           PATH `::` `*` |
           PATH `::` `{` USE_TREE_LIST `}` |
           PATH [`as` IDENT]

Parses * or {...}.

Parses a UseTreeKind::Nested(list).

USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]

Parses extern crate links.

Examples
extern crate foo;
extern crate bar as foo;

Parses extern for foreign ABIs modules.

extern is expected to have been consumed before calling this method.

Examples
extern "C" {}
extern {}

Parses a foreign item (one in an extern { ... } block).

Recover on const mut with const already eaten.

Recover on const impl with const already eaten.

Parse ["const" | ("static" "mut"?)] $ident ":" $ty (= $expr)? with ["const" | ("static" "mut"?)] already parsed and stored in m.

When m is "const", $ident may also be "_".

We were supposed to parse ":" $ty but the : or the type was missing. This means that the type is missing.

Parses an enum declaration.

Parses struct Foo { ... }.

Parses union Foo { ... }.

Parses an element of a struct declaration.

Parses a structure field declaration.

Parses a structure field.

Parses a field identifier. Specialized version of parse_ident_common for better diagnostics and suggestions.

Parses a declarative macro 2.0 definition. The macro keyword has already been parsed.

MacBody = "{" TOKEN_STREAM "}" ;
MacParams = "(" TOKEN_STREAM ")" ;
DeclMac = "macro" Ident MacParams? MacBody ;

Is this a possibly malformed start of a macro_rules! foo item definition?

Parses a macro_rules! foo { ... } declarative macro.

Item macro invocations or macro_rules! definitions need inherited visibility. If that’s not the case, emit an error.

Checks if current token is one of tokens which cannot be nested like kw::Enum. In case it is, we try to parse the item and report error about nested types.

Parsing of functions and methods.

Parse a function starting from the front matter (const ...) to the body { ... } or ;.

Parse the “body” of a function. This can either be ; when there’s no body, or e.g. a block when the function is a provided one.

Is the current token the start of an FnHeader / not a valid parse?

check_pub adds additional pub to the checks in case users place it wrongly, can be used to ensure pub never comes after default.

Parses all the “front matter” (or “qualifiers”) for a fn declaration, up to and including the fn keyword. The formal grammar is:

Extern = "extern" StringLit? ;
FnQual = "const"? "async"? "unsafe"? Extern? ;
FnFrontMatter = FnQual "fn" ;

vis represents the visibility that was already parsed, if any. Use Visibility::Inherited when no visibility is known.

We are parsing async fn. If we are on Rust 2015, emit an error.

Parses the parameter list and result type of a function declaration.

Parses the parameter list of a function, including the ( and ) delimiters.

Parses a single function parameter.

  • self is syntactically allowed when first_param holds.

Returns the parsed optional self parameter and whether a self shortcut was used.

Checks whether a non-terminal may begin with a particular token.

Returning false is a stability guarantee that such a matcher will never begin with that token. Be conservative (return true) if not sure. Inlined because it has a single call site.

Parse a non-terminal (e.g. MBE :pat or :ident). Inlined because there is only one call site.

Parses a pattern.

Corresponds to pat<no_top_alt> in RFC 2535 and does not admit or-patterns at the top level. Used when parsing the parameters of lambda expressions, functions, function pointers, and pat macro fragments.

Parses a pattern.

Corresponds to top_pat in RFC 2535 and allows or-pattern at the top level. Used for parsing patterns in all cases when pat<no_top_alt> is not used.

Note that after the FCP in https://github.com/rust-lang/rust/issues/81415, a leading vert is allowed in nested or-patterns, too. This allows us to simplify the grammar somewhat.

Returns the pattern and a bool indicating whether we recovered from a trailing vert (true = recovered).

Parse a pattern and (maybe) a Colon in positions where a pattern may be followed by a type annotation (e.g. for let bindings or fn params).

Generally, this corresponds to pat_no_top_alt followed by an optional Colon. It will eat the Colon token if one is present.

The return value represents the parsed pattern and true if a Colon was parsed (false otherwise).

Parse the pattern for a function or function pointer parameter, followed by a colon.

The return value represents the parsed pattern and true if a Colon was parsed (false otherwise).

Eat the or-pattern | separator. If instead a || token is encountered, recover and pretend we parsed |.

Recover if | or || is the current token and we have one of the tokens =>, if, =, :, ;, ,, ], ), or } ahead of us.

These tokens all indicate that we reached the end of the or-pattern list and can now reliably say that the | was an illegal trailing vert. Note that there are more tokens such as @ for which we know that the | is an illegal parse. However, the user’s intent is less clear in that case.

We have parsed || instead of |. Error and suggest | instead.

A | or possibly || token shouldn’t be here. Ban it.

Parses a pattern, with a setting whether modern range patterns (e.g., a..=b, a..b are allowed).

Recover from a typoed ... pattern that was encountered Ref: Issue #70388

Try to recover the more general form intersect ::= $pat_lhs @ $pat_rhs.

Allowed binding patterns generated by binding ::= ref? mut? $ident @ $pat_rhs should already have been parsed by now at this point, if the next token is @ then we can try to parse the more general form.

Consult parse_pat_ident for the binding grammar.

The notion of intersection patterns are found in e.g. F# where they are called AND-patterns.

Ban a range pattern if it has an ambiguous interpretation.

Parse &pat / &mut pat.

Parse a tuple or parenthesis pattern.

Parse a mutable binding with the mut token already eaten.

Recover on mut ref? ident @ pat and suggest that the order of mut and ref is incorrect.

Turn all by-value immutable bindings in a pattern into mutable bindings. Returns true if any change was made.

Error on mut $pat where $pat is not an ident.

Eat any extraneous muts and error + recover if we ate any.

Parse macro invocation

Parses the range pattern end form ".." | "..." | "..=" ;.

Parse a range pattern $begin $form $end? where $form = ".." | "..." | "..=" ;. $begin $form has already been parsed.

Parse a range-to pattern, ..X or ..=X where X remains to be parsed.

The form ...X is prohibited to reduce confusion with the potential expression syntax ...expr for splatting in expressions.

Is the token dist away from the current suitable as the start of a range patterns end?

Is this the start of a pattern beginning with a path?

Would parse_pat_ident be appropriate here?

Parses ident or ident @ pat. Used by the copy foo and ref foo patterns to give a good error message when parsing mistakes like ref foo(a, b).

Parse a struct (“record”) pattern (e.g. Foo { ... } or Foo::Bar { ... }).

Parse tuple struct or tuple variant pattern (e.g. Foo(...) or Foo::Bar(...)).

Are we sure this could not possibly be the start of a pattern?

Currently, this only accounts for tokens that can follow identifiers in patterns, but this can be extended as necessary.

Parses box pat

Parses the fields of a struct-like pattern.

Recover on ... as if it were .. to avoid further errors. See issue #46718.

Parses a qualified path. Assumes that the leading < has been parsed already.

qualified_path = <type [as trait_ref]>::path

Examples

<T>::default <T as U>::a <T as U>::F::a<S> (without disambiguator) <T as U>::F::a::<S> (with disambiguator)

Recover from an invalid single colon, when the user likely meant a qualified path. We avoid emitting this if not followed by an identifier, as our assumption that the user intended this to be a qualified path may not be correct.

<Bar as Baz<T>>:Qux
               ^ help: use double colon

Parses simple paths.

path = [::] segment+ segment = ident | ident[::]<args> | ident[::](args) [-> type]

Examples

a::b::C<D> (without disambiguator) a::b::C::<D> (with disambiguator) Fn(Args) (without disambiguator) Fn::(Args) (with disambiguator)

Parses generic args (within a path segment) with recovery for extra leading angle brackets. For the purposes of understanding the parsing logic of generic arguments, this function can be thought of being the same as just calling self.parse_angle_args() if the source had the correct amount of leading angle brackets.

bar::<<<<T as Foo>::Output>();
     ^^ help: remove extra angle brackets

Parses (possibly empty) list of generic arguments / associated item constraints, possibly including trailing comma.

Parses a single argument in the angle arguments <...> of a path segment.

Parse the term to the right of an associated item equality constraint. That is, parse <term> in Item = <term>. Right now, this only admits types in <term>.

We do not permit arbitrary expressions as const arguments. They must be one of:

  • An expression surrounded in {}.
  • A literal.
  • A numeric literal prefixed by -.
  • A single-segment path.

Parse a const argument, e.g. <3>. It is assumed the angle brackets will be parsed by the caller.

Parse a generic argument in a path segment. This does not include constraints, e.g., Item = u8, which is handled in parse_angle_arg.

Given a arg inside of generics, we try to destructure it as if it were the LHS in LHS = ..., i.e. an associated type binding. This returns a bool indicating if there are any for<'a, 'b> binder args, the identifier, and any GAT arguments.

Parses a statement. This stops just before trailing semicolons on everything but items. e.g., a StmtKind::Semi parses to a StmtKind::Expr, leaving the trailing ; unconsumed.

If force_collect is ForceCollect::Yes, forces collection of tokens regardless of whether or not we have attributes

Parses a statement macro mac!(args) provided a path representing mac. At this point, the ! token after the path has already been eaten.

Error on outer attributes in this context. Also error if the previous token was a doc comment.

Parses a local variable declaration.

report error for let 1x = 123

Parses the RHS of a local variable declaration (e.g., = 14;).

Parses a block. No inner attributes are allowed.

Parses a block. Inner attributes are allowed.

Parses a block. Inner attributes are allowed.

Parses the rest of a block expression or function body. Precondition: already parsed the ‘{’.

Parses a statement, including the trailing semicolon.

Parses a type.

Parse a type suitable for a function or function pointer parameter. The difference from parse_ty is that this version allows ... (CVarArgs) at the top level of the type.

Parses a type in restricted contexts where + is not permitted.

Example 1: &'a TYPE + is prohibited to maintain operator priority (P(+) < P(&)). Example 2: value1 as TYPE + value2 + is prohibited to avoid interactions with expression grammar.

Parses a type following an as cast. Similar to parse_ty_no_plus, but signaling origin for better diagnostics involving ?.

Parse a type without recovering : as -> to avoid breaking code such as where fn() : for<'a>

Parses an optional return type [ -> TY ] in a function declaration.

Parses either:

  • (TYPE), a parenthesized type.
  • (TYPE,), a tuple with a single field of type TYPE.

Parse the remainder of a bare trait object type given an already parsed list.

Parses a raw pointer type: *[const | mut] $type.

Parses an array ([TYPE; EXPR]) or slice ([TYPE]) type. The opening [ bracket is already eaten.

Parses a function pointer type (TyKind::BareFn).

   [unsafe] [extern "ABI"] fn (S) -> T
//  ^~~~~^          ^~~~^     ^~^    ^
//    |               |        |     |
//    |               |        |   Return type
// Function Style    ABI  Parameter types

We actually parse FnHeader FnDecl, but we error on const and async qualifiers.

Recover from function pointer types with a generic parameter list (e.g. fn<'a>(&'a str)).

Emit an error for the given bad function pointer qualifier.

Parses an impl B0 + ... + Bn type.

Is a dyn B0 + ... + Bn type allowed here?

Parses a dyn B0 + ... + Bn type.

Note that this does not parse bare trait objects.

Parses a type starting with a path.

This can be:

  1. a type macro, mac!(...),
  2. a bare trait object, B0 + ... + Bn,
  3. or a path, path::to::MyType.

Parses bounds of a type parameter BOUND + BOUND + ..., possibly with trailing +.

See parse_generic_bound for the BOUND grammar.

Can the current token begin a bound?

Parses a bound according to the grammar:

BOUND = TY_BOUND | LT_BOUND

Parses a lifetime (“outlives”) bound, e.g. 'a, according to:

LT_BOUND = LIFETIME

Emits an error if any trait bound modifiers were present.

Recover on ('lifetime) with ( already eaten.

Parses the modifiers that may precede a trait in a bound, e.g. ?Trait or ~const Trait.

If no modifiers are present, this does not consume any tokens.

TY_BOUND_MODIFIERS = ["~const"] ["?"]

Parses a type bound according to:

TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
TY_BOUND_NOPAREN = [TY_BOUND_MODIFIERS] [for<LT_PARAM_DEFS>] SIMPLE_PATH

For example, this grammar accepts ~const ?for<'a: 'b> m::Trait<'a>.

Optionally parses for<$generic_params>.

Parses a single lifetime 'a or panics.

Whether the parser is allowed to recover from broken code.

If this returns false, recovering broken code into valid code (especially if this recovery does lookahead) is not allowed. All recovery done by the parser must be gated behind this check.

Technically, this only needs to restrict eager recovery by doing lookahead at more tokens. But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.

Expects and consumes the token t. Signals an error if the next token is not t.

Expect next token to be edible or inedible token. If edible, then consume it; if inedible, then return without consuming anything. Signal a fatal error if next token is unexpected.

Checks if the next token is tok, and returns true if so.

This method will automatically add tok to expected_tokens if tok is not encountered.

Consumes a token ‘tok’ if it exists. Returns whether the given token was present.

the main purpose of this function is to reduce the cluttering of the suggestions list which using the normal eat method could introduce in some cases.

Consumes a token ‘tok’ if it exists. Returns whether the given token was present.

If the next token is the given keyword, returns true without eating it. An expectation is also added for diagnostics purposes.

If the next token is the given keyword, eats it and returns true. Otherwise, returns false. An expectation is also added for diagnostics purposes.

Eats a keyword, optionally ignoring the case. If the case differs (and is ignored) an error is issued. This is useful for recovery.

If the given word is not a keyword, signals an error. If the next token is not the given word, signals an error. Otherwise, eats it.

Is the given keyword kw followed by a non-reserved identifier?

Checks to see if the next token is either + or +=. Otherwise returns false.

Eats the expected token if it’s present possibly breaking compound tokens like multi-character operators in process. Returns true if the token was eaten.

Eats + possibly breaking tokens like += in process.

Eats & possibly breaking tokens like && in process. Signals an error if & is not eaten.

Eats | possibly breaking tokens like || in process. Signals an error if | was not eaten.

Eats < possibly breaking tokens like << in process.

Eats < possibly breaking tokens like << in process. Signals an error if < was not eaten.

Eats > possibly breaking tokens like >> in process. Signals an error if > was not eaten.

Parses a sequence, not including the closing delimiter. The function f must consume tokens until reaching the next separator or closing bracket.

Parses a sequence, including the closing delimiter. The function f must consume tokens until reaching the next separator or closing bracket.

Parses a sequence, including the closing delimiter. The function f must consume tokens until reaching the next separator or closing bracket.

Advance the parser by one token using provided token as the next one.

This always-inlined version should only be used on hot code paths.

Advance the parser by one token.

Look-ahead dist tokens of self.token and get access to that token there. When dist == 0 then the current token is looked at.

Returns whether any of the given keywords are dist tokens ahead of the current one.

Parses asyncness: async or nothing.

Parses unsafety: unsafe or nothing.

Parses constness: const or nothing.

Parses inline const expressions.

Parses mutability (mut or nothing).

Possibly parses mutability (const or mut).

Parses a single token tree from the input.

Parses a stream of tokens into a list of TokenTrees, up to EOF.

Evaluates the closure with restrictions in place.

Afters the closure is evaluated, restrictions are reset.

Parses pub and pub(in path) plus shortcuts pub(crate) for pub(in crate), pub(self) for pub(in self) and pub(super) for pub(in super). If the following element can’t be a tuple (i.e., it’s a function definition), then it’s not a tuple struct field), and the contents within the parentheses aren’t valid, so emit a proper diagnostic.

Recovery for e.g. pub(something) fn ... or struct X { pub(something) y: Z }

Parses extern string_literal?.

Parses a string literal as an ABI spec.

::{ or ::*

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.

Size: 336 bytes