Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add query validation #507

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
40 changes: 36 additions & 4 deletions src/utilities/tree_sitter_utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,43 @@ impl TSQuery {
impl Validator for TSQuery {
fn validate(&self) -> Result<(), String> {
let mut parser = get_ts_query_parser();
parser
let tree = parser
.parse(self.get_query(), None)
.filter(|x| number_of_errors(&x.root_node()) == 0)
.map(|_| Ok(()))
.unwrap_or(Err(format!("Cannot parse - {}", self.get_query())))
.filter(|x| number_of_errors(&x.root_node()) == 0);

if tree.is_none() {
return Err(format!("Cannot parse - {}", self.get_query()));
}

let mut first_child_node: Node;

let _tree = tree.unwrap();
if let Some(n) = _tree.root_node().named_child(0) {
first_child_node = n;
} else {
return Ok(());
}

loop {
let kind = first_child_node.kind();
if kind == "named_node" || kind == "list" {
break;
}
if kind == "grouping" {
if let Some(c) = first_child_node.named_child(0) {
first_child_node = c;
continue;
} else {
return Err(
"The first node of the query should be a group, named node or a list".to_string(),
);
}
}
return Err(
"The first node of the query should be a group, named node or a list".to_string(),
);
}
Ok(())
}
}

Expand Down