visitor

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2019 License: MIT Imports: 12 Imported by: 9

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

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 (
	"bytes"
	"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(bytes.NewBufferString(src), "test.php")
	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: 10-144}
|   "Stmts":
|     [*stmt.Namespace]
|       "Position": Pos{Line: 3-11 Pos: 10-144}
|       "freefloating":
|         "Start": "<?php"
|         "Start": "\n\n\t\t"
|         "Stmts": "\n\t\t"
|       "NamespaceName":
|         [*name.Name]
|           "Position": Pos{Line: 3-3 Pos: 20-22}
|           "freefloating":
|             "Start": " "
|             "End": " "
|           "Parts":
|             [*name.NamePart]
|               "Position": Pos{Line: 3-3 Pos: 20-22}
|               "Value": "Foo"
|       "Stmts":
|         [*stmt.Class]
|           "Position": Pos{Line: 4-10 Pos: 29-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: 35-37}
|               "freefloating":
|                 "Start": " "
|               "Value": "Bar"
|           "Stmts":
|             [*stmt.ClassMethod]
|               "Position": Pos{Line: 5-9 Pos: 46-135}
|               "freefloating":
|                 "Start": " \n\t\t\t\t"
|                 "ModifierList": " "
|                 "Function": " "
|               "ReturnsRef": false
|               "PhpDocComment": ""
|               "MethodName":
|                 [*node.Identifier]
|                   "Position": Pos{Line: 5-5 Pos: 62-73}
|                   "Value": "FunctionName"
|               "Modifiers":
|                 [*node.Identifier]
|                   "Position": Pos{Line: 5-5 Pos: 46-51}
|                   "Value": "public"
|               "Params":
|                 [*node.Parameter]
|                   "Position": Pos{Line: 5-5 Pos: 75-90}
|                   "freefloating":
|                     "OptionalType": " "
|                     "Var": " "
|                   "Variadic": false
|                   "ByRef": false
|                   "VariableType":
|                     [*name.Name]
|                       "Position": Pos{Line: 5-5 Pos: 75-78}
|                       "NamespacedName": "Foo\\Type"
|                       "Parts":
|                         [*name.NamePart]
|                           "Position": Pos{Line: 5-5 Pos: 75-78}
|                           "Value": "Type"
|                   "Variable":
|                     [*expr.Variable]
|                       "Position": Pos{Line: 5-5 Pos: 80-83}
|                       "freefloating":
|                         "Dollar": "$"
|                       "VarName":
|                         [*node.Identifier]
|                           "Position": Pos{Line: 5-5 Pos: 80-83}
|                           "Value": "var"
|                   "DefaultValue":
|                     [*expr.ConstFetch]
|                       "Position": Pos{Line: 5-5 Pos: 87-90}
|                       "freefloating":
|                         "Start": " "
|                       "Constant":
|                         [*name.Name]
|                           "Position": Pos{Line: 5-5 Pos: 87-90}
|                           "NamespacedName": "null"
|                           "Parts":
|                             [*name.NamePart]
|                               "Position": Pos{Line: 5-5 Pos: 87-90}
|                               "Value": "null"
|               "Stmt":
|                 [*stmt.StmtList]
|                   "Position": Pos{Line: 6-9 Pos: 97-135}
|                   "freefloating":
|                     "Start": "\n\t\t\t\t"
|                     "Stmts": "\n\t\t\t\t"
|                   "Stmts":
|                     [*stmt.Expression]
|                       "Position": Pos{Line: 8-8 Pos: 125-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: 125-128}
|                           "freefloating":
|                             "Dollar": "$"
|                           "VarName":
|                             [*node.Identifier]
|                               "Position": Pos{Line: 8-8 Pos: 125-128}
|                               "Value": "var"

func (*Dumper) EnterChildList added in v0.6.0

func (d *Dumper) EnterChildList(key string, w walker.Walkable)

func (*Dumper) EnterChildNode added in v0.6.0

func (d *Dumper) EnterChildNode(key string, w walker.Walkable)

GetChildrenVisitor is invoked at every node parameter that contains children nodes

func (*Dumper) EnterNode

func (d *Dumper) EnterNode(w walker.Walkable) bool

EnterNode is invoked at every node in hierarchy

func (*Dumper) LeaveChildList added in v0.6.0

