@@ -5,13 +5,17 @@ use crate::ast::declarations::{
55 Declaration , GlobalDeclaration , InterfaceDeclaration , ModuleAliasDeclaration ,
66 ModuleDeclaration , ModuleMember , ModuleSelf , TypeAliasDeclaration ,
77} ;
8+ use crate :: ast:: directives:: {
9+ Directive , UseClause , UseDirective , UseSingleClause , UseWildcardClause ,
10+ } ;
811use crate :: ast:: location:: {
912 AliasDeclarationLocation , AliasLocation , AliasMemberLocation , AttributeMemberLocation ,
1013 ClassDeclarationLocation , ClassInstanceLocation , ClassSingletonLocation , ClassSuperLocation ,
1114 ConstantDeclarationLocation , FunctionParamLocation , GlobalDeclarationLocation ,
1215 InterfaceDeclarationLocation , InterfaceLocation , LocationRange , MethodDefinitionLocation ,
1316 MethodTypeLocation , MixinMemberLocation , ModuleDeclarationLocation , ModuleSelfLocation ,
14- TypeAliasDeclarationLocation , TypeParamLocation , VariableMemberLocation ,
17+ TypeAliasDeclarationLocation , TypeParamLocation , UseDirectiveLocation , UseSingleClauseLocation ,
18+ UseWildcardClauseLocation , VariableMemberLocation ,
1519} ;
1620use crate :: ast:: members:: {
1721 AliasKind , AliasMember , AttrAccessorMember , AttrReaderMember , AttrWriterMember , AttributeKind ,
@@ -38,9 +42,10 @@ use crate::node::{
3842 FunctionTypeNode , GlobalNode , IncludeNode , InstanceVariableNode , InterfaceNode ,
3943 InterfaceTypeNode , MethodDefinitionKind as NodeMethodDefinitionKind , MethodDefinitionNode ,
4044 MethodDefinitionOverloadNode , MethodDefinitionVisibility as NodeMethodDefinitionVisibility ,
41- MethodTypeNode , ModuleAliasNode , ModuleNode , ModuleSelfNode , Node , PrependNode , PrivateNode ,
42- PublicNode , RBSLocationRange , SymbolNode , TypeAliasNode , TypeNameNode , TypeParamNode ,
43- TypeParamVariance , UntypedFunctionTypeNode ,
45+ MethodTypeNode , ModuleAliasNode , ModuleNode , ModuleSelfNode , NamespaceNode , Node , PrependNode ,
46+ PrivateNode , PublicNode , RBSLocationRange , SymbolNode , TypeAliasNode , TypeNameNode ,
47+ TypeParamNode , TypeParamVariance , UntypedFunctionTypeNode , UseNode , UseSingleClauseNode ,
48+ UseWildcardClauseNode ,
4449} ;
4550use crate :: type_name:: TypeNameInterner ;
4651
@@ -108,6 +113,13 @@ impl<'a> AstConverter<'a> {
108113 }
109114 }
110115
116+ pub fn convert_directive ( & mut self , node : & Node < ' _ > ) -> Directive {
117+ match node {
118+ Node :: Use ( node) => Directive :: Use ( self . convert_use_directive ( node) ) ,
119+ _ => panic_expected ( "directive node while converting directive" , node) ,
120+ }
121+ }
122+
111123 pub fn convert_type ( & mut self , node : & Node < ' _ > ) -> Type {
112124 match node {
113125 Node :: AliasType ( node) => Type :: Alias ( self . convert_alias_type ( node) ) ,
@@ -245,6 +257,53 @@ impl<'a> AstConverter<'a> {
245257 }
246258 }
247259
260+ fn convert_use_directive ( & mut self , node : & UseNode < ' _ > ) -> UseDirective {
261+ UseDirective {
262+ clauses : self . convert_use_clauses ( node. clauses ( ) ) ,
263+ location : Some ( UseDirectiveLocation {
264+ range : convert_range ( node. location ( ) ) ,
265+ keyword_range : convert_range ( node. keyword_location ( ) ) ,
266+ } ) ,
267+ }
268+ }
269+
270+ fn convert_use_clause ( & mut self , node : & Node < ' _ > ) -> UseClause {
271+ match node {
272+ Node :: UseSingleClause ( node) => UseClause :: Single ( self . convert_use_single_clause ( node) ) ,
273+ Node :: UseWildcardClause ( node) => {
274+ UseClause :: Wildcard ( self . convert_use_wildcard_clause ( node) )
275+ }
276+ _ => panic_expected ( "use clause node while converting use directive" , node) ,
277+ }
278+ }
279+
280+ fn convert_use_single_clause ( & mut self , node : & UseSingleClauseNode < ' _ > ) -> UseSingleClause {
281+ UseSingleClause {
282+ type_name : self . convert_type_name ( & node. type_name ( ) ) ,
283+ new_name : node. new_name ( ) . map ( |name| self . intern_symbol ( & name) ) ,
284+ location : Some ( UseSingleClauseLocation {
285+ range : convert_range ( node. location ( ) ) ,
286+ type_name_range : convert_range ( node. type_name_location ( ) ) ,
287+ keyword_range : convert_optional_range ( node. keyword_location ( ) ) ,
288+ new_name_range : convert_optional_range ( node. new_name_location ( ) ) ,
289+ } ) ,
290+ }
291+ }
292+
293+ fn convert_use_wildcard_clause (
294+ & mut self ,
295+ node : & UseWildcardClauseNode < ' _ > ,
296+ ) -> UseWildcardClause {
297+ UseWildcardClause {
298+ namespace : self . convert_namespace ( & node. namespace ( ) ) ,
299+ location : Some ( UseWildcardClauseLocation {
300+ range : convert_range ( node. location ( ) ) ,
301+ namespace_range : convert_range ( node. namespace_location ( ) ) ,
302+ star_range : convert_range ( node. star_location ( ) ) ,
303+ } ) ,
304+ }
305+ }
306+
248307 fn convert_module_declaration ( & mut self , node : & ModuleNode < ' _ > ) -> ModuleDeclaration {
249308 ModuleDeclaration {
250309 name : self . convert_type_name ( & node. name ( ) ) ,
@@ -757,6 +816,12 @@ impl<'a> AstConverter<'a> {
757816 . collect ( )
758817 }
759818
819+ fn convert_use_clauses ( & mut self , list : crate :: node:: NodeList < ' _ > ) -> Vec < UseClause > {
820+ list. iter ( )
821+ . map ( |node| self . convert_use_clause ( & node) )
822+ . collect ( )
823+ }
824+
760825 fn convert_class_members ( & mut self , list : crate :: node:: NodeList < ' _ > ) -> Vec < ClassMember > {
761826 list. iter ( )
762827 . map ( |node| match node {
@@ -946,9 +1011,14 @@ impl<'a> AstConverter<'a> {
9461011 }
9471012
9481013 fn convert_type_name ( & mut self , node : & TypeNameNode < ' _ > ) -> TypeName {
949- let namespace = node. namespace ( ) ;
950- let mut name = self . type_names . root ( namespace. absolute ( ) ) ;
951- for segment_node in namespace. path ( ) . iter ( ) {
1014+ let name = self . convert_namespace ( & node. namespace ( ) ) ;
1015+ let final_segment = self . intern_symbol ( & node. name ( ) ) ;
1016+ self . type_names . append ( name, final_segment)
1017+ }
1018+
1019+ fn convert_namespace ( & mut self , node : & NamespaceNode < ' _ > ) -> TypeName {
1020+ let mut name = self . type_names . root ( node. absolute ( ) ) ;
1021+ for segment_node in node. path ( ) . iter ( ) {
9521022 match segment_node {
9531023 Node :: Symbol ( segment) => {
9541024 let segment = self . intern_symbol ( & segment) ;
@@ -960,8 +1030,7 @@ impl<'a> AstConverter<'a> {
9601030 ) ,
9611031 }
9621032 }
963- let final_segment = self . intern_symbol ( & node. name ( ) ) ;
964- self . type_names . append ( name, final_segment)
1033+ name
9651034 }
9661035
9671036 fn intern_symbol ( & mut self , node : & SymbolNode < ' _ > ) -> SymbolId {
0 commit comments