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


Golang parser.ParseFile函数代码示例

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


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

示例1: format

// format parses src, prints the corresponding AST, verifies the resulting
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte, mode checkMode) ([]byte, error) {
	// parse src
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	// filter exports if necessary
	if mode&export != 0 {
		ast.FileExports(f) // ignore result
		f.Comments = nil   // don't print comments that are not in AST
	}

	// determine printer configuration
	cfg := Config{Tabwidth: tabwidth}
	if mode&rawFormat != 0 {
		cfg.Mode |= RawFormat
	}

	// print AST
	var buf bytes.Buffer
	if err := cfg.Fprint(&buf, fset, f); err != nil {
		return nil, fmt.Errorf("print: %s", err)
	}

	// make sure formatted output is syntactically correct
	res := buf.Bytes()
	if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
		return nil, fmt.Errorf("re-parse: %s\n%s", err, buf.Bytes())
	}

	return res, nil
}
开发者ID:h8liu,项目名称:golang,代码行数:36,代码来源:printer_test.go

示例2: 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

示例3: Parse

// Parse parses the source in filename and returns a list of tags.
func Parse(filename string) ([]Tag, error) {
	p := &tagParser{
		fset:  token.NewFileSet(),
		tags:  []Tag{},
		types: make([]string, 0),
	}

	var f *ast.File
	var err error

	if filename != "-" {
		f, err = parser.ParseFile(p.fset, filename, nil, 0)
		if err != nil {
			return nil, err
		}
	} else {
		bytes, err := ioutil.ReadAll(os.Stdin)

		f, err = parser.ParseFile(p.fset, "", string(bytes), 0)
		if err != nil {
			return nil, err
		}
	}

	// package
	p.parsePackage(f)

	// imports
	p.parseImports(f)

	// declarations
	p.parseDeclarations(f)

	return p.tags, nil
}
开发者ID:ppalucki,项目名称:gotags,代码行数:36,代码来源:parser.go

示例4: NewParser

// NewParser creates a new Parser reference from the given options
func NewParser(opts *ParserOptions) (*Parser, error) {
	var mode parser.Mode
	if opts != nil && opts.Comments {
		mode = parser.ParseComments
	}

	fset := token.NewFileSet()
	p := &Parser{fset: fset}
	var err error

	switch {
	case opts.File != "":
		p.file, err = parser.ParseFile(fset, opts.File, nil, mode)
		if err != nil {
			return nil, err
		}
	case opts.Dir != "":
		p.pkgs, err = parser.ParseDir(fset, opts.Dir, nil, mode)
		if err != nil {
			return nil, err
		}
	case opts.Src != nil:
		p.file, err = parser.ParseFile(fset, "src.go", opts.Src, mode)
		if err != nil {
			return nil, err
		}
	default:
		return nil, errors.New("file, src or dir is not specified")
	}

	return p, nil
}
开发者ID:fatih,项目名称:motion,代码行数:33,代码来源:parser.go

示例5: checkGoFile

func checkGoFile(filePath string) bool {
	if strings.HasSuffix(filePath, ".go") {
		var (
			fset       = token.NewFileSet()
			astFile    *ast.File
			err        error
			hasGoglImp = false
		)
		if astFile, err = parser.ParseFile(fset, filePath, nil, parser.ImportsOnly); err != nil {
			panic(err)
		}
		for _, s := range astFile.Imports {
			if hasGoglImp = (strings.Index(s.Path.Value, "github.com/go3d/go-opengl/core/") >= 0); hasGoglImp {
				break
			}
		}
		if hasGoglImp {
			if astFile, err = parser.ParseFile(fset, filePath, nil, 0); err != nil {
				panic(err)
			}
			curFilePath = filePath
			ast.Inspect(astFile, inspectNode)
		}
	}
	return true
}
开发者ID:Raven67854,项目名称:go-ngine,代码行数:26,代码来源:main.go

示例6: parse

