From 60058e5dbe1ef466010cc34aa31e84ccf1ebb330 Mon Sep 17 00:00:00 2001 From: Mark Simulacrum Date: Thu, 31 May 2018 16:53:30 -0600 Subject: [PATCH] Crate-ify and delete unused code in syntax::parse --- src/libsyntax/lib.rs | 1 + src/libsyntax/parse/attr.rs | 11 +- src/libsyntax/parse/common.rs | 36 --- src/libsyntax/parse/lexer/comments.rs | 2 +- src/libsyntax/parse/lexer/mod.rs | 40 ++- src/libsyntax/parse/lexer/tokentrees.rs | 2 +- src/libsyntax/parse/lexer/unicode_chars.rs | 2 +- src/libsyntax/parse/mod.rs | 57 ++-- src/libsyntax/parse/parser.rs | 339 +++++++-------------- src/libsyntax/parse/token.rs | 66 ++-- 10 files changed, 191 insertions(+), 365 deletions(-) delete mode 100644 src/libsyntax/parse/common.rs diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index e98170345696a..2ee14bd61c285 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -25,6 +25,7 @@ #![feature(const_atomic_usize_new)] #![feature(rustc_attrs)] #![feature(str_escape)] +#![feature(crate_visibility_modifier)] #![recursion_limit="256"] diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index cceed58921256..9919d910fbcca 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -11,8 +11,7 @@ use attr; use ast; use codemap::respan; -use parse::common::SeqSep; -use parse::PResult; +use parse::{SeqSep, PResult}; use parse::token::{self, Nonterminal}; use parse::parser::{Parser, TokenType, PathStyle}; use tokenstream::TokenStream; @@ -28,7 +27,7 @@ const DEFAULT_UNEXPECTED_INNER_ATTR_ERR_MSG: &'static str = "an inner attribute impl<'a> Parser<'a> { /// Parse attributes that appear before an item - pub fn parse_outer_attributes(&mut self) -> PResult<'a, Vec> { + crate fn parse_outer_attributes(&mut self) -> PResult<'a, Vec> { let mut attrs: Vec = Vec::new(); let mut just_parsed_doc_comment = false; loop { @@ -139,7 +138,7 @@ impl<'a> Parser<'a> { }) } - pub fn parse_path_and_tokens(&mut self) -> PResult<'a, (ast::Path, TokenStream)> { + crate fn parse_path_and_tokens(&mut self) -> PResult<'a, (ast::Path, TokenStream)> { let meta = match self.token { token::Interpolated(ref nt) => match nt.0 { Nonterminal::NtMeta(ref meta) => Some(meta.clone()), @@ -160,7 +159,7 @@ impl<'a> Parser<'a> { /// terminated by a semicolon. /// matches inner_attrs* - pub fn parse_inner_attributes(&mut self) -> PResult<'a, Vec> { + crate fn parse_inner_attributes(&mut self) -> PResult<'a, Vec> { let mut attrs: Vec = vec![]; loop { match self.token { @@ -231,7 +230,7 @@ impl<'a> Parser<'a> { Ok(ast::MetaItem { ident, node, span }) } - pub fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> { + crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> { Ok(if self.eat(&token::Eq) { ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?) } else if self.eat(&token::OpenDelim(token::Paren)) { diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs deleted file mode 100644 index fe931f7cf6a64..0000000000000 --- a/src/libsyntax/parse/common.rs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Common routines shared by parser mods - -use parse::token; - -/// `SeqSep` : a sequence separator (token) -/// and whether a trailing separator is allowed. -pub struct SeqSep { - pub sep: Option, - pub trailing_sep_allowed: bool, -} - -impl SeqSep { - pub fn trailing_allowed(t: token::Token) -> SeqSep { - SeqSep { - sep: Some(t), - trailing_sep_allowed: true, - } - } - - pub fn none() -> SeqSep { - SeqSep { - sep: None, - trailing_sep_allowed: false, - } - } -} diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 7ead1ceeed0ef..7da0d816d0f7a 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -40,7 +40,7 @@ pub struct Comment { pub pos: BytePos, } -pub fn is_doc_comment(s: &str) -> bool { +fn is_doc_comment(s: &str) -> bool { (s.starts_with("///") && super::is_doc_comment(s)) || s.starts_with("//!") || (s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!") } diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 04e180cd0538f..8363c1f39aa37 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -51,16 +51,16 @@ pub struct StringReader<'a> { pub ch: Option, pub filemap: Lrc, /// Stop reading src at this index. - pub end_src_index: usize, + end_src_index: usize, /// Whether to record new-lines and multibyte chars in filemap. /// This is only necessary the first time a filemap is lexed. /// If part of a filemap is being re-lexed, this should be set to false. - pub save_new_lines_and_multibyte: bool, + save_new_lines_and_multibyte: bool, // cached: peek_tok: token::Token, peek_span: Span, peek_span_src_raw: Span, - pub fatal_errs: Vec>, + fatal_errs: Vec>, // cache a direct reference to the source text, so that we don't have to // retrieve it via `self.filemap.src.as_ref().unwrap()` all the time. src: Lrc, @@ -70,7 +70,7 @@ pub struct StringReader<'a> { /// The raw source span which *does not* take `override_span` into account span_src_raw: Span, open_braces: Vec<(token::DelimToken, Span)>, - pub override_span: Option, + crate override_span: Option, } impl<'a> StringReader<'a> { @@ -163,11 +163,9 @@ impl<'a> StringReader<'a> { sp: self.peek_span, } } -} -impl<'a> StringReader<'a> { /// For comments.rs, which hackily pokes into next_pos and ch - pub fn new_raw(sess: &'a ParseSess, filemap: Lrc, + fn new_raw(sess: &'a ParseSess, filemap: Lrc, override_span: Option) -> Self { let mut sr = StringReader::new_raw_internal(sess, filemap, override_span); sr.bump(); @@ -240,17 +238,17 @@ impl<'a> StringReader<'a> { sr } - pub fn ch_is(&self, c: char) -> bool { + fn ch_is(&self, c: char) -> bool { self.ch == Some(c) } /// Report a fatal lexical error with a given span. - pub fn fatal_span(&self, sp: Span, m: &str) -> FatalError { + fn fatal_span(&self, sp: Span, m: &str) -> FatalError { self.sess.span_diagnostic.span_fatal(sp, m) } /// Report a lexical error with a given span. - pub fn err_span(&self, sp: Span, m: &str) { + fn err_span(&self, sp: Span, m: &str) { self.sess.span_diagnostic.span_err(sp, m) } @@ -375,7 +373,7 @@ impl<'a> StringReader<'a> { /// Calls `f` with a string slice of the source text spanning from `start` /// up to but excluding `self.pos`, meaning the slice does not include /// the character `self.ch`. - pub fn with_str_from(&self, start: BytePos, f: F) -> T + fn with_str_from(&self, start: BytePos, f: F) -> T where F: FnOnce(&str) -> T { self.with_str_from_to(start, self.pos, f) @@ -384,13 +382,13 @@ impl<'a> StringReader<'a> { /// Create a Name from a given offset to the current offset, each /// adjusted 1 towards each other (assumes that on either side there is a /// single-byte delimiter). - pub fn name_from(&self, start: BytePos) -> ast::Name { + fn name_from(&self, start: BytePos) -> ast::Name { debug!("taking an ident from {:?} to {:?}", start, self.pos); self.with_str_from(start, Symbol::intern) } /// As name_from, with an explicit endpoint. - pub fn name_from_to(&self, start: BytePos, end: BytePos) -> ast::Name { + fn name_from_to(&self, start: BytePos, end: BytePos) -> ast::Name { debug!("taking an ident from {:?} to {:?}", start, end); self.with_str_from_to(start, end, Symbol::intern) } @@ -454,7 +452,7 @@ impl<'a> StringReader<'a> { /// Advance the StringReader by one character. If a newline is /// discovered, add it to the FileMap's list of line start offsets. - pub fn bump(&mut self) { + crate fn bump(&mut self) { let next_src_index = self.src_index(self.next_pos); if next_src_index < self.end_src_index { let next_ch = char_at(&self.src, next_src_index); @@ -481,7 +479,7 @@ impl<'a> StringReader<'a> { } } - pub fn nextch(&self) -> Option { + fn nextch(&self) -> Option { let next_src_index = self.src_index(self.next_pos); if next_src_index < self.end_src_index { Some(char_at(&self.src, next_src_index)) @@ -490,11 +488,11 @@ impl<'a> StringReader<'a> { } } - pub fn nextch_is(&self, c: char) -> bool { + fn nextch_is(&self, c: char) -> bool { self.nextch() == Some(c) } - pub fn nextnextch(&self) -> Option { + fn nextnextch(&self) -> Option { let next_src_index = self.src_index(self.next_pos); if next_src_index < self.end_src_index { let next_next_src_index = @@ -506,7 +504,7 @@ impl<'a> StringReader<'a> { None } - pub fn nextnextch_is(&self, c: char) -> bool { + fn nextnextch_is(&self, c: char) -> bool { self.nextnextch() == Some(c) } @@ -1732,7 +1730,7 @@ impl<'a> StringReader<'a> { // This tests the character for the unicode property 'PATTERN_WHITE_SPACE' which // is guaranteed to be forward compatible. http://unicode.org/reports/tr31/#R3 -pub fn is_pattern_whitespace(c: Option) -> bool { +crate fn is_pattern_whitespace(c: Option) -> bool { c.map_or(false, Pattern_White_Space) } @@ -1747,14 +1745,14 @@ fn is_dec_digit(c: Option) -> bool { in_range(c, '0', '9') } -pub fn is_doc_comment(s: &str) -> bool { +fn is_doc_comment(s: &str) -> bool { let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/') || s.starts_with("//!"); debug!("is {:?} a doc comment? {}", s, res); res } -pub fn is_block_doc_comment(s: &str) -> bool { +fn is_block_doc_comment(s: &str) -> bool { // Prevent `/**/` from being parsed as a doc comment let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*') || s.starts_with("/*!")) && s.len() >= 5; diff --git a/src/libsyntax/parse/lexer/tokentrees.rs b/src/libsyntax/parse/lexer/tokentrees.rs index 278b8c991f7b8..36c220fa0d943 100644 --- a/src/libsyntax/parse/lexer/tokentrees.rs +++ b/src/libsyntax/parse/lexer/tokentrees.rs @@ -15,7 +15,7 @@ use tokenstream::{Delimited, TokenStream, TokenTree}; impl<'a> StringReader<'a> { // Parse a stream of tokens into a list of `TokenTree`s, up to an `Eof`. - pub fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> { + crate fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> { let mut tts = Vec::new(); while self.token != token::Eof { tts.push(self.parse_token_tree()?); diff --git a/src/libsyntax/parse/lexer/unicode_chars.rs b/src/libsyntax/parse/lexer/unicode_chars.rs index c5c2e02523302..a32b515672eca 100644 --- a/src/libsyntax/parse/lexer/unicode_chars.rs +++ b/src/libsyntax/parse/lexer/unicode_chars.rs @@ -333,7 +333,7 @@ const ASCII_ARRAY: &'static [(char, &'static str)] = &[ ('=', "Equals Sign"), ('>', "Greater-Than Sign"), ]; -pub fn check_for_substitution<'a>(reader: &StringReader<'a>, +crate fn check_for_substitution<'a>(reader: &StringReader<'a>, ch: char, err: &mut DiagnosticBuilder<'a>) -> bool { UNICODE_ARRAY diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 25490829f9e56..0050434d42e3f 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -38,7 +38,6 @@ pub mod lexer; pub mod token; pub mod attr; -pub mod common; pub mod classify; /// Info about a parsing session. @@ -51,7 +50,7 @@ pub struct ParseSess { /// raw identifiers pub raw_identifier_spans: Lock>, /// The registered diagnostics codes - pub registered_diagnostics: Lock, + crate registered_diagnostics: Lock, // Spans where a `mod foo;` statement was included in a non-mod.rs file. // These are used to issue errors if the non_modrs_mods feature is not enabled. pub non_modrs_mods: Lock>, @@ -131,7 +130,7 @@ pub fn parse_crate_attrs_from_source_str(name: FileName, source: String, sess: & new_parser_from_source_str(sess, name, source).parse_inner_attributes() } -pub fn parse_expr_from_source_str(name: FileName, source: String, sess: &ParseSess) +crate fn parse_expr_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult> { new_parser_from_source_str(sess, name, source).parse_expr() } @@ -140,17 +139,12 @@ pub fn parse_expr_from_source_str(name: FileName, source: String, sess: &ParseSe /// /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err` /// when a syntax error occurred. -pub fn parse_item_from_source_str(name: FileName, source: String, sess: &ParseSess) +crate fn parse_item_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult>> { new_parser_from_source_str(sess, name, source).parse_item() } -pub fn parse_meta_from_source_str(name: FileName, source: String, sess: &ParseSess) - -> PResult { - new_parser_from_source_str(sess, name, source).parse_meta_item() -} - -pub fn parse_stmt_from_source_str(name: FileName, source: String, sess: &ParseSess) +crate fn parse_stmt_from_source_str(name: FileName, source: String, sess: &ParseSess) -> PResult> { new_parser_from_source_str(sess, name, source).parse_stmt() } @@ -178,7 +172,7 @@ pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Parser<'a> /// Given a session, a crate config, a path, and a span, add /// the file at the given path to the codemap, and return a parser. /// On an error, use the given span as the source of the problem. -pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, +crate fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, directory_ownership: DirectoryOwnership, module_name: Option, @@ -190,7 +184,7 @@ pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, } /// Given a filemap and config, return a parser -pub fn filemap_to_parser(sess: & ParseSess, filemap: Lrc) -> Parser { +fn filemap_to_parser(sess: & ParseSess, filemap: Lrc) -> Parser { let end_pos = filemap.end_pos; let mut parser = stream_to_parser(sess, filemap_to_stream(sess, filemap, None)); @@ -243,7 +237,7 @@ pub fn stream_to_parser(sess: &ParseSess, stream: TokenStream) -> Parser { /// Rather than just accepting/rejecting a given literal, unescapes it as /// well. Can take any slice prefixed by a character escape. Returns the /// character and the number of characters consumed. -pub fn char_lit(lit: &str, diag: Option<(Span, &Handler)>) -> (char, isize) { +fn char_lit(lit: &str, diag: Option<(Span, &Handler)>) -> (char, isize) { use std::char; // Handle non-escaped chars first. @@ -300,7 +294,7 @@ pub fn char_lit(lit: &str, diag: Option<(Span, &Handler)>) -> (char, isize) { /// Parse a string representing a string literal into its final form. Does /// unescaping. -pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String { +fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String { debug!("str_lit: given {}", lit.escape_default()); let mut res = String::with_capacity(lit.len()); @@ -369,7 +363,7 @@ pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String { /// Parse a string representing a raw string literal into its final form. The /// only operation this does is convert embedded CRLF into a single LF. -pub fn raw_str_lit(lit: &str) -> String { +fn raw_str_lit(lit: &str) -> String { debug!("raw_str_lit: given {}", lit.escape_default()); let mut res = String::with_capacity(lit.len()); @@ -406,7 +400,7 @@ macro_rules! err { } } -pub fn lit_token(lit: token::Lit, suf: Option, diag: Option<(Span, &Handler)>) +crate fn lit_token(lit: token::Lit, suf: Option, diag: Option<(Span, &Handler)>) -> (bool /* suffix illegal? */, Option) { use ast::LitKind; @@ -476,7 +470,7 @@ fn filtered_float_lit(data: Symbol, suffix: Option, diag: Option<(Span, } }) } -pub fn float_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) +fn float_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) -> Option { debug!("float_lit: {:?}, {:?}", s, suffix); // FIXME #2252: bounds checking float literals is deferred until trans @@ -485,7 +479,7 @@ pub fn float_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)> } /// Parse a string representing a byte literal into its final form. Similar to `char_lit` -pub fn byte_lit(lit: &str) -> (u8, usize) { +fn byte_lit(lit: &str) -> (u8, usize) { let err = |i| format!("lexer accepted invalid byte literal {} step {}", lit, i); if lit.len() == 1 { @@ -516,7 +510,7 @@ pub fn byte_lit(lit: &str) -> (u8, usize) { } } -pub fn byte_str_lit(lit: &str) -> Lrc> { +fn byte_str_lit(lit: &str) -> Lrc> { let mut res = Vec::with_capacity(lit.len()); let error = |i| format!("lexer should have rejected {} at {}", lit, i); @@ -575,7 +569,7 @@ pub fn byte_str_lit(lit: &str) -> Lrc> { Lrc::new(res) } -pub fn integer_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) +fn integer_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) -> Option { // s can only be ascii, byte indexing is fine @@ -1136,3 +1130,26 @@ mod tests { }); } } + +/// `SeqSep` : a sequence separator (token) +/// and whether a trailing separator is allowed. +pub struct SeqSep { + pub sep: Option, + pub trailing_sep_allowed: bool, +} + +impl SeqSep { + pub fn trailing_allowed(t: token::Token) -> SeqSep { + SeqSep { + sep: Some(t), + trailing_sep_allowed: true, + } + } + + pub fn none() -> SeqSep { + SeqSep { + sep: None, + trailing_sep_allowed: false, + } + } +} diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 53d0b5529efce..ab2371626c374 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -23,7 +23,7 @@ use ast::{Field, FnDecl}; use ast::{ForeignItem, ForeignItemKind, FunctionRetTy}; use ast::GenericParam; use ast::{Ident, ImplItem, IsAuto, Item, ItemKind}; -use ast::{Label, Lifetime, LifetimeDef, Lit, LitKind, UintTy}; +use ast::{Label, Lifetime, LifetimeDef, Lit, LitKind}; use ast::Local; use ast::MacStmtStyle; use ast::{Mac, Mac_, MacDelimiter}; @@ -44,8 +44,7 @@ use {ast, attr}; use codemap::{self, CodeMap, Spanned, respan}; use syntax_pos::{self, Span, MultiSpan, BytePos, FileName, DUMMY_SP}; use errors::{self, Applicability, DiagnosticBuilder}; -use parse::{self, classify, token}; -use parse::common::SeqSep; +use parse::{self, SeqSep, classify, token}; use parse::lexer::TokenAndSpan; use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership}; @@ -64,7 +63,7 @@ use std::path::{self, Path, PathBuf}; use std::slice; bitflags! { - pub struct Restrictions: u8 { + struct Restrictions: u8 { const STMT_EXPR = 1 << 0; const NO_STRUCT_LITERAL = 1 << 1; } @@ -96,13 +95,13 @@ pub enum PathStyle { } #[derive(Clone, Copy, Debug, PartialEq)] -pub enum SemiColonMode { +enum SemiColonMode { Break, Ignore, } #[derive(Clone, Copy, Debug, PartialEq)] -pub enum BlockMode { +enum BlockMode { Break, Ignore, } @@ -223,22 +222,22 @@ pub struct Parser<'a> { /// the span of the current token: pub span: Span, /// the span of the previous token: - pub meta_var_span: Option, + meta_var_span: Option, pub prev_span: Span, /// the previous token kind prev_token_kind: PrevTokenKind, - pub restrictions: Restrictions, + restrictions: Restrictions, /// Used to determine the path to externally loaded source files - pub directory: Directory<'a>, + crate directory: Directory<'a>, /// Whether to parse sub-modules in other files. pub recurse_into_file_modules: bool, /// 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. pub root_module_name: Option, - pub expected_tokens: Vec, + crate expected_tokens: Vec, token_cursor: TokenCursor, - pub desugar_doc_comments: bool, + desugar_doc_comments: bool, /// Whether we should configure out of line modules as we parse. pub cfg_mods: bool, } @@ -377,7 +376,7 @@ impl TokenCursor { } #[derive(PartialEq, Eq, Clone)] -pub enum TokenType { +crate enum TokenType { Token(token::Token), Keyword(keywords::Keyword), Operator, @@ -390,7 +389,7 @@ pub enum TokenType { impl TokenType { fn to_string(&self) -> String { match *self { - TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)), + TokenType::Token(ref t) => format!("`{}`", pprust::token_to_string(t)), TokenType::Keyword(kw) => format!("`{}`", kw.name()), TokenType::Operator => "an operator".to_string(), TokenType::Lifetime => "lifetime".to_string(), @@ -413,8 +412,8 @@ fn can_continue_type_after_non_fn_ident(t: &token::Token) -> bool { /// Information about the path to a module. pub struct ModulePath { - pub name: String, - pub path_exists: bool, + name: String, + path_exists: bool, pub result: Result, } @@ -424,11 +423,6 @@ pub struct ModulePathSuccess { warn: bool, } -pub struct ModulePathError { - pub err_msg: String, - pub help_msg: String, -} - pub enum Error { FileNotFoundForModule { mod_name: String, @@ -446,7 +440,7 @@ pub enum Error { } impl Error { - pub fn span_err>(self, + fn span_err>(self, sp: S, handler: &errors::Handler) -> DiagnosticBuilder { match self { @@ -489,7 +483,7 @@ impl Error { } #[derive(Debug)] -pub enum LhsExpr { +enum LhsExpr { NotYetParsed, AttributesParsed(ThinVec), AlreadyParsed(P), @@ -596,17 +590,12 @@ impl<'a> Parser<'a> { next } - /// Convert a token to a string using self's reader - pub fn token_to_string(token: &token::Token) -> String { - pprust::token_to_string(token) - } - /// Convert the current token to a string using self's reader pub fn this_token_to_string(&self) -> String { - Parser::token_to_string(&self.token) + pprust::token_to_string(&self.token) } - pub fn token_descr(&self) -> Option<&'static str> { + fn token_descr(&self) -> Option<&'static str> { Some(match &self.token { t if t.is_special_ident() => "reserved identifier", t if t.is_used_keyword() => "keyword", @@ -615,7 +604,7 @@ impl<'a> Parser<'a> { }) } - pub fn this_token_descr(&self) -> String { + fn this_token_descr(&self) -> String { if let Some(prefix) = self.token_descr() { format!("{} `{}`", prefix, self.this_token_to_string()) } else { @@ -623,12 +612,12 @@ impl<'a> Parser<'a> { } } - pub fn unexpected_last(&self, t: &token::Token) -> PResult<'a, T> { - let token_str = Parser::token_to_string(t); + fn unexpected_last(&self, t: &token::Token) -> PResult<'a, T> { + let token_str = pprust::token_to_string(t); Err(self.span_fatal(self.prev_span, &format!("unexpected token: `{}`", token_str))) } - pub fn unexpected(&mut self) -> PResult<'a, T> { + crate fn unexpected(&mut self) -> PResult<'a, T> { match self.expect_one_of(&[], &[]) { Err(e) => Err(e), Ok(_) => unreachable!(), @@ -643,7 +632,7 @@ impl<'a> Parser<'a> { self.bump(); Ok(()) } else { - let token_str = Parser::token_to_string(t); + let token_str = pprust::token_to_string(t); let this_token_str = self.this_token_to_string(); let mut err = self.fatal(&format!("expected `{}`, found `{}`", token_str, @@ -659,7 +648,7 @@ impl<'a> Parser<'a> { /// 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. - pub fn expect_one_of(&mut self, + fn expect_one_of(&mut self, edible: &[token::Token], inedible: &[token::Token]) -> PResult<'a, ()>{ fn tokens_to_string(tokens: &[TokenType]) -> String { @@ -771,7 +760,7 @@ impl<'a> Parser<'a> { err } - pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> { + fn parse_ident(&mut self) -> PResult<'a, ast::Ident> { self.parse_ident_common(true) } @@ -804,7 +793,7 @@ impl<'a> Parser<'a> { /// /// This method will automatically add `tok` to `expected_tokens` if `tok` is not /// encountered. - pub fn check(&mut self, tok: &token::Token) -> bool { + fn check(&mut self, tok: &token::Token) -> bool { let is_present = self.token == *tok; if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); } is_present @@ -818,7 +807,7 @@ impl<'a> Parser<'a> { is_present } - pub fn check_keyword(&mut self, kw: keywords::Keyword) -> bool { + fn check_keyword(&mut self, kw: keywords::Keyword) -> bool { self.expected_tokens.push(TokenType::Keyword(kw)); self.token.is_keyword(kw) } @@ -834,7 +823,7 @@ impl<'a> Parser<'a> { } } - pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool { + fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool { if self.token.is_keyword(kw) { self.bump(); true @@ -846,7 +835,7 @@ impl<'a> Parser<'a> { /// 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. - pub fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> { + fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> { if !self.eat_keyword(kw) { self.unexpected() } else { @@ -949,7 +938,7 @@ impl<'a> Parser<'a> { } } - pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option) { + fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option) { match suffix { None => {/* everything ok */} Some(suf) => { @@ -994,7 +983,7 @@ impl<'a> Parser<'a> { /// 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. - pub fn expect_gt(&mut self) -> PResult<'a, ()> { + fn expect_gt(&mut self) -> PResult<'a, ()> { self.expected_tokens.push(TokenType::Token(token::Gt)); match self.token { token::Gt => { @@ -1017,83 +1006,9 @@ impl<'a> Parser<'a> { } } - pub fn parse_seq_to_before_gt_or_return(&mut self, - sep: Option, - mut f: F) - -> PResult<'a, (Vec, bool)> - where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option>, - { - let mut v = Vec::new(); - // This loop works by alternating back and forth between parsing types - // and commas. For example, given a string `A, B,>`, the parser would - // first parse `A`, then a comma, then `B`, then a comma. After that it - // would encounter a `>` and stop. This lets the parser handle trailing - // commas in generic parameters, because it can stop either after - // parsing a type or after parsing a comma. - for i in 0.. { - if self.check(&token::Gt) - || self.token == token::BinOp(token::Shr) - || self.token == token::Ge - || self.token == token::BinOpEq(token::Shr) { - break; - } - - if i % 2 == 0 { - match f(self)? { - Some(result) => v.push(result), - None => return Ok((v, true)) - } - } else { - if let Some(t) = sep.as_ref() { - self.expect(t)?; - } - - } - } - return Ok((v, false)); - } - - /// Parse a sequence bracketed by '<' and '>', stopping - /// before the '>'. - pub fn parse_seq_to_before_gt(&mut self, - sep: Option, - mut f: F) - -> PResult<'a, Vec> where - F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, - { - let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, - |p| Ok(Some(f(p)?)))?; - assert!(!returned); - return Ok(result); - } - - pub fn parse_seq_to_gt(&mut self, - sep: Option, - f: F) - -> PResult<'a, Vec> where - F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, - { - let v = self.parse_seq_to_before_gt(sep, f)?; - self.expect_gt()?; - return Ok(v); - } - - pub fn parse_seq_to_gt_or_return(&mut self, - sep: Option, - f: F) - -> PResult<'a, (Vec, bool)> where - F: FnMut(&mut Parser<'a>) -> PResult<'a, Option>, - { - let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?; - if !returned { - self.expect_gt()?; - } - return Ok((v, returned)); - } - /// Eat and discard tokens until one of `kets` is encountered. Respects token trees, /// passes through any errors encountered. Used for error recovery. - pub fn eat_to_tokens(&mut self, kets: &[&token::Token]) { + fn eat_to_tokens(&mut self, kets: &[&token::Token]) { let handler = self.diagnostic(); if let Err(ref mut err) = self.parse_seq_to_before_tokens(kets, @@ -1107,7 +1022,7 @@ impl<'a> Parser<'a> { /// Parse a sequence, including the closing delimiter. The function /// f must consume tokens until reaching the next separator or /// closing bracket. - pub fn parse_seq_to_end(&mut self, + crate fn parse_seq_to_end(&mut self, ket: &token::Token, sep: SeqSep, f: F) @@ -1122,7 +1037,7 @@ impl<'a> Parser<'a> { /// Parse a sequence, not including the closing delimiter. The function /// f must consume tokens until reaching the next separator or /// closing bracket. - pub fn parse_seq_to_before_end(&mut self, + fn parse_seq_to_before_end(&mut self, ket: &token::Token, sep: SeqSep, f: F) @@ -1197,7 +1112,7 @@ impl<'a> Parser<'a> { /// Parse a sequence, including the closing delimiter. The function /// f must consume tokens until reaching the next separator or /// closing bracket. - pub fn parse_unspanned_seq(&mut self, + fn parse_unspanned_seq(&mut self, bra: &token::Token, ket: &token::Token, sep: SeqSep, @@ -1213,24 +1128,6 @@ impl<'a> Parser<'a> { Ok(result) } - // NB: Do not use this function unless you actually plan to place the - // spanned list in the AST. - pub fn parse_seq(&mut self, - bra: &token::Token, - ket: &token::Token, - sep: SeqSep, - f: F) - -> PResult<'a, Spanned>> where - F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, - { - let lo = self.span; - self.expect(bra)?; - let result = self.parse_seq_to_before_end(ket, sep, f)?; - let hi = self.span; - self.bump(); - Ok(respan(lo.to(hi), result)) - } - /// Advance the parser by one token pub fn bump(&mut self) { if self.prev_token_kind == PrevTokenKind::Eof { @@ -1261,7 +1158,7 @@ impl<'a> Parser<'a> { /// Advance the parser using provided token as a next one. Use this when /// consuming a part of a token. For example a single `<` from `<<`. - pub fn bump_with(&mut self, next: token::Token, span: Span) { + fn bump_with(&mut self, next: token::Token, span: Span) { self.prev_span = self.span.with_hi(span.lo()); // It would be incorrect to record the kind of the current token, but // fortunately for tokens currently using `bump_with`, the @@ -1301,13 +1198,13 @@ impl<'a> Parser<'a> { pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> { self.sess.span_diagnostic.struct_span_fatal(self.span, m) } - pub fn span_fatal>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> { + fn span_fatal>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> { self.sess.span_diagnostic.struct_span_fatal(sp, m) } - pub fn span_fatal_err>(&self, sp: S, err: Error) -> DiagnosticBuilder<'a> { + fn span_fatal_err>(&self, sp: S, err: Error) -> DiagnosticBuilder<'a> { err.span_err(sp, self.diagnostic()) } - pub fn span_fatal_help>(&self, + fn span_fatal_help>(&self, sp: S, m: &str, help: &str) -> DiagnosticBuilder<'a> { @@ -1315,30 +1212,19 @@ impl<'a> Parser<'a> { err.help(help); err } - pub fn bug(&self, m: &str) -> ! { + fn bug(&self, m: &str) -> ! { self.sess.span_diagnostic.span_bug(self.span, m) } - pub fn warn(&self, m: &str) { - self.sess.span_diagnostic.span_warn(self.span, m) - } - pub fn span_warn>(&self, sp: S, m: &str) { - self.sess.span_diagnostic.span_warn(sp, m) - } - pub fn span_err>(&self, sp: S, m: &str) { + fn span_err>(&self, sp: S, m: &str) { self.sess.span_diagnostic.span_err(sp, m) } - pub fn struct_span_err>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> { + fn struct_span_err>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> { self.sess.span_diagnostic.struct_span_err(sp, m) } - pub fn span_err_help>(&self, sp: S, m: &str, h: &str) { - let mut err = self.sess.span_diagnostic.mut_span_err(sp, m); - err.help(h); - err.emit(); - } - pub fn span_bug>(&self, sp: S, m: &str) -> ! { + crate fn span_bug>(&self, sp: S, m: &str) -> ! { self.sess.span_diagnostic.span_bug(sp, m) } - pub fn abort_if_errors(&self) { + crate fn abort_if_errors(&self) { self.sess.span_diagnostic.abort_if_errors(); } @@ -1346,20 +1232,20 @@ impl<'a> Parser<'a> { self.sess.span_diagnostic.cancel(err) } - pub fn diagnostic(&self) -> &'a errors::Handler { + crate fn diagnostic(&self) -> &'a errors::Handler { &self.sess.span_diagnostic } /// Is the current token one of the keywords that signals a bare function /// type? - pub fn token_is_bare_fn_keyword(&mut self) -> bool { + fn token_is_bare_fn_keyword(&mut self) -> bool { self.check_keyword(keywords::Fn) || self.check_keyword(keywords::Unsafe) || self.check_keyword(keywords::Extern) && self.is_extern_non_path() } /// parse a TyKind::BareFn type: - pub fn parse_ty_bare_fn(&mut self, generic_params: Vec) + fn parse_ty_bare_fn(&mut self, generic_params: Vec) -> PResult<'a, TyKind> { /* @@ -1786,7 +1672,7 @@ impl<'a> Parser<'a> { return Ok(TyKind::Rptr(opt_lifetime, MutTy { ty: ty, mutbl: mutbl })); } - pub fn parse_ptr(&mut self) -> PResult<'a, MutTy> { + fn parse_ptr(&mut self) -> PResult<'a, MutTy> { let mutbl = if self.eat_keyword(keywords::Mut) { Mutability::Mutable } else if self.eat_keyword(keywords::Const) { @@ -1819,7 +1705,7 @@ impl<'a> Parser<'a> { /// This version of parse arg doesn't necessarily require /// identifier names. - pub fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> { + fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> { maybe_whole!(self, NtArg, |x| x); let (pat, ty) = if require_name || self.is_named_argument() { @@ -1849,12 +1735,12 @@ impl<'a> Parser<'a> { } /// Parse a single function argument - pub fn parse_arg(&mut self) -> PResult<'a, Arg> { + crate fn parse_arg(&mut self) -> PResult<'a, Arg> { self.parse_arg_general(true) } /// Parse an argument in a lambda header e.g. |arg, arg| - pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> { + fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> { let pat = self.parse_pat()?; let t = if self.eat(&token::Colon) { self.parse_ty()? @@ -1872,7 +1758,7 @@ impl<'a> Parser<'a> { }) } - pub fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option>> { + fn maybe_parse_fixed_length_of_vec(&mut self) -> PResult<'a, Option>> { if self.eat(&token::Semi) { Ok(Some(self.parse_expr()?)) } else { @@ -1881,7 +1767,7 @@ impl<'a> Parser<'a> { } /// Matches token_lit = LIT_INTEGER | ... - pub fn parse_lit_token(&mut self) -> PResult<'a, LitKind> { + fn parse_lit_token(&mut self) -> PResult<'a, LitKind> { let out = match self.token { token::Interpolated(ref nt) => match nt.0 { token::NtExpr(ref v) | token::NtLiteral(ref v) => match v.node { @@ -1909,7 +1795,7 @@ impl<'a> Parser<'a> { } /// Matches lit = true | false | token_lit - pub fn parse_lit(&mut self) -> PResult<'a, Lit> { + crate fn parse_lit(&mut self) -> PResult<'a, Lit> { let lo = self.span; let lit = if self.eat_keyword(keywords::True) { LitKind::Bool(true) @@ -1923,7 +1809,7 @@ impl<'a> Parser<'a> { } /// matches '-' lit | lit (cf. ast_validation::AstValidator::check_expr_within_pat) - pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P> { + crate fn parse_literal_maybe_minus(&mut self) -> PResult<'a, P> { maybe_whole_expr!(self); let minus_lo = self.span; @@ -1942,7 +1828,7 @@ impl<'a> Parser<'a> { } } - pub fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> { + fn parse_path_segment_ident(&mut self) -> PResult<'a, ast::Ident> { match self.token { token::Ident(ident, _) if self.token.is_path_segment_keyword() => { let span = self.span; @@ -2000,11 +1886,11 @@ impl<'a> Parser<'a> { /// `a::b::C::` (with disambiguator) /// `Fn(Args)` (without disambiguator) /// `Fn::(Args)` (with disambiguator) - pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> { + crate fn parse_path(&mut self, style: PathStyle) -> PResult<'a, ast::Path> { self.parse_path_common(style, true) } - pub fn parse_path_common(&mut self, style: PathStyle, enable_warning: bool) + crate fn parse_path_common(&mut self, style: PathStyle, enable_warning: bool) -> PResult<'a, ast::Path> { maybe_whole!(self, NtPath, |path| { if style == PathStyle::Mod && @@ -2114,13 +2000,13 @@ impl<'a> Parser<'a> { }) } - pub fn check_lifetime(&mut self) -> bool { + crate fn check_lifetime(&mut self) -> bool { self.expected_tokens.push(TokenType::Lifetime); self.token.is_lifetime() } /// Parse single lifetime 'a or panic. - pub fn expect_lifetime(&mut self) -> Lifetime { + crate fn expect_lifetime(&mut self) -> Lifetime { if let Some(ident) = self.token.lifetime() { let span = self.span; self.bump(); @@ -2149,7 +2035,7 @@ impl<'a> Parser<'a> { } } - pub fn parse_field_name(&mut self) -> PResult<'a, Ident> { + fn parse_field_name(&mut self) -> PResult<'a, Ident> { if let token::Literal(token::Integer(name), None) = self.token { self.bump(); Ok(Ident::new(name, self.prev_span)) @@ -2159,7 +2045,7 @@ impl<'a> Parser<'a> { } /// Parse ident (COLON expr)? - pub fn parse_field(&mut self) -> PResult<'a, Field> { + fn parse_field(&mut self) -> PResult<'a, Field> { let attrs = self.parse_outer_attributes()?; let lo = self.span; @@ -2185,27 +2071,27 @@ impl<'a> Parser<'a> { }) } - pub fn mk_expr(&mut self, span: Span, node: ExprKind, attrs: ThinVec) -> P { + fn mk_expr(&mut self, span: Span, node: ExprKind, attrs: ThinVec) -> P { P(Expr { node, span, attrs, id: ast::DUMMY_NODE_ID }) } - pub fn mk_unary(&mut self, unop: ast::UnOp, expr: P) -> ast::ExprKind { + fn mk_unary(&mut self, unop: ast::UnOp, expr: P) -> ast::ExprKind { ExprKind::Unary(unop, expr) } - pub fn mk_binary(&mut self, binop: ast::BinOp, lhs: P, rhs: P) -> ast::ExprKind { + fn mk_binary(&mut self, binop: ast::BinOp, lhs: P, rhs: P) -> ast::ExprKind { ExprKind::Binary(binop, lhs, rhs) } - pub fn mk_call(&mut self, f: P, args: Vec>) -> ast::ExprKind { + fn mk_call(&mut self, f: P, args: Vec>) -> ast::ExprKind { ExprKind::Call(f, args) } - pub fn mk_index(&mut self, expr: P, idx: P) -> ast::ExprKind { + fn mk_index(&mut self, expr: P, idx: P) -> ast::ExprKind { ExprKind::Index(expr, idx) } - pub fn mk_range(&mut self, + fn mk_range(&mut self, start: Option>, end: Option>, limits: RangeLimits) @@ -2217,12 +2103,12 @@ impl<'a> Parser<'a> { } } - pub fn mk_assign_op(&mut self, binop: ast::BinOp, + fn mk_assign_op(&mut self, binop: ast::BinOp, lhs: P, rhs: P) -> ast::ExprKind { ExprKind::AssignOp(binop, lhs, rhs) } - pub fn mk_mac_expr(&mut self, span: Span, m: Mac_, attrs: ThinVec) -> P { + fn mk_mac_expr(&mut self, span: Span, m: Mac_, attrs: ThinVec) -> P { P(Expr { id: ast::DUMMY_NODE_ID, node: ExprKind::Mac(codemap::Spanned {node: m, span: span}), @@ -2231,21 +2117,6 @@ impl<'a> Parser<'a> { }) } - pub fn mk_lit_u32(&mut self, i: u32, attrs: ThinVec) -> P { - let span = &self.span; - let lv_lit = P(codemap::Spanned { - node: LitKind::Int(i as u128, ast::LitIntType::Unsigned(UintTy::U32)), - span: *span - }); - - P(Expr { - id: ast::DUMMY_NODE_ID, - node: ExprKind::Lit(lv_lit), - span: *span, - attrs, - }) - } - fn expect_delimited_token_tree(&mut self) -> PResult<'a, (MacDelimiter, ThinTokenStream)> { let delim = match self.token { token::OpenDelim(delim) => delim, @@ -2598,7 +2469,7 @@ impl<'a> Parser<'a> { } /// Parse a block or unsafe block - pub fn parse_block_expr(&mut self, opt_label: Option