Documentation
¶
Overview ¶
Package visitor contains walker.visitor implementations
Package visitor contains walker.visitor implementations ¶
Package visitor contains walker.visitor implementations ¶
Package visitor contains walker.visitor implementations ¶
Package visitor contains walker.visitor implementations
Index ¶
- type Dumper
- func (d *Dumper) EnterChildList(key string, w walker.Walkable)
- func (d *Dumper) EnterChildNode(key string, w walker.Walkable)
- func (d *Dumper) EnterNode(w walker.Walkable) bool
- func (d *Dumper) LeaveChildList(key string, w walker.Walkable)
- func (d *Dumper) LeaveChildNode(key string, w walker.Walkable)
- func (d *Dumper) LeaveNode(n walker.Walkable)
- type GoDumper
- func (d *GoDumper) EnterChildList(key string, w walker.Walkable)
- func (d *GoDumper) EnterChildNode(key string, w walker.Walkable)
- func (d *GoDumper) EnterNode(w walker.Walkable) bool
- func (d *GoDumper) LeaveChildList(key string, w walker.Walkable)
- func (d *GoDumper) LeaveChildNode(key string, w walker.Walkable)
- func (d *GoDumper) LeaveNode(n walker.Walkable)
- type JsonDumper
- func (d *JsonDumper) EnterChildList(key string, w walker.Walkable)
- func (d *JsonDumper) EnterChildNode(key string, w walker.Walkable)
- func (d *JsonDumper) EnterNode(w walker.Walkable) bool
- func (d *JsonDumper) LeaveChildList(key string, w walker.Walkable)
- func (d *JsonDumper) LeaveChildNode(key string, w walker.Walkable)
- func (d *JsonDumper) LeaveNode(n walker.Walkable)
- type Namespace
- type NamespaceResolver
- func (nsr *NamespaceResolver) AddAlias(useType string, nn node.Node, prefix []node.Node)
- func (nsr *NamespaceResolver) AddNamespacedName(nn node.Node, nodeName string)
- func (nsr *NamespaceResolver) EnterChildList(key string, w walker.Walkable)
- func (nsr *NamespaceResolver) EnterChildNode(key string, w walker.Walkable)
- func (nsr *NamespaceResolver) EnterNode(w walker.Walkable) bool
- func (nsr *NamespaceResolver) LeaveChildList(key string, w walker.Walkable)
- func (nsr *NamespaceResolver) LeaveChildNode(key string, w walker.Walkable)
- func (nsr *NamespaceResolver) LeaveNode(w walker.Walkable)
- func (nsr *NamespaceResolver) ResolveName(nameNode node.Node, aliasType string)
- func (nsr *NamespaceResolver) ResolveType(n node.Node)
- type PrettyJsonDumper
- func (d *PrettyJsonDumper) EnterChildList(key string, w walker.Walkable)
- func (d *PrettyJsonDumper) EnterChildNode(key string, w walker.Walkable)
- func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool
- func (d *PrettyJsonDumper) LeaveChildList(key string, w walker.Walkable)
- func (d *PrettyJsonDumper) LeaveChildNode(key string, w walker.Walkable)
- func (d *PrettyJsonDumper) LeaveNode(n walker.Walkable)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dumper ¶
type Dumper struct {
Writer io.Writer
Indent string
NsResolver *NamespaceResolver
}
Dumper writes ast hierarchy to an io.Writer Also prints comments and positions attached to nodes
Example ¶
package main
import (
"os"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
src := `<?php
namespace Foo {
class Bar {
public function FunctionName(Type $var = null)
{
// some comment
$var;
}
}
}`
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.WithFreeFloating()
php7parser.Parse()
nodes := php7parser.GetRootNode()
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := &visitor.Dumper{
Writer: os.Stdout,
Indent: "| ",
NsResolver: nsResolver,
}
nodes.Walk(dumper)
}
Output: | [*node.Root] | "Position": Pos{Line: 3-11 Pos: 9-144} | "Stmts": | [*stmt.Namespace] | "Position": Pos{Line: 3-11 Pos: 9-144} | "freefloating": | "Start": "<?php" | "Start": "\n\n\t\t" | "Stmts": "\n\t\t" | "NamespaceName": | [*name.Name] | "Position": Pos{Line: 3-3 Pos: 19-22} | "freefloating": | "Start": " " | "End": " " | "Parts": | [*name.NamePart] | "Position": Pos{Line: 3-3 Pos: 19-22} | "Value": "Foo" | "Stmts": | [*stmt.Class] | "Position": Pos{Line: 4-10 Pos: 28-140} | "NamespacedName": "Foo\\Bar" | "freefloating": | "Start": "\n\t\t\t" | "Name": " " | "Stmts": "\n\t\t\t" | "PhpDocComment": "" | "ClassName": | [*node.Identifier] | "Position": Pos{Line: 4-4 Pos: 34-37} | "freefloating": | "Start": " " | "Value": "Bar" | "Stmts": | [*stmt.ClassMethod] | "Position": Pos{Line: 5-9 Pos: 45-135} | "freefloating": | "Start": " \n\t\t\t\t" | "ModifierList": " " | "Function": " " | "ReturnsRef": false | "PhpDocComment": "" | "MethodName": | [*node.Identifier] | "Position": Pos{Line: 5-5 Pos: 61-73} | "Value": "FunctionName" | "Modifiers": | [*node.Identifier] | "Position": Pos{Line: 5-5 Pos: 45-51} | "Value": "public" | "Params": | [*node.Parameter] | "Position": Pos{Line: 5-5 Pos: 74-90} | "freefloating": | "OptionalType": " " | "Var": " " | "Variadic": false | "ByRef": false | "VariableType": | [*name.Name] | "Position": Pos{Line: 5-5 Pos: 74-78} | "NamespacedName": "Foo\\Type" | "Parts": | [*name.NamePart] | "Position": Pos{Line: 5-5 Pos: 74-78} | "Value": "Type" | "Variable": | [*expr.Variable] | "Position": Pos{Line: 5-5 Pos: 79-83} | "freefloating": | "Dollar": "$" | "VarName": | [*node.Identifier] | "Position": Pos{Line: 5-5 Pos: 79-83} | "Value": "var" | "DefaultValue": | [*expr.ConstFetch] | "Position": Pos{Line: 5-5 Pos: 86-90} | "freefloating": | "Start": " " | "Constant": | [*name.Name] | "Position": Pos{Line: 5-5 Pos: 86-90} | "NamespacedName": "null" | "Parts": | [*name.NamePart] | "Position": Pos{Line: 5-5 Pos: 86-90} | "Value": "null" | "Stmt": | [*stmt.StmtList] | "Position": Pos{Line: 6-9 Pos: 96-135} | "freefloating": | "Start": "\n\t\t\t\t" | "Stmts": "\n\t\t\t\t" | "Stmts": | [*stmt.Expression] | "Position": Pos{Line: 8-8 Pos: 124-129} | "freefloating": | "SemiColon": ";" | "Start": "\n\t\t\t\t\t" | "Start": "// some comment\n" | "Start": "\t\t\t\t\t" | "Expr": | [*expr.Variable] | "Position": Pos{Line: 8-8 Pos: 124-128} | "freefloating": | "Dollar": "$" | "VarName": | [*node.Identifier] | "Position": Pos{Line: 8-8 Pos: 124-128} | "Value": "var"
func (*Dumper) EnterChildList ¶ added in v0.6.0
func (*Dumper) EnterChildNode ¶ added in v0.6.0
GetChildrenVisitor is invoked at every node parameter that contains children nodes
func (*Dumper) LeaveChildList ¶ added in v0.6.0
func (*Dumper) LeaveChildNode ¶ added in v0.6.0
type GoDumper ¶ added in v0.6.0
GoDumper writes ast hierarchy to an io.Writer as native Golang struct
Example ¶
package main
import (
"os"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
src := `<?php
namespace Foo {
class Bar {
public function FunctionName(Type $var = null)
{
// some comment
$var;
}
}
}`
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.WithFreeFloating()
php7parser.Parse()
nodes := php7parser.GetRootNode()
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := &visitor.GoDumper{
Writer: os.Stdout,
}
nodes.Walk(dumper)
}
Output: &node.Root{ Position: &position.Position{ StartLine: 3, EndLine: 11, StartPos: 9, EndPos: 143, }, Stmts: []node.Node{ &stmt.Namespace{ Position: &position.Position{ StartLine: 3, EndLine: 11, StartPos: 9, EndPos: 143, }, FreeFloating: freefloating.Collection{ "Stmts": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 10, EndLine: 11, StartPos: 139, EndPos: 142, }, Value: "\n\t\t", }, }, "Start": []freefloating.String{ freefloating.String{ Type: freefloating.TokenType, Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 0, EndPos: 5, }, Value: "<?php", }, freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 1, EndLine: 3, StartPos: 5, EndPos: 9, }, Value: "\n\n\t\t", }, }, }, NamespaceName: &name.Name{ Position: &position.Position{ StartLine: 3, EndLine: 3, StartPos: 19, EndPos: 22, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 3, EndLine: 3, StartPos: 18, EndPos: 19, }, Value: " ", }, }, "End": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 3, EndLine: 3, StartPos: 22, EndPos: 23, }, Value: " ", }, }, }, Parts: []node.Node{ &name.NamePart{ Position: &position.Position{ StartLine: 3, EndLine: 3, StartPos: 19, EndPos: 22, }, Value: "Foo", }, }, }, Stmts: []node.Node{ &stmt.Class{ Position: &position.Position{ StartLine: 4, EndLine: 10, StartPos: 28, EndPos: 139, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 3, EndLine: 4, StartPos: 24, EndPos: 28, }, Value: "\n\t\t\t", }, }, "Name": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 4, EndLine: 4, StartPos: 37, EndPos: 38, }, Value: " ", }, }, "Stmts": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 9, EndLine: 10, StartPos: 134, EndPos: 138, }, Value: "\n\t\t\t", }, }, }, PhpDocComment: "", ClassName: &node.Identifier{ Position: &position.Position{ StartLine: 4, EndLine: 4, StartPos: 34, EndPos: 37, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 4, EndLine: 4, StartPos: 33, EndPos: 34, }, Value: " ", }, }, }, Value: "Bar", }, Stmts: []node.Node{ &stmt.ClassMethod{ Position: &position.Position{ StartLine: 5, EndLine: 9, StartPos: 44, EndPos: 134, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 4, EndLine: 5, StartPos: 39, EndPos: 44, }, Value: "\n\t\t\t\t", }, }, "ModifierList": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 50, EndPos: 51, }, Value: " ", }, }, "Function": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 59, EndPos: 60, }, Value: " ", }, }, }, ReturnsRef: false, PhpDocComment: "", MethodName: &node.Identifier{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 60, EndPos: 72, }, Value: "FunctionName", }, Modifiers: []node.Node{ &node.Identifier{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 44, EndPos: 50, }, Value: "public", }, }, Params: []node.Node{ &node.Parameter{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 73, EndPos: 89, }, FreeFloating: freefloating.Collection{ "OptionalType": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 77, EndPos: 78, }, Value: " ", }, }, "Var": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 82, EndPos: 83, }, Value: " ", }, }, }, ByRef: false, Variadic: false, VariableType: &name.Name{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 73, EndPos: 77, }, Parts: []node.Node{ &name.NamePart{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 73, EndPos: 77, }, Value: "Type", }, }, }, Variable: &expr.Variable{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 78, EndPos: 82, }, FreeFloating: freefloating.Collection{ "Dollar": []freefloating.String{ freefloating.String{ Type: freefloating.TokenType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 78, EndPos: 79, }, Value: "$", }, }, }, VarName: &node.Identifier{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 78, EndPos: 82, }, Value: "var", }, }, DefaultValue: &expr.ConstFetch{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 85, EndPos: 89, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 84, EndPos: 85, }, Value: " ", }, }, }, Constant: &name.Name{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 85, EndPos: 89, }, Parts: []node.Node{ &name.NamePart{ Position: &position.Position{ StartLine: 5, EndLine: 5, StartPos: 85, EndPos: 89, }, Value: "null", }, }, }, }, }, }, Stmt: &stmt.StmtList{ Position: &position.Position{ StartLine: 6, EndLine: 9, StartPos: 95, EndPos: 134, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 5, EndLine: 6, StartPos: 90, EndPos: 95, }, Value: "\n\t\t\t\t", }, }, "Stmts": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 8, EndLine: 9, StartPos: 128, EndPos: 133, }, Value: "\n\t\t\t\t", }, }, }, Stmts: []node.Node{ &stmt.Expression{ Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 123, EndPos: 128, }, FreeFloating: freefloating.Collection{ "Start": []freefloating.String{ freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 6, EndLine: 7, StartPos: 96, EndPos: 102, }, Value: "\n\t\t\t\t\t", }, freefloating.String{ Type: freefloating.CommentType, Position: &position.Position{ StartLine: 7, EndLine: 7, StartPos: 102, EndPos: 118, }, Value: "// some comment\n", }, freefloating.String{ Type: freefloating.WhiteSpaceType, Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 118, EndPos: 123, }, Value: "\t\t\t\t\t", }, }, "SemiColon": []freefloating.String{ freefloating.String{ Type: freefloating.TokenType, Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 127, EndPos: 128, }, Value: ";", }, }, }, Expr: &expr.Variable{ Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 123, EndPos: 127, }, FreeFloating: freefloating.Collection{ "Dollar": []freefloating.String{ freefloating.String{ Type: freefloating.TokenType, Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 123, EndPos: 124, }, Value: "$", }, }, }, VarName: &node.Identifier{ Position: &position.Position{ StartLine: 8, EndLine: 8, StartPos: 123, EndPos: 127, }, Value: "var", }, }, }, }, }, }, }, }, }, }, }, }
func (*GoDumper) EnterChildList ¶ added in v0.6.0
func (*GoDumper) EnterChildNode ¶ added in v0.6.0
func (*GoDumper) LeaveChildList ¶ added in v0.6.0
func (*GoDumper) LeaveChildNode ¶ added in v0.6.0
type JsonDumper ¶ added in v0.6.0
type JsonDumper struct {
Writer io.Writer
NsResolver *NamespaceResolver
// contains filtered or unexported fields
}
Example ¶
package main
import (
"os"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
src := `<?php
namespace Foo {
class Bar {
public function FunctionName(Type $var = null)
{
// some comment
// second comment
$var1;
$var2;
}
}
}`
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.WithFreeFloating()
php7parser.Parse()
nodes := php7parser.GetRootNode()
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := &visitor.JsonDumper{
Writer: os.Stdout,
NsResolver: nsResolver,
}
nodes.Walk(dumper)
}
Output: {"type":"*node.Root","position":{"startPos":9,"endPos":179,"startLine":3,"endLine":13},"Stmts":[{"type":"*stmt.Namespace","position":{"startPos":9,"endPos":179,"startLine":3,"endLine":13},"freefloating":{"Start": [{"type":"freefloating.TokenType","value":"<?php"},{"type":"freefloating.WhiteSpaceType","value":"\n\n\t\t"}],"Stmts": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t"}]},"NamespaceName":{"type":"*name.Name","position":{"startPos":19,"endPos":22,"startLine":3,"endLine":3},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}],"End": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Parts":[{"type":"*name.NamePart","position":{"startPos":19,"endPos":22,"startLine":3,"endLine":3},"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":28,"endPos":175,"startLine":4,"endLine":12},"namespacedName":"Foo\\Bar","freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t"}],"Name": [{"type":"freefloating.WhiteSpaceType","value":" "}],"Stmts": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t"}]},"PhpDocComment":"","ClassName":{"type":"*node.Identifier","position":{"startPos":34,"endPos":37,"startLine":4,"endLine":4},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":44,"endPos":170,"startLine":5,"endLine":11},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t\t"}],"Function": [{"type":"freefloating.WhiteSpaceType","value":" "}],"ModifierList": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"PhpDocComment":"","ReturnsRef":false,"MethodName":{"type":"*node.Identifier","position":{"startPos":60,"endPos":72,"startLine":5,"endLine":5},"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":44,"endPos":50,"startLine":5,"endLine":5},"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":73,"endPos":89,"startLine":5,"endLine":5},"freefloating":{"Var": [{"type":"freefloating.WhiteSpaceType","value":" "}],"OptionalType": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"ByRef":false,"Variadic":false,"VariableType":{"type":"*name.Name","position":{"startPos":73,"endPos":77,"startLine":5,"endLine":5},"namespacedName":"Foo\\Type","Parts":[{"type":"*name.NamePart","position":{"startPos":73,"endPos":77,"startLine":5,"endLine":5},"Value":"Type"}]},"Variable":{"type":"*expr.Variable","position":{"startPos":78,"endPos":82,"startLine":5,"endLine":5},"freefloating":{"Dollar": [{"type":"freefloating.TokenType","value":"$"}]},"VarName":{"type":"*node.Identifier","position":{"startPos":78,"endPos":82,"startLine":5,"endLine":5},"Value":"var"}},"DefaultValue":{"type":"*expr.ConstFetch","position":{"startPos":85,"endPos":89,"startLine":5,"endLine":5},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Constant":{"type":"*name.Name","position":{"startPos":85,"endPos":89,"startLine":5,"endLine":5},"namespacedName":"null","Parts":[{"type":"*name.NamePart","position":{"startPos":85,"endPos":89,"startLine":5,"endLine":5},"Value":"null"}]}}}],"Stmt":{"type":"*stmt.StmtList","position":{"startPos":95,"endPos":170,"startLine":6,"endLine":11},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t\t"}],"Stmts": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t\t"}]},"Stmts":[{"type":"*stmt.Expression","position":{"startPos":146,"endPos":152,"startLine":9,"endLine":9},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t\t\t"},{"type":"freefloating.CommentType","value":"// some comment\n"},{"type":"freefloating.WhiteSpaceType","value":"\t\t\t\t\t"},{"type":"freefloating.CommentType","value":"// second comment\n"},{"type":"freefloating.WhiteSpaceType","value":"\t\t\t\t\t"}],"SemiColon": [{"type":"freefloating.TokenType","value":";"}]},"Expr":{"type":"*expr.Variable","position":{"startPos":146,"endPos":151,"startLine":9,"endLine":9},"freefloating":{"Dollar": [{"type":"freefloating.TokenType","value":"$"}]},"VarName":{"type":"*node.Identifier","position":{"startPos":146,"endPos":151,"startLine":9,"endLine":9},"Value":"var1"}}},{"type":"*stmt.Expression","position":{"startPos":158,"endPos":164,"startLine":10,"endLine":10},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":"\n\t\t\t\t\t"}],"SemiColon": [{"type":"freefloating.TokenType","value":";"}]},"Expr":{"type":"*expr.Variable","position":{"startPos":158,"endPos":163,"startLine":10,"endLine":10},"freefloating":{"Dollar": [{"type":"freefloating.TokenType","value":"$"}]},"VarName":{"type":"*node.Identifier","position":{"startPos":158,"endPos":163,"startLine":10,"endLine":10},"Value":"var2"}}}]}}]}]}]}
func (*JsonDumper) EnterChildList ¶ added in v0.6.0
func (d *JsonDumper) EnterChildList(key string, w walker.Walkable)
func (*JsonDumper) EnterChildNode ¶ added in v0.6.0
func (d *JsonDumper) EnterChildNode(key string, w walker.Walkable)
func (*JsonDumper) EnterNode ¶ added in v0.6.0
func (d *JsonDumper) EnterNode(w walker.Walkable) bool
EnterNode is invoked at every node in hierarchy
func (*JsonDumper) LeaveChildList ¶ added in v0.6.0
func (d *JsonDumper) LeaveChildList(key string, w walker.Walkable)
func (*JsonDumper) LeaveChildNode ¶ added in v0.6.0
func (d *JsonDumper) LeaveChildNode(key string, w walker.Walkable)
func (*JsonDumper) LeaveNode ¶ added in v0.6.0
func (d *JsonDumper) LeaveNode(n walker.Walkable)
LeaveNode is invoked after node process
type Namespace ¶ added in v0.3.0
Namespace context
func NewNamespace ¶ added in v0.3.0
NewNamespace constructor
func (*Namespace) ResolveAlias ¶ added in v0.3.0
ResolveAlias returns alias or error if not found
type NamespaceResolver ¶ added in v0.3.0
NamespaceResolver visitor
func NewNamespaceResolver ¶ added in v0.3.0
func NewNamespaceResolver() *NamespaceResolver
NewNamespaceResolver NamespaceResolver type constructor
func (*NamespaceResolver) AddNamespacedName ¶ added in v0.3.0
func (nsr *NamespaceResolver) AddNamespacedName(nn node.Node, nodeName string)
AddNamespacedName adds namespaced name by node
func (*NamespaceResolver) EnterChildList ¶ added in v0.6.0
func (nsr *NamespaceResolver) EnterChildList(key string, w walker.Walkable)
func (*NamespaceResolver) EnterChildNode ¶ added in v0.6.0
func (nsr *NamespaceResolver) EnterChildNode(key string, w walker.Walkable)
func (*NamespaceResolver) EnterNode ¶ added in v0.3.0
func (nsr *NamespaceResolver) EnterNode(w walker.Walkable) bool
EnterNode is invoked at every node in heirerchy
func (*NamespaceResolver) LeaveChildList ¶ added in v0.6.0
func (nsr *NamespaceResolver) LeaveChildList(key string, w walker.Walkable)
func (*NamespaceResolver) LeaveChildNode ¶ added in v0.6.0
func (nsr *NamespaceResolver) LeaveChildNode(key string, w walker.Walkable)
func (*NamespaceResolver) LeaveNode ¶ added in v0.3.0
func (nsr *NamespaceResolver) LeaveNode(w walker.Walkable)
LeaveNode is invoked after node process
func (*NamespaceResolver) ResolveName ¶ added in v0.3.0
func (nsr *NamespaceResolver) ResolveName(nameNode node.Node, aliasType string)
ResolveName adds a resolved fully qualified name by node
func (*NamespaceResolver) ResolveType ¶ added in v0.3.0
func (nsr *NamespaceResolver) ResolveType(n node.Node)
ResolveType adds a resolved fully qualified type name
type PrettyJsonDumper ¶ added in v0.6.0
type PrettyJsonDumper struct {
Writer io.Writer
NsResolver *NamespaceResolver
// contains filtered or unexported fields
}
Example ¶
package main
import (
"os"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
src := `<?php
namespace Foo {
class Bar {
public function FunctionName(Type $var = null)
{
// some comment
// second comment
$var;
}
}
function foo() {
;
}
}
`
php7parser := php7.NewParser([]byte(src), "7.4")
php7parser.WithFreeFloating()
php7parser.Parse()
nodes := php7parser.GetRootNode()
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := visitor.NewPrettyJsonDumper(os.Stdout, nsResolver)
nodes.Walk(dumper)
}
Output: { "type": "*node.Root", "position": { "startPos": 9, "endPos": 198, "startLine": 3, "endLine": 16 }, "freefloating": { "End": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t" } ] }, "Stmts": [ { "type": "*stmt.Namespace", "position": { "startPos": 9, "endPos": 198, "startLine": 3, "endLine": 16 }, "freefloating": { "Start": [ { "type": "freefloating.TokenType", "value": "<?php" }, { "type": "freefloating.WhiteSpaceType", "value": "\n\n\t\t" } ], "Stmts": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t" } ] }, "NamespaceName": { "type": "*name.Name", "position": { "startPos": 19, "endPos": 22, "startLine": 3, "endLine": 3 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ], "End": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "Parts": [ { "type": "*name.NamePart", "position": { "startPos": 19, "endPos": 22, "startLine": 3, "endLine": 3 }, "Value": "Foo" } ] }, "Stmts": [ { "type": "*stmt.Class", "position": { "startPos": 28, "endPos": 162, "startLine": 4, "endLine": 11 }, "namespacedName": "Foo\\Bar", "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t" } ], "Name": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ], "Stmts": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t" } ] }, "PhpDocComment": "", "ClassName": { "type": "*node.Identifier", "position": { "startPos": 34, "endPos": 37, "startLine": 4, "endLine": 4 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "Value": "Bar" }, "Stmts": [ { "type": "*stmt.ClassMethod", "position": { "startPos": 44, "endPos": 157, "startLine": 5, "endLine": 10 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t\t" } ], "ModifierList": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ], "Function": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "ReturnsRef": false, "PhpDocComment": "", "MethodName": { "type": "*node.Identifier", "position": { "startPos": 60, "endPos": 72, "startLine": 5, "endLine": 5 }, "Value": "FunctionName" }, "Modifiers": [ { "type": "*node.Identifier", "position": { "startPos": 44, "endPos": 50, "startLine": 5, "endLine": 5 }, "Value": "public" } ], "Params": [ { "type": "*node.Parameter", "position": { "startPos": 73, "endPos": 89, "startLine": 5, "endLine": 5 }, "freefloating": { "OptionalType": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ], "Var": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "ByRef": false, "Variadic": false, "VariableType": { "type": "*name.Name", "position": { "startPos": 73, "endPos": 77, "startLine": 5, "endLine": 5 }, "namespacedName": "Foo\\Type", "Parts": [ { "type": "*name.NamePart", "position": { "startPos": 73, "endPos": 77, "startLine": 5, "endLine": 5 }, "Value": "Type" } ] }, "Variable": { "type": "*expr.Variable", "position": { "startPos": 78, "endPos": 82, "startLine": 5, "endLine": 5 }, "freefloating": { "Dollar": [ { "type": "freefloating.TokenType", "value": "$" } ] }, "VarName": { "type": "*node.Identifier", "position": { "startPos": 78, "endPos": 82, "startLine": 5, "endLine": 5 }, "Value": "var" } }, "DefaultValue": { "type": "*expr.ConstFetch", "position": { "startPos": 85, "endPos": 89, "startLine": 5, "endLine": 5 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "Constant": { "type": "*name.Name", "position": { "startPos": 85, "endPos": 89, "startLine": 5, "endLine": 5 }, "namespacedName": "null", "Parts": [ { "type": "*name.NamePart", "position": { "startPos": 85, "endPos": 89, "startLine": 5, "endLine": 5 }, "Value": "null" } ] } } } ], "Stmt": { "type": "*stmt.StmtList", "position": { "startPos": 95, "endPos": 157, "startLine": 6, "endLine": 10 }, "freefloating": { "Stmts": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t\t" } ], "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t\t" } ] }, "Stmts": [ { "type": "*stmt.Expression", "position": { "startPos": 146, "endPos": 151, "startLine": 9, "endLine": 9 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t\t\t" }, { "type": "freefloating.CommentType", "value": "// some comment\n" }, { "type": "freefloating.WhiteSpaceType", "value": "\t\t\t\t\t" }, { "type": "freefloating.CommentType", "value": "// second comment\n" }, { "type": "freefloating.WhiteSpaceType", "value": "\t\t\t\t\t" } ], "SemiColon": [ { "type": "freefloating.TokenType", "value": ";" } ] }, "Expr": { "type": "*expr.Variable", "position": { "startPos": 146, "endPos": 150, "startLine": 9, "endLine": 9 }, "freefloating": { "Dollar": [ { "type": "freefloating.TokenType", "value": "$" } ] }, "VarName": { "type": "*node.Identifier", "position": { "startPos": 146, "endPos": 150, "startLine": 9, "endLine": 9 }, "Value": "var" } } } ] } } ] }, { "type": "*stmt.Function", "position": { "startPos": 167, "endPos": 194, "startLine": 13, "endLine": 15 }, "namespacedName": "Foo\\foo", "freefloating": { "Params": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ], "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\n\t\t\t" } ], "Stmts": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t" } ] }, "ReturnsRef": false, "PhpDocComment": "", "FunctionName": { "type": "*node.Identifier", "position": { "startPos": 176, "endPos": 179, "startLine": 13, "endLine": 13 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": " " } ] }, "Value": "foo" }, "Stmts": [ { "type": "*stmt.Nop", "position": { "startPos": 188, "endPos": 189, "startLine": 14, "endLine": 14 }, "freefloating": { "Start": [ { "type": "freefloating.WhiteSpaceType", "value": "\n\t\t\t\t" } ], "SemiColon": [ { "type": "freefloating.TokenType", "value": ";" } ] } } ] } ] } ] }
func NewPrettyJsonDumper ¶ added in v0.6.0
func NewPrettyJsonDumper(Writer io.Writer, NsResolver *NamespaceResolver) *PrettyJsonDumper
func (*PrettyJsonDumper) EnterChildList ¶ added in v0.6.0
func (d *PrettyJsonDumper) EnterChildList(key string, w walker.Walkable)
func (*PrettyJsonDumper) EnterChildNode ¶ added in v0.6.0
func (d *PrettyJsonDumper) EnterChildNode(key string, w walker.Walkable)
func (*PrettyJsonDumper) EnterNode ¶ added in v0.6.0
func (d *PrettyJsonDumper) EnterNode(w walker.Walkable) bool
EnterNode is invoked at every node in hierarchy
func (*PrettyJsonDumper) LeaveChildList ¶ added in v0.6.0
func (d *PrettyJsonDumper) LeaveChildList(key string, w walker.Walkable)
func (*PrettyJsonDumper) LeaveChildNode ¶ added in v0.6.0
func (d *PrettyJsonDumper) LeaveChildNode(key string, w walker.Walkable)
func (*PrettyJsonDumper) LeaveNode ¶ added in v0.6.0
func (d *PrettyJsonDumper) LeaveNode(n walker.Walkable)
LeaveNode is invoked after node process