php-parser/internal/positionbuilder/position_builder.go

208 lines
5.3 KiB
Go
Raw Normal View History

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
func (b *PositionBuilder) NewTokenPosition(t *scanner.Token) *position.Position {
2018-06-24 07:19:44 +00:00
return &position.Position{
StartLine: t.StartLine,
EndLine: t.EndLine,
StartPos: t.StartPos,
EndPos: t.EndPos,
}
2018-01-11 18:19:02 +00:00
}
// NewTokensPosition returns new Position
func (b *PositionBuilder) NewTokensPosition(startToken *scanner.Token, endToken *scanner.Token) *position.Position {
2018-06-24 07:19:44 +00:00
return &position.Position{
StartLine: startToken.StartLine,
EndLine: endToken.EndLine,
StartPos: startToken.StartPos,
EndPos: endToken.EndPos,
}
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{
StartLine: t.StartLine,
EndLine: b.getNodeEndPos(n).endLine,
StartPos: t.StartPos,
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,
EndLine: t.EndLine,
StartPos: b.getNodeStartPos(n).startPos,
EndPos: t.EndPos,
}
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,
EndLine: t.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: t.EndPos,
}
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{
StartLine: t.StartLine,
EndLine: b.getListEndPos(list).endLine,
StartPos: t.StartPos,
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{
StartLine: t.StartLine,
EndLine: endToken.EndLine,
StartPos: t.StartPos,
EndPos: endToken.EndPos,
}
}
return &position.Position{
StartLine: b.getListStartPos(list).startLine,
EndLine: endToken.EndLine,
StartPos: b.getListStartPos(list).startPos,
EndPos: endToken.EndPos,
}
2018-01-11 18:19:02 +00:00
}