Struct syntax::parse::parser::Parser [−] [src]

pub struct Parser<'a> {
    pub sess: &'a ParseSess,
    pub token: Token,
    pub span: Span,
    pub last_span: Span,
    pub cfg: CrateConfig,
    pub last_token: Option<Box<Token>>,
    pub buffer: [TokenAndSpan; 4],
    pub buffer_start: isize,
    pub buffer_end: isize,
    pub tokens_consumed: usize,
    pub restrictions: Restrictions,
    pub quote_depth: usize,
    pub reader: Box<Reader + 'a>,
    pub interner: Rc<IdentInterner>,
    pub obsolete_set: HashSet<ObsoleteSyntax>,
    pub mod_path_stack: Vec<InternedString>,
    pub open_braces: Vec<Span>,
    pub owns_directory: bool,
    pub root_module_name: Option<String>,
    pub expected_tokens: Vec<TokenType>,
}
Unstable

Fields

sess
Unstable
token
Unstable

the current token:

span
Unstable

the span of the current token:

last_span
Unstable

the span of the prior token:

cfg
Unstable
last_token
Unstable

the previous token or None (only stashed sometimes).

buffer
Unstable
buffer_start
Unstable
buffer_end
Unstable
tokens_consumed
Unstable
restrictions
Unstable
quote_depth
Unstable
reader
Unstable
interner
Unstable
obsolete_set
Unstable

The set of seen errors about obsolete syntax. Used to suppress extra detail when the same error is seen twice

mod_path_stack
Unstable

Used to determine the path to externally loaded source files

open_braces
Unstable

Stack of spans of open delimiters. Used for error message.

owns_directory
Unstable

Flag if this parser "owns" the directory that it is currently parsing in. This will affect how nested files are looked up.

root_module_name
Unstable

Name of the root module this parser originated from. If None, then the name is not known. This does not change while the parser is descending into modules, and sub-parsers have new values for this name.

expected_tokens
Unstable

Methods

impl<'a> Parser<'a>

fn new(sess: &'a ParseSess, cfg: CrateConfig, rdr: Box<Reader + 'a>) -> Parser<'a>

Unstable

fn parse_expr(&mut self) -> P<Expr>

Unstable

fn parse_item(&mut self) -> Option<P<Item>>

Unstable

fn parse_pat(&mut self) -> P<Pat>

Unstable

fn parse_arm(&mut self) -> Arm

Unstable

fn parse_ty(&mut self) -> P<Ty>

Unstable

fn parse_stmt(&mut self) -> Option<P<Stmt>>

Unstable

fn token_to_string(token: &Token) -> String

Unstable

Convert a token to a string using self's reader

fn this_token_to_string(&self) -> String

Unstable

Convert the current token to a string using self's reader

fn unexpected_last(&self, t: &Token) -> FatalError

Unstable

fn unexpected(&mut self) -> FatalError

Unstable

fn expect(&mut self, t: &Token) -> PResult<()>

Unstable

Expect and consume the token t. Signal an error if the next token is not t.

fn expect_one_of(&mut self, edible: &[Token], inedible: &[Token]) -> PResult<()>

Unstable

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.

fn check_for_erroneous_unit_struct_expecting(&mut self, expected: &[Token]) -> PResult<bool>

Unstable

Check for erroneous ident { }; if matches, signal error and recover (without consuming any expected input token). Returns true if and only if input was consumed for recovery.

fn commit_expr(&mut self, e: &Expr, edible: &[Token], inedible: &[Token]) -> PResult<()>

Unstable

Commit to parsing a complete expression e expected to be followed by some token from the set edible + inedible. Recover from anticipated input errors, discarding erroneous characters.

fn commit_expr_expecting(&mut self, e: &Expr, edible: Token) -> PResult<()>

Unstable

fn commit_stmt(&mut self, edible: &[Token], inedible: &[Token]) -> PResult<()>

Unstable

Commit to parsing a complete statement s, which expects to be followed by some token from the set edible + inedible. Check for recoverable input errors, discarding erroneous characters.

fn commit_stmt_expecting(&mut self, edible: Token) -> PResult<()>

