| 1 |
|
| /* |
| 2 |
|
| * eshu_go.h — Go language indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Go is brace-based like C but without preprocessor directives. |
| 5 |
|
| * Handles {}, (), [] nesting; interpreted strings "..."; raw string |
| 6 |
|
| * literals `...` (backtick, can span lines); rune literals '.' ; |
| 7 |
|
| * and // / block comments. No case_extra needed — Go switch/case |
| 8 |
|
| * keeps case labels at the same depth as the braces. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #ifndef ESHU_GO_H |
| 12 |
|
| #define ESHU_GO_H |
| 13 |
|
| |
| 14 |
|
| #include "eshu.h" |
| 15 |
|
| |
| 16 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 17 |
|
| * Scanner context — persists across lines |
| 18 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 19 |
|
| |
| 20 |
|
| typedef struct { |
| 21 |
|
| int depth; |
| 22 |
|
| enum eshu_state state; |
| 23 |
|
| eshu_config_t cfg; |
| 24 |
|
| } eshu_go_ctx_t; |
| 25 |
|
| |
| 26 |
89 |
| static void eshu_go_ctx_init(eshu_go_ctx_t *ctx, const eshu_config_t *cfg) { |
| 27 |
89 |
| ctx->depth = 0; |
| 28 |
89 |
| ctx->state = ESHU_CODE; |
| 29 |
89 |
| ctx->cfg = *cfg; |
| 30 |
89 |
| } |
| 31 |
|
| |
| 32 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 33 |
|
| * Helpers |
| 34 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 35 |
|
| |
| 36 |
|
| /* Go case/default labels sit at switch-brace level (depth-1 vs body). |
| 37 |
|
| * Returns 1 if trimmed line content is a case or default clause header. */ |
| 38 |
551 |
| static int eshu_go_is_case_label(const char *content, int len) { |
| 39 |
551 |
| const char *end = content + len; |
| 40 |
551 |
▶ | 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--; |
551 |
0 |
551 |
| while (end > content && (*(end-1) == ' ' || *(end-1) == '\t')) end--; |
0 |
551 |
551 |
| while (end > content && (*(end-1) == ' ' || *(end-1) == '\t')) end--; |
0 |
551 |
551 |
|
| 41 |
551 |
| len = (int)(end - content); |
| 42 |
551 |
▶ | if (len < 5) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 5) return 0; |
8 |
543 |
551 |
|
| 43 |
543 |
▶ | if (end == content || *(end-1) != ':') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (end == content || *(end-1) != ':') return 0; |
543 |
0 |
543 |
| if (end == content || *(end-1) != ':') return 0; |
514 |
29 |
543 |
|
| 44 |
29 |
▶ | if (strncmp(content, "case ", 5) == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(content, "case ", 5) == 0) return 1; |
21 |
8 |
29 |
|
| 45 |
8 |
▶ | if (len >= 7 && strncmp(content, "default", 7) == 0 && |
Branch: 3 branches
| Branch | True | False | Total |
| if (len >= 7 && strncmp(content, "default", 7) == 0 && |
8 |
0 |
8 |
| if (len >= 7 && strncmp(content, "default", 7) == 0 && |
8 |
0 |
8 |
| if (len >= 7 && strncmp(content, "default", 7) == 0 && |
8 |
0 |
8 |
|
| 46 |
8 |
▶ | (len == 7 || content[7] == ':' || content[7] == ' ')) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| (len == 7 || content[7] == ':' || content[7] == ' ')) return 1; |
0 |
8 |
8 |
| (len == 7 || content[7] == ':' || content[7] == ' ')) return 1; |
0 |
0 |
0 |
|
| 47 |
0 |
| return 0; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 51 |
|
| * Scan a line for nesting changes — called after emit |
| 52 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 53 |
|
| |
| 54 |
749 |
| static void eshu_go_scan_line(eshu_go_ctx_t *ctx, const char *p, const char *end) { |
| 55 |
13169 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
12423 |
746 |
13169 |
|
| 56 |
12423 |
| char c = *p; |
| 57 |
|
| |
| 58 |
12423 |
| switch (ctx->state) { |
| 59 |
11700 |
| case ESHU_CODE: |
| 60 |
11700 |
▶ | if (c == '{' || c == '(' || c == '[') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '{' || c == '(' || c == '[') { |
11442 |
258 |
11700 |
| if (c == '{' || c == '(' || c == '[') { |
11089 |
353 |
11442 |
| if (c == '{' || c == '(' || c == '[') { |
102 |
10987 |
11089 |
|
| 61 |
713 |
| ctx->depth++; |
| 62 |
10987 |
▶ | } else if (c == '}' || c == ')' || c == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '}' || c == ')' || c == ']') { |
10729 |
258 |
10987 |
| } else if (c == '}' || c == ')' || c == ']') { |
10376 |
353 |
10729 |
| } else if (c == '}' || c == ')' || c == ']') { |
102 |
10274 |
10376 |
|
| 63 |
713 |
| ctx->depth--; |
| 64 |
713 |
▶ | if (ctx->depth < 0) ctx->depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->depth < 0) ctx->depth = 0; |
0 |
713 |
713 |
|
| 65 |
10274 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
65 |
10209 |
10274 |
|
| 66 |
65 |
| ctx->state = ESHU_STRING_DQ; |
| 67 |
10209 |
▶ | } else if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '`') { |
2 |
10207 |
10209 |
|
| 68 |
2 |
| ctx->state = ESHU_GO_RAW_STR; |
| 69 |
10207 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
1 |
10206 |
10207 |
|
| 70 |
1 |
| ctx->state = ESHU_GO_RUNE; |
| 71 |
10206 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
7 |
10199 |
10206 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
7 |
0 |
7 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
3 |
4 |
7 |
|
| 72 |
3 |
| return; /* line comment — stop */ |
| 73 |
10203 |
▶ | } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
4 |
10199 |
10203 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
4 |
0 |
4 |
| } else if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
2 |
2 |
4 |
|
| 74 |
2 |
| ctx->state = ESHU_COMMENT_BLOCK; |
| 75 |
2 |
| p++; |
| 76 |
|
| } |
| 77 |
11697 |
| break; |
| 78 |
|
| |
| 79 |
621 |
| case ESHU_STRING_DQ: |
| 80 |
621 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
5 |
616 |
621 |
| if (c == '\\' && p + 1 < end) { |
5 |
0 |
5 |
|
| 81 |
5 |
| p++; /* skip escaped char */ |
| 82 |
616 |
▶ | } else if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '"') { |
65 |
551 |
616 |
|
| 83 |
65 |
| ctx->state = ESHU_CODE; |
| 84 |
|
| } |
| 85 |
621 |
| break; |
| 86 |
|
| |
| 87 |
43 |
| case ESHU_GO_RAW_STR: |
| 88 |
|
| /* backtick string: no escapes, terminated by next backtick */ |
| 89 |
43 |
▶ | if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') { |
2 |
41 |
43 |
|
| 90 |
2 |
| ctx->state = ESHU_CODE; |
| 91 |
|
| } |
| 92 |
43 |
| break; |
| 93 |
|
| |
| 94 |
2 |
| case ESHU_GO_RUNE: |
| 95 |
2 |
▶ | if (c == '\\' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < end) { |
0 |
2 |
2 |
| if (c == '\\' && p + 1 < end) { |
0 |
0 |
0 |
|
| 96 |
0 |
| p++; |
| 97 |
2 |
▶ | } else if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c == '\'') { |
1 |
1 |
2 |
|
| 98 |
1 |
| ctx->state = ESHU_CODE; |
| 99 |
|
| } |
| 100 |
2 |
| break; |
| 101 |
|
| |
| 102 |
57 |
| case ESHU_COMMENT_BLOCK: |
| 103 |
57 |
▶ | if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
2 |
55 |
57 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
2 |
0 |
2 |
| if (c == '*' && p + 1 < end && *(p + 1) == '/') { |
2 |
0 |
2 |
|
| 104 |
2 |
| ctx->state = ESHU_CODE; |
| 105 |
2 |
| p++; |
| 106 |
|
| } |
| 107 |
57 |
| break; |
| 108 |
|
| |
| 109 |
0 |
| default: |
| 110 |
0 |
| break; |
| 111 |
|
| } |
| 112 |
12420 |
| p++; |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 117 |
|
| * Process a single line — decide indent, emit, scan |
| 118 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 119 |
|
| |
| 120 |
763 |
| static void eshu_go_process_line(eshu_go_ctx_t *ctx, eshu_buf_t *out, |
| 121 |
|
| const char *line_start, const char *eol, |
| 122 |
|
| int lineno) { |
| 123 |
763 |
| const char *content = eshu_skip_leading_ws(line_start); |
| 124 |
|
| int line_len; |
| 125 |
|
| int indent_depth; |
| 126 |
|
| |
| 127 |
|
| /* empty line — preserve it */ |
| 128 |
763 |
▶ | if (content >= eol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (content >= eol) { |
14 |
749 |
763 |
|
| 129 |
14 |
| eshu_buf_putc(out, '\n'); |
| 130 |
14 |
| return; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
749 |
| line_len = (int)(eol - content); |
| 134 |
|
| |
| 135 |
|
| /* Inside a raw string or block comment: pass through verbatim */ |
| 136 |
749 |
▶ | if (ctx->state == ESHU_GO_RAW_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_GO_RAW_STR) { |
1 |
748 |
749 |
|
| 137 |
1 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
1 |
0 |
1 |
|
| 138 |
1 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 139 |
|
| } else { |
| 140 |
0 |
| eshu_buf_write_trimmed(out, line_start, (int)(eol - line_start)); |
| 141 |
|
| } |
| 142 |
1 |
| eshu_buf_putc(out, '\n'); |
| 143 |
1 |
| eshu_go_scan_line(ctx, content, eol); |
| 144 |
1 |
| return; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
748 |
▶ | if (ctx->state == ESHU_COMMENT_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->state == ESHU_COMMENT_BLOCK) { |
1 |
747 |
748 |
|
| 148 |
|
| /* Preserve interior whitespace of block comment continuation lines. */ |
| 149 |
1 |
| eshu_emit_indent(out, ctx->depth, &ctx->cfg); |
| 150 |
1 |
| eshu_buf_write_trimmed(out, line_start, (int)(eol - line_start)); |
| 151 |
1 |
| eshu_buf_putc(out, '\n'); |
| 152 |
1 |
| eshu_go_scan_line(ctx, content, eol); |
| 153 |
1 |
| return; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
747 |
| indent_depth = ctx->depth; |
| 157 |
|
| |
| 158 |
|
| /* Closing brace/paren/bracket — dedent this line */ |
| 159 |
747 |
▶ | if (*content == '}' || *content == ')' || *content == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*content == '}' || *content == ')' || *content == ']') { |
552 |
195 |
747 |
| if (*content == '}' || *content == ')' || *content == ']') { |
551 |
1 |
552 |
| if (*content == '}' || *content == ')' || *content == ']') { |
0 |
551 |
551 |
|
| 160 |
196 |
| indent_depth--; |
| 161 |
196 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
196 |
196 |
|
| 162 |
551 |
▶ | } else if (eshu_go_is_case_label(content, line_len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_go_is_case_label(content, line_len)) { |
29 |
522 |
551 |
|
| 163 |
|
| /* case/default labels sit one level shallower than the body */ |
| 164 |
29 |
| indent_depth--; |
| 165 |
29 |
▶ | if (indent_depth < 0) indent_depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (indent_depth < 0) indent_depth = 0; |
0 |
29 |
29 |
|
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
747 |
▶ | if (eshu_in_range(&ctx->cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(&ctx->cfg, lineno)) { |
747 |
0 |
747 |
|
| 169 |
747 |
| eshu_emit_indent(out, indent_depth, &ctx->cfg); |
| 170 |
|
| } else { |
| 171 |
0 |
| eshu_buf_write(out, line_start, (size_t)(content - line_start)); |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
747 |
| eshu_buf_write_trimmed(out, content, line_len); |
| 175 |
747 |
| eshu_buf_putc(out, '\n'); |
| 176 |
|
| |
| 177 |
747 |
| eshu_go_scan_line(ctx, content, eol); |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 181 |
|
| * Public API — indent a Go source string |
| 182 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 183 |
|
| |
| 184 |
89 |
| static char *eshu_indent_go(const char *src, size_t src_len, |
| 185 |
|
| const eshu_config_t *cfg, size_t *out_len) { |
| 186 |
|
| eshu_go_ctx_t ctx; |
| 187 |
|
| eshu_buf_t out; |
| 188 |
89 |
| const char *p = src; |
| 189 |
89 |
| const char *end = src + src_len; |
| 190 |
89 |
| int lineno = 1; |
| 191 |
|
| |
| 192 |
89 |
| eshu_go_ctx_init(&ctx, cfg); |
| 193 |
89 |
| eshu_buf_init(&out, src_len + 256); |
| 194 |
|
| |
| 195 |
852 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
763 |
89 |
852 |
|
| 196 |
763 |
| const char *eol = eshu_find_eol(p); |
| 197 |
763 |
| eshu_go_process_line(&ctx, &out, p, eol, lineno); |
| 198 |
763 |
| p = eol; |
| 199 |
763 |
▶ | if (*p == '\n') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') p++; |
763 |
0 |
763 |
|
| 200 |
763 |
| lineno++; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
89 |
| eshu_buf_putc(&out, '\0'); |
| 204 |
89 |
| out.len--; |
| 205 |
89 |
| *out_len = out.len; |
| 206 |
89 |
| return out.data; |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| |
| 210 |
|
| #include "eshu_hl_util.h" |
| 211 |
|
| |
| 212 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 213 |
|
| * Go keyword / builtin tables |
| 214 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 215 |
|
| |
| 216 |
|
| static const char * const eshu_hl_go_kw[] = { |
| 217 |
|
| "break", "case", "chan", "const", "continue", |
| 218 |
|
| "default", "defer", "else", "fallthrough", "for", |
| 219 |
|
| "func", "go", "goto", "if", "import", |
| 220 |
|
| "interface", "map", "package", "range", "return", |
| 221 |
|
| "select", "struct", "switch", "type", "var", |
| 222 |
|
| NULL |
| 223 |
|
| }; |
| 224 |
|
| |
| 225 |
|
| static const char * const eshu_hl_go_bi[] = { |
| 226 |
|
| /* predeclared functions */ |
| 227 |
|
| "append", "cap", "close", "complex", "copy", |
| 228 |
|
| "delete", "imag", "len", "make", "new", |
| 229 |
|
| "panic", "print", "println", "real", "recover", |
| 230 |
|
| /* predeclared types */ |
| 231 |
|
| "bool", "byte", "complex64", "complex128", |
| 232 |
|
| "error", "float32", "float64", |
| 233 |
|
| "int", "int8", "int16", "int32", "int64", |
| 234 |
|
| "rune", "string", |
| 235 |
|
| "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", |
| 236 |
|
| /* predeclared constants */ |
| 237 |
|
| "false", "iota", "nil", "true", |
| 238 |
|
| NULL |
| 239 |
|
| }; |
| 240 |
|
| |
| 241 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 242 |
|
| * Go highlighter |
| 243 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 244 |
|
| |
| 245 |
38 |
| static char *eshu_highlight_go(const char *src, size_t src_len, size_t *out_len) { |
| 246 |
|
| eshu_buf_t out; |
| 247 |
38 |
| const char *p = src; |
| 248 |
38 |
| const char *end = src + src_len; |
| 249 |
38 |
| const char *plain = p; |
| 250 |
|
| |
| 251 |
38 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 252 |
|
| |
| 253 |
|
| #define GO_SPAN(cls, ts, te) do { \ |
| 254 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 255 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 256 |
|
| p = (te); plain = p; \ |
| 257 |
|
| } while(0) |
| 258 |
|
| |
| 259 |
415 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
377 |
38 |
415 |
|
| 260 |
377 |
| char c = *p; |
| 261 |
|
| |
| 262 |
|
| /* line comment */ |
| 263 |
377 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
375 |
377 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
2 |
0 |
2 |
| if (c == '/' && p + 1 < end && *(p + 1) == '/') { |
1 |
1 |
2 |
|
| 264 |
1 |
| const char *ts = p; |
| 265 |
1 |
| p += 2; |
| 266 |
19 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
19 |
0 |
19 |
| while (p < end && *p != '\n') p++; |
18 |
1 |
19 |
|
| 267 |
1 |
| GO_SPAN("esh-c", ts, p); |
| 268 |
1 |
| continue; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
|
| /* block comment */ |
| 272 |
376 |
▶ | if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
375 |
376 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
| if (c == '/' && p + 1 < end && *(p + 1) == '*') { |
1 |
0 |
1 |
|
| 273 |
1 |
| const char *ts = p; |
| 274 |
1 |
| p += 2; |
| 275 |
8 |
▶ | while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
8 |
0 |
8 |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
7 |
1 |
8 |
| while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; |
0 |
1 |
1 |
|
| 276 |
1 |
▶ | if (p + 1 < end) p += 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + 1 < end) p += 2; |
1 |
0 |
1 |
|
| 277 |
1 |
| GO_SPAN("esh-c", ts, p); |
| 278 |
1 |
| continue; |
| 279 |
|
| } |
| 280 |
|
| |
| 281 |
|
| /* interpreted string "..." */ |
| 282 |
375 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
3 |
372 |
375 |
|
| 283 |
3 |
| const char *ts = p++; |
| 284 |
20 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
20 |
0 |
20 |
| while (p < end && *p != '"') { |
17 |
3 |
20 |
|
| 285 |
17 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
17 |
17 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 286 |
17 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
17 |
17 |
|
| 287 |
17 |
| p++; |
| 288 |
|
| } |
| 289 |
3 |
▶ | if (p < end && *p == '"') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '"') p++; |
3 |
0 |
3 |
| if (p < end && *p == '"') p++; |
3 |
0 |
3 |
|
| 290 |
3 |
| GO_SPAN("esh-s", ts, p); |
| 291 |
3 |
| continue; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
|
| /* raw string `...` — can span multiple lines */ |
| 295 |
372 |
▶ | if (c == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '`') { |
1 |
371 |
372 |
|
| 296 |
1 |
| const char *ts = p++; |
| 297 |
11 |
▶ | while (p < end && *p != '`') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '`') p++; |
11 |
0 |
11 |
| while (p < end && *p != '`') p++; |
10 |
1 |
11 |
|
| 298 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 299 |
1 |
| GO_SPAN("esh-s", ts, p); |
| 300 |
1 |
| continue; |
| 301 |
|
| } |
| 302 |
|
| |
| 303 |
|
| /* rune literal '.' */ |
| 304 |
371 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
1 |
370 |
371 |
|
| 305 |
1 |
| const char *ts = p++; |
| 306 |
2 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
2 |
0 |
2 |
| while (p < end && *p != '\'') { |
1 |
1 |
2 |
|
| 307 |
1 |
▶ | if (*p == '\\') { p++; if (p < end) p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
1 |
1 |
| if (*p == '\\') { p++; if (p < end) p++; continue; } |
0 |
0 |
0 |
|
| 308 |
1 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
1 |
1 |
|
| 309 |
1 |
| p++; |
| 310 |
|
| } |
| 311 |
1 |
▶ | if (p < end && *p == '\'') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '\'') p++; |
1 |
0 |
1 |
| if (p < end && *p == '\'') p++; |
1 |
0 |
1 |
|
| 312 |
1 |
| GO_SPAN("esh-s", ts, p); |
| 313 |
1 |
| continue; |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| /* number: digit, 0x hex, 0b binary, 0o octal, or .digit float */ |
| 317 |
370 |
▶ | if (isdigit((unsigned char)c) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (isdigit((unsigned char)c) || |
359 |
11 |
370 |
| if (isdigit((unsigned char)c) || |
2 |
357 |
359 |
|
| 318 |
2 |
▶ | (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)))) { |
2 |
0 |
2 |
| (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { |
0 |
2 |
2 |
|
| 319 |
11 |
| const char *ts = p; |
| 320 |
14 |
▶ | if (c == '0' && p + 1 < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '0' && p + 1 < end) { |
3 |
8 |
11 |
| if (c == '0' && p + 1 < end) { |
3 |
0 |
3 |
|
| 321 |
3 |
| char next = *(p + 1); |
| 322 |
3 |
▶ | if (next == 'x' || next == 'X') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (next == 'x' || next == 'X') { |
2 |
1 |
3 |
| if (next == 'x' || next == 'X') { |
0 |
2 |
2 |
|
| 323 |
1 |
| p += 2; |
| 324 |
3 |
▶ | while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
1 |
3 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
0 |
2 |
| while (p < end && (isxdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 325 |
2 |
▶ | } else if (next == 'b' || next == 'B') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (next == 'b' || next == 'B') { |
1 |
1 |
2 |
| } else if (next == 'b' || next == 'B') { |
0 |
1 |
1 |
|
| 326 |
1 |
| p += 2; |
| 327 |
5 |
▶ | while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
5 |
0 |
5 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
2 |
3 |
5 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
2 |
1 |
3 |
| while (p < end && (*p == '0' || *p == '1' || *p == '_')) p++; |
0 |
1 |
1 |
|
| 328 |
1 |
▶ | } else if (next == 'o' || next == 'O') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (next == 'o' || next == 'O') { |
0 |
1 |
1 |
| } else if (next == 'o' || next == 'O') { |
0 |
0 |
0 |
|
| 329 |
1 |
| p += 2; |
| 330 |
4 |
▶ | while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
4 |
0 |
4 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
3 |
1 |
4 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
3 |
0 |
3 |
| while (p < end && ((*p >= '0' && *p <= '7') || *p == '_')) p++; |
0 |
1 |
1 |
|
| 331 |
|
| } else { |
| 332 |
0 |
▶ | 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++; |
0 |
0 |
0 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 333 |
0 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
0 |
0 |
0 |
| if (p < end && *p == '.') { |
0 |
0 |
0 |
|
| 334 |
0 |
| p++; |
| 335 |
0 |
▶ | 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++; |
0 |
0 |
0 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
0 |
0 |
|
| 336 |
|
| } |
| 337 |
0 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
0 |
0 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
0 |
0 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
0 |
0 |
|
| 338 |
0 |
| p++; |
| 339 |
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 |
|
| 340 |
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 |
|
| 341 |
|
| } |
| 342 |
|
| } |
| 343 |
|
| } else { |
| 344 |
27 |
▶ | 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++; |
24 |
3 |
27 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
17 |
7 |
24 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
5 |
7 |
|
| 345 |
8 |
▶ | if (p < end && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '.') { |
5 |
3 |
8 |
| if (p < end && *p == '.') { |
1 |
4 |
5 |
|
| 346 |
1 |
| p++; |
| 347 |
3 |
▶ | 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++; |
3 |
0 |
3 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
2 |
1 |
3 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
1 |
1 |
|
| 348 |
|
| } |
| 349 |
8 |
▶ | if (p < end && (*p == 'e' || *p == 'E')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p < end && (*p == 'e' || *p == 'E')) { |
5 |
3 |
8 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
5 |
0 |
5 |
| if (p < end && (*p == 'e' || *p == 'E')) { |
0 |
5 |
5 |
|
| 350 |
0 |
| p++; |
| 351 |
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 |
|
| 352 |
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 |
|
| 353 |
|
| } |
| 354 |
|
| } |
| 355 |
|
| /* imaginary suffix */ |
| 356 |
11 |
▶ | if (p < end && *p == 'i') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == 'i') p++; |
7 |
4 |
11 |
| if (p < end && *p == 'i') p++; |
0 |
7 |
7 |
|
| 357 |
11 |
| GO_SPAN("esh-n", ts, p); |
| 358 |
11 |
| continue; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
|
| /* identifier / keyword / builtin */ |
| 362 |
359 |
▶ | if (eshu_hl_isalpha_(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_isalpha_(c)) { |
124 |
235 |
359 |
|
| 363 |
124 |
| const char *ts = p; |
| 364 |
535 |
▶ | while (p < end && eshu_hl_isalnum_(*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
525 |
10 |
535 |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
411 |
114 |
525 |
|
| 365 |
124 |
| eshu_hl_flush(&out, plain, ts); |
| 366 |
124 |
| plain = p; |
| 367 |
124 |
▶ | if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_go_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_go_kw)) { |
37 |
87 |
124 |
|
| 368 |
37 |
| eshu_hl_span(&out, "esh-k", ts, p); |
| 369 |
87 |
▶ | } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_go_bi)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_go_bi)) { |
28 |
59 |
87 |
|
| 370 |
28 |
| eshu_hl_span(&out, "esh-b", ts, p); |
| 371 |
|
| } else { |
| 372 |
59 |
| eshu_hl_write_html(&out, ts, (size_t)(p - ts)); |
| 373 |
|
| } |
| 374 |
124 |
| continue; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
235 |
| p++; |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
38 |
| eshu_hl_flush(&out, plain, end); |
| 381 |
38 |
| eshu_buf_putc(&out, '\0'); |
| 382 |
38 |
| *out_len = out.len - 1; |
| 383 |
38 |
| return out.data; |
| 384 |
|
| |
| 385 |
|
| #undef GO_SPAN |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
|
| #endif /* ESHU_GO_H */ |