Skip to main content

ASTNode

Struct ASTNode 

Source
#[non_exhaustive]
pub struct ASTNode { pub children: Option<Vec<ASTNode>>, pub token_count: u32, pub text: u32, }
Expand description

AST node. Grammar rules have children, tokens do not.

The total structure of the AST is defined by the grammar that it was parsed with.

Why isn’t this an enum? Option<Vec<ASTNode>> and other two-variant enums containing a Vec undergo niche optimization. If this were enum ASTNode { Rule{...}, Token(u32) } then it would look like you can just add a third variant (e.g. poisoned) without issue. However, doing that would actually increase the size of the ASTNode from 32 bytes to 40 bytes.

If the size is ever forced above 32 bytes (e.g. increasing token count from u32 to u64) then I’ll probably change it to an enum.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§children: Option<Vec<ASTNode>>

If Some, this node is a parent/nonterminal. If None, this node is a token/leaf/terminal.

§token_count: u32

Due to error recovery, AST nodes can be marked as “poisoned”.

When a node is poisoned, its token count is XOR’d with !0u32 (all one-bits).

§text: u32

Index into grammar.string_cache_inv, giving an Rc<String>.

For parents, it’s the name of the associated grammar rule.

For tokens, it’s the token content (the actual token contents, but what it was matched with, i.e. it’s not a regex).

Implementations§

Source§

impl ASTNode

Source

pub fn new(children: Option<Vec<ASTNode>>, token_count: u32, text: u32) -> Self

Source

pub fn is_poisoned(&self) -> bool

Source

pub fn get_real_token_count(&self) -> u32

Trait Implementations§

Source§

impl Clone for ASTNode

Source§

fn clone(&self) -> ASTNode

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ASTNode

Source§

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

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

impl Default for ASTNode

Source§

fn default() -> ASTNode

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

Auto Trait Implementations§

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.