Tue, 23 Aug 2016 15:55:02 +0200
refactors highlighter_t and removes abstraction overhead
| 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 | #include <string.h> | |
| 32 | #include <ctype.h> | |
| 33 | ||
| 34 | const char* ckeywords[] = { | |
| 35 | "auto", "break", "case", "char", "const", "continue", "default", "do", | |
| 36 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", | |
| 37 | "long", "register", "return", "short", "signed", "sizeof", "static", | |
| 38 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", | |
| 39 | "while", NULL | |
| 40 | }; | |
| 41 | ||
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
42 | #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
43 | dp += sizeof(str)-1 |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
44 | |
|
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
45 | void cparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) { |
|
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
46 | /* TODO: workaround for using old code with UcxBuffer */ |
|
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
47 | char *dest = destbuf->space + destbuf->pos; |
| 21 | 48 | |
| 49 | memset(hltr->word, 0, WORDBUF_SIZE); | |
|
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
|
50 | size_t wp = 0, ifp = 0, sp = (size_t)-1, dp = 0; |
| 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 */ |
| 21 | 56 | if (hltr->iscommentml) { |
| 57 | iscomment = 1; | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
58 | memcpy_const(dest, dp, "<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 == '/') { |
| 21 | 68 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { |
| 69 | iscomment = 0; | |
| 70 | hltr->iscommentml = 0; | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
71 | memcpy_const(dest, dp, "/</span>"); |
| 21 | 72 | continue; |
| 73 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
| 74 | iscomment = 1; | |
| 75 | hltr->iscommentml = (src[sp+1] == '*'); | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
76 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); |
| 21 | 77 | } |
| 78 | } | |
| 79 | ||
| 80 | if (iscomment) { | |
| 81 | if (c == '\n') { | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
82 | memcpy_const(dest, dp, "</span>"); |
| 21 | 83 | } |
| 84 | dp = writeescapedchar(dest, dp, c); | |
| 85 | } else if (isinclude) { | |
| 86 | if (c == '<') { | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
87 | memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">"); |
| 21 | 88 | dp = writeescapedchar(dest, dp, c); |
| 89 | } else if (c == '\"') { | |
| 90 | if (parseinclude) { | |
| 91 | dest[dp++] = '\"'; | |
| 92 | dest[dp++] = '>'; | |
| 93 | memcpy(&(dest[dp]), hltr->includefile, ifp); | |
| 94 | dp += ifp; | |
| 95 | ||
| 96 | dp = writeescapedchar(dest, dp, c); | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
97 | memcpy_const(dest, dp, "</a>"); |
| 21 | 98 | parseinclude = 0; |
| 99 | } else { | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
100 | memcpy_const(dest, dp, |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
101 | "<a class=\"c2html-userinclude\" href="); |
| 21 | 102 | dp = writeescapedchar(dest, dp, c); |
| 103 | ifp = 0; | |
| 104 | hltr->includefile[ifp++] = '\"'; | |
| 105 | parseinclude = 1; | |
| 106 | } | |
| 107 | } else if (c == '>') { | |
| 108 | dp = writeescapedchar(dest, dp, c); | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
109 | memcpy_const(dest, dp, "</span>"); |
| 21 | 110 | } else { |
| 111 | if (parseinclude) { | |
| 112 | hltr->includefile[ifp++] = c; | |
| 113 | } | |
| 114 | dp = writeescapedchar(dest, dp, c); | |
| 115 | } | |
| 116 | } else { | |
| 117 | /* strings */ | |
| 118 | if (!isescaping && (c == '\'' || c == '\"')) { | |
| 119 | if (isstring) { | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
120 | dp = writeescapedchar(dest, dp, c); |
|
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
121 | if (c == quote) { |
|
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
122 | isstring = 0; |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
123 | memcpy_const(dest, dp, "</span>"); |
|
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
124 | } else { |
|
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
125 | dp = writeescapedchar(dest, dp, c); |
|
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
126 | } |
| 21 | 127 | } else { |
|
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
128 | isstring = 1; |
|
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
129 | quote = c; |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
130 | memcpy_const(dest, dp, |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
131 | "<span class=\"c2html-string\">"); |
| 21 | 132 | dp = writeescapedchar(dest, dp, c); |
| 133 | } | |
| 134 | } else { | |
| 135 | if (isstring) { | |
| 136 | dp = writeescapedchar(dest, dp, c); | |
| 137 | } else if (!iswordcharacter(c)) { | |
| 138 | /* interpret word int_t */ | |
| 139 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
| 140 | int closespan = 1; | |
|
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
141 | if (check_keyword(hltr->word, ckeywords)) { |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
142 | memcpy_const(dest, dp, |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
143 | "<span class=\"c2html-keyword\">"); |
|
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
144 | } else if (hltr->word[wp-2] == '_' |
|
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
145 | && hltr->word[wp-1] == 't') { |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
146 | memcpy_const(dest, dp, |
|
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-type\">"); |
|
46
534a4ef4143d
refactors highlighter_t and removes abstraction overhead
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
148 | } else if (hltr->word[0] == '#') { |
| 21 | 149 | isinclude = !strncmp( |
| 150 | "#include", hltr->word, WORDBUF_SIZE); | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
151 | memcpy_const(dest, dp, |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
152 | "<span class=\"c2html-directive\">"); |
|
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
153 | } else if (check_capsonly(hltr->word, wp)) { |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
154 | memcpy_const(dest, dp, |
|
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
155 | "<span class=\"c2html-macroconst\">"); |
| 21 | 156 | } else { |
| 157 | closespan = 0; | |
| 158 | } | |
| 159 | for (int i = 0 ; i < wp ; i++) { | |
| 160 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 161 | } | |
| 162 | if (closespan) { | |
|
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
163 | memcpy_const(dest, dp, "</span>"); |
| 21 | 164 | } |
| 165 | } | |
| 166 | memset(hltr->word, 0, WORDBUF_SIZE); | |
| 167 | wp = 0; | |
| 168 | dp = writeescapedchar(dest, dp, c); | |
| 169 | } else { | |
| 170 | /* read word */ | |
| 171 | if (wp < WORDBUF_SIZE) { | |
| 172 | hltr->word[wp++] = c; | |
| 173 | } else if (wp == WORDBUF_SIZE) { | |
| 174 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
| 175 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 176 | } | |
| 177 | wp++; | |
| 178 | dp = writeescapedchar(dest, dp, c); | |
| 179 | } else { | |
| 180 | dp = writeescapedchar(dest, dp, c); | |
| 181 | } | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | isescaping = !isescaping & (c == '\\'); | |
| 186 | } | |
|
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
|
187 | } while (c != '\n'); |
| 21 | 188 | dest[dp] = 0; |
|
45
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
189 | |
|
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
190 | /* TODO: workaround */ |
|
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
191 | destbuf->pos += dp; |
|
1f3835182aeb
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
Mike Becker <universe@uap-core.de>
parents:
39
diff
changeset
|
192 | destbuf->size += dp; |
| 21 | 193 | } |