Skip to content

Commit

Permalink
feat: spread_operator_for_array
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 11, 2019
1 parent d5e7dcf commit bc3ee86
Show file tree
Hide file tree
Showing 20 changed files with 4,665 additions and 1,056 deletions.
6 changes: 6 additions & 0 deletions src/ast/entry.js
Expand Up @@ -14,14 +14,20 @@ const KIND = "entry";
* @extends {Expression}
* @property {Node|null} key The entry key/offset
* @property {Node} value The entry value
* @property {Boolean} byRef By reference
* @property {Boolean} unpack Argument unpacking
*/
module.exports = Expression.extends(KIND, function Entry(
key,
value,
byRef,
unpack,
docs,
location
) {
Expression.apply(this, [KIND, docs, location]);
this.key = key;
this.value = value;
this.byRef = byRef;
this.unpack = unpack;
});
43 changes: 29 additions & 14 deletions src/parser/array.js
Expand Up @@ -5,9 +5,6 @@
*/
"use strict";

const ArrayExpr = "array";
const ArrayEntry = "entry";

module.exports = {
/**
* Parse an array
Expand All @@ -19,7 +16,7 @@ module.exports = {
read_array: function() {
let expect = null;
let shortForm = false;
const result = this.node(ArrayExpr);
const result = this.node("array");

if (this.token === this.tok.T_ARRAY) {
this.next().expect("(");
Expand Down Expand Up @@ -69,27 +66,45 @@ module.exports = {
) {
return;
}

if (this.token === ",") {
return this.node("noop")();
}

const entry = this.node("entry");

let key = null;
let value = null;
let byRef = false;
let unpack = false;

if (this.token === "&") {
return this.read_byref(this.read_variable.bind(this, true, false));
this.next();
byRef = true;
value = this.read_variable(true, false);
} else if (this.token === this.tok.T_ELLIPSIS && this.php74) {
this.next();
unpack = true;
value = this.read_expr()
} else {
const entry = this.node(ArrayEntry);
const expr = this.read_expr();

if (this.token === this.tok.T_DOUBLE_ARROW) {
if (this.next().token === "&") {
return entry(
expr,
this.read_byref(this.read_variable.bind(this, true, false))
);
this.next();
key = expr;

if (this.token === "&") {
this.next();
byRef = true;
value = this.read_variable(true, false);
} else {
return entry(expr, this.read_expr());
value = this.read_expr();
}
} else {
entry.destroy();
value = expr;
}
return expr;
}

return entry(key, value, byRef, unpack);
}
};

0 comments on commit bc3ee86

Please sign in to comment.