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