Skip to content

Commit

Permalink
Added support for union types in function parameters & function retur…
Browse files Browse the repository at this point in the history
…n types
  • Loading branch information
cseufert committed Mar 22, 2021
1 parent 7de81ca commit 968da4e
Show file tree
Hide file tree
Showing 12 changed files with 818 additions and 467 deletions.
2 changes: 1 addition & 1 deletion src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ module.exports = {
nullable = true;
this.next();
}
returnType = this.read_type();
returnType = this.read_types();
}
if (this.expect(this.tok.T_DOUBLE_ARROW)) this.next();
const body = this.read_expr();
Expand Down
22 changes: 17 additions & 5 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ module.exports = {
nullable = true;
this.next();
}
returnType = this.read_type();
returnType = this.read_types();
}
if (type === 1) {
// closure
Expand Down Expand Up @@ -202,14 +202,14 @@ module.exports = {
const node = this.node("parameter");
let parameterName = null;
let value = null;
let type = null;
let types = null;
let nullable = false;
if (this.token === "?") {
this.next();
nullable = true;
}
type = this.read_type();
if (nullable && !type) {
types = this.read_types();
if (nullable && !types) {
this.raiseError(
"Expecting a type definition combined with nullable operator"
);
Expand All @@ -225,7 +225,19 @@ module.exports = {
if (this.token == "=") {
value = this.next().read_expr();
}
return node(parameterName, type, value, isRef, isVariadic, nullable);
return node(parameterName, types, value, isRef, isVariadic, nullable);
},
read_types() {
const types = [];
let type = this.read_type();
if (!type) return null;
types.push(type);
while (this.token === "|") {
this.next();
type = this.read_type();
types.push(type);
}
return types.length === 0 ? null : types;
},
/**
* Reads a list of arguments
Expand Down
Loading

0 comments on commit 968da4e

Please sign in to comment.