#![forbid(unsafe_code)]
pub use yolowsyntax::{
Limits, Outline, Section, Symbol, SymbolKind, compact, extract, langs, lexer, parser, recover,
scanner, stack, table, tree,
};
#[cfg(feature = "tools")]
pub use yolowsyntax::{cdata, convert, corpus, lexconv};
pub mod api {
pub use yolowsyntax::api::{Ev, ParseTree, VNode};
pub fn parse(
lang: &yolowsyntax::table::Language,
input: &[u8],
limits: yolowsyntax::Limits,
) -> ParseTree {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
yolowsyntax::api::parse(lang, input, limits)
}))
.unwrap_or_else(|_| {
yolowsyntax::api::parse(
lang,
input,
yolowsyntax::Limits {
max_nodes: 0,
..limits
},
)
})
}
}
pub fn outline(tag: &str, source: &str) -> Option<Outline> {
let mut out = yolowsyntax::outline(tag, source)?;
sanitize_sections(source, &mut out.sections);
Some(out)
}
fn ceil_utf8_boundary(s: &str, mut idx: usize) -> usize {
idx = idx.min(s.len());
while idx < s.len() && !s.is_char_boundary(idx) {
idx += 1;
}
idx
}
fn sanitize_sections(source: &str, sections: &mut Vec<Section>) {
let len = source.len();
if sections.is_empty() {
sections.push(Section {
start: 0,
end: len,
title: None,
});
return;
}
let mut cursor = 0usize;
let mut sanitized = Vec::with_capacity(sections.len());
for sec in sections.drain(..) {
let end = ceil_utf8_boundary(source, sec.end);
if end <= cursor {
continue;
}
sanitized.push(Section {
start: cursor,
end,
title: sec.title,
});
cursor = end;
}
match sanitized.last_mut() {
Some(last) => last.end = len,
None => sanitized.push(Section {
start: 0,
end: len,
title: None,
}),
}
*sections = sanitized;
}