parser.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 'webkit':
  66. $options[$key] = true;
  67. break;
  68. case 'ignore':
  69. $ignore_headers = explode(",", trim($value, "\""));
  70. break;
  71. case 'only':
  72. $only_files = explode(",", trim($value, "\""));
  73. break;
  74. case 'frameworks':
  75. $options[$key] = explode(",", trim($value, "\""));
  76. break;
  77. default:
  78. //print("unknown switch $key\n");
  79. break;
  80. }
  81. }
  82. }
  83. // ??? TESTING
  84. $testing = false;
  85. if ($testing) {
  86. $GLOBALS["argv"][] = "-webkit";
  87. $GLOBALS["argv"][] = "-root=/Developer/ObjectivePascal";
  88. $GLOBALS["argv"][] = "-delegates";
  89. //$GLOBALS["argv"][] = "-reference";
  90. //$GLOBALS["argv"][] = "-all";
  91. $GLOBALS["argv"][] = "-noprint";
  92. //$GLOBALS["argv"][] = "-show";
  93. //$GLOBALS["argv"][] = "-only=\"UIWindow.h\"";
  94. //$GLOBALS["argv"][] = "-frameworks=\"appkit,foundation\"";
  95. //$GLOBALS["argv"][] = "-framework_path=\"/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks\"";
  96. //$GLOBALS["argv"][] = "-header=\"uikit/UIView.h\"";
  97. //$GLOBALS["argv"][] = "-framework_path=\"/System/Library/Frameworks\"";
  98. //$GLOBALS["argv"][] = "-header=\"webkit/DOMDocument.h\"";
  99. //$GLOBALS["argv"][] = "-framework_path=\"/System/Library/Frameworks\"";
  100. //$GLOBALS["argv"][] = "-header=\"foundation/NSBundle.h\"";
  101. //$GLOBALS["argv"][] = "-header=\"appkit/NSBundle.h\"";
  102. $GLOBALS["argv"][] = "-ignore=\"NSGeometry.h,NSRange.h\"";
  103. $GLOBALS["argv"][] = "-objp";
  104. // Objective-P
  105. /* Notes for master compile (-all):
  106. • CocoaAll.pas:
  107. Compiling /Developer/ObjectivePascal/CocoaAll.pas
  108. 1) NSWorkspace.inc(35,46) Error: Duplicate identifier "NSWorkspaceLaunchAllowingClassicStartup"
  109. 2) NSClassDescription.inc(60,55) Error: Duplicate identifier "description"
  110. 3) NSScriptObjectSpecifiers.inc(194,44) Error: Duplicate identifier "classDescription"
  111. 4) NSScriptSuiteRegistry.inc(75,40) Error: Duplicate identifier "classDescription"
  112. 5) NSControl.inc(136,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  113. 6) NSWorkspace.inc(135,189) Error: Duplicate identifier "description"
  114. 7) NSMenuItemCell.inc(64,9) Error: Duplicate identifier "reserved"
  115. 8) NSRuleEditor.inc(127,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  116. 9) NSObjCRuntime.inc(79,24) Fatal: Syntax error, "identifier" expected but ":" found
  117. Fatal: Compilation aborted
  118. 1) NSWorkspace.h has a duplicate NSWorkspaceLaunchAllowingClassicStartup constant
  119. 2) NSObjcRuntime.h contains a bad external function:
  120. function __attribute__(: (format(__NSString__; : 1; : 2))): void NSLog(NSStringRef *format, ...); cdecl; external name '__attribute__';
  121. procedure NSLog(fmt:NSString); cdecl; varargs; external;
  122. 3) NSMenuItemCell.h has a duplicate (case sensitive name not allowed in Pascal) field that must be changed by hand.
  123. 4) These methods have problems in the params. This is a Objc convention where an absent type is always "id"
  124. - (void)performClick:sender;
  125. - (void)setDelegate:delegate;
  126. NSControl.inc(136,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  127. NSRuleEditor.inc(124,15) Error: Mismatch between number of declared parameters and number of colons in message string.
  128. 5) NSInteger types are wrong in NSObjcRuntime (should be long)
  129. NSInteger = clong;
  130. NSUInteger = culong;
  131. NSNotFound = high(NSInteger);
  132. 6) Many description and classDescription identifiers are not protected in name space and cause errors
  133. • iPhoneAll.pas
  134. 1) UIAccelerometer: FPC bug causes methods with a single character message to fail. Remove the 3 methods affected
  135. UIAccelerometer.inc(67,49) Error: Illegal expression after message directive
  136. 2) There's no way to know that UITextInputTraits is actually UITextInputTraitsProtocol due to name changes for Pascal syntax
  137. UITextField.inc(91,32) Error: Identifier not found "UITextInputTraits"
  138. • WebKit.pas
  139. 1) Extra duplicate type in DOMObject.inc
  140. DOMObjectInternal = Pointer;
  141. DOMObjectInternal = DOMObjectInternal;
  142. 2) DOMDocument has method with reserved keyword name "implementation"
  143. function implementation: DOMImplementation; message 'implementation';
  144. 3) DOMEvent has method with reserved keyword name "type"
  145. function type: NSString; message 'type';
  146. * reserved keywords are not protected in method names. This is messing up WebKit parsing badly
  147. - General notes:
  148. 1) NSObject.h is parsed for the merged category methods that should be inserted manually into the real root class
  149. 2) NSRange.h was ignored because it contains custom code and can be maintained by hand very easily
  150. 3) NSGeometry.h was ignored because it contains many parsing errors and custom code, do this by hand for now.
  151. 4) All instance variables are placed into "private" for now. There are a very small number of classes that have public ivar's.
  152. */
  153. //$GLOBALS["argv"][] = "-show";
  154. }
  155. if (count($GLOBALS["argv"]) == 1) {
  156. print("Cocoa Framework Parser ($version) usage:\n");
  157. print("php parser.php [switches]\n\n");
  158. print("switches:\n\n");
  159. print(" -all print all headers (.h) from AppKit/Foundation frameworks\n");
  160. print(" -header=\"foundation/NSObject.h\" prints a single header from system frameworks\n");
  161. print(" -root sets the root path of the pascocoa directory\n");
  162. print(" -framework_path sets the root path of the frameworks directory (defaults to /System/Library/Frameworks)\n");
  163. print(" -show prints output to screen instead of file\n");
  164. print(" -ignore=\"NSObject.h,NSArray.h\" ignores the list of headers during parsing (-all only, no spaces)\n");
  165. print(" -only=\"NSObject.h,NSArray.h\" only prints these files (-all only, no spaces)\n");
  166. print(" -noprint parses but does not print (-all only)\n");
  167. print(" -encodings prints Pascal type encoding glue for GenerateTypeEncodings.p (-all only)\n");
  168. print(" -delegates prints NSDelegateController.inc to foundation (-all only)\n");
  169. print(" -objp prints classes in FPC Objective-P dialect\n");
  170. print(" -iphone one-time parse for iPhone headers\n");
  171. print(" -cocoa one-time parse for Cocoa (AppKit/Foundation) headers\n");
  172. print(" -frameworks=\"appkit,foundation\" list of supported frameworks to parse\n");
  173. print("\n\n");
  174. }
  175. // get the command line options
  176. if (count($GLOBALS["argv"]) > 1) {
  177. HandleCommandLineOptions($GLOBALS["argv"]);
  178. //print_r($options);
  179. }
  180. // Make the output directory
  181. if ($options["out"]) {
  182. @mkdir($root_path, 0777);
  183. @mkdir($root_path."/foundation", 0777);
  184. @mkdir($root_path."/appkit", 0777);
  185. // @mkdir($root_path."/webkit", 0777);
  186. @mkdir($root_path."/uikit", 0777);
  187. // @mkdir($root_path."/reference", 0777);
  188. }
  189. // setup -iphone options
  190. if ($options["iphone"]) {
  191. $options["all"] = true;
  192. $options["objp"] = true;
  193. $options["frameworks"] = array("uikit");
  194. }
  195. // setup -cocoa options
  196. if ($options["cocoa"]) {
  197. $options["all"] = true;
  198. $options["objp"] = true;
  199. $options["frameworks"] = array("appkit","foundation");
  200. $ignore_headers = array("NSGeometry.h","NSRange.h");
  201. }
  202. if ($options["webkit"]) {
  203. $options["all"] = true;
  204. $options["objp"] = true;
  205. $options["frameworks"] = array("webkit");
  206. }
  207. // create the parser instance
  208. if ($options["objp"]) {
  209. $parser = new TObjPParser ($root_path, "", $options["frameworks"], $options["show"]);
  210. } else {
  211. $parser = new TPasCocoaParser ($root_path, "", $options["frameworks"], $options["show"]);
  212. }
  213. // Process single headers
  214. if ($options["header"] && !$options["all"]) {
  215. $path = $options["framework_path"]."/".$options["header"]["framework"].".framework/Headers/".$options["header"]["name"];
  216. print("* Processing $path...\n");
  217. $parser->ProcessFile($path, true);
  218. }
  219. //$parser->PrintIvarSizeComparison("/Users/ryanjoseph/Desktop/objp/IvarSize.p");
  220. //exit;
  221. // Process all headers
  222. if ($options["all"]) {
  223. $parser->ParseCocoaFrameworks($ignore_headers, null);
  224. if (!$options["noprint"]) $parser->PrintAllHeaders("", $duplicate_headers, $only_files, $options["reference"]);
  225. if ($options["delegates"]) $parser->ParseDelegateClasses();
  226. if ($options["encodings"]) $parser->PrintTypeEncodingGlue();
  227. }
  228. ?>