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

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

mercurial