concrete-syntax-tree 0.1.1

A small, lossless concrete syntax tree for parser tooling
Documentation
  • Coverage
  • 100%
    13 out of 13 items documented1 out of 9 items with examples
  • Size
  • Source code size: 27.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 104.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • legra-ai/concrete-syntax-tree
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jgeluk

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.