css-sanitizer
Classes to parse and sanitize CSS
Loading...
Searching...
No Matches
Wikimedia\CSS\Grammar\Matcher Class Reference

Base class for grammar matchers. More...

+ Inheritance diagram for Wikimedia\CSS\Grammar\Matcher:

Public Member Functions

 capture ( $captureName)
 Return a copy of this matcher that will capture its matches.
 
 matchAgainst (ComponentValueList $values, array $options=[])
 Match against a list of ComponentValues.
 
 getDefaultOptions ()
 Fetch the default options for this Matcher.
 
 setDefaultOptions (array $options)
 Set the default options for this Matcher.
 

Static Public Member Functions

static create (... $args)
 Create an instance.
 

Protected Member Functions

 next (ComponentValueList $values, $start, array $options)
 Find the next ComponentValue in the input, possibly skipping whitespace.
 
 makeMatch (ComponentValueList $list, $start, $end, GrammarMatch $submatch=null, array $stack=[])
 Create a GrammarMatch.
 
 generateMatches (ComponentValueList $values, $start, array $options)
 Match against a list of ComponentValues.
 

Protected Attributes

 $captureName = null
 
 $defaultOptions
 

Detailed Description

Base class for grammar matchers.

The CSS Syntax Level 3 and Values Level 3 specifications use a mostly context-free grammar to define what things like selectors and property values look like. The Matcher classes allow for constructing an object that will determine whether a ComponentValueList actually matches this grammar.

Member Function Documentation

◆ capture()

Wikimedia\CSS\Grammar\Matcher::capture (   $captureName)

Return a copy of this matcher that will capture its matches.

A "capturing" Matcher will produce GrammarMatches that return a value from the GrammarMatch::getName() method. The GrammarMatch::getCapturedMatches() method may be used to retrieve them from the top-level GrammarMatch.

The concept is similar to capturing groups in PCRE and other regex languages.

Parameters
string | null$captureNameName to apply to captured GrammarMatch objects
Returns
static

◆ create()

static Wikimedia\CSS\Grammar\Matcher::create (   $args)
static

Create an instance.

Parameters
mixed...$args See static::__construct()
Returns
static

◆ generateMatches()

Wikimedia\CSS\Grammar\Matcher::generateMatches ( ComponentValueList  $values,
  $start,
array  $options 
)
abstractprotected

Match against a list of ComponentValues.

The job of a Matcher is to determine all the ways its particular grammar fragment can consume ComponentValues starting at a particular location in the ComponentValueList, represented by returning GrammarMatch objects. For example, a matcher implementing IDENT* at a starting position where there are three IDENT tokens in a row would be able to match 0, 1, 2, or all 3 of those IDENT tokens, and therefore should return an iterator over that set of GrammarMatch objects.

Some matchers take other matchers as input, for example IDENT* is probably going to be implemented as a matcher for * that repeatedly applies a matcher for IDENT. The * matcher would call the IDENT matcher's generateMatches() method directly.

Most Matchers implement this method as a generator to not build up the full set of results when it's reasonably likely the caller is going to terminate early.

Parameters
ComponentValueList$values
int$startStarting position in $values
array$optionsSee self::$defaultOptions. Always use the options passed in, don't use $this->defaultOptions yourself.
Returns
Iterator<GrammarMatch> Iterates over the set of GrammarMatch objects defining all the ways this matcher can match.

Reimplemented in Wikimedia\CSS\Grammar\Alternative, Wikimedia\CSS\Grammar\AnythingMatcher, Wikimedia\CSS\Grammar\BlockMatcher, Wikimedia\CSS\Grammar\CheckedMatcher, Wikimedia\CSS\Grammar\DelimMatcher, Wikimedia\CSS\Grammar\FunctionMatcher, Wikimedia\CSS\Grammar\KeywordMatcher, Wikimedia\CSS\Grammar\NonEmpty, Wikimedia\CSS\Grammar\NothingMatcher, Wikimedia\CSS\Grammar\NoWhitespace, Wikimedia\CSS\Grammar\TokenMatcher, Wikimedia\CSS\Grammar\UrangeMatcher, Wikimedia\CSS\Grammar\UrlMatcher, and Wikimedia\CSS\Grammar\WhitespaceMatcher.

◆ getDefaultOptions()

Wikimedia\CSS\Grammar\Matcher::getDefaultOptions ( )

Fetch the default options for this Matcher.

Returns
array See self::$defaultOptions

◆ makeMatch()

Wikimedia\CSS\Grammar\Matcher::makeMatch ( ComponentValueList  $list,
  $start,
  $end,
GrammarMatch  $submatch = null,
array  $stack = [] 
)
protected

Create a GrammarMatch.

Parameters
ComponentValueList$list
int$start
int$endFirst position after the match
GrammarMatch | null$submatchSub-match, for capturing. If $submatch itself named it will be kept as a capture in the returned GrammarMatch, otherwise its captured matches (if any) as returned by getCapturedMatches() will be kept as captures in the returned GrammarMatch.
array$stackStack from which to fetch more submatches for capturing (see $submatch). The stack is expected to be an array of arrays, with the first element of each subarray being a GrammarMatch.
Returns
GrammarMatch

◆ matchAgainst()

Wikimedia\CSS\Grammar\Matcher::matchAgainst ( ComponentValueList  $values,
array  $options = [] 
)

Match against a list of ComponentValues.

Parameters
ComponentValueList$values
array$optionsMatching options, see self::$defaultOptions
Returns
GrammarMatch|null

◆ next()

Wikimedia\CSS\Grammar\Matcher::next ( ComponentValueList  $values,
  $start,
array  $options 
)
protected

Find the next ComponentValue in the input, possibly skipping whitespace.

Parameters
ComponentValueList$valuesInput values
int$startCurrent position in the input. May be -1, in which case the first position in the input should be returned.
array$optionsSee self::$defaultOptions
Returns
int Next token index

◆ setDefaultOptions()

Wikimedia\CSS\Grammar\Matcher::setDefaultOptions ( array  $options)

Set the default options for this Matcher.

Parameters
array$optionsSee self::$defaultOptions
Returns
static $this

Member Data Documentation

◆ $defaultOptions

Wikimedia\CSS\Grammar\Matcher::$defaultOptions
protected
Initial value:
= [
'skip-whitespace' => true,
'nonterminal' => false,
'mark-significance' => false,
]

The documentation for this class was generated from the following file: