universe@21: /* universe@21: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@21: * olaf@24: * Copyright 2015 Mike Becker. All rights reserved. universe@21: * universe@21: * Redistribution and use in source and binary forms, with or without universe@21: * modification, are permitted provided that the following conditions are met: universe@21: * universe@21: * 1. Redistributions of source code must retain the above copyright universe@21: * notice, this list of conditions and the following disclaimer. universe@21: * universe@21: * 2. Redistributions in binary form must reproduce the above copyright universe@21: * notice, this list of conditions and the following disclaimer in the universe@21: * documentation and/or other materials provided with the distribution. universe@21: * universe@21: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@21: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@21: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@21: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@21: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@21: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@21: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@21: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@21: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@21: * POSSIBILITY OF SUCH DAMAGE. universe@21: * universe@21: */ universe@21: universe@21: #include "javacodegen.h" universe@21: #include universe@21: #include universe@21: universe@21: const char* jkeywords[] = { universe@21: "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", universe@21: "package", "synchronized", "boolean", "do", "if", "private", "this", universe@21: "break", "double", "implements", "protected", "throw", "byte", "else", universe@21: "import", "public", "throws", "case", "enum", "instanceof", "return", universe@21: "transient", "catch", "extends", "int", "short", "try", "char", "final", universe@21: "interface", "static", "void", "class", "finally", "long", "strictfp", universe@21: "volatile", "const", "float", "native", "super", "while", NULL universe@21: }; universe@21: universe@21: int isjtype(char *word, size_t len) { universe@21: return isupper(word[0]); universe@21: } universe@21: universe@21: int isjdirective(char *word) { universe@21: return word[0] == '@'; universe@21: } universe@21: universe@21: void jparseline(char *src, char *dest, highlighter_t *hltr) { universe@21: size_t sp = 0, dp = 0; universe@21: /* indent */ universe@21: while (isspace(src[sp])) { universe@21: dest[dp++] = src[sp++]; universe@21: } universe@21: universe@21: memset(hltr->word, 0, WORDBUF_SIZE); universe@21: size_t wp = 0; universe@21: int isstring = 0, iscomment = 0, isimport = 0; universe@21: int isescaping = 0; universe@21: universe@21: if (hltr->iscommentml) { universe@21: iscomment = 1; universe@21: memcpy(&(dest[dp]), "", 29); universe@21: dp += 29; universe@21: } universe@21: universe@21: for (char c = src[sp] ; c ; c=src[++sp]) { universe@21: /* comments */ universe@26: if (!isstring && c == '/') { universe@21: if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { universe@21: iscomment = 0; universe@21: hltr->iscommentml = 0; universe@21: memcpy(&(dest[dp]), "/", 8); universe@21: dp += 8; universe@21: continue; universe@21: } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { universe@21: iscomment = 1; universe@21: hltr->iscommentml = (src[sp+1] == '*'); universe@21: memcpy(&(dest[dp]), "", 29); universe@21: dp += 29; universe@21: } universe@21: } universe@21: universe@21: if (iscomment) { universe@21: if (c == '\n') { universe@21: memcpy(&(dest[dp]), "", 7); universe@21: dp += 7; universe@21: } universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else if (isimport) { universe@21: // TODO: local imports universe@21: } else { universe@21: /* strings */ universe@21: if (!isescaping && (c == '\'' || c == '\"')) { universe@21: isstring ^= 1; universe@21: if (isstring) { universe@21: memcpy(&(dest[dp]), "", 28); universe@21: dp += 28; universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else { universe@21: dp = writeescapedchar(dest, dp, c); universe@21: memcpy(&(dest[dp]), "", 7); universe@21: dp += 7; universe@21: } universe@21: } else { universe@21: if (isstring) { universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else if (!iswordcharacter(c)) { universe@21: /* interpret word int_t */ universe@21: if (wp > 0 && wp < WORDBUF_SIZE) { universe@21: int closespan = 1; universe@21: if (iskeyword(hltr->word, hltr->keywords)) { universe@21: memcpy(&(dest[dp]), universe@21: "", 29); universe@21: dp += 29; universe@21: } else if (hltr->istype(hltr->word, wp)) { universe@21: memcpy(&(dest[dp]), universe@21: "", 26); universe@21: dp += 26; universe@21: } else if (hltr->isdirective(hltr->word)) { universe@21: memcpy(&(dest[dp]), universe@21: "", 31); universe@21: dp += 31; universe@21: } else if (iscapsonly(hltr->word, wp)) { universe@21: memcpy(&(dest[dp]), universe@21: "", 32); universe@21: dp += 32; universe@21: } else { universe@21: closespan = 0; universe@21: } universe@21: for (int i = 0 ; i < wp ; i++) { universe@21: dp = writeescapedchar(dest, dp, hltr->word[i]); universe@21: } universe@21: if (closespan) { universe@21: memcpy(&(dest[dp]), "", 7); universe@21: dp += 7; universe@21: } universe@21: } universe@21: memset(hltr->word, 0, WORDBUF_SIZE); universe@21: wp = 0; universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else { universe@21: /* read word */ universe@21: if (wp < WORDBUF_SIZE) { universe@21: hltr->word[wp++] = c; universe@21: } else if (wp == WORDBUF_SIZE) { universe@21: for (int i = 0 ; i < WORDBUF_SIZE ; i++) { universe@21: dp = writeescapedchar(dest, dp, hltr->word[i]); universe@21: } universe@21: wp++; universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } else { universe@21: dp = writeescapedchar(dest, dp, c); universe@21: } universe@21: } universe@21: } universe@21: universe@21: isescaping = !isescaping & (c == '\\'); universe@21: } universe@21: } universe@21: dest[dp] = 0; universe@21: }