Passed
Branch master (f3f280)
by Maciej
03:18
created

Shell   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 67
ccs 28
cts 30
cp 0.9333
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 6 1
A getIdentifier() 0 3 1
A setupRules() 0 41 1
1
<?php
2
3
/**
4
 * Highlighter
5
 *
6
 * Copyright (C) 2016, Some right reserved.
7
 *
8
 * @author Kacper "Kadet" Donat <[email protected]>
9
 *
10
 * Contact with author:
11
 * Xmpp: [email protected]
12
 * E-mail: [email protected]
13
 *
14
 * From Kadet with love.
15
 */
16
17
namespace Kadet\Highlighter\Language;
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\LanguageToken;
24
use Kadet\Highlighter\Parser\Token\Token;
25
use Kadet\Highlighter\Parser\TokenFactory;
26
use Kadet\Highlighter\Parser\Validator\Validator;
27
28
class Shell extends GreedyLanguage
29
{
30
31
    /**
32
     * Tokenization rules setup
33
     */
34 1
    public function setupRules()
35
    {
36 1
        $this->rules->addMany([
37 1
            'call' => new Rule(new RegexMatcher(
38 1
                '/(?>(?<![^\\\]\\\)(?<=\n|\(|\)|\||;|`|^|do|if|then|else|fi|esac|done\$\(|^\$\s)\s*\b(\w+))(?!\s*=)/i'
39 1
            ), ['priority' => 1, 'context' => ['*none', '*expression']]),
40
41 1
            'comment' => new Rule(new CommentMatcher(['#'])),
42 1
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"']),
43
44 1
            'keyword' => new Rule(new WordMatcher([
45 1
                'if', 'then', 'else', 'elif', 'fi', 'case', 'esac', 'for', 'select', 'while', 'until', 'do', 'done',
46
                'in', 'function', 'time', 'coproc'
47 1
            ]), ['priority' => 3]),
48
49
            'variable'  => [
50 1
                'assign' => new Rule(new RegexMatcher('/(\w+)[+-]?=/')),
51 1
                new Rule(new RegexMatcher('/(\$\w+)/i'), ['context' => ['*none', '*string.double']]),
52 1
                'special'  => new Rule(new RegexMatcher('/(\$[#@_])/i'), ['context' => ['*none', '*string.double']]),
53 1
                'argument' => new Rule(new RegexMatcher('/(\$\d+)/i'), ['context' => ['*none', '*string.double']]),
54 1
                new Rule(new RegexMatcher('/\$\{(\w+)(.*?)\}/i', [ 1 => Token::NAME, 2 => 'string' ]), ['context' => ['*none', '*string.double']])
55
            ],
56
57 1
            'number'    => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')),
58 1
            'delimiter' => new Rule(new RegexMatcher('/^(\$)/m')),
59
60 1
            'symbol.parameter' => new Rule(new RegexMatcher('/[\s|](-{1,2}[\w-]+=?)\b/i'), [
61 1
                'priority' => 0,
62
                'context'  => ['!string', '!comment', '!call']
63
            ]),
64
65
            'expression' => [
66 1
                new Rule(new RegexMatcher('/(?=(\$\(((?>[^$()]+|(?1))+)\)))/x'), [
67 1
                    'context' => Validator::everywhere(),
68 1
                    'factory' => new TokenFactory(LanguageToken::class),
69 1
                    'inject'  => $this
70
                ]),
71
            ],
72
73 1
            'operator.escape' => new Rule(new RegexMatcher('/(\\\(?:x[0-9a-fA-F]{1,2}|u\{[0-9a-fA-F]{1,6}\}|[0-7]{1,3}|.))/i'), [
74 1
                'context' => ['*']
75
            ]),
76
        ]);
77 1
    }
78
79
    /**
80
     * Unique language identifier, for example 'php'
81
     *
82
     * @return string
83
     */
84 1
    public function getIdentifier()
85
    {
86 1
        return 'shell';
87
    }
88
89
    public static function getMetadata()
90
    {
91
        return [
92
            'name'      => ['shell', 'bash', 'zsh', 'sh'],
93
            'mime'      => ['text/x-shellscript', 'application/x-shellscript'],
94
            'extension' => ['*.sh', '*.zsh', '*.bash', '*.ebuild', '*.eclass', '*.exheres-0', '*.exlib', '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'PKGBUILD']
95
        ];
96
    }
97
}
98