[refactoring] update printer

This commit is contained in:
Vadym Slizov 2020-12-11 09:54:26 +02:00
parent f3a605aba1
commit 497e7f82ee
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
10 changed files with 2285 additions and 3649 deletions

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -264,6 +264,8 @@ import (
start:
top_statement_list
{
yylex.(*Parser).currentToken.Value = nil
yylex.(*Parser).rootNode = &ast.Root{
Position: yylex.(*Parser).builder.NewNodeListPosition($1),
Stmts: $1,
@ -934,7 +936,6 @@ unticked_statement:
{
$2.(*ast.StmtGlobal).GlobalTkn = $1
$2.(*ast.StmtGlobal).SemiColonTkn = $3
$2.(*ast.StmtGlobal).SeparatorTkns = append($2.(*ast.StmtGlobal).SeparatorTkns, nil)
$2.(*ast.StmtGlobal).Position = yylex.(*Parser).builder.NewTokensPosition($1, $3)
$$ = $2
@ -943,7 +944,6 @@ unticked_statement:
{
$2.(*ast.StmtStatic).StaticTkn = $1
$2.(*ast.StmtStatic).SemiColonTkn = $3
$2.(*ast.StmtStatic).SeparatorTkns = append($2.(*ast.StmtStatic).SeparatorTkns, nil)
$2.(*ast.StmtStatic).Position = yylex.(*Parser).builder.NewTokensPosition($1, $3)
$$ = $2
@ -1069,8 +1069,8 @@ unticked_statement:
GotoTkn: $1,
Label: &ast.Identifier{
Position: yylex.(*Parser).builder.NewTokenPosition($1),
IdentifierTkn: $1,
Value: $1.Value,
IdentifierTkn: $2,
Value: $2.Value,
},
SemiColonTkn: $3,
}
@ -2197,9 +2197,9 @@ trait_adaptations:
{
$$ = &ast.StmtTraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenParenthesisTkn: $1,
Adaptations: $2,
CloseParenthesisTkn: $3,
OpenCurlyBracketTkn: $1,
Adaptations: $2,
CloseCurlyBracketTkn: $3,
}
}
;
@ -2691,7 +2691,7 @@ new_expr:
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
SeparatorTkns: $3.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $3.(*ast.ArgumentList).CloseParenthesisTkn,
}
} else {
$$ = &ast.ExprNew{
@ -2749,7 +2749,7 @@ expr_without_variable:
OpenParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $6.(*ast.ArgumentList).Arguments,
SeparatorTkns: $6.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $6.(*ast.ArgumentList).CloseParenthesisTkn,
}
} else {
_new = &ast.ExprNew{
@ -3620,7 +3620,7 @@ function_call:
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
SeparatorTkns: $2.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $2.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| T_NAMESPACE T_NS_SEPARATOR namespace_name function_call_parameter_list
@ -3637,7 +3637,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| T_NS_SEPARATOR namespace_name function_call_parameter_list
@ -3653,7 +3653,7 @@ function_call:
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
SeparatorTkns: $3.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $3.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
@ -3666,7 +3666,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list
@ -3679,7 +3679,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_name function_call_parameter_list
@ -3692,7 +3692,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM variable_without_objects function_call_parameter_list
@ -3705,7 +3705,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| variable_without_objects function_call_parameter_list
@ -3716,7 +3716,7 @@ function_call:
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
SeparatorTkns: $2.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $2.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
;
@ -4500,7 +4500,7 @@ scalar:
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenQoteTkn: $1,
Parts: $2,
CloseQoteTkn: $1,
CloseQoteTkn: $3,
}
}
| T_START_HEREDOC encaps_list T_END_HEREDOC
@ -4674,7 +4674,7 @@ variable:
OpenParenthesisTkn: mc.OpenParenthesisTkn,
Arguments: mc.Arguments,
SeparatorTkns: mc.SeparatorTkns,
CloseParenthesisTkn: mc.OpenParenthesisTkn,
CloseParenthesisTkn: mc.CloseParenthesisTkn,
},
)
$3 = append($3, $4[1:len($4)]...)

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -281,6 +281,8 @@ import (
start:
top_statement_list
{
yylex.(*Parser).currentToken.Value = nil
yylex.(*Parser).rootNode = &ast.Root{
Position: yylex.(*Parser).builder.NewNodeListPosition($1),
Stmts: $1,
@ -579,7 +581,9 @@ group_use_declaration:
mixed_group_use_declaration:
namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
{
$4.(*ast.ParserSeparatedList).SeparatorTkns = append($4.(*ast.ParserSeparatedList).SeparatorTkns, $5)
if $5 != nil {
$4.(*ast.ParserSeparatedList).SeparatorTkns = append($4.(*ast.ParserSeparatedList).SeparatorTkns, $5)
}
$$ = &ast.StmtGroupUse{
Position: yylex.(*Parser).builder.NewNodeListTokenPosition($1.(*ast.ParserSeparatedList).Items, $6),
@ -597,7 +601,9 @@ mixed_group_use_declaration:
}
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
{
$5.(*ast.ParserSeparatedList).SeparatorTkns = append($5.(*ast.ParserSeparatedList).SeparatorTkns, $6)
if $6 != nil {
$5.(*ast.ParserSeparatedList).SeparatorTkns = append($5.(*ast.ParserSeparatedList).SeparatorTkns, $6)
}
$$ = &ast.StmtGroupUse{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $7),
@ -941,7 +947,9 @@ statement:
{
$3.(*ast.StmtUnset).UnsetTkn = $1
$3.(*ast.StmtUnset).OpenParenthesisTkn = $2
$3.(*ast.StmtUnset).SeparatorTkns = append($3.(*ast.StmtUnset).SeparatorTkns, $4)
if $4 != nil {
$3.(*ast.StmtUnset).SeparatorTkns = append($3.(*ast.StmtUnset).SeparatorTkns, $4)
}
$3.(*ast.StmtUnset).CloseParenthesisTkn = $5
$3.(*ast.StmtUnset).SemiColonTkn = $6
$3.(*ast.StmtUnset).Position = yylex.(*Parser).builder.NewTokensPosition($1, $6)
@ -1815,7 +1823,9 @@ argument_list:
argumentList := $2.(*ast.ArgumentList)
argumentList.Position = yylex.(*Parser).builder.NewTokensPosition($1, $4)
argumentList.OpenParenthesisTkn = $1
argumentList.SeparatorTkns = append(argumentList.SeparatorTkns, $3)
if $3 != nil {
argumentList.SeparatorTkns = append(argumentList.SeparatorTkns, $3)
}
argumentList.CloseParenthesisTkn = $4
$$ = argumentList
@ -2029,17 +2039,17 @@ trait_adaptations:
{
$$ = &ast.StmtTraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $2),
OpenParenthesisTkn: $1,
CloseParenthesisTkn: $2,
OpenCurlyBracketTkn: $1,
CloseCurlyBracketTkn: $2,
}
}
| '{' trait_adaptation_list '}'
{
$$ = &ast.StmtTraitAdaptationList{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenParenthesisTkn: $1,
Adaptations: $2,
CloseParenthesisTkn: $3,
OpenCurlyBracketTkn: $1,
Adaptations: $2,
CloseCurlyBracketTkn: $3,
}
}
;
@ -2435,7 +2445,7 @@ anonymous_class:
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
SeparatorTkns: $2.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $2.(*ast.ArgumentList).CloseParenthesisTkn,
Extends: $3,
Implements: $4,
OpenCurlyBracket: $6,
@ -2456,7 +2466,7 @@ new_expr:
OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $3.(*ast.ArgumentList).Arguments,
SeparatorTkns: $3.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $3.(*ast.ArgumentList).CloseParenthesisTkn,
}
} else {
$$ = &ast.ExprNew{
@ -3284,7 +3294,7 @@ function_call:
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
SeparatorTkns: $2.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $2.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
@ -3297,7 +3307,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
@ -3310,7 +3320,7 @@ function_call:
OpenParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $4.(*ast.ArgumentList).Arguments,
SeparatorTkns: $4.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $4.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $4.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
| callable_expr argument_list
@ -3321,7 +3331,7 @@ function_call:
OpenParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
Arguments: $2.(*ast.ArgumentList).Arguments,
SeparatorTkns: $2.(*ast.ArgumentList).SeparatorTkns,
CloseParenthesisTkn: $2.(*ast.ArgumentList).OpenParenthesisTkn,
CloseParenthesisTkn: $2.(*ast.ArgumentList).CloseParenthesisTkn,
}
}
;
@ -3542,7 +3552,7 @@ scalar:
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
OpenQoteTkn: $1,
Parts: $2,
CloseQoteTkn: $1,
CloseQoteTkn: $3,
}
}
| T_START_HEREDOC encaps_list T_END_HEREDOC

