Documentation ¶
Overview ¶
Package astfmt implements `ast.Node` formatting with fmt-like API.
Example ¶
package main import ( "go/token" "os" "github.com/go-toolsmith/astfmt" "github.com/go-toolsmith/strparse" ) func main() { x := strparse.Expr(`foo(bar(baz(1+2)))`) astfmt.Println(x) // => foo(bar(baz(1 + 2))) astfmt.Fprintf(os.Stdout, "node=%s\n", x) // => node=foo(bar(baz(1 + 2))) // Can use specific file set with printer. fset := token.NewFileSet() // Suppose this fset is used when parsing pp := astfmt.NewPrinter(fset) pp.Println(x) // => foo(bar(baz(1 + 2))) }
Output: foo(bar(baz(1 + 2))) node=foo(bar(baz(1 + 2))) foo(bar(baz(1 + 2)))
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fprintf ¶
Fprintf calls fmt.Fprintf with additional support of %s format for ast.Node arguments.
Uses empty file set for AST printing.
func Println ¶
func Println(args ...interface{}) error
Println calls fmt.Println with additional support of %s format for ast.Node arguments.
Uses empty file set for AST printing.
Types ¶
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer provides API close to fmt package for printing AST nodes. Unlike freestanding functions from this package, it makes it possible to associate appropriate file set for better output.
func NewPrinter ¶
NewPrinter returns printer that uses bound file set when printing AST nodes.
func (*Printer) Fprintf ¶
Fprintf printer method is like Fprintf function, but uses bound file set when printing.
func (*Printer) Println ¶
Println printer method is like Println function, but uses bound file set when printing.