Skip to content
Permalink
Browse files

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

  • Loading branch information...
nnethercote committed May 23, 2019
1 parent 42eedf5 commit 8871a5cb67e06ed6cafc42b5348ed75fb0b9a378
Showing with 34 additions and 27 deletions.
  1. +27 −27 src/libsyntax/ext/tt/macro_parser.rs
  2. +7 −0 src/libsyntax_pos/symbol.rs
@@ -80,7 +80,7 @@ use crate::parse::{Directory, ParseSess};
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, Symbol, sym};
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);
}
}
@@ -784,7 +784,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;
@@ -812,7 +812,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 {
@@ -822,16 +822,16 @@ fn may_begin_with(name: &str, token: &Token) -> 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(_)
@@ -845,15 +845,15 @@ fn may_begin_with(name: &str, token: &Token) -> 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,
_ => may_be_ident(&nt),
},
_ => 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
@@ -869,7 +869,7 @@ fn may_begin_with(name: &str, token: &Token) -> 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,
@@ -896,34 +896,34 @@ fn may_begin_with(name: &str, token: &Token) -> 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)
@@ -932,10 +932,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 @@ symbols! {
existential_type,
expected,
export_name,
expr,
extern_absolute_paths,
external_doc,
extern_crate_item_prelude,
@@ -329,6 +330,7 @@ symbols! {
issue,
issue_5723_bootstrap,
issue_tracker_base_url,
item,
item_like_imports,
iter,
Iterator,
@@ -339,6 +341,7 @@ symbols! {
lang,
lang_items,
lib,
lifetime,
link,
linkage,
link_args,
@@ -347,6 +350,7 @@ symbols! {
link_name,
link_section,
lint_reasons,
literal,
local_inner_macros,
log_syntax,
loop_break_value,
@@ -435,6 +439,7 @@ symbols! {
partial_cmp,
PartialOrd,
passes,
pat,
path,
pattern_parentheses,
Pending,
@@ -578,6 +583,7 @@ symbols! {
static_recursion,
std,
str,
stmt,
stmt_expr_attributes,
stop_after_dataflow,
struct_field_attributes,
@@ -610,6 +616,7 @@ symbols! {
Try,
try_blocks,
try_trait,
tt,
tuple_indexing,
ty,
type_alias_enum_variants,

0 comments on commit 8871a5c

Please sign in to comment.
You can’t perform that action at this time.