/
php.js
283 lines (282 loc) · 8.81 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
/*
Language: PHP
Author: Victor Karamzin <Victor.Karamzin@enterra-inc.com>
Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Ivan Sagalaev <maniac@softwaremaniacs.org>
Website: https://www.php.net
Category: common
*/
/**
* @param {HLJSApi} hljs
* @returns {LanguageDetail}
* */
export default function(hljs) {
const regex = hljs.regex;
const VARIABLE = {
scope: 'variable',
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
// negative look-ahead tries to avoid matching patterns that are not
// Perl at all like $ident$, @ident@, etc.
`(?![A-Za-z0-9])(?![$])`
};
const CONSTANT = {
variants: [
{
begin: [
/const\s+/,
/\w+/,
/\s*=/,
],
beginScope: {
1: "keyword",
2: "variable",
},
},
{
begin: [
regex.concat(
/::/,
regex.lookahead(/(?!class\b)/)
),
/(\w+\b(?!\())/
],
beginScope: {
2: "variable",
},
},
{
begin: [
regex.concat(
/[A-Z]\w*/,
/::/,
regex.lookahead(/(?!class\b)/)
),
/(\w+\b(?!\())/,
],
beginScope: {
1: "title.class",
2: "variable",
},
},
{
begin: [
/::/,
/class/,
],
beginScope: {
2: "keyword",
},
},
{
begin: [
/[A-Z]\w*/,
/::/,
/class/,
],
beginScope: {
1: "title.class",
3: "keyword",
},
}
]
};
const CONSTRUCTOR = {
variants: [
{
begin: [
/new/,
/\s+/,
/\\?[A-Z]\w*/,
/\s*\(/,
],
beginScope: {
1: "keyword",
3: "title.class",
},
}
]
};
const PREPROCESSOR = {
scope: 'meta',
variants: [
{ begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
{ begin: /<\?[=]?/ },
{ 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 = hljs.END_SAME_AS_BEGIN({
begin: /<<<[ \t]*(\w+)\n/,
end: /[ \t]*(\w+)\b/,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
});
const STRING = {
scope: 'string',
contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
variants: [
hljs.inherit(SINGLE_QUOTED, {
begin: "b'", end: "'",
}),
hljs.inherit(DOUBLE_QUOTED, {
begin: 'b"', end: '"',
}),
DOUBLE_QUOTED,
SINGLE_QUOTED,
HEREDOC
]
};
const NUMBER = {
scope: 'number',
variants: [
{ begin: `\\b0b[01]+(?:_[01]+)*\\b` }, // Binary w/ underscore support
{ begin: `\\b0o[0-7]+(?:_[0-7]+)*\\b` }, // Octals w/ underscore support
{ begin: `\\b0x[\\da-f]+(?:_[\\da-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+)(?:e[+-]?\\d+)?` }
],
relevance: 0
};
const KEYWORDS = {
keyword:
// Magic constants:
// <https://www.php.net/manual/en/language.constants.predefined.php>
'__CLASS__ __DIR__ __FILE__ __FUNCTION__ __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',
literal: 'false null true',
built_in:
// Standard PHP library:
// <https://www.php.net/manual/en/book.spl.php>
'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive
'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 SplObserver SplPriorityQueue SplQueue SplStack SplSubject 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',
};
return {
case_insensitive: true,
keywords: KEYWORDS,
contains: [
hljs.HASH_COMMENT_MODE,
hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
hljs.COMMENT(
'/\\*',
'\\*/',
{
contains: [
{
scope: 'doctag',
begin: '@[A-Za-z]+'
}
]
}
),
hljs.COMMENT(
'__halt_compiler.+?;',
false,
{
endsWithParent: true,
keywords: '__halt_compiler'
}
),
PREPROCESSOR,
{
scope: 'keyword', begin: /\$this\b/
},
VARIABLE,
CONSTANT,
CONSTRUCTOR,
{
// swallow composed identifiers to avoid parsing them as keywords
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
},
{
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,
CONSTANT,
CONSTRUCTOR,
hljs.C_BLOCK_COMMENT_MODE,
STRING,
NUMBER
]
}
]
},
{
scope: 'class',
variants: [
{ beginKeywords: "enum", illegal: /[($"]/ },
{ beginKeywords: "class interface trait", illegal: /[:($"]/ }
],
relevance: 0,
end: /\{/,
excludeEnd: true,
contains: [
{beginKeywords: 'extends implements'},
hljs.UNDERSCORE_TITLE_MODE
]
},
{
beginKeywords: 'namespace',
relevance: 0,
end: ';',
illegal: /[.']/,
contains: [hljs.UNDERSCORE_TITLE_MODE]
},
{
beginKeywords: 'use',
relevance: 0,
end: ';',
contains: [hljs.UNDERSCORE_TITLE_MODE]
},
STRING,
NUMBER
]
};
}