php-parser/parser/position_builder.go

200 lines
4.4 KiB
Go
Raw Normal View History

2018-04-15 18:39:26 +00:00
package parser
2018-01-11 18:19:02 +00:00
import (
"github.com/z7zmey/php-parser/node"
2018-04-15 18:39:26 +00:00
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
2018-01-11 18:19:02 +00:00
)
2018-04-15 19:06:02 +00:00
// PositionBuilder provide functions to constuct positions
type PositionBuilder struct {
Positions *Positions
2018-01-11 18:19:02 +00:00
}
type startPos struct {
startLine int
startPos int
}
type endPos struct {
endLine int
endPos int
}
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) getListStartPos(l []node.Node) 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])
}
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) getNodeStartPos(n node.Node) startPos {
2018-01-11 18:19:02 +00:00
sl := -1
sp := -1
if n == nil {
return startPos{-1, -1}
}
p := (*b.Positions)[n]
if p != nil {
sl = p.StartLine
sp = p.StartPos
}
return startPos{sl, sp}
}
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) getListEndPos(l []node.Node) 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])
}
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) getNodeEndPos(n node.Node) endPos {
2018-01-11 18:19:02 +00:00
el := -1
ep := -1
if n == nil {
return endPos{-1, -1}
}
p := (*b.Positions)[n]
if p != nil {
el = p.EndLine
ep = p.EndPos
}
return endPos{el, ep}
}
// NewNodeListPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodeListPosition(list []node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getListStartPos(list).startLine,
b.getListEndPos(list).endLine,
b.getListStartPos(list).startPos,
b.getListEndPos(list).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewNodePosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodePosition(n node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getNodeStartPos(n).startLine,
b.getNodeEndPos(n).endLine,
b.getNodeStartPos(n).startPos,
b.getNodeEndPos(n).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewTokenPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewTokenPosition(t scanner.Token) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
t.StartLine,
t.EndLine,
t.StartPos,
t.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewTokensPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewTokensPosition(startToken scanner.Token, endToken scanner.Token) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
startToken.StartLine,
endToken.EndLine,
startToken.StartPos,
endToken.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewTokenNodePosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewTokenNodePosition(t scanner.Token, n node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
t.StartLine,
b.getNodeEndPos(n).endLine,
t.StartPos,
b.getNodeEndPos(n).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewNodeTokenPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodeTokenPosition(n node.Node, t scanner.Token) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getNodeStartPos(n).startLine,
t.EndLine,
b.getNodeStartPos(n).startPos,
t.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewNodesPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodesPosition(startNode node.Node, endNode node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getNodeStartPos(startNode).startLine,
b.getNodeEndPos(endNode).endLine,
b.getNodeStartPos(startNode).startPos,
b.getNodeEndPos(endNode).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewNodeListTokenPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodeListTokenPosition(list []node.Node, t scanner.Token) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getListStartPos(list).startLine,
t.EndLine,
b.getListStartPos(list).startPos,
t.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewTokenNodeListPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewTokenNodeListPosition(t scanner.Token, list []node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
t.StartLine,
b.getListEndPos(list).endLine,
t.StartPos,
b.getListEndPos(list).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewNodeNodeListPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewNodeNodeListPosition(n node.Node, list []node.Node) *position.Position {
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getNodeStartPos(n).startLine,
b.getListEndPos(list).endLine,
b.getNodeStartPos(n).startPos,
b.getListEndPos(list).endPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
// NewOptionalListTokensPosition returns new Position
2018-04-15 19:06:02 +00:00
func (b *PositionBuilder) NewOptionalListTokensPosition(list []node.Node, t scanner.Token, endToken scanner.Token) *position.Position {
2018-01-11 18:19:02 +00:00
if list == nil {
2018-04-15 19:06:02 +00:00
return position.NewPosition(
2018-01-11 18:19:02 +00:00
t.StartLine,
endToken.EndLine,
t.StartPos,
endToken.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}
2018-04-15 19:06:02 +00:00
return position.NewPosition(
2018-01-11 18:19:02 +00:00
b.getListStartPos(list).startLine,
endToken.EndLine,
b.getListStartPos(list).startPos,
endToken.EndPos,
2018-04-15 19:06:02 +00:00
)
2018-01-11 18:19:02 +00:00
}