7 comment parser - implements all the routines related to parsing a phpdoc comment
9 and returning a Method Data Object.
13 require_once 'PHP/CodeDoc/Data/Param.php';
15 class PHP_CodeDoc_Parser_Comment {
18 function parse() { // static method !!!! returns a Phpdoc data object
19 if ($r = PHP_CodeDoc_Parser_Comment::phpDocParse()) {
22 return PHP_CodeDoc_Parser_Comment::shortDocParse();
26 function phpDocParse() { // parse normal phpdoc comments
27 $desc = new PHP_CodeDoc_Data_PhpDoc;
28 if (!$this->last_comment_block)
31 $comment = $this->last_comment_block;
32 $this->last_comment_block = "";
33 if (substr(trim($comment),0,2) != "/*")
38 $desc->param = array();
39 $desc->author = array();
41 $desc->original = $comment;
43 $lines = explode("\n",$comment );
45 for($ln =0; $ln < count($lines); $ln++) {
49 $line = preg_replace('/^\s*\/\*+/','', $line); // replace /**
50 $line = preg_replace('/\s*\*+\/\s*$/','', $line);// replace ***/
51 $line = preg_replace('/^\s*\*+/','', $line); // replace *
53 if (preg_match("/^@\s*([a-z]+)\s*(.*)$/i",trim($line),$linedata)) {
58 $ar = explode(' ',trim($linedata[2]));
59 $param = new PHP_CodeDoc_Data_Param();
64 if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
68 $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
69 if (strlen($next)- strlen(ltrim($next)) > 4) {
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;
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;
99 // @param $val description
100 if (preg_match('/\s*(\$\w+)\s+(.*)/',$linedata[2],$args)) {
102 $param->var = $args[1];
103 $param->desc = trim($args[2]) . $add;
104 $desc->param[] = $param;
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;
118 //echo "GOT {$linedata[2]}\n";
119 $ar = explode(' ',trim($linedata[2]));
126 if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
130 $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
131 if (strlen($next)- strlen(ltrim($next)) > 4) {
142 //echo serialize($ar);
143 $desc->return = new PHP_CodeDoc_Data_Param();
144 $desc->return->type = $ar[0];
146 if ($ar[0] == "object") {
147 $desc->return->type = @$ar[1];
150 @$desc->return->var = $ar[$i];
153 @$desc->return->desc = trim(implode(' ',$ar)) . $add;
156 case "author": // multiple valued
157 $desc->author[] = $linedata[2];
160 $flags['rawdata'] = TRUE;
163 $desc->$key = $linedata[2];
167 // only get here if it wasnt a @ comment
168 if (trim($line) == "!") {
172 $desc->short = $line;
179 $desc->long .= "\n" . $line;
182 if (@$flags['rawdata']) {
183 $desc->long_raw = $desc->long;
184 $desc->long = htmlspecialchars($desc->long);
187 $desc->short="No Description";
193 function shortDocParse() { // parse short comments
194 $desc = new PHP_CodeDoc_Data_PhpDoc;
196 //echo "LOOKING FOR SHORT";
197 while($pos < $this->total) {
198 $v = $this->tokens[$pos];
202 if (($v[0] == T_WHITESPACE) && (strpos($v[1],"\n") !== FALSE))
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;