Skip to content

Commit

Permalink
enh(php) Add additional keywords, built-in classes, and '<?=' syntax (#…
Browse files Browse the repository at this point in the history
…2372)

* Added More Keywords

+ `__TRAIT__`
+ `bool`, `callable`, `float`, `from`, `int`, `iterable`, `object`, `string`, `void` <https://www.php.net/manual/en/language.types.intro.php> <https://www.php.net/manual/en/migration71.new-features.php>
+ `fn` <https://wiki.php.net/rfc/arrow_functions_v2>
+ `true`, `false` and `null` are now literals.

* `<?=` Tag is Enabled By Default Since PHP 5.4
* enh(zephir) add `fn` keyword for => functions
* chore(zephir) boost score by using likely/unlikely
* Added Built-In Keywords

 - This moves `exception`, `parent`, `self` and `static` keyword to built-in.
 - Added `class`, `extends`, `implements`, `interface` and `use` back to the keyword list.
 - Added aliases such as `boolean` and `integer` (alias of `bool` and `int`)  to the keywords.
 - Highlight keywords within function arguments (based on `csharp.js` language).

* fix auto-detect conflict with Java
* Exclude Parenthesis from `.hljs-params`

Co-authored-by: Josh Goebel <me@joshgoebel.com>
  • Loading branch information
taufik-nurrohman and joshgoebel committed Feb 9, 2020
1 parent c71c71b commit 71a32c3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Core Changes:

Language Improvements:

- enh(php) added more keywords and include `<?=` syntax to meta [Taufik Nurrohman][]
- fix(protobuf) Fix `rpc` when followed by a block (#) [Josh Goebel][]
- enh(zephir) almost complete rework of the zephir grammar (#2387) [Josh Goebel][]
- (markdown) much improved code block support (#2382) [Josh Goebel][]
Expand Down
46 changes: 34 additions & 12 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function(hljs) {
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
};
var PREPROCESSOR = {
className: 'meta', begin: /<\?(php)?|\?>/
className: 'meta', begin: /<\?(php|=)?|\?>/
};
var STRING = {
className: 'string',
Expand All @@ -28,18 +28,37 @@ function(hljs) {
]
};
var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]};
var KEYWORDS = {
keyword:
// Magic constants:
// <https://www.php.net/manual/en/language.constants.predefined.php>
'__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' +
// Function that look like language construct or language construct that look like function:
// List of keywords that may not require parenthesis
'die echo exit include include_once print require require_once ' +
// These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
// 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
// Other keywords:
// <https://www.php.net/manual/en/reserved.php>
// <https://www.php.net/manual/en/language.types.type-juggling.php>
'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',
literal: 'false null true',
built_in:
// Standard PHP library:
// <https://www.php.net/manual/en/book.spl.php>
'Error|0 ' + // error is too common a name esp since PHP is case in-sensative
'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 ' +
// Reserved interfaces:
// <https://www.php.net/manual/en/reserved.interfaces.php>
'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Throwable Traversable WeakReference ' +
// Reserved classes:
// <https://www.php.net/manual/en/reserved.classes.php>
'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass'
};
return {
aliases: ['php', 'php3', 'php4', 'php5', 'php6', 'php7'],
case_insensitive: true,
keywords:
'and include_once list abstract global private echo interface as static endswitch ' +
'array null if endwhile or const for endforeach self var while isset public ' +
'protected exit foreach throw elseif include __FILE__ empty require_once do xor ' +
'return parent clone use __CLASS__ __LINE__ else break print eval new ' +
'catch __METHOD__ case exception default die require __FUNCTION__ ' +
'enddeclare final try switch continue endfor endif declare unset true false ' +
'trait goto instanceof insteadof __DIR__ __NAMESPACE__ ' +
'yield finally',
keywords: KEYWORDS,
contains: [
hljs.HASH_COMMENT_MODE,
hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
Expand Down Expand Up @@ -89,13 +108,16 @@ function(hljs) {
},
{
className: 'function',
beginKeywords: 'function', end: /[;{]/, excludeEnd: true,
illegal: '\\$|\\[|%',
beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
illegal: '[$%\\[]',
contains: [
hljs.UNDERSCORE_TITLE_MODE,
{
className: 'params',
begin: '\\(', end: '\\)',
excludeBegin: true,
excludeEnd: true,
keywords: KEYWORDS,
contains: [
'self',
VARIABLE,
Expand Down
2 changes: 1 addition & 1 deletion test/detect/java/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
package l2f.gameserver.model;

public abstract class L2Char extends L2Object {
public abstract strictfp class L2Char extends L2Object {
public static final Short ERROR = 0x0001;

public void moveTo(int x, int y, int z) {
Expand Down
5 changes: 5 additions & 0 deletions test/detect/zephir/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Test extends CustomClass implements TestInterface
public function method1()
{
int a = 1, b = 2;
if likely a > b {
a++;
} else if unlikely b < a {
a--;
}
return a + b;
}

Expand Down
4 changes: 2 additions & 2 deletions test/markup/php/comments.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* <span class="hljs-doctag">@param</span> int $a
* <span class="hljs-doctag">@return</span> bool
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isEven</span><span class="hljs-params">($a)</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isEven</span>(<span class="hljs-params">$a</span>) </span>{
<span class="hljs-keyword">return</span> ($a % <span class="hljs-number">2</span>) === <span class="hljs-number">0</span>;
}

Expand All @@ -14,6 +14,6 @@
* <span class="hljs-doctag">@param</span> int $a
* <span class="hljs-doctag">@return</span> bool
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isOdd</span><span class="hljs-params">($a)</span> </span>{
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isOdd</span>(<span class="hljs-params">$a</span>) </span>{
<span class="hljs-keyword">return</span> ($a % <span class="hljs-number">2</span>) === <span class="hljs-number">1</span>;
}

0 comments on commit 71a32c3

Please sign in to comment.