Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions helix-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,27 +488,34 @@ fn parse_content(content: &Content) -> Result<Source, String> {

/// Runs the static analyzer on the parsed source to catch errors and generate diagnostics if any.
/// Otherwise returns the generated source object which is an IR used to transpile the queries to rust.
fn analyze_source(source: Source) -> Result<GeneratedSource, String> {
if source.schema.is_empty() {
fn analyze_source(content: &Content) -> Result<GeneratedSource, String> {
if content.source.schema.is_empty() {
return Err("No schema definitions provided".to_string());
}

let (diagnostics, source) = analyze(&source);
let (diagnostics, analyzed_source) = analyze(&content.source);
if !diagnostics.is_empty() {
for diag in diagnostics {
let filepath = diag.filepath.clone().unwrap_or("queries.hx".to_string());
println!("{}", diag.render(&source.src, &filepath));

// Find the correct file content for this diagnostic
let file_content = content.files.iter()
.find(|f| f.name == filepath)
.map(|f| &f.content)
.unwrap_or(&analyzed_source.src);

println!("{}", diag.render(file_content, &filepath));
}
return Err("compilation failed!".to_string());
}

Ok(source)
Ok(analyzed_source)
}

pub fn generate(files: &[DirEntry], path: &str) -> Result<(Content, GeneratedSource), String> {
let mut content = generate_content(files)?;
content.source = parse_content(&content)?;
let mut analyzed_source = analyze_source(content.source.clone())?;
let mut analyzed_source = analyze_source(&content)?;
analyzed_source.config = read_config(path)?;
Ok((content, analyzed_source))
}
Expand Down
18 changes: 9 additions & 9 deletions helix-db/src/helixc/parser/schema_parse_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl HelixParser {
) -> Result<NodeSchema, ParserError> {
let mut pairs = pair.clone().into_inner();
let name = pairs.next().unwrap().as_str().to_string();
let fields = self.parse_node_body(pairs.next().unwrap())?;
let fields = self.parse_node_body(pairs.next().unwrap(), filepath.clone())?;
Ok(NodeSchema {
name: (pair.loc(), name),
fields,
Expand All @@ -35,15 +35,15 @@ impl HelixParser {
) -> Result<VectorSchema, ParserError> {
let mut pairs = pair.clone().into_inner();
let name = pairs.next().unwrap().as_str().to_string();
let fields = self.parse_node_body(pairs.next().unwrap())?;
let fields = self.parse_node_body(pairs.next().unwrap(), filepath.clone())?;
Ok(VectorSchema {
name,
fields,
loc: pair.loc_with_filepath(filepath),
})
}

pub(super) fn parse_node_body(&self, pair: Pair<Rule>) -> Result<Vec<Field>, ParserError> {
pub(super) fn parse_node_body(&self, pair: Pair<Rule>, filepath: String) -> Result<Vec<Field>, ParserError> {
let field_defs = pair
.into_inner()
.find(|p| p.as_rule() == Rule::field_defs)
Expand All @@ -52,7 +52,7 @@ impl HelixParser {
// Now parse each individual field_def
field_defs
.into_inner()
.map(|p| self.parse_field_def(p))
.map(|p| self.parse_field_def(p, filepath.clone()))
.collect::<Result<Vec<_>, _>>()
}

Expand Down Expand Up @@ -405,7 +405,7 @@ impl HelixParser {
}
}

pub(super) fn parse_field_def(&self, pair: Pair<Rule>) -> Result<Field, ParserError> {
pub(super) fn parse_field_def(&self, pair: Pair<Rule>, filepath: String) -> Result<Field, ParserError> {
let mut pairs = pair.clone().into_inner();
// structure is index? ~ identifier ~ ":" ~ param_type
let prefix: FieldPrefix = match pairs.clone().next().unwrap().as_rule() {
Expand Down Expand Up @@ -433,7 +433,7 @@ impl HelixParser {
defaults,
name,
field_type,
loc: pair.loc(),
loc: pair.loc_with_filepath(filepath),
})
}

Expand All @@ -456,7 +456,7 @@ impl HelixParser {
(pair.loc(), pair.as_str().to_string())
};
let properties = match body_pairs.next() {
Some(pair) => Some(self.parse_properties(pair)?),
Some(pair) => Some(self.parse_properties(pair, filepath.clone())?),
None => None,
};

Expand All @@ -468,13 +468,13 @@ impl HelixParser {
loc: pair.loc_with_filepath(filepath),
})
}
pub(super) fn parse_properties(&self, pair: Pair<Rule>) -> Result<Vec<Field>, ParserError> {
pub(super) fn parse_properties(&self, pair: Pair<Rule>, filepath: String) -> Result<Vec<Field>, ParserError> {
pair.into_inner()
.find(|p| p.as_rule() == Rule::field_defs)
.map_or(Ok(Vec::new()), |field_defs| {
field_defs
.into_inner()
.map(|p| self.parse_field_def(p))
.map(|p| self.parse_field_def(p, filepath.clone()))
.collect::<Result<Vec<_>, _>>()
})
}
Expand Down
Loading