|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, 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\CloseRule; |
|
24
|
|
|
use Kadet\Highlighter\Parser\OpenRule; |
|
25
|
|
|
use Kadet\Highlighter\Parser\Rule; |
|
26
|
|
|
use Kadet\Highlighter\Parser\Token\ContextualToken; |
|
27
|
|
|
use Kadet\Highlighter\Parser\Token\TerminatorToken; |
|
28
|
|
|
use Kadet\Highlighter\Parser\TokenFactory; |
|
29
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
|
30
|
|
|
|
|
31
|
|
|
class C extends Language |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Tokenization rules setup |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setupRules() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->rules->addMany([ |
|
40
|
|
|
'keyword' => new Rule(new WordMatcher([ |
|
41
|
|
|
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', |
|
42
|
|
|
'if', 'register', 'return', 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'volatile', |
|
43
|
|
|
'while', |
|
44
|
|
|
])), |
|
45
|
|
|
|
|
46
|
|
|
'meta.newline' => new CloseRule(new RegexMatcher('/()\r?\n/'), [ |
|
47
|
|
|
'factory' => new TokenFactory(TerminatorToken::class), |
|
48
|
|
|
'context' => ['!keyword.escape', '*string', '*call.preprocessor'], |
|
49
|
|
|
'closes' => ['string.double', 'preprocessor'] |
|
50
|
|
|
]), |
|
51
|
|
|
|
|
52
|
|
|
'preprocessor' => new OpenRule(new RegexMatcher('/^(#)/m'), [ |
|
53
|
|
|
'context' => Validator::everywhere() |
|
54
|
|
|
]), |
|
55
|
|
|
|
|
56
|
|
|
'call' => [ |
|
57
|
|
|
'preprocessor' => new Rule(new RegexMatcher('/^#\s*(\w+)\b/m'), [ |
|
58
|
|
|
'context' => ['preprocessor'] |
|
59
|
|
|
]), |
|
60
|
|
|
new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => -1]), |
|
61
|
|
|
], |
|
62
|
|
|
|
|
63
|
|
|
'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:.|[0-7]{3}|x\x{2}))/'), [ |
|
64
|
|
|
'context' => Validator::everywhere() |
|
65
|
|
|
]), |
|
66
|
|
|
|
|
67
|
|
|
'keyword.format' => new Rule( |
|
68
|
|
|
new RegexMatcher('/(%[diuoxXfFeEgGaAcspn%][-+#0]?(?:[0-9]+|\*)?(?:\.(?:[0-9]+|\*))?)/'), [ |
|
69
|
|
|
'context' => ['string'] |
|
70
|
|
|
] |
|
71
|
|
|
), |
|
72
|
|
|
|
|
73
|
|
|
'string' => [ |
|
74
|
|
|
'double' => new Rule(new SubStringMatcher('"'), [ |
|
75
|
|
|
'factory' => new TokenFactory(ContextualToken::class), |
|
76
|
|
|
'context' => ['!comment'] |
|
77
|
|
|
]), |
|
78
|
|
|
'single' => new Rule(new SubStringMatcher('\''), [ |
|
79
|
|
|
'factory' => new TokenFactory(ContextualToken::class) |
|
80
|
|
|
]), |
|
81
|
|
|
new Rule(new RegexMatcher('/(<.*?>)/'), [ |
|
82
|
|
|
'context' => ['preprocessor'] |
|
83
|
|
|
]) |
|
84
|
|
|
], |
|
85
|
|
|
|
|
86
|
|
|
'symbol.type' => [ |
|
87
|
|
|
new Rule(new RegexMatcher('/(\w+)(?:\s+|\s*\*\s*)\w+\s*[=();,]/')), |
|
88
|
|
|
new Rule(new WordMatcher(['int', 'float', 'double', 'char', 'void', 'long', 'short', 'signed', 'unsigned'])) |
|
89
|
|
|
], |
|
90
|
|
|
|
|
91
|
|
|
'comment' => [ |
|
92
|
|
|
new Rule(new CommentMatcher(['//'], [['/*', '*/']]), ['priority' => 2]) |
|
93
|
|
|
], |
|
94
|
|
|
|
|
95
|
|
|
'number' => new Rule(new RegexMatcher('/\b(-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)[ful]*)\b/i')), |
|
96
|
|
|
'operator' => new Rule(new RegexMatcher('/([*&])/'), [ |
|
97
|
|
|
'priority' => 0 |
|
98
|
|
|
]), |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Unique language identifier, for example 'php' |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getIdentifier() |
|
108
|
|
|
{ |
|
109
|
|
|
return 'c'; |
|
110
|
|
|
} |
|
111
|
|
|
} |