create position builder

This commit is contained in:
z7zmey 2018-01-11 20:19:02 +02:00
parent a49fdcf8a0
commit e97b018b72
6 changed files with 1341 additions and 1322 deletions

View File

@ -2,6 +2,13 @@ package node
// Node interface // Node interface
type Node interface { type Node interface {
Attributes() map[string]interface{} Attributes() map[string]interface{} // Attributes returns node attributes as map
Walk(v Visitor) Walk(v Visitor) // Walk traverses nodes
}
// Visitor interface
type Visitor interface {
EnterNode(node Node) bool // EnterNode invoked for each node encountered by Walk.
GetChildrenVisitor(Key string) Visitor // GetChildrenVisitor returns visitor for children nodes
LeaveNode(node Node) // LeaveNode invoked after process node
} }

View File

@ -1,7 +0,0 @@
package node
type Visitor interface {
EnterNode(node Node) bool
GetChildrenVisitor(Key string) Visitor
LeaveNode(node Node)
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

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

198
position/builder.go Normal file
View File

@ -0,0 +1,198 @@
package position
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
// Builder provide functions to constuct positions
type Builder struct {
Positions *Positions
}
type startPos struct {
startLine int
startPos int
}
type endPos struct {
endLine int
endPos int
}
func (b *Builder) getListStartPos(l []node.Node) startPos {
if l == nil {
return startPos{-1, -1}
}
if len(l) == 0 {
return startPos{-1, -1}
}
return b.getNodeStartPos(l[0])
}
func (b *Builder) getNodeStartPos(n node.Node) startPos {
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}
}
func (b *Builder) getListEndPos(l []node.Node) endPos {
if l == nil {
return endPos{-1, -1}
}
if len(l) == 0 {
return endPos{-1, -1}
}
return b.getNodeEndPos(l[len(l)-1])
}
func (b *Builder) getNodeEndPos(n node.Node) endPos {
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
func (b *Builder) NewNodeListPosition(list []node.Node) *Position {
return &Position{
b.getListStartPos(list).startLine,
b.getListEndPos(list).endLine,
b.getListStartPos(list).startPos,
b.getListEndPos(list).endPos,
}
}
// NewNodePosition returns new Position
func (b *Builder) NewNodePosition(n node.Node) *Position {
return &Position{
b.getNodeStartPos(n).startLine,
b.getNodeEndPos(n).endLine,
b.getNodeStartPos(n).startPos,
b.getNodeEndPos(n).endPos,
}
}
// NewTokenPosition returns new Position
func (b *Builder) NewTokenPosition(t token.Token) *Position {
return &Position{
t.StartLine,
t.EndLine,
t.StartPos,
t.EndPos,
}
}
// NewTokensPosition returns new Position
func (b *Builder) NewTokensPosition(startToken token.Token, endToken token.Token) *Position {
return &Position{
startToken.StartLine,
endToken.EndLine,
startToken.StartPos,
endToken.EndPos,
}
}
// NewTokenNodePosition returns new Position
func (b *Builder) NewTokenNodePosition(t token.Token, n node.Node) *Position {
return &Position{
t.StartLine,
b.getNodeEndPos(n).endLine,
t.StartPos,
b.getNodeEndPos(n).endPos,
}
}
// NewNodeTokenPosition returns new Position
func (b *Builder) NewNodeTokenPosition(n node.Node, t token.Token) *Position {
return &Position{
b.getNodeStartPos(n).startLine,
t.EndLine,
b.getNodeStartPos(n).startPos,
t.EndPos,
}
}
// NewNodesPosition returns new Position
func (b *Builder) NewNodesPosition(startNode node.Node, endNode node.Node) *Position {
return &Position{
b.getNodeStartPos(startNode).startLine,
b.getNodeEndPos(endNode).endLine,
b.getNodeStartPos(startNode).startPos,
b.getNodeEndPos(endNode).endPos,
}
}
// NewNodeListTokenPosition returns new Position
func (b *Builder) NewNodeListTokenPosition(list []node.Node, t token.Token) *Position {
return &Position{
b.getListStartPos(list).startLine,
t.EndLine,
b.getListStartPos(list).startPos,
t.EndPos,
}
}
// NewTokenNodeListPosition returns new Position
func (b *Builder) NewTokenNodeListPosition(t token.Token, list []node.Node) *Position {
return &Position{
t.StartLine,
b.getListEndPos(list).endLine,
t.StartPos,
b.getListEndPos(list).endPos,
}
}
// NewNodeNodeListPosition returns new Position
func (b *Builder) NewNodeNodeListPosition(n node.Node, list []node.Node) *Position {
return &Position{
b.getNodeStartPos(n).startLine,
b.getListEndPos(list).endLine,
b.getNodeStartPos(n).startPos,
b.getListEndPos(list).endPos,
}
}
// NewOptionalListTokensPosition returns new Position
func (b *Builder) NewOptionalListTokensPosition(list []node.Node, t token.Token, endToken token.Token) *Position {
if list == nil {
return &Position{
t.StartLine,
endToken.EndLine,
t.StartPos,
endToken.EndPos,
}
}
return &Position{
b.getListStartPos(list).startLine,
endToken.EndLine,
b.getListStartPos(list).startPos,
endToken.EndPos,
}
}