2013-08-27 16:49:13 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
package importer
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// This file defines various utility functions exposed by the package
|
|
|
|
// and used by it.
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"go/ast"
|
|
|
|
"go/build"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-09-04 11:15:49 -06:00
|
|
|
"sync"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// CreatePackageFromArgs builds an initial Package from a list of
|
|
|
|
// command-line arguments.
|
|
|
|
// If args is a list of *.go files, they are parsed and type-checked.
|
|
|
|
// If args is a Go package import path, that package is imported.
|
|
|
|
// The rest result contains the suffix of args that were not consumed.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-31 14:14:13 -06:00
|
|
|
// This utility is provided to facilitate construction of command-line
|
|
|
|
// tools with a consistent user interface.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-31 14:14:13 -06:00
|
|
|
func CreatePackageFromArgs(imp *Importer, args []string) (info *PackageInfo, rest []string, err error) {
|
|
|
|
switch {
|
|
|
|
case len(args) == 0:
|
2013-09-04 11:15:49 -06:00
|
|
|
return nil, nil, errors.New("no *.go source files nor package name was specified.")
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
case strings.HasSuffix(args[0], ".go"):
|
|
|
|
// % tool a.go b.go ...
|
|
|
|
// Leading consecutive *.go arguments constitute main package.
|
|
|
|
i := 1
|
|
|
|
for ; i < len(args) && strings.HasSuffix(args[i], ".go"); i++ {
|
|
|
|
}
|
|
|
|
var files []*ast.File
|
|
|
|
files, err = ParseFiles(imp.Fset, ".", args[:i]...)
|
|
|
|
rest = args[i:]
|
2013-05-17 14:25:48 -06:00
|
|
|
if err == nil {
|
2013-09-04 11:15:49 -06:00
|
|
|
info, err = imp.CreateSourcePackage("main", files)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
default:
|
|
|
|
// % tool my/package ...
|
|
|
|
// First argument is import path of main package.
|
|
|
|
pkgname := args[0]
|
|
|
|
info, err = imp.LoadPackage(pkgname)
|
|
|
|
rest = args[1:]
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
return
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-09-04 11:15:49 -06:00
|
|
|
var cwd string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
cwd, err = os.Getwd()
|
2013-05-17 14:25:48 -06:00
|
|
|
if err != nil {
|
2013-09-04 11:15:49 -06:00
|
|
|
panic("getcwd failed: " + err.Error())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-09-04 11:15:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadPackage ascertains which files belong to package path, then
|
|
|
|
// loads, parses and returns them.
|
|
|
|
func loadPackage(ctxt *build.Context, fset *token.FileSet, path string) (files []*ast.File, err error) {
|
|
|
|
// TODO(adonovan): fix: Do we need cwd? Shouldn't
|
|
|
|
// ImportDir(path) / $GOROOT suffice?
|
|
|
|
bp, err := ctxt.Import(path, cwd, 0)
|
|
|
|
if _, ok := err.(*build.NoGoError); ok {
|
|
|
|
return nil, nil // empty directory
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-09-04 11:15:49 -06:00
|
|
|
if err != nil {
|
|
|
|
return // import failed
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-09-04 11:15:49 -06:00
|
|
|
return ParseFiles(fset, bp.Dir, bp.GoFiles...)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFiles parses the Go source files files within directory dir
|
|
|
|
// and returns their ASTs, or the first parse error if any.
|
|
|
|
//
|
2013-09-04 11:15:49 -06:00
|
|
|
func ParseFiles(fset *token.FileSet, dir string, files ...string) ([]*ast.File, error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
n := len(files)
|
|
|
|
parsed := make([]*ast.File, n, n)
|
|
|
|
errors := make([]error, n, n)
|
|
|
|
for i, file := range files {
|
2013-05-17 14:25:48 -06:00
|
|
|
if !filepath.IsAbs(file) {
|
|
|
|
file = filepath.Join(dir, file)
|
|
|
|
}
|
2013-09-04 11:15:49 -06:00
|
|
|
wg.Add(1)
|
|
|
|
go func(i int, file string) {
|
|
|
|
parsed[i], errors[i] = parser.ParseFile(fset, file, nil, parser.DeclarationErrors)
|
|
|
|
wg.Done()
|
|
|
|
}(i, file)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
for _, err := range errors {
|
2013-05-17 14:25:48 -06:00
|
|
|
if err != nil {
|
2013-09-04 11:15:49 -06:00
|
|
|
return nil, err
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 11:15:49 -06:00
|
|
|
return parsed, nil
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// ---------- Internal helpers ----------
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-31 14:14:13 -06:00
|
|
|
// unparen returns e with any enclosing parentheses stripped.
|
|
|
|
func unparen(e ast.Expr) ast.Expr {
|
|
|
|
for {
|
|
|
|
p, ok := e.(*ast.ParenExpr)
|
|
|
|
if !ok {
|
|
|
|
break
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
e = p.X
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-05-31 14:14:13 -06:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func unreachable() {
|
|
|
|
panic("unreachable")
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|