pub trait Language: Send + Sync {
type TokenType: TokenType;
type ElementType: ElementType;
type TypedRoot;
const NAME: &'static str;
const CATEGORY: LanguageCategory = LanguageCategory::Programming;
}Expand description
Language definition trait that coordinates all language-related types and behaviors.
This trait ties together language-specific components like lexers, parsers, and ASTs. It enables the framework to be language-agnostic while providing type-safe language-specific functionality.
Required Associated Constants§
Provided Associated Constants§
Sourceconst CATEGORY: LanguageCategory = LanguageCategory::Programming
const CATEGORY: LanguageCategory = LanguageCategory::Programming
The category of the language.
Required Associated Types§
Sourcetype TokenType: TokenType
type TokenType: TokenType
The token type used to represent different token and node types in the language.
This associated type defines how different syntactic elements (tokens, nodes) are
categorized and identified within the language. It must implement Copy and Eq
to ensure efficient handling in the parsing system.
§Requirements
The token type must:
- Implement the
TokenTypetrait - Be copyable to enable efficient passing
- Support equality comparison for token matching
- Be sendable across thread boundaries
§Examples
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum RustSyntaxKind {
LetKeyword,
Identifier,
Number,
// ... other token kinds
}Sourcetype ElementType: ElementType
type ElementType: ElementType
The element type used to represent composite structures in the parsed tree.
While tokens represent the atomic units of the language, elements represent the composite structures formed by combining tokens according to grammar rules. This includes expressions, statements, declarations, and other syntactic constructs.
§Requirements
The element type must:
- Implement the
ElementTypetrait - Be copyable for efficient handling
- Support equality comparison
- Be sendable across thread boundaries
Sourcetype TypedRoot
type TypedRoot
The root type for the parsed tree that represents the top-level structure of the language.
This associated type defines the structure of the root node in the parsed tree, which typically contains the entire parsed source code organized according to the language’s grammar rules. The root type serves as the entry point for traversing and manipulating the parsed representation.
§Design Considerations
The root type should:
- Contain references to all top-level language constructs
- Provide efficient access to the parsed content
- Support incremental updates when the source changes
§Examples
struct RustRoot {
items: Vec<RustItem>,
}
struct RustRoot {
modules: Vec<Module>,
imports: Vec<Import>,
declarations: Vec<Declaration>,
}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.