1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/javacodegen.c Thu Jan 23 09:19:37 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2014 Mike Becker. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + * 1.31 + */ 1.32 + 1.33 +#include "javacodegen.h" 1.34 +#include <string.h> 1.35 +#include <ctype.h> 1.36 + 1.37 +const char* jkeywords[] = { 1.38 + "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", 1.39 + "package", "synchronized", "boolean", "do", "if", "private", "this", 1.40 + "break", "double", "implements", "protected", "throw", "byte", "else", 1.41 + "import", "public", "throws", "case", "enum", "instanceof", "return", 1.42 + "transient", "catch", "extends", "int", "short", "try", "char", "final", 1.43 + "interface", "static", "void", "class", "finally", "long", "strictfp", 1.44 + "volatile", "const", "float", "native", "super", "while", NULL 1.45 +}; 1.46 + 1.47 +int isjtype(char *word, size_t len) { 1.48 + return isupper(word[0]); 1.49 +} 1.50 + 1.51 +int isjdirective(char *word) { 1.52 + return word[0] == '@'; 1.53 +} 1.54 + 1.55 +void jparseline(char *src, char *dest, highlighter_t *hltr) { 1.56 + size_t sp = 0, dp = 0; 1.57 + /* indent */ 1.58 + while (isspace(src[sp])) { 1.59 + dest[dp++] = src[sp++]; 1.60 + } 1.61 + 1.62 + memset(hltr->word, 0, WORDBUF_SIZE); 1.63 + size_t wp = 0; 1.64 + int isstring = 0, iscomment = 0, isimport = 0; 1.65 + int isescaping = 0; 1.66 + 1.67 + if (hltr->iscommentml) { 1.68 + iscomment = 1; 1.69 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.70 + dp += 29; 1.71 + } 1.72 + 1.73 + for (char c = src[sp] ; c ; c=src[++sp]) { 1.74 + /* comments */ 1.75 + if (c == '/') { 1.76 + if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 1.77 + iscomment = 0; 1.78 + hltr->iscommentml = 0; 1.79 + memcpy(&(dest[dp]), "/</span>", 8); 1.80 + dp += 8; 1.81 + continue; 1.82 + } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.83 + iscomment = 1; 1.84 + hltr->iscommentml = (src[sp+1] == '*'); 1.85 + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.86 + dp += 29; 1.87 + } 1.88 + } 1.89 + 1.90 + if (iscomment) { 1.91 + if (c == '\n') { 1.92 + memcpy(&(dest[dp]), "</span>", 7); 1.93 + dp += 7; 1.94 + } 1.95 + dp = writeescapedchar(dest, dp, c); 1.96 + } else if (isimport) { 1.97 + // TODO: local imports 1.98 + } else { 1.99 + /* strings */ 1.100 + if (!isescaping && (c == '\'' || c == '\"')) { 1.101 + isstring ^= 1; 1.102 + if (isstring) { 1.103 + memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 1.104 + dp += 28; 1.105 + dp = writeescapedchar(dest, dp, c); 1.106 + } else { 1.107 + dp = writeescapedchar(dest, dp, c); 1.108 + memcpy(&(dest[dp]), "</span>", 7); 1.109 + dp += 7; 1.110 + } 1.111 + } else { 1.112 + if (isstring) { 1.113 + dp = writeescapedchar(dest, dp, c); 1.114 + } else if (!iswordcharacter(c)) { 1.115 + /* interpret word int_t */ 1.116 + if (wp > 0 && wp < WORDBUF_SIZE) { 1.117 + int closespan = 1; 1.118 + if (iskeyword(hltr->word, hltr->keywords)) { 1.119 + memcpy(&(dest[dp]), 1.120 + "<span class=\"c2html-keyword\">", 29); 1.121 + dp += 29; 1.122 + } else if (hltr->istype(hltr->word, wp)) { 1.123 + memcpy(&(dest[dp]), 1.124 + "<span class=\"c2html-type\">", 26); 1.125 + dp += 26; 1.126 + } else if (hltr->isdirective(hltr->word)) { 1.127 + memcpy(&(dest[dp]), 1.128 + "<span class=\"c2html-directive\">", 31); 1.129 + dp += 31; 1.130 + } else if (iscapsonly(hltr->word, wp)) { 1.131 + memcpy(&(dest[dp]), 1.132 + "<span class=\"c2html-macroconst\">", 32); 1.133 + dp += 32; 1.134 + } else { 1.135 + closespan = 0; 1.136 + } 1.137 + for (int i = 0 ; i < wp ; i++) { 1.138 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.139 + } 1.140 + if (closespan) { 1.141 + memcpy(&(dest[dp]), "</span>", 7); 1.142 + dp += 7; 1.143 + } 1.144 + } 1.145 + memset(hltr->word, 0, WORDBUF_SIZE); 1.146 + wp = 0; 1.147 + dp = writeescapedchar(dest, dp, c); 1.148 + } else { 1.149 + /* read word */ 1.150 + if (wp < WORDBUF_SIZE) { 1.151 + hltr->word[wp++] = c; 1.152 + } else if (wp == WORDBUF_SIZE) { 1.153 + for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 1.154 + dp = writeescapedchar(dest, dp, hltr->word[i]); 1.155 + } 1.156 + wp++; 1.157 + dp = writeescapedchar(dest, dp, c); 1.158 + } else { 1.159 + dp = writeescapedchar(dest, dp, c); 1.160 + } 1.161 + } 1.162 + } 1.163 + 1.164 + isescaping = !isescaping & (c == '\\'); 1.165 + } 1.166 + } 1.167 + dest[dp] = 0; 1.168 +}