Merge pull request #87 from ryangjchandler/feature/true-false-null-return-types

This commit is contained in:
Ryan Chandler 2022-09-15 01:27:29 +01:00 committed by GitHub
commit b1c811a9d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -10,6 +10,9 @@ pub enum Type {
Union(Vec<ByteString>), Union(Vec<ByteString>),
Intersection(Vec<ByteString>), Intersection(Vec<ByteString>),
Void, Void,
Null,
True,
False,
} }
#[derive(Debug, Eq, PartialEq, Clone)] #[derive(Debug, Eq, PartialEq, Clone)]

View File

@ -57,9 +57,10 @@ impl Parser {
pub(crate) fn type_with_static(&mut self) -> ParseResult<ByteString> { pub(crate) fn type_with_static(&mut self) -> ParseResult<ByteString> {
Ok(match self.current.kind { Ok(match self.current.kind {
TokenKind::Static => { TokenKind::Static | TokenKind::Null | TokenKind::True | TokenKind::False => {
let str = self.current.kind.to_string();
self.next(); self.next();
"static".into() str.into()
} }
_ => self.full_name_maybe_type_keyword()?, _ => self.full_name_maybe_type_keyword()?,
}) })

View File

@ -163,6 +163,9 @@ impl Parser {
Ok(match &id[..] { Ok(match &id[..] {
b"void" => Type::Void, b"void" => Type::Void,
b"null" => Type::Null,
b"true" => Type::True,
b"false" => Type::False,
_ => Type::Plain(id), _ => Type::Plain(id),
}) })
} }
@ -4104,6 +4107,48 @@ mod tests {
assert_ast("<?php a:", &[Statement::Label { label: "a".into() }]); assert_ast("<?php a:", &[Statement::Label { label: "a".into() }]);
} }
#[test]
fn null_return_type() {
assert_ast(
"<?php function a(): null {}",
&[Statement::Function {
name: "a".into(),
params: vec![],
body: vec![],
return_type: Some(Type::Null),
by_ref: false,
}],
);
}
#[test]
fn true_return_type() {
assert_ast(
"<?php function a(): true {}",
&[Statement::Function {
name: "a".into(),
params: vec![],
body: vec![],
return_type: Some(Type::True),
by_ref: false,
}],
);
}
#[test]
fn false_return_type() {
assert_ast(
"<?php function a(): false {}",
&[Statement::Function {
name: "a".into(),
params: vec![],
body: vec![],
return_type: Some(Type::False),
by_ref: false,
}],
);
}
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();