Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions trunk_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Type {
Nullable(String),
Union(Vec<String>),
Intersection(Vec<String>),
Void,
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize)]
Expand Down
59 changes: 58 additions & 1 deletion trunk_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ impl Parser {

if self.current.kind != TokenKind::Pipe {
break;
} else {
self.next();
}
}

Expand All @@ -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> {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down