diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 4df7f78..3fea28b 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -1084,6 +1084,7 @@ fn parse_int(buffer: &str, base: u32) -> Result { fn identifier_to_keyword(ident: &[u8]) -> Option { Some(match ident { + b"endwhile" => TokenKind::EndWhile, b"endforeach" => TokenKind::EndForeach, b"endif" => TokenKind::EndIf, b"from" => TokenKind::From, diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index db7b173..5178047 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -308,11 +308,23 @@ impl Parser { let condition = self.expression(Precedence::Lowest)?; self.rparen()?; - self.lbrace()?; - let body = self.block(&TokenKind::RightBrace)?; + let end_token = if self.current.kind == TokenKind::Colon { + self.colon()?; + TokenKind::EndWhile + } else { + self.lbrace()?; + TokenKind::RightBrace + }; - self.rbrace()?; + let body = self.block(&end_token)?; + + if end_token == TokenKind::RightBrace { + self.rbrace()?; + } else { + expect!(self, TokenKind::EndWhile, "expected endwhile"); + self.semi()?; + } Statement::While { condition, body } } @@ -5150,6 +5162,18 @@ mod tests { ); } + #[test] + fn short_while() { + assert_ast("