Simplify with Symbol/Token::is_book_lit.

This commit is contained in:
Mazdak Farrokhzad
2019-08-27 10:21:41 +02:00
parent 5cc1559c60
commit e49b9581ba
4 changed files with 15 additions and 7 deletions

View File

@@ -104,7 +104,7 @@ impl LitKind {
Ok(match kind {
token::Bool => {
assert!(symbol == kw::True || symbol == kw::False);
assert!(symbol.is_bool_lit());
LitKind::Bool(symbol == kw::True)
}
token::Byte => return unescape_byte(&symbol.as_str())
@@ -261,7 +261,7 @@ impl Lit {
/// Converts arbitrary token into an AST literal.
crate fn from_token(token: &Token) -> Result<Lit, LitError> {
let lit = match token.kind {
token::Ident(name, false) if name == kw::True || name == kw::False =>
token::Ident(name, false) if name.is_bool_lit() =>
token::Lit::new(token::Bool, name, None),
token::Literal(lit) =>
lit,

View File

@@ -423,7 +423,7 @@ impl<'a> Parser<'a> {
// FIXME(const_generics): to distinguish between idents for types and consts,
// we should introduce a GenericArg::Ident in the AST and distinguish when
// lowering to the HIR. For now, idents for const args are not permitted.
if self.token.is_keyword(kw::True) || self.token.is_keyword(kw::False) {
if self.token.is_bool_lit() {
self.parse_literal_maybe_minus()?
} else {
return Err(

View File

@@ -417,10 +417,8 @@ impl Token {
/// for example a '-42', or one of the boolean idents).
crate fn can_begin_literal_or_bool(&self) -> bool {
match self.kind {
Literal(..) => true,
BinOp(Minus) => true,
Ident(name, false) if name == kw::True => true,
Ident(name, false) if name == kw::False => true,
Literal(..) | BinOp(Minus) => true,
Ident(name, false) if name.is_bool_lit() => true,
Interpolated(ref nt) => match **nt {
NtLiteral(..) => true,
_ => false,
@@ -537,6 +535,11 @@ impl Token {
self.is_non_raw_ident_where(ast::Ident::is_reserved)
}
/// Returns `true` if the token is the identifier `true` or `false`.
crate fn is_bool_lit(&self) -> bool {
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
}
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
fn is_non_raw_ident_where(&self, pred: impl FnOnce(ast::Ident) -> bool) -> bool {
match self.ident() {