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 "javacodegen.h" | |
| 31 | ||
| 32 | const char* jkeywords[] = { | |
| 33 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", | |
| 34 | "package", "synchronized", "boolean", "do", "if", "private", "this", | |
| 35 | "break", "double", "implements", "protected", "throw", "byte", "else", | |
| 36 | "import", "public", "throws", "case", "enum", "instanceof", "return", | |
| 37 | "transient", "catch", "extends", "int", "short", "try", "char", "final", | |
| 38 | "interface", "static", "void", "class", "finally", "long", "strictfp", | |
| 39 | "volatile", "const", "float", "native", "super", "while", NULL | |
| 40 | }; | |
| 41 | ||
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 42 | void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) { | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 43 | |
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 44 | /* TODO: try to replace this buffer */ | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 45 | char wordbuf[WORDBUF_SIZE]; | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 46 | sstr_t word; | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 47 | word.ptr = wordbuf; word.length = 0; | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 48 | |
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 49 | size_t sp = (size_t)-1; | 
| 21 | 50 | 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: 
26diff
changeset | 51 | char quote = '\0'; | 
| 21 | 52 | int isescaping = 0; | 
| 53 | ||
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 54 | if (*multiline_comment) { | 
| 21 | 55 | iscomment = 1; | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 56 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); | 
| 21 | 57 | } | 
| 58 | ||
| 39 
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
 Mike Becker <universe@uap-core.de> parents: 
36diff
changeset | 59 | 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: 
36diff
changeset | 60 | do { | 
| 
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
 Mike Becker <universe@uap-core.de> parents: 
36diff
changeset | 61 | 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: 
36diff
changeset | 62 | 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: 
36diff
changeset | 63 | |
| 21 | 64 | /* comments */ | 
| 26 
05c3c6842aef
fixed wrong comment formatting in strings
 Mike Becker <universe@uap-core.de> parents: 
24diff
changeset | 65 | if (!isstring && c == '/') { | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 66 | if (*multiline_comment && sp > 0 && src[sp-1] == '*') { | 
| 21 | 67 | iscomment = 0; | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 68 | *multiline_comment = 0; | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 69 | ucx_buffer_puts(dest, "/</span>"); | 
| 21 | 70 | continue; | 
| 71 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
| 72 | iscomment = 1; | |
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 73 | *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: 
47diff
changeset | 74 | ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); | 
| 21 | 75 | } | 
| 76 | } | |
| 77 | ||
| 78 | if (iscomment) { | |
| 79 | if (c == '\n') { | |
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 80 | 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: 
47diff
changeset | 81 | } else { | 
| 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 82 | put_htmlescaped(dest, c); | 
| 21 | 83 | } | 
| 84 | } else if (isimport) { | |
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 85 | /* TODO: local imports */ | 
| 21 | 86 | } else { | 
| 87 | /* strings */ | |
| 88 | if (!isescaping && (c == '\'' || c == '\"')) { | |
| 89 | if (isstring) { | |
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 90 | put_htmlescaped(dest, c); | 
| 28 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 91 | if (c == quote) { | 
| 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 92 | isstring = 0; | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 93 | ucx_buffer_puts(dest, "</span>"); | 
| 28 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 94 | } else { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 95 | put_htmlescaped(dest, c); | 
| 28 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 96 | } | 
| 21 | 97 | } else { | 
| 28 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 98 | isstring = 1; | 
| 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 Mike Becker <universe@uap-core.de> parents: 
26diff
changeset | 99 | quote = c; | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 100 | ucx_buffer_puts(dest, | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 101 | "<span class=\"c2html-string\">"); | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 102 | put_htmlescaped(dest, c); | 
| 21 | 103 | } | 
| 104 | } else { | |
| 105 | if (isstring) { | |
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 106 | put_htmlescaped(dest, c); | 
| 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 107 | } else if (!check_alnumex(c)) { | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 108 | if (word.length > 0 && word.length < WORDBUF_SIZE) { | 
| 21 | 109 | int closespan = 1; | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 110 | if (check_keyword(word, jkeywords)) { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 111 | ucx_buffer_puts(dest, | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 112 | "<span class=\"c2html-keyword\">"); | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 113 | } else if (isupper(word.ptr[0])) { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 114 | ucx_buffer_puts(dest, | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 115 | "<span class=\"c2html-type\">"); | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 116 | } else if (word.ptr[0] == '@') { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 117 | ucx_buffer_puts(dest, | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 118 | "<span class=\"c2html-directive\">"); | 
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 119 | } 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: 
47diff
changeset | 120 | ucx_buffer_puts(dest, | 
| 29 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 Mike Becker <universe@uap-core.de> parents: 
28diff
changeset | 121 | "<span class=\"c2html-macroconst\">"); | 
| 21 | 122 | } else { | 
| 123 | closespan = 0; | |
| 124 | } | |
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 125 | put_htmlescapedstr(dest, word); | 
| 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 126 | |
| 21 | 127 | if (closespan) { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 128 | ucx_buffer_puts(dest, "</span>"); | 
| 21 | 129 | } | 
| 130 | } | |
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 131 | word.length = 0; | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 132 | put_htmlescaped(dest, c); | 
| 21 | 133 | } else { | 
| 134 | /* read word */ | |
| 47 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 135 | if (word.length < WORDBUF_SIZE) { | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 136 | word.ptr[word.length++] = c; | 
| 
c39ecbbca7c0
words (token) are now stored as sstr_t
 Mike Becker <universe@uap-core.de> parents: 
46diff
changeset | 137 | } 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: 
47diff
changeset | 138 | /* 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: 
47diff
changeset | 139 | ucx_buffer_puts(dest, | 
| 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 140 | "!!! 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: 
47diff
changeset | 141 | word.length = 0; | 
| 21 | 142 | } else { | 
| 48 
b2724c711203
highlighter now use the UcxBuffer API for writing to the destination buffer
 Mike Becker <universe@uap-core.de> parents: 
47diff
changeset | 143 | put_htmlescaped(dest, c); | 
| 21 | 144 | } | 
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | isescaping = !isescaping & (c == '\\'); | |
| 149 | } | |
| 39 
ac35daceb24c
adds UCX + changes how the input file is read (uses an consecutive memory area now)
 Mike Becker <universe@uap-core.de> parents: 
36diff
changeset | 150 | } while (c != '\n'); | 
| 21 | 151 | } |