2017-12-31 18:53:55 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/token"
|
|
|
|
)
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
type startPos struct {
|
|
|
|
startLine int
|
|
|
|
startPos int
|
|
|
|
}
|
|
|
|
|
|
|
|
type endPos struct {
|
|
|
|
endLine int
|
|
|
|
endPos int
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
func getListStartPos(l []node.Node) startPos {
|
2017-12-31 18:53:55 +00:00
|
|
|
if l == nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
return startPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
2018-01-05 11:01:14 +00:00
|
|
|
return startPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
return getNodeStartPos(l[0])
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
func getNodeStartPos(n node.Node) startPos {
|
|
|
|
sl := -1
|
|
|
|
sp := -1
|
2017-12-31 18:53:55 +00:00
|
|
|
|
|
|
|
if n == nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
return startPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := n.Position()
|
|
|
|
if p != nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
sl = p.StartLine
|
|
|
|
sp = p.StartPos
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
return startPos{sl, sp}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
func getListEndPos(l []node.Node) endPos {
|
2017-12-31 18:53:55 +00:00
|
|
|
if l == nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
return endPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
2018-01-05 11:01:14 +00:00
|
|
|
return endPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
return getNodeEndPos(l[len(l)-1])
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
func getNodeEndPos(n node.Node) endPos {
|
|
|
|
el := -1
|
|
|
|
ep := -1
|
2017-12-31 18:53:55 +00:00
|
|
|
|
|
|
|
if n == nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
return endPos{-1, -1}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := n.Position()
|
|
|
|
if p != nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
el = p.EndLine
|
|
|
|
ep = p.EndPos
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
return endPos{el, ep}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeListPosition(list []node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getListStartPos(list).startLine,
|
|
|
|
getListEndPos(list).endLine,
|
|
|
|
getListStartPos(list).startPos,
|
|
|
|
getListEndPos(list).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodePosition(n node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getNodeStartPos(n).startLine,
|
|
|
|
getNodeEndPos(n).endLine,
|
|
|
|
getNodeStartPos(n).startPos,
|
|
|
|
getNodeEndPos(n).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTokenPosition(t token.Token) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
t.StartLine,
|
|
|
|
t.EndLine,
|
|
|
|
t.StartPos,
|
|
|
|
t.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 11:01:14 +00:00
|
|
|
func NewTokensPosition(startToken token.Token, endToken token.Token) *node.Position {
|
|
|
|
return &node.Position{
|
|
|
|
startToken.StartLine,
|
|
|
|
endToken.EndLine,
|
|
|
|
startToken.StartPos,
|
|
|
|
endToken.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTokenNodePosition(t token.Token, n node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
t.StartLine,
|
|
|
|
getNodeEndPos(n).endLine,
|
|
|
|
t.StartPos,
|
|
|
|
getNodeEndPos(n).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeTokenPosition(n node.Node, t token.Token) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getNodeStartPos(n).startLine,
|
|
|
|
t.EndLine,
|
|
|
|
getNodeStartPos(n).startPos,
|
|
|
|
t.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodesPosition(startNode node.Node, endNode node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getNodeStartPos(startNode).startLine,
|
|
|
|
getNodeEndPos(endNode).endLine,
|
|
|
|
getNodeStartPos(startNode).startPos,
|
|
|
|
getNodeEndPos(endNode).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeListTokenPosition(list []node.Node, t token.Token) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getListStartPos(list).startLine,
|
|
|
|
t.EndLine,
|
|
|
|
getListStartPos(list).startPos,
|
|
|
|
t.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTokenNodeListPosition(t token.Token, list []node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
t.StartLine,
|
|
|
|
getListEndPos(list).endLine,
|
|
|
|
t.StartPos,
|
|
|
|
getListEndPos(list).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeNodeListPosition(n node.Node, list []node.Node) *node.Position {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getNodeStartPos(n).startLine,
|
|
|
|
getListEndPos(list).endLine,
|
|
|
|
getNodeStartPos(n).startPos,
|
|
|
|
getListEndPos(list).endPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOptionalListTokensPosition(list []node.Node, t token.Token, endToken token.Token) *node.Position {
|
|
|
|
if list == nil {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
t.StartLine,
|
|
|
|
endToken.EndLine,
|
|
|
|
t.StartPos,
|
|
|
|
endToken.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
} else {
|
2018-01-05 11:01:14 +00:00
|
|
|
return &node.Position{
|
|
|
|
getListStartPos(list).startLine,
|
|
|
|
endToken.EndLine,
|
|
|
|
getListStartPos(list).startPos,
|
|
|
|
endToken.EndPos,
|
|
|
|
}
|
2017-12-31 18:53:55 +00:00
|
|
|
}
|
|
|
|
}
|