[refactoring] update ast structure for "Use" and "GroupUse" nodes

This commit is contained in:
Vadym Slizov 2020-08-22 16:59:26 +03:00
parent 97747c5ac0
commit 767187ff85
23 changed files with 4134 additions and 5001 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -395,52 +395,48 @@ top_statement:
}
| T_USE use_declarations ';'
{
useList := &ast.StmtUseList{ast.Node{}, $2}
$$ = &ast.StmtUse{ast.Node{}, useList}
// save position
useList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.SkippedTokens)
$$ = &ast.StmtUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
UseTkn: $1,
UseDeclarations: $2,
SemiColonTkn: $3,
}
}
| T_USE T_FUNCTION use_function_declarations ';'
{
identifier := &ast.Identifier{ast.Node{}, $2.Value}
useList := &ast.StmtUseList{ast.Node{}, $3}
useType := &ast.StmtUseType{ast.Node{}, identifier, useList}
$$ = &ast.StmtUse{ast.Node{}, useType}
// save position
identifier.GetNode().Position = position.NewTokenPosition($2)
useList.GetNode().Position = position.NewNodeListPosition($3)
useType.GetNode().Position = position.NewTokenNodePosition($2, useList)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating(identifier, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $4.SkippedTokens)
$$ = &ast.StmtUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
UseTkn: $1,
Type: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
Value: $2.Value,
},
UseDeclarations: $3,
SemiColonTkn: $4,
}
}
| T_USE T_CONST use_const_declarations ';'
{
identifier := &ast.Identifier{ast.Node{}, $2.Value}
useList := &ast.StmtUseList{ast.Node{}, $3}
useType := &ast.StmtUseType{ast.Node{}, identifier, useList}
$$ = &ast.StmtUse{ast.Node{}, useType}
// save position
identifier.GetNode().Position = position.NewTokenPosition($2)
useList.GetNode().Position = position.NewNodeListPosition($3)
useType.GetNode().Position = position.NewTokenNodePosition($2, useList)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating(identifier, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $4.SkippedTokens)
$$ = &ast.StmtUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
UseTkn: $1,
Type: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($2),
},
Value: $2.Value,
},
UseDeclarations: $3,
SemiColonTkn: $4,
}
}
| constant_declaration ';'
{
@ -458,10 +454,9 @@ top_statement:
use_declarations:
use_declarations ',' use_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| use_declaration
{
@ -472,73 +467,84 @@ use_declarations:
use_declaration:
namespace_name
{
name := &ast.NameName{ast.Node{}, $1}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
$$.GetNode().Position = position.NewNodeListPosition($1)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
}
}
| namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $3),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
AsTkn: $2,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
Value: $3.Value,
},
}
}
| T_NS_SEPARATOR namespace_name
{
name := &ast.NameName{ast.Node{}, $2}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $2),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
}
}
| T_NS_SEPARATOR namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4)
asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
AsTkn: $3,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($4),
},
Value: $4.Value,
},
}
}
;
use_function_declarations:
use_function_declarations ',' use_function_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| use_function_declaration
{
@ -549,73 +555,84 @@ use_function_declarations:
use_function_declaration:
namespace_name
{
name := &ast.NameName{ast.Node{}, $1}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
$$.GetNode().Position = position.NewNodeListPosition($1)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
}
}
| namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $3),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
AsTkn: $2,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
Value: $3.Value,
},
}
}
| T_NS_SEPARATOR namespace_name
{
name := &ast.NameName{ast.Node{}, $2}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $2),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
}
}
| T_NS_SEPARATOR namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4)
asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
AsTkn: $3,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($4),
},
Value: $4.Value,
},
}
}
;
use_const_declarations:
use_const_declarations ',' use_const_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| use_const_declaration
{
@ -626,63 +643,75 @@ use_const_declarations:
use_const_declaration:
namespace_name
{
name := &ast.NameName{ast.Node{}, $1}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
$$.GetNode().Position = position.NewNodeListPosition($1)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
}
}
| namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $3),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
AsTkn: $2,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
Value: $3.Value,
},
}
}
| T_NS_SEPARATOR namespace_name
{
name := &ast.NameName{ast.Node{}, $2}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $2),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
}
}
| T_NS_SEPARATOR namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4)
asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
NsSeparatorTkn: $1,
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
AsTkn: $3,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($4),
},
Value: $4.Value,
},
}
}
;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -485,55 +485,47 @@ top_statement:
}
| T_USE mixed_group_use_declaration ';'
{
$$ = &ast.StmtUse{ast.Node{}, $2}
use := $2.(*ast.StmtGroupUse)
// save position
$$.GetNode().Position = position.NewTokensPosition($1, $3)
use.Node.Position = position.NewTokensPosition($1, $3)
use.UseTkn = $1
use.SemiColonTkn = $3
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.SkippedTokens)
$$ = $2
}
| T_USE use_type group_use_declaration ';'
{
useType := &ast.StmtUseType{ast.Node{}, $2, $3}
$$ = &ast.StmtUse{ast.Node{}, useType}
use := $3.(*ast.StmtGroupUse)
// save position
useType.GetNode().Position = position.NewNodesPosition($2, $3)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
use.Node.Position = position.NewTokensPosition($1, $4)
use.UseTkn = $1
use.Type = $2
use.SemiColonTkn = $4
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $4.SkippedTokens)
$$ = $3
}
| T_USE use_declarations ';'
{
useList := &ast.StmtUseList{ast.Node{}, $2}
$$ = &ast.StmtUse{ast.Node{}, useList}
// save position
useList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.SkippedTokens)
$$ = &ast.StmtUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
UseTkn: $1,
UseDeclarations: $2,
SemiColonTkn: $3,
}
}
| T_USE use_type use_declarations ';'
{
useList := &ast.StmtUseList{ast.Node{}, $3}
useType := &ast.StmtUseType{ast.Node{}, $2, useList}
$$ = &ast.StmtUse{ast.Node{}, useType}
// save position
useList.GetNode().Position = position.NewNodeListPosition($3)
useType.GetNode().Position = position.NewNodesPosition($2, useList)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens($$, token.End, $4.SkippedTokens)
$$ = &ast.StmtUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
UseTkn: $1,
Type: $2,
UseDeclarations: $3,
SemiColonTkn: $4,
}
}
| T_CONST const_list ';'
{
@ -575,104 +567,90 @@ use_type:
group_use_declaration:
namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
{
name := &ast.NameName{ast.Node{}, $1}
useList := &ast.StmtUseList{ast.Node{}, $4}
useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useListNsSeparator}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
useList.GetNode().Position = position.NewNodeListPosition($4)
useListBrackets.GetNode().Position = position.NewTokensPosition($3, $6)
useListNsSeparator.GetNode().Position = position.NewTokensPosition($2, $6)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $6)
// save comments
if $5 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $5.SkippedTokens)
if len($4) > 0 {
$4[len($4)-1].(*ast.StmtUseDeclaration).CommaTkn = $5
}
$$ = &ast.StmtGroupUse{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $6),
},
Prefix: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
NsSeparatorTkn: $2,
OpenCurlyBracketTkn: $3,
UseDeclarations: $4,
CloseCurlyBracketTkn: $6,
}
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $2.SkippedTokens)
}
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
{
name := &ast.NameName{ast.Node{}, $2}
prefixNsSeparator := &ast.ParserNsSeparator{ast.Node{}, name}
useList := &ast.StmtUseList{ast.Node{}, $5}
useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, prefixNsSeparator, useListNsSeparator}
$5[len($5)-1].(*ast.StmtUseDeclaration).CommaTkn = $6
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
prefixNsSeparator.GetNode().Position = position.NewTokenNodePosition($1, name)
useList.GetNode().Position = position.NewNodeListPosition($5)
useListBrackets.GetNode().Position = position.NewTokensPosition($4, $7)
useListNsSeparator.GetNode().Position = position.NewTokensPosition($3, $7)
$$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments
yylex.(*Parser).setFreeFloating(prefixNsSeparator, token.Start, $1.SkippedTokens)
if $6 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $6.SkippedTokens)
$$ = &ast.StmtGroupUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $7),
},
LeadingNsSeparatorTkn: $1,
Prefix: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
NsSeparatorTkn: $3,
OpenCurlyBracketTkn: $4,
UseDeclarations: $5,
CloseCurlyBracketTkn: $7,
}
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $4.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $3.SkippedTokens)
}
;
mixed_group_use_declaration:
namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
{
name := &ast.NameName{ast.Node{}, $1}
useList := &ast.StmtUseList{ast.Node{}, $4}
useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useListNsSeparator}
$4[len($4)-1].(*ast.StmtUseDeclaration).CommaTkn = $5
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
useList.GetNode().Position = position.NewNodeListPosition($4)
useListBrackets.GetNode().Position = position.NewTokensPosition($3, $6)
useListNsSeparator.GetNode().Position = position.NewTokensPosition($2, $6)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $6)
// save comments
if $5 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $5.SkippedTokens)
$$ = &ast.StmtGroupUse{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $6),
},
Prefix: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
NsSeparatorTkn: $2,
OpenCurlyBracketTkn: $3,
UseDeclarations: $4,
CloseCurlyBracketTkn: $6,
}
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $6.SkippedTokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $2.SkippedTokens)
}
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
{
name := &ast.NameName{ast.Node{}, $2}
prefixNsSeparator := &ast.ParserNsSeparator{ast.Node{}, name}
useList := &ast.StmtUseList{ast.Node{}, $5}
useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, prefixNsSeparator, useListNsSeparator}
$5[len($5)-1].(*ast.StmtUseDeclaration).CommaTkn = $6
// save position
name.GetNode().Position = position.NewNodeListPosition($2)
prefixNsSeparator.GetNode().Position = position.NewTokenNodePosition($1, name)
useList.GetNode().Position = position.NewNodeListPosition($5)
useListBrackets.GetNode().Position = position.NewTokensPosition($4, $7)
useListNsSeparator.GetNode().Position = position.NewTokensPosition($3, $7)
$$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments
yylex.(*Parser).setFreeFloating(prefixNsSeparator, token.Start, $1.SkippedTokens)
if $6 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $6.SkippedTokens)
$$ = &ast.StmtGroupUse{
Node: ast.Node{
Position: position.NewTokensPosition($1, $7),
},
LeadingNsSeparatorTkn: $1,
Prefix: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($2),
},
Parts: $2,
},
NsSeparatorTkn: $3,
OpenCurlyBracketTkn: $4,
UseDeclarations: $5,
CloseCurlyBracketTkn: $7,
}
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $4.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $7.SkippedTokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $3.SkippedTokens)
}
;
@ -690,10 +668,9 @@ possible_comma:
inline_use_declarations:
inline_use_declarations ',' inline_use_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| inline_use_declaration
{
@ -704,10 +681,9 @@ inline_use_declarations:
unprefixed_use_declarations:
unprefixed_use_declarations ',' unprefixed_use_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| unprefixed_use_declaration
{
@ -718,10 +694,9 @@ unprefixed_use_declarations:
use_declarations:
use_declarations ',' use_declaration
{
$$ = append($1, $3)
$1[len($1)-1].(*ast.StmtUseDeclaration).CommaTkn = $2
// save comments
yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens)
$$ = append($1, $3)
}
| use_declaration
{
@ -736,39 +711,49 @@ inline_use_declaration:
}
| use_type unprefixed_use_declaration
{
$$ = &ast.StmtUseType{ast.Node{}, $1, $2}
decl := $2.(*ast.StmtUseDeclaration)
decl.Type = $1
decl.Node.Position = position.NewNodesPosition($1, $2)
// save position
$$.GetNode().Position = position.NewNodesPosition($1, $2)
$$ = $2
}
;
unprefixed_use_declaration:
namespace_name
{
name := &ast.NameName{ast.Node{}, $1}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
$$.GetNode().Position = position.NewNodePosition(name)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
}
}
| namespace_name T_AS T_STRING
{
name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value}
asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position
name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.SkippedTokens)
$$ = &ast.StmtUseDeclaration{
Node: ast.Node{
Position: position.NewNodeListTokenPosition($1, $3),
},
Use: &ast.NameName{
Node: ast.Node{
Position: position.NewNodeListPosition($1),
},
Parts: $1,
},
AsTkn: $2,
Alias: &ast.Identifier{
Node: ast.Node{
Position: position.NewTokenPosition($3),
},
Value: $3.Value,
},
}
}
;
@ -779,14 +764,11 @@ use_declaration:
}
| T_NS_SEPARATOR unprefixed_use_declaration
{
$$ = &ast.ParserNsSeparator{ast.Node{}, $2}
decl := $2.(*ast.StmtUseDeclaration)
decl.NsSeparatorTkn = $1
decl.Node.Position = position.NewTokenNodePosition($1, $2)
// save position
$2.GetNode().Position = position.NewTokenNodePosition($1, $2)
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
$$ = $2
}
;

