1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PhpUnitGen. |
5
|
|
|
* |
6
|
|
|
* (c) 2017-2018 Paul Thébaud <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhpUnitGen\Annotation; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Lexer\AbstractLexer; |
15
|
|
|
use PhpUnitGen\Exception\Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AnnotationLexer. |
19
|
|
|
* |
20
|
|
|
* @author Paul Thébaud <[email protected]>. |
21
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
22
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
23
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
24
|
|
|
* @since Class available since Release 2.0.0. |
25
|
|
|
*/ |
26
|
|
|
class AnnotationLexer extends AbstractLexer |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* All possible tokens. |
30
|
|
|
*/ |
31
|
|
|
public const T_ANNOTATION = 0; |
32
|
|
|
public const T_O_PARENTHESIS = 1; |
33
|
|
|
public const T_C_PARENTHESIS = 2; |
34
|
|
|
public const T_BACKSLASH = 3; |
35
|
|
|
public const T_SINGLE_QUOTE = 4; |
36
|
|
|
public const T_DOUBLE_QUOTE = 5; |
37
|
|
|
public const T_ASTERISK = 6; |
38
|
|
|
public const T_LINE_BREAK = 7; |
39
|
|
|
public const T_WHITESPACE = 8; |
40
|
|
|
public const T_OTHER = 9; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array PATTERNS_TOKENS Matching array between regex pattern and token. |
44
|
|
|
*/ |
45
|
|
|
public const PATTERNS_TOKENS = [ |
46
|
|
|
'(@(?i)(PhpUnitGen|Pug)\\\\[a-zA-Z0-9]+)' => AnnotationLexer::T_ANNOTATION, |
47
|
|
|
'(\()' => AnnotationLexer::T_O_PARENTHESIS, |
48
|
|
|
'(\))' => AnnotationLexer::T_C_PARENTHESIS, |
49
|
|
|
'(\\\\)' => AnnotationLexer::T_BACKSLASH, |
50
|
|
|
'(\')' => AnnotationLexer::T_SINGLE_QUOTE, |
51
|
|
|
'(")' => AnnotationLexer::T_DOUBLE_QUOTE, |
52
|
|
|
'(\*)' => AnnotationLexer::T_ASTERISK, |
53
|
|
|
'(\r|\n|\r\n|\n\r)' => AnnotationLexer::T_LINE_BREAK, |
54
|
|
|
'(\s)' => AnnotationLexer::T_WHITESPACE, |
55
|
|
|
'([^\\\\"\'\(\)\*\s]+)' => AnnotationLexer::T_OTHER, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string[] $patterns Array containing regex patterns. |
60
|
|
|
*/ |
61
|
|
|
private $patterns; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Lexer constructor. |
65
|
|
|
*/ |
66
|
|
|
public function __construct() |
67
|
|
|
{ |
68
|
|
|
$this->patterns = array_keys(AnnotationLexer::PATTERNS_TOKENS); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
protected function getCatchablePatterns(): array |
75
|
|
|
{ |
76
|
|
|
return $this->patterns; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected function getNonCatchablePatterns(): array |
83
|
|
|
{ |
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected function getType(&$value): int |
91
|
|
|
{ |
92
|
|
|
foreach ($this->getPatternsToken() as $pattern => $token) { |
93
|
|
|
if (preg_match($pattern, $value)) { |
94
|
|
|
return $token; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
throw new Exception('Invalid value given to lexer'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array All matchable patterns with corresponding tokens identifiers. |
102
|
|
|
*/ |
103
|
|
|
protected function getPatternsToken(): array |
104
|
|
|
{ |
105
|
|
|
return AnnotationLexer::PATTERNS_TOKENS; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|