src/ccodegen.c

changeset 52
33ded421c512
parent 51
f25ba6fd7a08
child 53
5e47a26a16f0
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 "ccodegen.h"
31
32 const char* ckeywords[] = {
33 "auto", "break", "case", "char", "const", "continue", "default", "do",
34 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
35 "long", "register", "return", "short", "signed", "sizeof", "static",
36 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
37 "while", NULL
38 };
39
40 void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
41 /* reset buffers without clearing them */
42 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
43 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
44
45 /* alias the buffers for better handling */
46 UcxBuffer *wbuf = hd->primary_buffer;
47 UcxBuffer *ifilebuf = hd->secondary_buffer;
48
49 /* local information */
50 size_t sp = (size_t)-1;
51 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
52 char quote = '\0';
53 int isescaping = 0;
54
55 /* continue a multi line comment highlighting */
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 (isinclude) {
87 if (c == '<') {
88 ucx_buffer_puts(dest,
89 "<span class=\"c2html-stdinclude\">&lt;");
90 } else if (c == '\"') {
91 if (parseinclude) {
92 ucx_buffer_puts(dest, "\">");
93 ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
94 ucx_buffer_puts(dest, "\"</a>");
95 parseinclude = 0;
96 } else {
97 ucx_buffer_puts(dest,
98 "<a class=\"c2html-userinclude\" href=\"");
99 ucx_buffer_putc(ifilebuf, '\"');
100 parseinclude = 1;
101 }
102 } else if (c == '>') {
103 ucx_buffer_puts(dest, "&gt;</span>");
104 } else {
105 if (parseinclude) {
106 ucx_buffer_putc(ifilebuf, c);
107 }
108 put_htmlescaped(dest, c);
109 }
110 } else {
111 /* strings */
112 if (!isescaping && (c == '\'' || c == '\"')) {
113 if (isstring) {
114 put_htmlescaped(dest, c);
115 if (c == quote) {
116 isstring = 0;
117 ucx_buffer_puts(dest, "</span>");
118 } else {
119 put_htmlescaped(dest, c);
120 }
121 } else {
122 isstring = 1;
123 quote = c;
124 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
125 put_htmlescaped(dest, c);
126 }
127 } else {
128 if (isstring) {
129 put_htmlescaped(dest, c);
130 } else if (!isalnum(c) && c!='_' && c!='#') {
131 /* write buffered word, if any */
132 if (wbuf->size > 0) {
133 sstr_t word = sstrn(wbuf->space, wbuf->size);
134 int closespan = 1;
135 sstr_t typesuffix = ST("_t");
136 if (check_keyword(word, ckeywords)) {
137 ucx_buffer_puts(dest,
138 "<span class=\"c2html-keyword\">");
139 } else if (sstrsuffix(word, typesuffix)) {
140 ucx_buffer_puts(dest,
141 "<span class=\"c2html-type\">");
142 } else if (word.ptr[0] == '#') {
143 isinclude = !sstrcmp(word, S("#include"));
144 ucx_buffer_puts(dest,
145 "<span class=\"c2html-directive\">");
146 } else if (check_capsonly(word)) {
147 ucx_buffer_puts(dest,
148 "<span class=\"c2html-macroconst\">");
149 } else {
150 closespan = 0;
151 }
152 put_htmlescapedstr(dest, word);
153 if (closespan) {
154 ucx_buffer_puts(dest, "</span>");
155 }
156 }
157 wbuf->pos = wbuf->size = 0; /* reset word buffer */
158
159 /* write current character */
160 put_htmlescaped(dest, c);
161 } else {
162 /* buffer the current word */
163 ucx_buffer_putc(wbuf, c);
164 }
165 }
166
167 isescaping = !isescaping & (c == '\\');
168 }
169 } while (c != '\n');
170 }

mercurial