Unstable

fn parse_ident(&mut self) -> PResult<Ident>

Unstable

fn parse_ident_or_self_type(&mut self) -> PResult<Ident>

Unstable

fn parse_path_list_item(&mut self) -> PResult<PathListItem>

Unstable

fn check(&mut self, tok: &Token) -> bool

Unstable

Check if the next token is tok, and return true if so.

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

fn eat(&mut self, tok: &Token) -> PResult<bool>

Unstable

Consume token 'tok' if it exists. Returns true if the given token was present, false otherwise.

fn check_keyword(&mut self, kw: Keyword) -> bool

Unstable

fn eat_keyword(&mut self, kw: Keyword) -> PResult<bool>

Unstable

If the next token is the given keyword, eat it and return true. Otherwise, return false.

fn eat_keyword_noexpect(&mut self, kw: Keyword) -> PResult<bool>

Unstable

fn expect_keyword(&mut self, kw: Keyword) -> PResult<()>

Unstable

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

fn check_strict_keywords(&mut self)

Unstable

Signal an error if the given string is a strict keyword

fn check_reserved_keywords(&mut self) -> PResult<()>

Unstable

Signal an error if the current token is a reserved keyword

fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<Name>)

Unstable

fn expect_gt(&mut self) -> PResult<()>

Unstable

Expect and consume a GT. if a >> is seen, replace it with a single > and continue. If a GT is not seen, signal an error.

fn parse_seq_to_before_gt_or_return<T, F>(&mut self, sep: Option<Token>, f: F) -> PResult<(OwnedSlice<T>, bool)> where F: FnMut(&mut Parser) -> PResult<Option<T>>

Unstable

fn parse_seq_to_before_gt<T, F>(&mut self, sep: Option<Token>, f: F) -> PResult<OwnedSlice<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

Parse a sequence bracketed by '<' and '>', stopping before the '>'.

fn parse_seq_to_gt<T, F>(&mut self, sep: Option<Token>, f: F) -> PResult<OwnedSlice<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

fn parse_seq_to_gt_or_return<T, F>(&mut self, sep: Option<Token>, f: F) -> PResult<(OwnedSlice<T>, bool)> where F: FnMut(&mut Parser) -> PResult<Option<T>>

Unstable

fn parse_seq_to_end<T, F>(&mut self, ket: &Token, sep: SeqSep, f: F) -> PResult<Vec<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

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

fn parse_seq_to_before_end<T, F>(&mut self, ket: &Token, sep: SeqSep, f: F) -> PResult<Vec<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

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

fn parse_unspanned_seq<T, F>(&mut self, bra: &Token, ket: &Token, sep: SeqSep, f: F) -> PResult<Vec<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

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

fn parse_enum_variant_seq<T, F>(&mut self, bra: &Token, ket: &Token, sep: SeqSep, f: F) -> PResult<Vec<T>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

Parse a sequence parameter of enum variant. For consistency purposes, these should not be empty.

fn parse_seq<T, F>(&mut self, bra: &Token, ket: &Token, sep: SeqSep, f: F) -> PResult<Spanned<Vec<T>>> where F: FnMut(&mut Parser) -> PResult<T>

Unstable

fn bump(&mut self) -> PResult<()>

Unstable

Advance the parser by one token

fn bump_and_get(&mut self) -> PResult<Token>

Unstable

Advance the parser by one token and return the bumped token.

fn replace_token(&mut self, next: Token, lo: BytePos, hi: BytePos)

Unstable

EFFECT: replace the current token and span with the given one

fn buffer_length(&mut self) -> isize

Unstable

fn look_ahead<R, F>(&mut self, distance: usize, f: F) -> R where F: FnOnce(&Token) -> R

Unstable

fn fatal(&self, m: &str) -> FatalError

Unstable

fn span_fatal(&self, sp: Span, m: &str) -> FatalError

Unstable

fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> FatalError

Unstable

fn span_note(&self, sp: Span, m: &str)

Unstable

fn span_help(&self, sp: Span, m: &str)

Unstable

fn span_suggestion(&self, sp: Span, m: &str, n: String)

Unstable

