예제 #1
0
func Example() {
	stmt, err := parserutil.ParseStmt("var x int")
	if err != nil {
		panic(err)
	}

	ast.Fprint(os.Stdout, nil, stmt, nil)

	// Output:
	//      0  *ast.DeclStmt {
	//      1  .  Decl: *ast.GenDecl {
	//      2  .  .  Doc: nil
	//      3  .  .  TokPos: 31
	//      4  .  .  Tok: var
	//      5  .  .  Lparen: 0
	//      6  .  .  Specs: []ast.Spec (len = 1) {
	//      7  .  .  .  0: *ast.ValueSpec {
	//      8  .  .  .  .  Doc: nil
	//      9  .  .  .  .  Names: []*ast.Ident (len = 1) {
	//     10  .  .  .  .  .  0: *ast.Ident {
	//     11  .  .  .  .  .  .  NamePos: 35
	//     12  .  .  .  .  .  .  Name: "x"
	//     13  .  .  .  .  .  .  Obj: *ast.Object {
	//     14  .  .  .  .  .  .  .  Kind: var
	//     15  .  .  .  .  .  .  .  Name: "x"
	//     16  .  .  .  .  .  .  .  Decl: *(obj @ 7)
	//     17  .  .  .  .  .  .  .  Data: 0
	//     18  .  .  .  .  .  .  .  Type: nil
	//     19  .  .  .  .  .  .  }
	//     20  .  .  .  .  .  }
	//     21  .  .  .  .  }
	//     22  .  .  .  .  Type: *ast.Ident {
	//     23  .  .  .  .  .  NamePos: 37
	//     24  .  .  .  .  .  Name: "int"
	//     25  .  .  .  .  .  Obj: nil
	//     26  .  .  .  .  }
	//     27  .  .  .  .  Values: nil
	//     28  .  .  .  .  Comment: nil
	//     29  .  .  .  }
	//     30  .  .  }
	//     31  .  .  Rparen: 0
	//     32  .  }
	//     33  }
}
예제 #2
0
func getParent2ArgExprAllAsAst() []ast.Expr {
	// TODO: Replace use of debug.Stack() with direct use of runtime package...
	stack := string(stack())

	// TODO: Bounds error checking, get rid of GetLine gists, etc.
	parentName := getLine(stack, 5)
	if !strings.Contains(parentName, ": ") {
		// TODO: This happens when source file isn't present in same location as when built. See if can do anything better
		//       via direct use of runtime package (instead of debug.Stack(), which will exclude any func names)...
		return nil
	}
	parentName = parentName[1:strings.Index(parentName, ": ")]
	if dotPos := strings.LastIndex(parentName, "."); dotPos != -1 { // Trim package prefix.
		parentName = parentName[dotPos+1:]
	}

	str := getLine(stack, 7)
	str = str[strings.Index(str, ": ")+len(": "):]
	p, err := parserutil.ParseStmt(str)
	if err != nil {
		return nil
	}

	innerQuery := func(i interface{}) bool {
		if ident, ok := i.(*ast.Ident); ok && ident.Name == parentName {
			return true
		}
		return false
	}

	query := func(i interface{}) bool {
		if c, ok := i.(*ast.CallExpr); ok && nil != reflectfind.First(c.Fun, innerQuery) {
			return true
		}
		return false
	}
	callExpr, _ := reflectfind.First(p, query).(*ast.CallExpr)

	if callExpr == nil {
		return nil
	}
	return callExpr.Args
}