~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/cmd/godoc/parser.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
// This file contains support functions for parsing .go files.
 
6
// Similar functionality is found in package go/parser but the
 
7
// functions here operate using godoc's file system fs instead
 
8
// of calling the OS's file operations directly.
 
9
 
 
10
package main
 
11
 
 
12
import (
 
13
        "go/ast"
 
14
        "go/parser"
 
15
        "go/token"
 
16
        "os"
 
17
        "path/filepath"
 
18
)
 
19
 
 
20
func parseFiles(fset *token.FileSet, filenames []string) (pkgs map[string]*ast.Package, first os.Error) {
 
21
        pkgs = make(map[string]*ast.Package)
 
22
        for _, filename := range filenames {
 
23
                src, err := fs.ReadFile(filename)
 
24
                if err != nil {
 
25
                        if first == nil {
 
26
                                first = err
 
27
                        }
 
28
                        continue
 
29
                }
 
30
 
 
31
                file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
 
32
                if err != nil {
 
33
                        if first == nil {
 
34
                                first = err
 
35
                        }
 
36
                        continue
 
37
                }
 
38
 
 
39
                name := file.Name.Name
 
40
                pkg, found := pkgs[name]
 
41
                if !found {
 
42
                        // TODO(gri) Use NewPackage here; reconsider ParseFiles API.
 
43
                        pkg = &ast.Package{name, nil, nil, make(map[string]*ast.File)}
 
44
                        pkgs[name] = pkg
 
45
                }
 
46
                pkg.Files[filename] = file
 
47
        }
 
48
        return
 
49
}
 
50
 
 
51
 
 
52
func parseDir(fset *token.FileSet, path string, filter func(FileInfo) bool) (map[string]*ast.Package, os.Error) {
 
53
        list, err := fs.ReadDir(path)
 
54
        if err != nil {
 
55
                return nil, err
 
56
        }
 
57
 
 
58
        filenames := make([]string, len(list))
 
59
        i := 0
 
60
        for _, d := range list {
 
61
                if filter == nil || filter(d) {
 
62
                        filenames[i] = filepath.Join(path, d.Name())
 
63
                        i++
 
64
                }
 
65
        }
 
66
        filenames = filenames[0:i]
 
67
 
 
68
        return parseFiles(fset, filenames)
 
69
}