Completed
Branch 0.8-dev (8bed0e)
by Kacper
02:45
created

Python::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 92
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 92
rs 8.491
cc 1
eloc 56
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SubStringMatcher;
22
use Kadet\Highlighter\Matcher\WordMatcher;
23
use Kadet\Highlighter\Parser\CloseRule;
24
use Kadet\Highlighter\Parser\Rule;
25
use Kadet\Highlighter\Parser\Token\ContextualToken;
26
use Kadet\Highlighter\Parser\Token\TerminatorToken;
27
use Kadet\Highlighter\Parser\Token\Token;
28
use Kadet\Highlighter\Parser\TokenFactory;
29
use Kadet\Highlighter\Parser\Validator\Validator;
30
31
class Python extends Language
32
{
33
34
    /**
35
     * Tokenization rules setup
36
     */
37
    public function setupRules()
38
    {
39
        $standard = new Validator(['!string', '!comment']);
40
41
        $this->rules->addMany([
42
            'keyword' => new Rule(new WordMatcher([
43
                'and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global', 'or', 'with', 'assert', 'else', 'if',
44
                'pass', 'yield', 'break', 'except', 'import', 'print', 'class', 'exec', 'in', 'raise', 'continue',
45
                'finally', 'is', 'return', 'def', 'for', 'lambda', 'try',
46
            ])),
47
48
            'operator' => new Rule(
49
                new RegexMatcher('/([-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~])|\b(or|and|not)\b/'), [
50
                    'priority' => -1
51
                ]
52
            ),
53
54
            'expression' => new Rule(new RegexMatcher('/\{(\S+)\}/'), [
55
                'context' => ['string']
56
            ]),
57
58
            'variable' => [
59
                new Rule(new RegexMatcher('/[^\w.]([a-z_]\w*)\.\w/i')),
60
                'property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [
61
                    'priority' => -2,
62
                    'context' => ['*none', '*expression']
63
                ]),
64
            ],
65
66
67
            'symbol' => [
68
                new Rule(new RegexMatcher('/import\s+([a-z_][\w.]*)(?:\s*,\s*([a-z_][\w.]*))*/i', [
69
                    1 => Token::NAME,
70
                    2 => Token::NAME,
71
                ])),
72
                'library' => new Rule(new RegexMatcher('/from\s+([a-z_][\w.]*)\s+import/i', [
73
                    1 => Token::NAME,
74
                ]))
75
            ],
76
77
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:.|[0-7]{3}|x\x{2}))/'), [
78
                'context' => Validator::everywhere()
79
            ]),
80
81
            'comment' => new Rule(new CommentMatcher(['#'])),
82
            'constant.special' => [
83
                new Rule(new WordMatcher(['True', 'False', 'NotImplemented', 'Ellipsis'], [
84
                    'case-sensitivity' => true
85
                ])),
86
                new Rule(new RegexMatcher('/\b(__\w+__)\b/'))
87
            ],
88
            'call' => new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => 2]),
89
90
            'meta.newline' => new CloseRule(new RegexMatcher('/()\r?\n/'), [
91
                'factory' => new TokenFactory(TerminatorToken::class),
92
                'context' => ['!keyword.escape', 'string.single-line'],
93
                'closes'  => ['string.single-line.double', 'string.single-line.single']
94
            ]),
95
96
            'number' => new Rule(
97
                new RegexMatcher('/(-?(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?)\b/')
98
            ),
99
100
            'string' => [
101
                'single-line' => [
102
                    'double' => new Rule(new RegexMatcher('/(?:^|[^"])(")(?=[^"]|$)/'), [
103
                        'factory' => new TokenFactory(ContextualToken::class),
104
                        'context' => $standard,
105
                    ]),
106
                    'single' => new Rule(new RegexMatcher('/(?:^|[^\'])(\')(?=[^\']|$)/'), [
107
                        'factory' => new TokenFactory(ContextualToken::class),
108
                        'context' => $standard,
109
                    ]),
110
                ],
111
                'multi-line' => [
112
                    'double' => new Rule(new SubStringMatcher('"""'), [
113
                        'factory'  => new TokenFactory(ContextualToken::class),
114
                        'context'  => $standard,
115
                        'priority' => 2,
116
                    ]),
117
                    'single' => new Rule(new SubStringMatcher('\'\'\''), [
118
                        'factory'  => new TokenFactory(ContextualToken::class),
119
                        'context'  => $standard,
120
                        'priority' => 2,
121
                    ]),
122
                ]
123
            ],
124
125
            'symbol.function' => new Rule(new RegexMatcher('/def\s+([a-z_]\w+)\s*\(/i')),
126
            'symbol.class'    => new Rule(new RegexMatcher('/class\s+([a-z_]\w+)/i')),
127
        ]);
128
    }
129
130
    /**
131
     * Unique language identifier, for example 'php'
132
     *
133
     * @return string
134
     */
135
    public function getIdentifier()
136
    {
137
        return 'python';
138
    }
139
}
140