summaryrefslogtreecommitdiff
path: root/tools/fmt-luadoc.php
blob: b51d7e7e86df5d992ed29e2feeb01855edd71cc1 (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
#!/usr/bin/php
<?php

$json = file_get_contents('../doc/luadoc.json');
$doc = array();
foreach (json_decode($json, true) as $b) {
	if (!isset ($b['type'])) { continue;}
	$doc[] = $b;
}

################################################################################

$classes = array();
$consts = array();
$constslist = array();
$funclist = array();
$classlist = array();

################################################################################

function my_die ($msg) {
	fwrite(STDERR,$msg."\n");
	exit(1);
}

##function ptr_strip ($ctype) {
#	# boost::shared_ptr<std::list<boost::shared_ptr<ARDOUR::Route>> > >
#	# -> std::list<ARDOUR::Route>
#	$ctype = preg_replace ('/boost::shared_ptr<([^>]*)[ ]*>/', '$1', $ctype);
#	return preg_replace ('/boost::shared_ptr<([^>]*)[ ]*>/', '$1', $ctype);
#}

function arg2lua ($argtype) {
	global $classes;
	global $consts;

	# LuaBridge abstracts C++ references
	$arg = preg_replace ('/&$/', '', $argtype);

	# filter out basic types
	$builtin = array ('float', 'double', 'bool', 'std::string', 'int', 'long', 'unsigned long', 'unsigned int', 'unsigned char', 'char', 'void', 'char*', 'unsigned char*', 'void*');
	if (in_array ($arg, $builtin)) return $argtype;

	# check Class declarations first
	foreach (array_merge ($classes, $consts) as $b) {
		if ($b['decl'] == $arg) {
			return $b['lua'];
		}
	}

	# strip class pointers -- TODO Check C'tor for given class
	$arg = preg_replace ('/[&*]*$/', '', $argtype);
	foreach (array_merge ($classes, $consts) as $b) {
		if ($b['decl'] == $arg) {
			return $b['lua'];
		}
	}
	return '--MISSING (' . $argtype . ')--';
}

function stripclass ($classname, $name) {
	$classname .= ':';
	if (strpos($name, $classname) !== 0) {
		my_die ('invalid class prefix' .$classname. ' -- '. $name);
	}
	return substr ($name, strlen ($classname));
}

function datatype ($decl) {
	# TODO handle spaces in type. Works because
	# we don't yet have templated types (with_space <here >)
	return substr($decl, 0, strpos ($decl, ' '));
}

function luafn2class ($lua) {
	return substr($lua, 0, strrpos ($lua, ':'));
}

function checkclass ($b) {
	global $classlist;
	if (!isset ($classlist[luafn2class($b['lua'])])) {
		my_die ('MISSING CLASS FOR '. print_r($b['lua'], true));
	}
}

# parse functions argument list to lua-names
function decl2args ($decl) {
	$start = strrpos ($decl, '(');
	$end = strrpos ($decl, ')');
	$args = substr ($decl, $start + 1, $end - $start - 1);
	$arglist = preg_split ('/, */', $args);
	$rv = array ();
	foreach ($arglist as $a) {
		if (empty ($a)) { continue; }
		$rv[] = arg2lua($a);
	}
	return $rv;
}

################################################################################
# step 1: build class indices

foreach ($doc as $b) {
	if (strpos ($b['type'], "[C] ") === 0) {
		$classes[] = $b;
		$classlist[$b['lua']] = array ();
		$classindex[$b['lua']] = $b;
		$classdecl[$b['decl']] = $b;
	}
}
foreach ($classes as $c) {
	if (isset ($c['parent'])) {
		if (isset ($classdecl[$c['parent']])) {
			$classindex[$c['lua']]['luaparent'] = $classdecl[$c['parent']]['lua'];
		} else {
			my_die ('unknown parent class: ' . print_r($c, true));
		}
	}
}

# step 2: extract constants/enum
foreach ($doc as $b) {
	switch ($b['type']) {
	case "Constant/Enum":
	case "Constant/Enum Member":
		$ns = luafn2class($b['lua']);
		$constlist[$ns][] = $b;
		if (strpos ($b['decl'], '::') === false) {
			# for extern c enums, use the Lua Namespace
			$b['decl'] = str_replace (':', '::', luafn2class($b['lua']));
		}
		$b['lua'] = $ns;
		$consts[] = $b;
		break;
	default:
		break;
	}
}

# step 3: process functions
foreach ($doc as $b) {
	switch ($b['type']) {
	case "Constructor":
	case "Weak/Shared Pointer Constructor":
		checkclass ($b);
		$classlist[luafn2class($b['lua'])]['ctor'][] = array (
			'name' => luafn2class($b['lua']),
			'args' => decl2args ($b['decl']),
		);
		break;
	case "Data Member":
		checkclass ($b);
		$classlist[luafn2class($b['lua'])]['data'][] = array (
			'name' => $b['lua'],
			'ret'  => arg2lua (datatype($b['decl']))
		);
		break;
	case "C Function":
		# we required C functions to be in a class namespace
	case "Ext C Function":
		checkclass ($b);
		$classlist[luafn2class($b['lua'])]['func'][] = array (
			'name' => $b['lua'],
			'args' => array ('--custom--'), // XXX
			'ret' => '(LUA)', // XXX
			'ext'  => true
		);
		break;
	case "Free Function":
	case "Free Function RefReturn":
		$funclist[luafn2class($b['lua'])][] = array (
			'name' => $b['lua'],
			'args' => decl2args ($b['decl']),
			'ret'  => arg2lua ($b['ret']),
			'ref'  => (strpos($b['type'], "RefReturn") !== false)
		);
		break;
	case "Member Function":
	case "Member Pointer Function":
	case "Weak/Shared Pointer Function":
	case "Weak/Shared Pointer Function RefReturn":
	case "Weak/Shared Null Check":
	case "Weak/Shared Pointer Cast":
	case "Static Member Function":
		checkclass ($b);
		$classlist[luafn2class($b['lua'])]['func'][] = array (
			'name' => $b['lua'],
			'args' => decl2args ($b['decl']),
			'ret'  => arg2lua ($b['ret']),
			'ref'  => (strpos($b['type'], "RefReturn") !== false)
		);
		#print_r(decl2args ($b['decl']));
		#echo arg2lua ($b['ret'])."\n";
		break;
	case "Constant/Enum":
	case "Constant/Enum Member":
		# already handled -> $consts
		break;
	default:
		if (strpos ($b['type'], "[C] ") !== 0) {
			my_die ('unhandled type: ' . $b['type']);
		}
		break;
	}
}


## TODO...

# step 4a: unify weak/shared Ptr classes
# step 4b: group namespaces (class, enums)

## list of possible functions producing the given type
## -> see also arg2lua()
#function arg2src ($argtype) {
#	global $classes;
#	global $consts;
#	$rv = array ();
#	# filter out basic types
#	$builtin = array ('float', 'double', 'bool', 'std::string', 'int');
#	if (in_array ($builtin)) return $rv;
#
#	# check class c'tors end enums
#	foreach (array_merge ($classes, $consts) as $b) {
#		if (strpos ($b['decl'], $argtype) === 0) {
#			$rv[$b['lua']] = $b;
#		}
#	}
#	# check C++ declarations next
#	foreach ($doc as $b) {
#		if (isset($b['ret']) && $b['ret'] == $argtype) {
#			$rv[$b['lua']] = $b;
#		}
#		if (strpos ($b['decl'], $argtype) === 0) {
#			$rv[$b['lua']] = $b;
#		}
#	}
#	# check lua-name for extern c enums
#	$argtype = str_replace ('::', ':', $argtype);
#	foreach ($doc as $b) {
#		if (strpos ($b['lua'], $argtype) === 0) {
#			$rv[$b['lua']] = $b;
#		}
#	}
#	return $rv;
#}

# step 5: output
define ('NL', "\n");

echo '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'.NL;
echo '<body>';

function format_args ($args) {
	$rv = ' (';
	$first = true;
	foreach ($args as $a) {
		if (!$first) { $rv .= ', '; }
		$rv .= '<em>'.$a.'</em>';
		$first = false;
	}
	$rv .= ')';
	return $rv;
}

function ctorname ($name) {
	return str_replace (':', '.', $name);
}

foreach ($classlist as $ns => $cl) {
	echo '<h2 id="'.$ns.'">'.$ns.'</h2>'.NL;
	if (isset ($classindex[$ns]['luaparent'])) {
		echo ' <p>is-a <a href="#'.$classindex[$ns]['luaparent'].'">'.$classindex[$ns]['luaparent'].'</a></p>'.NL;
	}
	// TODO highlight Pointer Classes

	// TODO optionally traverse all parent classes..
	// function format_class_members()
	if (isset ($cl['ctor'])) {
		echo ' <h3>Constructor</h3>'.NL;
		echo ' <ul>'.NL;
		foreach ($cl['ctor'] as $f) {
			echo '  <li>'.ctorname($f['name']).format_args($f['args']).'</li>'.NL;
		}
		echo ' </ul>'.NL;
	}
	if (isset ($cl['func'])) {
		echo ' <h3>Methods</h3>'.NL;
		echo ' <ul>'.NL;
		foreach ($cl['func'] as $f) {
			echo '  <li>'.$f['ret'].' '.stripclass($ns, $f['name']).format_args($f['args']).'</li>'.NL;
		}
		echo ' </ul>'.NL;
	}
	if (isset ($cl['data'])) {
		echo ' <h3>Data</h3>'.NL;
		echo ' <ul>'.NL;
		foreach ($cl['data'] as $f) {
			echo '  <li>'.stripclass($ns, $f['name']).'</li>'.NL;
		}
		echo ' </ul>'.NL;
	}
}

foreach ($funclist as $ns => $fl) {
	echo '<h2>'.$ns.'</h2>'.NL;
	echo ' <ul>'.NL;
	foreach ($fl as $f) {
		echo '  <li>'.$f['ret'].' '.stripclass($ns, $f['name']).format_args($f['args']).'</li>'.NL;
	}
	echo ' </ul>'.NL;
}

echo "</body>\n</html>\n";