diff --git a/phpast/samples/if.php b/phpast/samples/if.php new file mode 100644 index 0000000..7c55694 --- /dev/null +++ b/phpast/samples/if.php @@ -0,0 +1,7 @@ + }, Return { value: Option, diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index feed6a7..ac987df 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -354,7 +354,22 @@ impl Parser { expect!(self, TokenKind::RightBrace, "expected }"); } - Statement::If { condition, then } + if self.current.kind != TokenKind::Else { + return Ok(Statement::If { condition, then, r#else: None }); + } + + expect!(self, TokenKind::Else, "expected else"); + + expect!(self, TokenKind::LeftBrace, "expected {"); + + let mut r#else = Block::new(); + while ! self.is_eof() && self.current.kind != TokenKind::RightBrace { + r#else.push(self.statement()?); + } + + expect!(self, TokenKind::RightBrace, "expected }"); + + Statement::If { condition, then, r#else: Some(r#else) } }, TokenKind::Class => self.class()?, TokenKind::Echo => { @@ -1117,6 +1132,7 @@ mod tests { then: vec![ Statement::Return { value: Some(Expression::Variable("n".into())) } ], + r#else: None }, Statement::Return { value: Some(Expression::Infix( @@ -1149,16 +1165,28 @@ mod tests { #[test] fn one_liner_if_statement() { - assert_ast("\ -