words (token) are now stored as sstr_t

Tue, 23 Aug 2016 16:34:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 16:34:02 +0200
changeset 47
c39ecbbca7c0
parent 46
534a4ef4143d
child 48
b2724c711203

words (token) are now stored as sstr_t

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
     1.1 --- a/src/c2html.c	Tue Aug 23 15:55:02 2016 +0200
     1.2 +++ b/src/c2html.c	Tue Aug 23 16:34:02 2016 +0200
     1.3 @@ -48,7 +48,7 @@
     1.4  }
     1.5  
     1.6  /* TODO: remove this workaround after refactoring highlighter structure */
     1.7 -static void plainparseline(char *src, UcxBuffer *dest, HighlighterData* hltr) {
     1.8 +static void plainparseline(char *src, UcxBuffer *dest, int* x) {
     1.9      size_t dp = 0;
    1.10      char *buf = dest->space + dest->pos;
    1.11      while (*src && *src != '\n') {
    1.12 @@ -84,8 +84,7 @@
    1.13  
    1.14      /* process lines */
    1.15      size_t lineno = 0;
    1.16 -    HighlighterData highlighter_data;
    1.17 -    memset(&highlighter_data, 0, sizeof(HighlighterData));
    1.18 +    int multiline_comment = 0;
    1.19      
    1.20      UCX_FOREACH(sourceline, in) {
    1.21          /* increase line number and clean line buffer */
    1.22 @@ -100,7 +99,7 @@
    1.23          }
    1.24          
    1.25          /* process code line */
    1.26 -        highlighter(sourceline->data, line, &highlighter_data);
    1.27 +        highlighter(sourceline->data, line, &multiline_comment);
    1.28          
    1.29          /* write code line */
    1.30          out(line->space, 1, line->size, stream);
    1.31 @@ -174,7 +173,7 @@
    1.32          printhelp();
    1.33          return EXIT_FAILURE;
    1.34      } else {
    1.35 -        /* Configure highlighter */
    1.36 +        /* Choose highlighter */
    1.37          highlighter_func hltr = NULL;
    1.38          switch (sourcetype) {
    1.39              case SOURCE_C:
     2.1 --- a/src/ccodegen.c	Tue Aug 23 15:55:02 2016 +0200
     2.2 +++ b/src/ccodegen.c	Tue Aug 23 16:34:02 2016 +0200
     2.3 @@ -42,18 +42,27 @@
     2.4  #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
     2.5                                      dp += sizeof(str)-1
     2.6  
     2.7 -void cparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) {
     2.8 +void cparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
     2.9      /* TODO: workaround for using old code with UcxBuffer */
    2.10      char *dest = destbuf->space + destbuf->pos;
    2.11  
    2.12 -    memset(hltr->word, 0, WORDBUF_SIZE);
    2.13 -    size_t wp = 0, ifp = 0, sp = (size_t)-1, dp = 0;
    2.14 +    /* TODO: try to replace these buffers */
    2.15 +    char wordbuf[WORDBUF_SIZE];
    2.16 +    sstr_t word;
    2.17 +    word.ptr = wordbuf; word.length = 0;
    2.18 +    
    2.19 +    char includefilebuf[512];
    2.20 +    sstr_t includefile;
    2.21 +    includefile.ptr = includefilebuf;
    2.22 +    includefile.length = 0;
    2.23 +    
    2.24 +    size_t sp = (size_t)-1, dp = 0;
    2.25      int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
    2.26      char quote = '\0';
    2.27      int isescaping = 0;
    2.28      
    2.29      /* continue a multi line comment highlighting */
    2.30 -    if (hltr->iscommentml) {
    2.31 +    if (*multiline_comment) {
    2.32          iscomment = 1;
    2.33          memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    2.34      }
    2.35 @@ -65,14 +74,14 @@
    2.36          
    2.37          /* comments */
    2.38          if (!isstring && c == '/') {
    2.39 -            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
    2.40 +            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    2.41                  iscomment = 0;
    2.42 -                hltr->iscommentml = 0;
    2.43 +                *multiline_comment = 0;
    2.44                  memcpy_const(dest, dp, "/</span>");
    2.45                  continue;
    2.46              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    2.47                  iscomment = 1;
    2.48 -                hltr->iscommentml = (src[sp+1] == '*');
    2.49 +                *multiline_comment = (src[sp+1] == '*');
    2.50                  memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    2.51              }
    2.52          }
    2.53 @@ -90,8 +99,8 @@
    2.54                  if (parseinclude) {
    2.55                      dest[dp++] = '\"';
    2.56                      dest[dp++] = '>';
    2.57 -                    memcpy(&(dest[dp]), hltr->includefile, ifp);
    2.58 -                    dp += ifp;
    2.59 +                    memcpy(&(dest[dp]), includefile.ptr, includefile.length);
    2.60 +                    dp += includefile.length;
    2.61  
    2.62                      dp = writeescapedchar(dest, dp, c);
    2.63                      memcpy_const(dest, dp, "</a>");
    2.64 @@ -100,8 +109,8 @@
    2.65                      memcpy_const(dest, dp,
    2.66                          "<a class=\"c2html-userinclude\" href=");
    2.67                      dp = writeescapedchar(dest, dp, c);
    2.68 -                    ifp = 0;
    2.69 -                    hltr->includefile[ifp++] = '\"';
    2.70 +                    includefile.length = 0;
    2.71 +                    includefile.ptr[includefile.length++] = '\"';
    2.72                      parseinclude = 1;
    2.73                  }
    2.74              } else if (c == '>') {
    2.75 @@ -109,7 +118,7 @@
    2.76                  memcpy_const(dest, dp, "</span>");
    2.77              } else {
    2.78                  if (parseinclude) {
    2.79 -                    hltr->includefile[ifp++] = c;
    2.80 +                    includefile.ptr[includefile.length++] = c;
    2.81                  }
    2.82                  dp = writeescapedchar(dest, dp, c);
    2.83              }
    2.84 @@ -135,46 +144,43 @@
    2.85                  if (isstring) {
    2.86                      dp = writeescapedchar(dest, dp, c);
    2.87                  } else if (!iswordcharacter(c)) {
    2.88 -                    /* interpret word int_t */
    2.89 -                    if (wp > 0 && wp < WORDBUF_SIZE) {
    2.90 +                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
    2.91                          int closespan = 1;
    2.92 -                        if (check_keyword(hltr->word, ckeywords)) {
    2.93 +                        sstr_t typesuffix = ST("_t");
    2.94 +                        if (check_keyword(word, ckeywords)) {
    2.95                              memcpy_const(dest, dp, 
    2.96                                  "<span class=\"c2html-keyword\">");
    2.97 -                        } else if (hltr->word[wp-2] == '_'
    2.98 -                                && hltr->word[wp-1] == 't') {
    2.99 +                        } else if (sstrsuffix(word, typesuffix)) {
   2.100                              memcpy_const(dest, dp, 
   2.101                                  "<span class=\"c2html-type\">");
   2.102 -                        } else if (hltr->word[0] == '#') {
   2.103 -                            isinclude = !strncmp(
   2.104 -                                "#include", hltr->word, WORDBUF_SIZE);
   2.105 +                        } else if (word.ptr[0] == '#') {
   2.106 +                            isinclude = !sstrcmp(word, S("#include"));
   2.107                              memcpy_const(dest, dp, 
   2.108                                  "<span class=\"c2html-directive\">");
   2.109 -                        } else if (check_capsonly(hltr->word, wp)) {
   2.110 +                        } else if (check_capsonly(word)) {
   2.111                              memcpy_const(dest, dp, 
   2.112                                  "<span class=\"c2html-macroconst\">");
   2.113                          } else {
   2.114                              closespan = 0;
   2.115                          }
   2.116 -                        for (int i = 0 ; i < wp ; i++) {
   2.117 -                            dp = writeescapedchar(dest, dp, hltr->word[i]);
   2.118 +                        for (int i = 0 ; i < word.length ; i++) {
   2.119 +                            dp = writeescapedchar(dest, dp, word.ptr[i]);
   2.120                          }
   2.121                          if (closespan) {
   2.122                              memcpy_const(dest, dp, "</span>");
   2.123                          }
   2.124                      }
   2.125 -                    memset(hltr->word, 0, WORDBUF_SIZE);
   2.126 -                    wp = 0;
   2.127 +                    word.length = 0;
   2.128                      dp = writeescapedchar(dest, dp, c);
   2.129                  } else {
   2.130                      /* read word */
   2.131 -                    if (wp < WORDBUF_SIZE) {
   2.132 -                        hltr->word[wp++] = c;
   2.133 -                    } else if (wp == WORDBUF_SIZE) {
   2.134 +                    if (word.length < WORDBUF_SIZE) {
   2.135 +                        word.ptr[word.length++] = c;
   2.136 +                    } else if (word.length == WORDBUF_SIZE) {
   2.137                          for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   2.138 -                            dp = writeescapedchar(dest, dp, hltr->word[i]);
   2.139 +                            dp = writeescapedchar(dest, dp, word.ptr[i]);
   2.140                          }
   2.141 -                        wp++;
   2.142 +                        word.length++;
   2.143                          dp = writeescapedchar(dest, dp, c);
   2.144                      } else {
   2.145                          dp = writeescapedchar(dest, dp, c);
     3.1 --- a/src/ccodegen.h	Tue Aug 23 15:55:02 2016 +0200
     3.2 +++ b/src/ccodegen.h	Tue Aug 23 16:34:02 2016 +0200
     3.3 @@ -41,7 +41,7 @@
     3.4  
     3.5  int check_ctype(char *word, size_t len);
     3.6  int check_cdirective(char *word);
     3.7 -void cparseline(char *src, UcxBuffer *dest, HighlighterData *hltr);
     3.8 +void cparseline(char *src, UcxBuffer *dest, int *mlc);
     3.9  
    3.10  #ifdef	__cplusplus
    3.11  }
     4.1 --- a/src/codegens.c	Tue Aug 23 15:55:02 2016 +0200
     4.2 +++ b/src/codegens.c	Tue Aug 23 16:34:02 2016 +0200
     4.3 @@ -44,18 +44,19 @@
     4.4      return dp;
     4.5  }
     4.6  
     4.7 -int check_keyword(char *word, const char** keywords) {
     4.8 +int check_keyword(sstr_t word, const char** keywords) {
     4.9      for (int i = 0 ; keywords[i] ; i++) {
    4.10 -        if (strncmp(keywords[i], word, WORDBUF_SIZE) == 0) {
    4.11 +        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
    4.12              return 1;
    4.13          }
    4.14      }
    4.15      return 0;
    4.16  }
    4.17  
    4.18 -int check_capsonly(char *word, size_t wp) {
    4.19 -    for (size_t i = 0 ; i < wp ; i++) {
    4.20 -        if (!isupper(word[i]) && !isdigit(word[i]) && word[i] != '_') {
    4.21 +int check_capsonly(sstr_t word) {
    4.22 +    for (size_t i = 0 ; i < word.length ; i++) {
    4.23 +        if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
    4.24 +                && word.ptr[i] != '_') {
    4.25              return 0;
    4.26          }
    4.27      }
     5.1 --- a/src/codegens.h	Tue Aug 23 15:55:02 2016 +0200
     5.2 +++ b/src/codegens.h	Tue Aug 23 16:34:02 2016 +0200
     5.3 @@ -31,26 +31,21 @@
     5.4  #define	CODEGENS_H
     5.5  
     5.6  #include <stdio.h>
     5.7 +#include "ucx/string.h"
     5.8  #include "ucx/buffer.h"
     5.9  
    5.10  #ifdef	__cplusplus
    5.11  extern "C" {
    5.12  #endif
    5.13  
    5.14 -#define WORDBUF_SIZE 64
    5.15 +#define WORDBUF_SIZE 256
    5.16  
    5.17 -typedef struct {
    5.18 -    int iscommentml;
    5.19 -    char word[WORDBUF_SIZE];
    5.20 -    char includefile[FILENAME_MAX];
    5.21 -} HighlighterData;
    5.22 -
    5.23 -typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
    5.24 +typedef void(*highlighter_func)(char*,UcxBuffer*,int*);
    5.25  
    5.26  #define iswordcharacter(c) (isalnum(c) || c=='_' || c=='#' || c=='@')
    5.27  size_t writeescapedchar(char *dest, size_t dp, char c);
    5.28 -int check_keyword(char *word, const char** keywords);
    5.29 -int check_capsonly(char *word, size_t wp);
    5.30 +int check_keyword(sstr_t word, const char** keywords);
    5.31 +int check_capsonly(sstr_t word);
    5.32  
    5.33  
    5.34  #ifdef	__cplusplus
     6.1 --- a/src/javacodegen.c	Tue Aug 23 15:55:02 2016 +0200
     6.2 +++ b/src/javacodegen.c	Tue Aug 23 16:34:02 2016 +0200
     6.3 @@ -44,17 +44,21 @@
     6.4  #define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
     6.5                                      dp += sizeof(str)-1
     6.6  
     6.7 -void jparseline(char *src, UcxBuffer *destbuf, HighlighterData *hltr) {
     6.8 +void jparseline(char *src, UcxBuffer *destbuf, int *multiline_comment) {
     6.9      /* TODO: workaround for using old code with UcxBuffer */
    6.10      char *dest = destbuf->space + destbuf->pos;
    6.11      
    6.12 -    memset(hltr->word, 0, WORDBUF_SIZE);
    6.13 -    size_t wp = 0, sp = (size_t)-1, dp = 0;
    6.14 +    /* TODO: try to replace this buffer */
    6.15 +    char wordbuf[WORDBUF_SIZE];
    6.16 +    sstr_t word;
    6.17 +    word.ptr = wordbuf; word.length = 0;
    6.18 +    
    6.19 +    size_t sp = (size_t)-1, dp = 0;
    6.20      int isstring = 0, iscomment = 0, isimport = 0;
    6.21      char quote = '\0';
    6.22      int isescaping = 0;
    6.23  
    6.24 -    if (hltr->iscommentml) {
    6.25 +    if (*multiline_comment) {
    6.26          iscomment = 1;
    6.27          memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    6.28      }
    6.29 @@ -66,14 +70,14 @@
    6.30          
    6.31          /* comments */
    6.32          if (!isstring && c == '/') {
    6.33 -            if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
    6.34 +            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
    6.35                  iscomment = 0;
    6.36 -                hltr->iscommentml = 0;
    6.37 +                *multiline_comment = 0;
    6.38                  memcpy_const(dest, dp, "/</span>");
    6.39                  continue;
    6.40              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
    6.41                  iscomment = 1;
    6.42 -                hltr->iscommentml = (src[sp+1] == '*');
    6.43 +                *multiline_comment = (src[sp+1] == '*');
    6.44                  memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
    6.45              }
    6.46          }
    6.47 @@ -85,7 +89,7 @@
    6.48              }
    6.49              dp = writeescapedchar(dest, dp, c);
    6.50          } else if (isimport) {
    6.51 -            // TODO: local imports
    6.52 +            /* TODO: local imports */
    6.53          } else {
    6.54              /* strings */
    6.55              if (!isescaping && (c == '\'' || c == '\"')) {
    6.56 @@ -108,43 +112,41 @@
    6.57                  if (isstring) {
    6.58                      dp = writeescapedchar(dest, dp, c);
    6.59                  } else if (!iswordcharacter(c)) {
    6.60 -                    /* interpret word int_t */
    6.61 -                    if (wp > 0 && wp < WORDBUF_SIZE) {
    6.62 +                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
    6.63                          int closespan = 1;
    6.64 -                        if (check_keyword(hltr->word, jkeywords)) {
    6.65 +                        if (check_keyword(word, jkeywords)) {
    6.66                              memcpy_const(dest, dp, 
    6.67                                  "<span class=\"c2html-keyword\">");
    6.68 -                        } else if (isupper(hltr->word[0])) {
    6.69 +                        } else if (isupper(word.ptr[0])) {
    6.70                              memcpy_const(dest, dp, 
    6.71                                  "<span class=\"c2html-type\">");
    6.72 -                        } else if (hltr->word[0] == '@') {
    6.73 +                        } else if (word.ptr[0] == '@') {
    6.74                              memcpy_const(dest, dp, 
    6.75                                  "<span class=\"c2html-directive\">");
    6.76 -                        } else if (check_capsonly(hltr->word, wp)) {
    6.77 +                        } else if (check_capsonly(word)) {
    6.78                              memcpy_const(dest, dp, 
    6.79                                  "<span class=\"c2html-macroconst\">");
    6.80                          } else {
    6.81                              closespan = 0;
    6.82                          }
    6.83 -                        for (int i = 0 ; i < wp ; i++) {
    6.84 -                            dp = writeescapedchar(dest, dp, hltr->word[i]);
    6.85 +                        for (int i = 0 ; i < word.length ; i++) {
    6.86 +                            dp = writeescapedchar(dest, dp, word.ptr[i]);
    6.87                          }
    6.88                          if (closespan) {
    6.89                              memcpy_const(dest, dp, "</span>");
    6.90                          }
    6.91                      }
    6.92 -                    memset(hltr->word, 0, WORDBUF_SIZE);
    6.93 -                    wp = 0;
    6.94 +                    word.length = 0;
    6.95                      dp = writeescapedchar(dest, dp, c);
    6.96                  } else {
    6.97                      /* read word */
    6.98 -                    if (wp < WORDBUF_SIZE) {
    6.99 -                        hltr->word[wp++] = c;
   6.100 -                    } else if (wp == WORDBUF_SIZE) {
   6.101 +                    if (word.length < WORDBUF_SIZE) {
   6.102 +                        word.ptr[word.length++] = c;
   6.103 +                    } else if (word.length == WORDBUF_SIZE) {
   6.104                          for (int i = 0 ; i < WORDBUF_SIZE ; i++) {
   6.105 -                            dp = writeescapedchar(dest, dp, hltr->word[i]);
   6.106 +                            dp = writeescapedchar(dest, dp, word.ptr[i]);
   6.107                          }
   6.108 -                        wp++;
   6.109 +                        word.length++;
   6.110                          dp = writeescapedchar(dest, dp, c);
   6.111                      } else {
   6.112                          dp = writeescapedchar(dest, dp, c);
     7.1 --- a/src/javacodegen.h	Tue Aug 23 15:55:02 2016 +0200
     7.2 +++ b/src/javacodegen.h	Tue Aug 23 16:34:02 2016 +0200
     7.3 @@ -41,7 +41,7 @@
     7.4  
     7.5  int check_jtype(char *word, size_t len);
     7.6  int check_jdirective(char *word);
     7.7 -void jparseline(char *src, UcxBuffer *dest, HighlighterData *hltr);
     7.8 +void jparseline(char *src, UcxBuffer *dest, int *mlc);
     7.9  
    7.10  
    7.11  #ifdef	__cplusplus

mercurial