Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const Position = require("./ast/position");
* - [Namespace](#namespace)
* - [PropertyStatement](#propertystatement)
* - [Property](#property)
* - [PropertyHook](#propertyhook)
* - [Declaration](#declaration)
* - [Class](#class)
* - [Interface](#interface)
Expand Down Expand Up @@ -557,6 +558,7 @@ AST.prototype.checkNodes = function () {
require("./ast/print"),
require("./ast/program"),
require("./ast/property"),
require("./ast/propertyhook"),
require("./ast/propertylookup"),
require("./ast/propertystatement"),
require("./ast/reference"),
Expand Down
3 changes: 3 additions & 0 deletions src/ast/parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const KIND = "parameter";
* @property {boolean} nullable
* @property {AttrGroup[]} attrGroups
* @property {MODIFIER_PUBLIC|MODIFIER_PROTECTED|MODIFIER_PRIVATE} flags
* @property {PropertyHook[]} hooks
*/
module.exports = Declaration.extends(
KIND,
Expand All @@ -45,6 +46,7 @@ module.exports = Declaration.extends(
readonly,
nullable,
flags,
hooks,
docs,
location,
) {
Expand All @@ -56,6 +58,7 @@ module.exports = Declaration.extends(
this.readonly = readonly;
this.nullable = nullable;
this.flags = flags || 0;
this.hooks = hooks || [];
this.attrGroups = [];
},
);
3 changes: 3 additions & 0 deletions src/ast/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const KIND = "property";
* @property {boolean} nullable
* @property {Identifier|Array<Identifier>|null} type
* @property {AttrGroup[]} attrGroups
* @property {PropertyHook[]} hooks
*/
module.exports = Statement.extends(
KIND,
Expand All @@ -29,6 +30,7 @@ module.exports = Statement.extends(
nullable,
type,
attrGroups,
hooks,
docs,
location,
) {
Expand All @@ -39,5 +41,6 @@ module.exports = Statement.extends(
this.nullable = nullable;
this.type = type;
this.attrGroups = attrGroups;
this.hooks = hooks || [];
},
);
44 changes: 44 additions & 0 deletions src/ast/propertyhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2024 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Node = require("./node");
const KIND = "propertyhook";

/**
* Defines a class property hook getter & setter
*
* @constructor PropertyHook
* @memberOf module:php-parser
* @extends {Node}
* @property {string} name
* @property {Boolean} isFinal
* @property {Boolean} byref
* @property {Parameter|null} parameter
* @property {Block|Expression|null} body
* @property {AttrGroup[]} attrGroups
*/
module.exports = Node.extends(
KIND,
function PropertyHook(
name,
isFinal,
byref,
parameter,
body,
attrGroups,
docs,
location,
) {
Node.apply(this, [KIND, docs, location]);
this.name = name;
this.isFinal = isFinal;
this.byref = byref;
this.parameter = parameter;
this.body = body;
this.attrGroups = attrGroups || [];
},
);
4 changes: 4 additions & 0 deletions src/ast/propertystatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const IS_PRIVATE = "private";
* @property {Property[]} properties
* @property {string|null} visibility
* @property {boolean} isStatic
* @property {boolean} isAbstract
* @property {boolean} isFinal
*/
const PropertyStatement = Statement.extends(
KIND,
Expand Down Expand Up @@ -52,6 +54,8 @@ PropertyStatement.prototype.parseFlags = function (flags) {
}

this.isStatic = flags[1] === 1;
this.isAbstract = flags[2] === 1;
this.isFinal = flags[2] === 2;
};

module.exports = PropertyStatement;
128 changes: 121 additions & 7 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ module.exports = {
// reads a variable
const variables = this.read_variable_list(flags, attrs);
attrs = [];
this.expect(";");
this.next();
result = result.concat(variables);
} else {
// raise an error
Expand All @@ -178,7 +176,7 @@ module.exports = {
* ```
*/
read_variable_list(flags, attrs) {
const result = this.node("propertystatement");
let property_statement = this.node("propertystatement");

const properties = this.read_list(
/*
Expand All @@ -203,19 +201,133 @@ module.exports = {
propName = propName(name);

let value = null;
let property_hooks = [];

this.expect([",", ";", "="]);
this.expect([",", ";", "=", "{"]);

// Property has a value
if (this.token === "=") {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815
value = this.next().read_expr();
}
return result(propName, value, readonly, nullable, type, attrs || []);

// Property is using a hook to define getter/setters
if (this.token === "{") {
property_hooks = this.read_property_hooks();
} else {
this.expect([";", ","]);
}

return result(
propName,
value,
readonly,
nullable,
type,
attrs || [],
property_hooks,
);
},
",",
);

return result(null, properties, flags);
property_statement = property_statement(null, properties, flags);

// semicolons are found only for regular properties definitions.
// Property hooks are terminated by a closing curly brace, }.
// property_statement is instanciated before this check to avoid including the semicolon in the AST end location of the property.
if (this.token === ";") {
this.next();
}
return property_statement;
},

/*
* Reads property hooks
*/
read_property_hooks() {
if (this.version < 804) {
this.raiseError("Parse Error: Property hooks require PHP 8.4+");
}
this.expect("{");
this.next();

const hooks = [];

while (this.token !== this.EOF && this.token !== "}") {
hooks.push(this.read_property_hook());
}

this.expect("}");
this.next();
return hooks;
},

read_property_hook() {
const property_hooks = this.node("propertyhook");

let attrs = [];
if (this.token === this.tok.T_ATTRIBUTE) {
attrs = this.read_attr_list();
}

const is_final = this.token === this.tok.T_FINAL;
if (is_final) this.next();

const is_reference = this.token === "&";
if (is_reference) this.next();

const method_name = this.text();

if (method_name !== "get" && method_name !== "set") {
this.raiseError(
"Parse Error: Property hooks must be either 'get' or 'set'",
);
}
this.next();

let parameter = null;
let body = null;
this.expect([this.tok.T_DOUBLE_ARROW, "{", "(", ";"]);

// interface or abstract definition
if (this.token === ";") {
this.next();
return property_hooks(
method_name,
is_final,
is_reference,
parameter,
body,
attrs,
);
}

if (this.token === "(") {
this.next();
parameter = this.read_parameter(false);
this.expect(")");
this.next();
}

if (this.token === this.tok.T_DOUBLE_ARROW) {
this.next();
body = this.read_expr();
this.next();
} else if (this.token === "{") {
body = this.read_code_block();
}

return property_hooks(
method_name,
is_final,
is_reference,
parameter,
body,
attrs,
);
},

/*
* Reads constant list
* ```ebnf
Expand Down Expand Up @@ -476,9 +588,11 @@ module.exports = {
if (this.expect(";")) {
this.next();
}
} else if (this.token === this.tok.T_STRING) {
result.push(this.read_variable_list(flags, attrs));
} else {
// raise an error
this.error([this.tok.T_CONST, this.tok.T_FUNCTION]);
this.error([this.tok.T_CONST, this.tok.T_FUNCTION, this.tok.T_STRING]);
this.next();
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ module.exports = {
if (this.token == "=") {
value = this.next().read_expr();
}
let hooks = [];
if (this.version >= 804 && flags && this.token === "{") {
hooks = this.read_property_hooks();
}
const result = node(
parameterName,
types,
Expand All @@ -315,6 +319,7 @@ module.exports = {
readonly,
nullable,
flags,
hooks,
);
if (attrs) result.attrGroups = attrs;
return result;
Expand Down
5 changes: 5 additions & 0 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ Program {
"visibility": "",
},
PropertyStatement {
"isAbstract": false,
"isFinal": false,
"isStatic": false,
"kind": "propertystatement",
"loc": Location {
Expand All @@ -852,6 +854,7 @@ Program {
"properties": [
Property {
"attrGroups": [],
"hooks": [],
"kind": "property",
"loc": Location {
"end": Position {
Expand Down Expand Up @@ -2173,6 +2176,7 @@ Program {
"attrGroups": [],
"byref": false,
"flags": 0,
"hooks": [],
"kind": "parameter",
"loc": Location {
"end": Position {
Expand Down Expand Up @@ -4135,6 +4139,7 @@ next:
"attrGroups": [],
"byref": false,
"flags": 0,
"hooks": [],
"kind": "parameter",
"loc": Location {
"end": Position {
Expand Down
Loading
Loading