func (d *Dumper) LeaveChildList(key string, w walker.Walkable)

func (*Dumper) LeaveChildNode added in v0.6.0

func (d *Dumper) LeaveChildNode(key string, w walker.Walkable)

func (*Dumper) LeaveNode

func (d *Dumper) LeaveNode(n walker.Walkable)

LeaveNode is invoked after node process

type GoDumper added in v0.6.0

type GoDumper struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

GoDumper writes ast hierarchy to an io.Writer as native Golang struct

Example
package main

import (
	"bytes"
	"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(bytes.NewBufferString(src), "test.php")
	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: 10,
		EndPos: 143,
	},
	Stmts: []node.Node{
		&stmt.Namespace{
			Position: &position.Position{
				StartLine: 3,
				EndLine: 11,
				StartPos: 10,
				EndPos: 143,
			},
			FreeFloating: freefloating.Collection{
				"Stmts": []freefloating.String{
					freefloating.String{
						Type: freefloating.WhiteSpaceType,
						Position: &position.Position{
							StartLine: 10,
							EndLine: 11,
							StartPos: 140,
							EndPos: 142,
						},
						Value: "\n\t\t",
					},
				},
				"Start": []freefloating.String{
					freefloating.String{
						Type: freefloating.TokenType,
						Position: &position.Position{
							StartLine: 1,
							EndLine: 1,
							StartPos: 1,
							EndPos: 5,
						},
						Value: "<?php",
					},
					freefloating.String{
						Type: freefloating.WhiteSpaceType,
						Position: &position.Position{
							StartLine: 1,
							EndLine: 3,
							StartPos: 6,
							EndPos: 9,
						},
						Value: "\n\n\t\t",
					},
				},
			},
			NamespaceName: &name.Name{
				Position: &position.Position{
					StartLine: 3,
					EndLine: 3,
					StartPos: 20,
					EndPos: 22,
				},
				FreeFloating: freefloating.Collection{
					"Start": []freefloating.String{
						freefloating.String{
							Type: freefloating.WhiteSpaceType,
							Position: &position.Position{
								StartLine: 3,
								EndLine: 3,
								StartPos: 19,
								EndPos: 19,
							},
							Value: " ",
						},
					},
					"End": []freefloating.String{
						freefloating.String{
							Type: freefloating.WhiteSpaceType,
							Position: &position.Position{
								StartLine: 3,
								EndLine: 3,
								StartPos: 23,
								EndPos: 23,
							},
							Value: " ",
						},
					},
				},
				Parts: []node.Node{
					&name.NamePart{
						Position: &position.Position{
							StartLine: 3,
							EndLine: 3,
							StartPos: 20,
							EndPos: 22,
						},
						Value: "Foo",
					},
				},
			},
			Stmts: []node.Node{
				&stmt.Class{
					Position: &position.Position{
						StartLine: 4,
						EndLine: 10,
						StartPos: 29,
						EndPos: 139,
					},
					FreeFloating: freefloating.Collection{
						"Start": []freefloating.String{
							freefloating.String{
								Type: freefloating.WhiteSpaceType,
								Position: &position.Position{
									StartLine: 3,
									EndLine: 4,
									StartPos: 25,
									EndPos: 28,
								},
								Value: "\n\t\t\t",
							},
						},
						"Name": []freefloating.String{
							freefloating.String{
								Type: freefloating.WhiteSpaceType,
								Position: &position.Position{
									StartLine: 4,
									EndLine: 4,
									StartPos: 38,
									EndPos: 38,
								},
								Value: " ",
							},
						},
						"Stmts": []freefloating.String{
							freefloating.String{
								Type: freefloating.WhiteSpaceType,
								Position: &position.Position{
									StartLine: 9,
									EndLine: 10,
									StartPos: 135,
									EndPos: 138,
								},
								Value: "\n\t\t\t",
							},
						},
					},
					PhpDocComment: "",
					ClassName: &node.Identifier{
						Position: &position.Position{
							StartLine: 4,
							EndLine: 4,
							StartPos: 35,
							EndPos: 37,
						},
						FreeFloating: freefloating.Collection{
							"Start": []freefloating.String{
								freefloating.String{
									Type: freefloating.WhiteSpaceType,
									Position: &position.Position{
										StartLine: 4,
										EndLine: 4,
										StartPos: 34,
										EndPos: 34,
									},
									Value: " ",
								},
							},
						},
						Value: "Bar",
					},
					Stmts: []node.Node{
						&stmt.ClassMethod{
							Position: &position.Position{
								StartLine: 5,
								EndLine: 9,
								StartPos: 45,
								EndPos: 134,
							},
							FreeFloating: freefloating.Collection{
								"Start": []freefloating.String{
									freefloating.String{
										Type: freefloating.WhiteSpaceType,
										Position: &position.Position{
											StartLine: 4,
											EndLine: 5,
											StartPos: 40,
											EndPos: 44,
										},
										Value: "\n\t\t\t\t",
									},
								},
								"ModifierList": []freefloating.String{
									freefloating.String{
										Type: freefloating.WhiteSpaceType,
										Position: &position.Position{
											StartLine: 5,
											EndLine: 5,
											StartPos: 51,
											EndPos: 51,
										},
										Value: " ",
									},
								},
								"Function": []freefloating.String{
									freefloating.String{
										Type: freefloating.WhiteSpaceType,
										Position: &position.Position{
											StartLine: 5,
											EndLine: 5,
											StartPos: 60,
											EndPos: 60,
										},
										Value: " ",
									},
								},
							},
							ReturnsRef: false,
							PhpDocComment: "",
							MethodName: &node.Identifier{
								Position: &position.Position{
									StartLine: 5,
									EndLine: 5,
									StartPos: 61,
									EndPos: 72,
								},
								Value: "FunctionName",
							},
							Modifiers: []node.Node{
								&node.Identifier{
									Position: &position.Position{
										StartLine: 5,
										EndLine: 5,
										StartPos: 45,
										EndPos: 50,
									},
									Value: "public",
								},
							},
							Params: []node.Node{
								&node.Parameter{
									Position: &position.Position{
										StartLine: 5,
										EndLine: 5,
										StartPos: 74,
										EndPos: 89,
									},
									FreeFloating: freefloating.Collection{
										"OptionalType": []freefloating.String{
											freefloating.String{
												Type: freefloating.WhiteSpaceType,
												Position: &position.Position{
													StartLine: 5,
													EndLine: 5,
													StartPos: 78,
													EndPos: 78,
												},
												Value: " ",
											},
										},
										"Var": []freefloating.String{
											freefloating.String{
												Type: freefloating.WhiteSpaceType,
												Position: &position.Position{
													StartLine: 5,
													EndLine: 5,
													StartPos: 83,
													EndPos: 83,
												},
												Value: " ",
											},
										},
									},
									ByRef: false,
									Variadic: false,
									VariableType: &name.Name{
										Position: &position.Position{
											StartLine: 5,
											EndLine: 5,
											StartPos: 74,
											EndPos: 77,
										},
										Parts: []node.Node{
											&name.NamePart{
												Position: &position.Position{
													StartLine: 5,
													EndLine: 5,
													StartPos: 74,
													EndPos: 77,
												},
												Value: "Type",
											},
										},
									},
									Variable: &expr.Variable{
										Position: &position.Position{
											StartLine: 5,
											EndLine: 5,
											StartPos: 79,
											EndPos: 82,
										},
										FreeFloating: freefloating.Collection{
											"Dollar": []freefloating.String{
												freefloating.String{
													Type: freefloating.TokenType,
													Position: &position.Position{
														StartLine: 5,
														EndLine: 5,
														StartPos: 79,
														EndPos: 80,
													},
													Value: "$",
												},
											},
										},
										VarName: &node.Identifier{
											Position: &position.Position{
												StartLine: 5,
												EndLine: 5,
												StartPos: 79,
												EndPos: 82,
											},
											Value: "var",
										},
									},
									DefaultValue: &expr.ConstFetch{
										Position: &position.Position{
											StartLine: 5,
											EndLine: 5,
											StartPos: 86,
											EndPos: 89,
										},
										FreeFloating: freefloating.Collection{
											"Start": []freefloating.String{
												freefloating.String{
													Type: freefloating.WhiteSpaceType,
													Position: &position.Position{
														StartLine: 5,
														EndLine: 5,
														StartPos: 85,
														EndPos: 85,
													},
													Value: " ",
												},
											},
										},
										Constant: &name.Name{
											Position: &position.Position{
												StartLine: 5,
												EndLine: 5,
												StartPos: 86,
												EndPos: 89,
											},
											Parts: []node.Node{
												&name.NamePart{
													Position: &position.Position{
														StartLine: 5,
														EndLine: 5,
														StartPos: 86,
														EndPos: 89,
													},
													Value: "null",
												},
											},
										},
									},
								},
							},
							Stmt: &stmt.StmtList{
								Position: &position.Position{
									StartLine: 6,
									EndLine: 9,
									StartPos: 96,
									EndPos: 134,
								},
								FreeFloating: freefloating.Collection{
									"Stmts": []freefloating.String{
										freefloating.String{
											Type: freefloating.WhiteSpaceType,
											Position: &position.Position{
												StartLine: 8,
												EndLine: 9,
												StartPos: 129,
												EndPos: 133,
											},
											Value: "\n\t\t\t\t",
										},
									},
									"Start": []freefloating.String{
										freefloating.String{
											Type: freefloating.WhiteSpaceType,
											Position: &position.Position{
												StartLine: 5,
												EndLine: 6,
												StartPos: 91,
												EndPos: 95,
											},
											Value: "\n\t\t\t\t",
										},
									},
								},
								Stmts: []node.Node{
									&stmt.Expression{
										Position: &position.Position{
											StartLine: 8,
											EndLine: 8,
											StartPos: 124,
											EndPos: 128,
										},
										FreeFloating: freefloating.Collection{
											"Start": []freefloating.String{
												freefloating.String{
													Type: freefloating.WhiteSpaceType,
													Position: &position.Position{
														StartLine: 6,
														EndLine: 7,
														StartPos: 97,
														EndPos: 102,
													},
													Value: "\n\t\t\t\t\t",
												},
												freefloating.String{
													Type: freefloating.CommentType,
													Position: &position.Position{
														StartLine: 7,
														EndLine: 7,
														StartPos: 103,
														EndPos: 118,
													},
													Value: "// some comment\n",
												},
												freefloating.String{
													Type: freefloating.WhiteSpaceType,
													Position: &position.Position{
														StartLine: 8,
														EndLine: 8,
														StartPos: 119,
														EndPos: 123,
													},
													Value: "\t\t\t\t\t",
												},
											},
											"SemiColon": []freefloating.String{
												freefloating.String{
													Type: freefloating.TokenType,
													Position: &position.Position{
														StartLine: 8,
														EndLine: 8,
														StartPos: 128,
														EndPos: 128,
													},
													Value: ";",
												},
											},
										},
										Expr: &expr.Variable{
											Position: &position.Position{
												StartLine: 8,
												EndLine: 8,
												StartPos: 124,
												EndPos: 127,
											},
											FreeFloating: freefloating.Collection{
												"Dollar": []freefloating.String{
													freefloating.String{
														Type: freefloating.TokenType,
														Position: &position.Position{
															StartLine: 8,
															EndLine: 8,
															StartPos: 124,
															EndPos: 125,
														},
														Value: "$",
													},
												},
											},
											VarName: &node.Identifier{
												Position: &position.Position{
													StartLine: 8,
													EndLine: 8,
													StartPos: 124,
													EndPos: 127,
												},
												Value: "var",
											},
										},
									},
								},
							},
						},
					},
				},
			},
		},
	},
}

