-
Notifications
You must be signed in to change notification settings - Fork 351
/
parse.go
346 lines (327 loc) · 8.44 KB
/
parse.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package stmt
import (
"regexp"
"unicode"
)
// prefixCount is the number of words to extract from a prefix.
const prefixCount = 6
// grab grabs i from r, or returns 0 if i >= end.
func grab(r []rune, i, end int) rune {
if i < end {
return r[i]
}
return 0
}
// findSpace finds first space rune in r, returning end if not found.
func findSpace(r []rune, i, end int) (int, bool) {
for ; i < end; i++ {
if IsSpaceOrControl(r[i]) {
return i, true
}
}
return i, false
}
// findNonSpace finds first non space rune in r, returning end if not found.
func findNonSpace(r []rune, i, end int) (int, bool) {
for ; i < end; i++ {
if !IsSpaceOrControl(r[i]) {
return i, true
}
}
return i, false
}
// findRune finds the next rune c in r, returning end if not found.
func findRune(r []rune, i, end int, c rune) (int, bool) {
for ; i < end; i++ {
if r[i] == c {
return i, true
}
}
return i, false
}
// isEmptyLine returns true when r is empty or composed of only whitespace.
func isEmptyLine(r []rune, i, end int) bool {
_, ok := findNonSpace(r, i, end)
return !ok
}
// identifierRE is a regexp that matches dollar tag identifiers ($tag$).
var identifierRE = regexp.MustCompile(`(?i)^[a-z_][a-z0-9_]{0,127}$`)
// readDollarAndTag reads a dollar style $tag$ in r, starting at i, returning
// the enclosed "tag" and position, or -1 if the dollar and tag was invalid.
func readDollarAndTag(r []rune, i, end int) (string, int, bool) {
start, found := i, false
i++
for ; i < end; i++ {
if r[i] == '$' {
found = true
break
}
if i-start > 128 {
break
}
}
if !found {
return "", i, false
}
// check valid identifier
id := string(r[start+1 : i])
if id != "" && !identifierRE.MatchString(id) {
return "", i, false
}
return id, i, true
}
// readString seeks to the end of a string returning the position and whether
// or not the string's end was found.
//
// If the string's terminator was not found, then the result will be the passed
// end.
func readString(r []rune, i, end int, quote rune, tag string) (int, bool) {
var prev, c, next rune
for ; i < end; i++ {
c, next = r[i], grab(r, i+1, end)
switch {
case quote == '\'' && c == '\\':
i++
prev = 0
continue
case quote == '\'' && c == '\'' && next == '\'':
i++
continue
case quote == '\'' && c == '\'' && prev != '\'',
quote == '"' && c == '"',
quote == '`' && c == '`':
return i, true
case quote == '$' && c == '$':
if id, pos, ok := readDollarAndTag(r, i, end); ok && tag == id {
return pos, true
}
}
prev = c
}
return end, false
}
// readMultilineComment finds the end of a multiline comment (ie, '*/').
func readMultilineComment(r []rune, i, end int) (int, bool) {
i++
for ; i < end; i++ {
if r[i-1] == '*' && r[i] == '/' {
return i, true
}
}
return end, false
}
// readStringVar reads a string quoted variable.
func readStringVar(r []rune, i, end int) *Var {
start, q := i, grab(r, i+1, end)
for i += 2; i < end; i++ {
c := grab(r, i, end)
if c == q {
if i-start < 3 {
return nil
}
return &Var{
I: start,
End: i + 1,
Quote: q,
Name: string(r[start+2 : i]),
}
}
/*
// this is commented out, because need to determine what should be
// the "right" behavior ... should we only allow "identifiers"?
else if c != '_' && !unicode.IsLetter(c) && !unicode.IsNumber(c) {
return nil
}
*/
}
return nil
}
// readVar reads variable from r.
func readVar(r []rune, i, end int) *Var {
if grab(r, i, end) != ':' || grab(r, i+1, end) == ':' {
return nil
}
if end-i < 2 {
return nil
}
if c := grab(r, i+1, end); c == '"' || c == '\'' {
return readStringVar(r, i, end)
}
start := i
i++
for ; i < end; i++ {
if c := grab(r, i, end); c != '_' && !unicode.IsLetter(c) && !unicode.IsNumber(c) {
break
}
}
if i-start < 2 {
return nil
}
return &Var{
I: start,
End: i,
Name: string(r[start+1 : i]),
}
}
// readCommand reads the command and any parameters from r, returning the
// offset from i for the end of command, and the end of the command parameters.
//
// A command is defined as the first non-blank text after \, followed by
// parameters up to either the next \ or a control character (for example, \n):
func readCommand(r []rune, i, end int) (int, int) {
command:
// find end of command
for ; i < end; i++ {
next := grab(r, i+1, end)
switch {
case next == 0:
return end, end
case next == '\\' || unicode.IsControl(next):
i++
return i, i
case unicode.IsSpace(next):
i++
break command
}
}
cmd, quote := i, rune(0)
params:
// find end of params
for ; i < end; i++ {
c, next := r[i], grab(r, i+1, end)
switch {
case next == 0:
return cmd, end
case quote == 0 && (c == '\'' || c == '"' || c == '`'):
quote = c
case quote != 0 && c == quote:
quote = 0
// skip escaped
case quote != 0 && c == '\\' && (next == quote || next == '\\'):
i++
case quote == 0 && (c == '\\' || unicode.IsControl(c)):
break params
}
}
// log.Printf(">>> params: %q remaining: %q", string(r[cmd:i]), string(r[i:end]))
return cmd, i
}
// findPrefix finds the prefix in r up to n words.
func findPrefix(r []rune, n int, allowCComments, allowHashComments, allowMultilineComments bool) string {
var s []rune
var words int
loop:
for i, end := 0, len(r); i < end; i++ {
// skip space + control characters
if j, _ := findNonSpace(r, i, end); i != j {
r, end, i = r[j:], end-j, 0
}
// grab current and next character
c, next := grab(r, i, end), grab(r, i+1, end)
switch {
// do nothing
case c == 0:
// statement terminator
case c == ';':
break loop
// single line comments '--' and '//'
case c == '-' && next == '-', c == '/' && next == '/' && allowCComments, c == '#' && allowHashComments:
if i != 0 {
s, words = appendUpperRunes(s, r[:i], ' '), words+1
}
// find line end
if i, _ = findRune(r, i, end, '\n'); i >= end {
break
}
r, end, i = r[i+1:], end-i-1, -1
// multiline comments '/*' '*/'
case c == '/' && next == '*' && allowMultilineComments:
if i != 0 {
s, words = appendUpperRunes(s, r[:i]), words+1
}
// find comment end '*/'
for i += 2; i < end; i++ {
if grab(r, i, end) == '*' && grab(r, i+1, end) == '/' {
r, end, i = r[i+2:], end-i-2, -1
break
}
}
// add space when remaining runes begin with space, and previous
// captured word did not
if sl := len(s); end > 0 && sl != 0 && IsSpaceOrControl(r[0]) && !IsSpaceOrControl(s[sl-1]) {
s = append(s, ' ')
}
// end of statement, max words, or punctuation that can be ignored
case words == n || !unicode.IsLetter(c):
break loop
// ignore remaining, as no prefix can come after
case next != '/' && !unicode.IsLetter(next):
s, words = appendUpperRunes(s, r[:i+1], ' '), words+1
if next == 0 {
break
}
if next == ';' {
break loop
}
r, end, i = r[i+2:], end-i-2, -1
}
}
// trim right ' ', if any
if sl := len(s); sl != 0 && s[sl-1] == ' ' {
return string(s[:sl-1])
}
return string(s)
}
// FindPrefix finds the first 6 prefix words in s.
func FindPrefix(s string, allowCComments, allowHashComments, allowMultilineComments bool) string {
return findPrefix([]rune(s), prefixCount, allowCComments, allowHashComments, allowMultilineComments)
}
// substitute substitutes n runes in r starting at i with the runes in s.
// Dynamically grows r if necessary.
func substitute(r []rune, i, end, n int, s string) ([]rune, int) {
sr, rcap := []rune(s), cap(r)
sn := len(sr)
// grow ...
tlen := len(r) + sn - n
if tlen > rcap {
z := make([]rune, tlen)
copy(z, r)
r = z
} else {
r = r[:rcap]
}
// substitute
copy(r[i+sn:], r[i+n:])
copy(r[i:], sr)
return r[:tlen], tlen
}
// substituteVar substitutes part of r, based on v, with s.
func substituteVar(r []rune, v *Var, s string) ([]rune, int) {
sr, rcap := []rune(s), cap(r)
v.Len = len(sr)
// grow ...
tlen := len(r) + v.Len - (v.End - v.I)
if tlen > rcap {
z := make([]rune, tlen)
copy(z, r)
r = z
} else {
r = r[:rcap]
}
// substitute
copy(r[v.I+v.Len:], r[v.End:])
copy(r[v.I:v.I+v.Len], sr)
return r[:tlen], tlen
}
// appendUpperRunes creates a new []rune from s, with the runes in r on the end
// converted to upper case. extra runes will be appended to the final []rune.
func appendUpperRunes(s []rune, r []rune, extra ...rune) []rune {
sl, rl, el := len(s), len(r), len(extra)
sre := make([]rune, sl+rl+el)
copy(sre[:sl], s)
for i := 0; i < rl; i++ {
sre[sl+i] = unicode.ToUpper(r[i])
}
copy(sre[sl+rl:], extra)
return sre
}