use oneyaml_core::code::{
INVALID_ESCAPE, INVALID_NODE_START, QUOTED_SCALAR_INDENTATION, UNTERMINATED_QUOTED_SCALAR,
};
use oneyaml_core::{Applicability, Code, Diagnostic, Label, Replacement, Span, Suggestion};
use super::head::quote_fix;
use super::scalar::escape_scan;
use super::scan::{break_len, byte, is_nb_json, line_end, skip_white};
#[derive(Debug, Default)]
pub(super) struct Polish {
labels: Vec<Label>,
help: Option<&'static str>,
fixes: Vec<Suggestion>,
}
impl Polish {
fn downgrade(mut self) -> Self {
for fix in &mut self.fixes {
fix.applicability = Applicability::MaybeIncorrect;
}
self
}
pub(super) fn apply(self, d: Diagnostic) -> Diagnostic {
let mut d = d;
for label in self.labels {
d = d.with_secondary(label);
}
if let Some(help) = self.help {
d = d.with_help(help);
}
for fix in self.fixes {
d = d.with_suggestion(fix);
}
d
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Place {
pub(super) key: bool,
pub(super) key_only: bool,
pub(super) parent: Option<usize>,
pub(super) flow: bool,
pub(super) sound: bool,
}
fn how(safe: bool) -> Applicability {
if safe {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
}
}
fn continues(src: &str, at: usize, place: Place) -> bool {
if place.flow {
return false;
}
let mut i = line_end(src, at);
loop {
let br = break_len(src, i);
if br == 0 {
return false; }
let start = i.saturating_add(br);
let content = src
.get(start..)
.map_or(0, |r| r.bytes().take_while(|b| *b == b' ').count());
let first = start.saturating_add(content);
match byte(src, first) {
None => return false,
Some(b'\n' | b'\r') => i = first,
Some(b'\t') => {
let after = skip_white(src, first);
if matches!(byte(src, after), None | Some(b'\n' | b'\r')) {
i = after;
} else {
return true;
}
}
Some(b'#') => return false,
Some(_) => {
let marker = content == 0
&& src
.get(start..)
.is_some_and(|r| r.starts_with("---") || r.starts_with("..."));
return !marker && place.parent.is_none_or(|p| content > p);
}
}
}
}
fn clean_after(src: &str, end: usize, place: Place) -> bool {
let i = skip_white(src, end);
let colon = byte(src, i) == Some(b':')
&& (place.flow
|| matches!(
byte(src, i.saturating_add(1)),
None | Some(b' ' | b'\t' | b'\n' | b'\r')
));
if place.key_only {
return colon;
}
match byte(src, i) {
None | Some(b'\n' | b'\r') => true,
Some(b'#') => i > end,
Some(b':') => colon && place.key,
Some(b',' | b']' | b'}') => place.flow,
_ => false,
}
}
fn value_end(src: &str, from: usize, key: bool, flow: bool) -> (usize, Option<u8>) {
let bytes = src.as_bytes();
let line = line_end(src, from);
let mut end = line;
let mut stop = None;
let mut prev_white = false;
for (k, b) in bytes.get(from..line).unwrap_or(&[]).iter().enumerate() {
let at = from.saturating_add(k);
let next = bytes.get(at.saturating_add(1));
let separated = next.is_none_or(|n| {
matches!(n, b' ' | b'\t' | b'\n' | b'\r')
|| flow && matches!(n, b',' | b'[' | b']' | b'{' | b'}')
});
let colon = *b == b':' && key && separated;
let bracket = flow && matches!(b, b',' | b'[' | b']' | b'{' | b'}');
if *b == b'#' && prev_white {
end = at;
break;
}
if colon || bracket {
end = at;
stop = Some(*b);
break;
}
prev_white = matches!(b, b' ' | b'\t');
}
let trimmed = src
.get(from..end)
.map_or(0, |t| t.trim_end_matches([' ', '\t']).len());
(from.saturating_add(trimmed), stop)
}
fn valid_double(text: &str) -> bool {
let scan = escape_scan(text, 0);
scan.end == Some(text.len())
&& scan.first.is_none()
&& text
.chars()
.all(|c| is_nb_json(c) && c != '\n' && c != '\r')
}
fn valid_single(text: &str) -> bool {
let Some(inner) = text.strip_prefix('\'').and_then(|t| t.strip_suffix('\'')) else {
return false;
};
let mut quotes = 0usize;
for c in inner.chars() {
if c == '\'' {
quotes = quotes.saturating_add(1);
} else {
if quotes % 2 == 1 {
return false;
}
quotes = 0;
}
if !is_nb_json(c) || c == '\n' || c == '\r' {
return false;
}
}
quotes % 2 == 0
}
fn escape_quotes(inner: &str, double: bool) -> String {
let mut out = String::with_capacity(inner.len().saturating_add(4));
let mut chars = inner.chars().peekable();
while let Some(c) = chars.next() {
match c {
'\'' if !double => {
out.push_str("''");
if chars.peek() == Some(&'\'') {
chars.next();
}
}
'\\' if double => {
out.push(c);
if let Some(n) = chars.next() {
out.push(n);
}
}
'"' if double => out.push_str("\\\""),
c => out.push(c),
}
}
out
}
pub(super) fn content(src: &str, code: Code, at: usize, place: Place) -> Option<Polish> {
let polish = match (code, byte(src, at)) {
(INVALID_ESCAPE, Some(b'"')) => Some(invalid_escape(src, at, place)),
(INVALID_NODE_START, Some(c @ (b'@' | b'`'))) => Some(reserved(src, at, c, place)),
(UNTERMINATED_QUOTED_SCALAR | QUOTED_SCALAR_INDENTATION, Some(q @ (b'"' | b'\''))) => {
Some(unclosed_quote(src, code, at, q))
}
_ => None,
};
polish.map(|p| if place.sound { p } else { p.downgrade() })
}
fn invalid_escape(src: &str, at: usize, place: Place) -> Polish {
let scan = escape_scan(src, at);
let mut polish = Polish::default();
if let Some(first) = scan.first {
let text = src.get(first.start..first.end).unwrap_or("\\");
polish
.labels
.push(Label::new(first, format!("`{text}` is not an escape")));
if text == "\\'" {
polish.help = Some("a `'` needs no escape inside double quotes; remove the `\\`");
}
}
let Some(end) = scan.end else {
if !scan.edits.is_empty() {
polish.fixes.push(Suggestion::new(
"make the backslashes literal",
scan.edits,
Applicability::MaybeIncorrect,
));
}
return polish;
};
let settled = clean_after(src, end, place) && !continues(src, end, place);
let fixed = {
let mut out = String::new();
let mut from = at;
for e in &scan.edits {
out.push_str(src.get(from..e.span.start).unwrap_or(""));
out.push_str(&e.text);
from = e.span.end;
}
out.push_str(src.get(from..end).unwrap_or(""));
out
};
let single = (!scan.valid && !scan.apostrophe)
.then(|| src.get(at.saturating_add(1)..end.saturating_sub(1)))
.flatten()
.map(|inner| format!("'{inner}'"))
.filter(|t| valid_single(t));
if !scan.edits.is_empty() {
polish.fixes.push(Suggestion::new(
"make the backslashes literal",
scan.edits,
how(settled && valid_double(&fixed) && !scan.ambiguous),
));
}
if let Some(text) = single {
polish.fixes.push(Suggestion::new(
"use single quotes, where `\\` is literal",
vec![Replacement::new(Span::new(at, end), text)],
Applicability::MaybeIncorrect,
));
}
polish
}
fn reserved(src: &str, at: usize, c: u8, place: Place) -> Polish {
let (end, stop) = value_end(src, at, place.key, place.flow);
let text = src.get(at..end).unwrap_or("");
let fix = quote_fix(src, at, end, Applicability::MaybeIncorrect);
let ends_node = match stop {
Some(b':') => place.key,
Some(b',' | b']' | b'}') => place.flow,
_ => false,
};
let mapping_meant = text.contains(": ") || text.ends_with(':');
let whole = if place.flow || place.key_only {
ends_node && !mapping_meant
} else {
!mapping_meant && !continues(src, at, place)
};
let valid = !text.is_empty() && fix.edits.first().is_some_and(|e| valid_single(&e.text));
let fix = valid.then(|| Suggestion::new(fix.message, fix.edits, how(whole)));
let label = if c == b'@' {
"`@` is reserved"
} else {
"`` ` `` is reserved"
};
Polish {
labels: vec![Label::new(Span::new(at, at.saturating_add(1)), label)],
help: Some(
"`@` and `` ` `` are reserved indicators (YAML 1.2 ยง5.3): a plain scalar \
cannot start with them, so quote the value",
),
fixes: fix.into_iter().collect(),
}
}
fn unclosed_quote(src: &str, code: Code, at: usize, q: u8) -> Polish {
let (end, _) = value_end(src, at.saturating_add(1), false, false);
let backslashes = src
.get(at.saturating_add(1)..end)
.map_or(0, |t| t.bytes().rev().take_while(|b| *b == b'\\').count());
let escapes_quote = q == b'"' && backslashes % 2 == 1;
let quote = if q == b'"' { "\"" } else { "'" };
let fixes = if escapes_quote {
Vec::new()
} else {
vec![Suggestion::new(
"close the quote on this line",
vec![Replacement::new(Span::empty(end), quote)],
Applicability::MaybeIncorrect,
)]
};
Polish {
labels: vec![Label::new(
Span::new(at, at.saturating_add(1)),
"the quoted scalar starts here",
)],
help: (code == QUOTED_SCALAR_INDENTATION).then_some(
"a quoted scalar goes on until its closing quote, and its continuation lines \
must be indented more than its parent; if it should end on its first line, \
add the closing quote there",
),
fixes,
}
}
pub(super) fn after_quote(
src: &str,
(qs, qe): (usize, usize),
pos: usize,
parent: Option<usize>,
) -> Option<Polish> {
let q = byte(src, qs).filter(|q| matches!(q, b'\'' | b'"'))?;
if qe != pos || qe <= qs {
return None;
}
let place = Place {
key: false,
key_only: false,
parent,
flow: false,
sound: true,
};
let closing = Label::new(
Span::new(qe.saturating_sub(1), qe),
"the quoted scalar ends here",
);
if byte(src, pos) == Some(b'#') {
let settled = !continues(src, pos, place);
return Some(Polish {
labels: vec![closing],
help: Some("a comment needs white space before its `#` (ยง6.6)"),
fixes: vec![Suggestion::new(
"separate the comment from the value",
vec![Replacement::new(Span::empty(pos), " ")],
how(settled),
)],
});
}
let double = q == b'"';
let single_line = src.get(qs..qe).is_some_and(|t| !t.contains(['\n', '\r']));
let (rest_end, _) = value_end(src, pos, false, false);
let inner_end = rest_end.saturating_sub(1);
let fix = (single_line && rest_end > pos && byte(src, inner_end) == Some(q))
.then(|| {
let quoted = src.get(qs.saturating_add(1)..pos)?;
let glued = src.get(pos..inner_end)?;
let mut inner = String::with_capacity(quoted.len().saturating_add(glued.len()));
inner.push_str(quoted);
inner.extend(glued.chars().filter(|c| *c != '\u{feff}'));
Some(inner)
})
.flatten()
.map(|inner| {
let mut text = String::with_capacity(inner.len().saturating_add(4));
text.push(char::from(q));
text.push_str(&escape_quotes(&inner, double));
text.push(char::from(q));
(text, inner)
})
.filter(|(text, _)| {
if double {
valid_double(text)
} else {
valid_single(text)
}
})
.map(|(text, _)| {
let what = if double {
"escape the inner `\"`"
} else {
"double the inner `'`"
};
let safe = !continues(src, pos, place);
Suggestion::new(
what,
vec![Replacement::new(Span::new(qs, rest_end), text)],
how(safe),
)
});
let help = if double {
"inside double quotes, write `\\\"` for a literal `\"`"
} else {
"inside single quotes, write `''` for a literal `'`"
};
Some(Polish {
labels: vec![closing],
help: Some(help),
fixes: fix.into_iter().collect(),
})
}