-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Michael-F-Bryan/bump-tree-sitter
Bump the tree-sitter version to `0.22.5` and add a `xtask codegen [ast]` sub-command
- Loading branch information
Showing
13 changed files
with
649 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
mod ast; | ||
|
||
use clap::Parser; | ||
use color_eyre::Report; | ||
|
||
use crate::codegen::ast::Ast; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
#[clap(subcommand_value_name = "TARGET", subcommand_help_heading = "Targets")] | ||
pub struct Codegen { | ||
#[clap(subcommand)] | ||
target: Option<Target>, | ||
} | ||
|
||
impl Codegen { | ||
pub fn run(self) -> Result<(), Report> { | ||
let Codegen { target } = self; | ||
|
||
match target { | ||
Some(target) => target.generate(), | ||
None => run_all_generators(), | ||
} | ||
} | ||
} | ||
|
||
/// Run all code generators using the default settings. | ||
fn run_all_generators() -> Result<(), Report> { | ||
Ast::default().generate()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
enum Target { | ||
/// Generate strongly-typed AST nodes. | ||
Ast(Ast), | ||
} | ||
|
||
impl Target { | ||
fn generate(self) -> Result<(), Report> { | ||
match self { | ||
Target::Ast(a) => a.generate(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
mod codegen; | ||
mod utils; | ||
|
||
use clap::Parser; | ||
use color_eyre::Report; | ||
|
||
use crate::codegen::Codegen; | ||
|
||
fn main() -> Result<(), Report> { | ||
color_eyre::install()?; | ||
|
||
fn main() { | ||
let _ = Cmd::parse(); | ||
todo!(); | ||
let cmd = Cmd::parse(); | ||
|
||
match cmd { | ||
Cmd::Codegen(c) => c.run(), | ||
} | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version)] | ||
enum Cmd {} | ||
enum Cmd { | ||
/// Run code generation. | ||
Codegen(Codegen), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#![allow(dead_code)] // used during testing | ||
|
||
use std::path::Path; | ||
|
||
use color_eyre::eyre::{Context, Report}; | ||
use quote::ToTokens; | ||
|
||
/// Format some Rust tokens. | ||
/// | ||
/// # Panics | ||
/// | ||
/// It is assumed that the tokens would parse as a Rust file. | ||
pub fn format_rust(contents: impl ToTokens) -> String { | ||
let contents = | ||
syn::parse2(contents.to_token_stream()).expect("Unable to parse the tokens as a syn::File"); | ||
prettyplease::unparse(&contents) | ||
} | ||
|
||
/// Check that a particular file has the desired contents. | ||
/// | ||
/// If the file is missing or outdated, this function will update the file and | ||
/// error out. | ||
pub fn ensure_file_contents( | ||
path: impl AsRef<Path>, | ||
contents: impl AsRef<str>, | ||
) -> Result<(), Report> { | ||
let path = path.as_ref(); | ||
let contents = normalize_newlines(contents.as_ref()); | ||
|
||
if let Ok(old_contents) = std::fs::read_to_string(path) { | ||
if contents == normalize_newlines(&old_contents) { | ||
// File is already up to date | ||
return Ok(()); | ||
} | ||
} | ||
|
||
let display_path = path.strip_prefix(project_root()).unwrap_or(path); | ||
|
||
eprintln!( | ||
"\"{}\" was not up-to-date, updating...", | ||
display_path.display() | ||
); | ||
|
||
if std::env::var("CI").is_ok() { | ||
eprintln!("Note: run codegen locally and commit the updated files"); | ||
} | ||
|
||
if let Some(parent) = path.parent() { | ||
let _ = std::fs::create_dir_all(parent); | ||
} | ||
std::fs::write(path, contents) | ||
.wrap_err_with(|| format!("Unable to save to \"{}\"", path.display()))?; | ||
|
||
color_eyre::eyre::bail!( | ||
"\"{}\" was not up to date and has been updated. Please re-run the tests.", | ||
display_path.display() | ||
); | ||
} | ||
|
||
fn normalize_newlines(s: &str) -> String { | ||
s.replace("\r\n", "\n") | ||
} | ||
|
||
/// Get the root directory for this repository. | ||
pub fn project_root() -> &'static Path { | ||
Path::new(env!("CARGO_MANIFEST_DIR")) | ||
.ancestors() | ||
.find(|p| p.join(".git").exists()) | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.