Skip to content

Commit df922d9

Browse files
authored
PHP: Added support for PHP 8.0 features (#2591)
1 parent f59a85f commit df922d9

11 files changed

+817
-86
lines changed

components/prism-php.js

Lines changed: 166 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
* Modified by Miles Johnson: http://milesj.me
44
* Rewritten by Tom Pavelec
55
*
6-
* Supports PHP 5.3 - 7.4
6+
* Supports PHP 5.3 - 8.0
77
*/
88
(function (Prism) {
9+
var comment = /\/\*[\s\S]*?\*\/|\/\/.*|#(?!\[).*/;
10+
var constant = [
11+
{
12+
pattern: /\b(?:false|true)\b/i,
13+
alias: 'boolean'
14+
},
15+
/\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/,
16+
/\b(?:null)\b/i,
17+
];
18+
var number = /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)*|\B\.\d+)(?:e[+-]?\d+)?/i;
19+
var operator = /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/;
20+
var punctuation = /[{}\[\](),:;]/;
21+
922
Prism.languages.php = {
1023
'delimiter': {
1124
pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
1225
alias: 'important'
1326
},
14-
'comment': [
15-
/\/\*[\s\S]*?\*\/|\/\/.*|#.*/
16-
],
27+
'comment': comment,
1728
'variable': /\$+(?:\w+\b|(?={))/i,
1829
'package': {
1930
pattern: /(namespace\s+|use\s+(?:function\s+)?)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
@@ -30,37 +41,82 @@
3041
lookbehind: true
3142
},
3243
{
33-
pattern: /([(,?]\s*)\b(?:bool|boolean|int|integer|float|string|object|array(?!\s*\()|mixed|self|static|callable|iterable)\b(?=\s*\$)/i,
44+
pattern: /([(,?]\s*)\b(?:bool|int|float|string|object|array(?!\s*\()|mixed|self|static|callable|iterable|(?:null|false)(?=\s*\|))\b(?=\s*\$)/i,
3445
alias: 'type-hint',
3546
greedy: true,
3647
lookbehind: true
3748
},
3849
{
39-
pattern: /(\)\s*:\s*\?*\s*)\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed|self|static|callable|iterable)\b/i,
50+
pattern: /([(,?]\s*[a-z0-9_|]\|\s*)(?:null|false)\b(?=\s*\$)/i,
51+
alias: 'type-hint',
52+
greedy: true,
53+
lookbehind: true
54+
},
55+
{
56+
pattern: /(\)\s*:\s*\??\s*)\b(?:bool|int|float|string|object|void|array(?!\s*\()|mixed|self|static|callable|iterable|(?:null|false)(?=\s*\|))\b/i,
4057
alias: 'return-type',
4158
greedy: true,
4259
lookbehind: true
4360
},
4461
{
45-
pattern: /\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed|iterable)\b/i,
62+
pattern: /(\)\s*:\s*\??\s*[a-z0-9_|]\|\s*)(?:null|false)\b/i,
63+
alias: 'return-type',
64+
greedy: true,
65+
lookbehind: true
66+
},
67+
{
68+
pattern: /\b(?:bool|int|float|string|object|void|array(?!\s*\()|mixed|iterable|(?:null|false)(?=\s*\|))\b/i,
4669
alias: 'type-declaration',
4770
greedy: true
4871
},
72+
{
73+
pattern: /(\|\s*)(?:null|false)\b/i,
74+
alias: 'type-declaration',
75+
greedy: true,
76+
lookbehind: true
77+
},
4978
{
5079
pattern: /\b(?:parent|self|static)(?=\s*::)/i,
5180
alias: 'static-context',
5281
greedy: true
5382
},
5483
/\b(?:__halt_compiler|abstract|and|array|as|break|callable|case|catch|class|clone|const|continue|declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends|final|finally|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset|list|namespace|match|new|or|parent|print|private|protected|public|require|require_once|return|self|static|switch|throw|trait|try|unset|use|var|while|xor|yield)\b/i
5584
],
85+
'argument-name': /\b[a-z_]\w*(?=\s*:(?!:))/i,
5686
'class-name': [
5787
{
58-
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s+\()\b[a-z_]\w*(?!\\)\b/i,
88+
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
89+
greedy: true,
90+
lookbehind: true
91+
},
92+
{
93+
pattern: /(\|\s*)\b[a-z_]\w*(?!\\)\b/i,
5994
greedy: true,
6095
lookbehind: true
6196
},
6297
{
63-
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self\b|\s+static\b))\s+|\bcatch\s+\()(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
98+
pattern: /\b[a-z_]\w*(?!\\)\b(?=\s*\|)/i,
99+
greedy: true
100+
},
101+
{
102+
pattern: /(\|\s*)(?:\\?\b[a-z_]\w*)+\b/i,
103+
alias: 'class-name-fully-qualified',
104+
greedy: true,
105+
lookbehind: true,
106+
inside: {
107+
'punctuation': /\\/
108+
}
109+
},
110+
{
111+
pattern: /(?:\\?\b[a-z_]\w*)+\b(?=\s*\|)/i,
112+
alias: 'class-name-fully-qualified',
113+
greedy: true,
114+
inside: {
115+
'punctuation': /\\/
116+
}
117+
},
118+
{
119+
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self\b|\s+static\b))\s+|\bcatch\s*\()(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
64120
alias: 'class-name-fully-qualified',
65121
greedy: true,
66122
lookbehind: true,
@@ -110,13 +166,13 @@
110166
}
111167
},
112168
{
113-
pattern: /(\)\s*:\s*\?*\s*)\b[a-z_]\w*(?!\\)\b/i,
169+
pattern: /(\)\s*:\s*\??\s*)\b[a-z_]\w*(?!\\)\b/i,
114170
alias: 'return-type',
115171
greedy: true,
116172
lookbehind: true
117173
},
118174
{
119-
pattern: /(\)\s*:\s*\?*\s*)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
175+
pattern: /(\)\s*:\s*\??\s*)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
120176
alias: ['class-name-fully-qualified', 'return-type'],
121177
greedy: true,
122178
lookbehind: true,
@@ -125,22 +181,15 @@
125181
}
126182
}
127183
],
128-
'constant': [
129-
{
130-
pattern: /\b(?:false|true)\b/i,
131-
alias: 'boolean'
132-
},
133-
/\b[A-Z_][A-Z0-9_]*\b/,
134-
/\b(?:null)\b/i,
135-
],
184+
'constant': constant,
136185
'function': /\w+\s*(?=\()/,
137186
'property': {
138187
pattern: /(->)[\w]+/,
139188
lookbehind: true
140189
},
141-
'number': /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)*|\B\.\d+)(?:e[+-]?\d+)?/i,
142-
'operator': /<?=>|\?\?=?|\.{3}|->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||[?~]|[/^|%*&<>.+-]=?/,
143-
'punctuation': /[{}\[\](),:;]/
190+
'number': number,
191+
'operator': operator,
192+
'punctuation': punctuation
144193
};
145194

