diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-06 01:32:59 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-06 01:32:59 +0100 |
| commit | 7b4d4d6002a9c3da8fafd912f3c7b2da617f19c0 (patch) | |
| tree | e491f5fcf33c1032c63746003ac7bef6c3c5478f /src/syntax/mod.rs | |
| parent | 2e77b1c836220766398e379ae0157736fb448874 (diff) | |
Pretty printing 🦋
- Syntax tree and value pretty printing
- Better value evaluation (top-level strings and content are evaluated plainly, everything else is pretty printed)
Diffstat (limited to 'src/syntax/mod.rs')
| -rw-r--r-- | src/syntax/mod.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 9c78fbc3..22f51d82 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -12,5 +12,33 @@ pub use node::*; pub use span::*; pub use token::*; -/// A collection of nodes which form a tree together with their children. +use crate::pretty::{Pretty, Printer}; + +/// The abstract syntax tree. pub type Tree = SpanVec<Node>; + +impl Pretty for Tree { + fn pretty(&self, p: &mut Printer) { + for node in self { + node.v.pretty(p); + } + } +} + +#[cfg(test)] +mod tests { + use crate::parse::parse; + use crate::pretty::pretty; + + #[track_caller] + pub fn test_pretty(src: &str, exp: &str) { + let tree = parse(src).output; + let found = pretty(&tree); + if exp != found { + println!("tree: {:#?}", tree); + println!("expected: {}", exp); + println!("found: {}", found); + panic!("test failed"); + } + } +} |
