Skip to content

Commit 1f30d5f

Browse files
committed
Rust: generate schema.py and extractor from ungrammar
1 parent 61ac8d6 commit 1f30d5f

File tree

8 files changed

+539
-84
lines changed

8 files changed

+539
-84
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ r.from_cargo(
6161
"//ruby/extractor:Cargo.toml",
6262
"//rust/extractor:Cargo.toml",
6363
"//rust/extractor/macros:Cargo.toml",
64+
"//rust/generate-schema:Cargo.toml",
6465
"//shared/tree-sitter-extractor:Cargo.toml",
6566
],
6667
)

rust/extractor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ra_ap_paths = "0.0.232"
1818
ra_ap_project_model = "0.0.232"
1919
ra_ap_syntax = "0.0.232"
2020
ra_ap_vfs = "0.0.232"
21+
ra_ap_parser = "0.0.232"
2122
serde = "1.0.209"
2223
serde_with = "3.9.0"
2324
stderrlog = "0.6.0"

rust/extractor/src/main.rs

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
use crate::trap::TrapId;
21
use anyhow::Context;
3-
use itertools::Itertools;
4-
use log::info;
5-
use ra_ap_hir::db::DefDatabase;
6-
use ra_ap_hir::Crate;
7-
use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
8-
use ra_ap_project_model::CargoConfig;
9-
use ra_ap_project_model::RustLibSource;
10-
use ra_ap_vfs::AbsPathBuf;
11-
use std::path::PathBuf;
12-
2+
use ra_ap_ide_db::line_index::LineIndex;
133
mod archive;
144
mod config;
155
pub mod generated;
166
mod translate;
177
pub mod trap;
188

19-
fn find_project_manifests(
20-
files: &[PathBuf],
21-
) -> anyhow::Result<Vec<ra_ap_project_model::ProjectManifest>> {
22-
let current = std::env::current_dir()?;
23-
let abs_files: Vec<_> = files
24-
.iter()
25-
.map(|path| AbsPathBuf::assert_utf8(current.join(path)))
26-
.collect();
27-
let ret = ra_ap_project_model::ProjectManifest::discover_all(&abs_files);
28-
info!(
29-
"found manifests: {}",
30-
ret.iter().map(|m| format!("{m}")).join(", ")
31-
);
32-
Ok(ret)
33-
}
34-
359
fn main() -> anyhow::Result<()> {
3610
let cfg = config::Config::extract().context("failed to load configuration")?;
3711
stderrlog::new()
@@ -43,52 +17,20 @@ fn main() -> anyhow::Result<()> {
4317
let archiver = archive::Archiver {
4418
root: cfg.source_archive_dir,
4519
};
46-
47-
let config = CargoConfig {
48-
sysroot: Some(RustLibSource::Discover),
49-
target_dir: ra_ap_paths::Utf8PathBuf::from_path_buf(cfg.scratch_dir)
50-
.map(|x| x.join("target"))
51-
.ok(),
52-
..Default::default()
53-
};
54-
let progress = |t| (log::info!("progress: {}", t));
55-
let load_config = LoadCargoConfig {
56-
load_out_dirs_from_check: true,
57-
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
58-
prefill_caches: false,
59-
};
60-
let projects = find_project_manifests(&cfg.inputs).context("loading inputs")?;
61-
for project in projects {
62-
let (db, vfs, _macro_server) = load_workspace_at(
63-
project.manifest_path().as_ref(),
64-
&config,
65-
&load_config,
66-
&progress,
67-
)?;
68-
69-
let crates = <dyn DefDatabase>::crate_graph(&db);
70-
for crate_id in crates.iter() {
71-
let krate = Crate::from(crate_id);
72-
if !cfg.extract_dependencies && !krate.origin(&db).is_local() {
73-
continue;
74-
}
75-
let name = krate.display_name(&db);
76-
let crate_name = name
77-
.as_ref()
78-
.map(|n| n.canonical_name().as_str())
79-
.unwrap_or("");
80-
let trap = traps.create(
81-
"crates",
82-
&PathBuf::from(format!(
83-
"/{}_{}",
84-
crate_name,
85-
crate_id.into_raw().into_u32()
86-
)),
87-
);
88-
translate::CrateTranslator::new(&db, trap, &krate, &vfs, &archiver)
89-
.emit_crate()
90-
.context("writing trap file")?;
91-
}
20+
for file in cfg.inputs {
21+
let file = std::path::absolute(&file).unwrap_or(file);
22+
let file = std::fs::canonicalize(&file).unwrap_or(file);
23+
archiver.archive(&file);
24+
let input = std::fs::read(&file)?;
25+
let input = String::from_utf8(input)?;
26+
let line_index = LineIndex::new(&input);
27+
let display_path = file.to_string_lossy();
28+
let mut trap = traps.create("source", &file);
29+
let label = trap.emit_file(&file);
30+
translate::SourceFileTranslator::new(trap, label, line_index)
31+
.extract(&display_path, &input)
32+
.context("writing trap file")?;
9233
}
34+
9335
Ok(())
9436
}

rust/generate-schema/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("//misc/bazel:rust.bzl", "codeql_rust_binary")
2+
3+
codeql_rust_binary(
4+
name = "generate-schema",
5+
srcs = glob(["src/**/*.rs"]),
6+
aliases = aliases(),
7+
proc_macro_deps = all_crate_deps(
8+
proc_macro = True,
9+
),
10+
visibility = ["//rust:__subpackages__"],
11+
deps = all_crate_deps(
12+
normal = True,
13+
),
14+
)

rust/generate-schema/src/codegen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub mod grammar;
2-
32
pub fn reformat(x: String) -> String {
43
x
54
}

rust/generate-schema/src/codegen/grammar.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
project_root,
2222
};
2323

24-
mod ast_src;
24+
pub mod ast_src;
2525
use self::ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc};
2626

