Commits

Erno Hopearuoho committed 7984381

Fixed to work with Go 1 release.

Comments (0)

Files changed (1)

 
 // Date: 2009-11-12
 // Updated: 2010-08-21
+// Updated: 2011-06-23
+//    Sean E. Russell <seanerussell@gmail.com>
+//    Fixed numerous compilation errors; the go APIs probably changed since the
+//    last time Michael updated it.
+// Updated: 2012-03-29
+//    Erno Hopearuoho <erno.hopearuoho@cs.helsinki.fi>
+//    Updated to work with Go 1.
 
 //
 // usage: gotags filename [ filename... ] > tags
 package main
 
 import (
-	"container/vector"
 	"fmt"
 	"go/ast"
 	"go/parser"
+	"go/token"
+	"log"
 	"os"
 	"sort"
 )
 
 var (
-	tags vector.StringVector
+	tags []string = make([]string, 0, 20)
 )
 
-func output_tag(name *ast.Ident, kind byte) {
-	tags.Push(fmt.Sprintf("%s\t%s\t%d;\"\t%c",
-		name.Name(), name.Position.Filename, name.Position.Line, kind))
+func output_tag(fset *token.FileSet, name *ast.Ident, kind byte) {
+	position := fset.Position(name.NamePos)
+	tags = append(tags, fmt.Sprintf("%s\t%s\t%d;\"\t%c",
+		name.Name, position.Filename, position.Line, kind))
 }
 
 func main() {
 	parse_files()
 
-	println("!_TAG_FILE_SORTED\t1\t")
-	sort.SortStrings(tags)
+	fmt.Println("!_TAG_FILE_SORTED\t1\t")
+	sort.Strings(tags)
 	for _, s := range tags {
-		println(s)
+		fmt.Println(s)
 	}
 }
 
 const FUNC, TYPE, VAR = 'f', 't', 'v'
 
 func parse_files() {
-	for i, m := 1, len(os.Args); i < m; i++ {
-		tree, ok := parser.ParseFile(os.Args[i], nil, nil, 0)
-		if ok != nil {
-			println("error parsing file", os.Args[i], ok.String())
-			panic(nil)
+	for _, f := range os.Args[1:] {
+		fileset := token.NewFileSet()
+		fi, err := os.Lstat(f)
+		if err != nil {
+			log.Fatalf("error opening file %s: %s", f, err.Error())
+		}
+		fileset.AddFile(f, fileset.Base(), int(fi.Size()))
+		tree, err := parser.ParseFile(fileset, f, nil, 0)
+		if err != nil {
+			log.Fatalf("error parsing file %s: %s", f, err.Error())
 		}
 
 		for _, node := range tree.Decls {
 			switch n := node.(type) {
 			case *ast.FuncDecl:
-				output_tag(n.Name, FUNC)
+				output_tag(fileset, n.Name, FUNC)
 			case *ast.GenDecl:
-				do_gen_decl(n)
+				do_gen_decl(fileset, n)
 			}
 		}
 	}
 
 }
 
-func do_gen_decl(node *ast.GenDecl) {
+func do_gen_decl(fset *token.FileSet, node *ast.GenDecl) {
 	for _, v := range node.Specs {
 		switch n := v.(type) {
 		case *ast.TypeSpec:
-			output_tag(n.Name, TYPE)
+			output_tag(fset, n.Name, TYPE)
 
 		case *ast.ValueSpec:
 			for _, vv := range n.Names {
-				output_tag(vv, VAR)
+				output_tag(fset, vv, VAR)
 			}
 		}
 	}