-
Notifications
You must be signed in to change notification settings - Fork 46
/
parser.go
183 lines (177 loc) · 5.04 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
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
package parser
import (
"github.com/yaklang/yaklang/common/utils"
"strings"
)
type fuzztagPos struct {
tagType *TagDefine
start, end int
subs []*fuzztagPos
}
func Parse(raw string, tagTypes ...*TagDefine) ([]Node, error) {
// 去重、提取标签边界符
utagTypes := []*TagDefine{}
tagBoundaryMap := map[string]rune{} // 转义符映射
var tagBoundarys []string
tagBoundarysDirection := map[string]int{}
typeMap := map[string]struct{}{}
for _, tagType := range tagTypes {
if _, ok := typeMap[tagType.name]; !ok {
tagBoundarysDirection[tagType.start] = 0
tagBoundarysDirection[tagType.end] = 1
l := []string{tagType.start, tagType.end}
if tagType.start == tagType.end {
l = []string{tagType.start}
}
for _, t := range l {
if _, ok := tagBoundaryMap[t]; !ok {
tagBoundarys = append(tagBoundarys, t)
} else {
return nil, utils.Errorf("tag boundary of tag `%s` conflicts with other tags", tagType.name)
}
}
utagTypes = append(utagTypes, tagType)
}
}
// 查找所有标签位置信息
tagBoundaryPostions1 := IndexAllSubstrings(raw, tagBoundarys...)
pre := [2]int{-1, -1}
tagBoundaryPostions := [][2]int{}
for _, postion := range tagBoundaryPostions1 {
if pre[0] != -1 {
if pre[1] == postion[1] { // 当多个字符串之前存在包含关系时,只保留长的字符串
tagBoundaryPostions = tagBoundaryPostions[:len(tagBoundaryPostions)-1]
}
if pre[0] == postion[0] {
switch tagBoundarysDirection[tagBoundarys[pre[0]]] {
case 0: // left
if pre[1]+len(tagBoundarys[pre[0]]) > postion[1] {
tagBoundaryPostions = tagBoundaryPostions[:len(tagBoundaryPostions)-1]
tagBoundaryPostions = append(tagBoundaryPostions, postion)
pre = postion
continue
}
case 1:
if pre[1]+len(tagBoundarys[pre[0]]) > postion[1] {
pre = postion
continue
}
}
}
}
pre = postion
tagBoundaryPostions = append(tagBoundaryPostions, postion)
}
escapeSymbol := `\`
stack := utils.NewStack[*fuzztagPos]()
rootTags := []*fuzztagPos{}
nextStart := 0
//var prePos [2]int
//first := false
//for _, pos := range tagBoundaryPostions {
// if first {
// prePos = pos
// first = false
// } else {
// switch pos[0] {
// case 0:
// case :
//
// }
// }
//}
for _, pos := range tagBoundaryPostions {
if pos[1] < nextStart {
continue
}
if stack.Size() > 0 && pos[1] >= len(escapeSymbol) { // 跳过被转义的边界符
if raw[pos[1]-len(escapeSymbol):pos[1]] == escapeSymbol {
nextStart = pos[1] + len(tagBoundarys[pos[0]])
continue
}
}
tagIndex := pos[0] / 2
isleft := pos[0]%2 == 0
typ := tagTypes[tagIndex]
if isleft {
if stack.Size() != 0 && stack.Peek().tagType.raw && !typ.raw {
continue
}
tag := &fuzztagPos{start: pos[1] + len(typ.start), tagType: typ}
if stack.Size() != 0 {
top := stack.Peek()
top.subs = append(top.subs, tag)
} else {
rootTags = append(rootTags, tag)
}
stack.Push(tag)
} else if stack.Size() != 0 && stack.Peek().tagType.name == typ.name {
top := stack.Pop()
top.end = pos[1]
}
}
// 过滤未闭合的标签
var filterValidTag func(rootTags []*fuzztagPos) (result []*fuzztagPos)
filterValidTag = func(rootTags []*fuzztagPos) (result []*fuzztagPos) {
for _, tag := range rootTags {
if tag.end == 0 { // 无效tag
result = append(result, filterValidTag(tag.subs)...)
} else {
result = append(result, tag)
}
}
return
}
//escapersMap := map[*TagDefine]*Escaper{}
//for _, tagType := range tagTypes {
// escapersMap[tagType] = NewDefaultEscaper(escapeSymbol, tagType.start, tagType.end)
//}
escapersMap := map[*TagDefine]func(s string) string{}
for _, tagType := range tagTypes {
tagType := tagType
escapersMap[tagType] = func(s string) string {
s = strings.Replace(s, escapeSymbol+tagType.start, tagType.start, -1)
s = strings.Replace(s, escapeSymbol+tagType.end, tagType.end, -1)
return s
}
}
// 根据标签位位置信息解析tag
var newDatasFromPos func(start, end int, tagType *TagDefine, poss []*fuzztagPos, deep int) []Node
newDatasFromPos = func(start, end int, tagType *TagDefine, poss []*fuzztagPos, deep int) []Node {
pre := start
res := []Node{}
var addRes func(s Node)
if deep > 0 {
addRes = func(s Node) {
if v, ok := s.(StringNode); ok && tagType != nil {
v1 := escapersMap[tagType](string(v))
res = append(res, StringNode(v1))
} else {
res = append(res, s)
}
}
} else {
addRes = func(s Node) {
res = append(res, s)
}
}
for _, pos := range poss {
if pos.start < start || pos.end > end { // 不解析参数外的fuzztag
continue
}
tag := pos.tagType.NewTag()
tag.AddData(newDatasFromPos(pos.start, pos.end, pos.tagType, pos.subs, deep+1)...)
s := raw[pre : pos.start-len(pos.tagType.start)]
if len(s) != 0 {
addRes(StringNode(s))
}
addRes(tag)
pre = pos.end + len(pos.tagType.end)
}
if pre < end {
addRes(StringNode(raw[pre:end]))
}
return res
}
return newDatasFromPos(0, len(raw), nil, filterValidTag(rootTags), 0), nil
}