Pretty print programs partially
This commit is contained in:
parent
969570fc4b
commit
b21619dabd
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::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:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
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