diff --git a/.travis.yml b/.travis.yml index 8855bac..e8e9767 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index ae10ee3..fcca784 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Atn/ATNDeserializer.php b/src/Atn/ATNDeserializer.php index 1f56f62..bd68745 100644 --- a/src/Atn/ATNDeserializer.php +++ b/src/Atn/ATNDeserializer.php @@ -68,9 +68,14 @@ public function __construct(?ATNDeserializationOptions $options = null) $this->deserializationOptions = $options ?? ATNDeserializationOptions::defaultOptions(); } - public function deserialize(string $data) : ATN + /** + * @param array $data + */ + public function deserialize(array $data) : ATN { - $this->reset($data); + $this->data = $data; + $this->pos = 0; + $this->checkVersion(); $atn = $this->readATN(); $this->readStates($atn); @@ -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(); diff --git a/src/Parser.php b/src/Parser.php index 94e19c0..cc9393a 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -32,10 +32,8 @@ abstract class Parser extends Recognizer * {@see ATN} with bypass alternatives. * * @see ATNDeserializationOptions::isGenerateRuleBypassTransitions() - * - * @var array */ - private static $bypassAltsAtnCache = []; + private static ?ATN $bypassAltsAtnCache = null; /** * The error handling strategy for the parser. The default value is a new @@ -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 diff --git a/src/Recognizer.php b/src/Recognizer.php index e54a98b..95901e9 100644 --- a/src/Recognizer.php +++ b/src/Recognizer.php @@ -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 */ - public function getSerializedATN() : string + public function getSerializedATN() : array { throw new \InvalidArgumentException('there is no serialized ATN'); }