Skip to content

Commit 52dec2d

Browse files
authored
refactor: extract quantifier_operator_kind helper (#240)
1 parent d10b3c6 commit 52dec2d

1 file changed

Lines changed: 25 additions & 33 deletions

File tree

crates/plotnik-lib/src/compile/navigation.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,41 @@ pub fn is_down_nav(nav: Option<Nav>) -> bool {
101101
matches!(nav, Some(Nav::Down | Nav::DownSkip | Nav::DownExact))
102102
}
103103

104-
/// Check if expression is optional (?) or star (*) - patterns that can match zero times.
104+
/// Extract the operator kind from an expression if it's a quantifier.
105105
/// Unwraps CapturedExpr if present.
106-
pub fn is_skippable_quantifier(expr: &Expr) -> bool {
107-
use crate::parser::cst::SyntaxKind;
108-
109-
// Unwrap CapturedExpr to check the inner quantifier
106+
fn quantifier_operator_kind(expr: &Expr) -> Option<crate::parser::cst::SyntaxKind> {
110107
let expr = match expr {
111-
Expr::CapturedExpr(cap) => match cap.inner() {
112-
Some(inner) => inner,
113-
None => return false,
114-
},
108+
Expr::CapturedExpr(cap) => cap.inner()?,
115109
e => e.clone(),
116110
};
117111

118-
let Expr::QuantifiedExpr(q) = &expr else {
119-
return false;
120-
};
121-
let Some(op) = q.operator() else {
122-
return false;
123-
};
124-
matches!(
125-
op.kind(),
126-
SyntaxKind::Question
127-
| SyntaxKind::QuestionQuestion
128-
| SyntaxKind::Star
129-
| SyntaxKind::StarQuestion
130-
)
112+
let Expr::QuantifiedExpr(q) = &expr else { return None };
113+
Some(q.operator()?.kind())
114+
}
115+
116+
/// Check if expression is optional (?) or star (*) - patterns that can match zero times.
117+
pub fn is_skippable_quantifier(expr: &Expr) -> bool {
118+
use crate::parser::cst::SyntaxKind;
119+
quantifier_operator_kind(expr).is_some_and(|k| {
120+
matches!(
121+
k,
122+
SyntaxKind::Question
123+
| SyntaxKind::QuestionQuestion
124+
| SyntaxKind::Star
125+
| SyntaxKind::StarQuestion
126+
)
127+
})
131128
}
132129

133130
/// Syntactic check for star/plus quantifier (fallback when type info unavailable).
134131
pub fn is_star_or_plus_quantifier(expr: Option<&Expr>) -> bool {
135132
use crate::parser::cst::SyntaxKind;
136-
137-
let Some(Expr::QuantifiedExpr(q)) = expr else {
138-
return false;
139-
};
140-
let Some(op) = q.operator() else {
141-
return false;
142-
};
143-
matches!(
144-
op.kind(),
145-
SyntaxKind::Star | SyntaxKind::StarQuestion | SyntaxKind::Plus | SyntaxKind::PlusQuestion
146-
)
133+
expr.and_then(quantifier_operator_kind).is_some_and(|k| {
134+
matches!(
135+
k,
136+
SyntaxKind::Star | SyntaxKind::StarQuestion | SyntaxKind::Plus | SyntaxKind::PlusQuestion
137+
)
138+
})
147139
}
148140

149141
/// Determines if an expression creates a scope boundary when captured.

0 commit comments

Comments
 (0)