From caeb75337a661fe5f712f9e62d9ec5460e5f1b93 Mon Sep 17 00:00:00 2001 From: "Guillem L. Jara" <4lon3ly0@tutanota.com> Date: Mon, 18 May 2026 10:12:51 +0200 Subject: [PATCH] chore(parser): refactor debug output --- parser/src/ast.rs | 39 --------------------------------------- parser/src/sexpr.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index a58bc83..29eb9c1 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -543,42 +543,3 @@ impl<'a> From>> for Body<'a> { Self(value) } } - -impl Debug for Expr<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Leaf(atom) => write!(f, "{atom:?}"), - Self::Node(expr) => match expr.as_ref() { - ExprNode::FunctionCall(ident, args) => { - write!(f, "({ident:?}")?; - for arg in args { - write!(f, " {arg:?}")?; - } - write!(f, ")") - } - ExprNode::IndirectCall(ident, args) => { - write!(f, "(@{ident:?}")?; - for arg in args { - write!(f, " {arg:?}")?; - } - write!(f, ")") - } - ExprNode::UnaryOperation(op, a) => write!(f, "({op:?} {a:?})"), - ExprNode::BinaryOperation(op, a, b) => write!(f, "({op:?} {a:?} {b:?})"), - ExprNode::BinaryPlaceOperation(op, a, b) => write!(f, "({op:?} {a:?} {b:?})"), - ExprNode::UnaryPlaceOperation(op, a) => write!(f, "({op:?} {a:?})"), - ExprNode::Ternary(a, b, c) => write!(f, "(?: {a:?} {b:?} {c:?})"), - ExprNode::Getline(getline) => match getline { - Getline::FromInput(Some(a)) => write!(f, "(getline {a:?})"), - Getline::FromInput(None) => write!(f, "(getline)"), - Getline::FromFile(Some(a), b) => write!(f, "(getline< {b:?} {a:?})"), - Getline::FromFile(None, b) => write!(f, "(getline< {b:?})"), - Getline::PipeOut(Some(a), b) => write!(f, "(getline| {b:?} {a:?})"), - Getline::PipeOut(None, b) => write!(f, "(getline| {b:?})"), - Getline::CoprocessOut(Some(a), b) => write!(f, "(getline|& {b:?} {a:?})"), - Getline::CoprocessOut(None, b) => write!(f, "(getline|& {b:?})"), - }, - }, - } - } -} diff --git a/parser/src/sexpr.rs b/parser/src/sexpr.rs index eaefc88..7aa8bfa 100644 --- a/parser/src/sexpr.rs +++ b/parser/src/sexpr.rs @@ -6,7 +6,8 @@ use std::fmt::{Debug, Display, Formatter, Result}; use crate::ast::{ - Atom, Body, Identifier, Place, Redirection, RulePattern, SimpleStatement, Statement, Variable, + Atom, Body, Expr, ExprNode, Getline, Identifier, Place, Redirection, RulePattern, + SimpleStatement, Statement, Variable, }; const PRETTY_PRINT_INDENT: usize = 2; @@ -187,6 +188,45 @@ impl Debug for SimpleStatement<'_> { } } +impl Debug for Expr<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + match self { + Self::Leaf(atom) => write!(f, "{atom:?}"), + Self::Node(expr) => match expr.as_ref() { + ExprNode::FunctionCall(ident, args) => { + write!(f, "({ident:?}")?; + for arg in args { + write!(f, " {arg:?}")?; + } + write!(f, ")") + } + ExprNode::IndirectCall(ident, args) => { + write!(f, "(@{ident:?}")?; + for arg in args { + write!(f, " {arg:?}")?; + } + write!(f, ")") + } + ExprNode::UnaryOperation(op, a) => write!(f, "({op:?} {a:?})"), + ExprNode::BinaryOperation(op, a, b) => write!(f, "({op:?} {a:?} {b:?})"), + ExprNode::BinaryPlaceOperation(op, a, b) => write!(f, "({op:?} {a:?} {b:?})"), + ExprNode::UnaryPlaceOperation(op, a) => write!(f, "({op:?} {a:?})"), + ExprNode::Ternary(a, b, c) => write!(f, "(?: {a:?} {b:?} {c:?})"), + ExprNode::Getline(getline) => match getline { + Getline::FromInput(Some(a)) => write!(f, "(getline {a:?})"), + Getline::FromInput(None) => write!(f, "(getline)"), + Getline::FromFile(Some(a), b) => write!(f, "(getline< {b:?} {a:?})"), + Getline::FromFile(None, b) => write!(f, "(getline< {b:?})"), + Getline::PipeOut(Some(a), b) => write!(f, "(getline| {b:?} {a:?})"), + Getline::PipeOut(None, b) => write!(f, "(getline| {b:?})"), + Getline::CoprocessOut(Some(a), b) => write!(f, "(getline|& {b:?} {a:?})"), + Getline::CoprocessOut(None, b) => write!(f, "(getline|& {b:?})"), + }, + }, + } + } +} + struct ListLispFmt<'a, T: Debug>(&'a [T]); impl Debug for ListLispFmt<'_, T> { fn fmt(&self, f: &mut Formatter<'_>) -> Result {