visitor

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2018 License: MIT Imports: 12 Imported by: 0

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   comment.Comments
	Positions  position.Positions
	NsResolver *NamespaceResolver
}

Dumper prints ast hierarchy to stdout 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;
				}
			}
		}`

	nodes, comments, positions := php7.Parse(bytes.NewBufferString(src), "test.php")

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

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

}
Output:

| [*stmt.StmtList]
|   "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};
|               "PhpDocComment": ;
|               "ReturnsRef": false;
|               "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;
|               "Stmts":
|                 [*stmt.Expression]
|                   "Position": Pos{Line: 8-8 Pos: 124-128};
|                   "Comments":
|                     "// some comment\n"
|                   "Expr":
|                     [*expr.Variable]
|                       "Position": Pos{Line: 8-8 Pos: 124-127};
|                       "Comments":
|                         "// some comment\n"
|                       "VarName":
|                         [*node.Identifier]
|                           "Position": Pos{Line: 8-8 Pos: 124-127};
|                           "Value": var;
|                           "Comments":
|                             "// some comment\n"

func (Dumper) EnterNode

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

EnterNode is invoked at every node in heirerchy

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

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

Namespace context

func NewNamespace

func NewNamespace(NSName string) *Namespace

NewNamespace constructor

func (*Namespace) AddAlias

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

AddAlias adds a new alias

func (*Namespace) ResolveAlias

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

ResolveAlias returns alias or error if not found

func (*Namespace) ResolveName

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

ResolveName returns a resolved fully qualified name

type NamespaceResolver

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

NamespaceResolver visitor

func NewNamespaceResolver

func NewNamespaceResolver() *NamespaceResolver

NewNamespaceResolver NamespaceResolver type constructor

func (*NamespaceResolver) AddAlias

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

AddAlias adds a new alias

func (*NamespaceResolver) AddNamespacedName

func (nsr *NamespaceResolver) AddNamespacedName(nn node.Node, nodeName string)

AddNamespacedName adds namespaced name by node

func (*NamespaceResolver) EnterNode

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

EnterNode is invoked at every node in heirerchy

func (*NamespaceResolver) GetChildrenVisitor

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

GetChildrenVisitor is invoked at every node parameter that contains children nodes

func (*NamespaceResolver) LeaveNode

func (nsr *NamespaceResolver) LeaveNode(w walker.Walkable)

LeaveNode is invoked after node process

func (*NamespaceResolver) ResolveName

func (nsr *NamespaceResolver) ResolveName(nameNode node.Node, aliasType string)

ResolveName adds a resolved fully qualified name by node

func (*NamespaceResolver) ResolveType

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