Pretty print field accesses and assignments

This commit is contained in:
Joscha 2022-11-20 23:50:53 +01:00
parent 81e2a28b06
commit 5bd43ee37a
3 changed files with 66 additions and 1 deletions

View file

@ -1,6 +1,7 @@
mod basic; mod basic;
mod call; mod call;
mod expr; mod expr;
mod field;
mod lit; mod lit;
mod program; mod program;
mod table_constr; mod table_constr;

View file

@ -27,7 +27,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr {
match self { match self {
Self::Lit(lit) => lit.pretty(allocator), Self::Lit(lit) => lit.pretty(allocator),
Self::Call(call) => call.pretty(allocator), Self::Call(call) => call.pretty(allocator),
Self::Field(field) => allocator.text("<field>"), Self::Field(field) => field.pretty(allocator),
Self::Var(var) => allocator.text("<var>"), Self::Var(var) => allocator.text("<var>"),
Self::TableConstr(constr) => constr.pretty(allocator), Self::TableConstr(constr) => constr.pretty(allocator),
Self::TableDestr(destr) => allocator.text("<destr>"), Self::TableDestr(destr) => allocator.text("<destr>"),

64
src/pretty/field.rs Normal file
View file

@ -0,0 +1,64 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::Field;
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Field {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
match self {
Self::Access {
expr,
s0,
s1,
index,
s2,
span: _,
} => expr
.pretty(allocator)
.append(index.pretty(allocator).brackets()),
Self::Assign {
expr,
s0,
s1,
index,
s2,
s3,
s4,
value,
span: _,
} => expr
.pretty(allocator)
.append(index.pretty(allocator).brackets())
.append(allocator.text(" = "))
.append(value.pretty(allocator)),
Self::AccessIdent {
expr,
s0,
s1,
ident,
span: _,
} => expr
.pretty(allocator)
.append(allocator.line_())
.append(allocator.text("."))
.append(ident.pretty(allocator))
.group(),
Self::AssignIdent {
expr,
s0,
s1,
ident,
s2,
s3,
value,
span: _,
} => expr
.pretty(allocator)
.append(allocator.line_())
.append(allocator.text("."))
.append(ident.pretty(allocator))
.append(allocator.text(" = "))
.append(value.pretty(allocator))
.group(),
}
}
}