Pretty print programs partially

This commit is contained in:
Joscha 2022-11-20 20:32:14 +01:00
parent 969570fc4b
commit b21619dabd
3 changed files with 28 additions and 17 deletions

View file

@ -1,7 +1,6 @@
use std::fs;
use std::path::PathBuf;
use ::pretty::{Pretty, RcAllocator};
use chumsky::Parser as _;
use clap::Parser;
@ -48,17 +47,14 @@ fn main() -> anyhow::Result<()> {
let stream = span::stream_from_str(&content);
match parser::parser().parse(stream) {
Ok(program) => {
println!("Successful parse");
let doc = program.pretty(&RcAllocator);
let mut out = vec![];
doc.render(100, &mut out)?;
let str = String::from_utf8(out)?;
println!("{str}");
program.to_doc().render(100, &mut out)?;
println!("{}", String::from_utf8(out)?);
}
Err(errs) => {
println!("Parsing failed");
eprintln!("Parsing failed");
for err in errs {
println!("{err:?}");
eprintln!("{err:?}");
}
}
}

View file

@ -1,9 +1 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::Program;
impl<'a, A: DocAllocator<'a>> Pretty<'a, A> for Program {
fn pretty(self, allocator: &'a A) -> DocBuilder<'a, A, ()> {
allocator.text("Hello world")
}
}
mod program;

23
src/pretty/program.rs Normal file
View file

@ -0,0 +1,23 @@
use pretty::RcDoc;
use crate::ast::Program;
impl Program {
pub fn to_doc(&self) -> RcDoc {
match self {
Program::Expr {
s0,
expr,
s1,
span: _,
} => RcDoc::nil(),
Program::Module {
s0,
s1,
elems,
s2,
span: _,
} => RcDoc::text("module"),
}
}
}