parser.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. $version = "FrameworkParser: 1.3. PasCocoa 0.3, Objective-P 0.4";
  3. require("pascocoa_parser.php");
  4. require("objp_parser.php");
  5. /**
  6. * Cocoa framework parser for PasCocoa
  7. *
  8. * @author Ryan Joseph
  9. **/
  10. // These files have duplicates in AppKit and Foundation so we ignore the foundation versions and everything is merged into AppKit
  11. $duplicate_headers = array("foundation/NSAttributedString.inc", "foundation/NSAffineTransform.inc");
  12. // Print only these files
  13. $only_files = null;
  14. $options = array();
  15. function HandleCommandLineOptions ($argv) {
  16. global $options;
  17. global $root_path;
  18. global $ignore_headers;
  19. global $only_files;
  20. // defaults
  21. $options["framework_path"] = "/System/Library/Frameworks";
  22. foreach ($argv as $option) {
  23. $pair = explode("=", $option);
  24. $key = trim($pair[0], "-");
  25. $value = $pair[1];
  26. switch ($key) {
  27. case 'root':
  28. $root_path = trim($value, "\"");
  29. break;
  30. case 'header':
  31. $where = explode("/", trim($value, "\""));
  32. $options[$key]["framework"] = ucfirst($where[0]);
  33. $options[$key]["name"] = $where[1];
  34. break;
  35. case 'framework_path':
  36. $options["framework_path"] = trim($value, "\"");
  37. break;
  38. case 'all':
  39. $options[$key] = true;
  40. break;
  41. case 'objp':
  42. $options[$key] = true;
  43. break;
  44. case 'encodings':
  45. $options[$key] = true;
  46. break;
  47. case 'delegates':
  48. $options[$key] = true;
  49. break;
  50. case 'noprint':
  51. $options[$key] = true;
  52. break;
  53. case 'show':
  54. $options[$key] = true;
  55. break;
  56. case 'reference':
  57. $options[$key] = true;
  58. break;
  59. case 'iphone':
  60. $options[$key] = true;
  61. break;
  62. case 'cocoa':
  63. $options[$key] = true;
  64. break;
  65. case 'ignore':
  66. $ignore_headers = explode(",", trim($value, "\""));
  67. break;
  68. case 'only':
  69. $only_files = explode(",", trim($value, "\""));
  70. break;
  71. case 'frameworks':
  72. $options[$key] = explode(",", trim($value, "\""));
  73. break;
  74. default:
  75. //print("unknown switch $key\n");
  76. break;
  77. }
  78. }
  79. }
  80. // ??? TESTING
  81. $testing = false;
  82. if ($testing) {
  83. $GLOBALS["argv"][] = "-iphone";
  84. $GLOBALS["argv"][] = "-root=/Developer/ObjectivePascal";
  85. //$GLOBALS["argv"][] = "-delegates";
  86. //$GLOBALS["argv"][] = "-reference";
  87. //$GLOBALS["argv"][] = "-all";
  88. //$GLOBALS["argv"][] = "-noprint";
  89. //$GLOBALS["argv"][] = "-show";
  90. //$GLOBALS["argv"][] = "-only=\"UIWindow.h\"";
  91. $GLOBALS["argv"][] = "-frameworks=\"appkit,foundation\"";
  92. $GLOBALS["argv"][] = "-framework_path=\"/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks\"";
  93. $GLOBALS["argv"][] = "-header=\"uikit/UIView.h\"";
  94. //$GLOBALS["argv"][] = "-framework_path=\"/System/Library/Frameworks\"";
  95. //$GLOBALS["argv"][] = "-header=\"foundation/NSXMLNodeOptions.h\"";
  96. //$GLOBALS["argv"][] = "-header=\"appkit/NSBrowser.h\"";
  97. $GLOBALS["argv"][] = "-ignore=\"NSGeometry.h,NSRange.h\"";
  98. $GLOBALS["argv"][] = "-objp";
  99. // Objective-P
  100. /* Notes for master compile (-all):
  101. • CocoaAll.pas:
  102. 1) NSWorkspace.h has a duplicate NSWorkspaceLaunchAllowingClassicStartup constant
  103. 2) NSObjcRuntime.h contains a bad external function:
  104. function __attribute__(: (format(__NSString__; : 1; : 2))): void NSLog(NSStringRef *format, ...); cdecl; external name '__attribute__';
  105. procedure NSLog(fmt:NSString); cdecl; varargs; external;
  106. 3) NSMenuItemCell.h has a duplicate (case sensitive name not allowed in Pascal) field that must be changed by hand.
  107. - Extra hand parsing. These units parse but contain unions which need to be fixed by hand.
  108. • NSEvent.h
  109. • NSIndexSet.h:
  110. _internal: record
  111.  case byte of
  112.    0: (_singleRange:
  113.          record
  114.            _range: NSRange;
  115.          end;
  116.       );
  117.    1: (_multipleRanges:
  118.          record
  119.            _data: pointer;
  120.            _reserved: pointer;
  121.          end;
  122.       );
  123. end;
  124. 4) These methods have problems in the params. This is a Objc convention where an absent type is always "id"
  125. - (void)performClick:sender;
  126. - (void)setDelegate:delegate;
  127. NSControl.inc(136,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  128. NSRuleEditor.inc(124,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  129. 5) NSInteger types are wrong in NSObjcRuntime (should be long)
  130. NSInteger = clong;
  131. NSUInteger = culong;
  132. NSNotFound = high(NSInteger);
  133. 6) Many description and classDescription identifiers are not protected in name space and cause errors
  134. • iPhoneAll.pas
  135. 1) UIAccelerometer: FPC bug causes methods with a single character message to fail. Remove the 3 methods affected
  136. UIAccelerometer.inc(67,49) Error: Illegal expression after message directive
  137. 2) There's no way to know that UITextInputTraits is actually UITextInputTraitsProtocol due to name changes for Pascal syntax
  138. UITextField.inc(91,32) Error: Identifier not found "UITextInputTraits"
  139. - General notes:
  140. 1) NSObject.h is parsed for the merged category methods that should be inserted manually into the real root class
  141. 2) NSRange.h was ignored because it contains custom code and can be maintained by hand very easily
  142. 3) NSGeometry.h was ignored because it contains many parsing errors and custom code, do this by hand for now.
  143. 4) All instance variables are placed into "private" for now. There are a very small number of classes that have public ivar's.
  144. */
  145. //$GLOBALS["argv"][] = "-show";
  146. }
  147. if (count($GLOBALS["argv"]) == 1) {
  148. print("Cocoa Framework Parser ($version) usage:\n");
  149. print("php parser.php [switches]\n\n");
  150. print("switches:\n\n");
  151. print(" -all print all headers (.h) from AppKit/Foundation frameworks\n");
  152. print(" -header=\"foundation/NSObject.h\" prints a single header from system frameworks\n");
  153. print(" -root sets the root path of the pascocoa directory\n");
  154. print(" -framework_path sets the root path of the frameworks directory (defaults to /System/Library/Frameworks)\n");
  155. print(" -show prints output to screen instead of file\n");
  156. print(" -ignore=\"NSObject.h,NSArray.h\" ignores the list of headers during parsing (-all only, no spaces)\n");
  157. print(" -only=\"NSObject.h,NSArray.h\" only prints these files (-all only, no spaces)\n");
  158. print(" -noprint parses but does not print (-all only)\n");
  159. print(" -encodings prints Pascal type encoding glue for GenerateTypeEncodings.p (-all only)\n");
  160. print(" -delegates prints NSDelegateController.inc to foundation (-all only)\n");
  161. print(" -objp prints classes in FPC Objective-P dialect\n");
  162. print(" -iphone one-time parse for iPhone headers\n");
  163. print(" -cocoa one-time parse for Cocoa (AppKit/Foundation) headers\n");
  164. print(" -frameworks=\"appkit,foundation\" list of supported frameworks to parse\n");
  165. print("\n\n");
  166. }
  167. // get the command line options
  168. if (count($GLOBALS["argv"]) > 1) {
  169. HandleCommandLineOptions($GLOBALS["argv"]);
  170. //print_r($options);
  171. }
  172. // Make the output directory
  173. if ($options["out"]) {
  174. @mkdir($root_path, 0777);
  175. @mkdir($root_path."/foundation", 0777);
  176. @mkdir($root_path."/appkit", 0777);
  177. // @mkdir($root_path."/webkit", 0777);
  178. @mkdir($root_path."/uikit", 0777);
  179. // @mkdir($root_path."/reference", 0777);
  180. }
  181. // setup -iphone options
  182. if ($options["iphone"]) {
  183. if (!$root_path) $root_path .= "/units/i386-darwin/cocoaint/src";
  184. $options["all"] = true;
  185. $options["objp"] = true;
  186. $options["frameworks"] = array("uikit");
  187. }
  188. // setup -cocoa options
  189. if ($options["cocoa"]) {
  190. $options["all"] = true;
  191. $options["objp"] = true;
  192. $options["frameworks"] = array("appkit","foundation");
  193. $ignore_headers = array("NSGeometry.h","NSRange.h");
  194. }
  195. // create the parser instance
  196. if ($options["objp"]) {
  197. $parser = new TObjPParser ($root_path, "", $options["frameworks"], $options["show"]);
  198. } else {
  199. $parser = new TPasCocoaParser ($root_path, "", $options["frameworks"], $options["show"]);
  200. }
  201. // Process single headers
  202. if ($options["header"] && !$options["all"]) {
  203. $path = $options["framework_path"]."/".$options["header"]["framework"].".framework/Headers/".$options["header"]["name"];
  204. print("* Processing $path...\n");
  205. $parser->ProcessFile($path, true);
  206. }
  207. //$parser->PrintIvarSizeComparison("/Users/ryanjoseph/Desktop/objp/IvarSize.p");
  208. //exit;
  209. // Process all headers
  210. if ($options["all"]) {
  211. $parser->ParseCocoaFrameworks($ignore_headers, null);
  212. if (!$options["noprint"]) $parser->PrintAllHeaders("", $duplicate_headers, $only_files, $options["reference"]);
  213. if ($options["delegates"]) $parser->ParseDelegateClasses();
  214. if ($options["encodings"]) $parser->PrintTypeEncodingGlue();
  215. }
  216. ?>