Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(syn-solidity): add statements and expressions #199

Merged
merged 27 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/sol-macro/src/expand/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use super::ExpCtxt;
use crate::expand::generate_name;
use ast::{EventParameter, Item, Parameters, Type, TypeArray, VariableDeclaration};
use ast::{
r#type::{Type, TypeArray},
EventParameter, Item, Parameters, VariableDeclaration,
};
use proc_macro2::{Literal, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use std::{fmt, num::NonZeroU16};
Expand Down
4 changes: 2 additions & 2 deletions crates/syn-solidity/src/attribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use syn::{
};

mod function;
pub use function::{FunctionAttribute, FunctionAttributes};
pub use function::*;

mod variable;
pub use variable::{VariableAttribute, VariableAttributes};
pub use variable::*;

kw_enum! {
/// A storage location.
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/ident/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::{
};

mod path;
pub use path::SolPath;
pub use path::*;

/// A Solidity identifier.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/item/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kw, Parameters, SolIdent, Type};
use crate::{kw, r#type::Type, Parameters, SolIdent};
use proc_macro2::Span;
use std::fmt;
use syn::{
Expand Down
4 changes: 3 additions & 1 deletion crates/syn-solidity/src/item/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{kw, utils::DebugPunctuated, ParameterList, SolIdent, Type, VariableDeclaration};
use crate::{
kw, r#type::Type, utils::DebugPunctuated, ParameterList, SolIdent, VariableDeclaration,
};
use proc_macro2::Span;
use std::fmt;
use syn::{
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/item/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kw, Block, FunctionAttributes, Parameters, SolIdent, Type};
use crate::{kw, r#type::Type, Block, FunctionAttributes, Parameters, SolIdent};
use proc_macro2::Span;
use std::{
fmt,
Expand Down
24 changes: 11 additions & 13 deletions crates/syn-solidity/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,34 @@ use syn::{
};

mod contract;
pub use contract::ItemContract;
pub use contract::*;

mod r#enum;
pub use r#enum::ItemEnum;
pub use r#enum::*;

mod error;
pub use error::ItemError;
pub use error::*;

mod event;
pub use event::{EventParameter, ItemEvent};
pub use event::*;

mod function;
pub use function::{ItemFunction, Returns};
pub use function::*;

mod import;
pub use import::{
ImportAlias, ImportAliases, ImportDirective, ImportGlob, ImportPath, ImportPlain,
};
pub use import::*;

mod pragma;
pub use pragma::{PragmaDirective, PragmaTokens};
pub use pragma::*;

mod r#struct;
pub use r#struct::ItemStruct;
pub use r#struct::*;

mod udt;
pub use udt::ItemUdt;
pub use udt::*;

mod using;
pub use using::{UserDefinableOperator, UsingDirective, UsingList, UsingListItem, UsingType};
pub use using::*;

/// An AST item. A more expanded version of a [Solidity source unit][ref].
///
Expand Down Expand Up @@ -104,7 +102,7 @@ impl Parse for Item {
input.parse().map(Self::Import)
} else if lookahead.peek(kw::using) {
input.parse().map(Self::Using)
} else if crate::Type::peek(&lookahead) {
} else if crate::r#type::Type::peek(&lookahead) {
input.parse().map(Self::Variable)
} else {
Err(lookahead.error())
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/item/struct.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Parameters, SolIdent, Type};
use crate::{r#type::Type, Parameters, SolIdent};
use proc_macro2::Span;
use std::{
fmt,
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/item/udt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kw, SolIdent, Type};
use crate::{kw, r#type::Type, SolIdent};
use proc_macro2::Span;
use std::{
fmt,
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/item/using.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kw, SolPath, Type};
use crate::{kw, r#type::Type, SolPath};
use proc_macro2::Span;
use syn::{
braced,
Expand Down
8 changes: 8 additions & 0 deletions crates/syn-solidity/src/kw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ custom_keywords!(

// Error
error,
panic,

// Event
event,
Expand Down Expand Up @@ -70,4 +71,11 @@ custom_keywords!(
// Other
is,
unicode,
catch,
delete,
unchecked,
new,
emit,
revert,
assembly,
);
43 changes: 19 additions & 24 deletions crates/syn-solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,36 @@ use syn::Result;
#[macro_use]
mod macros;

mod attribute;
pub use attribute::{
FunctionAttribute, FunctionAttributes, Modifier, Mutability, Override, Storage,
VariableAttribute, VariableAttributes, Visibility,
};
pub mod attribute;
pub mod literals;
pub use attribute::*;

mod file;
pub use file::File;
pub mod statments;
pub use statments::*;

mod ident;
pub use ident::{SolIdent, SolPath};
pub mod file;
pub use file::*;

mod item;
pub use item::{
EventParameter, ImportAlias, ImportAliases, ImportDirective, ImportGlob, ImportPath,
ImportPlain, Item, ItemContract, ItemEnum, ItemError, ItemEvent, ItemFunction, ItemStruct,
ItemUdt, PragmaDirective, PragmaTokens, Returns, UserDefinableOperator, UsingDirective,
UsingList, UsingListItem, UsingType,
};
pub mod ident;
pub use ident::*;

mod lit;
pub use lit::LitStr;
pub mod item;
pub use item::*;
pub mod lit;
pub use lit::*;

pub mod kw;

mod stmt;
pub use stmt::Block;
pub mod stmt;
pub use stmt::*;

mod r#type;
pub use r#type::{Type, TypeArray, TypeFunction, TypeMapping, TypeTuple};
pub mod r#type;
pub use r#type::Type;

pub(crate) mod utils;

mod variable;
pub use variable::{FieldList, ParameterList, Parameters, VariableDeclaration, VariableDefinition};
pub mod variable;
pub use variable::*;

#[cfg(feature = "visit")]
pub mod visit;
Expand Down
22 changes: 22 additions & 0 deletions crates/syn-solidity/src/literals/lits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use syn::{parse::Parse, Error, LitBool, LitInt, LitStr};

#[derive(Debug, Clone)]
pub enum Literals {
String(LitStr),
Number(LitInt),
Bool(LitBool),
}

impl Parse for Literals {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
if input.peek(LitStr) {
Ok(Self::String(input.parse()?))
} else if input.peek(LitInt) {
Ok(Self::Number(input.parse()?))
} else if input.peek(LitBool) {
Ok(Self::Bool(input.parse()?))
} else {
Err(Error::new(input.span(), "failed to parse literal"))
}
}
}
1 change: 1 addition & 0 deletions crates/syn-solidity/src/literals/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod lits;
23 changes: 23 additions & 0 deletions crates/syn-solidity/src/statments/assembly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use proc_macro2::TokenStream;
use syn::{braced, parse::Parse, token::Brace};

use crate::kw;

#[derive(Debug, Clone)]
pub struct Assembly {
kw: kw::assembly,
// stuff here
brace: Brace,
input: TokenStream,
}

impl Parse for Assembly {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
let content;
let kw = input.parse()?;
let brace = braced!(content in input);
let input = input.parse()?;

Ok(Self { input, kw, brace })
}
}
21 changes: 21 additions & 0 deletions crates/syn-solidity/src/statments/binop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::binops::Binop;
use syn::parse::Parse;

use crate::expr::Expr;

#[derive(Debug, Clone)]
pub struct BinopExpr {
pub left: Box<Expr>,
pub op: Binop,
pub right: Box<Expr>,
}

impl Parse for BinopExpr {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
Ok(Self {
left: Box::new(input.parse()?),
op: input.parse()?,
right: Box::new(input.parse()?),
})
}
}
101 changes: 101 additions & 0 deletions crates/syn-solidity/src/statments/binops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
pub mod pow;
pub mod ternary;

use crate::kw;

use self::pow::PowOps;

use super::binops::ternary::Ternary;
use syn::{parse::Parse, Error, Token};

#[derive(Debug, Clone)]
pub enum Binop {
Assign(Token![=]),
Add(Token![+]),
AddAssign(Token![+=]),
Minus(Token![-]),
MinusAssign(Token![-=]),
Mul(Token![*]),
MulAssign(Token![*=]),
Not(Token![!]),
BitNot(Token![~]),
Div(Token![/]),
DivAssign(Token![/=]),
Mod(Token![%]),
ModAssign(Token![%=]),
BitAnd(Token![&]),
AndAssign(Token![&=]),
BitXor(Token![^]),
XorAssign(Token![^=]),
BitOr(Token![|]),
BitOrAssign(Token![|=]),
Shl(Token![<<]),
ShlAssign(Token![<<=]),
Shr(Token![>>]),
ShrAssign(Token![>>=]),
Equality(Token![==]),
And(Token![&&]),
Or(Token![||]),
Ternary(Ternary),
// don't have in rust but swag
Exponent(PowOps),
Delete(kw::delete),
}

impl Parse for Binop {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
if input.peek(Token![=]) {
Ok(Self::Assign(input.parse()?))
} else if input.peek(Token![~]) {
Ok(Self::BitNot(input.parse()?))
} else if input.peek(Token![==]) {
Ok(Self::Equality(input.parse()?))
} else if input.peek(Token![+]) {
Ok(Self::Add(input.parse()?))
} else if input.peek(Token![+=]) {
Ok(Self::AddAssign(input.parse()?))
} else if input.peek(Token![-]) {
Ok(Self::Minus(input.parse()?))
} else if input.peek(Token![-=]) {
Ok(Self::MinusAssign(input.parse()?))
} else if input.peek(Token![*]) {
Ok(Self::Mul(input.parse()?))
} else if input.peek(Token![*=]) {
Ok(Self::MulAssign(input.parse()?))
} else if input.peek(Token![/]) {
Ok(Self::Div(input.parse()?))
} else if input.peek(Token![/=]) {
Ok(Self::DivAssign(input.parse()?))
} else if input.peek(Token![%]) {
Ok(Self::Mod(input.parse()?))
} else if input.peek(Token![%=]) {
Ok(Self::ModAssign(input.parse()?))
} else if input.peek(Token![&]) {
Ok(Self::BitAnd(input.parse()?))
} else if input.peek(Token![&=]) {
Ok(Self::AndAssign(input.parse()?))
} else if input.peek(Token![^]) {
Ok(Self::BitXor(input.parse()?))
} else if input.peek(Token![^=]) {
Ok(Self::XorAssign(input.parse()?))
} else if input.peek(Token![|]) {
Ok(Self::BitOr(input.parse()?))
} else if input.peek(Token![|=]) {
Ok(Self::BitOrAssign(input.parse()?))
} else if input.peek(Token![<<]) {
Ok(Self::Shl(input.parse()?))
} else if input.peek(Token![<<=]) {
Ok(Self::ShlAssign(input.parse()?))
} else if input.peek(Token![>>]) {
Ok(Self::Shr(input.parse()?))
} else if input.peek(Token![>>=]) {
Ok(Self::ShrAssign(input.parse()?))
} else if input.peek(Token![&&]) {
Ok(Self::And(input.parse()?))
} else if input.peek(Token![||]) {
Ok(Self::Or(input.parse()?))
} else {
Err(Error::new(input.span(), "failed to parse binop"))
}
}
}
17 changes: 17 additions & 0 deletions crates/syn-solidity/src/statments/binops/pow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use syn::{parse::Parse, Token};

/// def not right way todo this but we ballin
#[derive(Debug, Clone)]
pub struct PowOps {
pub star0: Token!(*),
pub star1: Token!(*),
}

impl Parse for PowOps {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
Ok(Self {
star0: input.parse()?,
star1: input.parse()?,
})
}
}
Loading