pub trait Syntax: Sized + Copy + Debug + Eq {
// Required methods
fn from_raw(raw: RawSyntaxKind) -> Self;
fn into_raw(self) -> RawSyntaxKind;
fn static_text(self) -> Option<&'static str>;
}
Expand description
A type that represents what items in your language can be.
Typically, this is an enum
with variants such as Identifier
, Literal
, …
The Syntax
trait is the bridge between the internal cstree
representation and your
language’s types.
This is essential for providing a SyntaxNode
API that can be used with your types, as in the
s_expressions
example:
#[derive(Debug, Clone, Copy, PartialEq, Eq, cstree::Syntax)]
#[repr(u32)]
enum SyntaxKind {
#[static_text("+")]
Plus, // `+`
#[static_text("-")]
Minus, // `-`
Integer, // like `15`
Expression, // combined expression, like `5 + 4 - 3`
Whitespace, // whitespace is explicit
}
cstree
provides a procedural macro called cstree_derive
to automatically generate Syntax
implementations for
syntax kind enums if its derive
feature is enabled.
Required Methods§
sourcefn from_raw(raw: RawSyntaxKind) -> Self
fn from_raw(raw: RawSyntaxKind) -> Self
Construct a semantic item kind from the compact representation.
sourcefn into_raw(self) -> RawSyntaxKind
fn into_raw(self) -> RawSyntaxKind
Convert a semantic item kind into a more compact representation.
sourcefn static_text(self) -> Option<&'static str>
fn static_text(self) -> Option<&'static str>
Fixed text for a particular syntax kind.
Implement for kinds that will only ever represent the same text, such as punctuation (like a
semicolon), keywords (like fn
), or operators (like <=
).
Indicating tokens that have a static_text
this way allows cstree
to store them more efficiently, which makes
it faster to add them to a syntax tree and to look up their text. Since there can often be many occurrences
of these tokens inside a file, doing so will improve the performance of using cstree
.