[refactoring] internal position package

This commit is contained in:
Vadym Slizov 2020-06-29 14:38:10 +03:00
parent ee673e5667
commit 7eff83624e
9 changed files with 1079 additions and 1122 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"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/errors"
@ -13,12 +12,11 @@ import (
// Parser structure
type Parser struct {
Lexer *scanner.Lexer
currentToken *scanner.Token
positionBuilder *positionbuilder.PositionBuilder
rootNode ast.Vertex
errors []*errors.Error
withTokens bool
Lexer *scanner.Lexer
currentToken *scanner.Token
rootNode ast.Vertex
errors []*errors.Error
withTokens bool
}
// NewParser creates and returns new Parser
@ -66,7 +64,6 @@ func (p *Parser) Parse() int {
// init
p.errors = nil
p.rootNode = nil
p.positionBuilder = &positionbuilder.PositionBuilder{}
// parse

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,6 @@ package php7
import (
"bytes"
"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/errors"
@ -12,12 +11,11 @@ import (
// Parser structure
type Parser struct {
Lexer *scanner.Lexer
currentToken *scanner.Token
positionBuilder *positionbuilder.PositionBuilder
rootNode ast.Vertex
errors []*errors.Error
withTokens bool
Lexer *scanner.Lexer
currentToken *scanner.Token
rootNode ast.Vertex
errors []*errors.Error
withTokens bool
}
// NewParser creates and returns new Parser
@ -64,7 +62,6 @@ func (l *Parser) Parse() int {
// init
l.errors = nil
l.rootNode = nil
l.positionBuilder = &positionbuilder.PositionBuilder{}
// parse

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,204 @@
package position
import (
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/position"
)
type startPos struct {
startLine int
startPos int
}
type endPos struct {
endLine int
endPos int
}
func getListStartPos(l []ast.Vertex) startPos {
if l == nil {
return startPos{-1, -1}
}
if len(l) == 0 {
return startPos{-1, -1}
}
return getNodeStartPos(l[0])
}
func getNodeStartPos(n ast.Vertex) startPos {
sl := -1
sp := -1
if n == nil {
return startPos{-1, -1}
}
p := n.GetNode().Position
if p != nil {
sl = p.StartLine
sp = p.StartPos
}
return startPos{sl, sp}
}
func getListEndPos(l []ast.Vertex) endPos {
if l == nil {
return endPos{-1, -1}
}
if len(l) == 0 {
return endPos{-1, -1}
}
return getNodeEndPos(l[len(l)-1])
}
func getNodeEndPos(n ast.Vertex) endPos {
el := -1
ep := -1
if n == nil {
return endPos{-1, -1}
}
p := n.GetNode().Position
if p != nil {
el = p.EndLine
ep = p.EndPos
}
return endPos{el, ep}
}
// NewNodeListPosition returns new Position
func NewNodeListPosition(list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: getListStartPos(list).startLine,
EndLine: getListEndPos(list).endLine,
StartPos: getListStartPos(list).startPos,
EndPos: getListEndPos(list).endPos,
}
}
// NewNodePosition returns new Position
func NewNodePosition(n ast.Vertex) *position.Position {
return &position.Position{
StartLine: getNodeStartPos(n).startLine,
EndLine: getNodeEndPos(n).endLine,
StartPos: getNodeStartPos(n).startPos,
EndPos: getNodeEndPos(n).endPos,
}
}
// NewTokenPosition returns new Position
func NewTokenPosition(t *scanner.Token) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: t.Position.EndLine,
StartPos: t.Position.StartPos,
EndPos: t.Position.EndPos,
}
}
// NewTokensPosition returns new Position
func NewTokensPosition(startToken *scanner.Token, endToken *scanner.Token) *position.Position {
return &position.Position{
StartLine: startToken.Position.StartLine,
EndLine: endToken.Position.EndLine,
StartPos: startToken.Position.StartPos,
EndPos: endToken.Position.EndPos,
}
}
// NewTokenNodePosition returns new Position
func NewTokenNodePosition(t *scanner.Token, n ast.Vertex) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: getNodeEndPos(n).endLine,
StartPos: t.Position.StartPos,
EndPos: getNodeEndPos(n).endPos,
}
}
// NewNodeTokenPosition returns new Position
func NewNodeTokenPosition(n ast.Vertex, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: getNodeStartPos(n).startLine,
EndLine: t.Position.EndLine,
StartPos: getNodeStartPos(n).startPos,
EndPos: t.Position.EndPos,
}
}
// NewNodesPosition returns new Position
func NewNodesPosition(startNode ast.Vertex, endNode ast.Vertex) *position.Position {
return &position.Position{
StartLine: getNodeStartPos(startNode).startLine,
EndLine: getNodeEndPos(endNode).endLine,
StartPos: getNodeStartPos(startNode).startPos,
EndPos: getNodeEndPos(endNode).endPos,
}
}
// NewNodeListTokenPosition returns new Position
func NewNodeListTokenPosition(list []ast.Vertex, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: getListStartPos(list).startLine,
EndLine: t.Position.EndLine,
StartPos: getListStartPos(list).startPos,
EndPos: t.Position.EndPos,
}
}
// NewTokenNodeListPosition returns new Position
func NewTokenNodeListPosition(t *scanner.Token, list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: getListEndPos(list).endLine,
StartPos: t.Position.StartPos,
EndPos: getListEndPos(list).endPos,
}
}
// NewNodeNodeListPosition returns new Position
func NewNodeNodeListPosition(n ast.Vertex, list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: getNodeStartPos(n).startLine,
EndLine: getListEndPos(list).endLine,
StartPos: getNodeStartPos(n).startPos,
EndPos: getListEndPos(list).endPos,
}
}
// NewNodeListNodePosition returns new Position
func NewNodeListNodePosition(list []ast.Vertex, n ast.Vertex) *position.Position {
return &position.Position{
StartLine: getListStartPos(list).startLine,
EndLine: getNodeEndPos(n).endLine,
StartPos: getListStartPos(list).startPos,
EndPos: getNodeEndPos(n).endPos,
}
}
// NewOptionalListTokensPosition returns new Position
func NewOptionalListTokensPosition(list []ast.Vertex, t *scanner.Token, endToken *scanner.Token) *position.Position {
if list == nil {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: endToken.Position.EndLine,
StartPos: t.Position.StartPos,
EndPos: endToken.Position.EndPos,
}
}
return &position.Position{
StartLine: getListStartPos(list).startLine,
EndLine: endToken.Position.EndLine,
StartPos: getListStartPos(list).startPos,
EndPos: endToken.Position.EndPos,
}
}

