pub struct SemanticModel { /* private fields */ }Expand description
Semantic analysis types for hover, tokens, and code understanding. A stable, query-oriented view of semantic information over a parsed file.
LSP and other consumers should use this instead of talking to SemanticAnalyzer directly.
This provides a clean API that insulates consumers from internal analyzer implementation details.
§Performance Characteristics
- Symbol resolution: <50μs average lookup time
- Reference queries: O(1) lookup via pre-computed indices
- Scope queries: O(log n) with binary search on scope ranges
§LSP Workflow Integration
Core component in Parse → Index → Navigate → Complete → Analyze pipeline:
- Parse Perl source → AST
- Build SemanticModel from AST
- Query for symbols, references, completions
- Respond to LSP requests with precise semantic data
§Example
use perl_parser::Parser;
use perl_parser::semantic::SemanticModel;
let code = "my $x = 42; $x + 10;";
let mut parser = Parser::new(code);
let ast = parser.parse()?;
let model = SemanticModel::build(&ast, code);
let tokens = model.tokens();
assert!(!tokens.is_empty());Implementations§
Source§impl SemanticModel
impl SemanticModel
Sourcepub fn build(root: &Node, source: &str) -> SemanticModel
pub fn build(root: &Node, source: &str) -> SemanticModel
Sourcepub fn tokens(&self) -> &[SemanticToken]
pub fn tokens(&self) -> &[SemanticToken]
All semantic tokens for syntax highlighting.
Returns tokens in source order for efficient LSP semantic tokens encoding.
§Performance
- Lookup: O(1) - pre-computed during analysis
- Memory: ~32 bytes per token
Sourcepub fn symbol_table(&self) -> &SymbolTable
pub fn symbol_table(&self) -> &SymbolTable
Access the underlying symbol table for advanced queries.
§Note
Most consumers should use the higher-level query methods on SemanticModel
rather than accessing the symbol table directly.
Sourcepub fn hover_info_at(&self, location: ByteSpan) -> Option<&HoverInfo>
pub fn hover_info_at(&self, location: ByteSpan) -> Option<&HoverInfo>
Get hover information for a symbol at a specific location during Navigate/Analyze.
§Parameters
location: Source location to query (line, column)
§Returns
Some(HoverInfo)if a symbol with hover info exists at this locationNoneif no symbol or no hover info available
§Performance
- Lookup: <100μs for typical files
- Memory: Cached hover info reused across queries
Workflow: Navigate/Analyze hover lookup.
Sourcepub fn definition_at(&self, position: usize) -> Option<&Symbol>
pub fn definition_at(&self, position: usize) -> Option<&Symbol>
Find the definition of a symbol at a specific byte position.
§Parameters
position: Byte offset in the source code
§Returns
Some(Symbol)if a symbol definition is found at this positionNoneif no symbol exists at this position
§Performance
- Lookup: <50μs average for typical files
- Uses pre-computed symbol table for O(1) lookups
§Example
use perl_parser::Parser;
use perl_parser::semantic::SemanticModel;
let code = "my $x = 1;\n$x + 2;\n";
let mut parser = Parser::new(code);
let ast = parser.parse()?;
let model = SemanticModel::build(&ast, code);
// Find definition of $x on line 1 (byte position ~11)
if let Some(symbol) = model.definition_at(11) {
assert_eq!(symbol.location.start.line, 0);
}