Skip to content

Commit

Permalink
Create GroupSpan type to display groups correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
PoignardAzur committed Apr 11, 2022
1 parent 12cbe05 commit 9c4cafe
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::types::{
NamedStructFields, Struct, StructFields, TupleField, TupleStructFields, TyExpr, Union,
VisMarker, WhereClause, WhereClauseItem,
};
use crate::types_edition::GroupSpan;
use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, TokenStream, TokenTree};
use std::iter::Peekable;

Expand Down Expand Up @@ -37,7 +38,7 @@ fn consume_attributes(tokens: &mut TokenIter) -> Vec<Attribute> {
attributes.push(Attribute {
_hashbang: hashbang,
child_tokens: group.stream().into_iter().collect(),
_braces: group,
_braces: GroupSpan::new(&group),
});
}

Expand Down Expand Up @@ -398,7 +399,7 @@ fn parse_tuple_fields(token_group: Group) -> TupleStructFields {

TupleStructFields {
fields,
tk_parens: token_group,
tk_parens: GroupSpan::new(&token_group),
}
}

Expand Down Expand Up @@ -441,7 +442,7 @@ fn parse_named_fields(token_group: Group) -> NamedStructFields {

NamedStructFields {
fields,
tk_braces: token_group,
tk_braces: GroupSpan::new(&token_group),
}
}

Expand Down Expand Up @@ -734,7 +735,7 @@ pub fn parse_declaration(tokens: TokenStream) -> Declaration {
name: enum_name,
generic_params,
where_clause,
tk_braces: group,
tk_braces: GroupSpan::new(&group),
variants: enum_variants,
})
}
Expand All @@ -746,9 +747,9 @@ pub fn parse_declaration(tokens: TokenStream) -> Declaration {
let generic_params = consume_generic_params(&mut tokens);
let where_clause = consume_where_clause(&mut tokens);

let (group, union_fields) = match tokens.next().unwrap() {
let union_fields = match tokens.next().unwrap() {
TokenTree::Group(group) if group.delimiter() == Delimiter::Brace => {
(group.clone(), parse_named_fields(group.clone()))
parse_named_fields(group)
}
token => panic!("cannot parse union: unexpected token {:?}", token),
};
Expand All @@ -760,7 +761,6 @@ pub fn parse_declaration(tokens: TokenStream) -> Declaration {
name: union_name,
generic_params,
where_clause,
tk_braces: group,
fields: union_fields,
})
}
Expand Down
58 changes: 47 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(missing_docs)]

use proc_macro2::{Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::{ToTokens, TokenStreamExt as _};

use crate::Punctuated;
Expand Down Expand Up @@ -74,13 +74,13 @@ pub enum StructFields {
#[derive(Clone)]
pub struct TupleStructFields {
pub fields: Punctuated<TupleField>,
pub tk_parens: Group,
pub tk_parens: GroupSpan,
}

#[derive(Clone)]
pub struct NamedStructFields {
pub fields: Punctuated<NamedField>,
pub tk_braces: Group,
pub tk_braces: GroupSpan,
}

/// Declaration of an enum.
Expand All @@ -100,7 +100,7 @@ pub struct Enum {
pub name: Ident,
pub generic_params: Option<GenericParams>,
pub where_clause: Option<WhereClause>,
pub tk_braces: Group,
pub tk_braces: GroupSpan,
pub variants: Punctuated<EnumVariant>,
}

Expand Down Expand Up @@ -137,7 +137,6 @@ pub struct Union {
pub name: Ident,
pub generic_params: Option<GenericParams>,
pub where_clause: Option<WhereClause>,
pub tk_braces: Group,
pub fields: NamedStructFields,
}

Expand Down Expand Up @@ -238,7 +237,7 @@ pub struct NamedField {
#[derive(Clone)]
pub struct Attribute {
pub _hashbang: Punct,
pub _braces: Group,
pub _braces: GroupSpan,
pub child_tokens: Vec<TokenTree>,
}

Expand Down Expand Up @@ -349,6 +348,12 @@ pub struct EnumDiscriminant {
pub expression: Expression,
}

#[derive(Clone)]
pub struct GroupSpan {
pub delimiter: Delimiter,
pub span: Span,
}

// --- Debug impls ---

struct TokenRef<'a>(&'a TokenTree);
Expand Down Expand Up @@ -492,8 +497,29 @@ impl std::fmt::Debug for TyExpr {
}
}

impl std::fmt::Debug for GroupSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.delimiter {
Delimiter::Parenthesis => f.write_str("()"),
Delimiter::Brace => f.write_str("{}"),
Delimiter::Bracket => f.write_str("[]"),
Delimiter::None => f.write_str("Ø"),
}
}
}

// --- ToTokens impls ---

impl GroupSpan {
fn quote_with(&self, tokens: &mut TokenStream, f: impl FnOnce(&mut TokenStream)) {
let mut inner = TokenStream::new();
f(&mut inner);
let mut g = Group::new(self.delimiter, inner);
g.set_span(self.span);
tokens.append(g);
}
}

impl ToTokens for Expression {
fn to_tokens(&self, tokens: &mut TokenStream) {
for token in &self.tokens {
Expand Down Expand Up @@ -546,13 +572,17 @@ impl ToTokens for StructFields {

impl ToTokens for TupleStructFields {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_parens.to_tokens(tokens);
self.tk_parens.quote_with(tokens, |tokens| {
self.fields.to_tokens(tokens);
});
}
}

impl ToTokens for NamedStructFields {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_braces.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
self.fields.to_tokens(tokens);
});
}
}

Expand All @@ -566,7 +596,9 @@ impl ToTokens for Enum {
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_braces.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
self.variants.to_tokens(tokens);
});
}
}

Expand All @@ -592,7 +624,7 @@ impl ToTokens for Union {
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_braces.to_tokens(tokens);
self.fields.to_tokens(tokens);
}
}

Expand Down Expand Up @@ -660,7 +692,11 @@ impl ToTokens for NamedField {
impl ToTokens for Attribute {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self._hashbang.clone());
tokens.append(self._braces.clone());
self._braces.quote_with(tokens, |tokens| {
for token in &self.child_tokens {
tokens.append(token.clone())
}
});
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/types_edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

pub use crate::types::{
Attribute, Declaration, Enum, EnumDiscriminant, EnumVariant, Function, GenericBound,
GenericParam, GenericParams, InlineGenericArgs, NamedField, Struct, StructFields, TupleField,
TyExpr, Union, VisMarker, WhereClause, WhereClauseItem,
GenericParam, GenericParams, GroupSpan, InlineGenericArgs, NamedField, Struct, StructFields,
TupleField, TyExpr, Union, VisMarker, WhereClause, WhereClauseItem,
};
use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use proc_macro2::{Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

impl Declaration {
pub fn generic_params(&self) -> Option<&GenericParams> {
Expand Down Expand Up @@ -498,3 +498,13 @@ impl WhereClauseItem {
}
}
}

impl GroupSpan {
/// Create from proc_macro2 Group.
pub fn new(group: &Group) -> Self {
Self {
span: group.span(),
delimiter: group.delimiter(),
}
}
}

0 comments on commit 9c4cafe

Please sign in to comment.