File diff suppressed because it is too large Load Diff

View File

@ -84,10 +84,8 @@ type NodeVisitor interface {
StmtTry(n *StmtTry)
StmtUnset(n *StmtUnset)
StmtUse(n *StmtUse)
StmtGroupUseList(n *StmtGroupUseList)
StmtUseList(n *StmtUseList)
StmtGroupUse(n *StmtGroupUse)
StmtUseDeclaration(n *StmtUseDeclaration)
StmtUseType(n *StmtUseType)
StmtWhile(n *StmtWhile)
ExprArray(n *ExprArray)

View File

@ -796,56 +796,49 @@ func (n *StmtUnset) Accept(v NodeVisitor) {
// StmtUse node
type StmtUse struct {
Node
UseList Vertex
UseTkn *token.Token
Type Vertex
UseDeclarations []Vertex
SemiColonTkn *token.Token
}
func (n *StmtUse) Accept(v NodeVisitor) {
v.StmtUse(n)
}
// StmtGroupUseList node
type StmtGroupUseList struct {
// StmtGroupUse node
type StmtGroupUse struct {
Node
Prefix Vertex
UseList Vertex
UseTkn *token.Token
Type Vertex
LeadingNsSeparatorTkn *token.Token
Prefix Vertex
NsSeparatorTkn *token.Token
OpenCurlyBracketTkn *token.Token
UseDeclarations []Vertex
CloseCurlyBracketTkn *token.Token
SemiColonTkn *token.Token
}
func (n *StmtGroupUseList) Accept(v NodeVisitor) {
v.StmtGroupUseList(n)
}
// StmtUseList node
type StmtUseList struct {
Node
UseDeclarations []Vertex
}
func (n *StmtUseList) Accept(v NodeVisitor) {
v.StmtUseList(n)
func (n *StmtGroupUse) Accept(v NodeVisitor) {
v.StmtGroupUse(n)
}
// StmtUseDeclaration node
type StmtUseDeclaration struct {
Node
Use Vertex
Alias Vertex
Type Vertex
NsSeparatorTkn *token.Token
Use Vertex
AsTkn *token.Token
Alias Vertex
CommaTkn *token.Token
}
func (n *StmtUseDeclaration) Accept(v NodeVisitor) {
v.StmtUseDeclaration(n)
}
// StmtUseType node
type StmtUseType struct {
Node
Type Vertex
Use Vertex
}
func (n *StmtUseType) Accept(v NodeVisitor) {
v.StmtUseType(n)
}
// StmtWhile node
type StmtWhile struct {
Node

View File

@ -1150,35 +1150,35 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseList != nil {
t.visitor.Enter("UseList", true)
t.Traverse(nn.UseList)
t.visitor.Leave("UseList", true)
if nn.Type != nil {
t.visitor.Enter("Type", true)
t.Traverse(nn.Type)
t.visitor.Leave("Type", true)
}
case *ast.StmtGroupUseList:
if nn.UseDeclarations != nil {
t.visitor.Enter("UseDeclarations", false)
for _, c := range nn.UseDeclarations {
t.Traverse(c)
}
t.visitor.Leave("UseDeclarations", false)
}
case *ast.StmtGroupUse:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Type != nil {
t.visitor.Enter("Type", true)
t.Traverse(nn.Type)
t.visitor.Leave("Type", true)
}
if nn.Prefix != nil {
t.visitor.Enter("Prefix", true)
t.Traverse(nn.Prefix)
t.visitor.Leave("Prefix", true)
}
if nn.UseList != nil {
t.visitor.Enter("UseList", true)
t.Traverse(nn.UseList)
t.visitor.Leave("UseList", true)
}
case *ast.StmtUseList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.UseDeclarations != nil {
t.visitor.Enter("UseDeclarations", false)
for _, c := range nn.UseDeclarations {
@ -1187,23 +1187,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("UseDeclarations", false)
}
case *ast.StmtUseDeclaration:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Use != nil {
t.visitor.Enter("Use", true)
t.Traverse(nn.Use)
t.visitor.Leave("Use", true)
}
if nn.Alias != nil {
t.visitor.Enter("Alias", true)
t.Traverse(nn.Alias)
t.visitor.Leave("Alias", true)
}
case *ast.StmtUseType:
if nn == nil {
return
}
@ -1220,6 +1203,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Use)
t.visitor.Leave("Use", true)
}
if nn.Alias != nil {
t.visitor.Enter("Alias", true)
t.Traverse(nn.Alias)
t.visitor.Leave("Alias", true)
}
case *ast.StmtWhile:
if nn == nil {
return

View File

@ -169,6 +169,28 @@ func (v *Dump) printNode(n *ast.Node) {
v.print("},\n")
}
func (v *Dump) printToken(key string, t *token.Token) {
if t == nil {
return
}
v.printIndent(v.indent)
v.print(key)
v.print(": &token.Token{\n")
v.printIndent(v.indent + 1)
v.print("ID: token." + t.ID.String() + ",\n")
v.printIndent(v.indent + 1)
v.print("Value: []byte(" + strconv.Quote(string(t.Value)) + "),\n")
v.printIndent(v.indent + 1)
v.print("Skipped: []byte(" + strconv.Quote(string(t.Skipped)) + "),\n")
v.printIndent(v.indent)
v.print("},\n")
}
func (v *Dump) Root(n *ast.Root) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Root{\n")
@ -588,30 +610,30 @@ func (v *Dump) StmtUse(n *ast.StmtUse) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUse{\n")
v.printNode(n.GetNode())
v.printToken("UseTkn", n.UseTkn)
v.printToken("SemiColonTkn", n.SemiColonTkn)
}
func (v *Dump) StmtGroupUseList(n *ast.StmtGroupUseList) {
func (v *Dump) StmtGroupUse(n *ast.StmtGroupUse) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtGroupUseList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseList(n *ast.StmtUseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseList{\n")
v.print("&ast.StmtGroupUse{\n")
v.printNode(n.GetNode())
v.printToken("UseTkn", n.UseTkn)
v.printToken("LeadingNsSeparatorTkn", n.LeadingNsSeparatorTkn)
v.printToken("NsSeparatorTkn", n.NsSeparatorTkn)
v.printToken("OpenCurlyBracketTkn", n.OpenCurlyBracketTkn)
v.printToken("CloseCurlyBracketTkn", n.CloseCurlyBracketTkn)
v.printToken("SemiColonTkn", n.SemiColonTkn)
}
func (v *Dump) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseDeclaration{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseType(n *ast.StmtUseType) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseType{\n")
v.printNode(n.GetNode())
v.printToken("NsSeparatorTkn", n.NsSeparatorTkn)
v.printToken("AsTkn", n.AsTkn)
v.printToken("CommaTkn", n.CommaTkn)
}
func (v *Dump) StmtWhile(n *ast.StmtWhile) {

View File

@ -13,34 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true
}
func (v *FilterParserNodes) StmtGroupUseList(n *ast.StmtGroupUseList) {
if nn, ok := n.Prefix.(*ast.ParserNsSeparator); ok {
n.Prefix = nn.Child
}
if nn, ok := n.UseList.(*ast.ParserNsSeparator); ok {
n.UseList = nn.Child
}
if nn, ok := n.UseList.(*ast.ParserBrackets); ok {
n.UseList = nn.Child
}
}
func (v *FilterParserNodes) StmtUseList(n *ast.StmtUseList) {
for k, v := range n.UseDeclarations {
if nn, ok := v.(*ast.ParserNsSeparator); ok {
n.UseDeclarations[k] = nn.Child
}
}
}
func (v *FilterParserNodes) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
if nn, ok := n.Alias.(*ast.ParserAs); ok {
n.Alias = nn.Child
}
}
func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {

View File

@ -10,5 +10,26 @@ type FilterTokens struct {
func (v *FilterTokens) EnterNode(n ast.Vertex) bool {
n.GetNode().Tokens = nil
n.Accept(v)
return true
}
func (v *FilterTokens) StmtUse(n *ast.StmtUse) {
n.UseTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtGroupUse(n *ast.StmtGroupUse) {
n.UseTkn = nil
n.LeadingNsSeparatorTkn = nil
n.NsSeparatorTkn = nil
n.OpenCurlyBracketTkn = nil
n.CloseCurlyBracketTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
n.NsSeparatorTkn = nil
n.AsTkn = nil
n.CommaTkn = nil
}

View File

@ -13,9 +13,7 @@ type NamespaceResolver struct {
Namespace *Namespace
ResolvedNames map[ast.Vertex]string
goDeep bool
useType string
usePrefix []ast.Vertex
goDeep bool
}
// NewNamespaceResolver NamespaceResolver type constructor
@ -47,28 +45,28 @@ func (nsr *NamespaceResolver) StmtNamespace(n *ast.StmtNamespace) {
}
}
func (nsr *NamespaceResolver) StmtUseType(n *ast.StmtUseType) {
func (nsr *NamespaceResolver) StmtUse(n *ast.StmtUse) {
useType := ""
if n.Type != nil {
nsr.useType = string(n.Type.(*ast.Identifier).Value)
useType = string(n.Type.(*ast.Identifier).Value)
}
for _, nn := range n.UseDeclarations {
nsr.AddAlias(useType, nn, nil)
}
nsr.goDeep = false
}
func (nsr *NamespaceResolver) StmtGroupUseList(n *ast.StmtGroupUseList) {
if n.Prefix != nil {
nsr.usePrefix = n.Prefix.(*ast.NameName).Parts
}
}
func (nsr *NamespaceResolver) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
useNameParts := n.Use.(*ast.NameName).Parts
var alias string
if n.Alias == nil {
alias = string(useNameParts[len(useNameParts)-1].(*ast.NameNamePart).Value)
} else {
alias = string(n.Alias.(*ast.Identifier).Value)
func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUse) {
useType := ""
if n.Type != nil {
useType = string(n.Type.(*ast.Identifier).Value)
}
nsr.Namespace.AddAlias(nsr.useType, concatNameParts(nsr.usePrefix, useNameParts), alias)
for _, nn := range n.UseDeclarations {
nsr.AddAlias(useType, nn, n.Prefix.(*ast.NameName).Parts)
}
nsr.goDeep = false
}
@ -215,10 +213,26 @@ func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) {
if nn.Stmts != nil {
nsr.Namespace = NewNamespace("")
}
case *ast.StmtUseType:
nsr.useType = ""
case *ast.StmtGroupUseList:
nsr.usePrefix = nil
}
}
// AddAlias adds a new alias
func (nsr *NamespaceResolver) AddAlias(useType string, nn ast.Vertex, prefix []ast.Vertex) {
switch use := nn.(type) {
case *ast.StmtUseDeclaration:
if use.Type != nil {
useType = string(use.Type.(*ast.Identifier).Value)
}
useNameParts := use.Use.(*ast.NameName).Parts
var alias string
if use.Alias == nil {
alias = string(useNameParts[len(useNameParts)-1].(*ast.NameNamePart).Value)
} else {
alias = string(use.Alias.(*ast.Identifier).Value)
}
nsr.Namespace.AddAlias(useType, concatNameParts(prefix, useNameParts), alias)
}
}

View File

@ -17,11 +17,9 @@ func TestResolveStaticCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -51,11 +49,9 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -84,11 +80,9 @@ func TestResolveClassConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -117,11 +111,9 @@ func TestResolveNew(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -150,11 +142,9 @@ func TestResolveInstanceOf(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -186,14 +176,13 @@ func TestResolveInstanceCatch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
}, &ast.StmtUseDeclaration{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
&ast.StmtUseDeclaration{
Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")},
},
},
},
@ -232,14 +221,10 @@ func TestResolveFunctionCall(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
Type: &ast.Identifier{Value: []byte("function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -268,14 +253,10 @@ func TestResolveConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
Type: &ast.Identifier{Value: []byte("const")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -305,39 +286,25 @@ func TestResolveGroupUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtGroupUseList{
Prefix: nameAB,
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("Function")},
Use: &ast.StmtUseDeclaration{
Use: nameF,
},
},
&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseDeclaration{
Use: nameC,
},
},
},
&ast.StmtGroupUse{
Prefix: nameAB,
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("Function")},
Use: nameF,
},
&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("const")},
Use: nameC,
},
},
},
&ast.StmtUse{
UseList: &ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("Function")},
Use: &ast.StmtGroupUseList{
Prefix: nameBD,
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameE,
},
},
},
&ast.StmtGroupUse{
Prefix: nameBD,
Type: &ast.Identifier{Value: []byte("Function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameE,
},
},
},
@ -381,11 +348,9 @@ func TestResolveTraitUse(t *testing.T) {
stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
},
@ -704,11 +669,9 @@ func TestResolveNamespaces(t *testing.T) {
NamespaceName: namespaceCD,
Stmts: []ast.Vertex{
&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAC,
},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameAC,
},
},
},

View File

@ -282,11 +282,7 @@ func (v *Null) StmtUse(_ *ast.StmtUse) {
// do nothing
}
func (v *Null) StmtGroupUseList(_ *ast.StmtGroupUseList) {
// do nothing
}
func (v *Null) StmtUseList(_ *ast.StmtUseList) {
func (v *Null) StmtGroupUse(_ *ast.StmtGroupUse) {
// do nothing
}
@ -294,10 +290,6 @@ func (v *Null) StmtUseDeclaration(_ *ast.StmtUseDeclaration) {
// do nothing
}
func (v *Null) StmtUseType(_ *ast.StmtUseType) {
// do nothing
}
func (v *Null) StmtWhile(_ *ast.StmtWhile) {
// do nothing
}

View File

@ -399,14 +399,10 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
p.printStmtUnset(n)
case *ast.StmtUse:
p.printStmtUse(n)
case *ast.StmtGroupUseList:
p.printStmtGroupUseList(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtGroupUse:
p.printStmtGroupUse(n)
case *ast.StmtUseDeclaration:
p.printStmtUseDeclaration(n)
case *ast.StmtUseType:
p.printStmtUseType(n)
case *ast.StmtWhile:
p.printStmtWhile(n)
}
@ -2139,30 +2135,41 @@ func (p *PrettyPrinter) printStmtUse(n ast.Vertex) {
io.WriteString(p.w, "use ")
p.Print(nn.UseList)
if nn.Type != nil {
p.Print(nn.Type)
io.WriteString(p.w, " ")
}
p.joinPrint(", ", nn.UseDeclarations)
io.WriteString(p.w, ";")
}
func (p *PrettyPrinter) printStmtGroupUseList(n ast.Vertex) {
nn := n.(*ast.StmtGroupUseList)
func (p *PrettyPrinter) printStmtGroupUse(n ast.Vertex) {
nn := n.(*ast.StmtGroupUse)
io.WriteString(p.w, "use ")
if nn.Type != nil {
p.Print(nn.Type)
io.WriteString(p.w, " ")
}
p.Print(nn.Prefix)
io.WriteString(p.w, "\\{")
p.Print(nn.UseList)
io.WriteString(p.w, "}")
}
func (p *PrettyPrinter) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
p.joinPrint(", ", nn.UseDeclarations)
io.WriteString(p.w, "}")
}
func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
nn := n.(*ast.StmtUseDeclaration)
if nn.Type != nil {
p.Print(nn.Type)
io.WriteString(p.w, " ")
}
p.Print(nn.Use)
if nn.Alias != nil {
@ -2171,15 +2178,6 @@ func (p *PrettyPrinter) printStmtUseDeclaration(n ast.Vertex) {
}
}
func (p *PrettyPrinter) printStmtUseType(n ast.Vertex) {
nn := n.(*ast.StmtUseType)
p.Print(nn.Type)
io.WriteString(p.w, " ")
p.Print(nn.Use)
}
func (p *PrettyPrinter) printStmtWhile(n ast.Vertex) {
nn := n.(*ast.StmtWhile)

View File

@ -3954,39 +3954,7 @@ func TestPrintUse(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{},
})
expected := `use ;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtGroupUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtGroupUseList{
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: &ast.StmtUseList{},
})
expected := `Foo\{}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseList{
Type: &ast.Identifier{Value: []byte("function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
@ -3998,7 +3966,33 @@ func TestPrintStmtUseList(t *testing.T) {
},
})
expected := `Foo as Bar, Baz`
expected := `use function Foo as Bar, Baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintStmtGroupUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtGroupUse{
Type: &ast.Identifier{Value: []byte("function")},
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
},
},
})
expected := `use function Foo\{Foo as Bar, Baz}`
actual := o.String()
if expected != actual {
@ -4011,30 +4005,11 @@ func TestPrintUseDeclaration(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
})
expected := `Foo as Bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrintUseType(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
})
expected := `function Foo as Bar`
actual := o.String()

View File

@ -76,6 +76,23 @@ func (p *Printer) printFreeFloatingOrDefault(n ast.Vertex, pos token.Position, d
}
}
func (p *Printer) printToken(t *token.Token, def string) {
if t != nil {
p.w.Write(t.Skipped)
p.w.Write(t.Value)
p.bufStart = ""
return
}
if def != "" {
p.w.Write([]byte(p.bufStart))
p.bufStart = ""
p.w.Write([]byte(def))
return
}
}
func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
if n == nil {
return
@ -87,7 +104,7 @@ func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
}
func (p *Printer) printNode(n ast.Vertex) {
switch n.(type) {
switch n := n.(type) {
// node
@ -438,14 +455,10 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtUnset(n)
case *ast.StmtUse:
p.printStmtUse(n)
case *ast.StmtGroupUseList:
p.printStmtGroupUseList(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtGroupUse:
p.printStmtGroupUse(n)
case *ast.StmtUseDeclaration:
p.printStmtUseDeclaration(n)
case *ast.StmtUseType:
p.printStmtUseType(n)
case *ast.StmtWhile:
p.printStmtWhile(n)
case *ast.ParserAs:
@ -3260,74 +3273,66 @@ func (p *Printer) printStmtUnset(n ast.Vertex) {
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUse(n ast.Vertex) {
nn := n.(*ast.StmtUse)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "use")
func (p *Printer) printStmtUse(n *ast.StmtUse) {
p.printToken(n.UseTkn, "use")
p.bufStart = " "
p.Print(nn.UseList)
p.Print(n.Type)
p.printFreeFloatingOrDefault(nn, token.End, ";")
p.bufStart = " "
p.joinPrint(",", n.UseDeclarations)
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtGroupUseList(n ast.Vertex) {
nn := n.(*ast.StmtGroupUseList)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtGroupUse(n *ast.StmtGroupUse) {
p.printToken(n.UseTkn, "use")
p.Print(nn.Prefix)
p.bufStart = " "
p.Print(n.Type)
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
io.WriteString(p.w, "\\{")
}
p.bufStart = " "
p.printToken(n.LeadingNsSeparatorTkn, "")
p.Print(nn.UseList)
p.Print(n.Prefix)
p.printToken(n.NsSeparatorTkn, "\\")
p.printToken(n.OpenCurlyBracketTkn, "{")
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
io.WriteString(p.w, "}")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList)
p.printFreeFloating(nn, token.Start)
p.joinPrint(",", nn.UseDeclarations)
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtUseDeclaration(n ast.Vertex) {
nn := n.(*ast.StmtUseDeclaration)
p.printFreeFloating(nn, token.Start)
p.Print(nn.Use)
if nn.Alias != nil {
if _, ok := nn.Alias.(*ast.ParserAs); !ok {
io.WriteString(p.w, " as")
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.bufStart = " "
p.Print(nn.Alias)
}
p.printFreeFloating(nn, token.End)
p.printToken(n.CloseCurlyBracketTkn, "}")
p.printToken(n.SemiColonTkn, ";")
}
func (p *Printer) printStmtUseType(n ast.Vertex) {
nn := n.(*ast.StmtUseType)
p.printFreeFloating(nn, token.Start)
func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
p.Print(n.Type)
p.Print(nn.Type)
if n.Type != nil {
p.bufStart = " "
}
p.printToken(n.NsSeparatorTkn, "")
p.Print(n.Use)
if n.Alias == nil {
return
}
p.bufStart = " "
p.Print(nn.Use)
p.printToken(n.AsTkn, "as")
p.printFreeFloating(nn, token.End)
p.bufStart = " "
p.Print(n.Alias)
}
func (p *Printer) printStmtWhile(n ast.Vertex) {

View File

@ -1308,7 +1308,8 @@ func TestParseAndPrintPhp5Unset(t *testing.T) {
}
func TestParseAndPrintPhp5UseList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
use Foo ;
use \ Foo as Bar ;
use function \ Foo as Bar ;

View File

@ -1159,7 +1159,8 @@ func TestParseAndPrintGoto(t *testing.T) {
}
func TestParseAndPrintGroupUse(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
use function Foo \ { Bar as Baz , Quuz , } ;
use Foo \ { function Bar as Baz , const Quuz } ;
use \ Foo \ { function Bar as Baz , const Quuz , } ;
@ -1440,7 +1441,8 @@ func TestParseAndPrintUnset(t *testing.T) {
}
func TestParseAndPrintUseList(t *testing.T) {
src := `<?php
// TODO: remove ; after <?php
src := `<?php ;
use Foo ;
use \ Foo as Bar ;
use function \ Foo as Bar ;

View File

@ -4391,45 +4391,7 @@ func TestPrinterPrintUse(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
},
},
},
})
expected := `use Foo;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtGroupUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtGroupUseList{
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseList: &ast.StmtUseList{},
})
expected := `Foo\{}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtUseList(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseList{
Type: &ast.Identifier{Value: []byte("function")},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
@ -4441,7 +4403,33 @@ func TestPrinterPrintStmtUseList(t *testing.T) {
},
})
expected := `Foo as Bar,Baz`
expected := `use function Foo as Bar,Baz;`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtGroupUse(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtGroupUse{
Type: &ast.Identifier{Value: []byte("function")},
Prefix: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
&ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Baz")}}},
},
},
})
expected := `use function Foo\{Foo as Bar,Baz};`
actual := o.String()
if expected != actual {
@ -4454,30 +4442,11 @@ func TestPrinterPrintUseDeclaration(t *testing.T) {
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
})
expected := `Foo as Bar`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintUseType(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")},
},
})
expected := `function Foo as Bar`
actual := o.String()