php.js (6609B)
1 /* 2 Language: PHP 3 Author: Victor Karamzin <Victor.Karamzin@enterra-inc.com> 4 Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Ivan Sagalaev <maniac@softwaremaniacs.org> 5 Website: https://www.php.net 6 Category: common 7 */ 8 9 /** 10 * @param {HLJSApi} hljs 11 * @returns {LanguageDetail} 12 * */ 13 function php(hljs) { 14 var VARIABLE = { 15 begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' 16 }; 17 var PREPROCESSOR = { 18 className: 'meta', 19 variants: [ 20 { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP 21 { begin: /<\?[=]?/ }, 22 { begin: /\?>/ } // end php tag 23 ] 24 }; 25 var SUBST = { 26 className: 'subst', 27 variants: [ 28 { begin: /\$\w+/ }, 29 { begin: /\{\$/, end: /\}/ } 30 ] 31 }; 32 var SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, { 33 illegal: null, 34 }); 35 var DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, { 36 illegal: null, 37 contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST), 38 }); 39 var HEREDOC = hljs.END_SAME_AS_BEGIN({ 40 begin: /<<<[ \t]*(\w+)\n/, 41 end: /[ \t]*(\w+)\b/, 42 contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST), 43 }); 44 var STRING = { 45 className: 'string', 46 contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR], 47 variants: [ 48 hljs.inherit(SINGLE_QUOTED, { 49 begin: "b'", end: "'", 50 }), 51 hljs.inherit(DOUBLE_QUOTED, { 52 begin: 'b"', end: '"', 53 }), 54 DOUBLE_QUOTED, 55 SINGLE_QUOTED, 56 HEREDOC 57 ] 58 }; 59 var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]}; 60 var KEYWORDS = { 61 keyword: 62 // Magic constants: 63 // <https://www.php.net/manual/en/language.constants.predefined.php> 64 '__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' + 65 // Function that look like language construct or language construct that look like function: 66 // List of keywords that may not require parenthesis 67 'die echo exit include include_once print require require_once ' + 68 // These are not language construct (function) but operate on the currently-executing function and can access the current symbol table 69 // 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' + 70 // Other keywords: 71 // <https://www.php.net/manual/en/reserved.php> 72 // <https://www.php.net/manual/en/language.types.type-juggling.php> 73 'array abstract and as binary bool boolean break callable case catch class clone const continue declare default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile eval extends final finally float for foreach from global goto if implements instanceof insteadof int integer interface isset iterable list new object or private protected public real return string switch throw trait try unset use var void while xor yield', 74 literal: 'false null true', 75 built_in: 76 // Standard PHP library: 77 // <https://www.php.net/manual/en/book.spl.php> 78 'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive 79 '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 ' + 80 // Reserved interfaces: 81 // <https://www.php.net/manual/en/reserved.interfaces.php> 82 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Throwable Traversable WeakReference ' + 83 // Reserved classes: 84 // <https://www.php.net/manual/en/reserved.classes.php> 85 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass' 86 }; 87 return { 88 aliases: ['php', 'php3', 'php4', 'php5', 'php6', 'php7'], 89 case_insensitive: true, 90 keywords: KEYWORDS, 91 contains: [ 92 hljs.HASH_COMMENT_MODE, 93 hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}), 94 hljs.COMMENT( 95 '/\\*', 96 '\\*/', 97 { 98 contains: [ 99 { 100 className: 'doctag', 101 begin: '@[A-Za-z]+' 102 } 103 ] 104 } 105 ), 106 hljs.COMMENT( 107 '__halt_compiler.+?;', 108 false, 109 { 110 endsWithParent: true, 111 keywords: '__halt_compiler' 112 } 113 ), 114 PREPROCESSOR, 115 { 116 className: 'keyword', begin: /\$this\b/ 117 }, 118 VARIABLE, 119 { 120 // swallow composed identifiers to avoid parsing them as keywords 121 begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ 122 }, 123 { 124 className: 'function', 125 beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true, 126 illegal: '[$%\\[]', 127 contains: [ 128 hljs.UNDERSCORE_TITLE_MODE, 129 { 130 className: 'params', 131 begin: '\\(', end: '\\)', 132 excludeBegin: true, 133 excludeEnd: true, 134 keywords: KEYWORDS, 135 contains: [ 136 'self', 137 VARIABLE, 138 hljs.C_BLOCK_COMMENT_MODE, 139 STRING, 140 NUMBER 141 ] 142 } 143 ] 144 }, 145 { 146 className: 'class', 147 beginKeywords: 'class interface', end: '{', excludeEnd: true, 148 illegal: /[:\(\$"]/, 149 contains: [ 150 {beginKeywords: 'extends implements'}, 151 hljs.UNDERSCORE_TITLE_MODE 152 ] 153 }, 154 { 155 beginKeywords: 'namespace', end: ';', 156 illegal: /[\.']/, 157 contains: [hljs.UNDERSCORE_TITLE_MODE] 158 }, 159 { 160 beginKeywords: 'use', end: ';', 161 contains: [hljs.UNDERSCORE_TITLE_MODE] 162 }, 163 { 164 begin: '=>' // No markup, just a relevance booster 165 }, 166 STRING, 167 NUMBER 168 ] 169 }; 170 } 171 172 module.exports = php;