Thu, 25 Aug 2016 12:16:57 +0200
replaces stack buffers with UCX buffers
21 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
21 | 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 | */ | |
29 | ||
30 | #include "ccodegen.h" | |
31 | ||
32 | const char* ckeywords[] = { | |
33 | "auto", "break", "case", "char", "const", "continue", "default", "do", | |
34 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", | |
35 | "long", "register", "return", "short", "signed", "sizeof", "static", | |
36 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", | |
37 | "while", NULL | |
38 | }; | |
39 | ||
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
40 | void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
41 | /* reset buffers without clearing them */ |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
42 | hd->primary_buffer->size = hd->primary_buffer->pos = 0; |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
43 | hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
44 | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
45 | /* alias the buffers for better handling */ |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
46 | UcxBuffer *wbuf = hd->primary_buffer; |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
47 | UcxBuffer *ifilebuf = hd->secondary_buffer; |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
48 | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
49 | /* local information */ |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
50 | size_t sp = (size_t)-1; |
21 | 51 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
52 | char quote = '\0'; |
21 | 53 | int isescaping = 0; |
31
50ae611a785c
fixed corrupted multi line comments, when a blank line (containing only white spaces) is present in the comment
universe
parents:
29
diff
changeset
|
54 | |
50ae611a785c
fixed corrupted multi line comments, when a blank line (containing only white spaces) is present in the comment
universe
parents:
29
diff
changeset
|
55 | /* continue a multi line comment highlighting */ |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
56 | if (hd->multiline_comment) { |
21 | 57 | iscomment = 1; |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
58 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); |
21 | 59 | } |
60 | ||
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
61 | char c; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
62 | do { |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
63 | c = src[++sp]; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
64 | if (!c) break; |
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
65 | |
21 | 66 | /* comments */ |
26
05c3c6842aef
fixed wrong comment formatting in strings
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
67 | if (!isstring && c == '/') { |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
68 | if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { |
21 | 69 | iscomment = 0; |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
70 | hd->multiline_comment = 0; |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
71 | ucx_buffer_puts(dest, "/</span>"); |
21 | 72 | continue; |
73 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
74 | iscomment = 1; | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
75 | hd->multiline_comment = (src[sp+1] == '*'); |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
76 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); |
21 | 77 | } |
78 | } | |
79 | ||
80 | if (iscomment) { | |
81 | if (c == '\n') { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
82 | ucx_buffer_puts(dest, "</span>\n"); |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
83 | } else { |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
84 | put_htmlescaped(dest, c); |
21 | 85 | } |
86 | } else if (isinclude) { | |
87 | if (c == '<') { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
88 | ucx_buffer_puts(dest, |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
89 | "<span class=\"c2html-stdinclude\"><"); |
21 | 90 | } else if (c == '\"') { |
91 | if (parseinclude) { | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
92 | ucx_buffer_puts(dest, "\">"); |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
93 | ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest); |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
94 | ucx_buffer_puts(dest, "\"</a>"); |
21 | 95 | parseinclude = 0; |
96 | } else { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
97 | ucx_buffer_puts(dest, |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
98 | "<a class=\"c2html-userinclude\" href=\""); |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
99 | ucx_buffer_putc(ifilebuf, '\"'); |
21 | 100 | parseinclude = 1; |
101 | } | |
102 | } else if (c == '>') { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
103 | ucx_buffer_puts(dest, "></span>"); |
21 | 104 | } else { |
105 | if (parseinclude) { | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
106 | ucx_buffer_putc(ifilebuf, c); |
21 | 107 | } |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
108 | put_htmlescaped(dest, c); |
21 | 109 | } |
110 | } else { | |
111 | /* strings */ | |
112 | if (!isescaping && (c == '\'' || c == '\"')) { | |
113 | if (isstring) { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
114 | put_htmlescaped(dest, c); |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
115 | if (c == quote) { |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
116 | isstring = 0; |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
117 | ucx_buffer_puts(dest, "</span>"); |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
118 | } else { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
119 | put_htmlescaped(dest, c); |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
120 | } |
21 | 121 | } else { |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
122 | isstring = 1; |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
123 | quote = c; |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
124 | ucx_buffer_puts(dest, "<span class=\"c2html-string\">"); |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
125 | put_htmlescaped(dest, c); |
21 | 126 | } |
127 | } else { | |
128 | if (isstring) { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
129 | put_htmlescaped(dest, c); |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
130 | } else if (!isalnum(c) && c!='_' && c!='#') { |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
131 | /* write buffered word, if any */ |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
132 | if (wbuf->size > 0) { |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
133 | sstr_t word = sstrn(wbuf->space, wbuf->size); |
21 | 134 | int closespan = 1; |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
135 | sstr_t typesuffix = ST("_t"); |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
136 | if (check_keyword(word, ckeywords)) { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
137 | ucx_buffer_puts(dest, |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
138 | "<span class=\"c2html-keyword\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
139 | } else if (sstrsuffix(word, typesuffix)) { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
140 | ucx_buffer_puts(dest, |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
141 | "<span class=\"c2html-type\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
142 | } else if (word.ptr[0] == '#') { |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
143 | isinclude = !sstrcmp(word, S("#include")); |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
144 | ucx_buffer_puts(dest, |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
145 | "<span class=\"c2html-directive\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
146 | } else if (check_capsonly(word)) { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
147 | ucx_buffer_puts(dest, |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
148 | "<span class=\"c2html-macroconst\">"); |
21 | 149 | } else { |
150 | closespan = 0; | |
151 | } | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
152 | put_htmlescapedstr(dest, word); |
21 | 153 | if (closespan) { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
154 | ucx_buffer_puts(dest, "</span>"); |
21 | 155 | } |
156 | } | |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
157 | wbuf->pos = wbuf->size = 0; /* reset word buffer */ |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
158 | |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
159 | /* write current character */ |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
160 | put_htmlescaped(dest, c); |
21 | 161 | } else { |
51
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
162 | /* buffer the current word */ |
f25ba6fd7a08
replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
163 | ucx_buffer_putc(wbuf, c); |
21 | 164 | } |
165 | } | |
166 | ||
167 | isescaping = !isescaping & (c == '\\'); | |
168 | } | |
39
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
169 | } while (c != '\n'); |
21 | 170 | } |