tokit 0.0.0

Blazing fast parser combinators: parse-while-lexing (zero-copy), deterministic LALR-style parsing, no backtracking. Flexible emitters for fail-fast runtime or greedy compiler diagnostics
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
//! Concrete Syntax Tree (CST) utilities built on top of [rowan](https://docs.rs/rowan).
//!
//! This module provides infrastructure for building and working with typed concrete syntax trees.
//! Unlike Abstract Syntax Trees (ASTs), CSTs preserve all source information including whitespace,
//! comments, and exact token positions, making them ideal for:
//!
//! - **Code formatters**: Preserve exact formatting and whitespace
//! - **Linters**: Access complete source information for analysis
//! - **Language servers**: Provide accurate position information for IDE features
//! - **Refactoring tools**: Transform code while preserving formatting
//! - **Documentation generators**: Extract and preserve comments
//!
//! # Architecture
//!
//! The CST infrastructure has several key components:
//!
//! 1. **[`SyntaxTreeBuilder`](crate::cst::SyntaxTreeBuilder)**: Constructs CSTs from tokens using rowan's green tree builder
//! 2. **[`Parseable`](crate::cst::Parseable)**: Trait for types that can produce CST parsers
//! 3. **[`CstElement`](crate::cst::CstElement)**: Base trait for all typed CST elements (nodes and tokens)
//! 4. **[`CstNode`](crate::cst::CstNode)**: Trait for typed CST nodes with zero-cost conversions
//! 5. **[`CstToken`](crate::cst::CstToken)**: Trait for typed CST tokens (terminal elements)
//! 6. **[`cast`](crate::cst::cast)**: Utility functions for working with CST nodes and tokens
//! 7. **[`error`](crate::cst::error)**: Error types for CST operations
//!
//! # Design Philosophy
//!
//! - **Zero-cost abstractions**: Typed CST nodes are just pointers, no runtime overhead
//! - **Lossless**: All source information is preserved in the tree
//! - **Immutable**: Trees are immutable by default (use `clone_for_update()` for mutations)
//! - **Type-safe**: Compile-time guarantees about node types and relationships
//!
//! # Basic Usage
//!
//! ```rust,ignore
//! use tokit::cst::{SyntaxTreeBuilder, Node, Parseable};
//! use rowan::Language;
//!
//! // 1. Define your language
//! #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
//! enum SyntaxKind {
//!     Root,
//!     Identifier,
//!     Number,
//!     // ... other kinds
//! }
//!
//! #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
//! struct MyLanguage;
//!
//! impl Language for MyLanguage {
//!     type Kind = SyntaxKind;
//!
//!     fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
//!         // Convert from rowan's raw kind
//!         todo!()
//!     }
//!
//!     fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
//!         // Convert to rowan's raw kind
//!         todo!()
//!     }
//! }
//!
//! // 2. Create a builder
//! let builder = SyntaxTreeBuilder::<MyLanguage>::new();
//!
//! // 3. Build the tree (usually done by a parser)
//! builder.start_node(SyntaxKind::Root);
//! builder.token(SyntaxKind::Identifier, "foo");
//! builder.finish_node();
//!
//! let green = builder.finish();
//! let root = SyntaxNode::new_root(green);
//! ```
//!
//! # Integration with Parsers
//!
//! The [`Parseable`](crate::cst::Parseable) trait integrates CST building with Chumsky parsers:
//!
//! ```rust,ignore
//! use tokit::cst::{Parseable, SyntaxTreeBuilder};
//!
//! struct Expression;
//!
//! impl<'a, I, T, Error> Parseable<'a, I, T, Error> for Expression
//! where
//!     I: Tokenizer<'a, T>,
//!     T: TriviaToken<'a>,
//! {
//!     type Language = MyLanguage;
//!
//!     fn parser<E>(
//!         builder: &'a SyntaxTreeBuilder<Self::Language>,
//!     ) -> impl chumsky::Parser<'a, I, (), E> + Clone
//!     where
//!         E: chumsky::extra::ParserExtra<'a, I, Error = Error>,
//!     {
//!         // Return a parser that builds CST nodes
//!         todo!()
//!     }
//! }
//! ```
//!
//! # Working with Typed Nodes
//!
//! The [`Node`](crate::cst::Node) trait provides zero-cost typed wrappers around syntax nodes:
//!
//! ```rust,ignore
//! use tokit::cst::Node;
//!
//! #[derive(Debug)]
//! struct IdentifierNode {
//!     syntax: SyntaxNode<MyLanguage>,
//! }
//!
//! impl Node for IdentifierNode {
//!     type Language = MyLanguage;
//!     const KIND: SyntaxKind = SyntaxKind::Identifier;
//!
//!     fn can_cast(kind: SyntaxKind) -> bool {
//!         kind == Self::KIND
//!     }
//!
//!     fn try_cast_node(syntax: SyntaxNode<Self::Language>) -> Result<Self, error::CstNodeMismatch<Self>> {
//!         if Self::can_cast(syntax.kind()) {
//!             Ok(Self { syntax })
//!         } else {
//!             Err(error::CstNodeMismatch::new(Self::KIND, syntax))
//!         }
//!     }
//!
//!     fn syntax(&self) -> &SyntaxNode<Self::Language> {
//!         &self.syntax
//!     }
//! }
//! ```
//!
//! # See Also
//!
//! - [rowan documentation](https://docs.rs/rowan) - The underlying CST library
//! - [`TriviaToken`](crate::TriviaToken) - For handling trivia in CSTs

