perl-parser-core 0.13.3

Core parser engine for perl-parser
Documentation
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
//! Parser context with error recovery support
//!
//! This module provides a parsing context that tracks errors, positions,
//! and supports error recovery for IDE scenarios.

use crate::{
    error::{BudgetTracker, ParseBudget},
    error_recovery::ParseError,
    position::{Position, Range},
    token_wrapper::TokenWithPosition,
};
use perl_ast_v2::NodeIdGenerator;
use perl_lexer::TokenType;
use perl_position_tracking::LineStartsCache;
use std::collections::VecDeque;

/// Parser context with error tracking and recovery
pub struct ParserContext {
    /// Token stream with positions
    tokens: VecDeque<TokenWithPosition>,
    /// Current token index
    current: usize,
    /// Node ID generator
    pub id_generator: NodeIdGenerator,
    /// Accumulated parse errors
    errors: Vec<ParseError>,
    /// Source text
    source: String,
    /// Position tracker for efficient position mapping
    _position_tracker: PositionTracker,
    /// Budget limits for this parse
    budget: ParseBudget,
    /// Budget consumption tracker
    budget_tracker: BudgetTracker,
}

/// Efficient position tracking using line starts cache
///
/// This implementation leverages the existing LineStartsCache for O(log n) position lookups
/// instead of O(n) character-by-character advancement. It provides UTF-16 aware position
/// mapping for LSP compatibility while integrating with the existing position infrastructure.
struct PositionTracker {
    /// Cache for O(log n) position lookups
    line_cache: LineStartsCache,
    /// Source text reference
    source: String,
}

impl PositionTracker {
    fn new(source: String) -> Self {
        let line_cache = LineStartsCache::new(&source);
        PositionTracker { line_cache, source }
    }

    /// Convert byte offset to position with UTF-16 support
    fn byte_to_position(&self, byte_offset: usize) -> Position {
        let (line, character) = self.line_cache.offset_to_position(&self.source, byte_offset);
        // LineStartsCache returns 0-based line numbers, but Position expects 1-based
        Position::new(byte_offset, line + 1, character + 1)
    }
}

impl ParserContext {
    /// Create a new parser context
    pub fn new(source: String) -> Self {
        let mut tokens = VecDeque::new();
        let position_tracker = PositionTracker::new(source.clone());

        // Tokenize the source using mode-aware lexer
        let mut lexer = perl_lexer::PerlLexer::new(&source);
        loop {
            match lexer.next_token() {
                Some(token) => {
                    // Skip EOF tokens to avoid infinite loop
                    if matches!(token.token_type, TokenType::EOF) {
                        break;
                    }

                    let start = token.start;
                    let end = token.end;

                    // Use efficient position mapping with UTF-16 support
                    let start_pos = position_tracker.byte_to_position(start);
                    let end_pos = position_tracker.byte_to_position(end);

                    tokens.push_back(TokenWithPosition::new(token, start_pos, end_pos));
                }
                None => break,
            }
        }

        ParserContext {
            tokens,
            current: 0,
            id_generator: NodeIdGenerator::new(),
            errors: Vec::new(),
            source,
            _position_tracker: position_tracker,
            budget: ParseBudget::default(),
            budget_tracker: BudgetTracker::new(),
        }
    }

    /// Create a new parser context with a custom budget.
    pub fn with_budget(source: String, budget: ParseBudget) -> Self {
        let mut ctx = Self::new(source);
        ctx.budget = budget;
        ctx
    }

    /// Get the current budget.
    pub fn budget(&self) -> &ParseBudget {
        &self.budget
    }

    /// Get the budget tracker.
    pub fn budget_tracker(&self) -> &BudgetTracker {
        &self.budget_tracker
    }

    /// Get mutable access to the budget tracker.
    pub fn budget_tracker_mut(&mut self) -> &mut BudgetTracker {
        &mut self.budget_tracker
    }

    /// Check if error budget is exhausted.
    pub fn errors_exhausted(&self) -> bool {
        self.budget_tracker.errors_exhausted(&self.budget)
    }

    /// Check if depth budget would be exceeded.
    pub fn depth_would_exceed(&self) -> bool {
        self.budget_tracker.depth_would_exceed(&self.budget)
    }

