Pretty print programs partially
This commit is contained in:
parent
1b364061e4
commit
200b653e61
3 changed files with 28 additions and 17 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
23
src/pretty/program.rs
Normal 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue