X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/vendor/wikimedia/cldr-plural-rule-parser/src/Converter/Operator.php diff --git a/vendor/wikimedia/cldr-plural-rule-parser/src/Converter/Operator.php b/vendor/wikimedia/cldr-plural-rule-parser/src/Converter/Operator.php new file mode 100644 index 00000000..3a23d39c --- /dev/null +++ b/vendor/wikimedia/cldr-plural-rule-parser/src/Converter/Operator.php @@ -0,0 +1,114 @@ + 'bbb', + 'and' => 'bbb', + 'is' => 'nnb', + 'is-not' => 'nnb', + 'in' => 'nrb', + 'not-in' => 'nrb', + 'within' => 'nrb', + 'not-within' => 'nrb', + 'mod' => 'nnn', + ',' => 'rrr', + '..' => 'nnr', + ); + + /** + * Map converting from the abbrevation to the full form. + * + * @var array + */ + private static $typeSpecMap = array( + 'b' => 'boolean', + 'n' => 'number', + 'r' => 'range', + ); + + /** + * Map for converting the new operators introduced in Rev 33 to the old forms + */ + private static $aliasMap = array( + '%' => 'mod', + '!=' => 'not-in', + '=' => 'in' + ); + + /** + * Initialize a new instance of a CLDRPluralRuleConverterOperator object + * + * @param Converter $parser The parser + * @param string $name The operator name + * @param int $pos The length + * @param int $length + */ + function __construct( Converter $parser, $name, $pos, $length ) { + parent::__construct( $parser, $pos, $length ); + if ( isset( self::$aliasMap[$name] ) ) { + $name = self::$aliasMap[$name]; + } + $this->name = $name; + } + + /** + * Compute the operation + * + * @param Expression $left The left part of the expression + * @param Expression $right The right part of the expression + * @return Expression The result of the operation + */ + public function operate( Expression $left, Expression $right ) { + $typeSpec = self::$opTypes[$this->name]; + + $leftType = self::$typeSpecMap[$typeSpec[0]]; + $rightType = self::$typeSpecMap[$typeSpec[1]]; + $resultType = self::$typeSpecMap[$typeSpec[2]]; + + $start = min( $this->pos, $left->pos, $right->pos ); + $end = max( $this->end, $left->end, $right->end ); + $length = $end - $start; + + $newExpr = new Expression( $this->parser, $resultType, + "{$left->rpn} {$right->rpn} {$this->name}", + $start, $length ); + + if ( !$left->isType( $leftType ) ) { + $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" ); + } + + if ( !$right->isType( $rightType ) ) { + $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" ); + } + + return $newExpr; + } +}