mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Convert bool to ident instead of literal in mbe
This commit is contained in:
parent
b949500126
commit
a1b5cf81eb
5 changed files with 47 additions and 14 deletions
|
@ -187,7 +187,11 @@ impl<'a> TtIter<'a> {
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
Separator::Literal(lhs) => match fork.expect_literal() {
|
Separator::Literal(lhs) => match fork.expect_literal() {
|
||||||
Ok(rhs) => rhs.text == lhs.text,
|
Ok(rhs) => match rhs {
|
||||||
|
tt::Leaf::Literal(rhs) => rhs.text == lhs.text,
|
||||||
|
tt::Leaf::Ident(rhs) => rhs.text == lhs.text,
|
||||||
|
tt::Leaf::Punct(_) => false,
|
||||||
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
Separator::Puncts(lhss) => lhss.iter().all(|lhs| match fork.expect_punct() {
|
Separator::Puncts(lhss) => lhss.iter().all(|lhs| match fork.expect_punct() {
|
||||||
|
|
|
@ -158,20 +158,17 @@ fn convert_literal(l: &tt::Literal) -> TtToken {
|
||||||
let kind = lex_single_syntax_kind(&l.text)
|
let kind = lex_single_syntax_kind(&l.text)
|
||||||
.map(|(kind, _error)| kind)
|
.map(|(kind, _error)| kind)
|
||||||
.filter(|kind| kind.is_literal())
|
.filter(|kind| kind.is_literal())
|
||||||
.unwrap_or_else(|| match l.text.as_ref() {
|
.unwrap_or_else(|| panic!("Fail to convert given literal {:#?}", &l));
|
||||||
"true" => T![true],
|
|
||||||
"false" => T![false],
|
|
||||||
_ => panic!("Fail to convert given literal {:#?}", &l),
|
|
||||||
});
|
|
||||||
|
|
||||||
TtToken { kind, is_joint_to_next: false, text: l.text.clone() }
|
TtToken { kind, is_joint_to_next: false, text: l.text.clone() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_ident(ident: &tt::Ident) -> TtToken {
|
fn convert_ident(ident: &tt::Ident) -> TtToken {
|
||||||
let kind = if ident.text.starts_with('\'') {
|
let kind = match ident.text.as_ref() {
|
||||||
LIFETIME
|
"true" => T![true],
|
||||||
} else {
|
"false" => T![false],
|
||||||
SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT)
|
i if i.starts_with('\'') => LIFETIME,
|
||||||
|
_ => SyntaxKind::from_keyword(ident.text.as_str()).unwrap_or(IDENT),
|
||||||
};
|
};
|
||||||
|
|
||||||
TtToken { kind, is_joint_to_next: false, text: ident.text.clone() }
|
TtToken { kind, is_joint_to_next: false, text: ident.text.clone() }
|
||||||
|
|
|
@ -376,7 +376,7 @@ trait TokenConvertor {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let leaf: tt::Leaf = match k {
|
let leaf: tt::Leaf = match k {
|
||||||
T![true] | T![false] => make_leaf!(Literal),
|
T![true] | T![false] => make_leaf!(Ident),
|
||||||
IDENT => make_leaf!(Ident),
|
IDENT => make_leaf!(Ident),
|
||||||
k if k.is_keyword() => make_leaf!(Ident),
|
k if k.is_keyword() => make_leaf!(Ident),
|
||||||
k if k.is_literal() => make_leaf!(Literal),
|
k if k.is_literal() => make_leaf!(Literal),
|
||||||
|
|
|
@ -1015,6 +1015,36 @@ fn test_literal() {
|
||||||
.assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#);
|
.assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_boolean_is_ident() {
|
||||||
|
parse_macro(
|
||||||
|
r#"
|
||||||
|
macro_rules! foo {
|
||||||
|
($lit0:literal, $lit1:literal) => { const VALUE: (bool,bool) = ($lit0,$lit1); };
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.assert_expand(
|
||||||
|
r#"foo!(true,false);"#,
|
||||||
|
r#"
|
||||||
|
SUBTREE $
|
||||||
|
IDENT const 14
|
||||||
|
IDENT VALUE 15
|
||||||
|
PUNCH : [alone] 16
|
||||||
|
SUBTREE () 17
|
||||||
|
IDENT bool 18
|
||||||
|
PUNCH , [alone] 19
|
||||||
|
IDENT bool 20
|
||||||
|
PUNCH = [alone] 21
|
||||||
|
SUBTREE () 22
|
||||||
|
IDENT true 29
|
||||||
|
PUNCH , [joint] 25
|
||||||
|
IDENT false 31
|
||||||
|
PUNCH ; [alone] 28
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vis() {
|
fn test_vis() {
|
||||||
parse_macro(
|
parse_macro(
|
||||||
|
|
|
@ -40,9 +40,11 @@ impl<'a> TtIter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expect_literal(&mut self) -> Result<&'a tt::Literal, ()> {
|
pub(crate) fn expect_literal(&mut self) -> Result<&'a tt::Leaf, ()> {
|
||||||
match self.expect_leaf()? {
|
let it = self.expect_leaf()?;
|
||||||
tt::Leaf::Literal(it) => Ok(it),
|
match it {
|
||||||
|
tt::Leaf::Literal(_) => Ok(it),
|
||||||
|
tt::Leaf::Ident(ident) if ident.text == "true" || ident.text == "false" => Ok(it),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue