[refactoring] update ast structure of "Constant" nodes

This commit is contained in:
Vadym Slizov
2020-08-24 23:28:44 +03:00
parent 0285900fe5
commit c63213630a
19 changed files with 1457 additions and 1418 deletions

View File

@@ -318,8 +318,10 @@ func (n *StmtClass) Accept(v NodeVisitor) {
// StmtClassConstList node
type StmtClassConstList struct {
Node
Modifiers []Vertex
Consts []Vertex
Modifiers []Vertex
ConstTkn *token.Token
Consts []Vertex
SemiColonTkn *token.Token
}
func (n *StmtClassConstList) Accept(v NodeVisitor) {
@@ -364,7 +366,9 @@ func (n *StmtClassMethod) Accept(v NodeVisitor) {
// StmtConstList node
type StmtConstList struct {
Node
Consts []Vertex
ConstTkn *token.Token
Consts []Vertex
SemiColonTkn *token.Token
}
func (n *StmtConstList) Accept(v NodeVisitor) {
@@ -374,8 +378,10 @@ func (n *StmtConstList) Accept(v NodeVisitor) {
// StmtConstant node
type StmtConstant struct {
Node
ConstantName Vertex
Expr Vertex
Name Vertex
EqualTkn *token.Token
Expr Vertex
CommaTkn *token.Token
}
func (n *StmtConstant) Accept(v NodeVisitor) {

View File

@@ -487,10 +487,10 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.ConstantName != nil {
t.visitor.Enter("ConstantName", true)
t.Traverse(nn.ConstantName)
t.visitor.Leave("ConstantName", true)
if nn.Name != nil {
t.visitor.Enter("Name", true)
t.Traverse(nn.Name)
t.visitor.Leave("Name", true)
}
if nn.Expr != nil {
t.visitor.Enter("Expr", true)
@@ -1334,9 +1334,9 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("Class", true)
}
if nn.ConstantName != nil {
t.visitor.Enter("ConstantName", true)
t.visitor.Enter("Name", true)
t.Traverse(nn.ConstantName)
t.visitor.Leave("ConstantName", true)
t.visitor.Leave("Name", true)
}
case *ast.ExprClone:
if nn == nil {

View File

@@ -67,3 +67,18 @@ func (v *FilterTokens) StmtHaltCompiler(n *ast.StmtHaltCompiler) {
n.CloseParenthesisTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtConstList(n *ast.StmtConstList) {
n.ConstTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtClassConstList(n *ast.StmtClassConstList) {
n.ConstTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtConstant(n *ast.StmtConstant) {
n.EqualTkn = nil
n.CommaTkn = nil
}

View File

@@ -141,7 +141,7 @@ func (nsr *NamespaceResolver) StmtPropertyList(n *ast.StmtPropertyList) {
func (nsr *NamespaceResolver) StmtConstList(n *ast.StmtConstList) {
for _, constant := range n.Consts {
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).ConstantName.(*ast.Identifier).Value))
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).Name.(*ast.Identifier).Value))
}
}

View File

@@ -594,12 +594,12 @@ func TestResolveConstantsName(t *testing.T) {
nameAB := &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
constantB := &ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("B")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
Name: &ast.Identifier{Value: []byte("B")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
}
constantC := &ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("C")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
Name: &ast.Identifier{Value: []byte("C")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
}
stxTree := &ast.StmtStmtList{
@@ -638,12 +638,12 @@ func TestResolveNamespaces(t *testing.T) {
relativeNameCE := &ast.NameRelative{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("C")}, &ast.NameNamePart{Value: []byte("E")}}}
constantB := &ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("B")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
Name: &ast.Identifier{Value: []byte("B")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
}
constantC := &ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("C")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
Name: &ast.Identifier{Value: []byte("C")},
Expr: &ast.ScalarLnumber{Value: []byte("1")},
}
stxTree := &ast.StmtStmtList{

View File

@@ -1603,7 +1603,7 @@ func (p *PrettyPrinter) printStmtClassConstList(n ast.Vertex) {
func (p *PrettyPrinter) printStmtConstant(n ast.Vertex) {
nn := n.(*ast.StmtConstant)
p.Print(nn.ConstantName)
p.Print(nn.Name)
io.WriteString(p.w, " = ")
p.Print(nn.Expr)
}

View File

@@ -2530,8 +2530,8 @@ func TestPrintStmtClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -2585,8 +2585,8 @@ func TestPrintStmtAnonymousClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -2616,12 +2616,12 @@ func TestPrintStmtClassConstList(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -2639,8 +2639,8 @@ func TestPrintStmtConstant(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
})
expected := "FOO = 'BAR'"
@@ -2676,8 +2676,8 @@ func TestPrintStmtDeclareStmts(t *testing.T) {
&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtStmtList{
@@ -2710,8 +2710,8 @@ func TestPrintStmtDeclareExpr(t *testing.T) {
&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
@@ -2737,8 +2737,8 @@ func TestPrintStmtDeclareNop(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtNop{},

View File

@@ -2421,69 +2421,27 @@ func (p *Printer) printStmtClass(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtClassConstList(n ast.Vertex) {
nn := n.(*ast.StmtClassConstList)
p.printFreeFloating(nn, token.Start)
if nn.Modifiers != nil {
for k, m := range nn.Modifiers {
if k > 0 && m.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.Print(m)
}
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
}
p.printFreeFloating(nn, token.ModifierList)
io.WriteString(p.w, "const")
if nn.Consts[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Consts)
p.printFreeFloating(nn, token.ConstList)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtClassConstList(n *ast.StmtClassConstList) {
p.joinPrintRefactored(" ", n.Modifiers)
p.bufStart = " "
p.printToken(n.ConstTkn, "const")
p.bufStart = " "
p.joinPrintRefactored(",", n.Consts)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtConstList(n ast.Vertex) {
nn := n.(*ast.StmtConstList)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "const")
if nn.Consts[0].GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, " ")
}
p.joinPrint(",", nn.Consts)
p.printFreeFloating(nn, token.Stmts)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtConstList(n *ast.StmtConstList) {
p.printToken(n.ConstTkn, "const")
p.bufStart = " "
p.joinPrintRefactored(",", n.Consts)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtConstant(n ast.Vertex) {
nn := n.(*ast.StmtConstant)
p.printFreeFloating(nn, token.Start)
p.Print(nn.ConstantName)
p.printFreeFloating(nn, token.Name)
io.WriteString(p.w, "=")
p.Print(nn.Expr)
p.printFreeFloating(nn, token.End)
func (p *Printer) printStmtConstant(n *ast.StmtConstant) {
p.Print(n.Name)
p.printToken(n.EqualTkn, "=")
p.Print(n.Expr)
p.printToken(n.CommaTkn, "")
}
func (p *Printer) printStmtContinue(n ast.Vertex) {
@@ -2515,7 +2473,7 @@ func (p *Printer) printStmtDeclare(n ast.Vertex) {
io.WriteString(p.w, "declare")
p.printFreeFloating(nn, token.Declare)
io.WriteString(p.w, "(")
p.joinPrint(",", nn.Consts)
p.joinPrintRefactored(",", nn.Consts)
p.printFreeFloating(nn, token.ConstList)
io.WriteString(p.w, ")")

View File

@@ -896,7 +896,8 @@ func TestParseAndPrintPhp5ClassConstList(t *testing.T) {
}
func TestParseAndPrintPhp5ConstList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
const FOO = 1 , BAR = 2 ;
`
@@ -924,7 +925,7 @@ func TestParseAndPrintPhp5Continue(t *testing.T) {
func TestParseAndPrintPhp5Declare(t *testing.T) {
src := `<?php
declare ( FOO = 'bar' ) ;
declare ( FOO = 'bar' , BAR = 'foo' ) ;
declare ( FOO = 'bar' ) $a ;
declare ( FOO = 'bar' ) { }

View File

@@ -1012,7 +1012,8 @@ func TestParseAndPrintClassConstList(t *testing.T) {
}
func TestParseAndPrintConstList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
const FOO = 1 , BAR = 2 ;
`
@@ -1040,7 +1041,7 @@ func TestParseAndPrintContinue(t *testing.T) {
func TestParseAndPrintDeclare(t *testing.T) {
src := `<?php
declare ( FOO = 'bar' ) ;
declare ( FOO = 'bar' , BAR = "foo" ) ;
declare ( FOO = 'bar' ) $a ;
declare ( FOO = 'bar' ) { }

View File

@@ -3028,8 +3028,8 @@ func TestPrinterPrintStmtClass(t *testing.T) {
},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -3078,8 +3078,8 @@ func TestPrinterPrintStmtAnonymousClass(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
},
@@ -3102,12 +3102,12 @@ func TestPrinterPrintStmtClassConstList(t *testing.T) {
Modifiers: []ast.Vertex{&ast.Identifier{Value: []byte("public")}},
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -3127,12 +3127,12 @@ func TestPrinterPrintStmtConstList(t *testing.T) {
p.Print(&ast.StmtConstList{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'a'")},
},
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
Name: &ast.Identifier{Value: []byte("BAR")},
Expr: &ast.ScalarString{Value: []byte("'b'")},
},
},
})
@@ -3150,8 +3150,8 @@ func TestPrinterPrintStmtConstant(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'BAR'")},
})
expected := "FOO='BAR'"
@@ -3187,8 +3187,8 @@ func TestPrinterPrintStmtDeclareStmts(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtStmtList{
@@ -3213,8 +3213,8 @@ func TestPrinterPrintStmtDeclareExpr(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtExpression{Expr: &ast.ScalarString{Value: []byte("'bar'")}},
@@ -3235,8 +3235,8 @@ func TestPrinterPrintStmtDeclareNop(t *testing.T) {
p.Print(&ast.StmtDeclare{
Consts: []ast.Vertex{
&ast.StmtConstant{
ConstantName: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
Name: &ast.Identifier{Value: []byte("FOO")},
Expr: &ast.ScalarString{Value: []byte("'bar'")},
},
},
Stmt: &ast.StmtNop{},