highlighter now use the UcxBuffer API for writing to the destination buffer

Tue, 23 Aug 2016 17:24:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 17:24:58 +0200
changeset 48
b2724c711203
parent 47
c39ecbbca7c0
child 49
f86f0b054464

highlighter now use the UcxBuffer API for writing to the destination buffer

src/c2html.c 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/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/c2html.c	Tue Aug 23 17:24:58 2016 +0200
@@ -47,18 +47,12 @@
         "\n");
 }
 
-/* TODO: remove this workaround after refactoring highlighter structure */
-static void plainparseline(char *src, UcxBuffer *dest, int* x) {
-    size_t dp = 0;
-    char *buf = dest->space + dest->pos;
+static void plain_highlighter(char *src, UcxBuffer *dest, int* x) {
     while (*src && *src != '\n') {
-        dp = writeescapedchar(buf, dp, *src);
+        put_htmlescaped(dest, *src);
         src++;
     }
-    buf[dp++] = '\n';
-    buf[dp] = '\0';
-    dest->pos += dp;
-    dest->size += dp;
+    ucx_buffer_putc(dest, '\n');
 }
 
 int formatlines(highlighter_func highlighter,
@@ -101,7 +95,7 @@
         /* process code line */
         highlighter(sourceline->data, line, &multiline_comment);
         
-        /* write code line */
+        /* write code line and reset buffer */
         out(line->space, 1, line->size, stream);
     }
     
@@ -177,13 +171,13 @@
         highlighter_func hltr = NULL;
         switch (sourcetype) {
             case SOURCE_C:
-                hltr = cparseline;
+                hltr = c_highlighter;
                 break;
             case SOURCE_JAVA:
-                hltr = jparseline;
+                hltr = java_highlighter;
                 break;
             case SOURCE_PLAIN:
-                hltr = plainparseline;
+                hltr = plain_highlighter;
                 break;
             default: /* should be unreachable */
                 fprintf(stderr, "error in enum source_type\n");
--- a/src/ccodegen.c	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/ccodegen.c	Tue Aug 23 17:24:58 2016 +0200
@@ -28,6 +28,7 @@
  */
 
 #include "ccodegen.h"
+#include "ucx/utils.h"
 #include <string.h>
 #include <ctype.h>
 
@@ -39,13 +40,7 @@
     "while", NULL
 };
 
-#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
-                                    dp += sizeof(str)-1
-
-void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
-    /* TODO: workaround for using old code with UcxBuffer */
-    char *dest = destbuf->space + destbuf->pos;
-
+void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
     /* TODO: try to replace these buffers */
     char wordbuf[WORDBUF_SIZE];
     sstr_t word;
@@ -56,7 +51,7 @@
     includefile.ptr = includefilebuf;
     includefile.length = 0;
     
-    size_t sp = (size_t)-1, dp = 0;
+    size_t sp = (size_t)-1;
     int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
     char quote = '\0';
     int isescaping = 0;
@@ -64,7 +59,7 @@
     /* continue a multi line comment highlighting */
     if (*multiline_comment) {
         iscomment = 1;
-        memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
+        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
     }
 
     char c;
@@ -77,113 +72,103 @@
             if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
                 *multiline_comment = 0;
-                memcpy_const(dest, dp, "/</span>");
+                ucx_buffer_puts(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
                 *multiline_comment = (src[sp+1] == '*');
-                memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
+                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
             }
         }
 
         if (iscomment) {
             if (c == '\n') {
-                memcpy_const(dest, dp,  "</span>");
+                ucx_buffer_puts(dest, "</span>\n");
+            } else {
+                put_htmlescaped(dest, c);
             }
-            dp = writeescapedchar(dest, dp, c);
         } else if (isinclude) {
             if (c == '<') {
-                memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
-                dp = writeescapedchar(dest, dp, c);
+                ucx_buffer_puts(dest,
+                        "<span class=\"c2html-stdinclude\">&lt;");
             } else if (c == '\"') {
                 if (parseinclude) {
-                    dest[dp++] = '\"';
-                    dest[dp++] = '>';
-                    memcpy(&(dest[dp]), includefile.ptr, includefile.length);
-                    dp += includefile.length;
-
-                    dp = writeescapedchar(dest, dp, c);
-                    memcpy_const(dest, dp, "</a>");
+                    ucx_bprintf(dest, "\">%.*s\"</a>",
+                            includefile.length, includefile.ptr);
                     parseinclude = 0;
                 } else {
-                    memcpy_const(dest, dp,
-                        "<a class=\"c2html-userinclude\" href=");
-                    dp = writeescapedchar(dest, dp, c);
+                    ucx_buffer_puts(dest,
+                            "<a class=\"c2html-userinclude\" href=\"");
                     includefile.length = 0;
                     includefile.ptr[includefile.length++] = '\"';
                     parseinclude = 1;
                 }
             } else if (c == '>') {
-                dp = writeescapedchar(dest, dp, c);
-                memcpy_const(dest, dp, "</span>");
+                ucx_buffer_puts(dest,  "&gt;</span>");
             } else {
                 if (parseinclude) {
                     includefile.ptr[includefile.length++] = c;
                 }
-                dp = writeescapedchar(dest, dp, c);
+                put_htmlescaped(dest, c);
             }
         } else {
             /* strings */
             if (!isescaping && (c == '\'' || c == '\"')) {
                 if (isstring) {
-                    dp = writeescapedchar(dest, dp, c);
+                    put_htmlescaped(dest, c);
                     if (c == quote) {
                         isstring = 0;
-                        memcpy_const(dest, dp, "</span>");
+                        ucx_buffer_puts(dest, "</span>");
                     } else {
-                        dp = writeescapedchar(dest, dp, c);
+                        put_htmlescaped(dest, c);
                     }
                 } else {
                     isstring = 1;
                     quote = c;
-                    memcpy_const(dest, dp,
-                        "<span class=\"c2html-string\">");
-                    dp = writeescapedchar(dest, dp, c);
+                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
+                    put_htmlescaped(dest, c);
                 }
             } else {
                 if (isstring) {
-                    dp = writeescapedchar(dest, dp, c);
-                } else if (!iswordcharacter(c)) {
+                    put_htmlescaped(dest, c);
+                } else if (!check_alnumex(c)) {
                     if (word.length > 0 && word.length < WORDBUF_SIZE) {
                         int closespan = 1;
                         sstr_t typesuffix = ST("_t");
                         if (check_keyword(word, ckeywords)) {
-                            memcpy_const(dest, dp, 
-                                "<span class=\"c2html-keyword\">");
+                            ucx_buffer_puts(dest,
+                                    "<span class=\"c2html-keyword\">");
                         } else if (sstrsuffix(word, typesuffix)) {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-type\">");
                         } else if (word.ptr[0] == '#') {
                             isinclude = !sstrcmp(word, S("#include"));
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-directive\">");
                         } else if (check_capsonly(word)) {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-macroconst\">");
                         } else {
                             closespan = 0;
                         }
-                        for (int i = 0 ; i < word.length ; i++) {
-                            dp = writeescapedchar(dest, dp, word.ptr[i]);
-                        }
+                        put_htmlescapedstr(dest, word);
                         if (closespan) {
-                            memcpy_const(dest, dp, "</span>");
+                            ucx_buffer_puts(dest, "</span>");
                         }
                     }
                     word.length = 0;
