merges all highlighter functions into one highlighter module

Fri, 26 Aug 2016 13:49:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 26 Aug 2016 13:49:19 +0200
changeset 52
33ded421c512
parent 51
f25ba6fd7a08
child 53
5e47a26a16f0

merges all highlighter functions into one highlighter module

Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
src/ccodegen.c file | annotate | diff | comparison | revisions
src/ccodegen.h file | annotate | diff | comparison | revisions
src/codegens.c file | annotate | diff | comparison | revisions
src/codegens.h file | annotate | diff | comparison | revisions
src/highlighter.c file | annotate | diff | comparison | revisions
src/highlighter.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Thu Aug 25 12:16:57 2016 +0200
     1.2 +++ b/Makefile	Fri Aug 26 13:49:19 2016 +0200
     1.3 @@ -33,9 +33,7 @@
     1.4  
     1.5  
     1.6  SRC  = c2html.c
     1.7 -SRC += codegens.c
     1.8 -SRC += ccodegen.c
     1.9 -SRC += javacodegen.c
    1.10 +SRC += highlighter.c
    1.11  SRC += ucx/allocator.c
    1.12  SRC += ucx/buffer.c
    1.13  SRC += ucx/list.c
     2.1 --- a/src/c2html.c	Thu Aug 25 12:16:57 2016 +0200
     2.2 +++ b/src/c2html.c	Fri Aug 26 13:49:19 2016 +0200
     2.3 @@ -26,7 +26,11 @@
     2.4   * POSSIBILITY OF SUCH DAMAGE.
     2.5   *
     2.6   */
     2.7 +
     2.8 +#include <unistd.h>
     2.9 +
    2.10  #include "c2html.h"
    2.11 +#include "highlighter.h"
    2.12  
    2.13  #include "ucx/list.h"
    2.14  
    2.15 @@ -45,14 +49,6 @@
    2.16          "\n");
    2.17  }
    2.18  
    2.19 -static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    2.20 -    while (*src && *src != '\n') {
    2.21 -        put_htmlescaped(dest, *src);
    2.22 -        src++;
    2.23 -    }
    2.24 -    ucx_buffer_putc(dest, '\n');
    2.25 -}
    2.26 -
    2.27  void formatlines(highlighter_func highlighter,
    2.28          UcxList *in, write_func out, void *stream, int showlineno) {
    2.29      
     3.1 --- a/src/c2html.h	Thu Aug 25 12:16:57 2016 +0200
     3.2 +++ b/src/c2html.h	Fri Aug 26 13:49:19 2016 +0200
     3.3 @@ -30,12 +30,6 @@
     3.4  #ifndef C2HTML_H
     3.5  #define	C2HTML_H
     3.6  
     3.7 -#include <fcntl.h>
     3.8 -#include <unistd.h>
     3.9 -
    3.10 -#include "javacodegen.h"
    3.11 -#include "ccodegen.h"
    3.12 -
    3.13  #ifdef	__cplusplus
    3.14  extern "C" {
    3.15  #endif
     4.1 --- a/src/ccodegen.c	Thu Aug 25 12:16:57 2016 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,170 +0,0 @@
     4.4 -/*
     4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 - *
     4.7 - * Copyright 2016 Mike Becker. All rights reserved.
     4.8 - *
     4.9 - * Redistribution and use in source and binary forms, with or without
    4.10 - * modification, are permitted provided that the following conditions are met:
    4.11 - *
    4.12 - *   1. Redistributions of source code must retain the above copyright
    4.13 - *      notice, this list of conditions and the following disclaimer.
    4.14 - *
    4.15 - *   2. Redistributions in binary form must reproduce the above copyright
    4.16 - *      notice, this list of conditions and the following disclaimer in the
    4.17 - *      documentation and/or other materials provided with the distribution.
    4.18 - *
    4.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    4.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    4.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    4.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    4.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    4.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    4.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    4.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    4.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    4.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    4.29 - * POSSIBILITY OF SUCH DAMAGE.
    4.30 - *
    4.31 - */
    4.32 -
    4.33 -#include "ccodegen.h"
    4.34 -
    4.35 -const char* ckeywords[] = {
    4.36 -    "auto", "break", "case", "char", "const", "continue", "default", "do",
    4.37 -    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
    4.38 -    "long", "register", "return", "short", "signed", "sizeof", "static",
    4.39 -    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
    4.40 -    "while", NULL
    4.41 -};
    4.42 -
    4.43 -void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    4.44 -    /* reset buffers without clearing them */
    4.45 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    4.46 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    4.47 -    
    4.48 -    /* alias the buffers for better handling */
    4.49 -    UcxBuffer *wbuf = hd->primary_buffer;
    4.50 -    UcxBuffer *ifilebuf = hd->secondary_buffer;
    4.51 -    
    4.52 -    /* local information */
    4.53 -    size_t sp = (size_t)-1;
    4.54 -    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    4.55 -    char quote = '\0';
    4.56 -    int isescaping = 0;
    4.57 -    
    4.58 -    /* continue a multi line comment highlighting */
    4.59 -    if (hd->multiline_comment) {
    4.60 -        iscomment = 1;
    4.61 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    4.62 -    }
    4.63 -
    4.64 -    char c;
    4.65 -    do {
    4.66 -        c = src[++sp];
    4.67 -        if (!c) break;
    4.68 -        
    4.69 -        /* comments */
    4.70 -        if (!isstring && c == '/') {
    4.71 -            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    4.72 -                iscomment = 0;
    4.73 -                hd->multiline_comment = 0;
    4.74 -                ucx_buffer_puts(dest, "/</span>");
    4.75 -                continue;
    4.76 -            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    4.77 -                iscomment = 1;
    4.78 -                hd->multiline_comment = (src[sp+1] == '*');
    4.79 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    4.80 -            }
    4.81 -        }
    4.82 -
    4.83 -        if (iscomment) {
    4.84 -            if (c == '\n') {
    4.85 -                ucx_buffer_puts(dest, "</span>\n");
    4.86 -            } else {
    4.87 -                put_htmlescaped(dest, c);
    4.88 -            }
    4.89 -        } else if (isinclude) {
    4.90 -            if (c == '<') {
    4.91 -                ucx_buffer_puts(dest,
    4.92 -                        "<span class=\"c2html-stdinclude\">&lt;");
    4.93 -            } else if (c == '\"') {
    4.94 -                if (parseinclude) {
    4.95 -                    ucx_buffer_puts(dest, "\">");
    4.96 -                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
    4.97 -                    ucx_buffer_puts(dest, "\"</a>");
    4.98 -                    parseinclude = 0;
    4.99 -                } else {
   4.100 -                    ucx_buffer_puts(dest,
   4.101 -                            "<a class=\"c2html-userinclude\" href=\"");
   4.102 -                    ucx_buffer_putc(ifilebuf, '\"');
   4.103 -                    parseinclude = 1;
   4.104 -                }
   4.105 -            } else if (c == '>') {
   4.106 -                ucx_buffer_puts(dest,  "&gt;</span>");
   4.107 -            } else {
   4.108 -                if (parseinclude) {
   4.109 -                    ucx_buffer_putc(ifilebuf, c);
   4.110 -                }
   4.111 -                put_htmlescaped(dest, c);
   4.112 -            }
   4.113 -        } else {
   4.114 -            /* strings */
   4.115 -            if (!isescaping && (c == '\'' || c == '\"')) {
   4.116 -                if (isstring) {
   4.117 -                    put_htmlescaped(dest, c);
   4.118 -                    if (c == quote) {
   4.119 -                        isstring = 0;
   4.120 -                        ucx_buffer_puts(dest, "</span>");
   4.121 -                    } else {
   4.122 -                        put_htmlescaped(dest, c);
   4.123 -                    }
   4.124 -                } else {
   4.125 -                    isstring = 1;
   4.126 -                    quote = c;
   4.127 -                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   4.128 -                    put_htmlescaped(dest, c);
   4.129 -                }
   4.130 -            } else {
   4.131 -                if (isstring) {
   4.132 -                    put_htmlescaped(dest, c);
   4.133 -                } else if (!isalnum(c) && c!='_' && c!='#') {
   4.134 -                    /* write buffered word, if any */
   4.135 -                    if (wbuf->size > 0) {
   4.136 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   4.137 -                        int closespan = 1;
   4.138 -                        sstr_t typesuffix = ST("_t");
   4.139 -                        if (check_keyword(word, ckeywords)) {
   4.140 -                            ucx_buffer_puts(dest,
   4.141 -                                    "<span class=\"c2html-keyword\">");
   4.142 -                        } else if (sstrsuffix(word, typesuffix)) {
   4.143 -                            ucx_buffer_puts(dest,
   4.144 -                                "<span class=\"c2html-type\">");
   4.145 -                        } else if (word.ptr[0] == '#') {
   4.146 -                            isinclude = !sstrcmp(word, S("#include"));
   4.147 -                            ucx_buffer_puts(dest,
   4.148 -                                "<span class=\"c2html-directive\">");
   4.149 -                        } else if (check_capsonly(word)) {
   4.150 -                            ucx_buffer_puts(dest,
   4.151 -                                "<span class=\"c2html-macroconst\">");
   4.152 -                        } else {
   4.153 -                            closespan = 0;
   4.154 -                        }
   4.155 -                        put_htmlescapedstr(dest, word);
   4.156 -                        if (closespan) {
   4.157 -                            ucx_buffer_puts(dest, "</span>");
   4.158 -                        }
   4.159 -                    }
   4.160 -                    wbuf->pos = wbuf->size = 0; /* reset word buffer */
   4.161 -                    
   4.162 -                    /* write current character */
   4.163 -                    put_htmlescaped(dest, c);
   4.164 -                } else {
   4.165 -                    /* buffer the current word */
   4.166 -                    ucx_buffer_putc(wbuf, c);
   4.167 -                }
   4.168 -            }
   4.169 -
   4.170 -            isescaping = !isescaping & (c == '\\');
   4.171 -        }
   4.172 -    } while (c != '\n');
   4.173 -}
     5.1 --- a/src/ccodegen.h	Thu Aug 25 12:16:57 2016 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,46 +0,0 @@
     5.4 -/*
     5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 - *
     5.7 - * Copyright 2016 Mike Becker. All rights reserved.
     5.8 - *
     5.9 - * Redistribution and use in source and binary forms, with or without
    5.10 - * modification, are permitted provided that the following conditions are met:
    5.11 - *
    5.12 - *   1. Redistributions of source code must retain the above copyright
    5.13 - *      notice, this list of conditions and the following disclaimer.
    5.14 - *
    5.15 - *   2. Redistributions in binary form must reproduce the above copyright
    5.16 - *      notice, this list of conditions and the following disclaimer in the
    5.17 - *      documentation and/or other materials provided with the distribution.
    5.18 - *
    5.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    5.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    5.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    5.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    5.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    5.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    5.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    5.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    5.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    5.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    5.29 - * POSSIBILITY OF SUCH DAMAGE.
    5.30 - *
    5.31 - */
    5.32 -
    5.33 -#ifndef CCODEGEN_H
    5.34 -#define	CCODEGEN_H
    5.35 -
    5.36 -#include "codegens.h"
    5.37 -
    5.38 -#ifdef	__cplusplus
    5.39 -extern "C" {
    5.40 -#endif
    5.41 -
    5.42 -void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
    5.43 -
    5.44 -#ifdef	__cplusplus
    5.45 -}
    5.46 -#endif
    5.47 -
    5.48 -#endif	/* CCODEGEN_H */
    5.49 -
     6.1 --- a/src/codegens.c	Thu Aug 25 12:16:57 2016 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,83 +0,0 @@
     6.4 -/*
     6.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 - *
     6.7 - * Copyright 2016 Mike Becker. All rights reserved.
     6.8 - *
     6.9 - * Redistribution and use in source and binary forms, with or without
    6.10 - * modification, are permitted provided that the following conditions are met:
    6.11 - *
    6.12 - *   1. Redistributions of source code must retain the above copyright
    6.13 - *      notice, this list of conditions and the following disclaimer.
    6.14 - *
    6.15 - *   2. Redistributions in binary form must reproduce the above copyright
    6.16 - *      notice, this list of conditions and the following disclaimer in the
    6.17 - *      documentation and/or other materials provided with the distribution.
    6.18 - *
    6.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    6.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    6.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    6.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    6.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    6.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    6.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    6.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    6.29 - * POSSIBILITY OF SUCH DAMAGE.
    6.30 - *
    6.31 - */
    6.32 -
    6.33 -#include "codegens.h"
    6.34 -
    6.35 -HighlighterData* new_highlighter_data() {
    6.36 -    HighlighterData* hd = malloc(sizeof(HighlighterData));
    6.37 -    if (hd == NULL) {
    6.38 -        return NULL;
    6.39 -    } else {
    6.40 -        hd->multiline_comment = 0;
    6.41 -        hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
    6.42 -        hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    6.43 -        return hd;
    6.44 -    }
    6.45 -}
    6.46 -
    6.47 -void free_highlighter_data(HighlighterData *hd) {
    6.48 -    ucx_buffer_free(hd->primary_buffer);
    6.49 -    ucx_buffer_free(hd->secondary_buffer);
    6.50 -    free(hd);
    6.51 -}
    6.52 -
    6.53 -void put_htmlescaped(UcxBuffer *dest, char c) {
    6.54 -    if (c == '>') {
    6.55 -        ucx_buffer_puts(dest, "&gt;");
    6.56 -    } else if (c == '<') {
    6.57 -        ucx_buffer_puts(dest, "&lt;");
    6.58 -    } else {
    6.59 -        ucx_buffer_putc(dest, c);
    6.60 -    }
    6.61 -}
    6.62 -
    6.63 -void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
    6.64 -    for (int i = 0 ; i < s.length ; i++) {
    6.65 -        put_htmlescaped(dest, s.ptr[i]);
    6.66 -    }
    6.67 -}
    6.68 -
    6.69 -int check_keyword(sstr_t word, const char** keywords) {
    6.70 -    for (int i = 0 ; keywords[i] ; i++) {
    6.71 -        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
    6.72 -            return 1;
    6.73 -        }
    6.74 -    }
    6.75 -    return 0;
    6.76 -}
    6.77 -
    6.78 -int check_capsonly(sstr_t word) {
    6.79 -    for (size_t i = 0 ; i < word.length ; i++) {
    6.80 -        if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
    6.81 -                && word.ptr[i] != '_') {
    6.82 -            return 0;
    6.83 -        }
    6.84 -    }
    6.85 -    return 1;
    6.86 -}
     7.1 --- a/src/codegens.h	Thu Aug 25 12:16:57 2016 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,68 +0,0 @@
     7.4 -/*
     7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6 - *
     7.7 - * Copyright 2016 Mike Becker. All rights reserved.
     7.8 - *
     7.9 - * Redistribution and use in source and binary forms, with or without
    7.10 - * modification, are permitted provided that the following conditions are met:
    7.11 - *
    7.12 - *   1. Redistributions of source code must retain the above copyright
    7.13 - *      notice, this list of conditions and the following disclaimer.
    7.14 - *
    7.15 - *   2. Redistributions in binary form must reproduce the above copyright
    7.16 - *      notice, this list of conditions and the following disclaimer in the
    7.17 - *      documentation and/or other materials provided with the distribution.
    7.18 - *
    7.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    7.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    7.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    7.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    7.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    7.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    7.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    7.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    7.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    7.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    7.29 - * POSSIBILITY OF SUCH DAMAGE.
    7.30 - *
    7.31 - */
    7.32 -
    7.33 -#ifndef CODEGENS_H
    7.34 -#define	CODEGENS_H
    7.35 -
    7.36 -#include <stdlib.h>
    7.37 -#include <stdio.h>
    7.38 -#include <string.h>
    7.39 -#include <ctype.h>
    7.40 -#include "ucx/string.h"
    7.41 -#include "ucx/buffer.h"
    7.42 -#include "ucx/utils.h"
    7.43 -
    7.44 -#ifdef	__cplusplus
    7.45 -extern "C" {
    7.46 -#endif
    7.47 -    
    7.48 -typedef struct {
    7.49 -    int multiline_comment;
    7.50 -    UcxBuffer* primary_buffer;
    7.51 -    UcxBuffer* secondary_buffer;
    7.52 -} HighlighterData;
    7.53 -
    7.54 -HighlighterData* new_highlighter_data();
    7.55 -void free_highlighter_data(HighlighterData*);
    7.56 -
    7.57 -typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
    7.58 -
    7.59 -void put_htmlescaped(UcxBuffer *dest, char c);
    7.60 -void put_htmlescapedstr(UcxBuffer *dest, sstr_t s);
    7.61 -
    7.62 -int check_keyword(sstr_t word, const char** keywords);
    7.63 -int check_capsonly(sstr_t word);
    7.64 -
    7.65 -
    7.66 -#ifdef	__cplusplus
    7.67 -}
    7.68 -#endif
    7.69 -
    7.70 -#endif	/* CODEGENS_H */
    7.71 -
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/highlighter.c	Fri Aug 26 13:49:19 2016 +0200
     8.3 @@ -0,0 +1,356 @@
     8.4 +/*
     8.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     8.6 + *
     8.7 + * Copyright 2016 Mike Becker. All rights reserved.
     8.8 + *
     8.9 + * Redistribution and use in source and binary forms, with or without
    8.10 + * modification, are permitted provided that the following conditions are met:
    8.11 + *
    8.12 + *   1. Redistributions of source code must retain the above copyright
    8.13 + *      notice, this list of conditions and the following disclaimer.
    8.14 + *
    8.15 + *   2. Redistributions in binary form must reproduce the above copyright
    8.16 + *      notice, this list of conditions and the following disclaimer in the
    8.17 + *      documentation and/or other materials provided with the distribution.
    8.18 + *
    8.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    8.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    8.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    8.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    8.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    8.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    8.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    8.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    8.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    8.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    8.29 + * POSSIBILITY OF SUCH DAMAGE.
    8.30 + *
    8.31 + */
    8.32 +
    8.33 +#include "highlighter.h"
    8.34 +
    8.35 +HighlighterData* new_highlighter_data() {
    8.36 +    HighlighterData* hd = malloc(sizeof(HighlighterData));
    8.37 +    if (hd == NULL) {
    8.38 +        return NULL;
    8.39 +    } else {
    8.40 +        hd->multiline_comment = 0;
    8.41 +        hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
    8.42 +        hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    8.43 +        return hd;
    8.44 +    }
    8.45 +}
    8.46 +
    8.47 +void free_highlighter_data(HighlighterData *hd) {
    8.48 +    ucx_buffer_free(hd->primary_buffer);
    8.49 +    ucx_buffer_free(hd->secondary_buffer);
    8.50 +    free(hd);
    8.51 +}
    8.52 +
    8.53 +static void put_htmlescaped(UcxBuffer *dest, char c) {
    8.54 +    if (c == '>') {
    8.55 +        ucx_buffer_puts(dest, "&gt;");
    8.56 +    } else if (c == '<') {
    8.57 +        ucx_buffer_puts(dest, "&lt;");
    8.58 +    } else {
    8.59 +        ucx_buffer_putc(dest, c);
    8.60 +    }
    8.61 +}
    8.62 +
    8.63 +static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
    8.64 +    for (int i = 0 ; i < s.length ; i++) {
    8.65 +        put_htmlescaped(dest, s.ptr[i]);
    8.66 +    }
    8.67 +}
    8.68 +
    8.69 +static int check_keyword(sstr_t word, const char** keywords) {
    8.70 +    for (int i = 0 ; keywords[i] ; i++) {
    8.71 +        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
    8.72 +            return 1;
    8.73 +        }
    8.74 +    }
    8.75 +    return 0;
    8.76 +}
    8.77 +
    8.78 +static int check_capsonly(sstr_t word) {
    8.79 +    for (size_t i = 0 ; i < word.length ; i++) {
    8.80 +        if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
    8.81 +                && word.ptr[i] != '_') {
    8.82 +            return 0;
    8.83 +        }
    8.84 +    }
    8.85 +    return 1;
    8.86 +}
    8.87 +
    8.88 +/* Plaintext Highlighter */
    8.89 +
    8.90 +void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
    8.91 +    while (*src && *src != '\n') {
    8.92 +        put_htmlescaped(dest, *src);
    8.93 +        src++;
    8.94 +    }
    8.95 +    ucx_buffer_putc(dest, '\n');
    8.96 +}
    8.97 +
    8.98 +/* C Highlighter */
    8.99 +
   8.100 +static const char* ckeywords[] = {
   8.101 +    "auto", "break", "case", "char", "const", "continue", "default", "do",
   8.102 +    "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
   8.103 +    "long", "register", "return", "short", "signed", "sizeof", "static",
   8.104 +    "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
   8.105 +    "while", NULL
   8.106 +};
   8.107 +
   8.108 +void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
   8.109 +    /* reset buffers without clearing them */
   8.110 +    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
   8.111 +    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
   8.112 +    
   8.113 +    /* alias the buffers for better handling */
   8.114 +    UcxBuffer *wbuf = hd->primary_buffer;
   8.115 +    UcxBuffer *ifilebuf = hd->secondary_buffer;
   8.116 +    
   8.117 +    /* local information */
   8.118 +    size_t sp = (size_t)-1;
   8.119 +    int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
   8.120 +    char quote = '\0';
   8.121 +    int isescaping = 0;
   8.122 +    
   8.123 +    /* continue a multi line comment highlighting */
   8.124 +    if (hd->multiline_comment) {
   8.125 +        iscomment = 1;
   8.126 +        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   8.127 +    }
   8.128 +
   8.129 +    char c;
   8.130 +    do {
   8.131 +        c = src[++sp];
   8.132 +        if (!c) break;
   8.133 +        
   8.134 +        /* comments */
   8.135 +        if (!isstring && c == '/') {
   8.136 +            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
   8.137 +                iscomment = 0;
   8.138 +                hd->multiline_comment = 0;
   8.139 +                ucx_buffer_puts(dest, "/</span>");
   8.140 +                continue;
   8.141 +            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   8.142 +                iscomment = 1;
   8.143 +                hd->multiline_comment = (src[sp+1] == '*');
   8.144 +                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   8.145 +            }
   8.146 +        }
   8.147 +
   8.148 +        if (iscomment) {
   8.149 +            if (c == '\n') {
   8.150 +                ucx_buffer_puts(dest, "</span>\n");
   8.151 +            } else {
   8.152 +                put_htmlescaped(dest, c);
   8.153 +            }
   8.154 +        } else if (isinclude) {
   8.155 +            if (c == '<') {
   8.156 +                ucx_buffer_puts(dest,
   8.157 +                        "<span class=\"c2html-stdinclude\">&lt;");
   8.158 +            } else if (c == '\"') {
   8.159 +                if (parseinclude) {
   8.160 +                    ucx_buffer_puts(dest, "\">");
   8.161 +                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
   8.162 +                    ucx_buffer_puts(dest, "\"</a>");
   8.163 +                    parseinclude = 0;
   8.164 +                } else {
   8.165 +                    ucx_buffer_puts(dest,
   8.166 +                            "<a class=\"c2html-userinclude\" href=\"");
   8.167 +                    ucx_buffer_putc(ifilebuf, '\"');
   8.168 +                    parseinclude = 1;
   8.169 +                }
   8.170 +            } else if (c == '>') {
   8.171 +                ucx_buffer_puts(dest,  "&gt;</span>");
   8.172 +            } else {
   8.173 +                if (parseinclude) {
   8.174 +                    ucx_buffer_putc(ifilebuf, c);
   8.175 +                }
   8.176 +                put_htmlescaped(dest, c);
   8.177 +            }
   8.178 +        } else {
   8.179 +            /* strings */
   8.180 +            if (!isescaping && (c == '\'' || c == '\"')) {
   8.181 +                if (isstring) {
   8.182 +                    put_htmlescaped(dest, c);
   8.183 +                    if (c == quote) {
   8.184 +                        isstring = 0;
   8.185 +                        ucx_buffer_puts(dest, "</span>");
   8.186 +                    } else {
   8.187 +                        put_htmlescaped(dest, c);
   8.188 +                    }
   8.189 +                } else {
   8.190 +                    isstring = 1;
   8.191 +                    quote = c;
   8.192 +                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   8.193 +                    put_htmlescaped(dest, c);
   8.194 +                }
   8.195 +            } else {
   8.196 +                if (isstring) {
   8.197 +                    put_htmlescaped(dest, c);
   8.198 +                } else if (!isalnum(c) && c!='_' && c!='#') {
   8.199 +                    /* write buffered word, if any */
   8.200 +                    if (wbuf->size > 0) {
   8.201 +                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   8.202 +                        int closespan = 1;
   8.203 +                        sstr_t typesuffix = ST("_t");
   8.204 +                        if (check_keyword(word, ckeywords)) {
   8.205 +                            ucx_buffer_puts(dest,
   8.206 +                                    "<span class=\"c2html-keyword\">");
   8.207 +                        } else if (sstrsuffix(word, typesuffix)) {
   8.208 +                            ucx_buffer_puts(dest,
   8.209 +                                "<span class=\"c2html-type\">");
   8.210 +                        } else if (word.ptr[0] == '#') {
   8.211 +                            isinclude = !sstrcmp(word, S("#include"));
   8.212 +                            ucx_buffer_puts(dest,
   8.213 +                                "<span class=\"c2html-directive\">");
   8.214 +                        } else if (check_capsonly(word)) {
   8.215 +                            ucx_buffer_puts(dest,
   8.216 +                                "<span class=\"c2html-macroconst\">");
   8.217 +                        } else {
   8.218 +                            closespan = 0;
   8.219 +                        }
   8.220 +                        put_htmlescapedstr(dest, word);
   8.221 +                        if (closespan) {
   8.222 +                            ucx_buffer_puts(dest, "</span>");
   8.223 +                        }
   8.224 +                    }
   8.225 +                    wbuf->pos = wbuf->size = 0; /* reset word buffer */
   8.226 +                    
   8.227 +                    /* write current character */
   8.228 +                    put_htmlescaped(dest, c);
   8.229 +                } else {
   8.230 +                    /* buffer the current word */
   8.231 +                    ucx_buffer_putc(wbuf, c);
   8.232 +                }
   8.233 +            }
   8.234 +
   8.235 +            isescaping = !isescaping & (c == '\\');
   8.236 +        }
   8.237 +    } while (c != '\n');
   8.238 +}
   8.239 +
   8.240 +/* Java Highlighter */
   8.241 +
   8.242 +static const char* jkeywords[] = {
   8.243 +    "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
   8.244 +    "package", "synchronized", "boolean", "do", "if", "private", "this",
   8.245 +    "break", "double", "implements", "protected", "throw", "byte", "else",
   8.246 +    "import", "public", "throws", "case", "enum", "instanceof", "return",
   8.247 +    "transient", "catch", "extends", "int", "short", "try", "char", "final",
   8.248 +    "interface", "static", "void", "class", "finally", "long", "strictfp",
   8.249 +    "volatile", "const", "float", "native", "super", "while", NULL
   8.250 +};
   8.251 +
   8.252 +void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
   8.253 +    /* reset buffers without clearing them */
   8.254 +    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
   8.255 +    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
   8.256 +
   8.257 +    /* alias the buffers for better handling */
   8.258 +    UcxBuffer *wbuf = hd->primary_buffer;
   8.259 +    
   8.260 +    /* local information */
   8.261 +    size_t sp = (size_t)-1;
   8.262 +    int isstring = 0, iscomment = 0, isimport = 0;
   8.263 +    char quote = '\0';
   8.264 +    int isescaping = 0;
   8.265 +
   8.266 +    if (hd->multiline_comment) {
   8.267 +        iscomment = 1;
   8.268 +        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   8.269 +    }
   8.270 +
   8.271 +    char c;
   8.272 +    do {
   8.273 +        c = src[++sp];
   8.274 +        if (!c) break;
   8.275 +        
   8.276 +        /* comments */
   8.277 +        if (!isstring && c == '/') {
   8.278 +            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
   8.279 +                iscomment = 0;
   8.280 +                hd->multiline_comment = 0;
   8.281 +                ucx_buffer_puts(dest, "/</span>");
   8.282 +                continue;
   8.283 +            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   8.284 +                iscomment = 1;
   8.285 +                hd->multiline_comment = (src[sp+1] == '*');
   8.286 +                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   8.287 +            }
   8.288 +        }
   8.289 +
   8.290 +        if (iscomment) {
   8.291 +            if (c == '\n') {
   8.292 +                ucx_buffer_puts(dest, "</span>\n");
   8.293 +            } else {
   8.294 +                put_htmlescaped(dest, c);
   8.295 +            }
   8.296 +        } else if (isimport) {
   8.297 +            /* TODO: local imports */
   8.298 +        } else {
   8.299 +            /* strings */
   8.300 +            if (!isescaping && (c == '\'' || c == '\"')) {
   8.301 +                if (isstring) {
   8.302 +                    put_htmlescaped(dest, c);
   8.303 +                    if (c == quote) {
   8.304 +                        isstring = 0;
   8.305 +                        ucx_buffer_puts(dest, "</span>");
   8.306 +                    } else {
   8.307 +                        put_htmlescaped(dest, c);
   8.308 +                    }
   8.309 +                } else {
   8.310 +                    isstring = 1;
   8.311 +                    quote = c;
   8.312 +                    ucx_buffer_puts(dest,
   8.313 +                        "<span class=\"c2html-string\">");
   8.314 +                    put_htmlescaped(dest, c);
   8.315 +                }
   8.316 +            } else {
   8.317 +                if (isstring) {
   8.318 +                    put_htmlescaped(dest, c);
   8.319 +                } else if (!isalnum(c) && c!='_' && c!='@') {
   8.320 +                    /* write buffered word, if any */
   8.321 +                    if (wbuf->size > 0) {
   8.322 +                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   8.323 +                        int closespan = 1;
   8.324 +                        if (check_keyword(word, jkeywords)) {
   8.325 +                            ucx_buffer_puts(dest,
   8.326 +                                "<span class=\"c2html-keyword\">");
   8.327 +                        } else if (isupper(word.ptr[0])) {
   8.328 +                            ucx_buffer_puts(dest,
   8.329 +                                "<span class=\"c2html-type\">");
   8.330 +                        } else if (word.ptr[0] == '@') {
   8.331 +                            ucx_buffer_puts(dest,
   8.332 +                                "<span class=\"c2html-directive\">");
   8.333 +                        } else if (check_capsonly(word)) {
   8.334 +                            ucx_buffer_puts(dest,
   8.335 +                                "<span class=\"c2html-macroconst\">");
   8.336 +                        } else {
   8.337 +                            closespan = 0;
   8.338 +                        }
   8.339 +                        put_htmlescapedstr(dest, word);
   8.340 +                        
   8.341 +                        if (closespan) {
   8.342 +                            ucx_buffer_puts(dest, "</span>");
   8.343 +                        }
   8.344 +                    }
   8.345 +                    wbuf->pos = wbuf->size = 0; /* reset buffer */
   8.346 +                    
   8.347 +                    /* write current character */
   8.348 +                    put_htmlescaped(dest, c);
   8.349 +                } else {
   8.350 +                    /* buffer the current word */
   8.351 +                    ucx_buffer_putc(wbuf, c);
   8.352 +                }
   8.353 +            }
   8.354 +
   8.355 +            isescaping = !isescaping & (c == '\\');
   8.356 +        }
   8.357 +    } while (c != '\n');
   8.358 +}
   8.359 +
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/highlighter.h	Fri Aug 26 13:49:19 2016 +0200
     9.3 @@ -0,0 +1,65 @@
     9.4 +/*
     9.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     9.6 + *
     9.7 + * Copyright 2016 Mike Becker. All rights reserved.
     9.8 + *
     9.9 + * Redistribution and use in source and binary forms, with or without
    9.10 + * modification, are permitted provided that the following conditions are met:
    9.11 + *
    9.12 + *   1. Redistributions of source code must retain the above copyright
    9.13 + *      notice, this list of conditions and the following disclaimer.
    9.14 + *
    9.15 + *   2. Redistributions in binary form must reproduce the above copyright
    9.16 + *      notice, this list of conditions and the following disclaimer in the
    9.17 + *      documentation and/or other materials provided with the distribution.
    9.18 + *
    9.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    9.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    9.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    9.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    9.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    9.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    9.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    9.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    9.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    9.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    9.29 + * POSSIBILITY OF SUCH DAMAGE.
    9.30 + *
    9.31 + */
    9.32 +
    9.33 +#ifndef CODEGENS_H
    9.34 +#define	CODEGENS_H
    9.35 +
    9.36 +#include <stdlib.h>
    9.37 +#include <stdio.h>
    9.38 +#include <string.h>
    9.39 +#include <ctype.h>
    9.40 +#include "ucx/string.h"
    9.41 +#include "ucx/buffer.h"
    9.42 +#include "ucx/utils.h"
    9.43 +
    9.44 +#ifdef	__cplusplus
    9.45 +extern "C" {
    9.46 +#endif
    9.47 +    
    9.48 +typedef struct {
    9.49 +    int multiline_comment;
    9.50 +    UcxBuffer* primary_buffer;
    9.51 +    UcxBuffer* secondary_buffer;
    9.52 +} HighlighterData;
    9.53 +
    9.54 +HighlighterData* new_highlighter_data();
    9.55 +void free_highlighter_data(HighlighterData*);
    9.56 +
    9.57 +typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
    9.58 +
    9.59 +void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
    9.60 +void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
    9.61 +void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
    9.62 +
    9.63 +#ifdef	__cplusplus
    9.64 +}
    9.65 +#endif
    9.66 +
    9.67 +#endif	/* CODEGENS_H */
    9.68 +
    10.1 --- a/src/javacodegen.c	Thu Aug 25 12:16:57 2016 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,148 +0,0 @@
    10.4 -/*
    10.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    10.6 - *
    10.7 - * Copyright 2016 Mike Becker. All rights reserved.
    10.8 - *
    10.9 - * Redistribution and use in source and binary forms, with or without
   10.10 - * modification, are permitted provided that the following conditions are met:
   10.11 - *
   10.12 - *   1. Redistributions of source code must retain the above copyright
   10.13 - *      notice, this list of conditions and the following disclaimer.
   10.14 - *
   10.15 - *   2. Redistributions in binary form must reproduce the above copyright
   10.16 - *      notice, this list of conditions and the following disclaimer in the
   10.17 - *      documentation and/or other materials provided with the distribution.
   10.18 - *
   10.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   10.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   10.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   10.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   10.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   10.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   10.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   10.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   10.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   10.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   10.29 - * POSSIBILITY OF SUCH DAMAGE.
   10.30 - *
   10.31 - */
   10.32 -
   10.33 -#include "javacodegen.h"
   10.34 -
   10.35 -const char* jkeywords[] = {
   10.36 -    "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
   10.37 -    "package", "synchronized", "boolean", "do", "if", "private", "this",
   10.38 -    "break", "double", "implements", "protected", "throw", "byte", "else",
   10.39 -    "import", "public", "throws", "case", "enum", "instanceof", "return",
   10.40 -    "transient", "catch", "extends", "int", "short", "try", "char", "final",
   10.41 -    "interface", "static", "void", "class", "finally", "long", "strictfp",
   10.42 -    "volatile", "const", "float", "native", "super", "while", NULL
   10.43 -};
   10.44 -
   10.45 -void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
   10.46 -    /* reset buffers without clearing them */
   10.47 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
   10.48 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
   10.49 -
   10.50 -    /* alias the buffers for better handling */
   10.51 -    UcxBuffer *wbuf = hd->primary_buffer;
   10.52 -    
   10.53 -    /* local information */
   10.54 -    size_t sp = (size_t)-1;
   10.55 -    int isstring = 0, iscomment = 0, isimport = 0;
   10.56 -    char quote = '\0';
   10.57 -    int isescaping = 0;
   10.58 -
   10.59 -    if (hd->multiline_comment) {
   10.60 -        iscomment = 1;
   10.61 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   10.62 -    }
   10.63 -
   10.64 -    char c;
   10.65 -    do {
   10.66 -        c = src[++sp];
   10.67 -        if (!c) break;
   10.68 -        
   10.69 -        /* comments */
   10.70 -        if (!isstring && c == '/') {
   10.71 -            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
   10.72 -                iscomment = 0;
   10.73 -                hd->multiline_comment = 0;
   10.74 -                ucx_buffer_puts(dest, "/</span>");
   10.75 -                continue;
   10.76 -            } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   10.77 -                iscomment = 1;
   10.78 -                hd->multiline_comment = (src[sp+1] == '*');
   10.79 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   10.80 -            }
   10.81 -        }
   10.82 -
   10.83 -        if (iscomment) {
   10.84 -            if (c == '\n') {
   10.85 -                ucx_buffer_puts(dest, "</span>\n");
   10.86 -            } else {
   10.87 -                put_htmlescaped(dest, c);
   10.88 -            }
   10.89 -        } else if (isimport) {
   10.90 -            /* TODO: local imports */
   10.91 -        } else {
   10.92 -            /* strings */
   10.93 -            if (!isescaping && (c == '\'' || c == '\"')) {
   10.94 -                if (isstring) {
   10.95 -                    put_htmlescaped(dest, c);
   10.96 -                    if (c == quote) {
   10.97 -                        isstring = 0;
   10.98 -                        ucx_buffer_puts(dest, "</span>");
   10.99 -                    } else {
  10.100 -                        put_htmlescaped(dest, c);
  10.101 -                    }
  10.102 -                } else {
  10.103 -                    isstring = 1;
  10.104 -                    quote = c;
  10.105 -                    ucx_buffer_puts(dest,
  10.106 -                        "<span class=\"c2html-string\">");
  10.107 -                    put_htmlescaped(dest, c);
  10.108 -                }
  10.109 -            } else {
  10.110 -                if (isstring) {
  10.111 -                    put_htmlescaped(dest, c);
  10.112 -                } else if (!isalnum(c) && c!='_' && c!='@') {
  10.113 -                    /* write buffered word, if any */
  10.114 -                    if (wbuf->size > 0) {
  10.115 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
  10.116 -                        int closespan = 1;
  10.117 -                        if (check_keyword(word, jkeywords)) {
  10.118 -                            ucx_buffer_puts(dest,
  10.119 -                                "<span class=\"c2html-keyword\">");
  10.120 -                        } else if (isupper(word.ptr[0])) {
  10.121 -                            ucx_buffer_puts(dest,
  10.122 -                                "<span class=\"c2html-type\">");
  10.123 -                        } else if (word.ptr[0] == '@') {
  10.124 -                            ucx_buffer_puts(dest,
  10.125 -                                "<span class=\"c2html-directive\">");
  10.126 -                        } else if (check_capsonly(word)) {
  10.127 -                            ucx_buffer_puts(dest,
  10.128 -                                "<span class=\"c2html-macroconst\">");
  10.129 -                        } else {
  10.130 -                            closespan = 0;
  10.131 -                        }
  10.132 -                        put_htmlescapedstr(dest, word);
  10.133 -                        
  10.134 -                        if (closespan) {
  10.135 -                            ucx_buffer_puts(dest, "</span>");
  10.136 -                        }
  10.137 -                    }
  10.138 -                    wbuf->pos = wbuf->size = 0; /* reset buffer */
  10.139 -                    
  10.140 -                    /* write current character */
  10.141 -                    put_htmlescaped(dest, c);
  10.142 -                } else {
  10.143 -                    /* buffer the current word */
  10.144 -                    ucx_buffer_putc(wbuf, c);
  10.145 -                }
  10.146 -            }
  10.147 -
  10.148 -            isescaping = !isescaping & (c == '\\');
  10.149 -        }
  10.150 -    } while (c != '\n');
  10.151 -}
    11.1 --- a/src/javacodegen.h	Thu Aug 25 12:16:57 2016 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,46 +0,0 @@
    11.4 -/*
    11.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    11.6 - *
    11.7 - * Copyright 2016 Mike Becker. All rights reserved.
    11.8 - *
    11.9 - * Redistribution and use in source and binary forms, with or without
   11.10 - * modification, are permitted provided that the following conditions are met:
   11.11 - *
   11.12 - *   1. Redistributions of source code must retain the above copyright
   11.13 - *      notice, this list of conditions and the following disclaimer.
   11.14 - *
   11.15 - *   2. Redistributions in binary form must reproduce the above copyright
   11.16 - *      notice, this list of conditions and the following disclaimer in the
   11.17 - *      documentation and/or other materials provided with the distribution.
   11.18 - *
   11.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   11.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   11.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   11.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   11.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   11.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   11.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   11.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   11.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   11.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   11.29 - * POSSIBILITY OF SUCH DAMAGE.
   11.30 - *
   11.31 - */
   11.32 -
   11.33 -#ifndef JAVACODEGEN_H
   11.34 -#define	JAVACODEGEN_H
   11.35 -
   11.36 -#include "codegens.h"
   11.37 -
   11.38 -#ifdef	__cplusplus
   11.39 -extern "C" {
   11.40 -#endif
   11.41 -
   11.42 -void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
   11.43 -
   11.44 -#ifdef	__cplusplus
   11.45 -}
   11.46 -#endif
   11.47 -
   11.48 -#endif	/* JAVACODEGEN_H */
   11.49 -

mercurial