[refactoring] add Parser nodes for use

This commit is contained in:
Vadym Slizov 2020-08-09 00:56:21 +02:00
parent 45464654c6
commit fd5d891037
15 changed files with 880 additions and 111 deletions

File diff suppressed because it is too large Load Diff

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -525,15 +525,17 @@ use_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value} alias := &ast.Identifier{ast.Node{}, $3.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3) alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments // save comments
yylex.(*Parser).setFreeFloating(name, token.End, $2.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -541,14 +543,16 @@ use_declaration:
| T_NS_SEPARATOR namespace_name | T_NS_SEPARATOR namespace_name
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil} useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewNodeListPosition($2) useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
@ -556,16 +560,20 @@ use_declaration:
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value} alias := &ast.Identifier{ast.Node{}, $4.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4) alias.GetNode().Position = position.NewTokenPosition($4)
$$.GetNode().Position = position.NewNodeListTokenPosition($2, $4) asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).setFreeFloating(name, token.End, $3.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -606,15 +614,17 @@ use_function_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value} alias := &ast.Identifier{ast.Node{}, $3.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3) alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments // save comments
yylex.(*Parser).setFreeFloating(name, token.End, $2.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -622,14 +632,16 @@ use_function_declaration:
| T_NS_SEPARATOR namespace_name | T_NS_SEPARATOR namespace_name
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil} useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewNodeListPosition($2) useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
@ -637,16 +649,20 @@ use_function_declaration:
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value} alias := &ast.Identifier{ast.Node{}, $4.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4) alias.GetNode().Position = position.NewTokenPosition($4)
$$.GetNode().Position = position.NewNodeListTokenPosition($2, $4) asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).setFreeFloating(name, token.End, $3.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -687,15 +703,17 @@ use_const_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value} alias := &ast.Identifier{ast.Node{}, $3.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3) alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments // save comments
yylex.(*Parser).setFreeFloating(name, token.End, $2.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -703,14 +721,16 @@ use_const_declaration:
| T_NS_SEPARATOR namespace_name | T_NS_SEPARATOR namespace_name
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, nil} useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, nil}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewNodeListPosition($2) useDeclaration.GetNode().Position = position.NewTokenNodePosition($1, name)
$$.GetNode().Position = position.NewTokenNodePosition($1, name)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
@ -718,16 +738,20 @@ use_const_declaration:
{ {
name := &ast.NameName{ast.Node{}, $2} name := &ast.NameName{ast.Node{}, $2}
alias := &ast.Identifier{ast.Node{}, $4.Value} alias := &ast.Identifier{ast.Node{}, $4.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
useDeclaration := &ast.StmtUseDeclaration{ast.Node{}, name, alias}
$$ = &ast.ParserNsSeparator{ast.Node{}, useDeclaration}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
alias.GetNode().Position = position.NewTokenPosition($4) alias.GetNode().Position = position.NewTokenPosition($4)
$$.GetNode().Position = position.NewNodeListTokenPosition($2, $4) asAlias.GetNode().Position = position.NewTokensPosition($3, $4)
useDeclaration.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).setFreeFloating(name, token.End, $3.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $3.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $4.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)

View File

@ -9,6 +9,8 @@ import (
"github.com/z7zmey/php-parser/internal/php5" "github.com/z7zmey/php-parser/internal/php5"
"github.com/z7zmey/php-parser/internal/scanner" "github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/traverser"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
"github.com/z7zmey/php-parser/pkg/errors" "github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/position" "github.com/z7zmey/php-parser/pkg/position"
) )
@ -8594,7 +8596,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 155, StartLine: 155,
EndLine: 155, EndLine: 155,
StartPos: 3295, StartPos: 3294,
EndPos: 3298, EndPos: 3298,
}, },
}, },
@ -8604,7 +8606,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 155, StartLine: 155,
EndLine: 155, EndLine: 155,
StartPos: 3295, StartPos: 3294,
EndPos: 3298, EndPos: 3298,
}, },
}, },
@ -8649,7 +8651,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 156, StartLine: 156,
EndLine: 156, EndLine: 156,
StartPos: 3305, StartPos: 3304,
EndPos: 3315, EndPos: 3315,
}, },
}, },
@ -8659,7 +8661,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 156, StartLine: 156,
EndLine: 156, EndLine: 156,
StartPos: 3305, StartPos: 3304,
EndPos: 3315, EndPos: 3315,
}, },
}, },
@ -8965,7 +8967,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 159, StartLine: 159,
EndLine: 159, EndLine: 159,
StartPos: 3371, StartPos: 3370,
EndPos: 3374, EndPos: 3374,
}, },
}, },
@ -9085,7 +9087,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 160, StartLine: 160,
EndLine: 160, EndLine: 160,
StartPos: 3402, StartPos: 3401,
EndPos: 3412, EndPos: 3412,
}, },
}, },
@ -9205,7 +9207,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 161, StartLine: 161,
EndLine: 161, EndLine: 161,
StartPos: 3430, StartPos: 3429,
EndPos: 3433, EndPos: 3433,
}, },
}, },
@ -9325,7 +9327,7 @@ func TestPhp5(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 162, StartLine: 162,
EndLine: 162, EndLine: 162,
StartPos: 3458, StartPos: 3457,
EndPos: 3468, EndPos: 3468,
}, },
}, },
@ -22214,6 +22216,7 @@ func TestPhp5(t *testing.T) {
php5parser := php5.NewParser(lexer, nil) php5parser := php5.NewParser(lexer, nil)
php5parser.Parse() php5parser.Parse()
actual := php5parser.GetRootNode() actual := php5parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
@ -22352,6 +22355,7 @@ func TestPhp5Strings(t *testing.T) {
php5parser := php5.NewParser(lexer, nil) php5parser := php5.NewParser(lexer, nil)
php5parser.Parse() php5parser.Parse()
actual := php5parser.GetRootNode() actual := php5parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
@ -22579,6 +22583,7 @@ CAD;
php5parser := php5.NewParser(lexer, nil) php5parser := php5.NewParser(lexer, nil)
php5parser.Parse() php5parser.Parse()
actual := php5parser.GetRootNode() actual := php5parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }

File diff suppressed because it is too large Load Diff

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -628,42 +628,52 @@ group_use_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
useList := &ast.StmtUseList{ast.Node{}, $4} useList := &ast.StmtUseList{ast.Node{}, $4}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useList} useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useListNsSeparator}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
useList.GetNode().Position = position.NewNodeListPosition($4) 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) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $6)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens(useList, token.Start, append($2.Tokens, $3.Tokens...))
if $5 != nil { if $5 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, append($5.Tokens, $6.Tokens...)) yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $5.Tokens)
} else {
yylex.(*Parser).setFreeFloating(useList, token.End, $6.Tokens)
} }
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $3.Tokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $6.Tokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $2.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
| 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} name := &ast.NameName{ast.Node{}, $2}
prefixNsSeparator := &ast.ParserNsSeparator{ast.Node{}, name}
useList := &ast.StmtUseList{ast.Node{}, $5} useList := &ast.StmtUseList{ast.Node{}, $5}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useList} useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, prefixNsSeparator, useListNsSeparator}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
prefixNsSeparator.GetNode().Position = position.NewTokenNodePosition($1, name)
useList.GetNode().Position = position.NewNodeListPosition($5) 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) $$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens(useList, token.Start, append($3.Tokens, $4.Tokens...)) yylex.(*Parser).setFreeFloating(prefixNsSeparator, token.Start, $1.Tokens)
if $6 != nil { if $6 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, append($6.Tokens, $7.Tokens...)) yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $6.Tokens)
} else {
yylex.(*Parser).setFreeFloating(useList, token.End, $7.Tokens)
} }
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $4.Tokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $7.Tokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
@ -674,42 +684,52 @@ mixed_group_use_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
useList := &ast.StmtUseList{ast.Node{}, $4} useList := &ast.StmtUseList{ast.Node{}, $4}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useList} useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useListNsSeparator}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
useList.GetNode().Position = position.NewNodeListPosition($4) 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) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $6)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens(useList, token.Start, append($2.Tokens, $3.Tokens...))
if $5 != nil { if $5 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, append($5.Tokens, $6.Tokens...)) yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $5.Tokens)
} else {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $6.Tokens)
} }
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $3.Tokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $6.Tokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $2.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
| 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} name := &ast.NameName{ast.Node{}, $2}
prefixNsSeparator := &ast.ParserNsSeparator{ast.Node{}, name}
useList := &ast.StmtUseList{ast.Node{}, $5} useList := &ast.StmtUseList{ast.Node{}, $5}
$$ = &ast.StmtGroupUseList{ast.Node{}, name, useList} useListBrackets := &ast.ParserBrackets{ast.Node{}, useList}
useListNsSeparator := &ast.ParserNsSeparator{ast.Node{}, useListBrackets}
$$ = &ast.StmtGroupUseList{ast.Node{}, prefixNsSeparator, useListNsSeparator}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($2) name.GetNode().Position = position.NewNodeListPosition($2)
prefixNsSeparator.GetNode().Position = position.NewTokenNodePosition($1, name)
useList.GetNode().Position = position.NewNodeListPosition($5) 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) $$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens(useList, token.Start, append($3.Tokens, $4.Tokens...)) yylex.(*Parser).setFreeFloating(prefixNsSeparator, token.Start, $1.Tokens)
if $6 != nil { if $6 != nil {
yylex.(*Parser).setFreeFloatingTokens(useList, token.End, append($6.Tokens, $7.Tokens...)) yylex.(*Parser).setFreeFloatingTokens(useList, token.End, $6.Tokens)
} else {
yylex.(*Parser).setFreeFloating(useList, token.End, $7.Tokens)
} }
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.Start, $4.Tokens)
yylex.(*Parser).setFreeFloatingTokens(useListBrackets, token.End, $7.Tokens)
yylex.(*Parser).setFreeFloating(useListNsSeparator, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }
@ -814,15 +834,17 @@ unprefixed_use_declaration:
{ {
name := &ast.NameName{ast.Node{}, $1} name := &ast.NameName{ast.Node{}, $1}
alias := &ast.Identifier{ast.Node{}, $3.Value} alias := &ast.Identifier{ast.Node{}, $3.Value}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, alias} asAlias := &ast.ParserAs{ast.Node{}, alias}
$$ = &ast.StmtUseDeclaration{ast.Node{}, name, asAlias}
// save position // save position
name.GetNode().Position = position.NewNodeListPosition($1) name.GetNode().Position = position.NewNodeListPosition($1)
alias.GetNode().Position = position.NewTokenPosition($3) alias.GetNode().Position = position.NewTokenPosition($3)
asAlias.GetNode().Position = position.NewTokensPosition($2, $3)
$$.GetNode().Position = position.NewNodeListTokenPosition($1, $3) $$.GetNode().Position = position.NewNodeListTokenPosition($1, $3)
// save comments // save comments
yylex.(*Parser).setFreeFloating(name, token.End, $2.Tokens) yylex.(*Parser).setFreeFloating(asAlias, token.Start, $2.Tokens)
yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens) yylex.(*Parser).setFreeFloating(alias, token.Start, $3.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
@ -838,10 +860,14 @@ use_declaration:
} }
| T_NS_SEPARATOR unprefixed_use_declaration | T_NS_SEPARATOR unprefixed_use_declaration
{ {
$$ = $2; $$ = &ast.ParserNsSeparator{ast.Node{}, $2}
// save position
$2.GetNode().Position = position.NewTokenNodePosition($1, $2)
$$.GetNode().Position = position.NewTokenNodePosition($1, $2)
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens)
yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL)
} }

