@@ -31,6 +31,68 @@ pub enum Node {
3131}
3232
3333impl Node {
34+ /// Applies a closure to the current [`ListInitialiser`].
35+ ///
36+ /// It applies the closure somewhere in the [`Node`]. If this closure
37+ /// returns a value, it is returns in `Ok(_)`. If no list initialiser is
38+ /// found, `Err(())` is returned.
39+ ///
40+ /// In the case of nested [`ListInitialiser`]s, the closure is applied to
41+ /// the one closest from the leaves.
42+ #[ allow( clippy:: min_ident_chars) ]
43+ pub fn apply_to_last_list_initialiser < T , F : Fn ( & mut Vec < Self > , & mut bool ) -> T > (
44+ & mut self ,
45+ f : & F ,
46+ ) -> Result < T , ( ) > {
47+ match self {
48+ //
49+ //
50+ // success
51+ Self :: ListInitialiser ( ListInitialiser {
52+ elts,
53+ full : full @ false ,
54+ } ) => {
55+ if let Some ( last) = elts. last_mut ( ) {
56+ if let res @ Ok ( _) = last. apply_to_last_list_initialiser ( f) {
57+ return res;
58+ }
59+ }
60+ Ok ( f ( elts, full) )
61+ }
62+ //
63+ //
64+ // failure
65+ // atomic
66+ Self :: Empty
67+ | Self :: Leaf ( _)
68+ | Self :: ParensBlock ( _)
69+ // child is none
70+ | Self :: Unary ( Unary { arg : None , ..} )
71+ | Self :: Binary ( Binary { arg_r : None , ..} )
72+ // full lists
73+ | Self :: Block ( Block { full : true , ..} )
74+ | Self :: FunctionCall ( FunctionCall { full : true , ..} )
75+ | Self :: ListInitialiser ( ListInitialiser { full : true , ..} ) => Err ( ( ) ) ,
76+ //
77+ //
78+ // recurse
79+ Self :: Unary ( Unary { arg : Some ( arg) , .. } )
80+ | Self :: Binary ( Binary { arg_r : Some ( arg) , .. } )
81+ | Self :: Ternary ( Ternary { failure : Some ( arg) , .. } | Ternary { condition : arg, .. } ) => {
82+ arg. apply_to_last_list_initialiser ( f)
83+ }
84+ //
85+ //
86+ // try recurse on non-full lists
87+ Self :: FunctionCall ( FunctionCall {
88+ full : false , args : vec, ..
89+ } )
90+ | Self :: Block ( Block { elts : vec, full : false } ) => vec
91+ . last_mut ( )
92+ . map_or ( Err ( ( ) ) , |node| node. apply_to_last_list_initialiser ( f) ) ,
93+ }
94+ }
95+
3496 /// Checks if a `{` is meant as a [`ListInitialiser`] or as a [`Block`].
3597 ///
3698 /// # Returns
@@ -90,73 +152,11 @@ impl Node {
90152 Self :: Block ( Block { elts : vec, full : false } )
91153 | Self :: FunctionCall ( FunctionCall { args : vec, full : false , .. } )
92154 | Self :: ListInitialiser ( ListInitialiser { elts : vec, full : false } ) => {
93- vec. last ( ) . map_or_else ( || Ok ( false ) , Self :: can_push_list_initialiser)
155+ vec. last ( ) . map_or ( Ok ( false ) , Self :: can_push_list_initialiser)
94156 }
95157 }
96158 }
97159
98- /// Applies a closure to the current [`ListInitialiser`].
99- ///
100- /// It applies the closure somewhere in the [`Node`]. If this closure
101- /// returns a value, it is returns in `Ok(_)`. If no list initialiser is
102- /// found, `Err(())` is returned.
103- ///
104- /// In the case of nested [`ListInitialiser`]s, the closure is applied to
105- /// the one closest from the leaves.
106- #[ allow( clippy:: min_ident_chars) ]
107- pub fn edit_list_initialiser < T , F : Fn ( & mut Vec < Self > , & mut bool ) -> T > (
108- & mut self ,
109- f : & F ,
110- ) -> Result < T , ( ) > {
111- match self {
112- //
113- //
114- // success
115- Self :: ListInitialiser ( ListInitialiser {
116- elts,
117- full : full @ false ,
118- } ) => {
119- if let Some ( last) = elts. last_mut ( ) {
120- if let res @ Ok ( _) = last. edit_list_initialiser ( f) {
121- return res;
122- }
123- }
124- Ok ( f ( elts, full) )
125- }
126- //
127- //
128- // failure
129- // atomic
130- Self :: Empty
131- | Self :: Leaf ( _)
132- | Self :: ParensBlock ( _)
133- // child is none
134- | Self :: Unary ( Unary { arg : None , ..} )
135- | Self :: Binary ( Binary { arg_r : None , ..} )
136- // full lists
137- | Self :: Block ( Block { full : true , ..} )
138- | Self :: FunctionCall ( FunctionCall { full : true , ..} )
139- | Self :: ListInitialiser ( ListInitialiser { full : true , ..} ) => Err ( ( ) ) ,
140- //
141- //
142- // recurse
143- Self :: Unary ( Unary { arg : Some ( arg) , .. } )
144- | Self :: Binary ( Binary { arg_r : Some ( arg) , .. } )
145- | Self :: Ternary ( Ternary { failure : Some ( arg) , .. } | Ternary { condition : arg, .. } ) => {
146- arg. edit_list_initialiser ( f)
147- }
148- //
149- //
150- // try recurse on non-full lists
151- Self :: FunctionCall ( FunctionCall {
152- full : false , args : vec, ..
153- } )
154- | Self :: Block ( Block { elts : vec, full : false } ) => vec
155- . last_mut ( )
156- . map_or_else ( || Err ( ( ) ) , |node| node. edit_list_initialiser ( f) ) ,
157- }
158- }
159-
160160 /// Adds the colon of a [`super::TernaryOperator`].
161161 ///
162162 /// This method finds a ternary operator, and changes its reading state to
@@ -205,6 +205,28 @@ impl Node {
205205 }
206206 }
207207
208+ /// Checks if a [`Node`] is pushable
209+ ///
210+ /// # Returns
211+ /// - `false` if one child on the right branch is empty
212+ /// - `true` otherwise
213+ fn is_full ( & self ) -> bool {
214+ match self {
215+ Self :: Empty => false ,
216+ Self :: Leaf ( _) | Self :: ParensBlock ( _) => false ,
217+ Self :: Unary ( Unary { arg, .. } )
218+ | Self :: Binary ( Binary { arg_r : arg, .. } )
219+ | Self :: Ternary ( Ternary { failure : arg, .. } ) => {
220+ arg. as_ref ( ) . is_some_and ( |node| node. is_full ( ) )
221+ }
222+ Self :: Block ( Block { elts : vec, full } )
223+ | Self :: ListInitialiser ( ListInitialiser { full, elts : vec } )
224+ | Self :: FunctionCall ( FunctionCall {
225+ full, args : vec, ..
226+ } ) => * full || vec. last ( ) . is_some_and ( |node| node. is_full ( ) ) ,
227+ }
228+ }
229+
208230 /// Pushes a node at the bottom of the [`Node`].
209231 ///
210232 /// This methods consideres `node` as a leaf, and pushes it as a leaf into
@@ -232,7 +254,7 @@ impl Node {
232254 // full: ok, but create a new block
233255 Self :: Block ( Block { full : true , .. } ) => {
234256 * self = Self :: Block ( Block {
235- elts : vec ! [ mem:: take( self ) ] ,
257+ elts : vec ! [ mem:: take( self ) , node ] ,
236258 full : false ,
237259 } ) ;
238260 Ok ( ( ) )
@@ -279,10 +301,12 @@ impl Node {
279301 elts : vec,
280302 full : false ,
281303 } ) => {
282- if let Some ( last) = vec. last_mut ( ) {
304+ if let Some ( last) = vec. last_mut ( )
305+ && !last. is_full ( )
306+ {
283307 last. push_block_as_leaf ( node)
284308 } else {
285- * vec = vec ! [ node] ;
309+ vec. push ( node) ;
286310 Ok ( ( ) )
287311 }
288312 }
0 commit comments