Rust: integrate rust code generation into //rust/codegen

This commit is contained in:
Paolo Tranquilli
2024-10-08 10:37:53 +02:00
parent 96dda8808c
commit 61c3aa6288
6 changed files with 148 additions and 318 deletions

View File

@@ -1,3 +1,4 @@
load("@tree_sitter_extractors_deps//:defs.bzl", "aliases", "all_crate_deps")
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
codeql_rust_binary(
@@ -12,3 +13,9 @@ codeql_rust_binary(
normal = True,
),
)
filegroup(
name = "manifest",
srcs = ["Cargo.toml"],
visibility = ["//rust:__subpackages__"],
)

View File

@@ -12,5 +12,5 @@ pub fn ensure_file_contents(
contents: &String,
_check: bool,
) {
std::fs::write(path, contents).expect("Unable to write file");
std::fs::write(path, contents).unwrap_or_else(|_| panic!("Unable to write {}", path.display()));
}

View File

@@ -57,7 +57,7 @@ fn write_schema(
buf,
"# Generated by `ast-generator`, do not edit by hand.\n"
)?;
writeln!(buf, "from .prelude import *\n")?;
writeln!(buf, "from .prelude import *")?;
for node in &grammar.enums {
let super_classses = if let Some(cls) = super_types.get(&node.name) {
@@ -66,9 +66,13 @@ fn write_schema(
} else {
"AstNode".to_owned()
};
writeln!(buf, "class {}({}):", class_name(&node.name), super_classses)?;
writeln!(
buf,
"\nclass {}({}):",
class_name(&node.name),
super_classses
)?;
writeln!(buf, " pass")?;
writeln!(buf)?;
}
for node in &grammar.nodes {
let super_classses = if let Some(cls) = super_types.get(&node.name) {
@@ -77,7 +81,12 @@ fn write_schema(
} else {
"AstNode".to_owned()
};
writeln!(buf, "class {}({}):", class_name(&node.name), super_classses)?;
writeln!(
buf,
"\nclass {}({}):",
class_name(&node.name),
super_classses
)?;
let mut empty = true;
for field in get_fields(node) {
if field.tp == "SyntaxToken" {
@@ -111,7 +120,6 @@ fn write_schema(
if empty {
writeln!(buf, " pass")?;
}
writeln!(buf)?;
}
Ok(String::from_utf8_lossy(&buf).to_string())
}
@@ -409,6 +417,8 @@ fn write_extractor(grammar: &AstSrc) -> std::io::Result<String> {
writeln!(
buf,
"//! Generated by `ast-generator`, do not edit by hand.\n
#![cfg_attr(any(), rustfmt::skip)]
use crate::generated;
use super::base::{{TextValue, Translator}};
use crate::trap::{{Label, TrapId}};
@@ -548,7 +558,7 @@ fn main() -> std::io::Result<()> {
super_class_x.cmp(&super_class_y).then(x.name.cmp(&y.name))
});
let schema = write_schema(&grammar, super_types)?;
let schema_path = PathBuf::from("../schema/ast.py");
let schema_path = project_root().join("schema/ast.py");
codegen::ensure_file_contents(
crate::flags::CodegenType::Grammar,
&schema_path,
@@ -557,7 +567,7 @@ fn main() -> std::io::Result<()> {
);
let extractor = write_extractor(&grammar)?;
let extractor_path = PathBuf::from("../extractor/src/translate/generated.rs");
let extractor_path = project_root().join("extractor/src/translate/generated.rs");
codegen::ensure_file_contents(
crate::flags::CodegenType::Grammar,
&extractor_path,