printer: doc for exported symbols

Updates #9
This commit is contained in:
Michael McLoughlin
2019-01-04 19:03:02 -08:00
parent cfce8d94df
commit 88eff53893
4 changed files with 51 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ type goasm struct {
prnt.Generator prnt.Generator
} }
// NewGoAsm constructs a printer for writing Go assembly files.
func NewGoAsm(cfg Config) Printer { func NewGoAsm(cfg Config) Printer {
return &goasm{cfg: cfg} return &goasm{cfg: cfg}
} }

View File

@@ -1,3 +1,4 @@
// Package printer implements printing of avo files in various formats.
package printer package printer
import ( import (
@@ -10,18 +11,29 @@ import (
"github.com/mmcloughlin/avo/internal/stack" "github.com/mmcloughlin/avo/internal/stack"
) )
// Printer can produce output for an avo File.
type Printer interface { type Printer interface {
Print(*avo.File) ([]byte, error) Print(*avo.File) ([]byte, error)
} }
// Builder can construct a printer.
type Builder func(Config) Printer type Builder func(Config) Printer
// Config represents general printing configuration.
type Config struct { type Config struct {
Name string // Command-line arguments passed to the generator. If provided, this will be
// included in a code generation warning.
Argv []string Argv []string
Pkg string
// Name of the code generator.
Name string
// Name of Go package the generated code will belong to.
Pkg string
} }
// NewDefaultConfig produces a config with Name "avo".
// The package name is guessed from the current directory.
func NewDefaultConfig() Config { func NewDefaultConfig() Config {
return Config{ return Config{
Name: "avo", Name: "avo",
@@ -29,6 +41,8 @@ func NewDefaultConfig() Config {
} }
} }
// NewArgvConfig constructs a Config from os.Args.
// The package name is guessed from the current directory.
func NewArgvConfig() Config { func NewArgvConfig() Config {
return Config{ return Config{
Argv: os.Args, Argv: os.Args,
@@ -53,6 +67,7 @@ func NewGoRunConfig() Config {
} }
} }
// GeneratedBy returns a description of the code generator.
func (c Config) GeneratedBy() string { func (c Config) GeneratedBy() string {
if c.Argv == nil { if c.Argv == nil {
return c.Name return c.Name
@@ -60,6 +75,7 @@ func (c Config) GeneratedBy() string {
return fmt.Sprintf("command: %s", strings.Join(c.Argv, " ")) return fmt.Sprintf("command: %s", strings.Join(c.Argv, " "))
} }
// GeneratedWarning returns text for a code generation warning. Conforms to https://golang.org/s/generatedcode.
func (c Config) GeneratedWarning() string { func (c Config) GeneratedWarning() string {
return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy()) return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy())
} }

31
printer/printer_test.go Normal file
View File

@@ -0,0 +1,31 @@
package printer_test
import (
"fmt"
"github.com/mmcloughlin/avo/printer"
)
func ExampleConfig_GeneratedWarning() {
// Default configuration named "avo".
cfg := printer.NewDefaultConfig()
fmt.Println(cfg.GeneratedWarning())
// Name can be customized.
cfg = printer.Config{
Name: "mildred",
}
fmt.Println(cfg.GeneratedWarning())
// Argv takes precedence.
cfg = printer.Config{
Argv: []string{"echo", "hello", "world"},
Name: "mildred",
}
fmt.Println(cfg.GeneratedWarning())
// Output:
// Code generated by avo. DO NOT EDIT.
// Code generated by mildred. DO NOT EDIT.
// Code generated by command: echo hello world. DO NOT EDIT.
}

View File

@@ -10,6 +10,7 @@ type stubs struct {
prnt.Generator prnt.Generator
} }
// NewStubs constructs a printer for writing stub function declarations.
func NewStubs(cfg Config) Printer { func NewStubs(cfg Config) Printer {
return &stubs{cfg: cfg} return &stubs{cfg: cfg}
} }