[refactoring] update ast structure of "name" nodes

This commit is contained in:
Vadym Slizov 2020-08-24 14:20:20 +03:00
parent 767187ff85
commit c1b3e6f5b2
19 changed files with 446 additions and 356 deletions

View File

@ -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,

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -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)
}
;

View File

@ -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,
},
},

View File

@ -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,

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -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
{
$$ = &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,
}
}
;
@ -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)
}
;

View File

@ -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,
},
},

View File

@ -603,8 +603,12 @@ func (n *StmtLabel) Accept(v NodeVisitor) {
// StmtNamespace node
type StmtNamespace struct {
Node
NamespaceName Vertex
NsTkn *token.Token
Name Vertex
OpenCurlyBracket *token.Token
Stmts []Vertex
CloseCurlyBracket *token.Token
SemiColonTkn *token.Token
}
func (n *StmtNamespace) Accept(v NodeVisitor) {
@ -1841,6 +1845,7 @@ func (n *ExprBinarySpaceship) Accept(v NodeVisitor) {
type NameName struct {
Node
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
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
NsTkn *token.Token
NsSeparatorTkn *token.Token
Parts []Vertex
ListSeparatorTkn *token.Token
}
func (n *NameRelative) Accept(v NodeVisitor) {
@ -1867,6 +1877,8 @@ func (n *NameRelative) Accept(v NodeVisitor) {
type NameNamePart struct {
Node
NsSeparatorTkn *token.Token
StringTkn *token.Token
Value []byte
}

View File

@ -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)

View File

@ -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
}

View File

@ -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))
}
}

View File

@ -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")},
},

View File

@ -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 {

View File

@ -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")}}},
},

View File

@ -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")
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) {

View File

@ -45,8 +45,8 @@ func TestParseAndPrintPhp5Root(t *testing.T) {
}
func TestParseAndPrintPhp5Identifier(t *testing.T) {
src := `<?
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
`
@ -73,8 +73,8 @@ func TestParseAndPrintPhp5Parameter(t *testing.T) {
}
func TestParseAndPrintPhp5Argument(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( $a , $b
, $c
) ; `
@ -89,7 +89,8 @@ func TestParseAndPrintPhp5Argument(t *testing.T) {
// test name
func TestParseAndPrintPhp5Names(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@ -168,7 +169,8 @@ func TestParseAndPrintPhp5String(t *testing.T) {
}
func TestParseAndPrintPhp5Heredoc(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo(<<<EAP
test
EAP
@ -289,7 +291,8 @@ func TestParseAndPrintPhp5Cast(t *testing.T) {
// test expr
func TestParseAndPrintPhp5ArrayDimFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@ -415,7 +418,8 @@ func TestParseAndPrintPhp5Closure(t *testing.T) {
}
func TestParseAndPrintPhp5ConstFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
null ;
`
@ -481,7 +485,8 @@ func TestParseAndPrintPhp5Exit(t *testing.T) {
}
func TestParseAndPrintPhp5FunctionCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@ -551,7 +556,8 @@ func TestParseAndPrintPhp5MethodCall(t *testing.T) {
}
func TestParseAndPrintPhp5New(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
new Foo ;
@ -664,7 +670,8 @@ func TestParseAndPrintPhp5ShortArray(t *testing.T) {
}
func TestParseAndPrintPhp5StaticCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: bar ( $a , $b ) ;`
actual := printPhp5(parsePhp5(src))
@ -675,7 +682,8 @@ func TestParseAndPrintPhp5StaticCall(t *testing.T) {
}
func TestParseAndPrintPhp5StaticPropertyFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: $bar ;`
actual := printPhp5(parsePhp5(src))
@ -1117,7 +1125,8 @@ func TestParseAndPrintPhp5GotoLabel(t *testing.T) {
}
func TestParseAndPrintPhp5Namespace(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo \ Bar ;
namespace Baz {

View File

@ -13,7 +13,8 @@ import (
)
func ExamplePrinter() {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo;
@ -37,7 +38,7 @@ abstract class Bar extends Baz
// change namespace
parts := &rootNode.(*ast.Root).Stmts[0].(*ast.StmtNamespace).NamespaceName.(*ast.NameName).Parts
parts := &rootNode.(*ast.Root).Stmts[1].(*ast.StmtNamespace).Name.(*ast.NameName).Parts
*parts = append(*parts, &ast.NameNamePart{Value: []byte("Quuz")})
// print
@ -46,7 +47,7 @@ abstract class Bar extends Baz
p.Print(rootNode)
// Output:
//<?php
//<?php ;
//
// namespace Foo\Quuz;
//
@ -94,8 +95,8 @@ func TestParseAndPrintRoot(t *testing.T) {
}
func TestParseAndPrintIdentifier(t *testing.T) {
src := `<?
// TODO: remove ; after <?php
src := `<? ;
/* Foo */
Foo ( ) ;
`
@ -151,8 +152,8 @@ func TestParseAndPrintNullable(t *testing.T) {
}
func TestParseAndPrintArgument(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( $a , $b
, ... $c ,
) ; `
@ -167,7 +168,8 @@ func TestParseAndPrintArgument(t *testing.T) {
// test name
func TestParseAndPrintNames(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
\ foo ( ) ;
namespace \ foo ( ) ;
@ -246,7 +248,8 @@ func TestParseAndPrintString(t *testing.T) {
}
func TestParseAndPrintHeredoc(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo(<<<EAP
test
EAP
@ -370,7 +373,8 @@ func TestParseAndPrintCast(t *testing.T) {
// test expr
func TestParseAndPrintArrayDimFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
FOO [ ] ;
FOO [ 1 ] ;
$a [ ] ;
@ -509,7 +513,8 @@ func TestParseAndPrintArrowFunction(t *testing.T) {
}
func TestParseAndPrintConstFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
null ;
`
@ -575,7 +580,8 @@ func TestParseAndPrintExit(t *testing.T) {
}
func TestParseAndPrintFunctionCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
foo ( ) ;
$var ( $a , ... $b , $c ) ;
`
@ -776,7 +782,8 @@ func TestParseAndPrintShortList(t *testing.T) {
}
func TestParseAndPrintStaticCall(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: bar ( $a , $b ) ;`
actual := print(parse(src))
@ -787,7 +794,8 @@ func TestParseAndPrintStaticCall(t *testing.T) {
}
func TestParseAndPrintStaticPropertyFetch(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
Foo :: $bar ;`
actual := print(parse(src))
@ -1250,7 +1258,8 @@ func TestParseAndPrintGotoLabel(t *testing.T) {
}
func TestParseAndPrintNamespace(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
namespace Foo \ Bar ;
namespace Baz {

View File

@ -17,7 +17,7 @@ func TestPrinterPrintFile(t *testing.T) {
p.Print(&ast.Root{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
NamespaceName: &ast.NameName{
Name: &ast.NameName{
Parts: []ast.Vertex{
&ast.NameNamePart{Value: []byte("Foo")},
},
@ -3862,7 +3862,7 @@ func TestPrinterPrintNamespace(t *testing.T) {
p := printer.NewPrinter(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;`
@ -3878,7 +3878,7 @@ func TestPrinterPrintNamespaceWithStmts(t *testing.T) {
p := printer.NewPrinter(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")}}},
Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},