use core::{cell::RefCell, marker::PhantomData};

use derive_more::{From, Into};
use rowan::{GreenNodeBuilder, Language, SyntaxNode, SyntaxToken};

use crate::syntax::Syntax;

/// A builder for constructing concrete syntax trees.
///
/// `SyntaxTreeBuilder` wraps rowan's [`GreenNodeBuilder`] and provides a convenient
/// interface for building syntax trees from tokens during parsing. The builder uses
/// interior mutability ([`RefCell`]) to allow sharing across parser combinators.
///
/// # Type Parameters
///
/// - `Lang`: The [`Language`] type that defines the syntax kinds for your language
///
/// # Usage Pattern
///
/// The typical usage pattern is:
///
/// 1. Create a builder with [`new()`](Self::new)
/// 2. Pass it to your parser implementation
/// 3. The parser calls [`start_node()`](Self::start_node), [`token()`](Self::token),
///    and [`finish_node()`](Self::finish_node) to build the tree
/// 4. Call [`finish()`](Self::finish) to get the final [`rowan::GreenNode`]
///
/// # Examples
///
/// ```rust,ignore
/// use tokit::cst::SyntaxTreeBuilder;
///
/// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
///
/// // Build a simple tree: Root(Identifier("hello"))
/// builder.start_node(SyntaxKind::Root);
/// builder.token(SyntaxKind::Identifier, "hello");
/// builder.finish_node();
///
/// let green_node = builder.finish();
/// ```
///
/// ## Using Checkpoints for Lookahead
///
/// Checkpoints allow you to start nodes retroactively, which is useful for
/// handling left-recursive or ambiguous grammars:
///
/// ```rust,ignore
/// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
/// let checkpoint = builder.checkpoint();
///
/// builder.token(SyntaxKind::Number, "42");
///
/// // Decide to wrap the number in a UnaryExpression
/// builder.start_node_at(checkpoint, SyntaxKind::UnaryExpression);
/// builder.token(SyntaxKind::Plus, "+");
/// builder.finish_node();
/// ```
///
/// # Interior Mutability
///
/// The builder uses [`RefCell`] internally, which means:
/// - It can be shared immutably across parser combinators
/// - Mutations are checked at runtime (will panic if you violate borrow rules)
/// - Typically safe in single-threaded parsing contexts
#[derive(Debug)]
pub struct SyntaxTreeBuilder<Lang> {
  builder: RefCell<GreenNodeBuilder<'static>>,
  _marker: PhantomData<Lang>,
}

