Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / languages / utils / CLDRPluralRuleConverterOperator.php
blobde17f291127bdc730ed807cb2cb0e31352528141
1 <?php
2 /**
3 * @author Niklas Laxström, Tim Starling
5 * @copyright Copyright © 2010-2012, Niklas Laxström
6 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 * @file
9 * @since 1.20
12 /**
13 * Helper for CLDRPluralRuleConverter.
14 * An operator object, representing a region of the input string (for error
15 * messages), and the binary operator at that location.
17 class CLDRPluralRuleConverterOperator extends CLDRPluralRuleConverterFragment {
18 /** @var string The name */
19 public $name;
21 /**
22 * Each op type has three characters: left operand type, right operand type and result type
24 * b = boolean
25 * n = number
26 * r = range
28 * A number is a kind of range.
30 * @var array
32 private static $opTypes = array(
33 'or' => 'bbb',
34 'and' => 'bbb',
35 'is' => 'nnb',
36 'is-not' => 'nnb',
37 'in' => 'nrb',
38 'not-in' => 'nrb',
39 'within' => 'nrb',
40 'not-within' => 'nrb',
41 'mod' => 'nnn',
42 ',' => 'rrr',
43 '..' => 'nnr',
46 /**
47 * Map converting from the abbrevation to the full form.
49 * @var array
51 private static $typeSpecMap = array(
52 'b' => 'boolean',
53 'n' => 'number',
54 'r' => 'range',
57 /**
58 * Map for converting the new operators introduced in Rev 33 to the old forms
60 private static $aliasMap = array(
61 '%' => 'mod',
62 '!=' => 'not-in',
63 '=' => 'in'
66 /**
67 * Initialize a new instance of a CLDRPluralRuleConverterOperator object
69 * @param CLDRPluralRuleConverter $parser The parser
70 * @param string $name The operator name
71 * @param int $pos The length
72 * @param int $length
74 function __construct( $parser, $name, $pos, $length ) {
75 parent::__construct( $parser, $pos, $length );
76 if ( isset( self::$aliasMap[$name] ) ) {
77 $name = self::$aliasMap[$name];
79 $this->name = $name;
82 /**
83 * Compute the operation
85 * @param CLDRPluralRuleConverterExpression $left The left part of the expression
86 * @param CLDRPluralRuleConverterExpression $right The right part of the expression
87 * @return CLDRPluralRuleConverterExpression The result of the operation
89 public function operate( $left, $right ) {
90 $typeSpec = self::$opTypes[$this->name];
92 $leftType = self::$typeSpecMap[$typeSpec[0]];
93 $rightType = self::$typeSpecMap[$typeSpec[1]];
94 $resultType = self::$typeSpecMap[$typeSpec[2]];
96 $start = min( $this->pos, $left->pos, $right->pos );
97 $end = max( $this->end, $left->end, $right->end );
98 $length = $end - $start;
100 $newExpr = new CLDRPluralRuleConverterExpression( $this->parser, $resultType,
101 "{$left->rpn} {$right->rpn} {$this->name}",
102 $start, $length );
104 if ( !$left->isType( $leftType ) ) {
105 $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
108 if ( !$right->isType( $rightType ) ) {
109 $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );
112 return $newExpr;