34e394adff0ac2a6c5aa23693d843631b91479da
[PHP_CodeDoc] / CodeDoc / Parser / Comment.php
1 <?php
2 /*
3 *@package PHP_CodeDoc
4 */
5 /*
6
7 comment parser -  implements all the routines related to parsing a phpdoc comment
8
9 and returning a Method Data Object.
10
11 */
12  
13 require_once 'PHP/CodeDoc/Data/Param.php';
14
15 class PHP_CodeDoc_Parser_Comment {
16
17     
18     function parse() { // static method !!!! returns a Phpdoc data object
19         if ($r = PHP_CodeDoc_Parser_Comment::phpDocParse()) {
20             return $r;
21         }
22         return PHP_CodeDoc_Parser_Comment::shortDocParse();
23     } 
24     
25     
26     function phpDocParse() { // parse normal phpdoc comments
27         $desc = new PHP_CodeDoc_Data_PhpDoc;   
28         if (!$this->last_comment_block) 
29             return;
30         // clear last comment
31         $comment  = $this->last_comment_block;
32         $this->last_comment_block = "";
33         if (substr(trim($comment),0,2) != "/*") 
34             return;
35         
36          
37         $flags = array();
38         $desc->param = array();
39         $desc->author = array();
40         
41         $desc->original = $comment;
42         
43         $lines = explode("\n",$comment  );
44          
45         for($ln =0; $ln < count($lines); $ln++) {
46             $line = $lines[$ln];
47             $linedata = array();
48             // special key!
49             $line = preg_replace('/^\s*\/\*+/','', $line); // replace    /**
50             $line = preg_replace('/\s*\*+\/\s*$/','', $line);// replace ***/
51             $line = preg_replace('/^\s*\*+/','', $line); // replace    *
52             
53             if (preg_match("/^@\s*([a-z]+)\s*(.*)$/i",trim($line),$linedata)) {
54                 $key = $linedata[1];
55                 switch ($key) {
56                     // mutliple values
57                     case "param":
58                         $ar = explode(' ',trim($linedata[2]));
59                         $param = new PHP_CodeDoc_Data_Param();
60                         $add = '';
61                         $nl = $ln;
62                         while(true) {
63                             
64                             if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
65                                 $ln = $nl;
66                                 break;
67                             }
68                             $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
69                             if (strlen($next)-  strlen(ltrim($next)) > 4) {
70                                 // it's carry on...
71                                 $add .= "\n$next";
72                                 $nl++;
73                                 continue;
74                             }
75                             $ln = $nl;
76                             break;
77                             
78                         }
79                         //var_dump($add);
80                         
81                         // wierd logic ! 
82                         // @param object sometype $val description
83                         if (preg_match('/\s*(\w+)\s+(\w+)\s+(\$\w+)\s+(.*)/',$linedata[2],$args)) {
84                             $param->type = $args[2];
85                             $param->var = $args[3];
86                             $param->desc = trim($args[4]) . $add; 
87                             $desc->param[] = $param;
88                             break;
89                         }
90                         
91                         // @param sometype $val description
92                         if (preg_match('/\s*(\w+)\s+(\$\w+)\s+(.*)/',$linedata[2],$args)) {
93                             $param->type = $args[1];
94                             $param->var = $args[2];
95                             $param->desc = trim($args[3]) . $add; 
96                             $desc->param[] = $param;
97                             break;
98                         }
99                         // @param $val description
100                         if (preg_match('/\s*(\$\w+)\s+(.*)/',$linedata[2],$args)) {
101                             $param->type = "";
102                             $param->var = $args[1];
103                             $param->desc = trim($args[2]) . $add; 
104                             $desc->param[] = $param;
105                             break;
106                         }
107                          // @param type description
108                         if (preg_match('/\s*(\w+)\s+(.*)/',$linedata[2],$args)) {
109                             $param->type = $args[1];
110                             $param->desc = trim($args[2]) . $add; 
111                             $desc->param[] = $param;
112                             break;
113                         }
114                         
115                         break;
116                         
117                     case "return":
118                         //echo "GOT {$linedata[2]}\n";
119                         $ar = explode(' ',trim($linedata[2]));
120                         
121                         
122                         $add = '';
123                         $nl = $ln;
124                         while(true) {
125                             
126                             if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
127                                 $ln = $nl;
128                                 break;
129                             }
130                             $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
131                             if (strlen($next)-  strlen(ltrim($next)) > 4) {
132                                 // it's carry on...
133                                 $add .= "\n$next";
134                                 $nl++;
135                                 continue;
136                             }
137                             $ln = $nl;
138                             break;
139                             
140                         }
141                         
142                         //echo serialize($ar);
143                         $desc->return = new PHP_CodeDoc_Data_Param();
144                         $desc->return->type = $ar[0];
145                         $i=0;
146                         if ($ar[0] == "object") {
147                             $desc->return->type = @$ar[1];
148                             $i=1;
149                         }
150                         @$desc->return->var = $ar[$i];
151                         $ar[0]="";
152                         $ar[$i]="";
153                         @$desc->return->desc = trim(implode(' ',$ar)) . $add;
154                         break;
155                         
156                     case "author":      // multiple valued
157                         $desc->author[] = $linedata[2];
158                         break;
159                     case "CDATA":
160                         $flags['rawdata'] = TRUE;
161                         break;
162                     default:
163                         $desc->$key = $linedata[2];\r
164                 }
165                 continue;
166             }
167             // only get here if it wasnt a @ comment
168             if (trim($line) == "!") {
169                 continue;
170             }
171             if (!$desc->short) {
172                 $desc->short = $line;
173                 continue;
174             }
175             if (!$desc->long) {
176                 $desc->long = $line;
177                 continue;
178             }
179             $desc->long .= "\n" . $line;
180         }
181         
182         if (@$flags['rawdata']) {
183             $desc->long_raw = $desc->long;
184             $desc->long = htmlspecialchars($desc->long);
185         }
186         if (!$desc->short) 
187             $desc->short="No Description";
188         
189         return $desc;
190     } 
191     
192     
193     function shortDocParse() { // parse short comments
194         $desc = new PHP_CodeDoc_Data_PhpDoc;    
195         $pos = $this->pos+1;
196         //echo "LOOKING FOR SHORT";
197         while($pos < $this->total) {
198             $v = $this->tokens[$pos];
199             
200             if (is_array($v)) {
201                 
202                 if (($v[0] == T_WHITESPACE) && (strpos($v[1],"\n") !== FALSE))
203                     return $desc;
204                 if ($v[0] == T_COMMENT) {
205                     //echo "setting short {$v[1]}\n";
206                     $desc->short = preg_replace('/^\/\//m','',$v[1]);
207                     if (strpos($v[1],"\n")) return $desc;
208                 }
209              
210             } 
211                
212             $pos++;
213         }
214     }
215     
216 }
217
218 ?>