@@ -13,7 +13,7 @@ use crate::lexer::api::Token;
1313use crate :: parser:: keyword:: control_flow:: node:: {
1414 switch_wanting_block, try_push_semicolon_control
1515} ;
16- use crate :: parser:: modifiers:: functions:: MakeFunction as _;
16+ use crate :: parser:: modifiers:: functions:: { CanMakeFnRes , MakeFunction as _} ;
1717use crate :: parser:: modifiers:: list_initialiser:: {
1818 apply_to_last_list_initialiser, can_push_list_initialiser
1919} ;
@@ -148,44 +148,74 @@ fn handle_parenthesis_open(
148148 tokens : & mut IntoIter < Token > ,
149149 location : ErrorLocation ,
150150) -> Res < ( ) > {
151- if let Some ( function_depth) = current. can_make_function ( ) {
152- let mut arguments_node = Ast :: FunctionArgsBuild ( vec ! [ Ast :: Empty ] ) ;
153- p_state. push_ctrl_flow ( false ) ;
154- parse_block ( tokens, p_state, & mut arguments_node) ?;
155- if p_state. pop_ctrl_flow ( ) . is_none ( ) {
156- return Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location) ) ;
157- }
158- if p_state. pop_and_compare_block ( & BlockType :: Parenthesis ) {
159- if let Ast :: FunctionArgsBuild ( vec) = & mut arguments_node {
160- let mut error = None ;
161- if vec. last ( ) . is_some_and ( Ast :: is_empty) {
162- vec. pop ( ) ;
163- if !vec. is_empty ( ) {
164- error = Some ( location. to_suggestion (
165- "Found extra comma in function argument list. Please remove the comma." . to_owned ( ) ,
166- ) ) ;
167- }
151+ match current. can_make_function ( ) {
152+ CanMakeFnRes :: CanMakeFn ( variable_depth) =>
153+ make_function ( current, p_state, tokens, location, variable_depth) ,
154+ CanMakeFnRes :: None =>
155+ handle_non_function_parenthesis_open ( current, p_state, tokens, location) ,
156+ CanMakeFnRes :: TooDeep => Res :: from (
157+ location. into_crash ( "Code to complex: AST to deep to fit depth in 32 bits." . to_owned ( ) ) ,
158+ ) ,
159+ }
160+ }
161+
162+ /// Create a function for the found '('
163+ ///
164+ /// Builds a function on a variable and adds its arguments.
165+ fn make_function (
166+ current : & mut Ast ,
167+ p_state : & mut ParsingState ,
168+ tokens : & mut IntoIter < Token > ,
169+ location : ErrorLocation ,
170+ variable_depth : u32 ,
171+ ) -> Res < ( ) > {
172+ let mut arguments_node = Ast :: FunctionArgsBuild ( vec ! [ Ast :: Empty ] ) ;
173+ p_state. push_ctrl_flow ( false ) ;
174+ parse_block ( tokens, p_state, & mut arguments_node) ?;
175+ if p_state. pop_ctrl_flow ( ) . is_none ( ) {
176+ return Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location) ) ;
177+ }
178+ if p_state. pop_and_compare_block ( & BlockType :: Parenthesis ) {
179+ if let Ast :: FunctionArgsBuild ( vec) = & mut arguments_node {
180+ let mut error = None ;
181+ if vec. last ( ) . is_some_and ( Ast :: is_empty) {
182+ vec. pop ( ) ;
183+ if !vec. is_empty ( ) {
184+ error = Some (
185+ location. to_suggestion (
186+ "Found extra comma in function argument list. Please remove the comma."
187+ . to_owned ( ) ,
188+ ) ,
189+ ) ;
168190 }
169- current. make_function ( function_depth, mem:: take ( vec) ) ;
170- parse_block ( tokens, p_state, current) . add_err ( error)
171- } else {
172- unreachable ! ( "a function args build cannot be dismissed as root" ) ;
173191 }
192+ current. make_function ( variable_depth, mem:: take ( vec) ) ;
193+ parse_block ( tokens, p_state, current) . add_err ( error)
174194 } else {
175- Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location ) )
195+ unreachable ! ( "a function args build cannot be dismissed as root" ) ;
176196 }
177197 } else {
178- let mut parenthesized_block = Ast :: Empty ;
179- parse_block ( tokens, p_state, & mut parenthesized_block) ?;
180- parenthesized_block. fill ( ) ;
181- if p_state. pop_and_compare_block ( & BlockType :: Parenthesis ) {
182- current
183- . push_block_as_leaf ( ParensBlock :: make_parens_ast ( parenthesized_block) )
184- . map_err ( |err| location. into_crash ( err) ) ?;
185- parse_block ( tokens, p_state, current)
186- } else {
187- Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location) )
188- }
198+ Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location) )
199+ }
200+ }
201+
202+ /// Handles an opening '(', but when it can't be a function call.
203+ fn handle_non_function_parenthesis_open (
204+ current : & mut Ast ,
205+ p_state : & mut ParsingState ,
206+ tokens : & mut IntoIter < Token > ,
207+ location : ErrorLocation ,
208+ ) -> Res < ( ) > {
209+ let mut parenthesized_block = Ast :: Empty ;
210+ parse_block ( tokens, p_state, & mut parenthesized_block) ?;
211+ parenthesized_block. fill ( ) ;
212+ if p_state. pop_and_compare_block ( & BlockType :: Parenthesis ) {
213+ current
214+ . push_block_as_leaf ( ParensBlock :: make_parens_ast ( parenthesized_block) )
215+ . map_err ( |err| location. into_crash ( err) ) ?;
216+ parse_block ( tokens, p_state, current)
217+ } else {
218+ Res :: from ( BlockType :: Parenthesis . mismatched_err_end ( location) )
189219 }
190220}
191221
0 commit comments