    /// Enter a nesting level, tracking depth.
    pub fn enter_depth(&mut self) -> bool {
        if self.depth_would_exceed() {
            return false;
        }
        self.budget_tracker.enter_depth();
        true
    }

    /// Exit a nesting level.
    pub fn exit_depth(&mut self) {
        self.budget_tracker.exit_depth();
    }

    /// Get current token
    pub fn current_token(&self) -> Option<&TokenWithPosition> {
        self.tokens.get(self.current)
    }

    /// Peek at next token
    pub fn peek_token(&self, offset: usize) -> Option<&TokenWithPosition> {
        self.tokens.get(self.current + offset)
    }

    /// Advance to next token
    pub fn advance(&mut self) -> Option<&TokenWithPosition> {
        if self.current < self.tokens.len() {
            self.current += 1;
        }
        self.current_token()
    }

    /// Check if at end of tokens
    pub fn is_eof(&self) -> bool {
        self.current >= self.tokens.len()
    }

    /// Get current position
    pub fn current_position(&self) -> Position {
        if let Some(token) = self.current_token() {
            token.range().start
        } else if let Some(last_token) = self.tokens.back() {
            // At EOF, use end of last token
            last_token.range().end
        } else {
            Position::new(0, 1, 1)
        }
    }

    /// Get current position range
    pub fn current_position_range(&self) -> Range {
        if let Some(token) = self.current_token() {
            token.range()
        } else {
            let pos = self.current_position();
            Range::new(pos, pos)
        }
    }

    /// Add a parse error, tracking budget consumption.
    ///
    /// Returns `true` if the error was added, `false` if error budget exhausted.
    pub fn add_error(&mut self, error: ParseError) -> bool {
        if self.errors_exhausted() {
            return false;
        }
        self.errors.push(error);
        self.budget_tracker.record_error();
        true
    }

    /// Add a parse error without checking budget (for critical errors).
    pub fn add_error_unchecked(&mut self, error: ParseError) {
        self.errors.push(error);
        self.budget_tracker.record_error();
    }

    /// Get all accumulated errors
    pub fn take_errors(&mut self) -> Vec<ParseError> {
        std::mem::take(&mut self.errors)
    }

    /// Get current token index (for saving/restoring)
    pub fn current_index(&self) -> usize {
        self.current
    }

    /// Set current token index (for restoring)
    pub fn set_index(&mut self, index: usize) {
        self.current = index.min(self.tokens.len());
    }

    /// Match and consume a specific token type
    pub fn expect(&mut self, expected: TokenType) -> Result<&TokenWithPosition, ParseError> {
        match self.current_token() {
            Some(token) if token.token.token_type == expected => {
                self.advance();
                Ok(&self.tokens[self.current - 1])
            }
            Some(token) => Err(ParseError::new(
                format!("Expected {:?}, found {:?}", expected, token.token.token_type),
                token.range(),
            )
            .with_expected(vec![format!("{:?}", expected)])
            .with_found(format!("{:?}", token.token.token_type))),
            None => Err(ParseError::new(
                format!("Expected {:?}, found end of file", expected),
                self.current_position_range(),
            )
            .with_expected(vec![format!("{:?}", expected)])
            .with_found("EOF".to_string())),
        }
    }

    /// Check if current token matches
    pub fn check(&self, token_type: &TokenType) -> bool {
        self.current_token().map(|t| &t.token.token_type == token_type).unwrap_or(false)
    }

    /// Consume token if it matches
    pub fn consume(&mut self, token_type: &TokenType) -> bool {
        if self.check(token_type) {
            self.advance();
            true
        } else {
            false
        }
    }

