Completed
Push — master ( 6465ca...dc4b6d )
by Kacper
08:36
created

Shell::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 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
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
                '/(?>(?<![^\\\]\\\)(?<=\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 1
                '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 1
            ],
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 1
                'context'  => ['!string', '!comment', '!call']
63 1
            ]),
64
65
            'expression' => [
66 1
                new Rule(new RegexMatcher('/(?=(\$\(((?>[^$()]+|(?1))+)\)))/x'), [
67 1
                    'context' => Validator::everywhere(),
68 1
                    'factory' => new TokenFactory(LanguageToken::class),
69
                    'inject'  => $this
70 1
                ]),
71 1
            ],
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 1
            ]),
76 1
        ]);
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