Skip to content

Commit

Permalink
ParseGoAsGoPlus flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 16, 2022
1 parent 2186534 commit 71ef87e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions parser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ const (
DeclarationErrors
// AllErrors - report all errors (not just the first 10 on different lines)
AllErrors
// ParseGoFilesByGo - parse Go files by go/parser, not gop/parser
// Result will save to gop/ast.Package.GoFiles
ParseGoFilesByGo
// ParseGoAsGoPlus - parse Go files by gop/parser
ParseGoAsGoPlus
)

// ParseFile parses the source code of a single Go source file and returns
Expand Down
4 changes: 2 additions & 2 deletions parser/parser_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, path string, conf Config) (p
if strings.HasPrefix(fname, "gop_autogen") {
continue
}
useGoParser = (conf.Mode & ParseGoFilesByGo) != 0
useGoParser = (conf.Mode & ParseGoAsGoPlus) == 0
default:
if isProj, isClass = conf.IsClass(ext); !isClass {
continue
Expand All @@ -161,7 +161,7 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, path string, conf Config) (p
filename := fs.Join(path, fname)
if useGoParser {
if filedata, err := fs.ReadFile(filename); err == nil {
if src, err := goparser.ParseFile(fset, filename, filedata, goparser.Mode(conf.Mode & ^ParseGoFilesByGo)); err == nil {
if src, err := goparser.ParseFile(fset, filename, filedata, goparser.Mode(conf.Mode)); err == nil {
pkg := reqPkg(pkgs, src.Name.Name)
if pkg.GoFiles == nil {
pkg.GoFiles = make(map[string]*goast.File)
Expand Down
14 changes: 7 additions & 7 deletions parser/parserdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testFrom(t *testing.T, pkgDir, sel string, exclude Mode) {
}
log.Println("Parsing", pkgDir)
fset := token.NewFileSet()
pkgs, err := ParseDir(fset, pkgDir, nil, (Trace|ParseComments)&^exclude)
pkgs, err := ParseDir(fset, pkgDir, nil, (Trace|ParseComments|ParseGoAsGoPlus)&^exclude)
if err != nil || len(pkgs) != 1 {
if errs, ok := err.(scanner.ErrorList); ok {
for _, e := range errs {
Expand All @@ -111,7 +111,7 @@ func testFrom(t *testing.T, pkgDir, sel string, exclude Mode) {

func TestParseGo(t *testing.T) {
fset := token.NewFileSet()
pkgs, err := ParseDirEx(fset, "./_testdata/functype", Config{Mode: Trace})
pkgs, err := ParseDirEx(fset, "./_testdata/functype", Config{Mode: Trace | ParseGoAsGoPlus})
if err != nil {
t.Fatal("TestParseGo: ParseDir failed -", err)
}
Expand All @@ -122,7 +122,7 @@ func TestParseGo(t *testing.T) {

func TestParseGoFiles(t *testing.T) {
fset := token.NewFileSet()
pkgs, err := ParseFiles(fset, []string{"./_testdata/functype/functype.go"}, Trace)
pkgs, err := ParseFiles(fset, []string{"./_testdata/functype/functype.go"}, Trace|ParseGoAsGoPlus)
if err != nil {
t.Fatal("TestParseGoFiles: ParseFiles failed -", err)
}
Expand All @@ -146,7 +146,7 @@ func TestGopAutoGen(t *testing.T) {
func TestGoFile(t *testing.T) {
fset := token.NewFileSet()
fs := parsertest.NewSingleFileFS("/foo", "test.go", `package foo`)
pkgs, err := ParseFSDir(fset, fs, "/foo", Config{Mode: ParseGoFilesByGo})
pkgs, err := ParseFSDir(fset, fs, "/foo", Config{})
if err != nil {
t.Fatal("ParseFSDir:", err)
}
Expand All @@ -158,19 +158,19 @@ func TestGoFile(t *testing.T) {
func TestErrParse(t *testing.T) {
fset := token.NewFileSet()
fs := parsertest.NewSingleFileFS("/foo", "test.go", `package foo bar`)
_, err := ParseFSDir(fset, fs, "/foo", Config{Mode: ParseGoFilesByGo})
_, err := ParseFSDir(fset, fs, "/foo", Config{})
if err == nil {
t.Fatal("ParseFSDir test.go: no error?")
}

fs = parsertest.NewSingleFileFS("/foo", "test.gop", `package foo bar`)
_, err = ParseFSDir(fset, fs, "/foo", Config{Mode: ParseGoFilesByGo})
_, err = ParseFSDir(fset, fs, "/foo", Config{})
if err == nil {
t.Fatal("ParseFSDir test.gop: no error?")
}

fs = parsertest.NewMemFS(map[string][]string{"/foo": {"test.go"}}, map[string]string{})
_, err = ParseFSDir(fset, fs, "/foo", Config{Mode: ParseGoFilesByGo})
_, err = ParseFSDir(fset, fs, "/foo", Config{})
if err != syscall.ENOENT {
t.Fatal("ParseFSDir:", err)
}
Expand Down

0 comments on commit 71ef87e

Please sign in to comment.