3fc4f2ab08b29c26a65fedbaca677ec0d066250f
[PHP_CodeDoc] / CodeDoc / Parser / Method.php
1 <?php
2 /*
3 *@package PHP_CodeDoc
4 */
5 /*
6
7 method parser -  implements all the routines related to parsing a method
8 and returning a Method Data Object.
9
10
11 NOTE: these methods are called statically from the main parser, and hence
12 inherit $this from the parser!!!!!!!
13
14
15 */
16
17
18
19  
20 class PHP_CodeDoc_Parser_Method {
21  
22  
23     function read($inclass) {
24         
25         if ($c = PHP_CodeDoc_Parser_Method::parse()) {
26             $this->debug(__METHOD__,$c->name);
27             if ($inclass) {
28                 
29                 $c->rfilename = $this->rfilename;
30                 $c->activeFile = $this->activeFile;
31                
32                 $this->classes[$this->_active_class]->Operations[$c->name] = $c;
33                 $this->classes[$this->_active_class]->Operations[$c->name]->class = $this->classes[$this->_active_class]->name;
34                 
35                 
36                 // silly stuff :)
37                 // merge method's author with class.
38                 // hey this is a cool use of all those array functions!
39                 
40                
41                 if (@$a = $this->classes[$this->_active_class]->Operations[$c->name]->description->author) 
42                       $this->classes[$this->_active_class]->description->author 
43                         = array_reverse(array_values( array_unique (array_reverse(array_merge(
44                             $this->classes[$this->_active_class]->description->author, $a)))));
45                 
46                 // echo $this->methods++;
47             } else {
48                 $c->active_package = &$this->packages[$this->active_package->name];
49                 $c->rfilename = $this->rfilename;
50                 $c->activeFile = $this->activeFile;
51                
52                 $this->active_directory->functions[] = $c;
53                 //$c->active_package->functions[$c->name] = $c;
54             }
55             $this->pos = $c->endpos;
56         }
57     }
58                                  
59
60     var $_active_method; // return object (PHP_CodeDoc_MethodParser)
61     
62     function parse() { // really parse
63         
64         // before pos... 2 points...
65         /// probably need to look back for 'string'?
66         $flags= array($this->look_nws(-1), $this->look_nws(-2), $this->look_nws(-3), $this->look_nws(-4));
67          
68         
69         $startpos = $this->pos;
70         if ($npos = $this->find_token_pos($startpos,array(T_STRING),'(')) {
71             $n = @$this->tokens[$npos][1];
72         } else {
73             return;
74         }
75         
76         $this->pos = $this->pos + 4;
77         $method = new PHP_CodeDoc_Data_Method;
78         $method->name = $n;
79         $method->type = "Public";
80         $method->isPublic = 1;
81         $method->isStatic = in_array(T_STATIC, $flags) ?  1 : 0;
82         $method->isFinal = in_array(T_FINAL, $flags) ?  1 : 0;
83         $method->visibility = 1;
84         if (in_array(T_PRIVATE, $flags)) {
85             $method->type = "Private";
86             $method ->isPublic = 0;
87             $method->visibility = 0;
88         }
89         if (in_array(T_PROTECTED, $flags)) {
90             $method->type = "Protected";
91             $method ->isPublic = 0;
92             $method->visibility = 0;
93         }
94         
95         
96         $method->Param = PHP_CodeDoc_Parser_Method::_get_args($method);
97         
98         if ($v = PHP_CodeDoc_Parser_Comment::parse()) {
99             $method->description = $v;
100         }
101         
102         $this->pos = $startpos;
103         PHP_CodeDoc_Parser_Method::_store_method_tokens($method);
104         // store the array of objects so It can be printed out!
105         $this->pos = $startpos;
106         
107         $method->endpos = $this->endpos;
108         return $method;
109         
110     }
111     
112             
113         
114     
115     function _store_method_tokens(&$method) { //remember the tokens for a method.. (for printing later)
116         //return;
117         $tokens=array();
118         $n=0;
119         $in_method =0;
120         $inbrak =0;
121         $level=0;
122         $pos = $this->pos;
123         $method->tokenStart = $this->pos;
124         $startpos = $this->pos;
125         while ($pos < $this->total) {
126             $v = $this->tokens[$pos];
127             if (is_array($v)) {
128                 if (!$inbrak && $v[0] == T_CURLY_OPEN) {
129                     if ($level ==0) 
130                         $in_method=1;
131                     $level++;
132                 }
133                 //if ($v[1] && $v[1]{0} == "(")
134                 //    $inbrak++;
135                 //$v[0] = token_name($v[0]);
136                 $v[2]= "{$level}:{$inbrak}";
137             } else {
138                 if (!$inbrak && trim($v) == "}") 
139                    $level--;
140                 if (!$inbrak && trim($v) == "{") {
141                     if ($level ==0) 
142                         $in_method=1;
143                     $level++;
144                 }
145                 if (trim($v) == "(") $inbrak++;
146                 if (trim($v) == ")") $inbrak--;
147            
148             }
149              
150             $tokens[$n] = $v;
151             if ($in_method && !$level) {
152                 $this->endpos = $pos;
153                 $method->tokenEnd = $pos;
154                 
155                 return;
156             }
157             $pos++;
158             $n++;
159         }
160         $this->endpos = $this->pos+1;
161         $method->tokenEnd = $this->pos+1;
162     }
163     
164     
165     
166     function _get_args (&$method) {// read arguments of a method call
167         $Param= array();
168         $level =0;
169         $p = -1;
170         while ($this->pos < $this->total) {
171             $v = $this->tokens[$this->pos];
172             if (is_array($v)) {
173                 if ($v[0] == T_WHITESPACE) { 
174                     $this->pos++;
175                     continue 1;
176                 }
177                 switch ($v[0]) {
178                     case T_VARIABLE:
179                         $p++;
180                         $Param[$p] = new StdClass;
181                         $Param[$p]->name = $v[1];
182                         break;
183                     
184                     case T_FILE:
185                     case T_LINE:
186                     case T_LNUMBER:
187                     case T_CONSTANT_ENCAPSED_STRING:
188                         if (!@$Param[$p]->Value) 
189                             $Param[$p]->Value = $v[1];
190                         break;
191                     case T_ARRAY:
192                         $Param[$p]->Value  = "array()";
193                         break;
194                 }
195                 //echo "XXX:{$this->pos}:" .token_name($v[0]) . ":{$this->level}:". $v[1] .":\n";
196             } else {
197                 //if ($v == "(") $level++;
198                 if ($v == "{") return $Param;
199                 
200                 //if ($v == ",") $current_var = "";
201                 //echo "XXX:{$this->pos}:RAW:{$v}\n";
202             }
203             $this->pos++;
204         }        
205         return $Param; // shouldnt really get here
206     }
207     
208    
209     
210