Skip to content

Commit 7b7cf7b

Browse files
committed
Refactor deserializer to avoid integer conversion
1 parent 750a81b commit 7b7cf7b

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

src/Atn/ATNDeserializer.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ public function __construct(?ATNDeserializationOptions $options = null)
6868
$this->deserializationOptions = $options ?? ATNDeserializationOptions::defaultOptions();
6969
}
7070

71-
public function deserialize(string $data) : ATN
71+
/**
72+
* @param array<int> $data
73+
*/
74+
public function deserialize(array $data) : ATN
7275
{
73-
$this->reset($data);
76+
$this->data = $data;
77+
$this->pos = 0;
78+
7479
$this->checkVersion();
7580
$atn = $this->readATN();
7681
$this->readStates($atn);
@@ -104,22 +109,6 @@ public function deserialize(string $data) : ATN
104109
return $atn;
105110
}
106111

107-
private function reset(string $data) : void
108-
{
109-
$characters = \preg_split('//u', $data, -1, \PREG_SPLIT_NO_EMPTY);
110-
111-
if ($characters === false) {
112-
return;
113-
}
114-
115-
for ($i = 0, $length = \count($characters); $i < $length; $i++) {
116-
$this->data[] = StringUtils::codePoint($characters[$i]);
117-
}
118-
119-
$this->pos = 0;
120-
}
121-
122-
123112
private function checkVersion() : void
124113
{
125114
$version = $this->readInt();

src/Parser.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ abstract class Parser extends Recognizer
3232
* {@see ATN} with bypass alternatives.
3333
*
3434
* @see ATNDeserializationOptions::isGenerateRuleBypassTransitions()
35-
*
36-
* @var array<string, ATN>
3735
*/
38-
private static $bypassAltsAtnCache = [];
36+
private static ?ATN $bypassAltsAtnCache = null;
3937

4038
/**
4139
* The error handling strategy for the parser. The default value is a new
@@ -394,17 +392,14 @@ public function setTokenFactory(TokenFactory $factory) : void
394392
*/
395393
public function getATNWithBypassAlts() : ATN
396394
{
397-
$serializedAtn = $this->getSerializedATN();
398-
$result = self::$bypassAltsAtnCache[$serializedAtn] ?? null;
399-
400-
if ($result === null) {
395+
if (self::$bypassAltsAtnCache === null) {
401396
$deserializationOptions = new ATNDeserializationOptions();
402397
$deserializationOptions->setGenerateRuleBypassTransitions(true);
403-
$result = (new ATNDeserializer($deserializationOptions))->deserialize($serializedAtn);
404-
self::$bypassAltsAtnCache[$serializedAtn] = $result;
398+
self::$bypassAltsAtnCache = (new ATNDeserializer($deserializationOptions))
399+
->deserialize($this->getSerializedATN());
405400
}
406401

407-
return $result;
402+
return self::$bypassAltsAtnCache;
408403
}
409404

410405
public function getErrorHandler() : ANTLRErrorStrategy

src/Recognizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ public function getTokenType(string $tokenName) : int
113113
* If this recognizer was generated, it will have a serialized ATN
114114
* representation of the grammar. For interpreters, we don't know
115115
* their serialized ATN despite having created the interpreter from it.
116+
*
117+
* @return array<int>
116118
*/
117-
public function getSerializedATN() : string
119+
public function getSerializedATN() : array
118120
{
119121
throw new \InvalidArgumentException('there is no serialized ATN');
120122
}

0 commit comments

Comments
 (0)