constant and declare nodes

This commit is contained in:
vadim
2017-12-06 20:52:51 +02:00
parent 3a5590d7bc
commit ea2494b9b8
6 changed files with 771 additions and 684 deletions

View File

@@ -4,8 +4,6 @@ import (
"bytes"
"fmt"
"io"
"github.com/z7zmey/php-parser/token"
)
type Node interface {
@@ -20,11 +18,6 @@ type SimpleNode struct {
Attributes map[string]string
}
type tokenNode struct {
*SimpleNode
token token.Token
}
func (n SimpleNode) String() string {
buf := new(bytes.Buffer)
n.Print(buf, " ")
@@ -46,20 +39,6 @@ func NewSimpleNode(name string) Node {
return SimpleNode{Name: name, Attributes: make(map[string]string)}
}
func TokenNode(name string, t token.Token) Node {
return tokenNode{
&SimpleNode{Name: name, Attributes: make(map[string]string)},
t,
}
}
func (n tokenNode) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
for _, nn := range n.Children {
nn.Print(out, indent+" ")
}
}
func (n SimpleNode) Append(nn ...Node) Node {
n.Children = append(n.Children, nn...)
return n

30
node/stmt/constant.go Normal file
View File

@@ -0,0 +1,30 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Constant struct {
node.SimpleNode
token token.Token
expr node.Node
}
func NewConstant(token token.Token, expr node.Node) node.Node {
return Constant{
node.SimpleNode{Name: "Constant", Attributes: make(map[string]string)},
token,
expr,
}
}
func (n Constant) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
fmt.Fprintf(out, "\n%vexpr:", indent+" ")
n.expr.Print(out, indent+" ")
}

43
node/stmt/declare.go Normal file
View File

@@ -0,0 +1,43 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Declare struct {
node.SimpleNode
token token.Token
consts []node.Node
stmts []node.Node
}
func NewDeclare(token token.Token, consts []node.Node, stmts []node.Node) node.Node {
return Declare{
node.SimpleNode{Name: "Declare", Attributes: make(map[string]string)},
token,
consts,
stmts,
}
}
func (n Declare) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
if n.consts != nil {
fmt.Fprintf(out, "\n%vconsts:", indent+" ")
for _, nn := range n.consts {
nn.Print(out, indent+" ")
}
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
}
}

34
node/stmt/stmt_const.go Normal file
View File

@@ -0,0 +1,34 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type StmtConst struct {
node.SimpleNode
token token.Token
consts []node.Node
}
func NewStmtConst(token token.Token, consts []node.Node) node.Node {
return StmtConst{
node.SimpleNode{Name: "StmtConst", Attributes: make(map[string]string)},
token,
consts,
}
}
func (n StmtConst) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
if n.consts != nil {
fmt.Fprintf(out, "\n%vconsts:", indent+" ")
for _, nn := range n.consts {
nn.Print(out, indent+" ")
}
}
}