View File

@ -1,18 +1,16 @@
package positionbuilder_test
package position_test
import (
"gotest.tools/assert"
"testing"
"github.com/z7zmey/php-parser/internal/positionbuilder"
builder "github.com/z7zmey/php-parser/internal/position"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/position"
)
func TestNewTokenPosition(t *testing.T) {
builder := positionbuilder.PositionBuilder{}
tkn := &scanner.Token{
Value: []byte(`foo`),
Position: position.Position{
@ -31,8 +29,6 @@ func TestNewTokenPosition(t *testing.T) {
}
func TestNewTokensPosition(t *testing.T) {
builder := positionbuilder.PositionBuilder{}
token1 := &scanner.Token{
Value: []byte(`foo`),
Position: position.Position{
@ -69,8 +65,6 @@ func TestNewNodePosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodePosition(n)
assert.DeepEqual(t, &position.Position{1, 1, 0, 3}, pos)
@ -97,8 +91,6 @@ func TestNewTokenNodePosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewTokenNodePosition(tkn, n)
assert.DeepEqual(t, &position.Position{1, 2, 0, 12}, pos)
@ -126,8 +118,6 @@ func TestNewNodeTokenPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeTokenPosition(n, tkn)
assert.DeepEqual(t, &position.Position{1, 2, 0, 12}, pos)
@ -156,8 +146,6 @@ func TestNewNodeListPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeListPosition([]ast.Vertex{n1, n2})
assert.DeepEqual(t, &position.Position{1, 2, 0, 19}, pos)
@ -186,8 +174,6 @@ func TestNewNodesPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodesPosition(n1, n2)
assert.DeepEqual(t, &position.Position{1, 2, 0, 19}, pos)
@ -226,8 +212,6 @@ func TestNewNodeListTokenPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn)
assert.DeepEqual(t, &position.Position{1, 3, 0, 22}, pos)
@ -266,8 +250,6 @@ func TestNewTokenNodeListPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2})
assert.DeepEqual(t, &position.Position{1, 3, 0, 20}, pos)
@ -307,8 +289,6 @@ func TestNewNodeNodeListPosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3})
assert.DeepEqual(t, &position.Position{1, 3, 0, 26}, pos)
@ -346,16 +326,12 @@ func TestNewNodeListNodePosition(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3)
assert.DeepEqual(t, &position.Position{1, 3, 0, 26}, pos)
}
func TestNewOptionalListTokensPosition(t *testing.T) {
builder := positionbuilder.PositionBuilder{}
token1 := &scanner.Token{
Value: []byte(`foo`),
Position: position.Position{
@ -402,8 +378,6 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
token1 := &scanner.Token{
Value: []byte(`foo`),
Position: position.Position{
@ -429,8 +403,6 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
}
func TestNilNodePos(t *testing.T) {
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodesPosition(nil, nil)
assert.DeepEqual(t, &position.Position{-1, -1, -1, -1}, pos)
@ -448,8 +420,6 @@ func TestNilNodeListPos(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeNodeListPosition(n1, nil)
assert.DeepEqual(t, &position.Position{1, -1, 0, -1}, pos)
@ -466,8 +436,6 @@ func TestNilNodeListTokenPos(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeListTokenPosition(nil, token)
assert.DeepEqual(t, &position.Position{-1, 1, -1, 3}, pos)
@ -485,8 +453,6 @@ func TestEmptyNodeListPos(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{})
assert.DeepEqual(t, &position.Position{1, -1, 0, -1}, pos)
@ -503,8 +469,6 @@ func TestEmptyNodeListTokenPos(t *testing.T) {
},
}
builder := positionbuilder.PositionBuilder{}
pos := builder.NewNodeListTokenPosition([]ast.Vertex{}, token)
assert.DeepEqual(t, &position.Position{-1, 1, -1, 3}, pos)

View File

@ -1,207 +0,0 @@
package positionbuilder
import (
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/position"
)
// PositionBuilder provide functions to constuct positions
type PositionBuilder struct{}
type startPos struct {
startLine int
startPos int
}
type endPos struct {
endLine int
endPos int
}
func (b *PositionBuilder) getListStartPos(l []ast.Vertex) startPos {
if l == nil {
return startPos{-1, -1}
}
if len(l) == 0 {
return startPos{-1, -1}
}
return b.getNodeStartPos(l[0])
}
func (b *PositionBuilder) getNodeStartPos(n ast.Vertex) startPos {
sl := -1
sp := -1
if n == nil {
return startPos{-1, -1}
}
p := n.GetNode().Position
if p != nil {
sl = p.StartLine
sp = p.StartPos
}
return startPos{sl, sp}
}
func (b *PositionBuilder) getListEndPos(l []ast.Vertex) endPos {
if l == nil {
return endPos{-1, -1}
}
if len(l) == 0 {
return endPos{-1, -1}
}
return b.getNodeEndPos(l[len(l)-1])
}
func (b *PositionBuilder) getNodeEndPos(n ast.Vertex) endPos {
el := -1
ep := -1
if n == nil {
return endPos{-1, -1}
}
p := n.GetNode().Position
if p != nil {
el = p.EndLine
ep = p.EndPos
}
return endPos{el, ep}
}
// NewNodeListPosition returns new Position
func (b *PositionBuilder) NewNodeListPosition(list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodePosition returns new Position
func (b *PositionBuilder) NewNodePosition(n ast.Vertex) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewTokenPosition returns new Position
func (b *PositionBuilder) NewTokenPosition(t *scanner.Token) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: t.Position.EndLine,
StartPos: t.Position.StartPos,
EndPos: t.Position.EndPos,
}
}
// NewTokensPosition returns new Position
func (b *PositionBuilder) NewTokensPosition(startToken *scanner.Token, endToken *scanner.Token) *position.Position {
return &position.Position{
StartLine: startToken.Position.StartLine,
EndLine: endToken.Position.EndLine,
StartPos: startToken.Position.StartPos,
EndPos: endToken.Position.EndPos,
}
}
// NewTokenNodePosition returns new Position
func (b *PositionBuilder) NewTokenNodePosition(t *scanner.Token, n ast.Vertex) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: t.Position.StartPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewNodeTokenPosition returns new Position
func (b *PositionBuilder) NewNodeTokenPosition(n ast.Vertex, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: t.Position.EndLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: t.Position.EndPos,
}
}
// NewNodesPosition returns new Position
func (b *PositionBuilder) NewNodesPosition(startNode ast.Vertex, endNode ast.Vertex) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(startNode).startLine,
EndLine: b.getNodeEndPos(endNode).endLine,
StartPos: b.getNodeStartPos(startNode).startPos,
EndPos: b.getNodeEndPos(endNode).endPos,
}
}
// NewNodeListTokenPosition returns new Position
func (b *PositionBuilder) NewNodeListTokenPosition(list []ast.Vertex, t *scanner.Token) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: t.Position.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: t.Position.EndPos,
}
}
// NewTokenNodeListPosition returns new Position
func (b *PositionBuilder) NewTokenNodeListPosition(t *scanner.Token, list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: t.Position.StartPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodeNodeListPosition returns new Position
func (b *PositionBuilder) NewNodeNodeListPosition(n ast.Vertex, list []ast.Vertex) *position.Position {
return &position.Position{
StartLine: b.getNodeStartPos(n).startLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: b.getListEndPos(list).endPos,
}
}
// NewNodeListNodePosition returns new Position
func (b *PositionBuilder) NewNodeListNodePosition(list []ast.Vertex, n ast.Vertex) *position.Position {
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: b.getNodeEndPos(n).endPos,
}
}
// NewOptionalListTokensPosition returns new Position
func (b *PositionBuilder) NewOptionalListTokensPosition(list []ast.Vertex, t *scanner.Token, endToken *scanner.Token) *position.Position {
if list == nil {
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: endToken.Position.EndLine,
StartPos: t.Position.StartPos,
EndPos: endToken.Position.EndPos,
}
}
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: endToken.Position.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: endToken.Position.EndPos,
}
}