-
Notifications
You must be signed in to change notification settings - Fork 4
/
AlifSyntax.py
110 lines (87 loc) · 3.18 KB
/
AlifSyntax.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
from PyQt6.QtCore import QRegularExpression
from PyQt6.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
blue = "#1ca3ff"
lightBlue = "#7fc5ff"
darkMagenta = "#f50f6f"
greenYellow = "#b1da00"
fireRed = "#ff5e4a"
gray = "#9a9a9a"
lightPurple = "#8845ca"
def format(color, style=''):
_color = QColor()
if type(color) is not str:
_color.setRgb(color[0], color[1], color[2])
else:
_color.setNamedColor(color)
_format = QTextCharFormat()
_format.setForeground(_color)
if 'black' in style:
_format.setFont(QFont("Tajawal Black"))
if 'bold' in style:
_format.setFontWeight(QFont.Weight.Bold)
if 'italic' in style:
_format.setFontItalic(True)
return _format
STYLES = {
'library': format(lightPurple, 'black'),
'keyword': format(blue, 'bold'),
'operator': format(lightBlue),
'brace': format(gray),
'string': format(greenYellow),
'comment': format(gray),
'numbers': format(fireRed),
'injlang': format(darkMagenta, 'italic')
}
class AlifHighlighter(QSyntaxHighlighter):
library = [
'ألف', 'مكتبة', 'نص'
]
keywords = [
'صنف', 'كائن', 'دالة', 'إذا', 'أو', 'وإلا', 'و', 'عدد', 'نص', 'اطبع',
'نافذة', 'كلما', 'نهاية', 'رئيسية', 'إرجاع'
]
operators = [
'=',
'==', '!=', '<', '<=', '>', '>=',
'\+', '-', '\*', '/', '//', '\%', '\*\*',
'\+=', '-=', '\*=', '/=', '\%=',
'\^', '\|', '\&', '\~', '>>', '<<',
]
braces = [
'\{', '\}', '\(', '\)', '\[', '\]',
]
injlang = [
'_ج_', '_ب_', '_س_'
]
def __init__(self, document):
super(AlifHighlighter, self).__init__(document)
rules = []
rules += [
(r' [^ء-ي ]*[+-]?[0-9]+|^[+-]?[0-9]+', 0, STYLES['numbers']),
(r' [^ء-ي ]*[+-]?[٠-٩]+|^[+-]?[٠-٩]+', 0, STYLES['numbers']),
(r'\b[+-]?0[xX][0-9A-Fa-f]+\b', 0, STYLES['numbers']),
]
rules += [('#%s($|[^\u0621-\u064A[^0-9]]*)' % l, 0, STYLES['library'])
for l in AlifHighlighter.library]
rules += [('(?:%s)(?![\w\u0621-\u064A])' % w, 0, STYLES['keyword'])
for w in AlifHighlighter.keywords]
rules += [('%s' % o, 0, STYLES['operator'])
for o in AlifHighlighter.operators]
rules += [('%s' % b, 0, STYLES['brace'])
for b in AlifHighlighter.braces]
rules += [('%s' % i, 0, STYLES['injlang'])
for i in AlifHighlighter.injlang]
rules += [
(r'--[^\n]*', 0, STYLES['comment']),
(r'"[^"\\]*(\\.[^"\\]*)*"', 0, STYLES['string']),
]
self.rules = [(QRegularExpression(pat), index, fmt)
for (pat, index, fmt) in rules]
def highlightBlock(self, text):
for expression, nth, format in self.rules:
match = expression.globalMatch(text, 0)
while match.hasNext():
matched = match.next()
index = matched.capturedStart()
length = matched.capturedLength()
self.setFormat(index, length, format)