-
Notifications
You must be signed in to change notification settings - Fork 0
/
marker.go
143 lines (107 loc) · 2.73 KB
/
marker.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
package markers
import (
"go/ast"
"go/token"
"strings"
)
type Validate interface {
Validate() error
}
type MarkerValues map[string][]any
func (markerValues MarkerValues) Count() int {
count := 0
for _, markers := range markerValues {
count = count + len(markers)
}
return count
}
func (markerValues MarkerValues) AllMarkers(name string) []any {
result := markerValues[name]
if len(result) == 0 {
return nil
}
return result
}
func (markerValues MarkerValues) First(name string) any {
result := markerValues[name]
if len(result) == 0 {
return nil
}
return result[0]
}
func (markerValues MarkerValues) CountByName(name string) int {
result := markerValues[name]
return len(result)
}
type markerComment struct {
commentLines []*ast.Comment
}
func newMarkerComment(comment *ast.Comment) *markerComment {
markerComment := &markerComment{
make([]*ast.Comment, 0),
}
markerComment.commentLines = append(markerComment.commentLines, comment)
return markerComment
}
func (c markerComment) Pos() token.Pos {
return c.commentLines[0].Pos()
}
func (c markerComment) End() token.Pos {
return c.commentLines[len(c.commentLines)].End()
}
func (c *markerComment) append(comment *ast.Comment) {
c.commentLines = append(c.commentLines, comment)
}
func (c *markerComment) Text() string {
var text string
for _, line := range c.commentLines {
comment := strings.TrimSpace(line.Text[2:])
if strings.HasSuffix(comment, "\\") {
comment = strings.TrimSpace(comment[:len(comment)-1])
}
if text == "" {
text = comment
} else {
text = text + " " + comment
}
}
return text
}
func splitMarker(marker string) (name string, anonymousName string, options string) {
marker = marker[1:]
nameFieldParts := strings.SplitN(marker, "=", 2)
if len(nameFieldParts) == 1 {
value := strings.TrimRight(nameFieldParts[0], " ")
value = strings.TrimLeft(value, " ")
value = strings.Split(value, " ")[0]
return value, value, ""
}
fields := nameFieldParts[1]
anonymousName = strings.TrimRight(nameFieldParts[0], "\t ")
anonymousName = strings.TrimLeft(anonymousName, "\t ")
name = anonymousName
nameParts := strings.Split(name, ":")
if len(nameParts) > 1 {
name = strings.Join(nameParts[:len(nameParts)-1], ":")
}
if len(anonymousName) >= len(name)+1 {
fields = anonymousName[len(name)+1:] + "=" + fields
} else {
fields = "=" + fields
}
return name, anonymousName, fields
}
func isMarkerComment(comment string) bool {
if comment[0:2] != "//" {
return false
}
stripped := strings.TrimSpace(comment[2:])
if len(stripped) < 1 || stripped[0] != '+' {
return false
}
return true
}
func isMultiLineComment(comment string) bool {
stripped := strings.TrimSpace(comment[2:])
return strings.HasSuffix(stripped, "\\")
}