handle nodes positions

This commit is contained in:
z7zmey 2017-12-31 20:53:55 +02:00
parent f2d972582f
commit 30187b1db1
7 changed files with 1530 additions and 1165 deletions

View File

@ -13,6 +13,9 @@ type dumper struct {
func (d dumper) EnterNode(n node.Node) bool {
fmt.Printf("%v%v", d.indent, n.Name())
if p := n.Position(); p != nil {
fmt.Printf(" %v", *p)
}
if a := n.Attributes(); len(a) > 0 {
fmt.Printf(" %v", a)
}

View File

@ -4,6 +4,28 @@ import (
"github.com/z7zmey/php-parser/node"
)
type ClassConstFetch struct {
name string
attributes map[string]interface{}
position *node.Position
class node.Node
constantName node.Node
}
func NewClassConstFetch(class node.Node, constantName node.Node) node.Node {
return ClassConstFetch{
"ClassConstFetch",
map[string]interface{}{},
nil,
class,
constantName,
}
}
func (n ClassConstFetch) Name() string {
return "ClassConstFetch"
}
func (n ClassConstFetch) Attributes() map[string]interface{} {
return n.attributes
}
@ -25,28 +47,6 @@ func (n ClassConstFetch) SetPosition(p *node.Position) node.Node {
return n
}
func (n ClassConstFetch) Name() string {
return "ClassConstFetch"
}
type ClassConstFetch struct {
name string
attributes map[string]interface{}
position *node.Position
class node.Node
constantName node.Node
}
func NewClassConstFetch(class node.Node, constantName node.Node) node.Node {
return ClassConstFetch{
"ClassConstFetch",
map[string]interface{}{},
nil,
class,
constantName,
}
}
func (n ClassConstFetch) Walk(v node.Visitor) {
if v.EnterNode(n) == false {
return

View File

@ -7,7 +7,7 @@ import (
type Encapsed struct {
name string
attributes map[string]interface{}
position *node.Position
position *node.Position
parts []node.Node
}

View File

@ -5,6 +5,30 @@ import (
"github.com/z7zmey/php-parser/token"
)
type Switch struct {
name string
attributes map[string]interface{}
position *node.Position
token token.Token
cond node.Node
cases []node.Node
}
func NewSwitch(token token.Token, cond node.Node, cases []node.Node) node.Node {
return Switch{
"Switch",
map[string]interface{}{},
nil,
token,
cond,
cases,
}
}
func (n Switch) Name() string {
return "Switch"
}
func (n Switch) Attributes() map[string]interface{} {
return n.attributes
}
@ -26,30 +50,6 @@ func (n Switch) SetPosition(p *node.Position) node.Node {
return n
}
func (n Switch) Name() string {
return "Switch"
}
type Switch struct {
name string
attributes map[string]interface{}
position *node.Position
token token.Token
cond node.Node
cases []node.Node
}
func NewSwitch(token token.Token, cond node.Node, cases []node.Node) node.Node {
return Switch{
"Switch",
map[string]interface{}{},
nil,
token,
cond,
cases,
}
}
func (n Switch) Walk(v node.Visitor) {
if v.EnterNode(n) == false {
return

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

121
parser/positions.go Normal file
View File

@ -0,0 +1,121 @@
package parser
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
func getListPosStartLine(l []node.Node) int {
startLine := -1
if l == nil {
return startLine
}
if len(l) == 0 {
return startLine
}
return getNodePosStartLine(l[0])
}
func getNodePosStartLine(n node.Node) int {
startLine := -1
if n == nil {
return startLine
}
p := n.Position()
if p != nil {
startLine = p.StartLine
}
return startLine
}
func getListPosEndLine(l []node.Node) int {
endLine := -1
if l == nil {
return endLine
}
if len(l) == 0 {
return endLine
}
return getNodePosEndLine(l[len(l)-1])
}
func getNodePosEndLine(n node.Node) int {
endLine := -1
if n == nil {
return endLine
}
p := n.Position()
if p != nil {
endLine = p.EndLine
}
return endLine
}
func NewNodeListPosition(list []node.Node) *node.Position {
return &node.Position{getListPosStartLine(list), getListPosEndLine(list)}
}
func NewNodePosition(n node.Node) *node.Position {
return &node.Position{getNodePosStartLine(n), getNodePosEndLine(n)}
}
func NewTokenPosition(t token.Token) *node.Position {
return &node.Position{t.StartLine, t.EndLine}
}
func NewTokensPosition(startToken token.Token, EndToken token.Token) *node.Position {
return &node.Position{startToken.StartLine, EndToken.EndLine}
}
func NewTokenNodePosition(t token.Token, n node.Node) *node.Position {
return &node.Position{t.StartLine, getNodePosEndLine(n)}
}
func NewNodeTokenPosition(n node.Node, t token.Token) *node.Position {
return &node.Position{getNodePosStartLine(n), t.EndLine}
}
func NewNodesPosition(startNode node.Node, endNode node.Node) *node.Position {
return &node.Position{getNodePosStartLine(startNode), getNodePosEndLine(endNode)}
}
func NewNodeListTokenPosition(list []node.Node, t token.Token) *node.Position {
return &node.Position{getListPosStartLine(list), t.EndLine}
}
func NewTokenNodeListPosition(t token.Token, list []node.Node) *node.Position {
return &node.Position{t.StartLine, getListPosEndLine(list)}
}
func NewNodeNodeListPosition(n node.Node, list []node.Node) *node.Position {
return &node.Position{getNodePosStartLine(n), getListPosEndLine(list)}
}
func NewOptionalListTokensPosition(list []node.Node, t token.Token, endToken token.Token) *node.Position {
if list == nil {
return &node.Position{t.StartLine, endToken.EndLine}
} else {
return &node.Position{getListPosStartLine(list), endToken.EndLine}
}
}
// AltIf Positions
func NewAltIfStartPosition(startToken token.Token) *node.Position {
return &node.Position{startToken.StartLine, -1}
}
func NewAltIfPosition(startLine int, EndToken token.Token) *node.Position {
return &node.Position{startLine, EndToken.EndLine}
}