// Merges multiple import blocks and then adds paths func (p *compiler) addAnthaImports(file *ast.File, paths []string) { var specs []ast.Spec var decls []ast.Decl var pos token.Pos // Dummy position to use for generated imports for _, d := range file.Decls { gd, ok := d.(*ast.GenDecl) if pos == token.NoPos { pos = d.Pos() } if !ok || gd.Tok != token.IMPORT { decls = append(decls, d) continue } for _, s := range gd.Specs { specs = append(specs, s) } } for _, p := range paths { specs = append(specs, &ast.ImportSpec{ Path: &ast.BasicLit{ Kind: token.STRING, Value: fmt.Sprintf(`"%s"`, p), ValuePos: pos, }}) } if len(specs) == 0 { if len(decls) != len(file.Decls) { // Clean up empty imports file.Decls = decls } return } //if pos == token.NoPos { // log.Panicf("no declarations in antha file: %s", file.Name) //} merged := &ast.GenDecl{ Tok: token.IMPORT, Lparen: pos, Rparen: pos, Specs: specs, } decls = append([]ast.Decl{merged}, decls...) file.Decls = decls // NB(ddn): tried to sort here, but the following needs proper token.Pos, // which are annoying to generate now. A gofmt on the generated file should // be just as good. //ast.SortImports(token.NewFileSet(), file) }
// fileExports removes unexported declarations from src in place. // func (r *reader) fileExports(src *ast.File) { j := 0 for _, d := range src.Decls { if r.filterDecl(d) { src.Decls[j] = d j++ } } src.Decls = src.Decls[0:j] }
// Remove antha param declarations since they are fully captured in indexParams() func (p *compiler) removeParamDecls(src *ast.File) { var newd []ast.Decl for _, decl := range src.Decls { toadd := true switch decl.(type) { case *ast.GenDecl: switch decl.(*ast.GenDecl).Tok { case token.PARAMETERS, token.DATA, token.INPUTS, token.OUTPUTS: toadd = false } } if toadd { newd = append(newd, decl) } } src.Decls = newd }