src/ccodegen.c

Sat, 25 Apr 2015 19:01:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Apr 2015 19:01:16 +0200
changeset 28
1be8ea902ef4
parent 26
05c3c6842aef
child 29
ec6e97454e64
permissions
-rw-r--r--

fixed string highlighting when different quote symbol is in string

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 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 int isctype(char *word, size_t len) {
    43     return (word[len-2] == '_' && word[len-1] == 't');
    44 }
    46 int iscdirective(char *word) {
    47     return (word[0] == '#');
    48 }
    50 void cparseline(char *src, char *dest, highlighter_t *hltr) {
    51     size_t sp = 0, dp = 0;
    52     /* indent */
    53     while (isspace(src[sp])) {
    54         dest[dp++] = src[sp++];
    55     }
    57     memset(hltr->word, 0, WORDBUF_SIZE);
    58     size_t wp = 0, ifp = 0;
    59     int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    60     char quote = '\0';
    61     int isescaping = 0;
    63     if (hltr->iscommentml) {
    64         iscomment = 1;
    65         memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
    66         dp += 29;
    67     }
    69     for (char c = src[sp] ; c ; c=src[++sp]) {
    70         /* comments */
    71         if (!isstring && c == '/') {
    72             if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
    73                 iscomment = 0;
    74                 hltr->iscommentml = 0;
    75                 memcpy(&(dest[dp]), "/</span>", 8);
    76                 dp += 8;
    77                 continue;
    78             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    79                 iscomment = 1;
    80                 hltr->iscommentml = (src[sp+1] == '*');
    81                 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
    82                 dp += 29;
    83             }
    84         }
    86         if (iscomment) {
    87             if (c == '\n') {
    88                 memcpy(&(dest[dp]), "</span>", 7);
    89                 dp += 7;
    90             }
    91             dp = writeescapedchar(dest, dp, c);
    92         } else if (isinclude) {
    93             if (c == '<') {
    94                 memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
    95                 dp += 32;
    96                 dp = writeescapedchar(dest, dp, c);
    97             } else if (c == '\"') {
    98                 if (parseinclude) {
    99                     dest[dp++] = '\"';
   100                     dest[dp++] = '>';
   101                     memcpy(&(dest[dp]), hltr->includefile, ifp);
   102                     dp += ifp;
   104                     dp = writeescapedchar(dest, dp, c);
   105                     memcpy(&(dest[dp]), "</a>", 4);
   106                     dp += 4;
   107                     parseinclude = 0;
   108                 } else {
   109                     memcpy(&(dest[dp]),
   110                         "<a class=\"c2html-userinclude\" href=", 35);
   111                     dp += 35;
   112                     dp = writeescapedchar(dest, dp, c);
   113                     ifp = 0;
   114                     hltr->includefile[ifp++] = '\"';
   115                     parseinclude = 1;
   116                 }
   117             } else if (c == '>') {
   118                 dp = writeescapedchar(dest, dp, c);
   119                 memcpy(&(dest[dp]), "</span>", 7);
   120                 dp += 7;
   121             } else {
   122                 if (parseinclude) {
   123                     hltr->includefile[ifp++] = c;
   124                 }
   125                 dp = writeescapedchar(dest, dp, c);
   126             }
   127         } else {
   128             /* strings */
   129             if (!isescaping && (c == '\'' || c == '\"')) {
   130                 if (isstring) {
   131                     if (c == quote) {
   132                         isstring = 0;
   133                         memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
   134                         dp += 28;
   135                         dp = writeescapedchar(dest, dp, c);
   136                     } else {
   137                         dp = writeescapedchar(dest, dp, c);
   138                     }
   139                 } else {
   140                     isstring = 1;
   141                     quote = c;
   142                     dp = writeescapedchar(dest, dp, c);
   143                     memcpy(&(dest[dp]), "</span>", 7);
   144                     dp += 7;
   145                 }
   146             } else {
   147                 if (isstring) {
   148                     dp = writeescapedchar(dest, dp, c);
   149                 } else if (!iswordcharacter(c)) {
   150                     /* interpret word int_t */
   151                     if (wp > 0 && wp < WORDBUF_SIZE) {
   152                         int closespan = 1;
   153                         if (iskeyword(hltr->word, hltr->keywords)) {
   154                             memcpy(&(dest[dp]),
   155                                 "<span class=\"c2html-keyword\">", 29);
   156                             dp += 29;
   157                         } else if (hltr->istype(hltr->word, wp)) {
   158                             memcpy(&(dest[dp]),
   159                                 "<span class=\"c2html-type\">", 26);
   160                             dp += 26;
   161                         } else if (hltr->isdirective(hltr->word)) {
   162                             isinclude = !strncmp(
   163                                 "#include", hltr->word, WORDBUF_SIZE);
   164                             memcpy(&(dest[dp]),
   165                                 "<span class=\"c2html-directive\">", 31);
   166                             dp += 31;
   167                         } else if (iscapsonly(hltr->word, wp)) {
   168                             memcpy(&(dest[dp]),
   169                                 "<span class=\"c2html-macroconst\">", 32);
   170                             dp += 32;
   171                         } else {
   172                             closespan = 0;
   173                         }
   174                         for (int i = 0 ; i < wp ; i++) {
   175                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   176                         }
   177                         if (closespan) {
   178                             memcpy(&(dest[dp]), "</span>", 7);
   179                             dp += 7;
   180                         }
   181                     }
   182                     memset(hltr->word, 0, WORDBUF_SIZE);
   183                     wp = 0;
   184                     dp = writeescapedchar(dest, dp, c);
   185                 } else {
   186                     /* read word */
   187                     if (wp < WORDBUF_SIZE) {
   188                         hltr->word[wp++] = c;
   189                     } else if (wp == WORDBUF_SIZE) {
   190                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   191                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   192                         }
   193                         wp++;
   194                         dp = writeescapedchar(dest, dp, c);
   195                     } else {
   196                         dp = writeescapedchar(dest, dp, c);
   197                     }
   198                 }
   199             }
   201             isescaping = !isescaping & (c == '\\');
   202         }
   203     }
   204     dest[dp] = 0;
   205 }

mercurial