Skip to content

Commit 7e8cd40

Browse files
PHP: Fixed methods with keyword names + minor improvements (#2818)
1 parent 31cc214 commit 7e8cd40

8 files changed

+703
-364
lines changed

components/prism-php.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
lookbehind: true,
4949
alias: 'class-name'
5050
},
51+
'function-definition': {
52+
pattern: /(\bfunction\s+)[a-z_]\w*(?=\s*\()/i,
53+
lookbehind: true,
54+
alias: 'function'
55+
},
5156
'keyword': [
5257
{
5358
pattern: /(\(\s*)\b(?:bool|boolean|int|integer|float|string|object|array)\b(?=\s*\))/i,
@@ -95,7 +100,21 @@
95100
alias: 'static-context',
96101
greedy: true
97102
},
98-
/\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|enum|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
103+
{
104+
// yield from
105+
pattern: /(\byield\s+)from\b/i,
106+
lookbehind: true
107+
},
108+
// `class` is always a keyword unlike other keywords
109+
/\bclass\b/i,
110+
{
111+
// https://www.php.net/manual/en/reserved.keywords.php
112+
//
113+
// keywords cannot be preceded by "->"
114+
// the complex lookbehind means `(?<!(?:->|::)\s*)`
115+
pattern: /((?:^|[^\s>:]|(?:^|[^-])>|(?:^|[^:]):)\s*)\b(?:__halt_compiler|abstract|and|array|as|break|callable|case|catch|clone|const|continue|declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|enum|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,
116+
lookbehind: true
117+
}
99118
],
100119
'argument-name': {
101120
pattern: /([(,]\s+)\b[a-z_]\w*(?=\s*:(?!:))/i,
@@ -200,9 +219,9 @@
200219
}
201220
],
202221
'constant': constant,
203-
'function': /\w+\s*(?=\()/,
222+
'function': /\b\w+(?=\s*\()/,
204223
'property': {
205-
pattern: /(->)[\w]+/,
224+
pattern: /(->\s*)\w+/,
206225
lookbehind: true
207226
},
208227
'number': number,
@@ -243,7 +262,7 @@
243262
'punctuation': /^<<<"?|[";]$/
244263
}
245264
},
246-
'interpolation': string_interpolation // See below
265+
'interpolation': string_interpolation
247266
}
248267
},
249268
{
@@ -261,16 +280,13 @@
261280
alias: 'double-quoted-string',
262281
greedy: true,
263282
inside: {
264-
'interpolation': string_interpolation // See below
283+
'interpolation': string_interpolation
265284
}
266285
}
267286
];
268287

269288
Prism.languages.insertBefore('php', 'variable', {
270289
'string': string,
271-
});
272-
273-
Prism.languages.insertBefore('php', 'variable', {
274290
'attribute': {
275291
pattern: /#\[(?:[^"'\/#]|\/(?![*/])|\/\/.*$|#(?!\[).*$|\/\*(?:[^*]|\*(?!\/))*\*\/|"(?:\\[\s\S]|[^\\"])*"|'(?:\\[\s\S]|[^\\'])*')+\](?=\s*[a-z$#])/mi,
276292
greedy: true,
@@ -316,7 +332,7 @@
316332
},
317333
});
318334

319-
Prism.hooks.add('before-tokenize', function(env) {
335+
Prism.hooks.add('before-tokenize', function (env) {
320336
if (!/<\?/.test(env.code)) {
321337
return;
322338
}
@@ -325,7 +341,7 @@
325341
Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
326342
});
327343

328-
Prism.hooks.add('after-tokenize', function(env) {
344+
Prism.hooks.add('after-tokenize', function (env) {
329345
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
330346
});
331347

components/prism-php.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)