use crate::block::parse_block_range_checked;
use crate::chars::is_trim_whitespace;
use crate::errors::{Diagnostic, LimaDiagnosticCode as Code, LimaError};
use crate::flow::parse_flow_or_scalar_value_checked;
use crate::normalize::{
begin_warning_collection, check_duplicate_key, check_key_length, finish_warning_collection,
DOCUMENT_SIZE_LIMIT, NESTING_DEPTH_LIMIT, TOP_LEVEL_KEY_LIMIT,
};
use crate::scalars::{check_string_limit, strip_comment, strip_key_quotes};
use crate::value::{
Builder, LimaValue, PlainBuilder, PositionedBuilder, PositionedValue, StringSourceSpan,
};
struct TopKey {
line: u32,
key_start: usize,
value_start: usize,
is_block: bool,
key: String,
}
fn find_key_sep(s: &str) -> Option<usize> {
let b = s.as_bytes();
let first = *b.first()?;
if first == b'\'' {
let mut i = 1;
while i < b.len() && b[i] != first {
i += 1;
}
if b.get(i + 1) == Some(&b':') && b.get(i + 2) == Some(&b' ') {
return Some(i + 1);
}
return None;
}
if first == b'"' {
let mut i = 1;
while i < b.len() && b[i] != b'"' {
i += if b[i] == b'\\' && i + 1 < b.len() {
2
} else {
1
};
}
if b.get(i + 1) == Some(&b':') && b.get(i + 2) == Some(&b' ') {
return Some(i + 1);
}
return None;
}
s.find(": ")
}
fn scan_top_keys(source: &str) -> Vec<TopKey> {
let mut result = Vec::new();
let bytes = source.as_bytes();
let mut pos = 0usize;
let mut line_no = 1u32;
while pos <= bytes.len() {
let line_end = source[pos..]
.find('\n')
.map(|p| pos + p)
.unwrap_or(bytes.len());
let line = &source[pos..line_end];
if !line.is_empty() && !line.starts_with([' ', '\t', '#']) {
if let Some(colon_pos) = find_key_sep(line) {
let key = strip_key_quotes(&line[..colon_pos]);
result.push(TopKey {
line: line_no,
key_start: pos,
value_start: pos + colon_pos + 2,
is_block: false,
key,
});
} else if let Some(key_part) = line.strip_suffix(':') {
if !key_part.is_empty() {
let key = strip_key_quotes(key_part);
let value_start = if line_end < bytes.len() {
line_end + 1
} else {
line_end
};
result.push(TopKey {
line: line_no,
key_start: pos,
value_start,
is_block: true,
key,
});
}
}
}
if line_end >= bytes.len() {
break;
}
pos = line_end + 1;
line_no += 1;
}
result
}
fn has_space_before_colon(line: &str) -> bool {
let b = line.as_bytes();
let Some("e) = b.first() else {
return false;
};
if quote != b'\'' && quote != b'"' {
return false;
}
let mut i = 1;
if quote == b'\'' {
while i < b.len() && b[i] != b'\'' {
i += 1;
}
} else {
while i < b.len() && b[i] != b'"' {
i += if b[i] == b'\\' && i + 1 < b.len() {
2
} else {
1
};
}
}
if i >= b.len() {
return false;
} i += 1; let ws_start = i;
while i < b.len() && (b[i] == b' ' || b[i] == b'\t') {
i += 1;
}
i > ws_start && b.get(i) == Some(&b':')
}
fn line_at(source: &str, byte_pos: usize) -> u32 {
1 + source[..byte_pos].bytes().filter(|&c| c == b'\n').count() as u32
}
fn merge_block_scalar(body: &str, key: &str, first_line: u32) -> (String, Vec<StringSourceSpan>) {
let body_lines: Vec<&str> = body.split('\n').collect();
let mut min_indent = usize::MAX;
for line in &body_lines {
let indent = line.len() - line.trim_start_matches(' ').len();
if indent == line.len() {
continue;
} min_indent = min_indent.min(indent);
}
min_indent = min_indent.min(key.chars().count() + 2);
let trim_amt = if min_indent > 1 && min_indent != usize::MAX {
min_indent
} else {
0
};
let mut merged: Vec<String> = Vec::new();
let mut line_spans: Vec<Vec<StringSourceSpan>> = Vec::new();
for (body_index, line) in body_lines.iter().enumerate() {
let b = line.as_bytes();
let mut start = trim_amt.min(line.len());
let is_continuation = b.get(start) == Some(&b'^') && b.get(start + 1) == Some(&b'^');
if is_continuation {
start += 2;
}
let mut end = line.len();
while end > start && b[end - 1] == b' ' {
end -= 1;
}
if end < start {
end = start;
}
let content = &line[start..end];
if is_continuation {
if let Some(last) = merged.last_mut() {
if !content.is_empty() {
let output_start = last.len() + 1;
last.push(' ');
last.push_str(content);
line_spans.last_mut().unwrap().push(StringSourceSpan {
start: output_start,
line: first_line + body_index as u32,
source_offset: start,
});
}
} else {
merged.push(content.to_string());
line_spans.push(if content.is_empty() {
Vec::new()
} else {
vec![StringSourceSpan {
start: 0,
line: first_line + body_index as u32,
source_offset: start,
}]
});
}
} else {
merged.push(content.to_string());
line_spans.push(if content.is_empty() {
Vec::new()
} else {
vec![StringSourceSpan {
start: 0,
line: first_line + body_index as u32,
source_offset: start,
}]
});
}
}
while merged.last().is_some_and(String::is_empty) {
merged.pop();
line_spans.pop();
}
let mut spans = Vec::new();
let mut output_start = 0;
for (line, mut pieces) in merged.iter().zip(line_spans) {
for span in &mut pieces {
span.start += output_start;
}
spans.extend(pieces);
output_start += line.len() + 1;
}
(merged.join("\n"), spans)
}
fn expand_leading_tabs(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for (i, line) in s.split('\n').enumerate() {
if i > 0 {
out.push('\n');
}
let leading_end = line
.find(|c: char| c != ' ' && c != '\t')
.unwrap_or(line.len());
let (leading, rest) = line.split_at(leading_end);
if leading.contains('\t') {
for c in leading.chars() {
if c == '\t' {
out.push_str(" ");
} else {
out.push(c);
}
}
} else {
out.push_str(leading);
}
out.push_str(rest);
}
out
}
fn strip_trailing_spaces(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for (i, line) in s.split('\n').enumerate() {
if i > 0 {
out.push('\n');
}
out.push_str(line.trim_end_matches(' '));
}
out
}
fn parse_core_generic<B: Builder, const CHECK_DUPLICATES: bool>(
front_matter: &str,
strict: bool,
) -> Result<B::Mapping, LimaError> {
if front_matter.len() > DOCUMENT_SIZE_LIMIT {
return Err(LimaError::new(
Code::ResourceLimit,
1,
format!("Lima: document exceeds maximum size of {DOCUMENT_SIZE_LIMIT} bytes at line 1"),
));
}
let mut owned = front_matter.to_string();
if owned.contains('\r') {
owned = owned.replace("\r\n", "\n").replace('\r', "\n");
}
if owned.contains('\t') {
owned = expand_leading_tabs(&owned);
}
if owned.contains(" \n") || owned.ends_with(' ') {
owned = strip_trailing_spaces(&owned);
}
let front_matter: &str = &owned;
if front_matter.is_empty() {
return Ok(B::m_create());
}
if strict {
let mut search_from = 0usize;
loop {
let line_end = front_matter[search_from..]
.find('\n')
.map(|p| search_from + p);
let line = &front_matter[search_from..line_end.unwrap_or(front_matter.len())];
if has_space_before_colon(line) {
let l = line_at(front_matter, search_from);
return Err(LimaError::new(
Code::InvalidQuote,
l,
format!("Lima: space between closing quote and colon at line {l}"),
));
}
match line_end {
Some(e) => search_from = e + 1,
None => break,
}
}
}
let top_keys = scan_top_keys(front_matter);
if top_keys.len() > TOP_LEVEL_KEY_LIMIT {
return Err(LimaError::new(
Code::ResourceLimit,
1,
format!("Lima: too many top-level key entries (max {TOP_LEVEL_KEY_LIMIT}) at line 1"),
));
}
let mut root: B::Mapping = B::m_create();
let mut depth_risk = false;
let mut top_keys = top_keys.into_iter().peekable();
while let Some(tk) = top_keys.next() {
let next_start = top_keys
.peek()
.map(|n| n.key_start)
.unwrap_or(front_matter.len());
check_key_length(&tk.key, tk.line)?;
if CHECK_DUPLICATES {
check_duplicate_key(B::m_has_key(&root, &tk.key), &tk.key, tk.line, strict)?;
}
let value = if tk.is_block {
parse_block_range_checked::<B, CHECK_DUPLICATES>(
front_matter,
tk.value_start,
next_start,
strict,
tk.line + 1,
&mut depth_risk,
)?
.unwrap_or(B::v_null(tk.line))
} else {
let span_end = next_start.min(front_matter.len());
let first_newline = front_matter[tk.value_start..span_end]
.find('\n')
.map(|p| p + tk.value_start);
match first_newline {
None => {
let val = &front_matter[tk.value_start..span_end];
if val.contains('#') {
let val = strip_comment(val);
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
&val, strict, tk.line,
)?
} else {
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
val, strict, tk.line,
)?
}
}
Some(nl) if nl == span_end.saturating_sub(1) => {
let val = &front_matter[tk.value_start..nl];
if val.contains('#') {
let val = strip_comment(val);
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
&val, strict, tk.line,
)?
} else {
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
val, strict, tk.line,
)?
}
}
Some(nl) => {
let line0 = &front_matter[tk.value_start..nl];
let line0_trimmed = line0.trim_matches(is_trim_whitespace);
if line0_trimmed != "|" {
if line0.contains('#') {
let val = strip_comment(line0);
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
&val, strict, tk.line,
)?
} else {
parse_flow_or_scalar_value_checked::<B, CHECK_DUPLICATES>(
line0, strict, tk.line,
)?
}
} else {
let body = &front_matter[nl + 1..span_end];
let (joined, spans) = merge_block_scalar(body, &tk.key, tk.line + 1);
check_string_limit(&joined, tk.line)?;
B::v_block_string(joined, tk.line + 1, spans)
}
}
}
};
B::m_set(&mut root, tk.key, value);
}
if depth_risk && B::m_max_depth(&root) > NESTING_DEPTH_LIMIT as u32 {
return Err(LimaError::new(
Code::ResourceLimit,
1,
format!("Lima: nesting depth exceeds maximum of {NESTING_DEPTH_LIMIT} at line 1"),
));
}
Ok(root)
}
#[derive(Default)]
pub struct CoreOptions {
pub strict: bool,
pub on_warning: Option<Box<dyn FnMut(Diagnostic)>>,
}
impl From<bool> for CoreOptions {
fn from(strict: bool) -> Self {
Self {
strict,
on_warning: None,
}
}
}
pub fn parse_core(
front_matter: &str,
options: impl Into<CoreOptions>,
) -> Result<LimaValue, LimaError> {
let mut options = options.into();
if options.on_warning.is_none() {
return if options.strict {
parse_core_generic::<PlainBuilder, true>(front_matter, true)
} else {
parse_core_generic::<PlainBuilder, false>(front_matter, false)
}
.map(LimaValue::Mapping);
}
begin_warning_collection(true);
let result = parse_core_generic::<PlainBuilder, true>(front_matter, options.strict)
.map(LimaValue::Mapping);
let warnings = finish_warning_collection();
if let Some(callback) = options.on_warning.as_mut() {
for warning in warnings {
callback(warning);
}
}
result
}
pub fn parse_core_with_positions(
front_matter: &str,
strict: bool,
) -> Result<Vec<(String, PositionedValue)>, LimaError> {
if strict {
parse_core_generic::<PositionedBuilder, true>(front_matter, true)
} else {
parse_core_generic::<PositionedBuilder, false>(front_matter, false)
}
}
pub(crate) fn parse_core_with_positions_options(
front_matter: &str,
strict: bool,
collect_warnings: bool,
) -> Result<Vec<(String, PositionedValue)>, LimaError> {
if strict || collect_warnings {
parse_core_generic::<PositionedBuilder, true>(front_matter, strict)
} else {
parse_core_generic::<PositionedBuilder, false>(front_matter, false)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get<'a>(v: &'a LimaValue, key: &str) -> &'a LimaValue {
let LimaValue::Mapping(entries) = v else {
panic!("expected mapping")
};
&entries
.iter()
.find(|(k, _)| k == key)
.unwrap_or_else(|| panic!("missing key {key:?} in {entries:?}"))
.1
}
#[test]
fn flat_document() {
let v = parse_core("title: Hello\ncount: 3\n", false).unwrap();
assert_eq!(get(&v, "title"), &LimaValue::String("Hello".into()));
assert_eq!(get(&v, "count"), &LimaValue::Int(3));
}
#[test]
fn nested_mapping() {
let v = parse_core("address:\n street: Main St\n city: Springfield\n", false).unwrap();
let addr = get(&v, "address");
assert_eq!(get(addr, "street"), &LimaValue::String("Main St".into()));
assert_eq!(get(addr, "city"), &LimaValue::String("Springfield".into()));
}
#[test]
fn block_sequence() {
let v = parse_core("tags:\n - a\n - b\n - c\n", false).unwrap();
let LimaValue::Array(items) = get(&v, "tags") else {
panic!("expected array")
};
assert_eq!(
items,
&[
LimaValue::String("a".into()),
LimaValue::String("b".into()),
LimaValue::String("c".into()),
]
);
}
#[test]
fn sequence_of_mappings() {
let v = parse_core(
"items:\n - name: a\n qty: 1\n - name: b\n qty: 2\n",
false,
)
.unwrap();
let LimaValue::Array(items) = get(&v, "items") else {
panic!("expected array")
};
assert_eq!(get(&items[0], "name"), &LimaValue::String("a".into()));
assert_eq!(get(&items[0], "qty"), &LimaValue::Int(1));
assert_eq!(get(&items[1], "name"), &LimaValue::String("b".into()));
}
#[test]
fn flow_sequence_and_mapping() {
let v = parse_core("tags: [a, b, c]\nauthor: {name: Alice, age: 30}\n", false).unwrap();
let LimaValue::Array(tags) = get(&v, "tags") else {
panic!("expected array")
};
assert_eq!(tags.len(), 3);
let author = get(&v, "author");
assert_eq!(get(author, "name"), &LimaValue::String("Alice".into()));
assert_eq!(get(author, "age"), &LimaValue::Int(30));
}
#[test]
fn duplicate_key_strict_throws() {
assert!(parse_core("title: First\ntitle: Second", true).is_err());
}
#[test]
fn duplicate_key_non_strict_last_wins() {
let v = parse_core("title: First\ntitle: Second", false).unwrap();
assert_eq!(get(&v, "title"), &LimaValue::String("Second".into()));
}
#[test]
fn comments_are_stripped() {
let v = parse_core(
"title: Hello # a comment\n# full line comment\ncount: 3\n",
false,
)
.unwrap();
assert_eq!(get(&v, "title"), &LimaValue::String("Hello".into()));
assert_eq!(get(&v, "count"), &LimaValue::Int(3));
}
#[test]
fn unrecognized_top_level_lines_are_skipped_even_strict() {
let v = parse_core(
"title: Hello\nthis is not a key line at all\ncount: 42",
true,
)
.unwrap();
assert_eq!(get(&v, "title"), &LimaValue::String("Hello".into()));
assert_eq!(get(&v, "count"), &LimaValue::Int(42));
}
#[test]
fn indented_line_is_not_a_top_level_key() {
let v = parse_core(" bogus: value\nreal: Hello", false).unwrap();
let LimaValue::Mapping(entries) = &v else {
panic!()
};
assert_eq!(entries.len(), 1);
assert_eq!(get(&v, "real"), &LimaValue::String("Hello".into()));
}
#[test]
fn block_scalar_basic() {
let v = parse_core("description: |\n Line one.\n Line two.\n", false).unwrap();
assert_eq!(
get(&v, "description"),
&LimaValue::String("Line one.\nLine two.".into())
);
}
#[test]
fn block_scalar_continuation() {
let v = parse_core(
"description: |\n This is long\n ^^and continues.\n",
false,
)
.unwrap();
assert_eq!(
get(&v, "description"),
&LimaValue::String("This is long and continues.".into())
);
}
#[test]
fn block_scalar_trailing_blank_lines_stripped() {
let v = parse_core("description: |\n Line one.\n\n\nnext: value\n", false).unwrap();
assert_eq!(
get(&v, "description"),
&LimaValue::String("Line one.".into())
);
assert_eq!(get(&v, "next"), &LimaValue::String("value".into()));
}
#[test]
fn quoted_key_with_escaped_quote() {
let v = parse_core(r#""say \"hi\"": value"#, false).unwrap();
assert_eq!(get(&v, "say \"hi\""), &LimaValue::String("value".into()));
}
#[test]
fn space_before_colon_strict_error() {
assert!(parse_core("\"first name\" : Alice", true).is_err());
}
}