当前位置: 首页>>代码示例>>Golang>>正文


Golang token.NewFileSet函数代码示例

本文整理汇总了Golang中go/token.NewFileSet函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFileSet函数的具体用法?Golang NewFileSet怎么用?Golang NewFileSet使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewFileSet函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetPackageList

func GetPackageList() {
	for _, fname := range files {
		fset := token.NewFileSet()
		file, err := parser.ParseFile(fset, fname, nil, parser.PackageClauseOnly)
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
		pname := file.Name.Name
		if pname == "main" {
			fset := token.NewFileSet()
			fullfile, err := parser.ParseFile(fset, fname, nil, 0)
			if err == nil {
				v := &MainCheckVisitor{fname: fname}
				ast.Walk(v, fullfile)
				if v.hasMain {
					// get the name from the filename
					fparts := strings.Split(fname, ".", -1)
					basename := path.Base(fparts[0])
					packages[basename] = nil
				} else {
					packages[pname] = nil
				}
			}
		} else {
			packages[file.Name.Name] = nil
		}
	}
}
开发者ID:alberts,项目名称:gomake,代码行数:29,代码来源:goinfo.go

示例2: generateRunner

func generateRunner(filename string, testMains []*TestMain) os.Error {
	src := bytes.NewBufferString("")
	fmt.Fprint(src, "package main\n\n")
	fmt.Fprint(src, "import \"sync\"\n")
	fmt.Fprint(src, "import \"testing\"\n")
	fmt.Fprint(src, "import (\n")
	for _, testMain := range testMains {
		name := testMain.underscorePkgName()
		fmt.Fprintf(src, "%s \"%s\"\n", name, testMain.pkgName)
	}
	fmt.Fprint(src, ")\n")
	fmt.Fprint(src, "func main() {\n")
	fmt.Fprint(src, "wg := new(sync.WaitGroup)\n")
	for _, testMain := range testMains {
		pkgName := testMain.underscorePkgName()
		fmt.Fprint(src, "wg.Add(1)\n")
		fmt.Fprint(src, "go func() {\n")
		fmt.Fprint(src, "tests := []testing.InternalTest{\n")
		for _, test := range testMain.tests {
			testFunc := pkgName + "." + test
			fmt.Fprintf(src, "{\"%s\", %s},\n", testMain.pkgName+"."+test, testFunc)
		}
		fmt.Fprint(src, "}\n")
		fmt.Fprint(src, "benchmarks := []testing.InternalBenchmark{\n")
		for _, bench := range testMain.benchmarks {
			benchFunc := pkgName + "." + bench
			fmt.Fprintf(src, "{\"%s\", %s},\n", testMain.pkgName+"."+bench, benchFunc)
		}
		fmt.Fprint(src, "}\n")
		fmt.Fprintf(src, "for i := 0; i < %d; i++ {\n", iters)
		fmt.Fprint(src, "testing.Main(regexp.MatchString, tests)\n")
		fmt.Fprint(src, "testing.RunBenchmarks(regexp.MatchString, benchmarks)\n")
		fmt.Fprint(src, "}\n")
		fmt.Fprint(src, "wg.Done()\n")
		fmt.Fprint(src, "}()\n\n")
	}
	fmt.Fprint(src, "wg.Wait()\n")
	fmt.Fprint(src, "}\n")

	file, err := os.Open(filename, os.O_CREAT|os.O_TRUNC|os.O_WRONLY, 0666)
	if err != nil {
		return err
	}
	defer file.Close()

	//fmt.Printf("%s\n", string(src.Bytes()))

	fileNode, err := parser.ParseFile(token.NewFileSet(), filename, src.Bytes(), 0)
	if err != nil {
		panic(err)
	}

	config := printer.Config{printer.TabIndent, 8}
	_, err = config.Fprint(file, token.NewFileSet(), fileNode)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:alberts,项目名称:gostress,代码行数:60,代码来源:gostress.go

示例3: 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))
	}
}
开发者ID:philipmulcahy,项目名称:godebug,代码行数:28,代码来源:astutil.go

