refactor php5
This commit is contained in:
parent
6a84d58ee6
commit
7e07f1ca26
@ -1,15 +1,14 @@
|
|||||||
package php5
|
package php5
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/z7zmey/php-parser/freefloating"
|
"bytes"
|
||||||
"github.com/z7zmey/php-parser/node"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/internal/positionbuilder"
|
"github.com/z7zmey/php-parser/internal/positionbuilder"
|
||||||
|
"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"
|
||||||
"github.com/z7zmey/php-parser/pkg/position"
|
"github.com/z7zmey/php-parser/pkg/position"
|
||||||
"github.com/z7zmey/php-parser/scanner"
|
"github.com/z7zmey/php-parser/pkg/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (lval *yySymType) Token(t *scanner.Token) {
|
func (lval *yySymType) Token(t *scanner.Token) {
|
||||||
@ -56,7 +55,7 @@ func (l *Parser) Error(msg string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Parser) WithTokens() {
|
func (l *Parser) WithTokens() {
|
||||||
l.Lexer.SetWithFreeFloating(true)
|
l.Lexer.SetWithTokens(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the php7 Parser entrypoint
|
// Parse the php7 Parser entrypoint
|
||||||
@ -83,14 +82,14 @@ func (l *Parser) GetErrors() []*errors.Error {
|
|||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
|
||||||
func lastNode(nn []node.Node) node.Node {
|
func lastNode(nn []ast.Vertex) ast.Vertex {
|
||||||
if len(nn) == 0 {
|
if len(nn) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return nn[len(nn)-1]
|
return nn[len(nn)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func firstNode(nn []node.Node) node.Node {
|
func firstNode(nn []ast.Vertex) ast.Vertex {
|
||||||
return nn[0]
|
return nn[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,20 +97,20 @@ func isDollar(r rune) bool {
|
|||||||
return r == '$'
|
return r == '$'
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Parser) MoveFreeFloating(src node.Node, dst node.Node) {
|
func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
|
||||||
if l.Lexer.GetWithFreeFloating() == false {
|
if l.Lexer.GetWithFreeFloating() == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if src.GetFreeFloating() == nil {
|
if src.GetNode().Tokens == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l.setFreeFloating(dst, freefloating.Start, (*src.GetFreeFloating())[freefloating.Start])
|
l.setFreeFloating(dst, token.Start, src.GetNode().Tokens[token.Start])
|
||||||
delete((*src.GetFreeFloating()), freefloating.Start)
|
delete(src.GetNode().Tokens, token.Start)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Parser) setFreeFloating(dst node.Node, p freefloating.Position, strings []freefloating.String) {
|
func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []token.Token) {
|
||||||
if l.Lexer.GetWithFreeFloating() == false {
|
if l.Lexer.GetWithFreeFloating() == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -120,100 +119,65 @@ func (l *Parser) setFreeFloating(dst node.Node, p freefloating.Position, strings
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dstCollection := dst.GetFreeFloating()
|
dstCollection := &dst.GetNode().Tokens
|
||||||
if *dstCollection == nil {
|
if *dstCollection == nil {
|
||||||
*dstCollection = make(freefloating.Collection)
|
*dstCollection = make(token.Collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
(*dstCollection)[p] = strings
|
(*dstCollection)[p] = strings
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []freefloating.String {
|
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
|
||||||
if l.Lexer.GetWithFreeFloating() == false {
|
if l.Lexer.GetWithFreeFloating() == false {
|
||||||
return []freefloating.String{}
|
return []token.Token{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.GetFreeFloatingToken()
|
tokens := make([]token.Token, len(t.Tokens))
|
||||||
|
copy(tokens, t.Tokens)
|
||||||
|
|
||||||
|
return tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Parser) addDollarToken(v node.Node) {
|
func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
|
||||||
if l.Lexer.GetWithFreeFloating() == false {
|
if l.Lexer.GetWithFreeFloating() == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l.setFreeFloating(v, freefloating.Dollar, []freefloating.String{
|
semiColon := prevNode.GetNode().Tokens[token.SemiColon]
|
||||||
{
|
delete(prevNode.GetNode().Tokens, token.SemiColon)
|
||||||
StringType: freefloating.TokenType,
|
|
||||||
Value: "$",
|
|
||||||
Position: &position.Position{
|
|
||||||
StartLine: v.GetPosition().StartLine,
|
|
||||||
EndLine: v.GetPosition().StartLine,
|
|
||||||
StartPos: v.GetPosition().StartPos,
|
|
||||||
EndPos: v.GetPosition().StartPos + 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode node.Node, prevNode node.Node) {
|
|
||||||
if l.Lexer.GetWithFreeFloating() == false {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
semiColon := (*prevNode.GetFreeFloating())[freefloating.SemiColon]
|
|
||||||
delete((*prevNode.GetFreeFloating()), freefloating.SemiColon)
|
|
||||||
if len(semiColon) == 0 {
|
if len(semiColon) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p := semiColon[0].Position
|
|
||||||
if semiColon[0].Value[0] == ';' {
|
if semiColon[0].Value[0] == ';' {
|
||||||
l.setFreeFloating(prevNode, freefloating.SemiColon, []freefloating.String{
|
l.setFreeFloating(prevNode, token.SemiColon, []token.Token{
|
||||||
{
|
{
|
||||||
StringType: freefloating.TokenType,
|
ID: token.ID(';'),
|
||||||
Value: ";",
|
Value: semiColon[0].Value[0:1],
|
||||||
Position: &position.Position{
|
|
||||||
StartLine: p.StartLine,
|
|
||||||
EndLine: p.StartLine,
|
|
||||||
StartPos: p.StartPos,
|
|
||||||
EndPos: p.StartPos + 1,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
vlen := len(semiColon[0].Value)
|
vlen := len(semiColon[0].Value)
|
||||||
tlen := 2
|
tlen := 2
|
||||||
if strings.HasSuffix(semiColon[0].Value, "?>\n") {
|
if bytes.HasSuffix(semiColon[0].Value, []byte("?>\n")) {
|
||||||
tlen = 3
|
tlen = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
phpCloseTag := []freefloating.String{}
|
phpCloseTag := []token.Token{}
|
||||||
if vlen-tlen > 1 {
|
if vlen-tlen > 1 {
|
||||||
phpCloseTag = append(phpCloseTag, freefloating.String{
|
phpCloseTag = append(phpCloseTag, token.Token{
|
||||||
StringType: freefloating.WhiteSpaceType,
|
ID: token.T_WHITESPACE,
|
||||||
Value: semiColon[0].Value[1 : vlen-tlen],
|
Value: semiColon[0].Value[1 : vlen-tlen],
|
||||||
Position: &position.Position{
|
|
||||||
StartLine: p.StartLine,
|
|
||||||
EndLine: p.EndLine,
|
|
||||||
StartPos: p.StartPos + 1,
|
|
||||||
EndPos: p.EndPos - tlen,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
phpCloseTag = append(phpCloseTag, freefloating.String{
|
phpCloseTag = append(phpCloseTag, token.Token{
|
||||||
StringType: freefloating.WhiteSpaceType,
|
ID: T_CLOSE_TAG,
|
||||||
Value: semiColon[0].Value[vlen-tlen:],
|
Value: semiColon[0].Value[vlen-tlen:],
|
||||||
Position: &position.Position{
|
|
||||||
StartLine: p.EndLine,
|
|
||||||
EndLine: p.EndLine,
|
|
||||||
StartPos: p.EndPos - tlen,
|
|
||||||
EndPos: p.EndPos,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
l.setFreeFloating(htmlNode, freefloating.Start, append(phpCloseTag, (*htmlNode.GetFreeFloating())[freefloating.Start]...))
|
l.setFreeFloating(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
|
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
|
||||||
|
Binary file not shown.
4207
internal/php5/php5.y
4207
internal/php5/php5.y
File diff suppressed because it is too large
Load Diff
@ -3,7 +3,7 @@ package php5_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/z7zmey/php-parser/php5"
|
"github.com/z7zmey/php-parser/internal/php5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkPhp5(b *testing.B) {
|
func BenchmarkPhp5(b *testing.B) {
|
||||||
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Loading…
Reference in New Issue
Block a user