| 1 |
|
| /* |
| 2 |
|
| * eshu_sql.h — SQL language indentation formatter |
| 3 |
|
| * |
| 4 |
|
| * SQL has no mandatory indentation; this pass applies conventional |
| 5 |
|
| * clause-based formatting: |
| 6 |
|
| * |
| 7 |
|
| * SELECT ← depth 0 |
| 8 |
|
| * col1, ← depth 1 |
| 9 |
|
| * FROM ← depth 0 |
| 10 |
|
| * table1 ← depth 1 |
| 11 |
|
| * WHERE ← depth 0 |
| 12 |
|
| * cond = 1 ← depth 1 |
| 13 |
|
| * |
| 14 |
|
| * Subqueries increase depth via `(` and decrease via `)`. |
| 15 |
|
| * BEGIN/END blocks increase/decrease depth like braces. |
| 16 |
|
| * CASE/WHEN/THEN/ELSE/END expressions are also tracked. |
| 17 |
|
| * |
| 18 |
|
| * Clause keywords that reset to depth 0 (within the current paren level): |
| 19 |
|
| * SELECT FROM WHERE JOIN (INNER/LEFT/RIGHT/FULL/CROSS JOIN) ON USING |
| 20 |
|
| * GROUP BY ORDER BY HAVING LIMIT OFFSET |
| 21 |
|
| * UNION INTERSECT EXCEPT |
| 22 |
|
| * INSERT INTO VALUES UPDATE SET DELETE |
| 23 |
|
| * RETURNING WITH |
| 24 |
|
| * |
| 25 |
|
| * String/identifier quoting handled so braces/parens inside strings |
| 26 |
|
| * don't affect depth. |
| 27 |
|
| */ |
| 28 |
|
| |
| 29 |
|
| #ifndef ESHU_SQL_H |
| 30 |
|
| #define ESHU_SQL_H |
| 31 |
|
| |
| 32 |
|
| #include "eshu.h" |
| 33 |
|
| #include <string.h> |
| 34 |
|
| #include <ctype.h> |
| 35 |
|
| |
| 36 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 37 |
|
| * Context |
| 38 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 39 |
|
| |
| 40 |
|
| typedef struct { |
| 41 |
|
| int depth; /* current indent depth */ |
| 42 |
|
| int paren_depth; /* net unclosed ( ) subquery nesting */ |
| 43 |
|
| int begin_depth; /* BEGIN/END block nesting */ |
| 44 |
|
| int case_depth; /* CASE/END expression nesting */ |
| 45 |
|
| int clause_active; /* 1 = we just emitted a clause kw */ |
| 46 |
|
| enum eshu_state state; |
| 47 |
|
| char dollar_tag[64];/* current $tag$ for dollar-quoting */ |
| 48 |
|
| int dollar_tag_len; |
| 49 |
|
| eshu_config_t cfg; |
| 50 |
|
| } eshu_sql_ctx_t; |
| 51 |
|
| |
| 52 |
69 |
| static void eshu_sql_ctx_init(eshu_sql_ctx_t *ctx, const eshu_config_t *cfg) { |
| 53 |
69 |
| memset(ctx, 0, sizeof(*ctx)); |
| 54 |
69 |
| ctx->state = ESHU_CODE; |
| 55 |
69 |
| ctx->cfg = *cfg; |
| 56 |
69 |
| } |
| 57 |
|
| |
| 58 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 59 |
|
| * Case-insensitive keyword helpers |
| 60 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 61 |
|
| |
| 62 |
7459 |
| static int eshu_sql_ci_eq(const char *a, const char *b, int len) { |
| 63 |
|
| int i; |
| 64 |
9037 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
8784 |
253 |
9037 |
|
| 65 |
8784 |
▶ | char ca = (a[i] >= 'a' && a[i] <= 'z') ? a[i] - 32 : a[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| char ca = (a[i] >= 'a' && a[i] <= 'z') ? a[i] - 32 : a[i]; |
3030 |
5754 |
8784 |
| char ca = (a[i] >= 'a' && a[i] <= 'z') ? a[i] - 32 : a[i]; |
3030 |
0 |
3030 |
|
| 66 |
8784 |
▶ | char cb = (b[i] >= 'a' && b[i] <= 'z') ? b[i] - 32 : b[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| char cb = (b[i] >= 'a' && b[i] <= 'z') ? b[i] - 32 : b[i]; |
0 |
8784 |
8784 |
| char cb = (b[i] >= 'a' && b[i] <= 'z') ? b[i] - 32 : b[i]; |
0 |
0 |
0 |
|
| 67 |
8784 |
▶ | if (ca != cb) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ca != cb) return 0; |
7206 |
1578 |
8784 |
|
| 68 |
|
| } |
| 69 |
253 |
| return 1; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| /* Match keyword kw (len klen) at position p in content. |
| 73 |
|
| * p must point to start of word; word must be followed by non-alnum. */ |
| 74 |
5224 |
| static int eshu_sql_kw_at(const char *p, const char *end, |
| 75 |
|
| const char *kw, int klen) { |
| 76 |
5224 |
▶ | if (p + klen > end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + klen > end) return 0; |
677 |
4547 |
5224 |
|
| 77 |
4547 |
▶ | if (!eshu_sql_ci_eq(p, kw, klen)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!eshu_sql_ci_eq(p, kw, klen)) return 0; |
4358 |
189 |
4547 |
|
| 78 |
189 |
▶ | if (p + klen < end && (isalnum((unsigned char)p[klen]) || p[klen] == '_')) |
Branch: 3 branches
| Branch | True | False | Total |
| if (p + klen < end && (isalnum((unsigned char)p[klen]) || p[klen] == '_')) |
155 |
34 |
189 |
| if (p + klen < end && (isalnum((unsigned char)p[klen]) || p[klen] == '_')) |
148 |
7 |
155 |
| if (p + klen < end && (isalnum((unsigned char)p[klen]) || p[klen] == '_')) |
0 |
148 |
148 |
|
| 79 |
7 |
| return 0; |
| 80 |
182 |
| return 1; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* Like eshu_sql_kw_at but also consume trailing whitespace and a second word. |
| 84 |
|
| * e.g. "GROUP BY", "ORDER BY", "INSERT INTO", "DELETE FROM", "LEFT JOIN" */ |
| 85 |
3333 |
| static int eshu_sql_kw2_at(const char *p, const char *end, |
| 86 |
|
| const char *w1, int l1, |
| 87 |
|
| const char *w2, int l2, |
| 88 |
|
| int *total_len) { |
| 89 |
3333 |
▶ | if (p + l1 >= end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + l1 >= end) return 0; |
453 |
2880 |
3333 |
|
| 90 |
2880 |
▶ | if (!eshu_sql_ci_eq(p, w1, l1)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!eshu_sql_ci_eq(p, w1, l1)) return 0; |
2848 |
32 |
2880 |
|
| 91 |
32 |
▶ | if (isalnum((unsigned char)p[l1]) || p[l1] == '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (isalnum((unsigned char)p[l1]) || p[l1] == '_') return 0; |
32 |
0 |
32 |
| if (isalnum((unsigned char)p[l1]) || p[l1] == '_') return 0; |
0 |
32 |
32 |
|
| 92 |
32 |
| const char *q = p + l1; |
| 93 |
64 |
▶ | while (q < end && (*q == ' ' || *q == '\t')) q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < end && (*q == ' ' || *q == '\t')) q++; |
64 |
0 |
64 |
| while (q < end && (*q == ' ' || *q == '\t')) q++; |
32 |
32 |
64 |
| while (q < end && (*q == ' ' || *q == '\t')) q++; |
0 |
32 |
32 |
|
| 94 |
32 |
▶ | if (q + l2 > end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q + l2 > end) return 0; |
0 |
32 |
32 |
|
| 95 |
32 |
▶ | if (!eshu_sql_ci_eq(q, w2, l2)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!eshu_sql_ci_eq(q, w2, l2)) return 0; |
0 |
32 |
32 |
|
| 96 |
32 |
▶ | if (q + l2 < end && (isalnum((unsigned char)q[l2]) || q[l2] == '_')) |
Branch: 3 branches
| Branch | True | False | Total |
| if (q + l2 < end && (isalnum((unsigned char)q[l2]) || q[l2] == '_')) |
32 |
0 |
32 |
| if (q + l2 < end && (isalnum((unsigned char)q[l2]) || q[l2] == '_')) |
32 |
0 |
32 |
| if (q + l2 < end && (isalnum((unsigned char)q[l2]) || q[l2] == '_')) |
0 |
32 |
32 |
|
| 97 |
0 |
| return 0; |
| 98 |
32 |
| *total_len = (int)(q + l2 - p); |
| 99 |
32 |
| return 1; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 103 |
|
| * Clause keyword detection at start of content |
| 104 |
|
| * |
| 105 |
|
| * Returns: |
| 106 |
|
| * 0 — not a top-level clause keyword |
| 107 |
|
| * 1 — clause keyword at depth 0 (SELECT, FROM, WHERE, ...) |
| 108 |
|
| * 2 — BEGIN keyword (open block) |
| 109 |
|
| * 3 — END keyword (close block / CASE end) |
| 110 |
|
| * 4 — CASE keyword (open case) |
| 111 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 112 |
|
| |
| 113 |
|
| #define SQL_KW1(w) eshu_sql_kw_at(p, end, w, (int)sizeof(w)-1) |
| 114 |
|
| #define SQL_KW2(a,b) eshu_sql_kw2_at(p, end, a, (int)sizeof(a)-1, b, (int)sizeof(b)-1, kw_len) |
| 115 |
|
| |
| 116 |
362 |
| static int eshu_sql_classify_line(const char *p, const char *end, int *kw_len) { |
| 117 |
|
| /* skip leading whitespace */ |
| 118 |
362 |
▶ | while (p < end && (*p == ' ' || *p == '\t')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
362 |
0 |
362 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
362 |
362 |
| while (p < end && (*p == ' ' || *p == '\t')) p++; |
0 |
362 |
362 |
|
| 119 |
362 |
▶ | if (p >= end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= end) return 0; |
0 |
362 |
362 |
|
| 120 |
|
| |
| 121 |
362 |
| *kw_len = 0; |
| 122 |
|
| |
| 123 |
|
| /* BEGIN / END */ |
| 124 |
362 |
▶ | if (SQL_KW1("BEGIN")) { *kw_len = 5; return 2; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("BEGIN")) { *kw_len = 5; return 2; } |
1 |
361 |
362 |
|
| 125 |
361 |
▶ | if (SQL_KW1("END")) { *kw_len = 3; return 3; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("END")) { *kw_len = 3; return 3; } |
2 |
359 |
361 |
|
| 126 |
359 |
▶ | if (SQL_KW1("CASE")) { *kw_len = 4; return 4; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("CASE")) { *kw_len = 4; return 4; } |
2 |
357 |
359 |
|
| 127 |
|
| |
| 128 |
|
| /* Multi-word clauses first (longer match wins) */ |
| 129 |
357 |
▶ | if (SQL_KW2("GROUP","BY")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("GROUP","BY")) return 1; |
5 |
352 |
357 |
|
| 130 |
352 |
▶ | if (SQL_KW2("ORDER","BY")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("ORDER","BY")) return 1; |
12 |
340 |
352 |
|
| 131 |
340 |
▶ | if (SQL_KW2("INSERT","INTO")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("INSERT","INTO")) return 1; |
8 |
332 |
340 |
|
| 132 |
332 |
▶ | if (SQL_KW2("DELETE","FROM")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("DELETE","FROM")) return 1; |
5 |
327 |
332 |
|
| 133 |
327 |
▶ | if (SQL_KW2("LEFT","JOIN")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("LEFT","JOIN")) return 1; |
2 |
325 |
327 |
|
| 134 |
325 |
▶ | if (SQL_KW2("RIGHT","JOIN")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("RIGHT","JOIN")) return 1; |
0 |
325 |
325 |
|
| 135 |
325 |
▶ | if (SQL_KW2("INNER","JOIN")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("INNER","JOIN")) return 1; |
0 |
325 |
325 |
|
| 136 |
325 |
▶ | if (SQL_KW2("FULL","JOIN")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("FULL","JOIN")) return 1; |
0 |
325 |
325 |
|
| 137 |
325 |
▶ | if (SQL_KW2("CROSS","JOIN")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("CROSS","JOIN")) return 1; |
0 |
325 |
325 |
|
| 138 |
325 |
▶ | if (SQL_KW2("FULL","OUTER")) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW2("FULL","OUTER")) return 1; |
0 |
325 |
325 |
|
| 139 |
|
| |
| 140 |
|
| /* Single-word clauses */ |
| 141 |
325 |
▶ | if (SQL_KW1("SELECT")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("SELECT")) { *kw_len=6; return 1; } |
53 |
272 |
325 |
|
| 142 |
272 |
▶ | if (SQL_KW1("FROM")) { *kw_len=4; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("FROM")) { *kw_len=4; return 1; } |
38 |
234 |
272 |
|
| 143 |
234 |
▶ | if (SQL_KW1("WHERE")) { *kw_len=5; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("WHERE")) { *kw_len=5; return 1; } |
33 |
201 |
234 |
|
| 144 |
201 |
▶ | if (SQL_KW1("JOIN")) { *kw_len=4; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("JOIN")) { *kw_len=4; return 1; } |
7 |
194 |
201 |
|
| 145 |
|
| /* ON / USING are continuations of JOIN — not standalone clause resets */ |
| 146 |
194 |
▶ | if (SQL_KW1("HAVING")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("HAVING")) { *kw_len=6; return 1; } |
2 |
192 |
194 |
|
| 147 |
192 |
▶ | if (SQL_KW1("LIMIT")) { *kw_len=5; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("LIMIT")) { *kw_len=5; return 1; } |
4 |
188 |
192 |
|
| 148 |
188 |
▶ | if (SQL_KW1("OFFSET")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("OFFSET")) { *kw_len=6; return 1; } |
0 |
188 |
188 |
|
| 149 |
188 |
▶ | if (SQL_KW1("UNION")) { *kw_len=5; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("UNION")) { *kw_len=5; return 1; } |
2 |
186 |
188 |
|
| 150 |
186 |
▶ | if (SQL_KW1("INTERSECT")) { *kw_len=9; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("INTERSECT")) { *kw_len=9; return 1; } |
0 |
186 |
186 |
|
| 151 |
186 |
▶ | if (SQL_KW1("EXCEPT")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("EXCEPT")) { *kw_len=6; return 1; } |
0 |
186 |
186 |
|
| 152 |
186 |
▶ | if (SQL_KW1("VALUES")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("VALUES")) { *kw_len=6; return 1; } |
4 |
182 |
186 |
|
| 153 |
182 |
▶ | if (SQL_KW1("UPDATE")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("UPDATE")) { *kw_len=6; return 1; } |
7 |
175 |
182 |
|
| 154 |
175 |
▶ | if (SQL_KW1("SET")) { *kw_len=3; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("SET")) { *kw_len=3; return 1; } |
4 |
171 |
175 |
|
| 155 |
171 |
▶ | if (SQL_KW1("RETURNING")) { *kw_len=9; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("RETURNING")) { *kw_len=9; return 1; } |
0 |
171 |
171 |
|
| 156 |
171 |
▶ | if (SQL_KW1("WITH")) { *kw_len=4; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("WITH")) { *kw_len=4; return 1; } |
5 |
166 |
171 |
|
| 157 |
166 |
▶ | if (SQL_KW1("CREATE")) { *kw_len=6; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("CREATE")) { *kw_len=6; return 1; } |
9 |
157 |
166 |
|
| 158 |
157 |
▶ | if (SQL_KW1("ALTER")) { *kw_len=5; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("ALTER")) { *kw_len=5; return 1; } |
1 |
156 |
157 |
|
| 159 |
156 |
▶ | if (SQL_KW1("DROP")) { *kw_len=4; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("DROP")) { *kw_len=4; return 1; } |
0 |
156 |
156 |
|
| 160 |
156 |
▶ | if (SQL_KW1("TRUNCATE")) { *kw_len=8; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("TRUNCATE")) { *kw_len=8; return 1; } |
0 |
156 |
156 |
|
| 161 |
|
| |
| 162 |
|
| /* WHEN/THEN/ELSE inside CASE — dedent/same-level transitions */ |
| 163 |
156 |
▶ | if (SQL_KW1("WHEN")) { *kw_len=4; return 5; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("WHEN")) { *kw_len=4; return 5; } |
6 |
150 |
156 |
|
| 164 |
150 |
▶ | if (SQL_KW1("THEN")) { *kw_len=4; return 6; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("THEN")) { *kw_len=4; return 6; } |
0 |
150 |
150 |
|
| 165 |
150 |
▶ | if (SQL_KW1("ELSE")) { *kw_len=4; return 6; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (SQL_KW1("ELSE")) { *kw_len=4; return 6; } |
2 |
148 |
150 |
|
| 166 |
|
| |
| 167 |
148 |
| return 0; |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
|
| #undef SQL_KW1 |
| 171 |
|
| #undef SQL_KW2 |
| 172 |
|
| |
| 173 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 174 |
|
| * Scan a line for paren depth changes (outside strings/comments) |
| 175 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 176 |
|
| |
| 177 |
362 |
| static void eshu_sql_scan_parens(eshu_sql_ctx_t *ctx, |
| 178 |
|
| const char *p, const char *end) |
| 179 |
|
| { |
| 180 |
9448 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
9088 |
360 |
9448 |
|
| 181 |
9088 |
| char c = *p; |
| 182 |
|
| |
| 183 |
9088 |
▶ | if (ctx->state == ESHU_SQL_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_COMMENT_BLOCK) { |
17 |
9071 |
9088 |
|
| 184 |
17 |
▶ | if (c == '*' && p+1 < end && *(p+1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p+1 < end && *(p+1) == '/') { |
1 |
16 |
17 |
| if (c == '*' && p+1 < end && *(p+1) == '/') { |
1 |
0 |
1 |
| if (c == '*' && p+1 < end && *(p+1) == '/') { |
1 |
0 |
1 |
|
| 185 |
1 |
| ctx->state = ESHU_CODE; p += 2; |
| 186 |
16 |
| } else p++; |
| 187 |
17 |
| continue; |
| 188 |
|
| } |
| 189 |
9071 |
▶ | if (ctx->state == ESHU_SQL_COMMENT_LINE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_COMMENT_LINE) { |
8 |
9063 |
9071 |
|
| 190 |
|
| /* handled by line end */ |
| 191 |
8 |
| p++; continue; |
| 192 |
|
| } |
| 193 |
9063 |
▶ | if (ctx->state == ESHU_SQL_STRING_SQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_STRING_SQ) { |
662 |
8401 |
9063 |
|
| 194 |
662 |
▶ | if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
83 |
579 |
662 |
| if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
62 |
21 |
83 |
| if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
1 |
61 |
62 |
|
| 195 |
661 |
▶ | if (c == '\'') { ctx->state = ESHU_CODE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { ctx->state = ESHU_CODE; } |
82 |
579 |
661 |
|
| 196 |
661 |
| p++; continue; |
| 197 |
|
| } |
| 198 |
8401 |
▶ | if (ctx->state == ESHU_SQL_STRING_DQ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_STRING_DQ) { |
0 |
8401 |
8401 |
|
| 199 |
0 |
▶ | if (c == '"') ctx->state = ESHU_CODE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') ctx->state = ESHU_CODE; |
0 |
0 |
0 |
|
| 200 |
0 |
| p++; continue; |
| 201 |
|
| } |
| 202 |
8401 |
▶ | if (ctx->state == ESHU_SQL_IDENT_BT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_IDENT_BT) { |
0 |
8401 |
8401 |
|
| 203 |
0 |
▶ | if (c == '`') ctx->state = ESHU_CODE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') ctx->state = ESHU_CODE; |
0 |
0 |
0 |
|
| 204 |
0 |
| p++; continue; |
| 205 |
|
| } |
| 206 |
8401 |
▶ | if (ctx->state == ESHU_SQL_IDENT_BR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_IDENT_BR) { |
0 |
8401 |
8401 |
|
| 207 |
0 |
▶ | if (c == ']') ctx->state = ESHU_CODE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ']') ctx->state = ESHU_CODE; |
0 |
0 |
0 |
|
| 208 |
0 |
| p++; continue; |
| 209 |
|
| } |
| 210 |
8401 |
▶ | if (ctx->state == ESHU_SQL_DOLLAR_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_DOLLAR_STR) { |
0 |
8401 |
8401 |
|
| 211 |
|
| /* look for closing $tag$ */ |
| 212 |
0 |
▶ | if (c == '$' && ctx->dollar_tag_len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '$' && ctx->dollar_tag_len > 0) { |
0 |
0 |
0 |
| if (c == '$' && ctx->dollar_tag_len > 0) { |
0 |
0 |
0 |
|
| 213 |
0 |
| int tl = ctx->dollar_tag_len; |
| 214 |
0 |
▶ | if (p + tl <= end && |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + tl <= end && |
0 |
0 |
0 |
|
| 215 |
0 |
▶ | memcmp(p, ctx->dollar_tag, (size_t)tl) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, ctx->dollar_tag, (size_t)tl) == 0) { |
0 |
0 |
0 |
|
| 216 |
0 |
| ctx->state = ESHU_CODE; |
| 217 |
0 |
| ctx->dollar_tag_len = 0; |
| 218 |
0 |
| p += tl; continue; |
| 219 |
|
| } |
| 220 |
|
| } |
| 221 |
0 |
| p++; continue; |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* ESHU_CODE */ |
| 225 |
8401 |
▶ | if (c == '-' && p+1 < end && *(p+1) == '-') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
17 |
8384 |
8401 |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
17 |
0 |
17 |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
2 |
15 |
17 |
|
| 226 |
2 |
| ctx->state = ESHU_SQL_COMMENT_LINE; |
| 227 |
2 |
| return; /* rest of line is comment */ |
| 228 |
|
| } |
| 229 |
8399 |
▶ | if (c == '/' && p+1 < end && *(p+1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
2 |
8397 |
8399 |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
2 |
0 |
2 |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
1 |
1 |
2 |
|
| 230 |
1 |
| ctx->state = ESHU_SQL_COMMENT_BLOCK; p += 2; continue; |
| 231 |
|
| } |
| 232 |
8398 |
▶ | if (c == '\'') { ctx->state = ESHU_SQL_STRING_SQ; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { ctx->state = ESHU_SQL_STRING_SQ; p++; continue; } |
82 |
8316 |
8398 |
|
| 233 |
8316 |
▶ | if (c == '"') { ctx->state = ESHU_SQL_STRING_DQ; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { ctx->state = ESHU_SQL_STRING_DQ; p++; continue; } |
0 |
8316 |
8316 |
|
| 234 |
8316 |
▶ | if (c == '`') { ctx->state = ESHU_SQL_IDENT_BT; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') { ctx->state = ESHU_SQL_IDENT_BT; p++; continue; } |
0 |
8316 |
8316 |
|
| 235 |
8316 |
▶ | if (c == '[') { ctx->state = ESHU_SQL_IDENT_BR; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '[') { ctx->state = ESHU_SQL_IDENT_BR; p++; continue; } |
0 |
8316 |
8316 |
|
| 236 |
|
| |
| 237 |
|
| /* dollar-quoting: $tag$ or $$ */ |
| 238 |
8316 |
▶ | if (c == '$') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '$') { |
0 |
8316 |
8316 |
|
| 239 |
0 |
| const char *q = p + 1; |
| 240 |
0 |
▶ | while (q < end && *q != '$' && *q != '\n') q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
|
| 241 |
0 |
▶ | if (q < end && *q == '$') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < end && *q == '$') { |
0 |
0 |
0 |
| if (q < end && *q == '$') { |
0 |
0 |
0 |
|
| 242 |
0 |
| int tl = (int)(q - p + 1); |
| 243 |
0 |
▶ | if (tl < 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tl < 64) { |
0 |
0 |
0 |
|
| 244 |
0 |
| memcpy(ctx->dollar_tag, p, (size_t)tl); |
| 245 |
0 |
| ctx->dollar_tag[tl] = '\0'; |
| 246 |
0 |
| ctx->dollar_tag_len = tl; |
| 247 |
0 |
| ctx->state = ESHU_SQL_DOLLAR_STR; |
| 248 |
0 |
| p = q + 1; continue; |
| 249 |
|
| } |
| 250 |
|
| } |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
8316 |
▶ | if (c == '(') { ctx->paren_depth++; ctx->depth++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '(') { ctx->paren_depth++; ctx->depth++; } |
133 |
8183 |
8316 |
|
| 254 |
8316 |
▶ | if (c == ')') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ')') { |
133 |
8183 |
8316 |
|
| 255 |
133 |
| ctx->paren_depth--; |
| 256 |
133 |
| ctx->depth--; |
| 257 |
133 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
133 |
133 |
|
| 258 |
|
| } |
| 259 |
8316 |
| p++; |
| 260 |
|
| } |
| 261 |
|
| /* end of line resets line-comment state */ |
| 262 |
360 |
▶ | if (ctx->state == ESHU_SQL_COMMENT_LINE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_COMMENT_LINE) |
2 |
358 |
360 |
|
| 263 |
2 |
| ctx->state = ESHU_CODE; |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 267 |
|
| * Process one line |
| 268 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 269 |
|
| |
| 270 |
370 |
| static void eshu_sql_process_line(eshu_sql_ctx_t *ctx, eshu_buf_t *out, |
| 271 |
|
| const char *line_start, const char *eol, |
| 272 |
|
| int lineno) |
| 273 |
|
| { |
| 274 |
370 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 275 |
|
| int line_len; |
| 276 |
|
| int indent_depth; |
| 277 |
370 |
| int kw_len = 0; |
| 278 |
|
| int kw_class; |
| 279 |
|
| |
| 280 |
|
| /* empty line */ |
| 281 |
370 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
8 |
362 |
370 |
|
| 282 |
8 |
| eshu_buf_putc(out, '\n'); |
| 283 |
19 |
| return; |
| 284 |
|
| } |
| 285 |
362 |
| line_len = (int)(eol - content); |
| 286 |
|
| |
| 287 |
|
| /* continuation of block comment or dollar-quoted string: emit verbatim */ |
| 288 |
362 |
▶ | if (ctx->state == ESHU_SQL_COMMENT_BLOCK || ctx->state == ESHU_SQL_DOLLAR_STR) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->state == ESHU_SQL_COMMENT_BLOCK || ctx->state == ESHU_SQL_DOLLAR_STR) { |
362 |
0 |
362 |
| if (ctx->state == ESHU_SQL_COMMENT_BLOCK || ctx->state == ESHU_SQL_DOLLAR_STR) { |
0 |
362 |
362 |
|
| 289 |
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 |
|
| 290 |
0 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 291 |
0 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 292 |
|
| } else { |
| 293 |
0 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 294 |
|
| } |
| 295 |
0 |
| eshu_buf_putc(out, '\n'); |
| 296 |
0 |
| eshu_sql_scan_parens(ctx, content, eol); |
| 297 |
0 |
| return; |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
|
| /* Classify the first keyword on this line */ |
| 301 |
362 |
| kw_class = eshu_sql_classify_line(content, eol, &kw_len); |
| 302 |
|
| |
| 303 |
362 |
| switch (kw_class) { |
| 304 |
201 |
| case 1: /* top-level clause keyword — emit at paren_depth */ |
| 305 |
201 |
| indent_depth = ctx->paren_depth; |
| 306 |
201 |
| ctx->clause_active = 1; |
| 307 |
201 |
| break; |
| 308 |
1 |
| case 2: /* BEGIN */ |
| 309 |
1 |
| indent_depth = ctx->paren_depth + ctx->begin_depth; |
| 310 |
1 |
| ctx->begin_depth++; |
| 311 |
1 |
| ctx->clause_active = 0; |
| 312 |
1 |
| break; |
| 313 |
2 |
| case 3: /* END */ |
| 314 |
2 |
▶ | if (ctx->case_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->case_depth > 0) { |
2 |
0 |
2 |
|
| 315 |
2 |
| ctx->case_depth--; |
| 316 |
2 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + ctx->case_depth; |
| 317 |
0 |
▶ | } else if (ctx->begin_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ctx->begin_depth > 0) { |
0 |
0 |
0 |
|
| 318 |
0 |
| ctx->begin_depth--; |
| 319 |
0 |
| indent_depth = ctx->paren_depth + ctx->begin_depth; |
| 320 |
|
| } else { |
| 321 |
0 |
| indent_depth = ctx->paren_depth; |
| 322 |
|
| } |
| 323 |
2 |
| ctx->clause_active = 0; |
| 324 |
2 |
| break; |
| 325 |
2 |
| case 4: /* CASE */ |
| 326 |
2 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + ctx->case_depth; |
| 327 |
2 |
| ctx->case_depth++; |
| 328 |
2 |
| ctx->clause_active = 0; |
| 329 |
2 |
| break; |
| 330 |
6 |
| case 5: /* WHEN — transition inside CASE, at case level */ |
| 331 |
6 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + |
| 332 |
6 |
| (ctx->case_depth > 0 ? ctx->case_depth - 1 : 0) + 1; |
| 333 |
6 |
| ctx->clause_active = 0; |
| 334 |
6 |
| break; |
| 335 |
2 |
| case 6: /* THEN/ELSE — same as WHEN position */ |
| 336 |
2 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + |
| 337 |
2 |
| (ctx->case_depth > 0 ? ctx->case_depth - 1 : 0) + 1; |
| 338 |
2 |
| ctx->clause_active = 0; |
| 339 |
2 |
| break; |
| 340 |
148 |
| default: |
| 341 |
|
| /* closing paren at start of content */ |
| 342 |
148 |
▶ | if (*content == ')') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*content == ')') { |
11 |
137 |
148 |
|
| 343 |
11 |
| eshu_sql_scan_parens(ctx, content, eol); |
| 344 |
11 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + ctx->case_depth; |
| 345 |
11 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
11 |
0 |
11 |
|
| 346 |
11 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 347 |
|
| } else { |
| 348 |
0 |
| eshu_buf_write(out, line_start, (size_t)(content - line_start)); |
| 349 |
|
| } |
| 350 |
11 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 351 |
11 |
| eshu_buf_putc(out, '\n'); |
| 352 |
11 |
| ctx->clause_active = 0; |
| 353 |
11 |
| return; |
| 354 |
|
| } |
| 355 |
|
| /* continuation line */ |
| 356 |
137 |
| indent_depth = ctx->paren_depth + ctx->begin_depth + ctx->case_depth |
| 357 |
137 |
| + (ctx->clause_active ? 1 : 0); |
| 358 |
137 |
| break; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
351 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
351 |
0 |
351 |
|
| 362 |
351 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 363 |
|
| } else { |
| 364 |
0 |
| eshu_buf_write(out, line_start, (size_t)(content - line_start)); |
| 365 |
|
| } |
| 366 |
351 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 367 |
351 |
| eshu_buf_putc(out, '\n'); |
| 368 |
|
| |
| 369 |
|
| { |
| 370 |
351 |
| int old_paren = ctx->paren_depth; |
| 371 |
351 |
| eshu_sql_scan_parens(ctx, content, eol); |
| 372 |
|
| /* If a clause kw opened a net paren, the paren provides indentation */ |
| 373 |
351 |
▶ | if (kw_class == 1 && ctx->paren_depth > old_paren) |
Branch: 2 branches
| Branch | True | False | Total |
| if (kw_class == 1 && ctx->paren_depth > old_paren) |
201 |
150 |
351 |
| if (kw_class == 1 && ctx->paren_depth > old_paren) |
9 |
192 |
201 |
|
| 374 |
9 |
| ctx->clause_active = 0; |
| 375 |
|
| } |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 379 |
|
| * Public API |
| 380 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 381 |
|
| |
| 382 |
69 |
| static char *eshu_indent_sql(const char *src, size_t src_len, |
| 383 |
|
| const eshu_config_t *cfg, size_t *out_len) |
| 384 |
|
| { |
| 385 |
|
| eshu_sql_ctx_t ctx; |
| 386 |
|
| eshu_buf_t out; |
| 387 |
69 |
| const char *p = src; |
| 388 |
69 |
| const char *end = src + src_len; |
| 389 |
69 |
| int lineno = 1; |
| 390 |
|
| |
| 391 |
69 |
| eshu_sql_ctx_init(&ctx, cfg); |
| 392 |
69 |
| eshu_buf_init(&out, src_len + 256); |
| 393 |
|
| |
| 394 |
439 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
370 |
69 |
439 |
|
| 395 |
370 |
| const char *eol = eshu_find_eol(p); |
| 396 |
370 |
| eshu_sql_process_line(&ctx, &out, p, eol, lineno); |
| 397 |
370 |
| p = eol; |
| 398 |
370 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
370 |
0 |
370 |
|
| 399 |
370 |
| lineno++; |
| 400 |
|
| } |
| 401 |
|
| |
| 402 |
69 |
| eshu_buf_putc(&out, '\0'); |
| 403 |
69 |
| out.len--; |
| 404 |
69 |
| *out_len = out.len; |
| 405 |
69 |
| return out.data; |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
|
| |
| 409 |
|
| #include "eshu_hl_util.h" |
| 410 |
|
| |
| 411 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 412 |
|
| * SQL keyword / builtin lists (stored uppercase; matched case-insensitively) |
| 413 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 414 |
|
| |
| 415 |
|
| static const char * const eshu_hl_sql_kw[] = { |
| 416 |
|
| /* DML */ |
| 417 |
|
| "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", |
| 418 |
|
| "FULL", "OUTER", "CROSS", "ON", "USING", |
| 419 |
|
| "GROUP", "BY", "HAVING", "ORDER", "LIMIT", "OFFSET", |
| 420 |
|
| "UNION", "ALL", "INTERSECT", "EXCEPT", |
| 421 |
|
| "INSERT", "INTO", "VALUES", "UPDATE", "SET", "DELETE", |
| 422 |
|
| "RETURNING", |
| 423 |
|
| /* DDL */ |
| 424 |
|
| "CREATE", "ALTER", "DROP", "TRUNCATE", "RENAME", |
| 425 |
|
| "TABLE", "VIEW", "INDEX", "SEQUENCE", "SCHEMA", |
| 426 |
|
| "COLUMN", "CONSTRAINT", "PRIMARY", "KEY", "FOREIGN", |
| 427 |
|
| "REFERENCES", "UNIQUE", "CHECK", "DEFAULT", "NOT", "NULL", |
| 428 |
|
| "AUTO_INCREMENT", "SERIAL", "IDENTITY", |
| 429 |
|
| /* DQL / expressions */ |
| 430 |
|
| "AS", "DISTINCT", "EXISTS", "IN", "BETWEEN", "LIKE", "ILIKE", |
| 431 |
|
| "IS", "AND", "OR", "CASE", "WHEN", "THEN", "ELSE", "END", |
| 432 |
|
| "CAST", "COALESCE", "NULLIF", "WITH", |
| 433 |
|
| /* procedural */ |
| 434 |
|
| "BEGIN", "DECLARE", "PROCEDURE", "FUNCTION", |
| 435 |
|
| "TRIGGER", "IF", "ELSIF", "ELSEIF", "LOOP", "WHILE", "FOR", |
| 436 |
|
| "RETURN", "RAISE", "EXCEPTION", |
| 437 |
|
| NULL |
| 438 |
|
| }; |
| 439 |
|
| |
| 440 |
|
| static const char * const eshu_hl_sql_bi[] = { |
| 441 |
|
| "COUNT", "SUM", "AVG", "MIN", "MAX", |
| 442 |
|
| "UPPER", "LOWER", "TRIM", "LENGTH", "SUBSTR", "SUBSTRING", |
| 443 |
|
| "REPLACE", "CONCAT", |
| 444 |
|
| "NOW", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", |
| 445 |
|
| "DATE_TRUNC", "DATE_PART", "EXTRACT", |
| 446 |
|
| "TO_CHAR", "TO_DATE", "TO_NUMBER", |
| 447 |
|
| "ROW_NUMBER", "RANK", "DENSE_RANK", "LEAD", "LAG", |
| 448 |
|
| "FIRST_VALUE", "LAST_VALUE", "NTILE", "OVER", "PARTITION", |
| 449 |
|
| NULL |
| 450 |
|
| }; |
| 451 |
|
| |
| 452 |
|
| /* Case-insensitive keyword lookup for SQL */ |
| 453 |
352 |
| static int eshu_hl_sql_kw_match(const char *s, size_t n, |
| 454 |
|
| const char * const *list) { |
| 455 |
|
| size_t i; |
| 456 |
15361 |
▶ | for (i = 0; list[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; list[i]; i++) { |
15149 |
212 |
15361 |
|
| 457 |
15149 |
| size_t klen = strlen(list[i]); |
| 458 |
15149 |
▶ | if (klen != n) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (klen != n) continue; |
13864 |
1285 |
15149 |
|
| 459 |
|
| size_t j; |
| 460 |
2044 |
▶ | for (j = 0; j < n; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) { |
1904 |
140 |
2044 |
|
| 461 |
1904 |
| char sc = s[j], kc = list[i][j]; |
| 462 |
1904 |
▶ | if (sc >= 'a' && sc <= 'z') sc -= 32; |
Branch: 2 branches
| Branch | True | False | Total |
| if (sc >= 'a' && sc <= 'z') sc -= 32; |
769 |
1135 |
1904 |
| if (sc >= 'a' && sc <= 'z') sc -= 32; |
769 |
0 |
769 |
|
| 463 |
1904 |
▶ | if (sc != kc) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sc != kc) break; |
1145 |
759 |
1904 |
|
| 464 |
|
| } |
| 465 |
1285 |
▶ | if (j == n) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == n) return 1; |
140 |
1145 |
1285 |
|
| 466 |
|
| } |
| 467 |
212 |
| return 0; |
| 468 |
|
| } |
| 469 |
|
| |
| 470 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 471 |
|
| * SQL highlighter |
| 472 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 473 |
|
| |
| 474 |
42 |
| static char *eshu_highlight_sql(const char *src, size_t src_len, size_t *out_len) { |
| 475 |
|
| eshu_buf_t out; |
| 476 |
42 |
| const char *p = src; |
| 477 |
42 |
| const char *end = src + src_len; |
| 478 |
42 |
| const char *plain = p; |
| 479 |
|
| /* dollar-quoting tag */ |
| 480 |
|
| char dtag[64]; |
| 481 |
42 |
| int dtag_len = 0; |
| 482 |
42 |
| int in_dollar = 0; |
| 483 |
|
| |
| 484 |
42 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 485 |
|
| |
| 486 |
|
| #define SQL_SPAN(cls, ts, te) do { \ |
| 487 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 488 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 489 |
|
| p = (te); plain = p; \ |
| 490 |
|
| } while(0) |
| 491 |
|
| |
| 492 |
625 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
583 |
42 |
625 |
|
| 493 |
583 |
| char c = *p; |
| 494 |
|
| |
| 495 |
|
| /* dollar-quoted string */ |
| 496 |
583 |
▶ | if (in_dollar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_dollar) { |
0 |
583 |
583 |
|
| 497 |
0 |
▶ | if (c == '$' && dtag_len > 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '$' && dtag_len > 0 && |
0 |
0 |
0 |
| if (c == '$' && dtag_len > 0 && |
0 |
0 |
0 |
|
| 498 |
0 |
▶ | (size_t)(end - p) >= (size_t)dtag_len && |
Branch: 1 branch
| Branch | True | False | Total |
| (size_t)(end - p) >= (size_t)dtag_len && |
0 |
0 |
0 |
|
| 499 |
0 |
▶ | memcmp(p, dtag, (size_t)dtag_len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, dtag, (size_t)dtag_len) == 0) { |
0 |
0 |
0 |
|
| 500 |
0 |
| p += dtag_len; |
| 501 |
0 |
| SQL_SPAN("esh-s", plain, p); |
| 502 |
0 |
| in_dollar = 0; dtag_len = 0; |
| 503 |
|
| /* plain already reset by SQL_SPAN */ |
| 504 |
|
| } else { |
| 505 |
0 |
| p++; |
| 506 |
|
| } |
| 507 |
0 |
| continue; |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
|
| /* line comment -- */ |
| 511 |
583 |
▶ | if (c == '-' && p+1 < end && *(p+1) == '-') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
1 |
582 |
583 |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
1 |
0 |
1 |
| if (c == '-' && p+1 < end && *(p+1) == '-') { |
1 |
0 |
1 |
|
| 512 |
1 |
| const char *ts = p; |
| 513 |
16 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
15 |
1 |
16 |
| while (p < end && *p != '\n') p++; |
15 |
0 |
15 |
|
| 514 |
1 |
| SQL_SPAN("esh-c", ts, p); |
| 515 |
1 |
| continue; |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
|
| /* block comment */ |
| 519 |
582 |
▶ | if (c == '/' && p+1 < end && *(p+1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
1 |
581 |
582 |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
1 |
0 |
1 |
| if (c == '/' && p+1 < end && *(p+1) == '*') { |
1 |
0 |
1 |
|
| 520 |
1 |
| const char *ts = p; |
| 521 |
1 |
| p += 2; |
| 522 |
16 |
▶ | while (p+1 < end && !(*p == '*' && *(p+1) == '/')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p+1 < end && !(*p == '*' && *(p+1) == '/')) p++; |
16 |
0 |
16 |
| while (p+1 < end && !(*p == '*' && *(p+1) == '/')) p++; |
15 |
1 |
16 |
| while (p+1 < end && !(*p == '*' && *(p+1) == '/')) p++; |
0 |
1 |
1 |
|
| 523 |
1 |
▶ | if (p+1 < end) p += 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p+1 < end) p += 2; |
1 |
0 |
1 |
|
| 524 |
1 |
| SQL_SPAN("esh-c", ts, p); |
| 525 |
1 |
| continue; |
| 526 |
|
| } |
| 527 |
|
| |
| 528 |
|
| /* single-quoted string '...' with '' escape */ |
| 529 |
581 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
4 |
577 |
581 |
|
| 530 |
4 |
| const char *ts = p++; |
| 531 |
15 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
15 |
0 |
15 |
|
| 532 |
15 |
▶ | if (*p == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
5 |
10 |
15 |
| if (*p == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
3 |
2 |
5 |
| if (*p == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; continue; } |
1 |
2 |
3 |
|
| 533 |
14 |
▶ | if (*p == '\'') { p++; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\'') { p++; break; } |
4 |
10 |
14 |
|
| 534 |
10 |
| p++; |
| 535 |
|
| } |
| 536 |
4 |
| SQL_SPAN("esh-s", ts, p); |
| 537 |
4 |
| continue; |
| 538 |
|
| } |
| 539 |
|
| |
| 540 |
|
| /* double-quoted identifier "..." */ |
| 541 |
577 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
1 |
576 |
577 |
|
| 542 |
1 |
| const char *ts = p++; |
| 543 |
7 |
▶ | while (p < end && *p != '"') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') p++; |
7 |
0 |
7 |
| while (p < end && *p != '"') p++; |
6 |
1 |
7 |
|
| 544 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 545 |
1 |
| SQL_SPAN("esh-a", ts, p); |
| 546 |
1 |
| continue; |
| 547 |
|
| } |
| 548 |
|
| |
| 549 |
|
| /* backtick identifier `...` (MySQL) */ |
| 550 |
576 |
▶ | if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') { |
1 |
575 |
576 |
|
| 551 |
1 |
| const char *ts = p++; |
| 552 |
7 |
▶ | while (p < end && *p != '`') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '`') p++; |
7 |
0 |
7 |
| while (p < end && *p != '`') p++; |
6 |
1 |
7 |
|
| 553 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 554 |
1 |
| SQL_SPAN("esh-a", ts, p); |
| 555 |
1 |
| continue; |
| 556 |
|
| } |
| 557 |
|
| |
| 558 |
|
| /* bracketed identifier [...] (T-SQL) */ |
| 559 |
575 |
▶ | if (c == '[') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '[') { |
1 |
574 |
575 |
|
| 560 |
1 |
| const char *ts = p++; |
| 561 |
7 |
▶ | while (p < end && *p != ']') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != ']') p++; |
7 |
0 |
7 |
| while (p < end && *p != ']') p++; |
6 |
1 |
7 |
|
| 562 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 563 |
1 |
| SQL_SPAN("esh-a", ts, p); |
| 564 |
1 |
| continue; |
| 565 |
|
| } |
| 566 |
|
| |
| 567 |
|
| /* dollar-quoting: $tag$ or $$ */ |
| 568 |
574 |
▶ | if (c == '$') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '$') { |
0 |
574 |
574 |
|
| 569 |
0 |
| const char *q = p + 1; |
| 570 |
0 |
▶ | while (q < end && *q != '$' && *q != '\n') q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
| while (q < end && *q != '$' && *q != '\n') q++; |
0 |
0 |
0 |
|
| 571 |
0 |
▶ | if (q < end && *q == '$') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < end && *q == '$') { |
0 |
0 |
0 |
| if (q < end && *q == '$') { |
0 |
0 |
0 |
|
| 572 |
0 |
| int tl = (int)(q - p + 1); |
| 573 |
0 |
▶ | if (tl < 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tl < 64) { |
0 |
0 |
0 |
|
| 574 |
|
| /* start of dollar-quoted string — the opening tag is plain */ |
| 575 |
0 |
| eshu_hl_flush(&out, plain, p); |
| 576 |
0 |
| plain = p; |
| 577 |
0 |
| memcpy(dtag, p, (size_t)tl); |
| 578 |
0 |
| dtag[tl] = '\0'; |
| 579 |
0 |
| dtag_len = tl; |
| 580 |
0 |
| in_dollar = 1; |
| 581 |
0 |
| p = q + 1; |
| 582 |
0 |
| continue; |
| 583 |
|
| } |
| 584 |
|
| } |
| 585 |
|
| } |
| 586 |
|
| |
| 587 |
|
| /* number */ |
| 588 |
574 |
▶ | if (isdigit((unsigned char)c) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (isdigit((unsigned char)c) || |
556 |
18 |
574 |
| if (isdigit((unsigned char)c) || |
6 |
550 |
556 |
|
| 589 |
6 |
▶ | (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)))) { |
6 |
0 |
6 |
| (c == '.' && p+1 < end && isdigit((unsigned char)*(p+1)))) { |
0 |
6 |
6 |
|
| 590 |
18 |
| const char *ts = p; |
| 591 |
42 |
▶ | while (p < end && (isdigit((unsigned char)*p) || *p == '.' || *p == '_')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && (isdigit((unsigned char)*p) || *p == '.' || *p == '_')) p++; |
32 |
10 |
42 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '.' || *p == '_')) p++; |
23 |
9 |
32 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '.' || *p == '_')) p++; |
1 |
8 |
9 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '.' || *p == '_')) p++; |
0 |
8 |
8 |
|
| 592 |
18 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
8 |
10 |
18 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
8 |
0 |
8 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
8 |
8 |
|
| 593 |
0 |
| p++; |
| 594 |
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 |
|
| 595 |
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 |
|
| 596 |
|
| } |
| 597 |
18 |
| SQL_SPAN("esh-n", ts, p); |
| 598 |
18 |
| continue; |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
|
| /* identifier / keyword / builtin (case-insensitive) */ |
| 602 |
556 |
▶ | if (eshu_hl_isalpha_(c) || c == '_') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (eshu_hl_isalpha_(c) || c == '_') { |
320 |
236 |
556 |
| if (eshu_hl_isalpha_(c) || c == '_') { |
0 |
320 |
320 |
|
| 603 |
236 |
| const char *ts = p; |
| 604 |
1097 |
▶ | while (p < end && (eshu_hl_isalnum_(*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '_')) p++; |
1071 |
26 |
1097 |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '_')) p++; |
861 |
210 |
1071 |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '_')) p++; |
0 |
210 |
210 |
|
| 605 |
236 |
| size_t wlen = (size_t)(p - ts); |
| 606 |
236 |
| eshu_hl_flush(&out, plain, ts); |
| 607 |
236 |
| plain = p; |
| 608 |
236 |
▶ | if (eshu_hl_sql_kw_match(ts, wlen, eshu_hl_sql_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_sql_kw_match(ts, wlen, eshu_hl_sql_kw)) { |
120 |
116 |
236 |
|
| 609 |
120 |
| eshu_hl_span(&out, "esh-k", ts, p); |
| 610 |
116 |
▶ | } else if (eshu_hl_sql_kw_match(ts, wlen, eshu_hl_sql_bi)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_hl_sql_kw_match(ts, wlen, eshu_hl_sql_bi)) { |
20 |
96 |
116 |
|
| 611 |
20 |
| eshu_hl_span(&out, "esh-b", ts, p); |
| 612 |
|
| } else { |
| 613 |
96 |
| eshu_hl_write_html(&out, ts, wlen); |
| 614 |
|
| } |
| 615 |
236 |
| continue; |
| 616 |
|
| } |
| 617 |
|
| |
| 618 |
320 |
| p++; |
| 619 |
|
| } |
| 620 |
|
| |
| 621 |
42 |
▶ | if (in_dollar) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_dollar) { |
0 |
42 |
42 |
|
| 622 |
|
| /* unclosed dollar-string — flush as string */ |
| 623 |
0 |
| SQL_SPAN("esh-s", plain, end); |
| 624 |
|
| } else { |
| 625 |
42 |
| eshu_hl_flush(&out, plain, end); |
| 626 |
|
| } |
| 627 |
42 |
| eshu_buf_putc(&out, '\0'); |
| 628 |
42 |
| *out_len = out.len - 1; |
| 629 |
42 |
| return out.data; |
| 630 |
|
| |
| 631 |
|
| #undef SQL_SPAN |
| 632 |
|
| } |
| 633 |
|
| |
| 634 |
|
| #endif /* ESHU_SQL_H */ |