synkit 0.0.2

A fast, syn-like incremental parser framework for Rust
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
//! End-to-end test demonstrating the full logos-parser-kit workflow.
//!
//! This test defines a simple grammar for parsing Rust-like struct definitions
//! and validates lexing, parsing, span tracking, error handling, and printing.

use thiserror::Error;

// Define error type first (needed by parser_kit macro)
#[derive(Error, Debug, Clone, Default, PartialEq)]
pub enum LexError {
    #[default]
    #[error("unknown lexing error")]
    Unknown,

    #[error("expected {expect}, found {found}")]
    Expected { expect: &'static str, found: String },

    #[error("expected {expect}, found EOF")]
    Empty { expect: &'static str },

    #[error("{source}")]
    Spanned {
        #[source]
        source: Box<LexError>,
        span: Span,
    },
}

// Generate the parser infrastructure
synkit::parser_kit! {
    error: LexError,

    skip_tokens: [Space, Tab, Newline],

    tokens: {
        #[token(" ", priority = 0)]
        Space,

        #[token("\t", priority = 0)]
        Tab,

        #[regex(r"\r?\n")]
        #[fmt("newline")]
        Newline,

        #[token("{")]
        LBrace,

        #[token("}")]
        RBrace,

        #[token("(")]
        LParen,

        #[token(")")]
        RParen,

        #[token("[")]
        LBracket,

        #[token("]")]
        RBracket,

        #[token(":")]
        Colon,

        #[token(",")]
        Comma,

        #[token(";")]
        Semi,

        #[token("struct")]
        KwStruct,

        #[token("enum")]
        KwEnum,

        #[token("type")]
        KwType,

        #[regex(r"[A-Za-z_][A-Za-z0-9_]*", |lex| lex.slice().to_string())]
        #[fmt("identifier")]
        #[derive(PartialOrd, Ord, Hash, Eq)]
        Ident(String),

        #[regex(r"[0-9]+", |lex| lex.slice().parse().ok())]
        #[fmt("number")]
        Number(i64),

        #[regex(r#""([^"\\]|\\.)*""#, |lex| {
            let s = lex.slice();
            s[1..s.len()-1].to_string()
        })]
        #[fmt("string")]
        String(String),
    },

    delimiters: {
        Brace => (LBrace, RBrace),
        Paren => (LParen, RParen),
        Bracket => (LBracket, RBracket),
    },

    span_derives: [Debug, Clone, PartialEq, Eq, Hash],
    token_derives: [Clone, PartialEq, Debug],
}

// Now we can use the generated types
impl LexError {
    pub fn expected<D: Diagnostic>(found: &Token) -> Self {
        Self::Expected {
            expect: D::fmt(),
            found: format!("{}", found),
        }
    }

    pub fn empty<D: Diagnostic>() -> Self {
        Self::Empty { expect: D::fmt() }
    }
}

impl synkit::SpannedError for LexError {
    type Span = Span;

    fn with_span(self, span: Span) -> Self {
        Self::Spanned {
            source: Box::new(self),
            span,
        }
    }

    fn span(&self) -> Option<&Span> {
        match self {
            Self::Spanned { span, .. } => Some(span),
            _ => None,
        }
    }
}

// Parse implementations for token structs are now auto-generated by parser_kit!

// AST node definitions
#[derive(Debug, Clone)]
pub struct StructField {
    pub name: Spanned<tokens::IdentToken>,
    pub colon: Spanned<tokens::ColonToken>,
    pub ty: Spanned<tokens::IdentToken>,
}

impl Peek for StructField {
    fn is(token: &Token) -> bool {
        tokens::IdentToken::is(token)
    }
}

impl Parse for StructField {
    fn parse(stream: &mut TokenStream) -> Result<Self, LexError> {
        Ok(Self {
            name: stream.parse()?,
            colon: stream.parse()?,
            ty: stream.parse()?,
        })
    }
}

