use super::super::compiler::NfaBuilder;
use crate::core::{Pattern, RepetitionPreference, RepetitionQuantifier};
use datafusion_common::Result;
pub(crate) fn compile_quantifier(
b: &mut NfaBuilder,
inner: &Pattern,
quant: &RepetitionQuantifier,
) -> Result<(usize, usize)> {
let bounds = quant.bounds();
let min = bounds.min as usize;
let max = bounds.max.map(|max| max as usize);
let preference = bounds.preference;
if let Some(max) = max
&& min > max
{
return datafusion_common::plan_err!(
"MATCH_RECOGNIZE pattern quantifier lower bound {} exceeds upper bound {}",
min,
max
);
}
if min == 0 && max == Some(0) {
let s = b.new_state(false)?;
return Ok((s, s));
}
let entry = if min == 0 {
let entry = b.new_state(false)?;
Some(entry)
} else {
None
};
let mut first_start = entry;
let mut last_end = entry;
let mut last_copy_start = None;
for i in 0..min {
let (s, e) = b.build_pattern(inner)?;
if i == 0 {
first_start = Some(entry.unwrap_or(s));
}
if let Some(last_end_state) = last_end {
b.add_epsilon(last_end_state, s);
}
last_end = Some(e);
last_copy_start = Some(s);
}
if let Some(max_bound) = max {
let remaining = max_bound.saturating_sub(min);
if remaining > 0 {
last_end = Some(compile_bounded_optional_tail(
b,
inner,
last_end.expect("bounded tail requires an entry state"),
remaining,
preference,
)?);
}
}
if max.is_none() {
b.record_choice();
if last_copy_start.is_none() {
let (s, e) = b.build_pattern(inner)?;
last_copy_start = Some(s);
last_end = Some(e);
}
let exit = b.new_state(false)?;
let last_end_state =
last_end.expect("unbounded quantifier requires an end state");
let continuation =
last_copy_start.expect("unbounded quantifier requires a loop start");
for target in ordered_targets(preference, continuation, exit) {
b.add_epsilon(last_end_state, target);
}
b.record_loop_back_edge(
last_end_state,
continuation,
preference == RepetitionPreference::Possessive,
);
if preference == RepetitionPreference::Possessive {
b.record_possessive_decision(last_end_state, continuation, exit);
}
if min == 0 {
let entry = first_start.expect("zero-min quantifier requires an entry state");
for target in ordered_targets(preference, continuation, exit) {
b.add_epsilon(entry, target);
}
}
last_end = Some(exit);
}
Ok((
first_start.expect("quantifier compilation must produce a start state"),
last_end.expect("quantifier compilation must produce an end state"),
))
}
fn ordered_targets(
preference: RepetitionPreference,
continuation: usize,
exit: usize,
) -> [usize; 2] {
if preference == RepetitionPreference::Reluctant {
[exit, continuation]
} else {
[continuation, exit]
}
}
fn compile_bounded_optional_tail(
b: &mut NfaBuilder,
inner: &Pattern,
tail_start: usize,
remaining: usize,
preference: RepetitionPreference,
) -> Result<usize> {
b.record_choice();
let exit = b.new_state(false)?;
let mut last_end = tail_start;
for _ in 0..remaining {
let (s, e) = b.build_pattern(inner)?;
for target in ordered_targets(preference, s, exit) {
b.add_epsilon(last_end, target);
}
last_end = e;
}
b.add_epsilon(last_end, exit);
Ok(exit)
}