fn fileline_help(&self, sp: Span, m: &str)

Unstable

fn bug(&self, m: &str) -> !

Unstable

fn warn(&self, m: &str)

Unstable

fn span_warn(&self, sp: Span, m: &str)

Unstable

fn span_err(&self, sp: Span, m: &str)

Unstable

fn span_bug(&self, sp: Span, m: &str) -> !

Unstable

fn abort_if_errors(&self)

Unstable

fn id_to_interned_str(&mut self, id: Ident) -> InternedString

Unstable

fn token_is_bare_fn_keyword(&mut self) -> bool

Unstable

Is the current token one of the keywords that signals a bare function type?

fn get_lifetime(&mut self) -> Ident

Unstable

fn parse_for_in_type(&mut self) -> PResult<Ty_>

Unstable

fn parse_ty_path(&mut self) -> PResult<Ty_>

Unstable

fn parse_ty_bare_fn(&mut self, lifetime_defs: Vec<LifetimeDef>) -> PResult<Ty_>

Unstable

parse a TyBareFn type:

fn parse_obsolete_closure_kind(&mut self) -> PResult<()>

Unstable

Parses an obsolete closure kind (&:, &mut:, or :).

fn parse_unsafety(&mut self) -> PResult<Unsafety>

Unstable

fn parse_trait_items(&mut self) -> PResult<Vec<P<TraitItem>>>

Unstable

Parse the items in a trait declaration

fn parse_mt(&mut self) -> PResult<MutTy>

Unstable

Parse a possibly mutable type

fn parse_ret_ty(&mut self) -> PResult<FunctionRetTy>

Unstable

Parse optional return type [ -> TY ] in function decl

fn parse_ty_sum(&mut self) -> PResult<P<Ty>>

Unstable

Parse a type in a context where T1+T2 is allowed.

fn parse_ty_nopanic(&mut self) -> PResult<P<Ty>>

Unstable

Parse a type.

fn parse_borrowed_pointee(&mut self) -> PResult<Ty_>

Unstable

fn parse_ptr(&mut self) -> PResult<MutTy>

Unstable

fn is_named_argument(&mut self) -> bool

Unstable

fn parse_arg_general(&mut self, require_name: bool) -> PResult<Arg>

Unstable

This version of parse arg doesn't necessarily require identifier names.

fn parse_arg(&mut self) -> PResult<Arg>

Unstable

Parse a single function argument

fn parse_fn_block_arg(&mut self) -> PResult<Arg>

Unstable

Parse an argument in a lambda header e.g. |arg, arg|

fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<Option<P<Expr>>>

Unstable

fn lit_from_token(&self, tok: &Token) -> PResult<Lit_>

Unstable

Matches token_lit = LIT_INTEGER | ...

fn parse_lit(&mut self) -> PResult<Lit>

Unstable

Matches lit = true | false | token_lit

fn parse_literal_maybe_minus(&mut self) -> PResult<P<Expr>>

Unstable

matches '-' lit | lit

fn parse_qualified_path(&mut self, mode: PathParsingMode) -> PResult<(QSelf, Path)>

Unstable

fn parse_path(&mut self, mode: PathParsingMode) -> PResult<Path>

Unstable

Parses a path and optional type parameter bounds, depending on the mode. The mode parameter determines whether lifetimes, types, and/or bounds are permitted and whether :: must precede type parameter groups.

fn parse_path_segments_without_colons(&mut self) -> PResult<Vec<PathSegment>>

Unstable

Examples: - a::b<T,U>::c<V,W> - a::b<T,U>::c(V) -> W - a::b<T,U>::c(V)

fn parse_path_segments_with_colons(&mut self) -> PResult<Vec<PathSegment>>

Unstable

Examples: - a::b::<T,U>::c

fn parse_path_segments_without_types(&mut self) -> PResult<Vec<PathSegment>>

Unstable

Examples: - a::b::c

fn parse_opt_lifetime(&mut self) -> PResult<Option<Lifetime>>

Unstable

parses 0 or 1 lifetime

fn parse_lifetime(&mut self) -> PResult<Lifetime>

Unstable

