Completed
Push — master ( 5ee129...947530 )
by Kacper
05:05
created

LatexLanguage   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 47 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 LatexLanguage 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
            /*'string' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
                new OpenRule(new SubStringMatcher('{'), ['context' => ['!!'], 'inside' => true, 'priority' => 0]),
40
                new CloseRule(new SubStringMatcher('}'), ['inside' => true, 'priority' => -2])
41
            ],*/
42
43
            'string.math' => [
44
                new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')),
45
                new Rule(
46
                    new RegexMatcher(
47
                        '/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s',
48
                        [2 => Token::NAME]
49
                    )
50
                ),
51
            ],
52
53
            'symbol.argument'    => new Rule(new RegexMatcher('/\[(.*?)\]/')),
54
            'symbol.environment' => new Rule(new RegexMatcher('/\\\(?:begin|end){(.*?)}/')),
55
56
            'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')),
57
58
            'operator' => [
59
                new Rule(new WordMatcher(['*', '&', '\\\\'], ['separated' => false]), ['context' => ['*']]),
60
                new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [
61
                    'context' => ['string.math'],
62
                    'priority' => -1
63
                ]),
64
            ],
65
            'comment' => new Rule(new CommentMatcher(['%'], [])),
66
67
            'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [
68
                2 => Token::NAME
69
            ])),
70
            'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [
71
                2 => Token::NAME
72
            ])),
73
74
            # math mode
75
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,]+))/'), [
76
                'context' => ['string.math'],
77
                'priority' => -2
78
            ]),
79
        ];
80
    }
81
82
    /**
83
     * Unique language identifier, for example 'php'
84
     *
85
     * @return string
86
     */
87
    public function getIdentifier()
88
    {
89
        return 'latex';
90
    }
91
}