337 lines
11 KiB
Rust
337 lines
11 KiB
Rust
use serde_json::Value;
|
|
|
|
use crate::{
|
|
alignment::{align_function, node_span, ControlKind, ControlLayout, ReadableControlTree},
|
|
diagnostics::Diagnostic,
|
|
input::SourceDocument,
|
|
model::{AnalysisFunction, MarkdownTree, NodeId},
|
|
};
|
|
|
|
pub fn check_document(document: &SourceDocument) -> Vec<Diagnostic> {
|
|
let Some(module) = &document.module else {
|
|
return Vec::new();
|
|
};
|
|
let mut diagnostics = Vec::new();
|
|
for function in &module.functions {
|
|
diagnostics.extend(check_function(&document.markdown, function));
|
|
}
|
|
diagnostics.sort_by(|left, right| {
|
|
(left.span.start, left.span.end, &left.id).cmp(&(
|
|
right.span.start,
|
|
right.span.end,
|
|
&right.id,
|
|
))
|
|
});
|
|
diagnostics
|
|
}
|
|
|
|
pub fn check_function(markdown: &MarkdownTree, analysis: &AnalysisFunction) -> Vec<Diagnostic> {
|
|
let tree = align_function(markdown, analysis);
|
|
if !tree.complete {
|
|
return Vec::new();
|
|
}
|
|
check_aligned(markdown, analysis, &tree)
|
|
}
|
|
|
|
pub(crate) fn check_aligned(
|
|
markdown: &MarkdownTree,
|
|
analysis: &AnalysisFunction,
|
|
tree: &ReadableControlTree,
|
|
) -> Vec<Diagnostic> {
|
|
let mut diagnostics = Vec::new();
|
|
for (index, control) in tree.controls.iter().enumerate() {
|
|
match control.kind {
|
|
ControlKind::Branch => {
|
|
branch_rules(markdown, analysis, tree, control, &mut diagnostics)
|
|
}
|
|
ControlKind::Loop => {
|
|
loop_rules(markdown, analysis, tree, control, &mut diagnostics)
|
|
}
|
|
}
|
|
nesting_rule(markdown, tree, index, &mut diagnostics);
|
|
end_rule(markdown, tree, control, &mut diagnostics);
|
|
}
|
|
diagnostics.sort_by(|left, right| {
|
|
(left.span.start, left.span.end, &left.id).cmp(&(
|
|
right.span.start,
|
|
right.span.end,
|
|
&right.id,
|
|
))
|
|
});
|
|
diagnostics
|
|
}
|
|
|
|
fn branch_rules(
|
|
markdown: &MarkdownTree,
|
|
analysis: &AnalysisFunction,
|
|
tree: &ReadableControlTree,
|
|
control: &ControlLayout,
|
|
diagnostics: &mut Vec<Diagnostic>,
|
|
) {
|
|
let opener_item = markdown.nearest(control.opener.node, "item");
|
|
let else_item = control
|
|
.else_carrier
|
|
.as_ref()
|
|
.and_then(|carrier| markdown.nearest(carrier.node, "item"));
|
|
if let (Some(item), Some(else_carrier), Some(other)) = (
|
|
opener_item,
|
|
control.else_carrier.as_ref(),
|
|
else_item,
|
|
) {
|
|
if item == other {
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-BRANCH-001", markdown.nodes[item].span)
|
|
.with_related(control.opener.span)
|
|
.with_related(else_carrier.span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
for carrier in std::iter::once(&control.opener).chain(control.else_carrier.iter()) {
|
|
let valid = markdown.nearest(carrier.node, "item").is_some_and(|item| {
|
|
markdown
|
|
.nearest(item, "list")
|
|
.is_some_and(|list| markdown.nodes[list].ordered == Some(true))
|
|
&& markdown.first_carrier_in_item(item).map(|first| first.node)
|
|
== Some(carrier.node)
|
|
});
|
|
if !valid {
|
|
let mut diagnostic = Diagnostic::new("MQD-BRANCH-002", carrier.span)
|
|
.with_function(&tree.function_name);
|
|
if let Some(item) = markdown.nearest(carrier.node, "item") {
|
|
diagnostic = diagnostic.with_related(markdown.nodes[item].span);
|
|
}
|
|
diagnostics.push(diagnostic);
|
|
}
|
|
}
|
|
|
|
if let Some(else_carrier) = &control.else_carrier {
|
|
if let (Some(if_item), Some(else_item)) = (opener_item, else_item) {
|
|
if !are_adjacent_ordered_siblings(markdown, if_item, else_item) {
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-BRANCH-003", control.opener.span)
|
|
.with_related(else_carrier.span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if !branch_is_nontrivial(analysis, control) {
|
|
return;
|
|
}
|
|
for carrier in std::iter::once(&control.opener).chain(control.else_carrier.iter()) {
|
|
let Some(item) = markdown.nearest(carrier.node, "item") else {
|
|
continue;
|
|
};
|
|
if !item_has_prose_after(markdown, control.function, item, carrier.span.end) {
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-BRANCH-004", carrier.span)
|
|
.with_related(markdown.nodes[item].span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn loop_rules(
|
|
markdown: &MarkdownTree,
|
|
analysis: &AnalysisFunction,
|
|
tree: &ReadableControlTree,
|
|
control: &ControlLayout,
|
|
diagnostics: &mut Vec<Diagnostic>,
|
|
) {
|
|
let item = markdown.nearest(control.opener.node, "item");
|
|
let valid = item.is_some_and(|item| {
|
|
markdown
|
|
.nearest(item, "list")
|
|
.is_some_and(|list| markdown.nodes[list].ordered == Some(false))
|
|
&& markdown.first_carrier_in_item(item).map(|first| first.node)
|
|
== Some(control.opener.node)
|
|
});
|
|
if !valid {
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-LOOP-001", control.opener.span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
}
|
|
let Some(item) = item else {
|
|
return;
|
|
};
|
|
if loop_contains_statements(analysis, control)
|
|
&& !item_has_prose_after(markdown, control.function, item, control.opener.span.end)
|
|
{
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-LOOP-002", control.opener.span)
|
|
.with_related(markdown.nodes[item].span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
}
|
|
}
|
|
|
|
fn nesting_rule(
|
|
markdown: &MarkdownTree,
|
|
tree: &ReadableControlTree,
|
|
index: usize,
|
|
diagnostics: &mut Vec<Diagnostic>,
|
|
) {
|
|
let control = &tree.controls[index];
|
|
let visual_parent = tree
|
|
.controls
|
|
.iter()
|
|
.enumerate()
|
|
.filter(|(candidate, parent)| {
|
|
*candidate != index
|
|
&& parent.end.as_ref().is_some_and(|end| {
|
|
control.opener.span.start > parent.opener.span.end
|
|
&& control.opener.span.start < end.span.start
|
|
})
|
|
&& visual_container(markdown, parent).is_some_and(|container| {
|
|
markdown.is_descendant(control.opener.node, container)
|
|
})
|
|
})
|
|
.max_by_key(|(_, parent)| parent.opener.span.start)
|
|
.map(|(candidate, _)| candidate);
|
|
if visual_parent != control.parent {
|
|
let mut diagnostic = Diagnostic::new("MQD-LOOP-003", control.opener.span)
|
|
.with_function(&tree.function_name);
|
|
if let Some(parent) = control.parent {
|
|
diagnostic = diagnostic.with_related(tree.controls[parent].opener.span);
|
|
}
|
|
diagnostics.push(diagnostic);
|
|
}
|
|
}
|
|
|
|
fn end_rule(
|
|
markdown: &MarkdownTree,
|
|
tree: &ReadableControlTree,
|
|
control: &ControlLayout,
|
|
diagnostics: &mut Vec<Diagnostic>,
|
|
) {
|
|
let Some(end) = &control.end else {
|
|
return;
|
|
};
|
|
let detached = match control.kind {
|
|
ControlKind::Branch => {
|
|
let opener_item = markdown.nearest(control.opener.node, "item");
|
|
let else_item = control
|
|
.else_carrier
|
|
.as_ref()
|
|
.and_then(|carrier| markdown.nearest(carrier.node, "item"));
|
|
if opener_item.is_some() && opener_item == else_item {
|
|
false
|
|
} else {
|
|
opener_item
|
|
.and_then(|item| markdown.nearest(item, "list"))
|
|
.is_some_and(|list| markdown.is_descendant(end.node, list))
|
|
}
|
|
}
|
|
ControlKind::Loop => markdown.nearest(control.opener.node, "item").is_some_and(|item| {
|
|
markdown.nearest(end.node, "item") != Some(item)
|
|
|| markdown
|
|
.item_carriers(item)
|
|
.into_iter()
|
|
.max_by_key(|carrier| carrier.span.start)
|
|
.map(|carrier| carrier.node)
|
|
!= Some(end.node)
|
|
}),
|
|
};
|
|
if detached {
|
|
diagnostics.push(
|
|
Diagnostic::new("MQD-CTRL-001", end.span)
|
|
.with_related(control.opener.span)
|
|
.with_function(&tree.function_name),
|
|
);
|
|
}
|
|
}
|
|
|
|
fn are_adjacent_ordered_siblings(
|
|
markdown: &MarkdownTree,
|
|
if_item: NodeId,
|
|
else_item: NodeId,
|
|
) -> bool {
|
|
let (Some(if_list), Some(else_list)) = (
|
|
markdown.nearest(if_item, "list"),
|
|
markdown.nearest(else_item, "list"),
|
|
) else {
|
|
return false;
|
|
};
|
|
if if_list != else_list || markdown.nodes[if_list].ordered != Some(true) {
|
|
return false;
|
|
}
|
|
let items: Vec<_> = markdown.nodes[if_list]
|
|
.children
|
|
.iter()
|
|
.copied()
|
|
.filter(|node| markdown.nodes[*node].kind == "item")
|
|
.collect();
|
|
let (Some(if_index), Some(else_index)) = (
|
|
items.iter().position(|node| *node == if_item),
|
|
items.iter().position(|node| *node == else_item),
|
|
) else {
|
|
return false;
|
|
};
|
|
else_index == if_index + 1
|
|
}
|
|
|
|
fn visual_container(markdown: &MarkdownTree, control: &ControlLayout) -> Option<NodeId> {
|
|
let item = markdown.nearest(control.opener.node, "item")?;
|
|
if control.kind == ControlKind::Branch {
|
|
if let Some(else_carrier) = &control.else_carrier {
|
|
let else_item = markdown.nearest(else_carrier.node, "item")?;
|
|
if are_adjacent_ordered_siblings(markdown, item, else_item) {
|
|
return markdown.nearest(item, "list");
|
|
}
|
|
}
|
|
}
|
|
Some(item)
|
|
}
|
|
|
|
fn item_has_prose_after(
|
|
markdown: &MarkdownTree,
|
|
function: NodeId,
|
|
item: NodeId,
|
|
start: usize,
|
|
) -> bool {
|
|
markdown.prose_fragments(function).into_iter().any(|fragment| {
|
|
fragment.span.start >= start
|
|
&& markdown.nearest(fragment.node, "item") == Some(item)
|
|
&& !fragment.text.trim().is_empty()
|
|
})
|
|
}
|
|
|
|
fn semantic_control<'a>(
|
|
analysis: &'a AnalysisFunction,
|
|
control: &ControlLayout,
|
|
) -> Option<&'a Value> {
|
|
let expected = match control.kind {
|
|
ControlKind::Branch => "branch",
|
|
ControlKind::Loop => "loop",
|
|
};
|
|
crate::model::semantic_nodes(&analysis.body).find(|node| {
|
|
node.get("kind").and_then(Value::as_str) == Some(expected)
|
|
&& node_span(node) == control.semantic_span
|
|
})
|
|
}
|
|
|
|
fn branch_is_nontrivial(analysis: &AnalysisFunction, control: &ControlLayout) -> bool {
|
|
semantic_control(analysis, control)
|
|
.and_then(|node| node.get("arms"))
|
|
.and_then(Value::as_array)
|
|
.is_some_and(|arms| {
|
|
arms.iter().any(|arm| {
|
|
arm.get("body")
|
|
.and_then(Value::as_array)
|
|
.is_some_and(|body| !body.is_empty())
|
|
})
|
|
})
|
|
}
|
|
|
|
fn loop_contains_statements(analysis: &AnalysisFunction, control: &ControlLayout) -> bool {
|
|
semantic_control(analysis, control)
|
|
.and_then(|node| node.get("body"))
|
|
.and_then(Value::as_array)
|
|
.is_some_and(|body| !body.is_empty())
|
|
} |