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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::query::Language;
use anyhow::{Context, Result};
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::collections::HashSet;
use std::fmt::{self, Display};
use std::fs;
use std::path::{Path, PathBuf};
use tree_sitter::{Parser, Point, Query, QueryCursor};
/// Extractor for extracting syntax information of program
#[derive(Debug)]
pub struct Extractor {
/// Language configuration
language: Language,
/// Language for tree_sitter
ts_language: tree_sitter::Language,
/// Tree_sitter query: a set of patterns that match nodes in a syntax tree.
query: Query,
/// Names of the captures used in the query.
captures: Vec<String>,
/// Ignored names with '_'
ignores: HashSet<usize>,
}
impl Extractor {
/// Build a new Extractor
///
/// # Arguments
///
/// * `language` - the language of source code
///
/// * `query` - tree_sitter query
///
/// # Returns
///
/// * `Extractor` object
///
/// # Example
///
/// ```no_run
/// # fn main() -> anyhow::Result<()> {
/// use rust_hero::query::{Language,Extractor};
///
/// let lang = Language::Rust;
/// let query = lang
/// .parse_query("(import_clause (upper_case_qid)@import)")
/// .unwrap();
/// let extractor = Extractor::new(lang, query);
/// # Ok(())
/// # }
/// ```
pub fn new(language: Language, query: Query) -> Extractor {
let captures = query.capture_names().to_vec();
let mut ignores = HashSet::default();
captures.iter().enumerate().for_each(|(i, name)| {
if name.starts_with('_') {
ignores.insert(i);
}
});
Extractor {
ts_language: (&language).language(),
language,
query,
captures,
ignores,
}
}
/// Get the language of Extractor
pub fn language(&self) -> &Language {
&self.language
}
/// Extracted query information from one source file
pub fn extract_from_file(
&self,
path: &Path,
parser: &mut Parser,
) -> Result<Option<ExtractedFile>> {
let source = fs::read(&path).context("could not read file")?;
self.extract_from_text(Some(path), &source, parser)
}
/// Extracted query information from one fragment program
///
/// # Arguments
///
/// * `path` - Option: the path of source file
///
/// * `source` - fragment program
///
/// * `parser` - tree_sitter Parser
///
/// # Returns
///
/// * `ExtractedFile` object
///
/// # Example
///
/// ```
/// # fn main() -> anyhow::Result<()> {
/// use rust_hero::query::{Language,Extractor};
/// use tree_sitter::Parser;
///
/// let lang = Language::Rust;
/// let query = lang
/// .parse_query("(function_item (identifier) @id) @function")
/// .unwrap();
/// let extractor = Extractor::new(lang, query);
/// let extracted = extractor
/// .extract_from_text(None, b"fn main(){println!(\"hello rust_hero\");}", &mut Parser::new())
/// // From Result<Option<ExtractedFile>>
/// .unwrap()
/// // From Option<ExtractedFile>
/// .unwrap();
///
/// println!("{:?},{:?},{:?}",extracted.matches.len(),extracted.matches[0].name,extracted.matches[0].text);
/// assert_eq!(extracted.matches.len(), 2);
/// assert_eq!(extracted.matches[0].name, "function");
/// assert_eq!(extracted.matches[0].text, "fn main(){println!(\"hello rust_hero\");}");
/// # Ok(())
/// # }
/// ```
pub fn extract_from_text(
&self,
path: Option<&Path>,
source: &[u8],
parser: &mut Parser,
) -> Result<Option<ExtractedFile>> {
parser
.set_language(self.ts_language)
.context("could not set language")?;
let tree = parser
.parse(&source, None)
// note: this could be a timeout or cancellation, but we don't set
// that so we know it's always a language error. Buuuut we also
// always set the language above so if this happens we also know
// it's an internal error.
.context(
"could not parse to a tree. This is an internal error and should be reported.",
)?;
let mut cursor = QueryCursor::new();
let extracted_matches = cursor
.matches(&self.query, tree.root_node(), source)
.flat_map(|query_match| query_match.captures)
// note: the casts here could potentially break if run on a 16-bit
// microcontroller. I don't think this is a huge problem, though,
// since even the gnarliest queries I've written have something on
// the order of 20 matches. Nowhere close to 2^16!
.filter(|capture| !self.ignores.contains(&(capture.index as usize)))
.map(|capture| {
let name = &self.captures[capture.index as usize];
let node = capture.node;
let text = match node
.utf8_text(source)
.map(|unowned| unowned.to_string())
.context("could not extract text from capture")
{
Ok(text) => text,
Err(problem) => return Err(problem),
};
Ok(ExtractedMatch {
kind: node.kind(),
name,
text,
start: node.start_position(),
end: node.end_position(),
})
})
.collect::<Result<Vec<ExtractedMatch>>>()?;
if extracted_matches.is_empty() {
Ok(None)
} else {
Ok(Some(ExtractedFile {
file: path.map(|p| p.to_owned()),
file_type: self.language.to_string(),
matches: extracted_matches,
}))
}
}
}
/// Extracted query from source file
#[derive(Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExtractedFile<'query> {
/// Extracted source file
pub file: Option<PathBuf>,
/// Language
pub file_type: String,
/// A set of patterns that match nodes in a syntax tree.
pub matches: Vec<ExtractedMatch<'query>>,
}
impl<'query> Display for ExtractedFile<'query> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: is there a better way to do this unwrapping? This implementation
// turns non-UTF-8 paths into "NON-UTF8 FILENAME". I don't know exactly
// what circumstances that could happen in... maybe we should just wait
// for bug reports?
let filename = self
.file
.as_ref()
.map(|f| f.to_str().unwrap_or("NON-UTF8 FILENAME"))
.unwrap_or("NO FILE");
for extraction in &self.matches {
writeln!(
f,
"{}:{}:{}:{}:{}",
filename,
extraction.start.row + 1,
extraction.start.column + 1,
extraction.name,
extraction.text
)?
}
Ok(())
}
}
/// Pattern matching nodes in a syntax tree.
#[derive(Debug, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExtractedMatch<'query> {
/// Pattern type
kind: &'static str,
/// Pattern name
pub name: &'query str,
/// Fragment program
pub text: String,
/// Start cordinate of current text
#[serde(serialize_with = "serialize_point")]
pub start: Point,
/// End cordinate of current text
#[serde(serialize_with = "serialize_point")]
pub end: Point,
}
fn serialize_point<S>(point: &Point, sz: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut out = sz.serialize_struct("Point", 2)?;
out.serialize_field("row", &(point.row + 1))?;
out.serialize_field("column", &(point.column + 1))?;
out.end()
}