Skip to main content

Crate concrete_syntax_tree

Crate concrete_syntax_tree 

Source
Expand description

§concrete-syntax-tree

Crates.io Documentation CI License

A small, lossless concrete syntax tree for parser tooling.

The crate provides a generic tree container. Language-specific parsers define their own node-kind enum and implement CstKind. The tree can represent grammar productions, tokens, whitespace, comments, and other trivia without discarding the original token text.

§Example

use concrete_syntax_tree::{CstKind, CstNode};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Kind {
    Root,
    Word,
    Space,
}

impl CstKind for Kind {
    fn is_trivia(self) -> bool {
        matches!(self, Self::Space)
    }

    fn description(self) -> &'static str {
        match self {
            Self::Root => "root",
            Self::Word => "word",
            Self::Space => "space",
        }
    }
}

let tree = CstNode::branch(
    Kind::Root,
    vec![
        CstNode::leaf(Kind::Word, 0..5, "hello"),
        CstNode::leaf(Kind::Space, 5..6, " "),
        CstNode::leaf(Kind::Word, 6..11, "world"),
    ],
);

assert_eq!(tree.to_source(), "hello world");
assert_eq!(tree.span, 0..11);

§API

CstKind supplies language-specific trivia classification and diagnostic names. CstNode<K> stores a node kind, byte span, child nodes, and—on leaf nodes—the exact token text. CstNode::to_source concatenates descendant leaf text in order, making lossless round-tripping possible when the parser records all tokens and trivia.

This initial 0.1 API intentionally keeps the tree representation simple. Future versions may add a document-owned source buffer and span-based text access without changing the role of the crate as a generic CST foundation.

§Scope

This crate does not parse a language, define a grammar, recover from syntax errors, or provide an AST. It is the reusable tree representation for those layers.

§License

Licensed under either of:

at your option. A small, lossless concrete syntax tree for parser tooling.

Structs§

CstNode
A node in a concrete syntax tree.

Traits§

CstKind
Trait implemented by each language’s concrete-syntax-tree node kind.