-
Notifications
You must be signed in to change notification settings - Fork 43
/
ir.go
110 lines (83 loc) · 1.81 KB
/
ir.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package ir
import (
"fmt"
"strings"
)
type File struct {
PkgPath string
RuleGroups []RuleGroup
CustomDecls []string
BundleImports []BundleImport
}
type BundleImport struct {
Line int
PkgPath string
Prefix string
}
type RuleGroup struct {
Line int
Name string
MatcherName string
DocTags []string
DocSummary string
DocBefore string
DocAfter string
DocNote string
Imports []PackageImport
Rules []Rule
}
type PackageImport struct {
Path string
Name string
}
type Rule struct {
Line int
SyntaxPatterns []PatternString
CommentPatterns []PatternString
ReportTemplate string
SuggestTemplate string
WhereExpr FilterExpr
LocationVar string
}
type PatternString struct {
Line int
Value string
}
// stringer -type=FilterOp -trimprefix=Filter
//go:generate go run ./gen_filter_op.go
type FilterOp int
func (op FilterOp) String() string { return filterOpNames[op] }
type FilterExpr struct {
Line int
Op FilterOp
Src string
Value interface{}
Args []FilterExpr
}
func (e FilterExpr) IsValid() bool { return e.Op != FilterInvalidOp }
func (e FilterExpr) IsBinaryExpr() bool { return filterOpFlags[e.Op]&flagIsBinaryExpr != 0 }
func (e FilterExpr) IsBasicLit() bool { return filterOpFlags[e.Op]&flagIsBasicLit != 0 }
func (e FilterExpr) String() string {
switch e.Op {
case FilterStringOp:
return `"` + e.Value.(string) + `"`
case FilterIntOp:
return fmt.Sprint(e.Value.(int64))
}
parts := make([]string, 0, len(e.Args)+2)
parts = append(parts, e.Op.String())
if e.Value != nil {
parts = append(parts, fmt.Sprintf("[%#v]", e.Value))
}
for _, arg := range e.Args {
parts = append(parts, arg.String())
}
if len(parts) == 1 {
return parts[0]
}
return "(" + strings.Join(parts, " ") + ")"
}
const (
flagIsBinaryExpr uint64 = 1 << iota
flagIsBasicLit
)