Completed
Push — master ( 6054a8...c11aa8 )
by Kacper
04:06
created

C::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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