src/javacodegen.c

changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
equal deleted inserted replaced
50:17408c3607ce 51:f25ba6fd7a08
37 "transient", "catch", "extends", "int", "short", "try", "char", "final", 37 "transient", "catch", "extends", "int", "short", "try", "char", "final",
38 "interface", "static", "void", "class", "finally", "long", "strictfp", 38 "interface", "static", "void", "class", "finally", "long", "strictfp",
39 "volatile", "const", "float", "native", "super", "while", NULL 39 "volatile", "const", "float", "native", "super", "while", NULL
40 }; 40 };
41 41
42 void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) { 42 void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
43 /* reset buffers without clearing them */
44 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
45 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
43 46
44 /* TODO: try to replace this buffer */ 47 /* alias the buffers for better handling */
45 char wordbuf[WORDBUF_SIZE]; 48 UcxBuffer *wbuf = hd->primary_buffer;
46 sstr_t word;
47 word.ptr = wordbuf; word.length = 0;
48 49
50 /* local information */
49 size_t sp = (size_t)-1; 51 size_t sp = (size_t)-1;
50 int isstring = 0, iscomment = 0, isimport = 0; 52 int isstring = 0, iscomment = 0, isimport = 0;
51 char quote = '\0'; 53 char quote = '\0';
52 int isescaping = 0; 54 int isescaping = 0;
53 55
54 if (*multiline_comment) { 56 if (hd->multiline_comment) {
55 iscomment = 1; 57 iscomment = 1;
56 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 58 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
57 } 59 }
58 60
59 char c; 61 char c;
61 c = src[++sp]; 63 c = src[++sp];
62 if (!c) break; 64 if (!c) break;
63 65
64 /* comments */ 66 /* comments */
65 if (!isstring && c == '/') { 67 if (!isstring && c == '/') {
66 if (*multiline_comment && sp > 0 && src[sp-1] == '*') { 68 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
67 iscomment = 0; 69 iscomment = 0;
68 *multiline_comment = 0; 70 hd->multiline_comment = 0;
69 ucx_buffer_puts(dest, "/</span>"); 71 ucx_buffer_puts(dest, "/</span>");
70 continue; 72 continue;
71 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 73 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
72 iscomment = 1; 74 iscomment = 1;
73 *multiline_comment = (src[sp+1] == '*'); 75 hd->multiline_comment = (src[sp+1] == '*');
74 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 76 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
75 } 77 }
76 } 78 }
77 79
78 if (iscomment) { 80 if (iscomment) {
102 put_htmlescaped(dest, c); 104 put_htmlescaped(dest, c);
103 } 105 }
104 } else { 106 } else {
105 if (isstring) { 107 if (isstring) {
106 put_htmlescaped(dest, c); 108 put_htmlescaped(dest, c);
107 } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') { 109 } else if (!isalnum(c) && c!='_' && c!='@') {
108 if (word.length > 0 && word.length < WORDBUF_SIZE) { 110 /* write buffered word, if any */
111 if (wbuf->size > 0) {
112 sstr_t word = sstrn(wbuf->space, wbuf->size);
109 int closespan = 1; 113 int closespan = 1;
110 if (check_keyword(word, jkeywords)) { 114 if (check_keyword(word, jkeywords)) {
111 ucx_buffer_puts(dest, 115 ucx_buffer_puts(dest,
112 "<span class=\"c2html-keyword\">"); 116 "<span class=\"c2html-keyword\">");
113 } else if (isupper(word.ptr[0])) { 117 } else if (isupper(word.ptr[0])) {
126 130
127 if (closespan) { 131 if (closespan) {
128 ucx_buffer_puts(dest, "</span>"); 132 ucx_buffer_puts(dest, "</span>");
129 } 133 }
130 } 134 }
131 word.length = 0; 135 wbuf->pos = wbuf->size = 0; /* reset buffer */
136
137 /* write current character */
132 put_htmlescaped(dest, c); 138 put_htmlescaped(dest, c);
133 } else { 139 } else {
134 /* read word */ 140 /* buffer the current word */
135 if (word.length < WORDBUF_SIZE) { 141 ucx_buffer_putc(wbuf, c);
136 word.ptr[word.length++] = c;
137 } else if (word.length == WORDBUF_SIZE) {
138 /* TODO: this will be removed */
139 ucx_buffer_puts(dest,
140 "!!! WARNING - WORD TOO LONG TO PARSE !!!");
141 word.length = 0;
142 } else {
143 put_htmlescaped(dest, c);
144 }
145 } 142 }
146 } 143 }
147 144
148 isescaping = !isescaping & (c == '\\'); 145 isescaping = !isescaping & (c == '\\');
149 } 146 }

mercurial