use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::{Arc, LazyLock};
use serde_json::{Map, Value};
use crate::attributes::{attributes_end, parse_attributes};
use crate::js::{JS_DOT, JS_SPACES, blank, is_str, js_not_space, js_trim, js_value, num, truthy, utf16_less};
use crate::pattern::Pattern;
use crate::policy::{Policy, PolicyLayer};
use crate::registry::{self, AttrSpec, BACKMATTER_KEYS, KindSpec, attr_spec, is_rule, keyword_names, registry};
use crate::types::{
Attrs, Diagnostic, Document, Form, Identity, KeyOrder, Metadata, Node, Object, Parameter, Range, Severity, Span, TREE_FORMAT,
};
use crate::yaml::{load_csv, load_json, load_yaml};
pub(crate) const MAX_NESTING: usize = 128;
#[derive(Clone, Debug, Default)]
pub struct ReadOptions {
pub fragment: bool,
pub uri: String,
pub host: Option<PolicyLayer>,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ProseRef {
pub(crate) source: String,
pub(crate) range: Option<Range>,
}
#[derive(Clone, Debug)]
pub struct Reading {
pub document: Document,
pub diagnostics: Vec<Diagnostic>,
pub identities: Vec<Identity>,
pub parameters: Vec<Parameter>,
pub footnotes: BTreeMap<String, u32>,
pub comments: BTreeSet<u32>,
pub lines: Vec<String>,
pub front_range: Option<Range>,
pub back_range: Option<Range>,
pub policy: Option<Policy>,
identity_index: HashMap<String, usize>,
parameter_index: HashMap<String, usize>,
pub(crate) prose_order: Vec<ProseRef>,
source: Arc<str>,
line_starts: Vec<usize>,
}
impl Reading {
pub(crate) fn empty() -> Reading {
Reading {
document: Document::default(),
diagnostics: Vec::new(),
identities: Vec::new(),
parameters: Vec::new(),
footnotes: BTreeMap::new(),
comments: BTreeSet::new(),
lines: Vec::new(),
front_range: None,
back_range: None,
policy: None,
identity_index: HashMap::new(),
parameter_index: HashMap::new(),
prose_order: Vec::new(),
source: Arc::from(""),
line_starts: Vec::new(),
}
}
pub fn source(&self) -> &str {
&self.source
}
pub fn line_offset(&self, line: u32) -> usize {
let i = (line as usize).saturating_sub(1);
self.line_starts.get(i).copied().unwrap_or(self.source.len())
}
pub fn changes_text(&self) -> bool {
self.text(None).trim_end_matches('\n') != self.source.trim_end_matches('\n')
}
pub fn identity(&self, id: &str) -> Option<&Identity> {
self.identity_index.get(id).and_then(|&i| self.identities.get(i))
}
pub fn parameter(&self, name: &str) -> Option<&Parameter> {
self.parameter_index.get(name).and_then(|&i| self.parameters.get(i))
}
pub fn has_errors(&self) -> bool {
self.diagnostics.iter().any(Diagnostic::is_error)
}
pub(crate) fn add_identity(&mut self, id: Identity, replace: bool) {
if let Some(&i) = self.identity_index.get(&id.id) {
if replace {
self.identities[i] = id;
}
return;
}
self.identity_index.insert(id.id.clone(), self.identities.len());
self.identities.push(id);
}
}
pub(crate) struct Rx {
pub(crate) open: Pattern,
pub(crate) close: Pattern,
pub(crate) fence: Pattern,
pub(crate) math: Pattern,
pub(crate) keyword: Pattern,
pub(crate) because: Pattern,
pub(crate) alert: Pattern,
pub(crate) quote: Pattern,
pub(crate) item: Pattern,
pub(crate) item_start: Pattern,
pub(crate) heading: Pattern,
pub(crate) thematic: Pattern,
pub(crate) heading_attr: Pattern,
pub(crate) footnote_def: Pattern,
pub(crate) quote_start: Pattern,
pub(crate) directive: Pattern,
pub(crate) closer: Pattern,
pub(crate) html_block: Pattern,
pub(crate) html_tag_line: Pattern,
pub(crate) backmatter: Pattern,
pub(crate) yaml_comment: Pattern,
pub(crate) region_part: Pattern,
pub(crate) url: Pattern,
pub(crate) duration: Pattern,
pub(crate) decimal: Pattern,
pub(crate) html_literals: Vec<(Pattern, Pattern)>,
}
pub(crate) static RX: LazyLock<Rx> = LazyLock::new(|| {
let dot = JS_DOT;
let sp = JS_SPACES;
let ns = js_not_space();
let kw = keyword_names();
let p = |s: &str| Pattern::must(s);
Rx {
open: p(&format!(r"^(:{{2,}})([a-z][a-z0-9-]*(?::[a-z][a-z0-9-]*)?)({dot}*)$")),
close: p(r"^(:{3,})[ \t]*$"),
fence: p(&format!(r"^((?:[ \t]*>)*[ \t]*)(`{{3,}}|~{{3,}})({dot}*)$")),
math: p(r"^\$\$[ \t]*$"),
keyword: p(&format!(r"^(\*\*)?({kw})(?:\[([^\]{sp}]*)\])?(?: \(if ([^()\r\n]*)\))?:(\*\*)?[ \t]+({ns}{dot}*)$")),
because: p(&format!(r"^(\*\*)?BECAUSE:(\*\*)?[ \t]+({ns}{dot}*)$")),
alert: p(r"^ {0,3}>[ \t]?\[!([A-Za-z]+(?:[ \t]+[A-Za-z]+)?)\][ \t]*$"),
quote: p(&format!(r"^ {{0,3}}>[ \t]?({dot}*)$")),
item: p(&format!(r"^([ \t]*)([-*+]|[0-9]{{1,9}}[.)])([ \t]+)({dot}*)$")),
item_start: p(r"^ {0,3}(?:[-*+]|[0-9]{1,9}[.)])(?:[ \t]|$)"),
heading: p(r"^ {0,3}#{1,6}(?:[ \t]|$)"),
thematic: p(r"^ {0,3}(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$"),
heading_attr: p(&format!(r"^ {{0,3}}#{{1,6}}[ \t]{dot}*?[ \t](\{{[^{{}}]*\}})[ \t]*$")),
footnote_def: p(&format!(r"^ {{0,3}}\[\^([^\]{sp}]+)\]:(?:[ \t]|$)")),
quote_start: p(r"^ {0,3}>"),
directive: p(r"^:{2,}[a-z]"),
closer: p(r"^(?:[ \t]*>)*[ \t]*(`+|~+)[ \t]*$"),
html_block: p(&format!(
r"(?i)^ {{0,3}}</?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:[{sp}/>]|$)"
)),
html_tag_line: p(&format!(
r#"^ {{0,3}}(?:<[A-Za-z][A-Za-z0-9-]*(?:[{sp}]+[A-Za-z_:][\w.:-]*(?:[{sp}]*=[{sp}]*(?:[^{sp}"'=<>`]+|'[^']*'|"[^"]*"))?)*[{sp}]*/?>|</[A-Za-z][A-Za-z0-9-]*[{sp}]*>)[{sp}]*$"#
)),
backmatter: p(&format!(r"^(?:{}):(?:[ \t]|$)", BACKMATTER_KEYS.join("|"))),
yaml_comment: p(r"^[ \t]*#"),
region_part: p(r"^(?:0|1|0?\.[0-9]+|1\.0+)$"),
url: p(&format!(r"(?i)^https?://[^{sp}]+$")),
duration: p(r"^[0-9]+(?:ms|s|m|h|d|w)$"),
decimal: p(r"^[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$"),
html_literals: vec![
(p(&format!(r"(?i)^( {{0,3}}<(?:script|pre|style|textarea))(?:[{sp}>]|$)")), p(r"(?i)</(?:script|pre|style|textarea)>")),
(p(r"^( {0,3}<\?)"), p(r"\?>")),
(p(r"^( {0,3}<!\[CDATA\[)"), p(r"\]\]>")),
(p(r"^( {0,3}<![A-Za-z])"), p(r">")),
],
}
});
fn indent_of(s: &str) -> usize {
let lead = s.len() - s.trim_start_matches([' ', '\t']).len();
lead + 3 * s[..lead].matches('\t').count()
}
#[derive(Clone, Debug)]
struct Line {
text: String,
n: u32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum BodyKind {
Parsed,
Literal,
Structured,
Opaque,
}
#[derive(Clone, Copy, Debug)]
struct CodeFence {
ch: u8,
length: usize,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Shape {
Line,
Item,
Quote,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum UnitKind {
Rule,
Reason,
Alert,
}
struct Unit {
kind: UnitKind,
node: usize,
rule: Option<usize>,
lines: Vec<Line>,
original: Vec<Line>,
shape: Shape,
offset: usize,
blanks: Vec<Line>,
last: String,
}
struct Frame {
node: Option<Node>,
nodes: Vec<Node>,
fence: usize,
body: BodyKind,
format: String,
raw: Vec<Line>,
raw_code: Option<CodeFence>,
md: Vec<Line>,
code: Option<CodeFence>,
html_end: Option<usize>,
html_blank: bool,
math_end: u32,
unit: Option<Unit>,
last_rule: Option<usize>,
prev_blank: bool,
paragraph: bool,
prev_quote: bool,
in_list: bool,
}
impl Frame {
fn new(node: Option<Node>, colons: usize, body: BodyKind, format: &str) -> Frame {
Frame {
node,
nodes: Vec::new(),
fence: colons,
body,
format: format.to_string(),
raw: Vec::new(),
raw_code: None,
md: Vec::new(),
code: None,
html_end: None,
html_blank: false,
math_end: 0,
unit: None,
last_rule: None,
prev_blank: true,
paragraph: false,
prev_quote: false,
in_list: false,
}
}
}
#[derive(Clone, Copy, Default)]
struct ScopeOptions {
directives: bool,
keywords: bool,
document: bool,
}
struct Reader {
r: Reading,
opts: ReadOptions,
front: Object,
front_order: KeyOrder,
back: Object,
aliases: BTreeSet<String>,
depth: usize,
}
pub fn read(text: &str, opts: &ReadOptions) -> Reading {
let mut reader = Reader {
r: Reading::empty(),
opts: opts.clone(),
front: Object::new(),
front_order: KeyOrder::default(),
back: Object::new(),
aliases: BTreeSet::new(),
depth: 0,
};
let body = text.strip_prefix('\u{feff}').unwrap_or(text);
reader.r.lines = split_lines(body);
let bom = text.len() - body.len();
reader.r.line_starts = line_starts(body).into_iter().map(|i| i + bom).collect();
reader.r.source = Arc::from(text);
reader.run()
}
fn line_starts(body: &str) -> Vec<usize> {
let mut starts = vec![0];
let b = body.as_bytes();
let mut i = 0;
while i < b.len() {
match b[i] {
b'\n' => starts.push(i + 1),
b'\r' => {
if i + 1 < b.len() && b[i + 1] == b'\n' {
i += 1;
}
starts.push(i + 1);
}
_ => {}
}
i += 1;
}
starts
}
#[derive(Clone, Debug, PartialEq)]
pub struct SplitBackmatter<'a> {
pub before: &'a str,
pub backmatter: Option<&'a str>,
pub range: Option<Range>,
pub data: Object,
pub diagnostics: Vec<Diagnostic>,
}
pub fn split_backmatter(text: &str) -> SplitBackmatter<'_> {
let r = read(text, &ReadOptions::default());
let Some(range) = r.back_range else {
return SplitBackmatter { before: text, backmatter: None, range: None, data: Object::new(), diagnostics: Vec::new() };
};
let cut = r.line_offset(range.start).min(text.len());
let (before, backmatter) = if text.is_char_boundary(cut) { text.split_at(cut) } else { (text, "") };
let diagnostics =
r.diagnostics.iter().filter(|d| d.code == "invalid-metadata" && d.line().is_some_and(|l| l >= range.start)).cloned().collect();
SplitBackmatter { before, backmatter: Some(backmatter), range: Some(range), data: r.document.metadata.back, diagnostics }
}
pub(crate) fn split_lines(body: &str) -> Vec<String> {
let mut lines = Vec::new();
let b = body.as_bytes();
let mut start = 0;
let mut i = 0;
while i < b.len() {
match b[i] {
b'\n' => {
lines.push(body[start..i].to_string());
start = i + 1;
}
b'\r' => {
lines.push(body[start..i].to_string());
if i + 1 < b.len() && b[i + 1] == b'\n' {
i += 1;
}
start = i + 1;
}
_ => {}
}
i += 1;
}
if start < b.len() {
lines.push(body[start..].to_string());
}
lines
}
pub(crate) fn sort_diagnostics(ds: &mut [Diagnostic], by_uri: bool) {
let start = |d: &Diagnostic| d.range.map_or(0, |r| r.start);
ds.sort_by(|a, b| {
if by_uri && a.uri != b.uri {
return if utf16_less(&a.uri, &b.uri) { std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater };
}
start(a).cmp(&start(b)).then_with(|| a.code.cmp(b.code))
});
}
impl Reader {
fn diag(&mut self, code: &'static str, severity: Severity, message: impl Into<String>, line: u32) {
self.r.diagnostics.push(Diagnostic::new(code, severity, message, line));
}
fn run(mut self) -> Reading {
let mut first = 0;
if !self.opts.fragment && self.r.lines.first().is_some_and(|l| l == "---") {
let close = self.r.lines.iter().skip(1).position(|l| l == "---").map(|p| p + 1);
match close {
None => {
self.diag("invalid-metadata", Severity::Error, "the front matter is not closed: it ends with a line of exactly ---", 1)
}
Some(close) => {
self.r.front_range = Some(Range::new(1, close as u32 + 1));
let text = self.r.lines[1..close].join("\n");
let (front, order) = self.metadata(&text, 2, "front matter");
self.front = front;
self.front_order = order;
first = close + 1;
}
}
}
self.check_front();
let lines: Vec<Line> =
self.r.lines[first..].iter().enumerate().map(|(i, t)| Line { text: t.clone(), n: (first + i + 1) as u32 }).collect();
let body = self.scope(&lines, ScopeOptions { directives: true, keywords: true, document: !self.opts.fragment });
self.check_parameters();
if !self.opts.fragment {
let at = self.r.front_range.map_or(1, |r| r.start);
self.r.policy =
crate::policy::check(&self.front, &self.front_order, at, self.opts.host.as_ref(), &body, &mut self.r.diagnostics);
}
let front = std::mem::take(&mut self.front);
let back = std::mem::take(&mut self.back);
self.r.document = Document {
format: TREE_FORMAT.into(),
uri: self.opts.uri.clone(),
metadata: Metadata { front, back, front_order: std::mem::take(&mut self.front_order) },
body,
raw: None,
};
sort_diagnostics(&mut self.r.diagnostics, false);
self.r
}
fn metadata(&mut self, yaml: &str, at: u32, what: &str) -> (Object, KeyOrder) {
match load_yaml(yaml) {
Err(e) => {
self.diag("invalid-metadata", Severity::Error, format!("the {what} is not valid YAML: {e}"), at);
(Object::new(), KeyOrder::default())
}
Ok((Value::Object(m), order)) => (m, order),
Ok(_) => {
self.diag("invalid-metadata", Severity::Error, format!("the {what} must be a YAML mapping"), at);
(Object::new(), KeyOrder::default())
}
}
}
fn scope(&mut self, lines: &[Line], o: ScopeOptions) -> Vec<Node> {
self.depth += 1;
let o = if self.depth > MAX_NESTING { ScopeOptions { keywords: false, ..o } } else { o };
let mut stack: Vec<Frame> = vec![Frame::new(None, usize::MAX, BodyKind::Parsed, "markdown")];
let mut comment_open = false;
let mut comment_line = 0;
let rx = &*RX;
let mut li = 0;
while li < lines.len() {
let ln = &lines[li];
li += 1;
let top_body = stack.last().map_or(BodyKind::Parsed, |f| f.body);
if top_body != BodyKind::Parsed {
self.raw_line(&mut stack, ln);
continue;
}
let Some(top) = stack.last_mut() else { break };
if let Some(code) = top.code {
literal_line(top, ln);
if closes_fence(&ln.text, code) {
top.code = None;
}
continue;
}
if let Some(end) = top.html_end {
literal_line(top, ln);
if rx.html_literals[end].1.is_match(&ln.text) {
top.html_end = None;
}
continue;
}
if top.math_end > 0 {
literal_line(top, ln);
if ln.n == top.math_end {
top.math_end = 0;
}
continue;
}
let mut cur = ln.clone();
if comment_open || ln.text.contains("<!--") {
let stripped = strip_comments(&ln.text, comment_open);
if stripped.opened && !comment_open {
comment_line = ln.n;
}
comment_open = stripped.still_open;
if stripped.only {
self.r.comments.insert(ln.n);
continue;
}
cur.text = stripped.text;
}
let depth = stack.len();
let top = stack.last_mut().expect("the root frame");
if top.html_blank {
if blank(&cur.text) {
top.html_blank = false;
} else {
literal_line(top, &cur);
continue;
}
}
if top.unit.is_some() {
if self.continue_unit(top, &cur) {
continue;
}
self.end_unit(top);
}
if o.document && depth == 1 && cur.text == "---" && top.prev_blank && li > 1 && backmatter_follows(lines, li - 1) {
self.backmatter(lines, li - 1);
break;
}
if let Some(m) = rx.fence.captures(&cur.text) {
let marker = m.get(&cur.text, 2).unwrap_or("```");
let info = m.get(&cur.text, 3).unwrap_or("");
if !(marker.starts_with('`') && info.contains('`')) {
literal_line(top, &cur);
top.code = Some(CodeFence { ch: marker.as_bytes()[0], length: marker.len() });
continue;
}
}
if let Some((h, n)) = opening_html_literal(&cur.text) {
literal_line(top, &cur);
if !rx.html_literals[h].1.is_match(&cur.text[n..]) {
top.html_end = Some(h);
}
continue;
}
if html_block(&cur.text) || !top.paragraph && rx.html_tag_line.is_match(&cur.text) {
literal_line(top, &cur);
top.html_blank = true;
continue;
}
if rx.math.is_match(&cur.text) {
if let Some(end) = math_end(lines, li - 1) {
literal_line(top, &cur);
top.math_end = end;
continue;
}
self.diag(
"math-unclosed",
Severity::Warning,
"a $$ display formula has no closing $$ line before the next directive; it stays as text",
cur.n,
);
}
if o.directives {
if let Some(m) = rx.close.captures(&cur.text)
&& stack.len() > 1
{
let colons = m.get(&cur.text, 1).map_or(0, str::len);
self.close(&mut stack, colons, cur.n, false);
continue;
}
if let Some(m) = rx.open.captures(&cur.text) {
let colons = m.get(&cur.text, 1).unwrap_or("").len();
let kind = m.get(&cur.text, 2).unwrap_or("").to_string();
let rest = m.get(&cur.text, 3).unwrap_or("").to_string();
if self.open(&mut stack, colons, &kind, &rest, &cur) {
continue;
}
}
}
let top = stack.last_mut().expect("the root frame");
if o.keywords && self.start_unit(top, &cur) {
continue;
}
self.prose_line(top, &cur);
}
if comment_open {
self.diag("comment-unclosed", Severity::Warning, "a comment is not closed; it runs to the end of the file", comment_line);
}
while stack.len() > 1 {
let f = stack.last().expect("an open frame");
let (kind, start) = f.node.as_ref().map_or((String::new(), 0), |n| (n.kind.clone(), n.range.map_or(0, |r| r.start)));
let fence = f.fence;
self.diag(
"unclosed-directive",
Severity::Error,
format!(":::{kind} is not closed: end it with a line of {}", ":".repeat(fence)),
start,
);
let end = lines.last().map_or(start, |l| l.n);
self.close(&mut stack, fence, end, true);
}
let mut root = stack.pop().expect("the root frame");
self.end_frame(&mut root);
self.depth -= 1;
root.nodes
}
fn prose_line(&mut self, f: &mut Frame, ln: &Line) {
let rx = &*RX;
let t = &ln.text;
if !blank(t) {
f.last_rule = None;
if let Some(h) = rx.heading_attr.captures(t)
&& let Some(list) = h.get(t, 1)
{
self.heading_identity(list, ln.n);
}
if let Some(fn_) = rx.footnote_def.captures(t)
&& let Some(label) = fn_.get(t, 1)
{
if !registry().id_pattern().is_match(label) {
self.diag(
"invalid-attributes",
Severity::Error,
format!("the footnote label \"{label}\" is not valid: use letters, digits, \".\", \"_\" and \"-\""),
ln.n,
);
} else if let Some(&first) = self.r.footnotes.get(label) {
self.diag(
"footnote-duplicate",
Severity::Error,
format!("the footnote [^{label}] is defined twice (first on line {first})"),
ln.n,
);
} else {
self.r.footnotes.insert(label.to_string(), ln.n);
}
}
}
if rx.item_start.is_match(t) {
f.in_list = !rx.thematic.is_match(t);
} else {
f.in_list = f.in_list && (blank(t) || indent_of(t) >= 2);
}
f.prev_quote = rx.quote.is_match(t) || f.prev_quote && !blank(t) && !starts_block(t);
f.md.push(ln.clone());
f.prev_blank = blank(t);
f.paragraph = !blank(t) && !rx.heading.is_match(t) && !rx.thematic.is_match(t);
}
fn flush(&mut self, f: &mut Frame) {
if f.md.iter().any(|l| !blank(&l.text)) {
let md = std::mem::take(&mut f.md);
let node = self.markdown(&md);
f.nodes.push(node);
}
f.md.clear();
}
fn before_node(&mut self, f: &mut Frame) {
self.flush(f);
f.last_rule = None;
f.prev_blank = false;
f.paragraph = false;
f.prev_quote = false;
f.in_list = false;
}
fn push(&mut self, f: &mut Frame, node: Node) -> usize {
self.before_node(f);
let rule = is_rule(&node.kind);
f.nodes.push(node);
let at = f.nodes.len() - 1;
if rule {
f.last_rule = Some(at);
}
at
}
fn markdown(&mut self, lines: &[Line]) -> Node {
let texts: Vec<&str> = lines.iter().map(|l| l.text.as_str()).collect();
let mut node = Node::markdown(texts.join("\n") + "\n");
node.range = Some(Range::new(lines[0].n, lines[lines.len() - 1].n));
node.prose = true;
self.r.prose_order.push(ProseRef { source: node.source.clone(), range: node.range });
node
}
fn start_unit(&mut self, f: &mut Frame, ln: &Line) -> bool {
let rx = &*RX;
let t = &ln.text;
if let Some(alert) = rx.alert.captures(t)
&& !f.prev_quote
{
let written = alert.get(t, 1).unwrap_or("");
let keyword = written.to_uppercase().split([' ', '\t']).filter(|s| !s.is_empty()).collect::<Vec<_>>().join(" ");
if let Some(kind) = registry().keywords.get(&keyword) {
let mut node = Node::directive(kind.clone(), Form::Alert, keyword_flags(&keyword));
node.range = Some(Range::line(ln.n));
node.keyword = Some(keyword);
let at = self.push(f, node);
f.unit = Some(Unit {
kind: UnitKind::Alert,
node: at,
rule: None,
lines: Vec::new(),
original: Vec::new(),
shape: Shape::Line,
offset: 0,
blanks: Vec::new(),
last: t.clone(),
});
return true;
}
}
let (shape, content, offset): (Shape, String, usize);
if let Some(q) = rx.quote.captures(t) {
shape = Shape::Quote;
content = q.get(t, 1).unwrap_or("").to_string();
offset = 0;
} else if let Some(item) =
rx.item.captures(t).filter(|item| !rx.thematic.is_match(t) && (indent_of(item.get(t, 1).unwrap_or("")) <= 3 || f.in_list))
{
shape = Shape::Item;
content = item.get(t, 4).unwrap_or("").to_string();
let mut gap = indent_of(item.get(t, 3).unwrap_or(""));
if gap > 4 {
gap = 1;
}
offset = indent_of(item.get(t, 1).unwrap_or("")) + item.get(t, 2).unwrap_or("").len() + gap;
} else if indent_of(t) > 3 {
return false;
} else {
shape = Shape::Line;
content = t.trim_start_matches([' ', '\t']).to_string();
offset = 0;
}
if let Some(k) = rx.keyword.captures(&content) {
let Some(node) = self.rule_node(&content, &k, ln) else { return false };
let text = k.get(&content, 6).unwrap_or("").to_string();
let at = self.push(f, node);
f.unit = Some(Unit {
kind: UnitKind::Rule,
node: at,
rule: None,
lines: vec![Line { text, n: ln.n }],
original: Vec::new(),
shape,
offset,
blanks: Vec::new(),
last: String::new(),
});
return true;
}
if let Some(b) = rx.because.captures(&content)
&& b.span(1).is_none() == b.span(2).is_none()
{
let rule = f.last_rule;
self.flush(f);
f.unit = Some(Unit {
kind: UnitKind::Reason,
node: 0,
rule,
lines: vec![Line { text: b.get(&content, 3).unwrap_or("").to_string(), n: ln.n }],
original: vec![ln.clone()],
shape,
offset,
blanks: Vec::new(),
last: String::new(),
});
return true;
}
false
}
fn rule_node(&mut self, content: &str, m: &crate::pattern::Captures, ln: &Line) -> Option<Node> {
let open = m.get(content, 1).unwrap_or("");
let keyword = m.get(content, 2).unwrap_or("").to_string();
let id = m.get(content, 3);
let cond = m.get(content, 4);
let closing = m.get(content, 5).unwrap_or("");
if open.is_empty() != closing.is_empty() {
return None;
}
let kind = registry().keywords.get(&keyword).cloned().unwrap_or_default();
let mut attrs = Attrs::new();
if let Some(id) = id {
if registry().id_pattern().is_match(id) {
attrs.insert("id".into(), Value::String(id.to_string()));
} else {
self.diag(
"invalid-attributes",
Severity::Error,
format!("\"{id}\" is not a valid rule name: use letters, digits, \".\", \"_\" and \"-\""),
ln.n,
);
}
}
if let Some(cond) = cond {
if !is_rule(&kind) {
self.diag(
"invalid-attributes",
Severity::Error,
format!("{keyword} is advice, not a rule: it takes no (if …) condition"),
ln.n,
);
} else if js_trim(cond).is_empty() {
self.diag("invalid-attributes", Severity::Error, "a rule's (if …) condition is not empty", ln.n);
} else {
attrs.insert("if".into(), Value::String(cond.to_string()));
}
}
for (k, v) in keyword_flags(&keyword) {
attrs.insert(k, v);
}
let mut node = Node::directive(kind.clone(), Form::Keyword, attrs);
node.range = Some(Range::line(ln.n));
node.keyword = Some(keyword);
if let Some(Value::String(id)) = node.attrs.get("id").cloned() {
self.identity(&id, &kind, ln.n);
}
Some(node)
}
fn continue_unit(&mut self, f: &mut Frame, ln: &Line) -> bool {
let rx = &*RX;
let t = &ln.text;
let Some(u) = f.unit.as_mut() else { return false };
if u.kind == UnitKind::Alert {
if let Some(q) = rx.quote.captures(t) {
let text = q.get(t, 1).unwrap_or("").to_string();
u.last = text.clone();
u.lines.push(Line { text, n: ln.n });
return true;
}
if !blank(t) && !blank(&u.last) && !starts_block(t) {
u.lines.push(Line { text: js_trim(t).to_string(), n: ln.n });
u.last = t.clone();
return true;
}
return false;
}
let keep = |u: &mut Unit, text: String| {
if u.kind == UnitKind::Reason {
u.original.push(ln.clone());
}
u.lines.push(Line { text, n: ln.n });
true
};
match u.shape {
Shape::Quote => {
if let Some(q) = rx.quote.captures(t) {
let inner = q.get(t, 1).unwrap_or("");
if blank(inner) || rx.keyword.is_match(inner) || rx.because.is_match(inner) {
return false;
}
return keep(u, inner.to_string());
}
if blank(t) || starts_block(t) {
return false;
}
keep(u, js_trim(t).to_string())
}
Shape::Item => {
if blank(t) {
u.blanks.push(ln.clone());
return true;
}
if indent_of(t) >= u.offset {
let inner = dedent(t, u.offset);
if u.kind == UnitKind::Rule
&& u.blanks.is_empty()
&& let Some(b) = rx.because.captures(inner)
{
let rule = u.node;
let offset = u.offset;
let reason = b.get(inner, 3).unwrap_or("").to_string();
self.end_unit(f);
f.unit = Some(Unit {
kind: UnitKind::Reason,
node: 0,
rule: Some(rule),
lines: vec![Line { text: reason, n: ln.n }],
original: vec![ln.clone()],
shape: Shape::Item,
offset,
blanks: Vec::new(),
last: String::new(),
});
return true;
}
let inner = inner.to_string();
let blanks = std::mem::take(&mut u.blanks);
if u.kind == UnitKind::Reason {
u.original.extend(blanks.iter().cloned());
u.original.push(ln.clone());
}
u.lines.extend(blanks);
u.lines.push(Line { text: inner, n: ln.n });
return true;
}
if !u.blanks.is_empty() || starts_block(t) || interrupts(t) {
return false;
}
keep(u, js_trim(t).to_string())
}
Shape::Line => {
if blank(t) || starts_block(t) || interrupts(t) {
return false;
}
keep(u, t.clone())
}
}
}
fn end_unit(&mut self, f: &mut Frame) {
let Some(u) = f.unit.take() else { return };
if u.kind == UnitKind::Alert {
let children = self.scope(&u.lines, ScopeOptions::default());
if let Some(node) = f.nodes.get_mut(u.node) {
node.children = Some(children);
let start = node.range.map_or(0, |r| r.start);
let end = u.lines.last().map_or(start, |l| l.n);
node.range = Some(Range::new(start, end));
f.last_rule = is_rule(&node.kind).then_some(u.node);
}
return;
}
let pending = u.blanks;
if u.kind == UnitKind::Rule {
let children = if u.shape == Shape::Item {
self.scope(&u.lines, ScopeOptions { keywords: true, ..ScopeOptions::default() })
} else {
vec![self.markdown(&u.lines)]
};
if let Some(node) = f.nodes.get_mut(u.node) {
node.children = Some(children);
let start = node.range.map_or(0, |r| r.start);
node.range = Some(Range::new(start, u.lines.last().map_or(start, |l| l.n)));
}
f.last_rule = Some(u.node);
} else {
let texts: Vec<&str> = u.lines.iter().map(|l| l.text.as_str()).collect();
let first = u.lines.first().map_or(0, |l| l.n);
match u.rule {
None => {
self.diag(
"orphan-reason",
Severity::Warning,
"BECAUSE: follows no rule here, so it stays prose: put it right after the rule it explains",
first,
);
for l in &u.original {
self.prose_line(f, l);
}
}
Some(rule) if f.nodes.get(rule).is_some_and(|n| n.attrs.contains_key("because")) => {
self.diag("reason-repeated", Severity::Error, "this rule already has a reason: give it one BECAUSE", first);
for l in &u.original {
self.prose_line(f, l);
}
}
Some(rule) => {
if let Some(node) = f.nodes.get_mut(rule) {
node.attrs.insert("because".into(), Value::String(texts.join("\n")));
node.reason = Some(Range::new(first, u.lines.last().map_or(first, |l| l.n)));
}
f.last_rule = Some(rule);
}
}
}
for l in &pending {
self.prose_line(f, l);
}
}
fn open(&mut self, stack: &mut Vec<Frame>, colons: usize, kind: &str, rest: &str, ln: &Line) -> bool {
let rest = rest.trim_end_matches([' ', '\t']);
let mut attr_text = "";
let mut has_attrs = false;
let mut attr_broken = false;
if !rest.is_empty() {
if !rest.starts_with('{') {
return false; }
if attributes_end(rest, 0) == Some(rest.len()) {
attr_text = &rest[1..rest.len() - 1];
has_attrs = true;
} else {
attr_broken = true;
}
}
let leaf = colons == 2;
let spec = registry::kind(kind);
let extension = kind.contains(':');
let (form, where_) = if leaf { (Form::Leaf, format!("::{kind}")) } else { (Form::Container, format!(":::{kind}")) };
if !leaf && stack.len() > MAX_NESTING {
self.diag(
"invalid-nesting",
Severity::Error,
format!("{where_} opens deeper than {MAX_NESTING} directives; it stays prose"),
ln.n,
);
return false;
}
let mut node = Node::directive(kind, form, Attrs::new());
node.range = Some(Range::line(ln.n));
if attr_broken {
self.diag(
"invalid-attributes",
Severity::Error,
format!("{where_}: its attribute list must close with }} at the end of the line"),
ln.n,
);
}
if has_attrs {
let owned_kind = kind.to_string();
let spec_of = move |key: &str| -> Option<&'static AttrSpec> {
spec?;
attr_spec(&owned_kind, key)
};
let (attrs, problems) = parse_attributes(attr_text, &spec_of, spec.is_some());
node.attrs = attrs;
for p in problems {
self.diag("invalid-attributes", Severity::Error, format!("{where_}: {p}"), ln.n);
}
}
if spec.is_none() && !extension {
self.diag(
"unknown-kind",
Severity::Error,
format!("{where_} is not a kind of this specification; it is kept as written, uninterpreted"),
ln.n,
);
}
if extension {
let alias = kind.split(':').next().unwrap_or("");
let declared = self.front.get("extensions").and_then(Value::as_object).and_then(|e| e.get(alias));
if !truthy(declared) {
self.diag(
"extension-unavailable",
Severity::Error,
format!("{where_}: the extension alias \"{alias}\" is not declared in the front matter's extensions"),
ln.n,
);
}
}
if let Some(spec) = spec {
self.check_directive(&node, spec, stack, ln.n, &where_);
}
if let Some(id) = node.attrs.get("id") {
let id = js_value(Some(id));
self.identity(&id, kind, ln.n);
}
if kind == "include"
&& let Some(Value::String(alias)) = node.attrs.get("as")
{
if self.aliases.contains(alias) {
self.diag("duplicate-id", Severity::Error, format!("the include alias \"{alias}\" is used twice"), ln.n);
}
self.aliases.insert(alias.clone());
}
let depth = stack.len();
let top = stack.last_mut().expect("the root frame");
if top.unit.is_some() {
self.end_unit(top);
}
if leaf {
if kind == "param" {
self.param_leaf(&node, ln.n);
}
self.push(top, node);
return true;
}
if depth > 1 && colons >= top.fence {
self.diag(
"invalid-nesting",
Severity::Error,
format!(
"{where_} opens inside a {} container with as many colons or more: an inner fence is shorter than its outer one",
":".repeat(top.fence)
),
ln.n,
);
}
let mut body = BodyKind::Parsed;
let mut format = "markdown".to_string();
match spec {
None => body = BodyKind::Opaque,
Some(spec) => {
format = if spec.body == "yaml" { "yaml".into() } else { "markdown".into() };
if let Some(Value::String(chosen)) = node.attrs.get("format")
&& spec.formats().contains(chosen)
{
format = chosen.clone();
}
if format != "markdown" {
body = BodyKind::Structured;
} else if spec.opaque || node.attrs.get("verbatim") == Some(&Value::Bool(true)) {
body = BodyKind::Literal;
}
}
}
let top = stack.last_mut().expect("the root frame");
self.before_node(top);
stack.push(Frame::new(Some(node), colons, body, &format));
true
}
fn check_directive(&mut self, node: &Node, spec: &KindSpec, stack: &[Frame], at: u32, where_: &str) {
let form = node.form.map_or("container", Form::as_str);
if !spec.takes(form) {
let forms: Vec<String> = spec
.forms
.iter()
.filter_map(|f| match f.as_str() {
"leaf" => Some(format!("::{}{{…}}", node.kind)),
"container" => Some(format!(":::{} … :::", node.kind)),
_ => None,
})
.collect();
self.diag("invalid-nesting", Severity::Error, format!("{where_}: {} is written {}", node.kind, forms.join(" or ")), at);
}
for key in &spec.required {
match node.attrs.get(key) {
None => self.diag("invalid-attributes", Severity::Error, format!("{where_} needs {key}=…"), at),
Some(Value::String(s)) if s.is_empty() => {
self.diag("invalid-attributes", Severity::Error, format!("{where_} needs {key}=…"), at)
}
_ => {}
}
}
if let Some(parent) = &spec.parent {
let actual = stack.iter().rev().find_map(|f| f.node.as_ref().map(|n| n.kind.as_str())).unwrap_or("");
if actual != parent {
self.diag("invalid-nesting", Severity::Error, format!("{where_} belongs inside :::{parent}"), at);
}
}
let a = &node.attrs;
if let Some(Value::String(format)) = a.get("format")
&& !spec.formats().contains(format)
{
self.diag("invalid-attributes", Severity::Error, format!("{where_}: format is {}", spec.formats().join(" or ")), at);
}
if is_str(a.get("mode"), "pinned") && !a.contains_key("sha256") {
self.diag("pin-mismatch", Severity::Error, format!("{where_}: a pinned reference needs sha256=sha256:<64 hex>"), at);
}
if spec.family == "connected"
&& let Some(Value::String(p)) = a.get("protocol")
&& !crate::allowance::protocol_ok(p)
{
self.diag(
"protocol-syntax",
Severity::Error,
format!("{where_}: protocol is a name, optionally with versions: a2a, x402/2, mcp/2026-07-28 or com.example/rail/1 (it is \"{p}\")"),
at,
);
}
if node.kind == "allowance" {
for p in crate::allowance::problems(a) {
self.diag(p.code, Severity::Error, format!("{where_}: {}", p.message), at);
if let (Some(Value::String(id)), Some(d)) = (a.get("id"), self.r.diagnostics.last_mut()) {
d.target = format!("#{id}");
}
}
}
if let (Some(start), Some(end)) = (a.get("start").and_then(Value::as_f64), a.get("end").and_then(Value::as_f64))
&& end <= start
{
self.diag("invalid-attributes", Severity::Error, format!("{where_}: end is after start"), at);
}
if let Some(Value::String(region)) = a.get("region")
&& !valid_region(region)
{
self.diag(
"invalid-attributes",
Severity::Error,
format!("{where_}: region is x,y,width,height, fractions in [0,1] inside the unit square"),
at,
);
}
}
fn close(&mut self, stack: &mut Vec<Frame>, colons: usize, n: u32, at_eof: bool) {
let Some(mut f) = stack.pop() else { return };
let kind = f.node.as_ref().map_or(String::new(), |node| node.kind.clone());
if !at_eof && colons < f.fence {
self.diag(
"invalid-nesting",
Severity::Error,
format!("the closing fence on line {n} is shorter than the :::{kind} it closes ({} colons)", f.fence),
n,
);
}
if let Some(node) = f.node.as_mut() {
let start = node.range.map_or(n, |r| r.start);
node.range = Some(Range::new(start, n));
let from = self.r.line_offset(start + 1);
let to = if at_eof { self.r.source.len() } else { self.r.line_offset(n) };
node.body = Some(Span { text: self.r.source.clone(), start: from, end: to.max(from) });
}
self.end_frame(&mut f);
let Some(node) = f.node.take() else { return };
let Some(parent) = stack.last_mut() else { return };
let rule = is_rule(&node.kind);
parent.nodes.push(node);
parent.last_rule = rule.then_some(parent.nodes.len() - 1);
parent.prev_blank = false;
parent.paragraph = false;
parent.prev_quote = false;
parent.in_list = false;
}
fn end_frame(&mut self, f: &mut Frame) {
if f.body == BodyKind::Parsed {
if f.unit.is_some() {
self.end_unit(f);
}
self.flush(f);
if let Some(node) = f.node.as_mut() {
node.children = Some(std::mem::take(&mut f.nodes));
}
return;
}
let Some(node) = f.node.as_mut() else { return };
let text =
if f.raw.is_empty() { String::new() } else { f.raw.iter().map(|l| l.text.as_str()).collect::<Vec<_>>().join("\n") + "\n" };
let start = node.range.map_or(0, |r| r.start);
let at = f.raw.first().map_or(start, |l| l.n);
if f.body == BodyKind::Literal || f.body == BodyKind::Opaque {
let mut children = Vec::new();
if !text.is_empty() {
let mut md = Node::markdown(text.clone());
md.range = Some(Range::new(at, f.raw.last().map_or(at, |l| l.n)));
children.push(md);
}
node.children = Some(children);
if f.body == BodyKind::Opaque {
let end = node.range.map_or(start, |r| r.end) as usize;
let from = (start as usize).saturating_sub(1).min(self.r.lines.len());
let to = end.min(self.r.lines.len()).max(from);
node.source = self.r.lines[from..to].join("\n") + "\n";
}
return;
}
let kind = node.kind.clone();
let mut value =
crate::types::StructuredValue { format: f.format.clone(), source: text.clone(), data: None, order: KeyOrder::default() };
let upper = f.format.to_uppercase();
let mut problems: Vec<String> = Vec::new();
match f.format.as_str() {
"yaml" | "json" => {
let loaded = if f.format == "yaml" { load_yaml(&text) } else { load_json(&text) };
match loaded {
Err(e) => problems.push(format!(":::{kind}: its {upper} body is invalid: {e}")),
Ok((data, order)) => {
if kind != "data" && !data.is_object() {
problems.push(format!(":::{kind}: its body is a {upper} mapping"));
}
value.data = Some(data);
value.order = order;
}
}
}
"csv" => match load_csv(&text) {
Err(e) => problems.push(format!(":::{kind}: {e}")),
Ok((header, rows)) => {
let records = rows
.into_iter()
.map(|row| {
let mut record = Map::new();
for (h, cell) in header.iter().zip(row) {
record.insert(h.clone(), Value::String(cell));
}
Value::Object(record)
})
.collect();
value.data = Some(Value::Array(records));
}
},
_ => {}
}
node.value = Some(value);
for p in problems {
self.diag("invalid-structured-body", Severity::Error, p, at);
}
}
fn raw_line(&mut self, stack: &mut Vec<Frame>, ln: &Line) {
let rx = &*RX;
let Some(f) = stack.last_mut() else { return };
if let Some(code) = f.raw_code {
f.raw.push(ln.clone());
if closes_fence(&ln.text, code) {
f.raw_code = None;
}
return;
}
if let Some(m) = rx.close.captures(&ln.text) {
let colons = m.get(&ln.text, 1).map_or(0, str::len);
if colons >= f.fence {
self.close(stack, colons, ln.n, false);
return;
}
}
if f.body == BodyKind::Literal
&& let Some(m) = rx.fence.captures(&ln.text)
{
let marker = m.get(&ln.text, 2).unwrap_or("```");
let info = m.get(&ln.text, 3).unwrap_or("");
if !(marker.starts_with('`') && info.contains('`')) {
f.raw_code = Some(CodeFence { ch: marker.as_bytes()[0], length: marker.len() });
}
}
f.raw.push(ln.clone());
}
fn backmatter(&mut self, lines: &[Line], li: usize) {
let rest = &lines[li + 1..];
let end = rest.last().map_or(lines[li].n, |l| l.n);
self.r.back_range = Some(Range::new(lines[li].n, end));
let text = rest.iter().map(|l| l.text.as_str()).collect::<Vec<_>>().join("\n");
let (back, _) = self.metadata(&text, lines[li].n + 1, "backmatter");
self.back = back;
if let Some(sources) = self.back.get("sources")
&& !sources.is_array()
{
self.diag("invalid-metadata", Severity::Error, "the backmatter's sources are a list of {id, title, uri} records", lines[li].n);
}
}
fn check_front(&mut self) {
let at = self.r.front_range.map_or(1, |r| r.start);
if let Some(spec) = self.front.get("spec").cloned() {
let spec = if spec.as_f64() == Some(1.0) {
self.front.insert("spec".into(), Value::String("1".into()));
Value::String("1".into())
} else {
spec
};
if !is_str(Some(&spec), "1") {
self.diag(
"unsupported-format",
Severity::Error,
format!("spec: \"{}\" is not a format this reader knows; it reads spec: \"1\"", js_value(Some(&spec))),
at,
);
}
}
for key in ["id", "version", "title", "description", "language", "license"] {
if let Some(v) = self.front.get(key)
&& !v.is_string()
{
self.diag("invalid-metadata", Severity::Error, format!("the front matter's {key} is a string"), at);
}
}
if let Some(tags) = self.front.get("tags")
&& !string_list(tags)
{
self.diag("invalid-metadata", Severity::Error, "the front matter's tags are a list of strings", at);
}
for key in ["parameters", "extensions", "references", "resolvers", "exports", "presentation"] {
if let Some(v) = self.front.get(key)
&& !v.is_object()
{
self.diag("invalid-metadata", Severity::Error, format!("the front matter's {key} is a mapping"), at);
}
}
if let Some(ext) = self.front.get("extends").cloned() {
match ext {
Value::Array(list) => {
for base in list {
let uri = base.get("uri").and_then(Value::as_str);
let alias = base.get("as").and_then(Value::as_str);
match (uri, alias) {
(Some(_), Some(alias)) => {
if self.aliases.contains(alias) {
self.diag("duplicate-id", Severity::Error, format!("the base alias \"{alias}\" is used twice"), at);
} else {
self.aliases.insert(alias.to_string());
}
}
_ => self.diag("invalid-metadata", Severity::Error, "each extends record has a uri and an as alias", at),
}
}
}
_ => self.diag("invalid-metadata", Severity::Error, "extends is a list of {uri, as} records", at),
}
}
let extensions = self.front.get("extensions").and_then(Value::as_object).cloned().unwrap_or_default();
for alias in self.front_order.keys("extensions", &extensions) {
let b = extensions.get(&alias);
let has_id = b.and_then(|b| b.get("id")).is_some_and(Value::is_string);
let has_version = b.and_then(|b| b.get("version")).is_some_and(Value::is_string);
if !has_id || !has_version {
self.diag("invalid-metadata", Severity::Error, format!("the extension \"{alias}\" binds an exact id and version"), at);
}
}
if let Some(req) = self.front.get("requires").cloned() {
match req {
Value::Array(list) => {
for alias in list {
let declared = alias.as_str().is_some_and(|name| extensions.contains_key(name));
if !declared {
self.diag(
"extension-unavailable",
Severity::Error,
format!("requires names \"{}\", which the extensions do not declare", js_value(Some(&alias))),
at,
);
}
}
}
_ => self.diag("invalid-metadata", Severity::Error, "requires is a list of extension aliases", at),
}
}
if let Some(Value::Object(params)) = self.front.get("parameters").cloned() {
for name in self.front_order.keys("parameters", ¶ms) {
match params.get(&name) {
Some(Value::Object(decl)) => self.declare(&name, decl, 0, at),
_ => {
self.diag("parameter-invalid", Severity::Error, format!("the parameter \"{name}\" is declared with a mapping"), at)
}
}
}
}
}
fn param_leaf(&mut self, node: &Node, at: u32) {
if let Some(Value::String(name)) = node.attrs.get("name") {
self.declare(&name.clone(), &node.attrs, at, at);
}
}
fn declare(&mut self, name: &str, d: &Object, declared_at: u32, at: u32) {
if !registry().parameter_pattern().is_match(name) {
self.diag("parameter-invalid", Severity::Error, format!("\"{name}\" is not a valid parameter name"), at);
return;
}
if self.r.parameter_index.contains_key(name) {
self.diag("parameter-invalid", Severity::Error, format!("the parameter \"{name}\" is declared twice"), at);
return;
}
let mut p = Parameter {
name: name.to_string(),
param_type: "string".into(),
required: d.get("required") == Some(&Value::Bool(true)),
line: declared_at,
..Parameter::default()
};
if let Some(Value::String(t)) = d.get("type") {
p.param_type = t.clone();
}
match d.get("values") {
Some(Value::Array(list)) => p.values = Some(list.iter().map(|x| js_value(Some(x))).collect()),
Some(Value::String(s)) => p.values = Some(s.split(',').map(|x| js_trim(x).to_string()).collect()),
_ => {}
}
p.default = d.get("default").cloned();
p.example = d.get("example").cloned();
if let Some(Value::String(s)) = d.get("description") {
p.description = Some(s.clone());
}
if let Some(Value::String(s)) = d.get("source") {
p.source = Some(s.clone());
}
self.r.parameter_index.insert(name.to_string(), self.r.parameters.len());
self.r.parameters.push(p);
}
fn check_parameters(&mut self) {
let front_at = self.r.front_range.map_or(1, |r| r.start);
let params = self.r.parameters.clone();
for p in ¶ms {
let at = if p.line == 0 { front_at } else { p.line };
if !PARAMETER_TYPES.contains(&p.param_type.as_str()) {
self.diag(
"parameter-invalid",
Severity::Error,
format!("the parameter \"{}\" has an unknown type \"{}\"", p.name, p.param_type),
at,
);
continue;
}
if p.param_type == "enum" && !p.values.as_ref().is_some_and(|v| !v.is_empty() && !v.iter().any(String::is_empty)) {
self.diag("parameter-invalid", Severity::Error, format!("the enum parameter \"{}\" lists its values", p.name), at);
}
if let Some(d) = &p.default
&& !value_fits(p, d)
{
self.diag(
"parameter-invalid",
Severity::Error,
format!("the default of \"{}\" is not a valid {}", p.name, p.param_type),
at,
);
}
}
}
fn identity(&mut self, id: &str, kind: &str, at: u32) {
if let Some(first) = self.r.identity(id).map(|i| i.line) {
self.diag(
"duplicate-id",
Severity::Error,
format!("the id \"{id}\" is used twice (first on line {first}); ids are unique in a document"),
at,
);
return;
}
self.r.add_identity(Identity { id: id.to_string(), kind: kind.to_string(), line: at }, false);
}
fn heading_identity(&mut self, list: &str, at: u32) {
let spec_of = |key: &str| -> Option<&'static AttrSpec> {
if matches!(key, "id" | "class" | "style") { attr_spec("section", key) } else { None }
};
let inner = list.get(1..list.len().saturating_sub(1)).unwrap_or("");
let (attrs, problems) = parse_attributes(inner, &spec_of, true);
for p in problems {
self.diag(
"invalid-attributes",
Severity::Error,
format!("heading attributes: {p}; a heading takes only id, class and style"),
at,
);
}
if let Some(Value::String(id)) = attrs.get("id") {
self.identity(id, "heading", at);
}
}
}
const PARAMETER_TYPES: &[&str] = &["string", "number", "integer", "boolean", "enum", "list", "url", "duration"];
fn keyword_flags(keyword: &str) -> Attrs {
let mut attrs = Attrs::new();
if let Some(flags) = registry().keyword_flags.get(keyword) {
for (k, v) in flags {
attrs.insert(k.clone(), v.clone());
}
}
attrs
}
fn literal_line(f: &mut Frame, ln: &Line) {
f.md.push(ln.clone());
f.last_rule = None;
f.prev_blank = false;
f.paragraph = false;
f.prev_quote = false;
}
fn opening_html_literal(t: &str) -> Option<(usize, usize)> {
for (i, (open, _)) in RX.html_literals.iter().enumerate() {
if let Some(c) = open.captures(t)
&& let Some((_, end)) = c.span(1)
{
return Some((i, end));
}
}
None
}
fn html_block(t: &str) -> bool {
RX.html_block.is_match(t)
}
fn closes_fence(text: &str, f: CodeFence) -> bool {
match RX.closer.captures(text).and_then(|m| m.get(text, 1).map(str::to_string)) {
Some(run) => run.as_bytes()[0] == f.ch && run.len() >= f.length,
None => false,
}
}
pub(crate) fn starts_block(t: &str) -> bool {
let rx = &*RX;
rx.heading.is_match(t)
|| rx.thematic.is_match(t)
|| rx.quote_start.is_match(t)
|| rx.item_start.is_match(t)
|| rx.fence.is_match(t)
|| rx.math.is_match(t)
|| rx.directive.is_match(t)
|| rx.close.is_match(t)
|| html_block(t)
|| opening_html_literal(t).is_some()
}
fn interrupts(t: &str) -> bool {
let mut s = t;
for _ in 0..3 {
match s.strip_prefix(' ') {
Some(rest) => s = rest,
None => break,
}
}
RX.keyword.is_match(s) || RX.because.is_match(s)
}
fn dedent(t: &str, n: usize) -> &str {
let b = t.as_bytes();
let (mut col, mut i) = (0, 0);
while i < b.len() && col < n && (b[i] == b' ' || b[i] == b'\t') {
if b[i] == b'\t' {
col += 4 - col % 4;
} else {
col += 1;
}
i += 1;
}
&t[i..]
}
fn backmatter_follows(lines: &[Line], li: usize) -> bool {
for l in &lines[li + 1..] {
if blank(&l.text) || RX.yaml_comment.is_match(&l.text) {
continue;
}
return RX.backmatter.is_match(&l.text);
}
false
}
fn math_end(lines: &[Line], li: usize) -> Option<u32> {
for l in &lines[li + 1..] {
if RX.math.is_match(&l.text) {
return Some(l.n);
}
if RX.close.is_match(&l.text) {
return None;
}
}
None
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stripped {
pub text: String,
pub still_open: bool,
pub opened: bool,
pub only: bool,
}
pub fn strip_comments(ln: &str, open: bool) -> Stripped {
let mut open = open;
let mut out = String::new();
let mut removed = false;
let mut opened = false;
let b = ln.as_bytes();
let mut i = 0;
while i < b.len() {
if open {
removed = true;
match ln[i..].find("-->") {
None => break,
Some(end) => {
open = false;
i += end + 3;
continue;
}
}
}
let c = b[i];
if c == b'\\' {
let end = (i + 2).min(b.len());
let end = if ln.is_char_boundary(end) { end } else { next_boundary(ln, end) };
out.push_str(&ln[i..end]);
i = end;
continue;
}
if c == b'`' {
let mut run = 0;
while i + run < b.len() && b[i + run] == b'`' {
run += 1;
}
let ticks = &ln[i..i + run];
if let Some(close) = ln[i + run..].find(ticks) {
let close = close + i + run;
if close + run >= b.len() || b[close + run] != b'`' {
out.push_str(&ln[i..close + run]);
i = close + run;
continue;
}
}
out.push_str(ticks);
i += run;
continue;
}
if ln[i..].starts_with("<!--") {
open = true;
opened = true;
removed = true;
i += 4;
continue;
}
let end = next_boundary(ln, i + 1);
out.push_str(&ln[i..end]);
i = end;
}
if removed && blank(&out) {
return Stripped { text: String::new(), still_open: open, opened, only: true };
}
Stripped { text: out, still_open: open, opened, only: false }
}
fn next_boundary(s: &str, mut i: usize) -> usize {
while i < s.len() && !s.is_char_boundary(i) {
i += 1;
}
i.min(s.len())
}
fn valid_region(s: &str) -> bool {
let parts: Vec<&str> = s.split(',').collect();
if parts.len() != 4 {
return false;
}
let mut v = [0.0f64; 4];
for (i, p) in parts.iter().enumerate() {
let p = js_trim(p);
if !RX.region_part.is_match(p) {
return false;
}
v[i] = p.parse().unwrap_or(0.0);
}
let [x, y, w, h] = v;
w > 0.0 && h > 0.0 && x + w <= 1.0 && y + h <= 1.0
}
pub fn value_from(p: &Parameter, text: &str) -> Value {
match p.param_type.as_str() {
"number" | "integer" => {
if let Some(n) = js_to_number(text)
&& !js_trim(text).is_empty()
&& !n.is_infinite()
{
return num(n);
}
}
"boolean" => match text {
"true" => return Value::Bool(true),
"false" => return Value::Bool(false),
_ => {}
},
"list" => {
if js_trim(text).is_empty() {
return Value::Array(Vec::new());
}
return Value::Array(text.split(',').map(|x| Value::String(js_trim(x).to_string())).collect());
}
_ => {}
}
Value::String(text.to_string())
}
fn js_to_number(text: &str) -> Option<f64> {
let s = js_trim(text);
if s.is_empty() {
return Some(0.0);
}
if s == "Infinity" || s == "+Infinity" {
return Some(f64::INFINITY);
}
if s == "-Infinity" {
return Some(f64::NEG_INFINITY);
}
if RX.decimal.is_match(s) {
return s.parse().ok();
}
let b = s.as_bytes();
if b.len() > 2 && b[0] == b'0' && b[2] != b'+' && b[2] != b'-' {
let base = match b[1] {
b'x' | b'X' => 16,
b'o' | b'O' => 8,
b'b' | b'B' => 2,
_ => return None,
};
let mut v = 0.0f64;
let mut exact: Option<u128> = Some(0);
for c in s[2..].chars() {
let d = c.to_digit(base)?;
exact = exact.and_then(|e| e.checked_mul(base as u128)).and_then(|e| e.checked_add(d as u128));
v = v * base as f64 + d as f64;
}
return Some(exact.map_or(v, |e| e as f64));
}
None
}
pub fn value_fits(p: &Parameter, v: &Value) -> bool {
match p.param_type.as_str() {
"string" => v.is_string(),
"number" => v.as_f64().is_some_and(f64::is_finite),
"integer" => v.as_f64().is_some_and(|f| f.is_finite() && f.fract() == 0.0),
"boolean" => v.is_boolean(),
"enum" => v.as_str().is_some_and(|s| p.values.as_ref().is_some_and(|vs| vs.iter().any(|x| x == s))),
"list" => string_list(v),
"url" => v.as_str().is_some_and(|s| RX.url.is_match(s)),
"duration" => v.as_str().is_some_and(|s| RX.duration.is_match(s)),
_ => false,
}
}
pub(crate) fn string_list(v: &Value) -> bool {
matches!(v, Value::Array(list) if list.iter().all(Value::is_string))
}
pub fn requires_of(r: &Reading) -> Vec<String> {
fn walk(nodes: &[Node], out: &mut std::collections::BTreeSet<String>) {
let set = |v: Option<&Value>| match v {
Some(Value::String(s)) => !s.trim().is_empty(),
Some(Value::Null) | None => false,
Some(_) => true,
};
for n in nodes {
if !n.is_directive() {
continue;
}
if let Some(k) = crate::registry::kind(&n.kind)
&& !k.family.is_empty()
{
out.insert(k.family.clone());
}
if n.kind == "agent" && !set(n.attrs.get("protocol")) && !set(n.attrs.get("src")) && !set(n.attrs.get("endpoint")) {
out.insert("agents".into());
}
if n.kind == "allowance" {
out.insert("spending".into());
}
walk(n.children(), out);
}
}
let mut out = std::collections::BTreeSet::new();
walk(&r.document.body, &mut out);
out.into_iter().collect()
}