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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use crate::Compiler::Core::Tokenizer::{Token, TokenType};
use crate::Compiler::AST::Position;
use crate::ErrorManager::ErrorManager;
/// Pattern types for identifier sequences
/// Used across DATA and QUICKFUNCS sections
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdentifierPatternType {
Unknown,
SimpleIdentifier, // x
LocalFunctionCall, // func()
LocalEnumAccess, // Status.ACTIVE
ImportedFunctionCall, // utils.func()
ImportedEnumAccess, // utils.Status.ACTIVE
StaticMethodCall, // Math.sqrt() (uppercase class)
TableOrGroupSyntax, // data.users: or data.users:: (DATA section only)
}
/// Represents an analyzed identifier pattern
#[derive(Debug, Clone)]
pub struct IdentifierPattern {
pub pattern_type: IdentifierPatternType,
pub first_part: String,
pub second_part: Option<String>,
pub third_part: Option<String>,
pub position: Position,
}
impl IdentifierPattern {
pub fn new(
pattern_type: IdentifierPatternType,
first_part: String,
position: Position,
) -> Self {
IdentifierPattern {
pattern_type,
first_part,
second_part: None,
third_part: None,
position,
}
}
pub fn with_second(
pattern_type: IdentifierPatternType,
first_part: String,
second_part: String,
position: Position,
) -> Self {
IdentifierPattern {
pattern_type,
first_part,
second_part: Some(second_part),
third_part: None,
position,
}
}
pub fn with_third(
pattern_type: IdentifierPatternType,
first_part: String,
second_part: String,
third_part: String,
position: Position,
) -> Self {
IdentifierPattern {
pattern_type,
first_part,
second_part: Some(second_part),
third_part: Some(third_part),
position,
}
}
}
/// Identifier pattern analysis utilities
pub struct IdentifierPatternAnalyzer;
impl IdentifierPatternAnalyzer {
/// Analyze identifier pattern in QUICKFUNCS context
/// Simpler than DATA - no table/group syntax
pub fn analyze_quickfuncs_pattern(
first_identifier: &str,
position: Position,
tokens: &[Token],
current_position: usize,
error_manager: Option<&ErrorManager>,
) -> IdentifierPattern {
Self::log_debug(
error_manager,
&format!("[QUICKFUNCS] Analyzing pattern: {}", first_identifier),
);
let next_token = Self::peek_ahead(tokens, current_position, 1);
if next_token.is_none() {
return IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
);
}
let next = next_token.unwrap();
// Check for function call: identifier(...)
if let TokenType::Symbol(sym) = &next.token_type {
if *sym == '(' {
return IdentifierPattern::new(
IdentifierPatternType::LocalFunctionCall,
first_identifier.to_string(),
position,
);
}
// Check for dot - multiple possibilities
if *sym == '.' {
return Self::analyze_dot_pattern_quickfuncs(
first_identifier,
position,
tokens,
current_position,
error_manager,
);
}
}
IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
)
}
/// Analyze identifier pattern in DATA context
/// More complex - includes table/group syntax detection
pub fn analyze_data_pattern(
first_identifier: &str,
position: Position,
tokens: &[Token],
current_position: usize,
error_manager: Option<&ErrorManager>,
) -> IdentifierPattern {
Self::log_debug(
error_manager,
&format!("[DATA] Analyzing pattern: {}", first_identifier),
);
let next_token = Self::peek_ahead(tokens, current_position, 1);
if next_token.is_none() {
return IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
);
}
let next = next_token.unwrap();
// Check for function call: identifier(...)
if let TokenType::Symbol(sym) = &next.token_type {
if *sym == '(' {
return IdentifierPattern::new(
IdentifierPatternType::LocalFunctionCall,
first_identifier.to_string(),
position,
);
}
// Check for dot - multiple possibilities
if *sym == '.' {
return Self::analyze_dot_pattern_data(
first_identifier,
position,
tokens,
current_position,
error_manager,
);
}
// Check for table/group syntax: identifier: or identifier::
if *sym == ':' {
return IdentifierPattern::new(
IdentifierPatternType::TableOrGroupSyntax,
first_identifier.to_string(),
position,
);
}
}
// Check for DoubleColon token
if matches!(next.token_type, TokenType::DoubleColon) {
return IdentifierPattern::new(
IdentifierPatternType::TableOrGroupSyntax,
first_identifier.to_string(),
position,
);
}
IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
)
}
// ==================== PRIVATE HELPERS ====================
fn analyze_dot_pattern_quickfuncs(
first_identifier: &str,
position: Position,
tokens: &[Token],
current_position: usize,
error_manager: Option<&ErrorManager>,
) -> IdentifierPattern {
let after_dot = Self::peek_ahead(tokens, current_position, 2);
if after_dot.is_none() {
return IdentifierPattern::new(
IdentifierPatternType::Unknown,
first_identifier.to_string(),
position,
);
}
let second_token = after_dot.unwrap();
// Extract second identifier value
let second_id = match &second_token.token_type {
TokenType::Identifier(id) => id.as_str(),
_ => {
return IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
);
}
};
let after_second = Self::peek_ahead(tokens, current_position, 3);
// STATIC METHOD: ClassName.method() (uppercase first letter)
if first_identifier.chars().next().is_some_and(|c| c.is_uppercase()) {
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == '(' {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{}() - Static Method", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::StaticMethodCall,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
}
}
}
// IMPORTED FUNCTION: namespace.function() (lowercase first letter)
if first_identifier.chars().next().is_some_and(|c| c.is_lowercase()) {
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == '(' {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{}() - Imported Function", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::ImportedFunctionCall,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
}
}
}
// IMPORTED ENUM: namespace.EnumName.VALUE (3 parts, no parens)
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == '.' {
let third_part = Self::peek_ahead(tokens, current_position, 4);
if let Some(third_token) = third_part {
if let TokenType::Identifier(id) = &third_token.token_type {
let third_id = id.as_str();
let after_third = Self::peek_ahead(tokens, current_position, 5);
// Make sure it's NOT followed by '('
let is_not_call = after_third.is_none_or(|t| {
!matches!(&t.token_type, TokenType::Symbol(s) if *s == '(')
});
if is_not_call {
Self::log_debug(
error_manager,
&format!(
"Pattern: {}.{}.{} - Imported Enum",
first_identifier, second_id, third_id
),
);
return IdentifierPattern::with_third(
IdentifierPatternType::ImportedEnumAccess,
first_identifier.to_string(),
second_id.to_string(),
third_id.to_string(),
position,
);
}
}
}
}
}
}
// LOCAL ENUM: EnumName.VALUE (2 parts, no parens)
let is_not_call = after_second.is_none_or(|t| {
!matches!(&t.token_type, TokenType::Symbol(s) if *s == '(')
});
if is_not_call {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{} - Local Enum", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::LocalEnumAccess,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
)
}
fn analyze_dot_pattern_data(
first_identifier: &str,
position: Position,
tokens: &[Token],
current_position: usize,
error_manager: Option<&ErrorManager>,
) -> IdentifierPattern {
let after_dot = Self::peek_ahead(tokens, current_position, 2);
if after_dot.is_none() {
return IdentifierPattern::new(
IdentifierPatternType::Unknown,
first_identifier.to_string(),
position,
);
}
let second_token = after_dot.unwrap();
// Extract second identifier value
let second_id = match &second_token.token_type {
TokenType::Identifier(id) => id.as_str(),
_ => {
return IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
);
}
};
let after_second = Self::peek_ahead(tokens, current_position, 3);
// Check for namespace.function()
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == '(' {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{}() - Imported Function", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::ImportedFunctionCall,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
}
}
// Check for namespace.Enum.VALUE (3 parts)
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == '.' {
let third_part = Self::peek_ahead(tokens, current_position, 4);
if let Some(third_token) = third_part {
if let TokenType::Identifier(id) = &third_token.token_type {
let third_id = id.as_str();
let after_third = Self::peek_ahead(tokens, current_position, 5);
// Not followed by ( or : or ::
// Handles: comma, newline, identifier, or end of tokens
let is_enum = match after_third {
None => true,
Some(t) => match &t.token_type {
TokenType::Symbol(s) => *s != '(' && *s != ':',
TokenType::DoubleColon => false,
_ => true, // comma, newline, identifier, etc.
}
};
if is_enum {
Self::log_debug(
error_manager,
&format!(
"Pattern: {}.{}.{} - Imported Enum",
first_identifier, second_id, third_id
),
);
return IdentifierPattern::with_third(
IdentifierPatternType::ImportedEnumAccess,
first_identifier.to_string(),
second_id.to_string(),
third_id.to_string(),
position,
);
}
}
}
}
}
}
// Check for local enum: Enum.VALUE
// FIXED: Correctly detect when NOT followed by '(' or ':' or '::'
// Handles: enum followed by comma, newline, identifier, or end of tokens
// IMPORTANT: Must explicitly check for DoubleColon to avoid misclassifying table syntax
let is_local_enum = match after_second {
None => true, // Nothing after = local enum
Some(t) => match &t.token_type {
TokenType::Symbol(s) => *s != '(' && *s != ':', // Symbol but not call or table
TokenType::DoubleColon => false, // Explicitly not enum (table syntax)
_ => true, // Non-symbol (comma, newline, identifier) = local enum
}
};
if is_local_enum {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{} - Local Enum", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::LocalEnumAccess,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
// Check for table syntax: identifier.property: or identifier.property::
if let Some(token) = after_second {
if let TokenType::Symbol(sym) = &token.token_type {
if *sym == ':' {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{}: - Table Property", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::TableOrGroupSyntax,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
}
if matches!(token.token_type, TokenType::DoubleColon) {
Self::log_debug(
error_manager,
&format!("Pattern: {}.{}:: - Group Array", first_identifier, second_id),
);
return IdentifierPattern::with_second(
IdentifierPatternType::TableOrGroupSyntax,
first_identifier.to_string(),
second_id.to_string(),
position,
);
}
}
// Fallback: simple identifier
IdentifierPattern::new(
IdentifierPatternType::SimpleIdentifier,
first_identifier.to_string(),
position,
)
}
/// Peek ahead N tokens without advancing position
/// Returns None if out of bounds
fn peek_ahead(tokens: &[Token], current_position: usize, offset: usize) -> Option<&Token> {
let look_ahead_pos = current_position.checked_add(offset)?;
tokens.get(look_ahead_pos)
}
/// Helper for debug logging (only logs if errorManager present and debug enabled)
fn log_debug(error_manager: Option<&ErrorManager>, message: &str) {
if let Some(em) = error_manager {
em.log_debug(&format!("[IdentifierPatternAnalyzer] {}", message));
}
}
}