/
fmtparser.go
253 lines (220 loc) · 5.51 KB
/
fmtparser.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package cmdutils
import (
"regexp"
"strings"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/sirupsen/logrus"
)
//nolint:gochecknoglobals
var (
ItemReDefault = regexp.MustCompile(`((?:[^%]|%[^(\n]|\n)*?)($|%\()`)
ItemReStatement = regexp.MustCompile(`((?:[^%]|%[^(\n]|\n)*?)($|[|)]|%\()`)
ItemReEnd = regexp.MustCompile(`((?:[^%]|%[^(\n]|\n)*?)($|\)|%\()`)
ItemReVarStart = regexp.MustCompile(`^%\(`)
ItemReVar = regexp.MustCompile(`^(@?\w+)`)
ItemReCondOper = regexp.MustCompile(`^[!=]=`)
ItemReSearch = regexp.MustCompile(`^~/([^/]+)/`)
ItemReCondVal = regexp.MustCompile(`^\'([\w ]+)\'`)
ItemReStmtOper = regexp.MustCompile(`^[=?]`)
ItemReNoStmtOper = regexp.MustCompile(`^\|`)
ItemReStmtEnd = regexp.MustCompile(`^\)`)
)
const (
MatchSymbol = iota + 1
MatchWord
MatchLookahead
)
type FormatParser struct {
Self string
CurPos int
Stk []string
Result string
Oper string
}
func MakeFormatParser(fmtStr string) *FormatParser {
return &FormatParser{
Self: fmtStr,
}
}
func (fp *FormatParser) LabelEndpoint(p *EndpointLabelerParam) string {
attrs := map[string]string{
"epname": p.EndpointName,
"human": p.Human,
"human_sender": p.HumanSender,
"args": p.Args,
"patterns": p.Patterns,
"needs_int": p.NeedsInt,
"controls": p.Controls,
}
MergeAttributesMap(attrs, p.Attrs)
return fp.Parse(attrs)
}
func (fp *FormatParser) LabelApp(appname, controls string, attrs map[string]*sysl.Attribute) string {
valMap := map[string]string{
"appname": appname,
"controls": controls,
}
MergeAttributesMap(valMap, attrs)
return fp.Parse(valMap)
}
func (fp *FormatParser) LabelClass(classname string) string {
valMap := map[string]string{
"classname": classname,
}
return fp.Parse(valMap)
}
func (fp *FormatParser) FmtSeq(epname, eplongname string, attrs map[string]*sysl.Attribute) string {
valMap := map[string]string{
"epname": epname,
"eplongname": eplongname,
}
MergeAttributesMap(valMap, attrs)
return fp.Parse(valMap)
}
func (fp *FormatParser) FmtOutput(appname, epname, eplongname string, attrs map[string]*sysl.Attribute) string {
valMap := map[string]string{
"appname": appname,
"epname": epname,
"eplongname": eplongname,
}
MergeAttributesMap(valMap, attrs)
return fp.Parse(valMap)
}
func (fp *FormatParser) Parse(attrs map[string]string) string {
logrus.Debugf("self: %s", fp.Self)
logrus.Debugf("attrs: %v", attrs)
fp.Expansions(ItemReDefault, attrs)
formatted := strings.Replace(fp.Result, "\n", "\\n", -1)
fp.Clear()
logrus.Debugf("format string: %s", formatted)
return formatted
}
func MergeAttributesMap(val map[string]string, attrs map[string]*sysl.Attribute) {
for k, v := range attrs {
val["@"+k] = v.GetS()
}
}
func (fp *FormatParser) Expansions(re *regexp.Regexp, attrs map[string]string) {
var result string
for fp.Eat(re) {
prefix := fp.Pop()
prefix = RemovePercentSymbol(prefix)
result += prefix
if fp.Eat(ItemReVarStart) {
var yesStmt, noStmt, varName, value string
isYesStmt, isUseVal, isEqualOper, isSearched := false, true, false, false
if !fp.Eat(ItemReVar) {
panic("missing variable reference")
}
varName = fp.Pop()
value = attrs[varName]
// conditionals
if fp.Eat(ItemReCondOper) {
isEqualOper = true
isUseVal = false
conOper := fp.Oper
fp.Oper = ""
if !fp.Eat(ItemReCondVal) {
panic("missing conditional value")
}
conVal := fp.Pop()
if conOper == "==" && value == conVal {
isYesStmt = true
}
if conOper == "!=" && value != conVal {
isYesStmt = true
}
}
if fp.Eat(ItemReSearch) {
isSearched = true
isUseVal = false
reWordBoundary := regexp.MustCompile(fp.Pop())
if reWordBoundary.MatchString(value) {
isYesStmt = true
}
}
have := fp.Eat(ItemReStmtOper)
if have {
isUseVal = false
fp.Expansions(ItemReStatement, attrs)
yesStmt = fp.Result
}
haveNot := fp.Eat(ItemReNoStmtOper)
if haveNot {
fp.Expansions(ItemReEnd, attrs)
noStmt = fp.Result
}
if !fp.Eat(ItemReStmtEnd) {
panic("unclosed expansion")
}
if isUseVal {
result += value
fp.Result = result
continue
}
if !isSearched && !isEqualOper && value != "" {
isYesStmt = true
}
if isYesStmt {
result += yesStmt
} else {
result += noStmt
}
fp.Result = result
} else {
fp.Result = result
return
}
}
}
func (fp *FormatParser) Eat(re *regexp.Regexp) bool {
matchStr := fp.Self[fp.CurPos:]
if matchStr == "" {
return false
}
subSelfArr := re.FindAllStringSubmatch(matchStr, 1)
if subSelfArr == nil {
return false
}
subSelf := subSelfArr[0]
subSelfLen := len(subSelf)
switch subSelfLen {
case MatchSymbol:
fp.CurPos += len(subSelf[0])
fp.Oper = subSelf[0]
case MatchWord:
insertion := subSelf[0]
fp.Stk = append(fp.Stk, subSelf[1])
fp.CurPos += len(insertion)
case MatchLookahead:
insertion := subSelf[1]
fp.Stk = append(fp.Stk, insertion)
fp.CurPos += len(insertion)
}
return subSelf != nil
}
func (fp *FormatParser) Pop() string {
if fp.Stk == nil {
return ""
}
n := len(fp.Stk)
if n == 0 {
return ""
}
popped := fp.Stk[n-1]
fp.Stk = fp.Stk[:n-1]
return popped
}
func (fp *FormatParser) Clear() {
fp.Result = ""
fp.CurPos = 0
fp.Stk = []string{}
fp.Oper = ""
}
func RemovePercentSymbol(src string) string {
substitute := string([]byte{1})
src = strings.Replace(src, "%%", substitute, -1)
src = strings.Replace(src, "%", "", -1)
src = strings.Replace(src, substitute, "%", -1)
return src
}