Skip to content

Commit

Permalink
refactor: byRef
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 31, 2019
1 parent 62973ef commit 98f510e
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 107 deletions.
2 changes: 2 additions & 0 deletions src/ast.js
Expand Up @@ -46,6 +46,7 @@ const Position = require("./ast/position");
* - [Exit](#exit)
* - [Clone](#clone)
* - [Assign](#assign)
* - [AssignRef](#assignref)
* - [Array](#array)
* - [List](#list)
* - [Variable](#variable)
Expand Down Expand Up @@ -458,6 +459,7 @@ AST.prototype.checkNodes = function() {
[
require("./ast/array"),
require("./ast/assign"),
require("./ast/assignref"),
require("./ast/bin"),
require("./ast/block"),
require("./ast/boolean"),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/assign.js
Expand Up @@ -24,7 +24,7 @@ module.exports = Expression.extends(KIND, function Assign(
location
) {
Expression.apply(this, [KIND, docs, location]);
this.operator = operator;
this.left = left;
this.right = right;
this.operator = operator;
});
28 changes: 28 additions & 0 deletions src/ast/assignref.js
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Expression = require("./expression");
const KIND = "assignref";

/**
* Assigns a value to the specified target
* @constructor Assign
* @extends {Expression}
* @property {Expression} left
* @property {Expression} right
* @property {String} operator
*/
module.exports = Expression.extends(KIND, function AssignRef(
left,
right,
docs,
location
) {
Expression.apply(this, [KIND, docs, location]);
this.left = left;
this.right = right;
});
26 changes: 12 additions & 14 deletions src/parser/expr.js
Expand Up @@ -354,21 +354,19 @@ module.exports = {
if (isConst) this.error("VARIABLE");
let right;
if (this.next().token == "&") {
right = this.read_byref(
function() {
if (this.token === this.tok.T_NEW) {
if (this.php7) {
this.error();
}
return this.read_new_expr();
} else {
return this.read_variable(false, false);
}
}.bind(this)
);
} else {
right = this.read_expr();
this.next();
if (this.token === this.tok.T_NEW) {
if (this.php7) {
this.error();
}
right = this.read_new_expr();
} else {
right = this.read_variable(false, false);
}

return result("assignref", expr, right);
}
right = this.read_expr();
return result("assign", expr, right, "=");
}

Expand Down
6 changes: 2 additions & 4 deletions test/snapshot/__snapshots__/assign.test.js.snap
Expand Up @@ -402,16 +402,14 @@ exports[`assign with ref 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"expression": AssignRef {
"kind": "assignref",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
"operator": "=",
"right": Variable {
"byref": true,
"curly": false,
"kind": "variable",
"name": "foo",
Expand Down

0 comments on commit 98f510e

Please sign in to comment.