Skip to content

Commit

Permalink
[PHP8] Set PHPDocBlock on attributed IPHPDocAwareDeclartions
Browse files Browse the repository at this point in the history
  • Loading branch information
zulus committed Oct 4, 2023
1 parent 2c5def3 commit b6a47ac
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ NOWDOC_CHARS=([^\n\r]|({NEWLINE}{TABS_AND_SPACES})+[^a-zA-Z_\u0080-\uffff\n\r \t
}

<ST_IN_SCRIPTING>"#[" {
return createSymbol(ParserConstants.T_ATTRIBUTE);
return createFullSymbol(ParserConstants.T_ATTRIBUTE);
}

<ST_IN_SCRIPTING>"->" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ parser code {:
this.doc = doc;
}
}


class NodeDocPair<T> {
public T node;
public PHPDocBlock doc;

public NodeDocPair(T node, PHPDocBlock doc) {
this.node = node;
this.doc = doc;
}
}

protected Expression detectScalar(Expression expr) {
if (expr instanceof FullyQualifiedReference) {
FullyQualifiedReference name = (FullyQualifiedReference) expr;
Expand All @@ -38,11 +48,17 @@ parser code {:
return expr;
}

protected void setAttributes(ASTNode target, List<Attribute> list)
protected void setAttributes(ASTNode target, NodeDocPair<List<Attribute>> list)
{
((IAttributed)target).setAttributes(list);
Attribute group = list.get(0);
((IAttributed)target).setAttributes(list.node);
Attribute group = list.node.get(0);
target.setStart(group.start());
if (list.doc != null && target instanceof IPHPDocAwareDeclaration) {
IPHPDocAwareDeclaration decl = (IPHPDocAwareDeclaration)target;
if (decl.getPHPDoc() == null) {
decl.setPHPDoc(list.doc);
}
}
}

protected int getElementTypeByUseType(int useType) {
Expand Down Expand Up @@ -212,7 +228,7 @@ terminal CompilerAstLexer.PHPDocBlockSymbolPair T_FN;
terminal PREC_ARROW_FUNCTION;

//php 8.0
terminal T_ATTRIBUTE;
terminal CompilerAstLexer.PHPDocBlockSymbolPair T_ATTRIBUTE;
terminal String T_MATCH;
terminal T_NULLSAFE_OBJECT_OPERATOR;
terminal String T_NAME_FULLY_QUALIFIED;
Expand Down Expand Up @@ -369,8 +385,8 @@ non terminal YieldExpression yield_expr;
//php8.0
non terminal Attribute attribute_decl;
non terminal List<Attribute> attribute_group;
non terminal List<Attribute> attribute;
non terminal List<Attribute> attributes;
non terminal NodeDocPair<List<Attribute>> attribute;
non terminal NodeDocPair<List<Attribute>> attributes;
non terminal Statement attributed_statement;
non terminal FormalParameter attributed_parameter;
non terminal Expression inline_function;
Expand Down Expand Up @@ -808,24 +824,25 @@ attribute_group:list T_COMMA attribute_decl:decl
:}
;
attribute ::=
T_ATTRIBUTE attribute_group:list possible_comma T_CLOSE_RECT
T_ATTRIBUTE:s attribute_group:list possible_comma T_CLOSE_RECT
{:
RESULT = list;
RESULT = new NodeDocPair<List<Attribute>>(list, s.doc);
:}
;

attributes ::=
attributes:attributesList attribute:attr
{:
attributesList.addAll(attr);
attributesList.node.addAll(attr.node);
if (attr.doc != null) {
attributesList.doc = attr.doc;
}

RESULT = attributesList;
:}
| attribute:attr
{:
List<Attribute> attributesList = new LinkedList<Attribute>(attr);

RESULT = attributesList;
{:
RESULT = attr;
:}
;

Expand Down Expand Up @@ -3067,7 +3084,7 @@ T_NEW:start class_name_reference:className ctor_arguments:ctor
| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
Expression className = new SimpleReference(tclassleft, tclassright, "class");
ac.setAttributes(attrs); //special case
ac.setAttributes(attrs.node); //special case
RESULT = new ClassInstanceCreation(startleft, acright, className, ctor, ac);
:}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ NOWDOC_CHARS=([^\n\r]|({NEWLINE}{TABS_AND_SPACES})+[^a-zA-Z_\u0080-\uffff\n\r \t
}

<ST_IN_SCRIPTING>"#[" {
return createSymbol(ParserConstants.T_ATTRIBUTE);
return createFullSymbol(ParserConstants.T_ATTRIBUTE);
}

<ST_IN_SCRIPTING>"->" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ parser code {:
this.doc = doc;
}
}

class NodeDocPair<T> {
public T node;
public PHPDocBlock doc;

public NodeDocPair(T node, PHPDocBlock doc) {
this.node = node;
this.doc = doc;
}
}

protected Expression detectScalar(Expression expr) {
if (expr instanceof FullyQualifiedReference) {
Expand All @@ -39,11 +49,17 @@ parser code {:
return expr;
}

protected void setAttributes(ASTNode target, List<Attribute> list)
protected void setAttributes(ASTNode target, NodeDocPair<List<Attribute>> list)
{
((IAttributed)target).setAttributes(list);
Attribute group = list.get(0);
((IAttributed)target).setAttributes(list.node);
Attribute group = list.node.get(0);
target.setStart(group.start());
if (list.doc != null && target instanceof IPHPDocAwareDeclaration) {
IPHPDocAwareDeclaration decl = (IPHPDocAwareDeclaration)target;
if (decl.getPHPDoc() == null) {
decl.setPHPDoc(list.doc);
}
}
}

protected int getElementTypeByUseType(int useType) {
Expand Down Expand Up @@ -213,7 +229,7 @@ terminal CompilerAstLexer.PHPDocBlockSymbolPair T_FN;
terminal PREC_ARROW_FUNCTION;

//php 8.0
terminal T_ATTRIBUTE;
terminal CompilerAstLexer.PHPDocBlockSymbolPair T_ATTRIBUTE;
terminal String T_MATCH;
terminal T_NULLSAFE_OBJECT_OPERATOR;
terminal String T_NAME_FULLY_QUALIFIED;
Expand Down Expand Up @@ -376,8 +392,8 @@ non terminal YieldExpression yield_expr;
//php8.0
non terminal Attribute attribute_decl;
non terminal List<Attribute> attribute_group;
non terminal List<Attribute> attribute;
non terminal List<Attribute> attributes;
non terminal NodeDocPair<List<Attribute>> attribute;
non terminal NodeDocPair<List<Attribute>> attributes;
non terminal Statement attributed_statement;
non terminal FormalParameter attributed_parameter;
non terminal Expression inline_function;
Expand Down Expand Up @@ -851,24 +867,25 @@ attribute_group:list T_COMMA attribute_decl:decl
:}
;
attribute ::=
T_ATTRIBUTE attribute_group:list possible_comma T_CLOSE_RECT
T_ATTRIBUTE:s attribute_group:list possible_comma T_CLOSE_RECT
{:
RESULT = list;
RESULT = new NodeDocPair<List<Attribute>>(list, s.doc);
:}
;

attributes ::=
attributes:attributesList attribute:attr
{:
attributesList.addAll(attr);
attributesList.node.addAll(attr.node);
if (attr.doc != null) {
attributesList.doc = attr.doc;
}

RESULT = attributesList;
:}
| attribute:attr
{:
List<Attribute> attributesList = new LinkedList<Attribute>(attr);

RESULT = attributesList;
{:
RESULT = attr;
:}
;

Expand Down Expand Up @@ -3286,7 +3303,7 @@ T_NEW:start class_name_reference:className ctor_arguments:ctor
| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
Expression className = new SimpleReference(tclassleft, tclassright, "class");
ac.setAttributes(attrs);
ac.setAttributes(attrs.node);
RESULT = new ClassInstanceCreation(startleft, acright, className, ctor, ac);
:}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ NOWDOC_CHARS=([^\n\r]|({NEWLINE}{TABS_AND_SPACES})+[^a-zA-Z_\u0080-\uffff\n\r \t
}

<ST_IN_SCRIPTING>"#[" {
return createSymbol(ParserConstants.T_ATTRIBUTE);
return createFullSymbol(ParserConstants.T_ATTRIBUTE);
}

<ST_IN_SCRIPTING>"->" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ parser code {:
this.doc = doc;
}
}

class NodeDocPair<T> {
public T node;
public PHPDocBlock doc;

public NodeDocPair(T node, PHPDocBlock doc) {
this.node = node;
this.doc = doc;
}
}

protected Expression detectScalar(Expression expr) {
if (expr instanceof FullyQualifiedReference) {
Expand All @@ -39,11 +49,17 @@ parser code {:
return expr;
}

protected void setAttributes(ASTNode target, List<Attribute> list)
protected void setAttributes(ASTNode target, NodeDocPair<List<Attribute>> list)
{
((IAttributed)target).setAttributes(list);
Attribute group = list.get(0);
((IAttributed)target).setAttributes(list.node);
Attribute group = list.node.get(0);
target.setStart(group.start());
if (list.doc != null && target instanceof IPHPDocAwareDeclaration) {
IPHPDocAwareDeclaration decl = (IPHPDocAwareDeclaration)target;
if (decl.getPHPDoc() == null) {
decl.setPHPDoc(list.doc);
}
}
}

protected int getElementTypeByUseType(int useType) {
Expand Down Expand Up @@ -213,7 +229,7 @@ terminal CompilerAstLexer.PHPDocBlockSymbolPair T_FN;
terminal PREC_ARROW_FUNCTION;

//php 8.0
terminal T_ATTRIBUTE;
terminal CompilerAstLexer.PHPDocBlockSymbolPair T_ATTRIBUTE;
terminal String T_MATCH;
terminal T_NULLSAFE_OBJECT_OPERATOR;
terminal String T_NAME_FULLY_QUALIFIED;
Expand Down Expand Up @@ -376,8 +392,8 @@ non terminal YieldExpression yield_expr;
//php8.0
non terminal Attribute attribute_decl;
non terminal List<Attribute> attribute_group;
non terminal List<Attribute> attribute;
non terminal List<Attribute> attributes;
non terminal NodeDocPair<List<Attribute>> attribute;
non terminal NodeDocPair<List<Attribute>> attributes;
non terminal Statement attributed_statement;
non terminal FormalParameter attributed_parameter;
non terminal Expression inline_function;
Expand Down Expand Up @@ -858,24 +874,25 @@ attribute_group:list T_COMMA attribute_decl:decl
:}
;
attribute ::=
T_ATTRIBUTE attribute_group:list possible_comma T_CLOSE_RECT
T_ATTRIBUTE:s attribute_group:list possible_comma T_CLOSE_RECT
{:
RESULT = list;
RESULT = new NodeDocPair<List<Attribute>>(list, s.doc);
:}
;

attributes ::=
attributes:attributesList attribute:attr
{:
attributesList.addAll(attr);
attributesList.node.addAll(attr.node);
if (attr.doc != null) {
attributesList.doc = attr.doc;
}

RESULT = attributesList;
:}
| attribute:attr
{:
List<Attribute> attributesList = new LinkedList<Attribute>(attr);

RESULT = attributesList;
{:
RESULT = attr;
:}
;

Expand Down Expand Up @@ -3347,7 +3364,7 @@ T_NEW:start class_name_reference:className ctor_arguments:ctor
| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
Expression className = new SimpleReference(tclassleft, tclassright, "class");
ac.setAttributes(attrs);
ac.setAttributes(attrs.node);
RESULT = new ClassInstanceCreation(startleft, acright, className, ctor, ac);
:}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
* var $anotherOne; private function myFunction($a) { } }
* </pre>
*/
public class ClassDeclaration extends TypeDeclaration
implements IPHPDocAwareDeclaration, IRecoverable, IAttributed {
public class ClassDeclaration extends TypeDeclaration implements IPHPDocAwareDeclaration, IRecoverable, IAttributed {

private PHPDocBlock phpDoc;
private TypeReference superClass;
Expand Down Expand Up @@ -221,4 +220,9 @@ public List<Attribute> getAttributes() {
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}

@Override
public void setPHPDoc(PHPDocBlock block) {
this.phpDoc = block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ public List<Attribute> getAttributes() {
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}

@Override
public void setPHPDoc(PHPDocBlock block) {
this.phpDoc = block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface IPHPDocAwareDeclaration {
* @return PHPDoc block
*/
public PHPDocBlock getPHPDoc();

public void setPHPDoc(PHPDocBlock block);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,9 @@ public void traverse(ASTVisitor visitor) throws Exception {
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}

@Override
public void setPHPDoc(PHPDocBlock block) {
this.phpDoc = block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public final void printNode(CorePrinter output) {
public String toString() {
return ASTPrintVisitor.toXMLString(this);
}

@Override
public void setPHPDoc(PHPDocBlock block) {
this.phpDoc = block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ public void setAttributes(List<Attribute> attributes) {
public boolean isReadonly() {
return (this.modifiers & IPHPModifiers.AccReadonly) != 0;
}

@Override
public void setPHPDoc(PHPDocBlock block) {
this.phpDoc = block;
}
}
Loading

0 comments on commit b6a47ac

Please sign in to comment.