本文整理汇总了Golang中go/ast.Print函数的典型用法代码示例。如果您正苦于以下问题:Golang Print函数的具体用法?Golang Print怎么用?Golang Print使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Print函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: printIncReturnsVerbose
func printIncReturnsVerbose(fset *token.FileSet, v map[*ast.ReturnStmt]*ast.FuncType) {
for ret, ftyp := range v {
fmt.Print("FUNC TYPE: ")
ast.Print(fset, ftyp)
fmt.Print(" RETURN: ")
ast.Print(fset, ret)
fmt.Println()
}
}
示例2: main
func main() {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "parsing.go", nil, 0)
if err != nil {
log.Fatalf("failed parsing file: %s", err)
}
ast.Print(fset, f)
expr, err := parser.ParseExpr(`foo.Bar(1, "argument", something())`)
if err != nil {
log.Fatal("failed parsing expression: %s", err)
}
ast.Print(nil, expr)
}
示例3: Visit
func (v *GoAstVisitor) Visit(node ast.Node) (w ast.Visitor) {
if node != nil {
fmt.Println(reflect.TypeOf(node))
if typeSpec, ok := node.(*ast.TypeSpec); ok {
ast.Print(fset, typeSpec)
v.TypeName = typeSpec.Name.Name
}
if funcSpec, ok := node.(*ast.FuncDecl); ok {
ast.Print(fset, funcSpec.Recv)
ast.Print(fset, funcSpec.Type)
v.Funcs = append(v.Funcs, funcSpec)
// funcSpec.Type.Params.List[0].Names[0].Name
}
}
return v
}
示例4: main
func main() {
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, "minimal.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
ast.Print(fileSet, astFile)
return
astFile.Decls = append(astFile.Decls[:0], append([]ast.Decl{&ast.FuncDecl{
Name: ast.NewIdent("MyNewFunc"),
Type: &ast.FuncType{
Func: 15,
Params: &ast.FieldList{
Opening: 29,
Closing: 30,
},
},
}}, astFile.Decls[0:]...)...)
offset := astFile.Decls[0].End() - astFile.Decls[0].Pos()
intoffset := int(offset)
fmt.Println("offset", offset)
astFile.Comments[0].List[0].Slash += offset
// astFile.Comments[0].List[0].Slash = 18
astFile.Decls[1].(*ast.GenDecl).TokPos += offset
astFile.Decls[1].(*ast.GenDecl).Lparen += offset
astFile.Decls[1].(*ast.GenDecl).Rparen += offset
fileSetFile := fileSet.File(1)
newFileSet := token.NewFileSet()
newFileSetFile := newFileSet.AddFile("whatever", 1, fileSetFile.Size()+int(offset))
newFileSetFile.SetLines([]int{
0,
13,
14,
15,
15 + intoffset,
20 + intoffset,
21 + intoffset,
32 + intoffset,
33 + intoffset,
}) // hardcoded for now
fmt.Println("astFile:")
spew.Dump(astFile)
fmt.Println()
fmt.Println()
fmt.Println("fileSet:")
spew.Dump(fileSet)
buf := new(bytes.Buffer)
err = printer.Fprint(buf, newFileSet, astFile)
if err != nil {
panic(err)
}
fmt.Println(buf)
}
示例5: NewTypeExpr
func NewTypeExpr(pkgName, importPath string, imports map[string]string, expr ast.Expr) (string, TypeExpr) {
switch t := expr.(type) {
case *ast.Ident:
if IsBultinType(t.Name) {
pkgName = ""
importPath = ""
}
return importPath, TypeExpr{t.Name, pkgName, 0, true}
case *ast.SelectorExpr:
_, e := NewTypeExpr(pkgName, importPath, imports, t.X)
return imports[e.Expr], TypeExpr{t.Sel.Name, e.Expr, e.PkgIdx, e.Valid}
case *ast.StarExpr:
i, e := NewTypeExpr(pkgName, importPath, imports, t.X)
return i, TypeExpr{"*" + e.Expr, e.PkgName, e.PkgIdx + 1, e.Valid}
case *ast.ArrayType:
i, e := NewTypeExpr(pkgName, importPath, imports, t.Elt)
return i, TypeExpr{"[]" + e.Expr, e.PkgName, e.PkgIdx + 2, e.Valid}
case *ast.Ellipsis:
i, e := NewTypeExpr(pkgName, importPath, imports, t.Elt)
return i, TypeExpr{"[]" + e.Expr, e.PkgName, e.PkgIdx + 3, e.Valid}
default:
log.Println("Failed to generate name for field.")
ast.Print(nil, expr)
}
return "", TypeExpr{Valid: false}
}
示例6: TestCheck
func TestCheck(t *testing.T) {
// src is the input for which we want to print the AST.
src := `
package main
// T comment
type T struct {
// S1 comment
S1 string
// S2 comment
S2 string
// S3 comment
S3 string
}
// E is a type E
type E int
const (
E1 E = 1
E2 E = 2
)
`
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
panic(err)
}
ast.Print(fset, f)
}
示例7: NewTypeExpr
// This returns the syntactic expression for referencing this type in Go.
func NewTypeExpr(pkgName string, expr ast.Expr) TypeExpr {
switch t := expr.(type) {
case *ast.Ident:
if IsBuiltinType(t.Name) {
pkgName = ""
}
return TypeExpr{t.Name, pkgName, 0}
case *ast.SelectorExpr:
e := NewTypeExpr(pkgName, t.X)
return TypeExpr{t.Sel.Name, e.Expr, 0}
case *ast.StarExpr:
e := NewTypeExpr(pkgName, t.X)
return TypeExpr{"*" + e.Expr, e.PkgName, e.pkgIndex + 1}
case *ast.ArrayType:
e := NewTypeExpr(pkgName, t.Elt)
return TypeExpr{"[]" + e.Expr, e.PkgName, e.pkgIndex + 2}
case *ast.Ellipsis:
e := NewTypeExpr(pkgName, t.Elt)
return TypeExpr{"[]" + e.Expr, e.PkgName, e.pkgIndex + 2}
default:
log.Println("Failed to generate name for field.")
ast.Print(nil, expr)
}
return TypeExpr{}
}
示例8: main
func main() {
var err error
var p *ast.Object
var pp []*ast.Object = make([]*ast.Object, 0)
for _, s := range packages {
p, err = types.GcImport(imports, s)
if err != nil {
panic(err)
}
pp = append(pp, p)
}
fset := token.NewFileSet()
for i, pkg := range pp {
Printf("\n")
Printf("%s: \n", packages[i])
Printf("\n")
Printf("pkg=%#v\n", pkg)
Printf("\n")
Printf("pkg.Data=%#v\n", pkg.Data)
Printf("\n")
ast.Print(fset, pkg)
Printf("\n")
Printf("\n")
}
}
示例9: Transform
func Transform(src *ast.File) *ast.File {
// find the golex import...
var toplevel ToplevelVisitor
for _, spec := range src.Imports {
ast.Print(nil, spec)
if conv, err := strconv.Unquote(spec.Path.Value); err != nil || conv != "golex" {
continue
}
if spec.Name != nil {
toplevel.golexPackage = spec.Name.Name
} else {
toplevel.golexPackage = "golex"
}
break
}
if toplevel.golexPackage == "" {
log.Print("Not a lex input")
return src
}
ast.Walk(&toplevel, src)
// Now, find switch statemnts...
return src
}
示例10: dumpAst
func dumpAst(files ...string) {
for _, arg := range files {
// Ensure file exists and not a directory
st, e := os.Stat(arg)
if e != nil {
fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", arg, e)
continue
}
if st.IsDir() {
fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", arg)
continue
}
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, arg, nil, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse file %s\n", err)
continue
}
// Print the AST.
ast.Print(fset, f)
}
}
示例11: main
func main() {
flag.Parse()
if len(flag.Args()) != 1 {
log.Fatal("Usage: go run goprint.go path")
}
bpkg, err := build.Default.Import(flag.Args()[0], ".", 0)
if err != nil {
log.Fatal(err)
}
fset := token.NewFileSet()
files := make(map[string]*ast.File)
for _, fname := range bpkg.GoFiles {
p, err := ioutil.ReadFile(filepath.Join(bpkg.SrcRoot, bpkg.ImportPath, fname))
if err != nil {
log.Fatal(err)
}
file, err := parser.ParseFile(fset, fname, p, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
files[fname] = file
}
c := spew.NewDefaultConfig()
c.DisableMethods = true
apkg, _ := ast.NewPackage(fset, files, importer, nil)
c.Dump(apkg)
ast.Print(fset, apkg)
dpkg := doc.New(apkg, bpkg.ImportPath, 0)
c.Dump(dpkg)
}
示例12: main
func main() {
// src is the input for which we want to print the AST.
src := `
package main
func anon(a int, b int) {
}
func named(a: int, b: int) {
}
func main() {
named(a: 3 + 2, b: 5)
}
`
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
// Print the AST.
ast.Print(fset, f)
fmt.Printf("%s\n", parser.RenderFile(f, fset))
}
示例13: main
func main() {
if len(os.Args) > 1 {
args(os.Args[1])
return
}
r := bufio.NewReader(os.Stdin)
for {
fmt.Print(">> ")
line, _, _ := r.ReadLine()
p := parser.ParseFromString("<REPL>", string(line)+"\n")
fmt.Println(p)
// a := generator.GenerateAST(p)
a := generator.EvalExprs(p)
fset := token.NewFileSet()
ast.Print(fset, a)
var buf bytes.Buffer
printer.Fprint(&buf, fset, a)
fmt.Printf("%s\n", buf.String())
}
}
示例14: ProcessFunc
func ProcessFunc(cur *Cursor, node *ast.FuncDecl) Candidates {
ast.Print(cur.FSet, GetOutNode(cur.Node, cur.Offset))
if cur.LeftWords != nil && len(cur.LeftWords) > 0 {
cands := Candidates{}
lastw := strings.ToLower(cur.GetLastWord())
pkg := cur.Asist.GetPackage(cur.PackagePath)
if lastw == "func" && cur.LastLetter == ' ' { //begin of func
file := pkg.GetFile(cur.File.GetPath())
if file == nil {
return nil
}
for _, t := range file.Types {
fmt.Println(t.Name, t.Type)
tmp := "(" + strings.ToLower(string(t.Name[0])) + " " + t.Name + ")"
cand := Candidate{Name: tmp}
if t.Type == "struct" {
cand.Perc = 50
}
cands = append(cands, cand)
}
return cands
}
}
return nil
}
示例15: Compile
// compiles source consisting of a number of statements. E.g.:
// src = "a = 1; b = sin(x)"
// code, err := world.Compile(src)
// code.Eval()
func (w *World) Compile(src string) (code *BlockStmt, e error) {
// parse
exprSrc := "func(){\n" + src + "\n}" // wrap in func to turn into expression
tree, err := parser.ParseExpr(exprSrc)
if err != nil {
return nil, fmt.Errorf("script line %v: ", err)
}
// catch compile errors and decode line number
if !Debug {
defer func() {
err := recover()
if err == nil {
return
}
if compErr, ok := err.(*compileErr); ok {
code = nil
e = fmt.Errorf("script %v: %v", pos2line(compErr.pos, exprSrc), compErr.msg)
} else {
panic(err)
}
}()
}
// compile
stmts := tree.(*ast.FuncLit).Body.List // strip func again
if Debug {
ast.Print(nil, stmts)
}
block := new(BlockStmt)
for _, s := range stmts {
block.append(w.compile(s), s)
}
return block, nil
}