146195
var string_interpolation = {
@@ -149,64 +198,112 @@
149198
inside: Prism.languages.php
150199
};
151200

152-
Prism.languages.insertBefore('php', 'variable', {
153-
'string': [
154-
{
155-
pattern: /<<<'([^']+)'[\r\n](?:.*[\r\n])*?\1;/,
156-
alias: 'nowdoc-string',
157-
greedy: true,
158-
inside: {
159-
'delimiter': {
160-
pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
161-
alias: 'symbol',
162-
inside: {
163-
'punctuation': /^<<<'?|[';]$/
164-
}
201+
var string = [
202+
{
203+
pattern: /<<<'([^']+)'[\r\n](?:.*[\r\n])*?\1;/,
204+
alias: 'nowdoc-string',
205+
greedy: true,
206+
inside: {
207+
'delimiter': {
208+
pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
209+
alias: 'symbol',
210+
inside: {
211+
'punctuation': /^<<<'?|[';]$/
165212
}
166213
}
167-
},
168-
{
169-
pattern: /<<<(?:"([^"]+)"[\r\n](?:.*[\r\n])*?\1;|([a-z_]\w*)[\r\n](?:.*[\r\n])*?\2;)/i,
170-
alias: 'heredoc-string',
171-
greedy: true,
172-
inside: {
173-
'delimiter': {
174-
pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
175-
alias: 'symbol',
176-
inside: {
177-
'punctuation': /^<<<"?|[";]$/
178-
}
179-
},
180-
'interpolation': string_interpolation // See below
181-
}
182-
},
183-
{
184-
pattern: /`(?:\\[\s\S]|[^\\`])*`/,
185-
alias: 'backtick-quoted-string',
186-
greedy: true
187-
},
188-
{
189-
pattern: /'(?:\\[\s\S]|[^\\'])*'/,
190-
alias: 'single-quoted-string',
191-
greedy: true
192-
},
193-
{
194-
pattern: /"(?:\\[\s\S]|[^\\"])*"/,
195-
alias: 'double-quoted-string',
196-
greedy: true,
197-
inside: {
198-
'interpolation': string_interpolation // See below
214+
}
215+
},
216+
{
217+
pattern: /<<<(?:"([^"]+)"[\r\n](?:.*[\r\n])*?\1;|([a-z_]\w*)[\r\n](?:.*[\r\n])*?\2;)/i,
218+
alias: 'heredoc-string',
219+
greedy: true,
220+
inside: {
221+
'delimiter': {
222+
pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
223+
alias: 'symbol',
224+
inside: {
225+
'punctuation': /^<<<"?|[";]$/
226+
}
227+
},
228+
'interpolation': string_interpolation // See below
229+
}
230+
},
231+
{
232+
pattern: /`(?:\\[\s\S]|[^\\`])*`/,
233+
alias: 'backtick-quoted-string',
234+
greedy: true
235+
},
236+
{
237+
pattern: /'(?:\\[\s\S]|[^\\'])*'/,
238+
alias: 'single-quoted-string',
239+
greedy: true
240+
},
241+
{
242+
pattern: /"(?:\\[\s\S]|[^\\"])*"/,
243+
alias: 'double-quoted-string',
244+
greedy: true,
245+
inside: {
246+
'interpolation': string_interpolation // See below
247+
}
248+
}
249+
];
250+
251+
Prism.languages.insertBefore('php', 'variable', {
252+
'string': string,
253+
});
254+
255+
Prism.languages.insertBefore('php', 'variable', {
256+
'attribute': {
257+
pattern: /#\[(?:[^"'\/#]|\/(?![*/])|\/\/.*$|#(?!\[).*$|\/\*(?:[^*]|\*(?!\/))*\*\/|"(?:\\[\s\S]|[^\\"])*"|'(?:\\[\s\S]|[^\\'])*')+\](?=\s*[a-z$#])/mi,
258+
greedy: true,
259+
inside: {
260+
'attribute-content': {
261+
pattern: /^(#\[)[\s\S]+(?=]$)/,
262+
lookbehind: true,
263+
// inside can appear subset of php
264+
inside: {
265+
'comment': comment,
266+
'string': string,
267+
'attribute-class-name': [
268+
{
269+
pattern: /([^:]|^)\b[a-z_]\w*(?!\\)\b/i,
270+
alias: 'class-name',
271+
greedy: true,
272+
lookbehind: true
273+
},
274+
{
275+
pattern: /([^:]|^)(?:\\?\b[a-z_]\w*)+/i,
276+
alias: [
277+
'class-name',
278+
'class-name-fully-qualified'
279+
],
280+
greedy: true,
281+
lookbehind: true,
282+
inside: {
283+
'punctuation': /\\/
284+
}
285+
}
286+
],
287+
'constant': constant,
288+
'number': number,
289+
'operator': operator,
290+
'punctuation': punctuation
291+
}
292+
},
293+
'delimiter': {
294+
pattern: /^#\[|]$/,
295+
alias: 'punctuation'
199296
}
200297
}
201-
],
298+
},
202299
});
203300

204301
Prism.hooks.add('before-tokenize', function(env) {
205302
if (!/<\?/.test(env.code)) {
206303
return;
207304
}
208305

209-
var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#)(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|\/\*[\s\S]*?(?:\*\/|$))*?(?:\?>|$)/ig;
306+
var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#(?!\[))(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|#\[|\/\*[\s\S]*?(?:\*\/|$))*?(?:\?>|$)/ig;
210307
Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
211308
});
212309

components/prism-php.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy