refactoring: create "ExprBrackets" node

This commit is contained in:
Vadym Slizov
2020-12-28 00:02:14 +02:00
parent ad884c99df
commit e4321b5e90
15 changed files with 281 additions and 180 deletions

View File

@@ -82,6 +82,7 @@ type NodeVisitor interface {
ExprArrayDimFetch(n *ExprArrayDimFetch)
ExprArrayItem(n *ExprArrayItem)
ExprArrowFunction(n *ExprArrowFunction)
ExprBrackets(n *ExprBrackets)
ExprBitwiseNot(n *ExprBitwiseNot)
ExprBooleanNot(n *ExprBooleanNot)
ExprClassConstFetch(n *ExprClassConstFetch)

View File

@@ -1243,6 +1243,21 @@ func (n *ExprBooleanNot) GetPosition() *position.Position {
return n.Position
}
type ExprBrackets struct {
Position *position.Position
OpenParenthesisTkn *token.Token
Expr Vertex
CloseParenthesisTkn *token.Token
}
func (n *ExprBrackets) Accept(v NodeVisitor) {
v.ExprBrackets(n)
}
func (n *ExprBrackets) GetPosition() *position.Position {
return n.Position
}
// ExprClassConstFetch node
type ExprClassConstFetch struct {
Position *position.Position

View File

@@ -1109,6 +1109,18 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Expr)
t.visitor.Leave("Expr", true)
}
case *ast.ExprBrackets:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Expr != nil {
t.visitor.Enter("Expr", true)
t.Traverse(nn.Expr)
t.visitor.Leave("Expr", true)
}
case *ast.ExprClassConstFetch:
if nn == nil {
return

View File

@@ -1108,6 +1108,19 @@ func (v *Dumper) ExprBooleanNot(n *ast.ExprBooleanNot) {
v.print(v.indent, "},\n")
}
func (v *Dumper) ExprBrackets(n *ast.ExprBrackets) {
v.print(0, "&ast.ExprBrackets{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
v.dumpVertex("Expr", n.Expr)
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ExprClassConstFetch(n *ast.ExprClassConstFetch) {
v.print(0, "&ast.ExprClassConstFetch{\n")
v.indent++

View File

@@ -1134,6 +1134,12 @@ func (f *formatter) ExprBooleanNot(n *ast.ExprBooleanNot) {
n.Expr.Accept(f)
}
func (f *formatter) ExprBrackets(n *ast.ExprBrackets) {
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
n.Expr.Accept(f)
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
}
func (f *formatter) ExprClassConstFetch(n *ast.ExprClassConstFetch) {
n.Class.Accept(f)
n.DoubleColonTkn = f.newToken(token.T_PAAMAYIM_NEKUDOTAYIM, []byte("::"))

View File

@@ -3571,6 +3571,31 @@ func TestFormatter_ExprBooleanNot(t *testing.T) {
}
}
func TestFormatter_ExprBrackets(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprBrackets{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$foo"),
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `($foo)`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_ExprClassConstFetch(t *testing.T) {
o := bytes.NewBufferString("")

View File

@@ -270,6 +270,10 @@ func (v *Null) ExprBooleanNot(_ *ast.ExprBooleanNot) {
// do nothing
}
func (v *Null) ExprBrackets(_ *ast.ExprBrackets) {
// do nothing
}
func (v *Null) ExprClassConstFetch(_ *ast.ExprClassConstFetch) {
// do nothing
}

View File

@@ -668,6 +668,12 @@ func (p *printer) ExprBooleanNot(n *ast.ExprBooleanNot) {
p.printNode(n.Expr)
}
func (p *printer) ExprBrackets(n *ast.ExprBrackets) {
p.printToken(n.OpenParenthesisTkn, nil)
p.printNode(n.Expr)
p.printToken(n.CloseParenthesisTkn, nil)
}
func (p *printer) ExprClassConstFetch(n *ast.ExprClassConstFetch) {
p.printNode(n.Class)
p.printToken(n.DoubleColonTkn, []byte("::"))

View File

@@ -1696,6 +1696,25 @@ func TestPrinterPrintExprBooleanNot(t *testing.T) {
}
}
func TestPrinterPrintExprBracket(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprBooleanNot{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
}
n.Accept(p)
expected := `!$var`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClassConstFetch(t *testing.T) {
o := bytes.NewBufferString("")