Tue, 23 Aug 2016 16:34:02 +0200
words (token) are now stored as sstr_t
/* * 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 <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 }; #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ dp += sizeof(str)-1 void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) { /* TODO: workaround for using old code with UcxBuffer */ char *dest = destbuf->space + destbuf->pos; /* 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, dp = 0; 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; memcpy_const(dest, dp, "<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; memcpy_const(dest, dp, "/</span>"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; *multiline_comment = (src[sp+1] == '*'); memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); } } if (iscomment) { if (c == '\n') { memcpy_const(dest, dp, "</span>"); } dp = writeescapedchar(dest, dp, c); } else if (isinclude) { if (c == '<') { memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">"); dp = writeescapedchar(dest, dp, c); } else if (c == '\"') { if (parseinclude) { dest[dp++] = '\"'; dest[dp++] = '>'; memcpy(&(dest[dp]), includefile.ptr, includefile.length); dp += includefile.length; dp = writeescapedchar(dest, dp, c); memcpy_const(dest, dp, "</a>"); parseinclude = 0; } else { memcpy_const(dest, dp, "<a class=\"c2html-userinclude\" href="); dp = writeescapedchar(dest, dp, c); includefile.length = 0; includefile.ptr[includefile.length++] = '\"'; parseinclude = 1; } } else if (c == '>') { dp = writeescapedchar(dest, dp, c); memcpy_const(dest, dp, "</span>"); } else { if (parseinclude) { includefile.ptr[includefile.length++] = c; } dp = writeescapedchar(dest, dp, c); } } else { /* strings */ if (!isescaping && (c == '\'' || c == '\"')) { if (isstring) { dp = writeescapedchar(dest, dp, c); if (c == quote) { isstring = 0; memcpy_const(dest, dp, "</span>"); } else { dp = writeescapedchar(dest, dp, c); } } else { isstring = 1; quote = c; memcpy_const(dest, dp, "<span class=\"c2html-string\">"); dp = writeescapedchar(dest, dp, c); } } else { if (isstring) { dp = writeescapedchar(dest, dp, c); } else if (!iswordcharacter(c)) { if (word.length > 0 && word.length < WORDBUF_SIZE) { int closespan = 1; sstr_t typesuffix = ST("_t"); if (check_keyword(word, ckeywords)) { memcpy_const(dest, dp, "<span class=\"c2html-keyword\">"); } else if (sstrsuffix(word, typesuffix)) { memcpy_const(dest, dp, "<span class=\"c2html-type\">"); } else if (word.ptr[0] == '#') { isinclude = !sstrcmp(word, S("#include")); memcpy_const(dest, dp, "<span class=\"c2html-directive\">"); } else if (check_capsonly(word)) { memcpy_const(dest, dp, "<span class=\"c2html-macroconst\">"); } else { closespan = 0; } for (int i = 0 ; i < word.length ; i++) { dp = writeescapedchar(dest, dp, word.ptr[i]); } if (closespan) { memcpy_const(dest, dp, "</span>"); } } word.length = 0; dp = writeescapedchar(dest, dp, c); } else { /* read word */ if (word.length < WORDBUF_SIZE) { word.ptr[word.length++] = c; } else if (word.length == WORDBUF_SIZE) { for (int i = 0 ; i < WORDBUF_SIZE ; i++) { dp = writeescapedchar(dest, dp, word.ptr[i]); } word.length++; dp = writeescapedchar(dest, dp, c); } else { dp = writeescapedchar(dest, dp, c); } } } isescaping = !isescaping & (c == '\\'); } } while (c != '\n'); dest[dp] = 0; /* TODO: workaround */ destbuf->pos += dp; destbuf->size += dp; }