java-lang 0.3.2

A Java AST parser in Rust, syn-style API for Java 25 (JLS SE 25)
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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use std::cell::{Cell, RefCell};

use crate::{
    ast::{Comment, CommentKind},
    error::{Error, Result},
    ident::Ident,
    lexer,
    span::Span,
    token::{Token, TokenKind},
};

/// Saved parser state for speculative parsing with backtracking.
pub struct ParserState {
    cursor: usize,
    pending_gts: u8,
}

/// The parsing cursor, similar to syn's `ParseBuffer`.
///
/// This is the primary interface for consuming tokens during parsing.
pub struct ParseStream<'a> {
    tokens: &'a [Token],
    cursor: Cell<usize>,
    errors: RefCell<Vec<Error>>,
    /// Pending greater-than tokens from splitting >> or >>> for nested generics
    pending_gts: Cell<u8>,
    /// Pre-allocated synthetic `>` token for >> / >>> splitting
    synthetic_gt: Token,
    /// Comments skipped by skip_comments(), available for collection
    pending_comments: RefCell<Vec<Comment>>,
}

impl<'a> ParseStream<'a> {
    pub(crate) fn new(tokens: &'a [Token]) -> Self {
        ParseStream {
            tokens,
            cursor: Cell::new(0),
            errors: RefCell::new(Vec::new()),
            pending_gts: Cell::new(0),
            synthetic_gt: Token {
                kind: TokenKind::Gt,
                span: Span::new(0, 0),
            },
            pending_comments: RefCell::new(Vec::new()),
        }
    }

    /// Returns true if there are no more tokens to parse (except EOF).
    pub fn is_empty(&self) -> bool {
        self.skip_comments();
        let cursor = self.cursor.get();
        cursor >= self.tokens.len() - 1
    }

    /// Returns true if there are no more tokens (including comments) except EOF.
    pub fn is_empty_raw(&self) -> bool {
        let cursor = self.cursor.get();
        cursor >= self.tokens.len() - 1
    }

    /// Peek at the current token without consuming it.
    /// Comment tokens are skipped transparently.
    pub fn peek(&self) -> &Token {
        if self.pending_gts.get() > 0 {
            return &self.synthetic_gt;
        }
        self.skip_comments();
        let cursor = self.cursor.get();
        &self.tokens[cursor.min(self.tokens.len() - 1)]
    }

    /// Peek at the raw token at the cursor without skipping comments.
    pub fn peek_raw(&self) -> &Token {
        let cursor = self.cursor.get();
        &self.tokens[cursor.min(self.tokens.len() - 1)]
    }

    /// Get the current cursor position.
    pub fn cursor(&self) -> usize {
        self.cursor.get()
    }

    /// Set the current cursor position (used for backtracking).
    pub fn set_cursor(&self, pos: usize) {
        self.cursor.set(pos);
    }

    /// Save the full parser state (cursor + pending_gts) for speculative parsing.
    pub fn save_state(&self) -> ParserState {
        ParserState {
            cursor: self.cursor.get(),
            pending_gts: self.pending_gts.get(),
        }
    }

    /// Restore the full parser state (cursor + pending_gts) after speculative parsing.
    pub fn restore_state(&self, state: ParserState) {
        self.cursor.set(state.cursor);
        self.pending_gts.set(state.pending_gts);
    }

    /// Advance past the current token and return it.
    /// Comment tokens are skipped transparently.
    fn advance(&self) -> &Token {
        if self.pending_gts.get() > 0 {
            self.pending_gts.set(self.pending_gts.get() - 1);
            return &self.synthetic_gt;
        }
        let cursor = self.cursor.get();
        let tok = &self.tokens[cursor];
        if cursor < self.tokens.len() - 1 {
            self.cursor.set(cursor + 1);
        }
        self.skip_comments();
        tok
    }

    /// Push an error to the error list.
    pub fn error<T: std::fmt::Display>(&self, span: Span, msg: T) -> Error {
        let err = Error::new(span, msg);
        self.errors.borrow_mut().push(err.clone());
        err
    }

    /// Split a >> token into two > tokens for nested generic parsing.
    pub fn split_gt(&self) {
        let cursor = self.cursor.get();
        let kind = &self.tokens[cursor.min(self.tokens.len() - 1)].kind;
        match kind {
            TokenKind::GtGt => {
                self.advance();
                self.pending_gts.set(self.pending_gts.get() + 1);
            }
            TokenKind::GtGtGt => {
                self.advance();
                self.pending_gts.set(self.pending_gts.get() + 2);
            }
            _ => {}
        }
    }

