forked from MarkTraceur/redlandside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynhigh.py
225 lines (177 loc) · 8.41 KB
/
synhigh.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
"""
Original software copyright 2010 Mark Holmquist and Logan May.
Some recent modifications copyright 2012 Mark Holmquist.
This file is part of redlandside.
redlandside is licensed under the GNU GPLv3 or later, please see the
COPYING file in this directory or http://www.gnu.org/licenses/gpl-3.0.html for
more information.
"""
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QColor, QFont, QSyntaxHighlighter, QTextCharFormat
def format(color, style='', bgcolor=''):
"""Return a QTextCharFormat with the given attributes.
"""
_color = QColor()
_color.setNamedColor(color)
_format = QTextCharFormat()
_format.setForeground(_color)
if 'bold' in style:
_format.setFontWeight(QFont.Bold)
if 'italic' in style:
_format.setFontItalic(True)
if bgcolor != '':
_color.setNamedColor(bgcolor)
_format.setBackground(_color)
return _format
class SyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, fileobj):
te = fileobj.parent.textedit
QSyntaxHighlighter.__init__(self, te.document())
self.styles = {
'keyword': format('red'),
'preproc': format('green'),
'brace': format('darkBlue'),
'defclass': format('black', 'bold'),
'literal': format('magenta'),
'comment': format('darkGreen', 'italic'),
'wsspace': format('black', bgcolor='blue'),
'wstab': format('black', bgcolor='red')
}
self.rules = []
rules = []
# C++
if fileobj.language == "C++":
kws = ['and','and_eq','asm','auto','bitand','bitor','bool',
'break','case','catch','char','class','compl','const',
'const_cast','continue','default','delete','do',
'double','dynamic_cast','else','enum','explicit',
'export','extern','false','float','for','friend',
'goto','if','inline','int','long','mutable',
'namespace','new','not','not_eq','operator','or',
'or_eq','private','protected','public','register',
'reinterpret_cast','return','short','signed','sizeof',
'static','static_cast','struct','switch','template',
'this','throw','true','try','typedef','typeid',
'typename','union','unsigned','using','virtual','void',
'volatile','wchar_t','while','xor','xor_eq']
rules += [(r'\b%s\b' % w, 0, self.styles['keyword']) for w in kws]
preprocs = ['#define','#error','#include','#line','#pragma',
'#undef','#if','#ifdef','#ifndef','#else','#elif',
'#endif']
rules += [(r'%s' % w, 0, self.styles['preproc']) for w in preprocs]
braces = ['\{','\}','\(','\)','\[','\]']
rules += [(r'%s' % w, 0, self.styles['brace']) for w in braces]
rules += [
# Double-quote strings
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['literal']),
# Single-quote strings
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['literal']),
# Numbers
(r'\b\d+\b', 0, self.styles['literal']),
# Comments
(r'\/\/[^\n]*', 0, self.styles['comment'])
]
# PYTHON
elif fileobj.language == "Python":
kws = ['and', 'del', 'for', 'is', 'raise', 'assert', 'elif',
'from', 'lambda', 'return', 'break', 'else', 'global',
'not', 'try', 'class', 'except', 'if', 'or', 'while',
'continue', 'exec', 'import', 'pass', 'yield', 'def',
'finally', 'in', 'print']
rules += [(r'\b%s\b' % w, 0, self.styles['keyword']) for w in kws]
preprocs = ['import', 'from' ]
rules += [(r'%s' % w, 0, self.styles['preproc']) for w in preprocs]
rules += [(r'def\:', 0, self.styles['defclass'])]
braces = ['\{','\}','\(','\)','\[','\]']
rules += [(r'%s' % w, 0, self.styles['brace']) for w in braces]
rules += [
# Double-quote strings
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['literal']),
# Single-quote strings
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['literal']),
# Numbers
(r'\b\d+\b', 0, self.styles['literal']),
# Comments
(r'\#[^\n]*', 0, self.styles['comment'])
]
# LOLCODE
elif fileobj.language == "LOLCODE":
kws = ['HAI', 'KTHXBYE', 'VISIBLE', 'GIMMEH', 'I HAS A',
'IM IN YR LOOP', 'IM OUTTA YR LOOP', 'IZ',
'BIGGER THAN', 'UP']
rules += [(r'\b%s\b' % w, 0, self.styles['keyword']) for w in kws]
preprocs = ['CAN HAS']
rules += [(r'%s' % w, 0, self.styles['preproc']) for w in preprocs]
rules += [(r'def\:', 0, self.styles['defclass'])]
braces = ['\{','\}','\(','\)','\[','\]']
rules += [(r'%s' % w, 0, self.styles['brace']) for w in braces]
rules += [
# Double-quote strings
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['literal']),
# Single-quote strings
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['literal']),
# Numbers
(r'\b\d+\b', 0, self.styles['literal']),
# Comments
(r'BTW[^\n]*', 0, self.styles['comment'])
]
# PROLOG
elif fileobj.language == "Prolog":
kws = ['block', 'dynamic', 'mode', 'module', 'multifile',
'meta_predicate', 'parallel', 'sequential', 'volatile']
rules += [(r'\b%s\b' % w, 0, self.styles['keyword']) for w in kws]
# For prolog we use the preproc style to highlight
# capitals for variables.
preprocs = [(r'\b[A-Z]+[a-zA-Z]*\b')]
rules += [(r'%s' % w, 0, self.styles['preproc']) for w in preprocs]
rules += [(r'def\:', 0, self.styles['defclass'])]
braces = ['\{','\}','\(','\)','\[','\]']
rules += [(r'%s' % w, 0, self.styles['brace']) for w in braces]
rules += [
# Double-quote strings
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['literal']),
# Single-quote strings
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['literal']),
# Numbers
(r'\b\d+\b', 0, self.styles['literal']),
# Comments
(r'\%[^\n]*', 0, self.styles['comment'])
]
# LISP
elif fileobj.language == "Lisp":
kws = ['car','cdr','setq','quote','eval','append','list','cons',
'atom','listp','null','memberp','nil','t','defun','abs',
'expt','sqrt','max','min','cond']
rules += [(r'\b%s\b' % w, 0, self.styles['keyword']) for w in kws]
braces = ['\(','\)']
rules += [(r'%s' % w, 0, self.styles['brace']) for w in braces]
rules += [
# Double-quote strings
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, self.styles['literal']),
# Single-quote strings
(r"'[^'\\]*(\\.[^'\\]*)*'", 0, self.styles['literal']),
# Numbers
(r'\b\d+\b', 0, self.styles['literal']),
# Comments
(r'\;[^\n]*', 0, self.styles['comment'])
]
# WHITESPACE
elif fileobj.language == "Whitespace":
# OK, we're gonna do some crazy stuff with this one.
# Most of it needs to be defined by hand.
rules += [
(r' ', 0, self.styles['wsspace']),
(r'\t', 0, self.styles['wstab'])
]
self.rules = [(QRegExp(p), i, f) for (p, i, f) in rules]
def highlightBlock(self, text):
# Do other syntax formatting
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 = len(expression.cap(nth))
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
self.setCurrentBlockState(0)