Skip to content

Commit

Permalink
refactor: ast item
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 3, 2023
1 parent 43d0537 commit bc8f9ee
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 76 deletions.
5 changes: 4 additions & 1 deletion crates/sol-macro/src/ast/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{CustomType, Function, Item, SolIdent, Type, Udt};
use super::{
item::{Function, Udt},
CustomType, Item, SolIdent, Type,
};
use proc_macro2::{Ident, TokenStream as TokenStream2};
use quote::ToTokens;
use std::collections::HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{kw, Parameters, SolIdent};
use crate::utils::from_into_tuples;
use crate::{
ast::{kw, Parameters, SolIdent},
utils::from_into_tuples,
};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use std::fmt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{kw, FunctionAttributes, Parameters, SolIdent, SolTuple, Type};
use crate::utils::from_into_tuples;
use crate::{
ast::{kw, FunctionAttributes, Parameters, Returns, SolIdent, SolTuple, Type},
utils::from_into_tuples,
};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, ToTokens};
use std::fmt;
Expand All @@ -8,62 +10,9 @@ use syn::{
parenthesized,
parse::{Parse, ParseStream},
token::{Brace, Paren},
Attribute, Error, Result, Token,
Attribute, Result, Token,
};

#[derive(Clone, PartialEq, Eq)]
pub struct Returns {
returns_token: kw::returns,
paren_token: Paren,
pub returns: Parameters<Token![,]>,
}

impl fmt::Debug for Returns {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Returns").field(&self.returns).finish()
}
}

impl fmt::Display for Returns {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("returns (")?;
for (i, r) in self.returns.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
write!(f, "{r}")?;
}
f.write_str(")")
}
}

impl Parse for Returns {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let this = Self {
returns_token: input.parse()?,
paren_token: parenthesized!(content in input),
returns: content.parse()?,
};
if this.returns.is_empty() {
Err(Error::new(
this.paren_token.span.join(),
"expected at least one return type",
))
} else {
Ok(this)
}
}
}

impl Returns {
#[allow(dead_code)]
pub fn span(&self) -> Span {
let span = self.returns_token.span;
span.join(self.paren_token.span.join()).unwrap_or(span)
}
}

pub struct Function {
pub attrs: Vec<Attribute>,
_function_token: kw::function,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{kw, Error, Function, Struct, Type, Udt};
use super::{kw, Type};
use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens;
use syn::{
Expand All @@ -7,6 +7,18 @@ use syn::{
Attribute, Result, Token,
};

mod error;
pub use error::Error;

mod function;
pub use function::Function;

mod r#struct;
pub use r#struct::Struct;

mod udt;
pub use udt::Udt;

#[derive(Debug)]
pub enum Item {
Udt(Udt),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{Parameters, SolIdent, Type, VariableDeclaration};
use crate::utils::from_into_tuples;
use crate::{
ast::{Parameters, SolIdent, Type, VariableDeclaration},
utils::from_into_tuples,
};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use std::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{kw, SolIdent, Type};
use crate::ast::{kw, SolIdent, Type};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use std::{
Expand Down
15 changes: 3 additions & 12 deletions crates/sol-macro/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ pub use attribute::{
mod ident;
pub use ident::{SolIdent, SolPath};

mod error;
pub use error::Error;

mod file;
pub use file::File;

mod function;
pub use function::{Function, Returns};

mod item;
pub mod item;
pub use item::Item;

mod storage;
pub use storage::Storage;

mod r#struct;
pub use r#struct::Struct;

mod r#type;
pub use r#type::{CustomType, SolArray, SolTuple, Type};

mod udt;
pub use udt::Udt;
mod returns;
pub use returns::Returns;

mod variable;
pub use variable::{Parameters, VariableDeclaration};
Expand Down
62 changes: 62 additions & 0 deletions crates/sol-macro/src/ast/returns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::ast::{kw, Parameters};
use proc_macro2::Span;
use std::fmt;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
token::Paren,
Error, Result, Token,
};

#[derive(Clone, PartialEq, Eq)]
pub struct Returns {
returns_token: kw::returns,
paren_token: Paren,
pub returns: Parameters<Token![,]>,
}

impl fmt::Debug for Returns {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Returns").field(&self.returns).finish()
}
}

impl fmt::Display for Returns {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("returns (")?;
for (i, r) in self.returns.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
write!(f, "{r}")?;
}
f.write_str(")")
}
}

impl Parse for Returns {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let this = Self {
returns_token: input.parse()?,
paren_token: parenthesized!(content in input),
returns: content.parse()?,
};
if this.returns.is_empty() {
Err(Error::new(
this.paren_token.span.join(),
"expected at least one return type",
))
} else {
Ok(this)
}
}
}

impl Returns {
#[allow(dead_code)]
pub fn span(&self) -> Span {
let span = self.returns_token.span;
span.join(self.paren_token.span.join()).unwrap_or(span)
}
}
5 changes: 4 additions & 1 deletion crates/sol-macro/src/ast/type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{kw, Struct, Udt};
use super::{
item::{Struct, Udt},
kw,
};
use proc_macro2::{Literal, Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use std::{
Expand Down

0 comments on commit bc8f9ee

Please sign in to comment.