PageRenderTime 30ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/javascript/dojo-release-1.6.1-src/util/docscripts/lib/parser2/dojo2.inc

https://code.google.com/p/realitybuilder/
Pascal | 442 lines | 158 code | 40 blank | 244 comment | 40 complexity | 634b83a17986526661f3aaa49f3958a3 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. ini_set('display_errors', 1);
  3. error_reporting(E_ALL ^ E_NOTICE);
  4. require_once('JavaScriptLanguage.php');
  5. require_once('JavaScriptParser.php');
  6. require_once('JavaScriptStatements.php');
  7. require_once('JavaScriptFunction.php');
  8. require_once('Dojo.php');
  9. require_once('DojoCommentBlock.php');
  10. $_dojo_properties_modules = array();
  11. function _dojo_get_namespaces($limit=null){
  12. static $namespaces;
  13. if (!isset($namespaces)) {
  14. $namespaces = array();
  15. $files = scandir('modules');
  16. foreach ($files as $file) {
  17. if (substr($file, -7) == '.module') {
  18. $namespace = substr($file, 0, -7);
  19. if (!$limit || in_array($namespace, $limit)) {
  20. include_once('modules/' . $file);
  21. $namespaces[] = substr($file, 0, -7);
  22. }
  23. }
  24. elseif (substr($file, -18) == '.module.properties') {
  25. $namespace = substr($file, 0, -18);
  26. if (!$limit || in_array($namespace, $limit)) {
  27. global $_dojo_properties_modules;
  28. foreach (preg_split('%[\n\r]+%', file_get_contents('modules/' . $file)) as $line) {
  29. list($line, ) = preg_split('%[!#]%', $line, 2);
  30. if ($line = trim($line)) {
  31. list($key, $line) = explode('=', $line, 2);
  32. $key = str_replace('\\ ', ' ', trim($key));
  33. $line = preg_replace('%^\s+%', '', $line);
  34. if ($key == 'location') {
  35. $line = _dojo_ensure_directory($line);
  36. }
  37. $_dojo_properties_modules[$namespace][$key] = $line;
  38. }
  39. }
  40. $namespaces[] = substr($file, 0, -18);
  41. }
  42. }
  43. }
  44. }
  45. return $namespaces;
  46. }
  47. function dojo_get_include($node, $provide) {
  48. if ($node->jsdoc_project_name == $provide->title) {
  49. return 'Included automatically';
  50. }
  51. else {
  52. return 'dojo.require("%s");';
  53. }
  54. }
  55. function _dojo_ensure_directory($directory) {
  56. if (!is_dir($directory)) {
  57. die("$directory is not a directory\n");
  58. }
  59. else {
  60. if(substr($directory, -1) != '/'){
  61. $directory .= '/';
  62. }
  63. }
  64. return $directory;
  65. }
  66. function dojo_get_file_time($namespace, $file) {
  67. if (function_exists($namespace . '_code_location')) {
  68. return filectime(_dojo_ensure_directory(call_user_func($namespace . '_code_location')) . $file);
  69. }
  70. else {
  71. global $_dojo_properties_modules;
  72. return filectime($_dojo_properties_modules[$namespace]['location'] . $file);
  73. }
  74. }
  75. function _jsdoc_file_list($dir = false, $recurse = false){
  76. $output = array();
  77. if (!$recurse) {
  78. $old_dir = getcwd();
  79. if (!is_dir($dir)) {
  80. return array();
  81. }
  82. chdir($dir);
  83. $dir = '.';
  84. }
  85. $files = scandir($dir);
  86. foreach ($files as $file) {
  87. if ($file{0} == '.') continue;
  88. if (is_dir($dir . '/' . $file)) {
  89. if ($recurse) {
  90. $file = $dir . '/' . $file;
  91. }
  92. $output = array_merge($output, _jsdoc_file_list($file, true));
  93. }else{
  94. if (substr($file, -3) == '.js' && substr($file, -6) != '.xd.js') {
  95. if ($recurse) {
  96. $file = $dir . '/' . $file;
  97. }
  98. $output[] = $file;
  99. }
  100. }
  101. }
  102. if (!$recurse) {
  103. chdir($old_dir);
  104. }
  105. return $output;
  106. }
  107. function dojo_get_files($limit=null) {
  108. $namespaces = _dojo_get_namespaces($limit);
  109. $files = array();
  110. foreach ($namespaces as $namespace) {
  111. if (function_exists($namespace . '_code_location')) {
  112. $location = _dojo_ensure_directory(call_user_func($namespace . '_code_location'));
  113. }
  114. else {
  115. global $_dojo_properties_modules;
  116. $location = $_dojo_properties_modules[$namespace]['location'];
  117. }
  118. if (!$location) die($namespace . '_code_location does not return useful result');
  119. $list = _jsdoc_file_list($location);
  120. foreach ($list as $i => $item) {
  121. // Skip internationalization/tests/demos files
  122. if (preg_match('%(^|/|\\\\)(nls|tests|demos)(\\\\|/)%', $item)) {
  123. unset($list[$i]);
  124. continue;
  125. }
  126. $list[$i] = array($namespace, $item);
  127. }
  128. $files = array_merge($files, array_values($list));
  129. }
  130. return $files;
  131. }
  132. function _slashtoname($thing){
  133. return str_replace("/", ".", $thing);
  134. }
  135. function _amd_unwrap($text){
  136. $re = '/\/\/\s*AMD\-ID\s*"([^\n]+)"/i';
  137. $bre = '/define\(([^\]]+)\]\s*\,[\s\n]*function.+/';
  138. $depre = '/^[\'"]([-_a-zA-Z\/]+)[\'"]\s*,\s*\[(.*)$/';
  139. preg_match($re, $text, $foo);
  140. $anonmoduleid = $foo[1];
  141. // either speciam AMD-ID string found, or file startswith("define(")
  142. if($anonmoduleid || substr($text, 0, 8) == "define(\""){
  143. preg_match($bre, $text, $matches);
  144. preg_match($depre, $matches[1], $parts);
  145. $provide = _slashtoname($anonmoduleid ? $anonmoduleid : $parts[1]);
  146. preg_match_all('/[\'"]([\w\/-_\!]+)[\'"],?/', $anonmoduleid ? $matches[0] : $parts[2], $deps);
  147. $requires = $deps[1];
  148. $prefixtext = 'dojo.provide("' . $provide . '");';
  149. $prefixtext .= "\n";
  150. preg_match_all('/[\'"]([\w\/-_\!]+)[\'"],?/', $parts[2], $deps);
  151. if(is_array($requires)){
  152. foreach($requires as $dep){
  153. $firstbit = substr($dep, 0, 5);
  154. if($firstbit == "text!"){
  155. // do nothing
  156. }else if($firstbit == "i18n!"){
  157. preg_match('/i18n\!(.+)\/nls\/(\w+)/', $dep, $bundlebits);
  158. $prefixtext .= 'dojo.requireLocalization("' . $bundlebits[1] . '", "' . $bundlebits[2] . '");';
  159. $prefixtext .= "\n";
  160. }else if($dep != "dojo" && $dep != "dijit" && $dep != "dojox"){
  161. $prefixtext .= 'dojo.require("' . _slashtoname($dep) . '");';
  162. $prefixtext .= "\n";
  163. }
  164. }
  165. }
  166. // strip out "AMD-result" comments? not sure what these are.
  167. $text = preg_replace($bre, "", $text);
  168. $text = preg_replace('/\s*(return\s+[_a-zA-Z\.0-9]+\s*;\s)?(\/\/.+)?\s*\}\);\s*$/', "", $text);
  169. $text = $prefixtext . $text;
  170. }
  171. return $text;
  172. }
  173. function dojo_get_contents($namespace, $file_name) {
  174. if (function_exists($namespace . '_code_location')) {
  175. $location = _dojo_ensure_directory(call_user_func($namespace . '_code_location'));
  176. }
  177. else {
  178. global $_dojo_properties_modules;
  179. $location = $_dojo_properties_modules[$namespace]['location'];
  180. }
  181. $filedata = file_get_contents($location . '/' . $file_name);
  182. $filedata = _amd_unwrap($filedata);
  183. $lines = preg_replace('%/\*={3,}|={3,}\*/%', '', $filedata);
  184. $parser = new JavaScriptParser(JavaScriptLanguage::tokenize($lines));
  185. // print '<pre>';
  186. // $statements = $parser->statements();
  187. // print $statements[0]->resolve();
  188. // print '</pre>';
  189. // die();
  190. $package = new JavaScriptStatements($parser->statements());
  191. $output = array();
  192. // Handle dojo.provide calls
  193. foreach ($package->function_calls(TRUE, 'dojo.provide') as $call) {
  194. if ($module = $call->arguments()->getString(0)) {
  195. $output['#provides'] = $module;
  196. }
  197. }
  198. $output['#resource'] = $file_name;
  199. // Handle dojo.require calls
  200. foreach ($package->function_calls(TRUE, 'dojo.require') as $call) {
  201. if ($module = $call->arguments()->getString(0)) {
  202. $output['#requires'][] = array('common', $module);
  203. }
  204. }
  205. // Handle mixin/extend calls
  206. foreach ($package->function_calls(TRUE, 'dojo.mixin', 'dojo.extend') as $call) {
  207. $arguments = $call->arguments();
  208. $assignment = $call->assignment();
  209. $root = NULL;
  210. if ($constructor = $arguments->getFunction(0)) {
  211. if ($assignment) {
  212. Dojo::roll_out($constructor, $assignment, FALSE, $output);
  213. }
  214. }
  215. else {
  216. $root = $arguments->getVariable(0, TRUE);
  217. if ($call->name() == 'dojo.extend') {
  218. $output[$root]['type'] = 'Function';
  219. }
  220. }
  221. foreach (array_diff(array_unique(array($assignment, $root)), array(NULL)) as $root) {
  222. $mixin = $call->name() == 'dojo.mixin';
  223. for ($i = 1; $i < $arguments->length; $i++) {
  224. if ($arguments->getObject($i)) {
  225. $keys = array();
  226. foreach ($arguments->getObject($i)->values() as $key => $values) {
  227. $keys[] = $key;
  228. $full_name = "$root.$key";
  229. foreach ($values as $value) {
  230. if ($value instanceof JavaScriptVariable) {
  231. $key = $mixin ? $full_name : "$root.prototype.$key";
  232. if ($key != $value->value()) {
  233. $output[$key]['alias'] = $value->value();
  234. }
  235. }
  236. else {
  237. Dojo::roll_out($value, $full_name, FALSE, $output);
  238. $output[$full_name][$mixin ? 'attached' : 'prototype'] = $root;
  239. }
  240. }
  241. }
  242. Dojo::roll_out_comment_block($arguments->getObject($i), $root, $output, $keys);
  243. }
  244. elseif ($root && $full_name = $arguments->getVariable($i)) {
  245. if ($mixin) {
  246. $output[$root]['mixins']['normal'][] = $full_name;
  247. }
  248. else {
  249. $output[$root]['chains']['prototype'][] = $full_name;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. foreach ($package->function_calls(TRUE, 'dojo.declare') as $call) {
  256. $arguments = $call->arguments();
  257. $name = $arguments->getString(0);
  258. if (!$name) {
  259. continue;
  260. }
  261. $output[$name]['type'] = 'Function';
  262. if ($superclass = $arguments->getVariable(1)) {
  263. if ($superclass != 'null') {
  264. $output[$name]['chains']['prototype'][] = $superclass;
  265. $output[$name]['chains']['call'][] = $superclass;
  266. }
  267. }
  268. elseif ($superclasses = $arguments->getArray(1)) {
  269. for($i = 0; TRUE; $i++) {
  270. if ($superclass = $superclasses->getVariable($i)) {
  271. $output[$name]['chains']['prototype'][] = $superclass . ($i ? '.prototype' : '');
  272. $output[$name]['chains']['call'][] = $superclass;
  273. }
  274. else {
  275. break;
  276. }
  277. }
  278. }
  279. if ($mixin = $arguments->getObject(2)) {
  280. $keys = $block_keys = Dojo::$block_keys;
  281. $new_keys = array();
  282. $constructors = array();
  283. // Remember that bad code can have multiple matching keys
  284. foreach ($mixin->values() as $key => $values) {
  285. $new_keys[] = $key;
  286. $full_name = "$name.$key";
  287. foreach ($values as $value) {
  288. if ($value instanceof JavaScriptFunction) {
  289. if (in_array($key, array('constructor', 'preamble', 'postscript'))) {
  290. $output[$full_name]['constructor'] = $key;
  291. $output[$full_name]['prototype'] = $name;
  292. $constructors[$full_name] = $value;
  293. continue;
  294. }
  295. }
  296. elseif ($value->type() == 'variable') {
  297. if ($full_name != $value->value()) {
  298. $output[$full_name]['alias'] = $value->value();
  299. }
  300. continue;
  301. }
  302. $output[$full_name]['prototype'] = $name;
  303. $new_keys = array_unique(array_merge($new_keys, Dojo::roll_out($value, $full_name, FALSE, $output, $new_keys)));
  304. }
  305. }
  306. foreach ($constructors as $full_name => $constructor) {
  307. $new_keys = array_unique(array_merge($new_keys, Dojo::roll_out($constructor, $name, FALSE, $output, $new_keys)));
  308. foreach ($output[$name] as $key => $value) {
  309. if ($key != 'chains') {
  310. $output[$full_name][$key] = $value;
  311. }
  312. }
  313. }
  314. Dojo::roll_out_comment_block($mixin, $name, $output, $new_keys);
  315. }
  316. }
  317. // Variable assignments (global)
  318. foreach ($package->assignments(TRUE) as $variable) {
  319. foreach ($variable->names() as $name) {
  320. $parts = explode('.', $name);
  321. $name = implode('.', array_diff($parts, array('prototype')));
  322. $last = array_pop($parts);
  323. $is_prototype = ($last == 'prototype');
  324. Dojo::roll_out($variable->value(), $name, FALSE, $output, array(), $is_prototype);
  325. if (count($parts) && !$is_prototype) {
  326. $output[$name]['attached'] = implode('.', $parts);
  327. }
  328. }
  329. }
  330. // dojo.provide creates new objects if needed
  331. if (!empty($output['#provides'])) {
  332. $parts = explode('.', $output['#provides']);
  333. while (count($parts)) {
  334. if (!array_key_exists(implode('.', $parts), $output)) {
  335. $output[implode('.', $parts)] = array('type' => 'Object');
  336. }
  337. array_pop($parts);
  338. }
  339. }
  340. // Set privacy, classlikeness, and clean up the summary a bit
  341. foreach ($output as $object_name => $object) {
  342. if ($object_name{0} == '#') {
  343. continue;
  344. }
  345. $parts = explode('.', $object_name);
  346. $last = array_pop($parts);
  347. if ($last{0} == '_') {
  348. $output[$object_name]['private'] = true;
  349. }
  350. if (preg_match('%\._+[^A-Z]%', implode('.', $parts), $match)) {
  351. $output[$object_name]['private_parent'] = true;
  352. }
  353. if (is_array($object['tags'])) {
  354. foreach ($object['tags'] as $tag) {
  355. if ($tag == 'protected') {
  356. unset($output[$object_name]['private']);
  357. $output[$object_name]['protected'] = true;
  358. }
  359. elseif ($tag == 'private') {
  360. unset($output[$object_name]['protected']);
  361. $output[$object_name]['private'] = true;
  362. }
  363. elseif ($tag == 'deprecated') {
  364. $output[$object_name]['deprecated'] = true;
  365. }
  366. }
  367. $output[$object_name]['tags'] = array_diff($object['tags'], array('private', 'protected', 'deprecated'));
  368. }
  369. if (isset($object['inferred_type'])) {
  370. if (empty($object['type'])) {
  371. $output[$object_name]['type'] = $object['inferred_type'];
  372. }
  373. unset($output[$object_name]['inferred_type']);
  374. }
  375. if ($object['type'] == 'Function') {
  376. if (preg_match('%^(_*)[A-Z]%', $last, $match)) {
  377. if (strlen($match[1]) < 2) {
  378. unset($output[$object_name]['private']);
  379. }
  380. $output[$object_name]['classlike'] = true;
  381. }
  382. }
  383. if ($object['prototype'] && $output[$object['prototype']]) {
  384. $output[$object['prototype']]['classlike'] = true;
  385. }
  386. elseif ($object['instance'] && $output[$object['instance']]) {
  387. $output[$object['instance']]['classlike'] = true;
  388. }
  389. }
  390. return $output;
  391. }