Subversion Repositories fastphp

Rev

Rev 82 | Rev 99 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 daniel-mar 1
<?php
2
 
3
// TODO: show full signature of each element?
4
 
5
error_reporting(0);
6
 
7
require_once __DIR__ . '/codeexplorer_api.inc.php';
8
 
9
define('ICON_TYPE_FUNCTION',     1);
10
define('ICON_TYPE_CONSTRUCTOR',  2);
11
define('ICON_TYPE_DESTRUCTOR',   3);
12
define('ICON_TYPE_MAGICMETHOD',  4);
13
define('ICON_TYPE_CLASS',        5);
14
define('ICON_TYPE_TRAIT',        6);
15
define('ICON_TYPE_INTERFACE',    7);
16
define('ICON_TYPE_VAR',          8);
17
define('ICON_TYPE_CONST',        9);
18
define('ICON_TYPE_TODO',        10);
19
define('ICON_TYPE_ERROR',       11);
20
 
21
class MyFastPHPIcon extends FastPHPIcon {
22
 
23
        public function imageIndex() {
24
                     if (($this->getType() == ICON_TYPE_CLASS)                               && (!$this->isAbstract())) return  0; // class
25
                else if (($this->getType() == ICON_TYPE_CLASS)                               && ( $this->isAbstract())) return  1; // abstract class
26
                else if (($this->getType() == ICON_TYPE_INTERFACE)                                                    ) return  2; // interface
27
                else if (($this->getType() == ICON_TYPE_TRAIT)                                                        ) return  3; // trait
28
                else if (($this->getType() == ICON_TYPE_CONST)     && ($this->isPrivate())                            ) return  4; // private const
29
                else if (($this->getType() == ICON_TYPE_VAR)       && ($this->isPrivate())                            ) return  5; // private var
30
                else if (($this->isMethod())                       && ($this->isPrivate())   && (!$this->isAbstract())) return  6; // private function
31
                else if (($this->isMethod())                       && ($this->isPrivate())   && ( $this->isAbstract())) return  7; // private abstract function
32
                else if (($this->getType() == ICON_TYPE_CONST)     && ($this->isProtected())                          ) return  8; // protected const
33
                else if (($this->getType() == ICON_TYPE_VAR)       && ($this->isProtected())                          ) return  9; // protected var
34
                else if (($this->isMethod())                       && ($this->isProtected()) && (!$this->isAbstract())) return 10; // protected function
35
                else if (($this->isMethod())                       && ($this->isProtected()) && ( $this->isAbstract())) return 11; // protected abstract function
36
                else if (($this->getType() == ICON_TYPE_CONST)     && ($this->isPublic())                             ) return 12; // public const
37
                else if (($this->getType() == ICON_TYPE_VAR)       && ($this->isPublic())                             ) return 13; // public var
38
                else if (($this->isMethod())                       && ($this->isPublic())    && (!$this->isAbstract())) return 14; // public function
39
                else if (($this->isMethod())                       && ($this->isPublic())    && ( $this->isAbstract())) return 15; // public abstract function
40
                else if (($this->getType() == ICON_TYPE_TODO)                                                         ) return 16; // To-Do comment
41
                else                                                                                                    return -1;
42
        }
43
 
44
        public function isMethod() {
45
                return (($this->getType() == ICON_TYPE_FUNCTION)    ||
46
                        ($this->getType() == ICON_TYPE_CONSTRUCTOR) ||
47
                        ($this->getType() == ICON_TYPE_DESTRUCTOR)  ||
48
                        ($this->getType() == ICON_TYPE_MAGICMETHOD));
49
        }
50
 
51
}
52
 
