Completed
Push — master ( 6054a8...c11aa8 )
by Kacper
04:06
created

Shell::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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\TokenFactory;
25
use Kadet\Highlighter\Parser\Validator\Validator;
26
27
class Shell extends GreedyLanguage
28
{
29
30
    /**
31
     * Tokenization rules setup
32
     */
33
    public function setupRules()
34
    {
35
        $this->rules->addMany([
36
            'call' => new Rule(new RegexMatcher(
37
                '/(?>(?<![^\\\]\\\)(?<=\n|\(|\||;|`|^|do|if|then|else|\$\(|^\$\s)\s*(\w+))(?!\s*=)/im'
38
            ), ['priority' => 1, 'context' => ['*none', '*expression']]),
39
40
            'comment' => new Rule(new CommentMatcher(['#'])),
41
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"']),
42
43
            'keyword' => new Rule(new WordMatcher([
44
                'if', 'then', 'else', 'elif', 'fi', 'case', 'esac', 'for', 'select', 'while', 'until', 'do', 'done',
45
                'in', 'function', 'time', 'coproc'
46
            ]), ['priority' => 3]),
47
48
            'variable'  => [
49
                'assign' => new Rule(new RegexMatcher('/(\w+)\s*=/')),
50
                new Rule(new RegexMatcher('/(\$\w+)/i'), ['context' => ['*none', '*string.double']])
51
            ],
52
53
            'number'    => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')),
54
            'delimiter' => new Rule(new RegexMatcher('/^(\$)/m')),
55
56
            'symbol.parameter' => new Rule(new RegexMatcher('/\s(-{1,2}\w+=?)\b/i'), [
57
                'priority' => 0,
58
                'context'  => ['!string', '!comment', '!call']
59
            ]),
60
61
            'expression' => [
62
                new Rule(new RegexMatcher('/(?=(\$\(((?>[^$()]+|(?1))+)\)))/x'), [
63
                    'context' => Validator::everywhere(),
64
                    'factory' => new TokenFactory(LanguageToken::class),
65
                    'inject'  => $this
66
                ]),
67
            ],
68
69
            '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'), [
70
                'context' => ['string']
71
            ]),
72
        ]);
73
    }
74
75
    /**
76
     * Unique language identifier, for example 'php'
77
     *
78
     * @return string
79
     */
80
    public function getIdentifier()
81
    {
82
        return 'shell';
83
    }
84
85
    public static function getMetadata()
86
    {
87
        return [
88
            'name'      => ['shell', 'bash', 'zsh', 'sh'],
89
            'mime'      => ['text/x-shellscript', 'application/x-shellscript'],
90
            'extension' => ['*.sh', '*.zsh', '*.bash', '*.ebuild', '*.eclass', '*.exheres-0', '*.exlib', '.bashrc', 'bashrc', '.bash_*', 'bash_*', 'PKGBUILD']
91
        ];
92
    }
93
}
94