View File

@ -954,10 +954,10 @@ func (n *StmtTrait) GetPosition() *position.Position {
// StmtTraitAdaptationList node
type StmtTraitAdaptationList struct {
Position *position.Position
OpenParenthesisTkn *token.Token
Adaptations []Vertex
CloseParenthesisTkn *token.Token
Position *position.Position
OpenCurlyBracketTkn *token.Token
Adaptations []Vertex
CloseCurlyBracketTkn *token.Token
}
func (n *StmtTraitAdaptationList) Accept(v NodeVisitor) {

1239
pkg/ast/visitor/printer.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,14 @@
package printer_test
package visitor_test
import (
"bytes"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
"testing"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/internal/php5"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/printer"
)
func parsePhp5(src string) ast.Vertex {
@ -22,8 +22,8 @@ func parsePhp5(src string) ast.Vertex {
func printPhp5(n ast.Vertex) string {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(n)
printer := visitor.NewPrinter(o)
n.Accept(printer)
return o.String()
}

View File

@ -1,15 +1,14 @@
package printer_test
package visitor_test
import (
"bytes"
"os"
"testing"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/internal/php7"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/printer"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
)
func ExamplePrinter() {
@ -42,8 +41,8 @@ abstract class Bar extends Baz
// print
p := printer.NewPrinter(os.Stdout)
p.Print(rootNode)
printer := visitor.NewPrinter(os.Stdout)
rootNode.Accept(printer)
// Output:
//<?php
@ -71,8 +70,8 @@ func parse(src string) ast.Vertex {
func print(n ast.Vertex) string {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(n)
printer := visitor.NewPrinter(o)
n.Accept(printer)
return o.String()
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff