@@ -64,6 +64,7 @@ impl<'a> PythonParser<'a> {
6464 for child_node in traverse:: PreOrder :: new ( node. walk ( ) ) {
6565 if child_node. start_position ( ) . row + 1 == * self . line && self . node_types . contains ( & child_node. kind ( ) ) {
6666 return match child_node. kind ( ) {
67+ "class_definition" => Some ( self . parse_class ( & child_node) ) ,
6768 "function_definition" => Some ( self . parse_function ( & child_node) ) ,
6869 _ => None ,
6970 } ;
@@ -73,6 +74,60 @@ impl<'a> PythonParser<'a> {
7374 None
7475 }
7576
77+ fn parse_class ( & self , node : & Node ) -> Result < Map < String , Value > , String > {
78+ let mut tokens = Map :: new ( ) ;
79+
80+ for child_node in node. children ( & mut node. walk ( ) ) {
81+ match child_node. kind ( ) {
82+ "identifier" => {
83+ tokens. insert ( "name" . to_string ( ) , Value :: String ( self . get_node_text ( & child_node) ) ) ;
84+ } ,
85+ "block" => {
86+ let attributes = self . parse_class_attributes ( & child_node) ;
87+ if !attributes. is_empty ( ) {
88+ tokens. insert ( "attributes" . to_string ( ) , Value :: Array ( attributes) ) ;
89+ }
90+ }
91+ _ => { } ,
92+ }
93+ }
94+
95+ Ok ( tokens)
96+ }
97+
98+ fn parse_class_attributes ( & self , node : & Node ) -> Vec < Value > {
99+ let mut attributes = Vec :: new ( ) ;
100+
101+ node
102+ . children ( & mut node. walk ( ) )
103+ . filter ( |node| node. kind ( ) == "expression_statement" )
104+ . for_each ( |node| {
105+ let mut attr = Map :: new ( ) ;
106+
107+ node
108+ . children ( & mut node. walk ( ) )
109+ . filter ( |node| node. kind ( ) == "assignment" )
110+ . for_each ( |node|
111+ for child_node in node. children ( & mut node. walk ( ) ) {
112+ match child_node. kind ( ) {
113+ "identifier" => {
114+ attr. insert ( "name" . to_string ( ) , Value :: String ( self . get_node_text ( & child_node) ) ) ;
115+ } ,
116+ "type" => {
117+ attr. insert ( "type" . to_string ( ) , Value :: String ( self . get_node_text ( & child_node) ) ) ;
118+ } ,
119+ _ => { }
120+ }
121+ }
122+ ) ;
123+
124+ attributes. push ( Value :: Object ( attr) ) ;
125+ } ) ;
126+
127+ attributes
128+ }
129+
130+
76131 fn parse_function ( & self , node : & Node ) -> Result < Map < String , Value > , String > {
77132 let mut tokens = Map :: new ( ) ;
78133
0 commit comments