objp.php 127 KB

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