pub trait Language:
Send
+ Sync
+ 'static {
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 serves as the foundation for defining programming languages within the incremental parsing system. It acts as a marker trait that ties together various language-specific components like lexers, parsers, and rebuilders.
§Overview
The Language trait is the central abstraction that enables the parsing framework to be language-agnostic while still providing language-specific functionality. Each language implementation must define its own types for tokens, elements, and the root structure of the parsed tree.
§Design Philosophy
The trait follows a compositional design where:
TokenTypedefines the atomic units of the language (tokens)ElementTypedefines the composite structures (nodes)TypedRootdefines the top-level structure of the parsed document
This separation allows for maximum flexibility while maintaining type safety and performance characteristics required for incremental parsing.
§Examples
// Define a simple language
#[derive(Clone)]
struct MyLanguage;
impl Language for MyLanguage {
const NAME: &'static str = "my-language";
type TokenType = MyToken;
type ElementType = MyElement;
type TypedRoot = ();
}
// With corresponding type definitions
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum MyToken {
Identifier,
EndOfStream,
}
impl TokenType for MyToken {
const END_OF_STREAM: Self = MyToken::EndOfStream;
type Role = UniversalTokenRole;
fn role(&self) -> Self::Role { UniversalTokenRole::None }
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum MyElement {}
impl ElementType for MyElement {
type Role = UniversalElementRole;
fn role(&self) -> Self::Role { UniversalElementRole::None }
}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.