refactors highlighter_t and removes abstraction overhead

Tue, 23 Aug 2016 15:55:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 15:55:02 +0200
changeset 46
534a4ef4143d
parent 45
1f3835182aeb
child 47
c39ecbbca7c0

refactors highlighter_t and removes abstraction overhead

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.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 15:28:56 2016 +0200
+++ b/src/c2html.c	Tue Aug 23 15:55:02 2016 +0200
@@ -48,7 +48,7 @@
 }
 
 /* TODO: remove this workaround after refactoring highlighter structure */
-static void plainparseline(char *src, UcxBuffer *dest, highlighter_t* hltr) {
+static void plainparseline(char *src, UcxBuffer *dest, HighlighterData* hltr) {
     size_t dp = 0;
     char *buf = dest->space + dest->pos;
     while (*src && *src != '\n') {
@@ -61,10 +61,8 @@
     dest->size += dp;
 }
 
-int formatfile(
-        highlighter_t *highlighter,
-        UcxList *in, write_func out, void *stream,
-        int showlineno) {
+int formatlines(highlighter_func highlighter,
+        UcxList *in, write_func out, void *stream, int showlineno) {
     
     /* compute width of line numbering */
     int lnw;
@@ -86,6 +84,9 @@
 
     /* process lines */
     size_t lineno = 0;
+    HighlighterData highlighter_data;
+    memset(&highlighter_data, 0, sizeof(HighlighterData));
+    
     UCX_FOREACH(sourceline, in) {
         /* increase line number and clean line buffer */
         lineno++;
@@ -99,7 +100,7 @@
         }
         
         /* process code line */
-        highlighter->parser(sourceline->data, line, highlighter);
+        highlighter(sourceline->data, line, &highlighter_data);
         
         /* write code line */
         out(line->space, 1, line->size, stream);
@@ -174,22 +175,16 @@
         return EXIT_FAILURE;
     } else {
         /* Configure highlighter */
-        highlighter_t *highlighter = calloc(1, sizeof(highlighter_t));
+        highlighter_func hltr = NULL;
         switch (sourcetype) {
             case SOURCE_C:
-                highlighter->isdirective = check_cdirective;
-                highlighter->istype = check_ctype;
-                highlighter->keywords = ckeywords;
-                highlighter->parser = cparseline;
+                hltr = cparseline;
                 break;
             case SOURCE_JAVA:
-                highlighter->isdirective = check_jdirective;
-                highlighter->istype = check_jtype;
-                highlighter->keywords = jkeywords;
-                highlighter->parser = jparseline;
+                hltr = jparseline;
                 break;
             case SOURCE_PLAIN:
-                highlighter->parser = plainparseline;
+                hltr = plainparseline;
                 break;
             default: /* should be unreachable */
                 fprintf(stderr, "error in enum source_type\n");
@@ -254,14 +249,9 @@
                 }
             }
             
-            formatfile(
-                    highlighter,
-                    inputlines,
-                    (write_func) fwrite,
-                    fout,
-                    settings.showlinenumbers);
+            formatlines(hltr, inputlines,
+                    (write_func) fwrite, fout, settings.showlinenumbers);
             
-            free(highlighter);
             ucx_buffer_free(content);
         } else {
             perror("Error opening input file");
--- a/src/ccodegen.c	Tue Aug 23 15:28:56 2016 +0200
+++ b/src/ccodegen.c	Tue Aug 23 15:55:02 2016 +0200
@@ -39,18 +39,10 @@
     "while", NULL
 };
 
-int check_ctype(char *word, size_t len) {
-    return (word[len-2] == '_' && word[len-1] == 't');
-}
-
-int check_cdirective(char *word) {
-    return (word[0] == '#');
-}
-
 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
                                     dp += sizeof(str)-1
 
-void cparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) {
+void cparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) {
     /* TODO: workaround for using old code with UcxBuffer */
     char *dest = destbuf->space + destbuf->pos;
 
@@ -146,13 +138,14 @@
                     /* interpret word int_t */
                     if (wp > 0 && wp < WORDBUF_SIZE) {
                         int closespan = 1;
-                        if (check_keyword(hltr->word, hltr->keywords)) {
+                        if (check_keyword(hltr->word, ckeywords)) {
                             memcpy_const(dest, dp, 
                                 "<span class=\"c2html-keyword\">");
-                        } else if (hltr->istype(hltr->word, wp)) {
+                        } else if (hltr->word[wp-2] == '_'
+                                && hltr->word[wp-1] == 't') {
                             memcpy_const(dest, dp, 
                                 "<span class=\"c2html-type\">");
-                        } else if (hltr->isdirective(hltr->word)) {
+                        } else if (hltr->word[0] == '#') {
                             isinclude = !strncmp(
                                 "#include", hltr->word, WORDBUF_SIZE);
                             memcpy_const(dest, dp, 
--- a/src/ccodegen.h	Tue Aug 23 15:28:56 2016 +0200
+++ b/src/ccodegen.h	Tue Aug 23 15:55:02 2016 +0200
@@ -41,7 +41,7 @@
 
 int check_ctype(char *word, size_t len);
 int check_cdirective(char *word);
-void cparseline(char *src, UcxBuffer *dest, highlighter_t *hltr);
+void cparseline(char *src, UcxBuffer *dest, HighlighterData *hltr);
 
 #ifdef	__cplusplus
 }
--- a/src/codegens.h	Tue Aug 23 15:28:56 2016 +0200
+++ b/src/codegens.h	Tue Aug 23 15:55:02 2016 +0200
@@ -39,21 +39,15 @@
 
 #define WORDBUF_SIZE 64
 
-#define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
-    
-typedef struct _highlighter_t highlighter_t;
-typedef void(*parser_fnc)(char*,UcxBuffer*,highlighter_t*);
-
-struct _highlighter_t {
-    const char** keywords;
-    int(*istype)(char*,size_t);
-    int(*isdirective)(char*);
-    parser_fnc parser;
+typedef struct {
     int iscommentml;
     char word[WORDBUF_SIZE];
     char includefile[FILENAME_MAX];
-};
+} HighlighterData;
 
+typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
+
+#define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
 size_t writeescapedchar(char *dest, size_t dp, char c);
 int check_keyword(char *word, const char** keywords);
 int check_capsonly(char *word, size_t wp);
--- a/src/javacodegen.c	Tue Aug 23 15:28:56 2016 +0200
+++ b/src/javacodegen.c	Tue Aug 23 15:55:02 2016 +0200
@@ -41,18 +41,10 @@
     "volatile", "const", "float", "native", "super", "while", NULL
 };
 
-int check_jtype(char *word, size_t len) {
-    return isupper(word[0]);
-}
-
-int check_jdirective(char *word) {
-    return word[0] == '@';
-}
-
 #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
                                     dp += sizeof(str)-1
 
-void jparseline(char *src, UcxBuffer *destbuf, highlighter_t *hltr) {
+void jparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) {
     /* TODO: workaround for using old code with UcxBuffer */
     char *dest = destbuf->space + destbuf->pos;
     
@@ -119,13 +111,13 @@
                     /* interpret word int_t */
                     if (wp > 0 && wp < WORDBUF_SIZE) {
                         int closespan = 1;
-                        if (check_keyword(hltr->word, hltr->keywords)) {
+                        if (check_keyword(hltr->word, jkeywords)) {
                             memcpy_const(dest, dp, 
                                 "<span class=\"c2html-keyword\">");
-                        } else if (hltr->istype(hltr->word, wp)) {
+                        } else if (isupper(hltr->word[0])) {
                             memcpy_const(dest, dp, 
                                 "<span class=\"c2html-type\">");
-                        } else if (hltr->isdirective(hltr->word)) {
+                        } else if (hltr->word[0] == '@') {
                             memcpy_const(dest, dp, 
                                 "<span class=\"c2html-directive\">");
                         } else if (check_capsonly(hltr->word, wp)) {
--- a/src/javacodegen.h	Tue Aug 23 15:28:56 2016 +0200
+++ b/src/javacodegen.h	Tue Aug 23 15:55:02 2016 +0200
@@ -41,7 +41,7 @@
 
 int check_jtype(char *word, size_t len);
 int check_jdirective(char *word);
-void jparseline(char *src, UcxBuffer *dest, highlighter_t *hltr);
+void jparseline(char *src, UcxBuffer *dest, HighlighterData *hltr);
 
 
 #ifdef	__cplusplus

mercurial