Parses a single lifetime Matches lifetime = LIFETIME

fn parse_lifetime_defs(&mut self) -> PResult<Vec<LifetimeDef>>

Unstable

Parses lifetime_defs = [ lifetime_defs { ',' lifetime_defs } ] where lifetime_def = lifetime [':' lifetimes]

fn parse_lifetimes(&mut self, sep: Token) -> PResult<Vec<Lifetime>>

Unstable

matches lifetimes = ( lifetime ) | ( lifetime , lifetimes ) actually, it matches the empty one too, but putting that in there messes up the grammar....

Parses zero or more comma separated lifetimes. Expects each lifetime to be followed by either a comma or >. Used when parsing type parameter lists, where we expect something like <'a, 'b, T>.

fn parse_mutability(&mut self) -> PResult<Mutability>

Unstable

Parse mutability declaration (mut/const/imm)

fn parse_field(&mut self) -> PResult<Field>

Unstable

Parse ident COLON expr

fn mk_expr(&mut self, lo: BytePos, hi: BytePos, node: Expr_) -> P<Expr>

Unstable

fn mk_unary(&mut self, unop: UnOp, expr: P<Expr>) -> Expr_

Unstable

fn mk_binary(&mut self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> Expr_

Unstable

fn mk_call(&mut self, f: P<Expr>, args: Vec<P<Expr>>) -> Expr_

Unstable

fn mk_index(&mut self, expr: P<Expr>, idx: P<Expr>) -> Expr_

Unstable

fn mk_range(&mut self, start: Option<P<Expr>>, end: Option<P<Expr>>) -> Expr_

Unstable

fn mk_field(&mut self, expr: P<Expr>, ident: SpannedIdent) -> Expr_

Unstable

fn mk_tup_field(&mut self, expr: P<Expr>, idx: Spanned<usize>) -> Expr_

Unstable

fn mk_assign_op(&mut self, binop: BinOp, lhs: P<Expr>, rhs: P<Expr>) -> Expr_

Unstable

fn mk_mac_expr(&mut self, lo: BytePos, hi: BytePos, m: Mac_) -> P<Expr>

Unstable

fn mk_lit_u32(&mut self, i: u32) -> P<Expr>

Unstable

fn parse_bottom_expr(&mut self) -> PResult<P<Expr>>

Unstable

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

fn parse_block_expr(&mut self, lo: BytePos, blk_mode: BlockCheckMode) -> PResult<P<Expr>>

Unstable

Parse a block or unsafe block

fn parse_dot_or_call_expr(&mut self) -> PResult<P<Expr>>

Unstable

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

fn parse_dot_or_call_expr_with(&mut self, e0: P<Expr>) -> PResult<P<Expr>>

Unstable

fn check_unknown_macro_variable(&mut self) -> PResult<()>

Unstable

fn parse_sep_and_kleene_op(&mut self) -> PResult<(Option<Token>, KleeneOp)>

Unstable

Parse an optional separator followed by a Kleene-style repetition token (+ or *).

fn parse_token_tree(&mut self) -> PResult<TokenTree>

Unstable

parse a single token tree from the input.

fn parse_all_token_trees(&mut self) -> PResult<Vec<TokenTree>>

Unstable

fn parse_prefix_expr(&mut self) -> PResult<P<Expr>>

Unstable

Parse a prefix-operator expr

fn parse_binops(&mut self) -> PResult<P<Expr>>

Unstable

Parse an expression of binops

fn parse_more_binops(&mut self, lhs: P<Expr>, min_prec: usize) -> PResult<P<Expr>>

Unstable

Parse an expression of binops of at least min_prec precedence

fn parse_assign_expr(&mut self) -> PResult<P<Expr>>

Unstable

Parse an assignment expression.... actually, this seems to be the main entry point for parsing an arbitrary expression.

fn parse_assign_expr_with(&mut self, lhs: P<Expr>) -> PResult<P<Expr>>

Unstable

fn parse_if_expr(&mut self) -> PResult<P<Expr>>

Unstable

Parse an 'if' or 'if let' expression ('if' token already eaten)

fn parse_if_let_expr(&mut self) -> PResult<P<Expr>>

Unstable

Parse an 'if let' expression ('if' token already eaten)

fn parse_lambda_expr(&mut self, capture_clause: CaptureClause) -> PResult<P<Expr>>

Unstable

fn parse_else_expr(&mut self) -> PResult<P<Expr>>

Unstable

fn parse_for_expr(&mut self, opt_ident: Option<Ident>) -> PResult<P<Expr>>

Unstable

Parse a 'for' .. 'in' expression ('for' token already eaten)

fn parse_while_expr(&mut self, opt_ident: Option<Ident>) -> PResult<P<Expr>>

Unstable

Parse a 'while' or 'while let' expression ('while' token already eaten)

fn parse_while_let_expr(&mut self, opt_ident: Option<Ident>) -> PResult<P<Expr>>

Unstable

Parse a 'while let' expression ('while' token already eaten)

fn parse_loop_expr(&mut self, opt_ident: Option<Ident>) -> PResult<P<Expr>>

Unstable

fn parse_arm_nopanic(&mut self) -> PResult<Arm>

Unstable

fn parse_expr_nopanic(&mut self) -> PResult<P<Expr>>

Unstable

Parse an expression

fn parse_expr_res(&mut self, r: Restrictions) -> PResult<P<Expr>>

Unstable

Parse an expression, subject to the given restrictions

fn parse_pat_nopanic(&mut self) -> PResult<P<Pat>>

Unstable

Parse a pattern.

fn parse_stmt_nopanic(&mut self) -> PResult<Option<P<Stmt>>>

Unstable

Parse a statement. may include decl.

fn parse_block(&mut self) -> PResult<P<Block>>

Unstable

Parse a block. No inner attrs are allowed.

fn parse_generics(&mut self) -> PResult<Generics>

Unstable

Parse 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 )

