1
1
// based on https://rust-unofficial.github.io/patterns/patterns/behavioural/visitor.html
2
2
// and https://github.com/rust-lang/rust/blob/5bc7b9ac8ace5312e1d2cdc2722715cf58d4f926/compiler/rustc_ast_ir/src/visit.rs
3
3
4
- use crate :: ast:: * ;
5
-
6
- /// Placeholder macro for visiting, It is used instead of calling visitor methods directly.
7
- /// Would be useful if we need some operation to happen for every visit.
8
- macro_rules! visit {
9
- ( $expr: expr) => {
10
- $expr
11
- } ;
12
- }
13
-
14
- macro_rules! visit_list {
15
- ( $visitor: ident. $method: ident( $list: expr $( , $( $extra_args: expr) , * ) ?) ) => {
16
- for elem in $list {
17
- visit!( $visitor. $method( elem $( , $( $extra_args) ,* ) ?) )
18
- }
19
- } ;
20
- }
21
-
22
- macro_rules! visit_scope {
23
- ( $visitor: ident => $block: block) => {
24
- $visitor. enter_scope( ) ;
25
- $block
26
- $visitor. leave_scope( ) ;
27
- } ;
28
- }
4
+ use fuse_ast:: ast:: * ;
5
+ use crate :: { visit, visit_scope, visit_list} ;
29
6
30
7
pub trait Visitor < ' ast > : Sized {
31
8
fn enter_scope ( & mut self ) { }
@@ -41,37 +18,37 @@ pub trait Visitor<'ast>: Sized {
41
18
}
42
19
43
20
fn visit_statement ( & mut self , statement : & ' ast Statement ) {
44
- walk_statement ( self , & statement)
21
+ walk_statement ( self , statement)
45
22
}
46
23
47
24
fn visit_variable_declaration ( & mut self , decl : & ' ast VariableDeclaration ) {
48
- walk_variable_declaration ( self , & decl)
25
+ walk_variable_declaration ( self , decl)
49
26
}
50
27
51
28
fn visit_function_declaration ( & mut self , decl : & ' ast Function ) {
52
- walk_function ( self , & decl)
29
+ walk_function ( self , decl)
53
30
}
54
31
55
32
fn visit_enum_declaration ( & mut self , decl : & ' ast EnumDeclaration ) {
56
- walk_enum_declaration ( self , & decl)
33
+ walk_enum_declaration ( self , decl)
57
34
}
58
35
59
36
fn visit_enum_variant ( & mut self , var : & ' ast EnumVariant ) {
60
- walk_enum_variant ( self , & var)
37
+ walk_enum_variant ( self , var)
61
38
}
62
39
63
40
fn visit_struct_declaration ( & mut self , decl : & ' ast StructDeclaration ) {
64
- walk_struct_declaration ( self , & decl)
41
+ walk_struct_declaration ( self , decl)
65
42
}
66
43
67
44
fn visit_struct_field ( & mut self , field : & ' ast StructField ) {
68
- walk_struct_field ( self , & field)
45
+ walk_struct_field ( self , field)
69
46
}
70
47
71
48
fn visit_visibility_modifier ( & mut self , _: & ' ast VisibilityModifier ) { }
72
49
73
50
fn visit_expression ( & mut self , expression : & ' ast Expression ) {
74
- walk_expression ( self , & expression)
51
+ walk_expression ( self , expression)
75
52
}
76
53
77
54
fn visit_number_literal ( & mut self , _: & ' ast NumberLiteral ) { }
@@ -83,31 +60,31 @@ pub trait Visitor<'ast>: Sized {
83
60
fn visit_identifier ( & mut self , _: & ' ast Identifier ) { }
84
61
85
62
fn visit_function ( & mut self , func : & ' ast Function ) {
86
- walk_function ( self , & func)
63
+ walk_function ( self , func)
87
64
}
88
65
89
66
fn visit_function_signature ( & mut self , sign : & ' ast FunctionSignature ) {
90
- walk_function_signature ( self , & sign)
67
+ walk_function_signature ( self , sign)
91
68
}
92
69
93
70
fn visit_function_parameters ( & mut self , params : & ' ast FunctionParameters ) {
94
- walk_function_parameters ( self , & params)
71
+ walk_function_parameters ( self , params)
95
72
}
96
73
97
74
fn visit_function_parameter ( & mut self , param : & ' ast FunctionParameter ) {
98
- walk_function_parameter ( self , & param)
75
+ walk_function_parameter ( self , param)
99
76
}
100
77
101
78
fn visit_function_body ( & mut self , body : & ' ast FunctionBody ) {
102
- walk_function_body ( self , & body)
79
+ walk_function_body ( self , body)
103
80
}
104
81
105
82
fn visit_if ( & mut self , r#if : & ' ast If ) {
106
- walk_if ( self , & r#if)
83
+ walk_if ( self , r#if)
107
84
}
108
85
109
86
fn visit_else ( & mut self , r#else : & ' ast Else ) {
110
- walk_else ( self , & r#else)
87
+ walk_else ( self , r#else)
111
88
}
112
89
113
90
fn visit_unary_operator ( & mut self , op : & ' ast UnaryOperator ) {
@@ -151,27 +128,27 @@ pub trait Visitor<'ast>: Sized {
151
128
}
152
129
153
130
fn visit_binding_pattern ( & mut self , pattern : & ' ast BindingPattern ) {
154
- walk_binding_pattern ( self , & pattern)
131
+ walk_binding_pattern ( self , pattern)
155
132
}
156
133
157
134
fn visit_binding_identifier ( & mut self , pattern : & ' ast BindingIdentifier ) {
158
- walk_binding_identifier ( self , & pattern)
135
+ walk_binding_identifier ( self , pattern)
159
136
}
160
137
161
138
fn visit_binding_rest ( & mut self , arg : & ' ast BindingRest ) {
162
- walk_binding_rest ( self , & arg)
139
+ walk_binding_rest ( self , arg)
163
140
}
164
141
165
142
fn visit_key_value_argument ( & mut self , arg : & ' ast KeyValueArgument ) {
166
- walk_key_value_argument ( self , & arg)
143
+ walk_key_value_argument ( self , arg)
167
144
}
168
145
169
146
fn visit_spread_argument ( & mut self , arg : & ' ast SpreadArgument ) {
170
- walk_spread_argument ( self , & arg)
147
+ walk_spread_argument ( self , arg)
171
148
}
172
149
173
150
fn visit_type_annotation ( & mut self , annotation : & ' ast TypeAnnotation ) {
174
- walk_type_annotation ( self , & annotation)
151
+ walk_type_annotation ( self , annotation)
175
152
}
176
153
}
177
154
0 commit comments