Trait vhdl_lang::HasTokenSpan
source · pub trait HasTokenSpan {
// Required methods
fn get_start_token(&self) -> TokenId;
fn get_end_token(&self) -> TokenId;
fn get_token_slice<'a>(&self, tokens: &'a dyn TokenAccess) -> &'a [Token];
fn get_pos(&self, tokens: &dyn TokenAccess) -> SrcPos;
// Provided methods
fn get_span(&self, ctx: &dyn TokenAccess) -> SrcPos { ... }
fn span(&self) -> TokenSpan { ... }
}
Expand description
AST elements for which it is necessary to get the underlying tokens can implement the HasTokenSpan
trait.
The trait provides getters for the start and end token.
Using the with_token_span
attribute macro, the necessary fields can be inserted, and HasTokenSpan
is implemented automatically.
For enums containing alternative AST elements the custom derive macro can be used directly under certain constraints:
- All variants must contain exactly one unnamed field.
- The fields of all variants must implement the
HasTokenSpan
trait one way or another.
Example:
use vhdl_lang_macros::{with_token_span, TokenSpan};
// With `with_token_span` a field `info` of type `(TokenId, TokenId)` is inserted.
// Additionally the `HasTokenSpan` trait is implemented using the `TokenSpan` derive macro
#[with_token_span]
#[derive(PartialEq, Debug, Clone)]
pub struct UseClause {
pub name_list: ::vhdl_lang::ast::NameList,
}
#[with_token_span]
#[derive(PartialEq, Debug, Clone)]
pub struct ContextReference {
pub name_list: ::vhdl_lang::ast::NameList,
}
#[with_token_span]
#[derive(PartialEq, Debug, Clone)]
pub struct LibraryClause {
pub name_list: ::vhdl_lang::ast::IdentList,
}
// Enums can use the `TokenSpan` derive macro directly
#[derive(PartialEq, Debug, Clone, TokenSpan)]
pub enum ContextItem {
Use(UseClause),
Library(LibraryClause),
Context(ContextReference),
}