func (*GoDumper) EnterChildList added in v0.6.0

func (d *GoDumper) EnterChildList(key string, w walker.Walkable)

func (*GoDumper) EnterChildNode added in v0.6.0

func (d *GoDumper) EnterChildNode(key string, w walker.Walkable)

func (*GoDumper) EnterNode added in v0.6.0

func (d *GoDumper) EnterNode(w walker.Walkable) bool

EnterNode is invoked at every node in hierarchy

func (*GoDumper) LeaveChildList added in v0.6.0

func (d *GoDumper) LeaveChildList(key string, w walker.Walkable)

func (*GoDumper) LeaveChildNode added in v0.6.0

func (d *GoDumper) LeaveChildNode(key string, w walker.Walkable)

func (*GoDumper) LeaveNode added in v0.6.0

func (d *GoDumper) LeaveNode(n walker.Walkable)

LeaveNode is invoked after node process

type JsonDumper added in v0.6.0

type JsonDumper struct {
	Writer     io.Writer
	NsResolver *NamespaceResolver
}
Example
package main

import (
	"bytes"
	"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;
				}
			}
		}`

	php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
	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":10,"endPos":166,"startLine":3,"endLine":12},"Stmts":[{"type":"*stmt.Namespace","position":{"startPos":10,"endPos":166,"startLine":3,"endLine":12},"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":20,"endPos":22,"startLine":3,"endLine":3},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}],"End": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Parts":[{"type":"*name.NamePart","position":{"startPos":20,"endPos":22,"startLine":3,"endLine":3},"Value":"Foo"}]},"Stmts":[{"type":"*stmt.Class","position":{"startPos":29,"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":35,"endPos":37,"startLine":4,"endLine":4},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Value":"Bar"},"Stmts":[{"type":"*stmt.ClassMethod","position":{"startPos":45,"endPos":157,"startLine":5,"endLine":10},"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":61,"endPos":72,"startLine":5,"endLine":5},"Value":"FunctionName"},"Modifiers":[{"type":"*node.Identifier","position":{"startPos":45,"endPos":50,"startLine":5,"endLine":5},"Value":"public"}],"Params":[{"type":"*node.Parameter","position":{"startPos":74,"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":74,"endPos":77,"startLine":5,"endLine":5},"namespacedName":"Foo\\Type","Parts":[{"type":"*name.NamePart","position":{"startPos":74,"endPos":77,"startLine":5,"endLine":5},"Value":"Type"}]},"Variable":{"type":"*expr.Variable","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"freefloating":{"Dollar": [{"type":"freefloating.TokenType","value":"$"}]},"VarName":{"type":"*node.Identifier","position":{"startPos":79,"endPos":82,"startLine":5,"endLine":5},"Value":"var"}},"DefaultValue":{"type":"*expr.ConstFetch","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"freefloating":{"Start": [{"type":"freefloating.WhiteSpaceType","value":" "}]},"Constant":{"type":"*name.Name","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"namespacedName":"null","Parts":[{"type":"*name.NamePart","position":{"startPos":86,"endPos":89,"startLine":5,"endLine":5},"Value":"null"}]}}}],"Stmt":{"type":"*stmt.StmtList","position":{"startPos":96,"endPos":157,"startLine":6,"endLine":10},"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":147,"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":147,"endPos":150,"startLine":9,"endLine":9},"freefloating":{"Dollar": [{"type":"freefloating.TokenType","value":"$"}]},"VarName":{"type":"*node.Identifier","position":{"startPos":147,"endPos":150,"startLine":9,"endLine":9},"Value":"var"}}}]}}]}]}]}

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

type Namespace struct {
	Namespace string
	Aliases   map[string]map[string]string
}

Namespace context

func NewNamespace added in v0.3.0

func NewNamespace(NSName string) *Namespace

NewNamespace constructor

func (*Namespace) AddAlias added in v0.3.0

func (ns *Namespace) AddAlias(aliasType string, aliasName string, alias string)

AddAlias adds a new alias

func (*Namespace) ResolveAlias added in v0.3.0

func (ns *Namespace) ResolveAlias(nameNode node.Node, aliasType string) (string, error)

ResolveAlias returns alias or error if not found

func (*Namespace) ResolveName added in v0.3.0

func (ns *Namespace) ResolveName(nameNode node.Node, aliasType string) (string, error)

ResolveName returns a resolved fully qualified name

type NamespaceResolver added in v0.3.0

type NamespaceResolver struct {
	Namespace     *Namespace
	ResolvedNames map[node.Node]string
}

NamespaceResolver visitor

func NewNamespaceResolver added in v0.3.0

func NewNamespaceResolver() *NamespaceResolver

NewNamespaceResolver NamespaceResolver type constructor

func (*NamespaceResolver) AddAlias added in v0.3.0

func (nsr *NamespaceResolver) AddAlias(useType string, nn node.Node, prefix []node.Node)

AddAlias adds a new alias

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 (
	"bytes"
	"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(bytes.NewBufferString(src), "test.php")
	php7parser.WithFreeFloating()
	php7parser.Parse()
	nodes := php7parser.GetRootNode()

	nsResolver := visitor.NewNamespaceResolver()
	nodes.Walk(nsResolver)

	dumper := visitor.NewPrettyJsonDumper(os.Stdout, nsResolver)
	nodes.Walk(dumper)

	
Output:

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL