use oneyaml_core::Span;
use oneyaml_core::code::{
BLOCK_SCALAR_INDENTATION, DUPLICATE_PROPERTY, INVALID_BLOCK_HEADER, SCALAR_TOO_LONG,
TAB_INDENTATION,
};
use crate::event::{BlockHeader, Chomping, Event, EventKind, Properties, ScalarStyle, TriviaKind};
use super::scalar::{Count, Sink};
use super::scan::{
any_marker_at, blank_or_end, break_len, break_or_end, byte, first_mark, line_end, skip_white,
};
use super::{NodeCtx, Parser};
fn prefix_break_at(src: &str, i: usize) -> bool {
any_marker_at(src, i) || src.get(i..).is_some_and(|r| r.starts_with('\u{feff}'))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Header {
pub(super) end: usize,
pub(super) chomping: Chomping,
pub(super) indicator: Option<u8>,
}
pub(super) fn header(src: &str, start: usize) -> Option<Header> {
let mut i = start.saturating_add(1);
let mut chomping = None;
let mut indicator = None;
loop {
match byte(src, i) {
Some(d @ b'1'..=b'9') if indicator.is_none() => {
indicator = Some(d.saturating_sub(b'0'));
}
Some(b'+') if chomping.is_none() => chomping = Some(Chomping::Keep),
Some(b'-') if chomping.is_none() => chomping = Some(Chomping::Strip),
_ => break,
}
i = i.saturating_add(1);
}
blank_or_end(src, i).then_some(Header {
end: i,
chomping: chomping.unwrap_or(Chomping::Clip),
indicator,
})
}
#[derive(Debug, PartialEq, Eq)]
pub(super) struct Body<S> {
pub(super) indent: usize,
pub(super) end: usize,
pub(super) value: S,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum BodyError {
LeadingIndentation { line: usize },
Tab { line: usize },
TooLong { line: usize },
}
struct Line {
start: usize,
spaces: usize,
end: usize,
next: usize,
has_break: bool,
all_spaces: bool,
}
fn line_at(src: &str, start: usize) -> Line {
let end = line_end(src, start);
let spaces = src
.as_bytes()
.get(start..end)
.map_or(0, |r| r.iter().take_while(|b| **b == b' ').count());
let blen = break_len(src, end);
Line {
start,
spaces,
end,
next: end.saturating_add(blen),
has_break: blen > 0,
all_spaces: start.saturating_add(spaces) == end,
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Indent {
pub(super) min: usize,
pub(super) known: Option<usize>,
}
impl Indent {
pub(super) fn of(parent: Option<usize>, header: Header) -> Self {
Self {
min: parent.map_or(0, |n| n.saturating_add(1)),
known: header.indicator.map(|m| {
parent.map_or(usize::from(m).saturating_sub(1), |n| {
n.saturating_add(usize::from(m))
})
}),
}
}
}
pub(super) fn body<S: Sink>(
src: &str,
body: usize,
indentation: Indent,
chomping: Chomping,
folded: bool,
) -> Result<Body<S>, BodyError> {
let min = indentation.min;
let mut indent = indentation.known;
let mut leading_max = 0usize;
let mut value = S::from_raw("");
let mut empties = 0usize;
let mut prev: Option<bool> = None;
let mut content_end = body;
let mut keep_end = body;
let mut i = body;
let mut stop: Option<Line> = None;
while i < src.len() {
let line = line_at(src, i);
let n = match indent {
Some(n) => n,
None if line.all_spaces => {
leading_max = leading_max.max(line.spaces);
empties = empties.saturating_add(1);
if !line.has_break {
break;
}
keep_end = line.next;
i = line.next;
continue;
}
None => {
let marker = line.spaces == 0 && prefix_break_at(src, line.start);
if line.spaces < min || marker {
stop = Some(line);
break;
}
if leading_max > line.spaces {
return Err(BodyError::LeadingIndentation { line: line.start });
}
indent = Some(line.spaces);
line.spaces
}
};
if line.all_spaces && line.spaces <= n {
empties = empties.saturating_add(1);
if !line.has_break {
break;
}
keep_end = line.next;
i = line.next;
continue;
}
let marker = n == 0 && prefix_break_at(src, line.start);
if line.spaces < n || marker {
stop = Some(line);
break;
}
let text = src
.get(line.start.saturating_add(n)..line.end)
.unwrap_or("");
let folded_text = !text.starts_with([' ', '\t']);
match prev {
None => value.push_repeat('\n', empties),
Some(true) if folded && folded_text && empties == 0 => value.push_char(' '),
Some(true) if folded && folded_text => {
value.push_repeat('\n', empties);
}
Some(_) => value.push_repeat('\n', empties.saturating_add(1)),
}
value.push_str(text);
prev = Some(folded_text);
empties = 0;
content_end = line.next;
keep_end = line.next;
i = line.next;
if !line.has_break {
break;
}
}
if let Some(line) = stop {
let ws_end = skip_white(src, line.start);
if ws_end == line.end && line.end > line.start.saturating_add(line.spaces) {
return Err(BodyError::Tab { line: line.start });
}
}
let indent = indent.unwrap_or(min.max(leading_max));
let content = usize::from(prev.is_some());
let (end, tail) = match chomping {
Chomping::Keep => (keep_end, empties.saturating_add(content)),
Chomping::Clip => (if prev.is_some() { content_end } else { body }, content),
_ => (if prev.is_some() { content_end } else { body }, 0),
};
value.push_repeat('\n', tail);
Ok(Body { indent, end, value })
}
pub(super) fn block_value(
src: &str,
span: Span,
header: BlockHeader,
folded: bool,
) -> Option<String> {
let text = src.get(span.start..span.end)?;
let n = header.content_indent;
let indent = Indent {
min: 0,
known: Some(n),
};
let mut value = body::<String>(text, 0, indent, header.chomping, folded)
.ok()
.filter(|b| b.end == text.len())?
.value;
let rest = src.get(span.end..).unwrap_or("");
let unterminated_empty = !rest.is_empty() && rest.len() <= n && rest.bytes().all(|b| b == b' ');
if header.chomping == Chomping::Keep && unterminated_empty {
value.push('\n');
}
Some(value)
}
impl<'a> Parser<'a> {
pub(super) fn block_scalar(
&mut self,
ctx: NodeCtx,
mut events: Vec<Event<'a>>,
props: Properties,
) {
let src = self.src;
let start = events.last().map_or(self.pos, |e| e.span().end);
let folded = byte(src, start) == Some(b'>');
let header_mark = first_mark(src, start, line_end(src, start)).filter(|m| {
src.as_bytes()
.get(start.saturating_add(1)..*m)
.is_some_and(|r| {
r.iter()
.all(|b| matches!(b, b'1'..=b'9' | b'+' | b'-' | b' ' | b'\t'))
})
});
let Some(hdr) = header(src, start) else {
return self.mark_then_fail(
header_mark,
INVALID_BLOCK_HEADER,
"malformed block scalar header",
true,
);
};
let ws_end = skip_white(src, hdr.end);
let comment = ws_end > hdr.end && byte(src, ws_end) == Some(b'#');
let text_end = if comment {
line_end(src, ws_end)
} else {
ws_end
};
if !break_or_end(src, text_end) {
return self.mark_then_fail(
header_mark,
INVALID_BLOCK_HEADER,
"text after a block scalar header",
true,
);
}
let body_start = text_end.saturating_add(break_len(src, text_end));
if !self.merge_props(props) {
return self.fail(DUPLICATE_PROPERTY, "duplicate anchor or tag", true);
}
let indent = Indent::of(ctx.parent, hdr);
let scanned = body::<Count>(src, body_start, indent, hdr.chomping, folded).and_then(|b| {
if self.scalar_len_ok(b.value.0) {
Ok(b)
} else {
Err(BodyError::TooLong { line: body_start })
}
});
let trivia =
|kind, a: usize, b: usize| Event::new(EventKind::Trivia(kind), Span::new(a, b));
events.push(trivia(TriviaKind::BlockScalarHeader, start, hdr.end));
if ws_end > hdr.end {
events.push(trivia(TriviaKind::Whitespace, hdr.end, ws_end));
}
if comment {
events.push(trivia(TriviaKind::Comment, ws_end, text_end));
}
if body_start > text_end {
events.push(trivia(TriviaKind::LineBreak, text_end, body_start));
}
self.begin_content();
self.commit_scratch(events);
match scanned {
Ok(b) => {
let props = core::mem::take(&mut self.props);
let style = if folded {
ScalarStyle::Folded
} else {
ScalarStyle::Literal
};
let header = BlockHeader::new(
hdr.chomping,
hdr.indicator,
b.indent,
Span::new(start, hdr.end),
);
let span = Span::new(body_start, b.end);
self.push_event(Event::scalar(span, style, props, Some(header)));
self.pos = b.end;
self.marks_in_token(start, b.end);
let at_line_start = b.end == body_start && body_start > text_end
|| src
.as_bytes()
.get(..b.end)
.is_some_and(|r| r.ends_with(b"\n") || r.ends_with(b"\r"));
if at_line_start {
self.line_start = b.end;
self.line_indent = 0;
self.fresh = true;
}
self.child_done();
}
Err(err) => self.block_scalar_error(body_start, err),
}
}
fn block_scalar_error(&mut self, body_start: usize, err: BodyError) {
let src = self.src;
let (line, code, message) = match err {
BodyError::LeadingIndentation { line } => (
line,
BLOCK_SCALAR_INDENTATION,
"a leading empty line is indented more than the block scalar's content",
),
BodyError::Tab { line } => (
line,
TAB_INDENTATION,
"tabs cannot be used for block indentation",
),
BodyError::TooLong { line } => (line, SCALAR_TOO_LONG, "the scalar is too long"),
};
let mut i = body_start;
while i < line {
let l = line_at(src, i);
if skip_white(src, i) != l.end {
break;
}
i = l.next;
}
let target = if i >= line { line } else { body_start };
self.out.trivia(body_start, target);
self.emit_pos = target;
let lead = src
.as_bytes()
.get(target..)
.map_or(0, |r| r.iter().take_while(|b| **b == b' ').count());
let text = target.saturating_add(lead);
if matches!(err, BodyError::LeadingIndentation { .. }) && target == line && lead > 0 {
self.push(EventKind::Trivia(TriviaKind::Whitespace), target, text);
self.pos = text;
} else {
self.pos = target;
}
self.emit_pos = self.pos;
self.line_start = target;
self.mark_before_error = first_mark(src, line, line_end(src, line));
self.fail(code, message, true);
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::indexing_slicing)]
use super::*;
fn run(
src: &str,
parent: Option<usize>,
folded: bool,
) -> Result<(usize, usize, String), BodyError> {
let h = header(src, 0).unwrap();
let body_start = line_at(src, 0).next;
let indent = Indent::of(parent, h);
let counted = body::<Count>(src, body_start, indent, h.chomping, folded);
let b = body::<String>(src, body_start, indent, h.chomping, folded)?;
assert_eq!(counted.map(|c| c.value.0), Ok(b.value.len()));
let hdr = BlockHeader::new(h.chomping, h.indicator, b.indent, Span::new(0, h.end));
let span = Span::new(body_start, b.end);
assert_eq!(
block_value(src, span, hdr, folded).as_deref(),
Some(b.value.as_str()),
"{src:?}"
);
Ok((b.indent, b.end, b.value))
}
#[test]
fn headers() {
assert_eq!(header("|", 0).unwrap().chomping, Chomping::Clip);
let h = header(">2- # c", 0).unwrap();
assert_eq!(
(h.end, h.chomping, h.indicator),
(3, Chomping::Strip, Some(2))
);
let h = header("|+9\n", 0).unwrap();
assert_eq!(
(h.end, h.chomping, h.indicator),
(3, Chomping::Keep, Some(9))
);
assert!(header("|0", 0).is_none());
assert!(header("|22", 0).is_none());
assert!(header("|x", 0).is_none());
assert!(header("|--", 0).is_none());
}
#[test]
fn literal_chomping() {
let src = "|\n a\n b\n\n c\n\n\nd";
assert_eq!(run(src, Some(0), false), Ok((1, 13, "a\n b\n\nc\n".into())));
let keep = "|+\n a\n\n\nd";
assert_eq!(run(keep, Some(0), false), Ok((1, 8, "a\n\n\n".into())));
let strip = "|-\n a\n\n";
assert_eq!(run(strip, Some(0), false), Ok((1, 6, "a".into())));
assert_eq!(run("|\n a", Some(0), false), Ok((1, 4, "a\n".into())));
assert_eq!(run("|-\n a", Some(0), false), Ok((1, 5, "a".into())));
assert_eq!(run("|+\n a\n ", Some(0), false), Ok((1, 6, "a\n\n".into())));
assert_eq!(run("|+\n ", Some(0), false), Ok((3, 3, "\n".into())));
}
#[test]
fn empty_bodies() {
assert_eq!(run("|\n", Some(0), false), Ok((1, 2, String::new())));
assert_eq!(
run("|+\n\n \nx", Some(0), false),
Ok((2, 7, "\n\n".into()))
);
assert_eq!(
run("|+\n\n \nx", None, false),
Err(BodyError::LeadingIndentation { line: 7 })
);
assert_eq!(run("|\n\nx", Some(0), false), Ok((1, 2, String::new())));
}
#[test]
fn folding() {
let src = ">\n a\n b\n\n c\n d\n e\n";
assert_eq!(run(src, None, true).unwrap().2, "a b\nc\n d\ne\n");
assert_eq!(run(">\n\n a\n", None, true).unwrap().2, "\na\n");
}
#[test]
fn explicit_indentation_and_errors() {
assert_eq!(run("|2\n a\n b\n", Some(0), false).unwrap().2, " a\nb\n");
assert_eq!(run("|1\n a\n", None, false).unwrap().2, " a\n");
assert_eq!(
run(">\n \n a\n", Some(0), true),
Err(BodyError::LeadingIndentation { line: 5 })
);
assert_eq!(
run("|\n\t\nb", Some(0), false),
Err(BodyError::Tab { line: 2 })
);
assert_eq!(run("|\n---\n", None, false), Ok((0, 2, String::new())));
}
}