|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2015, Some right reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Language; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Kadet\Highlighter\Matcher\CommentMatcher; |
|
20
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
|
21
|
|
|
use Kadet\Highlighter\Matcher\SubStringMatcher; |
|
22
|
|
|
use Kadet\Highlighter\Matcher\WordMatcher; |
|
23
|
|
|
use Kadet\Highlighter\Parser\ContextualToken; |
|
24
|
|
|
use Kadet\Highlighter\Parser\Rule; |
|
25
|
|
|
use Kadet\Highlighter\Parser\Token; |
|
26
|
|
|
use Kadet\Highlighter\Parser\TokenFactory; |
|
27
|
|
|
|
|
28
|
|
|
class PowerShell extends Language |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Tokenization rules definition |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getRules() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
'string.single' => new Rule(new SubStringMatcher('\''), [ |
|
40
|
|
|
'context' => ['!keyword.escape', '!comment', '!string'], |
|
41
|
|
|
'factory' => new TokenFactory(ContextualToken::class), |
|
42
|
|
|
]), |
|
43
|
|
|
|
|
44
|
|
|
'string.double' => new Rule(new SubStringMatcher('"'), [ |
|
45
|
|
|
'context' => ['!keyword.escape', '!comment', '!string'], |
|
46
|
|
|
'factory' => new TokenFactory(ContextualToken::class), |
|
47
|
|
|
]), |
|
48
|
|
|
|
|
49
|
|
|
'variable' => [ |
|
50
|
|
|
new Rule(new RegexMatcher('/[^\^](\$(?P<namespace>\w+:)?[a-z_]\w*)/i'), [ |
|
51
|
|
|
'context' => ['!string.single', '!comment'], |
|
52
|
|
|
'priority' => 0 |
|
53
|
|
|
]), |
|
54
|
|
|
new Rule(new RegexMatcher('/[^\^](\$\{(?P<namespace>\w+:)?[a-z_]\w*\})/i'), [ |
|
55
|
|
|
'context' => ['!string.single', '!comment'], |
|
56
|
|
|
'priority' => 0 |
|
57
|
|
|
]), |
|
58
|
|
|
], |
|
59
|
|
|
|
|
60
|
|
|
'variable.splat' => new Rule(new RegexMatcher('/[^\^](\@(?P<namespace>\w+:)?[a-z_]\w*)/i'), [ |
|
61
|
|
|
'context' => ['!string.single', '!comment'], |
|
62
|
|
|
'priority' => 0 |
|
63
|
|
|
]), |
|
64
|
|
|
|
|
65
|
|
|
'variable.special' => new Rule(new RegexMatcher('/(\$(?:\$|\^|\?|_|true|false|null))/i'), [ |
|
66
|
|
|
'priority' => 5, |
|
67
|
|
|
'context' => ['!string.single', '!comment'] |
|
68
|
|
|
]), |
|
69
|
|
|
|
|
70
|
|
|
'variable.scope' => new Rule(null, ['context' => ['*variable']]), |
|
71
|
|
|
|
|
72
|
|
|
'comment' => new Rule(new CommentMatcher(['#'], [['<#', '#>']])), |
|
73
|
|
|
'keyword.doc-section' => new Rule(new RegexMatcher('/[\s\n](\.\w+)/i'), [ |
|
74
|
|
|
'context' => ['comment'] |
|
75
|
|
|
]), |
|
76
|
|
|
|
|
77
|
|
|
'symbol.dotnet' => new Rule(new RegexMatcher('/\[([\w\.]+(?:\[\])?)\]/si')), |
|
78
|
|
|
|
|
79
|
|
|
'annotation' => new Rule( |
|
80
|
|
|
new RegexMatcher('/\[([\w\.]+)\s*(?P<arguments>\((?>[^()]+|(?&arguments))*\))\s*\]/si', [ |
|
81
|
|
|
1 => Token::NAME, |
|
82
|
|
|
'arguments' => '$.arguments' |
|
83
|
|
|
]) |
|
84
|
|
|
), |
|
85
|
|
|
|
|
86
|
|
|
'keyword' => new Rule(new WordMatcher([ |
|
87
|
|
|
'Begin', 'Break', 'Catch', 'Continue', 'Data', 'Do', 'DynamicParam', |
|
88
|
|
|
'Else', 'Elseif', 'End', 'Exit', 'Filter', 'Finally', 'For', 'ForEach', |
|
89
|
|
|
'From', 'Function', 'If', 'In', 'InlineScript', 'Hidden', 'Parallel', 'Param', |
|
90
|
|
|
'Process', 'Return', 'Sequence', 'Switch', 'Throw', 'Trap', 'Try', 'Until', 'While', 'Workflow' |
|
91
|
|
|
]), ['priority' => 3]), |
|
92
|
|
|
|
|
93
|
|
|
'operator' => new Rule(new RegexMatcher('/(&|\-eq|\-ne|\-gt|\-ge|\-lt|\-le|\-ieq|\-ine|\-igt|\-ige|\-ilt|\-ile|\-ceq|\-cne|\-cgt|\-cge|\-clt|\-cle|\-like|\-notlike|\-match|\-notmatch|\-ilike|\-inotlike|\-imatch|\-inotmatch|\-clike|\-cnotlike|\-cmatch|\-cnotmatch|\-contains|\-notcontains|\-icontains|\-inotcontains|\-ccontains|\-cnotcontains|\-isnot|\-is|\-as|\-replace|\-ireplace|\-creplace|\-and|\-or|\-band|\-bor|\-not|\-bnot|\-f|\-casesensitive|\-exact|\-file|\-regex|\-wildcard)\b/i'), [ |
|
94
|
|
|
'context' => ['!string', '!comment'] |
|
95
|
|
|
]), |
|
96
|
|
|
|
|
97
|
|
|
'parameter' => new Rule(new RegexMatcher('/\s(-\w+:?)\b/i'), [ |
|
98
|
|
|
'priority' => 0, |
|
99
|
|
|
'context' => ['!string', '!comment', '!call'] |
|
100
|
|
|
]), |
|
101
|
|
|
|
|
102
|
|
|
'operator.punctuation' => new Rule(new WordMatcher([',', ';', '.', '::', '%'], ['separated' => false]), [ |
|
103
|
|
|
'priority' => 0, |
|
104
|
|
|
'context' => ['!string', '!comment', '!call'] |
|
105
|
|
|
]), |
|
106
|
|
|
|
|
107
|
|
|
'number' => [ |
|
108
|
|
|
new Rule(new RegexMatcher('/(-?(?:0x[0-9a-f]+|\d+)l?(?:kb|mb|gb|tb|pb)?)/i'), [ |
|
109
|
|
|
'priority' => 0, |
|
110
|
|
|
'context' => ['!string', '!comment', '!variable', '!call'] |
|
111
|
|
|
]), |
|
112
|
|
|
new Rule(new RegexMatcher('/(-?(?>\d+)?\.\d+(?>d|l)(?>e(?:\+|-)?\d+)?(?:kb|mb|gb|tb|pb)?)/i'), [ |
|
113
|
|
|
'priority' => 0, |
|
114
|
|
|
'context' => ['!string', '!comment', '!variable', '!call'] |
|
115
|
|
|
]) |
|
116
|
|
|
], |
|
117
|
|
|
|
|
118
|
|
|
'call' => new Rule(new RegexMatcher('/(?<![^`]`)(?<=\n|\{|\(|\}|\||=|;|^|function|filter)\s*(\w[\w-\.]+)/i'), ['priority' => 2]) |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Unique language identifier, for example 'php' |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getIdentifier() |
|
128
|
|
|
{ |
|
129
|
|
|
return 'PowerShell'; |
|
130
|
|
|
} |
|
131
|
|
|
} |