-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathphp.js
616 lines (599 loc) · 13.8 KB
/
php.js
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
Language: PHP
Author: Victor Karamzin <[email protected]>
Contributors: Evgeny Stepanischev <[email protected]>, Ivan Sagalaev <[email protected]>
Website: https://www.php.net
Category: common
*/
/**
* @param {HLJSApi} hljs
* @returns {LanguageDetail}
* */
export default function(hljs) {
const regex = hljs.regex;
// negative look-ahead tries to avoid matching patterns that are not
// Perl at all like $ident$, @ident@, etc.
const NOT_PERL_ETC = /(?![A-Za-z0-9])(?![$])/;
const IDENT_RE = regex.concat(
/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/,
NOT_PERL_ETC);
// Will not detect camelCase classes
const PASCAL_CASE_CLASS_NAME_RE = regex.concat(
/(\\?[A-Z][a-z0-9_\x7f-\xff]+|\\?[A-Z]+(?=[A-Z][a-z0-9_\x7f-\xff])){1,}/,
NOT_PERL_ETC);
const VARIABLE = {
scope: 'variable',
match: '\\$+' + IDENT_RE,
};
const PREPROCESSOR = {
scope: 'meta',
variants: [
{ begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
{ begin: /<\?=/ },
// less relevant per PSR-1 which says not to use short-tags
{ begin: /<\?/, relevance: 0.1 },
{ begin: /\?>/ } // end php tag
]
};
const SUBST = {
scope: 'subst',
variants: [
{ begin: /\$\w+/ },
{
begin: /\{\$/,
end: /\}/
}
]
};
const SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, { illegal: null, });
const DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
illegal: null,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
});
const HEREDOC = {
begin: /<<<[ \t]*(?:(\w+)|"(\w+)")\n/,
end: /[ \t]*(\w+)\b/,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
'on:begin': (m, resp) => { resp.data._beginMatch = m[1] || m[2]; },
'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch(); },
};
const NOWDOC = hljs.END_SAME_AS_BEGIN({
begin: /<<<[ \t]*'(\w+)'\n/,
end: /[ \t]*(\w+)\b/,
});
// list of valid whitespaces because non-breaking space might be part of a IDENT_RE
const WHITESPACE = '[ \t\n]';
const STRING = {
scope: 'string',
variants: [
DOUBLE_QUOTED,
SINGLE_QUOTED,
HEREDOC,
NOWDOC
]
};
const NUMBER = {
scope: 'number',
variants: [
{ begin: `\\b0[bB][01]+(?:_[01]+)*\\b` }, // Binary w/ underscore support
{ begin: `\\b0[oO][0-7]+(?:_[0-7]+)*\\b` }, // Octals w/ underscore support
{ begin: `\\b0[xX][\\da-fA-F]+(?:_[\\da-fA-F]+)*\\b` }, // Hex w/ underscore support
// Decimals w/ underscore support, with optional fragments and scientific exponent (e) suffix.
{ begin: `(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:[eE][+-]?\\d+)?` }
],
relevance: 0
};
const LITERALS = [
"false",
"null",
"true"
];
const KWS = [
// Magic constants:
// <https://www.php.net/manual/en/language.constants.predefined.php>
"__CLASS__",
"__DIR__",
"__FILE__",
"__FUNCTION__",
"__COMPILER_HALT_OFFSET__",
"__LINE__",
"__METHOD__",
"__NAMESPACE__",
"__TRAIT__",
// Function that look like language construct or language construct that look like function:
// List of keywords that may not require parenthesis
"die",
"echo",
"exit",
"include",
"include_once",
"print",
"require",
"require_once",
// These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
// 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
// Other keywords:
// <https://www.php.net/manual/en/reserved.php>
// <https://www.php.net/manual/en/language.types.type-juggling.php>
"array",
"abstract",
"and",
"as",
"binary",
"bool",
"boolean",
"break",
"callable",
"case",
"catch",
"class",
"clone",
"const",
"continue",
"declare",
"default",
"do",
"double",
"else",
"elseif",
"empty",
"enddeclare",
"endfor",
"endforeach",
"endif",
"endswitch",
"endwhile",
"enum",
"eval",
"extends",
"final",
"finally",
"float",
"for",
"foreach",
"from",
"global",
"goto",
"if",
"implements",
"instanceof",
"insteadof",
"int",
"integer",
"interface",
"isset",
"iterable",
"list",
"match|0",
"mixed",
"new",
"never",
"object",
"or",
"private",
"protected",
"public",
"readonly",
"real",
"return",
"string",
"switch",
"throw",
"trait",
"try",
"unset",
"use",
"var",
"void",
"while",
"xor",
"yield"
];
const BUILT_INS = [
// Standard PHP library:
// <https://www.php.net/manual/en/book.spl.php>
"Error|0",
"AppendIterator",
"ArgumentCountError",
"ArithmeticError",
"ArrayIterator",
"ArrayObject",
"AssertionError",
"BadFunctionCallException",
"BadMethodCallException",
"CachingIterator",
"CallbackFilterIterator",
"CompileError",
"Countable",
"DirectoryIterator",
"DivisionByZeroError",
"DomainException",
"EmptyIterator",
"ErrorException",
"Exception",
"FilesystemIterator",
"FilterIterator",
"GlobIterator",
"InfiniteIterator",
"InvalidArgumentException",
"IteratorIterator",
"LengthException",
"LimitIterator",
"LogicException",
"MultipleIterator",
"NoRewindIterator",
"OutOfBoundsException",
"OutOfRangeException",
"OuterIterator",
"OverflowException",
"ParentIterator",
"ParseError",
"RangeException",
"RecursiveArrayIterator",
"RecursiveCachingIterator",
"RecursiveCallbackFilterIterator",
"RecursiveDirectoryIterator",
"RecursiveFilterIterator",
"RecursiveIterator",
"RecursiveIteratorIterator",
"RecursiveRegexIterator",
"RecursiveTreeIterator",
"RegexIterator",
"RuntimeException",
"SeekableIterator",
"SplDoublyLinkedList",
"SplFileInfo",
"SplFileObject",
"SplFixedArray",
"SplHeap",
"SplMaxHeap",
"SplMinHeap",
"SplObjectStorage",
"SplObserver",
"SplPriorityQueue",
"SplQueue",
"SplStack",
"SplSubject",
"SplTempFileObject",
"TypeError",
"UnderflowException",
"UnexpectedValueException",
"UnhandledMatchError",
// Reserved interfaces:
// <https://www.php.net/manual/en/reserved.interfaces.php>
"ArrayAccess",
"BackedEnum",
"Closure",
"Fiber",
"Generator",
"Iterator",
"IteratorAggregate",
"Serializable",
"Stringable",
"Throwable",
"Traversable",
"UnitEnum",
"WeakReference",
"WeakMap",
// Reserved classes:
// <https://www.php.net/manual/en/reserved.classes.php>
"Directory",
"__PHP_Incomplete_Class",
"parent",
"php_user_filter",
"self",
"static",
"stdClass"
];
/** Dual-case keywords
*
* ["then","FILE"] =>
* ["then", "THEN", "FILE", "file"]
*
* @param {string[]} items */
const dualCase = (items) => {
/** @type string[] */
const result = [];
items.forEach(item => {
result.push(item);
if (item.toLowerCase() === item) {
result.push(item.toUpperCase());
} else {
result.push(item.toLowerCase());
}
});
return result;
};
const KEYWORDS = {
keyword: KWS,
literal: dualCase(LITERALS),
built_in: BUILT_INS,
};
/**
* @param {string[]} items */
const normalizeKeywords = (items) => {
return items.map(item => {
return item.replace(/\|\d+$/, "");
});
};
const CONSTRUCTOR_CALL = { variants: [
{
match: [
/new/,
regex.concat(WHITESPACE, "+"),
// to prevent built ins from being confused as the class constructor call
regex.concat("(?!", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
PASCAL_CASE_CLASS_NAME_RE,
],
scope: {
1: "keyword",
4: "title.class",
},
}
] };
const CONSTANT_REFERENCE = regex.concat(IDENT_RE, "\\b(?!\\()");
const LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON = { variants: [
{
match: [
regex.concat(
/::/,
regex.lookahead(/(?!class\b)/)
),
CONSTANT_REFERENCE,
],
scope: { 2: "variable.constant", },
},
{
match: [
/::/,
/class/,
],
scope: { 2: "variable.language", },
},
{
match: [
PASCAL_CASE_CLASS_NAME_RE,
regex.concat(
/::/,
regex.lookahead(/(?!class\b)/)
),
CONSTANT_REFERENCE,
],
scope: {
1: "title.class",
3: "variable.constant",
},
},
{
match: [
PASCAL_CASE_CLASS_NAME_RE,
regex.concat(
"::",
regex.lookahead(/(?!class\b)/)
),
],
scope: { 1: "title.class", },
},
{
match: [
PASCAL_CASE_CLASS_NAME_RE,
/::/,
/class/,
],
scope: {
1: "title.class",
3: "variable.language",
},
}
] };
const NAMED_ARGUMENT = {
scope: 'attr',
match: regex.concat(IDENT_RE, regex.lookahead(':'), regex.lookahead(/(?!::)/)),
};
const FUNCTION_DECLARATION = {
scope: 'function',
relevance: 0,
beginKeywords: 'fn function',
end: /[;{]/,
excludeEnd: true,
illegal: '[$%\\[]',
contains: [
{ beginKeywords: 'use', },
hljs.UNDERSCORE_TITLE_MODE,
{
begin: '=>', // No markup, just a relevance booster
endsParent: true
},
{
scope: 'params',
begin: '\\(',
end: '\\)',
excludeBegin: true,
excludeEnd: true,
keywords: KEYWORDS,
contains: [
'self',
VARIABLE,
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
hljs.C_BLOCK_COMMENT_MODE,
STRING,
NUMBER
]
},
]
};
const PARAMS_MODE = {
relevance: 0,
begin: /\(/,
end: /\)/,
keywords: KEYWORDS,
contains: [
NAMED_ARGUMENT,
VARIABLE,
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
hljs.C_BLOCK_COMMENT_MODE,
STRING,
NUMBER,
CONSTRUCTOR_CALL,
FUNCTION_DECLARATION,
],
};
const FUNCTION_INVOKE = {
relevance: 0,
match: [
/\b/,
// to prevent keywords from being confused as the function title
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
IDENT_RE,
regex.concat(WHITESPACE, "*"),
regex.lookahead(/(?=\()/)
],
scope: { 3: "title.function.invoke", },
contains: [ PARAMS_MODE ]
};
PARAMS_MODE.contains.push(FUNCTION_INVOKE);
const ATTRIBUTE_CONTAINS = [
NAMED_ARGUMENT,
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
hljs.C_BLOCK_COMMENT_MODE,
STRING,
NUMBER,
CONSTRUCTOR_CALL,
];
const ATTRIBUTES = {
begin: regex.concat(/#\[\s*/, PASCAL_CASE_CLASS_NAME_RE),
beginScope: "meta",
end: /]/,
endScope: "meta",
keywords: {
literal: LITERALS,
keyword: [
'new',
'array',
]
},
contains: [
{
begin: /\[/,
end: /]/,
keywords: {
literal: LITERALS,
keyword: [
'new',
'array',
]
},
contains: [
'self',
...ATTRIBUTE_CONTAINS,
]
},
...ATTRIBUTE_CONTAINS,
{
scope: 'meta',
match: PASCAL_CASE_CLASS_NAME_RE
}
]
};
return {
case_insensitive: false,
keywords: KEYWORDS,
contains: [
ATTRIBUTES,
hljs.HASH_COMMENT_MODE,
hljs.COMMENT('//', '$'),
hljs.COMMENT(
'/\\*',
'\\*/',
{ contains: [
{
scope: 'doctag',
match: '@[A-Za-z]+'
}
] }
),
{
match: /__halt_compiler\(\);/,
keywords: '__halt_compiler',
starts: {
scope: "comment",
end: hljs.MATCH_NOTHING_RE,
contains: [
{
match: /\?>/,
scope: "meta",
endsParent: true
}
]
}
},
PREPROCESSOR,
{
scope: 'variable.language',
match: /\$this\b/
},
VARIABLE,
FUNCTION_INVOKE,
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
{
match: [
/const/,
/\s/,
IDENT_RE,
],
scope: {
1: "keyword",
3: "variable.constant",
},
},
CONSTRUCTOR_CALL,
FUNCTION_DECLARATION,
{
scope: 'class',
variants: [
{
beginKeywords: "enum",
illegal: /[($"]/
},
{
beginKeywords: "class interface trait",
illegal: /[:($"]/
}
],
relevance: 0,
end: /\{/,
excludeEnd: true,
contains: [
{ beginKeywords: 'extends implements' },
hljs.UNDERSCORE_TITLE_MODE
]
},
// both use and namespace still use "old style" rules (vs multi-match)
// because the namespace name can include `\` and we still want each
// element to be treated as its own *individual* title
{
beginKeywords: 'namespace',
relevance: 0,
end: ';',
illegal: /[.']/,
contains: [ hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, { scope: "title.class" }) ]
},
{
beginKeywords: 'use',
relevance: 0,
end: ';',
contains: [
// TODO: title.function vs title.class
{
match: /\b(as|const|function)\b/,
scope: "keyword"
},
// TODO: could be title.class or title.function
hljs.UNDERSCORE_TITLE_MODE
]
},
STRING,
NUMBER,
]
};
}