示例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)

}
开发者ID:petergtz,项目名称:goextract,代码行数:60,代码来源:exploring.go

示例5: Output

func (m Mocks) Output(pkg, dir string, chanSize int, dest io.Writer) error {
	if _, err := dest.Write([]byte(commentHeader)); err != nil {
		return err
	}

	fset := token.NewFileSet()

	f := &ast.File{
		Name:  &ast.Ident{Name: pkg},
		Decls: m.decls(chanSize),
	}

	var b bytes.Buffer
	format.Node(&b, fset, f)

	// TODO: Determine why adding imports without creating a new ast file
	// will only allow one import to be printed to the file.
	fset = token.NewFileSet()
	file, err := parser.ParseFile(fset, pkg, &b, 0)
	if err != nil {
		return err
	}

	file, fset, err = addImports(file, fset, dir)
	if err != nil {
		return err
	}

	return format.Node(dest, fset, file)
}
开发者ID:nelsam,项目名称:hel,代码行数:30,代码来源:mocks.go

示例6: doParseFiles

func doParseFiles(filePathes []string, fset *token.FileSet) (*token.FileSet, []*ast.File, error) {
	if fset == nil {
		fset = token.NewFileSet()
	}
	util.Info("parsing files %v", filePathes)
	astFiles := make([]*ast.File, 0, len(filePathes))
	for _, f := range filePathes {
		//XXX: Ignoring files with packages ends with _test.
		//XXX: Doing that because getting error in check()
		//XXX: cause source file is still going to current
		//XXX: packages. Need to analyze package before
		//XXX: and check both packages separately.
		tempFset := token.NewFileSet()
		astFile, err := parser.ParseFile(tempFset, f, nil, 0)
		if !strings.HasSuffix(astFile.Name.Name, "_test") {
			if err != nil {
				return nil, nil, err
			}
			astFile, _ := parser.ParseFile(fset, f, nil, 0)
			astFiles = append(astFiles, astFile)
		}
	}

	iterateFunc := func(f *token.File) bool {
		util.Debug("\t%s", f.Name())
		return true
	}
	fset.Iterate(iterateFunc)
	return fset, astFiles, nil
}
开发者ID:dooman87,项目名称:gounexport,代码行数:30,代码来源:importer.go

示例7: TestVeryLongFile

// TestVeryLongFile tests the position of an import object declared in
// a very long input file.  Line numbers greater than maxlines are
// reported as line 1, not garbage or token.NoPos.
func TestVeryLongFile(t *testing.T) {
	// parse and typecheck
	longFile := "package foo" + strings.Repeat("\n", 123456) + "var X int"
	fset1 := token.NewFileSet()
	f, err := parser.ParseFile(fset1, "foo.go", longFile, 0)
	if err != nil {
		t.Fatal(err)
	}
	var conf types.Config
	pkg, err := conf.Check("foo", fset1, []*ast.File{f}, nil)
	if err != nil {
		t.Fatal(err)
	}

	// export
	exportdata := gcimporter.BExportData(fset1, pkg)

	// import
	imports := make(map[string]*types.Package)
	fset2 := token.NewFileSet()
	_, pkg2, err := gcimporter.BImportData(fset2, imports, exportdata, pkg.Path())
	if err != nil {
		t.Fatalf("BImportData(%s): %v", pkg.Path(), err)
	}

	// compare
	posn1 := fset1.Position(pkg.Scope().Lookup("X").Pos())
	posn2 := fset2.Position(pkg2.Scope().Lookup("X").Pos())
	if want := "foo.go:1:1"; posn2.String() != want {
		t.Errorf("X position = %s, want %s (orig was %s)",
			posn2, want, posn1)
	}
}
开发者ID:tsandall,项目名称:opa,代码行数:36,代码来源:bexport_test.go

示例8: rewriteImportsInFile

