Completed
Push — master ( 51cde5...f3ba84 )
by Kacper
02:21
created

JavaScriptLanguage::getRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 1
eloc 29
nc 1
nop 0
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, 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
18
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\SubStringMatcher;
21
use Kadet\Highlighter\Matcher\WordMatcher;
22
use Kadet\Highlighter\Parser\Rule;
23
use Kadet\Highlighter\Parser\TokenFactory;
24
25
class JavaScriptLanguage extends Language
26
{
27
    const IDENTIFIER = '[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*';
28
29
    public function getRules()
30
    {
31
        return [
32
            'string.single' => new Rule(new SubStringMatcher('\''), [
33
                'context' => ['!keyword.escape', '!comment', '!string', '!keyword.nowdoc'],
34
                'factory' => new TokenFactory('Kadet\\Highlighter\\Parser\\MarkerToken'),
35
            ]),
36
37
            'string.double' => new Rule(new SubStringMatcher('"'), [
38
                'context' => ['!keyword.escape', '!comment', '!string'],
39
                'factory' => new TokenFactory('Kadet\\Highlighter\\Parser\\MarkerToken'),
40
            ]),
41
42
            'variable' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')/iu'), ['priority' => -10000]),
43
            'variable.property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [
44
                'priority' => -2
45
            ]),
46
47
            'symbol.function' => new Rule(new RegexMatcher('/function\s+([a-z_]\w+)\s*\(/i')),
48
49
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\(?:x[0-9a-fA-F]{1,2}|u\{[0-9a-fA-F]{1,6}\}|[0-7]{1,3}|.))/i'), [
50
                'context' => ['string']
51
            ]),
52
53
            'comment' => new Rule(new CommentMatcher(['//'], [
54
                ['/* ', '*/']
55
            ])),
56
57
            'call' => new Rule(new RegexMatcher('/(' . self::IDENTIFIER . ')\s*\(/iu'), ['priority' => -1]),
58
59
            'keyword' => new Rule(new WordMatcher([
60
                'do', 'if', 'in', 'for', 'let', 'new', 'try', 'var', 'case', 'else', 'enum', 'eval', 'false',
61
                'null', 'this', 'true', 'void', 'with', 'break', 'catch', 'class', 'const', 'super', 'throw',
62
                'while', 'yield', 'delete', 'export', 'import', 'public', 'return', 'static', 'switch',
63
                'typeof', 'default', 'extends', 'finally', 'package', 'private', 'continue', 'debugger',
64
                'function', 'arguments', 'interface', 'protected', 'implements', 'instanceof',
65
            ]), ['context' => ['!string', '!variable', '!comment']]),
66
67
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|\d+))/')),
68
69
            'operator.punctuation' => new Rule(new WordMatcher([',', ';'], ['separated' => false]), ['priority' => 0]),
70
            'operator' => new Rule(new WordMatcher([
71
                '->', '++', '--', '-', '+', '/', '*', '**', '||', '&&', '^', '%', '&', '@', '!', '|', ':', '.'
72
            ], ['separated' => false]), ['priority' => 0])
73
        ];
74
    }
75
76
    public function getIdentifier()
77
    {
78
        return 'javascript';
79
    }
80
}