Tue, 23 Aug 2016 17:24:58 +0200
highlighter now use the UcxBuffer API for writing to the destination buffer
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 "ccodegen.h"
31 #include "ucx/utils.h"
32 #include <string.h>
33 #include <ctype.h>
35 const char* ckeywords[] = {
36 "auto", "break", "case", "char", "const", "continue", "default", "do",
37 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
38 "long", "register", "return", "short", "signed", "sizeof", "static",
39 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
40 "while", NULL
41 };
43 void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
44 /* TODO: try to replace these buffers */
45 char wordbuf[WORDBUF_SIZE];
46 sstr_t word;
47 word.ptr = wordbuf; word.length = 0;
49 char includefilebuf[512];
50 sstr_t includefile;
51 includefile.ptr = includefilebuf;
52 includefile.length = 0;
54 size_t sp = (size_t)-1;
55 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
56 char quote = '\0';
57 int isescaping = 0;
59 /* continue a multi line comment highlighting */
60 if (*multiline_comment) {
61 iscomment = 1;
62 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
63 }
65 char c;
66 do {
67 c = src[++sp];
68 if (!c) break;
70 /* comments */
71 if (!isstring && c == '/') {
72 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
73 iscomment = 0;
74 *multiline_comment = 0;
75 ucx_buffer_puts(dest, "/</span>");
76 continue;
77 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
78 iscomment = 1;
79 *multiline_comment = (src[sp+1] == '*');
80 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
81 }
82 }
84 if (iscomment) {
85 if (c == '\n') {
86 ucx_buffer_puts(dest, "</span>\n");
87 } else {
88 put_htmlescaped(dest, c);
89 }
90 } else if (isinclude) {
91 if (c == '<') {
92 ucx_buffer_puts(dest,
93 "<span class=\"c2html-stdinclude\"><");
94 } else if (c == '\"') {
95 if (parseinclude) {
96 ucx_bprintf(dest, "\">%.*s\"</a>",
97 includefile.length, includefile.ptr);
98 parseinclude = 0;
99 } else {
100 ucx_buffer_puts(dest,
101 "<a class=\"c2html-userinclude\" href=\"");
102 includefile.length = 0;
103 includefile.ptr[includefile.length++] = '\"';
104 parseinclude = 1;
105 }
106 } else if (c == '>') {
107 ucx_buffer_puts(dest, "></span>");
108 } else {
109 if (parseinclude) {
110 includefile.ptr[includefile.length++] = c;
111 }
112 put_htmlescaped(dest, c);
113 }
114 } else {
115 /* strings */
116 if (!isescaping && (c == '\'' || c == '\"')) {
117 if (isstring) {
118 put_htmlescaped(dest, c);
119 if (c == quote) {
120 isstring = 0;
121 ucx_buffer_puts(dest, "</span>");
122 } else {
123 put_htmlescaped(dest, c);
124 }
125 } else {
126 isstring = 1;
127 quote = c;
128 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
129 put_htmlescaped(dest, c);
130 }
131 } else {
132 if (isstring) {
133 put_htmlescaped(dest, c);
134 } else if (!check_alnumex(c)) {
135 if (word.length > 0 && word.length < WORDBUF_SIZE) {
136 int closespan = 1;
137 sstr_t typesuffix = ST("_t");
138 if (check_keyword(word, ckeywords)) {
139 ucx_buffer_puts(dest,
140 "<span class=\"c2html-keyword\">");
141 } else if (sstrsuffix(word, typesuffix)) {
142 ucx_buffer_puts(dest,
143 "<span class=\"c2html-type\">");
144 } else if (word.ptr[0] == '#') {
145 isinclude = !sstrcmp(word, S("#include"));
146 ucx_buffer_puts(dest,
147 "<span class=\"c2html-directive\">");
148 } else if (check_capsonly(word)) {
149 ucx_buffer_puts(dest,
150 "<span class=\"c2html-macroconst\">");
151 } else {
152 closespan = 0;
153 }
154 put_htmlescapedstr(dest, word);
155 if (closespan) {
156 ucx_buffer_puts(dest, "</span>");
157 }
158 }
159 word.length = 0;
160 put_htmlescaped(dest, c);
161 } else {
162 /* read word */
163 if (word.length < WORDBUF_SIZE) {
164 word.ptr[word.length++] = c;
165 } else if (word.length == WORDBUF_SIZE) {
166 /* TODO: this will be removed */
167 ucx_buffer_puts(dest,
168 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
169 word.length = 0;
170 } else {
171 put_htmlescaped(dest, c);
172 }
173 }
174 }
176 isescaping = !isescaping & (c == '\\');
177 }
178 } while (c != '\n');
179 }