pub fn print_with_source<O, T, F>(
o: O,
tree: &Tree<T, F>,
source: &str,
) -> Result<(), Error>Expand description
Pretty-print a tree with the source spans printed.
This prints like print, except that the source reference of each
element without children is the text of source covered by its span. If
the span is out of range of source or does not fall on character
boundaries, + is printed instead.
Elements are told apart by whether they have children, so a node without children is printed exactly like a token.
ยง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"