macro: use parse_hex_pattern for binnvec
This commit is contained in:
parent
1444dd6977
commit
d5910d8350
@ -7,68 +7,6 @@ macro_rules! forest {
|
|||||||
($($x:expr),+) => {[$( TokenTree::from($x.clone()), )+]};
|
($($x:expr),+) => {[$( TokenTree::from($x.clone()), )+]};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_hex_values(items: TokenStream) -> TokenStream {
|
|
||||||
let mut new_tokens: Vec<TokenTree> = vec![];
|
|
||||||
let comma: Punct = Punct::new(',', Spacing::Alone);
|
|
||||||
let zero = Literal::u8_suffixed(0);
|
|
||||||
let mut prev_separated: bool = true;
|
|
||||||
let mut prev_is_mask: bool = false;
|
|
||||||
|
|
||||||
for token in items {
|
|
||||||
match token {
|
|
||||||
TokenTree::Punct(ref x) if x.as_char() == ',' => {
|
|
||||||
new_tokens.push(token);
|
|
||||||
prev_separated = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
TokenTree::Punct(ref x) if x.as_char() == '?' => {
|
|
||||||
if prev_is_mask {
|
|
||||||
if !prev_separated {
|
|
||||||
new_tokens.push(comma.clone().into());
|
|
||||||
}
|
|
||||||
let mut val = zero.clone();
|
|
||||||
val.set_span(x.span());
|
|
||||||
new_tokens.push(zero.clone().into());
|
|
||||||
prev_is_mask = false;
|
|
||||||
prev_separated = false;
|
|
||||||
} else if !prev_is_mask {
|
|
||||||
prev_is_mask = true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
TokenTree::Ident(ref x) if x.to_string().len() == 2 => {
|
|
||||||
if !prev_separated {
|
|
||||||
new_tokens.push(comma.clone().into());
|
|
||||||
}
|
|
||||||
let lit_str = format!("0x{}u8", x);
|
|
||||||
let mut val: Literal = lit_str.as_str().parse().expect("parse literal from ident");
|
|
||||||
val.set_span(x.span());
|
|
||||||
new_tokens.push(val.into());
|
|
||||||
}
|
|
||||||
TokenTree::Literal(ref x) => {
|
|
||||||
if !prev_separated {
|
|
||||||
new_tokens.push(comma.clone().into());
|
|
||||||
}
|
|
||||||
|
|
||||||
let in_str = x.to_string();
|
|
||||||
if in_str.starts_with("0x") {
|
|
||||||
new_tokens.push(token);
|
|
||||||
} else {
|
|
||||||
let lit_str = format!("0x{}u8", in_str);
|
|
||||||
let mut val: Literal = lit_str.as_str().parse().expect("parse literal");
|
|
||||||
val.set_span(x.span());
|
|
||||||
new_tokens.push(val.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
prev_separated = false;
|
|
||||||
}
|
|
||||||
let mut new_stream = TokenStream::new();
|
|
||||||
new_stream.extend(new_tokens);
|
|
||||||
new_stream
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_hex_pattern(items: TokenStream) -> (TokenStream, TokenStream) {
|
fn parse_hex_pattern(items: TokenStream) -> (TokenStream, TokenStream) {
|
||||||
let mut vec_tokens: Vec<TokenTree> = vec![];
|
let mut vec_tokens: Vec<TokenTree> = vec![];
|
||||||
let mut mask_tokens: Vec<TokenTree> = vec![];
|
let mut mask_tokens: Vec<TokenTree> = vec![];
|
||||||
@ -157,7 +95,7 @@ fn parse_hex_pattern(items: TokenStream) -> (TokenStream, TokenStream) {
|
|||||||
|
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn binnvec(_item: TokenStream) -> TokenStream {
|
pub fn binnvec(_item: TokenStream) -> TokenStream {
|
||||||
let group_elements = parse_hex_values(_item);
|
let (group_elements, ..) = parse_hex_pattern(_item);
|
||||||
|
|
||||||
let group = Group::new(Delimiter::Bracket, group_elements);
|
let group = Group::new(Delimiter::Bracket, group_elements);
|
||||||
let mut result = TokenStream::new();
|
let mut result = TokenStream::new();
|
||||||
@ -194,13 +132,7 @@ pub fn binnpat(_item: TokenStream) -> TokenStream {
|
|||||||
let i_from = Ident::new("from", Span::call_site());
|
let i_from = Ident::new("from", Span::call_site());
|
||||||
|
|
||||||
let mut result = TokenStream::new();
|
let mut result = TokenStream::new();
|
||||||
result.extend(forest!(
|
result.extend(forest!(i_struct, sep_colon, sep_colon, i_from, param_group));
|
||||||
i_struct,
|
|
||||||
sep_colon,
|
|
||||||
sep_colon,
|
|
||||||
i_from,
|
|
||||||
param_group
|
|
||||||
));
|
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user