objp_parser.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. class TObjPParser extends TPasCocoaParser {
  3. var $trailing_underscore = true;
  4. // ignore these classes when testing for ivar size
  5. var $ignore_class_ivar_comparison = array( "NSNibOutletConnector", "NSNibConnector", "NSNibControlConnector", "NSPredicateEditorRowTemplate", "NSSegmentedCell",
  6. "NSSimpleHorizontalTypesetter", "NSInvocation", "NSPointerFunctions", "NSConstantString");
  7. // These methods require that the last parameter append a trailing underscore (if $trailing_underscore is on)
  8. var $trailing_underscore_methods = array("- (void)copy:(id)sender;", "- (void)setNeedsDisplay:(BOOL)flag;","- (void*)QTMovie;","- (QTMovie *)QTMovie;","- (BOOL)load:(NSError **)error;");
  9. var $ignore_methods = array("observationInfo");
  10. // Converts an Objective-c method name to Pascal
  11. function ConvertObjcMethodName ($method) {
  12. $params = explode(":", $method);
  13. $name = "";
  14. $count = 0;
  15. if (count($params) > 1) {
  16. foreach ($params as $value) {
  17. if (eregi("([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1]."_";
  18. }
  19. } else {
  20. if (eregi("([a-zA-Z0-9]+)(;)*$", $params[0], $captures)) $name .= $captures[1]."_";
  21. }
  22. // clean it up
  23. if ($this->trailing_underscore) {
  24. if (!in_array($method, $this->trailing_underscore_methods)) $name = trim($name, "_");
  25. }
  26. $name = $this->ReplaceObjcType($name);
  27. return $name;
  28. }
  29. // Converts an Objective-C method to Pascal format
  30. function ConvertObjcMethodToPascal ($class, $source, $parts, $protected_keywords, $has_params, $deprecatedmods) {
  31. //print("$source\n");
  32. //print_r($parts);
  33. // replace "hinted" params comment with hinted type
  34. if ($this->replace_hinted_params) {
  35. // param string
  36. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[4], $captures)) {
  37. // ??? change the parameter to the hinted type
  38. //$parts[4] = eregi_replace("(/\*.*\*/)", $captures[2], $parts[4]);
  39. //$parts[4] = trim($parts[4], " ");
  40. }
  41. // return type
  42. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[2], $captures)) $parts[2] = $this->ReplaceRemoteMessagingModifiers($captures[2], $null);
  43. //print_r($parts);
  44. } else { // remove comments from params and return type
  45. $parts[4] = eregi_replace("(/\*.*\*/)", "", $parts[4]);
  46. $parts[4] = trim($parts[4]);
  47. $parts[2] = eregi_replace("(/\*.*\*/)", "", $parts[2]);
  48. $parts[2] = $this->ReplaceRemoteMessagingModifiers($parts[2], $null);
  49. }
  50. $return_type_clean = $parts[2];
  51. $return_type_pointers = preg_replace("![^*]+!e", "", $return_type_clean);
  52. $return_type_clean = trim($return_type_clean,"* ");
  53. // perform preformatting before attempting to protect keywords
  54. $parts[2] = $this->FormatObjcType($parts[2], $modifiers);
  55. $parts[4] = $this->FormatObjcParams($parts[4], $variable_arguments);
  56. //print($parts[4]."\n");
  57. if ($has_params) {
  58. $name = $this->ConvertObjcMethodName($source);
  59. // merge default protected keywords for the class/category
  60. if ($this->default_protected["*"]) $protected_keywords = array_merge($this->default_protected["*"], $protected_keywords);
  61. if ($this->default_protected[$class]) $protected_keywords = array_merge($this->default_protected[$class], $protected_keywords);
  62. $param_array = $this->ConvertObjcParamsToPascal($parts[4], $protected_keywords);
  63. $params = "(".$param_array["string"].")";
  64. $params_with_modifiers = "(".$param_array["string_with_modifiers"].")";
  65. } else {
  66. $params = "";
  67. $params_with_modifiers = "";
  68. // no parameters -> definitely no underscore normally, but there are some
  69. // conflicts...
  70. $name = $parts[3];
  71. // clean it up
  72. if ($this->trailing_underscore) {
  73. if (in_array($source, $this->trailing_underscore_methods)) $name = $name . "_";
  74. }
  75. $param_array = null;
  76. $variable_arguments = false;
  77. }
  78. // protect method name from keywords
  79. if ($this->IsKeywordReserved($name)) $name .= "_";
  80. // replace objc type
  81. $return_type = $this->ConvertReturnType($return_type_clean,$return_type_pointers);
  82. $virtual = "";
  83. $class_prefix = "";
  84. // determine the type based on return value
  85. if (ereg($this->regex_procedure_type, $return_type_clean.$return_type_pointers)) {
  86. $kind = "procedure";
  87. } else {
  88. $kind = "function";
  89. }
  90. // determine if this is a class method
  91. if ($parts[1] == "+") {
  92. $class_prefix = "class ";
  93. // These methods probably return the an allocated instance of the class, a typical convenience method.
  94. // ??? Ack! $class may be the category or protocol name
  95. //if ($return_type == $this->objc_id) $return_type = $class;
  96. }
  97. // Replace SEL with the string equivalent
  98. if ($this->register_selectors) {
  99. $params_with_modifiers = str_replace_word("SEL", $this->sel_string, $params_with_modifiers);
  100. }
  101. // make method templates
  102. if ($kind != "function") {
  103. if ($variable_arguments) $modifier .= " varargs;";
  104. $method = "$class_prefix$kind $name$params_with_modifiers;$modifier$virtual";
  105. $method_template = "[KIND] [PREFIX]$name"."[PARAMS];$modifier";
  106. } else {
  107. if ($variable_arguments) $return_type = "$return_type; varargs";
  108. $method = $class_prefix."function $name$params_with_modifiers: $return_type;$modifier$virtual";
  109. $method_template = "[KIND] [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  110. }
  111. $method_template_procedure = "procedure [PREFIX]$name"."[PARAMS];$modifier";
  112. $method_template_function = "function [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  113. // build structure
  114. $struct["def"] = $method;
  115. $struct["template"] = $method_template;
  116. $struct["template_function"] = $method_template_function;
  117. $struct["template_procedure"] = $method_template_procedure;
  118. $struct["objc_method"] = $this->CopyObjcMethodName($source);
  119. $struct["class_prefix"] = $class_prefix;
  120. if ($deprecatedmods != "") $struct["deprecated"] = $deprecatedmods.";";
  121. //$struct["def_objc"] = eregi("(.*);", $source, $captures[1]);
  122. if ($return_type == "void") $return_type = "";
  123. $struct["return"] = $return_type;
  124. if (in_array($return_type, $this->cocoa_classes)) $struct["returns_wrapper"] = true;
  125. $struct["param_string_clean"] = trim($params, "()");
  126. $struct["param_string_clean_with_modifiers"] = trim($params_with_modifiers, "()");
  127. $struct["param_string"] = $params;
  128. $struct["param_string_with_modifiers"] = $params_with_modifiers;
  129. $struct["param_array"] = $param_array["pairs"];
  130. $struct["param_list"] = $param_array["list"];
  131. $struct["class"] = $class;
  132. $struct["name"] = $name;
  133. $struct["kind"] = $kind;
  134. if ($struct["param_array"] != null) $struct["has_params"] = true;
  135. // FPC bug work around
  136. if (strlen($name) > $this->maximum_method_length) {
  137. $struct["can_override"] = false;
  138. print(" # WARNING: method $name can't override because the name is too long\n");
  139. $this->warning_count ++;
  140. }
  141. return $struct;
  142. }
  143. function InsertPatches ($header) {
  144. $path = "$this->root/patches/".$header["name_clean"].".patch";
  145. if ($handle = @fopen($path, "r")) {
  146. $text = ReadTextFile($path);
  147. $this->PrintOutput(0, $text);
  148. fclose($handle);
  149. }
  150. }
  151. function HeaderContainsPatch ($header) {
  152. if ($handle = @fopen("$this->root/patches/".$header["name_clean"].".patch", "r")) {
  153. fclose($handle);
  154. return true;
  155. }
  156. }
  157. function PrintGlobalClassInfo($all_classes, $defined_classes, $anon_classes) {
  158. // add all classes as anonymous external classes to a separate unit.
  159. // They will be overridden by the actual definitions in the translated
  160. // headers part of the main unit, but this way they can appear as record
  161. // field types and as callback parameters
  162. // open the output file if we not printing to terminal
  163. if (!$this->show) {
  164. $this->output = fopen("$this->root$this->out/AnonClassDefinitions".ucfirst($this->framework).".pas", "w+");
  165. }
  166. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  167. $date = @date("D M j G:i:s T Y");
  168. $this->PrintOutput(0, "");
  169. // allows parameter names conflicting with field names
  170. $this->PrintOutput(0, "{\$mode delphi}");
  171. $this->PrintOutput(0, "{\$modeswitch objectivec1}");
  172. // enables "external" after the semi-colon
  173. $this->PrintOutput(0, "{\$modeswitch cvar}");
  174. $this->PrintOutput(0, "");
  175. $this->PrintOutPut(0,"unit AnonClassDefinitions".ucfirst($this->framework).";");
  176. $this->PrintOutput(0, "");
  177. $this->PrintOutput(0, "interface");
  178. $this->PrintOutput(0, "");
  179. $this->PrintOutput(0, "type");
  180. foreach ($all_classes as $class)
  181. $this->PrintOutput(1, $class." = objcclass; external;");
  182. $this->PrintOutput(0, "");
  183. $this->PrintOutput(0, "implementation");
  184. $this->PrintOutput(0, "");
  185. $this->PrintOutput(0, "end.");
  186. // Now all anonymous external classes that have no real definition to an
  187. // include file that is added to the main unit. This way it is possible
  188. // to declare variables of these types in user programs without having to
  189. // include the unit above will all anonymous classes (should not be used)
  190. // open the output file if we not printing to terminal
  191. if (!$this->show) {
  192. $this->output = fopen("$this->root$this->out/$this->framework/AnonIncludeClassDefinitions".ucfirst($this->framework).".inc", "w+");
  193. }
  194. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  195. $date = @date("D M j G:i:s T Y");
  196. // add all classes as anonymous external classes. They will be overridden
  197. // by the actual definitions in the translated headers, but this way they
  198. // can appear as record field types and as callback parameters
  199. $first = true;
  200. foreach ($anon_classes as $class) {
  201. if (!in_array($class,$defined_classes)) {
  202. if ($first) {
  203. $this->PrintOutput(0, "type");
  204. $first = false;
  205. }
  206. $this->PrintOutput(1, $class." = objcclass; external;");
  207. }
  208. }
  209. }
  210. // Prints all classes from the header in Objective-P FPC format
  211. function PrintHeader ($header) {
  212. global $version;
  213. //print_r($header);
  214. //print_r($this->dump["categories"]);
  215. // open the output file if we not printing to terminal
  216. if (!$this->show) {
  217. if ($this->merge_headers) {
  218. $this->output = fopen($header["path_merge"], "w+");
  219. } else {
  220. $this->output = fopen($header["path"], "w+");
  221. }
  222. }
  223. $this->PrintOutput(0, "{ Parsed from ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  224. $date = @date("D M j G:i:s T Y");
  225. $this->PrintOutput(0, "{ Version: $version - $date }");
  226. $this->PrintOutput(0, "");
  227. $macro = strtoupper(substr($header["name"], 0, (strripos($header["name"], "."))));
  228. $this->PrintOutput(0, "");
  229. $this->PrintOutput(0, "{\$ifdef TYPES}");
  230. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_T}");
  231. $this->PrintOutput(0, "{\$define $macro"."_PAS_T}");
  232. $this->PrintTypes($header, false);
  233. $this->PrintOutput(0, "");
  234. $this->PrintOutput(0, "{\$endif}");
  235. $this->PrintOutput(0, "{\$endif}");
  236. $this->PrintOutput(0, "");
  237. $this->PrintOutput(0, "{\$ifdef RECORDS}");
  238. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_R}");
  239. $this->PrintOutput(0, "{\$define $macro"."_PAS_R}");
  240. // Records from types
  241. $this->PrintRecords($header);
  242. $this->PrintOutput(0, "");
  243. $this->PrintOutput(0, "{\$endif}");
  244. $this->PrintOutput(0, "{\$endif}");
  245. $this->PrintOutput(0, "");
  246. $this->PrintOutput(0, "{\$ifdef FUNCTIONS}");
  247. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_F}");
  248. $this->PrintOutput(0, "{\$define $macro"."_PAS_F}");
  249. $this->PrintFunctions($header);
  250. $this->PrintOutput(0, "");
  251. $this->PrintOutput(0, "{\$endif}");
  252. $this->PrintOutput(0, "{\$endif}");
  253. $this->PrintOutput(0, "");
  254. $this->PrintOutput(0, "{\$ifdef EXTERNAL_SYMBOLS}");
  255. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_S}");
  256. $this->PrintOutput(0, "{\$define $macro"."_PAS_S}");
  257. $this->PrintExternalSymbols($header);
  258. $this->PrintOutput(0, "");
  259. $this->PrintOutput(0, "{\$endif}");
  260. $this->PrintOutput(0, "{\$endif}");
  261. // insert user patches
  262. if ($this->HeaderContainsPatch($header)) {
  263. $this->PrintOutput(0, "");
  264. $this->PrintOutput(0, "{\$ifdef USER_PATCHES}");
  265. //$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_PATCH}");
  266. //$this->PrintOutput(0, "{\$define $macro"."_PAS_PATCH}");
  267. $this->InsertPatches($header);
  268. $this->PrintOutput(0, "");
  269. //$this->PrintOutput(0, "{\$endif}");
  270. $this->PrintOutput(0, "{\$endif}");
  271. }
  272. if (($header["classes"]) || ($header["protocols"])) {
  273. $this->PrintOutput(0, "");
  274. $this->PrintOutput(0, "{\$ifdef FORWARD}");
  275. if ($header["protocols"]) {
  276. foreach ($header["protocols"] as $protocol) $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol;");
  277. }
  278. if ($header["classes"]) {
  279. foreach ($header["classes"] as $class) {
  280. if ($class["name"]) {
  281. $this->PrintOutput(1, $class["name"]." = objcclass;");
  282. $this->PrintOutput(1, $class["name"].$this->class_pointer_suffix." = ^".$class["name"].";");
  283. // for consistency also offer Ptr-name variant
  284. $this->PrintOutput(1, $class["name"]."Ptr = ".$class["name"].$this->class_pointer_suffix.";");
  285. }
  286. }
  287. }
  288. $this->PrintOutput(0, "");
  289. $this->PrintOutput(0, "{\$endif}");
  290. }
  291. if ($header["classes"]) {
  292. $this->PrintOutput(0, "");
  293. $this->PrintOutput(0, "{\$ifdef CLASSES}");
  294. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_C}");
  295. $this->PrintOutput(0, "{\$define $macro"."_PAS_C}");
  296. foreach ($header["classes"] as $class) {
  297. if ($class["name"]) $this->PrintClass($class);
  298. }
  299. if (count($header["categories"]) > 0) {
  300. foreach ($header["categories"] as $category) {
  301. $this->PrintCategory($class, $category);
  302. }
  303. }
  304. $this->PrintOutput(0, "");
  305. $this->PrintOutput(0, "{\$endif}");
  306. $this->PrintOutput(0, "{\$endif}");
  307. }
  308. if ($header["protocols"]) {
  309. $this->PrintOutput(0, "{\$ifdef PROTOCOLS}");
  310. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_P}");
  311. $this->PrintOutput(0, "{\$define $macro"."_PAS_P}");
  312. foreach ($header["protocols"] as $protocol) {
  313. $this->PrintOutput(1, "");
  314. $this->PrintOutput(0, "{ ".$protocol["name"]." Protocol }");
  315. if ($protocol["comment"]) $this->PrintOutput(0, $protocol["comment"]);
  316. $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol");
  317. // print methods
  318. if ($protocol["methods"]) {
  319. foreach ($protocol["methods"] as $name => $method) {
  320. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  321. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  322. }
  323. }
  324. $this->PrintOutput(1, "end; external name '".$protocol["name"]."';");
  325. }
  326. $this->PrintOutput(0, "{\$endif}");
  327. $this->PrintOutput(0, "{\$endif}");
  328. }
  329. }
  330. function PrintCategory ($class, $category) {
  331. // declare real category if external
  332. if ($category["external"]) {
  333. $new_name = " name '".$category["external_name"]."'";
  334. }
  335. $category_name = $category["name"].$this->category_suffix;
  336. $this->PrintOutput(0, "");
  337. $this->PrintOutput(0, "{ $category_name }");
  338. if ($category["comment"]) $this->PrintOutput(0, $category["comment"]);
  339. // print super class or protocol which the class conforms to
  340. $this->PrintOutput(1, "$category_name = objccategory(".$category["super"].")");
  341. // print methods
  342. if ($category["methods"]) {
  343. foreach ($category["methods"] as $method) {
  344. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  345. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  346. }
  347. }
  348. $this->PrintOutput(1, "end; external$new_name;");
  349. }
  350. function PrintClass ($class) {
  351. $this->PrintOutput(0, "");
  352. $this->PrintOutput(0, "{ ".$class["name"]." }");
  353. if ($class["comment"]) $this->PrintOutput(0, $class["comment"]);
  354. //print_r($class["methods"]);
  355. // print super class or protocol which the class conforms to
  356. if ($class["adopts"]) {
  357. $this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].", ".$class["adopts"].")");
  358. } elseif ($class["super"]) {
  359. $this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].")");
  360. }
  361. // print instance variables
  362. if ($class["ivars"]) {
  363. $this->PrintOutput(1, "private");
  364. foreach ($class["ivars"] as $ivar) {
  365. $this->PrintOutput(2, $ivar);
  366. }
  367. }
  368. // print alloc method for the class
  369. $this->PrintOutput(2, "");
  370. $this->PrintOutput(1, "public");
  371. $this->PrintOutput(2, "class function alloc: ".$class["name"]."; message 'alloc';");
  372. // print class-level methods
  373. if ($class["methods"]) {
  374. $this->PrintOutput(0, "");
  375. foreach ($class["methods"] as $method) {
  376. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  377. $this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  378. }
  379. }
  380. // print adopted protocol methods
  381. if (count($class["protocols"]) > 0) {
  382. $this->PrintOutput(0, "");
  383. $this->PrintOutput(2, "{ Adopted Protocols }");
  384. //print_r($this->dump["protocols"]);
  385. foreach ($class["protocols"] as $name) {
  386. if ($this->dump["protocols"][$name]) {
  387. foreach ($this->dump["protocols"][$name] as $method) {
  388. if (!$this->ClassContainsMethod($class, $method)) $this->PrintOutput(2, $method["def"]);
  389. }
  390. }
  391. }
  392. }
  393. $this->PrintOutput(1, "end; external;");
  394. }
  395. }
  396. ?>