fn parse_where_clause(&mut self) -> PResult<WhereClause>

Unstable

Parses an optional where clause and places it in generics.

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

fn parse_fn_decl(&mut self, allow_variadic: bool) -> PResult<P<FnDecl>>

Unstable

Parse the argument list and result type of a function declaration

fn parse_impl_item(&mut self) -> PResult<P<ImplItem>>

Unstable

Parse an impl item.

fn parse_record_struct_body(&mut self, class_name: &Ident) -> PResult<Vec<StructField>>

Unstable

fn parse_tuple_struct_body(&mut self, class_name: &Ident, generics: &mut Generics) -> PResult<Vec<StructField>>

Unstable

fn parse_single_struct_field(&mut self, vis: Visibility, attrs: Vec<Attribute>) -> PResult<StructField>

Unstable

Parse a structure field declaration

fn parse_item_nopanic(&mut self) -> PResult<Option<P<Item>>>

Unstable

fn parse_crate_mod(&mut self) -> PResult<Crate>

Unstable

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

fn parse_optional_str(&mut self) -> PResult<Option<(InternedString, StrStyle, Option<Name>)>>

Unstable

fn parse_str(&mut self) -> PResult<(InternedString, StrStyle)>

Unstable

Trait Implementations

impl<'a> ParserAttr for Parser<'a>

fn parse_outer_attributes(&mut self) -> Vec<Attribute>

fn parse_attribute(&mut self, permit_inner: bool) -> Attribute

fn parse_inner_attributes(&mut self) -> Vec<Attribute>

fn parse_meta_item(&mut self) -> P<MetaItem>

fn parse_meta_seq(&mut self) -> Vec<P<MetaItem>>

fn parse_optional_meta(&mut self) -> Vec<P<MetaItem>>

impl<'a> ParserObsoleteMethods for Parser<'a>

fn obsolete(&mut self, sp: Span, kind: ObsoleteSyntax)

fn obsolete_expr(&mut self, sp: Span, kind: ObsoleteSyntax) -> P<Expr>

fn report(&mut self, sp: Span, kind: ObsoleteSyntax, kind_str: &str, desc: &str, error: bool)

fn is_obsolete_ident(&mut self, ident: &str) -> bool

fn eat_obsolete_ident(&mut self, ident: &str) -> bool