Completed
Push — master ( 9b98d4...f3f280 )
by Maciej
06:39
created

Xml::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 1
rs 9.7333
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * Highlighter
5
 *1
6
 * Copyright (C) 2016, Some right reserved.
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
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\SubStringMatcher;
21
use Kadet\Highlighter\Parser\CloseRule;
22
use Kadet\Highlighter\Parser\OpenRule;
23
use Kadet\Highlighter\Parser\Rule;
24
use Kadet\Highlighter\Parser\Token\Token;
25
26
class Xml extends GreedyLanguage
27
{
28
    private const IDENTIFIER = '(?P<namespace>[\w\.-]+:)?(?P<name>[\w\.-]+)';
29
30
    /**
31
     * Tokenization rules
32
     */
33 2
    public function setupRules()
34
    {
35 2
        $this->rules->addMany([
36
            'tag.open'  => [
37 2
                new OpenRule(new RegexMatcher('/(<[\w\.-]+)[:\/>:\s]/')),
38 2
                new CloseRule(new SubStringMatcher('>'), ['context' => ['!string', '!comment']])
39
            ],
40 2
            'tag.close' => new Rule(new RegexMatcher('/(<\/' . self::IDENTIFIER . '>)/')),
41
42 2
            'symbol.tag' => new Rule(new RegexMatcher('/<\\/?' . self::IDENTIFIER . '/', [
43 2
                'name'      => Token::NAME,
44 2
                'namespace' => '$.namespace'
45 2
            ]), ['context' => ['tag', '!string']]),
46
47 2
            'symbol.attribute' => new Rule(new RegexMatcher('/' . self::IDENTIFIER . '=/', [
48 2
                'name'      => Token::NAME,
49 2
                'namespace' => '$.namespace'
50 2
            ]), ['context' => ['tag', '!string']]),
51
52 2
            'constant.entity' => new Rule(new RegexMatcher('/(&(?:\#\d+|[a-z])+;)/si')),
53
54 2
            'comment' => new Rule(new CommentMatcher(null, [['<!--', '-->']])),
55 2
            'string'  => CommonFeatures::strings(['single' => '\'', 'double' => '"'], ['context' => ['tag']]),
56
        ]);
57 2
    }
58
59
    /** {@inheritdoc} */
60
    public function getIdentifier()
61
    {
62
        return 'xml';
63
    }
64
65
    public static function getMetadata()
66
    {
67
        return [
68
            'name'      => ['xml'],
69
            'mime'      => ['application/xml', 'text/xml'],
70
            'extension' => ['*.xml']
71
        ];
72
    }
73
}
74