Eshu

 view release on metacpan or  search on metacpan

include/eshu_ts.h  view on Meta::CPAN

                             const eshu_config_t *cfg, size_t *out_len) {
    eshu_ts_ctx_t  ctx;
    eshu_buf_t     out;
    const char    *p   = src;
    const char    *end = src + src_len;
    int            line_num = 1;

    eshu_ts_ctx_init(&ctx, cfg);
    ctx.cfg.lang = ESHU_LANG_TS;
    eshu_buf_init(&out, src_len + 256);

    while (p < end) {
        const char *eol = eshu_find_eol(p);
        if (eshu_in_range(cfg, line_num)) {
            eshu_js_process_line((eshu_js_ctx_t *)&ctx, &out, p, eol);
        } else {
            size_t saved = out.len;
            eshu_js_process_line((eshu_js_ctx_t *)&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++;
    }

    eshu_buf_putc(&out, '\0');
    out.len--;
    *out_len = out.len;
    return out.data;
}


#include "eshu_hl_util.h"

/* ══════════════════════════════════════════════════════════════════
 *  TypeScript highlighter
 * ══════════════════════════════════════════════════════════════════ */

static const char * const eshu_hl_ts_kw[] = {
    /* JS keywords */
    "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",
    "Infinity", "NaN",
    /* TS-only keywords */
    "abstract", "any", "as", "asserts", "bigint", "boolean",
    "declare", "enum", "from", "implements", "infer", "interface",
    "is", "keyof", "module", "namespace", "never", "number", "object",
    "override", "private", "protected", "public", "readonly",
    "satisfies", "string", "symbol", "type", "unique", "unknown",
    "defined?",
    NULL
};

static const char * const eshu_hl_ts_bi[] = {
    /* JS builtins */
    "Array", "ArrayBuffer", "Boolean", "DataView", "Date", "Error",
    "Float32Array", "Float64Array", "Function", "Generator",
    "Int8Array", "Int16Array", "Int32Array",
    "Map", "Math", "Number", "Object", "Promise", "Proxy",
    "RangeError", "ReferenceError", "RegExp", "Set", "String",
    "Symbol", "SyntaxError", "TypeError", "Uint8Array",
    "Uint16Array", "Uint32Array", "URIError", "WeakMap", "WeakSet",
    "JSON", "console", "document", "window", "globalThis",
    /* TS utility types */
    "Partial", "Required", "Readonly", "Record", "Pick", "Omit",
    "Exclude", "Extract", "NonNullable", "ReturnType", "InstanceType",
    "Parameters", "ConstructorParameters", "Awaited",
    NULL
};

static char *eshu_highlight_ts(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;

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

#define TS_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;
            while (p < end && *p != '\n') p++;
            TS_SPAN("esh-c", ts, p);
            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;



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