-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc3_pre_select.go
201 lines (175 loc) · 6.5 KB
/
proc3_pre_select.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
// MIT License
//
// Copyright (c) 2021 @gxlb
// Url:
// https://github.com/gxlb
// https://gitee.com/gxlb
// AUTHORS:
// Ally Dale <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gogp
import (
"fmt"
"strings"
)
// deal with #GOGP_IFDEF and #GOGP_SWITCH for .gp file
func (this *gopgProcessor) step3PretreatGpCodeSelector(gpContent string, section string) (replaced string) {
this.maps.clear()
replaced = gpContent
repCnt := 1 //init first loop
for depth := 0; repCnt > 0; depth++ {
replaced, repCnt = this.pretreatSelector(replaced, section, depth)
}
return
}
var boolTrueValues = []string{"true", "t", "yes", "y", "1"}
// parse bool value from string, treat unknown strings as false
func parseBoolValue(val string) bool {
for _, v := range boolTrueValues {
if strings.EqualFold(val, v) {
return true
}
}
return false
}
func (this *gopgProcessor) selectPart(section, sel string, depth int) string {
if depth <= maxRecursionDepth {
rep, _ := this.pretreatSelector(sel, section, depth+1)
return gogpExpComment.ReplaceAllString(rep, "")
}
return gogpExpComment.ReplaceAllString(sel, "")
}
// " <key> || !<key> || <key> == xxx || <key> != xxx "
func (this *gopgProcessor) checkCondition(section, condition string, predefKey string) bool {
conds := strings.Split(condition, "||")
selOk := false
for _, cond := range conds {
elems := gogpExpCondition.FindAllStringSubmatch(cond, -1)
if len(elems) == 0 {
fmt.Printf("[gogp error]: [%s:%s %s] condition(%s) not match patten\n", relateGoPath(this.gpgPath), relateGoPath(this.gpPath), section, cond)
return false
}
// ["", "NOT", "KEY", "OP","VALUE"]
elem := elems[0]
not, key, op, value := elem[1], elem[2], elem[3], elem[4]
condValCheck := (not == "")
if s := len(key); s >= 2 && key[0] == '<' && key[s-1] == '>' { // <key> -> key
key = key[1 : s-1]
}
cfg := this.getGpgCfg(section, key, false)
condResult := false
switch {
case predefKey != "":
if op != "" || value != "" {
fmt.Printf("[gogp warn]: [%s:%s %s] condition(%s) unexpected operator [%s, %s]\n", relateGoPath(this.gpgPath), relateGoPath(this.gpPath), section, cond, op, value)
}
if s := len(predefKey); s >= 2 && predefKey[0] == '<' && predefKey[s-1] == '>' { // <predefKey> -> predefKey
predefKey = predefKey[1 : s-1]
}
predefVal := this.getGpgCfg(section, predefKey, false)
condResult = (predefVal == key)
case value != "":
switch op {
case "==":
condResult = (cfg == value)
case "!=":
condResult = (cfg != value)
default:
fmt.Printf("[gogp error]: [%s:%s %s] condition(%s) undefined operator [%s]\n", relateGoPath(this.gpgPath), relateGoPath(this.gpPath), section, cond, op)
condResult = false
}
default:
if op != "" {
fmt.Printf("[gogp warn]: [%s:%s %s] condition(%s) unexpected operator [%s]\n", relateGoPath(this.gpgPath), relateGoPath(this.gpPath), section, cond, op)
}
condResult = parseBoolValue(cfg)
}
if condResult == condValCheck {
selOk = true
break
}
}
return selOk
}
func (this *gopgProcessor) selectByCondition(section, cond, t, f string, depth int) string {
ret := ""
if this.checkCondition(section, cond, "") {
ret = this.selectPart(section, t, depth)
} else {
ret = this.selectPart(section, f, depth)
}
return ret
}
func (this *gopgProcessor) selectByCases(section, cases string, predefKey string, multiSwitch bool) string {
defaultContent := ""
found := false
repaced := gogpExpCases.ReplaceAllStringFunc(cases, func(src string) string {
if found && !multiSwitch { //ignore the rest cases if has found in one-way switch
return ""
}
elem := gogpExpCases.FindAllStringSubmatch(src, -1)[0]
cond, content := elem[1], elem[2]
switch {
case cond == "": //DEFAULT branch
defaultContent = content
case this.checkCondition(section, cond, predefKey): //CASE branch
found = true
return content
}
return ""
})
if !found {
return defaultContent
}
return repaced
}
func (this *gopgProcessor) pretreatSelector(gpContent string, section string, depth int) (replaced string, repCnt int) {
if depth > maxRecursionDepth { //limit recursion depth
s := fmt.Sprintf("[gogp error]: [%s:%s %s depth=%d] replace recursion too deep\n", relateGoPath(this.gpgPath), relateGoPath(this.gpPath), section, depth)
fmt.Errorf("%s", s)
return gpContent, 0
}
replaced = gogpExpCodeSelector.ReplaceAllStringFunc(gpContent, func(src string) (rep string) {
repCnt++
// []string{"", "IGNORE", "GPONLY", "MAPSRC", "MAPDST", "SWITCHKEY", "SWITCHCONTENT", "MULTISWITCHKEY", "MULTISWITCHCONTENT", "IFCOND", "IFT", "IFF", "IFCOND2", "IFT2", "IFF2"}
elem := gogpExpCodeSelector.FindAllStringSubmatch(src, -1)[0]
ignore, gponly, mapK, mapV, switchKey, switchCases, multiswitchKey, multiswitchCases, condk, condHit, condMiss, condk2, condHit2, condMiss2 :=
elem[1], elem[2], elem[3], elem[4], elem[5], elem[6], elem[7], elem[8], elem[9], elem[10], elem[11], elem[12], elem[13], elem[14]
switch {
case condk != "":
rep = this.selectByCondition(section, condk, condHit, condMiss, depth)
case condk2 != "":
rep = this.selectByCondition(section, condk2, condHit2, condMiss2, depth)
case switchCases != "":
return this.selectByCases(section, switchCases, switchKey, false)
case multiswitchCases != "":
return this.selectByCases(section, multiswitchCases, multiswitchKey, true)
case mapK != "":
this.maps.insert(mapK, mapV, false)
rep = ""
case ignore != "" || gponly != "":
rep = "\n\n"
default:
rep = ""
}
return
})
return
}