Skip to content

Commit

Permalink
PHP8.* Support - Formatter support #148, #149, #150
Browse files Browse the repository at this point in the history
* Parser adjustments for attributes
* Formatter settings for new features (match, enum, attribute)
* Allow "InstanceCreation" in function / attribute initializers in for >= PHP8.1
* Bug fixes in DOM AST
  • Loading branch information
zulus committed May 7, 2023
1 parent 48adc59 commit 1c3f33b
Show file tree
Hide file tree
Showing 59 changed files with 1,559 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ parser code {:
}
return expr;
}

protected void setAttributes(ASTNode target, List<AttributeGroup> list)
{
((IAttributed)target).attributes().addAll(list);
AttributeGroup group = list.get(0);
target.setSourceRange(group.getStart(), target.getEnd() - group.getStart());
}

public final void setAST (AST ast) {
this.ast = ast;
Expand Down Expand Up @@ -742,6 +749,9 @@ class_name:name
:}
| class_name:name T_OPEN_PARENTHESE:start function_call_parameter_list:paramsList T_CLOSE_PARENTHESE:end
{:
if (paramsList.isEmpty()) {
paramsList.add(new EmptyExpression(startright, parser.ast));
}
RESULT = new Attribute(nameleft, endright, ast, name, paramsList);
:}
;
Expand Down Expand Up @@ -888,7 +898,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
statement.attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}

Expand Down Expand Up @@ -1109,7 +1119,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((AttributedStatement) statement).attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}
;
Expand Down Expand Up @@ -1743,7 +1753,9 @@ match_arm_list ::=
:}
| non_empty_match_arm_list:list possible_comma:comma
{:
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
if (comma != null) {
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
}
RESULT = list;
:}
;
Expand Down Expand Up @@ -1888,7 +1900,7 @@ attributed_parameter:parameter

attributed_parameter ::=
attributes:attrs parameter:param {:
((AttributedStatement) param).attributes().addAll(attrs);
setAttributes(param, attrs);
RESULT = param;
:}
| parameter:param {:
Expand Down Expand Up @@ -2254,7 +2266,7 @@ attributed_class_statement:statement

| attributes:attrs attributed_class_statement:statement
{:
((AttributedStatement)statement).attributes().addAll(attrs);
setAttributes(statement, attrs);

RESULT = statement;
:}
Expand Down Expand Up @@ -2631,6 +2643,7 @@ T_NEW:s class_name_reference:className ctor_arguments:ctor

| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
// specified case where atttributes are outside anonymous class AST setAttributes(ac, attrs);
ac.attributes().addAll(attrs);
ClassName className = new ClassName(tclassleft, tclassright, parser.ast, new Identifier(tclassleft, tclassright, parser.ast, "class"));
RESULT = new ClassInstanceCreation(startleft, acright, parser.ast, className, ctor, ac);
Expand Down Expand Up @@ -3023,7 +3036,7 @@ T_LIST:s T_OPEN_PARENTHESE assignment_list:varList T_CLOSE_PARENTHESE:close T_EQ

| attributes:attrs inline_function:fnc
{:
fnc.attributes().addAll(attrs);
setAttributes(fnc, attrs);
RESULT = fnc;
:}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ parser code {:
}
return expr;
}

protected void setAttributes(ASTNode target, List<Attribute> list)
{
((IAttributed)target).setAttributes(list);
Attribute group = list.get(0);
target.setStart(group.start());
}

