@@ -13,123 +13,123 @@ |
||
13 | 13 | abstract class AbstractPreprocessor |
14 | 14 | { |
15 | 15 | |
16 | - private $defines = []; |
|
17 | - private $stack = []; |
|
18 | - |
|
19 | - public function __construct() |
|
20 | - { |
|
21 | - $this->resetDefines(); |
|
22 | - } |
|
23 | - |
|
24 | - public function resetDefines(): void |
|
25 | - { |
|
26 | - $this->defines = []; |
|
27 | - } |
|
28 | - |
|
29 | - public function addDefines(array $defines): void |
|
30 | - { |
|
31 | - $this->defines = array_merge($this->defines, $defines); |
|
32 | - } |
|
33 | - |
|
34 | - public function define($name, $value = 1): void |
|
35 | - { |
|
36 | - $this->defines[$name] = $value; |
|
37 | - } |
|
38 | - |
|
39 | - public function undefine($name): void |
|
40 | - { |
|
41 | - unset($this->defines[$name]); |
|
42 | - } |
|
43 | - |
|
44 | - public function preprocessFile($filename) |
|
45 | - { |
|
46 | - return $this->preprocess(file_get_contents($filename)); |
|
47 | - } |
|
48 | - |
|
49 | - public function preprocess($content) |
|
50 | - { |
|
51 | - $this->stack = []; |
|
52 | - |
|
53 | - return $this->parseContent($content); |
|
54 | - } |
|
55 | - |
|
56 | - abstract protected function parseContent($content); |
|
57 | - |
|
58 | - protected function handle($command, $expression): bool |
|
59 | - { |
|
60 | - switch (strtolower($command)) { |
|
61 | - case 'if': |
|
62 | - $name = self::wordShift($expression); |
|
63 | - $state = $this->getState(); |
|
64 | - if (empty($expression)) { |
|
65 | - $this->stack[] = $state && !empty($this->defines[$name]); |
|
66 | - } else { |
|
67 | - $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
68 | - } |
|
69 | - break; |
|
70 | - |
|
71 | - case 'ifdef': |
|
72 | - $this->stack[] = $this->getState() && isset($this->defines[$expression]); |
|
73 | - break; |
|
74 | - |
|
75 | - case 'ifndef': |
|
76 | - $this->stack[] = $this->getState() && !isset($this->defines[$expression]); |
|
77 | - break; |
|
78 | - |
|
79 | - case 'else': |
|
80 | - $state = $this->getState(); |
|
81 | - array_pop($this->stack); |
|
82 | - $this->stack[] = !$state; |
|
83 | - break; |
|
84 | - |
|
85 | - case 'elif': |
|
86 | - $name = self::wordShift($expression); |
|
87 | - $state = $this->getState(); |
|
88 | - array_pop($this->stack); |
|
89 | - if (empty($expression)) { |
|
90 | - $this->stack[] = !$state && !empty($this->defines[$name]); |
|
91 | - } else { |
|
92 | - $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
93 | - } |
|
94 | - break; |
|
95 | - |
|
96 | - case 'define': |
|
97 | - $name = self::wordShift($expression); |
|
98 | - $this->defines[$name] = $expression; |
|
99 | - break; |
|
100 | - |
|
101 | - case 'undef': |
|
102 | - unset($this->defines[$expression]); |
|
103 | - break; |
|
104 | - |
|
105 | - case 'endif': |
|
106 | - array_pop($this->stack); |
|
107 | - break; |
|
108 | - |
|
109 | - default: |
|
110 | - return false; |
|
111 | - } |
|
112 | - |
|
113 | - return true; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Get the first word from a string and remove it from the string. |
|
118 | - * |
|
119 | - * @param string $data |
|
120 | - * @return boolean|string |
|
121 | - */ |
|
122 | - private static function wordShift(&$data) |
|
123 | - { |
|
124 | - if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
125 | - $data = $matches[2]; |
|
126 | - return $matches[1]; |
|
127 | - } |
|
128 | - return false; |
|
129 | - } |
|
130 | - |
|
131 | - protected function getState(): bool |
|
132 | - { |
|
133 | - return empty($this->stack) || end($this->stack); |
|
134 | - } |
|
16 | + private $defines = []; |
|
17 | + private $stack = []; |
|
18 | + |
|
19 | + public function __construct() |
|
20 | + { |
|
21 | + $this->resetDefines(); |
|
22 | + } |
|
23 | + |
|
24 | + public function resetDefines(): void |
|
25 | + { |
|
26 | + $this->defines = []; |
|
27 | + } |
|
28 | + |
|
29 | + public function addDefines(array $defines): void |
|
30 | + { |
|
31 | + $this->defines = array_merge($this->defines, $defines); |
|
32 | + } |
|
33 | + |
|
34 | + public function define($name, $value = 1): void |
|
35 | + { |
|
36 | + $this->defines[$name] = $value; |
|
37 | + } |
|
38 | + |
|
39 | + public function undefine($name): void |
|
40 | + { |
|
41 | + unset($this->defines[$name]); |
|
42 | + } |
|
43 | + |
|
44 | + public function preprocessFile($filename) |
|
45 | + { |
|
46 | + return $this->preprocess(file_get_contents($filename)); |
|
47 | + } |
|
48 | + |
|
49 | + public function preprocess($content) |
|
50 | + { |
|
51 | + $this->stack = []; |
|
52 | + |
|
53 | + return $this->parseContent($content); |
|
54 | + } |
|
55 | + |
|
56 | + abstract protected function parseContent($content); |
|
57 | + |
|
58 | + protected function handle($command, $expression): bool |
|
59 | + { |
|
60 | + switch (strtolower($command)) { |
|
61 | + case 'if': |
|
62 | + $name = self::wordShift($expression); |
|
63 | + $state = $this->getState(); |
|
64 | + if (empty($expression)) { |
|
65 | + $this->stack[] = $state && !empty($this->defines[$name]); |
|
66 | + } else { |
|
67 | + $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
68 | + } |
|
69 | + break; |
|
70 | + |
|
71 | + case 'ifdef': |
|
72 | + $this->stack[] = $this->getState() && isset($this->defines[$expression]); |
|
73 | + break; |
|
74 | + |
|
75 | + case 'ifndef': |
|
76 | + $this->stack[] = $this->getState() && !isset($this->defines[$expression]); |
|
77 | + break; |
|
78 | + |
|
79 | + case 'else': |
|
80 | + $state = $this->getState(); |
|
81 | + array_pop($this->stack); |
|
82 | + $this->stack[] = !$state; |
|
83 | + break; |
|
84 | + |
|
85 | + case 'elif': |
|
86 | + $name = self::wordShift($expression); |
|
87 | + $state = $this->getState(); |
|
88 | + array_pop($this->stack); |
|
89 | + if (empty($expression)) { |
|
90 | + $this->stack[] = !$state && !empty($this->defines[$name]); |
|
91 | + } else { |
|
92 | + $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
93 | + } |
|
94 | + break; |
|
95 | + |
|
96 | + case 'define': |
|
97 | + $name = self::wordShift($expression); |
|
98 | + $this->defines[$name] = $expression; |
|
99 | + break; |
|
100 | + |
|
101 | + case 'undef': |
|
102 | + unset($this->defines[$expression]); |
|
103 | + break; |
|
104 | + |
|
105 | + case 'endif': |
|
106 | + array_pop($this->stack); |
|
107 | + break; |
|
108 | + |
|
109 | + default: |
|
110 | + return false; |
|
111 | + } |
|
112 | + |
|
113 | + return true; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Get the first word from a string and remove it from the string. |
|
118 | + * |
|
119 | + * @param string $data |
|
120 | + * @return boolean|string |
|
121 | + */ |
|
122 | + private static function wordShift(&$data) |
|
123 | + { |
|
124 | + if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
125 | + $data = $matches[2]; |
|
126 | + return $matches[1]; |
|
127 | + } |
|
128 | + return false; |
|
129 | + } |
|
130 | + |
|
131 | + protected function getState(): bool |
|
132 | + { |
|
133 | + return empty($this->stack) || end($this->stack); |
|
134 | + } |
|
135 | 135 | } |
@@ -18,72 +18,72 @@ |
||
18 | 18 | class Preprocessor extends AbstractPreprocessor |
19 | 19 | { |
20 | 20 | |
21 | - private $prefix = 'rest'; |
|
21 | + private $prefix = 'rest'; |
|
22 | 22 | |
23 | - public function __construct($prefix = null) |
|
24 | - { |
|
25 | - parent::__construct(); |
|
26 | - if (!empty($prefix)) { |
|
27 | - $this->prefix = $prefix; |
|
28 | - } |
|
29 | - } |
|
23 | + public function __construct($prefix = null) |
|
24 | + { |
|
25 | + parent::__construct(); |
|
26 | + if (!empty($prefix)) { |
|
27 | + $this->prefix = $prefix; |
|
28 | + } |
|
29 | + } |
|
30 | 30 | |
31 | - protected function parseContent($content): string |
|
32 | - { |
|
33 | - $pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/'; |
|
31 | + protected function parseContent($content): string |
|
32 | + { |
|
33 | + $pattern = '/@' . preg_quote($this->getPrefix(), '/') . '\\\\([a-z]+)\\s*(.*)$/'; |
|
34 | 34 | |
35 | - $output_file = ''; |
|
35 | + $output_file = ''; |
|
36 | 36 | |
37 | - foreach (token_get_all($content) as $token) { |
|
38 | - $output = ''; |
|
37 | + foreach (token_get_all($content) as $token) { |
|
38 | + $output = ''; |
|
39 | 39 | |
40 | - if (is_array($token)) { |
|
41 | - switch ($token[0]) { |
|
42 | - case T_DOC_COMMENT: |
|
43 | - case T_COMMENT: |
|
44 | - foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
45 | - if ($index % 2) { |
|
46 | - $output .= $line; |
|
47 | - } else { |
|
48 | - $match = []; |
|
49 | - if (preg_match($pattern, $line, $match) === 1) { |
|
50 | - if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
51 | - $output .= $line; |
|
52 | - } else { |
|
53 | - $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line); |
|
54 | - } |
|
55 | - } else { |
|
56 | - $output .= $line; |
|
57 | - } |
|
58 | - } |
|
59 | - } |
|
60 | - break; |
|
40 | + if (is_array($token)) { |
|
41 | + switch ($token[0]) { |
|
42 | + case T_DOC_COMMENT: |
|
43 | + case T_COMMENT: |
|
44 | + foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
45 | + if ($index % 2) { |
|
46 | + $output .= $line; |
|
47 | + } else { |
|
48 | + $match = []; |
|
49 | + if (preg_match($pattern, $line, $match) === 1) { |
|
50 | + if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
51 | + $output .= $line; |
|
52 | + } else { |
|
53 | + $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line); |
|
54 | + } |
|
55 | + } else { |
|
56 | + $output .= $line; |
|
57 | + } |
|
58 | + } |
|
59 | + } |
|
60 | + break; |
|
61 | 61 | |
62 | - default: |
|
63 | - $output .= $token[1]; |
|
64 | - } |
|
65 | - } else { |
|
66 | - $output .= $token; |
|
67 | - } |
|
62 | + default: |
|
63 | + $output .= $token[1]; |
|
64 | + } |
|
65 | + } else { |
|
66 | + $output .= $token; |
|
67 | + } |
|
68 | 68 | |
69 | - if ($this->getState()) { |
|
70 | - $output_file .= $output; |
|
71 | - } else { |
|
72 | - $output_file .= '/* ' . $output . ' */'; |
|
73 | - } |
|
74 | - } |
|
69 | + if ($this->getState()) { |
|
70 | + $output_file .= $output; |
|
71 | + } else { |
|
72 | + $output_file .= '/* ' . $output . ' */'; |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - return $output_file; |
|
77 | - } |
|
76 | + return $output_file; |
|
77 | + } |
|
78 | 78 | |
79 | - public function getPrefix() |
|
80 | - { |
|
81 | - return $this->prefix; |
|
82 | - } |
|
79 | + public function getPrefix() |
|
80 | + { |
|
81 | + return $this->prefix; |
|
82 | + } |
|
83 | 83 | |
84 | - public function setPrefix($prefix): void |
|
85 | - { |
|
86 | - $this->prefix = $prefix; |
|
87 | - } |
|
84 | + public function setPrefix($prefix): void |
|
85 | + { |
|
86 | + $this->prefix = $prefix; |
|
87 | + } |
|
88 | 88 | |
89 | 89 | } |
@@ -22,365 +22,365 @@ |
||
22 | 22 | class Parser extends AbstractEntity implements IParser |
23 | 23 | { |
24 | 24 | |
25 | - const COMMENT_TAG = 'rest'; |
|
25 | + const COMMENT_TAG = 'rest'; |
|
26 | 26 | |
27 | 27 | // transient |
28 | - /** @var Statement[] */ |
|
29 | - public $statements = []; |
|
30 | - /** |
|
31 | - * @var ParserClass[] |
|
32 | - */ |
|
33 | - public $classes = []; |
|
34 | - /** |
|
35 | - * @var ParserFunction[] |
|
36 | - */ |
|
37 | - public $Functions = []; |
|
38 | - /** |
|
39 | - * Directories available to all parse calls |
|
40 | - * |
|
41 | - * @var string[] |
|
42 | - */ |
|
43 | - protected $common_dirs = []; |
|
28 | + /** @var Statement[] */ |
|
29 | + public $statements = []; |
|
30 | + /** |
|
31 | + * @var ParserClass[] |
|
32 | + */ |
|
33 | + public $classes = []; |
|
34 | + /** |
|
35 | + * @var ParserFunction[] |
|
36 | + */ |
|
37 | + public $Functions = []; |
|
38 | + /** |
|
39 | + * Directories available to all parse calls |
|
40 | + * |
|
41 | + * @var string[] |
|
42 | + */ |
|
43 | + protected $common_dirs = []; |
|
44 | 44 | // States |
45 | - private $current_file; |
|
46 | - private $files_queued = []; |
|
47 | - private $files_done = []; |
|
48 | - private $dirs = []; |
|
49 | - /** |
|
50 | - * @var Statement[]|null |
|
51 | - */ |
|
52 | - private $lastStatements = []; |
|
53 | - /** |
|
54 | - * @var AbstractPreprocessor |
|
55 | - */ |
|
56 | - private $Preprocessor; |
|
57 | - |
|
58 | - public function __construct(array $dirs = []) |
|
59 | - { |
|
60 | - foreach ($dirs as $dir) { |
|
61 | - $this->common_dirs[] = realpath($dir); |
|
62 | - } |
|
63 | - |
|
64 | - $this->Preprocessor = new Preprocessor(self::COMMENT_TAG); |
|
65 | - } |
|
66 | - |
|
67 | - public function addDirs(array $dirs): void |
|
68 | - { |
|
69 | - foreach ($dirs as $dir) { |
|
70 | - $this->common_dirs[] = realpath($dir); |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @throws Exception |
|
76 | - */ |
|
77 | - public function parse($file, array $dirs = [], array $defines = []): array |
|
78 | - { |
|
79 | - $this->dirs = $this->common_dirs; |
|
80 | - foreach ($dirs as $dir) { |
|
81 | - $this->dirs[] = realpath($dir); |
|
82 | - } |
|
83 | - |
|
84 | - $this->parseFiles(array($file), $defines); |
|
85 | - |
|
86 | - // Inherit classes |
|
87 | - foreach ($this->classes as $Class) { |
|
88 | - $this->inherit($Class); |
|
89 | - } |
|
90 | - |
|
91 | - // Expand functions with used and seen functions/methods. |
|
92 | - foreach ($this->classes as $Class) { |
|
93 | - foreach ($Class->methods as $Method) { |
|
94 | - $Method->statements = $this->expand($Method->statements, $Class); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - return $this->extractStatements(); |
|
99 | - } |
|
100 | - |
|
101 | - private function parseFiles(array $files, array $defines = []): void |
|
102 | - { |
|
103 | - $this->files_queued = $files; |
|
104 | - |
|
105 | - $index = 0; |
|
106 | - while (($file = array_shift($this->files_queued)) !== null) { |
|
107 | - $file = realpath($file); |
|
108 | - |
|
109 | - // @todo Test if this works |
|
110 | - if (in_array($file, $this->files_done, true)) { |
|
111 | - continue; |
|
112 | - } |
|
113 | - |
|
114 | - $this->current_file = $file; |
|
115 | - $this->files_done[] = $file; |
|
116 | - ++$index; |
|
117 | - |
|
118 | - $this->Preprocessor->resetDefines(); |
|
119 | - $this->Preprocessor->addDefines($defines); |
|
120 | - $source = $this->Preprocessor->preprocessFile($file); |
|
121 | - |
|
122 | - $this->parseTokens($source); |
|
123 | - |
|
124 | - if ($this->lastStatements !== null) { |
|
125 | - array_push($this->statements, ...$this->lastStatements); |
|
126 | - $this->lastStatements = []; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - $this->current_file = null; |
|
131 | - } |
|
132 | - |
|
133 | - private function parseTokens($source): void |
|
134 | - { |
|
135 | - $mode = null; |
|
136 | - $namespace = ''; |
|
137 | - |
|
138 | - $tokens = token_get_all($source); |
|
139 | - $token = reset($tokens); |
|
140 | - while ($token) { |
|
141 | - switch ($token[0]) { |
|
142 | - case T_NAMESPACE: |
|
143 | - $mode = T_NAMESPACE; |
|
144 | - break; |
|
145 | - |
|
146 | - case T_NS_SEPARATOR: |
|
147 | - case T_STRING: |
|
148 | - if ($mode === T_NAMESPACE) { |
|
149 | - $namespace .= $token[1]; |
|
150 | - } |
|
151 | - break; |
|
152 | - |
|
153 | - case ';': |
|
154 | - $mode = null; |
|
155 | - break; |
|
156 | - |
|
157 | - case T_CLASS: |
|
158 | - case T_INTERFACE: |
|
159 | - $Class = new Entity\ParserClass($this, $tokens, $this->lastStatements); |
|
160 | - $this->classes[strtolower($Class->name)] = $Class; |
|
161 | - $this->lastStatements = []; |
|
162 | - break; |
|
163 | - |
|
164 | - case T_FUNCTION: |
|
165 | - $Function = new ParserFunction($this, $tokens, $this->lastStatements); |
|
166 | - $this->Functions[strtolower($Function->name)] = $Function; |
|
167 | - $this->lastStatements = []; |
|
168 | - break; |
|
169 | - |
|
170 | - case T_COMMENT: |
|
171 | - array_push($this->statements, ...$this->lastStatements); |
|
172 | - $this->lastStatements = []; |
|
173 | - $statements = $this->tokenToStatements($token); |
|
174 | - $this->queueClassesFromComments($statements); |
|
175 | - array_push($this->statements, ...$statements); |
|
176 | - break; |
|
177 | - |
|
178 | - case T_DOC_COMMENT: |
|
179 | - array_push($this->statements, ...$this->lastStatements); |
|
180 | - $statements = $this->tokenToStatements($token); |
|
181 | - $this->queueClassesFromComments($statements); |
|
182 | - $this->lastStatements = $statements; |
|
183 | - break; |
|
184 | - } |
|
185 | - |
|
186 | - $token = next($tokens); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Convert a T_*_COMMENT string to an array of Statements |
|
192 | - * @param array $token |
|
193 | - * @return Statement[] |
|
194 | - */ |
|
195 | - public function tokenToStatements($token): array |
|
196 | - { |
|
197 | - [, $comment, $commentLineNumber] = $token; |
|
198 | - $commentLines = []; |
|
199 | - |
|
200 | - $match = []; |
|
201 | - if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) { |
|
202 | - $lines = explode("\n", $match[0]); |
|
203 | - foreach ($lines as $line) { |
|
204 | - if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1) |
|
205 | - && !empty($match[1])) { |
|
206 | - $commentLines[] = trim($match[1]); |
|
207 | - } |
|
208 | - } |
|
209 | - } elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) { |
|
210 | - $commentLines[] = trim($match[1]); |
|
211 | - } |
|
212 | - // to commands |
|
213 | - $match = []; |
|
214 | - $command = null; |
|
215 | - $data = ''; |
|
216 | - $commandLineNumber = 0; |
|
217 | - $statements = []; |
|
218 | - foreach ($commentLines as $lineNumber => $line) { |
|
219 | - // If new @-command, store any old and start new |
|
220 | - if ($command !== null |
|
221 | - && chr(ord($line)) === '@' |
|
222 | - ) { |
|
223 | - $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber); |
|
224 | - $command = null; |
|
225 | - $data = ''; |
|
226 | - } |
|
227 | - |
|
228 | - if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) { |
|
229 | - [, $command, $data] = $match; |
|
230 | - $commandLineNumber = $lineNumber; |
|
231 | - } elseif ($command !== null) { |
|
232 | - if ($lineNumber < count($commentLines) - 1) { |
|
233 | - $data .= ' ' . $line; |
|
234 | - } else { |
|
235 | - $data .= preg_replace('~\s*\**\/\s*$~', '', $line); |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - if ($command !== null) { |
|
241 | - $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber); |
|
242 | - } |
|
243 | - |
|
244 | - return $statements; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Add to the queue any classes based on the commands. |
|
249 | - * @param Statement[] $Statements |
|
250 | - */ |
|
251 | - public function queueClassesFromComments(array $Statements): void |
|
252 | - { |
|
253 | - foreach ($Statements as $Statement) { |
|
254 | - if (in_array($Statement->getCommand(), array('uses', 'see'))) { |
|
255 | - $match = []; |
|
256 | - if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1) |
|
257 | - && !in_array($match[1], array('self', '$this'))) { |
|
258 | - $this->queueClass($match[1]); |
|
259 | - } |
|
260 | - } |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - public function queueClass($classname): void |
|
265 | - { |
|
266 | - foreach ($this->dirs as $dir) { |
|
267 | - $paths = array( |
|
268 | - $dir . DIRECTORY_SEPARATOR . $classname . '.php', |
|
269 | - $dir . DIRECTORY_SEPARATOR . $classname . '.class.php', |
|
270 | - ); |
|
271 | - |
|
272 | - foreach ($paths as $path) { |
|
273 | - $realpath = realpath($path); |
|
274 | - if (in_array($realpath, $this->files_done, true)) { |
|
275 | - return; |
|
276 | - } |
|
277 | - |
|
278 | - if (is_file($realpath)) { |
|
279 | - $this->files_queued[] = $realpath; |
|
280 | - return; |
|
281 | - } |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - // assume it's a class; |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Inherit the statements |
|
290 | - * @param ParserClass $class |
|
291 | - */ |
|
292 | - private function inherit(ParserClass $class): void |
|
293 | - { |
|
294 | - $inherits = array_merge(array($class->extends), $class->implements); |
|
295 | - while (($inherit = array_shift($inherits)) !== null) { |
|
296 | - if (isset($this->classes[strtolower($inherit)])) { |
|
297 | - $inheritedClass = $this->classes[strtolower($inherit)]; |
|
298 | - $this->inherit($inheritedClass); |
|
299 | - |
|
300 | - foreach ($inheritedClass->methods as $name => $Method) { |
|
301 | - if (!isset($class->methods[$name])) { |
|
302 | - $class->methods[$name] = $Method; |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Expands a set of comments with comments of methods referred to by rest\uses statements. |
|
311 | - * |
|
312 | - * @param Statement[] $Statements |
|
313 | - * @return Statement[] |
|
314 | - * @throws Exception |
|
315 | - * @throws Exception |
|
316 | - * @throws Exception |
|
317 | - */ |
|
318 | - private function expand(array $Statements, ?ParserClass $Self = null): array |
|
319 | - { |
|
320 | - $output = []; |
|
321 | - |
|
322 | - $match = []; |
|
323 | - foreach ($Statements as $Statement) { |
|
324 | - if (in_array($Statement->getCommand(), array('uses', 'see'))) { |
|
325 | - if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) { |
|
326 | - if (count($match) >= 3) { |
|
327 | - $Class = null; |
|
328 | - if (in_array($match[1], array('$this', 'self', 'static'))) { |
|
329 | - $Class = $Self; |
|
330 | - } elseif (isset($this->classes[$match[1]])) { |
|
331 | - $Class = $this->classes[$match[1]]; |
|
332 | - } |
|
333 | - |
|
334 | - if ($Class) { |
|
335 | - if (isset($Class->methods[$match[3]])) { |
|
336 | - $Method = $Class->methods[$match[3]]; |
|
337 | - $Method->statements = $this->expand($Method->statements, $Class); |
|
338 | - array_push($output, ...$Method->statements); |
|
339 | - } else { |
|
340 | - throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found"); |
|
341 | - } |
|
342 | - } else { |
|
343 | - throw new Exception("Class '{$match[1]}' not found"); |
|
344 | - } |
|
345 | - } elseif (isset($this->Functions[$match[1]])) { |
|
346 | - $Function = $this->Functions[$match[1]]; |
|
347 | - $Function->statements = $this->expand($Function->statements); |
|
348 | - array_push($output, ...$Function->statements); |
|
349 | - } else { |
|
350 | - throw new Exception("Function '{$match[1]}' not found"); |
|
351 | - } |
|
352 | - } |
|
353 | - } else { |
|
354 | - $output[] = $Statement; |
|
355 | - } |
|
356 | - } |
|
357 | - |
|
358 | - return $output; |
|
359 | - } |
|
360 | - |
|
361 | - private function extractStatements(): array |
|
362 | - { |
|
363 | - // Core comments |
|
364 | - $statements = $this->statements; |
|
365 | - |
|
366 | - // Functions |
|
367 | - foreach ($this->Functions as $Function) { |
|
368 | - if ($Function->hasCommand('method')) { |
|
369 | - array_push($statements, ...$Function->statements); |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - // Classes |
|
374 | - foreach ($this->classes as $class) { |
|
375 | - array_push($statements, ...$class->statements); |
|
376 | - foreach ($class->methods as $method) { |
|
377 | - if ($method->hasCommand('method')) { |
|
378 | - array_push($statements, ...$method->statements); |
|
379 | - } |
|
380 | - } |
|
381 | - } |
|
382 | - |
|
383 | - return $statements; |
|
384 | - } |
|
45 | + private $current_file; |
|
46 | + private $files_queued = []; |
|
47 | + private $files_done = []; |
|
48 | + private $dirs = []; |
|
49 | + /** |
|
50 | + * @var Statement[]|null |
|
51 | + */ |
|
52 | + private $lastStatements = []; |
|
53 | + /** |
|
54 | + * @var AbstractPreprocessor |
|
55 | + */ |
|
56 | + private $Preprocessor; |
|
57 | + |
|
58 | + public function __construct(array $dirs = []) |
|
59 | + { |
|
60 | + foreach ($dirs as $dir) { |
|
61 | + $this->common_dirs[] = realpath($dir); |
|
62 | + } |
|
63 | + |
|
64 | + $this->Preprocessor = new Preprocessor(self::COMMENT_TAG); |
|
65 | + } |
|
66 | + |
|
67 | + public function addDirs(array $dirs): void |
|
68 | + { |
|
69 | + foreach ($dirs as $dir) { |
|
70 | + $this->common_dirs[] = realpath($dir); |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @throws Exception |
|
76 | + */ |
|
77 | + public function parse($file, array $dirs = [], array $defines = []): array |
|
78 | + { |
|
79 | + $this->dirs = $this->common_dirs; |
|
80 | + foreach ($dirs as $dir) { |
|
81 | + $this->dirs[] = realpath($dir); |
|
82 | + } |
|
83 | + |
|
84 | + $this->parseFiles(array($file), $defines); |
|
85 | + |
|
86 | + // Inherit classes |
|
87 | + foreach ($this->classes as $Class) { |
|
88 | + $this->inherit($Class); |
|
89 | + } |
|
90 | + |
|
91 | + // Expand functions with used and seen functions/methods. |
|
92 | + foreach ($this->classes as $Class) { |
|
93 | + foreach ($Class->methods as $Method) { |
|
94 | + $Method->statements = $this->expand($Method->statements, $Class); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + return $this->extractStatements(); |
|
99 | + } |
|
100 | + |
|
101 | + private function parseFiles(array $files, array $defines = []): void |
|
102 | + { |
|
103 | + $this->files_queued = $files; |
|
104 | + |
|
105 | + $index = 0; |
|
106 | + while (($file = array_shift($this->files_queued)) !== null) { |
|
107 | + $file = realpath($file); |
|
108 | + |
|
109 | + // @todo Test if this works |
|
110 | + if (in_array($file, $this->files_done, true)) { |
|
111 | + continue; |
|
112 | + } |
|
113 | + |
|
114 | + $this->current_file = $file; |
|
115 | + $this->files_done[] = $file; |
|
116 | + ++$index; |
|
117 | + |
|
118 | + $this->Preprocessor->resetDefines(); |
|
119 | + $this->Preprocessor->addDefines($defines); |
|
120 | + $source = $this->Preprocessor->preprocessFile($file); |
|
121 | + |
|
122 | + $this->parseTokens($source); |
|
123 | + |
|
124 | + if ($this->lastStatements !== null) { |
|
125 | + array_push($this->statements, ...$this->lastStatements); |
|
126 | + $this->lastStatements = []; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + $this->current_file = null; |
|
131 | + } |
|
132 | + |
|
133 | + private function parseTokens($source): void |
|
134 | + { |
|
135 | + $mode = null; |
|
136 | + $namespace = ''; |
|
137 | + |
|
138 | + $tokens = token_get_all($source); |
|
139 | + $token = reset($tokens); |
|
140 | + while ($token) { |
|
141 | + switch ($token[0]) { |
|
142 | + case T_NAMESPACE: |
|
143 | + $mode = T_NAMESPACE; |
|
144 | + break; |
|
145 | + |
|
146 | + case T_NS_SEPARATOR: |
|
147 | + case T_STRING: |
|
148 | + if ($mode === T_NAMESPACE) { |
|
149 | + $namespace .= $token[1]; |
|
150 | + } |
|
151 | + break; |
|
152 | + |
|
153 | + case ';': |
|
154 | + $mode = null; |
|
155 | + break; |
|
156 | + |
|
157 | + case T_CLASS: |
|
158 | + case T_INTERFACE: |
|
159 | + $Class = new Entity\ParserClass($this, $tokens, $this->lastStatements); |
|
160 | + $this->classes[strtolower($Class->name)] = $Class; |
|
161 | + $this->lastStatements = []; |
|
162 | + break; |
|
163 | + |
|
164 | + case T_FUNCTION: |
|
165 | + $Function = new ParserFunction($this, $tokens, $this->lastStatements); |
|
166 | + $this->Functions[strtolower($Function->name)] = $Function; |
|
167 | + $this->lastStatements = []; |
|
168 | + break; |
|
169 | + |
|
170 | + case T_COMMENT: |
|
171 | + array_push($this->statements, ...$this->lastStatements); |
|
172 | + $this->lastStatements = []; |
|
173 | + $statements = $this->tokenToStatements($token); |
|
174 | + $this->queueClassesFromComments($statements); |
|
175 | + array_push($this->statements, ...$statements); |
|
176 | + break; |
|
177 | + |
|
178 | + case T_DOC_COMMENT: |
|
179 | + array_push($this->statements, ...$this->lastStatements); |
|
180 | + $statements = $this->tokenToStatements($token); |
|
181 | + $this->queueClassesFromComments($statements); |
|
182 | + $this->lastStatements = $statements; |
|
183 | + break; |
|
184 | + } |
|
185 | + |
|
186 | + $token = next($tokens); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Convert a T_*_COMMENT string to an array of Statements |
|
192 | + * @param array $token |
|
193 | + * @return Statement[] |
|
194 | + */ |
|
195 | + public function tokenToStatements($token): array |
|
196 | + { |
|
197 | + [, $comment, $commentLineNumber] = $token; |
|
198 | + $commentLines = []; |
|
199 | + |
|
200 | + $match = []; |
|
201 | + if (preg_match('~^/\*\*?\s*(.*)\s*\*\/$~sm', $comment, $match) === 1) { |
|
202 | + $lines = explode("\n", $match[0]); |
|
203 | + foreach ($lines as $line) { |
|
204 | + if ((preg_match('~^\s*\*?\s*(.*?)\s*$~', $line, $match) === 1) |
|
205 | + && !empty($match[1])) { |
|
206 | + $commentLines[] = trim($match[1]); |
|
207 | + } |
|
208 | + } |
|
209 | + } elseif (preg_match('~^//\s*(.*)$~', $comment, $match) === 1) { |
|
210 | + $commentLines[] = trim($match[1]); |
|
211 | + } |
|
212 | + // to commands |
|
213 | + $match = []; |
|
214 | + $command = null; |
|
215 | + $data = ''; |
|
216 | + $commandLineNumber = 0; |
|
217 | + $statements = []; |
|
218 | + foreach ($commentLines as $lineNumber => $line) { |
|
219 | + // If new @-command, store any old and start new |
|
220 | + if ($command !== null |
|
221 | + && chr(ord($line)) === '@' |
|
222 | + ) { |
|
223 | + $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber); |
|
224 | + $command = null; |
|
225 | + $data = ''; |
|
226 | + } |
|
227 | + |
|
228 | + if (preg_match('~^@' . preg_quote(self::COMMENT_TAG, '~') . '\\\\([a-z][-a-z]*[?!]?)\\s*(.*)$~', $line, $match) === 1) { |
|
229 | + [, $command, $data] = $match; |
|
230 | + $commandLineNumber = $lineNumber; |
|
231 | + } elseif ($command !== null) { |
|
232 | + if ($lineNumber < count($commentLines) - 1) { |
|
233 | + $data .= ' ' . $line; |
|
234 | + } else { |
|
235 | + $data .= preg_replace('~\s*\**\/\s*$~', '', $line); |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + if ($command !== null) { |
|
241 | + $statements[] = new Statement($command, $data, $this->current_file, $commentLineNumber + $commandLineNumber); |
|
242 | + } |
|
243 | + |
|
244 | + return $statements; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Add to the queue any classes based on the commands. |
|
249 | + * @param Statement[] $Statements |
|
250 | + */ |
|
251 | + public function queueClassesFromComments(array $Statements): void |
|
252 | + { |
|
253 | + foreach ($Statements as $Statement) { |
|
254 | + if (in_array($Statement->getCommand(), array('uses', 'see'))) { |
|
255 | + $match = []; |
|
256 | + if ((preg_match('~^(\w+)(::|->)?(\w+)?(?:\(\))?$~', $Statement->getData(), $match) === 1) |
|
257 | + && !in_array($match[1], array('self', '$this'))) { |
|
258 | + $this->queueClass($match[1]); |
|
259 | + } |
|
260 | + } |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + public function queueClass($classname): void |
|
265 | + { |
|
266 | + foreach ($this->dirs as $dir) { |
|
267 | + $paths = array( |
|
268 | + $dir . DIRECTORY_SEPARATOR . $classname . '.php', |
|
269 | + $dir . DIRECTORY_SEPARATOR . $classname . '.class.php', |
|
270 | + ); |
|
271 | + |
|
272 | + foreach ($paths as $path) { |
|
273 | + $realpath = realpath($path); |
|
274 | + if (in_array($realpath, $this->files_done, true)) { |
|
275 | + return; |
|
276 | + } |
|
277 | + |
|
278 | + if (is_file($realpath)) { |
|
279 | + $this->files_queued[] = $realpath; |
|
280 | + return; |
|
281 | + } |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + // assume it's a class; |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Inherit the statements |
|
290 | + * @param ParserClass $class |
|
291 | + */ |
|
292 | + private function inherit(ParserClass $class): void |
|
293 | + { |
|
294 | + $inherits = array_merge(array($class->extends), $class->implements); |
|
295 | + while (($inherit = array_shift($inherits)) !== null) { |
|
296 | + if (isset($this->classes[strtolower($inherit)])) { |
|
297 | + $inheritedClass = $this->classes[strtolower($inherit)]; |
|
298 | + $this->inherit($inheritedClass); |
|
299 | + |
|
300 | + foreach ($inheritedClass->methods as $name => $Method) { |
|
301 | + if (!isset($class->methods[$name])) { |
|
302 | + $class->methods[$name] = $Method; |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Expands a set of comments with comments of methods referred to by rest\uses statements. |
|
311 | + * |
|
312 | + * @param Statement[] $Statements |
|
313 | + * @return Statement[] |
|
314 | + * @throws Exception |
|
315 | + * @throws Exception |
|
316 | + * @throws Exception |
|
317 | + */ |
|
318 | + private function expand(array $Statements, ?ParserClass $Self = null): array |
|
319 | + { |
|
320 | + $output = []; |
|
321 | + |
|
322 | + $match = []; |
|
323 | + foreach ($Statements as $Statement) { |
|
324 | + if (in_array($Statement->getCommand(), array('uses', 'see'))) { |
|
325 | + if (preg_match('/^((?:\\w+)|\$this)(?:(::|->)(\\w+))?(?:\\(\\))?$/', strtolower($Statement->getData()), $match) === 1) { |
|
326 | + if (count($match) >= 3) { |
|
327 | + $Class = null; |
|
328 | + if (in_array($match[1], array('$this', 'self', 'static'))) { |
|
329 | + $Class = $Self; |
|
330 | + } elseif (isset($this->classes[$match[1]])) { |
|
331 | + $Class = $this->classes[$match[1]]; |
|
332 | + } |
|
333 | + |
|
334 | + if ($Class) { |
|
335 | + if (isset($Class->methods[$match[3]])) { |
|
336 | + $Method = $Class->methods[$match[3]]; |
|
337 | + $Method->statements = $this->expand($Method->statements, $Class); |
|
338 | + array_push($output, ...$Method->statements); |
|
339 | + } else { |
|
340 | + throw new Exception("Method '{$match[3]}' for class '{$match[1]}' not found"); |
|
341 | + } |
|
342 | + } else { |
|
343 | + throw new Exception("Class '{$match[1]}' not found"); |
|
344 | + } |
|
345 | + } elseif (isset($this->Functions[$match[1]])) { |
|
346 | + $Function = $this->Functions[$match[1]]; |
|
347 | + $Function->statements = $this->expand($Function->statements); |
|
348 | + array_push($output, ...$Function->statements); |
|
349 | + } else { |
|
350 | + throw new Exception("Function '{$match[1]}' not found"); |
|
351 | + } |
|
352 | + } |
|
353 | + } else { |
|
354 | + $output[] = $Statement; |
|
355 | + } |
|
356 | + } |
|
357 | + |
|
358 | + return $output; |
|
359 | + } |
|
360 | + |
|
361 | + private function extractStatements(): array |
|
362 | + { |
|
363 | + // Core comments |
|
364 | + $statements = $this->statements; |
|
365 | + |
|
366 | + // Functions |
|
367 | + foreach ($this->Functions as $Function) { |
|
368 | + if ($Function->hasCommand('method')) { |
|
369 | + array_push($statements, ...$Function->statements); |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + // Classes |
|
374 | + foreach ($this->classes as $class) { |
|
375 | + array_push($statements, ...$class->statements); |
|
376 | + foreach ($class->methods as $method) { |
|
377 | + if ($method->hasCommand('method')) { |
|
378 | + array_push($statements, ...$method->statements); |
|
379 | + } |
|
380 | + } |
|
381 | + } |
|
382 | + |
|
383 | + return $statements; |
|
384 | + } |
|
385 | 385 | |
386 | 386 | } |
@@ -15,29 +15,29 @@ |
||
15 | 15 | class AbstractEntity |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var Statement[] |
|
20 | - */ |
|
21 | - public $statements = []; |
|
18 | + /** |
|
19 | + * @var Statement[] |
|
20 | + */ |
|
21 | + public $statements = []; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Returns true if a statement with the specified command exists. |
|
25 | - * @param string $command |
|
26 | - * @return boolean |
|
27 | - */ |
|
28 | - public function hasCommand(string $command): bool |
|
29 | - { |
|
30 | - foreach ($this->statements as $statement) { |
|
31 | - if ($statement->getCommand() === $command) { |
|
32 | - return true; |
|
33 | - } |
|
34 | - } |
|
23 | + /** |
|
24 | + * Returns true if a statement with the specified command exists. |
|
25 | + * @param string $command |
|
26 | + * @return boolean |
|
27 | + */ |
|
28 | + public function hasCommand(string $command): bool |
|
29 | + { |
|
30 | + foreach ($this->statements as $statement) { |
|
31 | + if ($statement->getCommand() === $command) { |
|
32 | + return true; |
|
33 | + } |
|
34 | + } |
|
35 | 35 | |
36 | - return false; |
|
37 | - } |
|
36 | + return false; |
|
37 | + } |
|
38 | 38 | |
39 | - public function getStatements(): array |
|
40 | - { |
|
41 | - return $this->statements; |
|
42 | - } |
|
39 | + public function getStatements(): array |
|
40 | + { |
|
41 | + return $this->statements; |
|
42 | + } |
|
43 | 43 | } |
@@ -15,111 +15,111 @@ |
||
15 | 15 | class ParserClass extends AbstractEntity |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - public $name; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var ParserFunction[] |
|
25 | - */ |
|
26 | - public $methods = []; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $extends; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - public $implements = []; |
|
37 | - private $lastStatements = []; |
|
38 | - |
|
39 | - public function __construct(Parser $Parser, &$tokens, $statements) |
|
40 | - { |
|
41 | - $lastStatements = []; |
|
42 | - |
|
43 | - if ($statements) { |
|
44 | - $this->statements = array_merge($this->statements, $statements); |
|
45 | - } |
|
46 | - |
|
47 | - $depth = 0; |
|
48 | - |
|
49 | - $mode = T_CLASS; |
|
50 | - |
|
51 | - $token = current($tokens); |
|
52 | - while ($token) { |
|
53 | - switch ($token[0]) { |
|
54 | - case T_STRING: |
|
55 | - switch ($mode) { |
|
56 | - case T_CLASS: |
|
57 | - $this->name = $token[1]; |
|
58 | - $mode = null; |
|
59 | - break; |
|
60 | - |
|
61 | - case T_EXTENDS: |
|
62 | - $Parser->queueClass($token[1]); |
|
63 | - $this->extends = $token[1]; |
|
64 | - $mode = null; |
|
65 | - break; |
|
66 | - |
|
67 | - case T_IMPLEMENTS: |
|
68 | - $Parser->queueClass($token[1]); |
|
69 | - $this->implements[] = $token[1]; |
|
70 | - break; |
|
71 | - } |
|
72 | - break; |
|
73 | - |
|
74 | - case '{': |
|
75 | - case T_CURLY_OPEN: |
|
76 | - case T_DOLLAR_OPEN_CURLY_BRACES: |
|
77 | - case T_STRING_VARNAME: |
|
78 | - $mode = null; |
|
79 | - ++$depth; |
|
80 | - break; |
|
81 | - |
|
82 | - case '}': |
|
83 | - --$depth; |
|
84 | - if ($depth === 0) { |
|
85 | - array_push($this->statements, ...$lastStatements); |
|
86 | - return; |
|
87 | - } |
|
88 | - break; |
|
89 | - |
|
90 | - case T_FUNCTION: |
|
91 | - $Method = new ParserFunction($Parser, $tokens, $lastStatements); |
|
92 | - $this->methods[strtolower($Method->name)] = $Method; |
|
93 | - $lastStatements = []; |
|
94 | - break; |
|
95 | - |
|
96 | - case T_EXTENDS: |
|
97 | - $mode = T_EXTENDS; |
|
98 | - break; |
|
99 | - |
|
100 | - case T_IMPLEMENTS: |
|
101 | - $mode = T_IMPLEMENTS; |
|
102 | - break; |
|
103 | - |
|
104 | - case T_COMMENT: |
|
105 | - array_push($this->statements, ...$lastStatements); |
|
106 | - $lastStatements = []; |
|
107 | - $statements = $Parser->tokenToStatements($token); |
|
108 | - $Parser->queueClassesFromComments($statements); |
|
109 | - array_push($this->statements, ...$statements); |
|
110 | - break; |
|
111 | - |
|
112 | - case T_DOC_COMMENT: |
|
113 | - array_push($this->statements, ...$lastStatements); |
|
114 | - $statements = $Parser->tokenToStatements($token); |
|
115 | - $Parser->queueClassesFromComments($statements); |
|
116 | - $lastStatements = $statements; |
|
117 | - break; |
|
118 | - } |
|
119 | - |
|
120 | - $token = next($tokens); |
|
121 | - } |
|
122 | - |
|
123 | - array_push($this->statements, ...$lastStatements); |
|
124 | - } |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + public $name; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var ParserFunction[] |
|
25 | + */ |
|
26 | + public $methods = []; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $extends; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + public $implements = []; |
|
37 | + private $lastStatements = []; |
|
38 | + |
|
39 | + public function __construct(Parser $Parser, &$tokens, $statements) |
|
40 | + { |
|
41 | + $lastStatements = []; |
|
42 | + |
|
43 | + if ($statements) { |
|
44 | + $this->statements = array_merge($this->statements, $statements); |
|
45 | + } |
|
46 | + |
|
47 | + $depth = 0; |
|
48 | + |
|
49 | + $mode = T_CLASS; |
|
50 | + |
|
51 | + $token = current($tokens); |
|
52 | + while ($token) { |
|
53 | + switch ($token[0]) { |
|
54 | + case T_STRING: |
|
55 | + switch ($mode) { |
|
56 | + case T_CLASS: |
|
57 | + $this->name = $token[1]; |
|
58 | + $mode = null; |
|
59 | + break; |
|
60 | + |
|
61 | + case T_EXTENDS: |
|
62 | + $Parser->queueClass($token[1]); |
|
63 | + $this->extends = $token[1]; |
|
64 | + $mode = null; |
|
65 | + break; |
|
66 | + |
|
67 | + case T_IMPLEMENTS: |
|
68 | + $Parser->queueClass($token[1]); |
|
69 | + $this->implements[] = $token[1]; |
|
70 | + break; |
|
71 | + } |
|
72 | + break; |
|
73 | + |
|
74 | + case '{': |
|
75 | + case T_CURLY_OPEN: |
|
76 | + case T_DOLLAR_OPEN_CURLY_BRACES: |
|
77 | + case T_STRING_VARNAME: |
|
78 | + $mode = null; |
|
79 | + ++$depth; |
|
80 | + break; |
|
81 | + |
|
82 | + case '}': |
|
83 | + --$depth; |
|
84 | + if ($depth === 0) { |
|
85 | + array_push($this->statements, ...$lastStatements); |
|
86 | + return; |
|
87 | + } |
|
88 | + break; |
|
89 | + |
|
90 | + case T_FUNCTION: |
|
91 | + $Method = new ParserFunction($Parser, $tokens, $lastStatements); |
|
92 | + $this->methods[strtolower($Method->name)] = $Method; |
|
93 | + $lastStatements = []; |
|
94 | + break; |
|
95 | + |
|
96 | + case T_EXTENDS: |
|
97 | + $mode = T_EXTENDS; |
|
98 | + break; |
|
99 | + |
|
100 | + case T_IMPLEMENTS: |
|
101 | + $mode = T_IMPLEMENTS; |
|
102 | + break; |
|
103 | + |
|
104 | + case T_COMMENT: |
|
105 | + array_push($this->statements, ...$lastStatements); |
|
106 | + $lastStatements = []; |
|
107 | + $statements = $Parser->tokenToStatements($token); |
|
108 | + $Parser->queueClassesFromComments($statements); |
|
109 | + array_push($this->statements, ...$statements); |
|
110 | + break; |
|
111 | + |
|
112 | + case T_DOC_COMMENT: |
|
113 | + array_push($this->statements, ...$lastStatements); |
|
114 | + $statements = $Parser->tokenToStatements($token); |
|
115 | + $Parser->queueClassesFromComments($statements); |
|
116 | + $lastStatements = $statements; |
|
117 | + break; |
|
118 | + } |
|
119 | + |
|
120 | + $token = next($tokens); |
|
121 | + } |
|
122 | + |
|
123 | + array_push($this->statements, ...$lastStatements); |
|
124 | + } |
|
125 | 125 | } |
@@ -15,61 +15,61 @@ |
||
15 | 15 | */ |
16 | 16 | class ParserFunction extends AbstractEntity |
17 | 17 | { |
18 | - public string|null $name = null; |
|
18 | + public string|null $name = null; |
|
19 | 19 | |
20 | - public function __construct(Parser $Parser, &$tokens, array $statements = null) |
|
21 | - { |
|
22 | - $lastStatements = []; |
|
20 | + public function __construct(Parser $Parser, &$tokens, array $statements = null) |
|
21 | + { |
|
22 | + $lastStatements = []; |
|
23 | 23 | |
24 | - if (!empty($statements)) { |
|
25 | - $this->statements = array_merge($this->statements, $statements); |
|
26 | - } |
|
24 | + if (!empty($statements)) { |
|
25 | + $this->statements = array_merge($this->statements, $statements); |
|
26 | + } |
|
27 | 27 | |
28 | - $depth = 0; |
|
28 | + $depth = 0; |
|
29 | 29 | |
30 | - $token = current($tokens); |
|
31 | - while ($token) { |
|
32 | - switch ($token[0]) { |
|
33 | - case T_STRING: |
|
34 | - if (empty($this->name)) { |
|
35 | - $this->name = $token[1]; |
|
36 | - } |
|
37 | - break; |
|
30 | + $token = current($tokens); |
|
31 | + while ($token) { |
|
32 | + switch ($token[0]) { |
|
33 | + case T_STRING: |
|
34 | + if (empty($this->name)) { |
|
35 | + $this->name = $token[1]; |
|
36 | + } |
|
37 | + break; |
|
38 | 38 | |
39 | - case '{': |
|
40 | - case T_CURLY_OPEN: |
|
41 | - case T_DOLLAR_OPEN_CURLY_BRACES: |
|
42 | - case T_STRING_VARNAME: |
|
43 | - ++$depth; |
|
44 | - break; |
|
39 | + case '{': |
|
40 | + case T_CURLY_OPEN: |
|
41 | + case T_DOLLAR_OPEN_CURLY_BRACES: |
|
42 | + case T_STRING_VARNAME: |
|
43 | + ++$depth; |
|
44 | + break; |
|
45 | 45 | |
46 | - case '}': |
|
47 | - --$depth; |
|
48 | - if ($depth === 0) { |
|
49 | - array_push($this->statements, ...$lastStatements); |
|
50 | - return; |
|
51 | - } |
|
52 | - break; |
|
46 | + case '}': |
|
47 | + --$depth; |
|
48 | + if ($depth === 0) { |
|
49 | + array_push($this->statements, ...$lastStatements); |
|
50 | + return; |
|
51 | + } |
|
52 | + break; |
|
53 | 53 | |
54 | - case T_COMMENT: |
|
55 | - array_push($this->statements, ...$lastStatements); |
|
56 | - $lastStatements = []; |
|
57 | - $statements = $Parser->tokenToStatements($token); |
|
58 | - $Parser->queueClassesFromComments($statements); |
|
59 | - array_push($this->statements, ...$statements); |
|
60 | - break; |
|
54 | + case T_COMMENT: |
|
55 | + array_push($this->statements, ...$lastStatements); |
|
56 | + $lastStatements = []; |
|
57 | + $statements = $Parser->tokenToStatements($token); |
|
58 | + $Parser->queueClassesFromComments($statements); |
|
59 | + array_push($this->statements, ...$statements); |
|
60 | + break; |
|
61 | 61 | |
62 | - case T_DOC_COMMENT: |
|
63 | - array_push($this->statements, ...$lastStatements); |
|
64 | - $statements = $Parser->tokenToStatements($token); |
|
65 | - $Parser->queueClassesFromComments($statements); |
|
66 | - $lastStatements = $statements; |
|
67 | - break; |
|
68 | - } |
|
62 | + case T_DOC_COMMENT: |
|
63 | + array_push($this->statements, ...$lastStatements); |
|
64 | + $statements = $Parser->tokenToStatements($token); |
|
65 | + $Parser->queueClassesFromComments($statements); |
|
66 | + $lastStatements = $statements; |
|
67 | + break; |
|
68 | + } |
|
69 | 69 | |
70 | - $token = next($tokens); |
|
71 | - } |
|
70 | + $token = next($tokens); |
|
71 | + } |
|
72 | 72 | |
73 | - array_push($this->statements, ...$lastStatements); |
|
74 | - } |
|
73 | + array_push($this->statements, ...$lastStatements); |
|
74 | + } |
|
75 | 75 | } |
@@ -15,7 +15,7 @@ |
||
15 | 15 | */ |
16 | 16 | class ParserFunction extends AbstractEntity |
17 | 17 | { |
18 | - public string|null $name = null; |
|
18 | + public string | null $name = null; |
|
19 | 19 | |
20 | 20 | public function __construct(Parser $Parser, &$tokens, array $statements = null) |
21 | 21 | { |
@@ -18,30 +18,30 @@ |
||
18 | 18 | class Preprocessor extends AbstractPreprocessor |
19 | 19 | { |
20 | 20 | |
21 | - protected function parseContent($content): string |
|
22 | - { |
|
23 | - $pattern = '/\\s*([a-z]+)\\s*(.*)\\s*/'; |
|
24 | - |
|
25 | - $output = ''; |
|
26 | - |
|
27 | - foreach (preg_split('/(\\R)/m', $content, null, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
28 | - if ($index % 2) { |
|
29 | - $output .= $line; |
|
30 | - } else { |
|
31 | - $match = []; |
|
32 | - if (preg_match($pattern, $line, $match) === 1) { |
|
33 | - if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
34 | - $output .= $line; |
|
35 | - } else { |
|
36 | - $output .= ''; |
|
37 | - } |
|
38 | - } else { |
|
39 | - $output .= $line; |
|
40 | - } |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - return $output; |
|
45 | - } |
|
21 | + protected function parseContent($content): string |
|
22 | + { |
|
23 | + $pattern = '/\\s*([a-z]+)\\s*(.*)\\s*/'; |
|
24 | + |
|
25 | + $output = ''; |
|
26 | + |
|
27 | + foreach (preg_split('/(\\R)/m', $content, null, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
28 | + if ($index % 2) { |
|
29 | + $output .= $line; |
|
30 | + } else { |
|
31 | + $match = []; |
|
32 | + if (preg_match($pattern, $line, $match) === 1) { |
|
33 | + if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
34 | + $output .= $line; |
|
35 | + } else { |
|
36 | + $output .= ''; |
|
37 | + } |
|
38 | + } else { |
|
39 | + $output .= $line; |
|
40 | + } |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + return $output; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | } |
@@ -17,99 +17,99 @@ |
||
17 | 17 | class Parser implements IParser |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * List of directories to scan for class files referenced in the parsed |
|
22 | - * command statements. |
|
23 | - * |
|
24 | - * @var string[] |
|
25 | - */ |
|
26 | - protected $common_dirs = []; |
|
20 | + /** |
|
21 | + * List of directories to scan for class files referenced in the parsed |
|
22 | + * command statements. |
|
23 | + * |
|
24 | + * @var string[] |
|
25 | + */ |
|
26 | + protected $common_dirs = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var AbstractPreprocessor |
|
30 | - */ |
|
31 | - private $Preprocessor; |
|
28 | + /** |
|
29 | + * @var AbstractPreprocessor |
|
30 | + */ |
|
31 | + private $Preprocessor; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Create a new text parser and set directories to scan for referenced |
|
35 | - * class files. |
|
36 | - * |
|
37 | - * @param string[] $dirs |
|
38 | - */ |
|
39 | - public function __construct(array $dirs = []) |
|
40 | - { |
|
41 | - foreach ($dirs as $dir) { |
|
42 | - $this->common_dirs[] = realpath($dir); |
|
43 | - } |
|
33 | + /** |
|
34 | + * Create a new text parser and set directories to scan for referenced |
|
35 | + * class files. |
|
36 | + * |
|
37 | + * @param string[] $dirs |
|
38 | + */ |
|
39 | + public function __construct(array $dirs = []) |
|
40 | + { |
|
41 | + foreach ($dirs as $dir) { |
|
42 | + $this->common_dirs[] = realpath($dir); |
|
43 | + } |
|
44 | 44 | |
45 | - $this->Preprocessor = new Preprocessor(); |
|
46 | - } |
|
45 | + $this->Preprocessor = new Preprocessor(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Add additional directories to scan for referenced class files. |
|
50 | - * |
|
51 | - * @param string[] $dirs |
|
52 | - */ |
|
53 | - public function addDirs(array $dirs): void |
|
54 | - { |
|
55 | - foreach ($dirs as $dir) { |
|
56 | - $this->common_dirs[] = realpath($dir); |
|
57 | - } |
|
58 | - } |
|
48 | + /** |
|
49 | + * Add additional directories to scan for referenced class files. |
|
50 | + * |
|
51 | + * @param string[] $dirs |
|
52 | + */ |
|
53 | + public function addDirs(array $dirs): void |
|
54 | + { |
|
55 | + foreach ($dirs as $dir) { |
|
56 | + $this->common_dirs[] = realpath($dir); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Parse a text file |
|
62 | - * |
|
63 | - * @param string $file |
|
64 | - * @param string[] $dirs |
|
65 | - * @param string[] $defines |
|
66 | - * @return Statement[] |
|
67 | - */ |
|
68 | - public function parse($file, array $dirs = [], array $defines = []): array |
|
69 | - { |
|
70 | - return $this->parseText(file_get_contents(realpath($file)), $dirs); |
|
71 | - } |
|
60 | + /** |
|
61 | + * Parse a text file |
|
62 | + * |
|
63 | + * @param string $file |
|
64 | + * @param string[] $dirs |
|
65 | + * @param string[] $defines |
|
66 | + * @return Statement[] |
|
67 | + */ |
|
68 | + public function parse($file, array $dirs = [], array $defines = []): array |
|
69 | + { |
|
70 | + return $this->parseText(file_get_contents(realpath($file)), $dirs); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Parse plain text |
|
75 | - * |
|
76 | - * @param string $text |
|
77 | - * @param string[] $dirs |
|
78 | - * @param string[] $defines |
|
79 | - * @return Statement[] |
|
80 | - */ |
|
81 | - public function parseText($text, array $dirs = [], array $defines = []): array |
|
82 | - { |
|
83 | - $this->Preprocessor->resetDefines(); |
|
84 | - $this->Preprocessor->addDefines($defines); |
|
85 | - $text = $this->Preprocessor->preprocess($text); |
|
73 | + /** |
|
74 | + * Parse plain text |
|
75 | + * |
|
76 | + * @param string $text |
|
77 | + * @param string[] $dirs |
|
78 | + * @param string[] $defines |
|
79 | + * @return Statement[] |
|
80 | + */ |
|
81 | + public function parseText($text, array $dirs = [], array $defines = []): array |
|
82 | + { |
|
83 | + $this->Preprocessor->resetDefines(); |
|
84 | + $this->Preprocessor->addDefines($defines); |
|
85 | + $text = $this->Preprocessor->preprocess($text); |
|
86 | 86 | |
87 | - $Statements = []; |
|
87 | + $Statements = []; |
|
88 | 88 | |
89 | - foreach (preg_split('/\\R/m', $text) as $line => $data) { |
|
90 | - $data = trim($data); |
|
91 | - $command = self::wordShift($data); |
|
92 | - if (!empty($command)) { |
|
93 | - $Statements[] = new Statement($command, $data, null, $line); |
|
94 | - } |
|
95 | - } |
|
89 | + foreach (preg_split('/\\R/m', $text) as $line => $data) { |
|
90 | + $data = trim($data); |
|
91 | + $command = self::wordShift($data); |
|
92 | + if (!empty($command)) { |
|
93 | + $Statements[] = new Statement($command, $data, null, $line); |
|
94 | + } |
|
95 | + } |
|
96 | 96 | |
97 | - return $Statements; |
|
98 | - } |
|
97 | + return $Statements; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Get the first word from a string and remove it from the string. |
|
102 | - * |
|
103 | - * @param string $data |
|
104 | - * @return boolean|string |
|
105 | - */ |
|
106 | - private static function wordShift(&$data) |
|
107 | - { |
|
108 | - if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
109 | - $data = $matches[2]; |
|
110 | - return $matches[1]; |
|
111 | - } |
|
112 | - return false; |
|
113 | - } |
|
100 | + /** |
|
101 | + * Get the first word from a string and remove it from the string. |
|
102 | + * |
|
103 | + * @param string $data |
|
104 | + * @return boolean|string |
|
105 | + */ |
|
106 | + private static function wordShift(&$data) |
|
107 | + { |
|
108 | + if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
109 | + $data = $matches[2]; |
|
110 | + return $matches[1]; |
|
111 | + } |
|
112 | + return false; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
@@ -16,27 +16,27 @@ |
||
16 | 16 | class StatementException extends Exception |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * @var Statement |
|
21 | - */ |
|
22 | - private $statement; |
|
19 | + /** |
|
20 | + * @var Statement |
|
21 | + */ |
|
22 | + private $statement; |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * @param string $message |
|
27 | - * @param int $code |
|
28 | - * @param Throwable $previous |
|
29 | - * @param Statement $statement |
|
30 | - */ |
|
31 | - public function __construct($message = "", $code = 0, $previous = null, $statement = null) |
|
32 | - { |
|
33 | - $this->statement = $statement; |
|
24 | + /** |
|
25 | + * |
|
26 | + * @param string $message |
|
27 | + * @param int $code |
|
28 | + * @param Throwable $previous |
|
29 | + * @param Statement $statement |
|
30 | + */ |
|
31 | + public function __construct($message = "", $code = 0, $previous = null, $statement = null) |
|
32 | + { |
|
33 | + $this->statement = $statement; |
|
34 | 34 | |
35 | - parent::__construct($message, $code, $previous); |
|
36 | - } |
|
35 | + parent::__construct($message, $code, $previous); |
|
36 | + } |
|
37 | 37 | |
38 | - public function getStatement(): ?Statement |
|
39 | - { |
|
40 | - return $this->statement; |
|
41 | - } |
|
38 | + public function getStatement(): ?Statement |
|
39 | + { |
|
40 | + return $this->statement; |
|
41 | + } |
|
42 | 42 | } |