summaryrefslogtreecommitdiff
path: root/includes/cbt/CBTCompiler.php
blob: 759557978c314c59a0094e216f9194009ee09e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php

/**
 * This file contains functions to convert callback templates to other languages.
 * The template should first be pre-processed with CBTProcessor to remove static
 * sections.
 */


require_once( dirname( __FILE__ ) . '/CBTProcessor.php' );

/**
 * Push a value onto the stack
 * Argument 1: value
 */
define( 'CBT_PUSH', 1 );

/**
 * Pop, concatenate argument, push
 * Argument 1: value
 */
define( 'CBT_CAT', 2 );

/**
 * Concatenate where the argument is on the stack, instead of immediate
 */
define( 'CBT_CATS', 3 );

/**
 * Call a function, push the return value onto the stack and put it in the cache
 * Argument 1: argument count
 *
 * The arguments to the function are on the stack
 */
define( 'CBT_CALL', 4 );

/**
 * Pop, htmlspecialchars, push
 */
define( 'CBT_HX', 5 );

class CBTOp {
	var $opcode;
	var $arg1;
	var $arg2;

	function CBTOp( $opcode, $arg1, $arg2 ) {
		$this->opcode = $opcode;
		$this->arg1 = $arg1;
		$this->arg2 = $arg2;
	}

	function name() {
		$opcodeNames = array(
			CBT_PUSH => 'PUSH',
			CBT_CAT => 'CAT',
			CBT_CATS => 'CATS',
			CBT_CALL => 'CALL',
			CBT_HX => 'HX',
		);
		return $opcodeNames[$this->opcode];
	}
};

class CBTCompiler {
	var $mOps = array();
	var $mCode;

	function CBTCompiler( $text ) {
		$this->mText = $text;
	}

	/**
	 * Compile the text.
	 * Returns true on success, error message on failure
	 */
	function compile() {
		$this->mLastError = false;
		$this->mOps = array();

		$this->doText( 0, strlen( $this->mText ) );

		if ( $this->mLastError !== false ) {
			$pos = $this->mErrorPos;

			// Find the line number at which the error occurred
			$startLine = 0;
			$endLine = 0;
			$line = 0;
			do {
				if ( $endLine ) {
					$startLine = $endLine + 1;
				}
				$endLine = strpos( $this->mText, "\n", $startLine );
				++$line;
			} while ( $endLine !== false && $endLine < $pos );

			$text = "Template error at line $line: $this->mLastError\n<pre>\n";

			$context = rtrim( str_replace( "\t", " ", substr( $this->mText, $startLine, $endLine - $startLine ) ) );
			$text .= htmlspecialchars( $context ) . "\n" . str_repeat( ' ', $pos - $startLine ) . "^\n</pre>\n";
		} else {
			$text = true;
		}

		return $text;
	}

	/** Shortcut for doOpenText( $start, $end, false */
	function doText( $start, $end ) {
		return $this->doOpenText( $start, $end, false );
	}

	function phpQuote( $text ) {
		return "'" . strtr( $text, array( "\\" => "\\\\", "'" => "\\'" ) ) . "'";
	}

	function op( $opcode, $arg1 = null, $arg2 = null) {
		return new CBTOp( $opcode, $arg1, $arg2 );
	}

	/**
	 * Recursive workhorse for text mode.
	 *
	 * Processes text mode starting from offset $p, until either $end is
	 * reached or a closing brace is found. If $needClosing is false, a
	 * closing brace will flag an error, if $needClosing is true, the lack
	 * of a closing brace will flag an error.
	 *
	 * The parameter $p is advanced to the position after the closing brace,
	 * or after the end. A CBTValue is returned.
	 *
	 * @private
	 */
	function doOpenText( &$p, $end, $needClosing = true ) {
		$in =& $this->mText;
		$start = $p;
		$atStart = true;

		$foundClosing = false;
		while ( $p < $end ) {
			$matchLength = strcspn( $in, CBT_BRACE, $p, $end - $p );
			$pToken = $p + $matchLength;

			if ( $pToken >= $end ) {
				// No more braces, output remainder
				if ( $atStart ) {
					$this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p ) );
					$atStart = false;
				} else {
					$this->mOps[] = $this->op( CBT_CAT, substr( $in, $p ) );
				}
				$p = $end;
				break;
			}

