src/javacodegen.c

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
parent 36
be60c22cddfe
child 45
1f3835182aeb
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

     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 int check_jtype(char *word, size_t len) {
    45     return isupper(word[0]);
    46 }
    48 int check_jdirective(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     memset(hltr->word, 0, WORDBUF_SIZE);
    57     size_t wp = 0, sp = (size_t)-1, dp = 0;
    58     int isstring = 0, iscomment = 0, isimport = 0;
    59     char quote = '\0';
    60     int isescaping = 0;
    62     if (hltr->iscommentml) {
    63         iscomment = 1;
    64         memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    65     }
    67     char c;
    68     do {
    69         c = src[++sp];
    70         if (!c) break;
    72         /* comments */
    73         if (!isstring && c == '/') {
    74             if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
    75                 iscomment = 0;
    76                 hltr->iscommentml = 0;
    77                 memcpy_const(dest, dp, "/</span>");
    78                 continue;
    79             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    80                 iscomment = 1;
    81                 hltr->iscommentml = (src[sp+1] == '*');
    82                 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    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 (isimport) {
    93             // TODO: local imports
    94         } else {
    95             /* strings */
    96             if (!isescaping && (c == '\'' || c == '\"')) {
    97                 if (isstring) {
    98                     dp = writeescapedchar(dest, dp, c);
    99                     if (c == quote) {
   100                         isstring = 0;
   101                         memcpy_const(dest, dp, "</span>");
   102                     } else {
   103                         dp = writeescapedchar(dest, dp, c);
   104                     }
   105                 } else {
   106                     isstring = 1;
   107                     quote = c;
   108                     memcpy_const(dest, dp,
   109                         "<span class=\"c2html-string\">");
   110                     dp = writeescapedchar(dest, dp, c);
   111                 }
   112             } else {
   113                 if (isstring) {
   114                     dp = writeescapedchar(dest, dp, c);
   115                 } else if (!iswordcharacter(c)) {
   116                     /* interpret word int_t */
   117                     if (wp > 0 && wp < WORDBUF_SIZE) {
   118                         int closespan = 1;
   119                         if (check_keyword(hltr->word, hltr->keywords)) {
   120                             memcpy_const(dest, dp, 
   121                                 "<span class=\"c2html-keyword\">");
   122                         } else if (hltr->istype(hltr->word, wp)) {
   123                             memcpy_const(dest, dp, 
   124                                 "<span class=\"c2html-type\">");
   125                         } else if (hltr->isdirective(hltr->word)) {
   126                             memcpy_const(dest, dp, 
   127                                 "<span class=\"c2html-directive\">");
   128                         } else if (check_capsonly(hltr->word, wp)) {
   129                             memcpy_const(dest, dp, 
   130                                 "<span class=\"c2html-macroconst\">");
   131                         } else {
   132                             closespan = 0;
   133                         }
   134                         for (int i = 0 ; i < wp ; i++) {
   135                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   136                         }
   137                         if (closespan) {
   138                             memcpy_const(dest, dp, "</span>");
   139                         }
   140                     }
   141                     memset(hltr->word, 0, WORDBUF_SIZE);
   142                     wp = 0;
   143                     dp = writeescapedchar(dest, dp, c);
   144                 } else {
   145                     /* read word */
   146                     if (wp < WORDBUF_SIZE) {
   147                         hltr->word[wp++] = c;
   148                     } else if (wp == WORDBUF_SIZE) {
   149                         for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   150                             dp = writeescapedchar(dest, dp, hltr->word[i]);
   151                         }
   152                         wp++;
   153                         dp = writeescapedchar(dest, dp, c);
   154                     } else {
   155                         dp = writeescapedchar(dest, dp, c);
   156                     }
   157                 }
   158             }
   160             isescaping = !isescaping & (c == '\\');
   161         }
   162     } while (c != '\n');
   163     dest[dp] = 0;
   164 }

mercurial