src/ccodegen.c

changeset 52
33ded421c512
parent 51
f25ba6fd7a08
child 53
5e47a26a16f0
     1.1 --- a/src/ccodegen.c	Thu Aug 25 12:16:57 2016 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,170 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2016 Mike Becker. All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - *
    1.31 - */
    1.32 -
    1.33 -#include "ccodegen.h"
    1.34 -
    1.35 -const char* ckeywords[] = {
    1.36 -    "auto", "break", "case", "char", "const", "continue", "default", "do",
    1.37 -    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    1.38 -    "long", "register", "return", "short", "signed", "sizeof", "static",
    1.39 -    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    1.40 -    "while", NULL
    1.41 -};
    1.42 -
    1.43 -void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    1.44 -    /* reset buffers without clearing them */
    1.45 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    1.46 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    1.47 -    
    1.48 -    /* alias the buffers for better handling */
    1.49 -    UcxBuffer *wbuf = hd->primary_buffer;
    1.50 -    UcxBuffer *ifilebuf = hd->secondary_buffer;
    1.51 -    
    1.52 -    /* local information */
    1.53 -    size_t sp = (size_t)-1;
    1.54 -    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    1.55 -    char quote = '\0';
    1.56 -    int isescaping = 0;
    1.57 -    
    1.58 -    /* continue a multi line comment highlighting */
    1.59 -    if (hd->multiline_comment) {
    1.60 -        iscomment = 1;
    1.61 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    1.62 -    }
    1.63 -
    1.64 -    char c;
    1.65 -    do {
    1.66 -        c = src[++sp];
    1.67 -        if (!c) break;
    1.68 -        
    1.69 -        /* comments */
    1.70 -        if (!isstring && c == '/') {
    1.71 -            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    1.72 -                iscomment = 0;
    1.73 -                hd->multiline_comment = 0;
    1.74 -                ucx_buffer_puts(dest, "/</span>");
    1.75 -                continue;
    1.76 -            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    1.77 -                iscomment = 1;
    1.78 -                hd->multiline_comment = (src[sp+1] == '*');
    1.79 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    1.80 -            }
    1.81 -        }
    1.82 -
    1.83 -        if (iscomment) {
    1.84 -            if (c == '\n') {
    1.85 -                ucx_buffer_puts(dest, "</span>\n");
    1.86 -            } else {
    1.87 -                put_htmlescaped(dest, c);
    1.88 -            }
    1.89 -        } else if (isinclude) {
    1.90 -            if (c == '<') {
    1.91 -                ucx_buffer_puts(dest,
    1.92 -                        "<span class=\"c2html-stdinclude\">&lt;");
    1.93 -            } else if (c == '\"') {
    1.94 -                if (parseinclude) {
    1.95 -                    ucx_buffer_puts(dest, "\">");
    1.96 -                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
    1.97 -                    ucx_buffer_puts(dest, "\"</a>");
    1.98 -                    parseinclude = 0;
    1.99 -                } else {
   1.100 -                    ucx_buffer_puts(dest,
   1.101 -                            "<a class=\"c2html-userinclude\" href=\"");
   1.102 -                    ucx_buffer_putc(ifilebuf, '\"');
   1.103 -                    parseinclude = 1;
   1.104 -                }
   1.105 -            } else if (c == '>') {
   1.106 -                ucx_buffer_puts(dest,  "&gt;</span>");
   1.107 -            } else {
   1.108 -                if (parseinclude) {
   1.109 -                    ucx_buffer_putc(ifilebuf, c);
   1.110 -                }
   1.111 -                put_htmlescaped(dest, c);
   1.112 -            }
   1.113 -        } else {
   1.114 -            /* strings */
   1.115 -            if (!isescaping && (c == '\'' || c == '\"')) {
   1.116 -                if (isstring) {
   1.117 -                    put_htmlescaped(dest, c);
   1.118 -                    if (c == quote) {
   1.119 -                        isstring = 0;
   1.120 -                        ucx_buffer_puts(dest, "</span>");
   1.121 -                    } else {
   1.122 -                        put_htmlescaped(dest, c);
   1.123 -                    }
   1.124 -                } else {
   1.125 -                    isstring = 1;
   1.126 -                    quote = c;
   1.127 -                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   1.128 -                    put_htmlescaped(dest, c);
   1.129 -                }
   1.130 -            } else {
   1.131 -                if (isstring) {
   1.132 -                    put_htmlescaped(dest, c);
   1.133 -                } else if (!isalnum(c) && c!='_' && c!='#') {
   1.134 -                    /* write buffered word, if any */
   1.135 -                    if (wbuf->size > 0) {
   1.136 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   1.137 -                        int closespan = 1;
   1.138 -                        sstr_t typesuffix = ST("_t");
   1.139 -                        if (check_keyword(word, ckeywords)) {
   1.140 -                            ucx_buffer_puts(dest,
   1.141 -                                    "<span class=\"c2html-keyword\">");
   1.142 -                        } else if (sstrsuffix(word, typesuffix)) {
   1.143 -                            ucx_buffer_puts(dest,
   1.144 -                                "<span class=\"c2html-type\">");
   1.145 -                        } else if (word.ptr[0] == '#') {
   1.146 -                            isinclude = !sstrcmp(word, S("#include"));
   1.147 -                            ucx_buffer_puts(dest,
   1.148 -                                "<span class=\"c2html-directive\">");
   1.149 -                        } else if (check_capsonly(word)) {
   1.150 -                            ucx_buffer_puts(dest,
   1.151 -                                "<span class=\"c2html-macroconst\">");
   1.152 -                        } else {
   1.153 -                            closespan = 0;
   1.154 -                        }
   1.155 -                        put_htmlescapedstr(dest, word);
   1.156 -                        if (closespan) {
   1.157 -                            ucx_buffer_puts(dest, "</span>");
   1.158 -                        }
   1.159 -                    }
   1.160 -                    wbuf->pos = wbuf->size = 0; /* reset word buffer */
   1.161 -                    
   1.162 -                    /* write current character */
   1.163 -                    put_htmlescaped(dest, c);
   1.164 -                } else {
   1.165 -                    /* buffer the current word */
   1.166 -                    ucx_buffer_putc(wbuf, c);
   1.167 -                }
   1.168 -            }
   1.169 -
   1.170 -            isescaping = !isescaping & (c == '\\');
   1.171 -        }
   1.172 -    } while (c != '\n');
   1.173 -}

mercurial