github.com/reviewdog/reviewdog@v0.17.5-0.20240516205324-0cd103a83d58/parser/parser.go (about) 1 package parser 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 8 "github.com/reviewdog/errorformat/fmts" 9 10 "github.com/reviewdog/reviewdog/proto/rdf" 11 ) 12 13 // Parser is an interface which parses compilers, linters, or any tools 14 // results. 15 type Parser interface { 16 Parse(r io.Reader) ([]*rdf.Diagnostic, error) 17 } 18 19 // Option represents option to create Parser. Either FormatName or 20 // Errorformat should be specified. 21 type Option struct { 22 FormatName string 23 Errorformat []string 24 DiffStrip int 25 } 26 27 // New returns Parser based on Option. 28 func New(opt *Option) (Parser, error) { 29 name := opt.FormatName 30 31 if name != "" && len(opt.Errorformat) > 0 { 32 return nil, errors.New("you cannot specify both format name and errorformat at the same time") 33 } 34 35 switch name { 36 case "checkstyle": 37 return NewCheckStyleParser(), nil 38 case "rdjsonl": 39 return NewRDJSONLParser(), nil 40 case "rdjson": 41 return NewRDJSONParser(), nil 42 case "diff": 43 return NewDiffParser(opt.DiffStrip), nil 44 case "sarif": 45 return NewSarifParser(), nil 46 } 47 48 // use defined errorformat 49 if name != "" { 50 efm, ok := fmts.DefinedFmts()[name] 51 if !ok { 52 return nil, fmt.Errorf("%q is not supported. consider to add new errorformat to https://github.com/reviewdog/errorformat", name) 53 } 54 opt.Errorformat = efm.Errorformat 55 } 56 if len(opt.Errorformat) == 0 { 57 return nil, errors.New("errorformat is empty") 58 } 59 return NewErrorformatParserString(opt.Errorformat) 60 } 61 62 func severity(s string) rdf.Severity { 63 switch s { 64 case "error", "ERROR", "Error", "e", "E": 65 return rdf.Severity_ERROR 66 case "warning", "WARNING", "Warning", "w", "W": 67 return rdf.Severity_WARNING 68 case "info", "INFO", "Info", "i", "I", 69 "note", "NOTE", "Note", "n", "N": // Treat note as info. 70 return rdf.Severity_INFO 71 default: 72 return rdf.Severity_UNKNOWN_SEVERITY 73 } 74 }