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

Latex   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getRules() 0 45 1
A getIdentifier() 0 4 1
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\Rule;
23
use Kadet\Highlighter\Parser\Token;
24
25
class Latex extends Language
26
{
27
    protected static $mathEnvironments = ['align', 'equation', 'math'];
28
29
    /**
30
     * Tokenization rules definition
31
     *
32
     * @return array
33
     */
34
    public function getRules()
35
    {
36
        return [
37
            'call.symbol' => new Rule(new RegexMatcher('/(\\\[a-z]+)/si'), ['context' => ['!!'], 'priority' => -1]),
38
39
            'string.math' => [
40
                new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')),
41
                new Rule(new RegexMatcher('/(\\\\\(.*?\\\\\))/s')),
42
                new Rule(new RegexMatcher('/(\\\\\[.*?\\\\\])/s')),
43
                new Rule(
44
                    new RegexMatcher(
45
                        '/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s',
46
                        [2 => Token::NAME]
47
                    )
48
                ),
49
            ],
50
51
            'symbol.argument'    => new Rule(new RegexMatcher('/\[(.*?)\]/')),
52
            'symbol.environment' => new Rule(new RegexMatcher('/\\\(?:begin|end){(.*?)}/')),
53
54
            'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')),
55
56
            'operator' => [
57
                new Rule(new WordMatcher(['*', '&', '\\\\'], ['separated' => false]), ['context' => ['!!']]),
58
                new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [
59
                    'context' => ['string.math'],
60
                    'priority' => -1
61
                ]),
62
            ],
63
            'comment' => new Rule(new CommentMatcher(['%'], [])),
64
65
            'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [
66
                2 => Token::NAME
67
            ])),
68
            'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [
69
                2 => Token::NAME
70
            ])),
71
72
            # math mode
73
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,]+))/'), [
74
                'context' => ['string.math'],
75
                'priority' => -2
76
            ]),
77
        ];
78
    }
79
80
    /**
81
     * Unique language identifier, for example 'php'
82
     *
83
     * @return string
84
     */
85
    public function getIdentifier()
86
    {
87
        return 'latex';
88
    }
89
}