@@ -31,19 +31,15 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface
3131 * Default token name for unidentified tokens.
3232 *
3333 * @var non-empty-string
34- *
35- * @final
3634 */
37- public const DEFAULT_UNKNOWN_TOKEN_NAME = UnknownToken::DEFAULT_TOKEN_NAME ;
35+ final public const DEFAULT_UNKNOWN_TOKEN_NAME = UnknownToken::DEFAULT_TOKEN_NAME ;
3836
3937 /**
4038 * Default token name for end of input.
4139 *
4240 * @var non-empty-string
43- *
44- * @final
4541 */
46- public const DEFAULT_EOI_TOKEN_NAME = EndOfInput::DEFAULT_TOKEN_NAME ;
42+ final public const DEFAULT_EOI_TOKEN_NAME = EndOfInput::DEFAULT_TOKEN_NAME ;
4743
4844 private DriverInterface $ driver ;
4945
@@ -53,17 +49,9 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface
5349
5450 private HandlerInterface $ onEndOfInput ;
5551
56- /**
57- * @var non-empty-string
58- */
59- private readonly string $ unknown ;
60-
6152 private readonly SourceFactoryInterface $ sources ;
6253
6354 /**
64- * @param array<array-key, non-empty-string> $tokens list of
65- * token names/identifiers and its patterns
66- * @param list<array-key> $skip list of hidden token names/identifiers
6755 * @param HandlerInterface $onUnknownToken This setting is responsible for
6856 * the behavior of the lexer in case of detection of unrecognized
6957 * tokens.
@@ -87,37 +75,61 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface
8775 *
8876 * Note that you can also define your own {@see HandlerInterface} to
8977 * override behavior.
90- * @param non-empty-string $unknown The identifier that marks each unknown
91- * token inside the executor (internal runtime). This parameter only
92- * needs to be changed if the name is already in use in the user's
93- * token set (in the {@see $tokens} parameter), otherwise it makes
94- * no sense.
95- * @param non-empty-string $eoi
9678 */
9779 public function __construct (
80+ /**
81+ * List of token names/identifiers and its patterns.
82+ *
83+ * @var array<array-key, non-empty-string>
84+ */
9885 protected array $ tokens = [],
86+ /**
87+ * List of hidden token names/identifiers.
88+ *
89+ * @var list<array-key>
90+ */
9991 protected array $ skip = [],
10092 ?DriverInterface $ driver = null ,
10193 ?HandlerInterface $ onHiddenToken = null ,
10294 ?HandlerInterface $ onUnknownToken = null ,
10395 ?HandlerInterface $ onEndOfInput = null ,
104- string $ unknown = Lexer::DEFAULT_UNKNOWN_TOKEN_NAME ,
10596 /**
106- * @readonly
97+ * The identifier that marks each unknown token inside the executor
98+ * (internal runtime). This parameter only needs to be changed if the
99+ * name is already in use in the user's token set (in the {@see $tokens}
100+ * parameter), otherwise it makes no sense.
101+ *
102+ * @var non-empty-string
103+ */
104+ private readonly string $ unknown = Lexer::DEFAULT_UNKNOWN_TOKEN_NAME ,
105+ /**
106+ * @var non-empty-string
107107 */
108- private string $ eoi = Lexer::DEFAULT_EOI_TOKEN_NAME ,
109- ?SourceFactoryInterface $ sources = null
108+ private readonly string $ eoi = Lexer::DEFAULT_EOI_TOKEN_NAME ,
109+ ?SourceFactoryInterface $ sources = null ,
110110 ) {
111111 $ this ->driver = $ driver ?? new Markers (new MarkersCompiler (), $ unknown );
112- $ this ->unknown = $ unknown ;
113-
114112 $ this ->onHiddenToken = $ onHiddenToken ?? new NullHandler ();
115113 $ this ->onUnknownToken = $ onUnknownToken ?? new ThrowErrorHandler ();
116114 $ this ->onEndOfInput = $ onEndOfInput ?? new PassthroughHandler ();
117-
118115 $ this ->sources = $ sources ?? new SourceFactory ();
119116 }
120117
118+ /**
119+ * @deprecated since phplrt 3.6 and will be removed in 4.0. Please use
120+ * "$onUnknownToken" argument of the {@see __construct()}
121+ * or {@see Lexer::withUnknownTokenHandler()} method instead.
122+ */
123+ public function disableUnrecognizedTokenException (): void
124+ {
125+ trigger_deprecation ('phplrt/lexer ' , '3.6 ' , <<<'MSG'
126+ Using "%s::disableUnrecognizedTokenException()" is deprecated.
127+ Please use %1$s::withUnknownTokenHandler() instead.
128+ MSG, static ::class);
129+
130+ $ this ->onUnknownToken = new PassthroughHandler ();
131+ }
132+
121133 /**
122134 * @psalm-immutable This method returns a new {@see LexerInterface} instance
123135 * and does not change the current state of the lexer.
@@ -169,6 +181,36 @@ public function withEndOfInputHandler(HandlerInterface $handler): self
169181 return $ self ;
170182 }
171183
184+ /**
185+ * @deprecated since phplrt 3.6 and will be removed in 4.0.
186+ *
187+ * @api
188+ */
189+ public function getDriver (): DriverInterface
190+ {
191+ trigger_deprecation ('phplrt/lexer ' , '3.6 ' , <<<'MSG'
192+ Using "%s::getDriver()" is deprecated.
193+ MSG, static ::class);
194+
195+ return $ this ->driver ;
196+ }
197+
198+ /**
199+ * @deprecated since phplrt 3.6 and will be removed in 4.0.
200+ *
201+ * @api
202+ */
203+ public function setDriver (DriverInterface $ driver ): self
204+ {
205+ trigger_deprecation ('phplrt/lexer ' , '3.6 ' , <<<'MSG'
206+ Using "%s::setDriver(DriverInterface $driver)" is deprecated.
207+ MSG, static ::class);
208+
209+ $ this ->driver = $ driver ;
210+
211+ return $ this ;
212+ }
213+
172214 public function skip (string ...$ tokens ): self
173215 {
174216 $ this ->skip = \array_merge ($ this ->skip , $ tokens );
@@ -277,9 +319,13 @@ public function lex(mixed $source, int $offset = 0): iterable
277319 continue ;
278320 }
279321
280- if ($ unknown !== [] && ($ result = $ this ->handleUnknownToken ($ source , $ unknown ))) {
281- yield $ result ;
282- $ unknown = [];
322+ if ($ unknown !== []) {
323+ $ result = $ this ->handleUnknownToken ($ source , $ unknown );
324+
325+ if ($ result !== null ) {
326+ yield $ result ;
327+ $ unknown = [];
328+ }
283329 }
284330
285331 yield $ token ;
@@ -288,11 +334,17 @@ public function lex(mixed $source, int $offset = 0): iterable
288334 throw LexerException::fromInternalError ($ e );
289335 }
290336
291- if ($ unknown !== [] && $ result = $ this ->handleUnknownToken ($ source , $ unknown )) {
292- yield $ token = $ result ;
337+ if ($ unknown !== []) {
338+ $ result = $ this ->handleUnknownToken ($ source , $ unknown );
339+
340+ if ($ result !== null ) {
341+ yield $ token = $ result ;
342+ }
293343 }
294344
295- if (($ eoi = $ this ->handleEoiToken ($ source , $ token ?? null )) !== null ) {
345+ $ eoi = $ this ->handleEoiToken ($ source , $ token ?? null );
346+
347+ if ($ eoi !== null ) {
296348 yield $ eoi ;
297349 }
298350 }
@@ -317,9 +369,7 @@ private function handleEoiToken(ReadableInterface $source, ?TokenInterface $last
317369 */
318370 private function reduceUnknownToken (array $ tokens ): TokenInterface
319371 {
320- $ concat = static function (string $ data , TokenInterface $ token ): string {
321- return $ data . $ token ->getValue ();
322- };
372+ $ concat = static fn (string $ data , TokenInterface $ token ): string => $ data . $ token ->getValue ();
323373
324374 $ value = \array_reduce ($ tokens , $ concat , '' );
325375
0 commit comments