diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 6ab5b7a..ab1446d 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -8546,7 +8546,7 @@ func TestStmtNamespace(t *testing.T) { EndPos: 17, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -8604,7 +8604,7 @@ func TestStmtNamespace_Stmts(t *testing.T) { EndPos: 19, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, diff --git a/internal/php5/php5.go b/internal/php5/php5.go index a1be3c0..b074c96 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 a462f00..668b080 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -301,26 +301,26 @@ top_statement_list: namespace_name: T_STRING { - namePart := &ast.NameNamePart{ast.Node{}, $1.Value} - $$ = []ast.Vertex{namePart} - - // save position - namePart.GetNode().Position = position.NewTokenPosition($1) - - // save comments - yylex.(*Parser).setFreeFloating(namePart, token.Start, $1.SkippedTokens) + $$ = []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: position.NewTokenPosition($1), + }, + StringTkn: $1, + Value: $1.Value, + }, + } } | namespace_name T_NS_SEPARATOR T_STRING { - namePart := &ast.NameNamePart{ast.Node{}, $3.Value} - $$ = append($1, namePart) - - // save position - namePart.GetNode().Position = position.NewTokenPosition($3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(namePart, token.Start, $3.SkippedTokens) + $$ = append($1, &ast.NameNamePart{ + Node: ast.Node{ + Position: position.NewTokensPosition($2, $3), + }, + NsSeparatorTkn: $2, + StringTkn: $3, + Value: $3.Value, + }) } ; @@ -355,43 +355,49 @@ top_statement: } | T_NAMESPACE namespace_name ';' { - name := &ast.NameName{ast.Node{}, $2} - $$ = &ast.StmtNamespace{ast.Node{}, name, nil} - - // save position - name.GetNode().Position = position.NewNodeListPosition($2) - $$.GetNode().Position = position.NewTokensPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(name, token.End, $3.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $3), + }, + NsTkn: $1, + Name: &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($2), + }, + Parts: $2, + }, + SemiColonTkn: $3, + } } | T_NAMESPACE namespace_name '{' top_statement_list '}' { - name := &ast.NameName{ast.Node{}, $2} - $$ = &ast.StmtNamespace{ast.Node{}, name, $4} - - // save position - name.GetNode().Position = position.NewNodeListPosition($2) - $$.GetNode().Position = position.NewTokensPosition($1, $5) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(name, token.End, $3.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Stmts, $5.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $5), + }, + NsTkn: $1, + Name: &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($2), + }, + Parts: $2, + }, + OpenCurlyBracket: $3, + Stmts: $4, + CloseCurlyBracket: $5, + } } | T_NAMESPACE '{' top_statement_list '}' { - $$ = &ast.StmtNamespace{ast.Node{}, nil, $3} - - // save position - $$.GetNode().Position = position.NewTokensPosition($1, $4) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $4), + }, + NsTkn: $1, + OpenCurlyBracket: $2, + Stmts: $3, + CloseCurlyBracket: $4, + } } | T_USE use_declarations ';' { @@ -1595,10 +1601,12 @@ interface_list: } | interface_list ',' fully_qualified_class_name { + switch n := lastNode($1).(type) { + case *ast.NameName: n.ListSeparatorTkn = $2 + case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 + case *ast.NameRelative: n.ListSeparatorTkn = $2 + } $$ = append($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) } ; @@ -2413,10 +2421,12 @@ trait_list: } | trait_list ',' fully_qualified_class_name { + switch n := lastNode($1).(type) { + case *ast.NameName: n.ListSeparatorTkn = $2 + case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 + case *ast.NameRelative: n.ListSeparatorTkn = $2 + } $$ = append($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) } ; @@ -3992,37 +4002,45 @@ lexical_var_list: function_call: namespace_name function_call_parameter_list { - name := &ast.NameName{ast.Node{}, $1} + name := &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } $$ = &ast.ExprFunctionCall{ast.Node{}, name, $2.(*ast.ArgumentList)} // save position - name.GetNode().Position = position.NewNodeListPosition($1) $$.GetNode().Position = position.NewNodesPosition(name, $2) } | T_NAMESPACE T_NS_SEPARATOR namespace_name function_call_parameter_list { - funcName := &ast.NameRelative{ast.Node{}, $3} - $$ = &ast.ExprFunctionCall{ast.Node{}, funcName, $4.(*ast.ArgumentList)} + name := &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } + $$ = &ast.ExprFunctionCall{ast.Node{}, name, $4.(*ast.ArgumentList)} // save position - funcName.GetNode().Position = position.NewTokenNodeListPosition($1, $3) - $$.GetNode().Position = position.NewNodesPosition(funcName, $4) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(funcName, token.Namespace, $2.SkippedTokens) + $$.GetNode().Position = position.NewNodesPosition(name, $4) } | T_NS_SEPARATOR namespace_name function_call_parameter_list { - funcName := &ast.NameFullyQualified{ast.Node{}, $2} - $$ = &ast.ExprFunctionCall{ast.Node{}, funcName, $3.(*ast.ArgumentList)} + name := &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } + $$ = &ast.ExprFunctionCall{ast.Node{}, name, $3.(*ast.ArgumentList)} // save position - funcName.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - $$.GetNode().Position = position.NewNodesPosition(funcName, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$.GetNode().Position = position.NewNodesPosition(name, $3) } | class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list { @@ -4093,62 +4111,66 @@ class_name: } | namespace_name { - $$ = &ast.NameName{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeListPosition($1) + $$ = &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } } | T_NAMESPACE T_NS_SEPARATOR namespace_name { - $$ = &ast.NameRelative{ast.Node{}, $3} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) + $$ = &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } } | T_NS_SEPARATOR namespace_name { - $$ = &ast.NameFullyQualified{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } } ; fully_qualified_class_name: namespace_name { - $$ = &ast.NameName{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeListPosition($1) + $$ = &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } } | T_NAMESPACE T_NS_SEPARATOR namespace_name { - $$ = &ast.NameRelative{ast.Node{}, $3} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) + $$ = &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } } | T_NS_SEPARATOR namespace_name { - $$ = &ast.NameFullyQualified{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } } ; @@ -4444,37 +4466,45 @@ static_scalar_value: } | namespace_name { - name := &ast.NameName{ast.Node{}, $1} + name := &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewNodeListPosition($1) $$.GetNode().Position = position.NewNodePosition(name) } | T_NAMESPACE T_NS_SEPARATOR namespace_name { - name := &ast.NameRelative{ast.Node{}, $3} + name := &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewTokenNodeListPosition($1, $3) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) } | T_NS_SEPARATOR namespace_name { - name := &ast.NameFullyQualified{ast.Node{}, $2} + name := &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewTokenNodeListPosition($1, $2) $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) } | T_ARRAY '(' static_array_pair_list ')' { @@ -4888,37 +4918,45 @@ general_constant: } | namespace_name { - name := &ast.NameName{ast.Node{}, $1} + name := &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewNodeListPosition($1) $$.GetNode().Position = position.NewNodePosition(name) } | T_NAMESPACE T_NS_SEPARATOR namespace_name { - name := &ast.NameRelative{ast.Node{}, $3} + name := &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewTokenNodeListPosition($1, $3) $$.GetNode().Position = position.NewNodePosition(name) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating(name, token.Namespace, $2.SkippedTokens) } | T_NS_SEPARATOR namespace_name { - name := &ast.NameFullyQualified{ast.Node{}, $2} + name := &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } $$ = &ast.ExprConstFetch{ast.Node{}, name} // save position - name.GetNode().Position = position.NewTokenNodeListPosition($1, $2) $$.GetNode().Position = position.NewNodePosition(name) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) } ; diff --git a/internal/php5/php5_test.go b/internal/php5/php5_test.go index d802b19..a8b3fcb 100644 --- a/internal/php5/php5_test.go +++ b/internal/php5/php5_test.go @@ -6290,7 +6290,7 @@ func TestPhp5(t *testing.T) { EndPos: 2376, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 109, @@ -6323,7 +6323,7 @@ func TestPhp5(t *testing.T) { EndPos: 2397, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 110, @@ -6349,7 +6349,7 @@ func TestPhp5(t *testing.T) { Position: &position.Position{ StartLine: 110, EndLine: 110, - StartPos: 2391, + StartPos: 2390, EndPos: 2394, }, }, diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index b33e201..4cb66c8 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -9214,7 +9214,7 @@ func TestStmtNamespace(t *testing.T) { EndPos: 17, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, @@ -9272,7 +9272,7 @@ func TestStmtNamespace_Stmts(t *testing.T) { EndPos: 19, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 1, diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 2e0a57b..e818e2c 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 f6589cd..33c558c 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -350,57 +350,59 @@ top_statement_list: namespace_name: T_STRING { - namePart := &ast.NameNamePart{ast.Node{}, $1.Value} - $$ = []ast.Vertex{namePart} - - // save position - namePart.GetNode().Position = position.NewTokenPosition($1) - - // save comments - yylex.(*Parser).setFreeFloating(namePart, token.Start, $1.SkippedTokens) + $$ = []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: position.NewTokenPosition($1), + }, + StringTkn: $1, + Value: $1.Value, + }, + } } | namespace_name T_NS_SEPARATOR T_STRING { - namePart := &ast.NameNamePart{ast.Node{}, $3.Value} - $$ = append($1, namePart) - - // save position - namePart.GetNode().Position = position.NewTokenPosition($3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating(namePart, token.Start, $3.SkippedTokens) + $$ = append($1, &ast.NameNamePart{ + Node: ast.Node{ + Position: position.NewTokensPosition($2, $3), + }, + NsSeparatorTkn: $2, + StringTkn: $3, + Value: $3.Value, + }) } ; name: namespace_name { - $$ = &ast.NameName{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeListPosition($1) + $$ = &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($1), + }, + Parts: $1, + } } - | T_NAMESPACE T_NS_SEPARATOR namespace_name + | T_NAMESPACE T_NS_SEPARATOR namespace_name { - $$ = &ast.NameRelative{ast.Node{}, $3} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) + $$ = &ast.NameRelative{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $3), + }, + NsTkn: $1, + NsSeparatorTkn: $2, + Parts: $3, + } } - | T_NS_SEPARATOR namespace_name + | T_NS_SEPARATOR namespace_name { - $$ = &ast.NameFullyQualified{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.NameFullyQualified{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2), + }, + NsSeparatorTkn: $1, + Parts: $2, + } } ; @@ -443,45 +445,49 @@ top_statement: } | T_NAMESPACE namespace_name ';' { - name := &ast.NameName{ast.Node{}, $2} - $$ = &ast.StmtNamespace{ast.Node{}, name, nil} - - // save position - name.GetNode().Position = position.NewNodeListPosition($2) - $$.GetNode().Position = position.NewTokensPosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).MoveFreeFloating($2[0], name) - yylex.(*Parser).setFreeFloating(name, token.End, $3.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $3), + }, + NsTkn: $1, + Name: &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($2), + }, + Parts: $2, + }, + SemiColonTkn: $3, + } } | T_NAMESPACE namespace_name '{' top_statement_list '}' { - name := &ast.NameName{ast.Node{}, $2} - $$ = &ast.StmtNamespace{ast.Node{}, name, $4} - - // save position - name.GetNode().Position = position.NewNodeListPosition($2) - $$.GetNode().Position = position.NewTokensPosition($1, $5) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).MoveFreeFloating($2[0], name) - yylex.(*Parser).setFreeFloating(name, token.End, $3.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Stmts, $5.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $5), + }, + NsTkn: $1, + Name: &ast.NameName{ + Node: ast.Node{ + Position: position.NewNodeListPosition($2), + }, + Parts: $2, + }, + OpenCurlyBracket: $3, + Stmts: $4, + CloseCurlyBracket: $5, + } } | T_NAMESPACE '{' top_statement_list '}' { - $$ = &ast.StmtNamespace{ast.Node{}, nil, $3} - - // save position - $$.GetNode().Position = position.NewTokensPosition($1, $4) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Namespace, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Stmts, $4.SkippedTokens) + $$ = &ast.StmtNamespace{ + Node: ast.Node{ + Position: position.NewTokensPosition($1, $4), + }, + NsTkn: $1, + OpenCurlyBracket: $2, + Stmts: $3, + CloseCurlyBracket: $4, + } } | T_USE mixed_group_use_declaration ';' { @@ -1222,10 +1228,12 @@ catch_name_list: } | catch_name_list '|' name { + switch n := lastNode($1).(type) { + case *ast.NameName: n.ListSeparatorTkn = $2 + case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 + case *ast.NameRelative: n.ListSeparatorTkn = $2 + } $$ = append($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) } ; @@ -2251,10 +2259,12 @@ name_list: } | name_list ',' name { + switch n := lastNode($1).(type) { + case *ast.NameName: n.ListSeparatorTkn = $2 + case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 + case *ast.NameRelative: n.ListSeparatorTkn = $2 + } $$ = append($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) } ; diff --git a/internal/php7/php7_test.go b/internal/php7/php7_test.go index 00d02cc..d717743 100644 --- a/internal/php7/php7_test.go +++ b/internal/php7/php7_test.go @@ -6962,7 +6962,7 @@ func TestPhp7(t *testing.T) { EndPos: 2525, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 115, @@ -6995,7 +6995,7 @@ func TestPhp7(t *testing.T) { EndPos: 2542, }, }, - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Node: ast.Node{ Position: &position.Position{ StartLine: 116, @@ -17695,7 +17695,7 @@ func TestPhp7(t *testing.T) { Position: &position.Position{ StartLine: 318, EndLine: 318, - StartPos: 5212, + StartPos: 5211, EndPos: 5215, }, }, diff --git a/pkg/ast/node.go b/pkg/ast/node.go index d7dd01d..d64da63 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -603,8 +603,12 @@ func (n *StmtLabel) Accept(v NodeVisitor) { // StmtNamespace node type StmtNamespace struct { Node - NamespaceName Vertex - Stmts []Vertex + NsTkn *token.Token + Name Vertex + OpenCurlyBracket *token.Token + Stmts []Vertex + CloseCurlyBracket *token.Token + SemiColonTkn *token.Token } func (n *StmtNamespace) Accept(v NodeVisitor) { @@ -1840,7 +1844,8 @@ func (n *ExprBinarySpaceship) Accept(v NodeVisitor) { type NameName struct { Node - Parts []Vertex + Parts []Vertex + ListSeparatorTkn *token.Token } func (n *NameName) Accept(v NodeVisitor) { @@ -1849,7 +1854,9 @@ func (n *NameName) Accept(v NodeVisitor) { type NameFullyQualified struct { Node - Parts []Vertex + NsSeparatorTkn *token.Token + Parts []Vertex + ListSeparatorTkn *token.Token } func (n *NameFullyQualified) Accept(v NodeVisitor) { @@ -1858,7 +1865,10 @@ func (n *NameFullyQualified) Accept(v NodeVisitor) { type NameRelative struct { Node - Parts []Vertex + NsTkn *token.Token + NsSeparatorTkn *token.Token + Parts []Vertex + ListSeparatorTkn *token.Token } func (n *NameRelative) Accept(v NodeVisitor) { @@ -1867,7 +1877,9 @@ func (n *NameRelative) Accept(v NodeVisitor) { type NameNamePart struct { Node - Value []byte + NsSeparatorTkn *token.Token + StringTkn *token.Token + Value []byte } func (n *NameNamePart) Accept(v NodeVisitor) { diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index 20eecf3..7e41a44 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -845,10 +845,10 @@ func (t *DFS) Traverse(n ast.Vertex) { if !t.visitor.EnterNode(nn) { return } - if nn.NamespaceName != nil { - t.visitor.Enter("NamespaceName", true) - t.Traverse(nn.NamespaceName) - t.visitor.Leave("NamespaceName", true) + if nn.Name != nil { + t.visitor.Enter("Name", true) + t.Traverse(nn.Name) + t.visitor.Leave("Name", true) } if nn.Stmts != nil { t.visitor.Enter("Stmts", false) diff --git a/pkg/ast/visitor/filter_tokens.go b/pkg/ast/visitor/filter_tokens.go index 7101b86..adba189 100644 --- a/pkg/ast/visitor/filter_tokens.go +++ b/pkg/ast/visitor/filter_tokens.go @@ -33,3 +33,30 @@ func (v *FilterTokens) StmtUseDeclaration(n *ast.StmtUseDeclaration) { n.AsTkn = nil n.CommaTkn = nil } + +func (v *FilterTokens) NameNamePart(n *ast.NameNamePart) { + n.NsSeparatorTkn = nil + n.StringTkn = nil +} + +func (v *FilterTokens) NameName(n *ast.NameName) { + n.ListSeparatorTkn = nil +} + +func (v *FilterTokens) NameFullyQualified(n *ast.NameFullyQualified) { + n.NsSeparatorTkn = nil + n.ListSeparatorTkn = nil +} + +func (v *FilterTokens) NameRelative(n *ast.NameRelative) { + n.NsTkn = nil + n.NsSeparatorTkn = nil + n.ListSeparatorTkn = nil +} + +func (v *FilterTokens) StmtNamespace(n *ast.StmtNamespace) { + n.NsTkn = nil + n.OpenCurlyBracket = nil + n.CloseCurlyBracket = nil + n.SemiColonTkn = nil +} diff --git a/pkg/ast/visitor/namespace_resolver.go b/pkg/ast/visitor/namespace_resolver.go index 34c92c7..fb5659d 100644 --- a/pkg/ast/visitor/namespace_resolver.go +++ b/pkg/ast/visitor/namespace_resolver.go @@ -37,10 +37,10 @@ func (nsr *NamespaceResolver) EnterNode(n ast.Vertex) bool { } func (nsr *NamespaceResolver) StmtNamespace(n *ast.StmtNamespace) { - if n.NamespaceName == nil { + if n.Name == nil { nsr.Namespace = NewNamespace("") } else { - NSParts := n.NamespaceName.(*ast.NameName).Parts + NSParts := n.Name.(*ast.NameName).Parts nsr.Namespace = NewNamespace(concatNameParts(NSParts)) } } diff --git a/pkg/ast/visitor/namespace_resolver_test.go b/pkg/ast/visitor/namespace_resolver_test.go index 34a31f7..5b01ecc 100644 --- a/pkg/ast/visitor/namespace_resolver_test.go +++ b/pkg/ast/visitor/namespace_resolver_test.go @@ -605,7 +605,7 @@ func TestResolveConstantsName(t *testing.T) { stxTree := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: nameAB, + Name: nameAB, }, &ast.StmtConstList{ Consts: []ast.Vertex{ @@ -649,7 +649,7 @@ func TestResolveNamespaces(t *testing.T) { stxTree := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: namespaceAB, + Name: namespaceAB, }, &ast.StmtConstList{ Consts: []ast.Vertex{ @@ -666,7 +666,7 @@ func TestResolveNamespaces(t *testing.T) { Stmts: []ast.Vertex{}, }, &ast.StmtNamespace{ - NamespaceName: namespaceCD, + Name: namespaceCD, Stmts: []ast.Vertex{ &ast.StmtUse{ UseDeclarations: []ast.Vertex{ @@ -749,7 +749,7 @@ func TestDoNotResolveReservedConstants(t *testing.T) { stxTree := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: namespaceName, + Name: namespaceName, }, &ast.StmtExpression{ Expr: &ast.ExprConstFetch{ @@ -877,7 +877,7 @@ func TestDoNotResolveReservedNames(t *testing.T) { stxTree := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Parts: []ast.Vertex{ &ast.NameNamePart{Value: []byte("Foo")}, }, @@ -955,7 +955,7 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) { stxTree := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Parts: []ast.Vertex{ &ast.NameNamePart{Value: []byte("Foo")}, }, @@ -1007,7 +1007,7 @@ func TestResolvePropertyTypeName(t *testing.T) { stmts := &ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Parts: []ast.Vertex{ &ast.NameNamePart{Value: []byte("Foo")}, }, diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index 1aab5d1..fca19fd 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -1931,9 +1931,9 @@ func (p *PrettyPrinter) printStmtNamespace(n ast.Vertex) { io.WriteString(p.w, "namespace") - if nn.NamespaceName != nil { + if nn.Name != nil { io.WriteString(p.w, " ") - p.Print(nn.NamespaceName) + p.Print(nn.Name) } if nn.Stmts != nil { diff --git a/pkg/printer/pretty_printer_test.go b/pkg/printer/pretty_printer_test.go index 199bc2f..c457cb8 100644 --- a/pkg/printer/pretty_printer_test.go +++ b/pkg/printer/pretty_printer_test.go @@ -22,7 +22,7 @@ abstract class Bar extends Baz rootNode := &ast.Root{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: &ast.NameName{ + Name: &ast.NameName{ Parts: []ast.Vertex{ &ast.NameNamePart{Value: []byte("Foo")}, }, @@ -3454,7 +3454,7 @@ func TestPrintNamespace(t *testing.T) { p := printer.NewPrettyPrinter(o, " ") p.Print(&ast.StmtNamespace{ - NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, + Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, }) expected := `namespace Foo;` @@ -3472,7 +3472,7 @@ func TestPrintNamespaceWithStmts(t *testing.T) { p.Print(&ast.StmtStmtList{ Stmts: []ast.Vertex{ &ast.StmtNamespace{ - NamespaceName: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, + Name: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, Stmts: []ast.Vertex{ &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, }, diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 8d73f1b..0e8a0dd 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -55,6 +55,16 @@ func (p *Printer) joinPrint(glue string, nn []ast.Vertex) { } } +func (p *Printer) joinPrintRefactored(glue string, nn []ast.Vertex) { + for k, n := range nn { + if k > 0 { + p.bufStart = glue + } + + p.Print(n) + } +} + func (p *Printer) printNodes(nn []ast.Vertex) { for _, n := range nn { p.Print(n) @@ -558,50 +568,36 @@ func (p *Printer) printNodeArgument(n ast.Vertex) { // name -func (p *Printer) printNameNamePart(n ast.Vertex) { - nn := n.(*ast.NameNamePart) - p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) - p.bufStart = "" - - io.WriteString(p.w, string(nn.Value)) - - p.printFreeFloating(nn, token.End) +func (p *Printer) printNameNamePart(n *ast.NameNamePart) { + p.printToken(n.NsSeparatorTkn, "") + p.printToken(n.StringTkn, string(n.Value)) } -func (p *Printer) printNameName(n ast.Vertex) { - nn := n.(*ast.NameName) - p.printFreeFloating(nn, token.Start) +func (p *Printer) printNameName(n *ast.NameName) { + p.printFreeFloating(n, token.Start) - p.joinPrint("\\", nn.Parts) + p.joinPrintRefactored("\\", n.Parts) - p.printFreeFloating(nn, token.End) + p.printToken(n.ListSeparatorTkn, "") } -func (p *Printer) printNameFullyQualified(n ast.Vertex) { - nn := n.(*ast.NameFullyQualified) - p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) - p.bufStart = "" +func (p *Printer) printNameFullyQualified(n *ast.NameFullyQualified) { + p.printFreeFloating(n, token.Start) + p.printToken(n.NsSeparatorTkn, "\\") - io.WriteString(p.w, "\\") - p.joinPrint("\\", nn.Parts) + p.joinPrintRefactored("\\", n.Parts) - p.printFreeFloating(nn, token.End) + p.printToken(n.ListSeparatorTkn, "") } -func (p *Printer) printNameRelative(n ast.Vertex) { - nn := n.(*ast.NameRelative) - p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart) - p.bufStart = "" +func (p *Printer) printNameRelative(n *ast.NameRelative) { + p.printFreeFloating(n, token.Start) + p.printToken(n.NsTkn, "namespace") + p.printToken(n.NsSeparatorTkn, "\\") - io.WriteString(p.w, "namespace") - p.printFreeFloating(nn, token.Namespace) + p.joinPrintRefactored("\\", n.Parts) - for _, part := range nn.Parts { - io.WriteString(p.w, "\\") - p.Print(part) - } - - p.printFreeFloating(nn, token.End) + p.printToken(n.ListSeparatorTkn, "") } // scalar @@ -2299,7 +2295,9 @@ func (p *Printer) printStmtCatch(n ast.Vertex) { io.WriteString(p.w, "catch") p.printFreeFloating(nn, token.Catch) io.WriteString(p.w, "(") - p.joinPrint("|", nn.Types) + + p.joinPrintRefactored("|", nn.Types) + p.Print(nn.Var) p.printFreeFloating(nn, token.Var) io.WriteString(p.w, ")") @@ -2409,10 +2407,9 @@ func (p *Printer) printStmtClass(n ast.Vertex) { io.WriteString(p.w, " ") } io.WriteString(p.w, "implements") - if nn.Implements.InterfaceNames[0].GetNode().Tokens.IsEmpty() { - io.WriteString(p.w, " ") - } - p.joinPrint(",", nn.Implements.InterfaceNames) + p.bufStart = " " + p.joinPrintRefactored(",", nn.Implements.InterfaceNames) + } p.printFreeFloating(nn, token.Name) @@ -2896,10 +2893,8 @@ func (p *Printer) printStmtInterface(n ast.Vertex) { io.WriteString(p.w, " ") } io.WriteString(p.w, "extends") - if nn.Extends.InterfaceNames[0].GetNode().Tokens.IsEmpty() { - io.WriteString(p.w, " ") - } - p.joinPrint(",", nn.Extends.InterfaceNames) + p.bufStart = " " + p.joinPrintRefactored(",", nn.Extends.InterfaceNames) } p.printFreeFloating(nn, token.Name) @@ -2923,32 +2918,28 @@ func (p *Printer) printStmtLabel(n ast.Vertex) { p.printFreeFloating(nn, token.End) } -func (p *Printer) printStmtNamespace(n ast.Vertex) { - nn := n.(*ast.StmtNamespace) - p.printFreeFloating(nn, token.Start) - io.WriteString(p.w, "namespace") +func (p *Printer) printStmtNamespace(n *ast.StmtNamespace) { + p.printToken(n.NsTkn, "namespace") - if nn.NamespaceName != nil { - if nn.NamespaceName.GetNode().Tokens.IsEmpty() { - io.WriteString(p.w, " ") - } - p.Print(nn.NamespaceName) + if n.Name != nil { + p.bufStart = " " + p.Print(n.Name) } - if nn.Stmts != nil { - p.printFreeFloating(nn, token.Namespace) - io.WriteString(p.w, "{") - p.printNodes(nn.Stmts) - p.printFreeFloating(nn, token.Stmts) - io.WriteString(p.w, "}") - } else { - p.printFreeFloating(nn, token.SemiColon) - if nn.GetNode().Tokens.IsEmpty() { - io.WriteString(p.w, ";") - } + if n.Stmts != nil { + p.printToken(n.OpenCurlyBracket, "{") + p.printNodes(n.Stmts) + p.printToken(n.CloseCurlyBracket, "}") + return } - p.printFreeFloating(nn, token.End) + if n.OpenCurlyBracket != nil { + p.printToken(n.OpenCurlyBracket, "{") + p.printToken(n.CloseCurlyBracket, "}") + return + } + + p.printToken(n.SemiColonTkn, ";") } func (p *Printer) printStmtNop(n ast.Vertex) { @@ -3202,10 +3193,8 @@ func (p *Printer) printStmtTraitUse(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "use") - if nn.Traits[0].GetNode().Tokens.IsEmpty() { - io.WriteString(p.w, " ") - } - p.joinPrint(",", nn.Traits) + p.bufStart = " " + p.joinPrintRefactored(",", nn.Traits) p.Print(nn.TraitAdaptationList) @@ -3276,11 +3265,13 @@ func (p *Printer) printStmtUnset(n ast.Vertex) { func (p *Printer) printStmtUse(n *ast.StmtUse) { p.printToken(n.UseTkn, "use") - p.bufStart = " " - p.Print(n.Type) + if n.Type != nil { + p.bufStart = " " + p.Print(n.Type) + } p.bufStart = " " - p.joinPrint(",", n.UseDeclarations) + p.joinPrintRefactored(",", n.UseDeclarations) p.printToken(n.SemiColonTkn, ";") } @@ -3298,16 +3289,7 @@ func (p *Printer) printStmtGroupUse(n *ast.StmtGroupUse) { p.printToken(n.NsSeparatorTkn, "\\") p.printToken(n.OpenCurlyBracketTkn, "{") - for k, v := range n.UseDeclarations { - p.Print(v) - var def string - if k != len(n.UseDeclarations)-1 { - def = "," - } - if decl, ok := v.(*ast.StmtUseDeclaration); ok { - p.printToken(decl.CommaTkn, def) - } - } + p.joinPrintRefactored(",", n.UseDeclarations) p.printToken(n.CloseCurlyBracketTkn, "}") p.printToken(n.SemiColonTkn, ";") @@ -3325,6 +3307,7 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) { p.Print(n.Use) if n.Alias == nil { + p.printToken(n.CommaTkn, "") return } @@ -3333,6 +3316,8 @@ func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) { p.bufStart = " " p.Print(n.Alias) + + p.printToken(n.CommaTkn, "") } func (p *Printer) printStmtWhile(n ast.Vertex) { diff --git a/pkg/printer/printer_parsed_php5_test.go b/pkg/printer/printer_parsed_php5_test.go index 60cdc39..17cef65 100644 --- a/pkg/printer/printer_parsed_php5_test.go +++ b/pkg/printer/printer_parsed_php5_test.go @@ -45,8 +45,8 @@ func TestParseAndPrintPhp5Root(t *testing.T) { } func TestParseAndPrintPhp5Identifier(t *testing.T) { - - src := `