    /// Consume and return the current token regardless of its kind.
    pub fn next(&self) -> &Token {
        self.advance()
    }

    /// Check if the current token matches the given kind.
    pub fn is(&self, kind: &TokenKind) -> bool {
        &self.peek().kind == kind
    }

    /// Check if the current token is an identifier with the given name.
    pub fn is_ident(&self, name: &str) -> bool {
        match &self.peek().kind {
            TokenKind::Ident(s) => s == name,
            _ => false,
        }
    }

    /// Check if the current token is any identifier (including contextual keywords).
    pub fn is_any_ident(&self) -> bool {
        matches!(
            &self.peek().kind,
            TokenKind::Ident(_)
                | TokenKind::Record
                | TokenKind::Sealed
                | TokenKind::Var
                | TokenKind::Yield
                | TokenKind::Open
                | TokenKind::Provides
                | TokenKind::Requires
                | TokenKind::Uses
                | TokenKind::With
                | TokenKind::When
                | TokenKind::To
                | TokenKind::Exports
                | TokenKind::Opens
                | TokenKind::Transitive
                | TokenKind::Permits
                | TokenKind::NonSealed
                | TokenKind::Module
                | TokenKind::Byte
                | TokenKind::Short
                | TokenKind::Int
                | TokenKind::Long
                | TokenKind::Char
                | TokenKind::Float
                | TokenKind::Double
                | TokenKind::Boolean
                | TokenKind::Void
        )
    }

    /// Check if the current token can be used as a type name.
    pub fn is_type_ident(&self) -> bool {
        self.is_any_ident() || self.is(&TokenKind::At)
    }

    /// Check if the current token is a given keyword.
    pub fn is_keyword(&self, kind: TokenKind) -> bool {
        self.peek().kind == kind
    }

    /// Consume the next token if it matches the expected kind.
    pub fn eat(&self, expected: &TokenKind) -> bool {
        if self.is(expected) {
            self.advance();
            true
        } else {
            false
        }
    }

    /// If the current token matches the given kind, consume it.
    /// Otherwise, report an error.
    pub fn expect(&self, kind: TokenKind) -> Result<()> {
        if self.is(&kind) {
            self.advance();
            Ok(())
        } else {
            Err(Error::expected_token(self.peek().span, &kind.to_string()))
        }
    }

    /// Look ahead `n` tokens (skipping comments).
    pub fn look_ahead(&self, n: usize) -> &Token {
        let mut pos = self.cursor.get();
        let mut remaining = n;
        while pos < self.tokens.len() - 1 {
            if !is_comment_token(&self.tokens[pos].kind) {
                if remaining == 0 {
                    break;
                }
                remaining -= 1;
            }
            pos += 1;
        }
        &self.tokens[pos.min(self.tokens.len() - 1)]
    }

    /// Parse a comma-separated list of items terminated by some token.
    pub fn parse_terminated<T, F>(&self, mut parse_item: F) -> Result<Vec<T>>
    where
        F: FnMut(&ParseStream) -> Result<T>,
    {
        let mut items = Vec::new();
        if self.is_empty() || !can_start_item(&self.peek().kind) {
            return Ok(items);
        }
        loop {
            items.push(parse_item(self)?);
            if !self.eat(&TokenKind::Comma) {
                break;
            }
        }
        Ok(items)
    }

    /// Parse zero or more items separated by commas, not requiring a terminator.
    pub fn parse_separated<T, F>(
        &self,
        can_start_fn: fn(&TokenKind) -> bool,
        mut parse_item: F,
    ) -> Result<Vec<T>>
    where
        F: FnMut(&ParseStream) -> Result<T>,
    {
        let mut items = Vec::new();
        if self.is_empty() || !can_start_fn(&self.peek().kind) {
            return Ok(items);
        }
        loop {
            items.push(parse_item(self)?);
            if !self.eat(&TokenKind::Comma) {
                break;
            }
            if self.is_empty() {
                break;
            }
        }
        Ok(items)
    }

    /// Try to parse something. If parsing fails, revert the cursor.
    pub fn try_parse<T, F>(&self, f: F) -> Option<T>
    where
        F: FnOnce(&ParseStream) -> Result<T>,
    {
        let saved = self.cursor.get();
        match f(self) {
            Ok(t) => Some(t),
            Err(_) => {
                self.cursor.set(saved);
                None
            }
        }
    }

    /// Parse something inside parentheses.
    pub fn parse_parenthesized<T, F>(&self, mut f: F) -> Result<T>
    where
        F: FnMut(&ParseStream) -> Result<T>,
    {
        self.expect(TokenKind::LParen)?;
        let result = f(self)?;
        self.expect(TokenKind::RParen)?;
        Ok(result)
    }

