Passed
Branch fuck-54 (abba45)
by Kacper
02:24
created

Css::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\ContextualToken;
25
use Kadet\Highlighter\Parser\OpenRule;
26
use Kadet\Highlighter\Parser\Rule;
27
use Kadet\Highlighter\Parser\TokenFactory;
28
29
class Css extends Language
30
{
31
32
    /**
33
     * Tokenization rules definition
34
     *
35
     * @return array
36
     */
37
    public function getRules()
38
    {
39
        $identifier = '[\w-]+';
40
41
        return [
42
            'declaration' => [
43
                new OpenRule(new SubStringMatcher('{'), ['context' => ['!declaration.media']]),
44
                new CloseRule(new SubStringMatcher('}')),
45
            ],
46
47
            'declaration.media' => [
48
                new Rule(new RegexMatcher('/@media(.*?\{)/'), ['context' => ['!!']]),
49
            ],
50
51
            'declaration.rule' => [
52
                new OpenRule(new SubStringMatcher('('), ['context' => ['declaration.media']]),
53
                new CloseRule(new SubStringMatcher(')')),
54
            ],
55
56
            'keyword.control-sequence' => new Rule(new RegexMatcher("/(@$identifier)/")),
57
58
            'string.single' => new Rule(new SubStringMatcher('\''), [
59
                'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'],
60
                'factory' => new TokenFactory(ContextualToken::class),
61
            ]),
62
63
            'string.double' => new Rule(new SubStringMatcher('"'), [
64
                'context' => ['!keyword.escape', '!comment', '!string'],
65
                'factory' => new TokenFactory(ContextualToken::class),
66
            ]),
67
68
            'symbol.selector.id'    => new Rule(new RegexMatcher("/(#$identifier)/")),
69
            'symbol.selector.tag'   => new Rule(new RegexMatcher("/(?=(?>\\s|\\/|}|^)(\\w+).*\\{)/")),
70
            'symbol.selector.class' => new Rule(new RegexMatcher("/(\\.$identifier)/")),
71
72
            'symbol.selector.class.pseudo' => new Rule(new RegexMatcher("/(:$identifier)/")),
73
74
            'number' => new Rule(new RegexMatcher("/([-+]?[0-9]*\\.?[0-9]+([\\w%]+)?)/"), [
75
                'context' => ['declaration', '!constant.color']
76
            ]),
77
            'constant.property' => new Rule(new RegexMatcher("/($identifier):/"), ['context' => ['declaration']]),
78
79
            'call' => new Rule(new RegexMatcher("/($identifier)\\(/"), ['context' => ['!!']]),
80
81
            'constant.color' => new Rule(new RegexMatcher("/(#[0-9a-f]{1,6})/i"), [
82
                'priority' => 2,
83
                'context' => ['declaration', '!symbol.color']
84
            ]),
85
86
            'operator' => new Rule(new WordMatcher([':', '>', '+', '*', ';', ',', '!important'], ['separated' => false]), [
87
                'context' => ['!comment']
88
            ]),
89
90
            'comment' => new Rule(new CommentMatcher([], [['/*', '*/']]))
91
        ];
92
    }
93
94
    /**
95
     * Unique language identifier, for example 'php'
96
     *
97
     * @return string
98
     */
99
    public function getIdentifier()
100
    {
101
        return 'css';
102
    }
103
}