diff --git a/internal/php5/php5.go b/internal/php5/php5.go index ece4fad..d65f45d 100644 Binary files a/internal/php5/php5.go and b/internal/php5/php5.go differ diff --git a/internal/php5/php5.y b/internal/php5/php5.y index 67d3536..e5d4558 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -18,9 +18,6 @@ import ( list []ast.Vertex simpleIndirectReference simpleIndirectReference - ClassExtends *ast.StmtClassExtends - ClassImplements *ast.StmtClassImplements - InterfaceExtends *ast.StmtInterfaceExtends ClosureUse *ast.ExprClosureUse } @@ -243,13 +240,12 @@ import ( %type ctor_arguments function_call_parameter_list echo_expr_list %type trait_adaptations unset_variables declare_list %type switch_case_list non_empty_function_call_parameter_list -%type method_body +%type method_body trait_reference_list %type foreach_statement for_statement while_statement %type foreach_variable foreach_optional_arg - -%type extends_from -%type implements_list -%type interface_extends_list +%type extends_from interface_list trait_list +%type implements_list +%type interface_extends_list %type lexical_vars %type top_statement_list namespace_name use_declarations use_function_declarations use_const_declarations @@ -258,8 +254,8 @@ import ( %type for_expr case_list catch_statement additional_catches %type non_empty_additional_catches parameter_list non_empty_parameter_list class_statement_list %type class_statement_list variable_modifiers method_modifiers class_variable_declaration -%type interface_list trait_list trait_adaptation_list non_empty_trait_adaptation_list -%type trait_reference_list non_empty_member_modifiers backticks_expr static_array_pair_list non_empty_static_array_pair_list +%type trait_adaptation_list non_empty_trait_adaptation_list +%type non_empty_member_modifiers backticks_expr static_array_pair_list non_empty_static_array_pair_list %type chaining_dereference chaining_instance_call chaining_method_or_property instance_call variable_property %type method_or_not array_method_dereference object_property object_dim_list dynamic_class_name_variable_property @@ -1527,13 +1523,13 @@ extends_from: } | T_EXTENDS fully_qualified_class_name { - $$ = &ast.StmtClassExtends{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtClassExtends{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + ExtendTkn: $1, + ClassName: $2, + } } ; @@ -1551,13 +1547,14 @@ interface_extends_list: } | T_EXTENDS interface_list { - $$ = &ast.StmtInterfaceExtends{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtInterfaceExtends{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items), + }, + ExtendsTkn: $1, + InterfaceNames: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + }; } ; @@ -1568,29 +1565,30 @@ implements_list: } | T_IMPLEMENTS interface_list { - $$ = &ast.StmtClassImplements{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtClassImplements{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items), + }, + ImplementsTkn: $1, + InterfaceNames: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + }; } ; interface_list: fully_qualified_class_name { - $$ = []ast.Vertex{$1} + $$ = &ast.ParserSeparatedList{ + Items: []ast.Vertex{$1}, + } } | interface_list ',' fully_qualified_class_name { - switch n := lastNode($1).(type) { - case *ast.NameName: n.ListSeparatorTkn = $2 - case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 - case *ast.NameRelative: n.ListSeparatorTkn = $2 - } - $$ = append($1, $3) + $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) + $1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, $3) + + $$ = $1 } ; @@ -2520,29 +2518,31 @@ class_statement: trait_use_statement: T_USE trait_list trait_adaptations { - $$ = &ast.StmtTraitUse{ast.Node{}, $2, $3} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtTraitUse{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + UseTkn: $1, + Traits: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + Adaptations: $3, + } } ; trait_list: fully_qualified_class_name { - $$ = []ast.Vertex{$1} + $$ = &ast.ParserSeparatedList{ + Items: []ast.Vertex{$1}, + } } | trait_list ',' fully_qualified_class_name { - switch n := lastNode($1).(type) { - case *ast.NameName: n.ListSeparatorTkn = $2 - case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 - case *ast.NameRelative: n.ListSeparatorTkn = $2 - } - $$ = append($1, $3) + $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) + $1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, $3) + + $$ = $1 } ; @@ -2612,28 +2612,31 @@ trait_adaptation_statement: trait_precedence: trait_method_reference_fully_qualified T_INSTEADOF trait_reference_list { - $$ = &ast.StmtTraitUsePrecedence{ast.Node{}, $1, $3} - - // save position - $$.GetNode().Position = position.NewNodeNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Ref, $2.SkippedTokens) + $$ = &ast.StmtTraitUsePrecedence{ + Node: ast.Node{ + Position: position.NewNodeNodeListPosition($1, $3.(*ast.ParserSeparatedList).Items), + }, + Ref: $1, + InsteadofTkn: $2, + Insteadof: $3.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns, + } } ; trait_reference_list: fully_qualified_class_name { - $$ = []ast.Vertex{$1} + $$ = &ast.ParserSeparatedList{ + Items: []ast.Vertex{$1}, + } } | trait_reference_list ',' fully_qualified_class_name { - $$ = append($1, $3) + $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) + $1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, $3) - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) + $$ = $1 } ; diff --git a/internal/php7/php7.go b/internal/php7/php7.go index aa80680..0a0e9af 100644 Binary files a/internal/php7/php7.go and b/internal/php7/php7.go differ diff --git a/internal/php7/php7.y b/internal/php7/php7.y index 5577a10..58eed16 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -18,9 +18,6 @@ import ( tkn *token.Token list []ast.Vertex - ClassExtends *ast.StmtClassExtends - ClassImplements *ast.StmtClassImplements - InterfaceExtends *ast.StmtInterfaceExtends ClosureUse *ast.ExprClosureUse } @@ -249,7 +246,7 @@ import ( %type exit_expr scalar lexical_var function_call member_name property_name %type variable_class_name dereferencable_scalar constant dereferencable %type callable_expr callable_variable static_member new_variable -%type encaps_var encaps_var_offset echo_expr_list catch_name_list +%type encaps_var encaps_var_offset echo_expr_list catch_name_list name_list %type if_stmt const_list non_empty_argument_list %type alt_if_stmt %type if_stmt_without_else @@ -265,9 +262,9 @@ import ( %type foreach_statement for_statement while_statement %type inline_function %type unset_variables -%type extends_from -%type implements_list -%type interface_extends_list +%type extends_from +%type implements_list +%type interface_extends_list %type lexical_vars %type member_modifier @@ -283,7 +280,7 @@ import ( %type array_pair_list top_statement_list %type inner_statement_list parameter_list non_empty_parameter_list class_statement_list %type method_modifiers variable_modifiers -%type non_empty_member_modifiers name_list class_modifiers +%type non_empty_member_modifiers class_modifiers %% @@ -1399,13 +1396,13 @@ extends_from: } | T_EXTENDS name { - $$ = &ast.StmtClassExtends{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtClassExtends{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + ExtendTkn: $1, + ClassName: $2, + } } ; @@ -1416,13 +1413,14 @@ interface_extends_list: } | T_EXTENDS name_list { - $$ = &ast.StmtInterfaceExtends{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtInterfaceExtends{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items), + }, + ExtendsTkn: $1, + InterfaceNames: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + }; } ; @@ -1433,13 +1431,14 @@ implements_list: } | T_IMPLEMENTS name_list { - $$ = &ast.StmtClassImplements{ast.Node{}, $2}; - - // save position - $$.GetNode().Position = position.NewTokenNodeListPosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtClassImplements{ + Node: ast.Node{ + Position: position.NewTokenNodeListPosition($1, $2.(*ast.ParserSeparatedList).Items), + }, + ImplementsTkn: $1, + InterfaceNames: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + }; } ; @@ -2223,13 +2222,15 @@ class_statement: } | T_USE name_list trait_adaptations { - $$ = &ast.StmtTraitUse{ast.Node{}, $2, $3} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.StmtTraitUse{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + UseTkn: $1, + Traits: $2.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $2.(*ast.ParserSeparatedList).SeparatorTkns, + Adaptations: $3, + } } | method_modifiers T_FUNCTION returns_ref identifier backup_doc_comment '(' parameter_list ')' return_type method_body { @@ -2269,16 +2270,16 @@ class_statement: name_list: name { - $$ = []ast.Vertex{$1} + $$ = &ast.ParserSeparatedList{ + Items: []ast.Vertex{$1}, + } } | name_list ',' name { - switch n := lastNode($1).(type) { - case *ast.NameName: n.ListSeparatorTkn = $2 - case *ast.NameFullyQualified: n.ListSeparatorTkn = $2 - case *ast.NameRelative: n.ListSeparatorTkn = $2 - } - $$ = append($1, $3) + $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) + $1.(*ast.ParserSeparatedList).Items = append($1.(*ast.ParserSeparatedList).Items, $3) + + $$ = $1 } ; @@ -2347,14 +2348,15 @@ trait_adaptation: trait_precedence: absolute_trait_method_reference T_INSTEADOF name_list { - $$ = &ast.StmtTraitUsePrecedence{ast.Node{}, $1, $3} - - // save position - $$.GetNode().Position = position.NewNodeNodeListPosition($1, $3) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Ref, $2.SkippedTokens) + $$ = &ast.StmtTraitUsePrecedence{ + Node: ast.Node{ + Position: position.NewNodeNodeListPosition($1, $3.(*ast.ParserSeparatedList).Items), + }, + Ref: $1, + InsteadofTkn: $2, + Insteadof: $3.(*ast.ParserSeparatedList).Items, + SeparatorTkns: $3.(*ast.ParserSeparatedList).SeparatorTkns, + } } ; diff --git a/pkg/ast/node.go b/pkg/ast/node.go index cb820b0..3c64e3d 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -242,8 +242,8 @@ type StmtClass struct { ClassTkn *token.Token ClassName Vertex ArgumentList Vertex - Extends *StmtClassExtends - Implements *StmtClassImplements + Extends Vertex + Implements Vertex OpenCurlyBracket *token.Token Stmts []Vertex CloseCurlyBracket *token.Token @@ -269,6 +269,7 @@ func (n *StmtClassConstList) Accept(v NodeVisitor) { // StmtClassExtends node type StmtClassExtends struct { Node + ExtendTkn *token.Token ClassName Vertex } @@ -279,7 +280,9 @@ func (n *StmtClassExtends) Accept(v NodeVisitor) { // StmtClassImplements node type StmtClassImplements struct { Node + ImplementsTkn *token.Token InterfaceNames []Vertex + SeparatorTkns []*token.Token } func (n *StmtClassImplements) Accept(v NodeVisitor) { @@ -582,7 +585,7 @@ func (n *StmtInlineHtml) Accept(v NodeVisitor) { type StmtInterface struct { Node InterfaceName Vertex - Extends *StmtInterfaceExtends + Extends Vertex Stmts []Vertex } @@ -593,7 +596,9 @@ func (n *StmtInterface) Accept(v NodeVisitor) { // StmtInterfaceExtends node type StmtInterfaceExtends struct { Node + ExtendsTkn *token.Token InterfaceNames []Vertex + SeparatorTkns []*token.Token } func (n *StmtInterfaceExtends) Accept(v NodeVisitor) { @@ -746,8 +751,8 @@ type StmtTrait struct { Node TraitTkn *token.Token TraitName Vertex - Extends *StmtClassExtends - Implements *StmtClassImplements + Extends Vertex + Implements Vertex OpenCurlyBracket *token.Token Stmts []Vertex CloseCurlyBracket *token.Token @@ -781,8 +786,10 @@ func (n *StmtTraitMethodRef) Accept(v NodeVisitor) { // StmtTraitUse node type StmtTraitUse struct { Node - Traits []Vertex - TraitAdaptationList Vertex + UseTkn *token.Token + Traits []Vertex + SeparatorTkns []*token.Token + Adaptations Vertex } func (n *StmtTraitUse) Accept(v NodeVisitor) { @@ -804,8 +811,10 @@ func (n *StmtTraitUseAlias) Accept(v NodeVisitor) { // StmtTraitUsePrecedence node type StmtTraitUsePrecedence struct { Node - Ref Vertex - Insteadof []Vertex + Ref Vertex + InsteadofTkn *token.Token + Insteadof []Vertex + SeparatorTkns []*token.Token } func (n *StmtTraitUsePrecedence) Accept(v NodeVisitor) { diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index b9189a0..8a6cdec 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -893,10 +893,10 @@ func (t *DFS) Traverse(n ast.Vertex) { } t.visitor.Leave("Traits", false) } - if nn.TraitAdaptationList != nil { - t.visitor.Enter("TraitAdaptationList", true) - t.Traverse(nn.TraitAdaptationList) - t.visitor.Leave("TraitAdaptationList", true) + if nn.Adaptations != nil { + t.visitor.Enter("Adaptations", true) + t.Traverse(nn.Adaptations) + t.visitor.Leave("Adaptations", true) } case *ast.StmtTraitUseAlias: if nn == nil { diff --git a/pkg/ast/visitor/namespace_resolver.go b/pkg/ast/visitor/namespace_resolver.go index 3eac4a1..5bc82ca 100644 --- a/pkg/ast/visitor/namespace_resolver.go +++ b/pkg/ast/visitor/namespace_resolver.go @@ -73,11 +73,11 @@ func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUse) { func (nsr *NamespaceResolver) StmtClass(n *ast.StmtClass) { if n.Extends != nil { - nsr.ResolveName(n.Extends.ClassName, "") + nsr.ResolveName(n.Extends.(*ast.StmtClassExtends).ClassName, "") } if n.Implements != nil { - for _, interfaceName := range n.Implements.InterfaceNames { + for _, interfaceName := range n.Implements.(*ast.StmtClassImplements).InterfaceNames { nsr.ResolveName(interfaceName, "") } } @@ -89,7 +89,7 @@ func (nsr *NamespaceResolver) StmtClass(n *ast.StmtClass) { func (nsr *NamespaceResolver) StmtInterface(n *ast.StmtInterface) { if n.Extends != nil { - for _, interfaceName := range n.Extends.InterfaceNames { + for _, interfaceName := range n.Extends.(*ast.StmtInterfaceExtends).InterfaceNames { nsr.ResolveName(interfaceName, "") } } @@ -184,7 +184,7 @@ func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) { nsr.ResolveName(t, "") } - if adaptationList, ok := n.TraitAdaptationList.(*ast.StmtTraitAdaptationList); ok { + if adaptationList, ok := n.Adaptations.(*ast.StmtTraitAdaptationList); ok { for _, a := range adaptationList.Adaptations { switch aa := a.(type) { case *ast.StmtTraitUsePrecedence: diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index 943cc10..65c4cf6 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -1482,12 +1482,12 @@ func (p *PrettyPrinter) printStmtClass(n ast.Vertex) { if nn.Extends != nil { io.WriteString(p.w, " extends ") - p.Print(nn.Extends.ClassName) + p.Print(nn.Extends.(*ast.StmtClassExtends).ClassName) } if nn.Implements != nil { io.WriteString(p.w, " implements ") - p.joinPrint(", ", nn.Implements.InterfaceNames) + p.joinPrint(", ", nn.Implements.(*ast.StmtClassImplements).InterfaceNames) } io.WriteString(p.w, "\n") @@ -1888,7 +1888,7 @@ func (p *PrettyPrinter) printStmtInterface(n ast.Vertex) { if nn.Extends != nil { io.WriteString(p.w, " extends ") - p.joinPrint(", ", nn.Extends.InterfaceNames) + p.joinPrint(", ", nn.Extends.(*ast.StmtInterfaceExtends).InterfaceNames) } io.WriteString(p.w, "\n") @@ -2073,7 +2073,7 @@ func (p *PrettyPrinter) printStmtTraitUse(n ast.Vertex) { io.WriteString(p.w, "use ") p.joinPrint(", ", nn.Traits) - if adaptationList, ok := nn.TraitAdaptationList.(*ast.StmtTraitAdaptationList); ok { + if adaptationList, ok := nn.Adaptations.(*ast.StmtTraitAdaptationList); ok { adaptations := adaptationList.Adaptations io.WriteString(p.w, " {\n") p.printNodes(adaptations) diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 302773a..5848f39 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -2108,7 +2108,7 @@ func (p *Printer) printStmtClass(n ast.Vertex) { } p.write([]byte("extends")) p.bufStart = " " - p.Print(nn.Extends.ClassName) + p.Print(nn.Extends.(*ast.StmtClassExtends).ClassName) } if nn.Implements != nil { @@ -2118,7 +2118,7 @@ func (p *Printer) printStmtClass(n ast.Vertex) { } p.write([]byte("implements")) p.bufStart = " " - p.joinPrintRefactored(",", nn.Implements.InterfaceNames) + p.joinPrintRefactored(",", nn.Implements.(*ast.StmtClassImplements).InterfaceNames) } @@ -2503,7 +2503,7 @@ func (p *Printer) printStmtInterface(n ast.Vertex) { } p.write([]byte("extends")) p.bufStart = " " - p.joinPrintRefactored(",", nn.Extends.InterfaceNames) + p.joinPrintRefactored(",", nn.Extends.(*ast.StmtInterfaceExtends).InterfaceNames) } p.printFreeFloating(nn, token.Name) @@ -2755,7 +2755,7 @@ func (p *Printer) printStmtTraitUse(n ast.Vertex) { p.bufStart = " " p.joinPrintRefactored(",", nn.Traits) - p.Print(nn.TraitAdaptationList) + p.Print(nn.Adaptations) p.printFreeFloating(nn, token.End) }