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