| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast\Syntax; |
| 11 |
|
| 12 |
/** |
| 13 |
* Longest Sequence Matcher. Utility class used by the scanner to consume |
| 14 |
* the longest sequence of character given a set of allowed characters sequences. |
| 15 |
* |
| 16 |
* @author Marco Marchiò <[email protected]> |
| 17 |
*/ |
| 18 |
class LSM |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Internal sequences map |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $map = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Encoding handle flag |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
protected $handleEncoding = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Class constructor |
| 36 |
* |
| 37 |
* @param array $sequences Allowed characters sequences |
| 38 |
* @param bool $handleEncoding True to handle encoding when matching |
| 39 |
*/ |
| 40 |
function __construct($sequences, $handleEncoding = false) |
| 41 |
{ |
| 42 |
$this->handleEncoding = $handleEncoding; |
| 43 |
foreach ($sequences as $s) { |
| 44 |
$this->add($s); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Adds a sequence |
| 50 |
* |
| 51 |
* @param string $sequence Sequence to add |
| 52 |
* |
| 53 |
* @return $this |
| 54 |
*/ |
| 55 |
public function add($sequence) |
| 56 |
{ |
| 57 |
if ($this->handleEncoding) { |
| 58 |
$s = Utils::stringToUTF8Array($sequence); |
| 59 |
$first = $s[0]; |
| 60 |
$len = count($s); |
| 61 |
} else { |
| 62 |
$first = $sequence[0]; |
| 63 |
$len = strlen($sequence); |
| 64 |
} |
| 65 |
if (!isset($this->map[$first])) { |
| 66 |
$this->map[$first] = array( |
| 67 |
"maxLen" => $len, |
| 68 |
"map" => array($sequence) |
| 69 |
); |
| 70 |
} else { |
| 71 |
$this->map[$first]["map"][] = $sequence; |
| 72 |
$this->map[$first]["maxLen"] = max($this->map[$first]["maxLen"], $len); |
| 73 |
} |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Removes a sequence |
| 79 |
* |
| 80 |
* @param string $sequence Sequence to remove |
| 81 |
* |
| 82 |
* @return $this |
| 83 |
*/ |
| 84 |
public function remove($sequence) |
| 85 |
{ |
| 86 |
if ($this->handleEncoding) { |
| 87 |
$s = Utils::stringToUTF8Array($sequence); |
| 88 |
$first = $s[0]; |
| 89 |
} else { |
| 90 |
$first = $sequence[0]; |
| 91 |
} |
| 92 |
if (isset($this->map[$first])) { |
| 93 |
$len = $this->handleEncoding ? count($s) : strlen($sequence); |
| 94 |
$this->map[$first]["map"] = array_diff( |
| 95 |
$this->map[$first]["map"], array($sequence) |
| 96 |
); |
| 97 |
if (!count($this->map[$first]["map"])) { |
| 98 |
unset($this->map[$first]); |
| 99 |
} elseif ($this->map[$first]["maxLen"] === $len) { |
| 100 |
// Recalculate the max length if necessary |
| 101 |
foreach ($this->map[$first]["map"] as $m) { |
| 102 |
$this->map[$first]["maxLen"] = max( |
| 103 |
$this->map[$first]["maxLen"], |
| 104 |
strlen($m) |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Executes the match. It returns an array where the first element is the |
| 114 |
* number of consumed characters and the second element is the match. If |
| 115 |
* no match is found it returns null. |
| 116 |
* |
| 117 |
* @param Scanner $scanner Scanner instance |
| 118 |
* @param int $index Current index |
| 119 |
* @param string $char Current character |
| 120 |
* |
| 121 |
* @return array|null |
| 122 |
*/ |
| 123 |
public function match($scanner, $index, $char) |
| 124 |
{ |
| 125 |
$consumed = 1; |
| 126 |
$bestMatch = null; |
| 127 |
if (isset($this->map[$char])) { |
| 128 |
//If the character is present in the map and it has a max length of |
| 129 |
//1, match immediately |
| 130 |
if ($this->map[$char]["maxLen"] === 1) { |
| 131 |
$bestMatch = array($consumed, $char); |
| 132 |
} else { |
| 133 |
//Otherwise consume a number of characters equal to the max |
| 134 |
//length and find the longest match |
| 135 |
$buffer = $char; |
| 136 |
$map = $this->map[$char]["map"]; |
| 137 |
$maxLen = $this->map[$char]["maxLen"]; |
| 138 |
do { |
| 139 |
if (in_array($buffer, $map)) { |
| 140 |
$bestMatch = array($consumed, $buffer); |
| 141 |
} |
| 142 |
$nextChar = $scanner->charAt($index + $consumed); |
| 143 |
if ($nextChar === null) { |
| 144 |
break; |
| 145 |
} |
| 146 |
$buffer .= $nextChar; |
| 147 |
$consumed++; |
| 148 |
} while ($consumed <= $maxLen); |
| 149 |
} |
| 150 |
} |
| 151 |
return $bestMatch; |
| 152 |
} |
| 153 |
} |