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

Perl::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 84
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 84
rs 8.7169
cc 1
eloc 52
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\OpenRule;
25
use Kadet\Highlighter\Parser\Rule;
26
use Kadet\Highlighter\Parser\Token\ContextualToken;
27
use Kadet\Highlighter\Parser\Token\LanguageToken;
28
use Kadet\Highlighter\Parser\Token\Token;
29
use Kadet\Highlighter\Parser\TokenFactory;
30
use Kadet\Highlighter\Parser\Validator\Validator;
31
32
class Perl extends Language
33
{
34
    /**
35
     * Tokenization rules definition
36
     *
37
     * @return Rule[]|Rule[][]
38
     */
39
    public function setupRules()
40
    {
41
        $identifier = '\w+';
42
        $number = '[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?';
43
44
        $this->rules->addMany([
45
            'comment' => new Rule(new CommentMatcher(['#'], [])),
46
47
            'keyword' => new Rule(new WordMatcher([
48
                'case', 'continue', 'do', 'else', 'elsif', 'for', 'foreach',
49
                'if', 'last', 'my', 'next', 'our', 'redo', 'reset', 'then',
50
                'unless', 'until', 'while', 'use', 'print', 'new', 'BEGIN',
51
                'sub', 'CHECK', 'INIT', 'END', 'return', 'exit'
52
            ])),
53
54
            'string.single' => new Rule(new SubStringMatcher('\''), [
55
                'context' => ['!keyword', '!comment', '!string', '!language', '!number'],
56
                'factory' => new TokenFactory(ContextualToken::class),
57
            ]),
58
59
            'string.double' => new Rule(new SubStringMatcher('"'), [
60
                'context' => ['!keyword', '!comment', '!string', '!language', '!number'],
61
                'factory' => new TokenFactory(ContextualToken::class),
62
            ]),
63
64
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/'), [
65
                'context' => ['string']
66
            ]),
67
68
            'string.nowdoc'  => new Rule(
69
                new RegexMatcher('/<<\s*\'(\w+)\';(?P<string>.*?)\n\1/sm', [
70
                    'string' => Token::NAME,
71
                          0  => 'keyword.nowdoc'
72
                ]), ['context' => ['!comment']]
73
            ),
74
75
            'language.shell' => new Rule(new SubStringMatcher('`'), [
76
                'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'],
77
                'factory' => new TokenFactory(ContextualToken::class),
78
            ]),
79
80
            'variable.scalar' => new Rule(new RegexMatcher("/(\\\$$identifier)/")),
81
            'variable.array'  => new Rule(new RegexMatcher("/(\\@$identifier)/")),
82
            'variable.hash'   => new Rule(new RegexMatcher("/(\\%$identifier)/")),
83
84
            'variable.property'   => new Rule(new RegexMatcher("/\\\$$identifier{($identifier)}/")),
85
86
            // Stupidly named var? Perl one, for sure.
87
            'variable.special'   => new Rule(new RegexMatcher('/([$@%][^\s\w]+[\w]*)/')),
88
89
            'operator' => [
90
                new Rule(new RegexMatcher('/(-[rwxoRWXOezsfdlpSbctugkTBMAC])/')),
91
                new Rule(new WordMatcher([
92
                    'not', 'and', 'or', 'xor', 'goto', 'last', 'next', 'redo', 'dump',
93
                    'eq', 'ne', 'cmp', 'not', 'and', 'or', 'xor'
94
                ], ['atomic' => true])),
95
            ],
96
97
            'call' => new Rule(new RegexMatcher('/([a-z]\w+)(?:\s*\(|\s+[$%@"\'`{])/i')),
98
99
            'number' => [
100
                new Rule(new RegexMatcher("/(\\b|\"|')$number\\1/", [
101
                    0 => Token::NAME
102
                ]), ['priority' => 5]),
103
            ],
104
105
            'string.regex' => [
106
                new OpenRule(new RegexMatcher('#~\s*[ms]?(/).*?/#m'), [
107
                    'context' => Validator::everywhere()
108
                ]),
109
                new OpenRule(new RegexMatcher('#~\s*(s/).*?/#m')),
110
111
                new Rule(new RegexMatcher('#(?=\/.*?(/[gimuy]{0,5}))#m'), [
112
                    'priority' => 1,
113
                    'factory'  => new TokenFactory(ContextualToken::class),
114
                    'context'  => ['!keyword.escape', 'string.regex']
115
                ])
116
            ],
117
            
118
            'symbol.iterator' => [
119
                new Rule(new RegexMatcher('#(<\w+>)#s'))
120
            ]
121
        ]);
122
    }
123
124
    /**
125
     * Unique language identifier, for example 'php'
126
     *
127
     * @return string
128
     */
129
    public function getIdentifier()
130
    {
131
        return 'perl';
132
    }
133
134
    public function getEnds($embedded = false)
135
    {
136
        return [
137
            parent::getEnds($embedded),
138
            new CloseRule(new SubStringMatcher('__END__'), [
139
                'factory'  => new TokenFactory(LanguageToken::class),
140
                'language' => $this
141
            ])
142
        ];
143
    }
144
}