[refactoring] remove *FreeFloating* functions

This commit is contained in:
Vadym Slizov 2020-12-04 13:10:12 +02:00
parent af968a7c3b
commit 94897d8c66
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
6 changed files with 0 additions and 266 deletions

View File

@ -1,8 +1,6 @@
package php5 package php5
import ( import (
"bytes"
"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/errors" "github.com/z7zmey/php-parser/pkg/errors"
@ -62,110 +60,3 @@ func lastNode(nn []ast.Vertex) ast.Vertex {
} }
return nn[len(nn)-1] return nn[len(nn)-1]
} }
func (p *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if _, ok := src.GetNode().Tokens[token.Start]; !ok {
return
}
if src.GetNode().Tokens == nil {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
(*dstCollection)[token.Start] = src.GetNode().Tokens[token.Start]
delete(src.GetNode().Tokens, token.Start)
}
func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
l := len(tokens)
for _, v := range tokens[0 : l-1] {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
func (p *Parser) setFreeFloatingTokens(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
(*dstCollection)[pos] = make([]*token.Token, 0)
for _, v := range tokens {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
func (p *Parser) setToken(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
l := len(tokens)
(*dstCollection)[pos] = append((*dstCollection)[pos], tokens[l-1])
}
func (p *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if _, ok := prevNode.GetNode().Tokens[token.SemiColon]; !ok {
return
}
semiColon := prevNode.GetNode().Tokens[token.SemiColon]
delete(prevNode.GetNode().Tokens, token.SemiColon)
if len(semiColon) == 0 {
return
}
if semiColon[0].Value[0] == ';' {
p.setFreeFloatingTokens(prevNode, token.SemiColon, []*token.Token{
{
ID: token.ID(';'),
Value: semiColon[0].Value[0:1],
},
})
}
vlen := len(semiColon[0].Value)
tlen := 2
if bytes.HasSuffix(semiColon[0].Value, []byte("?>\n")) {
tlen = 3
}
phpCloseTag := []*token.Token{}
if vlen-tlen > 1 {
phpCloseTag = append(phpCloseTag, &token.Token{
ID: token.T_WHITESPACE,
Value: semiColon[0].Value[1 : vlen-tlen],
})
}
phpCloseTag = append(phpCloseTag, &token.Token{
ID: T_CLOSE_TAG,
Value: semiColon[0].Value[vlen-tlen:],
})
p.setFreeFloatingTokens(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
}

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -280,11 +280,6 @@ start:
top_statement_list: top_statement_list:
top_statement_list top_statement top_statement_list top_statement
{ {
if inlineHtmlNode, ok := $2.(*ast.StmtInlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemiColonAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil { if $2 != nil {
$$ = append($1, $2) $$ = append($1, $2)
} }
@ -744,8 +739,6 @@ constant_declaration:
}) })
$$ = $1 $$ = $1
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtConstList).Consts).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens)
} }
| T_CONST T_STRING '=' static_scalar | T_CONST T_STRING '=' static_scalar
{ {
@ -771,19 +764,12 @@ constant_declaration:
}, },
}, },
} }
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtConstList).Consts).(*ast.StmtConstant).Name, token.Start, $2.SkippedTokens)
} }
; ;
inner_statement_list: inner_statement_list:
inner_statement_list inner_statement inner_statement_list inner_statement
{ {
if inlineHtmlNode, ok := $2.(*ast.StmtInlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemiColonAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil { if $2 != nil {
$$ = append($1, $2) $$ = append($1, $2)
} }
@ -1752,8 +1738,6 @@ declare_list:
}, },
}, },
} }
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.ParserSeparatedList).Items).(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens)
} }
| declare_list ',' T_STRING '=' static_scalar | declare_list ',' T_STRING '=' static_scalar
{ {
@ -1777,8 +1761,6 @@ declare_list:
) )
$$ = $1 $$ = $1
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.ParserSeparatedList).Items).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens)
} }
; ;
@ -2923,8 +2905,6 @@ class_constant_declaration:
}) })
$$ = $1 $$ = $1
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtClassConstList).Consts).(*ast.StmtConstant).Name, token.Start, $3.SkippedTokens)
} }
| T_CONST T_STRING '=' static_scalar | T_CONST T_STRING '=' static_scalar
{ {
@ -2950,8 +2930,6 @@ class_constant_declaration:
}, },
}, },
} }
yylex.(*Parser).setFreeFloating(lastNode($$.(*ast.StmtClassConstList).Consts).(*ast.StmtConstant).Name, token.Start, $2.SkippedTokens)
} }
; ;
@ -3624,7 +3602,6 @@ expr_without_variable:
OpTkn: $2, OpTkn: $2,
Right: $3, Right: $3,
} }
yylex.(*Parser).setToken($$, token.Equal, $2.SkippedTokens)
} }
| expr '<' expr | expr '<' expr
{ {
@ -4416,13 +4393,11 @@ dynamic_class_name_reference:
nn.Var = $$ nn.Var = $$
$$.GetNode().Position = position.NewNodesPosition($$, nn) $$.GetNode().Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprPropertyFetch: case *ast.ExprPropertyFetch:
nn.Var = $$ nn.Var = $$
$$.GetNode().Position = position.NewNodesPosition($$, nn) $$.GetNode().Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
} }
} }
@ -4432,13 +4407,11 @@ dynamic_class_name_reference:
nn.Var = $$ nn.Var = $$
$$.GetNode().Position = position.NewNodesPosition($$, nn) $$.GetNode().Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
case *ast.ExprPropertyFetch: case *ast.ExprPropertyFetch:
nn.Var = $$ nn.Var = $$
$$.GetNode().Position = position.NewNodesPosition($$, nn) $$.GetNode().Position = position.NewNodesPosition($$, nn)
$$ = nn $$ = nn
yylex.(*Parser).MoveFreeFloating(nn.Var, $$)
} }
} }
} }
@ -5046,7 +5019,6 @@ static_operation:
OpTkn: $2, OpTkn: $2,
Right: $3, Right: $3,
} }
yylex.(*Parser).setToken($$, token.Equal, $2.SkippedTokens)
} }
| static_scalar_value '<' static_scalar_value | static_scalar_value '<' static_scalar_value
{ {
@ -6333,12 +6305,6 @@ encaps_var_offset:
Value: $1.Value, Value: $1.Value,
} }
} }
// save position
$$.GetNode().Position = position.NewTokenPosition($1)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
} }
| T_VARIABLE | T_VARIABLE
{ {

View File

@ -1,8 +1,6 @@
package php7 package php7
import ( import (
"bytes"
"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/errors" "github.com/z7zmey/php-parser/pkg/errors"
@ -62,110 +60,3 @@ func lastNode(nn []ast.Vertex) ast.Vertex {
} }
return nn[len(nn)-1] return nn[len(nn)-1]
} }
func (p *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if _, ok := src.GetNode().Tokens[token.Start]; !ok {
return
}
if src.GetNode().Tokens == nil {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
(*dstCollection)[token.Start] = src.GetNode().Tokens[token.Start]
delete(src.GetNode().Tokens, token.Start)
}
func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
l := len(tokens)
for _, v := range tokens[0 : l-1] {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
func (p *Parser) setFreeFloatingTokens(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
(*dstCollection)[pos] = make([]*token.Token, 0)
for _, v := range tokens {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
func (p *Parser) setToken(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
l := len(tokens)
(*dstCollection)[pos] = append((*dstCollection)[pos], tokens[l-1])
}
func (p *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if _, ok := prevNode.GetNode().Tokens[token.SemiColon]; !ok {
return
}
semiColon := prevNode.GetNode().Tokens[token.SemiColon]
delete(prevNode.GetNode().Tokens, token.SemiColon)
if len(semiColon) == 0 {
return
}
if semiColon[0].Value[0] == ';' {
p.setFreeFloatingTokens(prevNode, token.SemiColon, []*token.Token{
{
ID: token.ID(';'),
Value: semiColon[0].Value[0:1],
},
})
}
vlen := len(semiColon[0].Value)
tlen := 2
if bytes.HasSuffix(semiColon[0].Value, []byte("?>\n")) {
tlen = 3
}
phpCloseTag := []*token.Token{}
if vlen-tlen > 1 {
phpCloseTag = append(phpCloseTag, &token.Token{
ID: token.T_WHITESPACE,
Value: semiColon[0].Value[1 : vlen-tlen],
})
}
phpCloseTag = append(phpCloseTag, &token.Token{
ID: T_CLOSE_TAG,
Value: semiColon[0].Value[vlen-tlen:],
})
p.setFreeFloatingTokens(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
}

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -329,11 +329,6 @@ identifier:
top_statement_list: top_statement_list:
top_statement_list top_statement top_statement_list top_statement
{ {
if inlineHtmlNode, ok := $2.(*ast.StmtInlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemiColonAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil { if $2 != nil {
$$ = append($1, $2) $$ = append($1, $2)
} }
@ -796,11 +791,6 @@ const_list:
inner_statement_list: inner_statement_list:
inner_statement_list inner_statement inner_statement_list inner_statement
{ {
if inlineHtmlNode, ok := $2.(*ast.StmtInlineHtml); ok && len($1) > 0 {
prevNode := lastNode($1)
yylex.(*Parser).splitSemiColonAndPhpCloseTag(inlineHtmlNode, prevNode)
}
if $2 != nil { if $2 != nil {
$$ = append($1, $2) $$ = append($1, $2)
} }
@ -2633,8 +2623,6 @@ class_const_decl:
EqualTkn: $2, EqualTkn: $2,
Expr: $3, Expr: $3,
} }
yylex.(*Parser).setFreeFloating($$.(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens)
} }
; ;
@ -2655,8 +2643,6 @@ const_decl:
EqualTkn: $2, EqualTkn: $2,
Expr: $3, Expr: $3,
} }
yylex.(*Parser).setFreeFloating($$.(*ast.StmtConstant).Name, token.Start, $1.SkippedTokens)
} }
; ;