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

Latex::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 47
rs 9.0303
cc 1
eloc 27
nc 1
nop 0
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
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\WordMatcher;
21
use Kadet\Highlighter\Parser\Rule;
22
use Kadet\Highlighter\Parser\Token\Token;
23
use Kadet\Highlighter\Parser\Validator\Validator;
24
25
class Latex extends Language
26
{
27
    protected static $mathEnvironments = ['align', 'equation', 'math'];
28
29
    /**
30
     * Tokenization rules
31
     *
32
     * @return \Kadet\Highlighter\Parser\Rule[]|\Kadet\Highlighter\Parser\Rule[][]
33
     */
34
    public function setupRules()
35
    {
36
        $this->rules->addMany([
37
            'call.symbol' => new Rule(new RegexMatcher('/(\\\[a-z]+)/si'), ['context' => Validator::everywhere(), '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' => Validator::everywhere(
58
                )
59
                ]),
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
    /** {@inheritdoc} */
83
    public function getIdentifier()
84
    {
85
        return 'latex';
86
    }
87
}
88