View File

@ -9,6 +9,8 @@ import (
"github.com/z7zmey/php-parser/internal/php7" "github.com/z7zmey/php-parser/internal/php7"
"github.com/z7zmey/php-parser/internal/scanner" "github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/traverser"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
"github.com/z7zmey/php-parser/pkg/errors" "github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/position" "github.com/z7zmey/php-parser/pkg/position"
) )
@ -9082,7 +9084,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 162, StartLine: 162,
EndLine: 162, EndLine: 162,
StartPos: 3376, StartPos: 3375,
EndPos: 3379, EndPos: 3379,
}, },
}, },
@ -9092,7 +9094,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 162, StartLine: 162,
EndLine: 162, EndLine: 162,
StartPos: 3376, StartPos: 3375,
EndPos: 3379, EndPos: 3379,
}, },
}, },
@ -9137,7 +9139,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 163, StartLine: 163,
EndLine: 163, EndLine: 163,
StartPos: 3386, StartPos: 3385,
EndPos: 3396, EndPos: 3396,
}, },
}, },
@ -9147,7 +9149,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 163, StartLine: 163,
EndLine: 163, EndLine: 163,
StartPos: 3386, StartPos: 3385,
EndPos: 3396, EndPos: 3396,
}, },
}, },
@ -9453,7 +9455,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 166, StartLine: 166,
EndLine: 166, EndLine: 166,
StartPos: 3452, StartPos: 3451,
EndPos: 3455, EndPos: 3455,
}, },
}, },
@ -9573,7 +9575,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 167, StartLine: 167,
EndLine: 167, EndLine: 167,
StartPos: 3483, StartPos: 3482,
EndPos: 3493, EndPos: 3493,
}, },
}, },
@ -9693,7 +9695,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 168, StartLine: 168,
EndLine: 168, EndLine: 168,
StartPos: 3511, StartPos: 3510,
EndPos: 3514, EndPos: 3514,
}, },
}, },
@ -9813,7 +9815,7 @@ func TestPhp7(t *testing.T) {
Position: &position.Position{ Position: &position.Position{
StartLine: 169, StartLine: 169,
EndLine: 169, EndLine: 169,
StartPos: 3539, StartPos: 3538,
EndPos: 3549, EndPos: 3549,
}, },
}, },
@ -19597,6 +19599,7 @@ func TestPhp7(t *testing.T) {
php7parser := php7.NewParser(lexer, nil) php7parser := php7.NewParser(lexer, nil)
php7parser.Parse() php7parser.Parse()
actual := php7parser.GetRootNode() actual := php7parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
@ -19735,6 +19738,7 @@ func TestPhp5Strings(t *testing.T) {
php7parser := php7.NewParser(lexer, nil) php7parser := php7.NewParser(lexer, nil)
php7parser.Parse() php7parser.Parse()
actual := php7parser.GetRootNode() actual := php7parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
@ -19962,6 +19966,7 @@ CAD;
php7parser := php7.NewParser(lexer, nil) php7parser := php7.NewParser(lexer, nil)
php7parser.Parse() php7parser.Parse()
actual := php7parser.GetRootNode() actual := php7parser.GetRootNode()
traverser.NewDFS(new(visitor.FilterParserNodes)).Traverse(actual)
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }

View File

@ -198,4 +198,8 @@ type NodeVisitor interface {
NameFullyQualified(n *NameFullyQualified) NameFullyQualified(n *NameFullyQualified)
NameRelative(n *NameRelative) NameRelative(n *NameRelative)
NameNamePart(n *NameNamePart) NameNamePart(n *NameNamePart)
ParserAs(n *ParserAs)
ParserNsSeparator(n *ParserNsSeparator)
ParserBrackets(n *ParserBrackets)
} }

