#[non_exhaustive]pub enum UniversalElementRole {
Show 19 variants
Root,
Container,
Definition,
Binding,
Reference,
Typing,
Documentation,
Metadata,
Attribute,
AttributeKey,
Detail,
Name,
Statement,
Expression,
Call,
Value,
Embedded,
Error,
None,
}Expand description
Represents the general structural role of a syntax tree element.
§Universal Grammar
This mechanism is inspired by Noam Chomsky’s Universal Grammar theory, applied here to the structural hierarchy of syntax trees. It posits that while the “Surface Structure” (the specific production rules of a grammar) varies across languages, they share a common “Deep Structure” (structural intent).
In the Oak framework, syntax tree elements are categorized by their role:
- Surface Structure: Refers to specific node kinds defined by a language
(e.g., Rust’s
FnDeclaration, SQL’sSelectStatement, or YAML’sMapping). - Deep Structure: Refers to the universal structural patterns defined in this enum.
By mapping to these roles, we can perform sophisticated analysis across diverse language families:
- Containers & Statements: Identify hierarchical scopes and their constituents (e.g., a SQL table is a container, its clauses are statements).
- Bindings & References: Identify the flow of information and identifiers (e.g., an ASM label is a binding, a jump instruction is a reference).
- Values: Identify the atomic data payload or expression results.
§Design Philosophy: The 99% Rule
This enum is designed to provide a “sufficiently complete” abstraction for common tool requirements (Highlighting, Outline, Navigation, and Refactoring) while maintaining language-agnostic simplicity.
§1. Structural Identity (The “What”)
Roles describe a node’s primary structural responsibility in the tree, not its domain-specific semantic meaning. For example:
- A “Class” or “Function” is structurally a [
Definition] and often a [Container]. - An “Import” is structurally a [
Statement] that contains a [Reference].
§2. Broad Categories (The “How”)
We categorize elements into four major structural groups:
- Flow Control & logic: [
Statement], [Expression], [Call], and [Root]. - Symbol Management: [
Definition], [Binding], and [Reference]. - Hierarchy & Scoping: [
Container]. - Metadata & Auxiliaries: [
Typing], [Metadata], [Attribute], [Documentation], etc.
§3. Intent-Based Selection
When a node could fit multiple roles, choose the one that represents its primary structural intent.
- Example: In Rust, an
ifexpression is both anExpressionand aContainer. However, its primary role in the tree is as an [Expression] (producing a value), whereas its children (the blocks) are [Container]s. - Example: In Markdown, a “List” is a [
Container], while each “ListItem” is a [Statement] within that container.
§4. Intentional Exclusions
We intentionally exclude roles that can be represented by combining existing roles or that require deep semantic analysis:
- Keyword-specific roles: Roles like “Loop”, “Conditional”, or “Module” are excluded.
These are surface-level distinctions. In the Deep Structure, they are all [
Container]s or [Statement]s. - Semantic Relationships: Roles like “Inheritance”, “Implementation”, or “Dependency” are excluded. These are better handled by semantic graph analysis rather than syntactic tree roles.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Root
The top-level root of the syntax tree, representing the entire document or source file.
Container
A high-level structural container that defines a scope or logical grouping.
Definition
A node that represents the entire declaration or definition of a symbol.
This role identifies the “whole” entity that defines something in the code, which is crucial for building symbol trees and navigation outlines.
§Examples
- Rust: The entire
Fndeclaration block,Structitem, orEnum. - Markdown:
HeadingorLinkDefinition. - SQL: The whole
CREATE TABLEorCREATE PROCEDUREstatement. - ASM: A
Proc(procedure) block or a multi-line data definition. - YAML: A schema-defined object or a complex configuration block.
Binding
A node that specifically performs the act of binding a name to an entity.
Unlike Definition, which represents the entire construct, Binding targets
the specific part (usually the identifier) that introduces the name.
§Examples
- Rust: The identifier node in a
letpattern or function name. - Markdown:
LinkLabelin a reference link definition. - SQL: The
Tablename identifier inCREATE TABLE. - ASM: A
Labelnode (e.g.,main:). - YAML: The
Keyin a key-value mapping.
Reference
A node that refers to an existing name or entity defined elsewhere.
§Examples
- Rust:
PathExpr(variable usage) orMethodCall. - Markdown:
LinkReferenceorFootnoteReference. - SQL:
ColumnNamein aSELECTclause orTableNameinFROM. - ASM: A
Labelreference in a jump (e.g.,JMP main). - YAML: An
Aliasanchor (e.g.,*anchor_name).
Typing
A node representing a type signature, constraint, or type reference.
This role distinguishes type information from general logic or values, which is essential for type checking and intelligent completion.
§Examples
- Rust:
TypePath(e.g.,: i32),GenericArgument, orWhereClause. - SQL:
DataType(e.g.,VARCHAR(255)orINT). - ASM: Size specifiers (e.g.,
DWORD,PTR). - TypeScript:
TypeAnnotationorInterfaceDeclaration.
Documentation
Structured comments or documentation nodes attached to other elements.
Unlike raw Comment tokens, these are syntax nodes that may contain
their own internal structure (like Markdown or Tagged parameters).
§Examples
- Rust:
DocComment(e.g.,/// ...). - Java:
Javadocblocks. - Python:
Docstringliterals.
Metadata
High-level annotations, decorators, or macros that provide extra semantic info.
§Metadata vs Attribute
- Metadata: Usually refers to language-level extensions that “decorate” an element from the outside, often affecting compilation or runtime behavior (e.g., Rust attributes).
- Attribute: Usually refers to built-in, structural properties that are part of the element’s native definition (e.g., HTML attributes).
§Examples
- Rust:
Attribute(e.g.,#[derive(...)]) orMacroCall. - Markdown:
Frontmatter(YAML/TOML header). - Java/TS:
@Decoratoror@Annotation. - Python:
@decoratorsyntax.
Attribute
A specific property, flag, or attribute-value pair.
Unlike Metadata, which decorates an element with external logic, Attribute
represents intrinsic properties defined by the language’s schema or structure.
§Examples
- HTML/XML: An
Attribute(e.g.,id="main"). - Markdown:
LinkTitleorImageAlttext. - YAML: A specific configuration property.
- ASM: Segment attributes (e.g.,
READONLY,EXECUTE).
AttributeKey
The key part of an attribute, property, or configuration entry.
This role is distinct because:
- It is not a Reference (it doesn’t refer to an external symbol).
- It is not a traditional Binding (it doesn’t define a symbol in a global or lexical scope).
- It is not a Keyword (it is typically a user-defined or schema-defined identifier).
§Examples
- HTML: The
idinid="main". - Markdown:
AttributeName(in Pandoc-style{ #id .class }). - YAML: The key in a property mapping.
- TOML: The key in a table entry.
Detail
A node that provides additional details or secondary information for another element.
§Examples
- Rust:
GenericParameterlist,FunctionParameterlist. - SQL:
Constraintdetails.
Name
A node that represents the name of an element, typically used in declarations.
§Examples
- Rust: The name identifier in a function or struct definition.
- HTML: The tag name in an element.
Statement
A discrete syntactic unit within a container, representing a single logical entry or instruction.
This typically maps to a Statement in programming languages, or a standalone instruction in assembly. In markup, it could represent a list item or a table row.
§Examples
- Rust: A
Stmtinside a block. - Markdown:
ListItemorTableCell. - SQL: A standalone
Statementor aClause(likeWHERE). - ASM: A single
Instruction(e.g.,NOP).
Expression
A node representing a computed result or a complex logical operation.
Unlike a simple Value (which is an atomic literal), an Expression involves
operators or logic that must be evaluated.
§Examples
- Rust:
BinaryExpr,UnaryExpr, orRangeExpr. - SQL:
BinaryOpin aWHEREclause. - Python:
ListComprehensionorLambda.
Call
A node that performs an invocation or call to a function, method, or macro.
This role identifies the active execution of a named entity with optional arguments.
§Examples
- Rust:
CallExpr,MethodCallExpr, orMacroInvocation. - SQL:
FunctionCall(e.g.,COUNT(*)). - Excel: A formula call.
Value
A node representing an atomic data value or a primitive constant.
This role is strictly for atomic values like numbers, strings, or booleans.
It does not include composite structures like arrays [] or objects {},
which should be categorized as UniversalElementRole::Container.
§Examples
- Rust:
Literal(strings, numbers, booleans). - Markdown:
InlineCode,Emphasis, orStrong. - SQL:
Literalvalues. - JSON/YAML: Atomic
Scalarvalues (strings, integers, nulls).
Embedded
A node that acts as a host for content in a different language or a raw fragment requiring a separate parsing pass (Language Injection).
§Examples
- HTML: A
<script>or<style>block containing JS/CSS. - Markdown:
CodeBlock(host for other languages). - Rust/Java: A string literal containing SQL (if marked for injection).
- PHP: Raw HTML fragments outside of
<?php ... ?>tags.
Error
A node specifically created to represent a syntax error or recovery point in the source code.
None
No specific structural role assigned or recognized for this element.
Trait Implementations§
Source§impl Clone for UniversalElementRole
impl Clone for UniversalElementRole
Source§fn clone(&self) -> UniversalElementRole
fn clone(&self) -> UniversalElementRole
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more