Skip to main content

Element

Enum Element 

Source
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§

§

Node(Node<K>)

A nested interior node.

§

Token(Token<K>)

A leaf token: a classified span of source.

Implementations§

Source§

impl<K> Element<K>

Source

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));
Source

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");
Source

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());
Source

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"));
Source

pub fn is_node(&self) -> bool

Whether this child is a nested node.

§Examples
use syntax_lang::{Element, Span, Token};

assert!(!Element::Token(Token::new(0u8, Span::new(0, 1))).is_node());
Source

pub fn is_token(&self) -> bool

Whether this child is a leaf token.

§Examples
use syntax_lang::{Element, Span, Token};

assert!(Element::Token(Token::new(0u8, Span::new(0, 1))).is_token());

Trait Implementations§

Source§

impl<K: Clone> Clone for Element<K>

Source§

fn clone(&self) -> Element<K>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug> Debug for Element<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Eq> Eq for Element<K>

Source§

impl<K: PartialEq> PartialEq for Element<K>

Source§

fn eq(&self, other: &Element<K>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: PartialEq> StructuralPartialEq for Element<K>

Auto Trait Implementations§

§

impl<K> Freeze for Element<K>
where K: Freeze,

§

impl<K> RefUnwindSafe for Element<K>
where K: RefUnwindSafe,

§

impl<K> Send for Element<K>
where K: Send,

§

impl<K> Sync for Element<K>
where K: Sync,

§

impl<K> Unpin for Element<K>
where K: Unpin,

§

impl<K> UnsafeUnpin for Element<K>
where K: UnsafeUnpin,

§

impl<K> UnwindSafe for Element<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.