parser: support continue statements

This commit is contained in:
Ryan Chandler 2022-07-27 21:14:32 +01:00
parent 3ad45e6d1e
commit e1eff91ee7
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 29 additions and 0 deletions

View File

@ -623,6 +623,7 @@ fn identifier_to_keyword(ident: &str) -> Option<TokenKind> {
"as" => TokenKind::As,
"break" => TokenKind::Break,
"class" => TokenKind::Class,
"continue" => TokenKind::Continue,
"const" => TokenKind::Const,
"declare" => TokenKind::Declare,
"echo" => TokenKind::Echo,

View File

@ -187,6 +187,9 @@ pub enum Statement {
Break {
num: Option<Expression>,
},
Continue {
num: Option<Expression>,
},
Echo {
values: Vec<Expression>,
},

View File

@ -361,6 +361,20 @@ impl Parser {
expect!(self, TokenKind::SemiColon, "expected semi-colon at the end of an echo statement");
Statement::Echo { values }
},
TokenKind::Continue => {
self.next();
let mut num = None;
if self.current.kind != TokenKind::SemiColon {
num = Some(self.expression(0)?);
}
dbg!(&self.current.kind);
expect!(self, TokenKind::SemiColon, "expected semi-colon");
Statement::Continue { num }
},
TokenKind::Break => {
self.next();
@ -1007,6 +1021,17 @@ mod tests {
]);
}
#[test]
fn continues() {
assert_ast("<?php continue;", &[
Statement::Continue { num: None }
]);
assert_ast("<?php continue 2;", &[
Statement::Continue { num: Some(Expression::Int(2)) }
]);
}
#[test]
fn math_precedence() {
assert_ast("<?php 1 + 2 * 3 / 4 - 5;", &[