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

refactor: sol! AST and macro expansion #61

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::{kw, Modifier, Mutability, Override, Visibility};
use super::{kw, Modifier, Mutability, Override, Visibility};
use proc_macro2::Span;
use std::{
collections::HashSet,
Expand All @@ -14,11 +14,13 @@ use syn::{
Error, Ident, Result, Token,
};

/// A list of unique function attributes. Used in
/// [Function][crate::ast::item::Function].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionAttributes(pub HashSet<FunctionAttribute>);

impl Parse for FunctionAttributes {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut attributes = HashSet::<FunctionAttribute>::new();
while !(input.is_empty()
|| input.peek(kw::returns)
Expand All @@ -37,6 +39,7 @@ impl Parse for FunctionAttributes {
}
}

/// A function attribute.
#[derive(Clone)]
pub enum FunctionAttribute {
Visibility(Visibility),
Expand All @@ -62,7 +65,10 @@ impl fmt::Debug for FunctionAttribute {

impl PartialEq for FunctionAttribute {
fn eq(&self, other: &Self) -> bool {
mem::discriminant(self) == mem::discriminant(other)
match (self, other) {
(Self::Modifier(a), Self::Modifier(b)) => a == b,
_ => mem::discriminant(self) == mem::discriminant(other),
}
}
}

Expand All @@ -78,7 +84,7 @@ impl Hash for FunctionAttribute {
}

impl Parse for FunctionAttribute {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(kw::external)
|| lookahead.peek(kw::public)
Expand Down Expand Up @@ -120,4 +126,15 @@ impl FunctionAttribute {
Self::Modifier(m) => m.span(),
}
}

pub fn set_span(&mut self, span: Span) {
match self {
Self::Visibility(v) => v.set_span(span),
Self::Mutability(m) => m.set_span(span),
Self::Virtual(v) => v.span = span,
Self::Override(o) => o.set_span(span),
Self::Immutable(i) => i.span = span,
Self::Modifier(m) => m.set_span(span),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ident::SolPath, kw};
use super::{kw, SolPath};
use proc_macro2::{Span, TokenStream};
use std::{
fmt,
Expand All @@ -19,6 +19,7 @@ pub use function::{FunctionAttribute, FunctionAttributes};
mod variable;
pub use variable::{VariableAttribute, VariableAttributes};

/// A visibility attribute.
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Visibility {
External(kw::external),
Expand All @@ -40,7 +41,7 @@ impl fmt::Display for Visibility {
}

impl Parse for Visibility {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(kw::external) {
Ok(Self::External(input.parse()?))
Expand All @@ -66,6 +67,15 @@ impl Visibility {
}
}

pub fn set_span(&mut self, span: Span) {
match self {
Self::External(kw) => kw.span = span,
Self::Public(kw) => kw.span = span,
Self::Internal(kw) => kw.span = span,
Self::Private(kw) => kw.span = span,
}
}

pub const fn as_debug_str(&self) -> &'static str {
match self {
Self::External(_) => "External",
Expand Down Expand Up @@ -106,7 +116,7 @@ impl fmt::Display for Mutability {
}

impl Parse for Mutability {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(kw::pure) {
Ok(Self::Pure(input.parse()?))
Expand All @@ -132,6 +142,15 @@ impl Mutability {
}
}

pub fn set_span(&mut self, span: Span) {
match self {
Self::Pure(kw) => kw.span = span,
Self::View(kw) => kw.span = span,
Self::Constant(kw) => kw.span = span,
Self::Payable(kw) => kw.span = span,
}
}

pub const fn as_debug_str(&self) -> &'static str {
match self {
Self::Pure(_) => "Pure",
Expand All @@ -151,11 +170,12 @@ impl Mutability {
}
}

/// The `override` attribute.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Override {
override_token: kw::Override,
paren_token: Option<Paren>,
paths: Punctuated<SolPath, Token![,]>,
pub override_token: kw::Override,
pub paren_token: Option<Paren>,
pub paths: Punctuated<SolPath, Token![,]>,
}

impl fmt::Debug for Override {
Expand All @@ -182,7 +202,7 @@ impl fmt::Display for Override {
}

impl Parse for Override {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let override_token = input.parse()?;
let this = if input.peek(Paren) {
let content;
Expand All @@ -205,19 +225,26 @@ impl Parse for Override {
impl Override {
pub fn span(&self) -> Span {
let span = self.override_token.span;
match &self.paren_token {
Some(paren_token) => span.join(paren_token.span.join()).unwrap_or(span),
None => span,
self.paren_token
.and_then(|paren_token| span.join(paren_token.span.join()))
.unwrap_or(span)
}

pub fn set_span(&mut self, span: Span) {
self.override_token.span = span;
if let Some(paren_token) = &mut self.paren_token {
*paren_token = Paren(span);
}
}
}

/// A modifier invocation, or an inheritance specifier.
#[derive(Clone)]
pub struct Modifier {
name: SolPath,
paren_token: Paren,
pub name: SolPath,
pub paren_token: Paren,
// TODO: Expr
arguments: Punctuated<TokenStream, Token![,]>,
pub arguments: Punctuated<TokenStream, Token![,]>,
}

impl PartialEq for Modifier {
Expand All @@ -244,7 +271,7 @@ impl fmt::Debug for Modifier {
}

impl Parse for Modifier {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let content;
Ok(Self {
name: input.parse()?,
Expand All @@ -259,4 +286,9 @@ impl Modifier {
let span = self.name.span();
span.join(self.paren_token.span.join()).unwrap_or(span)
}

pub fn set_span(&mut self, span: Span) {
self.name.set_span(span);
self.paren_token = Paren(span);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::{kw, Override, Visibility};
use super::{kw, Override, Visibility};
use proc_macro2::Span;
use std::{
collections::HashSet,
Expand All @@ -11,10 +11,11 @@ use syn::{
Error, Result,
};

/// A list of unique variable attributes.
pub struct VariableAttributes(pub HashSet<VariableAttribute>);

impl Parse for VariableAttributes {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut attributes = HashSet::new();
while let Ok(attribute) = input.parse::<VariableAttribute>() {
let error = |prev: &VariableAttribute| {
Expand Down Expand Up @@ -51,6 +52,7 @@ impl Parse for VariableAttributes {
}
}

/// A variable attribute.
#[derive(Clone)]
pub enum VariableAttribute {
Visibility(Visibility),
Expand All @@ -74,7 +76,7 @@ impl Hash for VariableAttribute {
}

impl Parse for VariableAttribute {
fn parse(input: ParseStream) -> Result<Self> {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(kw::external)
|| lookahead.peek(kw::public)
Expand Down Expand Up @@ -103,4 +105,13 @@ impl VariableAttribute {
Self::Immutable(i) => i.span(),
}
}

pub fn set_span(&mut self, span: Span) {
match self {
Self::Visibility(v) => v.set_span(span),
Self::Constant(c) => c.span = span,
Self::Override(o) => o.set_span(span),
Self::Immutable(i) => i.span = span,
}
}
}
Loading