Skip to content

Commit

Permalink
Merge pull request #17543 from github/aibaars/rust-gen-extractor
Browse files Browse the repository at this point in the history
Rust: generate the extractor
  • Loading branch information
aibaars authored Sep 24, 2024
2 parents 5b45d36 + d14e77b commit 4795333
Show file tree
Hide file tree
Showing 8 changed files with 2,349 additions and 2,191 deletions.
45 changes: 33 additions & 12 deletions rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
use anyhow::Context;
use ra_ap_ide_db::line_index::LineIndex;
use ra_ap_parser::Edition;
mod archive;
mod config;
pub mod generated;
mod translate;
pub mod trap;
use ra_ap_syntax::ast::SourceFile;
use ra_ap_syntax::AstNode;

fn extract(
archiver: &archive::Archiver,
traps: &trap::TrapFileProvider,
file: std::path::PathBuf,
) -> anyhow::Result<()> {
let file = std::path::absolute(&file).unwrap_or(file);
let file = std::fs::canonicalize(&file).unwrap_or(file);
archiver.archive(&file);
let input = std::fs::read(&file)?;
let input = String::from_utf8(input)?;
let line_index = LineIndex::new(&input);
let display_path = file.to_string_lossy();
let mut trap = traps.create("source", &file);
let label = trap.emit_file(&file);
let mut translator = translate::Translator::new(trap, label, line_index);

let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
for err in parse.errors() {
let (start, _) = translator.location(err.range());
log::warn!("{}:{}:{}: {}", display_path, start.line, start.col, err);
}
if let Some(ast) = SourceFile::cast(parse.syntax_node()) {
translator.emit_source_file(ast);
translator.trap.commit()?
} else {
log::warn!("Skipped {}", display_path);
}
Ok(())
}
fn main() -> anyhow::Result<()> {
let cfg = config::Config::extract().context("failed to load configuration")?;
stderrlog::new()
Expand All @@ -18,18 +50,7 @@ fn main() -> anyhow::Result<()> {
root: cfg.source_archive_dir,
};
for file in cfg.inputs {
let file = std::path::absolute(&file).unwrap_or(file);
let file = std::fs::canonicalize(&file).unwrap_or(file);
archiver.archive(&file);
let input = std::fs::read(&file)?;
let input = String::from_utf8(input)?;
let line_index = LineIndex::new(&input);
let display_path = file.to_string_lossy();
let mut trap = traps.create("source", &file);
let label = trap.emit_file(&file);
translate::SourceFileTranslator::new(trap, label, line_index)
.extract(&display_path, &input)
.context("writing trap file")?;
extract(&archiver, &traps, file)?;
}

Ok(())
Expand Down
Loading

0 comments on commit 4795333

Please sign in to comment.