src/ccodegen.c

Thu, 25 Aug 2016 12:16:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Aug 2016 12:16:57 +0200
changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
permissions
-rw-r--r--

replaces stack buffers with UCX buffers

universe@21 1 /*
universe@21 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@21 3 *
universe@35 4 * Copyright 2016 Mike Becker. All rights reserved.
universe@21 5 *
universe@21 6 * Redistribution and use in source and binary forms, with or without
universe@21 7 * modification, are permitted provided that the following conditions are met:
universe@21 8 *
universe@21 9 * 1. Redistributions of source code must retain the above copyright
universe@21 10 * notice, this list of conditions and the following disclaimer.
universe@21 11 *
universe@21 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@21 13 * notice, this list of conditions and the following disclaimer in the
universe@21 14 * documentation and/or other materials provided with the distribution.
universe@21 15 *
universe@21 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@21 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@21 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@21 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@21 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@21 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@21 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@21 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@21 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@21 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@21 26 * POSSIBILITY OF SUCH DAMAGE.
universe@21 27 *
universe@21 28 */
universe@21 29
universe@21 30 #include "ccodegen.h"
universe@21 31
universe@21 32 const char* ckeywords[] = {
universe@21 33 "auto", "break", "case", "char", "const", "continue", "default", "do",
universe@21 34 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
universe@21 35 "long", "register", "return", "short", "signed", "sizeof", "static",
universe@21 36 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
universe@21 37 "while", NULL
universe@21 38 };
universe@21 39
universe@51 40 void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
universe@51 41 /* reset buffers without clearing them */
universe@51 42 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
universe@51 43 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
universe@47 44
universe@51 45 /* alias the buffers for better handling */
universe@51 46 UcxBuffer *wbuf = hd->primary_buffer;
universe@51 47 UcxBuffer *ifilebuf = hd->secondary_buffer;
universe@47 48
universe@51 49 /* local information */
universe@48 50 size_t sp = (size_t)-1;
universe@21 51 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
universe@28 52 char quote = '\0';
universe@21 53 int isescaping = 0;
universe@31 54
universe@31 55 /* continue a multi line comment highlighting */
universe@51 56 if (hd->multiline_comment) {
universe@21 57 iscomment = 1;
universe@48 58 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 59 }
universe@21 60
universe@39 61 char c;
universe@39 62 do {
universe@39 63 c = src[++sp];
universe@39 64 if (!c) break;
universe@39 65
universe@21 66 /* comments */
universe@26 67 if (!isstring && c == '/') {
universe@51 68 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
universe@21 69 iscomment = 0;
universe@51 70 hd->multiline_comment = 0;
universe@48 71 ucx_buffer_puts(dest, "/</span>");
universe@21 72 continue;
universe@21 73 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
universe@21 74 iscomment = 1;
universe@51 75 hd->multiline_comment = (src[sp+1] == '*');
universe@48 76 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 77 }
universe@21 78 }
universe@21 79
universe@21 80 if (iscomment) {
universe@21 81 if (c == '\n') {
universe@48 82 ucx_buffer_puts(dest, "</span>\n");
universe@48 83 } else {
universe@48 84 put_htmlescaped(dest, c);
universe@21 85 }
universe@21 86 } else if (isinclude) {
universe@21 87 if (c == '<') {
universe@48 88 ucx_buffer_puts(dest,
universe@48 89 "<span class=\"c2html-stdinclude\">&lt;");
universe@21 90 } else if (c == '\"') {
universe@21 91 if (parseinclude) {
universe@51 92 ucx_buffer_puts(dest, "\">");
universe@51 93 ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
universe@51 94 ucx_buffer_puts(dest, "\"</a>");
universe@21 95 parseinclude = 0;
universe@21 96 } else {
universe@48 97 ucx_buffer_puts(dest,
universe@48 98 "<a class=\"c2html-userinclude\" href=\"");
universe@51 99 ucx_buffer_putc(ifilebuf, '\"');
universe@21 100 parseinclude = 1;
universe@21 101 }
universe@21 102 } else if (c == '>') {
universe@48 103 ucx_buffer_puts(dest, "&gt;</span>");
universe@21 104 } else {
universe@21 105 if (parseinclude) {
universe@51 106 ucx_buffer_putc(ifilebuf, c);
universe@21 107 }
universe@48 108 put_htmlescaped(dest, c);
universe@21 109 }
universe@21 110 } else {
universe@21 111 /* strings */
universe@21 112 if (!isescaping && (c == '\'' || c == '\"')) {
universe@21 113 if (isstring) {
universe@48 114 put_htmlescaped(dest, c);
universe@28 115 if (c == quote) {
universe@28 116 isstring = 0;
universe@48 117 ucx_buffer_puts(dest, "</span>");
universe@28 118 } else {
universe@48 119 put_htmlescaped(dest, c);
universe@28 120 }
universe@21 121 } else {
universe@28 122 isstring = 1;
universe@28 123 quote = c;
universe@48 124 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
universe@48 125 put_htmlescaped(dest, c);
universe@21 126 }
universe@21 127 } else {
universe@21 128 if (isstring) {
universe@48 129 put_htmlescaped(dest, c);
universe@51 130 } else if (!isalnum(c) && c!='_' && c!='#') {
universe@51 131 /* write buffered word, if any */
universe@51 132 if (wbuf->size > 0) {
universe@51 133 sstr_t word = sstrn(wbuf->space, wbuf->size);
universe@21 134 int closespan = 1;
universe@47 135 sstr_t typesuffix = ST("_t");
universe@47 136 if (check_keyword(word, ckeywords)) {
universe@48 137 ucx_buffer_puts(dest,
universe@48 138 "<span class=\"c2html-keyword\">");
universe@47 139 } else if (sstrsuffix(word, typesuffix)) {
universe@48 140 ucx_buffer_puts(dest,
universe@29 141 "<span class=\"c2html-type\">");
universe@47 142 } else if (word.ptr[0] == '#') {
universe@47 143 isinclude = !sstrcmp(word, S("#include"));
universe@48 144 ucx_buffer_puts(dest,
universe@29 145 "<span class=\"c2html-directive\">");
universe@47 146 } else if (check_capsonly(word)) {
universe@48 147 ucx_buffer_puts(dest,
universe@29 148 "<span class=\"c2html-macroconst\">");
universe@21 149 } else {
universe@21 150 closespan = 0;
universe@21 151 }
universe@48 152 put_htmlescapedstr(dest, word);
universe@21 153 if (closespan) {
universe@48 154 ucx_buffer_puts(dest, "</span>");
universe@21 155 }
universe@21 156 }
universe@51 157 wbuf->pos = wbuf->size = 0; /* reset word buffer */
universe@51 158
universe@51 159 /* write current character */
universe@48 160 put_htmlescaped(dest, c);
universe@21 161 } else {
universe@51 162 /* buffer the current word */
universe@51 163 ucx_buffer_putc(wbuf, c);
universe@21 164 }
universe@21 165 }
universe@21 166
universe@21 167 isescaping = !isescaping & (c == '\\');
universe@21 168 }
universe@39 169 } while (c != '\n');
universe@21 170 }

mercurial