Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ language: php
dist: trusty

php:
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'

before_script:
- travis_retry composer self-update
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "7.4 - 8.0",
"php": "^7.4 || ^8.0",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
25 changes: 7 additions & 18 deletions src/Atn/ATNDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ public function __construct(?ATNDeserializationOptions $options = null)
$this->deserializationOptions = $options ?? ATNDeserializationOptions::defaultOptions();
}

public function deserialize(string $data) : ATN
/**
* @param array<int> $data
*/
public function deserialize(array $data) : ATN
{
$this->reset($data);
$this->data = $data;
$this->pos = 0;

$this->checkVersion();
$atn = $this->readATN();
$this->readStates($atn);
Expand Down Expand Up @@ -104,22 +109,6 @@ public function deserialize(string $data) : ATN
return $atn;
}

private function reset(string $data) : void
{
$characters = \preg_split('//u', $data, -1, \PREG_SPLIT_NO_EMPTY);

if ($characters === false) {
return;
}

for ($i = 0, $length = \count($characters); $i < $length; $i++) {
$this->data[] = StringUtils::codePoint($characters[$i]);
}

$this->pos = 0;
}


private function checkVersion() : void
{
$version = $this->readInt();
Expand Down
15 changes: 5 additions & 10 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ abstract class Parser extends Recognizer
* {@see ATN} with bypass alternatives.
*
* @see ATNDeserializationOptions::isGenerateRuleBypassTransitions()
*
* @var array<string, ATN>
*/
private static $bypassAltsAtnCache = [];
private static ?ATN $bypassAltsAtnCache = null;

/**
* The error handling strategy for the parser. The default value is a new
Expand Down Expand Up @@ -394,17 +392,14 @@ public function setTokenFactory(TokenFactory $factory) : void
*/
public function getATNWithBypassAlts() : ATN
{
$serializedAtn = $this->getSerializedATN();
$result = self::$bypassAltsAtnCache[$serializedAtn] ?? null;

if ($result === null) {
if (self::$bypassAltsAtnCache === null) {
$deserializationOptions = new ATNDeserializationOptions();
$deserializationOptions->setGenerateRuleBypassTransitions(true);
$result = (new ATNDeserializer($deserializationOptions))->deserialize($serializedAtn);
self::$bypassAltsAtnCache[$serializedAtn] = $result;
self::$bypassAltsAtnCache = (new ATNDeserializer($deserializationOptions))
->deserialize($this->getSerializedATN());
}

return $result;
return self::$bypassAltsAtnCache;
}

public function getErrorHandler() : ANTLRErrorStrategy
Expand Down
4 changes: 3 additions & 1 deletion src/Recognizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ public function getTokenType(string $tokenName) : int
* If this recognizer was generated, it will have a serialized ATN
* representation of the grammar. For interpreters, we don't know
* their serialized ATN despite having created the interpreter from it.
*
* @return array<int>
*/
public function getSerializedATN() : string
public function getSerializedATN() : array
{
throw new \InvalidArgumentException('there is no serialized ATN');
}
Expand Down