/
syntax.py
556 lines (509 loc) · 19 KB
/
syntax.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import sys
import os
from PyQt4.QtCore import QRegExp
from PyQt4.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
def format(color):
#Return a QTextCharFormat with the given attributes.
_color = QColor()
_color.setNamedColor(color)
_format = QTextCharFormat()
_format.setForeground(_color)
return _format
class BoostSourceHighlighter(QSyntaxHighlighter):
global syntax_mode
file_extensions = ["py", "java", "cpp", "c", "htm", "html", "js",
"css", "php", "h", "cs", "sh", "rs"]
def __init__(self, document, filename, syntax_mode, syntax_theme):
self.document = document
QSyntaxHighlighter.__init__(self, self.document)
try:
filename_extension = filename.rsplit('.', 1)
filename_extension = filename_extension[1]
except:
filename_extension = ""
for extension in BoostSourceHighlighter.file_extensions:
if extension == filename_extension:
syntax_mode = filename_extension
# Syntax styles that can be shared by all languages
# Theme support: temporarily sys.argv input
try:
if syntax_theme == "Monochrome" or str(sys.argv[1]) == "monochrome":
STYLES = {
'keyword': format('#38898C'),
'operator': format('#419141'),
'brace': format('#878787'),
'defclass': format('#759F6F'),
'string': format('#759F6F'),
'string2': format('#759F6F'),
'comment': format('#6b6b6b'),
'self': format('#38898C'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Red" or str(sys.argv[1]) == "red":
STYLES = {
'keyword': format('#770000'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#9C4343'),
'string': format('#9C4343'),
'string2': format('#9C4343'),
'comment': format('#494949'),
'self': format('#770000'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Orange" or str(sys.argv[1]) == "orange":
STYLES = {
'keyword': format('#8F471E'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#e87330'),
'string': format('#e87330'),
'string2': format('#e87330'),
'comment': format('#494949'),
'self': format('#8F471E'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Yellow" or str(sys.argv[1]) == "yellow":
STYLES = {
'keyword': format('#777700'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#A5A448'),
'string': format('#848337'),
'string2': format('#848337'),
'comment': format('#494949'),
'self': format('#777700'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Blue" or str(sys.argv[1]) == "blue":
STYLES = {
'keyword': format('#003377'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#375c84'),
'string': format('#375c84'),
'string2': format('#375c84'),
'comment': format('#494949'),
'self': format('#003377'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Green" or str(sys.argv[1]) == "green":
STYLES = {
'keyword': format('#007765'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#378437'),
'string': format('#378437'),
'string2': format('#378437'),
'comment': format('#494949'),
'self': format('#007765'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Flo\'s V8 Cafe" or str(sys.argv[1]) == "flosv8cafe":
STYLES = {
'keyword': format('#0D8580'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#EF7196'),
'string': format('#EF7196'),
'string2': format('#EF7196'),
'comment': format('#494949'),
'self': format('#0D8580'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Stranger Things" or str(sys.argv[1]) == "strangerthings":
STYLES = {
'keyword': format('#222F57'),
'operator': format('#7A7A7A'),
'brace': format('#878787'),
'defclass': format('#BF1515'),
'string': format('#BF1515'),
'string2': format('#BF1515'),
'comment': format('#494949'),
'self': format('#222F57'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Royal" or str(sys.argv[1]) == "royal":
STYLES = {
'keyword': format('#00626a'),
'operator': format('#4D1675'),
'brace': format('#878787'),
'defclass': format('#e8b290'),
'string': format('#e8b290'),
'string2': format('#e8b290'),
'comment': format('#494949'),
'self': format('#00626a'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Scoops Ahoy" or str(sys.argv[1]) == "scoopsahoy":
STYLES = {
'keyword': format('#473B5F'),
'operator': format('#943B41'),
'brace': format('#878787'),
'defclass': format('#943B41'),
'string': format('#D2A89C'),
'string2': format('#D2A89C'),
'comment': format('#494949'),
'self': format('#473B5F'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Old Locomotive" or str(sys.argv[1]) == "oldlocomotive":
STYLES = {
'keyword': format('#916438'),
'operator': format('#878787'),
'brace': format('#878787'),
'defclass': format('#389138'),
'string': format('#389138'),
'string2': format('#389138'),
'comment': format('#6b6b6b'),
'self': format('#916438'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Green (Muted)" or str(sys.argv[1]) == "greenmuted":
STYLES = {
'keyword': format('#38898C'),
'operator': format('#878787'),
'brace': format('#878787'),
'defclass': format('#759F6F'),
'string': format('#759F6F'),
'string2': format('#759F6F'),
'comment': format('#6b6b6b'),
'self': format('#38898C'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Winter" or str(sys.argv[1]) == "winter":
STYLES = {
'keyword': format('#875F5F'),
'operator': format('#878787'),
'brace': format('#878787'),
'defclass': format('#5F5F87'),
'string': format('#38898C'),
'string2': format('#38898C'),
'comment': format('#6b6b6b'),
'self': format('#38898C'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "SpaCy" or str(sys.argv[1]) == "spacy":
STYLES = {
'keyword': format('#DB2C55'),
'operator': format('#878787'),
'brace': format('#878787'),
'defclass': format('#FFB86C'),
'string': format('#FFB86C'),
'string2': format('#FFB86C'),
'comment': format('#6b6b6b'),
'self': format('#DEDEDE'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Alabaster" or str(sys.argv[1]) == "alabaster":
STYLES = {
'keyword': format('#007765'),
'operator': format('#7A7A7A'),
'brace': format('#000000'),
'defclass': format('#378437'),
'string': format('#378437'),
'string2': format('#378437'),
'comment': format('#378437'),
'self': format('#000000'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "SpaceX" or str(sys.argv[1]) == "spacex":
STYLES = {
'keyword': format('#186298'),
'operator': format('#878787'),
'brace': format('#878787'),
'defclass': format('#287FB1'),
'string': format('#FF9210'),
'string2': format('#FF9210'),
'comment': format('#FF9210'),
'self': format('#DEDEDE'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "Forest" or str(sys.argv[1]) == "forest":
STYLES = {
'keyword': format('#379439'),
'operator': format('#BBC1A8'),
'brace': format('#8C8C8C'),
'defclass': format('#CC652D'),
'string': format('#CC652D'),
'string2': format('#CC652D'),
'comment': format('#8C8C8C'),
'self': format('#FFFFFF'),
'numbers': format('#8A8A8A'),
}
elif syntax_theme == "":
STYLES = {
'keyword': format('#2273A5'),
'operator': format('#DBD9D5'),
'brace': format('#DBD9D5'),
'defclass': format('#ccac55'),
'string': format('#77D479'),
'string2': format('#77D479'),
'comment': format('#8b939b'),
'self': format('#C37A5A'),
'numbers': format('#DBD9D5'),
}
except:
STYLES = {
'keyword': format('#2273A5'),
'operator': format('#DBD9D5'),
'brace': format('#DBD9D5'),
'defclass': format('#ccac55'),
'string': format('#77D479'),
'string2': format('#77D479'),
'comment': format('#8b939b'),
'self': format('#C37A5A'),
'numbers': format('#DBD9D5'),
}
if syntax_mode == "html":
keywords = [
'a', 'abbr', 'acronym', 'address', 'applet',
'area', 'article', 'aside', 'audio', 'b', 'base',
'basefont', 'bdi', 'bdo', 'bgsound', 'big', 'blink',
'blockquote', 'body', 'br', 'button', 'canvas', 'caption',
'center', 'cite', 'code', 'col', 'colgroup', 'command',
'content', 'data', 'datalist', 'dd', 'del', 'details',
'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'element',
'em', 'embed', 'fieldset', 'figcaption', 'figure', 'font',
'footer', 'form', 'frame', 'frameset', 'h1', 'head', 'header',
'hgroup', 'hr', 'html', 'i', 'iframe', 'image', 'img', 'input',
'ins', 'isindex', 'kbd', 'keygen', 'label', 'legend', 'li',
'link', 'listing', 'main', 'map', 'mark', 'marquee', 'menu',
'menuitem', 'meta', 'meter', 'multicol', 'nav', 'nextid', 'nobr',
'noembed', 'noframes', 'noscript', 'object', 'ol', 'optgroup',
'option', 'output', 'p', 'param', 'picture', 'plaintext', 'pre',
'progress', 'q', 'rb', 'rp', 'rt', 'rtc', 'ruby', 's', 'samp',
'script', 'section', 'select', 'shadow', 'slot', 'small', 'source',
'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary',
'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot',
'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul',
'var', 'video', 'wbr', 'xmp',
]
operators = [
# Arithmetic
'\+', '-', '\*', '/', '//', '\%', '\*\*',
]
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
elif syntax_mode == "js":
keywords = [
'abstract', 'arguments', 'await*', 'boolean', 'break', 'byte', 'case',
'catch', 'char', 'class*', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'double', 'else', 'enum*', 'evalexport*', 'extends*',
'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if',
'implements', 'import*', 'in', 'instanceof', 'int', 'interface', 'let*',
'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public',
'return', 'short', 'static', 'super*', 'switch', 'synchronized', 'this',
'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void',
'volatile', 'while', 'with', 'yield'
]
operators = [
# Arithmetic
'\+', '-', '\*', '/', '//', '\%', '\*\*',
]
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
elif syntax_mode == "cpp":
keywords = [
'__abstract', '__alignof', 'Operator', '__asm', '__assume', '__based',
'__box', '__cdecl', '__declspec', '__delegate', '__event', '__except',
'__fastcall', '__finally', '__forceinline', '__gc', '__hook', '__identifier',
'__if_exists', '__if_not_exists', '__inline', '__int16', '__int32', '__int64',
'__int8', '__interface', '__leave', '__m128', '__m128d', '__m128i', '__m64',
'__multiple_inheritance', '__nogc', '__noop', '__pin', '__property', '__ptr32',
'__ptr644', '__raise', '__restrict', '__sealed', '__single_inheritance4', '__sptr4',
'__stdcall', '__super', '__thiscall', '__try_cast', '__unaligned', '__unhook',
'__uptr', '__uuidof', '__value', '__vectorcall', '__virtual_inheritance', '__w64',
'__wchar_t', 'abstract', 'alignas', 'array', 'auto', 'bool', 'break', 'case',
'catch', 'char', 'char16_t', 'char32_t', 'class', 'const', 'const_cast', 'constexpr',
'continue', 'decltype', 'default', 'delegate', 'delete', 'deprecated', 'dllexport',
'dllimport', 'do', 'double', 'dynamic_cast', 'else', 'enum', 'enum', 'class', 'enum', 'include',
'struct', 'event', 'explicit', 'extern', 'false', 'finally', 'float', 'for', 'for',
'each', 'in', 'friend', 'friend_as', 'gcnew', 'generic', 'goto', 'if', 'initonly',
'inline', 'int', 'interface', 'class', 'interface', 'struct', 'interior_ptr', 'literal',
'long', 'mutable', 'naked', 'namespace', 'new', 'new', 'noexcept', 'noinline', 'noreturn',
'nothrow', 'novtable', 'nullptr', 'operator', 'private', 'property', 'property', 'protected',
'public', 'ref', 'class', 'ref', 'struct', 'register', 'reinterpret_cast', 'return',
'safecast', 'sealed', 'selectany', 'short', 'signed', 'sizeof', 'static', 'static_assert',
'static_cast', 'struct', 'string', 'switch', 'template', 'this', 'thread', 'throw', 'true', 'try',
'typedef', 'typeid', 'typeid', 'typename', 'union', 'unsigned', 'using', 'declaration', 'using',
'directive', 'uuid', 'value', 'class', 'value', 'struct', 'virtual', 'void', 'volatile', 'while'
]
operators = [
# Arithmetic
'\+', '-', '\*', '/', '//', '\%', '\*\*',
]
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
elif syntax_mode == "rs":
keywords = [
'_', 'abstract', 'alignof', 'as', 'become', 'box', 'break', 'const',
'continue', 'crate', 'do', 'else', 'enum', 'extern', 'false', 'final',
'fn', 'for', 'if', 'impl', 'in', 'let', 'loop', 'macro', 'match', 'mod',
'move', 'mut', 'offsetof', 'override', 'priv', 'proc', 'pub', 'pure',
'ref', 'return', 'Self', 'self', 'sizeof', 'static', 'struct', 'super',
'trait', 'true', 'type', 'typeof', 'unsafe', 'unsized', 'use', 'virtual',
'where', 'while', 'yield'
]
operators = [
# Arithmetic
'\+', '-', '\*', '/', '//', '\%', '\*\*',
]
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
# Python operators
elif syntax_mode in BoostSourceHighlighter.file_extensions:
# Python keywords
keywords = [
'and', 'assert', 'break', 'class', 'continue', 'def',
'del', 'elif', 'else', 'except', 'exec', 'finally',
'for', 'from', 'global', 'if', 'import', 'in',
'is', 'lambda', 'not', 'or', 'pass', 'print',
'raise', 'return', 'try', 'while', 'yield',
'None', 'True', 'False',
]
operators = [
'=',
# Comparison
'==', '!=', '<', '<=', '>', '>=',
# Arithmetic
'\+', '-', '\*', '/', '//', '\%', '\*\*',
# In-place
'\+=', '-=', '\*=', '/=', '\%=',
# Bitwise
'\^', '\|', '\&', '\~', '>>', '<<',
]
# Python braces
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
#Everything not a source file
else:
keywords = []
operators = []
braces = []
STYLES = {
'keyword': format('#DBD9D5'),
'operator': format('#DBD9D5'),
'brace': format('#DBD9D5'),
'defclass': format('#DBD9D5'),
'string': format('#DBD9D5'),
'string2': format('#DBD9D5'),
'comment': format('#DBD9D5'),
'self': format('#DBD9D5'),
'numbers': format('#DBD9D5'),
}
QSyntaxHighlighter.__init__(self, self.document)
# Multi-line strings (expression, flag, style)
# FIXME: The triple-quotes in these two lines will mess up the
# syntax highlighting from this point onward
self.tri_single = (QRegExp("'''"), 1, STYLES['comment'])
self.tri_double = (QRegExp('"""'), 2, STYLES['comment'])
highlight_rules = []
# Keyword, operator, and brace rules
highlight_rules += [(r'\b%s\b' % w, 0, STYLES['keyword'])
for w in keywords]
highlight_rules += [(r'%s' % o, 0, STYLES['operator'])
for o in operators]
highlight_rules += [(r'%s' % b, 0, STYLES['brace'])
for b in braces]
# All other rules
if str(syntax_mode) != "html" and str(syntax_mode) != "htm":
highlight_rules += [
# 'self'
(r'\bself\b', 0, STYLES['self']),
# 'def' followed by an identifier
(r'\bdef\b\s*(\w+)', 1, STYLES['defclass']),
# 'class' followed by an identifier
(r'\bclass\b\s*(\w+)', 1, STYLES['defclass']),
# Function call after . and before (
# (r'\.(?:[^\.]*(?=([(])))', 0, STYLES['function']),
# (r'\.[\w]+(?=\()', 0, STYLES['function']),
# Numeric literals
(r'\b[+-]?[0-9]+[lL]?\b', 0, STYLES['numbers']),
(r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, STYLES['numbers']),
(r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, STYLES['numbers']),
# From '//' until a newline
(r'//[^\n]*', 0, STYLES['comment']),
#Comments between /* and */
(r'\/\*(?:(?!\*)(?:.|[\r\n]+))*\*\/', 1, STYLES['comment']),
]
#Python comment
if str(syntax_mode) == "py":
highlight_rules += [
# From '#' until a newline
(r'#[^\n]*', 0, STYLES['comment']),
]
elif str(syntax_mode) == "html" or str(syntax_mode) == "htm":
highlight_rules += [
(r'\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>', 1, STYLES['comment']),
]
highlight_rules += [
# Double-quoted string, possibly containing escape sequences
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']),
# Single-quoted string, possibly containing escape sequences
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, STYLES['string']),
]
self.rules = [(QRegExp(pat), index, fmt)
for (pat, index, fmt) in highlight_rules]
def highlightBlock(self, text):
"""Apply syntax highlighting to the given block of text.
"""
# Do other syntax formatting
try:
for expression, nth, format in self.rules:
index = expression.indexIn(text, 0)
while index >= 0:
# We actually want the index of the nth match
index = expression.pos(nth)
length = expression.cap(nth).length()
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
self.setCurrentBlockState(0)
# Do multi-line strings
in_multiline = self.match_multiline(text, *self.tri_single)
if not in_multiline:
in_multiline = self.match_multiline(text, *self.tri_double)
except:
pass
def match_multiline(self, text, delimiter, in_state, style):
"""Do highlighting of multi-line strings. ``delimiter`` should be a
``QRegExp`` for triple-single-quotes or triple-double-quotes, and
``in_state`` should be a unique integer to represent the corresponding
state changes when inside those strings. Returns True if we're still
inside a multi-line string when this function is finished.
"""
# If inside triple-single quotes, start at 0
if self.previousBlockState() == in_state:
start = 0
add = 0
# Otherwise, look for the delimiter on this line
else:
start = delimiter.indexIn(text)
# Move past this match
add = delimiter.matchedLength()
# As long as there's a delimiter match on this line...
while start >= 0:
# Look for the ending delimiter
end = delimiter.indexIn(text, start + add)
# Ending delimiter on this line?
if end >= add:
length = end - start + add + delimiter.matchedLength()
self.setCurrentBlockState(0)
# No; multi-line string
else:
self.setCurrentBlockState(in_state)
length = text.length() - start + add
# Apply formatting
self.setFormat(start, length, style)
# Look for the next match
start = delimiter.indexIn(text, start + length)
# Return True if still inside a multi-line string, False otherwise
if self.currentBlockState() == in_state:
return True
else:
return False