syntree::print

Function print_with_source

Source
pub fn print_with_source<O, T, F>(
    o: O,
    tree: &Tree<T, F>,
    source: &str,
) -> Result<(), Error>
where O: Write, T: Copy + Debug, F: Flavor<Index: Display>,
Expand description

Pretty-print a tree with the source spans printed.

ยงExamples

#[derive(Debug, Clone, Copy)]
enum Syntax {
    NUMBER,
    WHITESPACE,
    OPERATOR,
    PLUS,
}

use Syntax::*;

let source = "128 + 64";

let tree = syntree::tree! {
    NUMBER => {
        (NUMBER, 3),
    },
    (WHITESPACE, 1),
    OPERATOR => {
        (PLUS, 1)
    },
    (WHITESPACE, 1),
    NUMBER => {
        (NUMBER, 2),
    },
};

let mut s = Vec::new();
syntree::print::print_with_source(&mut s, &tree, source)?;

This would write:

NUMBER@0..3
  NUMBER@0..3 "128"
WHITESPACE@3..4 " "
OPERATOR@4..5
  PLUS@4..5 "+"
WHITESPACE@5..6 " "
NUMBER@6..8
  NUMBER@6..8 "64"