Parses the branches of a multiline `IF` statement.
(
&mut self,
if_pos: LineCol,
branches: &mut Vec<IfBranchSpan>,
)
| 1081 | |
| 1082 | /// Parses the branches of a multiline `IF` statement. |
| 1083 | fn parse_if_multiline( |
| 1084 | &mut self, |
| 1085 | if_pos: LineCol, |
| 1086 | branches: &mut Vec<IfBranchSpan>, |
| 1087 | ) -> Result<()> { |
| 1088 | debug_assert!(!branches.is_empty(), "Caller must populate the guard of the first branch"); |
| 1089 | |
| 1090 | let mut i = 0; |
| 1091 | let mut last = false; |
| 1092 | loop { |
| 1093 | let peeked = self.lexer.peek()?; |
| 1094 | match peeked.token { |
| 1095 | Token::Eol => { |
| 1096 | self.lexer.consume_peeked(); |
| 1097 | } |
| 1098 | |
| 1099 | Token::Elseif => { |
| 1100 | if last { |
| 1101 | return Err(Error::Bad( |
| 1102 | peeked.pos, |
| 1103 | "Unexpected ELSEIF after ELSE".to_owned(), |
| 1104 | )); |
| 1105 | } |
| 1106 | |
| 1107 | self.lexer.consume_peeked(); |
| 1108 | let expr = self.parse_required_expr("No expression in ELSEIF statement")?; |
| 1109 | self.expect_and_consume(Token::Then, "No THEN in ELSEIF statement")?; |
| 1110 | self.expect_and_consume(Token::Eol, "Expecting newline after THEN")?; |
| 1111 | branches.push(IfBranchSpan { guard: expr, body: vec![] }); |
| 1112 | i += 1; |
| 1113 | } |
| 1114 | |
| 1115 | Token::Else => { |
| 1116 | if last { |
| 1117 | return Err(Error::Bad(peeked.pos, "Duplicate ELSE after ELSE".to_owned())); |
| 1118 | } |
| 1119 | |
| 1120 | let else_span = self.lexer.consume_peeked(); |
| 1121 | self.expect_and_consume(Token::Eol, "Expecting newline after ELSE")?; |
| 1122 | |
| 1123 | let expr = Expr::Boolean(BooleanSpan { value: true, pos: else_span.pos }); |
| 1124 | branches.push(IfBranchSpan { guard: expr, body: vec![] }); |
| 1125 | i += 1; |
| 1126 | |
| 1127 | last = true; |
| 1128 | } |
| 1129 | |
| 1130 | Token::End => { |
| 1131 | let token_span = self.lexer.consume_peeked(); |
| 1132 | match self.maybe_parse_end(token_span.pos)? { |
| 1133 | Ok(stmt) => { |
| 1134 | branches[i].body.push(stmt); |
| 1135 | } |
| 1136 | Err(Token::If) => { |
| 1137 | break; |
| 1138 | } |
| 1139 | Err(token) => { |
| 1140 | return Err(Error::Bad( |
no test coverage detected