Skip to content

Commit 71920d3

Browse files
committed
Use Symbol equality in may_begin_with and parse_nt.
1 parent c389116 commit 71920d3

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
}
@@ -784,7 +784,7 @@ pub fn parse(
784784
let match_cur = item.match_cur;
785785
item.push_match(
786786
match_cur,
787-
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, &ident.as_str()))),
787+
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, ident.name))),
788788
);
789789
item.idx += 1;
790790
item.match_cur += 1;
@@ -812,7 +812,7 @@ fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
812812
///
813813
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
814814
/// token. Be conservative (return true) if not sure.
815-
fn may_begin_with(name: &str, token: &Token) -> bool {
815+
fn may_begin_with(name: Symbol, token: &Token) -> bool {
816816
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
817817
fn may_be_ident(nt: &token::Nonterminal) -> bool {
818818
match *nt {
@@ -822,16 +822,16 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
822822
}
823823

824824
match name {
825-
"expr" => token.can_begin_expr(),
826-
"ty" => token.can_begin_type(),
827-
"ident" => get_macro_ident(token).is_some(),
828-
"literal" => token.can_begin_literal_or_bool(),
829-
"vis" => match *token {
825+
sym::expr => token.can_begin_expr(),
826+
sym::ty => token.can_begin_type(),
827+
sym::ident => get_macro_ident(token).is_some(),
828+
sym::literal => token.can_begin_literal_or_bool(),
829+
sym::vis => match *token {
830830
// The follow-set of :vis + "priv" keyword + interpolated
831831
Token::Comma | Token::Ident(..) | Token::Interpolated(_) => true,
832832
_ => token.can_begin_type(),
833833
},
834-
"block" => match *token {
834+
sym::block => match *token {
835835
Token::OpenDelim(token::Brace) => true,
836836
Token::Interpolated(ref nt) => match **nt {
837837
token::NtItem(_)
@@ -845,15 +845,15 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
845845
},
846846
_ => false,
847847
},
848-
"path" | "meta" => match *token {
848+
sym::path | sym::meta => match *token {
849849
Token::ModSep | Token::Ident(..) => true,
850850
Token::Interpolated(ref nt) => match **nt {
851851
token::NtPath(_) | token::NtMeta(_) => true,
852852
_ => may_be_ident(&nt),
853853
},
854854
_ => false,
855855
},
856-
"pat" => match *token {
856+
sym::pat => match *token {
857857
Token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
858858
Token::OpenDelim(token::Paren) | // tuple pattern
859859
Token::OpenDelim(token::Bracket) | // slice pattern
@@ -869,7 +869,7 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
869869
Token::Interpolated(ref nt) => may_be_ident(nt),
870870
_ => false,
871871
},
872-
"lifetime" => match *token {
872+
sym::lifetime => match *token {
873873
Token::Lifetime(_) => true,
874874
Token::Interpolated(ref nt) => match **nt {
875875
token::NtLifetime(_) | token::NtTT(_) => true,
@@ -896,34 +896,34 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
896896
/// # Returns
897897
///
898898
/// The parsed non-terminal.
899-
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
900-
if name == "tt" {
899+
fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> Nonterminal {
900+
if name == sym::tt {
901901
return token::NtTT(p.parse_token_tree());
902902
}
903903
// check at the beginning and the parser checks after each bump
904904
p.process_potential_macro_variable();
905905
match name {
906-
"item" => match panictry!(p.parse_item()) {
906+
sym::item => match panictry!(p.parse_item()) {
907907
Some(i) => token::NtItem(i),
908908
None => {
909909
p.fatal("expected an item keyword").emit();
910910
FatalError.raise();
911911
}
912912
},
913-
"block" => token::NtBlock(panictry!(p.parse_block())),
914-
"stmt" => match panictry!(p.parse_stmt()) {
913+
sym::block => token::NtBlock(panictry!(p.parse_block())),
914+
sym::stmt => match panictry!(p.parse_stmt()) {
915915
Some(s) => token::NtStmt(s),
916916
None => {
917917
p.fatal("expected a statement").emit();
918918
FatalError.raise();
919919
}
920920
},
921-
"pat" => token::NtPat(panictry!(p.parse_pat(None))),
922-
"expr" => token::NtExpr(panictry!(p.parse_expr())),
923-
"literal" => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
924-
"ty" => token::NtTy(panictry!(p.parse_ty())),
921+
sym::pat => token::NtPat(panictry!(p.parse_pat(None))),
922+
sym::expr => token::NtExpr(panictry!(p.parse_expr())),
923+
sym::literal => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
924+
sym::ty => token::NtTy(panictry!(p.parse_ty())),
925925
// this could be handled like a token, since it is one
926-
"ident" => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
926+
sym::ident => if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
927927
let span = p.span;
928928
p.bump();
929929
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 {
932932
p.fatal(&format!("expected ident, found {}", &token_str)).emit();
933933
FatalError.raise()
934934
}
935-
"path" => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
936-
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
937-
"vis" => token::NtVis(panictry!(p.parse_visibility(true))),
938-
"lifetime" => if p.check_lifetime() {
935+
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
936+
sym::meta => token::NtMeta(panictry!(p.parse_meta_item())),
937+
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
938+
sym::lifetime => if p.check_lifetime() {
939939
token::NtLifetime(p.expect_lifetime().ident)
940940
} else {
941941
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)