use oneyaml_core::Severity;
use super::SyntaxKind as K;
use super::arena::Flat;
pub(super) const LONG_RUN: usize = 64;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub(super) struct StreamFacts {
trivia_runs: Vec<(u32, u32)>,
directive_errors: Vec<u32>,
mixed_directive_errors: Vec<u32>,
docs: Option<Vec<u32>>,
}
const fn is_directive(k: K) -> bool {
matches!(k, K::YamlDirective | K::TagDirective | K::ReservedDirective)
}
impl StreamFacts {
#[cfg(test)]
pub(super) fn heap_bytes(&self) -> usize {
self.trivia_runs
.capacity()
.saturating_mul(size_of::<(u32, u32)>())
.saturating_add(
self.directive_errors
.capacity()
.saturating_mul(size_of::<u32>()),
)
.saturating_add(
self.mixed_directive_errors
.capacity()
.saturating_mul(size_of::<u32>()),
)
.saturating_add(
self.docs
.as_ref()
.map_or(0, |d| d.capacity().saturating_mul(size_of::<u32>())),
)
}
pub(super) fn of(
flat: Flat<'_>,
children: impl Iterator<Item = usize> + Clone,
errors: bool,
) -> Self {
let mut facts = Self::default();
let (mut docs, mut stray) = (0usize, false);
let all = children.clone();
let mut run: Option<(usize, usize)> = None;
let close = |run: Option<(usize, usize)>, out: &mut Vec<(u32, u32)>| {
if let Some((a, b)) = run {
if b.saturating_sub(a) > LONG_RUN {
if let (Ok(a), Ok(b)) = (u32::try_from(a), u32::try_from(b)) {
out.push((a, b));
}
}
}
};
for i in children {
let Some(k) = flat.kind(i) else { break };
if k.is_trivia() {
run = match run {
Some((a, b)) if b == i => Some((a, i.saturating_add(1))),
other => {
close(other, &mut facts.trivia_runs);
Some((i, i.saturating_add(1)))
}
};
continue;
}
close(run.take(), &mut facts.trivia_runs);
if k == K::Document {
docs = docs.saturating_add(1);
} else {
stray = true;
}
if errors && k == K::Document {
if let Ok(j) = u32::try_from(i) {
if Self::has_directive_error(flat, i) {
facts.directive_errors.push(j);
}
if Self::mixed(flat, i) {
facts.mixed_directive_errors.push(j);
}
}
}
}
close(run, &mut facts.trivia_runs);
if stray {
let mut list = Vec::with_capacity(docs);
list.extend(
all.filter(|i| flat.kind(*i) == Some(K::Document))
.filter_map(|i| u32::try_from(i).ok()),
);
facts.docs = Some(list);
}
facts
}
fn has_directive_error(flat: Flat<'_>, doc: usize) -> bool {
let end = flat.end(doc);
let list = flat.diagnostics;
let lo = list.partition_point(|(n, _)| (*n as usize) < doc);
let hi = list.partition_point(|(n, _)| (*n as usize) < end).max(lo);
for (e, d) in list.get(lo..hi).unwrap_or_default() {
let e = *e as usize;
if d.severity() != Severity::Error || flat.parent(e) != Some(doc) {
continue;
}
let mut at = Some(e);
while let Some(x) = at {
match flat.kind(x) {
Some(K::DocumentStartMarker) => return true,
Some(k) if is_directive(k) || k.is_trivia() || k == K::Error => {}
_ => break,
}
at = flat.next_sibling(x);
}
}
false
}
fn mixed(flat: Flat<'_>, doc: usize) -> bool {
let end = flat.end(doc);
let list = flat.diagnostics;
let lo = list.partition_point(|(n, _)| (*n as usize) < doc);
let hi = list.partition_point(|(n, _)| (*n as usize) < end).max(lo);
let mut other = false;
for (_, d) in list.get(lo..hi).unwrap_or_default() {
if d.severity() != Severity::Error {
continue;
}
if super::validate::is_directive_error(d.code()) {
if other {
return true;
}
} else {
other = true;
}
}
false
}
pub(super) fn run_start(&self, local: usize) -> Option<usize> {
let k = self
.trivia_runs
.partition_point(|(a, _)| (*a as usize) <= local);
let (a, b) = *self.trivia_runs.get(k.checked_sub(1)?)?;
(local < b as usize).then_some(a as usize)
}
pub(super) fn run_end(&self, local: usize) -> Option<usize> {
let k = self
.trivia_runs
.partition_point(|(a, _)| (*a as usize) <= local);
let (_, b) = *self.trivia_runs.get(k.checked_sub(1)?)?;
let b = (b as usize).checked_sub(1)?;
(local <= b).then_some(b)
}
fn top(flat: Flat<'_>, mut x: usize) -> Option<usize> {
let stream = (flat.kind(0) == Some(K::Stream)).then_some(0usize);
let mut guard = flat.len();
while guard > 0 {
guard = guard.saturating_sub(1);
match flat.parent(x) {
p if p == stream => return (Some(x) != stream).then_some(x),
Some(p) => x = p,
None => return None,
}
}
None
}
#[cfg(test)]
pub(super) const fn has_doc_list(&self) -> bool {
self.docs.is_some()
}
pub(super) fn holds_doc(&self, flat: Flat<'_>, a: u32, b: u32) -> bool {
self.first_doc_in(flat, a, b).is_some()
}
pub(super) fn last_doc_in(&self, flat: Flat<'_>, a: u32, b: u32) -> Option<u32> {
if let Some(list) = &self.docs {
let hi = list.partition_point(|d| *d < b);
return list.get(hi.checked_sub(1)?).copied().filter(|d| *d >= a);
}
let a = a as usize;
let b = (b as usize).min(flat.len());
if b <= a {
return None;
}
let mut t = Self::top(flat, b.checked_sub(1)?)?;
let mut guard = flat.len();
while guard > 0 && t >= a {
guard = guard.saturating_sub(1);
let k = flat.kind(t)?;
if k == K::Document {
return u32::try_from(t).ok();
}
let first = if k.is_trivia() {
self.run_start(t).unwrap_or(t)
} else {
t
};
t = Self::top(flat, first.checked_sub(1)?)?;
}
None
}
pub(super) fn first_doc_in(&self, flat: Flat<'_>, a: u32, b: u32) -> Option<u32> {
if let Some(list) = &self.docs {
let lo = list.partition_point(|d| *d < a);
return list.get(lo).copied().filter(|d| *d < b);
}
let a = a as usize;
let b = (b as usize).min(flat.len());
if b <= a {
return None;
}
let mut c = match Self::top(flat, a) {
Some(t) if t == a => a,
Some(t) => flat.end(t),
None => a.saturating_add(1),
};
let mut guard = flat.len();
while guard > 0 && c < b {
guard = guard.saturating_sub(1);
let k = flat.kind(c)?;
if k == K::Document {
return u32::try_from(c).ok();
}
let last = if k.is_trivia() {
self.run_end(c).unwrap_or(c)
} else {
c
};
c = flat.end(last).max(last.saturating_add(1));
}
None
}
pub(super) fn mixed_directive_error(&self, local: usize) -> bool {
u32::try_from(local).is_ok_and(|l| self.mixed_directive_errors.binary_search(&l).is_ok())
}
pub(super) fn directive_error(&self, local: usize) -> bool {
u32::try_from(local).is_ok_and(|l| self.directive_errors.binary_search(&l).is_ok())
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::indexing_slicing)]
use super::{K, StreamFacts};
use crate::tree::SyntaxTree;
#[test]
fn long_runs_and_directive_errors_are_recorded() {
let src = format!("a: 1\n{}--- b\n", "# c\n".repeat(40));
let t = SyntaxTree::parse(src);
assert_eq!(t.facts.trivia_runs.len(), 1);
let (a, b) = t.facts.trivia_runs[0];
assert_eq!(b - a, 81);
assert_eq!(t.facts.run_start(b as usize - 1), Some(a as usize));
assert_eq!(t.facts.run_start(b as usize), None);
assert_eq!(t.facts.run_end(a as usize), Some(b as usize - 1));
assert_eq!(t.facts.run_end(b as usize - 1), Some(b as usize - 1));
assert_eq!(t.facts.run_end(b as usize), None);
assert_eq!(t.facts.run_end(a as usize - 1), None);
assert!(t.facts.directive_errors.is_empty());
let short = SyntaxTree::parse("a: 1\n# c\n--- b\n");
assert!(short.facts.trivia_runs.is_empty());
for (src, n) in [
("%YAML 2.0\n%FOO bar\n---\na: 1\n", 1),
("%TAG !a! x\n%TAG !a! y\n%FOO\n--- a\n", 1),
("%YAML 1.2\n---\na: [1\n", 0),
("--- a\n...\n%YAML 2.0\n--- b\n", 1),
] {
let t = SyntaxTree::parse(src);
assert_eq!(t.facts.directive_errors.len(), n, "{src:?}");
}
}
#[test]
fn documents_are_found_with_and_without_a_list() {
let long = "# between\n".repeat(80);
let inputs = [
String::new(),
"# only a comment\n".to_owned(),
"a: 1\n".to_owned(),
"a: 1\n---\nb: 2\n--- c\n".to_owned(),
format!("%YAML 1.2\n--- a\n...\n{long}--- b\n...\n# t\n"),
format!("\u{feff}--- a\n{long}\n---\n- b\n...\n{long}"),
format!("a\n...\n...\n# x\n{long}...\n--- b\n...\n"),
"a: 1\n...\n]\n# c\n--- b\n".to_owned(),
];
let (mut with_list, mut multi_without) = (0, 0);
for src in &inputs {
let t = SyntaxTree::parse(src.as_str());
let flat = t.base_flat();
let n = flat.len();
let kids: Vec<usize> = flat
.roots()
.flat_map(|r| {
let mut out = Vec::new();
let mut c = flat.first_child(r);
while let Some(x) = c {
out.push(x);
c = flat.next_sibling(x);
}
out
})
.collect();
let docs: Vec<u32> = kids
.iter()
.filter(|c| flat.kind(**c) == Some(K::Document))
.map(|c| *c as u32)
.collect();
let stray = kids.iter().any(|c| {
flat.kind(*c)
.is_some_and(|k| k != K::Document && !k.is_trivia())
});
assert_eq!(t.facts.docs.is_some(), stray, "{src:?}");
with_list += usize::from(stray);
multi_without += usize::from(!stray && docs.len() > 1);
if !stray {
assert_eq!(t.facts.heap_bytes() - t.facts.trivia_runs.capacity() * 8, 0);
}
let walk = StreamFacts {
docs: None,
..t.facts.clone()
};
let n = u32::try_from(n).unwrap();
for a in 0..=n {
for b in (a..=n).step_by(((n as usize) / 50).max(1)).chain([n]) {
let first = docs.iter().copied().find(|d| a <= *d && *d < b);
let last = docs.iter().copied().rev().find(|d| a <= *d && *d < b);
for f in [&t.facts, &walk] {
assert_eq!(f.first_doc_in(flat, a, b), first, "{src:?} {a}..{b}");
assert_eq!(f.last_doc_in(flat, a, b), last, "{src:?} {a}..{b}");
assert_eq!(f.holds_doc(flat, a, b), first.is_some());
}
}
}
}
assert!(
with_list >= 2 && multi_without >= 3,
"{with_list} {multi_without}"
);
}
}