mirror of
https://github.com/danog/parser.git
synced 2024-12-03 09:57:45 +01:00
parser: shorthand switch
This commit is contained in:
parent
af915e4575
commit
1d377936e0
@ -1084,6 +1084,7 @@ fn parse_int(buffer: &str, base: u32) -> Result<TokenKind, LexerError> {
|
|||||||
|
|
||||||
fn identifier_to_keyword(ident: &[u8]) -> Option<TokenKind> {
|
fn identifier_to_keyword(ident: &[u8]) -> Option<TokenKind> {
|
||||||
Some(match ident {
|
Some(match ident {
|
||||||
|
b"endswitch" => TokenKind::EndSwitch,
|
||||||
b"endfor" => TokenKind::EndFor,
|
b"endfor" => TokenKind::EndFor,
|
||||||
b"endwhile" => TokenKind::EndWhile,
|
b"endwhile" => TokenKind::EndWhile,
|
||||||
b"endforeach" => TokenKind::EndForeach,
|
b"endforeach" => TokenKind::EndForeach,
|
||||||
|
@ -770,11 +770,17 @@ impl Parser {
|
|||||||
|
|
||||||
self.rparen()?;
|
self.rparen()?;
|
||||||
|
|
||||||
self.lbrace()?;
|
let end_token = if self.current.kind == TokenKind::Colon {
|
||||||
|
self.colon()?;
|
||||||
|
TokenKind::EndSwitch
|
||||||
|
} else {
|
||||||
|
self.lbrace()?;
|
||||||
|
TokenKind::RightBrace
|
||||||
|
};
|
||||||
|
|
||||||
let mut cases = Vec::new();
|
let mut cases = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
if self.current.kind == TokenKind::RightBrace {
|
if self.current.kind == end_token {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -828,7 +834,12 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.rbrace()?;
|
if end_token == TokenKind::EndSwitch {
|
||||||
|
expect!(self, TokenKind::EndSwitch, "expected endswitch");
|
||||||
|
self.semi()?;
|
||||||
|
} else {
|
||||||
|
self.rbrace()?;
|
||||||
|
}
|
||||||
|
|
||||||
Statement::Switch { condition, cases }
|
Statement::Switch { condition, cases }
|
||||||
}
|
}
|
||||||
@ -5200,6 +5211,13 @@ mod tests {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shorthand_switch() {
|
||||||
|
assert_ast("<?php switch ($a): endswitch;", &[
|
||||||
|
Statement::Switch { condition: Expression::Variable { name: "a".into() }, cases: vec![] }
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
fn assert_ast(source: &str, expected: &[Statement]) {
|
fn assert_ast(source: &str, expected: &[Statement]) {
|
||||||
let mut lexer = Lexer::new(None);
|
let mut lexer = Lexer::new(None);
|
||||||
let tokens = lexer.tokenize(source).unwrap();
|
let tokens = lexer.tokenize(source).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user