-
Notifications
You must be signed in to change notification settings - Fork 8
/
syntax_highlighting_golang.go
343 lines (327 loc) · 8.72 KB
/
syntax_highlighting_golang.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
package main
import "github.com/spaolacci/murmur3"
func detect_golang(file [][]string) bool {
for _, row := range file {
var rowhash uint64
for _, cell := range row {
if cell == "/" {
break
}
switch cell {
case "p", "a", "c", "k", "g":
rowhash = hash(cell[0], rowhash)
case "e":
if rowhash == hashstr("packag") {
return true
}
case " ", "\t", "\r", "\n", "":
rowhash = 0
default:
return false
}
}
}
return false
}
func reprocess_syntax_highlighting_golang(file [][]string) (out [][5]int) {
var comments, strings bool
var open [3]int
if !detect_golang(file) {
return out
}
for y := range file {
out = append(out, reprocess_syntax_highlighting_row_golang(file[y], y, &comments, &strings, &open)...)
}
return out
}
func hashstr(str string) (x uint64) {
for i := range str {
x = hash(str[i], x)
}
return x
}
func hash(b byte, x uint64) uint64 {
x += uint64(b)
x ^= x << 13
x ^= x >> 7
x ^= x << 17
return x - 1
}
// constants for brace highlighter
var constant1 = uint64(16307611254389450217)
var constant0 = uint64(16367132170988004358)
var constant2 = uint64(9739585463058420216)
const xor1 = 157128949
const xor0 = 553920306
const xor2 = 725611491
func reprocess_syntax_highlighting_row_golang_color(x, y int, constant, id uint64) (ret [5]int) {
ret[0] = x
ret[1] = y
var hash = murmur3.Sum32WithSeed(nil, uint32((((((id)*888286447)+constant)*271164791)+id)*749130113))
ret[2] = int(hash>>10) & 255
ret[3] = int(hash>>0) & 255
ret[4] = int(hash>>20) & 255
for i := 64; i > 0; i >>= 1 {
if ret[2]+ret[3]+ret[4] < i*3 {
if ret[2] < 255-i {
ret[2] += i
}
if ret[3] < 255-i {
ret[3] += i
}
if ret[4] < 255-64 {
ret[4] += i
}
}
}
return
}
func reprocess_syntax_highlighting_row_golang(row []string, y int, comments, strings *bool, open *[3]int) (out [][5]int) {
var loaded uint64
var digits, sglquote, dblquote, backquote, comment1, comment2, comment, escape, alpha bool
comment = *comments
if comment {
out = append(out, [5]int{0, y, 0, 128, 255})
}
backquote = *strings
if backquote {
out = append(out, [5]int{0, y, 0, 255, 0})
}
var length int
for x, a := range row {
switch a {
case "*":
if !sglquote && !dblquote && !backquote {
if comment {
comment2 = true
comment1 = false
} else if comment1 {
out = append(out, [5]int{x - 1, y, 0, 128, 255})
*comments = true
comment = true
}
}
if !comment && !sglquote && !dblquote && !backquote {
out = append(out, reprocess_syntax_highlighting_end_golang(loaded, length, x, y, digits)...)
length = 0
loaded = 0
}
case "/":
if !sglquote && !dblquote && !backquote {
if comment1 {
out = append(out, [5]int{x - 1, y, 0, 128, 255})
comment = true
} else if comment2 {
comment = false
comment1 = false
comment2 = false
*comments = false
out = append(out, [5]int{x + 1, y, 255, 255, 255})
length = 0
loaded = 0
} else {
comment1 = true
}
}
case "\\":
if dblquote {
out = append(out, [5]int{x, y, 128, 128, 255})
out = append(out, [5]int{x + 2, y, 0, 255, 0})
escape = true
continue
}
case "'":
if !comment {
if sglquote && !escape {
out = append(out, [5]int{x + 1, y, 255, 255, 255})
sglquote = false
escape = false
length = 0
loaded = 0
} else {
out = append(out, [5]int{x, y, 0, 255, 0})
sglquote = true
escape = false
}
}
break
case "\"":
if !comment {
if dblquote && !escape {
out = append(out, [5]int{x + 1, y, 255, 255, 255})
dblquote = false
escape = false
length = 0
loaded = 0
} else {
out = append(out, [5]int{x, y, 0, 255, 0})
dblquote = true
escape = false
}
}
break
case "`":
if !comment {
if backquote && !dblquote && !sglquote {
out = append(out, [5]int{x + 1, y, 255, 255, 255})
backquote = false
length = 0
loaded = 0
} else {
out = append(out, [5]int{x, y, 0, 255, 0})
backquote = !dblquote
}
*strings = backquote
}
continue
case "x":
if !comment && !sglquote && !dblquote && !backquote {
if !(length == 1 && digits && !alpha) {
length++
break
}
} else {
break
}
fallthrough
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
if !comment && !sglquote && !dblquote && !backquote {
loaded = hash(a[0], loaded)
if !digits {
digits = !alpha
}
length++
}
comment1 = false
comment2 = false
case "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F":
if !comment && !sglquote && !dblquote && !backquote {
if !(digits && !alpha) {
digits = false
}
loaded = hash(a[0], loaded)
length++
}
comment1 = false
comment2 = false
case "j", "q", "z":
fallthrough
case "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z":
comment1 = false
comment2 = false
digits = false
alpha = true
if !comment && !sglquote && !dblquote && !backquote {
length++
}
case "s", "r", "v", "i", "u", "o", "w", "n", "l", "p", "t", "h", "y", "k", "g", "m":
if !comment && !sglquote && !dblquote && !backquote {
loaded = hash(a[0], loaded)
length++
digits = false
alpha = true
}
comment1 = false
comment2 = false
case ".":
if !comment && !sglquote && !dblquote && !backquote {
if digits {
loaded = hash(a[0], loaded)
length++
break
}
} else {
break
}
fallthrough
case ":", " ", "", "\t", ";", ",", "+", "&", "|", "-", ">", "<":
if !comment && !sglquote && !dblquote && !backquote {
out = append(out, reprocess_syntax_highlighting_end_golang(loaded, length, x, y, digits)...)
length = 0
loaded = 0
}
fallthrough
case "(", ")", "{", "[", "]", "}":
comment1 = false
if !comment && !sglquote && !dblquote && !backquote {
out = append(out, reprocess_syntax_highlighting_end_golang(loaded, length, x, y, digits)...)
length = 0
loaded = 0
switch a {
case "}":
open[0]--
case ")":
open[1]--
case "]":
open[2]--
}
switch a {
case "}", "{":
out = append(out, reprocess_syntax_highlighting_row_golang_color(x, y, constant0, uint64(open[0])^xor0))
case ")", "(":
out = append(out, reprocess_syntax_highlighting_row_golang_color(x, y, constant1, uint64(open[1])^xor1))
case "]", "[":
out = append(out, reprocess_syntax_highlighting_row_golang_color(x, y, constant2, uint64(open[2])^xor2))
}
switch a {
case "{":
open[0]++
case "(":
open[1]++
case "[":
open[2]++
}
}
alpha = false
digits = false
default:
digits = false
comment1 = false
comment2 = false
alpha = false
}
if (x&7 == 0) && !comment && (sglquote || dblquote || backquote) {
out = append(out, [5]int{x, y, 0, 255, 0})
}
if (x&7 == 0) && (comment) {
out = append(out, [5]int{x, y, 0, 128, 255})
}
if (x&7 == 0) && !comment && (digits && !alpha) {
out = append(out, [5]int{x, y, 255, 0, 255})
}
escape = false
}
out = append(out, reprocess_syntax_highlighting_end_golang(loaded, length, len(row), y, digits)...)
return out
}
func reprocess_syntax_highlighting_end_golang(loaded uint64, length, x, y int, digits bool) (out [][5]int) {
switch loaded {
case hashstr("func"), hashstr("if"), hashstr("return"), hashstr("case"), hashstr("for"),
hashstr("switch"), hashstr("len"), hashstr("append"), hashstr("range"), hashstr("else"),
hashstr("package"), hashstr("else"), hashstr("default"), hashstr("var"), hashstr("struct"),
hashstr("type"), hashstr("import"), hashstr("break"), hashstr("continue"), hashstr("fallthrough"),
hashstr("const"), hashstr("interface"), hashstr("cap"), hashstr("go"), hashstr("make"),
hashstr("new"), hashstr("panic"):
out = append(out, [5]int{x - length, y, 255, 128, 0})
out = append(out, [5]int{x, y, 255, 255, 255})
case hashstr("int"), hashstr("uint"), hashstr("int64"), hashstr("int32"), hashstr("uint64"),
hashstr("uint32"), hashstr("bool"), hashstr("byte"), hashstr("string"), hashstr("error"),
hashstr("int8"), hashstr("uint8"), hashstr("float32"), hashstr("float64"),
hashstr("chan"), hashstr("map"):
out = append(out, [5]int{x - length, y, 0, 255, 255})
out = append(out, [5]int{x, y, 255, 255, 255})
case hashstr("true"), hashstr("false"), hashstr("nil"):
out = append(out, [5]int{x - length, y, 255, 0, 255})
out = append(out, [5]int{x, y, 255, 255, 255})
default:
if !(loaded == hashstr("x") || loaded == hashstr("xx")) {
if digits {
out = append(out, [5]int{x - length, y, 255, 0, 255})
out = append(out, [5]int{x, y, 255, 255, 255})
} else {
out = append(out, [5]int{x - length, y, 255, 255, 255})
}
}
}
return out
}