Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #include "lsp_internal.h"
15 :
16 220 : static inline zend_string *lsp_phpdoc_normalized_signature_slice(const char *start, size_t length)
17 : {
18 220 : smart_str signature = {0};
19 220 : bool pending_space = false;
20 : size_t i;
21 : char c;
22 :
23 2308 : for (i = 0; i < length; i++) {
24 2088 : c = start[i];
25 2088 : if (isspace((unsigned char) c)) {
26 88 : pending_space = true;
27 :
28 88 : continue;
29 : }
30 :
31 2000 : if (pending_space && signature.s && ZSTR_LEN(signature.s) > 0 &&
32 88 : c != ')' && c != ',' && c != ':' && c != '|' && c != '&' &&
33 88 : ZSTR_VAL(signature.s)[ZSTR_LEN(signature.s) - 1] != '(' &&
34 88 : ZSTR_VAL(signature.s)[ZSTR_LEN(signature.s) - 1] != '|'
35 : ) {
36 88 : smart_str_appendc(&signature, ' ');
37 : }
38 :
39 2000 : smart_str_appendc(&signature, c);
40 2000 : pending_space = false;
41 : }
42 :
43 220 : smart_str_0(&signature);
44 :
45 220 : return signature.s ? signature.s : zend_string_init("", 0, 0);
46 : }
47 :
48 2688 : static inline bool lsp_phpdoc_modifier_before(zend_string *text, size_t end, const char *word, size_t *start)
49 : {
50 2688 : const char *value = ZSTR_VAL(text);
51 : size_t p, length;
52 :
53 2688 : p = end;
54 6624 : while (p > 0 && isspace((unsigned char) value[p - 1])) {
55 3936 : p--;
56 : }
57 :
58 2688 : length = strlen(word);
59 2688 : if (p < length) {
60 32 : return false;
61 : }
62 :
63 2656 : *start = p - length;
64 2656 : if (strncasecmp(value + *start, word, length) != 0) {
65 2164 : return false;
66 : }
67 :
68 492 : if (*start > 0 && lsp_doc_is_identifier_char(value[*start - 1])) {
69 0 : return false;
70 : }
71 :
72 492 : return p >= ZSTR_LEN(text) || !lsp_doc_is_identifier_char(value[p]);
73 : }
74 :
75 716 : static inline size_t lsp_phpdoc_class_anchor(zend_string *text, size_t class_start)
76 : {
77 : size_t anchor, modifier_start;
78 :
79 716 : anchor = class_start;
80 : while (true) {
81 1972 : if (lsp_phpdoc_modifier_before(text, anchor, "final", &modifier_start) ||
82 1480 : lsp_phpdoc_modifier_before(text, anchor, "abstract", &modifier_start) ||
83 716 : lsp_phpdoc_modifier_before(text, anchor, "readonly", &modifier_start)
84 : ) {
85 492 : anchor = modifier_start;
86 :
87 492 : continue;
88 : }
89 :
90 716 : break;
91 : }
92 :
93 716 : return anchor;
94 : }
95 :
96 716 : static inline bool lsp_phpdoc_class_docblock_bounds(zend_string *text, size_t class_start, size_t *doc_start, size_t *doc_end)
97 : {
98 716 : const char *value = ZSTR_VAL(text);
99 : size_t anchor, p, q;
100 :
101 716 : anchor = lsp_phpdoc_class_anchor(text, class_start);
102 716 : p = anchor;
103 1848 : while (p > 0 && isspace((unsigned char) value[p - 1])) {
104 1132 : p--;
105 : }
106 :
107 716 : if (p < 2 || value[p - 2] != '*' || value[p - 1] != '/') {
108 560 : return false;
109 : }
110 :
111 156 : *doc_end = p;
112 156 : q = p - 2;
113 17912 : while (q > 1) {
114 17912 : if (value[q - 2] == '/' && value[q - 1] == '*' && value[q] == '*') {
115 156 : *doc_start = q - 2;
116 :
117 156 : return true;
118 : }
119 :
120 17756 : q--;
121 : }
122 :
123 0 : return false;
124 : }
125 :
126 84 : static inline bool lsp_phpdoc_first_class_start(zend_string *text, size_t *class_start)
127 : {
128 84 : const char *keywords[4] = {"class", "interface", "trait", "enum"}, *value = ZSTR_VAL(text);
129 : size_t i, keyword_index, keyword_length;
130 :
131 8744 : for (i = 0; i + sizeof("class") - 1 < ZSTR_LEN(text); i++) {
132 43400 : for (keyword_index = 0; keyword_index < sizeof(keywords) / sizeof(keywords[0]); keyword_index++) {
133 34740 : keyword_length = strlen(keywords[keyword_index]);
134 34740 : if (i + keyword_length >= ZSTR_LEN(text) || memcmp(value + i, keywords[keyword_index], keyword_length) != 0) {
135 34656 : continue;
136 : }
137 :
138 84 : if (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '$')) {
139 0 : continue;
140 : }
141 :
142 84 : if (!lsp_text_is_word_boundary(text, i + keyword_length)) {
143 0 : continue;
144 : }
145 :
146 84 : *class_start = i;
147 :
148 84 : return true;
149 : }
150 : }
151 :
152 0 : return false;
153 : }
154 :
155 232 : static inline const char *lsp_phpdoc_method_tag_in_line(const char *line_start, const char *line_end)
156 : {
157 : const char *p;
158 : size_t tag_length;
159 :
160 232 : p = line_start;
161 420 : while (p < line_end && isspace((unsigned char) *p)) {
162 188 : p++;
163 : }
164 :
165 232 : if (p + sizeof("/**") - 1 <= line_end && memcmp(p, "/**", sizeof("/**") - 1) == 0) {
166 44 : p += sizeof("/**") - 1;
167 : }
168 :
169 232 : while (p < line_end && isspace((unsigned char) *p)) {
170 0 : p++;
171 : }
172 :
173 232 : if (p < line_end && *p == '*') {
174 188 : p++;
175 188 : if (p < line_end && *p == '/') {
176 44 : return NULL;
177 : }
178 :
179 288 : while (p < line_end && isspace((unsigned char) *p)) {
180 144 : p++;
181 : }
182 : }
183 :
184 188 : tag_length = sizeof("@method") - 1;
185 :
186 188 : if (p + tag_length > line_end || memcmp(p, "@method", tag_length) != 0) {
187 112 : return NULL;
188 : }
189 :
190 76 : if (p + tag_length < line_end && !isspace((unsigned char) p[tag_length])) {
191 0 : return NULL;
192 : }
193 :
194 76 : return p + tag_length;
195 : }
196 :
197 232 : static inline const char *lsp_phpdoc_property_tag_in_line(const char *line_start, const char *line_end)
198 : {
199 : const char *p;
200 : size_t tag_length;
201 :
202 232 : p = line_start;
203 420 : while (p < line_end && isspace((unsigned char) *p)) {
204 188 : p++;
205 : }
206 :
207 232 : if (p + sizeof("/**") - 1 <= line_end && memcmp(p, "/**", sizeof("/**") - 1) == 0) {
208 44 : p += sizeof("/**") - 1;
209 : }
210 :
211 232 : while (p < line_end && isspace((unsigned char) *p)) {
212 0 : p++;
213 : }
214 :
215 232 : if (p < line_end && *p == '*') {
216 188 : p++;
217 :
218 188 : if (p < line_end && *p == '/') {
219 44 : return NULL;
220 : }
221 :
222 288 : while (p < line_end && isspace((unsigned char) *p)) {
223 144 : p++;
224 : }
225 : }
226 :
227 188 : tag_length = sizeof("@property") - 1;
228 188 : if (p + tag_length > line_end || memcmp(p, "@property", tag_length) != 0) {
229 128 : return NULL;
230 : }
231 :
232 60 : p += tag_length;
233 60 : if (p < line_end && *p == '-') {
234 0 : p++;
235 0 : while (p < line_end && isalpha((unsigned char) *p)) {
236 0 : p++;
237 : }
238 : }
239 :
240 60 : if (p < line_end && !isspace((unsigned char) *p)) {
241 0 : return NULL;
242 : }
243 :
244 60 : return p;
245 : }
246 :
247 72 : static inline const char *lsp_phpdoc_use_tag_in_line(const char *line_start, const char *line_end)
248 : {
249 : const char *p;
250 : size_t tag_length;
251 :
252 72 : p = line_start;
253 184 : while (p < line_end && isspace((unsigned char) *p)) {
254 112 : p++;
255 : }
256 :
257 72 : if (p + sizeof("/**") - 1 <= line_end && memcmp(p, "/**", sizeof("/**") - 1) == 0) {
258 4 : p += sizeof("/**") - 1;
259 : }
260 :
261 76 : while (p < line_end && isspace((unsigned char) *p)) {
262 4 : p++;
263 : }
264 :
265 72 : if (p < line_end && *p == '*') {
266 0 : p++;
267 0 : if (p < line_end && *p == '/') {
268 0 : return NULL;
269 : }
270 :
271 0 : while (p < line_end && isspace((unsigned char) *p)) {
272 0 : p++;
273 : }
274 : }
275 :
276 72 : tag_length = sizeof("@use") - 1;
277 72 : if (p + tag_length > line_end || memcmp(p, "@use", tag_length) != 0) {
278 68 : return NULL;
279 : }
280 :
281 4 : if (p + tag_length < line_end && !isspace((unsigned char) p[tag_length])) {
282 0 : return NULL;
283 : }
284 :
285 4 : return p + tag_length;
286 : }
287 :
288 4 : static inline zend_string *lsp_phpdoc_use_type_from_line(const char *tag, const char *line_end)
289 : {
290 : const char *p, *type_start, *type_end;
291 : int32_t depth;
292 :
293 4 : p = tag;
294 8 : while (p < line_end && isspace((unsigned char) *p)) {
295 4 : p++;
296 : }
297 :
298 4 : type_start = p;
299 4 : depth = 0;
300 96 : while (p < line_end) {
301 96 : if (p + 1 < line_end && p[0] == '*' && p[1] == '/') {
302 0 : break;
303 : }
304 :
305 96 : if (*p == '<' || *p == '(' || *p == '[' || *p == '{') {
306 4 : depth++;
307 92 : } else if ((*p == '>' || *p == ')' || *p == ']' || *p == '}') && depth > 0) {
308 4 : depth--;
309 88 : } else if (depth == 0 && isspace((unsigned char) *p)) {
310 4 : break;
311 : }
312 :
313 92 : p++;
314 : }
315 :
316 4 : type_end = p;
317 4 : while (type_end > type_start && isspace((unsigned char) type_end[-1])) {
318 0 : type_end--;
319 : }
320 :
321 4 : if (type_end <= type_start) {
322 0 : return NULL;
323 : }
324 :
325 4 : return lsp_phpdoc_normalized_signature_slice(type_start, type_end - type_start);
326 : }
327 :
328 4 : static inline zend_string *lsp_phpdoc_generic_base_type(zend_string *type)
329 : {
330 : const char *value, *end, *open;
331 :
332 4 : value = ZSTR_VAL(type);
333 4 : end = value + ZSTR_LEN(type);
334 4 : open = memchr(value, '<', ZSTR_LEN(type));
335 4 : if (open) {
336 4 : end = open;
337 : }
338 :
339 4 : while (end > value && isspace((unsigned char) end[-1])) {
340 0 : end--;
341 : }
342 :
343 4 : return lsp_phpdoc_normalized_signature_slice(value, end - value);
344 : }
345 :
346 76 : static inline bool lsp_phpdoc_method_parse_line(const char *tag, const char *line_end, zend_string **label, zend_string **detail, bool *is_static)
347 : {
348 : const char *p, *q, *return_start, *return_end, *name_start, *name_end, *args_start, *args_end;
349 : zend_string *args, *return_type;
350 : size_t static_length;
351 : int32_t depth;
352 :
353 76 : *label = NULL;
354 76 : *detail = NULL;
355 76 : *is_static = false;
356 :
357 76 : while (line_end > tag && isspace((unsigned char) line_end[-1])) {
358 0 : line_end--;
359 : }
360 :
361 76 : if (line_end - tag >= 2 && line_end[-2] == '*' && line_end[-1] == '/') {
362 0 : line_end -= 2;
363 0 : while (line_end > tag && isspace((unsigned char) line_end[-1])) {
364 0 : line_end--;
365 : }
366 : }
367 :
368 76 : p = tag;
369 152 : while (p < line_end && isspace((unsigned char) *p)) {
370 76 : p++;
371 : }
372 :
373 76 : static_length = sizeof("static") - 1;
374 76 : if (p + static_length <= line_end &&
375 76 : strncasecmp(p, "static", static_length) == 0 &&
376 44 : (p + static_length == line_end || isspace((unsigned char) p[static_length]))
377 : ) {
378 44 : *is_static = true;
379 44 : p += static_length;
380 :
381 88 : while (p < line_end && isspace((unsigned char) *p)) {
382 44 : p++;
383 : }
384 : }
385 :
386 76 : return_start = p;
387 76 : args_start = NULL;
388 :
389 1888 : for (q = p; q < line_end; q++) {
390 1888 : if (*q == '(') {
391 76 : args_start = q;
392 76 : break;
393 : }
394 : }
395 :
396 76 : if (!args_start) {
397 0 : return false;
398 : }
399 :
400 76 : name_end = args_start;
401 76 : while (name_end > p && isspace((unsigned char) name_end[-1])) {
402 0 : name_end--;
403 : }
404 :
405 76 : name_start = name_end;
406 1268 : while (name_start > p && lsp_doc_is_identifier_char(name_start[-1])) {
407 1192 : name_start--;
408 : }
409 :
410 76 : if (name_start == name_end || !lsp_doc_is_identifier_start(*name_start)) {
411 0 : return false;
412 : }
413 :
414 76 : return_end = name_start;
415 152 : while (return_end > return_start && isspace((unsigned char) return_end[-1])) {
416 76 : return_end--;
417 : }
418 :
419 76 : args_end = NULL;
420 76 : depth = 0;
421 1052 : for (q = args_start; q < line_end; q++) {
422 1052 : if (*q == '(') {
423 76 : depth++;
424 976 : } else if (*q == ')') {
425 76 : depth--;
426 76 : if (depth == 0) {
427 76 : args_end = q + 1;
428 76 : break;
429 : }
430 : }
431 : }
432 :
433 76 : if (!args_end) {
434 0 : return false;
435 : }
436 :
437 76 : *label = zend_string_init(name_start, name_end - name_start, 0);
438 76 : args = lsp_phpdoc_normalized_signature_slice(args_start, args_end - args_start);
439 :
440 76 : if (return_end > return_start) {
441 76 : return_type = lsp_phpdoc_normalized_signature_slice(return_start, return_end - return_start);
442 152 : *detail = ZSTR_LEN(return_type) > 0
443 76 : ? strpprintf(0, "%s%s: %s", ZSTR_VAL(*label), ZSTR_VAL(args), ZSTR_VAL(return_type))
444 76 : : strpprintf(0, "%s%s", ZSTR_VAL(*label), ZSTR_VAL(args))
445 : ;
446 :
447 76 : zend_string_release(return_type);
448 : } else {
449 0 : *detail = strpprintf(0, "%s%s", ZSTR_VAL(*label), ZSTR_VAL(args));
450 : }
451 :
452 76 : zend_string_release(args);
453 :
454 76 : return true;
455 : }
456 :
457 60 : static inline bool lsp_phpdoc_property_parse_line(const char *tag, const char *line_end, zend_string **label, zend_string **detail, bool *is_static)
458 : {
459 : const char *p, *q, *type_start, *type_end, *name_start, *name_end;
460 : zend_string *type;
461 : size_t static_length;
462 :
463 60 : *label = NULL;
464 60 : *detail = NULL;
465 60 : *is_static = false;
466 :
467 60 : while (line_end > tag && isspace((unsigned char) line_end[-1])) {
468 0 : line_end--;
469 : }
470 :
471 60 : if (line_end - tag >= 2 && line_end[-2] == '*' && line_end[-1] == '/') {
472 0 : line_end -= 2;
473 0 : while (line_end > tag && isspace((unsigned char) line_end[-1])) {
474 0 : line_end--;
475 : }
476 : }
477 :
478 60 : p = tag;
479 120 : while (p < line_end && isspace((unsigned char) *p)) {
480 60 : p++;
481 : }
482 :
483 60 : static_length = sizeof("static") - 1;
484 60 : if (p + static_length <= line_end &&
485 60 : strncasecmp(p, "static", static_length) == 0 &&
486 28 : (p + static_length == line_end || isspace((unsigned char) p[static_length]))
487 : ) {
488 28 : *is_static = true;
489 28 : p += static_length;
490 :
491 56 : while (p < line_end && isspace((unsigned char) *p)) {
492 28 : p++;
493 : }
494 : }
495 :
496 60 : type_start = p;
497 60 : name_start = NULL;
498 60 : name_end = NULL;
499 480 : for (q = p; q < line_end; q++) {
500 480 : if (*q != '$' || q + 1 >= line_end || !lsp_doc_is_identifier_start(q[1])) {
501 420 : continue;
502 : }
503 :
504 60 : name_start = q + 1;
505 60 : name_end = name_start + 1;
506 956 : while (name_end < line_end && lsp_doc_is_identifier_char(*name_end)) {
507 896 : name_end++;
508 : }
509 :
510 60 : type_end = q;
511 120 : while (type_end > type_start && isspace((unsigned char) type_end[-1])) {
512 60 : type_end--;
513 : }
514 :
515 60 : *label = zend_string_init(name_start, name_end - name_start, 0);
516 60 : type = lsp_phpdoc_normalized_signature_slice(type_start, type_end - type_start);
517 120 : *detail = ZSTR_LEN(type) > 0
518 60 : ? strpprintf(0, "property %s $%s", ZSTR_VAL(type), ZSTR_VAL(*label))
519 60 : : strpprintf(0, "property $%s", ZSTR_VAL(*label))
520 : ;
521 :
522 60 : zend_string_release(type);
523 :
524 60 : return true;
525 : }
526 :
527 0 : return false;
528 : }
529 :
530 76 : static inline void lsp_phpdoc_add_cached_method(zval *methods, zend_string *label, zend_string *detail, bool is_static)
531 : {
532 : zval method;
533 :
534 76 : array_init(&method);
535 76 : add_assoc_str(&method, "label", zend_string_copy(label));
536 76 : add_assoc_str(&method, "detail", zend_string_copy(detail));
537 76 : add_assoc_bool(&method, "static", is_static);
538 76 : add_assoc_long(&method, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
539 76 : add_next_index_zval(methods, &method);
540 76 : }
541 :
542 60 : static inline void lsp_phpdoc_add_cached_property(zval *properties, zend_string *label, zend_string *detail, bool is_static)
543 : {
544 : zval property;
545 :
546 60 : array_init(&property);
547 60 : add_assoc_str(&property, "label", zend_string_copy(label));
548 60 : add_assoc_str(&property, "detail", zend_string_copy(detail));
549 60 : add_assoc_bool(&property, "static", is_static);
550 60 : add_assoc_long(&property, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
551 60 : add_next_index_zval(properties, &property);
552 60 : }
553 :
554 152 : static inline zend_string *lsp_phpdoc_template_name_from_line(const char *line_start, const char *line_end)
555 : {
556 : const char *p, *tag, *name_start, *name_end;
557 : size_t tag_length;
558 :
559 152 : p = line_start;
560 236 : while (p < line_end && isspace((unsigned char) *p)) {
561 84 : p++;
562 : }
563 :
564 152 : if (p + sizeof("/**") - 1 <= line_end && memcmp(p, "/**", sizeof("/**") - 1) == 0) {
565 68 : p += sizeof("/**") - 1;
566 : }
567 :
568 152 : while (p < line_end && isspace((unsigned char) *p)) {
569 0 : p++;
570 : }
571 :
572 152 : if (p < line_end && *p == '*') {
573 84 : p++;
574 84 : if (p < line_end && *p == '/') {
575 0 : return NULL;
576 : }
577 :
578 168 : while (p < line_end && isspace((unsigned char) *p)) {
579 84 : p++;
580 : }
581 : }
582 :
583 152 : tag_length = sizeof("@template") - 1;
584 152 : if (p + tag_length > line_end || memcmp(p, "@template", tag_length) != 0) {
585 68 : return NULL;
586 : }
587 :
588 84 : tag = p + tag_length;
589 84 : if (tag < line_end && *tag == '-') {
590 16 : tag++;
591 160 : while (tag < line_end && (islower((unsigned char) *tag) || *tag == '-')) {
592 144 : tag++;
593 : }
594 : }
595 :
596 84 : if (tag < line_end && !isspace((unsigned char) *tag)) {
597 0 : return NULL;
598 : }
599 :
600 84 : p = tag;
601 168 : while (p < line_end && isspace((unsigned char) *p)) {
602 84 : p++;
603 : }
604 :
605 84 : if (p >= line_end || !lsp_doc_is_identifier_start(*p)) {
606 0 : return NULL;
607 : }
608 :
609 84 : name_start = p++;
610 416 : while (p < line_end && lsp_doc_is_identifier_char(*p)) {
611 332 : p++;
612 : }
613 84 : name_end = p;
614 :
615 84 : return zend_string_init(name_start, name_end - name_start, 0);
616 : }
617 :
618 316 : extern void lsp_phpdoc_cache_class_methods(zval *methods, zend_string *contents, size_t class_start)
619 : {
620 316 : const char *value = ZSTR_VAL(contents), *doc_end_ptr, *line, *line_end, *tag;
621 : zend_string *label, *detail;
622 : size_t doc_start, doc_end;
623 : bool is_static;
624 :
625 316 : if (!lsp_phpdoc_class_docblock_bounds(contents, class_start, &doc_start, &doc_end)) {
626 272 : return;
627 : }
628 :
629 44 : line = value + doc_start;
630 44 : doc_end_ptr = value + doc_end;
631 276 : while (line < doc_end_ptr) {
632 232 : line_end = line;
633 7272 : while (line_end < doc_end_ptr && *line_end != '\n' && *line_end != '\r') {
634 7040 : line_end++;
635 : }
636 :
637 232 : tag = lsp_phpdoc_method_tag_in_line(line, line_end);
638 232 : if (tag && lsp_phpdoc_method_parse_line(tag, line_end, &label, &detail, &is_static)) {
639 76 : if (!zend_string_equals_literal(label, "__construct")) {
640 76 : lsp_phpdoc_add_cached_method(methods, label, detail, is_static);
641 : }
642 :
643 76 : zend_string_release(label);
644 76 : zend_string_release(detail);
645 : }
646 :
647 420 : while (line_end < doc_end_ptr && (*line_end == '\n' || *line_end == '\r')) {
648 188 : line_end++;
649 : }
650 :
651 232 : line = line_end;
652 : }
653 : }
654 :
655 316 : extern void lsp_phpdoc_cache_class_properties(zval *properties, zend_string *contents, size_t class_start)
656 : {
657 316 : const char *value = ZSTR_VAL(contents), *doc_end_ptr, *line, *line_end, *tag;
658 : zend_string *label, *detail;
659 : size_t doc_start, doc_end;
660 : bool is_static;
661 :
662 316 : if (!lsp_phpdoc_class_docblock_bounds(contents, class_start, &doc_start, &doc_end)) {
663 272 : return;
664 : }
665 :
666 44 : line = value + doc_start;
667 44 : doc_end_ptr = value + doc_end;
668 276 : while (line < doc_end_ptr) {
669 232 : line_end = line;
670 7272 : while (line_end < doc_end_ptr && *line_end != '\n' && *line_end != '\r') {
671 7040 : line_end++;
672 : }
673 :
674 232 : tag = lsp_phpdoc_property_tag_in_line(line, line_end);
675 232 : if (tag && lsp_phpdoc_property_parse_line(tag, line_end, &label, &detail, &is_static)) {
676 60 : lsp_phpdoc_add_cached_property(properties, label, detail, is_static);
677 60 : zend_string_release(label);
678 60 : zend_string_release(detail);
679 : }
680 :
681 420 : while (line_end < doc_end_ptr && (*line_end == '\n' || *line_end == '\r')) {
682 188 : line_end++;
683 : }
684 :
685 232 : line = line_end;
686 : }
687 : }
688 :
689 84 : extern zend_string *lsp_phpdoc_template_type_at(zend_string *text, uint32_t target_index)
690 : {
691 84 : const char *value = ZSTR_VAL(text), *doc_end_ptr, *line, *line_end;
692 : zend_string *name;
693 : size_t class_start, doc_start, doc_end;
694 : uint32_t index;
695 :
696 84 : if (!lsp_phpdoc_first_class_start(text, &class_start) ||
697 84 : !lsp_phpdoc_class_docblock_bounds(text, class_start, &doc_start, &doc_end)
698 : ) {
699 16 : return NULL;
700 : }
701 :
702 68 : line = value + doc_start;
703 68 : doc_end_ptr = value + doc_end;
704 68 : index = 0;
705 152 : while (line < doc_end_ptr) {
706 152 : line_end = line;
707 3080 : while (line_end < doc_end_ptr && *line_end != '\n' && *line_end != '\r') {
708 2928 : line_end++;
709 : }
710 :
711 152 : name = lsp_phpdoc_template_name_from_line(line, line_end);
712 152 : if (name) {
713 84 : if (index == target_index) {
714 68 : return name;
715 : }
716 16 : zend_string_release(name);
717 16 : index++;
718 : }
719 :
720 168 : while (line_end < doc_end_ptr && (*line_end == '\n' || *line_end == '\r')) {
721 84 : line_end++;
722 : }
723 :
724 84 : line = line_end;
725 : }
726 :
727 0 : return NULL;
728 : }
729 :
730 8 : extern zend_string *lsp_phpdoc_trait_use_generic_argument(zend_string *text, zend_string *trait_name, uint32_t target_index)
731 : {
732 8 : const char *value = ZSTR_VAL(text), *end = value + ZSTR_LEN(text), *line, *line_end, *tag;
733 : zend_string *use_type, *base_type, *resolved_trait, *argument, *resolved_argument;
734 :
735 8 : line = value;
736 76 : while (line < end) {
737 72 : line_end = line;
738 1476 : while (line_end < end && *line_end != '\n' && *line_end != '\r') {
739 1404 : line_end++;
740 : }
741 :
742 72 : tag = lsp_phpdoc_use_tag_in_line(line, line_end);
743 72 : if (tag) {
744 4 : use_type = lsp_phpdoc_use_type_from_line(tag, line_end);
745 4 : if (use_type) {
746 4 : base_type = lsp_phpdoc_generic_base_type(use_type);
747 4 : resolved_trait = lsp_resolve_class_name(text, base_type);
748 4 : if (resolved_trait && lsp_type_equals(resolved_trait, trait_name)) {
749 4 : argument = lsp_type_generic_argument(use_type, target_index);
750 4 : if (!argument && target_index > 0) {
751 0 : argument = lsp_type_generic_argument(use_type, 0);
752 : }
753 4 : if (argument) {
754 4 : resolved_argument = lsp_resolve_class_name(text, argument);
755 4 : zend_string_release(argument);
756 4 : zend_string_release(resolved_trait);
757 4 : zend_string_release(base_type);
758 4 : zend_string_release(use_type);
759 :
760 4 : return resolved_argument;
761 : }
762 : }
763 0 : if (resolved_trait) {
764 0 : zend_string_release(resolved_trait);
765 : }
766 0 : zend_string_release(base_type);
767 0 : zend_string_release(use_type);
768 : }
769 : }
770 :
771 148 : while (line_end < end && (*line_end == '\n' || *line_end == '\r')) {
772 80 : line_end++;
773 : }
774 :
775 68 : line = line_end;
776 : }
777 :
778 4 : return NULL;
779 : }
|