summaryrefslogtreecommitdiff
path: root/packages/cocoaint/utils/source/objp_parser.php
blob: c5d1bcd3c1f704bf1b5695b5bfd8bed62ce6efe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php

class TObjPParser extends TPasCocoaParser {
	
	var $trailing_underscore = true;
	
	// ignore these classes when testing for ivar size
	var $ignore_class_ivar_comparison = array(	"NSNibOutletConnector", "NSNibConnector", "NSNibControlConnector", "NSPredicateEditorRowTemplate", "NSSegmentedCell",
												"NSSimpleHorizontalTypesetter", "NSInvocation", "NSPointerFunctions", "NSConstantString");
	
	// These methods require that the last parameter append a trailing underscore (if $trailing_underscore is on)
	var $trailing_underscore_methods = array("- (void)copy:(id)sender;", "- (void)setNeedsDisplay:(BOOL)flag;","- (void*)QTMovie;","- (QTMovie *)QTMovie;","- (BOOL)load:(NSError **)error;");
	
	var $ignore_methods = array("observationInfo"); 

	// Converts an Objective-c method name to Pascal
	function ConvertObjcMethodName ($method) {
		$params = explode(":", $method);
		$name = "";
		$count = 0;
		
		if (count($params) > 1) {
			foreach ($params as $value) {
				if (eregi("([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1]."_";
			}
		} else {
			if (eregi("([a-zA-Z0-9]+)(;)*$", $params[0], $captures)) $name .= $captures[1]."_";
		}
		
		// clean it up
		if ($this->trailing_underscore) {
		 if (!in_array($method, $this->trailing_underscore_methods)) $name = trim($name, "_");	
		}
		
		$name = $this->ReplaceObjcType($name);
		
		return $name;
	} 

	// Converts an Objective-C method to Pascal format 
	function ConvertObjcMethodToPascal ($class, $source, $parts, $protected_keywords, $has_params, $deprecatedmods) {
		
		//print("$source\n");
		//print_r($parts);
		
		// replace "hinted" params comment with hinted type
		if ($this->replace_hinted_params) {

			// param string
			if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[4], $captures)) {
				// ??? change the parameter to the hinted type
				//$parts[4] = eregi_replace("(/\*.*\*/)", $captures[2], $parts[4]);
				//$parts[4] = trim($parts[4], " ");
			}

			// return type
			if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[2], $captures)) $parts[2] = $this->ReplaceRemoteMessagingModifiers($captures[2], $null);

