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
|
-- mod-version:3
--[[
language_php.lua
provides php syntax support allowing mixed html, css and js
version: 20220614_1
--]]
local syntax = require "core.syntax"
local common = require "core.common"
local config = require "core.config"
-- load syntax dependencies to add additional rules
require "plugins.language_css"
require "plugins.language_js"
local psql_found = pcall(require, "plugins.language_psql")
local sql_strings = {}
config.plugins.language_php = common.merge({
sql_strings = true,
-- The config specification used by the settings gui
config_spec = {
name = "Language PHP",
{
label = "SQL Strings",
description = "Highlight as SQL, strings starting with sql statements, "
.. "depends on language_psql.",
path = "sql_strings",
type = "toggle",
default = true,
on_apply = function(enabled)
local syntax_php = syntax.get("file.phps")
if enabled and psql_found then
if
not syntax_php.patterns[6].syntax
or
syntax_php.patterns[6].syntax ~= '.sql'
then
table.insert(syntax_php.patterns, 5, sql_strings[1])
table.insert(syntax_php.patterns, 6, sql_strings[2])
end
elseif
syntax_php.patterns[6].syntax
and
syntax_php.patterns[6].syntax == '.sql'
then
table.remove(syntax_php.patterns, 5)
table.remove(syntax_php.patterns, 5)
end
end
}
}
}, config.plugins.language_php)
-- Patterns to match some of the string inline variables
local inline_variables = {
{ pattern = "%s+", type = "string" },
{ pattern = "\\%$", type = "string" },
{ pattern = "%{[%$%s]*%}", type = "string" },
-- matches {$varname[index]}
{ pattern = "{"
.. "()%$[%a_][%w_]*"
.. "()%["
.. "()[%w%s_%-\"\'%(%)|;:,%.#@%%!%^&%*%+=%[%]<>~`%?\\/]*"
.. "()%]"
.. "}",
type = {
"keyword", "keyword2", "keyword", "string", "keyword"
}
},
{ pattern = "{"
.. "()%$[%a_][%w_]*"
.. "()%->"
.. "()[%a_][%w_]*"
.. "()}",
type = {
"keyword", "keyword2", "keyword", "symbol", "keyword"
}
},
{ pattern = "{()%$[%a_][%w_]*()}",
type = { "keyword", "keyword2", "keyword" }
},
{ pattern = "%$[%a_][%w_]*()%[()[%w_]*()%]",
type = { "keyword2", "keyword", "string", "keyword" }
},
{ pattern = "%$[%a_][%w_]*()%->()%a[%w_]*",
type = { "keyword2", "keyword", "symbol" }
},
{ pattern = "%$[%a_][%w_]*", type = "keyword2" },
{ pattern = "%w+", type = "string" }
}
local function combine_patterns(t1, t2)
local temp = { table.unpack(t1) }
for _, t in ipairs(t2) do
table.insert(temp, t)
end
return temp
end
local function clone(tbl)
local t = {}
if tbl then
for k, v in pairs(tbl) do
if type(v) == "table" then
t[k] = clone(v)
else
t[k] = v
end
end
end
return t
end
-- optionally allow sql syntax on strings
if psql_found then
-- generate SQL string marker regex
local sql_markers = { 'create', 'select', 'insert', 'update', 'replace', 'delete', 'drop', 'alter' }
local sql_regex = table.concat(sql_markers, '|')
-- inject inline variable rules to cloned psql syntax
local syntax_phpsql = clone(syntax.get("file.sql"))
syntax_phpsql.name = "PHP SQL"
syntax_phpsql.files = "%.phpsql$"
table.insert(syntax_phpsql.patterns, 2, { pattern = "\\%$", type = "symbol" })
table.insert(syntax_phpsql.patterns, 3, { pattern = "%{[%$%s]*%}", type = "symbol" })
for i=4, 9 do
table.insert(syntax_phpsql.patterns, i, inline_variables[i])
end
-- SQL strings
sql_strings = {
{
regex = { '"(?=[\\s(]*(?i:'..sql_regex..')\\s+)', '"', '\\' },
syntax = syntax_phpsql,
type = "string"
},
{
regex = { "'(?=[\\s(]*(?i:"..sql_regex..")\\s+)", '\'', '\\' },
syntax = '.sql',
type = "string"
},
}
end
-- define the core php syntax coloring
syntax.add {
name = "PHP Source",
files = { "%.phps$" },
headers = "^<%?php",
comment = "//",
block_comment = {"/*", "*/"},
patterns = {
-- Attributes
{ pattern = {"#%[", "%]"}, type = "normal" },
-- Comments
{ pattern = "//.-\n", type = "comment" },
{ pattern = "#.-\n", type = "comment" },
{ pattern = { "/%*", "%*/" }, type = "comment" },
-- Single quote string
{ pattern = { "'", "'", '\\' }, type = "string" },
{ pattern = { "<<<'%a%w*'\n", "^%s*%a%w*%f[;]", '\\' },
type = "string"
},
-- Strings with support for some inline variables syntax
{ pattern = { "<<<%a%w*\n", "^%s*%a%w*%f[;]", '\\' },
syntax = {
patterns = combine_patterns(inline_variables, {
-- prevent matching outside of the parent string
{ pattern = "^%s*%a%w*();$",
type = { "string", "normal" }
},
{ pattern = "%p", type = "string" },
}),
symbols = {}
},
type = "string"
},
{ pattern = { '"', '"', '\\' },
syntax = {
patterns = combine_patterns(inline_variables, {
-- prevent matching outside of the parent string
{ pattern = "[^\"]", type = "string" },
{ pattern = "%p+%f[\"]", type = "string" },
{ pattern = "%p", type = "string" },
}),
symbols = {}
},
type = "string"
},
{ pattern = { '`', '`', '\\' },
syntax = {
patterns = combine_patterns(inline_variables, {
-- prevent matching outside of the parent string
{ pattern = "[^`]", type = "string" },
{ pattern = "%p+%f[`]", type = "string" },
{ pattern = "%p", type = "string" },
}),
symbols = {}
},
type = "string"
},
{ pattern = "0[bB][%d]+", type = "number" },
{ pattern = "0[xX][%da-fA-F]+", type = "number" },
{ pattern = "-?%d[%d_%.eE]*", type = "number" },
{ pattern = "-?%.?%d+", type = "number" },
{ pattern = "[%.%+%-=/%*%^%%<>!~|&%?:@]", type = "operator" },
-- Variables
{ pattern = "%$[%w_]+", type = "keyword2" },
-- Respect control structures, treat as keyword not function
{ pattern = "if[%s]*%f[(]", type = "keyword" },
{ pattern = "else[%s]*%f[(]", type = "keyword" },
{ pattern = "elseif[%s]*%f[(]", type = "keyword" },
{ pattern = "for[%s]*%f[(]", type = "keyword" },
{ pattern = "foreach[%s]*%f[(]", type = "keyword" },
{ pattern = "while[%s]*%f[(]", type = "keyword" },
{ pattern = "catch[%s]*%f[(]", type = "keyword" },
{ pattern = "switch[%s]*%f[(]", type = "keyword" },
{ pattern = "match[%s]*%f[(]", type = "keyword" },
{ pattern = "fn[%s]*%f[(]", type = "keyword" },
-- All functions that aren't control structures
{ pattern = "[%a_][%w_]*[%s]*%f[(]", type = "function" },
-- Array type hint not added on symbols to also make it work
-- as a function call
{ pattern = "array", type = "literal" },
-- Match static or namespace container on sub element access
{ pattern = "[%a_][%w_]*[%s]*%f[:]", type = "literal" },
-- Uppercase constants of at least 2 chars in len
{
pattern = "%u[%u_][%u%d_]*%f[%s%+%*%-%.%(%)%?%^%%=/<>~|&;:,!]",
type = "number"
},
-- Magic constants
{ pattern = "__[%u]+__", type = "number" },
-- Everything else
{ pattern = "[%a_][%w_]*", type = "symbol" },
},
symbols = {
["return"] = "keyword",
["if"] = "keyword",
["else"] = "keyword",
["elseif"] = "keyword",
["endif"] = "keyword",
["declare"] = "keyword",
["enddeclare"] = "keyword",
["switch"] = "keyword",
["endswitch"] = "keyword",
["as"] = "keyword",
["do"] = "keyword",
["for"] = "keyword",
["endfor"] = "keyword",
["foreach"] = "keyword",
["endforeach"] = "keyword",
["while"] = "keyword",
["endwhile"] = "keyword",
["match"] = "keyword",
["case"] = "keyword",
["continue"] = "keyword",
["default"] = "keyword",
["break"] = "keyword",
["goto"] = "keyword",
["yield"] = "keyword",
["try"] = "keyword",
["catch"] = "keyword",
["throw"] = "keyword",
["finally"] = "keyword",
["class"] = "keyword",
["enum"] = "keyword",
["trait"] = "keyword",
["interface"] = "keyword",
["public"] = "keyword",
["static"] = "keyword",
["protected"] = "keyword",
["private"] = "keyword",
["readonly"] = "keyword",
["abstract"] = "keyword",
["final"] = "keyword",
["$this"] = "literal",
["function"] = "keyword",
["fn"] = "keyword",
["global"] = "keyword",
["var"] = "keyword",
["const"] = "keyword",
["bool"] = "literal",
["boolean"] = "literal",
["int"] = "literal",
["integer"] = "literal",
["real"] = "literal",
["double"] = "literal",
["float"] = "literal",
["string"] = "literal",
["object"] = "literal",
["callable"] = "literal",
["iterable"] = "literal",
["void"] = "literal",
["parent"] = "literal",
["self"] = "literal",
["mixed"] = "literal",
["never"] = "literal",
["namespace"] = "keyword",
["extends"] = "keyword",
["implements"] = "keyword",
["instanceof"] = "keyword",
["require"] = "keyword",
["require_once"] = "keyword",
["include"] = "keyword",
["include_once"] = "keyword",
["use"] = "keyword",
["new"] = "keyword",
["clone"] = "keyword",
["true"] = "number",
["false"] = "number",
["NULL"] = "number",
["null"] = "number",
["print"] = "function",
["echo"] = "function",
["exit"] = "function",
},
}
-- insert sql string rules after the "/%*", "%*/" pattern
if psql_found and config.plugins.language_php.sql_strings then
local syntax_php = syntax.get("file.phps")
table.insert(syntax_php.patterns, 5, sql_strings[1])
table.insert(syntax_php.patterns, 6, sql_strings[2])
end
-- allows html, css and js coloring on php files
syntax.add {
name = "PHP",
files = { "%.php$", "%.phtml" },
block_comment = { "<!--", "-->" },
patterns = {
{
regex = {
"<\\?php\\s+",
"(?:\\?>|(?=`{3}))" -- end if inside markdown code tags
},
syntax = ".phps",
type = "keyword2"
},
{
pattern = {
"<%?=?",
"%?>"
},
syntax = ".phps",
type = "keyword2"
},
{
pattern = {
"<%s*[sS][cC][rR][iI][pP][tT]%f[%s>].->",
"<%s*/%s*[sS][cC][rR][iI][pP][tT]%s*>"
},
syntax = ".js",
type = "function"
},
{
pattern = {
"<%s*[sS][tT][yY][lL][eE]%f[%s>].->",
"<%s*/%s*[sS][tT][yY][lL][eE]%s*>"
},
syntax = ".css",
type = "function"
},
{ pattern = { "<!%-%-", "%-%->" }, type = "comment" },
{ pattern = { '%f[^>][^<]', '%f[<]' }, type = "normal" },
{ pattern = { '"', '"', '\\' }, type = "string" },
{ pattern = { "'", "'", '\\' }, type = "string" },
{ pattern = "0x[%da-fA-F]+", type = "number" },
{ pattern = "-?%d+[%d%.]*f?", type = "number" },
{ pattern = "-?%.?%d+f?", type = "number" },
{ pattern = "%f[^<]![%a_][%w_]*", type = "keyword2" },
{ pattern = "%f[^<][%a_][%w_]*", type = "function" },
{ pattern = "%f[^<]/[%a_][%w_]*", type = "function" },
{ pattern = "[%a_][%w_]*", type = "keyword" },
{ pattern = "[/<>=]", type = "operator" },
-- match markdown code tags to be able to end php highlighting
-- when inside the subsyntax .phps
{ regex = "(?=`{3})", type = "string" }
},
symbols = {},
}
-- allow coloring of php code inside css and js code
local syntaxes = { "css", "js" }
for _, ext in pairs(syntaxes) do
local syntax_table = syntax.get("file."..ext, "")
table.insert(
syntax_table.patterns,
1,
{
pattern = {
"<%?=?",
"%?>"
},
syntax = ".phps",
type = "keyword2"
}
)
table.insert(
syntax_table.patterns,
1,
{
pattern = {
"<%?php%s+",
"%?>"
},
syntax = ".phps",
type = "keyword2"
}
)
end
|