Skip to main content

perl_semantic_analyzer/analysis/semantic/
tokens.rs

1//! Semantic token types and modifiers for LSP syntax highlighting.
2
3use crate::SourceLocation;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6/// Semantic token types for syntax highlighting in the Parse/Complete workflow.
7pub enum SemanticTokenType {
8    // Variables
9    /// Variable reference (scalar, array, or hash)
10    Variable,
11    /// Variable declaration site
12    VariableDeclaration,
13    /// Read-only variable (constant)
14    VariableReadonly,
15    /// Function parameter
16    Parameter,
17
18    // Functions
19    /// Function/subroutine reference
20    Function,
21    /// Function/subroutine declaration
22    FunctionDeclaration,
23    /// Object method call
24    Method,
25
26    // Types
27    /// Class/package name
28    Class,
29    /// Package namespace
30    Namespace,
31    /// Type annotation (modern Perl)
32    Type,
33
34    // Keywords
35    /// Language keyword (if, while, etc.)
36    Keyword,
37    /// Control flow keyword (return, next, last)
38    KeywordControl,
39    /// Variable modifier (my, our, local, state)
40    Modifier,
41
42    // Literals
43    /// Numeric literal
44    Number,
45    /// String literal
46    String,
47    /// Regular expression
48    Regex,
49
50    // Comments
51    /// Regular comment
52    Comment,
53    /// Documentation comment (POD)
54    CommentDoc,
55
56    // Other
57    /// Operator (+, -, =~, etc.)
58    Operator,
59    /// Punctuation marks and delimiters
60    Punctuation,
61    /// Code label for goto statements
62    Label,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66/// Semantic token modifiers for Analyze/Complete stage highlighting.
67///
68/// Provides additional context about semantic tokens beyond their base type,
69/// enabling rich editor highlighting with detailed symbol information.
70///
71/// # LSP Integration
72/// Maps to LSP `SemanticTokenModifiers` for consistent editor experience
73/// across different LSP clients with full Perl language semantics.
74pub enum SemanticTokenModifier {
75    /// Symbol is being declared at this location
76    Declaration,
77    /// Symbol is being defined at this location
78    Definition,
79    /// Symbol is read-only (constant)
80    Readonly,
81    /// Symbol has static storage duration (state variables)
82    Static,
83    /// Symbol is deprecated and should not be used
84    Deprecated,
85    /// Symbol is abstract (method without implementation)
86    Abstract,
87    /// Symbol represents an asynchronous operation
88    Async,
89    /// Symbol is being modified (written to)
90    Modification,
91    /// Symbol is documentation-related (POD)
92    Documentation,
93    /// Symbol is from the Perl standard library
94    DefaultLibrary,
95}
96
97#[derive(Debug, Clone)]
98/// A semantic token with type and modifiers for LSP syntax highlighting.
99///
100/// Represents a single semantic unit in Perl source code with precise location
101/// and rich type information for enhanced editor experience.
102///
103/// # Performance Characteristics
104/// - Memory: ~32 bytes per token (optimized for large files)
105/// - Serialization: Direct LSP protocol mapping
106/// - Batch processing: Efficient delta updates for incremental parsing
107///
108/// # LSP Workflow Integration
109/// Core component in Parse → Index → Navigate → Complete → Analyze pipeline
110/// for real-time syntax highlighting with ≤1ms update latency.
111pub struct SemanticToken {
112    /// Source location of the token
113    pub location: SourceLocation,
114    /// Semantic classification of the token
115    pub token_type: SemanticTokenType,
116    /// Additional modifiers for enhanced highlighting
117    pub modifiers: Vec<SemanticTokenModifier>,
118}