rename PositionBuilder

This commit is contained in:
z7zmey 2018-04-15 22:06:02 +03:00
parent 56127a4a2c
commit bc15825663
4 changed files with 62 additions and 62 deletions

View File

@ -6,8 +6,8 @@ import (
"github.com/z7zmey/php-parser/scanner" "github.com/z7zmey/php-parser/scanner"
) )
// Builder provide functions to constuct positions // PositionBuilder provide functions to constuct positions
type Builder struct { type PositionBuilder struct {
Positions *Positions Positions *Positions
} }
@ -21,7 +21,7 @@ type endPos struct {
endPos int endPos int
} }
func (b *Builder) getListStartPos(l []node.Node) startPos { func (b *PositionBuilder) getListStartPos(l []node.Node) startPos {
if l == nil { if l == nil {
return startPos{-1, -1} return startPos{-1, -1}
} }
@ -33,7 +33,7 @@ func (b *Builder) getListStartPos(l []node.Node) startPos {
return b.getNodeStartPos(l[0]) return b.getNodeStartPos(l[0])
} }
func (b *Builder) getNodeStartPos(n node.Node) startPos { func (b *PositionBuilder) getNodeStartPos(n node.Node) startPos {
sl := -1 sl := -1
sp := -1 sp := -1
@ -50,7 +50,7 @@ func (b *Builder) getNodeStartPos(n node.Node) startPos {
return startPos{sl, sp} return startPos{sl, sp}
} }
func (b *Builder) getListEndPos(l []node.Node) endPos { func (b *PositionBuilder) getListEndPos(l []node.Node) endPos {
if l == nil { if l == nil {
return endPos{-1, -1} return endPos{-1, -1}
} }
@ -62,7 +62,7 @@ func (b *Builder) getListEndPos(l []node.Node) endPos {
return b.getNodeEndPos(l[len(l)-1]) return b.getNodeEndPos(l[len(l)-1])
} }
func (b *Builder) getNodeEndPos(n node.Node) endPos { func (b *PositionBuilder) getNodeEndPos(n node.Node) endPos {
el := -1 el := -1
ep := -1 ep := -1
@ -80,120 +80,120 @@ func (b *Builder) getNodeEndPos(n node.Node) endPos {
} }
// NewNodeListPosition returns new Position // NewNodeListPosition returns new Position
func (b *Builder) NewNodeListPosition(list []node.Node) *position.Position { func (b *PositionBuilder) NewNodeListPosition(list []node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
b.getListStartPos(list).startLine, b.getListStartPos(list).startLine,
b.getListEndPos(list).endLine, b.getListEndPos(list).endLine,
b.getListStartPos(list).startPos, b.getListStartPos(list).startPos,
b.getListEndPos(list).endPos, b.getListEndPos(list).endPos,
} )
} }
// NewNodePosition returns new Position // NewNodePosition returns new Position
func (b *Builder) NewNodePosition(n node.Node) *position.Position { func (b *PositionBuilder) NewNodePosition(n node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
b.getNodeStartPos(n).startLine, b.getNodeStartPos(n).startLine,
b.getNodeEndPos(n).endLine, b.getNodeEndPos(n).endLine,
b.getNodeStartPos(n).startPos, b.getNodeStartPos(n).startPos,
b.getNodeEndPos(n).endPos, b.getNodeEndPos(n).endPos,
} )
} }
// NewTokenPosition returns new Position // NewTokenPosition returns new Position
func (b *Builder) NewTokenPosition(t scanner.Token) *position.Position { func (b *PositionBuilder) NewTokenPosition(t scanner.Token) *position.Position {
return &position.Position{ return position.NewPosition(
t.StartLine, t.StartLine,
t.EndLine, t.EndLine,
t.StartPos, t.StartPos,
t.EndPos, t.EndPos,
} )
} }
// NewTokensPosition returns new Position // NewTokensPosition returns new Position
func (b *Builder) NewTokensPosition(startToken scanner.Token, endToken scanner.Token) *position.Position { func (b *PositionBuilder) NewTokensPosition(startToken scanner.Token, endToken scanner.Token) *position.Position {
return &position.Position{ return position.NewPosition(
startToken.StartLine, startToken.StartLine,
endToken.EndLine, endToken.EndLine,
startToken.StartPos, startToken.StartPos,
endToken.EndPos, endToken.EndPos,
} )
} }
// NewTokenNodePosition returns new Position // NewTokenNodePosition returns new Position
func (b *Builder) NewTokenNodePosition(t scanner.Token, n node.Node) *position.Position { func (b *PositionBuilder) NewTokenNodePosition(t scanner.Token, n node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
t.StartLine, t.StartLine,
b.getNodeEndPos(n).endLine, b.getNodeEndPos(n).endLine,
t.StartPos, t.StartPos,
b.getNodeEndPos(n).endPos, b.getNodeEndPos(n).endPos,
} )
} }
// NewNodeTokenPosition returns new Position // NewNodeTokenPosition returns new Position
func (b *Builder) NewNodeTokenPosition(n node.Node, t scanner.Token) *position.Position { func (b *PositionBuilder) NewNodeTokenPosition(n node.Node, t scanner.Token) *position.Position {
return &position.Position{ return position.NewPosition(
b.getNodeStartPos(n).startLine, b.getNodeStartPos(n).startLine,
t.EndLine, t.EndLine,
b.getNodeStartPos(n).startPos, b.getNodeStartPos(n).startPos,
t.EndPos, t.EndPos,
} )
} }
// NewNodesPosition returns new Position // NewNodesPosition returns new Position
func (b *Builder) NewNodesPosition(startNode node.Node, endNode node.Node) *position.Position { func (b *PositionBuilder) NewNodesPosition(startNode node.Node, endNode node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
b.getNodeStartPos(startNode).startLine, b.getNodeStartPos(startNode).startLine,
b.getNodeEndPos(endNode).endLine, b.getNodeEndPos(endNode).endLine,
b.getNodeStartPos(startNode).startPos, b.getNodeStartPos(startNode).startPos,
b.getNodeEndPos(endNode).endPos, b.getNodeEndPos(endNode).endPos,
} )
} }
// NewNodeListTokenPosition returns new Position // NewNodeListTokenPosition returns new Position
func (b *Builder) NewNodeListTokenPosition(list []node.Node, t scanner.Token) *position.Position { func (b *PositionBuilder) NewNodeListTokenPosition(list []node.Node, t scanner.Token) *position.Position {
return &position.Position{ return position.NewPosition(
b.getListStartPos(list).startLine, b.getListStartPos(list).startLine,
t.EndLine, t.EndLine,
b.getListStartPos(list).startPos, b.getListStartPos(list).startPos,
t.EndPos, t.EndPos,
} )
} }
// NewTokenNodeListPosition returns new Position // NewTokenNodeListPosition returns new Position
func (b *Builder) NewTokenNodeListPosition(t scanner.Token, list []node.Node) *position.Position { func (b *PositionBuilder) NewTokenNodeListPosition(t scanner.Token, list []node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
t.StartLine, t.StartLine,
b.getListEndPos(list).endLine, b.getListEndPos(list).endLine,
t.StartPos, t.StartPos,
b.getListEndPos(list).endPos, b.getListEndPos(list).endPos,
} )
} }
// NewNodeNodeListPosition returns new Position // NewNodeNodeListPosition returns new Position
func (b *Builder) NewNodeNodeListPosition(n node.Node, list []node.Node) *position.Position { func (b *PositionBuilder) NewNodeNodeListPosition(n node.Node, list []node.Node) *position.Position {
return &position.Position{ return position.NewPosition(
b.getNodeStartPos(n).startLine, b.getNodeStartPos(n).startLine,
b.getListEndPos(list).endLine, b.getListEndPos(list).endLine,
b.getNodeStartPos(n).startPos, b.getNodeStartPos(n).startPos,
b.getListEndPos(list).endPos, b.getListEndPos(list).endPos,
} )
} }
// NewOptionalListTokensPosition returns new Position // NewOptionalListTokensPosition returns new Position
func (b *Builder) NewOptionalListTokensPosition(list []node.Node, t scanner.Token, endToken scanner.Token) *position.Position { func (b *PositionBuilder) NewOptionalListTokensPosition(list []node.Node, t scanner.Token, endToken scanner.Token) *position.Position {
if list == nil { if list == nil {
return &position.Position{ return position.NewPosition(
t.StartLine, t.StartLine,
endToken.EndLine, endToken.EndLine,
t.StartPos, t.StartPos,
endToken.EndPos, endToken.EndPos,
} )
} }
return &position.Position{ return position.NewPosition(
b.getListStartPos(list).startLine, b.getListStartPos(list).startLine,
endToken.EndLine, endToken.EndLine,
b.getListStartPos(list).startPos, b.getListStartPos(list).startPos,
endToken.EndPos, endToken.EndPos,
} )
} }

