4 * This file contains functions to convert callback templates to other languages.
5 * The template should first be pre-processed with CBTProcessor to remove static
10 require_once( dirname( __FILE__
) . '/CBTProcessor.php' );
13 * Push a value onto the stack
16 define( 'CBT_PUSH', 1 );
19 * Pop, concatenate argument, push
22 define( 'CBT_CAT', 2 );
25 * Concatenate where the argument is on the stack, instead of immediate
27 define( 'CBT_CATS', 3 );
30 * Call a function, push the return value onto the stack and put it in the cache
31 * Argument 1: argument count
33 * The arguments to the function are on the stack
35 define( 'CBT_CALL', 4 );
38 * Pop, htmlspecialchars, push
40 define( 'CBT_HX', 5 );
47 function CBTOp( $opcode, $arg1, $arg2 ) {
48 $this->opcode
= $opcode;
61 return $opcodeNames[$this->opcode
];
69 function CBTCompiler( $text ) {
75 * Returns true on success, error message on failure
78 $this->mLastError
= false;
79 $this->mOps
= array();
81 $this->doText( 0, strlen( $this->mText
) );
83 if ( $this->mLastError
!== false ) {
84 $pos = $this->mErrorPos
;
86 // Find the line number at which the error occurred
92 $startLine = $endLine +
1;
94 $endLine = strpos( $this->mText
, "\n", $startLine );
96 } while ( $endLine !== false && $endLine < $pos );
98 $text = "Template error at line $line: $this->mLastError\n<pre>\n";
100 $context = rtrim( str_replace( "\t", " ", substr( $this->mText
, $startLine, $endLine - $startLine ) ) );
101 $text .= htmlspecialchars( $context ) . "\n" . str_repeat( ' ', $pos - $startLine ) . "^\n</pre>\n";
109 /** Shortcut for doOpenText( $start, $end, false */
110 function doText( $start, $end ) {
111 return $this->doOpenText( $start, $end, false );
114 function phpQuote( $text ) {
115 return "'" . strtr( $text, array( "\\" => "\\\\", "'" => "\\'" ) ) . "'";
118 function op( $opcode, $arg1 = null, $arg2 = null) {
119 return new CBTOp( $opcode, $arg1, $arg2 );
123 * Recursive workhorse for text mode.
125 * Processes text mode starting from offset $p, until either $end is
126 * reached or a closing brace is found. If $needClosing is false, a
127 * closing brace will flag an error, if $needClosing is true, the lack
128 * of a closing brace will flag an error.
130 * The parameter $p is advanced to the position after the closing brace,
131 * or after the end. A CBTValue is returned.
135 function doOpenText( &$p, $end, $needClosing = true ) {
140 $foundClosing = false;
141 while ( $p < $end ) {
142 $matchLength = strcspn( $in, CBT_BRACE
, $p, $end - $p );
143 $pToken = $p +
$matchLength;
145 if ( $pToken >= $end ) {
146 // No more braces, output remainder
148 $this->mOps
[] = $this->op( CBT_PUSH
, substr( $in, $p ) );
151 $this->mOps
[] = $this->op( CBT_CAT
, substr( $in, $p ) );
157 // Output the text before the brace
159 $this->mOps
[] = $this->op( CBT_PUSH
, substr( $in, $p, $matchLength ) );
162 $this->mOps
[] = $this->op( CBT_CAT
, substr( $in, $p, $matchLength ) );
165 // Advance the pointer
168 // Check for closing brace
169 if ( $in[$pToken] == '}' ) {
170 $foundClosing = true;
174 // Handle the "{fn}" special case
175 if ( $pToken > 0 && $in[$pToken-1] == '"' ) {
176 $this->doOpenFunction( $p, $end );
177 if ( $p < $end && $in[$p] == '"' ) {
178 $this->mOps
[] = $this->op( CBT_HX
);
181 $this->doOpenFunction( $p, $end );
186 $this->mOps
[] = $this->op( CBT_CATS
);
189 if ( $foundClosing && !$needClosing ) {
190 $this->error( 'Errant closing brace', $p );
191 } elseif ( !$foundClosing && $needClosing ) {
192 $this->error( 'Unclosed text section', $start );
195 $this->mOps
[] = $this->op( CBT_PUSH
, '' );
201 * Recursive workhorse for function mode.
203 * Processes function mode starting from offset $p, until either $end is
204 * reached or a closing brace is found. If $needClosing is false, a
205 * closing brace will flag an error, if $needClosing is true, the lack
206 * of a closing brace will flag an error.
208 * The parameter $p is advanced to the position after the closing brace,
209 * or after the end. A CBTValue is returned.
213 function doOpenFunction( &$p, $end, $needClosing = true ) {
218 $foundClosing = false;
219 while ( $p < $end ) {
221 if ( $char == '{' ) {
222 // Switch to text mode
224 $this->doOpenText( $p, $end );
226 } elseif ( $char == '}' ) {
229 $foundClosing = true;
231 } elseif ( false !== strpos( CBT_WHITE
, $char ) ) {
233 // Consume the rest of the whitespace
234 $p +
= strspn( $in, CBT_WHITE
, $p, $end - $p );
236 // Token, find the end of it
237 $tokenLength = strcspn( $in, CBT_DELIM
, $p, $end - $p );
238 $this->mOps
[] = $this->op( CBT_PUSH
, substr( $in, $p, $tokenLength ) );
240 // Execute the token as a function if it's not the function name
242 $this->mOps
[] = $this->op( CBT_CALL
, 1 );
249 if ( !$foundClosing && $needClosing ) {
250 $this->error( 'Unclosed function', $start );
254 $this->mOps
[] = $this->op( CBT_CALL
, $argCount );
258 * Set a flag indicating that an error has been found.
260 function error( $text, $pos = false ) {
261 $this->mLastError
= $text;
262 if ( $pos === false ) {
263 $this->mErrorPos
= $this->mCurrentPos
;
265 $this->mErrorPos
= $pos;
269 function getLastError() {
270 return $this->mLastError
;
273 function opsToString() {
275 foreach( $this->mOps
as $op ) {
277 if ( !is_null( $op->arg1
) ) {
278 $s .= ' ' . var_export( $op->arg1
, true );
280 if ( !is_null( $op->arg2
) ) {
281 $s .= ' ' . var_export( $op->arg2
, true );
288 function generatePHP( $functionObj ) {
289 $fname = 'CBTCompiler::generatePHP';
290 wfProfileIn( $fname );
293 foreach( $this->mOps
as $op ) {
294 switch( $op->opcode
) {
296 $stack[] = $this->phpQuote( $op->arg1
);
299 $val = array_pop( $stack );
300 array_push( $stack, "$val . " . $this->phpQuote( $op->arg1
) );
303 $right = array_pop( $stack );
304 $left = array_pop( $stack );
305 array_push( $stack, "$left . $right" );
308 $args = array_slice( $stack, count( $stack ) - $op->arg1
, $op->arg1
);
309 $stack = array_slice( $stack, 0, count( $stack ) - $op->arg1
);
311 // Some special optimised expansions
312 if ( $op->arg1
== 0 ) {
315 $func = array_shift( $args );
316 if ( substr( $func, 0, 1 ) == "'" && substr( $func, -1 ) == "'" ) {
317 $func = substr( $func, 1, strlen( $func ) - 2 );
318 if ( $func == "if" ) {
319 if ( $op->arg1
< 3 ) {
320 // This should have been caught during processing
321 return "Not enough arguments to if";
322 } elseif ( $op->arg1
== 3 ) {
323 $result = "(({$args[0]} != '') ? ({$args[1]}) : '')";
325 $result = "(({$args[0]} != '') ? ({$args[1]}) : ({$args[2]}))";
327 } elseif ( $func == "true" ) {
329 } elseif( $func == "lbrace" ||
$func == "{" ) {
331 } elseif( $func == "rbrace" ||
$func == "}" ) {
333 } elseif ( $func == "escape" ||
$func == "~" ) {
334 $result = "htmlspecialchars({$args[0]})";
336 // Known function name
337 $result = "{$functionObj}->{$func}(" . implode( ', ', $args ) . ')';
340 // Unknown function name
341 $result = "call_user_func(array($functionObj, $func), " . implode( ', ', $args ) . ' )';
344 array_push( $stack, $result );
347 $val = array_pop( $stack );
348 array_push( $stack, "htmlspecialchars( $val )" );
351 return "Unknown opcode {$op->opcode}\n";
354 wfProfileOut( $fname );
355 if ( count( $stack ) !== 1 ) {
356 return "Error, stack count incorrect\n";
359 global $cbtExecutingGenerated;
360 ++$cbtExecutingGenerated;
361 $output = ' . $stack[0] . ';
362 --$cbtExecutingGenerated;