-                    dp = writeescapedchar(dest, dp, c);
+                    put_htmlescaped(dest, c);
                 } else {
                     /* read word */
                     if (word.length < WORDBUF_SIZE) {
                         word.ptr[word.length++] = c;
                     } else if (word.length == WORDBUF_SIZE) {
-                        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
-                            dp = writeescapedchar(dest, dp, word.ptr[i]);
-                        }
-                        word.length++;
-                        dp = writeescapedchar(dest, dp, c);
+                        /* TODO: this will be removed */
+                        ucx_buffer_puts(dest,
+                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
+                        word.length = 0;
                     } else {
-                        dp = writeescapedchar(dest, dp, c);
+                        put_htmlescaped(dest, c);
                     }
                 }
             }
@@ -191,9 +176,4 @@
             isescaping = !isescaping & (c == '\\');
         }
     } while (c != '\n');
-    dest[dp] = 0;
-    
-    /* TODO: workaround */
-    destbuf->pos += dp;
-    destbuf->size += dp;
 }
--- a/src/ccodegen.h	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/ccodegen.h	Tue Aug 23 17:24:58 2016 +0200
@@ -37,11 +37,7 @@
 extern "C" {
 #endif
 
-extern const char* ckeywords[];
-
-int check_ctype(char *word, size_t len);
-int check_cdirective(char *word);
-void cparseline(char *src, UcxBuffer *dest, int *mlc);
+void c_highlighter(char *src, UcxBuffer *dest, int *mlc);
 
 #ifdef	__cplusplus
 }
