diff --git a/Cargo.lock b/Cargo.lock index f954e52..d5cd11a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "atty" version = "0.2.14" @@ -146,6 +152,15 @@ version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "once_cell" version = "1.16.0" @@ -158,6 +173,18 @@ version = "6.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b5bf27447411e9ee3ff51186bf7a08e16c341efdde93f4d823e8844429bed7e" +[[package]] +name = "pretty" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83f3aa1e3ca87d3b124db7461265ac176b40c277f37e503eaa29c9c75c037846" +dependencies = [ + "arrayvec", + "log", + "typed-arena", + "unicode-segmentation", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -230,6 +257,7 @@ dependencies = [ "anyhow", "chumsky", "clap", + "pretty", ] [[package]] @@ -250,12 +278,24 @@ dependencies = [ "crunchy", ] +[[package]] +name = "typed-arena" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" + [[package]] name = "unicode-ident" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +[[package]] +name = "unicode-segmentation" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 43fe296..3a7b52e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" anyhow = "1.0.66" chumsky = "0.8.0" clap = { version = "4.0.26", features = ["derive", "deprecated"] } +pretty = "0.11.3" diff --git a/src/main.rs b/src/main.rs index aa1bdbb..57f1850 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ use std::fs; use std::path::PathBuf; +use ::pretty::{Pretty, RcAllocator}; use chumsky::Parser as _; use clap::Parser; mod ast; mod builtin; mod parser; +mod pretty; mod span; mod table; mod value; @@ -14,6 +16,7 @@ mod value; #[derive(Parser)] enum Command { Parse { file: PathBuf }, + Pretty { file: PathBuf }, } #[derive(Parser)] @@ -31,7 +34,27 @@ fn main() -> anyhow::Result<()> { let content = fs::read_to_string(&file)?; let stream = span::stream_from_str(&content); match parser::parser().parse(stream) { - Ok(lit) => println!("Successful parse: {lit:#?}"), + Ok(program) => println!("Successful parse: {program:#?}"), + Err(errs) => { + println!("Parsing failed"); + for err in errs { + println!("{err:?}"); + } + } + } + } + Command::Pretty { file } => { + let content = fs::read_to_string(&file)?; + 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}"); + } Err(errs) => { println!("Parsing failed"); for err in errs { diff --git a/src/pretty.rs b/src/pretty.rs new file mode 100644 index 0000000..1d8e288 --- /dev/null +++ b/src/pretty.rs @@ -0,0 +1,9 @@ +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") + } +}