Skip to content

Commit

Permalink
feat(ast_tools): ability to generate text files
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 19, 2024
1 parent 6939e4e commit 50f771a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion tasks/ast_tools/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl From<(PathBuf, TokenStream)> for SideEffect {

impl From<GeneratorOutput> for SideEffect {
fn from(output: GeneratorOutput) -> Self {
Self::from((output.path, output.tokens))
match output {
GeneratorOutput::Rust { path, tokens } => Self::from((path, tokens)),
GeneratorOutput::Raw { path, content } => Self { path, content: content.into() },
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Generator for AssertLayouts {

let header = generated_header!();

GeneratorOutput {
GeneratorOutput::Rust {
path: output(crate::AST_CRATE, "assert_layouts.rs"),
tokens: quote! {
#header
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Generator for AstBuilderGenerator {

let header = generated_header!();

GeneratorOutput {
GeneratorOutput::Rust {
path: output(crate::AST_CRATE, "ast_builder.rs"),
tokens: quote! {
#header
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Generator for AstKindGenerator {

let header = generated_header!();

GeneratorOutput {
GeneratorOutput::Rust {
path: output(crate::AST_CRATE, "ast_kind.rs"),
tokens: quote! {
#header
Expand Down
13 changes: 10 additions & 3 deletions tasks/ast_tools/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ pub trait Generator {
}

#[derive(Debug, Clone)]
pub struct GeneratorOutput {
pub path: PathBuf,
pub tokens: TokenStream,
pub enum GeneratorOutput {
Rust {
path: PathBuf,
tokens: TokenStream,
},
#[expect(dead_code)]
Raw {
path: PathBuf,
content: String,
},
}

macro_rules! define_generator {
Expand Down
4 changes: 2 additions & 2 deletions tasks/ast_tools/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define_generator! {

impl Generator for VisitGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
GeneratorOutput {
GeneratorOutput::Rust {
path: output(crate::AST_CRATE, "visit.rs"),
tokens: generate_visit::<false>(ctx),
}
Expand All @@ -37,7 +37,7 @@ impl Generator for VisitGenerator {

impl Generator for VisitMutGenerator {
fn generate(&mut self, ctx: &LateCtx) -> GeneratorOutput {
GeneratorOutput {
GeneratorOutput::Rust {
path: output(crate::AST_CRATE, "visit_mut.rs"),
tokens: generate_visit::<true>(ctx),
}
Expand Down

0 comments on commit 50f771a

Please sign in to comment.