(file_tree: &SourceTree)
| 9 | use crate::{Error, Errors, Result, SourceTree, WithErrorInfo}; |
| 10 | |
| 11 | pub fn parse(file_tree: &SourceTree) -> Result<pr::ModuleDef, Errors> { |
| 12 | // register a new stage of the compiler |
| 13 | // (here should register lexer stage first, but that all happens in a single call to prqlc_parser) |
| 14 | debug::log_entry(|| debug::DebugEntryKind::ReprPrql(file_tree.clone())); |
| 15 | debug::log_stage(debug::Stage::Parsing); |
| 16 | |
| 17 | let source_files = linearize_tree(file_tree)?; |
| 18 | |
| 19 | // reverse the id->file_path map |
| 20 | let ids: HashMap<_, _> = file_tree |
| 21 | .source_ids |
| 22 | .iter() |
| 23 | .map(|(a, b)| (b.as_path(), a)) |
| 24 | .collect(); |
| 25 | |
| 26 | // init the root module def |
| 27 | let mut root = pr::ModuleDef { |
| 28 | name: "Project".to_string(), |
| 29 | stmts: Vec::new(), |
| 30 | }; |
| 31 | |
| 32 | // parse and insert into the root |
| 33 | let mut errors = Vec::new(); |
| 34 | for source_file in source_files { |
| 35 | let id = ids |
| 36 | .get(&source_file.file_path) |
| 37 | .map(|x| **x) |
| 38 | .expect("source tree has malformed ids"); |
| 39 | |
| 40 | match parse_source(source_file.content, id) { |
| 41 | Ok(stmts) => { |
| 42 | insert_stmts_at_path(&mut root, source_file.module_path, stmts); |
| 43 | } |
| 44 | Err(errs) => errors.extend(errs), |
| 45 | } |
| 46 | } |
| 47 | if errors.is_empty() { |
| 48 | debug::log_entry(|| debug::DebugEntryKind::ReprPr(root.clone())); |
| 49 | Ok(root) |
| 50 | } else { |
| 51 | Err(Errors(errors)) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Build PR AST from a PRQL query string. |
| 56 | // We have this function in `prqlc` rather than in `prqlc-parser` crate since |
no test coverage detected