UNPKG

7.6 kBJavaScriptView Raw
1/*
2Language: PHP
3Author: Victor Karamzin <Victor.Karamzin@enterra-inc.com>
4Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Ivan Sagalaev <maniac@softwaremaniacs.org>
5Website: https://www.php.net
6Category: common
7*/
8
9/**
10 * @param {HLJSApi} hljs
11 * @returns {LanguageDetail}
12 * */
13function php(hljs) {
14 const VARIABLE = {
15 className: 'variable',
16 begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
17 // negative look-ahead tries to avoid matching patterns that are not
18 // Perl at all like $ident$, @ident@, etc.
19 `(?![A-Za-z0-9])(?![$])`
20 };
21 const PREPROCESSOR = {
22 className: 'meta',
23 variants: [
24 { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
25 { begin: /<\?[=]?/ },
26 { begin: /\?>/ } // end php tag
27 ]
28 };
29 const SUBST = {
30 className: 'subst',
31 variants: [
32 { begin: /\$\w+/ },
33 { begin: /\{\$/, end: /\}/ }
34 ]
35 };
36 const SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, {
37 illegal: null,
38 });
39 const DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
40 illegal: null,
41 contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
42 });
43 const HEREDOC = hljs.END_SAME_AS_BEGIN({
44 begin: /<<<[ \t]*(\w+)\n/,
45 end: /[ \t]*(\w+)\b/,
46 contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
47 });
48 const STRING = {
49 className: 'string',
50 contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
51 variants: [
52 hljs.inherit(SINGLE_QUOTED, {
53 begin: "b'", end: "'",
54 }),
55 hljs.inherit(DOUBLE_QUOTED, {
56 begin: 'b"', end: '"',
57 }),
58 DOUBLE_QUOTED,
59 SINGLE_QUOTED,
60 HEREDOC
61 ]
62 };
63 const NUMBER = {
64 className: 'number',
65 variants: [
66 { begin: `\\b0b[01]+(?:_[01]+)*\\b` }, // Binary w/ underscore support
67 { begin: `\\b0o[0-7]+(?:_[0-7]+)*\\b` }, // Octals w/ underscore support
68 { begin: `\\b0x[\\da-f]+(?:_[\\da-f]+)*\\b` }, // Hex w/ underscore support
69 // Decimals w/ underscore support, with optional fragments and scientific exponent (e) suffix.
70 { begin: `(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:e[+-]?\\d+)?` }
71 ],
72 relevance: 0
73 };
74 const KEYWORDS = {
75 keyword:
76 // Magic constants:
77 // <https://www.php.net/manual/en/language.constants.predefined.php>
78 '__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' +
79 // Function that look like language construct or language construct that look like function:
80 // List of keywords that may not require parenthesis
81 'die echo exit include include_once print require require_once ' +
82 // These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
83 // 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
84 // Other keywords:
85 // <https://www.php.net/manual/en/reserved.php>
86 // <https://www.php.net/manual/en/language.types.type-juggling.php>
87 'array abstract and as binary bool boolean break callable case catch class clone const continue declare ' +
88 'default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile enum eval extends ' +
89 'final finally float for foreach from global goto if implements instanceof insteadof int integer interface ' +
90 'isset iterable list match|0 mixed new object or private protected public real return string switch throw trait ' +
91 'try unset use var void while xor yield',
92 literal: 'false null true',
93 built_in:
94 // Standard PHP library:
95 // <https://www.php.net/manual/en/book.spl.php>
96 'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive
97 'AppendIterator ArgumentCountError ArithmeticError ArrayIterator ArrayObject AssertionError BadFunctionCallException BadMethodCallException CachingIterator CallbackFilterIterator CompileError Countable DirectoryIterator DivisionByZeroError DomainException EmptyIterator ErrorException Exception FilesystemIterator FilterIterator GlobIterator InfiniteIterator InvalidArgumentException IteratorIterator LengthException LimitIterator LogicException MultipleIterator NoRewindIterator OutOfBoundsException OutOfRangeException OuterIterator OverflowException ParentIterator ParseError RangeException RecursiveArrayIterator RecursiveCachingIterator RecursiveCallbackFilterIterator RecursiveDirectoryIterator RecursiveFilterIterator RecursiveIterator RecursiveIteratorIterator RecursiveRegexIterator RecursiveTreeIterator RegexIterator RuntimeException SeekableIterator SplDoublyLinkedList SplFileInfo SplFileObject SplFixedArray SplHeap SplMaxHeap SplMinHeap SplObjectStorage SplObserver SplObserver SplPriorityQueue SplQueue SplStack SplSubject SplSubject SplTempFileObject TypeError UnderflowException UnexpectedValueException UnhandledMatchError ' +
98 // Reserved interfaces:
99 // <https://www.php.net/manual/en/reserved.interfaces.php>
100 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable WeakReference WeakMap ' +
101 // Reserved classes:
102 // <https://www.php.net/manual/en/reserved.classes.php>
103 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass'
104 };
105 return {
106 case_insensitive: true,
107 keywords: KEYWORDS,
108 contains: [
109 hljs.HASH_COMMENT_MODE,
110 hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
111 hljs.COMMENT(
112 '/\\*',
113 '\\*/',
114 {
115 contains: [
116 {
117 className: 'doctag',
118 begin: '@[A-Za-z]+'
119 }
120 ]
121 }
122 ),
123 hljs.COMMENT(
124 '__halt_compiler.+?;',
125 false,
126 {
127 endsWithParent: true,
128 keywords: '__halt_compiler'
129 }
130 ),
131 PREPROCESSOR,
132 {
133 className: 'keyword', begin: /\$this\b/
134 },
135 VARIABLE,
136 {
137 // swallow composed identifiers to avoid parsing them as keywords
138 begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
139 },
140 {
141 className: 'function',
142 relevance: 0,
143 beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
144 illegal: '[$%\\[]',
145 contains: [
146 {
147 beginKeywords: 'use',
148 },
149 hljs.UNDERSCORE_TITLE_MODE,
150 {
151 begin: '=>', // No markup, just a relevance booster
152 endsParent: true
153 },
154 {
155 className: 'params',
156 begin: '\\(', end: '\\)',
157 excludeBegin: true,
158 excludeEnd: true,
159 keywords: KEYWORDS,
160 contains: [
161 'self',
162 VARIABLE,
163 hljs.C_BLOCK_COMMENT_MODE,
164 STRING,
165 NUMBER
166 ]
167 }
168 ]
169 },
170 {
171 className: 'class',
172 variants: [
173 { beginKeywords: "enum", illegal: /[($"]/ },
174 { beginKeywords: "class interface trait", illegal: /[:($"]/ }
175 ],
176 relevance: 0,
177 end: /\{/,
178 excludeEnd: true,
179 contains: [
180 {beginKeywords: 'extends implements'},
181 hljs.UNDERSCORE_TITLE_MODE
182 ]
183 },
184 {
185 beginKeywords: 'namespace',
186 relevance: 0,
187 end: ';',
188 illegal: /[.']/,
189 contains: [hljs.UNDERSCORE_TITLE_MODE]
190 },
191 {
192 beginKeywords: 'use',
193 relevance: 0,
194 end: ';',
195 contains: [hljs.UNDERSCORE_TITLE_MODE]
196 },
197 STRING,
198 NUMBER
199 ]
200 };
201}
202
203export { php as default };