    /// Parse any type that implements `Parse`.
    pub fn parse<T: Parse>(&self) -> Result<T> {
        T::parse(self)
    }

    /// Parse something inside braces.
    pub fn parse_braced<T, F>(&self, mut f: F) -> Result<T>
    where
        F: FnMut(&ParseStream) -> Result<T>,
    {
        self.expect(TokenKind::LBrace)?;
        let result = f(self)?;
        self.expect(TokenKind::RBrace)?;
        Ok(result)
    }

    /// Parse something inside brackets.
    pub fn parse_bracketed<T, F>(&self, mut f: F) -> Result<T>
    where
        F: FnMut(&ParseStream) -> Result<T>,
    {
        self.expect(TokenKind::LBracket)?;
        let result = f(self)?;
        self.expect(TokenKind::RBracket)?;
        Ok(result)
    }

    /// Consume the expected token, then return the span of the raw token at the cursor.
    /// Unlike `expect(kind); peek().span`, this does NOT skip comments after consuming.
    pub fn expect_then_raw_span(&self, kind: TokenKind) -> Result<Span> {
        self.expect(kind)?;
        Ok(self.peek_raw().span)
    }
    /// Also accepts contextual keywords (record, sealed, var, yield, open, etc.)
    pub fn parse_ident(&self) -> Result<Ident> {
        match &self.peek().kind {
            TokenKind::Ident(s) => {
                let span = self.peek().span;
                self.advance();
                Ok(Ident::new(s.clone(), span))
            }
            TokenKind::Record
            | TokenKind::Sealed
            | TokenKind::Var
            | TokenKind::Yield
            | TokenKind::Open
            | TokenKind::Provides
            | TokenKind::Requires
            | TokenKind::Uses
            | TokenKind::With
            | TokenKind::When
            | TokenKind::To
            | TokenKind::Exports
            | TokenKind::Opens
            | TokenKind::Transitive
            | TokenKind::Permits
            | TokenKind::NonSealed
            | TokenKind::Module
            | TokenKind::Byte
            | TokenKind::Short
            | TokenKind::Int
            | TokenKind::Long
            | TokenKind::Char
            | TokenKind::Float
            | TokenKind::Double
            | TokenKind::Boolean
            | TokenKind::Void => {
                let name = format!("{}", self.peek().kind);
                let span = self.peek().span;
                self.advance();
                Ok(Ident::new(name, span))
            }
            _other => Err(Error::expected_token(self.peek().span, "identifier")),
        }
    }

    /// Take any errors accumulated during parsing.
    pub fn take_errors(&self) -> Vec<Error> {
        self.errors.borrow_mut().drain(..).collect()
    }

    /// Create a span covering from the given start to the current position.
    pub fn span_since(&self, start: Span) -> Span {
        let end = if self.cursor.get() > 0 {
            self.tokens[self.cursor.get() - 1].span
        } else {
            start
        };
        start.join(end)
    }

    /// Skip past any comment tokens at the current cursor position.
    /// This is the public version for explicit comment skipping.
    pub fn skip_comments_to_peek(&self) {
        self.skip_comments();
    }

    fn skip_comments(&self) {
        while self.cursor.get() < self.tokens.len()
            && is_comment_token(&self.tokens[self.cursor.get()].kind)
        {
            let tok = &self.tokens[self.cursor.get()];
            self.pending_comments
                .borrow_mut()
                .push(token_to_comment(tok));
            self.cursor.set(self.cursor.get() + 1);
        }
    }

    /// Collect pending doc comments (skipped by peek/advance).
    /// Returns only doc comments (/// and /** */), discards regular comments.
    pub fn collect_pending_doc_comments(&self) -> Vec<Comment> {
        let all = self
            .pending_comments
            .borrow_mut()
            .drain(..)
            .collect::<Vec<_>>();
        all.into_iter()
            .filter(|c| c.kind == CommentKind::DocLine || c.kind == CommentKind::DocBlock)
            .collect()
    }

    /// Collect all pending comments (skipped by peek/advance).
    pub fn collect_pending_comments(&self) -> Vec<Comment> {
        self.pending_comments.borrow_mut().drain(..).collect()
    }

    /// Collect and consume leading doc comments (/// and /** */).
    /// Regular comments (// and /* */) are skipped.
    pub fn collect_leading_doc_comments(&self) -> Vec<Comment> {
        let mut comments = Vec::new();
        while self.cursor.get() < self.tokens.len() {
            match &self.tokens[self.cursor.get()].kind {
                TokenKind::DocLineComment(_) | TokenKind::DocBlockComment(_) => {
                    let tok = &self.tokens[self.cursor.get()];
                    comments.push(token_to_comment(tok));
                    self.cursor.set(self.cursor.get() + 1);
                }
                TokenKind::LineComment(_) | TokenKind::BlockComment(_) => {
                    // Skip regular comments
                    self.cursor.set(self.cursor.get() + 1);
                }
                _ => break,
            }
        }
        comments
    }

