Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generate a tags file for the Go Programming Language in the format used by
- // exuberant-ctags. Can be used with the VIM editor to navigate Go source code
- // files.
- //
- // usage: gotags filename [ filename... ] > tags
- //
- // Authors: michael.elkins@gmail.com, m.fellinger@gmail.com
- // Date: 2010-05-17
- package main
- import (
- "container/vector"
- "fmt"
- "go/ast"
- "go/parser"
- "os"
- "sort"
- )
- var tags = vector.StringVector{}
- func output_tag(name *ast.Ident, kind byte) {
- tags.Push(fmt.Sprintf("%s\t%s\t%d;\"\t%c",
- name.String(), name.Position.Filename, name.Position.Line, kind))
- }
- func main() {
- parse_files()
- println("!_TAG_FILE_SORTED\t1\t")
- sa := tags.Data()
- sort.SortStrings(sa)
- for _, s := range sa {
- 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 {
- panic("error parsing file" + os.Args[i] + ok.String())
- }
- for _, node := range tree.Decls {
- switch n := node.(type) {
- case *ast.FuncDecl:
- output_tag(n.Name, FUNC)
- case *ast.GenDecl:
- do_gen_decl(n)
- }
- }
- }
- }
- func do_gen_decl(node *ast.GenDecl) {
- for _, v := range node.Specs {
- switch n := v.(type) {
- case *ast.TypeSpec:
- output_tag(n.Name, TYPE)
- case *ast.ValueSpec:
- for _, vv := range n.Names {
- output_tag(vv, VAR)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment