src/ccodegen.c

Tue, 23 Aug 2016 17:24:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:24:58 +0200
changeset 48
b2724c711203
parent 47
c39ecbbca7c0
child 49
f86f0b054464
permissions
-rw-r--r--

highlighter now use the UcxBuffer API for writing to the destination buffer

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "ccodegen.h"
#include "ucx/utils.h"
#include <string.h>
#include <ctype.h>

const char* ckeywords[] = {
    "auto", "break", "case", "char", "const", "continue", "default", "do",
    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    "long", "register", "return", "short", "signed", "sizeof", "static",
    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    "while", NULL
};

void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
    /* TODO: try to replace these buffers */
    char wordbuf[WORDBUF_SIZE];
    sstr_t word;
    word.ptr = wordbuf; word.length = 0;
    
    char includefilebuf[512];
    sstr_t includefile;
    includefile.ptr = includefilebuf;
    includefile.length = 0;
    
    size_t sp = (size_t)-1;
    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    char quote = '\0';
    int isescaping = 0;
    
    /* continue a multi line comment highlighting */
    if (*multiline_comment) {
        iscomment = 1;
        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    }

    char c;
    do {
        c = src[++sp];
        if (!c) break;
        
        /* comments */
        if (!isstring && c == '/') {
            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
                iscomment = 0;
                *multiline_comment = 0;
                ucx_buffer_puts(dest, "/</span>");
                continue;
            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                iscomment = 1;
                *multiline_comment = (src[sp+1] == '*');
                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
            }
        }

        if (iscomment) {
            if (c == '\n') {
                ucx_buffer_puts(dest, "</span>\n");
            } else {
                put_htmlescaped(dest, c);
            }
        } else if (isinclude) {
            if (c == '<') {
                ucx_buffer_puts(dest,
                        "<span class=\"c2html-stdinclude\">&lt;");
            } else if (c == '\"') {
                if (parseinclude) {
                    ucx_bprintf(dest, "\">%.*s\"</a>",
                            includefile.length, includefile.ptr);
                    parseinclude = 0;
                } else {
                    ucx_buffer_puts(dest,
                            "<a class=\"c2html-userinclude\" href=\"");
                    includefile.length = 0;
                    includefile.ptr[includefile.length++] = '\"';
                    parseinclude = 1;
                }
            } else if (c == '>') {
                ucx_buffer_puts(dest,  "&gt;</span>");
            } else {
                if (parseinclude) {
                    includefile.ptr[includefile.length++] = c;
                }
                put_htmlescaped(dest, c);
            }
        } else {
            /* strings */
            if (!isescaping && (c == '\'' || c == '\"')) {
                if (isstring) {
                    put_htmlescaped(dest, c);
                    if (c == quote) {
                        isstring = 0;
                        ucx_buffer_puts(dest, "</span>");
                    } else {
                        put_htmlescaped(dest, c);
                    }
                } else {
                    isstring = 1;
                    quote = c;
                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
                    put_htmlescaped(dest, c);
                }
            } else {
                if (isstring) {
                    put_htmlescaped(dest, c);
                } else if (!check_alnumex(c)) {
                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
                        int closespan = 1;
                        sstr_t typesuffix = ST("_t");
                        if (check_keyword(word, ckeywords)) {
                            ucx_buffer_puts(dest,
                                    "<span class=\"c2html-keyword\">");
                        } else if (sstrsuffix(word, typesuffix)) {
                            ucx_buffer_puts(dest,
                                "<span class=\"c2html-type\">");
                        } else if (word.ptr[0] == '#') {
                            isinclude = !sstrcmp(word, S("#include"));
                            ucx_buffer_puts(dest,
                                "<span class=\"c2html-directive\">");
                        } else if (check_capsonly(word)) {
                            ucx_buffer_puts(dest,
                                "<span class=\"c2html-macroconst\">");
                        } else {
                            closespan = 0;
                        }
                        put_htmlescapedstr(dest, word);
                        if (closespan) {
                            ucx_buffer_puts(dest, "</span>");
                        }
                    }
                    word.length = 0;
                    put_htmlescaped(dest, c);
                } else {
                    /* read word */
                    if (word.length < WORDBUF_SIZE) {
                        word.ptr[word.length++] = c;
                    } else if (word.length == WORDBUF_SIZE) {
                        /* TODO: this will be removed */
                        ucx_buffer_puts(dest,
                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
                        word.length = 0;
                    } else {
                        put_htmlescaped(dest, c);
                    }
                }
            }

            isescaping = !isescaping & (c == '\\');
        }
    } while (c != '\n');
}

mercurial