			// Output the text before the brace
			if ( $atStart ) {
				$this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p, $matchLength ) );
				$atStart = false;
			} else {
				$this->mOps[] = $this->op( CBT_CAT, substr( $in, $p, $matchLength ) );
			}

			// Advance the pointer
			$p = $pToken + 1;

			// Check for closing brace
			if ( $in[$pToken] == '}' ) {
				$foundClosing = true;
				break;
			}

			// Handle the "{fn}" special case
			if ( $pToken > 0 && $in[$pToken-1] == '"' ) {
				$this->doOpenFunction( $p, $end );
				if ( $p < $end && $in[$p] == '"' ) {
					$this->mOps[] = $this->op( CBT_HX );
				}
			} else {
				$this->doOpenFunction( $p, $end );
			}
			if ( $atStart ) {
				$atStart = false;
			} else {
				$this->mOps[] = $this->op( CBT_CATS );
			}
		}
		if ( $foundClosing && !$needClosing ) {
			$this->error( 'Errant closing brace', $p );
		} elseif ( !$foundClosing && $needClosing ) {
			$this->error( 'Unclosed text section', $start );
		} else {
			if ( $atStart ) {
				$this->mOps[] = $this->op( CBT_PUSH, '' );
			}
		}
	}

	/**
	 * Recursive workhorse for function mode.
	 *
	 * Processes function mode starting from offset $p, until either $end is
	 * reached or a closing brace is found. If $needClosing is false, a
	 * closing brace will flag an error, if $needClosing is true, the lack
	 * of a closing brace will flag an error.
	 *
	 * The parameter $p is advanced to the position after the closing brace,
	 * or after the end. A CBTValue is returned.
	 *
	 * @private
	 */
	function doOpenFunction( &$p, $end, $needClosing = true ) {
		$in =& $this->mText;
		$start = $p;
		$argCount = 0;

		$foundClosing = false;
		while ( $p < $end ) {
			$char = $in[$p];
			if ( $char == '{' ) {
				// Switch to text mode
				++$p;
				$this->doOpenText( $p, $end );
				++$argCount;
			} elseif ( $char == '}' ) {
				// Block end
				++$p;
				$foundClosing = true;
				break;
			} elseif ( false !== strpos( CBT_WHITE, $char ) ) {
				// Whitespace
				// Consume the rest of the whitespace
				$p += strspn( $in, CBT_WHITE, $p, $end - $p );
			} else {
				// Token, find the end of it
				$tokenLength = strcspn( $in, CBT_DELIM, $p, $end - $p );
				$this->mOps[] = $this->op( CBT_PUSH, substr( $in, $p, $tokenLength ) );

				// Execute the token as a function if it's not the function name
				if ( $argCount ) {
					$this->mOps[] = $this->op( CBT_CALL, 1 );
				}

				$p += $tokenLength;
				++$argCount;
			}
		}
		if ( !$foundClosing && $needClosing ) {
			$this->error( 'Unclosed function', $start );
			return '';
		}

		$this->mOps[] = $this->op( CBT_CALL, $argCount );
	}

	/**
	 * Set a flag indicating that an error has been found.
	 */
	function error( $text, $pos = false ) {
		$this->mLastError = $text;
		if ( $pos === false ) {
			$this->mErrorPos = $this->mCurrentPos;
		} else {
			$this->mErrorPos = $pos;
		}
	}

	function getLastError() {
		return $this->mLastError;
	}

	function opsToString() {
		$s = '';
		foreach( $this->mOps as $op ) {
			$s .= $op->name();
			if ( !is_null( $op->arg1 ) ) {
				$s .= ' ' . var_export( $op->arg1, true );
			}
			if ( !is_null( $op->arg2 ) ) {
				$s .= ' ' . var_export( $op->arg2, true );
			}
			$s .= "\n";
		}
		return $s;
	}

	function generatePHP( $functionObj ) {
		$fname = 'CBTCompiler::generatePHP';
		wfProfileIn( $fname );
		$stack = array();

		foreach( $this->mOps as $op ) {
			switch( $op->opcode ) {
				case CBT_PUSH:
					$stack[] = $this->phpQuote( $op->arg1 );
					break;
				case CBT_CAT:
					$val = array_pop( $stack );
					array_push( $stack, "$val . " . $this->phpQuote( $op->arg1 ) );
					break;
				case CBT_CATS:
					$right = array_pop( $stack );
					$left = array_pop( $stack );
					array_push( $stack, "$left . $right" );
					break;
				case CBT_CALL:
					$args = array_slice( $stack, count( $stack ) - $op->arg1, $op->arg1 );
					$stack = array_slice( $stack, 0, count( $stack ) - $op->arg1 );

					// Some special optimised expansions
					if ( $op->arg1 == 0 ) {
						$result = '';
					} else {
						$func = array_shift( $args );
						if ( substr( $func, 0, 1 ) == "'" &&  substr( $func, -1 ) == "'" ) {
							$func = substr( $func, 1, strlen( $func ) - 2 );
							if ( $func == "if" ) {
								if ( $op->arg1 < 3 ) {
									// This should have been caught during processing
									return "Not enough arguments to if";
								} elseif ( $op->arg1 == 3 ) {
									$result = "(({$args[0]} != '') ? ({$args[1]}) : '')";
								} else {
									$result = "(({$args[0]} != '') ? ({$args[1]}) : ({$args[2]}))";
								}
							} elseif ( $func == "true" ) {
								$result = "true";
							} elseif( $func == "lbrace" || $func == "{" ) {
								$result = "{";
							} elseif( $func == "rbrace" || $func == "}" ) {
								$result = "}";
							} elseif ( $func == "escape" || $func == "~" ) {
								$result = "htmlspecialchars({$args[0]})";
							} else {
								// Known function name
								$result = "{$functionObj}->{$func}(" . implode( ', ', $args ) . ')';
							}
						} else {
							// Unknown function name
							$result = "call_user_func(array($functionObj, $func), " . implode( ', ', $args ) . ' )';
						}
					}
					array_push( $stack, $result );
					break;
				case CBT_HX:
					$val = array_pop( $stack );
					array_push( $stack, "htmlspecialchars( $val )" );
					break;
				default:
					return "Unknown opcode {$op->opcode}\n";
			}
		}
		wfProfileOut( $fname );
		if ( count( $stack ) !== 1 ) {
			return "Error, stack count incorrect\n";
		}
		return '
			global $cbtExecutingGenerated;
			++$cbtExecutingGenerated;
			$output = ' . $stack[0] . ';
			--$cbtExecutingGenerated;
			return $output;
			';
	}
}