Documentation ¶
Index ¶
- func Check(pkgs []*Package) error
- func InitLog(enabled bool)
- func Translate(pkgs []*Package) error
- type Gen
- func (gen *Gen) Check(paths []string) error
- func (gen *Gen) Generate(paths []string, verify bool) error
- func (gen *Gen) GeneratePackages(pkgDirs []string, verify bool) error
- func (gen *Gen) PackageDirs(paths []string) ([]string, error)
- func (gen *Gen) ParsePackages(pkgDirs []string) ([]*Package, error)
- func (gen *Gen) TranslatePackages(pkgDirs []string) ([]*Package, error)
- type Package
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check checks given packages. It eliminates all try() calls then runs type check against packages. Returning nil means check was OK.
func InitLog ¶
func InitLog(enabled bool)
InitLog initializes logging instance. When true is given as enabled, all logs are output to stderr while code generations.
func Translate ¶
Translate translates all given TryGo packages by modifying given slice of Packages directly. After translation, the given packages are translated to Go packages. Each Package instance's Node and Files fields must be set with the results of an AST and tokens parsed from TryGo source. And Birth must be set correctly as package directory of the TryGo source. When translation failed, it returns an error as soon as possible. Given Package instances may be no longer correct.
Types ¶
type Gen ¶
type Gen struct { // OutDir is a directory path to output directory. This value must be an absolute path OutDir string // Writer is a writer to output messages Writer io.Writer }
Gen represents a generator of trygo
Example ¶
package main import ( "fmt" "github.com/rhysd/trygo" "io/ioutil" "path/filepath" ) func main() { pkgDir := filepath.Join("testdata", "example") outDir := filepath.Join(pkgDir, "out") // Create a code generator for TryGo to Go translation gen, err := trygo.NewGen(outDir) if err != nil { panic(err) } // Generate() outputs translated file paths by default. If you don't want them, please set // ioutil.Discard as writer. gen.Writer = ioutil.Discard // Translate TryGo package into Go and generate it at outDir with output verification. // It generates testdata/example/out. if err := gen.Generate([]string{pkgDir}, true); err != nil { panic(err) } fmt.Println("OK") }
Output: OK
func NewGen ¶
NewGen creates a new Gen instance with given output directory. All translated packages are generated under the output directory. When the output directory does not exist, it is automatically created.
func (*Gen) Check ¶
Check checks packages in given paths. Nothing is generated. When check was OK, it returns nil.
func (*Gen) Generate ¶
Generate collects all TryGo packages under given paths, translates all the TryGo packages specified with directory paths and generates translated Go files with the same directory structures under output directory. When 'verify' argument is set to true, translated packages are verified with type checks after generating the Go files. When the verification reports some errors, generated Go files would be broken. This verification is mainly used for debugging. When collecting TryGo packages from paths failed, packages parsing TryGo sources failed or the translations failed, translated Go file could not be written, this function returns an error.
func (*Gen) GeneratePackages ¶
GeneratePackages translates all TryGo packages specified with directory paths and generates translated Go files with the same directory structures under output directory. When 'verify' argument is set to true, translated packages are verified with type checks after generating the Go files. When the verification reports some errors, generated Go files would be broken. This verification is mainly used for debugging. When parsing Go(TryGo) sources failed or the translations failed, translated Go file could not be written, this function returns an error.
func (*Gen) PackageDirs ¶
PackageDirs collects package directories under given paths. If paths argument is empty, it collects a package directory as `go generate` runs trygo. If no Go package is found or pacakge directory cannot be read, this function returns an error.
func (*Gen) ParsePackages ¶
ParsePackages parses given package directories and returns parsed packages. Output directory where translated package is put is calculated based on output directory.
func (*Gen) TranslatePackages ¶
TranslatePackages translates all packages specified with directory paths. It returns slice of Package which represent translated packages. When parsing Go(TryGo) sources failed or the translations failed, this function returns an error.
type Package ¶
type Package struct { // Files is a token file set to get position information of nodes. Files *token.FileSet // Node is an AST package node which was parsed from TryGo code. AST will be directly modified // by translations. Node *ast.Package // Path is a package path where this translated package *will* be created. Path string // Birth is a pacakge path where this translated package was translated from. Birth string // Types is a type information of the package. This field is nil by default and set as the result // of verification. So this field is non-nil only when verification was performed. Types *types.Package // contains filtered or unexported fields }
Package represents tranlated package. It contains tokens and AST of all Go files in the package
func NewPackage ¶
NewPackage creates a new Package instance containing additional information to AST node