Pretty print table destructors

This commit is contained in:
Joscha 2022-11-21 00:07:57 +01:00
parent c7fc8584ff
commit e7416fbc1e
3 changed files with 53 additions and 1 deletions

View file

@ -5,6 +5,7 @@ mod field;
mod lit; mod lit;
mod program; mod program;
mod table_constr; mod table_constr;
mod table_destr;
mod var; mod var;
const NEST_DEPTH: isize = 4; const NEST_DEPTH: isize = 4;

View file

@ -30,7 +30,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr {
Self::Field(field) => field.pretty(allocator), Self::Field(field) => field.pretty(allocator),
Self::Var(var) => var.pretty(allocator), Self::Var(var) => var.pretty(allocator),
Self::TableConstr(constr) => constr.pretty(allocator), Self::TableConstr(constr) => constr.pretty(allocator),
Self::TableDestr(destr) => allocator.text("<destr>"), Self::TableDestr(destr) => destr.pretty(allocator),
Self::FuncDef(def) => allocator.text("<def>"), Self::FuncDef(def) => allocator.text("<def>"),
Self::Paren { Self::Paren {
s0, s0,

51
src/pretty/table_destr.rs Normal file
View file

@ -0,0 +1,51 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{TableDestr, TablePattern, TablePatternElem};
use super::NEST_DEPTH;
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePatternElem {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
match self {
Self::Positional(ident) => ident.pretty(allocator),
Self::Named {
name,
s0,
s1,
ident,
span: _,
} => name
.pretty(allocator)
.append(allocator.text(": "))
.append(ident.pretty(allocator)),
}
}
}
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePattern {
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()
}
}
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TableDestr {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
// TODO Handle spaces
self.local
.map(|s| allocator.text("local "))
.unwrap_or_else(|| allocator.nil())
.append(self.pattern.pretty(allocator))
.append(allocator.text(" = "))
.append(self.value.pretty(allocator))
}
}