gorewrite

package module
v0.0.0-...-776af3d Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2015 License: BSD-3-Clause Imports: 2 Imported by: 7

README

gorewrite

Go has pretty amazing tooling to parse Go code and pretty-print the AST back into the original code. It also has a utility to visit an AST and modify parts of it as you Go, using ast.Walk() with a Visitor.

This is fine when you just to tweak the contents of existing AST nodes, or change the sub-nodes of a given node, but it doesn't let you replace an existing node with another one. This is needed, for instance, if you want to go from one type of Node to another (e.g. when you want to rewrite one type of expression as another type of expression).

gorewrite gives you the exact same thing as Walk() with a Visitor except that your Visitor, called a Rewriter can return a modified ast.Node, and that you need to call Rewrite() instead of Walk().

Documentation

Overview

Copyright 2009 The Go Authors. All rights reserved. Copyright 2015 Benoit Sigoure. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rewrite

func Rewrite(v Rewriter, node Node) (rewritten Node)

Rewrite traverses an AST in depth-first order: It starts by calling v.Rewrite(node); node must not be nil. If the visitor w returned by v.Rewrite(node) is not nil, Rewrite is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Rewrite(nil).

Types

type Rewriter

type Rewriter interface {
	Rewrite(node Node) (rewritten Node, w Rewriter)
}

A Rewriter's Rewrite method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Rewrite(nil).

Jump to

Keyboard shortcuts

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