From 29cdcfbe0ca7fa2bbcca463275b3e80331ef83b7 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sat, 4 May 2024 21:56:12 +0200 Subject: [PATCH] lang: make output errors debuggable --- crates/lang/src/lst_parser/output.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/lang/src/lst_parser/output.rs b/crates/lang/src/lst_parser/output.rs index b530ac3..141d7d9 100644 --- a/crates/lang/src/lst_parser/output.rs +++ b/crates/lang/src/lst_parser/output.rs @@ -9,9 +9,12 @@ pub struct Output { pub green_node: GreenNode, pub errors: Vec, } + impl std::fmt::Debug for Output { 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>, f: &mut std::fmt::Formatter<'_>, lvl: i32, + errs: &mut Vec<&SyntaxError>, ) -> std::fmt::Result { for _ in 0..lvl { f.write_str(" ")?; @@ -26,9 +30,18 @@ fn debug_print_green_node( match node { NodeOrToken::Node(n) => { - writeln!(f, "{:?} {{", Lang::kind_from_raw(node.kind()))?; + let kind = Lang::kind_from_raw(node.kind()); + if kind != SyntaxKind::PARSE_ERR { + 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() { - debug_print_green_node(c, f, lvl + 1)?; + debug_print_green_node(c, f, lvl + 1, errs)?; } for _ in 0..lvl { f.write_str(" ")?;