// inspired by godeps rewrite, rewrites import paths with gx vendored names
func rewriteImportsInFile(fi string, rw func(string) string) error {
	cfg := &printer.Config{Mode: printer.UseSpaces | printer.TabIndent, Tabwidth: 8}
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, fi, nil, parser.ParseComments)
	if err != nil {
		return err
	}

	var changed bool
	for _, imp := range file.Imports {
		p, err := strconv.Unquote(imp.Path.Value)
		if err != nil {
			return err
		}

		np := rw(p)

		if np != p {
			changed = true
			imp.Path.Value = strconv.Quote(np)
		}
	}

	if !changed {
		return nil
	}

	buf := bufpool.Get().(*bytes.Buffer)
	if err = cfg.Fprint(buf, fset, file); err != nil {
		return err
	}

	fset = token.NewFileSet()
	file, err = parser.ParseFile(fset, fi, buf, parser.ParseComments)
	if err != nil {
		return err
	}

	buf.Reset()
	bufpool.Put(buf)

	ast.SortImports(fset, file)

	wpath := fi + ".temp"
	w, err := os.Create(wpath)
	if err != nil {
		return err
	}

	if err = cfg.Fprint(w, fset, file); err != nil {
		return err
	}

	if err = w.Close(); err != nil {
		return err
	}

	return os.Rename(wpath, fi)
}
开发者ID:whyrusleeping,项目名称:gx-go,代码行数:60,代码来源:rewrite.go

示例9: extractTests

func extractTests(pkgs []string, except []string, pkgOut string, output io.Writer) error {
	here, err := os.Getwd()
	if err != nil {
		return err
	}
	fset := token.NewFileSet()
	result := &ast.File{}
	result.Name = &ast.Ident{
		Name: pkgOut,
	}
	result.Imports = append(result.Imports, &ast.ImportSpec{
		Path: &ast.BasicLit{
			Kind:  token.STRING,
			Value: "\"testing\"",
		},
	})

	for _, pkgName := range pkgs {
		pkg, err := build.Import(pkgName, here, 0)
		if err != nil {
			return err
		}
		result.Imports = append(result.Imports, &ast.ImportSpec{
			Path: &ast.BasicLit{
				Kind:  token.STRING,
				Value: "\"" + pkg.ImportPath + "\"",
			},
		})
		pkgFiles := token.NewFileSet()
		for _, src := range pkg.GoFiles {
			qsrc := filepath.Join(pkg.Dir, src)
			sfile, err := parser.ParseFile(pkgFiles, qsrc, nil, 0)
			if err != nil {
				return err
			}
			err = copyTests(result, sfile, except, pkg)
			if err != nil {
				return err
			}
		}
	}

	importDecls := make([]ast.Decl, len(result.Imports))
	for i, spec := range result.Imports {
		importDecls[i] = &ast.GenDecl{
			Tok:   token.IMPORT,
			Specs: []ast.Spec{spec},
		}
	}
	result.Decls = append(importDecls, result.Decls...)

	return format.Node(output, fset, result)
}
开发者ID:diffeo,项目名称:go-coordinate,代码行数:53,代码来源:main.go

示例10: compareTwoTrees

func compareTwoTrees(src string) {
	v1 := &channelPusher{}
	v1.fileSet = token.NewFileSet()
	v1.queue = make(chan *ast.Node)

	v2 := &channelPusher{}
	v2.fileSet = token.NewFileSet()
	v2.queue = make(chan *ast.Node)

	tree1, err := parser.ParseExpr(src)
	if err != nil {
		panic(err)
	}

	src2 := "x + 2*y"
	tree2, err := parser.ParseExpr(src2)
	if err != nil {
		panic(err)
	}
	done := make(chan struct{})
	defer close(done)

	go func() {
		ast.Walk(v1, tree1)
		close(v1.queue)
		done <- struct{}{}
	}()
	go func() {
		ast.Walk(v2, tree2)
		close(v2.queue)
		done <- struct{}{}
	}()

	var n1, n2 *ast.Node
	quit := false
	for !quit {
		select {
		case n1 = <-v1.queue:
		case n2 = <-v2.queue:
		case <-done:
			quit = true
		}
		if n1 != nil && n2 != nil {
			if !equalNodes(n1, n2) {
				println("!equalNodes")
				break
			}
			println("equalNodes")
			n1 = nil
			n2 = nil
		}
	}
}
开发者ID:rtfb,项目名称:sketchbook,代码行数:53,代码来源:parz.go

