refactoring: update ast structure of "Closure" and "ClosureUse" nodes

This commit is contained in:
Vadym Slizov 2020-12-26 18:20:10 +02:00
parent 03c7979ccd
commit b85bae2ec1
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
14 changed files with 654 additions and 589 deletions

View File

@ -31917,13 +31917,6 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 19,
},
},
ClosureUse: &ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
UseTkn: &token.Token{
ID: token.T_USE,
Value: []byte("use"),
@ -31946,7 +31939,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
OpenParenthesisTkn: &token.Token{
UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
@ -31968,8 +31961,15 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
Uses: []ast.Vertex{
&ast.ExprVariable{
Use: []ast.Vertex{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -31996,7 +31996,8 @@ func TestExprClosure_Use(t *testing.T) {
Value: []byte("$c"),
},
},
&ast.ExprReference{
},
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -32054,7 +32055,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
SeparatorTkns: []*token.Token{
UseSeparatorTkns: []*token.Token{
{
ID: token.ID(44),
Value: []byte(","),
@ -32066,7 +32067,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
CloseParenthesisTkn: &token.Token{
UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
@ -32076,7 +32077,6 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 33,
},
},
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
@ -32123,7 +32123,8 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
EndTkn: &token.Token{},
EndTkn: &token.Token{
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -32306,13 +32307,6 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 19,
},
},
ClosureUse: &ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
UseTkn: &token.Token{
ID: token.T_USE,
Value: []byte("use"),
@ -32335,7 +32329,7 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
OpenParenthesisTkn: &token.Token{
UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
@ -32357,8 +32351,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
Uses: []ast.Vertex{
&ast.ExprReference{
Use: []ast.Vertex{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -32403,7 +32397,14 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
&ast.ExprVariable{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -32443,7 +32444,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
SeparatorTkns: []*token.Token{
},
UseSeparatorTkns: []*token.Token{
{
ID: token.ID(44),
Value: []byte(","),
@ -32455,7 +32457,7 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
CloseParenthesisTkn: &token.Token{
UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
@ -32465,7 +32467,6 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 33,
},
},
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
@ -32512,7 +32513,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
EndTkn: &token.Token{},
EndTkn: &token.Token{
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -3367,36 +3367,38 @@ expr_without_variable:
}
| function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $9),
FunctionTkn: $1,
AmpersandTkn: $2,
OpenParenthesisTkn: $3,
Params: $4.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $4.(*ast.ParserSeparatedList).SeparatorTkns,
CloseParenthesisTkn: $5,
ClosureUse: $6,
OpenCurlyBracketTkn: $7,
Stmts: $8,
CloseCurlyBracketTkn: $9,
}
closure := $6.(*ast.ExprClosure)
closure.Position = yylex.(*Parser).builder.NewTokensPosition($1, $9)
closure.FunctionTkn = $1
closure.AmpersandTkn = $2
closure.OpenParenthesisTkn = $3
closure.Params = $4.(*ast.ParserSeparatedList).Items
closure.SeparatorTkns = $4.(*ast.ParserSeparatedList).SeparatorTkns
closure.CloseParenthesisTkn = $5
closure.OpenCurlyBracketTkn = $7
closure.Stmts = $8
closure.CloseCurlyBracketTkn = $9
$$ = closure
}
| T_STATIC function is_reference '(' parameter_list ')' lexical_vars '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $10),
StaticTkn: $1,
FunctionTkn: $2,
AmpersandTkn: $3,
OpenParenthesisTkn: $4,
Params: $5.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $5.(*ast.ParserSeparatedList).SeparatorTkns,
CloseParenthesisTkn: $6,
ClosureUse: $7,
OpenCurlyBracketTkn: $8,
Stmts: $9,
CloseCurlyBracketTkn: $10,
}
closure := $7.(*ast.ExprClosure)
closure.Position = yylex.(*Parser).builder.NewTokensPosition($1, $10)
closure.StaticTkn = $1
closure.FunctionTkn = $2
closure.AmpersandTkn = $3
closure.OpenParenthesisTkn = $4
closure.Params = $5.(*ast.ParserSeparatedList).Items
closure.SeparatorTkns = $5.(*ast.ParserSeparatedList).SeparatorTkns
closure.CloseParenthesisTkn = $6
closure.OpenCurlyBracketTkn = $8
closure.Stmts = $9
closure.CloseCurlyBracketTkn = $10
$$ = closure
}
;
@ -3520,17 +3522,16 @@ function:
lexical_vars:
/* empty */
{
$$ = nil
$$ = &ast.ExprClosure{}
}
| T_USE '(' lexical_var_list ')'
{
$$ = &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
$$ = &ast.ExprClosure{
UseTkn: $1,
OpenParenthesisTkn: $2,
Uses: $3.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
CloseParenthesisTkn: $4,
UseOpenParenthesisTkn: $2,
Use: $3.(*ast.ParserSeparatedList).Items,
UseSeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
UseCloseParenthesisTkn: $4,
}
}
;
@ -3538,13 +3539,16 @@ lexical_vars:
lexical_var_list:
lexical_var_list ',' T_VARIABLE
{
variable := &ast.ExprVariable{
variable := &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokenPosition($3),
Var: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($3),
VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($3),
IdentifierTkn: $3,
Value: $3.Value,
},
},
}
$1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2)
@ -3554,7 +3558,7 @@ lexical_var_list:
}
| lexical_var_list ',' '&' T_VARIABLE
{
reference := &ast.ExprReference{
variable := &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokensPosition($3, $4),
AmpersandTkn: $3,
Var: &ast.ExprVariable{
@ -3568,15 +3572,15 @@ lexical_var_list:
}
$1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2)
$1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, reference)
$1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, variable)
$$ = $1
}
| T_VARIABLE
{
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{
&ast.ExprVariable{
variable := &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
Var: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
@ -3584,14 +3588,15 @@ lexical_var_list:
Value: $1.Value,
},
},
},
}
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{ variable },
}
}
| '&' T_VARIABLE
{
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{
&ast.ExprReference{
variable := &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $2),
AmpersandTkn: $1,
Var: &ast.ExprVariable{
@ -3602,8 +3607,10 @@ lexical_var_list:
Value: $2.Value,
},
},
},
},
}
$$ = &ast.ParserSeparatedList{
Items: []ast.Vertex{ variable },
}
}
;

