Skip to content

Commit

Permalink
Parse tuples (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilehmann authored Dec 4, 2023
1 parent 269a4c9 commit 02b83dd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/flux-attrs/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,24 @@ pub enum Type {
Reference(TypeReference),
Constraint(TypeConstraint),
Array(TypeArray),
Tuple(TypeTuple),
}

#[derive(Debug)]
pub struct TypeTuple {
pub paren_token: token::Paren,
pub elems: Punctuated<Type, Token![,]>,
}

impl TypeTuple {
fn to_tokens_inner(&self, tokens: &mut TokenStream, mode: Mode) {
self.paren_token.surround(tokens, |tokens| {
for elem in self.elems.pairs() {
elem.value().to_tokens_inner(tokens, mode);
elem.punct().to_tokens(tokens);
}
});
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -1281,13 +1299,25 @@ impl Parse for Type {
} else {
parse_rty(input, BaseType::Slice(TypeSlice { bracket_token, ty }))?
}
} else if input.peek(token::Paren) {
input.parse().map(Type::Tuple)?
} else {
parse_rty(input, input.parse()?)?
};
Ok(ty)
}
}

impl Parse for TypeTuple {
fn parse(input: ParseStream) -> Result<Self> {
let content;
Ok(TypeTuple {
paren_token: parenthesized!(content in input),
elems: content.parse_terminated(Type::parse, Token![,])?,
})
}
}

fn parse_rty(input: ParseStream, bty: BaseType) -> Result<Type> {
let ty = if input.peek(token::Bracket) {
let content;
Expand Down Expand Up @@ -1901,6 +1931,7 @@ impl Type {
Type::Reference(ty_reference) => ty_reference.to_tokens_inner(tokens, mode),
Type::Constraint(ty_constraint) => ty_constraint.to_tokens_inner(tokens, mode),
Type::Array(ty_array) => ty_array.to_tokens_inner(tokens, mode),
Type::Tuple(tuple) => tuple.to_tokens_inner(tokens, mode),
}
}
}
Expand Down

0 comments on commit 02b83dd

Please sign in to comment.