UNPKG

8.23 kBJavaScriptView Raw
1/**
2 * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/
3 * Modified by Miles Johnson: http://milesj.me
4 * Rewritten by Tom Pavelec
5 *
6 * Supports PHP 5.3 - 8.0
7 */
8(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
22 Prism.languages.php = {
23 'delimiter': {
24 pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
25 alias: 'important'
26 },
27 'comment': comment,
28 'variable': /\$+(?:\w+\b|(?={))/i,
29 'package': {
30 pattern: /(namespace\s+|use\s+(?:function\s+)?)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
31 lookbehind: true,
32 inside: {
33 'punctuation': /\\/
34 }
35 },
36 'keyword': [
37 {
38 pattern: /(\(\s*)\b(?:bool|boolean|int|integer|float|string|object|array)\b(?=\s*\))/i,
39 alias: 'type-casting',
40 greedy: true,
41 lookbehind: true
42 },
43 {
44 pattern: /([(,?]\s*)\b(?:bool|int|float|string|object|array(?!\s*\()|mixed|self|static|callable|iterable|(?:null|false)(?=\s*\|))\b(?=\s*\$)/i,
45 alias: 'type-hint',
46 greedy: true,
47 lookbehind: true
48 },
49 {
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,
57 alias: 'return-type',
58 greedy: true,
59 lookbehind: true
60 },
61 {
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,
69 alias: 'type-declaration',
70 greedy: true
71 },
72 {
73 pattern: /(\|\s*)(?:null|false)\b/i,
74 alias: 'type-declaration',
75 greedy: true,
76 lookbehind: true
77 },
78 {
79 pattern: /\b(?:parent|self|static)(?=\s*::)/i,
80 alias: 'static-context',
81 greedy: true
82 },
83 /\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
84 ],
85 'argument-name': /\b[a-z_]\w*(?=\s*:(?!:))/i,
86 'class-name': [
87 {
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,
94 greedy: true,
95 lookbehind: true
96 },
97 {
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,
120 alias: 'class-name-fully-qualified',
121 greedy: true,
122 lookbehind: true,
123 inside: {
124 'punctuation': /\\/
125 }
126 },
127 {
128 pattern: /\b[a-z_]\w*(?=\s*\$)/i,
129 alias: 'type-declaration',
130 greedy: true
131 },
132 {
133 pattern: /(?:\\?\b[a-z_]\w*)+(?=\s*\$)/i,
134 alias: ['class-name-fully-qualified', 'type-declaration'],
135 greedy: true,
136 inside: {
137 'punctuation': /\\/
138 }
139 },
140 {
141 pattern: /\b[a-z_]\w*(?=\s*::)/i,
142 alias: 'static-context',
143 greedy: true
144 },
145 {
146 pattern: /(?:\\?\b[a-z_]\w*)+(?=\s*::)/i,
147 alias: ['class-name-fully-qualified', 'static-context'],
148 greedy: true,
149 inside: {
150 'punctuation': /\\/
151 }
152 },
153 {
154 pattern: /([(,?]\s*)[a-z_]\w*(?=\s*\$)/i,
155 alias: 'type-hint',
156 greedy: true,
157 lookbehind: true
158 },
159 {
160 pattern: /([(,?]\s*)(?:\\?\b[a-z_]\w*)+(?=\s*\$)/i,
161 alias: ['class-name-fully-qualified', 'type-hint'],
162 greedy: true,
163 lookbehind: true,
164 inside: {
165 'punctuation': /\\/
166 }
167 },
168 {
169 pattern: /(\)\s*:\s*(?:\?\s*)?)\b[a-z_]\w*(?!\\)\b/i,
170 alias: 'return-type',
171 greedy: true,
172 lookbehind: true
173 },
174 {
175 pattern: /(\)\s*:\s*(?:\?\s*)?)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
176 alias: ['class-name-fully-qualified', 'return-type'],
177 greedy: true,
178 lookbehind: true,
179 inside: {
180 'punctuation': /\\/
181 }
182 }
183 ],
184 'constant': constant,
185 'function': /\w+\s*(?=\()/,
186 'property': {
187 pattern: /(->)[\w]+/,
188 lookbehind: true
189 },
190 'number': number,
191 'operator': operator,
192 'punctuation': punctuation
193 };
194
195 var string_interpolation = {
196 pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)*)/,
197 lookbehind: true,
198 inside: Prism.languages.php
199 };
200
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': /^<<<'?|[';]$/
212 }
213 }
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'
296 }
297 }
298 },
299 });
300
301 Prism.hooks.add('before-tokenize', function(env) {
302 if (!/<\?/.test(env.code)) {
303 return;
304 }
305
306 var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#(?!\[))(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|#\[|\/\*(?:[^*]|\*(?!\/))*(?:\*\/|$))*?(?:\?>|$)/ig;
307 Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
308 });
309
310 Prism.hooks.add('after-tokenize', function(env) {
311 Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
312 });
313
314}(Prism));