src/ccodegen.c

changeset 48
b2724c711203
parent 47
c39ecbbca7c0
child 49
f86f0b054464
equal deleted inserted replaced
47:c39ecbbca7c0 48:b2724c711203
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 29
30 #include "ccodegen.h" 30 #include "ccodegen.h"
31 #include "ucx/utils.h"
31 #include <string.h> 32 #include <string.h>
32 #include <ctype.h> 33 #include <ctype.h>
33 34
34 const char* ckeywords[] = { 35 const char* ckeywords[] = {
35 "auto", "break", "case", "char", "const", "continue", "default", "do", 36 "auto", "break", "case", "char", "const", "continue", "default", "do",
37 "long", "register", "return", "short", "signed", "sizeof", "static", 38 "long", "register", "return", "short", "signed", "sizeof", "static",
38 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", 39 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
39 "while", NULL 40 "while", NULL
40 }; 41 };
41 42
42 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ 43 void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
43 dp += sizeof(str)-1
44
45 void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
46 /* TODO: workaround for using old code with UcxBuffer */
47 char *dest = destbuf->space + destbuf->pos;
48
49 /* TODO: try to replace these buffers */ 44 /* TODO: try to replace these buffers */
50 char wordbuf[WORDBUF_SIZE]; 45 char wordbuf[WORDBUF_SIZE];
51 sstr_t word; 46 sstr_t word;
52 word.ptr = wordbuf; word.length = 0; 47 word.ptr = wordbuf; word.length = 0;
53 48
54 char includefilebuf[512]; 49 char includefilebuf[512];
55 sstr_t includefile; 50 sstr_t includefile;
56 includefile.ptr = includefilebuf; 51 includefile.ptr = includefilebuf;
57 includefile.length = 0; 52 includefile.length = 0;
58 53
59 size_t sp = (size_t)-1, dp = 0; 54 size_t sp = (size_t)-1;
60 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 55 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
61 char quote = '\0'; 56 char quote = '\0';
62 int isescaping = 0; 57 int isescaping = 0;
63 58
64 /* continue a multi line comment highlighting */ 59 /* continue a multi line comment highlighting */
65 if (*multiline_comment) { 60 if (*multiline_comment) {
66 iscomment = 1; 61 iscomment = 1;
67 memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 62 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
68 } 63 }
69 64
70 char c; 65 char c;
71 do { 66 do {
72 c = src[++sp]; 67 c = src[++sp];
75 /* comments */ 70 /* comments */
76 if (!isstring && c == '/') { 71 if (!isstring && c == '/') {
77 if (*multiline_comment && sp > 0 && src[sp-1] == '*') { 72 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
78 iscomment = 0; 73 iscomment = 0;
79 *multiline_comment = 0; 74 *multiline_comment = 0;
80 memcpy_const(dest, dp, "/</span>"); 75 ucx_buffer_puts(dest, "/</span>");
81 continue; 76 continue;
82 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 77 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
83 iscomment = 1; 78 iscomment = 1;
84 *multiline_comment = (src[sp+1] == '*'); 79 *multiline_comment = (src[sp+1] == '*');
85 memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 80 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
86 } 81 }
87 } 82 }
88 83
89 if (iscomment) { 84 if (iscomment) {
90 if (c == '\n') { 85 if (c == '\n') {
91 memcpy_const(dest, dp, "</span>"); 86 ucx_buffer_puts(dest, "</span>\n");
87 } else {
88 put_htmlescaped(dest, c);
92 } 89 }
93 dp = writeescapedchar(dest, dp, c);
94 } else if (isinclude) { 90 } else if (isinclude) {
95 if (c == '<') { 91 if (c == '<') {
96 memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">"); 92 ucx_buffer_puts(dest,
97 dp = writeescapedchar(dest, dp, c); 93 "<span class=\"c2html-stdinclude\">&lt;");
98 } else if (c == '\"') { 94 } else if (c == '\"') {
99 if (parseinclude) { 95 if (parseinclude) {
100 dest[dp++] = '\"'; 96 ucx_bprintf(dest, "\">%.*s\"</a>",
101 dest[dp++] = '>'; 97 includefile.length, includefile.ptr);
102 memcpy(&(dest[dp]), includefile.ptr, includefile.length);
103 dp += includefile.length;
104
105 dp = writeescapedchar(dest, dp, c);
106 memcpy_const(dest, dp, "</a>");
107 parseinclude = 0; 98 parseinclude = 0;
108 } else { 99 } else {
109 memcpy_const(dest, dp, 100 ucx_buffer_puts(dest,
110 "<a class=\"c2html-userinclude\" href="); 101 "<a class=\"c2html-userinclude\" href=\"");
111 dp = writeescapedchar(dest, dp, c);
112 includefile.length = 0; 102 includefile.length = 0;
113 includefile.ptr[includefile.length++] = '\"'; 103 includefile.ptr[includefile.length++] = '\"';
114 parseinclude = 1; 104 parseinclude = 1;
115 } 105 }
116 } else if (c == '>') { 106 } else if (c == '>') {
117 dp = writeescapedchar(dest, dp, c); 107 ucx_buffer_puts(dest, "&gt;</span>");
118 memcpy_const(dest, dp, "</span>");
119 } else { 108 } else {
120 if (parseinclude) { 109 if (parseinclude) {
121 includefile.ptr[includefile.length++] = c; 110 includefile.ptr[includefile.length++] = c;
122 } 111 }
123 dp = writeescapedchar(dest, dp, c); 112 put_htmlescaped(dest, c);
124 } 113 }
125 } else { 114 } else {
126 /* strings */ 115 /* strings */
127 if (!isescaping && (c == '\'' || c == '\"')) { 116 if (!isescaping && (c == '\'' || c == '\"')) {
128 if (isstring) { 117 if (isstring) {
129 dp = writeescapedchar(dest, dp, c); 118 put_htmlescaped(dest, c);
130 if (c == quote) { 119 if (c == quote) {
131 isstring = 0; 120 isstring = 0;
132 memcpy_const(dest, dp, "</span>"); 121 ucx_buffer_puts(dest, "</span>");
133 } else { 122 } else {
134 dp = writeescapedchar(dest, dp, c); 123 put_htmlescaped(dest, c);
135 } 124 }
136 } else { 125 } else {
137 isstring = 1; 126 isstring = 1;
138 quote = c; 127 quote = c;
139 memcpy_const(dest, dp, 128 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
140 "<span class=\"c2html-string\">"); 129 put_htmlescaped(dest, c);
141 dp = writeescapedchar(dest, dp, c);
142 } 130 }
143 } else { 131 } else {
144 if (isstring) { 132 if (isstring) {
145 dp = writeescapedchar(dest, dp, c); 133 put_htmlescaped(dest, c);
146 } else if (!iswordcharacter(c)) { 134 } else if (!check_alnumex(c)) {
147 if (word.length > 0 && word.length < WORDBUF_SIZE) { 135 if (word.length > 0 && word.length < WORDBUF_SIZE) {
148 int closespan = 1; 136 int closespan = 1;
149 sstr_t typesuffix = ST("_t"); 137 sstr_t typesuffix = ST("_t");
150 if (check_keyword(word, ckeywords)) { 138 if (check_keyword(word, ckeywords)) {
151 memcpy_const(dest, dp, 139 ucx_buffer_puts(dest,
152 "<span class=\"c2html-keyword\">"); 140 "<span class=\"c2html-keyword\">");
153 } else if (sstrsuffix(word, typesuffix)) { 141 } else if (sstrsuffix(word, typesuffix)) {
154 memcpy_const(dest, dp, 142 ucx_buffer_puts(dest,
155 "<span class=\"c2html-type\">"); 143 "<span class=\"c2html-type\">");
156 } else if (word.ptr[0] == '#') { 144 } else if (word.ptr[0] == '#') {
157 isinclude = !sstrcmp(word, S("#include")); 145 isinclude = !sstrcmp(word, S("#include"));
158 memcpy_const(dest, dp, 146 ucx_buffer_puts(dest,
159 "<span class=\"c2html-directive\">"); 147 "<span class=\"c2html-directive\">");
160 } else if (check_capsonly(word)) { 148 } else if (check_capsonly(word)) {
161 memcpy_const(dest, dp, 149 ucx_buffer_puts(dest,
162 "<span class=\"c2html-macroconst\">"); 150 "<span class=\"c2html-macroconst\">");
163 } else { 151 } else {
164 closespan = 0; 152 closespan = 0;
165 } 153 }
166 for (int i = 0 ; i < word.length ; i++) { 154 put_htmlescapedstr(dest, word);
167 dp = writeescapedchar(dest, dp, word.ptr[i]);
168 }
169 if (closespan) { 155 if (closespan) {
170 memcpy_const(dest, dp, "</span>"); 156 ucx_buffer_puts(dest, "</span>");
171 } 157 }
172 } 158 }
173 word.length = 0; 159 word.length = 0;
174 dp = writeescapedchar(dest, dp, c); 160 put_htmlescaped(dest, c);
175 } else { 161 } else {
176 /* read word */ 162 /* read word */
177 if (word.length < WORDBUF_SIZE) { 163 if (word.length < WORDBUF_SIZE) {
178 word.ptr[word.length++] = c; 164 word.ptr[word.length++] = c;
179 } else if (word.length == WORDBUF_SIZE) { 165 } else if (word.length == WORDBUF_SIZE) {
180 for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 166 /* TODO: this will be removed */
181 dp = writeescapedchar(dest, dp, word.ptr[i]); 167 ucx_buffer_puts(dest,
182 } 168 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
183 word.length++; 169 word.length = 0;
184 dp = writeescapedchar(dest, dp, c);
185 } else { 170 } else {
186 dp = writeescapedchar(dest, dp, c); 171 put_htmlescaped(dest, c);
187 } 172 }
188 } 173 }
189 } 174 }
190 175
191 isescaping = !isescaping & (c == '\\'); 176 isescaping = !isescaping & (c == '\\');
192 } 177 }
193 } while (c != '\n'); 178 } while (c != '\n');
194 dest[dp] = 0;
195
196 /* TODO: workaround */
197 destbuf->pos += dp;
198 destbuf->size += dp;
199 } 179 }

mercurial