--- a/src/codegens.c	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/codegens.c	Tue Aug 23 17:24:58 2016 +0200
@@ -32,16 +32,20 @@
 #include "codegens.h"
 
 
-size_t writeescapedchar(char *dest, size_t dp, char c) {
+void put_htmlescaped(UcxBuffer *dest, char c) {
     if (c == '>') {
-        dest[dp++] = '&'; dest[dp++] = 'g'; dest[dp++] = 't'; dest[dp++] = ';';
+        ucx_buffer_puts(dest, "&gt;");
     } else if (c == '<') {
-        dest[dp++] = '&'; dest[dp++] = 'l'; dest[dp++] = 't'; dest[dp++] = ';';
+        ucx_buffer_puts(dest, "&lt;");
     } else {
-        dest[dp++] = c;
+        ucx_buffer_putc(dest, c);
     }
+}
 
-    return dp;
+void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
+    for (int i = 0 ; i < s.length ; i++) {
+        put_htmlescaped(dest, s.ptr[i]);
+    }
 }
 
 int check_keyword(sstr_t word, const char** keywords) {
--- a/src/codegens.h	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/codegens.h	Tue Aug 23 17:24:58 2016 +0200
@@ -42,8 +42,10 @@
 
 typedef void(*highlighter_func)(char*,UcxBuffer*,int*);
 
-#define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
-size_t writeescapedchar(char *dest, size_t dp, char c);
+void put_htmlescaped(UcxBuffer *dest, char c);
+void put_htmlescapedstr(UcxBuffer *dest, sstr_t s);
+
+#define check_alnumex(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
 int check_keyword(sstr_t word, const char** keywords);
 int check_capsonly(sstr_t word);
 
--- a/src/javacodegen.c	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/javacodegen.c	Tue Aug 23 17:24:58 2016 +0200
@@ -41,26 +41,21 @@
     "volatile", "const", "float", "native", "super", "while", NULL
 };
 
-#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
-                                    dp += sizeof(str)-1
+void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
 
-void jparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
-    /* TODO: workaround for using old code with UcxBuffer */
-    char *dest = destbuf->space + destbuf->pos;
-    
     /* TODO: try to replace this buffer */
     char wordbuf[WORDBUF_SIZE];
     sstr_t word;
     word.ptr = wordbuf; word.length = 0;
     
-    size_t sp = (size_t)-1, dp = 0;
+    size_t sp = (size_t)-1;
     int isstring = 0, iscomment = 0, isimport = 0;
     char quote = '\0';
     int isescaping = 0;
 
     if (*multiline_comment) {
         iscomment = 1;
-        memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
+        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
     }
 
     char c;
@@ -73,83 +68,81 @@
             if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
                 *multiline_comment = 0;
