2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-10-08 13:19:10 +08:00
|
|
|
|
2019-09-23 16:36:58 +02:00
|
|
|
namespace Rector\Php70;
|
2018-10-08 13:19:10 +08:00
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
2019-09-23 17:58:49 +02:00
|
|
|
use Rector\Php70\Exception\InvalidEregException;
|
2018-10-08 13:19:10 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @source https://gist.github.com/lifthrasiir/704754/7e486f43e62fd1c9d3669330c251f8ca4a59a3f8
|
2020-02-23 18:51:22 +01:00
|
|
|
*
|
|
|
|
* @see \Rector\Php70\Tests\EregToPcreTransformerTest
|
2018-10-08 13:19:10 +08:00
|
|
|
*/
|
|
|
|
final class EregToPcreTransformer
|
|
|
|
{
|
2020-02-18 23:09:25 +01:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-02-23 19:55:05 +01:00
|
|
|
private const CHARACTER_CLASS_MAP = [
|
2020-02-18 23:09:25 +01:00
|
|
|
':alnum:' => '[:alnum:]',
|
|
|
|
':alpha:' => '[:alpha:]',
|
|
|
|
':blank:' => '[:blank:]',
|
|
|
|
':cntrl:' => '[:cntrl:]',
|
|
|
|
':digit:' => '\d',
|
|
|
|
':graph:' => '[:graph:]',
|
|
|
|
':lower:' => '[:lower:]',
|
|
|
|
':print:' => '[:print:]',
|
|
|
|
':punct:' => '[:punct:]',
|
2020-05-12 17:20:40 +02:00
|
|
|
// should include VT
|
|
|
|
':space:' => '\013\s',
|
2020-02-18 23:09:25 +01:00
|
|
|
':upper:' => '[:upper:]',
|
|
|
|
':xdigit:' => '[:xdigit:]',
|
|
|
|
];
|
|
|
|
|
2018-10-08 13:19:10 +08:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $pcreDelimiter;
|
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $icache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $cache = [];
|
|
|
|
|
2018-10-08 13:19:10 +08:00
|
|
|
/**
|
2020-08-30 23:29:39 +02:00
|
|
|
* Change this via services configuratoin in rector.php if you need it
|
2018-10-08 13:19:10 +08:00
|
|
|
* Single type is chosen to prevent every regular with different delimiter.
|
|
|
|
*/
|
|
|
|
public function __construct(string $pcreDelimiter = '#')
|
|
|
|
{
|
|
|
|
$this->pcreDelimiter = $pcreDelimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transform(string $ereg, bool $isCaseInsensitive): string
|
|
|
|
{
|
|
|
|
if (! Strings::contains($ereg, $this->pcreDelimiter)) {
|
|
|
|
return $this->ere2pcre($ereg, $isCaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2020-07-06 01:57:19 +02:00
|
|
|
$quotedEreg = preg_quote($ereg, '#');
|
|
|
|
return $this->ere2pcre($quotedEreg, $isCaseInsensitive);
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// converts the ERE $s into the PCRE $r. triggers error on any invalid input.
|
2020-03-29 00:06:05 +01:00
|
|
|
private function ere2pcre(string $content, bool $ignorecase): string
|
2018-10-08 13:19:10 +08:00
|
|
|
{
|
|
|
|
if ($ignorecase) {
|
2020-02-23 19:55:05 +01:00
|
|
|
if (isset($this->icache[$content])) {
|
|
|
|
return $this->icache[$content];
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif (isset($this->cache[$content])) {
|
|
|
|
return $this->cache[$content];
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
[$r, $i] = $this->_ere2pcre($content, 0);
|
|
|
|
if ($i !== strlen($content)) {
|
2018-10-08 13:19:10 +08:00
|
|
|
throw new InvalidEregException('unescaped metacharacter ")"');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ignorecase) {
|
2020-02-23 19:55:05 +01:00
|
|
|
return $this->icache[$content] = '#' . $r . '#mi';
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
|
|
|
|
2020-02-23 20:13:15 +01:00
|
|
|
return $this->cache[$content] = '#' . $r . '#m';
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively converts ERE into PCRE, starting at the position $i.
|
|
|
|
*
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2020-02-23 19:55:05 +01:00
|
|
|
private function _ere2pcre(string $content, int $i): array
|
2018-10-08 13:19:10 +08:00
|
|
|
{
|
|
|
|
$r = [''];
|
|
|
|
$rr = 0;
|
2020-02-23 19:55:05 +01:00
|
|
|
$l = strlen($content);
|
2018-10-08 13:19:10 +08:00
|
|
|
while ($i < $l) {
|
|
|
|
// atom
|
2020-02-23 19:55:05 +01:00
|
|
|
$char = $content[$i];
|
|
|
|
if ($char === '(') {
|
|
|
|
$i = (int) $i;
|
|
|
|
$i = $this->processBracket($content, $i, $l, $r, $rr);
|
|
|
|
} elseif ($char === '[') {
|
2018-10-08 13:19:10 +08:00
|
|
|
++$i;
|
|
|
|
$cls = '';
|
2020-02-23 19:55:05 +01:00
|
|
|
if ($i < $l && $content[$i] === '^') {
|
2018-10-08 13:19:10 +08:00
|
|
|
$cls .= '^';
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
if ($i >= $l) {
|
|
|
|
throw new InvalidEregException('"[" does not have a matching "]"');
|
|
|
|
}
|
|
|
|
$start = true;
|
2020-02-13 11:10:09 +01:00
|
|
|
|
2020-02-23 18:51:22 +01:00
|
|
|
$i = (int) $i;
|
2020-02-23 19:55:05 +01:00
|
|
|
[$cls, $i] = $this->processSquareBracket($content, $i, $l, $cls, $start);
|
2020-02-23 18:51:22 +01:00
|
|
|
|
2018-10-08 13:19:10 +08:00
|
|
|
if ($i >= $l) {
|
|
|
|
throw new InvalidEregException('"[" does not have a matching "]"');
|
|
|
|
}
|
|
|
|
$r[$rr] .= '[' . $cls . ']';
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === ')') {
|
2018-10-08 13:19:10 +08:00
|
|
|
break;
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === '*' || $char === '+' || $char === '?') {
|
|
|
|
throw new InvalidEregException('unescaped metacharacter "' . $char . '"');
|
|
|
|
} elseif ($char === '{') {
|
|
|
|
if ($i + 1 < $l && Strings::contains('0123456789', $content[$i + 1])) {
|
2018-10-08 13:19:10 +08:00
|
|
|
$r[$rr] .= '\{';
|
|
|
|
} else {
|
2020-02-23 19:55:05 +01:00
|
|
|
throw new InvalidEregException('unescaped metacharacter "' . $char . '"');
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === '.') {
|
|
|
|
$r[$rr] .= $char;
|
|
|
|
} elseif ($char === '^' || $char === '$') {
|
|
|
|
$r[$rr] .= $char;
|
2018-10-08 13:19:10 +08:00
|
|
|
++$i;
|
|
|
|
continue;
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === '|') {
|
2018-10-08 13:19:10 +08:00
|
|
|
if ($r[$rr] === '') {
|
|
|
|
throw new InvalidEregException('empty branch');
|
|
|
|
}
|
|
|
|
$r[] = '';
|
|
|
|
++$rr;
|
|
|
|
++$i;
|
|
|
|
continue;
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === '\\') {
|
2018-10-08 13:19:10 +08:00
|
|
|
if (++$i >= $l) {
|
|
|
|
throw new InvalidEregException('an invalid escape sequence at the end');
|
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
$r[$rr] .= $this->_ere2pcre_escape($content[$i]);
|
2020-05-12 17:20:40 +02:00
|
|
|
} else {
|
|
|
|
// including ] and } which are allowed as a literal character
|
2020-02-23 19:55:05 +01:00
|
|
|
$r[$rr] .= $this->_ere2pcre_escape($char);
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
|
|
|
++$i;
|
|
|
|
if ($i >= $l) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// piece after the atom (only ONE of them is possible)
|
2020-02-23 19:55:05 +01:00
|
|
|
$char = $content[$i];
|
|
|
|
if ($char === '*' || $char === '+' || $char === '?') {
|
|
|
|
$r[$rr] .= $char;
|
2018-10-08 13:19:10 +08:00
|
|
|
++$i;
|
2020-02-23 19:55:05 +01:00
|
|
|
} elseif ($char === '{') {
|
2020-02-13 11:10:09 +01:00
|
|
|
$i = (int) $i;
|
2019-11-05 12:25:19 +01:00
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
$i = $this->processCurlyBracket($content, $i, $r, $rr);
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
|
2018-10-08 13:19:10 +08:00
|
|
|
if ($r[$rr] === '') {
|
|
|
|
throw new InvalidEregException('empty regular expression or branch');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [implode('|', $r), $i];
|
|
|
|
}
|
2018-10-31 16:34:37 +01:00
|
|
|
|
2020-07-27 08:56:25 +02:00
|
|
|
private function processBracket(string $content, int $i, int $l, array &$r, int $rr): int
|
2018-10-31 16:34:37 +01:00
|
|
|
{
|
2020-05-12 17:20:40 +02:00
|
|
|
// special case
|
|
|
|
if ($i + 1 < $l && $content[$i + 1] === ')') {
|
2020-04-26 02:57:47 +02:00
|
|
|
$r[$rr] .= '()';
|
|
|
|
++$i;
|
|
|
|
} else {
|
|
|
|
$position = (int) $i + 1;
|
|
|
|
[$t, $ii] = $this->_ere2pcre($content, $position);
|
|
|
|
if ($ii >= $l || $content[$ii] !== ')') {
|
|
|
|
throw new InvalidEregException('"(" does not have a matching ")"');
|
|
|
|
}
|
|
|
|
$r[$rr] .= '(' . $t . ')';
|
|
|
|
$i = $ii;
|
2018-10-31 16:34:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-26 02:57:47 +02:00
|
|
|
return $i;
|
2018-10-31 16:34:37 +01:00
|
|
|
}
|
2020-02-23 18:51:22 +01:00
|
|
|
|
2020-08-11 12:59:04 +02:00
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2020-02-23 19:55:05 +01:00
|
|
|
private function processSquareBracket(string $s, int $i, int $l, string $cls, bool $start): array
|
2020-02-23 18:51:22 +01:00
|
|
|
{
|
|
|
|
do {
|
2020-02-23 19:55:05 +01:00
|
|
|
if ($s[$i] === '[' && $i + 1 < $l && Strings::contains('.=:', $s[$i + 1])) {
|
2020-08-11 12:59:04 +02:00
|
|
|
/** @var string $cls */
|
2020-02-23 19:55:05 +01:00
|
|
|
[$cls, $i] = $this->processCharacterClass($s, $i, $cls);
|
2020-02-23 18:51:22 +01:00
|
|
|
} else {
|
|
|
|
$a = $s[$i++];
|
|
|
|
if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) {
|
|
|
|
throw new InvalidEregException('"-" is invalid for the start character in the brackets');
|
|
|
|
}
|
|
|
|
if ($i < $l && $s[$i] === '-') {
|
|
|
|
++$i;
|
|
|
|
$b = $s[$i++];
|
|
|
|
if ($b === ']') {
|
|
|
|
$cls .= $this->_ere2pcre_escape($a) . '\-';
|
|
|
|
break;
|
|
|
|
} elseif (ord($a) > ord($b)) {
|
|
|
|
throw new InvalidEregException(sprintf('an invalid character range %d-%d"', $a, $b));
|
|
|
|
}
|
|
|
|
$cls .= $this->_ere2pcre_escape($a) . '-' . $this->_ere2pcre_escape($b);
|
|
|
|
} else {
|
|
|
|
$cls .= $this->_ere2pcre_escape($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$start = false;
|
|
|
|
} while ($i < $l && $s[$i] !== ']');
|
|
|
|
|
|
|
|
return [$cls, $i];
|
|
|
|
}
|
|
|
|
|
2020-04-26 02:57:47 +02:00
|
|
|
private function _ere2pcre_escape(string $content): string
|
|
|
|
{
|
|
|
|
if ($content === "\0") {
|
|
|
|
throw new InvalidEregException('a literal null byte in the regex');
|
2020-07-20 14:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Strings::contains('\^$.[]|()?*+{}-/', $content)) {
|
2020-04-26 02:57:47 +02:00
|
|
|
return '\\' . $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
private function processCurlyBracket(string $s, int $i, array &$r, int $rr): int
|
2020-02-23 18:51:22 +01:00
|
|
|
{
|
|
|
|
$ii = strpos($s, '}', $i);
|
|
|
|
if ($ii === false) {
|
|
|
|
throw new InvalidEregException('"{" does not have a matching "}"');
|
|
|
|
}
|
|
|
|
|
|
|
|
$start = (int) $i + 1;
|
|
|
|
$length = (int) $ii - ($i + 1);
|
|
|
|
$bound = Strings::substring($s, $start, $length);
|
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
$matches = Strings::match($bound, '/^(\d|[1-9]\d|1\d\d|
|
2020-02-23 18:51:22 +01:00
|
|
|
2[0-4]\d|25[0-5])
|
|
|
|
(,(\d|[1-9]\d|1\d\d|
|
|
|
|
2[0-4]\d|25[0-5])?)?$/x');
|
2020-02-23 19:55:05 +01:00
|
|
|
if (! $matches) {
|
2020-02-23 18:51:22 +01:00
|
|
|
throw new InvalidEregException('an invalid bound');
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
if (isset($matches[3])) {
|
|
|
|
if ($matches[1] > $matches[3]) {
|
2020-02-23 18:51:22 +01:00
|
|
|
throw new InvalidEregException('an invalid bound');
|
|
|
|
}
|
2020-02-23 19:55:05 +01:00
|
|
|
$r[$rr] .= '{' . $matches[1] . ',' . $matches[3] . '}';
|
|
|
|
} elseif (isset($matches[2])) {
|
|
|
|
$r[$rr] .= '{' . $matches[1] . ',}';
|
|
|
|
} else {
|
|
|
|
$r[$rr] .= '{' . $matches[1] . '}';
|
|
|
|
}
|
|
|
|
|
2020-02-23 20:13:15 +01:00
|
|
|
return $ii + 1;
|
2020-02-23 19:55:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-11 12:59:04 +02:00
|
|
|
/**
|
|
|
|
* @return int[]|string[]
|
|
|
|
*/
|
2020-02-23 19:55:05 +01:00
|
|
|
private function processCharacterClass(string $content, int $i, string $cls): array
|
|
|
|
{
|
|
|
|
$offset = (int) $i;
|
|
|
|
$ii = strpos($content, ']', $offset);
|
|
|
|
if ($ii === false) {
|
|
|
|
throw new InvalidEregException('"[" does not have a matching "]"');
|
|
|
|
}
|
|
|
|
|
|
|
|
$start = (int) $i + 1;
|
|
|
|
|
|
|
|
$length = (int) ($ii - ($i + 1));
|
|
|
|
$ccls = Strings::substring($content, $start, $length);
|
|
|
|
if (! isset(self::CHARACTER_CLASS_MAP[$ccls])) {
|
|
|
|
throw new InvalidEregException('an invalid or unsupported character class [' . $ccls . ']');
|
|
|
|
}
|
|
|
|
$cls .= self::CHARACTER_CLASS_MAP[$ccls];
|
2020-02-23 18:51:22 +01:00
|
|
|
$i = $ii + 1;
|
|
|
|
|
2020-02-23 19:55:05 +01:00
|
|
|
return [$cls, $i];
|
2020-02-23 18:51:22 +01:00
|
|
|
}
|
2018-10-08 13:19:10 +08:00
|
|
|
}
|