This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.rs
162 lines (148 loc) · 6.2 KB
/
queries.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{AmlError, FunctionInfo, Location, Result, FUNC_NAME_CAPTURE};
use log::error;
use tree_sitter::{Parser, Query};
use tree_sitter_go::language;
const PACK_NAME_CAPTURE: &str = "pack.name";
fn new_parser() -> Result<Parser> {
let mut parser = Parser::new();
parser.set_language(language())?;
Ok(parser)
}
/// Query wrapper for "all autometrics functions in source"
#[derive(Debug)]
pub(super) struct AmQuery {
query: Query,
/// Index of the capture for a function name.
func_name_idx: u32,
/// Index of the capture for the package name.
mod_name_idx: u32,
}
impl AmQuery {
/// Failible constructor.
///
/// The constructor only fails if the given tree-sitter query does not have the
/// necessary named captures.
pub fn try_new() -> Result<Self> {
let query = Query::new(
language(),
include_str!("../../runtime/queries/go/autometrics.scm"),
)?;
let func_name_idx = query
.capture_index_for_name(FUNC_NAME_CAPTURE)
.ok_or_else(|| AmlError::MissingNamedCapture(FUNC_NAME_CAPTURE.to_string()))?;
let mod_name_idx = query
.capture_index_for_name(PACK_NAME_CAPTURE)
.ok_or_else(|| AmlError::MissingNamedCapture(PACK_NAME_CAPTURE.to_string()))?;
Ok(Self {
query,
func_name_idx,
mod_name_idx,
})
}
pub fn list_function_names(&self, file_name: &str, source: &str) -> Result<Vec<FunctionInfo>> {
let mut parser = new_parser()?;
let parsed_source = parser.parse(source, None).ok_or(AmlError::Parsing)?;
let mut cursor = tree_sitter::QueryCursor::new();
cursor
.matches(&self.query, parsed_source.root_node(), source.as_bytes())
.filter_map(|capture| -> Option<Result<FunctionInfo>> {
let module = capture
.nodes_for_capture_index(self.mod_name_idx)
.next()
.map(|node| node.utf8_text(source.as_bytes()).map(ToString::to_string))?;
let fn_node = capture.nodes_for_capture_index(self.func_name_idx).next()?;
let fn_name = fn_node
.utf8_text(source.as_bytes())
.map(ToString::to_string);
let start = fn_node.start_position();
let end = fn_node.end_position();
let instrumentation = Some(Location::from((file_name, start, end)));
let definition = Some(Location::from((file_name, start, end)));
match (module, fn_name) {
(Ok(module), Ok(function)) => Some(Ok(FunctionInfo {
id: (module, function).into(),
instrumentation,
definition,
})),
(Err(err_mod), _) => {
error!("could not fetch the package name: {err_mod}");
Some(Err(AmlError::InvalidText))
}
(_, Err(err_fn)) => {
error!("could not fetch the package name: {err_fn}");
Some(Err(AmlError::InvalidText))
}
}
})
.collect::<std::result::Result<Vec<_>, _>>()
}
}
/// Query wrapper for "all functions in source"
#[derive(Debug)]
pub(super) struct AllFunctionsQuery {
query: Query,
/// Index of the capture for a function name.
func_name_idx: u32,
/// Index of the capture for the package name.
mod_name_idx: u32,
}
impl AllFunctionsQuery {
/// Failible constructor.
///
/// The constructor only fails if the given tree-sitter query does not have the
/// necessary named captures.
pub fn try_new() -> Result<Self> {
let query = Query::new(
language(),
include_str!("../../runtime/queries/go/all_functions.scm"),
)?;
let func_name_idx = query
.capture_index_for_name(FUNC_NAME_CAPTURE)
.ok_or_else(|| AmlError::MissingNamedCapture(FUNC_NAME_CAPTURE.to_string()))?;
let mod_name_idx = query
.capture_index_for_name(PACK_NAME_CAPTURE)
.ok_or_else(|| AmlError::MissingNamedCapture(PACK_NAME_CAPTURE.to_string()))?;
Ok(Self {
query,
func_name_idx,
mod_name_idx,
})
}
pub fn list_function_names(&self, file_name: &str, source: &str) -> Result<Vec<FunctionInfo>> {
let mut parser = new_parser()?;
let parsed_source = parser.parse(source, None).ok_or(AmlError::Parsing)?;
let mut cursor = tree_sitter::QueryCursor::new();
cursor
.matches(&self.query, parsed_source.root_node(), source.as_bytes())
.filter_map(|capture| -> Option<Result<FunctionInfo>> {
let module = capture
.nodes_for_capture_index(self.mod_name_idx)
.next()
.map(|node| node.utf8_text(source.as_bytes()).map(ToString::to_string))?;
let fn_node = capture.nodes_for_capture_index(self.func_name_idx).next()?;
let fn_name = fn_node
.utf8_text(source.as_bytes())
.map(ToString::to_string);
let start = fn_node.start_position();
let end = fn_node.end_position();
let instrumentation = None;
let definition = Some(Location::from((file_name, start, end)));
match (module, fn_name) {
(Ok(module), Ok(function)) => Some(Ok(FunctionInfo {
id: (module, function).into(),
instrumentation,
definition,
})),
(Err(err_mod), _) => {
error!("could not fetch the package name: {err_mod}");
Some(Err(AmlError::InvalidText))
}
(_, Err(err_fn)) => {
error!("could not fetch the package name: {err_fn}");
Some(Err(AmlError::InvalidText))
}
}
})
.collect::<std::result::Result<Vec<_>, _>>()
}
}