View File

@ -36415,13 +36415,6 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 19,
},
},
ClosureUse: &ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
UseTkn: &token.Token{
ID: token.T_USE,
Value: []byte("use"),
@ -36444,7 +36437,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
OpenParenthesisTkn: &token.Token{
UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
@ -36466,8 +36459,15 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
Uses: []ast.Vertex{
&ast.ExprVariable{
Use: []ast.Vertex{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -36494,7 +36494,8 @@ func TestExprClosure_Use(t *testing.T) {
Value: []byte("$c"),
},
},
&ast.ExprReference{
},
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -36552,7 +36553,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
SeparatorTkns: []*token.Token{
UseSeparatorTkns: []*token.Token{
{
ID: token.ID(44),
Value: []byte(","),
@ -36564,7 +36565,7 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
CloseParenthesisTkn: &token.Token{
UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
@ -36574,7 +36575,6 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 33,
},
},
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
@ -36621,7 +36621,8 @@ func TestExprClosure_Use(t *testing.T) {
},
},
},
EndTkn: &token.Token{},
EndTkn: &token.Token{
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -36804,13 +36805,6 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 19,
},
},
ClosureUse: &ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
UseTkn: &token.Token{
ID: token.T_USE,
Value: []byte("use"),
@ -36833,7 +36827,7 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
OpenParenthesisTkn: &token.Token{
UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
@ -36855,8 +36849,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
Uses: []ast.Vertex{
&ast.ExprReference{
Use: []ast.Vertex{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -36901,7 +36895,14 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
&ast.ExprVariable{
&ast.ExprClosureUse{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
@ -36941,7 +36942,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
SeparatorTkns: []*token.Token{
},
UseSeparatorTkns: []*token.Token{
{
ID: token.ID(44),
Value: []byte(","),
@ -36953,7 +36955,7 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
CloseParenthesisTkn: &token.Token{
UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
@ -36963,7 +36965,6 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 33,
},
},
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
@ -37010,7 +37011,8 @@ func TestExprClosure_Use2(t *testing.T) {
},
},
},
EndTkn: &token.Token{},
EndTkn: &token.Token{
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -3178,21 +3178,22 @@ expr_without_variable:
inline_function:
T_FUNCTION returns_ref backup_doc_comment '(' parameter_list ')' lexical_vars return_type '{' inner_statement_list '}'
{
$$ = &ast.ExprClosure{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $11),
FunctionTkn: $1,
AmpersandTkn: $2,
OpenParenthesisTkn: $4,
Params: $5.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $5.(*ast.ParserSeparatedList).SeparatorTkns,
CloseParenthesisTkn: $6,
ClosureUse: $7,
ColonTkn: $8.(*ast.ReturnType).ColonTkn,
ReturnType: $8.(*ast.ReturnType).Type,
OpenCurlyBracketTkn: $9,
Stmts: $10,
CloseCurlyBracketTkn: $11,
}
closure := $7.(*ast.ExprClosure)
closure.Position = yylex.(*Parser).builder.NewTokensPosition($1, $11)
closure.FunctionTkn = $1
closure.AmpersandTkn = $2
closure.OpenParenthesisTkn = $4
closure.Params = $5.(*ast.ParserSeparatedList).Items
closure.SeparatorTkns = $5.(*ast.ParserSeparatedList).SeparatorTkns
closure.CloseParenthesisTkn = $6
closure.ColonTkn = $8.(*ast.ReturnType).ColonTkn
closure.ReturnType = $8.(*ast.ReturnType).Type
closure.OpenCurlyBracketTkn = $9
closure.Stmts = $10
closure.CloseCurlyBracketTkn = $11
$$ = closure
}
| T_FN returns_ref '(' parameter_list ')' return_type backup_doc_comment T_DOUBLE_ARROW expr
{
@ -3230,17 +3231,16 @@ returns_ref:
lexical_vars:
/* empty */
{
$$ = nil
$$ = &ast.ExprClosure{}
}
| T_USE '(' lexical_var_list ')'
{
$$ = &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $4),
$$ = &ast.ExprClosure{
UseTkn: $1,
OpenParenthesisTkn: $2,
Uses: $3.(*ast.ParserSeparatedList).Items,
SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
CloseParenthesisTkn: $4,
UseOpenParenthesisTkn: $2,
Use: $3.(*ast.ParserSeparatedList).Items,
UseSeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns,
UseCloseParenthesisTkn: $4,
}
}
;
@ -3264,18 +3264,21 @@ lexical_var_list:
lexical_var:
T_VARIABLE
{
$$ = &ast.ExprVariable{
$$ = &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
Var: &ast.ExprVariable{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
VarName: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
IdentifierTkn: $1,
Value: $1.Value,
},
},
}
}
| '&' T_VARIABLE
{
$$ = &ast.ExprReference{
$$ = &ast.ExprClosureUse{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $2),
AmpersandTkn: $1,
Var: &ast.ExprVariable{

View File

@ -1282,7 +1282,11 @@ type ExprClosure struct {
Params []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
ClosureUse Vertex
UseTkn *token.Token
UseOpenParenthesisTkn *token.Token
Use []Vertex
UseSeparatorTkns []*token.Token
UseCloseParenthesisTkn *token.Token
ColonTkn *token.Token
ReturnType Vertex
OpenCurlyBracketTkn *token.Token
@ -1301,11 +1305,8 @@ func (n *ExprClosure) GetPosition() *position.Position {
// ExprClosureUse node
type ExprClosureUse struct {
Position *position.Position
UseTkn *token.Token
OpenParenthesisTkn *token.Token
Uses []Vertex
SeparatorTkns []*token.Token
CloseParenthesisTkn *token.Token
AmpersandTkn *token.Token
Var Vertex
}
func (n *ExprClosureUse) Accept(v NodeVisitor) {

View File

@ -1152,10 +1152,12 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
t.visitor.Leave("Params", false)
}
if nn.ClosureUse != nil {
t.visitor.Enter("ClosureUse", true)
t.Traverse(nn.ClosureUse)
t.visitor.Leave("ClosureUse", true)
if nn.Use != nil {
t.visitor.Enter("Use", false)
for _, c := range nn.Use {
t.Traverse(c)
}
t.visitor.Leave("Use", false)
}
if nn.ReturnType != nil {
t.visitor.Enter("ReturnType", true)
@ -1176,12 +1178,10 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.Uses != nil {
t.visitor.Enter("Uses", false)
for _, c := range nn.Uses {
t.Traverse(c)
}
t.visitor.Leave("Uses", false)
if nn.Var != nil {
t.visitor.Enter("Var", true)
t.Traverse(nn.Var)
t.visitor.Leave("Var", true)
}
case *ast.ExprConstFetch:
if nn == nil {

View File

@ -1143,7 +1143,11 @@ func (v *Dumper) ExprClosure(n *ast.ExprClosure) {
v.dumpVertexList("Params", n.Params)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
v.dumpVertex("ClosureUse", n.ClosureUse)
v.dumpToken("UseTkn", n.UseTkn)
v.dumpToken("UseOpenParenthesisTkn", n.UseOpenParenthesisTkn)
v.dumpVertexList("Use", n.Use)
v.dumpTokenList("UseSeparatorTkns", n.UseSeparatorTkns)
v.dumpToken("UseCloseParenthesisTkn", n.UseCloseParenthesisTkn)
v.dumpToken("ColonTkn", n.ColonTkn)
v.dumpVertex("ReturnType", n.ReturnType)
v.dumpToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
@ -1159,11 +1163,8 @@ func (v *Dumper) ExprClosureUse(n *ast.ExprClosureUse) {
v.indent++
v.dumpPosition(n.Position)
v.dumpToken("UseTkn", n.UseTkn)
v.dumpToken("OpenParenthesisTkn", n.OpenParenthesisTkn)
v.dumpVertexList("Uses", n.Uses)
v.dumpTokenList("SeparatorTkns", n.SeparatorTkns)
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Var", n.Var)
v.indent--
v.print(v.indent, "},\n")

View File

@ -1162,9 +1162,16 @@ func (f *formatter) ExprClosure(n *ast.ExprClosure) {
}
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
if n.ClosureUse != nil {
n.UseTkn = nil
n.UseOpenParenthesisTkn = nil
n.UseCloseParenthesisTkn = nil
n.UseSeparatorTkns = nil
if len(n.Use) > 0 {
f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
n.ClosureUse.Accept(f)
n.UseTkn = f.newToken(token.T_USE, []byte("use"))
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
n.SeparatorTkns = f.formatList(n.Use, ',')
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
}
n.ColonTkn = nil
@ -1189,10 +1196,11 @@ func (f *formatter) ExprClosure(n *ast.ExprClosure) {
}
func (f *formatter) ExprClosureUse(n *ast.ExprClosureUse) {
n.UseTkn = f.newToken(token.T_USE, []byte("use"))
n.OpenParenthesisTkn = f.newToken('(', []byte("("))
n.SeparatorTkns = f.formatList(n.Uses, ',')
n.CloseParenthesisTkn = f.newToken(')', []byte(")"))
if n.AmpersandTkn != nil {
n.AmpersandTkn = f.newToken('&', []byte("&"))
}
n.Var.Accept(f)
}
func (f *formatter) ExprConstFetch(n *ast.ExprConstFetch) {

View File

@ -3714,9 +3714,9 @@ func TestFormatter_ExprClosure_Use(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprClosure{
ClosureUse: &ast.ExprClosureUse{
Uses: []ast.Vertex{
&ast.ExprVariable{
Use: []ast.Vertex{
&ast.ExprClosureUse{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$foo"),
},
@ -3748,18 +3748,11 @@ func TestFormatter_ExprClosureUse(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprClosureUse{
Uses: []ast.Vertex{
&ast.ExprVariable{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$a"),
},
},
&ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$b"),
},
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
@ -3768,7 +3761,33 @@ func TestFormatter_ExprClosureUse(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `use($a, $b)`
expected := `$a`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_ExprClosureUse_Reference(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.ExprClosureUse{
AmpersandTkn: &token.Token{},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$a"),
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `&$a`
actual := o.String()
if expected != actual {

View File

@ -562,7 +562,6 @@ func TestResolveClosureName(t *testing.T) {
Var: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}},
},
},
ClosureUse: nil,
ReturnType: &ast.Nullable{Expr: nameBC},
Stmts: []ast.Vertex{},
}

View File

@ -684,7 +684,10 @@ func (p *printer) ExprClosure(n *ast.ExprClosure) {
p.printToken(n.OpenParenthesisTkn, []byte("("))
p.printSeparatedList(n.Params, n.SeparatorTkns, []byte(","))
p.printToken(n.CloseParenthesisTkn, []byte(")"))
p.printNode(n.ClosureUse)
p.printToken(n.UseTkn, p.ifNodeList(n.Use, []byte("use")))
p.printToken(n.UseOpenParenthesisTkn, p.ifNodeList(n.Use, []byte("(")))
p.printSeparatedList(n.Use, n.UseSeparatorTkns, []byte(","))
p.printToken(n.UseCloseParenthesisTkn, p.ifNodeList(n.Use, []byte(")")))
p.printToken(n.ColonTkn, p.ifNode(n.ReturnType, []byte(":")))
p.printNode(n.ReturnType)
p.printToken(n.OpenCurlyBracketTkn, []byte("{"))
@ -693,10 +696,8 @@ func (p *printer) ExprClosure(n *ast.ExprClosure) {
}
func (p *printer) ExprClosureUse(n *ast.ExprClosureUse) {
p.printToken(n.UseTkn, []byte("use"))
p.printToken(n.OpenParenthesisTkn, []byte("("))
p.printSeparatedList(n.Uses, n.SeparatorTkns, []byte(","))
p.printToken(n.CloseParenthesisTkn, []byte(")"))
p.printToken(n.AmpersandTkn, nil)
p.printNode(n.Var)
}
func (p *printer) ExprConstFetch(n *ast.ExprConstFetch) {

View File

@ -1717,18 +1717,35 @@ func TestPrinterPrintExprClosureUse(t *testing.T) {
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprClosureUse{
Uses: []ast.Vertex{
&ast.ExprReference{Var: &ast.ExprVariable{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$foo")},
}},
&ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$bar")},
},
},
}
n.Accept(p)
expected := `use(&$foo,$bar)`
expected := `$foo`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintExprClosureUse_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.ExprClosureUse{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$foo")},
},
}
n.Accept(p)
expected := `&$foo`
actual := o.String()
if expected != actual {
@ -1754,12 +1771,17 @@ func TestPrinterPrintExprClosure(t *testing.T) {
},
},
},
ClosureUse: &ast.ExprClosureUse{
Uses: []ast.Vertex{
&ast.ExprReference{Var: &ast.ExprVariable{
Use: []ast.Vertex{
&ast.ExprClosureUse{
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
}},
&ast.ExprVariable{
},
},
&ast.ExprClosureUse{
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")},
},
},