src/javacodegen.c

changeset 52
33ded421c512
parent 50
17408c3607ce
equal deleted inserted replaced
51:f25ba6fd7a08 52:33ded421c512
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 */
29
30 #include "javacodegen.h"
31
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 };
41
42 void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
43 /* reset buffers without clearing them */
44 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
45 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
46
47 /* alias the buffers for better handling */
48 UcxBuffer *wbuf = hd->primary_buffer;
49
50 /* local information */
51 size_t sp = (size_t)-1;
52 int isstring = 0, iscomment = 0, isimport = 0;
53 char quote = '\0';
54 int isescaping = 0;
55
56 if (hd->multiline_comment) {
57 iscomment = 1;
58 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
59 }
60
61 char c;
62 do {
63 c = src[++sp];
64 if (!c) break;
65
66 /* comments */
67 if (!isstring && c == '/') {
68 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
69 iscomment = 0;
70 hd->multiline_comment = 0;
71 ucx_buffer_puts(dest, "/</span>");
72 continue;
73 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
74 iscomment = 1;
75 hd->multiline_comment = (src[sp+1] == '*');
76 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
77 }
78 }
79
80 if (iscomment) {
81 if (c == '\n') {
82 ucx_buffer_puts(dest, "</span>\n");
83 } else {
84 put_htmlescaped(dest, c);
85 }
86 } else if (isimport) {
87 /* TODO: local imports */
88 } else {
89 /* strings */
90 if (!isescaping && (c == '\'' || c == '\"')) {
91 if (isstring) {
92 put_htmlescaped(dest, c);
93 if (c == quote) {
94 isstring = 0;
95 ucx_buffer_puts(dest, "</span>");
96 } else {
97 put_htmlescaped(dest, c);
98 }
99 } else {
100 isstring = 1;
101 quote = c;
102 ucx_buffer_puts(dest,
103 "<span class=\"c2html-string\">");
104 put_htmlescaped(dest, c);
105 }
106 } else {
107 if (isstring) {
108 put_htmlescaped(dest, c);
109 } else if (!isalnum(c) && c!='_' && c!='@') {
110 /* write buffered word, if any */
111 if (wbuf->size > 0) {
112 sstr_t word = sstrn(wbuf->space, wbuf->size);
113 int closespan = 1;
114 if (check_keyword(word, jkeywords)) {
115 ucx_buffer_puts(dest,
116 "<span class=\"c2html-keyword\">");
117 } else if (isupper(word.ptr[0])) {
118 ucx_buffer_puts(dest,
119 "<span class=\"c2html-type\">");
120 } else if (word.ptr[0] == '@') {
121 ucx_buffer_puts(dest,
122 "<span class=\"c2html-directive\">");
123 } else if (check_capsonly(word)) {
124 ucx_buffer_puts(dest,
125 "<span class=\"c2html-macroconst\">");
126 } else {
127 closespan = 0;
128 }
129 put_htmlescapedstr(dest, word);
130
131 if (closespan) {
132 ucx_buffer_puts(dest, "</span>");
133 }
134 }
135 wbuf->pos = wbuf->size = 0; /* reset buffer */
136
137 /* write current character */
138 put_htmlescaped(dest, c);
139 } else {
140 /* buffer the current word */
141 ucx_buffer_putc(wbuf, c);
142 }
143 }
144
145 isescaping = !isescaping & (c == '\\');
146 }
147 } while (c != '\n');
148 }

mercurial