mirror of
https://github.com/danog/parser.git
synced 2025-01-22 13:01:32 +01:00
Merge pull request #87 from ryangjchandler/feature/true-false-null-return-types
This commit is contained in:
commit
b1c811a9d4
@ -10,6 +10,9 @@ pub enum Type {
|
||||
Union(Vec<ByteString>),
|
||||
Intersection(Vec<ByteString>),
|
||||
Void,
|
||||
Null,
|
||||
True,
|
||||
False,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
|
@ -57,9 +57,10 @@ impl Parser {
|
||||
|
||||
pub(crate) fn type_with_static(&mut self) -> ParseResult<ByteString> {
|
||||
Ok(match self.current.kind {
|
||||
TokenKind::Static => {
|
||||
TokenKind::Static | TokenKind::Null | TokenKind::True | TokenKind::False => {
|
||||
let str = self.current.kind.to_string();
|
||||
self.next();
|
||||
"static".into()
|
||||
str.into()
|
||||
}
|
||||
_ => self.full_name_maybe_type_keyword()?,
|
||||
})
|
||||
|
@ -163,6 +163,9 @@ impl Parser {
|
||||
|
||||
Ok(match &id[..] {
|
||||
b"void" => Type::Void,
|
||||
b"null" => Type::Null,
|
||||
b"true" => Type::True,
|
||||
b"false" => Type::False,
|
||||
_ => Type::Plain(id),
|
||||
})
|
||||
}
|
||||
@ -4104,6 +4107,48 @@ mod tests {
|
||||
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]) {
|
||||
let mut lexer = Lexer::new(None);
|
||||
let tokens = lexer.tokenize(source).unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user