Merge pull request #39 from ryangjchandler/fix/parse-more-complex-types

This commit is contained in:
Ryan Chandler 2022-09-12 13:01:03 +01:00 committed by GitHub
commit 5496276f60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 1 deletions

View File

@ -10,6 +10,7 @@ pub enum Type {
Nullable(String),
Union(Vec<String>),
Intersection(Vec<String>),
Void,
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize)]

View File

@ -132,6 +132,8 @@ impl Parser {
if self.current.kind != TokenKind::Pipe {
break;
} else {
self.next();
}
}
@ -149,13 +151,18 @@ impl Parser {
if self.current.kind != TokenKind::Ampersand {
break;
} else {
self.next();
}
}
return Ok(Type::Intersection(types));
}
Ok(Type::Plain(id))
Ok(match id.as_str() {
"void" => Type::Void,
_ => Type::Plain(id),
})
}
fn statement(&mut self) -> ParseResult<Statement> {
@ -2861,6 +2868,26 @@ mod tests {
return_type: None,
}],
);
assert_ast(
"<?php function foo(string|int|float $b) {}",
&[Statement::Function {
name: "foo".to_string().into(),
params: vec![Param {
name: Expression::Variable { name: "b".into() },
r#type: Some(Type::Union(vec![
"string".into(),
"int".into(),
"float".into(),
])),
variadic: false,
default: None,
flag: None,
}],
body: vec![],
return_type: None,
}],
);
}
#[test]
@ -2880,6 +2907,26 @@ mod tests {
return_type: None,
}],
);
assert_ast(
"<?php function foo(Foo&Bar&Baz $b) {}",
&[Statement::Function {
name: "foo".to_string().into(),
params: vec![Param {
name: Expression::Variable { name: "b".into() },
r#type: Some(Type::Intersection(vec![
"Foo".into(),
"Bar".into(),
"Baz".into(),
])),
variadic: false,
default: None,
flag: None,
}],
body: vec![],
return_type: None,
}],
);
}
#[test]
@ -2893,6 +2940,16 @@ mod tests {
return_type: Some(Type::Plain("string".into())),
}],
);
assert_ast(
"<?php function foo(): void {}",
&[Statement::Function {
name: "foo".to_string().into(),
params: vec![],
body: vec![],
return_type: Some(Type::Void),
}],
);
}
#[test]