Skip to main content

SemanticModel

Struct SemanticModel 

Source
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:

  1. Parse Perl source → AST
  2. Build SemanticModel from AST
  3. Query for symbols, references, completions
  4. 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

Source

pub fn build(root: &Node, source: &str) -> SemanticModel

Build a semantic model for a parsed syntax tree.

§Parameters
  • root: The root AST node from the parser
  • source: The original Perl source code
§Performance
  • Analysis time: O(n) where n is AST node count
  • Memory: ~1MB per 10K lines of Perl code
Source

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
Source

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.

Source

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 location
  • None if 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.

Source

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 position
  • None if 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);
}

Trait Implementations§

Source§

impl Debug for SemanticModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more