pub enum Element<K> {
Node(Node<K>),
Token(Token<K>),
}Expand description
One child of a Node: either a nested node or a leaf token.
A concrete syntax tree alternates between the two — a node groups a run of
children under a kind, and a Token is a leaf carrying a classified span of
source. Trivia (whitespace, comments) is not special: it rides as an ordinary
leaf token, which is what makes the tree lossless.
§Examples
use syntax_lang::{Element, Node, Span, Token};
let leaf: Element<&str> = Element::Token(Token::new("ident", Span::new(0, 3)));
assert!(leaf.is_token());
assert_eq!(leaf.kind(), &"ident");
assert_eq!(leaf.span(), Span::new(0, 3));
let group = Element::Node(Node::new("expr", vec![leaf]));
assert!(group.is_node());
assert_eq!(group.span(), Span::new(0, 3));Variants§
Implementations§
Source§impl<K> Element<K>
impl<K> Element<K>
Sourcepub fn span(&self) -> Span
pub fn span(&self) -> Span
The span of source this child covers — the node’s covering span or the token’s own span.
§Examples
use syntax_lang::{Element, Span, Token};
let e = Element::Token(Token::new('x', Span::new(4, 5)));
assert_eq!(e.span(), Span::new(4, 5));Sourcepub fn kind(&self) -> &K
pub fn kind(&self) -> &K
Borrows the kind of this child — the node’s kind or the token’s kind.
Node kinds and token kinds share one type K (the rowan model), so a
caller can read a child’s kind without first knowing whether it is a node
or a leaf.
§Examples
use syntax_lang::{Element, Span, Token};
let e = Element::Token(Token::new("plus", Span::new(1, 2)));
assert_eq!(e.kind(), &"plus");Sourcepub fn as_node(&self) -> Option<&Node<K>>
pub fn as_node(&self) -> Option<&Node<K>>
Returns the nested node if this child is one, otherwise None.
§Examples
use syntax_lang::{Element, Node, Span, Token};
let node = Element::Node(Node::new("n", vec![Element::Token(Token::new("t", Span::new(0, 1)))]));
assert!(node.as_node().is_some());
assert!(node.as_token().is_none());Sourcepub fn as_token(&self) -> Option<&Token<K>>
pub fn as_token(&self) -> Option<&Token<K>>
Returns the leaf token if this child is one, otherwise None.
§Examples
use syntax_lang::{Element, Span, Token};
let e = Element::Token(Token::new("t", Span::new(0, 1)));
assert_eq!(e.as_token().map(|t| *t.kind()), Some("t"));