/
toml-comments.go
243 lines (218 loc) · 5.67 KB
/
toml-comments.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
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package niseroku
import (
"bufio"
"fmt"
"regexp"
"strings"
)
var (
rxTomlSection = regexp.MustCompile(`^(\s*)(\[[^#]+?\])\s*$`)
rxTomlStatement = regexp.MustCompile(`^(\s*)([^#]+?)\s*=\s*([^#]+?)\s*$`)
rxTomlCommentLine = regexp.MustCompile(`^\s*#(.+?)\s*$`)
rxTomlCommentTrail = regexp.MustCompile(`^\s*([^#]+?)\s*#(.+?)\s*$`)
rxEmptyString = regexp.MustCompile(`^\s*$`)
)
type TomlComments []*TomlComment
func (t TomlComments) String() (v string) {
v += "[\n"
last := len(t) - 1
for idx, tc := range t {
v += "\t" + tc.String()
if idx < last {
v += ","
}
v += "\n"
}
v += "]\n"
return
}
type TomlComment struct {
Lines []string
Inline string
Statement string
}
func (t *TomlComment) String() (v string) {
v += "{ "
v += `s:"` + t.Statement + `", `
v += `l:"` + strings.Join(t.Lines, "\\n") + `", `
v += `t:"` + t.Inline + `"`
v += " }"
return
}
func ParseComments(content string) (tomlComments TomlComments, err error) {
input := strings.NewReader(content)
scanner := bufio.NewScanner(input)
var thisEntry, header *TomlComment
for scanner.Scan() {
line := scanner.Text()
if rxEmptyString.MatchString(line) {
if header == nil {
if thisEntry != nil {
if thisEntry.Statement == "" && len(tomlComments) == 0 {
header = thisEntry
header.Statement = "__header__"
tomlComments = append(tomlComments, header)
thisEntry = nil
}
}
}
continue
}
if rxTomlCommentLine.MatchString(line) {
m := rxTomlCommentLine.FindAllStringSubmatch(line, 1)
if thisEntry == nil {
thisEntry = new(TomlComment)
}
thisEntry.Lines = append(thisEntry.Lines, m[0][1])
continue
}
if rxTomlCommentTrail.MatchString(line) {
m := rxTomlCommentTrail.FindAllStringSubmatch(line, 1)
if thisEntry == nil {
thisEntry = new(TomlComment)
}
if rxTomlStatement.MatchString(m[0][1]) {
mm := rxTomlStatement.FindAllStringSubmatch(m[0][1], 1)
thisEntry.Statement = mm[0][2]
} else if rxTomlSection.MatchString(m[0][1]) {
mm := rxTomlSection.FindAllStringSubmatch(m[0][1], 1)
thisEntry.Statement = mm[0][2]
} else {
err = fmt.Errorf("unable to parse line with trailing comment: %v", line)
return
}
thisEntry.Inline = m[0][2]
tomlComments = append(tomlComments, thisEntry)
thisEntry = nil
continue
}
if thisEntry != nil {
if thisEntry.Statement == "" {
if rxTomlStatement.MatchString(line) {
m := rxTomlStatement.FindAllStringSubmatch(line, 1)
thisEntry.Statement = m[0][2]
tomlComments = append(tomlComments, thisEntry)
thisEntry = nil
continue
}
if rxTomlSection.MatchString(line) {
m := rxTomlSection.FindAllStringSubmatch(line, 1)
thisEntry.Statement = m[0][2]
tomlComments = append(tomlComments, thisEntry)
thisEntry = nil
continue
}
}
}
}
if thisEntry != nil {
if thisEntry.Statement == "" {
thisEntry.Statement = "__footer__"
}
tomlComments = append(tomlComments, thisEntry)
thisEntry = nil
}
err = scanner.Err()
return
}
func ApplyComments(content string, comments TomlComments) (modified string, err error) {
input := strings.NewReader(content)
scanner := bufio.NewScanner(input)
for _, comment := range comments {
if comment.Statement == "__header__" {
if len(comment.Lines) == 0 {
break
}
for _, cl := range comment.Lines {
modified += "#" + cl + "\n"
}
modified += "\n"
break
}
}
isFirstLine := true
prevIsEmpty := false
var prevIsSectionStart, thisIsSectionStart bool
for scanner.Scan() {
line := scanner.Text()
if rxEmptyString.MatchString(line) {
modified += "\n"
prevIsEmpty = true
continue
}
var valid bool
var pad, stmnt, actual string
if valid = rxTomlStatement.MatchString(line); valid {
m := rxTomlStatement.FindAllStringSubmatch(line, 1)
pad = m[0][1]
stmnt = m[0][2]
actual = pad + stmnt + " = " + m[0][3]
} else if valid = rxTomlSection.MatchString(line); valid {
m := rxTomlSection.FindAllStringSubmatch(line, 1)
pad = m[0][1]
stmnt = m[0][2]
actual = pad + stmnt
prevIsSectionStart = true
thisIsSectionStart = true
} else {
err = fmt.Errorf("invalid toml statement: %v\n", line)
return
}
found := false
for _, comment := range comments {
if found = comment.Statement == stmnt; found {
if len(comment.Lines) > 0 {
if !prevIsSectionStart && !isFirstLine && !prevIsEmpty {
modified += "\n"
}
for _, cl := range comment.Lines {
modified += pad + "#" + cl + "\n"
}
}
modified += actual
if comment.Inline != "" {
modified += " #" + comment.Inline
}
modified += "\n"
break
}
}
if !found {
modified += line + "\n"
}
prevIsEmpty = false
isFirstLine = false
if thisIsSectionStart {
thisIsSectionStart = false
} else {
prevIsSectionStart = false
}
}
for _, comment := range comments {
if comment.Statement == "__footer__" {
if len(comment.Lines) == 0 {
break
}
modified += "\n"
for _, cl := range comment.Lines {
modified += "#" + cl + "\n"
}
break
}
}
err = scanner.Err()
return
}