53
class MyFastPHPCodeExplorer {
54
 
55
        public function handle($code) {
56
                // Quick'n'Dirty fix to correctly parse the line
57
                // test(XYZ::class)
58
                $code = str_replace('::class', '', $code);
59
 
60
                // Quick'n'Dirty fix to correctly parse the line
61
                // $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
62
                $code = str_replace('{$', '{', $code);
63
 
64
                $token = token_get_all($code);
65
                $wait_function = false;
66
                $wait_const = false;
67
                $wait_class = false;
68
                $wait_trait = false;
69
                $icon = new MyFastPHPIcon();
70
                $wait_interface = false;
71
                $wait_abstract_func_list_end = false;
72
                $levelAry = array();
73
                $dep = 0;
74
                $insideFuncAry = array();
75
 
76
                if (!$token) {
77
                        $icon->setType(ICON_TYPE_ERROR);
78
                        FastPHPWriter::outputLeafNode($icon, 0, 'SYNTAX ERROR');
79
                }
80
 
81
                foreach ($token as $data) {
82
                        if ($data == '{') $dep++;
83
                        if ($data == '}') {
84
                                $dep--;
85
                                if ((count($levelAry) > 0) && (self::array_peek($levelAry) == $dep)) {
86
                                        array_pop($levelAry);
87
                                        FastPHPWriter::outputDecreaseLevel();
88
                                }
89
                                if ((count($insideFuncAry) > 0) && (self::array_peek($insideFuncAry) == $dep)) {
90
                                        array_pop($insideFuncAry);
91
                                }
92
                        }
93
 
94
                        $token = (!is_array($data)) ? null : $data[0];
95
                        $value = (!is_array($data)) ? null : $data[1];
96
                        $line  = (!is_array($data)) ? null : $data[2];
97
 
98
                        if ($value == '${') $dep++;
99
 
100
                        if ($wait_function && ($data == '{')) {
101
                                $wait_function = false; // Anonymous functions do not have a name
102
                        }
103
 
104
                        if ($wait_function && ($token == T_STRING)) {
105
                                $wait_function = false;
106
                                if ($icon->isAbstract()) {
107
                                        $desc = "abstract function $value()";
108
                                        $wait_abstract_func_list_end = true;
109
                                } else {
110
                                        $desc = "function $value()";
111
                                        $insideFuncAry[] = $dep;
112
                                }
113
 
114
                                if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein
115
                                        $icon->setType(ICON_TYPE_CONSTRUCTOR);
116
                                } else if ($value == '__destruct') {
117
                                        $icon->setType(ICON_TYPE_DESTRUCTOR);
118
                                } else if (substr($value, 0, 2) == '__') {
119
                                        $icon->setType(ICON_TYPE_MAGICMETHOD);
120
                                } else {
121
                                        $icon->setType(ICON_TYPE_FUNCTION);
122
                                }
123
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
124
                                $icon->reset();
125
                        }
126
 
127
                        if ($wait_class && ($token == T_STRING)) {
128
                                if ($icon->isAbstract()) {
129
                                        $desc = "Abstract Class $value\n";
130
                                } else {
131
                                        $desc = "Class $value\n";
132
                                }
133
                                $wait_class = false;
134
                                $levelAry[] = $dep;
135
 
136
                                $icon->setType(ICON_TYPE_CLASS);
137
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
138
                                $icon->reset();
139
 
140
                                FastPHPWriter::outputIncreaseLevel();
141
                        }
142
 
143
                        if ($wait_trait && ($token == T_STRING)) {
144
                                $desc = "Trait $value\n";
145
                                $wait_trait = false;
146
                                $levelAry[] = $dep;
147
 
148
                                $icon->setType(ICON_TYPE_TRAIT);
149
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
150
                                $icon->reset();
151
 
152
                                FastPHPWriter::outputIncreaseLevel();
153
                        }
154
 
155
                        if ($wait_interface && ($token == T_STRING)) {
156
                                $desc = "Interface $value\n";
157
                                $wait_interface = false;
158
                                $levelAry[] = $dep;
159
 
160
                                $icon->setType(ICON_TYPE_INTERFACE);
161
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
162
                                $icon->reset();
163
 
164
                                FastPHPWriter::outputIncreaseLevel();
165
                        }
166
 
167
                        if ($wait_const && ($token == T_STRING)) {
168
                                $desc = "const $value\n";
169
                                $wait_const = false;
170
 
171
                                $icon->setType(ICON_TYPE_CONST);
172
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
173
                                $icon->reset();
174
                        }
175
 
176
                        if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE)) {
177
                                $desc = "$value\n";
178
 
179
                                $icon->setType(ICON_TYPE_VAR);
180
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
181
                                $icon->reset();
182
                        }
