11use anyhow:: Context ;
22use ra_ap_ide_db:: line_index:: LineIndex ;
3+ use ra_ap_parser:: Edition ;
34mod archive;
45mod config;
56pub mod generated;
67mod translate;
78pub mod trap;
9+ use ra_ap_syntax:: ast:: SourceFile ;
10+ use ra_ap_syntax:: AstNode ;
811
12+ fn extract (
13+ archiver : & archive:: Archiver ,
14+ traps : & trap:: TrapFileProvider ,
15+ file : std:: path:: PathBuf ,
16+ ) -> anyhow:: Result < ( ) > {
17+ let file = std:: path:: absolute ( & file) . unwrap_or ( file) ;
18+ let file = std:: fs:: canonicalize ( & file) . unwrap_or ( file) ;
19+ archiver. archive ( & file) ;
20+ let input = std:: fs:: read ( & file) ?;
21+ let input = String :: from_utf8 ( input) ?;
22+ let line_index = LineIndex :: new ( & input) ;
23+ let display_path = file. to_string_lossy ( ) ;
24+ let mut trap = traps. create ( "source" , & file) ;
25+ let label = trap. emit_file ( & file) ;
26+ let mut translator = translate:: Translator :: new ( trap, label, line_index) ;
27+
28+ let parse = ra_ap_syntax:: ast:: SourceFile :: parse ( & input, Edition :: CURRENT ) ;
29+ for err in parse. errors ( ) {
30+ let ( start, _) = translator. location ( err. range ( ) ) ;
31+ log:: warn!( "{}:{}:{}: {}" , display_path, start. line, start. col, err) ;
32+ }
33+ if let Some ( ast) = SourceFile :: cast ( parse. syntax_node ( ) ) {
34+ translator. emit_source_file ( ast) ;
35+ translator. trap . commit ( ) ?
36+ } else {
37+ log:: warn!( "Skipped {}" , display_path) ;
38+ }
39+ Ok ( ( ) )
40+ }
941fn main ( ) -> anyhow:: Result < ( ) > {
1042 let cfg = config:: Config :: extract ( ) . context ( "failed to load configuration" ) ?;
1143 stderrlog:: new ( )
@@ -18,18 +50,7 @@ fn main() -> anyhow::Result<()> {
1850 root : cfg. source_archive_dir ,
1951 } ;
2052 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" ) ?;
53+ extract ( & archiver, & traps, file) ?;
3354 }
3455
3556 Ok ( ( ) )
0 commit comments