Pretty print table constructors
This commit is contained in:
parent
412eaffc07
commit
fc139c31f4
3 changed files with 44 additions and 1 deletions
|
|
@ -2,5 +2,6 @@ mod basic;
|
|||
mod expr;
|
||||
mod lit;
|
||||
mod program;
|
||||
mod table_constr;
|
||||
|
||||
const NEST_DEPTH: isize = 4;
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr {
|
|||
Self::Call(call) => allocator.text("<call>"),
|
||||
Self::Field(field) => allocator.text("<field>"),
|
||||
Self::Var(var) => allocator.text("<var>"),
|
||||
Self::TableConstr(constr) => allocator.text("<constr>"),
|
||||
Self::TableConstr(constr) => constr.pretty(allocator),
|
||||
Self::TableDestr(destr) => allocator.text("<destr>"),
|
||||
Self::FuncDef(def) => allocator.text("<def>"),
|
||||
Self::Paren {
|
||||
|
|
|
|||
42
src/pretty/table_constr.rs
Normal file
42
src/pretty/table_constr.rs
Normal file
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue