diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index ab1446d..52dc6df 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -4154,7 +4154,7 @@ func TestStmtClassConstList_WithoutModifiers(t *testing.T) { EndPos: 27, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -4186,7 +4186,7 @@ func TestStmtClassConstList_WithoutModifiers(t *testing.T) { EndPos: 36, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5124,7 +5124,7 @@ func TestStmtConstList(t *testing.T) { EndPos: 16, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5156,7 +5156,7 @@ func TestStmtConstList(t *testing.T) { EndPos: 25, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5451,7 +5451,7 @@ func TestStmtDeclare(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5530,7 +5530,7 @@ func TestStmtDeclare_Stmts(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5562,7 +5562,7 @@ func TestStmtDeclare_Stmts(t *testing.T) { EndPos: 34, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5642,7 +5642,7 @@ func TestStmtDeclare_Alt(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, diff --git a/internal/php5/php5.go b/internal/php5/php5.go index 926e1be..f836b8b 100644 Binary files a/internal/php5/php5.go and b/internal/php5/php5.go differ diff --git a/internal/php5/php5.y b/internal/php5/php5.y index 5d11a5d..181e9a4 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -447,14 +447,9 @@ top_statement: } | constant_declaration ';' { + $1.(*ast.StmtConstList).SemiColonTkn = $2 + $1.(*ast.StmtConstList).Node.Position = position.NewNodeTokenPosition($1, $2) $$ = $1 - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Stmts, $2.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens) } ; @@ -725,39 +720,52 @@ use_const_declaration: constant_declaration: constant_declaration ',' T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $3.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $5} constList := $1.(*ast.StmtConstList) - lastConst := lastNode(constList.Consts) - constList.Consts = append(constList.Consts, constant) + constList.Node.Position = position.NewNodesPosition($1, $5) + lastNode(constList.Consts).(*ast.StmtConstant).CommaTkn = $2 + constList.Consts = append(constList.Consts, &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($3, $5), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($3), + }, + Value: $3.Value, + }, + EqualTkn: $4, + Expr: $5, + }) + $$ = $1 - // save position - name.GetNode().Position = position.NewTokenPosition($3) - constant.GetNode().Position = position.NewTokenNodePosition($3, $5) - $$.GetNode().Position = position.NewNodeNodeListPosition($1, constList.Consts) - - // save comments - yylex.(*Parser).setFreeFloating(lastConst, token.End, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Start, $3.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $4.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtConstList).Consts).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens) } | T_CONST T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $2.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $4} - constList := []ast.Vertex{constant} - $$ = &ast.StmtConstList{ast.Node{}, constList} + $$ = &ast.StmtConstList{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $4), + }, + ConstTkn: $1, + Consts: []ast.Vertex{ + &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($2, $4), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($2), + }, + Value: $2.Value, + }, + EqualTkn: $3, + Expr: $4, + }, + }, + } - // save position - name.GetNode().Position = position.NewTokenPosition($2) - constant.GetNode().Position = position.NewTokenNodePosition($2, $4) - $$.GetNode().Position = position.NewTokenNodeListPosition($1, constList) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Start, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $3.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtConstList).Consts).(*ast.StmtConstant).Name, token.Start, $2.SkippedTokens) } ; @@ -1735,32 +1743,42 @@ declare_statement: declare_list: T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $1.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $3} - $$ = []ast.Vertex{constant} + $$ = []ast.Vertex{ + &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($1), + }, + Value: $1.Value, + }, + EqualTkn: $2, + Expr: $3, + }, + } - // save position - name.GetNode().Position = position.NewTokenPosition($1) - constant.GetNode().Position = position.NewTokenNodePosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating(constant, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $2.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$).(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens) } | declare_list ',' T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $3.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $5} - $$ = append($1, constant) + lastNode($1).(*ast.StmtConstant).CommaTkn = $2 + $$ = append($1, &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($3, $5), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($3), + }, + Value: $3.Value, + }, + EqualTkn: $4, + Expr: $5, + }) - // save position - name.GetNode().Position = position.NewTokenPosition($3) - constant.GetNode().Position = position.NewTokenNodePosition($3, $5) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Start, $3.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $4.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens) } ; @@ -2359,14 +2377,9 @@ class_statement: } | class_constant_declaration ';' { + $1.(*ast.StmtClassConstList).SemiColonTkn = $2 + $1.(*ast.StmtClassConstList).Node.Position = position.NewNodeTokenPosition($1, $2) $$ = $1 - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.ConstList, $2.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $2.SkippedTokens) } | trait_use_statement { @@ -2797,38 +2810,52 @@ class_variable_declaration: class_constant_declaration: class_constant_declaration ',' T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $3.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $5} constList := $1.(*ast.StmtClassConstList) - lastConst := lastNode(constList.Consts) - constList.Consts = append(constList.Consts, constant) + constList.Node.Position = position.NewNodesPosition($1, $5) + lastNode($$.(*ast.StmtClassConstList).Consts).(*ast.StmtConstant).CommaTkn = $2 + constList.Consts = append(constList.Consts, &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($3, $5), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($3), + }, + Value: $3.Value, + }, + EqualTkn: $4, + Expr: $5, + }) + $$ = $1 - // save position - name.GetNode().Position = position.NewTokenPosition($3) - constant.GetNode().Position = position.NewTokenNodePosition($3, $5) - $1.GetNode().Position = position.NewNodesPosition($1, $5) - - // save comments - yylex.(*Parser).setFreeFloating(lastConst, token.End, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Start, $3.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $4.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtClassConstList).Consts).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens) } | T_CONST T_STRING '=' static_scalar { - name := &ast.Identifier{ast.Node{}, $2.Value} - constant := &ast.StmtConstant{ast.Node{}, name, $4} - $$ = &ast.StmtClassConstList{ast.Node{}, nil, []ast.Vertex{constant}} + $$ = &ast.StmtClassConstList{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $4), + }, + ConstTkn: $1, + Consts: []ast.Vertex{ + &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($2, $4), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($2), + }, + Value: $2.Value, + }, + EqualTkn: $3, + Expr: $4, + }, + }, + } - // save position - name.GetNode().Position = position.NewTokenPosition($2) - constant.GetNode().Position = position.NewTokenNodePosition($2, $4) - $$.GetNode().Position = position.NewTokenNodePosition($1, $4) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Start, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(constant, token.Name, $3.SkippedTokens) + yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtClassConstList).Consts).(*ast.StmtConstant).Name, token.Start, $2.SkippedTokens) } ; diff --git a/internal/php5/php5_test.go b/internal/php5/php5_test.go index a8b3fcb..ad42cb2 100644 --- a/internal/php5/php5_test.go +++ b/internal/php5/php5_test.go @@ -3134,7 +3134,7 @@ func TestPhp5(t *testing.T) { EndPos: 1071, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 58, @@ -3166,7 +3166,7 @@ func TestPhp5(t *testing.T) { EndPos: 1080, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 58, @@ -3793,7 +3793,7 @@ func TestPhp5(t *testing.T) { EndPos: 1409, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 67, @@ -3825,7 +3825,7 @@ func TestPhp5(t *testing.T) { EndPos: 1418, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 67, @@ -4020,7 +4020,7 @@ func TestPhp5(t *testing.T) { EndPos: 1512, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 71, @@ -4074,7 +4074,7 @@ func TestPhp5(t *testing.T) { EndPos: 1530, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 72, @@ -4106,7 +4106,7 @@ func TestPhp5(t *testing.T) { EndPos: 1546, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 72, @@ -4162,7 +4162,7 @@ func TestPhp5(t *testing.T) { EndPos: 1566, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 73, diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index 4cb66c8..b0e66dd 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -4318,7 +4318,7 @@ func TestStmtClassConstList(t *testing.T) { EndPos: 34, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -4350,7 +4350,7 @@ func TestStmtClassConstList(t *testing.T) { EndPos: 43, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -4442,7 +4442,7 @@ func TestStmtClassConstList_WithoutModifiers(t *testing.T) { EndPos: 27, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -4474,7 +4474,7 @@ func TestStmtClassConstList_WithoutModifiers(t *testing.T) { EndPos: 36, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5721,7 +5721,7 @@ func TestStmtConstList(t *testing.T) { EndPos: 16, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -5753,7 +5753,7 @@ func TestStmtConstList(t *testing.T) { EndPos: 25, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -6048,7 +6048,7 @@ func TestStmtDeclare(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -6127,7 +6127,7 @@ func TestStmtDeclare_Stmts(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -6159,7 +6159,7 @@ func TestStmtDeclare_Stmts(t *testing.T) { EndPos: 34, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -6239,7 +6239,7 @@ func TestStmtDeclare_Alt(t *testing.T) { EndPos: 18, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, diff --git a/internal/php7/php7.go b/internal/php7/php7.go index f9ec7d4..39c7876 100644 Binary files a/internal/php7/php7.go and b/internal/php7/php7.go differ diff --git a/internal/php7/php7.y b/internal/php7/php7.y index 3013dfd..ab3d550 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -536,15 +536,14 @@ top_statement: } | T_CONST const_list ';' { - $$ = &ast.StmtConstList{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokensPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens) + $$ = &ast.StmtConstList{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $3), + }, + ConstTkn: $1, + Consts: $2, + SemiColonTkn: $3, + } } ; @@ -782,10 +781,9 @@ use_declaration: const_list: const_list ',' const_decl { - $$ = append($1, $3) + lastNode($1).(*ast.StmtConstant).CommaTkn = $2 - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) + $$ = append($1, $3) } | const_decl { @@ -2198,20 +2196,15 @@ class_statement: } | method_modifiers T_CONST class_const_list ';' { - $$ = &ast.StmtClassConstList{ast.Node{}, $1, $3} - - // save position - $$.GetNode().Position = position.NewOptionalListTokensPosition($1, $2, $4) - - // save comments - if len($1) > 0 { - yylex.(*Parser).MoveFreeFloating($1[0], $$) - yylex.(*Parser).setFreeFloating($$, token.ModifierList, $2.SkippedTokens) - } else { - yylex.(*Parser).setFreeFloating($$, token.Start, $2.SkippedTokens) + $$ = &ast.StmtClassConstList{ + Node: ast.Node{ + Position: position.NewOptionalListTokensPosition($1, $2, $4), + }, + Modifiers: $1, + ConstTkn: $2, + Consts: $3, + SemiColonTkn: $4, } - yylex.(*Parser).setFreeFloating($$, token.ConstList, $4.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens) } | T_USE name_list trait_adaptations { @@ -2616,10 +2609,9 @@ property: class_const_list: class_const_list ',' class_const_decl { - $$ = append($1, $3) + lastNode($1).(*ast.StmtConstant).CommaTkn = $2 - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) + $$ = append($1, $3) } | class_const_decl { @@ -2630,32 +2622,42 @@ class_const_list: class_const_decl: identifier '=' expr backup_doc_comment { - name := &ast.Identifier{ast.Node{}, $1.Value} - $$ = &ast.StmtConstant{ast.Node{}, name, $3} + $$ = &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($1), + }, + Value: $1.Value, + }, + EqualTkn: $2, + Expr: $3, + } - // save position - name.GetNode().Position = position.NewTokenPosition($1) - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Name, $2.SkippedTokens) + yylex.(*Parser).setFreeFloating($$.(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens) } ; const_decl: T_STRING '=' expr backup_doc_comment { - name := &ast.Identifier{ast.Node{}, $1.Value} - $$ = &ast.StmtConstant{ast.Node{}, name, $3} + $$ = &ast.StmtConstant{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + Name: &ast.Identifier{ + Node: ast.Node{ + Position: position.NewTokenPosition($1), + }, + Value: $1.Value, + }, + EqualTkn: $2, + Expr: $3, + } - // save position - name.GetNode().Position = position.NewTokenPosition($1) - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Name, $2.SkippedTokens) + yylex.(*Parser).setFreeFloating($$.(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens) } ; diff --git a/internal/php7/php7_test.go b/internal/php7/php7_test.go index d717743..8f474fa 100644 --- a/internal/php7/php7_test.go +++ b/internal/php7/php7_test.go @@ -3919,7 +3919,7 @@ func TestPhp7(t *testing.T) { EndPos: 1322, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 70, @@ -3951,7 +3951,7 @@ func TestPhp7(t *testing.T) { EndPos: 1331, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 70, @@ -4018,7 +4018,7 @@ func TestPhp7(t *testing.T) { EndPos: 1359, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 71, @@ -4050,7 +4050,7 @@ func TestPhp7(t *testing.T) { EndPos: 1368, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 71, @@ -4732,7 +4732,7 @@ func TestPhp7(t *testing.T) { EndPos: 1693, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 81, @@ -4764,7 +4764,7 @@ func TestPhp7(t *testing.T) { EndPos: 1702, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 81, @@ -4959,7 +4959,7 @@ func TestPhp7(t *testing.T) { EndPos: 1796, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 85, @@ -5013,7 +5013,7 @@ func TestPhp7(t *testing.T) { EndPos: 1814, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 86, @@ -5069,7 +5069,7 @@ func TestPhp7(t *testing.T) { EndPos: 1834, }, }, - ConstantName: &ast.Identifier{ + Name: &ast.Identifier{ Node: ast.Node{ Position: &position.Position{ StartLine: 87, diff --git a/pkg/ast/node.go b/pkg/ast/node.go index 4b633fe..038dc46 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -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) { diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index 7e41a44..3d9d5a5 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -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 { diff --git a/pkg/ast/visitor/filter_tokens.go b/pkg/ast/visitor/filter_tokens.go index ff7f336..5cd2215 100644 --- a/pkg/ast/visitor/filter_tokens.go +++ b/pkg/ast/visitor/filter_tokens.go @@ -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 +} diff --git a/pkg/ast/visitor/namespace_resolver.go b/pkg/ast/visitor/namespace_resolver.go index fb5659d..3eac4a1 100644 --- a/pkg/ast/visitor/namespace_resolver.go +++ b/pkg/ast/visitor/namespace_resolver.go @@ -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)) } } diff --git a/pkg/ast/visitor/namespace_resolver_test.go b/pkg/ast/visitor/namespace_resolver_test.go index 5b01ecc..8e84e88 100644 --- a/pkg/ast/visitor/namespace_resolver_test.go +++ b/pkg/ast/visitor/namespace_resolver_test.go @@ -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{ diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index fca19fd..cd5d20f 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -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) } diff --git a/pkg/printer/pretty_printer_test.go b/pkg/printer/pretty_printer_test.go index c457cb8..343703b 100644 --- a/pkg/printer/pretty_printer_test.go +++ b/pkg/printer/pretty_printer_test.go @@ -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{}, diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 450f928..f87cd9b 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -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, ")") diff --git a/pkg/printer/printer_parsed_php5_test.go b/pkg/printer/printer_parsed_php5_test.go index a65bd6b..5c14c0f 100644 --- a/pkg/printer/printer_parsed_php5_test.go +++ b/pkg/printer/printer_parsed_php5_test.go @@ -896,7 +896,8 @@ func TestParseAndPrintPhp5ClassConstList(t *testing.T) { } func TestParseAndPrintPhp5ConstList(t *testing.T) { - src := `