Improve capture quantifier computation

Compute quantifiers in a bottom-up manner, which allows more precise
results for alternations, where the quantifiers are now precisly joined.
This commit is contained in:
Hendrik van Antwerpen 2021-12-02 19:04:49 +01:00
parent 9bac066330
commit 1f1a449c76
5 changed files with 506 additions and 78 deletions

View file

@ -107,10 +107,11 @@ pub struct TSQueryCapture {
pub node: TSNode,
pub index: u32,
}
pub const TSQuantifier_One: TSQuantifier = 0;
pub const TSQuantifier_OneOrMore: TSQuantifier = 1;
pub const TSQuantifier_ZeroOrOne: TSQuantifier = 2;
pub const TSQuantifier_ZeroOrMore: TSQuantifier = 3;
pub const TSQuantifier_Zero: TSQuantifier = 0;
pub const TSQuantifier_ZeroOrOne: TSQuantifier = 1;
pub const TSQuantifier_ZeroOrMore: TSQuantifier = 2;
pub const TSQuantifier_One: TSQuantifier = 3;
pub const TSQuantifier_OneOrMore: TSQuantifier = 4;
pub type TSQuantifier = u32;
#[repr(C)]
#[derive(Debug, Copy, Clone)]

View file

@ -98,7 +98,7 @@ pub struct TreeCursor<'a>(ffi::TSTreeCursor, PhantomData<&'a ()>);
pub struct Query {
ptr: NonNull<ffi::TSQuery>,
capture_names: Vec<String>,
capture_quantifiers: Vec<Quantifier>,
capture_quantifiers: Vec<CaptureQuantifier>,
text_predicates: Vec<Box<[TextPredicate]>>,
property_settings: Vec<Box<[QueryProperty]>>,
property_predicates: Vec<Box<[(QueryProperty, bool)]>>,
@ -107,20 +107,20 @@ pub struct Query {
/// A quantifier for captures
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Quantifier {
pub enum CaptureQuantifier {
One,
OneOrMore,
ZeroOrOne,
ZeroOrMore,
}
impl From<ffi::TSQuantifier> for Quantifier {
impl From<ffi::TSQuantifier> for CaptureQuantifier {
fn from(value: ffi::TSQuantifier) -> Self {
match value {
ffi::TSQuantifier_One => Quantifier::One,
ffi::TSQuantifier_OneOrMore => Quantifier::OneOrMore,
ffi::TSQuantifier_ZeroOrOne => Quantifier::ZeroOrOne,
ffi::TSQuantifier_ZeroOrMore => Quantifier::ZeroOrMore,
ffi::TSQuantifier_One => CaptureQuantifier::One,
ffi::TSQuantifier_OneOrMore => CaptureQuantifier::OneOrMore,
ffi::TSQuantifier_ZeroOrOne => CaptureQuantifier::ZeroOrOne,
ffi::TSQuantifier_ZeroOrMore => CaptureQuantifier::ZeroOrMore,
_ => panic!("Unrecognized quantifier: {}", value),
}
}
@ -1550,7 +1550,7 @@ impl Query {
}
/// Get the quantifiers of the captures used in the query.
pub fn capture_quantifiers(&self) -> &[Quantifier] {
pub fn capture_quantifiers(&self) -> &[CaptureQuantifier] {
&self.capture_quantifiers
}