Skip to main content

leekscript_tooling/
tree_display.rs

1//! Utilities to display the parser's syntax tree in a readable ASCII/Unicode tree format.
2//!
3//! Delegates to sipha's grammar-agnostic [`format_syntax_tree`](sipha::tree_display::format_syntax_tree)
4//! with a kind-name callback for `LeekScript` syntax kinds.
5
6use sipha::red::SyntaxNode;
7use sipha::tree_display::format_syntax_tree as sipha_format_syntax_tree;
8
9use leekscript_core::syntax;
10
11/// Options for formatting the syntax tree (re-exported from sipha).
12pub use sipha::tree_display::TreeDisplayOptions;
13
14/// Format a syntax tree starting at `root` as a multi-line string.
15///
16/// Uses `LeekScript` kind names for node and token labels.
17#[must_use]
18pub fn format_syntax_tree(root: &SyntaxNode, options: &TreeDisplayOptions) -> String {
19    sipha_format_syntax_tree(root, options, |k| syntax::kind_name(k).to_string())
20}
21
22/// Print the syntax tree to stdout.
23pub fn print_syntax_tree(root: &SyntaxNode, options: &TreeDisplayOptions) {
24    println!("{}", format_syntax_tree(root, options));
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn options_default() {
33        let opts = TreeDisplayOptions::default();
34        assert!(opts.show_tokens);
35        assert!(!opts.show_trivia);
36    }
37
38    #[test]
39    fn options_structure_only() {
40        let opts = TreeDisplayOptions::structure_only();
41        assert!(!opts.show_tokens);
42    }
43
44    #[test]
45    fn options_full() {
46        let opts = TreeDisplayOptions::full();
47        assert!(opts.show_trivia);
48    }
49}