CBTCompiler.php 9.0 KB

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