1 | 'use strict'
|
---|
2 | var refractorMarkupTemplating = require('./markup-templating.js')
|
---|
3 | module.exports = php
|
---|
4 | php.displayName = 'php'
|
---|
5 | php.aliases = []
|
---|
6 | function php(Prism) {
|
---|
7 | Prism.register(refractorMarkupTemplating)
|
---|
8 | /**
|
---|
9 | * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/
|
---|
10 | * Modified by Miles Johnson: http://milesj.me
|
---|
11 | * Rewritten by Tom Pavelec
|
---|
12 | *
|
---|
13 | * Supports PHP 5.3 - 8.0
|
---|
14 | */
|
---|
15 | ;(function (Prism) {
|
---|
16 | var comment = /\/\*[\s\S]*?\*\/|\/\/.*|#(?!\[).*/
|
---|
17 | var constant = [
|
---|
18 | {
|
---|
19 | pattern: /\b(?:false|true)\b/i,
|
---|
20 | alias: 'boolean'
|
---|
21 | },
|
---|
22 | {
|
---|
23 | pattern: /(::\s*)\b[a-z_]\w*\b(?!\s*\()/i,
|
---|
24 | greedy: true,
|
---|
25 | lookbehind: true
|
---|
26 | },
|
---|
27 | {
|
---|
28 | pattern: /(\b(?:case|const)\s+)\b[a-z_]\w*(?=\s*[;=])/i,
|
---|
29 | greedy: true,
|
---|
30 | lookbehind: true
|
---|
31 | },
|
---|
32 | /\b(?:null)\b/i,
|
---|
33 | /\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/
|
---|
34 | ]
|
---|
35 | var number =
|
---|
36 | /\b0b[01]+(?:_[01]+)*\b|\b0o[0-7]+(?:_[0-7]+)*\b|\b0x[\da-f]+(?:_[\da-f]+)*\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)?|\B\.\d+)(?:e[+-]?\d+)?/i
|
---|
37 | var operator =
|
---|
38 | /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/
|
---|
39 | var punctuation = /[{}\[\](),:;]/
|
---|
40 | Prism.languages.php = {
|
---|
41 | delimiter: {
|
---|
42 | pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
|
---|
43 | alias: 'important'
|
---|
44 | },
|
---|
45 | comment: comment,
|
---|
46 | variable: /\$+(?:\w+\b|(?=\{))/,
|
---|
47 | package: {
|
---|
48 | pattern:
|
---|
49 | /(namespace\s+|use\s+(?:function\s+)?)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
|
---|
50 | lookbehind: true,
|
---|
51 | inside: {
|
---|
52 | punctuation: /\\/
|
---|
53 | }
|
---|
54 | },
|
---|
55 | 'class-name-definition': {
|
---|
56 | pattern: /(\b(?:class|enum|interface|trait)\s+)\b[a-z_]\w*(?!\\)\b/i,
|
---|
57 | lookbehind: true,
|
---|
58 | alias: 'class-name'
|
---|
59 | },
|
---|
60 | 'function-definition': {
|
---|
61 | pattern: /(\bfunction\s+)[a-z_]\w*(?=\s*\()/i,
|
---|
62 | lookbehind: true,
|
---|
63 | alias: 'function'
|
---|
64 | },
|
---|
65 | keyword: [
|
---|
66 | {
|
---|
67 | pattern:
|
---|
68 | /(\(\s*)\b(?:array|bool|boolean|float|int|integer|object|string)\b(?=\s*\))/i,
|
---|
69 | alias: 'type-casting',
|
---|
70 | greedy: true,
|
---|
71 | lookbehind: true
|
---|
72 | },
|
---|
73 | {
|
---|
74 | pattern:
|
---|
75 | /([(,?]\s*)\b(?:array(?!\s*\()|bool|callable|(?:false|null)(?=\s*\|)|float|int|iterable|mixed|object|self|static|string)\b(?=\s*\$)/i,
|
---|
76 | alias: 'type-hint',
|
---|
77 | greedy: true,
|
---|
78 | lookbehind: true
|
---|
79 | },
|
---|
80 | {
|
---|
81 | pattern:
|
---|
82 | /(\)\s*:\s*(?:\?\s*)?)\b(?:array(?!\s*\()|bool|callable|(?:false|null)(?=\s*\|)|float|int|iterable|mixed|object|self|static|string|void)\b/i,
|
---|
83 | alias: 'return-type',
|
---|
84 | greedy: true,
|
---|
85 | lookbehind: true
|
---|
86 | },
|
---|
87 | {
|
---|
88 | pattern:
|
---|
89 | /\b(?:array(?!\s*\()|bool|float|int|iterable|mixed|object|string|void)\b/i,
|
---|
90 | alias: 'type-declaration',
|
---|
91 | greedy: true
|
---|
92 | },
|
---|
93 | {
|
---|
94 | pattern: /(\|\s*)(?:false|null)\b|\b(?:false|null)(?=\s*\|)/i,
|
---|
95 | alias: 'type-declaration',
|
---|
96 | greedy: true,
|
---|
97 | lookbehind: true
|
---|
98 | },
|
---|
99 | {
|
---|
100 | pattern: /\b(?:parent|self|static)(?=\s*::)/i,
|
---|
101 | alias: 'static-context',
|
---|
102 | greedy: true
|
---|
103 | },
|
---|
104 | {
|
---|
105 | // yield from
|
---|
106 | pattern: /(\byield\s+)from\b/i,
|
---|
107 | lookbehind: true
|
---|
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:
|
---|
116 | /((?:^|[^\s>:]|(?:^|[^-])>|(?:^|[^:]):)\s*)\b(?: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|fn|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset|list|match|namespace|new|or|parent|print|private|protected|public|require|require_once|return|self|static|switch|throw|trait|try|unset|use|var|while|xor|yield|__halt_compiler)\b/i,
|
---|
117 | lookbehind: true
|
---|
118 | }
|
---|
119 | ],
|
---|
120 | 'argument-name': {
|
---|
121 | pattern: /([(,]\s+)\b[a-z_]\w*(?=\s*:(?!:))/i,
|
---|
122 | lookbehind: true
|
---|
123 | },
|
---|
124 | 'class-name': [
|
---|
125 | {
|
---|
126 | pattern:
|
---|
127 | /(\b(?:extends|implements|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
|
---|
128 | greedy: true,
|
---|
129 | lookbehind: true
|
---|
130 | },
|
---|
131 | {
|
---|
132 | pattern: /(\|\s*)\b[a-z_]\w*(?!\\)\b/i,
|
---|
133 | greedy: true,
|
---|
134 | lookbehind: true
|
---|
135 | },
|
---|
136 | {
|
---|
137 | pattern: /\b[a-z_]\w*(?!\\)\b(?=\s*\|)/i,
|
---|
138 | greedy: true
|
---|
139 | },
|
---|
140 | {
|
---|
141 | pattern: /(\|\s*)(?:\\?\b[a-z_]\w*)+\b/i,
|
---|
142 | alias: 'class-name-fully-qualified',
|
---|
143 | greedy: true,
|
---|
144 | lookbehind: true,
|
---|
145 | inside: {
|
---|
146 | punctuation: /\\/
|
---|
147 | }
|
---|
148 | },
|
---|
149 | {
|
---|
150 | pattern: /(?:\\?\b[a-z_]\w*)+\b(?=\s*\|)/i,
|
---|
151 | alias: 'class-name-fully-qualified',
|
---|
152 | greedy: true,
|
---|
153 | inside: {
|
---|
154 | punctuation: /\\/
|
---|
155 | }
|
---|
156 | },
|
---|
157 | {
|
---|
158 | pattern:
|
---|
159 | /(\b(?:extends|implements|instanceof|new(?!\s+self\b|\s+static\b))\s+|\bcatch\s*\()(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
|
---|
160 | alias: 'class-name-fully-qualified',
|
---|
161 | greedy: true,
|
---|
162 | lookbehind: true,
|
---|
163 | inside: {
|
---|
164 | punctuation: /\\/
|
---|
165 | }
|
---|
166 | },
|
---|
167 | {
|
---|
168 | pattern: /\b[a-z_]\w*(?=\s*\$)/i,
|
---|
169 | alias: 'type-declaration',
|
---|
170 | greedy: true
|
---|
171 | },
|
---|
172 | {
|
---|
173 | pattern: /(?:\\?\b[a-z_]\w*)+(?=\s*\$)/i,
|
---|
174 | alias: ['class-name-fully-qualified', 'type-declaration'],
|
---|
175 | greedy: true,
|
---|
176 | inside: {
|
---|
177 | punctuation: /\\/
|
---|
178 | }
|
---|
179 | },
|
---|
180 | {
|
---|
181 | pattern: /\b[a-z_]\w*(?=\s*::)/i,
|
---|
182 | alias: 'static-context',
|
---|
183 | greedy: true
|
---|
184 | },
|
---|
185 | {
|
---|
186 | pattern: /(?:\\?\b[a-z_]\w*)+(?=\s*::)/i,
|
---|
187 | alias: ['class-name-fully-qualified', 'static-context'],
|
---|
188 | greedy: true,
|
---|
189 | inside: {
|
---|
190 | punctuation: /\\/
|
---|
191 | }
|
---|
192 | },
|
---|
193 | {
|
---|
194 | pattern: /([(,?]\s*)[a-z_]\w*(?=\s*\$)/i,
|
---|
195 | alias: 'type-hint',
|
---|
196 | greedy: true,
|
---|
197 | lookbehind: true
|
---|
198 | },
|
---|
199 | {
|
---|
200 | pattern: /([(,?]\s*)(?:\\?\b[a-z_]\w*)+(?=\s*\$)/i,
|
---|
201 | alias: ['class-name-fully-qualified', 'type-hint'],
|
---|
202 | greedy: true,
|
---|
203 | lookbehind: true,
|
---|
204 | inside: {
|
---|
205 | punctuation: /\\/
|
---|
206 | }
|
---|
207 | },
|
---|
208 | {
|
---|
209 | pattern: /(\)\s*:\s*(?:\?\s*)?)\b[a-z_]\w*(?!\\)\b/i,
|
---|
210 | alias: 'return-type',
|
---|
211 | greedy: true,
|
---|
212 | lookbehind: true
|
---|
213 | },
|
---|
214 | {
|
---|
215 | pattern: /(\)\s*:\s*(?:\?\s*)?)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
|
---|
216 | alias: ['class-name-fully-qualified', 'return-type'],
|
---|
217 | greedy: true,
|
---|
218 | lookbehind: true,
|
---|
219 | inside: {
|
---|
220 | punctuation: /\\/
|
---|
221 | }
|
---|
222 | }
|
---|
223 | ],
|
---|
224 | constant: constant,
|
---|
225 | function: {
|
---|
226 | pattern: /(^|[^\\\w])\\?[a-z_](?:[\w\\]*\w)?(?=\s*\()/i,
|
---|
227 | lookbehind: true,
|
---|
228 | inside: {
|
---|
229 | punctuation: /\\/
|
---|
230 | }
|
---|
231 | },
|
---|
232 | property: {
|
---|
233 | pattern: /(->\s*)\w+/,
|
---|
234 | lookbehind: true
|
---|
235 | },
|
---|
236 | number: number,
|
---|
237 | operator: operator,
|
---|
238 | punctuation: punctuation
|
---|
239 | }
|
---|
240 | var string_interpolation = {
|
---|
241 | pattern:
|
---|
242 | /\{\$(?:\{(?:\{[^{}]+\}|[^{}]+)\}|[^{}])+\}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)?)/,
|
---|
243 | lookbehind: true,
|
---|
244 | inside: Prism.languages.php
|
---|
245 | }
|
---|
246 | var string = [
|
---|
247 | {
|
---|
248 | pattern: /<<<'([^']+)'[\r\n](?:.*[\r\n])*?\1;/,
|
---|
249 | alias: 'nowdoc-string',
|
---|
250 | greedy: true,
|
---|
251 | inside: {
|
---|
252 | delimiter: {
|
---|
253 | pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
|
---|
254 | alias: 'symbol',
|
---|
255 | inside: {
|
---|
256 | punctuation: /^<<<'?|[';]$/
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | },
|
---|
261 | {
|
---|
262 | pattern:
|
---|
263 | /<<<(?:"([^"]+)"[\r\n](?:.*[\r\n])*?\1;|([a-z_]\w*)[\r\n](?:.*[\r\n])*?\2;)/i,
|
---|
264 | alias: 'heredoc-string',
|
---|
265 | greedy: true,
|
---|
266 | inside: {
|
---|
267 | delimiter: {
|
---|
268 | pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
|
---|
269 | alias: 'symbol',
|
---|
270 | inside: {
|
---|
271 | punctuation: /^<<<"?|[";]$/
|
---|
272 | }
|
---|
273 | },
|
---|
274 | interpolation: string_interpolation
|
---|
275 | }
|
---|
276 | },
|
---|
277 | {
|
---|
278 | pattern: /`(?:\\[\s\S]|[^\\`])*`/,
|
---|
279 | alias: 'backtick-quoted-string',
|
---|
280 | greedy: true
|
---|
281 | },
|
---|
282 | {
|
---|
283 | pattern: /'(?:\\[\s\S]|[^\\'])*'/,
|
---|
284 | alias: 'single-quoted-string',
|
---|
285 | greedy: true
|
---|
286 | },
|
---|
287 | {
|
---|
288 | pattern: /"(?:\\[\s\S]|[^\\"])*"/,
|
---|
289 | alias: 'double-quoted-string',
|
---|
290 | greedy: true,
|
---|
291 | inside: {
|
---|
292 | interpolation: string_interpolation
|
---|
293 | }
|
---|
294 | }
|
---|
295 | ]
|
---|
296 | Prism.languages.insertBefore('php', 'variable', {
|
---|
297 | string: string,
|
---|
298 | attribute: {
|
---|
299 | pattern:
|
---|
300 | /#\[(?:[^"'\/#]|\/(?![*/])|\/\/.*$|#(?!\[).*$|\/\*(?:[^*]|\*(?!\/))*\*\/|"(?:\\[\s\S]|[^\\"])*"|'(?:\\[\s\S]|[^\\'])*')+\](?=\s*[a-z$#])/im,
|
---|
301 | greedy: true,
|
---|
302 | inside: {
|
---|
303 | 'attribute-content': {
|
---|
304 | pattern: /^(#\[)[\s\S]+(?=\]$)/,
|
---|
305 | lookbehind: true,
|
---|
306 | // inside can appear subset of php
|
---|
307 | inside: {
|
---|
308 | comment: comment,
|
---|
309 | string: string,
|
---|
310 | 'attribute-class-name': [
|
---|
311 | {
|
---|
312 | pattern: /([^:]|^)\b[a-z_]\w*(?!\\)\b/i,
|
---|
313 | alias: 'class-name',
|
---|
314 | greedy: true,
|
---|
315 | lookbehind: true
|
---|
316 | },
|
---|
317 | {
|
---|
318 | pattern: /([^:]|^)(?:\\?\b[a-z_]\w*)+/i,
|
---|
319 | alias: ['class-name', 'class-name-fully-qualified'],
|
---|
320 | greedy: true,
|
---|
321 | lookbehind: true,
|
---|
322 | inside: {
|
---|
323 | punctuation: /\\/
|
---|
324 | }
|
---|
325 | }
|
---|
326 | ],
|
---|
327 | constant: constant,
|
---|
328 | number: number,
|
---|
329 | operator: operator,
|
---|
330 | punctuation: punctuation
|
---|
331 | }
|
---|
332 | },
|
---|
333 | delimiter: {
|
---|
334 | pattern: /^#\[|\]$/,
|
---|
335 | alias: 'punctuation'
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 | })
|
---|
340 | Prism.hooks.add('before-tokenize', function (env) {
|
---|
341 | if (!/<\?/.test(env.code)) {
|
---|
342 | return
|
---|
343 | }
|
---|
344 | var phpPattern =
|
---|
345 | /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#(?!\[))(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|#\[|\/\*(?:[^*]|\*(?!\/))*(?:\*\/|$))*?(?:\?>|$)/g
|
---|
346 | Prism.languages['markup-templating'].buildPlaceholders(
|
---|
347 | env,
|
---|
348 | 'php',
|
---|
349 | phpPattern
|
---|
350 | )
|
---|
351 | })
|
---|
352 | Prism.hooks.add('after-tokenize', function (env) {
|
---|
353 | Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php')
|
---|
354 | })
|
---|
355 | })(Prism)
|
---|
356 | }
|
---|