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 dialect macros #378

Merged
merged 15 commits into from
Dec 5, 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
10 changes: 8 additions & 2 deletions macro/src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ pub fn generate_dialect(input: DialectInput) -> Result<TokenStream, Box<dyn std:
}

// spell-checker: disable-next-line
for path in input.includes().chain([&*llvm_config("--includedir")?]) {
let llvm_include_directory = llvm_config("--includedir")?;

for path in input
.include_directories()
.chain([llvm_include_directory.as_str()])
{
parser = parser.add_include_path(path);
}

Expand Down Expand Up @@ -61,7 +66,8 @@ fn generate_dialect_module(
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.filter(|operation| operation.dialect_name() == dialect_name)
.collect::<Vec<_>>();
.map(|operation| operation.to_tokens())
.collect::<Result<Vec<_>, _>>()?;

let doc = format!(
"`{name}` dialect.\n\n{}",
Expand Down
104 changes: 0 additions & 104 deletions macro/src/dialect/dialect.rs

This file was deleted.

11 changes: 7 additions & 4 deletions macro/src/dialect/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ impl From<FromUtf8Error> for Error {
pub enum OdsError {
ExpectedSuperClass(&'static str),
InvalidTrait,
UnexpectedSuperClass(&'static str),
}

impl Display for OdsError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Self::ExpectedSuperClass(class) => write!(
formatter,
"expected this record to be a subclass of {class}",
),
Self::ExpectedSuperClass(class) => {
write!(formatter, "record should be a sub-class of {class}",)
}
Self::InvalidTrait => write!(formatter, "record is not a supported trait"),
Self::UnexpectedSuperClass(class) => {
write!(formatter, "record should not be a sub-class of {class}",)
}
}
}
}
Expand Down
48 changes: 9 additions & 39 deletions macro/src/dialect/input.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use proc_macro2::Ident;
use quote::format_ident;
mod input_field;

use self::input_field::InputField;
use std::ops::Deref;
use syn::{bracketed, parse::Parse, punctuated::Punctuated, LitStr, Token};
use syn::{parse::Parse, punctuated::Punctuated, Token};

pub struct DialectInput {
name: String,
table_gen: Option<String>,
td_file: Option<String>,
includes: Vec<String>,
include_directories: Vec<String>,
}

impl DialectInput {
Expand All @@ -23,8 +24,8 @@ impl DialectInput {
self.td_file.as_deref()
}

pub fn includes(&self) -> impl Iterator<Item = &str> {
self.includes.iter().map(Deref::deref)
pub fn include_directories(&self) -> impl Iterator<Item = &str> {
self.include_directories.iter().map(Deref::deref)
}
}

Expand All @@ -40,7 +41,7 @@ impl Parse for DialectInput {
InputField::Name(field) => name = Some(field.value()),
InputField::TableGen(td) => table_gen = Some(td.value()),
InputField::TdFile(file) => td_file = Some(file.value()),
InputField::Includes(field) => {
InputField::IncludeDirectories(field) => {
includes = field.into_iter().map(|literal| literal.value()).collect()
}
}
Expand All @@ -50,38 +51,7 @@ impl Parse for DialectInput {
name: name.ok_or(input.error("dialect name required"))?,
table_gen,
td_file,
includes,
include_directories: includes,
})
}
}

enum InputField {
Name(LitStr),
TableGen(LitStr),
TdFile(LitStr),
Includes(Punctuated<LitStr, Token![,]>),
}

impl Parse for InputField {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident = input.parse::<Ident>()?;

input.parse::<Token![:]>()?;

if ident == format_ident!("name") {
Ok(Self::Name(input.parse()?))
} else if ident == format_ident!("table_gen") {
Ok(Self::TableGen(input.parse()?))
} else if ident == format_ident!("td_file") {
Ok(Self::TdFile(input.parse()?))
} else if ident == format_ident!("include_dirs") {
let content;
bracketed!(content in input);
Ok(Self::Includes(
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
))
} else {
Err(input.error(format!("invalid field {}", ident)))
}
}
}
34 changes: 34 additions & 0 deletions macro/src/dialect/input/input_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use proc_macro2::Ident;
use quote::format_ident;
use syn::{bracketed, parse::Parse, punctuated::Punctuated, LitStr, Token};

pub enum InputField {
Name(LitStr),
TableGen(LitStr),
TdFile(LitStr),
IncludeDirectories(Punctuated<LitStr, Token![,]>),
}

impl Parse for InputField {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident = input.parse::<Ident>()?;

input.parse::<Token![:]>()?;

if ident == format_ident!("name") {
Ok(Self::Name(input.parse()?))
} else if ident == format_ident!("table_gen") {
Ok(Self::TableGen(input.parse()?))
} else if ident == format_ident!("td_file") {
Ok(Self::TdFile(input.parse()?))
} else if ident == format_ident!("include_dirs") {
let content;
bracketed!(content in input);
Ok(Self::IncludeDirectories(
Punctuated::<LitStr, Token![,]>::parse_terminated(&content)?,
))
} else {
Err(input.error(format!("invalid field {}", ident)))
}
}
}
Loading