pkger/parser/file.go

43 lines
651 B
Go
Raw Permalink Normal View History

2019-08-01 19:03:12 +03:00
package parser
import (
"go/ast"
"go/parser"
"go/token"
"io"
"io/ioutil"
)
2019-08-01 19:06:50 +03:00
type parsedFile struct {
2019-08-01 19:03:12 +03:00
File string
FileSet *token.FileSet
Ast *ast.File
}
2019-08-01 19:06:50 +03:00
// parseFileMode ...
func parseFileMode(f string, mode parser.Mode) (parsedFile, error) {
pf := parsedFile{
2019-08-01 19:03:12 +03:00
File: f,
FileSet: token.NewFileSet(),
}
b, err := ioutil.ReadFile(f)
if err != nil {
return pf, err
}
src := string(b)
pff, err := parser.ParseFile(pf.FileSet, f, src, mode)
2019-10-16 17:20:02 +03:00
if err != nil && err != io.EOF {
2019-08-01 19:03:12 +03:00
return pf, err
}
pf.Ast = pff
return pf, nil
}
2019-08-01 19:06:50 +03:00
// parseFile ...
func parseFile(f string) (parsedFile, error) {
return parseFileMode(f, 0)
2019-08-01 19:03:12 +03:00
}