function LSM::match
Executes the match. It returns an array where the first element is the number of consumed characters and the second element is the match. If no match is found it returns null.
Parameters
Scanner $scanner Scanner instance:
int $index Current index:
string $char Current character:
Return value
array|null
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ LSM.php, line 123
Class
- LSM
- Longest Sequence Matcher. Utility class used by the scanner to consume the longest sequence of character given a set of allowed characters sequences.
Namespace
Peast\SyntaxCode
public function match($scanner, $index, $char) {
$consumed = 1;
$bestMatch = null;
if (isset($this->map[$char])) {
//If the character is present in the map and it has a max length of
//1, match immediately
if ($this->map[$char]["maxLen"] === 1) {
$bestMatch = array(
$consumed,
$char,
);
}
else {
//Otherwise consume a number of characters equal to the max
//length and find the longest match
$buffer = $char;
$map = $this->map[$char]["map"];
$maxLen = $this->map[$char]["maxLen"];
do {
if (in_array($buffer, $map)) {
$bestMatch = array(
$consumed,
$buffer,
);
}
$nextChar = $scanner->charAt($index + $consumed);
if ($nextChar === null) {
break;
}
$buffer .= $nextChar;
$consumed++;
} while ($consumed <= $maxLen);
}
}
return $bestMatch;
}