include/eshu_lua.h

filter: Statementstmt 82.4% 240 / 291 Branchbran 57.8% 213 / 368 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.7% 453 / 659 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Eshu.xs ↑ Summary include/eshu_rust.h »
1 /*
2 * eshu_lua.h — Lua language indentation scanner
3 *
4 * Lua uses keyword-based block nesting (do/end, if/then/end, etc.)
5 * with no braces for control flow (braces only for table constructors).
6 * Long strings [[...]] and long comments --[[...]] can span multiple lines.
7 */
8  
9 #ifndef ESHU_LUA_H
10 #define ESHU_LUA_H
11  
12 #include "eshu.h"
13  
14 /* ══════════════════════════════════════════════════════════════════
15 * Scanner context — persists across lines
16 * ══════════════════════════════════════════════════════════════════ */
17  
18 typedef struct {
19 int depth;
20 int brace_depth; /* { } table constructor nesting */
21 int paren_depth; /* ( ) nesting */
22 int long_level; /* = count in [=[...]=] */
23 enum eshu_state state;
24 eshu_config_t cfg;
25 } eshu_lua_ctx_t;
26  
27 76 static void eshu_lua_ctx_init(eshu_lua_ctx_t *ctx, const eshu_config_t *cfg) {
28 76 ctx->depth = 0;
29 76 ctx->brace_depth = 0;
30 76 ctx->paren_depth = 0;
31 76 ctx->long_level = 0;
32 76 ctx->state = ESHU_CODE;
33 76 ctx->cfg = *cfg;
34 76 }
35  
36 /* ══════════════════════════════════════════════════════════════════
37 * Helpers
38 * ══════════════════════════════════════════════════════════════════ */
39  
40 7601 static int eshu_lua_isword(char c) {
41 7601 ▶ return isalnum((unsigned char)c) || c == '_';
Branch: 2 branches
BranchTrueFalseTotal
return isalnum((unsigned char)c) || c == '_'; 2410 5191 7601
return isalnum((unsigned char)c) || c == '_'; 65 2345 2410
42 }
43  
44 /* Match keyword kw at p: must not be preceded by or followed by word chars */
45 18744 static int eshu_lua_kw(const char *src, const char *p, const char *end,
46 const char *kw, int klen) {
47 18744 ▶ if ((size_t)(end - p) < (size_t)klen) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(end - p) < (size_t)klen) return 0; 2669 16075 18744
48 16075 ▶ if (memcmp(p, kw, (size_t)klen) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p, kw, (size_t)klen) != 0) return 0; 15637 438 16075
49 438 ▶ if (p > src && eshu_lua_isword(*(p - 1))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p > src && eshu_lua_isword(*(p - 1))) return 0; 219 219 438
if (p > src && eshu_lua_isword(*(p - 1))) return 0; 0 219 219
50 438 ▶ if (p + klen < end && eshu_lua_isword(p[klen])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p + klen < end && eshu_lua_isword(p[klen])) return 0; 181 257 438
if (p + klen < end && eshu_lua_isword(p[klen])) return 0; 6 175 181
51 432 return 1;
52 }
53  
54 /* Scan past opening long bracket [=*[ starting at p (which points to first '[').
55 * Returns: level count (number of '='), or -1 if not a long bracket.
56 * Advances *pp past the opening bracket. */
57 87 static int eshu_lua_open_long(const char **pp, const char *end) {
58 87 const char *p = *pp;
59 87 int level = 0;
60 87 ▶ if (p >= end || *p != '[') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '[') return -1; 87 0 87
if (p >= end || *p != '[') return -1; 0 87 87
61 87 p++;
62 87 ▶ while (p < end && *p == '=') { level++; p++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == '=') { level++; p++; } 87 0 87
while (p < end && *p == '=') { level++; p++; } 0 87 87
63 87 ▶ if (p >= end || *p != '[') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '[') return -1; 87 0 87
if (p >= end || *p != '[') return -1; 85 2 87
64 2 p++;
65 2 *pp = p;
66 2 return level;
67 }
68  
69 /* Returns 1 if p points to closing long bracket ]=*] matching level.
70 * Advances *pp past the closing bracket if matched. */
71 2 static int eshu_lua_close_long(const char **pp, const char *end, int level) {
72 2 const char *p = *pp;
73 int i;
74 2 ▶ if (p >= end || *p != ']') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != ']') return 0; 2 0 2
if (p >= end || *p != ']') return 0; 0 2 2
75 2 p++;
76 2 ▶ for (i = 0; i < level; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < level; i++) { 0 2 2
77 0 ▶ if (p >= end || *p != '=') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '=') return 0; 0 0 0
if (p >= end || *p != '=') return 0; 0 0 0
78 0 p++;
79 }
80 2 ▶ if (p >= end || *p != ']') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != ']') return 0; 2 0 2
if (p >= end || *p != ']') return 0; 0 2 2
81 2 p++;
82 2 *pp = p;
83 2 return 1;
84 }
85  
86 /* ══════════════════════════════════════════════════════════════════
87 * Per-line scanner: returns pre/post depth deltas
88 * ══════════════════════════════════════════════════════════════════ */
89  
90 typedef struct {
91 int pre;
92 int post;
93 } eshu_lua_delta_t;
94  
95 623 static eshu_lua_delta_t eshu_lua_scan_line(eshu_lua_ctx_t *ctx,
96 const char *content, int len)
97 {
98 623 eshu_lua_delta_t d = {0, 0};
99 623 const char *p = content;
100 623 const char *end = content + len;
101 623 const char *src = content;
102 623 int local_brace_opens = 0; /* { opened on this line — cancel same-line } */
103  
104 10579 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 9959 620 10579
105 9959 char c = *p;
106  
107 /* ── string/comment contexts ── */
108  
109 /* single-quoted string */
110 9959 ▶ if (c == '\'' && ctx->state == ESHU_CODE) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\'' && ctx->state == ESHU_CODE) { 1 9958 9959
if (c == '\'' && ctx->state == ESHU_CODE) { 1 0 1
111 1 p++;
112 13 ▶ while (p < end && *p != '\'') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\'') { 13 0 13
while (p < end && *p != '\'') { 12 1 13
113 12 ▶ if (*p == '\\') { p++; if (p < end) p++; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; } 0 12 12
if (*p == '\\') { p++; if (p < end) p++; } 0 0 0
114 12 else p++;
115 }
116 1 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 1 0 1
117 1 continue;
118 }
119  
120 /* double-quoted string */
121 9958 ▶ if (c == '"' && ctx->state == ESHU_CODE) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '"' && ctx->state == ESHU_CODE) { 23 9935 9958
if (c == '"' && ctx->state == ESHU_CODE) { 23 0 23
122 23 p++;
123 163 ▶ while (p < end && *p != '"') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '"') { 163 0 163
while (p < end && *p != '"') { 140 23 163
124 140 ▶ if (*p == '\\') { p++; if (p < end) p++; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; } 2 138 140
if (*p == '\\') { p++; if (p < end) p++; } 2 0 2
125 138 else p++;
126 }
127 23 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 23 0 23
128 23 continue;
129 }
130  
131 /* long string [=*[ */
132 9935 ▶ if (c == '[' && ctx->state == ESHU_CODE) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '[' && ctx->state == ESHU_CODE) { 86 9849 9935
if (c == '[' && ctx->state == ESHU_CODE) { 86 0 86
133 86 const char *tp = p;
134 86 int level = eshu_lua_open_long(&tp, end);
135 86 ▶ if (level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level >= 0) { 1 85 86
136 1 ctx->long_level = level;
137 1 ctx->state = ESHU_LUA_LONG_STR;
138 1 p = tp;
139 /* scan rest of line for close */
140 1 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 1 1
141 0 ▶ if (*p == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ']') { 0 0 0
142 0 const char *cp = p;
143 0 ▶ if (eshu_lua_close_long(&cp, end, level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_close_long(&cp, end, level)) { 0 0 0
144 0 ctx->state = ESHU_CODE;
145 0 p = cp;
146 0 break;
147 }
148 }
149 0 p++;
150 }
151 1 continue;
152 }
153 }
154  
155 /* line comment -- */
156 9934 ▶ if (c == '-' && p + 1 < end && *(p + 1) == '-' &&
Branch: 3 branches
BranchTrueFalseTotal
if (c == '-' && p + 1 < end && *(p + 1) == '-' && 15 9919 9934
if (c == '-' && p + 1 < end && *(p + 1) == '-' && 15 0 15
if (c == '-' && p + 1 < end && *(p + 1) == '-' && 4 11 15
157 4 ▶ ctx->state == ESHU_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
ctx->state == ESHU_CODE) { 4 0 4
158 /* check for long comment --[=*[ */
159 4 const char *tp = p + 2;
160 4 ▶ if (tp < end && *tp == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (tp < end && *tp == '[') { 4 0 4
if (tp < end && *tp == '[') { 1 3 4
161 1 int level = eshu_lua_open_long(&tp, end);
162 1 ▶ if (level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level >= 0) { 1 0 1
163 1 ctx->long_level = level;
164 1 ctx->state = ESHU_LUA_LONG_CMT;
165 1 p = tp;
166 /* scan rest of line for close */
167 1 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 1 1
168 0 ▶ if (*p == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ']') { 0 0 0
169 0 const char *cp = p;
170 0 ▶ if (eshu_lua_close_long(&cp, end, level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_close_long(&cp, end, level)) { 0 0 0
171 0 ctx->state = ESHU_CODE;
172 0 p = cp;
173 0 break;
174 }
175 }
176 0 p++;
177 }
178 1 continue;
179 }
180 }
181 /* plain line comment: rest of line ignored */
182 3 break;
183 }
184  
185 /* inside long string: scan for close bracket */
186 9930 ▶ if (ctx->state == ESHU_LUA_LONG_STR ||
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_LUA_LONG_STR || 9930 0 9930
187 9930 ▶ ctx->state == ESHU_LUA_LONG_CMT) {
Branch: 1 branch
BranchTrueFalseTotal
ctx->state == ESHU_LUA_LONG_CMT) { 0 9930 9930
188 0 ▶ if (c == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ']') { 0 0 0
189 0 const char *cp = p;
190 0 ▶ if (eshu_lua_close_long(&cp, end, ctx->long_level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_close_long(&cp, end, ctx->long_level)) { 0 0 0
191 0 ctx->state = ESHU_CODE;
192 0 p = cp;
193 0 continue;
194 }
195 }
196 0 p++;
197 0 continue;
198 }
199  
200 /* paren tracking — suppress keyword detection inside () */
201 9930 ▶ if (c == '(' && ctx->state == ESHU_CODE) { ctx->paren_depth++; p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '(' && ctx->state == ESHU_CODE) { ctx->paren_depth++; p++; continue; } 264 9666 9930
if (c == '(' && ctx->state == ESHU_CODE) { ctx->paren_depth++; p++; continue; } 264 0 264
202 9666 ▶ if (c == ')' && ctx->paren_depth > 0) { ctx->paren_depth--; p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == ')' && ctx->paren_depth > 0) { ctx->paren_depth--; p++; continue; } 264 9402 9666
if (c == ')' && ctx->paren_depth > 0) { ctx->paren_depth--; p++; continue; } 264 0 264
203  
204 /* table brace tracking — same-line { } pairs cancel to avoid net depth change */
205 9402 ▶ if (c == '{' && ctx->state == ESHU_CODE) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' && ctx->state == ESHU_CODE) { 79 9323 9402
if (c == '{' && ctx->state == ESHU_CODE) { 79 0 79
206 79 ctx->brace_depth++;
207 79 local_brace_opens++;
208 79 p++; continue;
209 }
210 9323 ▶ if (c == '}' && ctx->brace_depth > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '}' && ctx->brace_depth > 0) { 79 9244 9323
if (c == '}' && ctx->brace_depth > 0) { 79 0 79
211 79 ctx->brace_depth--;
212 79 ▶ if (local_brace_opens > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (local_brace_opens > 0) { 75 4 79
213 75 local_brace_opens--; /* cancel same-line open — no net depth change */
214 } else {
215 4 d.pre--; /* close a brace opened on a previous line */
216 }
217 79 p++; continue;
218 }
219  
220 /* ── keyword detection at word boundaries, outside parens ── */
221 9244 ▶ if (ctx->state == ESHU_CODE && ctx->paren_depth == 0 &&
Branch: 3 branches
BranchTrueFalseTotal
if (ctx->state == ESHU_CODE && ctx->paren_depth == 0 && 9244 0 9244
if (ctx->state == ESHU_CODE && ctx->paren_depth == 0 && 7812 1432 9244
if (ctx->state == ESHU_CODE && ctx->paren_depth == 0 && 7201 611 7812
222 7201 ▶ (p == src || !eshu_lua_isword(*(p - 1)))) {
Branch: 1 branch
BranchTrueFalseTotal
(p == src || !eshu_lua_isword(*(p - 1)))) { 1951 5250 7201
223  
224 /* Closers: end, until */
225 2562 ▶ if (eshu_lua_kw(src, p, end, "end", 3)) { d.pre--; p += 3; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "end", 3)) { d.pre--; p += 3; continue; } 203 2359 2562
226 2359 ▶ if (eshu_lua_kw(src, p, end, "until", 5)) { d.pre--; p += 5; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "until", 5)) { d.pre--; p += 5; continue; } 2 2357 2359
227  
228 /* elseif: pre-- only; then on same line provides post++ */
229 2357 ▶ if (eshu_lua_kw(src, p, end, "elseif", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "elseif", 6)) { 5 2352 2357
230 5 d.pre--;
231 5 p += 6; continue;
232 }
233  
234 /* else: -1 pre, +1 post (no then follows) */
235 2352 ▶ if (eshu_lua_kw(src, p, end, "else", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "else", 4)) { 12 2340 2352
236 12 d.pre--; d.post++;
237 12 p += 4; continue;
238 }
239  
240 /* Deferred openers: then, do */
241 2340 ▶ if (eshu_lua_kw(src, p, end, "then", 4)) { d.post++; p += 4; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "then", 4)) { d.post++; p += 4; continue; } 50 2290 2340
242 2290 ▶ if (eshu_lua_kw(src, p, end, "do", 2)) { d.post++; p += 2; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "do", 2)) { d.post++; p += 2; continue; } 47 2243 2290
243  
244 /* repeat: opens directly (no then/do follows) */
245 2243 ▶ if (eshu_lua_kw(src, p, end, "repeat", 6)) { d.post++; p += 6; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "repeat", 6)) { d.post++; p += 6; continue; } 2 2241 2243
246  
247 /* function: +1 post */
248 2241 ▶ if (eshu_lua_kw(src, p, end, "function", 8)) { d.post++; p += 8; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_kw(src, p, end, "function", 8)) { d.post++; p += 8; continue; } 111 2130 2241
249 }
250  
251 8812 p++;
252 }
253  
254 /* remaining brace opens that weren't closed on this line → deferred depth increase */
255 623 d.post += local_brace_opens;
256  
257 623 return d;
258 }
259  
260 /* ══════════════════════════════════════════════════════════════════
261 * Main indentation entry point
262 * ══════════════════════════════════════════════════════════════════ */
263  
264 76 static char *eshu_indent_lua(const char *src, size_t src_len,
265 const eshu_config_t *cfg, size_t *out_len)
266 {
267 eshu_lua_ctx_t ctx;
268 eshu_buf_t out;
269 76 const char *p = src;
270 76 const char *end = src + src_len;
271 76 int lineno = 1;
272  
273 76 eshu_lua_ctx_init(&ctx, cfg);
274 76 eshu_buf_init(&out, src_len + 512);
275  
276 727 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 651 76 727
277 651 const char *line_start = p;
278 const char *eol;
279 const char *content;
280 int content_len;
281 int is_blank;
282 eshu_lua_delta_t delta;
283  
284 651 eol = p;
285 12692 ▶ while (eol < end && *eol != '\n') eol++;
Branch: 2 branches
BranchTrueFalseTotal
while (eol < end && *eol != '\n') eol++; 12692 0 12692
while (eol < end && *eol != '\n') eol++; 12041 651 12692
286  
287 651 content = eshu_skip_leading_ws(p);
288 651 content_len = (int)(eol - content);
289 651 ▶ while (content_len > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
while (content_len > 0 && 629 22 651
290 629 ▶ (content[content_len - 1] == ' ' ||
Branch: 1 branch
BranchTrueFalseTotal
(content[content_len - 1] == ' ' || 0 629 629
291 629 ▶ content[content_len - 1] == '\t'))
Branch: 1 branch
BranchTrueFalseTotal
content[content_len - 1] == '\t')) 0 629 629
292 0 content_len--;
293  
294 651 is_blank = (content_len == 0);
295  
296 /* Inside a long string or comment: emit verbatim */
297 651 ▶ if (ctx.state == ESHU_LUA_LONG_STR || ctx.state == ESHU_LUA_LONG_CMT) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx.state == ESHU_LUA_LONG_STR || ctx.state == ESHU_LUA_LONG_CMT) { 648 3 651
if (ctx.state == ESHU_LUA_LONG_STR || ctx.state == ESHU_LUA_LONG_CMT) { 3 645 648
298 /* scan for closing bracket on this line */
299 6 const char *pp = content;
300 6 const char *cl = content + content_len;
301 37 ▶ while (pp < cl) {
Branch: 1 branch
BranchTrueFalseTotal
while (pp < cl) { 33 4 37
302 33 ▶ if (*pp == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*pp == ']') { 2 31 33
303 2 const char *cp = pp;
304 2 ▶ if (eshu_lua_close_long(&cp, cl, ctx.long_level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_lua_close_long(&cp, cl, ctx.long_level)) { 2 0 2
305 2 ctx.state = ESHU_CODE;
306 2 break;
307 }
308 }
309 31 pp++;
310 }
311 6 eshu_buf_write(&out, line_start, (size_t)(eol - line_start));
312 6 ▶ if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; }
Branch: 1 branch
BranchTrueFalseTotal
if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; } 6 0 6
313 6 p = eol;
314 6 lineno++;
315 28 continue;
316 }
317  
318 645 ▶ if (is_blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank) { 22 623 645
319 22 ▶ if (eol < end) eshu_buf_putc(&out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (eol < end) eshu_buf_putc(&out, '\n'); 22 0 22
320 22 ▶ p = (eol < end) ? eol + 1 : eol;
Branch: 1 branch
BranchTrueFalseTotal
p = (eol < end) ? eol + 1 : eol; 22 0 22
321 22 lineno++;
322 22 continue;
323 }
324  
325 623 delta = eshu_lua_scan_line(&ctx, content, content_len);
326  
327 623 ctx.depth += delta.pre;
328 623 ▶ if (ctx.depth < 0) ctx.depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.depth < 0) ctx.depth = 0; 12 611 623
329  
330 623 ▶ if (eshu_in_range(cfg, lineno)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(cfg, lineno)) { 623 0 623
331 623 eshu_emit_indent(&out, ctx.depth, cfg);
332 } else {
333 0 eshu_buf_write(&out, line_start, (size_t)(content - line_start));
334 }
335  
336 623 eshu_buf_write_trimmed(&out, content, (int)(eol - content));
337 623 ▶ if (eol < end) eshu_buf_putc(&out, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (eol < end) eshu_buf_putc(&out, '\n'); 623 0 623
338  
339 623 ctx.depth += delta.post;
340 623 ▶ if (ctx.depth < 0) ctx.depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.depth < 0) ctx.depth = 0; 0 623 623
341  
342 623 ▶ p = (eol < end) ? eol + 1 : eol;
Branch: 1 branch
BranchTrueFalseTotal
p = (eol < end) ? eol + 1 : eol; 623 0 623
343 623 lineno++;
344 }
345  
346 76 eshu_buf_putc(&out, '\0');
347 76 *out_len = out.len - 1;
348 76 return out.data;
349 }
350  
351  
352 #include "eshu_hl_util.h"
353  
354 /* ══════════════════════════════════════════════════════════════════
355 * Lua keyword / builtin tables
356 * ══════════════════════════════════════════════════════════════════ */
357  
358 static const char * const eshu_hl_lua_kw[] = {
359 "and", "break", "do", "else", "elseif", "end",
360 "false", "for", "function", "goto", "if", "in",
361 "local", "nil", "not", "or", "repeat", "return",
362 "then", "true", "until", "while",
363 NULL
364 };
365  
366 static const char * const eshu_hl_lua_bi[] = {
367 /* standard library functions */
368 "assert", "collectgarbage", "dofile", "error",
369 "getmetatable", "ipairs", "load", "loadfile",
370 "next", "pairs", "pcall", "print",
371 "rawequal", "rawget", "rawlen", "rawset",
372 "require", "select", "setmetatable",
373 "tonumber", "tostring", "type", "warn", "xpcall",
374 /* standard library tables */
375 "coroutine", "debug", "io", "math", "os",
376 "package", "string", "table", "utf8",
377 NULL
378 };
379  
380 /* ══════════════════════════════════════════════════════════════════
381 * Lua highlighter
382 * ══════════════════════════════════════════════════════════════════ */
383  
384 41 static char *eshu_highlight_lua(const char *src, size_t src_len, size_t *out_len) {
385 eshu_buf_t out;
386 41 const char *p = src;
387 41 const char *end = src + src_len;
388 41 const char *plain = p;
389  
390 41 eshu_buf_init(&out, src_len * 2 + 64);
391  
392 #define LUA_SPAN(cls, ts, te) do { \
393 eshu_hl_flush(&out, plain, (ts)); \
394 eshu_hl_span(&out, (cls), (ts), (te)); \
395 p = (te); plain = p; \
396 } while(0)
397  
398 305 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 264 41 305
399 264 char c = *p;
400  
401 /* line comment -- (and long comment --[[ ) */
402 264 ▶ if (c == '-' && p + 1 < end && *(p + 1) == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '-' && p + 1 < end && *(p + 1) == '-') { 2 262 264
if (c == '-' && p + 1 < end && *(p + 1) == '-') { 2 0 2
if (c == '-' && p + 1 < end && *(p + 1) == '-') { 2 0 2
403 2 const char *ts = p;
404 2 p += 2;
405 /* check for long comment --[=*[ */
406 2 ▶ if (p < end && *p == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '[') { 2 0 2
if (p < end && *p == '[') { 1 1 2
407 1 const char *lp = p;
408 1 int level = 0;
409 1 lp++;
410 1 ▶ while (lp < end && *lp == '=') { level++; lp++; }
Branch: 2 branches
BranchTrueFalseTotal
while (lp < end && *lp == '=') { level++; lp++; } 1 0 1
while (lp < end && *lp == '=') { level++; lp++; } 0 1 1
411 1 ▶ if (lp < end && *lp == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (lp < end && *lp == '[') { 1 0 1
if (lp < end && *lp == '[') { 1 0 1
412 /* long comment: scan for closing ]=level=] */
413 1 lp++;
414 1 p = lp;
415 7 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 7 0 7
416 7 ▶ if (*p == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ']') { 1 6 7
417 1 const char *cp = p + 1;
418 int i;
419 1 ▶ for (i = 0; i < level && cp < end && *cp == '='; i++) cp++;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 1 1
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 0 0
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 0 0
420 1 ▶ if (i == level && cp < end && *cp == ']') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == level && cp < end && *cp == ']') { 1 0 1
if (i == level && cp < end && *cp == ']') { 1 0 1
if (i == level && cp < end && *cp == ']') { 1 0 1
421 1 p = cp + 1;
422 1 break;
423 }
424 }
425 6 p++;
426 }
427 1 LUA_SPAN("esh-c", ts, p);
428 1 continue;
429 }
430 }
431 /* plain line comment: to EOL */
432 11 ▶ while (p < end && *p != '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\n') p++; 10 1 11
while (p < end && *p != '\n') p++; 10 0 10
433 1 LUA_SPAN("esh-c", ts, p);
434 1 continue;
435 }
436  
437 /* double-quoted string */
438 262 ▶ if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { 7 255 262
439 7 const char *ts = p++;
440 31 ▶ while (p < end && *p != '"') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '"') { 31 0 31
while (p < end && *p != '"') { 24 7 31
441 24 ▶ if (*p == '\\') { p++; if (p < end) p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 24 24
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 0 0
442 24 ▶ if (*p == '\n') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') break; 0 24 24
443 24 p++;
444 }
445 7 ▶ if (p < end && *p == '"') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '"') p++; 7 0 7
if (p < end && *p == '"') p++; 7 0 7
446 7 LUA_SPAN("esh-s", ts, p);
447 7 continue;
448 }
449  
450 /* single-quoted string */
451 255 ▶ if (c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\'') { 1 254 255
452 1 const char *ts = p++;
453 6 ▶ while (p < end && *p != '\'') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\'') { 6 0 6
while (p < end && *p != '\'') { 5 1 6
454 5 ▶ if (*p == '\\') { p++; if (p < end) p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 5 5
if (*p == '\\') { p++; if (p < end) p++; continue; } 0 0 0
455 5 ▶ if (*p == '\n') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') break; 0 5 5
456 5 p++;
457 }
458 1 ▶ if (p < end && *p == '\'') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '\'') p++; 1 0 1
if (p < end && *p == '\'') p++; 1 0 1
459 1 LUA_SPAN("esh-s", ts, p);
460 1 continue;
461 }
462  
463 /* long string [=*[ */
464 254 ▶ if (c == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '[') { 0 254 254
465 0 const char *lp = p + 1;
466 0 int level = 0;
467 0 ▶ while (lp < end && *lp == '=') { level++; lp++; }
Branch: 2 branches
BranchTrueFalseTotal
while (lp < end && *lp == '=') { level++; lp++; } 0 0 0
while (lp < end && *lp == '=') { level++; lp++; } 0 0 0
468 0 ▶ if (lp < end && *lp == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (lp < end && *lp == '[') { 0 0 0
if (lp < end && *lp == '[') { 0 0 0
469 0 const char *ts = p;
470 0 lp++;
471 0 p = lp;
472 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
473 0 ▶ if (*p == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ']') { 0 0 0
474 0 const char *cp = p + 1;
475 int i;
476 0 ▶ for (i = 0; i < level && cp < end && *cp == '='; i++) cp++;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 0 0
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 0 0
for (i = 0; i < level && cp < end && *cp == '='; i++) cp++; 0 0 0
477 0 ▶ if (i == level && cp < end && *cp == ']') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == level && cp < end && *cp == ']') { 0 0 0
if (i == level && cp < end && *cp == ']') { 0 0 0
if (i == level && cp < end && *cp == ']') { 0 0 0
478 0 p = cp + 1;
479 0 break;
480 }
481 }
482 0 p++;
483 }
484 0 LUA_SPAN("esh-s", ts, p);
485 0 continue;
486 }
487 }
488  
489 /* number: 0x hex, decimal, float */
490 254 ▶ if (isdigit((unsigned char)c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isdigit((unsigned char)c)) { 9 245 254
491 9 const char *ts = p;
492 9 ▶ if (c == '0' && p + 1 < end &&
Branch: 2 branches
BranchTrueFalseTotal
if (c == '0' && p + 1 < end && 2 7 9
if (c == '0' && p + 1 < end && 2 0 2
493 2 ▶ (*(p + 1) == 'x' || *(p + 1) == 'X')) {
Branch: 2 branches
BranchTrueFalseTotal
(*(p + 1) == 'x' || *(p + 1) == 'X')) { 1 1 2
(*(p + 1) == 'x' || *(p + 1) == 'X')) { 0 1 1
494 1 p += 2;
495 3 ▶ while (p < end && isxdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isxdigit((unsigned char)*p)) p++; 2 1 3
while (p < end && isxdigit((unsigned char)*p)) p++; 2 0 2
496 1 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 0 1 1
if (p < end && *p == '.') { 0 0 0
497 0 p++;
498 0 ▶ while (p < end && isxdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isxdigit((unsigned char)*p)) p++; 0 0 0
while (p < end && isxdigit((unsigned char)*p)) p++; 0 0 0
499 }
500 1 ▶ if (p < end && (*p == 'p' || *p == 'P')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'p' || *p == 'P')) { 0 1 1
if (p < end && (*p == 'p' || *p == 'P')) { 0 0 0
if (p < end && (*p == 'p' || *p == 'P')) { 0 0 0
501 0 p++;
502 0 ▶ if (p < end && (*p == '+' || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
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
503 0 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 0 0 0
while (p < end && isdigit((unsigned char)*p)) p++; 0 0 0
504 }
505 } else {
506 19 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 17 2 19
while (p < end && isdigit((unsigned char)*p)) p++; 11 6 17
507 8 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 6 2 8
if (p < end && *p == '.') { 2 4 6
508 2 p++;
509 5 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 4 1 5
while (p < end && isdigit((unsigned char)*p)) p++; 3 1 4
510 }
511 8 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
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
512 0 p++;
513 0 ▶ if (p < end && (*p == '+' || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
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
514 0 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 0 0 0
while (p < end && isdigit((unsigned char)*p)) p++; 0 0 0
515 }
516 }
517 9 LUA_SPAN("esh-n", ts, p);
518 9 continue;
519 }
520  
521 /* identifier / keyword / builtin */
522 245 ▶ if (eshu_hl_isalpha_(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_hl_isalpha_(c)) { 109 136 245
523 109 const char *ts = p;
524 488 ▶ while (p < end && eshu_hl_isalnum_(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && eshu_hl_isalnum_(*p)) p++; 473 15 488
while (p < end && eshu_hl_isalnum_(*p)) p++; 379 94 473
525 109 eshu_hl_flush(&out, plain, ts);
526 109 plain = p;
527 109 ▶ if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_lua_kw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_lua_kw)) { 38 71 109
528 38 eshu_hl_span(&out, "esh-k", ts, p);
529 71 ▶ } else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_lua_bi)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_lua_bi)) { 24 47 71
530 24 eshu_hl_span(&out, "esh-b", ts, p);
531 } else {
532 47 eshu_hl_write_html(&out, ts, (size_t)(p - ts));
533 }
534 109 continue;
535 }
536  
537 136 p++;
538 }
539  
540 41 eshu_hl_flush(&out, plain, end);
541 41 eshu_buf_putc(&out, '\0');
542 41 *out_len = out.len - 1;
543 41 return out.data;
544  
545 #undef LUA_SPAN
546 }
547  
548 #endif /* ESHU_LUA_H */
« Eshu.xs ↑ Summary include/eshu_rust.h »