Passed
Push — master ( ad0973...f9c297 )
by Kacper
02:48
created

Xml::getRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
/**
3
 * Highlighter
4
 *1
5
 * Copyright (C) 2015, Some right reserved.
6
 * @author Kacper "Kadet" Donat <[email protected]>
7
 *
8
 * Contact with author:
9
 * Xmpp: [email protected]
10
 * E-mail: [email protected]
11
 *
12
 * From Kadet with love.
13
 */
14
15
namespace Kadet\Highlighter\Language;
16
17
use Kadet\Highlighter\Matcher\CommentMatcher;
18
use Kadet\Highlighter\Matcher\RegexMatcher;
19
use Kadet\Highlighter\Matcher\SubStringMatcher;
20
use Kadet\Highlighter\Parser\CloseRule;
21
use Kadet\Highlighter\Parser\Token\ContextualToken;
22
use Kadet\Highlighter\Parser\OpenRule;
23
use Kadet\Highlighter\Parser\Rule;
24
use Kadet\Highlighter\Parser\Token\Token;
25
use Kadet\Highlighter\Parser\TokenFactory;
26
27
class Xml extends Language
28
{
29
    const IDENTIFIER = '(?P<namespace>[\w\.-]+:)?(?P<name>[\w\.-]+)';
30
31
    /**
32
     * Tokenization rules
33
     *
34
     * @return \Kadet\Highlighter\Parser\Rule[]|\Kadet\Highlighter\Parser\Rule[][]
35
     */
36
    public function setupRules()
37
    {
38
        $this->rules->addMany([
39
            'tag.open'  => [
40
                new OpenRule(new RegexMatcher('/(<\w)/'), ['context' => ['!tag', '!comment']]),
41
                new CloseRule(new SubStringMatcher('>'), ['context' => ['!string', '!comment']])
42
            ],
43
            'tag.close' => new Rule(new RegexMatcher('/(<\/(?:\w+:)?(?:[\w\.]+)>)/')),
44
45
            'symbol.tag' => new Rule(new RegexMatcher('/<\\/?' . self::IDENTIFIER . '/', [
46
                'name'      => Token::NAME,
47
                'namespace' => '$.namespace'
48
            ]), ['context' => ['tag', '!string']]),
49
50
            'symbol.attribute' => new Rule(new RegexMatcher('/' . self::IDENTIFIER . '=/', [
51
                'name'      => Token::NAME,
52
                'namespace' => '$.namespace'
53
            ]), ['context' => ['tag', '!string']]),
54
55
            'string.single' => new Rule(new SubStringMatcher('\''), [
56
                'context' => ['tag'],
57
                'factory' => new TokenFactory(ContextualToken::class),
58
            ]),
59
60
            'string.double' => new Rule(new SubStringMatcher('"'), [
61
                'context' => ['tag'],
62
                'factory' => new TokenFactory(ContextualToken::class),
63
            ]),
64
65
            'comment' => new Rule(new CommentMatcher([], [['<!--', '-->']])),
66
67
            'constant.entity' => new Rule(new RegexMatcher('/(&[a-z]+;)/si')),
68
        ]);
69
    }
70
71
    /** {@inheritdoc} */
72
    public function getIdentifier()
73
    {
74
        return 'xml';
75
    }
76
}
77