Skip to content

Commit

Permalink
Start migrating to tree sitter
Browse files Browse the repository at this point in the history
Migrates all the lower level code to tree-sitter not touching passes yet.
  • Loading branch information
Noratrieb committed Dec 31, 2023
1 parent cf39338 commit cb0bad3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ target
/invalid.rs
/invalid
.direnv/
g
g
perf.data*
*.svg
42 changes: 42 additions & 0 deletions Cargo.lock

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

11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ license = "MIT OR Apache-2.0"
[profile.release]
lto = "thin"

[profile.dev]
opt-level = 1
[profile.dev.package.proc-macro2]
opt-level = 3
[profile.dev.package.syn]
opt-level = 3
[profile.dev.package.genemichaels]
opt-level = 3

[dependencies]
anyhow = "1.0.65"
Expand All @@ -36,4 +40,7 @@ tempfile = "3.3.0"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-tree = "0.2.2"
tree-sitter = "0.20.10"
tree-sitter-edit = "0.3.0"
tree-sitter-rust = "0.20.4"
walkdir = "2.3.2"
4 changes: 2 additions & 2 deletions src/dylib_flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl FromStr for RustFunction {
fn wrap_func_body(func: &str) -> Result<String> {
let closure = syn::parse_str::<syn::ExprClosure>(func).context("invalid rust syntax")?;

let syn_file = syn::parse_quote! {
let file = quote::quote! {
#[repr(C)]
pub struct __RawOutput {
out_ptr: *const u8,
Expand Down Expand Up @@ -80,7 +80,7 @@ fn wrap_func_body(func: &str) -> Result<String> {
}
};

crate::formatting::format(syn_file)
Ok(file.to_string())
}

impl RustFunction {
Expand Down
12 changes: 0 additions & 12 deletions src/formatting.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

mod build;
mod dylib_flag;
mod formatting;
mod tree_sitter;
mod passes;
mod processor;

Expand Down
20 changes: 11 additions & 9 deletions src/processor/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ mod file {
pub(crate) struct SourceFile {
path: PathBuf,
content_str: RefCell<String>,
content: RefCell<syn::File>,
content: RefCell<tree_sitter::Tree>,
}

impl SourceFile {
pub(crate) fn open(path: PathBuf) -> Result<Self> {
let string = std::fs::read_to_string(&path)
.with_context(|| format!("reading file {}", path.display()))?;
let content = syn::parse_file(&string)
.with_context(|| format!("parsing file {}", path.display()))?;

let content_ts = crate::tree_sitter::parse(&string)
.with_context(|| format!("parsing file {path:?}"))?;

Ok(SourceFile {
path,
content_str: RefCell::new(string),
content: RefCell::new(content),
content: RefCell::new(content_ts),
})
}

pub(crate) fn write(&self, new: syn::File) -> Result<()> {
let string = crate::formatting::format(new.clone())?;
pub(crate) fn write(&self, new: tree_sitter::Tree) -> Result<()> {
let string = crate::tree_sitter::format(new, &*self.content_str.borrow())?;
std::fs::write(&self.path, &string)
.with_context(|| format!("writing file {}", self.path.display()))?;
*self.content_str.borrow_mut() = string;
Expand Down Expand Up @@ -96,17 +98,17 @@ pub(crate) struct FileChange<'a, 'b> {
pub(crate) path: &'a Path,
source_file: &'a SourceFile,
before_content_str: String,
before_content: syn::File,
before_content: tree_sitter::Tree,
changes: &'b mut Changes,
has_written_change: bool,
}

impl FileChange<'_, '_> {
pub(crate) fn before_content(&self) -> (&str, &syn::File) {
pub(crate) fn before_content(&self) -> (&str, &tree_sitter::Tree) {
(&self.before_content_str, &self.before_content)
}

pub(crate) fn write(&mut self, new: syn::File) -> Result<()> {
pub(crate) fn write(&mut self, new: tree_sitter::Tree) -> Result<()> {
self.has_written_change = true;
self.source_file.write(new)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/processor/reaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Minimizer {

let result =
rustfix::apply_suggestions(change.before_content().0, &desired_suggestions)?;
let result = syn::parse_file(&result).context("parsing file after rustfix")?;
let result = crate::tree_sitter::parse(&result).context("parsing file after rustfix")?;
change.write(result)?;

let after = self.build.build()?;
Expand Down
34 changes: 34 additions & 0 deletions src/tree_sitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::{Context, Result};

pub fn parse(source: &str) -> Result<tree_sitter::Tree> {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(tree_sitter_rust::language())
.context("loading tree sitter rust grammar")?;
let content_ts = parser.parse(source, None).context("parsing file")?;
Ok(content_ts)
}

pub fn format(file: tree_sitter::Tree, source: &str) -> anyhow::Result<String> {
let mut s = Vec::new();
tree_sitter_edit::render(&mut s, &file, source.as_bytes(), &Editor);

Ok(String::from_utf8(s).unwrap())
}

struct Editor;

impl tree_sitter_edit::Editor for Editor {
fn has_edit(&self, tree: &tree_sitter::Tree, node: &tree_sitter::Node<'_>) -> bool {
false
}

fn edit(
&self,
source: &[u8],
tree: &tree_sitter::Tree,
node: &tree_sitter::Node<'_>,
) -> Vec<u8> {
unimplemented!()
}
}

0 comments on commit cb0bad3

Please sign in to comment.