2727
pub(crate) fn generate(check: bool) {
@@ -621,10 +621,16 @@ fn pluralize(s: &str) -> String {
621621
}
622622

623623
impl Field {
624-
fn is_many(&self) -> bool {
625-
matches!(self, Field::Node { cardinality: Cardinality::Many, .. })
624+
pub fn is_many(&self) -> bool {
625+
matches!(
626+
self,
627+
Field::Node {
628+
cardinality: Cardinality::Many,
629+
..
630+
}
631+
)
626632
}
627-
fn token_kind(&self) -> Option<proc_macro2::TokenStream> {
633+
pub fn token_kind(&self) -> Option<proc_macro2::TokenStream> {
628634
match self {
629635
Field::Token(token) => {
630636
let token: proc_macro2::TokenStream = token.parse().unwrap();
@@ -633,7 +639,7 @@ impl Field {
633639
_ => None,
634640
}
635641
}
636-
fn method_name(&self) -> String {
642+
pub fn method_name(&self) -> String {
637643
match self {
638644
Field::Token(name) => {
639645
let name = match name.as_str() {
@@ -679,7 +685,7 @@ impl Field {
679685
}
680686
}
681687
}
682-
fn ty(&self) -> proc_macro2::Ident {
688+
pub fn ty(&self) -> proc_macro2::Ident {
683689
match self {
684690
Field::Token(_) => format_ident!("SyntaxToken"),
685691
Field::Node { ty, .. } => format_ident!("{}", ty),
@@ -696,7 +702,7 @@ fn clean_token_name(name: &str) -> String {
696702
}
697703
}
698704

699-
fn lower(grammar: &Grammar) -> AstSrc {
705+
pub(crate) fn lower(grammar: &Grammar) -> AstSrc {
700706
let mut res = AstSrc {
701707
tokens:
702708
"Whitespace Comment String ByteString CString IntNumber FloatNumber Char Byte Ident"

0 commit comments

Comments
 (0)