| 1 |
|
| /* |
| 2 |
|
| * eshu_js.h — JavaScript indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Tracks {} () [] nesting depth while handling JS-specific constructs: |
| 5 |
|
| * double-quoted strings, single-quoted strings, template literals with |
| 6 |
|
| * ${} interpolation, regex literals, line comments, and block comments. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #ifndef ESHU_JS_H |
| 10 |
|
| #define ESHU_JS_H |
| 11 |
|
| |
| 12 |
|
| #include "eshu.h" |
| 13 |
|
| |
| 14 |
|
| #define ESHU_JS_MAX_TMPL_DEPTH 16 |
| 15 |
|
| |
| 16 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 17 |
|
| * Scanner context — persists across lines |
| 18 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 19 |
|
| |
| 20 |
|
| typedef struct { |
| 21 |
|
| int depth; |
| 22 |
|
| int case_depth; /* depth at which last case/default was seen */ |
| 23 |
|
| int case_extra; /* 1 = add extra indent to case body lines */ |
| 24 |
|
| int suppressed_parens; /* unclosed ( whose depth was suppressed */ |
| 25 |
|
| int line_paren_delta; /* net ( opens on current line */ |
| 26 |
|
| int line_brace_delta; /* net { opens on current line */ |
| 27 |
|
| int line_bracket_delta;/* net [ opens on current line */ |
| 28 |
|
| enum eshu_state state; |
| 29 |
|
| int tmpl_depth; |
| 30 |
|
| int tmpl_brace_depth[ESHU_JS_MAX_TMPL_DEPTH]; |
| 31 |
|
| int can_regex; /* 1 if next / starts a regex */ |
| 32 |
|
| eshu_config_t cfg; |
| 33 |
|
| } eshu_js_ctx_t; |
| 34 |
|
| |
| 35 |
228 |
| static void eshu_js_ctx_init(eshu_js_ctx_t *ctx, const eshu_config_t *cfg) { |
| 36 |
228 |
| ctx->depth = 0; |
| 37 |
228 |
| ctx->case_depth = 0; |
| 38 |
228 |
| ctx->case_extra = 0; |
| 39 |
228 |
| ctx->suppressed_parens = 0; |
| 40 |
228 |
| ctx->line_paren_delta = 0; |
| 41 |
228 |
| ctx->line_brace_delta = 0; |
| 42 |
228 |
| ctx->line_bracket_delta = 0; |
| 43 |
228 |
| ctx->state = ESHU_CODE; |
| 44 |
228 |
| ctx->tmpl_depth = 0; |
| 45 |
228 |
| ctx->can_regex = 1; |
| 46 |
228 |
| ctx->cfg = *cfg; |
| 47 |
228 |
| } |
| 48 |
|
| |
| 49 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 50 |
|
| * Classify first non-ws char for pre-indent adjustment |
| 51 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 52 |
|
| |
| 53 |
1905 |
| static int eshu_js_is_closing(char c) { |
| 54 |
1905 |
▶ | return c == '}' || c == ')' || c == ']'; |
Branch: 3 branches
| Branch | True | False | Total |
| return c == '}' || c == ')' || c == ']'; |
1385 |
520 |
1905 |
| return c == '}' || c == ')' || c == ']'; |
1383 |
2 |
1385 |
| return c == '}' || c == ')' || c == ']'; |
4 |
1379 |
1383 |
|
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* Is this line a switch case/default label? |
| 58 |
|
| * Case labels stay at brace depth; body content gets +1 via case_extra. */ |
| 59 |
1379 |
| static int eshu_js_is_case_label(const char *content, int len) { |
| 60 |
|
| const char *p, *end; |
| 61 |
1379 |
| int is_kw = 0; |
| 62 |
|
| |
| 63 |
1379 |
▶ | if (len < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 2) return 0; |
1 |
1378 |
1379 |
|
| 64 |
|
| |
| 65 |
1378 |
▶ | if (len >= 5 && strncmp(content, "case ", 5) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 5 && strncmp(content, "case ", 5) == 0) |
1359 |
19 |
1378 |
| if (len >= 5 && strncmp(content, "case ", 5) == 0) |
11 |
1348 |
1359 |
|
| 66 |
11 |
| is_kw = 1; |
| 67 |
1367 |
▶ | else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
Branch: 3 branches
| Branch | True | False | Total |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
1311 |
56 |
1367 |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
4 |
1307 |
1311 |
| else if (len >= 7 && strncmp(content, "default", 7) == 0 && |
4 |
0 |
4 |
|
| 68 |
4 |
▶ | (len == 7 || content[7] == ':' || content[7] == ' ' || |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 || content[7] == ':' || content[7] == ' ' || |
0 |
4 |
4 |
| (len == 7 || content[7] == ':' || content[7] == ' ' || |
0 |
0 |
0 |
|
| 69 |
0 |
▶ | content[7] == '\t' || content[7] == '/')) |
Branch: 2 branches
| Branch | True | False | Total |
| content[7] == '\t' || content[7] == '/')) |
0 |
0 |
0 |
| content[7] == '\t' || content[7] == '/')) |
0 |
0 |
0 |
|
| 70 |
4 |
| is_kw = 1; |
| 71 |
|
| |
| 72 |
1378 |
▶ | if (!is_kw) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_kw) return 0; |
1363 |
15 |
1378 |
|
| 73 |
|
| |
| 74 |
15 |
| p = content; |
| 75 |
15 |
| end = content + len; |
| 76 |
231 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
216 |
15 |
231 |
|
| 77 |
216 |
▶ | if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
216 |
216 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
| if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } |
0 |
0 |
0 |
|
| 78 |
216 |
▶ | if (*p == '"' || *p == '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '"' || *p == '\'') { |
216 |
0 |
216 |
| if (*p == '"' || *p == '\'') { |
10 |
206 |
216 |
|
| 79 |
10 |
| char d = *p++; |
| 80 |
90 |
▶ | while (p < end && *p != d) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != d) { |
90 |
0 |
90 |
| while (p < end && *p != d) { |
80 |
10 |
90 |
|
| 81 |
80 |
▶ | if (*p == '\\') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\\') p++; |
0 |
80 |
80 |
|
| 82 |
80 |
| p++; |
| 83 |
|
| } |
| 84 |
|
| } |
| 85 |
216 |
| p++; |
| 86 |
|
| } |
| 87 |
15 |
▶ | while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
15 |
0 |
15 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
15 |
15 |
| while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; |
0 |
15 |
15 |
|
| 88 |
15 |
▶ | return end > content && *(end - 1) == ':'; |
Branch: 2 branches
| Branch | True | False | Total |
| return end > content && *(end - 1) == ':'; |
15 |
0 |
15 |
| return end > content && *(end - 1) == ':'; |
11 |
4 |
15 |
|
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 92 |
|
| * Scan a line for nesting changes |
| 93 |
|
| * |
| 94 |
|
| * Called AFTER the line has been emitted. Updates ctx->state |
| 95 |
|
| * and ctx->depth for the next line. |
| 96 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 97 |
|
| |
| 98 |
1921 |
| static void eshu_js_scan_line(eshu_js_ctx_t *ctx, |
| 99 |
|
| const char *p, const char *end) { |
| 100 |
21157 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
19248 |
1909 |
21157 |
|
| 101 |
19248 |
| char c = *p; |
| 102 |
|
| |
| 103 |
19248 |
| switch (ctx->state) { |
| 104 |
|
| |
| 105 |
16706 |
| case ESHU_CODE: |
| 106 |
16706 |
▶ | if (c == '{') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '{') { |
634 |
16072 |
16706 |
|
| 107 |
634 |
| ctx->depth++; |
| 108 |
634 |
| ctx->line_brace_delta++; |
| 109 |
634 |
| ctx->can_regex = 1; |
| 110 |
16072 |
▶ | } else if (c == '(' || c == '[') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == '(' || c == '[') { |
14984 |
1088 |
16072 |
| } else if (c == '(' || c == '[') { |
164 |
14820 |
14984 |
|
| 111 |
1252 |
| ctx->depth++; |
| 112 |
1252 |
▶ | if (c == '(') ctx->line_paren_delta++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '(') ctx->line_paren_delta++; |
1088 |
164 |
1252 |
|
| 113 |
164 |
| else ctx->line_bracket_delta++; |
| 114 |
1252 |
| ctx->can_regex = 1; |
| 115 |
14820 |
▶ | } else if (c == '}') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '}') { |
692 |
14128 |
14820 |
|
| 116 |
692 |
| ctx->depth--; |
| 117 |
692 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
692 |
692 |
|
| 118 |
692 |
| ctx->line_brace_delta--; |
| 119 |
|
| /* Check if this closes a template expression */ |
| 120 |
692 |
▶ | if (ctx->tmpl_depth > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tmpl_depth > 0 && |
61 |
631 |
692 |
|
| 121 |
61 |
▶ | ctx->depth == ctx->tmpl_brace_depth[ctx->tmpl_depth - 1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->depth == ctx->tmpl_brace_depth[ctx->tmpl_depth - 1]) { |
60 |
1 |
61 |
|
| 122 |
60 |
| ctx->tmpl_depth--; |
| 123 |
60 |
| ctx->state = ESHU_JS_TEMPLATE; |
| 124 |
60 |
| ctx->can_regex = 0; |
| 125 |
60 |
| break; |
| 126 |
|
| } |
| 127 |
632 |
| ctx->can_regex = 0; |
| 128 |
14128 |
▶ | } else if (c == ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == ']') { |
164 |
13964 |
14128 |
|
| 129 |
164 |
| ctx->depth--; |
| 130 |
164 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
164 |
164 |
|
| 131 |
164 |
| ctx->line_bracket_delta--; |
| 132 |
164 |
| ctx->can_regex = 0; |
| 133 |
13964 |
▶ | } else if (c == ')') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == ')') { |
1086 |
12878 |
13964 |
|
| 134 |
1086 |
▶ | if (ctx->line_paren_delta > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->line_paren_delta > 0) { |
1060 |
26 |
1086 |
|
| 135 |
1060 |
| ctx->line_paren_delta--; |
| 136 |
1060 |
| ctx->depth--; |
| 137 |
1060 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
1060 |
1060 |
|
| 138 |
26 |
▶ | } else if (ctx->suppressed_parens > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (ctx->suppressed_parens > 0) { |
22 |
4 |
26 |
|
| 139 |
22 |
| ctx->suppressed_parens--; |
| 140 |
|
| } else { |
| 141 |
4 |
| ctx->depth--; |
| 142 |
4 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
4 |
4 |
|
| 143 |
|
| } |
| 144 |
1086 |
| ctx->can_regex = 0; |
| 145 |
12878 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
11 |
12867 |
12878 |
|
| 146 |
11 |
| ctx->state = ESHU_STRING_DQ; |
| 147 |
12867 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
125 |
12742 |
12867 |
|
| 148 |
125 |
| ctx->state = ESHU_STRING_SQ; |
| 149 |
12742 |
▶ | } else if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '`') { |
53 |
12689 |
12742 |
|
| 150 |
53 |
| ctx->state = ESHU_JS_TEMPLATE; |
| 151 |
53 |
| ctx->can_regex = 0; |
| 152 |
12689 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
35 |
12654 |
12689 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
35 |
0 |
35 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
12 |
23 |
35 |
|
| 153 |
|
| /* line comment — skip rest of line */ |
| 154 |
12 |
| return; |
| 155 |
12677 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
23 |
12654 |
12677 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
23 |
0 |
23 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
6 |
17 |
23 |
|
| 156 |
6 |
| ctx->state = ESHU_COMMENT_BLOCK; |
| 157 |
6 |
| p++; /* skip '*' */ |
| 158 |
12671 |
▶ | } else if (c == '/' && ctx->can_regex) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == '/' && ctx->can_regex) { |
17 |
12654 |
12671 |
| } else if (c == '/' && ctx->can_regex) { |
10 |
7 |
17 |
|
| 159 |
|
| /* regex literal */ |
| 160 |
10 |
| ctx->state = ESHU_JS_REGEX; |
| 161 |
12661 |
▶ | } else if (c == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '/') { |
7 |
12654 |
12661 |
|
| 162 |
|
| /* division operator */ |
| 163 |
7 |
| ctx->can_regex = 1; |
| 164 |
18035 |
▶ | } else if (isalnum((unsigned char)c) || c == '_' || c == '$') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (isalnum((unsigned char)c) || c == '_' || c == '$') { |
7310 |
5344 |
12654 |
| } else if (isalnum((unsigned char)c) || c == '_' || c == '$') { |
7275 |
35 |
7310 |
| } else if (isalnum((unsigned char)c) || c == '_' || c == '$') { |
2 |
7273 |
7275 |
|
| 165 |
5381 |
| ctx->can_regex = 0; |
| 166 |
|
| /* skip rest of identifier/number */ |
| 167 |
24326 |
▶ | while (p + 1 < end && |
Branch: 1 branch
| Branch | True | False | Total |
| while (p + 1 < end && |
24313 |
13 |
24326 |
|
| 168 |
24313 |
▶ | (isalnum((unsigned char)*(p + 1)) || |
Branch: 1 branch
| Branch | True | False | Total |
| (isalnum((unsigned char)*(p + 1)) || |
18943 |
5370 |
24313 |
|
| 169 |
5370 |
▶ | *(p + 1) == '_' || *(p + 1) == '$')) |
Branch: 2 branches
| Branch | True | False | Total |
| *(p + 1) == '_' || *(p + 1) == '$')) |
2 |
5368 |
5370 |
| *(p + 1) == '_' || *(p + 1) == '$')) |
0 |
5368 |
5368 |
|
| 170 |
18945 |
| p++; |
| 171 |
7273 |
▶ | } else if (c == '+' || c == '-') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == '+' || c == '-') { |
7226 |
47 |
7273 |
| } else if (c == '+' || c == '-') { |
23 |
7203 |
7226 |
|
| 172 |
70 |
▶ | if (p + 1 < end && *(p + 1) == c) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p + 1 < end && *(p + 1) == c) { |
70 |
0 |
70 |
| if (p + 1 < end && *(p + 1) == c) { |
14 |
56 |
70 |
|
| 173 |
14 |
| p++; /* ++ or -- */ |
| 174 |
14 |
| ctx->can_regex = 0; |
| 175 |
|
| } else { |
| 176 |
56 |
| ctx->can_regex = 1; |
| 177 |
|
| } |
| 178 |
7203 |
▶ | } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
Branch: 5 branches
| Branch | True | False | Total |
| } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
6547 |
656 |
7203 |
| } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
6117 |
430 |
6547 |
| } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
5294 |
823 |
6117 |
| } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
5238 |
56 |
5294 |
| } else if (c == '=' || c == ',' || c == ';' || c == '!' || |
5238 |
0 |
5238 |
|
| 179 |
5238 |
▶ | c == '~' || c == '<' || c == '>' || c == '&' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '~' || c == '<' || c == '>' || c == '&' || |
5089 |
149 |
5238 |
| c == '~' || c == '<' || c == '>' || c == '&' || |
4800 |
289 |
5089 |
| c == '~' || c == '<' || c == '>' || c == '&' || |
4766 |
34 |
4800 |
| c == '~' || c == '<' || c == '>' || c == '&' || |
4708 |
58 |
4766 |
|
| 180 |
4708 |
▶ | c == '|' || c == '^' || c == '?' || c == ':' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '|' || c == '^' || c == '?' || c == ':' || |
4706 |
2 |
4708 |
| c == '|' || c == '^' || c == '?' || c == ':' || |
4627 |
79 |
4706 |
| c == '|' || c == '^' || c == '?' || c == ':' || |
4213 |
414 |
4627 |
| c == '|' || c == '^' || c == '?' || c == ':' || |
4213 |
0 |
4213 |
|
| 181 |
4213 |
▶ | c == '%' || c == '*') { |
Branch: 1 branch
| Branch | True | False | Total |
| c == '%' || c == '*') { |
26 |
4187 |
4213 |
|
| 182 |
3016 |
| ctx->can_regex = 1; |
| 183 |
|
| } |
| 184 |
|
| /* whitespace does not change can_regex */ |
| 185 |
16634 |
| break; |
| 186 |
|
| |
| 187 |
182 |
| case ESHU_STRING_DQ: |
| 188 |
182 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
5 |
177 |
182 |
| if (c == '\\' && p + 1 < end) { |
5 |
0 |
5 |
|
| 189 |
5 |
| p++; /* skip escaped char */ |
| 190 |
177 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
11 |
166 |
177 |
|
| 191 |
11 |
| ctx->state = ESHU_CODE; |
| 192 |
11 |
| ctx->can_regex = 0; |
| 193 |
|
| } |
| 194 |
182 |
| break; |
| 195 |
|
| |
| 196 |
1281 |
| case ESHU_STRING_SQ: |
| 197 |
1281 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
2 |
1279 |
1281 |
| if (c == '\\' && p + 1 < end) { |
2 |
0 |
2 |
|
| 198 |
2 |
| p++; |
| 199 |
1279 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
123 |
1156 |
1279 |
|
| 200 |
123 |
| ctx->state = ESHU_CODE; |
| 201 |
123 |
| ctx->can_regex = 0; |
| 202 |
|
| } |
| 203 |
1281 |
| break; |
| 204 |
|
| |
| 205 |
804 |
| case ESHU_JS_TEMPLATE: |
| 206 |
804 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
0 |
804 |
804 |
| if (c == '\\' && p + 1 < end) { |
0 |
0 |
0 |
|
| 207 |
0 |
| p++; /* skip escaped char */ |
| 208 |
804 |
▶ | } else if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '`') { |
53 |
751 |
804 |
|
| 209 |
53 |
| ctx->state = ESHU_CODE; |
| 210 |
53 |
| ctx->can_regex = 0; |
| 211 |
751 |
▶ | } else if (c == '$' && p + 1 < end && *(p + 1) == '{') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '$' && p + 1 < end && *(p + 1) == '{') { |
61 |
690 |
751 |
| } else if (c == '$' && p + 1 < end && *(p + 1) == '{') { |
61 |
0 |
61 |
| } else if (c == '$' && p + 1 < end && *(p + 1) == '{') { |
60 |
1 |
61 |
|
| 212 |
60 |
| p++; /* skip '{' */ |
| 213 |
60 |
▶ | if (ctx->tmpl_depth < ESHU_JS_MAX_TMPL_DEPTH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tmpl_depth < ESHU_JS_MAX_TMPL_DEPTH) { |
60 |
0 |
60 |
|
| 214 |
60 |
| ctx->tmpl_brace_depth[ctx->tmpl_depth] = ctx->depth; |
| 215 |
60 |
| ctx->tmpl_depth++; |
| 216 |
|
| } |
| 217 |
60 |
| ctx->depth++; |
| 218 |
60 |
| ctx->state = ESHU_CODE; |
| 219 |
60 |
| ctx->can_regex = 1; |
| 220 |
|
| } |
| 221 |
804 |
| break; |
| 222 |
|
| |
| 223 |
63 |
| case ESHU_JS_REGEX: |
| 224 |
63 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
12 |
51 |
63 |
| if (c == '\\' && p + 1 < end) { |
12 |
0 |
12 |
|
| 225 |
12 |
| p++; |
| 226 |
51 |
▶ | } else if (c == '[') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '[') { |
3 |
48 |
51 |
|
| 227 |
3 |
| ctx->state = ESHU_JS_REGEX_CLASS; |
| 228 |
48 |
▶ | } else if (c == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '/') { |
10 |
38 |
48 |
|
| 229 |
|
| /* end of regex — skip flags */ |
| 230 |
16 |
▶ | while (p + 1 < end && isalpha((unsigned char)*(p + 1))) |
Branch: 2 branches
| Branch | True | False | Total |
| while (p + 1 < end && isalpha((unsigned char)*(p + 1))) |
16 |
0 |
16 |
| while (p + 1 < end && isalpha((unsigned char)*(p + 1))) |
6 |
10 |
16 |
|
| 231 |
6 |
| p++; |
| 232 |
10 |
| ctx->state = ESHU_CODE; |
| 233 |
10 |
| ctx->can_regex = 0; |
| 234 |
|
| } |
| 235 |
63 |
| break; |
| 236 |
|
| |
| 237 |
15 |
| case ESHU_JS_REGEX_CLASS: |
| 238 |
15 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
2 |
13 |
15 |
| if (c == '\\' && p + 1 < end) { |
2 |
0 |
2 |
|
| 239 |
2 |
| p++; |
| 240 |
13 |
▶ | } else if (c == ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == ']') { |
3 |
10 |
13 |
|
| 241 |
3 |
| ctx->state = ESHU_JS_REGEX; |
| 242 |
|
| } |
| 243 |
15 |
| break; |
| 244 |
|
| |
| 245 |
197 |
| case ESHU_COMMENT_BLOCK: |
| 246 |
197 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
16 |
181 |
197 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
15 |
1 |
16 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
6 |
9 |
15 |
|
| 247 |
6 |
| ctx->state = ESHU_CODE; |
| 248 |
6 |
| ctx->can_regex = 1; |
| 249 |
6 |
| p++; |
| 250 |
|
| } |
| 251 |
197 |
| break; |
| 252 |
|
| |
| 253 |
0 |
| default: |
| 254 |
0 |
| break; |
| 255 |
|
| } |
| 256 |
19236 |
| p++; |
| 257 |
|
| } |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 261 |
|
| * Process a single line — decide indent, emit, scan |
| 262 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 263 |
|
| |
| 264 |
2032 |
| static void eshu_js_process_line(eshu_js_ctx_t *ctx, eshu_buf_t *out, |
| 265 |
|
| const char *line_start, const char *eol) { |
| 266 |
2032 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 267 |
|
| int line_len; |
| 268 |
|
| int indent_depth; |
| 269 |
|
| |
| 270 |
|
| /* empty line — preserve it */ |
| 271 |
2032 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
111 |
1921 |
2032 |
|
| 272 |
111 |
| eshu_buf_putc(out, '\n'); |
| 273 |
111 |
| return; |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
1921 |
| line_len = (int)(eol - content); |
| 277 |
|
| |
| 278 |
|
| /* Template literal continuation: pass through verbatim |
| 279 |
|
| * (template literal whitespace is significant) */ |
| 280 |
1921 |
▶ | if (ctx->state == ESHU_JS_TEMPLATE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_JS_TEMPLATE) { |
3 |
1918 |
1921 |
|
| 281 |
3 |
| eshu_buf_write(out, line_start, (size_t)(eol - line_start)); |
| 282 |
3 |
▶ | if (*eol == '\n') eshu_buf_putc(out, '\n'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*eol == '\n') eshu_buf_putc(out, '\n'); |
3 |
0 |
3 |
|
| 283 |
3 |
| eshu_js_scan_line(ctx, line_start, eol); |
| 284 |
3 |
| return; |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| /* Block comment continuation */ |
| 288 |
1918 |
▶ | if (ctx->state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_COMMENT_BLOCK) { |
13 |
1905 |
1918 |
|
| 289 |
13 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 290 |
13 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 291 |
13 |
| eshu_buf_putc(out, '\n'); |
| 292 |
13 |
| eshu_js_scan_line(ctx, content, eol); |
| 293 |
13 |
| return; |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
|
| /* Regex spanning lines (rare but possible) */ |
| 297 |
1905 |
▶ | if (ctx->state == ESHU_JS_REGEX || |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_JS_REGEX || |
1905 |
0 |
1905 |
|
| 298 |
1905 |
▶ | ctx->state == ESHU_JS_REGEX_CLASS) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->state == ESHU_JS_REGEX_CLASS) { |
0 |
1905 |
1905 |
|
| 299 |
0 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 300 |
0 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 301 |
0 |
| eshu_buf_putc(out, '\n'); |
| 302 |
0 |
| eshu_js_scan_line(ctx, content, eol); |
| 303 |
0 |
| return; |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
|
| /* Normal code line */ |
| 307 |
1905 |
| indent_depth = ctx->depth; |
| 308 |
|
| |
| 309 |
|
| /* Leaving a case block: clear case_extra once depth drops below case_depth */ |
| 310 |
1905 |
▶ | if (ctx->case_extra && ctx->depth < ctx->case_depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->case_extra && ctx->depth < ctx->case_depth) |
35 |
1870 |
1905 |
| if (ctx->case_extra && ctx->depth < ctx->case_depth) |
3 |
32 |
35 |
|
| 311 |
3 |
| ctx->case_extra = 0; |
| 312 |
|
| |
| 313 |
1905 |
▶ | if (*content == ')' && ctx->suppressed_parens > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*content == ')' && ctx->suppressed_parens > 0) { |
2 |
1903 |
1905 |
| if (*content == ')' && ctx->suppressed_parens > 0) { |
0 |
2 |
2 |
|
| 314 |
|
| /* suppressed paren — don't dedent */ |
| 315 |
1905 |
▶ | } else if (eshu_js_is_closing(*content)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_js_is_closing(*content)) { |
526 |
1379 |
1905 |
|
| 316 |
526 |
| indent_depth--; |
| 317 |
526 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
526 |
526 |
|
| 318 |
|
| /* still inside a case block at deeper nesting */ |
| 319 |
526 |
▶ | if (ctx->case_extra && ctx->depth > ctx->case_depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->case_extra && ctx->depth > ctx->case_depth) |
6 |
520 |
526 |
| if (ctx->case_extra && ctx->depth > ctx->case_depth) |
2 |
4 |
6 |
|
| 320 |
2 |
| indent_depth++; |
| 321 |
1379 |
▶ | } else if (eshu_js_is_case_label(content, line_len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_js_is_case_label(content, line_len)) { |
11 |
1368 |
1379 |
|
| 322 |
|
| /* case/default label: stays at brace depth; body lines get +1 */ |
| 323 |
11 |
| ctx->case_depth = ctx->depth; |
| 324 |
11 |
| ctx->case_extra = 1; |
| 325 |
1368 |
▶ | } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
19 |
1349 |
1368 |
| } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { |
19 |
0 |
19 |
|
| 326 |
|
| /* body line inside a case block: add one extra indent level */ |
| 327 |
19 |
| indent_depth++; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
1905 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 331 |
1905 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 332 |
1905 |
| eshu_buf_putc(out, '\n'); |
| 333 |
|
| |
| 334 |
|
| /* Scan for nesting changes */ |
| 335 |
1905 |
| ctx->line_paren_delta = 0; |
| 336 |
1905 |
| ctx->line_brace_delta = 0; |
| 337 |
1905 |
| ctx->line_bracket_delta = 0; |
| 338 |
1905 |
| eshu_js_scan_line(ctx, content, eol); |
| 339 |
|
| |
| 340 |
|
| /* Suppress unmatched ( when { or [ also opened on the same line. |
| 341 |
|
| * e.g. foo((x) => { or foo((x) => [ — the outer ( is just call syntax, |
| 342 |
|
| * only the { or [ should add structural indentation. */ |
| 343 |
1905 |
▶ | if (ctx->line_paren_delta > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->line_paren_delta > 0 && |
28 |
1877 |
1905 |
|
| 344 |
28 |
▶ | (ctx->line_brace_delta > 0 || ctx->line_bracket_delta > 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| (ctx->line_brace_delta > 0 || ctx->line_bracket_delta > 0)) { |
6 |
22 |
28 |
| (ctx->line_brace_delta > 0 || ctx->line_bracket_delta > 0)) { |
0 |
6 |
6 |
|
| 345 |
22 |
| ctx->depth -= ctx->line_paren_delta; |
| 346 |
22 |
| ctx->suppressed_parens += ctx->line_paren_delta; |
| 347 |
|
| } |
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 351 |
|
| * Public API — indent a JavaScript source string |
| 352 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 353 |
|
| |
| 354 |
153 |
| static char * eshu_indent_js(const char *src, size_t src_len, |
| 355 |
|
| const eshu_config_t *cfg, size_t *out_len) { |
| 356 |
|
| eshu_js_ctx_t ctx; |
| 357 |
|
| eshu_buf_t out; |
| 358 |
153 |
| const char *p = src; |
| 359 |
153 |
| const char *end = src + src_len; |
| 360 |
|
| char *result; |
| 361 |
|
| |
| 362 |
153 |
| eshu_js_ctx_init(&ctx, cfg); |
| 363 |
153 |
| eshu_buf_init(&out, src_len + 256); |
| 364 |
|
| |
| 365 |
|
| { |
| 366 |
153 |
| int line_num = 1; |
| 367 |
1564 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
1411 |
153 |
1564 |
|
| 368 |
1411 |
| const char *eol = eshu_find_eol(p); |
| 369 |
|
| |
| 370 |
1411 |
▶ | if (eshu_in_range(cfg, line_num)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(cfg, line_num)) { |
1406 |
5 |
1411 |
|
| 371 |
1406 |
| eshu_js_process_line(&ctx, &out, p, eol); |
| 372 |
|
| } else { |
| 373 |
|
| /* Outside range: scan for state, emit verbatim */ |
| 374 |
5 |
| size_t saved = out.len; |
| 375 |
5 |
| eshu_js_process_line(&ctx, &out, p, eol); |
| 376 |
5 |
| out.len = saved; |
| 377 |
5 |
| eshu_buf_write_trimmed(&out, p, (int)(eol - p)); |
| 378 |
5 |
| eshu_buf_putc(&out, '\n'); |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
1411 |
| p = eol; |
| 382 |
1411 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
1411 |
0 |
1411 |
|
| 383 |
1411 |
| line_num++; |
| 384 |
|
| } |
| 385 |
|
| } |
| 386 |
|
| |
| 387 |
|
| /* NUL-terminate */ |
| 388 |
153 |
| eshu_buf_putc(&out, '\0'); |
| 389 |
153 |
| out.len--; |
| 390 |
|
| |
| 391 |
153 |
| *out_len = out.len; |
| 392 |
153 |
| result = out.data; |
| 393 |
153 |
| return result; |
| 394 |
|
| } |
| 395 |
|
| |
| 396 |
|
| |
| 397 |
|
| #include "eshu_hl_util.h" |
| 398 |
|
| |
| 399 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 400 |
|
| * JavaScript keyword list |
| 401 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 402 |
|
| |
| 403 |
|
| static const char * const eshu_hl_js_kw[] = { |
| 404 |
|
| "async", "await", |
| 405 |
|
| "break", |
| 406 |
|
| "case", "catch", "class", "const", "continue", |
| 407 |
|
| "debugger", "default", "delete", "do", |
| 408 |
|
| "else", "export", "extends", |
| 409 |
|
| "false", "finally", "for", "function", |
| 410 |
|
| "get", |
| 411 |
|
| "if", "import", "in", "instanceof", |
| 412 |
|
| "let", |
| 413 |
|
| "new", "null", |
| 414 |
|
| "of", |
| 415 |
|
| "return", |
| 416 |
|
| "set", "static", "super", "switch", |
| 417 |
|
| "this", "throw", "true", "try", "typeof", |
| 418 |
|
| "undefined", |
| 419 |
|
| "var", "void", |
| 420 |
|
| "while", "with", |
| 421 |
|
| "yield", |
| 422 |
|
| /* built-ins often treated as keywords */ |
| 423 |
|
| "Infinity", "NaN", |
| 424 |
|
| NULL |
| 425 |
|
| }; |
| 426 |
|
| |
| 427 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 428 |
|
| * JavaScript highlighter |
| 429 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 430 |
|
| |
| 431 |
55 |
| static char *eshu_highlight_js(const char *src, size_t src_len, size_t *out_len) { |
| 432 |
|
| eshu_buf_t out; |
| 433 |
55 |
| const char *p = src; |
| 434 |
55 |
| const char *end = src + src_len; |
| 435 |
55 |
| const char *plain = p; |
| 436 |
55 |
| int last_val = 0; |
| 437 |
55 |
| int tmpl_depth = 0; /* template literal nesting */ |
| 438 |
|
| |
| 439 |
55 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 440 |
|
| |
| 441 |
|
| #define JS_SPAN(cls, ts, te) do { \ |
| 442 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 443 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 444 |
|
| p = (te); plain = p; \ |
| 445 |
|
| } while(0) |
| 446 |
|
| |
| 447 |
584 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
529 |
55 |
584 |
|
| 448 |
529 |
| char c = *p; |
| 449 |
|
| |
| 450 |
|
| /* line comment */ |
| 451 |
529 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
8 |
521 |
529 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
8 |
0 |
8 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
6 |
8 |
|
| 452 |
2 |
| const char *ts = p; p += 2; |
| 453 |
31 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
31 |
0 |
31 |
| while (p < end && *p != '\n') p++; |
29 |
2 |
31 |
|
| 454 |
2 |
| JS_SPAN("esh-c", ts, p); |
| 455 |
2 |
| last_val = 0; continue; |
| 456 |
|
| } |
| 457 |
|
| |
| 458 |
|
| /* block comment */ |
| 459 |
527 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
6 |
521 |
527 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
6 |
0 |
6 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
2 |
4 |
6 |
|
| 460 |
2 |
| const char *ts = p; p += 2; |
| 461 |
37 |
▶ | while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
37 |
0 |
37 |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
35 |
2 |
37 |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
0 |
2 |
2 |
|
| 462 |
2 |
▶ | if (p + 1 < end) p += 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 1 < end) p += 2; |
2 |
0 |
2 |
|
| 463 |
2 |
| JS_SPAN("esh-c", ts, p); |
| 464 |
2 |
| last_val = 0; continue; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
|
| /* template literal */ |
| 468 |
525 |
▶ | if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') { |
2 |
523 |
525 |
|
| 469 |
2 |
| const char *ts = p++; |
| 470 |
2 |
| tmpl_depth++; |
| 471 |
38 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
38 |
0 |
38 |
|
| 472 |
38 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
38 |
38 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 473 |
38 |
▶ | if (*p == '`') { p++; tmpl_depth--; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '`') { p++; tmpl_depth--; break; } |
2 |
36 |
38 |
|
| 474 |
|
| /* ${...} we skip without sub-highlighting for now */ |
| 475 |
36 |
| p++; |
| 476 |
|
| } |
| 477 |
2 |
| JS_SPAN("esh-s", ts, p); |
| 478 |
2 |
| last_val = 1; continue; |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
|
| /* string */ |
| 482 |
523 |
▶ | if (c == '"' || c == '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '"' || c == '\'') { |
518 |
5 |
523 |
| if (c == '"' || c == '\'') { |
3 |
515 |
518 |
|
| 483 |
8 |
| const char *ts = p++; |
| 484 |
61 |
▶ | while (p < end && *p != c) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != c) { |
61 |
0 |
61 |
| while (p < end && *p != c) { |
53 |
8 |
61 |
|
| 485 |
53 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
53 |
53 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 486 |
53 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
53 |
53 |
|
| 487 |
53 |
| p++; |
| 488 |
|
| } |
| 489 |
8 |
▶ | if (p < end && *p == c) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == c) p++; |
8 |
0 |
8 |
| if (p < end && *p == c) p++; |
8 |
0 |
8 |
|
| 490 |
8 |
| JS_SPAN("esh-s", ts, p); |
| 491 |
8 |
| last_val = 1; continue; |
| 492 |
|
| } |
| 493 |
|
| |
| 494 |
|
| /* regex: / not preceded by rvalue and not // */ |
| 495 |
515 |
▶ | if (c == '/' && !last_val && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '/' && !last_val && |
4 |
511 |
515 |
| if (c == '/' && !last_val && |
3 |
1 |
4 |
|
| 496 |
3 |
▶ | !(p + 1 < end && *(p + 1) == '/') && |
Branch: 2 branches
| Branch | True | False | Total |
| !(p + 1 < end && *(p + 1) == '/') && |
3 |
0 |
3 |
| !(p + 1 < end && *(p + 1) == '/') && |
3 |
0 |
3 |
|
| 497 |
3 |
▶ | !(p + 1 < end && *(p + 1) == '*')) { |
Branch: 2 branches
| Branch | True | False | Total |
| !(p + 1 < end && *(p + 1) == '*')) { |
3 |
0 |
3 |
| !(p + 1 < end && *(p + 1) == '*')) { |
3 |
0 |
3 |
|
| 498 |
3 |
| const char *ts = p++; |
| 499 |
3 |
| int in_class = 0; |
| 500 |
15 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
15 |
0 |
15 |
|
| 501 |
15 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
15 |
15 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 502 |
15 |
▶ | if (*p == '[') { in_class = 1; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '[') { in_class = 1; p++; continue; } |
1 |
14 |
15 |
|
| 503 |
14 |
▶ | if (*p == ']') { in_class = 0; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ']') { in_class = 0; p++; continue; } |
1 |
13 |
14 |
|
| 504 |
13 |
▶ | if (!in_class && *p == '/') { p++; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!in_class && *p == '/') { p++; break; } |
10 |
3 |
13 |
| if (!in_class && *p == '/') { p++; break; } |
3 |
7 |
10 |
|
| 505 |
10 |
▶ | if (*p == '\n') { p = ts + 1; goto js_not_regex; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') { p = ts + 1; goto js_not_regex; } |
0 |
10 |
10 |
|
| 506 |
10 |
| p++; |
| 507 |
|
| } |
| 508 |
6 |
▶ | while (p < end && isalpha((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && isalpha((unsigned char)*p)) p++; |
5 |
1 |
6 |
| while (p < end && isalpha((unsigned char)*p)) p++; |
3 |
2 |
5 |
|
| 509 |
3 |
| JS_SPAN("esh-r", ts, p); |
| 510 |
3 |
| last_val = 1; continue; |
| 511 |
0 |
| js_not_regex:; |
| 512 |
|
| } |
| 513 |
|
| |
| 514 |
|
| /* number */ |
| 515 |
512 |
▶ | if (isdigit((unsigned char)c) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (isdigit((unsigned char)c) || |
490 |
22 |
512 |
| if (isdigit((unsigned char)c) || |
4 |
486 |
490 |
|
| 516 |
4 |
▶ | (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
Branch: 2 branches
| Branch | True | False | Total |
| (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
4 |
0 |
4 |
| (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
0 |
4 |
4 |
|
| 517 |
22 |
| const char *ts = p; |
| 518 |
22 |
▶ | if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { |
5 |
17 |
22 |
| if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { |
5 |
0 |
5 |
| if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { |
4 |
1 |
5 |
| if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { |
0 |
4 |
4 |
|
| 519 |
3 |
▶ | p += 2; while (p < end && isxdigit((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| p += 2; while (p < end && isxdigit((unsigned char)*p)) p++; |
2 |
1 |
3 |
| p += 2; while (p < end && isxdigit((unsigned char)*p)) p++; |
2 |
0 |
2 |
|
| 520 |
21 |
▶ | } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { |
4 |
17 |
21 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { |
4 |
0 |
4 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { |
3 |
1 |
4 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { |
0 |
3 |
3 |
|
| 521 |
5 |
▶ | p += 2; while (p < end && (*p == '0' || *p == '1')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| p += 2; while (p < end && (*p == '0' || *p == '1')) p++; |
4 |
1 |
5 |
| p += 2; while (p < end && (*p == '0' || *p == '1')) p++; |
2 |
2 |
4 |
| p += 2; while (p < end && (*p == '0' || *p == '1')) p++; |
2 |
0 |
2 |
|
| 522 |
20 |
▶ | } else if (c == '0' && p + 1 < end && (*(p + 1) == 'o' || *(p + 1) == 'O')) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'o' || *(p + 1) == 'O')) { |
3 |
17 |
20 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'o' || *(p + 1) == 'O')) { |
3 |
0 |
3 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'o' || *(p + 1) == 'O')) { |
2 |
1 |
3 |
| } else if (c == '0' && p + 1 < end && (*(p + 1) == 'o' || *(p + 1) == 'O')) { |
0 |
2 |
2 |
|
| 523 |
4 |
▶ | p += 2; while (p < end && *p >= '0' && *p <= '7') p++; |
Branch: 3 branches
| Branch | True | False | Total |
| p += 2; while (p < end && *p >= '0' && *p <= '7') p++; |
3 |
1 |
4 |
| p += 2; while (p < end && *p >= '0' && *p <= '7') p++; |
3 |
0 |
3 |
| p += 2; while (p < end && *p >= '0' && *p <= '7') p++; |
3 |
0 |
3 |
|
| 524 |
|
| } else { |
| 525 |
50 |
▶ | 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++; |
49 |
1 |
50 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
29 |
20 |
49 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
18 |
20 |
|
| 526 |
19 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
18 |
1 |
19 |
| if (p < end && *p == '.') { |
3 |
15 |
18 |
|
| 527 |
7 |
▶ | p++; while (p < end && isdigit((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| p++; while (p < end && isdigit((unsigned char)*p)) p++; |
6 |
1 |
7 |
| p++; while (p < end && isdigit((unsigned char)*p)) p++; |
4 |
2 |
6 |
|
| 528 |
|
| } |
| 529 |
19 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
17 |
2 |
19 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
15 |
2 |
17 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
15 |
15 |
|
| 530 |
2 |
▶ | p++; if (p < end && (*p == '+' || *p == '-')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| p++; if (p < end && (*p == '+' || *p == '-')) p++; |
2 |
0 |
2 |
| p++; if (p < end && (*p == '+' || *p == '-')) p++; |
1 |
1 |
2 |
| p++; if (p < end && (*p == '+' || *p == '-')) p++; |
1 |
0 |
1 |
|
| 531 |
4 |
▶ | while (p < end && isdigit((unsigned char)*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && isdigit((unsigned char)*p)) p++; |
2 |
2 |
4 |
| while (p < end && isdigit((unsigned char)*p)) p++; |
2 |
0 |
2 |
|
| 532 |
|
| } |
| 533 |
|
| } |
| 534 |
|
| /* bigint suffix */ |
| 535 |
22 |
▶ | if (p < end && *p == 'n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == 'n') p++; |
15 |
7 |
22 |
| if (p < end && *p == 'n') p++; |
1 |
14 |
15 |
|
| 536 |
22 |
| JS_SPAN("esh-n", ts, p); |
| 537 |
22 |
| last_val = 1; continue; |
| 538 |
|
| } |
| 539 |
|
| |
| 540 |
|
| /* identifier or keyword */ |
| 541 |
490 |
▶ | if (eshu_hl_isalpha_(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_isalpha_(c)) { |
136 |
354 |
490 |
|
| 542 |
136 |
| const char *ts = p; |
| 543 |
641 |
▶ | while (p < end && eshu_hl_isalnum_(*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
638 |
3 |
641 |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
505 |
133 |
638 |
|
| 544 |
136 |
| eshu_hl_flush(&out, plain, ts); |
| 545 |
136 |
| plain = p; |
| 546 |
136 |
| size_t ilen = (size_t)(p - ts); |
| 547 |
136 |
▶ | if (eshu_hl_kw(ts, ilen, eshu_hl_js_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, ilen, eshu_hl_js_kw)) { |
73 |
63 |
136 |
|
| 548 |
73 |
| eshu_hl_span(&out, "esh-k", ts, p); |
| 549 |
73 |
| last_val = 0; |
| 550 |
|
| } else { |
| 551 |
63 |
| eshu_hl_write_html(&out, ts, ilen); |
| 552 |
63 |
| last_val = 1; |
| 553 |
|
| } |
| 554 |
136 |
| continue; |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
|
| /* rvalue context tracking */ |
| 558 |
354 |
▶ | if (c == ')' || c == ']') last_val = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == ')' || c == ']') last_val = 1; |
330 |
24 |
354 |
| if (c == ')' || c == ']') last_val = 1; |
0 |
330 |
330 |
|
| 559 |
330 |
▶ | else if (c == '(' || c == '[' || c == ',' || c == ';' || |
Branch: 5 branches
| Branch | True | False | Total |
| else if (c == '(' || c == '[' || c == ',' || c == ';' || |
306 |
24 |
330 |
| else if (c == '(' || c == '[' || c == ',' || c == ';' || |
306 |
0 |
306 |
| else if (c == '(' || c == '[' || c == ',' || c == ';' || |
306 |
0 |
306 |
| else if (c == '(' || c == '[' || c == ',' || c == ';' || |
273 |
33 |
306 |
| else if (c == '(' || c == '[' || c == ',' || c == ';' || |
257 |
16 |
273 |
|
| 560 |
257 |
▶ | c == '=' || c == '!' || c == ':' || c == '?' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '=' || c == '!' || c == ':' || c == '?' || |
256 |
1 |
257 |
| c == '=' || c == '!' || c == ':' || c == '?' || |
255 |
1 |
256 |
| c == '=' || c == '!' || c == ':' || c == '?' || |
255 |
0 |
255 |
| c == '=' || c == '!' || c == ':' || c == '?' || |
232 |
23 |
255 |
|
| 561 |
232 |
▶ | c == '{' || c == '&' || c == '|' || c == '^' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '{' || c == '&' || c == '|' || c == '^' || |
230 |
2 |
232 |
| c == '{' || c == '&' || c == '|' || c == '^' || |
230 |
0 |
230 |
| c == '{' || c == '&' || c == '|' || c == '^' || |
230 |
0 |
230 |
| c == '{' || c == '&' || c == '|' || c == '^' || |
225 |
5 |
230 |
|
| 562 |
225 |
▶ | c == '+' || c == '-' || c == '*' || c == '<' || c == '>') |
Branch: 4 branches
| Branch | True | False | Total |
| c == '+' || c == '-' || c == '*' || c == '<' || c == '>') |
224 |
1 |
225 |
| c == '+' || c == '-' || c == '*' || c == '<' || c == '>') |
223 |
1 |
224 |
| c == '+' || c == '-' || c == '*' || c == '<' || c == '>') |
220 |
3 |
223 |
| c == '+' || c == '-' || c == '*' || c == '<' || c == '>') |
2 |
218 |
220 |
|
| 563 |
112 |
| last_val = 0; |
| 564 |
354 |
▶ | if (c == '\n') last_val = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\n') last_val = 0; |
8 |
346 |
354 |
|
| 565 |
354 |
| p++; |
| 566 |
|
| } |
| 567 |
|
| |
| 568 |
55 |
| eshu_hl_flush(&out, plain, end); |
| 569 |
55 |
| eshu_buf_putc(&out, '\0'); |
| 570 |
55 |
| *out_len = out.len - 1; |
| 571 |
55 |
| return out.data; |
| 572 |
|
| |
| 573 |
|
| #undef JS_SPAN |
| 574 |
|
| } |
| 575 |
|
| |
| 576 |
|
| #endif /* ESHU_JS_H */ |