mirror of
https://github.com/danog/parser.git
synced 2025-01-22 13:01:32 +01:00
parser: support shorthand while
This commit is contained in:
parent
f3b01d1812
commit
b534d2c9d4
@ -1084,6 +1084,7 @@ fn parse_int(buffer: &str, base: u32) -> Result<TokenKind, LexerError> {
|
||||
|
||||
fn identifier_to_keyword(ident: &[u8]) -> Option<TokenKind> {
|
||||
Some(match ident {
|
||||
b"endwhile" => TokenKind::EndWhile,
|
||||
b"endforeach" => TokenKind::EndForeach,
|
||||
b"endif" => TokenKind::EndIf,
|
||||
b"from" => TokenKind::From,
|
||||
|
@ -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("<?php while (true): $a; endwhile;", &[
|
||||
Statement::While {
|
||||
condition: Expression::Bool { value: true },
|
||||
body: vec![
|
||||
expr!(Expression::Variable { name: "a".into() })
|
||||
]
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
fn assert_ast(source: &str, expected: &[Statement]) {
|
||||
let mut lexer = Lexer::new(None);
|
||||
let tokens = lexer.tokenize(source).unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user