refactoring: remove ExprReference node

This commit is contained in:
Vadym Slizov 2020-12-26 22:38:35 +02:00
parent 0f5f5e7dc7
commit a593760569
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
13 changed files with 57 additions and 113 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -1000,12 +1000,12 @@ unticked_statement:
foreach.Var = $6.(*ast.StmtForeach).Var foreach.Var = $6.(*ast.StmtForeach).Var
} }
if val, ok := foreach.Key.(*ast.ExprReference); ok { if val, ok := foreach.Key.(*ast.StmtForeach); ok {
yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position)) yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position))
foreach.Key = val.Var foreach.Key = val.Var
} }
if val, ok := foreach.Var.(*ast.ExprReference); ok { if val, ok := foreach.Var.(*ast.StmtForeach); ok {
foreach.AmpersandTkn = val.AmpersandTkn foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var foreach.Var = val.Var
} }
@ -1030,12 +1030,12 @@ unticked_statement:
foreach.Var = $6.(*ast.StmtForeach).Var foreach.Var = $6.(*ast.StmtForeach).Var
} }
if val, ok := foreach.Key.(*ast.ExprReference); ok { if val, ok := foreach.Key.(*ast.StmtForeach); ok {
yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position)) yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position))
foreach.Key = val.Var foreach.Key = val.Var
} }
if val, ok := foreach.Var.(*ast.ExprReference); ok { if val, ok := foreach.Var.(*ast.StmtForeach); ok {
foreach.AmpersandTkn = val.AmpersandTkn foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var foreach.Var = val.Var
} }
@ -1462,7 +1462,7 @@ foreach_variable:
} }
| '&' variable | '&' variable
{ {
$$ = &ast.ExprReference{ $$ = &ast.StmtForeach{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1, AmpersandTkn: $1,
Var: $2, Var: $2,

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -972,7 +972,7 @@ statement:
foreach.Var = $5 foreach.Var = $5
foreach.CloseParenthesisTkn = $6 foreach.CloseParenthesisTkn = $6
if val, ok := $5.(*ast.ExprReference); ok { if val, ok := $5.(*ast.StmtForeach); ok {
foreach.AmpersandTkn = val.AmpersandTkn foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var foreach.Var = val.Var
} }
@ -993,7 +993,7 @@ statement:
foreach.Var = $7 foreach.Var = $7
foreach.CloseParenthesisTkn = $8 foreach.CloseParenthesisTkn = $8
if val, ok := $7.(*ast.ExprReference); ok { if val, ok := $7.(*ast.StmtForeach); ok {
foreach.AmpersandTkn = val.AmpersandTkn foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var foreach.Var = val.Var
} }
@ -1359,7 +1359,7 @@ foreach_variable:
} }
| '&' variable | '&' variable
{ {
$$ = &ast.ExprReference{ $$ = &ast.StmtForeach{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
AmpersandTkn: $1, AmpersandTkn: $1,
Var: $2, Var: $2,

View File

@ -107,7 +107,6 @@ type NodeVisitor interface {
ExprPreInc(n *ExprPreInc) ExprPreInc(n *ExprPreInc)
ExprPrint(n *ExprPrint) ExprPrint(n *ExprPrint)
ExprPropertyFetch(n *ExprPropertyFetch) ExprPropertyFetch(n *ExprPropertyFetch)
ExprReference(n *ExprReference)
ExprRequire(n *ExprRequire) ExprRequire(n *ExprRequire)
ExprRequireOnce(n *ExprRequireOnce) ExprRequireOnce(n *ExprRequireOnce)
ExprShellExec(n *ExprShellExec) ExprShellExec(n *ExprShellExec)

View File

@ -1629,21 +1629,6 @@ func (n *ExprPropertyFetch) GetPosition() *position.Position {
return n.Position return n.Position
} }
// ExprReference node
type ExprReference struct {
Position *position.Position
AmpersandTkn *token.Token
Var Vertex
}
func (n *ExprReference) Accept(v NodeVisitor) {
v.ExprReference(n)
}
func (n *ExprReference) GetPosition() *position.Position {
return n.Position
}
// ExprRequire node // ExprRequire node
type ExprRequire struct { type ExprRequire struct {
Position *position.Position Position *position.Position

View File

@ -1451,18 +1451,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Property) t.Traverse(nn.Property)
t.visitor.Leave("Property", true) t.visitor.Leave("Property", true)
} }
case *ast.ExprReference:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Var != nil {
t.visitor.Enter("Var", true)
t.Traverse(nn.Var)
t.visitor.Leave("Var", true)
}
case *ast.ExprRequire: case *ast.ExprRequire:
if nn == nil { if nn == nil {
return return

View File

@ -1425,18 +1425,6 @@ func (v *Dumper) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
v.print(v.indent, "},\n") v.print(v.indent, "},\n")
} }
func (v *Dumper) ExprReference(n *ast.ExprReference) {
v.print(0, "&ast.ExprReference{\n")
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Var", n.Var)
v.indent--
v.print(v.indent, "},\n")
}
func (v *Dumper) ExprRequire(n *ast.ExprRequire) { func (v *Dumper) ExprRequire(n *ast.ExprRequire) {
v.print(0, "&ast.ExprRequire{\n") v.print(0, "&ast.ExprRequire{\n")
v.indent++ v.indent++

View File

@ -1349,11 +1349,6 @@ func (f *formatter) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
n.Property.Accept(f) n.Property.Accept(f)
} }
func (f *formatter) ExprReference(n *ast.ExprReference) {
n.AmpersandTkn = f.newToken('&', []byte("&"))
n.Var.Accept(f)
}
func (f *formatter) ExprRequire(n *ast.ExprRequire) { func (f *formatter) ExprRequire(n *ast.ExprRequire) {
n.RequireTkn = f.newToken(token.T_REQUIRE, []byte("require")) n.RequireTkn = f.newToken(token.T_REQUIRE, []byte("require"))
f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) f.addFreeFloating(token.T_WHITESPACE, []byte(" "))

View File

@ -4483,31 +4483,6 @@ func TestFormatter_ExprPropertyFetch(t *testing.T) {
} }
} }
func TestFormatter_ExprReference(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprReference{
Var: &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_ExprRequire(t *testing.T) { func TestFormatter_ExprRequire(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")

View File

@ -362,10 +362,6 @@ func (v *Null) ExprPropertyFetch(_ *ast.ExprPropertyFetch) {
// do nothing // do nothing
} }
func (v *Null) ExprReference(_ *ast.ExprReference) {
// do nothing
}
func (v *Null) ExprRequire(_ *ast.ExprRequire) { func (v *Null) ExprRequire(_ *ast.ExprRequire) {
// do nothing // do nothing
} }

View File

@ -817,11 +817,6 @@ func (p *printer) ExprPropertyFetch(n *ast.ExprPropertyFetch) {
p.printNode(n.Property) p.printNode(n.Property)
} }
func (p *printer) ExprReference(n *ast.ExprReference) {
p.printToken(n.AmpersandTkn, []byte("&"))
p.printNode(n.Var)
}
func (p *printer) ExprRequire(n *ast.ExprRequire) { func (p *printer) ExprRequire(n *ast.ExprRequire) {
p.printToken(n.RequireTkn, []byte("require")) p.printToken(n.RequireTkn, []byte("require"))
p.printNode(n.Expr) p.printNode(n.Expr)

View File

@ -1634,9 +1634,12 @@ func TestPrinterPrintExprArray(t *testing.T) {
}, },
&ast.ExprArrayItem{ &ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")}, Key: &ast.ScalarLnumber{Value: []byte("2")},
Val: &ast.ExprReference{Var: &ast.ExprVariable{ AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")}, VarName: &ast.Identifier{Value: []byte("$var")},
}}, },
}, },
&ast.ExprArrayItem{ &ast.ExprArrayItem{
Val: &ast.ExprVariable{ Val: &ast.ExprVariable{
@ -2323,25 +2326,6 @@ func TestPrinterPrintPropertyFetch(t *testing.T) {
} }
} }
func TestPrinterPrintExprReference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprReference{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$foo")},
},
}
n.Accept(p)
expected := `&$foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintRequire(t *testing.T) { func TestPrinterPrintRequire(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
@ -2413,9 +2397,12 @@ func TestPrinterPrintExprShortArray(t *testing.T) {
}, },
&ast.ExprArrayItem{ &ast.ExprArrayItem{
Key: &ast.ScalarLnumber{Value: []byte("2")}, Key: &ast.ScalarLnumber{Value: []byte("2")},
Val: &ast.ExprReference{Var: &ast.ExprVariable{ AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Val: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")}, VarName: &ast.Identifier{Value: []byte("$var")},
}}, },
}, },
&ast.ExprArrayItem{ &ast.ExprArrayItem{
Val: &ast.ExprVariable{ Val: &ast.ExprVariable{
@ -2848,11 +2835,47 @@ func TestPrinterPrintAltForeach(t *testing.T) {
Key: &ast.ExprVariable{ Key: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$key")}, VarName: &ast.Identifier{Value: []byte("$key")},
}, },
Var: &ast.ExprReference{ Var: &ast.ExprVariable{
Var: &ast.ExprVariable{ VarName: &ast.Identifier{Value: []byte("$val")},
VarName: &ast.Identifier{Value: []byte("$val")}, },
ColonTkn: &token.Token{
Value: []byte(":"),
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$d")},
}},
}, },
}, },
}
n.Accept(p)
expected := `foreach($var as$key=>$val):$d;endforeach;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintAltForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
Key: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$key")},
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$val")},
},
ColonTkn: &token.Token{ ColonTkn: &token.Token{
Value: []byte(":"), Value: []byte(":"),
}, },