-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.bah
126 lines (111 loc) · 3.55 KB
/
colors.bah
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
//ASCII terminal sequences
const asciiEscape = "\033["
const asciiReset = "\033[m"
const asciiBold = "\033[1m"
const asciiUnderline = "\033[4m"
//Used to set a rgb text color in the terminal
rgbColor(r int, g int, b int) str {
return asciiEscape+"38;2;"+intToStr(r)+";"+intToStr(g)+";"+intToStr(b)+"m"
}
//Used for appending a cpstring to an array
appendStr(a []char, b str) uint32 {
i=0; for b[i] != null, i++ {
a[len(a)] = b[i]
}
return i
}
//Because of the 'else if in' bug
inArrStr(s str, arr []str) bool {
return s in arr
}
//Primitives bah types for syntax highlighting
bahTypes = []str{
"int", "uint", "int32", "uint32",
"cpstring", "char",
"float", "float32",
"ptr"
}
//Special values that are not variables for syntax highlighting
bahSpecialVals = []str {
"null"
}
//Default colors (from bcat)
defaultColor = rgbColor(218, 215, 218)
bgColor = rgbColor(33,33,33)
varColor = rgbColor(110,206,208)
fnColor = rgbColor(233,228,138)
typeColor = rgbColor(76,175,128)
strColor = rgbColor(248,135,80)
numColor = rgbColor(127,208,149)
kwColor = rgbColor(206,142,204)
hashColor = rgbColor(110,110,110)
constColor = rgbColor(33,143,236)
commColor = rgbColor(84,155,54)
lineColor = rgbColor(90, 50, 50)
//Text markup buffer
buff = []char
//Returns colored text from bah code.
syntaxHighlight(s []char) str {
clear(buff)
tokens = lexer(arrToStr(s), len(s))
if len(s) > 0 && (s[0] == '.' || s[0] == '$') {
appendStr(buff, lineColor)
i=0; for i < len(s), i++ {
buff[len(buff)] = s[i]
}
appendStr(buff, asciiReset)
return arrToStr(buff)
}
i=0; for i < len(s), i++ {
found = false
k=0; for k < len(tokens), k++ {
if tokens[k].pos == i {
found = true
break
} else if tokens[k].pos > i {
break
}
}
if s[i] == '/' && i+1 < len(s) && s[i+1] == '/' {
appendStr(buff, asciiBold + commColor)
appendStr(buff, arrToStr(s[i:]))
appendStr(buff, asciiReset)
break
}
if found == true {
color = ""
if tokens[k].type == TOKEN_TYPE_VAR {
if k+1 < len(tokens) && tokens[k+1].cont == "(" {
color = fnColor
} else if inArrStr(tokens[k].ogCont, bahTypes) {
color = typeColor
} else if inArrStr(tokens[k].ogCont, bahSpecialVals) {
color = constColor
} else {
color = varColor
}
} else if tokens[k].type == TOKEN_TYPE_BOOL {
color = constColor
} else if tokens[k].type == TOKEN_TYPE_STR && tokens[k].line == tokens[k].begLine {
color = strColor
} else if tokens[k].type == TOKEN_TYPE_INT || tokens[k].type == TOKEN_TYPE_FLOAT {
color = numColor
} else if tokens[k].type == TOKEN_TYPE_CAST {
color = typeColor
} else if tokens[k].type == TOKEN_TYPE_KEYWORD {
color = kwColor
if tokens[k].ogCont[0] == '#' {
color = rgbColor(255, 0, 0)
}
} else if tokens[k].ogCont == ";" {
color = rgbColor(255, 0, 0)
}
i += len(tokens[k].ogCont) - 1
appendStr(buff, color+tokens[k].ogCont+defaultColor)
continue
}
buff[len(buff)] = s[i]
}
appendStr(buff, asciiReset)
return arrToStr(buff)
}