示例11: ParseDocs

//ParseDocs parses the Package's documentation with go/doc.
//
//If you do not need a particular doc.Mode call this with 0.
//
//If the package directory contains a file of package documentation
//(and the package is not itself named documentation), it is parsed
//and its doc.Package.Doc string replaces the string generated
//by the package itself.
//
//Note that the go/doc package munges the AST so this method parses the AST
//again, regardless of the value in p.AST. As a consequence, it is valid
//to call this even if you have not called the Parse method or if you have
//called the Parse method and told it not to parse comments.
func (p *Package) ParseDocs(mode doc.Mode) error {
	if p.Doc != nil {
		return nil
	}
	pkg, _, err := p.parse(true)
	if err != nil {
		return err
	}

	p.Doc = doc.New(pkg, p.Build.ImportPath, mode)

	//we don't want the below running if we happen to be importing a package
	//whose name happens to be documentation.
	if p.Build.Name == "documentation" {
		return nil
	}

	//check ignored files for any package named documentation.
	//assume there is only one such file.
	//We ignore errors here as the ignored files may not be meant to parse.
	var docfile string
	for _, u := range p.Build.IgnoredGoFiles {
		path := filepath.Join(p.Build.Dir)
		fs := token.NewFileSet()
		f, err := parser.ParseFile(fs, path, nil, parser.PackageClauseOnly)
		if err != nil {
			continue
		}
		if f.Name.Name == "documentation" {
			docfile = u
			break
		}
	}

	//there's an ignored file of package documentation,
	//parse it and replace the package doc string with this doc string.
	if docfile != "" {
		fs := token.NewFileSet()
		f := func(fi os.FileInfo) bool {
			return !fi.IsDir() && fi.Name() == docfile
		}
		pkgs, err := parser.ParseDir(fs, p.Build.Dir, f, parser.ParseComments)
		if err != nil {
			return err
		}
		d := doc.New(pkgs["documentation"], p.Build.ImportPath, 0)
		p.Doc.Doc = d.Doc
	}

	return nil
}
开发者ID:jimmyfrasche,项目名称:goutil,代码行数:64,代码来源:package.go

示例12: main

func main() {
	fileSet := token.NewFileSet()
	comFileSet := token.NewFileSet() // The FileSet used to parse the COM declarations.
	mod := newModule(comFileSet)

	// Iterate over all the files specified on the command line.
	for _, filename := range os.Args[1:] {
		f, err := parser.ParseFile(fileSet, filename, nil, parser.ParseComments)
		if err != nil {
			log.Fatalln("error parsing the source file", filename, ":", err)
		}

		// Iterate over all the comments in the current file, picking out the ones that
		// start with "com".
		// Join them into a long string to be parsed, starting with a package declaration.
		chunks := []string{"package " + f.Name.Name}
		for _, cg := range f.Comments {
			for _, c := range cg.List {
				text := strings.TrimSpace(strings.Trim(c.Text, "/*"))
				if strings.HasPrefix(text, "com") {
					text = text[len("com"):]
					if text == "" || !strings.Contains(" \t\n", text[:1]) {
						continue
					}
					text = strings.TrimSpace(text)
					chunks = append(chunks, text)
				}
			}
		}
		comDecls := strings.Join(chunks, "\n")

		// Now parse the concatenated result as a Go source file.
		comAST, err := parser.ParseFile(comFileSet, filename, comDecls, parser.ParseComments)
		if err != nil {
			log.Fatalln("error parsing the COM declarations from", filename, ":", err)
		}

		err = mod.loadFile(comAST)
		if err != nil {
			log.Fatalln("error loading declarations from", filename, "into module:", err)
		}
	}

	err := mod.write(os.Stdout)
	if err != nil {
		log.Fatalln("error generating output:", err)
	}
}
开发者ID:vijaygiri10,项目名称:com-and-go,代码行数:48,代码来源:main.go

