Tue, 23 Aug 2016 17:31:15 +0200
cleans up includes
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Mike Becker. All rights reserved.
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 */
30 #include "javacodegen.h"
32 const char* jkeywords[] = {
33 "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
34 "package", "synchronized", "boolean", "do", "if", "private", "this",
35 "break", "double", "implements", "protected", "throw", "byte", "else",
36 "import", "public", "throws", "case", "enum", "instanceof", "return",
37 "transient", "catch", "extends", "int", "short", "try", "char", "final",
38 "interface", "static", "void", "class", "finally", "long", "strictfp",
39 "volatile", "const", "float", "native", "super", "while", NULL
40 };
42 void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
44 /* TODO: try to replace this buffer */
45 char wordbuf[WORDBUF_SIZE];
46 sstr_t word;
47 word.ptr = wordbuf; word.length = 0;
49 size_t sp = (size_t)-1;
50 int isstring = 0, iscomment = 0, isimport = 0;
51 char quote = '\0';
52 int isescaping = 0;
54 if (*multiline_comment) {
55 iscomment = 1;
56 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
57 }
59 char c;
60 do {
61 c = src[++sp];
62 if (!c) break;
64 /* comments */
65 if (!isstring && c == '/') {
66 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
67 iscomment = 0;
68 *multiline_comment = 0;
69 ucx_buffer_puts(dest, "/</span>");
70 continue;
71 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
72 iscomment = 1;
73 *multiline_comment = (src[sp+1] == '*');
74 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
75 }
76 }
78 if (iscomment) {
79 if (c == '\n') {
80 ucx_buffer_puts(dest, "</span>\n");
81 } else {
82 put_htmlescaped(dest, c);
83 }
84 } else if (isimport) {
85 /* TODO: local imports */
86 } else {
87 /* strings */
88 if (!isescaping && (c == '\'' || c == '\"')) {
89 if (isstring) {
90 put_htmlescaped(dest, c);
91 if (c == quote) {
92 isstring = 0;
93 ucx_buffer_puts(dest, "</span>");
94 } else {
95 put_htmlescaped(dest, c);
96 }
97 } else {
98 isstring = 1;
99 quote = c;
100 ucx_buffer_puts(dest,
101 "<span class=\"c2html-string\">");
102 put_htmlescaped(dest, c);
103 }
104 } else {
105 if (isstring) {
106 put_htmlescaped(dest, c);
107 } else if (!check_alnumex(c)) {
108 if (word.length > 0 && word.length < WORDBUF_SIZE) {
109 int closespan = 1;
110 if (check_keyword(word, jkeywords)) {
111 ucx_buffer_puts(dest,
112 "<span class=\"c2html-keyword\">");
113 } else if (isupper(word.ptr[0])) {
114 ucx_buffer_puts(dest,
115 "<span class=\"c2html-type\">");
116 } else if (word.ptr[0] == '@') {
117 ucx_buffer_puts(dest,
118 "<span class=\"c2html-directive\">");
119 } else if (check_capsonly(word)) {
120 ucx_buffer_puts(dest,
121 "<span class=\"c2html-macroconst\">");
122 } else {
123 closespan = 0;
124 }
125 put_htmlescapedstr(dest, word);
127 if (closespan) {
128 ucx_buffer_puts(dest, "</span>");
129 }
130 }
131 word.length = 0;
132 put_htmlescaped(dest, c);
133 } else {
134 /* read word */
135 if (word.length < WORDBUF_SIZE) {
136 word.ptr[word.length++] = c;
137 } else if (word.length == WORDBUF_SIZE) {
138 /* TODO: this will be removed */
139 ucx_buffer_puts(dest,
140 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
141 word.length = 0;
142 } else {
143 put_htmlescaped(dest, c);
144 }
145 }
146 }
148 isescaping = !isescaping & (c == '\\');
149 }
150 } while (c != '\n');
151 }