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
|
-- mod-version:3
local syntax = require "core.syntax"
local function table_merge(a, b)
local t = {}
for _, v in pairs(a) do table.insert(t, v) end
for _, v in pairs(b) do table.insert(t, v) end
return t
end
local python_symbols = {
["class"] = "keyword",
["finally"] = "keyword",
["is"] = "keyword",
["return"] = "keyword",
["continue"] = "keyword",
["for"] = "keyword",
["lambda"] = "keyword",
["try"] = "keyword",
["except"] = "keyword",
["def"] = "keyword",
["async"] = "keyword",
["await"] = "keyword",
["from"] = "keyword",
["nonlocal"] = "keyword",
["while"] = "keyword",
["and"] = "keyword",
["global"] = "keyword",
["not"] = "keyword",
["with"] = "keyword",
["as"] = "keyword",
["elif"] = "keyword",
["if"] = "keyword",
["or"] = "keyword",
["else"] = "keyword",
["match"] = "keyword",
["case"] = "keyword",
["import"] = "keyword",
["pass"] = "keyword",
["break"] = "keyword",
["in"] = "keyword",
["del"] = "keyword",
["raise"] = "keyword",
["yield"] = "keyword",
["assert"] = "keyword",
["self"] = "keyword2",
["None"] = "literal",
["True"] = "literal",
["False"] = "literal",
}
local python_fstring = {
patterns = {
{ pattern = "\\.", type = "string" },
{ pattern = '[^"\\{}\']+', type = "string" }
},
symbols = {}
}
local python_patterns = {
{ pattern = '[uUrR]%f["]', type = "keyword" },
{ pattern = { '[ruU]?"""', '"""', '\\' }, type = "string" },
{ pattern = { "[ruU]?'''", "'''", '\\' }, type = "string" },
{ pattern = { '[ruU]?"', '"', '\\' }, type = "string" },
{ pattern = { "[ruU]?'", "'", '\\' }, type = "string" },
{ pattern = { 'f"', '"', "\\" }, type = "string", syntax = python_fstring },
{ pattern = { "f'", "'", "\\" }, type = "string", syntax = python_fstring },
{ pattern = "%d+[%d%.eE_]*", type = "number" },
{ pattern = "0[xboXBO][%da-fA-F_]+", type = "number" },
{ pattern = "%.?%d+", type = "number" },
{ pattern = "%f[-%w_]-%f[%d%.]", type = "number" },
{ pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" },
{ pattern = "[%a_][%w_]*%f[(]", type = "function" },
{ pattern = "[%a_][%w_]+", type = "symbol" },
}
local python_type = {
patterns = {
{ pattern = "|", type = "operator" },
{ pattern = "[%w_]+", type = "keyword2" },
{ pattern = "[%a_][%w_]+", type = "symbol" },
},
symbols = {
["None"] = "literal"
}
}
-- Add this line after in order for the recursion to work.
-- Makes sure that the square brackets are well balanced when capturing the syntax
-- (in order to make something like this work: Tuple[Tuple[int, str], float])
table.insert(python_type.patterns, 1, { pattern = { "%[", "%]" }, syntax = python_type })
-- For things like this_list = other_list[a:b:c]
local not_python_type = {
patterns = python_patterns,
symbols = python_symbols
}
table.insert(not_python_type.patterns, 1, { pattern = { "%[", "%]" }, syntax = not_python_type })
table.insert(not_python_type.patterns, 1, { pattern = { "{", "}" }, syntax = not_python_type })
table.insert(python_fstring.patterns, 1, { pattern = { "{", "}" }, syntax = not_python_type })
local python_func = {
patterns = table_merge({
{ pattern = { "->", "%f[:]" }, type = "operator", syntax = python_type },
{ pattern = { ":%s*", "%f[^%[%]%w_]" }, syntax = python_type },
}, python_patterns),
symbols = python_symbols
}
table.insert(python_func.patterns, 1, { pattern = { "%(", "%)" }, syntax = python_func })
syntax.add {
name = "Python",
files = { "%.py$", "%.pyw$", "%.rpy$", "%.pyi$" },
headers = "^#!.*[ /]python",
comment = "#",
block_comment = { '"""', '"""' },
patterns = table_merge({
{ pattern = "#.*", type = "comment" },
{ pattern = { '^%s*"""', '"""' }, type = "comment" },
{ pattern = { "%[", "%]" }, syntax = not_python_type },
{ pattern = { "{", "}" }, syntax = not_python_type },
{ pattern = { "^%s*()def%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = python_func }, -- this and the following prevent one-liner highlight bugs
{ pattern = { "^%s*()for%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()if%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()elif%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()while%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()match%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()case%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = { "^%s*()except%f[%s]", ":" }, type = { "normal", "keyword" }, syntax = not_python_type },
{ pattern = "else():", type = { "keyword", "normal" } },
{ pattern = "try():", type = { "keyword", "normal" } },
{ pattern = "lambda()%s.+:", type = { "keyword", "normal" } },
{ pattern = "class%s+()[%a_][%w_]+().*:", type = { "keyword", "keyword2", "normal" } },
{ pattern = { ":%s*", "%f[^%[%]%w_]"}, syntax = python_type },
}, python_patterns),
symbols = python_symbols
}
|