1
Fork 0

Return a is_raw parameter from Token::ident rather than having separate methods.

This commit is contained in:
Lymia Aluysia 2018-03-18 12:16:02 -05:00
parent d2e7953d13
commit 5c3d6320de
No known key found for this signature in database
GPG key ID: DB2E204C989251F7
3 changed files with 19 additions and 34 deletions

View file

@ -364,8 +364,8 @@ pub fn parse_failure_msg(tok: Token) -> String {
/// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison) /// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
fn token_name_eq(t1: &Token, t2: &Token) -> bool { fn token_name_eq(t1: &Token, t2: &Token) -> bool {
if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) { if let (Some((id1, is_raw1)), Some((id2, is_raw2))) = (t1.ident(), t2.ident()) {
id1.name == id2.name && t1.is_raw_ident() == t2.is_raw_ident() id1.name == id2.name && is_raw1 == is_raw2
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) { } else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
id1.name == id2.name id1.name == id2.name
} else { } else {

View file

@ -200,7 +200,7 @@ pub fn parse(
let span = match trees.next() { let span = match trees.next() {
Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() { Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() {
Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() { Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() {
Some(kind) => { Some((kind, _)) => {
let span = end_sp.with_lo(start_sp.lo()); let span = end_sp.with_lo(start_sp.lo());
result.push(TokenTree::MetaVarDecl(span, ident, kind)); result.push(TokenTree::MetaVarDecl(span, ident, kind));
continue; continue;
@ -289,7 +289,7 @@ where
// `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special // `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special
// metavariable that names the crate of the invokation. // metavariable that names the crate of the invokation.
Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => { Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => {
let ident = token.ident().unwrap(); let (ident, _) = token.ident().unwrap();
let span = ident_span.with_lo(span.lo()); let span = ident_span.with_lo(span.lo());
if ident.name == keywords::Crate.name() { if ident.name == keywords::Crate.name() {
let ident = ast::Ident { let ident = ast::Ident {

View file

@ -310,32 +310,17 @@ impl Token {
} }
} }
fn ident_common(&self, allow_raw: bool) -> Option<ast::Ident> { pub fn ident(&self) -> Option<(ast::Ident, bool)> {
match *self { match *self {
Ident(ident, is_raw) if !is_raw || allow_raw => Some(ident), Ident(ident, is_raw) => Some((ident, is_raw)),
Interpolated(ref nt) => match nt.0 { Interpolated(ref nt) => match nt.0 {
NtIdent(ident, is_raw) if !is_raw || allow_raw => Some(ident.node), NtIdent(ident, is_raw) => Some((ident.node, is_raw)),
_ => None, _ => None,
}, },
_ => None, _ => None,
} }
} }
pub fn nonraw_ident(&self) -> Option<ast::Ident> {
self.ident_common(false)
}
pub fn is_raw_ident(&self) -> bool {
match *self {
Ident(_, true) => true,
_ => false,
}
}
pub fn ident(&self) -> Option<ast::Ident> {
self.ident_common(true)
}
/// Returns `true` if the token is an identifier. /// Returns `true` if the token is an identifier.
pub fn is_ident(&self) -> bool { pub fn is_ident(&self) -> bool {
self.ident().is_some() self.ident().is_some()
@ -404,37 +389,37 @@ impl Token {
/// Returns `true` if the token is a given keyword, `kw`. /// Returns `true` if the token is a given keyword, `kw`.
pub fn is_keyword(&self, kw: keywords::Keyword) -> bool { pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
self.nonraw_ident().map(|ident| ident.name == kw.name()).unwrap_or(false) self.ident().map(|(ident, is_raw)| ident.name == kw.name() && !is_raw).unwrap_or(false)
} }
pub fn is_path_segment_keyword(&self) -> bool { pub fn is_path_segment_keyword(&self) -> bool {
match self.nonraw_ident() { match self.ident() {
Some(id) => is_path_segment_keyword(id), Some((id, false)) => is_path_segment_keyword(id),
None => false, _ => false,
} }
} }
// Returns true for reserved identifiers used internally for elided lifetimes, // Returns true for reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc. // unnamed method parameters, crate root module, error recovery etc.
pub fn is_special_ident(&self) -> bool { pub fn is_special_ident(&self) -> bool {
match self.nonraw_ident() { match self.ident() {
Some(id) => is_special_ident(id), Some((id, false)) => is_special_ident(id),
_ => false, _ => false,
} }
} }
/// Returns `true` if the token is a keyword used in the language. /// Returns `true` if the token is a keyword used in the language.
pub fn is_used_keyword(&self) -> bool { pub fn is_used_keyword(&self) -> bool {
match self.nonraw_ident() { match self.ident() {
Some(id) => is_used_keyword(id), Some((id, false)) => is_used_keyword(id),
_ => false, _ => false,
} }
} }
/// Returns `true` if the token is a keyword reserved for possible future use. /// Returns `true` if the token is a keyword reserved for possible future use.
pub fn is_unused_keyword(&self) -> bool { pub fn is_unused_keyword(&self) -> bool {
match self.nonraw_ident() { match self.ident() {
Some(id) => is_unused_keyword(id), Some((id, false)) => is_unused_keyword(id),
_ => false, _ => false,
} }
} }
@ -507,8 +492,8 @@ impl Token {
/// Returns `true` if the token is either a special identifier or a keyword. /// Returns `true` if the token is either a special identifier or a keyword.
pub fn is_reserved_ident(&self) -> bool { pub fn is_reserved_ident(&self) -> bool {
match self.nonraw_ident() { match self.ident() {
Some(id) => is_reserved_ident(id), Some((id, false)) => is_reserved_ident(id),
_ => false, _ => false,
} }
} }