Skip to content

Commit 33a3206

Browse files
committed
Use Symbol equality in may_begin_with and parse_nt.
1 parent 15789a9 commit 33a3206

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use crate::parse::{Directory, ParseSess};
8080
use crate::parse::parser::{Parser, PathStyle};
8181
use crate::parse::token::{self, DocComment, Nonterminal, Token};
8282
use crate::print::pprust;
83-
use crate::symbol::kw;
83+
use crate::symbol::{kw, sym, Symbol};
8484
use crate::tokenstream::{DelimSpan, TokenStream};
8585

8686
use errors::FatalError;
@@ -598,7 +598,7 @@ fn inner_parse_loop<'root, 'tt>(
598598
TokenTree::MetaVarDecl(_, _, id) => {
599599
// Built-in nonterminals never start with these tokens,
600600
// so we can eliminate them from consideration.
601-
if may_begin_with(&*id.as_str(), token) {
601+
if may_begin_with(id.name, token) {
602602
bb_items.push(item);
603603
}
604604
}
@@ -791,7 +791,7 @@ pub fn parse(
791791
let match_cur = item.match_cur;
792792
item.push_match(
793793
match_cur,
794-
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, &ident.as_str()))),
794+
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, ident.name))),
795795
);
796796
item.idx += 1;
797797
item.match_cur += 1;
@@ -819,7 +819,7 @@ fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
819819
///
820820
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
821821
/// token. Be conservative (return true) if not sure.
822-
fn may_begin_with(name: &str, token: &Token) -> bool {
822+
fn may_begin_with(name: Symbol, token: &Token) -> bool {
823823
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
824824
fn may_be_ident(nt: &token::Nonterminal) -> bool {
825825
match *nt {
@@ -829,16 +829,16 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
829829
}
830830

831831
match name {
832-
"expr" => token.can_begin_expr(),
833-
"ty" => token.can_begin_type(),
834-
"ident" => get_macro_ident(token).is_some(),
835-
"literal" => token.can_begin_literal_or_bool(),
836-
"vis" => match *token {
832+
sym::expr => token.can_begin_expr(),
833+
sym::ty => token.can_begin_type(),
834+
sym::ident => get_macro_ident(token).is_some(),
835+
sym::literal => token.can_begin_literal_or_bool(),
836+
sym::vis => match *token {
837837
// The follow-set of :vis + "priv" keyword + interpolated
838838
Token::Comma | Token::Ident(..) | Token::Interpolated(_) => true,
839839
_ => token.can_begin_type(),
840840
},
841-
"block" => match *token {
841+
sym::block => match *token {
842842
Token::OpenDelim(token::Brace) => true,
843843
Token::Interpolated(ref nt) => match **nt {
844844
token::NtItem(_)
@@ -852,15 +852,15 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
852852
},
853853
_ => false,
854854
},
855-
"path" | "meta" => match *token {
855+
sym::path | sym::meta => match *token {
856856
Token::ModSep | Token::Ident(..) => true,
857857
Token::Interpolated(ref nt) => match **nt {
858858
token::NtPath(_) | token::NtMeta(_) => true,
859859
_ => may_be_ident(&nt),
860860
},
861861
_ => false,
862862
},
863-
"pat" => match *token {
863+
sym::pat => match *token {
864864
Token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
865865
Token::OpenDelim(token::Paren) | // tuple pattern
866866
Token::OpenDelim(token::Bracket) | // slice pattern
@@ -876,7 +876,7 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
876876
Token::Interpolated(ref nt) => may_be_ident(nt),
877877
_ => false,
878878
},
879-
"lifetime" => match *token {
879+
sym::lifetime => match *token {
880880
Token::Lifetime(_) => true,
881881
Token::Interpolated(ref nt) => match **nt {
882882
token::NtLifetime(_) | token::NtTT(_) => true,
@@ -903,34 +903,34 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
903903
/// # Returns
904904
///
905905
/// The parsed non-terminal.
906-
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
907-
if name == "tt" {
906+
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> Nonterminal {
907+
if name == sym::tt {
908908
return token::NtTT(p.parse_token_tree());
909909
}
910910
// check at the beginning and the parser checks after each bump
911911
p.process_potential_macro_variable();
912912
match name {
913-
"item" => match panictry!(p.parse_item()) {
913+
sym::item => match panictry!(p.parse_item()) {
914914
Some(i) => token::NtItem(i),
915915
None => {
916916
p.fatal("expected an item keyword").emit();
917917
FatalError.raise();
918918
}
919919
},
920-
"block" => token::NtBlock(panictry!(p.parse_block())),
921-
"stmt" => match panictry!(p.parse_stmt()) {
920+
sym::block => token::NtBlock(panictry!(p.parse_block())),
921+
sym::stmt => match panictry!(p.parse_stmt()) {
922922
Some(s) => token::NtStmt(s),
923923
None => {
924924
p.fatal("expected a statement").emit();
925925
FatalError.raise();
926926
}
927927
},
928-
"pat" => token::NtPat(panictry!(p.parse_pat(None))),
929-
"expr" => token::NtExpr(panictry!(p.parse_expr())),
930-
"literal" => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
931-
"ty" => token::NtTy(panictry!(p.parse_ty())),
928+
sym::pat => token::NtPat(panictry!(p.parse_pat(None))),
929+
sym::expr => token::NtExpr(panictry!(p.parse_expr())),
930+
sym::literal => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
931+
sym::ty => token::NtTy(panictry!(p.parse_ty())),
932932
// this could be handled like a token, since it is one
933-
"ident" => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
933+
sym::ident => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
934934
let span = p.span;
935935
p.bump();
936936
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 {
939939
p.fatal(&format!("expected ident, found {}", &token_str)).emit();
940940
FatalError.raise()
941941
}
942-
"path" => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
943-
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
944-
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
945-
"lifetime" => if p.check_lifetime() {
942+
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
943+
sym::meta => token::NtMeta(panictry!(p.parse_meta_item())),
944+
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
945+
sym::lifetime => if p.check_lifetime() {
946946
token::NtLifetime(p.expect_lifetime().ident)
947947
} else {
948948
let token_str = pprust::token_to_string(&p.token);

src/libsyntax_pos/symbol.rs

+7
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ symbols! {
252252
existential_type,
253253
expected,
254254
export_name,
255+
expr,
255256
extern_absolute_paths,
256257
external_doc,
257258
extern_crate_item_prelude,
@@ -329,6 +330,7 @@ symbols! {
329330
issue,
330331
issue_5723_bootstrap,
331332
issue_tracker_base_url,
333+
item,
332334
item_like_imports,
333335
iter,
334336
Iterator,
@@ -339,6 +341,7 @@ symbols! {
339341
lang,
340342
lang_items,
341343
lib,
344+
lifetime,
342345
link,
343346
linkage,
344347
link_args,
@@ -347,6 +350,7 @@ symbols! {
347350
link_name,
348351
link_section,
349352
lint_reasons,
353+
literal,
350354
local_inner_macros,
351355
log_syntax,
352356
loop_break_value,
@@ -435,6 +439,7 @@ symbols! {
435439
partial_cmp,
436440
PartialOrd,
437441
passes,
442+
pat,
438443
path,
439444
pattern_parentheses,
440445
Pending,
@@ -578,6 +583,7 @@ symbols! {
578583
static_recursion,
579584
std,
580585
str,
586+
stmt,
581587
stmt_expr_attributes,
582588
stop_after_dataflow,
583589
struct_field_attributes,
@@ -610,6 +616,7 @@ symbols! {
610616
Try,
611617
try_blocks,
612618
try_trait,
619+
tt,
613620
tuple_indexing,
614621
ty,
615622
type_alias_enum_variants,

0 commit comments

Comments
 (0)