Skip to content

Commit 61c3aa6

Browse files
author
Paolo Tranquilli
committed
Rust: integrate rust code generation into //rust/codegen
1 parent 96dda88 commit 61c3aa6

File tree

6 files changed

+148
-318
lines changed

6 files changed

+148
-318
lines changed

rust/ast-generator/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@tree_sitter_extractors_deps//:defs.bzl", "aliases", "all_crate_deps")
12
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
23

34
codeql_rust_binary(
@@ -12,3 +13,9 @@ codeql_rust_binary(
1213
normal = True,
1314
),
1415
)
16+
17+
filegroup(
18+
name = "manifest",
19+
srcs = ["Cargo.toml"],
20+
visibility = ["//rust:__subpackages__"],
21+
)

rust/ast-generator/src/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ pub fn ensure_file_contents(
1212
contents: &String,
1313
_check: bool,
1414
) {
15-
std::fs::write(path, contents).expect("Unable to write file");
15+
std::fs::write(path, contents).unwrap_or_else(|_| panic!("Unable to write {}", path.display()));
1616
}

rust/ast-generator/src/main.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn write_schema(
5757
buf,
5858
"# Generated by `ast-generator`, do not edit by hand.\n"
5959
)?;
60-
writeln!(buf, "from .prelude import *\n")?;
60+
writeln!(buf, "from .prelude import *")?;
6161

6262
for node in &grammar.enums {
6363
let super_classses = if let Some(cls) = super_types.get(&node.name) {
@@ -66,9 +66,13 @@ fn write_schema(
6666
} else {
6767
"AstNode".to_owned()
6868
};
69-
writeln!(buf, "class {}({}):", class_name(&node.name), super_classses)?;
69+
writeln!(
70+
buf,
71+
"\nclass {}({}):",
72+
class_name(&node.name),
73+
super_classses
74+
)?;
7075
writeln!(buf, " pass")?;
71-
writeln!(buf)?;
7276
}
7377
for node in &grammar.nodes {
7478
let super_classses = if let Some(cls) = super_types.get(&node.name) {
@@ -77,7 +81,12 @@ fn write_schema(
7781
} else {
7882
"AstNode".to_owned()
7983
};
80-
writeln!(buf, "class {}({}):", class_name(&node.name), super_classses)?;
84+
writeln!(
85+
buf,
86+
"\nclass {}({}):",
87+
class_name(&node.name),
88+
super_classses
89+
)?;
8190
let mut empty = true;
8291
for field in get_fields(node) {
8392
if field.tp == "SyntaxToken" {
@@ -111,7 +120,6 @@ fn write_schema(
111120
if empty {
112121
writeln!(buf, " pass")?;
113122
}
114-
writeln!(buf)?;
115123
}
116124
Ok(String::from_utf8_lossy(&buf).to_string())
117125
}
@@ -409,6 +417,8 @@ fn write_extractor(grammar: &AstSrc) -> std::io::Result<String> {
409417
writeln!(
410418
buf,
411419
"//! Generated by `ast-generator`, do not edit by hand.\n
420+
#![cfg_attr(any(), rustfmt::skip)]
421+
412422
use crate::generated;
413423
use super::base::{{TextValue, Translator}};
414424
use crate::trap::{{Label, TrapId}};
@@ -548,7 +558,7 @@ fn main() -> std::io::Result<()> {
548558
super_class_x.cmp(&super_class_y).then(x.name.cmp(&y.name))
549559
});
550560
let schema = write_schema(&grammar, super_types)?;
551-
let schema_path = PathBuf::from("../schema/ast.py");
561+
let schema_path = project_root().join("schema/ast.py");
552562
codegen::ensure_file_contents(
553563
crate::flags::CodegenType::Grammar,
554564
&schema_path,
@@ -557,7 +567,7 @@ fn main() -> std::io::Result<()> {
557567
);
558568

559569
let extractor = write_extractor(&grammar)?;
560-
let extractor_path = PathBuf::from("../extractor/src/translate/generated.rs");
570+
let extractor_path = project_root().join("extractor/src/translate/generated.rs");
561571
codegen::ensure_file_contents(
562572
crate::flags::CodegenType::Grammar,
563573
&extractor_path,

rust/codegen/BUILD.bazel

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
1+
_args = [
2+
"//rust/ast-generator",
3+
"//rust/ast-generator:manifest",
4+
"//misc/codegen",
5+
"//rust:codegen-conf",
6+
]
27

3-
native_binary(
8+
sh_binary(
49
name = "codegen",
5-
src = "//misc/codegen",
6-
out = "codegen",
7-
args = [
8-
"--configuration-file=$(location //rust:codegen-conf)",
9-
],
10-
data = [
11-
"//rust:codegen-conf",
12-
"//rust:schema",
13-
],
10+
srcs = ["codegen.sh"],
11+
args = ["$(rlocationpath %s)" % a for a in _args],
12+
data = _args,
1413
visibility = ["//rust:__subpackages__"],
14+
deps = [
15+
"//misc/bazel:sh_runfiles",
16+
],
1517
)

rust/codegen/codegen.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
source misc/bazel/runfiles.sh 2>/dev/null || source external/ql~/misc/bazel/runfiles.sh
6+
7+
ast_generator="$(rlocation "$1")"
8+
ast_generator_manifest="$(rlocation "$2")"
9+
codegen="$(rlocation "$3")"
10+
codegen_conf="$(rlocation "$4")"
11+
12+
CARGO_MANIFEST_DIR="$(dirname "$ast_generator_manifest")" "$ast_generator"
13+
"$codegen" --configuration-file="$codegen_conf"

0 commit comments

Comments
 (0)