|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2015, Some right reserved. |
|
6
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Contact with author: |
|
9
|
|
|
* Xmpp: [email protected] |
|
10
|
|
|
* E-mail: [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* From Kadet with love. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Kadet\Highlighter\Language; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Matcher\CommentMatcher; |
|
19
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
|
20
|
|
|
use Kadet\Highlighter\Matcher\SubStringMatcher; |
|
21
|
|
|
use Kadet\Highlighter\Matcher\WordMatcher; |
|
22
|
|
|
use Kadet\Highlighter\Parser\Rule; |
|
23
|
|
|
use Kadet\Highlighter\Parser\TokenFactory; |
|
24
|
|
|
|
|
25
|
|
|
class JavaScriptLanguage extends Language |
|
26
|
|
|
{ |
|
27
|
|
|
const IDENTIFIER = '[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*'; |
|
28
|
|
|
|
|
29
|
|
|
public function getRules() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'string.single' => new Rule(new SubStringMatcher('\''), [ |
|
33
|
|
|
'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'], |
|
34
|
|
|
'factory' => new TokenFactory('Kadet\\Highlighter\\Parser\\MarkerToken'), |
|
35
|
|
|
]), |
|
36
|
|
|
|
|
37
|
|
|
'string.double' => new Rule(new SubStringMatcher('"'), [ |
|
38
|
|
|
'context' => ['!keyword.escape', '!comment', '!string'], |
|
39
|
|
|
'factory' => new TokenFactory('Kadet\\Highlighter\\Parser\\MarkerToken'), |
|
40
|
|
|
]), |
|
41
|
|
|
|
|
42
|
|
|
'variable' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')/iu'), ['priority' => -10000]), |
|
43
|
|
|
'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [ |
|
44
|
|
|
'priority' => -2 |
|
45
|
|
|
]), |
|
46
|
|
|
|
|
47
|
|
|
'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')), |
|
48
|
|
|
|
|
49
|
|
|
'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:x[0-9a-fA-F]{1,2}|u\{[0-9a-fA-F]{1,6}\}|[0-7]{1,3}|.))/i'), [ |
|
50
|
|
|
'context' => ['string'] |
|
51
|
|
|
]), |
|
52
|
|
|
|
|
53
|
|
|
'comment' => new Rule(new CommentMatcher(['//'], [ |
|
54
|
|
|
['/*', '*/'] |
|
55
|
|
|
])), |
|
56
|
|
|
|
|
57
|
|
|
'call' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')\s*\(/iu'), ['priority' => -1]), |
|
58
|
|
|
|
|
59
|
|
|
'keyword' => new Rule(new WordMatcher([ |
|
60
|
|
|
'do', 'if', 'in', 'for', 'let', 'new', 'try', 'var', 'case', 'else', 'enum', 'eval', 'false', |
|
61
|
|
|
'null', 'this', 'true', 'void', 'with', 'break', 'catch', 'class', 'const', 'super', 'throw', |
|
62
|
|
|
'while', 'yield', 'delete', 'export', 'import', 'public', 'return', 'static', 'switch', |
|
63
|
|
|
'typeof', 'default', 'extends', 'finally', 'package', 'private', 'continue', 'debugger', |
|
64
|
|
|
'function', 'arguments', 'interface', 'protected', 'implements', 'instanceof', |
|
65
|
|
|
]), ['context' => ['!string', '!variable', '!comment']]), |
|
66
|
|
|
|
|
67
|
|
|
'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')), |
|
68
|
|
|
|
|
69
|
|
|
'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), ['priority' => 0]), |
|
70
|
|
|
'operator' => new Rule(new WordMatcher([ |
|
71
|
|
|
'->', '++', '--', '-', '+', '/', '*', '**', '||', '&&', '^', '%', '&', '@', '!', '|', ':', '.' |
|
72
|
|
|
], ['separated' => false]), ['priority' => 0]), |
|
73
|
|
|
|
|
74
|
|
|
// 'string.regex' => new Rule(new RegexMatcher('#\b/(.*?)/[gmixXsu\bUAJ]+#')) |
|
|
|
|
|
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getIdentifier() |
|
79
|
|
|
{ |
|
80
|
|
|
return 'javascript'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.