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
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
//! Trivia-preserving parser implementation
//!
//! This module provides a parser that preserves comments and whitespace
//! by attaching them to AST nodes as leading/trailing trivia.

use crate::trivia::{NodeWithTrivia, Trivia, TriviaToken};
use perl_ast_v2::{Node, NodeIdGenerator, NodeKind};
use perl_lexer::{PerlLexer, Token, TokenType};
use perl_position_tracking::{Position, Range};
use std::collections::VecDeque;

/// Token with trivia information
#[derive(Debug, Clone)]
pub(crate) struct TokenWithTrivia {
    /// The actual token
    token: Token,
    /// Leading trivia (comments/whitespace before this token)
    leading_trivia: Vec<TriviaToken>,
    /// Token range
    range: Range,
}

/// Parser context that preserves trivia
pub struct TriviaParserContext {
    /// Source text
    _source: String,
    /// Tokens with trivia
    tokens: VecDeque<TokenWithTrivia>,
    /// Current token index
    current: usize,
    /// Node ID generator
    id_generator: NodeIdGenerator,
    /// Position tracker for accurate line/column info
    position_tracker: PositionTracker,
}

/// Tracks position in source for accurate line/column information
struct PositionTracker {
    /// Line start offsets
    line_starts: Vec<usize>,
}

impl PositionTracker {
    fn new(source: &str) -> Self {
        let mut line_starts = vec![0];
        for (i, ch) in source.char_indices() {
            if ch == '\n' {
                line_starts.push(i + 1);
            }
        }
        PositionTracker { line_starts }
    }

    fn offset_to_position(&self, offset: usize) -> Position {
        let line = self.line_starts.binary_search(&offset).unwrap_or_else(|i| i.saturating_sub(1));
        let line_start = self.line_starts[line];
        let column = offset - line_start + 1;
        Position::new(offset, (line + 1) as u32, column as u32)
    }
}

impl TriviaParserContext {
    /// Create a new trivia-preserving parser context
    pub fn new(source: String) -> Self {
        let position_tracker = PositionTracker::new(&source);
        let mut tokens = VecDeque::new();

        // Custom tokenization that preserves trivia
        let mut position = 0;
        let _source_bytes = source.as_bytes();

        while position < source.len() {
            // Collect leading trivia
            let _trivia_start = position;
            let leading_trivia = Self::collect_trivia_at(&source, &mut position);

            if position >= source.len() {
                break;
            }

            // Get next meaningful token using the lexer
            let token_source = &source[position..];
            let mut lexer = PerlLexer::new(token_source);

            if let Some(token) = lexer.next_token() {
                // Skip EOF tokens
                if matches!(token.token_type, TokenType::EOF) {
                    break;
                }

                // Adjust token positions to be relative to the full source
                let adjusted_token = Token::new(
                    token.token_type.clone(),
                    token.text.clone(),
                    position + token.start,
                    position + token.end,
                );

                // Create range with proper line/column info
                let start_pos = position_tracker.offset_to_position(adjusted_token.start);
                let end_pos = position_tracker.offset_to_position(adjusted_token.end);
                let range = Range::new(start_pos, end_pos);

                tokens.push_back(TokenWithTrivia {
                    token: adjusted_token.clone(),
                    leading_trivia,
                    range,
                });

                // Advance position
                position = adjusted_token.end;
            } else {
                break;
            }
        }

        // Handle remaining trivia at EOF, or source that was entirely trivia
        if tokens.is_empty() || position < source.len() {
            let remaining_trivia = if position < source.len() {
                Self::collect_trivia_at(&source, &mut position)
            } else {
                Vec::new()
            };
            if !remaining_trivia.is_empty() || tokens.is_empty() {
                let trivia = if tokens.is_empty() {
                    // Source was entirely trivia — re-collect from start
                    let mut pos = 0;
                    Self::collect_trivia_at(&source, &mut pos)
                } else {
                    remaining_trivia
                };
                if !trivia.is_empty() {
                    let eof_pos = position_tracker.offset_to_position(source.len());
                    let eof_token =
                        Token::new(TokenType::EOF, String::new(), source.len(), source.len());
                    tokens.push_back(TokenWithTrivia {
                        token: eof_token,
                        leading_trivia: trivia,
                        range: Range::new(eof_pos, eof_pos),
                    });
                }
            }
        }

        TriviaParserContext {
            _source: source,
            tokens,
            current: 0,
            id_generator: NodeIdGenerator::new(),
            position_tracker,
        }
    }

