Fri, 04 Mar 2016 15:02:22 +0100
fixed possible naming conflicts with is.* functions
universe@21 | 1 | /* |
universe@21 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@21 | 3 | * |
universe@35 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
universe@21 | 5 | * |
universe@21 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@21 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@21 | 8 | * |
universe@21 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@21 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@21 | 11 | * |
universe@21 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@21 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@21 | 14 | * documentation and/or other materials provided with the distribution. |
universe@21 | 15 | * |
universe@21 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@21 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@21 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@21 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@21 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@21 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@21 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@21 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@21 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@21 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@21 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@21 | 27 | * |
universe@21 | 28 | */ |
universe@21 | 29 | |
universe@21 | 30 | #include "javacodegen.h" |
universe@21 | 31 | #include <string.h> |
universe@21 | 32 | #include <ctype.h> |
universe@21 | 33 | |
universe@21 | 34 | const char* jkeywords[] = { |
universe@21 | 35 | "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", |
universe@21 | 36 | "package", "synchronized", "boolean", "do", "if", "private", "this", |
universe@21 | 37 | "break", "double", "implements", "protected", "throw", "byte", "else", |
universe@21 | 38 | "import", "public", "throws", "case", "enum", "instanceof", "return", |
universe@21 | 39 | "transient", "catch", "extends", "int", "short", "try", "char", "final", |
universe@21 | 40 | "interface", "static", "void", "class", "finally", "long", "strictfp", |
universe@21 | 41 | "volatile", "const", "float", "native", "super", "while", NULL |
universe@21 | 42 | }; |
universe@21 | 43 | |
universe@36 | 44 | int check_jtype(char *word, size_t len) { |
universe@21 | 45 | return isupper(word[0]); |
universe@21 | 46 | } |
universe@21 | 47 | |
universe@36 | 48 | int check_jdirective(char *word) { |
universe@21 | 49 | return word[0] == '@'; |
universe@21 | 50 | } |
universe@21 | 51 | |
universe@29 | 52 | #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ |
universe@29 | 53 | dp += sizeof(str)-1 |
universe@29 | 54 | |
universe@21 | 55 | void jparseline(char *src, char *dest, highlighter_t *hltr) { |
universe@21 | 56 | memset(hltr->word, 0, WORDBUF_SIZE); |
universe@32 | 57 | size_t wp = 0, sp = 0, dp = 0; |
universe@21 | 58 | int isstring = 0, iscomment = 0, isimport = 0; |
universe@28 | 59 | char quote = '\0'; |
universe@21 | 60 | int isescaping = 0; |
universe@21 | 61 | |
universe@21 | 62 | if (hltr->iscommentml) { |
universe@21 | 63 | iscomment = 1; |
universe@29 | 64 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); |
universe@21 | 65 | } |
universe@21 | 66 | |
universe@21 | 67 | for (char c = src[sp] ; c ; c=src[++sp]) { |
universe@21 | 68 | /* comments */ |
universe@26 | 69 | if (!isstring && c == '/') { |
universe@21 | 70 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { |
universe@21 | 71 | iscomment = 0; |
universe@21 | 72 | hltr->iscommentml = 0; |
universe@29 | 73 | memcpy_const(dest, dp, "/</span>"); |
universe@21 | 74 | continue; |
universe@21 | 75 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { |
universe@21 | 76 | iscomment = 1; |
universe@21 | 77 | hltr->iscommentml = (src[sp+1] == '*'); |
universe@29 | 78 | memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); |
universe@21 | 79 | } |
universe@21 | 80 | } |
universe@21 | 81 | |
universe@21 | 82 | if (iscomment) { |
universe@21 | 83 | if (c == '\n') { |
universe@21 | 84 | memcpy(&(dest[dp]), "</span>", 7); |
universe@21 | 85 | dp += 7; |
universe@21 | 86 | } |
universe@21 | 87 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 88 | } else if (isimport) { |
universe@21 | 89 | // TODO: local imports |
universe@21 | 90 | } else { |
universe@21 | 91 | /* strings */ |
universe@21 | 92 | if (!isescaping && (c == '\'' || c == '\"')) { |
universe@21 | 93 | if (isstring) { |
universe@29 | 94 | dp = writeescapedchar(dest, dp, c); |
universe@28 | 95 | if (c == quote) { |
universe@28 | 96 | isstring = 0; |
universe@29 | 97 | memcpy_const(dest, dp, "</span>"); |
universe@28 | 98 | } else { |
universe@28 | 99 | dp = writeescapedchar(dest, dp, c); |
universe@28 | 100 | } |
universe@21 | 101 | } else { |
universe@28 | 102 | isstring = 1; |
universe@28 | 103 | quote = c; |
universe@29 | 104 | memcpy_const(dest, dp, |
universe@29 | 105 | "<span class=\"c2html-string\">"); |
universe@21 | 106 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 107 | } |
universe@21 | 108 | } else { |
universe@21 | 109 | if (isstring) { |
universe@21 | 110 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 111 | } else if (!iswordcharacter(c)) { |
universe@21 | 112 | /* interpret word int_t */ |
universe@21 | 113 | if (wp > 0 && wp < WORDBUF_SIZE) { |
universe@21 | 114 | int closespan = 1; |
universe@36 | 115 | if (check_keyword(hltr->word, hltr->keywords)) { |
universe@29 | 116 | memcpy_const(dest, dp, |
universe@29 | 117 | "<span class=\"c2html-keyword\">"); |
universe@21 | 118 | } else if (hltr->istype(hltr->word, wp)) { |
universe@29 | 119 | memcpy_const(dest, dp, |
universe@29 | 120 | "<span class=\"c2html-type\">"); |
universe@21 | 121 | } else if (hltr->isdirective(hltr->word)) { |
universe@29 | 122 | memcpy_const(dest, dp, |
universe@29 | 123 | "<span class=\"c2html-directive\">"); |
universe@36 | 124 | } else if (check_capsonly(hltr->word, wp)) { |
universe@29 | 125 | memcpy_const(dest, dp, |
universe@29 | 126 | "<span class=\"c2html-macroconst\">"); |
universe@21 | 127 | } else { |
universe@21 | 128 | closespan = 0; |
universe@21 | 129 | } |
universe@21 | 130 | for (int i = 0 ; i < wp ; i++) { |
universe@21 | 131 | dp = writeescapedchar(dest, dp, hltr->word[i]); |
universe@21 | 132 | } |
universe@21 | 133 | if (closespan) { |
universe@29 | 134 | memcpy_const(dest, dp, "</span>"); |
universe@21 | 135 | } |
universe@21 | 136 | } |
universe@21 | 137 | memset(hltr->word, 0, WORDBUF_SIZE); |
universe@21 | 138 | wp = 0; |
universe@21 | 139 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 140 | } else { |
universe@21 | 141 | /* read word */ |
universe@21 | 142 | if (wp < WORDBUF_SIZE) { |
universe@21 | 143 | hltr->word[wp++] = c; |
universe@21 | 144 | } else if (wp == WORDBUF_SIZE) { |
universe@21 | 145 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { |
universe@21 | 146 | dp = writeescapedchar(dest, dp, hltr->word[i]); |
universe@21 | 147 | } |
universe@21 | 148 | wp++; |
universe@21 | 149 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 150 | } else { |
universe@21 | 151 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 152 | } |
universe@21 | 153 | } |
universe@21 | 154 | } |
universe@21 | 155 | |
universe@21 | 156 | isescaping = !isescaping & (c == '\\'); |
universe@21 | 157 | } |
universe@21 | 158 | } |
universe@21 | 159 | dest[dp] = 0; |
universe@21 | 160 | } |