source: node_modules/highlight.js/lib/languages/php.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 7.5 KB
Line 
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 aliases: ['php3', 'php4', 'php5', 'php6', 'php7', 'php8'],
107 case_insensitive: true,
108 keywords: KEYWORDS,
109 contains: [
110 hljs.HASH_COMMENT_MODE,
111 hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
112 hljs.COMMENT(
113 '/\\*',
114 '\\*/',
115 {
116 contains: [
117 {
118 className: 'doctag',
119 begin: '@[A-Za-z]+'
120 }
121 ]
122 }
123 ),
124 hljs.COMMENT(
125 '__halt_compiler.+?;',
126 false,
127 {
128 endsWithParent: true,
129 keywords: '__halt_compiler'
130 }
131 ),
132 PREPROCESSOR,
133 {
134 className: 'keyword', begin: /\$this\b/
135 },
136 VARIABLE,
137 {
138 // swallow composed identifiers to avoid parsing them as keywords
139 begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
140 },
141 {
142 className: 'function',
143 relevance: 0,
144 beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
145 illegal: '[$%\\[]',
146 contains: [
147 {
148 beginKeywords: 'use',
149 },
150 hljs.UNDERSCORE_TITLE_MODE,
151 {
152 begin: '=>', // No markup, just a relevance booster
153 endsParent: true
154 },
155 {
156 className: 'params',
157 begin: '\\(', end: '\\)',
158 excludeBegin: true,
159 excludeEnd: true,
160 keywords: KEYWORDS,
161 contains: [
162 'self',
163 VARIABLE,
164 hljs.C_BLOCK_COMMENT_MODE,
165 STRING,
166 NUMBER
167 ]
168 }
169 ]
170 },
171 {
172 className: 'class',
173 variants: [
174 { beginKeywords: "enum", illegal: /[($"]/ },
175 { beginKeywords: "class interface trait", illegal: /[:($"]/ }
176 ],
177 relevance: 0,
178 end: /\{/,
179 excludeEnd: true,
180 contains: [
181 {beginKeywords: 'extends implements'},
182 hljs.UNDERSCORE_TITLE_MODE
183 ]
184 },
185 {
186 beginKeywords: 'namespace',
187 relevance: 0,
188 end: ';',
189 illegal: /[.']/,
190 contains: [hljs.UNDERSCORE_TITLE_MODE]
191 },
192 {
193 beginKeywords: 'use',
194 relevance: 0,
195 end: ';',
196 contains: [hljs.UNDERSCORE_TITLE_MODE]
197 },
198 STRING,
199 NUMBER
200 ]
201 };
202}
203
204module.exports = php;
Note: See TracBrowser for help on using the repository browser.