mirror of
https://gitee.com/openharmony/third_party_rust_quote
synced 2024-11-27 01:11:38 +00:00
Merge pull request #196 from dtolnay/boolliteral
Fix boolean as $literal
This commit is contained in:
commit
68bf5916a0
@ -279,14 +279,24 @@ pub fn push_lifetime_spanned(tokens: &mut TokenStream, span: Span, lifetime: &st
|
||||
}
|
||||
|
||||
pub fn push_literal(tokens: &mut TokenStream, repr: &str) {
|
||||
let literal: Literal = repr.parse().expect("invalid literal");
|
||||
tokens.extend(iter::once(TokenTree::Literal(literal)));
|
||||
// Macro_rules's $literal matcher also matches `true`, `-true`, `false`,
|
||||
// `-false` which are not considered valid values for a proc_macro::Literal.
|
||||
if repr.ends_with('e') {
|
||||
parse(tokens, repr);
|
||||
} else {
|
||||
let literal: Literal = repr.parse().expect("invalid literal");
|
||||
tokens.extend(iter::once(TokenTree::Literal(literal)));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_literal_spanned(tokens: &mut TokenStream, span: Span, repr: &str) {
|
||||
let mut literal: Literal = repr.parse().expect("invalid literal");
|
||||
literal.set_span(span);
|
||||
tokens.extend(iter::once(TokenTree::Literal(literal)));
|
||||
if repr.ends_with('e') {
|
||||
parse_spanned(tokens, span, repr);
|
||||
} else {
|
||||
let mut literal: Literal = repr.parse().expect("invalid literal");
|
||||
literal.set_span(span);
|
||||
tokens.extend(iter::once(TokenTree::Literal(literal)));
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! push_punct {
|
||||
|
@ -229,6 +229,31 @@ fn test_string() {
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_interpolated_literal() {
|
||||
macro_rules! m {
|
||||
($literal:literal) => {
|
||||
quote!($literal)
|
||||
};
|
||||
}
|
||||
|
||||
let tokens = m!(1);
|
||||
let expected = "1";
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
|
||||
let tokens = m!(-1);
|
||||
let expected = "- 1";
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
|
||||
let tokens = m!(true);
|
||||
let expected = "true";
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
|
||||
let tokens = m!(-true);
|
||||
let expected = "- true";
|
||||
assert_eq!(expected, tokens.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ident() {
|
||||
let foo = Ident::new("Foo", Span::call_site());
|
||||
|
Loading…
Reference in New Issue
Block a user