Tue, 21 Apr 2015 09:53:01 +0200
fixed wrong comment formatting in strings
| 21 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
|
24
e43dee5892f4
improved code structure and added option for disabling line numbers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
4 | * Copyright 2015 Mike Becker. All rights reserved. |
| 21 | 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 | */ | |
| 29 | ||
| 30 | #include "javacodegen.h" | |
| 31 | #include <string.h> | |
| 32 | #include <ctype.h> | |
| 33 | ||
| 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 | }; | |
| 43 | ||
| 44 | int isjtype(char *word, size_t len) { | |
| 45 | return isupper(word[0]); | |
| 46 | } | |
| 47 | ||
| 48 | int isjdirective(char *word) { | |
| 49 | return word[0] == '@'; | |
| 50 | } | |
| 51 | ||
| 52 | void jparseline(char *src, char *dest, highlighter_t *hltr) { | |
| 53 | size_t sp = 0, dp = 0; | |
| 54 | /* indent */ | |
| 55 | while (isspace(src[sp])) { | |
| 56 | dest[dp++] = src[sp++]; | |
| 57 | } | |
| 58 | ||
| 59 | memset(hltr->word, 0, WORDBUF_SIZE); | |
| 60 | size_t wp = 0; | |
| 61 | int isstring = 0, iscomment = 0, isimport = 0; | |
| 62 | int isescaping = 0; | |
| 63 | ||
| 64 | if (hltr->iscommentml) { | |
| 65 | iscomment = 1; | |
| 66 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
| 67 | dp += 29; | |
| 68 | } | |
| 69 | ||
| 70 | for (char c = src[sp] ; c ; c=src[++sp]) { | |
| 71 | /* comments */ | |
|
26
05c3c6842aef
fixed wrong comment formatting in strings
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
72 | if (!isstring && c == '/') { |
| 21 | 73 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { |
| 74 | iscomment = 0; | |
| 75 | hltr->iscommentml = 0; | |
| 76 | memcpy(&(dest[dp]), "/</span>", 8); | |
| 77 | dp += 8; | |
| 78 | continue; | |
| 79 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
| 80 | iscomment = 1; | |
| 81 | hltr->iscommentml = (src[sp+1] == '*'); | |
| 82 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); | |
| 83 | dp += 29; | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | if (iscomment) { | |
| 88 | if (c == '\n') { | |
| 89 | memcpy(&(dest[dp]), "</span>", 7); | |
| 90 | dp += 7; | |
| 91 | } | |
| 92 | dp = writeescapedchar(dest, dp, c); | |
| 93 | } else if (isimport) { | |
| 94 | // TODO: local imports | |
| 95 | } else { | |
| 96 | /* strings */ | |
| 97 | if (!isescaping && (c == '\'' || c == '\"')) { | |
| 98 | isstring ^= 1; | |
| 99 | if (isstring) { | |
| 100 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); | |
| 101 | dp += 28; | |
| 102 | dp = writeescapedchar(dest, dp, c); | |
| 103 | } else { | |
| 104 | dp = writeescapedchar(dest, dp, c); | |
| 105 | memcpy(&(dest[dp]), "</span>", 7); | |
| 106 | dp += 7; | |
| 107 | } | |
| 108 | } else { | |
| 109 | if (isstring) { | |
| 110 | dp = writeescapedchar(dest, dp, c); | |
| 111 | } else if (!iswordcharacter(c)) { | |
| 112 | /* interpret word int_t */ | |
| 113 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
| 114 | int closespan = 1; | |
| 115 | if (iskeyword(hltr->word, hltr->keywords)) { | |
| 116 | memcpy(&(dest[dp]), | |
| 117 | "<span class=\"c2html-keyword\">", 29); | |
| 118 | dp += 29; | |
| 119 | } else if (hltr->istype(hltr->word, wp)) { | |
| 120 | memcpy(&(dest[dp]), | |
| 121 | "<span class=\"c2html-type\">", 26); | |
| 122 | dp += 26; | |
| 123 | } else if (hltr->isdirective(hltr->word)) { | |
| 124 | memcpy(&(dest[dp]), | |
| 125 | "<span class=\"c2html-directive\">", 31); | |
| 126 | dp += 31; | |
| 127 | } else if (iscapsonly(hltr->word, wp)) { | |
| 128 | memcpy(&(dest[dp]), | |
| 129 | "<span class=\"c2html-macroconst\">", 32); | |
| 130 | dp += 32; | |
| 131 | } else { | |
| 132 | closespan = 0; | |
| 133 | } | |
| 134 | for (int i = 0 ; i < wp ; i++) { | |
| 135 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 136 | } | |
| 137 | if (closespan) { | |
| 138 | memcpy(&(dest[dp]), "</span>", 7); | |
| 139 | dp += 7; | |
| 140 | } | |
| 141 | } | |
| 142 | memset(hltr->word, 0, WORDBUF_SIZE); | |
| 143 | wp = 0; | |
| 144 | dp = writeescapedchar(dest, dp, c); | |
| 145 | } else { | |
| 146 | /* read word */ | |
| 147 | if (wp < WORDBUF_SIZE) { | |
| 148 | hltr->word[wp++] = c; | |
| 149 | } else if (wp == WORDBUF_SIZE) { | |
| 150 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
| 151 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 152 | } | |
| 153 | wp++; | |
| 154 | dp = writeescapedchar(dest, dp, c); | |
| 155 | } else { | |
| 156 | dp = writeescapedchar(dest, dp, c); | |
| 157 | } | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | isescaping = !isescaping & (c == '\\'); | |
| 162 | } | |
| 163 | } | |
| 164 | dest[dp] = 0; | |
| 165 | } |