impl ToTokens for StructField {
    fn write(&self, p: &mut Printer) {
        use synkit::Printer as _;
        p.token(&self.name.value.token());
        p.token(&self.colon.value.token());
        p.space();
        p.token(&self.ty.value.token());
    }
}

#[derive(Debug, Clone)]
pub struct StructDef {
    pub kw_struct: Spanned<tokens::KwStructToken>,
    pub name: Spanned<tokens::IdentToken>,
    pub lbrace: Spanned<tokens::LBraceToken>,
    pub fields: Vec<Spanned<StructField>>,
    pub rbrace: Spanned<tokens::RBraceToken>,
}

impl Peek for StructDef {
    fn is(token: &Token) -> bool {
        tokens::KwStructToken::is(token)
    }
}

impl Parse for StructDef {
    fn parse(stream: &mut TokenStream) -> Result<Self, LexError> {
        let kw_struct = stream.parse()?;
        let name = stream.parse()?;
        let lbrace = stream.parse()?;

        let mut fields = Vec::new();
        while stream.peek::<StructField>() {
            fields.push(stream.parse()?);
            if stream.peek::<tokens::CommaToken>() {
                let _comma: Spanned<tokens::CommaToken> = stream.parse()?;
            }
        }

        let rbrace = stream.parse()?;
        Ok(Self {
            kw_struct,
            name,
            lbrace,
            fields,
            rbrace,
        })
    }
}

impl ToTokens for StructDef {
    fn write(&self, p: &mut Printer) {
        use synkit::Printer as _;
        p.token(&self.kw_struct.value.token());
        p.space();
        p.token(&self.name.value.token());
        p.space();
        p.token(&self.lbrace.value.token());
        p.indent();

        for (i, field) in self.fields.iter().enumerate() {
            p.newline();
            field.value.write(p);
            if i < self.fields.len() - 1 {
                p.word(",");
            }
        }

        p.dedent();
        p.newline();
        p.token(&self.rbrace.value.token());
    }
}

/// Visitor trait for traversing the AST
pub trait AstVisitor {
    fn visit_struct_def(&mut self, node: &StructDef) {
        self.walk_struct_def(node);
    }

    fn visit_struct_field(&mut self, node: &StructField) {
        self.walk_struct_field(node);
    }

    fn visit_ident(&mut self, _node: &tokens::IdentToken) {}

    fn walk_struct_def(&mut self, node: &StructDef) {
        self.visit_ident(&node.name.value);
        for field in &node.fields {
            self.visit_struct_field(&field.value);
        }
    }

    fn walk_struct_field(&mut self, node: &StructField) {
        self.visit_ident(&node.name.value);
        self.visit_ident(&node.ty.value);
    }
}

/// Example visitor that collects all identifiers
struct IdentCollector {
    idents: Vec<String>,
}

impl AstVisitor for IdentCollector {
    fn visit_ident(&mut self, node: &tokens::IdentToken) {
        self.idents.push(node.0.clone());
    }
}

/// Example visitor that counts fields
struct FieldCounter {
    count: usize,
}