View File

@ -11,7 +11,7 @@ import (
) )
func TestNewTokenPosition(t *testing.T) { func TestNewTokenPosition(t *testing.T) {
builder := parser.Builder{} builder := parser.PositionBuilder{}
tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3) tkn := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
@ -23,7 +23,7 @@ func TestNewTokenPosition(t *testing.T) {
} }
func TestNewTokensPosition(t *testing.T) { func TestNewTokensPosition(t *testing.T) {
builder := parser.Builder{} builder := parser.PositionBuilder{}
token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3) token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
token2 := scanner.NewToken([]byte(`foo`), 2, 2, 4, 6) token2 := scanner.NewToken([]byte(`foo`), 2, 2, 4, 6)
@ -46,7 +46,7 @@ func TestNewNodePosition(t *testing.T) {
EndPos: 3, EndPos: 3,
}) })
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: p, Positions: p,
} }
@ -69,7 +69,7 @@ func TestNewTokenNodePosition(t *testing.T) {
EndPos: 12, EndPos: 12,
}) })
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: p, Positions: p,
} }
@ -92,7 +92,7 @@ func TestNewNodeTokenPosition(t *testing.T) {
EndPos: 9, EndPos: 9,
}) })
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: p, Positions: p,
} }
@ -107,7 +107,7 @@ func TestNewNodeListPosition(t *testing.T) {
n1 := node.NewIdentifier("test node") n1 := node.NewIdentifier("test node")
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -135,7 +135,7 @@ func TestNewNodesPosition(t *testing.T) {
n1 := node.NewIdentifier("test node") n1 := node.NewIdentifier("test node")
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -164,7 +164,7 @@ func TestNewNodeListTokenPosition(t *testing.T) {
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
tkn := scanner.NewToken([]byte(`foo`), 3, 3, 20, 22) tkn := scanner.NewToken([]byte(`foo`), 3, 3, 20, 22)
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -193,7 +193,7 @@ func TestNewTokenNodeListPosition(t *testing.T) {
n1 := node.NewIdentifier("test node") n1 := node.NewIdentifier("test node")
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 2, StartLine: 2,
@ -222,7 +222,7 @@ func TestNewNodeNodeListPosition(t *testing.T) {
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
n3 := node.NewIdentifier("test node") n3 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -253,7 +253,7 @@ func TestNewNodeNodeListPosition(t *testing.T) {
} }
func TestNewOptionalListTokensPosition(t *testing.T) { func TestNewOptionalListTokensPosition(t *testing.T) {
builder := parser.Builder{} builder := parser.PositionBuilder{}
token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3) token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
token2 := scanner.NewToken([]byte(`foo`), 2, 2, 4, 6) token2 := scanner.NewToken([]byte(`foo`), 2, 2, 4, 6)
@ -270,7 +270,7 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
n2 := node.NewIdentifier("test node") n2 := node.NewIdentifier("test node")
n3 := node.NewIdentifier("test node") n3 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -304,7 +304,7 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
} }
func TestNilNodePos(t *testing.T) { func TestNilNodePos(t *testing.T) {
builder := parser.Builder{} builder := parser.PositionBuilder{}
pos := builder.NewNodesPosition(nil, nil) pos := builder.NewNodesPosition(nil, nil)
@ -316,7 +316,7 @@ func TestNilNodePos(t *testing.T) {
func TestNilNodeListPos(t *testing.T) { func TestNilNodeListPos(t *testing.T) {
n1 := node.NewIdentifier("test node") n1 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -337,7 +337,7 @@ func TestNilNodeListPos(t *testing.T) {
func TestNilNodeListTokenPos(t *testing.T) { func TestNilNodeListTokenPos(t *testing.T) {
token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3) token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
builder := parser.Builder{} builder := parser.PositionBuilder{}
pos := builder.NewNodeListTokenPosition(nil, token1) pos := builder.NewNodeListTokenPosition(nil, token1)
@ -349,7 +349,7 @@ func TestNilNodeListTokenPos(t *testing.T) {
func TestEmptyNodeListPos(t *testing.T) { func TestEmptyNodeListPos(t *testing.T) {
n1 := node.NewIdentifier("test node") n1 := node.NewIdentifier("test node")
builder := parser.Builder{ builder := parser.PositionBuilder{
Positions: &parser.Positions{ Positions: &parser.Positions{
n1: &position.Position{ n1: &position.Position{
StartLine: 1, StartLine: 1,
@ -370,7 +370,7 @@ func TestEmptyNodeListPos(t *testing.T) {
func TestEmptyNodeListTokenPos(t *testing.T) { func TestEmptyNodeListTokenPos(t *testing.T) {
token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3) token1 := scanner.NewToken([]byte(`foo`), 1, 1, 0, 3)
builder := parser.Builder{} builder := parser.PositionBuilder{}
pos := builder.NewNodeListTokenPosition([]node.Node{}, token1) pos := builder.NewNodeListTokenPosition([]node.Node{}, token1)

View File

@ -19,7 +19,7 @@ type Parser struct {
*scanner.Lexer *scanner.Lexer
path string path string
lastToken *scanner.Token lastToken *scanner.Token
positionBuilder *parser.Builder positionBuilder *parser.PositionBuilder
errors []*errors.Error errors []*errors.Error
rootNode node.Node rootNode node.Node
comments parser.Comments comments parser.Comments
@ -63,7 +63,7 @@ func (l *Parser) Parse() int {
l.rootNode = nil l.rootNode = nil
l.comments = parser.Comments{} l.comments = parser.Comments{}
l.positions = parser.Positions{} l.positions = parser.Positions{}
l.positionBuilder = &parser.Builder{ l.positionBuilder = &parser.PositionBuilder{
Positions: &l.positions, Positions: &l.positions,
} }

View File

@ -19,7 +19,7 @@ type Parser struct {
*scanner.Lexer *scanner.Lexer
path string path string
lastToken *scanner.Token lastToken *scanner.Token
positionBuilder *parser.Builder positionBuilder *parser.PositionBuilder
errors []*errors.Error errors []*errors.Error
rootNode node.Node rootNode node.Node
comments parser.Comments comments parser.Comments
@ -63,7 +63,7 @@ func (l *Parser) Parse() int {
l.rootNode = nil l.rootNode = nil
l.comments = parser.Comments{} l.comments = parser.Comments{}
l.positions = parser.Positions{} l.positions = parser.Positions{}
l.positionBuilder = &parser.Builder{ l.positionBuilder = &parser.PositionBuilder{
Positions: &l.positions, Positions: &l.positions,
} }