MCPcopy Create free account
hub / github.com/PRQL/prql / parse

Function parse

prqlc/prqlc/src/parser.rs:11–53  ·  view source on GitHub ↗
(file_tree: &SourceTree)

Source from the content-addressed store, hash-verified

9use crate::{Error, Errors, Result, SourceTree, WithErrorInfo};
10
11pub 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

Callers 2

compiler_versionFunction · 0.70
prql_to_pl_treeFunction · 0.70

Calls 11

log_entryFunction · 0.85
log_stageFunction · 0.85
linearize_treeFunction · 0.85
insert_stmts_at_pathFunction · 0.85
ErrorsClass · 0.85
mapMethod · 0.80
iterMethod · 0.80
is_emptyMethod · 0.80
parse_sourceFunction · 0.70
collectMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected