func HandleScannerError(errList scanner.ErrorList) { fmt.Fprintf(os.Stderr, "%d errors\n", errList.Len()) maxErrs := 10 errCount := 0 for _, scannerErr := range errList { if errCount == maxErrs { fmt.Fprint(os.Stderr, "too many errors") break } errCount++ fmt.Fprintf(os.Stderr, "Error: %v\n", scannerErr) } os.Exit(1) }
func processFile(filename string) ([]*prototype, error) { info, err := os.Stat(filename) if err != nil { return nil, err } src, err := ioutil.ReadFile(filename) if err != nil { return nil, err } // Scan file protos := make([]*prototype, 0) var errorList scanner.ErrorList var s scanner.Scanner fset := token.NewFileSet() file := fset.AddFile(filename, fset.Base(), int(info.Size())) s.Init(file, src, nil, scanner.ScanComments) for findPkg, isPkgToken := true, false; ; { _, tok, lit := s.Scan() if tok == token.EOF { break } // package name if findPkg { if tok == token.PACKAGE { isPkgToken = true continue } if isPkgToken { if PKG_NAME == "" { PKG_NAME = lit if PKG_NAME == "syscall" { SYSCALL_DOT = "" } } else if PKG_NAME != lit { return nil, errors.New("the files have different package name") } findPkg = false continue } } // scan comments if tok != token.COMMENT || !strings.HasPrefix(lit, PREFIX) { continue } lit = strings.Replace(lit, PREFIX, "", 1) if lit[0] != ' ' && lit[0] != '\t' { continue } lit = strings.TrimSpace(lit) proto, err := scanProtype([]byte(lit)) if err != nil { errorList.Add(err.Pos, err.Msg) continue } if errorList.Len() == 0 { protos = append(protos, proto) } } if errorList.Len() != 0 { return nil, errorList } return protos, nil }
func processFile(filename string, proto *prototype) error { info, err := os.Stat(filename) if err != nil { return err } src, err := ioutil.ReadFile(filename) if err != nil { return err } // Scan file var errorList scanner.ErrorList var s scanner.Scanner fset := token.NewFileSet() file := fset.AddFile(filename, fset.Base(), int(info.Size())) s.Init(file, src, nil, scanner.ScanComments) for findPkg, isPkgToken := true, false; ; { _, tok, lit := s.Scan() if tok == token.EOF { break } // package name if findPkg { if tok == token.PACKAGE { isPkgToken = true continue } if isPkgToken { if proto.pkgName == "" { (*proto).pkgName = lit } else if proto.pkgName != lit { return errors.New("the files have different package name") } findPkg = false continue } } // scan comments if tok != token.COMMENT || !strings.HasPrefix(lit, PREFIX) { continue } lit = strings.Replace(lit, PREFIX, "", 1) if lit[0] == '/' && lit[1] == '/' { proto.comment = lit continue } if lit[0] != ' ' && lit[0] != '\t' { continue } lit_ := strings.TrimSpace(lit) // multiple comments for { if lit[len(lit)-1] == ',' { // the line continues on the next one _, tok, lit = s.Scan() if tok == token.COMMENT { lit = strings.Replace(lit, "//", "", 1) lit = strings.TrimSpace(lit) lit_ += lit } else { break } } else { break } } err := scanProtype(proto, []byte(lit_)) if err != nil { errorList.Add(err.Pos, err.Msg) continue } } if errorList.Len() != 0 { return errorList } return nil }