/src/idl/schema.php

https://github.com/tmjnaid/hiphop-php · PHP · 342 lines · 210 code · 52 blank · 80 comment · 40 complexity · a71cf93757127bee5eeb201e6c114175 MD5 · raw file

  1. <?php
  2. require_once 'base.php';
  3. $ext = $argv[1];
  4. if (preg_match('/\.idl\.php/', $ext)) {
  5. require_once $ext;
  6. $ext = preg_replace('/\.idl\.php/', '', $ext);
  7. } else {
  8. require_once "$ext.idl.php";
  9. }
  10. $net = isset($argv[2]) ? $argv[2] : -1; // 1: always phpnet; 0; auto; -1; no
  11. ///////////////////////////////////////////////////////////////////////////////
  12. $output = <<<CODE
  13. <?php
  14. /**
  15. * Automatically generated by running "php schema.php $ext".
  16. *
  17. * You may modify the file, but re-running schema.php against this file will
  18. * standardize the format without losing your schema changes. It does lose
  19. * any changes that are not part of schema. Use "note" field to comment on
  20. * schema itself, and "note" fields are not used in any code generation but
  21. * only staying within this file.
  22. */
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // Preamble: C++ code inserted at beginning of ext_{name}.h
  25. DefinePreamble(<<<CPP
  26. $preamble
  27. CPP
  28. );
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // Constants
  31. //
  32. // array (
  33. // 'name' => name of the constant
  34. // 'type' => type of the constant
  35. // 'note' => additional note about this constant's schema
  36. // )
  37. CODE;
  38. define_constants($constants);
  39. $output .= <<<CODE
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // Functions
  42. //
  43. // array (
  44. // 'name' => name of the function
  45. // 'desc' => description of the function's purpose
  46. // 'flags' => attributes of the function, see base.php for possible values
  47. // 'opt' => optimization callback function's name for compiler
  48. // 'note' => additional note about this function's schema
  49. // 'return' =>
  50. // array (
  51. // 'type' => return type, use Reference for ref return
  52. // 'desc' => description of the return value
  53. // )
  54. // 'args' => arguments
  55. // array (
  56. // 'name' => name of the argument
  57. // 'type' => type of the argument, use Reference for output parameter
  58. // 'value' => default value of the argument
  59. // 'desc' => description of the argument
  60. // )
  61. // )
  62. CODE;
  63. foreach ($funcs as $func) {
  64. define_function($func);
  65. }
  66. $output .= <<<CODE
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // Classes
  69. //
  70. // BeginClass
  71. // array (
  72. // 'name' => name of the class
  73. // 'desc' => description of the class's purpose
  74. // 'flags' => attributes of the class, see base.php for possible values
  75. // 'note' => additional note about this class's schema
  76. // 'parent' => parent class name, if any
  77. // 'ifaces' => array of interfaces tihs class implements
  78. // 'bases' => extra internal and special base classes this class requires
  79. // 'footer' => extra C++ inserted at end of class declaration
  80. // )
  81. //
  82. // DefineConstant(..)
  83. // DefineConstant(..)
  84. // ...
  85. // DefineFunction(..)
  86. // DefineFunction(..)
  87. // ...
  88. // DefineProperty
  89. // DefineProperty
  90. //
  91. // array (
  92. // 'name' => name of the property
  93. // 'type' => type of the property
  94. // 'flags' => attributes of the property
  95. // 'desc' => description of the property
  96. // 'note' => additional note about this property's schema
  97. // )
  98. //
  99. // EndClass()
  100. CODE;
  101. foreach ($classes as $class) {
  102. define_class($class);
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////
  105. print $output;
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // output helpers
  108. function idx_flags($arr, $name) {
  109. return get_flag_names($arr, $name);
  110. }
  111. function idx_type($arr, $name) {
  112. return !empty($arr[$name]) ? get_idl_name($arr[$name]) : '';
  113. }
  114. function idx_string($arr, $name) {
  115. // not empty() testing to allow a string of '0'
  116. return array_key_exists($name, $arr) ? $arr[$name] : '';
  117. }
  118. function idx_array($arr, $name) {
  119. if (!empty($arr[$name])) {
  120. return "array('" . implode("', '", $arr[$name]) . "')";
  121. }
  122. return '';
  123. }
  124. function begin_function($name) {
  125. global $indent, $output;
  126. $output .= str_repeat(' ', $indent) . "$name(\n";
  127. $indent += 2;
  128. }
  129. function end_function() {
  130. global $indent, $output;
  131. $indent -= 2;
  132. $output .= str_repeat(' ', $indent) . ");\n\n";
  133. }
  134. function begin_array($leading = true) {
  135. global $indent, $output;
  136. if ($leading) {
  137. $output .= str_repeat(' ', $indent);
  138. }
  139. $output .= "array(\n";
  140. $indent += 2;
  141. }
  142. function end_array($trailing_comma=true) {
  143. global $indent, $output;
  144. $indent -= 2;
  145. $output .= str_repeat(' ', $indent) . ")";
  146. if ($trailing_comma) $output .= ",\n";
  147. }
  148. function push_globals($inc=4) {
  149. global $indent, $output, $saved;
  150. $saved = $output; $output = '';
  151. $indent += $inc;
  152. }
  153. function pop_globals($dec=4) {
  154. global $indent, $output, $saved;
  155. $ret = $output; $output = $saved;
  156. $indent -= $dec;
  157. return $ret;
  158. }
  159. function out_str($name, $var, $required=false, $formatted=false, $doc=false) {
  160. global $indent, $output;
  161. if ($required && ($var === null || $var === '')) {
  162. throw new Exception("missing definition for $name");
  163. }
  164. if ($required || !($var === null || $var === '')) {
  165. $name = str_pad("'$name'", 8);
  166. if (!$formatted) {
  167. $var = '"'.escape_php($var).'"';
  168. }
  169. $output .= str_repeat(' ', $indent) . "$name => ";
  170. if ($doc) {
  171. $output .= "<<<EOT\n$var\nEOT\n,\n";
  172. } else {
  173. $output .= "$var,\n";
  174. }
  175. }
  176. }
  177. function out_fmt($name, $var, $required=false) {
  178. return out_str($name, $var, $required, true);
  179. }
  180. function out_doc($name, $var, $required=false) {
  181. return out_str($name, $var, $required, true, true);
  182. }
  183. function define_class($class) {
  184. global $output, $net;
  185. $name = $class['name'];
  186. $desc = idx_string($class, 'desc');
  187. if ((empty($desc) || $net == 1) && $net != -1) {
  188. $desc = phpnet_get_class_desc($name);
  189. }
  190. $output .= "////////////////////////////////////////".
  191. "///////////////////////////////////////\n\n";
  192. begin_function('BeginClass');
  193. begin_array();
  194. out_str('name', $name, true);
  195. out_str('parent', $class['parent']);
  196. out_fmt('ifaces', idx_array ($class, 'ifaces'));
  197. out_fmt('bases', idx_array ($class, 'bases'));
  198. out_str('desc', $desc);
  199. out_fmt('flags', idx_flags ($class, 'flags'));
  200. out_str('note', idx_string($class, 'note'));
  201. out_doc('footer', idx_string($class, 'footer'));
  202. end_array(false);
  203. end_function();
  204. define_constants($class['consts']);
  205. foreach ($class['methods'] as $func) {
  206. define_function($func, $name);
  207. }
  208. define_properties($class['properties']);
  209. begin_function('EndClass');
  210. end_function();
  211. }
  212. function define_function($func, $clsname = 'function') {
  213. global $net;
  214. $phpnet = false;
  215. $desc = idx_string($func, 'desc');
  216. if ((empty($desc) || $net == 1) && $net != -1) {
  217. $phpnet = phpnet_get_function_info($func['name'], $clsname);
  218. if (!empty($phpnet['desc'])) $desc = ($phpnet['desc']);
  219. }
  220. // prepare return type
  221. $ret_desc = idx_string($func, 'ret_desc');
  222. if ((empty($ret_desc) || $net == 1) && $net != -1) {
  223. if (!empty($phpnet['ret'])) $ret_desc = $phpnet['ret'];
  224. }
  225. push_globals();
  226. begin_array(false);
  227. out_fmt('type', get_idl_name($func['return'], 'null'));
  228. out_str('desc', $ret_desc);
  229. end_array(false);
  230. $return = pop_globals();
  231. if ($func['args']) {
  232. push_globals();
  233. begin_array(false);
  234. $i = -1;
  235. foreach ($func['args'] as $arg) {
  236. $i++;
  237. $arg_desc = idx_string($arg, 'desc');
  238. if ((empty($arg_desc) || $net == 1) && $net != -1) {
  239. if (!empty($phpnet['params'][$i])) {
  240. $arg_desc = $phpnet['params'][$i];
  241. }
  242. }
  243. begin_array();
  244. out_str('name', $arg['name'], true);
  245. out_fmt('type', idx_type($arg, 'type'));
  246. out_str('value', idx_string($arg, 'default'));
  247. out_str('desc', $arg_desc);
  248. end_array();
  249. }
  250. end_array(false);
  251. $args = pop_globals();
  252. } else {
  253. $args = '';
  254. }
  255. begin_function('DefineFunction');
  256. begin_array();
  257. out_str('name', $func['name'], true);
  258. out_str('desc', $desc);
  259. out_fmt('flags', idx_flags($func, 'flags'));
  260. out_str('opt', idx_string($func, 'opt'));
  261. out_fmt('return', $return);
  262. out_fmt('args', $args);
  263. out_str('note', idx_string($func, 'note'));
  264. end_array(false);
  265. end_function();
  266. }
  267. function define_constants($consts) {
  268. foreach ($consts as $constant) {
  269. begin_function('DefineConstant');
  270. begin_array();
  271. out_str('name', $constant['name'], true);
  272. out_fmt('type', idx_type($constant, 'type'), true);
  273. out_str('note', idx_string($constant, 'note'));
  274. end_array(false);
  275. end_function();
  276. }
  277. }
  278. function define_properties($properties) {
  279. foreach ($properties as $property) {
  280. begin_function('DefineProperty');
  281. begin_array();
  282. out_str('name', $property['name'], true);
  283. out_fmt('type', idx_type ($property, 'type'));
  284. out_fmt('flags', idx_flags ($property, 'flags'));
  285. out_str('desc', idx_string($property, 'desc'));
  286. out_str('note', idx_string($property, 'note'));
  287. end_array(false);
  288. end_function();
  289. }
  290. }