本文整理汇总了Golang中go/ast.Scope.Lookup方法的典型用法代码示例。如果您正苦于以下问题:Golang Scope.Lookup方法的具体用法?Golang Scope.Lookup怎么用?Golang Scope.Lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.Scope
的用法示例。
在下文中一共展示了Scope.Lookup方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: declare
// Declare inserts a named object of the given kind in scope.
func (p *gcParser) declare(scope *ast.Scope, kind ast.ObjKind, name string) *ast.Object {
// a type may have been declared before - if it exists
// already in the respective package scope, return that
// type
if kind == ast.Typ {
if obj := scope.Lookup(name); obj != nil {
assert(obj.Kind == ast.Typ)
return obj
}
}
// any other object must be a newly declared object -
// create it and insert it into the package scope
obj := ast.NewObj(kind, name)
if scope.Insert(obj) != nil {
p.errorf("already declared: %v %s", kind, obj.Name)
}
// a new type object is a named type and may be referred
// to before the underlying type is known - set it up
if kind == ast.Typ {
obj.Type = &Name{Obj: obj}
}
return obj
}
示例2: Lookup
func Lookup(scope *ast.Scope, name string) *ast.Object {
for scope != nil {
if obj := scope.Lookup(name); obj != nil {
return obj
}
scope = scope.Outer
}
return nil
}
示例3: findIdentInScope
func (p *parser) findIdentInScope(scope *ast.Scope) *ast.Ident {
pos := p.pos
name := "_"
var obj *ast.Object
if p.tok == token.IDENT {
name = string(p.lit)
obj = scope.Lookup(name)
p.next()
} else {
p.expect(token.IDENT) // use expect() error handling
}
if obj == nil {
// TODO(gri) At the moment we always arrive here because
// we don't track the lookup scope (and sometimes
// we can't). Just create a useable ident for now.
obj = ast.NewObj(ast.Err, pos, name)
}
return &ast.Ident{pos, obj}
}
示例4: declare
// Declare inserts a named object of the given kind in scope.
func (p *gcParser) declare(scope *ast.Scope, kind ast.ObjKind, name string) *ast.Object {
// the object may have been imported before - if it exists
// already in the respective package scope, return that object
if obj := scope.Lookup(name); obj != nil {
assert(obj.Kind == kind)
return obj
}
// otherwise create a new object and insert it into the package scope
obj := ast.NewObj(kind, name)
if scope.Insert(obj) != nil {
p.errorf("already declared: %v %s", kind, obj.Name)
}
// if the new type object is a named type it may be referred
// to before the underlying type is known - set it up
if kind == ast.Typ {
obj.Type = &NamedType{Obj: obj}
}
return obj
}