-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight_scala.py
180 lines (134 loc) · 4.79 KB
/
highlight_scala.py
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
class HighlighterScala:
builtins = [
'Math'
]
kwlist = [
'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends',
'false', 'final', 'finally', 'for', 'forSome', 'if', 'implicit',
'import', 'lazy', 'match', 'new', 'null', 'object', 'override',
'package', 'private', 'protected', 'return', 'sealed', 'super',
'this', 'throw', 'trait', 'true', 'try', 'type', 'val', 'var',
'while', 'with', 'yield'
]
colors = [
# (start tokens, color, type, end token)
#(['class'], 52, 'between', ':('),
(['def'], 52, 'between', '(='),
#(['self'], 209),
(kwlist, 209),
(builtins, 52),
(':', 52, 'between', ',=)('),
('=-+*%^&|></~⇒←#@_:', 161),
('1234567890', 7),
#('#', 245, 'till', '\n'),
('\"', 11, 'till', '\"'),
('\'', 11, 'till', '\'')
]
def __init__(self, rules=None):
if rules:
self.rules = rules
else:
self.rules = HighlighterScala.colors
self.multiLineComment = None
self.backslash = False
self.backslashColor = 6
def getSpecial(self, c):
if len(c) <= 2:
return None
else:
return c[1:4]
def split(self, line):
dividers = ' 1234567890(){}[]=+-*^%|?&<>.,:;@/~#\'\"\\'
ls = []
latest = ''
currentDividing = False
for char in line:
if char in dividers:
if latest != '':
ls.append(latest)
currentDividing = True
latest = char
else:
if currentDividing:
currentDividing = False
ls.append(latest)
latest = ''
latest += char
ls.append(latest)
return ls
def getColors(self, line):
ls = self.split(line)
newLs = []
special = None
# f = open('debug.log', 'a')
# t = self.rules[4][0]
# f.write(f'update = {t}\n')
last3 = []
for k in ls:
last3.append(k)
if len(last3) > 3:
last3.pop(0)
if self.multiLineComment:
# print(f'debug {last3} {special}')
if ''.join(last3) == self.multiLineComment:
# print('end')
self.multiLineComment = None
newLs.append((k, 11))
continue
# print(f'special {special} {k}')
if special:
type_ = special[1]
color = special[0]
# if type_ != 'between':
# newLs.append((k, special[0]))
# char right after a backslash
if self.backslash:
newLs.append((k, self.backslashColor))
self.backslash = False
continue
# check for backslash in a string
if k == '\\' and special == (11, 'till', '\'\"'):
newLs.append((k, self.backslashColor))
self.backslash = True
continue
if k in special[2] and type_ in ['till', 'between']:
# end of special
# f.write(f'ending {special}\n')
special = None
if type_ != 'between':
newLs.append((k, color))
continue
else:
pass # between at the end (i.e. at the end token)
else:
# not the end, i.e. middle section
newLs.append((k, color))
continue
if ''.join(last3) in ['\'\'\'', '"""']: #11, 'till', '\'\"'
# print('debug!')
# print(last3)
self.multiLineComment = ''.join(last3)
special = None
newLs.append((k, 11))
continue
for c in self.rules:
if k in c[0]:
s = self.getSpecial(c)
if s:
assert s[1] in ['till', 'between']
special = s
# f.write(f'activating {c}, key = {k}\n')
if s and s[1] == 'between':
continue
newLs.append((k, c[1]))
break
else:
newLs.append((k, 16))
# f.close()
return newLs
def getAllColors(self, lines):
self.multiLineComment = None
return [self.getColors(l) for l in lines]
if __name__ == '__main__':
h = Highlighter()
print(h.getColors(' t.column\'s\\a\' abc'))