src/javacodegen.c

Tue, 23 Aug 2016 17:31:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:31:15 +0200
changeset 49
f86f0b054464
parent 48
b2724c711203
child 50
17408c3607ce
permissions
-rw-r--r--

cleans up includes

universe@21 1 /*
universe@21 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@21 3 *
universe@35 4 * Copyright 2016 Mike Becker. All rights reserved.
universe@21 5 *
universe@21 6 * Redistribution and use in source and binary forms, with or without
universe@21 7 * modification, are permitted provided that the following conditions are met:
universe@21 8 *
universe@21 9 * 1. Redistributions of source code must retain the above copyright
universe@21 10 * notice, this list of conditions and the following disclaimer.
universe@21 11 *
universe@21 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@21 13 * notice, this list of conditions and the following disclaimer in the
universe@21 14 * documentation and/or other materials provided with the distribution.
universe@21 15 *
universe@21 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@21 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@21 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@21 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@21 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@21 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@21 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@21 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@21 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@21 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@21 26 * POSSIBILITY OF SUCH DAMAGE.
universe@21 27 *
universe@21 28 */
universe@21 29
universe@21 30 #include "javacodegen.h"
universe@21 31
universe@21 32 const char* jkeywords[] = {
universe@21 33 "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
universe@21 34 "package", "synchronized", "boolean", "do", "if", "private", "this",
universe@21 35 "break", "double", "implements", "protected", "throw", "byte", "else",
universe@21 36 "import", "public", "throws", "case", "enum", "instanceof", "return",
universe@21 37 "transient", "catch", "extends", "int", "short", "try", "char", "final",
universe@21 38 "interface", "static", "void", "class", "finally", "long", "strictfp",
universe@21 39 "volatile", "const", "float", "native", "super", "while", NULL
universe@21 40 };
universe@21 41
universe@48 42 void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
universe@29 43
universe@47 44 /* TODO: try to replace this buffer */
universe@47 45 char wordbuf[WORDBUF_SIZE];
universe@47 46 sstr_t word;
universe@47 47 word.ptr = wordbuf; word.length = 0;
universe@47 48
universe@48 49 size_t sp = (size_t)-1;
universe@21 50 int isstring = 0, iscomment = 0, isimport = 0;
universe@28 51 char quote = '\0';
universe@21 52 int isescaping = 0;
universe@21 53
universe@47 54 if (*multiline_comment) {
universe@21 55 iscomment = 1;
universe@48 56 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 57 }
universe@21 58
universe@39 59 char c;
universe@39 60 do {
universe@39 61 c = src[++sp];
universe@39 62 if (!c) break;
universe@39 63
universe@21 64 /* comments */
universe@26 65 if (!isstring && c == '/') {
universe@47 66 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
universe@21 67 iscomment = 0;
universe@47 68 *multiline_comment = 0;
universe@48 69 ucx_buffer_puts(dest, "/</span>");
universe@21 70 continue;
universe@21 71 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
universe@21 72 iscomment = 1;
universe@47 73 *multiline_comment = (src[sp+1] == '*');
universe@48 74 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
universe@21 75 }
universe@21 76 }
universe@21 77
universe@21 78 if (iscomment) {
universe@21 79 if (c == '\n') {
universe@48 80 ucx_buffer_puts(dest, "</span>\n");
universe@48 81 } else {
universe@48 82 put_htmlescaped(dest, c);
universe@21 83 }
universe@21 84 } else if (isimport) {
universe@47 85 /* TODO: local imports */
universe@21 86 } else {
universe@21 87 /* strings */
universe@21 88 if (!isescaping && (c == '\'' || c == '\"')) {
universe@21 89 if (isstring) {
universe@48 90 put_htmlescaped(dest, c);
universe@28 91 if (c == quote) {
universe@28 92 isstring = 0;
universe@48 93 ucx_buffer_puts(dest, "</span>");
universe@28 94 } else {
universe@48 95 put_htmlescaped(dest, c);
universe@28 96 }
universe@21 97 } else {
universe@28 98 isstring = 1;
universe@28 99 quote = c;
universe@48 100 ucx_buffer_puts(dest,
universe@29 101 "<span class=\"c2html-string\">");
universe@48 102 put_htmlescaped(dest, c);
universe@21 103 }
universe@21 104 } else {
universe@21 105 if (isstring) {
universe@48 106 put_htmlescaped(dest, c);
universe@48 107 } else if (!check_alnumex(c)) {
universe@47 108 if (word.length > 0 && word.length < WORDBUF_SIZE) {
universe@21 109 int closespan = 1;
universe@47 110 if (check_keyword(word, jkeywords)) {
universe@48 111 ucx_buffer_puts(dest,
universe@29 112 "<span class=\"c2html-keyword\">");
universe@47 113 } else if (isupper(word.ptr[0])) {
universe@48 114 ucx_buffer_puts(dest,
universe@29 115 "<span class=\"c2html-type\">");
universe@47 116 } else if (word.ptr[0] == '@') {
universe@48 117 ucx_buffer_puts(dest,
universe@29 118 "<span class=\"c2html-directive\">");
universe@47 119 } else if (check_capsonly(word)) {
universe@48 120 ucx_buffer_puts(dest,
universe@29 121 "<span class=\"c2html-macroconst\">");
universe@21 122 } else {
universe@21 123 closespan = 0;
universe@21 124 }
universe@48 125 put_htmlescapedstr(dest, word);
universe@48 126
universe@21 127 if (closespan) {
universe@48 128 ucx_buffer_puts(dest, "</span>");
universe@21 129 }
universe@21 130 }
universe@47 131 word.length = 0;
universe@48 132 put_htmlescaped(dest, c);
universe@21 133 } else {
universe@21 134 /* read word */
universe@47 135 if (word.length < WORDBUF_SIZE) {
universe@47 136 word.ptr[word.length++] = c;
universe@47 137 } else if (word.length == WORDBUF_SIZE) {
universe@48 138 /* TODO: this will be removed */
universe@48 139 ucx_buffer_puts(dest,
universe@48 140 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
universe@48 141 word.length = 0;
universe@21 142 } else {
universe@48 143 put_htmlescaped(dest, c);
universe@21 144 }
universe@21 145 }
universe@21 146 }
universe@21 147
universe@21 148 isescaping = !isescaping & (c == '\\');
universe@21 149 }
universe@39 150 } while (c != '\n');
universe@21 151 }

mercurial