src/ccodegen.c

changeset 29
ec6e97454e64
parent 28
1be8ea902ef4
child 31
50ae611a785c
equal deleted inserted replaced
28:1be8ea902ef4 29:ec6e97454e64
45 45
46 int iscdirective(char *word) { 46 int iscdirective(char *word) {
47 return (word[0] == '#'); 47 return (word[0] == '#');
48 } 48 }
49 49
50 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
51 dp += sizeof(str)-1
52
50 void cparseline(char *src, char *dest, highlighter_t *hltr) { 53 void cparseline(char *src, char *dest, highlighter_t *hltr) {
51 size_t sp = 0, dp = 0; 54 size_t sp = 0, dp = 0;
52 /* indent */ 55 /* indent */
53 while (isspace(src[sp])) { 56 while (isspace(src[sp])) {
54 dest[dp++] = src[sp++]; 57 dest[dp++] = src[sp++];
60 char quote = '\0'; 63 char quote = '\0';
61 int isescaping = 0; 64 int isescaping = 0;
62 65
63 if (hltr->iscommentml) { 66 if (hltr->iscommentml) {
64 iscomment = 1; 67 iscomment = 1;
65 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 68 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
66 dp += 29;
67 } 69 }
68 70
69 for (char c = src[sp] ; c ; c=src[++sp]) { 71 for (char c = src[sp] ; c ; c=src[++sp]) {
70 /* comments */ 72 /* comments */
71 if (!isstring && c == '/') { 73 if (!isstring && c == '/') {
72 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 74 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
73 iscomment = 0; 75 iscomment = 0;
74 hltr->iscommentml = 0; 76 hltr->iscommentml = 0;
75 memcpy(&(dest[dp]), "/</span>", 8); 77 memcpy_const(dest, dp, "/</span>");
76 dp += 8;
77 continue; 78 continue;
78 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 79 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
79 iscomment = 1; 80 iscomment = 1;
80 hltr->iscommentml = (src[sp+1] == '*'); 81 hltr->iscommentml = (src[sp+1] == '*');
81 memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 82 memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
82 dp += 29;
83 } 83 }
84 } 84 }
85 85
86 if (iscomment) { 86 if (iscomment) {
87 if (c == '\n') { 87 if (c == '\n') {
88 memcpy(&(dest[dp]), "</span>", 7); 88 memcpy_const(dest, dp, "</span>");
89 dp += 7;
90 } 89 }
91 dp = writeescapedchar(dest, dp, c); 90 dp = writeescapedchar(dest, dp, c);
92 } else if (isinclude) { 91 } else if (isinclude) {
93 if (c == '<') { 92 if (c == '<') {
94 memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); 93 memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
95 dp += 32;
96 dp = writeescapedchar(dest, dp, c); 94 dp = writeescapedchar(dest, dp, c);
97 } else if (c == '\"') { 95 } else if (c == '\"') {
98 if (parseinclude) { 96 if (parseinclude) {
99 dest[dp++] = '\"'; 97 dest[dp++] = '\"';
100 dest[dp++] = '>'; 98 dest[dp++] = '>';
101 memcpy(&(dest[dp]), hltr->includefile, ifp); 99 memcpy(&(dest[dp]), hltr->includefile, ifp);
102 dp += ifp; 100 dp += ifp;
103 101
104 dp = writeescapedchar(dest, dp, c); 102 dp = writeescapedchar(dest, dp, c);
105 memcpy(&(dest[dp]), "</a>", 4); 103 memcpy_const(dest, dp, "</a>");
106 dp += 4;
107 parseinclude = 0; 104 parseinclude = 0;
108 } else { 105 } else {
109 memcpy(&(dest[dp]), 106 memcpy_const(dest, dp,
110 "<a class=\"c2html-userinclude\" href=", 35); 107 "<a class=\"c2html-userinclude\" href=");
111 dp += 35;
112 dp = writeescapedchar(dest, dp, c); 108 dp = writeescapedchar(dest, dp, c);
113 ifp = 0; 109 ifp = 0;
114 hltr->includefile[ifp++] = '\"'; 110 hltr->includefile[ifp++] = '\"';
115 parseinclude = 1; 111 parseinclude = 1;
116 } 112 }
117 } else if (c == '>') { 113 } else if (c == '>') {
118 dp = writeescapedchar(dest, dp, c); 114 dp = writeescapedchar(dest, dp, c);
119 memcpy(&(dest[dp]), "</span>", 7); 115 memcpy_const(dest, dp, "</span>");
120 dp += 7;
121 } else { 116 } else {
122 if (parseinclude) { 117 if (parseinclude) {
123 hltr->includefile[ifp++] = c; 118 hltr->includefile[ifp++] = c;
124 } 119 }
125 dp = writeescapedchar(dest, dp, c); 120 dp = writeescapedchar(dest, dp, c);
126 } 121 }
127 } else { 122 } else {
128 /* strings */ 123 /* strings */
129 if (!isescaping && (c == '\'' || c == '\"')) { 124 if (!isescaping && (c == '\'' || c == '\"')) {
130 if (isstring) { 125 if (isstring) {
126 dp = writeescapedchar(dest, dp, c);
131 if (c == quote) { 127 if (c == quote) {
132 isstring = 0; 128 isstring = 0;
133 memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 129 memcpy_const(dest, dp, "</span>");
134 dp += 28;
135 dp = writeescapedchar(dest, dp, c);
136 } else { 130 } else {
137 dp = writeescapedchar(dest, dp, c); 131 dp = writeescapedchar(dest, dp, c);
138 } 132 }
139 } else { 133 } else {
140 isstring = 1; 134 isstring = 1;
141 quote = c; 135 quote = c;
136 memcpy_const(dest, dp,
137 "<span class=\"c2html-string\">");
142 dp = writeescapedchar(dest, dp, c); 138 dp = writeescapedchar(dest, dp, c);
143 memcpy(&(dest[dp]), "</span>", 7);
144 dp += 7;
145 } 139 }
146 } else { 140 } else {
147 if (isstring) { 141 if (isstring) {
148 dp = writeescapedchar(dest, dp, c); 142 dp = writeescapedchar(dest, dp, c);
149 } else if (!iswordcharacter(c)) { 143 } else if (!iswordcharacter(c)) {
150 /* interpret word int_t */ 144 /* interpret word int_t */
151 if (wp > 0 && wp < WORDBUF_SIZE) { 145 if (wp > 0 && wp < WORDBUF_SIZE) {
152 int closespan = 1; 146 int closespan = 1;
153 if (iskeyword(hltr->word, hltr->keywords)) { 147 if (iskeyword(hltr->word, hltr->keywords)) {
154 memcpy(&(dest[dp]), 148 memcpy_const(dest, dp,
155 "<span class=\"c2html-keyword\">", 29); 149 "<span class=\"c2html-keyword\">");
156 dp += 29;
157 } else if (hltr->istype(hltr->word, wp)) { 150 } else if (hltr->istype(hltr->word, wp)) {
158 memcpy(&(dest[dp]), 151 memcpy_const(dest, dp,
159 "<span class=\"c2html-type\">", 26); 152 "<span class=\"c2html-type\">");
160 dp += 26;
161 } else if (hltr->isdirective(hltr->word)) { 153 } else if (hltr->isdirective(hltr->word)) {
162 isinclude = !strncmp( 154 isinclude = !strncmp(
163 "#include", hltr->word, WORDBUF_SIZE); 155 "#include", hltr->word, WORDBUF_SIZE);
164 memcpy(&(dest[dp]), 156 memcpy_const(dest, dp,
165 "<span class=\"c2html-directive\">", 31); 157 "<span class=\"c2html-directive\">");
166 dp += 31;
167 } else if (iscapsonly(hltr->word, wp)) { 158 } else if (iscapsonly(hltr->word, wp)) {
168 memcpy(&(dest[dp]), 159 memcpy_const(dest, dp,
169 "<span class=\"c2html-macroconst\">", 32); 160 "<span class=\"c2html-macroconst\">");
170 dp += 32;
171 } else { 161 } else {
172 closespan = 0; 162 closespan = 0;
173 } 163 }
174 for (int i = 0 ; i < wp ; i++) { 164 for (int i = 0 ; i < wp ; i++) {
175 dp = writeescapedchar(dest, dp, hltr->word[i]); 165 dp = writeescapedchar(dest, dp, hltr->word[i]);
176 } 166 }
177 if (closespan) { 167 if (closespan) {
178 memcpy(&(dest[dp]), "</span>", 7); 168 memcpy_const(dest, dp, "</span>");
179 dp += 7;
180 } 169 }
181 } 170 }
182 memset(hltr->word, 0, WORDBUF_SIZE); 171 memset(hltr->word, 0, WORDBUF_SIZE);
183 wp = 0; 172 wp = 0;
184 dp = writeescapedchar(dest, dp, c); 173 dp = writeescapedchar(dest, dp, c);

mercurial