Repository navigation
Expand file tree
/
Copy pathparser.php
More file actions
344 lines (295 loc) · 9.98 KB
/
Copy pathparser.php
File metadata and controls
344 lines (295 loc) · 9.98 KB
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
<?
ini_set('xdebug.var_display_max_depth', '10');
/**
* A custom JSDoc parser that extracts what we need to generate automatic
* documentation pages.
*
* Written to be compatible with the JSDoc comments and code style in the
* Isogenic Game Engine 1.1.x and may not work with other JSDoc commented
* files. IGE 1.1.0 uses a class-based pattern and standard JSDoc parsers
* seem to have a problem understanding them so this was written. It is
* probably not the fastest, cleanest way to achieve the required result
* but it *works* - that's all that matters here!
*
* @author Rob Evans (rob@irrelon.com)
* @param $path {String} The path to the file to parse.
* @return {Array} A multi-dimension associative array parser result data.
*/
function parseFile($path) {
// Read javascript file contents
$code = file_get_contents($path);
// Check we got some code!
if ($code) {
// Get all comments and the code beneath them
preg_match_all("/\/\*\*([\s\S]*?)\*\/([\s\S]*?){/", $code, $matches);
foreach ($matches[1] as $key => $value) {
unset($item);
unset($parameters);
$codeLine = $matches[2][$key];
if (strstr($codeLine, 'var IgeClass = (function () ')) {
$codeLine = str_replace('var IgeClass = (function () ', 'var IgeClass = none.extend(', $codeLine);
}
//echo "<B>Comment</B>: " . var_dump($value) . '<BR>';
//echo "<B>Code</B>:" . $matches[2][$key] . '<BR><BR>';
// Get the description from the comment
preg_match_all("/\*\s(.*?)\n/", $value, $descriptionLines);
// Strip and concat the description lines
$description = '';
foreach ($descriptionLines[1] as $descripKey => $descripText) {
// Continue adding lines until we reach an @ marker
if (substr($descripText, 0, 1) === '@') {
break;
}
$description .= trim($descripText) . ' ';
}
$description = trim($description);
// Grab the parameter markers
reset($descriptionLines);
$itemText = '';
$returnText = '';
$parametersArray = Array();
$exampleArray = Array();
$privateMethod = false;
$constructorMethod = false;
$mode = '';
foreach ($descriptionLines[1] as $descripKey => $descripText) {
if (substr($descripText, 0, 1) === '@') {
// Check if we are already in a mode and if so, store the entry
// for it because we have found a new entry to process
if ($mode == '@param') {
$parametersArray[] = $itemText;
$itemText = '';
$mode = '';
}
if ($mode == '@return') {
$returnText = $itemText;
$itemText = '';
$mode = '';
}
if ($mode == '@example') {
$exampleArray[] = str_replace('@example ', '', $itemText);
$itemText = '';
$mode = '';
}
if (substr($descripText, 0, 6) === '@param') {
$mode = '@param';
$itemText = trim($descripText);
}
if (substr($descripText, 0, 7) === '@return') {
$mode = '@return';
$itemText = trim($descripText);
}
if (substr($descripText, 0, 8) === '@example') {
$mode = '@example';
$itemText = $descripText;
}
if (substr($descripText, 0, 8) === '@private') {
$privateMethod = true;
$mode = '';
}
if (substr($descripText, 0, 12) === '@constructor') {
$constructorMethod = true;
$mode = '';
}
} else {
// Handle new lines
if ($mode) {
switch ($mode) {
case '@example':
$descripText = str_replace("\t", '<!TT>', $descripText);
$descripText = str_replace(" ", '<!TT>', $descripText);
$descripText = str_replace("\n", '<!BN>', $descripText);
$descripText = str_replace("\r", '<!BR>', $descripText);
$descripText = trim($descripText);
$descripText = str_replace('<!TT>', "\t", $descripText);
$descripText = str_replace('<!BN>', "\n", $descripText);
$descripText = str_replace('<!BR>', "\r", $descripText);
$itemText .= $descripText;
break;
default:
if ($itemText) {
$itemText .= ' ';
}
$itemText .= trim($descripText);
break;
}
}
}
}
if ($mode == '@param') {
$parametersArray[] = $itemText;
}
if ($mode == '@return') {
$returnText = $itemText;
}
if ($mode == '@example') {
$exampleArray[] = str_replace('@example ', '', $itemText);
}
// Extract type, name and description from parameter line
foreach ($parametersArray as $paramKey => $paramVal) {
// Check for type
preg_match("/\{(.*?)\}/", $paramVal, $paramType);
unset($param);
$param['type'] = $paramType[1];
$param['optional'] = false;
$param['name'] = '';
$param['desc'] = '';
if ($param['type']) {
// Check if the parameter is optional
if (substr($param['type'], strlen($param['type']) - 1, 1) === '=') {
// The parameter is optional, remove the = from the type name
$param['type'] = substr($param['type'], 0, strlen($param['type']) - 1);
$param['optional'] = true;
} else {
$param['optional'] = false;
}
// Extract the parameter name
preg_match("/\}\s(.*?)$/", $paramVal, $paramLine);
// Check if more than one word exists in the line
if (strstr($paramLine[1], ' ')) {
// Split the text by space
$textSplit = explode(' ', $paramLine[1]);
$param['name'] = trim($textSplit[0], ' ');
foreach ($textSplit as $paramDescKey => $paramDescVal) {
if ($paramDescKey !== 0) {
$param['desc'] .= $paramDescVal . ' ';
}
}
$param['desc'] = trim($param['desc']);
} else {
$param['name'] = $paramLine[1];
}
} else {
// Extract the parameter name
preg_match("/\s(.*?)$/", $paramVal, $paramLine);
// Check if more than one word exists in the line
if (strstr($paramLine[1], ' ')) {
// Split the text by space
$textSplit = explode(' ', $paramLine[1]);
$param['name'] = trim($textSplit[0], ' ');
foreach ($textSplit as $paramDescKey => $paramDescVal) {
if ($paramDescKey !== 0) {
$param['desc'] .= $paramDescVal . ' ';
}
}
$param['desc'] = trim($param['desc']);
} else {
$param['name'] = $paramLine[1];
}
}
/*echo "Parameter <B>" . $param['name'] . "</B> {" . $param['type'] . "}";
if ($param['optional']) { echo " (*Optional*)"; }
if ($param['desc']) { echo " " . $param['desc']; }
echo "<BR>";*/
$parameters[] = $param;
}
// Extract type and description from return line
if ($returnText) {
// Check for type
preg_match("/\{(.*?)\}/", $returnText, $paramType);
unset($param);
$param['type'] = $paramType[1];
$param['optional'] = false;
$param['desc'] = '';
if ($param['type']) {
// Check if the parameter is optional
if (substr($param['type'], strlen($param['type']) - 1, 1) === '=') {
// The parameter is optional, remove the = from the type name
$param['type'] = substr($param['type'], 0, strlen($param['type']) - 1);
$param['optional'] = true;
} else {
$param['optional'] = false;
}
// Extract the description
preg_match("/\}\s(.*?)$/", $returnText, $paramLine);
$param['desc'] = trim($paramLine[1]);
} else {
// Extract the description
preg_match("/\}\s(.*?)$/", $returnText, $paramLine);
$param['desc'] = trim($paramLine[1]);
}
/*echo "RETURNSTUFF <B>" . $param['name'] . "</B> {" . $param['type'] . "}";
if ($param['optional']) { echo " (*Optional*)"; }
if ($param['desc']) { echo " " . $param['desc']; }
echo "<BR>";*/
$returnData = $param;
//$returnData['original'] = $returnText;
} else {
$returnData = null;
}
// Check if the code is a class that is extending another
preg_match_all("/=\s(.*?)\.extend/", $codeLine, $extendedClass);
$itemType = '';
if ($extendedClass[1]) {
$extendedClass = $extendedClass[1][0];
$itemType = 'class';
// Extract the class name
preg_match("/(.*?)\s=/", $codeLine, $classNameArr);
$className = trim(str_replace('var ', '', $classNameArr[1]));
/*echo "Extending from " . $extendedClass . '<BR><BR>';*/
} else {
$extendedClass = '';
$arguments = Array();
// Check if it is a object property-style function declaration
if (strstr($codeLine, ': function')) {
$itemType = 'function';
preg_match("/(.*?):\s*?function\s*?\((.*?)\)/", $codeLine, $argArray);
}
// Check if it is a function-style declaration
if (strstr($codeLine, '= function')) {
$itemType = 'function';
preg_match("/(.*?)=\s*?function\s*?\((.*?)\)/", $codeLine, $argArray);
}
if (strstr($codeLine, '= (function')) {
$itemType = 'function';
preg_match("/(.*?)=\s*?\(function\s*?\((.*?)\)/", $codeLine, $argArray);
}
if ($itemType === 'function') {
if ($argArray[1]) {
$functionName = trim($argArray[1]);
}
if ($argArray[2]) {
$argArray[2] = str_replace(' ', '', $argArray[2]);
$arguments = explode(',', $argArray[2]);
}
/*echo "<B>Function</B>: " . $functionName . '<BR>';
foreach($arguments as $argKey => $argValue) {
echo "<B>Argument</B>: " . $argValue . '<BR>';
}
echo "<B>Desc</B>: " . $description . '<BR><BR>';*/
}
}
// Add the details to the data array
switch ($itemType) {
case 'class':
$item['type'] = 'class';
$item['name'] = $className;
$item['extends'] = $extendedClass != 'none' ? $extendedClass : '';
break;
case 'function':
$item['type'] = 'function';
$item['name'] = $functionName;
$item['params'] = $parameters;
$item['arguments'] = $arguments;
$item['private'] = $privateMethod;
$item['constructor'] = $constructorMethod;
if ($returnData) {
$item['returnData'] = $returnData;
}
if ($exampleArray) {
$item['examples'] = $exampleArray;
}
break;
}
// Add shared data
$item['desc'] = $description;
// If the item was assigned a type (we recognise it), add it to the items array
if ($item['type']) {
$docItem[] = $item;
}
}
return $docItem;
}
return null;
}
?>