提交 33a3206d 编写于 作者: N Nicholas Nethercote

Use `Symbol` equality in `may_begin_with` and `parse_nt`.

上级 15789a97
......@@ -80,7 +80,7 @@
use crate::parse::parser::{Parser, PathStyle};
use crate::parse::token::{self, DocComment, Nonterminal, Token};
use crate::print::pprust;
use crate::symbol::kw;
use crate::symbol::{kw, sym, Symbol};
use crate::tokenstream::{DelimSpan, TokenStream};
use errors::FatalError;
......@@ -598,7 +598,7 @@ fn inner_parse_loop<'root, 'tt>(
TokenTree::MetaVarDecl(_, _, id) => {
// Built-in nonterminals never start with these tokens,
// so we can eliminate them from consideration.
if may_begin_with(&*id.as_str(), token) {
if may_begin_with(id.name, token) {
bb_items.push(item);
}
}
......@@ -791,7 +791,7 @@ pub fn parse(
let match_cur = item.match_cur;
item.push_match(
match_cur,
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, &ident.as_str()))),
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, ident.name))),
);
item.idx += 1;
item.match_cur += 1;
......@@ -819,7 +819,7 @@ fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
///
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
/// token. Be conservative (return true) if not sure.
fn may_begin_with(name: &str, token: &Token) -> bool {
fn may_begin_with(name: Symbol, token: &Token) -> bool {
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
fn may_be_ident(nt: &token::Nonterminal) -> bool {
match *nt {
......@@ -829,16 +829,16 @@ fn may_be_ident(nt: &token::Nonterminal) -> bool {
}
match name {
"expr" => token.can_begin_expr(),
"ty" => token.can_begin_type(),
"ident" => get_macro_ident(token).is_some(),
"literal" => token.can_begin_literal_or_bool(),
"vis" => match *token {
sym::expr => token.can_begin_expr(),
sym::ty => token.can_begin_type(),
sym::ident => get_macro_ident(token).is_some(),
sym::literal => token.can_begin_literal_or_bool(),
sym::vis => match *token {
// The follow-set of :vis + "priv" keyword + interpolated
Token::Comma | Token::Ident(..) | Token::Interpolated(_) => true,
_ => token.can_begin_type(),
},
"block" => match *token {
sym::block => match *token {
Token::OpenDelim(token::Brace) => true,
Token::Interpolated(ref nt) => match **nt {
token::NtItem(_)
......@@ -852,7 +852,7 @@ fn may_be_ident(nt: &token::Nonterminal) -> bool {
},
_ => false,
},
"path" | "meta" => match *token {
sym::path | sym::meta => match *token {
Token::ModSep | Token::Ident(..) => true,
Token::Interpolated(ref nt) => match **nt {
token::NtPath(_) | token::NtMeta(_) => true,
......@@ -860,7 +860,7 @@ fn may_be_ident(nt: &token::Nonterminal) -> bool {
},
_ => false,
},
"pat" => match *token {
sym::pat => match *token {
Token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
Token::OpenDelim(token::Paren) | // tuple pattern
Token::OpenDelim(token::Bracket) | // slice pattern
......@@ -876,7 +876,7 @@ fn may_be_ident(nt: &token::Nonterminal) -> bool {
Token::Interpolated(ref nt) => may_be_ident(nt),
_ => false,
},
"lifetime" => match *token {
sym::lifetime => match *token {
Token::Lifetime(_) => true,
Token::Interpolated(ref nt) => match **nt {
token::NtLifetime(_) | token::NtTT(_) => true,
......@@ -903,34 +903,34 @@ fn may_be_ident(nt: &token::Nonterminal) -> bool {
/// # Returns
///
/// The parsed non-terminal.
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
if name == "tt" {
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> Nonterminal {
if name == sym::tt {
return token::NtTT(p.parse_token_tree());
}
// check at the beginning and the parser checks after each bump
p.process_potential_macro_variable();
match name {
"item" => match panictry!(p.parse_item()) {
sym::item => match panictry!(p.parse_item()) {
Some(i) => token::NtItem(i),
None => {
p.fatal("expected an item keyword").emit();
FatalError.raise();
}
},
"block" => token::NtBlock(panictry!(p.parse_block())),
"stmt" => match panictry!(p.parse_stmt()) {
sym::block => token::NtBlock(panictry!(p.parse_block())),
sym::stmt => match panictry!(p.parse_stmt()) {
Some(s) => token::NtStmt(s),
None => {
p.fatal("expected a statement").emit();
FatalError.raise();
}
},
"pat" => token::NtPat(panictry!(p.parse_pat(None))),
"expr" => token::NtExpr(panictry!(p.parse_expr())),
"literal" => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
"ty" => token::NtTy(panictry!(p.parse_ty())),
sym::pat => token::NtPat(panictry!(p.parse_pat(None))),
sym::expr => token::NtExpr(panictry!(p.parse_expr())),
sym::literal => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
sym::ty => token::NtTy(panictry!(p.parse_ty())),
// this could be handled like a token, since it is one
"ident" => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
sym::ident => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
let span = p.span;
p.bump();
token::NtIdent(Ident::new(ident.name, span), is_raw)
......@@ -939,10 +939,10 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
p.fatal(&format!("expected ident, found {}", &token_str)).emit();
FatalError.raise()
}
"path" => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
"lifetime" => if p.check_lifetime() {
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
sym::meta => token::NtMeta(panictry!(p.parse_meta_item())),
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
sym::lifetime => if p.check_lifetime() {
token::NtLifetime(p.expect_lifetime().ident)
} else {
let token_str = pprust::token_to_string(&p.token);
......
......@@ -252,6 +252,7 @@
existential_type,
expected,
export_name,
expr,
extern_absolute_paths,
external_doc,
extern_crate_item_prelude,
......@@ -329,6 +330,7 @@
issue,
issue_5723_bootstrap,
issue_tracker_base_url,
item,
item_like_imports,
iter,
Iterator,
......@@ -339,6 +341,7 @@
lang,
lang_items,
lib,
lifetime,
link,
linkage,
link_args,
......@@ -347,6 +350,7 @@
link_name,
link_section,
lint_reasons,
literal,
local_inner_macros,
log_syntax,
loop_break_value,
......@@ -435,6 +439,7 @@
partial_cmp,
PartialOrd,
passes,
pat,
path,
pattern_parentheses,
Pending,
......@@ -578,6 +583,7 @@
static_recursion,
std,
str,
stmt,
stmt_expr_attributes,
stop_after_dataflow,
struct_field_attributes,
......@@ -610,6 +616,7 @@
Try,
try_blocks,
try_trait,
tt,
tuple_indexing,
ty,
type_alias_enum_variants,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册