    /// Collect trivia at the given position
    fn collect_trivia_at(source: &str, position: &mut usize) -> Vec<TriviaToken> {
        let mut trivia = Vec::new();
        let bytes = source.as_bytes();

        while *position < source.len() {
            let _start = *position;
            let ch = bytes[*position];

            match ch {
                // Whitespace
                b' ' | b'\t' | b'\r' => {
                    let ws_start = *position;
                    while *position < source.len()
                        && matches!(bytes[*position], b' ' | b'\t' | b'\r')
                    {
                        *position += 1;
                    }

                    let ws = &source[ws_start..*position];
                    trivia.push(TriviaToken::new(
                        Trivia::Whitespace(ws.to_string()),
                        Range::new(Position::new(ws_start, 0, 0), Position::new(*position, 0, 0)),
                    ));
                }

                // Newline
                b'\n' => {
                    trivia.push(TriviaToken::new(
                        Trivia::Newline,
                        Range::new(
                            Position::new(*position, 0, 0),
                            Position::new(*position + 1, 0, 0),
                        ),
                    ));
                    *position += 1;
                }

                // Comment
                b'#' => {
                    let comment_start = *position;
                    // Find end of line
                    while *position < source.len() && bytes[*position] != b'\n' {
                        *position += 1;
                    }

                    let comment = &source[comment_start..*position];
                    trivia.push(TriviaToken::new(
                        Trivia::LineComment(comment.to_string()),
                        Range::new(
                            Position::new(comment_start, 0, 0),
                            Position::new(*position, 0, 0),
                        ),
                    ));
                }

                // POD documentation
                b'=' if *position == 0 || (*position > 0 && bytes[*position - 1] == b'\n') => {
                    // Check if this starts a POD section
                    let remaining = &source[*position..];
                    if remaining.starts_with("=pod")
                        || remaining.starts_with("=head")
                        || remaining.starts_with("=over")
                        || remaining.starts_with("=item")
                        || remaining.starts_with("=back")
                        || remaining.starts_with("=begin")
                        || remaining.starts_with("=end")
                        || remaining.starts_with("=for")
                        || remaining.starts_with("=encoding")
                    {
                        let pod_start = *position;

                        // Edge case fix: Find =cut at start of line (including position 0 or after newline)
                        let mut found_cut = false;
                        while *position < source.len() {
                            // Check for =cut at the start of a line
                            if (*position == 0 || (*position > 0 && bytes[*position - 1] == b'\n'))
                                && source[*position..].starts_with("=cut")
                            {
                                *position += 4; // Skip "=cut"
                                // Skip to end of line
                                while *position < source.len() && bytes[*position] != b'\n' {
                                    *position += 1;
                                }
                                if *position < source.len() {
                                    *position += 1; // Skip newline
                                }
                                found_cut = true;
                                break;
                            }
                            *position += 1;
                        }

                        // Edge case fix: If no =cut found, POD extends to end of file
                        if !found_cut {
                            *position = source.len();
                        }

                        let pod = &source[pod_start..*position];
                        trivia.push(TriviaToken::new(
                            Trivia::PodComment(pod.to_string()),
                            Range::new(
                                Position::new(pod_start, 0, 0),
                                Position::new(*position, 0, 0),
                            ),
                        ));
                    } else {
                        // Not POD, this is a regular token
                        break;
                    }
                }

                // Non-trivia character
                _ => {
                    // Check for Unicode whitespace
                    if ch >= 128 {
                        let ch_str = &source[*position..];
                        if let Some(unicode_ch) = ch_str.chars().next() {
                            if unicode_ch.is_whitespace() {
                                let ch_len = unicode_ch.len_utf8();
                                trivia.push(TriviaToken::new(
                                    Trivia::Whitespace(unicode_ch.to_string()),
                                    Range::new(
                                        Position::new(*position, 0, 0),
                                        Position::new(*position + ch_len, 0, 0),
                                    ),
                                ));
                                *position += ch_len;
                                continue;
                            }
                        }
                    }

                    // Not trivia, stop collecting
                    break;
                }
            }
        }

        trivia
    }

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

    /// Advance to next token
    pub(crate) fn advance(&mut self) -> Option<&TokenWithTrivia> {
        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()
    }
}