impl AstVisitor for FieldCounter {
    fn visit_struct_field(&mut self, node: &StructField) {
        self.count += 1;
        self.walk_struct_field(node);
    }
}

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

    #[test]
    fn test_lexer_basic_tokens() {
        let source = "struct Foo { }";
        let ts = stream::TokenStream::lex(source).expect("lexing failed");

        let all = ts.all();
        assert!(!all.is_empty());

        // Filter out whitespace for this test
        let kinds: Vec<_> = all
            .iter()
            .filter(|t| !matches!(&t.value, Token::Space | Token::Tab | Token::Newline))
            .map(|t| &t.value)
            .collect();
        assert!(matches!(kinds[0], Token::KwStruct));
        assert!(matches!(kinds[1], Token::Ident(s) if s == "Foo"));
        assert!(matches!(kinds[2], Token::LBrace));
        assert!(matches!(kinds[3], Token::RBrace));
    }

    #[test]
    fn test_lexer_with_whitespace() {
        let source = "struct   Foo\n{\n  x: i32\n}";
        let ts = stream::TokenStream::lex(source).expect("lexing failed");

        let all = ts.all();
        let kinds: Vec<_> = all
            .iter()
            .map(|t| &t.value)
            .filter(|t| !matches!(t, Token::Space | Token::Tab | Token::Newline))
            .collect();

        assert!(matches!(kinds[0], Token::KwStruct));
        assert!(matches!(kinds[1], Token::Ident(s) if s == "Foo"));
        assert!(matches!(kinds[2], Token::LBrace));
        assert!(matches!(kinds[3], Token::Ident(s) if s == "x"));
        assert!(matches!(kinds[4], Token::Colon));
        assert!(matches!(kinds[5], Token::Ident(s) if s == "i32"));
        assert!(matches!(kinds[6], Token::RBrace));
    }

    #[test]
    fn test_token_stream_skips_whitespace() {
        use synkit::TokenStream as _;

        let source = "struct   Foo";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");

        let tok1 = ts.next().expect("expected token");
        assert!(matches!(tok1.value, Token::KwStruct));

        let tok2 = ts.next().expect("expected token");
        assert!(matches!(tok2.value, Token::Ident(s) if s == "Foo"));
    }

    #[test]
    fn test_parse_struct() {
        let source = r#"struct Point {
            x: i32,
            y: i32
        }"#;

        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        assert_eq!(*parsed.value.name.value, "Point");
        assert_eq!(parsed.value.fields.len(), 2);
        assert_eq!(*parsed.value.fields[0].value.name.value, "x");
        assert_eq!(*parsed.value.fields[0].value.ty.value, "i32");
        assert_eq!(*parsed.value.fields[1].value.name.value, "y");
        assert_eq!(*parsed.value.fields[1].value.ty.value, "i32");
    }

    #[test]
    fn test_span_tracking() {
        use synkit::SpanLike;

        let source = "struct Foo { }";
        let ts = stream::TokenStream::lex(source).expect("lexing failed");

        let all = ts.all();
        // First token is "struct" at 0..6
        assert_eq!(all[0].span.start(), 0);
        assert_eq!(all[0].span.end(), 6);

        // Second token is " " (space) at 6..7
        // Third token is "Foo" at 7..10
        assert_eq!(all[2].span.start(), 7);
        assert_eq!(all[2].span.end(), 10);
    }

    #[test]
    fn test_error_with_span() {
        let source = "struct 123";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");

        let _kw: Spanned<tokens::KwStructToken> = ts.parse().expect("parse struct kw");
        let err: Result<Spanned<tokens::IdentToken>, _> = ts.parse();

        assert!(err.is_err());
    }

    #[test]
    fn test_peek_without_consume() {
        let source = "struct Foo";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");

        assert!(ts.peek::<tokens::KwStructToken>());
        assert!(ts.peek::<tokens::KwStructToken>());

        let _: Spanned<tokens::KwStructToken> = ts.parse().unwrap();

        assert!(ts.peek::<tokens::IdentToken>());
        assert!(!ts.peek::<tokens::KwStructToken>());
    }

    #[test]
    fn test_fork_and_rewind() {
        use synkit::TokenStream as _;

        let source = "struct Foo { }";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");

        let pos = ts.cursor();
        let _: Spanned<tokens::KwStructToken> = ts.parse().unwrap();
        let _: Spanned<tokens::IdentToken> = ts.parse().unwrap();

        ts.rewind(pos);

        let kw: Spanned<tokens::KwStructToken> = ts.parse().unwrap();
        assert_eq!(kw.value.token(), Token::KwStruct);
    }

    #[test]
    fn test_diagnostic_fmt() {
        assert_eq!(tokens::IdentToken::fmt(), "identifier");
        assert_eq!(tokens::NumberToken::fmt(), "number");
        assert_eq!(tokens::KwStructToken::fmt(), "struct");
    }

    #[test]
    fn test_token_display() {
        assert_eq!(format!("{}", Token::KwStruct), "struct");
        assert_eq!(format!("{}", Token::LBrace), "{");
        assert_eq!(format!("{}", Token::Ident("foo".to_string())), "foo");
        assert_eq!(format!("{}", Token::Number(42)), "42");
    }

    #[test]
    fn test_printer_basic() {
        use synkit::Printer as _;

        let mut printer = printer::Printer::new();
        printer.token(&Token::KwStruct);
        printer.space();
        printer.token(&Token::Ident("Foo".to_string()));

        assert_eq!(printer.into_string(), "struct Foo");
    }

    #[test]
    fn test_printer_indentation() {
        use synkit::Printer as _;

        let mut printer = printer::Printer::new();
        printer.token(&Token::LBrace);
        printer.indent();
        printer.newline();
        printer.word("content");
        printer.dedent();
        printer.newline();
        printer.token(&Token::RBrace);

        let output = printer.into_string();
        assert!(output.contains("    content"));
    }

    #[test]
    fn test_numbers_and_strings() {
        let source = r#"123 "hello world""#;
        let ts = stream::TokenStream::lex(source).expect("lexing failed");

        let all = ts.all();
        assert!(matches!(&all[0].value, Token::Number(123)));
        assert!(matches!(&all[2].value, Token::String(s) if s == "hello world"));
    }

    #[test]
    fn test_empty_struct() {
        let source = "struct Empty { }";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        assert_eq!(*parsed.value.name.value, "Empty");
        assert!(parsed.value.fields.is_empty());
    }

    #[test_case::test_case("struct A { x: T }", "A", &["x"]; "single field")]
    #[test_case::test_case("struct B { a: X, b: Y, c: Z }", "B", &["a", "b", "c"]; "multiple fields")]
    #[test_case::test_case("struct C { }", "C", &[]; "empty struct")]
    fn test_struct_parsing_variants(source: &str, expected_name: &str, expected_fields: &[&str]) {
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        assert_eq!(&**parsed.value.name.value, expected_name);
        assert_eq!(parsed.value.fields.len(), expected_fields.len());

        for (field, expected) in parsed.value.fields.iter().zip(expected_fields.iter()) {
            assert_eq!(&**field.value.name.value, *expected);
        }
    }

    // ========================================
    // Visitor pattern tests
    // ========================================

    #[test]
    fn test_visitor_collect_idents() {
        let source = "struct Point { x: i32, y: f64 }";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        let mut collector = IdentCollector { idents: Vec::new() };
        collector.visit_struct_def(&parsed.value);

        // Should collect: Point, x, i32, y, f64
        assert_eq!(collector.idents, vec!["Point", "x", "i32", "y", "f64"]);
    }

    #[test]
    fn test_visitor_count_fields() {
        let source = "struct Data { a: A, b: B, c: C, d: D }";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        let mut counter = FieldCounter { count: 0 };
        counter.visit_struct_def(&parsed.value);

        assert_eq!(counter.count, 4);
    }

    #[test]
    fn test_to_tokens_struct_field() {
        let field = StructField {
            name: Spanned::new(0, 1, tokens::IdentToken::new("x".to_string())),
            colon: Spanned::new(1, 2, tokens::ColonToken::new()),
            ty: Spanned::new(3, 6, tokens::IdentToken::new("i32".to_string())),
        };

        let output = field.to_string_formatted();
        assert_eq!(output, "x: i32");
    }

    #[test]
    fn test_to_tokens_struct_def() {
        let source = "struct Point { x: i32, y: i32 }";
        let mut ts = stream::TokenStream::lex(source).expect("lexing failed");
        let parsed: Spanned<StructDef> = ts.parse().expect("parsing failed");

        let output = parsed.value.to_string_formatted();

        // Should produce formatted output with proper indentation
        assert!(output.contains("struct"));
        assert!(output.contains("Point"));
        assert!(output.contains("x: i32"));
        assert!(output.contains("y: i32"));
    }
}