-
Notifications
You must be signed in to change notification settings - Fork 6
/
highlighter.py
90 lines (87 loc) · 5.54 KB
/
highlighter.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
from PySide6 import QtCore
import re
from PySide6.QtCore import Qt
from PySide6.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QColor
from binaryninja import *
from binaryninjaui import getThemeColor
class Highlighter(QSyntaxHighlighter):
def __init__(self,doc,selected,args):
super(Highlighter,self).__init__(doc)
self.selected = selected
self.args = args
def highlightBlock(self, text):
# Highlight keywords
keywords = ["(?<=\\W\\()\\w+(?=[ *&]*\\))","\\w+ (?=.*\\)$)","^ +\\w+ (?=[*&_A-Za-z0-9\\[\\] ]+;)","^\w+","\\bin_addr\\b","\\bssize_t\\b","\\bsocklen_t\\b","\\bsa_family_t\\b","\\b__int32_t\\b","\\b__int8_t\\b","\\b__int16_t\\b","\\b__uint32_t\\b","\\b__uint8_t\\b","\\b__uint16_t\\b","\\bpid_t\\b","\\bcode\\b","\\bLPSTR\\b","\\bSIZE_T\\b","\\bLPVOID\\b","\\bDWORD\\b","\\bclock_t\\b","\\bthis\\b","\\bUINT\\b","\\bHANDLE\\b","\\blonglong\\b","\\bushort\\b","\\bFILE\\b","\\bulong\\b","\\bbyte\\b","\\bfalse\\b","\\btrue\\b","\\buint\\b","\\bsize_t\\b","\\bundefined\\d*\\b","\\bchar\\b", "\\bclass\\b", "\\bconst\\b", "\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b","\\bfriend\\b", "\\binline\\b", "\\bint\\b","\\blong\\b", "\\bnamespace\\b", "\\boperator\\b","\\bprivate\\b", "\\bprotected\\b", "\\bpublic\\b","\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b","\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b","\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b","\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b","\\bvoid\\b", "\\bvolatile\\b", "\\bbool\\b"]
keyword_format = QTextCharFormat()
keyword_format.setForeground(getThemeColor(enums.ThemeColor.KeywordColor))
for keyword in keywords:
for match in re.finditer(keyword, text):
self.setFormat(match.start(), match.end() - match.start(), keyword_format)
# Highlight flow words
flow_words = ["\\breturn\\b","\\bif\\b","\\belse\\b","\\bswitch\\b","\\bcase\\b","\\bwhile\\b","\\bfor\\b","\\bdo\\b","\\bgoto\\b"]
flow_format = QTextCharFormat()
flow_format.setForeground(getThemeColor(enums.ThemeColor.TokenHighlightColor))
for flow in flow_words:
for match in re.finditer(flow, text):
self.setFormat(match.start(), match.end() - match.start(), flow_format)
# Highlight functions
function_format = QTextCharFormat()
function_format.setForeground(getThemeColor(enums.ThemeColor.CodeSymbolColor))
function_pattern = "\\b\\w+(?=\\()"
for match in re.finditer(function_pattern, text):
self.setFormat(match.start(), match.end() - match.start(), function_format)
# Highlight comments
comment_format = QTextCharFormat()
comment_format.setForeground(getThemeColor(enums.ThemeColor.CommentColor))
comment_pattern = "\/\/.*$"
for match in re.finditer(comment_pattern, text):
self.setFormat(match.start(), match.end() - match.start(), comment_format)
multi_comment_pattern = "(?s)\\/\\*.*?\\*\\/"
for match in re.finditer(multi_comment_pattern, text):
self.setFormat(match.start(), match.end() - match.start(), comment_format)
# Highlight string constants
const_format = QTextCharFormat()
const_format.setForeground(getThemeColor(enums.ThemeColor.StringColor))
string_consts = "\"(.*?)\""
for match in re.finditer(string_consts, text):
self.setFormat(match.start(), match.end() - match.start(), const_format)
# Highlight numeric constants
num_const_format = QTextCharFormat()
num_const_format.setForeground(getThemeColor(enums.ThemeColor.NumberColor))
num_consts = "\\b\\d+\\b"
for match in re.finditer(num_consts, text):
self.setFormat(match.start(), match.end() - match.start(), num_const_format)
hex_const = "0x[0-9a-f]+\\b"
for match in re.finditer(hex_const, text):
self.setFormat(match.start(), match.end() - match.start(), num_const_format)
# Highlight data
data_format = QTextCharFormat()
data_format.setForeground(getThemeColor(enums.ThemeColor.DataSymbolColor))
data_consts = "\\b(PTR)?_?DAT_[0-9a-zA-Z]+\\b"
for match in re.finditer(data_consts, text):
self.setFormat(match.start(), match.end() - match.start(), data_format)
# Highlight CPP Class paths
cpp_format = QTextCharFormat()
cpp_format.setForeground(getThemeColor(enums.ThemeColor.NameSpaceColor))
cpp_path = "\\b\\w*(?=::)"
for match in re.finditer(cpp_path, text):
self.setFormat(match.start(), match.end() - match.start(), cpp_format)
# Params
params_format = QTextCharFormat()
params_format.setForeground(getThemeColor(enums.ThemeColor.FieldNameColor))
for arg in self.args:
params_pattern = "\\b" + arg + "\\b"
for match in re.finditer(params_pattern, text):
self.setFormat(match.start(), match.end() - match.start(), params_format)
# Highlight selection
if self.selected:
selection_format = QTextCharFormat()
#selection_format.setBackground(getThemeColor(enums.ThemeColor.Highlight))
selection_format.setBackground(QColor.fromRgb(121,195,231))
selection_format.setForeground(QColor.fromRgb(42,42,42))
try:
selection_pattern = self.selected
for match in re.finditer(selection_pattern, text):
self.setFormat(match.start(), match.end() - match.start(), selection_format)
except:
pass