equal
deleted
inserted
replaced
4 * A gettext Plural-Forms parser. |
4 * A gettext Plural-Forms parser. |
5 * |
5 * |
6 * @since 4.9.0 |
6 * @since 4.9.0 |
7 */ |
7 */ |
8 if ( ! class_exists( 'Plural_Forms', false ) ) : |
8 if ( ! class_exists( 'Plural_Forms', false ) ) : |
|
9 #[AllowDynamicProperties] |
9 class Plural_Forms { |
10 class Plural_Forms { |
10 /** |
11 /** |
11 * Operator characters. |
12 * Operator characters. |
12 * |
13 * |
13 * @since 4.9.0 |
14 * @since 4.9.0 |
107 |
108 |
108 switch ( $next ) { |
109 switch ( $next ) { |
109 // Ignore whitespace. |
110 // Ignore whitespace. |
110 case ' ': |
111 case ' ': |
111 case "\t": |
112 case "\t": |
112 $pos++; |
113 ++$pos; |
113 break; |
114 break; |
114 |
115 |
115 // Variable (n). |
116 // Variable (n). |
116 case 'n': |
117 case 'n': |
117 $output[] = array( 'var' ); |
118 $output[] = array( 'var' ); |
118 $pos++; |
119 ++$pos; |
119 break; |
120 break; |
120 |
121 |
121 // Parentheses. |
122 // Parentheses. |
122 case '(': |
123 case '(': |
123 $stack[] = $next; |
124 $stack[] = $next; |
124 $pos++; |
125 ++$pos; |
125 break; |
126 break; |
126 |
127 |
127 case ')': |
128 case ')': |
128 $found = false; |
129 $found = false; |
129 while ( ! empty( $stack ) ) { |
130 while ( ! empty( $stack ) ) { |
141 |
142 |
142 if ( ! $found ) { |
143 if ( ! $found ) { |
143 throw new Exception( 'Mismatched parentheses' ); |
144 throw new Exception( 'Mismatched parentheses' ); |
144 } |
145 } |
145 |
146 |
146 $pos++; |
147 ++$pos; |
147 break; |
148 break; |
148 |
149 |
149 // Operators. |
150 // Operators. |
150 case '|': |
151 case '|': |
151 case '&': |
152 case '&': |
186 $s_pos = count( $stack ) - 1; |
187 $s_pos = count( $stack ) - 1; |
187 while ( $s_pos >= 0 ) { |
188 while ( $s_pos >= 0 ) { |
188 $o2 = $stack[ $s_pos ]; |
189 $o2 = $stack[ $s_pos ]; |
189 if ( '?' !== $o2 ) { |
190 if ( '?' !== $o2 ) { |
190 $output[] = array( 'op', array_pop( $stack ) ); |
191 $output[] = array( 'op', array_pop( $stack ) ); |
191 $s_pos--; |
192 --$s_pos; |
192 continue; |
193 continue; |
193 } |
194 } |
194 |
195 |
195 // Replace. |
196 // Replace. |
196 $stack[ $s_pos ] = '?:'; |
197 $stack[ $s_pos ] = '?:'; |
199 } |
200 } |
200 |
201 |
201 if ( ! $found ) { |
202 if ( ! $found ) { |
202 throw new Exception( 'Missing starting "?" ternary operator' ); |
203 throw new Exception( 'Missing starting "?" ternary operator' ); |
203 } |
204 } |
204 $pos++; |
205 ++$pos; |
205 break; |
206 break; |
206 |
207 |
207 // Default - number or invalid. |
208 // Default - number or invalid. |
208 default: |
209 default: |
209 if ( $next >= '0' && $next <= '9' ) { |
210 if ( $next >= '0' && $next <= '9' ) { |
261 $stack = array(); |
262 $stack = array(); |
262 $i = 0; |
263 $i = 0; |
263 $total = count( $this->tokens ); |
264 $total = count( $this->tokens ); |
264 while ( $i < $total ) { |
265 while ( $i < $total ) { |
265 $next = $this->tokens[ $i ]; |
266 $next = $this->tokens[ $i ]; |
266 $i++; |
267 ++$i; |
267 if ( 'var' === $next[0] ) { |
268 if ( 'var' === $next[0] ) { |
268 $stack[] = $n; |
269 $stack[] = $n; |
269 continue; |
270 continue; |
270 } elseif ( 'value' === $next[0] ) { |
271 } elseif ( 'value' === $next[0] ) { |
271 $stack[] = $next[1]; |
272 $stack[] = $next[1]; |
317 break; |
318 break; |
318 |
319 |
319 case '!=': |
320 case '!=': |
320 $v2 = array_pop( $stack ); |
321 $v2 = array_pop( $stack ); |
321 $v1 = array_pop( $stack ); |
322 $v1 = array_pop( $stack ); |
322 $stack[] = $v1 != $v2; |
323 $stack[] = $v1 !== $v2; |
323 break; |
324 break; |
324 |
325 |
325 case '==': |
326 case '==': |
326 $v2 = array_pop( $stack ); |
327 $v2 = array_pop( $stack ); |
327 $v1 = array_pop( $stack ); |
328 $v1 = array_pop( $stack ); |
328 $stack[] = $v1 == $v2; |
329 $stack[] = $v1 === $v2; |
329 break; |
330 break; |
330 |
331 |
331 case '?:': |
332 case '?:': |
332 $v3 = array_pop( $stack ); |
333 $v3 = array_pop( $stack ); |
333 $v2 = array_pop( $stack ); |
334 $v2 = array_pop( $stack ); |