本文整理汇总了Golang中go/ast.Decl.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang Decl.Pos方法的具体用法?Golang Decl.Pos怎么用?Golang Decl.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.Decl
的用法示例。
在下文中一共展示了Decl.Pos方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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())))
}
示例3: 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
}
示例4: 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
}