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

JavaScript::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
 * @author Kacper "Kadet" Donat <[email protected]>
7
 *
8
 * Contact with author:
9
 * Xmpp: [email protected]
10
 * E-mail: [email protected]
11
 *
12
 * From Kadet with love.
13
 */
14
15
namespace Kadet\Highlighter\Language;
16
17
use Kadet\Highlighter\Matcher\CommentMatcher;
18
use Kadet\Highlighter\Matcher\RegexMatcher;
19
use Kadet\Highlighter\Matcher\SubStringMatcher;
20
use Kadet\Highlighter\Matcher\WordMatcher;
21
use Kadet\Highlighter\Parser\Token\ContextualToken;
22
use Kadet\Highlighter\Parser\OpenRule;
23
use Kadet\Highlighter\Parser\Rule;
24
use Kadet\Highlighter\Parser\TokenFactory;
25
26
/**
27
 * Class JavaScriptLanguage
28
 *
29
 * @package Kadet\Highlighter\Language
30
 *
31
 * @property bool $variables
32
 */
33
class JavaScript extends GreedyLanguage
34
{
35
    
36
    protected $_options = [
37
        'variables' => false,
38
    ];
39
40
    const IDENTIFIER = '[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*';
41
42
    /**
43
     * Tokenization rules
44
     */
45
    public function setupRules()
46
    {
47
        $this->rules->addMany([
48
            'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], [
49
                'context' => ['!operator.escape', '!comment', '!string'],
50
            ]),
51
52
            'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [
53
                'priority' => -2
54
            ]),
55
56
            'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')),
57
58
            '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'), [
59
                'context' => ['string']
60
            ]),
61
62
            'comment' => new Rule(new CommentMatcher(['//'], [['/*', '*/']]), ['priority' => 3]),
63
64
            'call' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')\s*\(/iu'), ['priority' => -1]),
65
66
            'keyword' => new Rule(new WordMatcher([
67
                'do', 'if', 'in', 'for', 'let', 'new', 'try', 'var', 'case', 'else', 'enum', 'eval',
68
                'void', 'with', 'break', 'catch', 'class', 'const', 'super', 'throw',
69
                'while', 'yield', 'delete', 'export', 'import', 'public', 'return', 'static', 'switch',
70
                'typeof', 'default', 'extends', 'finally', 'package', 'private', 'continue', 'debugger',
71
                'function', 'arguments', 'interface', 'protected', 'implements', 'instanceof', 'get', 'set', 'from'
72
            ]), ['context' => ['!string', '!comment', '!symbol', '!call']]),
73
74
            'constant.special' => new Rule(new WordMatcher(['null', 'true', 'false'])),
75
            'variable.special' => new Rule(new SubStringMatcher('this')),
76
77
            'number' => new Rule(new RegexMatcher('/\b(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))\b/')),
78
79
            'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), ['priority' => 0]),
80
            'operator'             => new Rule(new RegexMatcher('/(=>|\+{1,2}|-{1,2}|={1,3}|\|{1,2}|&{1,2})/'), ['priority' => 0]),
81
82
            'string.regex' => [
83
                new OpenRule(new RegexMatcher('#(?>[\[=(?:+,!]|^|return|=>|&&|\|\|)\s*(/).*?/#m'), [
84
                    'context' => ['!comment']
85
                ]),
86
                new Rule(new RegexMatcher('#\/.*(/[gimuy]{0,5})#m'), [
87
                    'priority' => 1,
88
                    'factory'  => new TokenFactory(ContextualToken::class),
89
                    'context'  => ['!operator.escape', 'string.regex']
90
                ])
91
            ],
92
93
            'variable' => new Rule(new RegexMatcher('/\b(?<!\.)(' . self::IDENTIFIER . ':?)/iu'), [
94
                'priority' => -1,
95
                'enabled'  => $this->variables
96
            ])
97
        ]);
98
    }
99
100
    public function getIdentifier()
101
    {
102
        return 'javascript';
103
    }
104
105
    public static function getMetadata()
106
    {
107
        return [
108
            'name'      => ['js', 'jscript', 'javascript'],
109
            'mime'      => ['application/javascript', 'application/x-javascript', 'text/x-javascript', 'text/javascript'],
110
            'extension' => ['*.js', '*.jsx'],
111
        ];
112
    }
113
}
114