Paul Lecuq
36c5d6f2ed
- web service with json support - web page - cron service to update database infos
44 lines
767 B
Go
44 lines
767 B
Go
package parser
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/gobuffalo/packd"
|
|
"github.com/markbates/errx"
|
|
)
|
|
|
|
// ParsedFile ...
|
|
type ParsedFile struct {
|
|
File packd.SimpleFile
|
|
FileSet *token.FileSet
|
|
Ast *ast.File
|
|
Lines []string
|
|
}
|
|
|
|
// ParseFileMode ...
|
|
func ParseFileMode(gf packd.SimpleFile, mode parser.Mode) (ParsedFile, error) {
|
|
pf := ParsedFile{
|
|
FileSet: token.NewFileSet(),
|
|
File: gf,
|
|
}
|
|
|
|
src := gf.String()
|
|
f, err := parser.ParseFile(pf.FileSet, gf.Name(), src, mode)
|
|
if err != nil && errx.Unwrap(err) != io.EOF {
|
|
return pf, err
|
|
}
|
|
pf.Ast = f
|
|
|
|
pf.Lines = strings.Split(src, "\n")
|
|
return pf, nil
|
|
}
|
|
|
|
// ParseFile ...
|
|
func ParseFile(gf packd.SimpleFile) (ParsedFile, error) {
|
|
return ParseFileMode(gf, 0)
|
|
}
|