• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ast.Decl类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中go/ast.Decl的典型用法代码示例。如果您正苦于以下问题:Golang Decl类的具体用法?Golang Decl怎么用?Golang Decl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Decl类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: processDecl

func (self *PackageFile) processDecl(decl ast.Decl) {
	if self.scope != self.filescope {
		if t, ok := decl.(*ast.GenDecl); ok && t.Offset > self.cursor {
			return
		}
	}
	decls := splitDecls(decl)
	for _, decl := range decls {
		names := declNames(decl)
		values := declValues(decl)

		for i, name := range names {
			var value ast.Expr = nil
			valueindex := -1
			if values != nil {
				if len(values) > 1 {
					value = values[i]
				} else {
					value = values[0]
					valueindex = i
				}
			}

			d := NewDeclFromAstDecl(name, 0, decl, value, valueindex, self.scope)
			if d == nil {
				continue
			}

			methodof := MethodOf(decl)
			if methodof != "" {
				decl, ok := self.decls[methodof]
				if ok {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, self.scope)
					self.decls[methodof] = decl
					decl.AddChild(d)
				}
			} else {
				if self.scope != self.filescope {
					// the declaration itself has a scope which follows it's definition
					// and it's false for type declarations
					if d.Class != DECL_TYPE {
						self.scope = NewScope(self.scope)
						self.topscope = self.scope
					}
					self.scope.addNamedDecl(d)
				} else {
					self.addVarDecl(d)
				}
			}
		}
	}
}
开发者ID:kij,项目名称:gocode,代码行数:54,代码来源:file.go


示例2: memberFromDecl

func memberFromDecl(decl ast.Decl, imp *importer.Importer,
	consts []*string, funcs []*string, types []*string, vars []*string) (
	[]*string, []*string, []*string, []*string) {
	switch decl := decl.(type) {
	case *ast.GenDecl: // import, const, type or var
		switch decl.Tok {
		case token.CONST:
			for _, spec := range decl.Specs {
				for _, id := range spec.(*ast.ValueSpec).Names {
					if isExportedIdent(id) {
						consts = append(consts, &id.Name)
					}
				}
			}

		case token.VAR:
			for _, spec := range decl.Specs {
				for _, id := range spec.(*ast.ValueSpec).Names {
					if isExportedIdent(id) {
						vars = append(vars, &id.Name)
					}
				}
			}

		case token.TYPE:
			for _, spec := range decl.Specs {
				id := spec.(*ast.TypeSpec).Name
				if isExportedIdent(id) {
					types = append(types, &id.Name)
				}
			}
		}

	case *ast.FuncDecl:
		id := decl.Name
		if decl.Recv == nil && id.Name == "init" {
			return consts, funcs, types, vars
		}
		if isExportedIdent(id) && !strings.HasPrefix(id.Name, "Test") {
			// Can't handle receiver methods yet
			if decl.Recv == nil {
				filename := imp.Fset.File(decl.Pos()).Name()
				if !strings.HasSuffix(filename, "_test.go") {
					funcs = append(funcs, &id.Name)
				}
			}
		}
	}
	return consts, funcs, types, vars
}
开发者ID:raff,项目名称:go-fish,代码行数:50,代码来源:make_env.go


示例3: VisitDecl

func (c *compiler) VisitDecl(decl ast.Decl) Value {
	c.setDebugLine(decl.Pos())
	// This is temporary. We'll return errors later, rather than panicking.
	if c.Logger != nil {
		c.Logger.Println("Compile declaration:", c.fileset.Position(decl.Pos()))
	}
	defer func() {
		if e := recover(); e != nil {
			elist := new(scanner.ErrorList)
			elist.Add(c.fileset.Position(decl.Pos()), fmt.Sprint(e))
			panic(elist)
		}
	}()

	switch x := decl.(type) {
	case *ast.FuncDecl:
		return c.VisitFuncDecl(x)
	case *ast.GenDecl:
		c.VisitGenDecl(x)
		return nil
	}
	panic(fmt.Sprintf("Unhandled decl (%s) at %s\n",
		reflect.TypeOf(decl),
		c.fileset.Position(decl.Pos())))
}
开发者ID:qioixiy,项目名称:llgo,代码行数:25,代码来源:decl.go


示例4: append_to_top_decls

func append_to_top_decls(decls map[string]*decl, decl ast.Decl, scope *scope) {
	foreach_decl(decl, func(data *foreach_decl_struct) {
		class := ast_decl_class(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.type_value_index(i)

			d := new_decl_full(name.Name, class, 0, typ, v, vi, scope)
			if d == nil {
				return
			}

			methodof := method_of(decl)
			if methodof != "" {
				decl, ok := decls[methodof]
				if ok {
					decl.add_child(d)
				} else {
					decl = new_decl(methodof, decl_methods_stub, scope)
					decls[methodof] = decl
					decl.add_child(d)
				}
			} else {
				decl, ok := decls[d.name]
				if ok {
					decl.expand_or_replace(d)
				} else {
					decls[d.name] = d
				}
			}
		}
	})
}
开发者ID:ntcong,项目名称:sublime-text-2-config,代码行数:32,代码来源:declcache.go


示例5: appendToTopDecls

