Return a is_raw parameter from Token::ident rather than having separate methods.
This commit is contained in:
parent
d2e7953d13
commit
5c3d6320de
3 changed files with 19 additions and 34 deletions
|
@ -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)
|
||||
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
|
||||
if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) {
|
||||
id1.name == id2.name && t1.is_raw_ident() == t2.is_raw_ident()
|
||||
if let (Some((id1, is_raw1)), Some((id2, is_raw2))) = (t1.ident(), t2.ident()) {
|
||||
id1.name == id2.name && is_raw1 == is_raw2
|
||||
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
|
||||
id1.name == id2.name
|
||||
} else {
|
||||
|
|
|
@ -200,7 +200,7 @@ pub fn parse(
|
|||
let span = 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(kind) => {
|
||||
Some((kind, _)) => {
|
||||
let span = end_sp.with_lo(start_sp.lo());
|
||||
result.push(TokenTree::MetaVarDecl(span, ident, kind));
|
||||
continue;
|
||||
|
@ -289,7 +289,7 @@ where
|
|||
// `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special
|
||||
// metavariable that names the crate of the invokation.
|
||||
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());
|
||||
if ident.name == keywords::Crate.name() {
|
||||
let ident = ast::Ident {
|
||||
|
|
|
@ -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 {
|
||||
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 {
|
||||
NtIdent(ident, is_raw) if !is_raw || allow_raw => Some(ident.node),
|
||||
NtIdent(ident, is_raw) => Some((ident.node, is_raw)),
|
||||
_ => 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.
|
||||
pub fn is_ident(&self) -> bool {
|
||||
self.ident().is_some()
|
||||
|
@ -404,37 +389,37 @@ impl Token {
|
|||
|
||||
/// Returns `true` if the token is a given keyword, `kw`.
|
||||
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 {
|
||||
match self.nonraw_ident() {
|
||||
Some(id) => is_path_segment_keyword(id),
|
||||
None => false,
|
||||
match self.ident() {
|
||||
Some((id, false)) => is_path_segment_keyword(id),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true for reserved identifiers used internally for elided lifetimes,
|
||||
// unnamed method parameters, crate root module, error recovery etc.
|
||||
pub fn is_special_ident(&self) -> bool {
|
||||
match self.nonraw_ident() {
|
||||
Some(id) => is_special_ident(id),
|
||||
match self.ident() {
|
||||
Some((id, false)) => is_special_ident(id),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the token is a keyword used in the language.
|
||||
pub fn is_used_keyword(&self) -> bool {
|
||||
match self.nonraw_ident() {
|
||||
Some(id) => is_used_keyword(id),
|
||||
match self.ident() {
|
||||
Some((id, false)) => is_used_keyword(id),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the token is a keyword reserved for possible future use.
|
||||
pub fn is_unused_keyword(&self) -> bool {
|
||||
match self.nonraw_ident() {
|
||||
Some(id) => is_unused_keyword(id),
|
||||
match self.ident() {
|
||||
Some((id, false)) => is_unused_keyword(id),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@ -507,8 +492,8 @@ impl Token {
|
|||
|
||||
/// Returns `true` if the token is either a special identifier or a keyword.
|
||||
pub fn is_reserved_ident(&self) -> bool {
|
||||
match self.nonraw_ident() {
|
||||
Some(id) => is_reserved_ident(id),
|
||||
match self.ident() {
|
||||
Some((id, false)) => is_reserved_ident(id),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue