Wed, 10 Feb 2016 12:45:14 +0100
applied multi line comment fix to java code generator
| 21 | 1 | /* | 
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 
24
 
e43dee5892f4
improved code structure and added option for disabling line numbers
 
Olaf Wintermann <olaf.wintermann@gmail.com> 
parents: 
21 
diff
changeset
 | 
4 | * Copyright 2015 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 | ||
| 44 | int isjtype(char *word, size_t len) { | |
| 45 | return isupper(word[0]); | |
| 46 | } | |
| 47 | ||
| 48 | int isjdirective(char *word) { | |
| 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 | |
| 21 | 55 | void jparseline(char *src, char *dest, highlighter_t *hltr) { | 
| 56 | memset(hltr->word, 0, WORDBUF_SIZE); | |
| 
32
 
10389d866a4d
applied multi line comment fix to java code generator
 
Mike Becker <universe@uap-core.de> 
parents: 
29 
diff
changeset
 | 
57 | size_t wp = 0, sp = 0, dp = 0; | 
| 21 | 58 | 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
 | 
59 | char quote = '\0'; | 
| 21 | 60 | int isescaping = 0; | 
| 61 | ||
| 62 | if (hltr->iscommentml) { | |
| 63 | iscomment = 1; | |
| 
29
 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 
Mike Becker <universe@uap-core.de> 
parents: 
28 
diff
changeset
 | 
64 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); | 
| 21 | 65 | } | 
| 66 | ||
| 67 | for (char c = src[sp] ; c ; c=src[++sp]) { | |
| 68 | /* comments */ | |
| 
26
 
05c3c6842aef
fixed wrong comment formatting in strings
 
Mike Becker <universe@uap-core.de> 
parents: 
24 
diff
changeset
 | 
69 | if (!isstring && c == '/') { | 
| 21 | 70 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { | 
| 71 | iscomment = 0; | |
| 72 | 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
 | 
73 | memcpy_const(dest, dp, "/</span>"); | 
| 21 | 74 | continue; | 
| 75 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { | |
| 76 | iscomment = 1; | |
| 77 | 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
 | 
78 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); | 
| 21 | 79 | } | 
| 80 | } | |
| 81 | ||
| 82 | if (iscomment) { | |
| 83 | if (c == '\n') { | |
| 84 | memcpy(&(dest[dp]), "</span>", 7); | |
| 85 | dp += 7; | |
| 86 | } | |
| 87 | dp = writeescapedchar(dest, dp, c); | |
| 88 | } else if (isimport) { | |
| 89 | // TODO: local imports | |
| 90 | } else { | |
| 91 | /* strings */ | |
| 92 | if (!isescaping && (c == '\'' || c == '\"')) { | |
| 93 | if (isstring) { | |
| 
29
 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 
Mike Becker <universe@uap-core.de> 
parents: 
28 
diff
changeset
 | 
94 | 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
 | 
95 | if (c == quote) { | 
| 
 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 
Mike Becker <universe@uap-core.de> 
parents: 
26 
diff
changeset
 | 
96 | isstring = 0; | 
| 
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, "</span>"); | 
| 
28
 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 
Mike Becker <universe@uap-core.de> 
parents: 
26 
diff
changeset
 | 
98 | } else { | 
| 
 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 
Mike Becker <universe@uap-core.de> 
parents: 
26 
diff
changeset
 | 
99 | 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
 | 
100 | } | 
| 21 | 101 | } else { | 
| 
28
 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 
Mike Becker <universe@uap-core.de> 
parents: 
26 
diff
changeset
 | 
102 | isstring = 1; | 
| 
 
1be8ea902ef4
fixed string highlighting when different quote symbol is in string
 
Mike Becker <universe@uap-core.de> 
parents: 
26 
diff
changeset
 | 
103 | quote = c; | 
| 
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, | 
| 
 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 
Mike Becker <universe@uap-core.de> 
parents: 
28 
diff
changeset
 | 
105 | "<span class=\"c2html-string\">"); | 
| 21 | 106 | dp = writeescapedchar(dest, dp, c); | 
| 107 | } | |
| 108 | } else { | |
| 109 | if (isstring) { | |
| 110 | dp = writeescapedchar(dest, dp, c); | |
| 111 | } else if (!iswordcharacter(c)) { | |
| 112 | /* interpret word int_t */ | |
| 113 | if (wp > 0 && wp < WORDBUF_SIZE) { | |
| 114 | int closespan = 1; | |
| 115 | if (iskeyword(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
 | 
116 | 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
 | 
117 | "<span class=\"c2html-keyword\">"); | 
| 21 | 118 | } 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
 | 
119 | 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
 | 
120 | "<span class=\"c2html-type\">"); | 
| 21 | 121 | } 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
 | 
122 | 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
 | 
123 | "<span class=\"c2html-directive\">"); | 
| 21 | 124 | } else if (iscapsonly(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
 | 
125 | 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
 | 
126 | "<span class=\"c2html-macroconst\">"); | 
| 21 | 127 | } else { | 
| 128 | closespan = 0; | |
| 129 | } | |
| 130 | for (int i = 0 ; i < wp ; i++) { | |
| 131 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 132 | } | |
| 133 | if (closespan) { | |
| 
29
 
ec6e97454e64
introduced macro for constant string memcpy + fixed string highlight fix
 
Mike Becker <universe@uap-core.de> 
parents: 
28 
diff
changeset
 | 
134 | memcpy_const(dest, dp, "</span>"); | 
| 21 | 135 | } | 
| 136 | } | |
| 137 | memset(hltr->word, 0, WORDBUF_SIZE); | |
| 138 | wp = 0; | |
| 139 | dp = writeescapedchar(dest, dp, c); | |
| 140 | } else { | |
| 141 | /* read word */ | |
| 142 | if (wp < WORDBUF_SIZE) { | |
| 143 | hltr->word[wp++] = c; | |
| 144 | } else if (wp == WORDBUF_SIZE) { | |
| 145 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { | |
| 146 | dp = writeescapedchar(dest, dp, hltr->word[i]); | |
| 147 | } | |
| 148 | wp++; | |
| 149 | dp = writeescapedchar(dest, dp, c); | |
| 150 | } else { | |
| 151 | dp = writeescapedchar(dest, dp, c); | |
| 152 | } | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 156 | isescaping = !isescaping & (c == '\\'); | |
| 157 | } | |
| 158 | } | |
| 159 | dest[dp] = 0; | |
| 160 | } |