blob: 76cbc01d41429cf28e391f69cd905a2d3545b41f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<?php
/**
* Scanner.php - template parser lexical scanner
*
* Simple wrapper class around the Zend engine's lexical scanner. It
* automatically skips whitespace.
*
* @copyright 2013 Elmar Ludwig
* @license GPL2 or any later version
*/
namespace exTpl;
class Scanner
{
private array $tokens;
private mixed $token_type;
private mixed $token_value;
/**
* Initializes a new Scanner instance for the given text.
*
* @param string $text string to parse
*/
public function __construct(string $text)
{
$this->tokens = token_get_all('<?php ' . $text);
}
/**
* Advances the scanner to the next token and returns its token type.
* The valid token types are those defined for token_get_all() in the
* PHP documentation. Returns false when the end of input is reached.
*/
public function nextToken(): mixed
{
do {
$token = next($this->tokens);
$key = key($this->tokens);
// FIXME this workaround should be dropped
while (
$token && $token[0] === T_STRING
&& isset($this->tokens[$key + 2])
&& $this->tokens[++$key] === '-'
&& $this->tokens[++$key][0] === T_STRING
) {
$token[1] .= '-' . $this->tokens[$key][1];
next($this->tokens);
next($this->tokens);
}
} while (is_array($token) && $token[0] === T_WHITESPACE);
if (is_string($token) || $token === false) {
$this->token_type = $token;
$this->token_value = null;
} else {
$this->token_type = $token[0];
$this->token_value = match ($token[0]) {
T_CONSTANT_ENCAPSED_STRING => stripcslashes(substr($token[1], 1, -1)),
T_DNUMBER => (double) $token[1],
T_LNUMBER => (int) $token[1],
default => $token[1],
};
}
return $this->token_type;
}
/**
* Returns the current token type. The valid token types are
* those defined for token_get_all() in the PHP documentation.
*/
public function tokenType(): mixed
{
return $this->token_type;
}
/**
* Returns the current token value if the token type supports
* a value (T_STRING, T_LNUMBER etc.). Returns null otherwise.
*/
public function tokenValue(): mixed
{
return $this->token_value;
}
}
|