本文整理汇总了Golang中go/token.FileSet.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang FileSet.Write方法的具体用法?Golang FileSet.Write怎么用?Golang FileSet.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/token.FileSet
的用法示例。
在下文中一共展示了FileSet.Write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TranslatePackage
//.........这里部分代码省略.........
for _, name := range spec.(*ast.ValueSpec).Names {
if !isBlank(name) {
o := c.p.info.Defs[name].(*types.Var)
vars = append(vars, o)
c.objectName(o) // register toplevel name
}
}
}
case token.CONST:
// skip, constants are inlined
}
}
}
}
collectDependencies := func(self types.Object, f func()) []DepId {
c.p.dependencies = make(map[types.Object]bool)
f()
var deps []string
for dep := range c.p.dependencies {
if dep != self {
deps = append(deps, dep.Pkg().Path()+":"+dep.Name())
}
}
sort.Strings(deps)
depIds := make([]DepId, len(deps))
for i, dep := range deps {
depIds[i] = DepId(dep)
}
return depIds
}
gcData := bytes.NewBuffer(nil)
gcexporter.Write(typesPkg, gcData, sizes32)
encodedFileSet := bytes.NewBuffer(nil)
if err := fileSet.Write(json.NewEncoder(encodedFileSet).Encode); err != nil {
return nil, err
}
archive := &Archive{
ImportPath: importPath,
GcData: gcData.Bytes(),
Dependencies: []string{"runtime"}, // all packages depend on runtime
FileSet: encodedFileSet.Bytes(),
}
// imports
for _, importedPkg := range typesPkg.Imports() {
varName := c.newVariable(importedPkg.Name())
c.p.pkgVars[importedPkg.Path()] = varName
archive.Imports = append(archive.Imports, Import{Path: importedPkg.Path(), VarName: varName})
}
// types
for _, o := range toplevelTypes {
typeName := c.objectName(o)
var d Decl
d.Var = typeName
d.DceFilters = []DepId{DepId(o.Name())}
d.DceDeps = collectDependencies(o, func() {
d.BodyCode = c.CatchOutput(0, func() { c.translateType(o, true) })
d.InitCode = c.CatchOutput(1, func() { c.initType(o) })
})
archive.Declarations = append(archive.Declarations, d)
}
// functions
示例2: Compile
func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, importContext *ImportContext, minify bool) (*Archive, error) {
typesInfo := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
var importError error
var errList ErrorList
var previousErr error
config := &types.Config{
Importer: packageImporter{
importContext: importContext,
importError: &importError,
},
Sizes: sizes32,
Error: func(err error) {
if previousErr != nil && previousErr.Error() == err.Error() {
return
}
errList = append(errList, err)
previousErr = err
},
}
typesPkg, err := config.Check(importPath, fileSet, files, typesInfo)
if importError != nil {
return nil, importError
}
if errList != nil {
if len(errList) > 10 {
pos := token.NoPos
if last, ok := errList[9].(types.Error); ok {
pos = last.Pos
}
errList = append(errList[:10], types.Error{Fset: fileSet, Pos: pos, Msg: "too many errors"})
}
return nil, errList
}
if err != nil {
return nil, err
}
importContext.Packages[importPath] = typesPkg
exportData := importer.ExportData(typesPkg)
encodedFileSet := bytes.NewBuffer(nil)
if err := fileSet.Write(json.NewEncoder(encodedFileSet).Encode); err != nil {
return nil, err
}
isBlocking := func(f *types.Func) bool {
archive, err := importContext.Import(f.Pkg().Path())
if err != nil {
panic(err)
}
fullName := f.FullName()
for _, d := range archive.Declarations {
if string(d.FullName) == fullName {
return d.Blocking
}
}
panic(fullName)
}
pkgInfo := analysis.AnalyzePkg(files, fileSet, typesInfo, typesPkg, isBlocking)
c := &funcContext{
FuncInfo: pkgInfo.InitFuncInfo,
p: &pkgContext{
Info: pkgInfo,
additionalSelections: make(map[*ast.SelectorExpr]selection),
pkgVars: make(map[string]string),
objectNames: make(map[types.Object]string),
varPtrNames: make(map[*types.Var]string),
escapingVars: make(map[*types.Var]bool),
indentation: 1,
dependencies: make(map[types.Object]bool),
minify: minify,
fileSet: fileSet,
},
allVars: make(map[string]int),
flowDatas: map[*types.Label]*flowData{nil: {}},
caseCounter: 1,
labelCases: make(map[*types.Label]int),
}
for name := range reservedKeywords {
c.allVars[name] = 1
}
// imports
var importDecls []*Decl
var importedPaths []string
for _, importedPkg := range typesPkg.Imports() {
c.p.pkgVars[importedPkg.Path()] = c.newVariableWithLevel(importedPkg.Name(), true)
importedPaths = append(importedPaths, importedPkg.Path())
}
sort.Strings(importedPaths)
for _, impPath := range importedPaths {
id := c.newIdent(fmt.Sprintf(`%s.$init`, c.p.pkgVars[impPath]), types.NewSignature(nil, nil, nil, false))
//.........这里部分代码省略.........
示例3: Compile
func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, importContext *ImportContext, minify bool) (*Archive, error) {
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
var errList ErrorList
var previousErr error
config := &types.Config{
Packages: importContext.Packages,
Import: func(_ map[string]*types.Package, path string) (*types.Package, error) {
if _, err := importContext.Import(path); err != nil {
return nil, err
}
return importContext.Packages[path], nil
},
Sizes: sizes32,
Error: func(err error) {
if previousErr != nil && previousErr.Error() == err.Error() {
return
}
errList = append(errList, err)
previousErr = err
},
}
typesPkg, err := config.Check(importPath, fileSet, files, info)
if errList != nil {
return nil, errList
}
if err != nil {
return nil, err
}
importContext.Packages[importPath] = typesPkg
gcData := bytes.NewBuffer(nil)
gcexporter.Write(typesPkg, gcData, sizes32)
encodedFileSet := bytes.NewBuffer(nil)
if err := fileSet.Write(json.NewEncoder(encodedFileSet).Encode); err != nil {
return nil, err
}
archive := &Archive{
ImportPath: PkgPath(importPath),
GcData: gcData.Bytes(),
Dependencies: []PkgPath{PkgPath("github.com/gopherjs/gopherjs/js"), PkgPath("runtime")}, // all packages depend on those
FileSet: encodedFileSet.Bytes(),
Minified: minify,
}
c := &funcContext{
p: &pkgContext{
pkg: typesPkg,
info: info,
importContext: importContext,
comments: make(ast.CommentMap),
funcContexts: make(map[*types.Func]*funcContext),
pkgVars: make(map[string]string),
objectVars: make(map[types.Object]string),
escapingVars: make(map[types.Object]bool),
indentation: 1,
dependencies: make(map[types.Object]bool),
minify: minify,
},
allVars: make(map[string]int),
flowDatas: map[string]*flowData{"": &flowData{}},
flattened: make(map[ast.Node]bool),
blocking: make(map[ast.Node]bool),
caseCounter: 1,
labelCases: make(map[string]int),
localCalls: make(map[*types.Func][][]ast.Node),
}
for name := range reservedKeywords {
c.allVars[name] = 1
}
// imports
var importedPaths []string
for _, importedPkg := range typesPkg.Imports() {
varName := c.newVariableWithLevel(importedPkg.Name(), true, "")
c.p.pkgVars[importedPkg.Path()] = varName
archive.Imports = append(archive.Imports, PkgImport{Path: PkgPath(importedPkg.Path()), VarName: varName})
importedPaths = append(importedPaths, importedPkg.Path())
}
sort.Strings(importedPaths)
for _, impPath := range importedPaths {
impOutput, err := importContext.Import(impPath)
if err != nil {
return nil, err
}
archive.AddDependenciesOf(impOutput)
}
var functions []*ast.FuncDecl
var toplevelTypes []*types.TypeName
var vars []*types.Var
for _, file := range files {
for k, v := range ast.NewCommentMap(fileSet, file, file.Comments) {
c.p.comments[k] = v
//.........这里部分代码省略.........