本文整理汇总了Golang中go/ast.NewPackage函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPackage函数的具体用法?Golang NewPackage怎么用?Golang NewPackage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewPackage函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Call
func (m *mDoc) Call() (interface{}, string) {
res := []*Doc{}
fset, af, err := parseAstFile(m.Fn, m.Src, parser.ParseComments)
if err != nil {
return res, err.Error()
}
sel, id := identAt(fset, af, m.Offset)
if id == nil {
return res, ""
}
pkgs, _ := parser.ParseDir(fset, filepath.Dir(m.Fn), fiHasGoExt, parser.ParseComments)
if pkgs == nil {
pkgs = map[string]*ast.Package{}
}
pkgName := af.Name.Name
files := map[string]*ast.File{}
pkg, _ := pkgs[pkgName]
if pkg != nil {
files = pkg.Files
}
files[m.Fn] = af
pkg, _ = ast.NewPackage(fset, files, nil, nil)
if pkg == nil {
return res, ""
}
pkgs[pkg.Name] = pkg
obj, pkg, objPkgs := findUnderlyingObj(fset, af, pkg, pkgs, rootDirs(m.Env), sel, id)
if obj != nil {
res = append(res, objDoc(fset, pkg, m.TabIndent, m.TabWidth, obj))
if objPkgs != nil {
xName := "Example" + obj.Name
xPrefix := xName + "_"
for _, objPkg := range objPkgs {
xPkg, _ := ast.NewPackage(fset, objPkg.Files, nil, nil)
if xPkg == nil || xPkg.Scope == nil {
continue
}
for _, xObj := range xPkg.Scope.Objects {
if xObj.Name == xName || strings.HasPrefix(xObj.Name, xPrefix) {
res = append(res, objDoc(fset, xPkg, m.TabIndent, m.TabWidth, xObj))
}
}
}
}
}
return res, ""
}
开发者ID:jason-riddle,项目名称:dotfiles,代码行数:53,代码来源:m_doc.go
示例2: check
func check(t *testing.T, testname string, testfiles []string) {
// TODO(gri) Eventually all these different phases should be
// subsumed into a single function call that takes
// a set of files and creates a fully resolved and
// type-checked AST.
files, err := parseFiles(t, testname, testfiles)
// we are expecting the following errors
// (collect these after parsing the files so that
// they are found in the file set)
errors := expectedErrors(t, testname, files)
// verify errors returned by the parser
eliminate(t, errors, err)
// verify errors returned after resolving identifiers
pkg, err := ast.NewPackage(fset, files, GcImporter, Universe)
eliminate(t, errors, err)
// verify errors returned by the typechecker
_, err = Check(fset, pkg)
eliminate(t, errors, err)
// there should be no expected errors left
if len(errors) > 0 {
t.Errorf("%s: %d errors not reported:", testname, len(errors))
for pos, msg := range errors {
t.Errorf("%s: %s\n", fset.Position(pos), msg)
}
}
}
开发者ID:rgmabs19357,项目名称:gcc,代码行数:32,代码来源:check_test.go
示例3: parseReflect
// parseReflect parses the reflect package and type-checks its AST.
// This is used to generate runtime type structures.
func (c *compiler) parseReflect() (*ast.Package, error) {
buildpkg, err := build.Import("reflect", "", 0)
if err != nil {
return nil, err
}
filenames := make([]string, len(buildpkg.GoFiles))
for i, f := range buildpkg.GoFiles {
filenames[i] = path.Join(buildpkg.Dir, f)
}
fset := token.NewFileSet()
files, err := parseFiles(fset, filenames)
if err != nil {
return nil, err
}
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
return nil, err
}
_, err = types.Check(buildpkg.Name, c, fset, pkg)
if err != nil {
return nil, err
}
return pkg, nil
}
开发者ID:kelsieflynn,项目名称:llgo,代码行数:29,代码来源:runtime.go
示例4: compilePackage
func compilePackage(fset *token.FileSet, files map[string]*ast.File) (*llgo.Module, error) {
// make a package (resolve all identifiers)
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
report(err)
return nil, err
}
exprTypes, err := types.Check(fset, pkg)
if err != nil {
report(err)
return nil, err
}
if *dumpast {
ast.Fprint(os.Stderr, fset, pkg, nil)
os.Exit(0)
}
compiler := llgo.NewCompiler()
compiler.SetTraceEnabled(*trace)
compiler.SetTargetArch(*arch)
compiler.SetTargetOs(*os_)
return compiler.Compile(fset, pkg, exprTypes)
}
开发者ID:c0der007,项目名称:llgo,代码行数:25,代码来源:llgo.go
示例5: TestFilterDuplicates
func TestFilterDuplicates(t *testing.T) {
// parse input
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", input, 0)
if err != nil {
t.Fatal(err)
}
// create package
files := map[string]*ast.File{"": file}
pkg, err := ast.NewPackage(fset, files, nil, nil)
if err != nil {
t.Fatal(err)
}
// filter
merged := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates)
// pretty-print
var buf bytes.Buffer
if err := format.Node(&buf, fset, merged); err != nil {
t.Fatal(err)
}
output := buf.String()
if output != golden {
t.Errorf("incorrect output:\n%s", output)
}
}
开发者ID:Clarifai,项目名称:kubernetes,代码行数:29,代码来源:filter_test.go
示例6: doParseReflect
func doParseReflect() {
buildpkg, err := build.Import("reflect", "", 0)
if err != nil {
parseReflectResult, parseReflectError = nil, err
return
}
filenames := make([]string, len(buildpkg.GoFiles))
for i, f := range buildpkg.GoFiles {
filenames[i] = path.Join(buildpkg.Dir, f)
}
fset := token.NewFileSet()
files, err := parseFiles(fset, filenames)
if err != nil {
parseReflectResult, parseReflectError = nil, err
return
}
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
parseReflectResult, parseReflectError = nil, err
return
}
_, err = types.Check(fset, pkg)
if err != nil {
parseReflectResult, parseReflectError = nil, err
return
}
parseReflectResult, parseReflectError = pkg, nil
return
}
开发者ID:spate,项目名称:llgo,代码行数:33,代码来源:reflect.go
示例7: compilePackage
func compilePackage(compiler llgo.Compiler, fset *token.FileSet, files map[string]*ast.File, importpath string) (*llgo.Module, error) {
// make a package (resolve all identifiers)
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
report(err)
return nil, err
}
// an empty importpath means the same as the package name
if importpath == "" {
importpath = pkg.Name
}
exprTypes, err := types.Check(importpath, compiler, fset, pkg)
if err != nil {
report(err)
return nil, err
}
if *dumpast {
ast.Fprint(os.Stderr, fset, pkg, nil)
os.Exit(0)
}
return compiler.Compile(fset, pkg, importpath, exprTypes)
}
开发者ID:kelsieflynn,项目名称:llgo,代码行数:26,代码来源:llgo.go
示例8: main
func main() {
flag.Parse()
if len(flag.Args()) != 1 {
log.Fatal("Usage: go run goprint.go path")
}
bpkg, err := build.Default.Import(flag.Args()[0], ".", 0)
if err != nil {
log.Fatal(err)
}
fset := token.NewFileSet()
files := make(map[string]*ast.File)
for _, fname := range bpkg.GoFiles {
p, err := ioutil.ReadFile(filepath.Join(bpkg.SrcRoot, bpkg.ImportPath, fname))
if err != nil {
log.Fatal(err)
}
file, err := parser.ParseFile(fset, fname, p, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
files[fname] = file
}
c := spew.NewDefaultConfig()
c.DisableMethods = true
apkg, _ := ast.NewPackage(fset, files, importer, nil)
c.Dump(apkg)
ast.Print(fset, apkg)
dpkg := doc.New(apkg, bpkg.ImportPath, 0)
c.Dump(dpkg)
}
开发者ID:maddyonline,项目名称:gddo,代码行数:30,代码来源:goprint.go
示例9: ExtractStructs
func ExtractStructs(inputPath string) (string, []*StructInfo, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments)
if err != nil {
return "", nil, err
}
packageName := f.Name.String()
structs := make(map[string]*StructInfo)
for k, d := range f.Scope.Objects {
if d.Kind == ast.Typ {
incl, err := shouldInclude(d)
if err != nil {
return "", nil, err
}
if incl {
stobj := NewStructInfo(k)
structs[k] = stobj
}
}
}
files := map[string]*ast.File{
inputPath: f,
}
pkg, _ := ast.NewPackage(fset, files, nil, nil)
d := doc.New(pkg, f.Name.String(), doc.AllDecls)
for _, t := range d.Types {
if skipre.MatchString(t.Doc) {
delete(structs, t.Name)
} else {
if skipdec.MatchString(t.Doc) {
s, ok := structs[t.Name]
if ok {
s.Options.SkipDecoder = true
}
}
if skipenc.MatchString(t.Doc) {
s, ok := structs[t.Name]
if ok {
s.Options.SkipEncoder = true
}
}
}
}
rv := make([]*StructInfo, 0)
for _, v := range structs {
rv = append(rv, v)
}
return packageName, rv, nil
}
开发者ID:nolenroyalty,项目名称:bangarang,代码行数:58,代码来源:parser.go
示例10: build
// build generates data from source files.
func (w *routerWalker) build(srcs []*source) (*Package, error) {
// Add source files to walker, I skipped references here.
w.srcs = make(map[string]*source)
for _, src := range srcs {
w.srcs[src.name] = src
}
w.fset = token.NewFileSet()
// Find the package and associated files.
ctxt := gobuild.Context{
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
CgoEnabled: true,
JoinPath: path.Join,
IsAbsPath: path.IsAbs,
SplitPathList: func(list string) []string { return strings.Split(list, ":") },
IsDir: func(path string) bool { panic("unexpected") },
HasSubdir: func(root, dir string) (rel string, ok bool) { panic("unexpected") },
ReadDir: func(dir string) (fi []os.FileInfo, err error) { return w.readDir(dir) },
OpenFile: func(path string) (r io.ReadCloser, err error) { return w.openFile(path) },
Compiler: "gc",
}
bpkg, err := ctxt.ImportDir(w.pdoc.ImportPath, 0)
// Continue if there are no Go source files; we still want the directory info.
_, nogo := err.(*gobuild.NoGoError)
if err != nil {
if nogo {
err = nil
} else {
return nil, errors.New("routerWalker.build -> " + err.Error())
}
}
// Parse the Go files
files := make(map[string]*ast.File)
for _, name := range append(bpkg.GoFiles, bpkg.CgoFiles...) {
file, err := parser.ParseFile(w.fset, name, w.srcs[name].data, parser.ParseComments)
if err != nil {
return nil, errors.New("routerWalker.build -> parse go files: " + err.Error())
}
files[name] = file
}
apkg, _ := ast.NewPackage(w.fset, files, simpleImporter, nil)
mode := doc.Mode(0)
if w.pdoc.ImportPath == "builtin" {
mode |= doc.AllDecls
}
pdoc := doc.New(apkg, w.pdoc.ImportPath, mode)
w.pdoc.Types = w.types(pdoc.Types)
return w.pdoc, err
}
开发者ID:Linvas,项目名称:ant,代码行数:59,代码来源:autorouter.go
示例11: TestResolveQualifiedIdents
func TestResolveQualifiedIdents(t *testing.T) {
// parse package files
fset := token.NewFileSet()
files := make(map[string]*ast.File)
for i, src := range sources {
filename := fmt.Sprintf("file%d", i)
f, err := parser.ParseFile(fset, filename, src, parser.DeclarationErrors)
if err != nil {
t.Fatal(err)
}
files[filename] = f
}
// resolve package AST
pkg, err := ast.NewPackage(fset, files, GcImport, Universe)
if err != nil {
t.Fatal(err)
}
// check that all packages were imported
for _, name := range pkgnames {
if pkg.Imports[name] == nil {
t.Errorf("package %s not imported", name)
}
}
// check that there are no top-level unresolved identifiers
for _, f := range pkg.Files {
for _, x := range f.Unresolved {
t.Errorf("%s: unresolved global identifier %s", fset.Position(x.Pos()), x.Name)
}
}
// resolve qualified identifiers
if err := ResolveQualifiedIdents(fset, pkg); err != nil {
t.Error(err)
}
// check that qualified identifiers are resolved
ast.Inspect(pkg, func(n ast.Node) bool {
if s, ok := n.(*ast.SelectorExpr); ok {
if x, ok := s.X.(*ast.Ident); ok {
if x.Obj == nil {
t.Errorf("%s: unresolved qualified identifier %s", fset.Position(x.Pos()), x.Name)
return false
}
if x.Obj.Kind == ast.Pkg && s.Sel != nil && s.Sel.Obj == nil {
t.Errorf("%s: unresolved selector %s", fset.Position(s.Sel.Pos()), s.Sel.Name)
return false
}
return false
}
return false
}
return true
})
}
开发者ID:strickyak,项目名称:goterp,代码行数:57,代码来源:resolver_test.go
示例12: AnalyzeFile
// AnalyzeFile scans the provided file for references to packages with the given
// package prefix. The list of unique (package, identifier) pairs is returned
func AnalyzeFile(file *ast.File, pkgPrefix string) (*References, error) {
visitor := newRefsSaver(pkgPrefix)
fset := token.NewFileSet()
files := map[string]*ast.File{file.Name.Name: file}
// Ignore errors (from unknown packages)
pkg, _ := ast.NewPackage(fset, files, visitor.importer(), nil)
ast.Walk(visitor, pkg)
return &visitor.References, nil
}
开发者ID:pankona,项目名称:mobile,代码行数:11,代码来源:ast.go
示例13: NamedFunction
func (c *FunctionCache) NamedFunction(name string, signature string) llvm.Value {
f, _ := c.functions[name+":"+signature]
if !f.IsNil() {
return f
}
if strings.HasPrefix(name, c.module.Name+".") {
obj := c.pkg.Scope.Lookup(name[len(c.module.Name)+1:])
value := c.Resolve(obj)
f = value.LLVMValue()
} else {
fset := token.NewFileSet()
code := `package runtime;import("unsafe");` + signature + `{panic()}`
file, err := parser.ParseFile(fset, "", code, 0)
if err != nil {
panic(err)
}
// Parse the runtime package, since we may need to refer to
// its types. TODO cache the package.
buildpkg, err := build.Import("github.com/axw/llgo/pkg/runtime", "", 0)
if err != nil {
panic(err)
}
runtimefiles := make([]string, len(buildpkg.GoFiles))
for i, f := range buildpkg.GoFiles {
runtimefiles[i] = path.Join(buildpkg.Dir, f)
}
files, err := parseFiles(fset, runtimefiles)
if err != nil {
panic(err)
}
files["<src>"] = file
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
panic(err)
}
_, err = types.Check(fset, pkg)
if err != nil {
panic(err)
}
fdecl := file.Decls[len(file.Decls)-1].(*ast.FuncDecl)
ftype := fdecl.Name.Obj.Type.(*types.Func)
llvmfptrtype := c.types.ToLLVM(ftype)
f = llvm.AddFunction(c.module.Module, name, llvmfptrtype.ElementType())
if !strings.HasPrefix(name, "llvm.") {
f.SetLinkage(llvm.AvailableExternallyLinkage)
}
}
c.functions[name+":"+signature] = f
return f
}
开发者ID:spate,项目名称:llgo,代码行数:55,代码来源:runtime.go
示例14: ExtractStructs
//ExtractStructs extract struct infos from file
func ExtractStructs(inputPath string) (string, []*ActStructInfo, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments)
if err != nil {
return "", nil, err
}
packageName := f.Name.String()
structs := make(map[string]*ActStructInfo)
for k, d := range f.Scope.Objects {
if d.Kind == ast.Typ {
ts, ok := d.Decl.(*ast.TypeSpec)
if !ok {
return "", nil, fmt.Errorf("Unknown type without TypeSec: %v", d)
}
_, ok = ts.Type.(*ast.StructType)
if !ok {
_, ok = ts.Type.(*ast.ArrayType)
if !ok {
_, ok = ts.Type.(*ast.MapType)
if !ok {
continue
}
}
}
stobj := NewStructInfo(k)
structs[k] = stobj
}
}
files := map[string]*ast.File{
inputPath: f,
}
pkg, _ := ast.NewPackage(fset, files, nil, nil)
d := doc.New(pkg, f.Name.String(), doc.AllDecls)
for _, t := range d.Types {
if skipre.MatchString(t.Doc) {
delete(structs, t.Name)
}
}
var rv []*ActStructInfo
for _, v := range structs {
rv = append(rv, v)
}
return packageName, rv, nil
}
开发者ID:darvik80,项目名称:go-msgpack,代码行数:56,代码来源:act.go
示例15: loadPackage
func (ctx *Context) loadPackage(importPath string, flags int) (*Package, error) {
bpkg, err := build.Import(importPath, ctx.cwd, 0)
if _, ok := err.(*build.NoGoError); ok {
return &Package{bpkg: bpkg}, nil
}
if err != nil {
return nil, err
}
pkg := &Package{
fset: token.NewFileSet(),
bpkg: bpkg,
}
files := make(map[string]*ast.File)
for _, name := range append(pkg.bpkg.GoFiles, pkg.bpkg.CgoFiles...) {
file, err := pkg.parseFile(name)
if err != nil {
pkg.errors = append(pkg.errors, err)
continue
}
files[name] = file
}
pkg.apkg, _ = ast.NewPackage(pkg.fset, files, simpleImporter, nil)
if flags&loadDoc != 0 {
mode := doc.Mode(0)
if pkg.bpkg.ImportPath == "builtin" || flags&loadUnexported != 0 {
mode |= doc.AllDecls
}
pkg.dpkg = doc.New(pkg.apkg, pkg.bpkg.ImportPath, mode)
if pkg.bpkg.ImportPath == "builtin" {
for _, t := range pkg.dpkg.Types {
pkg.dpkg.Funcs = append(pkg.dpkg.Funcs, t.Funcs...)
t.Funcs = nil
}
sort.Sort(byFuncName(pkg.dpkg.Funcs))
}
}
if flags&loadExamples != 0 {
for _, name := range append(pkg.bpkg.TestGoFiles, pkg.bpkg.XTestGoFiles...) {
file, err := pkg.parseFile(name)
if err != nil {
pkg.errors = append(pkg.errors, err)
continue
}
pkg.examples = append(pkg.examples, doc.Examples(file)...)
}
}
return pkg, nil
}
开发者ID:purzelrakete,项目名称:dotfiles,代码行数:54,代码来源:context.go
示例16: processPackage
func processPackage(fset *token.FileSet, files map[string]*ast.File) {
// make a package (resolve all identifiers)
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
report(err)
return
}
if err = types.Check(fset, pkg, nil, nil); err != nil {
report(err)
}
}
开发者ID:redcatmiss,项目名称:gcc,代码行数:11,代码来源:gotype.go
示例17: LookupType
func LookupType(pkgs map[string]*ast.Package, name string) *ast.Object {
for k := range pkgs {
fset := token.NewFileSet()
pkg, err := ast.NewPackage(fset, pkgs[k].Files, nil, nil)
if err != nil {
// panic(err) // too much errors due to the abscence of an importer (incomplete types)
}
return pkg.Scope.Lookup(name)
}
return nil
}
开发者ID:konek,项目名称:struct-to-json,代码行数:11,代码来源:main.go
示例18: NamedFunction
func (c *FunctionCache) NamedFunction(name string, signature string) llvm.Value {
f, _ := c.functions[name+":"+signature]
if !f.IsNil() {
return f
}
if strings.HasPrefix(name, c.module.Name+".") {
obj := c.pkg.Scope.Lookup(name[len(c.module.Name)+1:])
value := c.Resolve(obj)
f = value.LLVMValue()
} else {
fset := token.NewFileSet()
code := `package runtime;import("unsafe");` + signature + `{panic()}`
file, err := parser.ParseFile(fset, "", code, 0)
if err != nil {
panic(err)
}
// Parse the runtime package, since we may need to refer to
// its types. Can't be cached, because type-checking can't
// be done twice on the AST.
buildpkg, err := build.Import("github.com/axw/llgo/pkg/runtime", "", 0)
if err != nil {
panic(err)
}
// All types visible to the compiler are in "types.go".
runtimefiles := []string{path.Join(buildpkg.Dir, "types.go")}
files, err := parseFiles(fset, runtimefiles)
if err != nil {
panic(err)
}
files["<src>"] = file
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
panic(err)
}
_, err = types.Check("", c.compiler, fset, pkg)
if err != nil {
panic(err)
}
fdecl := file.Decls[len(file.Decls)-1].(*ast.FuncDecl)
ftype := fdecl.Name.Obj.Type.(*types.Func)
llvmfptrtype := c.types.ToLLVM(ftype)
f = llvm.AddFunction(c.module.Module, name, llvmfptrtype.ElementType())
}
c.functions[name+":"+signature] = f
return f
}
开发者ID:kelsieflynn,项目名称:llgo,代码行数:53,代码来源:runtime.go
示例19: New
func New(p *build.Package, mode parser.Mode) (*token.FileSet, *ast.Package, error) {
fset := token.NewFileSet()
var names []string
names = append(names, p.GoFiles...)
names = append(names, p.CgoFiles...)
files, err := ParseFiles(fset, p.Dir, names, mode)
if err != nil {
return nil, nil, err
}
a, _ := ast.NewPackage(fset, files, Importer, nil)
return fset, a, nil
}
开发者ID:rainycape,项目名称:gondola,代码行数:12,代码来源:pkg.go
示例20: _importPackage
func (e Environment) _importPackage(p string, shallow bool) (*Package, error) {
b, err := e.importBuildPackage(p)
if err != nil {
if noBuildable(err) && !shallow {
dir, sub, err := e.importSubpackages(p)
if err != nil {
return nil, err
}
return &Package{name: path.Base(p), dir: dir, Packages: sub, env: e}, nil
}
}
fset := token.NewFileSet()
var names []string
names = append(names, b.GoFiles...)
names = append(names, b.CgoFiles...)
files, err := e.parseFiles(fset, b.Dir, names, parser.ParseComments)
if err != nil {
return nil, err
}
bodies := make(map[*ast.FuncDecl]*ast.BlockStmt)
for _, f := range files {
for _, d := range f.Decls {
if fn, ok := d.(*ast.FuncDecl); ok {
bodies[fn] = fn.Body
}
}
}
// NewPackage will always return errors because it won't
// resolve builtin types.
a, _ := ast.NewPackage(fset, files, astutil.Importer, nil)
flags := doc.AllMethods
if p == "builtin" {
flags |= doc.AllDecls
}
pkg := &Package{
fset: fset,
bpkg: b,
apkg: a,
dpkg: doc.New(a, b.ImportPath, flags),
bodies: bodies,
env: e,
}
if !shallow {
sub, err := e.ImportPackages(b.Dir)
if err != nil {
return nil, err
}
pkg.Packages = sub
}
return pkg, nil
}
开发者ID:rainycape,项目名称:gondola,代码行数:52,代码来源:package.go
注:本文中的go/ast.NewPackage函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论