1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use mortar_compiler::{Token, TokenInfo, tokenize};
use tower_lsp_server::lsp_types::*;
use crate::backend::Backend;
impl Backend {
/// Analyze semantic tokens for syntax highlighting
pub fn analyze_semantic_tokens(&self, content: &str) -> Vec<SemanticToken> {
let mut tokens = Vec::new();
let mut last_line = 0u32;
let mut last_column = 0u32;
// Tokenize the entire document instead of processing line by line
let compiler_tokens = tokenize(content);
for (i, token_info) in compiler_tokens.iter().enumerate() {
let token_type =
self.get_semantic_token_type_with_context(&token_info.token, &compiler_tokens, i);
// Calculate token line and column position
let (token_line, token_column) =
self.get_line_column_position(content, token_info.start);
// Calculate token's UTF-16 length (instead of UTF-8 byte length)
let token_text = &content[token_info.start..token_info.end];
let length = token_text.encode_utf16().count() as u32;
let delta_line = token_line - last_line;
let delta_start = if delta_line == 0 {
token_column - last_column
} else {
token_column
};
tokens.push(SemanticToken {
delta_line,
delta_start,
length,
token_type,
token_modifiers_bitset: 0,
});
last_line = token_line;
last_column = token_column;
}
tokens
}
/// Calculate line and column position from byte offset
/// LSP uses UTF-16 code units for column positions
fn get_line_column_position(&self, content: &str, offset: usize) -> (u32, u32) {
let mut line = 0u32;
let mut utf16_column = 0u32;
// Use byte indices for iteration to ensure offset matching with tokenizer
for (i, ch) in content.char_indices() {
if i >= offset {
break;
}
if ch == '\n' {
line += 1;
utf16_column = 0;
} else {
// UTF-16 code unit count: most characters take 1 unit, but some Unicode characters need 2 units
utf16_column += ch.len_utf16() as u32;
}
}
(line, utf16_column)
}
/// Check if the current identifier is in a choice context (e.g., choice list)
fn is_in_choice_context(&self, all_tokens: &[TokenInfo], current_index: usize) -> bool {
// Look backward to find 'choice' keyword and corresponding structure
let mut bracket_depth = 0;
for i in (0..current_index).rev() {
match all_tokens[i].token {
Token::RightBracket => bracket_depth += 1,
Token::LeftBracket => {
bracket_depth -= 1;
if bracket_depth < 0 {
// Found matching left bracket, continue looking for choice keyword
for j in (0..i).rev() {
match all_tokens[j].token {
Token::Choice => return true,
Token::LeftBrace | Token::RightBrace => break, // Crossed node boundary
_ => continue,
}
}
break;
}
}
_ => {}
}
}
false
}
/// Get semantic token type from compiler lexical token with context awareness
fn get_semantic_token_type_with_context(
&self,
token: &Token,
all_tokens: &[TokenInfo],
current_index: usize,
) -> u32 {
const KEYWORD: u32 = 0;
const STRING: u32 = 1;
const NUMBER: u32 = 2;
const COMMENT: u32 = 3;
const FUNCTION: u32 = 4;
const VARIABLE: u32 = 5;
const METHOD: u32 = 6; // Used for function calls
const OPERATOR: u32 = 7;
const PUNCTUATION: u32 = 8;
match token {
Token::SingleLineComment(_) | Token::MultiLineComment(_) => COMMENT,
Token::Node
| Token::Text
| Token::Events
| Token::Choice
| Token::Fn
| Token::Return
| Token::Break
| Token::When
| Token::Let
| Token::Const
| Token::Pub
| Token::Enum
| Token::Branch
| Token::If
| Token::Else
| Token::Event
| Token::Run
| Token::With
| Token::Now
| Token::Timeline
| Token::Wait
| Token::Index
| Token::Action
| Token::Duration
| Token::StringType
| Token::NumberType
| Token::BooleanType
| Token::True
| Token::False => KEYWORD,
Token::String(_) | Token::InterpolatedString(_) => STRING,
Token::Number(_) => NUMBER,
Token::Arrow => OPERATOR,
Token::Colon
| Token::Comma
| Token::Semicolon
| Token::Dot
| Token::LeftBrace
| Token::RightBrace
| Token::LeftBracket
| Token::RightBracket
| Token::LeftParen
| Token::RightParen
| Token::Equals
| Token::Less
| Token::Greater
| Token::LessEqual
| Token::GreaterEqual
| Token::EqualEqual
| Token::NotEqual
| Token::And
| Token::Or
| Token::Not => PUNCTUATION,
Token::Identifier(_) => {
// Check if it's an identifier after node/nd or fn (function/node definition)
if current_index > 0
&& let Some(prev_token_info) = all_tokens.get(current_index - 1)
{
match prev_token_info.token {
Token::Node | Token::Fn => return FUNCTION,
_ => {}
}
}
// Check if it's a function call (identifier followed by left parenthesis)
if current_index + 1 < all_tokens.len()
&& let Some(next_token_info) = all_tokens.get(current_index + 1)
&& matches!(next_token_info.token, Token::LeftParen)
{
return METHOD;
}
// Check if it's a node call (identifier in choice or jump context)
// In this case, identifier usually appears after arrow (->) or comma
if current_index > 0
&& let Some(prev_token_info) = all_tokens.get(current_index - 1)
{
match prev_token_info.token {
Token::Arrow => return METHOD, // Node jump
Token::Comma => {
// Node reference in choice list
// Check if previous tokens indicate this is a choice context
if self.is_in_choice_context(all_tokens, current_index) {
return METHOD;
}
}
_ => {}
}
}
VARIABLE
}
Token::Error => KEYWORD,
}
}
}