Pretty print programs partially

This commit is contained in:
Joscha 2022-11-20 20:32:14 +01:00
parent 1b364061e4
commit 200b653e61
3 changed files with 28 additions and 17 deletions

View file

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

View file

@ -1,9 +1 @@
use pretty::{DocAllocator, DocBuilder, Pretty}; mod program;
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")
}
}

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"),
}
}
}