pkger/parser/file.go

45 lines
694 B
Go
Raw Normal View History

2019-08-01 19:03:12 +03:00
package parser
import (
"go/ast"
"go/parser"
"go/token"
"io"
"io/ioutil"
"github.com/markbates/errx"
)
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)
if err != nil && errx.Unwrap(err) != io.EOF {
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
}