func appendToTopDecls(decls map[string]*Decl, decl ast.Decl, scope *Scope) {
	foreachDecl(decl, func(data *foreachDeclStruct) {
		class := astDeclClass(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.typeValueIndex(i, 0)

			d := NewDecl2(name.Name, class, 0, typ, v, vi, scope)
			if d == nil {
				return
			}

			methodof := MethodOf(decl)
			if methodof != "" {
				decl, ok := decls[methodof]
				if ok {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, scope)
					decls[methodof] = decl
					decl.AddChild(d)
				}
			} else {
				decl, ok := decls[d.Name]
				if ok {
					decl.ExpandOrReplace(d)
				} else {
					decls[d.Name] = d
				}
			}
		}
	})
}
开发者ID:Zeouterlimits,项目名称:gocode,代码行数:32,代码来源:declcache.go


示例6: add_ast_decl_to_package

func add_ast_decl_to_package(pkg *decl, decl ast.Decl, scope *scope) {
	foreach_decl(decl, func(data *foreach_decl_struct) {
		class := ast_decl_class(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.type_value_index(i)

			d := new_decl_full(name.Name, class, decl_foreign, typ, v, vi, scope)
			if d == nil {
				return
			}
			d.pkg = pkg
			for _, c := range d.children {
				c.pkg = pkg
			}

			if !name.IsExported() && d.class != decl_type {
				return
			}

			methodof := method_of(data.decl)
			if methodof != "" {
				decl := pkg.find_child(methodof)
				if decl != nil {
					decl.add_child(d)
				} else {
					decl = new_decl(methodof, decl_methods_stub, scope)
					decl.add_child(d)
					pkg.add_child(decl)
				}
			} else {
				decl := pkg.find_child(d.name)
				if decl != nil {
					decl.expand_or_replace(d)
				} else {
					pkg.add_child(d)
				}
			}
		}
	})
}
开发者ID:wesen,项目名称:gocode,代码行数:40,代码来源:package.go


示例7: addAstDeclToPackage

func addAstDeclToPackage(pkg *Decl, decl ast.Decl, scope *Scope) {
	foreachDecl(decl, func(data *foreachDeclStruct) {
		class := astDeclClass(data.decl)
		for i, name := range data.names {
			typ, v, vi := data.typeValueIndex(i, DECL_FOREIGN)

			d := NewDecl2(name.Name, class, DECL_FOREIGN, typ, v, vi, scope)
			if d == nil {
				return
			}

			if !name.IsExported() && d.Class != DECL_TYPE {
				return
			}

			methodof := MethodOf(data.decl)
			if methodof != "" {
				decl := pkg.FindChild(methodof)
				if decl != nil {
					decl.AddChild(d)
				} else {
					decl = NewDecl(methodof, DECL_METHODS_STUB, scope)
					decl.AddChild(d)
					pkg.AddChild(decl)
				}
			} else {
				decl := pkg.FindChild(d.Name)
				if decl != nil {
					decl.ExpandOrReplace(d)
				} else {
					pkg.AddChild(d)
				}
			}
		}
	})
}
开发者ID:senior7515,项目名称:gocode,代码行数:36,代码来源:package.go


示例8: getStructTypeDecl

// getStructTypeDecl checks if the given decl is a type declaration for a
// struct.  If so, the TypeSpec is returned.
func getStructTypeDecl(decl ast.Decl, fset *token.FileSet) (spec *ast.TypeSpec, found bool) {
	genDecl, ok := decl.(*ast.GenDecl)
	if !ok {
		return
	}

	if genDecl.Tok != token.TYPE {
		return
	}

	if len(genDecl.Specs) == 0 {
		revel.WARN.Printf("Surprising: %s:%d Decl contains no specifications", fset.Position(decl.Pos()).Filename, fset.Position(decl.Pos()).Line)
		return
	}

	spec = genDecl.Specs[0].(*ast.TypeSpec)
	_, found = spec.Type.(*ast.StructType)

	return
}
开发者ID:argon,项目名称:cmd,代码行数:22,代码来源:reflect.go


示例9: validTopLevelDecl

func (f *File) validTopLevelDecl(decl ast.Decl) *Error {
	if decl == nil {
		return &Error{errors.New("Top level decl is nil"), 0}
	}
	if genDecl, ok := decl.(*ast.GenDecl); ok {
		if genDecl.Tok != token.IMPORT {
			return &Error{errors.New(fmt.Sprintf("Top level decl is not import statement or function declaration, decl:%v", genDecl)), genDecl.Pos()}
		}
	} else if funcDecl, ok := decl.(*ast.FuncDecl); ok {
		// Only functions (not methods) are allowed
		if funcDecl.Recv != nil {
			return &Error{errors.New("Only functions (not methods) are allowed"), funcDecl.Recv.Pos()}
		}
		if err := f.validFuncType(funcDecl.Type); err != nil {

			return err
		}
		if _, _, err := f.validFuncBody(funcDecl.Body); err != nil {
			return err
		}
	} else {
		return &Error{errors.New(fmt.Sprintf("Top level decl is not import statement or function declaration, decl:%v", decl)), decl.Pos()}
	}
	return nil
}
开发者ID:bjwbell,项目名称:gensimd,代码行数:25,代码来源:parse.go



注:本文中的go/ast.Decl类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ast.Expr类代码示例发布时间:2022-05-28
下一篇:
Golang ast.CompositeLit类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap