Tue, 23 Aug 2016 17:31:15 +0200
cleans up includes
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 | ||
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
40 | void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) { |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
41 | /* TODO: try to replace these buffers */ |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
42 | char wordbuf[WORDBUF_SIZE]; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
43 | sstr_t word; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
44 | word.ptr = wordbuf; word.length = 0; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
45 | |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
46 | char includefilebuf[512]; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
47 | sstr_t includefile; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
48 | includefile.ptr = includefilebuf; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
49 | includefile.length = 0; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
50 | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
51 | size_t sp = (size_t)-1; |
21 | 52 | 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
|
53 | char quote = '\0'; |
21 | 54 | 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
|
55 | |
50ae611a785c
fixed corrupted multi line comments, when a blank line (containing only white spaces) is present in the comment
universe
parents:
29
diff
changeset
|
56 | /* continue a multi line comment highlighting */ |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
57 | if (*multiline_comment) { |
21 | 58 | 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
|
59 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); |
21 | 60 | } |
61 | ||
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
|
62 | 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
|
63 | 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
|
64 | 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
|
65 | 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
|
66 | |
21 | 67 | /* comments */ |
26
05c3c6842aef
fixed wrong comment formatting in strings
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
68 | if (!isstring && c == '/') { |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
69 | if (*multiline_comment && sp > 0 && src[sp-1] == '*') { |
21 | 70 | iscomment = 0; |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
71 | *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
|
72 | ucx_buffer_puts(dest, "/</span>"); |
21 | 73 | continue; |
74 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
75 | iscomment = 1; | |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
76 | *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
|
77 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); |
21 | 78 | } |
79 | } | |
80 | ||
81 | if (iscomment) { | |
82 | 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
|
83 | 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
|
84 | } else { |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
85 | put_htmlescaped(dest, c); |
21 | 86 | } |
87 | } else if (isinclude) { | |
88 | 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
|
89 | 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
|
90 | "<span class=\"c2html-stdinclude\"><"); |
21 | 91 | } else if (c == '\"') { |
92 | if (parseinclude) { | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
93 | ucx_bprintf(dest, "\">%.*s\"</a>", |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
94 | includefile.length, includefile.ptr); |
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=\""); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
99 | includefile.length = 0; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
100 | includefile.ptr[includefile.length++] = '\"'; |
21 | 101 | parseinclude = 1; |
102 | } | |
103 | } 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
|
104 | ucx_buffer_puts(dest, "></span>"); |
21 | 105 | } else { |
106 | if (parseinclude) { | |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
107 | includefile.ptr[includefile.length++] = c; |
21 | 108 | } |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
109 | put_htmlescaped(dest, c); |
21 | 110 | } |
111 | } else { | |
112 | /* strings */ | |
113 | if (!isescaping && (c == '\'' || c == '\"')) { | |
114 | 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
|
115 | 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
|
116 | if (c == quote) { |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
117 | 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
|
118 | 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
|
119 | } 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
|
120 | 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
|
121 | } |
21 | 122 | } else { |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
123 | isstring = 1; |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
124 | 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
|
125 | 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
|
126 | put_htmlescaped(dest, c); |
21 | 127 | } |
128 | } else { | |
129 | 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
|
130 | put_htmlescaped(dest, c); |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
131 | } else if (!check_alnumex(c)) { |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
132 | if (word.length > 0 && word.length < WORDBUF_SIZE) { |
21 | 133 | int closespan = 1; |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
134 | sstr_t typesuffix = ST("_t"); |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
135 | 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
|
136 | 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
|
137 | "<span class=\"c2html-keyword\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
138 | } 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
|
139 | 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
|
140 | "<span class=\"c2html-type\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
141 | } else if (word.ptr[0] == '#') { |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
142 | 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
|
143 | 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
|
144 | "<span class=\"c2html-directive\">"); |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
145 | } 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
|
146 | 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
|
147 | "<span class=\"c2html-macroconst\">"); |
21 | 148 | } else { |
149 | closespan = 0; | |
150 | } | |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
151 | put_htmlescapedstr(dest, word); |
21 | 152 | 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
|
153 | ucx_buffer_puts(dest, "</span>"); |
21 | 154 | } |
155 | } | |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
156 | word.length = 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
|
157 | put_htmlescaped(dest, c); |
21 | 158 | } else { |
159 | /* read word */ | |
47
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
160 | if (word.length < WORDBUF_SIZE) { |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
161 | word.ptr[word.length++] = c; |
c39ecbbca7c0
words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
162 | } else if (word.length == WORDBUF_SIZE) { |
48
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
163 | /* TODO: this will be removed */ |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
164 | 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
|
165 | "!!! WARNING - WORD TOO LONG TO PARSE !!!"); |
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
166 | word.length = 0; |
21 | 167 | } 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
|
168 | put_htmlescaped(dest, c); |
21 | 169 | } |
170 | } | |
171 | } | |
172 | ||
173 | isescaping = !isescaping & (c == '\\'); | |
174 | } | |
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
|
175 | } while (c != '\n'); |
21 | 176 | } |