src/javacodegen.c

Tue, 23 Aug 2016 17:24:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:24:58 +0200
changeset 48
b2724c711203
parent 47
c39ecbbca7c0
child 49
f86f0b054464
permissions
-rw-r--r--

highlighter now use the UcxBuffer API for writing to the destination buffer

     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 void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
    46     /* TODO: try to replace this buffer */
    47     char wordbuf[WORDBUF_SIZE];
    48     sstr_t word;
    49     word.ptr = wordbuf; word.length = 0;
    51     size_t sp = (size_t)-1;
    52     int isstring = 0, iscomment = 0, isimport = 0;
    53     char quote = '\0';
    54     int isescaping = 0;
    56     if (*multiline_comment) {
    57         iscomment = 1;
    58         ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    59     }
    61     char c;
    62     do {
    63         c = src[++sp];
    64         if (!c) break;
    66         /* comments */
    67         if (!isstring && c == '/') {
    68             if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    69                 iscomment = 0;
    70                 *multiline_comment = 0;
    71                 ucx_buffer_puts(dest, "/</span>");
    72                 continue;
    73             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    74                 iscomment = 1;
    75                 *multiline_comment = (src[sp+1] == '*');
    76                 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    77             }
    78         }
    80         if (iscomment) {
    81             if (c == '\n') {
    82                 ucx_buffer_puts(dest, "</span>\n");
    83             } else {
    84                 put_htmlescaped(dest, c);
    85             }
    86         } else if (isimport) {
    87             /* TODO: local imports */
    88         } else {
    89             /* strings */
    90             if (!isescaping && (c == '\'' || c == '\"')) {
    91                 if (isstring) {
    92                     put_htmlescaped(dest, c);
    93                     if (c == quote) {
    94                         isstring = 0;
    95                         ucx_buffer_puts(dest, "</span>");
    96                     } else {
    97                         put_htmlescaped(dest, c);
    98                     }
    99                 } else {
   100                     isstring = 1;
   101                     quote = c;
   102                     ucx_buffer_puts(dest,
   103                         "<span class=\"c2html-string\">");
   104                     put_htmlescaped(dest, c);
   105                 }
   106             } else {
   107                 if (isstring) {
   108                     put_htmlescaped(dest, c);
   109                 } else if (!check_alnumex(c)) {
   110                     if (word.length > 0 && word.length < WORDBUF_SIZE) {
   111                         int closespan = 1;
   112                         if (check_keyword(word, jkeywords)) {
   113                             ucx_buffer_puts(dest,
   114                                 "<span class=\"c2html-keyword\">");
   115                         } else if (isupper(word.ptr[0])) {
   116                             ucx_buffer_puts(dest,
   117                                 "<span class=\"c2html-type\">");
   118                         } else if (word.ptr[0] == '@') {
   119                             ucx_buffer_puts(dest,
   120                                 "<span class=\"c2html-directive\">");
   121                         } else if (check_capsonly(word)) {
   122                             ucx_buffer_puts(dest,
   123                                 "<span class=\"c2html-macroconst\">");
   124                         } else {
   125                             closespan = 0;
   126                         }
   127                         put_htmlescapedstr(dest, word);
   129                         if (closespan) {
   130                             ucx_buffer_puts(dest, "</span>");
   131                         }
   132                     }
   133                     word.length = 0;
   134                     put_htmlescaped(dest, c);
   135                 } else {
   136                     /* read word */
   137                     if (word.length < WORDBUF_SIZE) {
   138                         word.ptr[word.length++] = c;
   139                     } else if (word.length == WORDBUF_SIZE) {
   140                         /* TODO: this will be removed */
   141                         ucx_buffer_puts(dest,
   142                                 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
   143                         word.length = 0;
   144                     } else {
   145                         put_htmlescaped(dest, c);
   146                     }
   147                 }
   148             }
   150             isescaping = !isescaping & (c == '\\');
   151         }
   152     } while (c != '\n');
   153 }

mercurial