Rights log changes:
[lhc/web/wiklou.git] / includes / cbt / CBTCompiler.php
1 <?php
2
3 /**
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
6 * sections.
7 */
8
9
10 require_once( dirname( __FILE__ ) . '/CBTProcessor.php' );
11
12 /**
13 * Push a value onto the stack
14 * Argument 1: value
15 */
16 define( 'CBT_PUSH', 1 );
17
18 /**
19 * Pop, concatenate argument, push
20 * Argument 1: value
21 */
22 define( 'CBT_CAT', 2 );
23
24 /**
25 * Concatenate where the argument is on the stack, instead of immediate
26 */
27 define( 'CBT_CATS', 3 );
28
29 /**
30 * Call a function, push the return value onto the stack and put it in the cache
31 * Argument 1: argument count
32 *
33 * The arguments to the function are on the stack
34 */
35 define( 'CBT_CALL', 4 );
36
37 /**
38 * Pop, htmlspecialchars, push
39 */
40 define( 'CBT_HX', 5 );
41
42 class CBTOp {
43 var $opcode;
44 var $arg1;
45 var $arg2;
46
47 function CBTOp( $opcode, $arg1, $arg2 ) {
48 $this->opcode = $opcode;
49 $this->arg1 = $arg1;
50 $this->arg2 = $arg2;
51 }
52
53 function name() {
54 $opcodeNames = array(
55 CBT_PUSH => 'PUSH',
56 CBT_CAT => 'CAT',
57 CBT_CATS => 'CATS',
58 CBT_CALL => 'CALL',
59 CBT_HX => 'HX',
60 );
61 return $opcodeNames[$this->opcode];
62 }
63 };
64
65 class CBTCompiler {
66 var $mOps = array();
67 var $mCode;
68
69 function CBTCompiler( $text ) {
70 $this->mText = $text;
71 }
72
73 /**
74 * Compile the text.
75 * Returns true on success, error message on failure
76 */
77 function compile() {
78 $fname = 'CBTProcessor::compile';
79 $this->mLastError = false;
80 $this->mOps = array();
81
82 $this->doText( 0, strlen( $this->mText ) );
83
84 if ( $this->mLastError !== false ) {
85 $pos = $this->mErrorPos;
86
87 // Find the line number at which the error occurred
88 $startLine = 0;
89 $endLine = 0;
90 $line = 0;
91 do {
92 if ( $endLine ) {
93 $startLine = $endLine + 1;
94 }
95 $endLine = strpos( $this->mText, "\n", $startLine );
96 ++$line;
97 } while ( $endLine !== false && $endLine < $pos );
98
99 $text = "Template error at line $line: $this->mLastError\n<pre>\n";
100
101 $context = rtrim( str_replace( "\t", " ", substr( $this->mText, $startLine, $endLine - $startLine ) ) );
102 $text .= htmlspecialchars( $context ) . "\n" . str_repeat( ' ', $pos - $startLine ) . "^\n</pre>\n";
103 } else {
104 $text = true;
105 }
106
107 return $text;
108 }
109
110 /** Shortcut for doOpenText( $start, $end, false */
111 function doText( $start, $end ) {
112 return $this->doOpenText( $start, $end, false );
113 }
114
115 function phpQuote( $text ) {
116 return "'" . strtr( $text, array( "\\" => "\\\\", "'" => "\\'" ) ) . "'";
117 }
118
119 function op( $opcode, $arg1 = null, $arg2 = null) {
120 return new CBTOp( $opcode, $arg1, $arg2 );
121 }
122
123 /**
124 * Recursive workhorse for text mode.
125 *
126 * Processes text mode starting from offset $p, until either $end is
127 * reached or a closing brace is found. If $needClosing is false, a
128 * closing brace will flag an error, if $needClosing is true, the lack
129 * of a closing brace will flag an error.
130 *
131 * The parameter $p is advanced to the position after the closing brace,
132 * or after the end. A CBTValue is returned.
133 *
134 * @private
135 */
136 function doOpenText( &$p, $end, $needClosing = true ) {
137 $in =& $this->mText;
138 $start = $p;
139 $atStart = true;
140
141 $foundClosing = false;
142 while ( $p < $end ) {
143 $matchLength = strcspn( $in, CBT_BRACE, $p, $end - $p );
144 $pToken = $p + $matchLength;
145
146 if ( $pToken >= $end ) {
147 // No more braces, output remainder
148 if ( $atStart ) {
149 $this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p ) );
150 $atStart = false;
151 } else {
152 $this->mOps[] = $this->op( CBT_CAT, substr( $in, $p ) );
153 }
154 $p = $end;
155 break;
156 }
157
158 // Output the text before the brace
159 if ( $atStart ) {
160 $this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p, $matchLength ) );
161 $atStart = false;
162 } else {
163 $this->mOps[] = $this->op( CBT_CAT, substr( $in, $p, $matchLength ) );
164 }
165
166 // Advance the pointer
167 $p = $pToken + 1;
168
169 // Check for closing brace
170 if ( $in[$pToken] == '}' ) {
171 $foundClosing = true;
172 break;
173 }
174
175 // Handle the "{fn}" special case
176 if ( $pToken > 0 && $in[$pToken-1] == '"' ) {
177 $this->doOpenFunction( $p, $end );
178 if ( $p < $end && $in[$p] == '"' ) {
179 $this->mOps[] = $this->op( CBT_HX );
180 }
181 } else {
182 $this->doOpenFunction( $p, $end );
183 }
184 if ( $atStart ) {
185 $atStart = false;
186 } else {
187 $this->mOps[] = $this->op( CBT_CATS );
188 }
189 }
190 if ( $foundClosing && !$needClosing ) {
191 $this->error( 'Errant closing brace', $p );
192 } elseif ( !$foundClosing && $needClosing ) {
193 $this->error( 'Unclosed text section', $start );
194 } else {
195 if ( $atStart ) {
196 $this->mOps[] = $this->op( CBT_PUSH, '' );
197 }
198 }
199 }
200
201 /**
202 * Recursive workhorse for function mode.
203 *
204 * Processes function mode starting from offset $p, until either $end is
205 * reached or a closing brace is found. If $needClosing is false, a
206 * closing brace will flag an error, if $needClosing is true, the lack
207 * of a closing brace will flag an error.
208 *
209 * The parameter $p is advanced to the position after the closing brace,
210 * or after the end. A CBTValue is returned.
211 *
212 * @private
213 */
214 function doOpenFunction( &$p, $end, $needClosing = true ) {
215 $in =& $this->mText;
216 $start = $p;
217 $argCount = 0;
218
219 $foundClosing = false;
220 while ( $p < $end ) {
221 $char = $in[$p];
222 if ( $char == '{' ) {
223 // Switch to text mode
224 ++$p;
225 $tokenStart = $p;
226 $this->doOpenText( $p, $end );
227 ++$argCount;
228 } elseif ( $char == '}' ) {
229 // Block end
230 ++$p;
231 $foundClosing = true;
232 break;
233 } elseif ( false !== strpos( CBT_WHITE, $char ) ) {
234 // Whitespace
235 // Consume the rest of the whitespace
236 $p += strspn( $in, CBT_WHITE, $p, $end - $p );
237 } else {
238 // Token, find the end of it
239 $tokenLength = strcspn( $in, CBT_DELIM, $p, $end - $p );
240 $this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p, $tokenLength ) );
241
242 // Execute the token as a function if it's not the function name
243 if ( $argCount ) {
244 $this->mOps[] = $this->op( CBT_CALL, 1 );
245 }
246
247 $p += $tokenLength;
248 ++$argCount;
249 }
250 }
251 if ( !$foundClosing && $needClosing ) {
252 $this->error( 'Unclosed function', $start );
253 return '';
254 }
255
256 $this->mOps[] = $this->op( CBT_CALL, $argCount );
257 }
258
259 /**
260 * Set a flag indicating that an error has been found.
261 */
262 function error( $text, $pos = false ) {
263 $this->mLastError = $text;
264 if ( $pos === false ) {
265 $this->mErrorPos = $this->mCurrentPos;
266 } else {
267 $this->mErrorPos = $pos;
268 }
269 }
270
271 function getLastError() {
272 return $this->mLastError;
273 }
274
275 function opsToString() {
276 $s = '';
277 foreach( $this->mOps as $op ) {
278 $s .= $op->name();
279 if ( !is_null( $op->arg1 ) ) {
280 $s .= ' ' . var_export( $op->arg1, true );
281 }
282 if ( !is_null( $op->arg2 ) ) {
283 $s .= ' ' . var_export( $op->arg2, true );
284 }
285 $s .= "\n";
286 }
287 return $s;
288 }
289
290 function generatePHP( $functionObj ) {
291 $fname = 'CBTCompiler::generatePHP';
292 wfProfileIn( $fname );
293 $stack = array();
294
295 foreach( $this->mOps as $index => $op ) {
296 switch( $op->opcode ) {
297 case CBT_PUSH:
298 $stack[] = $this->phpQuote( $op->arg1 );
299 break;
300 case CBT_CAT:
301 $val = array_pop( $stack );
302 array_push( $stack, "$val . " . $this->phpQuote( $op->arg1 ) );
303 break;
304 case CBT_CATS:
305 $right = array_pop( $stack );
306 $left = array_pop( $stack );
307 array_push( $stack, "$left . $right" );
308 break;
309 case CBT_CALL:
310 $args = array_slice( $stack, count( $stack ) - $op->arg1, $op->arg1 );
311 $stack = array_slice( $stack, 0, count( $stack ) - $op->arg1 );
312
313 // Some special optimised expansions
314 if ( $op->arg1 == 0 ) {
315 $result = '';
316 } else {
317 $func = array_shift( $args );
318 if ( substr( $func, 0, 1 ) == "'" && substr( $func, -1 ) == "'" ) {
319 $func = substr( $func, 1, strlen( $func ) - 2 );
320 if ( $func == "if" ) {
321 if ( $op->arg1 < 3 ) {
322 // This should have been caught during processing
323 return "Not enough arguments to if";
324 } elseif ( $op->arg1 == 3 ) {
325 $result = "(({$args[0]} != '') ? ({$args[1]}) : '')";
326 } else {
327 $result = "(({$args[0]} != '') ? ({$args[1]}) : ({$args[2]}))";
328 }
329 } elseif ( $func == "true" ) {
330 $result = "true";
331 } elseif( $func == "lbrace" || $func == "{" ) {
332 $result = "{";
333 } elseif( $func == "rbrace" || $func == "}" ) {
334 $result = "}";
335 } elseif ( $func == "escape" || $func == "~" ) {
336 $result = "htmlspecialchars({$args[0]})";
337 } else {
338 // Known function name
339 $result = "{$functionObj}->{$func}(" . implode( ', ', $args ) . ')';
340 }
341 } else {
342 // Unknown function name
343 $result = "call_user_func(array($functionObj, $func), " . implode( ', ', $args ) . ' )';
344 }
345 }
346 array_push( $stack, $result );
347 break;
348 case CBT_HX:
349 $val = array_pop( $stack );
350 array_push( $stack, "htmlspecialchars( $val )" );
351 break;
352 default:
353 return "Unknown opcode {$op->opcode}\n";
354 }
355 }
356 wfProfileOut( $fname );
357 if ( count( $stack ) !== 1 ) {
358 return "Error, stack count incorrect\n";
359 }
360 return '
361 global $cbtExecutingGenerated;
362 ++$cbtExecutingGenerated;
363 $output = ' . $stack[0] . ';
364 --$cbtExecutingGenerated;
365 return $output;
366 ';
367 }
368 }
369 ?>