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