本文整理汇总了Golang中go/ast.Node类的典型用法代码示例。如果您正苦于以下问题:Golang Node类的具体用法?Golang Node怎么用?Golang Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Node类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Visit
func (v *visitor) Visit(node ast.Node) ast.Visitor {
switch node := node.(type) {
case *ast.FuncDecl:
v.funcName = node.Name.Name
v.m = make(map[*ast.Object][]string)
case *ast.DeferStmt:
if sel, ok := node.Call.Fun.(*ast.SelectorExpr); ok {
if ident, ok := sel.X.(*ast.Ident); ok {
if selectors, ok := v.m[ident.Obj]; !ok {
v.m[ident.Obj] = []string{sel.Sel.Name}
} else {
found := false
for _, selname := range selectors {
if selname == sel.Sel.Name {
pos := v.fset.Position(node.Pos())
fmt.Printf("%s: %s:%d:%d: Repeating defer %s.%s() inside function %s\n",
v.pkgPath, pos.Filename, pos.Line, pos.Column,
ident.Name, selname, v.funcName)
found = true
exitStatus = 1
break
}
}
if !found {
v.m[ident.Obj] = append(selectors, sel.Sel.Name)
}
}
}
}
}
return v
}
开发者ID:aganno2,项目名称:check,代码行数:33,代码来源:defercheck.go
示例2: errorf
// The variadic arguments may start with link and category types,
// and must end with a format string and any arguments.
func (f *file) errorf(n ast.Node, confidence float64, args ...interface{}) {
if confidence < f.config.MinConfidence {
return
}
p := f.fset.Position(n.Pos())
problem := Problem{
File: f.filename,
Position: p,
Confidence: confidence,
LineText: srcLine(f.src, p),
}
argLoop:
for len(args) > 1 { // always leave at least the format string in args
switch v := args[0].(type) {
case link:
problem.Link = string(v)
case category:
problem.Category = string(v)
default:
break argLoop
}
args = args[1:]
}
problem.Text = fmt.Sprintf(args[0].(string), args[1:]...)
f.problems = append(f.problems, problem)
}
开发者ID:plurodel,项目名称:hint,代码行数:32,代码来源:lint.go
示例3: printNode
func (f *File) printNode(node, ident ast.Node, url string) {
if !f.doPrint {
f.found = true
return
}
fmt.Printf("%s%s%s", url, f.sourcePos(f.fset.Position(ident.Pos())), f.docs(node))
}
开发者ID:nguyentm83,项目名称:liteide,代码行数:7,代码来源:doc.go
示例4: trim
func (lp *linePrinter) trim(n ast.Node) bool {
stmt, ok := n.(ast.Stmt)
if !ok {
return true
}
line := lp.fset.Position(n.Pos()).Line
if line != lp.line {
return false
}
switch stmt := stmt.(type) {
case *ast.IfStmt:
stmt.Body = lp.trimBlock(stmt.Body)
case *ast.SwitchStmt:
stmt.Body = lp.trimBlock(stmt.Body)
case *ast.TypeSwitchStmt:
stmt.Body = lp.trimBlock(stmt.Body)
case *ast.CaseClause:
stmt.Body = lp.trimList(stmt.Body)
case *ast.CommClause:
stmt.Body = lp.trimList(stmt.Body)
case *ast.BlockStmt:
stmt.List = lp.trimList(stmt.List)
}
return true
}
开发者ID:9cc9,项目名称:dea_ng,代码行数:25,代码来源:printer.go
示例5: Visit
func (v *funcVisitor) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.FuncDecl:
// Function name is prepended with "T." if there is a receiver, where
// T is the type of the receiver, dereferenced if it is a pointer.
name := n.Name.Name
if n.Recv != nil {
field := n.Recv.List[0]
switch recv := field.Type.(type) {
case *ast.StarExpr:
name = recv.X.(*ast.Ident).Name + "." + name
case *ast.Ident:
name = recv.Name + "." + name
}
}
start, end := v.fset.Position(n.Pos()), v.fset.Position(n.End())
f := v.pkg.RegisterFunction(name, start.Filename, start.Offset, end.Offset)
v.state.functions = append(v.state.functions, f)
sv := &stmtVisitor{v.state}
if n.Body != nil {
sv.VisitStmt(n.Body)
}
// TODO function coverage (insert "function.Enter", "function.Leave").
// TODO come up with naming scheme for function literals.
// case *ast.FuncLit:
}
return v
}
开发者ID:fiber,项目名称:gocov,代码行数:29,代码来源:instrument.go
示例6: getSourceString
func getSourceString(node ast.Node, fset *token.FileSet) string {
p1 := fset.Position(node.Pos())
p2 := fset.Position(node.End())
b := getFileBytes(p1.Filename)
return string(b[p1.Offset:p2.Offset])
}
开发者ID:serussell,项目名称:gen,代码行数:7,代码来源:main.go
示例7: normalizeNodePos
// normalizeNodePos resets all position information of node and its descendants.
func normalizeNodePos(node ast.Node) {
ast.Inspect(node, func(node ast.Node) bool {
if node == nil {
return true
}
if node.Pos() == token.NoPos && node.End() == token.NoPos {
return true
}
pv := reflect.ValueOf(node)
if pv.Kind() != reflect.Ptr {
return true
}
v := pv.Elem()
if v.Kind() != reflect.Struct {
return true
}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := f.Type()
if f.CanSet() && ft.PkgPath() == "go/token" && ft.Name() == "Pos" && f.Int() != 0 {
f.SetInt(1)
}
}
return true
})
}
开发者ID:gopherds,项目名称:gophernotes,代码行数:32,代码来源:node.go
示例8: posLink_urlFunc
func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
var relpath string
var line int
var low, high int // selection
if p := node.Pos(); p.IsValid() {
pos := fset.Position(p)
relpath = pos.Filename
line = pos.Line
low = pos.Offset
}
if p := node.End(); p.IsValid() {
high = fset.Position(p).Offset
}
var buf bytes.Buffer
template.HTMLEscape(&buf, []byte(relpath))
// selection ranges are of form "s=low:high"
if low < high {
fmt.Fprintf(&buf, "?s=%d:%d", low, high) // no need for URL escaping
// if we have a selection, position the page
// such that the selection is a bit below the top
line -= 10
if line < 1 {
line = 1
}
}
// line id's in html-printed source are of the
// form "L%d" where %d stands for the line number
if line > 0 {
fmt.Fprintf(&buf, "#L%d", line) // no need for URL escaping
}
return buf.String()
}
开发者ID:tw4452852,项目名称:go-src,代码行数:35,代码来源:godoc.go
示例9: errorf
// The variadic arguments may start with link and category types,
// and must end with a format string and any arguments.
// It returns the new Problem.
func (f *file) errorf(n ast.Node, confidence float64, args ...interface{}) *Problem {
pos := f.fset.Position(n.Pos())
if pos.Filename == "" {
pos.Filename = f.filename
}
return f.pkg.errorfAt(pos, confidence, args...)
}
开发者ID:swadhin4,项目名称:lint,代码行数:10,代码来源:lint.go
示例10: findCalls
/*
* findCalls() parses the node passed in and, if it's a
* SelectorExpr (which any call to a function/method will be)
* populates the map passed in as the second argument.
* This is the function used for finder.find().
*
* Functions will have the package as the X node,
* whereas methods will have the object identifier as the
* X. We do not differentiate in this function (there
* really isn't a good way without access to the list of
* imports and identifiers, so that has to be done higher up
* the stack).
*/
func findCalls(n ast.Node, f *finder) bool {
switch x := n.(type) {
case *ast.File:
locfields := strings.Split(fset.Position(n.Pos()).String(), ":")
f.currentFile = locfields[0]
case *ast.SelectorExpr:
switch y := x.X.(type) {
case *ast.Ident:
locfields := strings.Split(fset.Position(y.NamePos).String(), ":")
ln, err := strconv.Atoi(locfields[1])
if err != nil {
ln = -1
}
f.dset.AddPackageCall(Call{
Qual: y.Name,
Sel: x.Sel.Name,
// Location: y.NamePos,
Line: ln,
}, f.currentFile, true)
default:
return true
} // END switch y :=
default:
return true
} // END switch x :=
return true
}
开发者ID:ScarletTanager,项目名称:gimports,代码行数:40,代码来源:util.go
示例11: defineImplicit
// Used for implicit objects created by some ImportSpecs and CaseClauses.
func (r *resolver) defineImplicit(b *Block, n ast.Node, name string) {
obj := r.info.Implicits[n]
if obj == nil {
logf("%s: internal error: not an implicit definition: %T\n",
r.fset.Position(n.Pos()), n)
}
r.defineObject(b, name, obj)
}
开发者ID:Christeefym,项目名称:lantern,代码行数:9,代码来源:lexical.go
示例12: newIssueRangeFromNode
func (f *gofile) newIssueRangeFromNode(n ast.Node) *issueRange {
s := f.Fset().Position(n.Pos())
e := f.Fset().Position(n.End())
return &issueRange{
s,
e,
}
}
开发者ID:guycook,项目名称:tenets,代码行数:8,代码来源:file.go
示例13: definePkg
func (a *stmtCompiler) definePkg(ident ast.Node, id, path string) *PkgIdent {
v, prev := a.block.DefinePackage(id, path, ident.Pos())
if prev != nil {
a.diagAt(ident.Pos(), "%s redeclared as imported package name\n\tprevious declaration at %s", id, a.fset.Position(prev.Pos()))
return nil
}
return v
}
开发者ID:hdczsf,项目名称:go-eval,代码行数:8,代码来源:stmt.go
示例14: errorf
func (f *file) errorf(n ast.Node, confidence float64, format string, a ...interface{}) {
p := f.fset.Position(n.Pos())
f.problems = append(f.problems, Problem{
Position: p,
Text: fmt.Sprintf(format, a...),
Confidence: confidence,
LineText: srcLine(f.src, p),
})
}
开发者ID:GeertJohan,项目名称:lint,代码行数:9,代码来源:lint.go
示例15: Visit
// Visit implements ast.Visistor's Visit method
func (t *prohibitVisitor) Visit(node ast.Node) ast.Visitor {
if node == nil {
return t
}
if filterChanStmtOrExpr(node) != nil {
t.AddError(node.Pos(), fmt.Sprintf("Channel operation in non top-level block: %v", node))
}
return t
}
开发者ID:petar,项目名称:vitamix,代码行数:10,代码来源:prohibit.go
示例16: addAnnoation
func (v *annotationVisitor) addAnnoation(n ast.Node, packageName string, name string) {
pos := v.fset.Position(n.Pos())
end := v.fset.Position(n.End())
v.annotations = append(v.annotations, TypeAnnotation{
pos.Offset - len(packageWrapper),
end.Offset - len(packageWrapper),
packageName,
name})
}
开发者ID:goods,项目名称:thegoods,代码行数:9,代码来源:build.go
示例17: compile
// compiles expression or statement
func (w *World) compile(n ast.Node) Expr {
switch n := n.(type) {
case ast.Stmt:
return w.compileStmt(n)
case ast.Expr:
return w.compileExpr(n)
default:
panic(err(n.Pos(), "not allowed"))
}
}
开发者ID:kyeongdong,项目名称:3,代码行数:11,代码来源:stmt.go
示例18: Visit
func (vis *findNodeVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node == nil || vis.result != nil {
return nil
}
if utils.ComparePosWithinFile(vis.stPos, vis.fset.Position(node.Pos())) == 0 &&
utils.ComparePosWithinFile(vis.endPos, vis.fset.Position(node.End())) == 0 {
vis.result = node
return nil
}
return vis
}
开发者ID:vpavkin,项目名称:GoRefactor,代码行数:11,代码来源:findNodeVisitor.go
示例19: nodeLabel
func nodeLabel(fset *token.FileSet, x ast.Node) string {
pos := fset.Position(x.Pos())
label := fmt.Sprintf("%T %s:%d", x, pos.Filename, pos.Line)
switch x := x.(type) {
case *ast.Ident:
label = x.Name + " " + label
case *ast.SelectorExpr:
label = "." + x.Sel.Name + " " + label
}
return label
}
开发者ID:xwb1989,项目名称:grind,代码行数:11,代码来源:vardecl.go
示例20: indentOf
func (f *file) indentOf(node ast.Node) string {
line := srcLine(f.src, f.fset.Position(node.Pos()))
for i, r := range line {
switch r {
case ' ', '\t':
default:
return line[:i]
}
}
return line // unusual or empty line
}
开发者ID:swadhin4,项目名称:lint,代码行数:11,代码来源:lint.go
注:本文中的go/ast.Node类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论