| 1 |
|
| /* |
| 2 |
|
| * eshu.h — Core types, config, and public API for Eshu indentation fixer |
| 3 |
|
| * |
| 4 |
|
| * Pure C, no Perl dependencies. |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| #ifndef ESHU_H |
| 8 |
|
| #define ESHU_H |
| 9 |
|
| |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <ctype.h> |
| 13 |
|
| |
| 14 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 15 |
|
| * Language enum |
| 16 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 17 |
|
| |
| 18 |
|
| enum eshu_lang { |
| 19 |
|
| ESHU_LANG_C = 0, |
| 20 |
|
| ESHU_LANG_PERL = 1, |
| 21 |
|
| ESHU_LANG_XS = 2, |
| 22 |
|
| ESHU_LANG_XML = 3, |
| 23 |
|
| ESHU_LANG_HTML = 4, |
| 24 |
|
| ESHU_LANG_CSS = 5, |
| 25 |
|
| ESHU_LANG_JS = 6, |
| 26 |
|
| ESHU_LANG_POD = 7, |
| 27 |
|
| ESHU_LANG_PYTHON = 8, |
| 28 |
|
| ESHU_LANG_BASH = 9, |
| 29 |
|
| ESHU_LANG_GO = 10, |
| 30 |
|
| ESHU_LANG_RUST = 11, |
| 31 |
|
| ESHU_LANG_TS = 12, |
| 32 |
|
| ESHU_LANG_RUBY = 13, |
| 33 |
|
| ESHU_LANG_LUA = 14, |
| 34 |
|
| ESHU_LANG_JAVA = 15, |
| 35 |
|
| ESHU_LANG_SQL = 16, |
| 36 |
|
| ESHU_LANG_JSON = 17, |
| 37 |
|
| ESHU_LANG_YAML = 18, |
| 38 |
|
| ESHU_LANG_PHP = 19 |
| 39 |
|
| }; |
| 40 |
|
| |
| 41 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 42 |
|
| * Scanner state |
| 43 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 44 |
|
| |
| 45 |
|
| enum eshu_state { |
| 46 |
|
| ESHU_CODE, |
| 47 |
|
| ESHU_STRING_DQ, |
| 48 |
|
| ESHU_STRING_SQ, |
| 49 |
|
| ESHU_CHAR_LIT, |
| 50 |
|
| ESHU_COMMENT_LINE, |
| 51 |
|
| ESHU_COMMENT_BLOCK, |
| 52 |
|
| ESHU_PREPROCESSOR, |
| 53 |
|
| /* Perl-specific states */ |
| 54 |
|
| ESHU_HEREDOC, |
| 55 |
|
| ESHU_HEREDOC_INDENT, |
| 56 |
|
| ESHU_REGEX, |
| 57 |
|
| ESHU_QW, |
| 58 |
|
| ESHU_QQ, |
| 59 |
|
| ESHU_Q, |
| 60 |
|
| ESHU_POD, |
| 61 |
|
| /* XML/HTML-specific states */ |
| 62 |
|
| ESHU_XML_TAG, |
| 63 |
|
| ESHU_XML_COMMENT, |
| 64 |
|
| ESHU_XML_CDATA, |
| 65 |
|
| ESHU_XML_PI, |
| 66 |
|
| ESHU_XML_DOCTYPE, |
| 67 |
|
| ESHU_XML_ATTR_DQ, |
| 68 |
|
| ESHU_XML_ATTR_SQ, |
| 69 |
|
| ESHU_XML_VERBATIM, |
| 70 |
|
| /* CSS-specific states */ |
| 71 |
|
| ESHU_CSS_STRING_DQ, |
| 72 |
|
| ESHU_CSS_STRING_SQ, |
| 73 |
|
| ESHU_CSS_COMMENT, |
| 74 |
|
| ESHU_CSS_URL, |
| 75 |
|
| /* JS-specific states */ |
| 76 |
|
| ESHU_JS_TEMPLATE, |
| 77 |
|
| ESHU_JS_REGEX, |
| 78 |
|
| ESHU_JS_REGEX_CLASS, |
| 79 |
|
| /* POD-specific states */ |
| 80 |
|
| ESHU_POD_VERBATIM, |
| 81 |
|
| ESHU_POD_OVER, |
| 82 |
|
| /* Python-specific states */ |
| 83 |
|
| ESHU_PY_STRING_DQ3, /* triple double-quote string """...""" */ |
| 84 |
|
| ESHU_PY_STRING_SQ3, /* triple single-quote string '''...''' */ |
| 85 |
|
| ESHU_PY_STRING_DQ, /* single double-quote "..." */ |
| 86 |
|
| ESHU_PY_STRING_SQ, /* single single-quote '...' */ |
| 87 |
|
| ESHU_PY_FSTRING_DQ, /* f"..." — same as DQ but tracks {} */ |
| 88 |
|
| ESHU_PY_FSTRING_SQ, /* f'...' */ |
| 89 |
|
| /* JSON/JSONC states */ |
| 90 |
|
| ESHU_JSON_STRING, /* "..." — content opaque, track \-escapes */ |
| 91 |
|
| ESHU_JSON_COMMENT_LINE, /* // to EOL (JSONC only) */ |
| 92 |
|
| ESHU_JSON_COMMENT_BLOCK, /* block comment (JSONC only) */ |
| 93 |
|
| /* Bash-specific states */ |
| 94 |
|
| ESHU_BASH_HEREDOC, /* <<WORD heredoc body */ |
| 95 |
|
| ESHU_BASH_HEREDOC_IND, /* <<-WORD heredoc with stripped tabs */ |
| 96 |
|
| ESHU_BASH_ANSI_STR, /* $'...' ANSI-C quoting */ |
| 97 |
|
| ESHU_BASH_DQUOTE, /* "..." with $var interpolation */ |
| 98 |
|
| ESHU_BASH_ARITH, /* $(( )) arithmetic expansion */ |
| 99 |
|
| /* Go-specific states */ |
| 100 |
|
| ESHU_GO_RAW_STR, /* `...` raw string literal (backtick) */ |
| 101 |
|
| ESHU_GO_RUNE, /* '.' rune literal */ |
| 102 |
|
| /* Lua-specific states */ |
| 103 |
|
| ESHU_LUA_STRING_DQ, /* "..." double-quoted string */ |
| 104 |
|
| ESHU_LUA_STRING_SQ, /* '...' single-quoted string */ |
| 105 |
|
| ESHU_LUA_LONG_STR, /* [[...]] / [=[...]=] long string */ |
| 106 |
|
| ESHU_LUA_LONG_CMT, /* --[[...]] long comment */ |
| 107 |
|
| /* Java-specific states */ |
| 108 |
|
| ESHU_JAVA_TEXT_BLOCK, /* """...""" text block (Java 15+) */ |
| 109 |
|
| ESHU_JAVA_CHAR, /* '.' char literal */ |
| 110 |
|
| ESHU_JAVA_ANNOTATION, /* @Annotation — single-line */ |
| 111 |
|
| /* PHP-specific states */ |
| 112 |
|
| ESHU_PHP_STRING_DQ, /* "..." with $var interpolation */ |
| 113 |
|
| ESHU_PHP_STRING_SQ, /* '...' no interpolation */ |
| 114 |
|
| ESHU_PHP_HEREDOC, /* <<<EOT heredoc body */ |
| 115 |
|
| ESHU_PHP_NOWDOC, /* <<<'EOT' nowdoc body */ |
| 116 |
|
| ESHU_PHP_HTML, /* content between ?> and <?php */ |
| 117 |
|
| /* Rust-specific states */ |
| 118 |
|
| ESHU_RUST_RAW_STR, /* r"...", r#"..."#, r##"..."## raw strings */ |
| 119 |
|
| ESHU_RUST_BYTE_STR, /* b"..." byte string literal */ |
| 120 |
|
| ESHU_RUST_CHAR, /* '.' char literal */ |
| 121 |
|
| ESHU_RUST_LIFETIME, /* 'a lifetime annotation */ |
| 122 |
|
| /* Ruby-specific states */ |
| 123 |
|
| ESHU_RB_STRING_DQ, /* "..." with #{} interpolation */ |
| 124 |
|
| ESHU_RB_STRING_SQ, /* '...' no interpolation */ |
| 125 |
|
| ESHU_RB_STRING_PCT, /* %w[] %i[] %Q[] etc. */ |
| 126 |
|
| ESHU_RB_HEREDOC, /* <<HEREDOC body */ |
| 127 |
|
| ESHU_RB_HEREDOC_SQUIG, /* <<~HEREDOC body (strips indent) */ |
| 128 |
|
| ESHU_RB_REGEX, /* /regex/ literal */ |
| 129 |
|
| ESHU_RB_SYMBOL, /* :symbol */ |
| 130 |
|
| ESHU_RB_INTERP, /* #{...} interpolation inside string */ |
| 131 |
|
| /* SQL-specific states */ |
| 132 |
|
| ESHU_SQL_STRING_SQ, /* '...' single-quoted string (ANSI) */ |
| 133 |
|
| ESHU_SQL_STRING_DQ, /* "..." double-quoted identifier */ |
| 134 |
|
| ESHU_SQL_IDENT_BT, /* `...` backtick identifier (MySQL) */ |
| 135 |
|
| ESHU_SQL_IDENT_BR, /* [...] bracketed identifier (T-SQL) */ |
| 136 |
|
| ESHU_SQL_COMMENT_LINE, /* -- to EOL */ |
| 137 |
|
| ESHU_SQL_COMMENT_BLOCK, /* block comment */ |
| 138 |
|
| ESHU_SQL_DOLLAR_STR, /* $$ ... $$ dollar-quoted (PostgreSQL) */ |
| 139 |
|
| /* YAML-specific states */ |
| 140 |
|
| ESHU_YAML_BLOCK_SCALAR_LIT, /* | literal block scalar body */ |
| 141 |
|
| ESHU_YAML_BLOCK_SCALAR_FOLD, /* > folded block scalar body */ |
| 142 |
|
| ESHU_YAML_FLOW_MAP, /* { ... } flow mapping */ |
| 143 |
|
| ESHU_YAML_FLOW_SEQ, /* [ ... ] flow sequence */ |
| 144 |
|
| ESHU_YAML_STRING_DQ, /* "..." double-quoted scalar */ |
| 145 |
|
| ESHU_YAML_STRING_SQ, /* '...' single-quoted scalar */ |
| 146 |
|
| ESHU_YAML_DIRECTIVE /* %YAML / %TAG directives */ |
| 147 |
|
| }; |
| 148 |
|
| |
| 149 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 150 |
|
| * Configuration |
| 151 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 152 |
|
| |
| 153 |
|
| typedef struct { |
| 154 |
|
| char indent_char; /* '\t' or ' ' */ |
| 155 |
|
| int indent_width; /* spaces per level (ignored for tabs) */ |
| 156 |
|
| int indent_pp; /* indent preprocessor directives? */ |
| 157 |
|
| int lang; /* eshu_lang enum value */ |
| 158 |
|
| int range_start; /* first line to reindent (1-based, 0=all) */ |
| 159 |
|
| int range_end; /* last line to reindent (1-based, 0=all) */ |
| 160 |
|
| } eshu_config_t; |
| 161 |
|
| |
| 162 |
2106 |
| static eshu_config_t eshu_default_config(void) { |
| 163 |
|
| eshu_config_t c; |
| 164 |
2106 |
| c.indent_char = '\t'; |
| 165 |
2106 |
| c.indent_width = 1; |
| 166 |
2106 |
| c.indent_pp = 0; |
| 167 |
2106 |
| c.lang = ESHU_LANG_C; |
| 168 |
2106 |
| c.range_start = 0; |
| 169 |
2106 |
| c.range_end = 0; |
| 170 |
2106 |
| return c; |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
|
| /* Check if a line number is within the configured range (or range is disabled) */ |
| 174 |
20514 |
| static int eshu_in_range(const eshu_config_t *cfg, int line_num) { |
| 175 |
20514 |
▶ | if (cfg->range_start == 0) return 1; /* no range = all lines */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->range_start == 0) return 1; /* no range = all lines */ |
20497 |
17 |
20514 |
|
| 176 |
17 |
▶ | return line_num >= cfg->range_start && line_num <= cfg->range_end; |
Branch: 2 branches
| Branch | True | False | Total |
| return line_num >= cfg->range_start && line_num <= cfg->range_end; |
13 |
4 |
17 |
| return line_num >= cfg->range_start && line_num <= cfg->range_end; |
6 |
7 |
13 |
|
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 180 |
|
| * Dynamic buffer |
| 181 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 182 |
|
| |
| 183 |
|
| typedef struct { |
| 184 |
|
| char *data; |
| 185 |
|
| size_t len; |
| 186 |
|
| size_t cap; |
| 187 |
|
| } eshu_buf_t; |
| 188 |
|
| |
| 189 |
3323 |
| static void eshu_buf_init(eshu_buf_t *b, size_t initial) { |
| 190 |
3323 |
▶ | b->cap = initial > 0 ? initial : 4096; |
Branch: 1 branch
| Branch | True | False | Total |
| b->cap = initial > 0 ? initial : 4096; |
3323 |
0 |
3323 |
|
| 191 |
3323 |
| b->data = (char *)malloc(b->cap); |
| 192 |
3323 |
| b->len = 0; |
| 193 |
3323 |
| } |
| 194 |
|
| |
| 195 |
125540 |
| static void eshu_buf_ensure(eshu_buf_t *b, size_t extra) { |
| 196 |
125889 |
▶ | while (b->len + extra > b->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (b->len + extra > b->cap) { |
349 |
125540 |
125889 |
|
| 197 |
349 |
| b->cap *= 2; |
| 198 |
349 |
| b->data = (char *)realloc(b->data, b->cap); |
| 199 |
|
| } |
| 200 |
125540 |
| } |
| 201 |
|
| |
| 202 |
93382 |
| static void eshu_buf_putc(eshu_buf_t *b, char c) { |
| 203 |
93382 |
| eshu_buf_ensure(b, 1); |
| 204 |
93382 |
| b->data[b->len++] = c; |
| 205 |
93382 |
| } |
| 206 |
|
| |
| 207 |
32158 |
| static void eshu_buf_write(eshu_buf_t *b, const char *s, size_t n) { |
| 208 |
32158 |
| eshu_buf_ensure(b, n); |
| 209 |
32158 |
| memcpy(b->data + b->len, s, n); |
| 210 |
32158 |
| b->len += n; |
| 211 |
32158 |
| } |
| 212 |
|
| |
| 213 |
316 |
| static void eshu_buf_free(eshu_buf_t *b) { |
| 214 |
316 |
▶ | if (b->data) free(b->data); |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->data) free(b->data); |
316 |
0 |
316 |
|
| 215 |
316 |
| b->data = NULL; |
| 216 |
316 |
| b->len = 0; |
| 217 |
316 |
| b->cap = 0; |
| 218 |
316 |
| } |
| 219 |
|
| |
| 220 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 221 |
|
| * Line extraction helper |
| 222 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 223 |
|
| |
| 224 |
|
| /* Find end of current line. Returns pointer to '\n' or to the |
| 225 |
|
| * terminating NUL if no newline. */ |
| 226 |
25994 |
| static const char * eshu_find_eol(const char *p) { |
| 227 |
623719 |
▶ | while (*p && *p != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != '\n') |
623716 |
3 |
623719 |
| while (*p && *p != '\n') |
597725 |
25991 |
623716 |
|
| 228 |
597725 |
| p++; |
| 229 |
25994 |
| return p; |
| 230 |
|
| } |
| 231 |
|
| |
| 232 |
|
| /* Return pointer to first non-whitespace char in line */ |
| 233 |
28745 |
| static const char * eshu_skip_leading_ws(const char *p) { |
| 234 |
63742 |
▶ | while (*p == ' ' || *p == '\t') |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p == ' ' || *p == '\t') |
19757 |
43985 |
63742 |
| while (*p == ' ' || *p == '\t') |
15240 |
28745 |
43985 |
|
| 235 |
34997 |
| p++; |
| 236 |
28745 |
| return p; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 240 |
|
| * Trim trailing whitespace |
| 241 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 242 |
|
| |
| 243 |
|
| /* Return length of content with trailing whitespace removed */ |
| 244 |
22841 |
| static int eshu_trimmed_len(const char *content, int len) { |
| 245 |
22862 |
▶ | while (len > 0 && (content[len - 1] == ' ' || content[len - 1] == '\t')) |
Branch: 3 branches
| Branch | True | False | Total |
| while (len > 0 && (content[len - 1] == ' ' || content[len - 1] == '\t')) |
22862 |
0 |
22862 |
| while (len > 0 && (content[len - 1] == ' ' || content[len - 1] == '\t')) |
21 |
22841 |
22862 |
| while (len > 0 && (content[len - 1] == ' ' || content[len - 1] == '\t')) |
0 |
22841 |
22841 |
|
| 246 |
21 |
| len--; |
| 247 |
22841 |
| return len; |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
|
| /* Write content to buffer with trailing whitespace stripped */ |
| 251 |
22841 |
| static void eshu_buf_write_trimmed(eshu_buf_t *b, const char *s, int n) { |
| 252 |
22841 |
| n = eshu_trimmed_len(s, n); |
| 253 |
22841 |
▶ | if (n > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) |
22841 |
0 |
22841 |
|
| 254 |
22841 |
| eshu_buf_write(b, s, (size_t)n); |
| 255 |
22841 |
| } |
| 256 |
|
| |
| 257 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 258 |
|
| * Emit indentation |
| 259 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 260 |
|
| |
| 261 |
21765 |
| static void eshu_emit_indent(eshu_buf_t *out, int depth, |
| 262 |
|
| const eshu_config_t *cfg) { |
| 263 |
|
| int i; |
| 264 |
21765 |
▶ | if (depth < 0) depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth < 0) depth = 0; |
0 |
21765 |
21765 |
|
| 265 |
21765 |
▶ | if (cfg->indent_char == '\t') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->indent_char == '\t') { |
16367 |
5398 |
21765 |
|
| 266 |
37562 |
▶ | for (i = 0; i < depth; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < depth; i++) |
21195 |
16367 |
37562 |
|
| 267 |
21195 |
| eshu_buf_putc(out, '\t'); |
| 268 |
|
| } else { |
| 269 |
5398 |
| int n = depth * cfg->indent_width; |
| 270 |
24034 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
18636 |
5398 |
24034 |
|
| 271 |
18636 |
| eshu_buf_putc(out, ' '); |
| 272 |
|
| } |
| 273 |
21765 |
| } |
| 274 |
|
| |
| 275 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 276 |
|
| * Python context |
| 277 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 278 |
|
| |
| 279 |
|
| typedef struct { |
| 280 |
|
| int depth; /* current logical indent depth */ |
| 281 |
|
| int depth_stack[64]; /* leading-space count per depth level */ |
| 282 |
|
| int depth_top; /* number of entries in depth_stack */ |
| 283 |
|
| int bracket_depth; /* unclosed ( [ { count */ |
| 284 |
|
| int in_continuation; /* 1 if prev line ended with backslash */ |
| 285 |
|
| enum eshu_state state; |
| 286 |
|
| eshu_config_t cfg; |
| 287 |
|
| } eshu_py_ctx_t; |
| 288 |
|
| |
| 289 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 290 |
|
| * JSON context |
| 291 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 292 |
|
| |
| 293 |
|
| typedef struct { |
| 294 |
|
| int depth; |
| 295 |
|
| int jsonc; /* 1 = tolerate // and block comments */ |
| 296 |
|
| enum eshu_state state; |
| 297 |
|
| eshu_config_t cfg; |
| 298 |
|
| } eshu_json_ctx_t; |
| 299 |
|
| |
| 300 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 301 |
|
| * Java context |
| 302 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 303 |
|
| |
| 304 |
|
| typedef struct { |
| 305 |
|
| int depth; |
| 306 |
|
| int paren_depth; |
| 307 |
|
| int bracket_depth; |
| 308 |
|
| int case_depth; |
| 309 |
|
| int case_extra; |
| 310 |
|
| int switch_arrow; |
| 311 |
|
| enum eshu_state state; |
| 312 |
|
| eshu_config_t cfg; |
| 313 |
|
| } eshu_java_ctx_t; |
| 314 |
|
| |
| 315 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 316 |
|
| * PHP context |
| 317 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 318 |
|
| |
| 319 |
|
| typedef struct { |
| 320 |
|
| int depth; |
| 321 |
|
| int paren_depth; |
| 322 |
|
| int bracket_depth; |
| 323 |
|
| int case_depth; |
| 324 |
|
| int case_extra; |
| 325 |
|
| int in_php; /* 0 = HTML mode, 1 = PHP mode */ |
| 326 |
|
| char heredoc_tag[64]; /* end-marker for current heredoc */ |
| 327 |
|
| int heredoc_indent; /* leading-ws chars on end marker */ |
| 328 |
|
| enum eshu_state state; |
| 329 |
|
| eshu_config_t cfg; |
| 330 |
|
| } eshu_php_ctx_t; |
| 331 |
|
| |
| 332 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 333 |
|
| * Ruby context |
| 334 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 335 |
|
| |
| 336 |
|
| typedef struct { |
| 337 |
|
| int depth; |
| 338 |
|
| int brace_depth; /* { } for hash/block */ |
| 339 |
|
| int paren_depth; /* ( ) */ |
| 340 |
|
| int bracket_depth; /* [ ] */ |
| 341 |
|
| int interp_depth; /* #{} nesting in strings */ |
| 342 |
|
| char heredoc_tag[64]; |
| 343 |
|
| int heredoc_squig; /* 1 if <<~ */ |
| 344 |
|
| int can_regex; /* 1 if / starts a regex */ |
| 345 |
|
| enum eshu_state state; |
| 346 |
|
| eshu_config_t cfg; |
| 347 |
|
| } eshu_rb_ctx_t; |
| 348 |
|
| |
| 349 |
|
| #endif /* ESHU_H */ |