impl<Lang> Default for SyntaxTreeBuilder<Lang>
where
  Lang: Language,
{
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

impl<Lang> SyntaxTreeBuilder<Lang>
where
  Lang: Language,
{
  /// Creates a new empty syntax tree builder.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  /// ```
  #[inline]
  pub fn new() -> Self {
    Self {
      builder: RefCell::new(GreenNodeBuilder::new()),
      _marker: PhantomData,
    }
  }

  /// Creates a checkpoint representing the current position in the tree.
  ///
  /// Checkpoints can be used with [`start_node_at()`](Self::start_node_at) to
  /// retroactively wrap already-added children in a new parent node. This is
  /// useful for handling left-recursive or ambiguous grammars.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  /// let checkpoint = builder.checkpoint();
  ///
  /// builder.token(SyntaxKind::Number, "42");
  ///
  /// // Wrap the number in an expression node
  /// builder.start_node_at(checkpoint, SyntaxKind::Expression);
  /// builder.finish_node();
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::checkpoint`]
  #[inline]
  pub fn checkpoint(&self) -> rowan::Checkpoint {
    self.builder.borrow().checkpoint()
  }

  /// Starts a new node with the given syntax kind.
  ///
  /// Must be paired with a corresponding [`finish_node()`](Self::finish_node) call.
  /// All tokens and child nodes added between `start_node()` and `finish_node()`
  /// will be children of this node.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  ///
  /// builder.start_node(SyntaxKind::BinaryExpression);
  /// builder.token(SyntaxKind::Number, "1");
  /// builder.token(SyntaxKind::Plus, "+");
  /// builder.token(SyntaxKind::Number, "2");
  /// builder.finish_node();
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::start_node`]
  #[inline]
  pub fn start_node(&self, kind: Lang::Kind) {
    self
      .builder
      .borrow_mut()
      .start_node(Lang::kind_to_raw(kind));
  }

  /// Starts a new node at a previously created checkpoint.
  ///
  /// This allows you to retroactively wrap children that were added after the
  /// checkpoint was created. Useful for handling operator precedence and
  /// left-recursive grammars.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  /// let checkpoint = builder.checkpoint();
  ///
  /// // Add a number
  /// builder.token(SyntaxKind::Number, "42");
  ///
  /// // Later, decide to wrap it in a unary expression
  /// builder.start_node_at(checkpoint, SyntaxKind::UnaryExpression);
  /// builder.token(SyntaxKind::Minus, "-");
  /// builder.finish_node();
  /// // Result: UnaryExpression(Number("42"), Minus("-"))
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::start_node_at`]
  #[inline]
  pub fn start_node_at(&self, checkpoint: rowan::Checkpoint, kind: Lang::Kind) {
    self
      .builder
      .borrow_mut()
      .start_node_at(checkpoint, Lang::kind_to_raw(kind));
  }

  /// Adds a token with the given kind and text to the current node.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  ///
  /// builder.start_node(SyntaxKind::Identifier);
  /// builder.token(SyntaxKind::IdentifierToken, "my_variable");
  /// builder.finish_node();
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::token`]
  #[inline]
  pub fn token(&self, kind: Lang::Kind, text: &str) {
    self
      .builder
      .borrow_mut()
      .token(Lang::kind_to_raw(kind), text);
  }

  /// Finishes the current node started with [`start_node()`](Self::start_node)
  /// or [`start_node_at()`](Self::start_node_at).
  ///
  /// # Panics
  ///
  /// Panics if there is no node to finish (i.e., `finish_node()` was called
  /// more times than `start_node()`).
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  ///
  /// builder.start_node(SyntaxKind::Root);
  /// builder.token(SyntaxKind::Identifier, "foo");
  /// builder.finish_node(); // Finishes the Root node
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::finish_node`]
  #[inline]
  pub fn finish_node(&self) {
    self.builder.borrow_mut().finish_node();
  }

  /// Completes the tree building and returns the final green node.
  ///
  /// This consumes the builder and returns the root [`rowan::GreenNode`],
  /// which can be converted to a [`rowan::SyntaxNode`] for traversal.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::SyntaxTreeBuilder;
  /// use rowan::SyntaxNode;
  ///
  /// let builder = SyntaxTreeBuilder::<MyLanguage>::new();
  ///
  /// builder.start_node(SyntaxKind::Root);
  /// builder.token(SyntaxKind::Identifier, "foo");
  /// builder.finish_node();
  ///
  /// let green = builder.finish();
  /// let root = SyntaxNode::new_root(green);
  /// ```
  ///
  /// See also: [`rowan::GreenNodeBuilder::finish`]
  #[inline]
  pub fn finish(self) -> rowan::GreenNode {
    self.builder.into_inner().finish()
  }
}

