Tue, 23 Aug 2016 15:28:56 +0200
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
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 <string.h>
32 #include <ctype.h>
34 const char* ckeywords[] = {
35 "auto", "break", "case", "char", "const", "continue", "default", "do",
36 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
37 "long", "register", "return", "short", "signed", "sizeof", "static",
38 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
39 "while", NULL
40 };
42 int check_ctype(char *word, size_t len) {
43 return (word[len-2] == '_' && word[len-1] == 't');
44 }
46 int check_cdirective(char *word) {
47 return (word[0] == '#');
48 }
50 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
51 dp += sizeof(str)-1
53 void cparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) {
54 /* TODO: workaround for using old code with UcxBuffer */
55 char *dest = destbuf->space + destbuf->pos;
57 memset(hltr->word, 0, WORDBUF_SIZE);
58 size_t wp = 0, ifp = 0, sp = (size_t)-1, dp = 0;
59 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
60 char quote = '\0';
61 int isescaping = 0;
63 /* continue a multi line comment highlighting */
64 if (hltr->iscommentml) {
65 iscomment = 1;
66 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
67 }
69 char c;
70 do {
71 c = src[++sp];
72 if (!c) break;
74 /* comments */
75 if (!isstring && c == '/') {
76 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
77 iscomment = 0;
78 hltr->iscommentml = 0;
79 memcpy_const(dest, dp, "/</span>");
80 continue;
81 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
82 iscomment = 1;
83 hltr->iscommentml = (src[sp+1] == '*');
84 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
85 }
86 }
88 if (iscomment) {
89 if (c == '\n') {
90 memcpy_const(dest, dp, "</span>");
91 }
92 dp = writeescapedchar(dest, dp, c);
93 } else if (isinclude) {
94 if (c == '<') {
95 memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
96 dp = writeescapedchar(dest, dp, c);
97 } else if (c == '\"') {
98 if (parseinclude) {
99 dest[dp++] = '\"';
100 dest[dp++] = '>';
101 memcpy(&(dest[dp]), hltr->includefile, ifp);
102 dp += ifp;
104 dp = writeescapedchar(dest, dp, c);
105 memcpy_const(dest, dp, "</a>");
106 parseinclude = 0;
107 } else {
108 memcpy_const(dest, dp,
109 "<a class=\"c2html-userinclude\" href=");
110 dp = writeescapedchar(dest, dp, c);
111 ifp = 0;
112 hltr->includefile[ifp++] = '\"';
113 parseinclude = 1;
114 }
115 } else if (c == '>') {
116 dp = writeescapedchar(dest, dp, c);
117 memcpy_const(dest, dp, "</span>");
118 } else {
119 if (parseinclude) {
120 hltr->includefile[ifp++] = c;
121 }
122 dp = writeescapedchar(dest, dp, c);
123 }
124 } else {
125 /* strings */
126 if (!isescaping && (c == '\'' || c == '\"')) {
127 if (isstring) {
128 dp = writeescapedchar(dest, dp, c);
129 if (c == quote) {
130 isstring = 0;
131 memcpy_const(dest, dp, "</span>");
132 } else {
133 dp = writeescapedchar(dest, dp, c);
134 }
135 } else {
136 isstring = 1;
137 quote = c;
138 memcpy_const(dest, dp,
139 "<span class=\"c2html-string\">");
140 dp = writeescapedchar(dest, dp, c);
141 }
142 } else {
143 if (isstring) {
144 dp = writeescapedchar(dest, dp, c);
145 } else if (!iswordcharacter(c)) {
146 /* interpret word int_t */
147 if (wp > 0 && wp < WORDBUF_SIZE) {
148 int closespan = 1;
149 if (check_keyword(hltr->word, hltr->keywords)) {
150 memcpy_const(dest, dp,
151 "<span class=\"c2html-keyword\">");
152 } else if (hltr->istype(hltr->word, wp)) {
153 memcpy_const(dest, dp,
154 "<span class=\"c2html-type\">");
155 } else if (hltr->isdirective(hltr->word)) {
156 isinclude = !strncmp(
157 "#include", hltr->word, WORDBUF_SIZE);
158 memcpy_const(dest, dp,
159 "<span class=\"c2html-directive\">");
160 } else if (check_capsonly(hltr->word, wp)) {
161 memcpy_const(dest, dp,
162 "<span class=\"c2html-macroconst\">");
163 } else {
164 closespan = 0;
165 }
166 for (int i = 0 ; i < wp ; i++) {
167 dp = writeescapedchar(dest, dp, hltr->word[i]);
168 }
169 if (closespan) {
170 memcpy_const(dest, dp, "</span>");
171 }
172 }
173 memset(hltr->word, 0, WORDBUF_SIZE);
174 wp = 0;
175 dp = writeescapedchar(dest, dp, c);
176 } else {
177 /* read word */
178 if (wp < WORDBUF_SIZE) {
179 hltr->word[wp++] = c;
180 } else if (wp == WORDBUF_SIZE) {
181 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
182 dp = writeescapedchar(dest, dp, hltr->word[i]);
183 }
184 wp++;
185 dp = writeescapedchar(dest, dp, c);
186 } else {
187 dp = writeescapedchar(dest, dp, c);
188 }
189 }
190 }
192 isescaping = !isescaping & (c == '\\');
193 }
194 } while (c != '\n');
195 dest[dp] = 0;
197 /* TODO: workaround */
198 destbuf->pos += dp;
199 destbuf->size += dp;
200 }