| 1 |
|
| /* |
| 2 |
|
| * eshu_php.h — PHP language indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Handles brace-based blocks, alternative control syntax (if(): endif;), |
| 5 |
|
| * heredocs/nowdocs, PHP/HTML mixed mode, and switch case labels. |
| 6 |
|
| * |
| 7 |
|
| * Pure C, no Perl dependencies, header-only. |
| 8 |
|
| */ |
| 9 |
|
| |
| 10 |
|
| #ifndef ESHU_PHP_H |
| 11 |
|
| #define ESHU_PHP_H |
| 12 |
|
| |
| 13 |
|
| #include "eshu.h" |
| 14 |
|
| |
| 15 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 16 |
|
| * Line classifiers |
| 17 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 18 |
|
| |
| 19 |
803 |
| static int eshu_php_is_closing(char c) { |
| 20 |
803 |
| return c == '}'; |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
|
| /* Returns 1 if line is a PHP switch case/default label. */ |
| 24 |
596 |
| static int eshu_php_is_case_label(const char *content, int len) { |
| 25 |
|
| const char *p, *end; |
| 26 |
596 |
| int is_case = 0; |
| 27 |
|
| |
| 28 |
596 |
▶ | if (len < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 2) return 0; |
0 |
596 |
596 |
|
| 29 |
596 |
▶ | if (len >= 5 && strncmp(content, "case ", 5) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 5 && strncmp(content, "case ", 5) == 0) |
588 |
8 |
596 |
| if (len >= 5 && strncmp(content, "case ", 5) == 0) |
10 |
578 |
588 |
|
| 30 |
10 |
| is_case = 1; |
| 31 |
586 |
▶ | else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
Branch: 3 branches
| Branch | True | False | Total |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
517 |
69 |
586 |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
5 |
512 |
517 |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
5 |
0 |
5 |
|
| 32 |
5 |
▶ | (len == 7 || content[7] == ':' || content[7] == ' ' || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 || content[7] == ':' || content[7] == ' ' || |
3 |
2 |
5 |
| (len == 7 || content[7] == ':' || content[7] == ' ' || |
0 |
3 |
3 |
|
| 33 |
0 |
▶ | content[7] == '\t' || content[7] == ';' || content[7] == '/')) |
Branch: 3 branches
| Branch | True | False | Total |
| content[7] == '\t' || content[7] == ';' || content[7] == '/')) |
0 |
0 |
0 |
| content[7] == '\t' || content[7] == ';' || content[7] == '/')) |
0 |
0 |
0 |
| content[7] == '\t' || content[7] == ';' || content[7] == '/')) |
0 |
0 |
0 |
|
| 34 |
5 |
| is_case = 1; |
| 35 |
|
| |
| 36 |
596 |
▶ | if (!is_case) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_case) return 0; |
581 |
15 |
596 |
|
| 37 |
|
| |
| 38 |
15 |
| p = content; |
| 39 |
15 |
| end = content + len; |
| 40 |
209 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
194 |
15 |
209 |
|
| 41 |
194 |
▶ | if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
194 |
194 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
|
| 42 |
194 |
▶ | if (*p == '#') { end = p; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') { end = p; break; } |
0 |
194 |
194 |
|
| 43 |
194 |
▶ | if (*p == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '"') { |
0 |
194 |
194 |
|
| 44 |
0 |
| p++; |
| 45 |
0 |
▶ | while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
|
| 46 |
194 |
▶ | } else if (*p == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*p == '\'') { |
8 |
186 |
194 |
|
| 47 |
8 |
| p++; |
| 48 |
54 |
▶ | while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
0 |
46 |
46 |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
54 |
0 |
54 |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
46 |
8 |
54 |
|
| 49 |
|
| } |
| 50 |
194 |
| p++; |
| 51 |
|
| } |
| 52 |
15 |
▶ | while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
15 |
0 |
15 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
15 |
15 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
15 |
15 |
|
| 53 |
15 |
▶ | return end > content && *(end - 1) == ':'; |
Branch: 2 branches
| Branch | True | False | Total |
| return end > content && *(end - 1) == ':'; |
15 |
0 |
15 |
| return end > content && *(end - 1) == ':'; |
6 |
9 |
15 |
|
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| /* Returns 1 if line ends an alternative control block: endif; endfor; etc. */ |
| 57 |
803 |
| static int eshu_php_is_alt_close(const char *content, int len) { |
| 58 |
803 |
▶ | if (len < 5) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 5) return 0; |
193 |
610 |
803 |
|
| 59 |
610 |
▶ | if (strncmp(content, "endif", 5) == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "endif", 5) == 0) return 1; |
2 |
608 |
610 |
|
| 60 |
608 |
▶ | if (len >= 6 && strncmp(content, "endfor", 6) == 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 6 && strncmp(content, "endfor", 6) == 0 && |
551 |
57 |
608 |
| if (len >= 6 && strncmp(content, "endfor", 6) == 0 && |
2 |
549 |
551 |
|
| 61 |
2 |
▶ | strncmp(content, "endforeach", 10) != 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "endforeach", 10) != 0) return 1; |
1 |
1 |
2 |
|
| 62 |
607 |
▶ | if (len >= 10 && strncmp(content, "endforeach", 10) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 10 && strncmp(content, "endforeach", 10) == 0) return 1; |
503 |
104 |
607 |
| if (len >= 10 && strncmp(content, "endforeach", 10) == 0) return 1; |
1 |
502 |
503 |
|
| 63 |
606 |
▶ | if (len >= 8 && strncmp(content, "endwhile", 8) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 8 && strncmp(content, "endwhile", 8) == 0) return 1; |
538 |
68 |
606 |
| if (len >= 8 && strncmp(content, "endwhile", 8) == 0) return 1; |
1 |
537 |
538 |
|
| 64 |
605 |
▶ | if (len >= 9 && strncmp(content, "endswitch", 9) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 9 && strncmp(content, "endswitch", 9) == 0) return 1; |
519 |
86 |
605 |
| if (len >= 9 && strncmp(content, "endswitch", 9) == 0) return 1; |
0 |
519 |
519 |
|
| 65 |
605 |
▶ | if (len >= 10 && strncmp(content, "enddeclare",10) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 10 && strncmp(content, "enddeclare",10) == 0) return 1; |
502 |
103 |
605 |
| if (len >= 10 && strncmp(content, "enddeclare",10) == 0) return 1; |
0 |
502 |
502 |
|
| 66 |
605 |
| return 0; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* Returns 1 if line is else: or elseif (...): — closes previous alt block |
| 70 |
|
| * and opens another at the same depth. */ |
| 71 |
803 |
| static int eshu_php_is_alt_middle(const char *content, int len) { |
| 72 |
|
| const char *end, *p; |
| 73 |
|
| |
| 74 |
803 |
▶ | if (len < 4) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 4) return 0; |
190 |
613 |
803 |
|
| 75 |
613 |
▶ | if (strncmp(content, "else", 4) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "else", 4) != 0) return 0; |
611 |
2 |
613 |
|
| 76 |
|
| |
| 77 |
|
| /* else: */ |
| 78 |
2 |
| p = content + 4; |
| 79 |
2 |
▶ | while (*p == ' ' || *p == '\t') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p == ' ' || *p == '\t') p++; |
0 |
2 |
2 |
| while (*p == ' ' || *p == '\t') p++; |
0 |
2 |
2 |
|
| 80 |
2 |
▶ | if (*p == ':') return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ':') return 1; |
1 |
1 |
2 |
|
| 81 |
|
| |
| 82 |
|
| /* elseif (...): */ |
| 83 |
1 |
▶ | if (strncmp(p, "if", 2) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(p, "if", 2) == 0) { |
1 |
0 |
1 |
|
| 84 |
1 |
| end = content + len; |
| 85 |
1 |
| p = content; |
| 86 |
13 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
12 |
1 |
13 |
|
| 87 |
12 |
▶ | if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
12 |
12 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
|
| 88 |
12 |
▶ | if (*p == '#') { end = p; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') { end = p; break; } |
0 |
12 |
12 |
|
| 89 |
12 |
| p++; |
| 90 |
|
| } |
| 91 |
1 |
▶ | while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
1 |
0 |
1 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
1 |
1 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
1 |
1 |
|
| 92 |
1 |
▶ | return end > content && *(end - 1) == ':'; |
Branch: 2 branches
| Branch | True | False | Total |
| return end > content && *(end - 1) == ':'; |
1 |
0 |
1 |
| return end > content && *(end - 1) == ':'; |
1 |
0 |
1 |
|
| 93 |
|
| } |
| 94 |
0 |
| return 0; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| /* Returns 1 if line is a control-structure line opening an alt block (if():). */ |
| 98 |
803 |
| static int eshu_php_is_alt_open(const char *content, int len) { |
| 99 |
|
| const char *end, *p; |
| 100 |
|
| |
| 101 |
803 |
▶ | if (len < 3) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 3) return 0; |
187 |
616 |
803 |
|
| 102 |
|
| |
| 103 |
|
| /* exclude else/elseif — handled by alt_middle */ |
| 104 |
616 |
▶ | if (strncmp(content, "else", 4) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "else", 4) == 0) return 0; |
2 |
614 |
616 |
|
| 105 |
|
| |
| 106 |
|
| /* control keywords */ |
| 107 |
614 |
▶ | if (strncmp(content, "if", 2) != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "if", 2) != 0 && |
579 |
35 |
614 |
|
| 108 |
579 |
▶ | strncmp(content, "for", 3) != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "for", 3) != 0 && |
552 |
27 |
579 |
|
| 109 |
552 |
▶ | strncmp(content, "foreach", 7) != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "foreach", 7) != 0 && |
552 |
0 |
552 |
|
| 110 |
552 |
▶ | strncmp(content, "while", 5) != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "while", 5) != 0 && |
549 |
3 |
552 |
|
| 111 |
549 |
▶ | strncmp(content, "switch", 6) != 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "switch", 6) != 0 && |
547 |
2 |
549 |
|
| 112 |
547 |
▶ | strncmp(content, "declare", 7) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(content, "declare", 7) != 0) |
546 |
1 |
547 |
|
| 113 |
546 |
| return 0; |
| 114 |
|
| |
| 115 |
|
| /* must end with ':' (ignoring comments) */ |
| 116 |
68 |
| end = content + len; |
| 117 |
68 |
| p = content; |
| 118 |
1956 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
1888 |
68 |
1956 |
|
| 119 |
1888 |
▶ | if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
1888 |
1888 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
|
| 120 |
1888 |
▶ | if (*p == '#') { end = p; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') { end = p; break; } |
0 |
1888 |
1888 |
|
| 121 |
1888 |
▶ | if (*p == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '"') { |
0 |
1888 |
1888 |
|
| 122 |
0 |
| p++; |
| 123 |
0 |
▶ | while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
| while (p < end && *p != '"') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
|
| 124 |
1888 |
▶ | } else if (*p == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*p == '\'') { |
2 |
1886 |
1888 |
|
| 125 |
2 |
| p++; |
| 126 |
2 |
▶ | while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
0 |
0 |
0 |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
2 |
0 |
2 |
| while (p < end && *p != '\'') { if (*p == '\\') p++; p++; } |
0 |
2 |
2 |
|
| 127 |
|
| } |
| 128 |
1888 |
| p++; |
| 129 |
|
| } |
| 130 |
68 |
▶ | while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
68 |
0 |
68 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
68 |
68 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
68 |
68 |
|
| 131 |
68 |
▶ | if (end <= content || *(end - 1) != ':') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (end <= content || *(end - 1) != ':') return 0; |
68 |
0 |
68 |
| if (end <= content || *(end - 1) != ':') return 0; |
63 |
5 |
68 |
|
| 132 |
|
| |
| 133 |
|
| /* Must NOT be a case label (case X:) */ |
| 134 |
5 |
▶ | if (strncmp(content, "case ", 5) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "case ", 5) == 0) return 0; |
0 |
5 |
5 |
|
| 135 |
|
| |
| 136 |
5 |
| return 1; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| /* Parse <<<TAG or <<<'TAG' at p[0..]. Stores tag in buf (max 63 chars). |
| 140 |
|
| * Sets *nowdoc=1 for <<<'TAG'. Returns pointer past the <<<TAG line. */ |
| 141 |
3 |
| static const char *eshu_php_heredoc_parse(const char *p, const char *end, |
| 142 |
|
| char *buf, int *nowdoc) { |
| 143 |
3 |
| int nowdoc_q = 0; |
| 144 |
3 |
| int i = 0; |
| 145 |
3 |
| *nowdoc = 0; |
| 146 |
|
| |
| 147 |
|
| /* p points at first char of tag name (or ' for nowdoc) */ |
| 148 |
3 |
▶ | if (p < end && *p == '\'') { nowdoc_q = 1; p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '\'') { nowdoc_q = 1; p++; } |
3 |
0 |
3 |
| if (p < end && *p == '\'') { nowdoc_q = 1; p++; } |
1 |
2 |
3 |
|
| 149 |
2 |
▶ | else if (p < end && *p == '"') { p++; } /* optional double-quote delim */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (p < end && *p == '"') { p++; } /* optional double-quote delim */ |
2 |
0 |
2 |
| else if (p < end && *p == '"') { p++; } /* optional double-quote delim */ |
0 |
2 |
2 |
|
| 150 |
|
| |
| 151 |
13 |
▶ | while (p < end && *p != '\n' && *p != '\r' && *p != '\'' && *p != '"' |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n' && *p != '\r' && *p != '\'' && *p != '"' |
10 |
0 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && *p != '\'' && *p != '"' |
10 |
0 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && *p != '\'' && *p != '"' |
9 |
1 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && *p != '\'' && *p != '"' |
9 |
0 |
9 |
|
| 152 |
21 |
▶ | && (isalnum((unsigned char)*p) || *p == '_')) { |
Branch: 3 branches
| Branch | True | False | Total |
| && (isalnum((unsigned char)*p) || *p == '_')) { |
10 |
2 |
12 |
| && (isalnum((unsigned char)*p) || *p == '_')) { |
9 |
0 |
9 |
| && (isalnum((unsigned char)*p) || *p == '_')) { |
0 |
0 |
0 |
|
| 153 |
9 |
▶ | if (i < 63) buf[i++] = *p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 63) buf[i++] = *p; |
9 |
0 |
9 |
|
| 154 |
9 |
| p++; |
| 155 |
|
| } |
| 156 |
3 |
| buf[i] = '\0'; |
| 157 |
|
| |
| 158 |
3 |
▶ | if (nowdoc_q) { *nowdoc = 1; if (p < end && *p == '\'') p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (nowdoc_q) { *nowdoc = 1; if (p < end && *p == '\'') p++; } |
1 |
2 |
3 |
| if (nowdoc_q) { *nowdoc = 1; if (p < end && *p == '\'') p++; } |
1 |
0 |
1 |
| if (nowdoc_q) { *nowdoc = 1; if (p < end && *p == '\'') p++; } |
1 |
0 |
1 |
|
| 159 |
2 |
▶ | else if (p < end && *p == '"') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (p < end && *p == '"') p++; |
0 |
2 |
2 |
| else if (p < end && *p == '"') p++; |
0 |
0 |
0 |
|
| 160 |
|
| |
| 161 |
|
| /* advance to EOL */ |
| 162 |
3 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
0 |
3 |
3 |
| while (p < end && *p != '\n') p++; |
0 |
0 |
0 |
|
| 163 |
|
| |
| 164 |
3 |
| return p; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* Returns 1 if content (stripped) starts with heredoc_tag and is a valid end. */ |
| 168 |
7 |
| static int eshu_php_heredoc_ends(const char *content, int len, |
| 169 |
|
| const char *tag) { |
| 170 |
7 |
| size_t tlen = strlen(tag); |
| 171 |
7 |
▶ | if ((size_t)len < tlen) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)len < tlen) return 0; |
0 |
7 |
7 |
|
| 172 |
7 |
▶ | if (strncmp(content, tag, tlen) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, tag, tlen) != 0) return 0; |
4 |
3 |
7 |
|
| 173 |
|
| /* after tag: optional ; or , or end of trimmed content */ |
| 174 |
3 |
| size_t rest = (size_t)len - tlen; |
| 175 |
3 |
▶ | if (rest == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rest == 0) return 1; |
0 |
3 |
3 |
|
| 176 |
3 |
| char c = content[tlen]; |
| 177 |
3 |
▶ | return c == ';' || c == ',' || c == ' ' || c == '\t'; |
Branch: 4 branches
| Branch | True | False | Total |
| return c == ';' || c == ',' || c == ' ' || c == '\t'; |
0 |
3 |
3 |
| return c == ';' || c == ',' || c == ' ' || c == '\t'; |
0 |
0 |
0 |
| return c == ';' || c == ',' || c == ' ' || c == '\t'; |
0 |
0 |
0 |
| return c == ';' || c == ',' || c == ' ' || c == '\t'; |
0 |
0 |
0 |
|
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 181 |
|
| * Scan a line — update ctx state and depth for the next line |
| 182 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 183 |
|
| |
| 184 |
808 |
| static void eshu_php_scan_line(eshu_php_ctx_t *ctx, |
| 185 |
|
| const char *p, const char *end) { |
| 186 |
17057 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
16256 |
801 |
17057 |
|
| 187 |
16256 |
| char c = *p; |
| 188 |
|
| |
| 189 |
16256 |
| switch (ctx->state) { |
| 190 |
15396 |
| case ESHU_CODE: |
| 191 |
15396 |
▶ | if (!ctx->in_php) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->in_php) { |
0 |
15396 |
15396 |
|
| 192 |
|
| /* HTML mode: look for <?php or <?= */ |
| 193 |
0 |
▶ | if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
|
| 194 |
0 |
| p += 2; |
| 195 |
0 |
▶ | if (p + 2 < end && *(p) == 'p' && *(p+1) == 'h' && *(p+2) == 'p') |
Branch: 4 branches
| Branch | True | False | Total |
| if (p + 2 < end && *(p) == 'p' && *(p+1) == 'h' && *(p+2) == 'p') |
0 |
0 |
0 |
| if (p + 2 < end && *(p) == 'p' && *(p+1) == 'h' && *(p+2) == 'p') |
0 |
0 |
0 |
| if (p + 2 < end && *(p) == 'p' && *(p+1) == 'h' && *(p+2) == 'p') |
0 |
0 |
0 |
| if (p + 2 < end && *(p) == 'p' && *(p+1) == 'h' && *(p+2) == 'p') |
0 |
0 |
0 |
|
| 196 |
0 |
| { p += 3; ctx->in_php = 1; } |
| 197 |
0 |
▶ | else if (p < end && *p == '=') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (p < end && *p == '=') |
0 |
0 |
0 |
| else if (p < end && *p == '=') |
0 |
0 |
0 |
|
| 198 |
0 |
| { p++; ctx->in_php = 1; } |
| 199 |
0 |
| continue; |
| 200 |
|
| } |
| 201 |
0 |
| p++; |
| 202 |
0 |
| continue; |
| 203 |
|
| } |
| 204 |
|
| /* PHP mode */ |
| 205 |
15396 |
▶ | if (c == '{') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '{') { |
223 |
15173 |
15396 |
|
| 206 |
223 |
| ctx->depth++; |
| 207 |
15173 |
▶ | } else if (c == '}') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '}') { |
223 |
14950 |
15173 |
|
| 208 |
223 |
| ctx->depth--; |
| 209 |
223 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
223 |
223 |
|
| 210 |
14950 |
▶ | } else if (c == '(') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '(') { |
382 |
14568 |
14950 |
|
| 211 |
382 |
| ctx->paren_depth++; |
| 212 |
14568 |
▶ | } else if (c == ')') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == ')') { |
382 |
14186 |
14568 |
|
| 213 |
382 |
▶ | if (ctx->paren_depth > 0) ctx->paren_depth--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->paren_depth > 0) ctx->paren_depth--; |
382 |
0 |
382 |
|
| 214 |
14186 |
▶ | } else if (c == '[') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '[') { |
98 |
14088 |
14186 |
|
| 215 |
98 |
| ctx->bracket_depth++; |
| 216 |
14088 |
▶ | } else if (c == ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == ']') { |
98 |
13990 |
14088 |
|
| 217 |
98 |
▶ | if (ctx->bracket_depth > 0) ctx->bracket_depth--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->bracket_depth > 0) ctx->bracket_depth--; |
98 |
0 |
98 |
|
| 218 |
13990 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
33 |
13957 |
13990 |
|
| 219 |
33 |
| ctx->state = ESHU_PHP_STRING_DQ; |
| 220 |
13957 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
49 |
13908 |
13957 |
|
| 221 |
49 |
| ctx->state = ESHU_PHP_STRING_SQ; |
| 222 |
13908 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
5 |
13903 |
13908 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
5 |
0 |
5 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
3 |
2 |
5 |
|
| 223 |
3 |
| return; /* line comment */ |
| 224 |
13905 |
▶ | } else if (c == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '#') { |
1 |
13904 |
13905 |
|
| 225 |
1 |
| return; /* line comment */ |
| 226 |
13904 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
2 |
13902 |
13904 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
2 |
0 |
2 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
2 |
0 |
2 |
|
| 227 |
2 |
| ctx->state = ESHU_COMMENT_BLOCK; |
| 228 |
2 |
| p++; |
| 229 |
13902 |
▶ | } else if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
70 |
13832 |
13902 |
| } else if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
70 |
0 |
70 |
| } else if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
3 |
67 |
70 |
| } else if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
3 |
0 |
3 |
|
| 230 |
|
| /* heredoc/nowdoc */ |
| 231 |
3 |
| int nowdoc = 0; |
| 232 |
3 |
| p = eshu_php_heredoc_parse(p + 3, end, |
| 233 |
3 |
| ctx->heredoc_tag, &nowdoc); |
| 234 |
3 |
▶ | ctx->state = nowdoc ? ESHU_PHP_NOWDOC : ESHU_PHP_HEREDOC; |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state = nowdoc ? ESHU_PHP_NOWDOC : ESHU_PHP_HEREDOC; |
1 |
2 |
3 |
|
| 235 |
3 |
| return; /* rest of line is <<<TAG\n */ |
| 236 |
13899 |
▶ | } else if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
65 |
13834 |
13899 |
| } else if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
65 |
0 |
65 |
| } else if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
0 |
65 |
65 |
|
| 237 |
0 |
| ctx->in_php = 0; |
| 238 |
0 |
| p++; |
| 239 |
|
| } |
| 240 |
15389 |
| break; |
| 241 |
|
| |
| 242 |
480 |
| case ESHU_PHP_STRING_DQ: |
| 243 |
480 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
7 |
473 |
480 |
| if (c == '\\' && p + 1 < end) { |
7 |
0 |
7 |
|
| 244 |
7 |
| p++; /* skip escaped char */ |
| 245 |
473 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
33 |
440 |
473 |
|
| 246 |
33 |
| ctx->state = ESHU_CODE; |
| 247 |
|
| } |
| 248 |
|
| /* $var inside DQ string: ignore, no depth change */ |
| 249 |
480 |
| break; |
| 250 |
|
| |
| 251 |
321 |
| case ESHU_PHP_STRING_SQ: |
| 252 |
321 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
3 |
318 |
321 |
| if (c == '\\' && p + 1 < end) { |
3 |
0 |
3 |
|
| 253 |
3 |
| p++; |
| 254 |
318 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
49 |
269 |
318 |
|
| 255 |
49 |
| ctx->state = ESHU_CODE; |
| 256 |
|
| } |
| 257 |
321 |
| break; |
| 258 |
|
| |
| 259 |
59 |
| case ESHU_COMMENT_BLOCK: |
| 260 |
59 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
6 |
53 |
59 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
4 |
2 |
6 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
2 |
2 |
4 |
|
| 261 |
2 |
| ctx->state = ESHU_CODE; |
| 262 |
2 |
| p++; |
| 263 |
|
| } |
| 264 |
59 |
| break; |
| 265 |
|
| |
| 266 |
0 |
| case ESHU_PHP_HEREDOC: |
| 267 |
|
| case ESHU_PHP_NOWDOC: |
| 268 |
|
| /* end detection is handled in process_line */ |
| 269 |
0 |
| return; |
| 270 |
|
| |
| 271 |
0 |
| default: |
| 272 |
0 |
| break; |
| 273 |
|
| } |
| 274 |
16249 |
| p++; |
| 275 |
|
| } |
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 279 |
|
| * Process a single line |
| 280 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 281 |
|
| |
| 282 |
880 |
| static void eshu_php_process_line(eshu_php_ctx_t *ctx, eshu_buf_t *out, |
| 283 |
|
| const char *line_start, const char *eol) { |
| 284 |
880 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 285 |
|
| int line_len; |
| 286 |
|
| int indent_depth; |
| 287 |
|
| |
| 288 |
|
| /* empty line */ |
| 289 |
880 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
65 |
815 |
880 |
|
| 290 |
65 |
| eshu_buf_putc(out, '\n'); |
| 291 |
65 |
| return; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
815 |
| line_len = (int)(eol - content); |
| 295 |
|
| |
| 296 |
|
| /* HTML mode: emit verbatim */ |
| 297 |
815 |
▶ | if (!ctx->in_php && ctx->state == ESHU_CODE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ctx->in_php && ctx->state == ESHU_CODE) { |
0 |
815 |
815 |
| if (!ctx->in_php && ctx->state == ESHU_CODE) { |
0 |
0 |
0 |
|
| 298 |
|
| /* Check if this line re-enters PHP mode (<?php / <?=) */ |
| 299 |
0 |
| const char *p = content; |
| 300 |
0 |
▶ | while (p < eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < eol) { |
0 |
0 |
0 |
|
| 301 |
0 |
▶ | if (*p == '<' && p + 1 < eol && *(p + 1) == '?') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '<' && p + 1 < eol && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (*p == '<' && p + 1 < eol && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (*p == '<' && p + 1 < eol && *(p + 1) == '?') { |
0 |
0 |
0 |
|
| 302 |
|
| /* entering PHP mode on this line — but still emit verbatim */ |
| 303 |
0 |
| break; |
| 304 |
|
| } |
| 305 |
0 |
| p++; |
| 306 |
|
| } |
| 307 |
0 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 308 |
0 |
| eshu_buf_putc(out, '\n'); |
| 309 |
0 |
| eshu_php_scan_line(ctx, content, eol); |
| 310 |
0 |
| return; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| /* Heredoc/nowdoc body */ |
| 314 |
815 |
▶ | if (ctx->state == ESHU_PHP_HEREDOC || ctx->state == ESHU_PHP_NOWDOC) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->state == ESHU_PHP_HEREDOC || ctx->state == ESHU_PHP_NOWDOC) { |
810 |
5 |
815 |
| if (ctx->state == ESHU_PHP_HEREDOC || ctx->state == ESHU_PHP_NOWDOC) { |
2 |
808 |
810 |
|
| 315 |
7 |
▶ | if (*ctx->heredoc_tag && eshu_php_heredoc_ends(content, line_len, ctx->heredoc_tag)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*ctx->heredoc_tag && eshu_php_heredoc_ends(content, line_len, ctx->heredoc_tag)) { |
7 |
0 |
7 |
| if (*ctx->heredoc_tag && eshu_php_heredoc_ends(content, line_len, ctx->heredoc_tag)) { |
3 |
4 |
7 |
|
| 316 |
|
| /* end marker: emit at depth 0 (traditional) */ |
| 317 |
3 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 318 |
3 |
| eshu_buf_putc(out, '\n'); |
| 319 |
3 |
| ctx->state = ESHU_CODE; |
| 320 |
|
| } else { |
| 321 |
|
| /* body: verbatim */ |
| 322 |
4 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 323 |
4 |
| eshu_buf_putc(out, '\n'); |
| 324 |
|
| } |
| 325 |
7 |
| return; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
|
| /* Block comment continuation */ |
| 329 |
808 |
▶ | if (ctx->state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_COMMENT_BLOCK) { |
5 |
803 |
808 |
|
| 330 |
5 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 331 |
5 |
| eshu_buf_write_trimmed(out, line_start, (int)(eol - line_start)); |
| 332 |
5 |
| eshu_buf_putc(out, '\n'); |
| 333 |
5 |
| eshu_php_scan_line(ctx, content, eol); |
| 334 |
5 |
| return; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* Normal PHP code */ |
| 338 |
803 |
| indent_depth = ctx->depth; |
| 339 |
|
| |
| 340 |
|
| /* Clear case_extra if depth dropped below case_depth */ |
| 341 |
803 |
▶ | if (ctx->case_extra && ctx->depth < ctx->case_depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->case_extra && ctx->depth < ctx->case_depth) |
15 |
788 |
803 |
| if (ctx->case_extra && ctx->depth < ctx->case_depth) |
1 |
14 |
15 |
|
| 342 |
1 |
| ctx->case_extra = 0; |
| 343 |
|
| |
| 344 |
803 |
| int is_close = eshu_php_is_closing(*content); |
| 345 |
803 |
| int is_alt_close = eshu_php_is_alt_close(content, line_len); |
| 346 |
803 |
| int is_alt_mid = eshu_php_is_alt_middle(content, line_len); |
| 347 |
803 |
| int is_alt_op = eshu_php_is_alt_open(content, line_len); |
| 348 |
|
| |
| 349 |
803 |
▶ | if (is_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_close) { |
200 |
603 |
803 |
|
| 350 |
200 |
| indent_depth--; |
| 351 |
200 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
200 |
200 |
|
| 352 |
200 |
▶ | if (ctx->case_extra && ctx->depth > ctx->case_depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->case_extra && ctx->depth > ctx->case_depth) |
2 |
198 |
200 |
| if (ctx->case_extra && ctx->depth > ctx->case_depth) |
0 |
2 |
2 |
|
| 353 |
0 |
| indent_depth++; |
| 354 |
603 |
▶ | } else if (is_alt_close || is_alt_mid) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (is_alt_close || is_alt_mid) { |
598 |
5 |
603 |
| } else if (is_alt_close || is_alt_mid) { |
2 |
596 |
598 |
|
| 355 |
7 |
| indent_depth--; |
| 356 |
7 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
7 |
7 |
|
| 357 |
596 |
▶ | } else if (eshu_php_is_case_label(content, line_len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_php_is_case_label(content, line_len)) { |
6 |
590 |
596 |
|
| 358 |
6 |
| ctx->case_depth = ctx->depth; |
| 359 |
6 |
| ctx->case_extra = 1; |
| 360 |
590 |
▶ | } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
8 |
582 |
590 |
| } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
8 |
0 |
8 |
|
| 361 |
8 |
| indent_depth++; |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
803 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 365 |
803 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 366 |
803 |
| eshu_buf_putc(out, '\n'); |
| 367 |
|
| |
| 368 |
803 |
| eshu_php_scan_line(ctx, content, eol); |
| 369 |
|
| |
| 370 |
|
| /* Alt-syntax depth adjustments (no { } involved) */ |
| 371 |
803 |
▶ | if (is_alt_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_alt_close) { |
5 |
798 |
803 |
|
| 372 |
5 |
| ctx->depth--; |
| 373 |
5 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
5 |
5 |
|
| 374 |
|
| /* also clear case_extra if it was inside the alt block */ |
| 375 |
5 |
▶ | if (ctx->case_extra && ctx->depth <= ctx->case_depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->case_extra && ctx->depth <= ctx->case_depth) |
0 |
5 |
5 |
| if (ctx->case_extra && ctx->depth <= ctx->case_depth) |
0 |
0 |
0 |
|
| 376 |
0 |
| ctx->case_extra = 0; |
| 377 |
798 |
▶ | } else if (is_alt_op) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (is_alt_op) { |
5 |
793 |
798 |
|
| 378 |
5 |
| ctx->depth++; |
| 379 |
|
| } |
| 380 |
|
| /* is_alt_mid: depth already adjusted? No — previous alt block stays open. |
| 381 |
|
| * We just emitted `else:` at depth-1. Depth remains at its current value |
| 382 |
|
| * so the next body lines get the correct indent. */ |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 386 |
|
| * Public API |
| 387 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 388 |
|
| |
| 389 |
82 |
| static char *eshu_indent_php(const char *src, size_t src_len, |
| 390 |
|
| const eshu_config_t *cfg, size_t *out_len) { |
| 391 |
|
| eshu_php_ctx_t ctx; |
| 392 |
|
| eshu_buf_t out; |
| 393 |
82 |
| const char *p = src; |
| 394 |
82 |
| const char *end = src + src_len; |
| 395 |
|
| char *result; |
| 396 |
|
| |
| 397 |
82 |
| memset(&ctx, 0, sizeof(ctx)); |
| 398 |
82 |
| ctx.state = ESHU_CODE; |
| 399 |
82 |
| ctx.in_php = 1; /* default: PHP mode (most PHP files start with <?php) */ |
| 400 |
82 |
| ctx.cfg = *cfg; |
| 401 |
|
| |
| 402 |
82 |
| eshu_buf_init(&out, src_len + 256); |
| 403 |
|
| |
| 404 |
|
| { |
| 405 |
82 |
| int line_num = 1; |
| 406 |
962 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
880 |
82 |
962 |
|
| 407 |
880 |
| const char *eol = eshu_find_eol(p); |
| 408 |
|
| |
| 409 |
880 |
▶ | if (eshu_in_range(cfg, line_num)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(cfg, line_num)) { |
880 |
0 |
880 |
|
| 410 |
880 |
| eshu_php_process_line(&ctx, &out, p, eol); |
| 411 |
|
| } else { |
| 412 |
0 |
| size_t saved = out.len; |
| 413 |
0 |
| eshu_php_process_line(&ctx, &out, p, eol); |
| 414 |
0 |
| out.len = saved; |
| 415 |
0 |
| eshu_buf_write_trimmed(&out, p, (int)(eol - p)); |
| 416 |
0 |
| eshu_buf_putc(&out, '\n'); |
| 417 |
|
| } |
| 418 |
|
| |
| 419 |
880 |
| p = eol; |
| 420 |
880 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
880 |
0 |
880 |
|
| 421 |
880 |
| line_num++; |
| 422 |
|
| } |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
82 |
| eshu_buf_putc(&out, '\0'); |
| 426 |
82 |
| out.len--; |
| 427 |
82 |
| *out_len = out.len; |
| 428 |
82 |
| result = out.data; |
| 429 |
82 |
| return result; |
| 430 |
|
| } |
| 431 |
|
| |
| 432 |
|
| |
| 433 |
|
| #include "eshu_hl_util.h" |
| 434 |
|
| |
| 435 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 436 |
|
| * PHP highlighting |
| 437 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 438 |
|
| |
| 439 |
|
| static const char * const eshu_hl_php_kw[] = { |
| 440 |
|
| "abstract", "array", "as", "break", "callable", "case", "catch", |
| 441 |
|
| "class", "clone", "const", "continue", "declare", "default", "do", |
| 442 |
|
| "echo", "else", "elseif", "empty", "enddeclare", "endfor", |
| 443 |
|
| "endforeach", "endif", "endswitch", "endwhile", "enum", "exit", |
| 444 |
|
| "extends", "final", "finally", "fn", "for", "foreach", "function", |
| 445 |
|
| "global", "goto", "if", "implements", "include", "include_once", |
| 446 |
|
| "instanceof", "insteadof", "interface", "isset", "list", "match", |
| 447 |
|
| "namespace", "new", "null", "print", "private", "protected", |
| 448 |
|
| "public", "readonly", "require", "require_once", "return", |
| 449 |
|
| "static", "switch", "throw", "trait", "try", "unset", "use", |
| 450 |
|
| "var", "while", "yield", "false", "true", "NULL", "FALSE", "TRUE", |
| 451 |
|
| NULL |
| 452 |
|
| }; |
| 453 |
|
| |
| 454 |
|
| static const char * const eshu_hl_php_bi[] = { |
| 455 |
|
| "array_key_exists", "array_keys", "array_map", "array_merge", |
| 456 |
|
| "array_pop", "array_push", "array_shift", "array_slice", |
| 457 |
|
| "array_splice", "array_unshift", "array_values", "ceil", |
| 458 |
|
| "class_exists", "compact", "count", "date", "defined", |
| 459 |
|
| "explode", "file_exists", "file_get_contents", "file_put_contents", |
| 460 |
|
| "floor", "get_class", "header", "htmlentities", "htmlspecialchars", |
| 461 |
|
| "implode", "in_array", "intval", "is_array", "is_int", "is_null", |
| 462 |
|
| "is_numeric", "is_object", "is_string", "json_decode", "json_encode", |
| 463 |
|
| "max", "mb_strlen", "mb_strtolower", "mb_strtoupper", "mb_substr", |
| 464 |
|
| "method_exists", "microtime", "min", "number_format", "ob_end_clean", |
| 465 |
|
| "ob_start", "ord", "preg_match", "preg_replace", "preg_split", |
| 466 |
|
| "print_r", "rtrim", "session_start", "sort", "sprintf", "str_contains", |
| 467 |
|
| "str_pad", "str_repeat", "str_replace", "strlen", "strpos", |
| 468 |
|
| "strtolower", "strtoupper", "substr", "trim", "ucfirst", "var_dump", |
| 469 |
|
| "Exception", "InvalidArgumentException", "RuntimeException", |
| 470 |
|
| "LogicException", "BadMethodCallException", "OutOfRangeException", |
| 471 |
|
| "ArrayAccess", "Countable", "Iterator", "IteratorAggregate", |
| 472 |
|
| "Serializable", "Stringable", "Throwable", |
| 473 |
|
| "DateTime", "DateTimeImmutable", "DateInterval", "DateTimeInterface", |
| 474 |
|
| "PDO", "PDOStatement", "PDOException", |
| 475 |
|
| NULL |
| 476 |
|
| }; |
| 477 |
|
| |
| 478 |
47 |
| static char *eshu_highlight_php(const char *src, size_t src_len, size_t *out_len) { |
| 479 |
|
| eshu_buf_t out; |
| 480 |
47 |
| const char *p = src; |
| 481 |
47 |
| const char *end = src + src_len; |
| 482 |
|
| const char *plain; |
| 483 |
47 |
| int in_php = 1; /* assume PHP mode (may start with <?php) */ |
| 484 |
47 |
| int in_heredoc = 0; |
| 485 |
|
| char heredoc_tag[64]; |
| 486 |
47 |
| int heredoc_len = 0; |
| 487 |
|
| /* block comment state */ |
| 488 |
47 |
| int in_block_comment = 0; |
| 489 |
|
| |
| 490 |
|
| #define PHP_SPAN(cls, ts, te) do { \ |
| 491 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 492 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 493 |
|
| plain = (te); \ |
| 494 |
|
| } while (0) |
| 495 |
|
| |
| 496 |
47 |
| eshu_buf_init(&out, src_len * 2); |
| 497 |
47 |
| plain = p; |
| 498 |
|
| |
| 499 |
|
| /* strip leading <?php or <?= to enter PHP mode */ |
| 500 |
47 |
▶ | if (src_len >= 5 && strncmp(src, "<?php", 5) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (src_len >= 5 && strncmp(src, "<?php", 5) == 0) { |
44 |
3 |
47 |
| if (src_len >= 5 && strncmp(src, "<?php", 5) == 0) { |
43 |
1 |
44 |
|
| 501 |
|
| /* pass through verbatim */ |
| 502 |
43 |
▶ | } else if (src_len >= 2 && strncmp(src, "<?", 2) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (src_len >= 2 && strncmp(src, "<?", 2) == 0) { |
43 |
0 |
43 |
|
| 503 |
|
| /* pass through */ |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
560 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
513 |
47 |
560 |
|
| 507 |
513 |
| unsigned char c = (unsigned char)*p; |
| 508 |
|
| |
| 509 |
|
| /* heredoc body: verbatim until end marker */ |
| 510 |
513 |
▶ | if (in_heredoc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_heredoc) { |
0 |
513 |
513 |
|
| 511 |
0 |
▶ | if (*p == '\n' || p == end - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\n' || p == end - 1) { |
0 |
0 |
0 |
| if (*p == '\n' || p == end - 1) { |
0 |
0 |
0 |
|
| 512 |
|
| /* check if next line starts with heredoc_tag */ |
| 513 |
0 |
| const char *nl = p + (*p == '\n' ? 1 : 1); |
| 514 |
0 |
▶ | if (nl < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl < end) { |
0 |
0 |
0 |
|
| 515 |
0 |
| const char *line_end = nl; |
| 516 |
0 |
▶ | while (line_end < end && *line_end != '\n') line_end++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (line_end < end && *line_end != '\n') line_end++; |
0 |
0 |
0 |
| while (line_end < end && *line_end != '\n') line_end++; |
0 |
0 |
0 |
|
| 517 |
|
| /* skip leading whitespace (PHP 7.3+ flexible heredoc) */ |
| 518 |
0 |
▶ | while (nl < line_end && (*nl == ' ' || *nl == '\t')) nl++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (nl < line_end && (*nl == ' ' || *nl == '\t')) nl++; |
0 |
0 |
0 |
| while (nl < line_end && (*nl == ' ' || *nl == '\t')) nl++; |
0 |
0 |
0 |
| while (nl < line_end && (*nl == ' ' || *nl == '\t')) nl++; |
0 |
0 |
0 |
|
| 519 |
0 |
▶ | if ((int)(line_end - nl) >= heredoc_len && |
Branch: 1 branch
| Branch | True | False | Total |
| if ((int)(line_end - nl) >= heredoc_len && |
0 |
0 |
0 |
|
| 520 |
0 |
▶ | strncmp(nl, heredoc_tag, (size_t)heredoc_len) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| strncmp(nl, heredoc_tag, (size_t)heredoc_len) == 0 && |
0 |
0 |
0 |
|
| 521 |
0 |
▶ | (nl + heredoc_len >= line_end || |
Branch: 1 branch
| Branch | True | False | Total |
| (nl + heredoc_len >= line_end || |
0 |
0 |
0 |
|
| 522 |
0 |
▶ | nl[heredoc_len] == ';' || nl[heredoc_len] == ',' || |
Branch: 2 branches
| Branch | True | False | Total |
| nl[heredoc_len] == ';' || nl[heredoc_len] == ',' || |
0 |
0 |
0 |
| nl[heredoc_len] == ';' || nl[heredoc_len] == ',' || |
0 |
0 |
0 |
|
| 523 |
0 |
▶ | nl[heredoc_len] == ' ' || nl[heredoc_len] == '\t')) { |
Branch: 2 branches
| Branch | True | False | Total |
| nl[heredoc_len] == ' ' || nl[heredoc_len] == '\t')) { |
0 |
0 |
0 |
| nl[heredoc_len] == ' ' || nl[heredoc_len] == '\t')) { |
0 |
0 |
0 |
|
| 524 |
|
| /* flush heredoc body as string span */ |
| 525 |
0 |
| PHP_SPAN("esh-s", plain, p + 1); |
| 526 |
0 |
| in_heredoc = 0; |
| 527 |
|
| } |
| 528 |
|
| } |
| 529 |
|
| } |
| 530 |
0 |
| p++; |
| 531 |
0 |
| continue; |
| 532 |
|
| } |
| 533 |
|
| |
| 534 |
|
| /* block comment */ |
| 535 |
513 |
▶ | if (in_block_comment) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_block_comment) { |
0 |
513 |
513 |
|
| 536 |
0 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
0 |
0 |
0 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
0 |
0 |
0 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
0 |
0 |
0 |
|
| 537 |
0 |
| PHP_SPAN("esh-c", plain, p + 2); |
| 538 |
0 |
| p += 2; |
| 539 |
0 |
| in_block_comment = 0; |
| 540 |
|
| } else { |
| 541 |
0 |
| p++; |
| 542 |
|
| } |
| 543 |
0 |
| continue; |
| 544 |
|
| } |
| 545 |
|
| |
| 546 |
|
| /* HTML mode: look for <?php / <?= */ |
| 547 |
513 |
▶ | if (!in_php) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_php) { |
0 |
513 |
513 |
|
| 548 |
0 |
▶ | if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
| if (c == '<' && p + 1 < end && *(p + 1) == '?') { |
0 |
0 |
0 |
|
| 549 |
0 |
| p += 2; |
| 550 |
0 |
▶ | if (p + 3 <= end && strncmp(p, "php", 3) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p + 3 <= end && strncmp(p, "php", 3) == 0) { |
0 |
0 |
0 |
| if (p + 3 <= end && strncmp(p, "php", 3) == 0) { |
0 |
0 |
0 |
|
| 551 |
0 |
| in_php = 1; |
| 552 |
|
| } else { |
| 553 |
0 |
| in_php = 1; |
| 554 |
|
| } |
| 555 |
0 |
| continue; |
| 556 |
|
| } |
| 557 |
0 |
| p++; |
| 558 |
0 |
| continue; |
| 559 |
|
| } |
| 560 |
|
| |
| 561 |
|
| /* PHP mode */ |
| 562 |
|
| |
| 563 |
|
| /* ?> — exit PHP mode */ |
| 564 |
513 |
▶ | if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
2 |
511 |
513 |
| if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
2 |
0 |
2 |
| if (c == '?' && p + 1 < end && *(p + 1) == '>') { |
1 |
1 |
2 |
|
| 565 |
1 |
| in_php = 0; |
| 566 |
1 |
| p += 2; |
| 567 |
1 |
| continue; |
| 568 |
|
| } |
| 569 |
|
| |
| 570 |
|
| /* line comment // */ |
| 571 |
512 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
510 |
512 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
0 |
2 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
1 |
1 |
2 |
|
| 572 |
1 |
| const char *ls = p; |
| 573 |
16 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
15 |
1 |
16 |
| while (p < end && *p != '\n') p++; |
15 |
0 |
15 |
|
| 574 |
1 |
| PHP_SPAN("esh-c", ls, p); |
| 575 |
1 |
| continue; |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
|
| /* line comment # */ |
| 579 |
511 |
▶ | if (c == '#' && !(p + 1 < end && *(p + 1) == '[')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '#' && !(p + 1 < end && *(p + 1) == '[')) { |
2 |
509 |
511 |
| if (c == '#' && !(p + 1 < end && *(p + 1) == '[')) { |
2 |
0 |
2 |
| if (c == '#' && !(p + 1 < end && *(p + 1) == '[')) { |
1 |
1 |
2 |
|
| 580 |
1 |
| const char *ls = p; |
| 581 |
15 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
14 |
1 |
15 |
| while (p < end && *p != '\n') p++; |
14 |
0 |
14 |
|
| 582 |
1 |
| PHP_SPAN("esh-c", ls, p); |
| 583 |
1 |
| continue; |
| 584 |
|
| } |
| 585 |
|
| |
| 586 |
|
| /* block comment */ |
| 587 |
510 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
509 |
510 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
|
| 588 |
1 |
| const char *cs = p; |
| 589 |
1 |
| p += 2; |
| 590 |
8 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
8 |
0 |
8 |
|
| 591 |
8 |
▶ | if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
7 |
8 |
| if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
0 |
1 |
| if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
0 |
1 |
|
| 592 |
1 |
| p += 2; |
| 593 |
1 |
| break; |
| 594 |
|
| } |
| 595 |
7 |
| p++; |
| 596 |
|
| } |
| 597 |
1 |
| PHP_SPAN("esh-c", cs, p); |
| 598 |
1 |
| continue; |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
|
| /* double-quoted string */ |
| 602 |
509 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
11 |
498 |
509 |
|
| 603 |
11 |
| const char *qs = p; |
| 604 |
11 |
| p++; |
| 605 |
59 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
59 |
0 |
59 |
| while (p < end && *p != '"') { |
48 |
11 |
59 |
|
| 606 |
48 |
▶ | if (*p == '\\' && p + 1 < end) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\' && p + 1 < end) p++; |
0 |
48 |
48 |
| if (*p == '\\' && p + 1 < end) p++; |
0 |
0 |
0 |
|
| 607 |
48 |
| p++; |
| 608 |
|
| } |
| 609 |
11 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
11 |
0 |
11 |
|
| 610 |
11 |
| PHP_SPAN("esh-s", qs, p); |
| 611 |
11 |
| continue; |
| 612 |
|
| } |
| 613 |
|
| |
| 614 |
|
| /* single-quoted string */ |
| 615 |
498 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
1 |
497 |
498 |
|
| 616 |
1 |
| const char *qs = p; |
| 617 |
1 |
| p++; |
| 618 |
14 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
14 |
0 |
14 |
| while (p < end && *p != '\'') { |
13 |
1 |
14 |
|
| 619 |
13 |
▶ | if (*p == '\\' && p + 1 < end) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\' && p + 1 < end) p++; |
0 |
13 |
13 |
| if (*p == '\\' && p + 1 < end) p++; |
0 |
0 |
0 |
|
| 620 |
13 |
| p++; |
| 621 |
|
| } |
| 622 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 623 |
1 |
| PHP_SPAN("esh-s", qs, p); |
| 624 |
1 |
| continue; |
| 625 |
|
| } |
| 626 |
|
| |
| 627 |
|
| /* heredoc: <<< */ |
| 628 |
497 |
▶ | if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
4 |
493 |
497 |
| if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
4 |
0 |
4 |
| if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
0 |
4 |
4 |
| if (c == '<' && p + 2 < end && *(p+1) == '<' && *(p+2) == '<') { |
0 |
0 |
0 |
|
| 629 |
0 |
| const char *hs = p; |
| 630 |
0 |
| int q = 0; |
| 631 |
0 |
| p += 3; |
| 632 |
0 |
▶ | if (p < end && (*p == '\'' || *p == '"')) { q = *p; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == '\'' || *p == '"')) { q = *p; p++; } |
0 |
0 |
0 |
| if (p < end && (*p == '\'' || *p == '"')) { q = *p; p++; } |
0 |
0 |
0 |
| if (p < end && (*p == '\'' || *p == '"')) { q = *p; p++; } |
0 |
0 |
0 |
|
| 633 |
0 |
| heredoc_len = 0; |
| 634 |
0 |
▶ | while (p < end && *p != '\n' && *p != '\r' && |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n' && *p != '\r' && |
0 |
0 |
0 |
| while (p < end && *p != '\n' && *p != '\r' && |
0 |
0 |
0 |
| while (p < end && *p != '\n' && *p != '\r' && |
0 |
0 |
0 |
|
| 635 |
0 |
▶ | (isalnum((unsigned char)*p) || *p == '_')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (isalnum((unsigned char)*p) || *p == '_')) { |
0 |
0 |
0 |
| (isalnum((unsigned char)*p) || *p == '_')) { |
0 |
0 |
0 |
|
| 636 |
0 |
▶ | if (heredoc_len < 63) heredoc_tag[heredoc_len++] = *p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (heredoc_len < 63) heredoc_tag[heredoc_len++] = *p; |
0 |
0 |
0 |
|
| 637 |
0 |
| p++; |
| 638 |
|
| } |
| 639 |
0 |
| heredoc_tag[heredoc_len] = '\0'; |
| 640 |
0 |
▶ | if (q && p < end && *p == (char)q) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (q && p < end && *p == (char)q) p++; |
0 |
0 |
0 |
| if (q && p < end && *p == (char)q) p++; |
0 |
0 |
0 |
| if (q && p < end && *p == (char)q) p++; |
0 |
0 |
0 |
|
| 641 |
0 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
0 |
0 |
0 |
| while (p < end && *p != '\n') p++; |
0 |
0 |
0 |
|
| 642 |
0 |
| PHP_SPAN("esh-s", hs, p); |
| 643 |
0 |
▶ | if (heredoc_len > 0) in_heredoc = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (heredoc_len > 0) in_heredoc = 1; |
0 |
0 |
0 |
|
| 644 |
0 |
| continue; |
| 645 |
|
| } |
| 646 |
|
| |
| 647 |
|
| /* number */ |
| 648 |
497 |
▶ | if (isdigit(c) || (c == '.' && p + 1 < end && isdigit((unsigned char)*(p+1)))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (isdigit(c) || (c == '.' && p + 1 < end && isdigit((unsigned char)*(p+1)))) { |
482 |
15 |
497 |
| if (isdigit(c) || (c == '.' && p + 1 < end && isdigit((unsigned char)*(p+1)))) { |
0 |
482 |
482 |
| if (isdigit(c) || (c == '.' && p + 1 < end && isdigit((unsigned char)*(p+1)))) { |
0 |
0 |
0 |
| if (isdigit(c) || (c == '.' && p + 1 < end && isdigit((unsigned char)*(p+1)))) { |
0 |
0 |
0 |
|
| 649 |
15 |
| const char *ns = p; |
| 650 |
15 |
▶ | if (c == '0' && p + 1 < end && (*(p+1) == 'x' || *(p+1) == 'X')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '0' && p + 1 < end && (*(p+1) == 'x' || *(p+1) == 'X')) { |
4 |
11 |
15 |
| if (c == '0' && p + 1 < end && (*(p+1) == 'x' || *(p+1) == 'X')) { |
4 |
0 |
4 |
| if (c == '0' && p + 1 < end && (*(p+1) == 'x' || *(p+1) == 'X')) { |
3 |
1 |
4 |
| if (c == '0' && p + 1 < end && (*(p+1) == 'x' || *(p+1) == 'X')) { |
0 |
3 |
3 |
|
| 651 |
1 |
| p += 2; |
| 652 |
3 |
▶ | while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
1 |
3 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
0 |
2 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 653 |
14 |
▶ | } else if (c == '0' && p + 1 < end && (*(p+1) == 'b' || *(p+1) == 'B')) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'b' || *(p+1) == 'B')) { |
3 |
11 |
14 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'b' || *(p+1) == 'B')) { |
3 |
0 |
3 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'b' || *(p+1) == 'B')) { |
2 |
1 |
3 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'b' || *(p+1) == 'B')) { |
0 |
2 |
2 |
|
| 654 |
1 |
| p += 2; |
| 655 |
5 |
▶ | while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
4 |
1 |
5 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
2 |
2 |
4 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
2 |
0 |
2 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
0 |
0 |
0 |
|
| 656 |
13 |
▶ | } else if (c == '0' && p + 1 < end && (*(p+1) == 'o' || *(p+1) == 'O')) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'o' || *(p+1) == 'O')) { |
2 |
11 |
13 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'o' || *(p+1) == 'O')) { |
2 |
0 |
2 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'o' || *(p+1) == 'O')) { |
2 |
0 |
2 |
| } else if (c == '0' && p + 1 < end && (*(p+1) == 'o' || *(p+1) == 'O')) { |
0 |
2 |
2 |
|
| 657 |
0 |
| p += 2; |
| 658 |
0 |
▶ | while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
0 |
0 |
0 |
|
| 659 |
|
| } else { |
| 660 |
29 |
▶ | while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
28 |
1 |
29 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
16 |
12 |
28 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
12 |
12 |
|
| 661 |
13 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
12 |
1 |
13 |
| if (p < end && *p == '.') { |
1 |
11 |
12 |
|
| 662 |
1 |
| p++; |
| 663 |
3 |
▶ | while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
1 |
3 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
0 |
2 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 664 |
|
| } |
| 665 |
13 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
11 |
2 |
13 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
11 |
0 |
11 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
11 |
11 |
|
| 666 |
0 |
| p++; |
| 667 |
0 |
▶ | if (p < end && (*p == '+' || *p == '-')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == '+' || *p == '-')) p++; |
0 |
0 |
0 |
| if (p < end && (*p == '+' || *p == '-')) p++; |
0 |
0 |
0 |
| if (p < end && (*p == '+' || *p == '-')) p++; |
0 |
0 |
0 |
|
| 668 |
0 |
▶ | while (p < end && isdigit((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && isdigit((unsigned char)*p)) p++; |
0 |
0 |
0 |
| while (p < end && isdigit((unsigned char)*p)) p++; |
0 |
0 |
0 |
|
| 669 |
|
| } |
| 670 |
|
| } |
| 671 |
15 |
| PHP_SPAN("esh-n", ns, p); |
| 672 |
15 |
| continue; |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| /* variable $name */ |
| 676 |
482 |
▶ | if (c == '$' && p + 1 < end && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '$' && p + 1 < end && |
57 |
425 |
482 |
| if (c == '$' && p + 1 < end && |
57 |
0 |
57 |
|
| 677 |
57 |
▶ | (isalpha((unsigned char)*(p+1)) || *(p+1) == '_')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (isalpha((unsigned char)*(p+1)) || *(p+1) == '_')) { |
0 |
57 |
57 |
| (isalpha((unsigned char)*(p+1)) || *(p+1) == '_')) { |
0 |
0 |
0 |
|
| 678 |
57 |
| const char *vs = p; |
| 679 |
57 |
| p++; |
| 680 |
199 |
▶ | while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
199 |
0 |
199 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
141 |
58 |
199 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
1 |
57 |
58 |
|
| 681 |
57 |
| PHP_SPAN("esh-p", vs, p); |
| 682 |
57 |
| continue; |
| 683 |
|
| } |
| 684 |
|
| |
| 685 |
|
| /* attribute #[...] */ |
| 686 |
425 |
▶ | if (c == '#' && p + 1 < end && *(p+1) == '[') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '#' && p + 1 < end && *(p+1) == '[') { |
1 |
424 |
425 |
| if (c == '#' && p + 1 < end && *(p+1) == '[') { |
1 |
0 |
1 |
| if (c == '#' && p + 1 < end && *(p+1) == '[') { |
1 |
0 |
1 |
|
| 687 |
1 |
| const char *as = p; |
| 688 |
1 |
| int depth = 0; |
| 689 |
1 |
| p++; |
| 690 |
12 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
12 |
0 |
12 |
|
| 691 |
12 |
▶ | if (*p == '[') { depth++; p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '[') { depth++; p++; } |
1 |
11 |
12 |
|
| 692 |
11 |
▶ | else if (*p == ']') { depth--; p++; if (depth <= 0) break; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (*p == ']') { depth--; p++; if (depth <= 0) break; } |
1 |
10 |
11 |
| else if (*p == ']') { depth--; p++; if (depth <= 0) break; } |
1 |
0 |
1 |
|
| 693 |
10 |
| else p++; |
| 694 |
|
| } |
| 695 |
1 |
| PHP_SPAN("esh-p", as, p); |
| 696 |
1 |
| continue; |
| 697 |
|
| } |
| 698 |
|
| |
| 699 |
|
| /* keyword / identifier */ |
| 700 |
424 |
▶ | if (isalpha(c) || c == '_') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (isalpha(c) || c == '_') { |
342 |
82 |
424 |
| if (isalpha(c) || c == '_') { |
0 |
342 |
342 |
|
| 701 |
82 |
| const char *ts = p; |
| 702 |
587 |
▶ | while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
586 |
1 |
587 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
497 |
89 |
586 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
8 |
81 |
89 |
|
| 703 |
82 |
▶ | if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_php_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_php_kw)) { |
49 |
33 |
82 |
|
| 704 |
49 |
| PHP_SPAN("esh-k", ts, p); |
| 705 |
33 |
▶ | } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_php_bi)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_php_bi)) { |
18 |
15 |
33 |
|
| 706 |
18 |
| PHP_SPAN("esh-b", ts, p); |
| 707 |
|
| } |
| 708 |
82 |
| plain = p; |
| 709 |
82 |
| continue; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
342 |
| p++; |
| 713 |
|
| } |
| 714 |
|
| |
| 715 |
47 |
| eshu_hl_flush(&out, plain, end); |
| 716 |
47 |
| eshu_buf_putc(&out, '\0'); |
| 717 |
47 |
| *out_len = out.len - 1; |
| 718 |
47 |
| return out.data; |
| 719 |
|
| |
| 720 |
|
| #undef PHP_SPAN |
| 721 |
|
| } |
| 722 |
|
| |
| 723 |
|
| #endif /* ESHU_PHP_H */ |