mirror of https://github.com/markbates/pkger.git
45 lines
694 B
Go
45 lines
694 B
Go
package parser
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/markbates/errx"
|
|
)
|
|
|
|
type parsedFile struct {
|
|
File string
|
|
FileSet *token.FileSet
|
|
Ast *ast.File
|
|
}
|
|
|
|
// parseFileMode ...
|
|
func parseFileMode(f string, mode parser.Mode) (parsedFile, error) {
|
|
pf := parsedFile{
|
|
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
|
|
}
|
|
|
|
// parseFile ...
|
|
func parseFile(f string) (parsedFile, error) {
|
|
return parseFileMode(f, 0)
|
|
}
|