本文整理汇总了Golang中go/ast.Fprint函数的典型用法代码示例。如果您正苦于以下问题:Golang Fprint函数的具体用法?Golang Fprint怎么用?Golang Fprint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fprint函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: compilePackage
func compilePackage(compiler llgo.Compiler, fset *token.FileSet, files map[string]*ast.File, importpath string) (*llgo.Module, error) {
// make a package (resolve all identifiers)
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
report(err)
return nil, err
}
// an empty importpath means the same as the package name
if importpath == "" {
importpath = pkg.Name
}
exprTypes, err := types.Check(importpath, compiler, fset, pkg)
if err != nil {
report(err)
return nil, err
}
if *dumpast {
ast.Fprint(os.Stderr, fset, pkg, nil)
os.Exit(0)
}
return compiler.Compile(fset, pkg, importpath, exprTypes)
}
开发者ID:kelsieflynn,项目名称:llgo,代码行数:26,代码来源:llgo.go
示例2: debugRender
func (f *file) debugRender(x interface{}) string {
var buf bytes.Buffer
if err := ast.Fprint(&buf, f.fset, x, nil); err != nil {
panic(err)
}
return buf.String()
}
开发者ID:swadhin4,项目名称:lint,代码行数:7,代码来源:lint.go
示例3: compilePackage
func compilePackage(fset *token.FileSet, files map[string]*ast.File) (*llgo.Module, error) {
// make a package (resolve all identifiers)
pkg, err := ast.NewPackage(fset, files, types.GcImport, types.Universe)
if err != nil {
report(err)
return nil, err
}
exprTypes, err := types.Check(fset, pkg)
if err != nil {
report(err)
return nil, err
}
if *dumpast {
ast.Fprint(os.Stderr, fset, pkg, nil)
os.Exit(0)
}
compiler := llgo.NewCompiler()
compiler.SetTraceEnabled(*trace)
compiler.SetTargetArch(*arch)
compiler.SetTargetOs(*os_)
return compiler.Compile(fset, pkg, exprTypes)
}
开发者ID:c0der007,项目名称:llgo,代码行数:25,代码来源:llgo.go
示例4: main
func main() {
var filename string
var src io.Reader
var err error
var astfile *ast.File
fargs := flag.Args()
if len(fargs) == 0 {
filename = "-"
src = os.Stdin
} else if len(fargs) == 1 {
filename = fargs[0]
src, err = os.Open(filename)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Printf("usage: %s [-h] [options] [file]\n", path.Base(os.Args[0]))
os.Exit(2)
}
fset := token.NewFileSet()
astfile, err = parser.ParseFile(fset, filename, src, mode)
if err != nil {
log.Fatal(err)
}
if *astPrint {
err = ast.Fprint(os.Stdout, fset, astfile, nil)
if err != nil {
log.Fatal(err)
}
}
}
开发者ID:crunchyroll,项目名称:goparse,代码行数:34,代码来源:main.go
示例5: nodeDebug
func (w *Walker) nodeDebug(node interface{}) string {
if node == nil {
return ""
}
var b bytes.Buffer
ast.Fprint(&b, w.fset, node, nil)
return b.String()
}
开发者ID:joninvski,项目名称:go,代码行数:8,代码来源:goapi.go
示例6: TestAddImport
func TestAddImport(t *testing.T) {
for _, test := range addTests {
file := parse(t, test.name, test.in)
var before bytes.Buffer
ast.Fprint(&before, fset, file, nil)
AddNamedImport(fset, file, test.renamedPkg, test.pkg)
if got := print(t, test.name, file); got != test.out {
if test.broken {
t.Logf("%s is known broken:\ngot: %s\nwant: %s", test.name, got, test.out)
} else {
t.Errorf("%s:\ngot: %s\nwant: %s", test.name, got, test.out)
}
var after bytes.Buffer
ast.Fprint(&after, fset, file, nil)
t.Logf("AST before:\n%s\nAST after:\n%s\n", before.String(), after.String())
}
}
}
开发者ID:Karthikvb,项目名称:15640_projects,代码行数:19,代码来源:imports_test.go
示例7: Parse
func Parse(filename string, source string) (a *Ast, dump string, err error) {
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, filename, source, 0)
// Print the AST.
var bf bytes.Buffer
ast.Fprint(&bf, fset, f, ast.NotNilFilter)
a, err = BuildAst("", f)
if err != nil {
return nil, "", err
}
return a, string(bf.Bytes()), nil
}
开发者ID:AnuchitPrasertsang,项目名称:goast-viewer,代码行数:16,代码来源:ast.go
示例8: main
func main() {
var input io.Reader
var err os.Error
var filename string
{
temp := "a⍨c"
fmt.Printf("Len: %v\n", len(temp))
for i := 0; i < len(temp); i++ {
fmt.Printf("%3d: %02x\n", i, temp[i])
}
}
if flag.NArg() > 0 {
filename = flag.Arg(0)
if input, err = os.Open(flag.Arg(0)); err != nil {
fmt.Print("Failed to open input: ", err)
os.Exit(1)
}
} else {
input = os.Stdin
filename = "stdin"
}
var src_ast *ast.File
fset := token.NewFileSet()
src_ast, err = parser.ParseFile(fset, filename, input, parser.ParseComments) // | parser.Trace)
if err != nil {
panic(err)
}
Transform(src_ast)
//dfa := CreateAutomata(source)
outfile, err := os.Create(*output_file)
if err != nil {
fmt.Print("Failed to open output file: ", err)
os.Exit(1)
}
//if err := printer.Fprint(outfile, fset, src_ast); err != nil {
//fmt.Printf("%#v\n", src_ast)
if _, err := ast.Fprint(outfile, fset, src_ast, nil); err != nil {
fmt.Print("Write failed: ", err)
os.Exit(1)
}
outfile.Close()
}
开发者ID:glenker,项目名称:go-lex,代码行数:47,代码来源:lex.go
示例9: Example
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 }
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:44,代码来源:parserutil_test.go
示例10: main
func main() {
if len(os.Args) != 2 {
fmt.Printf("usage: astprint <filename.go>\n")
return
}
// Создаём хранилище данных об исходных файлах
fset := token.NewFileSet()
// Вызываем парсер
if file, err := parser.ParseFile(
fset, // данные об исходниках
os.Args[1], // имя файла с исходником программы
nil, // пусть парсер сам загрузит исходник
parser.ParseComments, // приказываем сохранять комментарии
); err == nil {
// Если парсер отработал без ошибок, печатаем дерево
ast.Fprint(os.Stdout, fset, file, nil)
} else {
// в противном случае, выводим сообщение об ошибке
fmt.Printf("Error: %v", err)
}
}
开发者ID:bmstu-iu9,项目名称:compiler-labs,代码行数:23,代码来源:astprint.go
示例11: Process
func Process(filename string) (pkg string, mocks []*Mock, err os.Error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return "", nil, err
}
var toString func(interface{}) string
toString = func(x interface{}) (str string) {
switch x := x.(type) {
case *ast.BasicLit:
return x.Value
case *ast.Ident:
return x.Name
case *ast.ArrayType:
return "[" + toString(x.Len) + "]" + toString(x.Elt)
case *ast.MapType:
return "map[" + toString(x.Key) + "]" + toString(x.Value)
case *ast.SelectorExpr:
return toString(x.X) + "." + toString(x.Sel)
case nil:
return ""
default:
// TODO(kevlar): Ellipsis (variadic functions)
fmt.Fprintf(os.Stderr, "Unable to determine string for %T:\n", x)
ast.Fprint(os.Stderr, fset, x, nil)
}
return "unknown"
}
pkg = toString(file.Name)
for _, decl := range file.Decls {
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.TYPE {
for _, spec := range gen.Specs {
if tspec, ok := spec.(*ast.TypeSpec); ok {
if iface, ok := tspec.Type.(*ast.InterfaceType); ok {
mock := &Mock{
Name: toString(tspec.Name),
}
mocks = append(mocks, mock)
for _, method := range iface.Methods.List {
meth := &Method{
Name: toString(method.Names[0]),
}
mock.Methods = append(mock.Methods, meth)
if f, ok := method.Type.(*ast.FuncType); ok {
if f.Params != nil {
for _, param := range f.Params.List {
meth.Params = append(meth.Params, toString(param.Type))
}
}
if f.Results != nil {
for _, ret := range f.Results.List {
meth.Returns = append(meth.Returns, toString(ret.Type))
}
}
}
}
}
}
}
}
}
return
}
开发者ID:kylelemons,项目名称:gimmock,代码行数:67,代码来源:gimmock.go
示例12: oneFile
func oneFile(dir, s string, fset *token.FileSet, f *ast.File) error {
// Inspect the AST and change all instances of main()
isMain := false
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.File:
x.Name.Name = config.CmdName
case *ast.FuncDecl:
if x.Name.Name == "main" {
x.Name.Name = fmt.Sprintf("Main")
// Append a return.
x.Body.List = append(x.Body.List, &ast.ReturnStmt{})
isMain = true
}
case *ast.CallExpr:
debug("%v %v\n", reflect.TypeOf(n), n)
switch z := x.Fun.(type) {
case *ast.SelectorExpr:
// somebody tell me how to do this.
sel := fmt.Sprintf("%v", z.X)
// TODO: Need to have fixFlag and fixFlagVar
// as the Var variation has name in the SECOND argument.
if sel == "flag" {
if ix, ok := fixFlag[z.Sel.Name]; ok {
switch zz := x.Args[ix].(type) {
case *ast.BasicLit:
zz.Value = "\"" + config.CmdName + "." + zz.Value[1:]
}
}
}
}
}
return true
})
if *dumpAST {
ast.Fprint(os.Stderr, fset, f, nil)
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, f); err != nil {
panic(err)
}
debug("%s", buf.Bytes())
out := string(buf.Bytes())
// fix up any imports. We may have forced the issue
// with os.Args
opts := imports.Options{
Fragment: true,
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 8,
}
fullCode, err := imports.Process("commandline", []byte(out), &opts)
if err != nil {
log.Fatalf("bad parse: '%v': %v", out, err)
}
of := path.Join(dir, path.Base(s))
if err := ioutil.WriteFile(of, []byte(fullCode), 0666); err != nil {
log.Fatalf("%v\n", err)
}
// fun: must write the file first so the import fixup works :-)
if isMain {
// Write the file to interface to the command package.
t := template.Must(template.New("cmdFunc").Parse(cmdFunc))
var b bytes.Buffer
if err := t.Execute(&b, config); err != nil {
log.Fatalf("spec %v: %v\n", cmdFunc, err)
}
fullCode, err := imports.Process("commandline", []byte(b.Bytes()), &opts)
if err != nil {
log.Fatalf("bad parse: '%v': %v", out, err)
}
if err := ioutil.WriteFile(path.Join(config.Bbsh, "cmd_"+config.CmdName+".go"), fullCode, 0444); err != nil {
log.Fatalf("%v\n", err)
}
}
return nil
}
开发者ID:u-root,项目名称:u-root,代码行数:84,代码来源:bb.go
示例13: createAstFileDump
func createAstFileDump(filename string, fileSet *token.FileSet, astFile *ast.File) {
file, err := os.Create(filename)
util.PanicOnError(err)
defer file.Close()
ast.Fprint(file, fileSet, astFile, ast.NotNilFilter)
}
开发者ID:petergtz,项目名称:goextract,代码行数:6,代码来源:ast_utils.go
示例14: printNode
func printNode(n ast.Node) {
var w bytes.Buffer
ast.Fprint(&w, nil, n, nil)
fmt.Printf(w.String())
}
开发者ID:patrickToca,项目名称:ut,代码行数:5,代码来源:localtypes.go
注:本文中的go/ast.Fprint函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论