| 1 |
|
| /* |
| 2 |
|
| * eshu_rust.h — Rust language indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Rust is brace-based like Go. Additional constructs handled: |
| 5 |
|
| * - nested block comments (comment_depth counter) |
| 6 |
|
| * - raw strings r"...", r#"..."#, r##"..."## (raw_hash_count) |
| 7 |
|
| * - byte strings b"...", byte chars b'.', raw byte strings br#"..."# |
| 8 |
|
| * - char literals '.' vs lifetime annotations 'a |
| 9 |
|
| * - attributes #[...] and #![...] (treated as plain lines) |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef ESHU_RUST_H |
| 13 |
|
| #define ESHU_RUST_H |
| 14 |
|
| |
| 15 |
|
| #include "eshu.h" |
| 16 |
|
| |
| 17 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 18 |
|
| * Scanner context — persists across lines |
| 19 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 20 |
|
| |
| 21 |
|
| typedef struct { |
| 22 |
|
| int depth; |
| 23 |
|
| int comment_depth; /* nesting level for block comments */ |
| 24 |
|
| int raw_hash_count; /* # count in r#"..."# literals */ |
| 25 |
|
| enum eshu_state state; |
| 26 |
|
| eshu_config_t cfg; |
| 27 |
|
| } eshu_rust_ctx_t; |
| 28 |
|
| |
| 29 |
79 |
| static void eshu_rust_ctx_init(eshu_rust_ctx_t *ctx, const eshu_config_t *cfg) { |
| 30 |
79 |
| ctx->depth = 0; |
| 31 |
79 |
| ctx->comment_depth = 0; |
| 32 |
79 |
| ctx->raw_hash_count = 0; |
| 33 |
79 |
| ctx->state = ESHU_CODE; |
| 34 |
79 |
| ctx->cfg = *cfg; |
| 35 |
79 |
| } |
| 36 |
|
| |
| 37 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 38 |
|
| * Helpers |
| 39 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 40 |
|
| |
| 41 |
|
| /* Returns 1 if the ' at p is a lifetime annotation rather than a char literal. |
| 42 |
|
| * Lifetime: 'ident not followed by ' (e.g., 'a, 'static, 'lifetime) |
| 43 |
|
| * Char lit: '.' or 'a' or '\n' etc. */ |
| 44 |
24 |
| static int eshu_rust_is_lifetime(const char *p, const char *end) { |
| 45 |
24 |
| const char *q = p + 1; |
| 46 |
24 |
▶ | if (q >= end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q >= end) return 0; |
0 |
24 |
24 |
|
| 47 |
|
| /* escape sequences are always char literals */ |
| 48 |
24 |
▶ | if (*q == '\\') return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*q == '\\') return 0; |
2 |
22 |
24 |
|
| 49 |
|
| /* non-alphabetic start → char literal */ |
| 50 |
22 |
▶ | if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
13 |
9 |
22 |
| if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
12 |
1 |
13 |
|
| 51 |
|
| /* scan the identifier body */ |
| 52 |
10 |
| q++; |
| 53 |
15 |
▶ | while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
15 |
0 |
15 |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
5 |
10 |
15 |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
0 |
10 |
10 |
|
| 54 |
|
| /* if the identifier is immediately followed by ' → char literal, else lifetime */ |
| 55 |
10 |
▶ | return (q >= end || *q != '\'') ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (q >= end || *q != '\'') ? 1 : 0; |
10 |
0 |
10 |
| return (q >= end || *q != '\'') ? 1 : 0; |
10 |
0 |
10 |
|
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| /* Try to match a raw string opener at *pp (which may point to 'r' or 'b'). |
| 59 |
|
| * Handles r"", r#""#, r##""##, br"", br#""#. |
| 60 |
|
| * Returns number of # signs (>= 0) on success, -1 on failure. |
| 61 |
|
| * Advances *pp past the opening quote on success. */ |
| 62 |
475 |
| static int eshu_rust_raw_open(const char **pp, const char *end) { |
| 63 |
475 |
| const char *p = *pp; |
| 64 |
|
| /* skip optional 'b' prefix: br"" */ |
| 65 |
475 |
▶ | if (*p == 'b' && p + 1 < end && *(p + 1) == 'r') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == 'b' && p + 1 < end && *(p + 1) == 'r') p++; |
2 |
473 |
475 |
| if (*p == 'b' && p + 1 < end && *(p + 1) == 'r') p++; |
2 |
0 |
2 |
| if (*p == 'b' && p + 1 < end && *(p + 1) == 'r') p++; |
2 |
0 |
2 |
|
| 66 |
475 |
▶ | if (p >= end || *p != 'r') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= end || *p != 'r') return -1; |
475 |
0 |
475 |
| if (p >= end || *p != 'r') return -1; |
0 |
475 |
475 |
|
| 67 |
475 |
| p++; |
| 68 |
475 |
| int hashes = 0; |
| 69 |
476 |
▶ | while (p < end && *p == '#') { hashes++; p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p == '#') { hashes++; p++; } |
474 |
2 |
476 |
| while (p < end && *p == '#') { hashes++; p++; } |
1 |
473 |
474 |
|
| 70 |
475 |
▶ | if (p >= end || *p != '"') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= end || *p != '"') return -1; |
473 |
2 |
475 |
| if (p >= end || *p != '"') return -1; |
471 |
2 |
473 |
|
| 71 |
2 |
| p++; /* skip opening " */ |
| 72 |
2 |
| *pp = p; |
| 73 |
2 |
| return hashes; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| /* Try to match a raw string closer at *pp: " followed by raw_hash_count '#' chars. |
| 77 |
|
| * Advances *pp past the closer on success. */ |
| 78 |
40 |
| static int eshu_rust_raw_close(const char **pp, const char *end, int hashes) { |
| 79 |
40 |
| const char *p = *pp; |
| 80 |
40 |
▶ | if (p >= end || *p != '"') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= end || *p != '"') return 0; |
40 |
0 |
40 |
| if (p >= end || *p != '"') return 0; |
36 |
4 |
40 |
|
| 81 |
4 |
| p++; |
| 82 |
4 |
| int h = 0; |
| 83 |
5 |
▶ | while (h < hashes && p < end && *p == '#') { h++; p++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (h < hashes && p < end && *p == '#') { h++; p++; } |
3 |
2 |
5 |
| while (h < hashes && p < end && *p == '#') { h++; p++; } |
3 |
0 |
3 |
| while (h < hashes && p < end && *p == '#') { h++; p++; } |
1 |
2 |
3 |
|
| 84 |
4 |
▶ | if (h < hashes) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h < hashes) return 0; |
2 |
2 |
4 |
|
| 85 |
2 |
| *pp = p; |
| 86 |
2 |
| return 1; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 90 |
|
| * Line scanner — updates ctx->depth in place |
| 91 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 92 |
|
| |
| 93 |
608 |
| static void eshu_rust_scan_line(eshu_rust_ctx_t *ctx, |
| 94 |
|
| const char *p, const char *end) |
| 95 |
|
| { |
| 96 |
12358 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
11752 |
606 |
12358 |
|
| 97 |
11752 |
| char c = *p; |
| 98 |
|
| |
| 99 |
|
| /* ── block comment (potentially nested) ── */ |
| 100 |
11752 |
▶ | if (ctx->state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_COMMENT_BLOCK) { |
80 |
11672 |
11752 |
|
| 101 |
80 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
79 |
80 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
|
| 102 |
1 |
| ctx->comment_depth++; |
| 103 |
1 |
| p += 2; continue; |
| 104 |
|
| } |
| 105 |
79 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
6 |
73 |
79 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
6 |
0 |
6 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
4 |
2 |
6 |
|
| 106 |
4 |
▶ | if (--ctx->comment_depth <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--ctx->comment_depth <= 0) { |
3 |
1 |
4 |
|
| 107 |
3 |
| ctx->comment_depth = 0; |
| 108 |
3 |
| ctx->state = ESHU_CODE; |
| 109 |
|
| } |
| 110 |
4 |
| p += 2; continue; |
| 111 |
|
| } |
| 112 |
75 |
| p++; continue; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
|
| /* ── raw string ── */ |
| 116 |
11672 |
▶ | if (ctx->state == ESHU_RUST_RAW_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_RUST_RAW_STR) { |
40 |
11632 |
11672 |
|
| 117 |
40 |
| const char *tp = p; |
| 118 |
40 |
▶ | if (eshu_rust_raw_close(&tp, end, ctx->raw_hash_count)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_rust_raw_close(&tp, end, ctx->raw_hash_count)) { |
2 |
38 |
40 |
|
| 119 |
2 |
| ctx->state = ESHU_CODE; |
| 120 |
2 |
| p = tp; |
| 121 |
|
| } else { |
| 122 |
38 |
| p++; |
| 123 |
|
| } |
| 124 |
40 |
| continue; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| /* ── regular DQ string / byte string ── */ |
| 128 |
11632 |
▶ | if (ctx->state == ESHU_STRING_DQ || ctx->state == ESHU_RUST_BYTE_STR) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->state == ESHU_STRING_DQ || ctx->state == ESHU_RUST_BYTE_STR) { |
11388 |
244 |
11632 |
| if (ctx->state == ESHU_STRING_DQ || ctx->state == ESHU_RUST_BYTE_STR) { |
16 |
11372 |
11388 |
|
| 129 |
260 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
2 |
258 |
260 |
| if (c == '\\' && p + 1 < end) { |
2 |
0 |
2 |
|
| 130 |
2 |
| p += 2; continue; |
| 131 |
|
| } |
| 132 |
258 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
25 |
233 |
258 |
|
| 133 |
25 |
| ctx->state = ESHU_CODE; |
| 134 |
|
| } |
| 135 |
258 |
| p++; continue; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* ── char literal ── */ |
| 139 |
11372 |
▶ | if (ctx->state == ESHU_RUST_CHAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_RUST_CHAR) { |
28 |
11344 |
11372 |
|
| 140 |
28 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
2 |
26 |
28 |
| if (c == '\\' && p + 1 < end) { |
2 |
0 |
2 |
|
| 141 |
2 |
| p += 2; continue; |
| 142 |
|
| } |
| 143 |
26 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
14 |
12 |
26 |
|
| 144 |
14 |
| ctx->state = ESHU_CODE; |
| 145 |
|
| } |
| 146 |
26 |
| p++; continue; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| /* ══ ESHU_CODE ══ */ |
| 150 |
|
| |
| 151 |
|
| /* line comment: // */ |
| 152 |
11344 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
11 |
11333 |
11344 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
11 |
0 |
11 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
9 |
11 |
|
| 153 |
2 |
| return; /* rest of line is comment */ |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| /* block comment open: / * */ |
| 157 |
11342 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
9 |
11333 |
11342 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
9 |
0 |
9 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
3 |
6 |
9 |
|
| 158 |
3 |
| ctx->state = ESHU_COMMENT_BLOCK; |
| 159 |
3 |
| ctx->comment_depth = 1; |
| 160 |
3 |
| p += 2; continue; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
|
| /* attribute: #[ or #![ — treat as plain, no depth change */ |
| 164 |
11339 |
▶ | if (c == '#' && p + 1 < end && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '#' && p + 1 < end && |
4 |
11335 |
11339 |
| if (c == '#' && p + 1 < end && |
4 |
0 |
4 |
|
| 165 |
4 |
▶ | (*(p + 1) == '[' || (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
Branch: 4 branches
| Branch | True | False | Total |
| (*(p + 1) == '[' || (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
4 |
4 |
| (*(p + 1) == '[' || (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
| (*(p + 1) == '[' || (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
| (*(p + 1) == '[' || (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
|
| 166 |
|
| /* skip to matching ] */ |
| 167 |
4 |
| int attr_depth = 0; |
| 168 |
4 |
| p++; |
| 169 |
78 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
78 |
0 |
78 |
|
| 170 |
78 |
▶ | if (*p == '[') { attr_depth++; p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '[') { attr_depth++; p++; } |
4 |
74 |
78 |
|
| 171 |
74 |
▶ | else if (*p == ']') { attr_depth--; p++; if (attr_depth <= 0) break; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (*p == ']') { attr_depth--; p++; if (attr_depth <= 0) break; } |
4 |
70 |
74 |
| else if (*p == ']') { attr_depth--; p++; if (attr_depth <= 0) break; } |
4 |
0 |
4 |
|
| 172 |
70 |
| else p++; |
| 173 |
|
| } |
| 174 |
4 |
| continue; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| /* raw string: r"", r#""#, br"" */ |
| 178 |
11335 |
▶ | if ((c == 'r' || (c == 'b' && p + 1 < end && *(p + 1) == 'r')) && |
Branch: 4 branches
| Branch | True | False | Total |
| if ((c == 'r' || (c == 'b' && p + 1 < end && *(p + 1) == 'r')) && |
10862 |
473 |
11335 |
| if ((c == 'r' || (c == 'b' && p + 1 < end && *(p + 1) == 'r')) && |
93 |
10769 |
10862 |
| if ((c == 'r' || (c == 'b' && p + 1 < end && *(p + 1) == 'r')) && |
90 |
3 |
93 |
| if ((c == 'r' || (c == 'b' && p + 1 < end && *(p + 1) == 'r')) && |
2 |
88 |
90 |
|
| 179 |
475 |
▶ | ctx->state == ESHU_CODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state == ESHU_CODE) { |
475 |
0 |
475 |
|
| 180 |
475 |
| const char *tp = p; |
| 181 |
475 |
| int hashes = eshu_rust_raw_open(&tp, end); |
| 182 |
475 |
▶ | if (hashes >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hashes >= 0) { |
2 |
473 |
475 |
|
| 183 |
2 |
| ctx->raw_hash_count = hashes; |
| 184 |
2 |
| ctx->state = ESHU_RUST_RAW_STR; |
| 185 |
2 |
| p = tp; |
| 186 |
2 |
| continue; |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* byte string: b"..." or byte char: b'.' */ |
| 191 |
11333 |
▶ | if (c == 'b' && p + 1 < end && ctx->state == ESHU_CODE) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'b' && p + 1 < end && ctx->state == ESHU_CODE) { |
93 |
11240 |
11333 |
| if (c == 'b' && p + 1 < end && ctx->state == ESHU_CODE) { |
90 |
3 |
93 |
| if (c == 'b' && p + 1 < end && ctx->state == ESHU_CODE) { |
90 |
0 |
90 |
|
| 192 |
90 |
▶ | if (*(p + 1) == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*(p + 1) == '"') { |
1 |
89 |
90 |
|
| 193 |
1 |
| ctx->state = ESHU_RUST_BYTE_STR; |
| 194 |
1 |
| p += 2; continue; |
| 195 |
|
| } |
| 196 |
89 |
▶ | if (*(p + 1) == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*(p + 1) == '\'') { |
0 |
89 |
89 |
|
| 197 |
0 |
| ctx->state = ESHU_RUST_CHAR; |
| 198 |
0 |
| p += 2; continue; |
| 199 |
|
| } |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| /* regular DQ string */ |
| 203 |
11332 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
24 |
11308 |
11332 |
|
| 204 |
24 |
| ctx->state = ESHU_STRING_DQ; |
| 205 |
24 |
| p++; continue; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
|
| /* char literal or lifetime */ |
| 209 |
11308 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
24 |
11284 |
11308 |
|
| 210 |
24 |
▶ | if (eshu_rust_is_lifetime(p, end)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_rust_is_lifetime(p, end)) { |
10 |
14 |
24 |
|
| 211 |
|
| /* lifetime 'a — skip the identifier */ |
| 212 |
10 |
| p++; |
| 213 |
25 |
▶ | 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++; |
25 |
0 |
25 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
14 |
11 |
25 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
1 |
10 |
11 |
|
| 214 |
10 |
| continue; |
| 215 |
|
| } |
| 216 |
14 |
| ctx->state = ESHU_RUST_CHAR; |
| 217 |
14 |
| p++; continue; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| /* braces / parens / brackets */ |
| 221 |
11284 |
▶ | if (c == '{' || c == '(' || c == '[') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '{' || c == '(' || c == '[') { |
11071 |
213 |
11284 |
| if (c == '{' || c == '(' || c == '[') { |
10737 |
334 |
11071 |
| if (c == '{' || c == '(' || c == '[') { |
119 |
10618 |
10737 |
|
| 222 |
666 |
| ctx->depth++; |
| 223 |
10618 |
▶ | } else if (c == '}' || c == ')' || c == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '}' || c == ')' || c == ']') { |
10405 |
213 |
10618 |
| } else if (c == '}' || c == ')' || c == ']') { |
10071 |
334 |
10405 |
| } else if (c == '}' || c == ')' || c == ']') { |
119 |
9952 |
10071 |
|
| 224 |
666 |
| ctx->depth--; |
| 225 |
666 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
666 |
666 |
|
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
11284 |
| p++; |
| 229 |
|
| } |
| 230 |
|
| } |
| 231 |
|
| |
| 232 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 233 |
|
| * Process a single line — decide indent, emit, scan |
| 234 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 235 |
|
| |
| 236 |
618 |
| static void eshu_rust_process_line(eshu_rust_ctx_t *ctx, eshu_buf_t *out, |
| 237 |
|
| const char *line_start, const char *eol, |
| 238 |
|
| int lineno) |
| 239 |
|
| { |
| 240 |
618 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 241 |
|
| int line_len; |
| 242 |
|
| int indent_depth; |
| 243 |
|
| |
| 244 |
|
| /* empty line */ |
| 245 |
618 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
10 |
608 |
618 |
|
| 246 |
10 |
| eshu_buf_putc(out, '\n'); |
| 247 |
10 |
| return; |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
608 |
| line_len = (int)(eol - content); |
| 251 |
|
| |
| 252 |
|
| /* raw string continuation: emit verbatim (no reindent) */ |
| 253 |
608 |
▶ | if (ctx->state == ESHU_RUST_RAW_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_RUST_RAW_STR) { |
0 |
608 |
608 |
|
| 254 |
0 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
0 |
0 |
0 |
|
| 255 |
0 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 256 |
|
| } else { |
| 257 |
0 |
| eshu_buf_write_trimmed(out, line_start, (int)(eol - line_start)); |
| 258 |
|
| } |
| 259 |
0 |
| eshu_buf_putc(out, '\n'); |
| 260 |
0 |
| eshu_rust_scan_line(ctx, content, eol); |
| 261 |
0 |
| return; |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
|
| /* block comment continuation: emit with current indent + original content */ |
| 265 |
608 |
▶ | if (ctx->state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_COMMENT_BLOCK) { |
3 |
605 |
608 |
|
| 266 |
3 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 267 |
3 |
| eshu_buf_write_trimmed(out, line_start, (int)(eol - line_start)); |
| 268 |
3 |
| eshu_buf_putc(out, '\n'); |
| 269 |
3 |
| eshu_rust_scan_line(ctx, content, eol); |
| 270 |
3 |
| return; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
605 |
| indent_depth = ctx->depth; |
| 274 |
|
| |
| 275 |
|
| /* closing brace/bracket/paren at start of content → dedent this line */ |
| 276 |
605 |
▶ | if (*content == '}' || *content == ')' || *content == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*content == '}' || *content == ')' || *content == ']') { |
448 |
157 |
605 |
| if (*content == '}' || *content == ')' || *content == ']') { |
447 |
1 |
448 |
| if (*content == '}' || *content == ')' || *content == ']') { |
0 |
447 |
447 |
|
| 277 |
158 |
| indent_depth--; |
| 278 |
158 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
158 |
158 |
|
| 279 |
|
| } |
| 280 |
|
| |
| 281 |
605 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
605 |
0 |
605 |
|
| 282 |
605 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 283 |
|
| } else { |
| 284 |
0 |
| eshu_buf_write(out, line_start, (size_t)(content - line_start)); |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
605 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 288 |
605 |
| eshu_buf_putc(out, '\n'); |
| 289 |
|
| |
| 290 |
605 |
| eshu_rust_scan_line(ctx, content, eol); |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 294 |
|
| * Public API — indent a Rust source string |
| 295 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 296 |
|
| |
| 297 |
79 |
| static char *eshu_indent_rust(const char *src, size_t src_len, |
| 298 |
|
| const eshu_config_t *cfg, size_t *out_len) |
| 299 |
|
| { |
| 300 |
|
| eshu_rust_ctx_t ctx; |
| 301 |
|
| eshu_buf_t out; |
| 302 |
79 |
| const char *p = src; |
| 303 |
79 |
| const char *end = src + src_len; |
| 304 |
79 |
| int lineno = 1; |
| 305 |
|
| |
| 306 |
79 |
| eshu_rust_ctx_init(&ctx, cfg); |
| 307 |
79 |
| eshu_buf_init(&out, src_len + 256); |
| 308 |
|
| |
| 309 |
697 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
618 |
79 |
697 |
|
| 310 |
618 |
| const char *eol = eshu_find_eol(p); |
| 311 |
618 |
| eshu_rust_process_line(&ctx, &out, p, eol, lineno); |
| 312 |
618 |
| p = eol; |
| 313 |
618 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
618 |
0 |
618 |
|
| 314 |
618 |
| lineno++; |
| 315 |
|
| } |
| 316 |
|
| |
| 317 |
79 |
| eshu_buf_putc(&out, '\0'); |
| 318 |
79 |
| out.len--; |
| 319 |
79 |
| *out_len = out.len; |
| 320 |
79 |
| return out.data; |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
|
| |
| 324 |
|
| #include "eshu_hl_util.h" |
| 325 |
|
| |
| 326 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 327 |
|
| * Rust keyword / builtin lists |
| 328 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 329 |
|
| |
| 330 |
|
| static const char * const eshu_hl_rust_kw[] = { |
| 331 |
|
| "Self", "as", "async", "await", "break", "const", "continue", |
| 332 |
|
| "crate", "dyn", "else", "enum", "extern", "false", "fn", "for", |
| 333 |
|
| "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", |
| 334 |
|
| "pub", "ref", "return", "self", "static", "struct", "super", |
| 335 |
|
| "trait", "true", "type", "union", "unsafe", "use", "where", "while", |
| 336 |
|
| /* reserved / future keywords */ |
| 337 |
|
| "abstract", "become", "box", "do", "final", "macro", "override", |
| 338 |
|
| "priv", "try", "typeof", "unsized", "virtual", "yield", |
| 339 |
|
| NULL |
| 340 |
|
| }; |
| 341 |
|
| |
| 342 |
|
| static const char * const eshu_hl_rust_bi[] = { |
| 343 |
|
| /* primitive types */ |
| 344 |
|
| "bool", "char", "f32", "f64", |
| 345 |
|
| "i8", "i16", "i32", "i64", "i128", "isize", |
| 346 |
|
| "str", "u8", "u16", "u32", "u64", "u128", "usize", |
| 347 |
|
| /* standard library types / traits */ |
| 348 |
|
| "Box", "Clone", "Copy", "Debug", "Default", "Display", "Drop", |
| 349 |
|
| "Eq", "Error", "From", "Hash", "Into", "Iterator", |
| 350 |
|
| "None", "Option", "Ord", "PartialEq", "PartialOrd", |
| 351 |
|
| "Result", "Send", "Some", "String", "Sync", "ToString", "Vec", |
| 352 |
|
| /* common macros (bare name, '!' handled separately) */ |
| 353 |
|
| "assert", "assert_eq", "assert_ne", |
| 354 |
|
| "dbg", "eprint", "eprintln", "format", |
| 355 |
|
| "include_bytes", "include_str", |
| 356 |
|
| "panic", "print", "println", |
| 357 |
|
| "todo", "unimplemented", "unreachable", "vec", "write", "writeln", |
| 358 |
|
| NULL |
| 359 |
|
| }; |
| 360 |
|
| |
| 361 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 362 |
|
| * Rust highlighter helpers |
| 363 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 364 |
|
| |
| 365 |
1 |
| static int eshu_hl_rust_is_lifetime(const char *p, const char *end) { |
| 366 |
1 |
| const char *q = p + 1; |
| 367 |
1 |
▶ | if (q >= end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q >= end) return 0; |
0 |
1 |
1 |
|
| 368 |
1 |
▶ | if (*q == '\\') return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*q == '\\') return 0; |
0 |
1 |
1 |
|
| 369 |
1 |
▶ | if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
0 |
1 |
1 |
| if (!isalpha((unsigned char)*q) && *q != '_') return 0; |
0 |
0 |
0 |
|
| 370 |
1 |
| q++; |
| 371 |
1 |
▶ | while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
1 |
0 |
1 |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
0 |
1 |
1 |
| while (q < end && (isalnum((unsigned char)*q) || *q == '_')) q++; |
0 |
1 |
1 |
|
| 372 |
1 |
▶ | return (q >= end || *q != '\'') ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (q >= end || *q != '\'') ? 1 : 0; |
1 |
0 |
1 |
| return (q >= end || *q != '\'') ? 1 : 0; |
0 |
1 |
1 |
|
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 376 |
|
| * Rust highlighter |
| 377 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 378 |
|
| |
| 379 |
53 |
| static char *eshu_highlight_rust(const char *src, size_t src_len, size_t *out_len) { |
| 380 |
|
| eshu_buf_t out; |
| 381 |
53 |
| const char *p = src; |
| 382 |
53 |
| const char *end = src + src_len; |
| 383 |
53 |
| const char *plain = p; |
| 384 |
|
| |
| 385 |
53 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 386 |
|
| |
| 387 |
|
| #define RUST_SPAN(cls, ts, te) do { \ |
| 388 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 389 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 390 |
|
| p = (te); plain = p; \ |
| 391 |
|
| } while(0) |
| 392 |
|
| |
| 393 |
592 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
539 |
53 |
592 |
|
| 394 |
539 |
| char c = *p; |
| 395 |
|
| |
| 396 |
|
| /* line comment: // */ |
| 397 |
539 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
3 |
536 |
539 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
3 |
0 |
3 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
1 |
3 |
|
| 398 |
2 |
| const char *ts = p; |
| 399 |
2 |
| p += 2; |
| 400 |
28 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
26 |
2 |
28 |
| while (p < end && *p != '\n') p++; |
26 |
0 |
26 |
|
| 401 |
2 |
| RUST_SPAN("esh-c", ts, p); |
| 402 |
2 |
| continue; |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| /* nested block comment */ |
| 406 |
537 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
536 |
537 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
|
| 407 |
1 |
| const char *ts = p; |
| 408 |
1 |
| int depth = 1; |
| 409 |
1 |
| p += 2; |
| 410 |
9 |
▶ | while (p < end && depth > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && depth > 0) { |
8 |
1 |
9 |
| while (p < end && depth > 0) { |
8 |
0 |
8 |
|
| 411 |
8 |
▶ | if (*p == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '/' && p + 1 < end && *(p + 1) == '*') { |
0 |
8 |
8 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '*') { |
0 |
0 |
0 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '*') { |
0 |
0 |
0 |
|
| 412 |
0 |
| depth++; p += 2; |
| 413 |
8 |
▶ | } else if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
7 |
8 |
| } else if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
0 |
1 |
| } else if (*p == '*' && p + 1 < end && *(p + 1) == '/') { |
1 |
0 |
1 |
|
| 414 |
1 |
| depth--; p += 2; |
| 415 |
|
| } else { |
| 416 |
7 |
| p++; |
| 417 |
|
| } |
| 418 |
|
| } |
| 419 |
1 |
| RUST_SPAN("esh-c", ts, p); |
| 420 |
1 |
| continue; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| /* attribute: #[...] or #![...] */ |
| 424 |
536 |
▶ | if (c == '#' && p + 1 < end && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '#' && p + 1 < end && |
2 |
534 |
536 |
| if (c == '#' && p + 1 < end && |
2 |
0 |
2 |
|
| 425 |
2 |
▶ | (*(p + 1) == '[' || |
Branch: 1 branch
| Branch | True | False | Total |
| (*(p + 1) == '[' || |
0 |
2 |
2 |
|
| 426 |
0 |
▶ | (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
Branch: 3 branches
| Branch | True | False | Total |
| (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
| (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
| (*(p + 1) == '!' && p + 2 < end && *(p + 2) == '['))) { |
0 |
0 |
0 |
|
| 427 |
2 |
| const char *ts = p; |
| 428 |
2 |
| int depth = 0; |
| 429 |
28 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
28 |
0 |
28 |
|
| 430 |
28 |
▶ | if (*p == '[') { depth++; p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '[') { depth++; p++; } |
2 |
26 |
28 |
|
| 431 |
26 |
▶ | 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; } |
2 |
24 |
26 |
| else if (*p == ']') { depth--; p++; if (depth <= 0) break; } |
2 |
0 |
2 |
|
| 432 |
24 |
▶ | else if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*p == '\n') break; |
0 |
24 |
24 |
|
| 433 |
24 |
| else p++; |
| 434 |
|
| } |
| 435 |
2 |
| RUST_SPAN("esh-p", ts, p); |
| 436 |
2 |
| continue; |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
|
| /* raw byte string: br"..." or br#"..."# */ |
| 440 |
534 |
▶ | if (c == 'b' && p + 1 < end && *(p + 1) == 'r') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'b' && p + 1 < end && *(p + 1) == 'r') { |
4 |
530 |
534 |
| if (c == 'b' && p + 1 < end && *(p + 1) == 'r') { |
4 |
0 |
4 |
| if (c == 'b' && p + 1 < end && *(p + 1) == 'r') { |
1 |
3 |
4 |
|
| 441 |
1 |
| const char *rp = p + 2; |
| 442 |
1 |
| int hashes = 0; |
| 443 |
1 |
▶ | while (rp < end && *rp == '#') { hashes++; rp++; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (rp < end && *rp == '#') { hashes++; rp++; } |
1 |
0 |
1 |
| while (rp < end && *rp == '#') { hashes++; rp++; } |
0 |
1 |
1 |
|
| 444 |
1 |
▶ | if (rp < end && *rp == '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rp < end && *rp == '"') { |
1 |
0 |
1 |
| if (rp < end && *rp == '"') { |
0 |
1 |
1 |
|
| 445 |
0 |
| const char *ts = p; |
| 446 |
0 |
| rp++; |
| 447 |
0 |
▶ | while (rp < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (rp < end) { |
0 |
0 |
0 |
|
| 448 |
0 |
▶ | if (*rp == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*rp == '"') { |
0 |
0 |
0 |
|
| 449 |
0 |
| const char *cp = rp + 1; |
| 450 |
0 |
| int h = 0; |
| 451 |
0 |
▶ | while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
0 |
0 |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
0 |
0 |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
0 |
0 |
|
| 452 |
0 |
▶ | if (h == hashes) { rp = cp; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h == hashes) { rp = cp; break; } |
0 |
0 |
0 |
|
| 453 |
|
| } |
| 454 |
0 |
| rp++; |
| 455 |
|
| } |
| 456 |
0 |
| RUST_SPAN("esh-s", ts, rp); |
| 457 |
0 |
| continue; |
| 458 |
|
| } |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| /* byte string: b"..." */ |
| 462 |
534 |
▶ | if (c == 'b' && p + 1 < end && *(p + 1) == '"') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'b' && p + 1 < end && *(p + 1) == '"') { |
4 |
530 |
534 |
| if (c == 'b' && p + 1 < end && *(p + 1) == '"') { |
4 |
0 |
4 |
| if (c == 'b' && p + 1 < end && *(p + 1) == '"') { |
0 |
4 |
4 |
|
| 463 |
0 |
| const char *ts = p; |
| 464 |
0 |
| p += 2; |
| 465 |
0 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
0 |
0 |
0 |
| while (p < end && *p != '"') { |
0 |
0 |
0 |
|
| 466 |
0 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 467 |
0 |
| p++; |
| 468 |
|
| } |
| 469 |
0 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
0 |
0 |
0 |
|
| 470 |
0 |
| RUST_SPAN("esh-s", ts, p); |
| 471 |
0 |
| continue; |
| 472 |
|
| } |
| 473 |
|
| |
| 474 |
|
| /* byte char: b'.' */ |
| 475 |
534 |
▶ | if (c == 'b' && p + 1 < end && *(p + 1) == '\'') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'b' && p + 1 < end && *(p + 1) == '\'') { |
4 |
530 |
534 |
| if (c == 'b' && p + 1 < end && *(p + 1) == '\'') { |
4 |
0 |
4 |
| if (c == 'b' && p + 1 < end && *(p + 1) == '\'') { |
0 |
4 |
4 |
|
| 476 |
0 |
| const char *ts = p; |
| 477 |
0 |
| p += 2; |
| 478 |
0 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
|
| 479 |
0 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 480 |
0 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
0 |
0 |
|
| 481 |
0 |
| p++; |
| 482 |
|
| } |
| 483 |
0 |
▶ | if (p < end && *p == '\'') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '\'') p++; |
0 |
0 |
0 |
| if (p < end && *p == '\'') p++; |
0 |
0 |
0 |
|
| 484 |
0 |
| RUST_SPAN("esh-s", ts, p); |
| 485 |
0 |
| continue; |
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
|
| /* raw string: r"..." or r#"..."# */ |
| 489 |
534 |
▶ | if (c == 'r') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 'r') { |
2 |
532 |
534 |
|
| 490 |
2 |
| const char *rp = p + 1; |
| 491 |
2 |
| int hashes = 0; |
| 492 |
2 |
▶ | while (rp < end && *rp == '#') { hashes++; rp++; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (rp < end && *rp == '#') { hashes++; rp++; } |
2 |
0 |
2 |
| while (rp < end && *rp == '#') { hashes++; rp++; } |
0 |
2 |
2 |
|
| 493 |
2 |
▶ | if (rp < end && *rp == '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rp < end && *rp == '"') { |
2 |
0 |
2 |
| if (rp < end && *rp == '"') { |
1 |
1 |
2 |
|
| 494 |
1 |
| const char *ts = p; |
| 495 |
1 |
| rp++; |
| 496 |
11 |
▶ | while (rp < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (rp < end) { |
11 |
0 |
11 |
|
| 497 |
11 |
▶ | if (*rp == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*rp == '"') { |
1 |
10 |
11 |
|
| 498 |
1 |
| const char *cp = rp + 1; |
| 499 |
1 |
| int h = 0; |
| 500 |
1 |
▶ | while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
Branch: 3 branches
| Branch | True | False | Total |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
1 |
1 |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
0 |
0 |
| while (h < hashes && cp < end && *cp == '#') { h++; cp++; } |
0 |
0 |
0 |
|
| 501 |
1 |
▶ | if (h == hashes) { rp = cp; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h == hashes) { rp = cp; break; } |
1 |
0 |
1 |
|
| 502 |
|
| } |
| 503 |
10 |
| rp++; |
| 504 |
|
| } |
| 505 |
1 |
| RUST_SPAN("esh-s", ts, rp); |
| 506 |
1 |
| continue; |
| 507 |
|
| } |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| /* regular DQ string "..." */ |
| 511 |
533 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
4 |
529 |
533 |
|
| 512 |
4 |
| const char *ts = p++; |
| 513 |
25 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
25 |
0 |
25 |
| while (p < end && *p != '"') { |
21 |
4 |
25 |
|
| 514 |
21 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
21 |
21 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 515 |
21 |
| p++; |
| 516 |
|
| } |
| 517 |
4 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
4 |
0 |
4 |
|
| 518 |
4 |
| RUST_SPAN("esh-s", ts, p); |
| 519 |
4 |
| continue; |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
|
| /* char literal '.' or lifetime 'a */ |
| 523 |
529 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
1 |
528 |
529 |
|
| 524 |
1 |
▶ | if (eshu_hl_rust_is_lifetime(p, end)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_rust_is_lifetime(p, end)) { |
0 |
1 |
1 |
|
| 525 |
|
| /* lifetime annotation: highlight as builtin */ |
| 526 |
0 |
| const char *ts = p++; |
| 527 |
0 |
▶ | 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++; |
0 |
0 |
0 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 528 |
0 |
| RUST_SPAN("esh-b", ts, p); |
| 529 |
0 |
| continue; |
| 530 |
|
| } |
| 531 |
1 |
| const char *ts = p++; |
| 532 |
2 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
2 |
0 |
2 |
| while (p < end && *p != '\'') { |
1 |
1 |
2 |
|
| 533 |
1 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
1 |
1 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 534 |
1 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
1 |
1 |
|
| 535 |
1 |
| p++; |
| 536 |
|
| } |
| 537 |
1 |
▶ | if (p < end && *p == '\'') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '\'') p++; |
1 |
0 |
1 |
| if (p < end && *p == '\'') p++; |
1 |
0 |
1 |
|
| 538 |
1 |
| RUST_SPAN("esh-s", ts, p); |
| 539 |
1 |
| continue; |
| 540 |
|
| } |
| 541 |
|
| |
| 542 |
|
| /* number: 0x, 0b, 0o, decimal, float */ |
| 543 |
528 |
▶ | if (isdigit((unsigned char)c) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (isdigit((unsigned char)c) || |
508 |
20 |
528 |
| if (isdigit((unsigned char)c) || |
4 |
504 |
508 |
|
| 544 |
4 |
▶ | (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
Branch: 2 branches
| Branch | True | False | Total |
| (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
4 |
0 |
4 |
| (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
1 |
3 |
4 |
|
| 545 |
21 |
| const char *ts = p; |
| 546 |
26 |
▶ | if (c == '0' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '0' && p + 1 < end) { |
5 |
16 |
21 |
| if (c == '0' && p + 1 < end) { |
5 |
0 |
5 |
|
| 547 |
5 |
| char nx = *(p + 1); |
| 548 |
5 |
▶ | if (nx == 'x' || nx == 'X') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nx == 'x' || nx == 'X') { |
4 |
1 |
5 |
| if (nx == 'x' || nx == 'X') { |
0 |
4 |
4 |
|
| 549 |
1 |
| p += 2; |
| 550 |
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 |
|
| 551 |
4 |
▶ | } else if (nx == 'b' || nx == 'B') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (nx == 'b' || nx == 'B') { |
3 |
1 |
4 |
| } else if (nx == 'b' || nx == 'B') { |
0 |
3 |
3 |
|
| 552 |
1 |
| p += 2; |
| 553 |
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 |
|
| 554 |
3 |
▶ | } else if (nx == 'o' || nx == 'O') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (nx == 'o' || nx == 'O') { |
3 |
0 |
3 |
| } else if (nx == 'o' || nx == 'O') { |
0 |
3 |
3 |
|
| 555 |
0 |
| p += 2; |
| 556 |
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 |
|
| 557 |
|
| } else { |
| 558 |
6 |
▶ | 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++; |
6 |
0 |
6 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
3 |
3 |
6 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
3 |
3 |
|
| 559 |
3 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
3 |
0 |
3 |
| if (p < end && *p == '.') { |
1 |
2 |
3 |
|
| 560 |
1 |
| p++; |
| 561 |
1 |
▶ | 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++; |
1 |
0 |
1 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
1 |
1 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
1 |
1 |
|
| 562 |
|
| } |
| 563 |
3 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
3 |
0 |
3 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
3 |
0 |
3 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
3 |
3 |
|
| 564 |
0 |
| p++; |
| 565 |
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 |
|
| 566 |
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 |
|
| 567 |
|
| } |
| 568 |
|
| } |
| 569 |
|
| } else { |
| 570 |
41 |
▶ | 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++; |
39 |
2 |
41 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
23 |
16 |
39 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
14 |
16 |
|
| 571 |
16 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
14 |
2 |
16 |
| if (p < end && *p == '.') { |
2 |
12 |
14 |
|
| 572 |
2 |
| p++; |
| 573 |
6 |
▶ | 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++; |
5 |
1 |
6 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
4 |
1 |
5 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
1 |
1 |
|
| 574 |
|
| } |
| 575 |
16 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
13 |
3 |
16 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
13 |
0 |
13 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
13 |
13 |
|
| 576 |
0 |
| p++; |
| 577 |
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 |
|
| 578 |
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 |
|
| 579 |
|
| } |
| 580 |
|
| } |
| 581 |
21 |
| RUST_SPAN("esh-n", ts, p); |
| 582 |
21 |
| continue; |
| 583 |
|
| } |
| 584 |
|
| |
| 585 |
|
| /* identifier / keyword / builtin / macro */ |
| 586 |
507 |
▶ | if (eshu_hl_isalpha_(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_isalpha_(c)) { |
145 |
362 |
507 |
|
| 587 |
145 |
| const char *ts = p; |
| 588 |
649 |
▶ | while (p < end && eshu_hl_isalnum_(*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
646 |
3 |
649 |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
504 |
142 |
646 |
|
| 589 |
|
| /* check for macro call: identifier followed by '!' */ |
| 590 |
150 |
▶ | int is_macro = (p < end && *p == '!' && |
Branch: 2 branches
| Branch | True | False | Total |
| int is_macro = (p < end && *p == '!' && |
142 |
3 |
145 |
| int is_macro = (p < end && *p == '!' && |
5 |
137 |
142 |
|
| 591 |
5 |
▶ | !(p + 1 < end && *(p + 1) == '=')); |
Branch: 2 branches
| Branch | True | False | Total |
| !(p + 1 < end && *(p + 1) == '=')); |
5 |
0 |
5 |
| !(p + 1 < end && *(p + 1) == '=')); |
5 |
0 |
5 |
|
| 592 |
145 |
| eshu_hl_flush(&out, plain, ts); |
| 593 |
145 |
| plain = p; |
| 594 |
145 |
▶ | if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_rust_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_rust_kw)) { |
55 |
90 |
145 |
|
| 595 |
55 |
| eshu_hl_span(&out, "esh-k", ts, p); |
| 596 |
90 |
▶ | } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_rust_bi) || is_macro) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_rust_bi) || is_macro) { |
58 |
32 |
90 |
| } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_rust_bi) || is_macro) { |
0 |
58 |
58 |
|
| 597 |
32 |
| eshu_hl_span(&out, "esh-b", ts, p); |
| 598 |
|
| } else { |
| 599 |
58 |
| eshu_hl_write_html(&out, ts, (size_t)(p - ts)); |
| 600 |
|
| } |
| 601 |
145 |
| continue; |
| 602 |
|
| } |
| 603 |
|
| |
| 604 |
362 |
| p++; |
| 605 |
|
| } |
| 606 |
|
| |
| 607 |
53 |
| eshu_hl_flush(&out, plain, end); |
| 608 |
53 |
| eshu_buf_putc(&out, '\0'); |
| 609 |
53 |
| *out_len = out.len - 1; |
| 610 |
53 |
| return out.data; |
| 611 |
|
| |
| 612 |
|
| #undef RUST_SPAN |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| #endif /* ESHU_RUST_H */ |