/
rules.go
179 lines (150 loc) · 3.63 KB
/
rules.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package translator
import (
"github.com/dlclark/regexp2"
)
type Rules map[RuleTarget][]RuleSpec
type ConstRules map[ConstScope]ConstRule
type PtrTips map[TipScope][]TipSpec
type TypeTips map[TipScope][]TipSpec
type MemTips []TipSpec
type Validations []ValidationSpec
type ValidationSpec struct {
ValidateFunc string
Ret string
MatchedFunc string
}
func (v ValidationSpec) MatchFunc(name string) bool {
reg := regexp2.MustCompile(v.MatchedFunc, 0)
matched, _ := reg.MatchString(name)
return matched
}
type RuleSpec struct {
From, To string
Action RuleAction
Transform RuleTransform
Load string
}
func (r *RuleSpec) LoadSpec(r2 RuleSpec) {
if len(r.From) == 0 {
r.From = r2.From
}
if len(r.To) == 0 {
r.To = r2.To
}
if len(r.Action) == 0 {
r.Action = r2.Action
}
if len(r.Transform) == 0 {
r.Transform = r2.Transform
}
}
type RuleAction string
const (
ActionNone RuleAction = ""
ActionAccept RuleAction = "accept"
ActionIgnore RuleAction = "ignore"
ActionReplace RuleAction = "replace"
ActionDocument RuleAction = "doc"
)
var ruleActions = []RuleAction{
ActionAccept, ActionIgnore, ActionReplace, ActionDocument,
}
type RuleTransform string
const (
TransformLower RuleTransform = "lower"
TransformTitle RuleTransform = "title"
TransformExport RuleTransform = "export"
TransformUnexport RuleTransform = "unexport"
TransformUpper RuleTransform = "upper"
)
type RuleTarget string
const (
NoTarget RuleTarget = ""
TargetGlobal RuleTarget = "global"
TargetPostGlobal RuleTarget = "post-global"
//
TargetConst RuleTarget = "const"
TargetType RuleTarget = "type"
TargetFunction RuleTarget = "function"
//
TargetPublic RuleTarget = "public"
TargetPrivate RuleTarget = "private"
)
type ConstRule string
const (
ConstCGOAlias ConstRule = "cgo"
ConstExpand ConstRule = "expand"
ConstEval ConstRule = "eval"
)
type ConstScope string
const (
ConstEnum ConstScope = "enum"
ConstDecl ConstScope = "decl"
ConstDefines ConstScope = "defines"
)
type Tip string
const (
TipPtrSRef Tip = "sref"
TipPtrRef Tip = "ref"
TipPtrArr Tip = "arr"
TipPtrInst Tip = "inst"
TipMemRaw Tip = "raw"
TipTypeNamed Tip = "named"
TipTypePlain Tip = "plain"
TipTypeString Tip = "string"
TipTypeUnsigned Tip = "unsigned"
NoTip Tip = ""
)
type TipKind string
const (
TipKindUnknown TipKind = "unknown"
TipKindPtr TipKind = "ptr"
TipKindType TipKind = "type"
TipKindMem TipKind = "mem"
)
func (t Tip) Kind() TipKind {
switch t {
case TipPtrArr, TipPtrRef, TipPtrSRef, TipPtrInst:
return TipKindPtr
case TipTypePlain, TipTypeNamed, TipTypeString, TipTypeUnsigned:
return TipKindType
case TipMemRaw:
return TipKindMem
default:
return TipKindUnknown
}
}
func (t Tip) IsValid() bool {
switch t {
case TipPtrArr, TipPtrRef, TipPtrSRef, TipPtrInst:
return true
case TipTypePlain, TipTypeNamed, TipTypeString:
return true
case TipMemRaw:
return true
case TipTypeUnsigned:
return true
default:
return false
}
}
type TipSpec struct {
Target string
Tips Tips
Self Tip
Default Tip
}
type TipScope string
const (
TipScopeAny TipScope = "any"
TipScopeStruct TipScope = "struct"
TipScopeType TipScope = "type"
TipScopeEnum TipScope = "enum"
TipScopeFunction TipScope = "function"
)
type Tips []Tip
var builtinRules = map[string]RuleSpec{
"snakecase": {Action: ActionReplace, From: "_([^_]+)", To: "$1", Transform: TransformTitle},
"doc.file": {Action: ActionDocument, To: "$path:$line"},
"doc.google": {Action: ActionDocument, To: "https://google.com/search?q=$file+$name"},
}