Skip to main content

ElementType

Trait ElementType 

Source
pub trait ElementType:
    Copy
    + Eq
    + Hash
    + Send
    + Sync
    + 'static
    + Debug {
    type Role: ElementRole;

    // Required method
    fn role(&self) -> Self::Role;

    // Provided methods
    fn is_role(&self, role: Self::Role) -> bool { ... }
    fn is_universal(&self, role: UniversalElementRole) -> bool { ... }
    fn is_root(&self) -> bool { ... }
    fn is_error(&self) -> bool { ... }
}
Expand description

Element type definitions for nodes in the parsed tree.

While tokens represent the atomic units of a language, elements represent the composite structures formed by combining tokens according to grammar rules. This includes expressions, statements, declarations, and other syntactic constructs.

§Universal Grammar Philosophy

Just like tokens, syntax tree elements are mapped from their “Surface Structure” (language-specific nodes) to a “Deep Structure” via UniversalElementRole.

This allows structural analysis tools (like symbol outline extractors) to identify UniversalElementRole::Binding (definitions) or UniversalElementRole::Container (scopes/blocks) uniformly across different language families.

§Implementation Guidelines

When implementing this trait for a specific language:

  • Use an enum with discriminant values for efficient matching
  • Include a Root variant to identify the top-level element
  • Include an Error variant for malformed constructs
  • Define a Role associated type and implement the role() method.

§Examples

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum MyElement {
    Root,
    FunctionDeclaration,
    Block,
    Error,
}

impl ElementType for MyElement {
    type Role = UniversalElementRole;

    fn role(&self) -> Self::Role {
        match self {
            MyElement::Root => UniversalElementRole::Root,
            MyElement::FunctionDeclaration => UniversalElementRole::Binding,
            MyElement::Block => UniversalElementRole::Container,
            MyElement::Error => UniversalElementRole::Error,
        }
    }

    fn is_root(&self) -> bool {
        matches!(self, MyElement::Root)
    }

    fn is_error(&self) -> bool {
        matches!(self, MyElement::Error)
    }
}

Required Associated Types§

Source

type Role: ElementRole

The associated role type for this element kind.

Required Methods§

Source

fn role(&self) -> Self::Role

Returns the general syntactic role of this element.

This helps external tools understand the structural purpose of a node (e.g., is it a container, a binding, or a value) without deep language knowledge.

Provided Methods§

Source

fn is_role(&self, role: Self::Role) -> bool

Returns true if this element matches the specified language-specific role.

Source

fn is_universal(&self, role: UniversalElementRole) -> bool

Returns true if this element matches the specified universal role.

Source

fn is_root(&self) -> bool

Returns true if this element represents the root of the parsed tree.

§Default Implementation

Based on UniversalElementRole::Root.

Source

fn is_error(&self) -> bool

Returns true if this element represents an error condition.

§Default Implementation

Based on UniversalElementRole::Error.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§