protected int getElementTypeByUseType(int useType) {
int elementType = 0;
Expand Down Expand Up @@ -917,7 +924,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((IAttributedStatement) statement).setAttributes(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}

Expand Down Expand Up @@ -1198,7 +1205,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((IAttributedStatement) statement).setAttributes(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}
;
Expand Down Expand Up @@ -2125,7 +2132,7 @@ attributed_parameter:parameter

attributed_parameter ::=
attributes:attrs parameter:param {:
((IAttributedStatement) param).setAttributes(attrs);
setAttributes(param, attrs);
RESULT = param;
:}
| parameter:param {:
Expand Down Expand Up @@ -2628,8 +2635,8 @@ attributed_class_statement:statement
| attributes:attrs attributed_class_statement:statement
{:
ASTNode node = parser.getRecentDeclarationStatement();
if (node instanceof IAttributedStatement) {
((IAttributedStatement)node).setAttributes(attrs);
if (node instanceof IAttributed) {
setAttributes(node, attrs);
}

RESULT = statement;
Expand Down Expand Up @@ -3057,7 +3064,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); //special case
RESULT = new ClassInstanceCreation(startleft, acright, className, ctor, ac);
:}
;
Expand Down Expand Up @@ -3471,7 +3478,7 @@ T_LIST:start T_OPEN_PARENTHESE array_pair_list:list T_CLOSE_PARENTHESE:end T_EQU

| attributes:attrs inline_function:fnc
{:
((IAttributedStatement) fnc).setAttributes(attrs);
setAttributes(fnc, attrs);
RESULT = fnc;
:}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ parser code {:
return expr;
}

protected void setAttributes(ASTNode target, List<AttributeGroup> list)
{
((IAttributed)target).attributes().addAll(list);
AttributeGroup group = list.get(0);
target.setSourceRange(group.getStart(), target.getEnd() - group.getStart());
}

public final void setAST (AST ast) {
this.ast = ast;
}
Expand Down Expand Up @@ -785,6 +792,9 @@ class_name:name
:}
| class_name:name T_OPEN_PARENTHESE:start function_call_parameter_list:paramsList T_CLOSE_PARENTHESE:end
{:
if (paramsList.isEmpty()) {
paramsList.add(new EmptyExpression(startright, parser.ast));
}
RESULT = new Attribute(nameleft, endright, ast, name, paramsList);
:}
;
Expand Down Expand Up @@ -931,7 +941,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
statement.attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}

Expand Down Expand Up @@ -1152,7 +1162,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((AttributedStatement) statement).attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}
;
Expand Down Expand Up @@ -1852,7 +1862,9 @@ match_arm_list ::=
:}
| non_empty_match_arm_list:list possible_comma:comma
{:
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
if (comma != null) {
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
}
RESULT = list;
:}
;
Expand Down Expand Up @@ -1997,7 +2009,7 @@ attributed_parameter:parameter

attributed_parameter ::=
attributes:attrs parameter:param {:
((AttributedStatement) param).attributes().addAll(attrs);
setAttributes(param, attrs);
RESULT = param;
:}
| parameter:param {:
Expand Down Expand Up @@ -2451,7 +2463,7 @@ attributed_class_statement:statement

| attributes:attrs attributed_class_statement:statement
{:
((AttributedStatement)statement).attributes().addAll(attrs);
setAttributes(statement, attrs);

RESULT = statement;
:}
Expand Down Expand Up @@ -2850,6 +2862,7 @@ T_NEW:s class_name_reference:className ctor_arguments:ctor

| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
// specified case where atttributes are outside anonymous class AST setAttributes(ac, attrs);
ac.attributes().addAll(attrs);
ClassName className = new ClassName(tclassleft, tclassright, parser.ast, new Identifier(tclassleft, tclassright, parser.ast, "class"));
RESULT = new ClassInstanceCreation(startleft, acright, parser.ast, className, ctor, ac);
Expand Down Expand Up @@ -3242,7 +3255,7 @@ T_LIST:s T_OPEN_PARENTHESE assignment_list:varList T_CLOSE_PARENTHESE:close T_EQ

| attributes:attrs inline_function:fnc
{:
fnc.attributes().addAll(attrs);
setAttributes(fnc, attrs);
RESULT = fnc;
:}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ parser code {:
}
return expr;
}

protected void setAttributes(ASTNode target, List<Attribute> list)
{
((IAttributed)target).setAttributes(list);
Attribute group = list.get(0);
target.setStart(group.start());
}