View File

@ -1876,3 +1876,30 @@ type NameNamePart struct {
func (n *NameNamePart) Accept(v NodeVisitor) { func (n *NameNamePart) Accept(v NodeVisitor) {
v.NameNamePart(n) v.NameNamePart(n)
} }
type ParserAs struct {
Node
Child Vertex
}
func (n *ParserAs) Accept(v NodeVisitor) {
v.ParserAs(n)
}
type ParserNsSeparator struct {
Node
Child Vertex
}
func (n *ParserNsSeparator) Accept(v NodeVisitor) {
v.ParserNsSeparator(n)
}
type ParserBrackets struct {
Node
Child Vertex
}
func (n *ParserBrackets) Accept(v NodeVisitor) {
v.ParserBrackets(n)
}

View File

@ -2783,6 +2783,42 @@ func (t *DFS) Traverse(n ast.Vertex) {
if !t.visitor.EnterNode(nn) { if !t.visitor.EnterNode(nn) {
return return
} }
case *ast.ParserAs:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Child != nil {
t.visitor.Enter("Child", true)
t.Traverse(nn.Child)
t.visitor.Leave("Child", true)
}
case *ast.ParserNsSeparator:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Child != nil {
t.visitor.Enter("Child", true)
t.Traverse(nn.Child)
t.visitor.Leave("Child", true)
}
case *ast.ParserBrackets:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Child != nil {
t.visitor.Enter("Child", true)
t.Traverse(nn.Child)
t.visitor.Leave("Child", true)
}
default: default:
panic("unexpected type of node") panic("unexpected type of node")
} }

