Tue, 23 Aug 2016 15:28:56 +0200
changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet
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 "javacodegen.h" | |
31 | #include <string.h> | |
32 | #include <ctype.h> | |
33 | ||
34 | const char* jkeywords[] = { | |
35 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", | |
36 | "package", "synchronized", "boolean", "do", "if", "private", "this", | |
37 | "break", "double", "implements", "protected", "throw", "byte", "else", | |
38 | "import", "public", "throws", "case", "enum", "instanceof", "return", | |
39 | "transient", "catch", "extends", "int", "short", "try", "char", "final", | |
40 | "interface", "static", "void", "class", "finally", "long", "strictfp", | |
41 | "volatile", "const", "float", "native", "super", "while", NULL | |
42 | }; | |
43 | ||
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
44 | int check_jtype(char *word, size_t len) { |
21 | 45 | return isupper(word[0]); |
46 | } | |
47 | ||
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
48 | int check_jdirective(char *word) { |
21 | 49 | return word[0] == '@'; |
50 | } | |
51 | ||
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
52 | #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
|
53 | 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
|
54 | |
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
|
55 | void jparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) { |
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
|
56 | /* 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
|
57 | char *dest = destbuf->space + destbuf->pos; |
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
|
58 | |
21 | 59 | 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
|
60 | size_t wp = 0, sp = (size_t)-1, dp = 0; |
21 | 61 | int isstring = 0, iscomment = 0, isimport = 0; |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
62 | char quote = '\0'; |
21 | 63 | int isescaping = 0; |
64 | ||
65 | if (hltr->iscommentml) { | |
66 | iscomment = 1; | |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
67 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); |
21 | 68 | } |
69 | ||
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
|
70 | 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
|
71 | 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
|
72 | 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
|
73 | 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
|
74 | |
21 | 75 | /* comments */ |
26
05c3c6842aef
fixed wrong comment formatting in strings
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
76 | if (!isstring && c == '/') { |
21 | 77 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { |
78 | iscomment = 0; | |
79 | 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
|
80 | memcpy_const(dest, dp, "/</span>"); |
21 | 81 | continue; |
82 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
83 | iscomment = 1; | |
84 | 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
|
85 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); |
21 | 86 | } |
87 | } | |
88 | ||
89 | if (iscomment) { | |
90 | if (c == '\n') { | |
91 | memcpy(&(dest[dp]), "</span>", 7); | |
92 | dp += 7; | |
93 | } | |
94 | dp = writeescapedchar(dest, dp, c); | |
95 | } else if (isimport) { | |
96 | // TODO: local imports | |
97 | } else { | |
98 | /* strings */ | |
99 | if (!isescaping && (c == '\'' || c == '\"')) { | |
100 | if (isstring) { | |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
101 | 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
|
102 | if (c == quote) { |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
103 | isstring = 0; |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
104 | 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
|
105 | } else { |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
106 | 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
|
107 | } |
21 | 108 | } else { |
28
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
109 | isstring = 1; |
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
110 | quote = c; |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
111 | 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
|
112 | "<span class=\"c2html-string\">"); |
21 | 113 | dp = writeescapedchar(dest, dp, c); |
114 | } | |
115 | } else { | |
116 | if (isstring) { | |
117 | dp = writeescapedchar(dest, dp, c); | |
118 | } else if (!iswordcharacter(c)) { | |
119 | /* interpret word int_t */ | |
120 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
121 | int closespan = 1; | |
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
122 | if (check_keyword(hltr->word, hltr->keywords)) { |
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, |
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
124 | "<span class=\"c2html-keyword\">"); |
21 | 125 | } else if (hltr->istype(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
|
126 | 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
|
127 | "<span class=\"c2html-type\">"); |
21 | 128 | } else if (hltr->isdirective(hltr->word)) { |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
129 | 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
|
130 | "<span class=\"c2html-directive\">"); |
36
be60c22cddfe
fixed possible naming conflicts with is.* functions
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
131 | } 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
|
132 | 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
|
133 | "<span class=\"c2html-macroconst\">"); |
21 | 134 | } else { |
135 | closespan = 0; | |
136 | } | |
137 | for (int i = 0 ; i < wp ; i++) { | |
138 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
139 | } | |
140 | if (closespan) { | |
29
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
141 | memcpy_const(dest, dp, "</span>"); |
21 | 142 | } |
143 | } | |
144 | memset(hltr->word, 0, WORDBUF_SIZE); | |
145 | wp = 0; | |
146 | dp = writeescapedchar(dest, dp, c); | |
147 | } else { | |
148 | /* read word */ | |
149 | if (wp < WORDBUF_SIZE) { | |
150 | hltr->word[wp++] = c; | |
151 | } else if (wp == WORDBUF_SIZE) { | |
152 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
153 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
154 | } | |
155 | wp++; | |
156 | dp = writeescapedchar(dest, dp, c); | |
157 | } else { | |
158 | dp = writeescapedchar(dest, dp, c); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | isescaping = !isescaping & (c == '\\'); | |
164 | } | |
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
|
165 | } while (c != '\n'); |
21 | 166 | 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
|
167 | |
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
|
168 | /* 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
|
169 | 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
|
170 | destbuf->size += dp; |
21 | 171 | } |