src/ccodegen.c

changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
equal deleted inserted replaced
50:17408c3607ce 51:f25ba6fd7a08
35 "long", "register", "return", "short", "signed", "sizeof", "static", 35 "long", "register", "return", "short", "signed", "sizeof", "static",
36 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", 36 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
37 "while", NULL 37 "while", NULL
38 }; 38 };
39 39
40 void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) { 40 void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
41 /* TODO: try to replace these buffers */ 41 /* reset buffers without clearing them */
42 char wordbuf[WORDBUF_SIZE]; 42 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
43 sstr_t word; 43 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
44 word.ptr = wordbuf; word.length = 0;
45 44
46 char includefilebuf[512]; 45 /* alias the buffers for better handling */
47 sstr_t includefile; 46 UcxBuffer *wbuf = hd->primary_buffer;
48 includefile.ptr = includefilebuf; 47 UcxBuffer *ifilebuf = hd->secondary_buffer;
49 includefile.length = 0;
50 48
49 /* local information */
51 size_t sp = (size_t)-1; 50 size_t sp = (size_t)-1;
52 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 51 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
53 char quote = '\0'; 52 char quote = '\0';
54 int isescaping = 0; 53 int isescaping = 0;
55 54
56 /* continue a multi line comment highlighting */ 55 /* continue a multi line comment highlighting */
57 if (*multiline_comment) { 56 if (hd->multiline_comment) {
58 iscomment = 1; 57 iscomment = 1;
59 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 58 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
60 } 59 }
61 60
62 char c; 61 char c;
64 c = src[++sp]; 63 c = src[++sp];
65 if (!c) break; 64 if (!c) break;
66 65
67 /* comments */ 66 /* comments */
68 if (!isstring && c == '/') { 67 if (!isstring && c == '/') {
69 if (*multiline_comment && sp > 0 && src[sp-1] == '*') { 68 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
70 iscomment = 0; 69 iscomment = 0;
71 *multiline_comment = 0; 70 hd->multiline_comment = 0;
72 ucx_buffer_puts(dest, "/</span>"); 71 ucx_buffer_puts(dest, "/</span>");
73 continue; 72 continue;
74 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 73 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
75 iscomment = 1; 74 iscomment = 1;
76 *multiline_comment = (src[sp+1] == '*'); 75 hd->multiline_comment = (src[sp+1] == '*');
77 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 76 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
78 } 77 }
79 } 78 }
80 79
81 if (iscomment) { 80 if (iscomment) {
88 if (c == '<') { 87 if (c == '<') {
89 ucx_buffer_puts(dest, 88 ucx_buffer_puts(dest,
90 "<span class=\"c2html-stdinclude\">&lt;"); 89 "<span class=\"c2html-stdinclude\">&lt;");
91 } else if (c == '\"') { 90 } else if (c == '\"') {
92 if (parseinclude) { 91 if (parseinclude) {
93 ucx_bprintf(dest, "\">%.*s\"</a>", 92 ucx_buffer_puts(dest, "\">");
94 includefile.length, includefile.ptr); 93 ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
94 ucx_buffer_puts(dest, "\"</a>");
95 parseinclude = 0; 95 parseinclude = 0;
96 } else { 96 } else {
97 ucx_buffer_puts(dest, 97 ucx_buffer_puts(dest,
98 "<a class=\"c2html-userinclude\" href=\""); 98 "<a class=\"c2html-userinclude\" href=\"");
99 includefile.length = 0; 99 ucx_buffer_putc(ifilebuf, '\"');
100 includefile.ptr[includefile.length++] = '\"';
101 parseinclude = 1; 100 parseinclude = 1;
102 } 101 }
103 } else if (c == '>') { 102 } else if (c == '>') {
104 ucx_buffer_puts(dest, "&gt;</span>"); 103 ucx_buffer_puts(dest, "&gt;</span>");
105 } else { 104 } else {
106 if (parseinclude) { 105 if (parseinclude) {
107 includefile.ptr[includefile.length++] = c; 106 ucx_buffer_putc(ifilebuf, c);
108 } 107 }
109 put_htmlescaped(dest, c); 108 put_htmlescaped(dest, c);
110 } 109 }
111 } else { 110 } else {
112 /* strings */ 111 /* strings */
126 put_htmlescaped(dest, c); 125 put_htmlescaped(dest, c);
127 } 126 }
128 } else { 127 } else {
129 if (isstring) { 128 if (isstring) {
130 put_htmlescaped(dest, c); 129 put_htmlescaped(dest, c);
131 } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') { 130 } else if (!isalnum(c) && c!='_' && c!='#') {
132 if (word.length > 0 && word.length < WORDBUF_SIZE) { 131 /* write buffered word, if any */
132 if (wbuf->size > 0) {
133 sstr_t word = sstrn(wbuf->space, wbuf->size);
133 int closespan = 1; 134 int closespan = 1;
134 sstr_t typesuffix = ST("_t"); 135 sstr_t typesuffix = ST("_t");
135 if (check_keyword(word, ckeywords)) { 136 if (check_keyword(word, ckeywords)) {
136 ucx_buffer_puts(dest, 137 ucx_buffer_puts(dest,
137 "<span class=\"c2html-keyword\">"); 138 "<span class=\"c2html-keyword\">");
151 put_htmlescapedstr(dest, word); 152 put_htmlescapedstr(dest, word);
152 if (closespan) { 153 if (closespan) {
153 ucx_buffer_puts(dest, "</span>"); 154 ucx_buffer_puts(dest, "</span>");
154 } 155 }
155 } 156 }
156 word.length = 0; 157 wbuf->pos = wbuf->size = 0; /* reset word buffer */
158
159 /* write current character */
157 put_htmlescaped(dest, c); 160 put_htmlescaped(dest, c);
158 } else { 161 } else {
159 /* read word */ 162 /* buffer the current word */
160 if (word.length < WORDBUF_SIZE) { 163 ucx_buffer_putc(wbuf, c);
161 word.ptr[word.length++] = c;
162 } else if (word.length == WORDBUF_SIZE) {
163 /* TODO: this will be removed */
164 ucx_buffer_puts(dest,
165 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
166 word.length = 0;
167 } else {
168 put_htmlescaped(dest, c);
169 }
170 } 164 }
171 } 165 }
172 166
173 isescaping = !isescaping & (c == '\\'); 167 isescaping = !isescaping & (c == '\\');
174 } 168 }

mercurial