| 1 |
use core::slice; |
| 2 |
use std::cell::UnsafeCell; |
| 3 |
use std::marker::PhantomData; |
| 4 |
use std::mem; |
| 5 |
use std::ops::Range; |
| 6 |
use std::ptr::NonNull; |
| 7 |
|
| 8 |
use crate::node::NodeRaw; |
| 9 |
use crate::query::{Capture, Pattern, Query, QueryData}; |
| 10 |
use crate::{Input, IntoInput, Node, Tree}; |
| 11 |
|
| 12 |
enum QueryCursorData {} |
| 13 |
|
| 14 |
thread_local! { |
| 15 |
static CURSOR_CACHE: UnsafeCell<Vec<InactiveQueryCursor>> = UnsafeCell::new(Vec::with_capacity(8)); |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
unsafe fn with_cache<T>(f: impl FnOnce(&mut Vec<InactiveQueryCursor>) -> T) -> T { |
| 20 |
CURSOR_CACHE.with(|cache| f(&mut *cache.get())) |
| 21 |
} |
| 22 |
|
| 23 |
pub struct QueryCursor<'a, 'tree, I: Input> { |
| 24 |
query: &'a Query, |
| 25 |
ptr: NonNull<QueryCursorData>, |
| 26 |
tree: PhantomData<&'tree Tree>, |
| 27 |
input: I, |
| 28 |
} |
| 29 |
|
| 30 |
impl<'tree, I: Input> QueryCursor<'_, 'tree, I> { |
| 31 |
pub fn next_match(&mut self) -> Option<QueryMatch<'_, 'tree>> { |
| 32 |
let mut query_match = TSQueryMatch { |
| 33 |
id: 0, |
| 34 |
pattern_index: 0, |
| 35 |
capture_count: 0, |
| 36 |
captures: None, |
| 37 |
}; |
| 38 |
loop { |
| 39 |
let success = |
| 40 |
unsafe { ts_query_cursor_next_match(self.ptr.as_ptr(), &mut query_match) }; |
| 41 |
if !success { |
| 42 |
return None; |
| 43 |
} |
| 44 |
let matched_nodes: &[_] = match query_match.captures { |
| 45 |
None => &[], |
| 46 |
Some(ptr) => unsafe { |
| 47 |
slice::from_raw_parts(ptr.cast().as_ptr(), query_match.capture_count as usize) |
| 48 |
}, |
| 49 |
}; |
| 50 |
let satisfies_predicates = self |
| 51 |
.query |
| 52 |
.pattern_text_predicates(query_match.pattern_index) |
| 53 |
.iter() |
| 54 |
.all(|predicate| predicate.satisfied(&mut self.input, matched_nodes, self.query)); |
| 55 |
if satisfies_predicates { |
| 56 |
let res = QueryMatch { |
| 57 |
id: query_match.id, |
| 58 |
pattern: Pattern(query_match.pattern_index as u32), |
| 59 |
matched_nodes, |
| 60 |
query_cursor: unsafe { self.ptr.as_mut() }, |
| 61 |
_tree: PhantomData, |
| 62 |
}; |
| 63 |
return Some(res); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
pub fn next_matched_node(&mut self) -> Option<(QueryMatch<'_, 'tree>, MatchedNodeIdx)> { |
| 69 |
let mut query_match = TSQueryMatch { |
| 70 |
id: 0, |
| 71 |
pattern_index: 0, |
| 72 |
capture_count: 0, |
| 73 |
captures: None, |
| 74 |
}; |
| 75 |
let mut capture_idx = 0; |
| 76 |
loop { |
| 77 |
let success = unsafe { |
| 78 |
ts_query_cursor_next_capture(self.ptr.as_ptr(), &mut query_match, &mut capture_idx) |
| 79 |
}; |
| 80 |
if !success { |
| 81 |
return None; |
| 82 |
} |
| 83 |
let matched_nodes: &[_] = match query_match.captures { |
| 84 |
None => &[], |
| 85 |
Some(ptr) => unsafe { |
| 86 |
slice::from_raw_parts(ptr.cast().as_ptr(), query_match.capture_count as usize) |
| 87 |
}, |
| 88 |
}; |
| 89 |
let satisfies_predicates = self |
| 90 |
.query |
| 91 |
.pattern_text_predicates(query_match.pattern_index) |
| 92 |
.iter() |
| 93 |
.all(|predicate| predicate.satisfied(&mut self.input, matched_nodes, self.query)); |
| 94 |
if satisfies_predicates { |
| 95 |
let res = QueryMatch { |
| 96 |
id: query_match.id, |
| 97 |
pattern: Pattern(query_match.pattern_index as u32), |
| 98 |
matched_nodes, |
| 99 |
query_cursor: unsafe { self.ptr.as_mut() }, |
| 100 |
_tree: PhantomData, |
| 101 |
}; |
| 102 |
return Some((res, capture_idx)); |
| 103 |
} else { |
| 104 |
unsafe { |
| 105 |
ts_query_cursor_remove_match(self.ptr.as_ptr(), query_match.id); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
pub fn set_byte_range(&mut self, range: Range<u32>) { |
| 112 |
unsafe { |
| 113 |
ts_query_cursor_set_byte_range(self.ptr.as_ptr(), range.start, range.end); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
pub fn reuse(self) -> InactiveQueryCursor { |
| 118 |
let res = InactiveQueryCursor { ptr: self.ptr }; |
| 119 |
mem::forget(self); |
| 120 |
res |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
impl<I: Input> Drop for QueryCursor<'_, '_, I> { |
| 125 |
fn drop(&mut self) { |
| 126 |
unsafe { with_cache(|cache| cache.push(InactiveQueryCursor { ptr: self.ptr })) } |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
pub struct InactiveQueryCursor { |
| 132 |
ptr: NonNull<QueryCursorData>, |
| 133 |
} |
| 134 |
|
| 135 |
impl InactiveQueryCursor { |
| 136 |
#[must_use] |
| 137 |
pub fn new(range: Range<u32>, limit: u32) -> Self { |
| 138 |
let mut this = unsafe { |
| 139 |
with_cache(|cache| { |
| 140 |
cache.pop().unwrap_or_else(|| InactiveQueryCursor { |
| 141 |
ptr: NonNull::new_unchecked(ts_query_cursor_new()), |
| 142 |
}) |
| 143 |
}) |
| 144 |
}; |
| 145 |
this.set_byte_range(range); |
| 146 |
this.set_match_limit(limit); |
| 147 |
this |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
#[doc(alias = "ts_query_cursor_match_limit")] |
| 152 |
#[must_use] |
| 153 |
pub fn match_limit(&self) -> u32 { |
| 154 |
unsafe { ts_query_cursor_match_limit(self.ptr.as_ptr()) } |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#[doc(alias = "ts_query_cursor_set_match_limit")] |
| 160 |
pub fn set_match_limit(&mut self, limit: u32) { |
| 161 |
unsafe { |
| 162 |
ts_query_cursor_set_match_limit(self.ptr.as_ptr(), limit); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#[doc(alias = "ts_query_cursor_did_exceed_match_limit")] |
| 169 |
#[must_use] |
| 170 |
pub fn did_exceed_match_limit(&self) -> bool { |
| 171 |
unsafe { ts_query_cursor_did_exceed_match_limit(self.ptr.as_ptr()) } |
| 172 |
} |
| 173 |
|
| 174 |
pub fn set_byte_range(&mut self, range: Range<u32>) { |
| 175 |
unsafe { |
| 176 |
ts_query_cursor_set_byte_range(self.ptr.as_ptr(), range.start, range.end); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
pub fn execute_query<'a, 'tree, I: IntoInput>( |
| 181 |
self, |
| 182 |
query: &'a Query, |
| 183 |
node: &Node<'tree>, |
| 184 |
input: I, |
| 185 |
) -> QueryCursor<'a, 'tree, I::Input> { |
| 186 |
let ptr = self.ptr; |
| 187 |
unsafe { ts_query_cursor_exec(ptr.as_ptr(), query.raw.as_ref(), node.as_raw()) }; |
| 188 |
mem::forget(self); |
| 189 |
QueryCursor { |
| 190 |
query, |
| 191 |
ptr, |
| 192 |
tree: PhantomData, |
| 193 |
input: input.into_input(), |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
impl Default for InactiveQueryCursor { |
| 199 |
fn default() -> Self { |
| 200 |
Self::new(0..u32::MAX, u32::MAX) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl Drop for InactiveQueryCursor { |
| 205 |
fn drop(&mut self) { |
| 206 |
unsafe { ts_query_cursor_delete(self.ptr.as_ptr()) } |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
pub type MatchedNodeIdx = u32; |
| 211 |
|
| 212 |
#[repr(C)] |
| 213 |
#[derive(Debug, Clone)] |
| 214 |
pub struct MatchedNode<'tree> { |
| 215 |
pub node: Node<'tree>, |
| 216 |
pub capture: Capture, |
| 217 |
} |
| 218 |
|
| 219 |
pub struct QueryMatch<'cursor, 'tree> { |
| 220 |
id: u32, |
| 221 |
pattern: Pattern, |
| 222 |
matched_nodes: &'cursor [MatchedNode<'tree>], |
| 223 |
query_cursor: &'cursor mut QueryCursorData, |
| 224 |
_tree: PhantomData<&'tree super::Tree>, |
| 225 |
} |
| 226 |
|
| 227 |
impl std::fmt::Debug for QueryMatch<'_, '_> { |
| 228 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 229 |
f.debug_struct("QueryMatch") |
| 230 |
.field("id", &self.id) |
| 231 |
.field("pattern", &self.pattern) |
| 232 |
.field("matched_nodes", &self.matched_nodes) |
| 233 |
.finish_non_exhaustive() |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
impl<'tree> QueryMatch<'_, 'tree> { |
| 238 |
pub fn matched_nodes(&self) -> impl Iterator<Item = &MatchedNode<'tree>> { |
| 239 |
self.matched_nodes.iter() |
| 240 |
} |
| 241 |
|
| 242 |
pub fn nodes_for_capture(&self, capture: Capture) -> impl Iterator<Item = &Node<'tree>> { |
| 243 |
self.matched_nodes |
| 244 |
.iter() |
| 245 |
.filter(move |mat| mat.capture == capture) |
| 246 |
.map(|mat| &mat.node) |
| 247 |
} |
| 248 |
|
| 249 |
pub fn matched_node(&self, i: MatchedNodeIdx) -> &MatchedNode<'tree> { |
| 250 |
&self.matched_nodes[i as usize] |
| 251 |
} |
| 252 |
|
| 253 |
#[must_use] |
| 254 |
pub const fn id(&self) -> u32 { |
| 255 |
self.id |
| 256 |
} |
| 257 |
|
| 258 |
#[must_use] |
| 259 |
pub const fn pattern(&self) -> Pattern { |
| 260 |
self.pattern |
| 261 |
} |
| 262 |
|
| 263 |
#[doc(alias = "ts_query_cursor_remove_match")] |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
pub fn remove(self) { |
| 268 |
unsafe { |
| 269 |
ts_query_cursor_remove_match(self.query_cursor, self.id); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
#[repr(C)] |
| 275 |
#[derive(Debug)] |
| 276 |
struct TSQueryCapture { |
| 277 |
node: NodeRaw, |
| 278 |
index: u32, |
| 279 |
} |
| 280 |
|
| 281 |
#[repr(C)] |
| 282 |
#[derive(Debug)] |
| 283 |
struct TSQueryMatch { |
| 284 |
id: u32, |
| 285 |
pattern_index: u16, |
| 286 |
capture_count: u16, |
| 287 |
captures: Option<NonNull<TSQueryCapture>>, |
| 288 |
} |
| 289 |
|
| 290 |
extern "C" { |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
fn ts_query_cursor_next_capture( |
| 295 |
self_: *mut QueryCursorData, |
| 296 |
match_: &mut TSQueryMatch, |
| 297 |
capture_index: &mut u32, |
| 298 |
) -> bool; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
fn ts_query_cursor_next_match(self_: *mut QueryCursorData, match_: &mut TSQueryMatch) -> bool; |
| 305 |
fn ts_query_cursor_remove_match(self_: *mut QueryCursorData, match_id: u32); |
| 306 |
|
| 307 |
fn ts_query_cursor_delete(self_: *mut QueryCursorData); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
fn ts_query_cursor_new() -> *mut QueryCursorData; |
| 329 |
|
| 330 |
|
| 331 |
fn ts_query_cursor_exec(self_: *mut QueryCursorData, query: &QueryData, node: NodeRaw); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
ow |
| 339 |
|
| 340 |
|
| 341 |
fn ts_query_cursor_did_exceed_match_limit(self_: *const QueryCursorData) -> bool; |
| 342 |
fn ts_query_cursor_match_limit(self_: *const QueryCursorData) -> u32; |
| 343 |
fn ts_query_cursor_set_match_limit(self_: *mut QueryCursorData, limit: u32); |
| 344 |
|
| 345 |
|
| 346 |
fn ts_query_cursor_set_byte_range(self_: *mut QueryCursorData, start_byte: u32, end_byte: u32); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
#[cfg(test)] |
| 351 |
mod tests { |
| 352 |
use std::path::Path; |
| 353 |
|
| 354 |
use crate::{Grammar, InactiveQueryCursor, Input, Parser, Query}; |
| 355 |
|
| 356 |
struct StrInput<'a> { |
| 357 |
src: &'a str, |
| 358 |
cursor: &'a str, |
| 359 |
} |
| 360 |
|
| 361 |
impl<'a> StrInput<'a> { |
| 362 |
fn new(src: &'a str) -> Self { |
| 363 |
Self { src, cursor: src } |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
impl<'a> Input for StrInput<'a> { |
| 368 |
type Cursor = &'a str; |
| 369 |
|
| 370 |
fn cursor_at(&mut self, _offset: u32) -> &mut &'a str { |
| 371 |
|
| 372 |
|
| 373 |
self.cursor = self.src; |
| 374 |
&mut self.cursor |
| 375 |
} |
| 376 |
|
| 377 |
fn eq(&mut self, r1: std::ops::Range<u32>, r2: std::ops::Range<u32>) -> bool { |
| 378 |
let b = self.src.as_bytes(); |
| 379 |
b[r1.start as usize..r1.end as usize] == b[r2.start as usize..r2.end as usize] |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
fn python_grammar() -> Grammar { |
| 384 |
let so = Path::new(env!("CARGO_MANIFEST_DIR")).join("../test-grammars/python/python.so"); |
| 385 |
unsafe { Grammar::new("python", &so) }.expect("python grammar") |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
#[test] |
| 395 |
fn next_match_with_all_captures_disabled() { |
| 396 |
let grammar = python_grammar(); |
| 397 |
let mut query = Query::new(grammar, "(identifier) @name", |_, _| Ok(())).unwrap(); |
| 398 |
query.disable_capture("name"); |
| 399 |
|
| 400 |
let src = "x = 1"; |
| 401 |
let mut parser = Parser::new(); |
| 402 |
parser.set_grammar(grammar).unwrap(); |
| 403 |
let tree = parser.parse(StrInput::new(src), None).unwrap(); |
| 404 |
|
| 405 |
let root = tree.root_node(); |
| 406 |
let cursor = InactiveQueryCursor::new(0..src.len() as u32, u32::MAX); |
| 407 |
let mut cursor = cursor.execute_query(&query, &root, StrInput::new(src)); |
| 408 |
|
| 409 |
|
| 410 |
let mut count = 0; |
| 411 |
while let Some(mat) = cursor.next_match() { |
| 412 |
assert!(mat.matched_nodes().count() == 0); |
| 413 |
count += 1; |
| 414 |
} |
| 415 |
assert!(count > 0, "expected at least one match"); |
| 416 |
} |
| 417 |
} |