source: Dev/trunk/src/client/util/docscripts/lib/parser2/Dojo.php

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 7.2 KB
Line 
1<?php
2
3require_once('JavaScriptStatements.php');
4require_once('JavaScriptFunction.php');
5require_once('DojoCommentBlock.php');
6
7class Dojo {
8  public static $block_keys = array('summary', 'description', 'returns', 'tags', 'this', 'exceptions');
9
10  public static function property_text(&$text, &$on) {
11    if (preg_match('%^\s*([a-z\s]+)\]\s*%', $text, $match)) {
12      $on['tags'] = preg_split('%\s+%', $match[1]);
13      $text = trim(substr($text, strlen($match[0])));
14    }
15
16    if (preg_match('%\s%', $text)) {
17      list($type, $summary) = preg_split('%\s+%', $text, 2);
18    }
19    else {
20      $type = $text;
21      $summary = '';
22    }
23
24    if(strpos($type, '?')){
25      $type = str_replace('?', '', $type);
26      $on['optional'] = true;
27    }
28    if(strpos($type, '...')){
29      $type = str_replace('...', '', $type);
30      $on['repeating'] = true;
31    }
32
33    $type = trim($type);
34    $summary = trim($summary);
35
36    if (!empty($on['type']) && $type != $on['type']) {
37      $summary = trim("$type $summary");
38    }
39    elseif (!empty($type) && (empty($on['type']) || $type != 'Object')) {
40      $on['type'] = $type;
41    }
42
43    if (!empty($summary)) {
44      $on['summary'] = self::format_summary($summary);
45    }
46  }
47
48  private static function roll_out_object($object, $name, &$output, $new_keys=array(), $on_prototype=NULL) {
49    $new_keys = array_unique(array_merge($new_keys, array_keys($object->values())));
50    foreach ($object->values() as $key => $values) {
51      foreach ($values as $value) {
52        self::roll_out($value, "$name.$key", FALSE, $output, $new_keys);
53        if ($on_prototype) {
54          $output["$name.$key"]['prototype'] = $on_prototype;
55        }
56      }
57    }
58    self::roll_out_comment_block($object, $name, $output, $new_keys);
59  }
60
61  private static function set_type($object, &$on) {
62    if (empty($on['type']) && ($type = $object->type()) && $type != 'null') {
63      if ($type == 'variable') {
64        $on['alias'] = $object->value();
65      }
66      elseif (empty($on['inferred_type']) || $type != 'Object') {
67        $on['inferred_type'] = $type;
68      }
69    }
70  }
71
72  public static function roll_out_comment_block($object, $name, &$output, $new_keys=array()) {
73    $comments = new DojoCommentBlock($object->comments(), self::$block_keys, array('example'));
74    foreach ($new_keys as $key) {
75      $comments->add_key($key);
76    }
77    self::roll_out_comments($comments, $name, self::$block_keys, $new_keys, $output);
78  }
79
80  private static function roll_out_comments($comments, $name, $keys, $new_keys, &$output) {
81    foreach ($comments->all() as $key => $text) {
82      if ($key == 'example') {
83        $output[$name]['examples'] = $text;
84      }
85      elseif ($key == 'tags') {
86        $output[$name]['tags'] = preg_split('%\s+%', trim($text));
87      }
88      elseif ($key == 'returns') {
89        $output[$name]['return_summary'] = $text;
90      }
91      elseif (in_array($key, $keys) && !empty($text)) {
92        $output[$name][$key] = ($key == 'summary') ? self::format_summary($text) : $text;
93      }
94      elseif (in_array($key, $new_keys)) {
95        self::property_text($text, $output[$name . '.' . $key]);
96      }
97      elseif (!empty($output[$name]['parameters']) && array_key_exists($key, $output[$name]['parameters'])) {
98        self::property_text($text, $output[$name]['parameters'][$key]);
99      }
100    }
101  }
102
103  public static function roll_out($object, $name, $into_function, &$output, $new_keys=array(), $is_prototype=FALSE) {
104    if (empty($output[$name])) {
105      $output[$name] = array();
106    }
107
108    self::set_type($object, $output[$name]);
109
110    $keys = self::$block_keys;
111    $this_keys = array();
112
113    if ($object instanceof JavaScriptObject || $object instanceof JavaScriptFunction) {
114      if ($object instanceof JavaScriptObject) {
115        self::roll_out_object($object, $name, $output, $keys, $is_prototype ? $name : NULL);
116      }
117      elseif ($object instanceof JavaScriptFunction) {
118        $comments = new DojoCommentBlock($object->comments(), $keys, array('example'));
119        $parent = $this_comment = $comments->get('this');
120        if (!$parent) {
121          $parent = $name;
122        }
123        elseif ($parent == 'namespace') {
124          $parent = implode('.', array_slice(explode('.', $name), 0, -1));
125        }
126        $body = new JavaScriptStatements($object->body());
127
128        foreach ($body->assignments(FALSE, $into_function) as $variable) {
129          if (substr($variable->name(), 0, 5) == 'this.') {
130            $variable_name = substr($variable->name(), 5);
131            $comments->add_key($variable_name);
132            $this_keys[] = $variable_name;
133            $full_variable_name = $parent . '.' . $variable_name;
134
135            if (!$this_comment) {
136              $found = FALSE;
137              if (!empty($output[$name]['prototype'])) {
138                $found = TRUE;
139                $full_variable_name = $output[$name]['prototype'] . '.' . $variable_name;
140                $output[$full_variable_name]['prototype'] = $output[$name]['prototype'];
141              }
142              if (!empty($output[$name]['instance'])) {
143                $found = TRUE;
144                $full_variable_name = $output[$name]['instance'] . '.' . $variable_name;
145                $output[$full_variable_name]['instance'] = $output[$name]['instance'];
146              }
147              if (!$found) {
148                $output[$full_variable_name]['instance'] = $name;
149              }
150            }
151
152            if ($variable_type = $variable->type()) {
153              if ($variable_type == 'Function') {
154                self::roll_out($variable->value(), $full_variable_name, FALSE, $output);
155              }
156              $output[$full_variable_name]['inferred_type'] = $variable_type;
157            }
158          }
159        }
160
161        $blocks = $comments->all();
162        foreach ($this_keys as $key) {
163          if ($blocks[$key]) {
164            self::property_text($blocks[$key], $output[$parent . '.' . $key]);
165          }
166        }
167
168        // TODO: Look for mixins on the same sort of values
169        foreach ($object->parameters() as $parameter) {
170          $comments->add_key($parameter->name);
171
172          $output[$name]['parameters'][$parameter->name]['name'] = $parameter->name;
173
174          $type = '';
175          if (!empty($parameter->comments)) {
176            $type = preg_replace('%(^/\*\s*|\s*\*/$)%', '', $parameter->comments[0]);
177          }
178
179          self::property_text($type, $output[$name]['parameters'][$parameter->name]);
180        }
181
182        if ($body->function_calls(TRUE, 'dojo.deprecated')) {
183          $output[$name]['deprecated'] = TRUE;
184        }
185
186        $returns = empty($output[$name]['returns']) ? array() : explode('|', $output[$name]['returns']);
187        foreach ($body->prefix('return') as $return) {
188          if (($pos = strrpos($return->line, '//')) !== false) {
189            $returns = array_merge($returns, preg_split('%\s*\|\s*%', trim(substr($return->line, $pos + 2))));
190          }
191        }
192        if (!empty($returns)) {
193          $output[$name]['returns'] = implode('|', array_unique($returns));
194        }
195
196        self::roll_out_comments($comments, $name, $keys, array(), $output);
197      }
198    }
199
200    return $new_keys;
201  }
202
203  private static function format_summary($summary) {
204      return htmlentities($summary);
205    //return preg_replace('%`([^`]+)`%', '<code>$1</code>', htmlentities($summary));
206  }
207}
Note: See TracBrowser for help on using the repository browser.