Completed
Push — master ( 7e5b44...8768bd )
by Kacper
04:26
created

Haskell::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
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
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\WordMatcher;
21
use Kadet\Highlighter\Parser\Rule;
22
23
class Haskell extends GreedyLanguage
24
{
25
    
26
    protected $_keywords = [
27
        'as', '(?:data|type)(?:\s+(?:instance|family))?', 'default', 'deriving(?:\s+instance)?', 'forall', 'foreign',
28
        'hiding', 'infix[lr]?', 'import', 'instance', 'mdo', 'module', 'proc', 'qualified', 'rec', 'where', 'do'
29
    ];
30
31
    /**
32
     * Tokenization rules
33
     */
34
    public function setupRules()
35
    {
36
        $this->rules->addMany([
37
            'keyword' => [
38
                new Rule(new WordMatcher($this->_keywords, ['escape' => false, 'separated' => true]))
39
            ],
40
41
            'constant' => new Rule(new WordMatcher(['False', 'True', 'Nothing']), ['priority' => 3]),
42
            'comment'  => new Rule(new CommentMatcher(['--'], [['{-', '-}']]), ['priority' => 20]),
43
44
            'symbol.type' => new Rule(new RegexMatcher('/\b([A-Z]\w*)\b/'), ['priority' => 1]),
45
            'symbol.function' => new Rule(new RegexMatcher('/([a-z]\w*)\s*::/')),
46
            'operator.named' => new Rule(new RegexMatcher('/(`\w+`)/')),
47
48
            'string'   => CommonFeatures::strings(['single' => '\'', 'double' => '"']),
49
50
            'number'          => new Rule(new RegexMatcher('/\b(\d+(?>(?>\.|e[+-]?)\d+)?|0o[0-7]+|0x[0-9a-f])\b/i'), ['context' => ['!string', '!comment']]),
51
            'operator.escape' => new Rule(new RegexMatcher('/(\\[\\0\'|bnrtZ%_])/'), ['context' => ['string']]),
52
            'operator'        => new Rule(new RegexMatcher('/([^\r\n\s\w\"\'\`\[\]\(\)\{\}]+)/'), [
53
                'priority' => -1,
54
                'context'  => ['!comment', '!string']
55
            ])
56
        ]);
57
    }
58
59
    /** {@inheritdoc} */
60
    public function getIdentifier()
61
    {
62
        return 'haskell';
63
    }
64
65
    public static function getMetadata()
66
    {
67
        return [
68
            'name'      => ['haskell'],
69
            'mime'      => ['text/x-haskell'],
70
            'extension' => ['*.hs']
71
        ];
72
    }
73
}
74