endOfStream function
What is wrong with document once a stream has ended: every child,
slot or root id that still names no node (dangling_child, an error)
and every node no root reaches (unreachable_node, a warning).
Implementation
List<EditDiagnostic> endOfStream(LayoutDocument document) {
final out = <EditDiagnostic>[];
// A script written against names it does not have fails only when it
// runs: said now, so the agent's next turn can fix it.
for (final p in scriptSourceProblems(document.extra['scripts'])) {
out.add(EditDiagnostic(op: -1, code: 'unknown_name', error: true, path: '/scripts/${p.index}/source', message: '${p.message} Did you mean "${p.suggestion}"?'));
}
for (final n in document.nodes.values) {
var i = 0;
for (final c in n.children) {
if (!document.nodes.containsKey(c)) {
out.add(EditDiagnostic.of(EditCodes.danglingChild, -1, 'Child "$c" of "${n.id}" never arrived: send its set op, or remove it from the children.',
path: editPointer(editPointer(editPointer('/nodes', n.id), 'children'), i), nodeId: n.id));
}
i++;
}
for (final e in n.slots.entries) {
if (!document.nodes.containsKey(e.value)) {
out.add(EditDiagnostic.of(EditCodes.danglingChild, -1, 'Slot "${e.key}" of "${n.id}" names "${e.value}", which never arrived.',
path: editPointer(editPointer(editPointer('/nodes', n.id), 'slots'), e.key), nodeId: n.id));
}
}
}
for (var i = 0; i < document.roots.length; i++) {
if (!document.nodes.containsKey(document.roots[i])) {
out.add(EditDiagnostic.of(EditCodes.danglingChild, -1, 'Root "${document.roots[i]}" never arrived.', path: editPointer('/roots', i)));
}
}
final reached = <String>{};
void visit(String id) {
if (!reached.add(id)) return;
for (final c in document.nodes[id]?.childIds ?? const <String>[]) {
visit(c);
}
}
for (final r in document.roots) {
visit(r);
}
for (final id in document.nodes.keys) {
if (reached.contains(id) || document.parentOf(id) != null) continue;
out.add(EditDiagnostic.of(EditCodes.unreachableNode, -1, 'Node "$id" is under no root: nothing lists it as a child or slot, so it is not shown.',
path: editPointer('/nodes', id), nodeId: id));
}
return out;
}