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

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@48 31 #include "ucx/utils.h"
universe@21 32 #include <string.h>
universe@21 33 #include <ctype.h>
universe@21 34
universe@21 35 const char* ckeywords[] = {
universe@21 36 "auto", "break", "case", "char", "const", "continue", "default", "do",
universe@21 37 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
universe@21 38 "long", "register", "return", "short", "signed", "sizeof", "static",
universe@21 39 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
universe@21 40 "while", NULL
universe@21 41 };
universe@21 42
universe@48 43 void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
universe@47 44 /* TODO: try to replace these buffers */
universe@47 45 char wordbuf[WORDBUF_SIZE];
universe@47 46 sstr_t word;
universe@47 47 word.ptr = wordbuf; word.length = 0;
universe@47 48
universe@47 49 char includefilebuf[512];
universe@47 50 sstr_t includefile;
universe@47 51 includefile.ptr = includefilebuf;
universe@47 52 includefile.length = 0;
universe@47 53
universe@48 54 size_t sp = (size_t)-1;
universe@21 55 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
universe@28 56 char quote = '\0';
universe@21 57 int isescaping = 0;
universe@31 58
universe@31 59 /* continue a multi line comment highlighting */
universe@47 60 if (*multiline_comment) {
universe@21 61 iscomment = 1;
universe@48 62 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 63 }
universe@21 64
universe@39 65 char c;
universe@39 66 do {
universe@39 67 c = src[++sp];
universe@39 68 if (!c) break;
universe@39 69
universe@21 70 /* comments */
universe@26 71 if (!isstring && c == '/') {
universe@47 72 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
universe@21 73 iscomment = 0;
universe@47 74 *multiline_comment = 0;
universe@48 75 ucx_buffer_puts(dest, "/</span>");
universe@21 76 continue;
universe@21 77 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
universe@21 78 iscomment = 1;
universe@47 79 *multiline_comment = (src[sp+1] == '*');
universe@48 80 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 81 }
universe@21 82 }
universe@21 83
universe@21 84 if (iscomment) {
universe@21 85 if (c == '\n') {
universe@48 86 ucx_buffer_puts(dest, "</span>\n");
universe@48 87 } else {
universe@48 88 put_htmlescaped(dest, c);
universe@21 89 }
universe@21 90 } else if (isinclude) {
universe@21 91 if (c == '<') {
universe@48 92 ucx_buffer_puts(dest,
universe@48 93 "<span class=\"c2html-stdinclude\">&lt;");
universe@21 94 } else if (c == '\"') {
universe@21 95 if (parseinclude) {
universe@48 96 ucx_bprintf(dest, "\">%.*s\"</a>",
universe@48 97 includefile.length, includefile.ptr);
universe@21 98 parseinclude = 0;
universe@21 99 } else {
universe@48 100 ucx_buffer_puts(dest,
universe@48 101 "<a class=\"c2html-userinclude\" href=\"");
universe@47 102 includefile.length = 0;
universe@47 103 includefile.ptr[includefile.length++] = '\"';
universe@21 104 parseinclude = 1;
universe@21 105 }
universe@21 106 } else if (c == '>') {
universe@48 107 ucx_buffer_puts(dest, "&gt;</span>");
universe@21 108 } else {
universe@21 109 if (parseinclude) {
universe@47 110 includefile.ptr[includefile.length++] = c;
universe@21 111 }
universe@48 112 put_htmlescaped(dest, c);
universe@21 113 }
universe@21 114 } else {
universe@21 115 /* strings */
universe@21 116 if (!isescaping && (c == '\'' || c == '\"')) {
universe@21 117 if (isstring) {
universe@48 118 put_htmlescaped(dest, c);
universe@28 119 if (c == quote) {
universe@28 120 isstring = 0;
universe@48 121 ucx_buffer_puts(dest, "</span>");
universe@28 122 } else {
universe@48 123 put_htmlescaped(dest, c);
universe@28 124 }
universe@21 125 } else {
universe@28 126 isstring = 1;
universe@28 127 quote = c;
universe@48 128 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
universe@48 129 put_htmlescaped(dest, c);
universe@21 130 }
universe@21 131 } else {
universe@21 132 if (isstring) {
universe@48 133 put_htmlescaped(dest, c);
universe@48 134 } else if (!check_alnumex(c)) {
universe@47 135 if (word.length > 0 && word.length < WORDBUF_SIZE) {
universe@21 136 int closespan = 1;
universe@47 137 sstr_t typesuffix = ST("_t");
universe@47 138 if (check_keyword(word, ckeywords)) {
universe@48 139 ucx_buffer_puts(dest,
universe@48 140 "<span class=\"c2html-keyword\">");
universe@47 141 } else if (sstrsuffix(word, typesuffix)) {
universe@48 142 ucx_buffer_puts(dest,
universe@29 143 "<span class=\"c2html-type\">");
universe@47 144 } else if (word.ptr[0] == '#') {
universe@47 145 isinclude = !sstrcmp(word, S("#include"));
universe@48 146 ucx_buffer_puts(dest,
universe@29 147 "<span class=\"c2html-directive\">");
universe@47 148 } else if (check_capsonly(word)) {
universe@48 149 ucx_buffer_puts(dest,
universe@29 150 "<span class=\"c2html-macroconst\">");
universe@21 151 } else {
universe@21 152 closespan = 0;
universe@21 153 }
universe@48 154 put_htmlescapedstr(dest, word);
universe@21 155 if (closespan) {
universe@48 156 ucx_buffer_puts(dest, "</span>");
universe@21 157 }
universe@21 158 }
universe@47 159 word.length = 0;
universe@48 160 put_htmlescaped(dest, c);
universe@21 161 } else {
universe@21 162 /* read word */
universe@47 163 if (word.length < WORDBUF_SIZE) {
universe@47 164 word.ptr[word.length++] = c;
universe@47 165 } else if (word.length == WORDBUF_SIZE) {
universe@48 166 /* TODO: this will be removed */
universe@48 167 ucx_buffer_puts(dest,
universe@48 168 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
universe@48 169 word.length = 0;
universe@21 170 } else {
universe@48 171 put_htmlescaped(dest, c);
universe@21 172 }
universe@21 173 }
universe@21 174 }
universe@21 175
universe@21 176 isescaping = !isescaping & (c == '\\');
universe@21 177 }
universe@39 178 } while (c != '\n');
universe@21 179 }

mercurial