func parse(fset *token.FileSet, src []byte) (interface{}, error) {
	// Try as a complete source file.
	file, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err == nil {
		return file, nil
	}
	// If the source is missing a package clause, try as a source fragment; otherwise fail.
	if !strings.Contains(err.Error(), "expected 'package'") {
		return nil, err
	}

	// Try as a declaration list by prepending a package clause in front of src.
	// Use ';' not '\n' to keep line numbers intact.
	psrc := append([]byte("package p;"), src...)
	file, err = parser.ParseFile(fset, "", psrc, parser.ParseComments)
	if err == nil {
		return file.Decls, nil
	}
	// If the source is missing a declaration, try as a statement list; otherwise fail.
	if !strings.Contains(err.Error(), "expected declaration") {
		return nil, err
	}

	// Try as statement list by wrapping a function around src.
	fsrc := append(append([]byte("package p; func _() {"), src...), '}')
	file, err = parser.ParseFile(fset, "", fsrc, parser.ParseComments)
	if err == nil {
		return file.Decls[0].(*ast.FuncDecl).Body.List, nil
	}

	// Failed, and out of options.
	return nil, err
}
开发者ID:ZeusbasePython,项目名称:appscale,代码行数:33,代码来源:format.go

示例7: GetCurrentAppSettings

func GetCurrentAppSettings(settings_path string)(settings map[string]string,err error){

    fset := token.NewFileSet()

    f, err := parser.ParseFile(fset, default_settings_path, nil, 0)
    if err != nil {
        f, err = parser.ParseFile(fset, default_relative_settings_path, nil, 0)
        if err!=nil{
            return nil,err
        }
    }

    conf := types.Config{Importer: importer.Default()}
    pkg, err := conf.Check("wapour/settings", fset, []*ast.File{f}, nil)
    if err != nil {
        return nil,err
    }
    settings=make(map[string]string,0)
    for word_id := range initial {
        word:=initial[word_id]
        existing_set:=pkg.Scope().Lookup(word).(*types.Const).Val().String()
        settings[word]=existing_set
    }
    return settings,err
}
开发者ID:gtfour,项目名称:actuator,代码行数:25,代码来源:run.go

示例8: GetPackageList

