|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Yacc package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace PhpYacc\Yacc; |
|
11
|
|
|
|
|
12
|
|
|
use PhpYacc\Exception\LexingException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Token. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Token |
|
18
|
|
|
{ |
|
19
|
|
|
public const T_EOF = -1; |
|
20
|
|
|
public const T_UNKNOWN = 0; |
|
21
|
|
|
public const T_NAME = 1; |
|
22
|
|
|
public const T_NUMBER = 2; |
|
23
|
|
|
public const T_COLON = 3; |
|
24
|
|
|
public const T_SPACE = 4; |
|
25
|
|
|
public const T_NEWLINE = 5; |
|
26
|
|
|
public const T_MARK = 6; |
|
27
|
|
|
public const T_BEGIN_INC = 7; |
|
28
|
|
|
public const T_END_INC = 8; |
|
29
|
|
|
public const T_TOKEN = 9; |
|
30
|
|
|
public const T_LEFT = 10; |
|
31
|
|
|
public const T_RIGHT = 11; |
|
32
|
|
|
public const T_NON_ASSOC = 12; |
|
33
|
|
|
public const T_PRECTOK = 13; |
|
34
|
|
|
public const T_TYPE = 14; |
|
35
|
|
|
public const T_UNION = 15; |
|
36
|
|
|
public const T_START = 16; |
|
37
|
|
|
public const T_COMMENT = 17; |
|
38
|
|
|
public const T_EXPECT = 18; |
|
39
|
|
|
public const T_PURE_PARSER = 19; |
|
40
|
|
|
public const T_STRING = 20; |
|
41
|
|
|
public const T_COMMA = 21; |
|
42
|
|
|
public const T_SEMICOLON = 22; |
|
43
|
|
|
public const T_DOLLAR = 23; |
|
44
|
|
|
|
|
45
|
|
|
private const TOKEN_MAP = [ |
|
46
|
|
|
self::T_NAME => 'NAME', |
|
47
|
|
|
self::T_NUMBER => 'NUMBER', |
|
48
|
|
|
self::T_COLON => 'COLON', |
|
49
|
|
|
self::T_SPACE => 'SPACE', |
|
50
|
|
|
self::T_NEWLINE => 'NEWLINE', |
|
51
|
|
|
self::T_MARK => 'MARK', |
|
52
|
|
|
self::T_BEGIN_INC => 'BEGININC', |
|
53
|
|
|
self::T_END_INC => 'ENDINC', |
|
54
|
|
|
self::T_TOKEN => 'TOKEN', |
|
55
|
|
|
self::T_LEFT => 'LEFT', |
|
56
|
|
|
self::T_RIGHT => 'RIGHT', |
|
57
|
|
|
self::T_NON_ASSOC => 'NONASSOC', |
|
58
|
|
|
self::T_PRECTOK => 'PRECTOK', |
|
59
|
|
|
self::T_TYPE => 'TYPE', |
|
60
|
|
|
self::T_UNION => 'UNION', |
|
61
|
|
|
self::T_START => 'START', |
|
62
|
|
|
self::T_COMMENT => 'COMMENT', |
|
63
|
|
|
self::T_EXPECT => 'EXPECT', |
|
64
|
|
|
self::T_PURE_PARSER => 'PURE_PARSER', |
|
65
|
|
|
self::T_EOF => 'EOF', |
|
66
|
|
|
self::T_UNKNOWN => 'UNKNOW', |
|
67
|
|
|
self::T_STRING => 'STRING', |
|
68
|
|
|
self::T_COMMA => 'COMMA', |
|
69
|
|
|
self::T_SEMICOLON => 'SEMICOLON', |
|
70
|
|
|
self::T_DOLLAR => 'DOLLAR', |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var int |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $type; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $value; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var int |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $line; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $filename; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Token constructor. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $type |
|
97
|
|
|
* @param string $value |
|
98
|
|
|
* @param int $line |
|
99
|
|
|
* @param string $filename |
|
100
|
|
|
* |
|
101
|
|
|
* @throws LexingException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function __construct(int $type, string $value, int $line = 0, string $filename = '') |
|
104
|
|
|
{ |
|
105
|
|
|
if (!isset(self::TOKEN_MAP[$type])) { |
|
106
|
|
|
throw new LexingException("Unknown token found: $type"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->type = $type; |
|
110
|
|
|
$this->value = $value; |
|
111
|
|
|
$this->line = $line; |
|
112
|
|
|
$this->filename = $filename; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getType() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->type; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getValue() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->value; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getLine() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->line; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getFilename() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->filename; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function __toString(): string |
|
151
|
|
|
{ |
|
152
|
|
|
$tag = self::decode($this->type); |
|
153
|
|
|
|
|
154
|
|
|
return \sprintf('[%s:%d] %s(%s)', $this->filename, $this->line, $tag, $this->value); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param $tag |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function decode($tag): string |
|
163
|
|
|
{ |
|
164
|
|
|
if (!isset(self::TOKEN_MAP[$tag])) { |
|
165
|
|
|
return "$tag"; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return 'Token::'.self::TOKEN_MAP[$tag]; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|