src/ccodegen.c

Tue, 23 Aug 2016 16:34:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 16:34:02 +0200
changeset 47
c39ecbbca7c0
parent 46
534a4ef4143d
child 48
b2724c711203
permissions
-rw-r--r--

words (token) are now stored as sstr_t

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2016 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include "ccodegen.h"
    31 #include <string.h>
    32 #include <ctype.h>
    34 const char* ckeywords[] = {
    35     "auto", "break", "case", "char", "const", "continue", "default", "do",
    36     "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    37     "long", "register", "return", "short", "signed", "sizeof", "static",
    38     "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    39     "while", NULL
    40 };
    42 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
    43                                     dp += sizeof(str)-1
    45 void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
    46     /* TODO: workaround for using old code with UcxBuffer */
    47     char *dest = destbuf->space + destbuf->pos;
    49     /* TODO: try to replace these buffers */
    50     char wordbuf[WORDBUF_SIZE];
    51     sstr_t word;
    52     word.ptr = wordbuf; word.length = 0;
    54     char includefilebuf[512];
    55     sstr_t includefile;
    56     includefile.ptr = includefilebuf;
    57     includefile.length = 0;
    59     size_t sp = (size_t)-1, dp = 0;
    60     int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    61     char quote = '\0';
    62     int isescaping = 0;
    64     /* continue a multi line comment highlighting */
    65     if (*multiline_comment) {
    66         iscomment = 1;
    67         memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    68     }
    70     char c;
    71     do {
    72         c = src[++sp];
    73         if (!c) break;
    75         /* comments */
    76         if (!isstring && c == '/') {
    77             if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    78                 iscomment = 0;
    79                 *multiline_comment = 0;
    80                 memcpy_const(dest, dp, "/</span>");
    81                 continue;
    82             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    83                 iscomment = 1;
    84                 *multiline_comment = (src[sp+1] == '*');
    85                 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    86             }
    87         }
    89         if (iscomment) {
    90             if (c == '\n') {
    91                 memcpy_const(dest, dp,  "</span>");
    92             }
    93             dp = writeescapedchar(dest, dp, c);
    94         } else if (isinclude) {
    95             if (c == '<') {
    96                 memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
    97                 dp = writeescapedchar(dest, dp, c);
    98             } else if (c == '\"') {
    99                 if (parseinclude) {
   100                     dest[dp++] = '\"';
   101                     dest[dp++] = '>';
   102                     memcpy(&(dest[dp]), includefile.ptr, includefile.length);
   103                     dp += includefile.length;
   105                     dp = writeescapedchar(dest, dp, c);
   106                     memcpy_const(dest, dp, "</a>");
   107                     parseinclude = 0;
   108                 } else {
   109                     memcpy_const(dest, dp,
   110                         "<a class=\"c2html-userinclude\" href=");
   111                     dp = writeescapedchar(dest, dp, c);
   112                     includefile.length = 0;
   113                     includefile.ptr[includefile.length++] = '\"';
   114                     parseinclude = 1;
   115                 }
   116             } else if (c == '>') {
   117                 dp = writeescapedchar(dest, dp, c);
   118                 memcpy_const(dest, dp, "</span>");
   119             } else {
   120                 if (parseinclude) {
   121                     includefile.ptr[includefile.length++] = c;
   122                 }
   123                 dp = writeescapedchar(dest, dp, c);
   124             }
   125         } else {
   126             /* strings */
   127             if (!isescaping && (c == '\'' || c == '\"')) {
   128                 if (isstring) {
   129                     dp = writeescapedchar(dest, dp, c);
   130                     if (c == quote) {
   131                         isstring = 0;
   132                         memcpy_const(dest, dp, "</span>");
   133                     } else {
   134                         dp = writeescapedchar(dest, dp, c);
   135                     }
   136                 } else {
   137                     isstring = 1;
   138                     quote = c;
   139                     memcpy_const(dest, dp,
   140                         "<span class=\"c2html-string\">");
   141                     dp = writeescapedchar(dest, dp, c);
   142                 }
   143             } else {
   144                 if (isstring) {
   145                     dp = writeescapedchar(dest, dp, c);
   146                 } else if (!iswordcharacter(c)) {
   147                     if (word.length > 0 && word.length < WORDBUF_SIZE) {
   148                         int closespan = 1;
   149                         sstr_t typesuffix = ST("_t");
   150                         if (check_keyword(word, ckeywords)) {
   151                             memcpy_const(dest, dp, 
   152                                 "<span class=\"c2html-keyword\">");
   153                         } else if (sstrsuffix(word, typesuffix)) {
   154                             memcpy_const(dest, dp, 
   155                                 "<span class=\"c2html-type\">");
   156                         } else if (word.ptr[0] == '#') {
   157                             isinclude = !sstrcmp(word, S("#include"));
   158                             memcpy_const(dest, dp, 
   159                                 "<span class=\"c2html-directive\">");
   160                         } else if (check_capsonly(word)) {
   161                             memcpy_const(dest, dp, 
   162                                 "<span class=\"c2html-macroconst\">");
   163                         } else {
   164                             closespan = 0;
   165                         }
   166                         for (int i = 0 ; i < word.length ; i++) {
   167                             dp = writeescapedchar(dest, dp, word.ptr[i]);
   168                         }
   169                         if (closespan) {
   170                             memcpy_const(dest, dp, "</span>");
   171                         }
   172                     }
   173                     word.length = 0;
   174                     dp = writeescapedchar(dest, dp, c);
   175                 } else {
   176                     /* read word */
   177                     if (word.length < WORDBUF_SIZE) {
   178                         word.ptr[word.length++] = c;
   179                     } else if (word.length == WORDBUF_SIZE) {
   180                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   181                             dp = writeescapedchar(dest, dp, word.ptr[i]);
   182                         }
   183                         word.length++;
   184                         dp = writeescapedchar(dest, dp, c);
   185                     } else {
   186                         dp = writeescapedchar(dest, dp, c);
   187                     }
   188                 }
   189             }
   191             isescaping = !isescaping & (c == '\\');
   192         }
   193     } while (c != '\n');
   194     dest[dp] = 0;
   196     /* TODO: workaround */
   197     destbuf->pos += dp;
   198     destbuf->size += dp;
   199 }

mercurial