Skip to content

Commit

Permalink
build: regenerate type sitter bindings with build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Sep 20, 2023
1 parent 5931650 commit f9656e3
Show file tree
Hide file tree
Showing 9 changed files with 10,015 additions and 8,709 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,6 @@ ij_html_space_around_equality_in_attribute = false
ij_html_space_inside_empty_tag = true
ij_html_text_wrap = normal

[*.rs]
indent_size = 4

[{*.markdown,*.md}]
ij_visual_guides = none
ij_markdown_force_one_space_after_blockquote_symbol = true
Expand Down
126 changes: 122 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ paste = "1.0.14"
tree-sitter = "~0.20.10"
tree-sitter-lura = "0.1.19"
type-sitter-lib = {git = "https://github.com/Jakobeha/type-sitter.git"}
type-sitter-gen = {git = "https://github.com/Jakobeha/type-sitter.git"}

[workspace.package]
edition = "2021"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ It's all based on the works:

And have some great inspirations in [rust-analyzer](https://github.com/rust-lang/rust-analyzer) code-base..

### Running

Clone the repository with `git submodule update --recursive --remote`

### Garbage-Collection

The goal of this compiler isn't optimizing things, it's more like an IDE, so the garbage collector is based on Reference
Expand Down
4 changes: 4 additions & 0 deletions lura-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ salsa-2022.workspace = true
tree-sitter.workspace = true
tree-sitter-lura.workspace = true
type-sitter-lib.workspace = true

[build-dependencies]
type-sitter-gen.workspace = true
rust-format = "0.3.4"
38 changes: 38 additions & 0 deletions lura-syntax/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{fmt::Display, fs::File, io::Write, path::Path};

use rust_format::{Formatter, RustFmt};
use type_sitter_gen::tree_sitter;

fn main() {
regenerate_node_types();

println!("cargo:rerun-if-changed=../vendor/tree-sitter-lura/src/parser.c");
}

/// Regenerates the `src/generated/lura.rs` file from the `src/node-types.json`
fn regenerate_node_types() {
let input_path = Path::new("../vendor/tree-sitter-lura/src/node-types.json");
let target = Path::new("src/generated/lura.rs");
let node_types = type_sitter_gen::generate_nodes(input_path, &tree_sitter()).expect("failed to generate nodes");
clear_output_files(target);
write_rust_file(target, node_types);
}

/// Clears the output file so we don't write a directory or an invalid file
/// when the build script fails.
fn clear_output_files(target: &Path) {
if target.is_dir() {
std::fs::remove_dir_all(target).expect("failed to remove directory");
}
if target.exists() && target.is_file() {
std::fs::remove_file(target).expect("failed to remove file");
}
}

/// Writes the generated Rust code to the output file, formatting it
/// with rustfmt.
fn write_rust_file(path: &Path, contents: impl Display) {
let mut file = File::create(path).expect("failed to create file");
write!(file, "{}", contents).expect("failed to write file");
RustFmt::new().format_file(path).expect("failed to format file");
}
Loading

0 comments on commit f9656e3

Please sign in to comment.