| 1 |
<?php |
| 2 |
/** |
| 3 |
* @file |
| 4 |
* @license https://opensource.org/licenses/Apache-2.0 Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Wikimedia\CSS\Grammar; |
| 8 |
|
| 9 |
use InvalidArgumentException; |
| 10 |
use Wikimedia\CSS\Objects\ComponentValueList; |
| 11 |
use Wikimedia\CSS\Objects\SimpleBlock; |
| 12 |
|
| 13 |
/** |
| 14 |
* Matcher that matches a SimpleBlock |
| 15 |
* |
| 16 |
* The grammar definitions in the standards seem to be written assuming they're |
| 17 |
* being passed a sequence of Tokens only, even though they're defined over a |
| 18 |
* sequence of ComponentValues (which includes SimpleBlocks and CSSFunctions) |
| 19 |
* instead. |
| 20 |
* |
| 21 |
* Thus, to be safe you'll want to use this when a grammar specifies something |
| 22 |
* like `'[' <stuff> ']'`. |
| 23 |
*/ |
| 24 |
class BlockMatcher extends Matcher { |
| 25 |
/** @var string One of the Token::T_* constants */ |
| 26 |
protected $blockType; |
| 27 |
|
| 28 |
/** @var Matcher */ |
| 29 |
protected $matcher; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string $blockType One of the Token::T_* constants |
| 33 |
* @param Matcher $matcher Matcher for the contents of the block |
| 34 |
*/ |
| 35 |
public function __construct( $blockType, Matcher $matcher ) { |
| 36 |
if ( SimpleBlock::matchingDelimiter( $blockType ) === null ) { |
| 37 |
throw new InvalidArgumentException( |
| 38 |
'A block is delimited by either {}, [], or ().' |
| 39 |
); |
| 40 |
} |
| 41 |
$this->blockType = $blockType; |
| 42 |
$this->matcher = $matcher; |
| 43 |
} |
| 44 |
|
| 45 |
/** @inheritDoc */ |
| 46 |
protected function generateMatches( ComponentValueList $values, $start, array $options ) { |
| 47 |
// @phan-suppress-next-line PhanCoalescingNeverNull |
| 48 |
$cv = $values[$start] ?? null; |
| 49 |
if ( $cv instanceof SimpleBlock && $cv->getStartTokenType() === $this->blockType ) { |
| 50 |
// To successfully match, our sub-Matcher needs to match the whole |
| 51 |
// content of the block. |
| 52 |
$l = $cv->getValue()->count(); |
| 53 |
$s = $this->next( $cv->getValue(), -1, $options ); |
| 54 |
foreach ( $this->matcher->generateMatches( $cv->getValue(), $s, $options ) as $match ) { |
| 55 |
if ( $match->getNext() === $l ) { |
| 56 |
// Matched the whole content of the block, so yield the |
| 57 |
// token after the block. |
| 58 |
yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ), $match ); |
| 59 |
return; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |