lang: make output errors debuggable

This commit is contained in:
Schrottkatze 2024-05-04 21:56:12 +02:00
parent afd493be16
commit 29cdcfbe0c
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc

View file

@ -9,9 +9,12 @@ pub struct Output {
pub green_node: GreenNode, pub green_node: GreenNode,
pub errors: Vec<SyntaxError>, pub errors: Vec<SyntaxError>,
} }
impl std::fmt::Debug for Output { impl std::fmt::Debug for Output {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug_print_green_node(NodeOrToken::Node(&self.green_node), f, 0) let mut errs: Vec<&SyntaxError> = self.errors.iter().collect();
errs.reverse();
debug_print_green_node(NodeOrToken::Node(&self.green_node), f, 0, &mut errs)
} }
} }
@ -19,6 +22,7 @@ fn debug_print_green_node(
node: NodeOrToken<&GreenNodeData, &GreenTokenData>, node: NodeOrToken<&GreenNodeData, &GreenTokenData>,
f: &mut std::fmt::Formatter<'_>, f: &mut std::fmt::Formatter<'_>,
lvl: i32, lvl: i32,
errs: &mut Vec<&SyntaxError>,
) -> std::fmt::Result { ) -> std::fmt::Result {
for _ in 0..lvl { for _ in 0..lvl {
f.write_str(" ")?; f.write_str(" ")?;
@ -26,9 +30,18 @@ fn debug_print_green_node(
match node { match node {
NodeOrToken::Node(n) => { NodeOrToken::Node(n) => {
let kind = Lang::kind_from_raw(node.kind());
if kind != SyntaxKind::PARSE_ERR {
writeln!(f, "{:?} {{", Lang::kind_from_raw(node.kind()))?; writeln!(f, "{:?} {{", Lang::kind_from_raw(node.kind()))?;
} else {
let err = errs
.pop()
.expect("all error syntax nodes should correspond to an error");
writeln!(f, "{:?}: {err:?} {{", kind)?;
}
for c in n.children() { for c in n.children() {
debug_print_green_node(c, f, lvl + 1)?; debug_print_green_node(c, f, lvl + 1, errs)?;
} }
for _ in 0..lvl { for _ in 0..lvl {
f.write_str(" ")?; f.write_str(" ")?;