-                memcpy_const(dest, dp, "/</span>");
+                ucx_buffer_puts(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
                 *multiline_comment = (src[sp+1] == '*');
-                memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
+                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
             }
         }
 
         if (iscomment) {
             if (c == '\n') {
-                memcpy(&(dest[dp]), "</span>", 7);
-                dp += 7;
+                ucx_buffer_puts(dest, "</span>\n");
+            } else {
+                put_htmlescaped(dest, c);
             }
-            dp = writeescapedchar(dest, dp, c);
         } else if (isimport) {
             /* TODO: local imports */
         } else {
             /* strings */
             if (!isescaping && (c == '\'' || c == '\"')) {
                 if (isstring) {
-                    dp = writeescapedchar(dest, dp, c);
+                    put_htmlescaped(dest, c);
                     if (c == quote) {
                         isstring = 0;
-                        memcpy_const(dest, dp, "</span>");
+                        ucx_buffer_puts(dest, "</span>");
                     } else {
-                        dp = writeescapedchar(dest, dp, c);
+                        put_htmlescaped(dest, c);
                     }
                 } else {
                     isstring = 1;
                     quote = c;
-                    memcpy_const(dest, dp,
+                    ucx_buffer_puts(dest,
                         "<span class=\"c2html-string\">");
-                    dp = writeescapedchar(dest, dp, c);
+                    put_htmlescaped(dest, c);
                 }
             } else {
                 if (isstring) {
-                    dp = writeescapedchar(dest, dp, c);
-                } else if (!iswordcharacter(c)) {
+                    put_htmlescaped(dest, c);
+                } else if (!check_alnumex(c)) {
                     if (word.length > 0 && word.length < WORDBUF_SIZE) {
                         int closespan = 1;
                         if (check_keyword(word, jkeywords)) {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-keyword\">");
                         } else if (isupper(word.ptr[0])) {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-type\">");
                         } else if (word.ptr[0] == '@') {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-directive\">");
                         } else if (check_capsonly(word)) {
-                            memcpy_const(dest, dp, 
+                            ucx_buffer_puts(dest,
                                 "<span class=\"c2html-macroconst\">");
                         } else {
                             closespan = 0;
                         }
-                        for (int i = 0 ; i < word.length ; i++) {
-                            dp = writeescapedchar(dest, dp, word.ptr[i]);
-                        }
+                        put_htmlescapedstr(dest, word);
+                        
                         if (closespan) {
-                            memcpy_const(dest, dp, "</span>");
+                            ucx_buffer_puts(dest, "</span>");
                         }
                     }
                     word.length = 0;
-                    dp = writeescapedchar(dest, dp, c);
+                    put_htmlescaped(dest, c);
                 } else {
                     /* read word */
                     if (word.length < WORDBUF_SIZE) {
                         word.ptr[word.length++] = c;
                     } else if (word.length == WORDBUF_SIZE) {
-                        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
-                            dp = writeescapedchar(dest, dp, word.ptr[i]);
-                        }
-                        word.length++;
-                        dp = writeescapedchar(dest, dp, c);
+                        /* TODO: this will be removed */
+                        ucx_buffer_puts(dest,
+                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
+                        word.length = 0;
                     } else {
-                        dp = writeescapedchar(dest, dp, c);
+                        put_htmlescaped(dest, c);
                     }
                 }
             }
@@ -157,9 +150,4 @@
             isescaping = !isescaping & (c == '\\');
         }
     } while (c != '\n');
-    dest[dp] = 0;
-    
-    /* TODO: workaround */
-    destbuf->pos += dp;
-    destbuf->size += dp;
 }
--- a/src/javacodegen.h	Tue Aug 23 16:34:02 2016 +0200
+++ b/src/javacodegen.h	Tue Aug 23 17:24:58 2016 +0200
@@ -37,12 +37,7 @@
 extern "C" {
 #endif
 
-extern const char* jkeywords[];
-
-int check_jtype(char *word, size_t len);
-int check_jdirective(char *word);
-void jparseline(char *src, UcxBuffer *dest, int *mlc);
-
+void java_highlighter(char *src, UcxBuffer *dest, int *mlc);
 
 #ifdef	__cplusplus
 }

mercurial