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

Draft: Dialect generation from ODS #274

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
86 changes: 81 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ proc-macro = true

[dependencies]
convert_case = "0.6.0"
lazy_static = "1.4.0"
raviqqe marked this conversation as resolved.
Show resolved Hide resolved
once_cell = "1.18.0"
proc-macro2 = "1"
quote = "1"
regex = "1.9.3"
syn = { version = "2", features = ["full"] }
tblgen = { version = "0.3.0", features = ["llvm16-0"] }
unindent = "0.2.2"

[dev-dependencies]
melior = { path = "../melior" }
Expand Down
76 changes: 76 additions & 0 deletions macro/src/dialect/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fmt::Display;

use proc_macro2::Span;
use tblgen::{
raviqqe marked this conversation as resolved.
Show resolved Hide resolved
error::{SourceError, TableGenError},
SourceInfo,
};

#[derive(Debug)]
pub enum Error {
Syn(syn::Error),
TableGen(tblgen::Error),
ExpectedSuperClass(SourceError<ExpectedSuperClassError>),
ParseError,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we directly propagate the errors from the tblgen crate?

}

impl Error {
pub fn add_source_info(self, info: SourceInfo) -> Self {
match self {
Self::TableGen(e) => e.add_source_info(info).into(),
Self::ExpectedSuperClass(e) => e.add_source_info(info).into(),
_ => self,
}
}
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Syn(e) => write!(f, "failed to parse macro input: {e}"),
Error::TableGen(e) => write!(f, "invalid ODS input: {e}"),
Error::ExpectedSuperClass(e) => write!(f, "invalid ODS input: {e}"),
Error::ParseError => write!(f, "error parsing TableGen source"),
}
}
}

impl std::error::Error for Error {}

impl From<SourceError<ExpectedSuperClassError>> for Error {
fn from(value: SourceError<ExpectedSuperClassError>) -> Self {
Self::ExpectedSuperClass(value)
}
}

impl From<SourceError<TableGenError>> for Error {
fn from(value: SourceError<TableGenError>) -> Self {
Self::TableGen(value)
}
}

impl From<syn::Error> for Error {
fn from(value: syn::Error) -> Self {
Self::Syn(value)
}
}

impl From<Error> for syn::Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this implementation for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a leftover from when the code was in a separate crate. I used to convert all errors to syn::Error and used syn::Error::into_compile_error. I guess this also why I made a custom error type, but it can probably be removed now.

fn from(value: Error) -> Self {
match value {
Error::Syn(e) => e,
_ => syn::Error::new(Span::call_site(), format!("{}", value)),
}
}
}

#[derive(Debug)]
pub struct ExpectedSuperClassError(pub String);

impl Display for ExpectedSuperClassError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "expected this record to be a subclass of {}", self.0)
}
}

impl std::error::Error for ExpectedSuperClassError {}
Loading