| 1 |
|
| /* |
| 2 |
|
| * eshu_xs.h — XS file indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Dual-mode: delegates to eshu_c.h for C code above `MODULE =`, |
| 5 |
|
| * then uses XS-specific rules for XSUB declarations below. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #ifndef ESHU_XS_H |
| 9 |
|
| #define ESHU_XS_H |
| 10 |
|
| |
| 11 |
|
| #include "eshu.h" |
| 12 |
|
| #include "eshu_c.h" |
| 13 |
|
| |
| 14 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 15 |
|
| * XS mode tracking |
| 16 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 17 |
|
| |
| 18 |
|
| enum eshu_xs_mode { |
| 19 |
|
| ESHU_XS_C_MODE, /* before MODULE = line */ |
| 20 |
|
| ESHU_XS_XSUB_MODE /* after MODULE = line */ |
| 21 |
|
| }; |
| 22 |
|
| |
| 23 |
|
| enum eshu_xs_section { |
| 24 |
|
| ESHU_XS_NONE, /* between XSUBs / return type line */ |
| 25 |
|
| ESHU_XS_PARAMS, /* parameter declarations */ |
| 26 |
|
| ESHU_XS_LABEL, /* just saw a label (CODE: etc) */ |
| 27 |
|
| ESHU_XS_BODY /* inside a label body */ |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
|
| typedef struct { |
| 31 |
|
| enum eshu_xs_mode mode; |
| 32 |
|
| enum eshu_xs_section section; |
| 33 |
|
| eshu_ctx_t c_ctx; /* C scanner context */ |
| 34 |
|
| int c_depth; /* brace depth in CODE: */ |
| 35 |
|
| int is_boot; /* inside BOOT section */ |
| 36 |
|
| eshu_config_t cfg; |
| 37 |
|
| } eshu_xs_ctx_t; |
| 38 |
|
| |
| 39 |
96 |
| static void eshu_xs_ctx_init(eshu_xs_ctx_t *ctx, const eshu_config_t *cfg) { |
| 40 |
96 |
| ctx->mode = ESHU_XS_C_MODE; |
| 41 |
96 |
| ctx->section = ESHU_XS_NONE; |
| 42 |
96 |
| ctx->c_depth = 0; |
| 43 |
96 |
| ctx->is_boot = 0; |
| 44 |
96 |
| ctx->cfg = *cfg; |
| 45 |
96 |
| eshu_ctx_init(&ctx->c_ctx, cfg); |
| 46 |
96 |
| } |
| 47 |
|
| |
| 48 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 49 |
|
| * Detection helpers |
| 50 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 51 |
|
| |
| 52 |
|
| /* Check if the line starts with MODULE followed by optional |
| 53 |
|
| * whitespace and '=' */ |
| 54 |
1310 |
| static int eshu_xs_is_module_line(const char *content, const char *eol) { |
| 55 |
1310 |
| const char *p = content; |
| 56 |
1310 |
▶ | if (eol - p < 8) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol - p < 8) return 0; |
509 |
801 |
1310 |
|
| 57 |
801 |
▶ | if (memcmp(p, "MODULE", 6) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, "MODULE", 6) != 0) return 0; |
704 |
97 |
801 |
|
| 58 |
97 |
| p += 6; |
| 59 |
186 |
▶ | while (p < eol && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
186 |
0 |
186 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
89 |
97 |
186 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
0 |
97 |
97 |
|
| 60 |
97 |
▶ | return (p < eol && *p == '='); |
Branch: 2 branches
| Branch | True | False | Total |
| return (p < eol && *p == '='); |
97 |
0 |
97 |
| return (p < eol && *p == '='); |
97 |
0 |
97 |
|
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| /* XS labels: CODE, INIT, OUTPUT, PREINIT, CLEANUP, POSTCALL, |
| 64 |
|
| * PPCODE, BOOT, CASE, INTERFACE, INTERFACE_MACRO, PROTOTYPES, |
| 65 |
|
| * VERSIONCHECK, INCLUDE, FALLBACK, OVERLOAD, ALIAS, ATTRS */ |
| 66 |
1067 |
| static int eshu_xs_is_label(const char *content, const char *eol, |
| 67 |
|
| int *is_boot) { |
| 68 |
1067 |
| const char *p = content; |
| 69 |
|
| const char *start; |
| 70 |
|
| int len; |
| 71 |
|
| |
| 72 |
1067 |
| *is_boot = 0; |
| 73 |
|
| |
| 74 |
|
| /* must start with alpha */ |
| 75 |
1067 |
▶ | if (!isalpha((unsigned char)*p)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isalpha((unsigned char)*p)) return 0; |
44 |
1023 |
1067 |
|
| 76 |
|
| |
| 77 |
1023 |
| start = p; |
| 78 |
6033 |
▶ | while (p < eol && (isalpha((unsigned char)*p) || *p == '_')) |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < eol && (isalpha((unsigned char)*p) || *p == '_')) |
5883 |
150 |
6033 |
| while (p < eol && (isalpha((unsigned char)*p) || *p == '_')) |
4915 |
968 |
5883 |
| while (p < eol && (isalpha((unsigned char)*p) || *p == '_')) |
95 |
873 |
968 |
|
| 79 |
5010 |
| p++; |
| 80 |
1023 |
| len = (int)(p - start); |
| 81 |
|
| |
| 82 |
|
| /* must be followed by ':' (possibly with whitespace) */ |
| 83 |
1425 |
▶ | while (p < eol && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
1275 |
150 |
1425 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
402 |
873 |
1275 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
0 |
873 |
873 |
|
| 84 |
1023 |
▶ | if (p >= eol || *p != ':') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= eol || *p != ':') return 0; |
873 |
150 |
1023 |
| if (p >= eol || *p != ':') return 0; |
661 |
212 |
873 |
|
| 85 |
|
| |
| 86 |
|
| /* check it's not a C label like 'default:' or 'case:' */ |
| 87 |
|
| /* it also must not be a :: (package separator) */ |
| 88 |
212 |
▶ | if (p + 1 < eol && *(p + 1) == ':') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p + 1 < eol && *(p + 1) == ':') return 0; |
4 |
208 |
212 |
| if (p + 1 < eol && *(p + 1) == ':') return 0; |
0 |
4 |
4 |
|
| 89 |
|
| |
| 90 |
|
| /* Known XS labels */ |
| 91 |
212 |
▶ | if ((len == 4 && memcmp(start, "CODE", 4) == 0) || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((len == 4 && memcmp(start, "CODE", 4) == 0) || |
103 |
109 |
212 |
| if ((len == 4 && memcmp(start, "CODE", 4) == 0) || |
99 |
4 |
103 |
| if ((len == 4 && memcmp(start, "CODE", 4) == 0) || |
4 |
109 |
113 |
|
| 92 |
113 |
▶ | (len == 4 && memcmp(start, "INIT", 4) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 4 && memcmp(start, "INIT", 4) == 0) || |
4 |
0 |
4 |
| (len == 4 && memcmp(start, "INIT", 4) == 0) || |
75 |
34 |
109 |
|
| 93 |
109 |
▶ | (len == 6 && memcmp(start, "OUTPUT", 6) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 6 && memcmp(start, "OUTPUT", 6) == 0) || |
69 |
6 |
75 |
| (len == 6 && memcmp(start, "OUTPUT", 6) == 0) || |
27 |
13 |
40 |
|
| 94 |
40 |
▶ | (len == 7 && memcmp(start, "PREINIT", 7) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 && memcmp(start, "PREINIT", 7) == 0) || |
20 |
7 |
27 |
| (len == 7 && memcmp(start, "PREINIT", 7) == 0) || |
7 |
13 |
20 |
|
| 95 |
20 |
▶ | (len == 7 && memcmp(start, "CLEANUP", 7) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 && memcmp(start, "CLEANUP", 7) == 0) || |
3 |
4 |
7 |
| (len == 7 && memcmp(start, "CLEANUP", 7) == 0) || |
1 |
16 |
17 |
|
| 96 |
17 |
▶ | (len == 8 && memcmp(start, "POSTCALL", 8) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 8 && memcmp(start, "POSTCALL", 8) == 0) || |
1 |
0 |
1 |
| (len == 8 && memcmp(start, "POSTCALL", 8) == 0) || |
6 |
10 |
16 |
|
| 97 |
16 |
▶ | (len == 6 && memcmp(start, "PPCODE", 6) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 6 && memcmp(start, "PPCODE", 6) == 0) || |
6 |
0 |
6 |
| (len == 6 && memcmp(start, "PPCODE", 6) == 0) || |
0 |
10 |
10 |
|
| 98 |
10 |
▶ | (len == 4 && memcmp(start, "CASE", 4) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 4 && memcmp(start, "CASE", 4) == 0) || |
0 |
0 |
0 |
| (len == 4 && memcmp(start, "CASE", 4) == 0) || |
1 |
9 |
10 |
|
| 99 |
10 |
▶ | (len == 9 && memcmp(start, "INTERFACE", 9) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 9 && memcmp(start, "INTERFACE", 9) == 0) || |
0 |
1 |
1 |
| (len == 9 && memcmp(start, "INTERFACE", 9) == 0) || |
0 |
10 |
10 |
|
| 100 |
10 |
▶ | (len == 15 && memcmp(start, "INTERFACE_MACRO", 15) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 15 && memcmp(start, "INTERFACE_MACRO", 15) == 0) || |
0 |
0 |
0 |
| (len == 15 && memcmp(start, "INTERFACE_MACRO", 15) == 0) || |
0 |
10 |
10 |
|
| 101 |
10 |
▶ | (len == 10 && memcmp(start, "PROTOTYPES", 10) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 10 && memcmp(start, "PROTOTYPES", 10) == 0) || |
0 |
0 |
0 |
| (len == 10 && memcmp(start, "PROTOTYPES", 10) == 0) || |
0 |
10 |
10 |
|
| 102 |
10 |
▶ | (len == 12 && memcmp(start, "VERSIONCHECK", 12) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 12 && memcmp(start, "VERSIONCHECK", 12) == 0) || |
0 |
0 |
0 |
| (len == 12 && memcmp(start, "VERSIONCHECK", 12) == 0) || |
4 |
6 |
10 |
|
| 103 |
10 |
▶ | (len == 7 && memcmp(start, "INCLUDE", 7) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 && memcmp(start, "INCLUDE", 7) == 0) || |
1 |
3 |
4 |
| (len == 7 && memcmp(start, "INCLUDE", 7) == 0) || |
0 |
9 |
9 |
|
| 104 |
9 |
▶ | (len == 8 && memcmp(start, "FALLBACK", 8) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 8 && memcmp(start, "FALLBACK", 8) == 0) || |
0 |
0 |
0 |
| (len == 8 && memcmp(start, "FALLBACK", 8) == 0) || |
0 |
9 |
9 |
|
| 105 |
9 |
▶ | (len == 8 && memcmp(start, "OVERLOAD", 8) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 8 && memcmp(start, "OVERLOAD", 8) == 0) || |
0 |
0 |
0 |
| (len == 8 && memcmp(start, "OVERLOAD", 8) == 0) || |
5 |
4 |
9 |
|
| 106 |
9 |
▶ | (len == 5 && memcmp(start, "ALIAS", 5) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 5 && memcmp(start, "ALIAS", 5) == 0) || |
4 |
1 |
5 |
| (len == 5 && memcmp(start, "ALIAS", 5) == 0) || |
1 |
4 |
5 |
|
| 107 |
1 |
▶ | (len == 5 && memcmp(start, "ATTRS", 5) == 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| (len == 5 && memcmp(start, "ATTRS", 5) == 0)) { |
0 |
1 |
1 |
|
| 108 |
|
| /* not a label that belongs to these */ |
| 109 |
|
| } else { |
| 110 |
5 |
| return 0; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
207 |
▶ | if (len == 4 && memcmp(start, "BOOT", 4) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == 4 && memcmp(start, "BOOT", 4) == 0) |
103 |
104 |
207 |
| if (len == 4 && memcmp(start, "BOOT", 4) == 0) |
0 |
103 |
103 |
|
| 114 |
0 |
| *is_boot = 1; |
| 115 |
|
| /* BOOT is checked above but it's not in the list — add it */ |
| 116 |
|
| |
| 117 |
207 |
| return 1; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* Check for BOOT: label specifically */ |
| 121 |
1079 |
| static int eshu_xs_is_boot_label(const char *content, const char *eol) { |
| 122 |
1079 |
| const char *p = content; |
| 123 |
1079 |
▶ | if (eol - p < 5) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol - p < 5) return 0; |
133 |
946 |
1079 |
|
| 124 |
946 |
▶ | if (memcmp(p, "BOOT", 4) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, "BOOT", 4) != 0) return 0; |
934 |
12 |
946 |
|
| 125 |
12 |
| p += 4; |
| 126 |
12 |
▶ | while (p < eol && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
12 |
0 |
12 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
0 |
12 |
12 |
| while (p < eol && (*p == ' ' || *p == '\t')) p++; |
0 |
12 |
12 |
|
| 127 |
12 |
▶ | return (p < eol && *p == ':' && (p + 1 >= eol || *(p + 1) != ':')); |
Branch: 4 branches
| Branch | True | False | Total |
| return (p < eol && *p == ':' && (p + 1 >= eol || *(p + 1) != ':')); |
12 |
0 |
12 |
| return (p < eol && *p == ':' && (p + 1 >= eol || *(p + 1) != ':')); |
12 |
0 |
12 |
| return (p < eol && *p == ':' && (p + 1 >= eol || *(p + 1) != ':')); |
0 |
12 |
12 |
| return (p < eol && *p == ':' && (p + 1 >= eol || *(p + 1) != ':')); |
0 |
0 |
0 |
|
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| /* Detect if line is a new XSUB return type / function header. |
| 131 |
|
| * In XS mode at depth 0, a line that starts with an identifier |
| 132 |
|
| * (C type name) at column 0 and is NOT a label indicates |
| 133 |
|
| * a new XSUB. */ |
| 134 |
0 |
| static int eshu_xs_is_xsub_start(const char *content, const char *eol) { |
| 135 |
0 |
| const char *p = content; |
| 136 |
|
| |
| 137 |
|
| /* Must start with alpha or underscore (type name) */ |
| 138 |
0 |
▶ | if (!isalpha((unsigned char)*p) && *p != '_') |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isalpha((unsigned char)*p) && *p != '_') |
0 |
0 |
0 |
| if (!isalpha((unsigned char)*p) && *p != '_') |
0 |
0 |
0 |
|
| 139 |
0 |
| return 0; |
| 140 |
|
| |
| 141 |
|
| /* Skip the identifier */ |
| 142 |
0 |
▶ | while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
Branch: 6 branches
| Branch | True | False | Total |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
| while (p < eol && (isalnum((unsigned char)*p) || *p == '_' || *p == '*' || *p == ' ' || *p == '\t')) |
0 |
0 |
0 |
|
| 143 |
0 |
| p++; |
| 144 |
|
| |
| 145 |
|
| /* Could be a function name with parens, or just a type */ |
| 146 |
|
| /* This is sufficient — we rely on being in XS mode at depth 0 */ |
| 147 |
0 |
| return 1; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 151 |
|
| * Scan a line for C nesting changes (simplified for XS body) |
| 152 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 153 |
|
| |
| 154 |
510 |
| static void eshu_xs_scan_c_nesting(eshu_xs_ctx_t *ctx, |
| 155 |
|
| const char *p, const char *end) { |
| 156 |
|
| /* Track {}/()/ nesting within XSUB body code using the C scanner's |
| 157 |
|
| * state machine for strings/comments, but tracking depth locally */ |
| 158 |
10456 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
9946 |
510 |
10456 |
|
| 159 |
9946 |
| char c = *p; |
| 160 |
|
| |
| 161 |
9946 |
| switch (ctx->c_ctx.state) { |
| 162 |
9327 |
| case ESHU_CODE: |
| 163 |
9327 |
▶ | if (c == '{') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '{') { |
33 |
9294 |
9327 |
|
| 164 |
33 |
| ctx->c_depth++; |
| 165 |
9294 |
▶ | } else if (c == '}') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '}') { |
33 |
9261 |
9294 |
|
| 166 |
33 |
| ctx->c_depth--; |
| 167 |
33 |
▶ | if (ctx->c_depth < 0) ctx->c_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->c_depth < 0) ctx->c_depth = 0; |
0 |
33 |
33 |
|
| 168 |
9261 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
67 |
9194 |
9261 |
|
| 169 |
67 |
| ctx->c_ctx.state = ESHU_STRING_DQ; |
| 170 |
9194 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
3 |
9191 |
9194 |
|
| 171 |
3 |
| ctx->c_ctx.state = ESHU_CHAR_LIT; |
| 172 |
9191 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
7 |
9184 |
9191 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
7 |
0 |
7 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
0 |
7 |
7 |
|
| 173 |
0 |
| return; /* line comment */ |
| 174 |
9191 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
7 |
9184 |
9191 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
7 |
0 |
7 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
4 |
3 |
7 |
|
| 175 |
4 |
| ctx->c_ctx.state = ESHU_COMMENT_BLOCK; |
| 176 |
4 |
| p++; |
| 177 |
|
| } |
| 178 |
9327 |
| break; |
| 179 |
|
| |
| 180 |
525 |
| case ESHU_STRING_DQ: |
| 181 |
525 |
▶ | if (c == '\\' && p + 1 < end) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) p++; |
15 |
510 |
525 |
| if (c == '\\' && p + 1 < end) p++; |
15 |
0 |
15 |
|
| 182 |
510 |
▶ | else if (c == '"') ctx->c_ctx.state = ESHU_CODE; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '"') ctx->c_ctx.state = ESHU_CODE; |
67 |
443 |
510 |
|
| 183 |
525 |
| break; |
| 184 |
|
| |
| 185 |
6 |
| case ESHU_CHAR_LIT: |
| 186 |
6 |
▶ | if (c == '\\' && p + 1 < end) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) p++; |
3 |
3 |
6 |
| if (c == '\\' && p + 1 < end) p++; |
3 |
0 |
3 |
|
| 187 |
3 |
▶ | else if (c == '\'') ctx->c_ctx.state = ESHU_CODE; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == '\'') ctx->c_ctx.state = ESHU_CODE; |
3 |
0 |
3 |
|
| 188 |
6 |
| break; |
| 189 |
|
| |
| 190 |
88 |
| case ESHU_COMMENT_BLOCK: |
| 191 |
88 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
4 |
84 |
88 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
4 |
0 |
4 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
4 |
0 |
4 |
|
| 192 |
4 |
| ctx->c_ctx.state = ESHU_CODE; |
| 193 |
4 |
| p++; |
| 194 |
|
| } |
| 195 |
88 |
| break; |
| 196 |
|
| |
| 197 |
0 |
| default: |
| 198 |
0 |
| break; |
| 199 |
|
| } |
| 200 |
9946 |
| p++; |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 205 |
|
| * Process a single XS line |
| 206 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 207 |
|
| |
| 208 |
1451 |
| static void eshu_xs_process_line(eshu_xs_ctx_t *ctx, eshu_buf_t *out, |
| 209 |
|
| const char *line_start, const char *eol) { |
| 210 |
1451 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 211 |
|
| int line_len; |
| 212 |
|
| |
| 213 |
|
| /* empty line — preserve it */ |
| 214 |
1451 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
141 |
1310 |
1451 |
|
| 215 |
141 |
| eshu_buf_putc(out, '\n'); |
| 216 |
141 |
| return; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
1310 |
| line_len = (int)(eol - content); |
| 220 |
|
| |
| 221 |
|
| /* ── C mode: before MODULE = ── */ |
| 222 |
1310 |
▶ | if (ctx->mode == ESHU_XS_C_MODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->mode == ESHU_XS_C_MODE) { |
225 |
1085 |
1310 |
|
| 223 |
|
| /* Check if this is the MODULE line */ |
| 224 |
225 |
▶ | if (eshu_xs_is_module_line(content, eol)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_xs_is_module_line(content, eol)) { |
96 |
129 |
225 |
|
| 225 |
96 |
| ctx->mode = ESHU_XS_XSUB_MODE; |
| 226 |
96 |
| ctx->section = ESHU_XS_NONE; |
| 227 |
96 |
| ctx->c_depth = 0; |
| 228 |
96 |
| ctx->c_ctx.state = ESHU_CODE; |
| 229 |
|
| /* MODULE line at column 0 */ |
| 230 |
96 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 231 |
96 |
| eshu_buf_putc(out, '\n'); |
| 232 |
96 |
| return; |
| 233 |
|
| } |
| 234 |
|
| /* Delegate to C scanner */ |
| 235 |
129 |
| eshu_c_process_line(&ctx->c_ctx, out, line_start, eol); |
| 236 |
129 |
| return; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
|
| /* ── XS mode ── */ |
| 240 |
|
| |
| 241 |
|
| /* Another MODULE line resets */ |
| 242 |
1085 |
▶ | if (eshu_xs_is_module_line(content, eol)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_xs_is_module_line(content, eol)) { |
1 |
1084 |
1085 |
|
| 243 |
1 |
| ctx->section = ESHU_XS_NONE; |
| 244 |
1 |
| ctx->c_depth = 0; |
| 245 |
1 |
| ctx->c_ctx.state = ESHU_CODE; |
| 246 |
1 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 247 |
1 |
| eshu_buf_putc(out, '\n'); |
| 248 |
1 |
| return; |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| /* Block comment continuation in XS body */ |
| 252 |
1084 |
▶ | if (ctx->c_ctx.state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->c_ctx.state == ESHU_COMMENT_BLOCK) { |
0 |
1084 |
1084 |
|
| 253 |
0 |
▶ | int depth = (ctx->section == ESHU_XS_BODY) ? 2 + ctx->c_depth : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| int depth = (ctx->section == ESHU_XS_BODY) ? 2 + ctx->c_depth : 1; |
0 |
0 |
0 |
|
| 254 |
0 |
| eshu_emit_indent(out, depth, &ctx->cfg); |
| 255 |
0 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 256 |
0 |
| eshu_buf_putc(out, '\n'); |
| 257 |
0 |
| eshu_xs_scan_c_nesting(ctx, content, eol); |
| 258 |
0 |
| return; |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* Check for module-level directives that must stay at column 0: |
| 262 |
|
| * PROTOTYPES: ENABLE/DISABLE and VERSIONCHECK: ENABLE/DISABLE */ |
| 263 |
|
| { |
| 264 |
1084 |
| int mlen = (int)(eol - content); |
| 265 |
1084 |
▶ | if ((mlen >= 11 && memcmp(content, "PROTOTYPES:", 11) == 0) || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((mlen >= 11 && memcmp(content, "PROTOTYPES:", 11) == 0) || |
458 |
626 |
1084 |
| if ((mlen >= 11 && memcmp(content, "PROTOTYPES:", 11) == 0) || |
454 |
4 |
458 |
| if ((mlen >= 11 && memcmp(content, "PROTOTYPES:", 11) == 0) || |
386 |
694 |
1080 |
|
| 266 |
386 |
▶ | (mlen >= 13 && memcmp(content, "VERSIONCHECK:", 13) == 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| (mlen >= 13 && memcmp(content, "VERSIONCHECK:", 13) == 0)) { |
1 |
385 |
386 |
|
| 267 |
5 |
| ctx->section = ESHU_XS_NONE; |
| 268 |
5 |
| ctx->c_depth = 0; |
| 269 |
5 |
| ctx->c_ctx.state = ESHU_CODE; |
| 270 |
5 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 271 |
5 |
| eshu_buf_putc(out, '\n'); |
| 272 |
5 |
| return; |
| 273 |
|
| } |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
|
| /* Check for BOOT: label (special: depth 0) */ |
| 277 |
1079 |
▶ | if (eshu_xs_is_boot_label(content, eol)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_xs_is_boot_label(content, eol)) { |
12 |
1067 |
1079 |
|
| 278 |
12 |
| ctx->section = ESHU_XS_LABEL; |
| 279 |
12 |
| ctx->c_depth = 0; |
| 280 |
12 |
| ctx->is_boot = 1; |
| 281 |
12 |
| ctx->c_ctx.state = ESHU_CODE; |
| 282 |
|
| /* BOOT: at depth 0 */ |
| 283 |
12 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 284 |
12 |
| eshu_buf_putc(out, '\n'); |
| 285 |
12 |
| ctx->section = ESHU_XS_BODY; |
| 286 |
12 |
| return; |
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
|
| /* Check for XS labels (CODE:, OUTPUT:, etc.) */ |
| 290 |
|
| { |
| 291 |
1067 |
| int is_boot = 0; |
| 292 |
1067 |
▶ | if (eshu_xs_is_label(content, eol, &is_boot)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_xs_is_label(content, eol, &is_boot)) { |
207 |
860 |
1067 |
|
| 293 |
207 |
| ctx->section = ESHU_XS_LABEL; |
| 294 |
207 |
| ctx->c_depth = 0; |
| 295 |
207 |
| ctx->is_boot = 0; |
| 296 |
207 |
| ctx->c_ctx.state = ESHU_CODE; |
| 297 |
|
| /* Labels at depth 1 */ |
| 298 |
207 |
| eshu_emit_indent(out, 1, &ctx->cfg); |
| 299 |
207 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 300 |
207 |
| eshu_buf_putc(out, '\n'); |
| 301 |
207 |
| ctx->section = ESHU_XS_BODY; |
| 302 |
207 |
| return; |
| 303 |
|
| } |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
|
| /* Check for preprocessor in XS mode */ |
| 307 |
860 |
▶ | if (*content == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*content == '#') { |
6 |
854 |
860 |
|
| 308 |
|
| /* Preprocessor stays at column 0 in XS too */ |
| 309 |
6 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 310 |
6 |
| eshu_buf_putc(out, '\n'); |
| 311 |
6 |
| return; |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
|
| /* Determine indent based on current section */ |
| 315 |
854 |
| switch (ctx->section) { |
| 316 |
210 |
| case ESHU_XS_NONE: { |
| 317 |
|
| /* XSUB return type / function signature — depth 0 */ |
| 318 |
210 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 319 |
210 |
| eshu_buf_putc(out, '\n'); |
| 320 |
|
| |
| 321 |
|
| /* If line has '(' it might be the function name line; |
| 322 |
|
| * the next lines will be params until we hit a label */ |
| 323 |
|
| { |
| 324 |
210 |
| const char *pp = content; |
| 325 |
1306 |
▶ | while (pp < eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pp < eol) { |
1201 |
105 |
1306 |
|
| 326 |
1201 |
▶ | if (*pp == '(') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pp == '(') { |
105 |
1096 |
1201 |
|
| 327 |
105 |
| ctx->section = ESHU_XS_PARAMS; |
| 328 |
105 |
| break; |
| 329 |
|
| } |
| 330 |
1096 |
| pp++; |
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
210 |
| break; |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
134 |
| case ESHU_XS_PARAMS: |
| 337 |
|
| /* Parameter declarations at depth 1 */ |
| 338 |
134 |
| eshu_emit_indent(out, 1, &ctx->cfg); |
| 339 |
134 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 340 |
134 |
| eshu_buf_putc(out, '\n'); |
| 341 |
134 |
| break; |
| 342 |
|
| |
| 343 |
0 |
| case ESHU_XS_LABEL: |
| 344 |
|
| /* Shouldn't get here — labels transition to BODY immediately */ |
| 345 |
0 |
| eshu_emit_indent(out, 2, &ctx->cfg); |
| 346 |
0 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 347 |
0 |
| eshu_buf_putc(out, '\n'); |
| 348 |
0 |
| break; |
| 349 |
|
| |
| 350 |
510 |
| case ESHU_XS_BODY: { |
| 351 |
|
| /* Code inside a label body — base depth 2, or 1 for BOOT */ |
| 352 |
510 |
▶ | int base = ctx->is_boot ? 1 : 2; |
Branch: 1 branch
| Branch | True | False | Total |
| int base = ctx->is_boot ? 1 : 2; |
29 |
481 |
510 |
|
| 353 |
510 |
| int depth = base + ctx->c_depth; |
| 354 |
|
| |
| 355 |
|
| /* If line starts with '}', dedent first */ |
| 356 |
510 |
▶ | if (*content == '}') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*content == '}') { |
28 |
482 |
510 |
|
| 357 |
28 |
| depth--; |
| 358 |
28 |
▶ | if (depth < base) depth = base; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth < base) depth = base; |
0 |
28 |
28 |
|
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
510 |
| eshu_emit_indent(out, depth, &ctx->cfg); |
| 362 |
510 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 363 |
510 |
| eshu_buf_putc(out, '\n'); |
| 364 |
|
| |
| 365 |
|
| /* Scan for C nesting changes */ |
| 366 |
510 |
| eshu_xs_scan_c_nesting(ctx, content, eol); |
| 367 |
510 |
| break; |
| 368 |
|
| } |
| 369 |
|
| } |
| 370 |
|
| } |
| 371 |
|
| |
| 372 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 373 |
|
| * Detect new XSUB boundary — reset section when we see a blank |
| 374 |
|
| * line or a new return-type line at depth 0 after a completed XSUB |
| 375 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 376 |
|
| |
| 377 |
1451 |
| static void eshu_xs_check_xsub_boundary(eshu_xs_ctx_t *ctx, |
| 378 |
|
| const char *content, const char *eol) { |
| 379 |
|
| /* If we're in XS mode and see a blank line, reset section */ |
| 380 |
1451 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
141 |
1310 |
1451 |
|
| 381 |
141 |
▶ | if (ctx->mode == ESHU_XS_XSUB_MODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->mode == ESHU_XS_XSUB_MODE) { |
116 |
25 |
141 |
|
| 382 |
116 |
| ctx->section = ESHU_XS_NONE; |
| 383 |
116 |
| ctx->c_depth = 0; |
| 384 |
116 |
| ctx->is_boot = 0; |
| 385 |
116 |
| ctx->c_ctx.state = ESHU_CODE; |
| 386 |
|
| } |
| 387 |
141 |
| return; |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
|
| /* If we're in params/body and hit a line at column 0 that looks |
| 391 |
|
| * like a new return type, reset */ |
| 392 |
1310 |
▶ | if (ctx->mode == ESHU_XS_XSUB_MODE && |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->mode == ESHU_XS_XSUB_MODE && |
1085 |
225 |
1310 |
|
| 393 |
1085 |
▶ | ctx->c_depth == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->c_depth == 0 && |
985 |
100 |
1085 |
|
| 394 |
985 |
▶ | (ctx->section == ESHU_XS_PARAMS || ctx->section == ESHU_XS_BODY)) { |
Branch: 1 branch
| Branch | True | False | Total |
| (ctx->section == ESHU_XS_PARAMS || ctx->section == ESHU_XS_BODY)) { |
746 |
239 |
985 |
|
| 395 |
|
| /* A non-indented line that doesn't look like it belongs to |
| 396 |
|
| * current XSUB — might be new return type */ |
| 397 |
|
| /* We handle this via blank-line separation instead */ |
| 398 |
|
| } |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 402 |
|
| * Public API — indent an XS source string |
| 403 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 404 |
|
| |
| 405 |
96 |
| static char * eshu_indent_xs(const char *src, size_t src_len, |
| 406 |
|
| const eshu_config_t *cfg, size_t *out_len) { |
| 407 |
|
| eshu_xs_ctx_t ctx; |
| 408 |
|
| eshu_buf_t out; |
| 409 |
96 |
| const char *p = src; |
| 410 |
96 |
| const char *end = src + src_len; |
| 411 |
|
| char *result; |
| 412 |
|
| |
| 413 |
96 |
| eshu_xs_ctx_init(&ctx, cfg); |
| 414 |
96 |
| eshu_buf_init(&out, src_len + 256); |
| 415 |
|
| |
| 416 |
|
| { |
| 417 |
96 |
| int line_num = 1; |
| 418 |
1547 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
1451 |
96 |
1547 |
|
| 419 |
1451 |
| const char *eol = eshu_find_eol(p); |
| 420 |
1451 |
| const char *content = eshu_skip_leading_ws(p); |
| 421 |
|
| |
| 422 |
|
| /* Check for XSUB boundary (blank lines reset section) */ |
| 423 |
1451 |
| eshu_xs_check_xsub_boundary(&ctx, content, eol); |
| 424 |
|
| |
| 425 |
1451 |
▶ | if (eshu_in_range(cfg, line_num)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(cfg, line_num)) { |
1451 |
0 |
1451 |
|
| 426 |
1451 |
| eshu_xs_process_line(&ctx, &out, p, eol); |
| 427 |
|
| } else { |
| 428 |
0 |
| size_t saved = out.len; |
| 429 |
0 |
| eshu_xs_process_line(&ctx, &out, p, eol); |
| 430 |
0 |
| out.len = saved; |
| 431 |
0 |
| eshu_buf_write_trimmed(&out, p, (int)(eol - p)); |
| 432 |
0 |
| eshu_buf_putc(&out, '\n'); |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
1451 |
| p = eol; |
| 436 |
1451 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
1451 |
0 |
1451 |
|
| 437 |
1451 |
| line_num++; |
| 438 |
|
| } |
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
|
| /* NUL-terminate */ |
| 442 |
96 |
| eshu_buf_putc(&out, '\0'); |
| 443 |
96 |
| out.len--; |
| 444 |
|
| |
| 445 |
96 |
| *out_len = out.len; |
| 446 |
96 |
| result = out.data; |
| 447 |
96 |
| return result; |
| 448 |
|
| } |
| 449 |
|
| |
| 450 |
|
| #endif /* ESHU_XS_H */ |