Tue, 23 Aug 2016 15:55:02 +0200
refactors highlighter_t and removes abstraction overhead
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"
31 #include <string.h>
32 #include <ctype.h>
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 };
44 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
45 dp += sizeof(str)-1
47 void jparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) {
48 /* TODO: workaround for using old code with UcxBuffer */
49 char *dest = destbuf->space + destbuf->pos;
51 memset(hltr->word, 0, WORDBUF_SIZE);
52 size_t wp = 0, sp = (size_t)-1, dp = 0;
53 int isstring = 0, iscomment = 0, isimport = 0;
54 char quote = '\0';
55 int isescaping = 0;
57 if (hltr->iscommentml) {
58 iscomment = 1;
59 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
60 }
62 char c;
63 do {
64 c = src[++sp];
65 if (!c) break;
67 /* comments */
68 if (!isstring && c == '/') {
69 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
70 iscomment = 0;
71 hltr->iscommentml = 0;
72 memcpy_const(dest, dp, "/</span>");
73 continue;
74 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
75 iscomment = 1;
76 hltr->iscommentml = (src[sp+1] == '*');
77 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
78 }
79 }
81 if (iscomment) {
82 if (c == '\n') {
83 memcpy(&(dest[dp]), "</span>", 7);
84 dp += 7;
85 }
86 dp = writeescapedchar(dest, dp, c);
87 } else if (isimport) {
88 // TODO: local imports
89 } else {
90 /* strings */
91 if (!isescaping && (c == '\'' || c == '\"')) {
92 if (isstring) {
93 dp = writeescapedchar(dest, dp, c);
94 if (c == quote) {
95 isstring = 0;
96 memcpy_const(dest, dp, "</span>");
97 } else {
98 dp = writeescapedchar(dest, dp, c);
99 }
100 } else {
101 isstring = 1;
102 quote = c;
103 memcpy_const(dest, dp,
104 "<span class=\"c2html-string\">");
105 dp = writeescapedchar(dest, dp, c);
106 }
107 } else {
108 if (isstring) {
109 dp = writeescapedchar(dest, dp, c);
110 } else if (!iswordcharacter(c)) {
111 /* interpret word int_t */
112 if (wp > 0 && wp < WORDBUF_SIZE) {
113 int closespan = 1;
114 if (check_keyword(hltr->word, jkeywords)) {
115 memcpy_const(dest, dp,
116 "<span class=\"c2html-keyword\">");
117 } else if (isupper(hltr->word[0])) {
118 memcpy_const(dest, dp,
119 "<span class=\"c2html-type\">");
120 } else if (hltr->word[0] == '@') {
121 memcpy_const(dest, dp,
122 "<span class=\"c2html-directive\">");
123 } else if (check_capsonly(hltr->word, wp)) {
124 memcpy_const(dest, dp,
125 "<span class=\"c2html-macroconst\">");
126 } else {
127 closespan = 0;
128 }
129 for (int i = 0 ; i < wp ; i++) {
130 dp = writeescapedchar(dest, dp, hltr->word[i]);
131 }
132 if (closespan) {
133 memcpy_const(dest, dp, "</span>");
134 }
135 }
136 memset(hltr->word, 0, WORDBUF_SIZE);
137 wp = 0;
138 dp = writeescapedchar(dest, dp, c);
139 } else {
140 /* read word */
141 if (wp < WORDBUF_SIZE) {
142 hltr->word[wp++] = c;
143 } else if (wp == WORDBUF_SIZE) {
144 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
145 dp = writeescapedchar(dest, dp, hltr->word[i]);
146 }
147 wp++;
148 dp = writeescapedchar(dest, dp, c);
149 } else {
150 dp = writeescapedchar(dest, dp, c);
151 }
152 }
153 }
155 isescaping = !isescaping & (c == '\\');
156 }
157 } while (c != '\n');
158 dest[dp] = 0;
160 /* TODO: workaround */
161 destbuf->pos += dp;
162 destbuf->size += dp;
163 }