universe@21: /* universe@21: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@21: * universe@35: * Copyright 2016 Mike Becker. All rights reserved. universe@21: * universe@21: * Redistribution and use in source and binary forms, with or without universe@21: * modification, are permitted provided that the following conditions are met: universe@21: * universe@21: * 1. Redistributions of source code must retain the above copyright universe@21: * notice, this list of conditions and the following disclaimer. universe@21: * universe@21: * 2. Redistributions in binary form must reproduce the above copyright universe@21: * notice, this list of conditions and the following disclaimer in the universe@21: * documentation and/or other materials provided with the distribution. universe@21: * universe@21: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@21: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@21: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@21: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@21: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@21: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@21: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@21: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@21: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@21: * POSSIBILITY OF SUCH DAMAGE. universe@21: * universe@21: */ universe@21: universe@21: #include "ccodegen.h" universe@21: #include universe@21: #include universe@21: universe@21: const char* ckeywords[] = { universe@21: "auto", "break", "case", "char", "const", "continue", "default", "do", universe@21: "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", universe@21: "long", "register", "return", "short", "signed", "sizeof", "static", universe@21: "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", universe@21: "while", NULL universe@21: }; universe@21: universe@29: #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ universe@29: dp += sizeof(str)-1 universe@29: universe@47: void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) { universe@45: /* TODO: workaround for using old code with UcxBuffer */ universe@45: char *dest = destbuf->space + destbuf->pos; universe@21: universe@47: /* TODO: try to replace these buffers */ universe@47: char wordbuf[WORDBUF_SIZE]; universe@47: sstr_t word; universe@47: word.ptr = wordbuf; word.length = 0; universe@47: universe@47: char includefilebuf[512]; universe@47: sstr_t includefile; universe@47: includefile.ptr = includefilebuf; universe@47: includefile.length = 0; universe@47: universe@47: size_t sp = (size_t)-1, dp = 0; universe@21: int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; universe@28: char quote = '\0'; universe@21: int isescaping = 0; universe@31: universe@31: /* continue a multi line comment highlighting */ universe@47: if (*multiline_comment) { universe@21: iscomment = 1; universe@29: memcpy_const(dest, dp, ""); universe@21: } universe@21: universe@39: char c; universe@39: do { universe@39: c = src[++sp]; universe@39: if (!c) break; universe@39: universe@21: /* comments */ universe@26: if (!isstring && c == '/') { universe@47: if (*multiline_comment && sp > 0 && src[sp-1] == '*') { universe@21: iscomment = 0; universe@47: *multiline_comment = 0; universe@29: memcpy_const(dest, dp, "/"); universe@21: continue; universe@21: } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { universe@21: iscomment = 1; universe@47: *multiline_comment = (src[sp+1] == '*'); universe@29: memcpy_const(dest, dp, ""); universe@21: } universe@21: } universe@21: universe@21: if (iscomment) { universe@21: if (c == '\n') { universe@29: memcpy_const(dest, dp, ""); universe@21: } universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else if (isinclude) { universe@21: if (c == '<') { universe@29: memcpy_const(dest, dp, ""); universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else if (c == '\"') { universe@21: if (parseinclude) { universe@21: dest[dp++] = '\"'; universe@21: dest[dp++] = '>'; universe@47: memcpy(&(dest[dp]), includefile.ptr, includefile.length); universe@47: dp += includefile.length; universe@21: universe@21: dp = writeescapedchar(dest, dp, c); universe@29: memcpy_const(dest, dp, ""); universe@21: parseinclude = 0; universe@21: } else { universe@29: memcpy_const(dest, dp, universe@29: "') { universe@21: dp = writeescapedchar(dest, dp, c); universe@29: memcpy_const(dest, dp, ""); universe@21: } else { universe@21: if (parseinclude) { universe@47: includefile.ptr[includefile.length++] = c; universe@21: } universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } universe@21: } else { universe@21: /* strings */ universe@21: if (!isescaping && (c == '\'' || c == '\"')) { universe@21: if (isstring) { universe@29: dp = writeescapedchar(dest, dp, c); universe@28: if (c == quote) { universe@28: isstring = 0; universe@29: memcpy_const(dest, dp, ""); universe@28: } else { universe@28: dp = writeescapedchar(dest, dp, c); universe@28: } universe@21: } else { universe@28: isstring = 1; universe@28: quote = c; universe@29: memcpy_const(dest, dp, universe@29: ""); universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } universe@21: } else { universe@21: if (isstring) { universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else if (!iswordcharacter(c)) { universe@47: if (word.length > 0 && word.length < WORDBUF_SIZE) { universe@21: int closespan = 1; universe@47: sstr_t typesuffix = ST("_t"); universe@47: if (check_keyword(word, ckeywords)) { universe@29: memcpy_const(dest, dp, universe@29: ""); universe@47: } else if (sstrsuffix(word, typesuffix)) { universe@29: memcpy_const(dest, dp, universe@29: ""); universe@47: } else if (word.ptr[0] == '#') { universe@47: isinclude = !sstrcmp(word, S("#include")); universe@29: memcpy_const(dest, dp, universe@29: ""); universe@47: } else if (check_capsonly(word)) { universe@29: memcpy_const(dest, dp, universe@29: ""); universe@21: } else { universe@21: closespan = 0; universe@21: } universe@47: for (int i = 0 ; i < word.length ; i++) { universe@47: dp = writeescapedchar(dest, dp, word.ptr[i]); universe@21: } universe@21: if (closespan) { universe@29: memcpy_const(dest, dp, ""); universe@21: } universe@21: } universe@47: word.length = 0; universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else { universe@21: /* read word */ universe@47: if (word.length < WORDBUF_SIZE) { universe@47: word.ptr[word.length++] = c; universe@47: } else if (word.length == WORDBUF_SIZE) { universe@21: for (int i = 0 ; i < WORDBUF_SIZE ; i++) { universe@47: dp = writeescapedchar(dest, dp, word.ptr[i]); universe@21: } universe@47: word.length++; universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else { universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } universe@21: } universe@21: } universe@21: universe@21: isescaping = !isescaping & (c == '\\'); universe@21: } universe@39: } while (c != '\n'); universe@21: dest[dp] = 0; universe@45: universe@45: /* TODO: workaround */ universe@45: destbuf->pos += dp; universe@45: destbuf->size += dp; universe@21: }