astfmt: github.com/go-toolsmith/astfmt Index | Examples | Files

package astfmt

import "github.com/go-toolsmith/astfmt"

Package astfmt implements `ast.Node` formatting with fmt-like API.

Code:

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

Package Files

astfmt.go

func Fprintf Uses

func Fprintf(w io.Writer, format string, args ...interface{}) error

Fprintf calls fmt.Fprintf with additional support of %s format for ast.Node arguments.

Uses empty file set for AST printing.

func Println Uses

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.

func Sprint Uses

func Sprint(args ...interface{}) string

Sprint calls fmt.Sprint with additional support of %s format for ast.Node arguments.

Uses empty file set for AST printing.

func Sprintf Uses

func Sprintf(format string, args ...interface{}) string

Sprintf calls fmt.Sprintf with additional support of %s format for ast.Node arguments.

Uses empty file set for AST printing.

type Printer Uses

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 Uses

func NewPrinter(fset *token.FileSet) *Printer

NewPrinter returns printer that uses bound file set when printing AST nodes.

func (*Printer) Fprintf Uses

func (p *Printer) Fprintf(w io.Writer, format string, args ...interface{}) error

Fprintf printer method is like Fprintf function, but uses bound file set when printing.

func (*Printer) Println Uses

func (p *Printer) Println(args ...interface{}) error

Println printer method is like Println function, but uses bound file set when printing.

func (*Printer) Sprint Uses

func (p *Printer) Sprint(args ...interface{}) string

Sprint printer method is like Sprint function, but uses bound file set when printing.

func (*Printer) Sprintf Uses

func (p *Printer) Sprintf(format string, args ...interface{}) string

Sprintf printer method is like Sprintf function, but uses bound file set when printing.

Package astfmt imports 6 packages (graph) and is imported by 1 packages. Updated 2018-06-16. Refresh now. Tools for package owners.