From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/go/go%2Fprinter%2Findex.html | 100 +++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 devdocs/go/go%2Fprinter%2Findex.html (limited to 'devdocs/go/go%2Fprinter%2Findex.html') diff --git a/devdocs/go/go%2Fprinter%2Findex.html b/devdocs/go/go%2Fprinter%2Findex.html new file mode 100644 index 00000000..b801c989 --- /dev/null +++ b/devdocs/go/go%2Fprinter%2Findex.html @@ -0,0 +1,100 @@ +

Package printer

Overview

Package printer implements printing of AST nodes.

Index

Examples

Fprint

Package files

comment.go gobuild.go nodes.go printer.go

func Fprint

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

Fprint "pretty-prints" an AST node to output. It calls Config.Fprint with default settings. Note that gofmt uses tabs for indentation but spaces for alignment; use format.Node (package go/format) for output that matches gofmt.

Example +

Code:

package printer_test
+
+import (
+    "bytes"
+    "fmt"
+    "go/ast"
+    "go/parser"
+    "go/printer"
+    "go/token"
+    "strings"
+)
+
+func parseFunc(filename, functionname string) (fun *ast.FuncDecl, fset *token.FileSet) {
+    fset = token.NewFileSet()
+    if file, err := parser.ParseFile(fset, filename, nil, 0); err == nil {
+        for _, d := range file.Decls {
+            if f, ok := d.(*ast.FuncDecl); ok && f.Name.Name == functionname {
+                fun = f
+                return
+            }
+        }
+    }
+    panic("function not found")
+}
+
+func printSelf() {
+    // Parse source file and extract the AST without comments for
+    // this function, with position information referring to the
+    // file set fset.
+    funcAST, fset := parseFunc("example_test.go", "printSelf")
+
+    // Print the function body into buffer buf.
+    // The file set is provided to the printer so that it knows
+    // about the original source formatting and can add additional
+    // line breaks where they were present in the source.
+    var buf bytes.Buffer
+    printer.Fprint(&buf, fset, funcAST.Body)
+
+    // Remove braces {} enclosing the function body, unindent,
+    // and trim leading and trailing white space.
+    s := buf.String()
+    s = s[1 : len(s)-1]
+    s = strings.TrimSpace(strings.ReplaceAll(s, "\n\t", "\n"))
+
+    // Print the cleaned-up body text to stdout.
+    fmt.Println(s)
+}
+
+func ExampleFprint() {
+    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)
+}
+

type CommentedNode

A CommentedNode bundles an AST node and corresponding comments. It may be provided as argument to any of the Fprint functions.

+
type CommentedNode struct {
+    Node     any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
+    Comments []*ast.CommentGroup
+}
+

type Config

A Config node controls the output of Fprint.

+
type Config struct {
+    Mode     Mode // default: 0
+    Tabwidth int  // default: 8
+    Indent   int  // default: 0 (all code is indented at least by this much); added in Go 1.1
+}
+

func (*Config) Fprint

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

Fprint "pretty-prints" an AST node to output for a given configuration cfg. Position information is interpreted relative to the file set fset. The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.

+

type Mode

A Mode value is a set of flags (or 0). They control printing.

+
type Mode uint
const (
+    RawFormat Mode = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
+    TabIndent                  // use tabs for indentation independent of UseSpaces
+    UseSpaces                  // use spaces instead of tabs for alignment
+    SourcePos                  // emit //line directives to preserve original source positions
+)
+

+ © Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
+ http://golang.org/pkg/go/printer/ +

+
-- cgit v1.2.3