4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 namespace Wikimedia\CSS\Grammar;
9 use Wikimedia\CSS\Objects\ComponentValueList;
10 use Wikimedia\CSS\Objects\Token;
13 * Matcher that matches a token of a particular type
14 * @see https://www.w3.org/TR/2016/CR-css-values-3-20160929/#component-types
16 class TokenMatcher extends Matcher {
17 /** @var string One of the Token::T_* constants */
20 /** @var callable|null Something to call to further validate the token */
21 protected $callback = null;
24 * @param string $type Token type to match
25 * @param callable|null $callback Something to call to further validate the token.
26 * bool callback( Token )
28 public function __construct( $type, callable $callback = null ) {
30 $this->callback = $callback;
33 protected function generateMatches( ComponentValueList $values, $start, array $options ) {
34 $cv = isset( $values[$start] ) ? $values[$start] : null;
35 if ( $cv instanceof Token && $cv->type() === $this->type &&
36 ( !$this->callback || call_user_func( $this->callback, $cv ) )
38 yield $this->makeMatch( $values, $start, $this->next( $values, $start, $options ) );