astfmt

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 6 Imported by: 10

README

astfmt

build-img pkg-img reportcard-img version-img

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

Installation

Go version 1.16+

go get github.com/go-toolsmith/astfmt

Example

package main

import (
	"go/token"
	"os"

	"github.com/go-toolsmith/astfmt"
	"github.com/go-toolsmith/strparse"
)

func Example() {
	x := strparse.Expr(`foo(bar(baz(1+2)))`)
	// astfmt functions add %s support for ast.Node arguments.
	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)))
}

License

MIT License.

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

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

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

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

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.

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

func NewPrinter(fset *token.FileSet) *Printer

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

func (*Printer) Fprintf

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

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

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

func (*Printer) Sprint

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

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

func (*Printer) Sprintf

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

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

Jump to

Keyboard shortcuts

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