Completed
Push — master ( dc4b6d...04cf1c )
by Kacper
04:06
created

Go   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 5.88%

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 2
cts 34
cp 0.0588
wmc 3
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setupRules() 0 34 1
A getMetadata() 0 8 1
A getIdentifier() 0 4 1
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
24
class Go extends GreedyLanguage
25
{
26
27
    /**
28
     * Tokenization rules setup
29
     */
30
    public function setupRules()
31
    {
32
        $identifier = '[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*';
33
        $this->rules->addMany([
34
            'comment'          => new Rule(new CommentMatcher(['//'], [['/*', '*/']])),
35
            'keyword'          => new Rule(new WordMatcher([
36
                'break', 'default', 'func', 'interface', 'select', 'case', 'defer', 'go', 'map', 'struct', 'chan',
37
                'else', 'goto', 'package', 'switch', 'const', 'fallthrough', 'if', 'range', 'type', 'continue', 'for',
38
                'import', 'return', 'var',
39
            ])),
40
            'number'           => [
41
                'integer' => new Rule(new RegexMatcher('/\b([1-9]\d*|0x\x+|[0-7]+)\b/si')),
42
                'float'   => new Rule(new RegexMatcher('/\v((?:\d+\.\d*(?P<exponent>e[+-]?\d+)?|\.\d+(?&exponent)?|\d+(?&exponent))i?)\b/si')),
43
            ],
44
            'string'           => [
45
                'rune' => new Rule(new RegexMatcher('/(\'(?:\\\(?:[abfnrtv\\\'"]|[0-7]{3}|x\x{2})|u\x{4}|U\x{8})\')/si')),
46
                CommonFeatures::strings(['single' => '`', 'double' => '"']),
47
            ],
48
49
            'constant.special' => new Rule(new WordMatcher(['true', 'false', 'iota'])),
50
            'type'             => new Rule(new RegexMatcher('/((?:\*\s*)?(?:u?int(?:8|16|32|64|ptr)?|float(?:32|64)|complex(?:64|128)|byte|rune|string|error))/')),
51
            'symbol'           => [
52
                'function'  => new Rule(new RegexMatcher("/func\\s*($identifier)/")),
53
                'struct'    => new Rule(new RegexMatcher("/type\\s*($identifier)\\s*struct/")),
54
                'interface' => new Rule(new RegexMatcher("/type\\s*($identifier)\\s*interface/")),
55
            ],
56
            'call'             => new Rule(new RegexMatcher("/($identifier)\\s*\\(/i"), ['priority' => -1]),
57
58
            'operator' => [
59
                new Rule(new RegexMatcher('~((?>&{2}|<-|[+-]{2}|\|\||[+&=!/:%*^-|]?=|<{1,2}=?|>{1,2}=?|&^=?))~si')),
60
                'punctuation' => new Rule(new RegexMatcher('/([;,]|\.\.\.)/')),
61
            ],
62
        ]);
63
    }
64
65
    /**
66
     * Unique language identifier, for example 'php'
67
     *
68
     * @return string
69
     */
70 1
    public function getIdentifier()
71
    {
72 1
        return 'go';
73
    }
74
75
    public static function getMetadata()
76
    {
77
        return [
78
            'name'      => ['go', 'golang'],
79
            'mime'      => ['text/x-go', 'application/x-go', 'text/x-golang', 'application/x-golang'],
80
            'extension' => ['*.go']
81
        ];
82
    }
83
}
84