/// Base trait for all typed CST elements (nodes and tokens).
///
/// `CstElement` provides the common interface shared by both CST nodes
/// ([`CstNode`]) and CST tokens ([`CstToken`]). It enables:
/// - **Type checking**: Verify if an untyped element can be cast to a specific type
/// - **Type identity**: Associate elements with their syntax kind
/// - **Polymorphism**: Write generic code that works with both nodes and tokens
///
/// # Design
///
/// This trait serves as the foundation of the typed CST hierarchy:
/// ```text
/// CstElement (base)
///     ├── CstNode (for interior nodes)
///     └── CstToken (for leaf tokens)
/// ```
///
/// # Type Parameters
///
/// - `Language`: The rowan [`Language`] type defining syntax kinds
///
/// # Implementation
///
/// You typically don't implement this trait directly. Instead:
/// - For nodes: implement [`CstNode`] (which extends this trait)
/// - For tokens: implement [`CstToken`] (which extends this trait)
///
/// # Examples
///
/// ## Simple Token Implementation
///
/// ```rust,ignore
/// use tokit::cst::{CstElement, CstToken};
///
/// #[derive(Debug)]
/// struct Comma {
///     syntax: SyntaxToken<MyLanguage>,
/// }
///
/// impl CstElement for Comma {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::Comma;
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         kind == SyntaxKind::Comma
///     }
/// }
///
/// impl CstToken for Comma {
///     // ... implement token-specific methods
/// }
/// ```
///
/// ## Node with Multiple Variants
///
/// ```rust,ignore
/// use tokit::cst::{CstElement, CstNode};
///
/// #[derive(Debug)]
/// enum Literal {
///     Number(NumberLiteral),
///     String(StringLiteral),
///     Boolean(BooleanLiteral),
/// }
///
/// impl CstElement for Literal {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::Literal; // Marker
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         matches!(
///             kind,
///             SyntaxKind::NumberLiteral
///             | SyntaxKind::StringLiteral
///             | SyntaxKind::BooleanLiteral
///         )
///     }
/// }
///
/// impl CstNode for Literal {
///     // ... implement node-specific methods
/// }
/// ```
///
/// ## Generic Functions Using Elements
///
/// ```rust,ignore
/// use tokit::cst::CstElement;
///
/// fn element_kind<T: CstElement>(element: &T) -> String {
///     format!("{:?}", T::KIND)
/// }
///
/// // Works with both nodes and tokens
/// let comma: Comma = ...;
/// let expr: Expression = ...;
/// println!("Token kind: {}", element_kind(&comma));
/// println!("Node kind: {}", element_kind(&expr));
/// ```
pub trait CstElement<Lang: Language>: core::fmt::Debug {
  /// The syntax kind of this CST element.
  ///
  /// For enum elements representing multiple variants, this can be a marker value
  /// that is not directly used for casting but serves as documentation.
  const KIND: Lang::Kind;

  /// Returns `true` if the given kind can be cast to this CST element.
  ///
  /// This method determines whether an untyped rowan element with a specific
  /// syntax kind can be safely converted to this typed element.
  ///
  /// # Implementation Guidelines
  ///
  /// - **Single variant**: Return `kind == Self::KIND`
  /// - **Multiple variants**: Use pattern matching to check all valid kinds
  /// - **Performance**: This method is often called frequently, keep it fast
  ///
  /// # Examples
  ///
  /// ## Simple Element (Single Kind)
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstElement;
  ///
  /// impl CstElement for Comma {
  ///     type Language = MyLanguage;
  ///     const KIND: SyntaxKind = SyntaxKind::Comma;
  ///
  ///     fn can_cast(kind: SyntaxKind) -> bool {
  ///         kind == SyntaxKind::Comma
  ///     }
  /// }
  /// ```
  ///
  /// ## Enum Element (Multiple Kinds)
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstElement;
  ///
  /// impl CstElement for BinaryOperator {
  ///     type Language = MyLanguage;
  ///     const KIND: SyntaxKind = SyntaxKind::BinaryOp; // Marker
  ///
  ///     fn can_cast(kind: SyntaxKind) -> bool {
  ///         matches!(
  ///             kind,
  ///             SyntaxKind::Plus
  ///             | SyntaxKind::Minus
  ///             | SyntaxKind::Star
  ///             | SyntaxKind::Slash
  ///         )
  ///     }
  /// }
  /// ```
  ///
  /// ## Usage in Type Checking
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstElement;
  ///
  /// // Check before casting
  /// if Comma::can_cast(token.kind()) {
  ///     let comma = Comma::try_cast_node(token).unwrap();
  /// }
  /// ```
  fn can_cast(kind: Lang::Kind) -> bool
  where
    Self: Sized;
}

/// Trait for typed CST tokens (leaf elements in the syntax tree).
///
/// `CstToken` provides a type-safe wrapper around rowan's untyped [`SyntaxToken`],
/// representing terminal elements in the concrete syntax tree. Tokens are the leaf nodes
/// that contain actual source text (keywords, identifiers, literals, punctuation, etc.).
///
/// # Design
///
/// Tokens differ from nodes ([`CstNode`]) in that:
/// - **Tokens are leaves**: They contain source text directly
/// - **Nodes are interior**: They have children and structure the tree
/// - **Zero-cost**: Token wrappers have the same memory layout as [`SyntaxToken`]
///
/// # Type Parameters
///
/// - `Language`: The rowan [`Language`] type defining syntax kinds
///
/// # Implementation
///
/// To implement `CstToken`, you need to:
/// 1. Implement [`CstElement`] to define the token's kind and casting logic
/// 2. Implement [`try_cast_token()`](Self::try_cast_token) to convert from untyped tokens
/// 3. Implement [`syntax()`](Self::syntax) to access the underlying token
/// 4. Optionally override [`text()`](Self::text) if custom text extraction is needed
///
/// # Examples
///
/// ## Simple Token Implementation
///
/// ```rust,ignore
/// use tokit::cst::{CstElement, CstToken, error};
/// use rowan::SyntaxToken;
///
/// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// struct Comma {
///     syntax: SyntaxToken<MyLanguage>,
/// }
///
/// impl CstElement for Comma {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::Comma;
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         kind == SyntaxKind::Comma
///     }
/// }
///
/// impl CstToken for Comma {
///     fn try_cast_token(
///         syntax: SyntaxToken<Self::Language>
///     ) -> Result<Self, error::CstTokenMismatch<Self>> {
///         if Self::can_cast(syntax.kind()) {
///             Ok(Self { syntax })
///         } else {
///             Err(error::CstTokenMismatch::new(Self::KIND, syntax))
///         }
///     }
///
///     fn syntax(&self) -> &SyntaxToken<Self::Language> {
///         &self.syntax
///     }
/// }
/// ```
///
/// ## Token with Multiple Variants (Enum)
///
/// ```rust,ignore
/// use tokit::cst::{CstElement, CstToken};
///
/// #[derive(Debug, Clone)]
/// enum BinaryOperator {
///     Plus(PlusToken),
///     Minus(MinusToken),
///     Star(StarToken),
///     Slash(SlashToken),
/// }
///
/// impl CstElement for BinaryOperator {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::BinaryOp; // Marker
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         matches!(
///             kind,
///             SyntaxKind::Plus | SyntaxKind::Minus
///             | SyntaxKind::Star | SyntaxKind::Slash
///         )
///     }
/// }
///
/// impl CstToken for BinaryOperator {
///     fn try_cast_token(
///         syntax: SyntaxToken<Self::Language>
///     ) -> Result<Self, error::CstTokenMismatch<Self>> {
///         match syntax.kind() {
///             SyntaxKind::Plus => Ok(BinaryOperator::Plus(PlusToken { syntax })),
///             SyntaxKind::Minus => Ok(BinaryOperator::Minus(MinusToken { syntax })),
///             SyntaxKind::Star => Ok(BinaryOperator::Star(StarToken { syntax })),
///             SyntaxKind::Slash => Ok(BinaryOperator::Slash(SlashToken { syntax })),
///             _ => Err(error::CstTokenMismatch::new(Self::KIND, syntax)),
///         }
///     }
///
///     fn syntax(&self) -> &SyntaxToken<Self::Language> {
///         match self {
///             BinaryOperator::Plus(t) => &t.syntax,
///             BinaryOperator::Minus(t) => &t.syntax,
///             BinaryOperator::Star(t) => &t.syntax,
///             BinaryOperator::Slash(t) => &t.syntax,
///         }
///     }
/// }
/// ```
///
/// ## Using Tokens
///
/// ```rust,ignore
/// use tokit::cst::{CstToken, cast};
///
/// // Cast from untyped token
/// let comma = Comma::try_cast_token(syntax_token)?;
///
/// // Access token text
/// assert_eq!(comma.text(), ",");
///
/// // Get underlying syntax token for rowan APIs
/// let parent = comma.syntax().parent();
/// ```
///
/// ## Finding Tokens in Nodes
///
/// ```rust,ignore
/// use tokit::cst::cast;
///
/// // Find a specific token in a node
/// let equals_token = cast::token(&assignment_node, &SyntaxKind::Equals);
///
/// // Check if a token exists
/// if let Some(async_kw) = cast::token(&function_node, &SyntaxKind::AsyncKeyword) {
///     println!("Function is async");
/// }
/// ```
pub trait CstToken<Lang: Language>: CstElement<Lang> {
  /// Attempts to cast the given syntax token to this typed token.
  ///
  /// Returns an error if the token's kind doesn't match this type.
  ///
  /// # Errors
  ///
  /// Returns [`CstTokenMismatch`](error::CstTokenMismatch) if:
  /// - The token's kind doesn't match the expected kind for this type
  /// - For enum tokens, the kind is not one of the valid variants
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstToken;
  ///
  /// // Try to cast a token
  /// match Comma::try_cast_token(syntax_token) {
  ///     Ok(comma) => println!("Found comma at: {:?}", comma.syntax().text_range()),
  ///     Err(e) => eprintln!("Not a comma: {}", e),
  /// }
  ///
  /// // Unwrap if you're sure it's the right type
  /// let plus = PlusToken::try_cast_token(syntax_token).unwrap();
  /// ```
  fn try_cast_token(syntax: SyntaxToken<Lang>) -> Result<Self, error::CstTokenMismatch<Self, Lang>>
  where
    Self: Sized;

  /// Returns a reference to the underlying syntax token.
  ///
  /// This provides access to rowan's token APIs for inspecting position,
  /// text, and tree structure.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstToken;
  ///
  /// let comma: Comma = ...;
  ///
  /// // Get text range
  /// let range = comma.syntax().text_range();
  ///
  /// // Get parent node
  /// let parent = comma.syntax().parent();
  ///
  /// // Get next sibling
  /// let next = comma.syntax().next_sibling_or_token();
  /// ```
  fn syntax(&self) -> &SyntaxToken<Lang>;

  /// Returns the source text of this token.
  ///
  /// This is a convenience method that extracts the text from the underlying
  /// [`SyntaxToken`]. The text is always valid UTF-8.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::CstToken;
  ///
  /// let identifier: IdentifierToken = ...;
  /// assert_eq!(identifier.text(), "my_variable");
  ///
  /// let comma: Comma = ...;
  /// assert_eq!(comma.text(), ",");
  ///
  /// let number: NumberToken = ...;
  /// let value: i32 = number.text().parse()?;
  /// ```
  fn text(&self) -> &str
  where
    Lang: 'static,
  {
    self.syntax().text()
  }
}

/// The main trait for typed CST nodes with zero-cost conversions.
///
/// `Node` provides a type-safe wrapper around rowan's untyped [`SyntaxNode`], allowing
/// you to work with strongly-typed CST nodes. The conversion between typed and untyped
/// nodes has **zero runtime cost** - both representations have exactly the same memory
/// layout (a pointer to the tree root and a pointer to the node itself).
///
/// # Design
///
/// The `Node` trait enables:
/// - **Type safety**: Compile-time guarantees about node types
/// - **Zero-cost**: No runtime overhead for typed wrappers
/// - **Pattern matching**: Cast nodes to specific types
/// - **Tree traversal**: Navigate the CST with type information
///
/// # Type Parameters
///
/// - `Language`: The rowan [`Language`] type defining syntax kinds
///
/// # Implementation
///
/// To implement `Node`, you need to:
/// 1. Define a struct wrapping [`SyntaxNode<Language>`](SyntaxNode)
/// 2. Specify the [`KIND`](Self::KIND) constant
/// 3. Implement [`can_cast()`](Self::can_cast) to check if a kind matches
/// 4. Implement [`try_cast_node()`](Self::try_cast_node) to convert from untyped nodes
/// 5. Implement [`syntax()`](Self::syntax) to access the underlying node
///
/// # Examples
///
/// ## Basic Node Implementation
///
/// ```rust,ignore
/// use tokit::cst::{Node, error};
/// use rowan::SyntaxNode;
///
/// #[derive(Debug, Clone)]
/// struct IdentifierNode {
///     syntax: SyntaxNode<MyLanguage>,
/// }
///
/// impl Node for IdentifierNode {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::Identifier;
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         kind == Self::KIND
///     }
///
///     fn try_cast_node(
///         syntax: SyntaxNode<Self::Language>
///     ) -> Result<Self, error::CstNodeMismatch<Self>> {
///         if Self::can_cast(syntax.kind()) {
///             Ok(Self { syntax })
///         } else {
///             Err(error::CstNodeMismatch::new(Self::KIND, syntax))
///         }
///     }
///
///     fn syntax(&self) -> &SyntaxNode<Self::Language> {
///         &self.syntax
///     }
/// }
/// ```
///
/// ## Using Nodes
///
/// ```rust,ignore
/// use tokit::cst::CstNode;
///
/// // Try to cast an untyped node
/// let identifier = IdentifierNode::try_cast_node(syntax_node)?;
///
/// // Access the source text
/// let text = identifier.source_string();
///
/// // Clone for mutation
/// let mutable = identifier.clone_for_update();
/// ```
///
/// ## Enum Nodes for Variants
///
/// ```rust,ignore
/// use tokit::cst::CstNode;
///
/// #[derive(Debug, Clone)]
/// enum Expression {
///     Binary(BinaryExpr),
///     Unary(UnaryExpr),
///     Literal(LiteralExpr),
/// }
///
/// impl CstNode for Expression {
///     type Language = MyLanguage;
///     const KIND: SyntaxKind = SyntaxKind::Expression; // Marker, not used
///
///     fn can_cast(kind: SyntaxKind) -> bool {
///         matches!(
///             kind,
///             SyntaxKind::BinaryExpr | SyntaxKind::UnaryExpr | SyntaxKind::Literal
///         )
///     }
///
///     fn try_cast_node(
///         syntax: SyntaxNode<Self::Language>
///     ) -> Result<Self, error::CstNodeMismatch<Self>> {
///         match syntax.kind() {
///             SyntaxKind::BinaryExpr => Ok(Expression::Binary(BinaryExpr { syntax })),
///             SyntaxKind::UnaryExpr => Ok(Expression::Unary(UnaryExpr { syntax })),
///             SyntaxKind::Literal => Ok(Expression::Literal(LiteralExpr { syntax })),
///             _ => Err(error::CstNodeMismatch::new(Self::KIND, syntax)),
///         }
///     }
///
///     fn syntax(&self) -> &SyntaxNode<Self::Language> {
///         match self {
///             Expression::Binary(e) => &e.syntax,
///             Expression::Unary(e) => &e.syntax,
///             Expression::Literal(e) => &e.syntax,
///         }
///     }
/// }
/// ```
pub trait CstNode<Lang: Language>: CstElement<Lang> + Syntax {
  /// Attempts to cast the given syntax node to this CST node.
  ///
  /// Returns an error if the node's kind doesn't match this type.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::Node;
  ///
  /// let identifier = IdentifierNode::try_cast_node(syntax_node)?;
  /// ```
  fn try_cast_node(syntax: SyntaxNode<Lang>) -> Result<Self, error::SyntaxError<Self, Lang>>
  where
    Self: Sized;

  /// Returns a reference to the underlying syntax node.
  ///
  /// This provides access to rowan's tree traversal APIs.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::Node;
  ///
  /// let node: IdentifierNode = ...;
  /// let parent = node.syntax().parent();
  /// let children = node.syntax().children();
  /// ```
  fn syntax(&self) -> &SyntaxNode<Lang>;

  /// Returns the source string of this CST node.
  ///
  /// This includes all text spanned by this node, including whitespace and trivia.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::Node;
  ///
  /// let identifier: IdentifierNode = ...;
  /// assert_eq!(identifier.source_string(), "my_variable");
  /// ```
  fn source_string(&self) -> String {
    self.syntax().to_string()
  }

  /// Clones this CST node for update operations.
  ///
  /// This creates a mutable copy of the node that can be modified using rowan's
  /// mutation APIs. The original tree remains unchanged.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::Node;
  ///
  /// let original: IdentifierNode = ...;
  /// let mut mutable = original.clone_for_update();
  ///
  /// // Modify the mutable copy
  /// // (using rowan's mutation APIs)
  /// ```
  fn clone_for_update(&self) -> Self
  where
    Self: Sized,
  {
    Self::try_cast_node(self.syntax().clone_for_update()).unwrap()
  }

  /// Clones the subtree rooted at this CST node.
  ///
  /// This creates a deep copy of this node and all its descendants, detached
  /// from the original tree.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::Node;
  ///
  /// let original: ExpressionNode = ...;
  /// let copy = original.clone_subtree();
  ///
  /// // copy is independent of original
  /// ```
  fn clone_subtree(&self) -> Self
  where
    Self: Sized,
  {
    Self::try_cast_node(self.syntax().clone_subtree()).unwrap()
  }
}

/// An iterator over typed CST children of a particular node type.
///
/// `CstNodeChildren` filters and casts child nodes to a specific typed node type,
/// skipping any children that cannot be cast to the target type.
///
/// # Type Parameters
///
/// - `N`: The typed [`Node`] type to iterate over
///
/// # Examples
///
/// ```rust,ignore
/// use tokit::cst::{Node, cast};
///
/// // Get all Identifier children of a function
/// let identifiers: Vec<IdentifierNode> = cast::children(&function_node.syntax())
///     .collect();
///
/// // Filter by kind within the same type
/// let params = cast::children::<Parameter>(&function_node.syntax())
///     .by_kind(|k| k == SyntaxKind::Parameter);
/// ```
#[derive(Debug, From, Into)]
#[repr(transparent)]
pub struct CstNodeChildren<N, Lang: Language> {
  inner: rowan::SyntaxNodeChildren<Lang>,
  _m: PhantomData<N>,
}

impl<N, Lang: Language> Clone for CstNodeChildren<N, Lang> {
  #[inline]
  fn clone(&self) -> Self {
    Self {
      inner: self.inner.clone(),
      _m: PhantomData,
    }
  }
}

impl<N, Lang: Language> CstNodeChildren<N, Lang> {
  #[inline]
  fn new(parent: &SyntaxNode<Lang>) -> Self {
    Self {
      inner: parent.children(),
      _m: PhantomData,
    }
  }

  /// Returns an iterator over syntax node children matching a kind predicate.
  ///
  /// This allows further filtering of children based on their syntax kind,
  /// returning the underlying [`SyntaxNode`] instead of typed nodes.
  ///
  /// # Examples
  ///
  /// ```rust,ignore
  /// use tokit::cst::cast;
  ///
  /// // Get all expression children, filtered by specific kinds
  /// let binary_exprs = cast::children::<Expression>(&node)
  ///     .by_kind(|k| k == SyntaxKind::BinaryExpr);
  /// ```
  pub fn by_kind<F>(self, f: F) -> impl Iterator<Item = SyntaxNode<Lang>>
  where
    F: Fn(Lang::Kind) -> bool,
  {
    self.inner.by_kind(f)
  }
}

impl<N, Lang> Iterator for CstNodeChildren<N, Lang>
where
  N: CstNode<Lang>,
  Lang: Language,
{
  type Item = N;

  #[inline]
  fn next(&mut self) -> Option<N> {
    self.inner.find_map(|t| N::try_cast_node(t).ok())
  }
}

/// Utility functions for casting and accessing CST nodes.
///
/// This module provides convenient functions for working with typed CST nodes,
/// including finding children, accessing tokens, and casting between types.
///
/// # Examples
///
/// ```rust,ignore
/// use tokit::cst::cast;
///
/// // Get the first identifier child
/// let identifier = cast::child::<IdentifierNode>(&parent_node);
///
/// // Get all statement children
/// let statements: Vec<Statement> = cast::children(&function_node).collect();
///
/// // Get a specific token
/// let equals_token = cast::token(&assignment_node, &SyntaxKind::Equals);
/// ```
pub mod cast;

/// Error types for CST operations.
pub mod error;