			//print_r($parts);

		} else { // remove comments from params and return type
			$parts[4] = eregi_replace("(/\*.*\*/)", "", $parts[4]);
			$parts[4] = trim($parts[4]);

			$parts[2] = eregi_replace("(/\*.*\*/)", "", $parts[2]);
			$parts[2] = $this->ReplaceRemoteMessagingModifiers($parts[2], $null);
		}
	
		$return_type_clean = $parts[2];
		$return_type_pointers = preg_replace("![^*]+!e", "", $return_type_clean);
		$return_type_clean = trim($return_type_clean,"* 	");

		// perform preformatting before attempting to protect keywords
		$parts[2] = $this->FormatObjcType($parts[2], $modifiers);
		$parts[4] = $this->FormatObjcParams($parts[4], $variable_arguments);
		//print($parts[4]."\n");
		
		if ($has_params) {
			$name = $this->ConvertObjcMethodName($source);

			// merge default protected keywords for the class/category
			if ($this->default_protected["*"]) $protected_keywords = array_merge($this->default_protected["*"], $protected_keywords);
			if ($this->default_protected[$class]) $protected_keywords = array_merge($this->default_protected[$class], $protected_keywords);
			
			
			$param_array = $this->ConvertObjcParamsToPascal($parts[4], $protected_keywords);
			$params = "(".$param_array["string"].")";
			$params_with_modifiers = "(".$param_array["string_with_modifiers"].")";
			
		} else {
			$params = "";
			$params_with_modifiers = "";
			// no parameters -> definitely no underscore normally, but there are some
			// conflicts...
			$name = $parts[3];
			// clean it up
			if ($this->trailing_underscore) {
				if (in_array($source, $this->trailing_underscore_methods)) $name = $name . "_";	
			}
			$param_array = null;
			$variable_arguments = false;
		}

		// protect method name from keywords
		if ($this->IsKeywordReserved($name)) $name .= "_";
		
		// replace objc type
		$return_type = $this->ConvertReturnType($return_type_clean,$return_type_pointers);

		$virtual = "";
		$class_prefix = "";

		// determine the type based on return value
		if (ereg($this->regex_procedure_type, $return_type_clean.$return_type_pointers)) {
			$kind = "procedure";
		} else {
			$kind = "function";
		}

		// determine if this is a class method
		if ($parts[1] == "+") {
			$class_prefix = "class ";
		
			// These methods probably return the an allocated instance of the class, a typical convenience method.
			// ??? Ack! $class may be the category or protocol name
			//if ($return_type == $this->objc_id) $return_type = $class; 
		}

		// Replace SEL with the string equivalent
		if ($this->register_selectors) {
			$params_with_modifiers = str_replace_word("SEL", $this->sel_string, $params_with_modifiers);
		}

		// make method templates
		if ($kind != "function") {
			if ($variable_arguments) $modifier .= " varargs;";
		
			$method = "$class_prefix$kind $name$params_with_modifiers;$modifier$virtual";
			$method_template = "[KIND] [PREFIX]$name"."[PARAMS];$modifier";
		} else {
			if ($variable_arguments) $return_type = "$return_type; varargs";

			$method = $class_prefix."function $name$params_with_modifiers: $return_type;$modifier$virtual";
			$method_template = "[KIND] [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
		}

		$method_template_procedure = "procedure [PREFIX]$name"."[PARAMS];$modifier";
		$method_template_function = "function [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";

		// build structure
		$struct["def"] = $method;
		$struct["template"] = $method_template;
		$struct["template_function"] = $method_template_function;
		$struct["template_procedure"] = $method_template_procedure;
		$struct["objc_method"] = $this->CopyObjcMethodName($source);
		$struct["class_prefix"] = $class_prefix;
		if ($deprecatedmods != "") $struct["deprecated"] = $deprecatedmods.";";
		//$struct["def_objc"] = eregi("(.*);", $source, $captures[1]);
		if ($return_type == "void") $return_type = "";
		$struct["return"] = $return_type;
	
		if (in_array($return_type, $this->cocoa_classes)) $struct["returns_wrapper"] = true;
		$struct["param_string_clean"] = trim($params, "()");
		$struct["param_string_clean_with_modifiers"] = trim($params_with_modifiers, "()");
		$struct["param_string"] = $params;
		$struct["param_string_with_modifiers"] = $params_with_modifiers;
		$struct["param_array"] = $param_array["pairs"];
		$struct["param_list"] = $param_array["list"];
		$struct["class"] = $class;
		$struct["name"] = $name;
		$struct["kind"] = $kind;

		if ($struct["param_array"] != null) $struct["has_params"] = true;

		// FPC bug work around
		if (strlen($name) > $this->maximum_method_length) {
			$struct["can_override"] = false;
			print(" # WARNING: method $name can't override because the name is too long\n");
			$this->warning_count ++;
		}

		return $struct;
	}
	
	function InsertPatches ($header) {
		$path = "$this->root/patches/".$header["name_clean"].".patch";
		if ($handle = @fopen($path, "r")) {
			$text = ReadTextFile($path);
			$this->PrintOutput(0, $text);
			fclose($handle);
		}
	}
	
	function HeaderContainsPatch ($header) {
		if ($handle = @fopen("$this->root/patches/".$header["name_clean"].".patch", "r")) {
			fclose($handle);
			return true;
		}
	}

  function PrintGlobalClassInfo($all_classes, $defined_classes, $anon_classes) {
		// add all classes as anonymous external classes to a separate unit.
		// They will be overridden by the actual definitions in the translated
		// headers part of the main unit, but this way they can appear as record
		// field types and as callback parameters

		// open the output file if we not printing to terminal
		if (!$this->show) {
			$this->output = fopen("$this->root$this->out/AnonClassDefinitions".ucfirst($this->framework).".pas", "w+");
		}
		
		$this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");

		$date = @date("D M j G:i:s T Y");
		
		$this->PrintOutput(0, "");
		// allows parameter names conflicting with field names
		$this->PrintOutput(0, "{\$mode delphi}");
		$this->PrintOutput(0, "{\$modeswitch objectivec1}");
		// enables "external" after the semi-colon
		$this->PrintOutput(0, "{\$modeswitch cvar}");
		$this->PrintOutput(0, "");

		$this->PrintOutPut(0,"unit AnonClassDefinitions".ucfirst($this->framework).";");
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "interface");
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "type");

    foreach ($all_classes as $class)
			$this->PrintOutput(1, $class." = objcclass; external;");

		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "implementation");
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "end.");


		// Now all anonymous external classes that have no real definition to an
		// include file that is added to the main unit. This way it is possible
		// to declare variables of these types in user programs without having to
		// include the unit above will all anonymous classes (should not be used)

		// open the output file if we not printing to terminal
		if (!$this->show) {
			$this->output = fopen("$this->root$this->out/$this->framework/AnonIncludeClassDefinitions".ucfirst($this->framework).".inc", "w+");
		}
		
		$this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");

		$date = @date("D M j G:i:s T Y");
		

		// add all classes as anonymous external classes. They will be overridden
		// by the actual definitions in the translated headers, but this way they
		// can appear as record field types and as callback parameters
		$first = true;
    foreach ($anon_classes as $class) {
    	if (!in_array($class,$defined_classes)) {
    		if ($first) {
    			$this->PrintOutput(0, "type");
    			$first = false;
    		}
				$this->PrintOutput(1, $class." = objcclass; external;");
			}
		}
  }

	
	// Prints all classes from the header in Objective-P FPC format
	function PrintHeader ($header) {
		global $version;
		//print_r($header);
		//print_r($this->dump["categories"]);
		
		// open the output file if we not printing to terminal
		if (!$this->show) {
			if ($this->merge_headers) {
				$this->output = fopen($header["path_merge"], "w+");
			} else {
				$this->output = fopen($header["path"], "w+");
			}
		}

		$this->PrintOutput(0, "{ Parsed from ".ucfirst($header["framework"]).".framework ".$header["name"]." }");

		$date = @date("D M j G:i:s T Y");
		$this->PrintOutput(0, "{ Version: $version - $date }");
		$this->PrintOutput(0, "");

		$macro = strtoupper(substr($header["name"], 0, (strripos($header["name"], "."))));
		
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$ifdef TYPES}");
		$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_T}");
		$this->PrintOutput(0, "{\$define $macro"."_PAS_T}");
		$this->PrintTypes($header, false);
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$endif}");
		$this->PrintOutput(0, "{\$endif}");

		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$ifdef RECORDS}");
		$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_R}");
		$this->PrintOutput(0, "{\$define $macro"."_PAS_R}");
		
		// Records from types
		$this->PrintRecords($header);
		
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$endif}");
		$this->PrintOutput(0, "{\$endif}");

		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$ifdef FUNCTIONS}");
		$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_F}");
		$this->PrintOutput(0, "{\$define $macro"."_PAS_F}");
		$this->PrintFunctions($header);
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$endif}");
		$this->PrintOutput(0, "{\$endif}");

		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$ifdef EXTERNAL_SYMBOLS}");
		$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_S}");
		$this->PrintOutput(0, "{\$define $macro"."_PAS_S}");
		$this->PrintExternalSymbols($header);
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{\$endif}");
		$this->PrintOutput(0, "{\$endif}");
		
		// insert user patches
		if ($this->HeaderContainsPatch($header)) {
			$this->PrintOutput(0, "");
			$this->PrintOutput(0, "{\$ifdef USER_PATCHES}");
			//$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_PATCH}");
			//$this->PrintOutput(0, "{\$define $macro"."_PAS_PATCH}");
			$this->InsertPatches($header);
			$this->PrintOutput(0, "");
			//$this->PrintOutput(0, "{\$endif}");
			$this->PrintOutput(0, "{\$endif}");
		}

		if (($header["classes"]) || ($header["protocols"])) {
			$this->PrintOutput(0, "");
			$this->PrintOutput(0, "{\$ifdef FORWARD}");

			if ($header["protocols"]) {
				foreach ($header["protocols"] as $protocol) $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol;");
			}
			
			if ($header["classes"]) {
				foreach ($header["classes"] as $class) {
					if ($class["name"]) {
						$this->PrintOutput(1, $class["name"]." = objcclass;");
						$this->PrintOutput(1, $class["name"].$this->class_pointer_suffix." = ^".$class["name"].";");
						// for consistency also offer Ptr-name variant
						$this->PrintOutput(1, $class["name"]."Ptr = ".$class["name"].$this->class_pointer_suffix.";");
					}
				}
			}
			$this->PrintOutput(0, "");
			$this->PrintOutput(0, "{\$endif}");
		}

		if ($header["classes"]) {
			$this->PrintOutput(0, "");
			$this->PrintOutput(0, "{\$ifdef CLASSES}");
			$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_C}");
			$this->PrintOutput(0, "{\$define $macro"."_PAS_C}");

			foreach ($header["classes"] as $class) {
				if ($class["name"]) $this->PrintClass($class);
			}

			if (count($header["categories"]) > 0) {
				foreach ($header["categories"] as $category) {
					$this->PrintCategory($class, $category);
				}
			}

			$this->PrintOutput(0, "");
			$this->PrintOutput(0, "{\$endif}");
			$this->PrintOutput(0, "{\$endif}");
		}


		if ($header["protocols"]) {
			$this->PrintOutput(0, "{\$ifdef PROTOCOLS}");
			$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_P}");
			$this->PrintOutput(0, "{\$define $macro"."_PAS_P}");

			
			foreach ($header["protocols"] as $protocol) {
				$this->PrintOutput(1, "");
				$this->PrintOutput(0, "{ ".$protocol["name"]." Protocol }");
				if ($protocol["comment"]) $this->PrintOutput(0, $protocol["comment"]);
				$this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol");

				// print methods
				if ($protocol["methods"]) {
					foreach ($protocol["methods"] as $name => $method) {
						if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
						$this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
					}
				}
				
				$this->PrintOutput(1, "end; external name '".$protocol["name"]."';");		
			}
			
			$this->PrintOutput(0, "{\$endif}");
			$this->PrintOutput(0, "{\$endif}");
		}
	}
	
	function PrintCategory ($class, $category) {

		// declare real category if external
		if ($category["external"]) {
			$new_name = " name '".$category["external_name"]."'";
		}
		
		$category_name = $category["name"].$this->category_suffix;
		
		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{ $category_name }");
		if ($category["comment"]) $this->PrintOutput(0, $category["comment"]);
		
		// print super class or protocol which the class conforms to
		$this->PrintOutput(1, "$category_name = objccategory(".$category["super"].")");
		
		// print methods
		if ($category["methods"]) {
			foreach ($category["methods"] as $method) {
				if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
				$this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
			}
		} 

		$this->PrintOutput(1, "end; external$new_name;");
	}
		
	function PrintClass ($class) {

		$this->PrintOutput(0, "");
		$this->PrintOutput(0, "{ ".$class["name"]." }");
		
		if ($class["comment"]) $this->PrintOutput(0, $class["comment"]);
		//print_r($class["methods"]);
		
		
		// print super class or protocol which the class conforms to
		if ($class["adopts"]) {
			$this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].", ".$class["adopts"].")");
		} elseif ($class["super"]) {
			$this->PrintOutput(1, $class["name"]." = objcclass(".$class["super"].")");
		}

		// print instance variables
		if ($class["ivars"]) {
			$this->PrintOutput(1, "private");
			foreach ($class["ivars"] as $ivar) {
				$this->PrintOutput(2, $ivar);
			}
		}

		// print alloc method for the class
		$this->PrintOutput(2, "");
		$this->PrintOutput(1, "public");
		$this->PrintOutput(2, "class function alloc: ".$class["name"]."; message 'alloc';");
		
		// print class-level methods
		if ($class["methods"]) {
			$this->PrintOutput(0, "");
			foreach ($class["methods"] as $method) {
				if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
				$this->PrintOutput(2, $method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
			}
		}
		
		// print adopted protocol methods
		if (count($class["protocols"]) > 0) {
			$this->PrintOutput(0, "");
			$this->PrintOutput(2, "{ Adopted Protocols }");
			//print_r($this->dump["protocols"]);

			foreach ($class["protocols"] as $name) {
				if ($this->dump["protocols"][$name]) {
					foreach ($this->dump["protocols"][$name] as $method) {
						if (!$this->ClassContainsMethod($class, $method)) $this->PrintOutput(2, $method["def"]);
					}
				}
			}
		}

		$this->PrintOutput(1, "end; external;");
	}

}
?>