    /// Collect and consume all leading comments (both doc and regular).
    pub fn collect_leading_comments(&self) -> Vec<Comment> {
        let mut comments = Vec::new();
        while self.cursor.get() < self.tokens.len() {
            match &self.tokens[self.cursor.get()].kind {
                TokenKind::LineComment(_)
                | TokenKind::BlockComment(_)
                | TokenKind::DocLineComment(_)
                | TokenKind::DocBlockComment(_) => {
                    let tok = &self.tokens[self.cursor.get()];
                    comments.push(token_to_comment(tok));
                    self.cursor.set(self.cursor.get() + 1);
                }
                _ => break,
            }
        }
        comments
    }
}

fn is_comment_token(kind: &TokenKind) -> bool {
    matches!(
        kind,
        TokenKind::LineComment(_)
            | TokenKind::BlockComment(_)
            | TokenKind::DocLineComment(_)
            | TokenKind::DocBlockComment(_)
    )
}

fn token_to_comment(tok: &Token) -> Comment {
    let kind = match &tok.kind {
        TokenKind::DocLineComment(_) => CommentKind::DocLine,
        TokenKind::DocBlockComment(_) => CommentKind::DocBlock,
        TokenKind::LineComment(_) => CommentKind::Line,
        TokenKind::BlockComment(_) => CommentKind::Block,
        _ => unreachable!(),
    };
    Comment {
        kind,
        span: tok.span,
    }
}

fn can_start_item(kind: &TokenKind) -> bool {
    !matches!(
        kind,
        TokenKind::Eof
            | TokenKind::RParen
            | TokenKind::RBrace
            | TokenKind::RBracket
            | TokenKind::Semicolon
            | TokenKind::Comma
    )
}

/// A trait for types that can be parsed from a `ParseStream`.
///
/// This is the core parsing trait, analogous to syn's `Parse` trait.
///
/// # Example
///
/// ```
/// use java_lang::{Parse, ParseStream, parse_str, Ident};
///
/// struct SimpleName {
///     name: Ident,
/// }
///
/// impl Parse for SimpleName {
///     fn parse(input: &ParseStream) -> java_lang::Result<Self> {
///         Ok(SimpleName {
///             name: input.parse_ident()?,
///         })
///     }
/// }
/// ```
pub trait Parse: Sized {
    /// Parse this type from the given `ParseStream`.
    fn parse(input: &ParseStream) -> Result<Self>;
}

impl Parse for Ident {
    fn parse(input: &ParseStream) -> Result<Self> {
        input.parse_ident()
    }
}

/// Parse a string into a value of type `T` that implements `Parse`.
pub fn parse_str<T: Parse>(s: &str) -> Result<T> {
    let tokens = lexer::tokenize(s);
    let stream = ParseStream::new(&tokens);
    let result = T::parse(&stream)?;
    // Check for trailing tokens
    if !stream.is_empty() {
        return Err(Error::new(stream.peek().span, "unexpected trailing tokens"));
    }
    Ok(result)
}

/// Parse a string into a value of type `T` without checking for trailing tokens.
pub fn parse<T: Parse>(s: &str) -> Result<T> {
    let tokens = lexer::tokenize(s);
    let stream = ParseStream::new(&tokens);
    T::parse(&stream)
}

/// Parse a Java source file into a value of type `T`.
pub fn parse_file<T: Parse>(path: &std::path::Path) -> Result<T> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| Error::new(Span::call_site(), format!("failed to read file: {}", e)))?;
    parse_str(&content)
}

/// Peek at the next token and check if it matches.
#[macro_export]
macro_rules! peek {
    ($stream:expr, $kind:ident) => {
        $stream.is(&$crate::token::TokenKind::$kind)
    };
}

/// Peek at the next token and check if it is an identifier.
#[macro_export]
macro_rules! peek_ident {
    ($stream:expr) => {
        $stream.is_any_ident()
    };
}

/// Optionally parse something. Returns `None` if the next token doesn't match.
#[macro_export]
macro_rules! opt {
    ($stream:expr, $method:ident $(, $arg:expr)*) => {
        if $stream.is_empty() {
            None
        } else {
            match $stream.$method($($arg),*) {
                Ok(val) => Some(val),
                Err(_) => None,
            }
        }
    };
}