pascocoa_parser.php 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443
  1. <?php
  2. function ReadTextFile ($path) {
  3. if ($handle = fopen($path, "r")) {
  4. return fread($handle, 400 * 1024);
  5. fclose($handle);
  6. }
  7. }
  8. function str_replace_word ($needle, $replacement, $haystack) {
  9. $pattern = "/\b$needle\b/";
  10. $haystack = preg_replace($pattern, $replacement, $haystack);
  11. return $haystack;
  12. }
  13. function istr_replace_word ($needle, $replacement, $haystack) {
  14. $pattern = "/\b$needle\b/i";
  15. $haystack = preg_replace($pattern, $replacement, $haystack);
  16. return $haystack;
  17. }
  18. function strtolowerref(&$str) {
  19. $str = strtolower($str);
  20. }
  21. // converts "STR_XX_YY_ZZ" to "Str Xx Yy Zz"
  22. function DeprecatedMacroToDirective($str)
  23. {
  24. // AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_4 etc
  25. if (preg_match("!DEPRECATED_(IN_.*VERSION_.*)_AND_LATER!", $str, $matches)) {
  26. $str = strtolower($matches[1]);
  27. // cleanup
  28. $str = preg_replace("!in_mac_os_x_version_!", "in Mac OS X ", $str);
  29. $str = preg_replace("!([0-9])_([0-9])!e", '\1.\2', $str);
  30. return " deprecated '" . $str . " and later'";
  31. // NS_DEPRECATED(__MAC_10_1,__MAC_10_6,__IPHONE_NA,__IPHONE_NA),
  32. // __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_1,__MAC_10_6,__IPHONE_NA,__IPHONE_NA,
  33. // NS_DEPRECATED_IPHONE(__IPHONE_2_0,__IPHONE_2_0),
  34. // NS_DEPRECATED_MAC(__MAC_10_1, __MAC_10_5)
  35. } elseif (preg_match("!DEPRECATED(_IPHONE|_MAC)?\(([^)]+)\)!", $str, $matches)) {
  36. $str = strtolower($matches[2]);
  37. $versions = preg_replace("!__MAC_!e", "Mac OS X ", $str);
  38. $versions = preg_replace("!__IPHONE_!e", "iOS ", $versions);
  39. $versions = preg_replace("!([0-9])_([0-9])!e", '\1.\2', $versions);
  40. $deprecatedversions = explode(",", $matches[2]);
  41. $result = "";
  42. if ($matches[1] == "") {
  43. $macindex = 2;
  44. $iphoneindex = 4;
  45. } elseif ($matches[1] == "_IPHONE") {
  46. $macindex = -1;
  47. $iphoneindex = 2;
  48. } else {
  49. $macindex = 2;
  50. $iphoneindex = -1;
  51. }
  52. if (($macindex != -1) && !preg_match("!Mac OS X( Version)? NA|MAC_NA!", $deprecatedversions[$macindex])) {
  53. $result = "in " . $deprecatedversions[$macindex] . " and later";
  54. }
  55. if (($iphoneindex != -1) && !preg_match("!iOS( Version)? NA|IPHONE_NA!", $deprecatedversions[$iphoneindex])) {
  56. if ($result != "") $result .= ", ";
  57. $result .= "in " . $deprecatedversions[$iphoneindex] . " and later";
  58. }
  59. return " deprecated '" . $result . "'";
  60. }
  61. }
  62. define("CAST_HANDLE", true);
  63. define("DONT_CAST_HANDLE", false);
  64. define("ACCESS_HANDLE_DIRECT", 1);
  65. define("ACCESS_HANDLE_FUNCTION", 2);
  66. define("REGISTER_SEL", true);
  67. define("DONT_REGISTER_SEL", false);
  68. define("USE_HANDLE", true);
  69. define("DONT_USE_HANDLE", false);
  70. class TPasCocoaParser {
  71. // Frameworks to parse (loaded from frameworks.xml)
  72. var $frameworks = array();
  73. var $maximum_method_length = 111; // WE GET THIS ERROR: NSDelegatesAll.pas(6109,296) Error: Identifier not found "NSDelegateController_NSLayoutManagerDelegate_layoutManager_shouldUseTemporaryAttributes_forDrawingToScreen_atCharacterIndex_effectiveRange"
  74. var $output; // current output file handle
  75. var $root; // root for printing/locating resources
  76. var $out; // directory to print
  77. var $show; // print output to screen instead of file
  78. var $framework; // current framework being parsed
  79. var $current_class; // reference to current class structure being parsed
  80. var $current_header; // reference to current header structure being parsed
  81. var $method_count; // count of all methods parsed
  82. var $class_count; // count of all classes parsed
  83. var $warning_count; // count of parser warnings
  84. var $parser_skipping; // the parser is skipping lines in the current file
  85. var $inside_macro_block; // the parser is inside a macro block, no nesting!
  86. var $instance_var_scope; // current scope of the instance variable parser
  87. var $dump = array(); // Convert Pascal classes
  88. var $delegate_methods = array(); // Delegate methods and types from GEN_BRIDGE_METADATA XML data
  89. var $delegate_method_names = array(); // Delegate method name array
  90. var $type_encodings = array(); // Master listing of type encodings for each method in the frameworks
  91. // Comments Builder
  92. var $comment_eol;
  93. var $comment_terminated;
  94. var $comment_fragment;
  95. var $comment_fragment_previous;
  96. var $comment_fragment_open;
  97. var $comment_previous;
  98. var $comment_block_open;
  99. var $comment_block_closed;
  100. var $comment_header;
  101. // Macros Builder
  102. var $macro_block = "";
  103. var $in_macro_block = false;
  104. /**
  105. * PARSER OPTIONS
  106. */
  107. var $objc_id = "id"; // Default type for generic objects
  108. var $sel_string = "SEL"; // The selector string type which registers selectors internally
  109. var $protocol_suffix = "Protocol"; // All protocols append this suffix
  110. var $pointer_type_suffx = "Ref"; // NS*** pointers in parameter lists are suffixed with this
  111. var $class_pointer_suffix = "Pointer"; // Pointers to NS*** classes are suffxed with this
  112. var $register_selectors = true; // Register selectors automatically inside the wrappers
  113. var $show_added_messages = false; // show messages when methods are added to a class
  114. var $show_class_hierarchy = false;
  115. var $objects_are_wrappers = false; // Treat all objects (id) like wrappers. i.e aObject.Handle;
  116. var $replace_hinted_params = false; // replace comment hints with hinted type - (void * /* CMProfileRef */)colorSyncProfile;
  117. var $trailing_underscore = false; // Append the trailing underscore for last parameter
  118. var $record_keyword = "record"; // The keyword used for printing records
  119. var $bitpacked_record_keyword = "bitpacked record"; // The keyword used for printing bitpacked records
  120. var $string_macro = "NSString";
  121. var $category_suffix = "Category"; // To prevent naming conlicts all objccategories are suffixed with this work
  122. var $parse_comments = false; // Comments are parsed and inserted by context
  123. var $merge_headers = false; // Headers are printed by merging difference instead of overwritting
  124. var $comment_break = "\n"; // This text will be inserted before each comment to break lines
  125. var $comment_padding_left = " "; // Padding applied to the left side of all comments
  126. var $comment_padding_right = " "; // Padding applied to the right side of all comments
  127. var $varargs_param_name = "firstKey"; // The name of the first parameter for methods using varargs
  128. // array of all known framework classes (both regular and anonymous)
  129. var $cocoa_classes = array("Protocol");
  130. // array of all anonymous external classes. This list will include
  131. // classes that are first defined anonymously and then normally
  132. var $anon_cocoa_classes = array();
  133. // array of all defined external classes. This list will only
  134. // include classes that are completely defined
  135. var $defined_cocoa_classes = array();
  136. // array of function pointer types that are declared without an indirection
  137. // (e.g. NSUncaughtExceptionHandler in NSException.h) -> do not add Ptr
  138. // suffix when deriving the pointer type
  139. var $implicit_function_pointer_types = array();
  140. // array of opaque struct pointer types
  141. var $opaque_structs = array();
  142. // array of all known framework categories
  143. var $cocoa_categories = array();
  144. // Pascal keywords to protect
  145. var $reserved_keywords = array( "const", "object", "string", "array", "var", "set", "interface", "unit", "begin", "end",
  146. "type", "raise", "property", "to", "for", "with", "function", "procedure", "result",
  147. "pointer", "create", "new", "dispose", "label", "packed", "record", "class", "implementation",
  148. );
  149. // FPC methods that can't be overloaded
  150. var $reserved_methods = array("");
  151. // Types which can not be altered by reserved keywords
  152. var $reserved_types = array("Pointer");
  153. // Objective-c types to convert
  154. var $replace_types = array("BOOL"=>"Boolean", "long"=>"clong", "int"=>"cint",
  155. "unsigned long"=>"culong", "unsigned short"=>"cushort", "void *"=>"Pointer", "unsigned int"=>"cuint",
  156. "Class"=>"Pobjc_class", "uint"=>"cuint",
  157. "uint8_t"=>"cuint8", "signed int"=>"cint", "const char"=>"char", "const void *"=>"Pointer",
  158. "const uint8_t"=>"cuint8", "unsigned"=>"cuint", "int32_t"=>"cint32", "float"=>"single",
  159. "unsigned long long"=>"culonglong", "int64_t"=>"cint64", "uint32_t"=>"cuint32", "uint16_t"=>"cuint16",
  160. "unsigned char"=>"char", "short"=>"cshort", "double"=>"double", "long long"=>"clonglong",
  161. "uintptr_t"=>"culong","intptr_t"=>"clong",
  162. "signed char"=>"char", "uint64_t"=>"cuint64",
  163. // work-arounds - the type replacement needs regex to handle with spaces I guess
  164. "void*"=>"Pointer",
  165. // macros
  166. "IBAction"=>"void", "IBOutlet"=>"",
  167. // special pointers
  168. "const id *"=>"NSObjectArrayOfObjectsPtr", "Protocol *"=>"objc_protocol", "NSObject *"=>"NSObject",
  169. "const char *"=>"PChar", "const void *"=>"Pointer", "unsigned char *"=>"PByte", "char *"=>"PChar",
  170. "unsigned *"=>"pcuint", "unichar *"=>"PChar", "const unichar *"=>"PChar",
  171. );
  172. // These "types" are hints to the Objective-C garbage collector
  173. var $garbage_collector_hints = array("__strong", "__weak", "volatile", "___strong", "___weak");
  174. // These identifiers are used with remote messaging
  175. var $remote_messaging_modifiers = array("oneway", "in", "out", "inout", "bycopy", "byref");
  176. var $null_macros = array("IBOutlet", "IBAction");
  177. // External NSString macros. Additional values are imported from frameworks.xml
  178. var $external_string_macros = "APPKIT_EXTERN|FOUNDATION_EXPORT|EXTERN|extern";
  179. // Types which have known pointers declared in the headers
  180. var $pointer_types = array( // MacOSAll types
  181. // C/Cocoa types
  182. "void"=>"Pointer","const void"=>"Pointer",
  183. "Boolean"=>"pboolean",
  184. "clong"=>"pclong","cint"=>"pcint",
  185. "culong"=>"pculong","cushort"=>"pcushort","cuint"=>"pcuint",
  186. "cuint8"=>"pbyte","char"=>"PChar",
  187. "clonglong"=>"pclonglong","culonglong"=>"pculonglong",
  188. "cint64"=>"pcint64",
  189. "cuint32"=>"pcuint32","cuint16"=>"pcuint16",
  190. "cshort"=>"pcshort",
  191. "single"=>"psingle", "double"=>"pdouble",
  192. );
  193. // Types to ignore
  194. var $ignore_types = array("CGFloat");
  195. // Comments (or fragments starting with the pattern) to ignore (regex)
  196. var $ignore_comments = array();
  197. // Categories to ignore
  198. // NSURLLoading is deprecated in 10.4 and later, and causes problems
  199. // when parsing the iPhoneOS foundation
  200. var $ignore_categories = array("NSURLLoading");
  201. // Methods to ignore
  202. var $ignore_methods = array( "retain", "release", "retainCount", "copyWithZone", "mutableCopyWithZone",
  203. "allocWithZone", "alloc", "copy", "mutableCopy", "self_", "autorelease", "awakeFromNib",
  204. "observationInfo",
  205. );
  206. // Lines to ignore
  207. var $ignore_lines = array();
  208. // default protected keywords by class/category
  209. // these may be useful if super classes were not parsed before
  210. var $default_protected = array( "*"=>array("description", "classDescription", "zone"),
  211. // "NSDeprecated"=>array("accessoryView"),
  212. "NSToolbarSupport"=>array("toolbar"),
  213. "DOMNode"=>array("version"),
  214. "WebView"=>array("frame"),
  215. "DOMImplementation"=>array("version"),
  216. "NSTableView"=>array("cell","toolTip","menu"),
  217. "NSMovie"=>array("QTMovie"),
  218. "UIAcceleration"=>array("timestamp","x","y","z"),
  219. );
  220. var $skip_blocks = array( //"^#if __LP64__.*"=>"^#(else|endif)+",
  221. "^#if __BLOCKS__"=>"^#(else|endif)+",
  222. "^#if NS_BLOCKS_AVAILABLE"=>"^#(else|endif)+",
  223. "^#ifndef CGFLOAT_DEFINED"=>"^#(else|endif)+",
  224. );
  225. var $macro_blocks = array( "^#if \(__LP64__\)"=>"\$ifdef cpu64",
  226. "^#if __LP64__.*"=>"\$ifdef cpu64",
  227. "^#if !__LP64__.*"=>"\$ifndef cpu64",
  228. "^#if defined \(__LP64__\)"=>"\$ifdef cpu64",
  229. "^#if ![[:space:]]*\(__LP64__\)"=>"\$ifndef cpu64",
  230. "^#ifdef __BIG_ENDIAN__"=>"\$ifdef fpc_big_endian",
  231. // not really clean (assumes all arm devices are embedded)
  232. "^#if __LP64__ \|\| TARGET_OS_EMBEDDED \|\| TARGET_OS_IPHONE \|\| TARGET_OS_WIN32 \|\| NS_BUILD_32_LIKE_64"=>"\$if defined(cpu64) or defined(cpuarm) or defined(win32)",
  233. // replacement must be updated once we support AppleTV (presumably defines TARGET_OS_EMBEDDED but not TARGET_OS_IPHONE)
  234. "^#if __LP64__ \|\| (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) \|\| TARGET_OS_WIN32 \|\| NS_BUILD_32_LIKE_64"=>"\$if defined(cpu64) or defined(win32)"
  235. //"^#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_[0-9]+_[0-9]+"=>"*",
  236. );
  237. // These macros are used to suggest which version of a framework the symbol is available in but removed to assist the parser
  238. var $version_macros = array( "[[:space:]]*DEPRECATED_IN_.*_VERSION_[0-9]+_[0-9]+_AND_LATER[[:space:]]*",
  239. "[[:space:]]*AVAILABLE_.*_VERSION_[0-9]+_[0-9]+_AND_LATER_BUT_DEPRECATED_IN_.*_VERSION_[0-9]+_[0-9]+[[:space:]]*",
  240. "[[:space:]]*AVAILABLE_.*_VERSION_[0-9]+_[0-9]+_AND_LATER[[:space:]]*",
  241. "[[:space:]]*AVAILABLE_.*_VERSION_[0-9]+_[0-9]+[[:space:]]*",
  242. "[[:space:]]*.*_VERSION_MAX_ALLOWED[[:space:]]*",
  243. "[[:space:]]__OSX_AVAILABLE.*\([^)]+\)",
  244. );
  245. /**
  246. * COMMON REGULAR EXPRESIONS
  247. */
  248. var $regex_objc_method_params = "^(-|\+)[[:space:]]*\(([^)]*)\)[[:space:]]*([a-zA-Z0-9]+):(.*);";
  249. var $regex_objc_method_no_params = "^(-|\+)[[:space:]]*\(([^)]*)\)[[:space:]]*([a-zA-Z0-9]+);";
  250. var $regex_objc_method_partial = "^(-|\+)[[:space:]]*\(([^)]*)\)[[:space:]]*([a-zA-Z0-9]+):(.*)";
  251. var $regex_objc_method_terminate = "(.*);[[:space:]]*$";
  252. var $regex_objc_category = "^@interface ([a-zA-Z]+)[[:space:]]*\(([a-zA-Z]+)\)";
  253. var $regex_objc_class = "^@interface ([a-zA-Z]+)[[:space:]]*:[[:space:]]*([a-zA-Z]+)[[:space:]]*(<(.*)>)*";
  254. var $regex_objc_class_no_super = "^@interface ([a-zA-Z]+)[[:space:]]*<(.*)>[[:space:]]*";
  255. var $regex_objc_protocol = "^@protocol ([a-zA-Z]+)";
  256. var $regex_procedure_type = "^[[:space:]]*(void|IBAction)+[[:space:]]*$";
  257. var $regex_scope_compiler_directive = "^\s*@(private|protected|public)(;*)+\s*$";
  258. var $regex_objc_property_attributes = "^@property[[:space:]]*\(([^)]*)\)[[:space:]]*([a-zA-Z_0-9]+)[[:space:]]*(\*)*[[:space:]]*(.*);";
  259. var $regex_objc_property = "^@property[[:space:]]*([a-zA-Z_0-9]+)[[:space:]]*(\*)*[[:space:]]*(.*);";
  260. var $regex_objc_anon_class = "^@class[[:space:]]*(.*);";
  261. // $1 = return type
  262. // $2 = pointer modifiers
  263. // $3 = inline para name if any
  264. // $4 = parameteres
  265. var $pregex_function_pointer = "!^\s*([^*]+)\s*([*]*)\s*\(\s*[*]\s*(\w+)?\s*\)\s*\((.*)\)\s*;\s*$!";
  266. // same as above except no ";" at the end
  267. var $pregex_function_pointer_c_paratype = "!^\s*([^*]+)\s*([*]*)\s*\(\s*[*]\s*(\w+)?\s*\)\s*\((.*)\)\s*$!";
  268. // Obj-C: additional brackets and para name
  269. // $1 = return type
  270. // $2 = pointer modifiers for return type
  271. // $3 = inline type name if any
  272. // $4 = parameters
  273. // $5 = para name
  274. // last "word" is next part of method name before next colon
  275. var $pregex_function_pointer_objc_paratype = "!^\s*\(([^*]+)\s*([*]*)\s*\(\s*[*]\s*(\w+)?\s*\)\s*\((.*)\)\s*\)\s*(\w+)\s*\w+\s*$!";
  276. // the (*) for the function pointer is "optional" in the sense that you can
  277. // also declare a (useless?) function type (i.e., without the pointer) and
  278. // then afterwards declare variables to be pointers to this type
  279. var $pregex_function_pointer_typedef = "!^\s*typedef\s*([^*]+)\s*([*]*)\b\s*(?:\(\s*([*])\s*(\w+)?\s*\)|(\w+))\s*\((.*)\)\s*(\w+)?\s*;\s*$!";
  280. // semi-colon optional because sometimes on next line with availability
  281. // macro
  282. // $1 = return type
  283. // $2 = pointer modifiers for return type
  284. // $3 = function name
  285. // $4 = parameters
  286. var $pregex_external_function_end = "\s+([^*]+)\s*([*]*)\b\s*(\w+)\s*\((.*)\)\s*;?\s*$!";
  287. var $pregex_deprecated_macro = "!DEPRECATED!";
  288. /**
  289. * UTILITIES
  290. */
  291. // Skips blocks in the current file being parsed
  292. function SkipBlock ($line) {
  293. if ($line != "") {
  294. foreach ($this->skip_blocks as $key => $value) {
  295. if (@ereg($key, $line)) {
  296. $this->parser_skipping = true;
  297. }
  298. if (@ereg($value, $line)) {
  299. $this->parser_skipping = false;
  300. }
  301. }
  302. }
  303. return $this->parser_skipping;
  304. }
  305. function IsKeywordReserved($keyword) {
  306. $keyword = strtolower($keyword);
  307. if (in_array($keyword, $this->reserved_keywords)) return true;
  308. if (($this->current_class != null) &&
  309. ($this->dump["master"][$this->current_class["name"]] ["field_names"] != null) &&
  310. in_array($keyword, $this->dump["master"][$this->current_class["name"]] ["field_names"])) return true;
  311. }
  312. // Replace type with pointer equivalent
  313. function ReplacePointerType ($type) {
  314. $found = false;
  315. // all classes are implicit pointer types
  316. if (in_array($type, $this->cocoa_classes)) return $type;
  317. // function pointer types are sometimes also implicit
  318. // pointers
  319. if (in_array($type, $this->implicit_function_pointer_types)) return $type;
  320. // PPointer = ^Pointer
  321. if ($type == "Pointer") return "PPointer";
  322. if ($type == "PChar") return "PPChar";
  323. // use preferred pointer type
  324. foreach ($this->pointer_types as $objc_type => $replace_type) {
  325. if ($objc_type == $type) {
  326. $found = true;
  327. $type = $replace_type;
  328. break;
  329. }
  330. }
  331. //$type = "Pointer {".$type."}";
  332. // use generic pointer type
  333. if (!$found)
  334. $type = $type."Ptr";
  335. return $type;
  336. }
  337. // Makes a struct field into an inline array (or returns field un-changed)
  338. function MakeFieldInlineArray ($io_field, $line, $name, $type) {
  339. if (eregi("\[([0-9]+)\];", $line, $array_size)) {
  340. $length = (int)$array_size[1] - 1;
  341. if ($length > 0) {
  342. $io_field = " $name: array[0..$length] of $type;";
  343. }
  344. }
  345. return $io_field;
  346. }
  347. // Makes a type bitpacked (or returns type un-changed)
  348. function MakeFieldBitPacked ($ioType, $field, &$bitpacked) {
  349. $bitpacked = false;
  350. if (preg_match("!:([0-9]+)\s*(?:__attribute__\(\([^)]*\)\))?\s*;$!", $field, $bitpack)) {
  351. $length = (int)$bitpack[1];
  352. if ($length > 1) {
  353. $ioType = "0..((1 shl $length)-1)";
  354. } else {
  355. $ioType = "0..$length";
  356. }
  357. $bitpacked = true;
  358. }
  359. return $ioType;
  360. }
  361. // Replace objc type with preferred type
  362. function ReplaceObjcType ($type) {
  363. foreach ($this->replace_types as $objc_type => $replace_type) {
  364. if ($objc_type == $type) {
  365. $type = $replace_type;
  366. break;
  367. }
  368. }
  369. return $type;
  370. }
  371. // Replace garbage collector hints
  372. function ReplaceGarbageCollectorHints ($string, &$io_hint) {
  373. $io_hint = false;
  374. foreach ($this->garbage_collector_hints as $hint) {
  375. $out_string = str_ireplace($hint, "", $string);
  376. if ($out_string != $string) {
  377. $io_hint = $hint;
  378. $string = $out_string;
  379. }
  380. }
  381. return $string;
  382. }
  383. // Replace remote message passing modifiers
  384. function ReplaceRemoteMessagingModifiers ($string, &$io_modifier) {
  385. $io_hint = false;
  386. foreach ($this->remote_messaging_modifiers as $modifier) {
  387. $out_string = preg_replace("!\b".$modifier."\b!", "", $string);
  388. if ($out_string != $string) {
  389. $io_modifier = $modifier;
  390. $string = $out_string;
  391. }
  392. }
  393. return trim($string);
  394. }
  395. // Replace type of reference parameter with pointer
  396. function ReplaceReferenceParameterType ($type) {
  397. $type = $this->ReplaceObjcType($type);
  398. return $this->ReplacePointerType($type);
  399. }
  400. // Replace a framework class with generic id and comment hint
  401. function ReplaceFrameworkClassWithID ($string) {
  402. foreach ($this->cocoa_classes as $class) {
  403. $string = istr_replace_word($class, $this->objc_id, $string);
  404. }
  405. return $string;
  406. }
  407. // Copies the name from an Objective-C method definition
  408. function CopyObjcMethodName ($method) {
  409. // cut out comments first
  410. $method = eregi_replace("(/\*.*\*/)", "", $method);
  411. $method = eregi_replace("//.*$", "", $method);
  412. $method = trim($method, " ");
  413. $params = explode(":", $method);
  414. $name = "";
  415. if (count($params) > 1) {
  416. foreach ($params as $value) {
  417. $value = trim($value, " ");
  418. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $value, $captures)) $name .= $captures[1].":";
  419. /*
  420. // paremeter with no label (or first parameter)
  421. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*([a-zA-Z0-9]+)[[:space:]]*$", $value, $captures)) {
  422. if ($name != "") {
  423. $name .= ":";
  424. } else {
  425. $name .= $captures[1].":";
  426. }
  427. continue;
  428. }
  429. // parameter with label
  430. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*[a-zA-Z0-9_]+[[:space:]]+([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1].":";
  431. */
  432. }
  433. } else {
  434. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*(;)*$", $method, $captures)) $name = $captures[1];
  435. }
  436. return $name;
  437. }
  438. // in case of a non-function pointer and non-pointer type,
  439. // split a C field expression (e.g. "long afield" or
  440. // "long int :32") into its type and field name. Necessary
  441. // to recognise name that "long int :32" means "a field
  442. // without a name whose type/alignment is "long int" and that is
  443. // bitpacked
  444. function ExtractCFieldSimpleTypeAndSingleName($param) {
  445. // print("Converting field $param\n");
  446. // remove all "const" occurrences to simplify matching
  447. $clean_param = str_replace_word("const", "", $param);
  448. $got_type = false;
  449. // assume not a pointer type
  450. if (preg_match("!^\s*(unsigned|signed)\b!", $clean_param, $signedunsigned)) {
  451. $got_type = true;
  452. $clean_param = preg_replace("!^\s*(unsigned|signed)\b!", "", $clean_param);
  453. }
  454. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", $clean_param, $inttype)) {
  455. $got_type = true;
  456. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", "", $clean_param);
  457. }
  458. if ($got_type) {
  459. // remove bitfield and array, are encoded later
  460. $result["name"] = trim(preg_replace("!(?::.*)?(?:\[[^]]*\])?!","",$clean_param));
  461. $result["type"] = trim(preg_replace("!\s+!", " ", $signedunsigned[0] . " " . $inttype[0]));
  462. } else {
  463. // remove "struct"
  464. $clean_param = str_replace_word("struct", "", $clean_param);
  465. // has to be "type fieldname"
  466. preg_match("!^\s*(\w+)\b\s*(\w+)(?:\[[0-9]*\])?\s*!", $clean_param, $matches);
  467. $result["name"] = $matches[2]; // can be empty
  468. $result["type"] = $matches[1];
  469. }
  470. // print("field conversion result: ".$result["name"].": ".$result["type"]."\n");
  471. return $result;
  472. }
  473. // adds a function callback type with name $name, and returns
  474. // the name of the callback type
  475. function AddCallback($name, $function_pointer) {
  476. if ($this->current_header) {
  477. $type = $this->current_header["name_clean"].ucwords($name);
  478. if (!@in_array($function_pointer, $this->current_header["types"]["callbacks"])) {
  479. $count = 0;
  480. while (@array_key_exists($type, $this->current_header["types"]["callbacks"])) {
  481. $count ++;
  482. $type = "$type$count";
  483. }
  484. // append the new type to the the current class
  485. $this->current_header["types"]["callbacks"][$type] = $function_pointer;
  486. } else {
  487. // Use the name of the existing callback of matching type
  488. $type = array_search($function_pointer, $this->current_header["types"]["callbacks"]);
  489. }
  490. return $type;
  491. }
  492. return "invalid_callback_type_because_no_current_header";
  493. }
  494. function ExtractCParaNameAndType($param) {
  495. // print("Converting $param\n");
  496. // remove all "const" occurrences to simplify matching
  497. $clean_param = str_replace_word("const", "", $param);
  498. // remove all spaces between multiple pointer modifiers
  499. $clean_param = trim(preg_replace("![*]\s+[*]!", "**", $clean_param));
  500. // 1) varargs
  501. if ($clean_param == "...") {
  502. $result["name"] = "";
  503. $result["pointermods"] = "";
  504. $result["type"] = "...";
  505. // 2) type is a function pointer (does not yet match arrays of function
  506. // pointers!)
  507. } elseif (preg_match($this->pregex_function_pointer_c_paratype, $clean_param, $matches)) {
  508. $result["name"] = $matches[3];
  509. // not correct; simply assume no "pointer to function pointer" types are
  510. // used
  511. $result["pointermods"] = "";
  512. $funcptrtype = $this->ParseFunctionDeclaration($matches[1], $matches[2], "", $matches[4], false, "");
  513. $result["type"] = $this->AddCallback($matches[3], $funcptrtype);
  514. // 3) any other pointer type (including arrays)
  515. } elseif (preg_match("!^([^*]*)([*]+)\s*(\w+(?:\[[0-9]*\])?)?\s*$!", $clean_param, $matches)){
  516. $result["name"] = $matches[3]; // can be empty
  517. $result["pointermods"] = $matches[2];
  518. $result["type"] = trim($matches[1]);
  519. // 4) basic C type (including arrays)
  520. } else {
  521. // definitely not a pointer type
  522. $result["pointermods"] = "";
  523. $got_type = false;
  524. if (preg_match("!^\s*(unsigned|signed)!", $clean_param, $signedunsigned)) {
  525. $got_type = true;
  526. $clean_param = preg_replace("!^\s*(unsigned|signed)!", "", $clean_param);
  527. }
  528. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", $clean_param, $inttype)) {
  529. $got_type = true;
  530. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", "", $clean_param);
  531. }
  532. if ($got_type) {
  533. $result["name"] = trim($clean_param);
  534. $result["type"] = trim($signedunsigned[0] . " " . $inttype[0]);
  535. } else {
  536. // remove "struct"
  537. $clean_param = str_replace_word("struct", "", $clean_param);
  538. // has to be "type paraname", or just "type"
  539. preg_match("!^\s*(\w+)\b\s*(\w+(?:\[[0-9]*\])?)?\s*!", $clean_param, $matches);
  540. $result["name"] = $matches[2]; // can be empty
  541. $result["type"] = $matches[1];
  542. }
  543. }
  544. // print("param conversion result: ".$result["name"].": ".$result["pointermods"].$result["type"]."\n");
  545. return $result;
  546. }
  547. // Returns the parameter modifier string for a callback parameter type
  548. function GetCallbackParameterModifiers ($type, $name) {
  549. $modifiers = "";
  550. $type = trim($type, "*");
  551. // if the name starts with * it's a pointer
  552. // don't process framework classes since they're always pointers
  553. if (ereg("^[[:space:]]*\*", $name)) {
  554. if (!in_array($type, $this->cocoa_classes)) $modifiers = "var ";
  555. }
  556. // double ** are always var
  557. if (ereg("^[[:space:]]*\*\*", $name)) {
  558. $modifiers = "var ";
  559. }
  560. return $modifiers;
  561. }
  562. // Converts a C parameter string to Pascal
  563. function ConvertCParamsPascal ($string) {
  564. // print("params: $string\n");
  565. if ((trim($string) == "void") || (trim($string) == "")) return "";
  566. $params = explode(",", $string);
  567. $count = 0;
  568. $param_string = "";
  569. foreach ($params as $param) {
  570. $name_type = $this->ExtractCParaNameAndType($param);
  571. $type = $name_type["type"];
  572. $name = $name_type["name"];
  573. $pointertype = $name_type["pointermods"];
  574. // create name if none is specified
  575. if ($name == "") {
  576. $count ++;
  577. $name = "param$count";
  578. }
  579. // remove const keywords
  580. $type = str_replace_word("const", "", $type);
  581. $type = $this->ReplaceObjcType($type);
  582. // Remove array brackets (NSUInteger[])p
  583. if (eregi("\[[0-9]*\]", $name)) {
  584. $orgtype = $this->EncodePointerModifiers($type,$pointertype);
  585. $pointertype .= "*";
  586. $type = $this->EncodePointerModifiers($type,$pointertype)." {array of $orgtype}";
  587. $name = eregi_replace("\[[0-9]*\]", "", $name);
  588. } else {
  589. $type = $this->EncodePointerModifiers($type,$pointertype);
  590. }
  591. $modifier = "";
  592. if ($this->IsKeywordReserved($name)) $name .= "_";
  593. // multiple parameters
  594. if ($type == "...") {
  595. $param_string .= "varargs: array of const";
  596. break;
  597. }
  598. $param_string .= "$modifier$name: $type; ";
  599. }
  600. $param_string = trim($param_string, "; ");
  601. //print("$param_string\n");
  602. return $param_string;
  603. }
  604. // Remove OS X versions macros from a line
  605. // NOTE: These should be re-inlcuded in Pascal
  606. function RemoveVersionMacros ($line, &$deprecatedmods) {
  607. if (preg_match($this->pregex_deprecated_macro,$line)) {
  608. $deprecatedmods = DeprecatedMacroToDirective($line);
  609. } else {
  610. $deprecatedmods = "";
  611. }
  612. foreach ($this->version_macros as $macro) {
  613. $line = eregi_replace($macro, "", $line);
  614. }
  615. return $line;
  616. }
  617. // Insert macro blocks to replace c-style blocks
  618. function InsertMacroBlocks ($line, &$in_macro_block) {
  619. // only insert if we are in a block already.
  620. // NOTE: this does not handle nesting!
  621. if ($in_macro_block) {
  622. // macro else statment
  623. if (eregi("#else", $line)) {
  624. return "{\$else}";
  625. }
  626. // macro endif statment
  627. if (eregi("#endif", $line)) {
  628. $in_macro_block = false;
  629. return "{\$endif}";
  630. }
  631. }
  632. foreach ($this->macro_blocks as $key => $value) {
  633. if (eregi($key, $line, $captures)) {
  634. $in_macro_block = true;
  635. // replace the c-macro with a Pascal version
  636. if ($value == "*") {
  637. $captures[0] = trim($captures[0], "#");
  638. return "{\$".$captures[0]."}";
  639. } else {
  640. return "{".$value."}";
  641. }
  642. }
  643. }
  644. }
  645. // Appends the current macro to a recipient and resets it's reference
  646. function AppendCurrentMacro (&$recipient) {
  647. if ($this->in_macro_block) {
  648. $this->in_macro_block = false;
  649. $recipient[] = $this->macro_block;
  650. //if (is_array($recipient)) $recipient[] = $this->macro_block;
  651. //if (is_string($recipient)) $recipient .= $this->macro_block;
  652. }
  653. }
  654. // Returns the current macro and resets it's reference
  655. function InsertCurrentMacro () {
  656. if ($this->in_macro_block) {
  657. $this->in_macro_block = false;
  658. return $this->macro_block;
  659. } else {
  660. return null;
  661. }
  662. }
  663. // Build the current macro block
  664. function BuildMacroBlocks ($line) {
  665. // only insert if we are in a block already.
  666. // NOTE: this does not handle nesting!
  667. if ($this->in_macro_block) {
  668. // macro else statment
  669. if (eregi("#else", $line)) {
  670. $this->macro_block = "{\$else}";
  671. }
  672. // macro endif statment
  673. if (eregi("#endif", $line)) {
  674. $this->in_macro_block = false;
  675. $this->macro_block = "{\$endif}";
  676. }
  677. }
  678. foreach ($this->macro_blocks as $key => $value) {
  679. if (eregi($key, $line, $captures)) {
  680. $this->in_macro_block = true;
  681. // replace the c-macro with a Pascal version
  682. if ($value == "*") {
  683. $captures[0] = trim($captures[0], "#");
  684. $this->macro_block = "{\$".$captures[0]."}";
  685. } else {
  686. $this->macro_block = "{".$value."}";
  687. }
  688. }
  689. }
  690. }
  691. // Trim a comment string
  692. function TrimComment ($comment, $preserve_line_endings) {
  693. // trim line breaks
  694. if (!$preserve_line_endings) $comment = trim($comment, "\n");
  695. // remove remains of C comments
  696. $comment = eregi_replace("^[!*/ ]+", "", $comment);
  697. $comment = eregi_replace("[!*/ ]+$", "", $comment);
  698. // remove all Pascal comment characters to prevent nesting
  699. $comment = str_replace(array("{", "}"), "", $comment);
  700. //print("$comment\n");
  701. return $comment;
  702. }
  703. // Builds comments from $line spaning multiple lines or at the end of lines
  704. // Returns a terminated comment string or a fragment in $fragment for futher processing
  705. function BuildComment ($line, $file) {
  706. // set the current comment header being parsed
  707. $this->comment_header = $file;
  708. // comment parsing is off, bail!
  709. if (!$this->parse_comments) return;
  710. // single-line comments
  711. // ??? check if this is at the end of line or not! this could be a full line comment
  712. if (eregi("[[:space:]]+//(.*)", $line, $captures)) {
  713. //print("$captures[1]\n");
  714. // ignore comment
  715. foreach ($this->ignore_comments as $pattern) {
  716. if (eregi($pattern, $captures[1])) return;
  717. }
  718. $this->comment_eol = $captures[1];
  719. return;
  720. }
  721. // multi-line comments terminated
  722. if (eregi("/\*(.*)\*/", $line, $captures)) {
  723. //print("$captures[1]\n");
  724. // ignore comment
  725. foreach ($this->ignore_comments as $pattern) {
  726. if (eregi($pattern, $captures[1])) return;
  727. }
  728. $this->comment_terminated = $this->TrimComment($captures[1], false);
  729. return;
  730. }
  731. // terminate comment fragment
  732. if ($this->comment_fragment_open) {
  733. if (eregi("(.*)\*/", $line, $captures)) {
  734. // append fragment
  735. $comment = $this->TrimComment($captures[1], false);
  736. if ($comment) $this->comment_fragment .= $comment;
  737. // closed comment block
  738. if (!$captures[1]) {
  739. $this->comment_block_closed = true;
  740. }
  741. // set terminated comment with fragment
  742. $this->comment_terminated = $this->comment_fragment;
  743. // add extra line break for comment blocks
  744. if (($this->comment_block_open) && ($this->comment_block_closed)) {
  745. // ??? the printing will cut this out if we add line endings!
  746. $this->comment_terminated = "$this->comment_fragment";
  747. }
  748. $this->comment_fragment = null;
  749. $this->comment_fragment_open = false;
  750. $this->comment_block_open = false;
  751. $this->comment_block_closed = false;
  752. return;
  753. } else {
  754. // build the fragment
  755. $comment = $this->TrimComment($line, true);
  756. // ignore comment and stop building fragment
  757. foreach ($this->ignore_comments as $pattern) {
  758. if (eregi($pattern, $comment)) {
  759. $this->comment_fragment = null;
  760. $this->comment_fragment_open = false;
  761. $this->comment_block_open = false;
  762. $this->comment_block_closed = false;
  763. return;
  764. }
  765. }
  766. if (($this->comment_fragment_previous != $line) && ($comment)) $this->comment_fragment .= $comment."\n";
  767. $this->comment_fragment_previous = $line;
  768. return;
  769. }
  770. }
  771. // start comment fragment
  772. if (eregi("/\*(.*)", $line, $captures)) {
  773. $this->comment_terminated = null;
  774. // ignore comment
  775. foreach ($this->ignore_comments as $pattern) {
  776. if (eregi($pattern, $captures[1])) return;
  777. }
  778. $this->comment_fragment_open = true;
  779. $this->comment_fragment = "";
  780. $this->comment_block_open = true;
  781. $this->comment_block_closed = false;
  782. // prevent against empty comments
  783. if ((!$captures[1]) || ($captures[1] == "\n")) {
  784. $this->comment_block_open = true;
  785. return;
  786. }
  787. // append the line if valid
  788. $comment = $this->TrimComment($captures[1], false);
  789. if ($comment) $this->comment_fragment .= $comment;
  790. return;
  791. }
  792. }
  793. // Resets current comment references
  794. function ResetComment () {
  795. $this->comment_fragment = null;
  796. $this->comment_eol = null;
  797. $this->comment_terminated = null;
  798. $this->comment_block_open = false;
  799. $this->comment_block_closed = false;
  800. }
  801. // Checks/sets if a comment is a duplicate in the file
  802. function CheckDuplicateComment ($comment) {
  803. if ((@!in_array($comment, $this->dump[$this->comment_header]["comments"])) && ($comment)) {
  804. $this->dump[$this->comment_header]["comments"][] = $comment;
  805. return true;
  806. } else {
  807. return false;
  808. }
  809. }
  810. // Appends the eol comment to the output and clears the reference
  811. function AppendEOLComment () {
  812. if ($this->comment_eol) {
  813. $comment = " // ".$this->TrimComment($this->comment_eol, false);
  814. $this->comment_eol = "";
  815. return $comment;
  816. } else {
  817. return null;
  818. }
  819. }
  820. // Inserts the recently terminated comment to the output on a new line and clears the reference
  821. function InsertCurrentComment () {
  822. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated))) {
  823. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  824. $this->comment_previous = $this->comment_terminated;
  825. $this->comment_terminated = "";
  826. return $comment;
  827. } else {
  828. return null;
  829. }
  830. }
  831. // Appends the recently terminated comment to a recipient and clears the reference
  832. function AppendCurrentComment (&$recipient) {
  833. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated)) && (gettype($recipient) == "array")) {
  834. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  835. $recipient[] = $comment;
  836. //if (is_array($recipient)) $recipient[] = $comment;
  837. //if (is_string($recipient)) $recipient .= $comment;
  838. $this->comment_previous = $this->comment_terminated;
  839. $this->comment_terminated = "";
  840. }
  841. }
  842. // Removes all comments from a line
  843. function RemoveComments ($line) {
  844. // remove single-line comments
  845. $line = eregi_replace("[[:space:]]+//(.*)", "", $line);
  846. // remove multi-line comments /* ... */
  847. $line = eregi_replace("/\*.*\*/", "", $line);
  848. return $line;
  849. }
  850. // Performs additional formatting on Objective-c type i.e. (out NSObject **)
  851. function FormatObjcType ($type, &$modifiers) {
  852. $modifiers = "";
  853. // toss out all const identifiers
  854. $type = istr_replace_word("const", "", $type);
  855. // replace inout paramaters
  856. $type = istr_replace_word("inout", "", $type);
  857. $type = istr_replace_word("out", "", $type);
  858. $type_clean = trim($type, "* ");
  859. // Replace types before cleaning
  860. $type = $this->ReplaceObjcType($type);
  861. // Remove protocol which type conforms to (id <NSURLHandleClient>)
  862. $type = eregi_replace("<.*>", "", $type);
  863. // Remove array brackets (NSUInteger[])p
  864. $type = eregi_replace("\[[0-9]*\]", "", $type);
  865. // var params to non-object types (NSRange *)
  866. if (ereg("([a-zA-Z0-9_]+)[[:space:]]*[*]+$", $type, $captures)) {
  867. if (!in_array($captures[1], $this->cocoa_classes)) {
  868. $type = $this->ReplaceReferenceParameterType($type_clean);
  869. //$modifiers = "var ";
  870. }
  871. }
  872. // Handle NS*** pointers (NSError **)
  873. if (ereg("(NS[a-zA-Z0-9_]+)[[:space:]]*\*\*$", $type, $captures)) {
  874. if (in_array($captures[1], $this->cocoa_classes)) {
  875. $type = "$type_clean$this->class_pointer_suffix";
  876. }
  877. }
  878. // clean the type
  879. $type = trim($type, "* ");
  880. //print("$type\n");
  881. return $type;
  882. }
  883. // Performs additional formatting on Objective-c parameter types
  884. function FormatObjcParams ($string, &$variable_arguments) {
  885. $params = explode(":", $string);
  886. $string = "";
  887. $variable_arguments = false;
  888. // print_r($params);
  889. if (count($params) > 0) {
  890. foreach ($params as $value) {
  891. // parameter is varargs
  892. if (eregi("(.*),[[:space:]]*\.\.\.", $value)) {
  893. $string .= ":(id)$this->varargs_param_name";
  894. $variable_arguments = true;
  895. //print("$value\n");
  896. continue;
  897. }
  898. if (preg_match("!^[^(]*$!",$value)) {
  899. // parameter without type -> id
  900. $value = "(id)".$value;
  901. }
  902. // normal parameter
  903. if (ereg("\((.*)\)", $value, $captures)) {
  904. $new_value = $this->ReplaceObjcType($captures[1]);
  905. if ($new_value != $captures[1]) $value = ereg_replace("\((.*)\)", "($new_value)", $value);
  906. $string .= ":$value";
  907. continue;
  908. }
  909. }
  910. }
  911. $string = ltrim($string, ":");
  912. return $string;
  913. }
  914. // Converts an Objective-c parameter string to Pascal
  915. function ConvertObjcParamsToPascal ($string, $protected_keywords) {
  916. $params = explode(":", $string);
  917. $list = array();
  918. $list["pairs"] = array();
  919. $param_list = array();
  920. //print_r($params);
  921. //print("$string\n");
  922. if (count($params) > 0) {
  923. foreach ($params as $value) {
  924. $value = trim($value);
  925. $valid = false;
  926. $modifiers = "";
  927. $value = $this->ReplaceRemoteMessagingModifiers($value, $null);
  928. // function pointer (callback)
  929. if (preg_match($this->pregex_function_pointer_objc_paratype, $value, $captures)) {
  930. $name = $captures[5];
  931. $function_pointer = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[4], false, "");
  932. $type = $this->AddCallback($name, $function_pointer);
  933. $valid = true;
  934. } elseif ((eregi("\(([a-zA-Z_]+).*\)([a-zA-Z_]+).*\.\.\.", $value, $captures)) || (eregi("(.*),[[:space:]]*\.\.\.", $value, $captures))) { // variable arguments
  935. $name = $captures[2];
  936. $type = $captures[1];
  937. $valid = true;
  938. } elseif (eregi("\((.*)\)[[:space:]]*([a-zA-Z_]+)", $value, $captures)) { // standard parameter
  939. $captures[1] = trim($captures[1]);
  940. $type = $this->FormatObjcType($captures[1], $modifiers);
  941. $name = $captures[2];
  942. $valid = true;
  943. }
  944. if ($valid) {
  945. // protect reserved keywords
  946. if ($this->IsKeywordReserved($name)) $name .= "_";
  947. if (!in_array($type, $this->reserved_types)) {
  948. if ($this->IsKeywordReserved($type)) $type .= "_";
  949. }
  950. // can't have "boolean: boolean;" parameters
  951. if (preg_match("!\b$name\b!i",$type)) $name .= "_";
  952. // print("para name: \"$name\"\n");
  953. // print_r("protected: \"$protected_keywords\"\n");
  954. while (@in_array(strtolower($name), $protected_keywords)) $name .= "_";
  955. // case-insensitive in_array:
  956. // if( preg_match("/\b$name\b/i", join(" ", array_values($protected_keywords))) ) $name .= "_";
  957. // if (@in_array($type, $protected_keywords)) $type .= "_";
  958. // replace objc types
  959. $type = $this->ReplaceObjcType($type);
  960. // make sure we label duplicate params, which are allowed in Objective-C
  961. while (in_array($name, $param_list)) {
  962. $count ++;
  963. $name = "$name$count";
  964. }
  965. // id is always a wrapper
  966. if (($this->objects_are_wrappers) && ($type == $this->objc_id)) {
  967. $name_list = "$type(GetHandle($name))";
  968. } else {
  969. $name_list = $name;
  970. }
  971. // add modifiers to the name if there are any
  972. $name_with_modifiers = $modifiers.$name;
  973. // create pair array
  974. $pair["name"] = $name;
  975. $pair["type"] = $type;
  976. // append list
  977. $list["pairs"][] = $pair;
  978. $list["string_with_modifiers"] .= "$name_with_modifiers: $type; ";
  979. $list["string"] .= "$name: $type; ";
  980. $list["list"] .= "$name_list, ";
  981. $param_list[] = $name;
  982. }
  983. }
  984. }
  985. // clean up the strings
  986. $list["string"] = trim($list["string"], "; ");
  987. $list["string_with_modifiers"] = trim($list["string_with_modifiers"], "; ");
  988. $list["list"] = trim($list["list"], ", ");
  989. return $list;
  990. }
  991. // Print string to output file
  992. function PrintOutput ($indent, $string) {
  993. for ($i=0; $i < $indent; $i++) {
  994. $indent_string .= " ";
  995. }
  996. if (($this->output) && (!$this->show)) fwrite($this->output, "$indent_string$string\n");
  997. if ($this->show) print("$indent_string$string\n");
  998. }
  999. // Returns a class hierarchy array
  1000. function GetClassHierarchy ($class, &$hierarchy) {
  1001. if (!$hierarchy) $hierarchy = array();
  1002. $hierarchy[] = $class["name"];
  1003. if ($class["super_class"]) {
  1004. $hierarchy[] = $this->GetClassHierarchy($class["super_class"], $hierarchy);
  1005. } else {
  1006. $hierarchy[] = "NSObject";
  1007. }
  1008. return $class["name"];
  1009. }
  1010. // Returns all protected keywords in a class hierarchy
  1011. function GetProtectedKeywords ($in_class) {
  1012. $this->GetClassHierarchy($in_class, $hierarchy);
  1013. $keywords = array();
  1014. foreach ($hierarchy as $class) {
  1015. if ($this->dump["master"][$class]["protected_keywords"]) {
  1016. foreach ($this->dump["master"][$class]["protected_keywords"] as $keyword) $keywords[] = strtolower($keyword);
  1017. }
  1018. }
  1019. return $keywords;
  1020. }
  1021. // Returns header a category should be moved to
  1022. function FindCategoryHeader ($category) {
  1023. foreach ($this->dump as $name => $header) {
  1024. if ((@array_key_exists($category, $header["classes"])) && ($category != "NSObject")) {
  1025. return $name;
  1026. }
  1027. }
  1028. }
  1029. // Checks if $class (array) contains $method (array)
  1030. function ClassContainsMethod ($class, $method) {
  1031. if ($class["methods"]) {
  1032. foreach ($class["methods"] as $key) {
  1033. if ($key["name"] == $method["name"]) return true;
  1034. }
  1035. }
  1036. }
  1037. // Adds a method structure to a class and performs checks for overloaded methods
  1038. function AddMethodToClass (&$method, &$class) {
  1039. // ignore methods
  1040. // if (in_array($method["name"], $this->ignore_methods)) return false;
  1041. // add comment to the method
  1042. $method["comment"] = $this->InsertCurrentComment();
  1043. if (@!in_array($method["name"], $class["declared_methods"])) {
  1044. $class["all"][$method["name"]] = $method;
  1045. $class["protected_keywords"][] = $method["name"];
  1046. $class["declared_methods"][] = $method["name"];
  1047. $this->dump["all_methods"][$class["name"]][] = $method["objc_method"];
  1048. if ($this->show_added_messages) print(" @ Added ".$method["name"]." to ".$class["name"]."\n");
  1049. $this->method_count ++;
  1050. return true;
  1051. } else {
  1052. print(" ! ".$method["def"]." already exists in ".$class["name"]." defined as ".$class["all"][$method["name"]]["def"]."\n");
  1053. }
  1054. }
  1055. // Adds a typedef to the header and handles organization to prevent order conflicts
  1056. function AddTypeDef (&$header, $typedef) {
  1057. //$this->AppendCurrentMacro($header["types"]["typedef"]);
  1058. $this->AppendCurrentComment($header["types"]["typedef"]);
  1059. if ($this->comment_terminated) $header["types"]["typedef"][] = $this->InsertCurrentComment();
  1060. $header["types"]["typedef"][] = $typedef;
  1061. }
  1062. // Returns a paramater list string with options to modify
  1063. function MakeParamList ($param_array, $use_handle, $cast_handle, $direct, $register_selector) {
  1064. $params = "";
  1065. foreach ($param_array as $pair) {
  1066. // register selector parameters
  1067. if (($register_selector) && ($pair["type"] == "SEL")) {
  1068. $params .= "sel_registerName(".$pair["name"]."), ";
  1069. continue;
  1070. }
  1071. // use the object handle for NSObject descendants
  1072. if ($use_handle) {
  1073. if (in_array($pair["type"], $this->cocoa_classes)) {
  1074. // cast the param to the original class type
  1075. if ($cast_handle) {
  1076. if ($direct == ACCESS_HANDLE_DIRECT) {
  1077. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  1078. } else {
  1079. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  1080. }
  1081. } else {
  1082. if ($direct == ACCESS_HANDLE_DIRECT) {
  1083. $params .= $pair["name"].".Handle, ";
  1084. } else {
  1085. $params .= "GetHandle(".$pair["name"]."), ";
  1086. }
  1087. }
  1088. } else {
  1089. if (($this->objects_are_wrappers) && ($pair["type"] == $this->objc_id)) { // id is always a wrapper
  1090. if ($direct == ACCESS_HANDLE_DIRECT) {
  1091. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  1092. } else {
  1093. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  1094. }
  1095. } else {
  1096. $params .= $pair["name"].", ";
  1097. }
  1098. }
  1099. } else { // append without modification
  1100. $params .= $pair["name"].", ";
  1101. }
  1102. }
  1103. return trim($params, ", ");
  1104. }
  1105. // Returns a list of paramameter variables with NS*** class types cast to "id" or the original class
  1106. function MakeObjcTypeParamList ($param_array, $objc_type) {
  1107. $params = "";
  1108. foreach ($param_array as $pair) {
  1109. if (in_array($pair["type"], $this->cocoa_classes)) {
  1110. if ($objc_type) {
  1111. $params .= "$this->objc_id(".$pair["name"]."), ";
  1112. } else {
  1113. $params .= $pair["type"]."(".$pair["name"]."), ";
  1114. }
  1115. } else {
  1116. $params .= $pair["name"].", ";
  1117. }
  1118. }
  1119. return trim($params, ", ");
  1120. }
  1121. /**
  1122. * PRINTING METHODS
  1123. */
  1124. // Prints all externally defined symbols
  1125. function PrintExternalSymbols ($header) {
  1126. if (!$this->dump[$header["name"]]["types"]) return;
  1127. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1128. // External string constants
  1129. if ($key == "string_constant") {
  1130. $this->PrintOutput(0, "");
  1131. $this->PrintOutput(0, "{ External string constants }");
  1132. $this->PrintOutput(0, "var");
  1133. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1134. }
  1135. if ($key == "external_symbol") {
  1136. $this->PrintOutput(0, "");
  1137. $this->PrintOutput(0, "{ External symbols }");
  1138. $this->PrintOutput(0, "var");
  1139. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1140. }
  1141. }
  1142. }
  1143. // Prints all types in the header
  1144. function PrintTypes ($header) {
  1145. if (!$this->dump[$header["name"]]["types"]) return;
  1146. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1147. $section_printed = false;
  1148. // External defines
  1149. if ($key == "defines") {
  1150. foreach ($type_array as $type) {
  1151. if (!$section_printed) {
  1152. $this->PrintOutput(0, "");
  1153. $this->PrintOutput(0, "{ Defines }");
  1154. $this->PrintOutput(0, "const");
  1155. $section_printed = true;
  1156. }
  1157. $this->PrintOutput(1, $type);
  1158. }
  1159. }
  1160. // Enumerations
  1161. if ($key == "enums") {
  1162. $this->PrintOutput(0, "");
  1163. $this->PrintOutput(0, "{ Constants }");
  1164. foreach ($type_array as $block) {
  1165. $section_printed = false;
  1166. foreach ($block as $type) {
  1167. if (!$section_printed) {
  1168. $this->PrintOutput(0, "");
  1169. $this->PrintOutput(0, "const");
  1170. $section_printed = true;
  1171. }
  1172. $this->PrintOutput(1, $type);
  1173. }
  1174. }
  1175. }
  1176. // Typedefs
  1177. if (($key == "typedef") || ($key == "named_enums")) {
  1178. foreach ($type_array as $type) {
  1179. if (!$section_printed) {
  1180. $this->PrintOutput(0, "");
  1181. $this->PrintOutput(0, "{ Types }");
  1182. $this->PrintOutput(0, "type");
  1183. $section_printed = true;
  1184. }
  1185. $this->PrintOutput(1, $type);
  1186. }
  1187. }
  1188. // CallBacks
  1189. if ($key == "callbacks") {
  1190. foreach ($type_array as $name => $type) {
  1191. if (!$section_printed) {
  1192. $this->PrintOutput(0, "");
  1193. $this->PrintOutput(0, "{ Callbacks }");
  1194. $this->PrintOutput(0, "type");
  1195. $section_printed = true;
  1196. }
  1197. $this->PrintOutput(1, "$name = $type");
  1198. }
  1199. }
  1200. }
  1201. }
  1202. // Prints all records in the header
  1203. function PrintRecords ($header) {
  1204. if (!$this->dump[$header["name"]]["types"]) return;
  1205. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1206. // Structures
  1207. if ($key == "structs") {
  1208. $this->PrintOutput(0, "");
  1209. $this->PrintOutput(0, "{ Records }");
  1210. foreach ($type_array as $type) {
  1211. $this->PrintOutput(0, "type");
  1212. $this->PrintOutput(1, $type);
  1213. }
  1214. }
  1215. }
  1216. }
  1217. // Prints all external functions in the header
  1218. function PrintFunctions ($header) {
  1219. if (!$this->dump[$header["name"]]["types"]) return;
  1220. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1221. if ($key == "functions") {
  1222. $this->PrintOutput(0, "");
  1223. $this->PrintOutput(0, "{ Functions }");
  1224. foreach ($type_array as $type) $this->PrintOutput(0, $type);
  1225. }
  1226. }
  1227. }
  1228. // Prints all classes from the header in reference format (not for compiling)
  1229. function PrintHeaderReference ($header, $path) {
  1230. $this->output = fopen($path, "w+");
  1231. //$this->PrintOutput(0, "{ ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  1232. $this->PrintOutput(0, "unit ".$header["name_clean"].";");
  1233. $this->PrintOutput(0, "interface");
  1234. $this->PrintOutput(0, "uses");
  1235. $this->PrintOutput(1, "ctypes, objc, MacOSAll, AnonClassDefinitions;");
  1236. if ($header["classes"]) {
  1237. foreach ($header["classes"] as $class) {
  1238. $this->PrintOutput(0, "");
  1239. $this->PrintOutput(0, "type");
  1240. $this->PrintOutput(1, $class["name"]."Ref = ".$this->objc_id.";");
  1241. $this->PrintOutput(1, $class["name"]."Ptr = Pointer;");
  1242. }
  1243. }
  1244. // types
  1245. $this->PrintTypes($header);
  1246. $this->PrintRecords($header);
  1247. $this->PrintFunctions($header);
  1248. $this->PrintExternalSymbols($header);
  1249. if ($header["classes"]) {
  1250. foreach ($header["classes"] as $class) {
  1251. if (in_array($class["name"], $this->cocoa_classes)) {
  1252. $this->PrintOutput(0, "");
  1253. $this->PrintOutput(0, "type");
  1254. $this->PrintOutput(1, $class["name"]." = object(".$class["super"].")");
  1255. // print class-level methods
  1256. if (count($class["methods"]) > 0) {
  1257. $this->PrintOutput(0, "");
  1258. foreach ($class["methods"] as $method) {
  1259. $this->PrintOutput(2, $method["def"]);
  1260. }
  1261. }
  1262. // print category-level methods
  1263. if (count($class["categories"]) > 0) {
  1264. foreach ($class["categories"] as $name => $category) {
  1265. $this->PrintOutput(0, "");
  1266. $this->PrintOutput(2, "{ Category: $name }");
  1267. if ($category["methods"]) {
  1268. foreach ($category["methods"] as $method) {
  1269. $this->PrintOutput(2, $method["def"]);
  1270. }
  1271. }
  1272. }
  1273. }
  1274. $this->PrintOutput(1, "end;");
  1275. }
  1276. }
  1277. }
  1278. // print procedural protocols
  1279. if ($header["protocols"]) {
  1280. foreach ($header["protocols"] as $protocol) {
  1281. if ($protocol["methods"]) {
  1282. $this->PrintOutput(0, "");
  1283. $this->PrintOutput(0, "{ Protocol: ".$protocol["name"]." }");
  1284. foreach ($protocol["methods"] as $name => $method) {
  1285. if ($method["kind"] != "constructor") {
  1286. $this->PrintProtocolDeclaration($protocol, $method);
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292. $this->PrintOutput(0, "");
  1293. $this->PrintOutput(0, "implementation");
  1294. $this->PrintOutput(0, "end.");
  1295. }
  1296. // Merges two headers by difference
  1297. function MergeHeader ($header) {
  1298. /*
  1299. diff QLPreviewPanel_ORIGINAL.inc QLPreviewPanel_UPDATED.inc > diff.patch
  1300. patch QLPreviewPanel_ORIGINAL.inc -i diff.patch -o output.txt
  1301. patch QLPreviewPanel.inc -i header.patch -o QLPreviewPanel.inc.merged
  1302. also add -section="types,classes" which only prints into the sections and copies text from
  1303. the previous version if not specified
  1304. */
  1305. // set the paths to use
  1306. $path = $header["path"];
  1307. $patch = "$this->root$this->out/$this->framework/header.patch";
  1308. $merged = $header["path_merge"];
  1309. // create a patch using diff
  1310. exec("/usr/bin/diff \"$path\" \"$merged\" > \"$patch\"");
  1311. // parse the diff file by mode
  1312. $lines = file($patch);
  1313. $content = "";
  1314. $section_lines = null;
  1315. $section_part = null;
  1316. $count = 0;
  1317. $section_id = 0;
  1318. //print_r($lines);
  1319. if ($lines) {
  1320. foreach ($lines as $line) {
  1321. $count++;
  1322. // remove line endings to aid regex
  1323. $line = trim($line, "\n");
  1324. // parse section
  1325. if (($section_lines) && (!eregi("^[0-9]+", $line))) {
  1326. // append line to section
  1327. $section_lines[] = "$line\n";
  1328. // the section id is adding
  1329. if (eregi("^>", $line)) $section_part[$section_id] = true;
  1330. if (eregi("^<", $line)) $section_part[$section_id] = 0;
  1331. // section is changing type
  1332. if ($line == "---") {
  1333. $section_id++;
  1334. continue;
  1335. }
  1336. }
  1337. // line is a new section or eof
  1338. if ((eregi("^[0-9]+", $line)) || ($count == count($lines))) {
  1339. // append the section to the content
  1340. // we only accept sections where the first part (before ---) contains additions ">"
  1341. //print_r($section_part);
  1342. if (($section_part[0]) && (!$section_part[1])) $content .= implode($section_lines);
  1343. // init the new section
  1344. $section_lines = array();
  1345. $section_lines[] = "$line\n";
  1346. $section_id = 0;
  1347. $section_part = array(null, null);
  1348. continue;
  1349. }
  1350. }
  1351. //print($content);
  1352. // write the parsed patch back to the file
  1353. if ($content) {
  1354. file_put_contents($patch, $content);
  1355. } else {
  1356. $content = null;
  1357. }
  1358. }
  1359. // patch the header to the merged file
  1360. if ($content) {
  1361. exec("/usr/bin/patch \"$path\" -i \"$patch\" -o \"$merged\" ");
  1362. // swap the contents of the merged file with the original
  1363. file_put_contents($path, file_get_contents($merged));
  1364. }
  1365. // delete the temporary files
  1366. unlink($patch);
  1367. unlink($merged);
  1368. }
  1369. function PrintGlobalClassInfo($all_classes, $anon_classes) {
  1370. // for pascocoa: do nothing
  1371. }
  1372. // Prints all headers parsed
  1373. function PrintAllHeaders ($output_path, $ignore_output, $only_files, $print_header_references) {
  1374. //print("• Printing ".count($this->dump)." headers...\n");
  1375. foreach ($this->dump as $file => $header) {
  1376. // the framework is set to not print, ignore the header
  1377. if (!$this->frameworks[$header["framework"]]["print"]) continue;
  1378. if (eregi("^[a-zA-Z0-9]+\.h", $file)) {
  1379. // ignore these files
  1380. if (@in_array($header["path_partial"], $ignore_output)) continue;
  1381. // only parse these files
  1382. if ((@count($only_files) > 0) && (@!in_array($header["name"], $only_files))) continue;
  1383. $name_clean = substr($file, 0, (strripos($file, ".")));
  1384. // assign output path
  1385. if ($output_path != "") {
  1386. $header["path"] = $output_path."/".$name_clean.".inc";
  1387. }
  1388. // print the header
  1389. $this->PrintHeader($header);
  1390. // merge the headers
  1391. if (($this->merge_headers) && (!$this->show)) {
  1392. $this->MergeHeader($header);
  1393. }
  1394. if ($print_header_references) $this->PrintHeaderReference($header, $this->root.$this->out."/reference/".$name_clean.".pas");
  1395. if (!$this->show) print("* Printed $name_clean.h to ".$header["path"]."\n");
  1396. }
  1397. }
  1398. // print global stuff (for Obj-P: a unit containing all classes as
  1399. // anonymous external classes, so they can be used before they are
  1400. // declared)
  1401. $this->PrintGlobalClassInfo($this->cocoa_classes, $this->defined_cocoa_classes, $this->anon_cocoa_classes);
  1402. }
  1403. /**
  1404. * PARSING METHODS
  1405. */
  1406. function GetAnonBitFieldName() {
  1407. $name = "_anonbitfield_".$this->current_header["name_clean"].$this->current_header["anonbitfields"];
  1408. $this->current_header["anonbitfields"]++;
  1409. return $name;
  1410. }
  1411. function ParseNormalField($line, $protected_keywords, &$field_bitpacked, &$bitpacked_real_type, &$this_class_field_names) {
  1412. if (preg_match("!^\s*([^-{}*@+,<;:]*struct[^-*@+,<;:]+|[^-{}*@+,<;:]+)\b\s*(?:<([^>]*)>)?([*]*)\s*(\w+)((?:\s*,\s*[^:;[]+)*)?\s*(:[0-9]+)?\s*(\[.*\])?\s*(?:__attribute__\(\(([^)]*)\)\))?\s*;!", $line, $captures)) { // regular field
  1413. // captures[1]: type name (may be multiple words, and may not be complete
  1414. // in case of an anonymous bitfield without a name)
  1415. // Curly braces are only allowed in case of structs
  1416. // captures[2]: in case of id<protocol list>, the protocol list, empty
  1417. // otherwise
  1418. // captures[3]: all pointer modifiers (*, **, ...), but only "*" and "**"
  1419. // currently handled
  1420. // captures[4]: the field name (may be last part of type in case of
  1421. // anonymous bit field, e.g. "unsigned long : 32")
  1422. // captures[5]: in case of multiple fields, the other field names (note
  1423. // that things will go wrong if some of the fields in the
  1424. // list are arrays/bitfields, that is only supported for
  1425. // a single field)
  1426. // captures[6]: bitfield specification if any, empty otherwise
  1427. // captures[7]: array specification if any, empty otherwise
  1428. // captures[8]: attributes if any, empty otherwise
  1429. if ($captures[3] != "") { // pointer type -> ok (cannot be wrongly interpreted)
  1430. $type = trim($captures[1]);
  1431. $pointertype = $captures[3];
  1432. $name = trim($captures[4]);
  1433. // print("pointer field: $name: $pointertype $type\n");
  1434. } else {
  1435. // make sure we properly interpret stuff like "unsigned int :32"
  1436. // print("regular: type = \"$captures[1]\", name = $captures[3]\n");
  1437. $pair = $this->ExtractCFieldSimpleTypeAndSingleName($captures[1]." ".$captures[4]);
  1438. $type = $pair["type"];
  1439. $name = $pair["name"];
  1440. if ($name == "") $name = $this->GetAnonBitFieldName();
  1441. // print("regular field: \"$name\": $type\n");
  1442. }
  1443. // print("field \"$name\": \"$type\", attr: $captures[8]\n");
  1444. // if we have id <protocollist> and there's only one protocol in the
  1445. // in the list, we can replace id with the protocol
  1446. if (($type == "id") && ($captures[2] != "") && !strstr(",", $captures[2])) {
  1447. // print("id<protocol>: $type -> $captures[2]Protocol\n");
  1448. $type = $captures[2]."Protocol";
  1449. }
  1450. // in case of "type field1, field2, field3", ", field2, field3" gets
  1451. // stored in othernames
  1452. $othernames = $captures[5];
  1453. $field_prefix = "";
  1454. // Multiple Objective-C fields cannot have the same name, but they
  1455. // are case-insensitive and some only differ in case (only "reserved"
  1456. // fields until now, but that can change)
  1457. while (in_array(strtolower("$field_prefix$name"),$protected_keywords)) $field_prefix.="_";
  1458. if ($this_class_field_names != null) {
  1459. while (in_array(strtolower("$field_prefix$name"),$this_class_field_names)) $field_prefix.="_";
  1460. }
  1461. if ($this->IsKeywordReserved($field_prefix.$name)) $field_prefix .= "_";
  1462. // protect the name of these fields, in case there are methods with
  1463. // the same name
  1464. $this_class_field_names[] = strtolower($field_prefix.$name);
  1465. if ($othernames != "") {
  1466. $other_lower_case_names = preg_split("/\s*,\s*/",$othernames,-1,PREG_SPLIT_NO_EMPTY);
  1467. array_walk($other_lower_case_names,strtolowerref);
  1468. // should actually also check these for conflicts and add underscores if necessary
  1469. $this_class_field_names = array_merge ($this_class_field_names, $other_lower_case_names);
  1470. }
  1471. /*
  1472. // we should also add prefixes to the other names if required, but my
  1473. // php-fu is too weak to replace the elements in original string
  1474. // efficiently, and this problem does not occur in the supported headers
  1475. foreach (explode($othernames, ",") as $othername) {
  1476. while (in_array(strtolower("$field_prefix$othername"),$protected_keywords)) $field_prefix.="_";
  1477. if ($this->IsKeywordReserved($field_prefix.$othernamename)) $field_prefix .= "_";
  1478. }
  1479. */
  1480. // remove "struct" from the type
  1481. $type = preg_replace("!(\b)struct\b!","\1",$type);
  1482. // clean/convert type
  1483. $type = $this->ReplaceObjcType($type);
  1484. $bitpacked_real_type = $type;
  1485. $type = $this->MakeFieldBitPacked($type, $line, $field_bitpacked);
  1486. // add pointer modifiers
  1487. $type = $this->EncodePointerModifiers($type,$pointertype);
  1488. $field = "$field_prefix$name$othernames: $type";
  1489. if ($captures[8] && strstr("deprecated",$captures[8])) $field .= " deprecated";
  1490. $field .= ";";
  1491. $field = $this->MakeFieldInlineArray($field, $line, $name, $type);
  1492. $field = eregi_replace("<.*>", "", $field);
  1493. return $field;
  1494. }
  1495. return "";
  1496. }
  1497. function ParseInstanceVariables ($line, &$struct, $protected_keywords, &$this_class_field_names) {
  1498. $field = null;
  1499. $field_bitpacked = false;
  1500. // print("$line\n");
  1501. // insert macros
  1502. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) {
  1503. if ($struct["valid"]) {
  1504. $struct["fields"][] = $macro;
  1505. return null;
  1506. } else {
  1507. return $macro;
  1508. }
  1509. }
  1510. // got inline struct, probably a reference to a private struct
  1511. if (eregi("[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\*([a-zA-Z0-9_]+)", $line, $captures)) {
  1512. if ($struct["valid"]) {
  1513. // ??? These don't compile as inline records which I thought they did...
  1514. //$struct["fields"][] = "$captures[1] = record end;";
  1515. //$struct["fields"][] = "$captures[1]Pointer = ^$captures[1];";
  1516. $struct["fields"][] = "$captures[2]: Pointer;";
  1517. $struct["ignore"][] = "$captures[2]";
  1518. return null;
  1519. } else {
  1520. //$fields = array();
  1521. //$fields[] = "$captures[1] = record end;";
  1522. //$fields[] = "$captures[1]Ptr = ^$captures[1];";
  1523. $field = "_$captures[2]: Pointer;";
  1524. return $field;
  1525. }
  1526. }
  1527. // got struct
  1528. if (eregi("^[[:space:]]*struct.*{", $line)) {
  1529. $struct["valid"] = true;
  1530. $struct["ispsuedostruct"] = false;
  1531. $struct["fieldnames"] = array();
  1532. return null;
  1533. }
  1534. // create an anonymous struct in case we have bitpacked fields without a
  1535. // surrounding struct
  1536. if (!$struct["valid"] && preg_match("!.*:[0-9]+\s*;!", $line)) {
  1537. $struct["valid"] = true;
  1538. $struct["ispsuedostruct"] = true;
  1539. $struct["fieldnames"] = array();
  1540. }
  1541. // end of bunch of bitfields -> go to normal mode again
  1542. if ($struct["ispsuedostruct"] && !preg_match("!.*:[0-9]+\s*;!", $line)) {
  1543. $struct["name"] = "_anoninternstruct_".$this->current_header["name_clean"].$this->current_header["anoninternstrucs"];
  1544. $this->current_header["anoninternstrucs"]++;
  1545. $struct["isfinished"] = true;
  1546. // make sure the current field isn't added anymore
  1547. $struct["valid"] = false;
  1548. }
  1549. // end of struct
  1550. if (eregi("^[[:space:]]*}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  1551. $struct["name"] = "_".trim($captures[1], " ");
  1552. //print_r($struct);
  1553. $struct["isfinished"] = true;
  1554. return "struct";
  1555. }
  1556. // set field prefix to protect scope
  1557. if (!$struct["valid"]) $field_prefix = "_";
  1558. // remove null-defined macros:
  1559. $line = str_ireplace($this->null_macros, "", $line);
  1560. // replace garbage collector hints in the field
  1561. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  1562. if (preg_match($this->pregex_function_pointer, $line, $captures)) { // function pointer
  1563. // print("function pointer: $line\n");
  1564. $field =$this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1565. } else {
  1566. if (!$struct["valid"])
  1567. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$this_class_field_names);
  1568. else
  1569. // don't register the names of fields of embedded structs and field names of the current
  1570. // class, but do track them for the current struct as there may be conflicts due to
  1571. // Pascal's case-insensitivity
  1572. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$struct["fieldnames"]);
  1573. }
  1574. // mark the field as having a garbage collector field
  1575. if ($garbage_collector_hint) $field = "$field {garbage collector: $garbage_collector_hint }";
  1576. // return field
  1577. if ($struct["valid"]) {
  1578. if (!$struct["bitpacked"]) $struct["bitpacked_first_type"] = $bitpacked_real_type;
  1579. if ($field_bitpacked) $struct["bitpacked"] = true;
  1580. $struct["fields"][] = $field;
  1581. } else {
  1582. return $field;
  1583. }
  1584. }
  1585. // Parses $line into a function declaration string. Handles both
  1586. // function pointers (isexternfunc = false) and external functions
  1587. // (isexternfunc = true)
  1588. function ParseFunctionDeclaration($rettypestr, $retpointertypestr, $funcname, $parastr, $isexternfunc, $deprecatedmods) {
  1589. if ($deprecatedmods != "") $deprecatedmods .= ";";
  1590. if ($this->IsKeywordReserved($funcname)) $funcname .= "_";
  1591. $rettype = trim(str_replace_word("const","",$rettypestr));
  1592. $rettype = $this->ReplaceObjcType($rettype);
  1593. $rettype = $this->EncodePointerModifiers($rettype,$retpointertypestr);
  1594. $params = $this->ConvertCParamsPascal($parastr);
  1595. if ($rettype == "void")
  1596. $result = "procedure ";
  1597. else
  1598. $result = "function ";
  1599. // if no name specified, the caller will add it
  1600. if ($funcname != "") {
  1601. if (!$isexternfunc)
  1602. $result = "$funcname: " . $result;
  1603. else
  1604. $result .= $funcname;
  1605. }
  1606. if ($params != "") $params = "(" . $params . ")";
  1607. if ($rettype == "void")
  1608. $result .= $params . "; cdecl;";
  1609. else
  1610. $result .= $params . ": " . $rettype . "; cdecl;";
  1611. if ($isexternfunc)
  1612. $result .= " external;$deprecatedmods";
  1613. else
  1614. $result .= "\n";
  1615. return $result;
  1616. }
  1617. // Parses $line into the combined $struct_fields string
  1618. function ParseStructFields ($line, $protected_keywords, &$struct_fields, &$found_any_bitpacked, &$all_bitpacked, &$first_bitpacked_type) {
  1619. if (preg_match($this->pregex_function_pointer, $line, $captures)) {
  1620. $struct_fields .= " " . $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1621. $all_bitpacked = false;
  1622. } else {
  1623. // better: keep for entire struct, so we can escape conflicting names due to
  1624. // case-insensitivity
  1625. $tempnewprotected = array();
  1626. $new_field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$tempnewprotected);
  1627. // print("field: '$new_field', bitpacked: $field_bitpacked, any: $found_any_bitpacked, all: $all_bitpacked\n");
  1628. if ($new_field != "") {
  1629. $found_any_bitpacked |= $field_bitpacked;
  1630. if ($struct_fields == "") {
  1631. $all_bitpacked = $field_bitpacked;
  1632. $first_bitpacked_type = $bitpacked_real_type;
  1633. }
  1634. else $all_bitpacked &= $field_bitpacked;
  1635. $struct_fields .= " " . $new_field . $this->AppendEOLComment() . "\n";
  1636. }
  1637. // print(" after: any: $found_any_bitpacked, all: $all_bitpacked\n");
  1638. }
  1639. }
  1640. // Parse a single enum field
  1641. function ParseEnumFields ($line, $file_name, &$block_count, &$auto_increment) {
  1642. // insert macros
  1643. //if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $this->dump[$file_name]["types"]["enums"][$block_count][] = $macro;
  1644. if (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([a-zA-Z_]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // string value
  1645. $captures[2] = trim($captures[2], ", ");
  1646. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1647. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9-]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // integer value
  1648. $captures[2] = trim($captures[2], ", ");
  1649. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1650. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9]+[xX]+[a-fA-F0-9]+)[)]*", $line, $captures)) { // hexadecimal value
  1651. $captures[2] = trim($captures[2], ", ");
  1652. $captures[2] = eregi_replace("^0x", "$", $captures[2]);
  1653. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1654. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)", $line, $captures)) { // << shl value, no ()
  1655. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  1656. // remove integer type hints
  1657. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  1658. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  1659. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1660. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*\(([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)\)", $line, $captures)) { // << shl value
  1661. $captures[2] = trim($captures[2], ", ");
  1662. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  1663. // remove integer type hints
  1664. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  1665. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  1666. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1667. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*[,}]*[[:space:]]*$", $line, $captures)) { // non-value
  1668. // omit lines which started nested structures.
  1669. // bad practice but the single-line regex parser can't handle them
  1670. if (!eregi("[=|]+", $line)) {
  1671. $captures[1] = trim($captures[1], ", ");
  1672. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$auto_increment.";";
  1673. $auto_increment ++;
  1674. }
  1675. }
  1676. }
  1677. // create a variant record with a first anonymous field of type
  1678. // first_bitpacked_type so that the compiler may correctly align it
  1679. // (it does not do that currently, but this at least provides a
  1680. // hint to the programmer)
  1681. function BitPackedForceAlignment($first_bitpacked_type, $firstindent, $otherindents) {
  1682. $result = $firstindent . "case byte of\n" . $otherindents ."0: (" . $this->GetAnonBitFieldName() . ": $first_bitpacked_type);\n" . $otherindents . "1: (";
  1683. return $result;
  1684. }
  1685. // Parse external symbols, enums and typedef's from the header
  1686. function ParseHeaderTypes ($file) {
  1687. $contents = ReadTextFile($file);
  1688. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  1689. $any_field_bitpacked = false;
  1690. $all_fields_bitpacked = false;
  1691. // reset comments from previous parsing sections
  1692. $this->ResetComment();
  1693. $lines = explode("\n", $contents);
  1694. foreach ($lines as $line) {
  1695. // skip blocks
  1696. if ($this->SkipBlock($line)) continue;
  1697. // ignore lines
  1698. if (in_array($line, $this->ignore_lines)) continue;
  1699. // build comments
  1700. $this->BuildComment($line, $file_name);
  1701. // build macro blocks
  1702. $this->BuildMacroBlocks($line);
  1703. // garbage collector hints
  1704. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  1705. // remove macros
  1706. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  1707. // remove comments
  1708. $line = $this->RemoveComments($line);
  1709. $line = trim($line, " ");
  1710. if ($got_struct) {
  1711. // insert macros
  1712. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $struct_fields .= "$macro\n";
  1713. // collect fields
  1714. $this->ParseStructFields($line, array(), $struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  1715. // got end of struct
  1716. if (ereg("^}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  1717. if ($struct_name == "") {
  1718. $struct_name = $captures[1];
  1719. } else {
  1720. $struct_type = $captures[1];
  1721. }
  1722. // ignore this struct
  1723. if (in_array($struct_name, $this->ignore_types)) continue;
  1724. if ($all_fields_bitpacked) {
  1725. $struct = "$struct_comment$struct_name = $this->bitpacked_record_keyword\n";
  1726. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  1727. $struct_fields = str_replace(" "," ",$struct_fields);
  1728. $struct_fields .= " );\n";
  1729. } else {
  1730. $struct = "$struct_comment$struct_name = $this->record_keyword\n";
  1731. }
  1732. $struct .= $struct_fields;
  1733. $struct .= " end$deprecatedmods;\n";
  1734. if (($struct_type) && ($struct_name != $struct_type)) {
  1735. $struct .= "$struct_type = $struct_name;\n";
  1736. }
  1737. // pointer type
  1738. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  1739. $this->dump[$file_name]["types"]["structs"][] = $struct;
  1740. $this->dump["global_structs"][] = $struct_name;
  1741. $got_struct = false;
  1742. $any_field_bitpacked = false;
  1743. $all_fields_bitpacked = false;
  1744. }
  1745. }
  1746. // got single-line struct
  1747. if (ereg("^typedef[[:space:]]+struct[[:space:]]+{(.*)}[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  1748. $struct_name = trim($captures[2], " ");
  1749. if (!in_array($struct_name, $this->ignore_types)) {
  1750. // break the struct into lines
  1751. $single_struct_fields = "";
  1752. $fields = explode(";", $captures[1]);
  1753. $comment = $this->InsertCurrentComment();
  1754. $this->ResetComment();
  1755. // parse each line
  1756. foreach ($fields as $field) {
  1757. $field = trim($field);
  1758. $this->ParseStructFields($field.";", array(), $single_struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  1759. }
  1760. // merge the fields into the definition
  1761. $struct = "$comment\n"."$struct_name = ";
  1762. if ($all_fields_bitpacked) {
  1763. $struct .= "$this->bitpacked_record_keyword\n";
  1764. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  1765. $single_struct_fields = str_replace(" "," ",$single_struct_fields);
  1766. $single_struct_fields .= " );\n";
  1767. } else $struct .= "$this->record_keyword\n";
  1768. $struct .= $single_struct_fields;
  1769. $struct .= " end$deprecatedmods;\n";
  1770. // pointer type
  1771. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  1772. $this->dump[$file_name]["types"]["structs"][] = $struct;
  1773. $this->dump["global_structs"][] = $struct_name;
  1774. $any_field_bitpacked = false;
  1775. $all_fields_bitpacked = false;
  1776. //print("$single_struct_fields\n");
  1777. }
  1778. // got begin of struct
  1779. } elseif (ereg("^typedef struct(.*){", $line, $captures)) {
  1780. $struct_name = trim($captures[1], " ");
  1781. if (!in_array($struct_name, $this->ignore_types)) {
  1782. $struct_type = null;
  1783. $struct_fields = "";
  1784. $struct_comment = $this->InsertCurrentComment();
  1785. $this->ResetComment();
  1786. if ($struct_comment != "") $struct_comment = "$struct_comment\n";
  1787. $got_struct = true;
  1788. }
  1789. }
  1790. // got function pointer type
  1791. if (preg_match($this->pregex_function_pointer_typedef, $line, $captures)) {
  1792. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[6], false, $deprecatedmods);
  1793. $functypename = $captures[7];
  1794. if ($functypename == "") $functypename = $captures[5];
  1795. if ($functypename == "") $functypename = $captures[4];
  1796. $this->dump[$file_name]["types"]["callbacks"][$functypename] = $typestr;
  1797. // record if it is a function type instead of a function pointer type
  1798. if ($captures[3] == "") $this->implicit_function_pointer_types[] = $functypename;
  1799. continue;
  1800. }
  1801. // #defines
  1802. $got_define = false;
  1803. if (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\(\(.*\)(.*)\)", $line, $captures)) { // named constant with type case
  1804. $got_define = true;
  1805. } elseif (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+[(]*([0-9.-]+)[)]*", $line, $captures)) { //integer
  1806. $got_define = true;
  1807. }
  1808. if ($got_define) {
  1809. $define_name = $captures[1];
  1810. if (!in_array($define_name, $this->ignore_types)) {
  1811. $define_name = $this->ReplaceObjcType($define_name);
  1812. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["defines"][] = $this->InsertCurrentComment();
  1813. $this->AppendCurrentComment($this->dump[$file_name]["types"]["defines"]);
  1814. $this->dump[$file_name]["types"]["defines"][] = $define_name." = ".$captures[2].";".$this->AppendEOLComment();
  1815. } else {
  1816. $this->ResetComment();
  1817. }
  1818. }
  1819. // parse enum fields
  1820. if (($got_enum) || ($got_named_enum)) {
  1821. // print($line."\n");
  1822. $this->ParseEnumFields($line, $file_name, &$block_count, &$auto_increment);
  1823. // found the end
  1824. if (ereg("^};", $line)) $got_enum = false;
  1825. }
  1826. // ==== got inline named enum ===
  1827. if (ereg("^[[:space:]]*enum[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]*{(.*)};", $line, $captures)) {
  1828. //print("$line\n");
  1829. $enum_name = trim($captures[1], " ");
  1830. if (!in_array($enum_name, $this->ignore_types)) {
  1831. $block_count ++;
  1832. $auto_increment = 0;
  1833. // break the enum into lines
  1834. $fields = explode(",", $captures[2]);
  1835. //$this->AppendCurrentMacro($this->dump[$file_name]["types"]["enums"][$block_count]);
  1836. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  1837. if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  1838. $this->ResetComment();
  1839. // parse each line
  1840. foreach ($fields as $field) {
  1841. $field = trim($field, " ");
  1842. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  1843. }
  1844. }
  1845. continue;
  1846. }
  1847. // ==== got inline enum ===
  1848. if (ereg("^[[:space:]]*enum[[:space:]]*{(.*)};", $line, $captures)) {
  1849. //print("$line\n");
  1850. $block_count ++;
  1851. $auto_increment = 0;
  1852. // break the enum into lines
  1853. $fields = explode(",", $captures[1]);
  1854. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  1855. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  1856. $this->ResetComment();
  1857. // parse each line
  1858. foreach ($fields as $field) {
  1859. $field = trim($field, " ");
  1860. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  1861. }
  1862. continue;
  1863. }
  1864. // ==== got enum ===
  1865. if (ereg("^enum", $line)) {
  1866. $got_enum = true;
  1867. $block_count ++;
  1868. $auto_increment = 0;
  1869. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  1870. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  1871. }
  1872. // terminate named enum
  1873. if ($got_named_enum) {
  1874. if (ereg("^}[[:space:]]*([a-zA-Z0-9_]+);", $line, $captures)) {
  1875. $got_named_enum = false;
  1876. $named_enum = trim($named_enum, ", \n");
  1877. $name = $captures[1];
  1878. if (!in_array($name, $this->ignore_types)) {
  1879. $this->dump[$file_name]["types"]["named_enums"][] = "$name = culong;";
  1880. $this->dump["global_types"][$name] = $name;
  1881. }
  1882. }
  1883. }
  1884. // ==== got named enum ===
  1885. if (ereg("^typedef enum {", $line)) {
  1886. $got_named_enum = true;
  1887. $named_enum = "";
  1888. $auto_increment = 0;
  1889. $block_count ++;
  1890. $this->AppendCurrentComment($this->dump[$file_name]["types"]["named_enums"][$block_count]);
  1891. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["named_enums"][] = $this->InsertCurrentComment();
  1892. }
  1893. // ==== external functions ===
  1894. // doesn't work when $this->external_string_macros is added to
  1895. // the string at initialisation time, because it can still change
  1896. // later (while loading frameworks.xml)
  1897. if (preg_match("!^(?:$this->external_string_macros)+".$this->pregex_external_function_end, $line, $captures)) {
  1898. // ignore symbols
  1899. if (in_array($captures[3], $this->ignore_types)) continue;
  1900. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], true, $deprecatedmods);
  1901. $this->dump[$file_name]["types"]["functions"][] = $typestr;
  1902. continue;
  1903. }
  1904. // ==== external string constant ===
  1905. if (eregi("^($this->external_string_macros)+[[:space:]]+NSString[[:space:]]*\*[[:space:]]*(const)*[[:space:]]*([a-zA-Z0-9_]+)", $line, $captures)) {
  1906. $name = $captures[3];
  1907. if (in_array($name, $this->ignore_types)) continue;
  1908. // insert comments
  1909. $this->AppendCurrentComment($this->dump[$file_name]["types"]["string_constant"]);
  1910. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["string_constant"][] = $this->InsertCurrentComment();
  1911. $this->dump[$file_name]["types"]["string_constant"][] = "$name: $this->string_macro$deprecatedmods; cvar; external;";
  1912. }
  1913. // ==== external symbol ===
  1914. if (eregi("^($this->external_string_macros)+[[:space:]]+([a-zA-Z0-9_ ]+)[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  1915. $name = $captures[3];
  1916. $type = $captures[2];
  1917. // ignore symbols
  1918. if (in_array($name, $this->ignore_types)) continue;
  1919. $type = istr_replace_word("const", "", $type);
  1920. $type = $this->ReplaceObjcType(trim($type, " "));
  1921. $this->AppendCurrentComment($this->dump[$file_name]["types"]["external_symbol"]);
  1922. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["external_symbol"][] = $this->InsertCurrentComment();
  1923. $this->dump[$file_name]["types"]["external_symbol"][] = "$name: $type$deprecatedmods; cvar; external;";
  1924. }
  1925. // ==== got typedef ===
  1926. if (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // defined struct type
  1927. $real_type = $captures[1];
  1928. $struct_type = $captures[1];
  1929. $new_type = $captures[2];
  1930. // ignore types
  1931. if (in_array($struct_type, $this->ignore_types)) continue;
  1932. if (in_array($new_type, $this->ignore_types)) continue;
  1933. $this->AddTypeDef($this->dump[$file_name], "$struct_type = record end$deprecatedmods;");
  1934. $struct_type = $this->ReplaceObjcType($struct_type);
  1935. if ($new_type != $struct_type) {
  1936. $this->AddTypeDef($this->dump[$file_name], "$new_type = $struct_type$deprecatedmods;");
  1937. $this->dump["global_types"][$new_type] = $real_type;
  1938. }
  1939. $this->opaque_structs[] = $struct_type;
  1940. // also add pointer type to the opaque struct
  1941. $this->AddTypeDef($this->dump[$file_name], $new_type."Ptr = ^$new_type$deprecatedmods;");
  1942. $this->dump["global_types"][$struct_type] = "record end";
  1943. $this->dump["global_types"][$new_type."Ptr"] = "^".$new_type;
  1944. } elseif (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // pointer to struct
  1945. $real_type = $captures[1];
  1946. $clean_name = trim($captures[2], "*");
  1947. $pointer_type = $captures[1];
  1948. // ignore types
  1949. if (in_array($clean_name, $this->ignore_types)) continue;
  1950. $pointer_type = "Pointer";
  1951. $this->AddTypeDef($this->dump[$file_name], "$clean_name = $pointer_type$deprecatedmods;");
  1952. $this->dump["global_types"][$clean_name] = $real_type;
  1953. // also add pointer type
  1954. $this->AddTypeDef($this->dump[$file_name], $clean_name."Ptr = ^$clean_name$deprecatedmods;");
  1955. $this->dump["global_types"][$clean_name."Ptr"] = "^".$clean_name;
  1956. } elseif (ereg("^typedef[[:space:]]+(const)*[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_*]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // struct type (complex)
  1957. $real_type = $captures[1];
  1958. $typedef_name = $captures[3];
  1959. // ignore types
  1960. if (in_array($typedef_name, $this->ignore_types)) continue;
  1961. $captures[2] = $this->FormatObjcType($captures[2], $modifiers);
  1962. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = ".$captures[2].$deprecatedmods.";");
  1963. $this->dump["global_types"][$typedef_name] = $real_type;
  1964. // also add pointer type
  1965. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  1966. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  1967. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // single-word type
  1968. $real_type = $captures[1];
  1969. // type is a pointer
  1970. if ($captures[2][0] == "*") {
  1971. $captures[2] = trim($captures[2], "*");
  1972. $captures[1] = $this->ReplaceObjcType($captures[1]);
  1973. // ignore types
  1974. if (in_array($captures[2], $this->ignore_types)) continue;
  1975. $this->AddTypeDef($this->dump[$file_name], $captures[2]." = ^".$captures[1]."$deprecatedmods;");
  1976. $this->dump["global_types"][$captures[2]] = $real_type;
  1977. } else {
  1978. $captures[2] = trim($captures[2], "*");
  1979. $captures[1] = $this->ReplaceObjcType($captures[1]);
  1980. // ignore types
  1981. if (in_array($captures[2], $this->ignore_types)) continue;
  1982. $this->AddTypeDef($this->dump[$file_name],$captures[2]." = ".$captures[1]."$deprecatedmods;");
  1983. }
  1984. // also add pointer type
  1985. $this->AddTypeDef($this->dump[$file_name], $captures[2]."Ptr = ^$captures[2]$deprecatedmods;");
  1986. $this->dump["global_types"][$captures[2]."Ptr"] = "^".$captures[2];
  1987. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // double-word type
  1988. $real_type = $captures[1];
  1989. $typedef_name = trim($captures[3], "*");
  1990. $long_type = $captures[1]." ".$captures[2];
  1991. $long_type = $this->ReplaceObjcType($long_type);
  1992. // ignore types
  1993. if (in_array($captures[2], $this->ignore_types)) continue;
  1994. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = $long_type$deprecatedmods;");
  1995. $this->dump["global_types"][$typedef_name] = $real_type;
  1996. // also add pointer type
  1997. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  1998. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  1999. }
  2000. }
  2001. //print_r($this->dump[$file_name]["types"]);
  2002. }
  2003. // Parse all protocols in a header
  2004. function ParseHeaderProtocols ($file) {
  2005. $contents = ReadTextFile($file);
  2006. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2007. // reset comments from previous parsing sections
  2008. $this->ResetComment();
  2009. $lines = explode("\n", $contents);
  2010. foreach ($lines as $line) {
  2011. // skip blocks
  2012. if ($this->SkipBlock($line)) continue;
  2013. // ignore lines
  2014. if (in_array($line, $this->ignore_lines)) continue;
  2015. // remove macros
  2016. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2017. // build comments
  2018. if (!$got_got_protocol) $this->BuildComment($line, $file_name);
  2019. // parse protocol
  2020. if ($got_protocol) {
  2021. // build comments
  2022. $this->BuildComment($line, $file_name);
  2023. // remove comments
  2024. $line = $this->RemoveComments($line);
  2025. // found property
  2026. if ($this->LineHasProperty($line, $captures)) {
  2027. $properties = $this->ParseClassProperty($current_protocol, $captures, $deprecatedmods);
  2028. foreach ($properties as $property) {
  2029. if ($property["setter"]) {
  2030. $property["setter"]["comment"] = $this->InsertCurrentComment();
  2031. $this->current_header["protocols"][$current_protocol]["methods"][$property["setter"]["objc_method"]] = $property["setter"];
  2032. // append to master list of protocols
  2033. $this->dump["protocols"][$current_protocol][] = $property["setter"];
  2034. }
  2035. if ($property["getter"]) {
  2036. $property["getter"]["comment"] = $this->InsertCurrentComment();
  2037. $this->current_header["protocols"][$current_protocol]["methods"][$property["getter"]["objc_method"]] = $property["getter"];
  2038. // append to master list of protocols
  2039. $this->dump["protocols"][$current_protocol][] = $property["getter"];
  2040. }
  2041. }
  2042. continue;
  2043. }
  2044. // found method
  2045. $method = null;
  2046. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2047. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), true, $deprecatedmods);
  2048. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2049. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), false, $deprecatedmods);
  2050. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2051. $method_fragment = $line;
  2052. }
  2053. // append to classes
  2054. if (($method) /* && (!in_array($method["name"], $this->ignore_methods))*/ ) {
  2055. // add comment to the method
  2056. $method["comment"] = $this->InsertCurrentComment();
  2057. $this->current_header["protocols"][$current_protocol]["methods"][$method["objc_method"]] = $method;
  2058. // append to master list of protocols
  2059. $this->dump["protocols"][$current_protocol][] = $method;
  2060. }
  2061. // found the end
  2062. if (ereg("^@end", $line)) {
  2063. $this->ResetComment();
  2064. $got_protocol = false;
  2065. }
  2066. }
  2067. // got protocol
  2068. if ((eregi($this->regex_objc_protocol, $line, $captures)) && (!eregi(".*;$", $line))) {
  2069. $got_protocol = true;
  2070. $current_protocol = $captures[1];
  2071. print("+ Protocol $current_protocol\n");
  2072. if ($this->comment_terminated) $this->current_header["protocols"][$current_protocol]["comment"] = $this->InsertCurrentComment();
  2073. $this->current_header["protocols"][$current_protocol]["name"] = $captures[1];
  2074. }
  2075. }
  2076. //print_r($this->current_class);
  2077. }
  2078. // Parse all categories in a header
  2079. function ParseHeaderCategories ($file) {
  2080. $contents = ReadTextFile($file);
  2081. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2082. // reset comments from previous parsing sections
  2083. $this->ResetComment();
  2084. $lines = explode("\n", $contents);
  2085. foreach ($lines as $line) {
  2086. // skip blocks
  2087. if ($this->SkipBlock($line)) continue;
  2088. // ignore lines
  2089. if (in_array($line, $this->ignore_lines)) continue;
  2090. // remove macros
  2091. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2092. // build comments
  2093. if (!$got_category) $this->BuildComment($line, $file_name);
  2094. // parse category
  2095. if ($got_category) {
  2096. // build comments
  2097. $this->BuildComment($line, $file_name);
  2098. // remove comments
  2099. $line = $this->RemoveComments($line);
  2100. // found property
  2101. if ($this->LineHasProperty($line, $captures)) {
  2102. $properties = $this->ParseClassProperty($current_category, $captures, $deprecatedmods);
  2103. if (!in_array($current_category, $this->ignore_categories)) {
  2104. foreach ($properties as $property) {
  2105. if ($property["setter"]) {
  2106. if ($this->AddMethodToClass($property["setter"], $this->current_class)) {
  2107. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["setter"];
  2108. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["setter"];
  2109. }
  2110. }
  2111. if ($property["getter"]) {
  2112. if ($this->AddMethodToClass($property["getter"], $this->current_class)) {
  2113. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["getter"];
  2114. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["getter"];
  2115. }
  2116. }
  2117. }
  2118. }
  2119. continue;
  2120. }
  2121. // build method fragment
  2122. if ($method_fragment) $method_fragment .= " ".trim($line);
  2123. // found method fragment termination
  2124. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2125. $line = $method_fragment;
  2126. $method_fragment = null;
  2127. }
  2128. // found method
  2129. $method = null;
  2130. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2131. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2132. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2133. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2134. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2135. $method_fragment = $line;
  2136. }
  2137. // append to classes
  2138. if (($method) && !in_array($current_category, $this->ignore_categories)) {
  2139. if ($current_class) {
  2140. if ($this->AddMethodToClass($method, $this->current_class)) {
  2141. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $method;
  2142. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $method;
  2143. }
  2144. }
  2145. }
  2146. // found the end
  2147. if (ereg("^@end", $line)) {
  2148. $got_category = false;
  2149. continue;
  2150. }
  2151. }
  2152. // got category
  2153. if (eregi($this->regex_objc_category, $line, $captures)) {
  2154. $got_category = true;
  2155. $category_owner = $file_name;
  2156. $category_name = $captures[2];
  2157. $current_class = $captures[1];
  2158. $current_category = $category_name;
  2159. if (!in_array($current_category, $this->ignore_categories)) {
  2160. // Protect category names against duplicate identifiers by appending the class it extends to the name
  2161. if ((count($this->dump["categories"][$current_category]) > 0) || (in_array($category_name, $this->cocoa_classes))) {
  2162. $current_category = $category_name."_".$current_class;
  2163. $this->dump[$file_name]["categories"][$current_category]["external"] = true;
  2164. } else {
  2165. $this->dump[$file_name]["categories"][$current_category]["external"] = false;
  2166. }
  2167. $this->current_class = &$this->dump[$category_owner]["classes"][$current_class];
  2168. // insert into headers category array
  2169. $this->dump[$file_name]["categories"][$current_category]["name"] = $current_category;
  2170. $this->dump[$file_name]["categories"][$current_category]["super"] = $current_class;
  2171. $this->dump[$file_name]["categories"][$current_category]["comment"] = $this->InsertCurrentComment();
  2172. $this->dump[$file_name]["categories"][$current_category]["external_name"] = $category_name;
  2173. // append to master list of categories
  2174. $this->dump["categories"][$category_name][] = $current_class;
  2175. }
  2176. }
  2177. }
  2178. //print_r($this->current_class);
  2179. }
  2180. // Parse all "pre-defined" category methods in a header
  2181. function PreparseCategoryMethods ($file) {
  2182. $contents = ReadTextFile($file);
  2183. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2184. $lines = explode("\n", $contents);
  2185. foreach ($lines as $line) {
  2186. // skip blocks
  2187. if ($this->SkipBlock($line)) continue;
  2188. // parse category
  2189. if ($got_category) {
  2190. // build method fragment
  2191. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2192. // found method fragment termination
  2193. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2194. $line = $method_fragment;
  2195. $method_fragment = null;
  2196. }
  2197. // found method
  2198. $method = null;
  2199. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2200. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), true, "");
  2201. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2202. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), false, "");
  2203. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2204. $method_fragment = $line;
  2205. }
  2206. // append to classes
  2207. if (($method) && ($current_class)) {
  2208. $this->dump[$category_owner]["category_methods"][] = $method["name"];
  2209. //print($method["name"]."\n");
  2210. }
  2211. // found the end
  2212. if (ereg("^@end", $line)) $got_category = false;
  2213. }
  2214. // got category
  2215. if (eregi($this->regex_objc_category, $line, $captures)) {
  2216. $category_owner = $this->FindCategoryHeader($captures[1]);
  2217. if ($category_owner) {
  2218. $got_category = true;
  2219. $current_category = $captures[2];
  2220. $current_class = $captures[1];
  2221. } else {
  2222. $current_class = null;
  2223. }
  2224. }
  2225. }
  2226. return $this->dump[$category_owner]["category_methods"];
  2227. }
  2228. // Preparses a class for protected keywords
  2229. function PreparseClass ($lines, $line_count) {
  2230. $protected_keywords = array();
  2231. for ($i=$line_count; $i < count($lines); $i++) {
  2232. $line = $lines[$i - 1];
  2233. // skip blocks
  2234. if ($this->SkipBlock($line)) continue;
  2235. // build method fragment
  2236. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2237. // found method fragment termination
  2238. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2239. $line = $method_fragment;
  2240. $method_fragment = null;
  2241. }
  2242. // found method
  2243. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2244. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, true, "");
  2245. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  2246. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2247. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, false, "");
  2248. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  2249. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2250. $method_fragment = $line;
  2251. }
  2252. // class ended
  2253. if (ereg("^@end", $line)) return $protected_keywords;
  2254. }
  2255. }
  2256. // Gets the preferred property name from attributes
  2257. function GetPropertyName ($kind, $params, &$name) {
  2258. foreach ($params as $value) {
  2259. $pair = explode("=", $value);
  2260. if ($pair[0] == $kind) {
  2261. $name = $pair[1];
  2262. return true;
  2263. break;
  2264. }
  2265. }
  2266. }
  2267. function EncodePointerModifiers($type, $pointertype) {
  2268. if ($pointertype[0] == "*") $type = $this->ReplacePointerType($type);
  2269. if ($pointertype[1] == "*") {
  2270. if ($type == "Pointer") {
  2271. $type = "PPointer";
  2272. } elseif ($type == "PChar") {
  2273. $type = "PPChar";
  2274. } elseif (in_array($type, $this->cocoa_classes)) {
  2275. $type = $type.$this->class_pointer_suffix;
  2276. } else {
  2277. // to match MacOSAll types
  2278. $type = $type."Ptr";
  2279. }
  2280. }
  2281. return $type;
  2282. }
  2283. // Convert a method return type to Pascal
  2284. function ConvertReturnType ($type, $pointertype) {
  2285. $type = $this->ReplaceGarbageCollectorHints($type, $null);
  2286. $type = $this->ReplaceRemoteMessagingModifiers($type, $null);
  2287. // format the return type to make sure it's clean
  2288. $type = $this->FormatObjcType($type, $null_modifier);
  2289. // translate type to Pascal
  2290. $type = $this->ReplaceObjcType($type);
  2291. // incorportate pointer modifiers
  2292. $type = $this->EncodePointerModifiers($type,$pointertype);
  2293. return $type;
  2294. }
  2295. // Checks if a line has a property and returns the parts
  2296. function LineHasProperty ($line, &$parts) {
  2297. if (eregi($this->regex_objc_property_attributes, $line, $captures)) {
  2298. $parts = $captures;
  2299. //print_r($parts);
  2300. return true;
  2301. } elseif (eregi($this->regex_objc_property, $line, $captures)) {
  2302. $parts = $captures;
  2303. //print_r($parts);
  2304. return true;
  2305. }
  2306. }
  2307. // Parse a property into accessor methods
  2308. function ParseClassProperty ($class, $parts, $deprecatedmods) {
  2309. $method = array();
  2310. //print_r($parts);
  2311. // property has attributes
  2312. if (count($parts) == 5) {
  2313. $property["parameters"] = explode(",", $parts[1]);
  2314. $attributes = $parts[1];
  2315. $type = $parts[2];
  2316. $pointertype = $parts[3];
  2317. $content = $parts[4];
  2318. } else {
  2319. $property["parameters"] = array();
  2320. $type = $parts[1];
  2321. $pointertype = $parts[2];
  2322. $content = $parts[3];
  2323. }
  2324. // unspecified type -> id
  2325. if ($type == "") $type = "id";
  2326. // get property list
  2327. $list = explode(",", $content);
  2328. if (count($list) > 1) {
  2329. $property_list = array();
  2330. foreach ($list as $key) {
  2331. // clean the name and remove the return type
  2332. $property_list[] = trim($key);
  2333. }
  2334. //print_r($property_list);
  2335. } else {
  2336. $property_list = array($content);
  2337. }
  2338. $methods = array();
  2339. foreach ($property_list as $property_name) {
  2340. // property name
  2341. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $property_name, $captures)) $property["name"] = ucwords($captures[1]);
  2342. // property type
  2343. $type = $this->ConvertReturnType($type,$pointertype);
  2344. // prepare for appending
  2345. if ($deprecatedmods != "") $deprecatedmods .= ";";
  2346. // setter
  2347. if (!in_array("readonly", $property["parameters"])) {
  2348. $method["setter"] = array();
  2349. $name = $property["name"];
  2350. if (!$this->GetPropertyName("setter", $property["parameters"], $name)) {
  2351. $name = "set$name";
  2352. }
  2353. // protect method name from keywords
  2354. if ($this->IsKeywordReserved($name)) $name .= "_";
  2355. $method["setter"]["def"] = "procedure $name (newValue: $type);";
  2356. $method["setter"]["objc_method"] = "$name:";
  2357. $method["setter"]["class"] = $class;
  2358. $method["setter"]["name"] = $name;
  2359. $method["setter"]["kind"] = "procedure";
  2360. $method["setter"]["deprecated"]=$deprecatedmods;
  2361. //$method["setter"]["comment"] = $this->InsertCurrentComment();
  2362. }
  2363. // getter
  2364. $method["getter"] = array();
  2365. $name = $property["name"];
  2366. if (!$this->GetPropertyName("getter", $property["parameters"], $name)) {
  2367. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  2368. }
  2369. // protect method name from keywords
  2370. if ($this->IsKeywordReserved($name)) $name .= "_";
  2371. $method["getter"]["def"] = "function $name: $type;";
  2372. $method["getter"]["objc_method"] = $name;
  2373. $method["getter"]["class"] = $class;
  2374. $method["getter"]["name"] = $name;
  2375. $method["getter"]["kind"] = "function";
  2376. $method["getter"]["deprecated"]=$deprecatedmods;
  2377. //$method["getter"]["comment"] = $this->InsertCurrentComment();
  2378. // append to array of methods
  2379. $methods[] = $method;
  2380. }
  2381. //print_r($methods);
  2382. return $methods;
  2383. }
  2384. // Main entry to parse a header
  2385. function ParseHeaderClasses ($file) {
  2386. $contents = ReadTextFile($file);
  2387. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2388. $line_count = 0;
  2389. // reset comments from previous parsing sections
  2390. $this->ResetComment();
  2391. $lines = explode("\n", $contents);
  2392. foreach ($lines as $line) {
  2393. $line_count++;
  2394. // skip blocks
  2395. if ($this->SkipBlock($line)) continue;
  2396. // ignore lines
  2397. if (in_array($line, $this->ignore_lines)) continue;
  2398. // remove macros
  2399. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2400. // remove external class macros
  2401. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2402. // build comments
  2403. if (!$got_class) $this->BuildComment($line, $file_name);
  2404. // parse instance vars
  2405. if ($got_instance_vars) {
  2406. // scope compiler directive
  2407. if (eregi($this->regex_scope_compiler_directive, $line, $captures)) {
  2408. $this->instance_var_scope = $captures[1];
  2409. continue;
  2410. }
  2411. // remove comments
  2412. $line = $this->RemoveComments($line);
  2413. // parse instance variables
  2414. $result = $this->ParseInstanceVariables($line, $struct, $this->GetProtectedKeywords($this->current_class),$this->dump["master"][$this->current_class["name"]]["field_names"]);
  2415. // parse structures
  2416. if ($struct["isfinished"]) {
  2417. //print_r($struct);
  2418. //$this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": $current"."_".$struct["name"].";";
  2419. $this->dump[$file_name]["classes"][$current]["ivars_structs"][] = $struct;
  2420. // print inline-record type
  2421. if ($struct["bitpacked"]) {
  2422. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->bitpacked_record_keyword;
  2423. $this->dump[$file_name]["classes"][$current]["ivars"][] = $this->BitPackedForceAlignment($struct["bitpacked_first_type"], " ", " ");
  2424. } else {
  2425. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->record_keyword;
  2426. }
  2427. // print fields
  2428. if ($struct["fields"]) {
  2429. foreach ($struct["fields"] as $field) $this->dump[$file_name]["classes"][$current]["ivars"][] = " ".$field;
  2430. }
  2431. if ($struct["bitpacked"]) $this->dump[$file_name]["classes"][$current]["ivars"][] = " );";
  2432. $this->dump[$file_name]["classes"][$current]["ivars"][] = " end;";
  2433. $struct = null;
  2434. }
  2435. if(($result != null) && ($result != "struct")) {
  2436. //print($result);
  2437. // add a single string or an array of fields to the ivars array
  2438. if (count($result) <= 1) {
  2439. $this->dump[$file_name]["classes"][$current]["ivars"][] = $result;
  2440. } else {
  2441. foreach ($result as $field) {
  2442. $this->dump[$file_name]["classes"][$current]["ivars"][] = $field;
  2443. }
  2444. }
  2445. }
  2446. // instance var section terminated.
  2447. if (eregi("^\s*}\s*[;]*$", $line)) {
  2448. $struct = null;
  2449. $got_instance_vars = false;
  2450. $this->instance_var_scope = null;
  2451. }
  2452. } elseif ($got_class) { // parse the class
  2453. // the instance variable section started after the class line and no other ivar's were parsed yet
  2454. if (!$this->dump[$file_name]["classes"][$current]["ivars"]) {
  2455. if (eregi("{\s*$", $line)) {
  2456. $got_instance_vars = true;
  2457. continue;
  2458. }
  2459. }
  2460. // build comments
  2461. $this->BuildComment($line, $file_name);
  2462. // remove comments
  2463. $line = $this->RemoveComments($line);
  2464. // found property
  2465. if ($this->LineHasProperty($line, $captures)) {
  2466. $properties = $this->ParseClassProperty($current, $captures, $deprecatedmods);
  2467. foreach ($properties as $property) {
  2468. if ($property["setter"]) {
  2469. if ($this->AddMethodToClass($property["setter"], $this->dump[$file_name]["classes"][$current])) {
  2470. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["setter"];
  2471. }
  2472. }
  2473. if ($property["getter"]) {
  2474. if ($this->AddMethodToClass($property["getter"], $this->dump[$file_name]["classes"][$current])) {
  2475. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["getter"];
  2476. }
  2477. }
  2478. }
  2479. continue;
  2480. }
  2481. // build method fragment
  2482. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2483. // found method fragment termination
  2484. if (($method_fragment) && (eregi($this->regex_objc_method_terminate, $line))) {
  2485. $line = $method_fragment;
  2486. $method_fragment = null;
  2487. }
  2488. // found method
  2489. if (eregi($this->regex_objc_method_params, $line, $captures)) {
  2490. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2491. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2492. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2493. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2494. }
  2495. } elseif (eregi($this->regex_objc_method_no_params, $line, $captures)) {
  2496. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2497. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2498. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2499. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2500. }
  2501. } elseif (eregi($this->regex_objc_method_partial, $line, $captures)) {
  2502. $method_fragment = $line;
  2503. }
  2504. // found the end
  2505. if (ereg("^@end", $line)) {
  2506. $got_class = false;
  2507. $this->ResetComment();
  2508. }
  2509. }
  2510. // ==== got class ====
  2511. if ((eregi($this->regex_objc_class, $line, $captures)) || (eregi($this->regex_objc_class_no_super, $line, $captures))) {
  2512. $current = $captures[1];
  2513. $got_class = true;
  2514. // check for instance variable section
  2515. if (eregi("{\s*$", $line)) $got_instance_vars = true;
  2516. // get the protocols which the class adopts
  2517. if (eregi($this->regex_objc_class, $line, $captures)) {
  2518. if ($captures[3]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[3];
  2519. } else {
  2520. if ($captures[2]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[2];
  2521. }
  2522. // clean up the conforms string
  2523. if ($this->dump[$file_name]["classes"][$current]["adopts"]) {
  2524. $conform_protocols = explode(",", $this->dump[$file_name]["classes"][$current]["adopts"]);
  2525. $protocol_list = array();
  2526. foreach ($conform_protocols as $protocol) {
  2527. $protocol = trim($protocol, "<> ");
  2528. $protocol_clean .= $protocol."$this->protocol_suffix, ";
  2529. $protocol_list[] = $protocol;
  2530. }
  2531. $protocol_clean = trim($protocol_clean, ", ");
  2532. $this->dump[$file_name]["classes"][$current]["adopts"] = $protocol_clean;
  2533. $this->dump[$file_name]["classes"][$current]["protocols"] = $protocol_list;
  2534. $protocol_clean = "";
  2535. }
  2536. $this->dump[$file_name]["classes"][$current]["name"] = $captures[1];
  2537. $this->dump[$file_name]["classes"][$current]["super"] = $captures[2];
  2538. $this->dump[$file_name]["classes"][$current]["super_class"] = &$this->dump["master"][$captures[2]];
  2539. $this->dump[$file_name]["classes"][$current]["file_name"] = $file_name;
  2540. $this->dump[$file_name]["classes"][$current]["file_clean"] = substr($file_name, 0, (strripos($file_name, ".")));
  2541. $this->dump[$file_name]["classes"][$current]["protected_keywords"] = array();
  2542. $this->dump[$file_name]["classes"][$current]["declared_methods"] = array();
  2543. $this->dump[$file_name]["classes"][$current]["comment"] = $this->InsertCurrentComment();
  2544. $this->dump[$file_name]["category_methods"] = array();
  2545. $this->current_class = &$this->dump[$file_name]["classes"][$current];
  2546. // append master class listing
  2547. $this->dump["master"][$current] = &$this->dump[$file_name]["classes"][$current];
  2548. // preparse for protected keywords
  2549. $this->PreparseClass($lines, $line_count);
  2550. // preparse for category methods that may present naming conflicts
  2551. $category_methods = $this->PreparseCategoryMethods($file);
  2552. // add category methods to protected keywords
  2553. if ($category_methods) $this->current_class["protected_keywords"] = array_merge($this->current_class["protected_keywords"], $category_methods);
  2554. // print class hierarchy
  2555. if ($this->show_class_hierarchy) {
  2556. $this->GetClassHierarchy($this->current_class, $hierarchy);
  2557. $hierarchy_string = "";
  2558. foreach ($hierarchy as $value) {
  2559. $hierarchy_string .= "$value->";
  2560. }
  2561. $hierarchy_string = trim($hierarchy_string, "->");
  2562. print(" - $current: $hierarchy_string\n");
  2563. }
  2564. $this->class_count ++;
  2565. //print_r($this->dump[$file_name]["classes"][$current]);
  2566. }
  2567. }
  2568. //print_r($this->dump[$file_name]["classes"][$current]);
  2569. }
  2570. // Parse categories which depend on another header
  2571. function ParseHeaderDependents ($file) {
  2572. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2573. $this->ParseHeaderCategories($file);
  2574. print("+ Parsed $file_name for dependents\n");
  2575. }
  2576. // Main entry to parse a header
  2577. function ParseHeader ($file) {
  2578. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2579. $name_clean = substr($file_name, 0, (strripos($file_name, ".")));
  2580. // get the framework we're parsing from if it was specified during a batch parse
  2581. if ((!$this->framework) && (eregi("/([a-zA-Z]+)\.framework", $file, $captures))) $this->framework = strtolower($captures[1]);
  2582. // get the output path
  2583. $this->dump[$file_name]["path"] = "$this->root$this->out/$this->framework/$name_clean.inc";
  2584. $this->dump[$file_name]["path_partial"] = "$this->framework/$name_clean.inc";
  2585. $this->dump[$file_name]["path_merge"] = $this->dump[$file_name]["path"].".merge";
  2586. $this->dump[$file_name]["framework"] = $this->framework;
  2587. $this->dump[$file_name]["name"] = $file_name;
  2588. $this->dump[$file_name]["name_clean"] = $name_clean;
  2589. $this->dump[$file_name]["anoninternstrucs"] = 0;
  2590. $this->dump[$file_name]["anonbitfields"] = 0;
  2591. $this->current_header = &$this->dump[$file_name];
  2592. $this->ParseHeaderTypes($file);
  2593. $this->ParseHeaderProtocols($file);
  2594. $this->ParseHeaderClasses($file);
  2595. print("+ Parsed $file_name\n");
  2596. }
  2597. // Parse all AppKit and Foundation framework headers
  2598. function ParseAllFrameworks ($ignore_files, $parse_only) {
  2599. foreach ($this->frameworks as $framework_name => $framework_info) {
  2600. // framework is disabled
  2601. if ($framework_info["enabled"] != 1) continue;
  2602. // set the current framework being parsed
  2603. $this->framework = $framework_name;
  2604. if ($this->out != "/") {
  2605. $path = $this->root.$this->out."/".$framework_info["root"];
  2606. } else {
  2607. $path = $this->root.$framework_info["root"];
  2608. }
  2609. // Load the header if found
  2610. if (file_exists($path)) {
  2611. $contents = ReadTextFile($path);
  2612. $lines = explode("\n", $contents);
  2613. foreach ($lines as $line) {
  2614. $header = null;
  2615. $path = null;
  2616. // parse the header path from the {$include} macro
  2617. if (eregi($framework_info["include_pattern"], $line, $captures)) {
  2618. $header = $captures[1].".h";
  2619. $path = $framework_info["headers"]."/$header";
  2620. }
  2621. // parse the header path from {-parse} directive
  2622. if (eregi("^\{-parse[[:space:]]+(.*)[[:space:]]*\}", $line, $captures)) {
  2623. $header = $captures[1];
  2624. $path = $framework_info["headers"]."/$header";
  2625. }
  2626. // parse the header if valid
  2627. if (file_exists($path)) {
  2628. // main header
  2629. if ($parse_only) {
  2630. if (@in_array($header, $parse_only)) $this->ParseHeader($path);
  2631. } elseif (@!in_array($header, $ignore_files)) {
  2632. $this->ParseHeader($path);
  2633. }
  2634. // header dependents
  2635. if ($parse_only) {
  2636. if (@in_array($header, $parse_only)) $this->ParseHeaderDependents($path);
  2637. } elseif (@!in_array($header, $ignore_files)) {
  2638. $this->ParseHeaderDependents($path);
  2639. }
  2640. } elseif ($header) {
  2641. print("*** The header $path could not be found. ***\n");
  2642. $this->warning_count++;
  2643. }
  2644. }
  2645. } else {
  2646. print("*** The master include ".$framework_info["root"]." is missing. ***\n");
  2647. exit;
  2648. }
  2649. }
  2650. // diagnostics
  2651. print("\n• Parsed $this->method_count methods in $this->class_count classes.\n\n");
  2652. if ($this->warning_count > 0) print("• $this->warning_count warnings were encountered.\n\n");
  2653. }
  2654. // Parse all classes/categories (non-delegate) from the header
  2655. function CollectHeaderClasses ($file) {
  2656. // can't find the header, bail
  2657. if (!file_exists($file)) return;
  2658. $contents = ReadTextFile($file);
  2659. $lines = explode("\n", $contents);
  2660. foreach ($lines as $line) {
  2661. // remove external class macros
  2662. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2663. // classes
  2664. if (eregi($this->regex_objc_class, $line, $captures)) {
  2665. $this->defined_cocoa_classes[] = $captures[1];
  2666. // may already have been parsed as an anonymous class
  2667. if (!in_array($captures[1], $this->cocoa_classes))
  2668. $this->cocoa_classes[] = $captures[1];
  2669. }
  2670. if (eregi($this->regex_objc_class_no_super, $line, $captures)) {
  2671. $this->defined_cocoa_classes[] = $captures[1];
  2672. // may already have been parsed as an anonymous class
  2673. if (!in_array($captures[1], $this->cocoa_classes))
  2674. $this->cocoa_classes[] = $captures[1];
  2675. }
  2676. // anonymous classes ===
  2677. if (eregi($this->regex_objc_anon_class, $line, $captures)) {
  2678. $anon_classes = explode(",", $captures[1]);
  2679. foreach ($anon_classes as $anon_class) {
  2680. $anon_class=trim($anon_class);
  2681. // may already have been parsed as a regular class
  2682. if (!in_array($anon_class, $this->cocoa_classes)) {
  2683. $this->cocoa_classes[] = $anon_class;
  2684. if (!in_array($anon_class, $this->anon_cocoa_classes)) {
  2685. $this->anon_cocoa_classes[] = $anon_class;
  2686. }
  2687. }
  2688. }
  2689. }
  2690. // categories
  2691. if (eregi($this->regex_objc_category, $line, $captures)) {
  2692. $this->cocoa_categories[] = $captures[1];
  2693. }
  2694. }
  2695. }
  2696. // Build array of all known classes in frameworks
  2697. function BuildFrameworkClasses () {
  2698. foreach ($this->frameworks as $framework_name => $framework_info) {
  2699. // framework is disabled
  2700. if ($framework_info["enabled"] != 1) continue;
  2701. $handle = opendir($framework_info["headers"]);
  2702. while (($file = readdir($handle)) !== false) {
  2703. if (eregi($framework_info["header_pattern"], $file)) {
  2704. $this->CollectHeaderClasses($framework_info["headers"]."/$file");
  2705. }
  2706. }
  2707. closedir($handle);
  2708. }
  2709. }
  2710. function ProcessFile ($file, $print) {
  2711. // set the current framework to null so it's parsed from the framework
  2712. $this->framework = null;
  2713. $this->ParseHeader($file);
  2714. $this->ParseHeaderDependents($file);
  2715. if ($print) $this->PrintAllHeaders("", null, null, false);
  2716. }
  2717. function LoadFrameworksXML ($framework_path) {
  2718. $xml = new SimpleXMLElement(file_get_contents("frameworks.xml"));
  2719. foreach ($xml as $framework) {
  2720. $this->frameworks[(string) $framework->name]["root"] = (string) $framework->root;
  2721. $this->frameworks[(string) $framework->name]["bridge"] = (string) $framework->bridge;
  2722. $this->frameworks[(string) $framework->name]["headers"] = (string) $framework->headers;
  2723. if ($framework_path != "")
  2724. $this->frameworks[(string) $framework->name]["headers"] = preg_replace("!^.*/System/Library/Frameworks!", $framework_path, $this->frameworks[(string) $framework->name]["headers"]);
  2725. $this->frameworks[(string) $framework->name]["include_pattern"] = (string) $framework->include_pattern;
  2726. $this->frameworks[(string) $framework->name]["header_pattern"] = (string) $framework->header_pattern;
  2727. $this->frameworks[(string) $framework->name]["external_macro"] = (string) $framework->external_macro;
  2728. $this->frameworks[(string) $framework->name]["ignore_types"] = (string) $framework->ignore_types;
  2729. $this->frameworks[(string) $framework->name]["ignore_methods"] = (string) $framework->ignore_methods;
  2730. $this->frameworks[(string) $framework->name]["replace_types"] = $framework->replace_types;
  2731. $this->frameworks[(string) $framework->name]["ignore_lines"] = $framework->ignore_lines;
  2732. $this->frameworks[(string) $framework->name]["ignore_comments"] = $framework->ignore_comments;
  2733. $this->frameworks[(string) $framework->name]["enabled"] = false;
  2734. $this->frameworks[(string) $framework->name]["print"] = true;
  2735. }
  2736. }
  2737. function __construct ($directory, $out_directory, $frameworks, $frameworks_path, $show) {
  2738. $this->root = $directory;
  2739. $this->out = $out_directory;
  2740. $this->show = $show;
  2741. // load defined frameworks from xml
  2742. $this->LoadFrameworksXML($frameworks_path);
  2743. // enable frameworks requested by the command line options
  2744. if ($frameworks) {
  2745. foreach ($frameworks as $name) {
  2746. $name_clean = trim($name, "^ ");
  2747. $this->frameworks[$name_clean]["enabled"] = true;
  2748. // apply options from framework definition
  2749. if ($this->frameworks[$name_clean]["external_macro"]) $this->external_string_macros .= "|".$this->frameworks[$name_clean]["external_macro"];
  2750. if ($this->frameworks[$name_clean]["ignore_types"]) $this->ignore_types = array_merge($this->ignore_types, explode(",", $this->frameworks[$name_clean]["ignore_types"]));
  2751. if ($this->frameworks[$name_clean]["ignore_methods"]) $this->ignore_methods = array_merge($this->ignore_methods, explode(",", $this->frameworks[$name_clean]["ignore_methods"]));
  2752. if ($this->frameworks[$name_clean]["ignore_lines"]) {
  2753. foreach ($this->frameworks[$name_clean]["ignore_lines"]->line as $line) {
  2754. if (!in_array($line, $this->ignore_lines)) $this->ignore_lines[] = (string)$line;
  2755. }
  2756. }
  2757. if ($this->frameworks[$name_clean]["ignore_comments"]) {
  2758. foreach ($this->frameworks[$name_clean]["ignore_comments"]->line as $line) {
  2759. if (!in_array($line, $this->ignore_comments)) $this->ignore_comments[] = (string)$line;
  2760. }
  2761. }
  2762. if ($this->frameworks[$name_clean]["replace_types"]) {
  2763. foreach ($this->frameworks[$name_clean]["replace_types"]->type as $type) {
  2764. $pair = explode("=", (string)$type);
  2765. $this->replace_types[$pair[0]] = $pair[1];
  2766. }
  2767. }
  2768. // print mode
  2769. if ($name[0] == "^") $this->frameworks[$name_clean]["print"] = false;
  2770. }
  2771. }
  2772. //print_r($this->ignore_comments);
  2773. //print_r($this->ignore_lines);
  2774. //print_r($this->frameworks);
  2775. //exit;
  2776. $this->BuildFrameworkClasses();
  2777. }
  2778. }
  2779. ?>