CodeDoc/Parser/Comment.php
authorAlan Knowles <alan@roojs.com>
Tue, 25 Oct 2016 09:50:21 +0000 (17:50 +0800)
committerAlan Knowles <alan@roojs.com>
Tue, 25 Oct 2016 09:50:21 +0000 (17:50 +0800)
CodeDoc/Parser/Comment.php [new file with mode: 0644]

diff --git a/CodeDoc/Parser/Comment.php b/CodeDoc/Parser/Comment.php
new file mode 100644 (file)
index 0000000..a52e495
--- /dev/null
@@ -0,0 +1,218 @@
+<?php
+/*
+*@package PHP_CodeDoc
+*/
+/*
+
+comment parser -  implements all the routines related to parsing a phpdoc comment
+
+and returning a Method Data Object.
+
+*/
+require_once 'PHP/CodeDoc/Data/Param.php';
+
+class PHP_CodeDoc_Parser_Comment {
+
+    
+    function parse() { // static method !!!! returns a Phpdoc data object
+        if ($r = PHP_CodeDoc_Parser_Comment::phpDocParse()) {
+            return $r;
+        }
+        return PHP_CodeDoc_Parser_Comment::shortDocParse();
+    } 
+    
+    
+    function phpDocParse() { // parse normal phpdoc comments
+        $desc = new PHP_CodeDoc_Data_PhpDoc;   
+        if (!$this->last_comment_block) 
+            return;
+        // clear last comment
+        $comment  = $this->last_comment_block;
+        $this->last_comment_block = "";
+        if (substr(trim($comment),0,2) != "/*") 
+            return;
+        
+         
+        $flags = array();
+        $desc->param = array();
+        $desc->author = array();
+        
+        $desc->original = $comment;
+        
+        $lines = explode("\n",$comment  );
+         
+        for($ln =0; $ln < count($lines); $ln++) {
+            $line = $lines[$ln];
+            $linedata = array();
+            // special key!
+            $line = preg_replace('/^\s*\/\*+/','', $line); // replace    /**
+            $line = preg_replace('/\s*\*+\/\s*$/','', $line);// replace ***/
+            $line = preg_replace('/^\s*\*+/','', $line); // replace    *
+            
+            if (preg_match("/^@\s*([a-z]+)\s*(.*)$/i",trim($line),$linedata)) {
+                $key = $linedata[1];
+                switch ($key) {
+                    // mutliple values
+                    case "param":
+                        $ar = explode(' ',trim($linedata[2]));
+                        $param = new PHP_CodeDoc_Data_Param();
+                        $add = '';
+                        $nl = $ln;
+                        while(true) {
+                            
+                            if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
+                                $ln = $nl;
+                                break;
+                            }
+                            $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
+                            if (strlen($next)-  strlen(ltrim($next)) > 4) {
+                                // it's carry on...
+                                $add .= "\n$next";
+                                $nl++;
+                                continue;
+                            }
+                            $ln = $nl;
+                            break;
+                            
+                        }
+                        //var_dump($add);
+                        
+                        // wierd logic ! 
+                        // @param object sometype $val description
+                        if (preg_match('/\s*(\w+)\s+(\w+)\s+(\$\w+)\s+(.*)/',$linedata[2],$args)) {
+                            $param->type = $args[2];
+                            $param->var = $args[3];
+                            $param->desc = trim($args[4]) . $add; 
+                            $desc->param[] = $param;
+                            break;
+                        }
+                        
+                        // @param sometype $val description
+                        if (preg_match('/\s*(\w+)\s+(\$\w+)\s+(.*)/',$linedata[2],$args)) {
+                            $param->type = $args[1];
+                            $param->var = $args[2];
+                            $param->desc = trim($args[3]) . $add; 
+                            $desc->param[] = $param;
+                            break;
+                        }
+                        // @param $val description
+                        if (preg_match('/\s*(\$\w+)\s+(.*)/',$linedata[2],$args)) {
+                            $param->type = "";
+                            $param->var = $args[1];
+                            $param->desc = trim($args[2]) . $add; 
+                            $desc->param[] = $param;
+                            break;
+                        }
+                         // @param type description
+                        if (preg_match('/\s*(\w+)\s+(.*)/',$linedata[2],$args)) {
+                            $param->type = $args[1];
+                            $param->desc = trim($args[2]) . $add; 
+                            $desc->param[] = $param;
+                            break;
+                        }
+                        
+                        break;
+                        
+                    case "return":
+                        //echo "GOT {$linedata[2]}\n";
+                        $ar = explode(' ',trim($linedata[2]));
+                        
+                        
+                        $add = '';
+                        $nl = $ln;
+                        while(true) {
+                            
+                            if (preg_match('#^(\s*\*\s*@|\s*\*+/)#' , $lines[$nl+1])) {
+                                $ln = $nl;
+                                break;
+                            }
+                            $next = preg_replace('#^\s*\*+#', '', $lines[$nl+1]);
+                            if (strlen($next)-  strlen(ltrim($next)) > 4) {
+                                // it's carry on...
+                                $add .= "\n$next";
+                                $nl++;
+                                continue;
+                            }
+                            $ln = $nl;
+                            break;
+                            
+                        }
+                        
+                        //echo serialize($ar);
+                        $desc->return = new PHP_CodeDoc_Data_Param();
+                        $desc->return->type = $ar[0];
+                        $i=0;
+                        if ($ar[0] == "object") {
+                            $desc->return->type = @$ar[1];
+                            $i=1;
+                        }
+                        @$desc->return->var = $ar[$i];
+                        $ar[0]="";
+                        $ar[$i]="";
+                        @$desc->return->desc = trim(implode(' ',$ar)) . $add;
+                        break;
+                        
+                    case "author":      // multiple valued
+                        $desc->author[] = $linedata[2];
+                        break;
+                    case "CDATA":
+                        $flags['rawdata'] = TRUE;
+                        break;
+                    default:
+                        $desc->$key = $linedata[2];
+                }
+                continue;
+            }
+            // only get here if it wasnt a @ comment
+            if (trim($line) == "!") {
+                continue;
+            }
+            if (!$desc->short) {
+                $desc->short = $line;
+                continue;
+            }
+            if (!$desc->long) {
+                $desc->long = $line;
+                continue;
+            }
+            $desc->long .= "\n" . $line;
+        }
+        
+        if (@$flags['rawdata']) {
+            $desc->long_raw = $desc->long;
+            $desc->long = htmlspecialchars($desc->long);
+        }
+        if (!$desc->short) 
+            $desc->short="No Description";
+        
+        return $desc;
+    } 
+    
+    
+    function shortDocParse() { // parse short comments
+        $desc = new PHP_CodeDoc_Data_PhpDoc;    
+        $pos = $this->pos+1;
+        //echo "LOOKING FOR SHORT";
+        while($pos < $this->total) {
+            $v = $this->tokens[$pos];
+            
+            if (is_array($v)) {
+                
+                if (($v[0] == T_WHITESPACE) && (strpos($v[1],"\n") !== FALSE))
+                    return $desc;
+                if ($v[0] == T_COMMENT) {
+                    //echo "setting short {$v[1]}\n";
+                    $desc->short = preg_replace('/^\/\//m','',$v[1]);
+                    if (strpos($v[1],"\n")) return $desc;
+                }
+             
+            } 
+               
+            $pos++;
+        }
+    }
+    
+}
+
+?>
\ No newline at end of file