protected int getElementTypeByUseType(int useType) {
int elementType = 0;
Expand Down Expand Up @@ -960,7 +967,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((IAttributedStatement) statement).setAttributes(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}

Expand Down Expand Up @@ -1241,7 +1248,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((IAttributedStatement) statement).setAttributes(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}
;
Expand Down Expand Up @@ -2255,7 +2262,7 @@ attributed_parameter:parameter

attributed_parameter ::=
attributes:attrs parameter:param {:
((IAttributedStatement) param).setAttributes(attrs);
setAttributes(param, attrs);
RESULT = param;
:}
| parameter:param {:
Expand Down Expand Up @@ -2842,8 +2849,8 @@ attributed_class_statement:statement
| attributes:attrs attributed_class_statement:statement
{:
ASTNode node = parser.getRecentDeclarationStatement();
if (node instanceof IAttributedStatement) {
((IAttributedStatement)node).setAttributes(attrs);
if (node instanceof IAttributed) {
setAttributes(node, attrs);
}

RESULT = statement;
Expand Down Expand Up @@ -3695,7 +3702,7 @@ T_LIST:start T_OPEN_PARENTHESE array_pair_list:list T_CLOSE_PARENTHESE:end T_EQU

| attributes:attrs inline_function:fnc
{:
((IAttributedStatement) fnc).setAttributes(attrs);
setAttributes(fnc, attrs);
RESULT = fnc;
:}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ parser code {:
return expr;
}

protected void setAttributes(ASTNode target, List<AttributeGroup> list)
{
((IAttributed)target).attributes().addAll(list);
AttributeGroup group = list.get(0);
target.setSourceRange(group.getStart(), target.getEnd() - group.getStart());
}

public final void setAST (AST ast) {
this.ast = ast;
}
Expand Down Expand Up @@ -791,6 +798,9 @@ class_name:name
:}
| class_name:name T_OPEN_PARENTHESE:start function_call_parameter_list:paramsList T_CLOSE_PARENTHESE:end
{:
if (paramsList.isEmpty()) {
paramsList.add(new EmptyExpression(startright, parser.ast));
}
RESULT = new Attribute(nameleft, endright, ast, name, paramsList);
:}
;
Expand Down Expand Up @@ -937,7 +947,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
statement.attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}

Expand Down Expand Up @@ -1158,7 +1168,7 @@ statement:statement

| attributes:attrs attributed_statement:statement
{:
((AttributedStatement) statement).attributes().addAll(attrs);
setAttributes(statement, attrs);
RESULT = statement;
:}
;
Expand Down Expand Up @@ -1882,7 +1892,9 @@ match_arm_list ::=
:}
| non_empty_match_arm_list:list possible_comma:comma
{:
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
if (comma != null) {
list.add(new EmptyStatement(commaleft, commaright, parser.ast));
}
RESULT = list;
:}
;
Expand Down Expand Up @@ -2027,7 +2039,7 @@ attributed_parameter:parameter

attributed_parameter ::=
attributes:attrs parameter:param {:
((AttributedStatement) param).attributes().addAll(attrs);
setAttributes(param, attrs);
RESULT = param;
:}
| parameter:param {:
Expand Down Expand Up @@ -2505,7 +2517,7 @@ attributed_class_statement:statement

| attributes:attrs attributed_class_statement:statement
{:
((AttributedStatement)statement).attributes().addAll(attrs);
setAttributes(statement, attrs);

RESULT = statement;
:}
Expand Down Expand Up @@ -2904,6 +2916,7 @@ T_NEW:s class_name_reference:className ctor_arguments:ctor

| T_NEW:start attributes:attrs T_CLASS:tclass ctor_arguments:ctor anonymous_class:ac
{:
// specified case where atttributes are outside anonymous class AST setAttributes(ac, attrs);
ac.attributes().addAll(attrs);
ClassName className = new ClassName(tclassleft, tclassright, parser.ast, new Identifier(tclassleft, tclassright, parser.ast, "class"));
RESULT = new ClassInstanceCreation(startleft, acright, parser.ast, className, ctor, ac);
Expand Down Expand Up @@ -3296,7 +3309,7 @@ T_LIST:s T_OPEN_PARENTHESE assignment_list:varList T_CLOSE_PARENTHESE:close T_EQ

| attributes:attrs inline_function:fnc
{:
fnc.attributes().addAll(attrs);
setAttributes(fnc, attrs);
RESULT = fnc;
:}

Expand Down

0 comments on commit 1c3f33b

Please sign in to comment.