Skip to content

Commit 40a7779

Browse files
committed
Support .0.0 nested tuple index
1 parent f1ca210 commit 40a7779

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

impl/src/attr.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
1+
use proc_macro2::{Delimiter, Group, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
22
use quote::{format_ident, quote, ToTokens};
33
use std::collections::BTreeSet as Set;
44
use syn::parse::discouraged::Speculative;
55
use syn::parse::ParseStream;
66
use syn::{
7-
braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr, Meta,
8-
Result, Token,
7+
braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitFloat, LitInt,
8+
LitStr, Meta, Result, Token,
99
};
1010

1111
pub struct Attrs<'a> {
@@ -145,14 +145,42 @@ fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStr
145145
input.parse::<Token![.]>()?;
146146
begin_expr = false;
147147
continue;
148-
}
149-
if input.peek2(LitInt) {
148+
} else if input.peek2(LitInt) {
150149
input.parse::<Token![.]>()?;
151150
let int: Index = input.parse()?;
152-
let ident = format_ident!("_{}", int.index, span = int.span);
153-
tokens.push(TokenTree::Ident(ident));
151+
tokens.push({
152+
let ident = format_ident!("_{}", int.index, span = int.span);
153+
TokenTree::Ident(ident)
154+
});
154155
begin_expr = false;
155156
continue;
157+
} else if input.peek2(LitFloat) {
158+
let ahead = input.fork();
159+
ahead.parse::<Token![.]>()?;
160+
let float: LitFloat = ahead.parse()?;
161+
let repr = float.to_string();
162+
let mut indices = repr.split('.').map(syn::parse_str::<Index>);
163+
if let (Some(Ok(first)), Some(Ok(second)), None) =
164+
(indices.next(), indices.next(), indices.next())
165+
{
166+
input.advance_to(&ahead);
167+
tokens.push({
168+
let ident = format_ident!("_{}", first, span = float.span());
169+
TokenTree::Ident(ident)
170+
});
171+
tokens.push({
172+
let mut punct = Punct::new('.', Spacing::Alone);
173+
punct.set_span(float.span());
174+
TokenTree::Punct(punct)
175+
});
176+
tokens.push({
177+
let mut literal = Literal::u32_unsuffixed(second.index);
178+
literal.set_span(float.span());
179+
TokenTree::Literal(literal)
180+
});
181+
begin_expr = false;
182+
continue;
183+
}
156184
}
157185
}
158186

0 commit comments

Comments
 (0)