Completed
Push — master ( 5dda48...9477b8 )
by Kacper
03:00
created

Go::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
            'constant.special' => new Rule(new WordMatcher(['true', 'false', 'iota'])),
49
            'type' => new Rule(new RegexMatcher('/((?:\*\s*)?(?:u?int(?:8|16|32|64|ptr)?|float(?:32|64)|complex(?:64|128)|byte|rune|string|error))/')),
50
            'symbol.function' => new Rule(new RegexMatcher("/func ($identifier)/")),
51
            'call' => new Rule(new RegexMatcher("/($identifier)\\s*\\(/i"), ['priority' => -1]),
52
            'operator' => [
53
                new Rule(new RegexMatcher('~((?>&{2}|<-|[+-]{2}|\|\||[+&=!/:%*^-|]?=|<{1,2}=?|>{1,2}=?|&^=?))~si')),
54
                'punctuation' => new Rule(new RegexMatcher('/([;,]|\.\.\.)/'))
55
            ]
56
        ]);
57
    }
58
59
    /**
60
     * Unique language identifier, for example 'php'
61
     *
62
     * @return string
63
     */
64
    public function getIdentifier()
65
    {
66
        return 'go';
67
    }
68
}
69