Eshu

 view release on metacpan or  search on metacpan

include/eshu_js.h  view on Meta::CPAN

	eshu_buf_init(&out, src_len + 256);

	{
		int line_num = 1;
		while (p < end) {
			const char *eol = eshu_find_eol(p);

			if (eshu_in_range(cfg, line_num)) {
				eshu_js_process_line(&ctx, &out, p, eol);
			} else {
				/* Outside range: scan for state, emit verbatim */
				size_t saved = out.len;
				eshu_js_process_line(&ctx, &out, p, eol);
				out.len = saved;
				eshu_buf_write_trimmed(&out, p, (int)(eol - p));
				eshu_buf_putc(&out, '\n');
			}

			p = eol;
			if (*p == '\n') p++;
			line_num++;
		}
	}

	/* NUL-terminate */
	eshu_buf_putc(&out, '\0');
	out.len--;

	*out_len = out.len;
	result = out.data;
	return result;
}


#include "eshu_hl_util.h"

/* ══════════════════════════════════════════════════════════════════
 *  JavaScript keyword list
 * ══════════════════════════════════════════════════════════════════ */

static const char * const eshu_hl_js_kw[] = {
    "async", "await",
    "break",
    "case", "catch", "class", "const", "continue",
    "debugger", "default", "delete", "do",
    "else", "export", "extends",
    "false", "finally", "for", "function",
    "get",
    "if", "import", "in", "instanceof",
    "let",
    "new", "null",
    "of",
    "return",
    "set", "static", "super", "switch",
    "this", "throw", "true", "try", "typeof",
    "undefined",
    "var", "void",
    "while", "with",
    "yield",
    /* built-ins often treated as keywords */
    "Infinity", "NaN",
    NULL
};

/* ══════════════════════════════════════════════════════════════════
 *  JavaScript highlighter
 * ══════════════════════════════════════════════════════════════════ */

static char *eshu_highlight_js(const char *src, size_t src_len, size_t *out_len) {
    eshu_buf_t out;
    const char *p     = src;
    const char *end   = src + src_len;
    const char *plain = p;
    int last_val      = 0;
    int tmpl_depth    = 0; /* template literal nesting */

    eshu_buf_init(&out, src_len * 2 + 64);

#define JS_SPAN(cls, ts, te) do { \
    eshu_hl_flush(&out, plain, (ts)); \
    eshu_hl_span(&out, (cls), (ts), (te)); \
    p = (te); plain = p; \
} while(0)

    while (p < end) {
        char c = *p;

        /* line comment */
        if (c == '/' && p + 1 < end && *(p + 1) == '/') {
            const char *ts = p; p += 2;
            while (p < end && *p != '\n') p++;
            JS_SPAN("esh-c", ts, p);
            last_val = 0; continue;
        }

        /* block comment */
        if (c == '/' && p + 1 < end && *(p + 1) == '*') {
            const char *ts = p; p += 2;
            while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++;
            if (p + 1 < end) p += 2;
            JS_SPAN("esh-c", ts, p);
            last_val = 0; continue;
        }

        /* template literal */
        if (c == '`') {
            const char *ts = p++;
            tmpl_depth++;
            while (p < end) {
                if (*p == '\\') { p++; if (p < end) p++; continue; }
                if (*p == '`') { p++; tmpl_depth--; break; }
                /* ${...} we skip without sub-highlighting for now */
                p++;
            }
            JS_SPAN("esh-s", ts, p);
            last_val = 1; continue;
        }

        /* string */
        if (c == '"' || c == '\'') {
            const char *ts = p++;



( run in 0.764 second using v1.01-cache-2.11-cpan-60884a8df76 )