示例13: rewriteGoFile

// rewriteGoFile rewrites import statments in the named file
// according to the rules for func qualify.
func rewriteGoFile(name, qual string, paths []string) error {
	debugln("rewriteGoFile", name, ",", qual, ",", paths)
	printerConfig := &printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, name, nil, parser.ParseComments)
	if err != nil {
		return err
	}

	var changed bool
	for _, s := range f.Imports {
		name, err := strconv.Unquote(s.Path.Value)
		if err != nil {
			return err // can't happen
		}
		q := qualify(unqualify(name), qual, paths)
		if q != name {
			s.Path.Value = strconv.Quote(q)
			changed = true
		}
	}
	if !changed {
		return nil
	}
	var buffer bytes.Buffer
	if err = printerConfig.Fprint(&buffer, fset, f); err != nil {
		return err
	}
	fset = token.NewFileSet()
	f, err = parser.ParseFile(fset, name, &buffer, parser.ParseComments)
	ast.SortImports(fset, f)
	tpath := name + ".temp"
	t, err := os.Create(tpath)
	if err != nil {
		return err
	}
	if err = printerConfig.Fprint(t, fset, f); err != nil {
		return err
	}
	if err = t.Close(); err != nil {
		return err
	}
	// This is required before the rename on windows.
	if err = os.Remove(name); err != nil {
		return err
	}
	return os.Rename(tpath, name)
}
开发者ID:mitake,项目名称:godep,代码行数:50,代码来源:rewrite.go

示例14: TestMultiFileInitOrder

func TestMultiFileInitOrder(t *testing.T) {
	fset := token.NewFileSet()
	mustParse := func(src string) *ast.File {
		f, err := parser.ParseFile(fset, "main", src, 0)
		if err != nil {
			t.Fatal(err)
		}
		return f
	}

	fileA := mustParse(`package main; var a = 1`)
	fileB := mustParse(`package main; var b = 2`)

	// The initialization order must not depend on the parse
	// order of the files, only on the presentation order to
	// the type-checker.
	for _, test := range []struct {
		files []*ast.File
		want  string
	}{
		{[]*ast.File{fileA, fileB}, "[a = 1 b = 2]"},
		{[]*ast.File{fileB, fileA}, "[b = 2 a = 1]"},
	} {
		var info Info
		if _, err := new(Config).Check("main", fset, test.files, &info); err != nil {
			t.Fatal(err)
		}
		if got := fmt.Sprint(info.InitOrder); got != test.want {
			t.Fatalf("got %s; want %s", got, test.want)
		}
	}
}
开发者ID:glycerine,项目名称:llgo,代码行数:32,代码来源:api_test.go

示例15: parserPkg

func parserPkg(pkgRealpath, pkgpath string) error {
	if !compareFile(pkgRealpath) {
		Info(pkgRealpath + " don't has updated")
		return nil
	}
	fileSet := token.NewFileSet()
	astPkgs, err := parser.ParseDir(fileSet, pkgRealpath, func(info os.FileInfo) bool {
		name := info.Name()
		return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
	}, parser.ParseComments)

	if err != nil {
		return err
	}
	for _, pkg := range astPkgs {
		for _, fl := range pkg.Files {
			for _, d := range fl.Decls {
				switch specDecl := d.(type) {
				case *ast.FuncDecl:
					parserComments(specDecl.Doc, specDecl.Name.String(), fmt.Sprint(specDecl.Recv.List[0].Type.(*ast.StarExpr).X), pkgpath)
				}
			}
		}
	}
	genRouterCode()
	savetoFile(pkgRealpath)
	return nil
}
开发者ID:4eek,项目名称:beego,代码行数:28,代码来源:parser.go


注:本文中的go/token.NewFileSet函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。