183
 
184
                        if ($token == T_PRIVATE)   $icon->setPrivate();
185
                        if ($token == T_PROTECTED) $icon->setProtected();
186
                        if ($token == T_PUBLIC)    $icon->setPublic();
187
                        if ($token == T_ABSTRACT)  $icon->setAbstract();
188
                        if ($token == T_FINAL)     $icon->setFinal();
189
                        if ($token == T_STATIC)    $icon->setStatic();
190
 
191
                        if (($data == ';') || ($data == '{') || ($data == '}')) {
192
                                $wait_abstract_func_list_end = false;
193
                                $icon->reset();
194
                        }
195
 
196
                        if ($token == T_FUNCTION) {
197
                                $wait_function = true;
198
                        }
199
                        if ($token == T_CLASS) {
200
                                $wait_class = true;
201
                                $dep = 0;
202
                        }
203
                        if ($token == T_INTERFACE) {
204
                                $wait_interface = true;
205
                                $dep = 0;
206
                        }
207
                        if ($token == T_TRAIT) {
208
                                $wait_trait = true;
209
                                $dep = 0;
210
                        }
211
 
212
                        if ($token == T_CONST) {
213
                                $wait_const = true;
214
                        }
215
 
216
                        if (($token == T_COMMENT) && self::isToDoDescription($value)) {
217
                                $comment_lines = explode("\n", trim($value));
218
                                foreach ($comment_lines as $line_no => $comment_line) {
219
                                        if (self::isToDoDescription($comment_line)) {
220
                                                // Because a To-Do-entry can stand everywhere (e.g. between a "private" and a "function" keyword)
221
                                                // we shall not alter the $icon instance
222
                                                $todoIcon = clone $icon;
223
                                                $todoIcon->setType(ICON_TYPE_TODO);
224
                                                FastPHPWriter::outputLeafNode($todoIcon, $line+$line_no, self::stripComment($comment_line));
225
                                                unset($todoIcon);
226
                                        }
227
                                }
228
                        }
229
                }
230
        }
231
 
232
        private static function isToDoDescription($comment) {
233
                return ((stripos($comment, 'TODO')   !== false) ||
234
                        (stripos($comment, 'BUGBUG') !== false) ||
235
                        (stripos($comment, 'FIXME')  !== false) ||
236
                        (stripos($comment, 'XXX')    !== false));
237
        }
238
 
239
        private static function stripComment($x) {
240
                $x = trim($x);
241
                if (substr($x, 0, 1) == '#') return trim(substr($x, 1));
242
                if (substr($x, 0, 2) == '//') return trim(substr($x, 2));
243
                if (substr($x, 0, 2) == '/*') return trim(substr($x, 2, strlen($x)-4));
244
                return $x;
245
        }
246
 
247
 
248
        private static /*final*/ function array_peek($array) {
249
                if (!isset($array[count($array)-1])) return null;
250
                return $array[count($array)-1];
251
        }
252
}
253
 
92 daniel-mar 254
$parser = new MyFastPHPCodeExplorer();
82 daniel-mar 255
while (true) {
256
        try {
257
                $code = FastPHPReader::readCodeFromEditor();
258
        } catch (FastPHPExitSignalReceivedException $e) {
259
                die();
260
        }
261
        FastPHPWriter::outputHeader();
262
        $parser->handle($code);
263
        FastPHPWriter::outputExit();
264
        FastPHPWriter::signalOutputEnd();
265
        sleep(1);
92 daniel-mar 266
}