    /// Get source slice for a range
    pub fn source_slice(&self, range: &Range) -> &str {
        &self.source[range.start.byte..range.end.byte]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use perl_tdd_support::must_some;

    #[test]
    fn test_parser_context_creation() {
        let source = "my $x = 42;".to_string();
        let ctx = ParserContext::new(source);

        assert!(!ctx.is_eof());
        assert!(!ctx.tokens.is_empty());
    }

    #[test]
    fn test_token_advancement() {
        let source = "my $x".to_string();
        let mut ctx = ParserContext::new(source);

        // First token should be 'my'
        assert!(matches!(
            ctx.current_token().map(|t| &t.token.token_type),
            Some(TokenType::Keyword(k)) if k.as_ref() == "my"
        ));

        // Advance to next token
        ctx.advance();
        assert!(ctx.current_token().is_some());
    }

    #[test]
    fn test_error_accumulation() {
        let mut ctx = ParserContext::new("test".to_string());

        let error1 = ParseError::new("Error 1".to_string(), ctx.current_position_range());
        let error2 = ParseError::new("Error 2".to_string(), ctx.current_position_range());

        ctx.add_error(error1);
        ctx.add_error(error2);

        let errors = ctx.take_errors();
        assert_eq!(errors.len(), 2);
        assert_eq!(errors[0].message, "Error 1");
        assert_eq!(errors[1].message, "Error 2");
    }

    #[test]
    fn test_multiline_positions() {
        let source = "my $x = 42;\nmy $y = 43;".to_string();
        let ctx = ParserContext::new(source.clone());

        let first_offset = must_some(source.find("my"));
        let second_offset = must_some(source.rfind("my"));

        let first = must_some(ctx.tokens.iter().find(|t| t.range().start.byte == first_offset));
        assert_eq!(first.range().start.line, 1);
        assert_eq!(first.range().start.column, 1);
        assert_eq!(first.range().end.line, 1);
        assert_eq!(first.range().end.column, 3);

        let second = must_some(ctx.tokens.iter().find(|t| t.range().start.byte == second_offset));
        assert_eq!(second.range().start.line, 2);
        assert_eq!(second.range().start.column, 1);
        assert_eq!(second.range().end.line, 2);
        assert_eq!(second.range().end.column, 3);
    }

    #[test]
    fn test_multiline_string_token_positions() {
        let source = "my $s = \"a\nb\";".to_string();
        let ctx = ParserContext::new(source.clone());

        let string_offset = must_some(source.find('"'));
        let token = must_some(ctx.tokens.iter().find(|t| t.range().start.byte == string_offset));

        assert_eq!(token.range().start.line, 1);
        assert_eq!(token.range().start.column, 9);
        assert_eq!(token.range().end.line, 2);
        assert_eq!(token.range().end.column, 3);
    }

    #[test]
    fn test_utf16_position_mapping() {
        // Test with emoji which takes 2 UTF-16 code units
        let source = "my $emoji = 😀;".to_string();
        let ctx = ParserContext::new(source.clone());

        // Find the emoji token (if lexer produces it as separate token)
        // For now, test that positions are computed correctly for the = token
        let equals_offset = must_some(source.find('='));
        let equals_token =
            must_some(ctx.tokens.iter().find(|t| t.range().start.byte == equals_offset));

        // Before emoji: "my $emoji "  = 10 characters but the emoji counts as 2 UTF-16 units
        // So column should account for UTF-16 encoding
        assert_eq!(equals_token.range().start.line, 1);
        // The exact column depends on how the lexer tokenizes, but should be UTF-16 aware
        assert!(equals_token.range().start.column > 0);
    }

    #[test]
    fn test_crlf_line_endings() {
        let source = "my $x = 42;\r\nmy $y = 43;".to_string();
        let ctx = ParserContext::new(source.clone());

        let first_offset = must_some(source.find("my"));
        let second_offset = must_some(source.rfind("my"));

        let first = must_some(ctx.tokens.iter().find(|t| t.range().start.byte == first_offset));
        assert_eq!(first.range().start.line, 1);
        assert_eq!(first.range().start.column, 1);

        let second = must_some(ctx.tokens.iter().find(|t| t.range().start.byte == second_offset));
        assert_eq!(second.range().start.line, 2);
        assert_eq!(second.range().start.column, 1);
    }

    #[test]
    fn test_empty_source() {
        let source = "".to_string();
        let ctx = ParserContext::new(source);

        assert!(ctx.tokens.is_empty());
        assert!(ctx.is_eof());
    }

    #[test]
    fn test_single_token() {
        let source = "42".to_string();
        let ctx = ParserContext::new(source);

        assert_eq!(ctx.tokens.len(), 1);

        let token = &ctx.tokens[0];
        assert_eq!(token.range().start.byte, 0);
        assert_eq!(token.range().start.line, 1);
        assert_eq!(token.range().start.column, 1);
        assert_eq!(token.range().end.byte, 2);
        assert_eq!(token.range().end.line, 1);
        assert_eq!(token.range().end.column, 3);
    }
}