rename meta to freefloating; refactoring

This commit is contained in:
z7zmey
2019-02-13 22:18:07 +02:00
parent a7082117d9
commit b3800a2595
309 changed files with 9671 additions and 10115 deletions

View File

@@ -4,8 +4,9 @@ import (
"io"
"strings"
"github.com/z7zmey/php-parser/freefloating"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/position"
@@ -72,16 +73,6 @@ func (l *Parser) Parse() int {
return yyParse(l)
}
func (l *Parser) listGetFirstNodeMeta(list []node.Node) *meta.Collection {
if len(list) == 0 {
return nil
}
node := list[0]
return node.GetMeta()
}
// GetPath return path to file
func (l *Parser) GetPath() string {
return l.path
@@ -114,121 +105,122 @@ func isDollar(r rune) bool {
return r == '$'
}
func newInheritMetaFilter() meta.Filter {
return meta.StopOnFailureFilter(
meta.AndFilter(
meta.TokenNameFilter(meta.NodeStart),
meta.OrFilter(
meta.TypeFilter(meta.CommentType, meta.WhiteSpaceType),
meta.ValueFilter("<?php", "<?"),
),
),
)
}
func (l *Parser) appendMetaToken(n node.Node, t *scanner.Token, tn meta.TokenName) {
if !l.Lexer.WithMeta {
func (l *Parser) MoveFreeFloating(src node.Node, dst node.Node) {
if l.Lexer.WithMeta == false {
return
}
m := &meta.Data{
Value: t.Value,
Type: meta.TokenType,
Position: l.positionBuilder.NewTokenPosition(t),
TokenName: tn,
}
n.GetMeta().Push(m)
}
func (l *Parser) appendMeta(n node.Node, m *meta.Data, tn meta.TokenName) {
if !l.Lexer.WithMeta {
if src.GetFreeFloating() == nil {
return
}
n.GetMeta().Push(m)
l.setFreeFloating(dst, freefloating.Start, (*src.GetFreeFloating())[freefloating.Start])
delete((*src.GetFreeFloating()), freefloating.Start)
}
func (l *Parser) prependMetaToken(n node.Node, t *scanner.Token, tn meta.TokenName) {
if !l.Lexer.WithMeta {
func (l *Parser) setFreeFloating(dst node.Node, p freefloating.Position, strings []freefloating.String) {
if l.Lexer.WithMeta == false {
return
}
m := &meta.Data{
Value: t.Value,
Type: meta.TokenType,
Position: l.positionBuilder.NewTokenPosition(t),
TokenName: tn,
}
n.GetMeta().Unshift(m)
}
func (l *Parser) splitSemicolonTokenAndPhpCloseTag(htmlNode node.Node, prevNode node.Node) {
SemiColonTokenMeta := prevNode.GetMeta().Cut(meta.AndFilter(
meta.TokenNameFilter(meta.SemiColonToken),
meta.TypeFilter(meta.TokenType),
))
if len(*SemiColonTokenMeta) < 1 {
if len(strings) == 0 {
return
}
metaTokenValue := (*SemiColonTokenMeta)[0].Value
i := strings.Index(metaTokenValue, "?>")
if i < 0 {
SemiColonTokenMeta.AppendTo(prevNode.GetMeta())
} else {
if metaTokenValue[0] == ';' {
prevNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[0:1],
Type: meta.TokenType,
Position: nil,
TokenName: meta.SemiColonToken,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[1:i],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i : i+2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > i+2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[i+2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
} else {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[:2],
Type: meta.TokenType,
Position: nil,
TokenName: meta.NodeStart,
})
if len(metaTokenValue) > 2 {
htmlNode.GetMeta().Push(&meta.Data{
Value: metaTokenValue[2:],
Type: meta.WhiteSpaceType,
Position: nil,
TokenName: meta.NodeStart,
})
}
}
dstCollection := dst.GetFreeFloating()
if *dstCollection == nil {
*dstCollection = make(freefloating.Collection)
}
(*dstCollection)[p] = strings
}
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []freefloating.String {
if l.Lexer.WithMeta == false {
return []freefloating.String{}
}
return t.GetFreeFloatingToken()
}
func (l *Parser) addDollarToken(v node.Node) {
if l.Lexer.WithMeta == false {
return
}
l.setFreeFloating(v, freefloating.Dollar, []freefloating.String{
{
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.WithMeta == false {
return
}
semiColon := (*prevNode.GetFreeFloating())[freefloating.SemiColon]
delete((*prevNode.GetFreeFloating()), freefloating.SemiColon)
if len(semiColon) == 0 {
return
}
p := semiColon[0].Position
if semiColon[0].Value[0] == ';' {
l.setFreeFloating(prevNode, freefloating.SemiColon, []freefloating.String{
{
StringType: freefloating.TokenType,
Value: ";",
Position: &position.Position{
StartLine: p.StartLine,
EndLine: p.StartLine,
StartPos: p.StartPos,
EndPos: p.StartPos + 1,
},
},
})
}
vlen := len(semiColon[0].Value)
tlen := 2
if strings.HasSuffix(semiColon[0].Value, "?>\n") {
tlen = 3
}
phpCloseTag := []freefloating.String{}
if vlen-tlen > 1 {
phpCloseTag = append(phpCloseTag, freefloating.String{
StringType: freefloating.WhiteSpaceType,
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{
StringType: freefloating.WhiteSpaceType,
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]...))
}
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -2,37 +2,23 @@ package php5_test
import (
"bytes"
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/assign"
"github.com/z7zmey/php-parser/node/expr/binary"
"github.com/z7zmey/php-parser/node/expr/cast"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("expected and actual are not equal\n")
}
}
}
func TestPhp5(t *testing.T) {
src := `<?
foo($a, ...$b);
@@ -10872,6 +10858,12 @@ func TestPhp5(t *testing.T) {
Parts: []node.Node{
&scalar.EncapsedStringPart{
Value: "cmd",
Position: &position.Position{
StartLine: 229,
EndLine: 229,
StartPos: 4640,
EndPos: 4642,
},
},
},
},
@@ -18411,7 +18403,7 @@ func TestPhp5(t *testing.T) {
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual := php5parser.GetRootNode()
assertEqual(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}
func TestPhp5Strings(t *testing.T) {
@@ -18526,7 +18518,7 @@ func TestPhp5Strings(t *testing.T) {
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual := php5parser.GetRootNode()
assertEqual(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}
func TestPhp5Heredoc(t *testing.T) {
@@ -18707,7 +18699,7 @@ CAD;
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual := php5parser.GetRootNode()
assertEqual(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}
func TestPhp5ControlCharsErrors(t *testing.T) {
@@ -18727,5 +18719,5 @@ func TestPhp5ControlCharsErrors(t *testing.T) {
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual := php5parser.GetErrors()
assertEqual(t, expected, actual)
assert.DeepEqual(t, expected, actual)
}