src/javacodegen.c

Sat, 25 Apr 2015 19:14:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Apr 2015 19:14:57 +0200
changeset 29
ec6e97454e64
parent 28
1be8ea902ef4
child 32
10389d866a4d
permissions
-rw-r--r--

introduced macro for constant string memcpy + fixed string highlight fix

     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 "javacodegen.h"
    31 #include <string.h>
    32 #include <ctype.h>
    34 const char* jkeywords[] = {
    35     "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
    36     "package", "synchronized", "boolean", "do", "if", "private", "this",
    37     "break", "double", "implements", "protected", "throw", "byte", "else",
    38     "import", "public", "throws", "case", "enum", "instanceof", "return",
    39     "transient", "catch", "extends", "int", "short", "try", "char", "final",
    40     "interface", "static", "void", "class", "finally", "long", "strictfp",
    41     "volatile", "const", "float", "native", "super", "while", NULL
    42 };
    44 int isjtype(char *word, size_t len) {
    45     return isupper(word[0]);
    46 }
    48 int isjdirective(char *word) {
    49     return word[0] == '@';
    50 }
    52 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
    53                                     dp += sizeof(str)-1
    55 void jparseline(char *src, char *dest, highlighter_t *hltr) {
    56     size_t sp = 0, dp = 0;
    57     /* indent */
    58     while (isspace(src[sp])) {
    59         dest[dp++] = src[sp++];
    60     }
    62     memset(hltr->word, 0, WORDBUF_SIZE);
    63     size_t wp = 0;
    64     int isstring = 0, iscomment = 0, isimport = 0;
    65     char quote = '\0';
    66     int isescaping = 0;
    68     if (hltr->iscommentml) {
    69         iscomment = 1;
    70         memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    71     }
    73     for (char c = src[sp] ; c ; c=src[++sp]) {
    74         /* comments */
    75         if (!isstring && c == '/') {
    76             if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
    77                 iscomment = 0;
    78                 hltr->iscommentml = 0;
    79                 memcpy_const(dest, dp, "/</span>");
    80                 continue;
    81             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    82                 iscomment = 1;
    83                 hltr->iscommentml = (src[sp+1] == '*');
    84                 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    85             }
    86         }
    88         if (iscomment) {
    89             if (c == '\n') {
    90                 memcpy(&(dest[dp]), "</span>", 7);
    91                 dp += 7;
    92             }
    93             dp = writeescapedchar(dest, dp, c);
    94         } else if (isimport) {
    95             // TODO: local imports
    96         } else {
    97             /* strings */
    98             if (!isescaping && (c == '\'' || c == '\"')) {
    99                 if (isstring) {
   100                     dp = writeescapedchar(dest, dp, c);
   101                     if (c == quote) {
   102                         isstring = 0;
   103                         memcpy_const(dest, dp, "</span>");
   104                     } else {
   105                         dp = writeescapedchar(dest, dp, c);
   106                     }
   107                 } else {
   108                     isstring = 1;
   109                     quote = c;
   110                     memcpy_const(dest, dp,
   111                         "<span class=\"c2html-string\">");
   112                     dp = writeescapedchar(dest, dp, c);
   113                 }
   114             } else {
   115                 if (isstring) {
   116                     dp = writeescapedchar(dest, dp, c);
   117                 } else if (!iswordcharacter(c)) {
   118                     /* interpret word int_t */
   119                     if (wp > 0 && wp < WORDBUF_SIZE) {
   120                         int closespan = 1;
   121                         if (iskeyword(hltr->word, hltr->keywords)) {
   122                             memcpy_const(dest, dp, 
   123                                 "<span class=\"c2html-keyword\">");
   124                         } else if (hltr->istype(hltr->word, wp)) {
   125                             memcpy_const(dest, dp, 
   126                                 "<span class=\"c2html-type\">");
   127                         } else if (hltr->isdirective(hltr->word)) {
   128                             memcpy_const(dest, dp, 
   129                                 "<span class=\"c2html-directive\">");
   130                         } else if (iscapsonly(hltr->word, wp)) {
   131                             memcpy_const(dest, dp, 
   132                                 "<span class=\"c2html-macroconst\">");
   133                         } else {
   134                             closespan = 0;
   135                         }
   136                         for (int i = 0 ; i < wp ; i++) {
   137                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   138                         }
   139                         if (closespan) {
   140                             memcpy_const(dest, dp, "</span>");
   141                         }
   142                     }
   143                     memset(hltr->word, 0, WORDBUF_SIZE);
   144                     wp = 0;
   145                     dp = writeescapedchar(dest, dp, c);
   146                 } else {
   147                     /* read word */
   148                     if (wp < WORDBUF_SIZE) {
   149                         hltr->word[wp++] = c;
   150                     } else if (wp == WORDBUF_SIZE) {
   151                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   152                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   153                         }
   154                         wp++;
   155                         dp = writeescapedchar(dest, dp, c);
   156                     } else {
   157                         dp = writeescapedchar(dest, dp, c);
   158                     }
   159                 }
   160             }
   162             isescaping = !isescaping & (c == '\\');
   163         }
   164     }
   165     dest[dp] = 0;
   166 }

mercurial