Skip to main content

Builder

Struct Builder 

Source
pub struct Builder<K> { /* private fields */ }
Expand description

Assembles a Node tree from a stream of start_node / token / finish_node calls — the shape a parser drives as it recognises the grammar.

The builder keeps a stack of open nodes. start_node opens one, token appends a leaf to whichever node is innermost, and finish_node closes the innermost node and folds it into its parent. When the outermost node closes it becomes the root, and finish hands it back.

Misuse is never a panic. The first structural fault — an unbalanced close, a token at the root, a second root — is remembered and returned from finish as a BuildError, so a parser can drive the builder on its hot path without wrapping every call in a Result.

§Examples

Build (1) — a parenthesised literal — and read it back losslessly:

use syntax_lang::{Builder, Span, Token};

let mut b = Builder::new();
b.start_node("paren");
b.token(Token::new("(", Span::new(0, 1)));
b.start_node("lit");
b.token(Token::new("num", Span::new(1, 2)));
b.finish_node(); // close "lit"
b.token(Token::new(")", Span::new(2, 3)));
b.finish_node(); // close "paren"

let root = b.finish().expect("balanced");
assert_eq!(root.kind(), &"paren");
assert_eq!(root.span(), Span::new(0, 3));
assert_eq!(root.text("(1)"), Some("(1)"));
let kinds: Vec<_> = root.tokens().map(|t| *t.kind()).collect();
assert_eq!(kinds, ["(", "num", ")"]);

Implementations§

Source§

impl<K> Builder<K>

Source

pub const fn new() -> Self

Creates an empty builder.

§Examples
use syntax_lang::Builder;

let b = Builder::<&str>::new();
assert!(b.is_empty());
Source

pub fn is_empty(&self) -> bool

Whether nothing has been built yet — no open nodes and no completed root.

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

let mut b = Builder::new();
assert!(b.is_empty());
b.start_node("root");
assert!(!b.is_empty());
b.token(Token::new("t", Span::new(0, 1)));
b.finish_node();
let _ = b.finish();
Source

pub fn start_node(&mut self, kind: K)

Opens a new node of kind. Tokens and child nodes added until the matching finish_node become its children.

Starting a second node once the root has already closed records BuildError::MultipleRoots; the call is otherwise infallible.

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

let mut b = Builder::new();
b.start_node("root");
b.token(Token::new("t", Span::new(0, 1)));
b.finish_node();
assert!(b.finish().is_ok());
Source

pub fn token(&mut self, token: Token<K>)

Appends token as a leaf of the innermost open node.

Pushing a token with no node open records BuildError::TokenOutsideNode, since a tree’s root must be a node rather than a bare token.

§Examples
use syntax_lang::{BuildError, Builder, Span, Token};

let mut b = Builder::new();
b.token(Token::new("t", Span::new(0, 1))); // no node open yet
assert_eq!(b.finish(), Err(BuildError::TokenOutsideNode));
Source

pub fn finish_node(&mut self)

Closes the innermost open node, folding it into its parent — or promoting it to the root when it is the outermost node.

Closing with no node open records BuildError::UnbalancedFinish.

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

let mut b = Builder::new();
b.start_node("outer");
b.start_node("inner");
b.token(Token::new("t", Span::new(0, 1)));
b.finish_node(); // close "inner"
b.finish_node(); // close "outer"
let root = b.finish().expect("balanced");
assert_eq!(root.child_nodes().count(), 1);
Source

pub fn finish(self) -> Result<Node<K>, BuildError>

Consumes the builder, returning the finished root node.

Returns the first recorded BuildError if the call sequence was unbalanced, BuildError::UnclosedNodes if nodes are still open, or BuildError::EmptyTree if no node was ever started.

§Examples
use syntax_lang::{BuildError, Builder};

let b = Builder::<&str>::new();
assert_eq!(b.finish(), Err(BuildError::EmptyTree));

Trait Implementations§

Source§

impl<K: Debug> Debug for Builder<K>

Source§

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

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

impl<K> Default for Builder<K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<K> UnwindSafe for Builder<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> 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, 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.