visitor

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2018 License: MIT Imports: 12 Imported by: 9

Documentation

Overview

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
	Comments   parser.Comments
	Positions  parser.Positions
	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.Parse()
	nodes := php7parser.GetRootNode()

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

	dumper := visitor.Dumper{
		Writer:     os.Stdout,
		Indent:     "| ",
		Comments:   php7parser.GetComments(),
		Positions:  php7parser.GetPositions(),
		NsResolver: nsResolver,
	}
	nodes.Walk(dumper)

}
Output:

| [*node.Root]
|   "Position": Pos{Line: 3-11 Pos: 10-143};
|   "Stmts":
|     [*stmt.Namespace]
|       "Position": Pos{Line: 3-11 Pos: 10-143};
|       "NamespaceName":
|         [*name.Name]
|           "Position": Pos{Line: 3-3 Pos: 20-22};
|           "Parts":
|             [*name.NamePart]
|               "Position": Pos{Line: 3-3 Pos: 20-22};
|               "Value": Foo;
|       "Stmts":
|         [*stmt.Class]
|           "Position": Pos{Line: 4-10 Pos: 29-139};
|           "NamespacedName": Foo\Bar;
|           "PhpDocComment": ;
|           "ClassName":
|             [*node.Identifier]
|               "Position": Pos{Line: 4-4 Pos: 35-37};
|               "Value": Bar;
|           "Stmts":
|             [*stmt.ClassMethod]
|               "Position": Pos{Line: 5-9 Pos: 45-134};
|               "ReturnsRef": false;
|               "PhpDocComment": ;
|               "MethodName":
|                 [*node.Identifier]
|                   "Position": Pos{Line: 5-5 Pos: 61-72};
|                   "Value": FunctionName;
|               "Modifiers":
|                 [*node.Identifier]
|                   "Position": Pos{Line: 5-5 Pos: 45-50};
|                   "Value": public;
|               "Params":
|                 [*node.Parameter]
|                   "Position": Pos{Line: 5-5 Pos: 74-89};
|                   "ByRef": false;
|                   "Variadic": false;
|                   "VariableType":
|                     [*name.Name]
|                       "Position": Pos{Line: 5-5 Pos: 74-77};
|                       "NamespacedName": Foo\Type;
|                       "Parts":
|                         [*name.NamePart]
|                           "Position": Pos{Line: 5-5 Pos: 74-77};
|                           "Value": Type;
|                   "Variable":
|                     [*expr.Variable]
|                       "Position": Pos{Line: 5-5 Pos: 79-82};
|                       "VarName":
|                         [*node.Identifier]
|                           "Position": Pos{Line: 5-5 Pos: 79-82};
|                           "Value": var;
|                   "DefaultValue":
|                     [*expr.ConstFetch]
|                       "Position": Pos{Line: 5-5 Pos: 86-89};
|                       "Constant":
|                         [*name.Name]
|                           "Position": Pos{Line: 5-5 Pos: 86-89};
|                           "NamespacedName": Foo\null;
|                           "Parts":
|                             [*name.NamePart]
|                               "Position": Pos{Line: 5-5 Pos: 86-89};
|                               "Value": null;
|               "Stmt":
|                 [*stmt.StmtList]
|                   "Position": Pos{Line: 6-9 Pos: 96-134};
|                   "Stmts":
|                     [*stmt.Expression]
|                       "Position": Pos{Line: 8-8 Pos: 124-128};
|                       "Expr":
|                         [*expr.Variable]
|                           "Position": Pos{Line: 8-8 Pos: 124-127};
|                           "Comments":
|                             "// some comment\n" before "VariableToken"
|                           "VarName":
|                             [*node.Identifier]
|                               "Position": Pos{Line: 8-8 Pos: 124-127};
|                               "Value": var;

func (Dumper) EnterNode

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

EnterNode is invoked at every node in hierarchy

func (Dumper) GetChildrenVisitor

func (d Dumper) GetChildrenVisitor(key string) walker.Visitor

GetChildrenVisitor is invoked at every node parameter that contains children nodes

func (Dumper) LeaveNode

func (d Dumper) 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) EnterNode added in v0.3.0

func (nsr *NamespaceResolver) EnterNode(w walker.Walkable) bool

EnterNode is invoked at every node in heirerchy

func (*NamespaceResolver) GetChildrenVisitor added in v0.3.0

func (nsr *NamespaceResolver) GetChildrenVisitor(key string) walker.Visitor

GetChildrenVisitor is invoked at every node parameter that contains children nodes

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

Jump to

Keyboard shortcuts

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