printer

package
v1.23.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

printerパッケージは、ASTノードの印刷を実装しています。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(output io.Writer, fset *token.FileSet, node any) error

FprintはASTノードを出力に「整形表示」します。 それはデフォルトの設定でConfig.Fprintを呼び出します。 gofmt はインデントにタブを使用し、整列にはスペースを使用することに注意してください。 gofmtと一致する出力にはformat.Node(パッケージgo/format)を使用してください。

Example
printSelf()
Output:

funcAST, fset := parseFunc("example_test.go", "printSelf")

var buf bytes.Buffer
printer.Fprint(&buf, fset, funcAST.Body)

s := buf.String()
s = s[1 : len(s)-1]
s = strings.TrimSpace(strings.ReplaceAll(s, "\n\t", "\n"))

fmt.Println(s)

Types

type CommentedNode

type CommentedNode struct {
	Node     any
	Comments []*ast.CommentGroup
}

CommentedNodeは、ASTノードと対応するコメントをまとめたものです。 これは、Fprint 関数の引数として提供することができます。

type Config

type Config struct {
	Mode     Mode
	Tabwidth int
	Indent   int
}

ConfigノードはFprintの出力を制御します。

func (*Config) Fprint

func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node any) error

Fprintは与えられた設定cfgに対して、ASTノードを出力に「きれいに表示」します。 位置情報はファイルセットfsetを基準に解釈されます。 ノードの型は *ast.File、*CommentedNode、[]ast.Decl、[]ast.Stmt、または ast.Exprast.Declast.Specast.Stmt に互換性のあるものである必要があります。

type Mode

type Mode uint

モード値はフラグの集合(または0)です。これらは印刷を制御します。

const (
	RawFormat Mode = 1 << iota
	TabIndent
	UseSpaces
	SourcePos
)

Jump to

Keyboard shortcuts

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