本文整理汇总了Golang中go/printer.Fprint函数的典型用法代码示例。如果您正苦于以下问题:Golang Fprint函数的具体用法?Golang Fprint怎么用?Golang Fprint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fprint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: fprint
func fprint(w io.Writer, a interface{}) error {
switch x := a.(type) {
case *ast.FieldList:
// printer.Fprint does not support this type. Hack around it.
return fprintFieldList(w, x)
case ast.Node, []ast.Decl, []ast.Stmt:
return printer.Fprint(w, token.NewFileSet(), x)
case []ast.Expr:
i := 0
for ; i < len(x)-1; i++ {
if err := printer.Fprint(w, token.NewFileSet(), x[i]); err != nil {
return err
}
if _, err := w.Write([]byte(", ")); err != nil {
return err
}
}
if len(x) != 0 {
return printer.Fprint(w, token.NewFileSet(), x[i])
}
return nil
case string:
_, err := io.WriteString(w, x)
return err
default:
panic(fmt.Sprintf("unsupported value: %v", x))
}
}
示例2: printIncReturns
func printIncReturns(fset *token.FileSet, v map[*ast.ReturnStmt]*ast.FuncType) {
for ret, ftyp := range v {
fmt.Print("FUNC TYPE: ")
printer.Fprint(os.Stdout, fset, ftyp)
fmt.Print(" RETURN: ")
printer.Fprint(os.Stdout, fset, ret)
fmt.Println()
}
}
示例3: Do
// Do creates a concrete ast from the generic one and writes it to the given io.Writer
func (g *Generify) Do(packageName string, w io.Writer) error {
var decls []ast.Decl
g.genTypes, decls = findGenerics(g.file)
if len(g.genTypes) == 0 {
return fmt.Errorf("no generic types found")
}
if len(g.genTypes) != len(g.concreteTypes.Instance[0]) {
return fmt.Errorf("there are %d generic types but %d concrete types", len(g.genTypes), len(g.concreteTypes.Instance[0]))
}
removeCommentsFrom(decls)
decls = splitDeclsToUngroupedDecls(decls)
g.genericDecls = g.inspectAllDeclsForDependencies(decls)
g.checkMethodDependencies()
g.renameStructsAndVars()
g.renameFunctions()
file := ast.File{Name: g.file.Name, Decls: g.staticDecls(), Scope: g.file.Scope, Imports: g.file.Imports}
if packageName != "" {
file.Name.Name = packageName
}
fset := token.NewFileSet()
err := printer.Fprint(w, fset, &file)
if err != nil {
return err
}
w.Write(newline)
for _, types := range g.concreteTypes.Instance {
// rename all identifiers
for _, ra := range g.renameActions {
ra.rename(types)
}
// write the renamed ast
for _, decl := range g.genericDecls {
if len(decl.usedTypes) > 0 && !decl.isAllreadyWritten(types) {
err := printer.Fprint(w, fset, decl.decl)
if err != nil {
return err
}
w.Write(newline)
}
}
}
return nil
}
示例4: rangestr
func (v *visitor) rangestr(rs *ast.RangeStmt) string {
v.buffer.Reset()
v.buffer.WriteString("for ")
printer.Fprint(&v.buffer, v.fset, rs.Key)
if rs.Value != nil {
v.buffer.WriteString(", ")
printer.Fprint(&v.buffer, v.fset, rs.Value)
}
v.buffer.WriteString(" := range ")
printer.Fprint(&v.buffer, v.fset, rs.X)
return v.buffer.String()
}
示例5: main
func main() {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "simple.go", nil, 0)
if err != nil {
// Whoops!
}
ast.Walk(new(FuncVisitor), file)
printer.Fprint(os.Stdout, fset, file)
fmt.Println("-------------------------------------")
ast.Walk(new(ImportVisitor), file)
printer.Fprint(os.Stdout, fset, file)
f, _ := os.Create("/tmp/new_simple.go")
printer.Fprint(f, fset, file)
}
示例6: render
func (f *file) render(x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, f.fset, x); err != nil {
panic(err)
}
return buf.String()
}
示例7: Process
// Process takes a Go source file and bumps version declaration according to conf.
// Returns the modified code and a map from identifiers to updated versions and an error, if any.
func (conf Config) Process(filename string, src interface{}) ([]byte, map[string]string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
return nil, nil, err
}
versions, err := conf.ProcessNode(fset, file)
if err != nil {
return nil, nil, err
}
var buf bytes.Buffer
err = printer.Fprint(&buf, fset, file)
if err != nil {
return nil, nil, err
}
out := buf.Bytes()
out, err = format.Source(out)
if err != nil {
return nil, nil, err
}
return out, versions, nil
}
示例8: render
func render(fset *token.FileSet, x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
panic(err)
}
return buf.String()
}
示例9: astString
func astString(x ast.Expr) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
panic(err)
}
return buf.String()
}
示例10: main
func main() {
if filename == "" || line == -1 || column == -1 || newName == "" {
fmt.Printf("%v %v %v \n", filename, line, column)
fmt.Printf("Usage: goref -f <filename> -l <line number> -c <column number> -n <new symbol name>\n")
return
}
newPack, count := RenameIdent(".", filename, line, column, newName)
if newPack != nil {
fmt.Printf("res : %v\n", count)
for fname, f := range newPack.Files {
if nf, err := os.OpenFile("refgo_out_"+fname, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
fmt.Printf("file %v already exists,cannot finish output\n", "refgo_out_"+fname)
return
} else {
printer.Fprint(nf, f)
nf.Close()
}
}
} else {
fmt.Printf("error : %v\n", count)
}
}
示例11: formatType
func (r *InterfaceGen) formatType(fileset *token.FileSet, field *ast.Field) *Type {
var typeBuf bytes.Buffer
_ = printer.Fprint(&typeBuf, fileset, field.Type)
if len(field.Names) == 0 {
fatalNode(fileset, field, "RPC interface parameters and results must all be named")
}
for _, typeName := range types(field.Type) {
parts := strings.SplitN(typeName, ".", 2)
if len(parts) > 1 {
for _, imp := range r.CheckImports {
importPath := imp.Path.Value[1 : len(imp.Path.Value)-1]
if imp.Name != nil && imp.Name.String() == parts[0] {
r.Imports[fmt.Sprintf("%s %s", imp.Name, importPath)] = true
} else if filepath.Base(importPath) == parts[0] {
r.Imports[importPath] = true
}
}
}
}
t := &Type{Type: typeBuf.String()}
for _, n := range field.Names {
lowerName := n.Name
name := strings.ToUpper(lowerName[0:1]) + lowerName[1:]
t.Names = append(t.Names, name)
t.LowerNames = append(t.LowerNames, lowerName)
}
return t
}
示例12: extractFunc
func extractFunc(funcName string, inputPath string) ([]string, string, error) {
fset := token.NewFileSet()
fp, err := parser.ParseFile(fset, inputPath, nil, 0)
if err != nil {
return nil, "", err
}
imports := make([]string, 0)
for _, imp := range fp.Imports {
imports = append(imports, imp.Path.Value)
}
var buf bytes.Buffer
for _, decl := range fp.Decls {
f, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if f.Name.Name != funcName {
continue
}
f.Name.Name = "ffjson_" + f.Name.Name
printer.Fprint(&buf, fset, f)
break
}
return imports, string(buf.Bytes()), nil
}
示例13: walkFile
func (w *Walker) walkFile(name string, file *ast.File) {
// Not entering a scope here; file boundaries aren't interesting.
for _, di := range file.Decls {
switch d := di.(type) {
case *ast.GenDecl:
switch d.Tok {
case token.IMPORT:
continue
case token.CONST:
for _, sp := range d.Specs {
w.walkConst(sp.(*ast.ValueSpec))
}
case token.TYPE:
for _, sp := range d.Specs {
w.walkTypeSpec(sp.(*ast.TypeSpec))
}
case token.VAR:
for _, sp := range d.Specs {
w.walkVar(sp.(*ast.ValueSpec))
}
default:
log.Fatalf("unknown token type %d in GenDecl", d.Tok)
}
case *ast.FuncDecl:
// Ignore. Handled in subsequent pass, by go/doc.
default:
log.Printf("unhandled %T, %#v\n", di, di)
printer.Fprint(os.Stderr, w.fset, di)
os.Stderr.Write([]byte("\n"))
}
}
}
示例14: 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())
}
}
示例15: args
func args(filename string) {
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
p := parser.ParseFromString(filename, string(b)+"\n")
a := generator.GenerateAST(p)
fset := token.NewFileSet()
defaultImports := []string{"github.com/gsp-lang/stdlib/prelude", "github.com/gsp-lang/gsp/core"}
for _, defaultImport := range defaultImports {
split := strings.Split(defaultImport, "/")
pkgName := split[len(split)-1]
if !(a.Name.Name == "prelude" && pkgName == "prelude") {
astutil.AddImport(fset, a, defaultImport)
}
}
if a.Name.Name != "prelude" {
a.Decls = append(a.Decls, &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{&ast.ValueSpec{
Names: []*ast.Ident{&ast.Ident{Name: "_"}},
Values: []ast.Expr{&ast.Ident{Name: "prelude.Len"}},
}},
})
}
var buf bytes.Buffer
printer.Fprint(&buf, fset, a)
fmt.Printf("%s\n", buf.String())
}