Pretty print function definitions
This commit is contained in:
parent
c45a45f0b6
commit
b3eaa40902
3 changed files with 100 additions and 1 deletions
|
|
@ -2,6 +2,7 @@ mod basic;
|
||||||
mod call;
|
mod call;
|
||||||
mod expr;
|
mod expr;
|
||||||
mod field;
|
mod field;
|
||||||
|
mod func_def;
|
||||||
mod lit;
|
mod lit;
|
||||||
mod program;
|
mod program;
|
||||||
mod table_constr;
|
mod table_constr;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr {
|
||||||
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) => destr.pretty(allocator),
|
Self::TableDestr(destr) => destr.pretty(allocator),
|
||||||
Self::FuncDef(def) => allocator.text("<def>"),
|
Self::FuncDef(def) => def.pretty(allocator),
|
||||||
Self::Paren {
|
Self::Paren {
|
||||||
s0,
|
s0,
|
||||||
inner,
|
inner,
|
||||||
|
|
|
||||||
98
src/pretty/func_def.rs
Normal file
98
src/pretty/func_def.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
use pretty::{DocAllocator, DocBuilder, Pretty};
|
||||||
|
|
||||||
|
use crate::ast::FuncDef;
|
||||||
|
|
||||||
|
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for FuncDef {
|
||||||
|
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
|
||||||
|
match self {
|
||||||
|
Self::AnonNoArg {
|
||||||
|
s0,
|
||||||
|
s1,
|
||||||
|
s2,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => allocator.text("function() ").append(body.pretty(allocator)),
|
||||||
|
|
||||||
|
Self::AnonArg {
|
||||||
|
s0,
|
||||||
|
s1,
|
||||||
|
arg,
|
||||||
|
s2,
|
||||||
|
s3,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => allocator
|
||||||
|
.text("function")
|
||||||
|
.append(arg.pretty(allocator).parens())
|
||||||
|
.append(allocator.space())
|
||||||
|
.append(body.pretty(allocator)),
|
||||||
|
|
||||||
|
Self::AnonDestr {
|
||||||
|
s0,
|
||||||
|
pattern,
|
||||||
|
s1,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => allocator
|
||||||
|
.text("function")
|
||||||
|
.append(pattern.pretty(allocator))
|
||||||
|
.append(allocator.space())
|
||||||
|
.append(body.pretty(allocator)),
|
||||||
|
|
||||||
|
Self::NamedNoArg {
|
||||||
|
local,
|
||||||
|
s0,
|
||||||
|
name,
|
||||||
|
s1,
|
||||||
|
s2,
|
||||||
|
s3,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => local
|
||||||
|
.map(|s| allocator.text("local "))
|
||||||
|
.unwrap_or_else(|| allocator.nil())
|
||||||
|
.append(allocator.text("function "))
|
||||||
|
.append(name)
|
||||||
|
.append(allocator.text("() "))
|
||||||
|
.append(body.pretty(allocator)),
|
||||||
|
|
||||||
|
Self::NamedArg {
|
||||||
|
local,
|
||||||
|
s0,
|
||||||
|
name,
|
||||||
|
s1,
|
||||||
|
s2,
|
||||||
|
arg,
|
||||||
|
s3,
|
||||||
|
s4,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => local
|
||||||
|
.map(|s| allocator.text("local "))
|
||||||
|
.unwrap_or_else(|| allocator.nil())
|
||||||
|
.append(allocator.text("function "))
|
||||||
|
.append(name)
|
||||||
|
.append(arg.pretty(allocator).parens())
|
||||||
|
.append(allocator.space())
|
||||||
|
.append(body.pretty(allocator)),
|
||||||
|
|
||||||
|
Self::NamedDestr {
|
||||||
|
local,
|
||||||
|
s0,
|
||||||
|
name,
|
||||||
|
s1,
|
||||||
|
pattern,
|
||||||
|
s2,
|
||||||
|
body,
|
||||||
|
span: _,
|
||||||
|
} => local
|
||||||
|
.map(|s| allocator.text("local "))
|
||||||
|
.unwrap_or_else(|| allocator.nil())
|
||||||
|
.append(allocator.text("function "))
|
||||||
|
.append(name)
|
||||||
|
.append(pattern.pretty(allocator))
|
||||||
|
.append(allocator.space())
|
||||||
|
.append(body.pretty(allocator)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue