src/javacodegen.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 "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 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
    45                                     dp += sizeof(str)-1
    47 void jparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
    48     /* TODO: workaround for using old code with UcxBuffer */
    49     char *dest = destbuf->space + destbuf->pos;
    51     /* TODO: try to replace this buffer */
    52     char wordbuf[WORDBUF_SIZE];
    53     sstr_t word;
    54     word.ptr = wordbuf; word.length = 0;
    56     size_t sp = (size_t)-1, dp = 0;
    57     int isstring = 0, iscomment = 0, isimport = 0;
    58     char quote = '\0';
    59     int isescaping = 0;
    61     if (*multiline_comment) {
    62         iscomment = 1;
    63         memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    64     }
    66     char c;
    67     do {
    68         c = src[++sp];
    69         if (!c) break;
    71         /* comments */
    72         if (!isstring && c == '/') {
    73             if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    74                 iscomment = 0;
    75                 *multiline_comment = 0;
    76                 memcpy_const(dest, dp, "/</span>");
    77                 continue;
    78             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    79                 iscomment = 1;
    80                 *multiline_comment = (src[sp+1] == '*');
    81                 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    82             }
    83         }
    85         if (iscomment) {
    86             if (c == '\n') {
    87                 memcpy(&(dest[dp]), "</span>", 7);
    88                 dp += 7;
    89             }
    90             dp = writeescapedchar(dest, dp, c);
    91         } else if (isimport) {
    92             /* TODO: local imports */
    93         } else {
    94             /* strings */
    95             if (!isescaping && (c == '\'' || c == '\"')) {
    96                 if (isstring) {
    97                     dp = writeescapedchar(dest, dp, c);
    98                     if (c == quote) {
    99                         isstring = 0;
   100                         memcpy_const(dest, dp, "</span>");
   101                     } else {
   102                         dp = writeescapedchar(dest, dp, c);
   103                     }
   104                 } else {
   105                     isstring = 1;
   106                     quote = c;
   107                     memcpy_const(dest, dp,
   108                         "<span class=\"c2html-string\">");
   109                     dp = writeescapedchar(dest, dp, c);
   110                 }
   111             } else {
   112                 if (isstring) {
   113                     dp = writeescapedchar(dest, dp, c);
   114                 } else if (!iswordcharacter(c)) {
   115                     if (word.length > 0 && word.length < WORDBUF_SIZE) {
   116                         int closespan = 1;
   117                         if (check_keyword(word, jkeywords)) {
   118                             memcpy_const(dest, dp, 
   119                                 "<span class=\"c2html-keyword\">");
   120                         } else if (isupper(word.ptr[0])) {
   121                             memcpy_const(dest, dp, 
   122                                 "<span class=\"c2html-type\">");
   123                         } else if (word.ptr[0] == '@') {
   124                             memcpy_const(dest, dp, 
   125                                 "<span class=\"c2html-directive\">");
   126                         } else if (check_capsonly(word)) {
   127                             memcpy_const(dest, dp, 
   128                                 "<span class=\"c2html-macroconst\">");
   129                         } else {
   130                             closespan = 0;
   131                         }
   132                         for (int i = 0 ; i < word.length ; i++) {
   133                             dp = writeescapedchar(dest, dp, word.ptr[i]);
   134                         }
   135                         if (closespan) {
   136                             memcpy_const(dest, dp, "</span>");
   137                         }
   138                     }
   139                     word.length = 0;
   140                     dp = writeescapedchar(dest, dp, c);
   141                 } else {
   142                     /* read word */
   143                     if (word.length < WORDBUF_SIZE) {
   144                         word.ptr[word.length++] = c;
   145                     } else if (word.length == WORDBUF_SIZE) {
   146                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   147                             dp = writeescapedchar(dest, dp, word.ptr[i]);
   148                         }
   149                         word.length++;
   150                         dp = writeescapedchar(dest, dp, c);
   151                     } else {
   152                         dp = writeescapedchar(dest, dp, c);
   153                     }
   154                 }
   155             }
   157             isescaping = !isescaping & (c == '\\');
   158         }
   159     } while (c != '\n');
   160     dest[dp] = 0;
   162     /* TODO: workaround */
   163     destbuf->pos += dp;
   164     destbuf->size += dp;
   165 }

mercurial