@@ -99,6 +99,44 @@ fn parse_if_else_statement(input: &str) -> IResult<&str, Statement> {
9999 ) ( input)
100100}
101101
102+ pub fn parse_if_chain_statement ( input : & str ) -> IResult < & str , Statement > {
103+ let ( input, _) = keyword ( IF_KEYWORD ) ( input) ?;
104+ let ( input, cond_if) = preceded ( multispace1, parse_expression) ( input) ?;
105+ let ( input, block_if) = parse_block ( input) ?;
106+
107+ let mut branches = vec ! [ ( Box :: new( cond_if) , Box :: new( block_if) ) ] ;
108+ let mut current_input = input;
109+
110+ loop {
111+ let result = tuple ( (
112+ multispace0,
113+ keyword ( ELIF_KEYWORD ) ,
114+ preceded ( multispace1, parse_expression) ,
115+ parse_block,
116+ ) ) ( current_input) ;
117+
118+ match result {
119+ Ok ( ( next_input, ( _, _, cond_elif, block_elif) ) ) => {
120+ branches. push ( ( Box :: new ( cond_elif) , Box :: new ( block_elif) ) ) ;
121+ current_input = next_input;
122+ }
123+ Err ( _) => break ,
124+ }
125+ }
126+ let ( input, else_branch) = opt ( preceded (
127+ tuple ( ( multispace0, keyword ( ELSE_KEYWORD ) ) ) ,
128+ parse_block,
129+ ) ) ( current_input) ?;
130+
131+ Ok ( (
132+ input,
133+ Statement :: IfChain {
134+ branches,
135+ else_branch : else_branch. map ( Box :: new) ,
136+ } ,
137+ ) )
138+ }
139+
102140fn parse_while_statement ( input : & str ) -> IResult < & str , Statement > {
103141 map (
104142 tuple ( (
@@ -155,7 +193,7 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
155193 keyword ( DEF_KEYWORD ) ,
156194 preceded ( multispace1, identifier) ,
157195 delimited (
158- char:: <& str, Error <& str>>( LEFT_PAREN ) ,
196+ char : // Tenta parsear um else opcional : <& str, Error <& str>>( LEFT_PAREN ) ,
159197 separated_list0 (
160198 tuple ( (
161199 multispace0,
@@ -170,7 +208,7 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
170208 preceded ( multispace0, parse_type) ,
171209 parse_block,
172210 ) ) ,
173- |( _, name, args , _, t, block) | {
211+ |( _, name, ar // Tenta parsear um else opcionalgs , _, t, block)| {
174212 Statement :: FuncDef ( Function {
175213 name : name. to_string( ) ,
176214 kind : t,
0 commit comments