@@ -22,28 +22,83 @@ impl Program {
2222
2323 pub fn add_function_declaration ( & mut self , function_declaration : FunctionDeclaration ) {
2424 self . items
25- . push ( Item :: FunctionDeclaration ( function_declaration) )
25+ . push ( Item :: FunctionDeclaration ( function_declaration) ) ;
26+ }
27+
28+ pub fn add_trait_declaration ( & mut self , trait_declaration : TraitDeclaration ) {
29+ self . items . push ( Item :: TraitDeclaration ( trait_declaration) ) ;
30+ }
31+
32+ pub fn add_trait_implementation ( & mut self , trait_implementation : TraitImplementation ) {
33+ self . items
34+ . push ( Item :: TraitImplementation ( trait_implementation) ) ;
2635 }
2736}
2837
2938/// A node which may appear at the top-level of a program.
3039#[ derive( Clone , Debug ) ]
3140pub enum Item {
41+ TraitDeclaration ( TraitDeclaration ) ,
42+ TraitImplementation ( TraitImplementation ) ,
3243 FunctionDeclaration ( FunctionDeclaration ) ,
3344}
3445
46+ /// Trait declaration.
47+ ///
48+ /// ```
49+ /// trait MyTrait {
50+ /// fn a() -> bool;
51+ /// fn b(n: i32);
52+ /// }
53+ /// ```
54+ #[ derive( Clone , Debug ) ]
55+ pub struct TraitDeclaration {
56+ #[ expect( dead_code, reason = "token field" ) ]
57+ pub tok_trait : tok:: Trait ,
58+ pub name : tok:: Ident ,
59+ #[ expect( dead_code, reason = "token field" ) ]
60+ pub tok_l_brace : tok:: LBrace ,
61+ pub methods : Vec < ( FunctionSignature , tok:: SemiColon ) > ,
62+ #[ expect( dead_code, reason = "token field" ) ]
63+ pub tok_r_brace : tok:: RBrace ,
64+ }
65+
66+ /// Trait implementation.
67+ ///
68+ /// ```
69+ /// impl MyTrait for SomeType {
70+ /// fn a() -> bool {
71+ /// true
72+ /// }
73+ ///
74+ /// fn b(n: 123) { }
75+ /// }
76+ /// ```
77+ #[ derive( Clone , Debug ) ]
78+ pub struct TraitImplementation {
79+ #[ expect( dead_code, reason = "token field" ) ]
80+ pub tok_impl : tok:: Impl ,
81+ pub name : tok:: Ident ,
82+ #[ expect( dead_code, reason = "named fields will be implemented with struct" ) ]
83+ pub tok_for : tok:: For ,
84+ pub ty : CstType ,
85+ #[ expect( dead_code, reason = "token field" ) ]
86+ pub tok_l_brace : tok:: LBrace ,
87+ pub methods : Vec < FunctionDeclaration > ,
88+ #[ expect( dead_code, reason = "token field" ) ]
89+ pub tok_r_brace : tok:: RBrace ,
90+ }
91+
3592mod function {
3693 use super :: * ;
3794
38- /// Function declaration .
95+ /// Function signature .
3996 ///
4097 /// ```
41- /// fn some_function(parameter_a: usize, parameter_b: bool) -> f64 {
42- /// // Statements...
43- /// }
98+ /// fn some_function(parameter_a: usize, parameter_b: bool) -> f64
4499 /// ```
45100 #[ derive( Clone , Debug ) ]
46- pub struct FunctionDeclaration {
101+ pub struct FunctionSignature {
47102 #[ expect( dead_code, reason = "token field" ) ]
48103 pub tok_fn : tok:: Fn ,
49104 /// Name of the function.
@@ -56,6 +111,20 @@ mod function {
56111 pub tok_r_parenthesis : tok:: RParenthesis ,
57112 /// Optional return type for the function.
58113 pub return_ty : Option < FunctionReturnType > ,
114+ }
115+
116+ /// Function declaration.
117+ ///
118+ /// ```
119+ /// fn some_function(parameter_a: usize, parameter_b: bool) -> f64 {
120+ /// // Statements...
121+ /// }
122+ /// ```
123+ #[ derive( Clone , Debug ) ]
124+ pub struct FunctionDeclaration {
125+ /// Signature of the function.
126+ pub signature : FunctionSignature ,
127+ /// Implementation of the function.
59128 pub body : Block ,
60129 }
61130
@@ -212,6 +281,7 @@ mod expression {
212281 Variable ( Variable ) ,
213282 Tuple ( Tuple < Expression > ) ,
214283 Field ( Field ) ,
284+ QualifiedPath ( QualifiedPath ) ,
215285 }
216286
217287 /// Assignment.
@@ -425,6 +495,29 @@ mod expression {
425495 Named ( tok:: Ident ) ,
426496 }
427497
498+ /// Qualified path.
499+ ///
500+ /// ```
501+ /// <Ty as Trait>::item
502+ /// ```
503+ #[ derive( Clone , Debug ) ]
504+ pub struct QualifiedPath {
505+ #[ expect( dead_code, reason = "named fields will be implemented with struct" ) ]
506+ pub tok_l_angle : tok:: LAngle ,
507+ /// Self qualifier.
508+ pub ty : CstType ,
509+ #[ expect( dead_code, reason = "named fields will be implemented with struct" ) ]
510+ pub tok_as : tok:: As ,
511+ /// Qualification.
512+ pub name : tok:: Ident ,
513+ #[ expect( dead_code, reason = "named fields will be implemented with struct" ) ]
514+ pub tok_r_angle : tok:: RAngle ,
515+ #[ expect( dead_code, reason = "named fields will be implemented with struct" ) ]
516+ pub tok_colon_colon : tok:: ColonColon ,
517+ /// Path item.
518+ pub item : tok:: Ident ,
519+ }
520+
428521 enum_conversion ! {
429522 [ Expression ]
430523 Assign : Assign ,
@@ -439,6 +532,7 @@ mod expression {
439532 Variable : Variable ,
440533 Tuple : Tuple <Expression >,
441534 Field : Field ,
535+ QualifiedPath : QualifiedPath ,
442536 }
443537}
444538
0 commit comments