objp.php 127 KB

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