Commit 5b1885c2 authored by Robert Griesemer's avatar Robert Griesemer

go/parser: cleanups following CL 7307085

- use the new AllErrors flag where appropriate
- unless AllErrors is set, eliminate spurious
  errors before they are added to the errors list
  (it turns out that reporting spurious errors always
  leads to too many uninformative errors after all)

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7323065
parent 8e2b0e1c
...@@ -29,7 +29,7 @@ var ( ...@@ -29,7 +29,7 @@ var (
rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')") rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")
simplifyAST = flag.Bool("s", false, "simplify code") simplifyAST = flag.Bool("s", false, "simplify code")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files") doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
allErrors = flag.Bool("e", false, "print all (including spurious) errors") allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
// layout control // layout control
comments = flag.Bool("comments", true, "print comments") comments = flag.Bool("comments", true, "print comments")
...@@ -65,7 +65,7 @@ func initParserMode() { ...@@ -65,7 +65,7 @@ func initParserMode() {
parserMode |= parser.ParseComments parserMode |= parser.ParseComments
} }
if *allErrors { if *allErrors {
parserMode |= parser.SpuriousErrors parserMode |= parser.AllErrors
} }
} }
......
...@@ -23,7 +23,7 @@ var ( ...@@ -23,7 +23,7 @@ var (
pkgName = flag.String("p", "", "process only those files in package pkgName") pkgName = flag.String("p", "", "process only those files in package pkgName")
recursive = flag.Bool("r", false, "recursively process subdirectories") recursive = flag.Bool("r", false, "recursively process subdirectories")
verbose = flag.Bool("v", false, "verbose mode") verbose = flag.Bool("v", false, "verbose mode")
allErrors = flag.Bool("e", false, "print all (including spurious) errors") allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
// debugging support // debugging support
parseComments = flag.Bool("comments", false, "parse comments (ignored if -ast not set)") parseComments = flag.Bool("comments", false, "parse comments (ignored if -ast not set)")
...@@ -71,7 +71,7 @@ func parse(fset *token.FileSet, filename string, src []byte) *ast.File { ...@@ -71,7 +71,7 @@ func parse(fset *token.FileSet, filename string, src []byte) *ast.File {
// parse entire file // parse entire file
mode := parser.DeclarationErrors mode := parser.DeclarationErrors
if *allErrors { if *allErrors {
mode |= parser.SpuriousErrors mode |= parser.AllErrors
} }
if *parseComments && *printAST { if *parseComments && *printAST {
mode |= parser.ParseComments mode |= parser.ParseComments
......
...@@ -139,7 +139,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) { ...@@ -139,7 +139,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) {
return return
} }
_, err = ParseFile(fsetErrs, filename, src, DeclarationErrors) _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors|AllErrors)
found, ok := err.(scanner.ErrorList) found, ok := err.(scanner.ErrorList)
if err != nil && !ok { if err != nil && !ok {
t.Error(err) t.Error(err)
......
...@@ -52,13 +52,13 @@ func readSource(filename string, src interface{}) ([]byte, error) { ...@@ -52,13 +52,13 @@ func readSource(filename string, src interface{}) ([]byte, error) {
type Mode uint type Mode uint
const ( const (
PackageClauseOnly Mode = 1 << iota // parsing stops after package clause PackageClauseOnly Mode = 1 << iota // stop parsing after package clause
ImportsOnly // parsing stops after import declarations ImportsOnly // stop parsing after import declarations
ParseComments // parse comments and add them to AST ParseComments // parse comments and add them to AST
Trace // print a trace of parsed productions Trace // print a trace of parsed productions
DeclarationErrors // report declaration errors DeclarationErrors // report declaration errors
SpuriousErrors // same as AllErrors, for backward-compatibility SpuriousErrors // same as AllErrors, for backward-compatibility
AllErrors = SpuriousErrors // report all (not just the first 10) errors per file AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines)
) )
// ParseFile parses the source code of a single Go source file and returns // ParseFile parses the source code of a single Go source file and returns
......
...@@ -344,10 +344,22 @@ func (p *parser) next() { ...@@ -344,10 +344,22 @@ func (p *parser) next() {
type bailout struct{} type bailout struct{}
func (p *parser) error(pos token.Pos, msg string) { func (p *parser) error(pos token.Pos, msg string) {
if p.mode&SpuriousErrors == 0 && p.errors.Len() >= 10 { epos := p.file.Position(pos)
panic(bailout{})
// If AllErrors is not set, discard errors reported on the same line
// as the last recorded error and stop parsing if there are more than
// 10 errors.
if p.mode&AllErrors == 0 {
n := len(p.errors)
if n > 0 && p.errors[n-1].Pos.Line == epos.Line {
return // discard - likely a spurious error
}
if n > 10 {
panic(bailout{})
}
} }
p.errors.Add(p.file.Position(pos), msg)
p.errors.Add(epos, msg)
} }
func (p *parser) errorExpected(pos token.Pos, msg string) { func (p *parser) errorExpected(pos token.Pos, msg string) {
......
...@@ -92,7 +92,7 @@ func parseFiles(t *testing.T, testname string, filenames []string) ([]*ast.File, ...@@ -92,7 +92,7 @@ func parseFiles(t *testing.T, testname string, filenames []string) ([]*ast.File,
var files []*ast.File var files []*ast.File
var errlist []error var errlist []error
for _, filename := range filenames { for _, filename := range filenames {
file, err := parser.ParseFile(fset, filename, nil, parser.DeclarationErrors) file, err := parser.ParseFile(fset, filename, nil, parser.DeclarationErrors|parser.AllErrors)
if file == nil { if file == nil {
t.Fatalf("%s: could not parse file %s", testname, filename) t.Fatalf("%s: could not parse file %s", testname, filename)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment