* A gettext Plural-Forms parser.
if ( ! class_exists( 'Plural_Forms', false ) ) :
#[AllowDynamicProperties]
* @var string OP_CHARS Operator characters.
const OP_CHARS = '|&><!=%?:';
* Valid number characters.
* @var string NUM_CHARS Valid number characters.
const NUM_CHARS = '0123456789';
* Operator precedence from highest to lowest. Higher numbers indicate
* higher precedence, and are executed first.
* @see https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
* @var array $op_precedence Operator precedence from highest to lowest.
protected static $op_precedence = array(
* Tokens generated from the string.
* @var array $tokens List of tokens.
protected $tokens = array();
* Cache for repeated calls to the function.
* @var array $cache Map of $n => $result
protected $cache = array();
* @param string $str Plural function (just the bit after `plural=` from Plural-Forms)
public function __construct( $str ) {
* Parse a Plural-Forms string into tokens.
* Uses the shunting-yard algorithm to convert the string to Reverse Polish
* @throws Exception If there is a syntax or parsing error with the string.
* @param string $str String to parse.
protected function parse( $str ) {
// Convert infix operators to postfix using the shunting-yard algorithm.
$next = substr( $str, $pos, 1 );
$output[] = array( 'var' );
while ( ! empty( $stack ) ) {
$o2 = $stack[ count( $stack ) - 1 ];
$output[] = array( 'op', array_pop( $stack ) );
throw new Exception( 'Mismatched parentheses' );
$end_operator = strspn( $str, self::OP_CHARS, $pos );
$operator = substr( $str, $pos, $end_operator );
if ( ! array_key_exists( $operator, self::$op_precedence ) ) {
throw new Exception( sprintf( 'Unknown operator "%s"', $operator ) );
while ( ! empty( $stack ) ) {
$o2 = $stack[ count( $stack ) - 1 ];
// Ternary is right-associative in C.
if ( '?:' === $operator || '?' === $operator ) {
if ( self::$op_precedence[ $operator ] >= self::$op_precedence[ $o2 ] ) {
} elseif ( self::$op_precedence[ $operator ] > self::$op_precedence[ $o2 ] ) {
$output[] = array( 'op', array_pop( $stack ) );
$s_pos = count( $stack ) - 1;
$output[] = array( 'op', array_pop( $stack ) );
throw new Exception( 'Missing starting "?" ternary operator' );
// Default - number or invalid.
if ( $next >= '0' && $next <= '9' ) {
$span = strspn( $str, self::NUM_CHARS, $pos );
$output[] = array( 'value', intval( substr( $str, $pos, $span ) ) );
throw new Exception( sprintf( 'Unknown symbol "%s"', $next ) );
while ( ! empty( $stack ) ) {
$o2 = array_pop( $stack );
if ( '(' === $o2 || ')' === $o2 ) {
throw new Exception( 'Mismatched parentheses' );
$output[] = array( 'op', $o2 );
* Get the plural form for a number.
* Caches the value for repeated calls.
* @param int $num Number to get plural form for.
* @return int Plural form value.
public function get( $num ) {
if ( isset( $this->cache[ $num ] ) ) {
return $this->cache[ $num ];
$this->cache[ $num ] = $this->execute( $num );
return $this->cache[ $num ];
* Execute the plural form function.
* @throws Exception If the plural form value cannot be calculated.
* @param int $n Variable "n" to substitute.
* @return int Plural form value.
public function execute( $n ) {
$total = count( $this->tokens );
$next = $this->tokens[ $i ];
if ( 'var' === $next[0] ) {
} elseif ( 'value' === $next[0] ) {
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$v3 = array_pop( $stack );
$v2 = array_pop( $stack );
$v1 = array_pop( $stack );
$stack[] = $v1 ? $v2 : $v3;
throw new Exception( sprintf( 'Unknown operator "%s"', $next[1] ) );
if ( count( $stack ) !== 1 ) {
throw new Exception( 'Too many values remaining on the stack' );