2019-12-26 15:57:56 +00:00
|
|
|
package positionbuilder
|
2018-01-11 18:19:02 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-12 21:16:36 +00:00
|
|
|
"github.com/z7zmey/php-parser/internal/scanner"
|
|
|
|
"github.com/z7zmey/php-parser/pkg/ast"
|
|
|
|
"github.com/z7zmey/php-parser/pkg/position"
|
2018-01-11 18:19:02 +00:00
|
|
|
)
|
|
|
|
|
2018-04-15 19:06:02 +00:00
|
|
|
// PositionBuilder provide functions to constuct positions
|
2018-06-24 07:19:44 +00:00
|
|
|
type PositionBuilder struct{}
|
2018-01-11 18:19:02 +00:00
|
|
|
|
|
|
|
type startPos struct {
|
|
|
|
startLine int
|
|
|
|
startPos int
|
|
|
|
}
|
|
|
|
|
|
|
|
type endPos struct {
|
|
|
|
endLine int
|
|
|
|
endPos int
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) getListStartPos(l []ast.Vertex) startPos {
|
2018-01-11 18:19:02 +00:00
|
|
|
if l == nil {
|
|
|
|
return startPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
|
|
|
return startPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.getNodeStartPos(l[0])
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) getNodeStartPos(n ast.Vertex) startPos {
|
2018-01-11 18:19:02 +00:00
|
|
|
sl := -1
|
|
|
|
sp := -1
|
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
return startPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
p := n.GetNode().Position
|
2018-01-11 18:19:02 +00:00
|
|
|
if p != nil {
|
|
|
|
sl = p.StartLine
|
|
|
|
sp = p.StartPos
|
|
|
|
}
|
|
|
|
|
|
|
|
return startPos{sl, sp}
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) getListEndPos(l []ast.Vertex) endPos {
|
2018-01-11 18:19:02 +00:00
|
|
|
if l == nil {
|
|
|
|
return endPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
|
|
|
return endPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.getNodeEndPos(l[len(l)-1])
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) getNodeEndPos(n ast.Vertex) endPos {
|
2018-01-11 18:19:02 +00:00
|
|
|
el := -1
|
|
|
|
ep := -1
|
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
return endPos{-1, -1}
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:16:36 +00:00
|
|
|
p := n.GetNode().Position
|
2018-01-11 18:19:02 +00:00
|
|
|
if p != nil {
|
|
|
|
el = p.EndLine
|
|
|
|
ep = p.EndPos
|
|
|
|
}
|
|
|
|
|
|
|
|
return endPos{el, ep}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeListPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodeListPosition(list []ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getListStartPos(list).startLine,
|
|
|
|
EndLine: b.getListEndPos(list).endLine,
|
|
|
|
StartPos: b.getListStartPos(list).startPos,
|
|
|
|
EndPos: b.getListEndPos(list).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodePosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodePosition(n ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getNodeStartPos(n).startLine,
|
|
|
|
EndLine: b.getNodeEndPos(n).endLine,
|
|
|
|
StartPos: b.getNodeStartPos(n).startPos,
|
|
|
|
EndPos: b.getNodeEndPos(n).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokenPosition returns new Position
|
2018-04-15 19:56:20 +00:00
|
|
|
func (b *PositionBuilder) NewTokenPosition(t *scanner.Token) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
2020-05-17 20:50:23 +00:00
|
|
|
StartLine: t.Position.StartLine,
|
|
|
|
EndLine: t.Position.EndLine,
|
|
|
|
StartPos: t.Position.StartPos,
|
|
|
|
EndPos: t.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokensPosition returns new Position
|
2018-04-15 19:56:20 +00:00
|
|
|
func (b *PositionBuilder) NewTokensPosition(startToken *scanner.Token, endToken *scanner.Token) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
2020-05-17 20:50:23 +00:00
|
|
|
StartLine: startToken.Position.StartLine,
|
|
|
|
EndLine: endToken.Position.EndLine,
|
|
|
|
StartPos: startToken.Position.StartPos,
|
|
|
|
EndPos: endToken.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokenNodePosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewTokenNodePosition(t *scanner.Token, n ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
2020-05-17 20:50:23 +00:00
|
|
|
StartLine: t.Position.StartLine,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndLine: b.getNodeEndPos(n).endLine,
|
2020-05-17 20:50:23 +00:00
|
|
|
StartPos: t.Position.StartPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: b.getNodeEndPos(n).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeTokenPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodeTokenPosition(n ast.Vertex, t *scanner.Token) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getNodeStartPos(n).startLine,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndLine: t.Position.EndLine,
|
2018-06-24 07:19:44 +00:00
|
|
|
StartPos: b.getNodeStartPos(n).startPos,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndPos: t.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodesPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodesPosition(startNode ast.Vertex, endNode ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getNodeStartPos(startNode).startLine,
|
|
|
|
EndLine: b.getNodeEndPos(endNode).endLine,
|
|
|
|
StartPos: b.getNodeStartPos(startNode).startPos,
|
|
|
|
EndPos: b.getNodeEndPos(endNode).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeListTokenPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodeListTokenPosition(list []ast.Vertex, t *scanner.Token) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getListStartPos(list).startLine,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndLine: t.Position.EndLine,
|
2018-06-24 07:19:44 +00:00
|
|
|
StartPos: b.getListStartPos(list).startPos,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndPos: t.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTokenNodeListPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewTokenNodeListPosition(t *scanner.Token, list []ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
2020-05-17 20:50:23 +00:00
|
|
|
StartLine: t.Position.StartLine,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndLine: b.getListEndPos(list).endLine,
|
2020-05-17 20:50:23 +00:00
|
|
|
StartPos: t.Position.StartPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: b.getListEndPos(list).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeNodeListPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodeNodeListPosition(n ast.Vertex, list []ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getNodeStartPos(n).startLine,
|
|
|
|
EndLine: b.getListEndPos(list).endLine,
|
|
|
|
StartPos: b.getNodeStartPos(n).startPos,
|
|
|
|
EndPos: b.getListEndPos(list).endPos,
|
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 06:35:44 +00:00
|
|
|
// NewNodeListNodePosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewNodeListNodePosition(list []ast.Vertex, n ast.Vertex) *position.Position {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getListStartPos(list).startLine,
|
|
|
|
EndLine: b.getNodeEndPos(n).endLine,
|
|
|
|
StartPos: b.getListStartPos(list).startPos,
|
|
|
|
EndPos: b.getNodeEndPos(n).endPos,
|
|
|
|
}
|
2018-06-03 06:35:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:19:02 +00:00
|
|
|
// NewOptionalListTokensPosition returns new Position
|
2020-05-12 21:16:36 +00:00
|
|
|
func (b *PositionBuilder) NewOptionalListTokensPosition(list []ast.Vertex, t *scanner.Token, endToken *scanner.Token) *position.Position {
|
2018-01-11 18:19:02 +00:00
|
|
|
if list == nil {
|
2018-06-24 07:19:44 +00:00
|
|
|
return &position.Position{
|
2020-05-17 20:50:23 +00:00
|
|
|
StartLine: t.Position.StartLine,
|
|
|
|
EndLine: endToken.Position.EndLine,
|
|
|
|
StartPos: t.Position.StartPos,
|
|
|
|
EndPos: endToken.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &position.Position{
|
|
|
|
StartLine: b.getListStartPos(list).startLine,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndLine: endToken.Position.EndLine,
|
2018-06-24 07:19:44 +00:00
|
|
|
StartPos: b.getListStartPos(list).startPos,
|
2020-05-17 20:50:23 +00:00
|
|
|
EndPos: endToken.Position.EndPos,
|
2018-06-24 07:19:44 +00:00
|
|
|
}
|
2018-01-11 18:19:02 +00:00
|
|
|
}
|