Skip to content

Commit

Permalink
Merge pull request #904 from xushiwei/q
Browse files Browse the repository at this point in the history
TestIparseFile
  • Loading branch information
xushiwei committed Nov 17, 2021
2 parents a355693 + 5f29c21 commit 4a927b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 77 deletions.
73 changes: 1 addition & 72 deletions parser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func parseFile(fset *token.FileSet, filename string, src interface{}, mode Mode)
// set result values
if f == nil {
// source is not a valid Go source file - satisfy
// ParseFile API and return a valid (but) empty
// *ast.File
// ParseFile API and return a valid (but) empty *ast.File
f = &ast.File{
Name: new(ast.Ident),
Scope: ast.NewScope(nil),
Expand All @@ -95,73 +94,3 @@ func parseFile(fset *token.FileSet, filename string, src interface{}, mode Mode)

return
}

/*
// ParseExprFrom is a convenience function for parsing an expression.
// The arguments have the same meaning as for ParseFile, but the source must
// be a valid Go (type or value) expression. Specifically, fset must not
// be nil.
//
// If the source couldn't be read, the returned AST is nil and the error
// indicates the specific failure. If the source was read but syntax
// errors were found, the result is a partial AST (with ast.Bad* nodes
// representing the fragments of erroneous source code). Multiple errors
// are returned via a scanner.ErrorList which is sorted by source position.
//
func parseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (expr ast.Expr, err error) {
if fset == nil {
panic("parser.ParseExprFrom: no token.FileSet provided (fset == nil)")
}
// get source
text, err := readSource(src)
if err != nil {
return nil, err
}
var p parser
defer func() {
if e := recover(); e != nil {
// resume same panic if it's not a bailout
if _, ok := e.(bailout); !ok {
panic(e)
}
}
p.errors.Sort()
err = p.errors.Err()
}()
// parse expr
p.init(fset, filename, text, mode)
// Set up pkg-level scopes to avoid nil-pointer errors.
// This is not needed for a correct expression x as the
// parser will be ok with a nil topScope, but be cautious
// in case of an erroneous x.
p.openScope()
p.pkgScope = p.topScope
expr = p.parseRHSOrType()
p.closeScope()
assert(p.topScope == nil, "unbalanced scopes")
// If a semicolon was inserted, consume it;
// report an error if there's more tokens.
if p.tok == token.SEMICOLON && p.lit == "\n" {
p.next()
}
p.expect(token.EOF)
return
}
// ParseExpr is a convenience function for obtaining the AST of an expression x.
// The position information recorded in the AST is undefined. The filename used
// in error messages is the empty string.
//
// If syntax errors were found, the result is a partial AST (with ast.Bad* nodes
// representing the fragments of erroneous source code). Multiple errors are
// returned via a scanner.ErrorList which is sorted by source position.
//
func parseExpr(x string) (ast.Expr, error) {
return parseExprFrom(token.NewFileSet(), "", x, 0)
}
*/
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,7 @@ func (p *parser) parseFuncDeclOrCall() (*ast.FuncDecl, *ast.CallExpr) {
p.next()
if p.tok == token.LPAREN {
// func (recv) op(params) results { ... }
recv, ident, isOp = params, &ast.Ident{NamePos: oldpos, Name: oldtok.String()}, true
recv, ident = params, &ast.Ident{NamePos: oldpos, Name: oldtok.String()}
params, results = p.parseSignature(scope)
} else {
// func (params) typ { ... }()
Expand Down
4 changes: 0 additions & 4 deletions parser/parser_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ func ParseFSFile(fset *token.FileSet, fs FileSystem, filename string, src interf
if !isOk {
ft = ast.FileTypeGop
}
return parseFSFileEx(fset, fs, filename, src, mode, ft)
}

func parseFSFileEx(fset *token.FileSet, fs FileSystem, filename string, src interface{}, mode Mode, ft ast.FileType) (f *ast.File, err error) {
var code []byte
if src == nil {
code, err = fs.ReadFile(filename)
Expand Down
23 changes: 23 additions & 0 deletions parser/parserdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ func TestParseFile(t *testing.T) {
}
}

func TestIparseFileInvalidSrc(t *testing.T) {
fset := token.NewFileSet()
if _, err := parseFile(fset, "/foo/bar/not-exists", 1, PackageClauseOnly); err != errInvalidSource {
t.Fatal("ParseFile failed: not errInvalidSource?")
}
}

func TestIparseFileNoFset(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Fatal("ParseFile failed: no error?")
}
}()
parseFile(nil, "/foo/bar/not-exists", nil, PackageClauseOnly)
}

func TestParseDir(t *testing.T) {
fset := token.NewFileSet()
if _, err := ParseDir(fset, "/foo/bar/not-exists", nil, PackageClauseOnly); err == nil {
t.Fatal("ParseDir failed: no error?")
}
}

func testFrom(t *testing.T, pkgDir, sel string, exclude Mode) {
if sel != "" && !strings.Contains(pkgDir, sel) {
return
Expand Down

0 comments on commit 4a927b7

Please sign in to comment.