/// Parser that preserves trivia
pub struct TriviaPreservingParser {
    context: TriviaParserContext,
}

impl TriviaPreservingParser {
    /// Create a new trivia-preserving parser
    pub fn new(source: String) -> Self {
        TriviaPreservingParser { context: TriviaParserContext::new(source) }
    }

    /// Parse the source, preserving trivia
    pub fn parse(mut self) -> NodeWithTrivia {
        let start_pos = Position::new(0, 1, 1);
        let mut statement_nodes = Vec::new();

        // Collect all trivia from all tokens (including EOF) so that
        // blank lines between statements and inline POD are surfaced.
        let mut leading_trivia = Vec::new();
        for token in &self.context.tokens {
            leading_trivia.extend(token.leading_trivia.iter().cloned());
        }

        // Parse statements
        while !self.context.is_eof() {
            if let Some(stmt) = self.parse_statement() {
                statement_nodes.push(stmt.node);
            }
        }

        let end_pos = if let Some(last_token) = self.context.tokens.back() {
            last_token.range.end
        } else {
            start_pos
        };

        let program = Node::new(
            self.context.id_generator.next_id(),
            NodeKind::Program { statements: statement_nodes },
            Range::new(start_pos, end_pos),
        );

        NodeWithTrivia { node: program, leading_trivia, trailing_trivia: Vec::new() }
    }

    /// Parse a statement with trivia
    fn parse_statement(&mut self) -> Option<NodeWithTrivia> {
        let (token, leading_trivia, _token_range) = {
            let token_with_trivia = self.context.current_token()?;
            (
                token_with_trivia.token.clone(),
                token_with_trivia.leading_trivia.clone(),
                token_with_trivia.range,
            )
        };

        // Simple demonstration: parse variable declarations
        match &token.token_type {
            TokenType::Keyword(kw)
                if matches!(kw.as_ref(), "my" | "our" | "local" | "state" | "field") =>
            {
                let start_pos = self.context.position_tracker.offset_to_position(token.start);

                let declarator = kw.to_string();
                self.context.advance();

                // For demonstration, create a simple node
                let end_pos = self.context.position_tracker.offset_to_position(token.end);

                let node = Node::new(
                    self.context.id_generator.next_id(),
                    NodeKind::Identifier { name: declarator },
                    Range::new(start_pos, end_pos),
                );

                // Skip to next statement for demo
                while !self.context.is_eof() {
                    if let Some(t) = self.context.current_token() {
                        if matches!(t.token.token_type, TokenType::Semicolon) {
                            self.context.advance();
                            break;
                        }
                    }
                    self.context.advance();
                }

                Some(NodeWithTrivia { node, leading_trivia, trailing_trivia: Vec::new() })
            }
            _ => {
                // Skip unknown tokens for now
                self.context.advance();
                None
            }
        }
    }
}

/// Format an AST with trivia back to source code
pub fn format_with_trivia(node: &NodeWithTrivia) -> String {
    let mut result = String::new();

    // Add leading trivia
    for trivia in &node.leading_trivia {
        result.push_str(trivia.trivia.as_str());
    }

    // Add node content (simplified)
    result.push_str(&format!("{:?}", node.node.kind));

    // Add trailing trivia
    for trivia in &node.trailing_trivia {
        result.push_str(trivia.trivia.as_str());
    }

    result
}

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

    #[test]
    fn test_trivia_preservation() {
        let source = r#"#!/usr/bin/perl
# This is a comment
  
my $x = 42;  # end of line comment

=pod
This is POD documentation
=cut

our $y;"#
            .to_string();

        let parser = TriviaPreservingParser::new(source);
        let result = parser.parse();

        // Check that we have leading trivia
        assert!(!result.leading_trivia.is_empty());

        // First trivia should be the shebang comment
        assert!(matches!(
            &result.leading_trivia[0].trivia,
            Trivia::LineComment(s) if s.starts_with("#!/usr/bin/perl")
        ));
    }

    #[test]
    fn test_whitespace_preservation() {
        let source = "  \t  my $x;".to_string();
        let ctx = TriviaParserContext::new(source);

        let first_token = must_some(ctx.current_token());
        assert!(!first_token.leading_trivia.is_empty());
        assert!(matches!(
            &first_token.leading_trivia[0].trivia,
            Trivia::Whitespace(ws) if ws == "  \t  "
        ));
    }
}