1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Highlighter |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2016, Some right reserved. |
7
|
|
|
* |
8
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Contact with author: |
11
|
|
|
* Xmpp: [email protected] |
12
|
|
|
* E-mail: [email protected] |
13
|
|
|
* |
14
|
|
|
* From Kadet with love. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Kadet\Highlighter\Language; |
18
|
|
|
|
19
|
|
|
use Kadet\Highlighter\Matcher\CommentMatcher; |
20
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
21
|
|
|
use Kadet\Highlighter\Matcher\WordMatcher; |
22
|
|
|
use Kadet\Highlighter\Parser\CloseRule; |
23
|
|
|
use Kadet\Highlighter\Parser\OpenRule; |
24
|
|
|
use Kadet\Highlighter\Parser\Rule; |
25
|
|
|
use Kadet\Highlighter\Parser\Token\LanguageToken; |
26
|
|
|
use Kadet\Highlighter\Parser\Token\TerminatorToken; |
27
|
|
|
use Kadet\Highlighter\Parser\TokenFactory; |
28
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
29
|
|
|
|
30
|
|
|
class C extends GreedyLanguage |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tokenization rules setup |
35
|
|
|
*/ |
36
|
|
|
public function setupRules() |
37
|
|
|
{ |
38
|
|
|
$this->rules->addMany([ |
39
|
|
|
'keyword' => new Rule(new WordMatcher([ |
40
|
|
|
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'enum', 'extern', 'for', 'goto', |
41
|
|
|
'if', 'register', 'return', 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'volatile', |
42
|
|
|
'while', |
43
|
|
|
])), |
44
|
|
|
|
45
|
|
|
'meta.newline' => new CloseRule(new RegexMatcher('/()\R/m'), [ |
46
|
|
|
'factory' => new TokenFactory(TerminatorToken::class), |
47
|
|
|
'context' => ['!operator.escape', '*string', '*call.preprocessor'], |
48
|
|
|
'closes' => ['string.double', 'preprocessor'], |
49
|
|
|
]), |
50
|
|
|
|
51
|
|
|
'preprocessor' => new OpenRule(new RegexMatcher('/^(#)/m'), [ |
52
|
|
|
'context' => Validator::everywhere(), |
53
|
|
|
'priority' => -1 |
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
|
|
|
'operator.escape' => new Rule(new RegexMatcher('/(\\\(?:\R|.|[0-7]{3}|x\x{2}))/s'), [ |
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
|
|
|
'string' => array_merge([ |
73
|
|
|
new Rule(new RegexMatcher('/(<.*?>)/'), [ |
74
|
|
|
'context' => ['preprocessor'] |
75
|
|
|
]) |
76
|
|
|
], CommonFeatures::strings(['single' => '\'', 'double' => '"'], [ |
77
|
|
|
'context' => ['!operator.escape', '!comment', '!string'], |
78
|
|
|
])), |
79
|
|
|
|
80
|
|
|
'symbol.type' => [ |
81
|
|
|
new Rule(new RegexMatcher('/(\w+)(?:\s+|\s*\*\s*)\w+\s*[=();,]/'), ['name' => 'universal']), |
82
|
|
|
new Rule(new WordMatcher(['int', 'float', 'double', 'char', 'void', 'long', 'short', 'signed', 'unsigned']), ['name' => 'builtin']) |
83
|
|
|
], |
84
|
|
|
|
85
|
|
|
'comment' => [ |
86
|
|
|
new Rule(new CommentMatcher(['//'], [['/*', '*/']]), ['priority' => 2]) |
87
|
|
|
], |
88
|
|
|
|
89
|
|
|
'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*(?:->|\.)\s*([a-z_]\w*))/'), ['priority' => 0]), |
90
|
|
|
|
91
|
|
|
'number' => new Rule(new RegexMatcher('/\b(-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)[ful]*)\b/i')), |
92
|
|
|
'operator' => [ |
93
|
|
|
'punctuation' => new Rule(new RegexMatcher('/([;,])/'), ['priority' => 0]), |
94
|
|
|
new Rule(new RegexMatcher('/([*&])/'), ['priority' => 0]), |
95
|
|
|
new Rule(new RegexMatcher('/([!+\-\/*&|^<>=]{1,2}=?)/'), ['priority' => 0]) |
96
|
|
|
], |
97
|
|
|
|
98
|
|
|
'language.c' => [ |
99
|
|
|
new Rule(new RegexMatcher('/^#define\s+\w+(.*?)(?>[^\\\]\r\n|[^\\\\\r]\n|\Z)/sim'), [ |
100
|
|
|
'factory' => new TokenFactory(LanguageToken::class), |
101
|
|
|
'inject' => $this, |
102
|
|
|
'context' => ['preprocessor'], |
103
|
|
|
'priority' => 10 |
104
|
|
|
]), |
105
|
|
|
] |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Unique language identifier, for example 'php' |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
2 |
|
public function getIdentifier() |
115
|
|
|
{ |
116
|
2 |
|
return 'c'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function getMetadata() |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'name' => ['c'], |
123
|
|
|
'mime' => ['text/x-csrc', 'text/x-chdr'], |
124
|
|
|
'extension' => ['*.c', '*.h', '*.idc'] |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|