@@ -13,7 +13,7 @@ use rustpython_parser::location::Location;
13
13
use std:: collections:: HashMap ;
14
14
15
15
pub fn make_symbol_table ( program : & ast:: Program ) -> Result < SymbolScope , SymbolTableError > {
16
- let mut builder = SymbolTableBuilder :: new ( ) ;
16
+ let mut builder: SymbolTableBuilder = Default :: default ( ) ;
17
17
builder. enter_scope ( ) ;
18
18
builder. scan_program ( program) ?;
19
19
assert_eq ! ( builder. scopes. len( ) , 1 ) ;
@@ -26,7 +26,7 @@ pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTa
26
26
pub fn statements_to_symbol_table (
27
27
statements : & [ ast:: LocatedStatement ] ,
28
28
) -> Result < SymbolScope , SymbolTableError > {
29
- let mut builder = SymbolTableBuilder :: new ( ) ;
29
+ let mut builder: SymbolTableBuilder = Default :: default ( ) ;
30
30
builder. enter_scope ( ) ;
31
31
builder. scan_statements ( statements) ?;
32
32
assert_eq ! ( builder. scopes. len( ) , 1 ) ;
@@ -73,16 +73,18 @@ impl From<SymbolTableError> for CompileError {
73
73
type SymbolTableResult = Result < ( ) , SymbolTableError > ;
74
74
75
75
impl SymbolScope {
76
- pub fn new ( ) -> Self {
76
+ pub fn lookup ( & self , name : & str ) -> Option < & SymbolRole > {
77
+ self . symbols . get ( name)
78
+ }
79
+ }
80
+
81
+ impl Default for SymbolScope {
82
+ fn default ( ) -> Self {
77
83
SymbolScope {
78
84
symbols : HashMap :: new ( ) ,
79
85
sub_scopes : vec ! [ ] ,
80
86
}
81
87
}
82
-
83
- pub fn lookup ( & self , name : & str ) -> Option < & SymbolRole > {
84
- self . symbols . get ( name)
85
- }
86
88
}
87
89
88
90
impl std:: fmt:: Debug for SymbolScope {
@@ -153,13 +155,15 @@ pub struct SymbolTableBuilder {
153
155
pub scopes : Vec < SymbolScope > ,
154
156
}
155
157
156
- impl SymbolTableBuilder {
157
- pub fn new ( ) -> Self {
158
+ impl Default for SymbolTableBuilder {
159
+ fn default ( ) -> Self {
158
160
SymbolTableBuilder { scopes : vec ! [ ] }
159
161
}
162
+ }
160
163
164
+ impl SymbolTableBuilder {
161
165
pub fn enter_scope ( & mut self ) {
162
- let scope = SymbolScope :: new ( ) ;
166
+ let scope = Default :: default ( ) ;
163
167
self . scopes . push ( scope) ;
164
168
}
165
169
0 commit comments