strings and directives

Fri, 21 Jun 2013 11:42:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Jun 2013 11:42:18 +0200
changeset 7
02cf8ac736e6
parent 6
d10f7570add4
child 8
417cd3b29f97

strings and directives

src/c2html.c file | annotate | diff | comparison | revisions
test/header.html file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Wed Jun 12 14:50:27 2013 +0200
+++ b/src/c2html.c	Fri Jun 21 11:42:18 2013 +0200
@@ -37,6 +37,9 @@
 #define INPUTBUF_SIZE 2048
 #define WORDBUF_SIZE 16
 
+#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
+#define isdirective(word) (word[0] == '#')
+
 const char* keywords[] = {
   "auto", "break", "case", "char", "const", "continue", "default", "do", 
   "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", 
@@ -146,8 +149,6 @@
   return 0;
 }
 
-#define istype(word, len) (word[len-2] == '_' && word[len-1] == 't')
-
 void parseline(char *src, char *dest) {
   size_t sp = 0, dp = 0;
   /* indent */
@@ -157,47 +158,68 @@
   char word[WORDBUF_SIZE];
   memset(word, 0, WORDBUF_SIZE);
   size_t wp = 0;
-  int closespan;
+  int isstring = 0, iscomment = 0;
+  int isescaping = 0;
   for (char c = src[sp] ; c ; c=src[++sp]) {
-    if (!isalnum(c) && c != '_') {
-      /* interpret word int_t */
-      if (wp > 0 && wp < WORDBUF_SIZE) {
-        if (iskeyword(word)) {
-          memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
-          dp += 29;
-          closespan = 1;
-        } else if (istype(word, wp)) {
-          memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
-          dp += 26;
-          closespan = 1;
-        } else {
-          closespan = 0;
-        }
-        for (int i = 0 ; i < wp ; i++) {
-          dp = writeescapedchar(dest, dp, word[i]);
-        }
-        if (closespan) {
-          memcpy(&(dest[dp]), "</span>", 7);
-          dp += 7;
-        }
-        memset(word, 0, WORDBUF_SIZE);
-        wp = 0;
-      }
-      dp = writeescapedchar(dest, dp, c);
-    } else {
-      /* read word */
-      if (wp < WORDBUF_SIZE) {
-        word[wp++] = c;
-      } else if (wp == WORDBUF_SIZE) {
-        for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
-          dp = writeescapedchar(dest, dp, word[i]);
-        }
-        wp++;
+    /* comments and strings */
+    if (!isescaping && (c == '\'' || c == '\"')) {
+      isstring ^= 1;
+      if (isstring) {
+        memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
+        dp += 28;
         dp = writeescapedchar(dest, dp, c);
       } else {
         dp = writeescapedchar(dest, dp, c);
+        memcpy(&(dest[dp]), "</span>", 7);
+        dp += 7;
+      }
+    } else {
+      if (iscomment || isstring) {
+        dp = writeescapedchar(dest, dp, c);
+      } else if (!isalnum(c) && c != '_' && c != '#') {
+        /* interpret word int_t */
+        if (wp > 0 && wp < WORDBUF_SIZE) {
+          int closespan = 1;
+          if (iskeyword(word)) {
+            memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29);
+            dp += 29;
+          } else if (istype(word, wp)) {
+            memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26);
+            dp += 26;
+          } else if (isdirective(word)) {
+            memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31);
+            dp += 31;
+          } else {
+            closespan = 0;
+          }
+          for (int i = 0 ; i < wp ; i++) {
+            dp = writeescapedchar(dest, dp, word[i]);
+          }
+          if (closespan) {
+            memcpy(&(dest[dp]), "</span>", 7);
+            dp += 7;
+          }
+          memset(word, 0, WORDBUF_SIZE);
+          wp = 0;
+        }
+        dp = writeescapedchar(dest, dp, c);
+      } else {
+        /* read word */
+        if (wp < WORDBUF_SIZE) {
+          word[wp++] = c;
+        } else if (wp == WORDBUF_SIZE) {
+          for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
+            dp = writeescapedchar(dest, dp, word[i]);
+          }
+          wp++;
+          dp = writeescapedchar(dest, dp, c);
+        } else {
+          dp = writeescapedchar(dest, dp, c);
+        }
       }
     }
+    
+    isescaping = !isescaping & (c == '\\');
   }
   dest[dp] = 0;
 }
--- a/test/header.html	Wed Jun 12 14:50:27 2013 +0200
+++ b/test/header.html	Fri Jun 21 11:42:18 2013 +0200
@@ -13,6 +13,12 @@
       span.c2html-type {
         color: cornflowerblue;
       }
+      span.c2html-directive {
+        color: green;
+      }
+      span.c2html-string {
+        color: orange;
+      }
     </style>
   </head>
   <body>

mercurial