UNPKG

3.74 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 *
5 * Supports the following:
6 * - Extends clike syntax
7 * - Support for PHP 5.3+ (namespaces, traits, generators, etc)
8 * - Smarter constant and function matching
9 *
10 * Adds the following new token classes:
11 * constant, delimiter, variable, function, package
12 */
13(function (Prism) {
14 Prism.languages.php = Prism.languages.extend('clike', {
15 'keyword': /\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|new|or|parent|print|private|protected|public|require|require_once|return|static|switch|throw|trait|try|unset|use|var|while|xor|yield)\b/i,
16 'boolean': {
17 pattern: /\b(?:false|true)\b/i,
18 alias: 'constant'
19 },
20 'constant': [
21 /\b[A-Z_][A-Z0-9_]*\b/,
22 /\b(?:null)\b/i,
23 ],
24 'comment': {
25 pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
26 lookbehind: true
27 }
28 });
29
30 Prism.languages.insertBefore('php', 'string', {
31 'shell-comment': {
32 pattern: /(^|[^\\])#.*/,
33 lookbehind: true,
34 alias: 'comment'
35 }
36 });
37
38 Prism.languages.insertBefore('php', 'comment', {
39 'delimiter': {
40 pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
41 alias: 'important'
42 }
43 });
44
45 Prism.languages.insertBefore('php', 'keyword', {
46 'variable': /\$+(?:\w+\b|(?={))/i,
47 'package': {
48 pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
49 lookbehind: true,
50 inside: {
51 punctuation: /\\/
52 }
53 }
54 });
55
56 // Must be defined after the function pattern
57 Prism.languages.insertBefore('php', 'operator', {
58 'property': {
59 pattern: /(->)[\w]+/,
60 lookbehind: true
61 }
62 });
63
64 var string_interpolation = {
65 pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[.+?]|->\w+)*)/,
66 lookbehind: true,
67 inside: Prism.languages.php
68 };
69
70 Prism.languages.insertBefore('php', 'string', {
71 'nowdoc-string': {
72 pattern: /<<<'([^']+)'(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\1;/,
73 greedy: true,
74 alias: 'string',
75 inside: {
76 'delimiter': {
77 pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
78 alias: 'symbol',
79 inside: {
80 'punctuation': /^<<<'?|[';]$/
81 }
82 }
83 }
84 },
85 'heredoc-string': {
86 pattern: /<<<(?:"([^"]+)"(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\1;|([a-z_]\w*)(?:\r\n?|\n)(?:.*(?:\r\n?|\n))*?\2;)/i,
87 greedy: true,
88 alias: 'string',
89 inside: {
90 'delimiter': {
91 pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
92 alias: 'symbol',
93 inside: {
94 'punctuation': /^<<<"?|[";]$/
95 }
96 },
97 'interpolation': string_interpolation // See below
98 }
99 },
100 'single-quoted-string': {
101 pattern: /'(?:\\[\s\S]|[^\\'])*'/,
102 greedy: true,
103 alias: 'string'
104 },
105 'double-quoted-string': {
106 pattern: /"(?:\\[\s\S]|[^\\"])*"/,
107 greedy: true,
108 alias: 'string',
109 inside: {
110 'interpolation': string_interpolation // See below
111 }
112 }
113 });
114 // The different types of PHP strings "replace" the C-like standard string
115 delete Prism.languages.php['string'];
116
117 Prism.hooks.add('before-tokenize', function(env) {
118 if (!/<\?/.test(env.code)) {
119 return;
120 }
121
122 var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#)(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|\/\*[\s\S]*?(?:\*\/|$))*?(?:\?>|$)/ig;
123 Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
124 });
125
126 Prism.hooks.add('after-tokenize', function(env) {
127 Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
128 });
129
130}(Prism));