src/ccodegen.c

changeset 47
c39ecbbca7c0
parent 46
534a4ef4143d
child 48
b2724c711203
equal deleted inserted replaced
46:534a4ef4143d 47:c39ecbbca7c0
40 }; 40 };
41 41
42 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ 42 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
43 dp += sizeof(str)-1 43 dp += sizeof(str)-1
44 44
45 void cparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) { 45 void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
46 /* TODO: workaround for using old code with UcxBuffer */ 46 /* TODO: workaround for using old code with UcxBuffer */
47 char *dest = destbuf->space + destbuf->pos; 47 char *dest = destbuf->space + destbuf->pos;
48 48
49 memset(hltr->word, 0, WORDBUF_SIZE); 49 /* TODO: try to replace these buffers */
50 size_t wp = 0, ifp = 0, sp = (size_t)-1, dp = 0; 50 char wordbuf[WORDBUF_SIZE];
51 sstr_t word;
52 word.ptr = wordbuf; word.length = 0;
53
54 char includefilebuf[512];
55 sstr_t includefile;
56 includefile.ptr = includefilebuf;
57 includefile.length = 0;
58
59 size_t sp = (size_t)-1, dp = 0;
51 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 60 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
52 char quote = '\0'; 61 char quote = '\0';
53 int isescaping = 0; 62 int isescaping = 0;
54 63
55 /* continue a multi line comment highlighting */ 64 /* continue a multi line comment highlighting */
56 if (hltr->iscommentml) { 65 if (*multiline_comment) {
57 iscomment = 1; 66 iscomment = 1;
58 memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 67 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
59 } 68 }
60 69
61 char c; 70 char c;
63 c = src[++sp]; 72 c = src[++sp];
64 if (!c) break; 73 if (!c) break;
65 74
66 /* comments */ 75 /* comments */
67 if (!isstring && c == '/') { 76 if (!isstring && c == '/') {
68 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 77 if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
69 iscomment = 0; 78 iscomment = 0;
70 hltr->iscommentml = 0; 79 *multiline_comment = 0;
71 memcpy_const(dest, dp, "/</span>"); 80 memcpy_const(dest, dp, "/</span>");
72 continue; 81 continue;
73 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 82 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
74 iscomment = 1; 83 iscomment = 1;
75 hltr->iscommentml = (src[sp+1] == '*'); 84 *multiline_comment = (src[sp+1] == '*');
76 memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 85 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
77 } 86 }
78 } 87 }
79 88
80 if (iscomment) { 89 if (iscomment) {
88 dp = writeescapedchar(dest, dp, c); 97 dp = writeescapedchar(dest, dp, c);
89 } else if (c == '\"') { 98 } else if (c == '\"') {
90 if (parseinclude) { 99 if (parseinclude) {
91 dest[dp++] = '\"'; 100 dest[dp++] = '\"';
92 dest[dp++] = '>'; 101 dest[dp++] = '>';
93 memcpy(&(dest[dp]), hltr->includefile, ifp); 102 memcpy(&(dest[dp]), includefile.ptr, includefile.length);
94 dp += ifp; 103 dp += includefile.length;
95 104
96 dp = writeescapedchar(dest, dp, c); 105 dp = writeescapedchar(dest, dp, c);
97 memcpy_const(dest, dp, "</a>"); 106 memcpy_const(dest, dp, "</a>");
98 parseinclude = 0; 107 parseinclude = 0;
99 } else { 108 } else {
100 memcpy_const(dest, dp, 109 memcpy_const(dest, dp,
101 "<a class=\"c2html-userinclude\" href="); 110 "<a class=\"c2html-userinclude\" href=");
102 dp = writeescapedchar(dest, dp, c); 111 dp = writeescapedchar(dest, dp, c);
103 ifp = 0; 112 includefile.length = 0;
104 hltr->includefile[ifp++] = '\"'; 113 includefile.ptr[includefile.length++] = '\"';
105 parseinclude = 1; 114 parseinclude = 1;
106 } 115 }
107 } else if (c == '>') { 116 } else if (c == '>') {
108 dp = writeescapedchar(dest, dp, c); 117 dp = writeescapedchar(dest, dp, c);
109 memcpy_const(dest, dp, "</span>"); 118 memcpy_const(dest, dp, "</span>");
110 } else { 119 } else {
111 if (parseinclude) { 120 if (parseinclude) {
112 hltr->includefile[ifp++] = c; 121 includefile.ptr[includefile.length++] = c;
113 } 122 }
114 dp = writeescapedchar(dest, dp, c); 123 dp = writeescapedchar(dest, dp, c);
115 } 124 }
116 } else { 125 } else {
117 /* strings */ 126 /* strings */
133 } 142 }
134 } else { 143 } else {
135 if (isstring) { 144 if (isstring) {
136 dp = writeescapedchar(dest, dp, c); 145 dp = writeescapedchar(dest, dp, c);
137 } else if (!iswordcharacter(c)) { 146 } else if (!iswordcharacter(c)) {
138 /* interpret word int_t */ 147 if (word.length > 0 && word.length < WORDBUF_SIZE) {
139 if (wp > 0 && wp < WORDBUF_SIZE) {
140 int closespan = 1; 148 int closespan = 1;
141 if (check_keyword(hltr->word, ckeywords)) { 149 sstr_t typesuffix = ST("_t");
150 if (check_keyword(word, ckeywords)) {
142 memcpy_const(dest, dp, 151 memcpy_const(dest, dp,
143 "<span class=\"c2html-keyword\">"); 152 "<span class=\"c2html-keyword\">");
144 } else if (hltr->word[wp-2] == '_' 153 } else if (sstrsuffix(word, typesuffix)) {
145 && hltr->word[wp-1] == 't') {
146 memcpy_const(dest, dp, 154 memcpy_const(dest, dp,
147 "<span class=\"c2html-type\">"); 155 "<span class=\"c2html-type\">");
148 } else if (hltr->word[0] == '#') { 156 } else if (word.ptr[0] == '#') {
149 isinclude = !strncmp( 157 isinclude = !sstrcmp(word, S("#include"));
150 "#include", hltr->word, WORDBUF_SIZE);
151 memcpy_const(dest, dp, 158 memcpy_const(dest, dp,
152 "<span class=\"c2html-directive\">"); 159 "<span class=\"c2html-directive\">");
153 } else if (check_capsonly(hltr->word, wp)) { 160 } else if (check_capsonly(word)) {
154 memcpy_const(dest, dp, 161 memcpy_const(dest, dp,
155 "<span class=\"c2html-macroconst\">"); 162 "<span class=\"c2html-macroconst\">");
156 } else { 163 } else {
157 closespan = 0; 164 closespan = 0;
158 } 165 }
159 for (int i = 0 ; i < wp ; i++) { 166 for (int i = 0 ; i < word.length ; i++) {
160 dp = writeescapedchar(dest, dp, hltr->word[i]); 167 dp = writeescapedchar(dest, dp, word.ptr[i]);
161 } 168 }
162 if (closespan) { 169 if (closespan) {
163 memcpy_const(dest, dp, "</span>"); 170 memcpy_const(dest, dp, "</span>");
164 } 171 }
165 } 172 }
166 memset(hltr->word, 0, WORDBUF_SIZE); 173 word.length = 0;
167 wp = 0;
168 dp = writeescapedchar(dest, dp, c); 174 dp = writeescapedchar(dest, dp, c);
169 } else { 175 } else {
170 /* read word */ 176 /* read word */
171 if (wp < WORDBUF_SIZE) { 177 if (word.length < WORDBUF_SIZE) {
172 hltr->word[wp++] = c; 178 word.ptr[word.length++] = c;
173 } else if (wp == WORDBUF_SIZE) { 179 } else if (word.length == WORDBUF_SIZE) {
174 for (int i = 0 ; i < WORDBUF_SIZE ; i++) { 180 for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
175 dp = writeescapedchar(dest, dp, hltr->word[i]); 181 dp = writeescapedchar(dest, dp, word.ptr[i]);
176 } 182 }
177 wp++; 183 word.length++;
178 dp = writeescapedchar(dest, dp, c); 184 dp = writeescapedchar(dest, dp, c);
179 } else { 185 } else {
180 dp = writeescapedchar(dest, dp, c); 186 dp = writeescapedchar(dest, dp, c);
181 } 187 }
182 } 188 }

mercurial