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

Xml::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
 *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
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\ContextualToken;
23
use Kadet\Highlighter\Parser\OpenRule;
24
use Kadet\Highlighter\Parser\Rule;
25
use Kadet\Highlighter\Parser\Token;
26
use Kadet\Highlighter\Parser\TokenFactory;
27
28
class Xml extends Language
29
{
30
    const IDENTIFIER = '(?P<namespace>[\w\.-]+:)?(?P<name>[\w\.-]+)';
31
32
    public function getRules()
33
    {
34
        return [
35
            'tag.open' => [
36
                new OpenRule(new RegexMatcher('/(<\w)/'), ['context' => ['!tag', '!comment']]),
37
                new CloseRule(new SubStringMatcher('>'), ['context' => ['!string', '!comment']])
38
            ],
39
40
            'tag.close' => new Rule(new RegexMatcher('/(<\/(?:\w+:)?(?:[\w\.]+)>)/')),
41
42
            'symbol.tag' => new Rule(new RegexMatcher('/<\\/?' . self::IDENTIFIER . '/', [
43
                'name' => Token::NAME,
44
                'namespace' => '$.namespace'
45
            ]), ['context' => ['tag', '!string']]),
46
47
            'symbol.attribute' => new Rule(new RegexMatcher('/' . self::IDENTIFIER . '=/', [
48
                'name' => Token::NAME,
49
                'namespace' => '$.namespace'
50
            ]), ['context' => ['tag', '!string']]),
51
52
            'string.single' => new Rule(new SubStringMatcher('\''), [
53
                'context' => ['tag'],
54
                'factory' => new TokenFactory(ContextualToken::class),
55
            ]),
56
57
            'string.double' => new Rule(new SubStringMatcher('"'), [
58
                'context' => ['tag'],
59
                'factory' => new TokenFactory(ContextualToken::class),
60
            ]),
61
62
63
            'comment' => new Rule(new CommentMatcher([], [['<!--', '-->']])),
64
65
            'constant.entity' => new Rule(new RegexMatcher('/(&[a-z]+;)/si')),
66
        ];
67
    }
68
69
70
    public function getIdentifier()
71
    {
72
        return 'xml';
73
    }
74
}
75