PHP 8 (#1)
PHP 8 Update
- nullsafe operator (?->)
- Remove (real) cast
- Named arguments
- Remove (unset) cast
- Remove {} access
- match expression
- Union types in type hints and static typehint
- Block catch without variable
- Trailing comma in parameter lists
- throw can be used as an expression
- Concatenation precedence
- Declaring properties in the constructor
- Attributes
- Names in the namespace are treated as a single token
- Trailing comma in closure use list
- Check that ::class on object works
- Deferencable changes and arbitrary expressions in new/instanceof
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package dumper
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
)
|
||||
|
||||
@@ -192,6 +193,8 @@ func (v *Dumper) Parameter(n *ast.Parameter) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpVertex("Visibility", n.Visibility)
|
||||
v.dumpVertex("Type", n.Type)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
v.dumpToken("VariadicTkn", n.VariadicTkn)
|
||||
@@ -221,6 +224,8 @@ func (v *Dumper) Argument(n *ast.Argument) {
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
v.dumpToken("ColonTkn", n.ColonTkn)
|
||||
v.dumpToken("VariadicTkn", n.VariadicTkn)
|
||||
v.dumpVertex("Expr", n.Expr)
|
||||
|
||||
@@ -228,6 +233,63 @@ func (v *Dumper) Argument(n *ast.Argument) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) MatchArm(n *ast.MatchArm) {
|
||||
v.print(0, "&ast.MatchArm{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("DefaultTkn", n.DefaultTkn)
|
||||
v.dumpToken("DefaultCommaTkn", n.DefaultCommaTkn)
|
||||
v.dumpVertexList("Exprs", n.Exprs)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn)
|
||||
v.dumpVertex("ReturnExpr", n.ReturnExpr)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) Union(n *ast.Union) {
|
||||
v.print(0, "&ast.Union{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("Types", n.Types)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) Attribute(n *ast.Attribute) {
|
||||
v.print(0, "&ast.Attribute{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
|
||||
v.dumpVertexList("Args", n.Args)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) AttributeGroup(n *ast.AttributeGroup) {
|
||||
v.print(0, "&ast.AttributeGroup{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("OpenAttributeTkn", n.OpenAttributeTkn)
|
||||
v.dumpVertexList("Attrs", n.Attrs)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("CloseAttributeTkn", n.CloseAttributeTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) StmtBreak(n *ast.StmtBreak) {
|
||||
v.print(0, "&ast.StmtBreak{\n")
|
||||
v.indent++
|
||||
@@ -279,6 +341,7 @@ func (v *Dumper) StmtClass(n *ast.StmtClass) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpVertexList("Modifiers", n.Modifiers)
|
||||
v.dumpToken("ClassTkn", n.ClassTkn)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
@@ -304,6 +367,7 @@ func (v *Dumper) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpVertexList("Modifiers", n.Modifiers)
|
||||
v.dumpToken("ConstTkn", n.ConstTkn)
|
||||
v.dumpVertexList("Consts", n.Consts)
|
||||
@@ -319,6 +383,7 @@ func (v *Dumper) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpVertexList("Modifiers", n.Modifiers)
|
||||
v.dumpToken("FunctionTkn", n.FunctionTkn)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
@@ -547,6 +612,7 @@ func (v *Dumper) StmtFunction(n *ast.StmtFunction) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpToken("FunctionTkn", n.FunctionTkn)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
@@ -642,6 +708,7 @@ func (v *Dumper) StmtInterface(n *ast.StmtInterface) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpToken("InterfaceTkn", n.InterfaceTkn)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
v.dumpToken("ExtendsTkn", n.ExtendsTkn)
|
||||
@@ -712,6 +779,7 @@ func (v *Dumper) StmtPropertyList(n *ast.StmtPropertyList) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpVertexList("Modifiers", n.Modifiers)
|
||||
v.dumpVertex("Type", n.Type)
|
||||
v.dumpVertexList("Props", n.Props)
|
||||
@@ -814,6 +882,7 @@ func (v *Dumper) StmtTrait(n *ast.StmtTrait) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpToken("TraitTkn", n.TraitTkn)
|
||||
v.dumpVertex("Name", n.Name)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
@@ -1024,6 +1093,7 @@ func (v *Dumper) ExprArrowFunction(n *ast.ExprArrowFunction) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpToken("StaticTkn", n.StaticTkn)
|
||||
v.dumpToken("FnTkn", n.FnTkn)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
@@ -1107,6 +1177,7 @@ func (v *Dumper) ExprClosure(n *ast.ExprClosure) {
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertexList("AttrGroups", n.AttrGroups)
|
||||
v.dumpToken("StaticTkn", n.StaticTkn)
|
||||
v.dumpToken("FunctionTkn", n.FunctionTkn)
|
||||
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
|
||||
@@ -1307,6 +1378,25 @@ func (v *Dumper) ExprMethodCall(n *ast.ExprMethodCall) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprNullsafeMethodCall(n *ast.ExprNullsafeMethodCall) {
|
||||
v.print(0, "&ast.ExprNullsafeMethodCall{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertex("Var", n.Var)
|
||||
v.dumpToken("ObjectOperatorTkn", n.ObjectOperatorTkn)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertex("Method", n.Method)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
|
||||
v.dumpVertexList("Args", n.Args)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprNew(n *ast.ExprNew) {
|
||||
v.print(0, "&ast.ExprNew{\n")
|
||||
v.indent++
|
||||
@@ -1398,6 +1488,21 @@ func (v *Dumper) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprNullsafePropertyFetch(n *ast.ExprNullsafePropertyFetch) {
|
||||
v.print(0, "&ast.ExprNullsafePropertyFetch{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpVertex("Var", n.Var)
|
||||
v.dumpToken("ObjectOperatorTkn", n.ObjectOperatorTkn)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertex("Prop", n.Prop)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprRequire(n *ast.ExprRequire) {
|
||||
v.print(0, "&ast.ExprRequire{\n")
|
||||
v.indent++
|
||||
@@ -2177,6 +2282,37 @@ func (v *Dumper) ExprCastUnset(n *ast.ExprCastUnset) {
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprMatch(n *ast.ExprMatch) {
|
||||
v.print(0, "&ast.ExprMatch{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("MatchTkn", n.MatchTkn)
|
||||
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
|
||||
v.dumpVertex("Expr", n.Expr)
|
||||
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
|
||||
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
|
||||
v.dumpVertexList("Arms", n.Arms)
|
||||
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
|
||||
v.dumpToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ExprThrow(n *ast.ExprThrow) {
|
||||
v.print(0, "&ast.ExprThrow{\n")
|
||||
v.indent++
|
||||
|
||||
v.dumpPosition(n.Position)
|
||||
v.dumpToken("ThrowTkn", n.ThrowTkn)
|
||||
v.dumpVertex("Expr", n.Expr)
|
||||
v.dumpToken("SemiColonTkn", n.SemiColonTkn)
|
||||
|
||||
v.indent--
|
||||
v.print(v.indent, "},\n")
|
||||
}
|
||||
|
||||
func (v *Dumper) ScalarDnumber(n *ast.ScalarDnumber) {
|
||||
v.print(0, "&ast.ScalarDnumber{\n")
|
||||
v.indent++
|
||||
|
||||
@@ -2,6 +2,7 @@ package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
)
|
||||
@@ -153,6 +154,18 @@ func (f *formatter) Nullable(n *ast.Nullable) {
|
||||
}
|
||||
|
||||
func (f *formatter) Parameter(n *ast.Parameter) {
|
||||
if n.AttrGroups != nil {
|
||||
for _, group := range n.AttrGroups {
|
||||
group.Accept(f)
|
||||
}
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.Visibility != nil {
|
||||
n.Visibility.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.Type != nil {
|
||||
n.Type.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -185,6 +198,14 @@ func (f *formatter) Identifier(n *ast.Identifier) {
|
||||
}
|
||||
|
||||
func (f *formatter) Argument(n *ast.Argument) {
|
||||
if n.Name != nil {
|
||||
n.Name.Accept(f)
|
||||
}
|
||||
|
||||
if n.ColonTkn != nil {
|
||||
n.ColonTkn = f.newToken(':', []byte(":"))
|
||||
}
|
||||
|
||||
if n.VariadicTkn != nil {
|
||||
n.VariadicTkn = f.newToken(token.T_ELLIPSIS, []byte("..."))
|
||||
}
|
||||
@@ -196,6 +217,45 @@ func (f *formatter) Argument(n *ast.Argument) {
|
||||
n.Expr.Accept(f)
|
||||
}
|
||||
|
||||
func (f *formatter) MatchArm(n *ast.MatchArm) {
|
||||
if n.DefaultTkn != nil {
|
||||
n.DefaultTkn = f.newToken(token.T_DEFAULT, []byte("default"))
|
||||
}
|
||||
if n.DefaultCommaTkn != nil {
|
||||
n.DefaultCommaTkn = f.newToken(',', []byte(","))
|
||||
}
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.Exprs) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Exprs, ',')
|
||||
}
|
||||
n.DoubleArrowTkn = f.newToken(token.T_DOUBLE_ARROW, []byte("=>"))
|
||||
}
|
||||
|
||||
func (f *formatter) Union(n *ast.Union) {
|
||||
if len(n.Types) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Types, '|')
|
||||
}
|
||||
}
|
||||
|
||||
func (f *formatter) Attribute(n *ast.Attribute) {
|
||||
n.Name.Accept(f)
|
||||
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.Args) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Args, ',')
|
||||
}
|
||||
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
|
||||
}
|
||||
|
||||
func (f *formatter) AttributeGroup(n *ast.AttributeGroup) {
|
||||
n.OpenAttributeTkn = f.newToken(token.T_ATTRIBUTE, []byte("#["))
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.Attrs) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Attrs, ',')
|
||||
}
|
||||
n.CloseAttributeTkn = f.newToken(']', []byte("]"))
|
||||
}
|
||||
|
||||
func (f *formatter) StmtBreak(n *ast.StmtBreak) {
|
||||
n.BreakTkn = f.newToken(token.T_BREAK, []byte("break"))
|
||||
|
||||
@@ -307,6 +367,10 @@ func (f *formatter) StmtClass(n *ast.StmtClass) {
|
||||
}
|
||||
|
||||
func (f *formatter) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
for _, m := range n.AttrGroups {
|
||||
m.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte("\n"))
|
||||
}
|
||||
for _, m := range n.Modifiers {
|
||||
m.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -736,6 +800,10 @@ func (f *formatter) StmtProperty(n *ast.StmtProperty) {
|
||||
}
|
||||
|
||||
func (f *formatter) StmtPropertyList(n *ast.StmtPropertyList) {
|
||||
for _, m := range n.AttrGroups {
|
||||
m.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte("\n"))
|
||||
}
|
||||
for _, m := range n.Modifiers {
|
||||
m.Accept(f)
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -1061,6 +1129,13 @@ func (f *formatter) ExprArrayItem(n *ast.ExprArrayItem) {
|
||||
}
|
||||
|
||||
func (f *formatter) ExprArrowFunction(n *ast.ExprArrowFunction) {
|
||||
if n.AttrGroups != nil {
|
||||
for _, group := range n.AttrGroups {
|
||||
group.Accept(f)
|
||||
}
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.StaticTkn != nil {
|
||||
n.StaticTkn = f.newToken(token.T_STATIC, []byte("static"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -1123,6 +1198,13 @@ func (f *formatter) ExprClone(n *ast.ExprClone) {
|
||||
}
|
||||
|
||||
func (f *formatter) ExprClosure(n *ast.ExprClosure) {
|
||||
if n.AttrGroups != nil {
|
||||
for _, group := range n.AttrGroups {
|
||||
group.Accept(f)
|
||||
}
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
}
|
||||
|
||||
if n.StaticTkn != nil {
|
||||
n.StaticTkn = f.newToken(token.T_STATIC, []byte("static"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -1287,6 +1369,30 @@ func (f *formatter) ExprMethodCall(n *ast.ExprMethodCall) {
|
||||
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
|
||||
}
|
||||
|
||||
func (f *formatter) ExprNullsafeMethodCall(n *ast.ExprNullsafeMethodCall) {
|
||||
n.Var.Accept(f)
|
||||
n.ObjectOperatorTkn = f.newToken(token.T_NULLSAFE_OBJECT_OPERATOR, []byte("?->"))
|
||||
|
||||
n.OpenCurlyBracketTkn = nil
|
||||
n.CloseCurlyBracketTkn = nil
|
||||
switch n.Method.(type) {
|
||||
case *ast.Identifier:
|
||||
case *ast.ExprVariable:
|
||||
default:
|
||||
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
|
||||
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
|
||||
}
|
||||
|
||||
n.Method.Accept(f)
|
||||
|
||||
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.Args) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Args, ',')
|
||||
}
|
||||
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
|
||||
}
|
||||
|
||||
func (f *formatter) ExprNew(n *ast.ExprNew) {
|
||||
n.NewTkn = f.newToken(token.T_NEW, []byte("new"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -1347,6 +1453,23 @@ func (f *formatter) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
|
||||
n.Prop.Accept(f)
|
||||
}
|
||||
|
||||
func (f *formatter) ExprNullsafePropertyFetch(n *ast.ExprNullsafePropertyFetch) {
|
||||
n.Var.Accept(f)
|
||||
n.ObjectOperatorTkn = f.newToken(token.T_NULLSAFE_OBJECT_OPERATOR, []byte("?->"))
|
||||
|
||||
n.OpenCurlyBracketTkn = nil
|
||||
n.CloseCurlyBracketTkn = nil
|
||||
switch n.Prop.(type) {
|
||||
case *ast.Identifier:
|
||||
case *ast.ExprVariable:
|
||||
default:
|
||||
n.OpenCurlyBracketTkn = f.newToken('{', []byte("{"))
|
||||
n.CloseCurlyBracketTkn = f.newToken('}', []byte("}"))
|
||||
}
|
||||
|
||||
n.Prop.Accept(f)
|
||||
}
|
||||
|
||||
func (f *formatter) ExprRequire(n *ast.ExprRequire) {
|
||||
n.RequireTkn = f.newToken(token.T_REQUIRE, []byte("require"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
@@ -1916,6 +2039,27 @@ func (f *formatter) ExprCastUnset(n *ast.ExprCastUnset) {
|
||||
n.Expr.Accept(f)
|
||||
}
|
||||
|
||||
func (f *formatter) ExprMatch(n *ast.ExprMatch) {
|
||||
n.MatchTkn = f.newToken(token.T_MATCH, []byte("match"))
|
||||
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
|
||||
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
|
||||
n.OpenCurlyBracketTkn = f.newToken('}', []byte("}"))
|
||||
n.SeparatorTkns = nil
|
||||
if len(n.Arms) > 0 {
|
||||
n.SeparatorTkns = f.formatList(n.Arms, ',')
|
||||
}
|
||||
n.CloseCurlyBracketTkn = f.newToken('{', []byte("{"))
|
||||
}
|
||||
|
||||
func (f *formatter) ExprThrow(n *ast.ExprThrow) {
|
||||
n.ThrowTkn = f.newToken(token.T_THROW, []byte("throw"))
|
||||
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
|
||||
|
||||
n.Expr.Accept(f)
|
||||
|
||||
n.SemiColonTkn = f.newSemicolonTkn()
|
||||
}
|
||||
|
||||
func (f *formatter) ScalarDnumber(n *ast.ScalarDnumber) {
|
||||
if n.NumberTkn == nil {
|
||||
n.NumberTkn = f.newToken(token.T_STRING, n.Value)
|
||||
|
||||
@@ -3,9 +3,10 @@ package nsresolver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NamespaceResolver visitor
|
||||
|
||||
@@ -42,6 +42,22 @@ func (v *Null) Argument(_ *ast.Argument) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) MatchArm(_ *ast.MatchArm) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) Union(_ *ast.Union) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) Attribute(_ *ast.Attribute) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) AttributeGroup(_ *ast.AttributeGroup) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) StmtBreak(_ *ast.StmtBreak) {
|
||||
// do nothing
|
||||
}
|
||||
@@ -322,6 +338,10 @@ func (v *Null) ExprMethodCall(_ *ast.ExprMethodCall) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprNullsafeMethodCall(_ *ast.ExprNullsafeMethodCall) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprNew(_ *ast.ExprNew) {
|
||||
// do nothing
|
||||
}
|
||||
@@ -350,6 +370,10 @@ func (v *Null) ExprPropertyFetch(_ *ast.ExprPropertyFetch) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprNullsafePropertyFetch(_ *ast.ExprNullsafePropertyFetch) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprRequire(_ *ast.ExprRequire) {
|
||||
// do nothing
|
||||
}
|
||||
@@ -590,6 +614,14 @@ func (v *Null) ExprCastUnset(_ *ast.ExprCastUnset) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprMatch(_ *ast.ExprMatch) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ExprThrow(_ *ast.ExprThrow) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (v *Null) ScalarDnumber(_ *ast.ScalarDnumber) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package printer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
"io"
|
||||
)
|
||||
|
||||
type printerState int
|
||||
@@ -145,6 +146,8 @@ func (p *printer) Nullable(n *ast.Nullable) {
|
||||
}
|
||||
|
||||
func (p *printer) Parameter(n *ast.Parameter) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printNode(n.Visibility)
|
||||
p.printNode(n.Type)
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
p.printToken(n.VariadicTkn, nil)
|
||||
@@ -158,11 +161,38 @@ func (p *printer) Identifier(n *ast.Identifier) {
|
||||
}
|
||||
|
||||
func (p *printer) Argument(n *ast.Argument) {
|
||||
p.printNode(n.Name)
|
||||
p.printToken(n.ColonTkn, nil)
|
||||
p.printToken(n.VariadicTkn, nil)
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
p.printNode(n.Expr)
|
||||
}
|
||||
|
||||
func (p *printer) MatchArm(n *ast.MatchArm) {
|
||||
p.printToken(n.DefaultTkn, nil)
|
||||
p.printToken(n.DefaultCommaTkn, nil)
|
||||
p.printSeparatedList(n.Exprs, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.DoubleArrowTkn, []byte("=>"))
|
||||
p.printNode(n.ReturnExpr)
|
||||
}
|
||||
|
||||
func (p *printer) Union(n *ast.Union) {
|
||||
p.printSeparatedList(n.Types, n.SeparatorTkns, []byte("|"))
|
||||
}
|
||||
|
||||
func (p *printer) Attribute(n *ast.Attribute) {
|
||||
p.printNode(n.Name)
|
||||
p.printToken(n.OpenParenthesisTkn, p.ifNodeList(n.Args, []byte("(")))
|
||||
p.printSeparatedList(n.Args, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.CloseParenthesisTkn, p.ifNodeList(n.Args, []byte(")")))
|
||||
}
|
||||
|
||||
func (p *printer) AttributeGroup(n *ast.AttributeGroup) {
|
||||
p.printToken(n.OpenAttributeTkn, []byte("#["))
|
||||
p.printSeparatedList(n.Attrs, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.CloseAttributeTkn, []byte("]"))
|
||||
}
|
||||
|
||||
func (p *printer) StmtBreak(n *ast.StmtBreak) {
|
||||
p.printToken(n.BreakTkn, []byte("break"))
|
||||
p.printNode(n.Expr)
|
||||
@@ -188,6 +218,7 @@ func (p *printer) StmtCatch(n *ast.StmtCatch) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtClass(n *ast.StmtClass) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printList(n.Modifiers)
|
||||
p.printToken(n.ClassTkn, []byte("class"))
|
||||
p.printNode(n.Name)
|
||||
@@ -204,6 +235,7 @@ func (p *printer) StmtClass(n *ast.StmtClass) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printList(n.Modifiers)
|
||||
p.printToken(n.ConstTkn, []byte("const"))
|
||||
p.printSeparatedList(n.Consts, n.SeparatorTkns, []byte(","))
|
||||
@@ -211,6 +243,7 @@ func (p *printer) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printList(n.Modifiers)
|
||||
p.printToken(n.FunctionTkn, []byte("function"))
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
@@ -364,6 +397,7 @@ func (p *printer) StmtForeach(n *ast.StmtForeach) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtFunction(n *ast.StmtFunction) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printToken(n.FunctionTkn, []byte("function"))
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
p.printNode(n.Name)
|
||||
@@ -426,6 +460,7 @@ func (p *printer) StmtInlineHtml(n *ast.StmtInlineHtml) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtInterface(n *ast.StmtInterface) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printToken(n.InterfaceTkn, []byte("interface"))
|
||||
p.printNode(n.Name)
|
||||
p.printToken(n.ExtendsTkn, p.ifNodeList(n.Extends, []byte("extends")))
|
||||
@@ -460,6 +495,7 @@ func (p *printer) StmtProperty(n *ast.StmtProperty) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtPropertyList(n *ast.StmtPropertyList) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printList(n.Modifiers)
|
||||
p.printNode(n.Type)
|
||||
p.printSeparatedList(n.Props, n.SeparatorTkns, []byte(","))
|
||||
@@ -511,6 +547,7 @@ func (p *printer) StmtThrow(n *ast.StmtThrow) {
|
||||
}
|
||||
|
||||
func (p *printer) StmtTrait(n *ast.StmtTrait) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printToken(n.TraitTkn, []byte("trait"))
|
||||
p.printNode(n.Name)
|
||||
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
|
||||
@@ -630,6 +667,7 @@ func (p *printer) ExprArrayItem(n *ast.ExprArrayItem) {
|
||||
}
|
||||
|
||||
func (p *printer) ExprArrowFunction(n *ast.ExprArrowFunction) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printToken(n.StaticTkn, nil)
|
||||
p.printToken(n.FnTkn, []byte("fn"))
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
@@ -670,6 +708,7 @@ func (p *printer) ExprClone(n *ast.ExprClone) {
|
||||
}
|
||||
|
||||
func (p *printer) ExprClosure(n *ast.ExprClosure) {
|
||||
p.printList(n.AttrGroups)
|
||||
p.printToken(n.StaticTkn, nil)
|
||||
p.printToken(n.FunctionTkn, []byte("function"))
|
||||
p.printToken(n.AmpersandTkn, nil)
|
||||
@@ -770,6 +809,17 @@ func (p *printer) ExprMethodCall(n *ast.ExprMethodCall) {
|
||||
p.printToken(n.CloseParenthesisTkn, []byte(")"))
|
||||
}
|
||||
|
||||
func (p *printer) ExprNullsafeMethodCall(n *ast.ExprNullsafeMethodCall) {
|
||||
p.printNode(n.Var)
|
||||
p.printToken(n.ObjectOperatorTkn, []byte("?->"))
|
||||
p.printToken(n.OpenCurlyBracketTkn, nil)
|
||||
p.printNode(n.Method)
|
||||
p.printToken(n.CloseCurlyBracketTkn, nil)
|
||||
p.printToken(n.OpenParenthesisTkn, []byte("("))
|
||||
p.printSeparatedList(n.Args, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.CloseParenthesisTkn, []byte(")"))
|
||||
}
|
||||
|
||||
func (p *printer) ExprNew(n *ast.ExprNew) {
|
||||
p.printToken(n.NewTkn, []byte("new"))
|
||||
p.printNode(n.Class)
|
||||
@@ -811,6 +861,14 @@ func (p *printer) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
|
||||
p.printToken(n.CloseCurlyBracketTkn, nil)
|
||||
}
|
||||
|
||||
func (p *printer) ExprNullsafePropertyFetch(n *ast.ExprNullsafePropertyFetch) {
|
||||
p.printNode(n.Var)
|
||||
p.printToken(n.ObjectOperatorTkn, []byte("?->"))
|
||||
p.printToken(n.OpenCurlyBracketTkn, nil)
|
||||
p.printNode(n.Prop)
|
||||
p.printToken(n.CloseCurlyBracketTkn, nil)
|
||||
}
|
||||
|
||||
func (p *printer) ExprRequire(n *ast.ExprRequire) {
|
||||
p.printToken(n.RequireTkn, []byte("require"))
|
||||
p.printNode(n.Expr)
|
||||
@@ -1169,6 +1227,21 @@ func (p *printer) ExprCastUnset(n *ast.ExprCastUnset) {
|
||||
p.printNode(n.Expr)
|
||||
}
|
||||
|
||||
func (p *printer) ExprMatch(n *ast.ExprMatch) {
|
||||
p.printToken(n.MatchTkn, []byte("match"))
|
||||
p.printToken(n.OpenParenthesisTkn, []byte("("))
|
||||
p.printNode(n.Expr)
|
||||
p.printToken(n.CloseParenthesisTkn, []byte(")"))
|
||||
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
|
||||
p.printSeparatedList(n.Arms, n.SeparatorTkns, []byte(","))
|
||||
p.printToken(n.CloseCurlyBracketTkn, []byte("}"))
|
||||
}
|
||||
|
||||
func (p *printer) ExprThrow(n *ast.ExprThrow) {
|
||||
p.printToken(n.ThrowTkn, []byte("throw"))
|
||||
p.printNode(n.Expr)
|
||||
}
|
||||
|
||||
func (p *printer) ScalarDnumber(n *ast.ScalarDnumber) {
|
||||
p.printToken(n.NumberTkn, n.Value)
|
||||
}
|
||||
|
||||
2651
pkg/visitor/printer/printer_php8_test.go
Normal file
2651
pkg/visitor/printer/printer_php8_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -37,6 +37,11 @@ func (t *Traverser) Nullable(n *ast.Nullable) {
|
||||
func (t *Traverser) Parameter(n *ast.Parameter) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
|
||||
t.Traverse(n.Visibility)
|
||||
t.Traverse(n.Type)
|
||||
t.Traverse(n.Var)
|
||||
t.Traverse(n.DefaultValue)
|
||||
@@ -49,9 +54,44 @@ func (t *Traverser) Identifier(n *ast.Identifier) {
|
||||
func (t *Traverser) Argument(n *ast.Argument) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Name)
|
||||
t.Traverse(n.Expr)
|
||||
}
|
||||
|
||||
func (t *Traverser) MatchArm(n *ast.MatchArm) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.Exprs {
|
||||
nn.Accept(t)
|
||||
}
|
||||
t.Traverse(n.ReturnExpr)
|
||||
}
|
||||
|
||||
func (t *Traverser) Union(n *ast.Union) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.Types {
|
||||
nn.Accept(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) Attribute(n *ast.Attribute) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Name)
|
||||
for _, nn := range n.Args {
|
||||
nn.Accept(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) AttributeGroup(n *ast.AttributeGroup) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.Attrs {
|
||||
nn.Accept(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) StmtBreak(n *ast.StmtBreak) {
|
||||
n.Accept(t.v)
|
||||
|
||||
@@ -82,6 +122,9 @@ func (t *Traverser) StmtCatch(n *ast.StmtCatch) {
|
||||
func (t *Traverser) StmtClass(n *ast.StmtClass) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Modifiers {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -101,6 +144,9 @@ func (t *Traverser) StmtClass(n *ast.StmtClass) {
|
||||
func (t *Traverser) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Modifiers {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -112,6 +158,9 @@ func (t *Traverser) StmtClassConstList(n *ast.StmtClassConstList) {
|
||||
func (t *Traverser) StmtClassMethod(n *ast.StmtClassMethod) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Modifiers {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -230,6 +279,9 @@ func (t *Traverser) StmtForeach(n *ast.StmtForeach) {
|
||||
func (t *Traverser) StmtFunction(n *ast.StmtFunction) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
t.Traverse(n.Name)
|
||||
for _, nn := range n.Params {
|
||||
nn.Accept(t)
|
||||
@@ -276,6 +328,9 @@ func (t *Traverser) StmtInlineHtml(n *ast.StmtInlineHtml) {
|
||||
func (t *Traverser) StmtInterface(n *ast.StmtInterface) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
t.Traverse(n.Name)
|
||||
for _, nn := range n.Extends {
|
||||
nn.Accept(t)
|
||||
@@ -314,6 +369,9 @@ func (t *Traverser) StmtProperty(n *ast.StmtProperty) {
|
||||
func (t *Traverser) StmtPropertyList(n *ast.StmtPropertyList) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Modifiers {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -370,6 +428,9 @@ func (t *Traverser) StmtThrow(n *ast.StmtThrow) {
|
||||
func (t *Traverser) StmtTrait(n *ast.StmtTrait) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
t.Traverse(n.Name)
|
||||
for _, nn := range n.Stmts {
|
||||
nn.Accept(t)
|
||||
@@ -485,6 +546,9 @@ func (t *Traverser) ExprArrayItem(n *ast.ExprArrayItem) {
|
||||
func (t *Traverser) ExprArrowFunction(n *ast.ExprArrowFunction) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Params {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -526,6 +590,9 @@ func (t *Traverser) ExprClone(n *ast.ExprClone) {
|
||||
func (t *Traverser) ExprClosure(n *ast.ExprClosure) {
|
||||
n.Accept(t.v)
|
||||
|
||||
for _, nn := range n.AttrGroups {
|
||||
nn.Accept(t)
|
||||
}
|
||||
for _, nn := range n.Params {
|
||||
nn.Accept(t)
|
||||
}
|
||||
@@ -628,6 +695,16 @@ func (t *Traverser) ExprMethodCall(n *ast.ExprMethodCall) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprNullsafeMethodCall(n *ast.ExprNullsafeMethodCall) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Var)
|
||||
t.Traverse(n.Method)
|
||||
for _, nn := range n.Args {
|
||||
nn.Accept(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprNew(n *ast.ExprNew) {
|
||||
n.Accept(t.v)
|
||||
|
||||
@@ -674,6 +751,13 @@ func (t *Traverser) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
|
||||
t.Traverse(n.Prop)
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprNullsafePropertyFetch(n *ast.ExprNullsafePropertyFetch) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Var)
|
||||
t.Traverse(n.Prop)
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprRequire(n *ast.ExprRequire) {
|
||||
n.Accept(t.v)
|
||||
|
||||
@@ -1086,6 +1170,21 @@ func (t *Traverser) ExprCastUnset(n *ast.ExprCastUnset) {
|
||||
t.Traverse(n.Expr)
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprMatch(n *ast.ExprMatch) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Expr)
|
||||
for _, nn := range n.Arms {
|
||||
nn.Accept(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Traverser) ExprThrow(n *ast.ExprThrow) {
|
||||
n.Accept(t.v)
|
||||
|
||||
t.Traverse(n.Expr)
|
||||
}
|
||||
|
||||
func (t *Traverser) ScalarDnumber(n *ast.ScalarDnumber) {
|
||||
n.Accept(t.v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user