Skip to content
Open
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
45 changes: 31 additions & 14 deletions helper/src/rust/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use tree_sitter::{Parser, Node};
use serde_json::{Map, Value};
use tree_sitter::{Node, Parser};

use crate::base_parser::BaseParser;
use crate::traverse;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl<'a> BaseParser for RustParser<'a> {
new_line = sibling_node.start_position().row + 1;
temp_node = sibling_node;
} else {
break;
break;
}
}

Expand All @@ -55,12 +55,19 @@ impl<'a> RustParser<'a> {

let tree = parser.parse(code, None).unwrap();

Self { code, tree, line, node_types }
Self {
code,
tree,
line,
node_types,
}
}

fn parse_node(&self, node: &Node) -> Option<Result<Map<String, Value>, String>> {
for child_node in traverse::PreOrder::new(node.walk()) {
if child_node.start_position().row + 1 == *self.line && self.node_types.contains(&child_node.kind()) {
if child_node.start_position().row + 1 == *self.line
&& self.node_types.contains(&child_node.kind())
{
return match child_node.kind() {
"function_item" => Some(self.parse_function(&child_node)),
_ => None,
Expand All @@ -77,20 +84,27 @@ impl<'a> RustParser<'a> {
for child_node in node.children(&mut node.walk()) {
match child_node.kind() {
"function_modifiers" => {
let unsafe_modifier = child_node.children(&mut child_node.walk()).filter(|node| node.kind() == "unsafe").next();
let unsafe_modifier = child_node
.children(&mut child_node.walk())
.filter(|node| node.kind() == "unsafe")
.next();
if unsafe_modifier.is_some() {
tokens.insert("has_unsafe".to_string(), Value::Bool(true));
}
},
}
"generic_type" => {
let result_enum = child_node.children(&mut child_node.walk())
.filter(|node| node.kind() == "type_identifier" && self.get_node_text(&node) == "Result")
let result_enum = child_node
.children(&mut child_node.walk())
.filter(|node| {
node.kind() == "type_identifier"
&& self.get_node_text(&node) == "Result"
})
.next();

if result_enum.is_some() {
tokens.insert("has_errors".to_string(), Value::Bool(true));
}
},
}
"parameters" => {
let mut params = Vec::new();

Expand Down Expand Up @@ -118,12 +132,12 @@ impl<'a> RustParser<'a> {
if !params.is_empty() {
tokens.insert("params".to_string(), Value::Array(params));
}
},
}
"block" => {
let has_panics = self.has_panics(&child_node);
tokens.insert("has_panics".to_string(), Value::Bool(has_panics));
},
_ => {},
}
_ => {}
}
}

Expand All @@ -132,12 +146,15 @@ impl<'a> RustParser<'a> {

fn has_panics(&self, node: &Node) -> bool {
if node.kind() == "macro_invocation" {
let macro_name = node
let macro_name = match node
.children(&mut node.walk())
.filter(|node| node.kind() == "identifier")
.next()
.and_then(|node| Some(self.get_node_text(&node)))
.unwrap();
{
Some(name) => name,
None => return false,
};

if macro_name == "panic" {
return true;
Expand Down