Struct rustc_parse::parser::Parser [−][src]
pub struct Parser<'a> {Show 17 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>,
}
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.
Implementations
Parses attributes that appear before an item.
pub fn parse_attribute(
&mut self,
inner_parse_policy: InnerAttrPolicy<'_>
) -> PResult<'a, Attribute>
pub fn parse_attribute(
&mut self,
inner_parse_policy: InnerAttrPolicy<'_>
) -> PResult<'a, Attribute>
Matches attribute = # ! [ meta_item ]
.
inner_parse_policy
prescribes how to handle inner attributes.
fn annotate_following_item_if_applicable(
&self,
err: &mut DiagnosticBuilder<'_>,
span: Span,
attr_type: OuterAttributeType
) -> Option<Span>
pub(super) fn error_on_forbidden_inner_attr(
&self,
attr_sp: Span,
policy: InnerAttrPolicy<'_>
)
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) ;
.
pub fn collect_tokens_trailing_token<R: AstLike>(
&mut self,
attrs: AttrWrapper,
force_collect: ForceCollect,
f: impl FnOnce(&mut Self, Vec<Attribute>) -> PResult<'a, (R, TrailingToken)>
) -> PResult<'a, R>
pub fn collect_tokens_trailing_token<R: AstLike>(
&mut self,
attrs: AttrWrapper,
force_collect: ForceCollect,
f: impl FnOnce(&mut Self, Vec<Attribute>) -> PResult<'a, (R, TrailingToken)>
) -> PResult<'a, R>
Records all tokens consumed by the provided callback,
including the current token. These tokens are collected
into a LazyTokenStream
, 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 delimeter),
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.
pub(super) fn expected_one_of_not_found(
&mut self,
edible: &[TokenKind],
inedible: &[TokenKind]
) -> PResult<'a, bool>
pub fn maybe_suggest_struct_literal(
&mut self,
lo: Span,
s: BlockCheckMode
) -> Option<PResult<'a, P<Block>>>
pub fn maybe_annotate_with_ascription(
&mut self,
err: &mut DiagnosticBuilder<'_>,
maybe_expected_semicolon: bool
)
Eats and discards tokens until one of kets
is encountered. Respects token trees,
passes through any errors encountered. Used for error recovery.
pub(super) fn check_trailing_angle_brackets(
&mut self,
segment: &PathSegment,
end: &[&TokenKind]
) -> bool
pub(super) fn check_trailing_angle_brackets(
&mut self,
segment: &PathSegment,
end: &[&TokenKind]
) -> bool
This function checks if there are trailing angle brackets and produces a diagnostic to suggest removing them.
let _ = vec![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.
pub(super) fn check_mistyped_turbofish_with_multiple_type_params(
&mut self,
e: DiagnosticBuilder<'a>,
expr: &mut P<Expr>
) -> PResult<'a, ()>
pub(super) fn check_mistyped_turbofish_with_multiple_type_params(
&mut self,
e: DiagnosticBuilder<'a>,
expr: &mut P<Expr>
) -> PResult<'a, ()>
When writing a turbofish with multiple type parameters missing the leading ::
, we will
encounter a parse error when encountering the first ,
.
fn attempt_chained_comparison_suggestion(
&mut self,
err: &mut DiagnosticBuilder<'_>,
inner_op: &Expr,
outer_op: &Spanned<AssocOp>
) -> bool
fn attempt_chained_comparison_suggestion(
&mut self,
err: &mut DiagnosticBuilder<'_>,
inner_op: &Expr,
outer_op: &Spanned<AssocOp>
) -> bool
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
pub(super) fn maybe_report_ambiguous_plus(
&mut self,
allow_plus: AllowPlus,
impl_dyn_multi: bool,
ty: &Ty
)
pub(super) fn maybe_recover_from_bad_type_plus(
&mut self,
allow_plus: AllowPlus,
ty: &Ty
) -> PResult<'a, ()>
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
&mut self,
base: P<T>,
allow_recovery: bool
) -> PResult<'a, P<T>>
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
&mut self,
base: P<T>,
allow_recovery: bool
) -> PResult<'a, P<T>>
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.
pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
&mut self,
ty_span: Span,
ty: P<Ty>
) -> PResult<'a, P<T>>
pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
&mut self,
ty_span: Span,
ty: P<Ty>
) -> PResult<'a, P<T>>
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
.
pub(super) fn recover_seq_parse_error(
&mut self,
delim: DelimToken,
lo: Span,
result: PResult<'a, P<Expr>>
) -> P<Expr>
pub(super) fn recover_closing_delimiter(
&mut self,
tokens: &[TokenKind],
err: DiagnosticBuilder<'a>
) -> PResult<'a, bool>
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).
pub(super) fn recover_stmt_(
&mut self,
break_on_semi: SemiColonMode,
break_on_block: BlockMode
)
pub(super) fn recover_stmt_(
&mut self,
break_on_semi: SemiColonMode,
break_on_block: BlockMode
)
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.
pub(super) fn parameter_without_type(
&mut self,
err: &mut DiagnosticBuilder<'_>,
pat: P<Pat>,
require_name: bool,
first_param: bool
) -> Option<Ident>
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.
pub fn handle_ambiguous_unbraced_const_arg(
&mut self,
args: &mut Vec<AngleBracketedArg>
) -> PResult<'a, bool>
pub fn handle_ambiguous_unbraced_const_arg(
&mut self,
args: &mut Vec<AngleBracketedArg>
) -> PResult<'a, bool>
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.
pub fn recover_const_arg(
&mut self,
start: Span,
err: DiagnosticBuilder<'a>
) -> PResult<'a, GenericArg>
pub fn recover_const_arg(
&mut self,
start: Span,
err: DiagnosticBuilder<'a>
) -> PResult<'a, GenericArg>
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 that the resulting expression would be well formed.
pub(super) fn incorrect_move_async_order_found(
&self,
move_async_span: Span
) -> DiagnosticBuilder<'a>
pub(super) fn incorrect_move_async_order_found(
&self,
move_async_span: Span
) -> DiagnosticBuilder<'a>
Get the diagnostics for the cases where move async
is found.
move_async_span
starts at the ‘m’ of the move keyword and ends with the ‘c’ of the async keyword
Parses an expression.
Parses an expression, forcing tokens to be collected
Parses a sequence of expressions delimited by parentheses.
pub(super) fn parse_expr_res(
&mut self,
r: Restrictions,
already_parsed_attrs: Option<AttrWrapper>
) -> PResult<'a, P<Expr>>
pub(super) fn parse_expr_res(
&mut self,
r: Restrictions,
already_parsed_attrs: Option<AttrWrapper>
) -> PResult<'a, P<Expr>>
Parses an expression, subject to the given restrictions.
fn parse_assoc_expr(
&mut self,
already_parsed_attrs: Option<AttrWrapper>
) -> PResult<'a, P<Expr>>
fn parse_assoc_expr(
&mut self,
already_parsed_attrs: Option<AttrWrapper>
) -> PResult<'a, P<Expr>>
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.
Error on and
and or
suggesting &&
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.
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.
fn parse_assoc_op_cast(
&mut self,
lhs: P<Expr>,
lhs_span: Span,
expr_kind: fn(_: P<Expr>, _: P<Ty>) -> ExprKind
) -> PResult<'a, P<Expr>>
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
.
pub(super) fn parse_dot_or_call_expr_with(
&mut self,
e0: P<Expr>,
lo: Span,
attrs: Vec<Attribute>
) -> PResult<'a, P<Expr>>
fn parse_tuple_field_access_expr_float(
&mut self,
lo: Span,
base: P<Expr>,
float: Symbol,
suffix: Option<Symbol>
) -> P<Expr>
fn parse_tuple_field_access_expr(
&mut self,
lo: Span,
base: P<Expr>,
field: Symbol,
suffix: Option<Symbol>,
next_token: Option<(Token, Spacing)>
) -> P<Expr>
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()
.
fn parse_array_or_repeat_expr(
&mut self,
attrs: AttrVec,
close_delim: DelimToken
) -> PResult<'a, P<Expr>>
Parse 'label: $expr
. The label is already parsed.
Recover on the syntax do catch { ... }
suggesting try { ... }
instead.
Parse an expression if the token can begin one.
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.
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 | 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.
pub(super) fn parse_block_expr(
&mut self,
opt_label: Option<Label>,
lo: Span,
blk_mode: BlockCheckMode,
attrs: AttrVec
) -> PResult<'a, P<Expr>>
pub(super) fn parse_block_expr(
&mut self,
opt_label: Option<Label>,
lo: Span,
blk_mode: BlockCheckMode,
attrs: AttrVec
) -> PResult<'a, P<Expr>>
Parses a block or unsafe block.
Recover on an explicitly quantified closure expression, e.g., for<'a> |x: &'a u8| *x + 1
.
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.
The let
token has already been eaten.
Parses an else { ... }
expression (else
token already eaten).
fn error_on_if_block_attrs(
&self,
ctx_span: Span,
is_ctx_else: bool,
branch_span: Span,
attrs: &[Attribute]
)
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.
fn maybe_parse_struct_expr(
&mut self,
qself: Option<&QSelf>,
path: &Path,
attrs: &AttrVec
) -> Option<PResult<'a, P<Expr>>>
pub(super) fn parse_struct_fields(
&mut self,
pth: Path,
recover: bool,
close_delim: DelimToken
) -> PResult<'a, (Vec<ExprField>, StructRest, bool)>
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.
fn mk_range(
&mut self,
start: Option<P<Expr>>,
end: Option<P<Expr>>,
limits: RangeLimits
) -> ExprKind
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.
fn parse_item_(
&mut self,
req_name: fn(_: Edition) -> bool,
force_collect: ForceCollect
) -> PResult<'a, Option<Item>>
pub(super) fn parse_item_common(
&mut self,
attrs: AttrWrapper,
mac_allowed: bool,
attrs_allowed: bool,
req_name: fn(_: Edition) -> bool,
force_collect: ForceCollect
) -> PResult<'a, Option<Item>>
fn parse_item_common_(
&mut self,
attrs: Vec<Attribute>,
mac_allowed: bool,
attrs_allowed: bool,
req_name: fn(_: Edition) -> bool
) -> PResult<'a, Option<Item>>
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.
fn parse_item_impl(
&mut self,
attrs: &mut Vec<Attribute>,
defaultness: Defaultness
) -> PResult<'a, (Ident, ItemKind)>
fn parse_item_impl(
&mut self,
attrs: &mut Vec<Attribute>,
defaultness: Defaultness
) -> PResult<'a, (Ident, ItemKind)>
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 "}"
fn parse_item_list<T>(
&mut self,
attrs: &mut Vec<Attribute>,
parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>
) -> PResult<'a, Vec<T>>
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;
.
pub fn parse_impl_item(
&mut self,
force_collect: ForceCollect
) -> PResult<'a, Option<Option<P<AssocItem>>>>
pub fn parse_trait_item(
&mut self,
force_collect: ForceCollect
) -> PResult<'a, Option<Option<P<AssocItem>>>>
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
for foreign ABIs modules.
extern
is expected to have been consumed before calling this method.
Examples
extern "C" {}
extern {}
pub fn parse_foreign_item(
&mut self,
force_collect: ForceCollect
) -> PResult<'a, Option<Option<P<ForeignItem>>>>
pub fn parse_foreign_item(
&mut self,
force_collect: ForceCollect
) -> PResult<'a, Option<Option<P<ForeignItem>>>>
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 :
but the :
was missing.
This means that the type is missing.
Parses an element of a struct declaration.
fn parse_single_struct_field(
&mut self,
adt_ty: &str,
lo: Span,
vis: Visibility,
attrs: Vec<Attribute>
) -> PResult<'a, FieldDef>
fn parse_single_struct_field(
&mut self,
adt_ty: &str,
lo: Span,
vis: Visibility,
attrs: Vec<Attribute>
) -> PResult<'a, FieldDef>
Parses a structure field declaration.
fn parse_name_and_ty(
&mut self,
adt_ty: &str,
lo: Span,
vis: Visibility,
attrs: Vec<Attribute>
) -> PResult<'a, FieldDef>
fn parse_name_and_ty(
&mut self,
adt_ty: &str,
lo: Span,
vis: Visibility,
attrs: Vec<Attribute>
) -> PResult<'a, FieldDef>
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 unambiguously the 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" ;
We are parsing async fn
. If we are on Rust 2015, emit an error.
pub(super) fn parse_fn_decl(
&mut self,
req_name: fn(_: Edition) -> bool,
ret_allow_plus: AllowPlus,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, P<FnDecl>>
pub(super) fn parse_fn_decl(
&mut self,
req_name: fn(_: Edition) -> bool,
ret_allow_plus: AllowPlus,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, P<FnDecl>>
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 whenfirst_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.
Parse a non-terminal (e.g. MBE :pat
or :ident
).
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.
pub fn parse_pat_allow_top_alt(
&mut self,
expected: Option<&'static str>,
rc: RecoverComma,
ra: RecoverColon
) -> PResult<'a, P<Pat>>
pub fn parse_pat_allow_top_alt(
&mut self,
expected: Option<&'static str>,
rc: RecoverComma,
ra: RecoverColon
) -> PResult<'a, P<Pat>>
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.
fn parse_pat_allow_top_alt_inner(
&mut self,
expected: Option<&'static str>,
rc: RecoverComma,
ra: RecoverColon
) -> PResult<'a, (P<Pat>, bool)>
fn parse_pat_allow_top_alt_inner(
&mut self,
expected: Option<&'static str>,
rc: RecoverComma,
ra: RecoverColon
) -> PResult<'a, (P<Pat>, bool)>
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.
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.
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 mut
s and error + recover if we ate any.
Parse macro invocation
fn fatal_unexpected_non_pat(
&mut self,
err: DiagnosticBuilder<'a>,
expected: Option<&'static str>
) -> PResult<'a, P<Pat>>
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(...)
).
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)
pub(super) fn parse_path_segments(
&mut self,
segments: &mut Vec<PathSegment>,
style: PathStyle
) -> PResult<'a, ()>
fn parse_angle_args_with_leading_angle_bracket_recovery(
&mut self,
style: PathStyle,
lo: Span
) -> PResult<'a, Vec<AngleBracketedArg>>
fn parse_angle_args_with_leading_angle_bracket_recovery(
&mut self,
style: PathStyle,
lo: Span
) -> PResult<'a, Vec<AngleBracketedArg>>
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
.
fn get_ident_from_generic_arg(
&self,
gen_arg: GenericArg
) -> Result<(Ident, Option<GenericArgs>), GenericArg>
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.
pub(crate) fn parse_stmt_without_recovery(
&mut self,
capture_semi: bool,
force_collect: ForceCollect
) -> PResult<'a, Option<Stmt>>
pub(crate) fn parse_stmt_without_recovery(
&mut self,
capture_semi: bool,
force_collect: ForceCollect
) -> PResult<'a, Option<Stmt>>
If force_capture
is true, 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.
fn recover_stmt_local(
&mut self,
lo: Span,
attrs: AttrWrapper,
msg: &str,
sugg: &str
) -> PResult<'a, Stmt>
fn parse_local_mk(
&mut self,
lo: Span,
attrs: AttrWrapper,
capture_semi: bool,
force_collect: ForceCollect
) -> PResult<'a, Stmt>
Parses a local variable declaration.
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.
pub(crate) fn parse_block_tail(
&mut self,
lo: Span,
s: BlockCheckMode,
recover: AttemptLocalParseRecovery
) -> PResult<'a, P<Block>>
pub(crate) fn parse_block_tail(
&mut self,
lo: Span,
s: BlockCheckMode,
recover: AttemptLocalParseRecovery
) -> PResult<'a, P<Block>>
Parses the rest of a block expression or function body. Precondition: already parsed the ‘{’.
pub fn parse_full_stmt(
&mut self,
recover: AttemptLocalParseRecovery
) -> PResult<'a, Option<Stmt>>
pub fn parse_full_stmt(
&mut self,
recover: AttemptLocalParseRecovery
) -> PResult<'a, Option<Stmt>>
Parses a statement, including the trailing semicolon.
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.
Parse a type without recovering :
as ->
to avoid breaking code such as where fn() : for<'a>
pub(super) fn parse_ret_ty(
&mut self,
allow_plus: AllowPlus,
recover_qpath: RecoverQPath,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, FnRetTy>
pub(super) fn parse_ret_ty(
&mut self,
allow_plus: AllowPlus,
recover_qpath: RecoverQPath,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, FnRetTy>
Parses an optional return type [ -> TY ]
in a function declaration.
fn parse_ty_common(
&mut self,
allow_plus: AllowPlus,
allow_c_variadic: AllowCVariadic,
recover_qpath: RecoverQPath,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, P<Ty>>
Parses either:
(TYPE)
, a parenthesized type.(TYPE,)
, a tuple with a single field of type TYPE.
fn parse_remaining_bounds_path(
&mut self,
generic_params: Vec<GenericParam>,
path: Path,
lo: Span,
parse_plus: bool
) -> PResult<'a, TyKind>
fn parse_remaining_bounds(
&mut self,
bounds: GenericBounds,
plus: bool
) -> PResult<'a, TyKind>
fn parse_remaining_bounds(
&mut self,
bounds: GenericBounds,
plus: bool
) -> PResult<'a, TyKind>
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.
fn parse_ty_bare_fn(
&mut self,
lo: Span,
params: Vec<GenericParam>,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, TyKind>
fn parse_ty_bare_fn(
&mut self,
lo: Span,
params: Vec<GenericParam>,
recover_return_sign: RecoverReturnSign
) -> PResult<'a, TyKind>
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.
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:
- a type macro,
mac!(...)
, - a bare trait object,
B0 + ... + Bn
, - or a path,
path::to::MyType
.
pub(super) fn parse_generic_bounds(
&mut self,
colon_span: Option<Span>
) -> PResult<'a, GenericBounds>
fn parse_generic_bounds_common(
&mut self,
allow_plus: AllowPlus,
colon_span: Option<Span>
) -> PResult<'a, GenericBounds>
fn parse_generic_bounds_common(
&mut self,
allow_plus: AllowPlus,
colon_span: Option<Span>
) -> PResult<'a, GenericBounds>
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?
fn error_negative_bounds(
&self,
colon_span: Option<Span>,
bounds: &[GenericBound],
negative_bounds: Vec<Span>
)
Parses a bound according to the grammar:
BOUND = TY_BOUND | LT_BOUND
fn parse_generic_lt_bound(
&mut self,
lo: Span,
inner_lo: Span,
has_parens: bool
) -> PResult<'a, GenericBound>
fn parse_generic_lt_bound(
&mut self,
lo: Span,
inner_lo: Span,
has_parens: bool
) -> PResult<'a, GenericBound>
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"] ["?"]
fn parse_generic_ty_bound(
&mut self,
lo: Span,
has_parens: bool,
modifiers: BoundModifiers
) -> PResult<'a, GenericBound>
fn parse_generic_ty_bound(
&mut self,
lo: Span,
has_parens: bool,
modifiers: BoundModifiers
) -> PResult<'a, GenericBound>
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.
pub fn new(
sess: &'a ParseSess,
tokens: TokenStream,
desugar_doc_comments: bool,
subparser_name: Option<&'static str>
) -> Self
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.
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.
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.
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.
Signals an error if <
was not eaten.
Eats >
possibly breaking tokens like >>
in process.
Signals an error if >
was not eaten.
fn parse_seq_to_before_tokens<T>(
&mut self,
kets: &[&TokenKind],
sep: SeqSep,
expect: TokenExpectType,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>
) -> PResult<'a, (Vec<T>, bool, bool)>
fn recover_missing_braces_around_closure_body(
&mut self,
closure_spans: ClosureSpans,
expect_err: DiagnosticBuilder<'_>
) -> PResult<'a, ()>
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.
fn parse_delim_comma_seq<T>(
&mut self,
delim: DelimToken,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>
) -> PResult<'a, (Vec<T>, bool)>
fn parse_paren_comma_seq<T>(
&mut self,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>
) -> PResult<'a, (Vec<T>, bool)>
Advance the parser by one token using provided token as the next one.
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
).
fn parse_or_use_outer_attributes(
&mut self,
already_parsed_attrs: Option<AttrWrapper>
) -> PResult<'a, AttrWrapper>
Parses a single token tree from the input.
Parses a stream of tokens into a list of TokenTree
s, up to EOF.
Evaluates the closure with restrictions in place.
Afters the closure is evaluated, restrictions are reset.
Parses pub
, pub(crate)
and pub(in path)
plus shortcuts crate
for pub(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?
.
pub fn collect_tokens_no_attrs<R: AstLike>(
&mut self,
f: impl FnOnce(&mut Self) -> PResult<'a, R>
) -> PResult<'a, R>
::{
or ::*
Trait Implementations
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Parser<'a>
impl<'a> !UnwindSafe for Parser<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
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: 328 bytes