func GetPackageList() {
	for _, fname := range files {
		file, err := parser.ParseFile(fname, nil, parser.PackageClauseOnly)
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
		pname := file.Name.Name
		if pname == "main" {
			fullfile, err := parser.ParseFile(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:bytbox,项目名称:gomake,代码行数:27,代码来源:goinfo.go

示例9: Print

func Print(filename string, source interface{}) (pretty string, ok os.Error) {
	fileAst, ok := parser.ParseFile(filename, source, 4)

	// Make common corrections for snippet pastes
	if ok != nil && source != nil {
		src := source.(string)

		if m, _ := regexp.MatchString(`^package`, src); !m {
			src = "package main\n\n" + src
		}

		if fileAst, ok = parser.ParseFile(filename, src, 4); ok != nil {
			return
		}
	}

	coll := new(collector)
	coll.contents = new(vector.StringVector)

	(&printer.Config{
		Mode:     5,
		Tabwidth: 4,
		Styler:   new(HTMLStyler),
	}).Fprint(coll, fileAst)

	pretty = strings.Join(coll.contents.Data(), "")

	return
}
开发者ID:necrogami,项目名称:go-play,代码行数:29,代码来源:pretty.go

示例10: 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

示例11: parse

// parse parses src, which was read from filename,
// as a Go source file or statement list.
func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.File, func(orig, src []byte) []byte, error) {
	// Try as whole source file.
	file, err := parser.ParseFile(fset, filename, src, parserMode)
	if err == nil {
		return file, nil, nil
	}
	// If the error is that the source file didn't begin with a
	// package line and this is standard input, fall through to
	// try as a source fragment.  Stop and return on any other error.
	if !stdin || !strings.Contains(err.Error(), "expected 'package'") {
		return nil, nil, err
	}

	// If this is a declaration list, make it a source file
	// by inserting a package clause.
	// Insert using a ;, not a newline, so that the line numbers
	// in psrc match the ones in src.
	psrc := append([]byte("package p;"), src...)
	file, err = parser.ParseFile(fset, filename, psrc, parserMode)
	if err == nil {
		adjust := func(orig, src []byte) []byte {
			// Remove the package clause.
			// Gofmt has turned the ; into a \n.
			src = src[len("package p\n"):]
			return matchSpace(orig, src)
		}
		return file, adjust, nil
	}
	// If the error is that the source file didn't begin with a
	// declaration, fall through to try as a statement list.
	// Stop and return on any other error.
	if !strings.Contains(err.Error(), "expected declaration") {
		return nil, nil, err
	}

	// If this is a statement list, make it a source file
	// by inserting a package clause and turning the list
	// into a function body.  This handles expressions too.
	// Insert using a ;, not a newline, so that the line numbers
	// in fsrc match the ones in src.
	fsrc := append(append([]byte("package p; func _() {"), src...), '}')
	file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
	if err == nil {
		adjust := func(orig, src []byte) []byte {
			// Remove the wrapping.
			// Gofmt has turned the ; into a \n\n.
			src = src[len("package p\n\nfunc _() {"):]
			src = src[:len(src)-len("}\n")]
			// Gofmt has also indented the function body one level.
			// Remove that indent.
			src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
			return matchSpace(orig, src)
		}
		return file, adjust, nil
	}

	// Failed, and out of options.
	return nil, nil, err
}
开发者ID:h8liu,项目名称:golang,代码行数:61,代码来源:gofmt.go

示例12: getImports

func getImports(dir string) *DepSet {
	var pf *ast.File
	var err error
	fset = token.NewFileSet()
	files := getGoFiles(dir)
	dset := NewDepSet()

	for _, file := range files {
		if showCalls == true {
			pf, err = parser.ParseFile(fset, file, nil, 0)
			// Build a map with the imports mapping to the files
			for _, s := range pf.Imports {
				pname := strings.Trim(s.Path.Value, "\"")
				if debug {
					fmt.Printf("Import found: %s imports %s\n", file, pname)
				}
				dset.AddImport(pname, file)
			}

			if debug {
				for _, pkg := range dset.Packages() {
					fmt.Printf("Imported: %s\n", pkg)
				}
			}

			// ast.Inspect(pf, parseCalls)
			ast.Walk(finder{
				find: findCalls,
				dset: dset,
			}, pf)
		} else {
			pf, err = parser.ParseFile(fset, file, nil, parser.ImportsOnly)
			// Build a map with the imports mapping to the files
			for _, s := range pf.Imports {
				pname := strings.Trim(s.Path.Value, "\"")
				dset.AddImport(pname, file)
			}
		}
		if err != nil {
			// fmt.Println(err)
		}

		/*
		 * We'll need to AND the imports and the keys in the
		 * package calls map to trim out keys from the latter
		 * which are not actually packages.
		 * We also need to figure out how to account for methods -
		 * since the qualifier (X node) in those cases is the
		 * method receiver, not the package.
		 */
	}

	return dset
}
开发者ID:ScarletTanager,项目名称:gimports,代码行数:54,代码来源:util.go

示例13: merge

func (b *builder) merge() ([]byte, error) {
	var buf bytes.Buffer
	if err := b.tpl.Execute(&buf, b); err != nil {
		return nil, err
	}

	f, err := parser.ParseFile(b.fset, "", &buf, 0)
	if err != nil {
		return nil, err
	}
	// b.imports(f)
	b.deleteImports(f)
	b.files["main.go"] = f

	pkg, _ := ast.NewPackage(b.fset, b.files, nil, nil)
	pkg.Name = "main"

	ret, err := ast.MergePackageFiles(pkg, 0), nil
	if err != nil {
		return nil, err
	}

	// @TODO: we reread the file, probably something goes wrong with position
	buf.Reset()
	if err = format.Node(&buf, b.fset, ret); err != nil {
		return nil, err
	}

	ret, err = parser.ParseFile(b.fset, "", buf.Bytes(), 0)
	if err != nil {
		return nil, err
	}

	for _, spec := range b.imports {
		var name string
		if spec.Name != nil {
			name = spec.Name.Name
		}
		ipath, _ := strconv.Unquote(spec.Path.Value)
		addImport(b.fset, ret, name, ipath)
	}

	buf.Reset()
	if err := format.Node(&buf, b.fset, ret); err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
开发者ID:dasnook,项目名称:godog,代码行数:49,代码来源:builder.go

示例14: 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

示例15: 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


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