From fc139c31f483bcd09b36ec3e28eb0dc93042e4db Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 20 Nov 2022 23:41:39 +0100 Subject: [PATCH] Pretty print table constructors --- src/pretty.rs | 1 + src/pretty/expr.rs | 2 +- src/pretty/table_constr.rs | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/pretty/table_constr.rs diff --git a/src/pretty.rs b/src/pretty.rs index c375167..3a21e41 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -2,5 +2,6 @@ mod basic; mod expr; mod lit; mod program; +mod table_constr; const NEST_DEPTH: isize = 4; diff --git a/src/pretty/expr.rs b/src/pretty/expr.rs index d402480..f366edd 100644 --- a/src/pretty/expr.rs +++ b/src/pretty/expr.rs @@ -29,7 +29,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr { Self::Call(call) => allocator.text(""), Self::Field(field) => allocator.text(""), Self::Var(var) => allocator.text(""), - Self::TableConstr(constr) => allocator.text(""), + Self::TableConstr(constr) => constr.pretty(allocator), Self::TableDestr(destr) => allocator.text(""), Self::FuncDef(def) => allocator.text(""), Self::Paren { diff --git a/src/pretty/table_constr.rs b/src/pretty/table_constr.rs new file mode 100644 index 0000000..288bf84 --- /dev/null +++ b/src/pretty/table_constr.rs @@ -0,0 +1,42 @@ +use pretty::{DocAllocator, DocBuilder, Pretty}; + +use crate::ast::{TableConstr, TableConstrElem}; + +use super::NEST_DEPTH; + +impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TableConstrElem { + fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> { + match self { + Self::Lit(lit) => lit.pretty(allocator), + Self::Indexed { + s0, + index, + s1, + s2, + s3, + value, + span: _, + } => index + .pretty(allocator) + .brackets() + .append(allocator.text(": ")) + .append(value.pretty(allocator)), + } + } +} + +impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TableConstr { + fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> { + self.elems + .pretty( + allocator, + |a, e| a.line().append(e.pretty(a)), + |a, (s0, s1)| a.text(","), + |a, s| a.text(","), + ) + .nest(NEST_DEPTH) + .append(allocator.line()) + .braces() + .group() + } +}