[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 2761 additions and 3639 deletions

File diff suppressed because it is too large Load Diff

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

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

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

@ -169,6 +169,28 @@ func (v *Dump) printNode(n *ast.Node) {
v.print("},\n") 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) { func (v *Dump) Root(n *ast.Root) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.Root{\n") v.print("&ast.Root{\n")
@ -588,30 +610,30 @@ func (v *Dump) StmtUse(n *ast.StmtUse) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUse{\n") v.print("&ast.StmtUse{\n")
v.printNode(n.GetNode()) 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.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtGroupUseList{\n") v.print("&ast.StmtGroupUse{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtUseList(n *ast.StmtUseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseList{\n")
v.printNode(n.GetNode()) 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) { func (v *Dump) StmtUseDeclaration(n *ast.StmtUseDeclaration) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseDeclaration{\n") v.print("&ast.StmtUseDeclaration{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
} v.printToken("NsSeparatorTkn", n.NsSeparatorTkn)
v.printToken("AsTkn", n.AsTkn)
func (v *Dump) StmtUseType(n *ast.StmtUseType) { v.printToken("CommaTkn", n.CommaTkn)
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtUseType{\n")
v.printNode(n.GetNode())
} }
func (v *Dump) StmtWhile(n *ast.StmtWhile) { func (v *Dump) StmtWhile(n *ast.StmtWhile) {

View File

@ -13,34 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true 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) { func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) {
for { for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok { 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 { func (v *FilterTokens) EnterNode(n ast.Vertex) bool {
n.GetNode().Tokens = nil n.GetNode().Tokens = nil
n.Accept(v)
return true 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 Namespace *Namespace
ResolvedNames map[ast.Vertex]string ResolvedNames map[ast.Vertex]string
goDeep bool goDeep bool
useType string
usePrefix []ast.Vertex
} }
// NewNamespaceResolver NamespaceResolver type constructor // 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 { 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) { func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUse) {
if n.Prefix != nil { useType := ""
nsr.usePrefix = n.Prefix.(*ast.NameName).Parts if n.Type != nil {
} useType = string(n.Type.(*ast.Identifier).Value)
}
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)
} }
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 nsr.goDeep = false
} }
@ -215,10 +213,26 @@ func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) {
if nn.Stmts != nil { if nn.Stmts != nil {
nsr.Namespace = NewNamespace("") 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{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -51,11 +49,9 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -84,11 +80,9 @@ func TestResolveClassConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -117,11 +111,9 @@ func TestResolveNew(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -150,11 +142,9 @@ func TestResolveInstanceOf(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -186,14 +176,13 @@ func TestResolveInstanceCatch(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB, },
}, &ast.StmtUseDeclaration{ &ast.StmtUseDeclaration{
Use: nameDE, Use: nameDE,
Alias: &ast.Identifier{Value: []byte("F")}, Alias: &ast.Identifier{Value: []byte("F")},
},
}, },
}, },
}, },
@ -232,14 +221,10 @@ func TestResolveFunctionCall(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseType{ Type: &ast.Identifier{Value: []byte("function")},
Type: &ast.Identifier{Value: []byte("function")}, UseDeclarations: []ast.Vertex{
Use: &ast.StmtUseList{ &ast.StmtUseDeclaration{
UseDeclarations: []ast.Vertex{ Use: nameAB,
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
}, },
}, },
}, },
@ -268,14 +253,10 @@ func TestResolveConstFetch(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseType{ Type: &ast.Identifier{Value: []byte("const")},
Type: &ast.Identifier{Value: []byte("const")}, UseDeclarations: []ast.Vertex{
Use: &ast.StmtUseList{ &ast.StmtUseDeclaration{
UseDeclarations: []ast.Vertex{ Use: nameAB,
&ast.StmtUseDeclaration{
Use: nameAB,
},
},
}, },
}, },
}, },
@ -305,39 +286,25 @@ func TestResolveGroupUse(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtGroupUse{
UseList: &ast.StmtGroupUseList{ Prefix: nameAB,
Prefix: nameAB, UseDeclarations: []ast.Vertex{
UseList: &ast.StmtUseList{ &ast.StmtUseDeclaration{
UseDeclarations: []ast.Vertex{ Type: &ast.Identifier{Value: []byte("Function")},
&ast.StmtUseType{ Use: nameF,
Type: &ast.Identifier{Value: []byte("Function")}, },
Use: &ast.StmtUseDeclaration{ &ast.StmtUseDeclaration{
Use: nameF, Type: &ast.Identifier{Value: []byte("const")},
}, Use: nameC,
},
&ast.StmtUseType{
Type: &ast.Identifier{Value: []byte("const")},
Use: &ast.StmtUseDeclaration{
Use: nameC,
},
},
},
}, },
}, },
}, },
&ast.StmtUse{ &ast.StmtGroupUse{
UseList: &ast.StmtUseType{ Prefix: nameBD,
Type: &ast.Identifier{Value: []byte("Function")}, Type: &ast.Identifier{Value: []byte("Function")},
Use: &ast.StmtGroupUseList{ UseDeclarations: []ast.Vertex{
Prefix: nameBD, &ast.StmtUseDeclaration{
UseList: &ast.StmtUseList{ Use: nameE,
UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{
Use: nameE,
},
},
},
}, },
}, },
}, },
@ -381,11 +348,9 @@ func TestResolveTraitUse(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAB,
Use: nameAB,
},
}, },
}, },
}, },
@ -704,11 +669,9 @@ func TestResolveNamespaces(t *testing.T) {
NamespaceName: namespaceCD, NamespaceName: namespaceCD,
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtUse{ &ast.StmtUse{
UseList: &ast.StmtUseList{ UseDeclarations: []ast.Vertex{
UseDeclarations: []ast.Vertex{ &ast.StmtUseDeclaration{
&ast.StmtUseDeclaration{ Use: nameAC,
Use: nameAC,
},
}, },
}, },
}, },

View File

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

View File

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

View File

@ -3954,39 +3954,7 @@ func TestPrintUse(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUse{ p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{}, Type: &ast.Identifier{Value: []byte("function")},
})
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{
UseDeclarations: []ast.Vertex{ UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{ &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, 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() actual := o.String()
if expected != actual { if expected != actual {
@ -4011,30 +4005,11 @@ func TestPrintUseDeclaration(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtUseDeclaration{ p.Print(&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")}, 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` expected := `function Foo as Bar`
actual := o.String() 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) { func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
if n == nil { if n == nil {
return return
@ -87,7 +104,7 @@ func (p *Printer) printFreeFloating(n ast.Vertex, pos token.Position) {
} }
func (p *Printer) printNode(n ast.Vertex) { func (p *Printer) printNode(n ast.Vertex) {
switch n.(type) { switch n := n.(type) {
// node // node
@ -438,14 +455,10 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtUnset(n) p.printStmtUnset(n)
case *ast.StmtUse: case *ast.StmtUse:
p.printStmtUse(n) p.printStmtUse(n)
case *ast.StmtGroupUseList: case *ast.StmtGroupUse:
p.printStmtGroupUseList(n) p.printStmtGroupUse(n)
case *ast.StmtUseList:
p.printStmtUseList(n)
case *ast.StmtUseDeclaration: case *ast.StmtUseDeclaration:
p.printStmtUseDeclaration(n) p.printStmtUseDeclaration(n)
case *ast.StmtUseType:
p.printStmtUseType(n)
case *ast.StmtWhile: case *ast.StmtWhile:
p.printStmtWhile(n) p.printStmtWhile(n)
case *ast.ParserAs: case *ast.ParserAs:
@ -3260,74 +3273,66 @@ func (p *Printer) printStmtUnset(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtUse(n ast.Vertex) { func (p *Printer) printStmtUse(n *ast.StmtUse) {
nn := n.(*ast.StmtUse) p.printToken(n.UseTkn, "use")
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "use")
p.bufStart = " " 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) { func (p *Printer) printStmtGroupUse(n *ast.StmtGroupUse) {
nn := n.(*ast.StmtGroupUseList) p.printToken(n.UseTkn, "use")
p.printFreeFloating(nn, token.Start)
p.Print(nn.Prefix) p.bufStart = " "
p.Print(n.Type)
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok { p.bufStart = " "
io.WriteString(p.w, "\\{") 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 { for k, v := range n.UseDeclarations {
io.WriteString(p.w, "}") p.Print(v)
} var def string
if k != len(n.UseDeclarations)-1 {
p.printFreeFloating(nn, token.End) def = ","
} }
if decl, ok := v.(*ast.StmtUseDeclaration); ok {
func (p *Printer) printStmtUseList(n ast.Vertex) { p.printToken(decl.CommaTkn, def)
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")
} }
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) { func (p *Printer) printStmtUseDeclaration(n *ast.StmtUseDeclaration) {
nn := n.(*ast.StmtUseType) p.Print(n.Type)
p.printFreeFloating(nn, token.Start)
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.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) { func (p *Printer) printStmtWhile(n ast.Vertex) {

View File

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

View File

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

View File

@ -4391,45 +4391,7 @@ func TestPrinterPrintUse(t *testing.T) {
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtUse{ p.Print(&ast.StmtUse{
UseList: &ast.StmtUseList{ Type: &ast.Identifier{Value: []byte("function")},
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{
UseDeclarations: []ast.Vertex{ UseDeclarations: []ast.Vertex{
&ast.StmtUseDeclaration{ &ast.StmtUseDeclaration{
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, 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() actual := o.String()
if expected != actual { if expected != actual {
@ -4454,30 +4442,11 @@ func TestPrinterPrintUseDeclaration(t *testing.T) {
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtUseDeclaration{ p.Print(&ast.StmtUseDeclaration{
Type: &ast.Identifier{Value: []byte("function")},
Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}}, Use: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("Foo")}}},
Alias: &ast.Identifier{Value: []byte("Bar")}, 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` expected := `function Foo as Bar`
actual := o.String() actual := o.String()