View File

@ -1288,3 +1288,21 @@ func (v *Dump) NameNamePart(n *ast.NameNamePart) {
v.printIndent(v.indent) v.printIndent(v.indent)
v.print(fmt.Sprintf("Value: []byte(%q),\n", n.Value)) v.print(fmt.Sprintf("Value: []byte(%q),\n", n.Value))
} }
func (v *Dump) ParserAs(n *ast.ParserAs) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ParserAs{\n")
v.printNode(n.GetNode())
}
func (v *Dump) ParserNsSeparator(n *ast.ParserNsSeparator) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ParserNsSeparator{\n")
v.printNode(n.GetNode())
}
func (v *Dump) ParserBrackets(n *ast.ParserBrackets) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.ParserBrackets{\n")
v.printNode(n.GetNode())
}

View File

@ -0,0 +1,42 @@
package visitor
import (
"github.com/z7zmey/php-parser/pkg/ast"
)
type FilterParserNodes struct {
Null
}
func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
n.Accept(v)
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
}
}

View File

@ -713,3 +713,15 @@ func (v *Null) NameRelative(_ *ast.NameRelative) {
func (v *Null) NameNamePart(_ *ast.NameNamePart) { func (v *Null) NameNamePart(_ *ast.NameNamePart) {
// do nothing // do nothing
} }
func (v *Null) ParserAs(_ *ast.ParserAs) {
// do nothing
}
func (v *Null) ParserNsSeparator(_ *ast.ParserNsSeparator) {
// do nothing
}
func (v *Null) ParserBrackets(_ *ast.ParserBrackets) {
// do nothing
}

View File

@ -19,7 +19,6 @@ type Printer struct {
w io.Writer w io.Writer
s printerState s printerState
bufStart string bufStart string
bufEnd string
} }
// NewPrinter - Constructor for Printer // NewPrinter - Constructor for Printer
@ -449,6 +448,12 @@ func (p *Printer) printNode(n ast.Vertex) {
p.printStmtUseType(n) p.printStmtUseType(n)
case *ast.StmtWhile: case *ast.StmtWhile:
p.printStmtWhile(n) p.printStmtWhile(n)
case *ast.ParserAs:
p.printParserAs(n)
case *ast.ParserNsSeparator:
p.printParserNsSeparator(n)
case *ast.ParserBrackets:
p.printParserBrackets(n)
} }
} }
@ -556,8 +561,7 @@ func (p *Printer) printNameName(n ast.Vertex) {
p.joinPrint("\\", nn.Parts) p.joinPrint("\\", nn.Parts)
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd) p.printFreeFloating(nn, token.End)
p.bufEnd = ""
} }
func (p *Printer) printNameFullyQualified(n ast.Vertex) { func (p *Printer) printNameFullyQualified(n ast.Vertex) {
@ -568,8 +572,7 @@ func (p *Printer) printNameFullyQualified(n ast.Vertex) {
io.WriteString(p.w, "\\") io.WriteString(p.w, "\\")
p.joinPrint("\\", nn.Parts) p.joinPrint("\\", nn.Parts)
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd) p.printFreeFloating(nn, token.End)
p.bufEnd = ""
} }
func (p *Printer) printNameRelative(n ast.Vertex) { func (p *Printer) printNameRelative(n ast.Vertex) {
@ -585,8 +588,7 @@ func (p *Printer) printNameRelative(n ast.Vertex) {
p.Print(part) p.Print(part)
} }
p.printFreeFloatingOrDefault(nn, token.End, p.bufEnd) p.printFreeFloating(nn, token.End)
p.bufEnd = ""
} }
// scalar // scalar
@ -3203,43 +3205,38 @@ func (p *Printer) printStmtGroupUseList(n ast.Vertex) {
p.Print(nn.Prefix) p.Print(nn.Prefix)
p.bufStart = "\\{" if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
p.bufEnd = "}" io.WriteString(p.w, "\\{")
}
p.Print(nn.UseList) p.Print(nn.UseList)
if _, ok := nn.UseList.(*ast.ParserNsSeparator); !ok {
io.WriteString(p.w, "}")
}
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtUseList(n ast.Vertex) { func (p *Printer) printStmtUseList(n ast.Vertex) {
nn := n.(*ast.StmtUseList) nn := n.(*ast.StmtUseList)
bufEnd := p.bufEnd
p.bufEnd = ""
if p.bufStart == "\\{" {
p.printFreeFloatingOrDefault(nn, token.Start, p.bufStart)
p.bufStart = ""
} else {
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
}
p.joinPrint(",", nn.UseDeclarations) p.joinPrint(",", nn.UseDeclarations)
p.printFreeFloatingOrDefault(nn, token.End, bufEnd) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtUseDeclaration(n ast.Vertex) { func (p *Printer) printStmtUseDeclaration(n ast.Vertex) {
nn := n.(*ast.StmtUseDeclaration) nn := n.(*ast.StmtUseDeclaration)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
if nn.Alias != nil {
p.bufEnd = " "
}
p.Print(nn.Use) p.Print(nn.Use)
if nn.Alias != nil { if nn.Alias != nil {
io.WriteString(p.w, "as") if _, ok := nn.Alias.(*ast.ParserAs); !ok {
io.WriteString(p.w, " as")
}
p.bufStart = " " p.bufStart = " "
p.Print(nn.Alias) p.Print(nn.Alias)
@ -3275,3 +3272,32 @@ func (p *Printer) printStmtWhile(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printParserAs(n ast.Vertex) {
nn := n.(*ast.ParserAs)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "as")
p.Print(nn.Child)
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printParserNsSeparator(n ast.Vertex) {
nn := n.(*ast.ParserNsSeparator)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "\\")
p.Print(nn.Child)
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printParserBrackets(n ast.Vertex) {
nn := n.(*ast.ParserBrackets)
p.printFreeFloating(nn, token.Start)
p.Print(nn.Child)
p.printFreeFloating(nn, token.End)
}