| 1 |
|
| /* |
| 2 |
|
| * eshu_py.h — Python indentation normaliser |
| 3 |
|
| * |
| 4 |
|
| * Python uses significant whitespace: the indentation level IS the block |
| 5 |
|
| * structure. This pass cannot recompute depth from tokens the way C does |
| 6 |
|
| * (no braces to count). Instead it: |
| 7 |
|
| * |
| 8 |
|
| * 1. Reads the logical depth from existing leading whitespace via a |
| 9 |
|
| * depth stack (one entry per indent level, recording the column count |
| 10 |
|
| * at that level). |
| 11 |
|
| * 2. Re-emits each line with the configured indent_char / indent_width, |
| 12 |
|
| * preserving the relative nesting the author wrote. |
| 13 |
|
| * |
| 14 |
|
| * String states (single/double/triple, f-strings) are tracked so that |
| 15 |
|
| * leading whitespace inside multi-line strings is left untouched. |
| 16 |
|
| * |
| 17 |
|
| * Continuation lines (backslash at EOL, or inside unclosed brackets) |
| 18 |
|
| * are emitted at their source depth — they are not re-indented. |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| #ifndef ESHU_PY_H |
| 22 |
|
| #define ESHU_PY_H |
| 23 |
|
| |
| 24 |
|
| #include "eshu.h" |
| 25 |
|
| |
| 26 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 27 |
|
| * Measure leading whitespace, normalising tabs to 8-space stops |
| 28 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 29 |
|
| |
| 30 |
751 |
| static int eshu_py_measure_indent(const char *p, const char *eol) { |
| 31 |
751 |
| int col = 0; |
| 32 |
3893 |
▶ | while (p < eol && (*p == ' ' || *p == '\t')) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < eol && (*p == ' ' || *p == '\t')) { |
3893 |
0 |
3893 |
| while (p < eol && (*p == ' ' || *p == '\t')) { |
3142 |
751 |
3893 |
| while (p < eol && (*p == ' ' || *p == '\t')) { |
0 |
751 |
751 |
|
| 33 |
3142 |
▶ | if (*p == '\t') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\t') |
0 |
3142 |
3142 |
|
| 34 |
0 |
| col = (col + 8) & ~7; /* next 8-column tab stop */ |
| 35 |
|
| else |
| 36 |
3142 |
| col++; |
| 37 |
3142 |
| p++; |
| 38 |
|
| } |
| 39 |
751 |
| return col; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 43 |
|
| * Context init |
| 44 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 45 |
|
| |
| 46 |
137 |
| static void eshu_py_ctx_init(eshu_py_ctx_t *ctx, const eshu_config_t *cfg) { |
| 47 |
137 |
| ctx->depth = 0; |
| 48 |
137 |
| ctx->depth_stack[0] = 0; |
| 49 |
137 |
| ctx->depth_top = 1; /* level 0 = 0 spaces */ |
| 50 |
137 |
| ctx->bracket_depth = 0; |
| 51 |
137 |
| ctx->in_continuation = 0; |
| 52 |
137 |
| ctx->state = ESHU_CODE; |
| 53 |
137 |
| ctx->cfg = *cfg; |
| 54 |
137 |
| } |
| 55 |
|
| |
| 56 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 57 |
|
| * Scan a line's content to update bracket depth, detect |
| 58 |
|
| * continuation, and track string states. |
| 59 |
|
| * Returns 1 if the line ends with a backslash continuation. |
| 60 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 61 |
|
| |
| 62 |
797 |
| static int eshu_py_scan_line(eshu_py_ctx_t *ctx, |
| 63 |
|
| const char *p, const char *eol) { |
| 64 |
797 |
| int backslash_cont = 0; |
| 65 |
|
| |
| 66 |
17099 |
▶ | while (p < eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < eol) { |
16311 |
788 |
17099 |
|
| 67 |
16311 |
| char c = *p; |
| 68 |
|
| |
| 69 |
|
| /* ── inside triple-double-quote ── */ |
| 70 |
16311 |
▶ | if (ctx->state == ESHU_PY_STRING_DQ3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_PY_STRING_DQ3) { |
79 |
16232 |
16311 |
|
| 71 |
79 |
▶ | if (c == '"' && (eol - p) >= 3 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '"' && (eol - p) >= 3 && |
4 |
75 |
79 |
| if (c == '"' && (eol - p) >= 3 && |
4 |
0 |
4 |
|
| 72 |
4 |
▶ | p[1] == '"' && p[2] == '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| p[1] == '"' && p[2] == '"') { |
4 |
0 |
4 |
| p[1] == '"' && p[2] == '"') { |
4 |
0 |
4 |
|
| 73 |
4 |
| ctx->state = ESHU_CODE; |
| 74 |
4 |
| p += 3; continue; |
| 75 |
|
| } |
| 76 |
75 |
▶ | if (c == '\\') { p++; } /* skip escaped char */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') { p++; } /* skip escaped char */ |
0 |
75 |
75 |
|
| 77 |
75 |
| p++; continue; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* ── inside triple-single-quote ── */ |
| 81 |
16232 |
▶ | if (ctx->state == ESHU_PY_STRING_SQ3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_PY_STRING_SQ3) { |
28 |
16204 |
16232 |
|
| 82 |
28 |
▶ | if (c == '\'' && (eol - p) >= 3 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\'' && (eol - p) >= 3 && |
2 |
26 |
28 |
| if (c == '\'' && (eol - p) >= 3 && |
2 |
0 |
2 |
|
| 83 |
2 |
▶ | p[1] == '\'' && p[2] == '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| p[1] == '\'' && p[2] == '\'') { |
2 |
0 |
2 |
| p[1] == '\'' && p[2] == '\'') { |
2 |
0 |
2 |
|
| 84 |
2 |
| ctx->state = ESHU_CODE; |
| 85 |
2 |
| p += 3; continue; |
| 86 |
|
| } |
| 87 |
26 |
▶ | if (c == '\\') { p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') { p++; } |
0 |
26 |
26 |
|
| 88 |
26 |
| p++; continue; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* ── inside single double-quote ── */ |
| 92 |
16204 |
▶ | if (ctx->state == ESHU_PY_STRING_DQ || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_PY_STRING_DQ || |
15811 |
393 |
16204 |
|
| 93 |
15811 |
▶ | ctx->state == ESHU_PY_FSTRING_DQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state == ESHU_PY_FSTRING_DQ) { |
393 |
15418 |
15811 |
|
| 94 |
786 |
▶ | if (c == '"') { ctx->state = ESHU_CODE; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { ctx->state = ESHU_CODE; p++; continue; } |
72 |
714 |
786 |
|
| 95 |
714 |
▶ | if (c == '\\') { p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') { p++; } |
2 |
712 |
714 |
|
| 96 |
714 |
| p++; continue; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
|
| /* ── inside single single-quote ── */ |
| 100 |
15418 |
▶ | if (ctx->state == ESHU_PY_STRING_SQ || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_PY_STRING_SQ || |
15301 |
117 |
15418 |
|
| 101 |
15301 |
▶ | ctx->state == ESHU_PY_FSTRING_SQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state == ESHU_PY_FSTRING_SQ) { |
0 |
15301 |
15301 |
|
| 102 |
117 |
▶ | if (c == '\'') { ctx->state = ESHU_CODE; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { ctx->state = ESHU_CODE; p++; continue; } |
18 |
99 |
117 |
|
| 103 |
99 |
▶ | if (c == '\\') { p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\\') { p++; } |
5 |
94 |
99 |
|
| 104 |
99 |
| p++; continue; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /* ── normal code ── */ |
| 108 |
|
| |
| 109 |
|
| /* comment: rest of line is irrelevant */ |
| 110 |
15301 |
▶ | if (c == '#') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#') break; |
9 |
15292 |
15301 |
|
| 111 |
|
| |
| 112 |
|
| /* f-string / b-string / r-string prefixes */ |
| 113 |
15292 |
▶ | if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
Branch: 5 branches
| Branch | True | False | Total |
| if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
14623 |
669 |
15292 |
| if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
14617 |
6 |
14623 |
| if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
14478 |
139 |
14617 |
| if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
14471 |
7 |
14478 |
| if ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
13579 |
892 |
14471 |
|
| 114 |
13579 |
▶ | c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
Branch: 3 branches
| Branch | True | False | Total |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
13573 |
6 |
13579 |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
13264 |
309 |
13573 |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
4 |
13260 |
13264 |
|
| 115 |
2032 |
▶ | (p[1] == '"' || p[1] == '\'')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (p[1] == '"' || p[1] == '\'')) { |
2016 |
16 |
2032 |
| (p[1] == '"' || p[1] == '\'')) { |
2 |
2014 |
2016 |
|
| 116 |
18 |
▶ | int is_f = (c == 'f' || c == 'F'); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_f = (c == 'f' || c == 'F'); |
2 |
16 |
18 |
| int is_f = (c == 'f' || c == 'F'); |
0 |
2 |
2 |
|
| 117 |
18 |
| p++; |
| 118 |
18 |
| c = *p; |
| 119 |
|
| /* check for triple */ |
| 120 |
18 |
▶ | if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
18 |
0 |
18 |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
0 |
18 |
18 |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
0 |
0 |
0 |
|
| 121 |
0 |
| ctx->state = (c == '"') ? ESHU_PY_STRING_DQ3 |
| 122 |
0 |
▶ | : ESHU_PY_STRING_SQ3; |
Branch: 1 branch
| Branch | True | False | Total |
| : ESHU_PY_STRING_SQ3; |
0 |
0 |
0 |
|
| 123 |
0 |
| p += 3; continue; |
| 124 |
|
| } |
| 125 |
18 |
| ctx->state = is_f |
| 126 |
|
| ? (c == '"' ? ESHU_PY_FSTRING_DQ : ESHU_PY_FSTRING_SQ) |
| 127 |
18 |
▶ | : (c == '"' ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ); |
Branch: 3 branches
| Branch | True | False | Total |
| : (c == '"' ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ); |
16 |
2 |
18 |
| : (c == '"' ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ); |
16 |
0 |
16 |
| : (c == '"' ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ); |
0 |
2 |
2 |
|
| 128 |
18 |
| p++; continue; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| /* two-char prefix rb / br */ |
| 132 |
15274 |
▶ | if ((c == 'r' || c == 'R') && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c == 'r' || c == 'R') && |
14383 |
891 |
15274 |
| if ((c == 'r' || c == 'R') && |
6 |
14377 |
14383 |
|
| 133 |
897 |
▶ | (p[1] == 'b' || p[1] == 'B') && |
Branch: 2 branches
| Branch | True | False | Total |
| (p[1] == 'b' || p[1] == 'B') && |
897 |
0 |
897 |
| (p[1] == 'b' || p[1] == 'B') && |
0 |
897 |
897 |
|
| 134 |
0 |
▶ | (p[2] == '"' || p[2] == '\'')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (p[2] == '"' || p[2] == '\'')) { |
0 |
0 |
0 |
| (p[2] == '"' || p[2] == '\'')) { |
0 |
0 |
0 |
|
| 135 |
0 |
| p += 2; |
| 136 |
0 |
| c = *p; |
| 137 |
0 |
▶ | if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
0 |
0 |
0 |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
0 |
0 |
0 |
| if ((eol - p) >= 3 && p[1] == c && p[2] == c) { |
0 |
0 |
0 |
|
| 138 |
0 |
| ctx->state = (c == '"') ? ESHU_PY_STRING_DQ3 |
| 139 |
0 |
▶ | : ESHU_PY_STRING_SQ3; |
Branch: 1 branch
| Branch | True | False | Total |
| : ESHU_PY_STRING_SQ3; |
0 |
0 |
0 |
|
| 140 |
0 |
| p += 3; continue; |
| 141 |
|
| } |
| 142 |
0 |
▶ | ctx->state = (c == '"') ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ; |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state = (c == '"') ? ESHU_PY_STRING_DQ : ESHU_PY_STRING_SQ; |
0 |
0 |
0 |
|
| 143 |
0 |
| p++; continue; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* triple-quote open */ |
| 147 |
15274 |
▶ | if ((c == '"' || c == '\'') && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c == '"' || c == '\'') && |
15214 |
60 |
15274 |
| if ((c == '"' || c == '\'') && |
18 |
15196 |
15214 |
|
| 148 |
78 |
▶ | (eol - p) >= 3 && p[1] == c && p[2] == c) { |
Branch: 3 branches
| Branch | True | False | Total |
| (eol - p) >= 3 && p[1] == c && p[2] == c) { |
77 |
1 |
78 |
| (eol - p) >= 3 && p[1] == c && p[2] == c) { |
7 |
70 |
77 |
| (eol - p) >= 3 && p[1] == c && p[2] == c) { |
6 |
1 |
7 |
|
| 149 |
6 |
| ctx->state = (c == '"') ? ESHU_PY_STRING_DQ3 |
| 150 |
6 |
▶ | : ESHU_PY_STRING_SQ3; |
Branch: 1 branch
| Branch | True | False | Total |
| : ESHU_PY_STRING_SQ3; |
4 |
2 |
6 |
|
| 151 |
6 |
| p += 3; continue; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
|
| /* single-quote open */ |
| 155 |
15268 |
▶ | if (c == '"') { ctx->state = ESHU_PY_STRING_DQ; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { ctx->state = ESHU_PY_STRING_DQ; p++; continue; } |
56 |
15212 |
15268 |
|
| 156 |
15212 |
▶ | if (c == '\'') { ctx->state = ESHU_PY_STRING_SQ; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { ctx->state = ESHU_PY_STRING_SQ; p++; continue; } |
16 |
15196 |
15212 |
|
| 157 |
|
| |
| 158 |
|
| /* brackets */ |
| 159 |
15196 |
▶ | if (c == '(' || c == '[' || c == '{') { ctx->bracket_depth++; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '(' || c == '[' || c == '{') { ctx->bracket_depth++; } |
14784 |
412 |
15196 |
| if (c == '(' || c == '[' || c == '{') { ctx->bracket_depth++; } |
14698 |
86 |
14784 |
| if (c == '(' || c == '[' || c == '{') { ctx->bracket_depth++; } |
15 |
14683 |
14698 |
|
| 160 |
15196 |
▶ | if (c == ')' || c == ']' || c == '}') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == ')' || c == ']' || c == '}') { |
14784 |
412 |
15196 |
| if (c == ')' || c == ']' || c == '}') { |
14698 |
86 |
14784 |
| if (c == ')' || c == ']' || c == '}') { |
15 |
14683 |
14698 |
|
| 161 |
513 |
▶ | if (ctx->bracket_depth > 0) ctx->bracket_depth--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->bracket_depth > 0) ctx->bracket_depth--; |
513 |
0 |
513 |
|
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
15196 |
| p++; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* eol points to '\n' or NUL; the char immediately before is the |
| 168 |
|
| * last content character on the line. A trailing '\' means the |
| 169 |
|
| * logical line continues on the next physical line. */ |
| 170 |
797 |
▶ | backslash_cont = (eol > p && *(eol - 1) == '\\') ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| backslash_cont = (eol > p && *(eol - 1) == '\\') ? 1 : 0; |
9 |
788 |
797 |
| backslash_cont = (eol > p && *(eol - 1) == '\\') ? 1 : 0; |
0 |
9 |
9 |
|
| 171 |
797 |
| return backslash_cont; |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 175 |
|
| * Resolve logical depth for a line given its leading-space count. |
| 176 |
|
| * Pushes/pops the depth stack as needed. |
| 177 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 178 |
|
| |
| 179 |
751 |
| static int eshu_py_resolve_depth(eshu_py_ctx_t *ctx, int col) { |
| 180 |
|
| int top; |
| 181 |
|
| |
| 182 |
|
| /* Increase: new indent level */ |
| 183 |
751 |
▶ | if (ctx->depth_top < 64 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth_top < 64 && |
751 |
0 |
751 |
|
| 184 |
751 |
▶ | col > ctx->depth_stack[ctx->depth_top - 1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| col > ctx->depth_stack[ctx->depth_top - 1]) { |
275 |
476 |
751 |
|
| 185 |
275 |
| ctx->depth_stack[ctx->depth_top++] = col; |
| 186 |
275 |
| ctx->depth = ctx->depth_top - 1; |
| 187 |
275 |
| return ctx->depth; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* Decrease or same: pop until we find a matching level */ |
| 191 |
476 |
| top = ctx->depth_top; |
| 192 |
611 |
▶ | while (top > 1 && col < ctx->depth_stack[top - 1]) |
Branch: 2 branches
| Branch | True | False | Total |
| while (top > 1 && col < ctx->depth_stack[top - 1]) |
368 |
243 |
611 |
| while (top > 1 && col < ctx->depth_stack[top - 1]) |
135 |
233 |
368 |
|
| 193 |
135 |
| top--; |
| 194 |
476 |
| ctx->depth_top = top; |
| 195 |
|
| |
| 196 |
|
| /* If col matches the current top exactly, depth = top-1. |
| 197 |
|
| * If it doesn't match any known level (dedent to unknown), |
| 198 |
|
| * treat as matching the closest lower level. */ |
| 199 |
476 |
| ctx->depth = top - 1; |
| 200 |
476 |
| return ctx->depth; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 204 |
|
| * Process one line |
| 205 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 206 |
|
| |
| 207 |
851 |
| static void eshu_py_process_line(eshu_py_ctx_t *ctx, eshu_buf_t *out, |
| 208 |
|
| const char *line_start, const char *eol) { |
| 209 |
851 |
| const char *p = line_start; |
| 210 |
851 |
| const char *end = eol; |
| 211 |
851 |
| int col = 0; |
| 212 |
|
| int depth; |
| 213 |
|
| int in_ml_string; |
| 214 |
|
| int backslash_cont; |
| 215 |
|
| |
| 216 |
1694 |
▶ | in_ml_string = (ctx->state == ESHU_PY_STRING_DQ3 || |
Branch: 1 branch
| Branch | True | False | Total |
| in_ml_string = (ctx->state == ESHU_PY_STRING_DQ3 || |
843 |
8 |
851 |
|
| 217 |
843 |
▶ | ctx->state == ESHU_PY_STRING_SQ3); |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state == ESHU_PY_STRING_SQ3); |
3 |
840 |
843 |
|
| 218 |
|
| |
| 219 |
|
| /* ── Multi-line string body: emit verbatim ── */ |
| 220 |
851 |
▶ | if (in_ml_string) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_ml_string) { |
11 |
840 |
851 |
|
| 221 |
|
| /* scan for closing triple quote; emit the raw line unchanged */ |
| 222 |
11 |
| eshu_py_scan_line(ctx, p, end); |
| 223 |
11 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 224 |
11 |
| eshu_buf_putc(out, '\n'); |
| 225 |
11 |
| return; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| /* ── Blank line: emit as-is (just a newline) ── */ |
| 229 |
|
| { |
| 230 |
840 |
| const char *nc = eshu_skip_leading_ws(p); |
| 231 |
840 |
▶ | if (nc >= end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc >= end) { |
54 |
786 |
840 |
|
| 232 |
54 |
| eshu_buf_putc(out, '\n'); |
| 233 |
54 |
| return; |
| 234 |
|
| } |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
|
| /* ── Continuation line: preserve source indentation ── */ |
| 238 |
786 |
▶ | if (ctx->in_continuation || ctx->bracket_depth > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->in_continuation || ctx->bracket_depth > 0) { |
786 |
0 |
786 |
| if (ctx->in_continuation || ctx->bracket_depth > 0) { |
35 |
751 |
786 |
|
| 239 |
35 |
| backslash_cont = eshu_py_scan_line(ctx, p, end); |
| 240 |
35 |
| ctx->in_continuation = backslash_cont; |
| 241 |
35 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 242 |
35 |
| eshu_buf_putc(out, '\n'); |
| 243 |
35 |
| return; |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /* ── Normal line: measure indent, resolve depth, re-emit ── */ |
| 247 |
751 |
| col = eshu_py_measure_indent(p, end); |
| 248 |
751 |
| depth = eshu_py_resolve_depth(ctx, col); |
| 249 |
|
| |
| 250 |
|
| /* Skip past leading whitespace to the content */ |
| 251 |
3893 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
3893 |
0 |
3893 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
3142 |
751 |
3893 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
751 |
751 |
|
| 252 |
|
| |
| 253 |
|
| /* Emit re-indented line */ |
| 254 |
751 |
| eshu_emit_indent(out, depth, &ctx->cfg); |
| 255 |
751 |
| eshu_buf_write_trimmed(out, p, (int)(end - p)); |
| 256 |
751 |
| eshu_buf_putc(out, '\n'); |
| 257 |
|
| |
| 258 |
|
| /* Scan content to update bracket_depth and string state */ |
| 259 |
751 |
| backslash_cont = eshu_py_scan_line(ctx, p, end); |
| 260 |
751 |
| ctx->in_continuation = backslash_cont; |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 264 |
|
| * Public API |
| 265 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 266 |
|
| |
| 267 |
137 |
| static char *eshu_indent_python(const char *src, size_t src_len, |
| 268 |
|
| const eshu_config_t *cfg, size_t *out_len) { |
| 269 |
|
| eshu_py_ctx_t ctx; |
| 270 |
|
| eshu_buf_t out; |
| 271 |
137 |
| const char *p = src; |
| 272 |
137 |
| const char *end = src + src_len; |
| 273 |
|
| char *result; |
| 274 |
|
| |
| 275 |
137 |
| eshu_py_ctx_init(&ctx, cfg); |
| 276 |
137 |
| eshu_buf_init(&out, src_len + 256); |
| 277 |
|
| |
| 278 |
988 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
851 |
137 |
988 |
|
| 279 |
851 |
| const char *eol = eshu_find_eol(p); |
| 280 |
851 |
| eshu_py_process_line(&ctx, &out, p, eol); |
| 281 |
851 |
▶ | p = (*eol == '\n') ? eol + 1 : eol; |
Branch: 1 branch
| Branch | True | False | Total |
| p = (*eol == '\n') ? eol + 1 : eol; |
851 |
0 |
851 |
|
| 282 |
851 |
▶ | if (p > end) p = end; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > end) p = end; |
0 |
851 |
851 |
|
| 283 |
|
| } |
| 284 |
|
| |
| 285 |
137 |
| eshu_buf_putc(&out, '\0'); |
| 286 |
137 |
| out.len--; |
| 287 |
|
| |
| 288 |
137 |
| *out_len = out.len; |
| 289 |
137 |
| result = out.data; |
| 290 |
137 |
| return result; |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| |
| 294 |
|
| #include "eshu_hl_util.h" |
| 295 |
|
| |
| 296 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 297 |
|
| * Python keyword and builtin lists |
| 298 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 299 |
|
| |
| 300 |
|
| static const char * const eshu_hl_py_kw[] = { |
| 301 |
|
| "False", "None", "True", |
| 302 |
|
| "and", "as", "assert", "async", "await", |
| 303 |
|
| "break", "class", "continue", "def", "del", |
| 304 |
|
| "elif", "else", "except", "finally", "for", |
| 305 |
|
| "from", "global", "if", "import", "in", |
| 306 |
|
| "is", "lambda", "match", "case", |
| 307 |
|
| "nonlocal", "not", "or", "pass", "raise", |
| 308 |
|
| "return", "try", "while", "with", "yield", |
| 309 |
|
| NULL |
| 310 |
|
| }; |
| 311 |
|
| |
| 312 |
|
| static const char * const eshu_hl_py_bi[] = { |
| 313 |
|
| "abs", "all", "any", "ascii", "bin", "bool", "breakpoint", |
| 314 |
|
| "bytearray", "bytes", "callable", "chr", "classmethod", |
| 315 |
|
| "compile", "complex", "delattr", "dict", "dir", "divmod", |
| 316 |
|
| "enumerate", "eval", "exec", "filter", "float", "format", |
| 317 |
|
| "frozenset", "getattr", "globals", "hasattr", "hash", |
| 318 |
|
| "help", "hex", "id", "input", "int", "isinstance", |
| 319 |
|
| "issubclass", "iter", "len", "list", "locals", "map", |
| 320 |
|
| "max", "memoryview", "min", "next", "object", "oct", |
| 321 |
|
| "open", "ord", "pow", "print", "property", "range", |
| 322 |
|
| "repr", "reversed", "round", "set", "setattr", "slice", |
| 323 |
|
| "sorted", "staticmethod", "str", "sum", "super", "tuple", |
| 324 |
|
| "type", "vars", "zip", |
| 325 |
|
| NULL |
| 326 |
|
| }; |
| 327 |
|
| |
| 328 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 329 |
|
| * Python highlighter |
| 330 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 331 |
|
| |
| 332 |
|
| /* Scan a Python string body starting at *pp, emitting into out. |
| 333 |
|
| * q1 = opening quote char, triple = 1 for triple-quoted. |
| 334 |
|
| * Advances *pp past the closing delimiter. */ |
| 335 |
15 |
| static void eshu_hl_py_string(eshu_buf_t *out, const char **pp, |
| 336 |
|
| const char *end, char q1, int triple) { |
| 337 |
15 |
| const char *p = *pp; |
| 338 |
15 |
▶ | const char *ts = p - (triple ? 3 : 1) - 1; /* include opening quote(s) and any prefix */ |
Branch: 1 branch
| Branch | True | False | Total |
| const char *ts = p - (triple ? 3 : 1) - 1; /* include opening quote(s) and any prefix */ |
2 |
13 |
15 |
|
| 339 |
|
| /* We were called after the opening quote(s) were consumed. |
| 340 |
|
| * Back up to find the true start (prefix + quotes already emitted as plain). |
| 341 |
|
| * Instead: span starts at ts passed by caller — here we just scan the body |
| 342 |
|
| * and emit the whole span from the already-remembered start. */ |
| 343 |
|
| /* Actually the caller handles span emission; we just need to find the end. */ |
| 344 |
|
| (void)ts; |
| 345 |
|
| |
| 346 |
127 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
127 |
0 |
127 |
|
| 347 |
127 |
▶ | if (*p == '\\') { p += 2; continue; } /* skip escaped char */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\\') { p += 2; continue; } /* skip escaped char */ |
2 |
125 |
127 |
|
| 348 |
125 |
▶ | if (triple) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (triple) { |
23 |
102 |
125 |
|
| 349 |
23 |
▶ | if (*p == q1 && (end - p) >= 3 && p[1] == q1 && p[2] == q1) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (*p == q1 && (end - p) >= 3 && p[1] == q1 && p[2] == q1) { |
2 |
21 |
23 |
| if (*p == q1 && (end - p) >= 3 && p[1] == q1 && p[2] == q1) { |
2 |
0 |
2 |
| if (*p == q1 && (end - p) >= 3 && p[1] == q1 && p[2] == q1) { |
2 |
0 |
2 |
| if (*p == q1 && (end - p) >= 3 && p[1] == q1 && p[2] == q1) { |
2 |
0 |
2 |
|
| 350 |
2 |
| p += 3; break; |
| 351 |
|
| } |
| 352 |
|
| } else { |
| 353 |
102 |
▶ | if (*p == q1) { p++; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == q1) { p++; break; } |
13 |
89 |
102 |
|
| 354 |
89 |
▶ | if (*p == '\n') break; /* unterminated single-line string */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; /* unterminated single-line string */ |
0 |
89 |
89 |
|
| 355 |
|
| } |
| 356 |
110 |
| p++; |
| 357 |
|
| } |
| 358 |
15 |
| *pp = p; |
| 359 |
15 |
| } |
| 360 |
|
| |
| 361 |
63 |
| static char *eshu_highlight_py(const char *src, size_t src_len, size_t *out_len) { |
| 362 |
|
| eshu_buf_t out; |
| 363 |
63 |
| const char *p = src; |
| 364 |
63 |
| const char *end = src + src_len; |
| 365 |
63 |
| const char *plain = p; |
| 366 |
|
| |
| 367 |
63 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 368 |
|
| |
| 369 |
|
| #define PY_FLUSH(upto) eshu_hl_flush(&out, plain, (upto)) |
| 370 |
|
| #define PY_SPAN(cls, s, e) do { PY_FLUSH(s); eshu_hl_span(&out, (cls), (s), (e)); plain = (e); p = (e); } while(0) |
| 371 |
|
| |
| 372 |
587 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
524 |
63 |
587 |
|
| 373 |
524 |
| char c = *p; |
| 374 |
|
| |
| 375 |
|
| /* ── comment ── */ |
| 376 |
524 |
▶ | if (c == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#') { |
2 |
522 |
524 |
|
| 377 |
2 |
| const char *ts = p; |
| 378 |
37 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
35 |
2 |
37 |
| while (p < end && *p != '\n') p++; |
35 |
0 |
35 |
|
| 379 |
2 |
| PY_SPAN("esh-c", ts, p); |
| 380 |
2 |
| continue; |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
|
| /* ── decorator ── */ |
| 384 |
522 |
▶ | if (c == '@' && (p == src || *(p-1) == '\n')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '@' && (p == src || *(p-1) == '\n')) { |
2 |
520 |
522 |
| if (c == '@' && (p == src || *(p-1) == '\n')) { |
0 |
2 |
2 |
| if (c == '@' && (p == src || *(p-1) == '\n')) { |
0 |
0 |
0 |
|
| 385 |
2 |
| const char *ts = p++; |
| 386 |
23 |
▶ | while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++; |
23 |
0 |
23 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++; |
20 |
3 |
23 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++; |
0 |
3 |
3 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_' || *p == '.')) p++; |
1 |
2 |
3 |
|
| 387 |
2 |
| PY_SPAN("esh-p", ts, p); |
| 388 |
2 |
| continue; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
|
| /* ── string / bytes / f-string / r-string ── */ |
| 392 |
520 |
▶ | if (c == '"' || c == '\'' || |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '"' || c == '\'' || |
512 |
8 |
520 |
| if (c == '"' || c == '\'' || |
509 |
3 |
512 |
| if (c == '"' || c == '\'' || |
493 |
16 |
509 |
|
| 393 |
493 |
▶ | ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
Branch: 4 branches
| Branch | True | False | Total |
| ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
490 |
3 |
493 |
| ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
483 |
7 |
490 |
| ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
481 |
2 |
483 |
| ((c == 'f' || c == 'F' || c == 'b' || c == 'B' || |
471 |
10 |
481 |
|
| 394 |
471 |
▶ | c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
Branch: 3 branches
| Branch | True | False | Total |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
471 |
0 |
471 |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
471 |
0 |
471 |
| c == 'r' || c == 'R' || c == 'u' || c == 'U') && |
0 |
471 |
471 |
|
| 395 |
38 |
▶ | (p[1] == '"' || p[1] == '\'' || |
Branch: 2 branches
| Branch | True | False | Total |
| (p[1] == '"' || p[1] == '\'' || |
35 |
3 |
38 |
| (p[1] == '"' || p[1] == '\'' || |
34 |
1 |
35 |
|
| 396 |
34 |
▶ | ((p[1] == 'b' || p[1] == 'B' || p[1] == 'r' || p[1] == 'R') && |
Branch: 4 branches
| Branch | True | False | Total |
| ((p[1] == 'b' || p[1] == 'B' || p[1] == 'r' || p[1] == 'R') && |
34 |
0 |
34 |
| ((p[1] == 'b' || p[1] == 'B' || p[1] == 'r' || p[1] == 'R') && |
34 |
0 |
34 |
| ((p[1] == 'b' || p[1] == 'B' || p[1] == 'r' || p[1] == 'R') && |
32 |
2 |
34 |
| ((p[1] == 'b' || p[1] == 'B' || p[1] == 'r' || p[1] == 'R') && |
0 |
32 |
32 |
|
| 397 |
2 |
▶ | (p+2 < end) && (p[2] == '"' || p[2] == '\''))))) { |
Branch: 3 branches
| Branch | True | False | Total |
| (p+2 < end) && (p[2] == '"' || p[2] == '\''))))) { |
2 |
0 |
2 |
| (p+2 < end) && (p[2] == '"' || p[2] == '\''))))) { |
2 |
0 |
2 |
| (p+2 < end) && (p[2] == '"' || p[2] == '\''))))) { |
0 |
2 |
2 |
|
| 398 |
15 |
| const char *ts = p; |
| 399 |
15 |
▶ | int is_f = (c == 'f' || c == 'F'); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_f = (c == 'f' || c == 'F'); |
13 |
2 |
15 |
| int is_f = (c == 'f' || c == 'F'); |
0 |
13 |
13 |
|
| 400 |
|
| char q1; |
| 401 |
|
| int triple; |
| 402 |
|
| |
| 403 |
|
| /* skip prefix letters */ |
| 404 |
34 |
▶ | while (p < end && (isalpha((unsigned char)*p)) && |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end && (isalpha((unsigned char)*p)) && |
4 |
15 |
19 |
|
| 405 |
23 |
▶ | *p != '"' && *p != '\'') |
Branch: 3 branches
| Branch | True | False | Total |
| *p != '"' && *p != '\'') |
19 |
0 |
19 |
| *p != '"' && *p != '\'') |
4 |
0 |
4 |
| *p != '"' && *p != '\'') |
4 |
0 |
4 |
|
| 406 |
4 |
| p++; |
| 407 |
15 |
▶ | if (p >= end) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= end) break; |
0 |
15 |
15 |
|
| 408 |
15 |
| q1 = *p++; |
| 409 |
15 |
▶ | triple = (p + 1 < end && *p == q1 && *(p+1) == q1); |
Branch: 3 branches
| Branch | True | False | Total |
| triple = (p + 1 < end && *p == q1 && *(p+1) == q1); |
15 |
0 |
15 |
| triple = (p + 1 < end && *p == q1 && *(p+1) == q1); |
2 |
13 |
15 |
| triple = (p + 1 < end && *p == q1 && *(p+1) == q1); |
2 |
0 |
2 |
|
| 410 |
15 |
▶ | if (triple) p += 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (triple) p += 2; |
2 |
13 |
15 |
|
| 411 |
|
| |
| 412 |
15 |
| eshu_hl_py_string(&out, &p, end, q1, triple); |
| 413 |
|
| /* emit the whole token as esh-s (f-string exprs get no sub-span |
| 414 |
|
| * in this pass — full f-string interpolation would need a |
| 415 |
|
| * recursive sub-scanner) */ |
| 416 |
15 |
| PY_FLUSH(ts); |
| 417 |
15 |
▶ | eshu_hl_span(&out, is_f ? "esh-r" : "esh-s", ts, p); |
Branch: 1 branch
| Branch | True | False | Total |
| eshu_hl_span(&out, is_f ? "esh-r" : "esh-s", ts, p); |
2 |
13 |
15 |
|
| 418 |
15 |
| plain = p; |
| 419 |
15 |
| continue; |
| 420 |
|
| } |
| 421 |
|
| |
| 422 |
|
| /* ── number ── */ |
| 423 |
505 |
▶ | if (isdigit((unsigned char)c) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (isdigit((unsigned char)c) || |
489 |
16 |
505 |
| if (isdigit((unsigned char)c) || |
0 |
489 |
489 |
|
| 424 |
0 |
▶ | (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]))) { |
0 |
0 |
0 |
| (c == '.' && p+1 < end && isdigit((unsigned char)p[1]))) { |
0 |
0 |
0 |
|
| 425 |
16 |
| const char *ts = p; |
| 426 |
|
| /* hex / octal / binary */ |
| 427 |
16 |
▶ | if (c == '0' && p+1 < end && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '0' && p+1 < end && |
5 |
11 |
16 |
| if (c == '0' && p+1 < end && |
5 |
0 |
5 |
|
| 428 |
5 |
▶ | (p[1] == 'x' || p[1] == 'X' || |
Branch: 2 branches
| Branch | True | False | Total |
| (p[1] == 'x' || p[1] == 'X' || |
4 |
1 |
5 |
| (p[1] == 'x' || p[1] == 'X' || |
4 |
0 |
4 |
|
| 429 |
4 |
▶ | p[1] == 'o' || p[1] == 'O' || |
Branch: 2 branches
| Branch | True | False | Total |
| p[1] == 'o' || p[1] == 'O' || |
3 |
1 |
4 |
| p[1] == 'o' || p[1] == 'O' || |
3 |
0 |
3 |
|
| 430 |
3 |
▶ | p[1] == 'b' || p[1] == 'B')) { |
Branch: 2 branches
| Branch | True | False | Total |
| p[1] == 'b' || p[1] == 'B')) { |
2 |
1 |
3 |
| p[1] == 'b' || p[1] == 'B')) { |
0 |
2 |
2 |
|
| 431 |
3 |
| p += 2; |
| 432 |
11 |
▶ | 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++; |
8 |
3 |
11 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
8 |
0 |
8 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 433 |
|
| } else { |
| 434 |
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++; |
26 |
3 |
29 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
16 |
10 |
26 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
10 |
10 |
|
| 435 |
13 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
10 |
3 |
13 |
| if (p < end && *p == '.') { |
2 |
8 |
10 |
|
| 436 |
2 |
| p++; |
| 437 |
5 |
▶ | 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++; |
4 |
1 |
5 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
3 |
1 |
4 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
1 |
1 |
|
| 438 |
|
| } |
| 439 |
13 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
9 |
4 |
13 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
8 |
1 |
9 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
8 |
8 |
|
| 440 |
1 |
| p++; |
| 441 |
1 |
▶ | if (p < end && (*p == '+' || *p == '-')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == '+' || *p == '-')) p++; |
1 |
0 |
1 |
| if (p < end && (*p == '+' || *p == '-')) p++; |
1 |
0 |
1 |
| if (p < end && (*p == '+' || *p == '-')) p++; |
0 |
1 |
1 |
|
| 442 |
3 |
▶ | while (p < end && isdigit((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && isdigit((unsigned char)*p)) p++; |
2 |
1 |
3 |
| while (p < end && isdigit((unsigned char)*p)) p++; |
2 |
0 |
2 |
|
| 443 |
|
| } |
| 444 |
|
| /* complex suffix j/J */ |
| 445 |
13 |
▶ | if (p < end && (*p == 'j' || *p == 'J')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'j' || *p == 'J')) p++; |
8 |
5 |
13 |
| if (p < end && (*p == 'j' || *p == 'J')) p++; |
7 |
1 |
8 |
| if (p < end && (*p == 'j' || *p == 'J')) p++; |
0 |
7 |
7 |
|
| 446 |
|
| } |
| 447 |
16 |
| PY_SPAN("esh-n", ts, p); |
| 448 |
16 |
| continue; |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
|
| /* ── identifier: keyword or builtin ── */ |
| 452 |
489 |
▶ | if (isalpha((unsigned char)c) || c == '_') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (isalpha((unsigned char)c) || c == '_') { |
301 |
188 |
489 |
| if (isalpha((unsigned char)c) || c == '_') { |
0 |
301 |
301 |
|
| 453 |
188 |
| const char *ts = p; |
| 454 |
796 |
▶ | 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++; |
775 |
21 |
796 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
606 |
169 |
775 |
| while (p < end && (isalnum((unsigned char)*p) || *p == '_')) p++; |
2 |
167 |
169 |
|
| 455 |
188 |
▶ | if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_py_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_py_kw)) { |
73 |
115 |
188 |
|
| 456 |
73 |
| PY_SPAN("esh-k", ts, p); |
| 457 |
115 |
▶ | } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_py_bi)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_py_bi)) { |
24 |
91 |
115 |
|
| 458 |
24 |
| PY_SPAN("esh-b", ts, p); |
| 459 |
|
| } |
| 460 |
|
| /* else: plain identifier, leave in plain buffer */ |
| 461 |
188 |
| continue; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
301 |
| p++; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
63 |
| eshu_hl_flush(&out, plain, end); |
| 468 |
63 |
| eshu_buf_putc(&out, '\0'); |
| 469 |
63 |
| *out_len = out.len - 1; |
| 470 |
63 |
| return out.data; |
| 471 |
|
| |
| 472 |
|
| #undef PY_FLUSH |
| 473 |
|
| #undef PY_SPAN |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
|
| #endif /* ESHU_PY_H */ |